at91sam9n12.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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(at91sam9n12_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 u8 pllb_out[] = { 0 };
  33. static const struct clk_range pllb_outputs[] = {
  34. { .min = 30000000, .max = 100000000 },
  35. };
  36. static const struct clk_pll_characteristics pllb_characteristics = {
  37. .input = { .min = 2000000, .max = 32000000 },
  38. .num_output = ARRAY_SIZE(pllb_outputs),
  39. .output = pllb_outputs,
  40. .out = pllb_out,
  41. };
  42. static const struct {
  43. char *n;
  44. char *p;
  45. u8 id;
  46. } at91sam9n12_systemck[] = {
  47. { .n = "ddrck", .p = "masterck_div", .id = 2 },
  48. { .n = "lcdck", .p = "masterck_div", .id = 3 },
  49. { .n = "uhpck", .p = "usbck", .id = 6 },
  50. { .n = "udpck", .p = "usbck", .id = 7 },
  51. { .n = "pck0", .p = "prog0", .id = 8 },
  52. { .n = "pck1", .p = "prog1", .id = 9 },
  53. };
  54. static const struct clk_pcr_layout at91sam9n12_pcr_layout = {
  55. .offset = 0x10c,
  56. .cmd = BIT(12),
  57. .pid_mask = GENMASK(5, 0),
  58. .div_mask = GENMASK(17, 16),
  59. };
  60. struct pck {
  61. char *n;
  62. u8 id;
  63. };
  64. static const struct pck at91sam9n12_periphck[] = {
  65. { .n = "pioAB_clk", .id = 2, },
  66. { .n = "pioCD_clk", .id = 3, },
  67. { .n = "fuse_clk", .id = 4, },
  68. { .n = "usart0_clk", .id = 5, },
  69. { .n = "usart1_clk", .id = 6, },
  70. { .n = "usart2_clk", .id = 7, },
  71. { .n = "usart3_clk", .id = 8, },
  72. { .n = "twi0_clk", .id = 9, },
  73. { .n = "twi1_clk", .id = 10, },
  74. { .n = "mci0_clk", .id = 12, },
  75. { .n = "spi0_clk", .id = 13, },
  76. { .n = "spi1_clk", .id = 14, },
  77. { .n = "uart0_clk", .id = 15, },
  78. { .n = "uart1_clk", .id = 16, },
  79. { .n = "tcb_clk", .id = 17, },
  80. { .n = "pwm_clk", .id = 18, },
  81. { .n = "adc_clk", .id = 19, },
  82. { .n = "dma0_clk", .id = 20, },
  83. { .n = "uhphs_clk", .id = 22, },
  84. { .n = "udphs_clk", .id = 23, },
  85. { .n = "lcdc_clk", .id = 25, },
  86. { .n = "sha_clk", .id = 27, },
  87. { .n = "ssc0_clk", .id = 28, },
  88. { .n = "aes_clk", .id = 29, },
  89. { .n = "trng_clk", .id = 30, },
  90. };
  91. static void __init at91sam9n12_pmc_setup(struct device_node *np)
  92. {
  93. struct clk_range range = CLK_RANGE(0, 0);
  94. const char *slck_name, *mainxtal_name;
  95. struct pmc_data *at91sam9n12_pmc;
  96. const char *parent_names[6];
  97. struct regmap *regmap;
  98. struct clk_hw *hw;
  99. int i;
  100. bool bypass;
  101. i = of_property_match_string(np, "clock-names", "slow_clk");
  102. if (i < 0)
  103. return;
  104. slck_name = of_clk_get_parent_name(np, i);
  105. i = of_property_match_string(np, "clock-names", "main_xtal");
  106. if (i < 0)
  107. return;
  108. mainxtal_name = of_clk_get_parent_name(np, i);
  109. regmap = device_node_to_regmap(np);
  110. if (IS_ERR(regmap))
  111. return;
  112. at91sam9n12_pmc = pmc_data_allocate(PMC_PLLBCK + 1,
  113. nck(at91sam9n12_systemck), 31, 0, 2);
  114. if (!at91sam9n12_pmc)
  115. return;
  116. hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000,
  117. 50000000);
  118. if (IS_ERR(hw))
  119. goto err_free;
  120. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  121. hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name,
  122. bypass);
  123. if (IS_ERR(hw))
  124. goto err_free;
  125. parent_names[0] = "main_rc_osc";
  126. parent_names[1] = "main_osc";
  127. hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, 2);
  128. if (IS_ERR(hw))
  129. goto err_free;
  130. at91sam9n12_pmc->chws[PMC_MAIN] = hw;
  131. hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
  132. &at91rm9200_pll_layout, &plla_characteristics);
  133. if (IS_ERR(hw))
  134. goto err_free;
  135. hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
  136. if (IS_ERR(hw))
  137. goto err_free;
  138. at91sam9n12_pmc->chws[PMC_PLLACK] = hw;
  139. hw = at91_clk_register_pll(regmap, "pllbck", "mainck", 1,
  140. &at91rm9200_pll_layout, &pllb_characteristics);
  141. if (IS_ERR(hw))
  142. goto err_free;
  143. at91sam9n12_pmc->chws[PMC_PLLBCK] = hw;
  144. parent_names[0] = slck_name;
  145. parent_names[1] = "mainck";
  146. parent_names[2] = "plladivck";
  147. parent_names[3] = "pllbck";
  148. hw = at91_clk_register_master_pres(regmap, "masterck_pres", 4,
  149. parent_names,
  150. &at91sam9x5_master_layout,
  151. &mck_characteristics,
  152. &at91sam9n12_mck_lock);
  153. if (IS_ERR(hw))
  154. goto err_free;
  155. hw = at91_clk_register_master_div(regmap, "masterck_div",
  156. "masterck_pres",
  157. &at91sam9x5_master_layout,
  158. &mck_characteristics,
  159. &at91sam9n12_mck_lock,
  160. CLK_SET_RATE_GATE, 0);
  161. if (IS_ERR(hw))
  162. goto err_free;
  163. at91sam9n12_pmc->chws[PMC_MCK] = hw;
  164. hw = at91sam9n12_clk_register_usb(regmap, "usbck", "pllbck");
  165. if (IS_ERR(hw))
  166. goto err_free;
  167. parent_names[0] = slck_name;
  168. parent_names[1] = "mainck";
  169. parent_names[2] = "plladivck";
  170. parent_names[3] = "pllbck";
  171. parent_names[4] = "masterck_div";
  172. for (i = 0; i < 2; i++) {
  173. char name[6];
  174. snprintf(name, sizeof(name), "prog%d", i);
  175. hw = at91_clk_register_programmable(regmap, name,
  176. parent_names, 5, i,
  177. &at91sam9x5_programmable_layout,
  178. NULL);
  179. if (IS_ERR(hw))
  180. goto err_free;
  181. at91sam9n12_pmc->pchws[i] = hw;
  182. }
  183. for (i = 0; i < ARRAY_SIZE(at91sam9n12_systemck); i++) {
  184. hw = at91_clk_register_system(regmap, at91sam9n12_systemck[i].n,
  185. at91sam9n12_systemck[i].p,
  186. at91sam9n12_systemck[i].id);
  187. if (IS_ERR(hw))
  188. goto err_free;
  189. at91sam9n12_pmc->shws[at91sam9n12_systemck[i].id] = hw;
  190. }
  191. for (i = 0; i < ARRAY_SIZE(at91sam9n12_periphck); i++) {
  192. hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
  193. &at91sam9n12_pcr_layout,
  194. at91sam9n12_periphck[i].n,
  195. "masterck_div",
  196. at91sam9n12_periphck[i].id,
  197. &range, INT_MIN);
  198. if (IS_ERR(hw))
  199. goto err_free;
  200. at91sam9n12_pmc->phws[at91sam9n12_periphck[i].id] = hw;
  201. }
  202. of_clk_add_hw_provider(np, of_clk_hw_pmc_get, at91sam9n12_pmc);
  203. return;
  204. err_free:
  205. kfree(at91sam9n12_pmc);
  206. }
  207. /*
  208. * The TCB is used as the clocksource so its clock is needed early. This means
  209. * this can't be a platform driver.
  210. */
  211. CLK_OF_DECLARE(at91sam9n12_pmc, "atmel,at91sam9n12-pmc", at91sam9n12_pmc_setup);