xref: /netbsd-src/sys/arch/mac68k/dev/if_ae.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: if_ae.c,v 1.66 1997/11/02 00:29:56 thorpej Exp $	*/
2 
3 /*
4  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
5  * adapters.
6  *
7  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
8  *
9  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
10  * copied, distributed, and sold, in both source and binary form provided that
11  * the above copyright and these terms are retained.  Under no circumstances is
12  * the author responsible for the proper functioning of this software, nor does
13  * the author assume any responsibility for damages incurred with its use.
14  */
15 
16 #include "bpfilter.h"
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 #include <sys/mbuf.h>
22 #include <sys/socket.h>
23 
24 #include <net/if.h>
25 #include <net/if_media.h>
26 #include <net/if_ether.h>
27 
28 #include <machine/bus.h>
29 
30 #include <dev/ic/dp8390reg.h>
31 #include <dev/ic/dp8390var.h>
32 #include <mac68k/dev/if_aevar.h>
33 
34 struct cfdriver ae_cd = {
35 	NULL, "ae", DV_IFNET
36 };
37 
38 int
39 ae_size_card_memory(bst, bsh, ofs)
40 	bus_space_tag_t bst;
41 	bus_space_handle_t bsh;
42 	int ofs;
43 {
44 	int i1, i2, i3, i4;
45 
46 	/*
47 	 * banks; also assume it will generally mirror in upper banks
48 	 * if not installed.
49 	 */
50 	i1 = (8192 * 0);
51 	i2 = (8192 * 1);
52 	i3 = (8192 * 2);
53 	i4 = (8192 * 3);
54 
55 	bus_space_write_2(bst, bsh, ofs + i1, 0x1111);
56 	bus_space_write_2(bst, bsh, ofs + i2, 0x2222);
57 	bus_space_write_2(bst, bsh, ofs + i3, 0x3333);
58 	bus_space_write_2(bst, bsh, ofs + i4, 0x4444);
59 
60 	if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 &&
61 	    bus_space_read_2(bst, bsh, ofs + i2) == 0x2222 &&
62 	    bus_space_read_2(bst, bsh, ofs + i3) == 0x3333 &&
63 	    bus_space_read_2(bst, bsh, ofs + i4) == 0x4444)
64 		return 8192 * 4;
65 
66 	if ((bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 &&
67 	    bus_space_read_2(bst, bsh, ofs + i2) == 0x2222) ||
68 	    (bus_space_read_2(bst, bsh, ofs + i1) == 0x3333 &&
69 	    bus_space_read_2(bst, bsh, ofs + i2) == 0x4444))
70 		return 8192 * 2;
71 
72 	if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 ||
73 	    bus_space_read_2(bst, bsh, ofs + i1) == 0x4444)
74 		return 8192;
75 
76 	return 0;
77 }
78 
79 /*
80  * Zero memory and verify that it is clear.  The only difference between
81  * this and the default test_mem function is that the DP8390-based NuBus
82  * cards * apparently require word-wide writes and byte-wide reads, an
83  * `interesting' combination.
84  */
85 int
86 ae_test_mem(sc)
87 	struct dp8390_softc *sc;
88 {
89 	bus_space_tag_t buft = sc->sc_buft;
90 	bus_space_handle_t bufh = sc->sc_bufh;
91 	int i;
92 
93 	bus_space_set_region_2(buft, bufh, sc->mem_start, 0,
94 	    sc->mem_size / 2);
95 
96 	for (i = 0; i < sc->mem_size; ++i) {
97 		if (bus_space_read_1(sc->sc_buft, sc->sc_bufh, i)) {
98 			printf(": failed to clear NIC buffer at offset %x - "
99 			    "check configuration\n", (sc->mem_start + i));
100 			return 1;
101 		}
102 	}
103 
104 	return 0;
105 }
106 
107 /*
108  * Copy packet from mbuf to the board memory Currently uses an extra
109  * buffer/extra memory copy, unless the whole packet fits in one mbuf.
110  *
111  * As in the test_mem function, we use word-wide writes.
112  */
113 int
114 ae_write_mbuf(sc, m, buf)
115 	struct dp8390_softc *sc;
116 	struct mbuf *m;
117 	int buf;
118 {
119 	u_char *data, savebyte[2];
120 	int len, wantbyte;
121 	u_short totlen = 0;
122 
123 	wantbyte = 0;
124 
125 	for (; m ; m = m->m_next) {
126 		data = mtod(m, u_char *);
127 		len = m->m_len;
128 		totlen += len;
129 		if (len > 0) {
130 			/* Finish the last word. */
131 			if (wantbyte) {
132 				savebyte[1] = *data;
133 				bus_space_write_region_2(sc->sc_buft,
134 				    sc->sc_bufh, buf, savebyte, 1);
135 				buf += 2;
136 				data++;
137 				len--;
138 				wantbyte = 0;
139 			}
140 			/* Output contiguous words. */
141 			if (len > 1) {
142 				bus_space_write_region_2(sc->sc_buft,
143 				    sc->sc_bufh, buf, data, len >> 1);
144 				buf += len & ~1;
145 				data += len & ~1;
146 				len &= 1;
147 			}
148 			/* Save last byte, if necessary. */
149 			if (len == 1) {
150 				savebyte[0] = *data;
151 				wantbyte = 1;
152 			}
153 		}
154 	}
155 
156 	if (wantbyte) {
157 		savebyte[1] = 0;
158 		bus_space_write_region_2(sc->sc_buft, sc->sc_bufh,
159 		    buf, savebyte, 1);
160 	}
161 	return (totlen);
162 }
163