at91-reset.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. " strne %3, [%1, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
  120. /* Power down SDRAM1 */
  121. " strne %4, [%1, %6]\n\t"
  122. /* Reset CPU */
  123. " 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. );
  134. return NOTIFY_DONE;
  135. }
  136. static const char *at91_reset_reason(struct at91_reset *reset)
  137. {
  138. u32 reg = readl(reset->rstc_base + AT91_RSTC_SR);
  139. const char *reason;
  140. switch ((reg & AT91_RSTC_RSTTYP) >> 8) {
  141. case RESET_TYPE_GENERAL:
  142. reason = POWER_ON_REASON_REGULAR;
  143. break;
  144. case RESET_TYPE_WAKEUP:
  145. reason = POWER_ON_REASON_RTC;
  146. break;
  147. case RESET_TYPE_WATCHDOG:
  148. reason = POWER_ON_REASON_WATCHDOG;
  149. break;
  150. case RESET_TYPE_SOFTWARE:
  151. reason = POWER_ON_REASON_SOFTWARE;
  152. break;
  153. case RESET_TYPE_USER:
  154. reason = POWER_ON_REASON_RST_BTN;
  155. break;
  156. case RESET_TYPE_CPU_FAIL:
  157. reason = POWER_ON_REASON_CPU_CLK_FAIL;
  158. break;
  159. case RESET_TYPE_XTAL_FAIL:
  160. reason = POWER_ON_REASON_XTAL_FAIL;
  161. break;
  162. case RESET_TYPE_ULP2:
  163. reason = POWER_ON_REASON_BROWN_OUT;
  164. break;
  165. default:
  166. reason = POWER_ON_REASON_UNKNOWN;
  167. break;
  168. }
  169. return reason;
  170. }
  171. static ssize_t power_on_reason_show(struct device *dev,
  172. struct device_attribute *attr, char *buf)
  173. {
  174. struct platform_device *pdev = to_platform_device(dev);
  175. struct at91_reset *reset = platform_get_drvdata(pdev);
  176. return sprintf(buf, "%s\n", at91_reset_reason(reset));
  177. }
  178. static DEVICE_ATTR_RO(power_on_reason);
  179. static const struct of_device_id at91_ramc_of_match[] = {
  180. {
  181. .compatible = "atmel,at91sam9260-sdramc",
  182. .data = (void *)AT91_SDRAMC_LPR,
  183. },
  184. {
  185. .compatible = "atmel,at91sam9g45-ddramc",
  186. .data = (void *)AT91_DDRSDRC_LPR,
  187. },
  188. { /* sentinel */ }
  189. };
  190. static const struct at91_reset_data sam9260 = {
  191. .reset_args = AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST,
  192. };
  193. static const struct at91_reset_data samx7 = {
  194. .reset_args = AT91_RSTC_KEY | AT91_RSTC_PROCRST,
  195. };
  196. static const struct at91_reset_data sama7g5 = {
  197. .reset_args = AT91_RSTC_KEY | AT91_RSTC_PROCRST,
  198. .n_device_reset = 3,
  199. .device_reset_min_id = SAMA7G5_RESET_USB_PHY1,
  200. .device_reset_max_id = SAMA7G5_RESET_USB_PHY3,
  201. };
  202. static const struct of_device_id at91_reset_of_match[] = {
  203. {
  204. .compatible = "atmel,at91sam9260-rstc",
  205. .data = &sam9260,
  206. },
  207. {
  208. .compatible = "atmel,at91sam9g45-rstc",
  209. .data = &sam9260,
  210. },
  211. {
  212. .compatible = "atmel,sama5d3-rstc",
  213. .data = &sam9260,
  214. },
  215. {
  216. .compatible = "atmel,samx7-rstc",
  217. .data = &samx7,
  218. },
  219. {
  220. .compatible = "microchip,sam9x60-rstc",
  221. .data = &samx7,
  222. },
  223. {
  224. .compatible = "microchip,sama7g5-rstc",
  225. .data = &sama7g5,
  226. },
  227. { /* sentinel */ }
  228. };
  229. MODULE_DEVICE_TABLE(of, at91_reset_of_match);
  230. static int at91_reset_update(struct reset_controller_dev *rcdev,
  231. unsigned long id, bool assert)
  232. {
  233. struct at91_reset *reset = to_at91_reset(rcdev);
  234. unsigned long flags;
  235. u32 val;
  236. spin_lock_irqsave(&reset->lock, flags);
  237. val = readl_relaxed(reset->dev_base);
  238. if (assert)
  239. val |= BIT(id);
  240. else
  241. val &= ~BIT(id);
  242. writel_relaxed(val, reset->dev_base);
  243. spin_unlock_irqrestore(&reset->lock, flags);
  244. return 0;
  245. }
  246. static int at91_reset_assert(struct reset_controller_dev *rcdev,
  247. unsigned long id)
  248. {
  249. return at91_reset_update(rcdev, id, true);
  250. }
  251. static int at91_reset_deassert(struct reset_controller_dev *rcdev,
  252. unsigned long id)
  253. {
  254. return at91_reset_update(rcdev, id, false);
  255. }
  256. static int at91_reset_dev_status(struct reset_controller_dev *rcdev,
  257. unsigned long id)
  258. {
  259. struct at91_reset *reset = to_at91_reset(rcdev);
  260. u32 val;
  261. val = readl_relaxed(reset->dev_base);
  262. return !!(val & BIT(id));
  263. }
  264. static const struct reset_control_ops at91_reset_ops = {
  265. .assert = at91_reset_assert,
  266. .deassert = at91_reset_deassert,
  267. .status = at91_reset_dev_status,
  268. };
  269. static int at91_reset_of_xlate(struct reset_controller_dev *rcdev,
  270. const struct of_phandle_args *reset_spec)
  271. {
  272. struct at91_reset *reset = to_at91_reset(rcdev);
  273. if (!reset->data->n_device_reset ||
  274. (reset_spec->args[0] < reset->data->device_reset_min_id ||
  275. reset_spec->args[0] > reset->data->device_reset_max_id))
  276. return -EINVAL;
  277. return reset_spec->args[0];
  278. }
  279. static int at91_rcdev_init(struct at91_reset *reset,
  280. struct platform_device *pdev)
  281. {
  282. if (!reset->data->n_device_reset)
  283. return 0;
  284. reset->dev_base = devm_of_iomap(&pdev->dev, pdev->dev.of_node, 1,
  285. NULL);
  286. if (IS_ERR(reset->dev_base))
  287. return -ENODEV;
  288. spin_lock_init(&reset->lock);
  289. reset->rcdev.ops = &at91_reset_ops;
  290. reset->rcdev.owner = THIS_MODULE;
  291. reset->rcdev.of_node = pdev->dev.of_node;
  292. reset->rcdev.nr_resets = reset->data->n_device_reset;
  293. reset->rcdev.of_reset_n_cells = 1;
  294. reset->rcdev.of_xlate = at91_reset_of_xlate;
  295. return devm_reset_controller_register(&pdev->dev, &reset->rcdev);
  296. }
  297. static int at91_reset_probe(struct platform_device *pdev)
  298. {
  299. const struct of_device_id *match;
  300. struct at91_reset *reset;
  301. struct device_node *np;
  302. int ret, idx = 0;
  303. reset = devm_kzalloc(&pdev->dev, sizeof(*reset), GFP_KERNEL);
  304. if (!reset)
  305. return -ENOMEM;
  306. reset->rstc_base = devm_of_iomap(&pdev->dev, pdev->dev.of_node, 0, NULL);
  307. if (IS_ERR(reset->rstc_base)) {
  308. dev_err(&pdev->dev, "Could not map reset controller address\n");
  309. return -ENODEV;
  310. }
  311. if (!of_device_is_compatible(pdev->dev.of_node, "atmel,sama5d3-rstc")) {
  312. /* we need to shutdown the ddr controller, so get ramc base */
  313. for_each_matching_node_and_match(np, at91_ramc_of_match, &match) {
  314. reset->ramc_lpr = (u32)match->data;
  315. reset->ramc_base[idx] = devm_of_iomap(&pdev->dev, np, 0, NULL);
  316. if (IS_ERR(reset->ramc_base[idx])) {
  317. dev_err(&pdev->dev, "Could not map ram controller address\n");
  318. of_node_put(np);
  319. return -ENODEV;
  320. }
  321. idx++;
  322. }
  323. }
  324. reset->data = device_get_match_data(&pdev->dev);
  325. if (!reset->data)
  326. return -ENODEV;
  327. reset->nb.notifier_call = at91_reset;
  328. reset->nb.priority = 192;
  329. reset->sclk = devm_clk_get(&pdev->dev, NULL);
  330. if (IS_ERR(reset->sclk))
  331. return PTR_ERR(reset->sclk);
  332. ret = clk_prepare_enable(reset->sclk);
  333. if (ret) {
  334. dev_err(&pdev->dev, "Could not enable slow clock\n");
  335. return ret;
  336. }
  337. platform_set_drvdata(pdev, reset);
  338. ret = at91_rcdev_init(reset, pdev);
  339. if (ret)
  340. goto disable_clk;
  341. if (of_device_is_compatible(pdev->dev.of_node, "microchip,sam9x60-rstc")) {
  342. u32 val = readl(reset->rstc_base + AT91_RSTC_MR);
  343. writel(AT91_RSTC_KEY | AT91_RSTC_URSTASYNC | val,
  344. reset->rstc_base + AT91_RSTC_MR);
  345. }
  346. ret = register_restart_handler(&reset->nb);
  347. if (ret)
  348. goto disable_clk;
  349. ret = device_create_file(&pdev->dev, &dev_attr_power_on_reason);
  350. if (ret) {
  351. dev_err(&pdev->dev, "Could not create sysfs entry\n");
  352. return ret;
  353. }
  354. dev_info(&pdev->dev, "Starting after %s\n", at91_reset_reason(reset));
  355. return 0;
  356. disable_clk:
  357. clk_disable_unprepare(reset->sclk);
  358. return ret;
  359. }
  360. static void at91_reset_remove(struct platform_device *pdev)
  361. {
  362. struct at91_reset *reset = platform_get_drvdata(pdev);
  363. unregister_restart_handler(&reset->nb);
  364. clk_disable_unprepare(reset->sclk);
  365. }
  366. static struct platform_driver at91_reset_driver = {
  367. .probe = at91_reset_probe,
  368. .remove = at91_reset_remove,
  369. .driver = {
  370. .name = "at91-reset",
  371. .of_match_table = at91_reset_of_match,
  372. },
  373. };
  374. module_platform_driver(at91_reset_driver);
  375. MODULE_AUTHOR("Atmel Corporation");
  376. MODULE_DESCRIPTION("Reset driver for Atmel SoCs");
  377. MODULE_LICENSE("GPL v2");