bcm2835_pmwdog.c revision 1.3 1 1.3 mlelstv /* $NetBSD: bcm2835_pmwdog.c,v 1.3 2024/06/10 06:03:48 mlelstv Exp $ */
2 1.1 skrll
3 1.1 skrll /*-
4 1.1 skrll * Copyright (c) 2012, 2016 The NetBSD Foundation, Inc.
5 1.1 skrll * All rights reserved.
6 1.1 skrll *
7 1.1 skrll * This code is derived from software contributed to The NetBSD Foundation
8 1.1 skrll * by Nick Hudson
9 1.1 skrll *
10 1.1 skrll * Redistribution and use in source and binary forms, with or without
11 1.1 skrll * modification, are permitted provided that the following conditions
12 1.1 skrll * are met:
13 1.1 skrll * 1. Redistributions of source code must retain the above copyright
14 1.1 skrll * notice, this list of conditions and the following disclaimer.
15 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 skrll * notice, this list of conditions and the following disclaimer in the
17 1.1 skrll * documentation and/or other materials provided with the distribution.
18 1.1 skrll *
19 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 skrll * POSSIBILITY OF SUCH DAMAGE.
30 1.1 skrll */
31 1.1 skrll
32 1.1 skrll #include <sys/cdefs.h>
33 1.3 mlelstv __KERNEL_RCSID(0, "$NetBSD: bcm2835_pmwdog.c,v 1.3 2024/06/10 06:03:48 mlelstv Exp $");
34 1.1 skrll
35 1.1 skrll
36 1.1 skrll #include <sys/param.h>
37 1.1 skrll #include <sys/systm.h>
38 1.1 skrll #include <sys/device.h>
39 1.1 skrll #include <sys/kernel.h>
40 1.1 skrll #include <sys/timetc.h>
41 1.1 skrll #include <sys/wdog.h>
42 1.1 skrll #include <sys/bus.h>
43 1.1 skrll
44 1.1 skrll #include <dev/sysmon/sysmonvar.h>
45 1.1 skrll
46 1.1 skrll #include <arm/broadcom/bcm2835reg.h>
47 1.1 skrll #include <arm/broadcom/bcm2835_intr.h>
48 1.1 skrll #include <arm/broadcom/bcm2835_pmwdogvar.h>
49 1.1 skrll
50 1.1 skrll #include <dev/fdt/fdtvar.h>
51 1.1 skrll
52 1.1 skrll #ifndef BCM2835_PM_DEFAULT_PERIOD
53 1.1 skrll #define BCM2835_PM_DEFAULT_PERIOD 15 /* seconds */
54 1.1 skrll #endif
55 1.1 skrll
56 1.1 skrll #define BCM2835_PM_PASSWORD 0x5a000000
57 1.3 mlelstv #define BCM2835_PM_PASSWORD_MASK 0xff000000
58 1.1 skrll
59 1.1 skrll #define BCM2835_PM_RSTC 0x1c
60 1.1 skrll #define BCM2835_PM_RSTC_CONFIGMASK 0x00000030
61 1.1 skrll #define BCM2835_PM_RSTC_FULL_RESET 0x00000020
62 1.1 skrll #define BCM2835_PM_RSTC_RESET 0x00000102
63 1.1 skrll
64 1.3 mlelstv #define BCM2835_PM_RSTS 0x20
65 1.3 mlelstv #define BCM2835_PM_RSTS_PART(x) \
66 1.3 mlelstv ((uint16_t)((x) & 0x01) << 0 | (uint16_t)((x) & 0x02) << 1 | \
67 1.3 mlelstv (uint16_t)((x) & 0x04) << 2 | (uint16_t)((x) & 0x08) << 3 | \
68 1.3 mlelstv (uint16_t)((x) & 0x10) << 4 | (uint16_t)((x) & 0x20) << 5)
69 1.3 mlelstv
70 1.1 skrll #define BCM2835_PM_WDOG 0x24
71 1.1 skrll #define BCM2835_PM_WDOG_TIMEMASK 0x000fffff
72 1.1 skrll
73 1.1 skrll struct bcm2835pmwdog_softc {
74 1.1 skrll device_t sc_dev;
75 1.1 skrll
76 1.1 skrll bus_space_tag_t sc_iot;
77 1.1 skrll bus_space_handle_t sc_ioh;
78 1.1 skrll
79 1.1 skrll struct sysmon_wdog sc_smw;
80 1.1 skrll };
81 1.1 skrll
82 1.1 skrll static struct bcm2835pmwdog_softc *bcm2835pmwdog_sc;
83 1.1 skrll
84 1.1 skrll static int bcmpmwdog_match(device_t, cfdata_t, void *);
85 1.1 skrll static void bcmpmwdog_attach(device_t, device_t, void *);
86 1.1 skrll
87 1.1 skrll static void bcmpmwdog_set_timeout(struct bcm2835pmwdog_softc *, uint32_t);
88 1.1 skrll
89 1.1 skrll static int bcmpmwdog_setmode(struct sysmon_wdog *);
90 1.1 skrll static int bcmpmwdog_tickle(struct sysmon_wdog *);
91 1.1 skrll
92 1.3 mlelstv /* fdt power controller */
93 1.3 mlelstv static void bcm2835_power_reset(device_t);
94 1.3 mlelstv static void bcm2835_power_poweroff(device_t);
95 1.3 mlelstv
96 1.1 skrll CFATTACH_DECL_NEW(bcmpmwdog_fdt, sizeof(struct bcm2835pmwdog_softc),
97 1.1 skrll bcmpmwdog_match, bcmpmwdog_attach, NULL, NULL);
98 1.1 skrll
99 1.2 thorpej static const struct device_compatible_entry compat_data[] = {
100 1.2 thorpej { .compat = "brcm,bcm2835-pm-wdt" },
101 1.2 thorpej DEVICE_COMPAT_EOL
102 1.2 thorpej };
103 1.2 thorpej
104 1.3 mlelstv static struct fdtbus_power_controller_func bcmpmwdog_power_funcs = {
105 1.3 mlelstv .reset = bcm2835_power_reset,
106 1.3 mlelstv .poweroff = bcm2835_power_poweroff
107 1.3 mlelstv };
108 1.3 mlelstv
109 1.1 skrll /* ARGSUSED */
110 1.1 skrll static int
111 1.1 skrll bcmpmwdog_match(device_t parent, cfdata_t match, void *aux)
112 1.1 skrll {
113 1.1 skrll struct fdt_attach_args * const faa = aux;
114 1.1 skrll
115 1.2 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
116 1.1 skrll }
117 1.1 skrll
118 1.1 skrll static void
119 1.1 skrll bcmpmwdog_attach(device_t parent, device_t self, void *aux)
120 1.1 skrll {
121 1.1 skrll struct bcm2835pmwdog_softc *sc = device_private(self);
122 1.1 skrll struct fdt_attach_args * const faa = aux;
123 1.1 skrll const int phandle = faa->faa_phandle;
124 1.1 skrll
125 1.1 skrll aprint_naive("\n");
126 1.1 skrll aprint_normal(": Power management, Reset and Watchdog controller\n");
127 1.1 skrll
128 1.1 skrll if (bcm2835pmwdog_sc == NULL)
129 1.1 skrll bcm2835pmwdog_sc = sc;
130 1.1 skrll
131 1.1 skrll sc->sc_dev = self;
132 1.1 skrll sc->sc_iot = faa->faa_bst;
133 1.1 skrll
134 1.1 skrll bus_addr_t addr;
135 1.1 skrll bus_size_t size;
136 1.1 skrll
137 1.1 skrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
138 1.1 skrll aprint_error(": couldn't get register address\n");
139 1.1 skrll return;
140 1.1 skrll }
141 1.1 skrll
142 1.1 skrll if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh) != 0) {
143 1.1 skrll aprint_error_dev(sc->sc_dev, "unable to map device\n");
144 1.1 skrll return;
145 1.1 skrll }
146 1.1 skrll
147 1.1 skrll /* watchdog */
148 1.1 skrll sc->sc_smw.smw_name = device_xname(sc->sc_dev);
149 1.1 skrll sc->sc_smw.smw_cookie = sc;
150 1.1 skrll sc->sc_smw.smw_setmode = bcmpmwdog_setmode;
151 1.1 skrll sc->sc_smw.smw_tickle = bcmpmwdog_tickle;
152 1.1 skrll sc->sc_smw.smw_period = BCM2835_PM_DEFAULT_PERIOD;
153 1.1 skrll if (sysmon_wdog_register(&sc->sc_smw) != 0)
154 1.1 skrll aprint_error_dev(self, "couldn't register watchdog\n");
155 1.3 mlelstv
156 1.3 mlelstv fdtbus_register_power_controller(self, phandle,
157 1.3 mlelstv &bcmpmwdog_power_funcs);
158 1.1 skrll }
159 1.1 skrll
160 1.1 skrll static void
161 1.1 skrll bcmpmwdog_set_timeout(struct bcm2835pmwdog_softc *sc, uint32_t ticks)
162 1.1 skrll {
163 1.1 skrll uint32_t tmp, rstc, wdog;
164 1.1 skrll
165 1.1 skrll tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_RSTC);
166 1.1 skrll
167 1.1 skrll rstc = wdog = BCM2835_PM_PASSWORD;
168 1.1 skrll
169 1.1 skrll rstc |= tmp & ~BCM2835_PM_RSTC_CONFIGMASK;
170 1.1 skrll rstc |= BCM2835_PM_RSTC_FULL_RESET;
171 1.1 skrll
172 1.1 skrll wdog |= ticks & BCM2835_PM_WDOG_TIMEMASK;
173 1.1 skrll
174 1.1 skrll bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_WDOG, wdog);
175 1.1 skrll bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_RSTC, rstc);
176 1.1 skrll }
177 1.1 skrll
178 1.1 skrll static int
179 1.1 skrll bcmpmwdog_setmode(struct sysmon_wdog *smw)
180 1.1 skrll {
181 1.1 skrll struct bcm2835pmwdog_softc *sc = smw->smw_cookie;
182 1.1 skrll int error = 0;
183 1.1 skrll
184 1.1 skrll if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
185 1.1 skrll bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_RSTC,
186 1.1 skrll BCM2835_PM_PASSWORD | BCM2835_PM_RSTC_RESET);
187 1.1 skrll } else {
188 1.1 skrll if (smw->smw_period == WDOG_PERIOD_DEFAULT)
189 1.1 skrll smw->smw_period = BCM2835_PM_DEFAULT_PERIOD;
190 1.1 skrll if (smw->smw_period > (BCM2835_PM_WDOG_TIMEMASK >> 16))
191 1.1 skrll return EINVAL;
192 1.1 skrll error = bcmpmwdog_tickle(smw);
193 1.1 skrll }
194 1.1 skrll
195 1.1 skrll return error;
196 1.1 skrll }
197 1.1 skrll
198 1.3 mlelstv static void
199 1.3 mlelstv bcmpmwdog_set_partition(struct bcm2835pmwdog_softc *sc, uint8_t part)
200 1.3 mlelstv {
201 1.3 mlelstv uint32_t tmp;
202 1.3 mlelstv
203 1.3 mlelstv tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_RSTS);
204 1.3 mlelstv tmp &= ~(BCM2835_PM_PASSWORD_MASK | BCM2835_PM_RSTS_PART(~0));
205 1.3 mlelstv tmp |= BCM2835_PM_PASSWORD | BCM2835_PM_RSTS_PART(part);
206 1.3 mlelstv bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_RSTS, tmp);
207 1.3 mlelstv }
208 1.3 mlelstv
209 1.1 skrll static int
210 1.1 skrll bcmpmwdog_tickle(struct sysmon_wdog *smw)
211 1.1 skrll {
212 1.1 skrll struct bcm2835pmwdog_softc *sc = smw->smw_cookie;
213 1.1 skrll uint32_t timeout = smw->smw_period << 16;
214 1.1 skrll
215 1.1 skrll bcmpmwdog_set_timeout(sc, timeout);
216 1.1 skrll
217 1.1 skrll return 0;
218 1.1 skrll }
219 1.1 skrll
220 1.3 mlelstv static void
221 1.3 mlelstv bcm2835_restart(struct bcm2835pmwdog_softc *sc, int partition)
222 1.3 mlelstv {
223 1.3 mlelstv uint32_t timeout = 10;
224 1.3 mlelstv
225 1.3 mlelstv bcmpmwdog_set_partition(sc, partition);
226 1.3 mlelstv bcmpmwdog_set_timeout(sc, timeout);
227 1.3 mlelstv }
228 1.3 mlelstv
229 1.1 skrll void
230 1.1 skrll bcm2835_system_reset(void)
231 1.1 skrll {
232 1.1 skrll struct bcm2835pmwdog_softc *sc = bcm2835pmwdog_sc;
233 1.1 skrll
234 1.3 mlelstv bcm2835_restart(sc, 0);
235 1.3 mlelstv }
236 1.3 mlelstv
237 1.3 mlelstv static void
238 1.3 mlelstv bcm2835_power_reset(device_t self)
239 1.3 mlelstv {
240 1.3 mlelstv struct bcm2835pmwdog_softc *sc = device_private(self);
241 1.3 mlelstv
242 1.3 mlelstv bcm2835_restart(sc, 0);
243 1.3 mlelstv
244 1.3 mlelstv for (;;) continue;
245 1.3 mlelstv /* NOTREACHED */
246 1.3 mlelstv }
247 1.3 mlelstv
248 1.3 mlelstv static void
249 1.3 mlelstv bcm2835_power_poweroff(device_t self)
250 1.3 mlelstv {
251 1.3 mlelstv struct bcm2835pmwdog_softc *sc = device_private(self);
252 1.3 mlelstv
253 1.3 mlelstv /* Boot from partition 63 is magic to halt boot process */
254 1.3 mlelstv bcm2835_restart(sc, 63);
255 1.3 mlelstv
256 1.3 mlelstv for (;;) continue;
257 1.3 mlelstv /* NOTREACHED */
258 1.1 skrll }
259 1.3 mlelstv
260