Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / hw / misc / bcm2835_cprman.c
blob75e6c574d46dc87076a205cceb00c5bdd76618f1
1 /*
2 * BCM2835 CPRMAN clock manager
4 * Copyright (c) 2020 Luc Michel <luc@lmichel.fr>
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
9 /*
10 * This peripheral is roughly divided into 3 main parts:
11 * - the PLLs
12 * - the PLL channels
13 * - the clock muxes
15 * A main oscillator (xosc) feeds all the PLLs. Each PLLs has one or more
16 * channels. Those channel are then connected to the clock muxes. Each mux has
17 * multiples sources (usually the xosc, some of the PLL channels and some "test
18 * debug" clocks). A mux is configured to select a given source through its
19 * control register. Each mux has one output clock that also goes out of the
20 * CPRMAN. This output clock usually connects to another peripheral in the SoC
21 * (so a given mux is dedicated to a peripheral).
23 * At each level (PLL, channel and mux), the clock can be altered through
24 * dividers (and multipliers in case of the PLLs), and can be disabled (in this
25 * case, the next levels see no clock).
27 * This can be sum-up as follows (this is an example and not the actual BCM2835
28 * clock tree):
30 * /-->[PLL]-|->[PLL channel]--... [mux]--> to peripherals
31 * | |->[PLL channel] muxes takes [mux]
32 * | \->[PLL channel] inputs from [mux]
33 * | some channels [mux]
34 * [xosc]---|-->[PLL]-|->[PLL channel] and other srcs [mux]
35 * | \->[PLL channel] ...-->[mux]
36 * | [mux]
37 * \-->[PLL]--->[PLL channel] [mux]
39 * The page at https://elinux.org/The_Undocumented_Pi gives the actual clock
40 * tree configuration.
42 * The CPRMAN exposes clock outputs with the name of the clock mux suffixed
43 * with "-out" (e.g. "uart-out", "h264-out", ...).
46 #include "qemu/osdep.h"
47 #include "qemu/log.h"
48 #include "migration/vmstate.h"
49 #include "hw/qdev-properties.h"
50 #include "hw/misc/bcm2835_cprman.h"
51 #include "hw/misc/bcm2835_cprman_internals.h"
52 #include "trace.h"
54 /* PLL */
56 static void pll_reset(DeviceState *dev)
58 CprmanPllState *s = CPRMAN_PLL(dev);
59 const PLLResetInfo *info = &PLL_RESET_INFO[s->id];
61 *s->reg_cm = info->cm;
62 *s->reg_a2w_ctrl = info->a2w_ctrl;
63 memcpy(s->reg_a2w_ana, info->a2w_ana, sizeof(info->a2w_ana));
64 *s->reg_a2w_frac = info->a2w_frac;
67 static bool pll_is_locked(const CprmanPllState *pll)
69 return !FIELD_EX32(*pll->reg_a2w_ctrl, A2W_PLLx_CTRL, PWRDN)
70 && !FIELD_EX32(*pll->reg_cm, CM_PLLx, ANARST);
73 static void pll_update(CprmanPllState *pll)
75 uint64_t freq, ndiv, fdiv, pdiv;
77 if (!pll_is_locked(pll)) {
78 clock_update(pll->out, 0);
79 return;
82 pdiv = FIELD_EX32(*pll->reg_a2w_ctrl, A2W_PLLx_CTRL, PDIV);
84 if (!pdiv) {
85 clock_update(pll->out, 0);
86 return;
89 ndiv = FIELD_EX32(*pll->reg_a2w_ctrl, A2W_PLLx_CTRL, NDIV);
90 fdiv = FIELD_EX32(*pll->reg_a2w_frac, A2W_PLLx_FRAC, FRAC);
92 if (pll->reg_a2w_ana[1] & pll->prediv_mask) {
93 /* The prescaler doubles the parent frequency */
94 ndiv *= 2;
95 fdiv *= 2;
99 * We have a multiplier with an integer part (ndiv) and a fractional part
100 * (fdiv), and a divider (pdiv).
102 freq = clock_get_hz(pll->xosc_in) *
103 ((ndiv << R_A2W_PLLx_FRAC_FRAC_LENGTH) + fdiv);
104 freq /= pdiv;
105 freq >>= R_A2W_PLLx_FRAC_FRAC_LENGTH;
107 clock_update_hz(pll->out, freq);
110 static void pll_xosc_update(void *opaque, ClockEvent event)
112 pll_update(CPRMAN_PLL(opaque));
115 static void pll_init(Object *obj)
117 CprmanPllState *s = CPRMAN_PLL(obj);
119 s->xosc_in = qdev_init_clock_in(DEVICE(s), "xosc-in", pll_xosc_update,
120 s, ClockUpdate);
121 s->out = qdev_init_clock_out(DEVICE(s), "out");
124 static const VMStateDescription pll_vmstate = {
125 .name = TYPE_CPRMAN_PLL,
126 .version_id = 1,
127 .minimum_version_id = 1,
128 .fields = (VMStateField[]) {
129 VMSTATE_CLOCK(xosc_in, CprmanPllState),
130 VMSTATE_END_OF_LIST()
134 static void pll_class_init(ObjectClass *klass, void *data)
136 DeviceClass *dc = DEVICE_CLASS(klass);
138 dc->reset = pll_reset;
139 dc->vmsd = &pll_vmstate;
142 static const TypeInfo cprman_pll_info = {
143 .name = TYPE_CPRMAN_PLL,
144 .parent = TYPE_DEVICE,
145 .instance_size = sizeof(CprmanPllState),
146 .class_init = pll_class_init,
147 .instance_init = pll_init,
151 /* PLL channel */
153 static void pll_channel_reset(DeviceState *dev)
155 CprmanPllChannelState *s = CPRMAN_PLL_CHANNEL(dev);
156 const PLLChannelResetInfo *info = &PLL_CHANNEL_RESET_INFO[s->id];
158 *s->reg_a2w_ctrl = info->a2w_ctrl;
161 static bool pll_channel_is_enabled(CprmanPllChannelState *channel)
164 * XXX I'm not sure of the purpose of the LOAD field. The Linux driver does
165 * not set it when enabling the channel, but does clear it when disabling
166 * it.
168 return !FIELD_EX32(*channel->reg_a2w_ctrl, A2W_PLLx_CHANNELy, DISABLE)
169 && !(*channel->reg_cm & channel->hold_mask);
172 static void pll_channel_update(CprmanPllChannelState *channel)
174 uint64_t freq, div;
176 if (!pll_channel_is_enabled(channel)) {
177 clock_update(channel->out, 0);
178 return;
181 div = FIELD_EX32(*channel->reg_a2w_ctrl, A2W_PLLx_CHANNELy, DIV);
183 if (!div) {
185 * It seems that when the divider value is 0, it is considered as
186 * being maximum by the hardware (see the Linux driver).
188 div = R_A2W_PLLx_CHANNELy_DIV_MASK;
191 /* Some channels have an additional fixed divider */
192 freq = clock_get_hz(channel->pll_in) / (div * channel->fixed_divider);
194 clock_update_hz(channel->out, freq);
197 /* Update a PLL and all its channels */
198 static void pll_update_all_channels(BCM2835CprmanState *s,
199 CprmanPllState *pll)
201 size_t i;
203 pll_update(pll);
205 for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
206 CprmanPllChannelState *channel = &s->channels[i];
207 if (channel->parent == pll->id) {
208 pll_channel_update(channel);
213 static void pll_channel_pll_in_update(void *opaque, ClockEvent event)
215 pll_channel_update(CPRMAN_PLL_CHANNEL(opaque));
218 static void pll_channel_init(Object *obj)
220 CprmanPllChannelState *s = CPRMAN_PLL_CHANNEL(obj);
222 s->pll_in = qdev_init_clock_in(DEVICE(s), "pll-in",
223 pll_channel_pll_in_update, s,
224 ClockUpdate);
225 s->out = qdev_init_clock_out(DEVICE(s), "out");
228 static const VMStateDescription pll_channel_vmstate = {
229 .name = TYPE_CPRMAN_PLL_CHANNEL,
230 .version_id = 1,
231 .minimum_version_id = 1,
232 .fields = (VMStateField[]) {
233 VMSTATE_CLOCK(pll_in, CprmanPllChannelState),
234 VMSTATE_END_OF_LIST()
238 static void pll_channel_class_init(ObjectClass *klass, void *data)
240 DeviceClass *dc = DEVICE_CLASS(klass);
242 dc->reset = pll_channel_reset;
243 dc->vmsd = &pll_channel_vmstate;
246 static const TypeInfo cprman_pll_channel_info = {
247 .name = TYPE_CPRMAN_PLL_CHANNEL,
248 .parent = TYPE_DEVICE,
249 .instance_size = sizeof(CprmanPllChannelState),
250 .class_init = pll_channel_class_init,
251 .instance_init = pll_channel_init,
255 /* clock mux */
257 static bool clock_mux_is_enabled(CprmanClockMuxState *mux)
259 return FIELD_EX32(*mux->reg_ctl, CM_CLOCKx_CTL, ENABLE);
262 static void clock_mux_update(CprmanClockMuxState *mux)
264 uint64_t freq;
265 uint32_t div, src = FIELD_EX32(*mux->reg_ctl, CM_CLOCKx_CTL, SRC);
266 bool enabled = clock_mux_is_enabled(mux);
268 *mux->reg_ctl = FIELD_DP32(*mux->reg_ctl, CM_CLOCKx_CTL, BUSY, enabled);
270 if (!enabled) {
271 clock_update(mux->out, 0);
272 return;
275 freq = clock_get_hz(mux->srcs[src]);
277 if (mux->int_bits == 0 && mux->frac_bits == 0) {
278 clock_update_hz(mux->out, freq);
279 return;
283 * The divider has an integer and a fractional part. The size of each part
284 * varies with the muxes (int_bits and frac_bits). Both parts are
285 * concatenated, with the integer part always starting at bit 12.
287 * 31 12 11 0
288 * ------------------------------
289 * CM_DIV | | int | frac | |
290 * ------------------------------
291 * <-----> <------>
292 * int_bits frac_bits
294 div = extract32(*mux->reg_div,
295 R_CM_CLOCKx_DIV_FRAC_LENGTH - mux->frac_bits,
296 mux->int_bits + mux->frac_bits);
298 if (!div) {
299 clock_update(mux->out, 0);
300 return;
303 freq = muldiv64(freq, 1 << mux->frac_bits, div);
305 clock_update_hz(mux->out, freq);
308 static void clock_mux_src_update(void *opaque, ClockEvent event)
310 CprmanClockMuxState **backref = opaque;
311 CprmanClockMuxState *s = *backref;
312 CprmanClockMuxSource src = backref - s->backref;
314 if (FIELD_EX32(*s->reg_ctl, CM_CLOCKx_CTL, SRC) != src) {
315 return;
318 clock_mux_update(s);
321 static void clock_mux_reset(DeviceState *dev)
323 CprmanClockMuxState *clock = CPRMAN_CLOCK_MUX(dev);
324 const ClockMuxResetInfo *info = &CLOCK_MUX_RESET_INFO[clock->id];
326 *clock->reg_ctl = info->cm_ctl;
327 *clock->reg_div = info->cm_div;
330 static void clock_mux_init(Object *obj)
332 CprmanClockMuxState *s = CPRMAN_CLOCK_MUX(obj);
333 size_t i;
335 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX_SRC; i++) {
336 char *name = g_strdup_printf("srcs[%zu]", i);
337 s->backref[i] = s;
338 s->srcs[i] = qdev_init_clock_in(DEVICE(s), name,
339 clock_mux_src_update,
340 &s->backref[i],
341 ClockUpdate);
342 g_free(name);
345 s->out = qdev_init_clock_out(DEVICE(s), "out");
348 static const VMStateDescription clock_mux_vmstate = {
349 .name = TYPE_CPRMAN_CLOCK_MUX,
350 .version_id = 1,
351 .minimum_version_id = 1,
352 .fields = (VMStateField[]) {
353 VMSTATE_ARRAY_CLOCK(srcs, CprmanClockMuxState,
354 CPRMAN_NUM_CLOCK_MUX_SRC),
355 VMSTATE_END_OF_LIST()
359 static void clock_mux_class_init(ObjectClass *klass, void *data)
361 DeviceClass *dc = DEVICE_CLASS(klass);
363 dc->reset = clock_mux_reset;
364 dc->vmsd = &clock_mux_vmstate;
367 static const TypeInfo cprman_clock_mux_info = {
368 .name = TYPE_CPRMAN_CLOCK_MUX,
369 .parent = TYPE_DEVICE,
370 .instance_size = sizeof(CprmanClockMuxState),
371 .class_init = clock_mux_class_init,
372 .instance_init = clock_mux_init,
376 /* DSI0HSCK mux */
378 static void dsi0hsck_mux_update(CprmanDsi0HsckMuxState *s)
380 bool src_is_plld = FIELD_EX32(*s->reg_cm, CM_DSI0HSCK, SELPLLD);
381 Clock *src = src_is_plld ? s->plld_in : s->plla_in;
383 clock_update(s->out, clock_get(src));
386 static void dsi0hsck_mux_in_update(void *opaque, ClockEvent event)
388 dsi0hsck_mux_update(CPRMAN_DSI0HSCK_MUX(opaque));
391 static void dsi0hsck_mux_init(Object *obj)
393 CprmanDsi0HsckMuxState *s = CPRMAN_DSI0HSCK_MUX(obj);
394 DeviceState *dev = DEVICE(obj);
396 s->plla_in = qdev_init_clock_in(dev, "plla-in", dsi0hsck_mux_in_update,
397 s, ClockUpdate);
398 s->plld_in = qdev_init_clock_in(dev, "plld-in", dsi0hsck_mux_in_update,
399 s, ClockUpdate);
400 s->out = qdev_init_clock_out(DEVICE(s), "out");
403 static const VMStateDescription dsi0hsck_mux_vmstate = {
404 .name = TYPE_CPRMAN_DSI0HSCK_MUX,
405 .version_id = 1,
406 .minimum_version_id = 1,
407 .fields = (VMStateField[]) {
408 VMSTATE_CLOCK(plla_in, CprmanDsi0HsckMuxState),
409 VMSTATE_CLOCK(plld_in, CprmanDsi0HsckMuxState),
410 VMSTATE_END_OF_LIST()
414 static void dsi0hsck_mux_class_init(ObjectClass *klass, void *data)
416 DeviceClass *dc = DEVICE_CLASS(klass);
418 dc->vmsd = &dsi0hsck_mux_vmstate;
421 static const TypeInfo cprman_dsi0hsck_mux_info = {
422 .name = TYPE_CPRMAN_DSI0HSCK_MUX,
423 .parent = TYPE_DEVICE,
424 .instance_size = sizeof(CprmanDsi0HsckMuxState),
425 .class_init = dsi0hsck_mux_class_init,
426 .instance_init = dsi0hsck_mux_init,
430 /* CPRMAN "top level" model */
432 static uint32_t get_cm_lock(const BCM2835CprmanState *s)
434 static const int CM_LOCK_MAPPING[CPRMAN_NUM_PLL] = {
435 [CPRMAN_PLLA] = R_CM_LOCK_FLOCKA_SHIFT,
436 [CPRMAN_PLLC] = R_CM_LOCK_FLOCKC_SHIFT,
437 [CPRMAN_PLLD] = R_CM_LOCK_FLOCKD_SHIFT,
438 [CPRMAN_PLLH] = R_CM_LOCK_FLOCKH_SHIFT,
439 [CPRMAN_PLLB] = R_CM_LOCK_FLOCKB_SHIFT,
442 uint32_t r = 0;
443 size_t i;
445 for (i = 0; i < CPRMAN_NUM_PLL; i++) {
446 r |= pll_is_locked(&s->plls[i]) << CM_LOCK_MAPPING[i];
449 return r;
452 static uint64_t cprman_read(void *opaque, hwaddr offset,
453 unsigned size)
455 BCM2835CprmanState *s = CPRMAN(opaque);
456 uint64_t r = 0;
457 size_t idx = offset / sizeof(uint32_t);
459 switch (idx) {
460 case R_CM_LOCK:
461 r = get_cm_lock(s);
462 break;
464 default:
465 r = s->regs[idx];
468 trace_bcm2835_cprman_read(offset, r);
469 return r;
472 static inline void update_pll_and_channels_from_cm(BCM2835CprmanState *s,
473 size_t idx)
475 size_t i;
477 for (i = 0; i < CPRMAN_NUM_PLL; i++) {
478 if (PLL_INIT_INFO[i].cm_offset == idx) {
479 pll_update_all_channels(s, &s->plls[i]);
480 return;
485 static inline void update_channel_from_a2w(BCM2835CprmanState *s, size_t idx)
487 size_t i;
489 for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
490 if (PLL_CHANNEL_INIT_INFO[i].a2w_ctrl_offset == idx) {
491 pll_channel_update(&s->channels[i]);
492 return;
497 static inline void update_mux_from_cm(BCM2835CprmanState *s, size_t idx)
499 size_t i;
501 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
502 if ((CLOCK_MUX_INIT_INFO[i].cm_offset == idx) ||
503 (CLOCK_MUX_INIT_INFO[i].cm_offset + 4 == idx)) {
504 /* matches CM_CTL or CM_DIV mux register */
505 clock_mux_update(&s->clock_muxes[i]);
506 return;
511 #define CASE_PLL_A2W_REGS(pll_) \
512 case R_A2W_ ## pll_ ## _CTRL: \
513 case R_A2W_ ## pll_ ## _ANA0: \
514 case R_A2W_ ## pll_ ## _ANA1: \
515 case R_A2W_ ## pll_ ## _ANA2: \
516 case R_A2W_ ## pll_ ## _ANA3: \
517 case R_A2W_ ## pll_ ## _FRAC
519 static void cprman_write(void *opaque, hwaddr offset,
520 uint64_t value, unsigned size)
522 BCM2835CprmanState *s = CPRMAN(opaque);
523 size_t idx = offset / sizeof(uint32_t);
525 if (FIELD_EX32(value, CPRMAN, PASSWORD) != CPRMAN_PASSWORD) {
526 trace_bcm2835_cprman_write_invalid_magic(offset, value);
527 return;
530 value &= ~R_CPRMAN_PASSWORD_MASK;
532 trace_bcm2835_cprman_write(offset, value);
533 s->regs[idx] = value;
535 switch (idx) {
536 case R_CM_PLLA ... R_CM_PLLH:
537 case R_CM_PLLB:
539 * A given CM_PLLx register is shared by both the PLL and the channels
540 * of this PLL.
542 update_pll_and_channels_from_cm(s, idx);
543 break;
545 CASE_PLL_A2W_REGS(PLLA) :
546 pll_update(&s->plls[CPRMAN_PLLA]);
547 break;
549 CASE_PLL_A2W_REGS(PLLC) :
550 pll_update(&s->plls[CPRMAN_PLLC]);
551 break;
553 CASE_PLL_A2W_REGS(PLLD) :
554 pll_update(&s->plls[CPRMAN_PLLD]);
555 break;
557 CASE_PLL_A2W_REGS(PLLH) :
558 pll_update(&s->plls[CPRMAN_PLLH]);
559 break;
561 CASE_PLL_A2W_REGS(PLLB) :
562 pll_update(&s->plls[CPRMAN_PLLB]);
563 break;
565 case R_A2W_PLLA_DSI0:
566 case R_A2W_PLLA_CORE:
567 case R_A2W_PLLA_PER:
568 case R_A2W_PLLA_CCP2:
569 case R_A2W_PLLC_CORE2:
570 case R_A2W_PLLC_CORE1:
571 case R_A2W_PLLC_PER:
572 case R_A2W_PLLC_CORE0:
573 case R_A2W_PLLD_DSI0:
574 case R_A2W_PLLD_CORE:
575 case R_A2W_PLLD_PER:
576 case R_A2W_PLLD_DSI1:
577 case R_A2W_PLLH_AUX:
578 case R_A2W_PLLH_RCAL:
579 case R_A2W_PLLH_PIX:
580 case R_A2W_PLLB_ARM:
581 update_channel_from_a2w(s, idx);
582 break;
584 case R_CM_GNRICCTL ... R_CM_SMIDIV:
585 case R_CM_TCNTCNT ... R_CM_VECDIV:
586 case R_CM_PULSECTL ... R_CM_PULSEDIV:
587 case R_CM_SDCCTL ... R_CM_ARMCTL:
588 case R_CM_AVEOCTL ... R_CM_EMMCDIV:
589 case R_CM_EMMC2CTL ... R_CM_EMMC2DIV:
590 update_mux_from_cm(s, idx);
591 break;
593 case R_CM_DSI0HSCK:
594 dsi0hsck_mux_update(&s->dsi0hsck_mux);
595 break;
599 #undef CASE_PLL_A2W_REGS
601 static const MemoryRegionOps cprman_ops = {
602 .read = cprman_read,
603 .write = cprman_write,
604 .endianness = DEVICE_LITTLE_ENDIAN,
605 .valid = {
607 * Although this hasn't been checked against real hardware, nor the
608 * information can be found in a datasheet, it seems reasonable because
609 * of the "PASSWORD" magic value found in every registers.
611 .min_access_size = 4,
612 .max_access_size = 4,
613 .unaligned = false,
615 .impl = {
616 .max_access_size = 4,
620 static void cprman_reset(DeviceState *dev)
622 BCM2835CprmanState *s = CPRMAN(dev);
623 size_t i;
625 memset(s->regs, 0, sizeof(s->regs));
627 for (i = 0; i < CPRMAN_NUM_PLL; i++) {
628 device_cold_reset(DEVICE(&s->plls[i]));
631 for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
632 device_cold_reset(DEVICE(&s->channels[i]));
635 device_cold_reset(DEVICE(&s->dsi0hsck_mux));
637 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
638 device_cold_reset(DEVICE(&s->clock_muxes[i]));
641 clock_update_hz(s->xosc, s->xosc_freq);
644 static void cprman_init(Object *obj)
646 BCM2835CprmanState *s = CPRMAN(obj);
647 size_t i;
649 for (i = 0; i < CPRMAN_NUM_PLL; i++) {
650 object_initialize_child(obj, PLL_INIT_INFO[i].name,
651 &s->plls[i], TYPE_CPRMAN_PLL);
652 set_pll_init_info(s, &s->plls[i], i);
655 for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
656 object_initialize_child(obj, PLL_CHANNEL_INIT_INFO[i].name,
657 &s->channels[i],
658 TYPE_CPRMAN_PLL_CHANNEL);
659 set_pll_channel_init_info(s, &s->channels[i], i);
662 object_initialize_child(obj, "dsi0hsck-mux",
663 &s->dsi0hsck_mux, TYPE_CPRMAN_DSI0HSCK_MUX);
664 s->dsi0hsck_mux.reg_cm = &s->regs[R_CM_DSI0HSCK];
666 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
667 char *alias;
669 object_initialize_child(obj, CLOCK_MUX_INIT_INFO[i].name,
670 &s->clock_muxes[i],
671 TYPE_CPRMAN_CLOCK_MUX);
672 set_clock_mux_init_info(s, &s->clock_muxes[i], i);
674 /* Expose muxes output as CPRMAN outputs */
675 alias = g_strdup_printf("%s-out", CLOCK_MUX_INIT_INFO[i].name);
676 qdev_alias_clock(DEVICE(&s->clock_muxes[i]), "out", DEVICE(obj), alias);
677 g_free(alias);
680 s->xosc = clock_new(obj, "xosc");
681 s->gnd = clock_new(obj, "gnd");
683 clock_set(s->gnd, 0);
685 memory_region_init_io(&s->iomem, obj, &cprman_ops,
686 s, "bcm2835-cprman", 0x2000);
687 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
690 static void connect_mux_sources(BCM2835CprmanState *s,
691 CprmanClockMuxState *mux,
692 const CprmanPllChannel *clk_mapping)
694 size_t i;
695 Clock *td0 = s->clock_muxes[CPRMAN_CLOCK_TD0].out;
696 Clock *td1 = s->clock_muxes[CPRMAN_CLOCK_TD1].out;
698 /* For sources from 0 to 3. Source 4 to 9 are mux specific */
699 Clock * const CLK_SRC_MAPPING[] = {
700 [CPRMAN_CLOCK_SRC_GND] = s->gnd,
701 [CPRMAN_CLOCK_SRC_XOSC] = s->xosc,
702 [CPRMAN_CLOCK_SRC_TD0] = td0,
703 [CPRMAN_CLOCK_SRC_TD1] = td1,
706 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX_SRC; i++) {
707 CprmanPllChannel mapping = clk_mapping[i];
708 Clock *src;
710 if (mapping == CPRMAN_CLOCK_SRC_FORCE_GROUND) {
711 src = s->gnd;
712 } else if (mapping == CPRMAN_CLOCK_SRC_DSI0HSCK) {
713 src = s->dsi0hsck_mux.out;
714 } else if (i < CPRMAN_CLOCK_SRC_PLLA) {
715 src = CLK_SRC_MAPPING[i];
716 } else {
717 src = s->channels[mapping].out;
720 clock_set_source(mux->srcs[i], src);
724 static void cprman_realize(DeviceState *dev, Error **errp)
726 BCM2835CprmanState *s = CPRMAN(dev);
727 size_t i;
729 for (i = 0; i < CPRMAN_NUM_PLL; i++) {
730 CprmanPllState *pll = &s->plls[i];
732 clock_set_source(pll->xosc_in, s->xosc);
734 if (!qdev_realize(DEVICE(pll), NULL, errp)) {
735 return;
739 for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
740 CprmanPllChannelState *channel = &s->channels[i];
741 CprmanPll parent = PLL_CHANNEL_INIT_INFO[i].parent;
742 Clock *parent_clk = s->plls[parent].out;
744 clock_set_source(channel->pll_in, parent_clk);
746 if (!qdev_realize(DEVICE(channel), NULL, errp)) {
747 return;
751 clock_set_source(s->dsi0hsck_mux.plla_in,
752 s->channels[CPRMAN_PLLA_CHANNEL_DSI0].out);
753 clock_set_source(s->dsi0hsck_mux.plld_in,
754 s->channels[CPRMAN_PLLD_CHANNEL_DSI0].out);
756 if (!qdev_realize(DEVICE(&s->dsi0hsck_mux), NULL, errp)) {
757 return;
760 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
761 CprmanClockMuxState *clock_mux = &s->clock_muxes[i];
763 connect_mux_sources(s, clock_mux, CLOCK_MUX_INIT_INFO[i].src_mapping);
765 if (!qdev_realize(DEVICE(clock_mux), NULL, errp)) {
766 return;
771 static const VMStateDescription cprman_vmstate = {
772 .name = TYPE_BCM2835_CPRMAN,
773 .version_id = 1,
774 .minimum_version_id = 1,
775 .fields = (VMStateField[]) {
776 VMSTATE_UINT32_ARRAY(regs, BCM2835CprmanState, CPRMAN_NUM_REGS),
777 VMSTATE_END_OF_LIST()
781 static Property cprman_properties[] = {
782 DEFINE_PROP_UINT32("xosc-freq-hz", BCM2835CprmanState, xosc_freq, 19200000),
783 DEFINE_PROP_END_OF_LIST()
786 static void cprman_class_init(ObjectClass *klass, void *data)
788 DeviceClass *dc = DEVICE_CLASS(klass);
790 dc->realize = cprman_realize;
791 dc->reset = cprman_reset;
792 dc->vmsd = &cprman_vmstate;
793 device_class_set_props(dc, cprman_properties);
796 static const TypeInfo cprman_info = {
797 .name = TYPE_BCM2835_CPRMAN,
798 .parent = TYPE_SYS_BUS_DEVICE,
799 .instance_size = sizeof(BCM2835CprmanState),
800 .class_init = cprman_class_init,
801 .instance_init = cprman_init,
804 static void cprman_register_types(void)
806 type_register_static(&cprman_info);
807 type_register_static(&cprman_pll_info);
808 type_register_static(&cprman_pll_channel_info);
809 type_register_static(&cprman_clock_mux_info);
810 type_register_static(&cprman_dsi0hsck_mux_info);
813 type_init(cprman_register_types);