sam9x60.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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(pmc_pll_lock);
  8. static DEFINE_SPINLOCK(mck_lock);
  9. static const struct clk_master_characteristics mck_characteristics = {
  10. .output = { .min = 140000000, .max = 200000000 },
  11. .divisors = { 1, 2, 4, 3 },
  12. .have_div3_pres = 1,
  13. };
  14. static const struct clk_master_layout sam9x60_master_layout = {
  15. .mask = 0x373,
  16. .pres_shift = 4,
  17. .offset = 0x28,
  18. };
  19. static const struct clk_range plla_outputs[] = {
  20. { .min = 2343750, .max = 1200000000 },
  21. };
  22. static const struct clk_pll_characteristics plla_characteristics = {
  23. .input = { .min = 12000000, .max = 48000000 },
  24. .num_output = ARRAY_SIZE(plla_outputs),
  25. .output = plla_outputs,
  26. };
  27. static const struct clk_range upll_outputs[] = {
  28. { .min = 300000000, .max = 500000000 },
  29. };
  30. static const struct clk_pll_characteristics upll_characteristics = {
  31. .input = { .min = 12000000, .max = 48000000 },
  32. .num_output = ARRAY_SIZE(upll_outputs),
  33. .output = upll_outputs,
  34. .upll = true,
  35. };
  36. static const struct clk_pll_layout pll_frac_layout = {
  37. .mul_mask = GENMASK(31, 24),
  38. .frac_mask = GENMASK(21, 0),
  39. .mul_shift = 24,
  40. .frac_shift = 0,
  41. };
  42. static const struct clk_pll_layout pll_div_layout = {
  43. .div_mask = GENMASK(7, 0),
  44. .endiv_mask = BIT(29),
  45. .div_shift = 0,
  46. .endiv_shift = 29,
  47. };
  48. static const struct clk_programmable_layout sam9x60_programmable_layout = {
  49. .pres_mask = 0xff,
  50. .pres_shift = 8,
  51. .css_mask = 0x1f,
  52. .have_slck_mck = 0,
  53. .is_pres_direct = 1,
  54. };
  55. static const struct clk_pcr_layout sam9x60_pcr_layout = {
  56. .offset = 0x88,
  57. .cmd = BIT(31),
  58. .gckcss_mask = GENMASK(12, 8),
  59. .pid_mask = GENMASK(6, 0),
  60. };
  61. static const struct {
  62. char *n;
  63. char *p;
  64. u8 id;
  65. } sam9x60_systemck[] = {
  66. { .n = "ddrck", .p = "masterck_div", .id = 2 },
  67. { .n = "uhpck", .p = "usbck", .id = 6 },
  68. { .n = "pck0", .p = "prog0", .id = 8 },
  69. { .n = "pck1", .p = "prog1", .id = 9 },
  70. { .n = "qspick", .p = "masterck_div", .id = 19 },
  71. };
  72. static const struct {
  73. char *n;
  74. u8 id;
  75. } sam9x60_periphck[] = {
  76. { .n = "pioA_clk", .id = 2, },
  77. { .n = "pioB_clk", .id = 3, },
  78. { .n = "pioC_clk", .id = 4, },
  79. { .n = "flex0_clk", .id = 5, },
  80. { .n = "flex1_clk", .id = 6, },
  81. { .n = "flex2_clk", .id = 7, },
  82. { .n = "flex3_clk", .id = 8, },
  83. { .n = "flex6_clk", .id = 9, },
  84. { .n = "flex7_clk", .id = 10, },
  85. { .n = "flex8_clk", .id = 11, },
  86. { .n = "sdmmc0_clk", .id = 12, },
  87. { .n = "flex4_clk", .id = 13, },
  88. { .n = "flex5_clk", .id = 14, },
  89. { .n = "flex9_clk", .id = 15, },
  90. { .n = "flex10_clk", .id = 16, },
  91. { .n = "tcb0_clk", .id = 17, },
  92. { .n = "pwm_clk", .id = 18, },
  93. { .n = "adc_clk", .id = 19, },
  94. { .n = "dma0_clk", .id = 20, },
  95. { .n = "matrix_clk", .id = 21, },
  96. { .n = "uhphs_clk", .id = 22, },
  97. { .n = "udphs_clk", .id = 23, },
  98. { .n = "macb0_clk", .id = 24, },
  99. { .n = "lcd_clk", .id = 25, },
  100. { .n = "sdmmc1_clk", .id = 26, },
  101. { .n = "macb1_clk", .id = 27, },
  102. { .n = "ssc_clk", .id = 28, },
  103. { .n = "can0_clk", .id = 29, },
  104. { .n = "can1_clk", .id = 30, },
  105. { .n = "flex11_clk", .id = 32, },
  106. { .n = "flex12_clk", .id = 33, },
  107. { .n = "i2s_clk", .id = 34, },
  108. { .n = "qspi_clk", .id = 35, },
  109. { .n = "gfx2d_clk", .id = 36, },
  110. { .n = "pit64b_clk", .id = 37, },
  111. { .n = "trng_clk", .id = 38, },
  112. { .n = "aes_clk", .id = 39, },
  113. { .n = "tdes_clk", .id = 40, },
  114. { .n = "sha_clk", .id = 41, },
  115. { .n = "classd_clk", .id = 42, },
  116. { .n = "isi_clk", .id = 43, },
  117. { .n = "pioD_clk", .id = 44, },
  118. { .n = "tcb1_clk", .id = 45, },
  119. { .n = "dbgu_clk", .id = 47, },
  120. { .n = "mpddr_clk", .id = 49, },
  121. };
  122. static const struct {
  123. char *n;
  124. u8 id;
  125. struct clk_range r;
  126. } sam9x60_gck[] = {
  127. { .n = "flex0_gclk", .id = 5, },
  128. { .n = "flex1_gclk", .id = 6, },
  129. { .n = "flex2_gclk", .id = 7, },
  130. { .n = "flex3_gclk", .id = 8, },
  131. { .n = "flex6_gclk", .id = 9, },
  132. { .n = "flex7_gclk", .id = 10, },
  133. { .n = "flex8_gclk", .id = 11, },
  134. { .n = "sdmmc0_gclk", .id = 12, .r = { .min = 0, .max = 105000000 }, },
  135. { .n = "flex4_gclk", .id = 13, },
  136. { .n = "flex5_gclk", .id = 14, },
  137. { .n = "flex9_gclk", .id = 15, },
  138. { .n = "flex10_gclk", .id = 16, },
  139. { .n = "tcb0_gclk", .id = 17, },
  140. { .n = "adc_gclk", .id = 19, },
  141. { .n = "lcd_gclk", .id = 25, .r = { .min = 0, .max = 140000000 }, },
  142. { .n = "sdmmc1_gclk", .id = 26, .r = { .min = 0, .max = 105000000 }, },
  143. { .n = "flex11_gclk", .id = 32, },
  144. { .n = "flex12_gclk", .id = 33, },
  145. { .n = "i2s_gclk", .id = 34, .r = { .min = 0, .max = 105000000 }, },
  146. { .n = "pit64b_gclk", .id = 37, },
  147. { .n = "classd_gclk", .id = 42, .r = { .min = 0, .max = 100000000 }, },
  148. { .n = "tcb1_gclk", .id = 45, },
  149. { .n = "dbgu_gclk", .id = 47, },
  150. };
  151. static void __init sam9x60_pmc_setup(struct device_node *np)
  152. {
  153. struct clk_range range = CLK_RANGE(0, 0);
  154. const char *td_slck_name, *md_slck_name, *mainxtal_name;
  155. struct pmc_data *sam9x60_pmc;
  156. const char *parent_names[6];
  157. struct clk_hw *main_osc_hw;
  158. struct regmap *regmap;
  159. struct clk_hw *hw;
  160. int i;
  161. i = of_property_match_string(np, "clock-names", "td_slck");
  162. if (i < 0)
  163. return;
  164. td_slck_name = of_clk_get_parent_name(np, i);
  165. i = of_property_match_string(np, "clock-names", "md_slck");
  166. if (i < 0)
  167. return;
  168. md_slck_name = of_clk_get_parent_name(np, i);
  169. i = of_property_match_string(np, "clock-names", "main_xtal");
  170. if (i < 0)
  171. return;
  172. mainxtal_name = of_clk_get_parent_name(np, i);
  173. regmap = device_node_to_regmap(np);
  174. if (IS_ERR(regmap))
  175. return;
  176. sam9x60_pmc = pmc_data_allocate(PMC_PLLACK + 1,
  177. nck(sam9x60_systemck),
  178. nck(sam9x60_periphck),
  179. nck(sam9x60_gck), 8);
  180. if (!sam9x60_pmc)
  181. return;
  182. hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000,
  183. 50000000);
  184. if (IS_ERR(hw))
  185. goto err_free;
  186. hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name, 0);
  187. if (IS_ERR(hw))
  188. goto err_free;
  189. main_osc_hw = hw;
  190. parent_names[0] = "main_rc_osc";
  191. parent_names[1] = "main_osc";
  192. hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, 2);
  193. if (IS_ERR(hw))
  194. goto err_free;
  195. sam9x60_pmc->chws[PMC_MAIN] = hw;
  196. hw = sam9x60_clk_register_frac_pll(regmap, &pmc_pll_lock, "pllack_fracck",
  197. "mainck", sam9x60_pmc->chws[PMC_MAIN],
  198. 0, &plla_characteristics,
  199. &pll_frac_layout,
  200. /*
  201. * This feeds pllack_divck which
  202. * feeds CPU. It should not be
  203. * disabled.
  204. */
  205. CLK_IS_CRITICAL | CLK_SET_RATE_GATE);
  206. if (IS_ERR(hw))
  207. goto err_free;
  208. hw = sam9x60_clk_register_div_pll(regmap, &pmc_pll_lock, "pllack_divck",
  209. "pllack_fracck", 0, &plla_characteristics,
  210. &pll_div_layout,
  211. /*
  212. * This feeds CPU. It should not
  213. * be disabled.
  214. */
  215. CLK_IS_CRITICAL | CLK_SET_RATE_GATE, 0);
  216. if (IS_ERR(hw))
  217. goto err_free;
  218. sam9x60_pmc->chws[PMC_PLLACK] = hw;
  219. hw = sam9x60_clk_register_frac_pll(regmap, &pmc_pll_lock, "upllck_fracck",
  220. "main_osc", main_osc_hw, 1,
  221. &upll_characteristics,
  222. &pll_frac_layout, CLK_SET_RATE_GATE);
  223. if (IS_ERR(hw))
  224. goto err_free;
  225. hw = sam9x60_clk_register_div_pll(regmap, &pmc_pll_lock, "upllck_divck",
  226. "upllck_fracck", 1, &upll_characteristics,
  227. &pll_div_layout,
  228. CLK_SET_RATE_GATE |
  229. CLK_SET_PARENT_GATE |
  230. CLK_SET_RATE_PARENT, 0);
  231. if (IS_ERR(hw))
  232. goto err_free;
  233. sam9x60_pmc->chws[PMC_UTMI] = hw;
  234. parent_names[0] = md_slck_name;
  235. parent_names[1] = "mainck";
  236. parent_names[2] = "pllack_divck";
  237. hw = at91_clk_register_master_pres(regmap, "masterck_pres", 3,
  238. parent_names, &sam9x60_master_layout,
  239. &mck_characteristics, &mck_lock);
  240. if (IS_ERR(hw))
  241. goto err_free;
  242. hw = at91_clk_register_master_div(regmap, "masterck_div",
  243. "masterck_pres", &sam9x60_master_layout,
  244. &mck_characteristics, &mck_lock,
  245. CLK_SET_RATE_GATE, 0);
  246. if (IS_ERR(hw))
  247. goto err_free;
  248. sam9x60_pmc->chws[PMC_MCK] = hw;
  249. parent_names[0] = "pllack_divck";
  250. parent_names[1] = "upllck_divck";
  251. parent_names[2] = "main_osc";
  252. hw = sam9x60_clk_register_usb(regmap, "usbck", parent_names, 3);
  253. if (IS_ERR(hw))
  254. goto err_free;
  255. parent_names[0] = md_slck_name;
  256. parent_names[1] = td_slck_name;
  257. parent_names[2] = "mainck";
  258. parent_names[3] = "masterck_div";
  259. parent_names[4] = "pllack_divck";
  260. parent_names[5] = "upllck_divck";
  261. for (i = 0; i < 2; i++) {
  262. char name[6];
  263. snprintf(name, sizeof(name), "prog%d", i);
  264. hw = at91_clk_register_programmable(regmap, name,
  265. parent_names, 6, i,
  266. &sam9x60_programmable_layout,
  267. NULL);
  268. if (IS_ERR(hw))
  269. goto err_free;
  270. sam9x60_pmc->pchws[i] = hw;
  271. }
  272. for (i = 0; i < ARRAY_SIZE(sam9x60_systemck); i++) {
  273. hw = at91_clk_register_system(regmap, sam9x60_systemck[i].n,
  274. sam9x60_systemck[i].p,
  275. sam9x60_systemck[i].id);
  276. if (IS_ERR(hw))
  277. goto err_free;
  278. sam9x60_pmc->shws[sam9x60_systemck[i].id] = hw;
  279. }
  280. for (i = 0; i < ARRAY_SIZE(sam9x60_periphck); i++) {
  281. hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
  282. &sam9x60_pcr_layout,
  283. sam9x60_periphck[i].n,
  284. "masterck_div",
  285. sam9x60_periphck[i].id,
  286. &range, INT_MIN);
  287. if (IS_ERR(hw))
  288. goto err_free;
  289. sam9x60_pmc->phws[sam9x60_periphck[i].id] = hw;
  290. }
  291. for (i = 0; i < ARRAY_SIZE(sam9x60_gck); i++) {
  292. hw = at91_clk_register_generated(regmap, &pmc_pcr_lock,
  293. &sam9x60_pcr_layout,
  294. sam9x60_gck[i].n,
  295. parent_names, NULL, 6,
  296. sam9x60_gck[i].id,
  297. &sam9x60_gck[i].r, INT_MIN);
  298. if (IS_ERR(hw))
  299. goto err_free;
  300. sam9x60_pmc->ghws[sam9x60_gck[i].id] = hw;
  301. }
  302. of_clk_add_hw_provider(np, of_clk_hw_pmc_get, sam9x60_pmc);
  303. return;
  304. err_free:
  305. kfree(sam9x60_pmc);
  306. }
  307. /* Some clks are used for a clocksource */
  308. CLK_OF_DECLARE(sam9x60_pmc, "microchip,sam9x60-pmc", sam9x60_pmc_setup);