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