at91rm9200.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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(rm9200_mck_lock);
  8. struct sck {
  9. char *n;
  10. char *p;
  11. u8 id;
  12. };
  13. struct pck {
  14. char *n;
  15. u8 id;
  16. };
  17. static const struct clk_master_characteristics rm9200_mck_characteristics = {
  18. .output = { .min = 0, .max = 80000000 },
  19. .divisors = { 1, 2, 3, 4 },
  20. };
  21. static u8 rm9200_pll_out[] = { 0, 2 };
  22. static const struct clk_range rm9200_pll_outputs[] = {
  23. { .min = 80000000, .max = 160000000 },
  24. { .min = 150000000, .max = 180000000 },
  25. };
  26. static const struct clk_pll_characteristics rm9200_pll_characteristics = {
  27. .input = { .min = 1000000, .max = 32000000 },
  28. .num_output = ARRAY_SIZE(rm9200_pll_outputs),
  29. .output = rm9200_pll_outputs,
  30. .out = rm9200_pll_out,
  31. };
  32. static const struct sck at91rm9200_systemck[] = {
  33. { .n = "udpck", .p = "usbck", .id = 1 },
  34. { .n = "uhpck", .p = "usbck", .id = 4 },
  35. { .n = "pck0", .p = "prog0", .id = 8 },
  36. { .n = "pck1", .p = "prog1", .id = 9 },
  37. { .n = "pck2", .p = "prog2", .id = 10 },
  38. { .n = "pck3", .p = "prog3", .id = 11 },
  39. };
  40. static const struct pck at91rm9200_periphck[] = {
  41. { .n = "pioA_clk", .id = 2 },
  42. { .n = "pioB_clk", .id = 3 },
  43. { .n = "pioC_clk", .id = 4 },
  44. { .n = "pioD_clk", .id = 5 },
  45. { .n = "usart0_clk", .id = 6 },
  46. { .n = "usart1_clk", .id = 7 },
  47. { .n = "usart2_clk", .id = 8 },
  48. { .n = "usart3_clk", .id = 9 },
  49. { .n = "mci0_clk", .id = 10 },
  50. { .n = "udc_clk", .id = 11 },
  51. { .n = "twi0_clk", .id = 12 },
  52. { .n = "spi0_clk", .id = 13 },
  53. { .n = "ssc0_clk", .id = 14 },
  54. { .n = "ssc1_clk", .id = 15 },
  55. { .n = "ssc2_clk", .id = 16 },
  56. { .n = "tc0_clk", .id = 17 },
  57. { .n = "tc1_clk", .id = 18 },
  58. { .n = "tc2_clk", .id = 19 },
  59. { .n = "tc3_clk", .id = 20 },
  60. { .n = "tc4_clk", .id = 21 },
  61. { .n = "tc5_clk", .id = 22 },
  62. { .n = "ohci_clk", .id = 23 },
  63. { .n = "macb0_clk", .id = 24 },
  64. };
  65. static void __init at91rm9200_pmc_setup(struct device_node *np)
  66. {
  67. const char *slowxtal_name, *mainxtal_name;
  68. struct pmc_data *at91rm9200_pmc;
  69. u32 usb_div[] = { 1, 2, 0, 0 };
  70. const char *parent_names[6];
  71. struct regmap *regmap;
  72. struct clk_hw *hw;
  73. int i;
  74. bool bypass;
  75. i = of_property_match_string(np, "clock-names", "slow_xtal");
  76. if (i < 0)
  77. return;
  78. slowxtal_name = of_clk_get_parent_name(np, i);
  79. i = of_property_match_string(np, "clock-names", "main_xtal");
  80. if (i < 0)
  81. return;
  82. mainxtal_name = of_clk_get_parent_name(np, i);
  83. regmap = device_node_to_regmap(np);
  84. if (IS_ERR(regmap))
  85. return;
  86. at91rm9200_pmc = pmc_data_allocate(PMC_PLLBCK + 1,
  87. nck(at91rm9200_systemck),
  88. nck(at91rm9200_periphck), 0, 4);
  89. if (!at91rm9200_pmc)
  90. return;
  91. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  92. hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name,
  93. bypass);
  94. if (IS_ERR(hw))
  95. goto err_free;
  96. hw = at91_clk_register_rm9200_main(regmap, "mainck", "main_osc");
  97. if (IS_ERR(hw))
  98. goto err_free;
  99. at91rm9200_pmc->chws[PMC_MAIN] = hw;
  100. hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
  101. &at91rm9200_pll_layout,
  102. &rm9200_pll_characteristics);
  103. if (IS_ERR(hw))
  104. goto err_free;
  105. at91rm9200_pmc->chws[PMC_PLLACK] = hw;
  106. hw = at91_clk_register_pll(regmap, "pllbck", "mainck", 1,
  107. &at91rm9200_pll_layout,
  108. &rm9200_pll_characteristics);
  109. if (IS_ERR(hw))
  110. goto err_free;
  111. at91rm9200_pmc->chws[PMC_PLLBCK] = hw;
  112. parent_names[0] = slowxtal_name;
  113. parent_names[1] = "mainck";
  114. parent_names[2] = "pllack";
  115. parent_names[3] = "pllbck";
  116. hw = at91_clk_register_master_pres(regmap, "masterck_pres", 4,
  117. parent_names,
  118. &at91rm9200_master_layout,
  119. &rm9200_mck_characteristics,
  120. &rm9200_mck_lock);
  121. if (IS_ERR(hw))
  122. goto err_free;
  123. hw = at91_clk_register_master_div(regmap, "masterck_div",
  124. "masterck_pres",
  125. &at91rm9200_master_layout,
  126. &rm9200_mck_characteristics,
  127. &rm9200_mck_lock, CLK_SET_RATE_GATE, 0);
  128. if (IS_ERR(hw))
  129. goto err_free;
  130. at91rm9200_pmc->chws[PMC_MCK] = hw;
  131. hw = at91rm9200_clk_register_usb(regmap, "usbck", "pllbck", usb_div);
  132. if (IS_ERR(hw))
  133. goto err_free;
  134. parent_names[0] = slowxtal_name;
  135. parent_names[1] = "mainck";
  136. parent_names[2] = "pllack";
  137. parent_names[3] = "pllbck";
  138. for (i = 0; i < 4; i++) {
  139. char name[6];
  140. snprintf(name, sizeof(name), "prog%d", i);
  141. hw = at91_clk_register_programmable(regmap, name,
  142. parent_names, 4, i,
  143. &at91rm9200_programmable_layout,
  144. NULL);
  145. if (IS_ERR(hw))
  146. goto err_free;
  147. at91rm9200_pmc->pchws[i] = hw;
  148. }
  149. for (i = 0; i < ARRAY_SIZE(at91rm9200_systemck); i++) {
  150. hw = at91_clk_register_system(regmap, at91rm9200_systemck[i].n,
  151. at91rm9200_systemck[i].p,
  152. at91rm9200_systemck[i].id);
  153. if (IS_ERR(hw))
  154. goto err_free;
  155. at91rm9200_pmc->shws[at91rm9200_systemck[i].id] = hw;
  156. }
  157. for (i = 0; i < ARRAY_SIZE(at91rm9200_periphck); i++) {
  158. hw = at91_clk_register_peripheral(regmap,
  159. at91rm9200_periphck[i].n,
  160. "masterck_div",
  161. at91rm9200_periphck[i].id);
  162. if (IS_ERR(hw))
  163. goto err_free;
  164. at91rm9200_pmc->phws[at91rm9200_periphck[i].id] = hw;
  165. }
  166. of_clk_add_hw_provider(np, of_clk_hw_pmc_get, at91rm9200_pmc);
  167. return;
  168. err_free:
  169. kfree(at91rm9200_pmc);
  170. }
  171. /*
  172. * While the TCB can be used as the clocksource, the system timer is most likely
  173. * to be used instead. However, the pinctrl driver doesn't support probe
  174. * deferring properly. Once this is fixed, this can be switched to a platform
  175. * driver.
  176. */
  177. CLK_OF_DECLARE(at91rm9200_pmc, "atmel,at91rm9200-pmc", at91rm9200_pmc_setup);