rk3188: force dclk_lcdc0/1 even div
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-rk3188 / clock_data.c
1 /* linux/arch/arm/mach-rk30/clock_data.c
2  *
3  * Copyright (C) 2012 ROCKCHIP, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 #include <linux/clk.h>
16 #include <linux/clkdev.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/io.h>
22 #include <linux/delay.h>
23 #include <linux/hardirq.h>
24 #include <mach/cru.h>
25 #include <mach/iomux.h>
26 #include <mach/clock.h>
27 #include <mach/pmu.h>
28 #include <mach/dvfs.h>
29 #include <mach/ddr.h>
30
31 #define MHZ                     (1000UL * 1000UL)
32 #define KHZ                     (1000UL)
33 #define CLK_LOOPS_JIFFY_REF 11996091ULL
34 #define CLK_LOOPS_RATE_REF (1200UL) //Mhz
35 #define CLK_LOOPS_RECALC(new_rate)  div_u64(CLK_LOOPS_JIFFY_REF*(new_rate),CLK_LOOPS_RATE_REF*MHZ)
36 void rk30_clk_dump_regs(void);
37
38 //flags bit
39 //has extern 27mhz
40 #define CLK_FLG_EXT_27MHZ                       (1<<0)
41 //max i2s rate
42 #define CLK_FLG_MAX_I2S_12288KHZ        (1<<1)
43 #define CLK_FLG_MAX_I2S_22579_2KHZ      (1<<2)
44 #define CLK_FLG_MAX_I2S_24576KHZ        (1<<3)
45 #define CLK_FLG_MAX_I2S_49152KHZ        (1<<4)
46 //uart 1m\3m
47 #define CLK_FLG_UART_1_3M                       (1<<5)
48
49 #define ARCH_RK31
50
51 struct apll_clk_set {
52         unsigned long   rate;
53         u32     pllcon0;
54         u32     pllcon1;
55         u32     pllcon2; //nb=bwadj+1;0:11;nb=nf/2
56         u32     rst_dly;//us
57         u32     clksel0;
58         u32     clksel1;
59         unsigned long   lpj;
60 };
61 struct pll_clk_set {
62         unsigned long   rate;
63         u32     pllcon0;
64         u32     pllcon1;
65         u32     pllcon2; //nb=bwadj+1;0:11;nb=nf/2
66         unsigned long   rst_dly;//us
67 };
68
69 #define SET_PLL_DATA(_pll_id,_table) \
70 {\
71         .id=(_pll_id),\
72         .table=(_table),\
73 }
74
75
76 #define _PLL_SET_CLKS(_mhz, nr, nf, no) \
77 { \
78         .rate   = (_mhz) * KHZ, \
79         .pllcon0 = PLL_CLKR_SET(nr)|PLL_CLKOD_SET(no), \
80         .pllcon1 = PLL_CLKF_SET(nf),\
81         .rst_dly=((nr*500)/24+1),\
82 }
83
84
85 #define _APLL_SET_LPJ(_mhz) \
86         .lpj= (CLK_LOOPS_JIFFY_REF * _mhz)/CLK_LOOPS_RATE_REF
87
88
89 #define _APLL_SET_CLKS(_mhz, nr, nf, no, _periph_div, _axi_core_div,\
90                 _axi_div,_ahb_div, _apb_div,_ahb2apb) \
91 { \
92         .rate   = _mhz * MHZ, \
93         .pllcon0 = PLL_CLKR_SET(nr) | PLL_CLKOD_SET(no), \
94         .pllcon1 = PLL_CLKF_SET(nf),\
95         .clksel0 = CORE_PERIPH_W_MSK | CORE_PERIPH_##_periph_div,\
96         .clksel1 = CORE_ACLK_W_MSK | CORE_ACLK_##_axi_core_div,\
97         _APLL_SET_LPJ(_mhz),\
98         .rst_dly=((nr*500)/24+1),\
99 }
100
101 #define CRU_DIV_SET(mask,shift,max) \
102         .div_mask=(mask),\
103 .div_shift=(shift),\
104 .div_max=(max)
105
106
107 #define CRU_SRC_SET(mask,shift ) \
108         .src_mask=(mask),\
109 .src_shift=(shift)
110
111 #define CRU_PARENTS_SET(parents_array) \
112         .parents=(parents_array),\
113 .parents_num=ARRAY_SIZE((parents_array))
114
115 #define CRU_GATE_MODE_SET(_func,_IDX) \
116         .mode=_func,\
117 .gate_idx=(_IDX)
118
119 struct clk_src_sel {
120         struct clk      *parent;
121         u8      value;//crt bit
122         u8      flag;
123         //selgate
124 };
125
126 #define GATE_CLK(NAME,PARENT,ID) \
127         static struct clk clk_##NAME = { \
128                 .name           = #NAME, \
129                 .parent         = &PARENT, \
130                 .mode           = gate_mode, \
131                 .gate_idx       = CLK_GATE_##ID, \
132         }
133 #ifdef RK30_CLK_OFFBOARD_TEST
134 u32 TEST_GRF_REG[0x240];
135 u32 TEST_CRU_REG[0x240];
136 #define cru_readl(offset)       (TEST_CRU_REG[offset/4])
137
138 u32 cru_writel_is_pr(u32 offset)
139 {
140         return (offset == 0x4000);
141 }
142 void cru_writel(u32 v, u32 offset)
143 {
144
145         u32 mask_v = v >> 16;
146         TEST_CRU_REG[offset/4] &= (~mask_v);
147
148         v &= (mask_v);
149
150         TEST_CRU_REG[offset/4] |= v;
151         TEST_CRU_REG[offset/4] &= 0x0000ffff;
152
153         if(cru_writel_is_pr(offset)) {
154                 CLKDATA_DBG("cru w offset=%d,set=%x,reg=%x\n", offset, v, TEST_CRU_REG[offset/4]);
155
156         }
157
158 }
159 void cru_writel_i2s(u32 v, u32 offset)
160 {
161         TEST_CRU_REG[offset/4] = v;
162 }
163 #define cru_writel_frac(v,offset) cru_writel_i2s((v),(offset))
164
165 #define regfile_readl(offset)   (0xffffffff)
166 //#define pmu_readl(offset)        readl(RK30_GRF_BASE + offset)
167 void rk30_clkdev_add(struct clk_lookup *cl);
168 #else
169 #define regfile_readl(offset)   readl_relaxed(RK30_GRF_BASE + offset)
170 #define regfile_writel(v, offset) do { writel_relaxed(v, RK30_GRF_BASE + offset); dsb(); } while (0)
171 #define cru_readl(offset)       readl_relaxed(RK30_CRU_BASE + offset)
172 #define cru_writel(v, offset)   do { writel_relaxed(v, RK30_CRU_BASE + offset); dsb(); } while (0)
173
174 #define cru_writel_frac(v,offset) cru_writel((v),(offset))
175 #endif
176
177 //#define DEBUG
178 #ifdef DEBUG
179 #define CLKDATA_DBG(fmt, args...) printk(KERN_DEBUG "CLKDATA_DBG:\t"fmt, ##args)
180 #define CLKDATA_LOG(fmt, args...) printk(KERN_INFO "CLKDATA_LOG:\t"fmt, ##args)
181 //#define CLKDATA_DBG(fmt, args...) printk("CLKDATA_DBG:\t"fmt, ##args)
182 //#define CLKDATA_LOG(fmt, args...) printk("CLKDATA_LOG:\t"fmt, ##args)
183 #else
184 #define CLKDATA_DBG(fmt, args...) do {} while(0)
185 #define CLKDATA_LOG(fmt, args...) do {} while(0)
186 #endif
187 #define CLKDATA_ERR(fmt, args...) printk(KERN_ERR "CLKDATA_ERR:\t"fmt, ##args)
188 #define CLKDATA_WARNNING(fmt, args...) printk("CLKDATA_WANNING:\t"fmt, ##args)
189
190
191 #define get_cru_bits(con,mask,shift)\
192         ((cru_readl((con)) >> (shift)) & (mask))
193
194 #define set_cru_bits_w_msk(val,mask,shift,con)\
195         cru_writel(((mask)<<(shift+16))|((val)<<(shift)),(con))
196
197
198 #define PLLS_IN_NORM(pll_id) (((cru_readl(CRU_MODE_CON)&PLL_MODE_MSK(pll_id))==(PLL_MODE_NORM(pll_id)&PLL_MODE_MSK(pll_id)))\
199                 &&!(cru_readl(PLL_CONS(pll_id,3))&PLL_BYPASS))
200
201
202 static u32 rk30_clock_flags = 0;
203 static struct clk codec_pll_clk;
204 static struct clk general_pll_clk;
205 static struct clk arm_pll_clk;
206 static unsigned long lpj_gpll;
207 static unsigned int __initdata armclk = 504 * MHZ;
208
209
210 /************************clk recalc div rate*********************************/
211
212 //for free div
213 static unsigned long clksel_recalc_div(struct clk *clk)
214 {
215         u32 div = get_cru_bits(clk->clksel_con, clk->div_mask, clk->div_shift) + 1;
216
217         unsigned long rate = clk->parent->rate / div;
218         pr_debug("%s new clock rate is %lu (div %u)\n", clk->name, rate, div);
219         return rate;
220 }
221
222 //for div 1 2 4 2^n
223 static unsigned long clksel_recalc_shift(struct clk *clk)
224 {
225         u32 shift = get_cru_bits(clk->clksel_con, clk->div_mask, clk->div_shift);
226         unsigned long rate = clk->parent->rate >> shift;
227         pr_debug("%s new clock rate is %lu (shift %u)\n", clk->name, rate, shift);
228         return rate;
229 }
230
231
232 static unsigned long clksel_recalc_shift_2(struct clk *clk)
233 {
234         u32 shift = get_cru_bits(clk->clksel_con, clk->div_mask, clk->div_shift) + 1;
235         unsigned long rate = clk->parent->rate >> shift;
236         pr_debug("%s new clock rate is %lu (shift %u)\n", clk->name, rate, shift);
237         return rate;
238 }
239
240 static unsigned long clksel_recalc_parent_rate(struct clk *clk)
241 {
242         unsigned long rate = clk->parent->rate;
243         pr_debug("%s new clock rate is %lu\n", clk->name, rate);
244         return rate;
245 }
246 /********************************set div rate***********************************/
247
248 //for free div
249 static int clksel_set_rate_freediv(struct clk *clk, unsigned long rate)
250 {
251         u32 div;
252         for (div = 0; div < clk->div_max; div++) {
253                 u32 new_rate = clk->parent->rate / (div + 1);
254                 if (new_rate <= rate) {
255                         set_cru_bits_w_msk(div, clk->div_mask, clk->div_shift, clk->clksel_con);
256                         //clk->rate = new_rate;
257                         pr_debug("clksel_set_rate_freediv for clock %s to rate %ld (div %d)\n", 
258                                         clk->name, rate, div + 1);
259                         return 0;
260                 }
261         }
262         return -ENOENT;
263 }
264
265 //for div 1 2 4 2^n
266 static int clksel_set_rate_shift(struct clk *clk, unsigned long rate)
267 {
268         u32 shift;
269         for (shift = 0; (1 << shift) < clk->div_max; shift++) {
270                 u32 new_rate = clk->parent->rate >> shift;
271                 if (new_rate <= rate) {
272                         set_cru_bits_w_msk(shift, clk->div_mask, clk->div_shift, clk->clksel_con);
273                         clk->rate = new_rate;
274                         pr_debug("clksel_set_rate_shift for clock %s to rate %ld (shift %d)\n", 
275                                         clk->name, rate, shift);
276                         return 0;
277                 }
278         }
279         return -ENOENT;
280 }
281
282 //for div 2 4 2^n
283 static int clksel_set_rate_shift_2(struct clk *clk, unsigned long rate)
284 {
285         u32 shift;
286
287         for (shift = 1; (1 << shift) < clk->div_max; shift++) {
288                 u32 new_rate = clk->parent->rate >> shift;
289                 if (new_rate <= rate) {
290                         set_cru_bits_w_msk(shift - 1, clk->div_mask, clk->div_shift, clk->clksel_con);
291                         clk->rate = new_rate;
292                         pr_debug("clksel_set_rate_shift for clock %s to rate %ld (shift %d)\n", 
293                                         clk->name, rate, shift);
294                         return 0;
295                 }
296         }
297         return -ENOENT;
298 }
299
300 //for div 1 2 4 2*n
301 static int clksel_set_rate_even(struct clk *clk, unsigned long rate)
302 {
303         u32 div;
304         for (div = 2; div < clk->div_max; div += 2) {
305                 u32 new_rate = clk->parent->rate / div;
306                 if (new_rate <= rate) {
307                         set_cru_bits_w_msk(div - 1, clk->div_mask, clk->div_shift, clk->clksel_con);
308                         clk->rate = new_rate;
309                         pr_debug("%s for clock %s to rate %ld (even div = %d)\n", 
310                                         __func__, clk->name, rate, div);
311                         return 0;
312                 }
313         }
314         return -ENOENT;
315 }
316 static u32 clk_get_freediv(unsigned long rate_out, unsigned long rate , u32 div_max)
317 {
318         u32 div;
319         unsigned long new_rate;
320         for (div = 0; div < div_max; div++) {
321                 new_rate = rate / (div + 1);
322                 if (new_rate <= rate_out) {
323                         return div + 1;
324                 }
325         }
326         return div_max ? div_max : 1;
327 }
328 static u32 clk_get_evendiv(unsigned long rate_out, unsigned long rate , u32 div_max)
329 {
330         u32 div;
331         unsigned long new_rate;
332         for (div = 1; div < div_max; div += 2) {
333                 new_rate = rate / (div + 1);
334                 if (new_rate <= rate_out) {
335                         return div + 1;
336                 }
337         }
338         return div_max ? div_max : 1;
339 }
340 struct clk *get_freediv_parents_div(struct clk *clk, unsigned long rate, u32 *div_out) {
341         u32 div[2] = {0, 0};
342         unsigned long new_rate[2] = {0, 0};
343         u32 i;
344
345         if(clk->rate == rate)
346                 return clk->parent;
347         for(i = 0; i < 2; i++) {
348                 div[i] = clk_get_freediv(rate, clk->parents[i]->rate, clk->div_max);
349                 new_rate[i] = clk->parents[i]->rate / div[i];
350                 if(new_rate[i] == rate) {
351                         *div_out = div[i];
352                         return clk->parents[i];
353                 }
354         }
355         if(new_rate[0] < new_rate[1])
356                 i = 1;
357         else
358                 i = 0;
359         *div_out = div[i];
360         return clk->parents[i];
361 }
362 struct clk *get_evendiv_parents_div(struct clk *clk, unsigned long rate, u32 *div_out) {
363         u32 div[2] = {0, 0};
364         unsigned long new_rate[2] = {0, 0};
365         u32 i;
366
367         if(clk->rate == rate)
368                 return clk->parent;
369         for(i = 0; i < 2; i++) {
370                 div[i] = clk_get_evendiv(rate, clk->parents[i]->rate, clk->div_max);
371                 new_rate[i] = clk->parents[i]->rate / div[i];
372                 if(new_rate[i] == rate) {
373                         *div_out = div[i];
374                         return clk->parents[i];
375                 }
376         }
377         if(new_rate[0] < new_rate[1])
378                 i = 1;
379         else
380                 i = 0;
381         *div_out = div[i];
382         return clk->parents[i];
383 }
384
385 static int clkset_rate_freediv_autosel_parents(struct clk *clk, unsigned long rate)
386 {
387         struct clk *p_clk;
388         u32 div, old_div;
389         int ret = 0;
390         if(clk->rate == rate)
391                 return 0;
392         p_clk = get_freediv_parents_div(clk, rate, &div);
393
394         if(!p_clk)
395                 return -ENOENT;
396
397         CLKDATA_DBG("%s %lu,form %s\n", clk->name, rate, p_clk->name);
398         if (clk->parent != p_clk) {
399                 old_div = CRU_GET_REG_BITS_VAL(cru_readl(clk->clksel_con), clk->div_shift, clk->div_mask) + 1;
400
401                 if(div > old_div) {
402                         set_cru_bits_w_msk(div - 1, clk->div_mask, clk->div_shift, clk->clksel_con);
403                 }
404                 ret = clk_set_parent_nolock(clk, p_clk);
405                 if(ret) {
406                         CLKDATA_ERR("%s can't set %lu,reparent err\n", clk->name, rate);
407                         return -ENOENT;
408                 }
409         }
410         //set div
411         set_cru_bits_w_msk(div - 1, clk->div_mask, clk->div_shift, clk->clksel_con);
412         return 0;
413 }
414 static int clkset_rate_evendiv_autosel_parents(struct clk *clk, unsigned long rate)
415 {
416         struct clk *p_clk;
417         u32 div, old_div;
418         int ret = 0;
419         if(clk->rate == rate)
420                 return 0;
421         p_clk = get_evendiv_parents_div(clk, rate, &div);
422
423         if(!p_clk)
424                 return -ENOENT;
425
426         CLKDATA_DBG("%s %lu,form %s\n", clk->name, rate, p_clk->name);
427         if (clk->parent != p_clk) {
428                 old_div = CRU_GET_REG_BITS_VAL(cru_readl(clk->clksel_con), clk->div_shift, clk->div_mask) + 1;
429
430                 if(div > old_div) {
431                         set_cru_bits_w_msk(div - 1, clk->div_mask, clk->div_shift, clk->clksel_con);
432                 }
433                 ret = clk_set_parent_nolock(clk, p_clk);
434                 if(ret) {
435                         CLKDATA_ERR("%s can't set %lu,reparent err\n", clk->name, rate);
436                         return -ENOENT;
437                 }
438         }
439         //set div
440         set_cru_bits_w_msk(div - 1, clk->div_mask, clk->div_shift, clk->clksel_con);
441         return 0;
442 }
443
444 //rate==div rate //hdmi
445 static int clk_freediv_autosel_parents_set_fixed_rate(struct clk *clk, unsigned long rate)
446 {
447         struct clk *p_clk;
448         u32 div, old_div;
449         int ret;
450         p_clk = get_freediv_parents_div(clk, rate, &div);
451
452         if(!p_clk)
453                 return -ENOENT;
454
455         if((p_clk->rate / div) != rate || (p_clk->rate % div))
456                 return -ENOENT;
457
458         if (clk->parent != p_clk) {
459                 old_div = CRU_GET_REG_BITS_VAL(cru_readl(clk->clksel_con),
460                                 clk->div_shift, clk->div_mask) + 1;
461                 if(div > old_div) {
462                         set_cru_bits_w_msk(div - 1, clk->div_mask, clk->div_shift, clk->clksel_con);
463                 }
464                 ret = clk_set_parent_nolock(clk, p_clk);
465                 if (ret) {
466                         CLKDATA_DBG("%s can't get rate%lu,reparent err\n", clk->name, rate);
467                         return ret;
468                 }
469         }
470         //set div
471         set_cru_bits_w_msk(div - 1, clk->div_mask, clk->div_shift, clk->clksel_con);
472         return 0;
473 }
474
475 /***************************round********************************/
476
477 static long clksel_freediv_round_rate(struct clk *clk, unsigned long rate)
478 {
479         return clk->parent->rate / clk_get_freediv(rate, clk->parent->rate, clk->div_max);
480 }
481
482 static long clk_freediv_round_autosel_parents_rate(struct clk *clk, unsigned long rate)
483 {
484         u32 div;
485         struct clk *p_clk;
486         if(clk->rate == rate)
487                 return clk->rate;
488         p_clk = get_freediv_parents_div(clk, rate, &div);
489         if(!p_clk)
490                 return 0;
491         return p_clk->rate / div;
492 }
493
494 /**************************************others seting************************************/
495
496 static struct clk *clksel_get_parent(struct clk *clk) {
497         return clk->parents[(cru_readl(clk->clksel_con) >> clk->src_shift) & clk->src_mask];
498 }
499 static int clksel_set_parent(struct clk *clk, struct clk *parent)
500 {
501         u32 i;
502         if (unlikely(!clk->parents))
503                 return -EINVAL;
504         for (i = 0; (i < clk->parents_num); i++) {
505                 if (clk->parents[i] != parent)
506                         continue;
507                 set_cru_bits_w_msk(i, clk->src_mask, clk->src_shift, clk->clksel_con);
508                 return 0;
509         }
510         return -EINVAL;
511 }
512
513 static int gate_mode(struct clk *clk, int on)
514 {
515         int idx = clk->gate_idx;
516         if (idx >= CLK_GATE_MAX)
517                 return -EINVAL;
518         if(on) {
519                 cru_writel(CLK_GATE_W_MSK(idx) | CLK_UN_GATE(idx), CLK_GATE_CLKID_CONS(idx));
520                 //CLKDATA_DBG("un gate id=%d %s(%x),con %x\n",idx,clk->name,
521                 //      CLK_GATE_W_MSK(idx)|CLK_UN_GATE(idx),CLK_GATE_CLKID_CONS(idx));
522         } else {
523                 cru_writel(CLK_GATE_W_MSK(idx) | CLK_GATE(idx), CLK_GATE_CLKID_CONS(idx));
524                 //      CLKDATA_DBG("gate id=%d %s(%x),con%x\n",idx,clk->name,
525                 //      CLK_GATE_W_MSK(idx)|CLK_GATE(idx),CLK_GATE_CLKID_CONS(idx));
526         }
527         return 0;
528 }
529 /*****************************frac set******************************************/
530
531 static unsigned long clksel_recalc_frac(struct clk *clk)
532 {
533         unsigned long rate;
534         u64 rate64;
535         u32 r = cru_readl(clk->clksel_con), numerator, denominator;
536         if (r == 0) // FPGA ?
537                 return clk->parent->rate;
538         numerator = r >> 16;
539         denominator = r & 0xFFFF;
540         rate64 = (u64)clk->parent->rate * numerator;
541         do_div(rate64, denominator);
542         rate = rate64;
543         pr_debug("%s new clock rate is %lu (frac %u/%u)\n", clk->name, rate, numerator, denominator);
544         return rate;
545 }
546
547 static u32 clk_gcd(u32 numerator, u32 denominator)
548 {
549         u32 a, b;
550
551         if (!numerator || !denominator)
552                 return 0;
553         if (numerator > denominator) {
554                 a = numerator;
555                 b = denominator;
556         } else {
557                 a = denominator;
558                 b = numerator;
559         }
560         while (b != 0) {
561                 int r = b;
562                 b = a % b;
563                 a = r;
564         }
565
566         return a;
567 }
568
569 static int frac_div_get_seting(unsigned long rate_out, unsigned long rate,
570                 u32 *numerator, u32 *denominator)
571 {
572         u32 gcd_vl;
573         gcd_vl = clk_gcd(rate, rate_out);
574         CLKDATA_DBG("frac_get_seting rate=%lu,parent=%lu,gcd=%d\n", rate_out, rate, gcd_vl);
575
576         if (!gcd_vl) {
577                 CLKDATA_ERR("gcd=0, i2s frac div is not be supported\n");
578                 return -ENOENT;
579         }
580
581         *numerator = rate_out / gcd_vl;
582         *denominator = rate / gcd_vl;
583
584         CLKDATA_DBG("frac_get_seting numerator=%d,denominator=%d,times=%d\n",
585                         *numerator, *denominator, *denominator / *numerator);
586
587         if (*numerator > 0xffff || *denominator > 0xffff ||
588                         (*denominator / (*numerator)) < 20) {
589                 CLKDATA_ERR("can't get a available nume and deno\n");
590                 return -ENOENT;
591         }
592
593         return 0;
594
595 }
596 /* *********************pll **************************/
597
598 #define rk30_clock_udelay(a) udelay(a);
599
600 /*********************pll lock status**********************************/
601 //#define GRF_SOC_CON0       0x15c
602 static void pll_wait_lock(int pll_idx)
603 {
604         u32 pll_state[4] = {1, 0, 2, 3};
605         u32 bit = 0x20u << pll_state[pll_idx];
606         int delay = 24000000;
607         while (delay > 0) {
608                 if (regfile_readl(GRF_SOC_STATUS0) & bit)
609                         break;
610                 delay--;
611         }
612         if (delay == 0) {
613                 CLKDATA_ERR("wait pll bit 0x%x time out!\n", bit);
614                 while(1);
615         }
616 }
617
618
619
620 /***************************pll function**********************************/
621 static unsigned long pll_clk_recalc(u32 pll_id, unsigned long parent_rate)
622 {
623         unsigned long rate;
624
625         if (PLLS_IN_NORM(pll_id)) {
626                 u32 pll_con0 = cru_readl(PLL_CONS(pll_id, 0));
627                 u32 pll_con1 = cru_readl(PLL_CONS(pll_id, 1));
628
629
630                 u64 rate64 = (u64)parent_rate * PLL_NF(pll_con1);
631
632                 /*
633                    CLKDATA_DBG("selcon con0(%x) %x,con1(%x)%x, rate64 %llu\n",PLL_CONS(pll_id,0),pll_con0
634                    ,PLL_CONS(pll_id,1),pll_con1, rate64);
635                    */
636
637
638                 //CLKDATA_DBG("pll id=%d con0=%x,con1=%x,parent=%lu\n",pll_id,pll_con0,pll_con1,parent_rate);
639                 //CLKDATA_DBG("first pll id=%d rate is %lu (NF %d NR %d NO %d)\n",
640                 //pll_id, rate, PLL_NF(pll_con1), PLL_NR(pll_con0), 1 << PLL_NO(pll_con0));
641
642                 do_div(rate64, PLL_NR(pll_con0));
643                 do_div(rate64, PLL_NO(pll_con0));
644
645                 rate = rate64;
646                 /*
647                    CLKDATA_DBG("pll_clk_recalc id=%d rate=%lu (NF %d NR %d NO %d) rate64=%llu\n",
648                    pll_id, rate, PLL_NF(pll_con1), PLL_NR(pll_con0),PLL_NO(pll_con0), rate64);
649                    */
650         } else {
651                 rate = parent_rate;
652                 CLKDATA_DBG("pll_clk_recalc id=%d rate=%lu by pass mode\n", pll_id, rate);
653         }
654         return rate;
655 }
656 static unsigned long plls_clk_recalc(struct clk *clk)
657 {
658         return pll_clk_recalc(clk->pll->id, clk->parent->rate);
659 }
660
661 static int pll_clk_set_rate(struct pll_clk_set *clk_set, u8 pll_id)
662 {
663         //enter slowmode
664         cru_writel(PLL_MODE_SLOW(pll_id), CRU_MODE_CON);
665         cru_writel((0x1<<(16+1))|(0x1<<1), PLL_CONS(pll_id, 3));
666         dsb();
667         dsb();
668         dsb();
669         dsb();
670         dsb();
671         dsb();
672         cru_writel(clk_set->pllcon0, PLL_CONS(pll_id, 0));
673         cru_writel(clk_set->pllcon1, PLL_CONS(pll_id, 1));
674
675         rk30_clock_udelay(1);
676         cru_writel((0x1<<(16+1)), PLL_CONS(pll_id, 3));
677
678         pll_wait_lock(pll_id);
679
680         //return form slow
681         cru_writel(PLL_MODE_NORM(pll_id), CRU_MODE_CON);
682
683         /*
684            CLKDATA_ERR("pll reg id=%d,con0=%x,con1=%x,mode=%x\n",pll_id,
685            cru_readl(PLL_CONS(pll_id,0)),(PLL_CONS(pll_id,1)),cru_readl(CRU_MODE_CON));
686            */
687
688         return 0;
689 }
690 static int gpll_clk_set_rate(struct clk *c, unsigned long rate)
691 {
692         struct _pll_data *pll_data = c->pll;
693         struct pll_clk_set *clk_set = (struct pll_clk_set *)pll_data->table;
694
695         while(clk_set->rate) {
696                 if (clk_set->rate == rate) {
697                         break;
698                 }
699                 clk_set++;
700         }
701         if(clk_set->rate == rate) {
702                 pll_clk_set_rate(clk_set, pll_data->id);
703                 lpj_gpll = CLK_LOOPS_RECALC(rate);
704         } else {
705                 CLKDATA_ERR("gpll is no corresponding rate=%lu\n", rate);
706                 return -1;
707         }
708         return 0;
709 }
710
711 #define PLL_FREF_MIN (183*KHZ)
712 #define PLL_FREF_MAX (1500*MHZ)
713
714 #define PLL_FVCO_MIN (300*MHZ)
715 #define PLL_FVCO_MAX (1500*MHZ)
716
717 #define PLL_FOUT_MIN (18750*KHZ)
718 #define PLL_FOUT_MAX (1500*MHZ)
719
720 #define PLL_NF_MAX (65536)
721 #define PLL_NR_MAX (64)
722 #define PLL_NO_MAX (64)
723
724 static int pll_clk_get_set(unsigned long fin_hz, unsigned long fout_hz, u32 *clk_nr, u32 *clk_nf, u32 *clk_no)
725 {
726         u32 nr, nf, no, nonr;
727         u32 n;
728         u32 YFfenzi;
729         u32 YFfenmu;
730         unsigned long fref, fvco, fout;
731         u32 gcd_val = 0;
732
733         CLKDATA_DBG("pll_clk_get_set fin=%lu,fout=%lu\n", fin_hz, fout_hz);
734         if(!fin_hz || !fout_hz || fout_hz == fin_hz)
735                 return 0;
736         gcd_val = clk_gcd(fin_hz, fout_hz);
737         YFfenzi = fout_hz / gcd_val;
738         YFfenmu = fin_hz / gcd_val;
739
740         for(n = 1;; n++) {
741                 nf = YFfenzi * n;
742                 nonr = YFfenmu * n;
743                 if(nf > PLL_NF_MAX || nonr > (PLL_NO_MAX * PLL_NR_MAX))
744                         break;
745                 for(no = 1; no <= PLL_NO_MAX; no++) {
746                         if(!(no == 1 || !(no % 2)))
747                                 continue;
748
749                         if(nonr % no)
750                                 continue;
751                         nr = nonr / no;
752
753                         if(nr > PLL_NR_MAX) //PLL_NR_MAX
754                                 continue;
755
756                         fref = fin_hz / nr;
757                         if(fref < PLL_FREF_MIN || fref > PLL_FREF_MAX)
758                                 continue;
759
760                         fvco = (fin_hz / nr) * nf;
761                         if(fvco < PLL_FVCO_MIN || fvco > PLL_FVCO_MAX)
762                                 continue;
763                         fout = fvco / no;
764                         if(fout < PLL_FOUT_MIN || fout > PLL_FOUT_MAX)
765                                 continue;
766                         *clk_nr = nr;
767                         *clk_no = no;
768                         *clk_nf = nf;
769                         return 1;
770
771                 }
772
773         }
774         return 0;
775 }
776
777 static int pll_clk_mode(struct clk *clk, int on)
778 {
779         u8 pll_id = clk->pll->id;
780         u32 nr = PLL_NR(cru_readl(PLL_CONS(pll_id, 0)));
781         u32 dly = (nr * 500) / 24 + 1;
782         CLKDATA_DBG("pll_mode %s(%d)\n", clk->name, on);
783         if (on) {
784                 cru_writel(PLL_PWR_ON | PLL_PWR_DN_W_MSK, PLL_CONS(pll_id, 3));
785                 rk30_clock_udelay(dly);
786                 pll_wait_lock(pll_id);
787                 cru_writel(PLL_MODE_NORM(pll_id), CRU_MODE_CON);
788         } else {
789                 cru_writel(PLL_MODE_SLOW(pll_id), CRU_MODE_CON);
790                 cru_writel(PLL_PWR_DN | PLL_PWR_DN_W_MSK, PLL_CONS(pll_id, 3));
791         }
792         return 0;
793 }
794
795 static int cpll_clk_set_rate(struct clk *c, unsigned long rate)
796 {
797         struct _pll_data *pll_data = c->pll;
798         struct pll_clk_set *clk_set = (struct pll_clk_set *)pll_data->table;
799         struct pll_clk_set temp_clk_set;
800         u32 clk_nr, clk_nf, clk_no;
801
802
803         while(clk_set->rate) {
804                 if (clk_set->rate == rate) {
805                         break;
806                 }
807                 clk_set++;
808         }
809         if(clk_set->rate == rate) {
810                 CLKDATA_DBG("cpll get a rate\n");
811                 pll_clk_set_rate(clk_set, pll_data->id);
812
813         } else {
814                 CLKDATA_DBG("cpll get auto calc a rate\n");
815                 if(pll_clk_get_set(c->parent->rate, rate, &clk_nr, &clk_nf, &clk_no) == 0) {
816                         pr_err("cpll auto set rate error\n");
817                         return -ENOENT;
818                 }
819                 CLKDATA_DBG("cpll auto ger rate set nr=%d,nf=%d,no=%d\n", clk_nr, clk_nf, clk_no);
820                 temp_clk_set.pllcon0 = PLL_CLKR_SET(clk_nr) | PLL_CLKOD_SET(clk_no);
821                 temp_clk_set.pllcon1 = PLL_CLKF_SET(clk_nf);
822                 temp_clk_set.rst_dly = (clk_nr * 500) / 24 + 1;
823                 pll_clk_set_rate(&temp_clk_set, pll_data->id);
824
825         }
826         return 0;
827 }
828
829
830 /* ******************fixed input clk ***********************************************/
831 static struct clk xin24m = {
832         .name           = "xin24m",
833         .rate           = 24 * MHZ,
834         .flags          = RATE_FIXED,
835 };
836
837 static struct clk clk_12m = {
838         .name           = "clk_12m",
839         .parent         = &xin24m,
840         .rate           = 12 * MHZ,
841         .flags          = RATE_FIXED,
842 };
843
844 /************************************pll func***************************/
845 static const struct apll_clk_set *arm_pll_clk_get_best_pll_set(unsigned long rate,
846                 struct apll_clk_set *tables) {
847         const struct apll_clk_set *ps, *pt;
848
849         /* find the arm_pll we want. */
850         ps = pt = tables;
851         while (pt->rate) {
852                 if (pt->rate == rate) {
853                         ps = pt;
854                         break;
855                 }
856                 // we are sorted, and ps->rate > pt->rate.
857                 if ((pt->rate > rate || (rate - pt->rate < ps->rate - rate)))
858                         ps = pt;
859                 if (pt->rate < rate)
860                         break;
861                 pt++;
862         }
863         //CLKDATA_DBG("arm pll best rate=%lu\n",ps->rate);
864         return ps;
865 }
866 static long arm_pll_clk_round_rate(struct clk *clk, unsigned long rate)
867 {
868         return arm_pll_clk_get_best_pll_set(rate, clk->pll->table)->rate;
869 }
870 #if 1
871 struct arm_clks_div_set {
872         u32 rate;
873         u32     clksel0;
874         u32     clksel1;
875 };
876
877 #define _arm_clks_div_set(_mhz,_periph_div,_axi_div,_ahb_div, _apb_div,_ahb2apb) \
878 { \
879         .rate    =_mhz,\
880         .clksel0 = CORE_PERIPH_W_MSK|CORE_PERIPH_##_periph_div,\
881         .clksel1 = CORE_ACLK_W_MSK|CORE_ACLK_##_axi_div\
882         |ACLK_HCLK_W_MSK|ACLK_HCLK_##_ahb_div\
883         |ACLK_PCLK_W_MSK|ACLK_PCLK_##_apb_div\
884         |AHB2APB_W_MSK  |AHB2APB_##_ahb2apb,\
885 }
886 struct arm_clks_div_set arm_clk_div_tlb[] = {
887         _arm_clks_div_set(50 ,  2, 11, 11, 11, 11),//25,50,50,50,50
888         _arm_clks_div_set(100 , 4, 11, 21, 21, 11),//25,100,50,50,50
889         _arm_clks_div_set(150 , 4, 11, 21, 21, 11),//37,150,75,75,75
890         _arm_clks_div_set(200 , 8, 21, 21, 21, 11),//25,100,50,50,50
891         _arm_clks_div_set(300 , 8, 21, 21, 21, 11),//37,150,75,75,75
892         _arm_clks_div_set(400 , 8, 21, 21, 41, 21),//50,200,100,50,50
893         _arm_clks_div_set(0 ,   2, 11, 11, 11, 11),//25,50,50,50,50
894 };
895 struct arm_clks_div_set *arm_clks_get_div(u32 rate) {
896         int i = 0;
897         for(i = 0; arm_clk_div_tlb[i].rate != 0; i++) {
898                 if(arm_clk_div_tlb[i].rate >= rate)
899                         return &arm_clk_div_tlb[i];
900         }
901         return NULL;
902 }
903
904 #endif
905
906 static int arm_pll_clk_set_rate(struct clk *clk, unsigned long rate)
907 {
908         unsigned long flags;
909         const struct apll_clk_set *ps;
910         u32 pll_id = clk->pll->id;
911         u32 temp_div;
912         u32 old_aclk_div = 0, new_aclk_div;
913
914         ps = arm_pll_clk_get_best_pll_set(rate, (struct apll_clk_set *)clk->pll->table);
915
916         old_aclk_div = GET_CORE_ACLK_VAL(cru_readl(CRU_CLKSELS_CON(1))&CORE_ACLK_MSK);
917         new_aclk_div = GET_CORE_ACLK_VAL(ps->clksel1 & CORE_ACLK_MSK);
918
919         CLKDATA_LOG("apll will set rate(%lu) tlb con(%x,%x,%x),sel(%x,%x)\n",
920                         ps->rate, ps->pllcon0, ps->pllcon1, ps->pllcon2, ps->clksel0, ps->clksel1);
921
922         if(general_pll_clk.rate > clk->rate) {
923                 temp_div = clk_get_freediv(clk->rate, general_pll_clk.rate, 10);
924         } else {
925                 temp_div = 1;
926         }
927
928         // ungating cpu gpll path
929         //cru_writel(CLK_GATE_W_MSK(CLK_GATE_CPU_GPLL_PATH) | CLK_UN_GATE(CLK_GATE_CPU_GPLL_PATH), 
930         //      CLK_GATE_CLKID_CONS(CLK_GATE_CPU_GPLL_PATH));
931
932         local_irq_save(flags);
933         //div arm clk for gpll
934
935         cru_writel(CORE_CLK_DIV_W_MSK|CORE_CLK_DIV(temp_div), CRU_CLKSELS_CON(0));
936         cru_writel(CORE_SEL_PLL_W_MSK|CORE_SEL_GPLL, CRU_CLKSELS_CON(0));
937
938         loops_per_jiffy = lpj_gpll / temp_div;
939         smp_wmb();
940
941         /*if core src don't select gpll ,apll neet to enter slow mode */
942         //cru_writel(PLL_MODE_SLOW(APLL_ID), CRU_MODE_CON);
943
944
945         cru_writel((0x1<<(16+1))|(0x1<<1), PLL_CONS(pll_id, 3));
946         dsb();
947         dsb();
948         dsb();
949         dsb();
950         dsb();
951         dsb();
952         cru_writel(ps->pllcon0, PLL_CONS(pll_id, 0));
953         cru_writel(ps->pllcon1, PLL_CONS(pll_id, 1));
954
955         rk30_clock_udelay(1);
956         cru_writel((0x1<<(16+1)), PLL_CONS(pll_id, 3));
957
958         pll_wait_lock(pll_id);
959
960         //return form slow
961         //cru_writel(PLL_MODE_NORM(APLL_ID), CRU_MODE_CON);
962         //reparent to apll
963
964         if(new_aclk_div>=old_aclk_div) {
965                 cru_writel(ps->clksel0, CRU_CLKSELS_CON(0));
966                 cru_writel(ps->clksel1, CRU_CLKSELS_CON(1));
967         }
968
969         cru_writel(CORE_SEL_PLL_W_MSK | CORE_SEL_APLL, CRU_CLKSELS_CON(0));
970         if(old_aclk_div>new_aclk_div) {
971                 cru_writel(ps->clksel0, CRU_CLKSELS_CON(0));
972                 cru_writel(ps->clksel1, CRU_CLKSELS_CON(1));
973         }
974
975         cru_writel(CORE_CLK_DIV_W_MSK|CORE_CLK_DIV(1), CRU_CLKSELS_CON(0));
976
977         loops_per_jiffy = ps->lpj;
978         smp_wmb();
979
980         //CLKDATA_DBG("apll set loops_per_jiffy =%lu,rate(%lu)\n",loops_per_jiffy,ps->rate);
981
982         local_irq_restore(flags);
983
984         //gate gpll path
985         // FIXME
986         //cru_writel(CLK_GATE_W_MSK(CLK_GATE_CPU_GPLL_PATH) | CLK_GATE(CLK_GATE_CPU_GPLL_PATH)
987         //              , CLK_GATE_CLKID_CONS(CLK_GATE_CPU_GPLL_PATH));
988
989         CLKDATA_LOG("apll set over con(%x,%x,%x,%x),sel(%x,%x)\n", cru_readl(PLL_CONS(pll_id, 0)),
990                         cru_readl(PLL_CONS(pll_id, 1)), cru_readl(PLL_CONS(pll_id, 2)),
991                         cru_readl(PLL_CONS(pll_id, 3)), cru_readl(CRU_CLKSELS_CON(0)),
992                         cru_readl(CRU_CLKSELS_CON(1)));
993         return 0;
994 }
995
996
997 /************************************pll clocks***************************/
998
999 static const struct apll_clk_set apll_clks[] = {
1000         //_APLL_SET_CLKS(_mhz, nr, nf, no, _periph_div, 
1001         //              _axi_core_div, _axi_div, _ahb_div, _apb_div, _ahb2apb)  
1002         _APLL_SET_CLKS(2208, 1, 92, 1, 8,       81, 81, 21, 41, 21),
1003         _APLL_SET_CLKS(2184, 1, 91, 1, 8,       81, 81, 21, 41, 21),
1004         _APLL_SET_CLKS(2160, 1, 90, 1, 8,       81, 81, 21, 41, 21),
1005         _APLL_SET_CLKS(2136, 1, 89, 1, 8,       81, 81, 21, 41, 21),
1006         _APLL_SET_CLKS(2112, 1, 88, 1, 8,       81, 81, 21, 41, 21),
1007         _APLL_SET_CLKS(2088, 1, 87, 1, 8,       81, 81, 21, 41, 21),
1008         _APLL_SET_CLKS(2064, 1, 86, 1, 8,       81, 81, 21, 41, 21),
1009         _APLL_SET_CLKS(2040, 1, 85, 1, 8,       81, 81, 21, 41, 21),
1010         _APLL_SET_CLKS(2016, 1, 84, 1, 8,       81, 81, 21, 41, 21),
1011         _APLL_SET_CLKS(1992, 1, 83, 1, 8,       81, 81, 21, 41, 21),
1012         _APLL_SET_CLKS(1968, 1, 82, 1, 8,       81, 81, 21, 41, 21),
1013         _APLL_SET_CLKS(1944, 1, 81, 1, 8,       81, 81, 21, 41, 21),
1014         _APLL_SET_CLKS(1920, 1, 80, 1, 8,       81, 81, 21, 41, 21),
1015         _APLL_SET_CLKS(1896, 1, 79, 1, 8,       81, 81, 21, 41, 21),
1016         _APLL_SET_CLKS(1872, 1, 78, 1, 8,       81, 81, 21, 41, 21),
1017         _APLL_SET_CLKS(1848, 1, 77, 1, 8,       81, 81, 21, 41, 21),
1018         _APLL_SET_CLKS(1824, 1, 76, 1, 8,       81, 81, 21, 41, 21),
1019         _APLL_SET_CLKS(1800, 1, 75, 1, 8,       81, 81, 21, 41, 21),
1020         _APLL_SET_CLKS(1776, 1, 74, 1, 8,       81, 81, 21, 41, 21),
1021         _APLL_SET_CLKS(1752, 1, 73, 1, 8,       81, 81, 21, 41, 21),
1022         _APLL_SET_CLKS(1728, 1, 72, 1, 8,       81, 81, 21, 41, 21),
1023         _APLL_SET_CLKS(1704, 1, 71, 1, 8,       81, 81, 21, 41, 21),
1024         _APLL_SET_CLKS(1680, 1, 70, 1, 8,       41, 41, 21, 41, 21),
1025         _APLL_SET_CLKS(1656, 1, 69, 1, 8,       41, 41, 21, 41, 21),
1026         _APLL_SET_CLKS(1632, 1, 68, 1, 8,       41, 41, 21, 41, 21),
1027         _APLL_SET_CLKS(1608, 1, 67, 1, 8,       41, 41, 21, 41, 21),
1028         _APLL_SET_CLKS(1560, 1, 65, 1, 8,       41, 41, 21, 41, 21),
1029         _APLL_SET_CLKS(1512, 1, 63, 1, 8,       41, 41, 21, 41, 21),
1030         _APLL_SET_CLKS(1488, 1, 62, 1, 8,       41, 41, 21, 41, 21),
1031         _APLL_SET_CLKS(1464, 1, 61, 1, 8,       41, 41, 21, 41, 21),
1032         _APLL_SET_CLKS(1440, 1, 60, 1, 8,       41, 41, 21, 41, 21),
1033         _APLL_SET_CLKS(1416, 1, 59, 1, 8,       41, 41, 21, 41, 21),
1034         _APLL_SET_CLKS(1392, 1, 58, 1, 8,       41, 41, 21, 41, 21),
1035         _APLL_SET_CLKS(1368, 1, 57, 1, 8,       41, 41, 21, 41, 21),
1036         _APLL_SET_CLKS(1344, 1, 56, 1, 8,       41, 41, 21, 41, 21),
1037         _APLL_SET_CLKS(1320, 1, 55, 1, 8,       41, 41, 21, 41, 21),
1038         _APLL_SET_CLKS(1296, 1, 54, 1, 8,       41, 41, 21, 41, 21),
1039         _APLL_SET_CLKS(1272, 1, 53, 1, 8,       41, 41, 21, 41, 21),
1040         _APLL_SET_CLKS(1248, 1, 52, 1, 8,       41, 41, 21, 41, 21),
1041         _APLL_SET_CLKS(1224, 1, 51, 1, 8,       41, 41, 21, 41, 21),
1042         _APLL_SET_CLKS(1200, 1, 50, 1, 8,       41, 41, 21, 41, 21),
1043         _APLL_SET_CLKS(1176, 1, 49, 1, 8,       41, 41, 21, 41, 21),
1044         _APLL_SET_CLKS(1128, 1, 47, 1, 8,       41, 41, 21, 41, 21),
1045         _APLL_SET_CLKS(1104, 1, 46, 1, 8,       41, 41, 21, 41, 21),
1046         _APLL_SET_CLKS(1008, 1, 84, 2, 8,       41, 31, 21, 41, 21),
1047         _APLL_SET_CLKS(912,  1, 76, 2, 8,       41, 31, 21, 41, 21),
1048         _APLL_SET_CLKS(888,  1, 74, 2, 8,       41, 31, 21, 41, 21),
1049         _APLL_SET_CLKS(816 , 1, 68, 2, 8,       41, 31, 21, 41, 21),
1050         _APLL_SET_CLKS(792 , 1, 66, 2, 8,       41, 31, 21, 41, 21),
1051         _APLL_SET_CLKS(696 , 1, 58, 2, 8,       41, 31, 21, 41, 21),
1052         _APLL_SET_CLKS(600 , 1, 50, 2, 4,       41, 31, 21, 41, 21),
1053         _APLL_SET_CLKS(504 , 1, 84, 4, 4,       41, 21, 21, 41, 21),
1054         _APLL_SET_CLKS(408 , 1, 68, 4, 4,       21, 21, 21, 41, 21),
1055         _APLL_SET_CLKS(312 , 1, 52, 4, 2,       21, 21, 21, 21, 11),
1056         _APLL_SET_CLKS(252 , 1, 84, 8, 2,       21, 21, 21, 21, 11),
1057         _APLL_SET_CLKS(216 , 1, 72, 8, 2,       21, 21, 21, 21, 11),
1058         _APLL_SET_CLKS(126 , 1, 84, 16, 2,      11, 21, 11, 11, 11),
1059         _APLL_SET_CLKS(48  , 1, 64, 32, 2,      11, 11, 11, 11, 11),
1060         _APLL_SET_CLKS(0   , 1, 21, 4, 2,       11, 11, 11, 11, 11),
1061
1062 };
1063 static struct _pll_data apll_data = SET_PLL_DATA(APLL_ID, (void *)apll_clks);
1064 static struct clk arm_pll_clk = {
1065         .name           = "arm_pll",
1066         .parent         = &xin24m,
1067         .mode           = pll_clk_mode,
1068         .recalc         = plls_clk_recalc,
1069         .set_rate       = arm_pll_clk_set_rate,
1070         .round_rate     = arm_pll_clk_round_rate,
1071         .pll            = &apll_data,
1072 };
1073
1074 static int ddr_pll_clk_set_rate(struct clk *clk, unsigned long rate)
1075 {
1076         /* do nothing here */
1077         return 0;
1078 }
1079 static struct _pll_data dpll_data = SET_PLL_DATA(DPLL_ID, NULL);
1080 static struct clk ddr_pll_clk = {
1081         .name           = "ddr_pll",
1082         .parent         = &xin24m,
1083         .recalc         = plls_clk_recalc,
1084         .set_rate       = ddr_pll_clk_set_rate,
1085         .pll            = &dpll_data,
1086 };
1087
1088 static const struct pll_clk_set cpll_clks[] = {
1089         _PLL_SET_CLKS(360000, 1,  60,   4),
1090         _PLL_SET_CLKS(408000, 1,  68,   4),
1091         _PLL_SET_CLKS(456000, 1,  76,   4),
1092         _PLL_SET_CLKS(504000, 1,  84,   4),
1093         _PLL_SET_CLKS(552000, 1,  46,   2),
1094         _PLL_SET_CLKS(600000, 1,  50,   2),
1095         _PLL_SET_CLKS(742500, 8,  495,  2),
1096         _PLL_SET_CLKS(768000, 1,  64,   2),
1097         _PLL_SET_CLKS(798000, 2,  133,  2),
1098         _PLL_SET_CLKS(1188000, 2, 99,   1),
1099         _PLL_SET_CLKS(     0, 4,  133,  1),
1100 };
1101 static struct _pll_data cpll_data = SET_PLL_DATA(CPLL_ID, (void *)cpll_clks);
1102 static struct clk codec_pll_clk = {
1103         .name           = "codec_pll",
1104         .parent         = &xin24m,
1105         .mode           = pll_clk_mode,
1106         .recalc         = plls_clk_recalc,
1107         .set_rate       = cpll_clk_set_rate,
1108         .pll            = &cpll_data,
1109 };
1110
1111 static const struct pll_clk_set gpll_clks[] = {
1112         _PLL_SET_CLKS(148500,   2,      99,     8),
1113         _PLL_SET_CLKS(297000,   2,      198,    8),
1114         _PLL_SET_CLKS(300000,   1,      50,     4),
1115         _PLL_SET_CLKS(594000,   2,      198,    4),
1116         _PLL_SET_CLKS(1188000,  2,      99,     1),
1117         _PLL_SET_CLKS(1200000,  1,      50,     1),
1118         _PLL_SET_CLKS(0,        0,       0,     0),
1119 };
1120 static struct _pll_data gpll_data = SET_PLL_DATA(GPLL_ID, (void *)gpll_clks);
1121 static struct clk general_pll_clk = {
1122         .name           = "general_pll",
1123         .parent         = &xin24m,
1124         .recalc         = plls_clk_recalc,
1125         .set_rate       = gpll_clk_set_rate,
1126         .pll            = &gpll_data
1127 };
1128 /********************************clocks***********************************/
1129 GATE_CLK(ddr_gpll_path, general_pll_clk, DDR_GPLL);
1130
1131 /* core and cpu setting */
1132 static int ddr_clk_set_rate(struct clk *c, unsigned long rate)
1133 {
1134         CLKDATA_DBG("%s do nothing for ddr set rate\n", __func__);
1135         return 0;
1136 }
1137
1138 static long ddr_clk_round_rate(struct clk *clk, unsigned long rate)
1139 {
1140         CLKDATA_DBG("%s do nothing for ddr round rate\n", __func__);
1141         return ddr_set_pll(rate / MHZ, 0) * MHZ;
1142 }
1143 static unsigned long ddr_clk_recalc_rate(struct clk *clk)
1144 {
1145         u32 shift = get_cru_bits(clk->clksel_con, clk->div_mask, clk->div_shift);
1146         unsigned long rate = clk->parent->recalc(clk->parent) >> shift;
1147         //CLKDATA_DBG("%s new clock rate is %lu (shift %u)\n", clk->name, rate, shift);
1148         return rate;
1149 }
1150 static struct clk *clk_ddr_parents[2] = {&ddr_pll_clk, &clk_ddr_gpll_path};
1151 static struct clk clk_ddr = {
1152         .name           = "ddr",
1153         .parent         = &ddr_pll_clk,
1154         .recalc         = ddr_clk_recalc_rate,
1155         .set_rate       = ddr_clk_set_rate,
1156         .round_rate     = ddr_clk_round_rate,
1157         .gate_idx       = CLK_GATE_DDRPHY,
1158         .clksel_con     = CRU_CLKSELS_CON(26),
1159         CRU_DIV_SET(0x3, 0, 4),
1160         CRU_SRC_SET(1, 8),
1161         CRU_PARENTS_SET(clk_ddr_parents),
1162 };
1163 static int clk_core_set_rate(struct clk *c, unsigned long rate)
1164 {
1165         int ret;
1166
1167         ret = clk_set_rate_nolock(c->parent, rate);
1168         if (ret) {
1169                 CLKDATA_ERR("Failed to change clk pll %s to %lu\n", c->name, rate);
1170                 return ret;
1171         }
1172         //set arm pll div 1
1173         set_cru_bits_w_msk(0, c->div_mask, c->div_shift, c->clksel_con);
1174         return 0;
1175 }
1176 static unsigned long clk_core_get_rate(struct clk *c)
1177 {
1178         u32 div = (get_cru_bits(c->clksel_con, c->div_mask, c->div_shift) + 1);
1179         //c->parent->rate=c->parent->recalc(c->parent);
1180         return c->parent->rate / div;
1181 }
1182 static long core_clk_round_rate(struct clk *clk, unsigned long rate)
1183 {
1184         u32 div = (get_cru_bits(clk->clksel_con, clk->div_mask, clk->div_shift) + 1);
1185         return clk_round_rate_nolock(clk->parent, rate) / div;
1186 }
1187
1188 static int core_clksel_set_parent(struct clk *clk, struct clk *new_prt)
1189 {
1190
1191         u32 temp_div;
1192         struct clk *old_prt;
1193
1194         if(clk->parent == new_prt)
1195                 return 0;
1196         if (unlikely(!clk->parents))
1197                 return -EINVAL;
1198         CLKDATA_DBG("%s,reparent %s\n", clk->name, new_prt->name);
1199         //arm
1200         old_prt = clk->parent;
1201
1202         if(clk->parents[0] == new_prt) {
1203                 new_prt->set_rate(new_prt, 300 * MHZ);
1204                 set_cru_bits_w_msk(0, clk->div_mask, clk->div_shift, clk->clksel_con);
1205         } else if(clk->parents[1] == new_prt) {
1206
1207                 if(new_prt->rate > old_prt->rate) {
1208                         temp_div = clk_get_freediv(old_prt->rate, new_prt->rate, clk->div_max);
1209                         set_cru_bits_w_msk(temp_div - 1, clk->div_mask, clk->div_shift, clk->clksel_con);
1210                 }
1211                 set_cru_bits_w_msk(1, clk->src_mask, clk->src_shift, clk->clksel_con);
1212                 new_prt->set_rate(new_prt, 300 * MHZ);
1213         } else
1214                 return -1;
1215
1216
1217         return 0;
1218
1219 }
1220
1221 static int core_gpll_clk_set_rate(struct clk *c, unsigned long rate)
1222 {
1223         u32 temp_div;
1224         u32 old_aclk_div = 0, new_aclk_div;
1225         struct arm_clks_div_set *temp_clk_div;
1226         unsigned long arm_gpll_rate, arm_gpll_lpj;
1227         temp_div = clk_get_freediv(rate, c->parent->rate, c->div_max);
1228         arm_gpll_rate = c->parent->rate / temp_div;
1229
1230         temp_clk_div = arm_clks_get_div(arm_gpll_rate / MHZ);
1231         if(!temp_clk_div)
1232                 temp_clk_div = &arm_clk_div_tlb[4];
1233
1234         old_aclk_div = GET_CORE_ACLK_VAL(cru_readl(CRU_CLKSELS_CON(1))&CORE_ACLK_MSK);
1235         new_aclk_div = GET_CORE_ACLK_VAL(temp_clk_div->clksel1 & CORE_ACLK_MSK);
1236         if(c->rate >= rate) {
1237                 arm_gpll_lpj = lpj_gpll / temp_div;
1238                 set_cru_bits_w_msk(temp_div - 1, c->div_mask, c->div_shift, c->clksel_con);
1239         }
1240
1241         cru_writel((temp_clk_div->clksel1), CRU_CLKSELS_CON(1));
1242         cru_writel((temp_clk_div->clksel0) | CORE_CLK_DIV(temp_div) | CORE_CLK_DIV_W_MSK,
1243                         CRU_CLKSELS_CON(0));
1244         if((c->rate < rate)) {
1245                 arm_gpll_lpj = lpj_gpll / temp_div;
1246                 set_cru_bits_w_msk(temp_div - 1, c->div_mask, c->div_shift, c->clksel_con);
1247         }
1248         return 0;
1249 }
1250 static unsigned long arm_core_gpll_clk_get_rate(struct clk *c)
1251 {
1252         return c->parent->rate;
1253 }
1254 static struct clk clk_core_gpll_path = {
1255         .name           = "cpu_gpll_path",
1256         .parent         = &general_pll_clk,
1257         .recalc         = arm_core_gpll_clk_get_rate,
1258         .set_rate       = core_gpll_clk_set_rate,
1259         CRU_GATE_MODE_SET(gate_mode, CLK_GATE_CPU_GPLL_PATH),
1260 };
1261
1262
1263 static struct clk *clk_core_parents[2] = {&arm_pll_clk, &clk_core_gpll_path};
1264
1265 static struct clk clk_core = {
1266         .name           = "core",
1267         .parent         = &arm_pll_clk,
1268         .set_rate       = clk_core_set_rate,
1269         .recalc         = clk_core_get_rate,
1270         .round_rate     = core_clk_round_rate,
1271         .set_parent     = core_clksel_set_parent,
1272         .clksel_con     = CRU_CLKSELS_CON(0),
1273         CRU_DIV_SET(0x1f, 9, 32),
1274         CRU_SRC_SET(1, 8),
1275         CRU_PARENTS_SET(clk_core_parents),
1276 };
1277 GATE_CLK(l2c, clk_core, CLK_L2C);
1278 GATE_CLK(core_dbg, clk_core, CLK_CORE_DBG);
1279 static unsigned long aclk_recalc(struct clk *clk)
1280 {
1281         unsigned long rate;
1282         u32 div = get_cru_bits(clk->clksel_con, clk->div_mask, clk->div_shift) + 1;
1283
1284         BUG_ON(div > 5);
1285         if (div >= 5)
1286                 div = 8;
1287         rate = clk->parent->rate / div;
1288         pr_debug("%s new clock rate is %ld (div %d)\n", clk->name, rate, div);
1289
1290         return rate;
1291 };
1292 static struct clk core_periph = {
1293         .name           = "core_periph",
1294         .parent         = &clk_core,
1295         .gate_idx       = CLK_GATE_CORE_PERIPH,
1296         .recalc         = clksel_recalc_shift_2,
1297         .clksel_con     = CRU_CLKSELS_CON(0),
1298         CRU_DIV_SET(0x3, 6, 16),
1299 };
1300 static struct clk aclk_core = {
1301         .name           = "aclk_core",
1302         .parent         = &clk_core,
1303         .gate_idx       = CLK_GATE_ACLK_CORE,
1304         .recalc         = aclk_recalc,
1305         .clksel_con     = CRU_CLKSELS_CON(1),
1306         CRU_DIV_SET(0x7, 3, 8),
1307 };
1308
1309 static struct clk *clk_cpu_div_parents[2] = {&arm_pll_clk, &general_pll_clk};
1310 static struct clk clk_cpu_div = {
1311         .name           = "cpu_div",
1312         .parent         = &arm_pll_clk,
1313         .set_rate       = clksel_set_rate_freediv,
1314         .recalc         = clksel_recalc_div,
1315         .clksel_con     = CRU_CLKSELS_CON(0),
1316         CRU_DIV_SET(0x1f, 0, 32),
1317         CRU_SRC_SET(1, 5),
1318         CRU_PARENTS_SET(clk_cpu_div_parents),
1319 };
1320
1321 static struct clk aclk_cpu = {
1322         .name           = "aclk_cpu",
1323         .parent         = &clk_cpu_div,
1324         .gate_idx       = CLK_GATE_ACLK_CPU,
1325 };
1326
1327 static struct clk hclk_cpu = {
1328         .name           = "hclk_cpu",
1329         .parent         = &aclk_cpu,
1330         .gate_idx       = CLK_GATE_HCLK_CPU,
1331         .recalc         = clksel_recalc_shift,
1332         .set_rate       = clksel_set_rate_shift,
1333         .clksel_con     = CRU_CLKSELS_CON(1),
1334         CRU_DIV_SET(0x3, 8, 4),
1335
1336 };
1337
1338 static struct clk pclk_cpu = {
1339         .name           = "pclk_cpu",
1340         .parent         = &aclk_cpu,
1341         .gate_idx       = CLK_GATE_PCLK_CPU,
1342         .recalc         = clksel_recalc_shift,
1343         .set_rate       = clksel_set_rate_shift,
1344         .clksel_con     = CRU_CLKSELS_CON(1),
1345         CRU_DIV_SET(0x3, 12, 8),
1346 };
1347
1348 static struct clk ahb2apb_cpu = {
1349         .name           = "ahb2apb",
1350         .parent         = &hclk_cpu,
1351         .recalc         = clksel_recalc_shift,
1352         .set_rate       = clksel_set_rate_shift,
1353         .clksel_con     = CRU_CLKSELS_CON(1),
1354         CRU_DIV_SET(0x3, 14, 4),
1355 };
1356
1357
1358 static struct clk atclk_cpu = {
1359         .name           = "atclk_cpu",
1360         .parent         = &pclk_cpu,
1361         .gate_idx       = CLK_GATE_ATCLK_CPU,
1362 };
1363
1364 /* GPU setting */
1365 static int clk_gpu_set_rate(struct clk *clk, unsigned long rate)
1366 {
1367         unsigned long max_rate = rate / 100 * 105;      /* +5% */
1368         return clkset_rate_freediv_autosel_parents(clk, max_rate);
1369 };
1370
1371 static struct clk *aclk_gpu_parents[2] = {&codec_pll_clk, &general_pll_clk};
1372
1373 static struct clk aclk_gpu = {
1374         .name           = "aclk_gpu",
1375         .mode           = gate_mode,
1376         .recalc         = clksel_recalc_div,
1377         .round_rate     = clk_freediv_round_autosel_parents_rate,
1378         .set_rate       = clksel_set_rate_freediv,
1379         .clksel_con     = CRU_CLKSELS_CON(34),
1380         .gate_idx       = CLK_GATE_ACLK_GPU,
1381         CRU_DIV_SET(0x1f, 0, 32),
1382         CRU_SRC_SET(0x1, 7),
1383         CRU_PARENTS_SET(aclk_gpu_parents),
1384 };
1385
1386 /* vcodec setting */
1387 static unsigned long clksel_recalc_vpu_hclk(struct clk *clk)
1388 {
1389         unsigned long rate = clk->parent->rate / 4;
1390         pr_debug("%s new clock rate is %lu (div %u)\n", clk->name, rate, 4);
1391         return rate;
1392 }
1393
1394 static struct clk *aclk_vepu_parents[2] = {&codec_pll_clk, &general_pll_clk};
1395
1396 static struct clk aclk_vepu = {
1397         .name           = "aclk_vepu",
1398         .parent         = &codec_pll_clk,
1399         .mode           = gate_mode,
1400         .recalc         = clksel_recalc_div,
1401         .set_rate       = clkset_rate_freediv_autosel_parents,
1402         .clksel_con     = CRU_CLKSELS_CON(32),
1403         .gate_idx       = CLK_GATE_ACLK_VEPU,
1404         CRU_DIV_SET(0x1f, 0, 32),
1405         CRU_SRC_SET(0x1, 7),
1406         CRU_PARENTS_SET(aclk_vepu_parents),
1407 };
1408
1409 static struct clk *aclk_vdpu_parents[2] = {&codec_pll_clk, &general_pll_clk};
1410
1411 static struct clk aclk_vdpu = {
1412         .name           = "aclk_vdpu",
1413         .mode           = gate_mode,
1414         .recalc         = clksel_recalc_div,
1415         .set_rate       = clkset_rate_freediv_autosel_parents,
1416         .clksel_con     = CRU_CLKSELS_CON(32),
1417         .gate_idx       = CLK_GATE_ACLK_VDPU,
1418         CRU_DIV_SET(0x1f, 8, 32),
1419         CRU_SRC_SET(0x1, 15),
1420         CRU_PARENTS_SET(aclk_vdpu_parents),
1421 };
1422 static struct clk hclk_vepu = {
1423         .name           = "hclk_vepu",
1424         .parent         = &aclk_vepu,
1425         .mode           = gate_mode,
1426         .recalc         = clksel_recalc_vpu_hclk,
1427         .gate_idx       = CLK_GATE_HCLK_VEPU,
1428 };
1429
1430 static struct clk hclk_vdpu = {
1431         .name           = "hclk_vdpu",
1432         .parent         = &aclk_vdpu,
1433         .mode           = gate_mode,
1434         .recalc         = clksel_recalc_vpu_hclk,
1435         .gate_idx       = CLK_GATE_HCLK_VDPU,
1436 };
1437
1438 /* aclk lcdc setting */
1439 static struct clk *aclk_lcdc0_parents[] = {&codec_pll_clk, &general_pll_clk};
1440
1441 static struct clk aclk_lcdc0_pre = {
1442         .name           = "aclk_lcdc0_pre",
1443         .parent         = &codec_pll_clk,
1444         .mode           = gate_mode,
1445         .recalc         = clksel_recalc_div,
1446         .set_rate       = clkset_rate_freediv_autosel_parents,
1447         .gate_idx       = CLK_GATE_ACLK_LCDC0_SRC,
1448         .clksel_con     = CRU_CLKSELS_CON(31),
1449         CRU_DIV_SET(0x1f, 0, 32),
1450         CRU_SRC_SET(0x1, 7),
1451         CRU_PARENTS_SET(aclk_lcdc0_parents),
1452 };
1453
1454 static struct clk *aclk_lcdc1_parents[] = {&codec_pll_clk, &general_pll_clk};
1455
1456 static struct clk aclk_lcdc1_pre = {
1457         .name           = "aclk_lcdc1_pre",
1458         .parent         = &codec_pll_clk,
1459         .mode           = gate_mode,
1460         .recalc         = clksel_recalc_div,
1461         .set_rate       = clkset_rate_freediv_autosel_parents,
1462         .gate_idx       = CLK_GATE_ACLK_LCDC1_SRC,
1463         .clksel_con     = CRU_CLKSELS_CON(31),
1464         CRU_DIV_SET(0x1f, 8, 32),
1465         CRU_SRC_SET(0x1, 15),
1466         CRU_PARENTS_SET(aclk_lcdc1_parents),
1467 };
1468
1469 /* aclk/hclk/pclk periph setting */
1470 static struct clk *aclk_periph_parents[2] = {&codec_pll_clk, &general_pll_clk};
1471
1472 static struct clk aclk_periph = {
1473         .name           = "aclk_periph",
1474         .parent         = &general_pll_clk,
1475         .mode           = gate_mode,
1476         .gate_idx       = CLK_GATE_ACLK_PERIPH,
1477         .recalc         = clksel_recalc_div,
1478         .set_rate       = clksel_set_rate_freediv,
1479         .clksel_con     = CRU_CLKSELS_CON(10),
1480         CRU_DIV_SET(0x1f, 0, 32),
1481         CRU_SRC_SET(1, 15),
1482         CRU_PARENTS_SET(aclk_periph_parents),
1483 };
1484 GATE_CLK(periph_src, aclk_periph, PERIPH_SRC);
1485
1486 static struct clk pclk_periph = {
1487         .name           = "pclk_periph",
1488         .parent         = &aclk_periph,
1489         .mode           = gate_mode,
1490         .gate_idx       = CLK_GATE_PCLK_PERIPH,
1491         .recalc         = clksel_recalc_shift,
1492         .set_rate       = clksel_set_rate_shift,
1493         .clksel_con     = CRU_CLKSELS_CON(10),
1494         CRU_DIV_SET(0x3, 12, 8),
1495 };
1496
1497 static struct clk hclk_periph = {
1498         .name           = "hclk_periph",
1499         .parent         = &aclk_periph,
1500         .mode           = gate_mode,
1501         .gate_idx       = CLK_GATE_HCLK_PERIPH,
1502         .recalc         = clksel_recalc_shift,
1503         .set_rate       = clksel_set_rate_shift,
1504         .clksel_con     = CRU_CLKSELS_CON(10),
1505         CRU_DIV_SET(0x3, 8, 4),
1506 };
1507 /* dclk lcdc setting */
1508 // FIXME
1509 static int clksel_set_rate_hdmi(struct clk *clk, unsigned long rate)
1510 {
1511         u32 div, old_div;
1512         int i;
1513         unsigned long new_rate;
1514         int ret = 0;
1515
1516         if(clk->rate == rate)
1517                 return 0;
1518         for(i = 0; i < 2; i++) {
1519                 div = clk_get_freediv(rate, clk->parents[i]->rate, clk->div_max);
1520                 new_rate = clk->parents[i]->rate / div;
1521                 if((rate == new_rate) && !(clk->parents[i]->rate % div)) {
1522                         break;
1523                 }
1524         }
1525         if(i >= 2) {
1526                 CLKDATA_ERR("%s can't set fixed rate%lu\n", clk->name, rate);
1527                 return -1;
1528         }
1529
1530         //CLKDATA_DBG("%s set rate %lu(from %s)\n",clk->name,rate,clk->parents[i]->name);
1531
1532         old_div = CRU_GET_REG_BITS_VAL(cru_readl(clk->clksel_con),
1533                         clk->div_shift, clk->div_mask) + 1;
1534         if(div > old_div)
1535                 set_cru_bits_w_msk(div - 1, clk->div_mask, clk->div_shift, clk->clksel_con);
1536
1537         if(clk->parents[i] != clk->parent) {
1538                 ret = clk_set_parent_nolock(clk, clk->parents[i]);
1539         }
1540
1541         if (ret) {
1542                 CLKDATA_ERR("lcdc1 %s can't get rate%lu,reparent%s(now %s) err\n",
1543                                 clk->name, rate, clk->parents[i]->name, clk->parent->name);
1544                 return ret;
1545         }
1546         set_cru_bits_w_msk(div - 1, clk->div_mask, clk->div_shift, clk->clksel_con);
1547         return 0;
1548 }
1549
1550 static int dclk_lcdc_set_rate(struct clk *clk, unsigned long rate)
1551 {
1552         int ret = 0;
1553         struct clk *parent;
1554
1555         if (rate == 27 * MHZ && (rk30_clock_flags & CLK_FLG_EXT_27MHZ)) {
1556                 parent = clk->parents[1];
1557                 //CLKDATA_DBG(" %s from=%s\n",clk->name,parent->name);
1558         } else {
1559                 parent = clk->parents[0];
1560         }
1561         //CLKDATA_DBG(" %s set rate=%lu parent %s(old %s)\n",
1562         //clk->name,rate,parent->name,clk->parent->name);
1563
1564         if(parent != clk->parents[1]) {
1565                 ret = clk_set_rate_nolock(parent, rate); //div 1:1
1566                 if (ret) {
1567                         CLKDATA_DBG("%s set rate=%lu err\n", clk->name, rate);
1568                         return ret;
1569                 }
1570         }
1571         if (clk->parent != parent) {
1572                 ret = clk_set_parent_nolock(clk, parent);
1573                 if (ret) {
1574                         CLKDATA_DBG("%s can't get rate%lu,reparent err\n", clk->name, rate);
1575                         return ret;
1576                 }
1577         }
1578         return ret;
1579 }
1580
1581 static struct clk *dclk_lcdc0_parents[2] = {&codec_pll_clk, &general_pll_clk};
1582 static struct clk dclk_lcdc0 = {
1583         .name           = "dclk_lcdc0",
1584         .mode           = gate_mode,
1585         .set_rate       = clkset_rate_evendiv_autosel_parents,
1586         .recalc         = clksel_recalc_div,
1587         .gate_idx       = CLK_GATE_DCLK_LCDC0_SRC,
1588         .clksel_con     = CRU_CLKSELS_CON(27),
1589         CRU_SRC_SET(0x1, 0),
1590         CRU_DIV_SET(0xff, 8, 256),
1591         CRU_PARENTS_SET(dclk_lcdc0_parents),
1592 };
1593
1594 static struct clk *dclk_lcdc1_parents[2] = {&codec_pll_clk, &general_pll_clk};
1595 static struct clk dclk_lcdc1 = {
1596         .name           = "dclk_lcdc1",
1597         .mode           = gate_mode,
1598         .set_rate       = clkset_rate_evendiv_autosel_parents,
1599         .recalc         = clksel_recalc_div,
1600         .gate_idx       = CLK_GATE_DCLK_LCDC1_SRC,
1601         .clksel_con     = CRU_CLKSELS_CON(28),
1602         CRU_SRC_SET(0x1, 0),
1603         CRU_DIV_SET(0xff, 8, 256),
1604         CRU_PARENTS_SET(dclk_lcdc1_parents),
1605 };
1606
1607 /* cif setting */
1608 // FIXME
1609 static struct clk *cifout_sel_pll_parents[2] = {&codec_pll_clk, &general_pll_clk};
1610 static struct clk cif_out_pll = {
1611         .name           = "cif_out_pll",
1612         .parent         = &general_pll_clk,
1613         .clksel_con     = CRU_CLKSELS_CON(29),
1614         CRU_SRC_SET(0x1, 0),
1615         CRU_PARENTS_SET(cifout_sel_pll_parents),
1616 };
1617
1618 static struct clk cif0_out_div = {
1619         .name           = "cif0_out_div",
1620         .parent         = &cif_out_pll,
1621         .mode           = gate_mode,
1622         .recalc         = clksel_recalc_div,
1623         .set_rate       = clksel_set_rate_freediv,
1624         .gate_idx       = CLK_GATE_CIF0_OUT,
1625         .clksel_con     = CRU_CLKSELS_CON(29),
1626         CRU_DIV_SET(0x1f, 1, 32),
1627 };
1628
1629 static int cif_out_set_rate(struct clk *clk, unsigned long rate)
1630 {
1631         int ret = 0;
1632         struct clk *parent;
1633
1634         if (rate == 24 * MHZ) {
1635                 parent = clk->parents[1];
1636         } else {
1637                 parent = clk->parents[0];
1638                 ret = clk_set_rate_nolock(parent, rate);
1639                 if (ret)
1640                         return ret;
1641         }
1642         if (clk->parent != parent)
1643                 ret = clk_set_parent_nolock(clk, parent);
1644
1645         return ret;
1646 }
1647
1648 static struct clk *cif0_out_parents[2] = {&cif0_out_div, &xin24m};
1649 static struct clk cif0_out = {
1650         .name           = "cif0_out",
1651         .parent         = &cif0_out_div,
1652         .set_rate       = cif_out_set_rate,
1653         .clksel_con     = CRU_CLKSELS_CON(29),
1654         CRU_SRC_SET(0x1, 7),
1655         CRU_PARENTS_SET(cif0_out_parents),
1656 };
1657
1658 static struct clk pclkin_cif0 = {
1659         .name           = "pclkin_cif0",
1660         .mode           = gate_mode,
1661         .gate_idx       = CLK_GATE_PCLKIN_CIF0,
1662 };
1663
1664 static struct clk inv_cif0 = {
1665         .name           = "inv_cif0",
1666         .parent         = &pclkin_cif0,
1667 };
1668
1669 static struct clk *cif0_in_parents[2] = {&pclkin_cif0, &inv_cif0};
1670 static struct clk cif0_in = {
1671         .name           = "cif0_in",
1672         .parent         = &pclkin_cif0,
1673         .clksel_con     = CRU_CLKSELS_CON(30),
1674         CRU_SRC_SET(0x1, 8),
1675         CRU_PARENTS_SET(cif0_in_parents),
1676 };
1677
1678 /* i2s/spdif setting */
1679 static struct clk *clk_i2s_div_parents[] = {&general_pll_clk, &codec_pll_clk};
1680 static struct clk clk_i2s_pll = {
1681         .name           = "i2s_pll",
1682         .parent         = &general_pll_clk,
1683         .clksel_con     = CRU_CLKSELS_CON(2),
1684         CRU_SRC_SET(0x1, 15),
1685         CRU_PARENTS_SET(clk_i2s_div_parents),
1686 };
1687
1688 static struct clk clk_i2s0_div = {
1689         .name           = "i2s0_div",
1690         .parent         = &clk_i2s_pll,
1691         .mode           = gate_mode,
1692         .recalc         = clksel_recalc_div,
1693         .set_rate       = clksel_set_rate_freediv,
1694         .round_rate     = clksel_freediv_round_rate,
1695         .gate_idx       = CLK_GATE_I2S0_SRC,
1696         .clksel_con     = CRU_CLKSELS_CON(3),
1697         CRU_DIV_SET(0x7f, 0, 64),
1698 };
1699
1700 static struct clk clk_spdif_div = {
1701         .name           = "spdif_div",
1702         .parent         = &clk_i2s_pll,
1703         .recalc         = clksel_recalc_div,
1704         .set_rate       = clksel_set_rate_freediv,
1705         .round_rate     = clksel_freediv_round_rate,
1706         .mode           = gate_mode,
1707         .gate_idx       = CLK_GATE_SPDIF_SRC,
1708         .clksel_con     = CRU_CLKSELS_CON(5),
1709         CRU_DIV_SET(0x7f, 0, 64),
1710 };
1711 static int clk_i2s_fracdiv_set_rate(struct clk *clk, unsigned long rate)
1712 {
1713         u32 numerator, denominator;
1714         //clk_i2s_div->clk_i2s_pll->gpll/cpll
1715         //clk->parent->parent
1716         if(frac_div_get_seting(rate, clk->parent->parent->rate,
1717                                 &numerator, &denominator) == 0) {
1718                 clk_set_rate_nolock(clk->parent, clk->parent->parent->rate); //PLL:DIV 1:
1719                 cru_writel_frac(numerator << 16 | denominator, clk->clksel_con);
1720                 CLKDATA_DBG("%s set rate=%lu,is ok\n", clk->name, rate);
1721         } else {
1722                 CLKDATA_ERR("clk_frac_div can't get rate=%lu,%s\n", rate, clk->name);
1723                 return -ENOENT;
1724         }
1725         return 0;
1726 }
1727
1728 static struct clk clk_i2s0_frac_div = {
1729         .name           = "i2s0_frac_div",
1730         .parent         = &clk_i2s0_div,
1731         .mode           = gate_mode,
1732         .gate_idx       = CLK_GATE_I2S0_FRAC,
1733         .recalc         = clksel_recalc_frac,
1734         .set_rate       = clk_i2s_fracdiv_set_rate,
1735         .clksel_con     = CRU_CLKSELS_CON(7),
1736 };
1737
1738 static struct clk clk_spdif_frac_div = {
1739         .name           = "spdif_frac_div",
1740         .parent         = &clk_spdif_div,
1741         .mode           = gate_mode,
1742         .gate_idx       = CLK_GATE_SPDIF_FRAC,
1743         .recalc         = clksel_recalc_frac,
1744         .set_rate       = clk_i2s_fracdiv_set_rate,
1745         .clksel_con     = CRU_CLKSELS_CON(9),
1746 };
1747
1748 #define I2S_SRC_DIV     (0x0)
1749 #define I2S_SRC_FRAC    (0x1)
1750 #define I2S_SRC_12M     (0x2)
1751
1752 static int i2s_set_rate(struct clk *clk, unsigned long rate)
1753 {
1754         int ret = -EINVAL;
1755         struct clk *parent;
1756
1757         if (rate == clk->parents[I2S_SRC_12M]->rate) {
1758                 parent = clk->parents[I2S_SRC_12M];
1759         } else if((long)clk_round_rate_nolock(clk->parents[I2S_SRC_DIV], rate) == rate) {
1760                 parent = clk->parents[I2S_SRC_DIV];
1761         } else {
1762                 parent = clk->parents[I2S_SRC_FRAC];
1763         }
1764
1765         CLKDATA_DBG(" %s set rate=%lu parent %s(old %s)\n",
1766                         clk->name, rate, parent->name, clk->parent->name);
1767
1768         if(parent != clk->parents[I2S_SRC_12M]) {
1769                 ret = clk_set_rate_nolock(parent, rate); //div 1:1
1770                 if (ret) {
1771                         CLKDATA_DBG("%s set rate%lu err\n", clk->name, rate);
1772                         return ret;
1773                 }
1774         }
1775
1776         if (clk->parent != parent) {
1777                 ret = clk_set_parent_nolock(clk, parent);
1778                 if (ret) {
1779                         CLKDATA_DBG("%s can't get rate%lu,reparent err\n", clk->name, rate);
1780                         return ret;
1781                 }
1782         }
1783
1784         return ret;
1785 };
1786
1787 static struct clk *clk_i2s0_parents[3] = {&clk_i2s0_div, &clk_i2s0_frac_div, &clk_12m};
1788
1789 static struct clk clk_i2s0 = {
1790         .name           = "i2s0",
1791         .set_rate       = i2s_set_rate,
1792         .clksel_con     = CRU_CLKSELS_CON(3),
1793         CRU_SRC_SET(0x3, 8),
1794         CRU_PARENTS_SET(clk_i2s0_parents),
1795 };
1796
1797 static struct clk *clk_spdif_parents[3] = {&clk_spdif_div, &clk_spdif_frac_div, &clk_12m};
1798
1799 static struct clk clk_spdif = {
1800         .name           = "spdif",
1801         .parent         = &clk_spdif_frac_div,
1802         .set_rate       = i2s_set_rate,
1803         .clksel_con = CRU_CLKSELS_CON(5),
1804         CRU_SRC_SET(0x3, 8),
1805         CRU_PARENTS_SET(clk_spdif_parents),
1806 };
1807
1808 /* otgphy setting */
1809 GATE_CLK(otgphy0, xin24m, OTGPHY0);
1810 GATE_CLK(otgphy1, xin24m, OTGPHY1);
1811
1812 static struct clk clk_otgphy0_480m = {
1813         .name           = "otgphy0_480m",
1814         .parent         = &clk_otgphy0,
1815 };
1816 static struct clk clk_otgphy1_480m = {
1817         .name           = "otgphy1_480m",
1818         .parent         = &clk_otgphy1,
1819 };
1820
1821 /* hsicphy setting */
1822 #ifdef ARCH_RK31
1823 static struct clk *clk_hsicphy_parents[4] = {&clk_otgphy0_480m, &clk_otgphy1_480m, &general_pll_clk, &codec_pll_clk};
1824 static struct clk clk_hsicphy_480m = {
1825         .name           = "hsicphy_480m",
1826         .parent         = &clk_otgphy0_480m,
1827         .clksel_con      = CRU_CLKSELS_CON(30),
1828         CRU_SRC_SET(0x3, 0),
1829         CRU_PARENTS_SET(clk_hsicphy_parents),
1830 };
1831 static struct clk clk_hsicphy_12m = {
1832         .name           = "hsicphy_12m",
1833         .parent         = &clk_hsicphy_480m,
1834         .clksel_con     = CRU_CLKSELS_CON(11),
1835         CRU_DIV_SET(0x3f, 8, 64),
1836 };
1837 #endif
1838
1839 /* mac and rmii setting */
1840 // FIXME
1841 static struct clk rmii_clkin = {
1842         .name           = "rmii_clkin",
1843 };
1844 static struct clk *clk_mac_ref_div_parents[2] = {&general_pll_clk, &ddr_pll_clk};
1845 static struct clk clk_mac_pll_div = {
1846         .name           = "mac_pll_div",
1847         .parent         = &general_pll_clk,
1848         .mode           = gate_mode,
1849         .gate_idx       = CLK_GATE_MAC_SRC,
1850         .recalc         = clksel_recalc_div,
1851         .set_rate       = clksel_set_rate_freediv,
1852         .clksel_con     = CRU_CLKSELS_CON(21),
1853         CRU_DIV_SET(0x1f, 8, 32),
1854         CRU_SRC_SET(0x1, 0),
1855         CRU_PARENTS_SET(clk_mac_ref_div_parents),
1856 };
1857
1858 static int clksel_mac_ref_set_rate(struct clk *clk, unsigned long rate)
1859 {
1860         if(clk->parent == clk->parents[1]) {
1861                 CLKDATA_DBG("mac_ref clk is form mii clkin,can't set it\n" );
1862                 return -ENOENT;
1863         } else if(clk->parent == clk->parents[0]) {
1864                 return clk_set_rate_nolock(clk->parents[0], rate);
1865         }
1866         return -ENOENT;
1867 }
1868
1869 static struct clk *clk_mac_ref_parents[2] = {&clk_mac_pll_div, &rmii_clkin};
1870
1871 static struct clk clk_mac_ref = {
1872         .name           = "mac_ref",
1873         .parent         = &clk_mac_pll_div,
1874         .set_rate       = clksel_mac_ref_set_rate,
1875         .clksel_con     = CRU_CLKSELS_CON(21),
1876         CRU_SRC_SET(0x1, 4),
1877         CRU_PARENTS_SET(clk_mac_ref_parents),
1878 };
1879
1880 static int clk_set_mii_tx_parent(struct clk *clk, struct clk *parent)
1881 {
1882         return clk_set_parent_nolock(clk->parent, parent);
1883 }
1884
1885 static struct clk clk_mii_tx = {
1886         .name           = "mii_tx",
1887         .parent         = &clk_mac_ref,
1888         //.set_parent   = clk_set_mii_tx_parent,
1889         .mode           = gate_mode,
1890         .gate_idx       = CLK_GATE_MAC_LBTEST,
1891 };
1892
1893 /* hsadc and saradc */
1894 static struct clk *clk_hsadc_pll_parents[2] = {&general_pll_clk, &codec_pll_clk};
1895 static struct clk clk_hsadc_pll_div = {
1896         .name           = "hsadc_pll_div",
1897         .parent         = &general_pll_clk,
1898         .mode           = gate_mode,
1899         .gate_idx       = CLK_GATE_HSADC_SRC,
1900         .recalc         = clksel_recalc_div,
1901         .round_rate     = clk_freediv_round_autosel_parents_rate,
1902         .set_rate       = clkset_rate_freediv_autosel_parents,
1903         //.round_rate   = clksel_freediv_round_rate,
1904         //.set_rate     = clksel_set_rate_freediv,
1905         .clksel_con = CRU_CLKSELS_CON(22),
1906         CRU_DIV_SET(0xff, 8, 256),
1907         CRU_SRC_SET(0x1, 0),
1908         CRU_PARENTS_SET(clk_hsadc_pll_parents),
1909 };
1910 static int clk_hsadc_fracdiv_set_rate_fixed_parent(struct clk *clk, unsigned long rate)
1911 {
1912         u32 numerator, denominator;
1913         //        clk_hsadc_pll_div->gpll/cpll
1914         //clk->parent->parent
1915         if(frac_div_get_seting(rate, clk->parent->parent->rate,
1916                                 &numerator, &denominator) == 0) {
1917                 clk_set_rate_nolock(clk->parent, clk->parent->parent->rate); //PLL:DIV 1:
1918
1919                 cru_writel_frac(numerator << 16 | denominator, clk->clksel_con);
1920
1921                 CLKDATA_DBG("%s set rate=%lu,is ok\n", clk->name, rate);
1922         } else {
1923                 CLKDATA_ERR("clk_frac_div can't get rate=%lu,%s\n", rate, clk->name);
1924                 return -ENOENT;
1925         }
1926         return 0;
1927 }
1928 static int clk_hsadc_fracdiv_set_rate_auto_parents(struct clk *clk, unsigned long rate)
1929 {
1930         u32 numerator, denominator;
1931         u32 i, ret = 0;
1932         //        clk_hsadc_pll_div->gpll/cpll
1933         //clk->parent->parent
1934         for(i = 0; i < 2; i++) {
1935                 if(frac_div_get_seting(rate, clk->parent->parents[i]->rate,
1936                                         &numerator, &denominator) == 0)
1937                         break;
1938         }
1939         if(i >= 2)
1940                 return -ENOENT;
1941
1942         if(clk->parent->parent != clk->parent->parents[i])
1943                 ret = clk_set_parent_nolock(clk->parent, clk->parent->parents[i]);
1944         if(ret == 0) {
1945                 clk_set_rate_nolock(clk->parent, clk->parent->parents[i]->rate); //PLL:DIV 1:
1946
1947                 cru_writel_frac(numerator << 16 | denominator, clk->clksel_con);
1948
1949                 CLKDATA_DBG("clk_frac_div %s, rate=%lu\n", clk->name, rate);
1950         } else {
1951                 CLKDATA_ERR("clk_frac_div can't get rate=%lu,%s\n", rate, clk->name);
1952                 return -ENOENT;
1953         }
1954         return 0;
1955 }
1956
1957 static long clk_hsadc_fracdiv_round_rate(struct clk *clk, unsigned long rate)
1958 {
1959         u32 numerator, denominator;
1960
1961         CLKDATA_ERR("clk_hsadc_fracdiv_round_rate\n");
1962         if(frac_div_get_seting(rate, clk->parent->parent->rate,
1963                                 &numerator, &denominator) == 0)
1964                 return rate;
1965
1966         return 0;
1967 }
1968 static struct clk clk_hsadc_frac_div = {
1969         .name           = "hsadc_frac_div",
1970         .parent         = &clk_hsadc_pll_div,
1971         .mode           = gate_mode,
1972         .recalc         = clksel_recalc_frac,
1973         .set_rate       = clk_hsadc_fracdiv_set_rate_auto_parents,
1974         .round_rate     = clk_hsadc_fracdiv_round_rate,
1975         .gate_idx       = CLK_GATE_HSADC_FRAC_SRC,
1976         .clksel_con     = CRU_CLKSELS_CON(23),
1977 };
1978
1979 #define HSADC_SRC_DIV 0x0
1980 #define HSADC_SRC_FRAC 0x1
1981 #define HSADC_SRC_EXT 0x2
1982 static int clk_hsadc_set_rate(struct clk *clk, unsigned long rate)
1983 {
1984         int ret = -EINVAL;
1985         struct clk *parent;
1986
1987         if(clk->parent == clk->parents[HSADC_SRC_EXT]) {
1988                 CLKDATA_DBG("hsadc clk is form ext\n");
1989                 return 0;
1990         } else if((long)clk_round_rate_nolock(clk->parents[HSADC_SRC_DIV], rate) == rate) {
1991                 parent = clk->parents[HSADC_SRC_DIV];
1992         } else if((long)clk_round_rate_nolock(clk->parents[HSADC_SRC_FRAC], rate) == rate) {
1993                 parent = clk->parents[HSADC_SRC_FRAC];
1994         } else
1995                 parent = clk->parents[HSADC_SRC_DIV];
1996
1997         CLKDATA_DBG(" %s set rate=%lu parent %s(old %s)\n",
1998                         clk->name, rate, parent->name, clk->parent->name);
1999
2000         ret = clk_set_rate_nolock(parent, rate);
2001         if (ret) {
2002                 CLKDATA_ERR("%s set rate%lu err\n", clk->name, rate);
2003                 return ret;
2004         }
2005         if (clk->parent != parent) {
2006                 ret = clk_set_parent_nolock(clk, parent);
2007                 if (ret) {
2008                         CLKDATA_ERR("%s can't get rate%lu,reparent err\n", clk->name, rate);
2009                         return ret;
2010                 }
2011         }
2012         return ret;
2013 }
2014
2015 static struct clk clk_hsadc_ext = {
2016         .name           = "hsadc_ext",
2017 };
2018
2019 static struct clk *clk_hsadc_out_parents[3] = {&clk_hsadc_pll_div, &clk_hsadc_frac_div, &clk_hsadc_ext};
2020 static struct clk clk_hsadc_out = {
2021         .name           = "hsadc_out",
2022         .parent         = &clk_hsadc_pll_div,
2023         .set_rate       = clk_hsadc_set_rate,
2024         .clksel_con     = CRU_CLKSELS_CON(22),
2025         CRU_SRC_SET(0x3, 4),
2026         CRU_PARENTS_SET(clk_hsadc_out_parents),
2027 };
2028 static struct clk clk_hsadc_out_inv = {
2029         .name           = "hsadc_out_inv",
2030         .parent         = &clk_hsadc_out,
2031 };
2032
2033 static struct clk *clk_hsadc_parents[3] = {&clk_hsadc_out, &clk_hsadc_out_inv};
2034 static struct clk clk_hsadc = {
2035         .name           = "hsadc",
2036         .parent         = &clk_hsadc_out,
2037         .clksel_con     = CRU_CLKSELS_CON(22),
2038         CRU_SRC_SET(0x1, 7),
2039         CRU_PARENTS_SET(clk_hsadc_parents),
2040 };
2041
2042 static struct clk clk_saradc = {
2043         .name           = "saradc",
2044         .parent         = &xin24m,
2045         .mode           = gate_mode,
2046         .recalc         = clksel_recalc_div,
2047         .set_rate       = clksel_set_rate_freediv,
2048         .gate_idx       = CLK_GATE_SARADC_SRC,
2049         .clksel_con     = CRU_CLKSELS_CON(24),
2050         CRU_DIV_SET(0xff, 8, 256),
2051 };
2052
2053 /* smc setting */
2054 GATE_CLK(smc, hclk_periph, SMC_SRC);//smc
2055 static struct clk clkn_smc = {
2056         .name           = "smc_inv",
2057         .parent         = &clk_smc,
2058 };
2059
2060 /* spi setting */
2061 static struct clk clk_spi0 = {
2062         .name           = "spi0",
2063         .parent         = &pclk_periph,
2064         .mode           = gate_mode,
2065         .recalc         = clksel_recalc_div,
2066         .set_rate       = clksel_set_rate_freediv,
2067         .gate_idx       = CLK_GATE_SPI0_SRC,
2068         .clksel_con     = CRU_CLKSELS_CON(25),
2069         CRU_DIV_SET(0x7f, 0, 128),
2070 };
2071
2072 static struct clk clk_spi1 = {
2073         .name           = "spi1",
2074         .parent         = &pclk_periph,
2075         .mode           = gate_mode,
2076         .recalc         = clksel_recalc_div,
2077         .set_rate       = clksel_set_rate_freediv,
2078         .gate_idx       = CLK_GATE_SPI1_SRC,
2079         .clksel_con     = CRU_CLKSELS_CON(25),
2080         CRU_DIV_SET(0x7f, 8, 128),
2081 };
2082
2083 /* sdmmc/sdio/emmc setting */
2084 static struct clk clk_sdmmc = {
2085         .name           = "sdmmc",
2086         .parent         = &hclk_periph,
2087         .mode           = gate_mode,
2088         .recalc         = clksel_recalc_div,
2089         .set_rate       = clksel_set_rate_even,
2090         .gate_idx       = CLK_GATE_MMC0_SRC,
2091         .clksel_con     = CRU_CLKSELS_CON(11),
2092         CRU_DIV_SET(0x3f, 0, 64),
2093 };
2094
2095 static struct clk clk_sdio = {
2096         .name           = "sdio",
2097         .parent         = &hclk_periph,
2098         .mode           = gate_mode,
2099         .recalc         = clksel_recalc_div,
2100         .set_rate       = clksel_set_rate_even,
2101         .gate_idx       = CLK_GATE_SDIO_SRC,
2102         .clksel_con     = CRU_CLKSELS_CON(12),
2103         CRU_DIV_SET(0x3f, 0, 64),
2104
2105 };
2106
2107 static struct clk clk_emmc = {
2108         .name           = "emmc",
2109         .parent         = &hclk_periph,
2110         .mode           = gate_mode,
2111         .recalc         = clksel_recalc_div,
2112         .set_rate       = clksel_set_rate_freediv,
2113         .gate_idx       = CLK_GATE_EMMC_SRC,
2114         .clksel_con     = CRU_CLKSELS_CON(12),
2115         CRU_DIV_SET(0x3f, 8, 64),
2116 };
2117
2118 /* uart setting */
2119 static struct clk *clk_uart_src_parents[2] = {&general_pll_clk, &codec_pll_clk};
2120 static struct clk clk_uart_pll = {
2121         .name           = "uart_pll",
2122         .parent         = &general_pll_clk,
2123         .clksel_con     = CRU_CLKSELS_CON(12),
2124         CRU_SRC_SET(0x1, 15),
2125         CRU_PARENTS_SET(clk_uart_src_parents),
2126 };
2127 static struct clk clk_uart0_div = {
2128         .name           = "uart0_div",
2129         .parent         = &clk_uart_pll,
2130         .mode           = gate_mode,
2131         .gate_idx       = CLK_GATE_UART0_SRC,
2132         .recalc         = clksel_recalc_div,
2133         .set_rate       = clksel_set_rate_freediv,
2134         .round_rate     = clksel_freediv_round_rate,
2135         .clksel_con     = CRU_CLKSELS_CON(13),
2136         CRU_DIV_SET(0x7f, 0, 64),
2137 };
2138 static struct clk clk_uart1_div = {
2139         .name           = "uart1_div",
2140         .parent         = &clk_uart_pll,
2141         .mode           = gate_mode,
2142         .gate_idx       = CLK_GATE_UART1_SRC,
2143         .recalc         = clksel_recalc_div,
2144         .round_rate     = clksel_freediv_round_rate,
2145         .set_rate       = clksel_set_rate_freediv,
2146         .clksel_con     = CRU_CLKSELS_CON(14),
2147         CRU_DIV_SET(0x7f, 0, 64),
2148 };
2149
2150 static struct clk clk_uart2_div = {
2151         .name           = "uart2_div",
2152         .parent         = &clk_uart_pll,
2153         .mode           = gate_mode,
2154         .gate_idx       = CLK_GATE_UART2_SRC,
2155         .recalc         = clksel_recalc_div,
2156         .round_rate     = clksel_freediv_round_rate,
2157         .set_rate       = clksel_set_rate_freediv,
2158         .clksel_con     = CRU_CLKSELS_CON(15),
2159         CRU_DIV_SET(0x7f, 0, 64),
2160 };
2161
2162 static struct clk clk_uart3_div = {
2163         .name           = "uart3_div",
2164         .parent         = &clk_uart_pll,
2165         .mode           = gate_mode,
2166         .gate_idx       = CLK_GATE_UART3_SRC,
2167         .recalc         = clksel_recalc_div,
2168         .round_rate     = clksel_freediv_round_rate,
2169         .set_rate       = clksel_set_rate_freediv,
2170         .clksel_con     = CRU_CLKSELS_CON(16),
2171         CRU_DIV_SET(0x7f, 0, 64),
2172 };
2173 static int clk_uart_fracdiv_set_rate(struct clk *clk, unsigned long rate)
2174 {
2175         u32 numerator, denominator;
2176         //clk_uart0_div->clk_uart_pll->gpll/cpll
2177         //clk->parent->parent
2178         if(frac_div_get_seting(rate, clk->parent->parent->rate,
2179                                 &numerator, &denominator) == 0) {
2180                 clk_set_rate_nolock(clk->parent, clk->parent->parent->rate); //PLL:DIV 1:
2181
2182                 cru_writel_frac(numerator << 16 | denominator, clk->clksel_con);
2183
2184                 CLKDATA_DBG("%s set rate=%lu,is ok\n", clk->name, rate);
2185         } else {
2186                 CLKDATA_ERR("clk_frac_div can't get rate=%lu,%s\n", rate, clk->name);
2187                 return -ENOENT;
2188         }
2189         return 0;
2190 }
2191
2192 static struct clk clk_uart0_frac_div = {
2193         .name           = "uart0_frac_div",
2194         .parent         = &clk_uart0_div,
2195         .mode           = gate_mode,
2196         .recalc         = clksel_recalc_frac,
2197         .set_rate       = clk_uart_fracdiv_set_rate,
2198         .gate_idx       = CLK_GATE_UART0_FRAC_SRC,
2199         .clksel_con     = CRU_CLKSELS_CON(17),
2200 };
2201 static struct clk clk_uart1_frac_div = {
2202         .name           = "uart1_frac_div",
2203         .parent         = &clk_uart1_div,
2204         .mode           = gate_mode,
2205         .recalc         = clksel_recalc_frac,
2206         .set_rate       = clk_uart_fracdiv_set_rate,
2207         .gate_idx       = CLK_GATE_UART1_FRAC_SRC,
2208         .clksel_con     = CRU_CLKSELS_CON(18),
2209 };
2210 static struct clk clk_uart2_frac_div = {
2211         .name           = "uart2_frac_div",
2212         .mode           = gate_mode,
2213         .parent         = &clk_uart2_div,
2214         .recalc         = clksel_recalc_frac,
2215         .set_rate       = clk_uart_fracdiv_set_rate,
2216         .gate_idx       = CLK_GATE_UART2_FRAC_SRC,
2217         .clksel_con     = CRU_CLKSELS_CON(19),
2218 };
2219 static struct clk clk_uart3_frac_div = {
2220         .name           = "uart3_frac_div",
2221         .parent         = &clk_uart3_div,
2222         .mode           = gate_mode,
2223         .recalc         = clksel_recalc_frac,
2224         .set_rate       = clk_uart_fracdiv_set_rate,
2225         .gate_idx       = CLK_GATE_UART3_FRAC_SRC,
2226         .clksel_con     = CRU_CLKSELS_CON(20),
2227 };
2228
2229 #define UART_SRC_DIV 0
2230 #define UART_SRC_FRAC 1
2231 #define UART_SRC_24M 2
2232
2233 static int clk_uart_set_rate(struct clk *clk, unsigned long rate)
2234 {
2235         int ret = 0;
2236         struct clk *parent;
2237
2238         if(rate == clk->parents[UART_SRC_24M]->rate) { //24m
2239                 parent = clk->parents[UART_SRC_24M];
2240         } else if((long)clk_round_rate_nolock(clk->parents[UART_SRC_DIV], rate) == rate) {
2241                 parent = clk->parents[UART_SRC_DIV];
2242         } else {
2243                 parent = clk->parents[UART_SRC_FRAC];
2244         }
2245
2246         CLKDATA_DBG(" %s set rate=%lu parent %s(old %s)\n",
2247                         clk->name, rate, parent->name, clk->parent->name);
2248
2249         if(parent != clk->parents[UART_SRC_24M]) {
2250                 ret = clk_set_rate_nolock(parent, rate);
2251                 if (ret) {
2252                         CLKDATA_DBG("%s set rate%lu err\n", clk->name, rate);
2253                         return ret;
2254                 }
2255         }
2256
2257         if (clk->parent != parent) {
2258                 ret = clk_set_parent_nolock(clk, parent);
2259                 if (ret) {
2260                         CLKDATA_DBG("%s can't get rate%lu,reparent err\n", clk->name, rate);
2261                         return ret;
2262                 }
2263         }
2264
2265         return ret;
2266 }
2267
2268 static struct clk *clk_uart0_parents[3] = {&clk_uart0_div, &clk_uart0_frac_div, &xin24m};
2269 static struct clk clk_uart0 = {
2270         .name           = "uart0",
2271         .set_rate       = clk_uart_set_rate,
2272         .clksel_con     = CRU_CLKSELS_CON(13),
2273         CRU_SRC_SET(0x3, 8),
2274         CRU_PARENTS_SET(clk_uart0_parents),
2275 };
2276
2277 static struct clk *clk_uart1_parents[3] = {&clk_uart1_div, &clk_uart1_frac_div, &xin24m};
2278 static struct clk clk_uart1 = {
2279         .name           = "uart1",
2280         .set_rate       = clk_uart_set_rate,
2281         .clksel_con     = CRU_CLKSELS_CON(14),
2282         CRU_SRC_SET(0x3, 8),
2283         CRU_PARENTS_SET(clk_uart1_parents),
2284 };
2285
2286 static struct clk *clk_uart2_parents[3] = {&clk_uart2_div, &clk_uart2_frac_div, &xin24m};
2287 static struct clk clk_uart2 = {
2288         .name           = "uart2",
2289         .set_rate       = clk_uart_set_rate,
2290         .clksel_con     = CRU_CLKSELS_CON(15),
2291         CRU_SRC_SET(0x3, 8),
2292         CRU_PARENTS_SET(clk_uart2_parents),
2293 };
2294 static struct clk *clk_uart3_parents[3] = {&clk_uart3_div, &clk_uart3_frac_div, &xin24m};
2295 static struct clk clk_uart3 = {
2296         .name           = "uart3",
2297         .set_rate       = clk_uart_set_rate,
2298         .clksel_con     = CRU_CLKSELS_CON(16),
2299         CRU_SRC_SET(0x3, 8),
2300         CRU_PARENTS_SET(clk_uart3_parents),
2301 };
2302
2303 /* timer setting */
2304 GATE_CLK(timer0, xin24m, TIMER0);
2305 GATE_CLK(timer1, xin24m, TIMER1);
2306 GATE_CLK(timer2, xin24m, TIMER2);
2307 GATE_CLK(timer3, xin24m, TIMER3);
2308 GATE_CLK(timer4, xin24m, TIMER4);
2309 GATE_CLK(timer5, xin24m, TIMER5);
2310 GATE_CLK(timer6, xin24m, TIMER6);
2311
2312 /*********************power domain*******************************/
2313 #if 1
2314 #ifdef RK30_CLK_OFFBOARD_TEST
2315 void pmu_set_power_domain_test(enum pmu_power_domain pd, bool on) {};
2316 #define _pmu_set_power_domain pmu_set_power_domain_test//rk30_pmu_set_power_domain
2317 #else
2318 void pmu_set_power_domain(enum pmu_power_domain pd, bool on);
2319 #define _pmu_set_power_domain pmu_set_power_domain
2320 #endif
2321
2322 static int pd_video_mode(struct clk *clk, int on)
2323 {
2324         u32 gate[3];
2325         gate[0] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VEPU));
2326         gate[1] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VDPU));
2327         //gate[2] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VCODEC));
2328         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_VEPU), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VEPU));
2329         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_VDPU), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VDPU));
2330         //cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_VCODEC), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VCODEC));
2331         pmu_set_power_domain(PD_VIDEO, on);
2332         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_VEPU) | gate[0], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VEPU));
2333         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_VDPU) | gate[1], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VDPU));
2334         //cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_VCODEC) | gate[2], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VCODEC));
2335         return 0;
2336 }
2337
2338 static struct clk pd_video = {
2339         .name   = "pd_video",
2340         .flags  = IS_PD,
2341         .mode   = pd_video_mode,
2342         .gate_idx       = PD_VIDEO,
2343 };
2344 static int pd_display_mode(struct clk *clk, int on)
2345 {
2346         u32 gate[10];
2347         gate[0] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC0_SRC));
2348         gate[1] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC1_SRC));
2349         gate[2] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC0));
2350         gate[3] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC1));
2351         gate[4] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_CIF0));
2352         //gate[5] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_CIF1));
2353         gate[6] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VIO0));
2354         gate[7] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VIO1));
2355         gate[8] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_IPP));
2356         gate[9] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_RGA));
2357         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_LCDC0_SRC), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC0_SRC));
2358         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_LCDC1_SRC), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC1_SRC));
2359         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_LCDC0), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC0));
2360         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_LCDC1), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC1));
2361         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_CIF0), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_CIF0));
2362         //cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_CIF1), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_CIF1));
2363         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_VIO0), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VIO0));
2364         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_VIO1), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VIO1));
2365         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_IPP), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_IPP));
2366         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_RGA), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_RGA));
2367         pmu_set_power_domain(PD_VIO, on);
2368         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_LCDC0_SRC) | gate[0], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC0_SRC));
2369         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_LCDC1_SRC) | gate[1], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC1_SRC));
2370         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_LCDC0) | gate[2], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC0));
2371         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_LCDC1) | gate[3], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_LCDC1));
2372         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_CIF0) | gate[4], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_CIF0));
2373         //cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_CIF1) | gate[5], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_CIF1));
2374         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_VIO0) | gate[6], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VIO0));
2375         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_VIO1) | gate[7], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_VIO1));
2376         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_IPP) | gate[8], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_IPP));
2377         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_RGA) | gate[9], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_RGA));
2378         return 0;
2379 }
2380
2381 static struct clk pd_display = {
2382         .name   = "pd_vio",
2383         .flags  = IS_PD,
2384         .mode   = pd_display_mode,
2385         .gate_idx       = PD_VIO,
2386 };
2387 static struct clk pd_lcdc0 = {
2388         .parent = &pd_display,
2389         .name   = "pd_lcdc0",
2390 };
2391 static struct clk pd_lcdc1 = {
2392         .parent = &pd_display,
2393         .name   = "pd_lcdc1",
2394 };
2395 static struct clk pd_cif0 = {
2396         .parent = &pd_display,
2397         .name   = "pd_cif0",
2398 };
2399 static struct clk pd_rga = {
2400         .parent = &pd_display,
2401         .name   = "pd_rga",
2402 };
2403 static struct clk pd_ipp = {
2404         .parent = &pd_display,
2405         .name   = "pd_ipp",
2406 };
2407 static struct clk pd_hdmi = {
2408         .parent = &pd_display,
2409         .name   = "pd_hdmi",
2410 };
2411
2412
2413 static int pd_gpu_mode(struct clk *clk, int on)
2414 {
2415         u32 gate[3];
2416         gate[0] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_GPU));
2417         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_GPU), CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_GPU));
2418         pmu_set_power_domain(PD_GPU, on);
2419         cru_writel(CLK_GATE_W_MSK(CLK_GATE_ACLK_GPU) | gate[0], CLK_GATE_CLKID_CONS(CLK_GATE_ACLK_GPU));
2420         return 0;
2421 }
2422
2423 static struct clk pd_gpu = {
2424         .name   = "pd_gpu",
2425         .flags  = IS_PD,
2426         .mode   = pd_gpu_mode,
2427         .gate_idx       = PD_GPU,
2428 };
2429
2430 static int pm_off_mode(struct clk *clk, int on)
2431 {
2432         _pmu_set_power_domain(clk->gate_idx, on); //on 1
2433         return 0;
2434 }
2435 static struct clk pd_peri = {
2436         .name   = "pd_peri",
2437         .flags  = IS_PD,
2438         .mode   = pm_off_mode,
2439         .gate_idx       = PD_PERI,
2440 };
2441
2442
2443 #define PD_CLK(name) \
2444 {\
2445         .dev_id = NULL,\
2446         .con_id = #name,\
2447         .clk = &name,\
2448 }
2449
2450 #endif
2451 /************************rk30 fixed div clock****************************************/
2452
2453 /*************************aclk_cpu***********************/
2454
2455 GATE_CLK(dma1,          aclk_cpu,       ACLK_DMAC1);
2456 GATE_CLK(intmem,        aclk_cpu,       ACLK_INTMEM);
2457 GATE_CLK(aclk_strc_sys, aclk_cpu,       ACLK_STRC_SYS);
2458
2459 /*************************hclk_cpu***********************/
2460
2461 GATE_CLK(rom,           hclk_cpu,       HCLK_ROM);
2462 GATE_CLK(hclk_i2s0_2ch, hclk_cpu,       HCLK_I2S0_2CH);
2463 GATE_CLK(hclk_spdif,    hclk_cpu,       HCLK_SPDIF);
2464 GATE_CLK(hclk_cpubus,   hclk_cpu,       HCLK_CPUBUS);
2465 GATE_CLK(hclk_ahb2apb,  hclk_cpu,       HCLK_AHB2APB);
2466 GATE_CLK(hclk_vio_bus,  hclk_cpu,       HCLK_VIO_BUS);
2467 GATE_CLK(hclk_lcdc0,    hclk_cpu,       HCLK_LCDC0);
2468 GATE_CLK(hclk_lcdc1,    hclk_cpu,       HCLK_LCDC1);
2469 GATE_CLK(hclk_cif0,     hclk_cpu,       HCLK_CIF0);
2470 GATE_CLK(hclk_ipp,      hclk_cpu,       HCLK_IPP);
2471 GATE_CLK(hclk_rga,      hclk_cpu,       HCLK_RGA);
2472 GATE_CLK(hclk_imem0,    hclk_cpu,       HCLK_IMEM0);
2473 GATE_CLK(hclk_imem1,    hclk_cpu,       HCLK_IMEM1);
2474
2475 /*************************ahb2apb_cpu(pclk_cpu_h2p)***********************/
2476 GATE_CLK(pclk_uart0,    ahb2apb_cpu, PCLK_UART0);
2477 GATE_CLK(pclk_uart1,    ahb2apb_cpu, PCLK_UART1);
2478 /*************************pclk_cpu***********************/
2479 GATE_CLK(pwm01,         pclk_cpu,       PCLK_PWM01);//pwm 0¡¢1
2480 GATE_CLK(pclk_timer0,   pclk_cpu,       PCLK_TIMER0);
2481 GATE_CLK(pclk_timer2,   pclk_cpu,       PCLK_TIMER2);
2482 GATE_CLK(i2c0,  pclk_cpu, PCLK_I2C0);
2483 GATE_CLK(i2c1,  pclk_cpu, PCLK_I2C1);
2484 GATE_CLK(gpio0, pclk_cpu, PCLK_GPIO0);
2485 GATE_CLK(gpio1, pclk_cpu, PCLK_GPIO1);
2486 GATE_CLK(gpio2, pclk_cpu, PCLK_GPIO2);
2487 GATE_CLK(efuse, pclk_cpu, PCLK_EFUSE);
2488 GATE_CLK(tzpc,  pclk_cpu, PCLK_TZPC);
2489 GATE_CLK(pclk_ddrupctl, pclk_cpu, PCLK_DDRUPCTL);
2490 GATE_CLK(pclk_ddrpubl,  pclk_cpu, PCLK_PUBL);
2491 GATE_CLK(dbg,   pclk_cpu, PCLK_DBG);
2492 GATE_CLK(grf,   pclk_cpu, PCLK_GRF);
2493 GATE_CLK(pmu,   pclk_cpu, PCLK_PMU);
2494
2495 /*************************aclk_periph***********************/
2496
2497 GATE_CLK(dma2, aclk_periph, ACLK_DMAC2);
2498 GATE_CLK(aclk_smc, aclk_periph, ACLK_SMC);
2499 GATE_CLK(aclk_gps, aclk_periph, ACLK_GPS);
2500 GATE_CLK(aclk_peri_niu, aclk_periph, ACLK_PEI_NIU);
2501 GATE_CLK(aclk_cpu_peri, aclk_periph, ACLK_CPU_PERI);
2502 GATE_CLK(aclk_peri_axi_matrix, aclk_periph, ACLK_PERI_AXI_MATRIX);
2503
2504 /*************************hclk_periph***********************/
2505 GATE_CLK(hclk_peri_axi_matrix, hclk_periph, HCLK_PERI_AXI_MATRIX);
2506 GATE_CLK(hclk_peri_ahb_arbi, hclk_periph, HCLK_PERI_AHB_ARBI);
2507 GATE_CLK(hclk_emem_peri, hclk_periph, HCLK_EMEM_PERI);
2508 GATE_CLK(hclk_mac, hclk_periph, HCLK_EMAC);
2509 GATE_CLK(nandc, hclk_periph, HCLK_NANDC);
2510 GATE_CLK(hclk_usb_peri, hclk_periph, HCLK_USB_PERI);
2511 GATE_CLK(hclk_otg0, clk_hclk_usb_peri, HCLK_OTG0);
2512 GATE_CLK(hclk_otg1, clk_hclk_usb_peri, HCLK_OTG1);
2513 GATE_CLK(hclk_hsic, hclk_periph, HCLK_HSIC);
2514 GATE_CLK(hclk_hsadc, hclk_periph, HCLK_HSADC);
2515 GATE_CLK(hclk_pidfilter, hclk_periph, HCLK_PIDF);
2516 GATE_CLK(hclk_sdmmc, hclk_periph, HCLK_SDMMC0);
2517 GATE_CLK(hclk_sdio, hclk_periph, HCLK_SDIO);
2518 GATE_CLK(hclk_emmc, hclk_periph, HCLK_EMMC);
2519 /*************************pclk_periph***********************/
2520 GATE_CLK(pclk_peri_axi_matrix, pclk_periph, PCLK_PERI_AXI_MATRIX);
2521 GATE_CLK(pwm23, pclk_periph, PCLK_PWM23);
2522 GATE_CLK(wdt, pclk_periph, PCLK_WDT);
2523 GATE_CLK(pclk_spi0, pclk_periph, PCLK_SPI0);
2524 GATE_CLK(pclk_spi1, pclk_periph, PCLK_SPI1);
2525 GATE_CLK(pclk_uart2, pclk_periph, PCLK_UART2);
2526 GATE_CLK(pclk_uart3, pclk_periph, PCLK_UART3);
2527 GATE_CLK(i2c2, pclk_periph, PCLK_I2C2);
2528 GATE_CLK(i2c3, pclk_periph, PCLK_I2C3);
2529 GATE_CLK(i2c4, pclk_periph, PCLK_I2C4);
2530 GATE_CLK(gpio3, pclk_periph, PCLK_GPIO3);
2531 GATE_CLK(pclk_saradc, pclk_periph, PCLK_SARADC);
2532 /*************************aclk_lcdc0***********************/
2533
2534 GATE_CLK(aclk_vio0, aclk_lcdc0_pre, ACLK_VIO0);
2535 GATE_CLK(aclk_lcdc0, clk_aclk_vio0, ACLK_LCDC0);
2536 GATE_CLK(aclk_cif0, clk_aclk_vio0, ACLK_CIF0);
2537 GATE_CLK(aclk_ipp,  clk_aclk_vio0, ACLK_IPP);
2538
2539 /*************************aclk_lcdc0***********************/
2540
2541 GATE_CLK(aclk_vio1, aclk_lcdc1_pre, ACLK_VIO1);
2542 GATE_CLK(aclk_lcdc1, clk_aclk_vio1, ACLK_LCDC1);
2543 GATE_CLK(aclk_rga,  clk_aclk_vio1, ACLK_RGA);
2544
2545
2546 #if 1
2547 #define CLK(dev, con, ck) \
2548 {\
2549         .dev_id = dev,\
2550         .con_id = con,\
2551         .clk = ck,\
2552 }
2553
2554
2555 #define CLK1(name) \
2556 {\
2557         .dev_id = NULL,\
2558         .con_id = #name,\
2559         .clk = &clk_##name,\
2560 }
2561
2562 #endif
2563
2564 static struct clk_lookup clks[] = {
2565         CLK(NULL, "xin24m", &xin24m),
2566         //CLK(NULL, "xin27m", &xin27m),
2567         CLK(NULL, "xin12m", &clk_12m),
2568         CLK(NULL, "arm_pll", &arm_pll_clk),
2569         CLK(NULL, "ddr_pll", &ddr_pll_clk),
2570         CLK(NULL, "codec_pll", &codec_pll_clk),
2571         CLK(NULL, "general_pll", &general_pll_clk),
2572
2573         CLK(NULL, "arm_gpll", &clk_core_gpll_path),
2574         CLK(NULL, "ddr_gpll", &clk_ddr_gpll_path),
2575         
2576         CLK(NULL, "ddr", &clk_ddr),
2577         CLK(NULL, "cpu", &clk_core),
2578         CLK1(l2c),
2579         CLK1(core_dbg),
2580         CLK("smp_twd", NULL, &core_periph),
2581         CLK(NULL, "aclk_core", &aclk_core),
2582
2583         CLK(NULL, "logic", &clk_cpu_div),
2584         CLK(NULL, "aclk_cpu", &aclk_cpu),
2585         CLK(NULL, "pclk_cpu", &pclk_cpu),
2586         CLK(NULL, "atclk_cpu", &atclk_cpu),
2587         CLK(NULL, "hclk_cpu", &hclk_cpu),
2588         CLK(NULL, "ahb2apb_cpu", &ahb2apb_cpu),
2589
2590         CLK(NULL, "gpu",        &aclk_gpu),
2591
2592         CLK(NULL, "aclk_vepu",  &aclk_vepu),
2593         CLK(NULL, "hclk_vepu",  &hclk_vepu),
2594         CLK(NULL, "aclk_vdpu",  &aclk_vdpu),
2595         CLK(NULL, "hclk_vdpu",  &hclk_vdpu),
2596
2597         CLK(NULL, "aclk_lcdc0_pre", &aclk_lcdc0_pre),
2598         CLK(NULL, "aclk_lcdc1_pre", &aclk_lcdc1_pre),
2599
2600         CLK(NULL, "aclk_periph", &aclk_periph),
2601         CLK(NULL, "pclk_periph", &pclk_periph),
2602         CLK(NULL, "hclk_periph", &hclk_periph),
2603
2604         CLK(NULL, "dclk_lcdc0", &dclk_lcdc0),
2605         CLK(NULL, "dclk_lcdc1", &dclk_lcdc1),
2606         
2607         CLK(NULL, "cif_out_pll", &cif_out_pll),
2608         CLK(NULL, "cif0_out_div", &cif0_out_div),
2609         CLK(NULL, "cif0_out", &cif0_out),
2610
2611         CLK(NULL, "pclkin_cif0", &pclkin_cif0),
2612         CLK(NULL, "inv_cif0", &inv_cif0),
2613         CLK(NULL, "cif0_in", &cif0_in),
2614
2615         CLK1(i2s_pll),
2616         CLK("rk29_i2s.0", "i2s_div", &clk_i2s0_div),
2617         CLK("rk29_i2s.0", "i2s_frac_div", &clk_i2s0_frac_div),
2618         CLK("rk29_i2s.0", "i2s", &clk_i2s0),
2619         
2620         // actually no i2s1
2621         CLK("rk29_i2s.1", "i2s_div", &clk_i2s0_div),
2622         CLK("rk29_i2s.1", "i2s_frac_div", &clk_i2s0_frac_div),
2623         CLK("rk29_i2s.1", "i2s", &clk_i2s0),
2624
2625
2626         CLK1(spdif_div),
2627         CLK1(spdif_frac_div),
2628         CLK1(spdif),
2629
2630         CLK1(otgphy0),
2631         CLK1(otgphy1),
2632         CLK1(otgphy0_480m),
2633         CLK1(otgphy1_480m),
2634         CLK1(hsicphy_480m),
2635         CLK1(hsicphy_12m),
2636         
2637         CLK(NULL, "rmii_clkin", &rmii_clkin),
2638         CLK(NULL, "mac_ref_div", &clk_mac_pll_div), // compatible with rk29
2639         CLK1(mac_ref),
2640         CLK1(mii_tx),
2641
2642         CLK1(hsadc_pll_div),
2643         CLK1(hsadc_frac_div),
2644         CLK1(hsadc_ext),
2645         CLK1(hsadc_out),
2646         CLK1(hsadc_out_inv),
2647         CLK1(hsadc),
2648
2649         CLK1(saradc),
2650         
2651         CLK1(smc),
2652         CLK(NULL, "smc_inv",    &clkn_smc),
2653
2654         CLK("rk29xx_spim.0", "spi", &clk_spi0),
2655         CLK("rk29xx_spim.1", "spi", &clk_spi1),
2656
2657         CLK("rk29_sdmmc.0", "mmc", &clk_sdmmc),
2658         CLK("rk29_sdmmc.1", "mmc", &clk_sdio),
2659         CLK1(emmc),
2660
2661         CLK1(uart_pll),
2662         CLK("rk_serial.0", "uart_div", &clk_uart0_div),
2663         CLK("rk_serial.0", "uart_frac_div", &clk_uart0_frac_div),
2664         CLK("rk_serial.0", "uart", &clk_uart0),
2665         CLK("rk_serial.1", "uart_div", &clk_uart1_div),
2666         CLK("rk_serial.1", "uart_frac_div", &clk_uart1_frac_div),
2667         CLK("rk_serial.1", "uart", &clk_uart1),
2668         CLK("rk_serial.2", "uart_div", &clk_uart2_div),
2669         CLK("rk_serial.2", "uart_frac_div", &clk_uart2_frac_div),
2670         CLK("rk_serial.2", "uart", &clk_uart2),
2671         CLK("rk_serial.3", "uart_div", &clk_uart3_div),
2672         CLK("rk_serial.3", "uart_frac_div", &clk_uart3_frac_div),
2673         CLK("rk_serial.3", "uart", &clk_uart3),
2674
2675         CLK1(timer0),
2676         CLK1(timer1),
2677         CLK1(timer2),
2678         CLK1(timer3),
2679         CLK1(timer4),
2680         CLK1(timer5),
2681         CLK1(timer6),
2682
2683         /*************************aclk_cpu***********************/
2684         CLK1(dma1),
2685         CLK1(intmem),
2686         CLK1(aclk_strc_sys),
2687         
2688         /*************************hclk_cpu***********************/
2689         CLK1(rom),
2690         CLK("rk29_i2s.0", "hclk_i2s", &clk_hclk_i2s0_2ch),
2691         // actually no i2s1
2692         CLK("rk29_i2s.1", "hclk_i2s", &clk_hclk_i2s0_2ch),
2693         CLK1(hclk_spdif),
2694         CLK1(hclk_cpubus),
2695         CLK1(hclk_ahb2apb),
2696         CLK1(hclk_vio_bus),
2697         CLK1(hclk_lcdc0),
2698         CLK1(hclk_lcdc1),
2699         CLK1(hclk_cif0),
2700         CLK1(hclk_ipp),
2701         CLK1(hclk_rga),
2702         CLK1(hclk_imem0),
2703         CLK1(hclk_imem1),
2704
2705         /*************************pclk_cpu***********************/
2706         CLK1(pwm01),
2707         CLK1(pclk_timer0),
2708         CLK1(pclk_timer2),
2709         CLK("rk30_i2c.0", "i2c", &clk_i2c0),
2710         CLK("rk30_i2c.1", "i2c", &clk_i2c1),
2711         CLK1(gpio0),
2712         CLK1(gpio1),
2713         CLK1(gpio2),
2714         CLK1(efuse),
2715         CLK1(tzpc),
2716         CLK("rk_serial.0", "pclk_uart", &clk_pclk_uart0),
2717         CLK("rk_serial.1", "pclk_uart", &clk_pclk_uart1),
2718         CLK1(pclk_ddrupctl),
2719         CLK1(pclk_ddrpubl),
2720         CLK1(dbg),
2721         CLK1(grf),
2722         CLK1(pmu),
2723
2724         /*************************aclk_periph***********************/
2725         CLK1(dma2),
2726         CLK1(aclk_smc),
2727         CLK1(aclk_gps),
2728         CLK1(aclk_peri_niu),
2729         CLK1(aclk_cpu_peri),
2730         CLK1(aclk_peri_axi_matrix),
2731
2732         /*************************hclk_periph***********************/
2733         CLK1(hclk_peri_axi_matrix),
2734         CLK1(hclk_peri_ahb_arbi),
2735         CLK1(hclk_emem_peri),
2736         CLK1(hclk_mac),
2737         CLK1(nandc),
2738         CLK1(hclk_usb_peri),
2739         CLK1(hclk_otg0),
2740         CLK1(hclk_otg1),
2741         CLK1(hclk_hsic),
2742         CLK1(hclk_hsadc),
2743         CLK1(hclk_pidfilter),
2744         CLK("rk29_sdmmc.0", "hclk_mmc", &clk_hclk_sdmmc),
2745         CLK("rk29_sdmmc.1", "hclk_mmc", &clk_hclk_sdio),
2746         CLK1(hclk_emmc),
2747
2748         /*************************pclk_periph***********************/
2749         CLK1(pclk_peri_axi_matrix),
2750         CLK1(pwm23),
2751         CLK1(wdt),
2752         CLK("rk29xx_spim.0", "pclk_spi", &clk_pclk_spi0),
2753         CLK("rk29xx_spim.1", "pclk_spi", &clk_pclk_spi1),
2754         CLK("rk_serial.2", "pclk_uart", &clk_pclk_uart2),
2755         CLK("rk_serial.3", "pclk_uart", &clk_pclk_uart3),
2756         CLK("rk30_i2c.2", "i2c", &clk_i2c2),
2757         CLK("rk30_i2c.3", "i2c", &clk_i2c3),
2758         CLK("rk30_i2c.4", "i2c", &clk_i2c4),
2759         CLK1(gpio3),
2760         CLK1(pclk_saradc),
2761
2762         /*************************aclk_lcdc0***********************/
2763         CLK1(aclk_vio0),
2764         CLK(NULL, "aclk_lcdc0", &clk_aclk_lcdc0),
2765         CLK1(aclk_cif0),
2766         CLK1(aclk_ipp),
2767
2768         /*************************aclk_lcdc1***********************/
2769         CLK1(aclk_vio1),
2770         CLK(NULL, "aclk_lcdc1", &clk_aclk_lcdc1),
2771         CLK1(aclk_rga),
2772         /************************power domain**********************/
2773         
2774         PD_CLK(pd_peri),
2775         PD_CLK(pd_display),
2776         PD_CLK(pd_lcdc0),
2777         PD_CLK(pd_lcdc1),
2778         PD_CLK(pd_cif0),
2779         //PD_CLK(pd_cif1),
2780         PD_CLK(pd_rga),
2781         PD_CLK(pd_ipp),
2782         //PD_CLK(pd_video),
2783         PD_CLK(pd_gpu),
2784         //PD_CLK(pd_dbg),
2785 };
2786 static void __init rk30_init_enable_clocks(void)
2787 {
2788         #if 0
2789         //clk_enable_nolock(&xin24m);
2790         //clk_enable_nolock(&clk_12m);
2791         //clk_enable_nolock(&arm_pll_clk);
2792         //clk_enable_nolock(&ddr_pll_clk);
2793         //clk_enable_nolock(&codec_pll_clk);
2794         //clk_enable_nolock(&general_pll_clk);
2795         #endif
2796         clk_enable_nolock(&clk_ddr);
2797         //clk_enable_nolock(&clk_core);
2798         clk_enable_nolock(&clk_cpu_div);
2799         clk_enable_nolock(&clk_core_gpll_path);
2800         clk_enable_nolock(&clk_l2c);
2801         clk_enable_nolock(&clk_core_dbg);
2802         clk_enable_nolock(&core_periph);
2803         clk_enable_nolock(&aclk_core);
2804         //clk_enable_nolock(&aclk_cpu);
2805         //clk_enable_nolock(&pclk_cpu);
2806         clk_enable_nolock(&atclk_cpu);
2807         //clk_enable_nolock(&hclk_cpu);
2808         clk_enable_nolock(&ahb2apb_cpu);
2809         #if 0
2810          clk_enable_nolock(&clk_gpu);
2811          clk_enable_nolock(&aclk_gpu);
2812          clk_enable_nolock(&aclk_gpu_slv);
2813          clk_enable_nolock(&aclk_gpu_mst);
2814
2815          clk_enable_nolock(&aclk_vepu);
2816          clk_enable_nolock(&hclk_vepu);
2817          clk_enable_nolock(&aclk_vdpu);
2818          clk_enable_nolock(&hclk_vdpu);
2819
2820          clk_enable_nolock(&aclk_lcdc0_pre);
2821          clk_enable_nolock(&aclk_lcdc1_pre);
2822
2823          clk_enable_nolock(&aclk_periph);
2824         clk_enable_nolock(&pclk_periph);
2825         clk_enable_nolock(&hclk_periph);
2826         #endif
2827         #if 0
2828          clk_enable_nolock(&dclk_lcdc0);
2829          clk_enable_nolock(&dclk_lcdc1);
2830         
2831          clk_enable_nolock(&cif_out_pll);
2832          clk_enable_nolock(&cif0_out_div);
2833
2834          clk_enable_nolock(&cif0_out);
2835          clk_enable_nolock(&pclkin_cif0);
2836          clk_enable_nolock(&inv_cif0);
2837          clk_enable_nolock(&cif0_in);
2838
2839          clk_enable_nolock(&clk_i2s_pll);
2840          clk_enable_nolock(&clk_i2s0_div);
2841          clk_enable_nolock(&clk_i2s0_frac_div);
2842          clk_enable_nolock(&clk_i2s0);
2843         
2844           actually no i2s1
2845          clk_enable_nolock(&clk_i2s0_div);
2846          clk_enable_nolock(&clk_i2s0_frac_div);
2847          clk_enable_nolock(&clk_i2s0);
2848
2849         clk_enable_nolock(&clk_spdif_div);
2850         clk_enable_nolock(&clk_spdif_frac_div);
2851         clk_enable_nolock(&clk_spdif);
2852         #endif
2853         #if 0
2854         clk_enable_nolock(&clk_otgphy0);
2855         clk_enable_nolock(&clk_otgphy1);
2856         clk_enable_nolock(&clk_otgphy0_480m);
2857         clk_enable_nolock(&clk_otgphy1_480m);
2858         clk_enable_nolock(&clk_hsicphy_480m);
2859         clk_enable_nolock(&clk_hsicphy_12m);
2860         #endif  
2861         
2862         #if 0
2863         clk_enable_nolock(&rmii_clkin);
2864         clk_enable_nolock(&clk_mac_pll_div); // compatible with rk29
2865         clk_enable_nolock(&clk_mac_ref);
2866         clk_enable_nolock(&clk_mii_tx); 
2867         #endif
2868
2869         #if 0
2870         clk_enable_nolock(&clk_hsadc_pll_div);
2871         clk_enable_nolock(&clk_hsadc_frac_div);
2872         clk_enable_nolock(&clk_hsadc_ext);
2873         clk_enable_nolock(&clk_hsadc_out);
2874         clk_enable_nolock(&clk_hsadc_out_inv);
2875         clk_enable_nolock(&clk_hsadc);
2876
2877         clk_enable_nolock(&clk_saradc);
2878         #endif
2879         /*
2880         clk_enable_nolock(&clk_smc);
2881         clk_enable_nolock(&clkn_smc);
2882         */
2883         /*
2884         clk_enable_nolock(&clk_spi0);
2885         clk_enable_nolock(&clk_spi1);
2886         */
2887         /*
2888         clk_enable_nolock(&clk_sdmmc);
2889         clk_enable_nolock(&clk_sdio);
2890         clk_enable_nolock(&clk_emmc);
2891         */
2892         #if 0
2893         clk_enable_nolock(&clk_uart_pll);
2894         clk_enable_nolock(&clk_uart0_div);
2895         clk_enable_nolock(&clk_uart0_frac_div);
2896         clk_enable_nolock(&clk_uart0);
2897         clk_enable_nolock(&clk_uart1_div);
2898         clk_enable_nolock(&clk_uart1_frac_div);
2899         clk_enable_nolock(&clk_uart1);
2900         clk_enable_nolock(&clk_uart2_div);
2901         clk_enable_nolock(&clk_uart2_frac_div);
2902         clk_enable_nolock(&clk_uart2);
2903         clk_enable_nolock(&clk_uart3_div);
2904         clk_enable_nolock(&clk_uart3_frac_div);
2905         clk_enable_nolock(&clk_uart3);
2906         #endif
2907         #if CONFIG_RK_DEBUG_UART == 0
2908                 clk_enable_nolock(&clk_uart0);
2909                 clk_enable_nolock(&clk_pclk_uart0);
2910         #elif CONFIG_RK_DEBUG_UART == 1
2911                 clk_enable_nolock(&clk_uart1);
2912                 clk_enable_nolock(&clk_pclk_uart1);
2913
2914         #elif CONFIG_RK_DEBUG_UART == 2
2915                 clk_enable_nolock(&clk_uart2);
2916                 clk_enable_nolock(&clk_pclk_uart2);
2917
2918         #elif CONFIG_RK_DEBUG_UART == 3
2919                 clk_enable_nolock(&clk_uart3);
2920                 clk_enable_nolock(&clk_pclk_uart3);
2921
2922         #endif
2923         #if 0
2924         clk_enable_nolock(&clk_timer0);
2925         clk_enable_nolock(&clk_timer1);
2926         clk_enable_nolock(&clk_timer2);
2927         #endif
2928         
2929         /*************************aclk_cpu***********************/
2930         clk_enable_nolock(&clk_dma1);
2931         clk_enable_nolock(&clk_intmem);
2932         clk_enable_nolock(&clk_aclk_strc_sys);
2933         
2934         /*************************hclk_cpu***********************/
2935         clk_enable_nolock(&clk_rom);
2936         #if 0
2937         clk_enable_nolock(&clk_hclk_i2s0_2ch);
2938         // actually no i2s1
2939         clk_enable_nolock(&clk_hclk_i2s0_2ch);
2940         clk_enable_nolock(&clk_hclk_spdif);
2941         #endif
2942         clk_enable_nolock(&clk_hclk_cpubus);
2943         clk_enable_nolock(&clk_hclk_ahb2apb);
2944         clk_enable_nolock(&clk_hclk_vio_bus);
2945         #if 0
2946         clk_enable_nolock(&clk_hclk_lcdc0);
2947         clk_enable_nolock(&clk_hclk_lcdc1);
2948         clk_enable_nolock(&clk_hclk_cif0);
2949         clk_enable_nolock(&clk_hclk_ipp);
2950         clk_enable_nolock(&clk_hclk_rga);
2951         #endif
2952         clk_enable_nolock(&clk_hclk_imem0);
2953         clk_enable_nolock(&clk_hclk_imem1);
2954
2955         /*************************pclk_cpu***********************/
2956         #if 0
2957         clk_enable_nolock(&clk_pwm01);
2958         clk_enable_nolock(&clk_pclk_timer0);
2959         clk_enable_nolock(&clk_pclk_timer1);
2960         clk_enable_nolock(&clk_pclk_timer2);
2961         clk_enable_nolock(&clk_i2c0);
2962         clk_enable_nolock(&clk_i2c1);
2963         clk_enable_nolock(&clk_gpio0);
2964         clk_enable_nolock(&clk_gpio1);
2965         clk_enable_nolock(&clk_gpio2);
2966         clk_enable_nolock(&clk_efuse);
2967         #endif
2968         clk_enable_nolock(&clk_tzpc);
2969         //clk_enable_nolock(&clk_pclk_uart0);
2970         //clk_enable_nolock(&clk_pclk_uart1);
2971         clk_enable_nolock(&clk_pclk_ddrupctl);
2972         clk_enable_nolock(&clk_pclk_ddrpubl);
2973         clk_enable_nolock(&clk_dbg);
2974         clk_enable_nolock(&clk_grf);
2975         clk_enable_nolock(&clk_pmu);
2976
2977         /*************************aclk_periph***********************/
2978         clk_enable_nolock(&clk_dma2);
2979         clk_enable_nolock(&clk_aclk_smc);
2980         clk_enable_nolock(&clk_aclk_peri_niu);
2981         clk_enable_nolock(&clk_aclk_cpu_peri);
2982         clk_enable_nolock(&clk_aclk_peri_axi_matrix);
2983
2984         /*************************hclk_periph***********************/
2985         clk_enable_nolock(&clk_hclk_peri_axi_matrix);
2986         clk_enable_nolock(&clk_hclk_peri_ahb_arbi);
2987         clk_enable_nolock(&clk_hclk_emem_peri);
2988         //clk_enable_nolock(&clk_hclk_mac);
2989         //clk_enable_nolock(&clk_nandc);
2990         clk_enable_nolock(&clk_hclk_usb_peri);
2991         #if 0
2992         clk_enable_nolock(&clk_hclk_otg0);
2993         clk_enable_nolock(&clk_hclk_otg1);
2994         clk_enable_nolock(&clk_hclk_hsic);
2995         clk_enable_nolock(&clk_hclk_gps);
2996         clk_enable_nolock(&clk_hclk_hsadc);
2997         clk_enable_nolock(&clk_hclk_pidfilter);
2998         clk_enable_nolock(&clk_hclk_sdmmc);
2999         clk_enable_nolock(&clk_hclk_sdio);
3000         clk_enable_nolock(&clk_hclk_emmc);
3001         #endif
3002
3003         /*************************pclk_periph***********************/
3004         clk_enable_nolock(&clk_pclk_peri_axi_matrix);
3005         #if 0
3006         clk_enable_nolock(&clk_pwm23);
3007         clk_enable_nolock(&clk_wdt);
3008         clk_enable_nolock(&clk_pclk_spi0);
3009         clk_enable_nolock(&clk_pclk_spi1);
3010         clk_enable_nolock(&clk_pclk_uart2);
3011         clk_enable_nolock(&clk_pclk_uart3);
3012         #endif
3013         #if 0
3014         clk_enable_nolock(&clk_i2c2);
3015         clk_enable_nolock(&clk_i2c3);
3016         clk_enable_nolock(&clk_i2c4);
3017         clk_enable_nolock(&clk_gpio3);
3018         clk_enable_nolock(&clk_pclk_saradc);
3019         #endif
3020         /*************************aclk_lcdc0***********************/
3021 #if 1
3022         //clk_enable_nolock(&clk_aclk_vio0);
3023         //clk_enable_nolock(&clk_aclk_lcdc0);
3024         //clk_enable_nolock(&clk_aclk_cif0);
3025         //clk_enable_nolock(&clk_aclk_ipp);
3026 #endif
3027         /*************************aclk_lcdc1***********************/
3028 #if 1
3029         //clk_enable_nolock(&clk_aclk_vio1);
3030         //clk_enable_nolock(&clk_aclk_lcdc1);
3031         //clk_enable_nolock(&clk_aclk_rga);
3032 #endif
3033         /************************power domain**********************/
3034
3035 }
3036 static void periph_clk_set_init(void)
3037 {
3038         unsigned long aclk_p, hclk_p, pclk_p;
3039         unsigned long ppll_rate = general_pll_clk.rate;
3040         //aclk 148.5
3041
3042         /* general pll */
3043         switch (ppll_rate) {
3044                 case 148500* KHZ:
3045                         aclk_p = 148500 * KHZ;
3046                         hclk_p = aclk_p >> 1;
3047                         pclk_p = aclk_p >> 2;
3048                         break;
3049                 case 1188*MHZ:
3050                         aclk_p = aclk_p >> 3; // 0
3051                         hclk_p = aclk_p >> 1;
3052                         pclk_p = aclk_p >> 2;
3053
3054                 case 297 * MHZ:
3055                         aclk_p = ppll_rate >> 1;
3056                         hclk_p = aclk_p >> 0;
3057                         pclk_p = aclk_p >> 1;
3058                         break;
3059
3060                 case 300 * MHZ:
3061                         aclk_p = ppll_rate >> 1;
3062                         hclk_p = aclk_p >> 0;
3063                         pclk_p = aclk_p >> 1;
3064                         break;
3065                 case 594 * MHZ:
3066                         aclk_p = ppll_rate >> 2;
3067                         hclk_p = aclk_p >> 0;
3068                         pclk_p = aclk_p >> 1;
3069                         break;
3070                 default:
3071                         aclk_p = 150 * MHZ;
3072                         hclk_p = 150 * MHZ;
3073                         pclk_p = 75 * MHZ;
3074                         break;
3075         }
3076         clk_set_parent_nolock(&aclk_periph, &general_pll_clk);
3077         clk_set_rate_nolock(&aclk_periph, aclk_p);
3078         clk_set_rate_nolock(&hclk_periph, hclk_p);
3079         clk_set_rate_nolock(&pclk_periph, pclk_p);
3080 }
3081
3082 static void cpu_axi_init(void)
3083 {
3084         unsigned long cpu_div_rate, aclk_cpu_rate, hclk_cpu_rate, pclk_cpu_rate, ahb2apb_cpu_rate;
3085         unsigned long gpll_rate = general_pll_clk.rate;
3086
3087         switch (gpll_rate) {
3088                 case 297 * MHZ:
3089                         cpu_div_rate = gpll_rate;
3090                         aclk_cpu_rate = cpu_div_rate >> 0;
3091                         hclk_cpu_rate = aclk_cpu_rate >> 1;
3092                         pclk_cpu_rate = aclk_cpu_rate >> 2;
3093                         break;
3094
3095                 case 594 * MHZ:
3096                         cpu_div_rate = gpll_rate >> 1;
3097                         aclk_cpu_rate = cpu_div_rate >> 0;
3098                         hclk_cpu_rate = aclk_cpu_rate >> 1;
3099                         pclk_cpu_rate = aclk_cpu_rate >> 2;
3100                         break;
3101                 default:
3102                         aclk_cpu_rate = 150 * MHZ;
3103                         hclk_cpu_rate = 150 * MHZ;
3104                         pclk_cpu_rate = 75 * MHZ;
3105                         break;
3106         }
3107         ahb2apb_cpu_rate = pclk_cpu_rate;
3108
3109         clk_set_parent_nolock(&clk_cpu_div, &general_pll_clk);
3110         clk_set_rate_nolock(&clk_cpu_div, cpu_div_rate);
3111         clk_set_rate_nolock(&aclk_cpu, aclk_cpu_rate);
3112         clk_set_rate_nolock(&hclk_cpu, hclk_cpu_rate);
3113         clk_set_rate_nolock(&pclk_cpu, pclk_cpu_rate);
3114         clk_set_rate_nolock(&ahb2apb_cpu, ahb2apb_cpu_rate);
3115 }
3116
3117 void rk30_clock_common_i2s_init(void)
3118 {
3119         unsigned long i2s_rate;
3120         //20 times
3121         if(rk30_clock_flags & CLK_FLG_MAX_I2S_49152KHZ) {
3122                 i2s_rate = 49152000;
3123         } else if(rk30_clock_flags & CLK_FLG_MAX_I2S_24576KHZ) {
3124                 i2s_rate = 24576000;
3125         } else if(rk30_clock_flags & CLK_FLG_MAX_I2S_22579_2KHZ) {
3126                 i2s_rate = 22579000;
3127         } else if(rk30_clock_flags & CLK_FLG_MAX_I2S_12288KHZ) {
3128                 i2s_rate = 12288000;
3129         } else {
3130                 i2s_rate = 49152000;
3131         }
3132
3133         if(((i2s_rate * 20) <= general_pll_clk.rate) || !(general_pll_clk.rate % i2s_rate)) {
3134                 clk_set_parent_nolock(&clk_i2s_pll, &general_pll_clk);
3135         } else if(((i2s_rate * 20) <= codec_pll_clk.rate) || !(codec_pll_clk.rate % i2s_rate)) {
3136                 clk_set_parent_nolock(&clk_i2s_pll, &codec_pll_clk);
3137         } else {
3138                 if(general_pll_clk.rate > codec_pll_clk.rate)
3139                         clk_set_parent_nolock(&clk_i2s_pll, &general_pll_clk);
3140                 else
3141                         clk_set_parent_nolock(&clk_i2s_pll, &codec_pll_clk);
3142         }
3143 }
3144
3145 static void __init rk30_clock_common_init(unsigned long gpll_rate, unsigned long cpll_rate)
3146 {
3147
3148         //general
3149         clk_set_rate_nolock(&general_pll_clk, gpll_rate);
3150         //code pll
3151         clk_set_rate_nolock(&codec_pll_clk, cpll_rate);
3152
3153         cpu_axi_init();
3154         clk_set_rate_nolock(&clk_core, 816 * MHZ);
3155         //periph clk
3156         periph_clk_set_init();
3157
3158         //i2s
3159         rk30_clock_common_i2s_init();
3160
3161         // spi
3162         clk_set_rate_nolock(&clk_spi0, clk_spi0.parent->rate);
3163         clk_set_rate_nolock(&clk_spi1, clk_spi1.parent->rate);
3164
3165         // uart
3166         if(rk30_clock_flags & CLK_FLG_UART_1_3M)
3167                 clk_set_parent_nolock(&clk_uart_pll, &codec_pll_clk);
3168         else
3169                 clk_set_parent_nolock(&clk_uart_pll, &general_pll_clk);
3170         //mac
3171         if(!(gpll_rate % (50 * MHZ)))
3172                 clk_set_parent_nolock(&clk_mac_pll_div, &general_pll_clk);
3173         else if(!(ddr_pll_clk.rate % (50 * MHZ)))
3174                 clk_set_parent_nolock(&clk_mac_pll_div, &ddr_pll_clk);
3175         else
3176                 CLKDATA_ERR("mac can't get 50mhz\n");
3177
3178         //hsadc
3179         //auto pll sel
3180         //clk_set_parent_nolock(&clk_hsadc_pll_div, &general_pll_clk);
3181
3182         //lcdc0 lcd auto sel pll
3183         clk_set_parent_nolock(&dclk_lcdc0, &general_pll_clk);
3184         clk_set_parent_nolock(&dclk_lcdc1, &general_pll_clk);
3185
3186         //cif
3187         clk_set_parent_nolock(&cif_out_pll, &general_pll_clk);
3188
3189         //axi lcdc auto sel
3190         clk_set_parent_nolock(&aclk_lcdc0_pre, &general_pll_clk);
3191         clk_set_parent_nolock(&aclk_lcdc1_pre, &general_pll_clk);
3192         clk_set_rate_nolock(&aclk_lcdc0_pre, 300 * MHZ);
3193         clk_set_rate_nolock(&aclk_lcdc1_pre, 300 * MHZ);
3194
3195         //axi vepu auto sel
3196         //clk_set_parent_nolock(&aclk_vepu, &general_pll_clk);
3197         //clk_set_parent_nolock(&aclk_vdpu, &general_pll_clk);
3198
3199         clk_set_rate_nolock(&aclk_vepu, 300 * MHZ);
3200         clk_set_rate_nolock(&aclk_vdpu, 300 * MHZ);
3201         //gpu auto sel
3202         clk_set_parent_nolock(&aclk_gpu, &codec_pll_clk);
3203         clk_set_rate_nolock(&aclk_gpu, 200 * MHZ);
3204         
3205         clk_set_rate_nolock(&clk_uart0, 49500000);
3206         clk_set_rate_nolock(&clk_sdmmc, 24750000);
3207         clk_set_rate_nolock(&clk_sdio, 24750000);
3208
3209 }
3210
3211 static struct clk def_ops_clk = {
3212         .get_parent = clksel_get_parent,
3213         .set_parent = clksel_set_parent,
3214 };
3215
3216 #ifdef CONFIG_PROC_FS
3217 struct clk_dump_ops dump_ops;
3218 #endif
3219 void rk_dump_clock_info(void);
3220
3221 void __init _rk30_clock_data_init(unsigned long gpll, unsigned long cpll, int flags)
3222 {
3223         struct clk_lookup *lk;
3224
3225         clk_register_dump_ops(&dump_ops);
3226         clk_register_default_ops_clk(&def_ops_clk);
3227         rk30_clock_flags = flags;
3228         for (lk = clks; lk < clks + ARRAY_SIZE(clks); lk++) {
3229 #ifdef RK30_CLK_OFFBOARD_TEST
3230                 rk30_clkdev_add(lk);
3231 #else
3232                 clkdev_add(lk);
3233 #endif
3234                 clk_register(lk->clk);
3235         }
3236         clk_recalculate_root_clocks_nolock();
3237
3238         loops_per_jiffy = CLK_LOOPS_RECALC(arm_pll_clk.rate);
3239
3240         /*
3241          * Only enable those clocks we will need, let the drivers
3242          * enable other clocks as necessary
3243          */
3244         rk30_init_enable_clocks();
3245 #if 0
3246         // print loader config
3247         CLKDATA_DBG("%s clks register dbg start\n", __func__);
3248         rk_dump_clock_info();
3249         rk30_clk_dump_regs();
3250
3251         CLKDATA_DBG("%s clks register dbg end\n", __func__);
3252 #endif
3253         /*
3254          * Disable any unused clocks left on by the bootloader
3255          */
3256         //clk_disable_unused();
3257         rk30_clock_common_init(gpll, cpll);
3258         preset_lpj = loops_per_jiffy;
3259
3260         //gpio6_b7
3261         //regfile_writel(0xc0004000,0x10c);
3262         //cru_writel(0x07000000,CRU_MISC_CON);
3263
3264 }
3265 extern int rk3188_dvfs_init(void);
3266
3267 void __init rk30_clock_data_init(unsigned long gpll, unsigned long cpll, u32 flags)
3268 {
3269         _rk30_clock_data_init(gpll, cpll, flags);
3270         rk3188_dvfs_init();
3271 }
3272
3273 /*
3274  * You can override arm_clk rate with armclk= cmdline option.
3275  */
3276 static int __init armclk_setup(char *str)
3277 {
3278         get_option(&str, &armclk);
3279
3280         if (!armclk)
3281                 return 0;
3282         if (armclk < 10000)
3283                 armclk *= MHZ;
3284         //clk_set_rate_nolock(&arm_pll_clk, armclk);
3285         return 0;
3286 }
3287 #ifndef RK30_CLK_OFFBOARD_TEST
3288 early_param("armclk", armclk_setup);
3289 #endif
3290
3291
3292 static void rk_dump_clock(struct clk *clk, int deep, const struct list_head *root_clocks)
3293 {
3294         struct clk *ck;
3295         int i;
3296         unsigned long rate = clk->rate;
3297         //CLKDATA_DBG("dump_clock %s\n",clk->name);
3298         for (i = 0; i < deep; i++)
3299                 printk("    ");
3300
3301         printk("%-11s ", clk->name);
3302 #ifndef RK30_CLK_OFFBOARD_TEST
3303         if (clk->flags & IS_PD) {
3304                 printk("%s ", pmu_power_domain_is_on(clk->gate_idx) ? "on " : "off");
3305         }
3306 #endif
3307         if ((clk->mode == gate_mode) && (clk->gate_idx < CLK_GATE_MAX)) {
3308                 int idx = clk->gate_idx;
3309                 u32 v;
3310                 v = cru_readl(CLK_GATE_CLKID_CONS(idx)) & ((0x1) << (idx % 16));
3311                 printk("%s ", v ? "off" : "on ");
3312         }
3313
3314         if (clk->pll) {
3315                 u32 pll_mode;
3316                 u32 pll_id = clk->pll->id;
3317                 pll_mode = cru_readl(CRU_MODE_CON)&PLL_MODE_MSK(pll_id);
3318                 if (pll_mode == (PLL_MODE_SLOW(pll_id) & PLL_MODE_MSK(pll_id)))
3319                         printk("slow   ");
3320                 else if (pll_mode == (PLL_MODE_NORM(pll_id) & PLL_MODE_MSK(pll_id)))
3321                         printk("normal ");
3322                 else if (pll_mode == (PLL_MODE_DEEP(pll_id) & PLL_MODE_MSK(pll_id)))
3323                         printk("deep   ");
3324
3325                 if(cru_readl(PLL_CONS(pll_id, 3)) & PLL_BYPASS)
3326                         printk("bypass ");
3327         } else if(clk == &clk_ddr) {
3328                 rate = clk->recalc(clk);
3329         }
3330
3331         if (rate >= MHZ) {
3332                 if (rate % MHZ)
3333                         printk("%ld.%06ld MHz", rate / MHZ, rate % MHZ);
3334                 else
3335                         printk("%ld MHz", rate / MHZ);
3336         } else if (rate >= KHZ) {
3337                 if (rate % KHZ)
3338                         printk("%ld.%03ld KHz", rate / KHZ, rate % KHZ);
3339                 else
3340                         printk("%ld KHz", rate / KHZ);
3341         } else {
3342                 printk("%ld Hz", rate);
3343         }
3344
3345         printk(" usecount = %d", clk->usecount);
3346
3347         if (clk->parent)
3348                 printk(" parent = %s", clk->parent->name);
3349
3350         printk("\n");
3351
3352         list_for_each_entry(ck, root_clocks, node) {
3353                 if (ck->parent == clk)
3354                         rk_dump_clock(ck, deep + 1, root_clocks);
3355         }
3356 }
3357
3358 #if 1
3359 struct list_head *get_rk_clocks_head(void);
3360
3361 void rk_dump_clock_info(void)
3362 {
3363         struct clk* clk;
3364         list_for_each_entry(clk, get_rk_clocks_head(), node) {
3365                 if (!clk->parent)
3366                 rk_dump_clock(clk, 0,get_rk_clocks_head());
3367         }
3368 }
3369 #endif
3370
3371 #ifdef CONFIG_PROC_FS
3372
3373 static void dump_clock(struct seq_file *s, struct clk *clk, int deep, const struct list_head *root_clocks)
3374 {
3375         struct clk *ck;
3376         int i;
3377         unsigned long rate = clk->rate;
3378         //CLKDATA_DBG("dump_clock %s\n",clk->name);
3379         for (i = 0; i < deep; i++)
3380                 seq_printf(s, "    ");
3381
3382         seq_printf(s, "%-11s ", clk->name);
3383 #ifndef RK30_CLK_OFFBOARD_TEST
3384         if (clk->flags & IS_PD) {
3385                 seq_printf(s, "%s ", pmu_power_domain_is_on(clk->gate_idx) ? "on " : "off");
3386         }
3387 #endif
3388         if ((clk->mode == gate_mode) && (clk->gate_idx < CLK_GATE_MAX)) {
3389                 int idx = clk->gate_idx;
3390                 u32 v;
3391                 v = cru_readl(CLK_GATE_CLKID_CONS(idx)) & ((0x1) << (idx % 16));
3392                 seq_printf(s, "%s ", v ? "off" : "on ");
3393         }
3394
3395         if (clk->pll) {
3396                 u32 pll_mode;
3397                 u32 pll_id = clk->pll->id;
3398                 pll_mode = cru_readl(CRU_MODE_CON)&PLL_MODE_MSK(pll_id);
3399                 if (pll_mode == (PLL_MODE_SLOW(pll_id) & PLL_MODE_MSK(pll_id)))
3400                         seq_printf(s, "slow   ");
3401                 else if (pll_mode == (PLL_MODE_NORM(pll_id) & PLL_MODE_MSK(pll_id)))
3402                         seq_printf(s, "normal ");
3403                 else if (pll_mode == (PLL_MODE_DEEP(pll_id) & PLL_MODE_MSK(pll_id)))
3404                         seq_printf(s, "deep   ");
3405
3406                 if(cru_readl(PLL_CONS(pll_id, 3)) & PLL_BYPASS)
3407                         seq_printf(s, "bypass ");
3408         } else if(clk == &clk_ddr) {
3409                 rate = clk->recalc(clk);
3410         }
3411
3412         if (rate >= MHZ) {
3413                 if (rate % MHZ)
3414                         seq_printf(s, "%ld.%06ld MHz", rate / MHZ, rate % MHZ);
3415                 else
3416                         seq_printf(s, "%ld MHz", rate / MHZ);
3417         } else if (rate >= KHZ) {
3418                 if (rate % KHZ)
3419                         seq_printf(s, "%ld.%03ld KHz", rate / KHZ, rate % KHZ);
3420                 else
3421                         seq_printf(s, "%ld KHz", rate / KHZ);
3422         } else {
3423                 seq_printf(s, "%ld Hz", rate);
3424         }
3425
3426         seq_printf(s, " usecount = %d", clk->usecount);
3427
3428         if (clk->parent)
3429                 seq_printf(s, " parent = %s", clk->parent->name);
3430
3431         seq_printf(s, "\n");
3432
3433         list_for_each_entry(ck, root_clocks, node) {
3434                 if (ck->parent == clk)
3435                         dump_clock(s, ck, deep + 1, root_clocks);
3436         }
3437 }
3438
3439 static void dump_regs(struct seq_file *s)
3440 {
3441         int i = 0;
3442         seq_printf(s, "\nPLL(id=0 apll,id=1,dpll,id=2,cpll,id=3 cpll)\n");
3443         seq_printf(s, "\nPLLRegisters:\n");
3444         for(i = 0; i < END_PLL_ID; i++) {
3445                 seq_printf(s, "pll%d        :cons:%x,%x,%x,%x\n", i,
3446                                 cru_readl(PLL_CONS(i, 0)),
3447                                 cru_readl(PLL_CONS(i, 1)),
3448                                 cru_readl(PLL_CONS(i, 2)),
3449                                 cru_readl(PLL_CONS(i, 3))
3450                           );
3451         }
3452         seq_printf(s, "MODE        :%x\n", cru_readl(CRU_MODE_CON));
3453
3454         for(i = 0; i < CRU_CLKSELS_CON_CNT; i++) {
3455                 seq_printf(s, "CLKSEL%d            :%x\n", i, cru_readl(CRU_CLKSELS_CON(i)));
3456         }
3457         for(i = 0; i < CRU_CLKGATES_CON_CNT; i++) {
3458                 seq_printf(s, "CLKGATE%d          :%x\n", i, cru_readl(CRU_CLKGATES_CON(i)));
3459         }
3460         seq_printf(s, "GLB_SRST_FST:%x\n", cru_readl(CRU_GLB_SRST_FST));
3461         seq_printf(s, "GLB_SRST_SND:%x\n", cru_readl(CRU_GLB_SRST_SND));
3462
3463         for(i = 0; i < CRU_SOFTRSTS_CON_CNT; i++) {
3464                 seq_printf(s, "CLKGATE%d          :%x\n", i, cru_readl(CRU_SOFTRSTS_CON(i)));
3465         }
3466         seq_printf(s, "CRU MISC    :%x\n", cru_readl(CRU_MISC_CON));
3467         seq_printf(s, "GLB_CNT_TH  :%x\n", cru_readl(CRU_GLB_CNT_TH));
3468
3469 }
3470
3471 void rk30_clk_dump_regs(void)
3472 {
3473         int i = 0;
3474         printk("\nPLL(id=0 apll,id=1,dpll,id=2,cpll,id=3 cpll)\n");
3475         printk("\nPLLRegisters:\n");
3476         for(i = 0; i < END_PLL_ID; i++) {
3477                 printk("pll%d        :cons:%x,%x,%x,%x\n", i,
3478                                 cru_readl(PLL_CONS(i, 0)),
3479                                 cru_readl(PLL_CONS(i, 1)),
3480                                 cru_readl(PLL_CONS(i, 2)),
3481                                 cru_readl(PLL_CONS(i, 3))
3482                       );
3483         }
3484         printk("MODE        :%x\n", cru_readl(CRU_MODE_CON));
3485
3486         for(i = 0; i < CRU_CLKSELS_CON_CNT; i++) {
3487                 printk("CLKSEL%d           :%x\n", i, cru_readl(CRU_CLKSELS_CON(i)));
3488         }
3489         for(i = 0; i < CRU_CLKGATES_CON_CNT; i++) {
3490                 printk("CLKGATE%d         :%x\n", i, cru_readl(CRU_CLKGATES_CON(i)));
3491         }
3492         printk("GLB_SRST_FST:%x\n", cru_readl(CRU_GLB_SRST_FST));
3493         printk("GLB_SRST_SND:%x\n", cru_readl(CRU_GLB_SRST_SND));
3494
3495         for(i = 0; i < CRU_SOFTRSTS_CON_CNT; i++) {
3496                 printk("SOFTRST%d         :%x\n", i, cru_readl(CRU_SOFTRSTS_CON(i)));
3497         }
3498         printk("CRU MISC    :%x\n", cru_readl(CRU_MISC_CON));
3499         printk("GLB_CNT_TH  :%x\n", cru_readl(CRU_GLB_CNT_TH));
3500
3501 }
3502
3503
3504 #ifdef CONFIG_PROC_FS
3505 static void dump_clock(struct seq_file *s, struct clk *clk, int deep, const struct list_head *root_clocks);
3506 struct clk_dump_ops dump_ops = {
3507         .dump_clk = dump_clock,
3508         .dump_regs = dump_regs,
3509 };
3510 #endif
3511
3512
3513 #endif /* CONFIG_PROC_FS */
3514
3515
3516
3517
3518 #ifdef RK30_CLK_OFFBOARD_TEST
3519 struct clk *test_get_parent(struct clk *clk) {
3520         return clk->parent;
3521 }
3522
3523 void i2s_test(void)
3524 {
3525         struct clk *i2s_clk = &clk_i2s0;
3526
3527         clk_enable_nolock(i2s_clk);
3528
3529         clk_set_rate_nolock(i2s_clk, 12288000);
3530         printk("int %s parent is %s\n", i2s_clk->name, test_get_parent(i2s_clk)->name);
3531         clk_set_rate_nolock(i2s_clk, 297 * MHZ / 2);
3532         printk("int%s parent is %s\n", i2s_clk->name, test_get_parent(i2s_clk)->name);
3533         clk_set_rate_nolock(i2s_clk, 12 * MHZ);
3534         printk("int%s parent is %s\n", i2s_clk->name, test_get_parent(i2s_clk)->name);
3535
3536 }
3537
3538 void uart_test(void)
3539 {
3540         struct clk *uart_clk = &clk_uart0;
3541
3542         clk_enable_nolock(uart_clk);
3543
3544         clk_set_rate_nolock(uart_clk, 12288000);
3545         printk("int %s parent is %s\n", uart_clk->name, test_get_parent(uart_clk)->name);
3546         clk_set_rate_nolock(uart_clk, 297 * MHZ / 2);
3547         printk("int%s parent is %s\n", uart_clk->name, test_get_parent(uart_clk)->name);
3548         clk_set_rate_nolock(uart_clk, 12 * MHZ);
3549         printk("int%s parent is %s\n", uart_clk->name, test_get_parent(uart_clk)->name);
3550
3551 }
3552 void hsadc_test(void)
3553 {
3554         struct clk *hsadc_clk = &clk_hsadc;
3555
3556         printk("******************hsadc_test**********************\n");
3557         clk_enable_nolock(hsadc_clk);
3558
3559         clk_set_rate_nolock(hsadc_clk, 12288000);
3560         printk("****end %s parent is %s\n", hsadc_clk->name, test_get_parent(hsadc_clk)->name);
3561
3562
3563         clk_set_rate_nolock(hsadc_clk, 297 * MHZ / 2);
3564         printk("****end %s parent is %s\n", hsadc_clk->name, test_get_parent(hsadc_clk)->name);
3565
3566         clk_set_rate_nolock(hsadc_clk, 300 * MHZ / 2);
3567
3568         clk_set_rate_nolock(hsadc_clk, 296 * MHZ / 2);
3569
3570         printk("******************hsadc out clock**********************\n");
3571
3572         clk_set_parent_nolock(hsadc_clk, &clk_hsadc_ext);
3573         printk("****end %s parent is %s\n", hsadc_clk->name, test_get_parent(hsadc_clk)->name);
3574         clk_set_rate_nolock(hsadc_clk, 297 * MHZ / 2);
3575         printk("****end %s parent is %s\n", hsadc_clk->name, test_get_parent(hsadc_clk)->name);
3576
3577
3578
3579 }
3580
3581 static void __init rk30_clock_test_init(unsigned long ppll_rate)
3582 {
3583         //arm
3584         printk("*********arm_pll_clk***********\n");
3585         clk_set_rate_nolock(&arm_pll_clk, 816 * MHZ);
3586
3587         printk("*********set clk_core parent***********\n");
3588         clk_set_parent_nolock(&clk_core, &arm_pll_clk);
3589         clk_set_rate_nolock(&clk_core, 504 * MHZ);
3590
3591         //general
3592         printk("*********general_pll_clk***********\n");
3593         clk_set_rate_nolock(&general_pll_clk, ppll_rate);
3594
3595         //code pll
3596         printk("*********codec_pll_clk***********\n");
3597         clk_set_rate_nolock(&codec_pll_clk, 600 * MHZ);
3598
3599
3600         printk("*********periph_clk_set_init***********\n");
3601         clk_set_parent_nolock(&aclk_periph, &general_pll_clk);
3602         periph_clk_set_init();
3603
3604 #if 0 //
3605         clk_set_parent_nolock(&clk_i2s_pll, &codec_pll_clk);
3606 #else
3607         printk("*********clk i2s***********\n");
3608         clk_set_parent_nolock(&clk_i2s_pll, &general_pll_clk);
3609         printk("common %s parent is %s\n", clk_i2s_pll.name, test_get_parent(&clk_i2s_pll)->name);
3610         i2s_test();
3611 #endif
3612         // spi
3613         clk_enable_nolock(&clk_spi0);
3614         clk_set_rate_nolock(&clk_spi0, 30 * MHZ);
3615         printk("common %s parent is %s\n", clk_spi0.name, test_get_parent(&clk_spi0)->name);
3616         //saradc
3617         clk_enable_nolock(&clk_saradc);
3618         clk_set_rate_nolock(&clk_saradc, 6 * MHZ);
3619         printk("common %s parent is %s\n", clk_saradc.name, test_get_parent(&clk_saradc)->name);
3620         //sdio
3621         clk_enable_nolock(&clk_sdio);
3622         clk_set_rate_nolock(&clk_sdio, 50 * MHZ);
3623         printk("common %s parent is %s\n", clk_sdio.name, test_get_parent(&clk_sdio)->name);
3624         // uart
3625         clk_set_parent_nolock(&clk_uart_pll, &general_pll_clk);
3626         uart_test();
3627         //mac
3628         printk("*********mac***********\n");
3629
3630         clk_set_parent_nolock(&clk_mac_pll_div, &general_pll_clk);
3631         printk("common %s parent is %s\n", clk_mac_pll_div.name, test_get_parent(&clk_mac_pll_div)->name);
3632
3633         //clk_set_parent_nolock(&clk_mac_ref, &clk_mac_pll_div);
3634         clk_set_rate_nolock(&clk_mac_ref, 50 * MHZ);
3635         printk("common %s parent is %s\n", clk_mac_ref.name, test_get_parent(&clk_mac_ref)->name);
3636
3637         printk("*********mac mii set***********\n");
3638         clk_set_parent_nolock(&clk_mac_ref, &rmii_clkin);
3639         clk_set_rate_nolock(&clk_mac_ref, 20 * MHZ);
3640         printk("common %s parent is %s\n", clk_mac_ref.name, test_get_parent(&clk_mac_ref)->name);
3641         //hsadc
3642         printk("*********hsadc 1***********\n");
3643         //auto pll
3644         hsadc_test();
3645         //lcdc
3646         clk_enable_nolock(&dclk_lcdc0);
3647
3648         clk_set_rate_nolock(&dclk_lcdc0, 60 * MHZ);
3649         clk_set_rate_nolock(&dclk_lcdc0, 27 * MHZ);
3650
3651         //cif
3652         clk_enable_nolock(&cif0_out);
3653
3654         clk_set_parent_nolock(&cif_out_pll, &general_pll_clk);
3655         printk("common %s parent is %s\n", cif_out_pll.name, test_get_parent(&cif_out_pll)->name);
3656
3657         clk_set_rate_nolock(&cif0_out, 60 * MHZ);
3658         printk("common %s parent is %s\n", cif0_out.name, test_get_parent(&cif0_out)->name);
3659
3660         clk_set_rate_nolock(&cif0_out, 24 * MHZ);
3661         printk("common %s parent is %s\n", cif0_out.name, test_get_parent(&cif0_out)->name);
3662         //cif_in
3663         clk_enable_nolock(&cif0_in);
3664         clk_set_rate_nolock(&cif0_in, 24 * MHZ);
3665         //axi lcdc
3666         clk_enable_nolock(&aclk_lcdc0);
3667         clk_set_rate_nolock(&aclk_lcdc0, 150 * MHZ);
3668         printk("common %s parent is %s\n", aclk_lcdc0.name, test_get_parent(&aclk_lcdc0)->name);
3669         //axi vepu
3670         clk_enable_nolock(&aclk_vepu);
3671         clk_set_rate_nolock(&aclk_vepu, 300 * MHZ);
3672         printk("common %s parent is %s\n", aclk_vepu.name, test_get_parent(&aclk_vepu)->name);
3673
3674         clk_set_rate_nolock(&hclk_vepu, 300 * MHZ);
3675         printk("common %s parent is %s\n", hclk_vepu.name, test_get_parent(&hclk_vepu)->name);
3676
3677         printk("test end\n");
3678
3679         /* arm pll
3680            clk_set_rate_nolock(&arm_pll_clk, armclk);
3681            clk_set_rate_nolock(&clk_core,       armclk);//pll:core =1:1
3682            */
3683         //
3684         //clk_set_rate_nolock(&codec_pll_clk, ppll_rate*2);
3685         //
3686         //clk_set_rate_nolock(&aclk_vepu, 300 * MHZ);
3687         //clk_set_rate_nolock(&clk_gpu, 300 * MHZ);
3688
3689 }
3690
3691
3692
3693
3694
3695 static LIST_HEAD(rk30_clocks);
3696 static DEFINE_MUTEX(rk30_clocks_mutex);
3697
3698 static inline int __rk30clk_get(struct clk *clk)
3699 {
3700         return 1;
3701 }
3702 void rk30_clkdev_add(struct clk_lookup *cl)
3703 {
3704         mutex_lock(&rk30_clocks_mutex);
3705         list_add_tail(&cl->node, &rk30_clocks);
3706         mutex_unlock(&rk30_clocks_mutex);
3707 }
3708 static struct clk_lookup *rk30_clk_find(const char *dev_id, const char *con_id) {
3709         struct clk_lookup *p, *cl = NULL;
3710         int match, best = 0;
3711
3712         list_for_each_entry(p, &rk30_clocks, node) {
3713                 match = 0;
3714                 if (p->dev_id) {
3715                         if (!dev_id || strcmp(p->dev_id, dev_id))
3716                                 continue;
3717                         match += 2;
3718                 }
3719                 if (p->con_id) {
3720                         if (!con_id || strcmp(p->con_id, con_id))
3721                                 continue;
3722                         match += 1;
3723                 }
3724
3725                 if (match > best) {
3726                         cl = p;
3727                         if (match != 3)
3728                                 best = match;
3729                         else
3730                                 break;
3731                 }
3732         }
3733         return cl;
3734 }
3735
3736 struct clk *rk30_clk_get_sys(const char *dev_id, const char *con_id) {
3737         struct clk_lookup *cl;
3738
3739         mutex_lock(&rk30_clocks_mutex);
3740         cl = rk30_clk_find(dev_id, con_id);
3741         if (cl && !__rk30clk_get(cl->clk))
3742                 cl = NULL;
3743         mutex_unlock(&rk30_clocks_mutex);
3744
3745         return cl ? cl->clk : ERR_PTR(-ENOENT);
3746 }
3747 //EXPORT_SYMBOL(rk30_clk_get_sys);
3748
3749 struct clk *rk30_clk_get(struct device *dev, const char *con_id) {
3750         const char *dev_id = dev ? dev_name(dev) : NULL;
3751         return rk30_clk_get_sys(dev_id, con_id);
3752 }
3753 //EXPORT_SYMBOL(rk30_clk_get);
3754
3755
3756 int rk30_clk_set_rate(struct clk *clk, unsigned long rate);
3757
3758 void rk30_clocks_test(void)
3759 {
3760         struct clk *test_gpll;
3761         test_gpll = rk30_clk_get(NULL, "general_pll");
3762         if(test_gpll) {
3763                 rk30_clk_set_rate(test_gpll, 297 * 2 * MHZ);
3764                 printk("gpll rate=%lu\n", test_gpll->rate);
3765         }
3766         //while(1);
3767 }
3768
3769 void __init rk30_clock_init_test(void)
3770 {
3771
3772         rk30_clock_init(periph_pll_297mhz, codec_pll_360mhz, max_i2s_12288khz);
3773         //while(1);
3774 }
3775
3776
3777 #endif
3778
3779