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