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