xref: /netbsd/sys/arch/mipsco/obio/rambo.c (revision c4a72b64)
1 /*	$NetBSD: rambo.c,v 1.5 2002/10/02 05:38:11 thorpej 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/param.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/systm.h>
43 
44 #include <machine/cpu.h>
45 #include <machine/mainboard.h>
46 #include <machine/autoconf.h>
47 #include <machine/sysconf.h>
48 #include <machine/bus.h>
49 
50 #include <mipsco/obio/rambo.h>
51 
52 /*
53  * Timer & Interrupt manipulation routines for the Rambo Custom ASIC
54  */
55 
56 static int	rambo_match  __P((struct device *, struct cfdata *, void *));
57 static void	rambo_attach __P((struct device *, struct device *, void *));
58 static unsigned rambo_clkread __P((void));
59 void rambo_clkintr __P((struct clockframe *));
60 
61 struct rambo_softc {
62         struct device		dev;
63 	struct evcnt		sc_intrcnt;
64 	bus_space_tag_t		sc_bst;
65 	bus_space_handle_t	sc_bsh;
66 	u_int32_t		sc_tclast;
67 	u_int32_t		sc_hzticks;
68 };
69 
70 static struct rambo_softc *rambo;
71 
72 CFATTACH_DECL(rambo, sizeof(struct rambo_softc),
73     rambo_match, rambo_attach, NULL, NULL);
74 
75 static int
76 rambo_match(parent, cf, aux)
77 	struct device *parent;
78 	struct cfdata *cf;
79 	void *aux;
80 {
81 	return 1;
82 }
83 
84 static void
85 rambo_attach(parent, self, aux)
86 	struct device *parent, *self;
87 	void *aux;
88 {
89 	struct confargs *ca = aux;
90 	struct rambo_softc *sc = (void *)self;
91 
92 	sc->sc_bst = ca->ca_bustag;
93 
94 	if (bus_space_map(ca->ca_bustag, ca->ca_addr, 256,
95 			  BUS_SPACE_MAP_LINEAR,
96 			  &sc->sc_bsh) != 0) {
97 		printf(": cannot map registers\n");
98 		return;
99 	}
100 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
101 			     "timer", "intr");
102 
103 	/* Setup RAMBO Timer to generate timer interrupts */
104 	sc->sc_hzticks = HZ_TO_TICKS(hz);
105 
106 	sc->sc_tclast = 0;
107 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_TCOUNT, 0);
108 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_TBREAK, sc->sc_hzticks);
109 
110 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_CTLREG,
111 			  RB_PARITY_EN | RB_BUZZOFF | RB_CLR_IOERR);
112 
113 	printf(": parity enabled\n");
114 	rambo = sc;
115 	platform.clkread = rambo_clkread;
116 }
117 
118 void
119 rambo_clkintr(cf)
120 	struct clockframe *cf;
121 {
122 	register u_int32_t tbreak, tcount;
123 	register int delta;
124 
125 	rambo->sc_intrcnt.ev_count++;
126 	tbreak = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TBREAK);
127 	tcount = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh,	RB_TCOUNT);
128 	delta  = tcount - tbreak;
129 
130 	if (delta > (rambo->sc_hzticks>>1)) {
131 		/*
132 		 * Either tcount may overtake the updated tbreak value
133 		 * or we have missed several interrupt's
134 		 */
135 		int cycles = 10 * hz;
136 		while (cycles && tbreak < tcount) {
137 			hardclock(cf);
138 			rambo->sc_tclast = tbreak;
139 			tbreak += rambo->sc_hzticks;
140 			cycles--;
141 		}
142 		if (cycles == 0) { /* catchup failed - assume we are in sync */
143 			tcount = bus_space_read_4(rambo->sc_bst,
144 						  rambo->sc_bsh, RB_TCOUNT);
145 			rambo->sc_tclast = tbreak = tcount;
146 		}
147 	} else {
148 		hardclock(cf);
149 		rambo->sc_tclast = tbreak;
150 	}
151 
152 	tbreak += rambo->sc_hzticks;
153 
154 	bus_space_write_4(rambo->sc_bst, rambo->sc_bsh, RB_TBREAK, tbreak);
155 }
156 
157 /*
158  * Calculate the number of microseconds since the last clock tick
159  */
160 static unsigned
161 rambo_clkread()
162 {
163         register u_int32_t tcount;
164 
165 	tcount = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TCOUNT);
166 	return TICKS_TO_USECS(tcount - rambo->sc_tclast);
167 }
168