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