2 * Atmel AT91 SAM9 & SAMA5 SoCs reset code
4 * Copyright (C) 2007 Atmel Corporation.
5 * Copyright (C) BitBox Ltd 2010
7 * Copyright (C) 2014 Free Electrons
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.
14 #include <linux/clk.h>
16 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/platform_device.h>
19 #include <linux/reboot.h>
21 #include <soc/at91/at91sam9_ddrsdr.h>
22 #include <soc/at91/at91sam9_sdramc.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 */
30 #define AT91_RSTC_SR 0x04 /* Reset Controller Status Register */
31 #define AT91_RSTC_URSTS BIT(0) /* User Reset Status */
32 #define AT91_RSTC_RSTTYP GENMASK(10, 8) /* Reset Type */
33 #define AT91_RSTC_NRSTL BIT(16) /* NRST Pin Level */
34 #define AT91_RSTC_SRCMP BIT(17) /* Software Reset Command in Progress */
36 #define AT91_RSTC_MR 0x08 /* Reset Controller Mode Register */
37 #define AT91_RSTC_URSTEN BIT(0) /* User Reset Enable */
38 #define AT91_RSTC_URSTIEN BIT(4) /* User Reset Interrupt Enable */
39 #define AT91_RSTC_ERSTL GENMASK(11, 8) /* External Reset Length */
42 RESET_TYPE_GENERAL = 0,
43 RESET_TYPE_WAKEUP = 1,
44 RESET_TYPE_WATCHDOG = 2,
45 RESET_TYPE_SOFTWARE = 3,
47 RESET_TYPE_CPU_FAIL = 6,
48 RESET_TYPE_XTAL_FAIL = 7,
52 static void __iomem *at91_ramc_base[2], *at91_rstc_base;
53 static struct clk *sclk;
56 * unless the SDRAM is cleanly shutdown before we hit the
57 * reset register it can be left driving the data bus and
58 * killing the chance of a subsequent boot from NAND
60 static int at91sam9260_restart(struct notifier_block *this, unsigned long mode,
64 /* Align to cache lines */
67 /* Disable SDRAM accesses */
68 "str %2, [%0, #" __stringify(AT91_SDRAMC_TR) "]\n\t"
70 /* Power down SDRAM */
71 "str %3, [%0, #" __stringify(AT91_SDRAMC_LPR) "]\n\t"
74 "str %4, [%1, #" __stringify(AT91_RSTC_CR) "]\n\t"
78 : "r" (at91_ramc_base[0]),
81 "r" cpu_to_le32(AT91_SDRAMC_LPCB_POWER_DOWN),
82 "r" cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST));
87 static int at91sam9g45_restart(struct notifier_block *this, unsigned long mode,
92 * Test wether we have a second RAM controller to care
95 * First, test that we can dereference the virtual address.
100 /* Then, test that the RAM controller is enabled */
104 /* Align to cache lines */
107 /* Disable SDRAM0 accesses */
108 "1: str %3, [%0, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
109 /* Power down SDRAM0 */
110 " str %4, [%0, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
111 /* Disable SDRAM1 accesses */
112 " strne %3, [%1, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
113 /* Power down SDRAM1 */
114 " strne %4, [%1, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
116 " str %5, [%2, #" __stringify(AT91_RSTC_CR) "]\n\t"
120 : "r" (at91_ramc_base[0]),
121 "r" (at91_ramc_base[1]),
122 "r" (at91_rstc_base),
124 "r" cpu_to_le32(AT91_DDRSDRC_LPCB_POWER_DOWN),
125 "r" cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST)
131 static int sama5d3_restart(struct notifier_block *this, unsigned long mode,
134 writel(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST,
140 static int samx7_restart(struct notifier_block *this, unsigned long mode,
143 writel(AT91_RSTC_KEY | AT91_RSTC_PROCRST, at91_rstc_base);
147 static void __init at91_reset_status(struct platform_device *pdev)
150 u32 reg = readl(at91_rstc_base + AT91_RSTC_SR);
152 switch ((reg & AT91_RSTC_RSTTYP) >> 8) {
153 case RESET_TYPE_GENERAL:
154 reason = "general reset";
156 case RESET_TYPE_WAKEUP:
159 case RESET_TYPE_WATCHDOG:
160 reason = "watchdog reset";
162 case RESET_TYPE_SOFTWARE:
163 reason = "software reset";
165 case RESET_TYPE_USER:
166 reason = "user reset";
168 case RESET_TYPE_CPU_FAIL:
169 reason = "CPU clock failure detection";
171 case RESET_TYPE_XTAL_FAIL:
172 reason = "32.768 kHz crystal failure detection";
174 case RESET_TYPE_ULP2:
175 reason = "ULP2 reset";
178 reason = "unknown reset";
182 dev_info(&pdev->dev, "Starting after %s\n", reason);
185 static const struct of_device_id at91_ramc_of_match[] = {
186 { .compatible = "atmel,at91sam9260-sdramc", },
187 { .compatible = "atmel,at91sam9g45-ddramc", },
191 static const struct of_device_id at91_reset_of_match[] = {
192 { .compatible = "atmel,at91sam9260-rstc", .data = at91sam9260_restart },
193 { .compatible = "atmel,at91sam9g45-rstc", .data = at91sam9g45_restart },
194 { .compatible = "atmel,sama5d3-rstc", .data = sama5d3_restart },
195 { .compatible = "atmel,samx7-rstc", .data = samx7_restart },
196 { .compatible = "microchip,sam9x60-rstc", .data = samx7_restart },
199 MODULE_DEVICE_TABLE(of, at91_reset_of_match);
201 static struct notifier_block at91_restart_nb = {
205 static int __init at91_reset_probe(struct platform_device *pdev)
207 const struct of_device_id *match;
208 struct device_node *np;
211 at91_rstc_base = of_iomap(pdev->dev.of_node, 0);
212 if (!at91_rstc_base) {
213 dev_err(&pdev->dev, "Could not map reset controller address\n");
217 if (!of_device_is_compatible(pdev->dev.of_node, "atmel,sama5d3-rstc")) {
218 /* we need to shutdown the ddr controller, so get ramc base */
219 for_each_matching_node(np, at91_ramc_of_match) {
220 at91_ramc_base[idx] = of_iomap(np, 0);
221 if (!at91_ramc_base[idx]) {
222 dev_err(&pdev->dev, "Could not map ram controller address\n");
230 match = of_match_node(at91_reset_of_match, pdev->dev.of_node);
231 at91_restart_nb.notifier_call = match->data;
233 sclk = devm_clk_get(&pdev->dev, NULL);
235 return PTR_ERR(sclk);
237 ret = clk_prepare_enable(sclk);
239 dev_err(&pdev->dev, "Could not enable slow clock\n");
243 ret = register_restart_handler(&at91_restart_nb);
245 clk_disable_unprepare(sclk);
249 at91_reset_status(pdev);
254 static int __exit at91_reset_remove(struct platform_device *pdev)
256 unregister_restart_handler(&at91_restart_nb);
257 clk_disable_unprepare(sclk);
262 static struct platform_driver at91_reset_driver = {
263 .remove = __exit_p(at91_reset_remove),
265 .name = "at91-reset",
266 .of_match_table = at91_reset_of_match,
269 module_platform_driver_probe(at91_reset_driver, at91_reset_probe);
271 MODULE_AUTHOR("Atmel Corporation");
272 MODULE_DESCRIPTION("Reset driver for Atmel SoCs");
273 MODULE_LICENSE("GPL v2");