clk-vt8500.c 13 KB

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