pll.c 8.9 KB

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