at91sam9260.c 14 KB

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