power: reset: at91-reset: devm_kzalloc() for at91_reset data structure
[platform/kernel/linux-rpi.git] / drivers / power / reset / at91-reset.c
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
14 #include <linux/clk.h>
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
21 #include <soc/at91/at91sam9_ddrsdr.h>
22 #include <soc/at91/at91sam9_sdramc.h>
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         RESET_TYPE_CPU_FAIL     = 6,
48         RESET_TYPE_XTAL_FAIL    = 7,
49         RESET_TYPE_ULP2         = 8,
50 };
51
52 struct at91_reset {
53         void __iomem *rstc_base;
54         void __iomem *ramc_base[2];
55         struct clk *sclk;
56         struct notifier_block nb;
57 };
58
59 /*
60 * unless the SDRAM is cleanly shutdown before we hit the
61 * reset register it can be left driving the data bus and
62 * killing the chance of a subsequent boot from NAND
63 */
64 static int at91sam9260_restart(struct notifier_block *this, unsigned long mode,
65                                void *cmd)
66 {
67         struct at91_reset *reset = container_of(this, struct at91_reset, nb);
68
69         asm volatile(
70                 /* Align to cache lines */
71                 ".balign 32\n\t"
72
73                 /* Disable SDRAM accesses */
74                 "str    %2, [%0, #" __stringify(AT91_SDRAMC_TR) "]\n\t"
75
76                 /* Power down SDRAM */
77                 "str    %3, [%0, #" __stringify(AT91_SDRAMC_LPR) "]\n\t"
78
79                 /* Reset CPU */
80                 "str    %4, [%1, #" __stringify(AT91_RSTC_CR) "]\n\t"
81
82                 "b      .\n\t"
83                 :
84                 : "r" (reset->ramc_base[0]),
85                   "r" (reset->rstc_base),
86                   "r" (1),
87                   "r" cpu_to_le32(AT91_SDRAMC_LPCB_POWER_DOWN),
88                   "r" cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST));
89
90         return NOTIFY_DONE;
91 }
92
93 static int at91sam9g45_restart(struct notifier_block *this, unsigned long mode,
94                                void *cmd)
95 {
96         struct at91_reset *reset = container_of(this, struct at91_reset, nb);
97
98         asm volatile(
99                 /*
100                  * Test wether we have a second RAM controller to care
101                  * about.
102                  *
103                  * First, test that we can dereference the virtual address.
104                  */
105                 "cmp    %1, #0\n\t"
106                 "beq    1f\n\t"
107
108                 /* Then, test that the RAM controller is enabled */
109                 "ldr    r0, [%1]\n\t"
110                 "cmp    r0, #0\n\t"
111
112                 /* Align to cache lines */
113                 ".balign 32\n\t"
114
115                 /* Disable SDRAM0 accesses */
116                 "1:     str     %3, [%0, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
117                 /* Power down SDRAM0 */
118                 "       str     %4, [%0, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
119                 /* Disable SDRAM1 accesses */
120                 "       strne   %3, [%1, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t"
121                 /* Power down SDRAM1 */
122                 "       strne   %4, [%1, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
123                 /* Reset CPU */
124                 "       str     %5, [%2, #" __stringify(AT91_RSTC_CR) "]\n\t"
125
126                 "       b       .\n\t"
127                 :
128                 : "r" (reset->ramc_base[0]),
129                   "r" (reset->ramc_base[1]),
130                   "r" (reset->rstc_base),
131                   "r" (1),
132                   "r" cpu_to_le32(AT91_DDRSDRC_LPCB_POWER_DOWN),
133                   "r" cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST)
134                 : "r0");
135
136         return NOTIFY_DONE;
137 }
138
139 static int sama5d3_restart(struct notifier_block *this, unsigned long mode,
140                            void *cmd)
141 {
142         struct at91_reset *reset = container_of(this, struct at91_reset, nb);
143
144         writel(cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PERRST | AT91_RSTC_PROCRST),
145                reset->rstc_base);
146
147         return NOTIFY_DONE;
148 }
149
150 static int samx7_restart(struct notifier_block *this, unsigned long mode,
151                          void *cmd)
152 {
153         struct at91_reset *reset = container_of(this, struct at91_reset, nb);
154
155         writel(cpu_to_le32(AT91_RSTC_KEY | AT91_RSTC_PROCRST),
156                reset->rstc_base);
157
158         return NOTIFY_DONE;
159 }
160
161 static void __init at91_reset_status(struct platform_device *pdev,
162                                      void __iomem *base)
163 {
164         const char *reason;
165         u32 reg = readl(base + AT91_RSTC_SR);
166
167         switch ((reg & AT91_RSTC_RSTTYP) >> 8) {
168         case RESET_TYPE_GENERAL:
169                 reason = "general reset";
170                 break;
171         case RESET_TYPE_WAKEUP:
172                 reason = "wakeup";
173                 break;
174         case RESET_TYPE_WATCHDOG:
175                 reason = "watchdog reset";
176                 break;
177         case RESET_TYPE_SOFTWARE:
178                 reason = "software reset";
179                 break;
180         case RESET_TYPE_USER:
181                 reason = "user reset";
182                 break;
183         case RESET_TYPE_CPU_FAIL:
184                 reason = "CPU clock failure detection";
185                 break;
186         case RESET_TYPE_XTAL_FAIL:
187                 reason = "32.768 kHz crystal failure detection";
188                 break;
189         case RESET_TYPE_ULP2:
190                 reason = "ULP2 reset";
191                 break;
192         default:
193                 reason = "unknown reset";
194                 break;
195         }
196
197         dev_info(&pdev->dev, "Starting after %s\n", reason);
198 }
199
200 static const struct of_device_id at91_ramc_of_match[] = {
201         { .compatible = "atmel,at91sam9260-sdramc", },
202         { .compatible = "atmel,at91sam9g45-ddramc", },
203         { /* sentinel */ }
204 };
205
206 static const struct of_device_id at91_reset_of_match[] = {
207         { .compatible = "atmel,at91sam9260-rstc", .data = at91sam9260_restart },
208         { .compatible = "atmel,at91sam9g45-rstc", .data = at91sam9g45_restart },
209         { .compatible = "atmel,sama5d3-rstc", .data = sama5d3_restart },
210         { .compatible = "atmel,samx7-rstc", .data = samx7_restart },
211         { .compatible = "microchip,sam9x60-rstc", .data = samx7_restart },
212         { /* sentinel */ }
213 };
214 MODULE_DEVICE_TABLE(of, at91_reset_of_match);
215
216 static int __init at91_reset_probe(struct platform_device *pdev)
217 {
218         const struct of_device_id *match;
219         struct at91_reset *reset;
220         struct device_node *np;
221         int ret, idx = 0;
222
223         reset = devm_kzalloc(&pdev->dev, sizeof(*reset), GFP_KERNEL);
224         if (!reset)
225                 return -ENOMEM;
226
227         reset->rstc_base = of_iomap(pdev->dev.of_node, 0);
228         if (!reset->rstc_base) {
229                 dev_err(&pdev->dev, "Could not map reset controller address\n");
230                 return -ENODEV;
231         }
232
233         if (!of_device_is_compatible(pdev->dev.of_node, "atmel,sama5d3-rstc")) {
234                 /* we need to shutdown the ddr controller, so get ramc base */
235                 for_each_matching_node(np, at91_ramc_of_match) {
236                         reset->ramc_base[idx] = of_iomap(np, 0);
237                         if (!reset->ramc_base[idx]) {
238                                 dev_err(&pdev->dev, "Could not map ram controller address\n");
239                                 of_node_put(np);
240                                 return -ENODEV;
241                         }
242                         idx++;
243                 }
244         }
245
246         match = of_match_node(at91_reset_of_match, pdev->dev.of_node);
247         reset->nb.notifier_call = match->data;
248         reset->nb.priority = 192;
249
250         reset->sclk = devm_clk_get(&pdev->dev, NULL);
251         if (IS_ERR(reset->sclk))
252                 return PTR_ERR(reset->sclk);
253
254         ret = clk_prepare_enable(reset->sclk);
255         if (ret) {
256                 dev_err(&pdev->dev, "Could not enable slow clock\n");
257                 return ret;
258         }
259
260         platform_set_drvdata(pdev, reset);
261
262         ret = register_restart_handler(&reset->nb);
263         if (ret) {
264                 clk_disable_unprepare(reset->sclk);
265                 return ret;
266         }
267
268         at91_reset_status(pdev, reset->rstc_base);
269
270         return 0;
271 }
272
273 static int __exit at91_reset_remove(struct platform_device *pdev)
274 {
275         struct at91_reset *reset = platform_get_drvdata(pdev);
276
277         unregister_restart_handler(&reset->nb);
278         clk_disable_unprepare(reset->sclk);
279
280         return 0;
281 }
282
283 static struct platform_driver at91_reset_driver = {
284         .remove = __exit_p(at91_reset_remove),
285         .driver = {
286                 .name = "at91-reset",
287                 .of_match_table = at91_reset_of_match,
288         },
289 };
290 module_platform_driver_probe(at91_reset_driver, at91_reset_probe);
291
292 MODULE_AUTHOR("Atmel Corporation");
293 MODULE_DESCRIPTION("Reset driver for Atmel SoCs");
294 MODULE_LICENSE("GPL v2");