we.c revision 1.3 1 /* $NetBSD: we.c,v 1.3 2001/07/04 11:14:11 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 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 /*
41 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
42 * adapters.
43 *
44 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
45 *
46 * Copyright (C) 1993, David Greenman. This software may be used, modified,
47 * copied, distributed, and sold, in both source and binary form provided that
48 * the above copyright and these terms are retained. Under no circumstances is
49 * the author responsible for the proper functioning of this software, nor does
50 * the author assume any responsibility for damages incurred with its use.
51 */
52
53 /*
54 * Device driver for the Western Digital/SMC 8003 and 8013 series,
55 * and the SMC Elite Ultra (8216).
56 */
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/device.h>
61 #include <sys/socket.h>
62 #include <sys/mbuf.h>
63 #include <sys/syslog.h>
64
65 #include <net/if.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/if_media.h>
69
70 #include <net/if_ether.h>
71
72 #include <machine/bus.h>
73 #include <machine/bswap.h>
74 #include <machine/intr.h>
75
76 #include <dev/isa/isareg.h>
77 #include <dev/isa/isavar.h>
78
79 #include <dev/ic/dp8390reg.h>
80 #include <dev/ic/dp8390var.h>
81 #include <dev/ic/wereg.h>
82 #include <dev/ic/wevar.h>
83
84 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
85 #define bus_space_read_region_stream_2 bus_space_read_region_2
86 #define bus_space_write_stream_2 bus_space_write_2
87 #define bus_space_write_region_stream_2 bus_space_write_region_2
88 #endif
89
90 static void we_set_media __P((struct we_softc *, int));
91
92 static void we_media_init __P((struct dp8390_softc *));
93
94 static int we_mediachange __P((struct dp8390_softc *));
95 static void we_mediastatus __P((struct dp8390_softc *, struct ifmediareq *));
96
97 static void we_recv_int __P((struct dp8390_softc *));
98 static void we_init_card __P((struct dp8390_softc *));
99 static int we_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
100 static int we_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
101 static void we_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
102 static int we_test_mem __P((struct dp8390_softc *));
103
104 static __inline void we_readmem __P((struct we_softc *, int, u_int8_t *, int));
105
106 /*
107 * Delay needed when switching 16-bit access to shared memory.
108 */
109 #define WE_DELAY(wsc) delay(3)
110
111 /*
112 * Enable card RAM, and 16-bit access.
113 */
114 #define WE_MEM_ENABLE(wsc) \
115 do { \
116 if ((wsc)->sc_16bitp) \
117 bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
118 WE_LAAR, (wsc)->sc_laar_proto | WE_LAAR_M16EN); \
119 bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
120 WE_MSR, wsc->sc_msr_proto | WE_MSR_MENB); \
121 WE_DELAY((wsc)); \
122 } while (0)
123
124 /*
125 * Disable card RAM, and 16-bit access.
126 */
127 #define WE_MEM_DISABLE(wsc) \
128 do { \
129 bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
130 WE_MSR, (wsc)->sc_msr_proto); \
131 if ((wsc)->sc_16bitp) \
132 bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
133 WE_LAAR, (wsc)->sc_laar_proto); \
134 WE_DELAY((wsc)); \
135 } while (0)
136
137 int
138 we_config(self, wsc, typestr)
139 struct device *self;
140 struct we_softc *wsc;
141 const char *typestr;
142 {
143 struct dp8390_softc *sc = &wsc->sc_dp8390;
144 u_int8_t x;
145 int i, forced_16bit = 0;
146
147 /*
148 * Allow user to override 16-bit mode. 8-bit takes precedence.
149 */
150 if (self->dv_cfdata->cf_flags & DP8390_FORCE_16BIT_MODE) {
151 wsc->sc_16bitp = 1;
152 forced_16bit = 1;
153 }
154 if (self->dv_cfdata->cf_flags & DP8390_FORCE_8BIT_MODE)
155 wsc->sc_16bitp = 0;
156
157 /* Registers are linear. */
158 for (i = 0; i < 16; i++)
159 sc->sc_reg_map[i] = i;
160
161 /* Now we can use the NIC_{GET,PUT}() macros. */
162
163 printf("%s: %s Ethernet (%s-bit)\n", sc->sc_dev.dv_xname,
164 typestr, wsc->sc_16bitp ? "16" : "8");
165
166 /* Get station address from EEPROM. */
167 for (i = 0; i < ETHER_ADDR_LEN; i++)
168 sc->sc_enaddr[i] = bus_space_read_1(wsc->sc_asict,
169 wsc->sc_asich, WE_PROM + i);
170
171 /*
172 * Set upper address bits and 8/16 bit access to shared memory.
173 */
174 if (sc->is790) {
175 wsc->sc_laar_proto =
176 bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_LAAR) &
177 ~WE_LAAR_M16EN;
178 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_LAAR,
179 wsc->sc_laar_proto | (wsc->sc_16bitp ? WE_LAAR_M16EN : 0));
180 } else if ((wsc->sc_type & WE_SOFTCONFIG) ||
181 #ifdef TOSH_ETHER
182 (wsc->sc_type == WE_TYPE_TOSHIBA1) ||
183 (wsc->sc_type == WE_TYPE_TOSHIBA4) ||
184 #endif
185 (forced_16bit) ||
186 (wsc->sc_type == WE_TYPE_WD8013EBT)) {
187 wsc->sc_laar_proto = (wsc->sc_maddr >> 19) & WE_LAAR_ADDRHI;
188 if (wsc->sc_16bitp)
189 wsc->sc_laar_proto |= WE_LAAR_L16EN;
190 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_LAAR,
191 wsc->sc_laar_proto | (wsc->sc_16bitp ? WE_LAAR_M16EN : 0));
192 }
193
194 /*
195 * Set address and enable interface shared memory.
196 */
197 if (sc->is790) {
198 /* XXX MAGIC CONSTANTS XXX */
199 x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, 0x04);
200 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, 0x04, x | 0x80);
201 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, 0x0b,
202 ((wsc->sc_maddr >> 13) & 0x0f) |
203 ((wsc->sc_maddr >> 11) & 0x40) |
204 (bus_space_read_1(wsc->sc_asict, wsc->sc_asich, 0x0b) & 0xb0));
205 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, 0x04, x);
206 wsc->sc_msr_proto = 0x00;
207 sc->cr_proto = 0x00;
208 } else {
209 #ifdef TOSH_ETHER
210 if (wsc->sc_type == WE_TYPE_TOSHIBA1 ||
211 wsc->sc_type == WE_TYPE_TOSHIBA4) {
212 bus_space_write_1(wsc->sc_asict, wsc->sc_asich,
213 WE_MSR + 1,
214 ((wsc->sc_maddr >> 8) & 0xe0) | 0x04);
215 bus_space_write_1(wsc->sc_asict, wsc->sc_asich,
216 WE_MSR + 2,
217 ((wsc->sc_maddr >> 16) & 0x0f));
218 wsc->sc_msr_proto = WE_MSR_POW;
219 } else
220 #endif
221 wsc->sc_msr_proto = (wsc->sc_maddr >> 13) &
222 WE_MSR_ADDR;
223
224 sc->cr_proto = ED_CR_RD2;
225 }
226
227 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_MSR,
228 wsc->sc_msr_proto | WE_MSR_MENB);
229 WE_DELAY(wsc);
230
231 /*
232 * DCR gets:
233 *
234 * FIFO threshold to 8, No auto-init Remote DMA,
235 * byte order=80x86.
236 *
237 * 16-bit cards also get word-wide DMA transfers.
238 */
239 sc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
240 (wsc->sc_16bitp ? ED_DCR_WTS : 0);
241
242 sc->test_mem = we_test_mem;
243 sc->ring_copy = we_ring_copy;
244 sc->write_mbuf = we_write_mbuf;
245 sc->read_hdr = we_read_hdr;
246 sc->recv_int = we_recv_int;
247 sc->init_card = we_init_card;
248
249 sc->sc_mediachange = we_mediachange;
250 sc->sc_mediastatus = we_mediastatus;
251
252 sc->mem_start = 0;
253 /* sc->mem_size has to be set by frontend */
254
255 sc->sc_flags = self->dv_cfdata->cf_flags;
256
257 /* Do generic parts of attach. */
258 if (wsc->sc_type & WE_SOFTCONFIG)
259 sc->sc_media_init = we_media_init;
260 else
261 sc->sc_media_init = dp8390_media_init;
262 if (dp8390_config(sc)) {
263 printf("%s: configuration failed\n", sc->sc_dev.dv_xname);
264 return (1);
265 }
266
267 /*
268 * Disable 16-bit access to shared memory - we leave it disabled
269 * so that:
270 *
271 * (1) machines reboot properly when the board is set to
272 * 16-bit mode and there are conflicting 8-bit devices
273 * within the same 128k address space as this board's
274 * shared memory, and
275 *
276 * (2) so that other 8-bit devices with shared memory
277 * in this same 128k address space will work.
278 */
279 WE_MEM_DISABLE(wsc);
280
281 return (0);
282 }
283
284 static int
285 we_test_mem(sc)
286 struct dp8390_softc *sc;
287 {
288 struct we_softc *wsc = (struct we_softc *)sc;
289 bus_space_tag_t memt = sc->sc_buft;
290 bus_space_handle_t memh = sc->sc_bufh;
291 bus_size_t memsize = sc->mem_size;
292 int i;
293
294 if (wsc->sc_16bitp)
295 bus_space_set_region_2(memt, memh, 0, 0, memsize >> 1);
296 else
297 bus_space_set_region_1(memt, memh, 0, 0, memsize);
298
299 if (wsc->sc_16bitp) {
300 for (i = 0; i < memsize; i += 2) {
301 if (bus_space_read_2(memt, memh, i) != 0)
302 goto fail;
303 }
304 } else {
305 for (i = 0; i < memsize; i++) {
306 if (bus_space_read_1(memt, memh, i) != 0)
307 goto fail;
308 }
309 }
310
311 return (0);
312
313 fail:
314 printf("%s: failed to clear shared memory at offset 0x%x\n",
315 sc->sc_dev.dv_xname, i);
316 WE_MEM_DISABLE(wsc);
317 return (1);
318 }
319
320 /*
321 * Given a NIC memory source address and a host memory destination address,
322 * copy 'len' from NIC to host using shared memory. The 'len' is rounded
323 * up to a word - ok as long as mbufs are word-sized.
324 */
325 static __inline void
326 we_readmem(wsc, from, to, len)
327 struct we_softc *wsc;
328 int from;
329 u_int8_t *to;
330 int len;
331 {
332 bus_space_tag_t memt = wsc->sc_dp8390.sc_buft;
333 bus_space_handle_t memh = wsc->sc_dp8390.sc_bufh;
334
335 if (len & 1)
336 ++len;
337
338 if (wsc->sc_16bitp)
339 bus_space_read_region_stream_2(memt, memh, from,
340 (u_int16_t *)to, len >> 1);
341 else
342 bus_space_read_region_1(memt, memh, from,
343 to, len);
344 }
345
346 static int
347 we_write_mbuf(sc, m, buf)
348 struct dp8390_softc *sc;
349 struct mbuf *m;
350 int buf;
351 {
352 struct we_softc *wsc = (struct we_softc *)sc;
353 bus_space_tag_t memt = wsc->sc_dp8390.sc_buft;
354 bus_space_handle_t memh = wsc->sc_dp8390.sc_bufh;
355 u_int8_t *data, savebyte[2];
356 int savelen, len, leftover;
357 #ifdef DIAGNOSTIC
358 u_int8_t *lim;
359 #endif
360
361 savelen = m->m_pkthdr.len;
362
363 WE_MEM_ENABLE(wsc);
364
365 /*
366 * 8-bit boards are simple; no alignment tricks are necessary.
367 */
368 if (wsc->sc_16bitp == 0) {
369 for (; m != NULL; buf += m->m_len, m = m->m_next)
370 bus_space_write_region_1(memt, memh,
371 buf, mtod(m, u_int8_t *), m->m_len);
372 goto out;
373 }
374
375 /* Start out with no leftover data. */
376 leftover = 0;
377 savebyte[0] = savebyte[1] = 0;
378
379 for (; m != NULL; m = m->m_next) {
380 len = m->m_len;
381 if (len == 0)
382 continue;
383 data = mtod(m, u_int8_t *);
384 #ifdef DIAGNOSTIC
385 lim = data + len;
386 #endif
387 while (len > 0) {
388 if (leftover) {
389 /*
390 * Data left over (from mbuf or realignment).
391 * Buffer the next byte, and write it and
392 * the leftover data out.
393 */
394 savebyte[1] = *data++;
395 len--;
396 bus_space_write_stream_2(memt, memh, buf,
397 *(u_int16_t *)savebyte);
398 buf += 2;
399 leftover = 0;
400 } else if (BUS_SPACE_ALIGNED_POINTER(data, u_int16_t)
401 == 0) {
402 /*
403 * Unaligned dta; buffer the next byte.
404 */
405 savebyte[0] = *data++;
406 len--;
407 leftover = 1;
408 } else {
409 /*
410 * Aligned data; output contiguous words as
411 * much as we can, then buffer the remaining
412 * byte, if any.
413 */
414 leftover = len & 1;
415 len &= ~1;
416 bus_space_write_region_stream_2(memt, memh,
417 buf, (u_int16_t *)data, len >> 1);
418 data += len;
419 buf += len;
420 if (leftover)
421 savebyte[0] = *data++;
422 len = 0;
423 }
424 }
425 if (len < 0)
426 panic("we_write_mbuf: negative len");
427 #ifdef DIAGNOSTIC
428 if (data != lim)
429 panic("we_write_mbuf: data != lim");
430 #endif
431 }
432 if (leftover) {
433 savebyte[1] = 0;
434 bus_space_write_stream_2(memt, memh, buf,
435 *(u_int16_t *)savebyte);
436 }
437
438 out:
439 WE_MEM_DISABLE(wsc);
440
441 return (savelen);
442 }
443
444 static int
445 we_ring_copy(sc, src, dst, amount)
446 struct dp8390_softc *sc;
447 int src;
448 caddr_t dst;
449 u_short amount;
450 {
451 struct we_softc *wsc = (struct we_softc *)sc;
452 u_short tmp_amount;
453
454 /* Does copy wrap to lower addr in ring buffer? */
455 if (src + amount > sc->mem_end) {
456 tmp_amount = sc->mem_end - src;
457
458 /* Copy amount up to end of NIC memory. */
459 we_readmem(wsc, src, dst, tmp_amount);
460
461 amount -= tmp_amount;
462 src = sc->mem_ring;
463 dst += tmp_amount;
464 }
465
466 we_readmem(wsc, src, dst, amount);
467
468 return (src + amount);
469 }
470
471 static void
472 we_read_hdr(sc, packet_ptr, packet_hdrp)
473 struct dp8390_softc *sc;
474 int packet_ptr;
475 struct dp8390_ring *packet_hdrp;
476 {
477 struct we_softc *wsc = (struct we_softc *)sc;
478
479 we_readmem(wsc, packet_ptr, (u_int8_t *)packet_hdrp,
480 sizeof(struct dp8390_ring));
481 #if BYTE_ORDER == BIG_ENDIAN
482 packet_hdrp->count = bswap16(packet_hdrp->count);
483 #endif
484 }
485
486 static void
487 we_recv_int(sc)
488 struct dp8390_softc *sc;
489 {
490 struct we_softc *wsc = (struct we_softc *)sc;
491
492 WE_MEM_ENABLE(wsc);
493 dp8390_rint(sc);
494 WE_MEM_DISABLE(wsc);
495 }
496
497 static void
498 we_media_init(struct dp8390_softc *sc)
499 {
500 struct we_softc *wsc = (void *) sc;
501 int defmedia = IFM_ETHER;
502 u_int8_t x;
503
504 if (sc->is790) {
505 x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR);
506 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR,
507 x | WE790_HWR_SWH);
508 if (bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE790_GCR) &
509 WE790_GCR_GPOUT)
510 defmedia |= IFM_10_2;
511 else
512 defmedia |= IFM_10_5;
513 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR,
514 x & ~WE790_HWR_SWH);
515 } else {
516 x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_IRR);
517 if (x & WE_IRR_OUT2)
518 defmedia |= IFM_10_2;
519 else
520 defmedia |= IFM_10_5;
521 }
522
523 ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
524 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_2, 0, NULL);
525 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_5, 0, NULL);
526 ifmedia_set(&sc->sc_media, defmedia);
527 }
528
529 static int
530 we_mediachange(sc)
531 struct dp8390_softc *sc;
532 {
533
534 /*
535 * Current media is already set up. Just reset the interface
536 * to let the new value take hold. The new media will be
537 * set up in we_init_card() called via dp8390_init().
538 */
539 dp8390_reset(sc);
540 return (0);
541 }
542
543 static void
544 we_mediastatus(sc, ifmr)
545 struct dp8390_softc *sc;
546 struct ifmediareq *ifmr;
547 {
548 struct ifmedia *ifm = &sc->sc_media;
549
550 /*
551 * The currently selected media is always the active media.
552 */
553 ifmr->ifm_active = ifm->ifm_cur->ifm_media;
554 }
555
556 static void
557 we_init_card(sc)
558 struct dp8390_softc *sc;
559 {
560 struct we_softc *wsc = (struct we_softc *)sc;
561 struct ifmedia *ifm = &sc->sc_media;
562
563 if (wsc->sc_init_hook)
564 (*wsc->sc_init_hook)(wsc);
565
566 we_set_media(wsc, ifm->ifm_cur->ifm_media);
567 }
568
569 static void
570 we_set_media(wsc, media)
571 struct we_softc *wsc;
572 int media;
573 {
574 struct dp8390_softc *sc = &wsc->sc_dp8390;
575 bus_space_tag_t asict = wsc->sc_asict;
576 bus_space_handle_t asich = wsc->sc_asich;
577 u_int8_t hwr, gcr, irr;
578
579 if (sc->is790) {
580 hwr = bus_space_read_1(asict, asich, WE790_HWR);
581 bus_space_write_1(asict, asich, WE790_HWR,
582 hwr | WE790_HWR_SWH);
583 gcr = bus_space_read_1(asict, asich, WE790_GCR);
584 if (IFM_SUBTYPE(media) == IFM_10_2)
585 gcr |= WE790_GCR_GPOUT;
586 else
587 gcr &= ~WE790_GCR_GPOUT;
588 bus_space_write_1(asict, asich, WE790_GCR,
589 gcr | WE790_GCR_LIT);
590 bus_space_write_1(asict, asich, WE790_HWR,
591 hwr & ~WE790_HWR_SWH);
592 return;
593 }
594
595 irr = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_IRR);
596 if (IFM_SUBTYPE(media) == IFM_10_2)
597 irr |= WE_IRR_OUT2;
598 else
599 irr &= ~WE_IRR_OUT2;
600 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_IRR, irr);
601 }
602