Home | History | Annotate | Line # | Download | only in cortex
a9tmr.c revision 1.1.2.5
      1  1.1.2.3       tls /*	$NetBSD: a9tmr.c,v 1.1.2.5 2017/12/03 11:35:52 jdolecek Exp $	*/
      2      1.1      matt 
      3      1.1      matt /*-
      4      1.1      matt  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5      1.1      matt  * All rights reserved.
      6      1.1      matt  *
      7      1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1      matt  * by Matt Thomas
      9      1.1      matt  *
     10      1.1      matt  * Redistribution and use in source and binary forms, with or without
     11      1.1      matt  * modification, are permitted provided that the following conditions
     12      1.1      matt  * are met:
     13      1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14      1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15      1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17      1.1      matt  *    documentation and/or other materials provided with the distribution.
     18      1.1      matt  *
     19      1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1      matt  */
     31      1.1      matt 
     32      1.1      matt #include <sys/cdefs.h>
     33  1.1.2.3       tls __KERNEL_RCSID(0, "$NetBSD: a9tmr.c,v 1.1.2.5 2017/12/03 11:35:52 jdolecek Exp $");
     34      1.1      matt 
     35      1.1      matt #include <sys/param.h>
     36      1.1      matt #include <sys/bus.h>
     37      1.1      matt #include <sys/device.h>
     38      1.1      matt #include <sys/intr.h>
     39      1.1      matt #include <sys/kernel.h>
     40      1.1      matt #include <sys/proc.h>
     41      1.1      matt #include <sys/systm.h>
     42      1.1      matt #include <sys/timetc.h>
     43  1.1.2.5  jdolecek #include <sys/xcall.h>
     44      1.1      matt 
     45      1.1      matt #include <prop/proplib.h>
     46      1.1      matt 
     47      1.1      matt #include <arm/cortex/a9tmr_reg.h>
     48      1.1      matt #include <arm/cortex/a9tmr_var.h>
     49      1.1      matt 
     50      1.1      matt #include <arm/cortex/mpcore_var.h>
     51      1.1      matt 
     52      1.1      matt static int a9tmr_match(device_t, cfdata_t, void *);
     53      1.1      matt static void a9tmr_attach(device_t, device_t, void *);
     54      1.1      matt 
     55      1.1      matt static int clockhandler(void *);
     56      1.1      matt 
     57      1.1      matt static u_int a9tmr_get_timecount(struct timecounter *);
     58      1.1      matt 
     59      1.1      matt static struct a9tmr_softc a9tmr_sc;
     60      1.1      matt 
     61      1.1      matt static struct timecounter a9tmr_timecounter = {
     62      1.1      matt 	.tc_get_timecount = a9tmr_get_timecount,
     63      1.1      matt 	.tc_poll_pps = 0,
     64      1.1      matt 	.tc_counter_mask = ~0u,
     65      1.1      matt 	.tc_frequency = 0,			/* set by cpu_initclocks() */
     66      1.1      matt 	.tc_name = NULL,			/* set by attach */
     67      1.1      matt 	.tc_quality = 500,
     68      1.1      matt 	.tc_priv = &a9tmr_sc,
     69      1.1      matt 	.tc_next = NULL,
     70      1.1      matt };
     71      1.1      matt 
     72      1.1      matt CFATTACH_DECL_NEW(a9tmr, 0, a9tmr_match, a9tmr_attach, NULL, NULL);
     73      1.1      matt 
     74      1.1      matt static inline uint32_t
     75      1.1      matt a9tmr_global_read(struct a9tmr_softc *sc, bus_size_t o)
     76      1.1      matt {
     77      1.1      matt 	return bus_space_read_4(sc->sc_memt, sc->sc_global_memh, o);
     78      1.1      matt }
     79      1.1      matt 
     80      1.1      matt static inline void
     81      1.1      matt a9tmr_global_write(struct a9tmr_softc *sc, bus_size_t o, uint32_t v)
     82      1.1      matt {
     83      1.1      matt 	bus_space_write_4(sc->sc_memt, sc->sc_global_memh, o, v);
     84      1.1      matt }
     85      1.1      matt 
     86      1.1      matt 
     87      1.1      matt /* ARGSUSED */
     88      1.1      matt static int
     89      1.1      matt a9tmr_match(device_t parent, cfdata_t cf, void *aux)
     90      1.1      matt {
     91      1.1      matt 	struct mpcore_attach_args * const mpcaa = aux;
     92      1.1      matt 
     93      1.1      matt 	if (a9tmr_sc.sc_dev != NULL)
     94      1.1      matt 		return 0;
     95      1.1      matt 
     96  1.1.2.3       tls 	if ((armreg_pfr1_read() & ARM_PFR1_GTIMER_MASK) != 0)
     97  1.1.2.3       tls 		return 0;
     98  1.1.2.3       tls 
     99  1.1.2.5  jdolecek 	if (!CPU_ID_CORTEX_A9_P(curcpu()->ci_arm_cpuid) &&
    100  1.1.2.5  jdolecek 	    !CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid))
    101      1.1      matt 		return 0;
    102      1.1      matt 
    103      1.1      matt 	if (strcmp(mpcaa->mpcaa_name, cf->cf_name) != 0)
    104      1.1      matt 		return 0;
    105      1.1      matt 
    106      1.1      matt 	/*
    107      1.1      matt 	 * This isn't present on UP A9s (since CBAR isn't present).
    108      1.1      matt 	 */
    109      1.1      matt 	uint32_t mpidr = armreg_mpidr_read();
    110      1.1      matt 	if (mpidr == 0 || (mpidr & MPIDR_U))
    111      1.1      matt 		return 0;
    112      1.1      matt 
    113      1.1      matt 	return 1;
    114      1.1      matt }
    115      1.1      matt 
    116      1.1      matt static void
    117      1.1      matt a9tmr_attach(device_t parent, device_t self, void *aux)
    118      1.1      matt {
    119  1.1.2.5  jdolecek 	struct a9tmr_softc *sc = &a9tmr_sc;
    120      1.1      matt 	struct mpcore_attach_args * const mpcaa = aux;
    121      1.1      matt 	prop_dictionary_t dict = device_properties(self);
    122      1.1      matt 	char freqbuf[sizeof("XXX SHz")];
    123  1.1.2.5  jdolecek 	const char *cpu_type;
    124      1.1      matt 
    125      1.1      matt 	/*
    126      1.1      matt 	 * This runs at the ARM PERIPHCLOCK which should be 1/2 of the CPU clock.
    127      1.1      matt 	 * The MD code should have setup our frequency for us.
    128      1.1      matt 	 */
    129  1.1.2.3       tls 	prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq);
    130      1.1      matt 
    131      1.1      matt 	humanize_number(freqbuf, sizeof(freqbuf), sc->sc_freq, "Hz", 1000);
    132      1.1      matt 
    133      1.1      matt 	aprint_naive("\n");
    134  1.1.2.5  jdolecek 	if (CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid)) {
    135  1.1.2.5  jdolecek 		cpu_type = "A5";
    136  1.1.2.5  jdolecek 	} else {
    137  1.1.2.5  jdolecek 		cpu_type = "A9";
    138  1.1.2.5  jdolecek 	}
    139  1.1.2.5  jdolecek 	aprint_normal(": %s Global 64-bit Timer (%s)\n", cpu_type, freqbuf);
    140      1.1      matt 
    141  1.1.2.1       tls 	self->dv_private = sc;
    142      1.1      matt 	sc->sc_dev = self;
    143      1.1      matt 	sc->sc_memt = mpcaa->mpcaa_memt;
    144      1.1      matt 	sc->sc_memh = mpcaa->mpcaa_memh;
    145      1.1      matt 
    146  1.1.2.1       tls 	evcnt_attach_dynamic(&sc->sc_ev_missing_ticks, EVCNT_TYPE_MISC, NULL,
    147  1.1.2.1       tls 	    device_xname(self), "missing interrupts");
    148  1.1.2.1       tls 
    149      1.1      matt 	bus_space_subregion(sc->sc_memt, sc->sc_memh,
    150  1.1.2.5  jdolecek 	    TMR_GLOBAL_BASE, TMR_GLOBAL_SIZE, &sc->sc_global_memh);
    151      1.1      matt 	bus_space_subregion(sc->sc_memt, sc->sc_memh,
    152      1.1      matt 	    TMR_PRIVATE_BASE, TMR_PRIVATE_SIZE, &sc->sc_private_memh);
    153      1.1      matt 	bus_space_subregion(sc->sc_memt, sc->sc_memh,
    154      1.1      matt 	    TMR_WDOG_BASE, TMR_WDOG_SIZE, &sc->sc_wdog_memh);
    155      1.1      matt 
    156      1.1      matt 	sc->sc_global_ih = intr_establish(IRQ_A9TMR_PPI_GTIMER, IPL_CLOCK,
    157  1.1.2.4       tls 	    IST_EDGE | IST_MPSAFE, clockhandler, NULL);
    158      1.1      matt 	if (sc->sc_global_ih == NULL)
    159      1.1      matt 		panic("%s: unable to register timer interrupt", __func__);
    160      1.1      matt 	aprint_normal_dev(sc->sc_dev, "interrupting on irq %d\n",
    161      1.1      matt 	    IRQ_A9TMR_PPI_GTIMER);
    162      1.1      matt }
    163      1.1      matt 
    164      1.1      matt static inline uint64_t
    165      1.1      matt a9tmr_gettime(struct a9tmr_softc *sc)
    166      1.1      matt {
    167      1.1      matt 	uint32_t lo, hi;
    168      1.1      matt 
    169      1.1      matt 	do {
    170      1.1      matt 		hi = a9tmr_global_read(sc, TMR_GBL_CTR_U);
    171      1.1      matt 		lo = a9tmr_global_read(sc, TMR_GBL_CTR_L);
    172      1.1      matt 	} while (hi != a9tmr_global_read(sc, TMR_GBL_CTR_U));
    173      1.1      matt 
    174      1.1      matt 	return ((uint64_t)hi << 32) | lo;
    175      1.1      matt }
    176      1.1      matt 
    177      1.1      matt void
    178      1.1      matt a9tmr_init_cpu_clock(struct cpu_info *ci)
    179      1.1      matt {
    180      1.1      matt 	struct a9tmr_softc * const sc = &a9tmr_sc;
    181      1.1      matt 	uint64_t now = a9tmr_gettime(sc);
    182      1.1      matt 
    183      1.1      matt 	KASSERT(ci == curcpu());
    184      1.1      matt 
    185      1.1      matt 	ci->ci_lastintr = now;
    186      1.1      matt 
    187      1.1      matt 	a9tmr_global_write(sc, TMR_GBL_AUTOINC, sc->sc_autoinc);
    188      1.1      matt 
    189      1.1      matt 	/*
    190      1.1      matt 	 * To update the compare register we have to disable comparisions first.
    191      1.1      matt 	 */
    192      1.1      matt 	uint32_t ctl = a9tmr_global_read(sc, TMR_GBL_CTL);
    193      1.1      matt 	if (ctl & TMR_GBL_CTL_CMP_ENABLE) {
    194  1.1.2.5  jdolecek 		a9tmr_global_write(sc, TMR_GBL_CTL,
    195  1.1.2.5  jdolecek 		    ctl & ~TMR_GBL_CTL_CMP_ENABLE);
    196      1.1      matt 	}
    197      1.1      matt 
    198      1.1      matt 	/*
    199      1.1      matt 	 * Schedule the next interrupt.
    200      1.1      matt 	 */
    201      1.1      matt 	now += sc->sc_autoinc;
    202      1.1      matt 	a9tmr_global_write(sc, TMR_GBL_CMP_L, (uint32_t) now);
    203      1.1      matt 	a9tmr_global_write(sc, TMR_GBL_CMP_H, (uint32_t) (now >> 32));
    204      1.1      matt 
    205      1.1      matt 	/*
    206      1.1      matt 	 * Re-enable the comparator and now enable interrupts.
    207      1.1      matt 	 */
    208      1.1      matt 	a9tmr_global_write(sc, TMR_GBL_INT, 1);	/* clear interrupt pending */
    209  1.1.2.5  jdolecek 	ctl |= TMR_GBL_CTL_CMP_ENABLE | TMR_GBL_CTL_INT_ENABLE |
    210  1.1.2.5  jdolecek 	    TMR_GBL_CTL_AUTO_INC | TMR_CTL_ENABLE;
    211      1.1      matt 	a9tmr_global_write(sc, TMR_GBL_CTL, ctl);
    212      1.1      matt #if 0
    213      1.1      matt 	printf("%s: %s: ctl %#x autoinc %u cmp %#x%08x now %#"PRIx64"\n",
    214      1.1      matt 	    __func__, ci->ci_data.cpu_name,
    215      1.1      matt 	    a9tmr_global_read(sc, TMR_GBL_CTL),
    216      1.1      matt 	    a9tmr_global_read(sc, TMR_GBL_AUTOINC),
    217      1.1      matt 	    a9tmr_global_read(sc, TMR_GBL_CMP_H),
    218      1.1      matt 	    a9tmr_global_read(sc, TMR_GBL_CMP_L),
    219      1.1      matt 	    a9tmr_gettime(sc));
    220      1.1      matt 
    221      1.1      matt 	int s = splsched();
    222      1.1      matt 	uint64_t when = now;
    223      1.1      matt 	u_int n = 0;
    224      1.1      matt 	while ((now = a9tmr_gettime(sc)) < when) {
    225      1.1      matt 		/* spin */
    226      1.1      matt 		n++;
    227      1.1      matt 		KASSERTMSG(n <= sc->sc_autoinc,
    228      1.1      matt 		    "spun %u times but only %"PRIu64" has passed",
    229      1.1      matt 		    n, when - now);
    230      1.1      matt 	}
    231      1.1      matt 	printf("%s: %s: status %#x cmp %#x%08x now %#"PRIx64"\n",
    232      1.1      matt 	    __func__, ci->ci_data.cpu_name,
    233      1.1      matt 	    a9tmr_global_read(sc, TMR_GBL_INT),
    234      1.1      matt 	    a9tmr_global_read(sc, TMR_GBL_CMP_H),
    235      1.1      matt 	    a9tmr_global_read(sc, TMR_GBL_CMP_L),
    236      1.1      matt 	    a9tmr_gettime(sc));
    237      1.1      matt 	splx(s);
    238      1.1      matt #elif 0
    239      1.1      matt 	delay(1000000 / hz + 1000);
    240      1.1      matt #endif
    241      1.1      matt }
    242      1.1      matt 
    243      1.1      matt void
    244      1.1      matt cpu_initclocks(void)
    245      1.1      matt {
    246      1.1      matt 	struct a9tmr_softc * const sc = &a9tmr_sc;
    247  1.1.2.5  jdolecek 
    248      1.1      matt 	KASSERT(sc->sc_dev != NULL);
    249      1.1      matt 	KASSERT(sc->sc_freq != 0);
    250      1.1      matt 
    251      1.1      matt 	sc->sc_autoinc = sc->sc_freq / hz;
    252      1.1      matt 
    253      1.1      matt 	a9tmr_init_cpu_clock(curcpu());
    254      1.1      matt 
    255      1.1      matt 	a9tmr_timecounter.tc_name = device_xname(sc->sc_dev);
    256      1.1      matt 	a9tmr_timecounter.tc_frequency = sc->sc_freq;
    257      1.1      matt 
    258      1.1      matt 	tc_init(&a9tmr_timecounter);
    259      1.1      matt }
    260      1.1      matt 
    261  1.1.2.5  jdolecek static void
    262  1.1.2.5  jdolecek a9tmr_update_freq_cb(void *arg1, void *arg2)
    263  1.1.2.5  jdolecek {
    264  1.1.2.5  jdolecek 	a9tmr_init_cpu_clock(curcpu());
    265  1.1.2.5  jdolecek }
    266  1.1.2.5  jdolecek 
    267  1.1.2.5  jdolecek void
    268  1.1.2.5  jdolecek a9tmr_update_freq(uint32_t freq)
    269  1.1.2.5  jdolecek {
    270  1.1.2.5  jdolecek 	struct a9tmr_softc * const sc = &a9tmr_sc;
    271  1.1.2.5  jdolecek 	uint64_t xc;
    272  1.1.2.5  jdolecek 
    273  1.1.2.5  jdolecek 	KASSERT(sc->sc_dev != NULL);
    274  1.1.2.5  jdolecek 	KASSERT(freq != 0);
    275  1.1.2.5  jdolecek 
    276  1.1.2.5  jdolecek 	tc_detach(&a9tmr_timecounter);
    277  1.1.2.5  jdolecek 
    278  1.1.2.5  jdolecek 	sc->sc_freq = freq;
    279  1.1.2.5  jdolecek 	sc->sc_autoinc = sc->sc_freq / hz;
    280  1.1.2.5  jdolecek 
    281  1.1.2.5  jdolecek 	xc = xc_broadcast(0, a9tmr_update_freq_cb, NULL, NULL);
    282  1.1.2.5  jdolecek 	xc_wait(xc);
    283  1.1.2.5  jdolecek 
    284  1.1.2.5  jdolecek 	a9tmr_timecounter.tc_frequency = sc->sc_freq;
    285  1.1.2.5  jdolecek 	tc_init(&a9tmr_timecounter);
    286  1.1.2.5  jdolecek }
    287  1.1.2.5  jdolecek 
    288      1.1      matt void
    289      1.1      matt a9tmr_delay(unsigned int n)
    290      1.1      matt {
    291      1.1      matt 	struct a9tmr_softc * const sc = &a9tmr_sc;
    292      1.1      matt 
    293      1.1      matt 	KASSERT(sc != NULL);
    294      1.1      matt 
    295  1.1.2.5  jdolecek 	uint32_t freq = sc->sc_freq ? sc->sc_freq :
    296  1.1.2.5  jdolecek 	    curcpu()->ci_data.cpu_cc_freq / 2;
    297      1.1      matt 	KASSERT(freq != 0);
    298      1.1      matt 
    299      1.1      matt 	/*
    300      1.1      matt 	 * not quite divide by 1000000 but close enough
    301      1.1      matt 	 * (higher by 1.3% which means we wait 1.3% longer).
    302      1.1      matt 	 */
    303      1.1      matt 	const uint64_t incr_per_us = (freq >> 20) + (freq >> 24);
    304      1.1      matt 
    305      1.1      matt 	const uint64_t delta = n * incr_per_us;
    306      1.1      matt 	const uint64_t base = a9tmr_gettime(sc);
    307      1.1      matt 	const uint64_t finish = base + delta;
    308      1.1      matt 
    309      1.1      matt 	while (a9tmr_gettime(sc) < finish) {
    310      1.1      matt 		/* spin */
    311      1.1      matt 	}
    312      1.1      matt }
    313      1.1      matt 
    314      1.1      matt /*
    315      1.1      matt  * clockhandler:
    316      1.1      matt  *
    317      1.1      matt  *	Handle the hardclock interrupt.
    318      1.1      matt  */
    319      1.1      matt static int
    320      1.1      matt clockhandler(void *arg)
    321      1.1      matt {
    322      1.1      matt 	struct clockframe * const cf = arg;
    323      1.1      matt 	struct a9tmr_softc * const sc = &a9tmr_sc;
    324      1.1      matt 	struct cpu_info * const ci = curcpu();
    325  1.1.2.5  jdolecek 
    326      1.1      matt 	const uint64_t now = a9tmr_gettime(sc);
    327      1.1      matt 	uint64_t delta = now - ci->ci_lastintr;
    328      1.1      matt 
    329  1.1.2.5  jdolecek 	a9tmr_global_write(sc, TMR_GBL_INT, 1);	/* Ack the interrupt */
    330      1.1      matt 
    331      1.1      matt #if 0
    332      1.1      matt 	printf("%s(%p): %s: now %#"PRIx64" delta %"PRIu64"\n",
    333      1.1      matt 	     __func__, cf, ci->ci_data.cpu_name, now, delta);
    334      1.1      matt #endif
    335  1.1.2.5  jdolecek 	KASSERTMSG(delta > sc->sc_autoinc / 64,
    336      1.1      matt 	    "%s: interrupting too quickly (delta=%"PRIu64")",
    337      1.1      matt 	    ci->ci_data.cpu_name, delta);
    338      1.1      matt 
    339      1.1      matt 	ci->ci_lastintr = now;
    340      1.1      matt 
    341      1.1      matt 	hardclock(cf);
    342      1.1      matt 
    343  1.1.2.5  jdolecek 	if (delta > sc->sc_autoinc) {
    344  1.1.2.5  jdolecek 		u_int ticks = hz;
    345  1.1.2.5  jdolecek 		for (delta -= sc->sc_autoinc;
    346  1.1.2.5  jdolecek 		     delta >= sc->sc_autoinc && ticks > 0;
    347  1.1.2.5  jdolecek 		     delta -= sc->sc_autoinc, ticks--) {
    348  1.1.2.1       tls #if 0
    349  1.1.2.5  jdolecek 			/*
    350  1.1.2.5  jdolecek 			 * Try to make up up to a seconds amount of
    351  1.1.2.5  jdolecek 			 * missed clock interrupts
    352  1.1.2.5  jdolecek 			 */
    353  1.1.2.5  jdolecek 			hardclock(cf);
    354  1.1.2.1       tls #else
    355  1.1.2.5  jdolecek 			sc->sc_ev_missing_ticks.ev_count++;
    356  1.1.2.1       tls #endif
    357  1.1.2.5  jdolecek 		}
    358  1.1.2.5  jdolecek 	}
    359      1.1      matt 
    360      1.1      matt 	return 1;
    361      1.1      matt }
    362      1.1      matt 
    363      1.1      matt void
    364      1.1      matt setstatclockrate(int newhz)
    365      1.1      matt {
    366      1.1      matt }
    367      1.1      matt 
    368      1.1      matt static u_int
    369      1.1      matt a9tmr_get_timecount(struct timecounter *tc)
    370      1.1      matt {
    371      1.1      matt 	struct a9tmr_softc * const sc = tc->tc_priv;
    372      1.1      matt 
    373  1.1.2.1       tls 	return (u_int) (a9tmr_gettime(sc));
    374      1.1      matt }
    375