xref: /netbsd/sys/arch/sun2/dev/if_ie_obio.c (revision 6550d01e)
1 /*	$NetBSD: if_ie_obio.c,v 1.14 2011/02/01 20:19:31 chuck 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 and Matt Fredette.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1995 Charles D. Cranor
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55  */
56 
57 
58 
59 /*
60  * Sun2 OBIO front-end for the Intel 82586 Ethernet driver
61  *
62  * Converted to SUN ie driver by Charles D. Cranor,
63  *		October 1994, January 1995.
64  */
65 
66 /*
67  * The i82586 is a very painful chip, found in sun[23]'s, sun-4/100's
68  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
69  * SUN, making life difficult.  Programming this chip is mostly the same,
70  * but certain details differ from system to system.  This driver is
71  * written so that different "ie" interfaces can be controled by the same
72  * driver.
73  */
74 
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: if_ie_obio.c,v 1.14 2011/02/01 20:19:31 chuck Exp $");
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/errno.h>
81 #include <sys/device.h>
82 #include <sys/malloc.h>
83 #include <sys/protosw.h>
84 #include <sys/socket.h>
85 
86 #include <net/if.h>
87 #include <net/if_types.h>
88 #include <net/if_dl.h>
89 #include <net/if_media.h>
90 #include <net/if_ether.h>
91 
92 #include <uvm/uvm_extern.h>
93 
94 #include <machine/bus.h>
95 #include <machine/intr.h>
96 #include <machine/autoconf.h>
97 #include <machine/idprom.h>
98 
99 #include <dev/ic/i82586reg.h>
100 #include <dev/ic/i82586var.h>
101 
102 /*
103  * the on-board interface
104  */
105 struct ieob {
106 	u_char  obctrl;
107 };
108 #define IEOB_NORSET 0x80	/* don't reset the board */
109 #define IEOB_ONAIR  0x40	/* put us on the air */
110 #define IEOB_ATTEN  0x20	/* attention! */
111 #define IEOB_IENAB  0x10	/* interrupt enable */
112 #define IEOB_XXXXX  0x08	/* free bit */
113 #define IEOB_XCVRL2 0x04	/* level 2 transceiver? */
114 #define IEOB_BUSERR 0x02	/* bus error */
115 #define IEOB_INT    0x01	/* interrupt */
116 
117 #define IEOB_ADBASE 0x000000  /* KVA base addr of 24 bit address space */
118 
119 
120 static void ie_obreset(struct ie_softc *, int);
121 static void ie_obattend(struct ie_softc *, int);
122 static void ie_obrun(struct ie_softc *);
123 
124 int ie_obio_match(struct device *, struct cfdata *, void *);
125 void ie_obio_attach(struct device *, struct device *, void *);
126 
127 CFATTACH_DECL(ie_obio, sizeof(struct ie_softc),
128     ie_obio_match, ie_obio_attach, NULL, NULL);
129 
130 /* Supported media */
131 static int media[] = {
132 	IFM_ETHER | IFM_10_2,
133 };
134 #define NMEDIA	(sizeof(media) / sizeof(media[0]))
135 
136 
137 /*
138  * OBIO ie support routines
139  */
140 void
141 ie_obreset(struct ie_softc *sc, int what)
142 {
143 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
144 	ieo->obctrl = 0;
145 	delay(100);			/* XXX could be shorter? */
146 	ieo->obctrl = IEOB_NORSET;
147 }
148 void
149 ie_obattend(struct ie_softc *sc, int why)
150 {
151 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
152 
153 	ieo->obctrl |= IEOB_ATTEN;	/* flag! */
154 	ieo->obctrl &= ~IEOB_ATTEN;	/* down. */
155 }
156 
157 void
158 ie_obrun(struct ie_softc *sc)
159 {
160 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
161 
162 	ieo->obctrl |= (IEOB_ONAIR|IEOB_IENAB|IEOB_NORSET);
163 }
164 
165 void ie_obio_memcopyin(struct ie_softc *, void *, int, size_t);
166 void ie_obio_memcopyout(struct ie_softc *, const void *, int, size_t);
167 
168 /*
169  * Copy board memory to kernel.
170  */
171 void
172 ie_obio_memcopyin(struct ie_softc *sc, void *p, int offset, size_t size)
173 {
174 	bus_space_copyin(sc->bt, sc->bh, offset, p, size);
175 }
176 
177 /*
178  * Copy from kernel space to naord memory.
179  */
180 void
181 ie_obio_memcopyout(struct ie_softc *sc, const void *p, int offset, size_t size)
182 {
183 	bus_space_copyout(sc->bt, sc->bh, offset, p, size);
184 }
185 
186 /* read a 16-bit value at BH offset */
187 uint16_t ie_obio_read16(struct ie_softc *, int);
188 /* write a 16-bit value at BH offset */
189 void ie_obio_write16(struct ie_softc *, int, uint16_t);
190 void ie_obio_write24(struct ie_softc *, int, int);
191 
192 uint16_t
193 ie_obio_read16(struct ie_softc *sc, int offset)
194 {
195 	uint16_t v = bus_space_read_2(sc->bt, sc->bh, offset);
196 	return (((v&0xff)<<8) | ((v>>8)&0xff));
197 }
198 
199 void
200 ie_obio_write16(struct ie_softc *sc, int offset, uint16_t v)
201 {
202 	v = (((v&0xff)<<8) | ((v>>8)&0xff));
203 	bus_space_write_2(sc->bt, sc->bh, offset, v);
204 }
205 
206 void
207 ie_obio_write24(struct ie_softc *sc, int offset, int addr)
208 {
209 	u_char *f = (u_char *)&addr;
210 	uint16_t v0, v1;
211 	u_char *t;
212 
213 	t = (u_char *)&v0;
214 	t[0] = f[3]; t[1] = f[2];
215 	bus_space_write_2(sc->bt, sc->bh, offset, v0);
216 
217 	t = (u_char *)&v1;
218 	t[0] = f[1]; t[1] = 0;
219 	bus_space_write_2(sc->bt, sc->bh, offset+2, v1);
220 }
221 
222 int
223 ie_obio_match(struct device *parent, struct cfdata *cf, void *aux)
224 {
225 	struct obio_attach_args *oba = aux;
226 	bus_space_handle_t bh;
227 	int matched;
228 	uint8_t ctrl;
229 
230 	/* No default obio address. */
231 	if (oba->oba_paddr == -1)
232 		return(0);
233 
234 	/* Make sure there is something there... */
235 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr, sizeof(struct ieob),
236 			  0, &bh))
237 		return (0);
238 	matched = (!bus_space_poke_1(oba->oba_bustag, bh, 0, IEOB_NORSET) &&
239 		!bus_space_peek_1(oba->oba_bustag, bh, 0, &ctrl) &&
240 		(ctrl & (IEOB_ONAIR|IEOB_IENAB)) == 0);
241 	bus_space_unmap(oba->oba_bustag, bh, sizeof(struct ieob));
242 	if (!matched)
243 		return (0);
244 
245 	/* Default interrupt priority. */
246 	if (oba->oba_pri == -1)
247 		oba->oba_pri = 3;
248 
249 	return (1);
250 }
251 
252 void
253 ie_obio_attach(struct device *parent, struct device *self, void *aux)
254 {
255 	struct obio_attach_args *oba = aux;
256 	struct ie_softc *sc = (void *) self;
257 	bus_dma_tag_t dmatag = oba->oba_dmatag;
258 	bus_space_handle_t bh;
259 	bus_dma_segment_t seg;
260 	int rseg;
261 	int error;
262 	paddr_t pa;
263 	struct intrhand *ih;
264 	bus_size_t memsize;
265 	u_long iebase;
266 	uint8_t myaddr[ETHER_ADDR_LEN];
267 
268 	sc->bt = oba->oba_bustag;
269 
270 	sc->hwreset = ie_obreset;
271 	sc->chan_attn = ie_obattend;
272 	sc->hwinit = ie_obrun;
273 	sc->memcopyout = ie_obio_memcopyout;
274 	sc->memcopyin = ie_obio_memcopyin;
275 
276 	sc->ie_bus_barrier = NULL;
277 	sc->ie_bus_read16 = ie_obio_read16;
278 	sc->ie_bus_write16 = ie_obio_write16;
279 	sc->ie_bus_write24 = ie_obio_write24;
280 	sc->sc_msize = memsize = 65536; /* XXX */
281 
282 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr, sizeof(struct ieob),
283   			0, &bh))
284 		panic("ie_obio_attach: can't map regs");
285 	sc->sc_reg = (void *)bh;
286 
287 	/*
288 	 * Allocate control & buffer memory.
289 	 */
290 	if ((error = bus_dmamap_create(dmatag, memsize, 1, memsize, 0,
291 					BUS_DMA_NOWAIT|BUS_DMA_24BIT,
292 					&sc->sc_dmamap)) != 0) {
293 		printf("%s: DMA map create error %d\n",
294 			sc->sc_dev.dv_xname, error);
295 		return;
296 	}
297 	if ((error = bus_dmamem_alloc(dmatag, memsize, 64*1024, 0,
298 			     &seg, 1, &rseg,
299 			     BUS_DMA_NOWAIT | BUS_DMA_24BIT)) != 0) {
300 		printf("%s: DMA memory allocation error %d\n",
301 			self->dv_xname, error);
302 		return;
303 	}
304 
305 	/* Map DMA buffer in CPU addressable space */
306 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, memsize,
307 				    (void **)&sc->sc_maddr,
308 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
309 		printf("%s: DMA buffer map error %d\n",
310 			sc->sc_dev.dv_xname, error);
311 		bus_dmamem_free(dmatag, &seg, rseg);
312 		return;
313 	}
314 
315 	/* Load the segment */
316 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
317 				     sc->sc_maddr, memsize, NULL,
318 				     BUS_DMA_NOWAIT)) != 0) {
319 		printf("%s: DMA buffer map load error %d\n",
320 			sc->sc_dev.dv_xname, error);
321 		bus_dmamem_unmap(dmatag, sc->sc_maddr, memsize);
322 		bus_dmamem_free(dmatag, &seg, rseg);
323 		return;
324 	}
325 
326 	w16zero(sc->sc_maddr, memsize);
327 	sc->bh = (bus_space_handle_t)(sc->sc_maddr);
328 
329 	/*
330 	 * The i82586's 24-bit address space maps to all of
331 	 * KVA space ().  In addition, the SCP must appear
332 	 * at IE_SCP_ADDR within the 24-bit address space,
333 	 * i.e. at KVA  IEOB_ADBASE+IE_SCP_ADDR, at the very top of
334 	 * kernel space.  We double-map this last page to the first
335 	 * page (starting at `maddr') of the memory we allocate to the chip.
336 	 * (a side-effect of this double-map is that the ISCP and SCB
337 	 * structures also get aliased there, but we ignore this). The
338 	 * first page at `maddr' is only used for ISCP, SCB and the aliased
339 	 * SCP; the actual buffers start at maddr+PAGE_SIZE.
340 	 *
341 	 * In a picture:
342 
343 	|---//--- ISCP-SCB-----scp-|--//- buffers -//-|... |iscp-scb-----SCP-|
344 	|         |                |                  |    |             |   |
345 	|         |<---PAGE_SIZE-->|                  |    |<--PAGE_SIZE-+-->|
346 	|         |<------------ memsize ------------>|    |       ^     |
347 	|         |                                                |     |
348 	|         \@maddr                                 (last page dbl mapped)
349 	|                                                                |
350 	\@IEOB_ADBASE                           @IEOB_ADBASE+IE_SCP_ADDR-+
351 
352 	 *
353 	 */
354 
355 	/* Double map the SCP */
356 	if (pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_maddr, &pa) == false)
357 		panic("ie pmap_extract");
358 
359 	pmap_enter(pmap_kernel(), m68k_trunc_page(IEOB_ADBASE+IE_SCP_ADDR),
360 	    pa | PMAP_NC /*| PMAP_IOC*/,
361 	    VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
362 
363 	/* Map iscp at location 0 (relative to `maddr') */
364 	sc->iscp = 0;
365 
366 	/* scb follows iscp */
367 	sc->scb = IE_ISCP_SZ;
368 
369 	/* scp is at the fixed location IE_SCP_ADDR (modulo the page size) */
370 	sc->scp = IE_SCP_ADDR & PGOFSET;
371 
372 	/* Calculate the 24-bit base of i82586 operations */
373 	iebase = (u_long)sc->sc_dmamap->dm_segs[0].ds_addr -
374 			(u_long)IEOB_ADBASE;
375 	ie_obio_write16(sc, IE_ISCP_SCB(sc->iscp), sc->scb);
376 	ie_obio_write24(sc, IE_ISCP_BASE(sc->iscp), iebase);
377 	ie_obio_write24(sc, IE_SCP_ISCP(sc->scp), iebase + sc->iscp);
378 
379 	/*
380 	 * Rest of first page is unused (wasted!); the other pages
381 	 * are used for buffers.
382 	 */
383 	sc->buf_area = PAGE_SIZE;
384 	sc->buf_area_sz = memsize - PAGE_SIZE;
385 
386 	if (i82586_proberam(sc) == 0) {
387 		printf(": memory probe failed\n");
388 		return;
389 	}
390 
391 	idprom_etheraddr(myaddr);
392 	i82586_attach(sc, "onboard", myaddr, media, NMEDIA, media[0]);
393 
394 	/* Establish interrupt channel */
395 	ih = bus_intr_establish(oba->oba_bustag, oba->oba_pri, IPL_NET, 0,
396 				i82586_intr, sc);
397 }
398