clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * TNETV107X: Clock management APIs
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <common.h>
  22. #include <asm-generic/errno.h>
  23. #include <asm/io.h>
  24. #include <asm/processor.h>
  25. #include <asm/arch/clock.h>
  26. #define CLOCK_BASE TNETV107X_CLOCK_CONTROL_BASE
  27. #define PSC_BASE TNETV107X_PSC_BASE
  28. #define BIT(x) (1 << (x))
  29. #define MAX_PREDIV 64
  30. #define MAX_POSTDIV 8
  31. #define MAX_MULT 512
  32. #define MAX_DIV (MAX_PREDIV * MAX_POSTDIV)
  33. /* LPSC registers */
  34. #define PSC_PTCMD 0x120
  35. #define PSC_PTSTAT 0x128
  36. #define PSC_MDSTAT(n) (0x800 + (n) * 4)
  37. #define PSC_MDCTL(n) (0xA00 + (n) * 4)
  38. #define PSC_MDCTL_LRSTZ BIT(8)
  39. #define psc_reg_read(reg) __raw_readl((u32 *)(PSC_BASE + (reg)))
  40. #define psc_reg_write(reg, val) __raw_writel(val, (u32 *)(PSC_BASE + (reg)))
  41. /* SSPLL registers */
  42. struct sspll_regs {
  43. u32 modes;
  44. u32 postdiv;
  45. u32 prediv;
  46. u32 mult_factor;
  47. u32 divider_range;
  48. u32 bw_divider;
  49. u32 spr_amount;
  50. u32 spr_rate_div;
  51. u32 diag;
  52. };
  53. /* SSPLL base addresses */
  54. static struct sspll_regs *sspll_regs[] = {
  55. (struct sspll_regs *)(CLOCK_BASE + 0x040),
  56. (struct sspll_regs *)(CLOCK_BASE + 0x080),
  57. (struct sspll_regs *)(CLOCK_BASE + 0x0c0),
  58. };
  59. #define sspll_reg(pll, reg) (&(sspll_regs[pll]->reg))
  60. #define sspll_reg_read(pll, reg) __raw_readl(sspll_reg(pll, reg))
  61. #define sspll_reg_write(pll, reg, val) __raw_writel(val, sspll_reg(pll, reg))
  62. /* PLL Control Registers */
  63. struct pllctl_regs {
  64. u32 ctl; /* 00 */
  65. u32 ocsel; /* 04 */
  66. u32 secctl; /* 08 */
  67. u32 __pad0;
  68. u32 mult; /* 10 */
  69. u32 prediv; /* 14 */
  70. u32 div1; /* 18 */
  71. u32 div2; /* 1c */
  72. u32 div3; /* 20 */
  73. u32 oscdiv1; /* 24 */
  74. u32 postdiv; /* 28 */
  75. u32 bpdiv; /* 2c */
  76. u32 wakeup; /* 30 */
  77. u32 __pad1;
  78. u32 cmd; /* 38 */
  79. u32 stat; /* 3c */
  80. u32 alnctl; /* 40 */
  81. u32 dchange; /* 44 */
  82. u32 cken; /* 48 */
  83. u32 ckstat; /* 4c */
  84. u32 systat; /* 50 */
  85. u32 ckctl; /* 54 */
  86. u32 __pad2[2];
  87. u32 div4; /* 60 */
  88. u32 div5; /* 64 */
  89. u32 div6; /* 68 */
  90. u32 div7; /* 6c */
  91. u32 div8; /* 70 */
  92. };
  93. struct lpsc_map {
  94. int pll, div;
  95. };
  96. static struct pllctl_regs *pllctl_regs[] = {
  97. (struct pllctl_regs *)(CLOCK_BASE + 0x700),
  98. (struct pllctl_regs *)(CLOCK_BASE + 0x300),
  99. (struct pllctl_regs *)(CLOCK_BASE + 0x500),
  100. };
  101. #define pllctl_reg(pll, reg) (&(pllctl_regs[pll]->reg))
  102. #define pllctl_reg_read(pll, reg) __raw_readl(pllctl_reg(pll, reg))
  103. #define pllctl_reg_write(pll, reg, val) __raw_writel(val, pllctl_reg(pll, reg))
  104. #define pllctl_reg_rmw(pll, reg, mask, val) \
  105. pllctl_reg_write(pll, reg, \
  106. (pllctl_reg_read(pll, reg) & ~(mask)) | val)
  107. #define pllctl_reg_setbits(pll, reg, mask) \
  108. pllctl_reg_rmw(pll, reg, 0, mask)
  109. #define pllctl_reg_clrbits(pll, reg, mask) \
  110. pllctl_reg_rmw(pll, reg, mask, 0)
  111. /* PLLCTL Bits */
  112. #define PLLCTL_CLKMODE BIT(8)
  113. #define PLLCTL_PLLSELB BIT(7)
  114. #define PLLCTL_PLLENSRC BIT(5)
  115. #define PLLCTL_PLLDIS BIT(4)
  116. #define PLLCTL_PLLRST BIT(3)
  117. #define PLLCTL_PLLPWRDN BIT(1)
  118. #define PLLCTL_PLLEN BIT(0)
  119. #define PLLDIV_ENABLE BIT(15)
  120. static int pll_div_offset[] = {
  121. #define div_offset(reg) offsetof(struct pllctl_regs, reg)
  122. div_offset(div1), div_offset(div2), div_offset(div3),
  123. div_offset(div4), div_offset(div5), div_offset(div6),
  124. div_offset(div7), div_offset(div8),
  125. };
  126. static unsigned long pll_bypass_mask[] = { 1, 4, 2 };
  127. static unsigned long pll_div_mask[] = { 0x01ff, 0x00ff, 0x00ff };
  128. /* Mappings from PLL+DIV to subsystem clocks */
  129. #define sys_arm1176_clk {SYS_PLL, 0}
  130. #define sys_dsp_clk {SYS_PLL, 1}
  131. #define sys_ddr_clk {SYS_PLL, 2}
  132. #define sys_full_clk {SYS_PLL, 3}
  133. #define sys_lcd_clk {SYS_PLL, 4}
  134. #define sys_vlynq_ref_clk {SYS_PLL, 5}
  135. #define sys_tsc_clk {SYS_PLL, 6}
  136. #define sys_half_clk {SYS_PLL, 7}
  137. #define eth_clk_5 {ETH_PLL, 0}
  138. #define eth_clk_50 {ETH_PLL, 1}
  139. #define eth_clk_125 {ETH_PLL, 2}
  140. #define eth_clk_250 {ETH_PLL, 3}
  141. #define eth_clk_25 {ETH_PLL, 4}
  142. #define tdm_clk {TDM_PLL, 0}
  143. #define tdm_extra_clk {TDM_PLL, 1}
  144. #define tdm1_clk {TDM_PLL, 2}
  145. static const struct lpsc_map lpsc_clk_map[] = {
  146. [TNETV107X_LPSC_ARM] = sys_arm1176_clk,
  147. [TNETV107X_LPSC_GEM] = sys_dsp_clk,
  148. [TNETV107X_LPSC_DDR2_PHY] = sys_ddr_clk,
  149. [TNETV107X_LPSC_TPCC] = sys_full_clk,
  150. [TNETV107X_LPSC_TPTC0] = sys_full_clk,
  151. [TNETV107X_LPSC_TPTC1] = sys_full_clk,
  152. [TNETV107X_LPSC_RAM] = sys_full_clk,
  153. [TNETV107X_LPSC_MBX_LITE] = sys_arm1176_clk,
  154. [TNETV107X_LPSC_LCD] = sys_lcd_clk,
  155. [TNETV107X_LPSC_ETHSS] = eth_clk_125,
  156. [TNETV107X_LPSC_AEMIF] = sys_full_clk,
  157. [TNETV107X_LPSC_CHIP_CFG] = sys_half_clk,
  158. [TNETV107X_LPSC_TSC] = sys_tsc_clk,
  159. [TNETV107X_LPSC_ROM] = sys_half_clk,
  160. [TNETV107X_LPSC_UART2] = sys_half_clk,
  161. [TNETV107X_LPSC_PKTSEC] = sys_half_clk,
  162. [TNETV107X_LPSC_SECCTL] = sys_half_clk,
  163. [TNETV107X_LPSC_KEYMGR] = sys_half_clk,
  164. [TNETV107X_LPSC_KEYPAD] = sys_half_clk,
  165. [TNETV107X_LPSC_GPIO] = sys_half_clk,
  166. [TNETV107X_LPSC_MDIO] = sys_half_clk,
  167. [TNETV107X_LPSC_SDIO0] = sys_half_clk,
  168. [TNETV107X_LPSC_UART0] = sys_half_clk,
  169. [TNETV107X_LPSC_UART1] = sys_half_clk,
  170. [TNETV107X_LPSC_TIMER0] = sys_half_clk,
  171. [TNETV107X_LPSC_TIMER1] = sys_half_clk,
  172. [TNETV107X_LPSC_WDT_ARM] = sys_half_clk,
  173. [TNETV107X_LPSC_WDT_DSP] = sys_half_clk,
  174. [TNETV107X_LPSC_SSP] = sys_half_clk,
  175. [TNETV107X_LPSC_TDM0] = tdm_clk,
  176. [TNETV107X_LPSC_VLYNQ] = sys_vlynq_ref_clk,
  177. [TNETV107X_LPSC_MCDMA] = sys_half_clk,
  178. [TNETV107X_LPSC_USB0] = sys_half_clk,
  179. [TNETV107X_LPSC_TDM1] = tdm1_clk,
  180. [TNETV107X_LPSC_DEBUGSS] = sys_half_clk,
  181. [TNETV107X_LPSC_ETHSS_RGMII] = eth_clk_250,
  182. [TNETV107X_LPSC_SYSTEM] = sys_half_clk,
  183. [TNETV107X_LPSC_IMCOP] = sys_dsp_clk,
  184. [TNETV107X_LPSC_SPARE] = sys_half_clk,
  185. [TNETV107X_LPSC_SDIO1] = sys_half_clk,
  186. [TNETV107X_LPSC_USB1] = sys_half_clk,
  187. [TNETV107X_LPSC_USBSS] = sys_half_clk,
  188. [TNETV107X_LPSC_DDR2_EMIF1_VRST] = sys_ddr_clk,
  189. [TNETV107X_LPSC_DDR2_EMIF2_VCTL_RST] = sys_ddr_clk,
  190. };
  191. static const unsigned long pll_ext_freq[] = {
  192. [SYS_PLL] = CONFIG_PLL_SYS_EXT_FREQ,
  193. [ETH_PLL] = CONFIG_PLL_ETH_EXT_FREQ,
  194. [TDM_PLL] = CONFIG_PLL_TDM_EXT_FREQ,
  195. };
  196. static unsigned long pll_freq_get(int pll)
  197. {
  198. unsigned long mult = 1, prediv = 1, postdiv = 1;
  199. unsigned long ref = CONFIG_SYS_INT_OSC_FREQ;
  200. unsigned long ret;
  201. u32 bypass;
  202. bypass = __raw_readl((u32 *)(CLOCK_BASE));
  203. if (!(bypass & pll_bypass_mask[pll])) {
  204. mult = sspll_reg_read(pll, mult_factor);
  205. prediv = sspll_reg_read(pll, prediv) + 1;
  206. postdiv = sspll_reg_read(pll, postdiv) + 1;
  207. }
  208. if (pllctl_reg_read(pll, ctl) & PLLCTL_CLKMODE)
  209. ref = pll_ext_freq[pll];
  210. if (!(pllctl_reg_read(pll, ctl) & PLLCTL_PLLEN))
  211. return ref;
  212. ret = (unsigned long)(ref + ((unsigned long long)ref * mult) / 256);
  213. ret /= (prediv * postdiv);
  214. return ret;
  215. }
  216. static unsigned long __pll_div_freq_get(int pll, unsigned int fpll,
  217. int div)
  218. {
  219. int divider = 1;
  220. unsigned long divreg;
  221. divreg = __raw_readl((void *)pllctl_regs[pll] + pll_div_offset[div]);
  222. if (divreg & PLLDIV_ENABLE)
  223. divider = (divreg & pll_div_mask[pll]) + 1;
  224. return fpll / divider;
  225. }
  226. static unsigned long pll_div_freq_get(int pll, int div)
  227. {
  228. unsigned int fpll = pll_freq_get(pll);
  229. return __pll_div_freq_get(pll, fpll, div);
  230. }
  231. static void __pll_div_freq_set(int pll, unsigned int fpll, int div,
  232. unsigned long hz)
  233. {
  234. int divider = (fpll / hz - 1);
  235. divider &= pll_div_mask[pll];
  236. divider |= PLLDIV_ENABLE;
  237. __raw_writel(divider, (void *)pllctl_regs[pll] + pll_div_offset[div]);
  238. pllctl_reg_setbits(pll, alnctl, (1 << div));
  239. pllctl_reg_setbits(pll, dchange, (1 << div));
  240. }
  241. static unsigned long pll_div_freq_set(int pll, int div, unsigned long hz)
  242. {
  243. unsigned int fpll = pll_freq_get(pll);
  244. __pll_div_freq_set(pll, fpll, div, hz);
  245. pllctl_reg_write(pll, cmd, 1);
  246. /* Wait until new divider takes effect */
  247. while (pllctl_reg_read(pll, stat) & 0x01);
  248. return __pll_div_freq_get(pll, fpll, div);
  249. }
  250. unsigned long clk_get_rate(unsigned int clk)
  251. {
  252. return pll_div_freq_get(lpsc_clk_map[clk].pll, lpsc_clk_map[clk].div);
  253. }
  254. unsigned long clk_round_rate(unsigned int clk, unsigned long hz)
  255. {
  256. unsigned long fpll, divider, pll;
  257. pll = lpsc_clk_map[clk].pll;
  258. fpll = pll_freq_get(pll);
  259. divider = (fpll / hz - 1);
  260. divider &= pll_div_mask[pll];
  261. return fpll / (divider + 1);
  262. }
  263. int clk_set_rate(unsigned int clk, unsigned long _hz)
  264. {
  265. unsigned long hz;
  266. hz = clk_round_rate(clk, _hz);
  267. if (hz != _hz)
  268. return -EINVAL; /* Cannot set to target freq */
  269. pll_div_freq_set(lpsc_clk_map[clk].pll, lpsc_clk_map[clk].div, hz);
  270. return 0;
  271. }
  272. void lpsc_control(int mod, unsigned long state, int lrstz)
  273. {
  274. u32 mdctl;
  275. mdctl = psc_reg_read(PSC_MDCTL(mod));
  276. mdctl &= ~0x1f;
  277. mdctl |= state;
  278. if (lrstz == 0)
  279. mdctl &= ~PSC_MDCTL_LRSTZ;
  280. else if (lrstz == 1)
  281. mdctl |= PSC_MDCTL_LRSTZ;
  282. psc_reg_write(PSC_MDCTL(mod), mdctl);
  283. psc_reg_write(PSC_PTCMD, 1);
  284. /* wait for power domain transition to end */
  285. while (psc_reg_read(PSC_PTSTAT) & 1);
  286. /* Wait for module state change */
  287. while ((psc_reg_read(PSC_MDSTAT(mod)) & 0x1f) != state);
  288. }
  289. int lpsc_status(unsigned int id)
  290. {
  291. return psc_reg_read(PSC_MDSTAT(id)) & 0x1f;
  292. }
  293. static void init_pll(const struct pll_init_data *data)
  294. {
  295. unsigned long fpll;
  296. unsigned long best_pre = 0, best_post = 0, best_mult = 0;
  297. unsigned long div, prediv, postdiv, mult;
  298. unsigned long delta, actual;
  299. long best_delta = -1;
  300. int i;
  301. u32 tmp;
  302. if (data->pll == SYS_PLL)
  303. return; /* cannot reconfigure system pll on the fly */
  304. tmp = pllctl_reg_read(data->pll, ctl);
  305. if (data->internal_osc) {
  306. tmp &= ~PLLCTL_CLKMODE;
  307. fpll = CONFIG_SYS_INT_OSC_FREQ;
  308. } else {
  309. tmp |= PLLCTL_CLKMODE;
  310. fpll = pll_ext_freq[data->pll];
  311. }
  312. pllctl_reg_write(data->pll, ctl, tmp);
  313. mult = data->pll_freq / fpll;
  314. for (mult = MAX(mult, 1); mult <= MAX_MULT; mult++) {
  315. div = (fpll * mult) / data->pll_freq;
  316. if (div < 1 || div > MAX_DIV)
  317. continue;
  318. for (postdiv = 1; postdiv <= min(div, MAX_POSTDIV); postdiv++) {
  319. prediv = div / postdiv;
  320. if (prediv < 1 || prediv > MAX_PREDIV)
  321. continue;
  322. actual = (fpll / prediv) * (mult / postdiv);
  323. delta = (actual - data->pll_freq);
  324. if (delta < 0)
  325. delta = -delta;
  326. if ((delta < best_delta) || (best_delta == -1)) {
  327. best_delta = delta;
  328. best_mult = mult;
  329. best_pre = prediv;
  330. best_post = postdiv;
  331. if (delta == 0)
  332. goto done;
  333. }
  334. }
  335. }
  336. done:
  337. if (best_delta == -1) {
  338. printf("pll cannot derive %lu from %lu\n",
  339. data->pll_freq, fpll);
  340. return;
  341. }
  342. fpll = fpll * best_mult;
  343. fpll /= best_pre * best_post;
  344. pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLENSRC);
  345. pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLEN);
  346. pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLRST);
  347. pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLPWRDN);
  348. pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLDIS);
  349. sspll_reg_write(data->pll, mult_factor, (best_mult - 1) << 8);
  350. sspll_reg_write(data->pll, prediv, best_pre - 1);
  351. sspll_reg_write(data->pll, postdiv, best_post - 1);
  352. for (i = 0; i < 10; i++)
  353. if (data->div_freq[i])
  354. __pll_div_freq_set(data->pll, fpll, i,
  355. data->div_freq[i]);
  356. pllctl_reg_write(data->pll, cmd, 1);
  357. /* Wait until pll "go" operation completes */
  358. while (pllctl_reg_read(data->pll, stat) & 0x01);
  359. pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLRST);
  360. pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLEN);
  361. }
  362. void init_plls(int num_pll, struct pll_init_data *config)
  363. {
  364. int i;
  365. for (i = 0; i < num_pll; i++)
  366. init_pll(&config[i]);
  367. }