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