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