Home | History | Annotate | Line # | Download | only in amlogic
      1  1.3  jmcneill /* $NetBSD: mesongx_wdt.c,v 1.3 2022/09/28 10:23:37 jmcneill 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.3  jmcneill __KERNEL_RCSID(0, "$NetBSD: mesongx_wdt.c,v 1.3 2022/09/28 10:23:37 jmcneill 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 #define	CBUS_REG(x)		((x) << 2)
     43  1.1  jmcneill 
     44  1.1  jmcneill #define	WATCHDOG_CNTL		CBUS_REG(0)
     45  1.1  jmcneill #define	 CNTL_CLK_DIV_EN		__BIT(25)
     46  1.1  jmcneill #define	 CNTL_CLK_EN			__BIT(24)
     47  1.1  jmcneill #define	 CNTL_SYS_RESET_N_EN		__BIT(21)
     48  1.1  jmcneill #define	 CNTL_WATCHDOG_EN		__BIT(18)
     49  1.1  jmcneill #define	 CNTL_CLK_DIV_TCNT		__BITS(17,0)
     50  1.1  jmcneill #define	WATCHDOG_CNTL1		CBUS_REG(1)
     51  1.1  jmcneill #define	WATCHDOG_TCNT		CBUS_REG(2)
     52  1.1  jmcneill #define	WATCHDOG_RESET		CBUS_REG(3)
     53  1.1  jmcneill 
     54  1.1  jmcneill #define	WATCHDOG_PERIOD_DEFAULT		8
     55  1.1  jmcneill #define	WATCHDOG_PERIOD_MAX		8
     56  1.1  jmcneill 
     57  1.2   thorpej static const struct device_compatible_entry compat_data[] = {
     58  1.2   thorpej 	{ .compat = "amlogic,meson-gx-wdt" },
     59  1.3  jmcneill 	{ .compat = "amlogic,meson-gxbb-wdt" },
     60  1.2   thorpej 	DEVICE_COMPAT_EOL
     61  1.1  jmcneill };
     62  1.1  jmcneill 
     63  1.1  jmcneill struct mesongx_wdt_softc {
     64  1.1  jmcneill 	device_t		sc_dev;
     65  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     66  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     67  1.1  jmcneill 
     68  1.1  jmcneill 	struct sysmon_wdog	sc_wdog;
     69  1.1  jmcneill 	u_int			sc_rate;
     70  1.1  jmcneill };
     71  1.1  jmcneill 
     72  1.1  jmcneill #define	WDT_READ(sc, reg)		\
     73  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     74  1.1  jmcneill #define	WDT_WRITE(sc, reg, val)	\
     75  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     76  1.1  jmcneill 
     77  1.1  jmcneill static int
     78  1.1  jmcneill mesongx_wdt_setmode(struct sysmon_wdog *smw)
     79  1.1  jmcneill {
     80  1.1  jmcneill 	struct mesongx_wdt_softc * const sc = smw->smw_cookie;
     81  1.1  jmcneill 
     82  1.1  jmcneill 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
     83  1.1  jmcneill 		WDT_WRITE(sc, WATCHDOG_CNTL, 0);
     84  1.1  jmcneill 		return 0;
     85  1.1  jmcneill 	}
     86  1.1  jmcneill 
     87  1.1  jmcneill 	if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
     88  1.1  jmcneill 		sc->sc_wdog.smw_period = WATCHDOG_PERIOD_DEFAULT;
     89  1.1  jmcneill 	} else if (smw->smw_period == 0 ||
     90  1.1  jmcneill 		   smw->smw_period > WATCHDOG_PERIOD_MAX) {
     91  1.1  jmcneill 		return EINVAL;
     92  1.1  jmcneill 	} else {
     93  1.1  jmcneill 		sc->sc_wdog.smw_period = smw->smw_period;
     94  1.1  jmcneill 	}
     95  1.1  jmcneill 
     96  1.1  jmcneill 	const u_int tcnt = sc->sc_rate / 1000;
     97  1.1  jmcneill 
     98  1.1  jmcneill 	WDT_WRITE(sc, WATCHDOG_CNTL, 0);
     99  1.1  jmcneill 	WDT_WRITE(sc, WATCHDOG_RESET, 0);
    100  1.1  jmcneill 	WDT_WRITE(sc, WATCHDOG_TCNT, sc->sc_wdog.smw_period * 1000);
    101  1.1  jmcneill 	WDT_WRITE(sc, WATCHDOG_CNTL,
    102  1.1  jmcneill 	    __SHIFTIN(tcnt, CNTL_CLK_DIV_TCNT) |
    103  1.1  jmcneill 	    CNTL_CLK_DIV_EN | CNTL_CLK_EN |
    104  1.1  jmcneill 	    CNTL_SYS_RESET_N_EN | CNTL_WATCHDOG_EN);
    105  1.1  jmcneill 
    106  1.1  jmcneill 	return 0;
    107  1.1  jmcneill }
    108  1.1  jmcneill 
    109  1.1  jmcneill static int
    110  1.1  jmcneill mesongx_wdt_tickle(struct sysmon_wdog *smw)
    111  1.1  jmcneill {
    112  1.1  jmcneill 	struct mesongx_wdt_softc * const sc = smw->smw_cookie;
    113  1.1  jmcneill 
    114  1.1  jmcneill 	WDT_WRITE(sc, WATCHDOG_RESET, 0);
    115  1.1  jmcneill 
    116  1.1  jmcneill 	return 0;
    117  1.1  jmcneill }
    118  1.1  jmcneill 
    119  1.1  jmcneill static int
    120  1.1  jmcneill mesongx_wdt_match(device_t parent, cfdata_t cf, void *aux)
    121  1.1  jmcneill {
    122  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    123  1.1  jmcneill 
    124  1.2   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    125  1.1  jmcneill }
    126  1.1  jmcneill 
    127  1.1  jmcneill static void
    128  1.1  jmcneill mesongx_wdt_attach(device_t parent, device_t self, void *aux)
    129  1.1  jmcneill {
    130  1.1  jmcneill 	struct mesongx_wdt_softc * const sc = device_private(self);
    131  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    132  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    133  1.1  jmcneill 	struct clk *clk;
    134  1.1  jmcneill 	bus_addr_t addr;
    135  1.1  jmcneill 	bus_size_t size;
    136  1.1  jmcneill 
    137  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    138  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    139  1.1  jmcneill 		return;
    140  1.1  jmcneill 	}
    141  1.1  jmcneill 
    142  1.1  jmcneill 	sc->sc_dev = self;
    143  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    144  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    145  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    146  1.1  jmcneill 		return;
    147  1.1  jmcneill 	}
    148  1.1  jmcneill 
    149  1.1  jmcneill 	aprint_naive("\n");
    150  1.1  jmcneill 	aprint_normal(": EE-watchdog\n");
    151  1.1  jmcneill 
    152  1.1  jmcneill 	clk = fdtbus_clock_get_index(phandle, 0);
    153  1.1  jmcneill 	if (clk != NULL)
    154  1.1  jmcneill 		sc->sc_rate = clk_get_rate(clk);
    155  1.1  jmcneill 	else {
    156  1.1  jmcneill 		aprint_error_dev(self, "WARNING: couldn't get xtal clock, assuming 24 MHz\n");
    157  1.1  jmcneill 		sc->sc_rate = 24000000;
    158  1.1  jmcneill 	}
    159  1.1  jmcneill 
    160  1.1  jmcneill 	/* Disable watchdog */
    161  1.1  jmcneill 	WDT_WRITE(sc, WATCHDOG_CNTL, 0);
    162  1.1  jmcneill 
    163  1.1  jmcneill 	/* Register watchdog */
    164  1.1  jmcneill 	sc->sc_wdog.smw_name = "EE-watchdog";
    165  1.1  jmcneill 	sc->sc_wdog.smw_setmode = mesongx_wdt_setmode;
    166  1.1  jmcneill 	sc->sc_wdog.smw_tickle = mesongx_wdt_tickle;
    167  1.1  jmcneill 	sc->sc_wdog.smw_period = WATCHDOG_PERIOD_DEFAULT;
    168  1.1  jmcneill 	sc->sc_wdog.smw_cookie = sc;
    169  1.1  jmcneill 	sysmon_wdog_register(&sc->sc_wdog);
    170  1.1  jmcneill }
    171  1.1  jmcneill 
    172  1.1  jmcneill CFATTACH_DECL_NEW(mesongx_wdt, sizeof(struct mesongx_wdt_softc),
    173  1.1  jmcneill     mesongx_wdt_match, mesongx_wdt_attach, NULL, NULL);
    174