xref: /netbsd/sys/arch/sparc/dev/if_ie_obio.c (revision bf9ec67e)
1 /*	$NetBSD: if_ie_obio.c,v 1.24 2002/03/11 16:27:02 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 <uvm/uvm_extern.h>
102 
103 #include <machine/bus.h>
104 #include <machine/intr.h>
105 #include <machine/autoconf.h>
106 
107 #include <dev/ic/i82586reg.h>
108 #include <dev/ic/i82586var.h>
109 
110 /*
111  * the on-board interface
112  */
113 struct ieob {
114 	u_char  obctrl;
115 };
116 #define IEOB_NORSET 0x80	/* don't reset the board */
117 #define IEOB_ONAIR  0x40	/* put us on the air */
118 #define IEOB_ATTEN  0x20	/* attention! */
119 #define IEOB_IENAB  0x10	/* interrupt enable */
120 #define IEOB_XXXXX  0x08	/* free bit */
121 #define IEOB_XCVRL2 0x04	/* level 2 transceiver? */
122 #define IEOB_BUSERR 0x02	/* bus error */
123 #define IEOB_INT    0x01	/* interrupt */
124 
125 #define IEOB_ADBASE 0xff000000  /* KVA base addr of 24 bit address space */
126 
127 
128 static void ie_obreset __P((struct ie_softc *, int));
129 static void ie_obattend __P((struct ie_softc *, int));
130 static void ie_obrun __P((struct ie_softc *));
131 
132 int ie_obio_match __P((struct device *, struct cfdata *, void *));
133 void ie_obio_attach __P((struct device *, struct device *, void *));
134 
135 struct cfattach ie_obio_ca = {
136 	sizeof(struct ie_softc), ie_obio_match, ie_obio_attach
137 };
138 
139 /* Supported media */
140 static int media[] = {
141 	IFM_ETHER | IFM_10_2,
142 };
143 #define NMEDIA	(sizeof(media) / sizeof(media[0]))
144 
145 
146 /*
147  * OBIO ie support routines
148  */
149 void
150 ie_obreset(sc, what)
151 	struct ie_softc *sc;
152 {
153 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
154 	ieo->obctrl = 0;
155 	delay(100);			/* XXX could be shorter? */
156 	ieo->obctrl = IEOB_NORSET;
157 }
158 void
159 ie_obattend(sc, why)
160 	struct ie_softc *sc;
161 	int why;
162 {
163 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
164 
165 	ieo->obctrl |= IEOB_ATTEN;	/* flag! */
166 	ieo->obctrl &= ~IEOB_ATTEN;	/* down. */
167 }
168 
169 void
170 ie_obrun(sc)
171 	struct ie_softc *sc;
172 {
173 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
174 
175 	ieo->obctrl |= (IEOB_ONAIR|IEOB_IENAB|IEOB_NORSET);
176 }
177 
178 void ie_obio_memcopyin __P((struct ie_softc *, void *, int, size_t));
179 void ie_obio_memcopyout __P((struct ie_softc *, const void *, int, size_t));
180 
181 /*
182  * Copy board memory to kernel.
183  */
184 void
185 ie_obio_memcopyin(sc, p, offset, size)
186 	struct ie_softc	*sc;
187 	void *p;
188 	int offset;
189 	size_t size;
190 {
191 	void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
192 	wcopy(addr, p, size);
193 }
194 
195 /*
196  * Copy from kernel space to naord memory.
197  */
198 void
199 ie_obio_memcopyout(sc, p, offset, size)
200 	struct ie_softc	*sc;
201 	const void *p;
202 	int offset;
203 	size_t size;
204 {
205 	void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
206 	wcopy(p, addr, size);
207 }
208 
209 /* read a 16-bit value at BH offset */
210 u_int16_t ie_obio_read16 __P((struct ie_softc *, int offset));
211 /* write a 16-bit value at BH offset */
212 void ie_obio_write16 __P((struct ie_softc *, int offset, u_int16_t value));
213 void ie_obio_write24 __P((struct ie_softc *, int offset, int addr));
214 
215 u_int16_t
216 ie_obio_read16(sc, offset)
217 	struct ie_softc *sc;
218 	int offset;
219 {
220 	u_int16_t v = bus_space_read_2(sc->bt, sc->bh, offset);
221 	return (((v&0xff)<<8) | ((v>>8)&0xff));
222 }
223 
224 void
225 ie_obio_write16(sc, offset, v)
226 	struct ie_softc *sc;
227 	int offset;
228 	u_int16_t v;
229 {
230 	v = (((v&0xff)<<8) | ((v>>8)&0xff));
231 	bus_space_write_2(sc->bt, sc->bh, offset, v);
232 }
233 
234 void
235 ie_obio_write24(sc, offset, addr)
236 	struct ie_softc *sc;
237 	int offset;
238 	int addr;
239 {
240 	u_char *f = (u_char *)&addr;
241 	u_int16_t v0, v1;
242 	u_char *t;
243 
244 	t = (u_char *)&v0;
245 	t[0] = f[3]; t[1] = f[2];
246 	bus_space_write_2(sc->bt, sc->bh, offset, v0);
247 
248 	t = (u_char *)&v1;
249 	t[0] = f[1]; t[1] = 0;
250 	bus_space_write_2(sc->bt, sc->bh, offset+2, v1);
251 }
252 
253 int
254 ie_obio_match(parent, cf, aux)
255 	struct device *parent;
256 	struct cfdata *cf;
257 	void *aux;
258 {
259 	union obio_attach_args *uoba = aux;
260 	struct obio4_attach_args *oba;
261 
262 	if (uoba->uoba_isobio4 == 0)
263 		return (0);
264 
265 	oba = &uoba->uoba_oba4;
266 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
267 				1,	/* probe size */
268 				0,	/* offset */
269 				0,	/* flags */
270 				NULL, NULL));
271 }
272 
273 void
274 ie_obio_attach(parent, self, aux)
275 	struct device *parent;
276 	struct device *self;
277 	void   *aux;
278 {
279 	union obio_attach_args *uoba = aux;
280 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
281 	struct ie_softc *sc = (void *) self;
282 	bus_dma_tag_t dmatag = oba->oba_dmatag;
283 	bus_space_handle_t bh;
284 	bus_dma_segment_t seg;
285 	int rseg;
286 	int error;
287 	paddr_t pa;
288 	struct intrhand *ih;
289 	bus_size_t msize;
290 	u_long iebase;
291 	u_int8_t myaddr[ETHER_ADDR_LEN];
292 extern	void myetheraddr(u_char *);	/* should be elsewhere */
293 
294 	sc->bt = oba->oba_bustag;
295 
296 	sc->hwreset = ie_obreset;
297 	sc->chan_attn = ie_obattend;
298 	sc->hwinit = ie_obrun;
299 	sc->memcopyout = ie_obio_memcopyout;
300 	sc->memcopyin = ie_obio_memcopyin;
301 
302 	sc->ie_bus_barrier = NULL;
303 	sc->ie_bus_read16 = ie_obio_read16;
304 	sc->ie_bus_write16 = ie_obio_write16;
305 	sc->ie_bus_write24 = ie_obio_write24;
306 	sc->sc_msize = msize = 65536; /* XXX */
307 
308 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
309 			  sizeof(struct ieob),
310 			  BUS_SPACE_MAP_LINEAR,
311 			  &bh) != 0) {
312 		printf("%s: cannot map registers\n", self->dv_xname);
313 		return;
314 	}
315 	sc->sc_reg = (void *)bh;
316 
317 	/*
318 	 * Allocate control & buffer memory.
319 	 */
320 	if ((error = bus_dmamap_create(dmatag, msize, 1, msize, 0,
321 					BUS_DMA_NOWAIT|BUS_DMA_24BIT,
322 					&sc->sc_dmamap)) != 0) {
323 		printf("%s: DMA map create error %d\n",
324 			sc->sc_dev.dv_xname, error);
325 		return;
326 	}
327 	if ((error = bus_dmamem_alloc(dmatag, msize, 64*1024, 0,
328 			     &seg, 1, &rseg,
329 			     BUS_DMA_NOWAIT | BUS_DMA_24BIT)) != 0) {
330 		printf("%s: DMA memory allocation error %d\n",
331 			self->dv_xname, error);
332 		return;
333 	}
334 
335 	/* Map DMA buffer in CPU addressable space */
336 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, msize,
337 				    (caddr_t *)&sc->sc_maddr,
338 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
339 		printf("%s: DMA buffer map error %d\n",
340 			sc->sc_dev.dv_xname, error);
341 		bus_dmamem_free(dmatag, &seg, rseg);
342 		return;
343 	}
344 
345 	/* Load the segment */
346 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
347 				     sc->sc_maddr, msize, NULL,
348 				     BUS_DMA_NOWAIT)) != 0) {
349 		printf("%s: DMA buffer map load error %d\n",
350 			sc->sc_dev.dv_xname, error);
351 		bus_dmamem_unmap(dmatag, sc->sc_maddr, msize);
352 		bus_dmamem_free(dmatag, &seg, rseg);
353 		return;
354 	}
355 
356 	wzero(sc->sc_maddr, msize);
357 	sc->bh = (bus_space_handle_t)(sc->sc_maddr);
358 
359 	/*
360 	 * The i82586's 24-bit address space maps to the last 16MB of
361 	 * KVA space ().  In addition, the SCP must appear
362 	 * at IE_SCP_ADDR within the 24-bit address space,
363 	 * i.e. at KVA  IEOB_ADBASE+IE_SCP_ADDR, at the very top of
364 	 * kernel space.  We double-map this last page to the first
365 	 * page (starting at `maddr') of the memory we allocate to the chip.
366 	 * (a side-effect of this double-map is that the ISCP and SCB
367 	 * structures also get aliased there, but we ignore this). The
368 	 * first page at `maddr' is only used for ISCP, SCB and the aliased
369 	 * SCP; the actual buffers start at maddr+NBPG.
370 	 *
371 	 * In a picture:
372 
373 	|---//--- ISCP-SCB-----scp-|--//- buffers -//-|... |iscp-scb-----SCP-|
374 	|         |                |                  |    |             |   |
375 	|         |<----- NBPG --->|                  |    |<----- NBPG -+-->|
376 	|         |<------------- msize ------------->|    |       ^     |
377 	|         |                                                |     |
378 	|         \@maddr                                 (last page dbl mapped)
379 	|                                                                |
380 	\@IEOB_ADBASE                           @IEOB_ADBASE+IE_SCP_ADDR-+
381 
382 	 *
383 	 */
384 
385 	/* Double map the SCP */
386 	if (pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_maddr, &pa) == FALSE)
387 		panic("ie pmap_extract");
388 
389 	pmap_enter(pmap_kernel(), trunc_page(IEOB_ADBASE+IE_SCP_ADDR),
390 	    pa | PMAP_NC /*| PMAP_IOC*/,
391 	    VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
392 	pmap_update(pmap_kernel());
393 
394 	/* Map iscp at location 0 (relative to `maddr') */
395 	sc->iscp = 0;
396 
397 	/* scb follows iscp */
398 	sc->scb = IE_ISCP_SZ;
399 
400 	/* scp is at the fixed location IE_SCP_ADDR (modulo the page size) */
401 	sc->scp = IE_SCP_ADDR & PGOFSET;
402 
403 	/* Calculate the 24-bit base of i82586 operations */
404 	iebase = (u_long)sc->sc_dmamap->dm_segs[0].ds_addr -
405 			(u_long)IEOB_ADBASE;
406 	ie_obio_write16(sc, IE_ISCP_SCB(sc->iscp), sc->scb);
407 	ie_obio_write24(sc, IE_ISCP_BASE(sc->iscp), iebase);
408 	ie_obio_write24(sc, IE_SCP_ISCP(sc->scp), iebase + sc->iscp);
409 
410 	/*
411 	 * Rest of first page is unused (wasted!); the other pages
412 	 * are used for buffers.
413 	 */
414 	sc->buf_area = NBPG;
415 	sc->buf_area_sz = msize - NBPG;
416 
417 	if (i82586_proberam(sc) == 0) {
418 		printf(": memory probe failed\n");
419 		return;
420 	}
421 
422 	myetheraddr(myaddr);
423 	i82586_attach(sc, "onboard", myaddr, media, NMEDIA, media[0]);
424 
425 	/* Establish interrupt channel */
426 	ih = bus_intr_establish(oba->oba_bustag,
427 				oba->oba_pri, IPL_NET, 0,
428 				i82586_intr, sc);
429 }
430