at91sam9g45.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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(at91sam9g45_mck_lock);
  8. static const struct clk_master_characteristics mck_characteristics = {
  9. .output = { .min = 0, .max = 133333333 },
  10. .divisors = { 1, 2, 4, 3 },
  11. };
  12. static u8 plla_out[] = { 0, 1, 2, 3, 0, 1, 2, 3 };
  13. static u16 plla_icpll[] = { 0, 0, 0, 0, 1, 1, 1, 1 };
  14. static const struct clk_range plla_outputs[] = {
  15. { .min = 745000000, .max = 800000000 },
  16. { .min = 695000000, .max = 750000000 },
  17. { .min = 645000000, .max = 700000000 },
  18. { .min = 595000000, .max = 650000000 },
  19. { .min = 545000000, .max = 600000000 },
  20. { .min = 495000000, .max = 555000000 },
  21. { .min = 445000000, .max = 500000000 },
  22. { .min = 400000000, .max = 450000000 },
  23. };
  24. static const struct clk_pll_characteristics plla_characteristics = {
  25. .input = { .min = 2000000, .max = 32000000 },
  26. .num_output = ARRAY_SIZE(plla_outputs),
  27. .output = plla_outputs,
  28. .icpll = plla_icpll,
  29. .out = plla_out,
  30. };
  31. static const struct {
  32. char *n;
  33. char *p;
  34. unsigned long flags;
  35. u8 id;
  36. } at91sam9g45_systemck[] = {
  37. /*
  38. * ddrck feeds DDR controller and is enabled by bootloader thus we need
  39. * to keep it enabled in case there is no Linux consumer for it.
  40. */
  41. { .n = "ddrck", .p = "masterck_div", .id = 2, .flags = CLK_IS_CRITICAL },
  42. { .n = "uhpck", .p = "usbck", .id = 6 },
  43. { .n = "pck0", .p = "prog0", .id = 8 },
  44. { .n = "pck1", .p = "prog1", .id = 9 },
  45. };
  46. struct pck {
  47. char *n;
  48. u8 id;
  49. };
  50. static const struct pck at91sam9g45_periphck[] = {
  51. { .n = "pioA_clk", .id = 2, },
  52. { .n = "pioB_clk", .id = 3, },
  53. { .n = "pioC_clk", .id = 4, },
  54. { .n = "pioDE_clk", .id = 5, },
  55. { .n = "trng_clk", .id = 6, },
  56. { .n = "usart0_clk", .id = 7, },
  57. { .n = "usart1_clk", .id = 8, },
  58. { .n = "usart2_clk", .id = 9, },
  59. { .n = "usart3_clk", .id = 10, },
  60. { .n = "mci0_clk", .id = 11, },
  61. { .n = "twi0_clk", .id = 12, },
  62. { .n = "twi1_clk", .id = 13, },
  63. { .n = "spi0_clk", .id = 14, },
  64. { .n = "spi1_clk", .id = 15, },
  65. { .n = "ssc0_clk", .id = 16, },
  66. { .n = "ssc1_clk", .id = 17, },
  67. { .n = "tcb0_clk", .id = 18, },
  68. { .n = "pwm_clk", .id = 19, },
  69. { .n = "adc_clk", .id = 20, },
  70. { .n = "dma0_clk", .id = 21, },
  71. { .n = "uhphs_clk", .id = 22, },
  72. { .n = "lcd_clk", .id = 23, },
  73. { .n = "ac97_clk", .id = 24, },
  74. { .n = "macb0_clk", .id = 25, },
  75. { .n = "isi_clk", .id = 26, },
  76. { .n = "udphs_clk", .id = 27, },
  77. { .n = "aestdessha_clk", .id = 28, },
  78. { .n = "mci1_clk", .id = 29, },
  79. { .n = "vdec_clk", .id = 30, },
  80. };
  81. static void __init at91sam9g45_pmc_setup(struct device_node *np)
  82. {
  83. const char *slck_name, *mainxtal_name;
  84. struct pmc_data *at91sam9g45_pmc;
  85. const char *parent_names[6];
  86. struct regmap *regmap;
  87. struct clk_hw *hw;
  88. int i;
  89. bool bypass;
  90. i = of_property_match_string(np, "clock-names", "slow_clk");
  91. if (i < 0)
  92. return;
  93. slck_name = of_clk_get_parent_name(np, i);
  94. i = of_property_match_string(np, "clock-names", "main_xtal");
  95. if (i < 0)
  96. return;
  97. mainxtal_name = of_clk_get_parent_name(np, i);
  98. regmap = device_node_to_regmap(np);
  99. if (IS_ERR(regmap))
  100. return;
  101. at91sam9g45_pmc = pmc_data_allocate(PMC_PLLACK + 1,
  102. nck(at91sam9g45_systemck),
  103. nck(at91sam9g45_periphck), 0, 2);
  104. if (!at91sam9g45_pmc)
  105. return;
  106. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  107. hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name, NULL,
  108. bypass);
  109. if (IS_ERR(hw))
  110. goto err_free;
  111. hw = at91_clk_register_rm9200_main(regmap, "mainck", "main_osc", NULL);
  112. if (IS_ERR(hw))
  113. goto err_free;
  114. at91sam9g45_pmc->chws[PMC_MAIN] = hw;
  115. hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
  116. &at91rm9200_pll_layout, &plla_characteristics);
  117. if (IS_ERR(hw))
  118. goto err_free;
  119. hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
  120. if (IS_ERR(hw))
  121. goto err_free;
  122. at91sam9g45_pmc->chws[PMC_PLLACK] = hw;
  123. hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck", NULL);
  124. if (IS_ERR(hw))
  125. goto err_free;
  126. at91sam9g45_pmc->chws[PMC_UTMI] = hw;
  127. parent_names[0] = slck_name;
  128. parent_names[1] = "mainck";
  129. parent_names[2] = "plladivck";
  130. parent_names[3] = "utmick";
  131. hw = at91_clk_register_master_pres(regmap, "masterck_pres", 4,
  132. parent_names, NULL,
  133. &at91rm9200_master_layout,
  134. &mck_characteristics,
  135. &at91sam9g45_mck_lock);
  136. if (IS_ERR(hw))
  137. goto err_free;
  138. hw = at91_clk_register_master_div(regmap, "masterck_div",
  139. "masterck_pres", NULL,
  140. &at91rm9200_master_layout,
  141. &mck_characteristics,
  142. &at91sam9g45_mck_lock,
  143. CLK_SET_RATE_GATE, 0);
  144. if (IS_ERR(hw))
  145. goto err_free;
  146. at91sam9g45_pmc->chws[PMC_MCK] = hw;
  147. parent_names[0] = "plladivck";
  148. parent_names[1] = "utmick";
  149. hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2);
  150. if (IS_ERR(hw))
  151. goto err_free;
  152. parent_names[0] = slck_name;
  153. parent_names[1] = "mainck";
  154. parent_names[2] = "plladivck";
  155. parent_names[3] = "utmick";
  156. parent_names[4] = "masterck_div";
  157. for (i = 0; i < 2; i++) {
  158. char name[6];
  159. snprintf(name, sizeof(name), "prog%d", i);
  160. hw = at91_clk_register_programmable(regmap, name,
  161. parent_names, NULL, 5, i,
  162. &at91sam9g45_programmable_layout,
  163. NULL);
  164. if (IS_ERR(hw))
  165. goto err_free;
  166. at91sam9g45_pmc->pchws[i] = hw;
  167. }
  168. for (i = 0; i < ARRAY_SIZE(at91sam9g45_systemck); i++) {
  169. hw = at91_clk_register_system(regmap, at91sam9g45_systemck[i].n,
  170. at91sam9g45_systemck[i].p, NULL,
  171. at91sam9g45_systemck[i].id,
  172. at91sam9g45_systemck[i].flags);
  173. if (IS_ERR(hw))
  174. goto err_free;
  175. at91sam9g45_pmc->shws[at91sam9g45_systemck[i].id] = hw;
  176. }
  177. for (i = 0; i < ARRAY_SIZE(at91sam9g45_periphck); i++) {
  178. hw = at91_clk_register_peripheral(regmap,
  179. at91sam9g45_periphck[i].n,
  180. "masterck_div", NULL,
  181. at91sam9g45_periphck[i].id);
  182. if (IS_ERR(hw))
  183. goto err_free;
  184. at91sam9g45_pmc->phws[at91sam9g45_periphck[i].id] = hw;
  185. }
  186. of_clk_add_hw_provider(np, of_clk_hw_pmc_get, at91sam9g45_pmc);
  187. return;
  188. err_free:
  189. kfree(at91sam9g45_pmc);
  190. }
  191. /*
  192. * The TCB is used as the clocksource so its clock is needed early. This means
  193. * this can't be a platform driver.
  194. */
  195. CLK_OF_DECLARE(at91sam9g45_pmc, "atmel,at91sam9g45-pmc", at91sam9g45_pmc_setup);