sh: clkfwk: beyond ARRAY_SIZE of onchip_ops for sh7722.
[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, SH7723 & 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 #elif defined(CONFIG_CPU_SUBTYPE_SH7723)
180 #define MASTERDIVS      { 6, 8, 12, 16 }
181 #define STCMASK         0x1f
182 #define DIVCALC(div)    (div-1)
183 #define FRQCRKICK       0x00000000
184 #else
185 #define MASTERDIVS      { 2, 3, 4, 6, 8, 16 }
186 #define STCMASK         0x1f
187 #define DIVCALC(div)    (div-1)
188 #define FRQCRKICK       0x00000000
189 #endif
190
191 static int master_clk_setrate(struct clk *clk, unsigned long rate, int id)
192 {
193         int div = rate / clk->rate;
194         int master_divs[] = MASTERDIVS;
195         int index;
196         unsigned long frqcr;
197
198         for (index = 1; index < ARRAY_SIZE(master_divs); index++)
199                 if (div >= master_divs[index - 1] && div < master_divs[index])
200                         break;
201
202         if (index >= ARRAY_SIZE(master_divs))
203                 index = ARRAY_SIZE(master_divs);
204         div = master_divs[index - 1];
205
206         frqcr = ctrl_inl(FRQCR);
207         frqcr &= ~(STCMASK << 24);
208         frqcr |= (DIVCALC(div) << 24);
209         frqcr |= FRQCRKICK;
210         ctrl_outl(frqcr, FRQCR);
211
212         return 0;
213 }
214
215 static struct clk_ops sh7722_master_clk_ops = {
216         .init = master_clk_init,
217         .recalc = master_clk_recalc,
218         .set_rate = master_clk_setrate,
219 };
220
221 static struct clk_ops sh7722_module_clk_ops = {
222        .recalc = module_clk_recalc,
223 };
224
225 struct frqcr_context {
226         unsigned mask;
227         unsigned shift;
228 };
229
230 struct frqcr_context sh7722_get_clk_context(const char *name)
231 {
232         struct frqcr_context ctx = { 0, };
233
234         if (!strcmp(name, "peripheral_clk")) {
235                 ctx.shift = 0;
236                 ctx.mask = 0xF;
237         } else if (!strcmp(name, "sdram_clk")) {
238                 ctx.shift = 4;
239                 ctx.mask = 0xF;
240         } else if (!strcmp(name, "bus_clk")) {
241                 ctx.shift = 8;
242                 ctx.mask = 0xF;
243         } else if (!strcmp(name, "sh_clk")) {
244                 ctx.shift = 12;
245                 ctx.mask = 0xF;
246         } else if (!strcmp(name, "umem_clk")) {
247                 ctx.shift = 16;
248                 ctx.mask = 0xF;
249         } else if (!strcmp(name, "cpu_clk")) {
250                 ctx.shift = 20;
251                 ctx.mask = 7;
252         }
253         return ctx;
254 }
255
256 /**
257  * sh7722_find_div_index - find divisor for setting rate
258  *
259  * All sh7722 clocks use the same set of multipliers/divisors. This function
260  * chooses correct divisor to set the rate of clock with parent clock that
261  * generates frequency of 'parent_rate'
262  *
263  * @parent_rate: rate of parent clock
264  * @rate: requested rate to be set
265  */
266 static int sh7722_find_div_index(unsigned long parent_rate, unsigned rate)
267 {
268         unsigned div2 = parent_rate * 2 / rate;
269         int index;
270
271         if (rate > parent_rate)
272                 return -EINVAL;
273
274         for (index = 1; index < ARRAY_SIZE(divisors2); index++) {
275                 if (div2 > divisors2[index - 1] && div2 <= divisors2[index])
276                         break;
277         }
278         if (index >= ARRAY_SIZE(divisors2))
279                 index = ARRAY_SIZE(divisors2) - 1;
280         return index;
281 }
282
283 static unsigned long sh7722_frqcr_recalc(struct clk *clk)
284 {
285         struct frqcr_context ctx = sh7722_get_clk_context(clk->name);
286         unsigned long frqcr = ctrl_inl(FRQCR);
287         int index;
288
289         index = (frqcr >> ctx.shift) & ctx.mask;
290         return clk->parent->rate * 2 / divisors2[index];
291 }
292
293 static int sh7722_frqcr_set_rate(struct clk *clk, unsigned long rate,
294                                  int algo_id)
295 {
296         struct frqcr_context ctx = sh7722_get_clk_context(clk->name);
297         unsigned long parent_rate = clk->parent->rate;
298         int div;
299         unsigned long frqcr;
300         int err = 0;
301
302         /* pretty invalid */
303         if (parent_rate < rate)
304                 return -EINVAL;
305
306         /* look for multiplier/divisor pair */
307         div = sh7722_find_div_index(parent_rate, rate);
308         if (div<0)
309                 return div;
310
311         /* calculate new value of clock rate */
312         clk->rate = parent_rate * 2 / divisors2[div];
313         frqcr = ctrl_inl(FRQCR);
314
315         /* FIXME: adjust as algo_id specifies */
316         if (algo_id != NO_CHANGE) {
317                 int originator;
318                 char *algo_group_1[] = { "cpu_clk", "umem_clk", "sh_clk" };
319                 char *algo_group_2[] = { "sh_clk", "bus_clk" };
320                 char *algo_group_3[] = { "sh_clk", "sdram_clk" };
321                 char *algo_group_4[] = { "bus_clk", "peripheral_clk" };
322                 char *algo_group_5[] = { "cpu_clk", "peripheral_clk" };
323                 char **algo_current = NULL;
324                 /* 3 is the maximum number of clocks in relation */
325                 struct clk *ck[3];
326                 unsigned long values[3]; /* the same comment as above */
327                 int part_length = -1;
328                 int i;
329
330                 /*
331                  * all the steps below only required if adjustion was
332                  * requested
333                  */
334                 if (algo_id == IUS_N1_N1 ||
335                     algo_id == IUS_322 ||
336                     algo_id == IUS_522 ||
337                     algo_id == IUS_N11) {
338                         algo_current = algo_group_1;
339                         part_length = 3;
340                 }
341                 if (algo_id == SB_N1) {
342                         algo_current = algo_group_2;
343                         part_length = 2;
344                 }
345                 if (algo_id == SB3_N1 ||
346                     algo_id == SB3_32 ||
347                     algo_id == SB3_43 ||
348                     algo_id == SB3_54) {
349                         algo_current = algo_group_3;
350                         part_length = 2;
351                 }
352                 if (algo_id == BP_N1) {
353                         algo_current = algo_group_4;
354                         part_length = 2;
355                 }
356                 if (algo_id == IP_N1) {
357                         algo_current = algo_group_5;
358                         part_length = 2;
359                 }
360                 if (!algo_current)
361                         goto incorrect_algo_id;
362
363                 originator = -1;
364                 for (i = 0; i < part_length; i ++ ) {
365                         if (originator >= 0 && !strcmp(clk->name,
366                                                        algo_current[i]))
367                                 originator = i;
368                         ck[i] = clk_get(NULL, algo_current[i]);
369                         values[i] = clk_get_rate(ck[i]);
370                 }
371
372                 if (originator >= 0)
373                         adjust_clocks(originator, adjust_algos[algo_id],
374                                       values, part_length);
375
376                 for (i = 0; i < part_length; i ++ ) {
377                         struct frqcr_context part_ctx;
378                         int part_div;
379
380                         if (likely(!err)) {
381                                 part_div = sh7722_find_div_index(parent_rate,
382                                                                 rate);
383                                 if (part_div > 0) {
384                                         part_ctx = sh7722_get_clk_context(
385                                                                 ck[i]->name);
386                                         frqcr &= ~(part_ctx.mask <<
387                                                    part_ctx.shift);
388                                         frqcr |= part_div << part_ctx.shift;
389                                 } else
390                                         err = part_div;
391                         }
392
393                         ck[i]->ops->recalc(ck[i]);
394                         clk_put(ck[i]);
395                 }
396         }
397
398         /* was there any error during recalculation ? If so, bail out.. */
399         if (unlikely(err!=0))
400                 goto out_err;
401
402         /* clear FRQCR bits */
403         frqcr &= ~(ctx.mask << ctx.shift);
404         frqcr |= div << ctx.shift;
405         frqcr |= FRQCRKICK;
406
407         /* ...and perform actual change */
408         ctrl_outl(frqcr, FRQCR);
409         return 0;
410
411 incorrect_algo_id:
412         return -EINVAL;
413 out_err:
414         return err;
415 }
416
417 static long sh7722_frqcr_round_rate(struct clk *clk, unsigned long rate)
418 {
419         unsigned long parent_rate = clk->parent->rate;
420         int div;
421
422         /* look for multiplier/divisor pair */
423         div = sh7722_find_div_index(parent_rate, rate);
424         if (div < 0)
425                 return clk->rate;
426
427         /* calculate new value of clock rate */
428         return parent_rate * 2 / divisors2[div];
429 }
430
431 static struct clk_ops sh7722_frqcr_clk_ops = {
432         .recalc = sh7722_frqcr_recalc,
433         .set_rate = sh7722_frqcr_set_rate,
434         .round_rate = sh7722_frqcr_round_rate,
435 };
436
437 /*
438  * clock ops methods for SIU A/B and IrDA clock
439  */
440 #ifndef CONFIG_CPU_SUBTYPE_SH7343
441 static int sh7722_siu_set_rate(struct clk *clk, unsigned long rate, int algo_id)
442 {
443         unsigned long r;
444         int div;
445
446         r = ctrl_inl(clk->arch_flags);
447         div = sh7722_find_div_index(clk->parent->rate, rate);
448         if (div < 0)
449                 return div;
450         r = (r & ~0xF) | div;
451         ctrl_outl(r, clk->arch_flags);
452         return 0;
453 }
454
455 static unsigned long sh7722_siu_recalc(struct clk *clk)
456 {
457         unsigned long r;
458
459         r = ctrl_inl(clk->arch_flags);
460         return clk->parent->rate * 2 / divisors2[r & 0xF];
461 }
462
463 static int sh7722_siu_start_stop(struct clk *clk, int enable)
464 {
465         unsigned long r;
466
467         r = ctrl_inl(clk->arch_flags);
468         if (enable)
469                 ctrl_outl(r & ~(1 << 8), clk->arch_flags);
470         else
471                 ctrl_outl(r | (1 << 8), clk->arch_flags);
472         return 0;
473 }
474
475 static int sh7722_siu_enable(struct clk *clk)
476 {
477         return sh7722_siu_start_stop(clk, 1);
478 }
479
480 static void sh7722_siu_disable(struct clk *clk)
481 {
482         sh7722_siu_start_stop(clk, 0);
483 }
484
485 static struct clk_ops sh7722_siu_clk_ops = {
486         .recalc = sh7722_siu_recalc,
487         .set_rate = sh7722_siu_set_rate,
488         .enable = sh7722_siu_enable,
489         .disable = sh7722_siu_disable,
490 };
491
492 #endif /* CONFIG_CPU_SUBTYPE_SH7343 */
493
494 static int sh7722_video_enable(struct clk *clk)
495 {
496         unsigned long r;
497
498         r = ctrl_inl(VCLKCR);
499         ctrl_outl( r & ~(1<<8), VCLKCR);
500         return 0;
501 }
502
503 static void sh7722_video_disable(struct clk *clk)
504 {
505         unsigned long r;
506
507         r = ctrl_inl(VCLKCR);
508         ctrl_outl( r | (1<<8), VCLKCR);
509 }
510
511 static int sh7722_video_set_rate(struct clk *clk, unsigned long rate,
512                                  int algo_id)
513 {
514         unsigned long r;
515
516         r = ctrl_inl(VCLKCR);
517         r &= ~0x3F;
518         r |= ((clk->parent->rate / rate - 1) & 0x3F);
519         ctrl_outl(r, VCLKCR);
520         return 0;
521 }
522
523 static unsigned long sh7722_video_recalc(struct clk *clk)
524 {
525         unsigned long r;
526
527         r = ctrl_inl(VCLKCR);
528         return clk->parent->rate / ((r & 0x3F) + 1);
529 }
530
531 static struct clk_ops sh7722_video_clk_ops = {
532         .recalc = sh7722_video_recalc,
533         .set_rate = sh7722_video_set_rate,
534         .enable = sh7722_video_enable,
535         .disable = sh7722_video_disable,
536 };
537 /*
538  * and at last, clock definitions themselves
539  */
540 static struct clk sh7722_umem_clock = {
541         .name = "umem_clk",
542         .ops = &sh7722_frqcr_clk_ops,
543 };
544
545 static struct clk sh7722_sh_clock = {
546         .name = "sh_clk",
547         .ops = &sh7722_frqcr_clk_ops,
548 };
549
550 static struct clk sh7722_peripheral_clock = {
551         .name = "peripheral_clk",
552         .ops = &sh7722_frqcr_clk_ops,
553 };
554
555 static struct clk sh7722_sdram_clock = {
556         .name = "sdram_clk",
557         .ops = &sh7722_frqcr_clk_ops,
558 };
559
560 static struct clk sh7722_r_clock = {
561         .name = "r_clk",
562         .rate = 32768,
563 };
564
565 #if !defined(CONFIG_CPU_SUBTYPE_SH7343) &&\
566     !defined(CONFIG_CPU_SUBTYPE_SH7724)
567 /*
568  * these three clocks - SIU A, SIU B, IrDA - share the same clk_ops
569  * methods of clk_ops determine which register they should access by
570  * examining clk->name field
571  */
572 static struct clk sh7722_siu_a_clock = {
573         .name = "siu_a_clk",
574         .arch_flags = SCLKACR,
575         .ops = &sh7722_siu_clk_ops,
576 };
577
578 static struct clk sh7722_siu_b_clock = {
579         .name = "siu_b_clk",
580         .arch_flags = SCLKBCR,
581         .ops = &sh7722_siu_clk_ops,
582 };
583 #endif /* CONFIG_CPU_SUBTYPE_SH7343, SH7724 */
584
585 #if defined(CONFIG_CPU_SUBTYPE_SH7722) ||\
586     defined(CONFIG_CPU_SUBTYPE_SH7724)
587 static struct clk sh7722_irda_clock = {
588         .name = "irda_clk",
589         .arch_flags = IrDACLKCR,
590         .ops = &sh7722_siu_clk_ops,
591 };
592 #endif
593
594 static struct clk sh7722_video_clock = {
595         .name = "video_clk",
596         .ops = &sh7722_video_clk_ops,
597 };
598
599 #define MSTPCR_ARCH_FLAGS(reg, bit) (((reg) << 8) | (bit))
600 #define MSTPCR_ARCH_FLAGS_REG(value) ((value) >> 8)
601 #define MSTPCR_ARCH_FLAGS_BIT(value) ((value) & 0xff)
602
603 static int sh7722_mstpcr_start_stop(struct clk *clk, int enable)
604 {
605         unsigned long bit = MSTPCR_ARCH_FLAGS_BIT(clk->arch_flags);
606         unsigned long reg;
607         unsigned long r;
608
609         switch(MSTPCR_ARCH_FLAGS_REG(clk->arch_flags)) {
610         case 0:
611                 reg = MSTPCR0;
612                 break;
613         case 1:
614                 reg = MSTPCR1;
615                 break;
616         case 2:
617                 reg = MSTPCR2;
618                 break;
619         default:
620                 return -EINVAL;
621         }
622
623         r = ctrl_inl(reg);
624
625         if (enable)
626                 r &= ~(1 << bit);
627         else
628                 r |= (1 << bit);
629
630         ctrl_outl(r, reg);
631         return 0;
632 }
633
634 static int sh7722_mstpcr_enable(struct clk *clk)
635 {
636         return sh7722_mstpcr_start_stop(clk, 1);
637 }
638
639 static void sh7722_mstpcr_disable(struct clk *clk)
640 {
641         sh7722_mstpcr_start_stop(clk, 0);
642 }
643
644 static struct clk_ops sh7722_mstpcr_clk_ops = {
645         .enable = sh7722_mstpcr_enable,
646         .disable = sh7722_mstpcr_disable,
647         .recalc = followparent_recalc,
648 };
649
650 #define MSTPCR(_name, _parent, regnr, bitnr, _flags) \
651 {                                               \
652         .name = _name,                          \
653         .flags = _flags,                        \
654         .arch_flags = MSTPCR_ARCH_FLAGS(regnr, bitnr),  \
655         .ops = (void *)_parent,         \
656 }
657
658 static struct clk sh7722_mstpcr_clocks[] = {
659 #if defined(CONFIG_CPU_SUBTYPE_SH7722)
660         MSTPCR("uram0", "umem_clk", 0, 28, CLK_ENABLE_ON_INIT),
661         MSTPCR("xymem0", "bus_clk", 0, 26, CLK_ENABLE_ON_INIT),
662         MSTPCR("tmu0", "peripheral_clk", 0, 15, 0),
663         MSTPCR("cmt0", "r_clk", 0, 14, 0),
664         MSTPCR("rwdt0", "r_clk", 0, 13, 0),
665         MSTPCR("flctl0", "peripheral_clk", 0, 10, 0),
666         MSTPCR("scif0", "peripheral_clk", 0, 7, 0),
667         MSTPCR("scif1", "peripheral_clk", 0, 6, 0),
668         MSTPCR("scif2", "peripheral_clk", 0, 5, 0),
669         MSTPCR("i2c0", "peripheral_clk", 1, 9, 0),
670         MSTPCR("rtc0", "r_clk", 1, 8, 0),
671         MSTPCR("sdhi0", "peripheral_clk", 2, 18, 0),
672         MSTPCR("keysc0", "r_clk", 2, 14, 0),
673         MSTPCR("usbf0", "peripheral_clk", 2, 11, 0),
674         MSTPCR("2dg0", "bus_clk", 2, 9, 0),
675         MSTPCR("siu0", "bus_clk", 2, 8, 0),
676         MSTPCR("vou0", "bus_clk", 2, 5, 0),
677         MSTPCR("jpu0", "bus_clk", 2, 6, CLK_ENABLE_ON_INIT),
678         MSTPCR("beu0", "bus_clk", 2, 4, 0),
679         MSTPCR("ceu0", "bus_clk", 2, 3, 0),
680         MSTPCR("veu0", "bus_clk", 2, 2, CLK_ENABLE_ON_INIT),
681         MSTPCR("vpu0", "bus_clk", 2, 1, CLK_ENABLE_ON_INIT),
682         MSTPCR("lcdc0", "bus_clk", 2, 0, 0),
683 #endif
684 #if defined(CONFIG_CPU_SUBTYPE_SH7723)
685         /* See page 60 of Datasheet V1.0: Overview -> Block Diagram */
686         MSTPCR("tlb0", "cpu_clk", 0, 31, 0),
687         MSTPCR("ic0", "cpu_clk", 0, 30, 0),
688         MSTPCR("oc0", "cpu_clk", 0, 29, 0),
689         MSTPCR("l2c0", "sh_clk", 0, 28, 0),
690         MSTPCR("ilmem0", "cpu_clk", 0, 27, 0),
691         MSTPCR("fpu0", "cpu_clk", 0, 24, 0),
692         MSTPCR("intc0", "cpu_clk", 0, 22, 0),
693         MSTPCR("dmac0", "bus_clk", 0, 21, 0),
694         MSTPCR("sh0", "sh_clk", 0, 20, 0),
695         MSTPCR("hudi0", "peripheral_clk", 0, 19, 0),
696         MSTPCR("ubc0", "cpu_clk", 0, 17, 0),
697         MSTPCR("tmu0", "peripheral_clk", 0, 15, 0),
698         MSTPCR("cmt0", "r_clk", 0, 14, 0),
699         MSTPCR("rwdt0", "r_clk", 0, 13, 0),
700         MSTPCR("dmac1", "bus_clk", 0, 12, 0),
701         MSTPCR("tmu1", "peripheral_clk", 0, 11, 0),
702         MSTPCR("flctl0", "peripheral_clk", 0, 10, 0),
703         MSTPCR("scif0", "peripheral_clk", 0, 9, 0),
704         MSTPCR("scif1", "peripheral_clk", 0, 8, 0),
705         MSTPCR("scif2", "peripheral_clk", 0, 7, 0),
706         MSTPCR("scif3", "bus_clk", 0, 6, 0),
707         MSTPCR("scif4", "bus_clk", 0, 5, 0),
708         MSTPCR("scif5", "bus_clk", 0, 4, 0),
709         MSTPCR("msiof0", "bus_clk", 0, 2, 0),
710         MSTPCR("msiof1", "bus_clk", 0, 1, 0),
711         MSTPCR("meram0", "sh_clk", 0, 0, CLK_ENABLE_ON_INIT),
712         MSTPCR("i2c0", "peripheral_clk", 1, 9, 0),
713         MSTPCR("rtc0", "r_clk", 1, 8, 0),
714         MSTPCR("atapi0", "sh_clk", 2, 28, 0),
715         MSTPCR("adc0", "peripheral_clk", 2, 28, 0),
716         MSTPCR("tpu0", "bus_clk", 2, 25, 0),
717         MSTPCR("irda0", "peripheral_clk", 2, 24, 0),
718         MSTPCR("tsif0", "bus_clk", 2, 22, 0),
719         MSTPCR("icb0", "bus_clk", 2, 21, 0),
720         MSTPCR("sdhi0", "bus_clk", 2, 18, 0),
721         MSTPCR("sdhi1", "bus_clk", 2, 17, 0),
722         MSTPCR("keysc0", "r_clk", 2, 14, 0),
723         MSTPCR("usb0", "bus_clk", 2, 11, 0),
724         MSTPCR("2dg0", "bus_clk", 2, 10, 0),
725         MSTPCR("siu0", "bus_clk", 2, 8, 0),
726         MSTPCR("veu1", "bus_clk", 2, 6, CLK_ENABLE_ON_INIT),
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_SH7724)
735         /* See Datasheet : Overview -> Block Diagram */
736         MSTPCR("tlb0", "cpu_clk", 0, 31, 0),
737         MSTPCR("ic0", "cpu_clk", 0, 30, 0),
738         MSTPCR("oc0", "cpu_clk", 0, 29, 0),
739         MSTPCR("rs0", "bus_clk", 0, 28, 0),
740         MSTPCR("ilmem0", "cpu_clk", 0, 27, 0),
741         MSTPCR("l2c0", "sh_clk", 0, 26, 0),
742         MSTPCR("fpu0", "cpu_clk", 0, 24, 0),
743         MSTPCR("intc0", "peripheral_clk", 0, 22, 0),
744         MSTPCR("dmac0", "bus_clk", 0, 21, 0),
745         MSTPCR("sh0", "sh_clk", 0, 20, 0),
746         MSTPCR("hudi0", "peripheral_clk", 0, 19, 0),
747         MSTPCR("ubc0", "cpu_clk", 0, 17, 0),
748         MSTPCR("tmu0", "peripheral_clk", 0, 15, 0),
749         MSTPCR("cmt0", "r_clk", 0, 14, 0),
750         MSTPCR("rwdt0", "r_clk", 0, 13, 0),
751         MSTPCR("dmac1", "bus_clk", 0, 12, 0),
752         MSTPCR("tmu1", "peripheral_clk", 0, 10, 0),
753         MSTPCR("scif0", "peripheral_clk", 0, 9, 0),
754         MSTPCR("scif1", "peripheral_clk", 0, 8, 0),
755         MSTPCR("scif2", "peripheral_clk", 0, 7, 0),
756         MSTPCR("scif3", "bus_clk", 0, 6, 0),
757         MSTPCR("scif4", "bus_clk", 0, 5, 0),
758         MSTPCR("scif5", "bus_clk", 0, 4, 0),
759         MSTPCR("msiof0", "bus_clk", 0, 2, 0),
760         MSTPCR("msiof1", "bus_clk", 0, 1, 0),
761         MSTPCR("keysc0", "r_clk", 1, 12, 0),
762         MSTPCR("rtc0", "r_clk", 1, 11, 0),
763         MSTPCR("i2c0", "peripheral_clk", 1, 9, 0),
764         MSTPCR("i2c1", "peripheral_clk", 1, 8, 0),
765         MSTPCR("mmc0", "bus_clk", 2, 29, 0),
766         MSTPCR("eth0", "bus_clk", 2, 28, 0),
767         MSTPCR("atapi0", "bus_clk", 2, 26, 0),
768         MSTPCR("tpu0", "bus_clk", 2, 25, 0),
769         MSTPCR("irda0", "peripheral_clk", 2, 24, 0),
770         MSTPCR("tsif0", "bus_clk", 2, 22, 0),
771         MSTPCR("usb1", "bus_clk", 2, 21, 0),
772         MSTPCR("usb0", "bus_clk", 2, 20, 0),
773         MSTPCR("2dg0", "bus_clk", 2, 19, 0),
774         MSTPCR("sdhi0", "bus_clk", 2, 18, 0),
775         MSTPCR("sdhi1", "bus_clk", 2, 17, 0),
776         MSTPCR("veu1", "bus_clk", 2, 15, CLK_ENABLE_ON_INIT),
777         MSTPCR("ceu1", "bus_clk", 2, 13, 0),
778         MSTPCR("beu1", "bus_clk", 2, 12, 0),
779         MSTPCR("2ddmac0", "sh_clk", 2, 10, 0),
780         MSTPCR("spu0", "bus_clk", 2, 9, 0),
781         MSTPCR("jpu0", "bus_clk", 2, 6, 0),
782         MSTPCR("vou0", "bus_clk", 2, 5, 0),
783         MSTPCR("beu0", "bus_clk", 2, 4, 0),
784         MSTPCR("ceu0", "bus_clk", 2, 3, 0),
785         MSTPCR("veu0", "bus_clk", 2, 2, CLK_ENABLE_ON_INIT),
786         MSTPCR("vpu0", "bus_clk", 2, 1, CLK_ENABLE_ON_INIT),
787         MSTPCR("lcdc0", "bus_clk", 2, 0, 0),
788 #endif
789 #if defined(CONFIG_CPU_SUBTYPE_SH7343)
790         MSTPCR("uram0", "umem_clk", 0, 28, CLK_ENABLE_ON_INIT),
791         MSTPCR("xymem0", "bus_clk", 0, 26, CLK_ENABLE_ON_INIT),
792         MSTPCR("tmu0", "peripheral_clk", 0, 15, 0),
793         MSTPCR("cmt0", "r_clk", 0, 14, 0),
794         MSTPCR("rwdt0", "r_clk", 0, 13, 0),
795         MSTPCR("scif0", "peripheral_clk", 0, 7, 0),
796         MSTPCR("scif1", "peripheral_clk", 0, 6, 0),
797         MSTPCR("scif2", "peripheral_clk", 0, 5, 0),
798         MSTPCR("scif3", "peripheral_clk", 0, 4, 0),
799         MSTPCR("i2c0", "peripheral_clk", 1, 9, 0),
800         MSTPCR("i2c1", "peripheral_clk", 1, 8, 0),
801         MSTPCR("sdhi0", "peripheral_clk", 2, 18, 0),
802         MSTPCR("keysc0", "r_clk", 2, 14, 0),
803         MSTPCR("usbf0", "peripheral_clk", 2, 11, 0),
804         MSTPCR("siu0", "bus_clk", 2, 8, 0),
805         MSTPCR("jpu0", "bus_clk", 2, 6, CLK_ENABLE_ON_INIT),
806         MSTPCR("vou0", "bus_clk", 2, 5, 0),
807         MSTPCR("beu0", "bus_clk", 2, 4, 0),
808         MSTPCR("ceu0", "bus_clk", 2, 3, 0),
809         MSTPCR("veu0", "bus_clk", 2, 2, CLK_ENABLE_ON_INIT),
810         MSTPCR("vpu0", "bus_clk", 2, 1, CLK_ENABLE_ON_INIT),
811         MSTPCR("lcdc0", "bus_clk", 2, 0, 0),
812 #endif
813 #if defined(CONFIG_CPU_SUBTYPE_SH7366)
814         /* See page 52 of Datasheet V0.40: Overview -> Block Diagram */
815         MSTPCR("tlb0", "cpu_clk", 0, 31, 0),
816         MSTPCR("ic0", "cpu_clk", 0, 30, 0),
817         MSTPCR("oc0", "cpu_clk", 0, 29, 0),
818         MSTPCR("rsmem0", "sh_clk", 0, 28, CLK_ENABLE_ON_INIT),
819         MSTPCR("xymem0", "cpu_clk", 0, 26, CLK_ENABLE_ON_INIT),
820         MSTPCR("intc30", "peripheral_clk", 0, 23, 0),
821         MSTPCR("intc0", "peripheral_clk", 0, 22, 0),
822         MSTPCR("dmac0", "bus_clk", 0, 21, 0),
823         MSTPCR("sh0", "sh_clk", 0, 20, 0),
824         MSTPCR("hudi0", "peripheral_clk", 0, 19, 0),
825         MSTPCR("ubc0", "cpu_clk", 0, 17, 0),
826         MSTPCR("tmu0", "peripheral_clk", 0, 15, 0),
827         MSTPCR("cmt0", "r_clk", 0, 14, 0),
828         MSTPCR("rwdt0", "r_clk", 0, 13, 0),
829         MSTPCR("flctl0", "peripheral_clk", 0, 10, 0),
830         MSTPCR("scif0", "peripheral_clk", 0, 7, 0),
831         MSTPCR("scif1", "bus_clk", 0, 6, 0),
832         MSTPCR("scif2", "bus_clk", 0, 5, 0),
833         MSTPCR("msiof0", "peripheral_clk", 0, 2, 0),
834         MSTPCR("sbr0", "peripheral_clk", 0, 1, 0),
835         MSTPCR("i2c0", "peripheral_clk", 1, 9, 0),
836         MSTPCR("icb0", "bus_clk", 2, 27, 0),
837         MSTPCR("meram0", "sh_clk", 2, 26, 0),
838         MSTPCR("dacc0", "peripheral_clk", 2, 24, 0),
839         MSTPCR("dacy0", "peripheral_clk", 2, 23, 0),
840         MSTPCR("tsif0", "bus_clk", 2, 22, 0),
841         MSTPCR("sdhi0", "bus_clk", 2, 18, 0),
842         MSTPCR("mmcif0", "bus_clk", 2, 17, 0),
843         MSTPCR("usb0", "bus_clk", 2, 11, 0),
844         MSTPCR("siu0", "bus_clk", 2, 8, 0),
845         MSTPCR("veu1", "bus_clk", 2, 7, CLK_ENABLE_ON_INIT),
846         MSTPCR("vou0", "bus_clk", 2, 5, 0),
847         MSTPCR("beu0", "bus_clk", 2, 4, 0),
848         MSTPCR("ceu0", "bus_clk", 2, 3, 0),
849         MSTPCR("veu0", "bus_clk", 2, 2, CLK_ENABLE_ON_INIT),
850         MSTPCR("vpu0", "bus_clk", 2, 1, CLK_ENABLE_ON_INIT),
851         MSTPCR("lcdc0", "bus_clk", 2, 0, 0),
852 #endif
853 };
854
855 static struct clk *sh7722_clocks[] = {
856         &sh7722_umem_clock,
857         &sh7722_sh_clock,
858         &sh7722_peripheral_clock,
859         &sh7722_sdram_clock,
860 #if !defined(CONFIG_CPU_SUBTYPE_SH7343) &&\
861     !defined(CONFIG_CPU_SUBTYPE_SH7724)
862         &sh7722_siu_a_clock,
863         &sh7722_siu_b_clock,
864 #endif
865 /* 7724 should support FSI clock */
866 #if defined(CONFIG_CPU_SUBTYPE_SH7722) || \
867     defined(CONFIG_CPU_SUBTYPE_SH7724)
868         &sh7722_irda_clock,
869 #endif
870         &sh7722_video_clock,
871 };
872
873 /*
874  * init in order: master, module, bus, cpu
875  */
876 struct clk_ops *onchip_ops[] = {
877         &sh7722_master_clk_ops,
878         &sh7722_module_clk_ops,
879         &sh7722_frqcr_clk_ops,
880         &sh7722_frqcr_clk_ops,
881 };
882
883 void __init
884 arch_init_clk_ops(struct clk_ops **ops, int type)
885 {
886         BUG_ON(type < 0 || type >= ARRAY_SIZE(onchip_ops));
887         *ops = onchip_ops[type];
888 }
889
890 int __init arch_clk_init(void)
891 {
892         struct clk *clk;
893         int i;
894
895         cpg_clk_init();
896
897         clk = clk_get(NULL, "master_clk");
898         for (i = 0; i < ARRAY_SIZE(sh7722_clocks); i++) {
899                 pr_debug( "Registering clock '%s'\n", sh7722_clocks[i]->name);
900                 sh7722_clocks[i]->parent = clk;
901                 clk_register(sh7722_clocks[i]);
902         }
903         clk_put(clk);
904
905         clk_register(&sh7722_r_clock);
906
907         for (i = 0; i < ARRAY_SIZE(sh7722_mstpcr_clocks); i++) {
908                 pr_debug( "Registering mstpcr clock '%s'\n",
909                           sh7722_mstpcr_clocks[i].name);
910                 clk = clk_get(NULL, (void *) sh7722_mstpcr_clocks[i].ops);
911                 sh7722_mstpcr_clocks[i].parent = clk;
912                 sh7722_mstpcr_clocks[i].ops = &sh7722_mstpcr_clk_ops;
913                 clk_register(&sh7722_mstpcr_clocks[i]);
914                 clk_put(clk);
915         }
916
917         propagate_rate(&sh7722_r_clock); /* make sure rate gets propagated */
918
919         return 0;
920 }