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