reset-lpc18xx.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Reset driver for NXP LPC18xx/43xx Reset Generation Unit (RGU).
  3. *
  4. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/init.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/reboot.h>
  19. #include <linux/reset-controller.h>
  20. #include <linux/spinlock.h>
  21. /* LPC18xx RGU registers */
  22. #define LPC18XX_RGU_CTRL0 0x100
  23. #define LPC18XX_RGU_CTRL1 0x104
  24. #define LPC18XX_RGU_ACTIVE_STATUS0 0x150
  25. #define LPC18XX_RGU_ACTIVE_STATUS1 0x154
  26. #define LPC18XX_RGU_RESETS_PER_REG 32
  27. /* Internal reset outputs */
  28. #define LPC18XX_RGU_CORE_RST 0
  29. #define LPC43XX_RGU_M0SUB_RST 12
  30. #define LPC43XX_RGU_M0APP_RST 56
  31. struct lpc18xx_rgu_data {
  32. struct reset_controller_dev rcdev;
  33. struct notifier_block restart_nb;
  34. struct clk *clk_delay;
  35. struct clk *clk_reg;
  36. void __iomem *base;
  37. spinlock_t lock;
  38. u32 delay_us;
  39. };
  40. #define to_rgu_data(p) container_of(p, struct lpc18xx_rgu_data, rcdev)
  41. static int lpc18xx_rgu_restart(struct notifier_block *nb, unsigned long mode,
  42. void *cmd)
  43. {
  44. struct lpc18xx_rgu_data *rc = container_of(nb, struct lpc18xx_rgu_data,
  45. restart_nb);
  46. writel(BIT(LPC18XX_RGU_CORE_RST), rc->base + LPC18XX_RGU_CTRL0);
  47. mdelay(2000);
  48. pr_emerg("%s: unable to restart system\n", __func__);
  49. return NOTIFY_DONE;
  50. }
  51. /*
  52. * The LPC18xx RGU has mostly self-deasserting resets except for the
  53. * two reset lines going to the internal Cortex-M0 cores.
  54. *
  55. * To prevent the M0 core resets from accidentally getting deasserted
  56. * status register must be check and bits in control register set to
  57. * preserve the state.
  58. */
  59. static int lpc18xx_rgu_setclear_reset(struct reset_controller_dev *rcdev,
  60. unsigned long id, bool set)
  61. {
  62. struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
  63. u32 stat_offset = LPC18XX_RGU_ACTIVE_STATUS0;
  64. u32 ctrl_offset = LPC18XX_RGU_CTRL0;
  65. unsigned long flags;
  66. u32 stat, rst_bit;
  67. stat_offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
  68. ctrl_offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
  69. rst_bit = 1 << (id % LPC18XX_RGU_RESETS_PER_REG);
  70. spin_lock_irqsave(&rc->lock, flags);
  71. stat = ~readl(rc->base + stat_offset);
  72. if (set)
  73. writel(stat | rst_bit, rc->base + ctrl_offset);
  74. else
  75. writel(stat & ~rst_bit, rc->base + ctrl_offset);
  76. spin_unlock_irqrestore(&rc->lock, flags);
  77. return 0;
  78. }
  79. static int lpc18xx_rgu_assert(struct reset_controller_dev *rcdev,
  80. unsigned long id)
  81. {
  82. return lpc18xx_rgu_setclear_reset(rcdev, id, true);
  83. }
  84. static int lpc18xx_rgu_deassert(struct reset_controller_dev *rcdev,
  85. unsigned long id)
  86. {
  87. return lpc18xx_rgu_setclear_reset(rcdev, id, false);
  88. }
  89. /* Only M0 cores require explicit reset deassert */
  90. static int lpc18xx_rgu_reset(struct reset_controller_dev *rcdev,
  91. unsigned long id)
  92. {
  93. struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
  94. lpc18xx_rgu_assert(rcdev, id);
  95. udelay(rc->delay_us);
  96. switch (id) {
  97. case LPC43XX_RGU_M0SUB_RST:
  98. case LPC43XX_RGU_M0APP_RST:
  99. lpc18xx_rgu_setclear_reset(rcdev, id, false);
  100. }
  101. return 0;
  102. }
  103. static int lpc18xx_rgu_status(struct reset_controller_dev *rcdev,
  104. unsigned long id)
  105. {
  106. struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
  107. u32 bit, offset = LPC18XX_RGU_ACTIVE_STATUS0;
  108. offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
  109. bit = 1 << (id % LPC18XX_RGU_RESETS_PER_REG);
  110. return !(readl(rc->base + offset) & bit);
  111. }
  112. static const struct reset_control_ops lpc18xx_rgu_ops = {
  113. .reset = lpc18xx_rgu_reset,
  114. .assert = lpc18xx_rgu_assert,
  115. .deassert = lpc18xx_rgu_deassert,
  116. .status = lpc18xx_rgu_status,
  117. };
  118. static int lpc18xx_rgu_probe(struct platform_device *pdev)
  119. {
  120. struct lpc18xx_rgu_data *rc;
  121. struct resource *res;
  122. u32 fcclk, firc;
  123. int ret;
  124. rc = devm_kzalloc(&pdev->dev, sizeof(*rc), GFP_KERNEL);
  125. if (!rc)
  126. return -ENOMEM;
  127. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  128. rc->base = devm_ioremap_resource(&pdev->dev, res);
  129. if (IS_ERR(rc->base))
  130. return PTR_ERR(rc->base);
  131. rc->clk_reg = devm_clk_get(&pdev->dev, "reg");
  132. if (IS_ERR(rc->clk_reg)) {
  133. dev_err(&pdev->dev, "reg clock not found\n");
  134. return PTR_ERR(rc->clk_reg);
  135. }
  136. rc->clk_delay = devm_clk_get(&pdev->dev, "delay");
  137. if (IS_ERR(rc->clk_delay)) {
  138. dev_err(&pdev->dev, "delay clock not found\n");
  139. return PTR_ERR(rc->clk_delay);
  140. }
  141. ret = clk_prepare_enable(rc->clk_reg);
  142. if (ret) {
  143. dev_err(&pdev->dev, "unable to enable reg clock\n");
  144. return ret;
  145. }
  146. ret = clk_prepare_enable(rc->clk_delay);
  147. if (ret) {
  148. dev_err(&pdev->dev, "unable to enable delay clock\n");
  149. goto dis_clk_reg;
  150. }
  151. fcclk = clk_get_rate(rc->clk_reg) / USEC_PER_SEC;
  152. firc = clk_get_rate(rc->clk_delay) / USEC_PER_SEC;
  153. if (fcclk == 0 || firc == 0)
  154. rc->delay_us = 2;
  155. else
  156. rc->delay_us = DIV_ROUND_UP(fcclk, firc * firc);
  157. spin_lock_init(&rc->lock);
  158. rc->rcdev.owner = THIS_MODULE;
  159. rc->rcdev.nr_resets = 64;
  160. rc->rcdev.ops = &lpc18xx_rgu_ops;
  161. rc->rcdev.of_node = pdev->dev.of_node;
  162. platform_set_drvdata(pdev, rc);
  163. ret = reset_controller_register(&rc->rcdev);
  164. if (ret) {
  165. dev_err(&pdev->dev, "unable to register device\n");
  166. goto dis_clks;
  167. }
  168. rc->restart_nb.priority = 192,
  169. rc->restart_nb.notifier_call = lpc18xx_rgu_restart,
  170. ret = register_restart_handler(&rc->restart_nb);
  171. if (ret)
  172. dev_warn(&pdev->dev, "failed to register restart handler\n");
  173. return 0;
  174. dis_clks:
  175. clk_disable_unprepare(rc->clk_delay);
  176. dis_clk_reg:
  177. clk_disable_unprepare(rc->clk_reg);
  178. return ret;
  179. }
  180. static const struct of_device_id lpc18xx_rgu_match[] = {
  181. { .compatible = "nxp,lpc1850-rgu" },
  182. { }
  183. };
  184. static struct platform_driver lpc18xx_rgu_driver = {
  185. .probe = lpc18xx_rgu_probe,
  186. .driver = {
  187. .name = "lpc18xx-reset",
  188. .of_match_table = lpc18xx_rgu_match,
  189. .suppress_bind_attrs = true,
  190. },
  191. };
  192. builtin_platform_driver(lpc18xx_rgu_driver);