clk-zynq.c 9.2 KB

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