clk-vt8500.c 12 KB

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