Blame view

arch/arm/mach-at91/clock.c 18.6 KB
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
1
  /*
9d0412680   Andrew Victor   [ARM] 4124/1: Ren...
2
   * linux/arch/arm/mach-at91/clock.c
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   *
   * Copyright (C) 2005 David Brownell
   * Copyright (C) 2005 Ivan Kokshaysky
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   */
  
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/init.h>
  #include <linux/fs.h>
  #include <linux/debugfs.h>
  #include <linux/seq_file.h>
  #include <linux/list.h>
  #include <linux/errno.h>
  #include <linux/err.h>
  #include <linux/spinlock.h>
  #include <linux/delay.h>
  #include <linux/clk.h>
fced80c73   Russell King   [ARM] Convert asm...
25
  #include <linux/io.h>
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
26

a09e64fbc   Russell King   [ARM] Move includ...
27
28
29
  #include <mach/hardware.h>
  #include <mach/at91_pmc.h>
  #include <mach/cpu.h>
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
30

2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
31
  #include "clock.h"
5e38efae9   Andrew Victor   ARM: 5850/1: [AT9...
32
  #include "generic.h"
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
33

55c20c0af   Andrew Victor   [ARM] 3599/1: AT9...
34

73a59c1c4   SAN People   [ARM] 3240/2: AT9...
35
36
37
38
39
  /*
   * There's a lot more which can be done with clocks, including cpufreq
   * integration, slow clock mode support (for system suspend), letting
   * PLLB be used at other rates (on boards that don't need USB), etc.
   */
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
40
41
42
  #define clk_is_primary(x)	((x)->type & CLK_TYPE_PRIMARY)
  #define clk_is_programmable(x)	((x)->type & CLK_TYPE_PROGRAMMABLE)
  #define clk_is_peripheral(x)	((x)->type & CLK_TYPE_PERIPHERAL)
d481f8644   Andrew Victor   [ARM] 3959/1: AT9...
43
  #define clk_is_sys(x)		((x)->type & CLK_TYPE_SYSTEM)
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
44

6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
45
46
47
48
  /*
   * Chips have some kind of clocks : group them by functionality
   */
  #define cpu_has_utmi()		(  cpu_is_at91cap9() \
2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
49
50
  				|| cpu_is_at91sam9rl() \
  				|| cpu_is_at91sam9g45())
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
51

2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
52
53
  #define cpu_has_800M_plla()	(  cpu_is_at91sam9g20() \
  				|| cpu_is_at91sam9g45())
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
54

eab417086   Nicolas Ferre   [ARM] 5567/1: at9...
55
  #define cpu_has_300M_plla()	(cpu_is_at91sam9g10())
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
56

2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
57
58
59
60
  #define cpu_has_pllb()		(!(cpu_is_at91sam9rl() \
  				|| cpu_is_at91sam9g45()))
  
  #define cpu_has_upll()		(cpu_is_at91sam9g45())
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
61
62
63
64
65
  
  /* USB host HS & FS */
  #define cpu_has_uhp()		(!cpu_is_at91sam9rl())
  
  /* USB device FS only */
2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
66
67
  #define cpu_has_udpfs()		(!(cpu_is_at91sam9rl() \
  				|| cpu_is_at91sam9g45()))
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
68

2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
69
70
  static LIST_HEAD(clocks);
  static DEFINE_SPINLOCK(clk_lock);
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
71

2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
72
  static u32 at91_pllb_usb_init;
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
73
74
75
76
77
78
79
80
81
82
83
84
  
  /*
   * Four primary clock sources:  two crystal oscillators (32K, main), and
   * two PLLs.  PLLA usually runs the master clock; and PLLB must run at
   * 48 MHz (unless no USB function clocks are needed).  The main clock and
   * both PLLs are turned off to run in "slow clock mode" (system suspend).
   */
  static struct clk clk32k = {
  	.name		= "clk32k",
  	.rate_hz	= AT91_SLOW_CLOCK,
  	.users		= 1,		/* always on */
  	.id		= 0,
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
85
  	.type		= CLK_TYPE_PRIMARY,
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
86
87
88
  };
  static struct clk main_clk = {
  	.name		= "main",
91f8ed835   Andrew Victor   [ARM] 3578/1: AT9...
89
  	.pmc_mask	= AT91_PMC_MOSCS,	/* in PMC_SR */
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
90
  	.id		= 1,
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
91
  	.type		= CLK_TYPE_PRIMARY,
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
92
93
94
95
  };
  static struct clk plla = {
  	.name		= "plla",
  	.parent		= &main_clk,
91f8ed835   Andrew Victor   [ARM] 3578/1: AT9...
96
  	.pmc_mask	= AT91_PMC_LOCKA,	/* in PMC_SR */
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
97
  	.id		= 2,
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
98
  	.type		= CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
99
100
101
102
103
104
105
106
107
108
109
  };
  
  static void pllb_mode(struct clk *clk, int is_on)
  {
  	u32	value;
  
  	if (is_on) {
  		is_on = AT91_PMC_LOCKB;
  		value = at91_pllb_usb_init;
  	} else
  		value = 0;
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
110
  	// REVISIT: Add work-around for AT91RM9200 Errata #26 ?
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
111
112
113
114
115
116
117
118
119
120
  	at91_sys_write(AT91_CKGR_PLLBR, value);
  
  	do {
  		cpu_relax();
  	} while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on);
  }
  
  static struct clk pllb = {
  	.name		= "pllb",
  	.parent		= &main_clk,
91f8ed835   Andrew Victor   [ARM] 3578/1: AT9...
121
  	.pmc_mask	= AT91_PMC_LOCKB,	/* in PMC_SR */
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
122
123
  	.mode		= pllb_mode,
  	.id		= 3,
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
124
  	.type		= CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
125
126
127
128
129
130
131
132
133
  };
  
  static void pmc_sys_mode(struct clk *clk, int is_on)
  {
  	if (is_on)
  		at91_sys_write(AT91_PMC_SCER, clk->pmc_mask);
  	else
  		at91_sys_write(AT91_PMC_SCDR, clk->pmc_mask);
  }
53d716802   Stelian Pop   [ARM] 4933/1: AT9...
134
135
136
  static void pmc_uckr_mode(struct clk *clk, int is_on)
  {
  	unsigned int uckr = at91_sys_read(AT91_CKGR_UCKR);
2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
137
138
139
140
141
142
  	if (cpu_is_at91sam9g45()) {
  		if (is_on)
  			uckr |= AT91_PMC_BIASEN;
  		else
  			uckr &= ~AT91_PMC_BIASEN;
  	}
53d716802   Stelian Pop   [ARM] 4933/1: AT9...
143
144
145
146
147
148
149
150
151
152
  	if (is_on) {
  		is_on = AT91_PMC_LOCKU;
  		at91_sys_write(AT91_CKGR_UCKR, uckr | clk->pmc_mask);
  	} else
  		at91_sys_write(AT91_CKGR_UCKR, uckr & ~(clk->pmc_mask));
  
  	do {
  		cpu_relax();
  	} while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKU) != is_on);
  }
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
153
154
155
156
  /* USB function clocks (PLLB must be 48 MHz) */
  static struct clk udpck = {
  	.name		= "udpck",
  	.parent		= &pllb,
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
157
158
  	.mode		= pmc_sys_mode,
  };
bd6029959   Jean-Christophe PLAGNIOL-VILLARD   at91: switch to C...
159
  struct clk utmi_clk = {
53d716802   Stelian Pop   [ARM] 4933/1: AT9...
160
161
162
163
164
165
  	.name		= "utmi_clk",
  	.parent		= &main_clk,
  	.pmc_mask	= AT91_PMC_UPLLEN,	/* in CKGR_UCKR */
  	.mode		= pmc_uckr_mode,
  	.type		= CLK_TYPE_PLL,
  };
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
166
167
  static struct clk uhpck = {
  	.name		= "uhpck",
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
168
  	/*.parent		= ... we choose parent at runtime */
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
169
170
  	.mode		= pmc_sys_mode,
  };
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
171
172
173
174
175
176
  
  /*
   * The master clock is divided from the CPU clock (by 1-4).  It's used for
   * memory, interfaces to on-chip peripherals, the AIC, and sometimes more
   * (e.g baud rate generation).  It's sourced from one of the primary clocks.
   */
bd6029959   Jean-Christophe PLAGNIOL-VILLARD   at91: switch to C...
177
  struct clk mck = {
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
178
  	.name		= "mck",
91f8ed835   Andrew Victor   [ARM] 3578/1: AT9...
179
  	.pmc_mask	= AT91_PMC_MCKRDY,	/* in PMC_SR */
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
180
181
182
183
184
185
186
187
188
  };
  
  static void pmc_periph_mode(struct clk *clk, int is_on)
  {
  	if (is_on)
  		at91_sys_write(AT91_PMC_PCER, clk->pmc_mask);
  	else
  		at91_sys_write(AT91_PMC_PCDR, clk->pmc_mask);
  }
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
189
190
191
192
193
194
195
196
197
198
  static struct clk __init *at91_css_to_clk(unsigned long css)
  {
  	switch (css) {
  		case AT91_PMC_CSS_SLOW:
  			return &clk32k;
  		case AT91_PMC_CSS_MAIN:
  			return &main_clk;
  		case AT91_PMC_CSS_PLLA:
  			return &plla;
  		case AT91_PMC_CSS_PLLB:
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
199
200
201
202
203
  			if (cpu_has_upll())
  				/* CSS_PLLB == CSS_UPLL */
  				return &utmi_clk;
  			else if (cpu_has_pllb())
  				return &pllb;
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
204
  	}
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
205

2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
206
207
  	return NULL;
  }
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
208

73a59c1c4   SAN People   [ARM] 3240/2: AT9...
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  static void __clk_enable(struct clk *clk)
  {
  	if (clk->parent)
  		__clk_enable(clk->parent);
  	if (clk->users++ == 0 && clk->mode)
  		clk->mode(clk, 1);
  }
  
  int clk_enable(struct clk *clk)
  {
  	unsigned long	flags;
  
  	spin_lock_irqsave(&clk_lock, flags);
  	__clk_enable(clk);
  	spin_unlock_irqrestore(&clk_lock, flags);
  	return 0;
  }
  EXPORT_SYMBOL(clk_enable);
  
  static void __clk_disable(struct clk *clk)
  {
  	BUG_ON(clk->users == 0);
  	if (--clk->users == 0 && clk->mode)
  		clk->mode(clk, 0);
  	if (clk->parent)
  		__clk_disable(clk->parent);
  }
  
  void clk_disable(struct clk *clk)
  {
  	unsigned long	flags;
  
  	spin_lock_irqsave(&clk_lock, flags);
  	__clk_disable(clk);
  	spin_unlock_irqrestore(&clk_lock, flags);
  }
  EXPORT_SYMBOL(clk_disable);
  
  unsigned long clk_get_rate(struct clk *clk)
  {
  	unsigned long	flags;
  	unsigned long	rate;
  
  	spin_lock_irqsave(&clk_lock, flags);
  	for (;;) {
  		rate = clk->rate_hz;
  		if (rate || !clk->parent)
  			break;
  		clk = clk->parent;
  	}
  	spin_unlock_irqrestore(&clk_lock, flags);
  	return rate;
  }
  EXPORT_SYMBOL(clk_get_rate);
  
  /*------------------------------------------------------------------------*/
  
  #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  
  /*
   * For now, only the programmable clocks support reparenting (MCK could
   * do this too, with care) or rate changing (the PLLs could do this too,
   * ditto MCK but that's more for cpufreq).  Drivers may reparent to get
   * a better rate match; we don't.
   */
  
  long clk_round_rate(struct clk *clk, unsigned long rate)
  {
  	unsigned long	flags;
  	unsigned	prescale;
  	unsigned long	actual;
2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
280
  	unsigned long	prev = ULONG_MAX;
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
281

2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
282
  	if (!clk_is_programmable(clk))
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
283
284
285
286
287
  		return -EINVAL;
  	spin_lock_irqsave(&clk_lock, flags);
  
  	actual = clk->parent->rate_hz;
  	for (prescale = 0; prescale < 7; prescale++) {
2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
288
289
290
291
292
293
294
295
  		if (actual > rate)
  			prev = actual;
  
  		if (actual && actual <= rate) {
  			if ((prev - rate) < (rate - actual)) {
  				actual = prev;
  				prescale--;
  			}
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
296
  			break;
2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
297
  		}
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
298
299
300
301
302
303
304
305
306
307
308
309
310
  		actual >>= 1;
  	}
  
  	spin_unlock_irqrestore(&clk_lock, flags);
  	return (prescale < 7) ? actual : -ENOENT;
  }
  EXPORT_SYMBOL(clk_round_rate);
  
  int clk_set_rate(struct clk *clk, unsigned long rate)
  {
  	unsigned long	flags;
  	unsigned	prescale;
  	unsigned long	actual;
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
311
  	if (!clk_is_programmable(clk))
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
312
313
314
315
316
317
318
319
320
321
322
  		return -EINVAL;
  	if (clk->users)
  		return -EBUSY;
  	spin_lock_irqsave(&clk_lock, flags);
  
  	actual = clk->parent->rate_hz;
  	for (prescale = 0; prescale < 7; prescale++) {
  		if (actual && actual <= rate) {
  			u32	pckr;
  
  			pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
323
  			pckr &= AT91_PMC_CSS;	/* clock selection */
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  			pckr |= prescale << 2;
  			at91_sys_write(AT91_PMC_PCKR(clk->id), pckr);
  			clk->rate_hz = actual;
  			break;
  		}
  		actual >>= 1;
  	}
  
  	spin_unlock_irqrestore(&clk_lock, flags);
  	return (prescale < 7) ? actual : -ENOENT;
  }
  EXPORT_SYMBOL(clk_set_rate);
  
  struct clk *clk_get_parent(struct clk *clk)
  {
  	return clk->parent;
  }
  EXPORT_SYMBOL(clk_get_parent);
  
  int clk_set_parent(struct clk *clk, struct clk *parent)
  {
  	unsigned long	flags;
  
  	if (clk->users)
  		return -EBUSY;
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
349
  	if (!clk_is_primary(parent) || !clk_is_programmable(clk))
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
350
  		return -EINVAL;
2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
351
352
353
  
  	if (cpu_is_at91sam9rl() && parent->id == AT91_PMC_CSS_PLLB)
  		return -EINVAL;
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
354
355
356
357
358
359
360
361
362
363
  	spin_lock_irqsave(&clk_lock, flags);
  
  	clk->rate_hz = parent->rate_hz;
  	clk->parent = parent;
  	at91_sys_write(AT91_PMC_PCKR(clk->id), parent->id);
  
  	spin_unlock_irqrestore(&clk_lock, flags);
  	return 0;
  }
  EXPORT_SYMBOL(clk_set_parent);
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
364
  /* establish PCK0..PCKN parentage and rate */
72e7ae814   David Brownell   [ARM] 4823/1: AT9...
365
  static void __init init_programmable_clock(struct clk *clk)
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
366
367
368
369
370
371
372
  {
  	struct clk	*parent;
  	u32		pckr;
  
  	pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
  	parent = at91_css_to_clk(pckr & AT91_PMC_CSS);
  	clk->parent = parent;
a95c729b7   Andrew Victor   [ARM] 4604/2: AT9...
373
  	clk->rate_hz = parent->rate_hz / (1 << ((pckr & AT91_PMC_PRES) >> 2));
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
374
  }
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
375
376
377
378
379
380
381
382
  #endif	/* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  
  /*------------------------------------------------------------------------*/
  
  #ifdef CONFIG_DEBUG_FS
  
  static int at91_clk_show(struct seq_file *s, void *unused)
  {
53d716802   Stelian Pop   [ARM] 4933/1: AT9...
383
  	u32		scsr, pcsr, uckr = 0, sr;
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
384
  	struct clk	*clk;
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
385
386
387
388
389
  
  	seq_printf(s, "SCSR = %8x
  ", scsr = at91_sys_read(AT91_PMC_SCSR));
  	seq_printf(s, "PCSR = %8x
  ", pcsr = at91_sys_read(AT91_PMC_PCSR));
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
390
391
392
393
394
395
  	seq_printf(s, "MOR  = %8x
  ", at91_sys_read(AT91_CKGR_MOR));
  	seq_printf(s, "MCFR = %8x
  ", at91_sys_read(AT91_CKGR_MCFR));
  	seq_printf(s, "PLLA = %8x
  ", at91_sys_read(AT91_CKGR_PLLAR));
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
396
  	if (cpu_has_pllb())
ba45ca435   Nicolas Ferre   [ARM] 4940/1: AT9...
397
398
  		seq_printf(s, "PLLB = %8x
  ", at91_sys_read(AT91_CKGR_PLLBR));
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
399
  	if (cpu_has_utmi())
53d716802   Stelian Pop   [ARM] 4933/1: AT9...
400
401
  		seq_printf(s, "UCKR = %8x
  ", uckr = at91_sys_read(AT91_CKGR_UCKR));
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
402
403
  	seq_printf(s, "MCKR = %8x
  ", at91_sys_read(AT91_PMC_MCKR));
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
404
405
406
  	if (cpu_has_upll())
  		seq_printf(s, "USB  = %8x
  ", at91_sys_read(AT91_PMC_USB));
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
407
408
409
410
411
  	seq_printf(s, "SR   = %8x
  ", sr = at91_sys_read(AT91_PMC_SR));
  
  	seq_printf(s, "
  ");
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
412
413
  	list_for_each_entry(clk, &clocks, node) {
  		char	*state;
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
414
415
416
417
418
  
  		if (clk->mode == pmc_sys_mode)
  			state = (scsr & clk->pmc_mask) ? "on" : "off";
  		else if (clk->mode == pmc_periph_mode)
  			state = (pcsr & clk->pmc_mask) ? "on" : "off";
53d716802   Stelian Pop   [ARM] 4933/1: AT9...
419
420
  		else if (clk->mode == pmc_uckr_mode)
  			state = (uckr & clk->pmc_mask) ? "on" : "off";
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
421
422
423
424
425
426
  		else if (clk->pmc_mask)
  			state = (sr & clk->pmc_mask) ? "on" : "off";
  		else if (clk == &clk32k || clk == &main_clk)
  			state = "on";
  		else
  			state = "";
69b648a20   Andrew Victor   [ARM] 3386/1: AT9...
427
428
  		seq_printf(s, "%-10s users=%2d %-3s %9ld Hz %s
  ",
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
429
430
431
432
433
434
435
436
437
438
  			clk->name, clk->users, state, clk_get_rate(clk),
  			clk->parent ? clk->parent->name : "");
  	}
  	return 0;
  }
  
  static int at91_clk_open(struct inode *inode, struct file *file)
  {
  	return single_open(file, at91_clk_show, NULL);
  }
5dfe4c964   Arjan van de Ven   [PATCH] mark stru...
439
  static const struct file_operations at91_clk_operations = {
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
  	.open		= at91_clk_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= single_release,
  };
  
  static int __init at91_clk_debugfs_init(void)
  {
  	/* /sys/kernel/debug/at91_clk */
  	(void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
  
  	return 0;
  }
  postcore_initcall(at91_clk_debugfs_init);
  
  #endif
  
  /*------------------------------------------------------------------------*/
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
458
  /* Register a new clock */
bd6029959   Jean-Christophe PLAGNIOL-VILLARD   at91: switch to C...
459
460
461
462
463
464
465
466
  static void __init at91_clk_add(struct clk *clk)
  {
  	list_add_tail(&clk->node, &clocks);
  
  	clk->cl.con_id = clk->name;
  	clk->cl.clk = clk;
  	clkdev_add(&clk->cl);
  }
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
467
468
469
  int __init clk_register(struct clk *clk)
  {
  	if (clk_is_peripheral(clk)) {
5afddee41   Nicolas Ferre   AT91: clock: peri...
470
471
  		if (!clk->parent)
  			clk->parent = &mck;
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
472
  		clk->mode = pmc_periph_mode;
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
473
  	}
d481f8644   Andrew Victor   [ARM] 3959/1: AT9...
474
475
476
  	else if (clk_is_sys(clk)) {
  		clk->parent = &mck;
  		clk->mode = pmc_sys_mode;
d481f8644   Andrew Victor   [ARM] 3959/1: AT9...
477
  	}
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
478
479
480
481
  #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  	else if (clk_is_programmable(clk)) {
  		clk->mode = pmc_sys_mode;
  		init_programmable_clock(clk);
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
482
483
  	}
  #endif
bd6029959   Jean-Christophe PLAGNIOL-VILLARD   at91: switch to C...
484
  	at91_clk_add(clk);
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
485
486
  	return 0;
  }
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
487
  /*------------------------------------------------------------------------*/
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
488
489
490
491
492
493
494
495
496
497
498
  static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
  {
  	unsigned mul, div;
  
  	div = reg & 0xff;
  	mul = (reg >> 16) & 0x7ff;
  	if (div && mul) {
  		freq /= div;
  		freq *= mul + 1;
  	} else
  		freq = 0;
69b648a20   Andrew Victor   [ARM] 3386/1: AT9...
499

73a59c1c4   SAN People   [ARM] 3240/2: AT9...
500
501
  	return freq;
  }
69b648a20   Andrew Victor   [ARM] 3386/1: AT9...
502
503
504
505
506
507
508
  static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
  {
  	if (pll == &pllb && (reg & AT91_PMC_USB96M))
  		return freq / 2;
  	else
  		return freq;
  }
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
  static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
  {
  	unsigned i, div = 0, mul = 0, diff = 1 << 30;
  	unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  
  	/* PLL output max 240 MHz (or 180 MHz per errata) */
  	if (out_freq > 240000000)
  		goto fail;
  
  	for (i = 1; i < 256; i++) {
  		int diff1;
  		unsigned input, mul1;
  
  		/*
  		 * PLL input between 1MHz and 32MHz per spec, but lower
  		 * frequences seem necessary in some cases so allow 100K.
613526677   sedji gaouaou   [ARM] 5130/4: Sup...
525
  		 * Warning: some newer products need 2MHz min.
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
526
527
  		 */
  		input = main_freq / i;
613526677   sedji gaouaou   [ARM] 5130/4: Sup...
528
529
  		if (cpu_is_at91sam9g20() && input < 2000000)
  			continue;
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
530
531
532
533
534
535
  		if (input < 100000)
  			continue;
  		if (input > 32000000)
  			continue;
  
  		mul1 = out_freq / input;
613526677   sedji gaouaou   [ARM] 5130/4: Sup...
536
537
  		if (cpu_is_at91sam9g20() && mul > 63)
  			continue;
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
  		if (mul1 > 2048)
  			continue;
  		if (mul1 < 2)
  			goto fail;
  
  		diff1 = out_freq - input * mul1;
  		if (diff1 < 0)
  			diff1 = -diff1;
  		if (diff > diff1) {
  			diff = diff1;
  			div = i;
  			mul = mul1;
  			if (diff == 0)
  				break;
  		}
  	}
  	if (i == 256 && diff > (out_freq >> 5))
  		goto fail;
  	return ret | ((mul - 1) << 16) | div;
  fail:
  	return 0;
  }
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
560
561
562
563
564
  static struct clk *const standard_pmc_clocks[] __initdata = {
  	/* four primary clocks */
  	&clk32k,
  	&main_clk,
  	&plla,
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
565
566
567
568
  
  	/* MCK */
  	&mck
  };
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
  /* PLLB generated USB full speed clock init */
  static void __init at91_pllb_usbfs_clock_init(unsigned long main_clock)
  {
  	/*
  	 * USB clock init:  choose 48 MHz PLLB value,
  	 * disable 48MHz clock during usb peripheral suspend.
  	 *
  	 * REVISIT:  assumes MCK doesn't derive from PLLB!
  	 */
  	uhpck.parent = &pllb;
  
  	at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M;
  	pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
  	if (cpu_is_at91rm9200()) {
  		uhpck.pmc_mask = AT91RM9200_PMC_UHP;
  		udpck.pmc_mask = AT91RM9200_PMC_UDP;
  		at91_sys_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP);
eab417086   Nicolas Ferre   [ARM] 5567/1: at9...
586
587
  	} else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() ||
  		   cpu_is_at91sam9263() || cpu_is_at91sam9g20() ||
7a2207a0e   Jean-Christophe PLAGNIOL-VILLARD   at91: drop at572d...
588
  		   cpu_is_at91sam9g10()) {
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
  		uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  		udpck.pmc_mask = AT91SAM926x_PMC_UDP;
  	} else if (cpu_is_at91cap9()) {
  		uhpck.pmc_mask = AT91CAP9_PMC_UHP;
  	}
  	at91_sys_write(AT91_CKGR_PLLBR, 0);
  
  	udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  	uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  }
  
  /* UPLL generated USB full speed clock init */
  static void __init at91_upll_usbfs_clock_init(unsigned long main_clock)
  {
  	/*
  	 * USB clock init: choose 480 MHz from UPLL,
  	 */
  	unsigned int usbr = AT91_PMC_USBS_UPLL;
  
  	/* Setup divider by 10 to reach 48 MHz */
  	usbr |= ((10 - 1) << 8) & AT91_PMC_OHCIUSBDIV;
  
  	at91_sys_write(AT91_PMC_USB, usbr);
  
  	/* Now set uhpck values */
  	uhpck.parent = &utmi_clk;
  	uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
8251544f9   Ryan Mallon   at91: Fix uhpck c...
616
  	uhpck.rate_hz = utmi_clk.rate_hz;
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
617
618
  	uhpck.rate_hz /= 1 + ((at91_sys_read(AT91_PMC_USB) & AT91_PMC_OHCIUSBDIV) >> 8);
  }
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
619
620
621
  int __init at91_clock_init(unsigned long main_clock)
  {
  	unsigned tmp, freq, mckr;
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
622
  	int i;
2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
623
  	int pll_overclock = false;
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
624
625
626
627
628
629
630
631
632
633
  
  	/*
  	 * When the bootloader initialized the main oscillator correctly,
  	 * there's no problem using the cycle counter.  But if it didn't,
  	 * or when using oscillator bypass mode, we must be told the speed
  	 * of the main clock.
  	 */
  	if (!main_clock) {
  		do {
  			tmp = at91_sys_read(AT91_CKGR_MCFR);
69b648a20   Andrew Victor   [ARM] 3386/1: AT9...
634
635
  		} while (!(tmp & AT91_PMC_MAINRDY));
  		main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
636
637
638
639
640
  	}
  	main_clk.rate_hz = main_clock;
  
  	/* report if PLLA is more than mildly overclocked */
  	plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR));
2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
641
642
643
644
645
646
647
648
649
650
651
  	if (cpu_has_300M_plla()) {
  		if (plla.rate_hz > 300000000)
  			pll_overclock = true;
  	} else if (cpu_has_800M_plla()) {
  		if (plla.rate_hz > 800000000)
  			pll_overclock = true;
  	} else {
  		if (plla.rate_hz > 209000000)
  			pll_overclock = true;
  	}
  	if (pll_overclock)
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
652
653
  		pr_info("Clocks: PLLA overclocked, %ld MHz
  ", plla.rate_hz / 1000000);
2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
654
655
656
657
  	if (cpu_is_at91sam9g45()) {
  		mckr = at91_sys_read(AT91_PMC_MCKR);
  		plla.rate_hz /= (1 << ((mckr & AT91_PMC_PLLADIV2) >> 12));	/* plla divisor by 2 */
  	}
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
658

2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
659
  	if (!cpu_has_pllb() && cpu_has_upll()) {
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
660
661
662
663
  		/* setup UTMI clock as the fourth primary clock
  		 * (instead of pllb) */
  		utmi_clk.type |= CLK_TYPE_PRIMARY;
  		utmi_clk.id = 3;
d481f8644   Andrew Victor   [ARM] 3959/1: AT9...
664
  	}
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
665

69b648a20   Andrew Victor   [ARM] 3386/1: AT9...
666

73a59c1c4   SAN People   [ARM] 3240/2: AT9...
667
  	/*
53d716802   Stelian Pop   [ARM] 4933/1: AT9...
668
669
  	 * USB HS clock init
  	 */
5e38efae9   Andrew Victor   ARM: 5850/1: [AT9...
670
  	if (cpu_has_utmi()) {
53d716802   Stelian Pop   [ARM] 4933/1: AT9...
671
672
673
674
675
  		/*
  		 * multiplier is hard-wired to 40
  		 * (obtain the USB High Speed 480 MHz when input is 12 MHz)
  		 */
  		utmi_clk.rate_hz = 40 * utmi_clk.parent->rate_hz;
5e38efae9   Andrew Victor   ARM: 5850/1: [AT9...
676
  	}
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
677
678
679
680
681
682
683
684
685
  
  	/*
  	 * USB FS clock init
  	 */
  	if (cpu_has_pllb())
  		at91_pllb_usbfs_clock_init(main_clock);
  	if (cpu_has_upll())
  		/* assumes that we choose UPLL for USB and not PLLA */
  		at91_upll_usbfs_clock_init(main_clock);
53d716802   Stelian Pop   [ARM] 4933/1: AT9...
686
687
  
  	/*
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
688
689
690
691
  	 * MCK and CPU derive from one of those primary clocks.
  	 * For now, assume this parentage won't change.
  	 */
  	mckr = at91_sys_read(AT91_PMC_MCKR);
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
692
  	mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
693
  	freq = mck.parent->rate_hz;
a95c729b7   Andrew Victor   [ARM] 4604/2: AT9...
694
  	freq /= (1 << ((mckr & AT91_PMC_PRES) >> 2));				/* prescale */
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
695
  	if (cpu_is_at91rm9200()) {
a95c729b7   Andrew Victor   [ARM] 4604/2: AT9...
696
  		mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8));	/* mdiv */
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
697
  	} else if (cpu_is_at91sam9g20()) {
613526677   sedji gaouaou   [ARM] 5130/4: Sup...
698
699
700
701
  		mck.rate_hz = (mckr & AT91_PMC_MDIV) ?
  			freq / ((mckr & AT91_PMC_MDIV) >> 7) : freq;	/* mdiv ; (x >> 7) = ((x >> 8) * 2) */
  		if (mckr & AT91_PMC_PDIV)
  			freq /= 2;		/* processor clock division */
2ef9df7ad   Nicolas Ferre   [ARM] 5569/1: at9...
702
703
704
  	} else if (cpu_is_at91sam9g45()) {
  		mck.rate_hz = (mckr & AT91_PMC_MDIV) == AT91SAM9_PMC_MDIV_3 ?
  			freq / 3 : freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8));	/* mdiv */
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
705
  	} else {
5e38efae9   Andrew Victor   ARM: 5850/1: [AT9...
706
  		mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8));		/* mdiv */
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
707
  	}
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
708

2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
709
710
  	/* Register the PMC's standard clocks */
  	for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
bd6029959   Jean-Christophe PLAGNIOL-VILLARD   at91: switch to C...
711
  		at91_clk_add(standard_pmc_clocks[i]);
2eeaaa21d   Andrew Victor   [ARM] 3866/1: AT9...
712

6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
713
  	if (cpu_has_pllb())
bd6029959   Jean-Christophe PLAGNIOL-VILLARD   at91: switch to C...
714
  		at91_clk_add(&pllb);
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
715
716
  
  	if (cpu_has_uhp())
bd6029959   Jean-Christophe PLAGNIOL-VILLARD   at91: switch to C...
717
  		at91_clk_add(&uhpck);
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
718
719
  
  	if (cpu_has_udpfs())
bd6029959   Jean-Christophe PLAGNIOL-VILLARD   at91: switch to C...
720
  		at91_clk_add(&udpck);
6d0485a99   Nicolas Ferre   [ARM] 5438/1: AT9...
721
722
  
  	if (cpu_has_utmi())
bd6029959   Jean-Christophe PLAGNIOL-VILLARD   at91: switch to C...
723
  		at91_clk_add(&utmi_clk);
53d716802   Stelian Pop   [ARM] 4933/1: AT9...
724

91f8ed835   Andrew Victor   [ARM] 3578/1: AT9...
725
726
  	/* MCK and CPU clock are "always on" */
  	clk_enable(&mck);
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
727
728
729
730
731
  	printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz
  ",
  		freq / 1000000, (unsigned) mck.rate_hz / 1000000,
  		(unsigned) main_clock / 1000000,
  		((unsigned) main_clock % 1000000) / 1000);
c9b75d132   Andrew Victor   [ARM] 4154/1: AT9...
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
  	return 0;
  }
  
  /*
   * Several unused clocks may be active.  Turn them off.
   */
  static int __init at91_clock_reset(void)
  {
  	unsigned long pcdr = 0;
  	unsigned long scdr = 0;
  	struct clk *clk;
  
  	list_for_each_entry(clk, &clocks, node) {
  		if (clk->users > 0)
  			continue;
  
  		if (clk->mode == pmc_periph_mode)
  			pcdr |= clk->pmc_mask;
  
  		if (clk->mode == pmc_sys_mode)
  			scdr |= clk->pmc_mask;
  
  		pr_debug("Clocks: disable unused %s
  ", clk->name);
  	}
91f8ed835   Andrew Victor   [ARM] 3578/1: AT9...
757

c9b75d132   Andrew Victor   [ARM] 4154/1: AT9...
758
759
  	at91_sys_write(AT91_PMC_PCDR, pcdr);
  	at91_sys_write(AT91_PMC_SCDR, scdr);
73a59c1c4   SAN People   [ARM] 3240/2: AT9...
760
761
762
  
  	return 0;
  }
c9b75d132   Andrew Victor   [ARM] 4154/1: AT9...
763
  late_initcall(at91_clock_reset);