admwdog.c revision 1.4 1 1.4 chs /* $NetBSD: admwdog.c,v 1.4 2012/10/27 17:18:01 chs 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 *
16 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
17 1.1 dyoung * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 1.1 dyoung * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 1.1 dyoung * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
20 1.1 dyoung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 1.1 dyoung * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 1.1 dyoung * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 1.1 dyoung * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 dyoung * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 1.1 dyoung * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 1.1 dyoung * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 1.1 dyoung * OF SUCH DAMAGE.
28 1.1 dyoung */
29 1.1 dyoung
30 1.1 dyoung #include <sys/cdefs.h>
31 1.1 dyoung
32 1.4 chs __KERNEL_RCSID(0, "$NetBSD: admwdog.c,v 1.4 2012/10/27 17:18:01 chs Exp $");
33 1.1 dyoung
34 1.1 dyoung #include <sys/param.h>
35 1.1 dyoung #include <sys/systm.h>
36 1.1 dyoung #include <sys/device.h>
37 1.1 dyoung #include <sys/wdog.h>
38 1.1 dyoung #include <sys/kernel.h> /* for hz */
39 1.1 dyoung
40 1.3 dyoung #include <sys/bus.h>
41 1.1 dyoung
42 1.1 dyoung #include <dev/sysmon/sysmonvar.h>
43 1.1 dyoung
44 1.1 dyoung #include <mips/adm5120/include/adm5120reg.h>
45 1.1 dyoung #include <mips/adm5120/include/adm5120var.h>
46 1.1 dyoung #include <mips/adm5120/include/adm5120_obiovar.h>
47 1.1 dyoung #include <mips/adm5120/dev/if_admswvar.h>
48 1.1 dyoung
49 1.1 dyoung /* maximum watchdog period, seconds */
50 1.1 dyoung #define ADMWDOG_MAX_PERIOD (__SHIFTOUT_MASK(ADM5120_WDOG_WTS_MASK) / 100)
51 1.1 dyoung
52 1.1 dyoung static int admwdog_setmode(struct sysmon_wdog *smw);
53 1.1 dyoung static int admwdog_tickle(struct sysmon_wdog *smw);
54 1.1 dyoung
55 1.1 dyoung static inline uint32_t
56 1.1 dyoung admwdog_read(struct admsw_softc *sc)
57 1.1 dyoung {
58 1.1 dyoung return bus_space_read_4(sc->sc_st, sc->sc_ioh, ADM5120_WDOG0);
59 1.1 dyoung }
60 1.1 dyoung
61 1.1 dyoung static inline void
62 1.1 dyoung admwdog_write(struct admsw_softc *sc, uint32_t val)
63 1.1 dyoung {
64 1.1 dyoung bus_space_write_4(sc->sc_st, sc->sc_ioh, ADM5120_WDOG0, val);
65 1.1 dyoung }
66 1.1 dyoung
67 1.1 dyoung static void
68 1.1 dyoung admwdog_reset(struct admsw_softc *sc)
69 1.1 dyoung {
70 1.1 dyoung int s;
71 1.1 dyoung uint32_t wdog0;
72 1.1 dyoung
73 1.1 dyoung wdog0 = __SHIFTIN(sc->sc_smw.smw_period * 100, ADM5120_WDOG_WTS_MASK) |
74 1.1 dyoung __SHIFTIN(1, ADM5120_WDOG_WT_MASK);
75 1.1 dyoung
76 1.1 dyoung s = splhigh();
77 1.1 dyoung
78 1.1 dyoung admwdog_write(sc, wdog0);
79 1.1 dyoung (void)admwdog_read(sc);
80 1.1 dyoung admwdog_write(sc, ADM5120_WDOG0_WTTR | wdog0);
81 1.1 dyoung
82 1.1 dyoung splx(s);
83 1.1 dyoung }
84 1.1 dyoung
85 1.1 dyoung static int
86 1.1 dyoung admwdog_setmode(struct sysmon_wdog *smw)
87 1.1 dyoung {
88 1.1 dyoung struct admsw_softc *sc = smw->smw_cookie;
89 1.1 dyoung
90 1.1 dyoung if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
91 1.1 dyoung admwdog_write(sc, 0);
92 1.1 dyoung return 0;
93 1.1 dyoung }
94 1.1 dyoung
95 1.1 dyoung if (smw->smw_period == WDOG_PERIOD_DEFAULT)
96 1.1 dyoung smw->smw_period = 32;
97 1.1 dyoung else if (smw->smw_period > ADMWDOG_MAX_PERIOD)
98 1.1 dyoung return EINVAL;
99 1.1 dyoung
100 1.1 dyoung admwdog_reset(sc);
101 1.1 dyoung
102 1.1 dyoung return 0;
103 1.1 dyoung }
104 1.1 dyoung
105 1.1 dyoung static int
106 1.1 dyoung admwdog_tickle(struct sysmon_wdog *smw)
107 1.1 dyoung {
108 1.1 dyoung struct admsw_softc *sc = smw->smw_cookie;
109 1.1 dyoung
110 1.1 dyoung (void)admwdog_read(sc);
111 1.1 dyoung return 0;
112 1.1 dyoung }
113 1.1 dyoung
114 1.1 dyoung void
115 1.1 dyoung admwdog_attach(struct admsw_softc *sc)
116 1.1 dyoung {
117 1.1 dyoung struct sysmon_wdog *smw = &sc->sc_smw;
118 1.1 dyoung
119 1.1 dyoung /* deactivate watchdog */
120 1.1 dyoung admwdog_write(sc, 0);
121 1.1 dyoung
122 1.4 chs smw->smw_name = device_xname(sc->sc_dev);
123 1.1 dyoung smw->smw_cookie = sc;
124 1.1 dyoung smw->smw_setmode = admwdog_setmode;
125 1.1 dyoung smw->smw_tickle = admwdog_tickle;
126 1.1 dyoung smw->smw_period = ADMWDOG_MAX_PERIOD;
127 1.1 dyoung
128 1.1 dyoung sysmon_wdog_register(&sc->sc_smw);
129 1.1 dyoung }
130