1/* $NetBSD: e500wdog.c,v 1.4 2020/07/06 09:34:16 rin 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__KERNEL_RCSID(0, "$NetBSD: e500wdog.c,v 1.4 2020/07/06 09:34:16 rin Exp $"); 39 40#include "ioconf.h" 41 42#include <sys/param.h> 43#include <sys/cpu.h> 44#include <sys/device.h> 45#include <sys/wdog.h> 46 47#include <dev/sysmon/sysmonvar.h> 48 49#include <sys/intr.h> 50 51#include <powerpc/spr.h> 52#include <powerpc/booke/spr.h> 53 54#include <powerpc/booke/cpuvar.h> 55 56struct e500wdog_softc { 57 device_t sc_dev; 58 struct sysmon_wdog sc_smw; 59 u_int sc_wdog_period; 60 bool sc_wdog_armed; 61 uint64_t sc_timebase; 62}; 63#ifndef PQ3WDOG_PERIOD_DEFAULT 64#define PQ3WDOG_PERIOD_DEFAULT 10 65#endif 66 67static int e500wdog_match(device_t, cfdata_t, void *); 68static void e500wdog_attach(device_t, device_t, void *); 69 70CFATTACH_DECL_NEW(e500wdog, sizeof(struct e500wdog_softc), 71 e500wdog_match, e500wdog_attach, NULL, NULL); 72 73static int 74e500wdog_match(device_t parent, cfdata_t cf, void *aux) 75{ 76 struct cpunode_softc * const psc = device_private(parent); 77 struct cpunode_attach_args * const cna = aux; 78 struct cpunode_locators * const cnl = &cna->cna_locs; 79 80 if (!board_info_get_bool("pq3") 81 || cnl->cnl_instance != 0 82 || (psc->sc_children & cna->cna_childmask) 83 || strcmp(cnl->cnl_name, "wdog") != 0) 84 return 0; 85 86 return 1; 87} 88 89static int 90e500wdog_tickle(struct sysmon_wdog *smw) 91{ 92 mtspr(SPR_TSR, TSR_ENW); 93 return 0; 94} 95 96static int 97e500wdog_setmode(struct sysmon_wdog *smw) 98{ 99 struct e500wdog_softc * const sc = smw->smw_cookie; 100 101 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) { 102 /* 103 * We can't disarm the watchdog. 104 */ 105 if (sc->sc_wdog_armed) 106 return EBUSY; 107 } else { 108 /* 109 * If no changes, don't do anything. 110 */ 111 if (sc->sc_wdog_armed 112 && smw->smw_period == sc->sc_wdog_period) { 113 mtspr(SPR_TSR, TSR_ENW); 114 return 0; 115 } 116 printf("%s:%d %u %u\n", __func__, __LINE__, 117 sc->sc_wdog_period, smw->smw_period); 118 if (smw->smw_period == WDOG_PERIOD_DEFAULT) { 119 sc->sc_wdog_period = PQ3WDOG_PERIOD_DEFAULT; 120 smw->smw_period = PQ3WDOG_PERIOD_DEFAULT; 121 } 122 mtspr(SPR_TSR, TSR_ENW); 123 uint32_t tcr = mfspr(SPR_TCR); 124 if (!sc->sc_wdog_armed) 125 tcr = (tcr & ~TCR_WRC) | TCR_WRC_RESET | TCR_WIE; 126 const uint64_t period = sc->sc_wdog_period * sc->sc_timebase / 3; 127 const u_int wp = __builtin_clz(period) + 1; 128 tcr &= ~(TCR_WP|TCR_WPEXT); 129 tcr |= __SHIFTIN(wp, TCR_WP) | __SHIFTIN(wp >> 2, TCR_WPEXT); 130 mtspr(SPR_TCR, tcr); 131 sc->sc_wdog_period = 132 ((1ULL << (63 - wp)) + sc->sc_timebase - 1) 133 / sc->sc_timebase; 134 sc->sc_wdog_armed = true; 135 } 136 return 0; 137} 138 139static void 140e500wdog_attach(device_t parent, device_t self, void *aux) 141{ 142 struct cpunode_softc * const psc = device_private(parent); 143 struct e500wdog_softc * const sc = device_private(self); 144 struct cpunode_attach_args * const cna = aux; 145 146 psc->sc_children |= cna->cna_childmask; 147 sc->sc_dev = self; 148 149 sc->sc_timebase = board_info_get_number("timebase-frequency"); 150 const uint32_t tcr = mfspr(SPR_TCR); 151 u_int wp = __SHIFTOUT(tcr, TCR_WP) | (__SHIFTOUT(tcr, TCR_WPEXT) << 2); 152 153#if DEBUG > 1 154 printf(" tcr=%#x wp=%u", tcr, wp); 155 printf(" timebase=%"PRIu64, sc->sc_timebase); 156#endif 157 sc->sc_wdog_armed = (tcr & TCR_WRC) != 0; 158 if (sc->sc_wdog_armed) 159 sc->sc_wdog_period = ((1ULL << (63 - wp)) + sc->sc_timebase - 1) 160 / sc->sc_timebase; 161 else 162 sc->sc_wdog_period = PQ3WDOG_PERIOD_DEFAULT; 163 164 aprint_normal(": default period is %u seconds%s\n", 165 sc->sc_wdog_period, 166 sc->sc_wdog_armed ? " (wdog is active)" : ""); 167 168 sc->sc_smw.smw_name = device_xname(sc->sc_dev); 169 sc->sc_smw.smw_cookie = sc; 170 sc->sc_smw.smw_setmode = e500wdog_setmode; 171 sc->sc_smw.smw_tickle = e500wdog_tickle; 172 sc->sc_smw.smw_period = sc->sc_wdog_period; 173 174 if (sysmon_wdog_register(&sc->sc_smw) != 0) 175 aprint_error_dev(self, "unable to register with sysmon\n"); 176 177#if 1 178 if (sc->sc_wdog_armed) { 179 int error = sysmon_wdog_setmode(&sc->sc_smw, WDOG_MODE_KTICKLE, 180 sc->sc_wdog_period); 181 if (error) 182 aprint_error_dev(self, 183 "failed to start kernel tickler: %d\n", error); 184 } 185#else 186 //mtspr(SPR_TSR, TSR_ENW); 187#endif 188} 189