pll.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * PLL clock driver for Keystone devices
  3. *
  4. * Copyright (C) 2013 Texas Instruments Inc.
  5. * Murali Karicheri <m-karicheri2@ti.com>
  6. * Santosh Shilimkar <santosh.shilimkar@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of.h>
  20. #include <linux/module.h>
  21. #define PLLM_LOW_MASK 0x3f
  22. #define PLLM_HIGH_MASK 0x7ffc0
  23. #define MAIN_PLLM_HIGH_MASK 0x7f000
  24. #define PLLM_HIGH_SHIFT 6
  25. #define PLLD_MASK 0x3f
  26. /**
  27. * struct clk_pll_data - pll data structure
  28. * @has_pllctrl: If set to non zero, lower 6 bits of multiplier is in pllm
  29. * register of pll controller, else it is in the pll_ctrl0((bit 11-6)
  30. * @phy_pllm: Physical address of PLLM in pll controller. Used when
  31. * has_pllctrl is non zero.
  32. * @phy_pll_ctl0: Physical address of PLL ctrl0. This could be that of
  33. * Main PLL or any other PLLs in the device such as ARM PLL, DDR PLL
  34. * or PA PLL available on keystone2. These PLLs are controlled by
  35. * this register. Main PLL is controlled by a PLL controller.
  36. * @pllm: PLL register map address
  37. * @pll_ctl0: PLL controller map address
  38. * @pllm_lower_mask: multiplier lower mask
  39. * @pllm_upper_mask: multiplier upper mask
  40. * @pllm_upper_shift: multiplier upper shift
  41. * @plld_mask: divider mask
  42. * @postdiv: Post divider
  43. */
  44. struct clk_pll_data {
  45. bool has_pllctrl;
  46. u32 phy_pllm;
  47. u32 phy_pll_ctl0;
  48. void __iomem *pllm;
  49. void __iomem *pll_ctl0;
  50. u32 pllm_lower_mask;
  51. u32 pllm_upper_mask;
  52. u32 pllm_upper_shift;
  53. u32 plld_mask;
  54. u32 postdiv;
  55. };
  56. /**
  57. * struct clk_pll - Main pll clock
  58. * @hw: clk_hw for the pll
  59. * @pll_data: PLL driver specific data
  60. */
  61. struct clk_pll {
  62. struct clk_hw hw;
  63. struct clk_pll_data *pll_data;
  64. };
  65. #define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
  66. static unsigned long clk_pllclk_recalc(struct clk_hw *hw,
  67. unsigned long parent_rate)
  68. {
  69. struct clk_pll *pll = to_clk_pll(hw);
  70. struct clk_pll_data *pll_data = pll->pll_data;
  71. unsigned long rate = parent_rate;
  72. u32 mult = 0, prediv, postdiv, val;
  73. /*
  74. * get bits 0-5 of multiplier from pllctrl PLLM register
  75. * if has_pllctrl is non zero
  76. */
  77. if (pll_data->has_pllctrl) {
  78. val = readl(pll_data->pllm);
  79. mult = (val & pll_data->pllm_lower_mask);
  80. }
  81. /* bit6-12 of PLLM is in Main PLL control register */
  82. val = readl(pll_data->pll_ctl0);
  83. mult |= ((val & pll_data->pllm_upper_mask)
  84. >> pll_data->pllm_upper_shift);
  85. prediv = (val & pll_data->plld_mask);
  86. postdiv = pll_data->postdiv;
  87. rate /= (prediv + 1);
  88. rate = (rate * (mult + 1));
  89. rate /= postdiv;
  90. return rate;
  91. }
  92. static const struct clk_ops clk_pll_ops = {
  93. .recalc_rate = clk_pllclk_recalc,
  94. };
  95. static struct clk *clk_register_pll(struct device *dev,
  96. const char *name,
  97. const char *parent_name,
  98. struct clk_pll_data *pll_data)
  99. {
  100. struct clk_init_data init;
  101. struct clk_pll *pll;
  102. struct clk *clk;
  103. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  104. if (!pll)
  105. return ERR_PTR(-ENOMEM);
  106. init.name = name;
  107. init.ops = &clk_pll_ops;
  108. init.flags = 0;
  109. init.parent_names = (parent_name ? &parent_name : NULL);
  110. init.num_parents = (parent_name ? 1 : 0);
  111. pll->pll_data = pll_data;
  112. pll->hw.init = &init;
  113. clk = clk_register(NULL, &pll->hw);
  114. if (IS_ERR(clk))
  115. goto out;
  116. return clk;
  117. out:
  118. kfree(pll);
  119. return NULL;
  120. }
  121. /**
  122. * _of_clk_init - PLL initialisation via DT
  123. * @node: device tree node for this clock
  124. * @pllctrl: If true, lower 6 bits of multiplier is in pllm register of
  125. * pll controller, else it is in the control regsiter0(bit 11-6)
  126. */
  127. static void __init _of_pll_clk_init(struct device_node *node, bool pllctrl)
  128. {
  129. struct clk_pll_data *pll_data;
  130. const char *parent_name;
  131. struct clk *clk;
  132. int i;
  133. pll_data = kzalloc(sizeof(*pll_data), GFP_KERNEL);
  134. if (!pll_data) {
  135. pr_err("%s: Out of memory\n", __func__);
  136. return;
  137. }
  138. parent_name = of_clk_get_parent_name(node, 0);
  139. if (of_property_read_u32(node, "fixed-postdiv", &pll_data->postdiv))
  140. goto out;
  141. i = of_property_match_string(node, "reg-names", "control");
  142. pll_data->pll_ctl0 = of_iomap(node, i);
  143. if (!pll_data->pll_ctl0) {
  144. pr_err("%s: ioremap failed\n", __func__);
  145. goto out;
  146. }
  147. pll_data->pllm_lower_mask = PLLM_LOW_MASK;
  148. pll_data->pllm_upper_shift = PLLM_HIGH_SHIFT;
  149. pll_data->plld_mask = PLLD_MASK;
  150. pll_data->has_pllctrl = pllctrl;
  151. if (!pll_data->has_pllctrl) {
  152. pll_data->pllm_upper_mask = PLLM_HIGH_MASK;
  153. } else {
  154. pll_data->pllm_upper_mask = MAIN_PLLM_HIGH_MASK;
  155. i = of_property_match_string(node, "reg-names", "multiplier");
  156. pll_data->pllm = of_iomap(node, i);
  157. if (!pll_data->pllm) {
  158. iounmap(pll_data->pll_ctl0);
  159. goto out;
  160. }
  161. }
  162. clk = clk_register_pll(NULL, node->name, parent_name, pll_data);
  163. if (clk) {
  164. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  165. return;
  166. }
  167. out:
  168. pr_err("%s: error initializing pll %s\n", __func__, node->name);
  169. kfree(pll_data);
  170. }
  171. /**
  172. * of_keystone_pll_clk_init - PLL initialisation DT wrapper
  173. * @node: device tree node for this clock
  174. */
  175. static void __init of_keystone_pll_clk_init(struct device_node *node)
  176. {
  177. _of_pll_clk_init(node, false);
  178. }
  179. CLK_OF_DECLARE(keystone_pll_clock, "ti,keystone,pll-clock",
  180. of_keystone_pll_clk_init);
  181. /**
  182. * of_keystone_pll_main_clk_init - Main PLL initialisation DT wrapper
  183. * @node: device tree node for this clock
  184. */
  185. static void __init of_keystone_main_pll_clk_init(struct device_node *node)
  186. {
  187. _of_pll_clk_init(node, true);
  188. }
  189. CLK_OF_DECLARE(keystone_main_pll_clock, "ti,keystone,main-pll-clock",
  190. of_keystone_main_pll_clk_init);
  191. /**
  192. * of_pll_div_clk_init - PLL divider setup function
  193. * @node: device tree node for this clock
  194. */
  195. static void __init of_pll_div_clk_init(struct device_node *node)
  196. {
  197. const char *parent_name;
  198. void __iomem *reg;
  199. u32 shift, mask;
  200. struct clk *clk;
  201. const char *clk_name = node->name;
  202. of_property_read_string(node, "clock-output-names", &clk_name);
  203. reg = of_iomap(node, 0);
  204. if (!reg) {
  205. pr_err("%s: ioremap failed\n", __func__);
  206. return;
  207. }
  208. parent_name = of_clk_get_parent_name(node, 0);
  209. if (!parent_name) {
  210. pr_err("%s: missing parent clock\n", __func__);
  211. return;
  212. }
  213. if (of_property_read_u32(node, "bit-shift", &shift)) {
  214. pr_err("%s: missing 'shift' property\n", __func__);
  215. return;
  216. }
  217. if (of_property_read_u32(node, "bit-mask", &mask)) {
  218. pr_err("%s: missing 'bit-mask' property\n", __func__);
  219. return;
  220. }
  221. clk = clk_register_divider(NULL, clk_name, parent_name, 0, reg, shift,
  222. mask, 0, NULL);
  223. if (clk)
  224. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  225. else
  226. pr_err("%s: error registering divider %s\n", __func__, clk_name);
  227. }
  228. CLK_OF_DECLARE(pll_divider_clock, "ti,keystone,pll-divider-clock", of_pll_div_clk_init);
  229. /**
  230. * of_pll_mux_clk_init - PLL mux setup function
  231. * @node: device tree node for this clock
  232. */
  233. static void __init of_pll_mux_clk_init(struct device_node *node)
  234. {
  235. void __iomem *reg;
  236. u32 shift, mask;
  237. struct clk *clk;
  238. const char *parents[2];
  239. const char *clk_name = node->name;
  240. of_property_read_string(node, "clock-output-names", &clk_name);
  241. reg = of_iomap(node, 0);
  242. if (!reg) {
  243. pr_err("%s: ioremap failed\n", __func__);
  244. return;
  245. }
  246. parents[0] = of_clk_get_parent_name(node, 0);
  247. parents[1] = of_clk_get_parent_name(node, 1);
  248. if (!parents[0] || !parents[1]) {
  249. pr_err("%s: missing parent clocks\n", __func__);
  250. return;
  251. }
  252. if (of_property_read_u32(node, "bit-shift", &shift)) {
  253. pr_err("%s: missing 'shift' property\n", __func__);
  254. return;
  255. }
  256. if (of_property_read_u32(node, "bit-mask", &mask)) {
  257. pr_err("%s: missing 'bit-mask' property\n", __func__);
  258. return;
  259. }
  260. clk = clk_register_mux(NULL, clk_name, (const char **)&parents,
  261. ARRAY_SIZE(parents) , 0, reg, shift, mask,
  262. 0, NULL);
  263. if (clk)
  264. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  265. else
  266. pr_err("%s: error registering mux %s\n", __func__, clk_name);
  267. }
  268. CLK_OF_DECLARE(pll_mux_clock, "ti,keystone,pll-mux-clock", of_pll_mux_clk_init);