xref: /qemu/hw/misc/bcm2835_cprman.c (revision fc9840850ba0eb3e61c81894bff3df12b0534497)
1fc14176bSLuc Michel /*
2fc14176bSLuc Michel  * BCM2835 CPRMAN clock manager
3fc14176bSLuc Michel  *
4fc14176bSLuc Michel  * Copyright (c) 2020 Luc Michel <luc@lmichel.fr>
5fc14176bSLuc Michel  *
6fc14176bSLuc Michel  * SPDX-License-Identifier: GPL-2.0-or-later
7fc14176bSLuc Michel  */
8fc14176bSLuc Michel 
9fc14176bSLuc Michel /*
10fc14176bSLuc Michel  * This peripheral is roughly divided into 3 main parts:
11fc14176bSLuc Michel  *   - the PLLs
12fc14176bSLuc Michel  *   - the PLL channels
13fc14176bSLuc Michel  *   - the clock muxes
14fc14176bSLuc Michel  *
15fc14176bSLuc Michel  * A main oscillator (xosc) feeds all the PLLs. Each PLLs has one or more
16fc14176bSLuc Michel  * channels. Those channel are then connected to the clock muxes. Each mux has
17fc14176bSLuc Michel  * multiples sources (usually the xosc, some of the PLL channels and some "test
18fc14176bSLuc Michel  * debug" clocks). A mux is configured to select a given source through its
19fc14176bSLuc Michel  * control register. Each mux has one output clock that also goes out of the
20fc14176bSLuc Michel  * CPRMAN. This output clock usually connects to another peripheral in the SoC
21fc14176bSLuc Michel  * (so a given mux is dedicated to a peripheral).
22fc14176bSLuc Michel  *
23fc14176bSLuc Michel  * At each level (PLL, channel and mux), the clock can be altered through
24fc14176bSLuc Michel  * dividers (and multipliers in case of the PLLs), and can be disabled (in this
25fc14176bSLuc Michel  * case, the next levels see no clock).
26fc14176bSLuc Michel  *
27fc14176bSLuc Michel  * This can be sum-up as follows (this is an example and not the actual BCM2835
28fc14176bSLuc Michel  * clock tree):
29fc14176bSLuc Michel  *
30fc14176bSLuc Michel  *          /-->[PLL]-|->[PLL channel]--...            [mux]--> to peripherals
31fc14176bSLuc Michel  *          |         |->[PLL channel]  muxes takes    [mux]
32fc14176bSLuc Michel  *          |         \->[PLL channel]  inputs from    [mux]
33fc14176bSLuc Michel  *          |                           some channels  [mux]
34fc14176bSLuc Michel  * [xosc]---|-->[PLL]-|->[PLL channel]  and other srcs [mux]
35fc14176bSLuc Michel  *          |         \->[PLL channel]           ...-->[mux]
36fc14176bSLuc Michel  *          |                                          [mux]
37fc14176bSLuc Michel  *          \-->[PLL]--->[PLL channel]                 [mux]
38fc14176bSLuc Michel  *
39fc14176bSLuc Michel  * The page at https://elinux.org/The_Undocumented_Pi gives the actual clock
40fc14176bSLuc Michel  * tree configuration.
4172813624SLuc Michel  *
4272813624SLuc Michel  * The CPRMAN exposes clock outputs with the name of the clock mux suffixed
4372813624SLuc Michel  * with "-out" (e.g. "uart-out", "h264-out", ...).
44fc14176bSLuc Michel  */
45fc14176bSLuc Michel 
46fc14176bSLuc Michel #include "qemu/osdep.h"
47fc14176bSLuc Michel #include "qemu/log.h"
48fc14176bSLuc Michel #include "migration/vmstate.h"
49fc14176bSLuc Michel #include "hw/qdev-properties.h"
50fc14176bSLuc Michel #include "hw/misc/bcm2835_cprman.h"
51fc14176bSLuc Michel #include "hw/misc/bcm2835_cprman_internals.h"
52fc14176bSLuc Michel #include "trace.h"
53fc14176bSLuc Michel 
541e986e25SLuc Michel /* PLL */
551e986e25SLuc Michel 
566d2b874cSLuc Michel static bool pll_is_locked(const CprmanPllState *pll)
576d2b874cSLuc Michel {
586d2b874cSLuc Michel     return !FIELD_EX32(*pll->reg_a2w_ctrl, A2W_PLLx_CTRL, PWRDN)
596d2b874cSLuc Michel         && !FIELD_EX32(*pll->reg_cm, CM_PLLx, ANARST);
606d2b874cSLuc Michel }
616d2b874cSLuc Michel 
621e986e25SLuc Michel static void pll_update(CprmanPllState *pll)
631e986e25SLuc Michel {
646d2b874cSLuc Michel     uint64_t freq, ndiv, fdiv, pdiv;
656d2b874cSLuc Michel 
666d2b874cSLuc Michel     if (!pll_is_locked(pll)) {
671e986e25SLuc Michel         clock_update(pll->out, 0);
686d2b874cSLuc Michel         return;
696d2b874cSLuc Michel     }
706d2b874cSLuc Michel 
716d2b874cSLuc Michel     pdiv = FIELD_EX32(*pll->reg_a2w_ctrl, A2W_PLLx_CTRL, PDIV);
726d2b874cSLuc Michel 
736d2b874cSLuc Michel     if (!pdiv) {
746d2b874cSLuc Michel         clock_update(pll->out, 0);
756d2b874cSLuc Michel         return;
766d2b874cSLuc Michel     }
776d2b874cSLuc Michel 
786d2b874cSLuc Michel     ndiv = FIELD_EX32(*pll->reg_a2w_ctrl, A2W_PLLx_CTRL, NDIV);
796d2b874cSLuc Michel     fdiv = FIELD_EX32(*pll->reg_a2w_frac, A2W_PLLx_FRAC, FRAC);
806d2b874cSLuc Michel 
816d2b874cSLuc Michel     if (pll->reg_a2w_ana[1] & pll->prediv_mask) {
826d2b874cSLuc Michel         /* The prescaler doubles the parent frequency */
836d2b874cSLuc Michel         ndiv *= 2;
846d2b874cSLuc Michel         fdiv *= 2;
856d2b874cSLuc Michel     }
866d2b874cSLuc Michel 
876d2b874cSLuc Michel     /*
886d2b874cSLuc Michel      * We have a multiplier with an integer part (ndiv) and a fractional part
896d2b874cSLuc Michel      * (fdiv), and a divider (pdiv).
906d2b874cSLuc Michel      */
916d2b874cSLuc Michel     freq = clock_get_hz(pll->xosc_in) *
926d2b874cSLuc Michel         ((ndiv << R_A2W_PLLx_FRAC_FRAC_LENGTH) + fdiv);
936d2b874cSLuc Michel     freq /= pdiv;
946d2b874cSLuc Michel     freq >>= R_A2W_PLLx_FRAC_FRAC_LENGTH;
956d2b874cSLuc Michel 
966d2b874cSLuc Michel     clock_update_hz(pll->out, freq);
971e986e25SLuc Michel }
981e986e25SLuc Michel 
991e986e25SLuc Michel static void pll_xosc_update(void *opaque)
1001e986e25SLuc Michel {
1011e986e25SLuc Michel     pll_update(CPRMAN_PLL(opaque));
1021e986e25SLuc Michel }
1031e986e25SLuc Michel 
1041e986e25SLuc Michel static void pll_init(Object *obj)
1051e986e25SLuc Michel {
1061e986e25SLuc Michel     CprmanPllState *s = CPRMAN_PLL(obj);
1071e986e25SLuc Michel 
1081e986e25SLuc Michel     s->xosc_in = qdev_init_clock_in(DEVICE(s), "xosc-in", pll_xosc_update, s);
1091e986e25SLuc Michel     s->out = qdev_init_clock_out(DEVICE(s), "out");
1101e986e25SLuc Michel }
1111e986e25SLuc Michel 
1121e986e25SLuc Michel static const VMStateDescription pll_vmstate = {
1131e986e25SLuc Michel     .name = TYPE_CPRMAN_PLL,
1141e986e25SLuc Michel     .version_id = 1,
1151e986e25SLuc Michel     .minimum_version_id = 1,
1161e986e25SLuc Michel     .fields = (VMStateField[]) {
1171e986e25SLuc Michel         VMSTATE_CLOCK(xosc_in, CprmanPllState),
1181e986e25SLuc Michel         VMSTATE_END_OF_LIST()
1191e986e25SLuc Michel     }
1201e986e25SLuc Michel };
1211e986e25SLuc Michel 
1221e986e25SLuc Michel static void pll_class_init(ObjectClass *klass, void *data)
1231e986e25SLuc Michel {
1241e986e25SLuc Michel     DeviceClass *dc = DEVICE_CLASS(klass);
1251e986e25SLuc Michel 
1261e986e25SLuc Michel     dc->vmsd = &pll_vmstate;
1271e986e25SLuc Michel }
1281e986e25SLuc Michel 
1291e986e25SLuc Michel static const TypeInfo cprman_pll_info = {
1301e986e25SLuc Michel     .name = TYPE_CPRMAN_PLL,
1311e986e25SLuc Michel     .parent = TYPE_DEVICE,
1321e986e25SLuc Michel     .instance_size = sizeof(CprmanPllState),
1331e986e25SLuc Michel     .class_init = pll_class_init,
1341e986e25SLuc Michel     .instance_init = pll_init,
1351e986e25SLuc Michel };
1361e986e25SLuc Michel 
1371e986e25SLuc Michel 
13809d56bbcSLuc Michel /* PLL channel */
13909d56bbcSLuc Michel 
14095745811SLuc Michel static bool pll_channel_is_enabled(CprmanPllChannelState *channel)
14195745811SLuc Michel {
14295745811SLuc Michel     /*
14395745811SLuc Michel      * XXX I'm not sure of the purpose of the LOAD field. The Linux driver does
14495745811SLuc Michel      * not set it when enabling the channel, but does clear it when disabling
14595745811SLuc Michel      * it.
14695745811SLuc Michel      */
14795745811SLuc Michel     return !FIELD_EX32(*channel->reg_a2w_ctrl, A2W_PLLx_CHANNELy, DISABLE)
14895745811SLuc Michel         && !(*channel->reg_cm & channel->hold_mask);
14995745811SLuc Michel }
15095745811SLuc Michel 
15109d56bbcSLuc Michel static void pll_channel_update(CprmanPllChannelState *channel)
15209d56bbcSLuc Michel {
15395745811SLuc Michel     uint64_t freq, div;
15495745811SLuc Michel 
15595745811SLuc Michel     if (!pll_channel_is_enabled(channel)) {
15609d56bbcSLuc Michel         clock_update(channel->out, 0);
15795745811SLuc Michel         return;
15895745811SLuc Michel     }
15995745811SLuc Michel 
16095745811SLuc Michel     div = FIELD_EX32(*channel->reg_a2w_ctrl, A2W_PLLx_CHANNELy, DIV);
16195745811SLuc Michel 
16295745811SLuc Michel     if (!div) {
16395745811SLuc Michel         /*
16495745811SLuc Michel          * It seems that when the divider value is 0, it is considered as
16595745811SLuc Michel          * being maximum by the hardware (see the Linux driver).
16695745811SLuc Michel          */
16795745811SLuc Michel         div = R_A2W_PLLx_CHANNELy_DIV_MASK;
16895745811SLuc Michel     }
16995745811SLuc Michel 
17095745811SLuc Michel     /* Some channels have an additional fixed divider */
17195745811SLuc Michel     freq = clock_get_hz(channel->pll_in) / (div * channel->fixed_divider);
17295745811SLuc Michel 
17395745811SLuc Michel     clock_update_hz(channel->out, freq);
17409d56bbcSLuc Michel }
17509d56bbcSLuc Michel 
17609d56bbcSLuc Michel /* Update a PLL and all its channels */
17709d56bbcSLuc Michel static void pll_update_all_channels(BCM2835CprmanState *s,
17809d56bbcSLuc Michel                                     CprmanPllState *pll)
17909d56bbcSLuc Michel {
18009d56bbcSLuc Michel     size_t i;
18109d56bbcSLuc Michel 
18209d56bbcSLuc Michel     pll_update(pll);
18309d56bbcSLuc Michel 
18409d56bbcSLuc Michel     for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
18509d56bbcSLuc Michel         CprmanPllChannelState *channel = &s->channels[i];
18609d56bbcSLuc Michel         if (channel->parent == pll->id) {
18709d56bbcSLuc Michel             pll_channel_update(channel);
18809d56bbcSLuc Michel         }
18909d56bbcSLuc Michel     }
19009d56bbcSLuc Michel }
19109d56bbcSLuc Michel 
19209d56bbcSLuc Michel static void pll_channel_pll_in_update(void *opaque)
19309d56bbcSLuc Michel {
19409d56bbcSLuc Michel     pll_channel_update(CPRMAN_PLL_CHANNEL(opaque));
19509d56bbcSLuc Michel }
19609d56bbcSLuc Michel 
19709d56bbcSLuc Michel static void pll_channel_init(Object *obj)
19809d56bbcSLuc Michel {
19909d56bbcSLuc Michel     CprmanPllChannelState *s = CPRMAN_PLL_CHANNEL(obj);
20009d56bbcSLuc Michel 
20109d56bbcSLuc Michel     s->pll_in = qdev_init_clock_in(DEVICE(s), "pll-in",
20209d56bbcSLuc Michel                                    pll_channel_pll_in_update, s);
20309d56bbcSLuc Michel     s->out = qdev_init_clock_out(DEVICE(s), "out");
20409d56bbcSLuc Michel }
20509d56bbcSLuc Michel 
20609d56bbcSLuc Michel static const VMStateDescription pll_channel_vmstate = {
20709d56bbcSLuc Michel     .name = TYPE_CPRMAN_PLL_CHANNEL,
20809d56bbcSLuc Michel     .version_id = 1,
20909d56bbcSLuc Michel     .minimum_version_id = 1,
21009d56bbcSLuc Michel     .fields = (VMStateField[]) {
21109d56bbcSLuc Michel         VMSTATE_CLOCK(pll_in, CprmanPllChannelState),
21209d56bbcSLuc Michel         VMSTATE_END_OF_LIST()
21309d56bbcSLuc Michel     }
21409d56bbcSLuc Michel };
21509d56bbcSLuc Michel 
21609d56bbcSLuc Michel static void pll_channel_class_init(ObjectClass *klass, void *data)
21709d56bbcSLuc Michel {
21809d56bbcSLuc Michel     DeviceClass *dc = DEVICE_CLASS(klass);
21909d56bbcSLuc Michel 
22009d56bbcSLuc Michel     dc->vmsd = &pll_channel_vmstate;
22109d56bbcSLuc Michel }
22209d56bbcSLuc Michel 
22309d56bbcSLuc Michel static const TypeInfo cprman_pll_channel_info = {
22409d56bbcSLuc Michel     .name = TYPE_CPRMAN_PLL_CHANNEL,
22509d56bbcSLuc Michel     .parent = TYPE_DEVICE,
22609d56bbcSLuc Michel     .instance_size = sizeof(CprmanPllChannelState),
22709d56bbcSLuc Michel     .class_init = pll_channel_class_init,
22809d56bbcSLuc Michel     .instance_init = pll_channel_init,
22909d56bbcSLuc Michel };
23009d56bbcSLuc Michel 
23109d56bbcSLuc Michel 
23272813624SLuc Michel /* clock mux */
23372813624SLuc Michel 
234*fc984085SLuc Michel static bool clock_mux_is_enabled(CprmanClockMuxState *mux)
235*fc984085SLuc Michel {
236*fc984085SLuc Michel     return FIELD_EX32(*mux->reg_ctl, CM_CLOCKx_CTL, ENABLE);
237*fc984085SLuc Michel }
238*fc984085SLuc Michel 
23972813624SLuc Michel static void clock_mux_update(CprmanClockMuxState *mux)
24072813624SLuc Michel {
241*fc984085SLuc Michel     uint64_t freq;
242*fc984085SLuc Michel     uint32_t div, src = FIELD_EX32(*mux->reg_ctl, CM_CLOCKx_CTL, SRC);
243*fc984085SLuc Michel     bool enabled = clock_mux_is_enabled(mux);
244*fc984085SLuc Michel 
245*fc984085SLuc Michel     *mux->reg_ctl = FIELD_DP32(*mux->reg_ctl, CM_CLOCKx_CTL, BUSY, enabled);
246*fc984085SLuc Michel 
247*fc984085SLuc Michel     if (!enabled) {
24872813624SLuc Michel         clock_update(mux->out, 0);
249*fc984085SLuc Michel         return;
250*fc984085SLuc Michel     }
251*fc984085SLuc Michel 
252*fc984085SLuc Michel     freq = clock_get_hz(mux->srcs[src]);
253*fc984085SLuc Michel 
254*fc984085SLuc Michel     if (mux->int_bits == 0 && mux->frac_bits == 0) {
255*fc984085SLuc Michel         clock_update_hz(mux->out, freq);
256*fc984085SLuc Michel         return;
257*fc984085SLuc Michel     }
258*fc984085SLuc Michel 
259*fc984085SLuc Michel     /*
260*fc984085SLuc Michel      * The divider has an integer and a fractional part. The size of each part
261*fc984085SLuc Michel      * varies with the muxes (int_bits and frac_bits). Both parts are
262*fc984085SLuc Michel      * concatenated, with the integer part always starting at bit 12.
263*fc984085SLuc Michel      *
264*fc984085SLuc Michel      *         31          12 11          0
265*fc984085SLuc Michel      *        ------------------------------
266*fc984085SLuc Michel      * CM_DIV |      |  int  |  frac  |    |
267*fc984085SLuc Michel      *        ------------------------------
268*fc984085SLuc Michel      *                <-----> <------>
269*fc984085SLuc Michel      *                int_bits frac_bits
270*fc984085SLuc Michel      */
271*fc984085SLuc Michel     div = extract32(*mux->reg_div,
272*fc984085SLuc Michel                     R_CM_CLOCKx_DIV_FRAC_LENGTH - mux->frac_bits,
273*fc984085SLuc Michel                     mux->int_bits + mux->frac_bits);
274*fc984085SLuc Michel 
275*fc984085SLuc Michel     if (!div) {
276*fc984085SLuc Michel         clock_update(mux->out, 0);
277*fc984085SLuc Michel         return;
278*fc984085SLuc Michel     }
279*fc984085SLuc Michel 
280*fc984085SLuc Michel     freq = muldiv64(freq, 1 << mux->frac_bits, div);
281*fc984085SLuc Michel 
282*fc984085SLuc Michel     clock_update_hz(mux->out, freq);
28372813624SLuc Michel }
28472813624SLuc Michel 
28572813624SLuc Michel static void clock_mux_src_update(void *opaque)
28672813624SLuc Michel {
28772813624SLuc Michel     CprmanClockMuxState **backref = opaque;
28872813624SLuc Michel     CprmanClockMuxState *s = *backref;
289*fc984085SLuc Michel     CprmanClockMuxSource src = backref - s->backref;
290*fc984085SLuc Michel 
291*fc984085SLuc Michel     if (FIELD_EX32(*s->reg_ctl, CM_CLOCKx_CTL, SRC) != src) {
292*fc984085SLuc Michel         return;
293*fc984085SLuc Michel     }
29472813624SLuc Michel 
29572813624SLuc Michel     clock_mux_update(s);
29672813624SLuc Michel }
29772813624SLuc Michel 
29872813624SLuc Michel static void clock_mux_init(Object *obj)
29972813624SLuc Michel {
30072813624SLuc Michel     CprmanClockMuxState *s = CPRMAN_CLOCK_MUX(obj);
30172813624SLuc Michel     size_t i;
30272813624SLuc Michel 
30372813624SLuc Michel     for (i = 0; i < CPRMAN_NUM_CLOCK_MUX_SRC; i++) {
30472813624SLuc Michel         char *name = g_strdup_printf("srcs[%zu]", i);
30572813624SLuc Michel         s->backref[i] = s;
30672813624SLuc Michel         s->srcs[i] = qdev_init_clock_in(DEVICE(s), name,
30772813624SLuc Michel                                         clock_mux_src_update,
30872813624SLuc Michel                                         &s->backref[i]);
30972813624SLuc Michel         g_free(name);
31072813624SLuc Michel     }
31172813624SLuc Michel 
31272813624SLuc Michel     s->out = qdev_init_clock_out(DEVICE(s), "out");
31372813624SLuc Michel }
31472813624SLuc Michel 
31572813624SLuc Michel static const VMStateDescription clock_mux_vmstate = {
31672813624SLuc Michel     .name = TYPE_CPRMAN_CLOCK_MUX,
31772813624SLuc Michel     .version_id = 1,
31872813624SLuc Michel     .minimum_version_id = 1,
31972813624SLuc Michel     .fields = (VMStateField[]) {
32072813624SLuc Michel         VMSTATE_ARRAY_CLOCK(srcs, CprmanClockMuxState,
32172813624SLuc Michel                             CPRMAN_NUM_CLOCK_MUX_SRC),
32272813624SLuc Michel         VMSTATE_END_OF_LIST()
32372813624SLuc Michel     }
32472813624SLuc Michel };
32572813624SLuc Michel 
32672813624SLuc Michel static void clock_mux_class_init(ObjectClass *klass, void *data)
32772813624SLuc Michel {
32872813624SLuc Michel     DeviceClass *dc = DEVICE_CLASS(klass);
32972813624SLuc Michel 
33072813624SLuc Michel     dc->vmsd = &clock_mux_vmstate;
33172813624SLuc Michel }
33272813624SLuc Michel 
33372813624SLuc Michel static const TypeInfo cprman_clock_mux_info = {
33472813624SLuc Michel     .name = TYPE_CPRMAN_CLOCK_MUX,
33572813624SLuc Michel     .parent = TYPE_DEVICE,
33672813624SLuc Michel     .instance_size = sizeof(CprmanClockMuxState),
33772813624SLuc Michel     .class_init = clock_mux_class_init,
33872813624SLuc Michel     .instance_init = clock_mux_init,
33972813624SLuc Michel };
34072813624SLuc Michel 
34172813624SLuc Michel 
342fc14176bSLuc Michel /* CPRMAN "top level" model */
343fc14176bSLuc Michel 
3446d2b874cSLuc Michel static uint32_t get_cm_lock(const BCM2835CprmanState *s)
3456d2b874cSLuc Michel {
3466d2b874cSLuc Michel     static const int CM_LOCK_MAPPING[CPRMAN_NUM_PLL] = {
3476d2b874cSLuc Michel         [CPRMAN_PLLA] = R_CM_LOCK_FLOCKA_SHIFT,
3486d2b874cSLuc Michel         [CPRMAN_PLLC] = R_CM_LOCK_FLOCKC_SHIFT,
3496d2b874cSLuc Michel         [CPRMAN_PLLD] = R_CM_LOCK_FLOCKD_SHIFT,
3506d2b874cSLuc Michel         [CPRMAN_PLLH] = R_CM_LOCK_FLOCKH_SHIFT,
3516d2b874cSLuc Michel         [CPRMAN_PLLB] = R_CM_LOCK_FLOCKB_SHIFT,
3526d2b874cSLuc Michel     };
3536d2b874cSLuc Michel 
3546d2b874cSLuc Michel     uint32_t r = 0;
3556d2b874cSLuc Michel     size_t i;
3566d2b874cSLuc Michel 
3576d2b874cSLuc Michel     for (i = 0; i < CPRMAN_NUM_PLL; i++) {
3586d2b874cSLuc Michel         r |= pll_is_locked(&s->plls[i]) << CM_LOCK_MAPPING[i];
3596d2b874cSLuc Michel     }
3606d2b874cSLuc Michel 
3616d2b874cSLuc Michel     return r;
3626d2b874cSLuc Michel }
3636d2b874cSLuc Michel 
364fc14176bSLuc Michel static uint64_t cprman_read(void *opaque, hwaddr offset,
365fc14176bSLuc Michel                             unsigned size)
366fc14176bSLuc Michel {
367fc14176bSLuc Michel     BCM2835CprmanState *s = CPRMAN(opaque);
368fc14176bSLuc Michel     uint64_t r = 0;
369fc14176bSLuc Michel     size_t idx = offset / sizeof(uint32_t);
370fc14176bSLuc Michel 
371fc14176bSLuc Michel     switch (idx) {
3726d2b874cSLuc Michel     case R_CM_LOCK:
3736d2b874cSLuc Michel         r = get_cm_lock(s);
3746d2b874cSLuc Michel         break;
3756d2b874cSLuc Michel 
376fc14176bSLuc Michel     default:
377fc14176bSLuc Michel         r = s->regs[idx];
378fc14176bSLuc Michel     }
379fc14176bSLuc Michel 
380fc14176bSLuc Michel     trace_bcm2835_cprman_read(offset, r);
381fc14176bSLuc Michel     return r;
382fc14176bSLuc Michel }
383fc14176bSLuc Michel 
38409d56bbcSLuc Michel static inline void update_pll_and_channels_from_cm(BCM2835CprmanState *s,
38509d56bbcSLuc Michel                                                    size_t idx)
38609d56bbcSLuc Michel {
38709d56bbcSLuc Michel     size_t i;
38809d56bbcSLuc Michel 
38909d56bbcSLuc Michel     for (i = 0; i < CPRMAN_NUM_PLL; i++) {
39009d56bbcSLuc Michel         if (PLL_INIT_INFO[i].cm_offset == idx) {
39109d56bbcSLuc Michel             pll_update_all_channels(s, &s->plls[i]);
39209d56bbcSLuc Michel             return;
39309d56bbcSLuc Michel         }
39409d56bbcSLuc Michel     }
39509d56bbcSLuc Michel }
39609d56bbcSLuc Michel 
39709d56bbcSLuc Michel static inline void update_channel_from_a2w(BCM2835CprmanState *s, size_t idx)
39809d56bbcSLuc Michel {
39909d56bbcSLuc Michel     size_t i;
40009d56bbcSLuc Michel 
40109d56bbcSLuc Michel     for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
40209d56bbcSLuc Michel         if (PLL_CHANNEL_INIT_INFO[i].a2w_ctrl_offset == idx) {
40309d56bbcSLuc Michel             pll_channel_update(&s->channels[i]);
40409d56bbcSLuc Michel             return;
40509d56bbcSLuc Michel         }
40609d56bbcSLuc Michel     }
40709d56bbcSLuc Michel }
40809d56bbcSLuc Michel 
40972813624SLuc Michel static inline void update_mux_from_cm(BCM2835CprmanState *s, size_t idx)
41072813624SLuc Michel {
41172813624SLuc Michel     size_t i;
41272813624SLuc Michel 
41372813624SLuc Michel     for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
41472813624SLuc Michel         if ((CLOCK_MUX_INIT_INFO[i].cm_offset == idx) ||
41572813624SLuc Michel             (CLOCK_MUX_INIT_INFO[i].cm_offset + 4 == idx)) {
41672813624SLuc Michel             /* matches CM_CTL or CM_DIV mux register */
41772813624SLuc Michel             clock_mux_update(&s->clock_muxes[i]);
41872813624SLuc Michel             return;
41972813624SLuc Michel         }
42072813624SLuc Michel     }
42172813624SLuc Michel }
42272813624SLuc Michel 
42309d56bbcSLuc Michel #define CASE_PLL_A2W_REGS(pll_) \
4241e986e25SLuc Michel     case R_A2W_ ## pll_ ## _CTRL: \
4251e986e25SLuc Michel     case R_A2W_ ## pll_ ## _ANA0: \
4261e986e25SLuc Michel     case R_A2W_ ## pll_ ## _ANA1: \
4271e986e25SLuc Michel     case R_A2W_ ## pll_ ## _ANA2: \
4281e986e25SLuc Michel     case R_A2W_ ## pll_ ## _ANA3: \
4291e986e25SLuc Michel     case R_A2W_ ## pll_ ## _FRAC
4301e986e25SLuc Michel 
431fc14176bSLuc Michel static void cprman_write(void *opaque, hwaddr offset,
432fc14176bSLuc Michel                          uint64_t value, unsigned size)
433fc14176bSLuc Michel {
434fc14176bSLuc Michel     BCM2835CprmanState *s = CPRMAN(opaque);
435fc14176bSLuc Michel     size_t idx = offset / sizeof(uint32_t);
436fc14176bSLuc Michel 
437fc14176bSLuc Michel     if (FIELD_EX32(value, CPRMAN, PASSWORD) != CPRMAN_PASSWORD) {
438fc14176bSLuc Michel         trace_bcm2835_cprman_write_invalid_magic(offset, value);
439fc14176bSLuc Michel         return;
440fc14176bSLuc Michel     }
441fc14176bSLuc Michel 
442fc14176bSLuc Michel     value &= ~R_CPRMAN_PASSWORD_MASK;
443fc14176bSLuc Michel 
444fc14176bSLuc Michel     trace_bcm2835_cprman_write(offset, value);
445fc14176bSLuc Michel     s->regs[idx] = value;
446fc14176bSLuc Michel 
4471e986e25SLuc Michel     switch (idx) {
44809d56bbcSLuc Michel     case R_CM_PLLA ... R_CM_PLLH:
44909d56bbcSLuc Michel     case R_CM_PLLB:
45009d56bbcSLuc Michel         /*
45109d56bbcSLuc Michel          * A given CM_PLLx register is shared by both the PLL and the channels
45209d56bbcSLuc Michel          * of this PLL.
45309d56bbcSLuc Michel          */
45409d56bbcSLuc Michel         update_pll_and_channels_from_cm(s, idx);
45509d56bbcSLuc Michel         break;
45609d56bbcSLuc Michel 
45709d56bbcSLuc Michel     CASE_PLL_A2W_REGS(PLLA) :
4581e986e25SLuc Michel         pll_update(&s->plls[CPRMAN_PLLA]);
4591e986e25SLuc Michel         break;
4601e986e25SLuc Michel 
46109d56bbcSLuc Michel     CASE_PLL_A2W_REGS(PLLC) :
4621e986e25SLuc Michel         pll_update(&s->plls[CPRMAN_PLLC]);
4631e986e25SLuc Michel         break;
4641e986e25SLuc Michel 
46509d56bbcSLuc Michel     CASE_PLL_A2W_REGS(PLLD) :
4661e986e25SLuc Michel         pll_update(&s->plls[CPRMAN_PLLD]);
4671e986e25SLuc Michel         break;
4681e986e25SLuc Michel 
46909d56bbcSLuc Michel     CASE_PLL_A2W_REGS(PLLH) :
4701e986e25SLuc Michel         pll_update(&s->plls[CPRMAN_PLLH]);
4711e986e25SLuc Michel         break;
4721e986e25SLuc Michel 
47309d56bbcSLuc Michel     CASE_PLL_A2W_REGS(PLLB) :
4741e986e25SLuc Michel         pll_update(&s->plls[CPRMAN_PLLB]);
4751e986e25SLuc Michel         break;
47609d56bbcSLuc Michel 
47709d56bbcSLuc Michel     case R_A2W_PLLA_DSI0:
47809d56bbcSLuc Michel     case R_A2W_PLLA_CORE:
47909d56bbcSLuc Michel     case R_A2W_PLLA_PER:
48009d56bbcSLuc Michel     case R_A2W_PLLA_CCP2:
48109d56bbcSLuc Michel     case R_A2W_PLLC_CORE2:
48209d56bbcSLuc Michel     case R_A2W_PLLC_CORE1:
48309d56bbcSLuc Michel     case R_A2W_PLLC_PER:
48409d56bbcSLuc Michel     case R_A2W_PLLC_CORE0:
48509d56bbcSLuc Michel     case R_A2W_PLLD_DSI0:
48609d56bbcSLuc Michel     case R_A2W_PLLD_CORE:
48709d56bbcSLuc Michel     case R_A2W_PLLD_PER:
48809d56bbcSLuc Michel     case R_A2W_PLLD_DSI1:
48909d56bbcSLuc Michel     case R_A2W_PLLH_AUX:
49009d56bbcSLuc Michel     case R_A2W_PLLH_RCAL:
49109d56bbcSLuc Michel     case R_A2W_PLLH_PIX:
49209d56bbcSLuc Michel     case R_A2W_PLLB_ARM:
49309d56bbcSLuc Michel         update_channel_from_a2w(s, idx);
49409d56bbcSLuc Michel         break;
49572813624SLuc Michel 
49672813624SLuc Michel     case R_CM_GNRICCTL ... R_CM_SMIDIV:
49772813624SLuc Michel     case R_CM_TCNTCNT ... R_CM_VECDIV:
49872813624SLuc Michel     case R_CM_PULSECTL ... R_CM_PULSEDIV:
49972813624SLuc Michel     case R_CM_SDCCTL ... R_CM_ARMCTL:
50072813624SLuc Michel     case R_CM_AVEOCTL ... R_CM_EMMCDIV:
50172813624SLuc Michel     case R_CM_EMMC2CTL ... R_CM_EMMC2DIV:
50272813624SLuc Michel         update_mux_from_cm(s, idx);
50372813624SLuc Michel         break;
504fc14176bSLuc Michel     }
5051e986e25SLuc Michel }
5061e986e25SLuc Michel 
50709d56bbcSLuc Michel #undef CASE_PLL_A2W_REGS
508fc14176bSLuc Michel 
509fc14176bSLuc Michel static const MemoryRegionOps cprman_ops = {
510fc14176bSLuc Michel     .read = cprman_read,
511fc14176bSLuc Michel     .write = cprman_write,
512fc14176bSLuc Michel     .endianness = DEVICE_LITTLE_ENDIAN,
513fc14176bSLuc Michel     .valid = {
514fc14176bSLuc Michel         /*
515fc14176bSLuc Michel          * Although this hasn't been checked against real hardware, nor the
516fc14176bSLuc Michel          * information can be found in a datasheet, it seems reasonable because
517fc14176bSLuc Michel          * of the "PASSWORD" magic value found in every registers.
518fc14176bSLuc Michel          */
519fc14176bSLuc Michel         .min_access_size        = 4,
520fc14176bSLuc Michel         .max_access_size        = 4,
521fc14176bSLuc Michel         .unaligned              = false,
522fc14176bSLuc Michel     },
523fc14176bSLuc Michel     .impl = {
524fc14176bSLuc Michel         .max_access_size = 4,
525fc14176bSLuc Michel     },
526fc14176bSLuc Michel };
527fc14176bSLuc Michel 
528fc14176bSLuc Michel static void cprman_reset(DeviceState *dev)
529fc14176bSLuc Michel {
530fc14176bSLuc Michel     BCM2835CprmanState *s = CPRMAN(dev);
5311e986e25SLuc Michel     size_t i;
532fc14176bSLuc Michel 
533fc14176bSLuc Michel     memset(s->regs, 0, sizeof(s->regs));
534fc14176bSLuc Michel 
5351e986e25SLuc Michel     for (i = 0; i < CPRMAN_NUM_PLL; i++) {
5361e986e25SLuc Michel         device_cold_reset(DEVICE(&s->plls[i]));
5371e986e25SLuc Michel     }
5381e986e25SLuc Michel 
53909d56bbcSLuc Michel     for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
54009d56bbcSLuc Michel         device_cold_reset(DEVICE(&s->channels[i]));
54109d56bbcSLuc Michel     }
54209d56bbcSLuc Michel 
54372813624SLuc Michel     for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
54472813624SLuc Michel         device_cold_reset(DEVICE(&s->clock_muxes[i]));
54572813624SLuc Michel     }
54672813624SLuc Michel 
547fc14176bSLuc Michel     clock_update_hz(s->xosc, s->xosc_freq);
548fc14176bSLuc Michel }
549fc14176bSLuc Michel 
550fc14176bSLuc Michel static void cprman_init(Object *obj)
551fc14176bSLuc Michel {
552fc14176bSLuc Michel     BCM2835CprmanState *s = CPRMAN(obj);
5531e986e25SLuc Michel     size_t i;
5541e986e25SLuc Michel 
5551e986e25SLuc Michel     for (i = 0; i < CPRMAN_NUM_PLL; i++) {
5561e986e25SLuc Michel         object_initialize_child(obj, PLL_INIT_INFO[i].name,
5571e986e25SLuc Michel                                 &s->plls[i], TYPE_CPRMAN_PLL);
5581e986e25SLuc Michel         set_pll_init_info(s, &s->plls[i], i);
5591e986e25SLuc Michel     }
560fc14176bSLuc Michel 
56109d56bbcSLuc Michel     for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
56209d56bbcSLuc Michel         object_initialize_child(obj, PLL_CHANNEL_INIT_INFO[i].name,
56309d56bbcSLuc Michel                                 &s->channels[i],
56409d56bbcSLuc Michel                                 TYPE_CPRMAN_PLL_CHANNEL);
56509d56bbcSLuc Michel         set_pll_channel_init_info(s, &s->channels[i], i);
56609d56bbcSLuc Michel     }
56709d56bbcSLuc Michel 
56872813624SLuc Michel     for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
56972813624SLuc Michel         char *alias;
57072813624SLuc Michel 
57172813624SLuc Michel         object_initialize_child(obj, CLOCK_MUX_INIT_INFO[i].name,
57272813624SLuc Michel                                 &s->clock_muxes[i],
57372813624SLuc Michel                                 TYPE_CPRMAN_CLOCK_MUX);
57472813624SLuc Michel         set_clock_mux_init_info(s, &s->clock_muxes[i], i);
57572813624SLuc Michel 
57672813624SLuc Michel         /* Expose muxes output as CPRMAN outputs */
57772813624SLuc Michel         alias = g_strdup_printf("%s-out", CLOCK_MUX_INIT_INFO[i].name);
57872813624SLuc Michel         qdev_alias_clock(DEVICE(&s->clock_muxes[i]), "out", DEVICE(obj), alias);
57972813624SLuc Michel         g_free(alias);
58072813624SLuc Michel     }
58172813624SLuc Michel 
582fc14176bSLuc Michel     s->xosc = clock_new(obj, "xosc");
58372813624SLuc Michel     s->gnd = clock_new(obj, "gnd");
58472813624SLuc Michel 
58572813624SLuc Michel     clock_set(s->gnd, 0);
586fc14176bSLuc Michel 
587fc14176bSLuc Michel     memory_region_init_io(&s->iomem, obj, &cprman_ops,
588fc14176bSLuc Michel                           s, "bcm2835-cprman", 0x2000);
589fc14176bSLuc Michel     sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
590fc14176bSLuc Michel }
591fc14176bSLuc Michel 
59272813624SLuc Michel static void connect_mux_sources(BCM2835CprmanState *s,
59372813624SLuc Michel                                 CprmanClockMuxState *mux,
59472813624SLuc Michel                                 const CprmanPllChannel *clk_mapping)
59572813624SLuc Michel {
59672813624SLuc Michel     size_t i;
59772813624SLuc Michel     Clock *td0 = s->clock_muxes[CPRMAN_CLOCK_TD0].out;
59872813624SLuc Michel     Clock *td1 = s->clock_muxes[CPRMAN_CLOCK_TD1].out;
59972813624SLuc Michel 
60072813624SLuc Michel     /* For sources from 0 to 3. Source 4 to 9 are mux specific */
60172813624SLuc Michel     Clock * const CLK_SRC_MAPPING[] = {
60272813624SLuc Michel         [CPRMAN_CLOCK_SRC_GND] = s->gnd,
60372813624SLuc Michel         [CPRMAN_CLOCK_SRC_XOSC] = s->xosc,
60472813624SLuc Michel         [CPRMAN_CLOCK_SRC_TD0] = td0,
60572813624SLuc Michel         [CPRMAN_CLOCK_SRC_TD1] = td1,
60672813624SLuc Michel     };
60772813624SLuc Michel 
60872813624SLuc Michel     for (i = 0; i < CPRMAN_NUM_CLOCK_MUX_SRC; i++) {
60972813624SLuc Michel         CprmanPllChannel mapping = clk_mapping[i];
61072813624SLuc Michel         Clock *src;
61172813624SLuc Michel 
61272813624SLuc Michel         if (mapping == CPRMAN_CLOCK_SRC_FORCE_GROUND) {
61372813624SLuc Michel             src = s->gnd;
61472813624SLuc Michel         } else if (mapping == CPRMAN_CLOCK_SRC_DSI0HSCK) {
61572813624SLuc Michel             src = s->gnd; /* TODO */
61672813624SLuc Michel         } else if (i < CPRMAN_CLOCK_SRC_PLLA) {
61772813624SLuc Michel             src = CLK_SRC_MAPPING[i];
61872813624SLuc Michel         } else {
61972813624SLuc Michel             src = s->channels[mapping].out;
62072813624SLuc Michel         }
62172813624SLuc Michel 
62272813624SLuc Michel         clock_set_source(mux->srcs[i], src);
62372813624SLuc Michel     }
62472813624SLuc Michel }
62572813624SLuc Michel 
6261e986e25SLuc Michel static void cprman_realize(DeviceState *dev, Error **errp)
6271e986e25SLuc Michel {
6281e986e25SLuc Michel     BCM2835CprmanState *s = CPRMAN(dev);
6291e986e25SLuc Michel     size_t i;
6301e986e25SLuc Michel 
6311e986e25SLuc Michel     for (i = 0; i < CPRMAN_NUM_PLL; i++) {
6321e986e25SLuc Michel         CprmanPllState *pll = &s->plls[i];
6331e986e25SLuc Michel 
6341e986e25SLuc Michel         clock_set_source(pll->xosc_in, s->xosc);
6351e986e25SLuc Michel 
6361e986e25SLuc Michel         if (!qdev_realize(DEVICE(pll), NULL, errp)) {
6371e986e25SLuc Michel             return;
6381e986e25SLuc Michel         }
6391e986e25SLuc Michel     }
64009d56bbcSLuc Michel 
64109d56bbcSLuc Michel     for (i = 0; i < CPRMAN_NUM_PLL_CHANNEL; i++) {
64209d56bbcSLuc Michel         CprmanPllChannelState *channel = &s->channels[i];
64309d56bbcSLuc Michel         CprmanPll parent = PLL_CHANNEL_INIT_INFO[i].parent;
64409d56bbcSLuc Michel         Clock *parent_clk = s->plls[parent].out;
64509d56bbcSLuc Michel 
64609d56bbcSLuc Michel         clock_set_source(channel->pll_in, parent_clk);
64709d56bbcSLuc Michel 
64809d56bbcSLuc Michel         if (!qdev_realize(DEVICE(channel), NULL, errp)) {
64909d56bbcSLuc Michel             return;
65009d56bbcSLuc Michel         }
65109d56bbcSLuc Michel     }
65272813624SLuc Michel 
65372813624SLuc Michel     for (i = 0; i < CPRMAN_NUM_CLOCK_MUX; i++) {
65472813624SLuc Michel         CprmanClockMuxState *clock_mux = &s->clock_muxes[i];
65572813624SLuc Michel 
65672813624SLuc Michel         connect_mux_sources(s, clock_mux, CLOCK_MUX_INIT_INFO[i].src_mapping);
65772813624SLuc Michel 
65872813624SLuc Michel         if (!qdev_realize(DEVICE(clock_mux), NULL, errp)) {
65972813624SLuc Michel             return;
66072813624SLuc Michel         }
66172813624SLuc Michel     }
6621e986e25SLuc Michel }
6631e986e25SLuc Michel 
664fc14176bSLuc Michel static const VMStateDescription cprman_vmstate = {
665fc14176bSLuc Michel     .name = TYPE_BCM2835_CPRMAN,
666fc14176bSLuc Michel     .version_id = 1,
667fc14176bSLuc Michel     .minimum_version_id = 1,
668fc14176bSLuc Michel     .fields = (VMStateField[]) {
669fc14176bSLuc Michel         VMSTATE_UINT32_ARRAY(regs, BCM2835CprmanState, CPRMAN_NUM_REGS),
670fc14176bSLuc Michel         VMSTATE_END_OF_LIST()
671fc14176bSLuc Michel     }
672fc14176bSLuc Michel };
673fc14176bSLuc Michel 
674fc14176bSLuc Michel static Property cprman_properties[] = {
675fc14176bSLuc Michel     DEFINE_PROP_UINT32("xosc-freq-hz", BCM2835CprmanState, xosc_freq, 19200000),
676fc14176bSLuc Michel     DEFINE_PROP_END_OF_LIST()
677fc14176bSLuc Michel };
678fc14176bSLuc Michel 
679fc14176bSLuc Michel static void cprman_class_init(ObjectClass *klass, void *data)
680fc14176bSLuc Michel {
681fc14176bSLuc Michel     DeviceClass *dc = DEVICE_CLASS(klass);
682fc14176bSLuc Michel 
6831e986e25SLuc Michel     dc->realize = cprman_realize;
684fc14176bSLuc Michel     dc->reset = cprman_reset;
685fc14176bSLuc Michel     dc->vmsd = &cprman_vmstate;
686fc14176bSLuc Michel     device_class_set_props(dc, cprman_properties);
687fc14176bSLuc Michel }
688fc14176bSLuc Michel 
689fc14176bSLuc Michel static const TypeInfo cprman_info = {
690fc14176bSLuc Michel     .name = TYPE_BCM2835_CPRMAN,
691fc14176bSLuc Michel     .parent = TYPE_SYS_BUS_DEVICE,
692fc14176bSLuc Michel     .instance_size = sizeof(BCM2835CprmanState),
693fc14176bSLuc Michel     .class_init = cprman_class_init,
694fc14176bSLuc Michel     .instance_init = cprman_init,
695fc14176bSLuc Michel };
696fc14176bSLuc Michel 
697fc14176bSLuc Michel static void cprman_register_types(void)
698fc14176bSLuc Michel {
699fc14176bSLuc Michel     type_register_static(&cprman_info);
7001e986e25SLuc Michel     type_register_static(&cprman_pll_info);
70109d56bbcSLuc Michel     type_register_static(&cprman_pll_channel_info);
70272813624SLuc Michel     type_register_static(&cprman_clock_mux_info);
703fc14176bSLuc Michel }
704fc14176bSLuc Michel 
705fc14176bSLuc Michel type_init(cprman_register_types);
706