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