clk-pic32mzda.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Purna Chandra Mandal,<purna.mandal@microchip.com>
  3. * Copyright (C) 2015 Microchip Technology Inc. All rights reserved.
  4. *
  5. * This program is free software; you can distribute it and/or modify it
  6. * under the terms of the GNU General Public License (Version 2) as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #include <dt-bindings/clock/microchip,pic32-clock.h>
  15. #include <linux/clk.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/module.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <asm/traps.h>
  23. #include "clk-core.h"
  24. /* FRC Postscaler */
  25. #define OSC_FRCDIV_MASK 0x07
  26. #define OSC_FRCDIV_SHIFT 24
  27. /* SPLL fields */
  28. #define PLL_ICLK_MASK 0x01
  29. #define PLL_ICLK_SHIFT 7
  30. #define DECLARE_PERIPHERAL_CLOCK(__clk_name, __reg, __flags) \
  31. { \
  32. .ctrl_reg = (__reg), \
  33. .init_data = { \
  34. .name = (__clk_name), \
  35. .parent_names = (const char *[]) { \
  36. "sys_clk" \
  37. }, \
  38. .num_parents = 1, \
  39. .ops = &pic32_pbclk_ops, \
  40. .flags = (__flags), \
  41. }, \
  42. }
  43. #define DECLARE_REFO_CLOCK(__clkid, __reg) \
  44. { \
  45. .ctrl_reg = (__reg), \
  46. .init_data = { \
  47. .name = "refo" #__clkid "_clk", \
  48. .parent_names = (const char *[]) { \
  49. "sys_clk", "pb1_clk", "posc_clk", \
  50. "frc_clk", "lprc_clk", "sosc_clk", \
  51. "sys_pll", "refi" #__clkid "_clk", \
  52. "bfrc_clk", \
  53. }, \
  54. .num_parents = 9, \
  55. .flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE,\
  56. .ops = &pic32_roclk_ops, \
  57. }, \
  58. .parent_map = (const u32[]) { \
  59. 0, 1, 2, 3, 4, 5, 7, 8, 9 \
  60. }, \
  61. }
  62. static const struct pic32_ref_osc_data ref_clks[] = {
  63. DECLARE_REFO_CLOCK(1, 0x80),
  64. DECLARE_REFO_CLOCK(2, 0xa0),
  65. DECLARE_REFO_CLOCK(3, 0xc0),
  66. DECLARE_REFO_CLOCK(4, 0xe0),
  67. DECLARE_REFO_CLOCK(5, 0x100),
  68. };
  69. static const struct pic32_periph_clk_data periph_clocks[] = {
  70. DECLARE_PERIPHERAL_CLOCK("pb1_clk", 0x140, 0),
  71. DECLARE_PERIPHERAL_CLOCK("pb2_clk", 0x150, CLK_IGNORE_UNUSED),
  72. DECLARE_PERIPHERAL_CLOCK("pb3_clk", 0x160, 0),
  73. DECLARE_PERIPHERAL_CLOCK("pb4_clk", 0x170, 0),
  74. DECLARE_PERIPHERAL_CLOCK("pb5_clk", 0x180, 0),
  75. DECLARE_PERIPHERAL_CLOCK("pb6_clk", 0x190, 0),
  76. DECLARE_PERIPHERAL_CLOCK("cpu_clk", 0x1a0, CLK_IGNORE_UNUSED),
  77. };
  78. static const struct pic32_sys_clk_data sys_mux_clk = {
  79. .slew_reg = 0x1c0,
  80. .slew_div = 2, /* step of div_4 -> div_2 -> no_div */
  81. .init_data = {
  82. .name = "sys_clk",
  83. .parent_names = (const char *[]) {
  84. "frcdiv_clk", "sys_pll", "posc_clk",
  85. "sosc_clk", "lprc_clk", "frcdiv_clk",
  86. },
  87. .num_parents = 6,
  88. .ops = &pic32_sclk_ops,
  89. },
  90. .parent_map = (const u32[]) {
  91. 0, 1, 2, 4, 5, 7,
  92. },
  93. };
  94. static const struct pic32_sys_pll_data sys_pll = {
  95. .ctrl_reg = 0x020,
  96. .status_reg = 0x1d0,
  97. .lock_mask = BIT(7),
  98. .init_data = {
  99. .name = "sys_pll",
  100. .parent_names = (const char *[]) {
  101. "spll_mux_clk"
  102. },
  103. .num_parents = 1,
  104. .ops = &pic32_spll_ops,
  105. },
  106. };
  107. static const struct pic32_sec_osc_data sosc_clk = {
  108. .status_reg = 0x1d0,
  109. .enable_mask = BIT(1),
  110. .status_mask = BIT(4),
  111. .init_data = {
  112. .name = "sosc_clk",
  113. .parent_names = NULL,
  114. .ops = &pic32_sosc_ops,
  115. },
  116. };
  117. static int pic32mzda_critical_clks[] = {
  118. PB2CLK, PB7CLK
  119. };
  120. /* PIC32MZDA clock data */
  121. struct pic32mzda_clk_data {
  122. struct clk *clks[MAXCLKS];
  123. struct pic32_clk_common core;
  124. struct clk_onecell_data onecell_data;
  125. struct notifier_block failsafe_notifier;
  126. };
  127. static int pic32_fscm_nmi(struct notifier_block *nb,
  128. unsigned long action, void *data)
  129. {
  130. struct pic32mzda_clk_data *cd;
  131. cd = container_of(nb, struct pic32mzda_clk_data, failsafe_notifier);
  132. /* SYSCLK is now running from BFRCCLK. Report clock failure. */
  133. if (readl(cd->core.iobase) & BIT(2))
  134. pr_alert("pic32-clk: FSCM detected clk failure.\n");
  135. /* TODO: detect reason of failure and recover accordingly */
  136. return NOTIFY_OK;
  137. }
  138. static int pic32mzda_clk_probe(struct platform_device *pdev)
  139. {
  140. const char *const pll_mux_parents[] = {"posc_clk", "frc_clk"};
  141. struct device_node *np = pdev->dev.of_node;
  142. struct pic32mzda_clk_data *cd;
  143. struct pic32_clk_common *core;
  144. struct clk *pll_mux_clk, *clk;
  145. struct clk **clks;
  146. int nr_clks, i, ret;
  147. cd = devm_kzalloc(&pdev->dev, sizeof(*cd), GFP_KERNEL);
  148. if (!cd)
  149. return -ENOMEM;
  150. core = &cd->core;
  151. core->iobase = of_io_request_and_map(np, 0, of_node_full_name(np));
  152. if (IS_ERR(core->iobase)) {
  153. dev_err(&pdev->dev, "pic32-clk: failed to map registers\n");
  154. return PTR_ERR(core->iobase);
  155. }
  156. spin_lock_init(&core->reg_lock);
  157. core->dev = &pdev->dev;
  158. clks = &cd->clks[0];
  159. /* register fixed rate clocks */
  160. clks[POSCCLK] = clk_register_fixed_rate(&pdev->dev, "posc_clk", NULL,
  161. 0, 24000000);
  162. clks[FRCCLK] = clk_register_fixed_rate(&pdev->dev, "frc_clk", NULL,
  163. 0, 8000000);
  164. clks[BFRCCLK] = clk_register_fixed_rate(&pdev->dev, "bfrc_clk", NULL,
  165. 0, 8000000);
  166. clks[LPRCCLK] = clk_register_fixed_rate(&pdev->dev, "lprc_clk", NULL,
  167. 0, 32000);
  168. clks[UPLLCLK] = clk_register_fixed_rate(&pdev->dev, "usbphy_clk", NULL,
  169. 0, 24000000);
  170. /* fixed rate (optional) clock */
  171. if (of_find_property(np, "microchip,pic32mzda-sosc", NULL)) {
  172. pr_info("pic32-clk: dt requests SOSC.\n");
  173. clks[SOSCCLK] = pic32_sosc_clk_register(&sosc_clk, core);
  174. }
  175. /* divider clock */
  176. clks[FRCDIVCLK] = clk_register_divider(&pdev->dev, "frcdiv_clk",
  177. "frc_clk", 0,
  178. core->iobase,
  179. OSC_FRCDIV_SHIFT,
  180. OSC_FRCDIV_MASK,
  181. CLK_DIVIDER_POWER_OF_TWO,
  182. &core->reg_lock);
  183. /* PLL ICLK mux */
  184. pll_mux_clk = clk_register_mux(&pdev->dev, "spll_mux_clk",
  185. pll_mux_parents, 2, 0,
  186. core->iobase + 0x020,
  187. PLL_ICLK_SHIFT, 1, 0, &core->reg_lock);
  188. if (IS_ERR(pll_mux_clk))
  189. pr_err("spll_mux_clk: clk register failed\n");
  190. /* PLL */
  191. clks[PLLCLK] = pic32_spll_clk_register(&sys_pll, core);
  192. /* SYSTEM clock */
  193. clks[SCLK] = pic32_sys_clk_register(&sys_mux_clk, core);
  194. /* Peripheral bus clocks */
  195. for (nr_clks = PB1CLK, i = 0; nr_clks <= PB7CLK; i++, nr_clks++)
  196. clks[nr_clks] = pic32_periph_clk_register(&periph_clocks[i],
  197. core);
  198. /* Reference oscillator clock */
  199. for (nr_clks = REF1CLK, i = 0; nr_clks <= REF5CLK; i++, nr_clks++)
  200. clks[nr_clks] = pic32_refo_clk_register(&ref_clks[i], core);
  201. /* register clkdev */
  202. for (i = 0; i < MAXCLKS; i++) {
  203. if (IS_ERR(clks[i]))
  204. continue;
  205. clk_register_clkdev(clks[i], NULL, __clk_get_name(clks[i]));
  206. }
  207. /* register clock provider */
  208. cd->onecell_data.clks = clks;
  209. cd->onecell_data.clk_num = MAXCLKS;
  210. ret = of_clk_add_provider(np, of_clk_src_onecell_get,
  211. &cd->onecell_data);
  212. if (ret)
  213. return ret;
  214. /* force enable critical clocks */
  215. for (i = 0; i < ARRAY_SIZE(pic32mzda_critical_clks); i++) {
  216. clk = clks[pic32mzda_critical_clks[i]];
  217. if (clk_prepare_enable(clk))
  218. dev_err(&pdev->dev, "clk_prepare_enable(%s) failed\n",
  219. __clk_get_name(clk));
  220. }
  221. /* register NMI for failsafe clock monitor */
  222. cd->failsafe_notifier.notifier_call = pic32_fscm_nmi;
  223. return register_nmi_notifier(&cd->failsafe_notifier);
  224. }
  225. static const struct of_device_id pic32mzda_clk_match_table[] = {
  226. { .compatible = "microchip,pic32mzda-clk", },
  227. { }
  228. };
  229. MODULE_DEVICE_TABLE(of, pic32mzda_clk_match_table);
  230. static struct platform_driver pic32mzda_clk_driver = {
  231. .probe = pic32mzda_clk_probe,
  232. .driver = {
  233. .name = "clk-pic32mzda",
  234. .of_match_table = pic32mzda_clk_match_table,
  235. },
  236. };
  237. static int __init microchip_pic32mzda_clk_init(void)
  238. {
  239. return platform_driver_register(&pic32mzda_clk_driver);
  240. }
  241. core_initcall(microchip_pic32mzda_clk_init);
  242. MODULE_DESCRIPTION("Microchip PIC32MZDA Clock Driver");
  243. MODULE_LICENSE("GPL v2");
  244. MODULE_ALIAS("platform:clk-pic32mzda");