clk-vt8500.c 13 KB

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