xref: /netbsd-src/sys/arch/powerpc/ibm4xx/dev/wdog.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /* $NetBSD: wdog.c,v 1.7 2003/07/15 02:54:44 lukem 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 IBM 405GP processor.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.7 2003/07/15 02:54:44 lukem Exp $");
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/properties.h>
49 #include <sys/wdog.h>
50 
51 #include <powerpc/spr.h>
52 #include <powerpc/ibm4xx/dev/opbvar.h>
53 
54 #include <dev/sysmon/sysmonvar.h>
55 
56 static int wdog_match(struct device *, struct cfdata *, void *);
57 static void wdog_attach(struct device *, struct device *, void *);
58 static int wdog_tickle(struct sysmon_wdog *);
59 static int wdog_setmode(struct sysmon_wdog *);
60 
61 struct wdog_softc {
62 	struct device sc_dev;
63 	struct sysmon_wdog sc_smw;
64 	int sc_wdog_armed;
65 	int sc_wdog_period;
66 };
67 
68 CFATTACH_DECL(wdog, sizeof(struct wdog_softc),
69     wdog_match, wdog_attach, NULL, NULL);
70 
71 static int
72 wdog_match(struct device *parent, struct cfdata *cf, void *aux)
73 {
74 	struct opb_attach_args *oaa = aux;
75 
76 	/* match only watchdog devices */
77 	if (strcmp(oaa->opb_name, cf->cf_name) != 0)
78 		return (0);
79 
80 	return (1);
81 }
82 
83 static void
84 wdog_attach(struct device *parent, struct device *self, void *aux)
85 {
86 	struct wdog_softc *sc = (void *)self;
87 	unsigned int processor_freq;
88 
89 	if (board_info_get("processor-frequency",
90 	    &processor_freq, sizeof(processor_freq)) == -1)
91 		processor_freq = 200 * 1000 * 1000;	/* assume 200MHz */
92 
93 	sc->sc_wdog_period = (2LL << 29) / processor_freq;
94 	printf(": %d second period\n", sc->sc_wdog_period);
95 
96 	sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
97 	sc->sc_smw.smw_cookie = sc;
98 	sc->sc_smw.smw_setmode = wdog_setmode;
99 	sc->sc_smw.smw_tickle = wdog_tickle;
100 	sc->sc_smw.smw_period = sc->sc_wdog_period;
101 
102 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
103 		printf("%s: unable to register with sysmon\n",
104 		    sc->sc_dev.dv_xname);
105 
106 }
107 
108 static int
109 wdog_tickle(struct sysmon_wdog *smw)
110 {
111 	uint32_t tsr;
112 
113 	tsr = mfspr(SPR_TSR);
114 	tsr |= TSR_ENW | TSR_WIS;
115 	mtspr(SPR_TSR, tsr);
116 	return (0);
117 }
118 
119 static int
120 wdog_setmode(struct sysmon_wdog *smw)
121 {
122 	struct wdog_softc *sc = smw->smw_cookie;
123 	uint32_t tcr, tsr;
124 
125 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
126 		if (sc->sc_wdog_armed) {
127 			tsr = mfspr(SPR_TSR);
128 			tsr &= ~(TSR_ENW | TSR_WIS);
129 			mtspr(SPR_TSR, tsr);
130 		}
131 	} else {
132 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
133 			smw->smw_period = sc->sc_wdog_period;
134 		else if (smw->smw_period != sc->sc_wdog_period) {
135 			/*
136 			 * There's 4 set watchdog periods on the 405GP,
137 			 * but we only support the longest one (2.684
138 			 * seconds).
139 			 */
140 			return (EOPNOTSUPP);
141 		}
142 		sc->sc_wdog_armed = 1;
143 
144 		tcr = mfspr(SPR_TCR);
145 		tcr |= TCR_WP_2_29 | TCR_WRC_SYSTEM;
146 		mtspr(SPR_TCR, tcr);
147 
148 		/* Arm the watchdog. */
149 		wdog_tickle(smw);
150 	}
151 	return (0);
152 }
153