gemini_wdt.c revision 1.3 1 1.3 dyoung /* $NetBSD: gemini_wdt.c,v 1.3 2011/07/01 19:32:28 dyoung Exp $ */
2 1.1 matt
3 1.1 matt /*
4 1.1 matt * OMAP watchdog timers, common code
5 1.1 matt *
6 1.1 matt * Copyright (c) 2007 Microsoft
7 1.1 matt * All rights reserved.
8 1.1 matt *
9 1.1 matt * Redistribution and use in source and binary forms, with or without
10 1.1 matt * modification, are permitted provided that the following conditions
11 1.1 matt * are met:
12 1.1 matt * 1. Redistributions of source code must retain the above copyright
13 1.1 matt * notice, this list of conditions and the following disclaimer.
14 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 matt * notice, this list of conditions and the following disclaimer in the
16 1.1 matt * documentation and/or other materials provided with the distribution.
17 1.1 matt * 3. All advertising materials mentioning features or use of this software
18 1.1 matt * must display the following acknowledgement:
19 1.1 matt * This product includes software developed by Microsoft
20 1.1 matt *
21 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 1.1 matt * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 1.1 matt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 matt * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTERS BE LIABLE FOR ANY DIRECT,
25 1.1 matt * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 1.1 matt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 1.1 matt * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 matt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 matt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 matt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 matt * SUCH DAMAGE.
32 1.1 matt */
33 1.1 matt
34 1.1 matt #include <sys/cdefs.h>
35 1.1 matt
36 1.1 matt #include <sys/param.h>
37 1.1 matt #include <sys/callout.h>
38 1.1 matt #include <sys/cdefs.h>
39 1.1 matt #include <sys/device.h>
40 1.1 matt #include <sys/kernel.h>
41 1.1 matt #include <sys/systm.h>
42 1.1 matt #include <sys/wdog.h>
43 1.1 matt
44 1.1 matt #include <machine/param.h>
45 1.3 dyoung #include <sys/bus.h>
46 1.1 matt #include <dev/sysmon/sysmonvar.h>
47 1.1 matt
48 1.1 matt #include <arm/gemini/gemini_wdtvar.h>
49 1.1 matt #include <arm/gemini/gemini_reg.h>
50 1.1 matt
51 1.1 matt geminiwdt_softc_t *geminiwdt_sc;
52 1.1 matt
53 1.1 matt #define WATCHDOG_COUNT(nsec) \
54 1.1 matt (GEMINI_WDT_CLOCK_FREQ * (nsec))
55 1.1 matt
56 1.1 matt
57 1.1 matt static void
58 1.1 matt geminiwdt_start(void)
59 1.1 matt {
60 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc;
61 1.1 matt uint32_t r;
62 1.1 matt
63 1.1 matt r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR);
64 1.1 matt r |= (WDT_WDCR_RESET_ENB
65 1.1 matt |WDT_WDCR_ENB);
66 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR, r);
67 1.1 matt }
68 1.1 matt
69 1.1 matt static void
70 1.1 matt geminiwdt_stop(void)
71 1.1 matt {
72 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc;
73 1.1 matt uint32_t r;
74 1.1 matt
75 1.1 matt r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR);
76 1.1 matt r &= ~(WDT_WDCR_EXTSIG_ENB
77 1.1 matt |WDT_WDCR_RESET_ENB
78 1.1 matt |WDT_WDCR_INTR_ENB
79 1.1 matt |WDT_WDCR_ENB);
80 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR, r);
81 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh,
82 1.1 matt GEMINI_WDT_WDCLEAR, WDT_WDCLEAR_CLEAR);
83 1.1 matt }
84 1.1 matt
85 1.1 matt static void
86 1.1 matt geminiwdt_do_set_timeout(void)
87 1.1 matt {
88 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc;
89 1.1 matt uint32_t r;
90 1.1 matt
91 1.1 matt /*
92 1.1 matt * Disable the watchdog timer
93 1.1 matt */
94 1.1 matt if (sc->sc_armed)
95 1.1 matt geminiwdt_stop();
96 1.1 matt
97 1.1 matt /*
98 1.1 matt * Set WdLoad register
99 1.1 matt */
100 1.1 matt r = (sc->sc_smw.smw_period != 0) ?
101 1.1 matt WATCHDOG_COUNT(sc->sc_smw.smw_period) : WDT_WDLOAD_DFLT;
102 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDLOAD, r);
103 1.1 matt
104 1.1 matt /*
105 1.1 matt * feed MAGIC treat to dog
106 1.1 matt */
107 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh,
108 1.1 matt GEMINI_WDT_WDRESTART, WDT_WDRESTART_MAGIC);
109 1.1 matt
110 1.1 matt /*
111 1.1 matt * Select PCLK clock source
112 1.1 matt */
113 1.1 matt r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR);
114 1.1 matt r &= ~WDCR_CLKSRC_5MHZ;
115 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR, r);
116 1.1 matt
117 1.1 matt /*
118 1.1 matt * Enable the timer
119 1.1 matt */
120 1.1 matt if (sc->sc_armed)
121 1.1 matt geminiwdt_start();
122 1.1 matt }
123 1.1 matt
124 1.1 matt void
125 1.1 matt geminiwdt_set_timeout(unsigned int period)
126 1.1 matt {
127 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc;
128 1.1 matt int s = splhigh();
129 1.1 matt
130 1.1 matt if (period != sc->sc_smw.smw_period) {
131 1.1 matt sc->sc_smw.smw_period = period;
132 1.1 matt geminiwdt_do_set_timeout();
133 1.1 matt }
134 1.1 matt
135 1.1 matt splx(s);
136 1.1 matt }
137 1.1 matt
138 1.1 matt int
139 1.1 matt geminiwdt_enable(int enable)
140 1.1 matt {
141 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc;
142 1.1 matt int s;
143 1.1 matt int prev_state = geminiwdt_sc->sc_armed;
144 1.1 matt
145 1.1 matt /* Normalize the int to a boolean so we can compare values directly.
146 1.1 matt */
147 1.1 matt enable = !!enable;
148 1.1 matt
149 1.1 matt s = splhigh();
150 1.1 matt
151 1.1 matt if (enable != sc->sc_armed) {
152 1.1 matt if (enable) {
153 1.1 matt /* Make sure that the watchdog timeout is up to date.
154 1.1 matt */
155 1.1 matt geminiwdt_do_set_timeout();
156 1.1 matt geminiwdt_start();
157 1.1 matt } else {
158 1.1 matt geminiwdt_stop();
159 1.1 matt }
160 1.1 matt sc->sc_armed = enable;
161 1.1 matt }
162 1.1 matt
163 1.1 matt splx(s);
164 1.1 matt return prev_state;
165 1.1 matt }
166 1.1 matt
167 1.1 matt int
168 1.1 matt geminiwdt_setmode(struct sysmon_wdog *smw)
169 1.1 matt {
170 1.1 matt geminiwdt_softc_t *sc = smw->smw_cookie;
171 1.1 matt int error = 0;
172 1.1 matt
173 1.1 matt if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
174 1.1 matt geminiwdt_enable(0);
175 1.1 matt } else {
176 1.1 matt if (smw->smw_period == WDOG_PERIOD_DEFAULT)
177 1.1 matt sc->sc_smw.smw_period = WDT_WDLOAD_DFLT;
178 1.1 matt else
179 1.1 matt sc->sc_smw.smw_period = smw->smw_period;
180 1.1 matt geminiwdt_set_timeout(sc->sc_smw.smw_period);
181 1.1 matt geminiwdt_enable(1);
182 1.1 matt }
183 1.1 matt return error;
184 1.1 matt }
185 1.1 matt
186 1.1 matt int
187 1.1 matt geminiwdt_tickle(struct sysmon_wdog *smw)
188 1.1 matt {
189 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc;
190 1.1 matt int s = splhigh();
191 1.1 matt
192 1.1 matt /*
193 1.1 matt * feed the dog a MAGIC treat
194 1.1 matt */
195 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh,
196 1.1 matt GEMINI_WDT_WDRESTART, WDT_WDRESTART_MAGIC);
197 1.1 matt
198 1.1 matt splx(s);
199 1.1 matt return 0;
200 1.1 matt }
201