at91-reset.c 11 KB

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