Home | History | Annotate | Line # | Download | only in gemini
obio_timer.c revision 1.4.8.2
      1  1.4.8.2  yamt /*	$NetBSD: obio_timer.c,v 1.4.8.2 2009/05/04 08:10:42 yamt Exp $	*/
      2  1.4.8.2  yamt 
      3  1.4.8.2  yamt /* adapted from:
      4  1.4.8.2  yamt  *	NetBSD: obio_mputmr.c,v 1.3 2008/08/27 11:03:10 matt Exp
      5  1.4.8.2  yamt  */
      6  1.4.8.2  yamt 
      7  1.4.8.2  yamt /*
      8  1.4.8.2  yamt  * Based on omap_mputmr.c
      9  1.4.8.2  yamt  * Based on i80321_timer.c and arch/arm/sa11x0/sa11x0_ost.c
     10  1.4.8.2  yamt  *
     11  1.4.8.2  yamt  * Copyright (c) 1997 Mark Brinicombe.
     12  1.4.8.2  yamt  * Copyright (c) 1997 Causality Limited.
     13  1.4.8.2  yamt  * All rights reserved.
     14  1.4.8.2  yamt  *
     15  1.4.8.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
     16  1.4.8.2  yamt  * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
     17  1.4.8.2  yamt  *
     18  1.4.8.2  yamt  * Redistribution and use in source and binary forms, with or without
     19  1.4.8.2  yamt  * modification, are permitted provided that the following conditions
     20  1.4.8.2  yamt  * are met:
     21  1.4.8.2  yamt  * 1. Redistributions of source code must retain the above copyright
     22  1.4.8.2  yamt  *    notice, this list of conditions and the following disclaimer.
     23  1.4.8.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     24  1.4.8.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     25  1.4.8.2  yamt  *    documentation and/or other materials provided with the distribution.
     26  1.4.8.2  yamt  * 3. All advertising materials mentioning features or use of this software
     27  1.4.8.2  yamt  *    must display the following acknowledgement:
     28  1.4.8.2  yamt  *	This product includes software developed by the NetBSD
     29  1.4.8.2  yamt  *	Foundation, Inc. and its contributors.
     30  1.4.8.2  yamt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     31  1.4.8.2  yamt  *    contributors may be used to endorse or promote products derived
     32  1.4.8.2  yamt  *    from this software without specific prior written permission.
     33  1.4.8.2  yamt  *
     34  1.4.8.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     35  1.4.8.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     36  1.4.8.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     37  1.4.8.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     38  1.4.8.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     39  1.4.8.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     40  1.4.8.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     41  1.4.8.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42  1.4.8.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     43  1.4.8.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     44  1.4.8.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     45  1.4.8.2  yamt  *
     46  1.4.8.2  yamt  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
     47  1.4.8.2  yamt  * All rights reserved.
     48  1.4.8.2  yamt  *
     49  1.4.8.2  yamt  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     50  1.4.8.2  yamt  *
     51  1.4.8.2  yamt  * Redistribution and use in source and binary forms, with or without
     52  1.4.8.2  yamt  * modification, are permitted provided that the following conditions
     53  1.4.8.2  yamt  * are met:
     54  1.4.8.2  yamt  * 1. Redistributions of source code must retain the above copyright
     55  1.4.8.2  yamt  *    notice, this list of conditions and the following disclaimer.
     56  1.4.8.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     57  1.4.8.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     58  1.4.8.2  yamt  *    documentation and/or other materials provided with the distribution.
     59  1.4.8.2  yamt  * 3. All advertising materials mentioning features or use of this software
     60  1.4.8.2  yamt  *    must display the following acknowledgement:
     61  1.4.8.2  yamt  *	This product includes software developed for the NetBSD Project by
     62  1.4.8.2  yamt  *	Wasabi Systems, Inc.
     63  1.4.8.2  yamt  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     64  1.4.8.2  yamt  *    or promote products derived from this software without specific prior
     65  1.4.8.2  yamt  *    written permission.
     66  1.4.8.2  yamt  *
     67  1.4.8.2  yamt  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     68  1.4.8.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     69  1.4.8.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     70  1.4.8.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     71  1.4.8.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     72  1.4.8.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     73  1.4.8.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     74  1.4.8.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     75  1.4.8.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     76  1.4.8.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     77  1.4.8.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     78  1.4.8.2  yamt  *
     79  1.4.8.2  yamt  * Copyright (c) 2007 Microsoft
     80  1.4.8.2  yamt  * All rights reserved.
     81  1.4.8.2  yamt  *
     82  1.4.8.2  yamt  * Redistribution and use in source and binary forms, with or without
     83  1.4.8.2  yamt  * modification, are permitted provided that the following conditions
     84  1.4.8.2  yamt  * are met:
     85  1.4.8.2  yamt  * 1. Redistributions of source code must retain the above copyright
     86  1.4.8.2  yamt  *    notice, this list of conditions and the following disclaimer.
     87  1.4.8.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     88  1.4.8.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     89  1.4.8.2  yamt  *    documentation and/or other materials provided with the distribution.
     90  1.4.8.2  yamt  * 3. All advertising materials mentioning features or use of this software
     91  1.4.8.2  yamt  *    must display the following acknowledgement:
     92  1.4.8.2  yamt  *	This product includes software developed by Microsoft
     93  1.4.8.2  yamt  *
     94  1.4.8.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     95  1.4.8.2  yamt  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     96  1.4.8.2  yamt  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     97  1.4.8.2  yamt  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTERS BE LIABLE FOR ANY DIRECT,
     98  1.4.8.2  yamt  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     99  1.4.8.2  yamt  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    100  1.4.8.2  yamt  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    101  1.4.8.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    102  1.4.8.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    103  1.4.8.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    104  1.4.8.2  yamt  * SUCH DAMAGE.
    105  1.4.8.2  yamt  */
    106  1.4.8.2  yamt 
    107  1.4.8.2  yamt #include <sys/cdefs.h>
    108  1.4.8.2  yamt __KERNEL_RCSID(0, "$NetBSD: obio_timer.c,v 1.4.8.2 2009/05/04 08:10:42 yamt Exp $");
    109  1.4.8.2  yamt 
    110  1.4.8.2  yamt #include "opt_cpuoptions.h"
    111  1.4.8.2  yamt #include "opt_gemini.h"
    112  1.4.8.2  yamt #include "locators.h"
    113  1.4.8.2  yamt 
    114  1.4.8.2  yamt #include <sys/types.h>
    115  1.4.8.2  yamt #include <sys/param.h>
    116  1.4.8.2  yamt #include <sys/systm.h>
    117  1.4.8.2  yamt #include <sys/kernel.h>
    118  1.4.8.2  yamt #include <sys/time.h>
    119  1.4.8.2  yamt #include <sys/device.h>
    120  1.4.8.2  yamt 
    121  1.4.8.2  yamt #include <dev/clock_subr.h>
    122  1.4.8.2  yamt 
    123  1.4.8.2  yamt #include <machine/bus.h>
    124  1.4.8.2  yamt #include <machine/intr.h>
    125  1.4.8.2  yamt 
    126  1.4.8.2  yamt #include <arm/gemini/gemini_reg.h>
    127  1.4.8.2  yamt #include <arm/gemini/gemini_obiovar.h>
    128  1.4.8.2  yamt #include <arm/gemini/gemini_timervar.h>
    129  1.4.8.2  yamt 
    130  1.4.8.2  yamt #if STATHZ != HZ
    131  1.4.8.2  yamt # error system clock HZ and stat clock STATHZ must be same
    132  1.4.8.2  yamt #endif
    133  1.4.8.2  yamt 
    134  1.4.8.2  yamt 
    135  1.4.8.2  yamt #ifndef GEMINI_TIMER_CLOCK_FREQ
    136  1.4.8.2  yamt # error Specify the timer frequency in Hz with option GEMINI_TIMER_CLOCK_FREQ
    137  1.4.8.2  yamt #endif
    138  1.4.8.2  yamt 
    139  1.4.8.2  yamt static int	obiotimer_match(device_t, struct cfdata *, void *);
    140  1.4.8.2  yamt static void	obiotimer_attach(device_t, device_t, void *);
    141  1.4.8.2  yamt 
    142  1.4.8.2  yamt struct geminitmr_softc xsc;
    143  1.4.8.2  yamt 
    144  1.4.8.2  yamt 
    145  1.4.8.2  yamt 
    146  1.4.8.2  yamt typedef struct {
    147  1.4.8.2  yamt 	uint       timerno;
    148  1.4.8.2  yamt 	bus_addr_t addr;
    149  1.4.8.2  yamt 	uint       intr;
    150  1.4.8.2  yamt } obiotimer_instance_t;
    151  1.4.8.2  yamt 
    152  1.4.8.2  yamt /* XXX
    153  1.4.8.2  yamt  * this table can be used to match the GP Timers
    154  1.4.8.2  yamt  * until we use config(8) locators to distinguish between
    155  1.4.8.2  yamt  * gemini "sub-timers".
    156  1.4.8.2  yamt  */
    157  1.4.8.2  yamt #define GPT_ENTRY(n, i) { \
    158  1.4.8.2  yamt 		.timerno = (n), \
    159  1.4.8.2  yamt 		.addr = GEMINI_TIMER_BASE, \
    160  1.4.8.2  yamt 		.intr = i, \
    161  1.4.8.2  yamt 	}
    162  1.4.8.2  yamt static const obiotimer_instance_t obiotimer_instance_tab[] = {
    163  1.4.8.2  yamt 	GPT_ENTRY(1, 14),
    164  1.4.8.2  yamt 	GPT_ENTRY(2, 15),
    165  1.4.8.2  yamt 	GPT_ENTRY(3, 16),
    166  1.4.8.2  yamt };
    167  1.4.8.2  yamt #undef	GPT_ENTRY
    168  1.4.8.2  yamt #define GPTIMER_INSTANCE_CNT	__arraycount(obiotimer_instance_tab)
    169  1.4.8.2  yamt 
    170  1.4.8.2  yamt static const obiotimer_instance_t *
    171  1.4.8.2  yamt 		obiotimer_lookup(struct obio_attach_args *);
    172  1.4.8.2  yamt static void	obiotimer_enable(struct geminitmr_softc *,
    173  1.4.8.2  yamt 			struct obio_attach_args *,
    174  1.4.8.2  yamt 			const obiotimer_instance_t *);
    175  1.4.8.2  yamt 
    176  1.4.8.2  yamt static int	obiotimer_match(device_t, struct cfdata *, void *);
    177  1.4.8.2  yamt static void	obiotimer_attach(device_t, device_t, void *);
    178  1.4.8.2  yamt 
    179  1.4.8.2  yamt 
    180  1.4.8.2  yamt CFATTACH_DECL_NEW(obiotimer, sizeof(struct geminitmr_softc),
    181  1.4.8.2  yamt     obiotimer_match, obiotimer_attach, NULL, NULL);
    182  1.4.8.2  yamt 
    183  1.4.8.2  yamt 
    184  1.4.8.2  yamt static int
    185  1.4.8.2  yamt obiotimer_match(device_t parent, struct cfdata *match, void *aux)
    186  1.4.8.2  yamt {
    187  1.4.8.2  yamt 	struct obio_attach_args *obio = aux;
    188  1.4.8.2  yamt 
    189  1.4.8.2  yamt 	if ((obio->obio_addr == OBIOCF_ADDR_DEFAULT)
    190  1.4.8.2  yamt 	||  (obio->obio_intr == OBIOCF_INTR_DEFAULT))
    191  1.4.8.2  yamt 		panic("geminitmr must have addr and intr specified in config.");
    192  1.4.8.2  yamt 
    193  1.4.8.2  yamt 	if (obiotimer_lookup(obio) == NULL)
    194  1.4.8.2  yamt 		return 0;
    195  1.4.8.2  yamt 
    196  1.4.8.2  yamt 	return 1;
    197  1.4.8.2  yamt }
    198  1.4.8.2  yamt 
    199  1.4.8.2  yamt void
    200  1.4.8.2  yamt obiotimer_attach(device_t parent, device_t self, void *aux)
    201  1.4.8.2  yamt {
    202  1.4.8.2  yamt 	struct geminitmr_softc *sc = device_private(self);
    203  1.4.8.2  yamt 	struct obio_attach_args *obio = aux;
    204  1.4.8.2  yamt 	const obiotimer_instance_t *ip;
    205  1.4.8.2  yamt #ifndef GEMINI_SLAVE
    206  1.4.8.2  yamt 	static int once=1;
    207  1.4.8.2  yamt #endif
    208  1.4.8.2  yamt 
    209  1.4.8.2  yamt 	ip = obiotimer_lookup(obio);
    210  1.4.8.2  yamt 	if (ip == NULL)
    211  1.4.8.2  yamt 		panic("%s: bad lookup", device_xname(self));
    212  1.4.8.2  yamt 			/* should not fail since we already matched */
    213  1.4.8.2  yamt 
    214  1.4.8.2  yamt 	sc->sc_timerno = ip->timerno;
    215  1.4.8.2  yamt 	sc->sc_iot = obio->obio_iot;
    216  1.4.8.2  yamt 	sc->sc_intr = obio->obio_intr;
    217  1.4.8.2  yamt 	sc->sc_addr = obio->obio_addr;
    218  1.4.8.2  yamt 	sc->sc_size = (obio->obio_size == OBIOCF_SIZE_DEFAULT)
    219  1.4.8.2  yamt 		? (GEMINI_TIMER_INTRMASK + 4)
    220  1.4.8.2  yamt 		: obio->obio_size;
    221  1.4.8.2  yamt 
    222  1.4.8.2  yamt 	if (bus_space_map(sc->sc_iot, sc->sc_addr, sc->sc_size, 0, &sc->sc_ioh))
    223  1.4.8.2  yamt 		panic("%s: Cannot map registers", device_xname(self));
    224  1.4.8.2  yamt 
    225  1.4.8.2  yamt 	obiotimer_enable(sc, obio, obiotimer_lookup(obio));
    226  1.4.8.2  yamt 	aprint_normal("\n");
    227  1.4.8.2  yamt 	aprint_naive("\n");
    228  1.4.8.2  yamt 
    229  1.4.8.2  yamt #ifndef GEMINI_SLAVE
    230  1.4.8.2  yamt 	if (once) {
    231  1.4.8.2  yamt 		once = 0;
    232  1.4.8.2  yamt 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    233  1.4.8.2  yamt 			GEMINI_TIMER_TMCR, 0);
    234  1.4.8.2  yamt 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    235  1.4.8.2  yamt 			GEMINI_TIMER_INTRMASK, (uint32_t)~TIMER_INTRMASK_Resv);
    236  1.4.8.2  yamt 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    237  1.4.8.2  yamt 			GEMINI_TIMER_INTRSTATE, 0);
    238  1.4.8.2  yamt 	}
    239  1.4.8.2  yamt #endif
    240  1.4.8.2  yamt 
    241  1.4.8.2  yamt 	switch (sc->sc_timerno) {
    242  1.4.8.2  yamt 	case 1:
    243  1.4.8.2  yamt #ifndef GEMINI_SLAVE
    244  1.4.8.2  yamt 		/*
    245  1.4.8.2  yamt 		 * timer #1 is the combined system clock and stat clock
    246  1.4.8.2  yamt 		 * for the Master or Single Gemini CPU
    247  1.4.8.2  yamt 		 * it gets started later
    248  1.4.8.2  yamt 		 */
    249  1.4.8.2  yamt 		profhz = stathz = hz;
    250  1.4.8.2  yamt 		stat_sc = clock_sc = sc;
    251  1.4.8.2  yamt #endif
    252  1.4.8.2  yamt 		break;
    253  1.4.8.2  yamt 	case 2:
    254  1.4.8.2  yamt #ifdef GEMINI_SLAVE
    255  1.4.8.2  yamt 		/*
    256  1.4.8.2  yamt 		 * timer #2 is the combined system clock and stat clock
    257  1.4.8.2  yamt 		 * for the Slave Gemini CPU
    258  1.4.8.2  yamt 		 * it gets started later
    259  1.4.8.2  yamt 		 */
    260  1.4.8.2  yamt 		profhz = stathz = hz;
    261  1.4.8.2  yamt 		stat_sc = clock_sc = sc;
    262  1.4.8.2  yamt #endif
    263  1.4.8.2  yamt 		break;
    264  1.4.8.2  yamt 	case 3:
    265  1.4.8.2  yamt 		/*
    266  1.4.8.2  yamt 		 * Timer #3 is used for microtime reference clock and delay()
    267  1.4.8.2  yamt 		 * autoloading, non-interrupting, just wraps around
    268  1.4.8.2  yamt 		 * we start it now to make delay() available
    269  1.4.8.2  yamt 		 */
    270  1.4.8.2  yamt 		ref_sc = sc;
    271  1.4.8.2  yamt #ifndef GEMINI_SLAVE
    272  1.4.8.2  yamt 		gemini_microtime_init();
    273  1.4.8.2  yamt #endif
    274  1.4.8.2  yamt 		break;
    275  1.4.8.2  yamt 	default:
    276  1.4.8.2  yamt 		panic("bad gemini timer number %d\n", sc->sc_timerno);
    277  1.4.8.2  yamt 		break;
    278  1.4.8.2  yamt 	}
    279  1.4.8.2  yamt }
    280  1.4.8.2  yamt 
    281  1.4.8.2  yamt static const obiotimer_instance_t *
    282  1.4.8.2  yamt obiotimer_lookup(struct obio_attach_args *obio)
    283  1.4.8.2  yamt {
    284  1.4.8.2  yamt 	const obiotimer_instance_t *ip;
    285  1.4.8.2  yamt 	uint i;
    286  1.4.8.2  yamt 
    287  1.4.8.2  yamt 	for (i = 0, ip = obiotimer_instance_tab;
    288  1.4.8.2  yamt 	     i < GPTIMER_INSTANCE_CNT; i++, ip++) {
    289  1.4.8.2  yamt 		if (ip->addr == obio->obio_addr && ip->intr == obio->obio_intr)
    290  1.4.8.2  yamt 			return ip;
    291  1.4.8.2  yamt 	}
    292  1.4.8.2  yamt 
    293  1.4.8.2  yamt 	return NULL;
    294  1.4.8.2  yamt }
    295  1.4.8.2  yamt 
    296  1.4.8.2  yamt void
    297  1.4.8.2  yamt obiotimer_enable(
    298  1.4.8.2  yamt 	struct geminitmr_softc *sc,
    299  1.4.8.2  yamt 	struct obio_attach_args *obio,
    300  1.4.8.2  yamt 	const obiotimer_instance_t *ip)
    301  1.4.8.2  yamt {
    302  1.4.8.2  yamt 	/* nothing to do */
    303  1.4.8.2  yamt }
    304