]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/at91_rst.c
Merge compiler-rt trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / sys / arm / at91 / at91_rst.c
1 /*-
2  * Copyright (c) 2010 Greg Ansley.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 #include "opt_platform.h"
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/rman.h>
36 #include <sys/systm.h>
37
38 #include <machine/bus.h>
39
40 #include <arm/at91/at91var.h>
41 #include <arm/at91/at91_rstreg.h>
42 #include <arm/at91/at91board.h>
43
44 #ifdef FDT
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #define FDT_HACKS 1
48 #endif
49
50 #define RST_TIMEOUT (5) /* Seconds to hold NRST for hard reset */
51 #define RST_TICK (20)   /* sample NRST at hz/RST_TICK intervals */
52
53 #ifndef FDT
54 static int at91_rst_intr(void *arg);
55 #endif
56
57 static struct at91_rst_softc {
58         struct resource *mem_res;       /* Memory resource */
59         struct resource *irq_res;       /* IRQ resource */
60         void            *intrhand;      /* Interrupt handle */
61         struct callout  tick_ch;        /* Tick callout */
62         device_t        sc_dev;
63         u_int           shutdown;       /* Shutdown in progress */
64 } *at91_rst_sc;
65
66 static inline uint32_t
67 RD4(struct at91_rst_softc *sc, bus_size_t off)
68 {
69
70         return (bus_read_4(sc->mem_res, off));
71 }
72
73 static inline void
74 WR4(struct at91_rst_softc *sc, bus_size_t off, uint32_t val)
75 {
76
77         bus_write_4(sc->mem_res, off, val);
78 }
79
80 void cpu_reset_sam9g20(void) __attribute__((weak));
81 void cpu_reset_sam9g20(void) {}
82
83 void
84 at91_rst_cpu_reset(void)
85 {
86
87         if (at91_rst_sc) {
88                 cpu_reset_sam9g20(); /* May be null */
89
90                 WR4(at91_rst_sc, RST_MR,
91                     RST_MR_ERSTL(0xd) | RST_MR_URSTEN | RST_MR_KEY);
92
93                 WR4(at91_rst_sc, RST_CR,
94                     RST_CR_PROCRST |
95                     RST_CR_PERRST  |
96                     RST_CR_EXTRST  |
97                     RST_CR_KEY);
98         }
99         while(1)
100                 continue;
101 }
102
103 static int
104 at91_rst_probe(device_t dev)
105 {
106 #ifdef FDT
107         if (!ofw_bus_is_compatible(dev, "atmel,at91sam9260-rstc"))
108                 return (ENXIO);
109 #endif
110
111         device_set_desc(dev, "AT91SAM9 Reset Controller");
112         return (0);
113 }
114
115 static int
116 at91_rst_attach(device_t dev)
117 {
118         struct at91_rst_softc *sc;
119         const char *cause;
120         int rid, err = 0;
121
122         at91_rst_sc = sc = device_get_softc(dev);
123         sc->sc_dev = dev;
124
125         callout_init(&sc->tick_ch, 0);
126
127         rid = 0;
128         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
129             RF_ACTIVE);
130         if (sc->mem_res == NULL) {
131                 device_printf(dev, "could not allocate memory resources.\n");
132                 err = ENOMEM;
133                 goto out;
134         }
135
136 #ifndef FDT_HACKS
137         rid = 0;
138         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
139             RF_ACTIVE | RF_SHAREABLE);
140         if (sc->irq_res == NULL) {
141                 device_printf(dev, "could not allocate interrupt resources.\n");
142                 err = ENOMEM;
143                 goto out;
144         }
145
146         /* Activate the interrupt. */
147         err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
148             at91_rst_intr, NULL, sc, &sc->intrhand);
149         if (err)
150                 device_printf(dev, "could not establish interrupt handler.\n");
151 #endif
152
153         WR4(at91_rst_sc, RST_MR, RST_MR_ERSTL(0xd) | RST_MR_URSIEN | RST_MR_KEY);
154
155         switch (RD4(sc, RST_SR) & RST_SR_RST_MASK) {
156                 case    RST_SR_RST_POW:
157                         cause = "Power On";
158                         break;
159                 case    RST_SR_RST_WAKE:
160                         cause = "Wake Up";
161                         break;
162                 case    RST_SR_RST_WDT:
163                         cause = "Watchdog";
164                         break;
165                 case    RST_SR_RST_SOFT:
166                         cause = "Software Request";
167                         break;
168                 case    RST_SR_RST_USR:
169                         cause = "External (User)";
170                         break;
171                 default:
172                         cause = "Unknown";
173                         break;
174         }
175
176         device_printf(dev, "Reset cause: %s.\n", cause);
177 out:
178         return (err);
179 }
180
181 #ifndef FDT_HACKS
182 static void
183 at91_rst_tick(void *argp)
184 {
185         struct at91_rst_softc *sc = argp;
186
187         if (sc->shutdown++ >= RST_TIMEOUT * RST_TICK) {
188                 /* User released the button in morre than RST_TIMEOUT */
189                 cpu_reset();
190         } else if ((RD4(sc, RST_SR) & RST_SR_NRSTL)) {
191                 /* User released the button in less than RST_TIMEOUT */
192                 sc->shutdown = 0;
193                 device_printf(sc->sc_dev, "shutting down...\n");
194                 shutdown_nice(0);
195         } else {
196                 callout_reset(&sc->tick_ch, hz/RST_TICK, at91_rst_tick, sc);
197         }
198 }
199
200 static int
201 at91_rst_intr(void *argp)
202 {
203         struct at91_rst_softc *sc = argp;
204
205         if (RD4(sc, RST_SR) & RST_SR_URSTS) {
206                 if (sc->shutdown == 0)
207                         callout_reset(&sc->tick_ch, hz/RST_TICK, at91_rst_tick, sc);
208                 return (FILTER_HANDLED);
209         }
210         return (FILTER_STRAY);
211 }
212 #endif
213
214 static device_method_t at91_rst_methods[] = {
215         DEVMETHOD(device_probe, at91_rst_probe),
216         DEVMETHOD(device_attach, at91_rst_attach),
217         DEVMETHOD_END
218 };
219
220 static driver_t at91_rst_driver = {
221         "at91_rst",
222         at91_rst_methods,
223         sizeof(struct at91_rst_softc),
224 };
225
226 static devclass_t at91_rst_devclass;
227
228 #ifdef FDT
229 DRIVER_MODULE(at91_rst, simplebus, at91_rst_driver, at91_rst_devclass, NULL,
230     NULL);
231 #else
232 DRIVER_MODULE(at91_rst, atmelarm, at91_rst_driver, at91_rst_devclass, NULL,
233     NULL);
234 #endif