at91sam9x5.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 = 133333333 },
  10. .divisors = { 1, 2, 4, 3 },
  11. .have_div3_pres = 1,
  12. };
  13. static u8 plla_out[] = { 0, 1, 2, 3, 0, 1, 2, 3 };
  14. static u16 plla_icpll[] = { 0, 0, 0, 0, 1, 1, 1, 1 };
  15. static const struct clk_range plla_outputs[] = {
  16. { .min = 745000000, .max = 800000000 },
  17. { .min = 695000000, .max = 750000000 },
  18. { .min = 645000000, .max = 700000000 },
  19. { .min = 595000000, .max = 650000000 },
  20. { .min = 545000000, .max = 600000000 },
  21. { .min = 495000000, .max = 555000000 },
  22. { .min = 445000000, .max = 500000000 },
  23. { .min = 400000000, .max = 450000000 },
  24. };
  25. static const struct clk_pll_characteristics plla_characteristics = {
  26. .input = { .min = 2000000, .max = 32000000 },
  27. .num_output = ARRAY_SIZE(plla_outputs),
  28. .output = plla_outputs,
  29. .icpll = plla_icpll,
  30. .out = plla_out,
  31. };
  32. static const struct {
  33. char *n;
  34. char *p;
  35. unsigned long flags;
  36. u8 id;
  37. } at91sam9x5_systemck[] = {
  38. /*
  39. * ddrck feeds DDR controller and is enabled by bootloader thus we need
  40. * to keep it enabled in case there is no Linux consumer for it.
  41. */
  42. { .n = "ddrck", .p = "masterck_div", .id = 2, .flags = CLK_IS_CRITICAL },
  43. { .n = "smdck", .p = "smdclk", .id = 4 },
  44. { .n = "uhpck", .p = "usbck", .id = 6 },
  45. { .n = "udpck", .p = "usbck", .id = 7 },
  46. { .n = "pck0", .p = "prog0", .id = 8 },
  47. { .n = "pck1", .p = "prog1", .id = 9 },
  48. };
  49. static const struct clk_pcr_layout at91sam9x5_pcr_layout = {
  50. .offset = 0x10c,
  51. .cmd = BIT(12),
  52. .pid_mask = GENMASK(5, 0),
  53. .div_mask = GENMASK(17, 16),
  54. };
  55. struct pck {
  56. char *n;
  57. u8 id;
  58. };
  59. static const struct pck at91sam9x5_periphck[] = {
  60. { .n = "pioAB_clk", .id = 2, },
  61. { .n = "pioCD_clk", .id = 3, },
  62. { .n = "smd_clk", .id = 4, },
  63. { .n = "usart0_clk", .id = 5, },
  64. { .n = "usart1_clk", .id = 6, },
  65. { .n = "usart2_clk", .id = 7, },
  66. { .n = "twi0_clk", .id = 9, },
  67. { .n = "twi1_clk", .id = 10, },
  68. { .n = "twi2_clk", .id = 11, },
  69. { .n = "mci0_clk", .id = 12, },
  70. { .n = "spi0_clk", .id = 13, },
  71. { .n = "spi1_clk", .id = 14, },
  72. { .n = "uart0_clk", .id = 15, },
  73. { .n = "uart1_clk", .id = 16, },
  74. { .n = "tcb0_clk", .id = 17, },
  75. { .n = "pwm_clk", .id = 18, },
  76. { .n = "adc_clk", .id = 19, },
  77. { .n = "dma0_clk", .id = 20, },
  78. { .n = "dma1_clk", .id = 21, },
  79. { .n = "uhphs_clk", .id = 22, },
  80. { .n = "udphs_clk", .id = 23, },
  81. { .n = "mci1_clk", .id = 26, },
  82. { .n = "ssc0_clk", .id = 28, },
  83. };
  84. static const struct pck at91sam9g15_periphck[] = {
  85. { .n = "lcdc_clk", .id = 25, },
  86. { /* sentinel */}
  87. };
  88. static const struct pck at91sam9g25_periphck[] = {
  89. { .n = "usart3_clk", .id = 8, },
  90. { .n = "macb0_clk", .id = 24, },
  91. { .n = "isi_clk", .id = 25, },
  92. { /* sentinel */}
  93. };
  94. static const struct pck at91sam9g35_periphck[] = {
  95. { .n = "macb0_clk", .id = 24, },
  96. { .n = "lcdc_clk", .id = 25, },
  97. { /* sentinel */}
  98. };
  99. static const struct pck at91sam9x25_periphck[] = {
  100. { .n = "usart3_clk", .id = 8, },
  101. { .n = "macb0_clk", .id = 24, },
  102. { .n = "macb1_clk", .id = 27, },
  103. { .n = "can0_clk", .id = 29, },
  104. { .n = "can1_clk", .id = 30, },
  105. { /* sentinel */}
  106. };
  107. static const struct pck at91sam9x35_periphck[] = {
  108. { .n = "macb0_clk", .id = 24, },
  109. { .n = "lcdc_clk", .id = 25, },
  110. { .n = "can0_clk", .id = 29, },
  111. { .n = "can1_clk", .id = 30, },
  112. { /* sentinel */}
  113. };
  114. static void __init at91sam9x5_pmc_setup(struct device_node *np,
  115. const struct pck *extra_pcks,
  116. bool has_lcdck)
  117. {
  118. struct clk_range range = CLK_RANGE(0, 0);
  119. const char *slck_name, *mainxtal_name;
  120. struct pmc_data *at91sam9x5_pmc;
  121. const char *parent_names[6];
  122. struct regmap *regmap;
  123. struct clk_hw *hw;
  124. int i;
  125. bool bypass;
  126. i = of_property_match_string(np, "clock-names", "slow_clk");
  127. if (i < 0)
  128. return;
  129. slck_name = of_clk_get_parent_name(np, i);
  130. i = of_property_match_string(np, "clock-names", "main_xtal");
  131. if (i < 0)
  132. return;
  133. mainxtal_name = of_clk_get_parent_name(np, i);
  134. regmap = device_node_to_regmap(np);
  135. if (IS_ERR(regmap))
  136. return;
  137. at91sam9x5_pmc = pmc_data_allocate(PMC_PLLACK + 1,
  138. nck(at91sam9x5_systemck), 31, 0, 2);
  139. if (!at91sam9x5_pmc)
  140. return;
  141. hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000,
  142. 50000000);
  143. if (IS_ERR(hw))
  144. goto err_free;
  145. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  146. hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name, NULL,
  147. bypass);
  148. if (IS_ERR(hw))
  149. goto err_free;
  150. parent_names[0] = "main_rc_osc";
  151. parent_names[1] = "main_osc";
  152. hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, NULL, 2);
  153. if (IS_ERR(hw))
  154. goto err_free;
  155. at91sam9x5_pmc->chws[PMC_MAIN] = hw;
  156. hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
  157. &at91rm9200_pll_layout, &plla_characteristics);
  158. if (IS_ERR(hw))
  159. goto err_free;
  160. hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
  161. if (IS_ERR(hw))
  162. goto err_free;
  163. at91sam9x5_pmc->chws[PMC_PLLACK] = hw;
  164. hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck", NULL);
  165. if (IS_ERR(hw))
  166. goto err_free;
  167. at91sam9x5_pmc->chws[PMC_UTMI] = hw;
  168. parent_names[0] = slck_name;
  169. parent_names[1] = "mainck";
  170. parent_names[2] = "plladivck";
  171. parent_names[3] = "utmick";
  172. hw = at91_clk_register_master_pres(regmap, "masterck_pres", 4,
  173. parent_names, NULL,
  174. &at91sam9x5_master_layout,
  175. &mck_characteristics, &mck_lock);
  176. if (IS_ERR(hw))
  177. goto err_free;
  178. hw = at91_clk_register_master_div(regmap, "masterck_div",
  179. "masterck_pres", NULL,
  180. &at91sam9x5_master_layout,
  181. &mck_characteristics, &mck_lock,
  182. CLK_SET_RATE_GATE, 0);
  183. if (IS_ERR(hw))
  184. goto err_free;
  185. at91sam9x5_pmc->chws[PMC_MCK] = hw;
  186. parent_names[0] = "plladivck";
  187. parent_names[1] = "utmick";
  188. hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2);
  189. if (IS_ERR(hw))
  190. goto err_free;
  191. hw = at91sam9x5_clk_register_smd(regmap, "smdclk", parent_names, 2);
  192. if (IS_ERR(hw))
  193. goto err_free;
  194. parent_names[0] = slck_name;
  195. parent_names[1] = "mainck";
  196. parent_names[2] = "plladivck";
  197. parent_names[3] = "utmick";
  198. parent_names[4] = "masterck_div";
  199. for (i = 0; i < 2; i++) {
  200. char name[6];
  201. snprintf(name, sizeof(name), "prog%d", i);
  202. hw = at91_clk_register_programmable(regmap, name,
  203. parent_names, NULL, 5, i,
  204. &at91sam9x5_programmable_layout,
  205. NULL);
  206. if (IS_ERR(hw))
  207. goto err_free;
  208. at91sam9x5_pmc->pchws[i] = hw;
  209. }
  210. for (i = 0; i < ARRAY_SIZE(at91sam9x5_systemck); i++) {
  211. hw = at91_clk_register_system(regmap, at91sam9x5_systemck[i].n,
  212. at91sam9x5_systemck[i].p, NULL,
  213. at91sam9x5_systemck[i].id,
  214. at91sam9x5_systemck[i].flags);
  215. if (IS_ERR(hw))
  216. goto err_free;
  217. at91sam9x5_pmc->shws[at91sam9x5_systemck[i].id] = hw;
  218. }
  219. if (has_lcdck) {
  220. hw = at91_clk_register_system(regmap, "lcdck", "masterck_div",
  221. NULL, 3, 0);
  222. if (IS_ERR(hw))
  223. goto err_free;
  224. at91sam9x5_pmc->shws[3] = hw;
  225. }
  226. for (i = 0; i < ARRAY_SIZE(at91sam9x5_periphck); i++) {
  227. hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
  228. &at91sam9x5_pcr_layout,
  229. at91sam9x5_periphck[i].n,
  230. "masterck_div", NULL,
  231. at91sam9x5_periphck[i].id,
  232. &range, INT_MIN, 0);
  233. if (IS_ERR(hw))
  234. goto err_free;
  235. at91sam9x5_pmc->phws[at91sam9x5_periphck[i].id] = hw;
  236. }
  237. for (i = 0; extra_pcks[i].id; i++) {
  238. hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
  239. &at91sam9x5_pcr_layout,
  240. extra_pcks[i].n,
  241. "masterck_div", NULL,
  242. extra_pcks[i].id,
  243. &range, INT_MIN, 0);
  244. if (IS_ERR(hw))
  245. goto err_free;
  246. at91sam9x5_pmc->phws[extra_pcks[i].id] = hw;
  247. }
  248. of_clk_add_hw_provider(np, of_clk_hw_pmc_get, at91sam9x5_pmc);
  249. return;
  250. err_free:
  251. kfree(at91sam9x5_pmc);
  252. }
  253. static void __init at91sam9g15_pmc_setup(struct device_node *np)
  254. {
  255. at91sam9x5_pmc_setup(np, at91sam9g15_periphck, true);
  256. }
  257. CLK_OF_DECLARE(at91sam9g15_pmc, "atmel,at91sam9g15-pmc", at91sam9g15_pmc_setup);
  258. static void __init at91sam9g25_pmc_setup(struct device_node *np)
  259. {
  260. at91sam9x5_pmc_setup(np, at91sam9g25_periphck, false);
  261. }
  262. CLK_OF_DECLARE(at91sam9g25_pmc, "atmel,at91sam9g25-pmc", at91sam9g25_pmc_setup);
  263. static void __init at91sam9g35_pmc_setup(struct device_node *np)
  264. {
  265. at91sam9x5_pmc_setup(np, at91sam9g35_periphck, true);
  266. }
  267. CLK_OF_DECLARE(at91sam9g35_pmc, "atmel,at91sam9g35-pmc", at91sam9g35_pmc_setup);
  268. static void __init at91sam9x25_pmc_setup(struct device_node *np)
  269. {
  270. at91sam9x5_pmc_setup(np, at91sam9x25_periphck, false);
  271. }
  272. CLK_OF_DECLARE(at91sam9x25_pmc, "atmel,at91sam9x25-pmc", at91sam9x25_pmc_setup);
  273. static void __init at91sam9x35_pmc_setup(struct device_node *np)
  274. {
  275. at91sam9x5_pmc_setup(np, at91sam9x35_periphck, true);
  276. }
  277. CLK_OF_DECLARE(at91sam9x35_pmc, "atmel,at91sam9x35-pmc", at91sam9x35_pmc_setup);