Deleted Added
full compact
at91_rst.c (213498) at91_rst.c (234281)
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 <sys/cdefs.h>
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 <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/sys/arm/at91/at91_rst.c 213498 2010-10-06 22:40:27Z cognet $");
27__FBSDID("$FreeBSD: head/sys/arm/at91/at91_rst.c 234281 2012-04-14 11:29:32Z marius $");
28
29#include <sys/param.h>
30#include <sys/bus.h>
31#include <sys/kernel.h>
32#include <sys/module.h>
33#include <sys/rman.h>
34#include <sys/systm.h>
35
36#include <machine/bus.h>
37
38#include <arm/at91/at91var.h>
39#include <arm/at91/at91_rstreg.h>
40#include <arm/at91/at91board.h>
41
42#define RST_TIMEOUT (5) /* Seconds to hold NRST for hard reset */
28
29#include <sys/param.h>
30#include <sys/bus.h>
31#include <sys/kernel.h>
32#include <sys/module.h>
33#include <sys/rman.h>
34#include <sys/systm.h>
35
36#include <machine/bus.h>
37
38#include <arm/at91/at91var.h>
39#include <arm/at91/at91_rstreg.h>
40#include <arm/at91/at91board.h>
41
42#define RST_TIMEOUT (5) /* Seconds to hold NRST for hard reset */
43#define RST_TICK (20) /* sample NRST at hz/RST_TICK intervals */
43#define RST_TICK (20) /* sample NRST at hz/RST_TICK intervals */
44
45static int rst_intr(void *arg);
46
47static struct rst_softc {
48 struct resource *mem_res; /* Memory resource */
49 struct resource *irq_res; /* IRQ resource */
50 void *intrhand; /* Interrupt handle */
51 struct callout tick_ch; /* Tick callout */
52 device_t sc_dev;
53 u_int shutdown; /* Shutdown in progress */
54} *rst_sc;
55
56static inline uint32_t
57RD4(struct rst_softc *sc, bus_size_t off)
58{
59
60 return (bus_read_4(sc->mem_res, off));
61}
62
63static inline void
64WR4(struct rst_softc *sc, bus_size_t off, uint32_t val)
65{
66
67 bus_write_4(sc->mem_res, off, val);
68}
69
70static int
71at91_rst_probe(device_t dev)
72{
73
74 if (at91_is_sam9()) {
75 device_set_desc(dev, "AT91SAM9 Reset Controller");
76 return (0);
77 }
78 return (ENXIO);
79}
80
81static int
82at91_rst_attach(device_t dev)
83{
84 struct rst_softc *sc;
85 const char *cause;
86 int rid, err;
87
88 rst_sc = sc = device_get_softc(dev);
89 sc->sc_dev = dev;
90
91 callout_init(&sc->tick_ch, 0);
92
93 rid = 0;
94 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
95 RF_ACTIVE);
96 if (sc->mem_res == NULL) {
97 device_printf(dev, "could not allocate memory resources.\n");
98 err = ENOMEM;
99 goto out;
100 }
101 rid = 0;
102 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
103 RF_ACTIVE | RF_SHAREABLE);
104 if (sc->irq_res == NULL) {
105 device_printf(dev, "could not allocate interrupt resources.\n");
106 err = ENOMEM;
107 goto out;
108 }
109
110 /* Activate the interrupt. */
111 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
112 rst_intr, NULL, sc, &sc->intrhand);
113 if (err)
114 device_printf(dev, "could not establish interrupt handler.\n");
115
116 WR4(rst_sc, RST_MR, RST_MR_ERSTL(0xd) | RST_MR_URSIEN | RST_MR_KEY);
117
118 switch (RD4(sc, RST_SR) & RST_SR_RST_MASK) {
119 case RST_SR_RST_POW:
120 cause = "Power On";
121 break;
122 case RST_SR_RST_WAKE:
123 cause = "Wake Up";
124 break;
44
45static int rst_intr(void *arg);
46
47static struct rst_softc {
48 struct resource *mem_res; /* Memory resource */
49 struct resource *irq_res; /* IRQ resource */
50 void *intrhand; /* Interrupt handle */
51 struct callout tick_ch; /* Tick callout */
52 device_t sc_dev;
53 u_int shutdown; /* Shutdown in progress */
54} *rst_sc;
55
56static inline uint32_t
57RD4(struct rst_softc *sc, bus_size_t off)
58{
59
60 return (bus_read_4(sc->mem_res, off));
61}
62
63static inline void
64WR4(struct rst_softc *sc, bus_size_t off, uint32_t val)
65{
66
67 bus_write_4(sc->mem_res, off, val);
68}
69
70static int
71at91_rst_probe(device_t dev)
72{
73
74 if (at91_is_sam9()) {
75 device_set_desc(dev, "AT91SAM9 Reset Controller");
76 return (0);
77 }
78 return (ENXIO);
79}
80
81static int
82at91_rst_attach(device_t dev)
83{
84 struct rst_softc *sc;
85 const char *cause;
86 int rid, err;
87
88 rst_sc = sc = device_get_softc(dev);
89 sc->sc_dev = dev;
90
91 callout_init(&sc->tick_ch, 0);
92
93 rid = 0;
94 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
95 RF_ACTIVE);
96 if (sc->mem_res == NULL) {
97 device_printf(dev, "could not allocate memory resources.\n");
98 err = ENOMEM;
99 goto out;
100 }
101 rid = 0;
102 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
103 RF_ACTIVE | RF_SHAREABLE);
104 if (sc->irq_res == NULL) {
105 device_printf(dev, "could not allocate interrupt resources.\n");
106 err = ENOMEM;
107 goto out;
108 }
109
110 /* Activate the interrupt. */
111 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
112 rst_intr, NULL, sc, &sc->intrhand);
113 if (err)
114 device_printf(dev, "could not establish interrupt handler.\n");
115
116 WR4(rst_sc, RST_MR, RST_MR_ERSTL(0xd) | RST_MR_URSIEN | RST_MR_KEY);
117
118 switch (RD4(sc, RST_SR) & RST_SR_RST_MASK) {
119 case RST_SR_RST_POW:
120 cause = "Power On";
121 break;
122 case RST_SR_RST_WAKE:
123 cause = "Wake Up";
124 break;
125 case RST_SR_RST_WDT:
125 case RST_SR_RST_WDT:
126 cause = "Watchdog";
127 break;
128 case RST_SR_RST_SOFT:
129 cause = "Software Request";
130 break;
131 case RST_SR_RST_USR:
132 cause = "External (User)";
133 break;
134 default:
135 cause = "Unknown";
136 break;
137 }
138
139 device_printf(dev, "Reset cause: %s.\n", cause);
140 /* cpu_reset_addr = cpu_reset; */
141
142out:
143 return (err);
144}
145
146static void
147rst_tick(void *argp)
148{
149 struct rst_softc *sc = argp;
150
151 if (sc->shutdown++ >= RST_TIMEOUT * RST_TICK) {
152 /* User released the button in morre than RST_TIMEOUT */
153 cpu_reset();
154 } else if ((RD4(sc, RST_SR) & RST_SR_NRSTL)) {
155 /* User released the button in less than RST_TIMEOUT */
126 cause = "Watchdog";
127 break;
128 case RST_SR_RST_SOFT:
129 cause = "Software Request";
130 break;
131 case RST_SR_RST_USR:
132 cause = "External (User)";
133 break;
134 default:
135 cause = "Unknown";
136 break;
137 }
138
139 device_printf(dev, "Reset cause: %s.\n", cause);
140 /* cpu_reset_addr = cpu_reset; */
141
142out:
143 return (err);
144}
145
146static void
147rst_tick(void *argp)
148{
149 struct rst_softc *sc = argp;
150
151 if (sc->shutdown++ >= RST_TIMEOUT * RST_TICK) {
152 /* User released the button in morre than RST_TIMEOUT */
153 cpu_reset();
154 } else if ((RD4(sc, RST_SR) & RST_SR_NRSTL)) {
155 /* User released the button in less than RST_TIMEOUT */
156 sc->shutdown = 0;
156 sc->shutdown = 0;
157 device_printf(sc->sc_dev, "shutting down...\n");
158 shutdown_nice(0);
159 } else {
160 callout_reset(&sc->tick_ch, hz/RST_TICK, rst_tick, sc);
161 }
162}
163
164static int
165rst_intr(void *argp)
166{
167 struct rst_softc *sc = argp;
168
169 if (RD4(sc, RST_SR) & RST_SR_URSTS) {
157 device_printf(sc->sc_dev, "shutting down...\n");
158 shutdown_nice(0);
159 } else {
160 callout_reset(&sc->tick_ch, hz/RST_TICK, rst_tick, sc);
161 }
162}
163
164static int
165rst_intr(void *argp)
166{
167 struct rst_softc *sc = argp;
168
169 if (RD4(sc, RST_SR) & RST_SR_URSTS) {
170 if (sc->shutdown == 0)
170 if (sc->shutdown == 0)
171 callout_reset(&sc->tick_ch, hz/RST_TICK, rst_tick, sc);
172 return (FILTER_HANDLED);
173 }
174 return (FILTER_STRAY);
175}
176
177static device_method_t at91_rst_methods[] = {
178 DEVMETHOD(device_probe, at91_rst_probe),
179 DEVMETHOD(device_attach, at91_rst_attach),
171 callout_reset(&sc->tick_ch, hz/RST_TICK, rst_tick, sc);
172 return (FILTER_HANDLED);
173 }
174 return (FILTER_STRAY);
175}
176
177static device_method_t at91_rst_methods[] = {
178 DEVMETHOD(device_probe, at91_rst_probe),
179 DEVMETHOD(device_attach, at91_rst_attach),
180 {0,0},
180 DEVMETHOD_END
181};
182
183static driver_t at91_rst_driver = {
184 "at91_rst",
185 at91_rst_methods,
186 sizeof(struct rst_softc),
187};
188
189static devclass_t at91_rst_devclass;
190
181};
182
183static driver_t at91_rst_driver = {
184 "at91_rst",
185 at91_rst_methods,
186 sizeof(struct rst_softc),
187};
188
189static devclass_t at91_rst_devclass;
190
191DRIVER_MODULE(at91_rst, atmelarm, at91_rst_driver, at91_rst_devclass, 0, 0);
191DRIVER_MODULE(at91_rst, atmelarm, at91_rst_driver, at91_rst_devclass, NULL,
192 NULL);
192
193void cpu_reset_sam9g20(void) __attribute__((weak));
194void cpu_reset_sam9g20(void) {}
195
196void
197cpu_reset(void)
198{
199
200 if (rst_sc) {
193
194void cpu_reset_sam9g20(void) __attribute__((weak));
195void cpu_reset_sam9g20(void) {}
196
197void
198cpu_reset(void)
199{
200
201 if (rst_sc) {
201
202 cpu_reset_sam9g20(); /* May be null */
203
204 WR4(rst_sc, RST_MR,
205 RST_MR_ERSTL(0xd) | RST_MR_URSTEN | RST_MR_KEY);
206
207 WR4(rst_sc, RST_CR,
208 RST_CR_PROCRST |
209 RST_CR_PERRST |
210 RST_CR_EXTRST |
211 RST_CR_KEY);
212 }
213
202 cpu_reset_sam9g20(); /* May be null */
203
204 WR4(rst_sc, RST_MR,
205 RST_MR_ERSTL(0xd) | RST_MR_URSTEN | RST_MR_KEY);
206
207 WR4(rst_sc, RST_CR,
208 RST_CR_PROCRST |
209 RST_CR_PERRST |
210 RST_CR_EXTRST |
211 RST_CR_KEY);
212 }
213
214 for(;;) ;
214 for(;;)
215 ;
215}
216}