if_cs_ofisa.c revision 1.4
1/*	$NetBSD: if_cs_ofisa.c,v 1.4 2000/12/26 09:42:21 mycroft Exp $	*/
2
3/*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the NetBSD
22 *	Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/socket.h>
43#include <sys/device.h>
44#include <sys/malloc.h>
45
46#include "rnd.h"
47#if NRND > 0
48#include <sys/rnd.h>
49#endif
50
51#include <net/if.h>
52#include <net/if_ether.h>
53#include <net/if_media.h>
54#ifdef INET
55#include <netinet/in.h>
56#include <netinet/if_inarp.h>
57#endif
58
59#include <machine/bus.h>
60#include <machine/intr.h>
61
62#include <dev/ofw/openfirm.h>
63#include <dev/isa/isavar.h>
64#include <dev/ofisa/ofisavar.h>
65
66#include <dev/isa/cs89x0reg.h>
67#include <dev/isa/cs89x0var.h>
68
69int	cs_ofisa_match __P((struct device *, struct cfdata *, void *));
70void	cs_ofisa_attach __P((struct device *, struct device *, void *));
71
72struct cfattach cs_ofisa_ca = {
73	sizeof(struct cs_softc), cs_ofisa_match, cs_ofisa_attach
74};
75
76int
77cs_ofisa_match(parent, cf, aux)
78	struct device *parent;
79	struct cfdata *cf;
80	void *aux;
81{
82	struct ofisa_attach_args *aa = aux;
83	const char *compatible_strings[] = {
84		"CRUS,CS8900",
85		/* XXX CS8920, CS8920M? */
86		/* XXX PNP names? */
87		NULL,
88	};
89	int rv = 0;
90
91	if (of_compatible(aa->oba.oba_phandle, compatible_strings) != -1)
92		rv = 5;
93#ifdef _CS_OFISA_MD_MATCH
94	if (rv == 0)
95		rv = cs_ofisa_md_match(parent, cf, aux);
96#endif
97	return (rv);
98}
99
100void
101cs_ofisa_attach(parent, self, aux)
102	struct device *parent, *self;
103	void *aux;
104{
105	struct cs_softc *sc = (struct cs_softc *) self;
106	struct ofisa_attach_args *aa = aux;
107	struct ofisa_reg_desc reg[2];
108	struct ofisa_intr_desc intr;
109	struct ofisa_dma_desc dma;
110	int i, n, *media, nmedia, defmedia;
111	bus_addr_t io_addr, mem_addr;
112	char *model = NULL;
113	const char *message = NULL;
114	u_int8_t enaddr[6];
115
116	sc->sc_ic = aa->ic;
117	sc->sc_iot = aa->iot;
118	sc->sc_memt = aa->memt;
119
120	/*
121	 * We're living on an OFW.  We have to ask the OFW what our
122	 * registers and interrupts properties look like.
123	 *
124	 * We expect:
125	 *
126	 *	1 i/o register region
127	 *	0 or 1 memory region
128	 *	1 interrupt
129	 *	0 or 1 dma channel
130	 */
131
132	io_addr = mem_addr = -1;
133
134	n = ofisa_reg_get(aa->oba.oba_phandle, reg, 2);
135#ifdef _CS_OFISA_MD_REG_FIXUP
136	n = cs_ofisa_md_reg_fixup(parent, self, aux, reg, 2, n);
137#endif
138	if (n < 1 || n > 2) {
139		printf(": error getting register data\n");
140		return;
141	}
142
143	for (i = 0; i < n; i++) {
144		if (reg[i].type == OFISA_REG_TYPE_IO) {
145			if (io_addr != (bus_addr_t) -1) {
146				printf(": multiple I/O regions\n");
147				return;
148			}
149			if (reg[i].len != CS8900_IOSIZE) {
150				printf(": weird register size (%lu, expected %d)\n",
151				    (unsigned long)reg[i].len, CS8900_IOSIZE);
152				return;
153			}
154			io_addr = reg[i].addr;
155		} else {
156			if (mem_addr != (bus_addr_t) -1) {
157				printf(": multiple memory regions\n");
158				return;
159			}
160			if (reg[i].len != CS8900_MEMSIZE) {
161				printf(": weird register size (%lu, expected %d)\n",
162				    (unsigned long)reg[i].len, CS8900_MEMSIZE);
163				return;
164			}
165			mem_addr = reg[i].addr;
166		}
167	}
168
169	n = ofisa_intr_get(aa->oba.oba_phandle, &intr, 1);
170#ifdef _CS_OFISA_MD_INTR_FIXUP
171	n = cs_ofisa_md_intr_fixup(parent, self, aux, &intr, 1, n);
172#endif
173	if (n != 1) {
174		printf(": error getting interrupt data\n");
175		return;
176	}
177	sc->sc_irq = intr.irq;
178
179	if (CS8900_IRQ_ISVALID(sc->sc_irq) == 0) {
180		printf(": invalid IRQ %d\n", sc->sc_irq);
181		return;
182	}
183
184	sc->sc_drq = ISACF_DRQ_DEFAULT;
185	n = ofisa_dma_get(aa->oba.oba_phandle, &dma, 1);
186#ifdef _CS_OFISA_MD_DMA_FIXUP
187	n = cs_ofisa_md_dma_fixup(parent, self, aux, &dma, 1, n);
188#endif
189	if (n == 1)
190		sc->sc_drq = dma.drq;
191
192	if (io_addr == (bus_addr_t) -1) {
193		printf(": no I/O space\n");
194		return;
195	}
196	if (bus_space_map(sc->sc_iot, io_addr, CS8900_IOSIZE, 0,
197	    &sc->sc_ioh)) {
198		printf(": unable to map register space\n");
199		return;
200	}
201
202	if (mem_addr != (bus_addr_t) -1) {
203		if (bus_space_map(sc->sc_memt, mem_addr, CS8900_MEMSIZE, 0,
204		    &sc->sc_memh)) {
205			message = "unable to map memory space";
206		} else {
207			sc->sc_cfgflags |= CFGFLG_MEM_MODE;
208			sc->sc_pktpgaddr = mem_addr;
209		}
210	}
211
212	/* Dig MAC address out of the firmware. */
213	if (OF_getprop(aa->oba.oba_phandle, "mac-address", enaddr,
214	    sizeof(enaddr)) < 0) {
215		printf(": unable to get Ethernet address\n");
216		return;
217	}
218
219	/* Dig media out of the firmware. */
220	media = of_network_decode_media(aa->oba.oba_phandle, &nmedia,
221	    &defmedia);
222#ifdef _CS_OFISA_MD_MEDIA_FIXUP
223	media = cs_ofisa_md_media_fixup(parent, self, aux, media, &nmedia,
224	    &defmedia);
225#endif
226	if (media == NULL) {
227		printf(": unable to get media information\n");
228		return;
229	}
230
231	n = OF_getproplen(aa->oba.oba_phandle, "model");
232	if (n > 0) {
233		model = alloca(n);
234		if (OF_getprop(aa->oba.oba_phandle, "model", model, n) != n)
235			model = NULL;	/* Safe; alloca is on-stack */
236	}
237	if (model != NULL)
238		printf(": %s\n", model);
239	else
240		printf("\n");
241
242	if (message != NULL)
243		printf("%s: %s\n", sc->sc_dev.dv_xname, message);
244
245	if (defmedia == -1) {
246		printf("%s: unable to get default media\n",
247		    sc->sc_dev.dv_xname);
248		defmedia = media[0];	/* XXX What to do? */
249	}
250
251	sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, intr.share,
252	    IPL_NET, cs_intr, sc);
253	if (sc->sc_ih == NULL) {
254		printf("%s: unable to establish interrupt\n",
255		    sc->sc_dev.dv_xname);
256		return;
257	}
258
259#ifdef _CS_OFISA_MD_CFGFLAGS_FIXUP
260	sc->sc_cfgflags |= cs_ofisa_md_cfgflags_fixup(parent, self, aux);
261#endif
262
263	cs_attach(sc, enaddr, media, nmedia, defmedia);
264
265	/* This is malloc'd. */
266	free(media, M_DEVBUF);
267}
268