clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * arch/arm/mach-ep93xx/clock.c
  3. * Clock control for Cirrus EP93xx chips.
  4. *
  5. * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
  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 (at
  10. * your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/clk.h>
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/string.h>
  17. #include <linux/io.h>
  18. #include <linux/spinlock.h>
  19. #include <mach/hardware.h>
  20. #include <asm/clkdev.h>
  21. #include <asm/div64.h>
  22. struct clk {
  23. struct clk *parent;
  24. unsigned long rate;
  25. int users;
  26. int sw_locked;
  27. void __iomem *enable_reg;
  28. u32 enable_mask;
  29. unsigned long (*get_rate)(struct clk *clk);
  30. int (*set_rate)(struct clk *clk, unsigned long rate);
  31. };
  32. static unsigned long get_uart_rate(struct clk *clk);
  33. static int set_keytchclk_rate(struct clk *clk, unsigned long rate);
  34. static int set_div_rate(struct clk *clk, unsigned long rate);
  35. static struct clk clk_xtali = {
  36. .rate = EP93XX_EXT_CLK_RATE,
  37. };
  38. static struct clk clk_uart1 = {
  39. .parent = &clk_xtali,
  40. .sw_locked = 1,
  41. .enable_reg = EP93XX_SYSCON_DEVCFG,
  42. .enable_mask = EP93XX_SYSCON_DEVCFG_U1EN,
  43. .get_rate = get_uart_rate,
  44. };
  45. static struct clk clk_uart2 = {
  46. .parent = &clk_xtali,
  47. .sw_locked = 1,
  48. .enable_reg = EP93XX_SYSCON_DEVCFG,
  49. .enable_mask = EP93XX_SYSCON_DEVCFG_U2EN,
  50. .get_rate = get_uart_rate,
  51. };
  52. static struct clk clk_uart3 = {
  53. .parent = &clk_xtali,
  54. .sw_locked = 1,
  55. .enable_reg = EP93XX_SYSCON_DEVCFG,
  56. .enable_mask = EP93XX_SYSCON_DEVCFG_U3EN,
  57. .get_rate = get_uart_rate,
  58. };
  59. static struct clk clk_pll1 = {
  60. .parent = &clk_xtali,
  61. };
  62. static struct clk clk_f = {
  63. .parent = &clk_pll1,
  64. };
  65. static struct clk clk_h = {
  66. .parent = &clk_pll1,
  67. };
  68. static struct clk clk_p = {
  69. .parent = &clk_pll1,
  70. };
  71. static struct clk clk_pll2 = {
  72. .parent = &clk_xtali,
  73. };
  74. static struct clk clk_usb_host = {
  75. .parent = &clk_pll2,
  76. .enable_reg = EP93XX_SYSCON_PWRCNT,
  77. .enable_mask = EP93XX_SYSCON_PWRCNT_USH_EN,
  78. };
  79. static struct clk clk_keypad = {
  80. .parent = &clk_xtali,
  81. .sw_locked = 1,
  82. .enable_reg = EP93XX_SYSCON_KEYTCHCLKDIV,
  83. .enable_mask = EP93XX_SYSCON_KEYTCHCLKDIV_KEN,
  84. .set_rate = set_keytchclk_rate,
  85. };
  86. static struct clk clk_pwm = {
  87. .parent = &clk_xtali,
  88. .rate = EP93XX_EXT_CLK_RATE,
  89. };
  90. static struct clk clk_video = {
  91. .sw_locked = 1,
  92. .enable_reg = EP93XX_SYSCON_VIDCLKDIV,
  93. .enable_mask = EP93XX_SYSCON_CLKDIV_ENABLE,
  94. .set_rate = set_div_rate,
  95. };
  96. /* DMA Clocks */
  97. static struct clk clk_m2p0 = {
  98. .parent = &clk_h,
  99. .enable_reg = EP93XX_SYSCON_PWRCNT,
  100. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P0,
  101. };
  102. static struct clk clk_m2p1 = {
  103. .parent = &clk_h,
  104. .enable_reg = EP93XX_SYSCON_PWRCNT,
  105. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P1,
  106. };
  107. static struct clk clk_m2p2 = {
  108. .parent = &clk_h,
  109. .enable_reg = EP93XX_SYSCON_PWRCNT,
  110. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P2,
  111. };
  112. static struct clk clk_m2p3 = {
  113. .parent = &clk_h,
  114. .enable_reg = EP93XX_SYSCON_PWRCNT,
  115. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P3,
  116. };
  117. static struct clk clk_m2p4 = {
  118. .parent = &clk_h,
  119. .enable_reg = EP93XX_SYSCON_PWRCNT,
  120. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P4,
  121. };
  122. static struct clk clk_m2p5 = {
  123. .parent = &clk_h,
  124. .enable_reg = EP93XX_SYSCON_PWRCNT,
  125. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P5,
  126. };
  127. static struct clk clk_m2p6 = {
  128. .parent = &clk_h,
  129. .enable_reg = EP93XX_SYSCON_PWRCNT,
  130. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P6,
  131. };
  132. static struct clk clk_m2p7 = {
  133. .parent = &clk_h,
  134. .enable_reg = EP93XX_SYSCON_PWRCNT,
  135. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P7,
  136. };
  137. static struct clk clk_m2p8 = {
  138. .parent = &clk_h,
  139. .enable_reg = EP93XX_SYSCON_PWRCNT,
  140. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P8,
  141. };
  142. static struct clk clk_m2p9 = {
  143. .parent = &clk_h,
  144. .enable_reg = EP93XX_SYSCON_PWRCNT,
  145. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P9,
  146. };
  147. static struct clk clk_m2m0 = {
  148. .parent = &clk_h,
  149. .enable_reg = EP93XX_SYSCON_PWRCNT,
  150. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2M0,
  151. };
  152. static struct clk clk_m2m1 = {
  153. .parent = &clk_h,
  154. .enable_reg = EP93XX_SYSCON_PWRCNT,
  155. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2M1,
  156. };
  157. #define INIT_CK(dev,con,ck) \
  158. { .dev_id = dev, .con_id = con, .clk = ck }
  159. static struct clk_lookup clocks[] = {
  160. INIT_CK(NULL, "xtali", &clk_xtali),
  161. INIT_CK("apb:uart1", NULL, &clk_uart1),
  162. INIT_CK("apb:uart2", NULL, &clk_uart2),
  163. INIT_CK("apb:uart3", NULL, &clk_uart3),
  164. INIT_CK(NULL, "pll1", &clk_pll1),
  165. INIT_CK(NULL, "fclk", &clk_f),
  166. INIT_CK(NULL, "hclk", &clk_h),
  167. INIT_CK(NULL, "pclk", &clk_p),
  168. INIT_CK(NULL, "pll2", &clk_pll2),
  169. INIT_CK("ep93xx-ohci", NULL, &clk_usb_host),
  170. INIT_CK("ep93xx-keypad", NULL, &clk_keypad),
  171. INIT_CK("ep93xx-fb", NULL, &clk_video),
  172. INIT_CK(NULL, "pwm_clk", &clk_pwm),
  173. INIT_CK(NULL, "m2p0", &clk_m2p0),
  174. INIT_CK(NULL, "m2p1", &clk_m2p1),
  175. INIT_CK(NULL, "m2p2", &clk_m2p2),
  176. INIT_CK(NULL, "m2p3", &clk_m2p3),
  177. INIT_CK(NULL, "m2p4", &clk_m2p4),
  178. INIT_CK(NULL, "m2p5", &clk_m2p5),
  179. INIT_CK(NULL, "m2p6", &clk_m2p6),
  180. INIT_CK(NULL, "m2p7", &clk_m2p7),
  181. INIT_CK(NULL, "m2p8", &clk_m2p8),
  182. INIT_CK(NULL, "m2p9", &clk_m2p9),
  183. INIT_CK(NULL, "m2m0", &clk_m2m0),
  184. INIT_CK(NULL, "m2m1", &clk_m2m1),
  185. };
  186. static DEFINE_SPINLOCK(clk_lock);
  187. static void __clk_enable(struct clk *clk)
  188. {
  189. if (!clk->users++) {
  190. if (clk->parent)
  191. __clk_enable(clk->parent);
  192. if (clk->enable_reg) {
  193. u32 v;
  194. v = __raw_readl(clk->enable_reg);
  195. v |= clk->enable_mask;
  196. if (clk->sw_locked)
  197. ep93xx_syscon_swlocked_write(v, clk->enable_reg);
  198. else
  199. __raw_writel(v, clk->enable_reg);
  200. }
  201. }
  202. }
  203. int clk_enable(struct clk *clk)
  204. {
  205. unsigned long flags;
  206. if (!clk)
  207. return -EINVAL;
  208. spin_lock_irqsave(&clk_lock, flags);
  209. __clk_enable(clk);
  210. spin_unlock_irqrestore(&clk_lock, flags);
  211. return 0;
  212. }
  213. EXPORT_SYMBOL(clk_enable);
  214. static void __clk_disable(struct clk *clk)
  215. {
  216. if (!--clk->users) {
  217. if (clk->enable_reg) {
  218. u32 v;
  219. v = __raw_readl(clk->enable_reg);
  220. v &= ~clk->enable_mask;
  221. if (clk->sw_locked)
  222. ep93xx_syscon_swlocked_write(v, clk->enable_reg);
  223. else
  224. __raw_writel(v, clk->enable_reg);
  225. }
  226. if (clk->parent)
  227. __clk_disable(clk->parent);
  228. }
  229. }
  230. void clk_disable(struct clk *clk)
  231. {
  232. unsigned long flags;
  233. if (!clk)
  234. return;
  235. spin_lock_irqsave(&clk_lock, flags);
  236. __clk_disable(clk);
  237. spin_unlock_irqrestore(&clk_lock, flags);
  238. }
  239. EXPORT_SYMBOL(clk_disable);
  240. static unsigned long get_uart_rate(struct clk *clk)
  241. {
  242. unsigned long rate = clk_get_rate(clk->parent);
  243. u32 value;
  244. value = __raw_readl(EP93XX_SYSCON_PWRCNT);
  245. if (value & EP93XX_SYSCON_PWRCNT_UARTBAUD)
  246. return rate;
  247. else
  248. return rate / 2;
  249. }
  250. unsigned long clk_get_rate(struct clk *clk)
  251. {
  252. if (clk->get_rate)
  253. return clk->get_rate(clk);
  254. return clk->rate;
  255. }
  256. EXPORT_SYMBOL(clk_get_rate);
  257. static int set_keytchclk_rate(struct clk *clk, unsigned long rate)
  258. {
  259. u32 val;
  260. u32 div_bit;
  261. val = __raw_readl(clk->enable_reg);
  262. /*
  263. * The Key Matrix and ADC clocks are configured using the same
  264. * System Controller register. The clock used will be either
  265. * 1/4 or 1/16 the external clock rate depending on the
  266. * EP93XX_SYSCON_KEYTCHCLKDIV_KDIV/EP93XX_SYSCON_KEYTCHCLKDIV_ADIV
  267. * bit being set or cleared.
  268. */
  269. div_bit = clk->enable_mask >> 15;
  270. if (rate == EP93XX_KEYTCHCLK_DIV4)
  271. val |= div_bit;
  272. else if (rate == EP93XX_KEYTCHCLK_DIV16)
  273. val &= ~div_bit;
  274. else
  275. return -EINVAL;
  276. ep93xx_syscon_swlocked_write(val, clk->enable_reg);
  277. clk->rate = rate;
  278. return 0;
  279. }
  280. static int calc_clk_div(struct clk *clk, unsigned long rate,
  281. int *psel, int *esel, int *pdiv, int *div)
  282. {
  283. struct clk *mclk;
  284. unsigned long max_rate, actual_rate, mclk_rate, rate_err = -1;
  285. int i, found = 0, __div = 0, __pdiv = 0;
  286. /* Don't exceed the maximum rate */
  287. max_rate = max(max(clk_pll1.rate / 4, clk_pll2.rate / 4),
  288. clk_xtali.rate / 4);
  289. rate = min(rate, max_rate);
  290. /*
  291. * Try the two pll's and the external clock
  292. * Because the valid predividers are 2, 2.5 and 3, we multiply
  293. * all the clocks by 2 to avoid floating point math.
  294. *
  295. * This is based on the algorithm in the ep93xx raster guide:
  296. * http://be-a-maverick.com/en/pubs/appNote/AN269REV1.pdf
  297. *
  298. */
  299. for (i = 0; i < 3; i++) {
  300. if (i == 0)
  301. mclk = &clk_xtali;
  302. else if (i == 1)
  303. mclk = &clk_pll1;
  304. else
  305. mclk = &clk_pll2;
  306. mclk_rate = mclk->rate * 2;
  307. /* Try each predivider value */
  308. for (__pdiv = 4; __pdiv <= 6; __pdiv++) {
  309. __div = mclk_rate / (rate * __pdiv);
  310. if (__div < 2 || __div > 127)
  311. continue;
  312. actual_rate = mclk_rate / (__pdiv * __div);
  313. if (!found || abs(actual_rate - rate) < rate_err) {
  314. *pdiv = __pdiv - 3;
  315. *div = __div;
  316. *psel = (i == 2);
  317. *esel = (i != 0);
  318. clk->parent = mclk;
  319. clk->rate = actual_rate;
  320. rate_err = abs(actual_rate - rate);
  321. found = 1;
  322. }
  323. }
  324. }
  325. if (!found)
  326. return -EINVAL;
  327. return 0;
  328. }
  329. static int set_div_rate(struct clk *clk, unsigned long rate)
  330. {
  331. int err, psel = 0, esel = 0, pdiv = 0, div = 0;
  332. u32 val;
  333. err = calc_clk_div(clk, rate, &psel, &esel, &pdiv, &div);
  334. if (err)
  335. return err;
  336. /* Clear the esel, psel, pdiv and div bits */
  337. val = __raw_readl(clk->enable_reg);
  338. val &= ~0x7fff;
  339. /* Set the new esel, psel, pdiv and div bits for the new clock rate */
  340. val |= (esel ? EP93XX_SYSCON_CLKDIV_ESEL : 0) |
  341. (psel ? EP93XX_SYSCON_CLKDIV_PSEL : 0) |
  342. (pdiv << EP93XX_SYSCON_CLKDIV_PDIV_SHIFT) | div;
  343. ep93xx_syscon_swlocked_write(val, clk->enable_reg);
  344. return 0;
  345. }
  346. int clk_set_rate(struct clk *clk, unsigned long rate)
  347. {
  348. if (clk->set_rate)
  349. return clk->set_rate(clk, rate);
  350. return -EINVAL;
  351. }
  352. EXPORT_SYMBOL(clk_set_rate);
  353. static char fclk_divisors[] = { 1, 2, 4, 8, 16, 1, 1, 1 };
  354. static char hclk_divisors[] = { 1, 2, 4, 5, 6, 8, 16, 32 };
  355. static char pclk_divisors[] = { 1, 2, 4, 8 };
  356. /*
  357. * PLL rate = 14.7456 MHz * (X1FBD + 1) * (X2FBD + 1) / (X2IPD + 1) / 2^PS
  358. */
  359. static unsigned long calc_pll_rate(u32 config_word)
  360. {
  361. unsigned long long rate;
  362. int i;
  363. rate = clk_xtali.rate;
  364. rate *= ((config_word >> 11) & 0x1f) + 1; /* X1FBD */
  365. rate *= ((config_word >> 5) & 0x3f) + 1; /* X2FBD */
  366. do_div(rate, (config_word & 0x1f) + 1); /* X2IPD */
  367. for (i = 0; i < ((config_word >> 16) & 3); i++) /* PS */
  368. rate >>= 1;
  369. return (unsigned long)rate;
  370. }
  371. static void __init ep93xx_dma_clock_init(void)
  372. {
  373. clk_m2p0.rate = clk_h.rate;
  374. clk_m2p1.rate = clk_h.rate;
  375. clk_m2p2.rate = clk_h.rate;
  376. clk_m2p3.rate = clk_h.rate;
  377. clk_m2p4.rate = clk_h.rate;
  378. clk_m2p5.rate = clk_h.rate;
  379. clk_m2p6.rate = clk_h.rate;
  380. clk_m2p7.rate = clk_h.rate;
  381. clk_m2p8.rate = clk_h.rate;
  382. clk_m2p9.rate = clk_h.rate;
  383. clk_m2m0.rate = clk_h.rate;
  384. clk_m2m1.rate = clk_h.rate;
  385. }
  386. static int __init ep93xx_clock_init(void)
  387. {
  388. u32 value;
  389. int i;
  390. value = __raw_readl(EP93XX_SYSCON_CLOCK_SET1);
  391. if (!(value & 0x00800000)) { /* PLL1 bypassed? */
  392. clk_pll1.rate = clk_xtali.rate;
  393. } else {
  394. clk_pll1.rate = calc_pll_rate(value);
  395. }
  396. clk_f.rate = clk_pll1.rate / fclk_divisors[(value >> 25) & 0x7];
  397. clk_h.rate = clk_pll1.rate / hclk_divisors[(value >> 20) & 0x7];
  398. clk_p.rate = clk_h.rate / pclk_divisors[(value >> 18) & 0x3];
  399. ep93xx_dma_clock_init();
  400. value = __raw_readl(EP93XX_SYSCON_CLOCK_SET2);
  401. if (!(value & 0x00080000)) { /* PLL2 bypassed? */
  402. clk_pll2.rate = clk_xtali.rate;
  403. } else if (value & 0x00040000) { /* PLL2 enabled? */
  404. clk_pll2.rate = calc_pll_rate(value);
  405. } else {
  406. clk_pll2.rate = 0;
  407. }
  408. clk_usb_host.rate = clk_pll2.rate / (((value >> 28) & 0xf) + 1);
  409. printk(KERN_INFO "ep93xx: PLL1 running at %ld MHz, PLL2 at %ld MHz\n",
  410. clk_pll1.rate / 1000000, clk_pll2.rate / 1000000);
  411. printk(KERN_INFO "ep93xx: FCLK %ld MHz, HCLK %ld MHz, PCLK %ld MHz\n",
  412. clk_f.rate / 1000000, clk_h.rate / 1000000,
  413. clk_p.rate / 1000000);
  414. for (i = 0; i < ARRAY_SIZE(clocks); i++)
  415. clkdev_add(&clocks[i]);
  416. return 0;
  417. }
  418. arch_initcall(ep93xx_clock_init);