xref: /freebsd/sys/arm/ti/ti_wdt.c (revision fdafd315)
12ffc65f9SRui Paulo /*-
22ffc65f9SRui Paulo  * Copyright (c) 2014 Rui Paulo <rpaulo@FreeBSD.org>
32ffc65f9SRui Paulo  * All rights reserved.
42ffc65f9SRui Paulo  *
52ffc65f9SRui Paulo  * Redistribution and use in source and binary forms, with or without
62ffc65f9SRui Paulo  * modification, are permitted provided that the following conditions
72ffc65f9SRui Paulo  * are met:
82ffc65f9SRui Paulo  * 1. Redistributions of source code must retain the above copyright
92ffc65f9SRui Paulo  *    notice, this list of conditions and the following disclaimer.
102ffc65f9SRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
112ffc65f9SRui Paulo  *    notice, this list of conditions and the following disclaimer in the
122ffc65f9SRui Paulo  *    documentation and/or other materials provided with the distribution.
132ffc65f9SRui Paulo  *
142ffc65f9SRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
152ffc65f9SRui Paulo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
162ffc65f9SRui Paulo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
172ffc65f9SRui Paulo  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
182ffc65f9SRui Paulo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
192ffc65f9SRui Paulo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
202ffc65f9SRui Paulo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212ffc65f9SRui Paulo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
222ffc65f9SRui Paulo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
232ffc65f9SRui Paulo  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
242ffc65f9SRui Paulo  * POSSIBILITY OF SUCH DAMAGE.
252ffc65f9SRui Paulo  */
26fdafd315SWarner Losh 
272ffc65f9SRui Paulo #include <sys/param.h>
282ffc65f9SRui Paulo #include <sys/systm.h>
292ffc65f9SRui Paulo #include <sys/bus.h>
302ffc65f9SRui Paulo #include <sys/conf.h>
31e2e050c8SConrad Meyer #include <sys/eventhandler.h>
322ffc65f9SRui Paulo #include <sys/kernel.h>
332ffc65f9SRui Paulo #include <sys/module.h>
342ffc65f9SRui Paulo #include <sys/malloc.h>
352ffc65f9SRui Paulo #include <sys/rman.h>
362ffc65f9SRui Paulo #include <sys/event.h>
372ffc65f9SRui Paulo #include <sys/selinfo.h>
382ffc65f9SRui Paulo #include <sys/watchdog.h>
392ffc65f9SRui Paulo #include <machine/bus.h>
402ffc65f9SRui Paulo #include <machine/cpu.h>
412ffc65f9SRui Paulo #include <machine/frame.h>
422ffc65f9SRui Paulo #include <machine/intr.h>
432ffc65f9SRui Paulo 
442ffc65f9SRui Paulo #include <dev/ofw/openfirm.h>
452ffc65f9SRui Paulo #include <dev/ofw/ofw_bus.h>
462ffc65f9SRui Paulo #include <dev/ofw/ofw_bus_subr.h>
472ffc65f9SRui Paulo 
482ffc65f9SRui Paulo #include <machine/bus.h>
492ffc65f9SRui Paulo 
502ffc65f9SRui Paulo #include <arm/ti/ti_wdt.h>
512ffc65f9SRui Paulo 
522ffc65f9SRui Paulo #ifdef DEBUG
532ffc65f9SRui Paulo #define	DPRINTF(fmt, ...)	do {	\
542ffc65f9SRui Paulo 	printf("%s: ", __func__);	\
552ffc65f9SRui Paulo 	printf(fmt, __VA_ARGS__);	\
562ffc65f9SRui Paulo } while (0)
572ffc65f9SRui Paulo #else
582ffc65f9SRui Paulo #define	DPRINTF(fmt, ...)
592ffc65f9SRui Paulo #endif
602ffc65f9SRui Paulo 
612ffc65f9SRui Paulo static device_probe_t		ti_wdt_probe;
622ffc65f9SRui Paulo static device_attach_t		ti_wdt_attach;
632ffc65f9SRui Paulo static device_detach_t		ti_wdt_detach;
642ffc65f9SRui Paulo static void			ti_wdt_intr(void *);
652ffc65f9SRui Paulo static void			ti_wdt_event(void *, unsigned int, int *);
662ffc65f9SRui Paulo 
672ffc65f9SRui Paulo struct ti_wdt_softc {
682ffc65f9SRui Paulo 	struct resource 	*sc_mem_res;
692ffc65f9SRui Paulo 	struct resource 	*sc_irq_res;
702ffc65f9SRui Paulo 	void            	*sc_intr;
712ffc65f9SRui Paulo 	bus_space_tag_t		sc_bt;
722ffc65f9SRui Paulo 	bus_space_handle_t	sc_bh;
732ffc65f9SRui Paulo 	eventhandler_tag	sc_ev_tag;
742ffc65f9SRui Paulo };
752ffc65f9SRui Paulo 
762ffc65f9SRui Paulo static device_method_t ti_wdt_methods[] = {
772ffc65f9SRui Paulo 	DEVMETHOD(device_probe,		ti_wdt_probe),
782ffc65f9SRui Paulo 	DEVMETHOD(device_attach,	ti_wdt_attach),
792ffc65f9SRui Paulo 	DEVMETHOD(device_detach,	ti_wdt_detach),
802ffc65f9SRui Paulo 
812ffc65f9SRui Paulo 	DEVMETHOD_END
822ffc65f9SRui Paulo };
832ffc65f9SRui Paulo 
842ffc65f9SRui Paulo static driver_t ti_wdt_driver = {
852ffc65f9SRui Paulo 	"ti_wdt",
862ffc65f9SRui Paulo 	ti_wdt_methods,
872ffc65f9SRui Paulo 	sizeof(struct ti_wdt_softc)
882ffc65f9SRui Paulo };
892ffc65f9SRui Paulo 
908537e671SJohn Baldwin DRIVER_MODULE(ti_wdt, simplebus, ti_wdt_driver, 0, 0);
910050ea24SMichal Meloun MODULE_DEPEND(ti_wdt, ti_sysc, 1, 1, 1);
922ffc65f9SRui Paulo 
939d87c6c8SIan Lepore static __inline uint32_t
ti_wdt_reg_read(struct ti_wdt_softc * sc,uint32_t reg)942ffc65f9SRui Paulo ti_wdt_reg_read(struct ti_wdt_softc *sc, uint32_t reg)
952ffc65f9SRui Paulo {
96c5422af9SRui Paulo 
972ffc65f9SRui Paulo 	return (bus_space_read_4(sc->sc_bt, sc->sc_bh, reg));
982ffc65f9SRui Paulo }
992ffc65f9SRui Paulo 
1002ffc65f9SRui Paulo static __inline void
ti_wdt_reg_write(struct ti_wdt_softc * sc,uint32_t reg,uint32_t val)1012ffc65f9SRui Paulo ti_wdt_reg_write(struct ti_wdt_softc *sc, uint32_t reg, uint32_t val)
1022ffc65f9SRui Paulo {
103c5422af9SRui Paulo 
1042ffc65f9SRui Paulo 	bus_space_write_4(sc->sc_bt, sc->sc_bh, reg, val);
1052ffc65f9SRui Paulo }
1062ffc65f9SRui Paulo 
1072ffc65f9SRui Paulo /*
1082ffc65f9SRui Paulo  * Wait for the write to a specific synchronised register to complete.
1092ffc65f9SRui Paulo  */
1102ffc65f9SRui Paulo static __inline void
ti_wdt_reg_wait(struct ti_wdt_softc * sc,uint32_t bit)1112ffc65f9SRui Paulo ti_wdt_reg_wait(struct ti_wdt_softc *sc, uint32_t bit)
1122ffc65f9SRui Paulo {
113c5422af9SRui Paulo 
1142ffc65f9SRui Paulo 	while (ti_wdt_reg_read(sc, TI_WDT_WWPS) & bit)
1152ffc65f9SRui Paulo 		DELAY(10);
1162ffc65f9SRui Paulo }
1172ffc65f9SRui Paulo 
1182ffc65f9SRui Paulo static __inline void
ti_wdt_disable(struct ti_wdt_softc * sc)1192ffc65f9SRui Paulo ti_wdt_disable(struct ti_wdt_softc *sc)
1202ffc65f9SRui Paulo {
121c5422af9SRui Paulo 
1222ffc65f9SRui Paulo 	DPRINTF("disabling watchdog %p\n", sc);
1232ffc65f9SRui Paulo 	ti_wdt_reg_write(sc, TI_WDT_WSPR, 0xAAAA);
1242ffc65f9SRui Paulo 	ti_wdt_reg_wait(sc, TI_W_PEND_WSPR);
1252ffc65f9SRui Paulo 	ti_wdt_reg_write(sc, TI_WDT_WSPR, 0x5555);
1262ffc65f9SRui Paulo 	ti_wdt_reg_wait(sc, TI_W_PEND_WSPR);
1272ffc65f9SRui Paulo }
1282ffc65f9SRui Paulo 
1292ffc65f9SRui Paulo static __inline void
ti_wdt_enable(struct ti_wdt_softc * sc)1302ffc65f9SRui Paulo ti_wdt_enable(struct ti_wdt_softc *sc)
1312ffc65f9SRui Paulo {
132c5422af9SRui Paulo 
1332ffc65f9SRui Paulo 	DPRINTF("enabling watchdog %p\n", sc);
1342ffc65f9SRui Paulo 	ti_wdt_reg_write(sc, TI_WDT_WSPR, 0xBBBB);
1352ffc65f9SRui Paulo 	ti_wdt_reg_wait(sc, TI_W_PEND_WSPR);
1362ffc65f9SRui Paulo 	ti_wdt_reg_write(sc, TI_WDT_WSPR, 0x4444);
1372ffc65f9SRui Paulo 	ti_wdt_reg_wait(sc, TI_W_PEND_WSPR);
1382ffc65f9SRui Paulo }
1392ffc65f9SRui Paulo 
1402ffc65f9SRui Paulo static int
ti_wdt_probe(device_t dev)1412ffc65f9SRui Paulo ti_wdt_probe(device_t dev)
1422ffc65f9SRui Paulo {
1432ffc65f9SRui Paulo 
1442ffc65f9SRui Paulo 	if (!ofw_bus_status_okay(dev))
1452ffc65f9SRui Paulo 		return (ENXIO);
1462ffc65f9SRui Paulo 	if (ofw_bus_is_compatible(dev, "ti,omap3-wdt")) {
1472ffc65f9SRui Paulo 		device_set_desc(dev, "TI Watchdog Timer");
1482ffc65f9SRui Paulo 		return (BUS_PROBE_DEFAULT);
1492ffc65f9SRui Paulo 	}
1502ffc65f9SRui Paulo 
1512ffc65f9SRui Paulo 	return (ENXIO);
1522ffc65f9SRui Paulo }
1532ffc65f9SRui Paulo 
1542ffc65f9SRui Paulo static int
ti_wdt_attach(device_t dev)1552ffc65f9SRui Paulo ti_wdt_attach(device_t dev)
1562ffc65f9SRui Paulo {
1572ffc65f9SRui Paulo 	struct ti_wdt_softc *sc;
1582ffc65f9SRui Paulo 	int rid;
1592ffc65f9SRui Paulo 
1602ffc65f9SRui Paulo 	sc = device_get_softc(dev);
1612ffc65f9SRui Paulo 	rid = 0;
1622ffc65f9SRui Paulo 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1632ffc65f9SRui Paulo 	    RF_ACTIVE);
1642ffc65f9SRui Paulo 	if (sc->sc_mem_res == NULL) {
1652ffc65f9SRui Paulo 		device_printf(dev, "could not allocate memory resource\n");
1662ffc65f9SRui Paulo 		return (ENXIO);
1672ffc65f9SRui Paulo 	}
1682ffc65f9SRui Paulo 	sc->sc_bt = rman_get_bustag(sc->sc_mem_res);
1692ffc65f9SRui Paulo 	sc->sc_bh = rman_get_bushandle(sc->sc_mem_res);
1702ffc65f9SRui Paulo 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1712ffc65f9SRui Paulo 	if (sc->sc_irq_res == NULL) {
1722ffc65f9SRui Paulo 		device_printf(dev, "could not allocate interrupt resource\n");
1732ffc65f9SRui Paulo 		ti_wdt_detach(dev);
1742ffc65f9SRui Paulo 		return (ENXIO);
1752ffc65f9SRui Paulo 	}
1762ffc65f9SRui Paulo 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE | INTR_TYPE_MISC,
1772ffc65f9SRui Paulo 		NULL, ti_wdt_intr, sc,  &sc->sc_intr) != 0) {
1782ffc65f9SRui Paulo 		device_printf(dev,
1792ffc65f9SRui Paulo 		    "unable to setup the interrupt handler\n");
1802ffc65f9SRui Paulo 		ti_wdt_detach(dev);
1812ffc65f9SRui Paulo 		return (ENXIO);
1822ffc65f9SRui Paulo 	}
1832ffc65f9SRui Paulo 	/* Reset, enable interrupts and stop the watchdog. */
1842ffc65f9SRui Paulo 	ti_wdt_reg_write(sc, TI_WDT_WDSC,
1852ffc65f9SRui Paulo 	    ti_wdt_reg_read(sc, TI_WDT_WDSC) | TI_WDSC_SR);
1862ffc65f9SRui Paulo 	while (ti_wdt_reg_read(sc, TI_WDT_WDSC) & TI_WDSC_SR)
1872ffc65f9SRui Paulo 		DELAY(10);
1882ffc65f9SRui Paulo 	ti_wdt_reg_write(sc, TI_WDT_WIRQENSET, TI_IRQ_EN_OVF | TI_IRQ_EN_DLY);
1892ffc65f9SRui Paulo 	ti_wdt_disable(sc);
1902ffc65f9SRui Paulo 	if (bootverbose)
1912ffc65f9SRui Paulo 		device_printf(dev, "revision: 0x%x\n",
1922ffc65f9SRui Paulo 		    ti_wdt_reg_read(sc, TI_WDT_WIDR));
1932ffc65f9SRui Paulo 	sc->sc_ev_tag = EVENTHANDLER_REGISTER(watchdog_list, ti_wdt_event, sc,
1942ffc65f9SRui Paulo 	    0);
1952ffc65f9SRui Paulo 
1962ffc65f9SRui Paulo 	return (0);
1972ffc65f9SRui Paulo }
1982ffc65f9SRui Paulo 
1992ffc65f9SRui Paulo static int
ti_wdt_detach(device_t dev)2002ffc65f9SRui Paulo ti_wdt_detach(device_t dev)
2012ffc65f9SRui Paulo {
2022ffc65f9SRui Paulo 	struct ti_wdt_softc *sc;
2032ffc65f9SRui Paulo 
2042ffc65f9SRui Paulo 	sc = device_get_softc(dev);
2052ffc65f9SRui Paulo 	if (sc->sc_ev_tag)
2062ffc65f9SRui Paulo 		EVENTHANDLER_DEREGISTER(watchdog_list, sc->sc_ev_tag);
2072ffc65f9SRui Paulo 	if (sc->sc_intr)
2082ffc65f9SRui Paulo 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr);
2092ffc65f9SRui Paulo 	if (sc->sc_irq_res)
2102ffc65f9SRui Paulo 		bus_release_resource(dev, SYS_RES_IRQ,
2112ffc65f9SRui Paulo 		    rman_get_rid(sc->sc_irq_res), sc->sc_irq_res);
2122ffc65f9SRui Paulo 	if (sc->sc_mem_res)
2132ffc65f9SRui Paulo 		bus_release_resource(dev, SYS_RES_MEMORY,
2142ffc65f9SRui Paulo 		    rman_get_rid(sc->sc_mem_res),  sc->sc_mem_res);
2152ffc65f9SRui Paulo 
2162ffc65f9SRui Paulo 	return (0);
2172ffc65f9SRui Paulo }
2182ffc65f9SRui Paulo 
2192ffc65f9SRui Paulo static void
ti_wdt_intr(void * arg)2202ffc65f9SRui Paulo ti_wdt_intr(void *arg)
2212ffc65f9SRui Paulo {
2222ffc65f9SRui Paulo 	struct ti_wdt_softc *sc;
2232ffc65f9SRui Paulo 
2242ffc65f9SRui Paulo 	sc = arg;
2252ffc65f9SRui Paulo 	DPRINTF("interrupt %p", sc);
2262ffc65f9SRui Paulo 	ti_wdt_reg_write(sc, TI_WDT_WIRQSTAT, TI_IRQ_EV_OVF | TI_IRQ_EV_DLY);
2272ffc65f9SRui Paulo 	/* TODO: handle interrupt */
2282ffc65f9SRui Paulo }
2292ffc65f9SRui Paulo 
2302ffc65f9SRui Paulo static void
ti_wdt_event(void * arg,unsigned int cmd,int * error)2312ffc65f9SRui Paulo ti_wdt_event(void *arg, unsigned int cmd, int *error)
2322ffc65f9SRui Paulo {
2332ffc65f9SRui Paulo 	struct ti_wdt_softc *sc;
2342ffc65f9SRui Paulo 	uint8_t s;
2352ffc65f9SRui Paulo 	uint32_t wldr;
2362ffc65f9SRui Paulo 	uint32_t ptv;
2372ffc65f9SRui Paulo 
2382ffc65f9SRui Paulo 	sc = arg;
2392ffc65f9SRui Paulo 	ti_wdt_disable(sc);
2402ffc65f9SRui Paulo 	if (cmd == WD_TO_NEVER) {
2412ffc65f9SRui Paulo 		*error = 0;
2422ffc65f9SRui Paulo 		return;
2432ffc65f9SRui Paulo 	}
2442ffc65f9SRui Paulo 	DPRINTF("cmd 0x%x\n", cmd);
2452ffc65f9SRui Paulo 	cmd &= WD_INTERVAL;
2462ffc65f9SRui Paulo 	if (cmd < WD_TO_1SEC) {
2472ffc65f9SRui Paulo 		*error = EINVAL;
2482ffc65f9SRui Paulo 		return;
2492ffc65f9SRui Paulo 	}
2502ffc65f9SRui Paulo 	s = 1 << (cmd - WD_TO_1SEC);
2512ffc65f9SRui Paulo 	DPRINTF("seconds %u\n", s);
2522ffc65f9SRui Paulo 	/*
2532ffc65f9SRui Paulo 	 * Leave the pre-scaler with its default values:
2542ffc65f9SRui Paulo 	 * PTV = 0 == 2**0 == 1
2552ffc65f9SRui Paulo 	 * PRE = 1 (enabled)
2562ffc65f9SRui Paulo 	 *
2572ffc65f9SRui Paulo 	 * Compute the load register value assuming a 32kHz clock.
2582ffc65f9SRui Paulo 	 * See OVF_Rate in the WDT section of the AM335x TRM.
2592ffc65f9SRui Paulo 	 */
2602ffc65f9SRui Paulo 	ptv = 0;
2612ffc65f9SRui Paulo 	wldr = 0xffffffff - (s * (32768 / (1 << ptv))) + 1;
2622ffc65f9SRui Paulo 	DPRINTF("wldr 0x%x\n", wldr);
2632ffc65f9SRui Paulo 	ti_wdt_reg_write(sc, TI_WDT_WLDR, wldr);
2642ffc65f9SRui Paulo 	/*
2652ffc65f9SRui Paulo 	 * Trigger a timer reload.
2662ffc65f9SRui Paulo 	 */
2672ffc65f9SRui Paulo 	ti_wdt_reg_write(sc, TI_WDT_WTGR,
2682ffc65f9SRui Paulo 	    ti_wdt_reg_read(sc, TI_WDT_WTGR) + 1);
2692ffc65f9SRui Paulo 	ti_wdt_reg_wait(sc, TI_W_PEND_WTGR);
2702ffc65f9SRui Paulo 	ti_wdt_enable(sc);
2712ffc65f9SRui Paulo 	*error = 0;
2722ffc65f9SRui Paulo }
273