clkc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Zynq clock controller
  3. *
  4. * Copyright (C) 2012 - 2013 Xilinx
  5. *
  6. * Sören Brinkmann <soren.brinkmann@xilinx.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 v2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/clk/zynq.h>
  21. #include <linux/clk.h>
  22. #include <linux/clk-provider.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/slab.h>
  26. #include <linux/string.h>
  27. #include <linux/io.h>
  28. static void __iomem *zynq_clkc_base;
  29. #define SLCR_ARMPLL_CTRL (zynq_clkc_base + 0x00)
  30. #define SLCR_DDRPLL_CTRL (zynq_clkc_base + 0x04)
  31. #define SLCR_IOPLL_CTRL (zynq_clkc_base + 0x08)
  32. #define SLCR_PLL_STATUS (zynq_clkc_base + 0x0c)
  33. #define SLCR_ARM_CLK_CTRL (zynq_clkc_base + 0x20)
  34. #define SLCR_DDR_CLK_CTRL (zynq_clkc_base + 0x24)
  35. #define SLCR_DCI_CLK_CTRL (zynq_clkc_base + 0x28)
  36. #define SLCR_APER_CLK_CTRL (zynq_clkc_base + 0x2c)
  37. #define SLCR_GEM0_CLK_CTRL (zynq_clkc_base + 0x40)
  38. #define SLCR_GEM1_CLK_CTRL (zynq_clkc_base + 0x44)
  39. #define SLCR_SMC_CLK_CTRL (zynq_clkc_base + 0x48)
  40. #define SLCR_LQSPI_CLK_CTRL (zynq_clkc_base + 0x4c)
  41. #define SLCR_SDIO_CLK_CTRL (zynq_clkc_base + 0x50)
  42. #define SLCR_UART_CLK_CTRL (zynq_clkc_base + 0x54)
  43. #define SLCR_SPI_CLK_CTRL (zynq_clkc_base + 0x58)
  44. #define SLCR_CAN_CLK_CTRL (zynq_clkc_base + 0x5c)
  45. #define SLCR_CAN_MIOCLK_CTRL (zynq_clkc_base + 0x60)
  46. #define SLCR_DBG_CLK_CTRL (zynq_clkc_base + 0x64)
  47. #define SLCR_PCAP_CLK_CTRL (zynq_clkc_base + 0x68)
  48. #define SLCR_FPGA0_CLK_CTRL (zynq_clkc_base + 0x70)
  49. #define SLCR_621_TRUE (zynq_clkc_base + 0xc4)
  50. #define SLCR_SWDT_CLK_SEL (zynq_clkc_base + 0x204)
  51. #define NUM_MIO_PINS 54
  52. #define DBG_CLK_CTRL_CLKACT_TRC BIT(0)
  53. #define DBG_CLK_CTRL_CPU_1XCLKACT BIT(1)
  54. enum zynq_clk {
  55. armpll, ddrpll, iopll,
  56. cpu_6or4x, cpu_3or2x, cpu_2x, cpu_1x,
  57. ddr2x, ddr3x, dci,
  58. lqspi, smc, pcap, gem0, gem1, fclk0, fclk1, fclk2, fclk3, can0, can1,
  59. sdio0, sdio1, uart0, uart1, spi0, spi1, dma,
  60. usb0_aper, usb1_aper, gem0_aper, gem1_aper,
  61. sdio0_aper, sdio1_aper, spi0_aper, spi1_aper, can0_aper, can1_aper,
  62. i2c0_aper, i2c1_aper, uart0_aper, uart1_aper, gpio_aper, lqspi_aper,
  63. smc_aper, swdt, dbg_trc, dbg_apb, clk_max};
  64. static struct clk *ps_clk;
  65. static struct clk *clks[clk_max];
  66. static struct clk_onecell_data clk_data;
  67. static DEFINE_SPINLOCK(armpll_lock);
  68. static DEFINE_SPINLOCK(ddrpll_lock);
  69. static DEFINE_SPINLOCK(iopll_lock);
  70. static DEFINE_SPINLOCK(armclk_lock);
  71. static DEFINE_SPINLOCK(swdtclk_lock);
  72. static DEFINE_SPINLOCK(ddrclk_lock);
  73. static DEFINE_SPINLOCK(dciclk_lock);
  74. static DEFINE_SPINLOCK(gem0clk_lock);
  75. static DEFINE_SPINLOCK(gem1clk_lock);
  76. static DEFINE_SPINLOCK(canclk_lock);
  77. static DEFINE_SPINLOCK(canmioclk_lock);
  78. static DEFINE_SPINLOCK(dbgclk_lock);
  79. static DEFINE_SPINLOCK(aperclk_lock);
  80. static const char *const armpll_parents[] __initconst = {"armpll_int",
  81. "ps_clk"};
  82. static const char *const ddrpll_parents[] __initconst = {"ddrpll_int",
  83. "ps_clk"};
  84. static const char *const iopll_parents[] __initconst = {"iopll_int",
  85. "ps_clk"};
  86. static const char *gem0_mux_parents[] __initdata = {"gem0_div1", "dummy_name"};
  87. static const char *gem1_mux_parents[] __initdata = {"gem1_div1", "dummy_name"};
  88. static const char *const can0_mio_mux2_parents[] __initconst = {"can0_gate",
  89. "can0_mio_mux"};
  90. static const char *const can1_mio_mux2_parents[] __initconst = {"can1_gate",
  91. "can1_mio_mux"};
  92. static const char *dbg_emio_mux_parents[] __initdata = {"dbg_div",
  93. "dummy_name"};
  94. static const char *const dbgtrc_emio_input_names[] __initconst = {
  95. "trace_emio_clk"};
  96. static const char *const gem0_emio_input_names[] __initconst = {
  97. "gem0_emio_clk"};
  98. static const char *const gem1_emio_input_names[] __initconst = {
  99. "gem1_emio_clk"};
  100. static const char *const swdt_ext_clk_input_names[] __initconst = {
  101. "swdt_ext_clk"};
  102. static void __init zynq_clk_register_fclk(enum zynq_clk fclk,
  103. const char *clk_name, void __iomem *fclk_ctrl_reg,
  104. const char **parents, int enable)
  105. {
  106. struct clk *clk;
  107. u32 enable_reg;
  108. char *mux_name;
  109. char *div0_name;
  110. char *div1_name;
  111. spinlock_t *fclk_lock;
  112. spinlock_t *fclk_gate_lock;
  113. void __iomem *fclk_gate_reg = fclk_ctrl_reg + 8;
  114. fclk_lock = kmalloc(sizeof(*fclk_lock), GFP_KERNEL);
  115. if (!fclk_lock)
  116. goto err;
  117. fclk_gate_lock = kmalloc(sizeof(*fclk_gate_lock), GFP_KERNEL);
  118. if (!fclk_gate_lock)
  119. goto err_fclk_gate_lock;
  120. spin_lock_init(fclk_lock);
  121. spin_lock_init(fclk_gate_lock);
  122. mux_name = kasprintf(GFP_KERNEL, "%s_mux", clk_name);
  123. if (!mux_name)
  124. goto err_mux_name;
  125. div0_name = kasprintf(GFP_KERNEL, "%s_div0", clk_name);
  126. if (!div0_name)
  127. goto err_div0_name;
  128. div1_name = kasprintf(GFP_KERNEL, "%s_div1", clk_name);
  129. if (!div1_name)
  130. goto err_div1_name;
  131. clk = clk_register_mux(NULL, mux_name, parents, 4,
  132. CLK_SET_RATE_NO_REPARENT, fclk_ctrl_reg, 4, 2, 0,
  133. fclk_lock);
  134. clk = clk_register_divider(NULL, div0_name, mux_name,
  135. 0, fclk_ctrl_reg, 8, 6, CLK_DIVIDER_ONE_BASED |
  136. CLK_DIVIDER_ALLOW_ZERO, fclk_lock);
  137. clk = clk_register_divider(NULL, div1_name, div0_name,
  138. CLK_SET_RATE_PARENT, fclk_ctrl_reg, 20, 6,
  139. CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
  140. fclk_lock);
  141. clks[fclk] = clk_register_gate(NULL, clk_name,
  142. div1_name, CLK_SET_RATE_PARENT, fclk_gate_reg,
  143. 0, CLK_GATE_SET_TO_DISABLE, fclk_gate_lock);
  144. enable_reg = clk_readl(fclk_gate_reg) & 1;
  145. if (enable && !enable_reg) {
  146. if (clk_prepare_enable(clks[fclk]))
  147. pr_warn("%s: FCLK%u enable failed\n", __func__,
  148. fclk - fclk0);
  149. }
  150. kfree(mux_name);
  151. kfree(div0_name);
  152. kfree(div1_name);
  153. return;
  154. err_div1_name:
  155. kfree(div0_name);
  156. err_div0_name:
  157. kfree(mux_name);
  158. err_mux_name:
  159. kfree(fclk_gate_lock);
  160. err_fclk_gate_lock:
  161. kfree(fclk_lock);
  162. err:
  163. clks[fclk] = ERR_PTR(-ENOMEM);
  164. }
  165. static void __init zynq_clk_register_periph_clk(enum zynq_clk clk0,
  166. enum zynq_clk clk1, const char *clk_name0,
  167. const char *clk_name1, void __iomem *clk_ctrl,
  168. const char **parents, unsigned int two_gates)
  169. {
  170. struct clk *clk;
  171. char *mux_name;
  172. char *div_name;
  173. spinlock_t *lock;
  174. lock = kmalloc(sizeof(*lock), GFP_KERNEL);
  175. if (!lock)
  176. goto err;
  177. spin_lock_init(lock);
  178. mux_name = kasprintf(GFP_KERNEL, "%s_mux", clk_name0);
  179. div_name = kasprintf(GFP_KERNEL, "%s_div", clk_name0);
  180. clk = clk_register_mux(NULL, mux_name, parents, 4,
  181. CLK_SET_RATE_NO_REPARENT, clk_ctrl, 4, 2, 0, lock);
  182. clk = clk_register_divider(NULL, div_name, mux_name, 0, clk_ctrl, 8, 6,
  183. CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO, lock);
  184. clks[clk0] = clk_register_gate(NULL, clk_name0, div_name,
  185. CLK_SET_RATE_PARENT, clk_ctrl, 0, 0, lock);
  186. if (two_gates)
  187. clks[clk1] = clk_register_gate(NULL, clk_name1, div_name,
  188. CLK_SET_RATE_PARENT, clk_ctrl, 1, 0, lock);
  189. kfree(mux_name);
  190. kfree(div_name);
  191. return;
  192. err:
  193. clks[clk0] = ERR_PTR(-ENOMEM);
  194. if (two_gates)
  195. clks[clk1] = ERR_PTR(-ENOMEM);
  196. }
  197. static void __init zynq_clk_setup(struct device_node *np)
  198. {
  199. int i;
  200. u32 tmp;
  201. int ret;
  202. struct clk *clk;
  203. char *clk_name;
  204. unsigned int fclk_enable = 0;
  205. const char *clk_output_name[clk_max];
  206. const char *cpu_parents[4];
  207. const char *periph_parents[4];
  208. const char *swdt_ext_clk_mux_parents[2];
  209. const char *can_mio_mux_parents[NUM_MIO_PINS];
  210. const char *dummy_nm = "dummy_name";
  211. pr_info("Zynq clock init\n");
  212. /* get clock output names from DT */
  213. for (i = 0; i < clk_max; i++) {
  214. if (of_property_read_string_index(np, "clock-output-names",
  215. i, &clk_output_name[i])) {
  216. pr_err("%s: clock output name not in DT\n", __func__);
  217. BUG();
  218. }
  219. }
  220. cpu_parents[0] = clk_output_name[armpll];
  221. cpu_parents[1] = clk_output_name[armpll];
  222. cpu_parents[2] = clk_output_name[ddrpll];
  223. cpu_parents[3] = clk_output_name[iopll];
  224. periph_parents[0] = clk_output_name[iopll];
  225. periph_parents[1] = clk_output_name[iopll];
  226. periph_parents[2] = clk_output_name[armpll];
  227. periph_parents[3] = clk_output_name[ddrpll];
  228. of_property_read_u32(np, "fclk-enable", &fclk_enable);
  229. /* ps_clk */
  230. ret = of_property_read_u32(np, "ps-clk-frequency", &tmp);
  231. if (ret) {
  232. pr_warn("ps_clk frequency not specified, using 33 MHz.\n");
  233. tmp = 33333333;
  234. }
  235. ps_clk = clk_register_fixed_rate(NULL, "ps_clk", NULL, 0, tmp);
  236. /* PLLs */
  237. clk = clk_register_zynq_pll("armpll_int", "ps_clk", SLCR_ARMPLL_CTRL,
  238. SLCR_PLL_STATUS, 0, &armpll_lock);
  239. clks[armpll] = clk_register_mux(NULL, clk_output_name[armpll],
  240. armpll_parents, 2, CLK_SET_RATE_NO_REPARENT,
  241. SLCR_ARMPLL_CTRL, 4, 1, 0, &armpll_lock);
  242. clk = clk_register_zynq_pll("ddrpll_int", "ps_clk", SLCR_DDRPLL_CTRL,
  243. SLCR_PLL_STATUS, 1, &ddrpll_lock);
  244. clks[ddrpll] = clk_register_mux(NULL, clk_output_name[ddrpll],
  245. ddrpll_parents, 2, CLK_SET_RATE_NO_REPARENT,
  246. SLCR_DDRPLL_CTRL, 4, 1, 0, &ddrpll_lock);
  247. clk = clk_register_zynq_pll("iopll_int", "ps_clk", SLCR_IOPLL_CTRL,
  248. SLCR_PLL_STATUS, 2, &iopll_lock);
  249. clks[iopll] = clk_register_mux(NULL, clk_output_name[iopll],
  250. iopll_parents, 2, CLK_SET_RATE_NO_REPARENT,
  251. SLCR_IOPLL_CTRL, 4, 1, 0, &iopll_lock);
  252. /* CPU clocks */
  253. tmp = clk_readl(SLCR_621_TRUE) & 1;
  254. clk = clk_register_mux(NULL, "cpu_mux", cpu_parents, 4,
  255. CLK_SET_RATE_NO_REPARENT, SLCR_ARM_CLK_CTRL, 4, 2, 0,
  256. &armclk_lock);
  257. clk = clk_register_divider(NULL, "cpu_div", "cpu_mux", 0,
  258. SLCR_ARM_CLK_CTRL, 8, 6, CLK_DIVIDER_ONE_BASED |
  259. CLK_DIVIDER_ALLOW_ZERO, &armclk_lock);
  260. clks[cpu_6or4x] = clk_register_gate(NULL, clk_output_name[cpu_6or4x],
  261. "cpu_div", CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED,
  262. SLCR_ARM_CLK_CTRL, 24, 0, &armclk_lock);
  263. clk = clk_register_fixed_factor(NULL, "cpu_3or2x_div", "cpu_div", 0,
  264. 1, 2);
  265. clks[cpu_3or2x] = clk_register_gate(NULL, clk_output_name[cpu_3or2x],
  266. "cpu_3or2x_div", CLK_IGNORE_UNUSED,
  267. SLCR_ARM_CLK_CTRL, 25, 0, &armclk_lock);
  268. clk = clk_register_fixed_factor(NULL, "cpu_2x_div", "cpu_div", 0, 1,
  269. 2 + tmp);
  270. clks[cpu_2x] = clk_register_gate(NULL, clk_output_name[cpu_2x],
  271. "cpu_2x_div", CLK_IGNORE_UNUSED, SLCR_ARM_CLK_CTRL,
  272. 26, 0, &armclk_lock);
  273. clk_prepare_enable(clks[cpu_2x]);
  274. clk = clk_register_fixed_factor(NULL, "cpu_1x_div", "cpu_div", 0, 1,
  275. 4 + 2 * tmp);
  276. clks[cpu_1x] = clk_register_gate(NULL, clk_output_name[cpu_1x],
  277. "cpu_1x_div", CLK_IGNORE_UNUSED, SLCR_ARM_CLK_CTRL, 27,
  278. 0, &armclk_lock);
  279. /* Timers */
  280. swdt_ext_clk_mux_parents[0] = clk_output_name[cpu_1x];
  281. for (i = 0; i < ARRAY_SIZE(swdt_ext_clk_input_names); i++) {
  282. int idx = of_property_match_string(np, "clock-names",
  283. swdt_ext_clk_input_names[i]);
  284. if (idx >= 0)
  285. swdt_ext_clk_mux_parents[i + 1] =
  286. of_clk_get_parent_name(np, idx);
  287. else
  288. swdt_ext_clk_mux_parents[i + 1] = dummy_nm;
  289. }
  290. clks[swdt] = clk_register_mux(NULL, clk_output_name[swdt],
  291. swdt_ext_clk_mux_parents, 2, CLK_SET_RATE_PARENT |
  292. CLK_SET_RATE_NO_REPARENT, SLCR_SWDT_CLK_SEL, 0, 1, 0,
  293. &swdtclk_lock);
  294. /* DDR clocks */
  295. clk = clk_register_divider(NULL, "ddr2x_div", "ddrpll", 0,
  296. SLCR_DDR_CLK_CTRL, 26, 6, CLK_DIVIDER_ONE_BASED |
  297. CLK_DIVIDER_ALLOW_ZERO, &ddrclk_lock);
  298. clks[ddr2x] = clk_register_gate(NULL, clk_output_name[ddr2x],
  299. "ddr2x_div", 0, SLCR_DDR_CLK_CTRL, 1, 0, &ddrclk_lock);
  300. clk_prepare_enable(clks[ddr2x]);
  301. clk = clk_register_divider(NULL, "ddr3x_div", "ddrpll", 0,
  302. SLCR_DDR_CLK_CTRL, 20, 6, CLK_DIVIDER_ONE_BASED |
  303. CLK_DIVIDER_ALLOW_ZERO, &ddrclk_lock);
  304. clks[ddr3x] = clk_register_gate(NULL, clk_output_name[ddr3x],
  305. "ddr3x_div", 0, SLCR_DDR_CLK_CTRL, 0, 0, &ddrclk_lock);
  306. clk_prepare_enable(clks[ddr3x]);
  307. clk = clk_register_divider(NULL, "dci_div0", "ddrpll", 0,
  308. SLCR_DCI_CLK_CTRL, 8, 6, CLK_DIVIDER_ONE_BASED |
  309. CLK_DIVIDER_ALLOW_ZERO, &dciclk_lock);
  310. clk = clk_register_divider(NULL, "dci_div1", "dci_div0",
  311. CLK_SET_RATE_PARENT, SLCR_DCI_CLK_CTRL, 20, 6,
  312. CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
  313. &dciclk_lock);
  314. clks[dci] = clk_register_gate(NULL, clk_output_name[dci], "dci_div1",
  315. CLK_SET_RATE_PARENT, SLCR_DCI_CLK_CTRL, 0, 0,
  316. &dciclk_lock);
  317. clk_prepare_enable(clks[dci]);
  318. /* Peripheral clocks */
  319. for (i = fclk0; i <= fclk3; i++) {
  320. int enable = !!(fclk_enable & BIT(i - fclk0));
  321. zynq_clk_register_fclk(i, clk_output_name[i],
  322. SLCR_FPGA0_CLK_CTRL + 0x10 * (i - fclk0),
  323. periph_parents, enable);
  324. }
  325. zynq_clk_register_periph_clk(lqspi, 0, clk_output_name[lqspi], NULL,
  326. SLCR_LQSPI_CLK_CTRL, periph_parents, 0);
  327. zynq_clk_register_periph_clk(smc, 0, clk_output_name[smc], NULL,
  328. SLCR_SMC_CLK_CTRL, periph_parents, 0);
  329. zynq_clk_register_periph_clk(pcap, 0, clk_output_name[pcap], NULL,
  330. SLCR_PCAP_CLK_CTRL, periph_parents, 0);
  331. zynq_clk_register_periph_clk(sdio0, sdio1, clk_output_name[sdio0],
  332. clk_output_name[sdio1], SLCR_SDIO_CLK_CTRL,
  333. periph_parents, 1);
  334. zynq_clk_register_periph_clk(uart0, uart1, clk_output_name[uart0],
  335. clk_output_name[uart1], SLCR_UART_CLK_CTRL,
  336. periph_parents, 1);
  337. zynq_clk_register_periph_clk(spi0, spi1, clk_output_name[spi0],
  338. clk_output_name[spi1], SLCR_SPI_CLK_CTRL,
  339. periph_parents, 1);
  340. for (i = 0; i < ARRAY_SIZE(gem0_emio_input_names); i++) {
  341. int idx = of_property_match_string(np, "clock-names",
  342. gem0_emio_input_names[i]);
  343. if (idx >= 0)
  344. gem0_mux_parents[i + 1] = of_clk_get_parent_name(np,
  345. idx);
  346. }
  347. clk = clk_register_mux(NULL, "gem0_mux", periph_parents, 4,
  348. CLK_SET_RATE_NO_REPARENT, SLCR_GEM0_CLK_CTRL, 4, 2, 0,
  349. &gem0clk_lock);
  350. clk = clk_register_divider(NULL, "gem0_div0", "gem0_mux", 0,
  351. SLCR_GEM0_CLK_CTRL, 8, 6, CLK_DIVIDER_ONE_BASED |
  352. CLK_DIVIDER_ALLOW_ZERO, &gem0clk_lock);
  353. clk = clk_register_divider(NULL, "gem0_div1", "gem0_div0",
  354. CLK_SET_RATE_PARENT, SLCR_GEM0_CLK_CTRL, 20, 6,
  355. CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
  356. &gem0clk_lock);
  357. clk = clk_register_mux(NULL, "gem0_emio_mux", gem0_mux_parents, 2,
  358. CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT,
  359. SLCR_GEM0_CLK_CTRL, 6, 1, 0,
  360. &gem0clk_lock);
  361. clks[gem0] = clk_register_gate(NULL, clk_output_name[gem0],
  362. "gem0_emio_mux", CLK_SET_RATE_PARENT,
  363. SLCR_GEM0_CLK_CTRL, 0, 0, &gem0clk_lock);
  364. for (i = 0; i < ARRAY_SIZE(gem1_emio_input_names); i++) {
  365. int idx = of_property_match_string(np, "clock-names",
  366. gem1_emio_input_names[i]);
  367. if (idx >= 0)
  368. gem1_mux_parents[i + 1] = of_clk_get_parent_name(np,
  369. idx);
  370. }
  371. clk = clk_register_mux(NULL, "gem1_mux", periph_parents, 4,
  372. CLK_SET_RATE_NO_REPARENT, SLCR_GEM1_CLK_CTRL, 4, 2, 0,
  373. &gem1clk_lock);
  374. clk = clk_register_divider(NULL, "gem1_div0", "gem1_mux", 0,
  375. SLCR_GEM1_CLK_CTRL, 8, 6, CLK_DIVIDER_ONE_BASED |
  376. CLK_DIVIDER_ALLOW_ZERO, &gem1clk_lock);
  377. clk = clk_register_divider(NULL, "gem1_div1", "gem1_div0",
  378. CLK_SET_RATE_PARENT, SLCR_GEM1_CLK_CTRL, 20, 6,
  379. CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
  380. &gem1clk_lock);
  381. clk = clk_register_mux(NULL, "gem1_emio_mux", gem1_mux_parents, 2,
  382. CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT,
  383. SLCR_GEM1_CLK_CTRL, 6, 1, 0,
  384. &gem1clk_lock);
  385. clks[gem1] = clk_register_gate(NULL, clk_output_name[gem1],
  386. "gem1_emio_mux", CLK_SET_RATE_PARENT,
  387. SLCR_GEM1_CLK_CTRL, 0, 0, &gem1clk_lock);
  388. tmp = strlen("mio_clk_00x");
  389. clk_name = kmalloc(tmp, GFP_KERNEL);
  390. for (i = 0; i < NUM_MIO_PINS; i++) {
  391. int idx;
  392. snprintf(clk_name, tmp, "mio_clk_%2.2d", i);
  393. idx = of_property_match_string(np, "clock-names", clk_name);
  394. if (idx >= 0)
  395. can_mio_mux_parents[i] = of_clk_get_parent_name(np,
  396. idx);
  397. else
  398. can_mio_mux_parents[i] = dummy_nm;
  399. }
  400. kfree(clk_name);
  401. clk = clk_register_mux(NULL, "can_mux", periph_parents, 4,
  402. CLK_SET_RATE_NO_REPARENT, SLCR_CAN_CLK_CTRL, 4, 2, 0,
  403. &canclk_lock);
  404. clk = clk_register_divider(NULL, "can_div0", "can_mux", 0,
  405. SLCR_CAN_CLK_CTRL, 8, 6, CLK_DIVIDER_ONE_BASED |
  406. CLK_DIVIDER_ALLOW_ZERO, &canclk_lock);
  407. clk = clk_register_divider(NULL, "can_div1", "can_div0",
  408. CLK_SET_RATE_PARENT, SLCR_CAN_CLK_CTRL, 20, 6,
  409. CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
  410. &canclk_lock);
  411. clk = clk_register_gate(NULL, "can0_gate", "can_div1",
  412. CLK_SET_RATE_PARENT, SLCR_CAN_CLK_CTRL, 0, 0,
  413. &canclk_lock);
  414. clk = clk_register_gate(NULL, "can1_gate", "can_div1",
  415. CLK_SET_RATE_PARENT, SLCR_CAN_CLK_CTRL, 1, 0,
  416. &canclk_lock);
  417. clk = clk_register_mux(NULL, "can0_mio_mux",
  418. can_mio_mux_parents, 54, CLK_SET_RATE_PARENT |
  419. CLK_SET_RATE_NO_REPARENT, SLCR_CAN_MIOCLK_CTRL, 0, 6, 0,
  420. &canmioclk_lock);
  421. clk = clk_register_mux(NULL, "can1_mio_mux",
  422. can_mio_mux_parents, 54, CLK_SET_RATE_PARENT |
  423. CLK_SET_RATE_NO_REPARENT, SLCR_CAN_MIOCLK_CTRL, 16, 6,
  424. 0, &canmioclk_lock);
  425. clks[can0] = clk_register_mux(NULL, clk_output_name[can0],
  426. can0_mio_mux2_parents, 2, CLK_SET_RATE_PARENT |
  427. CLK_SET_RATE_NO_REPARENT, SLCR_CAN_MIOCLK_CTRL, 6, 1, 0,
  428. &canmioclk_lock);
  429. clks[can1] = clk_register_mux(NULL, clk_output_name[can1],
  430. can1_mio_mux2_parents, 2, CLK_SET_RATE_PARENT |
  431. CLK_SET_RATE_NO_REPARENT, SLCR_CAN_MIOCLK_CTRL, 22, 1,
  432. 0, &canmioclk_lock);
  433. for (i = 0; i < ARRAY_SIZE(dbgtrc_emio_input_names); i++) {
  434. int idx = of_property_match_string(np, "clock-names",
  435. dbgtrc_emio_input_names[i]);
  436. if (idx >= 0)
  437. dbg_emio_mux_parents[i + 1] = of_clk_get_parent_name(np,
  438. idx);
  439. }
  440. clk = clk_register_mux(NULL, "dbg_mux", periph_parents, 4,
  441. CLK_SET_RATE_NO_REPARENT, SLCR_DBG_CLK_CTRL, 4, 2, 0,
  442. &dbgclk_lock);
  443. clk = clk_register_divider(NULL, "dbg_div", "dbg_mux", 0,
  444. SLCR_DBG_CLK_CTRL, 8, 6, CLK_DIVIDER_ONE_BASED |
  445. CLK_DIVIDER_ALLOW_ZERO, &dbgclk_lock);
  446. clk = clk_register_mux(NULL, "dbg_emio_mux", dbg_emio_mux_parents, 2,
  447. CLK_SET_RATE_NO_REPARENT, SLCR_DBG_CLK_CTRL, 6, 1, 0,
  448. &dbgclk_lock);
  449. clks[dbg_trc] = clk_register_gate(NULL, clk_output_name[dbg_trc],
  450. "dbg_emio_mux", CLK_SET_RATE_PARENT, SLCR_DBG_CLK_CTRL,
  451. 0, 0, &dbgclk_lock);
  452. clks[dbg_apb] = clk_register_gate(NULL, clk_output_name[dbg_apb],
  453. clk_output_name[cpu_1x], 0, SLCR_DBG_CLK_CTRL, 1, 0,
  454. &dbgclk_lock);
  455. /* leave debug clocks in the state the bootloader set them up to */
  456. tmp = clk_readl(SLCR_DBG_CLK_CTRL);
  457. if (tmp & DBG_CLK_CTRL_CLKACT_TRC)
  458. if (clk_prepare_enable(clks[dbg_trc]))
  459. pr_warn("%s: trace clk enable failed\n", __func__);
  460. if (tmp & DBG_CLK_CTRL_CPU_1XCLKACT)
  461. if (clk_prepare_enable(clks[dbg_apb]))
  462. pr_warn("%s: debug APB clk enable failed\n", __func__);
  463. /* One gated clock for all APER clocks. */
  464. clks[dma] = clk_register_gate(NULL, clk_output_name[dma],
  465. clk_output_name[cpu_2x], 0, SLCR_APER_CLK_CTRL, 0, 0,
  466. &aperclk_lock);
  467. clks[usb0_aper] = clk_register_gate(NULL, clk_output_name[usb0_aper],
  468. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 2, 0,
  469. &aperclk_lock);
  470. clks[usb1_aper] = clk_register_gate(NULL, clk_output_name[usb1_aper],
  471. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 3, 0,
  472. &aperclk_lock);
  473. clks[gem0_aper] = clk_register_gate(NULL, clk_output_name[gem0_aper],
  474. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 6, 0,
  475. &aperclk_lock);
  476. clks[gem1_aper] = clk_register_gate(NULL, clk_output_name[gem1_aper],
  477. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 7, 0,
  478. &aperclk_lock);
  479. clks[sdio0_aper] = clk_register_gate(NULL, clk_output_name[sdio0_aper],
  480. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 10, 0,
  481. &aperclk_lock);
  482. clks[sdio1_aper] = clk_register_gate(NULL, clk_output_name[sdio1_aper],
  483. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 11, 0,
  484. &aperclk_lock);
  485. clks[spi0_aper] = clk_register_gate(NULL, clk_output_name[spi0_aper],
  486. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 14, 0,
  487. &aperclk_lock);
  488. clks[spi1_aper] = clk_register_gate(NULL, clk_output_name[spi1_aper],
  489. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 15, 0,
  490. &aperclk_lock);
  491. clks[can0_aper] = clk_register_gate(NULL, clk_output_name[can0_aper],
  492. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 16, 0,
  493. &aperclk_lock);
  494. clks[can1_aper] = clk_register_gate(NULL, clk_output_name[can1_aper],
  495. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 17, 0,
  496. &aperclk_lock);
  497. clks[i2c0_aper] = clk_register_gate(NULL, clk_output_name[i2c0_aper],
  498. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 18, 0,
  499. &aperclk_lock);
  500. clks[i2c1_aper] = clk_register_gate(NULL, clk_output_name[i2c1_aper],
  501. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 19, 0,
  502. &aperclk_lock);
  503. clks[uart0_aper] = clk_register_gate(NULL, clk_output_name[uart0_aper],
  504. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 20, 0,
  505. &aperclk_lock);
  506. clks[uart1_aper] = clk_register_gate(NULL, clk_output_name[uart1_aper],
  507. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 21, 0,
  508. &aperclk_lock);
  509. clks[gpio_aper] = clk_register_gate(NULL, clk_output_name[gpio_aper],
  510. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 22, 0,
  511. &aperclk_lock);
  512. clks[lqspi_aper] = clk_register_gate(NULL, clk_output_name[lqspi_aper],
  513. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 23, 0,
  514. &aperclk_lock);
  515. clks[smc_aper] = clk_register_gate(NULL, clk_output_name[smc_aper],
  516. clk_output_name[cpu_1x], 0, SLCR_APER_CLK_CTRL, 24, 0,
  517. &aperclk_lock);
  518. for (i = 0; i < ARRAY_SIZE(clks); i++) {
  519. if (IS_ERR(clks[i])) {
  520. pr_err("Zynq clk %d: register failed with %ld\n",
  521. i, PTR_ERR(clks[i]));
  522. BUG();
  523. }
  524. }
  525. clk_data.clks = clks;
  526. clk_data.clk_num = ARRAY_SIZE(clks);
  527. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
  528. }
  529. CLK_OF_DECLARE(zynq_clkc, "xlnx,ps7-clkc", zynq_clk_setup);
  530. void __init zynq_clock_init(void)
  531. {
  532. struct device_node *np;
  533. struct device_node *slcr;
  534. struct resource res;
  535. np = of_find_compatible_node(NULL, NULL, "xlnx,ps7-clkc");
  536. if (!np) {
  537. pr_err("%s: clkc node not found\n", __func__);
  538. goto np_err;
  539. }
  540. if (of_address_to_resource(np, 0, &res)) {
  541. pr_err("%s: failed to get resource\n", np->name);
  542. goto np_err;
  543. }
  544. slcr = of_get_parent(np);
  545. if (slcr->data) {
  546. zynq_clkc_base = (__force void __iomem *)slcr->data + res.start;
  547. } else {
  548. pr_err("%s: Unable to get I/O memory\n", np->name);
  549. of_node_put(slcr);
  550. goto np_err;
  551. }
  552. pr_info("%s: clkc starts at %p\n", __func__, zynq_clkc_base);
  553. of_node_put(slcr);
  554. of_node_put(np);
  555. return;
  556. np_err:
  557. of_node_put(np);
  558. BUG();
  559. }