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