at91-reset.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <soc/at91/at91sam9_ddrsdr.h>
  19. #include <soc/at91/at91sam9_sdramc.h>
  20. #define AT91_RSTC_CR 0x00 /* Reset Controller Control Register */
  21. #define AT91_RSTC_PROCRST BIT(0) /* Processor Reset */
  22. #define AT91_RSTC_PERRST BIT(2) /* Peripheral Reset */
  23. #define AT91_RSTC_EXTRST BIT(3) /* External Reset */
  24. #define AT91_RSTC_KEY (0xa5 << 24) /* KEY Password */
  25. #define AT91_RSTC_SR 0x04 /* Reset Controller Status Register */
  26. #define AT91_RSTC_URSTS BIT(0) /* User Reset Status */
  27. #define AT91_RSTC_RSTTYP GENMASK(10, 8) /* Reset Type */
  28. #define AT91_RSTC_NRSTL BIT(16) /* NRST Pin Level */
  29. #define AT91_RSTC_SRCMP BIT(17) /* Software Reset Command in Progress */
  30. #define AT91_RSTC_MR 0x08 /* Reset Controller Mode Register */
  31. #define AT91_RSTC_URSTEN BIT(0) /* User Reset Enable */
  32. #define AT91_RSTC_URSTIEN BIT(4) /* User Reset Interrupt Enable */
  33. #define AT91_RSTC_ERSTL GENMASK(11, 8) /* External Reset Length */
  34. enum reset_type {
  35. RESET_TYPE_GENERAL = 0,
  36. RESET_TYPE_WAKEUP = 1,
  37. RESET_TYPE_WATCHDOG = 2,
  38. RESET_TYPE_SOFTWARE = 3,
  39. RESET_TYPE_USER = 4,
  40. };
  41. static void __iomem *at91_ramc_base[2], *at91_rstc_base;
  42. /*
  43. * unless the SDRAM is cleanly shutdown before we hit the
  44. * reset register it can be left driving the data bus and
  45. * killing the chance of a subsequent boot from NAND
  46. */
  47. static int at91sam9260_restart(struct notifier_block *this, unsigned long mode,
  48. void *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" cpu_to_le32(AT91_SDRAMC_LPCB_POWER_DOWN),
  65. "r" cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST));
  66. return NOTIFY_DONE;
  67. }
  68. static int at91sam9g45_restart(struct notifier_block *this, unsigned long mode,
  69. void *cmd)
  70. {
  71. asm volatile(
  72. /*
  73. * Test wether we have a second RAM controller to care
  74. * about.
  75. *
  76. * First, test that we can dereference the virtual address.
  77. */
  78. "cmp %1, #0\n\t"
  79. "beq 1f\n\t"
  80. /* Then, test that the RAM controller is enabled */
  81. "ldr r0, [%1]\n\t"
  82. "cmp r0, #0\n\t"
  83. /* Align to cache lines */
  84. ".balign 32\n\t"
  85. /* Disable SDRAM0 accesses */
  86. "1: str %3, [%0, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
  87. /* Power down SDRAM0 */
  88. " str %4, [%0, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
  89. /* Disable SDRAM1 accesses */
  90. " strne %3, [%1, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
  91. /* Power down SDRAM1 */
  92. " strne %4, [%1, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
  93. /* Reset CPU */
  94. " str %5, [%2, #" __stringify(AT91_RSTC_CR) "]\n\t"
  95. " b .\n\t"
  96. :
  97. : "r" (at91_ramc_base[0]),
  98. "r" (at91_ramc_base[1]),
  99. "r" (at91_rstc_base),
  100. "r" (1),
  101. "r" cpu_to_le32(AT91_DDRSDRC_LPCB_POWER_DOWN),
  102. "r" cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST)
  103. : "r0");
  104. return NOTIFY_DONE;
  105. }
  106. static void __init at91_reset_status(struct platform_device *pdev)
  107. {
  108. u32 reg = readl(at91_rstc_base + AT91_RSTC_SR);
  109. char *reason;
  110. switch ((reg & AT91_RSTC_RSTTYP) >> 8) {
  111. case RESET_TYPE_GENERAL:
  112. reason = "general reset";
  113. break;
  114. case RESET_TYPE_WAKEUP:
  115. reason = "wakeup";
  116. break;
  117. case RESET_TYPE_WATCHDOG:
  118. reason = "watchdog reset";
  119. break;
  120. case RESET_TYPE_SOFTWARE:
  121. reason = "software reset";
  122. break;
  123. case RESET_TYPE_USER:
  124. reason = "user reset";
  125. break;
  126. default:
  127. reason = "unknown reset";
  128. break;
  129. }
  130. pr_info("AT91: Starting after %s\n", reason);
  131. }
  132. static const struct of_device_id at91_ramc_of_match[] = {
  133. { .compatible = "atmel,at91sam9260-sdramc", },
  134. { .compatible = "atmel,at91sam9g45-ddramc", },
  135. { .compatible = "atmel,sama5d3-ddramc", },
  136. { /* sentinel */ }
  137. };
  138. static const struct of_device_id at91_reset_of_match[] = {
  139. { .compatible = "atmel,at91sam9260-rstc", .data = at91sam9260_restart },
  140. { .compatible = "atmel,at91sam9g45-rstc", .data = at91sam9g45_restart },
  141. { /* sentinel */ }
  142. };
  143. static struct notifier_block at91_restart_nb = {
  144. .priority = 192,
  145. };
  146. static int at91_reset_of_probe(struct platform_device *pdev)
  147. {
  148. const struct of_device_id *match;
  149. struct device_node *np;
  150. int idx = 0;
  151. at91_rstc_base = of_iomap(pdev->dev.of_node, 0);
  152. if (!at91_rstc_base) {
  153. dev_err(&pdev->dev, "Could not map reset controller address\n");
  154. return -ENODEV;
  155. }
  156. for_each_matching_node(np, at91_ramc_of_match) {
  157. at91_ramc_base[idx] = of_iomap(np, 0);
  158. if (!at91_ramc_base[idx]) {
  159. dev_err(&pdev->dev, "Could not map ram controller address\n");
  160. return -ENODEV;
  161. }
  162. idx++;
  163. }
  164. match = of_match_node(at91_reset_of_match, pdev->dev.of_node);
  165. at91_restart_nb.notifier_call = match->data;
  166. return register_restart_handler(&at91_restart_nb);
  167. }
  168. static int at91_reset_platform_probe(struct platform_device *pdev)
  169. {
  170. const struct platform_device_id *match;
  171. struct resource *res;
  172. int idx = 0;
  173. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  174. at91_rstc_base = devm_ioremap_resource(&pdev->dev, res);
  175. if (IS_ERR(at91_rstc_base)) {
  176. dev_err(&pdev->dev, "Could not map reset controller address\n");
  177. return PTR_ERR(at91_rstc_base);
  178. }
  179. for (idx = 0; idx < 2; idx++) {
  180. res = platform_get_resource(pdev, IORESOURCE_MEM, idx + 1 );
  181. at91_ramc_base[idx] = devm_ioremap(&pdev->dev, res->start,
  182. resource_size(res));
  183. if (!at91_ramc_base[idx]) {
  184. dev_err(&pdev->dev, "Could not map ram controller address\n");
  185. return -ENOMEM;
  186. }
  187. }
  188. match = platform_get_device_id(pdev);
  189. at91_restart_nb.notifier_call =
  190. (int (*)(struct notifier_block *,
  191. unsigned long, void *)) match->driver_data;
  192. return register_restart_handler(&at91_restart_nb);
  193. }
  194. static int at91_reset_probe(struct platform_device *pdev)
  195. {
  196. int ret;
  197. if (pdev->dev.of_node)
  198. ret = at91_reset_of_probe(pdev);
  199. else
  200. ret = at91_reset_platform_probe(pdev);
  201. if (ret)
  202. return ret;
  203. at91_reset_status(pdev);
  204. return 0;
  205. }
  206. static struct platform_device_id at91_reset_plat_match[] = {
  207. { "at91-sam9260-reset", (unsigned long)at91sam9260_restart },
  208. { "at91-sam9g45-reset", (unsigned long)at91sam9g45_restart },
  209. { /* sentinel */ }
  210. };
  211. static struct platform_driver at91_reset_driver = {
  212. .probe = at91_reset_probe,
  213. .driver = {
  214. .name = "at91-reset",
  215. .of_match_table = at91_reset_of_match,
  216. },
  217. .id_table = at91_reset_plat_match,
  218. };
  219. module_platform_driver(at91_reset_driver);