No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / mips / adm5120 / dev / admwdog.c
blob3d80f2ceb8a49396ceef001a94110b5a2fad6112
1 /* $NetBSD: admwdog.c,v 1.1 2007/03/20 08:52:02 dyoung Exp $ */
3 /*-
4 * Copyright (c) 2007 David Young. All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: admwdog.c,v 1.1 2007/03/20 08:52:02 dyoung Exp $");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/wdog.h>
38 #include <sys/kernel.h> /* for hz */
40 #include <machine/bus.h>
42 #include <dev/sysmon/sysmonvar.h>
44 #include <mips/adm5120/include/adm5120reg.h>
45 #include <mips/adm5120/include/adm5120var.h>
46 #include <mips/adm5120/include/adm5120_obiovar.h>
47 #include <mips/adm5120/dev/if_admswvar.h>
49 /* maximum watchdog period, seconds */
50 #define ADMWDOG_MAX_PERIOD (__SHIFTOUT_MASK(ADM5120_WDOG_WTS_MASK) / 100)
52 static int admwdog_setmode(struct sysmon_wdog *smw);
53 static int admwdog_tickle(struct sysmon_wdog *smw);
55 static inline uint32_t
56 admwdog_read(struct admsw_softc *sc)
58 return bus_space_read_4(sc->sc_st, sc->sc_ioh, ADM5120_WDOG0);
61 static inline void
62 admwdog_write(struct admsw_softc *sc, uint32_t val)
64 bus_space_write_4(sc->sc_st, sc->sc_ioh, ADM5120_WDOG0, val);
67 static void
68 admwdog_reset(struct admsw_softc *sc)
70 int s;
71 uint32_t wdog0;
73 wdog0 = __SHIFTIN(sc->sc_smw.smw_period * 100, ADM5120_WDOG_WTS_MASK) |
74 __SHIFTIN(1, ADM5120_WDOG_WT_MASK);
76 s = splhigh();
78 admwdog_write(sc, wdog0);
79 (void)admwdog_read(sc);
80 admwdog_write(sc, ADM5120_WDOG0_WTTR | wdog0);
82 splx(s);
85 static int
86 admwdog_setmode(struct sysmon_wdog *smw)
88 struct admsw_softc *sc = smw->smw_cookie;
90 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
91 admwdog_write(sc, 0);
92 return 0;
95 if (smw->smw_period == WDOG_PERIOD_DEFAULT)
96 smw->smw_period = 32;
97 else if (smw->smw_period > ADMWDOG_MAX_PERIOD)
98 return EINVAL;
100 admwdog_reset(sc);
102 return 0;
105 static int
106 admwdog_tickle(struct sysmon_wdog *smw)
108 struct admsw_softc *sc = smw->smw_cookie;
110 (void)admwdog_read(sc);
111 return 0;
114 void
115 admwdog_attach(struct admsw_softc *sc)
117 struct sysmon_wdog *smw = &sc->sc_smw;
119 /* deactivate watchdog */
120 admwdog_write(sc, 0);
122 smw->smw_name = device_xname(&sc->sc_dev);
123 smw->smw_cookie = sc;
124 smw->smw_setmode = admwdog_setmode;
125 smw->smw_tickle = admwdog_tickle;
126 smw->smw_period = ADMWDOG_MAX_PERIOD;
128 sysmon_wdog_register(&sc->sc_smw);