1*7ed56e8cSmrg /* $NetBSD: sbwdog.c,v 1.15 2017/07/24 09:56:46 mrg Exp $ */
2e9dfdd9bSsimonb
3e9dfdd9bSsimonb /*
4e9dfdd9bSsimonb * Copyright (c) 2002 Wasabi Systems, Inc.
5e9dfdd9bSsimonb * All rights reserved.
6e9dfdd9bSsimonb *
7e9dfdd9bSsimonb * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
8e9dfdd9bSsimonb *
9e9dfdd9bSsimonb * Redistribution and use in source and binary forms, with or without
10e9dfdd9bSsimonb * modification, are permitted provided that the following conditions
11e9dfdd9bSsimonb * are met:
12e9dfdd9bSsimonb * 1. Redistributions of source code must retain the above copyright
13e9dfdd9bSsimonb * notice, this list of conditions and the following disclaimer.
14e9dfdd9bSsimonb * 2. Redistributions in binary form must reproduce the above copyright
15e9dfdd9bSsimonb * notice, this list of conditions and the following disclaimer in the
16e9dfdd9bSsimonb * documentation and/or other materials provided with the distribution.
17e9dfdd9bSsimonb * 3. All advertising materials mentioning features or use of this software
18e9dfdd9bSsimonb * must display the following acknowledgement:
19e9dfdd9bSsimonb * This product includes software developed for the NetBSD Project by
20e9dfdd9bSsimonb * Wasabi Systems, Inc.
21e9dfdd9bSsimonb * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22e9dfdd9bSsimonb * or promote products derived from this software without specific prior
23e9dfdd9bSsimonb * written permission.
24e9dfdd9bSsimonb *
25e9dfdd9bSsimonb * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26e9dfdd9bSsimonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27e9dfdd9bSsimonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28e9dfdd9bSsimonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29e9dfdd9bSsimonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30e9dfdd9bSsimonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31e9dfdd9bSsimonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32e9dfdd9bSsimonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33e9dfdd9bSsimonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34e9dfdd9bSsimonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35e9dfdd9bSsimonb * POSSIBILITY OF SUCH DAMAGE.
36e9dfdd9bSsimonb */
37e9dfdd9bSsimonb
38e9dfdd9bSsimonb /*
39e9dfdd9bSsimonb * Watchdog timer support for the Broadcom BCM1250 processor.
40e9dfdd9bSsimonb */
41e9dfdd9bSsimonb
424b2744bfSlukem #include <sys/cdefs.h>
43*7ed56e8cSmrg __KERNEL_RCSID(0, "$NetBSD: sbwdog.c,v 1.15 2017/07/24 09:56:46 mrg Exp $");
4482bf97dcSmatt
4582bf97dcSmatt #include "locators.h"
464b2744bfSlukem
47e9dfdd9bSsimonb #include <sys/param.h>
48e9dfdd9bSsimonb #include <sys/systm.h>
49e9dfdd9bSsimonb #include <sys/device.h>
50e9dfdd9bSsimonb #include <sys/wdog.h>
51e9dfdd9bSsimonb
52e9dfdd9bSsimonb #include <dev/sysmon/sysmonvar.h>
53e9dfdd9bSsimonb
54d1cae3a2Smatt #include <mips/locore.h>
55e9dfdd9bSsimonb
56e9dfdd9bSsimonb #include <mips/sibyte/include/sb1250_regs.h>
57e9dfdd9bSsimonb #include <mips/sibyte/include/sb1250_scd.h>
58e9dfdd9bSsimonb #include <mips/sibyte/dev/sbscdvar.h>
59e9dfdd9bSsimonb
60*7ed56e8cSmrg #include <evbmips/sbmips/systemsw.h>
61*7ed56e8cSmrg
62e9dfdd9bSsimonb #define SBWDOG_DEFAULT_PERIOD 5 /* Default to 5 seconds. */
63e9dfdd9bSsimonb
64e9dfdd9bSsimonb struct sbwdog_softc {
65916ee5b6Smatt device_t sc_dev;
66e9dfdd9bSsimonb struct sysmon_wdog sc_smw;
67e9dfdd9bSsimonb u_long sc_addr;
68e9dfdd9bSsimonb int sc_wdog_armed;
69e9dfdd9bSsimonb int sc_wdog_period;
70e9dfdd9bSsimonb };
71e9dfdd9bSsimonb
72916ee5b6Smatt static int sbwdog_match(device_t, cfdata_t, void *);
73916ee5b6Smatt static void sbwdog_attach(device_t, device_t, void *);
74e9dfdd9bSsimonb static int sbwdog_tickle(struct sysmon_wdog *);
75e9dfdd9bSsimonb static int sbwdog_setmode(struct sysmon_wdog *);
766f35b2ecSmatt static void sbwdog_intr(void *, uint32_t, vaddr_t);
77e9dfdd9bSsimonb
78916ee5b6Smatt CFATTACH_DECL_NEW(sbwdog, sizeof(struct sbwdog_softc),
7989bf5a8fSthorpej sbwdog_match, sbwdog_attach, NULL, NULL);
80e9dfdd9bSsimonb
8167c90d24Schristos #define READ_REG(rp) (mips3_ld((register_t)(rp)))
8267c90d24Schristos #define WRITE_REG(rp, val) (mips3_sd((register_t)(rp), (val)))
83e9dfdd9bSsimonb
84e9dfdd9bSsimonb static int
sbwdog_match(device_t parent,cfdata_t cf,void * aux)85916ee5b6Smatt sbwdog_match(device_t parent, cfdata_t cf, void *aux)
86e9dfdd9bSsimonb {
87e9dfdd9bSsimonb struct sbscd_attach_args *sa = aux;
88e9dfdd9bSsimonb
89e9dfdd9bSsimonb if (sa->sa_locs.sa_type != SBSCD_DEVTYPE_WDOG)
90e9dfdd9bSsimonb return (0);
91e9dfdd9bSsimonb
92e9dfdd9bSsimonb return (1);
93e9dfdd9bSsimonb }
94e9dfdd9bSsimonb
95e9dfdd9bSsimonb static void
sbwdog_attach(device_t parent,device_t self,void * aux)96916ee5b6Smatt sbwdog_attach(device_t parent, device_t self, void *aux)
97e9dfdd9bSsimonb {
98916ee5b6Smatt struct sbwdog_softc *sc = device_private(self);
99e9dfdd9bSsimonb struct sbscd_attach_args *sa = aux;
100e9dfdd9bSsimonb
101916ee5b6Smatt sc->sc_dev = self;
102e9dfdd9bSsimonb sc->sc_wdog_period = SBWDOG_DEFAULT_PERIOD;
1036f35b2ecSmatt sc->sc_addr = MIPS_PHYS_TO_KSEG1(sa->sa_base + sa->sa_locs.sa_offset);
104e9dfdd9bSsimonb
105916ee5b6Smatt aprint_normal(": %d second period\n", sc->sc_wdog_period);
106e9dfdd9bSsimonb
107916ee5b6Smatt sc->sc_smw.smw_name = device_xname(sc->sc_dev);
108e9dfdd9bSsimonb sc->sc_smw.smw_cookie = sc;
109e9dfdd9bSsimonb sc->sc_smw.smw_setmode = sbwdog_setmode;
110e9dfdd9bSsimonb sc->sc_smw.smw_tickle = sbwdog_tickle;
111e9dfdd9bSsimonb sc->sc_smw.smw_period = sc->sc_wdog_period;
112e9dfdd9bSsimonb
113e9dfdd9bSsimonb if (sysmon_wdog_register(&sc->sc_smw) != 0)
11482bf97dcSmatt aprint_error_dev(self, "unable to register with sysmon\n");
1156f35b2ecSmatt
1166f35b2ecSmatt if (sa->sa_locs.sa_intr[0] != SBOBIOCF_INTR_DEFAULT)
1176f35b2ecSmatt cpu_intr_establish(sa->sa_locs.sa_intr[0], IPL_HIGH,
1186f35b2ecSmatt sbwdog_intr, sc);
119e9dfdd9bSsimonb }
120e9dfdd9bSsimonb
121e9dfdd9bSsimonb static int
sbwdog_tickle(struct sysmon_wdog * smw)122e9dfdd9bSsimonb sbwdog_tickle(struct sysmon_wdog *smw)
123e9dfdd9bSsimonb {
124e9dfdd9bSsimonb struct sbwdog_softc *sc = smw->smw_cookie;
125e9dfdd9bSsimonb
126e9dfdd9bSsimonb WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, M_SCD_WDOG_ENABLE);
127e9dfdd9bSsimonb return (0);
128e9dfdd9bSsimonb }
129e9dfdd9bSsimonb
1306f35b2ecSmatt static void
sbwdog_intr(void * v,uint32_t cause,vaddr_t pc)1316f35b2ecSmatt sbwdog_intr(void *v, uint32_t cause, vaddr_t pc)
1326f35b2ecSmatt {
1336f35b2ecSmatt struct sbwdog_softc *sc = v;
1346f35b2ecSmatt
1356f35b2ecSmatt WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, M_SCD_WDOG_ENABLE);
1366f35b2ecSmatt WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, 0);
1376f35b2ecSmatt WRITE_REG(sc->sc_addr + R_SCD_WDOG_INIT,
1386f35b2ecSmatt sc->sc_wdog_period * V_SCD_WDOG_FREQ);
1396f35b2ecSmatt WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, M_SCD_WDOG_ENABLE);
1406f35b2ecSmatt }
1416f35b2ecSmatt
1426f35b2ecSmatt
143e9dfdd9bSsimonb static int
sbwdog_setmode(struct sysmon_wdog * smw)144e9dfdd9bSsimonb sbwdog_setmode(struct sysmon_wdog *smw)
145e9dfdd9bSsimonb {
146e9dfdd9bSsimonb struct sbwdog_softc *sc = smw->smw_cookie;
147e9dfdd9bSsimonb
148e9dfdd9bSsimonb if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
149e9dfdd9bSsimonb if (sc->sc_wdog_armed)
150e9dfdd9bSsimonb WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, 0);
151e9dfdd9bSsimonb } else {
152e9dfdd9bSsimonb if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
153e9dfdd9bSsimonb sc->sc_wdog_period = SBWDOG_DEFAULT_PERIOD;
154e9dfdd9bSsimonb smw->smw_period = SBWDOG_DEFAULT_PERIOD; /* XXX needed?? */
155e9dfdd9bSsimonb } else if (smw->smw_period > 8) {
156e9dfdd9bSsimonb /* Maximum of 2^23 usec watchdog period. */
157b4dfc6efSsimonb return (EINVAL);
158e9dfdd9bSsimonb }
159e9dfdd9bSsimonb sc->sc_wdog_period = smw->smw_period;
160e9dfdd9bSsimonb sc->sc_wdog_armed = 1;
161e9dfdd9bSsimonb WRITE_REG(sc->sc_addr + R_SCD_WDOG_INIT,
162e9dfdd9bSsimonb sc->sc_wdog_period * V_SCD_WDOG_FREQ);
163e9dfdd9bSsimonb
164e9dfdd9bSsimonb /* Watchdog is armed by tickling it. */
165e9dfdd9bSsimonb sbwdog_tickle(smw);
166e9dfdd9bSsimonb }
167e9dfdd9bSsimonb return (0);
168e9dfdd9bSsimonb }
169