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