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