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