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