1/* $NetBSD: wdog.c,v 1.6 2011/07/07 05:06:44 matt Exp $ */
2/*-
3 * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
4 * Copyright (c) 2006 Garrett D'Amore.
5 * All rights reserved.
6 *
7 * Portions of this code were written by Garrett D'Amore for the
8 * Champaign-Urbana Community Wireless Network Project.
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above
16 *    copyright notice, this list of conditions and the following
17 *    disclaimer in the documentation and/or other materials provided
18 *    with the distribution.
19 * 3. All advertising materials mentioning features or use of this
20 *    software must display the following acknowledgements:
21 *      This product includes software developed by the Urbana-Champaign
22 *      Independent Media Center.
23 *	This product includes software developed by Garrett D'Amore.
24 * 4. Urbana-Champaign Independent Media Center's name and Garrett
25 *    D'Amore's name may not be used to endorse or promote products
26 *    derived from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
29 * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
33 * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42/*
43 * Copyright (c) 2002 Wasabi Systems, Inc.
44 * All rights reserved.
45 *
46 * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 *    notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 *    notice, this list of conditions and the following disclaimer in the
55 *    documentation and/or other materials provided with the distribution.
56 * 3. All advertising materials mentioning features or use of this software
57 *    must display the following acknowledgement:
58 *	This product includes software developed for the NetBSD Project by
59 *	Wasabi Systems, Inc.
60 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
61 *    or promote products derived from this software without specific prior
62 *    written permission.
63 *
64 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
66 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
68 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
69 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
70 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
71 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
72 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
73 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
74 * POSSIBILITY OF SUCH DAMAGE.
75 */
76
77/*
78 * Watchdog timer support for the AR531x.
79 */
80
81#include <sys/cdefs.h>
82__KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.6 2011/07/07 05:06:44 matt Exp $");
83
84#include <sys/param.h>
85#include <sys/cpu.h>
86#include <sys/device.h>
87#include <sys/wdog.h>
88
89#include <mips/atheros/include/platform.h>
90
91#include <dev/sysmon/sysmonvar.h>
92
93#ifndef	WDOG_DEFAULT_PERIOD
94#define	WDOG_DEFAULT_PERIOD	5
95#endif
96
97static int wdog_match(device_t, cfdata_t, void *);
98static void wdog_attach(device_t, device_t, void *);
99static int wdog_tickle(struct sysmon_wdog *);
100static int wdog_setmode(struct sysmon_wdog *);
101
102struct wdog_softc {
103	struct sysmon_wdog	sc_smw;
104	int			sc_wdog_period;
105	int			sc_wdog_max;
106	uint32_t		sc_wdog_reload;
107	uint32_t		sc_mult;
108};
109
110CFATTACH_DECL_NEW(wdog, sizeof(struct wdog_softc),
111    wdog_match, wdog_attach, NULL, NULL);
112
113static int
114wdog_match(device_t parent, struct cfdata *cf, void *aux)
115{
116
117	return (1);
118}
119
120static void
121wdog_attach(device_t parent, device_t self, void *aux)
122{
123	struct wdog_softc *sc = device_private(self);
124
125	sc->sc_mult = atheros_get_bus_freq();
126	sc->sc_wdog_period = WDOG_DEFAULT_PERIOD;
127	sc->sc_wdog_max = 0xffffffffU / sc->sc_mult;
128	sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
129	aprint_normal(": %d second period\n", sc->sc_wdog_period);
130
131	sc->sc_smw.smw_name = device_xname(self);
132	sc->sc_smw.smw_cookie = sc;
133	sc->sc_smw.smw_setmode = wdog_setmode;
134	sc->sc_smw.smw_tickle = wdog_tickle;
135	sc->sc_smw.smw_period = sc->sc_wdog_period;
136
137	if (sysmon_wdog_register(&sc->sc_smw) != 0) {
138		aprint_error_dev(self,
139		    "unable to register with sysmon\n");
140	}
141}
142
143static int
144wdog_tickle(struct sysmon_wdog *smw)
145{
146	struct wdog_softc *sc = smw->smw_cookie;
147
148	atheros_wdog_reload(sc->sc_wdog_reload);
149	return (0);
150}
151
152static int
153wdog_setmode(struct sysmon_wdog *smw)
154{
155	struct wdog_softc *sc = smw->smw_cookie;
156
157	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
158		atheros_wdog_reload(0);
159	} else {
160
161		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
162			smw->smw_period = sc->sc_wdog_period;
163		else if (smw->smw_period != sc->sc_wdog_period) {
164			/*
165			 * we can't set watchdog periods in excess of
166			 * the processor maximum.
167			 */
168			if (smw->smw_period > sc->sc_wdog_max) {
169				return EOPNOTSUPP;
170			}
171			sc->sc_wdog_period = smw->smw_period;
172			sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
173		}
174
175		atheros_wdog_reload(sc->sc_wdog_reload);
176	}
177
178	return (0);
179}
180