clk-artpec6.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * ARTPEC-6 clock initialization
  3. *
  4. * Copyright 2015-2016 Axis Comunications AB.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/device.h>
  12. #include <linux/io.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <dt-bindings/clock/axis,artpec6-clkctrl.h>
  18. #define NUM_I2S_CLOCKS 2
  19. struct artpec6_clkctrl_drvdata {
  20. struct clk *clk_table[ARTPEC6_CLK_NUMCLOCKS];
  21. void __iomem *syscon_base;
  22. struct clk_onecell_data clk_data;
  23. spinlock_t i2scfg_lock;
  24. };
  25. static struct artpec6_clkctrl_drvdata *clkdata;
  26. static const char *const i2s_clk_names[NUM_I2S_CLOCKS] = {
  27. "i2s0",
  28. "i2s1",
  29. };
  30. static const int i2s_clk_indexes[NUM_I2S_CLOCKS] = {
  31. ARTPEC6_CLK_I2S0_CLK,
  32. ARTPEC6_CLK_I2S1_CLK,
  33. };
  34. static void of_artpec6_clkctrl_setup(struct device_node *np)
  35. {
  36. int i;
  37. const char *sys_refclk_name;
  38. u32 pll_mode, pll_m, pll_n;
  39. struct clk **clks;
  40. /* Mandatory parent clock. */
  41. i = of_property_match_string(np, "clock-names", "sys_refclk");
  42. if (i < 0)
  43. return;
  44. sys_refclk_name = of_clk_get_parent_name(np, i);
  45. clkdata = kzalloc(sizeof(*clkdata), GFP_KERNEL);
  46. if (!clkdata)
  47. return;
  48. clks = clkdata->clk_table;
  49. for (i = 0; i < ARTPEC6_CLK_NUMCLOCKS; ++i)
  50. clks[i] = ERR_PTR(-EPROBE_DEFER);
  51. clkdata->syscon_base = of_iomap(np, 0);
  52. BUG_ON(clkdata->syscon_base == NULL);
  53. /* Read PLL1 factors configured by boot strap pins. */
  54. pll_mode = (readl(clkdata->syscon_base) >> 6) & 3;
  55. switch (pll_mode) {
  56. case 0: /* DDR3-2133 mode */
  57. pll_m = 4;
  58. pll_n = 85;
  59. break;
  60. case 1: /* DDR3-1866 mode */
  61. pll_m = 6;
  62. pll_n = 112;
  63. break;
  64. case 2: /* DDR3-1600 mode */
  65. pll_m = 4;
  66. pll_n = 64;
  67. break;
  68. case 3: /* DDR3-1333 mode */
  69. pll_m = 8;
  70. pll_n = 106;
  71. break;
  72. }
  73. clks[ARTPEC6_CLK_CPU] =
  74. clk_register_fixed_factor(NULL, "cpu", sys_refclk_name, 0, pll_n,
  75. pll_m);
  76. clks[ARTPEC6_CLK_CPU_PERIPH] =
  77. clk_register_fixed_factor(NULL, "cpu_periph", "cpu", 0, 1, 2);
  78. /* EPROBE_DEFER on the apb_clock is not handled in amba devices. */
  79. clks[ARTPEC6_CLK_UART_PCLK] =
  80. clk_register_fixed_factor(NULL, "uart_pclk", "cpu", 0, 1, 8);
  81. clks[ARTPEC6_CLK_UART_REFCLK] =
  82. clk_register_fixed_rate(NULL, "uart_ref", sys_refclk_name, 0,
  83. 50000000);
  84. clks[ARTPEC6_CLK_SPI_PCLK] =
  85. clk_register_fixed_factor(NULL, "spi_pclk", "cpu", 0, 1, 8);
  86. clks[ARTPEC6_CLK_SPI_SSPCLK] =
  87. clk_register_fixed_rate(NULL, "spi_sspclk", sys_refclk_name, 0,
  88. 50000000);
  89. clks[ARTPEC6_CLK_DBG_PCLK] =
  90. clk_register_fixed_factor(NULL, "dbg_pclk", "cpu", 0, 1, 8);
  91. clkdata->clk_data.clks = clkdata->clk_table;
  92. clkdata->clk_data.clk_num = ARTPEC6_CLK_NUMCLOCKS;
  93. of_clk_add_provider(np, of_clk_src_onecell_get, &clkdata->clk_data);
  94. }
  95. CLK_OF_DECLARE_DRIVER(artpec6_clkctrl, "axis,artpec6-clkctrl",
  96. of_artpec6_clkctrl_setup);
  97. static int artpec6_clkctrl_probe(struct platform_device *pdev)
  98. {
  99. int propidx;
  100. struct device_node *np = pdev->dev.of_node;
  101. struct device *dev = &pdev->dev;
  102. struct clk **clks = clkdata->clk_table;
  103. const char *sys_refclk_name;
  104. const char *i2s_refclk_name = NULL;
  105. const char *frac_clk_name[2] = { NULL, NULL };
  106. const char *i2s_mux_parents[2];
  107. u32 muxreg;
  108. int i;
  109. int err = 0;
  110. /* Mandatory parent clock. */
  111. propidx = of_property_match_string(np, "clock-names", "sys_refclk");
  112. if (propidx < 0)
  113. return -EINVAL;
  114. sys_refclk_name = of_clk_get_parent_name(np, propidx);
  115. /* Find clock names of optional parent clocks. */
  116. propidx = of_property_match_string(np, "clock-names", "i2s_refclk");
  117. if (propidx >= 0)
  118. i2s_refclk_name = of_clk_get_parent_name(np, propidx);
  119. propidx = of_property_match_string(np, "clock-names", "frac_clk0");
  120. if (propidx >= 0)
  121. frac_clk_name[0] = of_clk_get_parent_name(np, propidx);
  122. propidx = of_property_match_string(np, "clock-names", "frac_clk1");
  123. if (propidx >= 0)
  124. frac_clk_name[1] = of_clk_get_parent_name(np, propidx);
  125. spin_lock_init(&clkdata->i2scfg_lock);
  126. clks[ARTPEC6_CLK_NAND_CLKA] =
  127. clk_register_fixed_factor(dev, "nand_clka", "cpu", 0, 1, 8);
  128. clks[ARTPEC6_CLK_NAND_CLKB] =
  129. clk_register_fixed_rate(dev, "nand_clkb", sys_refclk_name, 0,
  130. 100000000);
  131. clks[ARTPEC6_CLK_ETH_ACLK] =
  132. clk_register_fixed_factor(dev, "eth_aclk", "cpu", 0, 1, 4);
  133. clks[ARTPEC6_CLK_DMA_ACLK] =
  134. clk_register_fixed_factor(dev, "dma_aclk", "cpu", 0, 1, 4);
  135. clks[ARTPEC6_CLK_PTP_REF] =
  136. clk_register_fixed_rate(dev, "ptp_ref", sys_refclk_name, 0,
  137. 100000000);
  138. clks[ARTPEC6_CLK_SD_PCLK] =
  139. clk_register_fixed_rate(dev, "sd_pclk", sys_refclk_name, 0,
  140. 100000000);
  141. clks[ARTPEC6_CLK_SD_IMCLK] =
  142. clk_register_fixed_rate(dev, "sd_imclk", sys_refclk_name, 0,
  143. 100000000);
  144. clks[ARTPEC6_CLK_I2S_HST] =
  145. clk_register_fixed_factor(dev, "i2s_hst", "cpu", 0, 1, 8);
  146. for (i = 0; i < NUM_I2S_CLOCKS; ++i) {
  147. if (i2s_refclk_name && frac_clk_name[i]) {
  148. i2s_mux_parents[0] = frac_clk_name[i];
  149. i2s_mux_parents[1] = i2s_refclk_name;
  150. clks[i2s_clk_indexes[i]] =
  151. clk_register_mux(dev, i2s_clk_names[i],
  152. i2s_mux_parents, 2,
  153. CLK_SET_RATE_NO_REPARENT |
  154. CLK_SET_RATE_PARENT,
  155. clkdata->syscon_base + 0x14, i, 1,
  156. 0, &clkdata->i2scfg_lock);
  157. } else if (frac_clk_name[i]) {
  158. /* Lock the mux for internal clock reference. */
  159. muxreg = readl(clkdata->syscon_base + 0x14);
  160. muxreg &= ~BIT(i);
  161. writel(muxreg, clkdata->syscon_base + 0x14);
  162. clks[i2s_clk_indexes[i]] =
  163. clk_register_fixed_factor(dev, i2s_clk_names[i],
  164. frac_clk_name[i], 0, 1,
  165. 1);
  166. } else if (i2s_refclk_name) {
  167. /* Lock the mux for external clock reference. */
  168. muxreg = readl(clkdata->syscon_base + 0x14);
  169. muxreg |= BIT(i);
  170. writel(muxreg, clkdata->syscon_base + 0x14);
  171. clks[i2s_clk_indexes[i]] =
  172. clk_register_fixed_factor(dev, i2s_clk_names[i],
  173. i2s_refclk_name, 0, 1, 1);
  174. }
  175. }
  176. clks[ARTPEC6_CLK_I2C] =
  177. clk_register_fixed_rate(dev, "i2c", sys_refclk_name, 0, 100000000);
  178. clks[ARTPEC6_CLK_SYS_TIMER] =
  179. clk_register_fixed_rate(dev, "timer", sys_refclk_name, 0,
  180. 100000000);
  181. clks[ARTPEC6_CLK_FRACDIV_IN] =
  182. clk_register_fixed_rate(dev, "fracdiv_in", sys_refclk_name, 0,
  183. 600000000);
  184. for (i = 0; i < ARTPEC6_CLK_NUMCLOCKS; ++i) {
  185. if (IS_ERR(clks[i]) && PTR_ERR(clks[i]) != -EPROBE_DEFER) {
  186. dev_err(dev,
  187. "Failed to register clock at index %d err=%ld\n",
  188. i, PTR_ERR(clks[i]));
  189. err = PTR_ERR(clks[i]);
  190. }
  191. }
  192. return err;
  193. }
  194. static const struct of_device_id artpec_clkctrl_of_match[] = {
  195. { .compatible = "axis,artpec6-clkctrl" },
  196. {}
  197. };
  198. static struct platform_driver artpec6_clkctrl_driver = {
  199. .probe = artpec6_clkctrl_probe,
  200. .driver = {
  201. .name = "artpec6_clkctrl",
  202. .of_match_table = artpec_clkctrl_of_match,
  203. },
  204. };
  205. builtin_platform_driver(artpec6_clkctrl_driver);