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
| 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
|
26#include <sys/cdefs.h>
| 28#include <sys/cdefs.h>
|
27__FBSDID("$FreeBSD: stable/10/sys/arm/at91/at91_rst.c 238369 2012-07-11 17:11:07Z imp $");
| 29__FBSDID("$FreeBSD: stable/10/sys/arm/at91/at91_rst.c 266196 2014-05-15 21:21:47Z ian $");
|
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
| 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/fdt/fdt_common.h> 46#include <dev/ofw/ofw_bus.h> 47#include <dev/ofw/ofw_bus_subr.h> 48#define FDT_HACKS 1 49#endif 50
|
42#define RST_TIMEOUT (5) /* Seconds to hold NRST for hard reset */ 43#define RST_TICK (20) /* sample NRST at hz/RST_TICK intervals */ 44
| 51#define RST_TIMEOUT (5) /* Seconds to hold NRST for hard reset */ 52#define RST_TICK (20) /* sample NRST at hz/RST_TICK intervals */ 53
|
| 54#ifndef FDT
|
45static int at91_rst_intr(void *arg);
| 55static int at91_rst_intr(void *arg);
|
| 56#endif
|
46 47static struct at91_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} *at91_rst_sc; 55 56static inline uint32_t 57RD4(struct at91_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 at91_rst_softc *sc, bus_size_t off, uint32_t val) 65{ 66 67 bus_write_4(sc->mem_res, off, val); 68} 69 70void cpu_reset_sam9g20(void) __attribute__((weak)); 71void cpu_reset_sam9g20(void) {} 72 73void 74at91_rst_cpu_reset(void) 75{ 76 77 if (at91_rst_sc) { 78 cpu_reset_sam9g20(); /* May be null */ 79 80 WR4(at91_rst_sc, RST_MR, 81 RST_MR_ERSTL(0xd) | RST_MR_URSTEN | RST_MR_KEY); 82 83 WR4(at91_rst_sc, RST_CR, 84 RST_CR_PROCRST | 85 RST_CR_PERRST | 86 RST_CR_EXTRST | 87 RST_CR_KEY); 88 } 89 while(1) 90 continue; 91} 92 93static int 94at91_rst_probe(device_t dev) 95{
| 57 58static struct at91_rst_softc { 59 struct resource *mem_res; /* Memory resource */ 60 struct resource *irq_res; /* IRQ resource */ 61 void *intrhand; /* Interrupt handle */ 62 struct callout tick_ch; /* Tick callout */ 63 device_t sc_dev; 64 u_int shutdown; /* Shutdown in progress */ 65} *at91_rst_sc; 66 67static inline uint32_t 68RD4(struct at91_rst_softc *sc, bus_size_t off) 69{ 70 71 return (bus_read_4(sc->mem_res, off)); 72} 73 74static inline void 75WR4(struct at91_rst_softc *sc, bus_size_t off, uint32_t val) 76{ 77 78 bus_write_4(sc->mem_res, off, val); 79} 80 81void cpu_reset_sam9g20(void) __attribute__((weak)); 82void cpu_reset_sam9g20(void) {} 83 84void 85at91_rst_cpu_reset(void) 86{ 87 88 if (at91_rst_sc) { 89 cpu_reset_sam9g20(); /* May be null */ 90 91 WR4(at91_rst_sc, RST_MR, 92 RST_MR_ERSTL(0xd) | RST_MR_URSTEN | RST_MR_KEY); 93 94 WR4(at91_rst_sc, RST_CR, 95 RST_CR_PROCRST | 96 RST_CR_PERRST | 97 RST_CR_EXTRST | 98 RST_CR_KEY); 99 } 100 while(1) 101 continue; 102} 103 104static int 105at91_rst_probe(device_t dev) 106{
|
| 107#ifdef FDT 108 if (!ofw_bus_is_compatible(dev, "atmel,at91sam9260-rstc")) 109 return (ENXIO); 110#endif
|
96 97 device_set_desc(dev, "AT91SAM9 Reset Controller"); 98 return (0); 99} 100 101static int 102at91_rst_attach(device_t dev) 103{ 104 struct at91_rst_softc *sc; 105 const char *cause;
| 111 112 device_set_desc(dev, "AT91SAM9 Reset Controller"); 113 return (0); 114} 115 116static int 117at91_rst_attach(device_t dev) 118{ 119 struct at91_rst_softc *sc; 120 const char *cause;
|
106 int rid, err;
| 121 int rid, err = 0;
|
107 108 at91_rst_sc = sc = device_get_softc(dev); 109 sc->sc_dev = dev; 110 111 callout_init(&sc->tick_ch, 0); 112 113 rid = 0; 114 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 115 RF_ACTIVE); 116 if (sc->mem_res == NULL) { 117 device_printf(dev, "could not allocate memory resources.\n"); 118 err = ENOMEM; 119 goto out; 120 }
| 122 123 at91_rst_sc = sc = device_get_softc(dev); 124 sc->sc_dev = dev; 125 126 callout_init(&sc->tick_ch, 0); 127 128 rid = 0; 129 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 130 RF_ACTIVE); 131 if (sc->mem_res == NULL) { 132 device_printf(dev, "could not allocate memory resources.\n"); 133 err = ENOMEM; 134 goto out; 135 }
|
| 136 137#ifndef FDT_HACKS
|
121 rid = 0; 122 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 123 RF_ACTIVE | RF_SHAREABLE); 124 if (sc->irq_res == NULL) { 125 device_printf(dev, "could not allocate interrupt resources.\n"); 126 err = ENOMEM; 127 goto out; 128 } 129 130 /* Activate the interrupt. */ 131 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE, 132 at91_rst_intr, NULL, sc, &sc->intrhand); 133 if (err) 134 device_printf(dev, "could not establish interrupt handler.\n");
| 138 rid = 0; 139 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 140 RF_ACTIVE | RF_SHAREABLE); 141 if (sc->irq_res == NULL) { 142 device_printf(dev, "could not allocate interrupt resources.\n"); 143 err = ENOMEM; 144 goto out; 145 } 146 147 /* Activate the interrupt. */ 148 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE, 149 at91_rst_intr, NULL, sc, &sc->intrhand); 150 if (err) 151 device_printf(dev, "could not establish interrupt handler.\n");
|
| 152#endif
|
135 136 WR4(at91_rst_sc, RST_MR, RST_MR_ERSTL(0xd) | RST_MR_URSIEN | RST_MR_KEY); 137 138 switch (RD4(sc, RST_SR) & RST_SR_RST_MASK) { 139 case RST_SR_RST_POW: 140 cause = "Power On"; 141 break; 142 case RST_SR_RST_WAKE: 143 cause = "Wake Up"; 144 break; 145 case RST_SR_RST_WDT: 146 cause = "Watchdog"; 147 break; 148 case RST_SR_RST_SOFT: 149 cause = "Software Request"; 150 break; 151 case RST_SR_RST_USR: 152 cause = "External (User)"; 153 break; 154 default: 155 cause = "Unknown"; 156 break; 157 } 158 159 device_printf(dev, "Reset cause: %s.\n", cause); 160out: 161 return (err); 162} 163
| 153 154 WR4(at91_rst_sc, RST_MR, RST_MR_ERSTL(0xd) | RST_MR_URSIEN | RST_MR_KEY); 155 156 switch (RD4(sc, RST_SR) & RST_SR_RST_MASK) { 157 case RST_SR_RST_POW: 158 cause = "Power On"; 159 break; 160 case RST_SR_RST_WAKE: 161 cause = "Wake Up"; 162 break; 163 case RST_SR_RST_WDT: 164 cause = "Watchdog"; 165 break; 166 case RST_SR_RST_SOFT: 167 cause = "Software Request"; 168 break; 169 case RST_SR_RST_USR: 170 cause = "External (User)"; 171 break; 172 default: 173 cause = "Unknown"; 174 break; 175 } 176 177 device_printf(dev, "Reset cause: %s.\n", cause); 178out: 179 return (err); 180} 181
|
| 182#ifndef FDT_HACKS
|
164static void 165at91_rst_tick(void *argp) 166{ 167 struct at91_rst_softc *sc = argp; 168 169 if (sc->shutdown++ >= RST_TIMEOUT * RST_TICK) { 170 /* User released the button in morre than RST_TIMEOUT */ 171 cpu_reset(); 172 } else if ((RD4(sc, RST_SR) & RST_SR_NRSTL)) { 173 /* User released the button in less than RST_TIMEOUT */ 174 sc->shutdown = 0; 175 device_printf(sc->sc_dev, "shutting down...\n"); 176 shutdown_nice(0); 177 } else { 178 callout_reset(&sc->tick_ch, hz/RST_TICK, at91_rst_tick, sc); 179 } 180} 181 182static int 183at91_rst_intr(void *argp) 184{ 185 struct at91_rst_softc *sc = argp; 186 187 if (RD4(sc, RST_SR) & RST_SR_URSTS) { 188 if (sc->shutdown == 0) 189 callout_reset(&sc->tick_ch, hz/RST_TICK, at91_rst_tick, sc); 190 return (FILTER_HANDLED); 191 } 192 return (FILTER_STRAY); 193}
| 183static void 184at91_rst_tick(void *argp) 185{ 186 struct at91_rst_softc *sc = argp; 187 188 if (sc->shutdown++ >= RST_TIMEOUT * RST_TICK) { 189 /* User released the button in morre than RST_TIMEOUT */ 190 cpu_reset(); 191 } else if ((RD4(sc, RST_SR) & RST_SR_NRSTL)) { 192 /* User released the button in less than RST_TIMEOUT */ 193 sc->shutdown = 0; 194 device_printf(sc->sc_dev, "shutting down...\n"); 195 shutdown_nice(0); 196 } else { 197 callout_reset(&sc->tick_ch, hz/RST_TICK, at91_rst_tick, sc); 198 } 199} 200 201static int 202at91_rst_intr(void *argp) 203{ 204 struct at91_rst_softc *sc = argp; 205 206 if (RD4(sc, RST_SR) & RST_SR_URSTS) { 207 if (sc->shutdown == 0) 208 callout_reset(&sc->tick_ch, hz/RST_TICK, at91_rst_tick, sc); 209 return (FILTER_HANDLED); 210 } 211 return (FILTER_STRAY); 212}
|
| 213#endif
|
194 195static device_method_t at91_rst_methods[] = { 196 DEVMETHOD(device_probe, at91_rst_probe), 197 DEVMETHOD(device_attach, at91_rst_attach), 198 DEVMETHOD_END 199}; 200 201static driver_t at91_rst_driver = { 202 "at91_rst", 203 at91_rst_methods, 204 sizeof(struct at91_rst_softc), 205}; 206 207static devclass_t at91_rst_devclass; 208
| 214 215static device_method_t at91_rst_methods[] = { 216 DEVMETHOD(device_probe, at91_rst_probe), 217 DEVMETHOD(device_attach, at91_rst_attach), 218 DEVMETHOD_END 219}; 220 221static driver_t at91_rst_driver = { 222 "at91_rst", 223 at91_rst_methods, 224 sizeof(struct at91_rst_softc), 225}; 226 227static devclass_t at91_rst_devclass; 228
|
| 229#ifdef FDT 230DRIVER_MODULE(at91_rst, simplebus, at91_rst_driver, at91_rst_devclass, NULL, 231 NULL); 232#else
|
209DRIVER_MODULE(at91_rst, atmelarm, at91_rst_driver, at91_rst_devclass, NULL, 210 NULL);
| 233DRIVER_MODULE(at91_rst, atmelarm, at91_rst_driver, at91_rst_devclass, NULL, 234 NULL);
|
| 235#endif
|
| |