blob: a5291e0e7004aa4431486a77a0d935a53a4ece8f [file] [log] [blame]
SAN People73a59c12006-01-09 17:05:41 +00001/*
Andrew Victor9d041262007-02-05 11:42:07 +01002 * linux/arch/arm/mach-at91/clock.c
SAN People73a59c12006-01-09 17:05:41 +00003 *
4 * Copyright (C) 2005 David Brownell
5 * Copyright (C) 2005 Ivan Kokshaysky
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/fs.h>
17#include <linux/debugfs.h>
18#include <linux/seq_file.h>
19#include <linux/list.h>
20#include <linux/errno.h>
21#include <linux/err.h>
22#include <linux/spinlock.h>
23#include <linux/delay.h>
24#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010025#include <linux/io.h>
SAN People73a59c12006-01-09 17:05:41 +000026
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/hardware.h>
28#include <mach/at91_pmc.h>
29#include <mach/cpu.h>
SAN People73a59c12006-01-09 17:05:41 +000030
Andrew Victor2eeaaa22006-09-27 10:50:59 +010031#include "clock.h"
Andrew Victor5e38efa2009-12-15 21:57:27 +010032#include "generic.h"
SAN People73a59c12006-01-09 17:05:41 +000033
Andrew Victor55c20c02006-06-20 19:31:39 +010034
SAN People73a59c12006-01-09 17:05:41 +000035/*
36 * There's a lot more which can be done with clocks, including cpufreq
37 * integration, slow clock mode support (for system suspend), letting
38 * PLLB be used at other rates (on boards that don't need USB), etc.
39 */
40
Andrew Victor2eeaaa22006-09-27 10:50:59 +010041#define clk_is_primary(x) ((x)->type & CLK_TYPE_PRIMARY)
42#define clk_is_programmable(x) ((x)->type & CLK_TYPE_PROGRAMMABLE)
43#define clk_is_peripheral(x) ((x)->type & CLK_TYPE_PERIPHERAL)
Andrew Victord481f862006-12-01 11:27:31 +010044#define clk_is_sys(x) ((x)->type & CLK_TYPE_SYSTEM)
SAN People73a59c12006-01-09 17:05:41 +000045
Andrew Victor2eeaaa22006-09-27 10:50:59 +010046
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010047/*
48 * Chips have some kind of clocks : group them by functionality
49 */
Jean-Christophe PLAGNIOL-VILLARD9918cea2012-01-26 14:07:09 +010050#define cpu_has_utmi() ( cpu_is_at91sam9rl() \
Nicolas Ferre11128722011-03-10 19:08:54 +010051 || cpu_is_at91sam9g45() \
52 || cpu_is_at91sam9x5())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010053
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010054#define cpu_has_800M_plla() ( cpu_is_at91sam9g20() \
Nicolas Ferre11128722011-03-10 19:08:54 +010055 || cpu_is_at91sam9g45() \
56 || cpu_is_at91sam9x5())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010057
Nicolas Ferreeab41702009-06-26 15:37:00 +010058#define cpu_has_300M_plla() (cpu_is_at91sam9g10())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010059
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010060#define cpu_has_pllb() (!(cpu_is_at91sam9rl() \
Nicolas Ferre11128722011-03-10 19:08:54 +010061 || cpu_is_at91sam9g45() \
62 || cpu_is_at91sam9x5()))
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010063
Nicolas Ferre11128722011-03-10 19:08:54 +010064#define cpu_has_upll() (cpu_is_at91sam9g45() \
65 || cpu_is_at91sam9x5())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010066
67/* USB host HS & FS */
68#define cpu_has_uhp() (!cpu_is_at91sam9rl())
69
70/* USB device FS only */
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010071#define cpu_has_udpfs() (!(cpu_is_at91sam9rl() \
Nicolas Ferre11128722011-03-10 19:08:54 +010072 || cpu_is_at91sam9g45() \
73 || cpu_is_at91sam9x5()))
74
75#define cpu_has_plladiv2() (cpu_is_at91sam9g45() \
76 || cpu_is_at91sam9x5())
77
78#define cpu_has_mdiv3() (cpu_is_at91sam9g45() \
79 || cpu_is_at91sam9x5())
80
81#define cpu_has_alt_prescaler() (cpu_is_at91sam9x5())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010082
Andrew Victor2eeaaa22006-09-27 10:50:59 +010083static LIST_HEAD(clocks);
84static DEFINE_SPINLOCK(clk_lock);
85
86static u32 at91_pllb_usb_init;
SAN People73a59c12006-01-09 17:05:41 +000087
88/*
89 * Four primary clock sources: two crystal oscillators (32K, main), and
90 * two PLLs. PLLA usually runs the master clock; and PLLB must run at
91 * 48 MHz (unless no USB function clocks are needed). The main clock and
92 * both PLLs are turned off to run in "slow clock mode" (system suspend).
93 */
94static struct clk clk32k = {
95 .name = "clk32k",
96 .rate_hz = AT91_SLOW_CLOCK,
97 .users = 1, /* always on */
98 .id = 0,
Andrew Victor2eeaaa22006-09-27 10:50:59 +010099 .type = CLK_TYPE_PRIMARY,
SAN People73a59c12006-01-09 17:05:41 +0000100};
101static struct clk main_clk = {
102 .name = "main",
Andrew Victor91f8ed82006-06-19 13:20:23 +0100103 .pmc_mask = AT91_PMC_MOSCS, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000104 .id = 1,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100105 .type = CLK_TYPE_PRIMARY,
SAN People73a59c12006-01-09 17:05:41 +0000106};
107static struct clk plla = {
108 .name = "plla",
109 .parent = &main_clk,
Andrew Victor91f8ed82006-06-19 13:20:23 +0100110 .pmc_mask = AT91_PMC_LOCKA, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000111 .id = 2,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100112 .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
SAN People73a59c12006-01-09 17:05:41 +0000113};
114
115static void pllb_mode(struct clk *clk, int is_on)
116{
117 u32 value;
118
119 if (is_on) {
120 is_on = AT91_PMC_LOCKB;
121 value = at91_pllb_usb_init;
122 } else
123 value = 0;
124
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100125 // REVISIT: Add work-around for AT91RM9200 Errata #26 ?
SAN People73a59c12006-01-09 17:05:41 +0000126 at91_sys_write(AT91_CKGR_PLLBR, value);
127
128 do {
129 cpu_relax();
130 } while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on);
131}
132
133static struct clk pllb = {
134 .name = "pllb",
135 .parent = &main_clk,
Andrew Victor91f8ed82006-06-19 13:20:23 +0100136 .pmc_mask = AT91_PMC_LOCKB, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000137 .mode = pllb_mode,
138 .id = 3,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100139 .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
SAN People73a59c12006-01-09 17:05:41 +0000140};
141
142static void pmc_sys_mode(struct clk *clk, int is_on)
143{
144 if (is_on)
145 at91_sys_write(AT91_PMC_SCER, clk->pmc_mask);
146 else
147 at91_sys_write(AT91_PMC_SCDR, clk->pmc_mask);
148}
149
Stelian Pop53d71682008-04-05 21:14:03 +0100150static void pmc_uckr_mode(struct clk *clk, int is_on)
151{
152 unsigned int uckr = at91_sys_read(AT91_CKGR_UCKR);
153
154 if (is_on) {
155 is_on = AT91_PMC_LOCKU;
156 at91_sys_write(AT91_CKGR_UCKR, uckr | clk->pmc_mask);
157 } else
158 at91_sys_write(AT91_CKGR_UCKR, uckr & ~(clk->pmc_mask));
159
160 do {
161 cpu_relax();
162 } while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKU) != is_on);
163}
164
SAN People73a59c12006-01-09 17:05:41 +0000165/* USB function clocks (PLLB must be 48 MHz) */
166static struct clk udpck = {
167 .name = "udpck",
168 .parent = &pllb,
SAN People73a59c12006-01-09 17:05:41 +0000169 .mode = pmc_sys_mode,
170};
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100171struct clk utmi_clk = {
Stelian Pop53d71682008-04-05 21:14:03 +0100172 .name = "utmi_clk",
173 .parent = &main_clk,
174 .pmc_mask = AT91_PMC_UPLLEN, /* in CKGR_UCKR */
175 .mode = pmc_uckr_mode,
176 .type = CLK_TYPE_PLL,
177};
SAN People73a59c12006-01-09 17:05:41 +0000178static struct clk uhpck = {
179 .name = "uhpck",
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100180 /*.parent = ... we choose parent at runtime */
SAN People73a59c12006-01-09 17:05:41 +0000181 .mode = pmc_sys_mode,
182};
183
SAN People73a59c12006-01-09 17:05:41 +0000184
185/*
186 * The master clock is divided from the CPU clock (by 1-4). It's used for
187 * memory, interfaces to on-chip peripherals, the AIC, and sometimes more
188 * (e.g baud rate generation). It's sourced from one of the primary clocks.
189 */
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100190struct clk mck = {
SAN People73a59c12006-01-09 17:05:41 +0000191 .name = "mck",
Andrew Victor91f8ed82006-06-19 13:20:23 +0100192 .pmc_mask = AT91_PMC_MCKRDY, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000193};
194
195static void pmc_periph_mode(struct clk *clk, int is_on)
196{
197 if (is_on)
198 at91_sys_write(AT91_PMC_PCER, clk->pmc_mask);
199 else
200 at91_sys_write(AT91_PMC_PCDR, clk->pmc_mask);
201}
202
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100203static struct clk __init *at91_css_to_clk(unsigned long css)
204{
205 switch (css) {
206 case AT91_PMC_CSS_SLOW:
207 return &clk32k;
208 case AT91_PMC_CSS_MAIN:
209 return &main_clk;
210 case AT91_PMC_CSS_PLLA:
211 return &plla;
212 case AT91_PMC_CSS_PLLB:
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100213 if (cpu_has_upll())
214 /* CSS_PLLB == CSS_UPLL */
215 return &utmi_clk;
216 else if (cpu_has_pllb())
217 return &pllb;
Nicolas Ferre11128722011-03-10 19:08:54 +0100218 break;
219 /* alternate PMC: can use master clock */
220 case AT91_PMC_CSS_MASTER:
221 return &mck;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100222 }
SAN People73a59c12006-01-09 17:05:41 +0000223
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100224 return NULL;
225}
SAN People73a59c12006-01-09 17:05:41 +0000226
Nicolas Ferre11128722011-03-10 19:08:54 +0100227static int pmc_prescaler_divider(u32 reg)
228{
229 if (cpu_has_alt_prescaler()) {
230 return 1 << ((reg & AT91_PMC_ALT_PRES) >> PMC_ALT_PRES_OFFSET);
231 } else {
232 return 1 << ((reg & AT91_PMC_PRES) >> PMC_PRES_OFFSET);
233 }
234}
235
SAN People73a59c12006-01-09 17:05:41 +0000236static void __clk_enable(struct clk *clk)
237{
238 if (clk->parent)
239 __clk_enable(clk->parent);
240 if (clk->users++ == 0 && clk->mode)
241 clk->mode(clk, 1);
242}
243
244int clk_enable(struct clk *clk)
245{
246 unsigned long flags;
247
248 spin_lock_irqsave(&clk_lock, flags);
249 __clk_enable(clk);
250 spin_unlock_irqrestore(&clk_lock, flags);
251 return 0;
252}
253EXPORT_SYMBOL(clk_enable);
254
255static void __clk_disable(struct clk *clk)
256{
257 BUG_ON(clk->users == 0);
258 if (--clk->users == 0 && clk->mode)
259 clk->mode(clk, 0);
260 if (clk->parent)
261 __clk_disable(clk->parent);
262}
263
264void clk_disable(struct clk *clk)
265{
266 unsigned long flags;
267
268 spin_lock_irqsave(&clk_lock, flags);
269 __clk_disable(clk);
270 spin_unlock_irqrestore(&clk_lock, flags);
271}
272EXPORT_SYMBOL(clk_disable);
273
274unsigned long clk_get_rate(struct clk *clk)
275{
276 unsigned long flags;
277 unsigned long rate;
278
279 spin_lock_irqsave(&clk_lock, flags);
280 for (;;) {
281 rate = clk->rate_hz;
282 if (rate || !clk->parent)
283 break;
284 clk = clk->parent;
285 }
286 spin_unlock_irqrestore(&clk_lock, flags);
287 return rate;
288}
289EXPORT_SYMBOL(clk_get_rate);
290
291/*------------------------------------------------------------------------*/
292
293#ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
294
295/*
296 * For now, only the programmable clocks support reparenting (MCK could
297 * do this too, with care) or rate changing (the PLLs could do this too,
298 * ditto MCK but that's more for cpufreq). Drivers may reparent to get
299 * a better rate match; we don't.
300 */
301
302long clk_round_rate(struct clk *clk, unsigned long rate)
303{
304 unsigned long flags;
305 unsigned prescale;
306 unsigned long actual;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100307 unsigned long prev = ULONG_MAX;
SAN People73a59c12006-01-09 17:05:41 +0000308
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100309 if (!clk_is_programmable(clk))
SAN People73a59c12006-01-09 17:05:41 +0000310 return -EINVAL;
311 spin_lock_irqsave(&clk_lock, flags);
312
313 actual = clk->parent->rate_hz;
314 for (prescale = 0; prescale < 7; prescale++) {
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100315 if (actual > rate)
316 prev = actual;
317
318 if (actual && actual <= rate) {
319 if ((prev - rate) < (rate - actual)) {
320 actual = prev;
321 prescale--;
322 }
SAN People73a59c12006-01-09 17:05:41 +0000323 break;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100324 }
SAN People73a59c12006-01-09 17:05:41 +0000325 actual >>= 1;
326 }
327
328 spin_unlock_irqrestore(&clk_lock, flags);
329 return (prescale < 7) ? actual : -ENOENT;
330}
331EXPORT_SYMBOL(clk_round_rate);
332
333int clk_set_rate(struct clk *clk, unsigned long rate)
334{
335 unsigned long flags;
336 unsigned prescale;
Nicolas Ferre11128722011-03-10 19:08:54 +0100337 unsigned long prescale_offset, css_mask;
SAN People73a59c12006-01-09 17:05:41 +0000338 unsigned long actual;
339
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100340 if (!clk_is_programmable(clk))
SAN People73a59c12006-01-09 17:05:41 +0000341 return -EINVAL;
342 if (clk->users)
343 return -EBUSY;
Nicolas Ferre11128722011-03-10 19:08:54 +0100344
345 if (cpu_has_alt_prescaler()) {
346 prescale_offset = PMC_ALT_PRES_OFFSET;
347 css_mask = AT91_PMC_ALT_PCKR_CSS;
348 } else {
349 prescale_offset = PMC_PRES_OFFSET;
350 css_mask = AT91_PMC_CSS;
351 }
352
SAN People73a59c12006-01-09 17:05:41 +0000353 spin_lock_irqsave(&clk_lock, flags);
354
355 actual = clk->parent->rate_hz;
356 for (prescale = 0; prescale < 7; prescale++) {
357 if (actual && actual <= rate) {
358 u32 pckr;
359
360 pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
Nicolas Ferre11128722011-03-10 19:08:54 +0100361 pckr &= css_mask; /* keep clock selection */
362 pckr |= prescale << prescale_offset;
SAN People73a59c12006-01-09 17:05:41 +0000363 at91_sys_write(AT91_PMC_PCKR(clk->id), pckr);
364 clk->rate_hz = actual;
365 break;
366 }
367 actual >>= 1;
368 }
369
370 spin_unlock_irqrestore(&clk_lock, flags);
371 return (prescale < 7) ? actual : -ENOENT;
372}
373EXPORT_SYMBOL(clk_set_rate);
374
375struct clk *clk_get_parent(struct clk *clk)
376{
377 return clk->parent;
378}
379EXPORT_SYMBOL(clk_get_parent);
380
381int clk_set_parent(struct clk *clk, struct clk *parent)
382{
383 unsigned long flags;
384
385 if (clk->users)
386 return -EBUSY;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100387 if (!clk_is_primary(parent) || !clk_is_programmable(clk))
SAN People73a59c12006-01-09 17:05:41 +0000388 return -EINVAL;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100389
390 if (cpu_is_at91sam9rl() && parent->id == AT91_PMC_CSS_PLLB)
391 return -EINVAL;
392
SAN People73a59c12006-01-09 17:05:41 +0000393 spin_lock_irqsave(&clk_lock, flags);
394
395 clk->rate_hz = parent->rate_hz;
396 clk->parent = parent;
397 at91_sys_write(AT91_PMC_PCKR(clk->id), parent->id);
398
399 spin_unlock_irqrestore(&clk_lock, flags);
400 return 0;
401}
402EXPORT_SYMBOL(clk_set_parent);
403
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100404/* establish PCK0..PCKN parentage and rate */
David Brownell72e7ae82008-02-06 22:03:42 +0100405static void __init init_programmable_clock(struct clk *clk)
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100406{
407 struct clk *parent;
408 u32 pckr;
Nicolas Ferre11128722011-03-10 19:08:54 +0100409 unsigned int css_mask;
410
411 if (cpu_has_alt_prescaler())
412 css_mask = AT91_PMC_ALT_PCKR_CSS;
413 else
414 css_mask = AT91_PMC_CSS;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100415
416 pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
Nicolas Ferre11128722011-03-10 19:08:54 +0100417 parent = at91_css_to_clk(pckr & css_mask);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100418 clk->parent = parent;
Nicolas Ferre11128722011-03-10 19:08:54 +0100419 clk->rate_hz = parent->rate_hz / pmc_prescaler_divider(pckr);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100420}
421
SAN People73a59c12006-01-09 17:05:41 +0000422#endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
423
424/*------------------------------------------------------------------------*/
425
426#ifdef CONFIG_DEBUG_FS
427
428static int at91_clk_show(struct seq_file *s, void *unused)
429{
Stelian Pop53d71682008-04-05 21:14:03 +0100430 u32 scsr, pcsr, uckr = 0, sr;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100431 struct clk *clk;
SAN People73a59c12006-01-09 17:05:41 +0000432
433 seq_printf(s, "SCSR = %8x\n", scsr = at91_sys_read(AT91_PMC_SCSR));
434 seq_printf(s, "PCSR = %8x\n", pcsr = at91_sys_read(AT91_PMC_PCSR));
SAN People73a59c12006-01-09 17:05:41 +0000435 seq_printf(s, "MOR = %8x\n", at91_sys_read(AT91_CKGR_MOR));
436 seq_printf(s, "MCFR = %8x\n", at91_sys_read(AT91_CKGR_MCFR));
437 seq_printf(s, "PLLA = %8x\n", at91_sys_read(AT91_CKGR_PLLAR));
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100438 if (cpu_has_pllb())
Nicolas Ferreba45ca42008-04-08 13:59:18 +0100439 seq_printf(s, "PLLB = %8x\n", at91_sys_read(AT91_CKGR_PLLBR));
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100440 if (cpu_has_utmi())
Stelian Pop53d71682008-04-05 21:14:03 +0100441 seq_printf(s, "UCKR = %8x\n", uckr = at91_sys_read(AT91_CKGR_UCKR));
SAN People73a59c12006-01-09 17:05:41 +0000442 seq_printf(s, "MCKR = %8x\n", at91_sys_read(AT91_PMC_MCKR));
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100443 if (cpu_has_upll())
444 seq_printf(s, "USB = %8x\n", at91_sys_read(AT91_PMC_USB));
SAN People73a59c12006-01-09 17:05:41 +0000445 seq_printf(s, "SR = %8x\n", sr = at91_sys_read(AT91_PMC_SR));
446
447 seq_printf(s, "\n");
448
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100449 list_for_each_entry(clk, &clocks, node) {
450 char *state;
SAN People73a59c12006-01-09 17:05:41 +0000451
452 if (clk->mode == pmc_sys_mode)
453 state = (scsr & clk->pmc_mask) ? "on" : "off";
454 else if (clk->mode == pmc_periph_mode)
455 state = (pcsr & clk->pmc_mask) ? "on" : "off";
Stelian Pop53d71682008-04-05 21:14:03 +0100456 else if (clk->mode == pmc_uckr_mode)
457 state = (uckr & clk->pmc_mask) ? "on" : "off";
SAN People73a59c12006-01-09 17:05:41 +0000458 else if (clk->pmc_mask)
459 state = (sr & clk->pmc_mask) ? "on" : "off";
460 else if (clk == &clk32k || clk == &main_clk)
461 state = "on";
462 else
463 state = "";
464
Andrew Victor69b648a2006-03-22 20:14:14 +0000465 seq_printf(s, "%-10s users=%2d %-3s %9ld Hz %s\n",
SAN People73a59c12006-01-09 17:05:41 +0000466 clk->name, clk->users, state, clk_get_rate(clk),
467 clk->parent ? clk->parent->name : "");
468 }
469 return 0;
470}
471
472static int at91_clk_open(struct inode *inode, struct file *file)
473{
474 return single_open(file, at91_clk_show, NULL);
475}
476
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800477static const struct file_operations at91_clk_operations = {
SAN People73a59c12006-01-09 17:05:41 +0000478 .open = at91_clk_open,
479 .read = seq_read,
480 .llseek = seq_lseek,
481 .release = single_release,
482};
483
484static int __init at91_clk_debugfs_init(void)
485{
486 /* /sys/kernel/debug/at91_clk */
487 (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
488
489 return 0;
490}
491postcore_initcall(at91_clk_debugfs_init);
492
493#endif
494
495/*------------------------------------------------------------------------*/
496
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100497/* Register a new clock */
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100498static void __init at91_clk_add(struct clk *clk)
499{
500 list_add_tail(&clk->node, &clocks);
501
502 clk->cl.con_id = clk->name;
503 clk->cl.clk = clk;
504 clkdev_add(&clk->cl);
505}
506
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100507int __init clk_register(struct clk *clk)
508{
509 if (clk_is_peripheral(clk)) {
Nicolas Ferre5afddee2010-09-09 19:58:23 +0200510 if (!clk->parent)
511 clk->parent = &mck;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100512 clk->mode = pmc_periph_mode;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100513 }
Andrew Victord481f862006-12-01 11:27:31 +0100514 else if (clk_is_sys(clk)) {
515 clk->parent = &mck;
516 clk->mode = pmc_sys_mode;
Andrew Victord481f862006-12-01 11:27:31 +0100517 }
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100518#ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
519 else if (clk_is_programmable(clk)) {
520 clk->mode = pmc_sys_mode;
521 init_programmable_clock(clk);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100522 }
523#endif
524
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100525 at91_clk_add(clk);
526
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100527 return 0;
528}
529
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100530/*------------------------------------------------------------------------*/
531
SAN People73a59c12006-01-09 17:05:41 +0000532static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
533{
534 unsigned mul, div;
535
536 div = reg & 0xff;
537 mul = (reg >> 16) & 0x7ff;
538 if (div && mul) {
539 freq /= div;
540 freq *= mul + 1;
541 } else
542 freq = 0;
Andrew Victor69b648a2006-03-22 20:14:14 +0000543
SAN People73a59c12006-01-09 17:05:41 +0000544 return freq;
545}
546
Andrew Victor69b648a2006-03-22 20:14:14 +0000547static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
548{
549 if (pll == &pllb && (reg & AT91_PMC_USB96M))
550 return freq / 2;
551 else
552 return freq;
553}
554
SAN People73a59c12006-01-09 17:05:41 +0000555static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
556{
557 unsigned i, div = 0, mul = 0, diff = 1 << 30;
558 unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
559
560 /* PLL output max 240 MHz (or 180 MHz per errata) */
561 if (out_freq > 240000000)
562 goto fail;
563
564 for (i = 1; i < 256; i++) {
565 int diff1;
566 unsigned input, mul1;
567
568 /*
569 * PLL input between 1MHz and 32MHz per spec, but lower
570 * frequences seem necessary in some cases so allow 100K.
sedji gaouaou61352662008-07-10 10:15:35 +0100571 * Warning: some newer products need 2MHz min.
SAN People73a59c12006-01-09 17:05:41 +0000572 */
573 input = main_freq / i;
sedji gaouaou61352662008-07-10 10:15:35 +0100574 if (cpu_is_at91sam9g20() && input < 2000000)
575 continue;
SAN People73a59c12006-01-09 17:05:41 +0000576 if (input < 100000)
577 continue;
578 if (input > 32000000)
579 continue;
580
581 mul1 = out_freq / input;
sedji gaouaou61352662008-07-10 10:15:35 +0100582 if (cpu_is_at91sam9g20() && mul > 63)
583 continue;
SAN People73a59c12006-01-09 17:05:41 +0000584 if (mul1 > 2048)
585 continue;
586 if (mul1 < 2)
587 goto fail;
588
589 diff1 = out_freq - input * mul1;
590 if (diff1 < 0)
591 diff1 = -diff1;
592 if (diff > diff1) {
593 diff = diff1;
594 div = i;
595 mul = mul1;
596 if (diff == 0)
597 break;
598 }
599 }
600 if (i == 256 && diff > (out_freq >> 5))
601 goto fail;
602 return ret | ((mul - 1) << 16) | div;
603fail:
604 return 0;
605}
606
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100607static struct clk *const standard_pmc_clocks[] __initdata = {
608 /* four primary clocks */
609 &clk32k,
610 &main_clk,
611 &plla,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100612
613 /* MCK */
614 &mck
615};
616
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100617/* PLLB generated USB full speed clock init */
618static void __init at91_pllb_usbfs_clock_init(unsigned long main_clock)
619{
620 /*
621 * USB clock init: choose 48 MHz PLLB value,
622 * disable 48MHz clock during usb peripheral suspend.
623 *
624 * REVISIT: assumes MCK doesn't derive from PLLB!
625 */
626 uhpck.parent = &pllb;
627
628 at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M;
629 pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
630 if (cpu_is_at91rm9200()) {
631 uhpck.pmc_mask = AT91RM9200_PMC_UHP;
632 udpck.pmc_mask = AT91RM9200_PMC_UDP;
633 at91_sys_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP);
Nicolas Ferreeab41702009-06-26 15:37:00 +0100634 } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() ||
635 cpu_is_at91sam9263() || cpu_is_at91sam9g20() ||
Jean-Christophe PLAGNIOL-VILLARD7a2207a2011-05-17 20:51:14 +0800636 cpu_is_at91sam9g10()) {
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100637 uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
638 udpck.pmc_mask = AT91SAM926x_PMC_UDP;
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100639 }
640 at91_sys_write(AT91_CKGR_PLLBR, 0);
641
642 udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
643 uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
644}
645
646/* UPLL generated USB full speed clock init */
647static void __init at91_upll_usbfs_clock_init(unsigned long main_clock)
648{
649 /*
650 * USB clock init: choose 480 MHz from UPLL,
651 */
652 unsigned int usbr = AT91_PMC_USBS_UPLL;
653
654 /* Setup divider by 10 to reach 48 MHz */
655 usbr |= ((10 - 1) << 8) & AT91_PMC_OHCIUSBDIV;
656
657 at91_sys_write(AT91_PMC_USB, usbr);
658
659 /* Now set uhpck values */
660 uhpck.parent = &utmi_clk;
661 uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
Ryan Mallon82515442010-06-02 12:55:36 +1200662 uhpck.rate_hz = utmi_clk.rate_hz;
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100663 uhpck.rate_hz /= 1 + ((at91_sys_read(AT91_PMC_USB) & AT91_PMC_OHCIUSBDIV) >> 8);
664}
665
SAN People73a59c12006-01-09 17:05:41 +0000666int __init at91_clock_init(unsigned long main_clock)
667{
668 unsigned tmp, freq, mckr;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100669 int i;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100670 int pll_overclock = false;
SAN People73a59c12006-01-09 17:05:41 +0000671
672 /*
673 * When the bootloader initialized the main oscillator correctly,
674 * there's no problem using the cycle counter. But if it didn't,
675 * or when using oscillator bypass mode, we must be told the speed
676 * of the main clock.
677 */
678 if (!main_clock) {
679 do {
680 tmp = at91_sys_read(AT91_CKGR_MCFR);
Andrew Victor69b648a2006-03-22 20:14:14 +0000681 } while (!(tmp & AT91_PMC_MAINRDY));
682 main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
SAN People73a59c12006-01-09 17:05:41 +0000683 }
684 main_clk.rate_hz = main_clock;
685
686 /* report if PLLA is more than mildly overclocked */
687 plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR));
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100688 if (cpu_has_300M_plla()) {
689 if (plla.rate_hz > 300000000)
690 pll_overclock = true;
691 } else if (cpu_has_800M_plla()) {
692 if (plla.rate_hz > 800000000)
693 pll_overclock = true;
694 } else {
695 if (plla.rate_hz > 209000000)
696 pll_overclock = true;
697 }
698 if (pll_overclock)
SAN People73a59c12006-01-09 17:05:41 +0000699 pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
700
Nicolas Ferre11128722011-03-10 19:08:54 +0100701 if (cpu_has_plladiv2()) {
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100702 mckr = at91_sys_read(AT91_PMC_MCKR);
703 plla.rate_hz /= (1 << ((mckr & AT91_PMC_PLLADIV2) >> 12)); /* plla divisor by 2 */
704 }
SAN People73a59c12006-01-09 17:05:41 +0000705
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100706 if (!cpu_has_pllb() && cpu_has_upll()) {
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100707 /* setup UTMI clock as the fourth primary clock
708 * (instead of pllb) */
709 utmi_clk.type |= CLK_TYPE_PRIMARY;
710 utmi_clk.id = 3;
711 }
712
Andrew Victor69b648a2006-03-22 20:14:14 +0000713
SAN People73a59c12006-01-09 17:05:41 +0000714 /*
Stelian Pop53d71682008-04-05 21:14:03 +0100715 * USB HS clock init
716 */
Andrew Victor5e38efa2009-12-15 21:57:27 +0100717 if (cpu_has_utmi()) {
Stelian Pop53d71682008-04-05 21:14:03 +0100718 /*
719 * multiplier is hard-wired to 40
720 * (obtain the USB High Speed 480 MHz when input is 12 MHz)
721 */
722 utmi_clk.rate_hz = 40 * utmi_clk.parent->rate_hz;
Nicolas Ferre11128722011-03-10 19:08:54 +0100723
724 /* UTMI bias and PLL are managed at the same time */
725 if (cpu_has_upll())
726 utmi_clk.pmc_mask |= AT91_PMC_BIASEN;
Andrew Victor5e38efa2009-12-15 21:57:27 +0100727 }
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100728
729 /*
730 * USB FS clock init
731 */
732 if (cpu_has_pllb())
733 at91_pllb_usbfs_clock_init(main_clock);
734 if (cpu_has_upll())
735 /* assumes that we choose UPLL for USB and not PLLA */
736 at91_upll_usbfs_clock_init(main_clock);
Stelian Pop53d71682008-04-05 21:14:03 +0100737
738 /*
SAN People73a59c12006-01-09 17:05:41 +0000739 * MCK and CPU derive from one of those primary clocks.
740 * For now, assume this parentage won't change.
741 */
742 mckr = at91_sys_read(AT91_PMC_MCKR);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100743 mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
SAN People73a59c12006-01-09 17:05:41 +0000744 freq = mck.parent->rate_hz;
Nicolas Ferre11128722011-03-10 19:08:54 +0100745 freq /= pmc_prescaler_divider(mckr); /* prescale */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100746 if (cpu_is_at91rm9200()) {
Andrew Victora95c7292007-11-19 11:52:09 +0100747 mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100748 } else if (cpu_is_at91sam9g20()) {
sedji gaouaou61352662008-07-10 10:15:35 +0100749 mck.rate_hz = (mckr & AT91_PMC_MDIV) ?
750 freq / ((mckr & AT91_PMC_MDIV) >> 7) : freq; /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
751 if (mckr & AT91_PMC_PDIV)
752 freq /= 2; /* processor clock division */
Nicolas Ferre11128722011-03-10 19:08:54 +0100753 } else if (cpu_has_mdiv3()) {
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100754 mck.rate_hz = (mckr & AT91_PMC_MDIV) == AT91SAM9_PMC_MDIV_3 ?
755 freq / 3 : freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100756 } else {
Andrew Victor5e38efa2009-12-15 21:57:27 +0100757 mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100758 }
SAN People73a59c12006-01-09 17:05:41 +0000759
Nicolas Ferre11128722011-03-10 19:08:54 +0100760 if (cpu_has_alt_prescaler()) {
761 /* Programmable clocks can use MCK */
762 mck.type |= CLK_TYPE_PRIMARY;
763 mck.id = 4;
764 }
765
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100766 /* Register the PMC's standard clocks */
767 for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100768 at91_clk_add(standard_pmc_clocks[i]);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100769
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100770 if (cpu_has_pllb())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100771 at91_clk_add(&pllb);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100772
773 if (cpu_has_uhp())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100774 at91_clk_add(&uhpck);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100775
776 if (cpu_has_udpfs())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100777 at91_clk_add(&udpck);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100778
779 if (cpu_has_utmi())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100780 at91_clk_add(&utmi_clk);
Stelian Pop53d71682008-04-05 21:14:03 +0100781
Andrew Victor91f8ed82006-06-19 13:20:23 +0100782 /* MCK and CPU clock are "always on" */
783 clk_enable(&mck);
784
SAN People73a59c12006-01-09 17:05:41 +0000785 printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
786 freq / 1000000, (unsigned) mck.rate_hz / 1000000,
787 (unsigned) main_clock / 1000000,
788 ((unsigned) main_clock % 1000000) / 1000);
789
Andrew Victorc9b75d12007-02-08 17:36:34 +0100790 return 0;
791}
Andrew Victor91f8ed82006-06-19 13:20:23 +0100792
Andrew Victorc9b75d12007-02-08 17:36:34 +0100793/*
794 * Several unused clocks may be active. Turn them off.
795 */
796static int __init at91_clock_reset(void)
797{
798 unsigned long pcdr = 0;
799 unsigned long scdr = 0;
800 struct clk *clk;
801
802 list_for_each_entry(clk, &clocks, node) {
803 if (clk->users > 0)
804 continue;
805
806 if (clk->mode == pmc_periph_mode)
807 pcdr |= clk->pmc_mask;
808
809 if (clk->mode == pmc_sys_mode)
810 scdr |= clk->pmc_mask;
811
812 pr_debug("Clocks: disable unused %s\n", clk->name);
813 }
814
815 at91_sys_write(AT91_PMC_PCDR, pcdr);
816 at91_sys_write(AT91_PMC_SCDR, scdr);
SAN People73a59c12006-01-09 17:05:41 +0000817
818 return 0;
819}
Andrew Victorc9b75d12007-02-08 17:36:34 +0100820late_initcall(at91_clock_reset);