Home | History | Annotate | Line # | Download | only in samsung
mct.c revision 1.11
      1  1.11  jmcneill /*	$NetBSD: mct.c,v 1.11 2017/06/11 01:09:44 jmcneill Exp $	*/
      2   1.1      matt 
      3   1.1      matt /*-
      4   1.1      matt  * Copyright (c) 2014 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 Reinoud Zandijk.
      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      matt 
     34  1.11  jmcneill __KERNEL_RCSID(1, "$NetBSD: mct.c,v 1.11 2017/06/11 01:09:44 jmcneill Exp $");
     35   1.1      matt 
     36   1.1      matt #include <sys/param.h>
     37   1.1      matt #include <sys/bus.h>
     38   1.1      matt #include <sys/device.h>
     39   1.1      matt #include <sys/intr.h>
     40   1.1      matt #include <sys/kernel.h>
     41   1.1      matt #include <sys/proc.h>
     42   1.1      matt #include <sys/systm.h>
     43   1.1      matt #include <sys/timetc.h>
     44   1.9     marty #include <sys/kmem.h>
     45   1.1      matt 
     46   1.1      matt #include <prop/proplib.h>
     47   1.1      matt 
     48   1.1      matt #include <arm/samsung/exynos_reg.h>
     49   1.1      matt #include <arm/samsung/exynos_var.h>
     50   1.1      matt #include <arm/samsung/mct_reg.h>
     51   1.1      matt #include <arm/samsung/mct_var.h>
     52   1.1      matt 
     53   1.7     marty #include <dev/fdt/fdtvar.h>
     54   1.1      matt 
     55   1.1      matt static int  mct_match(device_t, cfdata_t, void *);
     56   1.1      matt static void mct_attach(device_t, device_t, void *);
     57   1.1      matt 
     58  1.10     marty static int clockhandler(void *);
     59  1.11  jmcneill static u_int mct_get_timecount(struct timecounter *);
     60   1.1      matt 
     61   1.1      matt CFATTACH_DECL_NEW(exyo_mct, 0, mct_match, mct_attach, NULL, NULL);
     62   1.1      matt 
     63   1.1      matt 
     64   1.1      matt static struct timecounter mct_timecounter = {
     65   1.1      matt 	.tc_get_timecount = mct_get_timecount,
     66   1.1      matt 	.tc_poll_pps = 0,
     67   1.1      matt 	.tc_counter_mask = ~0u,
     68   1.1      matt 	.tc_frequency = 0,		/* set by cpu_initclocks() */
     69   1.1      matt 	.tc_name = NULL,		/* set by cpu_initclocks() */
     70   1.1      matt 	.tc_quality = 500,		/* why 500? */
     71   1.1      matt 	.tc_priv = &mct_sc,
     72   1.1      matt 	.tc_next = NULL,
     73   1.1      matt };
     74   1.1      matt 
     75   1.1      matt static inline uint32_t
     76   1.1      matt mct_read_global(struct mct_softc *sc, bus_size_t o)
     77   1.1      matt {
     78   1.1      matt 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
     79   1.1      matt }
     80   1.1      matt 
     81   1.1      matt 
     82   1.1      matt static inline void
     83   1.1      matt mct_write_global(struct mct_softc *sc, bus_size_t o, uint32_t v)
     84   1.1      matt {
     85   1.1      matt 	bus_size_t wreg;
     86   1.1      matt 	uint32_t bit;
     87   1.1      matt 	int i;
     88   1.1      matt 
     89   1.1      matt 	/* do the write */
     90   1.1      matt 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
     91   1.1      matt //	printf("%s: write %#x at %#x\n",
     92   1.1      matt //		__func__, ((uint32_t) sc->sc_bsh + (uint32_t) o), v);
     93   1.1      matt 
     94   1.1      matt 	/* dependent on the write address, do the ack dance */
     95   1.1      matt 	if (o == MCT_G_CNT_L || o == MCT_G_CNT_U) {
     96   1.1      matt 		wreg = MCT_G_CNT_WSTAT;
     97   1.1      matt 		bit  = (o == MCT_G_CNT_L) ? G_CNT_WSTAT_L : G_CNT_WSTAT_U;
     98   1.1      matt 	} else {
     99   1.1      matt 		wreg = MCT_G_WSTAT;
    100   1.1      matt 		switch (o) {
    101   1.1      matt 		case MCT_G_COMP0_L:
    102   1.1      matt 			bit  = G_WSTAT_COMP0_L;
    103   1.1      matt 			break;
    104   1.1      matt 		case MCT_G_COMP0_U:
    105   1.1      matt 			bit  = G_WSTAT_COMP0_U;
    106   1.1      matt 			break;
    107   1.1      matt 		case MCT_G_COMP0_ADD_INCR:
    108   1.1      matt 			bit  = G_WSTAT_ADD_INCR;
    109   1.1      matt 			break;
    110   1.1      matt 		case MCT_G_TCON:
    111   1.1      matt 			bit  = G_WSTAT_TCON;
    112   1.1      matt 			break;
    113   1.1      matt 		default:
    114   1.1      matt 			/* all other registers */
    115   1.1      matt 			return;
    116   1.1      matt 		}
    117   1.1      matt 	}
    118   1.1      matt 
    119   1.1      matt 	/* wait for ack */
    120   1.1      matt 	for (i = 0; i < 10000000; i++) {
    121   1.1      matt 		/* value accepted by the hardware/hal ? */
    122   1.1      matt 		if (mct_read_global(sc, wreg) & bit) {
    123   1.1      matt 			/* ack */
    124   1.1      matt 			bus_space_write_4(sc->sc_bst, sc->sc_bsh, wreg, bit);
    125   1.1      matt 			return;
    126   1.1      matt 		}
    127   1.1      matt 	}
    128   1.1      matt 	panic("MCT hangs after writing %#x at %#x", v, (uint32_t) o);
    129   1.1      matt }
    130   1.1      matt 
    131   1.1      matt static int
    132   1.1      matt mct_match(device_t parent, cfdata_t cf, void *aux)
    133   1.1      matt {
    134   1.7     marty 	const char * const compatible[] = { "samsung,exynos4210-mct",
    135   1.7     marty 					    NULL };
    136   1.1      matt 
    137   1.7     marty 	struct fdt_attach_args * const faa = aux;
    138   1.7     marty 	return of_match_compatible(faa->faa_phandle, compatible);
    139   1.1      matt }
    140   1.1      matt 
    141   1.1      matt 
    142   1.1      matt static void
    143   1.1      matt mct_attach(device_t parent, device_t self, void *aux)
    144   1.1      matt {
    145   1.1      matt 	struct mct_softc * const sc = &mct_sc;
    146   1.7     marty 	struct fdt_attach_args * const faa = aux;
    147   1.7     marty 	bus_addr_t addr;
    148   1.7     marty 	bus_size_t size;
    149   1.7     marty 	int error;
    150   1.7     marty 
    151   1.7     marty 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
    152   1.7     marty 		aprint_error(": couldn't get registers\n");
    153   1.7     marty 		return;
    154   1.7     marty 	}
    155   1.1      matt 
    156   1.1      matt 	self->dv_private = sc;
    157   1.1      matt 	sc->sc_dev = self;
    158   1.7     marty 	sc->sc_bst = faa->faa_bst;
    159  1.11  jmcneill 	sc->sc_freq = EXYNOS_F_IN_FREQ;
    160   1.7     marty 
    161   1.7     marty 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    162   1.7     marty 	if (error) {
    163   1.7     marty 		aprint_error(": couldn't map %#llx: %d",
    164   1.7     marty 			     (uint64_t)addr, error);
    165   1.7     marty 		return;
    166   1.7     marty 	}
    167   1.1      matt 
    168   1.1      matt 	aprint_naive("\n");
    169  1.11  jmcneill 	aprint_normal(": Exynos SoC multi core timer (64 bits)\n");
    170   1.1      matt 
    171   1.1      matt 	evcnt_attach_dynamic(&sc->sc_ev_missing_ticks, EVCNT_TYPE_MISC, NULL,
    172   1.1      matt 		device_xname(self), "missing interrupts");
    173   1.1      matt 
    174   1.9     marty 	for (int i = 0; i < 12; i++)
    175  1.10     marty 		fdtbus_intr_establish(faa->faa_phandle, i, 0, 0,
    176  1.10     marty 				      clockhandler, 0);
    177   1.9     marty }
    178   1.1      matt 
    179   1.1      matt static inline uint64_t
    180   1.1      matt mct_gettime(struct mct_softc *sc)
    181   1.1      matt {
    182   1.1      matt 	uint32_t lo, hi;
    183   1.1      matt 	do {
    184   1.1      matt 		hi = mct_read_global(sc, MCT_G_CNT_U);
    185   1.1      matt 		lo = mct_read_global(sc, MCT_G_CNT_L);
    186   1.1      matt 	} while (hi != mct_read_global(sc, MCT_G_CNT_U));
    187   1.1      matt 	return ((uint64_t) hi << 32) | lo;
    188   1.1      matt }
    189   1.1      matt 
    190  1.11  jmcneill static u_int
    191  1.11  jmcneill mct_get_timecount(struct timecounter *tc)
    192  1.11  jmcneill {
    193  1.11  jmcneill 	struct mct_softc *sc = tc->tc_priv;
    194  1.11  jmcneill 
    195  1.11  jmcneill 	return (u_int)mct_gettime(sc);
    196  1.11  jmcneill }
    197  1.11  jmcneill 
    198  1.11  jmcneill void
    199  1.11  jmcneill mct_delay(u_int us)
    200  1.11  jmcneill {
    201  1.11  jmcneill 	struct mct_softc *sc = &mct_sc;
    202  1.11  jmcneill 
    203  1.11  jmcneill 	if (sc->sc_bsh == (bus_space_handle_t)0)
    204  1.11  jmcneill 		return;
    205  1.11  jmcneill 
    206  1.11  jmcneill 	int64_t mct_ticks = ((uint64_t)us * sc->sc_freq) / 1000000;
    207  1.11  jmcneill 	uint64_t ticks_prev = mct_gettime(sc);
    208  1.11  jmcneill 	while (mct_ticks > 0) {
    209  1.11  jmcneill 		uint64_t ticks_cur = mct_gettime(sc);
    210  1.11  jmcneill 		mct_ticks -= (ticks_cur - ticks_prev);
    211  1.11  jmcneill 		ticks_prev = ticks_cur;
    212  1.11  jmcneill 	}
    213  1.11  jmcneill }
    214   1.1      matt 
    215   1.1      matt /* interrupt handler */
    216   1.1      matt static int
    217   1.1      matt clockhandler(void *arg)
    218   1.1      matt {
    219   1.1      matt 	struct clockframe * const cf = arg;
    220   1.1      matt 	struct mct_softc * const sc = &mct_sc;
    221   1.1      matt 	const uint64_t now = mct_gettime(sc);
    222   1.3   reinoud 	int64_t delta = now - sc->sc_lastintr;
    223   1.3   reinoud 	int64_t periods = delta / sc->sc_autoinc;
    224   1.3   reinoud 
    225   1.3   reinoud 	KASSERT(delta >= 0);
    226   1.3   reinoud 	KASSERT(periods >= 0);
    227   1.1      matt 
    228   1.1      matt 	/* ack the interrupt */
    229   1.1      matt 	mct_write_global(sc, MCT_G_INT_CSTAT, G_INT_CSTAT_CLEAR);
    230   1.1      matt 
    231   1.4   reinoud 	/* check if we missed clock interrupts */
    232   1.3   reinoud 	if (periods > 1)
    233   1.3   reinoud 		sc->sc_ev_missing_ticks.ev_count += periods - 1;
    234   1.1      matt 
    235   1.1      matt 	sc->sc_lastintr = now;
    236   1.1      matt 	hardclock(cf);
    237   1.1      matt 
    238   1.1      matt 	/* handled */
    239   1.1      matt 	return 1;
    240   1.1      matt }
    241   1.1      matt 
    242   1.1      matt void
    243   1.1      matt mct_init_cpu_clock(struct cpu_info *ci)
    244   1.1      matt {
    245   1.1      matt 	struct mct_softc * const sc = &mct_sc;
    246   1.1      matt 	uint64_t now = mct_gettime(sc);
    247   1.1      matt 	uint64_t then;
    248   1.1      matt 	uint32_t tcon;
    249   1.1      matt 
    250   1.1      matt 	KASSERT(ci == curcpu());
    251   1.1      matt 
    252   1.1      matt 	sc->sc_lastintr = now;
    253   1.1      matt 
    254   1.1      matt 	/* get current config */
    255   1.1      matt 	tcon = mct_read_global(sc, MCT_G_TCON);
    256   1.1      matt 
    257   1.1      matt 	/* setup auto increment */
    258   1.1      matt 	mct_write_global(sc, MCT_G_COMP0_ADD_INCR, sc->sc_autoinc);
    259   1.1      matt 
    260   1.1      matt 	/* (re)setup comparator */
    261   1.1      matt 	then = now + sc->sc_autoinc;
    262   1.1      matt 	mct_write_global(sc, MCT_G_COMP0_L, (uint32_t) then);
    263   1.1      matt 	mct_write_global(sc, MCT_G_COMP0_U, (uint32_t) (then >> 32));
    264   1.1      matt 	tcon |= G_TCON_COMP0_AUTOINC;
    265   1.1      matt 	tcon |= G_TCON_COMP0_ENABLE;
    266   1.1      matt 
    267   1.1      matt 	/* start timer */
    268   1.1      matt 	tcon |= G_TCON_START;
    269   1.1      matt 
    270   1.1      matt 	/* enable interrupt */
    271   1.1      matt 	mct_write_global(sc, MCT_G_INT_ENB, G_INT_ENB_ENABLE);
    272   1.1      matt 
    273   1.1      matt 	/* update config, starting the thing */
    274   1.1      matt 	mct_write_global(sc, MCT_G_TCON, tcon);
    275   1.1      matt }
    276   1.1      matt 
    277   1.1      matt void
    278   1.1      matt cpu_initclocks(void)
    279   1.1      matt {
    280   1.1      matt 	struct mct_softc * const sc = &mct_sc;
    281   1.1      matt 
    282   1.1      matt 	sc->sc_autoinc = sc->sc_freq / hz;
    283   1.1      matt 	mct_init_cpu_clock(curcpu());
    284   1.1      matt 
    285   1.1      matt 	mct_timecounter.tc_name = device_xname(sc->sc_dev);
    286   1.1      matt 	mct_timecounter.tc_frequency = sc->sc_freq;
    287   1.1      matt 
    288   1.1      matt 	tc_init(&mct_timecounter);
    289   1.1      matt 
    290   1.1      matt #if 0
    291   1.1      matt 	{
    292   1.1      matt 		uint64_t then, now;
    293   1.1      matt 
    294   1.1      matt 		printf("testing timer\n");
    295   1.1      matt 		for (int i = 0; i < 200; i++) {
    296   1.1      matt 			printf("cstat %d\n", mct_read_global(sc, MCT_G_INT_CSTAT));
    297   1.1      matt 			then = mct_get_timecount(&mct_timecounter);
    298   1.1      matt 			do {
    299   1.1      matt 				now = mct_get_timecount(&mct_timecounter);
    300   1.1      matt 			} while (now == then);
    301   1.1      matt 			printf("\tgot %"PRIu64"\n", now);
    302   1.1      matt 			for (int j = 0; j < 90000; j++);
    303   1.1      matt 		}
    304   1.1      matt 		printf("passed\n");
    305   1.1      matt 	}
    306   1.1      matt #endif
    307   1.1      matt }
    308   1.1      matt 
    309  1.11  jmcneill void
    310  1.11  jmcneill setstatclockrate(int newhz)
    311  1.11  jmcneill {
    312  1.11  jmcneill }
    313