wdog.c revision 1.2.2.2 1 1.2.2.2 gehenna /* $NetBSD: wdog.c,v 1.2.2.2 2002/08/31 13:45:45 gehenna Exp $ */
2 1.2.2.2 gehenna
3 1.2.2.2 gehenna /*
4 1.2.2.2 gehenna * Copyright (c) 2002 Wasabi Systems, Inc.
5 1.2.2.2 gehenna * All rights reserved.
6 1.2.2.2 gehenna *
7 1.2.2.2 gehenna * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
8 1.2.2.2 gehenna *
9 1.2.2.2 gehenna * Redistribution and use in source and binary forms, with or without
10 1.2.2.2 gehenna * modification, are permitted provided that the following conditions
11 1.2.2.2 gehenna * are met:
12 1.2.2.2 gehenna * 1. Redistributions of source code must retain the above copyright
13 1.2.2.2 gehenna * notice, this list of conditions and the following disclaimer.
14 1.2.2.2 gehenna * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.2.2 gehenna * notice, this list of conditions and the following disclaimer in the
16 1.2.2.2 gehenna * documentation and/or other materials provided with the distribution.
17 1.2.2.2 gehenna * 3. All advertising materials mentioning features or use of this software
18 1.2.2.2 gehenna * must display the following acknowledgement:
19 1.2.2.2 gehenna * This product includes software developed for the NetBSD Project by
20 1.2.2.2 gehenna * Wasabi Systems, Inc.
21 1.2.2.2 gehenna * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.2.2.2 gehenna * or promote products derived from this software without specific prior
23 1.2.2.2 gehenna * written permission.
24 1.2.2.2 gehenna *
25 1.2.2.2 gehenna * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.2.2.2 gehenna * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.2.2.2 gehenna * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.2.2.2 gehenna * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.2.2.2 gehenna * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.2.2.2 gehenna * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.2.2.2 gehenna * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.2.2.2 gehenna * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.2.2.2 gehenna * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.2.2.2 gehenna * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.2.2.2 gehenna * POSSIBILITY OF SUCH DAMAGE.
36 1.2.2.2 gehenna */
37 1.2.2.2 gehenna
38 1.2.2.2 gehenna /*
39 1.2.2.2 gehenna * Watchdog timer support for the IBM 405GP processor.
40 1.2.2.2 gehenna */
41 1.2.2.2 gehenna
42 1.2.2.2 gehenna #include <sys/param.h>
43 1.2.2.2 gehenna #include <sys/systm.h>
44 1.2.2.2 gehenna #include <sys/device.h>
45 1.2.2.2 gehenna #include <sys/properties.h>
46 1.2.2.2 gehenna #include <sys/wdog.h>
47 1.2.2.2 gehenna
48 1.2.2.2 gehenna #include <powerpc/spr.h>
49 1.2.2.2 gehenna #include <powerpc/ibm4xx/dev/opbvar.h>
50 1.2.2.2 gehenna
51 1.2.2.2 gehenna #include <dev/sysmon/sysmonvar.h>
52 1.2.2.2 gehenna
53 1.2.2.2 gehenna static int wdog_match(struct device *, struct cfdata *, void *);
54 1.2.2.2 gehenna static void wdog_attach(struct device *, struct device *, void *);
55 1.2.2.2 gehenna static int wdog_tickle(struct sysmon_wdog *);
56 1.2.2.2 gehenna static int wdog_setmode(struct sysmon_wdog *);
57 1.2.2.2 gehenna
58 1.2.2.2 gehenna struct wdog_softc {
59 1.2.2.2 gehenna struct device sc_dev;
60 1.2.2.2 gehenna struct sysmon_wdog sc_smw;
61 1.2.2.2 gehenna int sc_wdog_armed;
62 1.2.2.2 gehenna int sc_wdog_period;
63 1.2.2.2 gehenna };
64 1.2.2.2 gehenna
65 1.2.2.2 gehenna struct cfattach wdog_ca = {
66 1.2.2.2 gehenna sizeof(struct wdog_softc), wdog_match, wdog_attach,
67 1.2.2.2 gehenna };
68 1.2.2.2 gehenna
69 1.2.2.2 gehenna static int
70 1.2.2.2 gehenna wdog_match(struct device *parent, struct cfdata *cf, void *aux)
71 1.2.2.2 gehenna {
72 1.2.2.2 gehenna struct opb_attach_args *oaa = aux;
73 1.2.2.2 gehenna
74 1.2.2.2 gehenna /* match only watchdog devices */
75 1.2.2.2 gehenna if (strcmp(oaa->opb_name, cf->cf_driver->cd_name) != 0)
76 1.2.2.2 gehenna return (0);
77 1.2.2.2 gehenna
78 1.2.2.2 gehenna return (1);
79 1.2.2.2 gehenna }
80 1.2.2.2 gehenna
81 1.2.2.2 gehenna static void
82 1.2.2.2 gehenna wdog_attach(struct device *parent, struct device *self, void *aux)
83 1.2.2.2 gehenna {
84 1.2.2.2 gehenna struct wdog_softc *sc = (void *)self;
85 1.2.2.2 gehenna unsigned int processor_freq;
86 1.2.2.2 gehenna
87 1.2.2.2 gehenna if (board_info_get("processor-frequency",
88 1.2.2.2 gehenna &processor_freq, sizeof(processor_freq)) == -1)
89 1.2.2.2 gehenna processor_freq = 200 * 1000 * 1000; /* assume 200MHz */
90 1.2.2.2 gehenna
91 1.2.2.2 gehenna sc->sc_wdog_period = (2LL << 29) / processor_freq;
92 1.2.2.2 gehenna printf(": %d second period\n", sc->sc_wdog_period);
93 1.2.2.2 gehenna
94 1.2.2.2 gehenna sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
95 1.2.2.2 gehenna sc->sc_smw.smw_cookie = sc;
96 1.2.2.2 gehenna sc->sc_smw.smw_setmode = wdog_setmode;
97 1.2.2.2 gehenna sc->sc_smw.smw_tickle = wdog_tickle;
98 1.2.2.2 gehenna sc->sc_smw.smw_period = sc->sc_wdog_period;
99 1.2.2.2 gehenna
100 1.2.2.2 gehenna if (sysmon_wdog_register(&sc->sc_smw) != 0)
101 1.2.2.2 gehenna printf("%s: unable to register with sysmon\n",
102 1.2.2.2 gehenna sc->sc_dev.dv_xname);
103 1.2.2.2 gehenna
104 1.2.2.2 gehenna }
105 1.2.2.2 gehenna
106 1.2.2.2 gehenna static int
107 1.2.2.2 gehenna wdog_tickle(struct sysmon_wdog *smw)
108 1.2.2.2 gehenna {
109 1.2.2.2 gehenna uint32_t tsr;
110 1.2.2.2 gehenna
111 1.2.2.2 gehenna tsr = mfspr(SPR_TSR);
112 1.2.2.2 gehenna tsr |= TSR_ENW | TSR_WIS;
113 1.2.2.2 gehenna mtspr(SPR_TSR, tsr);
114 1.2.2.2 gehenna return (0);
115 1.2.2.2 gehenna }
116 1.2.2.2 gehenna
117 1.2.2.2 gehenna static int
118 1.2.2.2 gehenna wdog_setmode(struct sysmon_wdog *smw)
119 1.2.2.2 gehenna {
120 1.2.2.2 gehenna struct wdog_softc *sc = smw->smw_cookie;
121 1.2.2.2 gehenna uint32_t tcr, tsr;
122 1.2.2.2 gehenna
123 1.2.2.2 gehenna if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
124 1.2.2.2 gehenna if (sc->sc_wdog_armed) {
125 1.2.2.2 gehenna tsr = mfspr(SPR_TSR);
126 1.2.2.2 gehenna tsr &= ~(TSR_ENW | TSR_WIS);
127 1.2.2.2 gehenna mtspr(SPR_TSR, tsr);
128 1.2.2.2 gehenna }
129 1.2.2.2 gehenna } else {
130 1.2.2.2 gehenna if (smw->smw_period == WDOG_PERIOD_DEFAULT)
131 1.2.2.2 gehenna smw->smw_period = sc->sc_wdog_period;
132 1.2.2.2 gehenna else if (smw->smw_period != sc->sc_wdog_period) {
133 1.2.2.2 gehenna /*
134 1.2.2.2 gehenna * There's 4 set watchdog periods on the 405GP,
135 1.2.2.2 gehenna * but we only support the longest one (2.684
136 1.2.2.2 gehenna * seconds).
137 1.2.2.2 gehenna */
138 1.2.2.2 gehenna return (EOPNOTSUPP);
139 1.2.2.2 gehenna }
140 1.2.2.2 gehenna sc->sc_wdog_armed = 1;
141 1.2.2.2 gehenna
142 1.2.2.2 gehenna tcr = mfspr(SPR_TCR);
143 1.2.2.2 gehenna tcr |= TCR_WP_2_29 | TCR_WRC_SYSTEM;
144 1.2.2.2 gehenna mtspr(SPR_TCR, tcr);
145 1.2.2.2 gehenna
146 1.2.2.2 gehenna /* Arm the watchdog. */
147 1.2.2.2 gehenna wdog_tickle(smw);
148 1.2.2.2 gehenna }
149 1.2.2.2 gehenna return (0);
150 1.2.2.2 gehenna }
151