clk-vt8500.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * Clock implementation for VIA/Wondermedia SoC's
  3. * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/io.h>
  16. #include <linux/of.h>
  17. #include <linux/slab.h>
  18. #include <linux/bitops.h>
  19. #include <linux/clkdev.h>
  20. #include <linux/clk-provider.h>
  21. /* All clocks share the same lock as none can be changed concurrently */
  22. static DEFINE_SPINLOCK(_lock);
  23. struct clk_device {
  24. struct clk_hw hw;
  25. void __iomem *div_reg;
  26. unsigned int div_mask;
  27. void __iomem *en_reg;
  28. int en_bit;
  29. spinlock_t *lock;
  30. };
  31. /*
  32. * Add new PLL_TYPE_x definitions here as required. Use the first known model
  33. * to support the new type as the name.
  34. * Add case statements to vtwm_pll_recalc_rate(), vtwm_pll_round_round() and
  35. * vtwm_pll_set_rate() to handle the new PLL_TYPE_x
  36. */
  37. #define PLL_TYPE_VT8500 0
  38. #define PLL_TYPE_WM8650 1
  39. #define PLL_TYPE_WM8750 2
  40. struct clk_pll {
  41. struct clk_hw hw;
  42. void __iomem *reg;
  43. spinlock_t *lock;
  44. int type;
  45. };
  46. static void __iomem *pmc_base;
  47. #define to_clk_device(_hw) container_of(_hw, struct clk_device, hw)
  48. #define VT8500_PMC_BUSY_MASK 0x18
  49. static void vt8500_pmc_wait_busy(void)
  50. {
  51. while (readl(pmc_base) & VT8500_PMC_BUSY_MASK)
  52. cpu_relax();
  53. }
  54. static int vt8500_dclk_enable(struct clk_hw *hw)
  55. {
  56. struct clk_device *cdev = to_clk_device(hw);
  57. u32 en_val;
  58. unsigned long flags = 0;
  59. spin_lock_irqsave(cdev->lock, flags);
  60. en_val = readl(cdev->en_reg);
  61. en_val |= BIT(cdev->en_bit);
  62. writel(en_val, cdev->en_reg);
  63. spin_unlock_irqrestore(cdev->lock, flags);
  64. return 0;
  65. }
  66. static void vt8500_dclk_disable(struct clk_hw *hw)
  67. {
  68. struct clk_device *cdev = to_clk_device(hw);
  69. u32 en_val;
  70. unsigned long flags = 0;
  71. spin_lock_irqsave(cdev->lock, flags);
  72. en_val = readl(cdev->en_reg);
  73. en_val &= ~BIT(cdev->en_bit);
  74. writel(en_val, cdev->en_reg);
  75. spin_unlock_irqrestore(cdev->lock, flags);
  76. }
  77. static int vt8500_dclk_is_enabled(struct clk_hw *hw)
  78. {
  79. struct clk_device *cdev = to_clk_device(hw);
  80. u32 en_val = (readl(cdev->en_reg) & BIT(cdev->en_bit));
  81. return en_val ? 1 : 0;
  82. }
  83. static unsigned long vt8500_dclk_recalc_rate(struct clk_hw *hw,
  84. unsigned long parent_rate)
  85. {
  86. struct clk_device *cdev = to_clk_device(hw);
  87. u32 div = readl(cdev->div_reg) & cdev->div_mask;
  88. /* Special case for SDMMC devices */
  89. if ((cdev->div_mask == 0x3F) && (div & BIT(5)))
  90. div = 64 * (div & 0x1f);
  91. /* div == 0 is actually the highest divisor */
  92. if (div == 0)
  93. div = (cdev->div_mask + 1);
  94. return parent_rate / div;
  95. }
  96. static long vt8500_dclk_round_rate(struct clk_hw *hw, unsigned long rate,
  97. unsigned long *prate)
  98. {
  99. struct clk_device *cdev = to_clk_device(hw);
  100. u32 divisor;
  101. if (rate == 0)
  102. return 0;
  103. divisor = *prate / rate;
  104. /* If prate / rate would be decimal, incr the divisor */
  105. if (rate * divisor < *prate)
  106. divisor++;
  107. /*
  108. * If this is a request for SDMMC we have to adjust the divisor
  109. * when >31 to use the fixed predivisor
  110. */
  111. if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
  112. divisor = 64 * ((divisor / 64) + 1);
  113. }
  114. return *prate / divisor;
  115. }
  116. static int vt8500_dclk_set_rate(struct clk_hw *hw, unsigned long rate,
  117. unsigned long parent_rate)
  118. {
  119. struct clk_device *cdev = to_clk_device(hw);
  120. u32 divisor;
  121. unsigned long flags = 0;
  122. if (rate == 0)
  123. return 0;
  124. divisor = parent_rate / rate;
  125. /* If prate / rate would be decimal, incr the divisor */
  126. if (rate * divisor < parent_rate)
  127. divisor++;
  128. if (divisor == cdev->div_mask + 1)
  129. divisor = 0;
  130. /* SDMMC mask may need to be corrected before testing if its valid */
  131. if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
  132. /*
  133. * Bit 5 is a fixed /64 predivisor. If the requested divisor
  134. * is >31 then correct for the fixed divisor being required.
  135. */
  136. divisor = 0x20 + (divisor / 64);
  137. }
  138. if (divisor > cdev->div_mask) {
  139. pr_err("%s: invalid divisor for clock\n", __func__);
  140. return -EINVAL;
  141. }
  142. spin_lock_irqsave(cdev->lock, flags);
  143. vt8500_pmc_wait_busy();
  144. writel(divisor, cdev->div_reg);
  145. vt8500_pmc_wait_busy();
  146. spin_lock_irqsave(cdev->lock, flags);
  147. return 0;
  148. }
  149. static const struct clk_ops vt8500_gated_clk_ops = {
  150. .enable = vt8500_dclk_enable,
  151. .disable = vt8500_dclk_disable,
  152. .is_enabled = vt8500_dclk_is_enabled,
  153. };
  154. static const struct clk_ops vt8500_divisor_clk_ops = {
  155. .round_rate = vt8500_dclk_round_rate,
  156. .set_rate = vt8500_dclk_set_rate,
  157. .recalc_rate = vt8500_dclk_recalc_rate,
  158. };
  159. static const struct clk_ops vt8500_gated_divisor_clk_ops = {
  160. .enable = vt8500_dclk_enable,
  161. .disable = vt8500_dclk_disable,
  162. .is_enabled = vt8500_dclk_is_enabled,
  163. .round_rate = vt8500_dclk_round_rate,
  164. .set_rate = vt8500_dclk_set_rate,
  165. .recalc_rate = vt8500_dclk_recalc_rate,
  166. };
  167. #define CLK_INIT_GATED BIT(0)
  168. #define CLK_INIT_DIVISOR BIT(1)
  169. #define CLK_INIT_GATED_DIVISOR (CLK_INIT_DIVISOR | CLK_INIT_GATED)
  170. static __init void vtwm_device_clk_init(struct device_node *node)
  171. {
  172. u32 en_reg, div_reg;
  173. struct clk *clk;
  174. struct clk_device *dev_clk;
  175. const char *clk_name = node->name;
  176. const char *parent_name;
  177. struct clk_init_data init;
  178. int rc;
  179. int clk_init_flags = 0;
  180. dev_clk = kzalloc(sizeof(*dev_clk), GFP_KERNEL);
  181. if (WARN_ON(!dev_clk))
  182. return;
  183. dev_clk->lock = &_lock;
  184. rc = of_property_read_u32(node, "enable-reg", &en_reg);
  185. if (!rc) {
  186. dev_clk->en_reg = pmc_base + en_reg;
  187. rc = of_property_read_u32(node, "enable-bit", &dev_clk->en_bit);
  188. if (rc) {
  189. pr_err("%s: enable-bit property required for gated clock\n",
  190. __func__);
  191. return;
  192. }
  193. clk_init_flags |= CLK_INIT_GATED;
  194. }
  195. rc = of_property_read_u32(node, "divisor-reg", &div_reg);
  196. if (!rc) {
  197. dev_clk->div_reg = pmc_base + div_reg;
  198. /*
  199. * use 0x1f as the default mask since it covers
  200. * almost all the clocks and reduces dts properties
  201. */
  202. dev_clk->div_mask = 0x1f;
  203. of_property_read_u32(node, "divisor-mask", &dev_clk->div_mask);
  204. clk_init_flags |= CLK_INIT_DIVISOR;
  205. }
  206. of_property_read_string(node, "clock-output-names", &clk_name);
  207. switch (clk_init_flags) {
  208. case CLK_INIT_GATED:
  209. init.ops = &vt8500_gated_clk_ops;
  210. break;
  211. case CLK_INIT_DIVISOR:
  212. init.ops = &vt8500_divisor_clk_ops;
  213. break;
  214. case CLK_INIT_GATED_DIVISOR:
  215. init.ops = &vt8500_gated_divisor_clk_ops;
  216. break;
  217. default:
  218. pr_err("%s: Invalid clock description in device tree\n",
  219. __func__);
  220. kfree(dev_clk);
  221. return;
  222. }
  223. init.name = clk_name;
  224. init.flags = 0;
  225. parent_name = of_clk_get_parent_name(node, 0);
  226. init.parent_names = &parent_name;
  227. init.num_parents = 1;
  228. dev_clk->hw.init = &init;
  229. clk = clk_register(NULL, &dev_clk->hw);
  230. if (WARN_ON(IS_ERR(clk))) {
  231. kfree(dev_clk);
  232. return;
  233. }
  234. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  235. clk_register_clkdev(clk, clk_name, NULL);
  236. }
  237. CLK_OF_DECLARE(vt8500_device, "via,vt8500-device-clock", vtwm_device_clk_init);
  238. /* PLL clock related functions */
  239. #define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
  240. /* Helper macros for PLL_VT8500 */
  241. #define VT8500_PLL_MUL(x) ((x & 0x1F) << 1)
  242. #define VT8500_PLL_DIV(x) ((x & 0x100) ? 1 : 2)
  243. #define VT8500_BITS_TO_FREQ(r, m, d) \
  244. ((r / d) * m)
  245. #define VT8500_BITS_TO_VAL(m, d) \
  246. ((d == 2 ? 0 : 0x100) | ((m >> 1) & 0x1F))
  247. /* Helper macros for PLL_WM8650 */
  248. #define WM8650_PLL_MUL(x) (x & 0x3FF)
  249. #define WM8650_PLL_DIV(x) (((x >> 10) & 7) * (1 << ((x >> 13) & 3)))
  250. #define WM8650_BITS_TO_FREQ(r, m, d1, d2) \
  251. (r * m / (d1 * (1 << d2)))
  252. #define WM8650_BITS_TO_VAL(m, d1, d2) \
  253. ((d2 << 13) | (d1 << 10) | (m & 0x3FF))
  254. /* Helper macros for PLL_WM8750 */
  255. #define WM8750_PLL_MUL(x) (((x >> 16) & 0xFF) + 1)
  256. #define WM8750_PLL_DIV(x) ((((x >> 8) & 1) + 1) * (1 << (x & 7)))
  257. #define WM8750_BITS_TO_FREQ(r, m, d1, d2) \
  258. (r * (m+1) / ((d1+1) * (1 << d2)))
  259. #define WM8750_BITS_TO_VAL(f, m, d1, d2) \
  260. ((f << 24) | ((m - 1) << 16) | ((d1 - 1) << 8) | d2)
  261. static void vt8500_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  262. u32 *multiplier, u32 *prediv)
  263. {
  264. unsigned long tclk;
  265. /* sanity check */
  266. if ((rate < parent_rate * 4) || (rate > parent_rate * 62)) {
  267. pr_err("%s: requested rate out of range\n", __func__);
  268. *multiplier = 0;
  269. *prediv = 1;
  270. return;
  271. }
  272. if (rate <= parent_rate * 31)
  273. /* use the prediv to double the resolution */
  274. *prediv = 2;
  275. else
  276. *prediv = 1;
  277. *multiplier = rate / (parent_rate / *prediv);
  278. tclk = (parent_rate / *prediv) * *multiplier;
  279. if (tclk != rate)
  280. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__,
  281. rate, tclk);
  282. }
  283. static void wm8650_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  284. u32 *multiplier, u32 *divisor1, u32 *divisor2)
  285. {
  286. u32 mul, div1, div2;
  287. u32 best_mul, best_div1, best_div2;
  288. unsigned long tclk, rate_err, best_err;
  289. best_err = (unsigned long)-1;
  290. /* Find the closest match (lower or equal to requested) */
  291. for (div1 = 5; div1 >= 3; div1--)
  292. for (div2 = 3; div2 >= 0; div2--)
  293. for (mul = 3; mul <= 1023; mul++) {
  294. tclk = parent_rate * mul / (div1 * (1 << div2));
  295. if (tclk > rate)
  296. continue;
  297. /* error will always be +ve */
  298. rate_err = rate - tclk;
  299. if (rate_err == 0) {
  300. *multiplier = mul;
  301. *divisor1 = div1;
  302. *divisor2 = div2;
  303. return;
  304. }
  305. if (rate_err < best_err) {
  306. best_err = rate_err;
  307. best_mul = mul;
  308. best_div1 = div1;
  309. best_div2 = div2;
  310. }
  311. }
  312. /* if we got here, it wasn't an exact match */
  313. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
  314. rate - best_err);
  315. *multiplier = best_mul;
  316. *divisor1 = best_div1;
  317. *divisor2 = best_div2;
  318. }
  319. static u32 wm8750_get_filter(u32 parent_rate, u32 divisor1)
  320. {
  321. /* calculate frequency (MHz) after pre-divisor */
  322. u32 freq = (parent_rate / 1000000) / (divisor1 + 1);
  323. if ((freq < 10) || (freq > 200))
  324. pr_warn("%s: PLL recommended input frequency 10..200Mhz (requested %d Mhz)\n",
  325. __func__, freq);
  326. if (freq >= 166)
  327. return 7;
  328. else if (freq >= 104)
  329. return 6;
  330. else if (freq >= 65)
  331. return 5;
  332. else if (freq >= 42)
  333. return 4;
  334. else if (freq >= 26)
  335. return 3;
  336. else if (freq >= 16)
  337. return 2;
  338. else if (freq >= 10)
  339. return 1;
  340. return 0;
  341. }
  342. static void wm8750_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  343. u32 *filter, u32 *multiplier, u32 *divisor1, u32 *divisor2)
  344. {
  345. u32 mul, div1, div2;
  346. u32 best_mul, best_div1, best_div2;
  347. unsigned long tclk, rate_err, best_err;
  348. best_err = (unsigned long)-1;
  349. /* Find the closest match (lower or equal to requested) */
  350. for (div1 = 1; div1 >= 0; div1--)
  351. for (div2 = 7; div2 >= 0; div2--)
  352. for (mul = 0; mul <= 255; mul++) {
  353. tclk = parent_rate * (mul + 1) / ((div1 + 1) * (1 << div2));
  354. if (tclk > rate)
  355. continue;
  356. /* error will always be +ve */
  357. rate_err = rate - tclk;
  358. if (rate_err == 0) {
  359. *filter = wm8750_get_filter(parent_rate, div1);
  360. *multiplier = mul;
  361. *divisor1 = div1;
  362. *divisor2 = div2;
  363. return;
  364. }
  365. if (rate_err < best_err) {
  366. best_err = rate_err;
  367. best_mul = mul;
  368. best_div1 = div1;
  369. best_div2 = div2;
  370. }
  371. }
  372. /* if we got here, it wasn't an exact match */
  373. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
  374. rate - best_err);
  375. *filter = wm8750_get_filter(parent_rate, best_div1);
  376. *multiplier = best_mul;
  377. *divisor1 = best_div1;
  378. *divisor2 = best_div2;
  379. }
  380. static int vtwm_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  381. unsigned long parent_rate)
  382. {
  383. struct clk_pll *pll = to_clk_pll(hw);
  384. u32 filter, mul, div1, div2;
  385. u32 pll_val;
  386. unsigned long flags = 0;
  387. /* sanity check */
  388. switch (pll->type) {
  389. case PLL_TYPE_VT8500:
  390. vt8500_find_pll_bits(rate, parent_rate, &mul, &div1);
  391. pll_val = VT8500_BITS_TO_VAL(mul, div1);
  392. break;
  393. case PLL_TYPE_WM8650:
  394. wm8650_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
  395. pll_val = WM8650_BITS_TO_VAL(mul, div1, div2);
  396. break;
  397. case PLL_TYPE_WM8750:
  398. wm8750_find_pll_bits(rate, parent_rate, &filter, &mul, &div1, &div2);
  399. pll_val = WM8750_BITS_TO_VAL(filter, mul, div1, div2);
  400. default:
  401. pr_err("%s: invalid pll type\n", __func__);
  402. return 0;
  403. }
  404. spin_lock_irqsave(pll->lock, flags);
  405. vt8500_pmc_wait_busy();
  406. writel(pll_val, pll->reg);
  407. vt8500_pmc_wait_busy();
  408. spin_unlock_irqrestore(pll->lock, flags);
  409. return 0;
  410. }
  411. static long vtwm_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  412. unsigned long *prate)
  413. {
  414. struct clk_pll *pll = to_clk_pll(hw);
  415. u32 filter, mul, div1, div2;
  416. long round_rate;
  417. switch (pll->type) {
  418. case PLL_TYPE_VT8500:
  419. vt8500_find_pll_bits(rate, *prate, &mul, &div1);
  420. round_rate = VT8500_BITS_TO_FREQ(*prate, mul, div1);
  421. break;
  422. case PLL_TYPE_WM8650:
  423. wm8650_find_pll_bits(rate, *prate, &mul, &div1, &div2);
  424. round_rate = WM8650_BITS_TO_FREQ(*prate, mul, div1, div2);
  425. break;
  426. case PLL_TYPE_WM8750:
  427. wm8750_find_pll_bits(rate, *prate, &filter, &mul, &div1, &div2);
  428. round_rate = WM8750_BITS_TO_FREQ(*prate, mul, div1, div2);
  429. default:
  430. round_rate = 0;
  431. }
  432. return round_rate;
  433. }
  434. static unsigned long vtwm_pll_recalc_rate(struct clk_hw *hw,
  435. unsigned long parent_rate)
  436. {
  437. struct clk_pll *pll = to_clk_pll(hw);
  438. u32 pll_val = readl(pll->reg);
  439. unsigned long pll_freq;
  440. switch (pll->type) {
  441. case PLL_TYPE_VT8500:
  442. pll_freq = parent_rate * VT8500_PLL_MUL(pll_val);
  443. pll_freq /= VT8500_PLL_DIV(pll_val);
  444. break;
  445. case PLL_TYPE_WM8650:
  446. pll_freq = parent_rate * WM8650_PLL_MUL(pll_val);
  447. pll_freq /= WM8650_PLL_DIV(pll_val);
  448. break;
  449. case PLL_TYPE_WM8750:
  450. pll_freq = parent_rate * WM8750_PLL_MUL(pll_val);
  451. pll_freq /= WM8750_PLL_DIV(pll_val);
  452. break;
  453. default:
  454. pll_freq = 0;
  455. }
  456. return pll_freq;
  457. }
  458. const struct clk_ops vtwm_pll_ops = {
  459. .round_rate = vtwm_pll_round_rate,
  460. .set_rate = vtwm_pll_set_rate,
  461. .recalc_rate = vtwm_pll_recalc_rate,
  462. };
  463. static __init void vtwm_pll_clk_init(struct device_node *node, int pll_type)
  464. {
  465. u32 reg;
  466. struct clk *clk;
  467. struct clk_pll *pll_clk;
  468. const char *clk_name = node->name;
  469. const char *parent_name;
  470. struct clk_init_data init;
  471. int rc;
  472. rc = of_property_read_u32(node, "reg", &reg);
  473. if (WARN_ON(rc))
  474. return;
  475. pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
  476. if (WARN_ON(!pll_clk))
  477. return;
  478. pll_clk->reg = pmc_base + reg;
  479. pll_clk->lock = &_lock;
  480. pll_clk->type = pll_type;
  481. of_property_read_string(node, "clock-output-names", &clk_name);
  482. init.name = clk_name;
  483. init.ops = &vtwm_pll_ops;
  484. init.flags = 0;
  485. parent_name = of_clk_get_parent_name(node, 0);
  486. init.parent_names = &parent_name;
  487. init.num_parents = 1;
  488. pll_clk->hw.init = &init;
  489. clk = clk_register(NULL, &pll_clk->hw);
  490. if (WARN_ON(IS_ERR(clk))) {
  491. kfree(pll_clk);
  492. return;
  493. }
  494. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  495. clk_register_clkdev(clk, clk_name, NULL);
  496. }
  497. /* Wrappers for initialization functions */
  498. static void __init vt8500_pll_init(struct device_node *node)
  499. {
  500. vtwm_pll_clk_init(node, PLL_TYPE_VT8500);
  501. }
  502. CLK_OF_DECLARE(vt8500_pll, "via,vt8500-pll-clock", vt8500_pll_init);
  503. static void __init wm8650_pll_init(struct device_node *node)
  504. {
  505. vtwm_pll_clk_init(node, PLL_TYPE_WM8650);
  506. }
  507. CLK_OF_DECLARE(wm8650_pll, "wm,wm8650-pll-clock", wm8650_pll_init);
  508. static void __init wm8750_pll_init(struct device_node *node)
  509. {
  510. vtwm_pll_clk_init(node, PLL_TYPE_WM8750);
  511. }
  512. CLK_OF_DECLARE(wm8750_pll, "wm,wm8750-pll-clock", wm8750_pll_init);
  513. void __init vtwm_clk_init(void __iomem *base)
  514. {
  515. if (!base)
  516. return;
  517. pmc_base = base;
  518. of_clk_init(NULL);
  519. }