at91sam9x5.c 8.5 KB

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