at91-reset.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Atmel AT91 SAM9 & SAMA5 SoCs reset code
  3. *
  4. * Copyright (C) 2007 Atmel Corporation.
  5. * Copyright (C) BitBox Ltd 2010
  6. * Copyright (C) 2011 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcosoft.com>
  7. * Copyright (C) 2014 Free Electrons
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/of_address.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/reboot.h>
  19. #include <soc/at91/at91sam9_ddrsdr.h>
  20. #include <soc/at91/at91sam9_sdramc.h>
  21. #define AT91_RSTC_CR 0x00 /* Reset Controller Control Register */
  22. #define AT91_RSTC_PROCRST BIT(0) /* Processor Reset */
  23. #define AT91_RSTC_PERRST BIT(2) /* Peripheral Reset */
  24. #define AT91_RSTC_EXTRST BIT(3) /* External Reset */
  25. #define AT91_RSTC_KEY (0xa5 << 24) /* KEY Password */
  26. #define AT91_RSTC_SR 0x04 /* Reset Controller Status Register */
  27. #define AT91_RSTC_URSTS BIT(0) /* User Reset Status */
  28. #define AT91_RSTC_RSTTYP GENMASK(10, 8) /* Reset Type */
  29. #define AT91_RSTC_NRSTL BIT(16) /* NRST Pin Level */
  30. #define AT91_RSTC_SRCMP BIT(17) /* Software Reset Command in Progress */
  31. #define AT91_RSTC_MR 0x08 /* Reset Controller Mode Register */
  32. #define AT91_RSTC_URSTEN BIT(0) /* User Reset Enable */
  33. #define AT91_RSTC_URSTIEN BIT(4) /* User Reset Interrupt Enable */
  34. #define AT91_RSTC_ERSTL GENMASK(11, 8) /* External Reset Length */
  35. enum reset_type {
  36. RESET_TYPE_GENERAL = 0,
  37. RESET_TYPE_WAKEUP = 1,
  38. RESET_TYPE_WATCHDOG = 2,
  39. RESET_TYPE_SOFTWARE = 3,
  40. RESET_TYPE_USER = 4,
  41. };
  42. static void __iomem *at91_ramc_base[2], *at91_rstc_base;
  43. static struct clk *sclk;
  44. /*
  45. * unless the SDRAM is cleanly shutdown before we hit the
  46. * reset register it can be left driving the data bus and
  47. * killing the chance of a subsequent boot from NAND
  48. */
  49. static int at91sam9260_restart(struct notifier_block *this, unsigned long mode,
  50. void *cmd)
  51. {
  52. asm volatile(
  53. /* Align to cache lines */
  54. ".balign 32\n\t"
  55. /* Disable SDRAM accesses */
  56. "str %2, [%0, #" __stringify(AT91_SDRAMC_TR) "]\n\t"
  57. /* Power down SDRAM */
  58. "str %3, [%0, #" __stringify(AT91_SDRAMC_LPR) "]\n\t"
  59. /* Reset CPU */
  60. "str %4, [%1, #" __stringify(AT91_RSTC_CR) "]\n\t"
  61. "b .\n\t"
  62. :
  63. : "r" (at91_ramc_base[0]),
  64. "r" (at91_rstc_base),
  65. "r" (1),
  66. "r" cpu_to_le32(AT91_SDRAMC_LPCB_POWER_DOWN),
  67. "r" cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST));
  68. return NOTIFY_DONE;
  69. }
  70. static int at91sam9g45_restart(struct notifier_block *this, unsigned long mode,
  71. void *cmd)
  72. {
  73. asm volatile(
  74. /*
  75. * Test wether we have a second RAM controller to care
  76. * about.
  77. *
  78. * First, test that we can dereference the virtual address.
  79. */
  80. "cmp %1, #0\n\t"
  81. "beq 1f\n\t"
  82. /* Then, test that the RAM controller is enabled */
  83. "ldr r0, [%1]\n\t"
  84. "cmp r0, #0\n\t"
  85. /* Align to cache lines */
  86. ".balign 32\n\t"
  87. /* Disable SDRAM0 accesses */
  88. "1: str %3, [%0, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
  89. /* Power down SDRAM0 */
  90. " str %4, [%0, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
  91. /* Disable SDRAM1 accesses */
  92. " strne %3, [%1, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
  93. /* Power down SDRAM1 */
  94. " strne %4, [%1, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
  95. /* Reset CPU */
  96. " str %5, [%2, #" __stringify(AT91_RSTC_CR) "]\n\t"
  97. " b .\n\t"
  98. :
  99. : "r" (at91_ramc_base[0]),
  100. "r" (at91_ramc_base[1]),
  101. "r" (at91_rstc_base),
  102. "r" (1),
  103. "r" cpu_to_le32(AT91_DDRSDRC_LPCB_POWER_DOWN),
  104. "r" cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST)
  105. : "r0");
  106. return NOTIFY_DONE;
  107. }
  108. static int sama5d3_restart(struct notifier_block *this, unsigned long mode,
  109. void *cmd)
  110. {
  111. writel(cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST),
  112. at91_rstc_base);
  113. return NOTIFY_DONE;
  114. }
  115. static void __init at91_reset_status(struct platform_device *pdev)
  116. {
  117. u32 reg = readl(at91_rstc_base + AT91_RSTC_SR);
  118. char *reason;
  119. switch ((reg & AT91_RSTC_RSTTYP) >> 8) {
  120. case RESET_TYPE_GENERAL:
  121. reason = "general reset";
  122. break;
  123. case RESET_TYPE_WAKEUP:
  124. reason = "wakeup";
  125. break;
  126. case RESET_TYPE_WATCHDOG:
  127. reason = "watchdog reset";
  128. break;
  129. case RESET_TYPE_SOFTWARE:
  130. reason = "software reset";
  131. break;
  132. case RESET_TYPE_USER:
  133. reason = "user reset";
  134. break;
  135. default:
  136. reason = "unknown reset";
  137. break;
  138. }
  139. pr_info("AT91: Starting after %s\n", reason);
  140. }
  141. static const struct of_device_id at91_ramc_of_match[] = {
  142. { .compatible = "atmel,at91sam9260-sdramc", },
  143. { .compatible = "atmel,at91sam9g45-ddramc", },
  144. { /* sentinel */ }
  145. };
  146. static const struct of_device_id at91_reset_of_match[] = {
  147. { .compatible = "atmel,at91sam9260-rstc", .data = at91sam9260_restart },
  148. { .compatible = "atmel,at91sam9g45-rstc", .data = at91sam9g45_restart },
  149. { .compatible = "atmel,sama5d3-rstc", .data = sama5d3_restart },
  150. { /* sentinel */ }
  151. };
  152. static struct notifier_block at91_restart_nb = {
  153. .priority = 192,
  154. };
  155. static int __init at91_reset_probe(struct platform_device *pdev)
  156. {
  157. const struct of_device_id *match;
  158. struct device_node *np;
  159. int ret, idx = 0;
  160. at91_rstc_base = of_iomap(pdev->dev.of_node, 0);
  161. if (!at91_rstc_base) {
  162. dev_err(&pdev->dev, "Could not map reset controller address\n");
  163. return -ENODEV;
  164. }
  165. if (!of_device_is_compatible(pdev->dev.of_node, "atmel,sama5d3-rstc")) {
  166. /* we need to shutdown the ddr controller, so get ramc base */
  167. for_each_matching_node(np, at91_ramc_of_match) {
  168. at91_ramc_base[idx] = of_iomap(np, 0);
  169. if (!at91_ramc_base[idx]) {
  170. dev_err(&pdev->dev, "Could not map ram controller address\n");
  171. return -ENODEV;
  172. }
  173. idx++;
  174. }
  175. }
  176. match = of_match_node(at91_reset_of_match, pdev->dev.of_node);
  177. at91_restart_nb.notifier_call = match->data;
  178. sclk = devm_clk_get(&pdev->dev, NULL);
  179. if (IS_ERR(sclk))
  180. return PTR_ERR(sclk);
  181. ret = clk_prepare_enable(sclk);
  182. if (ret) {
  183. dev_err(&pdev->dev, "Could not enable slow clock\n");
  184. return ret;
  185. }
  186. ret = register_restart_handler(&at91_restart_nb);
  187. if (ret) {
  188. clk_disable_unprepare(sclk);
  189. return ret;
  190. }
  191. at91_reset_status(pdev);
  192. return 0;
  193. }
  194. static int __exit at91_reset_remove(struct platform_device *pdev)
  195. {
  196. unregister_restart_handler(&at91_restart_nb);
  197. clk_disable_unprepare(sclk);
  198. return 0;
  199. }
  200. static const struct platform_device_id at91_reset_plat_match[] = {
  201. { "at91-sam9260-reset", (unsigned long)at91sam9260_restart },
  202. { "at91-sam9g45-reset", (unsigned long)at91sam9g45_restart },
  203. { /* sentinel */ }
  204. };
  205. static struct platform_driver at91_reset_driver = {
  206. .remove = __exit_p(at91_reset_remove),
  207. .driver = {
  208. .name = "at91-reset",
  209. .of_match_table = at91_reset_of_match,
  210. },
  211. .id_table = at91_reset_plat_match,
  212. };
  213. module_platform_driver_probe(at91_reset_driver, at91_reset_probe);
  214. MODULE_AUTHOR("Atmel Corporation");
  215. MODULE_DESCRIPTION("Reset driver for Atmel SoCs");
  216. MODULE_LICENSE("GPL v2");