at91sam9260.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. struct sck {
  8. char *n;
  9. char *p;
  10. u8 id;
  11. };
  12. struct pck {
  13. char *n;
  14. u8 id;
  15. };
  16. struct at91sam926x_data {
  17. const struct clk_pll_layout *plla_layout;
  18. const struct clk_pll_characteristics *plla_characteristics;
  19. const struct clk_pll_layout *pllb_layout;
  20. const struct clk_pll_characteristics *pllb_characteristics;
  21. const struct clk_master_characteristics *mck_characteristics;
  22. const struct sck *sck;
  23. const struct pck *pck;
  24. u8 num_sck;
  25. u8 num_pck;
  26. u8 num_progck;
  27. bool has_slck;
  28. };
  29. static const struct clk_master_characteristics sam9260_mck_characteristics = {
  30. .output = { .min = 0, .max = 105000000 },
  31. .divisors = { 1, 2, 4, 0 },
  32. };
  33. static u8 sam9260_plla_out[] = { 0, 2 };
  34. static u16 sam9260_plla_icpll[] = { 1, 1 };
  35. static struct clk_range sam9260_plla_outputs[] = {
  36. { .min = 80000000, .max = 160000000 },
  37. { .min = 150000000, .max = 240000000 },
  38. };
  39. static const struct clk_pll_characteristics sam9260_plla_characteristics = {
  40. .input = { .min = 1000000, .max = 32000000 },
  41. .num_output = ARRAY_SIZE(sam9260_plla_outputs),
  42. .output = sam9260_plla_outputs,
  43. .icpll = sam9260_plla_icpll,
  44. .out = sam9260_plla_out,
  45. };
  46. static u8 sam9260_pllb_out[] = { 1 };
  47. static u16 sam9260_pllb_icpll[] = { 1 };
  48. static struct clk_range sam9260_pllb_outputs[] = {
  49. { .min = 70000000, .max = 130000000 },
  50. };
  51. static const struct clk_pll_characteristics sam9260_pllb_characteristics = {
  52. .input = { .min = 1000000, .max = 5000000 },
  53. .num_output = ARRAY_SIZE(sam9260_pllb_outputs),
  54. .output = sam9260_pllb_outputs,
  55. .icpll = sam9260_pllb_icpll,
  56. .out = sam9260_pllb_out,
  57. };
  58. static const struct sck at91sam9260_systemck[] = {
  59. { .n = "uhpck", .p = "usbck", .id = 6 },
  60. { .n = "udpck", .p = "usbck", .id = 7 },
  61. { .n = "pck0", .p = "prog0", .id = 8 },
  62. { .n = "pck1", .p = "prog1", .id = 9 },
  63. };
  64. static const struct pck at91sam9260_periphck[] = {
  65. { .n = "pioA_clk", .id = 2 },
  66. { .n = "pioB_clk", .id = 3 },
  67. { .n = "pioC_clk", .id = 4 },
  68. { .n = "adc_clk", .id = 5 },
  69. { .n = "usart0_clk", .id = 6 },
  70. { .n = "usart1_clk", .id = 7 },
  71. { .n = "usart2_clk", .id = 8 },
  72. { .n = "mci0_clk", .id = 9 },
  73. { .n = "udc_clk", .id = 10 },
  74. { .n = "twi0_clk", .id = 11 },
  75. { .n = "spi0_clk", .id = 12 },
  76. { .n = "spi1_clk", .id = 13 },
  77. { .n = "ssc0_clk", .id = 14 },
  78. { .n = "tc0_clk", .id = 17 },
  79. { .n = "tc1_clk", .id = 18 },
  80. { .n = "tc2_clk", .id = 19 },
  81. { .n = "ohci_clk", .id = 20 },
  82. { .n = "macb0_clk", .id = 21 },
  83. { .n = "isi_clk", .id = 22 },
  84. { .n = "usart3_clk", .id = 23 },
  85. { .n = "uart0_clk", .id = 24 },
  86. { .n = "uart1_clk", .id = 25 },
  87. { .n = "tc3_clk", .id = 26 },
  88. { .n = "tc4_clk", .id = 27 },
  89. { .n = "tc5_clk", .id = 28 },
  90. };
  91. static struct at91sam926x_data at91sam9260_data = {
  92. .plla_layout = &at91rm9200_pll_layout,
  93. .plla_characteristics = &sam9260_plla_characteristics,
  94. .pllb_layout = &at91rm9200_pll_layout,
  95. .pllb_characteristics = &sam9260_pllb_characteristics,
  96. .mck_characteristics = &sam9260_mck_characteristics,
  97. .sck = at91sam9260_systemck,
  98. .num_sck = ARRAY_SIZE(at91sam9260_systemck),
  99. .pck = at91sam9260_periphck,
  100. .num_pck = ARRAY_SIZE(at91sam9260_periphck),
  101. .num_progck = 2,
  102. .has_slck = true,
  103. };
  104. static const struct clk_master_characteristics sam9g20_mck_characteristics = {
  105. .output = { .min = 0, .max = 133000000 },
  106. .divisors = { 1, 2, 4, 6 },
  107. };
  108. static u8 sam9g20_plla_out[] = { 0, 1, 2, 3, 0, 1, 2, 3 };
  109. static u16 sam9g20_plla_icpll[] = { 0, 0, 0, 0, 1, 1, 1, 1 };
  110. static struct clk_range sam9g20_plla_outputs[] = {
  111. { .min = 745000000, .max = 800000000 },
  112. { .min = 695000000, .max = 750000000 },
  113. { .min = 645000000, .max = 700000000 },
  114. { .min = 595000000, .max = 650000000 },
  115. { .min = 545000000, .max = 600000000 },
  116. { .min = 495000000, .max = 550000000 },
  117. { .min = 445000000, .max = 500000000 },
  118. { .min = 400000000, .max = 450000000 },
  119. };
  120. static const struct clk_pll_characteristics sam9g20_plla_characteristics = {
  121. .input = { .min = 2000000, .max = 32000000 },
  122. .num_output = ARRAY_SIZE(sam9g20_plla_outputs),
  123. .output = sam9g20_plla_outputs,
  124. .icpll = sam9g20_plla_icpll,
  125. .out = sam9g20_plla_out,
  126. };
  127. static u8 sam9g20_pllb_out[] = { 0 };
  128. static u16 sam9g20_pllb_icpll[] = { 0 };
  129. static struct clk_range sam9g20_pllb_outputs[] = {
  130. { .min = 30000000, .max = 100000000 },
  131. };
  132. static const struct clk_pll_characteristics sam9g20_pllb_characteristics = {
  133. .input = { .min = 2000000, .max = 32000000 },
  134. .num_output = ARRAY_SIZE(sam9g20_pllb_outputs),
  135. .output = sam9g20_pllb_outputs,
  136. .icpll = sam9g20_pllb_icpll,
  137. .out = sam9g20_pllb_out,
  138. };
  139. static struct at91sam926x_data at91sam9g20_data = {
  140. .plla_layout = &at91sam9g45_pll_layout,
  141. .plla_characteristics = &sam9g20_plla_characteristics,
  142. .pllb_layout = &at91sam9g20_pllb_layout,
  143. .pllb_characteristics = &sam9g20_pllb_characteristics,
  144. .mck_characteristics = &sam9g20_mck_characteristics,
  145. .sck = at91sam9260_systemck,
  146. .num_sck = ARRAY_SIZE(at91sam9260_systemck),
  147. .pck = at91sam9260_periphck,
  148. .num_pck = ARRAY_SIZE(at91sam9260_periphck),
  149. .num_progck = 2,
  150. .has_slck = true,
  151. };
  152. static const struct clk_master_characteristics sam9261_mck_characteristics = {
  153. .output = { .min = 0, .max = 94000000 },
  154. .divisors = { 1, 2, 4, 0 },
  155. };
  156. static struct clk_range sam9261_plla_outputs[] = {
  157. { .min = 80000000, .max = 200000000 },
  158. { .min = 190000000, .max = 240000000 },
  159. };
  160. static const struct clk_pll_characteristics sam9261_plla_characteristics = {
  161. .input = { .min = 1000000, .max = 32000000 },
  162. .num_output = ARRAY_SIZE(sam9261_plla_outputs),
  163. .output = sam9261_plla_outputs,
  164. .icpll = sam9260_plla_icpll,
  165. .out = sam9260_plla_out,
  166. };
  167. static u8 sam9261_pllb_out[] = { 1 };
  168. static u16 sam9261_pllb_icpll[] = { 1 };
  169. static struct clk_range sam9261_pllb_outputs[] = {
  170. { .min = 70000000, .max = 130000000 },
  171. };
  172. static const struct clk_pll_characteristics sam9261_pllb_characteristics = {
  173. .input = { .min = 1000000, .max = 5000000 },
  174. .num_output = ARRAY_SIZE(sam9261_pllb_outputs),
  175. .output = sam9261_pllb_outputs,
  176. .icpll = sam9261_pllb_icpll,
  177. .out = sam9261_pllb_out,
  178. };
  179. static const struct sck at91sam9261_systemck[] = {
  180. { .n = "uhpck", .p = "usbck", .id = 6 },
  181. { .n = "udpck", .p = "usbck", .id = 7 },
  182. { .n = "pck0", .p = "prog0", .id = 8 },
  183. { .n = "pck1", .p = "prog1", .id = 9 },
  184. { .n = "pck2", .p = "prog2", .id = 10 },
  185. { .n = "pck3", .p = "prog3", .id = 11 },
  186. { .n = "hclk0", .p = "masterck", .id = 16 },
  187. { .n = "hclk1", .p = "masterck", .id = 17 },
  188. };
  189. static const struct pck at91sam9261_periphck[] = {
  190. { .n = "pioA_clk", .id = 2, },
  191. { .n = "pioB_clk", .id = 3, },
  192. { .n = "pioC_clk", .id = 4, },
  193. { .n = "usart0_clk", .id = 6, },
  194. { .n = "usart1_clk", .id = 7, },
  195. { .n = "usart2_clk", .id = 8, },
  196. { .n = "mci0_clk", .id = 9, },
  197. { .n = "udc_clk", .id = 10, },
  198. { .n = "twi0_clk", .id = 11, },
  199. { .n = "spi0_clk", .id = 12, },
  200. { .n = "spi1_clk", .id = 13, },
  201. { .n = "ssc0_clk", .id = 14, },
  202. { .n = "ssc1_clk", .id = 15, },
  203. { .n = "ssc2_clk", .id = 16, },
  204. { .n = "tc0_clk", .id = 17, },
  205. { .n = "tc1_clk", .id = 18, },
  206. { .n = "tc2_clk", .id = 19, },
  207. { .n = "ohci_clk", .id = 20, },
  208. { .n = "lcd_clk", .id = 21, },
  209. };
  210. static struct at91sam926x_data at91sam9261_data = {
  211. .plla_layout = &at91rm9200_pll_layout,
  212. .plla_characteristics = &sam9261_plla_characteristics,
  213. .pllb_layout = &at91rm9200_pll_layout,
  214. .pllb_characteristics = &sam9261_pllb_characteristics,
  215. .mck_characteristics = &sam9261_mck_characteristics,
  216. .sck = at91sam9261_systemck,
  217. .num_sck = ARRAY_SIZE(at91sam9261_systemck),
  218. .pck = at91sam9261_periphck,
  219. .num_pck = ARRAY_SIZE(at91sam9261_periphck),
  220. .num_progck = 4,
  221. };
  222. static const struct clk_master_characteristics sam9263_mck_characteristics = {
  223. .output = { .min = 0, .max = 120000000 },
  224. .divisors = { 1, 2, 4, 0 },
  225. };
  226. static struct clk_range sam9263_pll_outputs[] = {
  227. { .min = 80000000, .max = 200000000 },
  228. { .min = 190000000, .max = 240000000 },
  229. };
  230. static const struct clk_pll_characteristics sam9263_pll_characteristics = {
  231. .input = { .min = 1000000, .max = 32000000 },
  232. .num_output = ARRAY_SIZE(sam9263_pll_outputs),
  233. .output = sam9263_pll_outputs,
  234. .icpll = sam9260_plla_icpll,
  235. .out = sam9260_plla_out,
  236. };
  237. static const struct sck at91sam9263_systemck[] = {
  238. { .n = "uhpck", .p = "usbck", .id = 6 },
  239. { .n = "udpck", .p = "usbck", .id = 7 },
  240. { .n = "pck0", .p = "prog0", .id = 8 },
  241. { .n = "pck1", .p = "prog1", .id = 9 },
  242. { .n = "pck2", .p = "prog2", .id = 10 },
  243. { .n = "pck3", .p = "prog3", .id = 11 },
  244. };
  245. static const struct pck at91sam9263_periphck[] = {
  246. { .n = "pioA_clk", .id = 2, },
  247. { .n = "pioB_clk", .id = 3, },
  248. { .n = "pioCDE_clk", .id = 4, },
  249. { .n = "usart0_clk", .id = 7, },
  250. { .n = "usart1_clk", .id = 8, },
  251. { .n = "usart2_clk", .id = 9, },
  252. { .n = "mci0_clk", .id = 10, },
  253. { .n = "mci1_clk", .id = 11, },
  254. { .n = "can_clk", .id = 12, },
  255. { .n = "twi0_clk", .id = 13, },
  256. { .n = "spi0_clk", .id = 14, },
  257. { .n = "spi1_clk", .id = 15, },
  258. { .n = "ssc0_clk", .id = 16, },
  259. { .n = "ssc1_clk", .id = 17, },
  260. { .n = "ac97_clk", .id = 18, },
  261. { .n = "tcb_clk", .id = 19, },
  262. { .n = "pwm_clk", .id = 20, },
  263. { .n = "macb0_clk", .id = 21, },
  264. { .n = "g2de_clk", .id = 23, },
  265. { .n = "udc_clk", .id = 24, },
  266. { .n = "isi_clk", .id = 25, },
  267. { .n = "lcd_clk", .id = 26, },
  268. { .n = "dma_clk", .id = 27, },
  269. { .n = "ohci_clk", .id = 29, },
  270. };
  271. static struct at91sam926x_data at91sam9263_data = {
  272. .plla_layout = &at91rm9200_pll_layout,
  273. .plla_characteristics = &sam9263_pll_characteristics,
  274. .pllb_layout = &at91rm9200_pll_layout,
  275. .pllb_characteristics = &sam9263_pll_characteristics,
  276. .mck_characteristics = &sam9263_mck_characteristics,
  277. .sck = at91sam9263_systemck,
  278. .num_sck = ARRAY_SIZE(at91sam9263_systemck),
  279. .pck = at91sam9263_periphck,
  280. .num_pck = ARRAY_SIZE(at91sam9263_periphck),
  281. .num_progck = 4,
  282. };
  283. static void __init at91sam926x_pmc_setup(struct device_node *np,
  284. struct at91sam926x_data *data)
  285. {
  286. const char *slowxtal_name, *mainxtal_name;
  287. struct pmc_data *at91sam9260_pmc;
  288. u32 usb_div[] = { 1, 2, 4, 0 };
  289. const char *parent_names[6];
  290. const char *slck_name;
  291. struct regmap *regmap;
  292. struct clk_hw *hw;
  293. int i;
  294. bool bypass;
  295. i = of_property_match_string(np, "clock-names", "slow_xtal");
  296. if (i < 0)
  297. return;
  298. slowxtal_name = of_clk_get_parent_name(np, i);
  299. i = of_property_match_string(np, "clock-names", "main_xtal");
  300. if (i < 0)
  301. return;
  302. mainxtal_name = of_clk_get_parent_name(np, i);
  303. regmap = syscon_node_to_regmap(np);
  304. if (IS_ERR(regmap))
  305. return;
  306. at91sam9260_pmc = pmc_data_allocate(PMC_MAIN + 1,
  307. ndck(data->sck, data->num_sck),
  308. ndck(data->pck, data->num_pck), 0);
  309. if (!at91sam9260_pmc)
  310. return;
  311. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  312. hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name,
  313. bypass);
  314. if (IS_ERR(hw))
  315. goto err_free;
  316. hw = at91_clk_register_rm9200_main(regmap, "mainck", "main_osc");
  317. if (IS_ERR(hw))
  318. goto err_free;
  319. at91sam9260_pmc->chws[PMC_MAIN] = hw;
  320. if (data->has_slck) {
  321. hw = clk_hw_register_fixed_rate_with_accuracy(NULL,
  322. "slow_rc_osc",
  323. NULL, 0, 32768,
  324. 50000000);
  325. if (IS_ERR(hw))
  326. goto err_free;
  327. parent_names[0] = "slow_rc_osc";
  328. parent_names[1] = "slow_xtal";
  329. hw = at91_clk_register_sam9260_slow(regmap, "slck",
  330. parent_names, 2);
  331. if (IS_ERR(hw))
  332. goto err_free;
  333. at91sam9260_pmc->chws[PMC_SLOW] = hw;
  334. slck_name = "slck";
  335. } else {
  336. slck_name = slowxtal_name;
  337. }
  338. hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
  339. data->plla_layout,
  340. data->plla_characteristics);
  341. if (IS_ERR(hw))
  342. goto err_free;
  343. hw = at91_clk_register_pll(regmap, "pllbck", "mainck", 1,
  344. data->pllb_layout,
  345. data->pllb_characteristics);
  346. if (IS_ERR(hw))
  347. goto err_free;
  348. parent_names[0] = slck_name;
  349. parent_names[1] = "mainck";
  350. parent_names[2] = "pllack";
  351. parent_names[3] = "pllbck";
  352. hw = at91_clk_register_master(regmap, "masterck", 4, parent_names,
  353. &at91rm9200_master_layout,
  354. data->mck_characteristics);
  355. if (IS_ERR(hw))
  356. goto err_free;
  357. at91sam9260_pmc->chws[PMC_MCK] = hw;
  358. hw = at91rm9200_clk_register_usb(regmap, "usbck", "pllbck", usb_div);
  359. if (IS_ERR(hw))
  360. goto err_free;
  361. parent_names[0] = slck_name;
  362. parent_names[1] = "mainck";
  363. parent_names[2] = "pllack";
  364. parent_names[3] = "pllbck";
  365. for (i = 0; i < data->num_progck; i++) {
  366. char name[6];
  367. snprintf(name, sizeof(name), "prog%d", i);
  368. hw = at91_clk_register_programmable(regmap, name,
  369. parent_names, 4, i,
  370. &at91rm9200_programmable_layout);
  371. if (IS_ERR(hw))
  372. goto err_free;
  373. }
  374. for (i = 0; i < data->num_sck; i++) {
  375. hw = at91_clk_register_system(regmap, data->sck[i].n,
  376. data->sck[i].p,
  377. data->sck[i].id);
  378. if (IS_ERR(hw))
  379. goto err_free;
  380. at91sam9260_pmc->shws[data->sck[i].id] = hw;
  381. }
  382. for (i = 0; i < data->num_pck; i++) {
  383. hw = at91_clk_register_peripheral(regmap,
  384. data->pck[i].n,
  385. "masterck",
  386. data->pck[i].id);
  387. if (IS_ERR(hw))
  388. goto err_free;
  389. at91sam9260_pmc->phws[data->pck[i].id] = hw;
  390. }
  391. of_clk_add_hw_provider(np, of_clk_hw_pmc_get, at91sam9260_pmc);
  392. return;
  393. err_free:
  394. pmc_data_free(at91sam9260_pmc);
  395. }
  396. static void __init at91sam9260_pmc_setup(struct device_node *np)
  397. {
  398. at91sam926x_pmc_setup(np, &at91sam9260_data);
  399. }
  400. CLK_OF_DECLARE_DRIVER(at91sam9260_pmc, "atmel,at91sam9260-pmc",
  401. at91sam9260_pmc_setup);
  402. static void __init at91sam9261_pmc_setup(struct device_node *np)
  403. {
  404. at91sam926x_pmc_setup(np, &at91sam9261_data);
  405. }
  406. CLK_OF_DECLARE_DRIVER(at91sam9261_pmc, "atmel,at91sam9261-pmc",
  407. at91sam9261_pmc_setup);
  408. static void __init at91sam9263_pmc_setup(struct device_node *np)
  409. {
  410. at91sam926x_pmc_setup(np, &at91sam9263_data);
  411. }
  412. CLK_OF_DECLARE_DRIVER(at91sam9263_pmc, "atmel,at91sam9263-pmc",
  413. at91sam9263_pmc_setup);
  414. static void __init at91sam9g20_pmc_setup(struct device_node *np)
  415. {
  416. at91sam926x_pmc_setup(np, &at91sam9g20_data);
  417. }
  418. CLK_OF_DECLARE_DRIVER(at91sam9g20_pmc, "atmel,at91sam9g20-pmc",
  419. at91sam9g20_pmc_setup);