blob: 085a79dfaf76994aa3260e572984a63b3221dceb [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>
Jean-Christophe PLAGNIOL-VILLARDeb5e76f2012-03-02 20:44:23 +080026#include <linux/of_address.h>
SAN People73a59c12006-01-09 17:05:41 +000027
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/hardware.h>
29#include <mach/at91_pmc.h>
30#include <mach/cpu.h>
SAN People73a59c12006-01-09 17:05:41 +000031
Jean-Christophe PLAGNIOL-VILLARD0d781712012-02-05 20:25:32 +080032#include <asm/proc-fns.h>
33
Andrew Victor2eeaaa22006-09-27 10:50:59 +010034#include "clock.h"
Andrew Victor5e38efa2009-12-15 21:57:27 +010035#include "generic.h"
SAN People73a59c12006-01-09 17:05:41 +000036
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +080037void __iomem *at91_pmc_base;
Joachim Eastwoodf19b7972012-04-07 19:30:22 +020038EXPORT_SYMBOL_GPL(at91_pmc_base);
Andrew Victor55c20c02006-06-20 19:31:39 +010039
SAN People73a59c12006-01-09 17:05:41 +000040/*
41 * There's a lot more which can be done with clocks, including cpufreq
42 * integration, slow clock mode support (for system suspend), letting
43 * PLLB be used at other rates (on boards that don't need USB), etc.
44 */
45
Andrew Victor2eeaaa22006-09-27 10:50:59 +010046#define clk_is_primary(x) ((x)->type & CLK_TYPE_PRIMARY)
47#define clk_is_programmable(x) ((x)->type & CLK_TYPE_PROGRAMMABLE)
48#define clk_is_peripheral(x) ((x)->type & CLK_TYPE_PERIPHERAL)
Andrew Victord481f862006-12-01 11:27:31 +010049#define clk_is_sys(x) ((x)->type & CLK_TYPE_SYSTEM)
SAN People73a59c12006-01-09 17:05:41 +000050
Andrew Victor2eeaaa22006-09-27 10:50:59 +010051
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010052/*
53 * Chips have some kind of clocks : group them by functionality
54 */
Jean-Christophe PLAGNIOL-VILLARD9918cea2012-01-26 14:07:09 +010055#define cpu_has_utmi() ( cpu_is_at91sam9rl() \
Nicolas Ferre11128722011-03-10 19:08:54 +010056 || cpu_is_at91sam9g45() \
Ludovic Desroches8f4b4792013-03-22 13:24:12 +000057 || cpu_is_at91sam9x5() \
58 || cpu_is_sama5d3())
59
60#define cpu_has_1056M_plla() (cpu_is_sama5d3())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010061
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010062#define cpu_has_800M_plla() ( cpu_is_at91sam9g20() \
Nicolas Ferre11128722011-03-10 19:08:54 +010063 || cpu_is_at91sam9g45() \
Hong Xu74db4fb2012-04-17 14:26:31 +080064 || cpu_is_at91sam9x5() \
65 || cpu_is_at91sam9n12())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010066
Nicolas Ferreeab41702009-06-26 15:37:00 +010067#define cpu_has_300M_plla() (cpu_is_at91sam9g10())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010068
Nicolas Ferre2ed1f582012-07-09 21:06:25 +020069#define cpu_has_240M_plla() (cpu_is_at91sam9261() \
70 || cpu_is_at91sam9263() \
71 || cpu_is_at91sam9rl())
72
73#define cpu_has_210M_plla() (cpu_is_at91sam9260())
74
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010075#define cpu_has_pllb() (!(cpu_is_at91sam9rl() \
Nicolas Ferre11128722011-03-10 19:08:54 +010076 || cpu_is_at91sam9g45() \
Hong Xu74db4fb2012-04-17 14:26:31 +080077 || cpu_is_at91sam9x5() \
Nicolas Ferreed4a2af2013-06-24 10:20:55 +020078 || cpu_is_at91sam9n12() \
79 || cpu_is_sama5d3()))
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010080
Nicolas Ferre11128722011-03-10 19:08:54 +010081#define cpu_has_upll() (cpu_is_at91sam9g45() \
Ludovic Desroches8f4b4792013-03-22 13:24:12 +000082 || cpu_is_at91sam9x5() \
83 || cpu_is_sama5d3())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +010084
85/* USB host HS & FS */
86#define cpu_has_uhp() (!cpu_is_at91sam9rl())
87
88/* USB device FS only */
Nicolas Ferre2ef9df72009-06-26 15:36:57 +010089#define cpu_has_udpfs() (!(cpu_is_at91sam9rl() \
Nicolas Ferre11128722011-03-10 19:08:54 +010090 || cpu_is_at91sam9g45() \
Ludovic Desroches8f4b4792013-03-22 13:24:12 +000091 || cpu_is_at91sam9x5() \
92 || cpu_is_sama5d3()))
Nicolas Ferre11128722011-03-10 19:08:54 +010093
94#define cpu_has_plladiv2() (cpu_is_at91sam9g45() \
Hong Xu74db4fb2012-04-17 14:26:31 +080095 || cpu_is_at91sam9x5() \
Ludovic Desroches8f4b4792013-03-22 13:24:12 +000096 || cpu_is_at91sam9n12() \
97 || cpu_is_sama5d3())
Nicolas Ferre11128722011-03-10 19:08:54 +010098
99#define cpu_has_mdiv3() (cpu_is_at91sam9g45() \
Hong Xu74db4fb2012-04-17 14:26:31 +0800100 || cpu_is_at91sam9x5() \
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000101 || cpu_is_at91sam9n12() \
102 || cpu_is_sama5d3())
Nicolas Ferre11128722011-03-10 19:08:54 +0100103
Hong Xu74db4fb2012-04-17 14:26:31 +0800104#define cpu_has_alt_prescaler() (cpu_is_at91sam9x5() \
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000105 || cpu_is_at91sam9n12() \
106 || cpu_is_sama5d3())
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100107
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100108static LIST_HEAD(clocks);
109static DEFINE_SPINLOCK(clk_lock);
110
111static u32 at91_pllb_usb_init;
SAN People73a59c12006-01-09 17:05:41 +0000112
113/*
114 * Four primary clock sources: two crystal oscillators (32K, main), and
115 * two PLLs. PLLA usually runs the master clock; and PLLB must run at
116 * 48 MHz (unless no USB function clocks are needed). The main clock and
117 * both PLLs are turned off to run in "slow clock mode" (system suspend).
118 */
119static struct clk clk32k = {
120 .name = "clk32k",
121 .rate_hz = AT91_SLOW_CLOCK,
122 .users = 1, /* always on */
123 .id = 0,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100124 .type = CLK_TYPE_PRIMARY,
SAN People73a59c12006-01-09 17:05:41 +0000125};
126static struct clk main_clk = {
127 .name = "main",
Andrew Victor91f8ed82006-06-19 13:20:23 +0100128 .pmc_mask = AT91_PMC_MOSCS, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000129 .id = 1,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100130 .type = CLK_TYPE_PRIMARY,
SAN People73a59c12006-01-09 17:05:41 +0000131};
132static struct clk plla = {
133 .name = "plla",
134 .parent = &main_clk,
Andrew Victor91f8ed82006-06-19 13:20:23 +0100135 .pmc_mask = AT91_PMC_LOCKA, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000136 .id = 2,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100137 .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
SAN People73a59c12006-01-09 17:05:41 +0000138};
139
140static void pllb_mode(struct clk *clk, int is_on)
141{
142 u32 value;
143
144 if (is_on) {
145 is_on = AT91_PMC_LOCKB;
146 value = at91_pllb_usb_init;
147 } else
148 value = 0;
149
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100150 // REVISIT: Add work-around for AT91RM9200 Errata #26 ?
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800151 at91_pmc_write(AT91_CKGR_PLLBR, value);
SAN People73a59c12006-01-09 17:05:41 +0000152
153 do {
154 cpu_relax();
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800155 } while ((at91_pmc_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on);
SAN People73a59c12006-01-09 17:05:41 +0000156}
157
158static struct clk pllb = {
159 .name = "pllb",
160 .parent = &main_clk,
Andrew Victor91f8ed82006-06-19 13:20:23 +0100161 .pmc_mask = AT91_PMC_LOCKB, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000162 .mode = pllb_mode,
163 .id = 3,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100164 .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
SAN People73a59c12006-01-09 17:05:41 +0000165};
166
167static void pmc_sys_mode(struct clk *clk, int is_on)
168{
169 if (is_on)
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800170 at91_pmc_write(AT91_PMC_SCER, clk->pmc_mask);
SAN People73a59c12006-01-09 17:05:41 +0000171 else
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800172 at91_pmc_write(AT91_PMC_SCDR, clk->pmc_mask);
SAN People73a59c12006-01-09 17:05:41 +0000173}
174
Stelian Pop53d71682008-04-05 21:14:03 +0100175static void pmc_uckr_mode(struct clk *clk, int is_on)
176{
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800177 unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
Stelian Pop53d71682008-04-05 21:14:03 +0100178
179 if (is_on) {
180 is_on = AT91_PMC_LOCKU;
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800181 at91_pmc_write(AT91_CKGR_UCKR, uckr | clk->pmc_mask);
Stelian Pop53d71682008-04-05 21:14:03 +0100182 } else
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800183 at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(clk->pmc_mask));
Stelian Pop53d71682008-04-05 21:14:03 +0100184
185 do {
186 cpu_relax();
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800187 } while ((at91_pmc_read(AT91_PMC_SR) & AT91_PMC_LOCKU) != is_on);
Stelian Pop53d71682008-04-05 21:14:03 +0100188}
189
SAN People73a59c12006-01-09 17:05:41 +0000190/* USB function clocks (PLLB must be 48 MHz) */
191static struct clk udpck = {
192 .name = "udpck",
193 .parent = &pllb,
SAN People73a59c12006-01-09 17:05:41 +0000194 .mode = pmc_sys_mode,
195};
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100196struct clk utmi_clk = {
Stelian Pop53d71682008-04-05 21:14:03 +0100197 .name = "utmi_clk",
198 .parent = &main_clk,
199 .pmc_mask = AT91_PMC_UPLLEN, /* in CKGR_UCKR */
200 .mode = pmc_uckr_mode,
201 .type = CLK_TYPE_PLL,
202};
SAN People73a59c12006-01-09 17:05:41 +0000203static struct clk uhpck = {
204 .name = "uhpck",
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100205 /*.parent = ... we choose parent at runtime */
SAN People73a59c12006-01-09 17:05:41 +0000206 .mode = pmc_sys_mode,
207};
208
SAN People73a59c12006-01-09 17:05:41 +0000209
210/*
211 * The master clock is divided from the CPU clock (by 1-4). It's used for
212 * memory, interfaces to on-chip peripherals, the AIC, and sometimes more
213 * (e.g baud rate generation). It's sourced from one of the primary clocks.
214 */
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100215struct clk mck = {
SAN People73a59c12006-01-09 17:05:41 +0000216 .name = "mck",
Andrew Victor91f8ed82006-06-19 13:20:23 +0100217 .pmc_mask = AT91_PMC_MCKRDY, /* in PMC_SR */
SAN People73a59c12006-01-09 17:05:41 +0000218};
219
220static void pmc_periph_mode(struct clk *clk, int is_on)
221{
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000222 u32 regval = 0;
223
224 /*
225 * With sama5d3 devices, we are managing clock division so we have to
226 * use the Peripheral Control Register introduced from at91sam9x5
227 * devices.
228 */
229 if (cpu_is_sama5d3()) {
230 regval |= AT91_PMC_PCR_CMD; /* write command */
231 regval |= clk->pid & AT91_PMC_PCR_PID; /* peripheral selection */
232 regval |= AT91_PMC_PCR_DIV(clk->div);
233 if (is_on)
234 regval |= AT91_PMC_PCR_EN; /* enable clock */
235 at91_pmc_write(AT91_PMC_PCR, regval);
236 } else {
237 if (is_on)
238 at91_pmc_write(AT91_PMC_PCER, clk->pmc_mask);
239 else
240 at91_pmc_write(AT91_PMC_PCDR, clk->pmc_mask);
241 }
SAN People73a59c12006-01-09 17:05:41 +0000242}
243
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100244static struct clk __init *at91_css_to_clk(unsigned long css)
245{
246 switch (css) {
247 case AT91_PMC_CSS_SLOW:
248 return &clk32k;
249 case AT91_PMC_CSS_MAIN:
250 return &main_clk;
251 case AT91_PMC_CSS_PLLA:
252 return &plla;
253 case AT91_PMC_CSS_PLLB:
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100254 if (cpu_has_upll())
255 /* CSS_PLLB == CSS_UPLL */
256 return &utmi_clk;
257 else if (cpu_has_pllb())
258 return &pllb;
Nicolas Ferre11128722011-03-10 19:08:54 +0100259 break;
260 /* alternate PMC: can use master clock */
261 case AT91_PMC_CSS_MASTER:
262 return &mck;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100263 }
SAN People73a59c12006-01-09 17:05:41 +0000264
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100265 return NULL;
266}
SAN People73a59c12006-01-09 17:05:41 +0000267
Nicolas Ferre11128722011-03-10 19:08:54 +0100268static int pmc_prescaler_divider(u32 reg)
269{
270 if (cpu_has_alt_prescaler()) {
271 return 1 << ((reg & AT91_PMC_ALT_PRES) >> PMC_ALT_PRES_OFFSET);
272 } else {
273 return 1 << ((reg & AT91_PMC_PRES) >> PMC_PRES_OFFSET);
274 }
275}
276
SAN People73a59c12006-01-09 17:05:41 +0000277static void __clk_enable(struct clk *clk)
278{
279 if (clk->parent)
280 __clk_enable(clk->parent);
281 if (clk->users++ == 0 && clk->mode)
282 clk->mode(clk, 1);
283}
284
285int clk_enable(struct clk *clk)
286{
287 unsigned long flags;
288
289 spin_lock_irqsave(&clk_lock, flags);
290 __clk_enable(clk);
291 spin_unlock_irqrestore(&clk_lock, flags);
292 return 0;
293}
294EXPORT_SYMBOL(clk_enable);
295
296static void __clk_disable(struct clk *clk)
297{
298 BUG_ON(clk->users == 0);
299 if (--clk->users == 0 && clk->mode)
300 clk->mode(clk, 0);
301 if (clk->parent)
302 __clk_disable(clk->parent);
303}
304
305void clk_disable(struct clk *clk)
306{
307 unsigned long flags;
308
309 spin_lock_irqsave(&clk_lock, flags);
310 __clk_disable(clk);
311 spin_unlock_irqrestore(&clk_lock, flags);
312}
313EXPORT_SYMBOL(clk_disable);
314
315unsigned long clk_get_rate(struct clk *clk)
316{
317 unsigned long flags;
318 unsigned long rate;
319
320 spin_lock_irqsave(&clk_lock, flags);
321 for (;;) {
322 rate = clk->rate_hz;
323 if (rate || !clk->parent)
324 break;
325 clk = clk->parent;
326 }
327 spin_unlock_irqrestore(&clk_lock, flags);
328 return rate;
329}
330EXPORT_SYMBOL(clk_get_rate);
331
332/*------------------------------------------------------------------------*/
333
334#ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
335
336/*
337 * For now, only the programmable clocks support reparenting (MCK could
338 * do this too, with care) or rate changing (the PLLs could do this too,
339 * ditto MCK but that's more for cpufreq). Drivers may reparent to get
340 * a better rate match; we don't.
341 */
342
343long clk_round_rate(struct clk *clk, unsigned long rate)
344{
345 unsigned long flags;
346 unsigned prescale;
347 unsigned long actual;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100348 unsigned long prev = ULONG_MAX;
SAN People73a59c12006-01-09 17:05:41 +0000349
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100350 if (!clk_is_programmable(clk))
SAN People73a59c12006-01-09 17:05:41 +0000351 return -EINVAL;
352 spin_lock_irqsave(&clk_lock, flags);
353
354 actual = clk->parent->rate_hz;
355 for (prescale = 0; prescale < 7; prescale++) {
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100356 if (actual > rate)
357 prev = actual;
358
359 if (actual && actual <= rate) {
360 if ((prev - rate) < (rate - actual)) {
361 actual = prev;
362 prescale--;
363 }
SAN People73a59c12006-01-09 17:05:41 +0000364 break;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100365 }
SAN People73a59c12006-01-09 17:05:41 +0000366 actual >>= 1;
367 }
368
369 spin_unlock_irqrestore(&clk_lock, flags);
370 return (prescale < 7) ? actual : -ENOENT;
371}
372EXPORT_SYMBOL(clk_round_rate);
373
374int clk_set_rate(struct clk *clk, unsigned long rate)
375{
376 unsigned long flags;
377 unsigned prescale;
Nicolas Ferre11128722011-03-10 19:08:54 +0100378 unsigned long prescale_offset, css_mask;
SAN People73a59c12006-01-09 17:05:41 +0000379 unsigned long actual;
380
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100381 if (!clk_is_programmable(clk))
SAN People73a59c12006-01-09 17:05:41 +0000382 return -EINVAL;
383 if (clk->users)
384 return -EBUSY;
Nicolas Ferre11128722011-03-10 19:08:54 +0100385
386 if (cpu_has_alt_prescaler()) {
387 prescale_offset = PMC_ALT_PRES_OFFSET;
388 css_mask = AT91_PMC_ALT_PCKR_CSS;
389 } else {
390 prescale_offset = PMC_PRES_OFFSET;
391 css_mask = AT91_PMC_CSS;
392 }
393
SAN People73a59c12006-01-09 17:05:41 +0000394 spin_lock_irqsave(&clk_lock, flags);
395
396 actual = clk->parent->rate_hz;
397 for (prescale = 0; prescale < 7; prescale++) {
398 if (actual && actual <= rate) {
399 u32 pckr;
400
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800401 pckr = at91_pmc_read(AT91_PMC_PCKR(clk->id));
Nicolas Ferre11128722011-03-10 19:08:54 +0100402 pckr &= css_mask; /* keep clock selection */
403 pckr |= prescale << prescale_offset;
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800404 at91_pmc_write(AT91_PMC_PCKR(clk->id), pckr);
SAN People73a59c12006-01-09 17:05:41 +0000405 clk->rate_hz = actual;
406 break;
407 }
408 actual >>= 1;
409 }
410
411 spin_unlock_irqrestore(&clk_lock, flags);
412 return (prescale < 7) ? actual : -ENOENT;
413}
414EXPORT_SYMBOL(clk_set_rate);
415
416struct clk *clk_get_parent(struct clk *clk)
417{
418 return clk->parent;
419}
420EXPORT_SYMBOL(clk_get_parent);
421
422int clk_set_parent(struct clk *clk, struct clk *parent)
423{
424 unsigned long flags;
425
426 if (clk->users)
427 return -EBUSY;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100428 if (!clk_is_primary(parent) || !clk_is_programmable(clk))
SAN People73a59c12006-01-09 17:05:41 +0000429 return -EINVAL;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100430
431 if (cpu_is_at91sam9rl() && parent->id == AT91_PMC_CSS_PLLB)
432 return -EINVAL;
433
SAN People73a59c12006-01-09 17:05:41 +0000434 spin_lock_irqsave(&clk_lock, flags);
435
436 clk->rate_hz = parent->rate_hz;
437 clk->parent = parent;
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800438 at91_pmc_write(AT91_PMC_PCKR(clk->id), parent->id);
SAN People73a59c12006-01-09 17:05:41 +0000439
440 spin_unlock_irqrestore(&clk_lock, flags);
441 return 0;
442}
443EXPORT_SYMBOL(clk_set_parent);
444
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100445/* establish PCK0..PCKN parentage and rate */
David Brownell72e7ae82008-02-06 22:03:42 +0100446static void __init init_programmable_clock(struct clk *clk)
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100447{
448 struct clk *parent;
449 u32 pckr;
Nicolas Ferre11128722011-03-10 19:08:54 +0100450 unsigned int css_mask;
451
452 if (cpu_has_alt_prescaler())
453 css_mask = AT91_PMC_ALT_PCKR_CSS;
454 else
455 css_mask = AT91_PMC_CSS;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100456
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800457 pckr = at91_pmc_read(AT91_PMC_PCKR(clk->id));
Nicolas Ferre11128722011-03-10 19:08:54 +0100458 parent = at91_css_to_clk(pckr & css_mask);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100459 clk->parent = parent;
Nicolas Ferre11128722011-03-10 19:08:54 +0100460 clk->rate_hz = parent->rate_hz / pmc_prescaler_divider(pckr);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100461}
462
SAN People73a59c12006-01-09 17:05:41 +0000463#endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
464
465/*------------------------------------------------------------------------*/
466
467#ifdef CONFIG_DEBUG_FS
468
469static int at91_clk_show(struct seq_file *s, void *unused)
470{
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000471 u32 scsr, pcsr, pcsr1 = 0, uckr = 0, sr;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100472 struct clk *clk;
SAN People73a59c12006-01-09 17:05:41 +0000473
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800474 scsr = at91_pmc_read(AT91_PMC_SCSR);
475 pcsr = at91_pmc_read(AT91_PMC_PCSR);
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000476 if (cpu_is_sama5d3())
477 pcsr1 = at91_pmc_read(AT91_PMC_PCSR1);
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800478 sr = at91_pmc_read(AT91_PMC_SR);
Nicolas Ferre940192e2012-02-23 09:44:37 +0100479 seq_printf(s, "SCSR = %8x\n", scsr);
480 seq_printf(s, "PCSR = %8x\n", pcsr);
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000481 if (cpu_is_sama5d3())
482 seq_printf(s, "PCSR1 = %8x\n", pcsr1);
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800483 seq_printf(s, "MOR = %8x\n", at91_pmc_read(AT91_CKGR_MOR));
484 seq_printf(s, "MCFR = %8x\n", at91_pmc_read(AT91_CKGR_MCFR));
485 seq_printf(s, "PLLA = %8x\n", at91_pmc_read(AT91_CKGR_PLLAR));
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100486 if (cpu_has_pllb())
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800487 seq_printf(s, "PLLB = %8x\n", at91_pmc_read(AT91_CKGR_PLLBR));
Nicolas Ferre940192e2012-02-23 09:44:37 +0100488 if (cpu_has_utmi()) {
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800489 uckr = at91_pmc_read(AT91_CKGR_UCKR);
Nicolas Ferre940192e2012-02-23 09:44:37 +0100490 seq_printf(s, "UCKR = %8x\n", uckr);
491 }
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800492 seq_printf(s, "MCKR = %8x\n", at91_pmc_read(AT91_PMC_MCKR));
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100493 if (cpu_has_upll())
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800494 seq_printf(s, "USB = %8x\n", at91_pmc_read(AT91_PMC_USB));
Nicolas Ferre940192e2012-02-23 09:44:37 +0100495 seq_printf(s, "SR = %8x\n", sr);
SAN People73a59c12006-01-09 17:05:41 +0000496
497 seq_printf(s, "\n");
498
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100499 list_for_each_entry(clk, &clocks, node) {
500 char *state;
SAN People73a59c12006-01-09 17:05:41 +0000501
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000502 if (clk->mode == pmc_sys_mode) {
SAN People73a59c12006-01-09 17:05:41 +0000503 state = (scsr & clk->pmc_mask) ? "on" : "off";
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000504 } else if (clk->mode == pmc_periph_mode) {
505 if (cpu_is_sama5d3()) {
506 u32 pmc_mask = 1 << (clk->pid % 32);
SAN People73a59c12006-01-09 17:05:41 +0000507
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000508 if (clk->pid > 31)
509 state = (pcsr1 & pmc_mask) ? "on" : "off";
510 else
511 state = (pcsr & pmc_mask) ? "on" : "off";
512 } else {
513 state = (pcsr & clk->pmc_mask) ? "on" : "off";
514 }
515 } else if (clk->mode == pmc_uckr_mode) {
516 state = (uckr & clk->pmc_mask) ? "on" : "off";
517 } else if (clk->pmc_mask) {
518 state = (sr & clk->pmc_mask) ? "on" : "off";
519 } else if (clk == &clk32k || clk == &main_clk) {
520 state = "on";
521 } else {
522 state = "";
523 }
524
525 seq_printf(s, "%-10s users=%2d %-3s %9lu Hz %s\n",
SAN People73a59c12006-01-09 17:05:41 +0000526 clk->name, clk->users, state, clk_get_rate(clk),
527 clk->parent ? clk->parent->name : "");
528 }
529 return 0;
530}
531
532static int at91_clk_open(struct inode *inode, struct file *file)
533{
534 return single_open(file, at91_clk_show, NULL);
535}
536
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800537static const struct file_operations at91_clk_operations = {
SAN People73a59c12006-01-09 17:05:41 +0000538 .open = at91_clk_open,
539 .read = seq_read,
540 .llseek = seq_lseek,
541 .release = single_release,
542};
543
544static int __init at91_clk_debugfs_init(void)
545{
546 /* /sys/kernel/debug/at91_clk */
547 (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
548
549 return 0;
550}
551postcore_initcall(at91_clk_debugfs_init);
552
553#endif
554
555/*------------------------------------------------------------------------*/
556
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100557/* Register a new clock */
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100558static void __init at91_clk_add(struct clk *clk)
559{
560 list_add_tail(&clk->node, &clocks);
561
562 clk->cl.con_id = clk->name;
563 clk->cl.clk = clk;
564 clkdev_add(&clk->cl);
565}
566
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100567int __init clk_register(struct clk *clk)
568{
569 if (clk_is_peripheral(clk)) {
Nicolas Ferre5afddee2010-09-09 19:58:23 +0200570 if (!clk->parent)
571 clk->parent = &mck;
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000572 if (cpu_is_sama5d3())
573 clk->rate_hz = DIV_ROUND_UP(clk->parent->rate_hz,
574 1 << clk->div);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100575 clk->mode = pmc_periph_mode;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100576 }
Andrew Victord481f862006-12-01 11:27:31 +0100577 else if (clk_is_sys(clk)) {
578 clk->parent = &mck;
579 clk->mode = pmc_sys_mode;
Andrew Victord481f862006-12-01 11:27:31 +0100580 }
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100581#ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
582 else if (clk_is_programmable(clk)) {
583 clk->mode = pmc_sys_mode;
584 init_programmable_clock(clk);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100585 }
586#endif
587
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100588 at91_clk_add(clk);
589
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100590 return 0;
591}
592
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100593/*------------------------------------------------------------------------*/
594
SAN People73a59c12006-01-09 17:05:41 +0000595static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
596{
597 unsigned mul, div;
598
599 div = reg & 0xff;
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000600 if (cpu_is_sama5d3())
601 mul = AT91_PMC3_MUL_GET(reg);
602 else
603 mul = AT91_PMC_MUL_GET(reg);
604
SAN People73a59c12006-01-09 17:05:41 +0000605 if (div && mul) {
606 freq /= div;
607 freq *= mul + 1;
608 } else
609 freq = 0;
Andrew Victor69b648a2006-03-22 20:14:14 +0000610
SAN People73a59c12006-01-09 17:05:41 +0000611 return freq;
612}
613
Andrew Victor69b648a2006-03-22 20:14:14 +0000614static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
615{
616 if (pll == &pllb && (reg & AT91_PMC_USB96M))
617 return freq / 2;
618 else
619 return freq;
620}
621
SAN People73a59c12006-01-09 17:05:41 +0000622static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
623{
624 unsigned i, div = 0, mul = 0, diff = 1 << 30;
625 unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
626
627 /* PLL output max 240 MHz (or 180 MHz per errata) */
628 if (out_freq > 240000000)
629 goto fail;
630
631 for (i = 1; i < 256; i++) {
632 int diff1;
633 unsigned input, mul1;
634
635 /*
636 * PLL input between 1MHz and 32MHz per spec, but lower
637 * frequences seem necessary in some cases so allow 100K.
sedji gaouaou61352662008-07-10 10:15:35 +0100638 * Warning: some newer products need 2MHz min.
SAN People73a59c12006-01-09 17:05:41 +0000639 */
640 input = main_freq / i;
sedji gaouaou61352662008-07-10 10:15:35 +0100641 if (cpu_is_at91sam9g20() && input < 2000000)
642 continue;
SAN People73a59c12006-01-09 17:05:41 +0000643 if (input < 100000)
644 continue;
645 if (input > 32000000)
646 continue;
647
648 mul1 = out_freq / input;
sedji gaouaou61352662008-07-10 10:15:35 +0100649 if (cpu_is_at91sam9g20() && mul > 63)
650 continue;
SAN People73a59c12006-01-09 17:05:41 +0000651 if (mul1 > 2048)
652 continue;
653 if (mul1 < 2)
654 goto fail;
655
656 diff1 = out_freq - input * mul1;
657 if (diff1 < 0)
658 diff1 = -diff1;
659 if (diff > diff1) {
660 diff = diff1;
661 div = i;
662 mul = mul1;
663 if (diff == 0)
664 break;
665 }
666 }
667 if (i == 256 && diff > (out_freq >> 5))
668 goto fail;
669 return ret | ((mul - 1) << 16) | div;
670fail:
671 return 0;
672}
673
Andi Kleenbcad6dc2012-10-04 17:11:28 -0700674static struct clk *const standard_pmc_clocks[] __initconst = {
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100675 /* four primary clocks */
676 &clk32k,
677 &main_clk,
678 &plla,
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100679
680 /* MCK */
681 &mck
682};
683
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100684/* PLLB generated USB full speed clock init */
685static void __init at91_pllb_usbfs_clock_init(unsigned long main_clock)
686{
687 /*
688 * USB clock init: choose 48 MHz PLLB value,
689 * disable 48MHz clock during usb peripheral suspend.
690 *
691 * REVISIT: assumes MCK doesn't derive from PLLB!
692 */
693 uhpck.parent = &pllb;
694
695 at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M;
696 pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
697 if (cpu_is_at91rm9200()) {
698 uhpck.pmc_mask = AT91RM9200_PMC_UHP;
699 udpck.pmc_mask = AT91RM9200_PMC_UDP;
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800700 at91_pmc_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP);
Nicolas Ferreeab41702009-06-26 15:37:00 +0100701 } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() ||
702 cpu_is_at91sam9263() || cpu_is_at91sam9g20() ||
Jean-Christophe PLAGNIOL-VILLARD7a2207a2011-05-17 20:51:14 +0800703 cpu_is_at91sam9g10()) {
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100704 uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
705 udpck.pmc_mask = AT91SAM926x_PMC_UDP;
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100706 }
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800707 at91_pmc_write(AT91_CKGR_PLLBR, 0);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100708
709 udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
710 uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
711}
712
713/* UPLL generated USB full speed clock init */
714static void __init at91_upll_usbfs_clock_init(unsigned long main_clock)
715{
716 /*
717 * USB clock init: choose 480 MHz from UPLL,
718 */
719 unsigned int usbr = AT91_PMC_USBS_UPLL;
720
721 /* Setup divider by 10 to reach 48 MHz */
722 usbr |= ((10 - 1) << 8) & AT91_PMC_OHCIUSBDIV;
723
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800724 at91_pmc_write(AT91_PMC_USB, usbr);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100725
726 /* Now set uhpck values */
727 uhpck.parent = &utmi_clk;
728 uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
Ryan Mallon82515442010-06-02 12:55:36 +1200729 uhpck.rate_hz = utmi_clk.rate_hz;
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800730 uhpck.rate_hz /= 1 + ((at91_pmc_read(AT91_PMC_USB) & AT91_PMC_OHCIUSBDIV) >> 8);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100731}
732
Jean-Christophe PLAGNIOL-VILLARDeb5e76f2012-03-02 20:44:23 +0800733static int __init at91_pmc_init(unsigned long main_clock)
SAN People73a59c12006-01-09 17:05:41 +0000734{
735 unsigned tmp, freq, mckr;
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100736 int i;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100737 int pll_overclock = false;
SAN People73a59c12006-01-09 17:05:41 +0000738
739 /*
740 * When the bootloader initialized the main oscillator correctly,
741 * there's no problem using the cycle counter. But if it didn't,
742 * or when using oscillator bypass mode, we must be told the speed
743 * of the main clock.
744 */
745 if (!main_clock) {
746 do {
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800747 tmp = at91_pmc_read(AT91_CKGR_MCFR);
Andrew Victor69b648a2006-03-22 20:14:14 +0000748 } while (!(tmp & AT91_PMC_MAINRDY));
749 main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
SAN People73a59c12006-01-09 17:05:41 +0000750 }
751 main_clk.rate_hz = main_clock;
752
753 /* report if PLLA is more than mildly overclocked */
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800754 plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_pmc_read(AT91_CKGR_PLLAR));
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000755 if (cpu_has_1056M_plla()) {
756 if (plla.rate_hz > 1056000000)
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100757 pll_overclock = true;
758 } else if (cpu_has_800M_plla()) {
759 if (plla.rate_hz > 800000000)
760 pll_overclock = true;
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000761 } else if (cpu_has_300M_plla()) {
762 if (plla.rate_hz > 300000000)
763 pll_overclock = true;
Nicolas Ferre2ed1f582012-07-09 21:06:25 +0200764 } else if (cpu_has_240M_plla()) {
765 if (plla.rate_hz > 240000000)
766 pll_overclock = true;
767 } else if (cpu_has_210M_plla()) {
768 if (plla.rate_hz > 210000000)
769 pll_overclock = true;
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100770 } else {
771 if (plla.rate_hz > 209000000)
772 pll_overclock = true;
773 }
774 if (pll_overclock)
SAN People73a59c12006-01-09 17:05:41 +0000775 pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
776
Nicolas Ferre11128722011-03-10 19:08:54 +0100777 if (cpu_has_plladiv2()) {
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800778 mckr = at91_pmc_read(AT91_PMC_MCKR);
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100779 plla.rate_hz /= (1 << ((mckr & AT91_PMC_PLLADIV2) >> 12)); /* plla divisor by 2 */
780 }
SAN People73a59c12006-01-09 17:05:41 +0000781
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100782 if (!cpu_has_pllb() && cpu_has_upll()) {
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100783 /* setup UTMI clock as the fourth primary clock
784 * (instead of pllb) */
785 utmi_clk.type |= CLK_TYPE_PRIMARY;
786 utmi_clk.id = 3;
787 }
788
Andrew Victor69b648a2006-03-22 20:14:14 +0000789
SAN People73a59c12006-01-09 17:05:41 +0000790 /*
Stelian Pop53d71682008-04-05 21:14:03 +0100791 * USB HS clock init
792 */
Andrew Victor5e38efa2009-12-15 21:57:27 +0100793 if (cpu_has_utmi()) {
Stelian Pop53d71682008-04-05 21:14:03 +0100794 /*
795 * multiplier is hard-wired to 40
796 * (obtain the USB High Speed 480 MHz when input is 12 MHz)
797 */
798 utmi_clk.rate_hz = 40 * utmi_clk.parent->rate_hz;
Nicolas Ferre11128722011-03-10 19:08:54 +0100799
800 /* UTMI bias and PLL are managed at the same time */
801 if (cpu_has_upll())
802 utmi_clk.pmc_mask |= AT91_PMC_BIASEN;
Andrew Victor5e38efa2009-12-15 21:57:27 +0100803 }
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100804
805 /*
806 * USB FS clock init
807 */
808 if (cpu_has_pllb())
809 at91_pllb_usbfs_clock_init(main_clock);
810 if (cpu_has_upll())
811 /* assumes that we choose UPLL for USB and not PLLA */
812 at91_upll_usbfs_clock_init(main_clock);
Stelian Pop53d71682008-04-05 21:14:03 +0100813
814 /*
SAN People73a59c12006-01-09 17:05:41 +0000815 * MCK and CPU derive from one of those primary clocks.
816 * For now, assume this parentage won't change.
817 */
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800818 mckr = at91_pmc_read(AT91_PMC_MCKR);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100819 mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
SAN People73a59c12006-01-09 17:05:41 +0000820 freq = mck.parent->rate_hz;
Nicolas Ferre11128722011-03-10 19:08:54 +0100821 freq /= pmc_prescaler_divider(mckr); /* prescale */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100822 if (cpu_is_at91rm9200()) {
Andrew Victora95c7292007-11-19 11:52:09 +0100823 mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100824 } else if (cpu_is_at91sam9g20()) {
sedji gaouaou61352662008-07-10 10:15:35 +0100825 mck.rate_hz = (mckr & AT91_PMC_MDIV) ?
826 freq / ((mckr & AT91_PMC_MDIV) >> 7) : freq; /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
827 if (mckr & AT91_PMC_PDIV)
828 freq /= 2; /* processor clock division */
Nicolas Ferre11128722011-03-10 19:08:54 +0100829 } else if (cpu_has_mdiv3()) {
Nicolas Ferre2ef9df72009-06-26 15:36:57 +0100830 mck.rate_hz = (mckr & AT91_PMC_MDIV) == AT91SAM9_PMC_MDIV_3 ?
831 freq / 3 : freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100832 } else {
Andrew Victor5e38efa2009-12-15 21:57:27 +0100833 mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100834 }
SAN People73a59c12006-01-09 17:05:41 +0000835
Nicolas Ferre11128722011-03-10 19:08:54 +0100836 if (cpu_has_alt_prescaler()) {
837 /* Programmable clocks can use MCK */
838 mck.type |= CLK_TYPE_PRIMARY;
839 mck.id = 4;
840 }
841
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100842 /* Register the PMC's standard clocks */
843 for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100844 at91_clk_add(standard_pmc_clocks[i]);
Andrew Victor2eeaaa22006-09-27 10:50:59 +0100845
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100846 if (cpu_has_pllb())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100847 at91_clk_add(&pllb);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100848
849 if (cpu_has_uhp())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100850 at91_clk_add(&uhpck);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100851
852 if (cpu_has_udpfs())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100853 at91_clk_add(&udpck);
Nicolas Ferre6d0485a2009-03-31 17:13:15 +0100854
855 if (cpu_has_utmi())
Jean-Christophe PLAGNIOL-VILLARDbd602992011-02-02 07:27:07 +0100856 at91_clk_add(&utmi_clk);
Stelian Pop53d71682008-04-05 21:14:03 +0100857
Andrew Victor91f8ed82006-06-19 13:20:23 +0100858 /* MCK and CPU clock are "always on" */
859 clk_enable(&mck);
860
SAN People73a59c12006-01-09 17:05:41 +0000861 printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
862 freq / 1000000, (unsigned) mck.rate_hz / 1000000,
863 (unsigned) main_clock / 1000000,
864 ((unsigned) main_clock % 1000000) / 1000);
865
Andrew Victorc9b75d12007-02-08 17:36:34 +0100866 return 0;
867}
Andrew Victor91f8ed82006-06-19 13:20:23 +0100868
Jean-Christophe PLAGNIOL-VILLARDeb5e76f2012-03-02 20:44:23 +0800869#if defined(CONFIG_OF)
870static struct of_device_id pmc_ids[] = {
871 { .compatible = "atmel,at91rm9200-pmc" },
872 { /*sentinel*/ }
873};
874
875static struct of_device_id osc_ids[] = {
876 { .compatible = "atmel,osc" },
877 { /*sentinel*/ }
878};
879
880int __init at91_dt_clock_init(void)
881{
882 struct device_node *np;
883 u32 main_clock = 0;
884
885 np = of_find_matching_node(NULL, pmc_ids);
886 if (!np)
887 panic("unable to find compatible pmc node in dtb\n");
888
889 at91_pmc_base = of_iomap(np, 0);
890 if (!at91_pmc_base)
891 panic("unable to map pmc cpu registers\n");
892
893 of_node_put(np);
894
895 /* retrieve the freqency of fixed clocks from device tree */
896 np = of_find_matching_node(NULL, osc_ids);
897 if (np) {
898 u32 rate;
899 if (!of_property_read_u32(np, "clock-frequency", &rate))
900 main_clock = rate;
901 }
902
903 of_node_put(np);
904
905 return at91_pmc_init(main_clock);
906}
907#endif
908
909int __init at91_clock_init(unsigned long main_clock)
910{
911 at91_pmc_base = ioremap(AT91_PMC, 256);
912 if (!at91_pmc_base)
913 panic("Impossible to ioremap AT91_PMC 0x%x\n", AT91_PMC);
914
915 return at91_pmc_init(main_clock);
916}
917
Andrew Victorc9b75d12007-02-08 17:36:34 +0100918/*
919 * Several unused clocks may be active. Turn them off.
920 */
921static int __init at91_clock_reset(void)
922{
923 unsigned long pcdr = 0;
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000924 unsigned long pcdr1 = 0;
Andrew Victorc9b75d12007-02-08 17:36:34 +0100925 unsigned long scdr = 0;
926 struct clk *clk;
927
928 list_for_each_entry(clk, &clocks, node) {
929 if (clk->users > 0)
930 continue;
931
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000932 if (clk->mode == pmc_periph_mode) {
933 if (cpu_is_sama5d3()) {
934 u32 pmc_mask = 1 << (clk->pid % 32);
935
936 if (clk->pid > 31)
937 pcdr1 |= pmc_mask;
938 else
939 pcdr |= pmc_mask;
940 } else
941 pcdr |= clk->pmc_mask;
942 }
Andrew Victorc9b75d12007-02-08 17:36:34 +0100943
944 if (clk->mode == pmc_sys_mode)
945 scdr |= clk->pmc_mask;
946
947 pr_debug("Clocks: disable unused %s\n", clk->name);
948 }
949
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800950 at91_pmc_write(AT91_PMC_SCDR, scdr);
Ludovic Desroches8f4b4792013-03-22 13:24:12 +0000951 if (cpu_is_sama5d3())
952 at91_pmc_write(AT91_PMC_PCDR1, pcdr1);
SAN People73a59c12006-01-09 17:05:41 +0000953
954 return 0;
955}
Andrew Victorc9b75d12007-02-08 17:36:34 +0100956late_initcall(at91_clock_reset);
Jean-Christophe PLAGNIOL-VILLARD0d781712012-02-05 20:25:32 +0800957
958void at91sam9_idle(void)
959{
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +0800960 at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
Jean-Christophe PLAGNIOL-VILLARD0d781712012-02-05 20:25:32 +0800961 cpu_do_idle();
962}