Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tegra30_clocks.c
Go to the documentation of this file.
1 /*
2  * arch/arm/mach-tegra/tegra30_clocks.c
3  *
4  * Copyright (c) 2010-2012 NVIDIA CORPORATION. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  */
20 
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
25 #include <linux/delay.h>
26 #include <linux/err.h>
27 #include <linux/io.h>
28 #include <linux/clk.h>
29 #include <linux/cpufreq.h>
30 #include <linux/syscore_ops.h>
31 
32 #include <asm/clkdev.h>
33 
34 #include <mach/iomap.h>
35 
36 #include "clock.h"
37 #include "fuse.h"
38 #include "tegra_cpu_car.h"
39 
40 #define USE_PLL_LOCK_BITS 0
41 
42 #define RST_DEVICES_L 0x004
43 #define RST_DEVICES_H 0x008
44 #define RST_DEVICES_U 0x00C
45 #define RST_DEVICES_V 0x358
46 #define RST_DEVICES_W 0x35C
47 #define RST_DEVICES_SET_L 0x300
48 #define RST_DEVICES_CLR_L 0x304
49 #define RST_DEVICES_SET_V 0x430
50 #define RST_DEVICES_CLR_V 0x434
51 #define RST_DEVICES_NUM 5
52 
53 #define CLK_OUT_ENB_L 0x010
54 #define CLK_OUT_ENB_H 0x014
55 #define CLK_OUT_ENB_U 0x018
56 #define CLK_OUT_ENB_V 0x360
57 #define CLK_OUT_ENB_W 0x364
58 #define CLK_OUT_ENB_SET_L 0x320
59 #define CLK_OUT_ENB_CLR_L 0x324
60 #define CLK_OUT_ENB_SET_V 0x440
61 #define CLK_OUT_ENB_CLR_V 0x444
62 #define CLK_OUT_ENB_NUM 5
63 
64 #define RST_DEVICES_V_SWR_CPULP_RST_DIS (0x1 << 1)
65 #define CLK_OUT_ENB_V_CLK_ENB_CPULP_EN (0x1 << 1)
66 
67 #define PERIPH_CLK_TO_BIT(c) (1 << (c->u.periph.clk_num % 32))
68 #define PERIPH_CLK_TO_RST_REG(c) \
69  periph_clk_to_reg((c), RST_DEVICES_L, RST_DEVICES_V, 4)
70 #define PERIPH_CLK_TO_RST_SET_REG(c) \
71  periph_clk_to_reg((c), RST_DEVICES_SET_L, RST_DEVICES_SET_V, 8)
72 #define PERIPH_CLK_TO_RST_CLR_REG(c) \
73  periph_clk_to_reg((c), RST_DEVICES_CLR_L, RST_DEVICES_CLR_V, 8)
74 
75 #define PERIPH_CLK_TO_ENB_REG(c) \
76  periph_clk_to_reg((c), CLK_OUT_ENB_L, CLK_OUT_ENB_V, 4)
77 #define PERIPH_CLK_TO_ENB_SET_REG(c) \
78  periph_clk_to_reg((c), CLK_OUT_ENB_SET_L, CLK_OUT_ENB_SET_V, 8)
79 #define PERIPH_CLK_TO_ENB_CLR_REG(c) \
80  periph_clk_to_reg((c), CLK_OUT_ENB_CLR_L, CLK_OUT_ENB_CLR_V, 8)
81 
82 #define CLK_MASK_ARM 0x44
83 #define MISC_CLK_ENB 0x48
84 
85 #define OSC_CTRL 0x50
86 #define OSC_CTRL_OSC_FREQ_MASK (0xF<<28)
87 #define OSC_CTRL_OSC_FREQ_13MHZ (0x0<<28)
88 #define OSC_CTRL_OSC_FREQ_19_2MHZ (0x4<<28)
89 #define OSC_CTRL_OSC_FREQ_12MHZ (0x8<<28)
90 #define OSC_CTRL_OSC_FREQ_26MHZ (0xC<<28)
91 #define OSC_CTRL_OSC_FREQ_16_8MHZ (0x1<<28)
92 #define OSC_CTRL_OSC_FREQ_38_4MHZ (0x5<<28)
93 #define OSC_CTRL_OSC_FREQ_48MHZ (0x9<<28)
94 #define OSC_CTRL_MASK (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
95 
96 #define OSC_CTRL_PLL_REF_DIV_MASK (3<<26)
97 #define OSC_CTRL_PLL_REF_DIV_1 (0<<26)
98 #define OSC_CTRL_PLL_REF_DIV_2 (1<<26)
99 #define OSC_CTRL_PLL_REF_DIV_4 (2<<26)
100 
101 #define OSC_FREQ_DET 0x58
102 #define OSC_FREQ_DET_TRIG (1<<31)
103 
104 #define OSC_FREQ_DET_STATUS 0x5C
105 #define OSC_FREQ_DET_BUSY (1<<31)
106 #define OSC_FREQ_DET_CNT_MASK 0xFFFF
107 
108 #define PERIPH_CLK_SOURCE_I2S1 0x100
109 #define PERIPH_CLK_SOURCE_EMC 0x19c
110 #define PERIPH_CLK_SOURCE_OSC 0x1fc
111 #define PERIPH_CLK_SOURCE_NUM1 \
112  ((PERIPH_CLK_SOURCE_OSC - PERIPH_CLK_SOURCE_I2S1) / 4)
113 
114 #define PERIPH_CLK_SOURCE_G3D2 0x3b0
115 #define PERIPH_CLK_SOURCE_SE 0x42c
116 #define PERIPH_CLK_SOURCE_NUM2 \
117  ((PERIPH_CLK_SOURCE_SE - PERIPH_CLK_SOURCE_G3D2) / 4 + 1)
118 
119 #define AUDIO_DLY_CLK 0x49c
120 #define AUDIO_SYNC_CLK_SPDIF 0x4b4
121 #define PERIPH_CLK_SOURCE_NUM3 \
122  ((AUDIO_SYNC_CLK_SPDIF - AUDIO_DLY_CLK) / 4 + 1)
123 
124 #define PERIPH_CLK_SOURCE_NUM (PERIPH_CLK_SOURCE_NUM1 + \
125  PERIPH_CLK_SOURCE_NUM2 + \
126  PERIPH_CLK_SOURCE_NUM3)
127 
128 #define CPU_SOFTRST_CTRL 0x380
129 
130 #define PERIPH_CLK_SOURCE_DIVU71_MASK 0xFF
131 #define PERIPH_CLK_SOURCE_DIVU16_MASK 0xFFFF
132 #define PERIPH_CLK_SOURCE_DIV_SHIFT 0
133 #define PERIPH_CLK_SOURCE_DIVIDLE_SHIFT 8
134 #define PERIPH_CLK_SOURCE_DIVIDLE_VAL 50
135 #define PERIPH_CLK_UART_DIV_ENB (1<<24)
136 #define PERIPH_CLK_VI_SEL_EX_SHIFT 24
137 #define PERIPH_CLK_VI_SEL_EX_MASK (0x3<<PERIPH_CLK_VI_SEL_EX_SHIFT)
138 #define PERIPH_CLK_NAND_DIV_EX_ENB (1<<8)
139 #define PERIPH_CLK_DTV_POLARITY_INV (1<<25)
140 
141 #define AUDIO_SYNC_SOURCE_MASK 0x0F
142 #define AUDIO_SYNC_DISABLE_BIT 0x10
143 #define AUDIO_SYNC_TAP_NIBBLE_SHIFT(c) ((c->reg_shift - 24) * 4)
144 
145 #define PLL_BASE 0x0
146 #define PLL_BASE_BYPASS (1<<31)
147 #define PLL_BASE_ENABLE (1<<30)
148 #define PLL_BASE_REF_ENABLE (1<<29)
149 #define PLL_BASE_OVERRIDE (1<<28)
150 #define PLL_BASE_LOCK (1<<27)
151 #define PLL_BASE_DIVP_MASK (0x7<<20)
152 #define PLL_BASE_DIVP_SHIFT 20
153 #define PLL_BASE_DIVN_MASK (0x3FF<<8)
154 #define PLL_BASE_DIVN_SHIFT 8
155 #define PLL_BASE_DIVM_MASK (0x1F)
156 #define PLL_BASE_DIVM_SHIFT 0
157 
158 #define PLL_OUT_RATIO_MASK (0xFF<<8)
159 #define PLL_OUT_RATIO_SHIFT 8
160 #define PLL_OUT_OVERRIDE (1<<2)
161 #define PLL_OUT_CLKEN (1<<1)
162 #define PLL_OUT_RESET_DISABLE (1<<0)
163 
164 #define PLL_MISC(c) \
165  (((c)->flags & PLL_ALT_MISC_REG) ? 0x4 : 0xc)
166 #define PLL_MISC_LOCK_ENABLE(c) \
167  (((c)->flags & (PLLU | PLLD)) ? (1<<22) : (1<<18))
168 
169 #define PLL_MISC_DCCON_SHIFT 20
170 #define PLL_MISC_CPCON_SHIFT 8
171 #define PLL_MISC_CPCON_MASK (0xF<<PLL_MISC_CPCON_SHIFT)
172 #define PLL_MISC_LFCON_SHIFT 4
173 #define PLL_MISC_LFCON_MASK (0xF<<PLL_MISC_LFCON_SHIFT)
174 #define PLL_MISC_VCOCON_SHIFT 0
175 #define PLL_MISC_VCOCON_MASK (0xF<<PLL_MISC_VCOCON_SHIFT)
176 #define PLLD_MISC_CLKENABLE (1<<30)
177 
178 #define PLLU_BASE_POST_DIV (1<<20)
179 
180 #define PLLD_BASE_DSIB_MUX_SHIFT 25
181 #define PLLD_BASE_DSIB_MUX_MASK (1<<PLLD_BASE_DSIB_MUX_SHIFT)
182 #define PLLD_BASE_CSI_CLKENABLE (1<<26)
183 #define PLLD_MISC_DSI_CLKENABLE (1<<30)
184 #define PLLD_MISC_DIV_RST (1<<23)
185 #define PLLD_MISC_DCCON_SHIFT 12
186 
187 #define PLLDU_LFCON_SET_DIVN 600
188 
189 /* FIXME: OUT_OF_TABLE_CPCON per pll */
190 #define OUT_OF_TABLE_CPCON 0x8
191 
192 #define SUPER_CLK_MUX 0x00
193 #define SUPER_STATE_SHIFT 28
194 #define SUPER_STATE_MASK (0xF << SUPER_STATE_SHIFT)
195 #define SUPER_STATE_STANDBY (0x0 << SUPER_STATE_SHIFT)
196 #define SUPER_STATE_IDLE (0x1 << SUPER_STATE_SHIFT)
197 #define SUPER_STATE_RUN (0x2 << SUPER_STATE_SHIFT)
198 #define SUPER_STATE_IRQ (0x3 << SUPER_STATE_SHIFT)
199 #define SUPER_STATE_FIQ (0x4 << SUPER_STATE_SHIFT)
200 #define SUPER_LP_DIV2_BYPASS (0x1 << 16)
201 #define SUPER_SOURCE_MASK 0xF
202 #define SUPER_FIQ_SOURCE_SHIFT 12
203 #define SUPER_IRQ_SOURCE_SHIFT 8
204 #define SUPER_RUN_SOURCE_SHIFT 4
205 #define SUPER_IDLE_SOURCE_SHIFT 0
206 
207 #define SUPER_CLK_DIVIDER 0x04
208 #define SUPER_CLOCK_DIV_U71_SHIFT 16
209 #define SUPER_CLOCK_DIV_U71_MASK (0xff << SUPER_CLOCK_DIV_U71_SHIFT)
210 /* guarantees safe cpu backup */
211 #define SUPER_CLOCK_DIV_U71_MIN 0x2
212 
213 #define BUS_CLK_DISABLE (1<<3)
214 #define BUS_CLK_DIV_MASK 0x3
215 
216 #define PMC_CTRL 0x0
217  #define PMC_CTRL_BLINK_ENB (1 << 7)
218 
219 #define PMC_DPD_PADS_ORIDE 0x1c
220  #define PMC_DPD_PADS_ORIDE_BLINK_ENB (1 << 20)
221 
222 #define PMC_BLINK_TIMER_DATA_ON_SHIFT 0
223 #define PMC_BLINK_TIMER_DATA_ON_MASK 0x7fff
224 #define PMC_BLINK_TIMER_ENB (1 << 15)
225 #define PMC_BLINK_TIMER_DATA_OFF_SHIFT 16
226 #define PMC_BLINK_TIMER_DATA_OFF_MASK 0xffff
227 
228 #define PMC_PLLP_WB0_OVERRIDE 0xf8
229 #define PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE (1 << 12)
230 
231 #define UTMIP_PLL_CFG2 0x488
232 #define UTMIP_PLL_CFG2_STABLE_COUNT(x) (((x) & 0xfff) << 6)
233 #define UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(x) (((x) & 0x3f) << 18)
234 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN (1 << 0)
235 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN (1 << 2)
236 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN (1 << 4)
237 
238 #define UTMIP_PLL_CFG1 0x484
239 #define UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
240 #define UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
241 #define UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN (1 << 14)
242 #define UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN (1 << 12)
243 #define UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN (1 << 16)
244 
245 #define PLLE_BASE_CML_ENABLE (1<<31)
246 #define PLLE_BASE_ENABLE (1<<30)
247 #define PLLE_BASE_DIVCML_SHIFT 24
248 #define PLLE_BASE_DIVCML_MASK (0xf<<PLLE_BASE_DIVCML_SHIFT)
249 #define PLLE_BASE_DIVP_SHIFT 16
250 #define PLLE_BASE_DIVP_MASK (0x3f<<PLLE_BASE_DIVP_SHIFT)
251 #define PLLE_BASE_DIVN_SHIFT 8
252 #define PLLE_BASE_DIVN_MASK (0xFF<<PLLE_BASE_DIVN_SHIFT)
253 #define PLLE_BASE_DIVM_SHIFT 0
254 #define PLLE_BASE_DIVM_MASK (0xFF<<PLLE_BASE_DIVM_SHIFT)
255 #define PLLE_BASE_DIV_MASK \
256  (PLLE_BASE_DIVCML_MASK | PLLE_BASE_DIVP_MASK | \
257  PLLE_BASE_DIVN_MASK | PLLE_BASE_DIVM_MASK)
258 #define PLLE_BASE_DIV(m, n, p, cml) \
259  (((cml)<<PLLE_BASE_DIVCML_SHIFT) | ((p)<<PLLE_BASE_DIVP_SHIFT) | \
260  ((n)<<PLLE_BASE_DIVN_SHIFT) | ((m)<<PLLE_BASE_DIVM_SHIFT))
261 
262 #define PLLE_MISC_SETUP_BASE_SHIFT 16
263 #define PLLE_MISC_SETUP_BASE_MASK (0xFFFF<<PLLE_MISC_SETUP_BASE_SHIFT)
264 #define PLLE_MISC_READY (1<<15)
265 #define PLLE_MISC_LOCK (1<<11)
266 #define PLLE_MISC_LOCK_ENABLE (1<<9)
267 #define PLLE_MISC_SETUP_EX_SHIFT 2
268 #define PLLE_MISC_SETUP_EX_MASK (0x3<<PLLE_MISC_SETUP_EX_SHIFT)
269 #define PLLE_MISC_SETUP_MASK \
270  (PLLE_MISC_SETUP_BASE_MASK | PLLE_MISC_SETUP_EX_MASK)
271 #define PLLE_MISC_SETUP_VALUE \
272  ((0x7<<PLLE_MISC_SETUP_BASE_SHIFT) | (0x0<<PLLE_MISC_SETUP_EX_SHIFT))
273 
274 #define PLLE_SS_CTRL 0x68
275 #define PLLE_SS_INCINTRV_SHIFT 24
276 #define PLLE_SS_INCINTRV_MASK (0x3f<<PLLE_SS_INCINTRV_SHIFT)
277 #define PLLE_SS_INC_SHIFT 16
278 #define PLLE_SS_INC_MASK (0xff<<PLLE_SS_INC_SHIFT)
279 #define PLLE_SS_MAX_SHIFT 0
280 #define PLLE_SS_MAX_MASK (0x1ff<<PLLE_SS_MAX_SHIFT)
281 #define PLLE_SS_COEFFICIENTS_MASK \
282  (PLLE_SS_INCINTRV_MASK | PLLE_SS_INC_MASK | PLLE_SS_MAX_MASK)
283 #define PLLE_SS_COEFFICIENTS_12MHZ \
284  ((0x18<<PLLE_SS_INCINTRV_SHIFT) | (0x1<<PLLE_SS_INC_SHIFT) | \
285  (0x24<<PLLE_SS_MAX_SHIFT))
286 #define PLLE_SS_DISABLE ((1<<12) | (1<<11) | (1<<10))
287 
288 #define PLLE_AUX 0x48c
289 #define PLLE_AUX_PLLP_SEL (1<<2)
290 #define PLLE_AUX_CML_SATA_ENABLE (1<<1)
291 #define PLLE_AUX_CML_PCIE_ENABLE (1<<0)
292 
293 #define PMC_SATA_PWRGT 0x1ac
294 #define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE (1<<5)
295 #define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL (1<<4)
296 
297 #define ROUND_DIVIDER_UP 0
298 #define ROUND_DIVIDER_DOWN 1
299 
300 /* FIXME: recommended safety delay after lock is detected */
301 #define PLL_POST_LOCK_DELAY 100
302 
303 /* Tegra CPU clock and reset control regs */
304 #define TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX 0x4c
305 #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET 0x340
306 #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR 0x344
307 #define TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR 0x34c
308 #define TEGRA30_CLK_RST_CONTROLLER_CPU_CMPLX_STATUS 0x470
309 
310 #define CPU_CLOCK(cpu) (0x1 << (8 + cpu))
311 #define CPU_RESET(cpu) (0x1111ul << (cpu))
312 
317  /* Oscillator Frequency in KHz */
319  /* UTMIP PLL Enable Delay Count */
321  /* UTMIP PLL Stable count */
323  /* UTMIP PLL Active delay count */
325  /* UTMIP PLL Xtal frequency count */
327 };
328 
329 static const struct utmi_clk_param utmi_parameters[] = {
330  {
331  .osc_frequency = 13000000,
332  .enable_delay_count = 0x02,
333  .stable_count = 0x33,
334  .active_delay_count = 0x05,
335  .xtal_freq_count = 0x7F
336  },
337  {
338  .osc_frequency = 19200000,
339  .enable_delay_count = 0x03,
340  .stable_count = 0x4B,
341  .active_delay_count = 0x06,
342  .xtal_freq_count = 0xBB},
343  {
344  .osc_frequency = 12000000,
345  .enable_delay_count = 0x02,
346  .stable_count = 0x2F,
347  .active_delay_count = 0x04,
348  .xtal_freq_count = 0x76
349  },
350  {
351  .osc_frequency = 26000000,
352  .enable_delay_count = 0x04,
353  .stable_count = 0x66,
354  .active_delay_count = 0x09,
355  .xtal_freq_count = 0xFE
356  },
357  {
358  .osc_frequency = 16800000,
359  .enable_delay_count = 0x03,
360  .stable_count = 0x41,
361  .active_delay_count = 0x0A,
362  .xtal_freq_count = 0xA4
363  },
364 };
365 
366 static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
367 static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
368 static void __iomem *misc_gp_hidrev_base = IO_ADDRESS(TEGRA_APB_MISC_BASE);
369 
370 #define MISC_GP_HIDREV 0x804
371 
372 /*
373  * Some peripheral clocks share an enable bit, so refcount the enable bits
374  * in registers CLK_ENABLE_L, ... CLK_ENABLE_W
375  */
376 static int tegra_periph_clk_enable_refcount[CLK_OUT_ENB_NUM * 32];
377 
378 #define clk_writel(value, reg) \
379  __raw_writel(value, reg_clk_base + (reg))
380 #define clk_readl(reg) \
381  __raw_readl(reg_clk_base + (reg))
382 #define pmc_writel(value, reg) \
383  __raw_writel(value, reg_pmc_base + (reg))
384 #define pmc_readl(reg) \
385  __raw_readl(reg_pmc_base + (reg))
386 #define chipid_readl() \
387  __raw_readl(misc_gp_hidrev_base + MISC_GP_HIDREV)
388 
389 #define clk_writel_delay(value, reg) \
390  do { \
391  __raw_writel((value), reg_clk_base + (reg)); \
392  udelay(2); \
393  } while (0)
394 
395 static inline int clk_set_div(struct clk_tegra *c, u32 n)
396 {
397  struct clk *clk = c->hw.clk;
398 
399  return clk_set_rate(clk,
400  (__clk_get_rate(__clk_get_parent(clk)) + n - 1) / n);
401 }
402 
403 static inline u32 periph_clk_to_reg(
404  struct clk_tegra *c, u32 reg_L, u32 reg_V, int offs)
405 {
406  u32 reg = c->u.periph.clk_num / 32;
407  BUG_ON(reg >= RST_DEVICES_NUM);
408  if (reg < 3)
409  reg = reg_L + (reg * offs);
410  else
411  reg = reg_V + ((reg - 3) * offs);
412  return reg;
413 }
414 
415 static unsigned long clk_measure_input_freq(void)
416 {
417  u32 clock_autodetect;
420  clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
421  if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
422  return 12000000;
423  } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
424  return 13000000;
425  } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
426  return 19200000;
427  } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
428  return 26000000;
429  } else if (clock_autodetect >= 1025 - 3 && clock_autodetect <= 1025 + 3) {
430  return 16800000;
431  } else if (clock_autodetect >= 2344 - 3 && clock_autodetect <= 2344 + 3) {
432  return 38400000;
433  } else if (clock_autodetect >= 2928 - 3 && clock_autodetect <= 2928 + 3) {
434  return 48000000;
435  } else {
436  pr_err("%s: Unexpected clock autodetect value %d", __func__,
437  clock_autodetect);
438  BUG();
439  return 0;
440  }
441 }
442 
443 static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate,
444  u32 flags, u32 round_mode)
445 {
446  s64 divider_u71 = parent_rate;
447  if (!rate)
448  return -EINVAL;
449 
450  if (!(flags & DIV_U71_INT))
451  divider_u71 *= 2;
452  if (round_mode == ROUND_DIVIDER_UP)
453  divider_u71 += rate - 1;
454  do_div(divider_u71, rate);
455  if (flags & DIV_U71_INT)
456  divider_u71 *= 2;
457 
458  if (divider_u71 - 2 < 0)
459  return 0;
460 
461  if (divider_u71 - 2 > 255)
462  return -EINVAL;
463 
464  return divider_u71 - 2;
465 }
466 
467 static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
468 {
469  s64 divider_u16;
470 
471  divider_u16 = parent_rate;
472  if (!rate)
473  return -EINVAL;
474  divider_u16 += rate - 1;
475  do_div(divider_u16, rate);
476 
477  if (divider_u16 - 1 < 0)
478  return 0;
479 
480  if (divider_u16 - 1 > 0xFFFF)
481  return -EINVAL;
482 
483  return divider_u16 - 1;
484 }
485 
486 static unsigned long tegra30_clk_fixed_recalc_rate(struct clk_hw *hw,
487  unsigned long parent_rate)
488 {
489  return to_clk_tegra(hw)->fixed_rate;
490 }
491 
493  .recalc_rate = tegra30_clk_fixed_recalc_rate,
494 };
495 
496 /* clk_m functions */
497 static unsigned long tegra30_clk_m_recalc_rate(struct clk_hw *hw,
498  unsigned long parent_rate)
499 {
500  if (!to_clk_tegra(hw)->fixed_rate)
501  to_clk_tegra(hw)->fixed_rate = clk_measure_input_freq();
502  return to_clk_tegra(hw)->fixed_rate;
503 }
504 
505 static void tegra30_clk_m_init(struct clk_hw *hw)
506 {
507  u32 osc_ctrl = clk_readl(OSC_CTRL);
508  u32 auto_clock_control = osc_ctrl & ~OSC_CTRL_OSC_FREQ_MASK;
510 
511  switch (to_clk_tegra(hw)->fixed_rate) {
512  case 12000000:
513  auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
514  BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
515  break;
516  case 13000000:
517  auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
518  BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
519  break;
520  case 19200000:
521  auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
522  BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
523  break;
524  case 26000000:
525  auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
526  BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
527  break;
528  case 16800000:
529  auto_clock_control |= OSC_CTRL_OSC_FREQ_16_8MHZ;
530  BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
531  break;
532  case 38400000:
533  auto_clock_control |= OSC_CTRL_OSC_FREQ_38_4MHZ;
534  BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_2);
535  break;
536  case 48000000:
537  auto_clock_control |= OSC_CTRL_OSC_FREQ_48MHZ;
538  BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_4);
539  break;
540  default:
541  pr_err("%s: Unexpected clock rate %ld", __func__,
542  to_clk_tegra(hw)->fixed_rate);
543  BUG();
544  }
545  clk_writel(auto_clock_control, OSC_CTRL);
546 }
547 
549  .init = tegra30_clk_m_init,
550  .recalc_rate = tegra30_clk_m_recalc_rate,
551 };
552 
553 static unsigned long tegra30_clk_m_div_recalc_rate(struct clk_hw *hw,
554  unsigned long parent_rate)
555 {
556  struct clk_tegra *c = to_clk_tegra(hw);
557  u64 rate = parent_rate;
558 
559  if (c->mul != 0 && c->div != 0) {
560  rate *= c->mul;
561  rate += c->div - 1; /* round up */
562  do_div(rate, c->div);
563  }
564 
565  return rate;
566 }
567 
569  .recalc_rate = tegra30_clk_m_div_recalc_rate,
570 };
571 
572 /* PLL reference divider functions */
573 static unsigned long tegra30_pll_ref_recalc_rate(struct clk_hw *hw,
574  unsigned long parent_rate)
575 {
576  struct clk_tegra *c = to_clk_tegra(hw);
577  unsigned long rate = parent_rate;
579 
580  switch (pll_ref_div) {
582  c->div = 1;
583  break;
585  c->div = 2;
586  break;
588  c->div = 4;
589  break;
590  default:
591  pr_err("%s: Invalid pll ref divider %d", __func__, pll_ref_div);
592  BUG();
593  }
594  c->mul = 1;
595 
596  if (c->mul != 0 && c->div != 0) {
597  rate *= c->mul;
598  rate += c->div - 1; /* round up */
599  do_div(rate, c->div);
600  }
601 
602  return rate;
603 }
604 
606  .recalc_rate = tegra30_pll_ref_recalc_rate,
607 };
608 
609 /* super clock functions */
610 /* "super clocks" on tegra30 have two-stage muxes, fractional 7.1 divider and
611  * clock skipping super divider. We will ignore the clock skipping divider,
612  * since we can't lower the voltage when using the clock skip, but we can if
613  * we lower the PLL frequency. We will use 7.1 divider for CPU super-clock
614  * only when its parent is a fixed rate PLL, since we can't change PLL rate
615  * in this case.
616  */
617 static void tegra30_super_clk_init(struct clk_hw *hw)
618 {
619  struct clk_tegra *c = to_clk_tegra(hw);
620  struct clk_tegra *p =
622 
623  c->state = ON;
624  if (c->flags & DIV_U71) {
625  /* Init safe 7.1 divider value (does not affect PLLX path) */
627  c->reg + SUPER_CLK_DIVIDER);
628  c->mul = 2;
629  c->div = 2;
630  if (!(p->flags & PLLX))
632  } else
634 }
635 
636 static u8 tegra30_super_clk_get_parent(struct clk_hw *hw)
637 {
638  struct clk_tegra *c = to_clk_tegra(hw);
639  u32 val;
640  int source;
641  int shift;
642 
643  val = clk_readl(c->reg + SUPER_CLK_MUX);
644  BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
645  ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
646  shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
648  source = (val >> shift) & SUPER_SOURCE_MASK;
649  if (c->flags & DIV_2)
650  source |= val & SUPER_LP_DIV2_BYPASS;
651 
652  return source;
653 }
654 
655 static int tegra30_super_clk_set_parent(struct clk_hw *hw, u8 index)
656 {
657  struct clk_tegra *c = to_clk_tegra(hw);
658  struct clk_tegra *p =
660  u32 val;
661  int shift;
662 
663  val = clk_readl(c->reg + SUPER_CLK_MUX);
664  BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
665  ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
666  shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
668 
669  /* For LP mode super-clock switch between PLLX direct
670  and divided-by-2 outputs is allowed only when other
671  than PLLX clock source is current parent */
672  if ((c->flags & DIV_2) && (p->flags & PLLX) &&
673  ((index ^ val) & SUPER_LP_DIV2_BYPASS)) {
674  if (p->flags & PLLX)
675  return -EINVAL;
676  val ^= SUPER_LP_DIV2_BYPASS;
677  clk_writel_delay(val, c->reg);
678  }
679  val &= ~(SUPER_SOURCE_MASK << shift);
680  val |= (index & SUPER_SOURCE_MASK) << shift;
681 
682  /* 7.1 divider for CPU super-clock does not affect
683  PLLX path */
684  if (c->flags & DIV_U71) {
685  u32 div = 0;
686  if (!(p->flags & PLLX)) {
687  div = clk_readl(c->reg +
691  }
692  c->div = div + 2;
693  c->mul = 2;
694  }
695  clk_writel_delay(val, c->reg);
696 
697  return 0;
698 }
699 
700 /*
701  * Do not use super clocks "skippers", since dividing using a clock skipper
702  * does not allow the voltage to be scaled down. Instead adjust the rate of
703  * the parent clock. This requires that the parent of a super clock have no
704  * other children, otherwise the rate will change underneath the other
705  * children. Special case: if fixed rate PLL is CPU super clock parent the
706  * rate of this PLL can't be changed, and it has many other children. In
707  * this case use 7.1 fractional divider to adjust the super clock rate.
708  */
709 static int tegra30_super_clk_set_rate(struct clk_hw *hw, unsigned long rate,
710  unsigned long parent_rate)
711 {
712  struct clk_tegra *c = to_clk_tegra(hw);
713  struct clk *parent = __clk_get_parent(hw->clk);
714  struct clk_tegra *cparent = to_clk_tegra(__clk_get_hw(parent));
715 
716  if ((c->flags & DIV_U71) && (cparent->flags & PLL_FIXED)) {
717  int div = clk_div71_get_divider(parent_rate,
718  rate, c->flags, ROUND_DIVIDER_DOWN);
719  div = max(div, SUPER_CLOCK_DIV_U71_MIN);
720 
722  c->reg + SUPER_CLK_DIVIDER);
723  c->div = div + 2;
724  c->mul = 2;
725  return 0;
726  }
727  return 0;
728 }
729 
730 static unsigned long tegra30_super_clk_recalc_rate(struct clk_hw *hw,
731  unsigned long parent_rate)
732 {
733  struct clk_tegra *c = to_clk_tegra(hw);
734  u64 rate = parent_rate;
735 
736  if (c->mul != 0 && c->div != 0) {
737  rate *= c->mul;
738  rate += c->div - 1; /* round up */
739  do_div(rate, c->div);
740  }
741 
742  return rate;
743 }
744 
745 static long tegra30_super_clk_round_rate(struct clk_hw *hw, unsigned long rate,
746  unsigned long *prate)
747 {
748  struct clk_tegra *c = to_clk_tegra(hw);
749  struct clk *parent = __clk_get_parent(hw->clk);
750  struct clk_tegra *cparent = to_clk_tegra(__clk_get_hw(parent));
751  int mul = 2;
752  int div;
753 
754  if ((c->flags & DIV_U71) && (cparent->flags & PLL_FIXED)) {
755  div = clk_div71_get_divider(*prate,
756  rate, c->flags, ROUND_DIVIDER_DOWN);
757  div = max(div, SUPER_CLOCK_DIV_U71_MIN) + 2;
758  rate = *prate * mul;
759  rate += div - 1; /* round up */
760  do_div(rate, c->div);
761 
762  return rate;
763  }
764  return *prate;
765 }
766 
768  .init = tegra30_super_clk_init,
769  .set_parent = tegra30_super_clk_set_parent,
770  .get_parent = tegra30_super_clk_get_parent,
771  .recalc_rate = tegra30_super_clk_recalc_rate,
772  .round_rate = tegra30_super_clk_round_rate,
773  .set_rate = tegra30_super_clk_set_rate,
774 };
775 
776 static unsigned long tegra30_twd_clk_recalc_rate(struct clk_hw *hw,
777  unsigned long parent_rate)
778 {
779  struct clk_tegra *c = to_clk_tegra(hw);
780  u64 rate = parent_rate;
781 
782  if (c->mul != 0 && c->div != 0) {
783  rate *= c->mul;
784  rate += c->div - 1; /* round up */
785  do_div(rate, c->div);
786  }
787 
788  return rate;
789 }
790 
792  .recalc_rate = tegra30_twd_clk_recalc_rate,
793 };
794 
795 /* Blink output functions */
796 static int tegra30_blink_clk_is_enabled(struct clk_hw *hw)
797 {
798  struct clk_tegra *c = to_clk_tegra(hw);
799  u32 val;
800 
801  val = pmc_readl(PMC_CTRL);
802  c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
803  return c->state;
804 }
805 
806 static int tegra30_blink_clk_enable(struct clk_hw *hw)
807 {
808  u32 val;
809 
812 
813  val = pmc_readl(PMC_CTRL);
815 
816  return 0;
817 }
818 
819 static void tegra30_blink_clk_disable(struct clk_hw *hw)
820 {
821  u32 val;
822 
823  val = pmc_readl(PMC_CTRL);
825 
828 }
829 
830 static int tegra30_blink_clk_set_rate(struct clk_hw *hw, unsigned long rate,
831  unsigned long parent_rate)
832 {
833  struct clk_tegra *c = to_clk_tegra(hw);
834 
835  if (rate >= parent_rate) {
836  c->div = 1;
837  pmc_writel(0, c->reg);
838  } else {
839  unsigned int on_off;
840  u32 val;
841 
842  on_off = DIV_ROUND_UP(parent_rate / 8, rate);
843  c->div = on_off * 8;
844 
845  val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
849  val |= on_off;
850  val |= PMC_BLINK_TIMER_ENB;
851  pmc_writel(val, c->reg);
852  }
853 
854  return 0;
855 }
856 
857 static unsigned long tegra30_blink_clk_recalc_rate(struct clk_hw *hw,
858  unsigned long parent_rate)
859 {
860  struct clk_tegra *c = to_clk_tegra(hw);
861  u64 rate = parent_rate;
862  u32 val;
863  u32 mul;
864  u32 div;
865  u32 on_off;
866 
867  mul = 1;
868  val = pmc_readl(c->reg);
869 
870  if (val & PMC_BLINK_TIMER_ENB) {
871  on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
875  on_off += val;
876  /* each tick in the blink timer is 4 32KHz clocks */
877  div = on_off * 4;
878  } else {
879  div = 1;
880  }
881 
882  if (mul != 0 && div != 0) {
883  rate *= mul;
884  rate += div - 1; /* round up */
885  do_div(rate, div);
886  }
887  return rate;
888 }
889 
890 static long tegra30_blink_clk_round_rate(struct clk_hw *hw, unsigned long rate,
891  unsigned long *prate)
892 {
893  int div;
894  int mul;
895  long round_rate = *prate;
896 
897  mul = 1;
898 
899  if (rate >= *prate) {
900  div = 1;
901  } else {
902  div = DIV_ROUND_UP(*prate / 8, rate);
903  div *= 8;
904  }
905 
906  round_rate *= mul;
907  round_rate += div - 1;
908  do_div(round_rate, div);
909 
910  return round_rate;
911 }
912 
914  .is_enabled = tegra30_blink_clk_is_enabled,
915  .enable = tegra30_blink_clk_enable,
916  .disable = tegra30_blink_clk_disable,
917  .recalc_rate = tegra30_blink_clk_recalc_rate,
918  .round_rate = tegra30_blink_clk_round_rate,
919  .set_rate = tegra30_blink_clk_set_rate,
920 };
921 
922 static void tegra30_utmi_param_configure(struct clk_hw *hw)
923 {
924  unsigned long main_rate =
926  u32 reg;
927  int i;
928 
929  for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
930  if (main_rate == utmi_parameters[i].osc_frequency)
931  break;
932  }
933 
934  if (i >= ARRAY_SIZE(utmi_parameters)) {
935  pr_err("%s: Unexpected main rate %lu\n", __func__, main_rate);
936  return;
937  }
938 
939  reg = clk_readl(UTMIP_PLL_CFG2);
940 
941  /* Program UTMIP PLL stable and active counts */
942  /* [FIXME] arclk_rst.h says WRONG! This should be 1ms -> 0x50 Check! */
943  reg &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
945  utmi_parameters[i].stable_count);
946 
948 
950  utmi_parameters[i].active_delay_count);
951 
952  /* Remove power downs from UTMIP PLL control bits */
956 
958 
959  /* Program UTMIP PLL delay and oscillator frequency counts */
960  reg = clk_readl(UTMIP_PLL_CFG1);
962 
964  utmi_parameters[i].enable_delay_count);
965 
966  reg &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
968  utmi_parameters[i].xtal_freq_count);
969 
970  /* Remove power downs from UTMIP PLL control bits */
974 
976 }
977 
978 /* PLL Functions */
979 static int tegra30_pll_clk_wait_for_lock(struct clk_tegra *c, u32 lock_reg,
980  u32 lock_bit)
981 {
982  int ret = 0;
983 
984 #if USE_PLL_LOCK_BITS
985  int i;
986  for (i = 0; i < c->u.pll.lock_delay; i++) {
987  if (clk_readl(lock_reg) & lock_bit) {
989  return 0;
990  }
991  udelay(2); /* timeout = 2 * lock time */
992  }
993  pr_err("Timed out waiting for lock bit on pll %s",
994  __clk_get_name(hw->clk));
995  ret = -1;
996 #else
997  udelay(c->u.pll.lock_delay);
998 #endif
999  return ret;
1000 }
1001 
1002 static int tegra30_pll_clk_is_enabled(struct clk_hw *hw)
1003 {
1004  struct clk_tegra *c = to_clk_tegra(hw);
1005  u32 val = clk_readl(c->reg + PLL_BASE);
1006 
1007  c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
1008  return c->state;
1009 }
1010 
1011 static void tegra30_pll_clk_init(struct clk_hw *hw)
1012 {
1013  struct clk_tegra *c = to_clk_tegra(hw);
1014 
1015  if (c->flags & PLLU)
1016  tegra30_utmi_param_configure(hw);
1017 }
1018 
1019 static int tegra30_pll_clk_enable(struct clk_hw *hw)
1020 {
1021  struct clk_tegra *c = to_clk_tegra(hw);
1022  u32 val;
1023  pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
1024 
1025 #if USE_PLL_LOCK_BITS
1026  val = clk_readl(c->reg + PLL_MISC(c));
1027  val |= PLL_MISC_LOCK_ENABLE(c);
1028  clk_writel(val, c->reg + PLL_MISC(c));
1029 #endif
1030  val = clk_readl(c->reg + PLL_BASE);
1031  val &= ~PLL_BASE_BYPASS;
1032  val |= PLL_BASE_ENABLE;
1033  clk_writel(val, c->reg + PLL_BASE);
1034 
1035  if (c->flags & PLLM) {
1039  }
1040 
1041  tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_BASE, PLL_BASE_LOCK);
1042 
1043  return 0;
1044 }
1045 
1046 static void tegra30_pll_clk_disable(struct clk_hw *hw)
1047 {
1048  struct clk_tegra *c = to_clk_tegra(hw);
1049  u32 val;
1050  pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
1051 
1052  val = clk_readl(c->reg);
1053  val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
1054  clk_writel(val, c->reg);
1055 
1056  if (c->flags & PLLM) {
1060  }
1061 }
1062 
1063 static int tegra30_pll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1064  unsigned long parent_rate)
1065 {
1066  struct clk_tegra *c = to_clk_tegra(hw);
1067  u32 val, p_div, old_base;
1068  unsigned long input_rate;
1069  const struct clk_pll_freq_table *sel;
1070  struct clk_pll_freq_table cfg;
1071 
1072  if (c->flags & PLL_FIXED) {
1073  int ret = 0;
1074  if (rate != c->u.pll.fixed_rate) {
1075  pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
1076  __func__, __clk_get_name(hw->clk),
1077  c->u.pll.fixed_rate, rate);
1078  ret = -EINVAL;
1079  }
1080  return ret;
1081  }
1082 
1083  if (c->flags & PLLM) {
1084  if (rate != __clk_get_rate(hw->clk)) {
1085  pr_err("%s: Can not change memory %s rate in flight\n",
1086  __func__, __clk_get_name(hw->clk));
1087  return -EINVAL;
1088  }
1089  }
1090 
1091  p_div = 0;
1092  input_rate = parent_rate;
1093 
1094  /* Check if the target rate is tabulated */
1095  for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1096  if (sel->input_rate == input_rate && sel->output_rate == rate) {
1097  if (c->flags & PLLU) {
1098  BUG_ON(sel->p < 1 || sel->p > 2);
1099  if (sel->p == 1)
1100  p_div = PLLU_BASE_POST_DIV;
1101  } else {
1102  BUG_ON(sel->p < 1);
1103  for (val = sel->p; val > 1; val >>= 1)
1104  p_div++;
1105  p_div <<= PLL_BASE_DIVP_SHIFT;
1106  }
1107  break;
1108  }
1109  }
1110 
1111  /* Configure out-of-table rate */
1112  if (sel->input_rate == 0) {
1113  unsigned long cfreq;
1114  BUG_ON(c->flags & PLLU);
1115  sel = &cfg;
1116 
1117  switch (input_rate) {
1118  case 12000000:
1119  case 26000000:
1120  cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
1121  break;
1122  case 13000000:
1123  cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
1124  break;
1125  case 16800000:
1126  case 19200000:
1127  cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
1128  break;
1129  default:
1130  pr_err("%s: Unexpected reference rate %lu\n",
1131  __func__, input_rate);
1132  BUG();
1133  }
1134 
1135  /* Raise VCO to guarantee 0.5% accuracy */
1136  for (cfg.output_rate = rate; cfg.output_rate < 200 * cfreq;
1137  cfg.output_rate <<= 1)
1138  p_div++;
1139 
1140  cfg.p = 0x1 << p_div;
1141  cfg.m = input_rate / cfreq;
1142  cfg.n = cfg.output_rate / cfreq;
1143  cfg.cpcon = OUT_OF_TABLE_CPCON;
1144 
1145  if ((cfg.m > (PLL_BASE_DIVM_MASK >> PLL_BASE_DIVM_SHIFT)) ||
1147  (p_div > (PLL_BASE_DIVP_MASK >> PLL_BASE_DIVP_SHIFT)) ||
1148  (cfg.output_rate > c->u.pll.vco_max)) {
1149  pr_err("%s: Failed to set %s out-of-table rate %lu\n",
1150  __func__, __clk_get_name(hw->clk), rate);
1151  return -EINVAL;
1152  }
1153  p_div <<= PLL_BASE_DIVP_SHIFT;
1154  }
1155 
1156  c->mul = sel->n;
1157  c->div = sel->m * sel->p;
1158 
1159  old_base = val = clk_readl(c->reg + PLL_BASE);
1162  val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
1163  (sel->n << PLL_BASE_DIVN_SHIFT) | p_div;
1164  if (val == old_base)
1165  return 0;
1166 
1167  if (c->state == ON) {
1168  tegra30_pll_clk_disable(hw);
1169  val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
1170  }
1171  clk_writel(val, c->reg + PLL_BASE);
1172 
1173  if (c->flags & PLL_HAS_CPCON) {
1174  val = clk_readl(c->reg + PLL_MISC(c));
1175  val &= ~PLL_MISC_CPCON_MASK;
1176  val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
1177  if (c->flags & (PLLU | PLLD)) {
1178  val &= ~PLL_MISC_LFCON_MASK;
1179  if (sel->n >= PLLDU_LFCON_SET_DIVN)
1180  val |= 0x1 << PLL_MISC_LFCON_SHIFT;
1181  } else if (c->flags & (PLLX | PLLM)) {
1182  val &= ~(0x1 << PLL_MISC_DCCON_SHIFT);
1183  if (rate >= (c->u.pll.vco_max >> 1))
1184  val |= 0x1 << PLL_MISC_DCCON_SHIFT;
1185  }
1186  clk_writel(val, c->reg + PLL_MISC(c));
1187  }
1188 
1189  if (c->state == ON)
1190  tegra30_pll_clk_enable(hw);
1191 
1192  c->u.pll.fixed_rate = rate;
1193 
1194  return 0;
1195 }
1196 
1197 static long tegra30_pll_round_rate(struct clk_hw *hw, unsigned long rate,
1198  unsigned long *prate)
1199 {
1200  struct clk_tegra *c = to_clk_tegra(hw);
1201  unsigned long input_rate = *prate;
1202  u64 output_rate = *prate;
1203  const struct clk_pll_freq_table *sel;
1204  struct clk_pll_freq_table cfg;
1205  int mul;
1206  int div;
1207  u32 p_div;
1208  u32 val;
1209 
1210  if (c->flags & PLL_FIXED)
1211  return c->u.pll.fixed_rate;
1212 
1213  if (c->flags & PLLM)
1214  return __clk_get_rate(hw->clk);
1215 
1216  p_div = 0;
1217  /* Check if the target rate is tabulated */
1218  for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1219  if (sel->input_rate == input_rate && sel->output_rate == rate) {
1220  if (c->flags & PLLU) {
1221  BUG_ON(sel->p < 1 || sel->p > 2);
1222  if (sel->p == 1)
1223  p_div = PLLU_BASE_POST_DIV;
1224  } else {
1225  BUG_ON(sel->p < 1);
1226  for (val = sel->p; val > 1; val >>= 1)
1227  p_div++;
1228  p_div <<= PLL_BASE_DIVP_SHIFT;
1229  }
1230  break;
1231  }
1232  }
1233 
1234  if (sel->input_rate == 0) {
1235  unsigned long cfreq;
1236  BUG_ON(c->flags & PLLU);
1237  sel = &cfg;
1238 
1239  switch (input_rate) {
1240  case 12000000:
1241  case 26000000:
1242  cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
1243  break;
1244  case 13000000:
1245  cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
1246  break;
1247  case 16800000:
1248  case 19200000:
1249  cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
1250  break;
1251  default:
1252  pr_err("%s: Unexpected reference rate %lu\n",
1253  __func__, input_rate);
1254  BUG();
1255  }
1256 
1257  /* Raise VCO to guarantee 0.5% accuracy */
1258  for (cfg.output_rate = rate; cfg.output_rate < 200 * cfreq;
1259  cfg.output_rate <<= 1)
1260  p_div++;
1261 
1262  cfg.p = 0x1 << p_div;
1263  cfg.m = input_rate / cfreq;
1264  cfg.n = cfg.output_rate / cfreq;
1265  }
1266 
1267  mul = sel->n;
1268  div = sel->m * sel->p;
1269 
1270  output_rate *= mul;
1271  output_rate += div - 1; /* round up */
1272  do_div(output_rate, div);
1273 
1274  return output_rate;
1275 }
1276 
1277 static unsigned long tegra30_pll_recalc_rate(struct clk_hw *hw,
1278  unsigned long parent_rate)
1279 {
1280  struct clk_tegra *c = to_clk_tegra(hw);
1281  u64 rate = parent_rate;
1282  u32 val = clk_readl(c->reg + PLL_BASE);
1283 
1284  if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
1285  const struct clk_pll_freq_table *sel;
1286  for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1287  if (sel->input_rate == parent_rate &&
1288  sel->output_rate == c->u.pll.fixed_rate) {
1289  c->mul = sel->n;
1290  c->div = sel->m * sel->p;
1291  break;
1292  }
1293  }
1294  pr_err("Clock %s has unknown fixed frequency\n",
1295  __clk_get_name(hw->clk));
1296  BUG();
1297  } else if (val & PLL_BASE_BYPASS) {
1298  c->mul = 1;
1299  c->div = 1;
1300  } else {
1301  c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
1302  c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
1303  if (c->flags & PLLU)
1304  c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
1305  else
1306  c->div *= (0x1 << ((val & PLL_BASE_DIVP_MASK) >>
1308  }
1309 
1310  if (c->mul != 0 && c->div != 0) {
1311  rate *= c->mul;
1312  rate += c->div - 1; /* round up */
1313  do_div(rate, c->div);
1314  }
1315 
1316  return rate;
1317 }
1318 
1320  .is_enabled = tegra30_pll_clk_is_enabled,
1321  .init = tegra30_pll_clk_init,
1322  .enable = tegra30_pll_clk_enable,
1323  .disable = tegra30_pll_clk_disable,
1324  .recalc_rate = tegra30_pll_recalc_rate,
1325  .round_rate = tegra30_pll_round_rate,
1326  .set_rate = tegra30_pll_clk_set_rate,
1327 };
1328 
1329 int tegra30_plld_clk_cfg_ex(struct clk_hw *hw,
1330  enum tegra_clk_ex_param p, u32 setting)
1331 {
1332  struct clk_tegra *c = to_clk_tegra(hw);
1333  u32 val, mask, reg;
1334 
1335  switch (p) {
1337  mask = PLLD_BASE_CSI_CLKENABLE;
1338  reg = c->reg + PLL_BASE;
1339  break;
1341  mask = PLLD_MISC_DSI_CLKENABLE;
1342  reg = c->reg + PLL_MISC(c);
1343  break;
1345  if (!(c->flags & PLL_ALT_MISC_REG)) {
1346  mask = PLLD_BASE_DSIB_MUX_MASK;
1347  reg = c->reg + PLL_BASE;
1348  break;
1349  }
1350  /* fall through - error since PLLD2 does not have MUX_SEL control */
1351  default:
1352  return -EINVAL;
1353  }
1354 
1355  val = clk_readl(reg);
1356  if (setting)
1357  val |= mask;
1358  else
1359  val &= ~mask;
1360  clk_writel(val, reg);
1361  return 0;
1362 }
1363 
1364 static int tegra30_plle_clk_is_enabled(struct clk_hw *hw)
1365 {
1366  struct clk_tegra *c = to_clk_tegra(hw);
1367  u32 val;
1368 
1369  val = clk_readl(c->reg + PLL_BASE);
1370  c->state = (val & PLLE_BASE_ENABLE) ? ON : OFF;
1371  return c->state;
1372 }
1373 
1374 static void tegra30_plle_clk_disable(struct clk_hw *hw)
1375 {
1376  struct clk_tegra *c = to_clk_tegra(hw);
1377  u32 val;
1378 
1379  val = clk_readl(c->reg + PLL_BASE);
1381  clk_writel(val, c->reg + PLL_BASE);
1382 }
1383 
1384 static void tegra30_plle_training(struct clk_tegra *c)
1385 {
1386  u32 val;
1387 
1388  /* PLLE is already disabled, and setup cleared;
1389  * create falling edge on PLLE IDDQ input */
1390  val = pmc_readl(PMC_SATA_PWRGT);
1392  pmc_writel(val, PMC_SATA_PWRGT);
1393 
1394  val = pmc_readl(PMC_SATA_PWRGT);
1396  pmc_writel(val, PMC_SATA_PWRGT);
1397 
1398  val = pmc_readl(PMC_SATA_PWRGT);
1400  pmc_writel(val, PMC_SATA_PWRGT);
1401 
1402  do {
1403  val = clk_readl(c->reg + PLL_MISC(c));
1404  } while (!(val & PLLE_MISC_READY));
1405 }
1406 
1407 static int tegra30_plle_configure(struct clk_hw *hw, bool force_training)
1408 {
1409  struct clk_tegra *c = to_clk_tegra(hw);
1410  struct clk *parent = __clk_get_parent(hw->clk);
1411  const struct clk_pll_freq_table *sel;
1412  u32 val;
1413 
1414  unsigned long rate = c->u.pll.fixed_rate;
1415  unsigned long input_rate = __clk_get_rate(parent);
1416 
1417  for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1418  if (sel->input_rate == input_rate && sel->output_rate == rate)
1419  break;
1420  }
1421 
1422  if (sel->input_rate == 0)
1423  return -ENOSYS;
1424 
1425  /* disable PLLE, clear setup fiels */
1426  tegra30_plle_clk_disable(hw);
1427 
1428  val = clk_readl(c->reg + PLL_MISC(c));
1430  clk_writel(val, c->reg + PLL_MISC(c));
1431 
1432  /* training */
1433  val = clk_readl(c->reg + PLL_MISC(c));
1434  if (force_training || (!(val & PLLE_MISC_READY)))
1435  tegra30_plle_training(c);
1436 
1437  /* configure dividers, setup, disable SS */
1438  val = clk_readl(c->reg + PLL_BASE);
1439  val &= ~PLLE_BASE_DIV_MASK;
1440  val |= PLLE_BASE_DIV(sel->m, sel->n, sel->p, sel->cpcon);
1441  clk_writel(val, c->reg + PLL_BASE);
1442  c->mul = sel->n;
1443  c->div = sel->m * sel->p;
1444 
1445  val = clk_readl(c->reg + PLL_MISC(c));
1446  val |= PLLE_MISC_SETUP_VALUE;
1447  val |= PLLE_MISC_LOCK_ENABLE;
1448  clk_writel(val, c->reg + PLL_MISC(c));
1449 
1450  val = clk_readl(PLLE_SS_CTRL);
1451  val |= PLLE_SS_DISABLE;
1452  clk_writel(val, PLLE_SS_CTRL);
1453 
1454  /* enable and lock PLLE*/
1455  val = clk_readl(c->reg + PLL_BASE);
1457  clk_writel(val, c->reg + PLL_BASE);
1458 
1459  tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_MISC(c), PLLE_MISC_LOCK);
1460 
1461  return 0;
1462 }
1463 
1464 static int tegra30_plle_clk_enable(struct clk_hw *hw)
1465 {
1466  struct clk_tegra *c = to_clk_tegra(hw);
1467 
1468  return tegra30_plle_configure(hw, !c->set);
1469 }
1470 
1471 static unsigned long tegra30_plle_clk_recalc_rate(struct clk_hw *hw,
1472  unsigned long parent_rate)
1473 {
1474  struct clk_tegra *c = to_clk_tegra(hw);
1475  unsigned long rate = parent_rate;
1476  u32 val;
1477 
1478  val = clk_readl(c->reg + PLL_BASE);
1481  c->div *= (val & PLLE_BASE_DIVP_MASK) >> PLLE_BASE_DIVP_SHIFT;
1482 
1483  if (c->mul != 0 && c->div != 0) {
1484  rate *= c->mul;
1485  rate += c->div - 1; /* round up */
1486  do_div(rate, c->div);
1487  }
1488  return rate;
1489 }
1490 
1492  .is_enabled = tegra30_plle_clk_is_enabled,
1493  .enable = tegra30_plle_clk_enable,
1494  .disable = tegra30_plle_clk_disable,
1495  .recalc_rate = tegra30_plle_clk_recalc_rate,
1496 };
1497 
1498 /* Clock divider ops */
1499 static int tegra30_pll_div_clk_is_enabled(struct clk_hw *hw)
1500 {
1501  struct clk_tegra *c = to_clk_tegra(hw);
1502 
1503  if (c->flags & DIV_U71) {
1504  u32 val = clk_readl(c->reg);
1505  val >>= c->reg_shift;
1506  c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
1507  if (!(val & PLL_OUT_RESET_DISABLE))
1508  c->state = OFF;
1509  } else {
1510  c->state = ON;
1511  }
1512  return c->state;
1513 }
1514 
1515 static int tegra30_pll_div_clk_enable(struct clk_hw *hw)
1516 {
1517  struct clk_tegra *c = to_clk_tegra(hw);
1518  u32 val;
1519  u32 new_val;
1520 
1521  pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
1522  if (c->flags & DIV_U71) {
1523  val = clk_readl(c->reg);
1524  new_val = val >> c->reg_shift;
1525  new_val &= 0xFFFF;
1526 
1527  new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
1528 
1529  val &= ~(0xFFFF << c->reg_shift);
1530  val |= new_val << c->reg_shift;
1531  clk_writel_delay(val, c->reg);
1532  return 0;
1533  } else if (c->flags & DIV_2) {
1534  return 0;
1535  }
1536  return -EINVAL;
1537 }
1538 
1539 static void tegra30_pll_div_clk_disable(struct clk_hw *hw)
1540 {
1541  struct clk_tegra *c = to_clk_tegra(hw);
1542  u32 val;
1543  u32 new_val;
1544 
1545  pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
1546  if (c->flags & DIV_U71) {
1547  val = clk_readl(c->reg);
1548  new_val = val >> c->reg_shift;
1549  new_val &= 0xFFFF;
1550 
1551  new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
1552 
1553  val &= ~(0xFFFF << c->reg_shift);
1554  val |= new_val << c->reg_shift;
1555  clk_writel_delay(val, c->reg);
1556  }
1557 }
1558 
1559 static int tegra30_pll_div_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1560  unsigned long parent_rate)
1561 {
1562  struct clk_tegra *c = to_clk_tegra(hw);
1563  u32 val;
1564  u32 new_val;
1565  int divider_u71;
1566 
1567  if (c->flags & DIV_U71) {
1568  divider_u71 = clk_div71_get_divider(
1569  parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1570  if (divider_u71 >= 0) {
1571  val = clk_readl(c->reg);
1572  new_val = val >> c->reg_shift;
1573  new_val &= 0xFFFF;
1574  if (c->flags & DIV_U71_FIXED)
1575  new_val |= PLL_OUT_OVERRIDE;
1576  new_val &= ~PLL_OUT_RATIO_MASK;
1577  new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
1578 
1579  val &= ~(0xFFFF << c->reg_shift);
1580  val |= new_val << c->reg_shift;
1581  clk_writel_delay(val, c->reg);
1582  c->div = divider_u71 + 2;
1583  c->mul = 2;
1584  c->fixed_rate = rate;
1585  return 0;
1586  }
1587  } else if (c->flags & DIV_2) {
1588  c->fixed_rate = rate;
1589  return 0;
1590  }
1591 
1592  return -EINVAL;
1593 }
1594 
1595 static unsigned long tegra30_pll_div_clk_recalc_rate(struct clk_hw *hw,
1596  unsigned long parent_rate)
1597 {
1598  struct clk_tegra *c = to_clk_tegra(hw);
1599  u64 rate = parent_rate;
1600 
1601  if (c->flags & DIV_U71) {
1602  u32 divu71;
1603  u32 val = clk_readl(c->reg);
1604  val >>= c->reg_shift;
1605 
1606  divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
1607  c->div = (divu71 + 2);
1608  c->mul = 2;
1609  } else if (c->flags & DIV_2) {
1610  if (c->flags & (PLLD | PLLX)) {
1611  c->div = 2;
1612  c->mul = 1;
1613  } else
1614  BUG();
1615  } else {
1616  c->div = 1;
1617  c->mul = 1;
1618  }
1619  if (c->mul != 0 && c->div != 0) {
1620  rate *= c->mul;
1621  rate += c->div - 1; /* round up */
1622  do_div(rate, c->div);
1623  }
1624 
1625  return rate;
1626 }
1627 
1628 static long tegra30_pll_div_clk_round_rate(struct clk_hw *hw,
1629  unsigned long rate, unsigned long *prate)
1630 {
1631  struct clk_tegra *c = to_clk_tegra(hw);
1632  unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
1633  int divider;
1634 
1635  if (prate)
1636  parent_rate = *prate;
1637 
1638  if (c->flags & DIV_U71) {
1639  divider = clk_div71_get_divider(
1640  parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1641  if (divider < 0)
1642  return divider;
1643  return DIV_ROUND_UP(parent_rate * 2, divider + 2);
1644  } else if (c->flags & DIV_2) {
1645  *prate = rate * 2;
1646  return rate;
1647  }
1648 
1649  return -EINVAL;
1650 }
1651 
1653  .is_enabled = tegra30_pll_div_clk_is_enabled,
1654  .enable = tegra30_pll_div_clk_enable,
1655  .disable = tegra30_pll_div_clk_disable,
1656  .set_rate = tegra30_pll_div_clk_set_rate,
1657  .recalc_rate = tegra30_pll_div_clk_recalc_rate,
1658  .round_rate = tegra30_pll_div_clk_round_rate,
1659 };
1660 
1661 /* Periph clk ops */
1662 static inline u32 periph_clk_source_mask(struct clk_tegra *c)
1663 {
1664  if (c->flags & MUX8)
1665  return 7 << 29;
1666  else if (c->flags & MUX_PWM)
1667  return 3 << 28;
1668  else if (c->flags & MUX_CLK_OUT)
1669  return 3 << (c->u.periph.clk_num + 4);
1670  else if (c->flags & PLLD)
1671  return PLLD_BASE_DSIB_MUX_MASK;
1672  else
1673  return 3 << 30;
1674 }
1675 
1676 static inline u32 periph_clk_source_shift(struct clk_tegra *c)
1677 {
1678  if (c->flags & MUX8)
1679  return 29;
1680  else if (c->flags & MUX_PWM)
1681  return 28;
1682  else if (c->flags & MUX_CLK_OUT)
1683  return c->u.periph.clk_num + 4;
1684  else if (c->flags & PLLD)
1685  return PLLD_BASE_DSIB_MUX_SHIFT;
1686  else
1687  return 30;
1688 }
1689 
1690 static int tegra30_periph_clk_is_enabled(struct clk_hw *hw)
1691 {
1692  struct clk_tegra *c = to_clk_tegra(hw);
1693 
1694  c->state = ON;
1696  c->state = OFF;
1697  if (!(c->flags & PERIPH_NO_RESET))
1699  c->state = OFF;
1700  return c->state;
1701 }
1702 
1703 static int tegra30_periph_clk_enable(struct clk_hw *hw)
1704 {
1705  struct clk_tegra *c = to_clk_tegra(hw);
1706 
1707  tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
1708  if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
1709  return 0;
1710 
1712  if (!(c->flags & PERIPH_NO_RESET) &&
1713  !(c->flags & PERIPH_MANUAL_RESET)) {
1715  PERIPH_CLK_TO_BIT(c)) {
1716  udelay(5); /* reset propagation delay */
1719  }
1720  }
1721  return 0;
1722 }
1723 
1724 static void tegra30_periph_clk_disable(struct clk_hw *hw)
1725 {
1726  struct clk_tegra *c = to_clk_tegra(hw);
1727  unsigned long val;
1728 
1729  tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
1730 
1731  if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 0)
1732  return;
1733 
1734  /* If peripheral is in the APB bus then read the APB bus to
1735  * flush the write operation in apb bus. This will avoid the
1736  * peripheral access after disabling clock*/
1737  if (c->flags & PERIPH_ON_APB)
1738  val = chipid_readl();
1739 
1741 }
1742 
1743 void tegra30_periph_clk_reset(struct clk_hw *hw, bool assert)
1744 {
1745  struct clk_tegra *c = to_clk_tegra(hw);
1746  unsigned long val;
1747 
1748  if (!(c->flags & PERIPH_NO_RESET)) {
1749  if (assert) {
1750  /* If peripheral is in the APB bus then read the APB
1751  * bus to flush the write operation in apb bus. This
1752  * will avoid the peripheral access after disabling
1753  * clock */
1754  if (c->flags & PERIPH_ON_APB)
1755  val = chipid_readl();
1756 
1759  } else
1762  }
1763 }
1764 
1765 static int tegra30_periph_clk_set_parent(struct clk_hw *hw, u8 index)
1766 {
1767  struct clk_tegra *c = to_clk_tegra(hw);
1768  u32 val;
1769 
1770  if (!(c->flags & MUX))
1771  return (index == 0) ? 0 : (-EINVAL);
1772 
1773  val = clk_readl(c->reg);
1774  val &= ~periph_clk_source_mask(c);
1775  val |= (index << periph_clk_source_shift(c));
1776  clk_writel_delay(val, c->reg);
1777  return 0;
1778 }
1779 
1780 static u8 tegra30_periph_clk_get_parent(struct clk_hw *hw)
1781 {
1782  struct clk_tegra *c = to_clk_tegra(hw);
1783  u32 val = clk_readl(c->reg);
1784  int source = (val & periph_clk_source_mask(c)) >>
1785  periph_clk_source_shift(c);
1786 
1787  if (!(c->flags & MUX))
1788  return 0;
1789 
1790  return source;
1791 }
1792 
1793 static int tegra30_periph_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1794  unsigned long parent_rate)
1795 {
1796  struct clk_tegra *c = to_clk_tegra(hw);
1797  u32 val;
1798  int divider;
1799 
1800  if (c->flags & DIV_U71) {
1801  divider = clk_div71_get_divider(
1802  parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1803  if (divider >= 0) {
1804  val = clk_readl(c->reg);
1806  val |= divider;
1807  if (c->flags & DIV_U71_UART) {
1808  if (divider)
1809  val |= PERIPH_CLK_UART_DIV_ENB;
1810  else
1811  val &= ~PERIPH_CLK_UART_DIV_ENB;
1812  }
1813  clk_writel_delay(val, c->reg);
1814  c->div = divider + 2;
1815  c->mul = 2;
1816  return 0;
1817  }
1818  } else if (c->flags & DIV_U16) {
1819  divider = clk_div16_get_divider(parent_rate, rate);
1820  if (divider >= 0) {
1821  val = clk_readl(c->reg);
1823  val |= divider;
1824  clk_writel_delay(val, c->reg);
1825  c->div = divider + 1;
1826  c->mul = 1;
1827  return 0;
1828  }
1829  } else if (parent_rate <= rate) {
1830  c->div = 1;
1831  c->mul = 1;
1832  return 0;
1833  }
1834  return -EINVAL;
1835 }
1836 
1837 static long tegra30_periph_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1838  unsigned long *prate)
1839 {
1840  struct clk_tegra *c = to_clk_tegra(hw);
1841  unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
1842  int divider;
1843 
1844  if (prate)
1845  parent_rate = *prate;
1846 
1847  if (c->flags & DIV_U71) {
1848  divider = clk_div71_get_divider(
1849  parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1850  if (divider < 0)
1851  return divider;
1852 
1853  return DIV_ROUND_UP(parent_rate * 2, divider + 2);
1854  } else if (c->flags & DIV_U16) {
1855  divider = clk_div16_get_divider(parent_rate, rate);
1856  if (divider < 0)
1857  return divider;
1858  return DIV_ROUND_UP(parent_rate, divider + 1);
1859  }
1860  return -EINVAL;
1861 }
1862 
1863 static unsigned long tegra30_periph_clk_recalc_rate(struct clk_hw *hw,
1864  unsigned long parent_rate)
1865 {
1866  struct clk_tegra *c = to_clk_tegra(hw);
1867  u64 rate = parent_rate;
1868  u32 val = clk_readl(c->reg);
1869 
1870  if (c->flags & DIV_U71) {
1871  u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
1872  if ((c->flags & DIV_U71_UART) &&
1873  (!(val & PERIPH_CLK_UART_DIV_ENB))) {
1874  divu71 = 0;
1875  }
1876  if (c->flags & DIV_U71_IDLE) {
1877  val &= ~(PERIPH_CLK_SOURCE_DIVU71_MASK <<
1881  clk_writel(val, c->reg);
1882  }
1883  c->div = divu71 + 2;
1884  c->mul = 2;
1885  } else if (c->flags & DIV_U16) {
1886  u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
1887  c->div = divu16 + 1;
1888  c->mul = 1;
1889  } else {
1890  c->div = 1;
1891  c->mul = 1;
1892  }
1893 
1894  if (c->mul != 0 && c->div != 0) {
1895  rate *= c->mul;
1896  rate += c->div - 1; /* round up */
1897  do_div(rate, c->div);
1898  }
1899  return rate;
1900 }
1901 
1903  .is_enabled = tegra30_periph_clk_is_enabled,
1904  .enable = tegra30_periph_clk_enable,
1905  .disable = tegra30_periph_clk_disable,
1906  .set_parent = tegra30_periph_clk_set_parent,
1907  .get_parent = tegra30_periph_clk_get_parent,
1908  .set_rate = tegra30_periph_clk_set_rate,
1909  .round_rate = tegra30_periph_clk_round_rate,
1910  .recalc_rate = tegra30_periph_clk_recalc_rate,
1911 };
1912 
1913 static int tegra30_dsib_clk_set_parent(struct clk_hw *hw, u8 index)
1914 {
1915  struct clk *d = clk_get_sys(NULL, "pll_d");
1916  /* The DSIB parent selection bit is in PLLD base
1917  register - can not do direct r-m-w, must be
1918  protected by PLLD lock */
1920  d, TEGRA_CLK_PLLD_MIPI_MUX_SEL, index);
1921 
1922  return 0;
1923 }
1924 
1926  .is_enabled = tegra30_periph_clk_is_enabled,
1927  .enable = &tegra30_periph_clk_enable,
1928  .disable = &tegra30_periph_clk_disable,
1929  .set_parent = &tegra30_dsib_clk_set_parent,
1930  .get_parent = &tegra30_periph_clk_get_parent,
1931  .set_rate = &tegra30_periph_clk_set_rate,
1932  .round_rate = &tegra30_periph_clk_round_rate,
1933  .recalc_rate = &tegra30_periph_clk_recalc_rate,
1934 };
1935 
1936 /* Periph extended clock configuration ops */
1937 int tegra30_vi_clk_cfg_ex(struct clk_hw *hw,
1938  enum tegra_clk_ex_param p, u32 setting)
1939 {
1940  struct clk_tegra *c = to_clk_tegra(hw);
1941 
1942  if (p == TEGRA_CLK_VI_INP_SEL) {
1943  u32 val = clk_readl(c->reg);
1944  val &= ~PERIPH_CLK_VI_SEL_EX_MASK;
1945  val |= (setting << PERIPH_CLK_VI_SEL_EX_SHIFT) &
1947  clk_writel(val, c->reg);
1948  return 0;
1949  }
1950  return -EINVAL;
1951 }
1952 
1953 int tegra30_nand_clk_cfg_ex(struct clk_hw *hw,
1954  enum tegra_clk_ex_param p, u32 setting)
1955 {
1956  struct clk_tegra *c = to_clk_tegra(hw);
1957 
1958  if (p == TEGRA_CLK_NAND_PAD_DIV2_ENB) {
1959  u32 val = clk_readl(c->reg);
1960  if (setting)
1962  else
1964  clk_writel(val, c->reg);
1965  return 0;
1966  }
1967  return -EINVAL;
1968 }
1969 
1970 int tegra30_dtv_clk_cfg_ex(struct clk_hw *hw,
1971  enum tegra_clk_ex_param p, u32 setting)
1972 {
1973  struct clk_tegra *c = to_clk_tegra(hw);
1974 
1975  if (p == TEGRA_CLK_DTV_INVERT) {
1976  u32 val = clk_readl(c->reg);
1977  if (setting)
1979  else
1981  clk_writel(val, c->reg);
1982  return 0;
1983  }
1984  return -EINVAL;
1985 }
1986 
1987 /* Output clock ops */
1988 
1989 static DEFINE_SPINLOCK(clk_out_lock);
1990 
1991 static int tegra30_clk_out_is_enabled(struct clk_hw *hw)
1992 {
1993  struct clk_tegra *c = to_clk_tegra(hw);
1994  u32 val = pmc_readl(c->reg);
1995 
1996  c->state = (val & (0x1 << c->u.periph.clk_num)) ? ON : OFF;
1997  c->mul = 1;
1998  c->div = 1;
1999  return c->state;
2000 }
2001 
2002 static int tegra30_clk_out_enable(struct clk_hw *hw)
2003 {
2004  struct clk_tegra *c = to_clk_tegra(hw);
2005  u32 val;
2006  unsigned long flags;
2007 
2008  spin_lock_irqsave(&clk_out_lock, flags);
2009  val = pmc_readl(c->reg);
2010  val |= (0x1 << c->u.periph.clk_num);
2011  pmc_writel(val, c->reg);
2012  spin_unlock_irqrestore(&clk_out_lock, flags);
2013 
2014  return 0;
2015 }
2016 
2017 static void tegra30_clk_out_disable(struct clk_hw *hw)
2018 {
2019  struct clk_tegra *c = to_clk_tegra(hw);
2020  u32 val;
2021  unsigned long flags;
2022 
2023  spin_lock_irqsave(&clk_out_lock, flags);
2024  val = pmc_readl(c->reg);
2025  val &= ~(0x1 << c->u.periph.clk_num);
2026  pmc_writel(val, c->reg);
2027  spin_unlock_irqrestore(&clk_out_lock, flags);
2028 }
2029 
2030 static int tegra30_clk_out_set_parent(struct clk_hw *hw, u8 index)
2031 {
2032  struct clk_tegra *c = to_clk_tegra(hw);
2033  u32 val;
2034  unsigned long flags;
2035 
2036  spin_lock_irqsave(&clk_out_lock, flags);
2037  val = pmc_readl(c->reg);
2038  val &= ~periph_clk_source_mask(c);
2039  val |= (index << periph_clk_source_shift(c));
2040  pmc_writel(val, c->reg);
2041  spin_unlock_irqrestore(&clk_out_lock, flags);
2042 
2043  return 0;
2044 }
2045 
2046 static u8 tegra30_clk_out_get_parent(struct clk_hw *hw)
2047 {
2048  struct clk_tegra *c = to_clk_tegra(hw);
2049  u32 val = pmc_readl(c->reg);
2050  int source;
2051 
2052  source = (val & periph_clk_source_mask(c)) >>
2053  periph_clk_source_shift(c);
2054  return source;
2055 }
2056 
2058  .is_enabled = tegra30_clk_out_is_enabled,
2059  .enable = tegra30_clk_out_enable,
2060  .disable = tegra30_clk_out_disable,
2061  .set_parent = tegra30_clk_out_set_parent,
2062  .get_parent = tegra30_clk_out_get_parent,
2063  .recalc_rate = tegra30_clk_fixed_recalc_rate,
2064 };
2065 
2066 /* Clock doubler ops */
2067 static int tegra30_clk_double_is_enabled(struct clk_hw *hw)
2068 {
2069  struct clk_tegra *c = to_clk_tegra(hw);
2070 
2071  c->state = ON;
2073  c->state = OFF;
2074  return c->state;
2075 };
2076 
2077 static int tegra30_clk_double_set_rate(struct clk_hw *hw, unsigned long rate,
2078  unsigned long parent_rate)
2079 {
2080  struct clk_tegra *c = to_clk_tegra(hw);
2081  u32 val;
2082 
2083  if (rate == parent_rate) {
2084  val = clk_readl(c->reg) | (0x1 << c->reg_shift);
2085  clk_writel(val, c->reg);
2086  c->mul = 1;
2087  c->div = 1;
2088  return 0;
2089  } else if (rate == 2 * parent_rate) {
2090  val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
2091  clk_writel(val, c->reg);
2092  c->mul = 2;
2093  c->div = 1;
2094  return 0;
2095  }
2096  return -EINVAL;
2097 }
2098 
2099 static unsigned long tegra30_clk_double_recalc_rate(struct clk_hw *hw,
2100  unsigned long parent_rate)
2101 {
2102  struct clk_tegra *c = to_clk_tegra(hw);
2103  u64 rate = parent_rate;
2104 
2105  u32 val = clk_readl(c->reg);
2106  c->mul = val & (0x1 << c->reg_shift) ? 1 : 2;
2107  c->div = 1;
2108 
2109  if (c->mul != 0 && c->div != 0) {
2110  rate *= c->mul;
2111  rate += c->div - 1; /* round up */
2112  do_div(rate, c->div);
2113  }
2114 
2115  return rate;
2116 }
2117 
2118 static long tegra30_clk_double_round_rate(struct clk_hw *hw, unsigned long rate,
2119  unsigned long *prate)
2120 {
2121  unsigned long output_rate = *prate;
2122 
2123  do_div(output_rate, 2);
2124  return output_rate;
2125 }
2126 
2128  .is_enabled = tegra30_clk_double_is_enabled,
2129  .enable = tegra30_periph_clk_enable,
2130  .disable = tegra30_periph_clk_disable,
2131  .recalc_rate = tegra30_clk_double_recalc_rate,
2132  .round_rate = tegra30_clk_double_round_rate,
2133  .set_rate = tegra30_clk_double_set_rate,
2134 };
2135 
2136 /* Audio sync clock ops */
2138  .recalc_rate = tegra30_clk_fixed_recalc_rate,
2139 };
2140 
2141 static int tegra30_audio_sync_clk_is_enabled(struct clk_hw *hw)
2142 {
2143  struct clk_tegra *c = to_clk_tegra(hw);
2144  u32 val = clk_readl(c->reg);
2145  c->state = (val & AUDIO_SYNC_DISABLE_BIT) ? OFF : ON;
2146  return c->state;
2147 }
2148 
2149 static int tegra30_audio_sync_clk_enable(struct clk_hw *hw)
2150 {
2151  struct clk_tegra *c = to_clk_tegra(hw);
2152  u32 val = clk_readl(c->reg);
2153  clk_writel((val & (~AUDIO_SYNC_DISABLE_BIT)), c->reg);
2154  return 0;
2155 }
2156 
2157 static void tegra30_audio_sync_clk_disable(struct clk_hw *hw)
2158 {
2159  struct clk_tegra *c = to_clk_tegra(hw);
2160  u32 val = clk_readl(c->reg);
2161  clk_writel((val | AUDIO_SYNC_DISABLE_BIT), c->reg);
2162 }
2163 
2164 static int tegra30_audio_sync_clk_set_parent(struct clk_hw *hw, u8 index)
2165 {
2166  struct clk_tegra *c = to_clk_tegra(hw);
2167  u32 val;
2168 
2169  val = clk_readl(c->reg);
2170  val &= ~AUDIO_SYNC_SOURCE_MASK;
2171  val |= index;
2172 
2173  clk_writel(val, c->reg);
2174  return 0;
2175 }
2176 
2177 static u8 tegra30_audio_sync_clk_get_parent(struct clk_hw *hw)
2178 {
2179  struct clk_tegra *c = to_clk_tegra(hw);
2180  u32 val = clk_readl(c->reg);
2181  int source;
2182 
2183  source = val & AUDIO_SYNC_SOURCE_MASK;
2184  return source;
2185 }
2186 
2188  .is_enabled = tegra30_audio_sync_clk_is_enabled,
2189  .enable = tegra30_audio_sync_clk_enable,
2190  .disable = tegra30_audio_sync_clk_disable,
2191  .set_parent = tegra30_audio_sync_clk_set_parent,
2192  .get_parent = tegra30_audio_sync_clk_get_parent,
2193  .recalc_rate = tegra30_clk_fixed_recalc_rate,
2194 };
2195 
2196 /* cml0 (pcie), and cml1 (sata) clock ops */
2197 static int tegra30_cml_clk_is_enabled(struct clk_hw *hw)
2198 {
2199  struct clk_tegra *c = to_clk_tegra(hw);
2200  u32 val = clk_readl(c->reg);
2201  c->state = val & (0x1 << c->u.periph.clk_num) ? ON : OFF;
2202  return c->state;
2203 }
2204 
2205 static int tegra30_cml_clk_enable(struct clk_hw *hw)
2206 {
2207  struct clk_tegra *c = to_clk_tegra(hw);
2208 
2209  u32 val = clk_readl(c->reg);
2210  val |= (0x1 << c->u.periph.clk_num);
2211  clk_writel(val, c->reg);
2212 
2213  return 0;
2214 }
2215 
2216 static void tegra30_cml_clk_disable(struct clk_hw *hw)
2217 {
2218  struct clk_tegra *c = to_clk_tegra(hw);
2219 
2220  u32 val = clk_readl(c->reg);
2221  val &= ~(0x1 << c->u.periph.clk_num);
2222  clk_writel(val, c->reg);
2223 }
2224 
2226  .is_enabled = tegra30_cml_clk_is_enabled,
2227  .enable = tegra30_cml_clk_enable,
2228  .disable = tegra30_cml_clk_disable,
2229  .recalc_rate = tegra30_clk_fixed_recalc_rate,
2230 };
2231 
2233  .recalc_rate = tegra30_clk_fixed_recalc_rate,
2234 };
2235 
2236 /* Tegra30 CPU clock and reset control functions */
2237 static void tegra30_wait_cpu_in_reset(u32 cpu)
2238 {
2239  unsigned int reg;
2240 
2241  do {
2242  reg = readl(reg_clk_base +
2244  cpu_relax();
2245  } while (!(reg & (1 << cpu))); /* check CPU been reset or not */
2246 
2247  return;
2248 }
2249 
2250 static void tegra30_put_cpu_in_reset(u32 cpu)
2251 {
2252  writel(CPU_RESET(cpu),
2254  dmb();
2255 }
2256 
2257 static void tegra30_cpu_out_of_reset(u32 cpu)
2258 {
2259  writel(CPU_RESET(cpu),
2261  wmb();
2262 }
2263 
2264 static void tegra30_enable_cpu_clock(u32 cpu)
2265 {
2266  unsigned int reg;
2267 
2268  writel(CPU_CLOCK(cpu),
2270  reg = readl(reg_clk_base +
2272 }
2273 
2274 static void tegra30_disable_cpu_clock(u32 cpu)
2275 {
2276 
2277  unsigned int reg;
2278 
2279  reg = readl(reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
2280  writel(reg | CPU_CLOCK(cpu),
2282 }
2283 
2284 static struct tegra_cpu_car_ops tegra30_cpu_car_ops = {
2285  .wait_for_reset = tegra30_wait_cpu_in_reset,
2286  .put_in_reset = tegra30_put_cpu_in_reset,
2287  .out_of_reset = tegra30_cpu_out_of_reset,
2288  .enable_clock = tegra30_enable_cpu_clock,
2289  .disable_clock = tegra30_disable_cpu_clock,
2290 };
2291 
2293 {
2294  tegra_cpu_car_ops = &tegra30_cpu_car_ops;
2295 }