1 /* $NetBSD: admwdog.c,v 1.4 2012/10/27 17:18:01 chs Exp $ */
2
3 /*-
4 * Copyright (c) 2007 David Young. All rights reserved.
5 *
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.
15 *
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.
28 */
29
30 #include <sys/cdefs.h>
31
32 __KERNEL_RCSID(0, "$NetBSD: admwdog.c,v 1.4 2012/10/27 17:18:01 chs Exp $");
33
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 */
39
40 #include <sys/bus.h>
41
42 #include <dev/sysmon/sysmonvar.h>
43
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>
48
49 /* maximum watchdog period, seconds */
50 #define ADMWDOG_MAX_PERIOD (__SHIFTOUT_MASK(ADM5120_WDOG_WTS_MASK) / 100)
51
52 static int admwdog_setmode(struct sysmon_wdog *smw);
53 static int admwdog_tickle(struct sysmon_wdog *smw);
54
55 static inline uint32_t
admwdog_read(struct admsw_softc * sc)56 admwdog_read(struct admsw_softc *sc)
57 {
58 return bus_space_read_4(sc->sc_st, sc->sc_ioh, ADM5120_WDOG0);
59 }
60
61 static inline void
admwdog_write(struct admsw_softc * sc,uint32_t val)62 admwdog_write(struct admsw_softc *sc, uint32_t val)
63 {
64 bus_space_write_4(sc->sc_st, sc->sc_ioh, ADM5120_WDOG0, val);
65 }
66
67 static void
admwdog_reset(struct admsw_softc * sc)68 admwdog_reset(struct admsw_softc *sc)
69 {
70 int s;
71 uint32_t wdog0;
72
73 wdog0 = __SHIFTIN(sc->sc_smw.smw_period * 100, ADM5120_WDOG_WTS_MASK) |
74 __SHIFTIN(1, ADM5120_WDOG_WT_MASK);
75
76 s = splhigh();
77
78 admwdog_write(sc, wdog0);
79 (void)admwdog_read(sc);
80 admwdog_write(sc, ADM5120_WDOG0_WTTR | wdog0);
81
82 splx(s);
83 }
84
85 static int
admwdog_setmode(struct sysmon_wdog * smw)86 admwdog_setmode(struct sysmon_wdog *smw)
87 {
88 struct admsw_softc *sc = smw->smw_cookie;
89
90 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
91 admwdog_write(sc, 0);
92 return 0;
93 }
94
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;
99
100 admwdog_reset(sc);
101
102 return 0;
103 }
104
105 static int
admwdog_tickle(struct sysmon_wdog * smw)106 admwdog_tickle(struct sysmon_wdog *smw)
107 {
108 struct admsw_softc *sc = smw->smw_cookie;
109
110 (void)admwdog_read(sc);
111 return 0;
112 }
113
114 void
admwdog_attach(struct admsw_softc * sc)115 admwdog_attach(struct admsw_softc *sc)
116 {
117 struct sysmon_wdog *smw = &sc->sc_smw;
118
119 /* deactivate watchdog */
120 admwdog_write(sc, 0);
121
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;
127
128 sysmon_wdog_register(&sc->sc_smw);
129 }
130