blob: 5168c4dca1cac2c6f624ebeebc93dbf72b60bd68 [file] [log] [blame]
Manuel Lauss47440222014-07-23 16:36:48 +02001/*
2 * Alchemy clocks.
3 *
4 * Exposes all configurable internal clock sources to the clk framework.
5 *
6 * We have:
7 * - Root source, usually 12MHz supplied by an external crystal
8 * - 3 PLLs which generate multiples of root rate [AUX, CPU, AUX2]
9 *
10 * Dividers:
11 * - 6 clock dividers with:
12 * * selectable source [one of the PLLs],
13 * * output divided between [2 .. 512 in steps of 2] (!Au1300)
14 * or [1 .. 256 in steps of 1] (Au1300),
15 * * can be enabled individually.
16 *
17 * - up to 6 "internal" (fixed) consumers which:
18 * * take either AUXPLL or one of the above 6 dividers as input,
19 * * divide this input by 1, 2, or 4 (and 3 on Au1300).
20 * * can be disabled separately.
21 *
22 * Misc clocks:
23 * - sysbus clock: CPU core clock (CPUPLL) divided by 2, 3 or 4.
24 * depends on board design and should be set by bootloader, read-only.
25 * - peripheral clock: half the rate of sysbus clock, source for a lot
26 * of peripheral blocks, read-only.
27 * - memory clock: clk rate to main memory chips, depends on board
28 * design and is read-only,
29 * - lrclk: the static bus clock signal for synchronous operation.
30 * depends on board design, must be set by bootloader,
31 * but may be required to correctly configure devices attached to
32 * the static bus. The Au1000/1500/1100 manuals call it LCLK, on
33 * later models it's called RCLK.
34 */
35
36#include <linux/init.h>
37#include <linux/io.h>
Stephen Boyd4280cf52015-06-19 15:00:46 -070038#include <linux/clk.h>
Manuel Lauss47440222014-07-23 16:36:48 +020039#include <linux/clk-provider.h>
40#include <linux/clkdev.h>
Manuel Lauss47440222014-07-23 16:36:48 +020041#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/types.h>
44#include <asm/mach-au1x00/au1000.h>
45
46/* Base clock: 12MHz is the default in all databooks, and I haven't
47 * found any board yet which uses a different rate.
48 */
49#define ALCHEMY_ROOTCLK_RATE 12000000
50
51/*
52 * the internal sources which can be driven by the PLLs and dividers.
53 * Names taken from the databooks, refer to them for more information,
54 * especially which ones are share a clock line.
55 */
56static const char * const alchemy_au1300_intclknames[] = {
57 "lcd_intclk", "gpemgp_clk", "maempe_clk", "maebsa_clk",
58 "EXTCLK0", "EXTCLK1"
59};
60
61static const char * const alchemy_au1200_intclknames[] = {
62 "lcd_intclk", NULL, NULL, NULL, "EXTCLK0", "EXTCLK1"
63};
64
65static const char * const alchemy_au1550_intclknames[] = {
66 "usb_clk", "psc0_intclk", "psc1_intclk", "pci_clko",
67 "EXTCLK0", "EXTCLK1"
68};
69
70static const char * const alchemy_au1100_intclknames[] = {
71 "usb_clk", "lcd_intclk", NULL, "i2s_clk", "EXTCLK0", "EXTCLK1"
72};
73
74static const char * const alchemy_au1500_intclknames[] = {
75 NULL, "usbd_clk", "usbh_clk", "pci_clko", "EXTCLK0", "EXTCLK1"
76};
77
78static const char * const alchemy_au1000_intclknames[] = {
79 "irda_clk", "usbd_clk", "usbh_clk", "i2s_clk", "EXTCLK0",
80 "EXTCLK1"
81};
82
83/* aliases for a few on-chip sources which are either shared
84 * or have gone through name changes.
85 */
86static struct clk_aliastable {
87 char *alias;
88 char *base;
89 int cputype;
90} alchemy_clk_aliases[] __initdata = {
91 { "usbh_clk", "usb_clk", ALCHEMY_CPU_AU1100 },
92 { "usbd_clk", "usb_clk", ALCHEMY_CPU_AU1100 },
93 { "irda_clk", "usb_clk", ALCHEMY_CPU_AU1100 },
94 { "usbh_clk", "usb_clk", ALCHEMY_CPU_AU1550 },
95 { "usbd_clk", "usb_clk", ALCHEMY_CPU_AU1550 },
96 { "psc2_intclk", "usb_clk", ALCHEMY_CPU_AU1550 },
97 { "psc3_intclk", "EXTCLK0", ALCHEMY_CPU_AU1550 },
98 { "psc0_intclk", "EXTCLK0", ALCHEMY_CPU_AU1200 },
99 { "psc1_intclk", "EXTCLK1", ALCHEMY_CPU_AU1200 },
100 { "psc0_intclk", "EXTCLK0", ALCHEMY_CPU_AU1300 },
101 { "psc2_intclk", "EXTCLK0", ALCHEMY_CPU_AU1300 },
102 { "psc1_intclk", "EXTCLK1", ALCHEMY_CPU_AU1300 },
103 { "psc3_intclk", "EXTCLK1", ALCHEMY_CPU_AU1300 },
104
105 { NULL, NULL, 0 },
106};
107
108#define IOMEM(x) ((void __iomem *)(KSEG1ADDR(CPHYSADDR(x))))
109
110/* access locks to SYS_FREQCTRL0/1 and SYS_CLKSRC registers */
111static spinlock_t alchemy_clk_fg0_lock;
112static spinlock_t alchemy_clk_fg1_lock;
113static spinlock_t alchemy_clk_csrc_lock;
114
115/* CPU Core clock *****************************************************/
116
117static unsigned long alchemy_clk_cpu_recalc(struct clk_hw *hw,
118 unsigned long parent_rate)
119{
120 unsigned long t;
121
122 /*
123 * On early Au1000, sys_cpupll was write-only. Since these
124 * silicon versions of Au1000 are not sold, we don't bend
125 * over backwards trying to determine the frequency.
126 */
127 if (unlikely(au1xxx_cpu_has_pll_wo()))
128 t = 396000000;
129 else {
130 t = alchemy_rdsys(AU1000_SYS_CPUPLL) & 0x7f;
Manuel Lauss69e4e632015-02-18 11:01:56 +0100131 if (alchemy_get_cputype() < ALCHEMY_CPU_AU1300)
132 t &= 0x3f;
Manuel Lauss47440222014-07-23 16:36:48 +0200133 t *= parent_rate;
134 }
135
136 return t;
137}
138
Manuel Lauss45a848f2015-01-29 16:06:43 +0100139void __init alchemy_set_lpj(void)
140{
141 preset_lpj = alchemy_clk_cpu_recalc(NULL, ALCHEMY_ROOTCLK_RATE);
142 preset_lpj /= 2 * HZ;
143}
144
Manuel Lauss47440222014-07-23 16:36:48 +0200145static struct clk_ops alchemy_clkops_cpu = {
146 .recalc_rate = alchemy_clk_cpu_recalc,
147};
148
149static struct clk __init *alchemy_clk_setup_cpu(const char *parent_name,
150 int ctype)
151{
152 struct clk_init_data id;
153 struct clk_hw *h;
154
155 h = kzalloc(sizeof(*h), GFP_KERNEL);
156 if (!h)
157 return ERR_PTR(-ENOMEM);
158
159 id.name = ALCHEMY_CPU_CLK;
160 id.parent_names = &parent_name;
161 id.num_parents = 1;
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200162 id.flags = CLK_IS_BASIC;
Manuel Lauss47440222014-07-23 16:36:48 +0200163 id.ops = &alchemy_clkops_cpu;
164 h->init = &id;
165
166 return clk_register(NULL, h);
167}
168
169/* AUXPLLs ************************************************************/
170
171struct alchemy_auxpll_clk {
172 struct clk_hw hw;
173 unsigned long reg; /* au1300 has also AUXPLL2 */
174 int maxmult; /* max multiplier */
175};
176#define to_auxpll_clk(x) container_of(x, struct alchemy_auxpll_clk, hw)
177
178static unsigned long alchemy_clk_aux_recalc(struct clk_hw *hw,
179 unsigned long parent_rate)
180{
181 struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
182
183 return (alchemy_rdsys(a->reg) & 0xff) * parent_rate;
184}
185
186static int alchemy_clk_aux_setr(struct clk_hw *hw,
187 unsigned long rate,
188 unsigned long parent_rate)
189{
190 struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
191 unsigned long d = rate;
192
193 if (rate)
194 d /= parent_rate;
195 else
196 d = 0;
197
198 /* minimum is 84MHz, max is 756-1032 depending on variant */
199 if (((d < 7) && (d != 0)) || (d > a->maxmult))
200 return -EINVAL;
201
202 alchemy_wrsys(d, a->reg);
203 return 0;
204}
205
206static long alchemy_clk_aux_roundr(struct clk_hw *hw,
207 unsigned long rate,
208 unsigned long *parent_rate)
209{
210 struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
211 unsigned long mult;
212
213 if (!rate || !*parent_rate)
214 return 0;
215
216 mult = rate / (*parent_rate);
217
218 if (mult && (mult < 7))
219 mult = 7;
220 if (mult > a->maxmult)
221 mult = a->maxmult;
222
223 return (*parent_rate) * mult;
224}
225
226static struct clk_ops alchemy_clkops_aux = {
227 .recalc_rate = alchemy_clk_aux_recalc,
228 .set_rate = alchemy_clk_aux_setr,
229 .round_rate = alchemy_clk_aux_roundr,
230};
231
232static struct clk __init *alchemy_clk_setup_aux(const char *parent_name,
233 char *name, int maxmult,
234 unsigned long reg)
235{
236 struct clk_init_data id;
237 struct clk *c;
238 struct alchemy_auxpll_clk *a;
239
240 a = kzalloc(sizeof(*a), GFP_KERNEL);
241 if (!a)
242 return ERR_PTR(-ENOMEM);
243
244 id.name = name;
245 id.parent_names = &parent_name;
246 id.num_parents = 1;
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200247 id.flags = CLK_GET_RATE_NOCACHE;
Manuel Lauss47440222014-07-23 16:36:48 +0200248 id.ops = &alchemy_clkops_aux;
249
250 a->reg = reg;
251 a->maxmult = maxmult;
252 a->hw.init = &id;
253
254 c = clk_register(NULL, &a->hw);
255 if (!IS_ERR(c))
256 clk_register_clkdev(c, name, NULL);
257 else
258 kfree(a);
259
260 return c;
261}
262
263/* sysbus_clk *********************************************************/
264
265static struct clk __init *alchemy_clk_setup_sysbus(const char *pn)
266{
267 unsigned long v = (alchemy_rdsys(AU1000_SYS_POWERCTRL) & 3) + 2;
268 struct clk *c;
269
270 c = clk_register_fixed_factor(NULL, ALCHEMY_SYSBUS_CLK,
271 pn, 0, 1, v);
272 if (!IS_ERR(c))
273 clk_register_clkdev(c, ALCHEMY_SYSBUS_CLK, NULL);
274 return c;
275}
276
277/* Peripheral Clock ***************************************************/
278
279static struct clk __init *alchemy_clk_setup_periph(const char *pn)
280{
281 /* Peripheral clock runs at half the rate of sysbus clk */
282 struct clk *c;
283
284 c = clk_register_fixed_factor(NULL, ALCHEMY_PERIPH_CLK,
285 pn, 0, 1, 2);
286 if (!IS_ERR(c))
287 clk_register_clkdev(c, ALCHEMY_PERIPH_CLK, NULL);
288 return c;
289}
290
291/* mem clock **********************************************************/
292
293static struct clk __init *alchemy_clk_setup_mem(const char *pn, int ct)
294{
295 void __iomem *addr = IOMEM(AU1000_MEM_PHYS_ADDR);
296 unsigned long v;
297 struct clk *c;
298 int div;
299
300 switch (ct) {
301 case ALCHEMY_CPU_AU1550:
302 case ALCHEMY_CPU_AU1200:
303 v = __raw_readl(addr + AU1550_MEM_SDCONFIGB);
304 div = (v & (1 << 15)) ? 1 : 2;
305 break;
306 case ALCHEMY_CPU_AU1300:
307 v = __raw_readl(addr + AU1550_MEM_SDCONFIGB);
308 div = (v & (1 << 31)) ? 1 : 2;
309 break;
310 case ALCHEMY_CPU_AU1000:
311 case ALCHEMY_CPU_AU1500:
312 case ALCHEMY_CPU_AU1100:
313 default:
314 div = 2;
315 break;
316 }
317
318 c = clk_register_fixed_factor(NULL, ALCHEMY_MEM_CLK, pn,
319 0, 1, div);
320 if (!IS_ERR(c))
321 clk_register_clkdev(c, ALCHEMY_MEM_CLK, NULL);
322 return c;
323}
324
325/* lrclk: external synchronous static bus clock ***********************/
326
Manuel Lauss51f105d32015-01-29 16:06:42 +0100327static struct clk __init *alchemy_clk_setup_lrclk(const char *pn, int t)
Manuel Lauss47440222014-07-23 16:36:48 +0200328{
Manuel Lauss51f105d32015-01-29 16:06:42 +0100329 /* Au1000, Au1500: MEM_STCFG0[11]: If bit is set, lrclk=pclk/5,
330 * otherwise lrclk=pclk/4.
331 * All other variants: MEM_STCFG0[15:13] = divisor.
Manuel Lauss47440222014-07-23 16:36:48 +0200332 * L/RCLK = periph_clk / (divisor + 1)
333 * On Au1000, Au1500, Au1100 it's called LCLK,
334 * on later models it's called RCLK, but it's the same thing.
335 */
336 struct clk *c;
Manuel Lauss51f105d32015-01-29 16:06:42 +0100337 unsigned long v = alchemy_rdsmem(AU1000_MEM_STCFG0);
Manuel Lauss47440222014-07-23 16:36:48 +0200338
Manuel Lauss51f105d32015-01-29 16:06:42 +0100339 switch (t) {
340 case ALCHEMY_CPU_AU1000:
341 case ALCHEMY_CPU_AU1500:
342 v = 4 + ((v >> 11) & 1);
343 break;
344 default: /* all other models */
345 v = ((v >> 13) & 7) + 1;
346 }
Manuel Lauss47440222014-07-23 16:36:48 +0200347 c = clk_register_fixed_factor(NULL, ALCHEMY_LR_CLK,
348 pn, 0, 1, v);
349 if (!IS_ERR(c))
350 clk_register_clkdev(c, ALCHEMY_LR_CLK, NULL);
351 return c;
352}
353
354/* Clock dividers and muxes *******************************************/
355
356/* data for fgen and csrc mux-dividers */
357struct alchemy_fgcs_clk {
358 struct clk_hw hw;
359 spinlock_t *reglock; /* register lock */
360 unsigned long reg; /* SYS_FREQCTRL0/1 */
361 int shift; /* offset in register */
362 int parent; /* parent before disable [Au1300] */
363 int isen; /* is it enabled? */
364 int *dt; /* dividertable for csrc */
365};
366#define to_fgcs_clk(x) container_of(x, struct alchemy_fgcs_clk, hw)
367
368static long alchemy_calc_div(unsigned long rate, unsigned long prate,
369 int scale, int maxdiv, unsigned long *rv)
370{
371 long div1, div2;
372
373 div1 = prate / rate;
374 if ((prate / div1) > rate)
375 div1++;
376
377 if (scale == 2) { /* only div-by-multiple-of-2 possible */
378 if (div1 & 1)
379 div1++; /* stay <=prate */
380 }
381
382 div2 = (div1 / scale) - 1; /* value to write to register */
383
384 if (div2 > maxdiv)
385 div2 = maxdiv;
386 if (rv)
387 *rv = div2;
388
389 div1 = ((div2 + 1) * scale);
390 return div1;
391}
392
393static long alchemy_clk_fgcs_detr(struct clk_hw *hw, unsigned long rate,
394 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100395 struct clk_hw **best_parent_clk,
Manuel Lauss47440222014-07-23 16:36:48 +0200396 int scale, int maxdiv)
397{
398 struct clk *pc, *bpc, *free;
399 long tdv, tpr, pr, nr, br, bpr, diff, lastdiff;
400 int j;
401
402 lastdiff = INT_MAX;
403 bpr = 0;
404 bpc = NULL;
405 br = -EINVAL;
406 free = NULL;
407
408 /* look at the rates each enabled parent supplies and select
409 * the one that gets closest to but not over the requested rate.
410 */
411 for (j = 0; j < 7; j++) {
412 pc = clk_get_parent_by_index(hw->clk, j);
413 if (!pc)
414 break;
415
416 /* if this parent is currently unused, remember it.
Tomeu Vizoso24c71c82014-10-20 15:40:01 +0200417 * XXX: we would actually want clk_has_active_children()
418 * but this is a good-enough approximation for now.
Manuel Lauss47440222014-07-23 16:36:48 +0200419 */
Tomeu Vizoso24c71c82014-10-20 15:40:01 +0200420 if (!__clk_is_prepared(pc)) {
Manuel Lauss47440222014-07-23 16:36:48 +0200421 if (!free)
422 free = pc;
423 }
424
425 pr = clk_get_rate(pc);
426 if (pr < rate)
427 continue;
428
429 /* what can hardware actually provide */
430 tdv = alchemy_calc_div(rate, pr, scale, maxdiv, NULL);
431 nr = pr / tdv;
432 diff = rate - nr;
433 if (nr > rate)
434 continue;
435
436 if (diff < lastdiff) {
437 lastdiff = diff;
438 bpr = pr;
439 bpc = pc;
440 br = nr;
441 }
442 if (diff == 0)
443 break;
444 }
445
446 /* if we couldn't get the exact rate we wanted from the enabled
447 * parents, maybe we can tell an available disabled/inactive one
448 * to give us a rate we can divide down to the requested rate.
449 */
450 if (lastdiff && free) {
451 for (j = (maxdiv == 4) ? 1 : scale; j <= maxdiv; j += scale) {
452 tpr = rate * j;
453 if (tpr < 0)
454 break;
455 pr = clk_round_rate(free, tpr);
456
457 tdv = alchemy_calc_div(rate, pr, scale, maxdiv, NULL);
458 nr = pr / tdv;
459 diff = rate - nr;
460 if (nr > rate)
461 continue;
462 if (diff < lastdiff) {
463 lastdiff = diff;
464 bpr = pr;
465 bpc = free;
466 br = nr;
467 }
468 if (diff == 0)
469 break;
470 }
471 }
472
473 *best_parent_rate = bpr;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100474 *best_parent_clk = __clk_get_hw(bpc);
Manuel Lauss47440222014-07-23 16:36:48 +0200475 return br;
476}
477
478static int alchemy_clk_fgv1_en(struct clk_hw *hw)
479{
480 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
481 unsigned long v, flags;
482
483 spin_lock_irqsave(c->reglock, flags);
484 v = alchemy_rdsys(c->reg);
485 v |= (1 << 1) << c->shift;
486 alchemy_wrsys(v, c->reg);
487 spin_unlock_irqrestore(c->reglock, flags);
488
489 return 0;
490}
491
492static int alchemy_clk_fgv1_isen(struct clk_hw *hw)
493{
494 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
495 unsigned long v = alchemy_rdsys(c->reg) >> (c->shift + 1);
496
497 return v & 1;
498}
499
500static void alchemy_clk_fgv1_dis(struct clk_hw *hw)
501{
502 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
503 unsigned long v, flags;
504
505 spin_lock_irqsave(c->reglock, flags);
506 v = alchemy_rdsys(c->reg);
507 v &= ~((1 << 1) << c->shift);
508 alchemy_wrsys(v, c->reg);
509 spin_unlock_irqrestore(c->reglock, flags);
510}
511
512static int alchemy_clk_fgv1_setp(struct clk_hw *hw, u8 index)
513{
514 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
515 unsigned long v, flags;
516
517 spin_lock_irqsave(c->reglock, flags);
518 v = alchemy_rdsys(c->reg);
519 if (index)
520 v |= (1 << c->shift);
521 else
522 v &= ~(1 << c->shift);
523 alchemy_wrsys(v, c->reg);
524 spin_unlock_irqrestore(c->reglock, flags);
525
526 return 0;
527}
528
529static u8 alchemy_clk_fgv1_getp(struct clk_hw *hw)
530{
531 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
532
533 return (alchemy_rdsys(c->reg) >> c->shift) & 1;
534}
535
536static int alchemy_clk_fgv1_setr(struct clk_hw *hw, unsigned long rate,
537 unsigned long parent_rate)
538{
539 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
540 unsigned long div, v, flags, ret;
541 int sh = c->shift + 2;
542
543 if (!rate || !parent_rate || rate > (parent_rate / 2))
544 return -EINVAL;
545 ret = alchemy_calc_div(rate, parent_rate, 2, 512, &div);
546 spin_lock_irqsave(c->reglock, flags);
547 v = alchemy_rdsys(c->reg);
548 v &= ~(0xff << sh);
549 v |= div << sh;
550 alchemy_wrsys(v, c->reg);
551 spin_unlock_irqrestore(c->reglock, flags);
552
553 return 0;
554}
555
556static unsigned long alchemy_clk_fgv1_recalc(struct clk_hw *hw,
557 unsigned long parent_rate)
558{
559 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
560 unsigned long v = alchemy_rdsys(c->reg) >> (c->shift + 2);
561
562 v = ((v & 0xff) + 1) * 2;
563 return parent_rate / v;
564}
565
566static long alchemy_clk_fgv1_detr(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100567 unsigned long min_rate,
568 unsigned long max_rate,
Manuel Lauss47440222014-07-23 16:36:48 +0200569 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100570 struct clk_hw **best_parent_clk)
Manuel Lauss47440222014-07-23 16:36:48 +0200571{
572 return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
573 best_parent_clk, 2, 512);
574}
575
576/* Au1000, Au1100, Au15x0, Au12x0 */
577static struct clk_ops alchemy_clkops_fgenv1 = {
578 .recalc_rate = alchemy_clk_fgv1_recalc,
579 .determine_rate = alchemy_clk_fgv1_detr,
580 .set_rate = alchemy_clk_fgv1_setr,
581 .set_parent = alchemy_clk_fgv1_setp,
582 .get_parent = alchemy_clk_fgv1_getp,
583 .enable = alchemy_clk_fgv1_en,
584 .disable = alchemy_clk_fgv1_dis,
585 .is_enabled = alchemy_clk_fgv1_isen,
586};
587
588static void __alchemy_clk_fgv2_en(struct alchemy_fgcs_clk *c)
589{
590 unsigned long v = alchemy_rdsys(c->reg);
591
592 v &= ~(3 << c->shift);
593 v |= (c->parent & 3) << c->shift;
594 alchemy_wrsys(v, c->reg);
595 c->isen = 1;
596}
597
598static int alchemy_clk_fgv2_en(struct clk_hw *hw)
599{
600 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
601 unsigned long flags;
602
603 /* enable by setting the previous parent clock */
604 spin_lock_irqsave(c->reglock, flags);
605 __alchemy_clk_fgv2_en(c);
606 spin_unlock_irqrestore(c->reglock, flags);
607
608 return 0;
609}
610
611static int alchemy_clk_fgv2_isen(struct clk_hw *hw)
612{
613 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
614
615 return ((alchemy_rdsys(c->reg) >> c->shift) & 3) != 0;
616}
617
618static void alchemy_clk_fgv2_dis(struct clk_hw *hw)
619{
620 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
621 unsigned long v, flags;
622
623 spin_lock_irqsave(c->reglock, flags);
624 v = alchemy_rdsys(c->reg);
625 v &= ~(3 << c->shift); /* set input mux to "disabled" state */
626 alchemy_wrsys(v, c->reg);
627 c->isen = 0;
628 spin_unlock_irqrestore(c->reglock, flags);
629}
630
631static int alchemy_clk_fgv2_setp(struct clk_hw *hw, u8 index)
632{
633 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
634 unsigned long flags;
635
636 spin_lock_irqsave(c->reglock, flags);
637 c->parent = index + 1; /* value to write to register */
638 if (c->isen)
639 __alchemy_clk_fgv2_en(c);
640 spin_unlock_irqrestore(c->reglock, flags);
641
642 return 0;
643}
644
645static u8 alchemy_clk_fgv2_getp(struct clk_hw *hw)
646{
647 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
648 unsigned long flags, v;
649
650 spin_lock_irqsave(c->reglock, flags);
651 v = c->parent - 1;
652 spin_unlock_irqrestore(c->reglock, flags);
653 return v;
654}
655
656/* fg0-2 and fg4-6 share a "scale"-bit. With this bit cleared, the
657 * dividers behave exactly as on previous models (dividers are multiples
658 * of 2); with the bit set, dividers are multiples of 1, halving their
659 * range, but making them also much more flexible.
660 */
661static int alchemy_clk_fgv2_setr(struct clk_hw *hw, unsigned long rate,
662 unsigned long parent_rate)
663{
664 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
665 int sh = c->shift + 2;
666 unsigned long div, v, flags, ret;
667
668 if (!rate || !parent_rate || rate > parent_rate)
669 return -EINVAL;
670
671 v = alchemy_rdsys(c->reg) & (1 << 30); /* test "scale" bit */
672 ret = alchemy_calc_div(rate, parent_rate, v ? 1 : 2,
673 v ? 256 : 512, &div);
674
675 spin_lock_irqsave(c->reglock, flags);
676 v = alchemy_rdsys(c->reg);
677 v &= ~(0xff << sh);
678 v |= (div & 0xff) << sh;
679 alchemy_wrsys(v, c->reg);
680 spin_unlock_irqrestore(c->reglock, flags);
681
682 return 0;
683}
684
685static unsigned long alchemy_clk_fgv2_recalc(struct clk_hw *hw,
686 unsigned long parent_rate)
687{
688 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
689 int sh = c->shift + 2;
690 unsigned long v, t;
691
692 v = alchemy_rdsys(c->reg);
693 t = parent_rate / (((v >> sh) & 0xff) + 1);
694 if ((v & (1 << 30)) == 0) /* test scale bit */
695 t /= 2;
696
697 return t;
698}
699
700static long alchemy_clk_fgv2_detr(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100701 unsigned long min_rate,
702 unsigned long max_rate,
Manuel Lauss47440222014-07-23 16:36:48 +0200703 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100704 struct clk_hw **best_parent_clk)
Manuel Lauss47440222014-07-23 16:36:48 +0200705{
706 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
707 int scale, maxdiv;
708
709 if (alchemy_rdsys(c->reg) & (1 << 30)) {
710 scale = 1;
711 maxdiv = 256;
712 } else {
713 scale = 2;
714 maxdiv = 512;
715 }
716
717 return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
718 best_parent_clk, scale, maxdiv);
719}
720
721/* Au1300 larger input mux, no separate disable bit, flexible divider */
722static struct clk_ops alchemy_clkops_fgenv2 = {
723 .recalc_rate = alchemy_clk_fgv2_recalc,
724 .determine_rate = alchemy_clk_fgv2_detr,
725 .set_rate = alchemy_clk_fgv2_setr,
726 .set_parent = alchemy_clk_fgv2_setp,
727 .get_parent = alchemy_clk_fgv2_getp,
728 .enable = alchemy_clk_fgv2_en,
729 .disable = alchemy_clk_fgv2_dis,
730 .is_enabled = alchemy_clk_fgv2_isen,
731};
732
733static const char * const alchemy_clk_fgv1_parents[] = {
734 ALCHEMY_CPU_CLK, ALCHEMY_AUXPLL_CLK
735};
736
737static const char * const alchemy_clk_fgv2_parents[] = {
738 ALCHEMY_AUXPLL2_CLK, ALCHEMY_CPU_CLK, ALCHEMY_AUXPLL_CLK
739};
740
741static const char * const alchemy_clk_fgen_names[] = {
742 ALCHEMY_FG0_CLK, ALCHEMY_FG1_CLK, ALCHEMY_FG2_CLK,
743 ALCHEMY_FG3_CLK, ALCHEMY_FG4_CLK, ALCHEMY_FG5_CLK };
744
745static int __init alchemy_clk_init_fgens(int ctype)
746{
747 struct clk *c;
748 struct clk_init_data id;
749 struct alchemy_fgcs_clk *a;
750 unsigned long v;
751 int i, ret;
752
753 switch (ctype) {
754 case ALCHEMY_CPU_AU1000...ALCHEMY_CPU_AU1200:
755 id.ops = &alchemy_clkops_fgenv1;
Krzysztof Kozlowski2e93f682015-04-28 13:46:23 +0900756 id.parent_names = alchemy_clk_fgv1_parents;
Manuel Lauss47440222014-07-23 16:36:48 +0200757 id.num_parents = 2;
758 break;
759 case ALCHEMY_CPU_AU1300:
760 id.ops = &alchemy_clkops_fgenv2;
Krzysztof Kozlowski2e93f682015-04-28 13:46:23 +0900761 id.parent_names = alchemy_clk_fgv2_parents;
Manuel Lauss47440222014-07-23 16:36:48 +0200762 id.num_parents = 3;
763 break;
764 default:
765 return -ENODEV;
766 }
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200767 id.flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE;
Manuel Lauss47440222014-07-23 16:36:48 +0200768
769 a = kzalloc((sizeof(*a)) * 6, GFP_KERNEL);
770 if (!a)
771 return -ENOMEM;
772
773 spin_lock_init(&alchemy_clk_fg0_lock);
774 spin_lock_init(&alchemy_clk_fg1_lock);
775 ret = 0;
776 for (i = 0; i < 6; i++) {
777 id.name = alchemy_clk_fgen_names[i];
778 a->shift = 10 * (i < 3 ? i : i - 3);
779 if (i > 2) {
780 a->reg = AU1000_SYS_FREQCTRL1;
781 a->reglock = &alchemy_clk_fg1_lock;
782 } else {
783 a->reg = AU1000_SYS_FREQCTRL0;
784 a->reglock = &alchemy_clk_fg0_lock;
785 }
786
787 /* default to first parent if bootloader has set
788 * the mux to disabled state.
789 */
790 if (ctype == ALCHEMY_CPU_AU1300) {
791 v = alchemy_rdsys(a->reg);
792 a->parent = (v >> a->shift) & 3;
793 if (!a->parent) {
794 a->parent = 1;
795 a->isen = 0;
796 } else
797 a->isen = 1;
798 }
799
800 a->hw.init = &id;
801 c = clk_register(NULL, &a->hw);
802 if (IS_ERR(c))
803 ret++;
804 else
805 clk_register_clkdev(c, id.name, NULL);
806 a++;
807 }
808
809 return ret;
810}
811
812/* internal sources muxes *********************************************/
813
814static int alchemy_clk_csrc_isen(struct clk_hw *hw)
815{
816 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
817 unsigned long v = alchemy_rdsys(c->reg);
818
819 return (((v >> c->shift) >> 2) & 7) != 0;
820}
821
822static void __alchemy_clk_csrc_en(struct alchemy_fgcs_clk *c)
823{
824 unsigned long v = alchemy_rdsys(c->reg);
825
826 v &= ~((7 << 2) << c->shift);
827 v |= ((c->parent & 7) << 2) << c->shift;
828 alchemy_wrsys(v, c->reg);
829 c->isen = 1;
830}
831
832static int alchemy_clk_csrc_en(struct clk_hw *hw)
833{
834 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
835 unsigned long flags;
836
837 /* enable by setting the previous parent clock */
838 spin_lock_irqsave(c->reglock, flags);
839 __alchemy_clk_csrc_en(c);
840 spin_unlock_irqrestore(c->reglock, flags);
841
842 return 0;
843}
844
845static void alchemy_clk_csrc_dis(struct clk_hw *hw)
846{
847 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
848 unsigned long v, flags;
849
850 spin_lock_irqsave(c->reglock, flags);
851 v = alchemy_rdsys(c->reg);
852 v &= ~((3 << 2) << c->shift); /* mux to "disabled" state */
853 alchemy_wrsys(v, c->reg);
854 c->isen = 0;
855 spin_unlock_irqrestore(c->reglock, flags);
856}
857
858static int alchemy_clk_csrc_setp(struct clk_hw *hw, u8 index)
859{
860 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
861 unsigned long flags;
862
863 spin_lock_irqsave(c->reglock, flags);
864 c->parent = index + 1; /* value to write to register */
865 if (c->isen)
866 __alchemy_clk_csrc_en(c);
867 spin_unlock_irqrestore(c->reglock, flags);
868
869 return 0;
870}
871
872static u8 alchemy_clk_csrc_getp(struct clk_hw *hw)
873{
874 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
875
876 return c->parent - 1;
877}
878
879static unsigned long alchemy_clk_csrc_recalc(struct clk_hw *hw,
880 unsigned long parent_rate)
881{
882 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
883 unsigned long v = (alchemy_rdsys(c->reg) >> c->shift) & 3;
884
885 return parent_rate / c->dt[v];
886}
887
888static int alchemy_clk_csrc_setr(struct clk_hw *hw, unsigned long rate,
889 unsigned long parent_rate)
890{
891 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
892 unsigned long d, v, flags;
893 int i;
894
895 if (!rate || !parent_rate || rate > parent_rate)
896 return -EINVAL;
897
898 d = (parent_rate + (rate / 2)) / rate;
899 if (d > 4)
900 return -EINVAL;
901 if ((d == 3) && (c->dt[2] != 3))
902 d = 4;
903
904 for (i = 0; i < 4; i++)
905 if (c->dt[i] == d)
906 break;
907
908 if (i >= 4)
909 return -EINVAL; /* oops */
910
911 spin_lock_irqsave(c->reglock, flags);
912 v = alchemy_rdsys(c->reg);
913 v &= ~(3 << c->shift);
914 v |= (i & 3) << c->shift;
915 alchemy_wrsys(v, c->reg);
916 spin_unlock_irqrestore(c->reglock, flags);
917
918 return 0;
919}
920
921static long alchemy_clk_csrc_detr(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100922 unsigned long min_rate,
923 unsigned long max_rate,
Manuel Lauss47440222014-07-23 16:36:48 +0200924 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100925 struct clk_hw **best_parent_clk)
Manuel Lauss47440222014-07-23 16:36:48 +0200926{
927 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
928 int scale = c->dt[2] == 3 ? 1 : 2; /* au1300 check */
929
930 return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
931 best_parent_clk, scale, 4);
932}
933
934static struct clk_ops alchemy_clkops_csrc = {
935 .recalc_rate = alchemy_clk_csrc_recalc,
936 .determine_rate = alchemy_clk_csrc_detr,
937 .set_rate = alchemy_clk_csrc_setr,
938 .set_parent = alchemy_clk_csrc_setp,
939 .get_parent = alchemy_clk_csrc_getp,
940 .enable = alchemy_clk_csrc_en,
941 .disable = alchemy_clk_csrc_dis,
942 .is_enabled = alchemy_clk_csrc_isen,
943};
944
945static const char * const alchemy_clk_csrc_parents[] = {
946 /* disabled at index 0 */ ALCHEMY_AUXPLL_CLK,
947 ALCHEMY_FG0_CLK, ALCHEMY_FG1_CLK, ALCHEMY_FG2_CLK,
948 ALCHEMY_FG3_CLK, ALCHEMY_FG4_CLK, ALCHEMY_FG5_CLK
949};
950
951/* divider tables */
952static int alchemy_csrc_dt1[] = { 1, 4, 1, 2 }; /* rest */
953static int alchemy_csrc_dt2[] = { 1, 4, 3, 2 }; /* Au1300 */
954
955static int __init alchemy_clk_setup_imux(int ctype)
956{
957 struct alchemy_fgcs_clk *a;
958 const char * const *names;
959 struct clk_init_data id;
960 unsigned long v;
961 int i, ret, *dt;
962 struct clk *c;
963
964 id.ops = &alchemy_clkops_csrc;
Krzysztof Kozlowski2e93f682015-04-28 13:46:23 +0900965 id.parent_names = alchemy_clk_csrc_parents;
Manuel Lauss47440222014-07-23 16:36:48 +0200966 id.num_parents = 7;
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200967 id.flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE;
Manuel Lauss47440222014-07-23 16:36:48 +0200968
969 dt = alchemy_csrc_dt1;
970 switch (ctype) {
971 case ALCHEMY_CPU_AU1000:
972 names = alchemy_au1000_intclknames;
973 break;
974 case ALCHEMY_CPU_AU1500:
975 names = alchemy_au1500_intclknames;
976 break;
977 case ALCHEMY_CPU_AU1100:
978 names = alchemy_au1100_intclknames;
979 break;
980 case ALCHEMY_CPU_AU1550:
981 names = alchemy_au1550_intclknames;
982 break;
983 case ALCHEMY_CPU_AU1200:
984 names = alchemy_au1200_intclknames;
985 break;
986 case ALCHEMY_CPU_AU1300:
987 dt = alchemy_csrc_dt2;
988 names = alchemy_au1300_intclknames;
989 break;
990 default:
991 return -ENODEV;
992 }
993
994 a = kzalloc((sizeof(*a)) * 6, GFP_KERNEL);
995 if (!a)
996 return -ENOMEM;
997
998 spin_lock_init(&alchemy_clk_csrc_lock);
999 ret = 0;
1000
1001 for (i = 0; i < 6; i++) {
1002 id.name = names[i];
1003 if (!id.name)
1004 goto next;
1005
1006 a->shift = i * 5;
1007 a->reg = AU1000_SYS_CLKSRC;
1008 a->reglock = &alchemy_clk_csrc_lock;
1009 a->dt = dt;
1010
1011 /* default to first parent clock if mux is initially
1012 * set to disabled state.
1013 */
1014 v = alchemy_rdsys(a->reg);
1015 a->parent = ((v >> a->shift) >> 2) & 7;
1016 if (!a->parent) {
1017 a->parent = 1;
1018 a->isen = 0;
1019 } else
1020 a->isen = 1;
1021
1022 a->hw.init = &id;
1023 c = clk_register(NULL, &a->hw);
1024 if (IS_ERR(c))
1025 ret++;
1026 else
1027 clk_register_clkdev(c, id.name, NULL);
1028next:
1029 a++;
1030 }
1031
1032 return ret;
1033}
1034
1035
1036/**********************************************************************/
1037
1038
1039#define ERRCK(x) \
1040 if (IS_ERR(x)) { \
1041 ret = PTR_ERR(x); \
1042 goto out; \
1043 }
1044
1045static int __init alchemy_clk_init(void)
1046{
1047 int ctype = alchemy_get_cputype(), ret, i;
1048 struct clk_aliastable *t = alchemy_clk_aliases;
1049 struct clk *c;
1050
1051 /* Root of the Alchemy clock tree: external 12MHz crystal osc */
1052 c = clk_register_fixed_rate(NULL, ALCHEMY_ROOT_CLK, NULL,
1053 CLK_IS_ROOT,
1054 ALCHEMY_ROOTCLK_RATE);
1055 ERRCK(c)
1056
1057 /* CPU core clock */
1058 c = alchemy_clk_setup_cpu(ALCHEMY_ROOT_CLK, ctype);
1059 ERRCK(c)
1060
1061 /* AUXPLLs: max 1GHz on Au1300, 748MHz on older models */
1062 i = (ctype == ALCHEMY_CPU_AU1300) ? 84 : 63;
1063 c = alchemy_clk_setup_aux(ALCHEMY_ROOT_CLK, ALCHEMY_AUXPLL_CLK,
1064 i, AU1000_SYS_AUXPLL);
1065 ERRCK(c)
1066
1067 if (ctype == ALCHEMY_CPU_AU1300) {
1068 c = alchemy_clk_setup_aux(ALCHEMY_ROOT_CLK,
1069 ALCHEMY_AUXPLL2_CLK, i,
1070 AU1300_SYS_AUXPLL2);
1071 ERRCK(c)
1072 }
1073
1074 /* sysbus clock: cpu core clock divided by 2, 3 or 4 */
1075 c = alchemy_clk_setup_sysbus(ALCHEMY_CPU_CLK);
1076 ERRCK(c)
1077
1078 /* peripheral clock: runs at half rate of sysbus clk */
1079 c = alchemy_clk_setup_periph(ALCHEMY_SYSBUS_CLK);
1080 ERRCK(c)
1081
1082 /* SDR/DDR memory clock */
1083 c = alchemy_clk_setup_mem(ALCHEMY_SYSBUS_CLK, ctype);
1084 ERRCK(c)
1085
1086 /* L/RCLK: external static bus clock for synchronous mode */
Manuel Lauss51f105d32015-01-29 16:06:42 +01001087 c = alchemy_clk_setup_lrclk(ALCHEMY_PERIPH_CLK, ctype);
Manuel Lauss47440222014-07-23 16:36:48 +02001088 ERRCK(c)
1089
1090 /* Frequency dividers 0-5 */
1091 ret = alchemy_clk_init_fgens(ctype);
1092 if (ret) {
1093 ret = -ENODEV;
1094 goto out;
1095 }
1096
1097 /* diving muxes for internal sources */
1098 ret = alchemy_clk_setup_imux(ctype);
1099 if (ret) {
1100 ret = -ENODEV;
1101 goto out;
1102 }
1103
1104 /* set up aliases drivers might look for */
1105 while (t->base) {
1106 if (t->cputype == ctype)
1107 clk_add_alias(t->alias, NULL, t->base, NULL);
1108 t++;
1109 }
1110
1111 pr_info("Alchemy clocktree installed\n");
1112 return 0;
1113
1114out:
1115 return ret;
1116}
1117postcore_initcall(alchemy_clk_init);