sama5d3.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/clk-provider.h>
  3. #include <linux/mfd/syscon.h>
  4. #include <linux/slab.h>
  5. #include <dt-bindings/clock/at91.h>
  6. #include "pmc.h"
  7. static DEFINE_SPINLOCK(mck_lock);
  8. static const struct clk_master_characteristics mck_characteristics = {
  9. .output = { .min = 0, .max = 166000000 },
  10. .divisors = { 1, 2, 4, 3 },
  11. };
  12. static u8 plla_out[] = { 0 };
  13. static u16 plla_icpll[] = { 0 };
  14. static const struct clk_range plla_outputs[] = {
  15. { .min = 400000000, .max = 1000000000 },
  16. };
  17. static const struct clk_pll_characteristics plla_characteristics = {
  18. .input = { .min = 8000000, .max = 50000000 },
  19. .num_output = ARRAY_SIZE(plla_outputs),
  20. .output = plla_outputs,
  21. .icpll = plla_icpll,
  22. .out = plla_out,
  23. };
  24. static const struct clk_pcr_layout sama5d3_pcr_layout = {
  25. .offset = 0x10c,
  26. .cmd = BIT(12),
  27. .pid_mask = GENMASK(6, 0),
  28. .div_mask = GENMASK(17, 16),
  29. };
  30. static const struct {
  31. char *n;
  32. char *p;
  33. unsigned long flags;
  34. u8 id;
  35. } sama5d3_systemck[] = {
  36. /*
  37. * ddrck feeds DDR controller and is enabled by bootloader thus we need
  38. * to keep it enabled in case there is no Linux consumer for it.
  39. */
  40. { .n = "ddrck", .p = "masterck_div", .id = 2, .flags = CLK_IS_CRITICAL },
  41. { .n = "lcdck", .p = "masterck_div", .id = 3 },
  42. { .n = "smdck", .p = "smdclk", .id = 4 },
  43. { .n = "uhpck", .p = "usbck", .id = 6 },
  44. { .n = "udpck", .p = "usbck", .id = 7 },
  45. { .n = "pck0", .p = "prog0", .id = 8 },
  46. { .n = "pck1", .p = "prog1", .id = 9 },
  47. { .n = "pck2", .p = "prog2", .id = 10 },
  48. };
  49. static const struct {
  50. char *n;
  51. u8 id;
  52. struct clk_range r;
  53. unsigned long flags;
  54. } sama5d3_periphck[] = {
  55. { .n = "dbgu_clk", .id = 2, },
  56. { .n = "hsmc_clk", .id = 5, },
  57. { .n = "pioA_clk", .id = 6, },
  58. { .n = "pioB_clk", .id = 7, },
  59. { .n = "pioC_clk", .id = 8, },
  60. { .n = "pioD_clk", .id = 9, },
  61. { .n = "pioE_clk", .id = 10, },
  62. { .n = "usart0_clk", .id = 12, .r = { .min = 0, .max = 83000000 }, },
  63. { .n = "usart1_clk", .id = 13, .r = { .min = 0, .max = 83000000 }, },
  64. { .n = "usart2_clk", .id = 14, .r = { .min = 0, .max = 83000000 }, },
  65. { .n = "usart3_clk", .id = 15, .r = { .min = 0, .max = 83000000 }, },
  66. { .n = "uart0_clk", .id = 16, .r = { .min = 0, .max = 83000000 }, },
  67. { .n = "uart1_clk", .id = 17, .r = { .min = 0, .max = 83000000 }, },
  68. { .n = "twi0_clk", .id = 18, .r = { .min = 0, .max = 41500000 }, },
  69. { .n = "twi1_clk", .id = 19, .r = { .min = 0, .max = 41500000 }, },
  70. { .n = "twi2_clk", .id = 20, .r = { .min = 0, .max = 41500000 }, },
  71. { .n = "mci0_clk", .id = 21, },
  72. { .n = "mci1_clk", .id = 22, },
  73. { .n = "mci2_clk", .id = 23, },
  74. { .n = "spi0_clk", .id = 24, .r = { .min = 0, .max = 166000000 }, },
  75. { .n = "spi1_clk", .id = 25, .r = { .min = 0, .max = 166000000 }, },
  76. { .n = "tcb0_clk", .id = 26, .r = { .min = 0, .max = 166000000 }, },
  77. { .n = "tcb1_clk", .id = 27, .r = { .min = 0, .max = 166000000 }, },
  78. { .n = "pwm_clk", .id = 28, },
  79. { .n = "adc_clk", .id = 29, .r = { .min = 0, .max = 83000000 }, },
  80. { .n = "dma0_clk", .id = 30, },
  81. { .n = "dma1_clk", .id = 31, },
  82. { .n = "uhphs_clk", .id = 32, },
  83. { .n = "udphs_clk", .id = 33, },
  84. { .n = "macb0_clk", .id = 34, },
  85. { .n = "macb1_clk", .id = 35, },
  86. { .n = "lcdc_clk", .id = 36, },
  87. { .n = "isi_clk", .id = 37, },
  88. { .n = "ssc0_clk", .id = 38, .r = { .min = 0, .max = 83000000 }, },
  89. { .n = "ssc1_clk", .id = 39, .r = { .min = 0, .max = 83000000 }, },
  90. { .n = "can0_clk", .id = 40, .r = { .min = 0, .max = 83000000 }, },
  91. { .n = "can1_clk", .id = 41, .r = { .min = 0, .max = 83000000 }, },
  92. { .n = "sha_clk", .id = 42, },
  93. { .n = "aes_clk", .id = 43, },
  94. { .n = "tdes_clk", .id = 44, },
  95. { .n = "trng_clk", .id = 45, },
  96. { .n = "fuse_clk", .id = 48, },
  97. /*
  98. * mpddr_clk feeds DDR controller and is enabled by bootloader thus we
  99. * need to keep it enabled in case there is no Linux consumer for it.
  100. */
  101. { .n = "mpddr_clk", .id = 49, .flags = CLK_IS_CRITICAL },
  102. };
  103. static void __init sama5d3_pmc_setup(struct device_node *np)
  104. {
  105. const char *slck_name, *mainxtal_name;
  106. struct pmc_data *sama5d3_pmc;
  107. const char *parent_names[5];
  108. struct regmap *regmap;
  109. struct clk_hw *hw;
  110. int i;
  111. bool bypass;
  112. i = of_property_match_string(np, "clock-names", "slow_clk");
  113. if (i < 0)
  114. return;
  115. slck_name = of_clk_get_parent_name(np, i);
  116. i = of_property_match_string(np, "clock-names", "main_xtal");
  117. if (i < 0)
  118. return;
  119. mainxtal_name = of_clk_get_parent_name(np, i);
  120. regmap = device_node_to_regmap(np);
  121. if (IS_ERR(regmap))
  122. return;
  123. sama5d3_pmc = pmc_data_allocate(PMC_PLLACK + 1,
  124. nck(sama5d3_systemck),
  125. nck(sama5d3_periphck), 0, 3);
  126. if (!sama5d3_pmc)
  127. return;
  128. hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000,
  129. 50000000);
  130. if (IS_ERR(hw))
  131. goto err_free;
  132. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  133. hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name, NULL,
  134. bypass);
  135. if (IS_ERR(hw))
  136. goto err_free;
  137. parent_names[0] = "main_rc_osc";
  138. parent_names[1] = "main_osc";
  139. hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, NULL, 2);
  140. if (IS_ERR(hw))
  141. goto err_free;
  142. hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
  143. &sama5d3_pll_layout, &plla_characteristics);
  144. if (IS_ERR(hw))
  145. goto err_free;
  146. hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
  147. if (IS_ERR(hw))
  148. goto err_free;
  149. sama5d3_pmc->chws[PMC_PLLACK] = hw;
  150. hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck", NULL);
  151. if (IS_ERR(hw))
  152. goto err_free;
  153. sama5d3_pmc->chws[PMC_UTMI] = hw;
  154. parent_names[0] = slck_name;
  155. parent_names[1] = "mainck";
  156. parent_names[2] = "plladivck";
  157. parent_names[3] = "utmick";
  158. hw = at91_clk_register_master_pres(regmap, "masterck_pres", 4,
  159. parent_names, NULL,
  160. &at91sam9x5_master_layout,
  161. &mck_characteristics, &mck_lock);
  162. if (IS_ERR(hw))
  163. goto err_free;
  164. hw = at91_clk_register_master_div(regmap, "masterck_div",
  165. "masterck_pres", NULL,
  166. &at91sam9x5_master_layout,
  167. &mck_characteristics, &mck_lock,
  168. CLK_SET_RATE_GATE, 0);
  169. if (IS_ERR(hw))
  170. goto err_free;
  171. sama5d3_pmc->chws[PMC_MCK] = hw;
  172. parent_names[0] = "plladivck";
  173. parent_names[1] = "utmick";
  174. hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2);
  175. if (IS_ERR(hw))
  176. goto err_free;
  177. hw = at91sam9x5_clk_register_smd(regmap, "smdclk", parent_names, 2);
  178. if (IS_ERR(hw))
  179. goto err_free;
  180. parent_names[0] = slck_name;
  181. parent_names[1] = "mainck";
  182. parent_names[2] = "plladivck";
  183. parent_names[3] = "utmick";
  184. parent_names[4] = "masterck_div";
  185. for (i = 0; i < 3; i++) {
  186. char name[6];
  187. snprintf(name, sizeof(name), "prog%d", i);
  188. hw = at91_clk_register_programmable(regmap, name,
  189. parent_names, NULL, 5, i,
  190. &at91sam9x5_programmable_layout,
  191. NULL);
  192. if (IS_ERR(hw))
  193. goto err_free;
  194. sama5d3_pmc->pchws[i] = hw;
  195. }
  196. for (i = 0; i < ARRAY_SIZE(sama5d3_systemck); i++) {
  197. hw = at91_clk_register_system(regmap, sama5d3_systemck[i].n,
  198. sama5d3_systemck[i].p, NULL,
  199. sama5d3_systemck[i].id,
  200. sama5d3_systemck[i].flags);
  201. if (IS_ERR(hw))
  202. goto err_free;
  203. sama5d3_pmc->shws[sama5d3_systemck[i].id] = hw;
  204. }
  205. for (i = 0; i < ARRAY_SIZE(sama5d3_periphck); i++) {
  206. hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
  207. &sama5d3_pcr_layout,
  208. sama5d3_periphck[i].n,
  209. "masterck_div", NULL,
  210. sama5d3_periphck[i].id,
  211. &sama5d3_periphck[i].r,
  212. INT_MIN,
  213. sama5d3_periphck[i].flags);
  214. if (IS_ERR(hw))
  215. goto err_free;
  216. sama5d3_pmc->phws[sama5d3_periphck[i].id] = hw;
  217. }
  218. of_clk_add_hw_provider(np, of_clk_hw_pmc_get, sama5d3_pmc);
  219. return;
  220. err_free:
  221. kfree(sama5d3_pmc);
  222. }
  223. /*
  224. * The TCB is used as the clocksource so its clock is needed early. This means
  225. * this can't be a platform driver.
  226. */
  227. CLK_OF_DECLARE(sama5d3_pmc, "atmel,sama5d3-pmc", sama5d3_pmc_setup);