Date: | 2010-08-29 08:11:00 (12 years 9 months ago) |
---|---|
Author: | Maarten ter Huurne |
Commit: | e6dfdb74f1eaf2568521b628e27aa62b617de2d7 |
Message: | MIPS: JZ4740: Added setting of PLL rate and main dividers This functionality makes a cpufreq driver possible. Squashed version of the development done in the jz-2.6.39 branch. |
Files: |
arch/mips/jz4740/clock.c (9 diffs) arch/mips/jz4740/clock.h (2 diffs) |
Change Details
arch/mips/jz4740/clock.c | ||
---|---|---|
1 | 1 | /* |
2 | * Copyright (c) 2006-2007, Ingenic Semiconductor Inc. | |
2 | 3 | * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de> |
4 | * Copyright (c) 2010, Ulrich Hecht <ulrich.hecht@gmail.com> | |
5 | * Copyright (c) 2010, Maarten ter Huurne <maarten@treewalker.org> | |
3 | 6 | * JZ4740 SoC clock support |
4 | 7 | * |
5 | 8 | * This program is free software; you can redistribute it and/or modify it |
... | ... | |
41 | 44 | #define JZ_CLOCK_CTRL_I2S_SRC_PLL BIT(31) |
42 | 45 | #define JZ_CLOCK_CTRL_KO_ENABLE BIT(30) |
43 | 46 | #define JZ_CLOCK_CTRL_UDC_SRC_PLL BIT(29) |
44 | #define JZ_CLOCK_CTRL_UDIV_MASK 0x1f800000 | |
45 | 47 | #define JZ_CLOCK_CTRL_CHANGE_ENABLE BIT(22) |
46 | 48 | #define JZ_CLOCK_CTRL_PLL_HALF BIT(21) |
47 | #define JZ_CLOCK_CTRL_LDIV_MASK 0x001f0000 | |
48 | 49 | #define JZ_CLOCK_CTRL_UDIV_OFFSET 23 |
49 | 50 | #define JZ_CLOCK_CTRL_LDIV_OFFSET 16 |
50 | 51 | #define JZ_CLOCK_CTRL_MDIV_OFFSET 12 |
51 | 52 | #define JZ_CLOCK_CTRL_PDIV_OFFSET 8 |
52 | 53 | #define JZ_CLOCK_CTRL_HDIV_OFFSET 4 |
53 | 54 | #define JZ_CLOCK_CTRL_CDIV_OFFSET 0 |
55 | #define JZ_CLOCK_CTRL_UDIV_MASK (0x3f << JZ_CLOCK_CTRL_UDIV_OFFSET) | |
56 | #define JZ_CLOCK_CTRL_LDIV_MASK (0x1f << JZ_CLOCK_CTRL_LDIV_OFFSET) | |
57 | #define JZ_CLOCK_CTRL_MDIV_MASK (0x0f << JZ_CLOCK_CTRL_MDIV_OFFSET) | |
58 | #define JZ_CLOCK_CTRL_PDIV_MASK (0x0f << JZ_CLOCK_CTRL_PDIV_OFFSET) | |
59 | #define JZ_CLOCK_CTRL_HDIV_MASK (0x0f << JZ_CLOCK_CTRL_HDIV_OFFSET) | |
60 | #define JZ_CLOCK_CTRL_CDIV_MASK (0x0f << JZ_CLOCK_CTRL_CDIV_OFFSET) | |
54 | 61 | |
55 | 62 | #define JZ_CLOCK_GATE_UART0 BIT(0) |
56 | 63 | #define JZ_CLOCK_GATE_TCU BIT(1) |
... | ... | |
90 | 97 | #define JZ_CLOCK_PLL_M_OFFSET 23 |
91 | 98 | #define JZ_CLOCK_PLL_N_OFFSET 18 |
92 | 99 | #define JZ_CLOCK_PLL_OD_OFFSET 16 |
100 | #define JZ_CLOCK_PLL_STABILIZE_OFFSET 0 | |
93 | 101 | |
94 | 102 | #define JZ_CLOCK_LOW_POWER_MODE_DOZE BIT(2) |
95 | 103 | #define JZ_CLOCK_LOW_POWER_MODE_SLEEP BIT(0) |
... | ... | |
97 | 105 | #define JZ_CLOCK_SLEEP_CTRL_SUSPEND_UHC BIT(7) |
98 | 106 | #define JZ_CLOCK_SLEEP_CTRL_ENABLE_UDC BIT(6) |
99 | 107 | |
108 | #define JZ_REG_EMC_RTCNT 0x88 | |
109 | #define JZ_REG_EMC_RTCOR 0x8C | |
110 | ||
100 | 111 | static void __iomem *jz_clock_base; |
101 | 112 | static spinlock_t jz_clock_lock; |
102 | 113 | static LIST_HEAD(jz_clocks); |
103 | 114 | |
115 | static void __iomem *jz_emc_base; | |
116 | ||
104 | 117 | struct main_clk { |
105 | 118 | struct clk clk; |
106 | 119 | uint32_t div_offset; |
... | ... | |
204 | 217 | return !!(jz_clk_reg_read(JZ_REG_CLOCK_CTRL) & JZ_CLOCK_CTRL_KO_ENABLE); |
205 | 218 | } |
206 | 219 | |
220 | static struct static_clk jz_clk_ext; | |
221 | ||
222 | static unsigned long jz_clk_pll_calc_rate( | |
223 | unsigned int in_div, unsigned int feedback, unsigned int out_div) | |
224 | { | |
225 | return ((jz_clk_ext.rate / in_div) * feedback) / out_div; | |
226 | } | |
227 | ||
228 | static void jz_clk_pll_calc_dividers(unsigned long rate, | |
229 | unsigned int *in_div, unsigned int *feedback, unsigned int *out_div) | |
230 | { | |
231 | unsigned int target; | |
232 | ||
233 | /* The frequency after the input divider must be between 1 and 15 MHz. | |
234 | The highest divider yields the best resolution. */ | |
235 | *in_div = jz_clk_ext.rate / 1000000; | |
236 | if (*in_div >= 34) | |
237 | *in_div = 33; | |
238 | ||
239 | /* The frequency before the output divider must be between 100 and | |
240 | 500 MHz. The lowest target rate is more energy efficient. */ | |
241 | if (rate < 25000000) { | |
242 | *out_div = 4; | |
243 | target = 25000000 * 4; | |
244 | } else if (rate <= 50000000) { | |
245 | *out_div = 4; | |
246 | target = rate * 4; | |
247 | } else if (rate <= 100000000) { | |
248 | *out_div = 2; | |
249 | target = rate * 2; | |
250 | } else if (rate <= 500000000) { | |
251 | *out_div = 1; | |
252 | target = rate; | |
253 | } else { | |
254 | *out_div = 1; | |
255 | target = 500000000; | |
256 | } | |
257 | ||
258 | /* Compute the feedback divider. | |
259 | Since the divided input is at least 1 MHz and the target frequency | |
260 | at most 500 MHz, the feedback will be at most 500 and will therefore | |
261 | always fit in the 9-bit register. | |
262 | Similarly, the divided input is at most 15 MHz and the target | |
263 | frequency at least 100 MHz, so the feedback will be at least 6 | |
264 | where the minimum supported value is 2. */ | |
265 | *feedback = ((target / 1000) * *in_div) / (jz_clk_ext.rate / 1000); | |
266 | } | |
267 | ||
268 | static unsigned long jz_clk_pll_round_rate(struct clk *clk, unsigned long rate) | |
269 | { | |
270 | unsigned int in_div, feedback, out_div; | |
271 | /* The PLL frequency must be a multiple of 24 MHz, since the LCD pixel | |
272 | * clock must be exactly 12 MHz for the TV-out to work. | |
273 | * TODO: A multiple of 12 MHz for the PLL would work if the PLL would | |
274 | * not be divided by 2 before being passed to the set of derived | |
275 | * clocks that includes the LCD pixel clock. | |
276 | * TODO: Systemwide decisions like this should be made by the board | |
277 | * support code, so add some kind of hook for that. | |
278 | */ | |
279 | unsigned long rate24 = (rate / 24000000) * 24000000; | |
280 | ||
281 | jz_clk_pll_calc_dividers(rate24, &in_div, &feedback, &out_div); | |
282 | return jz_clk_pll_calc_rate(in_div, feedback, out_div); | |
283 | } | |
284 | ||
207 | 285 | static const int pllno[] = {1, 2, 2, 4}; |
208 | 286 | |
209 | 287 | static unsigned long jz_clk_pll_get_rate(struct clk *clk) |
210 | 288 | { |
211 | 289 | uint32_t val; |
212 | int m; | |
213 | int n; | |
214 | int od; | |
290 | unsigned int in_div, feedback, out_div; | |
215 | 291 | |
216 | 292 | val = jz_clk_reg_read(JZ_REG_CLOCK_PLL); |
217 | 293 | |
218 | 294 | if (val & JZ_CLOCK_PLL_BYPASS) |
219 | 295 | return clk_get_rate(clk->parent); |
220 | 296 | |
221 | m = ((val >> 23) & 0x1ff) + 2; | |
222 | n = ((val >> 18) & 0x1f) + 2; | |
223 | od = (val >> 16) & 0x3; | |
297 | feedback = ((val >> 23) & 0x1ff) + 2; | |
298 | in_div = ((val >> 18) & 0x1f) + 2; | |
299 | out_div = pllno[(val >> 16) & 0x3]; | |
224 | 300 | |
225 | return ((clk_get_rate(clk->parent) / n) * m) / pllno[od]; | |
301 | return jz_clk_pll_calc_rate(in_div, feedback, out_div); | |
226 | 302 | } |
227 | 303 | |
228 | 304 | static unsigned long jz_clk_pll_half_get_rate(struct clk *clk) |
... | ... | |
235 | 311 | return jz_clk_pll_get_rate(clk->parent) >> 1; |
236 | 312 | } |
237 | 313 | |
238 | static const int jz_clk_main_divs[] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32}; | |
314 | #define SDRAM_TREF 15625 /* Refresh period: 4096 refresh cycles/64ms */ | |
315 | ||
316 | static void sdram_set_pll(unsigned int pllin) | |
317 | { | |
318 | unsigned int ns, sdramclock; | |
319 | ||
320 | ns = 1000000000 / pllin; | |
321 | sdramclock = (SDRAM_TREF / ns) / 64 + 1; | |
322 | if (sdramclock > 0xff) sdramclock = 0xff; | |
323 | /* Set refresh registers */ | |
324 | writew(sdramclock, jz_emc_base + JZ_REG_EMC_RTCOR); | |
325 | writew(sdramclock, jz_emc_base + JZ_REG_EMC_RTCNT); | |
326 | } | |
327 | ||
328 | static int jz_clk_pll_set_rate(struct clk *clk, unsigned long rate) | |
329 | { | |
330 | unsigned int ctrl, plcr1; | |
331 | unsigned int feedback, in_div, out_div, pllout, pllout2; | |
332 | ||
333 | jz_clk_pll_calc_dividers(rate, &in_div, &feedback, &out_div); | |
334 | ||
335 | ctrl = jz_clk_reg_read(JZ_REG_CLOCK_CTRL); | |
336 | pllout = jz_clk_pll_calc_rate(in_div, feedback, out_div); | |
337 | pllout2 = (ctrl & JZ_CLOCK_CTRL_PLL_HALF) ? pllout : (pllout / 2); | |
338 | ||
339 | /* Init UHC clock */ | |
340 | writel(pllout2 / 48000000 - 1, jz_clock_base + JZ_REG_CLOCK_UHC); | |
341 | ||
342 | plcr1 = ((feedback - 2) << JZ_CLOCK_PLL_M_OFFSET) | | |
343 | ((in_div - 2) << JZ_CLOCK_PLL_N_OFFSET) | | |
344 | ((out_div - 1) << JZ_CLOCK_PLL_OD_OFFSET) | | |
345 | (0x20 << JZ_CLOCK_PLL_STABILIZE_OFFSET) | | |
346 | JZ_CLOCK_PLL_ENABLED; | |
347 | ||
348 | sdram_set_pll(pllout); | |
349 | ||
350 | /* LCD pixclock */ | |
351 | writel(pllout2 / 12000000 - 1, jz_clock_base + JZ_REG_CLOCK_LCD); | |
352 | ||
353 | /* configure PLL */ | |
354 | __asm__ __volatile__( | |
355 | ".set noreorder\n\t" | |
356 | ".align 5\n" | |
357 | "sw %1,0(%0)\n\t" | |
358 | "nop\n\t" | |
359 | "nop\n\t" | |
360 | "nop\n\t" | |
361 | "nop\n\t" | |
362 | "nop\n\t" | |
363 | "nop\n\t" | |
364 | "nop\n\t" | |
365 | ".set reorder\n\t" | |
366 | : | |
367 | : "r" (jz_clock_base + JZ_REG_CLOCK_PLL), "r" (plcr1)); | |
368 | ||
369 | /* MtH: For some reason the MSC will have problems if this flag is not | |
370 | restored, even though the MSC is supposedly the only divider | |
371 | that is not affected by this flag. */ | |
372 | jz_clk_reg_set_bits(JZ_REG_CLOCK_CTRL, JZ_CLOCK_CTRL_CHANGE_ENABLE); | |
373 | ||
374 | return 0; | |
375 | } | |
376 | ||
377 | static const unsigned int jz_clk_main_divs[] = { | |
378 | 1, 2, 3, 4, 6, 8, 12, 16, 24, 32 | |
379 | }; | |
380 | static const unsigned int jz_clk_main_divs_inv[] = { | |
381 | -1, 0, 1, 2, 3, -1, 4, -1, 5, -1, -1, -1, 6, -1, -1, -1, | |
382 | 7, -1, -1, -1, -1, -1, -1, -1, 8, -1, -1, -1, -1, -1, -1, -1, | |
383 | 9 | |
384 | }; | |
239 | 385 | |
240 | 386 | static unsigned long jz_clk_main_round_rate(struct clk *clk, unsigned long rate) |
241 | 387 | { |
... | ... | |
290 | 436 | return 0; |
291 | 437 | } |
292 | 438 | |
439 | static struct main_clk jz_clk_cpu; | |
440 | ||
441 | int clk_main_set_dividers(bool immediate, unsigned int cdiv, unsigned int hdiv, | |
442 | unsigned int mdiv, unsigned int pdiv) | |
443 | { | |
444 | unsigned int cdiv_enc, hdiv_enc, mdiv_enc, pdiv_enc; | |
445 | unsigned int ctrl; | |
446 | unsigned int tmp, wait; | |
447 | ||
448 | if (cdiv >= ARRAY_SIZE(jz_clk_main_divs_inv) || | |
449 | hdiv >= ARRAY_SIZE(jz_clk_main_divs_inv) || | |
450 | mdiv >= ARRAY_SIZE(jz_clk_main_divs_inv) || | |
451 | pdiv >= ARRAY_SIZE(jz_clk_main_divs_inv)) | |
452 | return -EINVAL; | |
453 | cdiv_enc = jz_clk_main_divs_inv[cdiv]; | |
454 | hdiv_enc = jz_clk_main_divs_inv[hdiv]; | |
455 | mdiv_enc = jz_clk_main_divs_inv[mdiv]; | |
456 | pdiv_enc = jz_clk_main_divs_inv[pdiv]; | |
457 | if (cdiv_enc == (unsigned int)-1 || | |
458 | hdiv_enc == (unsigned int)-1 || | |
459 | mdiv_enc == (unsigned int)-1 || | |
460 | pdiv_enc == (unsigned int)-1) | |
461 | return -EINVAL; | |
462 | ||
463 | ctrl = jz_clk_reg_read(JZ_REG_CLOCK_CTRL); | |
464 | ctrl &= ~(JZ_CLOCK_CTRL_CHANGE_ENABLE | | |
465 | JZ_CLOCK_CTRL_CDIV_MASK | JZ_CLOCK_CTRL_HDIV_MASK | | |
466 | JZ_CLOCK_CTRL_MDIV_MASK | JZ_CLOCK_CTRL_PDIV_MASK); | |
467 | if (immediate) ctrl |= JZ_CLOCK_CTRL_CHANGE_ENABLE; | |
468 | ctrl |= (cdiv_enc << JZ_CLOCK_CTRL_CDIV_OFFSET) | | |
469 | (hdiv_enc << JZ_CLOCK_CTRL_HDIV_OFFSET) | | |
470 | (mdiv_enc << JZ_CLOCK_CTRL_MDIV_OFFSET) | | |
471 | (pdiv_enc << JZ_CLOCK_CTRL_PDIV_OFFSET); | |
472 | ||
473 | /* set dividers */ | |
474 | /* delay loops lifted from the old Ingenic cpufreq driver */ | |
475 | wait = ((clk_get_rate(&jz_clk_cpu.clk) / 1000000) * 500) / 1000; | |
476 | __asm__ __volatile__( | |
477 | ".set noreorder\n\t" | |
478 | ".align 5\n" | |
479 | "sw %2,0(%1)\n\t" | |
480 | "li %0,0\n\t" | |
481 | "1:\n\t" | |
482 | "bne %0,%3,1b\n\t" | |
483 | "addi %0, 1\n\t" | |
484 | "nop\n\t" | |
485 | "nop\n\t" | |
486 | "nop\n\t" | |
487 | "nop\n\t" | |
488 | ".set reorder\n\t" | |
489 | : "=r" (tmp) | |
490 | : "r" (jz_clock_base + JZ_REG_CLOCK_CTRL), "r" (ctrl), | |
491 | "r" (wait)); | |
492 | ||
493 | return 0; | |
494 | } | |
495 | EXPORT_SYMBOL_GPL(clk_main_set_dividers); | |
496 | ||
293 | 497 | static struct clk_ops jz_clk_static_ops = { |
294 | 498 | .get_rate = jz_clk_static_get_rate, |
295 | 499 | .enable = jz_clk_enable_gating, |
... | ... | |
307 | 511 | |
308 | 512 | static struct clk_ops jz_clk_pll_ops = { |
309 | 513 | .get_rate = jz_clk_pll_get_rate, |
514 | .set_rate = jz_clk_pll_set_rate, | |
515 | .round_rate = jz_clk_pll_round_rate, | |
310 | 516 | }; |
311 | 517 | |
312 | 518 | static struct clk jz_clk_pll = { |
... | ... | |
897 | 1103 | if (!jz_clock_base) |
898 | 1104 | return -EBUSY; |
899 | 1105 | |
1106 | jz_emc_base = ioremap(JZ4740_EMC_BASE_ADDR, 0x100); | |
1107 | if (!jz_emc_base) | |
1108 | return -EBUSY; | |
1109 | ||
900 | 1110 | spin_lock_init(&jz_clock_lock); |
901 | 1111 | |
902 | 1112 | jz_clk_ext.rate = jz4740_clock_bdata.ext_rate; |
arch/mips/jz4740/clock.h | ||
---|---|---|
17 | 17 | #define __MIPS_JZ4740_CLOCK_H__ |
18 | 18 | |
19 | 19 | #include <linux/list.h> |
20 | #include <linux/types.h> | |
20 | 21 | |
21 | 22 | struct jz4740_clock_board_data { |
22 | 23 | unsigned long ext_rate; |
... | ... | |
63 | 64 | |
64 | 65 | int clk_is_enabled(struct clk *clk); |
65 | 66 | |
67 | int clk_main_set_dividers(bool immediate, unsigned int cdiv, unsigned int hdiv, | |
68 | unsigned int mdiv, unsigned int pdiv); | |
69 | ||
66 | 70 | #ifdef CONFIG_DEBUG_FS |
67 | 71 | void jz4740_clock_debugfs_init(void); |
68 | 72 | void jz4740_clock_debugfs_add_clk(struct clk *clk); |
Branches:
ben-wpan
ben-wpan-stefan
5396a9238205f20f811ea57898980d3ca82df0b6
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9