clk-pic32mzda.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. .fixed_rate = 32768,
  112. .init_data = {
  113. .name = "sosc_clk",
  114. .parent_names = NULL,
  115. .ops = &pic32_sosc_ops,
  116. },
  117. };
  118. static int pic32mzda_critical_clks[] = {
  119. PB2CLK, PB7CLK
  120. };
  121. /* PIC32MZDA clock data */
  122. struct pic32mzda_clk_data {
  123. struct clk *clks[MAXCLKS];
  124. struct pic32_clk_common core;
  125. struct clk_onecell_data onecell_data;
  126. struct notifier_block failsafe_notifier;
  127. };
  128. static int pic32_fscm_nmi(struct notifier_block *nb,
  129. unsigned long action, void *data)
  130. {
  131. struct pic32mzda_clk_data *cd;
  132. cd = container_of(nb, struct pic32mzda_clk_data, failsafe_notifier);
  133. /* SYSCLK is now running from BFRCCLK. Report clock failure. */
  134. if (readl(cd->core.iobase) & BIT(2))
  135. pr_alert("pic32-clk: FSCM detected clk failure.\n");
  136. /* TODO: detect reason of failure and recover accordingly */
  137. return NOTIFY_OK;
  138. }
  139. static int pic32mzda_clk_probe(struct platform_device *pdev)
  140. {
  141. const char *const pll_mux_parents[] = {"posc_clk", "frc_clk"};
  142. struct device_node *np = pdev->dev.of_node;
  143. struct pic32mzda_clk_data *cd;
  144. struct pic32_clk_common *core;
  145. struct clk *pll_mux_clk, *clk;
  146. struct clk **clks;
  147. int nr_clks, i, ret;
  148. cd = devm_kzalloc(&pdev->dev, sizeof(*cd), GFP_KERNEL);
  149. if (!cd)
  150. return -ENOMEM;
  151. core = &cd->core;
  152. core->iobase = of_io_request_and_map(np, 0, of_node_full_name(np));
  153. if (IS_ERR(core->iobase)) {
  154. dev_err(&pdev->dev, "pic32-clk: failed to map registers\n");
  155. return PTR_ERR(core->iobase);
  156. }
  157. spin_lock_init(&core->reg_lock);
  158. core->dev = &pdev->dev;
  159. clks = &cd->clks[0];
  160. /* register fixed rate clocks */
  161. clks[POSCCLK] = clk_register_fixed_rate(&pdev->dev, "posc_clk", NULL,
  162. 0, 24000000);
  163. clks[FRCCLK] = clk_register_fixed_rate(&pdev->dev, "frc_clk", NULL,
  164. 0, 8000000);
  165. clks[BFRCCLK] = clk_register_fixed_rate(&pdev->dev, "bfrc_clk", NULL,
  166. 0, 8000000);
  167. clks[LPRCCLK] = clk_register_fixed_rate(&pdev->dev, "lprc_clk", NULL,
  168. 0, 32000);
  169. clks[UPLLCLK] = clk_register_fixed_rate(&pdev->dev, "usbphy_clk", NULL,
  170. 0, 24000000);
  171. /* fixed rate (optional) clock */
  172. if (of_find_property(np, "microchip,pic32mzda-sosc", NULL)) {
  173. pr_info("pic32-clk: dt requests SOSC.\n");
  174. clks[SOSCCLK] = pic32_sosc_clk_register(&sosc_clk, core);
  175. }
  176. /* divider clock */
  177. clks[FRCDIVCLK] = clk_register_divider(&pdev->dev, "frcdiv_clk",
  178. "frc_clk", 0,
  179. core->iobase,
  180. OSC_FRCDIV_SHIFT,
  181. OSC_FRCDIV_MASK,
  182. CLK_DIVIDER_POWER_OF_TWO,
  183. &core->reg_lock);
  184. /* PLL ICLK mux */
  185. pll_mux_clk = clk_register_mux(&pdev->dev, "spll_mux_clk",
  186. pll_mux_parents, 2, 0,
  187. core->iobase + 0x020,
  188. PLL_ICLK_SHIFT, 1, 0, &core->reg_lock);
  189. if (IS_ERR(pll_mux_clk))
  190. pr_err("spll_mux_clk: clk register failed\n");
  191. /* PLL */
  192. clks[PLLCLK] = pic32_spll_clk_register(&sys_pll, core);
  193. /* SYSTEM clock */
  194. clks[SCLK] = pic32_sys_clk_register(&sys_mux_clk, core);
  195. /* Peripheral bus clocks */
  196. for (nr_clks = PB1CLK, i = 0; nr_clks <= PB7CLK; i++, nr_clks++)
  197. clks[nr_clks] = pic32_periph_clk_register(&periph_clocks[i],
  198. core);
  199. /* Reference oscillator clock */
  200. for (nr_clks = REF1CLK, i = 0; nr_clks <= REF5CLK; i++, nr_clks++)
  201. clks[nr_clks] = pic32_refo_clk_register(&ref_clks[i], core);
  202. /* register clkdev */
  203. for (i = 0; i < MAXCLKS; i++) {
  204. if (IS_ERR(clks[i]))
  205. continue;
  206. clk_register_clkdev(clks[i], NULL, __clk_get_name(clks[i]));
  207. }
  208. /* register clock provider */
  209. cd->onecell_data.clks = clks;
  210. cd->onecell_data.clk_num = MAXCLKS;
  211. ret = of_clk_add_provider(np, of_clk_src_onecell_get,
  212. &cd->onecell_data);
  213. if (ret)
  214. return ret;
  215. /* force enable critical clocks */
  216. for (i = 0; i < ARRAY_SIZE(pic32mzda_critical_clks); i++) {
  217. clk = clks[pic32mzda_critical_clks[i]];
  218. if (clk_prepare_enable(clk))
  219. dev_err(&pdev->dev, "clk_prepare_enable(%s) failed\n",
  220. __clk_get_name(clk));
  221. }
  222. /* register NMI for failsafe clock monitor */
  223. cd->failsafe_notifier.notifier_call = pic32_fscm_nmi;
  224. return register_nmi_notifier(&cd->failsafe_notifier);
  225. }
  226. static const struct of_device_id pic32mzda_clk_match_table[] = {
  227. { .compatible = "microchip,pic32mzda-clk", },
  228. { }
  229. };
  230. MODULE_DEVICE_TABLE(of, pic32mzda_clk_match_table);
  231. static struct platform_driver pic32mzda_clk_driver = {
  232. .probe = pic32mzda_clk_probe,
  233. .driver = {
  234. .name = "clk-pic32mzda",
  235. .of_match_table = pic32mzda_clk_match_table,
  236. },
  237. };
  238. static int __init microchip_pic32mzda_clk_init(void)
  239. {
  240. return platform_driver_register(&pic32mzda_clk_driver);
  241. }
  242. core_initcall(microchip_pic32mzda_clk_init);
  243. MODULE_DESCRIPTION("Microchip PIC32MZDA Clock Driver");
  244. MODULE_LICENSE("GPL v2");
  245. MODULE_ALIAS("platform:clk-pic32mzda");