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