clk-vt8500.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. struct clk_pll {
  40. struct clk_hw hw;
  41. void __iomem *reg;
  42. spinlock_t *lock;
  43. int type;
  44. };
  45. static void __iomem *pmc_base;
  46. #define to_clk_device(_hw) container_of(_hw, struct clk_device, hw)
  47. #define VT8500_PMC_BUSY_MASK 0x18
  48. static void vt8500_pmc_wait_busy(void)
  49. {
  50. while (readl(pmc_base) & VT8500_PMC_BUSY_MASK)
  51. cpu_relax();
  52. }
  53. static int vt8500_dclk_enable(struct clk_hw *hw)
  54. {
  55. struct clk_device *cdev = to_clk_device(hw);
  56. u32 en_val;
  57. unsigned long flags = 0;
  58. spin_lock_irqsave(cdev->lock, flags);
  59. en_val = readl(cdev->en_reg);
  60. en_val |= BIT(cdev->en_bit);
  61. writel(en_val, cdev->en_reg);
  62. spin_unlock_irqrestore(cdev->lock, flags);
  63. return 0;
  64. }
  65. static void vt8500_dclk_disable(struct clk_hw *hw)
  66. {
  67. struct clk_device *cdev = to_clk_device(hw);
  68. u32 en_val;
  69. unsigned long flags = 0;
  70. spin_lock_irqsave(cdev->lock, flags);
  71. en_val = readl(cdev->en_reg);
  72. en_val &= ~BIT(cdev->en_bit);
  73. writel(en_val, cdev->en_reg);
  74. spin_unlock_irqrestore(cdev->lock, flags);
  75. }
  76. static int vt8500_dclk_is_enabled(struct clk_hw *hw)
  77. {
  78. struct clk_device *cdev = to_clk_device(hw);
  79. u32 en_val = (readl(cdev->en_reg) & BIT(cdev->en_bit));
  80. return en_val ? 1 : 0;
  81. }
  82. static unsigned long vt8500_dclk_recalc_rate(struct clk_hw *hw,
  83. unsigned long parent_rate)
  84. {
  85. struct clk_device *cdev = to_clk_device(hw);
  86. u32 div = readl(cdev->div_reg) & cdev->div_mask;
  87. /* Special case for SDMMC devices */
  88. if ((cdev->div_mask == 0x3F) && (div & BIT(5)))
  89. div = 64 * (div & 0x1f);
  90. /* div == 0 is actually the highest divisor */
  91. if (div == 0)
  92. div = (cdev->div_mask + 1);
  93. return parent_rate / div;
  94. }
  95. static long vt8500_dclk_round_rate(struct clk_hw *hw, unsigned long rate,
  96. unsigned long *prate)
  97. {
  98. struct clk_device *cdev = to_clk_device(hw);
  99. u32 divisor = *prate / rate;
  100. /* If prate / rate would be decimal, incr the divisor */
  101. if (rate * divisor < *prate)
  102. divisor++;
  103. /*
  104. * If this is a request for SDMMC we have to adjust the divisor
  105. * when >31 to use the fixed predivisor
  106. */
  107. if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
  108. divisor = 64 * ((divisor / 64) + 1);
  109. }
  110. return *prate / divisor;
  111. }
  112. static int vt8500_dclk_set_rate(struct clk_hw *hw, unsigned long rate,
  113. unsigned long parent_rate)
  114. {
  115. struct clk_device *cdev = to_clk_device(hw);
  116. u32 divisor = parent_rate / rate;
  117. unsigned long flags = 0;
  118. /* If prate / rate would be decimal, incr the divisor */
  119. if (rate * divisor < *prate)
  120. divisor++;
  121. if (divisor == cdev->div_mask + 1)
  122. divisor = 0;
  123. /* SDMMC mask may need to be corrected before testing if its valid */
  124. if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
  125. /*
  126. * Bit 5 is a fixed /64 predivisor. If the requested divisor
  127. * is >31 then correct for the fixed divisor being required.
  128. */
  129. divisor = 0x20 + (divisor / 64);
  130. }
  131. if (divisor > cdev->div_mask) {
  132. pr_err("%s: invalid divisor for clock\n", __func__);
  133. return -EINVAL;
  134. }
  135. spin_lock_irqsave(cdev->lock, flags);
  136. vt8500_pmc_wait_busy();
  137. writel(divisor, cdev->div_reg);
  138. vt8500_pmc_wait_busy();
  139. spin_lock_irqsave(cdev->lock, flags);
  140. return 0;
  141. }
  142. static const struct clk_ops vt8500_gated_clk_ops = {
  143. .enable = vt8500_dclk_enable,
  144. .disable = vt8500_dclk_disable,
  145. .is_enabled = vt8500_dclk_is_enabled,
  146. };
  147. static const struct clk_ops vt8500_divisor_clk_ops = {
  148. .round_rate = vt8500_dclk_round_rate,
  149. .set_rate = vt8500_dclk_set_rate,
  150. .recalc_rate = vt8500_dclk_recalc_rate,
  151. };
  152. static const struct clk_ops vt8500_gated_divisor_clk_ops = {
  153. .enable = vt8500_dclk_enable,
  154. .disable = vt8500_dclk_disable,
  155. .is_enabled = vt8500_dclk_is_enabled,
  156. .round_rate = vt8500_dclk_round_rate,
  157. .set_rate = vt8500_dclk_set_rate,
  158. .recalc_rate = vt8500_dclk_recalc_rate,
  159. };
  160. #define CLK_INIT_GATED BIT(0)
  161. #define CLK_INIT_DIVISOR BIT(1)
  162. #define CLK_INIT_GATED_DIVISOR (CLK_INIT_DIVISOR | CLK_INIT_GATED)
  163. static __init void vtwm_device_clk_init(struct device_node *node)
  164. {
  165. u32 en_reg, div_reg;
  166. struct clk *clk;
  167. struct clk_device *dev_clk;
  168. const char *clk_name = node->name;
  169. const char *parent_name;
  170. struct clk_init_data init;
  171. int rc;
  172. int clk_init_flags = 0;
  173. dev_clk = kzalloc(sizeof(*dev_clk), GFP_KERNEL);
  174. if (WARN_ON(!dev_clk))
  175. return;
  176. dev_clk->lock = &_lock;
  177. rc = of_property_read_u32(node, "enable-reg", &en_reg);
  178. if (!rc) {
  179. dev_clk->en_reg = pmc_base + en_reg;
  180. rc = of_property_read_u32(node, "enable-bit", &dev_clk->en_bit);
  181. if (rc) {
  182. pr_err("%s: enable-bit property required for gated clock\n",
  183. __func__);
  184. return;
  185. }
  186. clk_init_flags |= CLK_INIT_GATED;
  187. }
  188. rc = of_property_read_u32(node, "divisor-reg", &div_reg);
  189. if (!rc) {
  190. dev_clk->div_reg = pmc_base + div_reg;
  191. /*
  192. * use 0x1f as the default mask since it covers
  193. * almost all the clocks and reduces dts properties
  194. */
  195. dev_clk->div_mask = 0x1f;
  196. of_property_read_u32(node, "divisor-mask", &dev_clk->div_mask);
  197. clk_init_flags |= CLK_INIT_DIVISOR;
  198. }
  199. of_property_read_string(node, "clock-output-names", &clk_name);
  200. switch (clk_init_flags) {
  201. case CLK_INIT_GATED:
  202. init.ops = &vt8500_gated_clk_ops;
  203. break;
  204. case CLK_INIT_DIVISOR:
  205. init.ops = &vt8500_divisor_clk_ops;
  206. break;
  207. case CLK_INIT_GATED_DIVISOR:
  208. init.ops = &vt8500_gated_divisor_clk_ops;
  209. break;
  210. default:
  211. pr_err("%s: Invalid clock description in device tree\n",
  212. __func__);
  213. kfree(dev_clk);
  214. return;
  215. }
  216. init.name = clk_name;
  217. init.flags = 0;
  218. parent_name = of_clk_get_parent_name(node, 0);
  219. init.parent_names = &parent_name;
  220. init.num_parents = 1;
  221. dev_clk->hw.init = &init;
  222. clk = clk_register(NULL, &dev_clk->hw);
  223. if (WARN_ON(IS_ERR(clk))) {
  224. kfree(dev_clk);
  225. return;
  226. }
  227. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  228. clk_register_clkdev(clk, clk_name, NULL);
  229. }
  230. /* PLL clock related functions */
  231. #define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
  232. /* Helper macros for PLL_VT8500 */
  233. #define VT8500_PLL_MUL(x) ((x & 0x1F) << 1)
  234. #define VT8500_PLL_DIV(x) ((x & 0x100) ? 1 : 2)
  235. #define VT8500_BITS_TO_FREQ(r, m, d) \
  236. ((r / d) * m)
  237. #define VT8500_BITS_TO_VAL(m, d) \
  238. ((d == 2 ? 0 : 0x100) | ((m >> 1) & 0x1F))
  239. /* Helper macros for PLL_WM8650 */
  240. #define WM8650_PLL_MUL(x) (x & 0x3FF)
  241. #define WM8650_PLL_DIV(x) (((x >> 10) & 7) * (1 << ((x >> 13) & 3)))
  242. #define WM8650_BITS_TO_FREQ(r, m, d1, d2) \
  243. (r * m / (d1 * (1 << d2)))
  244. #define WM8650_BITS_TO_VAL(m, d1, d2) \
  245. ((d2 << 13) | (d1 << 10) | (m & 0x3FF))
  246. static void vt8500_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  247. u32 *multiplier, u32 *prediv)
  248. {
  249. unsigned long tclk;
  250. /* sanity check */
  251. if ((rate < parent_rate * 4) || (rate > parent_rate * 62)) {
  252. pr_err("%s: requested rate out of range\n", __func__);
  253. *multiplier = 0;
  254. *prediv = 1;
  255. return;
  256. }
  257. if (rate <= parent_rate * 31)
  258. /* use the prediv to double the resolution */
  259. *prediv = 2;
  260. else
  261. *prediv = 1;
  262. *multiplier = rate / (parent_rate / *prediv);
  263. tclk = (parent_rate / *prediv) * *multiplier;
  264. if (tclk != rate)
  265. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__,
  266. rate, tclk);
  267. }
  268. static void wm8650_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  269. u32 *multiplier, u32 *divisor1, u32 *divisor2)
  270. {
  271. u32 mul, div1, div2;
  272. u32 best_mul, best_div1, best_div2;
  273. unsigned long tclk, rate_err, best_err;
  274. best_err = (unsigned long)-1;
  275. /* Find the closest match (lower or equal to requested) */
  276. for (div1 = 5; div1 >= 3; div1--)
  277. for (div2 = 3; div2 >= 0; div2--)
  278. for (mul = 3; mul <= 1023; mul++) {
  279. tclk = parent_rate * mul / (div1 * (1 << div2));
  280. if (tclk > rate)
  281. continue;
  282. /* error will always be +ve */
  283. rate_err = rate - tclk;
  284. if (rate_err == 0) {
  285. *multiplier = mul;
  286. *divisor1 = div1;
  287. *divisor2 = div2;
  288. return;
  289. }
  290. if (rate_err < best_err) {
  291. best_err = rate_err;
  292. best_mul = mul;
  293. best_div1 = div1;
  294. best_div2 = div2;
  295. }
  296. }
  297. /* if we got here, it wasn't an exact match */
  298. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
  299. rate - best_err);
  300. *multiplier = best_mul;
  301. *divisor1 = best_div1;
  302. *divisor2 = best_div2;
  303. }
  304. static int vtwm_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  305. unsigned long parent_rate)
  306. {
  307. struct clk_pll *pll = to_clk_pll(hw);
  308. u32 mul, div1, div2;
  309. u32 pll_val;
  310. unsigned long flags = 0;
  311. /* sanity check */
  312. switch (pll->type) {
  313. case PLL_TYPE_VT8500:
  314. vt8500_find_pll_bits(rate, parent_rate, &mul, &div1);
  315. pll_val = VT8500_BITS_TO_VAL(mul, div1);
  316. break;
  317. case PLL_TYPE_WM8650:
  318. wm8650_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
  319. pll_val = WM8650_BITS_TO_VAL(mul, div1, div2);
  320. break;
  321. default:
  322. pr_err("%s: invalid pll type\n", __func__);
  323. return 0;
  324. }
  325. spin_lock_irqsave(pll->lock, flags);
  326. vt8500_pmc_wait_busy();
  327. writel(pll_val, pll->reg);
  328. vt8500_pmc_wait_busy();
  329. spin_unlock_irqrestore(pll->lock, flags);
  330. return 0;
  331. }
  332. static long vtwm_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  333. unsigned long *prate)
  334. {
  335. struct clk_pll *pll = to_clk_pll(hw);
  336. u32 mul, div1, div2;
  337. long round_rate;
  338. switch (pll->type) {
  339. case PLL_TYPE_VT8500:
  340. vt8500_find_pll_bits(rate, *prate, &mul, &div1);
  341. round_rate = VT8500_BITS_TO_FREQ(*prate, mul, div1);
  342. break;
  343. case PLL_TYPE_WM8650:
  344. wm8650_find_pll_bits(rate, *prate, &mul, &div1, &div2);
  345. round_rate = WM8650_BITS_TO_FREQ(*prate, mul, div1, div2);
  346. break;
  347. default:
  348. round_rate = 0;
  349. }
  350. return round_rate;
  351. }
  352. static unsigned long vtwm_pll_recalc_rate(struct clk_hw *hw,
  353. unsigned long parent_rate)
  354. {
  355. struct clk_pll *pll = to_clk_pll(hw);
  356. u32 pll_val = readl(pll->reg);
  357. unsigned long pll_freq;
  358. switch (pll->type) {
  359. case PLL_TYPE_VT8500:
  360. pll_freq = parent_rate * VT8500_PLL_MUL(pll_val);
  361. pll_freq /= VT8500_PLL_DIV(pll_val);
  362. break;
  363. case PLL_TYPE_WM8650:
  364. pll_freq = parent_rate * WM8650_PLL_MUL(pll_val);
  365. pll_freq /= WM8650_PLL_DIV(pll_val);
  366. break;
  367. default:
  368. pll_freq = 0;
  369. }
  370. return pll_freq;
  371. }
  372. const struct clk_ops vtwm_pll_ops = {
  373. .round_rate = vtwm_pll_round_rate,
  374. .set_rate = vtwm_pll_set_rate,
  375. .recalc_rate = vtwm_pll_recalc_rate,
  376. };
  377. static __init void vtwm_pll_clk_init(struct device_node *node, int pll_type)
  378. {
  379. u32 reg;
  380. struct clk *clk;
  381. struct clk_pll *pll_clk;
  382. const char *clk_name = node->name;
  383. const char *parent_name;
  384. struct clk_init_data init;
  385. int rc;
  386. rc = of_property_read_u32(node, "reg", &reg);
  387. if (WARN_ON(rc))
  388. return;
  389. pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
  390. if (WARN_ON(!pll_clk))
  391. return;
  392. pll_clk->reg = pmc_base + reg;
  393. pll_clk->lock = &_lock;
  394. pll_clk->type = pll_type;
  395. of_property_read_string(node, "clock-output-names", &clk_name);
  396. init.name = clk_name;
  397. init.ops = &vtwm_pll_ops;
  398. init.flags = 0;
  399. parent_name = of_clk_get_parent_name(node, 0);
  400. init.parent_names = &parent_name;
  401. init.num_parents = 1;
  402. pll_clk->hw.init = &init;
  403. clk = clk_register(NULL, &pll_clk->hw);
  404. if (WARN_ON(IS_ERR(clk))) {
  405. kfree(pll_clk);
  406. return;
  407. }
  408. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  409. clk_register_clkdev(clk, clk_name, NULL);
  410. }
  411. /* Wrappers for initialization functions */
  412. static void __init vt8500_pll_init(struct device_node *node)
  413. {
  414. vtwm_pll_clk_init(node, PLL_TYPE_VT8500);
  415. }
  416. static void __init wm8650_pll_init(struct device_node *node)
  417. {
  418. vtwm_pll_clk_init(node, PLL_TYPE_WM8650);
  419. }
  420. static const __initconst struct of_device_id clk_match[] = {
  421. { .compatible = "fixed-clock", .data = of_fixed_clk_setup, },
  422. { .compatible = "via,vt8500-pll-clock", .data = vt8500_pll_init, },
  423. { .compatible = "wm,wm8650-pll-clock", .data = wm8650_pll_init, },
  424. { .compatible = "via,vt8500-device-clock",
  425. .data = vtwm_device_clk_init, },
  426. { /* sentinel */ }
  427. };
  428. void __init vtwm_clk_init(void __iomem *base)
  429. {
  430. if (!base)
  431. return;
  432. pmc_base = base;
  433. of_clk_init(clk_match);
  434. }