]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/mach-keystone/clock.c
ARM: keystone2: Cleanup PLL init code
[karo-tx-uboot.git] / arch / arm / mach-keystone / clock.c
1 /*
2  * Keystone2: pll initialization
3  *
4  * (C) Copyright 2012-2014
5  *     Texas Instruments Incorporated, <www.ti.com>
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9
10 #include <common.h>
11 #include <asm/arch/clock.h>
12 #include <asm/arch/clock_defs.h>
13
14 #define MAX_SPEEDS              13
15
16 static void wait_for_completion(const struct pll_init_data *data)
17 {
18         int i;
19         for (i = 0; i < 100; i++) {
20                 sdelay(450);
21                 if (!(pllctl_reg_read(data->pll, stat) & PLLSTAT_GOSTAT_MASK))
22                         break;
23         }
24 }
25
26 static inline void bypass_main_pll(const struct pll_init_data *data)
27 {
28         pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLENSRC_MASK |
29                            PLLCTL_PLLEN_MASK);
30
31         /* 4 cycles of reference clock CLKIN*/
32         sdelay(340);
33 }
34
35 static void configure_mult_div(const struct pll_init_data *data)
36 {
37         u32 pllm, plld, bwadj;
38
39         pllm = data->pll_m - 1;
40         plld = (data->pll_d - 1) & CFG_PLLCTL0_PLLD_MASK;
41
42         /* Program Multiplier */
43         if (data->pll == MAIN_PLL)
44                 pllctl_reg_write(data->pll, mult, pllm & PLLM_MULT_LO_MASK);
45
46         clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
47                         CFG_PLLCTL0_PLLM_MASK,
48                         pllm << CFG_PLLCTL0_PLLM_SHIFT);
49
50         /* Program BWADJ */
51         bwadj = (data->pll_m - 1) >> 1; /* Divide pllm by 2 */
52         clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
53                         CFG_PLLCTL0_BWADJ_MASK,
54                         (bwadj << CFG_PLLCTL0_BWADJ_SHIFT) &
55                         CFG_PLLCTL0_BWADJ_MASK);
56         bwadj = bwadj >> CFG_PLLCTL0_BWADJ_BITS;
57         clrsetbits_le32(keystone_pll_regs[data->pll].reg1,
58                         CFG_PLLCTL1_BWADJ_MASK, bwadj);
59
60         /* Program Divider */
61         clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
62                         CFG_PLLCTL0_PLLD_MASK, plld);
63 }
64
65 void configure_main_pll(const struct pll_init_data *data)
66 {
67         u32 tmp, pllod, i, alnctl_val = 0;
68         u32 *offset;
69
70         pllod = data->pll_od - 1;
71
72         /* 100 micro sec for stabilization */
73         sdelay(210000);
74
75         tmp = pllctl_reg_read(data->pll, secctl);
76
77         /* Check for Bypass */
78         if (tmp & SECCTL_BYPASS_MASK) {
79                 setbits_le32(keystone_pll_regs[data->pll].reg1,
80                              CFG_PLLCTL1_ENSAT_MASK);
81
82                 bypass_main_pll(data);
83
84                 /* Powerdown and powerup Main Pll */
85                 pllctl_reg_setbits(data->pll, secctl, SECCTL_BYPASS_MASK);
86                 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLPWRDN_MASK);
87                 /* 5 micro sec */
88                 sdelay(21000);
89
90                 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLPWRDN_MASK);
91         } else {
92                 bypass_main_pll(data);
93         }
94
95         configure_mult_div(data);
96
97         /* Program Output Divider */
98         pllctl_reg_rmw(data->pll, secctl, SECCTL_OP_DIV_MASK,
99                        ((pllod << SECCTL_OP_DIV_SHIFT) & SECCTL_OP_DIV_MASK));
100
101         /* Program PLLDIVn */
102         wait_for_completion(data);
103         for (i = 0; i < PLLDIV_MAX; i++) {
104                 if (i < 3)
105                         offset = pllctl_reg(data->pll, div1) + i;
106                 else
107                         offset = pllctl_reg(data->pll, div4) + (i - 3);
108
109                 if (divn_val[i] != -1) {
110                         __raw_writel(divn_val[i] | PLLDIV_ENABLE_MASK, offset);
111                         alnctl_val |= BIT(i);
112                 }
113         }
114
115         if (alnctl_val) {
116                 pllctl_reg_setbits(data->pll, alnctl, alnctl_val);
117                 /*
118                  * Set GOSET bit in PLLCMD to initiate the GO operation
119                  * to change the divide
120                  */
121                 pllctl_reg_setbits(data->pll, cmd, PLLSTAT_GOSTAT_MASK);
122                 wait_for_completion(data);
123         }
124
125         /* Reset PLL */
126         pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLRST_MASK);
127         sdelay(21000);  /* Wait for a minimum of 7 us*/
128         pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLRST_MASK);
129         sdelay(105000); /* Wait for PLL Lock time (min 50 us) */
130
131         /* Enable PLL */
132         pllctl_reg_clrbits(data->pll, secctl, SECCTL_BYPASS_MASK);
133         pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLEN_MASK);
134 }
135
136 void configure_secondary_pll(const struct pll_init_data *data)
137 {
138         int pllod = data->pll_od - 1;
139
140         /* Enable Bypass mode */
141         setbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_ENSAT_MASK);
142         setbits_le32(keystone_pll_regs[data->pll].reg0,
143                      CFG_PLLCTL0_BYPASS_MASK);
144
145         /* Enable Glitch free bypass for ARM PLL */
146         if (cpu_is_k2hk() && data->pll == TETRIS_PLL)
147                 clrbits_le32(KS2_MISC_CTRL, MISC_CTL1_ARM_PLL_EN);
148
149         configure_mult_div(data);
150
151         /* Program Output Divider */
152         clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
153                         CFG_PLLCTL0_CLKOD_MASK,
154                         (pllod << CFG_PLLCTL0_CLKOD_SHIFT) &
155                         CFG_PLLCTL0_CLKOD_MASK);
156
157         /* Reset PLL */
158         setbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
159         /* Wait for 5 micro seconds */
160         sdelay(21000);
161
162         /* Select the Output of PASS PLL as input to PASS */
163         if (data->pll == PASS_PLL)
164                 setbits_le32(keystone_pll_regs[data->pll].reg1,
165                              CFG_PLLCTL1_PAPLL_MASK);
166
167         /* Select the Output of ARM PLL as input to ARM */
168         if (data->pll == TETRIS_PLL)
169                 setbits_le32(KS2_MISC_CTRL, MISC_CTL1_ARM_PLL_EN);
170
171         clrbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
172         /* Wait for 500 * REFCLK cucles * (PLLD + 1) */
173         sdelay(105000);
174
175         /* Switch to PLL mode */
176         clrbits_le32(keystone_pll_regs[data->pll].reg0,
177                      CFG_PLLCTL0_BYPASS_MASK);
178 }
179
180 void init_pll(const struct pll_init_data *data)
181 {
182         if (data->pll == MAIN_PLL)
183                 configure_main_pll(data);
184         else
185                 configure_secondary_pll(data);
186
187         /*
188          * This is required to provide a delay between multiple
189          * consequent PPL configurations
190          */
191         sdelay(210000);
192 }
193
194 void init_plls(int num_pll, struct pll_init_data *config)
195 {
196         int i;
197
198         for (i = 0; i < num_pll; i++)
199                 init_pll(&config[i]);
200 }
201
202 static int get_max_speed(u32 val, int *speeds)
203 {
204         int j;
205
206         if (!val)
207                 return speeds[0];
208
209         for (j = 1; j < MAX_SPEEDS; j++) {
210                 if (val == 1)
211                         return speeds[j];
212                 val >>= 1;
213         }
214
215         return SPD800;
216 }
217
218 #ifdef CONFIG_SOC_K2HK
219 static u32 read_efuse_bootrom(void)
220 {
221         return (cpu_revision() > 1) ? __raw_readl(KS2_EFUSE_BOOTROM) :
222                 __raw_readl(KS2_REV1_DEVSPEED);
223 }
224 #else
225 static inline u32 read_efuse_bootrom(void)
226 {
227         return __raw_readl(KS2_EFUSE_BOOTROM);
228 }
229 #endif
230
231 #ifndef CONFIG_SOC_K2E
232 inline int get_max_arm_speed(void)
233 {
234         return get_max_speed(read_efuse_bootrom() & 0xffff, arm_speeds);
235 }
236 #endif
237
238 inline int get_max_dev_speed(void)
239 {
240         return get_max_speed((read_efuse_bootrom() >> 16) & 0xffff, dev_speeds);
241 }