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