if_ae_nubus.c revision 1.17 1 /* $NetBSD: if_ae_nubus.c,v 1.17 1997/05/01 18:17:16 briggs Exp $ */
2
3 /*
4 * Copyright (C) 1997 Scott Reynolds
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Scott Reynolds for
18 * the NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 /*
34 * Some parts are derived from code adapted for MacBSD by Brad Parker
35 * <brad (at) fcr.com>.
36 *
37 * Currently supports:
38 * Apple NB Ethernet Card
39 * Apple NB Ethernet Card II
40 * Interlan A310 NuBus Ethernet card
41 * Cayman Systems GatorCard
42 * Asante MacCon II/E
43 * Kinetics EtherPort SE/30
44 */
45
46 #include <sys/param.h>
47 #include <sys/device.h>
48 #include <sys/errno.h>
49 #include <sys/ioctl.h>
50 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/syslog.h>
53 #include <sys/systm.h>
54
55 #include <net/if.h>
56 #include <net/if_ether.h>
57
58 #include <machine/bus.h>
59 #include <machine/viareg.h>
60
61 #include "nubus.h"
62 #include <dev/ic/dp8390reg.h>
63 #include <dev/ic/dp8390var.h>
64 #include <arch/mac68k/dev/if_aevar.h>
65 #include <arch/mac68k/dev/if_aereg.h>
66
67 static int ae_nubus_match __P((struct device *, struct cfdata *, void *));
68 static void ae_nubus_attach __P((struct device *, struct device *, void *));
69 static int ae_nb_card_vendor __P((struct nubus_attach_args *));
70 static int ae_nb_get_enaddr __P((struct nubus_attach_args *, u_int8_t *));
71 #ifdef DEBUG
72 static void ae_nb_watchdog __P((struct ifnet *));
73 #endif
74
75 struct cfattach ae_nubus_ca = {
76 sizeof(struct dp8390_softc), ae_nubus_match, ae_nubus_attach
77 };
78
79 static int
80 ae_nubus_match(parent, cf, aux)
81 struct device *parent;
82 struct cfdata *cf;
83 void *aux;
84 {
85 struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
86 bus_space_handle_t bsh;
87 int rv;
88
89 if (bus_space_map(na->na_tag, NUBUS_SLOT2PA(na->slot), NBMEMSIZE,
90 0, &bsh))
91 return (0);
92
93 rv = 0;
94
95 if (na->category == NUBUS_CATEGORY_NETWORK &&
96 na->type == NUBUS_TYPE_ETHERNET) {
97 switch (ae_nb_card_vendor(na)) {
98 case DP8390_VENDOR_APPLE:
99 case DP8390_VENDOR_ASANTE:
100 case DP8390_VENDOR_FARALLON:
101 case DP8390_VENDOR_INTERLAN:
102 case DP8390_VENDOR_KINETICS:
103 rv = 1;
104 break;
105 case DP8390_VENDOR_DAYNA:
106 case DP8390_VENDOR_FOCUS:
107 rv = UNSUPP;
108 break;
109 default:
110 break;
111 }
112 }
113
114 bus_space_unmap(na->na_tag, bsh, NBMEMSIZE);
115
116 return rv;
117 }
118
119 /*
120 * Install interface into kernel networking data structures
121 */
122 static void
123 ae_nubus_attach(parent, self, aux)
124 struct device *parent, *self;
125 void *aux;
126 {
127 struct dp8390_softc *sc = (struct dp8390_softc *)self;
128 struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
129 #ifdef DEBUG
130 struct ifnet *ifp = &sc->sc_ec.ec_if;
131 #endif
132 bus_space_tag_t bst;
133 bus_space_handle_t bsh;
134 int i, success;
135 char *cardtype;
136
137 bst = na->na_tag;
138 if (bus_space_map(bst, NUBUS_SLOT2PA(na->slot), NBMEMSIZE,
139 0, &bsh)) {
140 printf(": can't map memory space\n");
141 return;
142 }
143
144 sc->sc_regt = sc->sc_buft = bst;
145 sc->sc_flags = self->dv_cfdata->cf_flags;
146
147 sc->vendor = ae_nb_card_vendor(na);
148 sc->type = 0;
149 cardtype = nubus_get_card_name(na->fmt);
150 i = strlen(cardtype);
151 if ((sc->type_str = malloc((u_long)(i + 1), M_DEVBUF, M_NOWAIT))) {
152 strncpy(sc->type_str, cardtype, i);
153 sc->type_str[i] = '\0';
154 }
155
156 sc->is790 = 0;
157
158 sc->mem_start = 0;
159 sc->mem_size = 0;
160
161 success = 0;
162
163 switch (sc->vendor) {
164 case DP8390_VENDOR_APPLE: /* Apple-compatible cards */
165 case DP8390_VENDOR_ASANTE:
166 /* Map register offsets */
167 for (i = 0; i < 16; i++) /* reverse order, longword aligned */
168 sc->sc_reg_map[i] = (15 - i) << 2;
169
170 sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
171 if (bus_space_subregion(bst, bsh,
172 AE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
173 printf(": failed to map register space\n");
174 break;
175 }
176 if ((sc->mem_size = ae_size_card_memory(bst, bsh,
177 AE_DATA_OFFSET)) == 0) {
178 printf(": failed to determine size of RAM.\n");
179 break;
180 }
181 if (bus_space_subregion(bst, bsh,
182 AE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
183 printf(": failed to map register space\n");
184 break;
185 }
186 #ifdef AE_OLD_GET_ENADDR
187 /* Get station address from on-board ROM */
188 for (i = 0; i < ETHER_ADDR_LEN; ++i)
189 sc->sc_enaddr[i] =
190 bus_space_read_1(bst, bsh, (AE_ROM_OFFSET + i * 2));
191 #else
192 if (ae_nb_get_enaddr(na, sc->sc_enaddr)) {
193 printf(": can't find MAC address\n");
194 break;
195 }
196 #endif
197
198 success = 1;
199 break;
200
201 case DP8390_VENDOR_DAYNA:
202 /* Map register offsets */
203 for (i = 0; i < 16; i++) /* normal order, longword aligned */
204 sc->sc_reg_map[i] = i << 2;
205
206 sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
207 if (bus_space_subregion(bst, bsh,
208 DP_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
209 printf(": failed to map register space\n");
210 break;
211 }
212 sc->mem_size = 8192;
213 if (bus_space_subregion(bst, bsh,
214 DP_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
215 printf(": failed to map register space\n");
216 break;
217 }
218 #ifdef AE_OLD_GET_ENADDR
219 /* Get station address from on-board ROM */
220 for (i = 0; i < ETHER_ADDR_LEN; ++i)
221 sc->sc_enaddr[i] =
222 bus_space_read_1(bst, bsh, (DP_ROM_OFFSET + i * 2));
223 #else
224 if (ae_nb_get_enaddr(na, sc->sc_enaddr)) {
225 printf(": can't find MAC address\n");
226 break;
227 }
228 #endif
229
230 printf(": unsupported Dayna hardware\n");
231 break;
232
233 case DP8390_VENDOR_FARALLON:
234 /* Map register offsets */
235 for (i = 0; i < 16; i++) /* reverse order, longword aligned */
236 sc->sc_reg_map[i] = (15 - i) << 2;
237
238 sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
239 if (bus_space_subregion(bst, bsh,
240 AE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
241 printf(": failed to map register space\n");
242 break;
243 }
244 if ((sc->mem_size = ae_size_card_memory(bst, bsh,
245 AE_DATA_OFFSET)) == 0) {
246 printf(": failed to determine size of RAM.\n");
247 break;
248 }
249 if (bus_space_subregion(bst, bsh,
250 AE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
251 printf(": failed to map register space\n");
252 break;
253 }
254 #ifdef AE_OLD_GET_ENADDR
255 /* Get station address from on-board ROM */
256 for (i = 0; i < ETHER_ADDR_LEN; ++i)
257 sc->sc_enaddr[i] =
258 bus_space_read_1(bst, bsh, (FE_ROM_OFFSET + i));
259 #else
260 if (ae_nb_get_enaddr(na, sc->sc_enaddr)) {
261 printf(": can't find MAC address\n");
262 break;
263 }
264 #endif
265
266 success = 1;
267 break;
268
269 case DP8390_VENDOR_FOCUS:
270 printf(": unsupported Focus hardware\n");
271 break;
272
273 case DP8390_VENDOR_INTERLAN:
274 /* Map register offsets */
275 for (i = 0; i < 16; i++) /* normal order, longword aligned */
276 sc->sc_reg_map[i] = i << 2;
277
278 sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
279 if (bus_space_subregion(bst, bsh,
280 GC_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
281 printf(": failed to map register space\n");
282 break;
283 }
284 if ((sc->mem_size = ae_size_card_memory(bst, bsh,
285 GC_DATA_OFFSET)) == 0) {
286 printf(": failed to determine size of RAM.\n");
287 break;
288 }
289 if (bus_space_subregion(bst, bsh,
290 GC_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
291 printf(": failed to map register space\n");
292 break;
293 }
294
295 /* reset the NIC chip */
296 bus_space_write_1(bst, bsh, GC_RESET_OFFSET, 0);
297
298 if (ae_nb_get_enaddr(na, sc->sc_enaddr)) {
299 /* Fall back to snarf directly from ROM. Ick. */
300 for (i = 0; i < ETHER_ADDR_LEN; ++i)
301 sc->sc_enaddr[i] =
302 bus_space_read_1(bst, bsh,
303 (GC_ROM_OFFSET + i * 4));
304 }
305
306 success = 1;
307 break;
308
309 case DP8390_VENDOR_KINETICS:
310 /* Map register offsets */
311 for (i = 0; i < 16; i++) /* normal order, longword aligned */
312 sc->sc_reg_map[i] = i << 2;
313
314 if (bus_space_subregion(bst, bsh,
315 KE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
316 printf(": failed to map register space\n");
317 break;
318 }
319 if ((sc->mem_size = ae_size_card_memory(bst, bsh,
320 KE_DATA_OFFSET)) == 0) {
321 printf(": failed to determine size of RAM.\n");
322 break;
323 }
324 if (bus_space_subregion(bst, bsh,
325 KE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
326 printf(": failed to map register space\n");
327 break;
328 }
329 if (ae_nb_get_enaddr(na, sc->sc_enaddr)) {
330 printf(": can't find MAC address\n");
331 break;
332 }
333
334 success = 1;
335 break;
336
337 default:
338 break;
339 }
340
341 if (!success) {
342 bus_space_unmap(bst, bsh, NBMEMSIZE);
343 return;
344 }
345
346 /*
347 * Override test_mem and write_mbuf functions; other defaults
348 * already work properly.
349 */
350 sc->test_mem = ae_test_mem;
351 sc->write_mbuf = ae_write_mbuf;
352 #ifdef DEBUG
353 ifp->if_watchdog = ae_nb_watchdog; /* Override watchdog */
354 #endif
355
356 if (dp8390_config(sc)) {
357 bus_space_unmap(bst, bsh, NBMEMSIZE);
358 return;
359 }
360
361 printf("%dKB memory\n", sc->mem_size / 1024);
362
363 /* make sure interrupts are vectored to us */
364 add_nubus_intr(na->slot, dp8390_intr, sc);
365 }
366
367 static int
368 ae_nb_card_vendor(na)
369 struct nubus_attach_args *na;
370 {
371 int vendor;
372
373 switch (na->drsw) {
374 case NUBUS_DRSW_3COM:
375 switch (na->drhw) {
376 case NUBUS_DRHW_APPLE_SN:
377 case NUBUS_DRHW_APPLE_SNT:
378 vendor = DP8390_VENDOR_UNKNOWN;
379 break;
380 default:
381 vendor = DP8390_VENDOR_APPLE;
382 break;
383 }
384 break;
385 case NUBUS_DRSW_APPLE:
386 case NUBUS_DRSW_TECHWORKS:
387 vendor = DP8390_VENDOR_APPLE;
388 break;
389 case NUBUS_DRSW_ASANTE:
390 vendor = DP8390_VENDOR_ASANTE;
391 break;
392 case NUBUS_DRSW_FARALLON:
393 vendor = DP8390_VENDOR_FARALLON;
394 break;
395 case NUBUS_DRSW_FOCUS:
396 vendor = DP8390_VENDOR_FOCUS;
397 break;
398 case NUBUS_DRSW_GATOR:
399 switch (na->drhw) {
400 default:
401 case NUBUS_DRHW_INTERLAN:
402 vendor = DP8390_VENDOR_INTERLAN;
403 break;
404 case NUBUS_DRHW_KINETICS:
405 if (strncmp(
406 nubus_get_card_name(na->fmt), "EtherPort", 9) == 0)
407 vendor = DP8390_VENDOR_KINETICS;
408 else
409 vendor = DP8390_VENDOR_DAYNA;
410 break;
411 }
412 break;
413 default:
414 vendor = DP8390_VENDOR_UNKNOWN;
415 }
416 return vendor;
417 }
418
419 static int
420 ae_nb_get_enaddr(na, ep)
421 struct nubus_attach_args *na;
422 u_int8_t *ep;
423 {
424 nubus_dir dir;
425 nubus_dirent dirent;
426
427 /*
428 * XXX - note hardwired resource IDs here (0x80); these are
429 * assumed to be used by all cards, but should be fixed when
430 * we find out more about Ethernet card resources.
431 */
432 nubus_get_main_dir(na->fmt, &dir);
433 if (nubus_find_rsrc(na->fmt, &dir, 0x80, &dirent) <= 0)
434 return 1;
435 nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
436 if (nubus_find_rsrc(na->fmt, &dir, 0x80, &dirent) <= 0)
437 return 1;
438 if (nubus_get_ind_data(na->fmt, &dirent, ep, ETHER_ADDR_LEN) <= 0)
439 return 1;
440
441 return 0;
442 }
443
444 #ifdef DEBUG
445 static void
446 ae_nb_watchdog(ifp)
447 struct ifnet *ifp;
448 {
449 struct dp8390_softc *sc = ifp->if_softc;
450
451 /*
452 * This is a kludge! The via code seems to miss slot interrupts
453 * sometimes. This kludges around that by calling the handler
454 * by hand if the watchdog is activated. -- XXX (akb)
455 */
456 (*via2itab[1])((void *) 1);
457
458 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
459 ++ifp->if_oerrors;
460
461 dp8390_reset(sc);
462 }
463 #endif
464