1202375Srdivacky/* $NetBSD: wdog.c,v 1.6 2011/07/07 05:06:44 matt Exp $ */
2202375Srdivacky/*-
3202375Srdivacky * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
4202375Srdivacky * Copyright (c) 2006 Garrett D'Amore.
5202375Srdivacky * All rights reserved.
6202375Srdivacky *
7202375Srdivacky * Portions of this code were written by Garrett D'Amore for the
8202375Srdivacky * Champaign-Urbana Community Wireless Network Project.
9202375Srdivacky *
10202375Srdivacky * Redistribution and use in source and binary forms, with or
11202375Srdivacky * without modification, are permitted provided that the following
12202375Srdivacky * conditions are met:
13202375Srdivacky * 1. Redistributions of source code must retain the above copyright
14202375Srdivacky *    notice, this list of conditions and the following disclaimer.
15202375Srdivacky * 2. Redistributions in binary form must reproduce the above
16202375Srdivacky *    copyright notice, this list of conditions and the following
17202375Srdivacky *    disclaimer in the documentation and/or other materials provided
18202375Srdivacky *    with the distribution.
19202375Srdivacky * 3. All advertising materials mentioning features or use of this
20202375Srdivacky *    software must display the following acknowledgements:
21202375Srdivacky *      This product includes software developed by the Urbana-Champaign
22202375Srdivacky *      Independent Media Center.
23202375Srdivacky *	This product includes software developed by Garrett D'Amore.
24202375Srdivacky * 4. Urbana-Champaign Independent Media Center's name and Garrett
25202375Srdivacky *    D'Amore's name may not be used to endorse or promote products
26202375Srdivacky *    derived from this software without specific prior written permission.
27202375Srdivacky *
28202375Srdivacky * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
29202375Srdivacky * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
30202375Srdivacky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31202375Srdivacky * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32202375Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
33202375Srdivacky * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
34202375Srdivacky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35202375Srdivacky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36202375Srdivacky * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37218893Sdim * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38202375Srdivacky * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39202375Srdivacky * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40202375Srdivacky * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41202375Srdivacky */
42202375Srdivacky/*
43202375Srdivacky * Copyright (c) 2002 Wasabi Systems, Inc.
44202375Srdivacky * All rights reserved.
45202375Srdivacky *
46202375Srdivacky * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
47202375Srdivacky *
48202375Srdivacky * Redistribution and use in source and binary forms, with or without
49202375Srdivacky * modification, are permitted provided that the following conditions
50202375Srdivacky * are met:
51202375Srdivacky * 1. Redistributions of source code must retain the above copyright
52202375Srdivacky *    notice, this list of conditions and the following disclaimer.
53202375Srdivacky * 2. Redistributions in binary form must reproduce the above copyright
54202375Srdivacky *    notice, this list of conditions and the following disclaimer in the
55202375Srdivacky *    documentation and/or other materials provided with the distribution.
56202375Srdivacky * 3. All advertising materials mentioning features or use of this software
57202375Srdivacky *    must display the following acknowledgement:
58202375Srdivacky *	This product includes software developed for the NetBSD Project by
59202375Srdivacky *	Wasabi Systems, Inc.
60202375Srdivacky * 4. The name of Wasabi Systems, Inc. may not be used to endorse
61202375Srdivacky *    or promote products derived from this software without specific prior
62202375Srdivacky *    written permission.
63202375Srdivacky *
64202375Srdivacky * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
65202375Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
66202375Srdivacky * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67202375Srdivacky * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
68202375Srdivacky * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
69202375Srdivacky * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
70202375Srdivacky * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
71202375Srdivacky * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
72202375Srdivacky * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
73202375Srdivacky * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
74202375Srdivacky * POSSIBILITY OF SUCH DAMAGE.
75202375Srdivacky */
76202375Srdivacky
77202375Srdivacky/*
78202375Srdivacky * Watchdog timer support for the AR531x.
79202375Srdivacky */
80202375Srdivacky
81202375Srdivacky#include <sys/cdefs.h>
82202375Srdivacky__KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.6 2011/07/07 05:06:44 matt Exp $");
83202375Srdivacky
84202375Srdivacky#include <sys/param.h>
85202375Srdivacky#include <sys/cpu.h>
86202375Srdivacky#include <sys/device.h>
87202375Srdivacky#include <sys/wdog.h>
88202375Srdivacky
89202375Srdivacky#include <mips/atheros/include/platform.h>
90202375Srdivacky
91202375Srdivacky#include <dev/sysmon/sysmonvar.h>
92202375Srdivacky
93202375Srdivacky#ifndef	WDOG_DEFAULT_PERIOD
94202375Srdivacky#define	WDOG_DEFAULT_PERIOD	5
95202375Srdivacky#endif
96202375Srdivacky
97202375Srdivackystatic int wdog_match(device_t, cfdata_t, void *);
98202375Srdivackystatic void wdog_attach(device_t, device_t, void *);
99202375Srdivackystatic int wdog_tickle(struct sysmon_wdog *);
100202375Srdivackystatic int wdog_setmode(struct sysmon_wdog *);
101202375Srdivacky
102202375Srdivackystruct wdog_softc {
103202375Srdivacky	struct sysmon_wdog	sc_smw;
104202375Srdivacky	int			sc_wdog_period;
105202375Srdivacky	int			sc_wdog_max;
106202375Srdivacky	uint32_t		sc_wdog_reload;
107204642Srdivacky	uint32_t		sc_mult;
108202375Srdivacky};
109202375Srdivacky
110203954SrdivackyCFATTACH_DECL_NEW(wdog, sizeof(struct wdog_softc),
111202375Srdivacky    wdog_match, wdog_attach, NULL, NULL);
112202375Srdivacky
113202375Srdivackystatic int
114202375Srdivackywdog_match(device_t parent, struct cfdata *cf, void *aux)
115202375Srdivacky{
116202375Srdivacky
117202375Srdivacky	return (1);
118202375Srdivacky}
119202375Srdivacky
120202375Srdivackystatic void
121202375Srdivackywdog_attach(device_t parent, device_t self, void *aux)
122202375Srdivacky{
123202375Srdivacky	struct wdog_softc *sc = device_private(self);
124218893Sdim
125202375Srdivacky	sc->sc_mult = atheros_get_bus_freq();
126202375Srdivacky	sc->sc_wdog_period = WDOG_DEFAULT_PERIOD;
127202375Srdivacky	sc->sc_wdog_max = 0xffffffffU / sc->sc_mult;
128202375Srdivacky	sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
129218893Sdim	aprint_normal(": %d second period\n", sc->sc_wdog_period);
130218893Sdim
131202375Srdivacky	sc->sc_smw.smw_name = device_xname(self);
132202375Srdivacky	sc->sc_smw.smw_cookie = sc;
133202375Srdivacky	sc->sc_smw.smw_setmode = wdog_setmode;
134202375Srdivacky	sc->sc_smw.smw_tickle = wdog_tickle;
135202375Srdivacky	sc->sc_smw.smw_period = sc->sc_wdog_period;
136202375Srdivacky
137202375Srdivacky	if (sysmon_wdog_register(&sc->sc_smw) != 0) {
138202375Srdivacky		aprint_error_dev(self,
139202375Srdivacky		    "unable to register with sysmon\n");
140202375Srdivacky	}
141203954Srdivacky}
142202375Srdivacky
143202375Srdivackystatic int
144202375Srdivackywdog_tickle(struct sysmon_wdog *smw)
145203954Srdivacky{
146202375Srdivacky	struct wdog_softc *sc = smw->smw_cookie;
147202375Srdivacky
148202375Srdivacky	atheros_wdog_reload(sc->sc_wdog_reload);
149202375Srdivacky	return (0);
150202375Srdivacky}
151202375Srdivacky
152202375Srdivackystatic int
153202375Srdivackywdog_setmode(struct sysmon_wdog *smw)
154202375Srdivacky{
155202375Srdivacky	struct wdog_softc *sc = smw->smw_cookie;
156202375Srdivacky
157202375Srdivacky	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
158202375Srdivacky		atheros_wdog_reload(0);
159202375Srdivacky	} else {
160202375Srdivacky
161202375Srdivacky		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
162202375Srdivacky			smw->smw_period = sc->sc_wdog_period;
163202375Srdivacky		else if (smw->smw_period != sc->sc_wdog_period) {
164202375Srdivacky			/*
165202375Srdivacky			 * we can't set watchdog periods in excess of
166202375Srdivacky			 * the processor maximum.
167202375Srdivacky			 */
168202375Srdivacky			if (smw->smw_period > sc->sc_wdog_max) {
169202375Srdivacky				return EOPNOTSUPP;
170202375Srdivacky			}
171202375Srdivacky			sc->sc_wdog_period = smw->smw_period;
172202375Srdivacky			sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
173202375Srdivacky		}
174202375Srdivacky
175202375Srdivacky		atheros_wdog_reload(sc->sc_wdog_reload);
176202375Srdivacky	}
177202375Srdivacky
178202375Srdivacky	return (0);
179202375Srdivacky}
180202375Srdivacky