pll.c 9.0 KB

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