Home | History | Annotate | Line # | Download | only in sunxi
sunxi_timer.c revision 1.3.4.1
      1  1.3.4.1  christos /* $NetBSD: sunxi_timer.c,v 1.3.4.1 2019/06/10 22:05:57 christos Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2017 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.4.1  christos __KERNEL_RCSID(0, "$NetBSD: sunxi_timer.c,v 1.3.4.1 2019/06/10 22:05:57 christos Exp $");
     31      1.1  jmcneill 
     32      1.1  jmcneill #include <sys/param.h>
     33      1.1  jmcneill #include <sys/kernel.h>
     34      1.1  jmcneill #include <sys/bus.h>
     35      1.1  jmcneill #include <sys/device.h>
     36      1.1  jmcneill #include <sys/intr.h>
     37      1.1  jmcneill #include <sys/systm.h>
     38      1.1  jmcneill #include <sys/timetc.h>
     39      1.1  jmcneill 
     40      1.1  jmcneill #include <arm/locore.h>
     41      1.1  jmcneill 
     42      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     43      1.1  jmcneill 
     44      1.1  jmcneill #include <arm/fdt/arm_fdtvar.h>
     45      1.1  jmcneill 
     46      1.3  jmcneill /* Timer 0 registers */
     47      1.1  jmcneill #define	TMR_IRQ_EN_REG		0x00
     48      1.1  jmcneill #define	 TMR_IRQ_EN(n)		__BIT(n)
     49      1.1  jmcneill #define	TMR_IRQ_STAS_REG	0x04
     50      1.1  jmcneill #define	 TMR_IRQ_STAS_PEND(n)	__BIT(n)
     51      1.1  jmcneill #define	TMR0_CTRL_REG		0x10
     52      1.1  jmcneill #define	 TMR0_CTRL_MODE		__BIT(7)
     53      1.1  jmcneill #define	 TMR0_CTRL_CLK_PRESCALE	__BITS(6,4)
     54      1.1  jmcneill #define	 TMR0_CTRL_CLK_SRC	__BITS(3,2)
     55      1.1  jmcneill #define	  TMR0_CTRL_CLK_SRC_OSC24M	1
     56      1.1  jmcneill #define	  TMR0_CTRL_CLK_SRC_PLL6_6	2
     57      1.1  jmcneill #define	 TMR0_CTRL_RELOAD	__BIT(1)
     58      1.1  jmcneill #define	 TMR0_CTRL_EN		__BIT(0)
     59      1.1  jmcneill #define	TMR0_INTV_VALUE_REG	0x14
     60      1.1  jmcneill #define	TMR0_CURNT_VALUE_REG	0x18
     61      1.3  jmcneill 
     62      1.3  jmcneill /* Timer 1 is used for delay() */
     63      1.3  jmcneill 
     64      1.3  jmcneill /* Timer 2 registers */
     65      1.3  jmcneill #define	TMR2_CTRL_REG		0x30
     66      1.3  jmcneill #define	 TMR2_CTRL_MODE		__BIT(7)
     67      1.3  jmcneill #define	 TMR2_CTRL_CLK_SRC	__BITS(3,2)
     68      1.3  jmcneill #define	  TMR2_CTRL_CLK_SRC_OSC24M	1
     69      1.3  jmcneill #define	 TMR2_CTRL_RELOAD	__BIT(1)
     70      1.3  jmcneill #define	 TMR2_CTRL_EN		__BIT(0)
     71      1.3  jmcneill #define	TMR2_INTV_VALUE_REG	0x34
     72      1.3  jmcneill #define	TMR2_CURNT_VALUE_REG	0x38
     73      1.1  jmcneill 
     74  1.3.4.1  christos /* Timer 4 registers */
     75  1.3.4.1  christos #define	TMR4_CTRL_REG		0x50
     76  1.3.4.1  christos #define	 TMR4_CTRL_RELOAD	__BIT(1)
     77  1.3.4.1  christos #define	 TMR4_CTRL_EN		__BIT(0)
     78  1.3.4.1  christos #define	TMR4_INTV_VALUE_REG	0x54
     79  1.3.4.1  christos #define	TMR4_CURNT_VALUE_REG	0x58
     80  1.3.4.1  christos 
     81  1.3.4.1  christos /* Control registers */
     82  1.3.4.1  christos #define	AVS_CNT_CTL_REG		0x80
     83  1.3.4.1  christos #define	AVS_CNT0_REG		0x84
     84  1.3.4.1  christos #define	AVS_CNT1_REG		0x88
     85  1.3.4.1  christos #define	AVS_CNT_DIV_REG		0x8c
     86  1.3.4.1  christos #define	WDOG_CTRL_REG		0x90
     87  1.3.4.1  christos #define	WDOG_MODE_REG		0x94
     88  1.3.4.1  christos #define	LOSC_CTRL_REG		0x100
     89  1.3.4.1  christos #define	 LOSC_CTRL_KEY_FIELD	__BITS(31,16)
     90  1.3.4.1  christos #define	 LOSC_CTRL_KEY_FIELD_V	0x16aa
     91  1.3.4.1  christos #define  LOSC_CTRL_OSC32K_AUTO_SWT_EN	__BIT(14)
     92  1.3.4.1  christos #define	 LOSC_CTRL_OSC32K_SEL	__BIT(0)
     93  1.3.4.1  christos 
     94      1.1  jmcneill static const char * const compatible[] = {
     95      1.1  jmcneill 	"allwinner,sun4i-a10-timer",
     96      1.1  jmcneill 	NULL
     97      1.1  jmcneill };
     98      1.1  jmcneill 
     99      1.1  jmcneill struct sunxi_timer_softc {
    100      1.1  jmcneill 	device_t sc_dev;
    101      1.1  jmcneill 	bus_space_tag_t sc_bst;
    102      1.1  jmcneill 	bus_space_handle_t sc_bsh;
    103      1.1  jmcneill 	int sc_phandle;
    104      1.1  jmcneill 	struct clk *sc_clk;
    105      1.1  jmcneill 
    106      1.1  jmcneill 	struct timecounter sc_tc;
    107  1.3.4.1  christos 	struct timecounter sc_tc_losc;
    108      1.1  jmcneill };
    109      1.1  jmcneill 
    110      1.1  jmcneill #define TIMER_READ(sc, reg) \
    111      1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    112      1.1  jmcneill #define TIMER_WRITE(sc, reg, val) \
    113      1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    114      1.1  jmcneill 
    115      1.1  jmcneill static struct sunxi_timer_softc *timer_softc;
    116      1.1  jmcneill 
    117      1.1  jmcneill static int
    118      1.1  jmcneill sunxi_timer_intr(void *arg)
    119      1.1  jmcneill {
    120      1.1  jmcneill 	struct sunxi_timer_softc * const sc = timer_softc;
    121      1.1  jmcneill 	struct clockframe *frame = arg;
    122      1.1  jmcneill 	uint32_t stas;
    123      1.1  jmcneill 
    124      1.1  jmcneill 	stas = TIMER_READ(sc, TMR_IRQ_STAS_REG);
    125      1.1  jmcneill 	if (stas == 0)
    126      1.1  jmcneill 		return 0;
    127      1.1  jmcneill 	TIMER_WRITE(sc, TMR_IRQ_STAS_REG, stas);
    128      1.1  jmcneill 
    129      1.1  jmcneill 	if ((stas & TMR_IRQ_STAS_PEND(0)) != 0)
    130      1.1  jmcneill 		hardclock(frame);
    131      1.1  jmcneill 
    132      1.1  jmcneill 	return 1;
    133      1.1  jmcneill }
    134      1.1  jmcneill 
    135      1.1  jmcneill static void
    136      1.1  jmcneill sunxi_timer_cpu_initclocks(void)
    137      1.1  jmcneill {
    138      1.1  jmcneill 	struct sunxi_timer_softc * const sc = timer_softc;
    139      1.1  jmcneill 	char intrstr[128];
    140      1.1  jmcneill 	void *ih;
    141      1.1  jmcneill 
    142      1.1  jmcneill 	KASSERT(sc != NULL);
    143      1.1  jmcneill 
    144      1.1  jmcneill 	if (!fdtbus_intr_str(sc->sc_phandle, 0, intrstr, sizeof(intrstr)))
    145      1.1  jmcneill 		panic("%s: failed to decode interrupt", __func__);
    146      1.1  jmcneill 
    147      1.1  jmcneill 	ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_CLOCK,
    148      1.1  jmcneill 	    FDT_INTR_MPSAFE, sunxi_timer_intr, NULL);
    149      1.1  jmcneill 	if (ih == NULL)
    150      1.1  jmcneill 		panic("%s: failed to establish timer interrupt", __func__);
    151      1.1  jmcneill 
    152      1.1  jmcneill 	aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
    153      1.1  jmcneill 
    154      1.1  jmcneill 	/* Enable Timer 0 IRQ */
    155      1.1  jmcneill 	const uint32_t irq_en = TIMER_READ(sc, TMR_IRQ_EN_REG);
    156      1.1  jmcneill 	TIMER_WRITE(sc, TMR_IRQ_EN_REG, irq_en | TMR_IRQ_EN(0));
    157      1.1  jmcneill }
    158      1.1  jmcneill 
    159      1.1  jmcneill static u_int
    160      1.1  jmcneill sunxi_timer_get_timecount(struct timecounter *tc)
    161      1.1  jmcneill {
    162      1.1  jmcneill 	struct sunxi_timer_softc * const sc = tc->tc_priv;
    163      1.1  jmcneill 
    164      1.3  jmcneill 	/* Timer current value is a 32-bit down counter. */
    165      1.3  jmcneill 	return ~TIMER_READ(sc, TMR2_CURNT_VALUE_REG);
    166      1.1  jmcneill }
    167      1.1  jmcneill 
    168  1.3.4.1  christos static u_int
    169  1.3.4.1  christos sunxi_timer_get_timecount_losc(struct timecounter *tc)
    170  1.3.4.1  christos {
    171  1.3.4.1  christos 	struct sunxi_timer_softc * const sc = tc->tc_priv;
    172  1.3.4.1  christos 
    173  1.3.4.1  christos 	return ~TIMER_READ(sc, TMR4_CURNT_VALUE_REG);
    174  1.3.4.1  christos }
    175  1.3.4.1  christos 
    176      1.1  jmcneill static int
    177      1.1  jmcneill sunxi_timer_match(device_t parent, cfdata_t cf, void *aux)
    178      1.1  jmcneill {
    179      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    180      1.1  jmcneill 
    181      1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    182      1.1  jmcneill }
    183      1.1  jmcneill 
    184      1.1  jmcneill static void
    185      1.1  jmcneill sunxi_timer_attach(device_t parent, device_t self, void *aux)
    186      1.1  jmcneill {
    187      1.1  jmcneill 	struct sunxi_timer_softc * const sc = device_private(self);
    188      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    189      1.1  jmcneill 	struct timecounter *tc = &sc->sc_tc;
    190  1.3.4.1  christos 	struct timecounter *tc_losc = &sc->sc_tc_losc;
    191      1.1  jmcneill 	const int phandle = faa->faa_phandle;
    192      1.1  jmcneill 	bus_addr_t addr;
    193      1.1  jmcneill 	bus_size_t size;
    194  1.3.4.1  christos 	u_int ticks;
    195  1.3.4.1  christos 	u_int reg;
    196      1.1  jmcneill 
    197      1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    198      1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    199      1.1  jmcneill 		return;
    200      1.1  jmcneill 	}
    201      1.1  jmcneill 
    202      1.1  jmcneill 	if ((sc->sc_clk = fdtbus_clock_get_index(phandle, 0)) == NULL) {
    203      1.1  jmcneill 		aprint_error(": couldn't get clock\n");
    204      1.1  jmcneill 		return;
    205      1.1  jmcneill 	}
    206      1.1  jmcneill 
    207      1.1  jmcneill 	sc->sc_dev = self;
    208      1.1  jmcneill 	sc->sc_phandle = phandle;
    209      1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    210      1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    211      1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    212      1.1  jmcneill 		return;
    213      1.1  jmcneill 	}
    214      1.1  jmcneill 
    215      1.1  jmcneill 	aprint_naive("\n");
    216      1.1  jmcneill 	aprint_normal(": Timer\n");
    217      1.1  jmcneill 
    218      1.1  jmcneill 	const u_int rate = clk_get_rate(sc->sc_clk);
    219      1.1  jmcneill 
    220      1.1  jmcneill 	/* Disable IRQs and all timers */
    221      1.1  jmcneill 	TIMER_WRITE(sc, TMR_IRQ_EN_REG, 0);
    222      1.1  jmcneill 	TIMER_WRITE(sc, TMR_IRQ_STAS_REG, TIMER_READ(sc, TMR_IRQ_STAS_REG));
    223      1.3  jmcneill 	/* Enable Timer 0 (hardclock) */
    224      1.1  jmcneill 	TIMER_WRITE(sc, TMR0_INTV_VALUE_REG, rate / hz);
    225      1.1  jmcneill 	TIMER_WRITE(sc, TMR0_CTRL_REG,
    226      1.1  jmcneill 	    __SHIFTIN(TMR0_CTRL_CLK_SRC_OSC24M, TMR0_CTRL_CLK_SRC) |
    227      1.1  jmcneill 	    TMR0_CTRL_RELOAD | TMR0_CTRL_EN);
    228      1.3  jmcneill 	/* Enable Timer 2 (timecounter) */
    229      1.3  jmcneill 	TIMER_WRITE(sc, TMR2_INTV_VALUE_REG, ~0u);
    230      1.3  jmcneill 	TIMER_WRITE(sc, TMR2_CTRL_REG,
    231      1.3  jmcneill 	    __SHIFTIN(TMR2_CTRL_CLK_SRC_OSC24M, TMR2_CTRL_CLK_SRC) |
    232      1.3  jmcneill 	    TMR2_CTRL_RELOAD | TMR2_CTRL_EN);
    233  1.3.4.1  christos 	/* Enable Timer 4 (timecounter for LOSC) */
    234  1.3.4.1  christos 	TIMER_WRITE(sc, TMR4_INTV_VALUE_REG, ~0u);
    235  1.3.4.1  christos 	TIMER_WRITE(sc, TMR4_CTRL_REG, TMR4_CTRL_RELOAD | TMR4_CTRL_EN);
    236      1.2  jmcneill 
    237      1.1  jmcneill 	/* Timecounter setup */
    238      1.1  jmcneill 	tc->tc_get_timecount = sunxi_timer_get_timecount;
    239      1.1  jmcneill 	tc->tc_counter_mask = ~0u,
    240  1.3.4.1  christos 	tc->tc_frequency = rate;
    241      1.3  jmcneill 	tc->tc_name = "Timer 2";
    242      1.3  jmcneill 	tc->tc_quality = 200;
    243      1.1  jmcneill 	tc->tc_priv = sc;
    244      1.1  jmcneill 	tc_init(tc);
    245  1.3.4.1  christos 	tc_losc->tc_get_timecount = sunxi_timer_get_timecount_losc;
    246  1.3.4.1  christos 	tc_losc->tc_counter_mask = ~0u;
    247  1.3.4.1  christos 	tc_losc->tc_frequency = 32768;
    248  1.3.4.1  christos 	tc_losc->tc_name = "LOSC";
    249  1.3.4.1  christos 	tc_losc->tc_quality = 150;
    250  1.3.4.1  christos 	tc_losc->tc_priv = sc;
    251  1.3.4.1  christos 	/*
    252  1.3.4.1  christos 	 * LOSC is optional to implement in hardware.
    253  1.3.4.1  christos 	 * Make sure it ticks before registering it.
    254  1.3.4.1  christos 	 */
    255  1.3.4.1  christos 	reg = __SHIFTIN(LOSC_CTRL_KEY_FIELD_V, LOSC_CTRL_KEY_FIELD) |
    256  1.3.4.1  christos 	    LOSC_CTRL_OSC32K_AUTO_SWT_EN |
    257  1.3.4.1  christos 	    LOSC_CTRL_OSC32K_SEL;
    258  1.3.4.1  christos 	TIMER_WRITE(sc, LOSC_CTRL_REG, reg);
    259  1.3.4.1  christos 	ticks = sunxi_timer_get_timecount_losc(tc_losc);
    260  1.3.4.1  christos 	delay(100);
    261  1.3.4.1  christos 	if (ticks != sunxi_timer_get_timecount_losc(tc_losc))
    262  1.3.4.1  christos 		tc_init(tc_losc);
    263  1.3.4.1  christos 	else
    264  1.3.4.1  christos 		TIMER_WRITE(sc, LOSC_CTRL_REG, reg & ~LOSC_CTRL_OSC32K_SEL);
    265      1.1  jmcneill 
    266      1.1  jmcneill 	/* Use this as the OS timer in UP configurations */
    267      1.1  jmcneill 	if (!arm_has_mpext_p) {
    268      1.1  jmcneill 		timer_softc = sc;
    269      1.1  jmcneill 		arm_fdt_timer_register(sunxi_timer_cpu_initclocks);
    270      1.1  jmcneill 	}
    271      1.1  jmcneill }
    272      1.1  jmcneill 
    273      1.1  jmcneill CFATTACH_DECL_NEW(sunxi_timer, sizeof(struct sunxi_timer_softc),
    274      1.1  jmcneill 	sunxi_timer_match, sunxi_timer_attach, NULL, NULL);
    275