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