xref: /netbsd/sys/arch/mips/sibyte/dev/sbwdog.c (revision c4a72b64)
1 /* $NetBSD: sbwdog.c,v 1.4 2002/10/02 15:52:26 thorpej 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/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/wdog.h>
46 
47 #include <dev/sysmon/sysmonvar.h>
48 
49 #include <machine/locore.h>
50 
51 #include <mips/sibyte/include/sb1250_regs.h>
52 #include <mips/sibyte/include/sb1250_scd.h>
53 #include <mips/sibyte/dev/sbscdvar.h>
54 
55 #define	SBWDOG_DEFAULT_PERIOD	5	/* Default to 5 seconds. */
56 
57 struct sbwdog_softc {
58 	struct device sc_dev;
59 	struct sysmon_wdog sc_smw;
60 	u_long sc_addr;
61 	int sc_wdog_armed;
62 	int sc_wdog_period;
63 };
64 
65 static int sbwdog_match(struct device *, struct cfdata *, void *);
66 static void sbwdog_attach(struct device *, struct device *, void *);
67 static int sbwdog_tickle(struct sysmon_wdog *);
68 static int sbwdog_setmode(struct sysmon_wdog *);
69 
70 CFATTACH_DECL(sbwdog, sizeof(struct sbwdog_softc),
71     sbwdog_match, sbwdog_attach, NULL, NULL);
72 
73 #define	READ_REG(rp)		(mips3_ld((uint64_t *)(rp)))
74 #define	WRITE_REG(rp, val)	(mips3_sd((uint64_t *)(rp), (val)))
75 
76 static int
77 sbwdog_match(struct device *parent, struct cfdata *cf, void *aux)
78 {
79 	struct sbscd_attach_args *sa = aux;
80 
81 	if (sa->sa_locs.sa_type != SBSCD_DEVTYPE_WDOG)
82 		return (0);
83 
84 	return (1);
85 }
86 
87 static void
88 sbwdog_attach(struct device *parent, struct device *self, void *aux)
89 {
90 	struct sbwdog_softc *sc = (void *)self;
91 	struct sbscd_attach_args *sa = aux;
92 
93 	sc->sc_wdog_period = SBWDOG_DEFAULT_PERIOD;
94 	sc->sc_addr = MIPS_PHYS_TO_KSEG1(sa->sa_base + sa->sa_locs.sa_offset);
95 
96 	printf(": %d second period\n", sc->sc_wdog_period);
97 
98 	sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
99 	sc->sc_smw.smw_cookie = sc;
100 	sc->sc_smw.smw_setmode = sbwdog_setmode;
101 	sc->sc_smw.smw_tickle = sbwdog_tickle;
102 	sc->sc_smw.smw_period = sc->sc_wdog_period;
103 
104 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
105 		printf("%s: unable to register with sysmon\n",
106 		    sc->sc_dev.dv_xname);
107 }
108 
109 static int
110 sbwdog_tickle(struct sysmon_wdog *smw)
111 {
112 	struct sbwdog_softc *sc = smw->smw_cookie;
113 
114 	WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, M_SCD_WDOG_ENABLE);
115 	return (0);
116 }
117 
118 static int
119 sbwdog_setmode(struct sysmon_wdog *smw)
120 {
121 	struct sbwdog_softc *sc = smw->smw_cookie;
122 
123 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
124 		if (sc->sc_wdog_armed)
125 			WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, 0);
126 	} else {
127 		if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
128 			sc->sc_wdog_period = SBWDOG_DEFAULT_PERIOD;
129 			smw->smw_period = SBWDOG_DEFAULT_PERIOD;	/* XXX needed?? */
130 		} else if (smw->smw_period > 8) {
131 			/* Maximum of 2^23 usec watchdog period. */
132 			return (EOPNOTSUPP);
133 		}
134 		sc->sc_wdog_period = smw->smw_period;
135 		sc->sc_wdog_armed = 1;
136 		WRITE_REG(sc->sc_addr + R_SCD_WDOG_INIT,
137 		    sc->sc_wdog_period * V_SCD_WDOG_FREQ);
138 
139 		/* Watchdog is armed by tickling it. */
140 		sbwdog_tickle(smw);
141 	}
142 	return (0);
143 }
144