sam9x60.c 10 KB

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