These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / hw / misc / imx_ccm.c
1 /*
2  * IMX31 Clock Control Module
3  *
4  * Copyright (C) 2012 NICTA
5  * Updated by Jean-Christophe Dubois <jcd@tribudubois.net>
6  *
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.
9  *
10  * This is an abstract base class used to get a common interface to
11  * retrieve the CCM frequencies from the various i.MX SOC.
12  */
13
14 #include "qemu/osdep.h"
15 #include "hw/misc/imx_ccm.h"
16
17 #ifndef DEBUG_IMX_CCM
18 #define DEBUG_IMX_CCM 0
19 #endif
20
21 #define DPRINTF(fmt, args...) \
22     do { \
23         if (DEBUG_IMX_CCM) { \
24             fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_CCM, \
25                                              __func__, ##args); \
26         } \
27     } while (0)
28
29
30 uint32_t imx_ccm_get_clock_frequency(IMXCCMState *dev, IMXClk clock)
31 {
32     uint32_t freq = 0;
33     IMXCCMClass *klass = IMX_GET_CLASS(dev);
34
35     if (klass->get_clock_frequency) {
36         freq = klass->get_clock_frequency(dev, clock);
37     }
38
39     DPRINTF("(clock = %d) = %d\n", clock, freq);
40
41     return freq;
42 }
43
44 /*
45  * Calculate PLL output frequency
46  */
47 uint32_t imx_ccm_calc_pll(uint32_t pllreg, uint32_t base_freq)
48 {
49     int32_t freq;
50     int32_t mfn = MFN(pllreg);  /* Numerator */
51     uint32_t mfi = MFI(pllreg); /* Integer part */
52     uint32_t mfd = 1 + MFD(pllreg); /* Denominator */
53     uint32_t pd = 1 + PD(pllreg);   /* Pre-divider */
54
55     if (mfi < 5) {
56         mfi = 5;
57     }
58
59     /* mfn is 10-bit signed twos-complement */
60     mfn <<= 32 - 10;
61     mfn >>= 32 - 10;
62
63     freq = ((2 * (base_freq >> 10) * (mfi * mfd + mfn)) /
64             (mfd * pd)) << 10;
65
66     DPRINTF("(pllreg = 0x%08x, base_freq = %d) = %d\n", pllreg, base_freq,
67             freq);
68
69     return freq;
70 }
71
72 static const TypeInfo imx_ccm_info = {
73     .name          = TYPE_IMX_CCM,
74     .parent        = TYPE_SYS_BUS_DEVICE,
75     .instance_size = sizeof(IMXCCMState),
76     .class_size    = sizeof(IMXCCMClass),
77     .abstract      = true,
78 };
79
80 static void imx_ccm_register_types(void)
81 {
82     type_register_static(&imx_ccm_info);
83 }
84
85 type_init(imx_ccm_register_types)