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