xref: /netbsd-src/sys/arch/sun2/dev/if_ie_obio.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: if_ie_obio.c,v 1.12 2007/03/04 06:00:51 christos 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  * 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  * Sun2 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 sun[23]'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/cdefs.h>
88 __KERNEL_RCSID(0, "$NetBSD: if_ie_obio.c,v 1.12 2007/03/04 06:00:51 christos Exp $");
89 
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/errno.h>
93 #include <sys/device.h>
94 #include <sys/malloc.h>
95 #include <sys/protosw.h>
96 #include <sys/socket.h>
97 
98 #include <net/if.h>
99 #include <net/if_types.h>
100 #include <net/if_dl.h>
101 #include <net/if_media.h>
102 #include <net/if_ether.h>
103 
104 #include <uvm/uvm_extern.h>
105 
106 #include <machine/bus.h>
107 #include <machine/intr.h>
108 #include <machine/autoconf.h>
109 #include <machine/idprom.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 0x000000  /* KVA base addr of 24 bit address space */
130 
131 
132 static void ie_obreset(struct ie_softc *, int);
133 static void ie_obattend(struct ie_softc *, int);
134 static void ie_obrun(struct ie_softc *);
135 
136 int ie_obio_match(struct device *, struct cfdata *, void *);
137 void ie_obio_attach(struct device *, struct device *, void *);
138 
139 CFATTACH_DECL(ie_obio, sizeof(struct ie_softc),
140     ie_obio_match, ie_obio_attach, NULL, NULL);
141 
142 /* Supported media */
143 static int media[] = {
144 	IFM_ETHER | IFM_10_2,
145 };
146 #define NMEDIA	(sizeof(media) / sizeof(media[0]))
147 
148 
149 /*
150  * OBIO ie support routines
151  */
152 void
153 ie_obreset(struct ie_softc *sc, int what)
154 {
155 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
156 	ieo->obctrl = 0;
157 	delay(100);			/* XXX could be shorter? */
158 	ieo->obctrl = IEOB_NORSET;
159 }
160 void
161 ie_obattend(struct ie_softc *sc, 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(struct ie_softc *sc)
171 {
172 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
173 
174 	ieo->obctrl |= (IEOB_ONAIR|IEOB_IENAB|IEOB_NORSET);
175 }
176 
177 void ie_obio_memcopyin(struct ie_softc *, void *, int, size_t);
178 void ie_obio_memcopyout(struct ie_softc *, const void *, int, size_t);
179 
180 /*
181  * Copy board memory to kernel.
182  */
183 void
184 ie_obio_memcopyin(struct ie_softc *sc, void *p, int offset, size_t size)
185 {
186 	bus_space_copyin(sc->bt, sc->bh, offset, p, size);
187 }
188 
189 /*
190  * Copy from kernel space to naord memory.
191  */
192 void
193 ie_obio_memcopyout(struct ie_softc *sc, const void *p, int offset, size_t size)
194 {
195 	bus_space_copyout(sc->bt, sc->bh, offset, p, size);
196 }
197 
198 /* read a 16-bit value at BH offset */
199 uint16_t ie_obio_read16(struct ie_softc *, int);
200 /* write a 16-bit value at BH offset */
201 void ie_obio_write16(struct ie_softc *, int, uint16_t);
202 void ie_obio_write24(struct ie_softc *, int, int);
203 
204 uint16_t
205 ie_obio_read16(struct ie_softc *sc, int offset)
206 {
207 	uint16_t v = bus_space_read_2(sc->bt, sc->bh, offset);
208 	return (((v&0xff)<<8) | ((v>>8)&0xff));
209 }
210 
211 void
212 ie_obio_write16(struct ie_softc *sc, int offset, uint16_t v)
213 {
214 	v = (((v&0xff)<<8) | ((v>>8)&0xff));
215 	bus_space_write_2(sc->bt, sc->bh, offset, v);
216 }
217 
218 void
219 ie_obio_write24(struct ie_softc *sc, int offset, int addr)
220 {
221 	u_char *f = (u_char *)&addr;
222 	uint16_t v0, v1;
223 	u_char *t;
224 
225 	t = (u_char *)&v0;
226 	t[0] = f[3]; t[1] = f[2];
227 	bus_space_write_2(sc->bt, sc->bh, offset, v0);
228 
229 	t = (u_char *)&v1;
230 	t[0] = f[1]; t[1] = 0;
231 	bus_space_write_2(sc->bt, sc->bh, offset+2, v1);
232 }
233 
234 int
235 ie_obio_match(struct device *parent, struct cfdata *cf, void *aux)
236 {
237 	struct obio_attach_args *oba = aux;
238 	bus_space_handle_t bh;
239 	int matched;
240 	uint8_t ctrl;
241 
242 	/* No default obio address. */
243 	if (oba->oba_paddr == -1)
244 		return(0);
245 
246 	/* Make sure there is something there... */
247 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr, sizeof(struct ieob),
248 			  0, &bh))
249 		return (0);
250 	matched = (!bus_space_poke_1(oba->oba_bustag, bh, 0, IEOB_NORSET) &&
251 		!bus_space_peek_1(oba->oba_bustag, bh, 0, &ctrl) &&
252 		(ctrl & (IEOB_ONAIR|IEOB_IENAB)) == 0);
253 	bus_space_unmap(oba->oba_bustag, bh, sizeof(struct ieob));
254 	if (!matched)
255 		return (0);
256 
257 	/* Default interrupt priority. */
258 	if (oba->oba_pri == -1)
259 		oba->oba_pri = 3;
260 
261 	return (1);
262 }
263 
264 void
265 ie_obio_attach(struct device *parent, struct device *self, void *aux)
266 {
267 	struct obio_attach_args *oba = aux;
268 	struct ie_softc *sc = (void *) self;
269 	bus_dma_tag_t dmatag = oba->oba_dmatag;
270 	bus_space_handle_t bh;
271 	bus_dma_segment_t seg;
272 	int rseg;
273 	int error;
274 	paddr_t pa;
275 	struct intrhand *ih;
276 	bus_size_t memsize;
277 	u_long iebase;
278 	uint8_t myaddr[ETHER_ADDR_LEN];
279 
280 	sc->bt = oba->oba_bustag;
281 
282 	sc->hwreset = ie_obreset;
283 	sc->chan_attn = ie_obattend;
284 	sc->hwinit = ie_obrun;
285 	sc->memcopyout = ie_obio_memcopyout;
286 	sc->memcopyin = ie_obio_memcopyin;
287 
288 	sc->ie_bus_barrier = NULL;
289 	sc->ie_bus_read16 = ie_obio_read16;
290 	sc->ie_bus_write16 = ie_obio_write16;
291 	sc->ie_bus_write24 = ie_obio_write24;
292 	sc->sc_msize = memsize = 65536; /* XXX */
293 
294 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr, sizeof(struct ieob),
295   			0, &bh))
296 		panic("ie_obio_attach: can't map regs");
297 	sc->sc_reg = (void *)bh;
298 
299 	/*
300 	 * Allocate control & buffer memory.
301 	 */
302 	if ((error = bus_dmamap_create(dmatag, memsize, 1, memsize, 0,
303 					BUS_DMA_NOWAIT|BUS_DMA_24BIT,
304 					&sc->sc_dmamap)) != 0) {
305 		printf("%s: DMA map create error %d\n",
306 			sc->sc_dev.dv_xname, error);
307 		return;
308 	}
309 	if ((error = bus_dmamem_alloc(dmatag, memsize, 64*1024, 0,
310 			     &seg, 1, &rseg,
311 			     BUS_DMA_NOWAIT | BUS_DMA_24BIT)) != 0) {
312 		printf("%s: DMA memory allocation error %d\n",
313 			self->dv_xname, error);
314 		return;
315 	}
316 
317 	/* Map DMA buffer in CPU addressable space */
318 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, memsize,
319 				    (void **)&sc->sc_maddr,
320 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
321 		printf("%s: DMA buffer map error %d\n",
322 			sc->sc_dev.dv_xname, error);
323 		bus_dmamem_free(dmatag, &seg, rseg);
324 		return;
325 	}
326 
327 	/* Load the segment */
328 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
329 				     sc->sc_maddr, memsize, NULL,
330 				     BUS_DMA_NOWAIT)) != 0) {
331 		printf("%s: DMA buffer map load error %d\n",
332 			sc->sc_dev.dv_xname, error);
333 		bus_dmamem_unmap(dmatag, sc->sc_maddr, memsize);
334 		bus_dmamem_free(dmatag, &seg, rseg);
335 		return;
336 	}
337 
338 	w16zero(sc->sc_maddr, memsize);
339 	sc->bh = (bus_space_handle_t)(sc->sc_maddr);
340 
341 	/*
342 	 * The i82586's 24-bit address space maps to all of
343 	 * KVA space ().  In addition, the SCP must appear
344 	 * at IE_SCP_ADDR within the 24-bit address space,
345 	 * i.e. at KVA  IEOB_ADBASE+IE_SCP_ADDR, at the very top of
346 	 * kernel space.  We double-map this last page to the first
347 	 * page (starting at `maddr') of the memory we allocate to the chip.
348 	 * (a side-effect of this double-map is that the ISCP and SCB
349 	 * structures also get aliased there, but we ignore this). The
350 	 * first page at `maddr' is only used for ISCP, SCB and the aliased
351 	 * SCP; the actual buffers start at maddr+PAGE_SIZE.
352 	 *
353 	 * In a picture:
354 
355 	|---//--- ISCP-SCB-----scp-|--//- buffers -//-|... |iscp-scb-----SCP-|
356 	|         |                |                  |    |             |   |
357 	|         |<---PAGE_SIZE-->|                  |    |<--PAGE_SIZE-+-->|
358 	|         |<------------ memsize ------------>|    |       ^     |
359 	|         |                                                |     |
360 	|         \@maddr                                 (last page dbl mapped)
361 	|                                                                |
362 	\@IEOB_ADBASE                           @IEOB_ADBASE+IE_SCP_ADDR-+
363 
364 	 *
365 	 */
366 
367 	/* Double map the SCP */
368 	if (pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_maddr, &pa) == false)
369 		panic("ie pmap_extract");
370 
371 	pmap_enter(pmap_kernel(), m68k_trunc_page(IEOB_ADBASE+IE_SCP_ADDR),
372 	    pa | PMAP_NC /*| PMAP_IOC*/,
373 	    VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
374 
375 	/* Map iscp at location 0 (relative to `maddr') */
376 	sc->iscp = 0;
377 
378 	/* scb follows iscp */
379 	sc->scb = IE_ISCP_SZ;
380 
381 	/* scp is at the fixed location IE_SCP_ADDR (modulo the page size) */
382 	sc->scp = IE_SCP_ADDR & PGOFSET;
383 
384 	/* Calculate the 24-bit base of i82586 operations */
385 	iebase = (u_long)sc->sc_dmamap->dm_segs[0].ds_addr -
386 			(u_long)IEOB_ADBASE;
387 	ie_obio_write16(sc, IE_ISCP_SCB(sc->iscp), sc->scb);
388 	ie_obio_write24(sc, IE_ISCP_BASE(sc->iscp), iebase);
389 	ie_obio_write24(sc, IE_SCP_ISCP(sc->scp), iebase + sc->iscp);
390 
391 	/*
392 	 * Rest of first page is unused (wasted!); the other pages
393 	 * are used for buffers.
394 	 */
395 	sc->buf_area = PAGE_SIZE;
396 	sc->buf_area_sz = memsize - PAGE_SIZE;
397 
398 	if (i82586_proberam(sc) == 0) {
399 		printf(": memory probe failed\n");
400 		return;
401 	}
402 
403 	idprom_etheraddr(myaddr);
404 	i82586_attach(sc, "onboard", myaddr, media, NMEDIA, media[0]);
405 
406 	/* Establish interrupt channel */
407 	ih = bus_intr_establish(oba->oba_bustag, oba->oba_pri, IPL_NET, 0,
408 				i82586_intr, sc);
409 }
410