am79c930.c revision 1.4 1 /* $NetBSD: am79c930.c,v 1.4 2000/03/22 11:22:22 onoe Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Bill Sommerfeld
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 /*
40 * Am79c930 chip driver.
41 *
42 * This is used by the awi driver to use the shared
43 * memory attached to the 79c930 to communicate with the firmware running
44 * in the 930's on-board 80188 core.
45 *
46 * The 79c930 can be mapped into just I/O space, or also have a
47 * memory mapping; the mapping must be set up by the bus front-end
48 * before am79c930_init is called.
49 */
50
51 /*
52 * operations:
53 *
54 * read_8, read_16, read_32, read_64, read_bytes
55 * write_8, write_16, write_32, write_64, write_bytes
56 * (two versions, depending on whether memory-space or i/o space is in use).
57 *
58 * interrupt E.C.
59 * start isr
60 * end isr
61 */
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #ifndef __FreeBSD__
67 #include <sys/device.h>
68 #endif
69
70 #include <machine/cpu.h>
71 #ifdef __FreeBSD__
72 #include <machine/bus_pio.h>
73 #include <machine/bus_memio.h>
74 #endif
75 #include <machine/bus.h>
76 #ifdef __NetBSD__
77 #include <machine/intr.h>
78 #endif
79
80 #ifdef __NetBSD__
81 #include <dev/ic/am79c930reg.h>
82 #include <dev/ic/am79c930var.h>
83 #endif
84 #ifdef __FreeBSD__
85 #include <dev/awi/am79c930reg.h>
86 #include <dev/awi/am79c930var.h>
87 #endif
88
89 #define AM930_DELAY(x) /*nothing*/
90
91 void am79c930_regdump __P((struct am79c930_softc *sc));
92
93 static void io_write_1 __P((struct am79c930_softc *, u_int32_t, u_int8_t));
94 static void io_write_2 __P((struct am79c930_softc *, u_int32_t, u_int16_t));
95 static void io_write_4 __P((struct am79c930_softc *, u_int32_t, u_int32_t));
96 static void io_write_bytes __P((struct am79c930_softc *, u_int32_t, u_int8_t *, size_t));
97
98 static u_int8_t io_read_1 __P((struct am79c930_softc *, u_int32_t));
99 static u_int16_t io_read_2 __P((struct am79c930_softc *, u_int32_t));
100 static u_int32_t io_read_4 __P((struct am79c930_softc *, u_int32_t));
101 static void io_read_bytes __P((struct am79c930_softc *, u_int32_t, u_int8_t *, size_t));
102
103 static void mem_write_1 __P((struct am79c930_softc *, u_int32_t, u_int8_t));
104 static void mem_write_2 __P((struct am79c930_softc *, u_int32_t, u_int16_t));
105 static void mem_write_4 __P((struct am79c930_softc *, u_int32_t, u_int32_t));
106 static void mem_write_bytes __P((struct am79c930_softc *, u_int32_t, u_int8_t *, size_t));
107
108 static u_int8_t mem_read_1 __P((struct am79c930_softc *, u_int32_t));
109 static u_int16_t mem_read_2 __P((struct am79c930_softc *, u_int32_t));
110 static u_int32_t mem_read_4 __P((struct am79c930_softc *, u_int32_t));
111 static void mem_read_bytes __P((struct am79c930_softc *, u_int32_t, u_int8_t *, size_t));
112
113 static struct am79c930_ops iospace_ops = {
114 io_write_1,
115 io_write_2,
116 io_write_4,
117 io_write_bytes,
118 io_read_1,
119 io_read_2,
120 io_read_4,
121 io_read_bytes
122 };
123
124 struct am79c930_ops memspace_ops = {
125 mem_write_1,
126 mem_write_2,
127 mem_write_4,
128 mem_write_bytes,
129 mem_read_1,
130 mem_read_2,
131 mem_read_4,
132 mem_read_bytes
133 };
134
135 static void io_write_1 (sc, off, val)
136 struct am79c930_softc *sc;
137 u_int32_t off;
138 u_int8_t val;
139 {
140 AM930_DELAY(1);
141 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
142 ((off>>8)& 0x7f));
143 AM930_DELAY(1);
144 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
145 AM930_DELAY(1);
146 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA, val);
147 AM930_DELAY(1);
148 }
149
150 static void io_write_2 (sc, off, val)
151 struct am79c930_softc *sc;
152 u_int32_t off;
153 u_int16_t val;
154 {
155 AM930_DELAY(1);
156 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
157 ((off>>8)& 0x7f));
158 AM930_DELAY(1);
159 bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_LMA_LO, (off&0xff));
160 AM930_DELAY(1);
161 bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA, val & 0xff);
162 AM930_DELAY(1);
163 bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA, (val>>8)&0xff);
164 AM930_DELAY(1);
165 }
166
167 static void io_write_4 (sc, off, val)
168 struct am79c930_softc *sc;
169 u_int32_t off;
170 u_int32_t val;
171 {
172 AM930_DELAY(1);
173 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
174 ((off>>8)& 0x7f));
175 AM930_DELAY(1);
176 bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_LMA_LO, (off&0xff));
177 AM930_DELAY(1);
178 bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,val & 0xff);
179 AM930_DELAY(1);
180 bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,(val>>8)&0xff);
181 AM930_DELAY(1);
182 bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,(val>>16)&0xff);
183 AM930_DELAY(1);
184 bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,(val>>24)&0xff);
185 AM930_DELAY(1);
186 }
187
188 static void io_write_bytes (sc, off, ptr, len)
189 struct am79c930_softc *sc;
190 u_int32_t off;
191 u_int8_t *ptr;
192 size_t len;
193 {
194 int i;
195
196 AM930_DELAY(1);
197 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
198 ((off>>8)& 0x7f));
199 AM930_DELAY(1);
200 bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_LMA_LO, (off&0xff));
201 AM930_DELAY(1);
202 for (i=0; i<len; i++)
203 bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,ptr[i]);
204 }
205
206 static u_int8_t io_read_1 (sc, off)
207 struct am79c930_softc *sc;
208 u_int32_t off;
209 {
210 u_int8_t val;
211
212 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
213 ((off>>8)& 0x7f));
214 AM930_DELAY(1);
215 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
216 AM930_DELAY(1);
217 val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA);
218 AM930_DELAY(1);
219 return val;
220 }
221
222 static u_int16_t io_read_2 (sc, off)
223 struct am79c930_softc *sc;
224 u_int32_t off;
225 {
226 u_int16_t val;
227
228 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
229 ((off>>8)& 0x7f));
230 AM930_DELAY(1);
231 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
232 AM930_DELAY(1);
233 val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA);
234 AM930_DELAY(1);
235 val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 8;
236 AM930_DELAY(1);
237 return val;
238 }
239
240 static u_int32_t io_read_4 (sc, off)
241 struct am79c930_softc *sc;
242 u_int32_t off;
243 {
244 u_int32_t val;
245
246 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
247 ((off>>8)& 0x7f));
248 AM930_DELAY(1);
249 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
250 AM930_DELAY(1);
251 val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA);
252 AM930_DELAY(1);
253 val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 8;
254 AM930_DELAY(1);
255 val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 16;
256 AM930_DELAY(1);
257 val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 24;
258 AM930_DELAY(1);
259 return val;
260 }
261
262 static void io_read_bytes (sc, off, ptr, len)
263 struct am79c930_softc *sc;
264 u_int32_t off;
265 u_int8_t *ptr;
266 size_t len;
267 {
268 int i;
269
270 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
271 ((off>>8)& 0x7f));
272 AM930_DELAY(1);
273 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
274 AM930_DELAY(1);
275 for (i=0; i<len; i++)
276 ptr[i] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
277 AM79C930_IODPA);
278 }
279
280 static void mem_write_1 (sc, off, val)
281 struct am79c930_softc *sc;
282 u_int32_t off;
283 u_int8_t val;
284 {
285 bus_space_write_1(sc->sc_memt, sc->sc_memh, off, val);
286 }
287
288 static void mem_write_2 (sc, off, val)
289 struct am79c930_softc *sc;
290 u_int32_t off;
291 u_int16_t val;
292 {
293 bus_space_write_2(sc->sc_memt, sc->sc_memh, off, val);
294 }
295
296 static void mem_write_4 (sc, off, val)
297 struct am79c930_softc *sc;
298 u_int32_t off;
299 u_int32_t val;
300 {
301 bus_space_write_4(sc->sc_memt, sc->sc_memh, off, val);
302 }
303
304 static void mem_write_bytes (sc, off, ptr, len)
305 struct am79c930_softc *sc;
306 u_int32_t off;
307 u_int8_t *ptr;
308 size_t len;
309 {
310 bus_space_write_region_1 (sc->sc_memt, sc->sc_memh, off, ptr, len);
311 }
312
313
314 static u_int8_t mem_read_1 (sc, off)
315 struct am79c930_softc *sc;
316 u_int32_t off;
317 {
318 return bus_space_read_1(sc->sc_memt, sc->sc_memh, off);
319 }
320
321 static u_int16_t mem_read_2 (sc, off)
322 struct am79c930_softc *sc;
323 u_int32_t off;
324 {
325 return bus_space_read_2(sc->sc_memt, sc->sc_memh, off);
326 }
327
328 static u_int32_t mem_read_4 (sc, off)
329 struct am79c930_softc *sc;
330 u_int32_t off;
331 {
332 return bus_space_read_4(sc->sc_memt, sc->sc_memh, off);
333 }
334
335
336
337 static void mem_read_bytes (sc, off, ptr, len)
338 struct am79c930_softc *sc;
339 u_int32_t off;
340 u_int8_t *ptr;
341 size_t len;
342 {
343 bus_space_read_region_1 (sc->sc_memt, sc->sc_memh, off, ptr, len);
344 }
345
346
347
348
349 /*
350 * Set bits in GCR.
351 */
352
353 void am79c930_gcr_setbits (sc, bits)
354 struct am79c930_softc *sc;
355 u_int8_t bits;
356 {
357 u_int8_t gcr = bus_space_read_1 (sc->sc_iot, sc->sc_ioh, AM79C930_GCR);
358
359 gcr |= bits;
360
361 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_GCR, gcr);
362 }
363
364 /*
365 * Clear bits in GCR.
366 */
367
368 void am79c930_gcr_clearbits (sc, bits)
369 struct am79c930_softc *sc;
370 u_int8_t bits;
371 {
372 u_int8_t gcr = bus_space_read_1 (sc->sc_iot, sc->sc_ioh, AM79C930_GCR);
373
374 gcr &= ~bits;
375
376 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_GCR, gcr);
377 }
378
379 u_int8_t am79c930_gcr_read (sc)
380 struct am79c930_softc *sc;
381 {
382 return bus_space_read_1 (sc->sc_iot, sc->sc_ioh, AM79C930_GCR);
383 }
384
385 #if 0
386 void am79c930_regdump (sc)
387 struct am79c930_softc *sc;
388 {
389 u_int8_t buf[8];
390 int i;
391
392 AM930_DELAY(5);
393 for (i=0; i<8; i++) {
394 buf[i] = bus_space_read_1 (sc->sc_iot, sc->sc_ioh, i);
395 AM930_DELAY(5);
396 }
397 printf("am79c930: regdump:");
398 for (i=0; i<8; i++) {
399 printf(" %02x", buf[i]);
400 }
401 printf("\n");
402 }
403 #endif
404
405 void am79c930_chip_init (sc, how)
406 struct am79c930_softc *sc;
407 {
408 /* zero the bank select register, and leave it that way.. */
409 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_BSS, 0);
410 if (how)
411 sc->sc_ops = &memspace_ops;
412 else
413 sc->sc_ops = &iospace_ops;
414 }
415
416
417