at91-reset.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Atmel AT91 SAM9 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/io.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/reboot.h>
  18. #include <asm/system_misc.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. /*
  44. * unless the SDRAM is cleanly shutdown before we hit the
  45. * reset register it can be left driving the data bus and
  46. * killing the chance of a subsequent boot from NAND
  47. */
  48. static void at91sam9260_restart(enum reboot_mode mode, const char *cmd)
  49. {
  50. asm volatile(
  51. /* Align to cache lines */
  52. ".balign 32\n\t"
  53. /* Disable SDRAM accesses */
  54. "str %2, [%0, #" __stringify(AT91_SDRAMC_TR) "]\n\t"
  55. /* Power down SDRAM */
  56. "str %3, [%0, #" __stringify(AT91_SDRAMC_LPR) "]\n\t"
  57. /* Reset CPU */
  58. "str %4, [%1, #" __stringify(AT91_RSTC_CR) "]\n\t"
  59. "b .\n\t"
  60. :
  61. : "r" (at91_ramc_base[0]),
  62. "r" (at91_rstc_base),
  63. "r" (1),
  64. "r" (AT91_SDRAMC_LPCB_POWER_DOWN),
  65. "r" (AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST));
  66. }
  67. static void at91sam9g45_restart(enum reboot_mode mode, const char *cmd)
  68. {
  69. asm volatile(
  70. /*
  71. * Test wether we have a second RAM controller to care
  72. * about.
  73. *
  74. * First, test that we can dereference the virtual address.
  75. */
  76. "cmp %1, #0\n\t"
  77. "beq 1f\n\t"
  78. /* Then, test that the RAM controller is enabled */
  79. "ldr r0, [%1]\n\t"
  80. "cmp r0, #0\n\t"
  81. /* Align to cache lines */
  82. ".balign 32\n\t"
  83. /* Disable SDRAM0 accesses */
  84. "1: str %3, [%0, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
  85. /* Power down SDRAM0 */
  86. " str %4, [%0, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
  87. /* Disable SDRAM1 accesses */
  88. " strne %3, [%1, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
  89. /* Power down SDRAM1 */
  90. " strne %4, [%1, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
  91. /* Reset CPU */
  92. " str %5, [%2, #" __stringify(AT91_RSTC_CR) "]\n\t"
  93. " b .\n\t"
  94. :
  95. : "r" (at91_ramc_base[0]),
  96. "r" (at91_ramc_base[1]),
  97. "r" (at91_rstc_base),
  98. "r" (1),
  99. "r" (AT91_DDRSDRC_LPCB_POWER_DOWN),
  100. "r" (AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST)
  101. : "r0");
  102. }
  103. static void __init at91_reset_status(struct platform_device *pdev)
  104. {
  105. u32 reg = readl(at91_rstc_base + AT91_RSTC_SR);
  106. char *reason;
  107. switch ((reg & AT91_RSTC_RSTTYP) >> 8) {
  108. case RESET_TYPE_GENERAL:
  109. reason = "general reset";
  110. break;
  111. case RESET_TYPE_WAKEUP:
  112. reason = "wakeup";
  113. break;
  114. case RESET_TYPE_WATCHDOG:
  115. reason = "watchdog reset";
  116. break;
  117. case RESET_TYPE_SOFTWARE:
  118. reason = "software reset";
  119. break;
  120. case RESET_TYPE_USER:
  121. reason = "user reset";
  122. break;
  123. default:
  124. reason = "unknown reset";
  125. break;
  126. }
  127. pr_info("AT91: Starting after %s\n", reason);
  128. }
  129. static struct of_device_id at91_ramc_of_match[] = {
  130. { .compatible = "atmel,at91sam9260-sdramc", },
  131. { .compatible = "atmel,at91sam9g45-ddramc", },
  132. { .compatible = "atmel,sama5d3-ddramc", },
  133. { /* sentinel */ }
  134. };
  135. static struct of_device_id at91_reset_of_match[] = {
  136. { .compatible = "atmel,at91sam9260-rstc", .data = at91sam9260_restart },
  137. { .compatible = "atmel,at91sam9g45-rstc", .data = at91sam9g45_restart },
  138. { /* sentinel */ }
  139. };
  140. static int at91_reset_of_probe(struct platform_device *pdev)
  141. {
  142. const struct of_device_id *match;
  143. struct device_node *np;
  144. int idx = 0;
  145. at91_rstc_base = of_iomap(pdev->dev.of_node, 0);
  146. if (!at91_rstc_base) {
  147. dev_err(&pdev->dev, "Could not map reset controller address\n");
  148. return -ENODEV;
  149. }
  150. for_each_matching_node(np, at91_ramc_of_match) {
  151. at91_ramc_base[idx] = of_iomap(np, 0);
  152. if (!at91_ramc_base[idx]) {
  153. dev_err(&pdev->dev, "Could not map ram controller address\n");
  154. return -ENODEV;
  155. }
  156. idx++;
  157. }
  158. match = of_match_node(at91_reset_of_match, pdev->dev.of_node);
  159. arm_pm_restart = match->data;
  160. return 0;
  161. }
  162. static int at91_reset_platform_probe(struct platform_device *pdev)
  163. {
  164. const struct platform_device_id *match;
  165. struct resource *res;
  166. int idx = 0;
  167. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  168. at91_rstc_base = devm_ioremap_resource(&pdev->dev, res);
  169. if (IS_ERR(at91_rstc_base)) {
  170. dev_err(&pdev->dev, "Could not map reset controller address\n");
  171. return PTR_ERR(at91_rstc_base);
  172. }
  173. for (idx = 0; idx < 2; idx++) {
  174. res = platform_get_resource(pdev, IORESOURCE_MEM, idx + 1 );
  175. at91_ramc_base[idx] = devm_ioremap(&pdev->dev, res->start,
  176. resource_size(res));
  177. if (IS_ERR(at91_ramc_base[idx])) {
  178. dev_err(&pdev->dev, "Could not map ram controller address\n");
  179. return PTR_ERR(at91_ramc_base[idx]);
  180. }
  181. }
  182. match = platform_get_device_id(pdev);
  183. arm_pm_restart = (void (*)(enum reboot_mode, const char*))
  184. match->driver_data;
  185. return 0;
  186. }
  187. static int at91_reset_probe(struct platform_device *pdev)
  188. {
  189. int ret;
  190. if (pdev->dev.of_node)
  191. ret = at91_reset_of_probe(pdev);
  192. else
  193. ret = at91_reset_platform_probe(pdev);
  194. if (ret)
  195. return ret;
  196. at91_reset_status(pdev);
  197. return 0;
  198. }
  199. static struct platform_device_id at91_reset_plat_match[] = {
  200. { "at91-sam9260-reset", (unsigned long)at91sam9260_restart },
  201. { "at91-sam9g45-reset", (unsigned long)at91sam9g45_restart },
  202. { /* sentinel */ }
  203. };
  204. static struct platform_driver at91_reset_driver = {
  205. .probe = at91_reset_probe,
  206. .driver = {
  207. .name = "at91-reset",
  208. .of_match_table = at91_reset_of_match,
  209. },
  210. .id_table = at91_reset_plat_match,
  211. };
  212. module_platform_driver(at91_reset_driver);