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