mesongx_wdt.c revision 1.2 1 1.2 thorpej /* $NetBSD: mesongx_wdt.c,v 1.2 2021/01/27 03:10:18 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.2 thorpej __KERNEL_RCSID(0, "$NetBSD: mesongx_wdt.c,v 1.2 2021/01/27 03:10:18 thorpej Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/cpu.h>
35 1.1 jmcneill #include <sys/device.h>
36 1.1 jmcneill #include <sys/wdog.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <dev/sysmon/sysmonvar.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/fdt/fdtvar.h>
41 1.1 jmcneill
42 1.1 jmcneill #define CBUS_REG(x) ((x) << 2)
43 1.1 jmcneill
44 1.1 jmcneill #define WATCHDOG_CNTL CBUS_REG(0)
45 1.1 jmcneill #define CNTL_CLK_DIV_EN __BIT(25)
46 1.1 jmcneill #define CNTL_CLK_EN __BIT(24)
47 1.1 jmcneill #define CNTL_SYS_RESET_N_EN __BIT(21)
48 1.1 jmcneill #define CNTL_WATCHDOG_EN __BIT(18)
49 1.1 jmcneill #define CNTL_CLK_DIV_TCNT __BITS(17,0)
50 1.1 jmcneill #define WATCHDOG_CNTL1 CBUS_REG(1)
51 1.1 jmcneill #define WATCHDOG_TCNT CBUS_REG(2)
52 1.1 jmcneill #define WATCHDOG_RESET CBUS_REG(3)
53 1.1 jmcneill
54 1.1 jmcneill #define WATCHDOG_PERIOD_DEFAULT 8
55 1.1 jmcneill #define WATCHDOG_PERIOD_MAX 8
56 1.1 jmcneill
57 1.2 thorpej static const struct device_compatible_entry compat_data[] = {
58 1.2 thorpej { .compat = "amlogic,meson-gx-wdt" },
59 1.2 thorpej DEVICE_COMPAT_EOL
60 1.1 jmcneill };
61 1.1 jmcneill
62 1.1 jmcneill struct mesongx_wdt_softc {
63 1.1 jmcneill device_t sc_dev;
64 1.1 jmcneill bus_space_tag_t sc_bst;
65 1.1 jmcneill bus_space_handle_t sc_bsh;
66 1.1 jmcneill
67 1.1 jmcneill struct sysmon_wdog sc_wdog;
68 1.1 jmcneill u_int sc_rate;
69 1.1 jmcneill };
70 1.1 jmcneill
71 1.1 jmcneill #define WDT_READ(sc, reg) \
72 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
73 1.1 jmcneill #define WDT_WRITE(sc, reg, val) \
74 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
75 1.1 jmcneill
76 1.1 jmcneill static int
77 1.1 jmcneill mesongx_wdt_setmode(struct sysmon_wdog *smw)
78 1.1 jmcneill {
79 1.1 jmcneill struct mesongx_wdt_softc * const sc = smw->smw_cookie;
80 1.1 jmcneill
81 1.1 jmcneill if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
82 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_CNTL, 0);
83 1.1 jmcneill return 0;
84 1.1 jmcneill }
85 1.1 jmcneill
86 1.1 jmcneill if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
87 1.1 jmcneill sc->sc_wdog.smw_period = WATCHDOG_PERIOD_DEFAULT;
88 1.1 jmcneill } else if (smw->smw_period == 0 ||
89 1.1 jmcneill smw->smw_period > WATCHDOG_PERIOD_MAX) {
90 1.1 jmcneill return EINVAL;
91 1.1 jmcneill } else {
92 1.1 jmcneill sc->sc_wdog.smw_period = smw->smw_period;
93 1.1 jmcneill }
94 1.1 jmcneill
95 1.1 jmcneill const u_int tcnt = sc->sc_rate / 1000;
96 1.1 jmcneill
97 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_CNTL, 0);
98 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_RESET, 0);
99 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_TCNT, sc->sc_wdog.smw_period * 1000);
100 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_CNTL,
101 1.1 jmcneill __SHIFTIN(tcnt, CNTL_CLK_DIV_TCNT) |
102 1.1 jmcneill CNTL_CLK_DIV_EN | CNTL_CLK_EN |
103 1.1 jmcneill CNTL_SYS_RESET_N_EN | CNTL_WATCHDOG_EN);
104 1.1 jmcneill
105 1.1 jmcneill return 0;
106 1.1 jmcneill }
107 1.1 jmcneill
108 1.1 jmcneill static int
109 1.1 jmcneill mesongx_wdt_tickle(struct sysmon_wdog *smw)
110 1.1 jmcneill {
111 1.1 jmcneill struct mesongx_wdt_softc * const sc = smw->smw_cookie;
112 1.1 jmcneill
113 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_RESET, 0);
114 1.1 jmcneill
115 1.1 jmcneill return 0;
116 1.1 jmcneill }
117 1.1 jmcneill
118 1.1 jmcneill static int
119 1.1 jmcneill mesongx_wdt_match(device_t parent, cfdata_t cf, void *aux)
120 1.1 jmcneill {
121 1.1 jmcneill struct fdt_attach_args * const faa = aux;
122 1.1 jmcneill
123 1.2 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
124 1.1 jmcneill }
125 1.1 jmcneill
126 1.1 jmcneill static void
127 1.1 jmcneill mesongx_wdt_attach(device_t parent, device_t self, void *aux)
128 1.1 jmcneill {
129 1.1 jmcneill struct mesongx_wdt_softc * const sc = device_private(self);
130 1.1 jmcneill struct fdt_attach_args * const faa = aux;
131 1.1 jmcneill const int phandle = faa->faa_phandle;
132 1.1 jmcneill struct clk *clk;
133 1.1 jmcneill bus_addr_t addr;
134 1.1 jmcneill bus_size_t size;
135 1.1 jmcneill
136 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
137 1.1 jmcneill aprint_error(": couldn't get registers\n");
138 1.1 jmcneill return;
139 1.1 jmcneill }
140 1.1 jmcneill
141 1.1 jmcneill sc->sc_dev = self;
142 1.1 jmcneill sc->sc_bst = faa->faa_bst;
143 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
144 1.1 jmcneill aprint_error(": couldn't map registers\n");
145 1.1 jmcneill return;
146 1.1 jmcneill }
147 1.1 jmcneill
148 1.1 jmcneill aprint_naive("\n");
149 1.1 jmcneill aprint_normal(": EE-watchdog\n");
150 1.1 jmcneill
151 1.1 jmcneill clk = fdtbus_clock_get_index(phandle, 0);
152 1.1 jmcneill if (clk != NULL)
153 1.1 jmcneill sc->sc_rate = clk_get_rate(clk);
154 1.1 jmcneill else {
155 1.1 jmcneill aprint_error_dev(self, "WARNING: couldn't get xtal clock, assuming 24 MHz\n");
156 1.1 jmcneill sc->sc_rate = 24000000;
157 1.1 jmcneill }
158 1.1 jmcneill
159 1.1 jmcneill /* Disable watchdog */
160 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_CNTL, 0);
161 1.1 jmcneill
162 1.1 jmcneill /* Register watchdog */
163 1.1 jmcneill sc->sc_wdog.smw_name = "EE-watchdog";
164 1.1 jmcneill sc->sc_wdog.smw_setmode = mesongx_wdt_setmode;
165 1.1 jmcneill sc->sc_wdog.smw_tickle = mesongx_wdt_tickle;
166 1.1 jmcneill sc->sc_wdog.smw_period = WATCHDOG_PERIOD_DEFAULT;
167 1.1 jmcneill sc->sc_wdog.smw_cookie = sc;
168 1.1 jmcneill sysmon_wdog_register(&sc->sc_wdog);
169 1.1 jmcneill }
170 1.1 jmcneill
171 1.1 jmcneill CFATTACH_DECL_NEW(mesongx_wdt, sizeof(struct mesongx_wdt_softc),
172 1.1 jmcneill mesongx_wdt_match, mesongx_wdt_attach, NULL, NULL);
173