sh: sh7723 clock framework rewrite V2
[safe/jmp/linux-2.6] / arch / sh / kernel / cpu / sh4a / clock-sh7722.c
1 /*
2  * arch/sh/kernel/cpu/sh4a/clock-sh7722.c
3  *
4  * SH7343, SH7722 & SH7366 support for the clock framework
5  *
6  * Copyright (c) 2006-2007 Nomad Global Solutions Inc
7  * Based on code for sh7343 by Paul Mundt
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file "COPYING" in the main directory of this archive
11  * for more details.
12  */
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/io.h>
16 #include <linux/errno.h>
17 #include <linux/stringify.h>
18 #include <asm/clock.h>
19 #include <asm/freq.h>
20
21 #define N  (-1)
22 #define NM (-2)
23 #define ROUND_NEAREST 0
24 #define ROUND_DOWN -1
25 #define ROUND_UP   +1
26
27 static int adjust_algos[][3] = {
28         {},     /* NO_CHANGE */
29         { NM, N, 1 },   /* N:1, N:1 */
30         { 3, 2, 2 },    /* 3:2:2 */
31         { 5, 2, 2 },    /* 5:2:2 */
32         { N, 1, 1 },    /* N:1:1 */
33
34         { N, 1 },       /* N:1 */
35
36         { N, 1 },       /* N:1 */
37         { 3, 2 },
38         { 4, 3 },
39         { 5, 4 },
40
41         { N, 1 }
42 };
43
44 static unsigned long adjust_pair_of_clocks(unsigned long r1, unsigned long r2,
45                         int m1, int m2, int round_flag)
46 {
47         unsigned long rem, div;
48         int the_one = 0;
49
50         pr_debug( "Actual values: r1 = %ld\n", r1);
51         pr_debug( "...............r2 = %ld\n", r2);
52
53         if (m1 == m2) {
54                 r2 = r1;
55                 pr_debug( "setting equal rates: r2 now %ld\n", r2);
56         } else if ((m2 == N  && m1 == 1) ||
57                    (m2 == NM && m1 == N)) { /* N:1 or NM:N */
58                 pr_debug( "Setting rates as 1:N (N:N*M)\n");
59                 rem = r2 % r1;
60                 pr_debug( "...remainder = %ld\n", rem);
61                 if (rem) {
62                         div = r2 / r1;
63                         pr_debug( "...div = %ld\n", div);
64                         switch (round_flag) {
65                         case ROUND_NEAREST:
66                                 the_one = rem >= r1/2 ? 1 : 0; break;
67                         case ROUND_UP:
68                                 the_one = 1; break;
69                         case ROUND_DOWN:
70                                 the_one = 0; break;
71                         }
72
73                         r2 = r1 * (div + the_one);
74                         pr_debug( "...setting r2 to %ld\n", r2);
75                 }
76         } else if ((m2 == 1  && m1 == N) ||
77                    (m2 == N && m1 == NM)) { /* 1:N or N:NM */
78                 pr_debug( "Setting rates as N:1 (N*M:N)\n");
79                 rem = r1 % r2;
80                 pr_debug( "...remainder = %ld\n", rem);
81                 if (rem) {
82                         div = r1 / r2;
83                         pr_debug( "...div = %ld\n", div);
84                         switch (round_flag) {
85                         case ROUND_NEAREST:
86                                 the_one = rem > r2/2 ? 1 : 0; break;
87                         case ROUND_UP:
88                                 the_one = 0; break;
89                         case ROUND_DOWN:
90                                 the_one = 1; break;
91                         }
92
93                         r2 = r1 / (div + the_one);
94                         pr_debug( "...setting r2 to %ld\n", r2);
95                 }
96         } else { /* value:value */
97                 pr_debug( "Setting rates as %d:%d\n", m1, m2);
98                 div = r1 / m1;
99                 r2 = div * m2;
100                 pr_debug( "...div = %ld\n", div);
101                 pr_debug( "...setting r2 to %ld\n", r2);
102         }
103
104         return r2;
105 }
106
107 static void adjust_clocks(int originate, int *l, unsigned long v[],
108                           int n_in_line)
109 {
110         int x;
111
112         pr_debug( "Go down from %d...\n", originate);
113         /* go up recalculation clocks */
114         for (x = originate; x>0; x -- )
115                 v[x-1] = adjust_pair_of_clocks(v[x], v[x-1],
116                                         l[x], l[x-1],
117                                         ROUND_UP);
118
119         pr_debug( "Go up from %d...\n", originate);
120         /* go down recalculation clocks */
121         for (x = originate; x<n_in_line - 1; x ++ )
122                 v[x+1] = adjust_pair_of_clocks(v[x], v[x+1],
123                                         l[x], l[x+1],
124                                         ROUND_UP);
125 }
126
127
128 /*
129  * SH7722 uses a common set of multipliers and divisors, so this
130  * is quite simple..
131  */
132
133 #if defined(CONFIG_CPU_SUBTYPE_SH7724)
134 #define STCPLL(frqcr) ((((frqcr >> 24) & 0x3f) + 1) * 2)
135 #else
136 #define STCPLL(frqcr) (((frqcr >> 24) & 0x1f) + 1)
137 #endif
138
139 /*
140  * Instead of having two separate multipliers/divisors set, like this:
141  *
142  * static int multipliers[] = { 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
143  * static int divisors[] = { 1, 3, 2, 5, 3, 4, 5, 6, 8, 10, 12, 16, 20 };
144  *
145  * I created the divisors2 array, which is used to calculate rate like
146  *   rate = parent * 2 / divisors2[ divisor ];
147 */
148 #if defined(CONFIG_CPU_SUBTYPE_SH7724)
149 static int divisors2[] = { 4, 1, 8, 12, 16, 24, 32, 1, 48, 64, 72, 96, 1, 144 };
150 #else
151 static int divisors2[] = { 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40 };
152 #endif
153
154 static unsigned long master_clk_recalc(struct clk *clk)
155 {
156         unsigned frqcr = ctrl_inl(FRQCR);
157
158         return CONFIG_SH_PCLK_FREQ * STCPLL(frqcr);
159 }
160
161 static void master_clk_init(struct clk *clk)
162 {
163         clk->parent = NULL;
164         clk->rate = master_clk_recalc(clk);
165 }
166
167 static unsigned long module_clk_recalc(struct clk *clk)
168 {
169         unsigned long frqcr = ctrl_inl(FRQCR);
170
171         return clk->parent->rate / STCPLL(frqcr);
172 }
173
174 #if defined(CONFIG_CPU_SUBTYPE_SH7724)
175 #define MASTERDIVS      { 12, 16, 24, 30, 32, 36, 48 }
176 #define STCMASK         0x3f
177 #define DIVCALC(div)    (div/2-1)
178 #define FRQCRKICK       0x80000000
179 #else
180 #define MASTERDIVS      { 2, 3, 4, 6, 8, 16 }
181 #define STCMASK         0x1f
182 #define DIVCALC(div)    (div-1)
183 #define FRQCRKICK       0x00000000
184 #endif
185
186 static int master_clk_setrate(struct clk *clk, unsigned long rate, int id)
187 {
188         int div = rate / clk->rate;
189         int master_divs[] = MASTERDIVS;
190         int index;
191         unsigned long frqcr;
192
193         for (index = 1; index < ARRAY_SIZE(master_divs); index++)
194                 if (div >= master_divs[index - 1] && div < master_divs[index])
195                         break;
196
197         if (index >= ARRAY_SIZE(master_divs))
198                 index = ARRAY_SIZE(master_divs);
199         div = master_divs[index - 1];
200
201         frqcr = ctrl_inl(FRQCR);
202         frqcr &= ~(STCMASK << 24);
203         frqcr |= (DIVCALC(div) << 24);
204         frqcr |= FRQCRKICK;
205         ctrl_outl(frqcr, FRQCR);
206
207         return 0;
208 }
209
210 static struct clk_ops sh7722_master_clk_ops = {
211         .init = master_clk_init,
212         .recalc = master_clk_recalc,
213         .set_rate = master_clk_setrate,
214 };
215
216 static struct clk_ops sh7722_module_clk_ops = {
217        .recalc = module_clk_recalc,
218 };
219
220 struct frqcr_context {
221         unsigned mask;
222         unsigned shift;
223 };
224
225 struct frqcr_context sh7722_get_clk_context(const char *name)
226 {
227         struct frqcr_context ctx = { 0, };
228
229         if (!strcmp(name, "peripheral_clk")) {
230                 ctx.shift = 0;
231                 ctx.mask = 0xF;
232         } else if (!strcmp(name, "sdram_clk")) {
233                 ctx.shift = 4;
234                 ctx.mask = 0xF;
235         } else if (!strcmp(name, "bus_clk")) {
236                 ctx.shift = 8;
237                 ctx.mask = 0xF;
238         } else if (!strcmp(name, "sh_clk")) {
239                 ctx.shift = 12;
240                 ctx.mask = 0xF;
241         } else if (!strcmp(name, "umem_clk")) {
242                 ctx.shift = 16;
243                 ctx.mask = 0xF;
244         } else if (!strcmp(name, "cpu_clk")) {
245                 ctx.shift = 20;
246                 ctx.mask = 7;
247         }
248         return ctx;
249 }
250
251 /**
252  * sh7722_find_div_index - find divisor for setting rate
253  *
254  * All sh7722 clocks use the same set of multipliers/divisors. This function
255  * chooses correct divisor to set the rate of clock with parent clock that
256  * generates frequency of 'parent_rate'
257  *
258  * @parent_rate: rate of parent clock
259  * @rate: requested rate to be set
260  */
261 static int sh7722_find_div_index(unsigned long parent_rate, unsigned rate)
262 {
263         unsigned div2 = parent_rate * 2 / rate;
264         int index;
265
266         if (rate > parent_rate)
267                 return -EINVAL;
268
269         for (index = 1; index < ARRAY_SIZE(divisors2); index++) {
270                 if (div2 > divisors2[index - 1] && div2 <= divisors2[index])
271                         break;
272         }
273         if (index >= ARRAY_SIZE(divisors2))
274                 index = ARRAY_SIZE(divisors2) - 1;
275         return index;
276 }
277
278 static unsigned long sh7722_frqcr_recalc(struct clk *clk)
279 {
280         struct frqcr_context ctx = sh7722_get_clk_context(clk->name);
281         unsigned long frqcr = ctrl_inl(FRQCR);
282         int index;
283
284         index = (frqcr >> ctx.shift) & ctx.mask;
285         return clk->parent->rate * 2 / divisors2[index];
286 }
287
288 static int sh7722_frqcr_set_rate(struct clk *clk, unsigned long rate,
289                                  int algo_id)
290 {
291         struct frqcr_context ctx = sh7722_get_clk_context(clk->name);
292         unsigned long parent_rate = clk->parent->rate;
293         int div;
294         unsigned long frqcr;
295         int err = 0;
296
297         /* pretty invalid */
298         if (parent_rate < rate)
299                 return -EINVAL;
300
301         /* look for multiplier/divisor pair */
302         div = sh7722_find_div_index(parent_rate, rate);
303         if (div<0)
304                 return div;
305
306         /* calculate new value of clock rate */
307         clk->rate = parent_rate * 2 / divisors2[div];
308         frqcr = ctrl_inl(FRQCR);
309
310         /* FIXME: adjust as algo_id specifies */
311         if (algo_id != NO_CHANGE) {
312                 int originator;
313                 char *algo_group_1[] = { "cpu_clk", "umem_clk", "sh_clk" };
314                 char *algo_group_2[] = { "sh_clk", "bus_clk" };
315                 char *algo_group_3[] = { "sh_clk", "sdram_clk" };
316                 char *algo_group_4[] = { "bus_clk", "peripheral_clk" };
317                 char *algo_group_5[] = { "cpu_clk", "peripheral_clk" };
318                 char **algo_current = NULL;
319                 /* 3 is the maximum number of clocks in relation */
320                 struct clk *ck[3];
321                 unsigned long values[3]; /* the same comment as above */
322                 int part_length = -1;
323                 int i;
324
325                 /*
326                  * all the steps below only required if adjustion was
327                  * requested
328                  */
329                 if (algo_id == IUS_N1_N1 ||
330                     algo_id == IUS_322 ||
331                     algo_id == IUS_522 ||
332                     algo_id == IUS_N11) {
333                         algo_current = algo_group_1;
334                         part_length = 3;
335                 }
336                 if (algo_id == SB_N1) {
337                         algo_current = algo_group_2;
338                         part_length = 2;
339                 }
340                 if (algo_id == SB3_N1 ||
341                     algo_id == SB3_32 ||
342                     algo_id == SB3_43 ||
343                     algo_id == SB3_54) {
344                         algo_current = algo_group_3;
345                         part_length = 2;
346                 }
347                 if (algo_id == BP_N1) {
348                         algo_current = algo_group_4;
349                         part_length = 2;
350                 }
351                 if (algo_id == IP_N1) {
352                         algo_current = algo_group_5;
353                         part_length = 2;
354                 }
355                 if (!algo_current)
356                         goto incorrect_algo_id;
357
358                 originator = -1;
359                 for (i = 0; i < part_length; i ++ ) {
360                         if (originator >= 0 && !strcmp(clk->name,
361                                                        algo_current[i]))
362                                 originator = i;
363                         ck[i] = clk_get(NULL, algo_current[i]);
364                         values[i] = clk_get_rate(ck[i]);
365                 }
366
367                 if (originator >= 0)
368                         adjust_clocks(originator, adjust_algos[algo_id],
369                                       values, part_length);
370
371                 for (i = 0; i < part_length; i ++ ) {
372                         struct frqcr_context part_ctx;
373                         int part_div;
374
375                         if (likely(!err)) {
376                                 part_div = sh7722_find_div_index(parent_rate,
377                                                                 rate);
378                                 if (part_div > 0) {
379                                         part_ctx = sh7722_get_clk_context(
380                                                                 ck[i]->name);
381                                         frqcr &= ~(part_ctx.mask <<
382                                                    part_ctx.shift);
383                                         frqcr |= part_div << part_ctx.shift;
384                                 } else
385                                         err = part_div;
386                         }
387
388                         ck[i]->ops->recalc(ck[i]);
389                         clk_put(ck[i]);
390                 }
391         }
392
393         /* was there any error during recalculation ? If so, bail out.. */
394         if (unlikely(err!=0))
395                 goto out_err;
396
397         /* clear FRQCR bits */
398         frqcr &= ~(ctx.mask << ctx.shift);
399         frqcr |= div << ctx.shift;
400         frqcr |= FRQCRKICK;
401
402         /* ...and perform actual change */
403         ctrl_outl(frqcr, FRQCR);
404         return 0;
405
406 incorrect_algo_id:
407         return -EINVAL;
408 out_err:
409         return err;
410 }
411
412 static long sh7722_frqcr_round_rate(struct clk *clk, unsigned long rate)
413 {
414         unsigned long parent_rate = clk->parent->rate;
415         int div;
416
417         /* look for multiplier/divisor pair */
418         div = sh7722_find_div_index(parent_rate, rate);
419         if (div < 0)
420                 return clk->rate;
421
422         /* calculate new value of clock rate */
423         return parent_rate * 2 / divisors2[div];
424 }
425
426 static struct clk_ops sh7722_frqcr_clk_ops = {
427         .recalc = sh7722_frqcr_recalc,
428         .set_rate = sh7722_frqcr_set_rate,
429         .round_rate = sh7722_frqcr_round_rate,
430 };
431
432 /*
433  * clock ops methods for SIU A/B and IrDA clock
434  */
435 #ifndef CONFIG_CPU_SUBTYPE_SH7343
436 static int sh7722_siu_set_rate(struct clk *clk, unsigned long rate, int algo_id)
437 {
438         unsigned long r;
439         int div;
440
441         r = ctrl_inl(clk->arch_flags);
442         div = sh7722_find_div_index(clk->parent->rate, rate);
443         if (div < 0)
444                 return div;
445         r = (r & ~0xF) | div;
446         ctrl_outl(r, clk->arch_flags);
447         return 0;
448 }
449
450 static unsigned long sh7722_siu_recalc(struct clk *clk)
451 {
452         unsigned long r;
453
454         r = ctrl_inl(clk->arch_flags);
455         return clk->parent->rate * 2 / divisors2[r & 0xF];
456 }
457
458 static int sh7722_siu_start_stop(struct clk *clk, int enable)
459 {
460         unsigned long r;
461
462         r = ctrl_inl(clk->arch_flags);
463         if (enable)
464                 ctrl_outl(r & ~(1 << 8), clk->arch_flags);
465         else
466                 ctrl_outl(r | (1 << 8), clk->arch_flags);
467         return 0;
468 }
469
470 static int sh7722_siu_enable(struct clk *clk)
471 {
472         return sh7722_siu_start_stop(clk, 1);
473 }
474
475 static void sh7722_siu_disable(struct clk *clk)
476 {
477         sh7722_siu_start_stop(clk, 0);
478 }
479
480 static struct clk_ops sh7722_siu_clk_ops = {
481         .recalc = sh7722_siu_recalc,
482         .set_rate = sh7722_siu_set_rate,
483         .enable = sh7722_siu_enable,
484         .disable = sh7722_siu_disable,
485 };
486
487 #endif /* CONFIG_CPU_SUBTYPE_SH7343 */
488
489 static int sh7722_video_enable(struct clk *clk)
490 {
491         unsigned long r;
492
493         r = ctrl_inl(VCLKCR);
494         ctrl_outl( r & ~(1<<8), VCLKCR);
495         return 0;
496 }
497
498 static void sh7722_video_disable(struct clk *clk)
499 {
500         unsigned long r;
501
502         r = ctrl_inl(VCLKCR);
503         ctrl_outl( r | (1<<8), VCLKCR);
504 }
505
506 static int sh7722_video_set_rate(struct clk *clk, unsigned long rate,
507                                  int algo_id)
508 {
509         unsigned long r;
510
511         r = ctrl_inl(VCLKCR);
512         r &= ~0x3F;
513         r |= ((clk->parent->rate / rate - 1) & 0x3F);
514         ctrl_outl(r, VCLKCR);
515         return 0;
516 }
517
518 static unsigned long sh7722_video_recalc(struct clk *clk)
519 {
520         unsigned long r;
521
522         r = ctrl_inl(VCLKCR);
523         return clk->parent->rate / ((r & 0x3F) + 1);
524 }
525
526 static struct clk_ops sh7722_video_clk_ops = {
527         .recalc = sh7722_video_recalc,
528         .set_rate = sh7722_video_set_rate,
529         .enable = sh7722_video_enable,
530         .disable = sh7722_video_disable,
531 };
532 /*
533  * and at last, clock definitions themselves
534  */
535 static struct clk sh7722_umem_clock = {
536         .name = "umem_clk",
537         .ops = &sh7722_frqcr_clk_ops,
538 };
539
540 static struct clk sh7722_sh_clock = {
541         .name = "sh_clk",
542         .ops = &sh7722_frqcr_clk_ops,
543 };
544
545 static struct clk sh7722_peripheral_clock = {
546         .name = "peripheral_clk",
547         .ops = &sh7722_frqcr_clk_ops,
548 };
549
550 static struct clk sh7722_sdram_clock = {
551         .name = "sdram_clk",
552         .ops = &sh7722_frqcr_clk_ops,
553 };
554
555 static struct clk sh7722_r_clock = {
556         .name = "r_clk",
557         .rate = 32768,
558 };
559
560 #if !defined(CONFIG_CPU_SUBTYPE_SH7343) &&\
561     !defined(CONFIG_CPU_SUBTYPE_SH7724)
562 /*
563  * these three clocks - SIU A, SIU B, IrDA - share the same clk_ops
564  * methods of clk_ops determine which register they should access by
565  * examining clk->name field
566  */
567 static struct clk sh7722_siu_a_clock = {
568         .name = "siu_a_clk",
569         .arch_flags = SCLKACR,
570         .ops = &sh7722_siu_clk_ops,
571 };
572
573 static struct clk sh7722_siu_b_clock = {
574         .name = "siu_b_clk",
575         .arch_flags = SCLKBCR,
576         .ops = &sh7722_siu_clk_ops,
577 };
578 #endif /* CONFIG_CPU_SUBTYPE_SH7343, SH7724 */
579
580 #if defined(CONFIG_CPU_SUBTYPE_SH7722) ||\
581     defined(CONFIG_CPU_SUBTYPE_SH7724)
582 static struct clk sh7722_irda_clock = {
583         .name = "irda_clk",
584         .arch_flags = IrDACLKCR,
585         .ops = &sh7722_siu_clk_ops,
586 };
587 #endif
588
589 static struct clk sh7722_video_clock = {
590         .name = "video_clk",
591         .ops = &sh7722_video_clk_ops,
592 };
593
594 #define MSTPCR_ARCH_FLAGS(reg, bit) (((reg) << 8) | (bit))
595 #define MSTPCR_ARCH_FLAGS_REG(value) ((value) >> 8)
596 #define MSTPCR_ARCH_FLAGS_BIT(value) ((value) & 0xff)
597
598 static int sh7722_mstpcr_start_stop(struct clk *clk, int enable)
599 {
600         unsigned long bit = MSTPCR_ARCH_FLAGS_BIT(clk->arch_flags);
601         unsigned long reg;
602         unsigned long r;
603
604         switch(MSTPCR_ARCH_FLAGS_REG(clk->arch_flags)) {
605         case 0:
606                 reg = MSTPCR0;
607                 break;
608         case 1:
609                 reg = MSTPCR1;
610                 break;
611         case 2:
612                 reg = MSTPCR2;
613                 break;
614         default:
615                 return -EINVAL;
616         }
617
618         r = ctrl_inl(reg);
619
620         if (enable)
621                 r &= ~(1 << bit);
622         else
623                 r |= (1 << bit);
624
625         ctrl_outl(r, reg);
626         return 0;
627 }
628
629 static int sh7722_mstpcr_enable(struct clk *clk)
630 {
631         return sh7722_mstpcr_start_stop(clk, 1);
632 }
633
634 static void sh7722_mstpcr_disable(struct clk *clk)
635 {
636         sh7722_mstpcr_start_stop(clk, 0);
637 }
638
639 static struct clk_ops sh7722_mstpcr_clk_ops = {
640         .enable = sh7722_mstpcr_enable,
641         .disable = sh7722_mstpcr_disable,
642         .recalc = followparent_recalc,
643 };
644
645 #define MSTPCR(_name, _parent, regnr, bitnr, _flags) \
646 {                                               \
647         .name = _name,                          \
648         .flags = _flags,                        \
649         .arch_flags = MSTPCR_ARCH_FLAGS(regnr, bitnr),  \
650         .ops = (void *)_parent,         \
651 }
652
653 static struct clk sh7722_mstpcr_clocks[] = {
654 #if defined(CONFIG_CPU_SUBTYPE_SH7722)
655         MSTPCR("uram0", "umem_clk", 0, 28, CLK_ENABLE_ON_INIT),
656         MSTPCR("xymem0", "bus_clk", 0, 26, CLK_ENABLE_ON_INIT),
657         MSTPCR("tmu0", "peripheral_clk", 0, 15, 0),
658         MSTPCR("cmt0", "r_clk", 0, 14, 0),
659         MSTPCR("rwdt0", "r_clk", 0, 13, 0),
660         MSTPCR("flctl0", "peripheral_clk", 0, 10, 0),
661         MSTPCR("scif0", "peripheral_clk", 0, 7, 0),
662         MSTPCR("scif1", "peripheral_clk", 0, 6, 0),
663         MSTPCR("scif2", "peripheral_clk", 0, 5, 0),
664         MSTPCR("i2c0", "peripheral_clk", 1, 9, 0),
665         MSTPCR("rtc0", "r_clk", 1, 8, 0),
666         MSTPCR("sdhi0", "peripheral_clk", 2, 18, 0),
667         MSTPCR("keysc0", "r_clk", 2, 14, 0),
668         MSTPCR("usbf0", "peripheral_clk", 2, 11, 0),
669         MSTPCR("2dg0", "bus_clk", 2, 9, 0),
670         MSTPCR("siu0", "bus_clk", 2, 8, 0),
671         MSTPCR("vou0", "bus_clk", 2, 5, 0),
672         MSTPCR("jpu0", "bus_clk", 2, 6, CLK_ENABLE_ON_INIT),
673         MSTPCR("beu0", "bus_clk", 2, 4, 0),
674         MSTPCR("ceu0", "bus_clk", 2, 3, 0),
675         MSTPCR("veu0", "bus_clk", 2, 2, CLK_ENABLE_ON_INIT),
676         MSTPCR("vpu0", "bus_clk", 2, 1, CLK_ENABLE_ON_INIT),
677         MSTPCR("lcdc0", "bus_clk", 2, 0, 0),
678 #endif
679 #if defined(CONFIG_CPU_SUBTYPE_SH7724)
680         /* See Datasheet : Overview -> Block Diagram */
681         MSTPCR("tlb0", "cpu_clk", 0, 31, 0),
682         MSTPCR("ic0", "cpu_clk", 0, 30, 0),
683         MSTPCR("oc0", "cpu_clk", 0, 29, 0),
684         MSTPCR("rs0", "bus_clk", 0, 28, 0),
685         MSTPCR("ilmem0", "cpu_clk", 0, 27, 0),
686         MSTPCR("l2c0", "sh_clk", 0, 26, 0),
687         MSTPCR("fpu0", "cpu_clk", 0, 24, 0),
688         MSTPCR("intc0", "peripheral_clk", 0, 22, 0),
689         MSTPCR("dmac0", "bus_clk", 0, 21, 0),
690         MSTPCR("sh0", "sh_clk", 0, 20, 0),
691         MSTPCR("hudi0", "peripheral_clk", 0, 19, 0),
692         MSTPCR("ubc0", "cpu_clk", 0, 17, 0),
693         MSTPCR("tmu0", "peripheral_clk", 0, 15, 0),
694         MSTPCR("cmt0", "r_clk", 0, 14, 0),
695         MSTPCR("rwdt0", "r_clk", 0, 13, 0),
696         MSTPCR("dmac1", "bus_clk", 0, 12, 0),
697         MSTPCR("tmu1", "peripheral_clk", 0, 10, 0),
698         MSTPCR("scif0", "peripheral_clk", 0, 9, 0),
699         MSTPCR("scif1", "peripheral_clk", 0, 8, 0),
700         MSTPCR("scif2", "peripheral_clk", 0, 7, 0),
701         MSTPCR("scif3", "bus_clk", 0, 6, 0),
702         MSTPCR("scif4", "bus_clk", 0, 5, 0),
703         MSTPCR("scif5", "bus_clk", 0, 4, 0),
704         MSTPCR("msiof0", "bus_clk", 0, 2, 0),
705         MSTPCR("msiof1", "bus_clk", 0, 1, 0),
706         MSTPCR("keysc0", "r_clk", 1, 12, 0),
707         MSTPCR("rtc0", "r_clk", 1, 11, 0),
708         MSTPCR("i2c0", "peripheral_clk", 1, 9, 0),
709         MSTPCR("i2c1", "peripheral_clk", 1, 8, 0),
710         MSTPCR("mmc0", "bus_clk", 2, 29, 0),
711         MSTPCR("eth0", "bus_clk", 2, 28, 0),
712         MSTPCR("atapi0", "bus_clk", 2, 26, 0),
713         MSTPCR("tpu0", "bus_clk", 2, 25, 0),
714         MSTPCR("irda0", "peripheral_clk", 2, 24, 0),
715         MSTPCR("tsif0", "bus_clk", 2, 22, 0),
716         MSTPCR("usb1", "bus_clk", 2, 21, 0),
717         MSTPCR("usb0", "bus_clk", 2, 20, 0),
718         MSTPCR("2dg0", "bus_clk", 2, 19, 0),
719         MSTPCR("sdhi0", "bus_clk", 2, 18, 0),
720         MSTPCR("sdhi1", "bus_clk", 2, 17, 0),
721         MSTPCR("veu1", "bus_clk", 2, 15, CLK_ENABLE_ON_INIT),
722         MSTPCR("ceu1", "bus_clk", 2, 13, 0),
723         MSTPCR("beu1", "bus_clk", 2, 12, 0),
724         MSTPCR("2ddmac0", "sh_clk", 2, 10, 0),
725         MSTPCR("spu0", "bus_clk", 2, 9, 0),
726         MSTPCR("jpu0", "bus_clk", 2, 6, 0),
727         MSTPCR("vou0", "bus_clk", 2, 5, 0),
728         MSTPCR("beu0", "bus_clk", 2, 4, 0),
729         MSTPCR("ceu0", "bus_clk", 2, 3, 0),
730         MSTPCR("veu0", "bus_clk", 2, 2, CLK_ENABLE_ON_INIT),
731         MSTPCR("vpu0", "bus_clk", 2, 1, CLK_ENABLE_ON_INIT),
732         MSTPCR("lcdc0", "bus_clk", 2, 0, 0),
733 #endif
734 #if defined(CONFIG_CPU_SUBTYPE_SH7343)
735         MSTPCR("uram0", "umem_clk", 0, 28, CLK_ENABLE_ON_INIT),
736         MSTPCR("xymem0", "bus_clk", 0, 26, CLK_ENABLE_ON_INIT),
737         MSTPCR("tmu0", "peripheral_clk", 0, 15, 0),
738         MSTPCR("cmt0", "r_clk", 0, 14, 0),
739         MSTPCR("rwdt0", "r_clk", 0, 13, 0),
740         MSTPCR("scif0", "peripheral_clk", 0, 7, 0),
741         MSTPCR("scif1", "peripheral_clk", 0, 6, 0),
742         MSTPCR("scif2", "peripheral_clk", 0, 5, 0),
743         MSTPCR("scif3", "peripheral_clk", 0, 4, 0),
744         MSTPCR("i2c0", "peripheral_clk", 1, 9, 0),
745         MSTPCR("i2c1", "peripheral_clk", 1, 8, 0),
746         MSTPCR("sdhi0", "peripheral_clk", 2, 18, 0),
747         MSTPCR("keysc0", "r_clk", 2, 14, 0),
748         MSTPCR("usbf0", "peripheral_clk", 2, 11, 0),
749         MSTPCR("siu0", "bus_clk", 2, 8, 0),
750         MSTPCR("jpu0", "bus_clk", 2, 6, CLK_ENABLE_ON_INIT),
751         MSTPCR("vou0", "bus_clk", 2, 5, 0),
752         MSTPCR("beu0", "bus_clk", 2, 4, 0),
753         MSTPCR("ceu0", "bus_clk", 2, 3, 0),
754         MSTPCR("veu0", "bus_clk", 2, 2, CLK_ENABLE_ON_INIT),
755         MSTPCR("vpu0", "bus_clk", 2, 1, CLK_ENABLE_ON_INIT),
756         MSTPCR("lcdc0", "bus_clk", 2, 0, 0),
757 #endif
758 #if defined(CONFIG_CPU_SUBTYPE_SH7366)
759         /* See page 52 of Datasheet V0.40: Overview -> Block Diagram */
760         MSTPCR("tlb0", "cpu_clk", 0, 31, 0),
761         MSTPCR("ic0", "cpu_clk", 0, 30, 0),
762         MSTPCR("oc0", "cpu_clk", 0, 29, 0),
763         MSTPCR("rsmem0", "sh_clk", 0, 28, CLK_ENABLE_ON_INIT),
764         MSTPCR("xymem0", "cpu_clk", 0, 26, CLK_ENABLE_ON_INIT),
765         MSTPCR("intc30", "peripheral_clk", 0, 23, 0),
766         MSTPCR("intc0", "peripheral_clk", 0, 22, 0),
767         MSTPCR("dmac0", "bus_clk", 0, 21, 0),
768         MSTPCR("sh0", "sh_clk", 0, 20, 0),
769         MSTPCR("hudi0", "peripheral_clk", 0, 19, 0),
770         MSTPCR("ubc0", "cpu_clk", 0, 17, 0),
771         MSTPCR("tmu0", "peripheral_clk", 0, 15, 0),
772         MSTPCR("cmt0", "r_clk", 0, 14, 0),
773         MSTPCR("rwdt0", "r_clk", 0, 13, 0),
774         MSTPCR("flctl0", "peripheral_clk", 0, 10, 0),
775         MSTPCR("scif0", "peripheral_clk", 0, 7, 0),
776         MSTPCR("scif1", "bus_clk", 0, 6, 0),
777         MSTPCR("scif2", "bus_clk", 0, 5, 0),
778         MSTPCR("msiof0", "peripheral_clk", 0, 2, 0),
779         MSTPCR("sbr0", "peripheral_clk", 0, 1, 0),
780         MSTPCR("i2c0", "peripheral_clk", 1, 9, 0),
781         MSTPCR("icb0", "bus_clk", 2, 27, 0),
782         MSTPCR("meram0", "sh_clk", 2, 26, 0),
783         MSTPCR("dacc0", "peripheral_clk", 2, 24, 0),
784         MSTPCR("dacy0", "peripheral_clk", 2, 23, 0),
785         MSTPCR("tsif0", "bus_clk", 2, 22, 0),
786         MSTPCR("sdhi0", "bus_clk", 2, 18, 0),
787         MSTPCR("mmcif0", "bus_clk", 2, 17, 0),
788         MSTPCR("usb0", "bus_clk", 2, 11, 0),
789         MSTPCR("siu0", "bus_clk", 2, 8, 0),
790         MSTPCR("veu1", "bus_clk", 2, 7, CLK_ENABLE_ON_INIT),
791         MSTPCR("vou0", "bus_clk", 2, 5, 0),
792         MSTPCR("beu0", "bus_clk", 2, 4, 0),
793         MSTPCR("ceu0", "bus_clk", 2, 3, 0),
794         MSTPCR("veu0", "bus_clk", 2, 2, CLK_ENABLE_ON_INIT),
795         MSTPCR("vpu0", "bus_clk", 2, 1, CLK_ENABLE_ON_INIT),
796         MSTPCR("lcdc0", "bus_clk", 2, 0, 0),
797 #endif
798 };
799
800 static struct clk *sh7722_clocks[] = {
801         &sh7722_umem_clock,
802         &sh7722_sh_clock,
803         &sh7722_peripheral_clock,
804         &sh7722_sdram_clock,
805 #if !defined(CONFIG_CPU_SUBTYPE_SH7343) &&\
806     !defined(CONFIG_CPU_SUBTYPE_SH7724)
807         &sh7722_siu_a_clock,
808         &sh7722_siu_b_clock,
809 #endif
810 /* 7724 should support FSI clock */
811 #if defined(CONFIG_CPU_SUBTYPE_SH7722) || \
812     defined(CONFIG_CPU_SUBTYPE_SH7724)
813         &sh7722_irda_clock,
814 #endif
815         &sh7722_video_clock,
816 };
817
818 /*
819  * init in order: master, module, bus, cpu
820  */
821 struct clk_ops *onchip_ops[] = {
822         &sh7722_master_clk_ops,
823         &sh7722_module_clk_ops,
824         &sh7722_frqcr_clk_ops,
825         &sh7722_frqcr_clk_ops,
826 };
827
828 void __init
829 arch_init_clk_ops(struct clk_ops **ops, int type)
830 {
831         BUG_ON(type < 0 || type >= ARRAY_SIZE(onchip_ops));
832         *ops = onchip_ops[type];
833 }
834
835 int __init arch_clk_init(void)
836 {
837         struct clk *clk;
838         int i;
839
840         cpg_clk_init();
841
842         clk = clk_get(NULL, "master_clk");
843         for (i = 0; i < ARRAY_SIZE(sh7722_clocks); i++) {
844                 pr_debug( "Registering clock '%s'\n", sh7722_clocks[i]->name);
845                 sh7722_clocks[i]->parent = clk;
846                 clk_register(sh7722_clocks[i]);
847         }
848         clk_put(clk);
849
850         clk_register(&sh7722_r_clock);
851
852         for (i = 0; i < ARRAY_SIZE(sh7722_mstpcr_clocks); i++) {
853                 pr_debug( "Registering mstpcr clock '%s'\n",
854                           sh7722_mstpcr_clocks[i].name);
855                 clk = clk_get(NULL, (void *) sh7722_mstpcr_clocks[i].ops);
856                 sh7722_mstpcr_clocks[i].parent = clk;
857                 sh7722_mstpcr_clocks[i].ops = &sh7722_mstpcr_clk_ops;
858                 clk_register(&sh7722_mstpcr_clocks[i]);
859                 clk_put(clk);
860         }
861
862         propagate_rate(&sh7722_r_clock); /* make sure rate gets propagated */
863
864         return 0;
865 }