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