clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. /* Optimization barrier */
  146. #define barrier() \
  147. __asm__ __volatile__("mov r0, r0\n" : : : "memory");
  148. static const struct lpsc_map lpsc_clk_map[] = {
  149. [TNETV107X_LPSC_ARM] = sys_arm1176_clk,
  150. [TNETV107X_LPSC_GEM] = sys_dsp_clk,
  151. [TNETV107X_LPSC_DDR2_PHY] = sys_ddr_clk,
  152. [TNETV107X_LPSC_TPCC] = sys_full_clk,
  153. [TNETV107X_LPSC_TPTC0] = sys_full_clk,
  154. [TNETV107X_LPSC_TPTC1] = sys_full_clk,
  155. [TNETV107X_LPSC_RAM] = sys_full_clk,
  156. [TNETV107X_LPSC_MBX_LITE] = sys_arm1176_clk,
  157. [TNETV107X_LPSC_LCD] = sys_lcd_clk,
  158. [TNETV107X_LPSC_ETHSS] = eth_clk_125,
  159. [TNETV107X_LPSC_AEMIF] = sys_full_clk,
  160. [TNETV107X_LPSC_CHIP_CFG] = sys_half_clk,
  161. [TNETV107X_LPSC_TSC] = sys_tsc_clk,
  162. [TNETV107X_LPSC_ROM] = sys_half_clk,
  163. [TNETV107X_LPSC_UART2] = sys_half_clk,
  164. [TNETV107X_LPSC_PKTSEC] = sys_half_clk,
  165. [TNETV107X_LPSC_SECCTL] = sys_half_clk,
  166. [TNETV107X_LPSC_KEYMGR] = sys_half_clk,
  167. [TNETV107X_LPSC_KEYPAD] = sys_half_clk,
  168. [TNETV107X_LPSC_GPIO] = sys_half_clk,
  169. [TNETV107X_LPSC_MDIO] = sys_half_clk,
  170. [TNETV107X_LPSC_SDIO0] = sys_half_clk,
  171. [TNETV107X_LPSC_UART0] = sys_half_clk,
  172. [TNETV107X_LPSC_UART1] = sys_half_clk,
  173. [TNETV107X_LPSC_TIMER0] = sys_half_clk,
  174. [TNETV107X_LPSC_TIMER1] = sys_half_clk,
  175. [TNETV107X_LPSC_WDT_ARM] = sys_half_clk,
  176. [TNETV107X_LPSC_WDT_DSP] = sys_half_clk,
  177. [TNETV107X_LPSC_SSP] = sys_half_clk,
  178. [TNETV107X_LPSC_TDM0] = tdm_clk,
  179. [TNETV107X_LPSC_VLYNQ] = sys_vlynq_ref_clk,
  180. [TNETV107X_LPSC_MCDMA] = sys_half_clk,
  181. [TNETV107X_LPSC_USB0] = sys_half_clk,
  182. [TNETV107X_LPSC_TDM1] = tdm1_clk,
  183. [TNETV107X_LPSC_DEBUGSS] = sys_half_clk,
  184. [TNETV107X_LPSC_ETHSS_RGMII] = eth_clk_250,
  185. [TNETV107X_LPSC_SYSTEM] = sys_half_clk,
  186. [TNETV107X_LPSC_IMCOP] = sys_dsp_clk,
  187. [TNETV107X_LPSC_SPARE] = sys_half_clk,
  188. [TNETV107X_LPSC_SDIO1] = sys_half_clk,
  189. [TNETV107X_LPSC_USB1] = sys_half_clk,
  190. [TNETV107X_LPSC_USBSS] = sys_half_clk,
  191. [TNETV107X_LPSC_DDR2_EMIF1_VRST] = sys_ddr_clk,
  192. [TNETV107X_LPSC_DDR2_EMIF2_VCTL_RST] = sys_ddr_clk,
  193. };
  194. static const unsigned long pll_ext_freq[] = {
  195. [SYS_PLL] = CONFIG_PLL_SYS_EXT_FREQ,
  196. [ETH_PLL] = CONFIG_PLL_ETH_EXT_FREQ,
  197. [TDM_PLL] = CONFIG_PLL_TDM_EXT_FREQ,
  198. };
  199. static unsigned long pll_freq_get(int pll)
  200. {
  201. unsigned long mult = 1, prediv = 1, postdiv = 1;
  202. unsigned long ref = CONFIG_SYS_INT_OSC_FREQ;
  203. unsigned long ret;
  204. u32 bypass;
  205. bypass = __raw_readl((u32 *)(CLOCK_BASE));
  206. if (!(bypass & pll_bypass_mask[pll])) {
  207. mult = sspll_reg_read(pll, mult_factor);
  208. prediv = sspll_reg_read(pll, prediv) + 1;
  209. postdiv = sspll_reg_read(pll, postdiv) + 1;
  210. }
  211. if (pllctl_reg_read(pll, ctl) & PLLCTL_CLKMODE)
  212. ref = pll_ext_freq[pll];
  213. if (!(pllctl_reg_read(pll, ctl) & PLLCTL_PLLEN))
  214. return ref;
  215. ret = (unsigned long)(ref + ((unsigned long long)ref * mult) / 256);
  216. ret /= (prediv * postdiv);
  217. return ret;
  218. }
  219. static unsigned long __pll_div_freq_get(int pll, unsigned int fpll,
  220. int div)
  221. {
  222. int divider = 1;
  223. unsigned long divreg;
  224. divreg = __raw_readl((void *)pllctl_regs[pll] + pll_div_offset[div]);
  225. if (divreg & PLLDIV_ENABLE)
  226. divider = (divreg & pll_div_mask[pll]) + 1;
  227. return fpll / divider;
  228. }
  229. static unsigned long pll_div_freq_get(int pll, int div)
  230. {
  231. unsigned int fpll = pll_freq_get(pll);
  232. return __pll_div_freq_get(pll, fpll, div);
  233. }
  234. static void __pll_div_freq_set(int pll, unsigned int fpll, int div,
  235. unsigned long hz)
  236. {
  237. int divider = (fpll / hz - 1);
  238. divider &= pll_div_mask[pll];
  239. divider |= PLLDIV_ENABLE;
  240. __raw_writel(divider, (void *)pllctl_regs[pll] + pll_div_offset[div]);
  241. pllctl_reg_setbits(pll, alnctl, (1 << div));
  242. pllctl_reg_setbits(pll, dchange, (1 << div));
  243. }
  244. static unsigned long pll_div_freq_set(int pll, int div, unsigned long hz)
  245. {
  246. unsigned int fpll = pll_freq_get(pll);
  247. __pll_div_freq_set(pll, fpll, div, hz);
  248. pllctl_reg_write(pll, cmd, 1);
  249. /* Wait until new divider takes effect */
  250. while (pllctl_reg_read(pll, stat) & 0x01);
  251. return __pll_div_freq_get(pll, fpll, div);
  252. }
  253. unsigned long clk_get_rate(unsigned int clk)
  254. {
  255. return pll_div_freq_get(lpsc_clk_map[clk].pll, lpsc_clk_map[clk].div);
  256. }
  257. unsigned long clk_round_rate(unsigned int clk, unsigned long hz)
  258. {
  259. unsigned long fpll, divider, pll;
  260. pll = lpsc_clk_map[clk].pll;
  261. fpll = pll_freq_get(pll);
  262. divider = (fpll / hz - 1);
  263. divider &= pll_div_mask[pll];
  264. return fpll / (divider + 1);
  265. }
  266. int clk_set_rate(unsigned int clk, unsigned long _hz)
  267. {
  268. unsigned long hz;
  269. hz = clk_round_rate(clk, _hz);
  270. if (hz != _hz)
  271. return -EINVAL; /* Cannot set to target freq */
  272. pll_div_freq_set(lpsc_clk_map[clk].pll, lpsc_clk_map[clk].div, hz);
  273. return 0;
  274. }
  275. void lpsc_control(int mod, unsigned long state, int lrstz)
  276. {
  277. u32 mdctl;
  278. mdctl = psc_reg_read(PSC_MDCTL(mod));
  279. mdctl &= ~0x1f;
  280. mdctl |= state;
  281. if (lrstz == 0)
  282. mdctl &= ~PSC_MDCTL_LRSTZ;
  283. else if (lrstz == 1)
  284. mdctl |= PSC_MDCTL_LRSTZ;
  285. psc_reg_write(PSC_MDCTL(mod), mdctl);
  286. psc_reg_write(PSC_PTCMD, 1);
  287. /* wait for power domain transition to end */
  288. while (psc_reg_read(PSC_PTSTAT) & 1);
  289. /* Wait for module state change */
  290. while ((psc_reg_read(PSC_MDSTAT(mod)) & 0x1f) != state);
  291. }
  292. int lpsc_status(unsigned int id)
  293. {
  294. return psc_reg_read(PSC_MDSTAT(id)) & 0x1f;
  295. }
  296. static void init_pll(const struct pll_init_data *data)
  297. {
  298. unsigned long fpll;
  299. unsigned long best_pre = 0, best_post = 0, best_mult = 0;
  300. unsigned long div, prediv, postdiv, mult;
  301. unsigned long delta, actual;
  302. long best_delta = -1;
  303. int i;
  304. u32 tmp;
  305. if (data->pll == SYS_PLL)
  306. return; /* cannot reconfigure system pll on the fly */
  307. tmp = pllctl_reg_read(data->pll, ctl);
  308. if (data->internal_osc) {
  309. tmp &= ~PLLCTL_CLKMODE;
  310. fpll = CONFIG_SYS_INT_OSC_FREQ;
  311. } else {
  312. tmp |= PLLCTL_CLKMODE;
  313. fpll = pll_ext_freq[data->pll];
  314. }
  315. pllctl_reg_write(data->pll, ctl, tmp);
  316. mult = data->pll_freq / fpll;
  317. for (mult = MAX(mult, 1); mult <= MAX_MULT; mult++) {
  318. div = (fpll * mult) / data->pll_freq;
  319. if (div < 1 || div > MAX_DIV)
  320. continue;
  321. for (postdiv = 1; postdiv <= min(div, MAX_POSTDIV); postdiv++) {
  322. prediv = div / postdiv;
  323. if (prediv < 1 || prediv > MAX_PREDIV)
  324. continue;
  325. actual = (fpll / prediv) * (mult / postdiv);
  326. delta = (actual - data->pll_freq);
  327. if (delta < 0)
  328. delta = -delta;
  329. if ((delta < best_delta) || (best_delta == -1)) {
  330. best_delta = delta;
  331. best_mult = mult;
  332. best_pre = prediv;
  333. best_post = postdiv;
  334. if (delta == 0)
  335. goto done;
  336. }
  337. }
  338. }
  339. done:
  340. if (best_delta == -1) {
  341. printf("pll cannot derive %lu from %lu\n",
  342. data->pll_freq, fpll);
  343. return;
  344. }
  345. fpll = fpll * best_mult;
  346. fpll /= best_pre * best_post;
  347. pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLENSRC);
  348. pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLEN);
  349. pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLRST);
  350. pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLPWRDN);
  351. pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLDIS);
  352. sspll_reg_write(data->pll, mult_factor, (best_mult - 1) << 8);
  353. sspll_reg_write(data->pll, prediv, best_pre - 1);
  354. sspll_reg_write(data->pll, postdiv, best_post - 1);
  355. for (i = 0; i < 10; i++)
  356. if (data->div_freq[i])
  357. __pll_div_freq_set(data->pll, fpll, i,
  358. data->div_freq[i]);
  359. pllctl_reg_write(data->pll, cmd, 1);
  360. /* Wait until pll "go" operation completes */
  361. while (pllctl_reg_read(data->pll, stat) & 0x01);
  362. pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLRST);
  363. pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLEN);
  364. }
  365. void init_plls(int num_pll, struct pll_init_data *config)
  366. {
  367. int i;
  368. for (i = 0; i < num_pll; i++)
  369. init_pll(&config[i]);
  370. }