xref: /netbsd-src/sys/arch/arm/apple/apple_wdog.c (revision 8aa5950677e00fd37740ebea83b48dbe2a3d350b)
1*8aa59506Sskrll /* $NetBSD: apple_wdog.c,v 1.3 2022/04/05 05:04:04 skrll Exp $ */
2db7e12daSjmcneill 
3db7e12daSjmcneill /*-
4db7e12daSjmcneill  * Copyright (c) 2021 Jared McNeill <jmcneill@invisible.ca>
5db7e12daSjmcneill  * All rights reserved.
6db7e12daSjmcneill  *
7db7e12daSjmcneill  * Redistribution and use in source and binary forms, with or without
8db7e12daSjmcneill  * modification, are permitted provided that the following conditions
9db7e12daSjmcneill  * are met:
10db7e12daSjmcneill  * 1. Redistributions of source code must retain the above copyright
11db7e12daSjmcneill  *    notice, this list of conditions and the following disclaimer.
12db7e12daSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13db7e12daSjmcneill  *    notice, this list of conditions and the following disclaimer in the
14db7e12daSjmcneill  *    documentation and/or other materials provided with the distribution.
15db7e12daSjmcneill  *
16db7e12daSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17db7e12daSjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18db7e12daSjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19db7e12daSjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20db7e12daSjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21db7e12daSjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22db7e12daSjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23db7e12daSjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24db7e12daSjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25db7e12daSjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26db7e12daSjmcneill  * SUCH DAMAGE.
27db7e12daSjmcneill  */
28db7e12daSjmcneill 
29db7e12daSjmcneill #include <sys/cdefs.h>
30*8aa59506Sskrll __KERNEL_RCSID(0, "$NetBSD: apple_wdog.c,v 1.3 2022/04/05 05:04:04 skrll Exp $");
31db7e12daSjmcneill 
32db7e12daSjmcneill #include <sys/param.h>
33db7e12daSjmcneill #include <sys/bus.h>
34db7e12daSjmcneill #include <sys/device.h>
35db7e12daSjmcneill #include <sys/intr.h>
36db7e12daSjmcneill #include <sys/kernel.h>
37db7e12daSjmcneill #include <sys/lwp.h>
38db7e12daSjmcneill #include <sys/systm.h>
39db7e12daSjmcneill 
40db7e12daSjmcneill #include <dev/fdt/fdtvar.h>
41db7e12daSjmcneill 
42db7e12daSjmcneill #define	WDOG_CHIP_CTL		0x000c
43db7e12daSjmcneill #define	WDOG_SYS_TMR		0x0010
44db7e12daSjmcneill #define	WDOG_SYS_RST		0x0014
45db7e12daSjmcneill #define	WDOG_SYS_CTL		0x001c
46db7e12daSjmcneill #define	 WDOG_SYS_CTL_ENABLE	__BIT(2)
47db7e12daSjmcneill 
48db7e12daSjmcneill static const struct device_compatible_entry compat_data[] = {
49*8aa59506Sskrll 	{ .compat = "apple,wdt" },
50db7e12daSjmcneill 	{ .compat = "apple,reboot-v0" },
51db7e12daSjmcneill 	DEVICE_COMPAT_EOL
52db7e12daSjmcneill };
53db7e12daSjmcneill 
54db7e12daSjmcneill struct apple_wdog_softc {
55db7e12daSjmcneill 	device_t sc_dev;
56db7e12daSjmcneill 	bus_space_tag_t sc_bst;
57db7e12daSjmcneill 	bus_space_handle_t sc_bsh;
58db7e12daSjmcneill };
59db7e12daSjmcneill 
60db7e12daSjmcneill #define WDOG_READ(sc, reg) \
61db7e12daSjmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
62db7e12daSjmcneill #define	WDOG_WRITE(sc, reg, val) \
63db7e12daSjmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
64db7e12daSjmcneill 
65db7e12daSjmcneill static void
apple_wdog_reset(device_t dev)66db7e12daSjmcneill apple_wdog_reset(device_t dev)
67db7e12daSjmcneill {
68db7e12daSjmcneill 	struct apple_wdog_softc * const sc = device_private(dev);
69db7e12daSjmcneill 
70db7e12daSjmcneill 	WDOG_WRITE(sc, WDOG_SYS_RST, 1);
71db7e12daSjmcneill 	WDOG_WRITE(sc, WDOG_SYS_CTL, WDOG_SYS_CTL_ENABLE);
72db7e12daSjmcneill 	WDOG_WRITE(sc, WDOG_SYS_TMR, 0);
73db7e12daSjmcneill }
74db7e12daSjmcneill 
75db7e12daSjmcneill static struct fdtbus_power_controller_func apple_wdog_power_funcs = {
76db7e12daSjmcneill 	.reset = apple_wdog_reset,
77db7e12daSjmcneill };
78db7e12daSjmcneill 
79db7e12daSjmcneill static int
apple_wdog_match(device_t parent,cfdata_t cf,void * aux)80db7e12daSjmcneill apple_wdog_match(device_t parent, cfdata_t cf, void *aux)
81db7e12daSjmcneill {
82db7e12daSjmcneill 	struct fdt_attach_args * const faa = aux;
83db7e12daSjmcneill 
84db7e12daSjmcneill 	return of_compatible_match(faa->faa_phandle, compat_data);
85db7e12daSjmcneill }
86db7e12daSjmcneill 
87db7e12daSjmcneill static void
apple_wdog_attach(device_t parent,device_t self,void * aux)88db7e12daSjmcneill apple_wdog_attach(device_t parent, device_t self, void *aux)
89db7e12daSjmcneill {
90db7e12daSjmcneill 	struct apple_wdog_softc * const sc = device_private(self);
91db7e12daSjmcneill 	struct fdt_attach_args * const faa = aux;
92db7e12daSjmcneill 	const int phandle = faa->faa_phandle;
93db7e12daSjmcneill 	bus_addr_t addr;
94db7e12daSjmcneill 	bus_size_t size;
95db7e12daSjmcneill 
96db7e12daSjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
97db7e12daSjmcneill 		aprint_error(": couldn't get registers\n");
98db7e12daSjmcneill 		return;
99db7e12daSjmcneill 	}
100db7e12daSjmcneill 
101db7e12daSjmcneill 	sc->sc_dev = self;
102db7e12daSjmcneill 	sc->sc_bst = faa->faa_bst;
10377a7b674Sjmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
104db7e12daSjmcneill 		aprint_error(": couldn't map registers\n");
105db7e12daSjmcneill 		return;
106db7e12daSjmcneill 	}
107db7e12daSjmcneill 
108db7e12daSjmcneill 	aprint_naive("\n");
109db7e12daSjmcneill 	aprint_normal(": Apple Watchdog\n");
110db7e12daSjmcneill 
111db7e12daSjmcneill 	WDOG_WRITE(sc, WDOG_CHIP_CTL, 0);
112db7e12daSjmcneill 	WDOG_WRITE(sc, WDOG_SYS_CTL, 0);
113db7e12daSjmcneill 
114db7e12daSjmcneill 	fdtbus_register_power_controller(self, phandle,
115db7e12daSjmcneill 	    &apple_wdog_power_funcs);
116db7e12daSjmcneill }
117db7e12daSjmcneill 
118db7e12daSjmcneill CFATTACH_DECL_NEW(apple_wdog, sizeof(struct apple_wdog_softc),
119db7e12daSjmcneill 	apple_wdog_match, apple_wdog_attach, NULL, NULL);
120