spapr: Do PCI device hotplug sanity checks at pre-plug only
[qemu/ar7.git] / hw / misc / bcm2835_cprman.c
blob7e415a017c904ca2eb2181c904c6945a5220ca86
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)
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, s);
120 s->out = qdev_init_clock_out(DEVICE(s), "out");
123 static const VMStateDescription pll_vmstate = {
124 .name = TYPE_CPRMAN_PLL,
125 .version_id = 1,
126 .minimum_version_id = 1,
127 .fields = (VMStateField[]) {
128 VMSTATE_CLOCK(xosc_in, CprmanPllState),
129 VMSTATE_END_OF_LIST()
133 static void pll_class_init(ObjectClass *klass, void *data)
135 DeviceClass *dc = DEVICE_CLASS(klass);
137 dc->reset = pll_reset;
138 dc->vmsd = &pll_vmstate;
141 static const TypeInfo cprman_pll_info = {
142 .name = TYPE_CPRMAN_PLL,
143 .parent = TYPE_DEVICE,
144 .instance_size = sizeof(CprmanPllState),
145 .class_init = pll_class_init,
146 .instance_init = pll_init,
150 /* PLL channel */
152 static void pll_channel_reset(DeviceState *dev)
154 CprmanPllChannelState *s = CPRMAN_PLL_CHANNEL(dev);
155 const PLLChannelResetInfo *info = &PLL_CHANNEL_RESET_INFO[s->id];
157 *s->reg_a2w_ctrl = info->a2w_ctrl;
160 static bool pll_channel_is_enabled(CprmanPllChannelState *channel)
163 * XXX I'm not sure of the purpose of the LOAD field. The Linux driver does
164 * not set it when enabling the channel, but does clear it when disabling
165 * it.
167 return !FIELD_EX32(*channel->reg_a2w_ctrl, A2W_PLLx_CHANNELy, DISABLE)
168 && !(*channel->reg_cm & channel->hold_mask);
171 static void pll_channel_update(CprmanPllChannelState *channel)
173 uint64_t freq, div;
175 if (!pll_channel_is_enabled(channel)) {
176 clock_update(channel->out, 0);
177 return;
180 div = FIELD_EX32(*channel->reg_a2w_ctrl, A2W_PLLx_CHANNELy, DIV);
182 if (!div) {
184 * It seems that when the divider value is 0, it is considered as
185 * being maximum by the hardware (see the Linux driver).
187 div = R_A2W_PLLx_CHANNELy_DIV_MASK;
190 /* Some channels have an additional fixed divider */
191 freq = clock_get_hz(channel->pll_in) / (div * channel->fixed_divider);
193 clock_update_hz(channel->out, freq);
196 /* Update a PLL and all its channels */
197 static void pll_update_all_channels(BCM2835CprmanState *s,
198 CprmanPllState *pll)
200 size_t i;
202 pll_update(pll);
204 for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
205 CprmanPllChannelState *channel = &s->channels[i];
206 if (channel->parent == pll->id) {
207 pll_channel_update(channel);
212 static void pll_channel_pll_in_update(void *opaque)
214 pll_channel_update(CPRMAN_PLL_CHANNEL(opaque));
217 static void pll_channel_init(Object *obj)
219 CprmanPllChannelState *s = CPRMAN_PLL_CHANNEL(obj);
221 s->pll_in = qdev_init_clock_in(DEVICE(s), "pll-in",
222 pll_channel_pll_in_update, s);
223 s->out = qdev_init_clock_out(DEVICE(s), "out");
226 static const VMStateDescription pll_channel_vmstate = {
227 .name = TYPE_CPRMAN_PLL_CHANNEL,
228 .version_id = 1,
229 .minimum_version_id = 1,
230 .fields = (VMStateField[]) {
231 VMSTATE_CLOCK(pll_in, CprmanPllChannelState),
232 VMSTATE_END_OF_LIST()
236 static void pll_channel_class_init(ObjectClass *klass, void *data)
238 DeviceClass *dc = DEVICE_CLASS(klass);
240 dc->reset = pll_channel_reset;
241 dc->vmsd = &pll_channel_vmstate;
244 static const TypeInfo cprman_pll_channel_info = {
245 .name = TYPE_CPRMAN_PLL_CHANNEL,
246 .parent = TYPE_DEVICE,
247 .instance_size = sizeof(CprmanPllChannelState),
248 .class_init = pll_channel_class_init,
249 .instance_init = pll_channel_init,
253 /* clock mux */
255 static bool clock_mux_is_enabled(CprmanClockMuxState *mux)
257 return FIELD_EX32(*mux->reg_ctl, CM_CLOCKx_CTL, ENABLE);
260 static void clock_mux_update(CprmanClockMuxState *mux)
262 uint64_t freq;
263 uint32_t div, src = FIELD_EX32(*mux->reg_ctl, CM_CLOCKx_CTL, SRC);
264 bool enabled = clock_mux_is_enabled(mux);
266 *mux->reg_ctl = FIELD_DP32(*mux->reg_ctl, CM_CLOCKx_CTL, BUSY, enabled);
268 if (!enabled) {
269 clock_update(mux->out, 0);
270 return;
273 freq = clock_get_hz(mux->srcs[src]);
275 if (mux->int_bits == 0 && mux->frac_bits == 0) {
276 clock_update_hz(mux->out, freq);
277 return;
281 * The divider has an integer and a fractional part. The size of each part
282 * varies with the muxes (int_bits and frac_bits). Both parts are
283 * concatenated, with the integer part always starting at bit 12.
285 * 31 12 11 0
286 * ------------------------------
287 * CM_DIV | | int | frac | |
288 * ------------------------------
289 * <-----> <------>
290 * int_bits frac_bits
292 div = extract32(*mux->reg_div,
293 R_CM_CLOCKx_DIV_FRAC_LENGTH - mux->frac_bits,
294 mux->int_bits + mux->frac_bits);
296 if (!div) {
297 clock_update(mux->out, 0);
298 return;
301 freq = muldiv64(freq, 1 << mux->frac_bits, div);
303 clock_update_hz(mux->out, freq);
306 static void clock_mux_src_update(void *opaque)
308 CprmanClockMuxState **backref = opaque;
309 CprmanClockMuxState *s = *backref;
310 CprmanClockMuxSource src = backref - s->backref;
312 if (FIELD_EX32(*s->reg_ctl, CM_CLOCKx_CTL, SRC) != src) {
313 return;
316 clock_mux_update(s);
319 static void clock_mux_reset(DeviceState *dev)
321 CprmanClockMuxState *clock = CPRMAN_CLOCK_MUX(dev);
322 const ClockMuxResetInfo *info = &CLOCK_MUX_RESET_INFO[clock->id];
324 *clock->reg_ctl = info->cm_ctl;
325 *clock->reg_div = info->cm_div;
328 static void clock_mux_init(Object *obj)
330 CprmanClockMuxState *s = CPRMAN_CLOCK_MUX(obj);
331 size_t i;
333 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX_SRC; i++) {
334 char *name = g_strdup_printf("srcs[%zu]", i);
335 s->backref[i] = s;
336 s->srcs[i] = qdev_init_clock_in(DEVICE(s), name,
337 clock_mux_src_update,
338 &s->backref[i]);
339 g_free(name);
342 s->out = qdev_init_clock_out(DEVICE(s), "out");
345 static const VMStateDescription clock_mux_vmstate = {
346 .name = TYPE_CPRMAN_CLOCK_MUX,
347 .version_id = 1,
348 .minimum_version_id = 1,
349 .fields = (VMStateField[]) {
350 VMSTATE_ARRAY_CLOCK(srcs, CprmanClockMuxState,
351 CPRMAN_NUM_CLOCK_MUX_SRC),
352 VMSTATE_END_OF_LIST()
356 static void clock_mux_class_init(ObjectClass *klass, void *data)
358 DeviceClass *dc = DEVICE_CLASS(klass);
360 dc->reset = clock_mux_reset;
361 dc->vmsd = &clock_mux_vmstate;
364 static const TypeInfo cprman_clock_mux_info = {
365 .name = TYPE_CPRMAN_CLOCK_MUX,
366 .parent = TYPE_DEVICE,
367 .instance_size = sizeof(CprmanClockMuxState),
368 .class_init = clock_mux_class_init,
369 .instance_init = clock_mux_init,
373 /* DSI0HSCK mux */
375 static void dsi0hsck_mux_update(CprmanDsi0HsckMuxState *s)
377 bool src_is_plld = FIELD_EX32(*s->reg_cm, CM_DSI0HSCK, SELPLLD);
378 Clock *src = src_is_plld ? s->plld_in : s->plla_in;
380 clock_update(s->out, clock_get(src));
383 static void dsi0hsck_mux_in_update(void *opaque)
385 dsi0hsck_mux_update(CPRMAN_DSI0HSCK_MUX(opaque));
388 static void dsi0hsck_mux_init(Object *obj)
390 CprmanDsi0HsckMuxState *s = CPRMAN_DSI0HSCK_MUX(obj);
391 DeviceState *dev = DEVICE(obj);
393 s->plla_in = qdev_init_clock_in(dev, "plla-in", dsi0hsck_mux_in_update, s);
394 s->plld_in = qdev_init_clock_in(dev, "plld-in", dsi0hsck_mux_in_update, s);
395 s->out = qdev_init_clock_out(DEVICE(s), "out");
398 static const VMStateDescription dsi0hsck_mux_vmstate = {
399 .name = TYPE_CPRMAN_DSI0HSCK_MUX,
400 .version_id = 1,
401 .minimum_version_id = 1,
402 .fields = (VMStateField[]) {
403 VMSTATE_CLOCK(plla_in, CprmanDsi0HsckMuxState),
404 VMSTATE_CLOCK(plld_in, CprmanDsi0HsckMuxState),
405 VMSTATE_END_OF_LIST()
409 static void dsi0hsck_mux_class_init(ObjectClass *klass, void *data)
411 DeviceClass *dc = DEVICE_CLASS(klass);
413 dc->vmsd = &dsi0hsck_mux_vmstate;
416 static const TypeInfo cprman_dsi0hsck_mux_info = {
417 .name = TYPE_CPRMAN_DSI0HSCK_MUX,
418 .parent = TYPE_DEVICE,
419 .instance_size = sizeof(CprmanDsi0HsckMuxState),
420 .class_init = dsi0hsck_mux_class_init,
421 .instance_init = dsi0hsck_mux_init,
425 /* CPRMAN "top level" model */
427 static uint32_t get_cm_lock(const BCM2835CprmanState *s)
429 static const int CM_LOCK_MAPPING[CPRMAN_NUM_PLL] = {
430 [CPRMAN_PLLA] = R_CM_LOCK_FLOCKA_SHIFT,
431 [CPRMAN_PLLC] = R_CM_LOCK_FLOCKC_SHIFT,
432 [CPRMAN_PLLD] = R_CM_LOCK_FLOCKD_SHIFT,
433 [CPRMAN_PLLH] = R_CM_LOCK_FLOCKH_SHIFT,
434 [CPRMAN_PLLB] = R_CM_LOCK_FLOCKB_SHIFT,
437 uint32_t r = 0;
438 size_t i;
440 for (i = 0; i < CPRMAN_NUM_PLL; i++) {
441 r |= pll_is_locked(&s->plls[i]) << CM_LOCK_MAPPING[i];
444 return r;
447 static uint64_t cprman_read(void *opaque, hwaddr offset,
448 unsigned size)
450 BCM2835CprmanState *s = CPRMAN(opaque);
451 uint64_t r = 0;
452 size_t idx = offset / sizeof(uint32_t);
454 switch (idx) {
455 case R_CM_LOCK:
456 r = get_cm_lock(s);
457 break;
459 default:
460 r = s->regs[idx];
463 trace_bcm2835_cprman_read(offset, r);
464 return r;
467 static inline void update_pll_and_channels_from_cm(BCM2835CprmanState *s,
468 size_t idx)
470 size_t i;
472 for (i = 0; i < CPRMAN_NUM_PLL; i++) {
473 if (PLL_INIT_INFO[i].cm_offset == idx) {
474 pll_update_all_channels(s, &s->plls[i]);
475 return;
480 static inline void update_channel_from_a2w(BCM2835CprmanState *s, size_t idx)
482 size_t i;
484 for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
485 if (PLL_CHANNEL_INIT_INFO[i].a2w_ctrl_offset == idx) {
486 pll_channel_update(&s->channels[i]);
487 return;
492 static inline void update_mux_from_cm(BCM2835CprmanState *s, size_t idx)
494 size_t i;
496 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
497 if ((CLOCK_MUX_INIT_INFO[i].cm_offset == idx) ||
498 (CLOCK_MUX_INIT_INFO[i].cm_offset + 4 == idx)) {
499 /* matches CM_CTL or CM_DIV mux register */
500 clock_mux_update(&s->clock_muxes[i]);
501 return;
506 #define CASE_PLL_A2W_REGS(pll_) \
507 case R_A2W_ ## pll_ ## _CTRL: \
508 case R_A2W_ ## pll_ ## _ANA0: \
509 case R_A2W_ ## pll_ ## _ANA1: \
510 case R_A2W_ ## pll_ ## _ANA2: \
511 case R_A2W_ ## pll_ ## _ANA3: \
512 case R_A2W_ ## pll_ ## _FRAC
514 static void cprman_write(void *opaque, hwaddr offset,
515 uint64_t value, unsigned size)
517 BCM2835CprmanState *s = CPRMAN(opaque);
518 size_t idx = offset / sizeof(uint32_t);
520 if (FIELD_EX32(value, CPRMAN, PASSWORD) != CPRMAN_PASSWORD) {
521 trace_bcm2835_cprman_write_invalid_magic(offset, value);
522 return;
525 value &= ~R_CPRMAN_PASSWORD_MASK;
527 trace_bcm2835_cprman_write(offset, value);
528 s->regs[idx] = value;
530 switch (idx) {
531 case R_CM_PLLA ... R_CM_PLLH:
532 case R_CM_PLLB:
534 * A given CM_PLLx register is shared by both the PLL and the channels
535 * of this PLL.
537 update_pll_and_channels_from_cm(s, idx);
538 break;
540 CASE_PLL_A2W_REGS(PLLA) :
541 pll_update(&s->plls[CPRMAN_PLLA]);
542 break;
544 CASE_PLL_A2W_REGS(PLLC) :
545 pll_update(&s->plls[CPRMAN_PLLC]);
546 break;
548 CASE_PLL_A2W_REGS(PLLD) :
549 pll_update(&s->plls[CPRMAN_PLLD]);
550 break;
552 CASE_PLL_A2W_REGS(PLLH) :
553 pll_update(&s->plls[CPRMAN_PLLH]);
554 break;
556 CASE_PLL_A2W_REGS(PLLB) :
557 pll_update(&s->plls[CPRMAN_PLLB]);
558 break;
560 case R_A2W_PLLA_DSI0:
561 case R_A2W_PLLA_CORE:
562 case R_A2W_PLLA_PER:
563 case R_A2W_PLLA_CCP2:
564 case R_A2W_PLLC_CORE2:
565 case R_A2W_PLLC_CORE1:
566 case R_A2W_PLLC_PER:
567 case R_A2W_PLLC_CORE0:
568 case R_A2W_PLLD_DSI0:
569 case R_A2W_PLLD_CORE:
570 case R_A2W_PLLD_PER:
571 case R_A2W_PLLD_DSI1:
572 case R_A2W_PLLH_AUX:
573 case R_A2W_PLLH_RCAL:
574 case R_A2W_PLLH_PIX:
575 case R_A2W_PLLB_ARM:
576 update_channel_from_a2w(s, idx);
577 break;
579 case R_CM_GNRICCTL ... R_CM_SMIDIV:
580 case R_CM_TCNTCNT ... R_CM_VECDIV:
581 case R_CM_PULSECTL ... R_CM_PULSEDIV:
582 case R_CM_SDCCTL ... R_CM_ARMCTL:
583 case R_CM_AVEOCTL ... R_CM_EMMCDIV:
584 case R_CM_EMMC2CTL ... R_CM_EMMC2DIV:
585 update_mux_from_cm(s, idx);
586 break;
588 case R_CM_DSI0HSCK:
589 dsi0hsck_mux_update(&s->dsi0hsck_mux);
590 break;
594 #undef CASE_PLL_A2W_REGS
596 static const MemoryRegionOps cprman_ops = {
597 .read = cprman_read,
598 .write = cprman_write,
599 .endianness = DEVICE_LITTLE_ENDIAN,
600 .valid = {
602 * Although this hasn't been checked against real hardware, nor the
603 * information can be found in a datasheet, it seems reasonable because
604 * of the "PASSWORD" magic value found in every registers.
606 .min_access_size = 4,
607 .max_access_size = 4,
608 .unaligned = false,
610 .impl = {
611 .max_access_size = 4,
615 static void cprman_reset(DeviceState *dev)
617 BCM2835CprmanState *s = CPRMAN(dev);
618 size_t i;
620 memset(s->regs, 0, sizeof(s->regs));
622 for (i = 0; i < CPRMAN_NUM_PLL; i++) {
623 device_cold_reset(DEVICE(&s->plls[i]));
626 for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
627 device_cold_reset(DEVICE(&s->channels[i]));
630 device_cold_reset(DEVICE(&s->dsi0hsck_mux));
632 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
633 device_cold_reset(DEVICE(&s->clock_muxes[i]));
636 clock_update_hz(s->xosc, s->xosc_freq);
639 static void cprman_init(Object *obj)
641 BCM2835CprmanState *s = CPRMAN(obj);
642 size_t i;
644 for (i = 0; i < CPRMAN_NUM_PLL; i++) {
645 object_initialize_child(obj, PLL_INIT_INFO[i].name,
646 &s->plls[i], TYPE_CPRMAN_PLL);
647 set_pll_init_info(s, &s->plls[i], i);
650 for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
651 object_initialize_child(obj, PLL_CHANNEL_INIT_INFO[i].name,
652 &s->channels[i],
653 TYPE_CPRMAN_PLL_CHANNEL);
654 set_pll_channel_init_info(s, &s->channels[i], i);
657 object_initialize_child(obj, "dsi0hsck-mux",
658 &s->dsi0hsck_mux, TYPE_CPRMAN_DSI0HSCK_MUX);
659 s->dsi0hsck_mux.reg_cm = &s->regs[R_CM_DSI0HSCK];
661 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
662 char *alias;
664 object_initialize_child(obj, CLOCK_MUX_INIT_INFO[i].name,
665 &s->clock_muxes[i],
666 TYPE_CPRMAN_CLOCK_MUX);
667 set_clock_mux_init_info(s, &s->clock_muxes[i], i);
669 /* Expose muxes output as CPRMAN outputs */
670 alias = g_strdup_printf("%s-out", CLOCK_MUX_INIT_INFO[i].name);
671 qdev_alias_clock(DEVICE(&s->clock_muxes[i]), "out", DEVICE(obj), alias);
672 g_free(alias);
675 s->xosc = clock_new(obj, "xosc");
676 s->gnd = clock_new(obj, "gnd");
678 clock_set(s->gnd, 0);
680 memory_region_init_io(&s->iomem, obj, &cprman_ops,
681 s, "bcm2835-cprman", 0x2000);
682 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
685 static void connect_mux_sources(BCM2835CprmanState *s,
686 CprmanClockMuxState *mux,
687 const CprmanPllChannel *clk_mapping)
689 size_t i;
690 Clock *td0 = s->clock_muxes[CPRMAN_CLOCK_TD0].out;
691 Clock *td1 = s->clock_muxes[CPRMAN_CLOCK_TD1].out;
693 /* For sources from 0 to 3. Source 4 to 9 are mux specific */
694 Clock * const CLK_SRC_MAPPING[] = {
695 [CPRMAN_CLOCK_SRC_GND] = s->gnd,
696 [CPRMAN_CLOCK_SRC_XOSC] = s->xosc,
697 [CPRMAN_CLOCK_SRC_TD0] = td0,
698 [CPRMAN_CLOCK_SRC_TD1] = td1,
701 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX_SRC; i++) {
702 CprmanPllChannel mapping = clk_mapping[i];
703 Clock *src;
705 if (mapping == CPRMAN_CLOCK_SRC_FORCE_GROUND) {
706 src = s->gnd;
707 } else if (mapping == CPRMAN_CLOCK_SRC_DSI0HSCK) {
708 src = s->dsi0hsck_mux.out;
709 } else if (i < CPRMAN_CLOCK_SRC_PLLA) {
710 src = CLK_SRC_MAPPING[i];
711 } else {
712 src = s->channels[mapping].out;
715 clock_set_source(mux->srcs[i], src);
719 static void cprman_realize(DeviceState *dev, Error **errp)
721 BCM2835CprmanState *s = CPRMAN(dev);
722 size_t i;
724 for (i = 0; i < CPRMAN_NUM_PLL; i++) {
725 CprmanPllState *pll = &s->plls[i];
727 clock_set_source(pll->xosc_in, s->xosc);
729 if (!qdev_realize(DEVICE(pll), NULL, errp)) {
730 return;
734 for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
735 CprmanPllChannelState *channel = &s->channels[i];
736 CprmanPll parent = PLL_CHANNEL_INIT_INFO[i].parent;
737 Clock *parent_clk = s->plls[parent].out;
739 clock_set_source(channel->pll_in, parent_clk);
741 if (!qdev_realize(DEVICE(channel), NULL, errp)) {
742 return;
746 clock_set_source(s->dsi0hsck_mux.plla_in,
747 s->channels[CPRMAN_PLLA_CHANNEL_DSI0].out);
748 clock_set_source(s->dsi0hsck_mux.plld_in,
749 s->channels[CPRMAN_PLLD_CHANNEL_DSI0].out);
751 if (!qdev_realize(DEVICE(&s->dsi0hsck_mux), NULL, errp)) {
752 return;
755 for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
756 CprmanClockMuxState *clock_mux = &s->clock_muxes[i];
758 connect_mux_sources(s, clock_mux, CLOCK_MUX_INIT_INFO[i].src_mapping);
760 if (!qdev_realize(DEVICE(clock_mux), NULL, errp)) {
761 return;
766 static const VMStateDescription cprman_vmstate = {
767 .name = TYPE_BCM2835_CPRMAN,
768 .version_id = 1,
769 .minimum_version_id = 1,
770 .fields = (VMStateField[]) {
771 VMSTATE_UINT32_ARRAY(regs, BCM2835CprmanState, CPRMAN_NUM_REGS),
772 VMSTATE_END_OF_LIST()
776 static Property cprman_properties[] = {
777 DEFINE_PROP_UINT32("xosc-freq-hz", BCM2835CprmanState, xosc_freq, 19200000),
778 DEFINE_PROP_END_OF_LIST()
781 static void cprman_class_init(ObjectClass *klass, void *data)
783 DeviceClass *dc = DEVICE_CLASS(klass);
785 dc->realize = cprman_realize;
786 dc->reset = cprman_reset;
787 dc->vmsd = &cprman_vmstate;
788 device_class_set_props(dc, cprman_properties);
791 static const TypeInfo cprman_info = {
792 .name = TYPE_BCM2835_CPRMAN,
793 .parent = TYPE_SYS_BUS_DEVICE,
794 .instance_size = sizeof(BCM2835CprmanState),
795 .class_init = cprman_class_init,
796 .instance_init = cprman_init,
799 static void cprman_register_types(void)
801 type_register_static(&cprman_info);
802 type_register_static(&cprman_pll_info);
803 type_register_static(&cprman_pll_channel_info);
804 type_register_static(&cprman_clock_mux_info);
805 type_register_static(&cprman_dsi0hsck_mux_info);
808 type_init(cprman_register_types);