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