Home | History | Annotate | Line # | Download | only in ti
      1  1.2   thorpej /* $NetBSD: ti_wdt.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.2   thorpej __KERNEL_RCSID(0, "$NetBSD: ti_wdt.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/cpu.h>
     35  1.1  jmcneill #include <sys/device.h>
     36  1.1  jmcneill #include <sys/wdog.h>
     37  1.1  jmcneill 
     38  1.1  jmcneill #include <dev/sysmon/sysmonvar.h>
     39  1.1  jmcneill 
     40  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #include <arm/ti/ti_prcm.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #define	WDT_WDSC		0x10
     45  1.1  jmcneill #define	 WDSC_SOFTRESET		__BIT(1)
     46  1.1  jmcneill #define	WDT_WDST		0x14
     47  1.1  jmcneill #define	WDT_WISR		0x18
     48  1.1  jmcneill #define	WDT_WIER		0x1c
     49  1.1  jmcneill #define	WDT_WCLR		0x24
     50  1.1  jmcneill #define	 WCLR_PRE		__BIT(5)
     51  1.1  jmcneill #define	 WCLR_PTV		__BITS(4,2)
     52  1.1  jmcneill #define	WDT_WCRR		0x28
     53  1.1  jmcneill #define	WDT_WLDR		0x2c
     54  1.1  jmcneill #define	WDT_WTGR		0x30
     55  1.1  jmcneill #define	WDT_WWPS		0x34
     56  1.1  jmcneill #define	 WWPS_W_PEND_WDLY	__BIT(5)
     57  1.1  jmcneill #define	 WWPS_W_PEND_WSPR	__BIT(4)
     58  1.1  jmcneill #define	 WWPS_W_PEND_WTGR	__BIT(3)
     59  1.1  jmcneill #define	 WWPS_W_PEND_WLDR	__BIT(2)
     60  1.1  jmcneill #define	 WWPS_W_PEND_WCRR	__BIT(1)
     61  1.1  jmcneill #define	 WWPS_W_PEND_WCLR	__BIT(0)
     62  1.1  jmcneill #define	 WWPS_W_PEND_MASK	__BITS(5,0)
     63  1.1  jmcneill #define	WDT_WDLY		0x44
     64  1.1  jmcneill #define	WDT_WSPR		0x48
     65  1.1  jmcneill #define	WDT_WIRQSTATRAW		0x54
     66  1.1  jmcneill #define	WDT_WIRQSTAT		0x58
     67  1.1  jmcneill #define	WDT_WIRQENSET		0x5c
     68  1.1  jmcneill #define	WDT_WIRQENCLR		0x60
     69  1.1  jmcneill #define	 WIRQ_EVENT_DLY		__BIT(1)
     70  1.1  jmcneill #define	 WIRQ_EVENT_OVF		__BIT(0)
     71  1.1  jmcneill 
     72  1.1  jmcneill #define	WATCHDOG_PERIOD_DEFAULT		10
     73  1.1  jmcneill 
     74  1.2   thorpej static const struct device_compatible_entry compat_data[] = {
     75  1.2   thorpej 	{ .compat = "ti,omap3-wdt" },
     76  1.2   thorpej 	DEVICE_COMPAT_EOL
     77  1.1  jmcneill };
     78  1.1  jmcneill 
     79  1.1  jmcneill struct ti_wdt_softc {
     80  1.1  jmcneill 	device_t		sc_dev;
     81  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     82  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     83  1.1  jmcneill 
     84  1.1  jmcneill 	struct sysmon_wdog	sc_wdog;
     85  1.1  jmcneill 	u_int			sc_rate;
     86  1.1  jmcneill };
     87  1.1  jmcneill 
     88  1.1  jmcneill #define	RD4(sc, reg)		\
     89  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     90  1.1  jmcneill #define	WR4(sc, reg, val)	\
     91  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     92  1.1  jmcneill 
     93  1.1  jmcneill static void
     94  1.1  jmcneill ti_wdt_sync(struct ti_wdt_softc *sc, uint32_t mask)
     95  1.1  jmcneill {
     96  1.1  jmcneill 	uint32_t val;
     97  1.1  jmcneill 	int retry;
     98  1.1  jmcneill 
     99  1.1  jmcneill 	for (retry = 10000; retry > 0; retry--) {
    100  1.1  jmcneill 		val = RD4(sc, WDT_WWPS);
    101  1.1  jmcneill 		if ((val & mask) == 0)
    102  1.1  jmcneill 			return;
    103  1.1  jmcneill 	}
    104  1.1  jmcneill 
    105  1.1  jmcneill 	aprint_error_dev(sc->sc_dev,
    106  1.1  jmcneill 	    "reg sync timeout, mask=%#x, wwps=%#x\n", mask, val);
    107  1.1  jmcneill }
    108  1.1  jmcneill 
    109  1.1  jmcneill static void
    110  1.1  jmcneill ti_wdt_sync_all(struct ti_wdt_softc *sc)
    111  1.1  jmcneill {
    112  1.1  jmcneill 	ti_wdt_sync(sc, WWPS_W_PEND_MASK);
    113  1.1  jmcneill }
    114  1.1  jmcneill 
    115  1.1  jmcneill static int
    116  1.1  jmcneill ti_wdt_reset(struct ti_wdt_softc *sc)
    117  1.1  jmcneill {
    118  1.1  jmcneill 	uint32_t val;
    119  1.1  jmcneill 	int retry;
    120  1.1  jmcneill 
    121  1.1  jmcneill 	val = RD4(sc, WDT_WDSC);
    122  1.1  jmcneill 	val |= WDSC_SOFTRESET;
    123  1.1  jmcneill 	WR4(sc, WDT_WDSC, val);
    124  1.1  jmcneill 	for (retry = 10000; retry > 0; retry--) {
    125  1.1  jmcneill 		val = RD4(sc, WDT_WDSC);
    126  1.1  jmcneill 		if ((val & WDSC_SOFTRESET) == 0)
    127  1.1  jmcneill 			return 0;
    128  1.1  jmcneill 		delay(10);
    129  1.1  jmcneill 	}
    130  1.1  jmcneill 
    131  1.1  jmcneill 	return EIO;
    132  1.1  jmcneill }
    133  1.1  jmcneill 
    134  1.1  jmcneill static void
    135  1.1  jmcneill ti_wdt_stop(struct ti_wdt_softc *sc)
    136  1.1  jmcneill {
    137  1.1  jmcneill 	WR4(sc, WDT_WSPR, 0xaaaa);
    138  1.1  jmcneill 	ti_wdt_sync(sc, WWPS_W_PEND_WSPR);
    139  1.1  jmcneill 	WR4(sc, WDT_WSPR, 0x5555);
    140  1.1  jmcneill 	ti_wdt_sync(sc, WWPS_W_PEND_WSPR);
    141  1.1  jmcneill }
    142  1.1  jmcneill 
    143  1.1  jmcneill static void
    144  1.1  jmcneill ti_wdt_start(struct ti_wdt_softc *sc)
    145  1.1  jmcneill {
    146  1.1  jmcneill 	WR4(sc, WDT_WSPR, 0xbbbb);
    147  1.1  jmcneill 	ti_wdt_sync(sc, WWPS_W_PEND_WSPR);
    148  1.1  jmcneill 	WR4(sc, WDT_WSPR, 0x4444);
    149  1.1  jmcneill 	ti_wdt_sync(sc, WWPS_W_PEND_WSPR);
    150  1.1  jmcneill }
    151  1.1  jmcneill 
    152  1.1  jmcneill static int
    153  1.1  jmcneill ti_wdt_setmode(struct sysmon_wdog *smw)
    154  1.1  jmcneill {
    155  1.1  jmcneill 	struct ti_wdt_softc * const sc = smw->smw_cookie;
    156  1.1  jmcneill 	uint32_t counter_val;
    157  1.1  jmcneill 
    158  1.1  jmcneill 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    159  1.1  jmcneill 		ti_wdt_stop(sc);
    160  1.1  jmcneill 		return 0;
    161  1.1  jmcneill 	}
    162  1.1  jmcneill 
    163  1.1  jmcneill 	if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    164  1.1  jmcneill 		sc->sc_wdog.smw_period = WATCHDOG_PERIOD_DEFAULT;
    165  1.1  jmcneill 	else
    166  1.1  jmcneill 		sc->sc_wdog.smw_period = smw->smw_period;
    167  1.1  jmcneill 
    168  1.1  jmcneill 	if (sc->sc_wdog.smw_period == 0)
    169  1.1  jmcneill 		counter_val = ~0u;
    170  1.1  jmcneill 	else
    171  1.1  jmcneill 		counter_val = ~(sc->sc_wdog.smw_period * sc->sc_rate / 2);
    172  1.1  jmcneill 
    173  1.1  jmcneill 	ti_wdt_stop(sc);
    174  1.1  jmcneill 	ti_wdt_sync_all(sc);
    175  1.1  jmcneill 
    176  1.1  jmcneill 	WR4(sc, WDT_WCLR, WCLR_PRE | __SHIFTIN(1, WCLR_PTV));
    177  1.1  jmcneill 	WR4(sc, WDT_WLDR, counter_val);
    178  1.1  jmcneill 	WR4(sc, WDT_WCRR, counter_val);
    179  1.1  jmcneill 
    180  1.1  jmcneill 	ti_wdt_sync_all(sc);
    181  1.1  jmcneill 
    182  1.1  jmcneill 	ti_wdt_start(sc);
    183  1.1  jmcneill 
    184  1.1  jmcneill 	return 0;
    185  1.1  jmcneill }
    186  1.1  jmcneill 
    187  1.1  jmcneill static int
    188  1.1  jmcneill ti_wdt_tickle(struct sysmon_wdog *smw)
    189  1.1  jmcneill {
    190  1.1  jmcneill 	struct ti_wdt_softc * const sc = smw->smw_cookie;
    191  1.1  jmcneill 	uint32_t val;
    192  1.1  jmcneill 
    193  1.1  jmcneill 	ti_wdt_sync_all(sc);
    194  1.1  jmcneill 	val = RD4(sc, WDT_WTGR);
    195  1.1  jmcneill 	WR4(sc, WDT_WTGR, ~val);
    196  1.1  jmcneill 
    197  1.1  jmcneill 	return 0;
    198  1.1  jmcneill }
    199  1.1  jmcneill 
    200  1.1  jmcneill static int
    201  1.1  jmcneill ti_wdt_match(device_t parent, cfdata_t cf, void *aux)
    202  1.1  jmcneill {
    203  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    204  1.1  jmcneill 
    205  1.2   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    206  1.1  jmcneill }
    207  1.1  jmcneill 
    208  1.1  jmcneill static void
    209  1.1  jmcneill ti_wdt_attach(device_t parent, device_t self, void *aux)
    210  1.1  jmcneill {
    211  1.1  jmcneill 	struct ti_wdt_softc * const sc = device_private(self);
    212  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    213  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    214  1.1  jmcneill 	struct clk *clk;
    215  1.1  jmcneill 	bus_addr_t addr;
    216  1.1  jmcneill 	bus_size_t size;
    217  1.1  jmcneill 
    218  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    219  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    220  1.1  jmcneill 		return;
    221  1.1  jmcneill 	}
    222  1.1  jmcneill 
    223  1.1  jmcneill 	clk = ti_prcm_get_hwmod(phandle, 0);
    224  1.1  jmcneill 	if (clk == NULL || clk_enable(clk) != 0) {
    225  1.1  jmcneill 		aprint_error(": couldn't enable hwmod\n");
    226  1.1  jmcneill 		return;
    227  1.1  jmcneill 	}
    228  1.1  jmcneill 
    229  1.1  jmcneill 	sc->sc_dev = self;
    230  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    231  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    232  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    233  1.1  jmcneill 		return;
    234  1.1  jmcneill 	}
    235  1.1  jmcneill 	sc->sc_rate = clk_get_rate(clk);
    236  1.1  jmcneill 
    237  1.1  jmcneill 	aprint_naive("\n");
    238  1.1  jmcneill 	aprint_normal(": WATCHDOG\n");
    239  1.1  jmcneill 
    240  1.1  jmcneill 	/* Software reset */
    241  1.1  jmcneill 	if (ti_wdt_reset(sc) != 0) {
    242  1.1  jmcneill 		aprint_error_dev(self, "software reset timeout\n");
    243  1.1  jmcneill 		return;
    244  1.1  jmcneill 	}
    245  1.1  jmcneill 
    246  1.1  jmcneill 	/* Stop the watchdog */
    247  1.1  jmcneill 	ti_wdt_stop(sc);
    248  1.1  jmcneill 
    249  1.1  jmcneill 	/* Register watchdog */
    250  1.1  jmcneill 	sc->sc_wdog.smw_name = device_xname(self);
    251  1.1  jmcneill 	sc->sc_wdog.smw_setmode = ti_wdt_setmode;
    252  1.1  jmcneill 	sc->sc_wdog.smw_tickle = ti_wdt_tickle;
    253  1.1  jmcneill 	sc->sc_wdog.smw_period = WATCHDOG_PERIOD_DEFAULT;
    254  1.1  jmcneill 	sc->sc_wdog.smw_cookie = sc;
    255  1.1  jmcneill 	sysmon_wdog_register(&sc->sc_wdog);
    256  1.1  jmcneill }
    257  1.1  jmcneill 
    258  1.1  jmcneill CFATTACH_DECL_NEW(ti_wdt, sizeof(struct ti_wdt_softc),
    259  1.1  jmcneill     ti_wdt_match, ti_wdt_attach, NULL, NULL);
    260