Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 1 | /* |
Nicolas Ferre | bc312cb | 2015-09-10 12:35:13 +0200 | [diff] [blame] | 2 | * Atmel AT91 SAM9 & SAMA5 SoCs reset code |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 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 | |
Alexandre Belloni | 2b2c614 | 2015-08-11 11:12:48 +0200 | [diff] [blame] | 14 | #include <linux/clk.h> |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 15 | #include <linux/io.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/of_address.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/reboot.h> |
| 20 | |
Alexandre Belloni | f0a0a58 | 2014-11-07 21:58:21 +0100 | [diff] [blame] | 21 | #include <soc/at91/at91sam9_ddrsdr.h> |
| 22 | #include <soc/at91/at91sam9_sdramc.h> |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 23 | |
| 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 | |
| 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 */ |
| 35 | |
| 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 */ |
| 40 | |
| 41 | enum reset_type { |
| 42 | RESET_TYPE_GENERAL = 0, |
| 43 | RESET_TYPE_WAKEUP = 1, |
| 44 | RESET_TYPE_WATCHDOG = 2, |
| 45 | RESET_TYPE_SOFTWARE = 3, |
| 46 | RESET_TYPE_USER = 4, |
| 47 | }; |
| 48 | |
| 49 | static void __iomem *at91_ramc_base[2], *at91_rstc_base; |
Alexandre Belloni | 2b2c614 | 2015-08-11 11:12:48 +0200 | [diff] [blame] | 50 | static struct clk *sclk; |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * unless the SDRAM is cleanly shutdown before we hit the |
| 54 | * reset register it can be left driving the data bus and |
| 55 | * killing the chance of a subsequent boot from NAND |
| 56 | */ |
Guenter Roeck | 481ff6f | 2015-01-25 12:30:41 -0800 | [diff] [blame] | 57 | static int at91sam9260_restart(struct notifier_block *this, unsigned long mode, |
| 58 | void *cmd) |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 59 | { |
| 60 | asm volatile( |
| 61 | /* Align to cache lines */ |
| 62 | ".balign 32\n\t" |
| 63 | |
| 64 | /* Disable SDRAM accesses */ |
| 65 | "str %2, [%0, #" __stringify(AT91_SDRAMC_TR) "]\n\t" |
| 66 | |
| 67 | /* Power down SDRAM */ |
| 68 | "str %3, [%0, #" __stringify(AT91_SDRAMC_LPR) "]\n\t" |
| 69 | |
| 70 | /* Reset CPU */ |
| 71 | "str %4, [%1, #" __stringify(AT91_RSTC_CR) "]\n\t" |
| 72 | |
| 73 | "b .\n\t" |
| 74 | : |
| 75 | : "r" (at91_ramc_base[0]), |
| 76 | "r" (at91_rstc_base), |
| 77 | "r" (1), |
Ben Dooks | 7be5ac2 | 2015-03-26 14:16:22 +0000 | [diff] [blame] | 78 | "r" cpu_to_le32(AT91_SDRAMC_LPCB_POWER_DOWN), |
| 79 | "r" cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST)); |
Guenter Roeck | 481ff6f | 2015-01-25 12:30:41 -0800 | [diff] [blame] | 80 | |
| 81 | return NOTIFY_DONE; |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Guenter Roeck | 481ff6f | 2015-01-25 12:30:41 -0800 | [diff] [blame] | 84 | static int at91sam9g45_restart(struct notifier_block *this, unsigned long mode, |
| 85 | void *cmd) |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 86 | { |
| 87 | asm volatile( |
| 88 | /* |
| 89 | * Test wether we have a second RAM controller to care |
| 90 | * about. |
| 91 | * |
| 92 | * First, test that we can dereference the virtual address. |
| 93 | */ |
| 94 | "cmp %1, #0\n\t" |
| 95 | "beq 1f\n\t" |
| 96 | |
| 97 | /* Then, test that the RAM controller is enabled */ |
| 98 | "ldr r0, [%1]\n\t" |
| 99 | "cmp r0, #0\n\t" |
| 100 | |
| 101 | /* Align to cache lines */ |
| 102 | ".balign 32\n\t" |
| 103 | |
| 104 | /* Disable SDRAM0 accesses */ |
| 105 | "1: str %3, [%0, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t" |
| 106 | /* Power down SDRAM0 */ |
Alexandre Belloni | 7cb4e71 | 2014-10-20 20:27:09 +0200 | [diff] [blame] | 107 | " str %4, [%0, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t" |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 108 | /* Disable SDRAM1 accesses */ |
| 109 | " strne %3, [%1, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t" |
| 110 | /* Power down SDRAM1 */ |
Alexandre Belloni | 7cb4e71 | 2014-10-20 20:27:09 +0200 | [diff] [blame] | 111 | " strne %4, [%1, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t" |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 112 | /* Reset CPU */ |
| 113 | " str %5, [%2, #" __stringify(AT91_RSTC_CR) "]\n\t" |
| 114 | |
| 115 | " b .\n\t" |
| 116 | : |
| 117 | : "r" (at91_ramc_base[0]), |
| 118 | "r" (at91_ramc_base[1]), |
| 119 | "r" (at91_rstc_base), |
| 120 | "r" (1), |
Ben Dooks | 7be5ac2 | 2015-03-26 14:16:22 +0000 | [diff] [blame] | 121 | "r" cpu_to_le32(AT91_DDRSDRC_LPCB_POWER_DOWN), |
| 122 | "r" cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST) |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 123 | : "r0"); |
Guenter Roeck | 481ff6f | 2015-01-25 12:30:41 -0800 | [diff] [blame] | 124 | |
| 125 | return NOTIFY_DONE; |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Josh Wu | 1ae25d6 | 2015-07-20 17:32:05 +0800 | [diff] [blame] | 128 | static int sama5d3_restart(struct notifier_block *this, unsigned long mode, |
| 129 | void *cmd) |
| 130 | { |
| 131 | writel(cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST), |
| 132 | at91_rstc_base); |
| 133 | |
| 134 | return NOTIFY_DONE; |
| 135 | } |
| 136 | |
Szemző András | f22dfd8 | 2017-01-18 00:07:37 +0100 | [diff] [blame] | 137 | static int samx7_restart(struct notifier_block *this, unsigned long mode, |
| 138 | void *cmd) |
| 139 | { |
| 140 | writel(cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PROCRST), |
| 141 | at91_rstc_base); |
| 142 | |
| 143 | return NOTIFY_DONE; |
| 144 | } |
| 145 | |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 146 | static void __init at91_reset_status(struct platform_device *pdev) |
| 147 | { |
| 148 | u32 reg = readl(at91_rstc_base + AT91_RSTC_SR); |
| 149 | char *reason; |
| 150 | |
| 151 | switch ((reg & AT91_RSTC_RSTTYP) >> 8) { |
| 152 | case RESET_TYPE_GENERAL: |
| 153 | reason = "general reset"; |
| 154 | break; |
| 155 | case RESET_TYPE_WAKEUP: |
| 156 | reason = "wakeup"; |
| 157 | break; |
| 158 | case RESET_TYPE_WATCHDOG: |
| 159 | reason = "watchdog reset"; |
| 160 | break; |
| 161 | case RESET_TYPE_SOFTWARE: |
| 162 | reason = "software reset"; |
| 163 | break; |
| 164 | case RESET_TYPE_USER: |
| 165 | reason = "user reset"; |
| 166 | break; |
| 167 | default: |
| 168 | reason = "unknown reset"; |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | pr_info("AT91: Starting after %s\n", reason); |
| 173 | } |
| 174 | |
Fabian Frederick | 8fb0885 | 2015-03-16 20:17:12 +0100 | [diff] [blame] | 175 | static const struct of_device_id at91_ramc_of_match[] = { |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 176 | { .compatible = "atmel,at91sam9260-sdramc", }, |
| 177 | { .compatible = "atmel,at91sam9g45-ddramc", }, |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 178 | { /* sentinel */ } |
| 179 | }; |
| 180 | |
Fabian Frederick | 8fb0885 | 2015-03-16 20:17:12 +0100 | [diff] [blame] | 181 | static const struct of_device_id at91_reset_of_match[] = { |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 182 | { .compatible = "atmel,at91sam9260-rstc", .data = at91sam9260_restart }, |
| 183 | { .compatible = "atmel,at91sam9g45-rstc", .data = at91sam9g45_restart }, |
Josh Wu | 1ae25d6 | 2015-07-20 17:32:05 +0800 | [diff] [blame] | 184 | { .compatible = "atmel,sama5d3-rstc", .data = sama5d3_restart }, |
Szemző András | f22dfd8 | 2017-01-18 00:07:37 +0100 | [diff] [blame] | 185 | { .compatible = "atmel,samx7-rstc", .data = samx7_restart }, |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 186 | { /* sentinel */ } |
| 187 | }; |
Javier Martinez Canillas | 991de44 | 2016-10-17 15:36:12 -0300 | [diff] [blame] | 188 | MODULE_DEVICE_TABLE(of, at91_reset_of_match); |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 189 | |
Guenter Roeck | 481ff6f | 2015-01-25 12:30:41 -0800 | [diff] [blame] | 190 | static struct notifier_block at91_restart_nb = { |
| 191 | .priority = 192, |
| 192 | }; |
| 193 | |
Alexandre Belloni | 6e64180 | 2015-08-11 11:12:47 +0200 | [diff] [blame] | 194 | static int __init at91_reset_probe(struct platform_device *pdev) |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 195 | { |
| 196 | const struct of_device_id *match; |
| 197 | struct device_node *np; |
Alexandre Belloni | eacd8d0 | 2015-08-11 11:12:46 +0200 | [diff] [blame] | 198 | int ret, idx = 0; |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 199 | |
| 200 | at91_rstc_base = of_iomap(pdev->dev.of_node, 0); |
| 201 | if (!at91_rstc_base) { |
| 202 | dev_err(&pdev->dev, "Could not map reset controller address\n"); |
| 203 | return -ENODEV; |
| 204 | } |
| 205 | |
Josh Wu | 1ae25d6 | 2015-07-20 17:32:05 +0800 | [diff] [blame] | 206 | if (!of_device_is_compatible(pdev->dev.of_node, "atmel,sama5d3-rstc")) { |
| 207 | /* we need to shutdown the ddr controller, so get ramc base */ |
| 208 | for_each_matching_node(np, at91_ramc_of_match) { |
| 209 | at91_ramc_base[idx] = of_iomap(np, 0); |
| 210 | if (!at91_ramc_base[idx]) { |
| 211 | dev_err(&pdev->dev, "Could not map ram controller address\n"); |
Julia Lawall | c4c0edf | 2015-11-18 23:04:14 +0100 | [diff] [blame] | 212 | of_node_put(np); |
Josh Wu | 1ae25d6 | 2015-07-20 17:32:05 +0800 | [diff] [blame] | 213 | return -ENODEV; |
| 214 | } |
| 215 | idx++; |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 216 | } |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | match = of_match_node(at91_reset_of_match, pdev->dev.of_node); |
Guenter Roeck | 481ff6f | 2015-01-25 12:30:41 -0800 | [diff] [blame] | 220 | at91_restart_nb.notifier_call = match->data; |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 221 | |
Alexandre Belloni | 2b2c614 | 2015-08-11 11:12:48 +0200 | [diff] [blame] | 222 | sclk = devm_clk_get(&pdev->dev, NULL); |
| 223 | if (IS_ERR(sclk)) |
| 224 | return PTR_ERR(sclk); |
| 225 | |
| 226 | ret = clk_prepare_enable(sclk); |
| 227 | if (ret) { |
| 228 | dev_err(&pdev->dev, "Could not enable slow clock\n"); |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 229 | return ret; |
Alexandre Belloni | 2b2c614 | 2015-08-11 11:12:48 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | ret = register_restart_handler(&at91_restart_nb); |
| 233 | if (ret) { |
| 234 | clk_disable_unprepare(sclk); |
| 235 | return ret; |
| 236 | } |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 237 | |
| 238 | at91_reset_status(pdev); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Alexandre Belloni | 6e64180 | 2015-08-11 11:12:47 +0200 | [diff] [blame] | 243 | static int __exit at91_reset_remove(struct platform_device *pdev) |
| 244 | { |
| 245 | unregister_restart_handler(&at91_restart_nb); |
Alexandre Belloni | 2b2c614 | 2015-08-11 11:12:48 +0200 | [diff] [blame] | 246 | clk_disable_unprepare(sclk); |
Alexandre Belloni | 6e64180 | 2015-08-11 11:12:47 +0200 | [diff] [blame] | 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 251 | static struct platform_driver at91_reset_driver = { |
Alexandre Belloni | 6e64180 | 2015-08-11 11:12:47 +0200 | [diff] [blame] | 252 | .remove = __exit_p(at91_reset_remove), |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 253 | .driver = { |
| 254 | .name = "at91-reset", |
| 255 | .of_match_table = at91_reset_of_match, |
| 256 | }, |
Maxime Ripard | ecfe64d | 2014-07-02 17:46:58 +0200 | [diff] [blame] | 257 | }; |
Alexandre Belloni | 6e64180 | 2015-08-11 11:12:47 +0200 | [diff] [blame] | 258 | module_platform_driver_probe(at91_reset_driver, at91_reset_probe); |
| 259 | |
| 260 | MODULE_AUTHOR("Atmel Corporation"); |
| 261 | MODULE_DESCRIPTION("Reset driver for Atmel SoCs"); |
| 262 | MODULE_LICENSE("GPL v2"); |