xref: /netbsd-src/sys/arch/mips/sibyte/dev/sbwdog.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /* $NetBSD: sbwdog.c,v 1.13 2011/07/10 23:32:03 matt 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.13 2011/07/10 23:32:03 matt 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 #define	SBWDOG_DEFAULT_PERIOD	5	/* Default to 5 seconds. */
61 
62 struct sbwdog_softc {
63 	device_t sc_dev;
64 	struct sysmon_wdog sc_smw;
65 	u_long sc_addr;
66 	int sc_wdog_armed;
67 	int sc_wdog_period;
68 };
69 
70 static int sbwdog_match(device_t, cfdata_t, void *);
71 static void sbwdog_attach(device_t, device_t, void *);
72 static int sbwdog_tickle(struct sysmon_wdog *);
73 static int sbwdog_setmode(struct sysmon_wdog *);
74 static void sbwdog_intr(void *, uint32_t, vaddr_t);
75 
76 CFATTACH_DECL_NEW(sbwdog, sizeof(struct sbwdog_softc),
77     sbwdog_match, sbwdog_attach, NULL, NULL);
78 
79 #define	READ_REG(rp)		(mips3_ld((volatile uint64_t *)(rp)))
80 #define	WRITE_REG(rp, val)	(mips3_sd((volatile uint64_t *)(rp), (val)))
81 
82 static int
83 sbwdog_match(device_t parent, cfdata_t cf, void *aux)
84 {
85 	struct sbscd_attach_args *sa = aux;
86 
87 	if (sa->sa_locs.sa_type != SBSCD_DEVTYPE_WDOG)
88 		return (0);
89 
90 	return (1);
91 }
92 
93 static void
94 sbwdog_attach(device_t parent, device_t self, void *aux)
95 {
96 	struct sbwdog_softc *sc = device_private(self);
97 	struct sbscd_attach_args *sa = aux;
98 
99 	sc->sc_dev = self;
100 	sc->sc_wdog_period = SBWDOG_DEFAULT_PERIOD;
101 	sc->sc_addr = MIPS_PHYS_TO_KSEG1(sa->sa_base + sa->sa_locs.sa_offset);
102 
103 	aprint_normal(": %d second period\n", sc->sc_wdog_period);
104 
105 	sc->sc_smw.smw_name = device_xname(sc->sc_dev);
106 	sc->sc_smw.smw_cookie = sc;
107 	sc->sc_smw.smw_setmode = sbwdog_setmode;
108 	sc->sc_smw.smw_tickle = sbwdog_tickle;
109 	sc->sc_smw.smw_period = sc->sc_wdog_period;
110 
111 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
112 		aprint_error_dev(self, "unable to register with sysmon\n");
113 
114 	if (sa->sa_locs.sa_intr[0] != SBOBIOCF_INTR_DEFAULT)
115 		cpu_intr_establish(sa->sa_locs.sa_intr[0], IPL_HIGH,
116 		    sbwdog_intr, sc);
117 }
118 
119 static int
120 sbwdog_tickle(struct sysmon_wdog *smw)
121 {
122 	struct sbwdog_softc *sc = smw->smw_cookie;
123 
124 	WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, M_SCD_WDOG_ENABLE);
125 	return (0);
126 }
127 
128 static void
129 sbwdog_intr(void *v, uint32_t cause, vaddr_t pc)
130 {
131 	struct sbwdog_softc *sc = v;
132 
133 	WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, M_SCD_WDOG_ENABLE);
134 	WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, 0);
135 	WRITE_REG(sc->sc_addr + R_SCD_WDOG_INIT,
136 	    sc->sc_wdog_period * V_SCD_WDOG_FREQ);
137 	WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, M_SCD_WDOG_ENABLE);
138 }
139 
140 
141 static int
142 sbwdog_setmode(struct sysmon_wdog *smw)
143 {
144 	struct sbwdog_softc *sc = smw->smw_cookie;
145 
146 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
147 		if (sc->sc_wdog_armed)
148 			WRITE_REG(sc->sc_addr + R_SCD_WDOG_CFG, 0);
149 	} else {
150 		if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
151 			sc->sc_wdog_period = SBWDOG_DEFAULT_PERIOD;
152 			smw->smw_period = SBWDOG_DEFAULT_PERIOD;	/* XXX needed?? */
153 		} else if (smw->smw_period > 8) {
154 			/* Maximum of 2^23 usec watchdog period. */
155 			return (EINVAL);
156 		}
157 		sc->sc_wdog_period = smw->smw_period;
158 		sc->sc_wdog_armed = 1;
159 		WRITE_REG(sc->sc_addr + R_SCD_WDOG_INIT,
160 		    sc->sc_wdog_period * V_SCD_WDOG_FREQ);
161 
162 		/* Watchdog is armed by tickling it. */
163 		sbwdog_tickle(smw);
164 	}
165 	return (0);
166 }
167