e500wdog.c revision 1.3 1 1.3 matt /* $NetBSD: e500wdog.c,v 1.3 2011/08/01 17:05:16 matt Exp $ */
2 1.2 matt /*-
3 1.2 matt * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4 1.2 matt * All rights reserved.
5 1.2 matt *
6 1.2 matt * This code is derived from software contributed to The NetBSD Foundation
7 1.2 matt * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8 1.2 matt * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9 1.2 matt *
10 1.2 matt * This material is based upon work supported by the Defense Advanced Research
11 1.2 matt * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12 1.2 matt * Contract No. N66001-09-C-2073.
13 1.2 matt * Approved for Public Release, Distribution Unlimited
14 1.2 matt *
15 1.2 matt * Redistribution and use in source and binary forms, with or without
16 1.2 matt * modification, are permitted provided that the following conditions
17 1.2 matt * are met:
18 1.2 matt * 1. Redistributions of source code must retain the above copyright
19 1.2 matt * notice, this list of conditions and the following disclaimer.
20 1.2 matt * 2. Redistributions in binary form must reproduce the above copyright
21 1.2 matt * notice, this list of conditions and the following disclaimer in the
22 1.2 matt * documentation and/or other materials provided with the distribution.
23 1.2 matt *
24 1.2 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 1.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 1.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 1.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 1.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 1.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 1.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 1.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 1.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 1.2 matt * POSSIBILITY OF SUCH DAMAGE.
35 1.2 matt */
36 1.2 matt
37 1.2 matt #include <sys/cdefs.h>
38 1.2 matt
39 1.3 matt __KERNEL_RCSID(0, "$NetBSD: e500wdog.c,v 1.3 2011/08/01 17:05:16 matt Exp $");
40 1.3 matt
41 1.2 matt #include <sys/param.h>
42 1.2 matt #include <sys/cpu.h>
43 1.2 matt #include <sys/device.h>
44 1.2 matt #include <sys/wdog.h>
45 1.2 matt
46 1.2 matt #include <dev/sysmon/sysmonvar.h>
47 1.2 matt
48 1.2 matt #include "ioconf.h"
49 1.2 matt
50 1.2 matt #include <sys/intr.h>
51 1.2 matt
52 1.2 matt #include <powerpc/spr.h>
53 1.2 matt #include <powerpc/booke/spr.h>
54 1.2 matt
55 1.2 matt #include <powerpc/booke/cpuvar.h>
56 1.2 matt
57 1.2 matt struct e500wdog_softc {
58 1.2 matt device_t sc_dev;
59 1.2 matt struct sysmon_wdog sc_smw;
60 1.2 matt u_int sc_wdog_period;
61 1.2 matt bool sc_wdog_armed;
62 1.2 matt uint64_t sc_timebase;
63 1.2 matt };
64 1.2 matt #ifndef PQ3WDOG_PERIOD_DEFAULT
65 1.2 matt #define PQ3WDOG_PERIOD_DEFAULT 10
66 1.2 matt #endif
67 1.2 matt
68 1.2 matt static int e500wdog_match(device_t, cfdata_t, void *);
69 1.2 matt static void e500wdog_attach(device_t, device_t, void *);
70 1.2 matt
71 1.2 matt CFATTACH_DECL_NEW(e500wdog, sizeof(struct e500wdog_softc),
72 1.2 matt e500wdog_match, e500wdog_attach, NULL, NULL);
73 1.2 matt
74 1.2 matt static int
75 1.2 matt e500wdog_match(device_t parent, cfdata_t cf, void *aux)
76 1.2 matt {
77 1.2 matt struct cpunode_softc * const psc = device_private(parent);
78 1.2 matt struct cpunode_attach_args * const cna = aux;
79 1.2 matt struct cpunode_locators * const cnl = &cna->cna_locs;
80 1.2 matt
81 1.2 matt if (!board_info_get_bool("pq3")
82 1.2 matt || cnl->cnl_instance != 0
83 1.2 matt || (psc->sc_children & cna->cna_childmask)
84 1.2 matt || strcmp(cnl->cnl_name, "wdog") != 0)
85 1.2 matt return 0;
86 1.2 matt
87 1.2 matt return 1;
88 1.2 matt }
89 1.2 matt
90 1.2 matt static int
91 1.2 matt e500wdog_tickle(struct sysmon_wdog *smw)
92 1.2 matt {
93 1.2 matt mtspr(SPR_TSR, TSR_ENW);
94 1.2 matt return 0;
95 1.2 matt }
96 1.2 matt
97 1.2 matt static int
98 1.2 matt e500wdog_setmode(struct sysmon_wdog *smw)
99 1.2 matt {
100 1.2 matt struct e500wdog_softc * const sc = smw->smw_cookie;
101 1.2 matt
102 1.2 matt if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
103 1.2 matt /*
104 1.2 matt * We can't disarm the watchdog.
105 1.2 matt */
106 1.2 matt if (sc->sc_wdog_armed)
107 1.2 matt return EBUSY;
108 1.2 matt } else {
109 1.2 matt /*
110 1.2 matt * If no changes, don't do anything.
111 1.2 matt */
112 1.2 matt if (sc->sc_wdog_armed
113 1.2 matt && smw->smw_period == sc->sc_wdog_period) {
114 1.2 matt mtspr(SPR_TSR, TSR_ENW);
115 1.2 matt return 0;
116 1.2 matt }
117 1.2 matt printf("%s:%d %u %u\n", __func__, __LINE__,
118 1.2 matt sc->sc_wdog_period, smw->smw_period);
119 1.2 matt if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
120 1.2 matt sc->sc_wdog_period = PQ3WDOG_PERIOD_DEFAULT;
121 1.2 matt smw->smw_period = PQ3WDOG_PERIOD_DEFAULT;
122 1.2 matt }
123 1.2 matt mtspr(SPR_TSR, TSR_ENW);
124 1.2 matt uint32_t tcr = mfspr(SPR_TCR);
125 1.2 matt if (!sc->sc_wdog_armed)
126 1.2 matt tcr = (tcr & ~TCR_WRC) | TCR_WRC_RESET | TCR_WIE;
127 1.2 matt const uint64_t period = sc->sc_wdog_period * sc->sc_timebase / 3;
128 1.2 matt const u_int wp = __builtin_clz(period) + 1;
129 1.2 matt tcr &= ~(TCR_WP|TCR_WPEXT);
130 1.2 matt tcr |= __SHIFTIN(wp, TCR_WP) | __SHIFTIN(wp >> 2, TCR_WPEXT);
131 1.2 matt mtspr(SPR_TCR, tcr);
132 1.2 matt sc->sc_wdog_period =
133 1.2 matt ((1ULL << (63 - wp)) + sc->sc_timebase - 1)
134 1.2 matt / sc->sc_timebase;
135 1.2 matt sc->sc_wdog_armed = true;
136 1.2 matt }
137 1.2 matt return 0;
138 1.2 matt }
139 1.2 matt
140 1.2 matt static void
141 1.2 matt e500wdog_attach(device_t parent, device_t self, void *aux)
142 1.2 matt {
143 1.2 matt struct cpunode_softc * const psc = device_private(parent);
144 1.2 matt struct e500wdog_softc * const sc = device_private(self);
145 1.2 matt struct cpunode_attach_args * const cna = aux;
146 1.2 matt
147 1.2 matt psc->sc_children |= cna->cna_childmask;
148 1.2 matt sc->sc_dev = self;
149 1.2 matt
150 1.2 matt sc->sc_timebase = board_info_get_number("timebase-frequency");
151 1.2 matt const uint32_t tcr = mfspr(SPR_TCR);
152 1.2 matt u_int wp = __SHIFTOUT(tcr, TCR_WP) | (__SHIFTOUT(tcr, TCR_WPEXT) << 2);
153 1.2 matt
154 1.2 matt #if DEBUG > 1
155 1.2 matt printf(" tcr=%#x wp=%u", tcr, wp);
156 1.2 matt printf(" timebase=%"PRIu64, sc->sc_timebase);
157 1.2 matt #endif
158 1.2 matt sc->sc_wdog_armed = (tcr & TCR_WRC) != 0;
159 1.2 matt if (sc->sc_wdog_armed)
160 1.2 matt sc->sc_wdog_period = ((1ULL << (63 - wp)) + sc->sc_timebase - 1)
161 1.2 matt / sc->sc_timebase;
162 1.2 matt else
163 1.2 matt sc->sc_wdog_period = PQ3WDOG_PERIOD_DEFAULT;
164 1.2 matt
165 1.2 matt aprint_normal(": default period is %u seconds%s\n",
166 1.2 matt sc->sc_wdog_period,
167 1.2 matt sc->sc_wdog_armed ? " (wdog is active)" : "");
168 1.2 matt
169 1.2 matt sc->sc_smw.smw_name = device_xname(sc->sc_dev);
170 1.2 matt sc->sc_smw.smw_cookie = sc;
171 1.2 matt sc->sc_smw.smw_setmode = e500wdog_setmode;
172 1.2 matt sc->sc_smw.smw_tickle = e500wdog_tickle;
173 1.2 matt sc->sc_smw.smw_period = sc->sc_wdog_period;
174 1.2 matt
175 1.2 matt if (sysmon_wdog_register(&sc->sc_smw) != 0)
176 1.2 matt aprint_error_dev(self, "unable to register with sysmon\n");
177 1.2 matt
178 1.2 matt #if 1
179 1.2 matt if (sc->sc_wdog_armed) {
180 1.2 matt int error = sysmon_wdog_setmode(&sc->sc_smw, WDOG_MODE_KTICKLE,
181 1.2 matt sc->sc_wdog_period);
182 1.2 matt if (error)
183 1.2 matt aprint_error_dev(self,
184 1.2 matt "failed to start kernel tickler: %d\n", error);
185 1.2 matt }
186 1.2 matt #else
187 1.2 matt //mtspr(SPR_TSR, TSR_ENW);
188 1.2 matt #endif
189 1.2 matt }
190