2 * IMX31 Clock Control Module
4 * Copyright (C) 2012 NICTA
5 * Updated by Jean-Christophe Dubois <jcd@tribudubois.net>
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
10 * To get the timer frequencies right, we need to emulate at least part of
14 #include "hw/misc/imx31_ccm.h"
16 #define CKIH_FREQ 26000000 /* 26MHz crystal input */
18 #ifndef DEBUG_IMX31_CCM
19 #define DEBUG_IMX31_CCM 0
22 #define DPRINTF(fmt, args...) \
24 if (DEBUG_IMX31_CCM) { \
25 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX31_CCM, \
30 static char const *imx31_ccm_reg_name(uint32_t reg
)
90 static const VMStateDescription vmstate_imx31_ccm
= {
91 .name
= TYPE_IMX31_CCM
,
93 .minimum_version_id
= 1,
94 .fields
= (VMStateField
[]) {
95 VMSTATE_UINT32(ccmr
, IMX31CCMState
),
96 VMSTATE_UINT32(pdr0
, IMX31CCMState
),
97 VMSTATE_UINT32(pdr1
, IMX31CCMState
),
98 VMSTATE_UINT32(mpctl
, IMX31CCMState
),
99 VMSTATE_UINT32(spctl
, IMX31CCMState
),
100 VMSTATE_UINT32_ARRAY(cgr
, IMX31CCMState
, 3),
101 VMSTATE_UINT32(pmcr0
, IMX31CCMState
),
102 VMSTATE_UINT32(pmcr1
, IMX31CCMState
),
103 VMSTATE_END_OF_LIST()
107 static uint32_t imx31_ccm_get_pll_ref_clk(IMXCCMState
*dev
)
110 IMX31CCMState
*s
= IMX31_CCM(dev
);
112 if ((s
->ccmr
& CCMR_PRCS
) == 2) {
113 if (s
->ccmr
& CCMR_FPME
) {
115 if (s
->ccmr
& CCMR_FPMF
) {
123 DPRINTF("freq = %d\n", freq
);
128 static uint32_t imx31_ccm_get_mpll_clk(IMXCCMState
*dev
)
131 IMX31CCMState
*s
= IMX31_CCM(dev
);
133 freq
= imx_ccm_calc_pll(s
->mpctl
, imx31_ccm_get_pll_ref_clk(dev
));
135 DPRINTF("freq = %d\n", freq
);
140 static uint32_t imx31_ccm_get_mcu_main_clk(IMXCCMState
*dev
)
143 IMX31CCMState
*s
= IMX31_CCM(dev
);
145 if ((s
->ccmr
& CCMR_MDS
) || !(s
->ccmr
& CCMR_MPE
)) {
146 freq
= imx31_ccm_get_pll_ref_clk(dev
);
148 freq
= imx31_ccm_get_mpll_clk(dev
);
151 DPRINTF("freq = %d\n", freq
);
156 static uint32_t imx31_ccm_get_mcu_clk(IMXCCMState
*dev
)
159 IMX31CCMState
*s
= IMX31_CCM(dev
);
161 freq
= imx31_ccm_get_mcu_main_clk(dev
) / (1 + EXTRACT(s
->pdr0
, MCU
));
163 DPRINTF("freq = %d\n", freq
);
168 static uint32_t imx31_ccm_get_hsp_clk(IMXCCMState
*dev
)
171 IMX31CCMState
*s
= IMX31_CCM(dev
);
173 freq
= imx31_ccm_get_mcu_main_clk(dev
) / (1 + EXTRACT(s
->pdr0
, HSP
));
175 DPRINTF("freq = %d\n", freq
);
180 static uint32_t imx31_ccm_get_hclk_clk(IMXCCMState
*dev
)
183 IMX31CCMState
*s
= IMX31_CCM(dev
);
185 freq
= imx31_ccm_get_mcu_main_clk(dev
) / (1 + EXTRACT(s
->pdr0
, MAX
));
187 DPRINTF("freq = %d\n", freq
);
192 static uint32_t imx31_ccm_get_ipg_clk(IMXCCMState
*dev
)
195 IMX31CCMState
*s
= IMX31_CCM(dev
);
197 freq
= imx31_ccm_get_hclk_clk(dev
) / (1 + EXTRACT(s
->pdr0
, IPG
));
199 DPRINTF("freq = %d\n", freq
);
204 static uint32_t imx31_ccm_get_clock_frequency(IMXCCMState
*dev
, IMXClk clock
)
212 freq
= imx31_ccm_get_mcu_clk(dev
);
215 freq
= imx31_ccm_get_hsp_clk(dev
);
218 freq
= imx31_ccm_get_ipg_clk(dev
);
224 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: unsupported clock %d\n",
225 TYPE_IMX31_CCM
, __func__
, clock
);
229 DPRINTF("Clock = %d) = %d\n", clock
, freq
);
234 static void imx31_ccm_reset(DeviceState
*dev
)
236 IMX31CCMState
*s
= IMX31_CCM(dev
);
240 s
->ccmr
= 0x074b0b7d;
241 s
->pdr0
= 0xff870b48;
242 s
->pdr1
= 0x49fcfe7f;
243 s
->mpctl
= 0x04001800;
244 s
->cgr
[0] = s
->cgr
[1] = s
->cgr
[2] = 0xffffffff;
245 s
->spctl
= 0x04043001;
246 s
->pmcr0
= 0x80209828;
247 s
->pmcr1
= 0x00aa0000;
250 static uint64_t imx31_ccm_read(void *opaque
, hwaddr offset
, unsigned size
)
253 IMX31CCMState
*s
= (IMX31CCMState
*)opaque
;
255 switch (offset
>> 2) {
287 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
288 HWADDR_PRIx
"\n", TYPE_IMX31_CCM
, __func__
, offset
);
292 DPRINTF("reg[%s] => 0x%" PRIx32
"\n", imx31_ccm_reg_name(offset
>> 2),
295 return (uint64_t)value
;
298 static void imx31_ccm_write(void *opaque
, hwaddr offset
, uint64_t value
,
301 IMX31CCMState
*s
= (IMX31CCMState
*)opaque
;
303 DPRINTF("reg[%s] <= 0x%" PRIx32
"\n", imx31_ccm_reg_name(offset
>> 2),
306 switch (offset
>> 2) {
308 s
->ccmr
= CCMR_FPMF
| (value
& 0x3b6fdfff);
311 s
->pdr0
= value
& 0xff9f3fff;
317 s
->mpctl
= value
& 0xbfff3fff;
320 s
->spctl
= value
& 0xbfff3fff;
332 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
333 HWADDR_PRIx
"\n", TYPE_IMX31_CCM
, __func__
, offset
);
338 static const struct MemoryRegionOps imx31_ccm_ops
= {
339 .read
= imx31_ccm_read
,
340 .write
= imx31_ccm_write
,
341 .endianness
= DEVICE_NATIVE_ENDIAN
,
344 * Our device would not work correctly if the guest was doing
345 * unaligned access. This might not be a limitation on the real
346 * device but in practice there is no reason for a guest to access
347 * this device unaligned.
349 .min_access_size
= 4,
350 .max_access_size
= 4,
356 static void imx31_ccm_init(Object
*obj
)
358 DeviceState
*dev
= DEVICE(obj
);
359 SysBusDevice
*sd
= SYS_BUS_DEVICE(obj
);
360 IMX31CCMState
*s
= IMX31_CCM(obj
);
362 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &imx31_ccm_ops
, s
,
363 TYPE_IMX31_CCM
, 0x1000);
364 sysbus_init_mmio(sd
, &s
->iomem
);
367 static void imx31_ccm_class_init(ObjectClass
*klass
, void *data
)
369 DeviceClass
*dc
= DEVICE_CLASS(klass
);
370 IMXCCMClass
*ccm
= IMX_CCM_CLASS(klass
);
372 dc
->reset
= imx31_ccm_reset
;
373 dc
->vmsd
= &vmstate_imx31_ccm
;
374 dc
->desc
= "i.MX31 Clock Control Module";
376 ccm
->get_clock_frequency
= imx31_ccm_get_clock_frequency
;
379 static const TypeInfo imx31_ccm_info
= {
380 .name
= TYPE_IMX31_CCM
,
381 .parent
= TYPE_IMX_CCM
,
382 .instance_size
= sizeof(IMX31CCMState
),
383 .instance_init
= imx31_ccm_init
,
384 .class_init
= imx31_ccm_class_init
,
387 static void imx31_ccm_register_types(void)
389 type_register_static(&imx31_ccm_info
);
392 type_init(imx31_ccm_register_types
)