clk-zynq.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright (c) 2012 National Instruments
  3. *
  4. * Josh Cartwright <josh.cartwright@ni.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/slab.h>
  21. #include <linux/kernel.h>
  22. #include <linux/clk-provider.h>
  23. static void __iomem *slcr_base;
  24. struct zynq_pll_clk {
  25. struct clk_hw hw;
  26. void __iomem *pll_ctrl;
  27. void __iomem *pll_cfg;
  28. };
  29. #define to_zynq_pll_clk(hw) container_of(hw, struct zynq_pll_clk, hw)
  30. #define CTRL_PLL_FDIV(x) ((x) >> 12)
  31. static unsigned long zynq_pll_recalc_rate(struct clk_hw *hw,
  32. unsigned long parent_rate)
  33. {
  34. struct zynq_pll_clk *pll = to_zynq_pll_clk(hw);
  35. return parent_rate * CTRL_PLL_FDIV(ioread32(pll->pll_ctrl));
  36. }
  37. static const struct clk_ops zynq_pll_clk_ops = {
  38. .recalc_rate = zynq_pll_recalc_rate,
  39. };
  40. static void __init zynq_pll_clk_setup(struct device_node *np)
  41. {
  42. struct clk_init_data init;
  43. struct zynq_pll_clk *pll;
  44. const char *parent_name;
  45. struct clk *clk;
  46. u32 regs[2];
  47. int ret;
  48. ret = of_property_read_u32_array(np, "reg", regs, ARRAY_SIZE(regs));
  49. if (WARN_ON(ret))
  50. return;
  51. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  52. if (WARN_ON(!pll))
  53. return;
  54. pll->pll_ctrl = slcr_base + regs[0];
  55. pll->pll_cfg = slcr_base + regs[1];
  56. of_property_read_string(np, "clock-output-names", &init.name);
  57. init.ops = &zynq_pll_clk_ops;
  58. parent_name = of_clk_get_parent_name(np, 0);
  59. init.parent_names = &parent_name;
  60. init.num_parents = 1;
  61. pll->hw.init = &init;
  62. clk = clk_register(NULL, &pll->hw);
  63. if (WARN_ON(IS_ERR(clk)))
  64. return;
  65. ret = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  66. if (WARN_ON(ret))
  67. return;
  68. }
  69. CLK_OF_DECLARE(zynq_pll, "xlnx,zynq-pll", zynq_pll_clk_setup);
  70. struct zynq_periph_clk {
  71. struct clk_hw hw;
  72. struct clk_onecell_data onecell_data;
  73. struct clk *gates[2];
  74. void __iomem *clk_ctrl;
  75. spinlock_t clkact_lock;
  76. };
  77. #define to_zynq_periph_clk(hw) container_of(hw, struct zynq_periph_clk, hw)
  78. static const u8 periph_clk_parent_map[] = {
  79. 0, 0, 1, 2
  80. };
  81. #define PERIPH_CLK_CTRL_SRC(x) (periph_clk_parent_map[((x) & 0x30) >> 4])
  82. #define PERIPH_CLK_CTRL_DIV(x) (((x) & 0x3F00) >> 8)
  83. static unsigned long zynq_periph_recalc_rate(struct clk_hw *hw,
  84. unsigned long parent_rate)
  85. {
  86. struct zynq_periph_clk *periph = to_zynq_periph_clk(hw);
  87. return parent_rate / PERIPH_CLK_CTRL_DIV(ioread32(periph->clk_ctrl));
  88. }
  89. static u8 zynq_periph_get_parent(struct clk_hw *hw)
  90. {
  91. struct zynq_periph_clk *periph = to_zynq_periph_clk(hw);
  92. return PERIPH_CLK_CTRL_SRC(ioread32(periph->clk_ctrl));
  93. }
  94. static const struct clk_ops zynq_periph_clk_ops = {
  95. .recalc_rate = zynq_periph_recalc_rate,
  96. .get_parent = zynq_periph_get_parent,
  97. };
  98. static void __init zynq_periph_clk_setup(struct device_node *np)
  99. {
  100. struct zynq_periph_clk *periph;
  101. const char *parent_names[3];
  102. struct clk_init_data init;
  103. int clk_num = 0, err;
  104. const char *name;
  105. struct clk *clk;
  106. u32 reg;
  107. int i;
  108. err = of_property_read_u32(np, "reg", &reg);
  109. if (WARN_ON(err))
  110. return;
  111. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  112. if (WARN_ON(!periph))
  113. return;
  114. periph->clk_ctrl = slcr_base + reg;
  115. spin_lock_init(&periph->clkact_lock);
  116. init.name = np->name;
  117. init.ops = &zynq_periph_clk_ops;
  118. for (i = 0; i < ARRAY_SIZE(parent_names); i++)
  119. parent_names[i] = of_clk_get_parent_name(np, i);
  120. init.parent_names = parent_names;
  121. init.num_parents = ARRAY_SIZE(parent_names);
  122. periph->hw.init = &init;
  123. clk = clk_register(NULL, &periph->hw);
  124. if (WARN_ON(IS_ERR(clk)))
  125. return;
  126. err = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  127. if (WARN_ON(err))
  128. return;
  129. err = of_property_read_string_index(np, "clock-output-names", 0,
  130. &name);
  131. if (WARN_ON(err))
  132. return;
  133. periph->gates[0] = clk_register_gate(NULL, name, np->name, 0,
  134. periph->clk_ctrl, 0, 0,
  135. &periph->clkact_lock);
  136. if (WARN_ON(IS_ERR(periph->gates[0])))
  137. return;
  138. clk_num++;
  139. /* some periph clks have 2 downstream gates */
  140. err = of_property_read_string_index(np, "clock-output-names", 1,
  141. &name);
  142. if (err != -ENODATA) {
  143. periph->gates[1] = clk_register_gate(NULL, name, np->name, 0,
  144. periph->clk_ctrl, 1, 0,
  145. &periph->clkact_lock);
  146. if (WARN_ON(IS_ERR(periph->gates[1])))
  147. return;
  148. clk_num++;
  149. }
  150. periph->onecell_data.clks = periph->gates;
  151. periph->onecell_data.clk_num = clk_num;
  152. err = of_clk_add_provider(np, of_clk_src_onecell_get,
  153. &periph->onecell_data);
  154. if (WARN_ON(err))
  155. return;
  156. }
  157. CLK_OF_DECLARE(zynq_periph, "xlnx,zynq-periph-clock", zynq_periph_clk_setup);
  158. /* CPU Clock domain is modelled as a mux with 4 children subclks, whose
  159. * derivative rates depend on CLK_621_TRUE
  160. */
  161. struct zynq_cpu_clk {
  162. struct clk_hw hw;
  163. struct clk_onecell_data onecell_data;
  164. struct clk *subclks[4];
  165. void __iomem *clk_ctrl;
  166. spinlock_t clkact_lock;
  167. };
  168. #define to_zynq_cpu_clk(hw) container_of(hw, struct zynq_cpu_clk, hw)
  169. static const u8 zynq_cpu_clk_parent_map[] = {
  170. 1, 1, 2, 0
  171. };
  172. #define CPU_CLK_SRCSEL(x) (zynq_cpu_clk_parent_map[(((x) & 0x30) >> 4)])
  173. #define CPU_CLK_CTRL_DIV(x) (((x) & 0x3F00) >> 8)
  174. static u8 zynq_cpu_clk_get_parent(struct clk_hw *hw)
  175. {
  176. struct zynq_cpu_clk *cpuclk = to_zynq_cpu_clk(hw);
  177. return CPU_CLK_SRCSEL(ioread32(cpuclk->clk_ctrl));
  178. }
  179. static unsigned long zynq_cpu_clk_recalc_rate(struct clk_hw *hw,
  180. unsigned long parent_rate)
  181. {
  182. struct zynq_cpu_clk *cpuclk = to_zynq_cpu_clk(hw);
  183. return parent_rate / CPU_CLK_CTRL_DIV(ioread32(cpuclk->clk_ctrl));
  184. }
  185. static const struct clk_ops zynq_cpu_clk_ops = {
  186. .get_parent = zynq_cpu_clk_get_parent,
  187. .recalc_rate = zynq_cpu_clk_recalc_rate,
  188. };
  189. struct zynq_cpu_subclk {
  190. struct clk_hw hw;
  191. void __iomem *clk_621;
  192. enum {
  193. CPU_SUBCLK_6X4X,
  194. CPU_SUBCLK_3X2X,
  195. CPU_SUBCLK_2X,
  196. CPU_SUBCLK_1X,
  197. } which;
  198. };
  199. #define CLK_621_TRUE(x) ((x) & 1)
  200. #define to_zynq_cpu_subclk(hw) container_of(hw, struct zynq_cpu_subclk, hw);
  201. static unsigned long zynq_cpu_subclk_recalc_rate(struct clk_hw *hw,
  202. unsigned long parent_rate)
  203. {
  204. unsigned long uninitialized_var(rate);
  205. struct zynq_cpu_subclk *subclk;
  206. bool is_621;
  207. subclk = to_zynq_cpu_subclk(hw)
  208. is_621 = CLK_621_TRUE(ioread32(subclk->clk_621));
  209. switch (subclk->which) {
  210. case CPU_SUBCLK_6X4X:
  211. rate = parent_rate;
  212. break;
  213. case CPU_SUBCLK_3X2X:
  214. rate = parent_rate / 2;
  215. break;
  216. case CPU_SUBCLK_2X:
  217. rate = parent_rate / (is_621 ? 3 : 2);
  218. break;
  219. case CPU_SUBCLK_1X:
  220. rate = parent_rate / (is_621 ? 6 : 4);
  221. break;
  222. };
  223. return rate;
  224. }
  225. static const struct clk_ops zynq_cpu_subclk_ops = {
  226. .recalc_rate = zynq_cpu_subclk_recalc_rate,
  227. };
  228. static struct clk *zynq_cpu_subclk_setup(struct device_node *np, u8 which,
  229. void __iomem *clk_621)
  230. {
  231. struct zynq_cpu_subclk *subclk;
  232. struct clk_init_data init;
  233. struct clk *clk;
  234. int err;
  235. err = of_property_read_string_index(np, "clock-output-names",
  236. which, &init.name);
  237. if (WARN_ON(err))
  238. goto err_read_output_name;
  239. subclk = kzalloc(sizeof(*subclk), GFP_KERNEL);
  240. if (!subclk)
  241. goto err_subclk_alloc;
  242. subclk->clk_621 = clk_621;
  243. subclk->which = which;
  244. init.ops = &zynq_cpu_subclk_ops;
  245. init.parent_names = &np->name;
  246. init.num_parents = 1;
  247. subclk->hw.init = &init;
  248. clk = clk_register(NULL, &subclk->hw);
  249. if (WARN_ON(IS_ERR(clk)))
  250. goto err_clk_register;
  251. return clk;
  252. err_clk_register:
  253. kfree(subclk);
  254. err_subclk_alloc:
  255. err_read_output_name:
  256. return ERR_PTR(-EINVAL);
  257. }
  258. static void __init zynq_cpu_clk_setup(struct device_node *np)
  259. {
  260. struct zynq_cpu_clk *cpuclk;
  261. const char *parent_names[3];
  262. struct clk_init_data init;
  263. void __iomem *clk_621;
  264. struct clk *clk;
  265. u32 reg[2];
  266. int err;
  267. int i;
  268. err = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg));
  269. if (WARN_ON(err))
  270. return;
  271. cpuclk = kzalloc(sizeof(*cpuclk), GFP_KERNEL);
  272. if (WARN_ON(!cpuclk))
  273. return;
  274. cpuclk->clk_ctrl = slcr_base + reg[0];
  275. clk_621 = slcr_base + reg[1];
  276. spin_lock_init(&cpuclk->clkact_lock);
  277. init.name = np->name;
  278. init.ops = &zynq_cpu_clk_ops;
  279. for (i = 0; i < ARRAY_SIZE(parent_names); i++)
  280. parent_names[i] = of_clk_get_parent_name(np, i);
  281. init.parent_names = parent_names;
  282. init.num_parents = ARRAY_SIZE(parent_names);
  283. cpuclk->hw.init = &init;
  284. clk = clk_register(NULL, &cpuclk->hw);
  285. if (WARN_ON(IS_ERR(clk)))
  286. return;
  287. err = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  288. if (WARN_ON(err))
  289. return;
  290. for (i = 0; i < 4; i++) {
  291. cpuclk->subclks[i] = zynq_cpu_subclk_setup(np, i, clk_621);
  292. if (WARN_ON(IS_ERR(cpuclk->subclks[i])))
  293. return;
  294. }
  295. cpuclk->onecell_data.clks = cpuclk->subclks;
  296. cpuclk->onecell_data.clk_num = i;
  297. err = of_clk_add_provider(np, of_clk_src_onecell_get,
  298. &cpuclk->onecell_data);
  299. if (WARN_ON(err))
  300. return;
  301. }
  302. CLK_OF_DECLARE(zynq_cpu, "xlnx,zynq-cpu-clock", zynq_cpu_clk_setup);
  303. void __init xilinx_zynq_clocks_init(void __iomem *slcr)
  304. {
  305. slcr_base = slcr;
  306. of_clk_init(NULL);
  307. }