at91-reset.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 <[email protected]>
  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 <linux/reset-controller.h>
  20. #include <soc/at91/at91sam9_ddrsdr.h>
  21. #include <soc/at91/at91sam9_sdramc.h>
  22. #include <dt-bindings/reset/sama7g5-reset.h>
  23. #define AT91_RSTC_CR 0x00 /* Reset Controller Control Register */
  24. #define AT91_RSTC_PROCRST BIT(0) /* Processor Reset */
  25. #define AT91_RSTC_PERRST BIT(2) /* Peripheral Reset */
  26. #define AT91_RSTC_EXTRST BIT(3) /* External Reset */
  27. #define AT91_RSTC_KEY (0xa5 << 24) /* KEY Password */
  28. #define AT91_RSTC_SR 0x04 /* Reset Controller Status Register */
  29. #define AT91_RSTC_URSTS BIT(0) /* User Reset Status */
  30. #define AT91_RSTC_RSTTYP GENMASK(10, 8) /* Reset Type */
  31. #define AT91_RSTC_NRSTL BIT(16) /* NRST Pin Level */
  32. #define AT91_RSTC_SRCMP BIT(17) /* Software Reset Command in Progress */
  33. #define AT91_RSTC_MR 0x08 /* Reset Controller Mode Register */
  34. #define AT91_RSTC_URSTEN BIT(0) /* User Reset Enable */
  35. #define AT91_RSTC_URSTASYNC BIT(2) /* User Reset Asynchronous Control */
  36. #define AT91_RSTC_URSTIEN BIT(4) /* User Reset Interrupt Enable */
  37. #define AT91_RSTC_ERSTL GENMASK(11, 8) /* External Reset Length */
  38. /**
  39. * enum reset_type - reset types
  40. * @RESET_TYPE_GENERAL: first power-up reset
  41. * @RESET_TYPE_WAKEUP: return from backup mode
  42. * @RESET_TYPE_WATCHDOG: watchdog fault
  43. * @RESET_TYPE_SOFTWARE: processor reset required by software
  44. * @RESET_TYPE_USER: NRST pin detected low
  45. * @RESET_TYPE_CPU_FAIL: CPU clock failure detection
  46. * @RESET_TYPE_XTAL_FAIL: 32KHz crystal failure dectection fault
  47. * @RESET_TYPE_ULP2: ULP2 reset
  48. */
  49. enum reset_type {
  50. RESET_TYPE_GENERAL = 0,
  51. RESET_TYPE_WAKEUP = 1,
  52. RESET_TYPE_WATCHDOG = 2,
  53. RESET_TYPE_SOFTWARE = 3,
  54. RESET_TYPE_USER = 4,
  55. RESET_TYPE_CPU_FAIL = 6,
  56. RESET_TYPE_XTAL_FAIL = 7,
  57. RESET_TYPE_ULP2 = 8,
  58. };
  59. /**
  60. * struct at91_reset - AT91 reset specific data structure
  61. * @rstc_base: base address for system reset
  62. * @ramc_base: array with base addresses of RAM controllers
  63. * @dev_base: base address for devices reset
  64. * @sclk: slow clock
  65. * @data: platform specific reset data
  66. * @rcdev: reset controller device
  67. * @lock: lock for devices reset register access
  68. * @nb: reset notifier block
  69. * @args: SoC specific system reset arguments
  70. * @ramc_lpr: SDRAM Controller Low Power Register
  71. */
  72. struct at91_reset {
  73. void __iomem *rstc_base;
  74. void __iomem *ramc_base[2];
  75. void __iomem *dev_base;
  76. struct clk *sclk;
  77. const struct at91_reset_data *data;
  78. struct reset_controller_dev rcdev;
  79. spinlock_t lock;
  80. struct notifier_block nb;
  81. u32 args;
  82. u32 ramc_lpr;
  83. };
  84. #define to_at91_reset(r) container_of(r, struct at91_reset, rcdev)
  85. /**
  86. * struct at91_reset_data - AT91 reset data
  87. * @reset_args: SoC specific system reset arguments
  88. * @n_device_reset: number of device resets
  89. * @device_reset_min_id: min id for device reset
  90. * @device_reset_max_id: max id for device reset
  91. */
  92. struct at91_reset_data {
  93. u32 reset_args;
  94. u32 n_device_reset;
  95. u8 device_reset_min_id;
  96. u8 device_reset_max_id;
  97. };
  98. /*
  99. * unless the SDRAM is cleanly shutdown before we hit the
  100. * reset register it can be left driving the data bus and
  101. * killing the chance of a subsequent boot from NAND
  102. */
  103. static int at91_reset(struct notifier_block *this, unsigned long mode,
  104. void *cmd)
  105. {
  106. struct at91_reset *reset = container_of(this, struct at91_reset, nb);
  107. asm volatile(
  108. /* Align to cache lines */
  109. ".balign 32\n\t"
  110. /* Disable SDRAM0 accesses */
  111. " tst %0, #0\n\t"
  112. " beq 1f\n\t"
  113. " str %3, [%0, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
  114. /* Power down SDRAM0 */
  115. " str %4, [%0, %6]\n\t"
  116. /* Disable SDRAM1 accesses */
  117. "1: tst %1, #0\n\t"
  118. " beq 2f\n\t"
  119. " strne %3, [%1, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
  120. /* Power down SDRAM1 */
  121. " strne %4, [%1, %6]\n\t"
  122. /* Reset CPU */
  123. "2: str %5, [%2, #" __stringify(AT91_RSTC_CR) "]\n\t"
  124. " b .\n\t"
  125. :
  126. : "r" (reset->ramc_base[0]),
  127. "r" (reset->ramc_base[1]),
  128. "r" (reset->rstc_base),
  129. "r" (1),
  130. "r" cpu_to_le32(AT91_DDRSDRC_LPCB_POWER_DOWN),
  131. "r" (reset->data->reset_args),
  132. "r" (reset->ramc_lpr)
  133. : "r4");
  134. return NOTIFY_DONE;
  135. }
  136. static void __init at91_reset_status(struct platform_device *pdev,
  137. void __iomem *base)
  138. {
  139. const char *reason;
  140. u32 reg = readl(base + AT91_RSTC_SR);
  141. switch ((reg & AT91_RSTC_RSTTYP) >> 8) {
  142. case RESET_TYPE_GENERAL:
  143. reason = "general reset";
  144. break;
  145. case RESET_TYPE_WAKEUP:
  146. reason = "wakeup";
  147. break;
  148. case RESET_TYPE_WATCHDOG:
  149. reason = "watchdog reset";
  150. break;
  151. case RESET_TYPE_SOFTWARE:
  152. reason = "software reset";
  153. break;
  154. case RESET_TYPE_USER:
  155. reason = "user reset";
  156. break;
  157. case RESET_TYPE_CPU_FAIL:
  158. reason = "CPU clock failure detection";
  159. break;
  160. case RESET_TYPE_XTAL_FAIL:
  161. reason = "32.768 kHz crystal failure detection";
  162. break;
  163. case RESET_TYPE_ULP2:
  164. reason = "ULP2 reset";
  165. break;
  166. default:
  167. reason = "unknown reset";
  168. break;
  169. }
  170. dev_info(&pdev->dev, "Starting after %s\n", reason);
  171. }
  172. static const struct of_device_id at91_ramc_of_match[] = {
  173. {
  174. .compatible = "atmel,at91sam9260-sdramc",
  175. .data = (void *)AT91_SDRAMC_LPR,
  176. },
  177. {
  178. .compatible = "atmel,at91sam9g45-ddramc",
  179. .data = (void *)AT91_DDRSDRC_LPR,
  180. },
  181. { /* sentinel */ }
  182. };
  183. static const struct at91_reset_data sam9260 = {
  184. .reset_args = AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST,
  185. };
  186. static const struct at91_reset_data samx7 = {
  187. .reset_args = AT91_RSTC_KEY | AT91_RSTC_PROCRST,
  188. };
  189. static const struct at91_reset_data sama7g5 = {
  190. .reset_args = AT91_RSTC_KEY | AT91_RSTC_PROCRST,
  191. .n_device_reset = 3,
  192. .device_reset_min_id = SAMA7G5_RESET_USB_PHY1,
  193. .device_reset_max_id = SAMA7G5_RESET_USB_PHY3,
  194. };
  195. static const struct of_device_id at91_reset_of_match[] = {
  196. {
  197. .compatible = "atmel,at91sam9260-rstc",
  198. .data = &sam9260,
  199. },
  200. {
  201. .compatible = "atmel,at91sam9g45-rstc",
  202. .data = &sam9260,
  203. },
  204. {
  205. .compatible = "atmel,sama5d3-rstc",
  206. .data = &sam9260,
  207. },
  208. {
  209. .compatible = "atmel,samx7-rstc",
  210. .data = &samx7,
  211. },
  212. {
  213. .compatible = "microchip,sam9x60-rstc",
  214. .data = &samx7,
  215. },
  216. {
  217. .compatible = "microchip,sama7g5-rstc",
  218. .data = &sama7g5,
  219. },
  220. { /* sentinel */ }
  221. };
  222. MODULE_DEVICE_TABLE(of, at91_reset_of_match);
  223. static int at91_reset_update(struct reset_controller_dev *rcdev,
  224. unsigned long id, bool assert)
  225. {
  226. struct at91_reset *reset = to_at91_reset(rcdev);
  227. unsigned long flags;
  228. u32 val;
  229. spin_lock_irqsave(&reset->lock, flags);
  230. val = readl_relaxed(reset->dev_base);
  231. if (assert)
  232. val |= BIT(id);
  233. else
  234. val &= ~BIT(id);
  235. writel_relaxed(val, reset->dev_base);
  236. spin_unlock_irqrestore(&reset->lock, flags);
  237. return 0;
  238. }
  239. static int at91_reset_assert(struct reset_controller_dev *rcdev,
  240. unsigned long id)
  241. {
  242. return at91_reset_update(rcdev, id, true);
  243. }
  244. static int at91_reset_deassert(struct reset_controller_dev *rcdev,
  245. unsigned long id)
  246. {
  247. return at91_reset_update(rcdev, id, false);
  248. }
  249. static int at91_reset_dev_status(struct reset_controller_dev *rcdev,
  250. unsigned long id)
  251. {
  252. struct at91_reset *reset = to_at91_reset(rcdev);
  253. u32 val;
  254. val = readl_relaxed(reset->dev_base);
  255. return !!(val & BIT(id));
  256. }
  257. static const struct reset_control_ops at91_reset_ops = {
  258. .assert = at91_reset_assert,
  259. .deassert = at91_reset_deassert,
  260. .status = at91_reset_dev_status,
  261. };
  262. static int at91_reset_of_xlate(struct reset_controller_dev *rcdev,
  263. const struct of_phandle_args *reset_spec)
  264. {
  265. struct at91_reset *reset = to_at91_reset(rcdev);
  266. if (!reset->data->n_device_reset ||
  267. (reset_spec->args[0] < reset->data->device_reset_min_id ||
  268. reset_spec->args[0] > reset->data->device_reset_max_id))
  269. return -EINVAL;
  270. return reset_spec->args[0];
  271. }
  272. static int at91_rcdev_init(struct at91_reset *reset,
  273. struct platform_device *pdev)
  274. {
  275. if (!reset->data->n_device_reset)
  276. return 0;
  277. reset->dev_base = devm_of_iomap(&pdev->dev, pdev->dev.of_node, 1,
  278. NULL);
  279. if (IS_ERR(reset->dev_base))
  280. return -ENODEV;
  281. spin_lock_init(&reset->lock);
  282. reset->rcdev.ops = &at91_reset_ops;
  283. reset->rcdev.owner = THIS_MODULE;
  284. reset->rcdev.of_node = pdev->dev.of_node;
  285. reset->rcdev.nr_resets = reset->data->n_device_reset;
  286. reset->rcdev.of_reset_n_cells = 1;
  287. reset->rcdev.of_xlate = at91_reset_of_xlate;
  288. return devm_reset_controller_register(&pdev->dev, &reset->rcdev);
  289. }
  290. static int __init at91_reset_probe(struct platform_device *pdev)
  291. {
  292. const struct of_device_id *match;
  293. struct at91_reset *reset;
  294. struct device_node *np;
  295. int ret, idx = 0;
  296. reset = devm_kzalloc(&pdev->dev, sizeof(*reset), GFP_KERNEL);
  297. if (!reset)
  298. return -ENOMEM;
  299. reset->rstc_base = devm_of_iomap(&pdev->dev, pdev->dev.of_node, 0, NULL);
  300. if (IS_ERR(reset->rstc_base)) {
  301. dev_err(&pdev->dev, "Could not map reset controller address\n");
  302. return -ENODEV;
  303. }
  304. if (!of_device_is_compatible(pdev->dev.of_node, "atmel,sama5d3-rstc")) {
  305. /* we need to shutdown the ddr controller, so get ramc base */
  306. for_each_matching_node_and_match(np, at91_ramc_of_match, &match) {
  307. reset->ramc_lpr = (u32)match->data;
  308. reset->ramc_base[idx] = devm_of_iomap(&pdev->dev, np, 0, NULL);
  309. if (IS_ERR(reset->ramc_base[idx])) {
  310. dev_err(&pdev->dev, "Could not map ram controller address\n");
  311. of_node_put(np);
  312. return -ENODEV;
  313. }
  314. idx++;
  315. }
  316. }
  317. reset->data = device_get_match_data(&pdev->dev);
  318. if (!reset->data)
  319. return -ENODEV;
  320. reset->nb.notifier_call = at91_reset;
  321. reset->nb.priority = 192;
  322. reset->sclk = devm_clk_get(&pdev->dev, NULL);
  323. if (IS_ERR(reset->sclk))
  324. return PTR_ERR(reset->sclk);
  325. ret = clk_prepare_enable(reset->sclk);
  326. if (ret) {
  327. dev_err(&pdev->dev, "Could not enable slow clock\n");
  328. return ret;
  329. }
  330. platform_set_drvdata(pdev, reset);
  331. ret = at91_rcdev_init(reset, pdev);
  332. if (ret)
  333. goto disable_clk;
  334. if (of_device_is_compatible(pdev->dev.of_node, "microchip,sam9x60-rstc")) {
  335. u32 val = readl(reset->rstc_base + AT91_RSTC_MR);
  336. writel(AT91_RSTC_KEY | AT91_RSTC_URSTASYNC | val,
  337. reset->rstc_base + AT91_RSTC_MR);
  338. }
  339. ret = register_restart_handler(&reset->nb);
  340. if (ret)
  341. goto disable_clk;
  342. at91_reset_status(pdev, reset->rstc_base);
  343. return 0;
  344. disable_clk:
  345. clk_disable_unprepare(reset->sclk);
  346. return ret;
  347. }
  348. static int __exit at91_reset_remove(struct platform_device *pdev)
  349. {
  350. struct at91_reset *reset = platform_get_drvdata(pdev);
  351. unregister_restart_handler(&reset->nb);
  352. clk_disable_unprepare(reset->sclk);
  353. return 0;
  354. }
  355. static struct platform_driver at91_reset_driver = {
  356. .remove = __exit_p(at91_reset_remove),
  357. .driver = {
  358. .name = "at91-reset",
  359. .of_match_table = at91_reset_of_match,
  360. },
  361. };
  362. module_platform_driver_probe(at91_reset_driver, at91_reset_probe);
  363. MODULE_AUTHOR("Atmel Corporation");
  364. MODULE_DESCRIPTION("Reset driver for Atmel SoCs");
  365. MODULE_LICENSE("GPL v2");