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