Home | History | Annotate | Line # | Download | only in nvidia
tegra_timer.c revision 1.3
      1 /* $NetBSD: tegra_timer.c,v 1.3 2015/12/22 22:10:36 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: tegra_timer.c,v 1.3 2015/12/22 22:10:36 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/intr.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/wdog.h>
     39 
     40 #include <dev/sysmon/sysmonvar.h>
     41 
     42 #include <arm/nvidia/tegra_reg.h>
     43 #include <arm/nvidia/tegra_timerreg.h>
     44 #include <arm/nvidia/tegra_var.h>
     45 
     46 #include <dev/fdt/fdtvar.h>
     47 
     48 #define TEGRA_TIMER_WDOG_PERIOD_DEFAULT	10
     49 
     50 static int	tegra_timer_match(device_t, cfdata_t, void *);
     51 static void	tegra_timer_attach(device_t, device_t, void *);
     52 
     53 struct tegra_timer_softc {
     54 	device_t		sc_dev;
     55 	bus_space_tag_t		sc_bst;
     56 	bus_space_handle_t	sc_bsh;
     57 	struct clk		*sc_clk_watchdog;
     58 
     59 	struct sysmon_wdog	sc_smw;
     60 };
     61 
     62 static int	tegra_timer_wdt_setmode(struct sysmon_wdog *);
     63 static int	tegra_timer_wdt_tickle(struct sysmon_wdog *);
     64 
     65 CFATTACH_DECL_NEW(tegra_timer, sizeof(struct tegra_timer_softc),
     66 	tegra_timer_match, tegra_timer_attach, NULL, NULL);
     67 
     68 #define TIMER_READ(sc, reg)			\
     69     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     70 #define TIMER_WRITE(sc, reg, val)		\
     71     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     72 #define TIMER_SET_CLEAR(sc, reg, set, clr)	\
     73     tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (set), (clr))
     74 
     75 static int
     76 tegra_timer_match(device_t parent, cfdata_t cf, void *aux)
     77 {
     78 	const char * const compatible[] = { "nvidia,tegra124-timer", NULL };
     79 	struct fdt_attach_args * const faa = aux;
     80 
     81 	return of_match_compatible(faa->faa_phandle, compatible);
     82 }
     83 
     84 static void
     85 tegra_timer_attach(device_t parent, device_t self, void *aux)
     86 {
     87 	struct tegra_timer_softc * const sc = device_private(self);
     88 	struct fdt_attach_args * const faa = aux;
     89 	bus_addr_t addr;
     90 	bus_size_t size;
     91 	int error;
     92 
     93 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
     94 		aprint_error(": couldn't get registers\n");
     95 		return;
     96 	}
     97 	sc->sc_clk_watchdog = fdtbus_clock_get(faa->faa_phandle, "watchdog");
     98 
     99 	sc->sc_dev = self;
    100 	sc->sc_bst = faa->faa_bst;
    101 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    102 	if (error) {
    103 		aprint_error(": couldn't map %#llx: %d", (uint64_t)addr, error);
    104 		return;
    105 	}
    106 
    107 	aprint_naive("\n");
    108 	aprint_normal(": Timers\n");
    109 
    110 	if (sc->sc_clk_watchdog) {
    111 		sc->sc_smw.smw_name = device_xname(self);
    112 		sc->sc_smw.smw_cookie = sc;
    113 		sc->sc_smw.smw_setmode = tegra_timer_wdt_setmode;
    114 		sc->sc_smw.smw_tickle = tegra_timer_wdt_tickle;
    115 		sc->sc_smw.smw_period = TEGRA_TIMER_WDOG_PERIOD_DEFAULT;
    116 
    117 		aprint_normal_dev(self,
    118 		    "default watchdog period is %u seconds\n",
    119 		    sc->sc_smw.smw_period);
    120 
    121 		if (sysmon_wdog_register(&sc->sc_smw) != 0) {
    122 			aprint_error_dev(self,
    123 			    "couldn't register with sysmon\n");
    124 		}
    125 	}
    126 }
    127 
    128 static int
    129 tegra_timer_wdt_setmode(struct sysmon_wdog *smw)
    130 {
    131 	struct tegra_timer_softc * const sc = smw->smw_cookie;
    132 
    133 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    134 		TIMER_SET_CLEAR(sc, TMR1_PTV_REG, 0, TMR_PTV_EN);
    135 		return clk_disable(sc->sc_clk_watchdog);
    136 	} else {
    137 		if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
    138 			sc->sc_smw.smw_period = TEGRA_TIMER_WDOG_PERIOD_DEFAULT;
    139 		} else if (smw->smw_period == 0 || smw->smw_period > 1000) {
    140 			return EINVAL;
    141 		} else {
    142 			sc->sc_smw.smw_period = smw->smw_period;
    143 		}
    144 		u_int tval = (sc->sc_smw.smw_period * 1000000) / 2;
    145 		TIMER_WRITE(sc, TMR1_PTV_REG,
    146 		    TMR_PTV_EN | TMR_PTV_PER | __SHIFTIN(tval, TMR_PTV_VAL));
    147 		TIMER_WRITE(sc, TMR1_PCR_REG, TMR_PCR_INTR_CLR);
    148 		return clk_enable(sc->sc_clk_watchdog);
    149 	}
    150 }
    151 
    152 static int
    153 tegra_timer_wdt_tickle(struct sysmon_wdog *smw)
    154 {
    155 	struct tegra_timer_softc * const sc = smw->smw_cookie;
    156 
    157 	TIMER_WRITE(sc, TMR1_PCR_REG, TMR_PCR_INTR_CLR);
    158 
    159 	return 0;
    160 }
    161 
    162 void
    163 delay(u_int us)
    164 {
    165 	static bool timerus_configured = false;
    166 	bus_space_tag_t bst = &armv7_generic_bs_tag;
    167 	bus_space_handle_t bsh;
    168 
    169 	bus_space_subregion(bst, tegra_ppsb_bsh, TEGRA_TIMER_OFFSET,
    170 	    TEGRA_TIMER_SIZE, &bsh);
    171 
    172 	if (__predict_false(timerus_configured == false)) {
    173 		/* clk_m frequency 12 MHz */
    174 		bus_space_write_4(bst, bsh, TMRUS_USEC_CFG_REG, 0xb);
    175 		timerus_configured = true;
    176 	}
    177 
    178 	u_int nus = 0;
    179 	u_int us_prev = bus_space_read_4(bst, bsh, TMRUS_CNTR_1US_REG);
    180 
    181 	while (nus < us) {
    182 		const u_int us_cur = bus_space_read_4(bst, bsh,
    183 		    TMRUS_CNTR_1US_REG);
    184 		if (us_cur < us_prev) {
    185 			nus += (0xffffffff - us_prev) + us_cur;
    186 		} else {
    187 			nus += (us_cur - us_prev);
    188 		}
    189 		us_prev = us_cur;
    190 	}
    191 }
    192