if_ie_obio.c revision 1.6 1 /* $NetBSD: if_ie_obio.c,v 1.6 1998/03/29 22:10:33 pk Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
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 * Copyright (c) 1995 Charles D. Cranor
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by Charles D. Cranor.
54 * 4. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69
70
71 /*
72 * Sparc OBIO front-end for the Intel 82586 Ethernet driver
73 *
74 * Converted to SUN ie driver by Charles D. Cranor,
75 * October 1994, January 1995.
76 */
77
78 /*
79 * The i82586 is a very painful chip, found in sun3's, sun-4/100's
80 * sun-4/200's, and VME based suns. The byte order is all wrong for a
81 * SUN, making life difficult. Programming this chip is mostly the same,
82 * but certain details differ from system to system. This driver is
83 * written so that different "ie" interfaces can be controled by the same
84 * driver.
85 */
86
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/errno.h>
90 #include <sys/device.h>
91 #include <sys/malloc.h>
92 #include <sys/protosw.h>
93 #include <sys/socket.h>
94
95 #include <net/if.h>
96 #include <net/if_types.h>
97 #include <net/if_dl.h>
98 #include <net/if_media.h>
99 #include <net/if_ether.h>
100
101 #include <vm/vm.h>
102 #ifdef UVM
103 #include <uvm/uvm.h>
104 #endif
105
106 #include <machine/autoconf.h>
107 #include <machine/cpu.h>
108 #include <machine/pmap.h>
109 #include <machine/bus.h>
110
111 #include <dev/ic/i82586reg.h>
112 #include <dev/ic/i82586var.h>
113
114 /*
115 * the on-board interface
116 */
117 struct ieob {
118 u_char obctrl;
119 };
120 #define IEOB_NORSET 0x80 /* don't reset the board */
121 #define IEOB_ONAIR 0x40 /* put us on the air */
122 #define IEOB_ATTEN 0x20 /* attention! */
123 #define IEOB_IENAB 0x10 /* interrupt enable */
124 #define IEOB_XXXXX 0x08 /* free bit */
125 #define IEOB_XCVRL2 0x04 /* level 2 transceiver? */
126 #define IEOB_BUSERR 0x02 /* bus error */
127 #define IEOB_INT 0x01 /* interrupt */
128
129 #define IEOB_ADBASE 0xff000000 /* KVA base addr of 24 bit address space */
130
131
132 static void ie_obreset __P((struct ie_softc *, int));
133 static void ie_obattend __P((struct ie_softc *));
134 static void ie_obrun __P((struct ie_softc *));
135
136 vm_map_t ie_map; /* XXX - needs change */
137
138 int ie_obio_match __P((struct device *, struct cfdata *, void *));
139 void ie_obio_attach __P((struct device *, struct device *, void *));
140
141 struct cfattach ie_obio_ca = {
142 sizeof(struct ie_softc), ie_obio_match, ie_obio_attach
143 };
144
145 /* Supported media */
146 static int media[] = {
147 IFM_ETHER | IFM_10_2,
148 };
149 #define NMEDIA (sizeof(media) / sizeof(media[0]))
150
151
152 /*
153 * OBIO ie support routines
154 */
155 void
156 ie_obreset(sc, what)
157 struct ie_softc *sc;
158 {
159 volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
160 ieo->obctrl = 0;
161 delay(100); /* XXX could be shorter? */
162 ieo->obctrl = IEOB_NORSET;
163 }
164 void
165 ie_obattend(sc)
166 struct ie_softc *sc;
167 {
168 volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
169
170 ieo->obctrl |= IEOB_ATTEN; /* flag! */
171 ieo->obctrl &= ~IEOB_ATTEN; /* down. */
172 }
173
174 void
175 ie_obrun(sc)
176 struct ie_softc *sc;
177 {
178 volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
179
180 ieo->obctrl |= (IEOB_ONAIR|IEOB_IENAB|IEOB_NORSET);
181 }
182
183 void ie_obio_memcopyin __P((struct ie_softc *, void *, int, size_t));
184 void ie_obio_memcopyout __P((struct ie_softc *, const void *, int, size_t));
185
186 /*
187 * Copy board memory to kernel.
188 */
189 void
190 ie_obio_memcopyin(sc, p, offset, size)
191 struct ie_softc *sc;
192 void *p;
193 int offset;
194 size_t size;
195 {
196 void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
197 wcopy(addr, p, size);
198 }
199
200 /*
201 * Copy from kernel space to naord memory.
202 */
203 void
204 ie_obio_memcopyout(sc, p, offset, size)
205 struct ie_softc *sc;
206 const void *p;
207 int offset;
208 size_t size;
209 {
210 void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
211 wcopy(p, addr, size);
212 }
213
214 /* read a 16-bit value at BH offset */
215 u_int16_t ie_obio_read16 __P((struct ie_softc *, int offset));
216 /* write a 16-bit value at BH offset */
217 void ie_obio_write16 __P((struct ie_softc *, int offset, u_int16_t value));
218 void ie_obio_write24 __P((struct ie_softc *, int offset, int addr));
219
220 u_int16_t
221 ie_obio_read16(sc, offset)
222 struct ie_softc *sc;
223 int offset;
224 {
225 u_int16_t v = bus_space_read_2(sc->bt, sc->bh, offset);
226 return (((v&0xff)<<8) | ((v>>8)&0xff));
227 }
228
229 void
230 ie_obio_write16(sc, offset, v)
231 struct ie_softc *sc;
232 int offset;
233 u_int16_t v;
234 {
235 v = (((v&0xff)<<8) | ((v>>8)&0xff));
236 bus_space_write_2(sc->bt, sc->bh, offset, v);
237 }
238
239 void
240 ie_obio_write24(sc, offset, addr)
241 struct ie_softc *sc;
242 int offset;
243 int addr;
244 {
245 u_int32_t v;
246 u_char *t = (u_char *)&v, *f = (u_char *)&addr;
247
248 t[0] = f[3]; t[1] = f[2]; t[2] = f[1]; t[3] = 0;
249 bus_space_write_4(sc->bt, sc->bh, offset, v);
250 }
251
252 int
253 ie_obio_match(parent, cf, aux)
254 struct device *parent;
255 struct cfdata *cf;
256 void *aux;
257 {
258 union obio_attach_args *uoba = aux;
259 struct obio4_attach_args *oba;
260
261 if (uoba->uoba_isobio4 == 0)
262 return (0);
263
264 oba = &uoba->uoba_oba4;
265 return (bus_space_probe(oba->oba_bustag, 0, oba->oba_paddr,
266 1, /* probe size */
267 0, /* offset */
268 0, /* flags */
269 NULL, NULL));
270 }
271
272 void
273 ie_obio_attach(parent, self, aux)
274 struct device *parent;
275 struct device *self;
276 void *aux;
277 {
278 union obio_attach_args *uoba = aux;
279 struct obio4_attach_args *oba = &uoba->uoba_oba4;
280 struct ie_softc *sc = (void *) self;
281 bus_space_handle_t bh;
282 struct bootpath *bp;
283 volatile struct ieob *ieo;
284 vm_offset_t pa;
285 struct intrhand *ih;
286 u_int8_t myaddr[ETHER_ADDR_LEN];
287 extern void myetheraddr(u_char *); /* should be elsewhere */
288
289 sc->bt = 0;
290
291 sc->hwreset = ie_obreset;
292 sc->chan_attn = ie_obattend;
293 sc->hwinit = ie_obrun;
294 sc->memcopyout = ie_obio_memcopyout;
295 sc->memcopyin = ie_obio_memcopyin;
296 sc->ie_bus_read16 = ie_obio_read16;
297 sc->ie_bus_write16 = ie_obio_write16;
298 sc->ie_bus_write24 = ie_obio_write24;
299 sc->sc_msize = 65536; /* XXX */
300
301 if (obio_bus_map(oba->oba_bustag, oba->oba_paddr,
302 0,
303 sizeof(struct ieob),
304 BUS_SPACE_MAP_LINEAR,
305 0, &bh) != 0) {
306 printf("%s: cannot map registers\n", self->dv_xname);
307 return;
308 }
309 sc->sc_reg = (void *)bh;
310 ieo = (volatile struct ieob *)bh;
311
312 /*
313 * the rest of the IE_OBIO case needs to be cleaned up
314 * XXX-should provide bus support for 24-bit devices..
315 */
316
317 #ifdef UVM
318 ie_map = uvm_map_create(pmap_kernel(), (vm_offset_t)IEOB_ADBASE,
319 (vm_offset_t)IEOB_ADBASE + sc->sc_msize, TRUE);
320 #else
321 ie_map = vm_map_create(pmap_kernel(), (vm_offset_t)IEOB_ADBASE,
322 (vm_offset_t)IEOB_ADBASE + sc->sc_msize, 1);
323 #endif
324 if (ie_map == NULL)
325 panic("ie_map");
326
327 #ifdef UVM
328 sc->sc_maddr = (caddr_t) uvm_km_alloc(ie_map, sc->sc_msize);
329 #else
330 sc->sc_maddr = (caddr_t) kmem_alloc(ie_map, sc->sc_msize);
331 #endif
332 if (sc->sc_maddr == NULL)
333 panic("ie kmem_alloc");
334
335 sc->bh = (bus_space_handle_t)(sc->sc_maddr);
336
337 kvm_uncache(sc->sc_maddr, sc->sc_msize >> PGSHIFT);
338 if (((u_long)sc->sc_maddr & ~(NBPG-1)) != (u_long)sc->sc_maddr)
339 panic("unaligned dvmamalloc breaks");
340
341 sc->sc_iobase = (caddr_t)IEOB_ADBASE; /* 24 bit base addr */
342 wzero(sc->sc_maddr, sc->sc_msize);
343
344 /*
345 * The i82586's 24-bit address space maps to the last 16MB of
346 * KVA space (). In addition, the SCP must appear
347 * at IE_SCP_ADDR within the 24-bit address space,
348 * i.e. at KVA IEOB_ADBASE+IE_SCP_ADDR, at the very top of
349 * kernel space. We double-map this last page to the first
350 * page (starting at `maddr') of the memory we allocate to the chip.
351 * (a side-effect of this double-map is that the ISCP and SCB
352 * structures also get aliased there, but we ignore this). The
353 * first page at `maddr' is only used for ISCP, SCB and the aliased
354 * SCP; the actual buffers start at maddr+NBPG.
355 *
356 * In a picture:
357
358 |---//--- ISCP-SCB-----scp-|--//- buffers -//-|... |iscp-scb-----SCP-|
359 | | | | | | |
360 | |<----- NBPG --->| | |<----- NBPG -+-->|
361 | |<------------- msize ------------->| | ^ |
362 | | | |
363 | \@maddr (last page dbl mapped)
364 | |
365 \@IEOB_ADBASE @IEOB_ADBASE+IE_SCP_ADDR-+
366
367 *
368 */
369
370 pa = pmap_extract(pmap_kernel(), (vm_offset_t)sc->sc_maddr);
371 if (pa == 0)
372 panic("ie pmap_extract");
373
374 pmap_enter(pmap_kernel(), trunc_page(IEOB_ADBASE+IE_SCP_ADDR),
375 (vm_offset_t)pa | PMAP_NC /*| PMAP_IOC*/,
376 VM_PROT_READ | VM_PROT_WRITE, 1);
377
378 /* Map iscp at location zero */
379 sc->iscp = 0;
380
381 /* scb follows iscp */
382 sc->scb = IE_ISCP_SZ;
383
384 sc->scp = IEOB_ADBASE + IE_SCP_ADDR;
385
386 ie_obio_write16(sc, IE_ISCP_SCB((u_long)sc->iscp), sc->scb);
387 ie_obio_write24(sc, IE_ISCP_BASE((u_long)sc->iscp), IEOB_ADBASE);
388 ie_obio_write24(sc, IE_SCP_ISCP((u_long)sc->scp),
389 (u_long)sc->iscp - (u_long)IEOB_ADBASE);
390
391
392 /*
393 * Rest of first page is unused (wasted!); rest of ram
394 * for buffers.
395 */
396 sc->buf_area = NBPG;
397 sc->buf_area_sz = sc->sc_msize - NBPG;
398
399 myetheraddr(myaddr);
400 i82586_attach(sc, "onboard", myaddr, media, NMEDIA, media[0]);
401
402 /* Establish interrupt channel */
403 ih = bus_intr_establish(oba->oba_bustag,
404 oba->oba_pri, 0,
405 i82586_intr, sc);
406
407 bp = oba->oba_bp;
408 if (bp != NULL && strcmp(bp->name, "ie") == 0 &&
409 sc->sc_dev.dv_unit == bp->val[1])
410 bp->dev = &sc->sc_dev;
411 }
412