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