xref: /netbsd-src/sys/arch/arm/sunxi/sunxi_hstimer.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: sunxi_hstimer.c,v 1.4 2021/01/27 03:10:20 thorpej Exp $ */
28369f724Stnn 
38369f724Stnn /*-
48369f724Stnn  * Copyright (c) 2019 Tobias Nygren <tnn@NetBSD.org>
58369f724Stnn  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
68369f724Stnn  * All rights reserved.
78369f724Stnn  *
88369f724Stnn  * Redistribution and use in source and binary forms, with or without
98369f724Stnn  * modification, are permitted provided that the following conditions
108369f724Stnn  * are met:
118369f724Stnn  * 1. Redistributions of source code must retain the above copyright
128369f724Stnn  *    notice, this list of conditions and the following disclaimer.
138369f724Stnn  * 2. Redistributions in binary form must reproduce the above copyright
148369f724Stnn  *    notice, this list of conditions and the following disclaimer in the
158369f724Stnn  *    documentation and/or other materials provided with the distribution.
168369f724Stnn  *
178369f724Stnn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
188369f724Stnn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
198369f724Stnn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
208369f724Stnn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
218369f724Stnn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
228369f724Stnn  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
238369f724Stnn  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
248369f724Stnn  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
258369f724Stnn  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268369f724Stnn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278369f724Stnn  * SUCH DAMAGE.
288369f724Stnn  */
298369f724Stnn 
308369f724Stnn #include <sys/cdefs.h>
31*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_hstimer.c,v 1.4 2021/01/27 03:10:20 thorpej Exp $");
328369f724Stnn 
338369f724Stnn #include <sys/bus.h>
348369f724Stnn #include <sys/device.h>
358369f724Stnn #include <sys/timetc.h>
368369f724Stnn #include <dev/fdt/fdtvar.h>
378369f724Stnn 
388369f724Stnn /* High Speed Timer registers */
398369f724Stnn #define	HS_TMR_IRQ_EN_REG	0x0
408369f724Stnn #define	HS_TMR_IRQ_EN(n)	__BIT(n)
418369f724Stnn #define	HS_TMR_IRQ_STAS_REG	0x4
428369f724Stnn #define	HS_TMR_STAS_PEND(n)	__BIT(n)
438369f724Stnn #define	HS_TMR0_CTRL_REG	0x10
448369f724Stnn #define	 HS_TMR0_CTRL_MODE	__BIT(7)
458369f724Stnn #define	 HS_TMR0_CTRL_CLK_PRESCALE	__BITS(6,4)
468369f724Stnn #define	 HS_TMR0_CTRL_RELOAD	__BIT(1)
478369f724Stnn #define	 HS_TMR0_CTRL_EN	__BIT(0)
488369f724Stnn #define	HS_TMR0_INTV_LO_REG	0x14
498369f724Stnn #define	HS_TMR0_INTV_HI_REG	0x18
508369f724Stnn #define	HS_TMR0_CURNT_LO_REG	0x1c
518369f724Stnn #define	HS_TMR0_CURNT_HI_REG	0x20
528369f724Stnn #define	HS_TMR1_CTRL_REG	0x30
538369f724Stnn #define	 HS_TMR1_CTRL_MODE	__BIT(7)
548369f724Stnn #define	 HS_TMR1_CTRL_CLK_PRESCALE	__BITS(6,4)
558369f724Stnn #define	 HS_TMR1_CTRL_RELOAD	__BIT(1)
568369f724Stnn #define	 HS_TMR1_CTRL_EN	__BIT(0)
578369f724Stnn #define	HS_TMR1_INTV_LO_REG	0x34
588369f724Stnn #define	HS_TMR1_INTV_HI_REG	0x38
598369f724Stnn #define	HS_TMR1_CURNT_LO_REG	0x3c
608369f724Stnn #define	HS_TMR1_CURNT_HI_REG	0x40
618369f724Stnn #define	HS_TMR2_CTRL_REG	0x50
628369f724Stnn #define	 HS_TMR2_CTRL_MODE	__BIT(7)
638369f724Stnn #define	 HS_TMR2_CTRL_CLK_PRESCALE	__BITS(6,4)
648369f724Stnn #define	 HS_TMR2_CTRL_RELOAD	__BIT(1)
658369f724Stnn #define	 HS_TMR2_CTRL_EN	__BIT(0)
668369f724Stnn #define	HS_TMR2_INTV_LO_REG	0x54
678369f724Stnn #define	HS_TMR2_INTV_HI_REG	0x58
688369f724Stnn #define	HS_TMR2_CURNT_LO_REG	0x5c
698369f724Stnn #define	HS_TMR2_CURNT_HI_REG	0x60
708369f724Stnn #define	HS_TMR3_CTRL_REG	0x70
718369f724Stnn #define	 HS_TMR3_CTRL_MODE	__BIT(7)
728369f724Stnn #define	 HS_TMR3_CTRL_CLK_PRESCALE	__BITS(6,4)
738369f724Stnn #define	 HS_TMR3_CTRL_RELOAD	__BIT(1)
748369f724Stnn #define	 HS_TMR3_CTRL_EN	__BIT(0)
758369f724Stnn #define	HS_TMR3_INTV_LO_REG	0x74
768369f724Stnn #define	HS_TMR3_INTV_HI_REG	0x78
778369f724Stnn #define	HS_TMR3_CURNT_LO_REG	0x7c
788369f724Stnn #define	HS_TMR3_CURNT_HI_REG	0x80
798369f724Stnn 
80*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
81*6e54367aSthorpej 	{ .compat = "allwinner,sun5i-a13-hstimer" },
82*6e54367aSthorpej 	{ .compat = "allwinner,sun6i-a31-hstimer" },
83*6e54367aSthorpej 	{ .compat = "allwinner,sun7i-a20-hstimer" },
84*6e54367aSthorpej 	DEVICE_COMPAT_EOL
858369f724Stnn };
868369f724Stnn 
878369f724Stnn struct sunxi_hstimer_softc {
888369f724Stnn 	device_t		sc_dev;
898369f724Stnn 	bus_space_tag_t		sc_bst;
908369f724Stnn 	bus_space_handle_t	sc_bsh;
918369f724Stnn 	int			sc_phandle;
928369f724Stnn 	void			*sc_ih;
938369f724Stnn 	struct timecounter	sc_tc;
948369f724Stnn };
958369f724Stnn 
968369f724Stnn #define TIMER_READ(sc, reg) \
978369f724Stnn     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
988369f724Stnn #define TIMER_WRITE(sc, reg, val) \
998369f724Stnn     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
1008369f724Stnn 
1018369f724Stnn static int
sunxi_hstimer_intr(void * arg)1028369f724Stnn sunxi_hstimer_intr(void *arg)
1038369f724Stnn {
1048369f724Stnn 	struct sunxi_hstimer_softc *sc = arg;
1058369f724Stnn 	uint32_t stas;
1068369f724Stnn 
1078369f724Stnn 	stas = TIMER_READ(sc, HS_TMR_IRQ_STAS_REG);
1088369f724Stnn 	if (stas == 0)
1098369f724Stnn 		return 0;
1108369f724Stnn 	TIMER_WRITE(sc, HS_TMR_IRQ_STAS_REG, stas);
1118369f724Stnn 
1128369f724Stnn 	return 1;
1138369f724Stnn }
1148369f724Stnn 
1158369f724Stnn static u_int
sunxi_hstimer_get_timecount(struct timecounter * tc)1168369f724Stnn sunxi_hstimer_get_timecount(struct timecounter *tc)
1178369f724Stnn {
1188369f724Stnn 	struct sunxi_hstimer_softc *sc = tc->tc_priv;
1198369f724Stnn 
1208369f724Stnn 	/*
1218369f724Stnn 	 * Timer current value is a 56-bit down counter.
1228369f724Stnn 	 * But we only need the lower 32 bits for timecounter.
1238369f724Stnn 	 */
1248369f724Stnn 	return ~TIMER_READ(sc, HS_TMR0_CURNT_LO_REG);
1258369f724Stnn }
1268369f724Stnn 
1278369f724Stnn static int
sunxi_hstimer_match(device_t parent,cfdata_t cf,void * aux)1288369f724Stnn sunxi_hstimer_match(device_t parent, cfdata_t cf, void *aux)
1298369f724Stnn {
1308369f724Stnn 	struct fdt_attach_args * const faa = aux;
1318369f724Stnn 
132*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
1338369f724Stnn }
1348369f724Stnn 
1358369f724Stnn static void
sunxi_hstimer_attach(device_t parent,device_t self,void * aux)1368369f724Stnn sunxi_hstimer_attach(device_t parent, device_t self, void *aux)
1378369f724Stnn {
1388369f724Stnn 	struct sunxi_hstimer_softc *sc = device_private(self);
1398369f724Stnn 	struct fdt_attach_args *faa = aux;
1408369f724Stnn 	struct timecounter *tc = &sc->sc_tc;
1418369f724Stnn 	const int phandle = faa->faa_phandle;
1428369f724Stnn 	bus_addr_t addr;
1438369f724Stnn 	bus_size_t size;
1448369f724Stnn 	char intrstr[128];
1458369f724Stnn 	struct clk *clk;
1468369f724Stnn 
1478369f724Stnn 	sc->sc_dev = self;
1488369f724Stnn 	sc->sc_phandle = phandle;
1498369f724Stnn 	sc->sc_bst = faa->faa_bst;
1508369f724Stnn 
1518369f724Stnn 	if ((clk = fdtbus_clock_get_index(phandle, 0)) == NULL
1528369f724Stnn 	    || clk_enable(clk) != 0) {
1538369f724Stnn 		aprint_error(": couldn't enable clock\n");
1548369f724Stnn 		return;
1558369f724Stnn 	}
1568369f724Stnn 
1578369f724Stnn 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
1588369f724Stnn 	    || bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
1598369f724Stnn 		aprint_error(": couldn't map registers\n");
1608369f724Stnn 		return;
1618369f724Stnn 	}
1628369f724Stnn 
1638369f724Stnn 	aprint_naive("\n");
1648369f724Stnn 	aprint_normal(": High Speed Timer\n");
1658369f724Stnn 
1668369f724Stnn 	/* Disable IRQs and all timers */
1678369f724Stnn 	TIMER_WRITE(sc, HS_TMR_IRQ_EN_REG, 0);
1688369f724Stnn 	TIMER_WRITE(sc, HS_TMR_IRQ_STAS_REG, TIMER_READ(sc, HS_TMR_IRQ_STAS_REG));
1698369f724Stnn 	/* Enable Timer 0 (timecounter) */
1708369f724Stnn 	TIMER_WRITE(sc, HS_TMR0_CTRL_REG, 0);
1718369f724Stnn 	TIMER_WRITE(sc, HS_TMR0_INTV_LO_REG, ~0u);
1728369f724Stnn 	TIMER_WRITE(sc, HS_TMR0_INTV_HI_REG, ~0u);
1738369f724Stnn 	TIMER_WRITE(sc, HS_TMR0_CTRL_REG,
1748369f724Stnn 	    HS_TMR0_CTRL_RELOAD | HS_TMR0_CTRL_EN);
1758369f724Stnn 
1768369f724Stnn 	/* Timecounter setup */
1778369f724Stnn 	tc->tc_get_timecount = sunxi_hstimer_get_timecount;
178349b1fb4Stnn 	tc->tc_counter_mask = ~0u;
1798369f724Stnn 	tc->tc_frequency = clk_get_rate(clk);
1808369f724Stnn 	tc->tc_name = "hstimer";
1818369f724Stnn 	tc->tc_quality = 300;
1828369f724Stnn 	tc->tc_priv = sc;
1838369f724Stnn 	tc_init(tc);
1848369f724Stnn 
1858369f724Stnn 	if (!fdtbus_intr_str(sc->sc_phandle, 0, intrstr, sizeof(intrstr))) {
1868369f724Stnn 		aprint_error_dev(self, "failed to decode interrupt\n");
1878369f724Stnn 		return;
1888369f724Stnn 	}
189076a1169Sjmcneill 	sc->sc_ih = fdtbus_intr_establish_xname(sc->sc_phandle, 0, IPL_CLOCK,
190076a1169Sjmcneill 	    FDT_INTR_MPSAFE, sunxi_hstimer_intr, sc, device_xname(sc->sc_dev));
1918369f724Stnn 	if (sc->sc_ih == NULL) {
1928369f724Stnn 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
1938369f724Stnn 				 intrstr);
1948369f724Stnn 		return;
1958369f724Stnn 	}
1968369f724Stnn 	aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
1978369f724Stnn }
1988369f724Stnn 
1998369f724Stnn CFATTACH_DECL_NEW(sunxi_hstimer, sizeof(struct sunxi_hstimer_softc),
2008369f724Stnn 	sunxi_hstimer_match, sunxi_hstimer_attach, NULL, NULL);
2018369f724Stnn 
202