Home | History | Annotate | Line # | Download | only in obio
rambo.c revision 1.9.14.1
      1  1.9.14.1      jym /*	$NetBSD: rambo.c,v 1.9.14.1 2009/05/13 17:18:04 jym Exp $	*/
      2       1.1      wdk 
      3       1.1      wdk /*
      4       1.1      wdk  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5       1.1      wdk  * All rights reserved.
      6       1.1      wdk  *
      7       1.1      wdk  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1      wdk  * by Wayne Knowles
      9       1.1      wdk  *
     10       1.1      wdk  * Redistribution and use in source and binary forms, with or without
     11       1.1      wdk  * modification, are permitted provided that the following conditions
     12       1.1      wdk  * are met:
     13       1.1      wdk  * 1. Redistributions of source code must retain the above copyright
     14       1.1      wdk  *    notice, this list of conditions and the following disclaimer.
     15       1.1      wdk  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      wdk  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      wdk  *    documentation and/or other materials provided with the distribution.
     18       1.1      wdk  *
     19       1.1      wdk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1      wdk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1      wdk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1      wdk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1      wdk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1      wdk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1      wdk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1      wdk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1      wdk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1      wdk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1      wdk  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1      wdk  */
     31       1.6    lukem 
     32       1.6    lukem #include <sys/cdefs.h>
     33  1.9.14.1      jym __KERNEL_RCSID(0, "$NetBSD: rambo.c,v 1.9.14.1 2009/05/13 17:18:04 jym Exp $");
     34       1.1      wdk 
     35       1.1      wdk #include <sys/param.h>
     36       1.1      wdk #include <sys/kernel.h>
     37       1.1      wdk #include <sys/device.h>
     38       1.1      wdk #include <sys/systm.h>
     39       1.8  gdamore #include <sys/timetc.h>
     40       1.1      wdk 
     41       1.1      wdk #include <machine/cpu.h>
     42       1.1      wdk #include <machine/mainboard.h>
     43       1.1      wdk #include <machine/autoconf.h>
     44       1.1      wdk #include <machine/sysconf.h>
     45       1.1      wdk #include <machine/bus.h>
     46       1.1      wdk 
     47       1.1      wdk #include <mipsco/obio/rambo.h>
     48       1.1      wdk 
     49       1.1      wdk /*
     50       1.1      wdk  * Timer & Interrupt manipulation routines for the Rambo Custom ASIC
     51       1.1      wdk  */
     52       1.1      wdk 
     53  1.9.14.1      jym static int	rambo_match(struct device *, struct cfdata *, void *);
     54  1.9.14.1      jym static void	rambo_attach(struct device *, struct device *, void *);
     55       1.8  gdamore static unsigned rambo_get_timecount(struct timecounter *);
     56  1.9.14.1      jym void rambo_clkintr(struct clockframe *);
     57       1.8  gdamore static void rambo_tc_init(void);
     58       1.1      wdk 
     59       1.1      wdk struct rambo_softc {
     60       1.1      wdk         struct device		dev;
     61       1.1      wdk 	struct evcnt		sc_intrcnt;
     62       1.1      wdk 	bus_space_tag_t		sc_bst;
     63       1.1      wdk 	bus_space_handle_t	sc_bsh;
     64       1.1      wdk 	u_int32_t		sc_tclast;
     65       1.1      wdk 	u_int32_t		sc_hzticks;
     66       1.1      wdk };
     67       1.1      wdk 
     68       1.1      wdk static struct rambo_softc *rambo;
     69       1.1      wdk 
     70       1.5  thorpej CFATTACH_DECL(rambo, sizeof(struct rambo_softc),
     71       1.5  thorpej     rambo_match, rambo_attach, NULL, NULL);
     72       1.1      wdk 
     73       1.1      wdk static int
     74  1.9.14.1      jym rambo_match(struct device *parent, struct cfdata *cf, void *aux)
     75       1.1      wdk {
     76       1.1      wdk 	return 1;
     77       1.1      wdk }
     78       1.1      wdk 
     79       1.1      wdk static void
     80  1.9.14.1      jym rambo_attach(struct device *parent, struct device *self, void *aux)
     81       1.1      wdk {
     82       1.1      wdk 	struct confargs *ca = aux;
     83       1.1      wdk 	struct rambo_softc *sc = (void *)self;
     84       1.1      wdk 
     85       1.1      wdk 	sc->sc_bst = ca->ca_bustag;
     86       1.1      wdk 
     87       1.1      wdk 	if (bus_space_map(ca->ca_bustag, ca->ca_addr, 256,
     88       1.1      wdk 			  BUS_SPACE_MAP_LINEAR,
     89       1.1      wdk 			  &sc->sc_bsh) != 0) {
     90       1.1      wdk 		printf(": cannot map registers\n");
     91       1.1      wdk 		return;
     92       1.1      wdk 	}
     93       1.1      wdk 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
     94       1.1      wdk 			     "timer", "intr");
     95       1.1      wdk 
     96       1.1      wdk 	/* Setup RAMBO Timer to generate timer interrupts */
     97       1.1      wdk 	sc->sc_hzticks = HZ_TO_TICKS(hz);
     98       1.1      wdk 
     99       1.3      wdk 	sc->sc_tclast = 0;
    100       1.1      wdk 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_TCOUNT, 0);
    101       1.1      wdk 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_TBREAK, sc->sc_hzticks);
    102       1.1      wdk 
    103       1.1      wdk 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_CTLREG,
    104       1.1      wdk 			  RB_PARITY_EN | RB_BUZZOFF | RB_CLR_IOERR);
    105       1.1      wdk 
    106       1.1      wdk 	printf(": parity enabled\n");
    107       1.1      wdk 	rambo = sc;
    108       1.8  gdamore 	platform.clkinit = rambo_tc_init;
    109       1.1      wdk }
    110       1.1      wdk 
    111       1.1      wdk void
    112  1.9.14.1      jym rambo_clkintr(struct clockframe *cf)
    113       1.1      wdk {
    114       1.3      wdk 	register u_int32_t tbreak, tcount;
    115       1.3      wdk 	register int delta;
    116       1.3      wdk 
    117       1.2      wdk 	rambo->sc_intrcnt.ev_count++;
    118       1.2      wdk 	tbreak = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TBREAK);
    119       1.3      wdk 	tcount = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh,	RB_TCOUNT);
    120       1.3      wdk 	delta  = tcount - tbreak;
    121       1.3      wdk 
    122       1.3      wdk 	if (delta > (rambo->sc_hzticks>>1)) {
    123       1.3      wdk 		/*
    124       1.3      wdk 		 * Either tcount may overtake the updated tbreak value
    125       1.3      wdk 		 * or we have missed several interrupt's
    126       1.3      wdk 		 */
    127       1.3      wdk 		int cycles = 10 * hz;
    128       1.3      wdk 		while (cycles && tbreak < tcount) {
    129       1.3      wdk 			hardclock(cf);
    130       1.3      wdk 			rambo->sc_tclast = tbreak;
    131       1.3      wdk 			tbreak += rambo->sc_hzticks;
    132       1.3      wdk 			cycles--;
    133       1.3      wdk 		}
    134       1.3      wdk 		if (cycles == 0) { /* catchup failed - assume we are in sync */
    135       1.3      wdk 			tcount = bus_space_read_4(rambo->sc_bst,
    136       1.3      wdk 						  rambo->sc_bsh, RB_TCOUNT);
    137       1.3      wdk 			rambo->sc_tclast = tbreak = tcount;
    138       1.3      wdk 		}
    139       1.3      wdk 	} else {
    140       1.2      wdk 		hardclock(cf);
    141       1.3      wdk 		rambo->sc_tclast = tbreak;
    142       1.2      wdk 	}
    143       1.1      wdk 
    144       1.3      wdk 	tbreak += rambo->sc_hzticks;
    145       1.3      wdk 
    146       1.2      wdk 	bus_space_write_4(rambo->sc_bst, rambo->sc_bsh, RB_TBREAK, tbreak);
    147       1.1      wdk }
    148       1.1      wdk 
    149       1.1      wdk /*
    150       1.1      wdk  * Calculate the number of microseconds since the last clock tick
    151       1.1      wdk  */
    152       1.3      wdk static unsigned
    153       1.8  gdamore rambo_get_timecount(struct timecounter *tc)
    154       1.1      wdk {
    155       1.8  gdamore 
    156       1.8  gdamore 	return (bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TCOUNT));
    157       1.8  gdamore }
    158       1.8  gdamore 
    159       1.8  gdamore static void
    160       1.8  gdamore rambo_tc_init(void)
    161       1.8  gdamore {
    162       1.8  gdamore 	static struct timecounter tc = {
    163       1.8  gdamore 		.tc_get_timecount = rambo_get_timecount,
    164       1.8  gdamore 		.tc_frequency = RB_FREQUENCY,
    165       1.8  gdamore 		.tc_quality = 100,
    166       1.8  gdamore 		.tc_name = "rambo_tcount",
    167       1.8  gdamore 		.tc_counter_mask = ~0
    168       1.8  gdamore 	};
    169       1.8  gdamore 
    170       1.8  gdamore 	tc_init(&tc);
    171       1.1      wdk }
    172