pll.c 8.4 KB

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