1 // SPDX-License-Identifier: GPL-2.0-only
3 * Marvell PXA27x family clocks
5 * Copyright (C) 2014 Robert Jarzmik
7 * Heavily inspired from former arch/arm/mach-pxa/clock.c.
9 #include <linux/clk-provider.h>
10 #include <mach/pxa2xx-regs.h>
12 #include <linux/clk.h>
13 #include <linux/clkdev.h>
16 #include <mach/smemc.h>
18 #include <dt-bindings/clock/pxa-clock.h>
22 #define MHz (1000 * 1000)
46 #define PXA27x_CLKCFG(B, HT, T) \
48 ((B) ? CLKCFG_FASTBUS : 0) | \
49 ((HT) ? CLKCFG_HALFTURBO : 0) | \
50 ((T) ? CLKCFG_TURBO : 0))
51 #define PXA27x_CCCR(A, L, N2) (A << 25 | N2 << 7 | L)
53 #define MDCNFG_DRAC2(mdcnfg) (((mdcnfg) >> 21) & 0x3)
54 #define MDCNFG_DRAC0(mdcnfg) (((mdcnfg) >> 5) & 0x3)
56 /* Define the refresh period in mSec for the SDRAM and the number of rows */
57 #define SDRAM_TREF 64 /* standard 64ms SDRAM */
59 static const char * const get_freq_khz
[] = {
60 "core", "run", "cpll", "memory",
64 static int get_sdram_rows(void)
66 static int sdram_rows
;
67 unsigned int drac2
= 0, drac0
= 0;
73 mdcnfg
= readl_relaxed(MDCNFG
);
75 if (mdcnfg
& (MDCNFG_DE2
| MDCNFG_DE3
))
76 drac2
= MDCNFG_DRAC2(mdcnfg
);
78 if (mdcnfg
& (MDCNFG_DE0
| MDCNFG_DE1
))
79 drac0
= MDCNFG_DRAC0(mdcnfg
);
81 sdram_rows
= 1 << (11 + max(drac0
, drac2
));
85 static u32
mdrefr_dri(unsigned int freq_khz
)
87 u32 interval
= freq_khz
* SDRAM_TREF
/ get_sdram_rows();
89 return (interval
- 31) / 32;
93 * Get the clock frequency as reflected by CCSR and the turbo flag.
94 * We assume these values have been applied via a fcs.
95 * If info is not 0 we also display the current settings.
97 unsigned int pxa27x_get_clk_frequency_khz(int info
)
100 unsigned long clks
[5];
103 for (i
= 0; i
< 5; i
++) {
104 clk
= clk_get(NULL
, get_freq_khz
[i
]);
108 clks
[i
] = clk_get_rate(clk
);
113 pr_info("Run Mode clock: %ld.%02ldMHz\n",
114 clks
[1] / 1000000, (clks
[1] % 1000000) / 10000);
115 pr_info("Turbo Mode clock: %ld.%02ldMHz\n",
116 clks
[2] / 1000000, (clks
[2] % 1000000) / 10000);
117 pr_info("Memory clock: %ld.%02ldMHz\n",
118 clks
[3] / 1000000, (clks
[3] % 1000000) / 10000);
119 pr_info("System bus clock: %ld.%02ldMHz\n",
120 clks
[4] / 1000000, (clks
[4] % 1000000) / 10000);
122 return (unsigned int)clks
[0] / KHz
;
125 bool pxa27x_is_ppll_disabled(void)
127 unsigned long ccsr
= readl(CCSR
);
129 return ccsr
& (1 << CCCR_PPDIS_BIT
);
132 #define PXA27X_CKEN(dev_id, con_id, parents, mult_hp, div_hp, \
134 PXA_CKEN(dev_id, con_id, bit, parents, 1, 1, mult_hp, div_hp, \
135 is_lp, CKEN, CKEN_ ## bit, flags)
136 #define PXA27X_PBUS_CKEN(dev_id, con_id, bit, mult_hp, div_hp, delay) \
137 PXA27X_CKEN(dev_id, con_id, pxa27x_pbus_parents, mult_hp, \
138 div_hp, bit, pxa27x_is_ppll_disabled, 0)
140 PARENTS(pxa27x_pbus
) = { "osc_13mhz", "ppll_312mhz" };
141 PARENTS(pxa27x_sbus
) = { "system_bus", "system_bus" };
142 PARENTS(pxa27x_32Mhz_bus
) = { "osc_32_768khz", "osc_32_768khz" };
143 PARENTS(pxa27x_lcd_bus
) = { "lcd_base", "lcd_base" };
144 PARENTS(pxa27x_membus
) = { "lcd_base", "lcd_base" };
146 #define PXA27X_CKEN_1RATE(dev_id, con_id, bit, parents, delay) \
147 PXA_CKEN_1RATE(dev_id, con_id, bit, parents, \
148 CKEN, CKEN_ ## bit, 0)
149 #define PXA27X_CKEN_1RATE_AO(dev_id, con_id, bit, parents, delay) \
150 PXA_CKEN_1RATE(dev_id, con_id, bit, parents, \
151 CKEN, CKEN_ ## bit, CLK_IGNORE_UNUSED)
153 static struct desc_clk_cken pxa27x_clocks
[] __initdata
= {
154 PXA27X_PBUS_CKEN("pxa2xx-uart.0", NULL
, FFUART
, 2, 42, 1),
155 PXA27X_PBUS_CKEN("pxa2xx-uart.1", NULL
, BTUART
, 2, 42, 1),
156 PXA27X_PBUS_CKEN("pxa2xx-uart.2", NULL
, STUART
, 2, 42, 1),
157 PXA27X_PBUS_CKEN("pxa2xx-i2s", NULL
, I2S
, 2, 51, 0),
158 PXA27X_PBUS_CKEN("pxa2xx-i2c.0", NULL
, I2C
, 2, 19, 0),
159 PXA27X_PBUS_CKEN("pxa27x-udc", NULL
, USB
, 2, 13, 5),
160 PXA27X_PBUS_CKEN("pxa2xx-mci.0", NULL
, MMC
, 2, 32, 0),
161 PXA27X_PBUS_CKEN("pxa2xx-ir", "FICPCLK", FICP
, 2, 13, 0),
162 PXA27X_PBUS_CKEN("pxa27x-ohci", NULL
, USBHOST
, 2, 13, 0),
163 PXA27X_PBUS_CKEN("pxa2xx-i2c.1", NULL
, PWRI2C
, 1, 24, 0),
164 PXA27X_PBUS_CKEN("pxa27x-ssp.0", NULL
, SSP1
, 1, 24, 0),
165 PXA27X_PBUS_CKEN("pxa27x-ssp.1", NULL
, SSP2
, 1, 24, 0),
166 PXA27X_PBUS_CKEN("pxa27x-ssp.2", NULL
, SSP3
, 1, 24, 0),
167 PXA27X_PBUS_CKEN("pxa27x-pwm.0", NULL
, PWM0
, 1, 24, 0),
168 PXA27X_PBUS_CKEN("pxa27x-pwm.1", NULL
, PWM1
, 1, 24, 0),
169 PXA27X_PBUS_CKEN(NULL
, "MSLCLK", MSL
, 2, 13, 0),
170 PXA27X_PBUS_CKEN(NULL
, "USIMCLK", USIM
, 2, 13, 0),
171 PXA27X_PBUS_CKEN(NULL
, "MSTKCLK", MEMSTK
, 2, 32, 0),
172 PXA27X_PBUS_CKEN(NULL
, "AC97CLK", AC97
, 1, 1, 0),
173 PXA27X_PBUS_CKEN(NULL
, "AC97CONFCLK", AC97CONF
, 1, 1, 0),
174 PXA27X_PBUS_CKEN(NULL
, "OSTIMER0", OSTIMER
, 1, 96, 0),
176 PXA27X_CKEN_1RATE("pxa27x-keypad", NULL
, KEYPAD
,
177 pxa27x_32Mhz_bus_parents
, 0),
178 PXA27X_CKEN_1RATE(NULL
, "IMCLK", IM
, pxa27x_sbus_parents
, 0),
179 PXA27X_CKEN_1RATE("pxa2xx-fb", NULL
, LCD
, pxa27x_lcd_bus_parents
, 0),
180 PXA27X_CKEN_1RATE("pxa27x-camera.0", NULL
, CAMERA
,
181 pxa27x_lcd_bus_parents
, 0),
182 PXA27X_CKEN_1RATE_AO("pxa2xx-pcmcia", NULL
, MEMC
,
183 pxa27x_membus_parents
, 0),
191 * Control variables are A, L, 2N for CCCR; B, HT, T for CLKCFG.
193 * A = 0 => memory controller clock from table 3-7,
194 * A = 1 => memory controller clock = system bus clock
195 * Run mode frequency = 13 MHz * L
196 * Turbo mode frequency = 13 MHz * L * N
197 * System bus frequency = 13 MHz * L / (B + 1)
201 * L = 16 oscillator to run mode ratio
202 * 2N = 6 2 * (turbo mode to run mode ratio)
205 * B = 1 Fast bus mode
206 * HT = 0 Half-Turbo mode
209 * For now, just support some of the combinations in table 3-7 of
210 * PXA27x Processor Family Developer's Manual to simplify frequency
213 static struct pxa2xx_freq pxa27x_freqs
[] = {
214 {104000000, 104000, PXA27x_CCCR(1, 8, 2), 0, PXA27x_CLKCFG(1, 0, 1) },
215 {156000000, 104000, PXA27x_CCCR(1, 8, 3), 0, PXA27x_CLKCFG(1, 0, 1) },
216 {208000000, 208000, PXA27x_CCCR(0, 16, 2), 1, PXA27x_CLKCFG(0, 0, 1) },
217 {312000000, 208000, PXA27x_CCCR(1, 16, 3), 1, PXA27x_CLKCFG(1, 0, 1) },
218 {416000000, 208000, PXA27x_CCCR(1, 16, 4), 1, PXA27x_CLKCFG(1, 0, 1) },
219 {520000000, 208000, PXA27x_CCCR(1, 16, 5), 1, PXA27x_CLKCFG(1, 0, 1) },
220 {624000000, 208000, PXA27x_CCCR(1, 16, 6), 1, PXA27x_CLKCFG(1, 0, 1) },
223 static unsigned long clk_pxa27x_cpll_get_rate(struct clk_hw
*hw
,
224 unsigned long parent_rate
)
226 unsigned long clkcfg
;
228 unsigned int l
, L
, n2
, N
;
229 unsigned long ccsr
= readl(CCSR
);
231 asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg
));
232 t
= clkcfg
& (1 << 0);
233 ht
= clkcfg
& (1 << 2);
235 l
= ccsr
& CCSR_L_MASK
;
236 n2
= (ccsr
& CCSR_N2_MASK
) >> CCSR_N2_SHIFT
;
243 static int clk_pxa27x_cpll_determine_rate(struct clk_hw
*hw
,
244 struct clk_rate_request
*req
)
246 return pxa2xx_determine_rate(req
, pxa27x_freqs
,
247 ARRAY_SIZE(pxa27x_freqs
));
250 static int clk_pxa27x_cpll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
251 unsigned long parent_rate
)
255 pr_debug("%s(rate=%lu parent_rate=%lu)\n", __func__
, rate
, parent_rate
);
256 for (i
= 0; i
< ARRAY_SIZE(pxa27x_freqs
); i
++)
257 if (pxa27x_freqs
[i
].cpll
== rate
)
260 if (i
>= ARRAY_SIZE(pxa27x_freqs
))
263 pxa2xx_cpll_change(&pxa27x_freqs
[i
], mdrefr_dri
, MDREFR
, CCCR
);
267 PARENTS(clk_pxa27x_cpll
) = { "osc_13mhz" };
268 RATE_OPS(clk_pxa27x_cpll
, "cpll");
270 static unsigned long clk_pxa27x_lcd_base_get_rate(struct clk_hw
*hw
,
271 unsigned long parent_rate
)
273 unsigned int l
, osc_forced
;
274 unsigned long ccsr
= readl(CCSR
);
275 unsigned long cccr
= readl(CCCR
);
277 l
= ccsr
& CCSR_L_MASK
;
278 osc_forced
= ccsr
& (1 << CCCR_CPDIS_BIT
);
280 if (cccr
& (1 << CCCR_LCD_26_BIT
))
281 return parent_rate
* 2;
289 return parent_rate
/ 2;
290 return parent_rate
/ 4;
293 static u8
clk_pxa27x_lcd_base_get_parent(struct clk_hw
*hw
)
295 unsigned int osc_forced
;
296 unsigned long ccsr
= readl(CCSR
);
298 osc_forced
= ccsr
& (1 << CCCR_CPDIS_BIT
);
300 return PXA_LCD_13Mhz
;
305 PARENTS(clk_pxa27x_lcd_base
) = { "osc_13mhz", "run" };
306 MUX_RO_RATE_RO_OPS(clk_pxa27x_lcd_base
, "lcd_base");
308 static void __init
pxa27x_register_plls(void)
310 clk_register_fixed_rate(NULL
, "osc_13mhz", NULL
,
311 CLK_GET_RATE_NOCACHE
,
313 clkdev_pxa_register(CLK_OSC32k768
, "osc_32_768khz", NULL
,
314 clk_register_fixed_rate(NULL
, "osc_32_768khz", NULL
,
315 CLK_GET_RATE_NOCACHE
,
317 clk_register_fixed_rate(NULL
, "clk_dummy", NULL
, 0, 0);
318 clk_register_fixed_factor(NULL
, "ppll_312mhz", "osc_13mhz", 0, 24, 1);
321 static u8
clk_pxa27x_core_get_parent(struct clk_hw
*hw
)
323 unsigned long clkcfg
;
324 unsigned int t
, ht
, osc_forced
;
325 unsigned long ccsr
= readl(CCSR
);
327 osc_forced
= ccsr
& (1 << CCCR_CPDIS_BIT
);
329 return PXA_CORE_13Mhz
;
331 asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg
));
332 t
= clkcfg
& (1 << 0);
333 ht
= clkcfg
& (1 << 2);
336 return PXA_CORE_TURBO
;
340 static int clk_pxa27x_core_set_parent(struct clk_hw
*hw
, u8 index
)
342 if (index
> PXA_CORE_TURBO
)
345 pxa2xx_core_turbo_switch(index
== PXA_CORE_TURBO
);
350 static int clk_pxa27x_core_determine_rate(struct clk_hw
*hw
,
351 struct clk_rate_request
*req
)
353 return __clk_mux_determine_rate(hw
, req
);
356 PARENTS(clk_pxa27x_core
) = { "osc_13mhz", "run", "cpll" };
357 MUX_OPS(clk_pxa27x_core
, "core", CLK_SET_RATE_PARENT
);
359 static unsigned long clk_pxa27x_run_get_rate(struct clk_hw
*hw
,
360 unsigned long parent_rate
)
362 unsigned long ccsr
= readl(CCSR
);
363 unsigned int n2
= (ccsr
& CCSR_N2_MASK
) >> CCSR_N2_SHIFT
;
365 return (parent_rate
/ n2
) * 2;
367 PARENTS(clk_pxa27x_run
) = { "cpll" };
368 RATE_RO_OPS(clk_pxa27x_run
, "run");
370 static void __init
pxa27x_register_core(void)
372 clkdev_pxa_register(CLK_NONE
, "cpll", NULL
,
373 clk_register_clk_pxa27x_cpll());
374 clkdev_pxa_register(CLK_NONE
, "run", NULL
,
375 clk_register_clk_pxa27x_run());
376 clkdev_pxa_register(CLK_CORE
, "core", NULL
,
377 clk_register_clk_pxa27x_core());
380 static unsigned long clk_pxa27x_system_bus_get_rate(struct clk_hw
*hw
,
381 unsigned long parent_rate
)
383 unsigned long clkcfg
;
384 unsigned int b
, osc_forced
;
385 unsigned long ccsr
= readl(CCSR
);
387 osc_forced
= ccsr
& (1 << CCCR_CPDIS_BIT
);
388 asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg
));
389 b
= clkcfg
& (1 << 3);
396 return parent_rate
/ 2;
399 static u8
clk_pxa27x_system_bus_get_parent(struct clk_hw
*hw
)
401 unsigned int osc_forced
;
402 unsigned long ccsr
= readl(CCSR
);
404 osc_forced
= ccsr
& (1 << CCCR_CPDIS_BIT
);
406 return PXA_BUS_13Mhz
;
411 PARENTS(clk_pxa27x_system_bus
) = { "osc_13mhz", "run" };
412 MUX_RO_RATE_RO_OPS(clk_pxa27x_system_bus
, "system_bus");
414 static unsigned long clk_pxa27x_memory_get_rate(struct clk_hw
*hw
,
415 unsigned long parent_rate
)
417 unsigned int a
, l
, osc_forced
;
418 unsigned long cccr
= readl(CCCR
);
419 unsigned long ccsr
= readl(CCSR
);
421 osc_forced
= ccsr
& (1 << CCCR_CPDIS_BIT
);
422 a
= cccr
& (1 << CCCR_A_BIT
);
423 l
= ccsr
& CCSR_L_MASK
;
430 return parent_rate
/ 2;
431 return parent_rate
/ 4;
434 static u8
clk_pxa27x_memory_get_parent(struct clk_hw
*hw
)
436 unsigned int osc_forced
, a
;
437 unsigned long cccr
= readl(CCCR
);
438 unsigned long ccsr
= readl(CCSR
);
440 osc_forced
= ccsr
& (1 << CCCR_CPDIS_BIT
);
441 a
= cccr
& (1 << CCCR_A_BIT
);
443 return PXA_MEM_13Mhz
;
445 return PXA_MEM_SYSTEM_BUS
;
450 PARENTS(clk_pxa27x_memory
) = { "osc_13mhz", "system_bus", "run" };
451 MUX_RO_RATE_RO_OPS(clk_pxa27x_memory
, "memory");
453 #define DUMMY_CLK(_con_id, _dev_id, _parent) \
454 { .con_id = _con_id, .dev_id = _dev_id, .parent = _parent }
460 static struct dummy_clk dummy_clks
[] __initdata
= {
461 DUMMY_CLK(NULL
, "pxa27x-gpio", "osc_32_768khz"),
462 DUMMY_CLK(NULL
, "pxa-rtc", "osc_32_768khz"),
463 DUMMY_CLK(NULL
, "sa1100-rtc", "osc_32_768khz"),
464 DUMMY_CLK("UARTCLK", "pxa2xx-ir", "STUART"),
467 static void __init
pxa27x_dummy_clocks_init(void)
474 for (i
= 0; i
< ARRAY_SIZE(dummy_clks
); i
++) {
476 name
= d
->dev_id
? d
->dev_id
: d
->con_id
;
477 clk
= clk_register_fixed_factor(NULL
, name
, d
->parent
, 0, 1, 1);
478 clk_register_clkdev(clk
, d
->con_id
, d
->dev_id
);
482 static void __init
pxa27x_base_clocks_init(void)
484 pxa27x_register_plls();
485 pxa27x_register_core();
486 clkdev_pxa_register(CLK_NONE
, "system_bus", NULL
,
487 clk_register_clk_pxa27x_system_bus());
488 clkdev_pxa_register(CLK_NONE
, "memory", NULL
,
489 clk_register_clk_pxa27x_memory());
490 clk_register_clk_pxa27x_lcd_base();
493 int __init
pxa27x_clocks_init(void)
495 pxa27x_base_clocks_init();
496 pxa27x_dummy_clocks_init();
497 return clk_pxa_cken_init(pxa27x_clocks
, ARRAY_SIZE(pxa27x_clocks
));
500 static void __init
pxa27x_dt_clocks_init(struct device_node
*np
)
502 pxa27x_clocks_init();
503 clk_pxa_dt_common_init(np
);
505 CLK_OF_DECLARE(pxa_clks
, "marvell,pxa270-clocks", pxa27x_dt_clocks_init
);