xref: /netbsd/sys/arch/mipsco/obio/rambo.c (revision bf9ec67e)
1 /*	$NetBSD: rambo.c,v 1.3 2000/09/06 07:52:47 wdk 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 struct cfattach rambo_ca = {
73 	sizeof(struct rambo_softc), rambo_match, rambo_attach
74 };
75 
76 static int
77 rambo_match(parent, cf, aux)
78 	struct device *parent;
79 	struct cfdata *cf;
80 	void *aux;
81 {
82 	return 1;
83 }
84 
85 static void
86 rambo_attach(parent, self, aux)
87 	struct device *parent, *self;
88 	void *aux;
89 {
90 	struct confargs *ca = aux;
91 	struct rambo_softc *sc = (void *)self;
92 
93 	sc->sc_bst = ca->ca_bustag;
94 
95 	if (bus_space_map(ca->ca_bustag, ca->ca_addr, 256,
96 			  BUS_SPACE_MAP_LINEAR,
97 			  &sc->sc_bsh) != 0) {
98 		printf(": cannot map registers\n");
99 		return;
100 	}
101 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
102 			     "timer", "intr");
103 
104 	/* Setup RAMBO Timer to generate timer interrupts */
105 	sc->sc_hzticks = HZ_TO_TICKS(hz);
106 
107 	sc->sc_tclast = 0;
108 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_TCOUNT, 0);
109 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_TBREAK, sc->sc_hzticks);
110 
111 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_CTLREG,
112 			  RB_PARITY_EN | RB_BUZZOFF | RB_CLR_IOERR);
113 
114 	printf(": parity enabled\n");
115 	rambo = sc;
116 	platform.clkread = rambo_clkread;
117 }
118 
119 void
120 rambo_clkintr(cf)
121 	struct clockframe *cf;
122 {
123 	register u_int32_t tbreak, tcount;
124 	register int delta;
125 
126 	rambo->sc_intrcnt.ev_count++;
127 	tbreak = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TBREAK);
128 	tcount = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh,	RB_TCOUNT);
129 	delta  = tcount - tbreak;
130 
131 	if (delta > (rambo->sc_hzticks>>1)) {
132 		/*
133 		 * Either tcount may overtake the updated tbreak value
134 		 * or we have missed several interrupt's
135 		 */
136 		int cycles = 10 * hz;
137 		while (cycles && tbreak < tcount) {
138 			hardclock(cf);
139 			rambo->sc_tclast = tbreak;
140 			tbreak += rambo->sc_hzticks;
141 			cycles--;
142 		}
143 		if (cycles == 0) { /* catchup failed - assume we are in sync */
144 			tcount = bus_space_read_4(rambo->sc_bst,
145 						  rambo->sc_bsh, RB_TCOUNT);
146 			rambo->sc_tclast = tbreak = tcount;
147 		}
148 	} else {
149 		hardclock(cf);
150 		rambo->sc_tclast = tbreak;
151 	}
152 
153 	tbreak += rambo->sc_hzticks;
154 
155 	bus_space_write_4(rambo->sc_bst, rambo->sc_bsh, RB_TBREAK, tbreak);
156 }
157 
158 /*
159  * Calculate the number of microseconds since the last clock tick
160  */
161 static unsigned
162 rambo_clkread()
163 {
164         register u_int32_t tcount;
165 
166 	tcount = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TCOUNT);
167 	return TICKS_TO_USECS(tcount - rambo->sc_tclast);
168 }
169