if_cs_ofisa.c revision 1.2
1/*	$NetBSD: if_cs_ofisa.c,v 1.2 1998/08/15 02:59:01 thorpej 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 <net/if.h>
47#include <net/if_ether.h>
48#include <net/if_media.h>
49#ifdef INET
50#include <netinet/in.h>
51#include <netinet/if_inarp.h>
52#endif
53
54#include <machine/bus.h>
55#include <machine/intr.h>
56
57#include <dev/ofw/openfirm.h>
58#include <dev/isa/isavar.h>
59#include <dev/ofisa/ofisavar.h>
60
61#include <dev/isa/cs89x0reg.h>
62#include <dev/isa/cs89x0var.h>
63
64int	cs_ofisa_match __P((struct device *, struct cfdata *, void *));
65void	cs_ofisa_attach __P((struct device *, struct device *, void *));
66
67struct cfattach cs_ofisa_ca = {
68	sizeof(struct cs_softc), cs_ofisa_match, cs_ofisa_attach
69};
70
71int
72cs_ofisa_match(parent, cf, aux)
73	struct device *parent;
74	struct cfdata *cf;
75	void *aux;
76{
77	struct ofisa_attach_args *aa = aux;
78	const char *compatible_strings[] = {
79		"CRUS,CS8900",
80		/* XXX CS8920, CS8920M? */
81		/* XXX PNP names? */
82		NULL,
83	};
84	int rv = 0;
85
86	if (of_compatible(aa->oba.oba_phandle, compatible_strings) != -1)
87		rv = 5;
88#ifdef _CS_OFISA_MD_MATCH
89	if (rv == 0)
90		rv = cs_ofisa_md_match(parent, cf, aux);
91#endif
92	return (rv);
93}
94
95void
96cs_ofisa_attach(parent, self, aux)
97	struct device *parent, *self;
98	void *aux;
99{
100	struct cs_softc *sc = (struct cs_softc *) self;
101	struct ofisa_attach_args *aa = aux;
102	struct ofisa_reg_desc reg[2];
103	struct ofisa_intr_desc intr;
104	struct ofisa_dma_desc dma;
105	int i, n, *media, nmedia, defmedia;
106	bus_addr_t io_addr, mem_addr;
107	char *model = NULL;
108	const char *message = NULL;
109	u_int8_t enaddr[6];
110
111	sc->sc_ic = aa->ic;
112	sc->sc_iot = aa->iot;
113	sc->sc_memt = aa->memt;
114
115	/*
116	 * We're living on an OFW.  We have to ask the OFW what our
117	 * registers and interrupts properties look like.
118	 *
119	 * We expect:
120	 *
121	 *	1 i/o register region
122	 *	0 or 1 memory region
123	 *	1 interrupt
124	 *	0 or 1 dma channel
125	 */
126
127	io_addr = mem_addr = -1;
128
129	n = ofisa_reg_get(aa->oba.oba_phandle, reg, 2);
130#ifdef _CS_OFISA_MD_REG_FIXUP
131	n = cs_ofisa_md_reg_fixup(parent, self, aux, &reg, 2, n);
132#endif
133	if (n < 1 || n > 2) {
134		printf(": error getting register data\n");
135		return;
136	}
137
138	for (i = 0; i < n; i++) {
139		if (reg[i].type == OFISA_REG_TYPE_IO) {
140			if (io_addr != (bus_addr_t) -1) {
141				printf(": multiple I/O regions\n");
142				return;
143			}
144			if (reg[i].len != CS8900_IOSIZE) {
145				printf(": weird register size (%lu, expected %d)\n",
146				    (unsigned long)reg[i].len, CS8900_IOSIZE);
147				return;
148			}
149			io_addr = reg[i].addr;
150		} else {
151			if (mem_addr != (bus_addr_t) -1) {
152				printf(": multiple memory regions\n");
153				return;
154			}
155			if (reg[i].len != CS8900_MEMSIZE) {
156				printf(": weird register size (%lu, expected %d)\n",
157				    (unsigned long)reg[i].len, CS8900_MEMSIZE);
158				return;
159			}
160			mem_addr = reg[i].addr;
161		}
162	}
163
164	n = ofisa_intr_get(aa->oba.oba_phandle, &intr, 1);
165#ifdef _CS_OFISA_MD_INTR_FIXUP
166	n = cs_ofisa_md_intr_fixup(parent, self, aux, &intr, 1, n);
167#endif
168	if (n != 1) {
169		printf(": error getting interrupt data\n");
170		return;
171	}
172	sc->sc_irq = intr.irq;
173
174	if (CS8900_IRQ_ISVALID(sc->sc_irq) == 0) {
175		printf(": invalid IRQ %d\n", sc->sc_irq);
176		return;
177	}
178
179	sc->sc_drq = ISACF_DRQ_DEFAULT;
180	n = ofisa_dma_get(aa->oba.oba_phandle, &dma, 1);
181#ifdef _CS_OFISA_MD_DMA_FIXUP
182	n = cs_ofisa_md_dma_fixup(parent, self, aux, &dma, 1, n);
183#endif
184	if (n == 1)
185		sc->sc_drq = dma.drq;
186
187	if (io_addr == (bus_addr_t) -1) {
188		printf(": no I/O space\n");
189		return;
190	}
191	if (bus_space_map(sc->sc_iot, io_addr, CS8900_IOSIZE, 0,
192	    &sc->sc_ioh)) {
193		printf(": unable to map register space\n");
194		return;
195	}
196
197	if (mem_addr != (bus_addr_t) -1) {
198		if (bus_space_map(sc->sc_memt, mem_addr, CS8900_MEMSIZE, 0,
199		    &sc->sc_memh)) {
200			message = "unable to map memory space";
201		} else {
202			sc->sc_cfgflags |= CFGFLG_MEM_MODE;
203			sc->sc_pktpgaddr = mem_addr;
204		}
205	}
206
207	/* Dig MAC address out of the firmware. */
208	if (OF_getprop(aa->oba.oba_phandle, "mac-address", enaddr,
209	    sizeof(enaddr)) < 0) {
210		printf(": unable to get Ethernet address\n");
211		return;
212	}
213
214	/* Dig media out of the firmware. */
215	media = of_network_decode_media(aa->oba.oba_phandle, &nmedia,
216	    &defmedia);
217#ifdef _CS_OFISA_MD_MEDIA_FIXUP
218	media = cs_ofisa_md_media_fixup(parent, self, aux, media, &nmedia,
219	    &defmedia);
220#endif
221	if (media == NULL) {
222		printf(": unable to get media information\n");
223		return;
224	}
225
226	n = OF_getproplen(aa->oba.oba_phandle, "model");
227	if (n > 0) {
228		model = alloca(n);
229		if (OF_getprop(aa->oba.oba_phandle, "model", model, n) != n)
230			model = NULL;	/* Safe; alloca is on-stack */
231	}
232	if (model != NULL)
233		printf(": %s\n", model);
234	else
235		printf("\n");
236
237	if (message != NULL)
238		printf("%s: %s\n", sc->sc_dev.dv_xname, message);
239
240	if (defmedia == -1) {
241		printf("%s: unable to get default media\n",
242		    sc->sc_dev.dv_xname);
243		defmedia = media[0];	/* XXX What to do? */
244	}
245
246	sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, intr.share,
247	    IPL_NET, cs_intr, sc);
248	if (sc->sc_ih == NULL) {
249		printf("%s: unable to establish interrupt\n",
250		    sc->sc_dev.dv_xname);
251		return;
252	}
253
254#ifdef _CS_OFISA_MD_CFGFLAGS_FIXUP
255	sc->sc_cfgflags |= cs_ofisa_md_cfgflags_fixup(parent, self, aux);
256#endif
257
258	cs_attach(sc, enaddr, media, nmedia, defmedia);
259
260	/* This is malloc'd. */
261	free(media, M_DEVBUF);
262}
263