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