blob: be51ca7f694d1a0515ddd9d5ecf0c4a9253dc078 [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
Jean-Christophe PLAGNIOL-VILLARD0d781712012-02-05 20:25:32 +080031#include <asm/proc-fns.h>
32
Andrew Victor2eeaaa22006-09-27 10:50:59 +010033#include "clock.h"
Andrew Victor5e38efa2009-12-15 21:57:27 +010034#include "generic.h"
SAN People73a59c12006-01-09 17:05:41 +000035
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +080036void __iomem *at91_pmc_base;
Andrew Victor55c20c02006-06-20 19:31:39 +010037
SAN People73a59c12006-01-09 17:05:41 +000038/*
39 * There's a lot more which can be done with clocks, including cpufreq
40 * integration, slow clock mode support (for system suspend), letting
41 * PLLB be used at other rates (on boards that don't need USB), etc.
42 */
43
Andrew Victor2eeaaa22006-09-27 10:50:59 +010044#define clk_is_primary(x) ((x)->type & CLK_TYPE_PRIMARY)
45#define clk_is_programmable(x) ((x)->type & CLK_TYPE_PROGRAMMABLE)
46#define clk_is_peripheral(x) ((x)->type & CLK_TYPE_PERIPHERAL)
Andrew Victord481f862006-12-01 11:27:31 +010047#define clk_is_sys(x) ((x)->type & CLK_TYPE_SYSTEM)
SAN People73a59c12006-01-09 17:05:41 +000048
Andrew Victor2eeaaa22006-09-27 10:50:59 +010049
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010050/*
51 * Chips have some kind of clocks : group them by functionality
52 */
Jean-Christophe PLAGNIOL-VILLARD9918cea2012-01-26 14:07:09 +010053#define cpu_has_utmi() ( cpu_is_at91sam9rl() \
Nicolas Ferre11128722011-03-10 19:08:54 +010054 || cpu_is_at91sam9g45() \
55 || cpu_is_at91sam9x5())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010056
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010057#define cpu_has_800M_plla() ( cpu_is_at91sam9g20() \
Nicolas Ferre11128722011-03-10 19:08:54 +010058 || cpu_is_at91sam9g45() \
59 || cpu_is_at91sam9x5())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010060
Nicolas Ferreeab41702009-06-26 15:37:00 +010061#define cpu_has_300M_plla() (cpu_is_at91sam9g10())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010062
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010063#define cpu_has_pllb() (!(cpu_is_at91sam9rl() \
Nicolas Ferre11128722011-03-10 19:08:54 +010064 || cpu_is_at91sam9g45() \
65 || cpu_is_at91sam9x5()))
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010066
Nicolas Ferre11128722011-03-10 19:08:54 +010067#define cpu_has_upll() (cpu_is_at91sam9g45() \
68 || cpu_is_at91sam9x5())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010069
70/* USB host HS & FS */
71#define cpu_has_uhp() (!cpu_is_at91sam9rl())
72
73/* USB device FS only */
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010074#define cpu_has_udpfs() (!(cpu_is_at91sam9rl() \
Nicolas Ferre11128722011-03-10 19:08:54 +010075 || cpu_is_at91sam9g45() \
76 || cpu_is_at91sam9x5()))
77
78#define cpu_has_plladiv2() (cpu_is_at91sam9g45() \
79 || cpu_is_at91sam9x5())
80
81#define cpu_has_mdiv3() (cpu_is_at91sam9g45() \
82 || cpu_is_at91sam9x5())
83
84#define cpu_has_alt_prescaler() (cpu_is_at91sam9x5())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010085
Andrew Victor2eeaaa22006-09-27 10:50:59 +010086static LIST_HEAD(clocks);
87static DEFINE_SPINLOCK(clk_lock);
88
89static u32 at91_pllb_usb_init;
SAN People73a59c12006-01-09 17:05:41 +000090
91/*
92 * Four primary clock sources: two crystal oscillators (32K, main), and
93 * two PLLs. PLLA usually runs the master clock; and PLLB must run at
94 * 48 MHz (unless no USB function clocks are needed). The main clock and
95 * both PLLs are turned off to run in "slow clock mode" (system suspend).
96 */
97static struct clk clk32k = {
98 .name = "clk32k",
99 .rate_hz = AT91_SLOW_CLOCK,
100 .users = 1, /* always on */
101 .id = 0,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100102 .type = CLK_TYPE_PRIMARY,
SAN People73a59c12006-01-09 17:05:41 +0000103};
104static struct clk main_clk = {
105 .name = "main",
Andrew Victor91f8ed82006-06-19 13:20:23 +0100106 .pmc_mask = AT91_PMC_MOSCS, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000107 .id = 1,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100108 .type = CLK_TYPE_PRIMARY,
SAN People73a59c12006-01-09 17:05:41 +0000109};
110static struct clk plla = {
111 .name = "plla",
112 .parent = &main_clk,
Andrew Victor91f8ed82006-06-19 13:20:23 +0100113 .pmc_mask = AT91_PMC_LOCKA, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000114 .id = 2,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100115 .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
SAN People73a59c12006-01-09 17:05:41 +0000116};
117
118static void pllb_mode(struct clk *clk, int is_on)
119{
120 u32 value;
121
122 if (is_on) {
123 is_on = AT91_PMC_LOCKB;
124 value = at91_pllb_usb_init;
125 } else
126 value = 0;
127
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100128 // REVISIT: Add work-around for AT91RM9200 Errata #26 ?
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800129 at91_pmc_write(AT91_CKGR_PLLBR, value);
SAN People73a59c12006-01-09 17:05:41 +0000130
131 do {
132 cpu_relax();
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800133 } while ((at91_pmc_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on);
SAN People73a59c12006-01-09 17:05:41 +0000134}
135
136static struct clk pllb = {
137 .name = "pllb",
138 .parent = &main_clk,
Andrew Victor91f8ed82006-06-19 13:20:23 +0100139 .pmc_mask = AT91_PMC_LOCKB, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000140 .mode = pllb_mode,
141 .id = 3,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100142 .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
SAN People73a59c12006-01-09 17:05:41 +0000143};
144
145static void pmc_sys_mode(struct clk *clk, int is_on)
146{
147 if (is_on)
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800148 at91_pmc_write(AT91_PMC_SCER, clk->pmc_mask);
SAN People73a59c12006-01-09 17:05:41 +0000149 else
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800150 at91_pmc_write(AT91_PMC_SCDR, clk->pmc_mask);
SAN People73a59c12006-01-09 17:05:41 +0000151}
152
Stelian Pop53d71682008-04-05 21:14:03 +0100153static void pmc_uckr_mode(struct clk *clk, int is_on)
154{
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800155 unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
Stelian Pop53d71682008-04-05 21:14:03 +0100156
157 if (is_on) {
158 is_on = AT91_PMC_LOCKU;
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800159 at91_pmc_write(AT91_CKGR_UCKR, uckr | clk->pmc_mask);
Stelian Pop53d71682008-04-05 21:14:03 +0100160 } else
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800161 at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(clk->pmc_mask));
Stelian Pop53d71682008-04-05 21:14:03 +0100162
163 do {
164 cpu_relax();
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800165 } while ((at91_pmc_read(AT91_PMC_SR) & AT91_PMC_LOCKU) != is_on);
Stelian Pop53d71682008-04-05 21:14:03 +0100166}
167
SAN People73a59c12006-01-09 17:05:41 +0000168/* USB function clocks (PLLB must be 48 MHz) */
169static struct clk udpck = {
170 .name = "udpck",
171 .parent = &pllb,
SAN People73a59c12006-01-09 17:05:41 +0000172 .mode = pmc_sys_mode,
173};
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100174struct clk utmi_clk = {
Stelian Pop53d71682008-04-05 21:14:03 +0100175 .name = "utmi_clk",
176 .parent = &main_clk,
177 .pmc_mask = AT91_PMC_UPLLEN, /* in CKGR_UCKR */
178 .mode = pmc_uckr_mode,
179 .type = CLK_TYPE_PLL,
180};
SAN People73a59c12006-01-09 17:05:41 +0000181static struct clk uhpck = {
182 .name = "uhpck",
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100183 /*.parent = ... we choose parent at runtime */
SAN People73a59c12006-01-09 17:05:41 +0000184 .mode = pmc_sys_mode,
185};
186
SAN People73a59c12006-01-09 17:05:41 +0000187
188/*
189 * The master clock is divided from the CPU clock (by 1-4). It's used for
190 * memory, interfaces to on-chip peripherals, the AIC, and sometimes more
191 * (e.g baud rate generation). It's sourced from one of the primary clocks.
192 */
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100193struct clk mck = {
SAN People73a59c12006-01-09 17:05:41 +0000194 .name = "mck",
Andrew Victor91f8ed82006-06-19 13:20:23 +0100195 .pmc_mask = AT91_PMC_MCKRDY, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000196};
197
198static void pmc_periph_mode(struct clk *clk, int is_on)
199{
200 if (is_on)
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800201 at91_pmc_write(AT91_PMC_PCER, clk->pmc_mask);
SAN People73a59c12006-01-09 17:05:41 +0000202 else
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800203 at91_pmc_write(AT91_PMC_PCDR, clk->pmc_mask);
SAN People73a59c12006-01-09 17:05:41 +0000204}
205
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100206static struct clk __init *at91_css_to_clk(unsigned long css)
207{
208 switch (css) {
209 case AT91_PMC_CSS_SLOW:
210 return &clk32k;
211 case AT91_PMC_CSS_MAIN:
212 return &main_clk;
213 case AT91_PMC_CSS_PLLA:
214 return &plla;
215 case AT91_PMC_CSS_PLLB:
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100216 if (cpu_has_upll())
217 /* CSS_PLLB == CSS_UPLL */
218 return &utmi_clk;
219 else if (cpu_has_pllb())
220 return &pllb;
Nicolas Ferre11128722011-03-10 19:08:54 +0100221 break;
222 /* alternate PMC: can use master clock */
223 case AT91_PMC_CSS_MASTER:
224 return &mck;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100225 }
SAN People73a59c12006-01-09 17:05:41 +0000226
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100227 return NULL;
228}
SAN People73a59c12006-01-09 17:05:41 +0000229
Nicolas Ferre11128722011-03-10 19:08:54 +0100230static int pmc_prescaler_divider(u32 reg)
231{
232 if (cpu_has_alt_prescaler()) {
233 return 1 << ((reg & AT91_PMC_ALT_PRES) >> PMC_ALT_PRES_OFFSET);
234 } else {
235 return 1 << ((reg & AT91_PMC_PRES) >> PMC_PRES_OFFSET);
236 }
237}
238
SAN People73a59c12006-01-09 17:05:41 +0000239static void __clk_enable(struct clk *clk)
240{
241 if (clk->parent)
242 __clk_enable(clk->parent);
243 if (clk->users++ == 0 && clk->mode)
244 clk->mode(clk, 1);
245}
246
247int clk_enable(struct clk *clk)
248{
249 unsigned long flags;
250
251 spin_lock_irqsave(&clk_lock, flags);
252 __clk_enable(clk);
253 spin_unlock_irqrestore(&clk_lock, flags);
254 return 0;
255}
256EXPORT_SYMBOL(clk_enable);
257
258static void __clk_disable(struct clk *clk)
259{
260 BUG_ON(clk->users == 0);
261 if (--clk->users == 0 && clk->mode)
262 clk->mode(clk, 0);
263 if (clk->parent)
264 __clk_disable(clk->parent);
265}
266
267void clk_disable(struct clk *clk)
268{
269 unsigned long flags;
270
271 spin_lock_irqsave(&clk_lock, flags);
272 __clk_disable(clk);
273 spin_unlock_irqrestore(&clk_lock, flags);
274}
275EXPORT_SYMBOL(clk_disable);
276
277unsigned long clk_get_rate(struct clk *clk)
278{
279 unsigned long flags;
280 unsigned long rate;
281
282 spin_lock_irqsave(&clk_lock, flags);
283 for (;;) {
284 rate = clk->rate_hz;
285 if (rate || !clk->parent)
286 break;
287 clk = clk->parent;
288 }
289 spin_unlock_irqrestore(&clk_lock, flags);
290 return rate;
291}
292EXPORT_SYMBOL(clk_get_rate);
293
294/*------------------------------------------------------------------------*/
295
296#ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
297
298/*
299 * For now, only the programmable clocks support reparenting (MCK could
300 * do this too, with care) or rate changing (the PLLs could do this too,
301 * ditto MCK but that's more for cpufreq). Drivers may reparent to get
302 * a better rate match; we don't.
303 */
304
305long clk_round_rate(struct clk *clk, unsigned long rate)
306{
307 unsigned long flags;
308 unsigned prescale;
309 unsigned long actual;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100310 unsigned long prev = ULONG_MAX;
SAN People73a59c12006-01-09 17:05:41 +0000311
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100312 if (!clk_is_programmable(clk))
SAN People73a59c12006-01-09 17:05:41 +0000313 return -EINVAL;
314 spin_lock_irqsave(&clk_lock, flags);
315
316 actual = clk->parent->rate_hz;
317 for (prescale = 0; prescale < 7; prescale++) {
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100318 if (actual > rate)
319 prev = actual;
320
321 if (actual && actual <= rate) {
322 if ((prev - rate) < (rate - actual)) {
323 actual = prev;
324 prescale--;
325 }
SAN People73a59c12006-01-09 17:05:41 +0000326 break;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100327 }
SAN People73a59c12006-01-09 17:05:41 +0000328 actual >>= 1;
329 }
330
331 spin_unlock_irqrestore(&clk_lock, flags);
332 return (prescale < 7) ? actual : -ENOENT;
333}
334EXPORT_SYMBOL(clk_round_rate);
335
336int clk_set_rate(struct clk *clk, unsigned long rate)
337{
338 unsigned long flags;
339 unsigned prescale;
Nicolas Ferre11128722011-03-10 19:08:54 +0100340 unsigned long prescale_offset, css_mask;
SAN People73a59c12006-01-09 17:05:41 +0000341 unsigned long actual;
342
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100343 if (!clk_is_programmable(clk))
SAN People73a59c12006-01-09 17:05:41 +0000344 return -EINVAL;
345 if (clk->users)
346 return -EBUSY;
Nicolas Ferre11128722011-03-10 19:08:54 +0100347
348 if (cpu_has_alt_prescaler()) {
349 prescale_offset = PMC_ALT_PRES_OFFSET;
350 css_mask = AT91_PMC_ALT_PCKR_CSS;
351 } else {
352 prescale_offset = PMC_PRES_OFFSET;
353 css_mask = AT91_PMC_CSS;
354 }
355
SAN People73a59c12006-01-09 17:05:41 +0000356 spin_lock_irqsave(&clk_lock, flags);
357
358 actual = clk->parent->rate_hz;
359 for (prescale = 0; prescale < 7; prescale++) {
360 if (actual && actual <= rate) {
361 u32 pckr;
362
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800363 pckr = at91_pmc_read(AT91_PMC_PCKR(clk->id));
Nicolas Ferre11128722011-03-10 19:08:54 +0100364 pckr &= css_mask; /* keep clock selection */
365 pckr |= prescale << prescale_offset;
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800366 at91_pmc_write(AT91_PMC_PCKR(clk->id), pckr);
SAN People73a59c12006-01-09 17:05:41 +0000367 clk->rate_hz = actual;
368 break;
369 }
370 actual >>= 1;
371 }
372
373 spin_unlock_irqrestore(&clk_lock, flags);
374 return (prescale < 7) ? actual : -ENOENT;
375}
376EXPORT_SYMBOL(clk_set_rate);
377
378struct clk *clk_get_parent(struct clk *clk)
379{
380 return clk->parent;
381}
382EXPORT_SYMBOL(clk_get_parent);
383
384int clk_set_parent(struct clk *clk, struct clk *parent)
385{
386 unsigned long flags;
387
388 if (clk->users)
389 return -EBUSY;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100390 if (!clk_is_primary(parent) || !clk_is_programmable(clk))
SAN People73a59c12006-01-09 17:05:41 +0000391 return -EINVAL;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100392
393 if (cpu_is_at91sam9rl() && parent->id == AT91_PMC_CSS_PLLB)
394 return -EINVAL;
395
SAN People73a59c12006-01-09 17:05:41 +0000396 spin_lock_irqsave(&clk_lock, flags);
397
398 clk->rate_hz = parent->rate_hz;
399 clk->parent = parent;
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800400 at91_pmc_write(AT91_PMC_PCKR(clk->id), parent->id);
SAN People73a59c12006-01-09 17:05:41 +0000401
402 spin_unlock_irqrestore(&clk_lock, flags);
403 return 0;
404}
405EXPORT_SYMBOL(clk_set_parent);
406
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100407/* establish PCK0..PCKN parentage and rate */
David Brownell72e7ae82008-02-06 22:03:42 +0100408static void __init init_programmable_clock(struct clk *clk)
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100409{
410 struct clk *parent;
411 u32 pckr;
Nicolas Ferre11128722011-03-10 19:08:54 +0100412 unsigned int css_mask;
413
414 if (cpu_has_alt_prescaler())
415 css_mask = AT91_PMC_ALT_PCKR_CSS;
416 else
417 css_mask = AT91_PMC_CSS;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100418
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800419 pckr = at91_pmc_read(AT91_PMC_PCKR(clk->id));
Nicolas Ferre11128722011-03-10 19:08:54 +0100420 parent = at91_css_to_clk(pckr & css_mask);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100421 clk->parent = parent;
Nicolas Ferre11128722011-03-10 19:08:54 +0100422 clk->rate_hz = parent->rate_hz / pmc_prescaler_divider(pckr);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100423}
424
SAN People73a59c12006-01-09 17:05:41 +0000425#endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
426
427/*------------------------------------------------------------------------*/
428
429#ifdef CONFIG_DEBUG_FS
430
431static int at91_clk_show(struct seq_file *s, void *unused)
432{
Stelian Pop53d71682008-04-05 21:14:03 +0100433 u32 scsr, pcsr, uckr = 0, sr;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100434 struct clk *clk;
SAN People73a59c12006-01-09 17:05:41 +0000435
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800436 scsr = at91_pmc_read(AT91_PMC_SCSR);
437 pcsr = at91_pmc_read(AT91_PMC_PCSR);
438 sr = at91_pmc_read(AT91_PMC_SR);
Nicolas Ferre940192e2012-02-23 09:44:37 +0100439 seq_printf(s, "SCSR = %8x\n", scsr);
440 seq_printf(s, "PCSR = %8x\n", pcsr);
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800441 seq_printf(s, "MOR = %8x\n", at91_pmc_read(AT91_CKGR_MOR));
442 seq_printf(s, "MCFR = %8x\n", at91_pmc_read(AT91_CKGR_MCFR));
443 seq_printf(s, "PLLA = %8x\n", at91_pmc_read(AT91_CKGR_PLLAR));
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100444 if (cpu_has_pllb())
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800445 seq_printf(s, "PLLB = %8x\n", at91_pmc_read(AT91_CKGR_PLLBR));
Nicolas Ferre940192e2012-02-23 09:44:37 +0100446 if (cpu_has_utmi()) {
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800447 uckr = at91_pmc_read(AT91_CKGR_UCKR);
Nicolas Ferre940192e2012-02-23 09:44:37 +0100448 seq_printf(s, "UCKR = %8x\n", uckr);
449 }
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800450 seq_printf(s, "MCKR = %8x\n", at91_pmc_read(AT91_PMC_MCKR));
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100451 if (cpu_has_upll())
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800452 seq_printf(s, "USB = %8x\n", at91_pmc_read(AT91_PMC_USB));
Nicolas Ferre940192e2012-02-23 09:44:37 +0100453 seq_printf(s, "SR = %8x\n", sr);
SAN People73a59c12006-01-09 17:05:41 +0000454
455 seq_printf(s, "\n");
456
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100457 list_for_each_entry(clk, &clocks, node) {
458 char *state;
SAN People73a59c12006-01-09 17:05:41 +0000459
460 if (clk->mode == pmc_sys_mode)
461 state = (scsr & clk->pmc_mask) ? "on" : "off";
462 else if (clk->mode == pmc_periph_mode)
463 state = (pcsr & clk->pmc_mask) ? "on" : "off";
Stelian Pop53d71682008-04-05 21:14:03 +0100464 else if (clk->mode == pmc_uckr_mode)
465 state = (uckr & clk->pmc_mask) ? "on" : "off";
SAN People73a59c12006-01-09 17:05:41 +0000466 else if (clk->pmc_mask)
467 state = (sr & clk->pmc_mask) ? "on" : "off";
468 else if (clk == &clk32k || clk == &main_clk)
469 state = "on";
470 else
471 state = "";
472
Andrew Victor69b648a2006-03-22 20:14:14 +0000473 seq_printf(s, "%-10s users=%2d %-3s %9ld Hz %s\n",
SAN People73a59c12006-01-09 17:05:41 +0000474 clk->name, clk->users, state, clk_get_rate(clk),
475 clk->parent ? clk->parent->name : "");
476 }
477 return 0;
478}
479
480static int at91_clk_open(struct inode *inode, struct file *file)
481{
482 return single_open(file, at91_clk_show, NULL);
483}
484
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800485static const struct file_operations at91_clk_operations = {
SAN People73a59c12006-01-09 17:05:41 +0000486 .open = at91_clk_open,
487 .read = seq_read,
488 .llseek = seq_lseek,
489 .release = single_release,
490};
491
492static int __init at91_clk_debugfs_init(void)
493{
494 /* /sys/kernel/debug/at91_clk */
495 (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
496
497 return 0;
498}
499postcore_initcall(at91_clk_debugfs_init);
500
501#endif
502
503/*------------------------------------------------------------------------*/
504
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100505/* Register a new clock */
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100506static void __init at91_clk_add(struct clk *clk)
507{
508 list_add_tail(&clk->node, &clocks);
509
510 clk->cl.con_id = clk->name;
511 clk->cl.clk = clk;
512 clkdev_add(&clk->cl);
513}
514
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100515int __init clk_register(struct clk *clk)
516{
517 if (clk_is_peripheral(clk)) {
Nicolas Ferre5afddee2010-09-09 19:58:23 +0200518 if (!clk->parent)
519 clk->parent = &mck;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100520 clk->mode = pmc_periph_mode;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100521 }
Andrew Victord481f862006-12-01 11:27:31 +0100522 else if (clk_is_sys(clk)) {
523 clk->parent = &mck;
524 clk->mode = pmc_sys_mode;
Andrew Victord481f862006-12-01 11:27:31 +0100525 }
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100526#ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
527 else if (clk_is_programmable(clk)) {
528 clk->mode = pmc_sys_mode;
529 init_programmable_clock(clk);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100530 }
531#endif
532
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100533 at91_clk_add(clk);
534
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100535 return 0;
536}
537
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100538/*------------------------------------------------------------------------*/
539
SAN People73a59c12006-01-09 17:05:41 +0000540static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
541{
542 unsigned mul, div;
543
544 div = reg & 0xff;
545 mul = (reg >> 16) & 0x7ff;
546 if (div && mul) {
547 freq /= div;
548 freq *= mul + 1;
549 } else
550 freq = 0;
Andrew Victor69b648a2006-03-22 20:14:14 +0000551
SAN People73a59c12006-01-09 17:05:41 +0000552 return freq;
553}
554
Andrew Victor69b648a2006-03-22 20:14:14 +0000555static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
556{
557 if (pll == &pllb && (reg & AT91_PMC_USB96M))
558 return freq / 2;
559 else
560 return freq;
561}
562
SAN People73a59c12006-01-09 17:05:41 +0000563static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
564{
565 unsigned i, div = 0, mul = 0, diff = 1 << 30;
566 unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
567
568 /* PLL output max 240 MHz (or 180 MHz per errata) */
569 if (out_freq > 240000000)
570 goto fail;
571
572 for (i = 1; i < 256; i++) {
573 int diff1;
574 unsigned input, mul1;
575
576 /*
577 * PLL input between 1MHz and 32MHz per spec, but lower
578 * frequences seem necessary in some cases so allow 100K.
sedji gaouaou61352662008-07-10 10:15:35 +0100579 * Warning: some newer products need 2MHz min.
SAN People73a59c12006-01-09 17:05:41 +0000580 */
581 input = main_freq / i;
sedji gaouaou61352662008-07-10 10:15:35 +0100582 if (cpu_is_at91sam9g20() && input < 2000000)
583 continue;
SAN People73a59c12006-01-09 17:05:41 +0000584 if (input < 100000)
585 continue;
586 if (input > 32000000)
587 continue;
588
589 mul1 = out_freq / input;
sedji gaouaou61352662008-07-10 10:15:35 +0100590 if (cpu_is_at91sam9g20() && mul > 63)
591 continue;
SAN People73a59c12006-01-09 17:05:41 +0000592 if (mul1 > 2048)
593 continue;
594 if (mul1 < 2)
595 goto fail;
596
597 diff1 = out_freq - input * mul1;
598 if (diff1 < 0)
599 diff1 = -diff1;
600 if (diff > diff1) {
601 diff = diff1;
602 div = i;
603 mul = mul1;
604 if (diff == 0)
605 break;
606 }
607 }
608 if (i == 256 && diff > (out_freq >> 5))
609 goto fail;
610 return ret | ((mul - 1) << 16) | div;
611fail:
612 return 0;
613}
614
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100615static struct clk *const standard_pmc_clocks[] __initdata = {
616 /* four primary clocks */
617 &clk32k,
618 &main_clk,
619 &plla,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100620
621 /* MCK */
622 &mck
623};
624
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100625/* PLLB generated USB full speed clock init */
626static void __init at91_pllb_usbfs_clock_init(unsigned long main_clock)
627{
628 /*
629 * USB clock init: choose 48 MHz PLLB value,
630 * disable 48MHz clock during usb peripheral suspend.
631 *
632 * REVISIT: assumes MCK doesn't derive from PLLB!
633 */
634 uhpck.parent = &pllb;
635
636 at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M;
637 pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
638 if (cpu_is_at91rm9200()) {
639 uhpck.pmc_mask = AT91RM9200_PMC_UHP;
640 udpck.pmc_mask = AT91RM9200_PMC_UDP;
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800641 at91_pmc_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP);
Nicolas Ferreeab41702009-06-26 15:37:00 +0100642 } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() ||
643 cpu_is_at91sam9263() || cpu_is_at91sam9g20() ||
Jean-Christophe PLAGNIOL-VILLARD7a2207a2011-05-17 20:51:14 +0800644 cpu_is_at91sam9g10()) {
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100645 uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
646 udpck.pmc_mask = AT91SAM926x_PMC_UDP;
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100647 }
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800648 at91_pmc_write(AT91_CKGR_PLLBR, 0);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100649
650 udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
651 uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
652}
653
654/* UPLL generated USB full speed clock init */
655static void __init at91_upll_usbfs_clock_init(unsigned long main_clock)
656{
657 /*
658 * USB clock init: choose 480 MHz from UPLL,
659 */
660 unsigned int usbr = AT91_PMC_USBS_UPLL;
661
662 /* Setup divider by 10 to reach 48 MHz */
663 usbr |= ((10 - 1) << 8) & AT91_PMC_OHCIUSBDIV;
664
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800665 at91_pmc_write(AT91_PMC_USB, usbr);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100666
667 /* Now set uhpck values */
668 uhpck.parent = &utmi_clk;
669 uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
Ryan Mallon82515442010-06-02 12:55:36 +1200670 uhpck.rate_hz = utmi_clk.rate_hz;
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800671 uhpck.rate_hz /= 1 + ((at91_pmc_read(AT91_PMC_USB) & AT91_PMC_OHCIUSBDIV) >> 8);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100672}
673
SAN People73a59c12006-01-09 17:05:41 +0000674int __init at91_clock_init(unsigned long main_clock)
675{
676 unsigned tmp, freq, mckr;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100677 int i;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100678 int pll_overclock = false;
SAN People73a59c12006-01-09 17:05:41 +0000679
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800680 at91_pmc_base = ioremap(AT91_PMC, 256);
681 if (!at91_pmc_base)
682 panic("Impossible to ioremap AT91_PMC 0x%x\n", AT91_PMC);
683
SAN People73a59c12006-01-09 17:05:41 +0000684 /*
685 * When the bootloader initialized the main oscillator correctly,
686 * there's no problem using the cycle counter. But if it didn't,
687 * or when using oscillator bypass mode, we must be told the speed
688 * of the main clock.
689 */
690 if (!main_clock) {
691 do {
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800692 tmp = at91_pmc_read(AT91_CKGR_MCFR);
Andrew Victor69b648a2006-03-22 20:14:14 +0000693 } while (!(tmp & AT91_PMC_MAINRDY));
694 main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
SAN People73a59c12006-01-09 17:05:41 +0000695 }
696 main_clk.rate_hz = main_clock;
697
698 /* report if PLLA is more than mildly overclocked */
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800699 plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_pmc_read(AT91_CKGR_PLLAR));
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100700 if (cpu_has_300M_plla()) {
701 if (plla.rate_hz > 300000000)
702 pll_overclock = true;
703 } else if (cpu_has_800M_plla()) {
704 if (plla.rate_hz > 800000000)
705 pll_overclock = true;
706 } else {
707 if (plla.rate_hz > 209000000)
708 pll_overclock = true;
709 }
710 if (pll_overclock)
SAN People73a59c12006-01-09 17:05:41 +0000711 pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
712
Nicolas Ferre11128722011-03-10 19:08:54 +0100713 if (cpu_has_plladiv2()) {
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800714 mckr = at91_pmc_read(AT91_PMC_MCKR);
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100715 plla.rate_hz /= (1 << ((mckr & AT91_PMC_PLLADIV2) >> 12)); /* plla divisor by 2 */
716 }
SAN People73a59c12006-01-09 17:05:41 +0000717
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100718 if (!cpu_has_pllb() && cpu_has_upll()) {
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100719 /* setup UTMI clock as the fourth primary clock
720 * (instead of pllb) */
721 utmi_clk.type |= CLK_TYPE_PRIMARY;
722 utmi_clk.id = 3;
723 }
724
Andrew Victor69b648a2006-03-22 20:14:14 +0000725
SAN People73a59c12006-01-09 17:05:41 +0000726 /*
Stelian Pop53d71682008-04-05 21:14:03 +0100727 * USB HS clock init
728 */
Andrew Victor5e38efa2009-12-15 21:57:27 +0100729 if (cpu_has_utmi()) {
Stelian Pop53d71682008-04-05 21:14:03 +0100730 /*
731 * multiplier is hard-wired to 40
732 * (obtain the USB High Speed 480 MHz when input is 12 MHz)
733 */
734 utmi_clk.rate_hz = 40 * utmi_clk.parent->rate_hz;
Nicolas Ferre11128722011-03-10 19:08:54 +0100735
736 /* UTMI bias and PLL are managed at the same time */
737 if (cpu_has_upll())
738 utmi_clk.pmc_mask |= AT91_PMC_BIASEN;
Andrew Victor5e38efa2009-12-15 21:57:27 +0100739 }
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100740
741 /*
742 * USB FS clock init
743 */
744 if (cpu_has_pllb())
745 at91_pllb_usbfs_clock_init(main_clock);
746 if (cpu_has_upll())
747 /* assumes that we choose UPLL for USB and not PLLA */
748 at91_upll_usbfs_clock_init(main_clock);
Stelian Pop53d71682008-04-05 21:14:03 +0100749
750 /*
SAN People73a59c12006-01-09 17:05:41 +0000751 * MCK and CPU derive from one of those primary clocks.
752 * For now, assume this parentage won't change.
753 */
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800754 mckr = at91_pmc_read(AT91_PMC_MCKR);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100755 mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
SAN People73a59c12006-01-09 17:05:41 +0000756 freq = mck.parent->rate_hz;
Nicolas Ferre11128722011-03-10 19:08:54 +0100757 freq /= pmc_prescaler_divider(mckr); /* prescale */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100758 if (cpu_is_at91rm9200()) {
Andrew Victora95c7292007-11-19 11:52:09 +0100759 mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100760 } else if (cpu_is_at91sam9g20()) {
sedji gaouaou61352662008-07-10 10:15:35 +0100761 mck.rate_hz = (mckr & AT91_PMC_MDIV) ?
762 freq / ((mckr & AT91_PMC_MDIV) >> 7) : freq; /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
763 if (mckr & AT91_PMC_PDIV)
764 freq /= 2; /* processor clock division */
Nicolas Ferre11128722011-03-10 19:08:54 +0100765 } else if (cpu_has_mdiv3()) {
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100766 mck.rate_hz = (mckr & AT91_PMC_MDIV) == AT91SAM9_PMC_MDIV_3 ?
767 freq / 3 : freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100768 } else {
Andrew Victor5e38efa2009-12-15 21:57:27 +0100769 mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100770 }
SAN People73a59c12006-01-09 17:05:41 +0000771
Nicolas Ferre11128722011-03-10 19:08:54 +0100772 if (cpu_has_alt_prescaler()) {
773 /* Programmable clocks can use MCK */
774 mck.type |= CLK_TYPE_PRIMARY;
775 mck.id = 4;
776 }
777
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100778 /* Register the PMC's standard clocks */
779 for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100780 at91_clk_add(standard_pmc_clocks[i]);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100781
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100782 if (cpu_has_pllb())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100783 at91_clk_add(&pllb);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100784
785 if (cpu_has_uhp())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100786 at91_clk_add(&uhpck);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100787
788 if (cpu_has_udpfs())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100789 at91_clk_add(&udpck);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100790
791 if (cpu_has_utmi())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100792 at91_clk_add(&utmi_clk);
Stelian Pop53d71682008-04-05 21:14:03 +0100793
Andrew Victor91f8ed82006-06-19 13:20:23 +0100794 /* MCK and CPU clock are "always on" */
795 clk_enable(&mck);
796
SAN People73a59c12006-01-09 17:05:41 +0000797 printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
798 freq / 1000000, (unsigned) mck.rate_hz / 1000000,
799 (unsigned) main_clock / 1000000,
800 ((unsigned) main_clock % 1000000) / 1000);
801
Andrew Victorc9b75d12007-02-08 17:36:34 +0100802 return 0;
803}
Andrew Victor91f8ed82006-06-19 13:20:23 +0100804
Andrew Victorc9b75d12007-02-08 17:36:34 +0100805/*
806 * Several unused clocks may be active. Turn them off.
807 */
808static int __init at91_clock_reset(void)
809{
810 unsigned long pcdr = 0;
811 unsigned long scdr = 0;
812 struct clk *clk;
813
814 list_for_each_entry(clk, &clocks, node) {
815 if (clk->users > 0)
816 continue;
817
818 if (clk->mode == pmc_periph_mode)
819 pcdr |= clk->pmc_mask;
820
821 if (clk->mode == pmc_sys_mode)
822 scdr |= clk->pmc_mask;
823
824 pr_debug("Clocks: disable unused %s\n", clk->name);
825 }
826
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800827 at91_pmc_write(AT91_PMC_PCDR, pcdr);
828 at91_pmc_write(AT91_PMC_SCDR, scdr);
SAN People73a59c12006-01-09 17:05:41 +0000829
830 return 0;
831}
Andrew Victorc9b75d12007-02-08 17:36:34 +0100832late_initcall(at91_clock_reset);
Jean-Christophe PLAGNIOL-VILLARD0d781712012-02-05 20:25:32 +0800833
834void at91sam9_idle(void)
835{
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800836 at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
Jean-Christophe PLAGNIOL-VILLARD0d781712012-02-05 20:25:32 +0800837 cpu_do_idle();
838}