if_ec.c revision 1.4 1 1.4 thorpej /* $NetBSD: if_ec.c,v 1.4 1998/01/26 19:21:36 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 thorpej * NASA Ames Research Center.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.1 thorpej * must display the following acknowledgement:
21 1.1 thorpej * This product includes software developed by the NetBSD
22 1.1 thorpej * Foundation, Inc. and its contributors.
23 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 thorpej * contributors may be used to endorse or promote products derived
25 1.1 thorpej * from this software without specific prior written permission.
26 1.1 thorpej *
27 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.1 thorpej */
39 1.1 thorpej
40 1.1 thorpej /*
41 1.1 thorpej * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
42 1.1 thorpej * adapters.
43 1.1 thorpej *
44 1.1 thorpej * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
45 1.1 thorpej *
46 1.1 thorpej * Copyright (C) 1993, David Greenman. This software may be used, modified,
47 1.1 thorpej * copied, distributed, and sold, in both source and binary form provided that
48 1.1 thorpej * the above copyright and these terms are retained. Under no circumstances is
49 1.1 thorpej * the author responsible for the proper functioning of this software, nor does
50 1.1 thorpej * the author assume any responsibility for damages incurred with its use.
51 1.1 thorpej */
52 1.1 thorpej
53 1.1 thorpej /*
54 1.1 thorpej * Device driver for the 3Com Etherlink II (3c503).
55 1.1 thorpej */
56 1.1 thorpej
57 1.1 thorpej #include "bpfilter.h"
58 1.1 thorpej #include "rnd.h"
59 1.1 thorpej
60 1.1 thorpej #include <sys/param.h>
61 1.1 thorpej #include <sys/systm.h>
62 1.1 thorpej #include <sys/device.h>
63 1.1 thorpej #include <sys/socket.h>
64 1.1 thorpej #include <sys/mbuf.h>
65 1.1 thorpej #include <sys/syslog.h>
66 1.1 thorpej
67 1.1 thorpej #include <net/if.h>
68 1.1 thorpej #include <net/if_dl.h>
69 1.1 thorpej #include <net/if_types.h>
70 1.1 thorpej #include <net/if_media.h>
71 1.1 thorpej
72 1.1 thorpej #include <net/if_ether.h>
73 1.1 thorpej
74 1.1 thorpej #ifdef INET
75 1.1 thorpej #include <netinet/in.h>
76 1.1 thorpej #include <netinet/in_systm.h>
77 1.1 thorpej #include <netinet/in_var.h>
78 1.1 thorpej #include <netinet/ip.h>
79 1.1 thorpej #include <netinet/if_inarp.h>
80 1.1 thorpej #endif
81 1.1 thorpej
82 1.1 thorpej #ifdef NS
83 1.1 thorpej #include <netns/ns.h>
84 1.1 thorpej #include <netns/ns_if.h>
85 1.1 thorpej #endif
86 1.1 thorpej
87 1.1 thorpej #if NBPFILTER > 0
88 1.1 thorpej #include <net/bpf.h>
89 1.1 thorpej #include <net/bpfdesc.h>
90 1.1 thorpej #endif
91 1.1 thorpej
92 1.1 thorpej #include <machine/bus.h>
93 1.1 thorpej #include <machine/intr.h>
94 1.1 thorpej
95 1.1 thorpej #include <dev/isa/isareg.h>
96 1.1 thorpej #include <dev/isa/isavar.h>
97 1.1 thorpej
98 1.1 thorpej #include <dev/ic/dp8390reg.h>
99 1.1 thorpej #include <dev/ic/dp8390var.h>
100 1.1 thorpej
101 1.1 thorpej #include <dev/isa/if_ecreg.h>
102 1.1 thorpej
103 1.1 thorpej struct ec_softc {
104 1.1 thorpej struct dp8390_softc sc_dp8390;
105 1.1 thorpej
106 1.1 thorpej bus_space_tag_t sc_asict; /* space tag for ASIC */
107 1.1 thorpej bus_space_handle_t sc_asich; /* space handle for ASIC */
108 1.1 thorpej
109 1.1 thorpej int sc_16bitp; /* are we 16 bit? */
110 1.1 thorpej
111 1.1 thorpej void *sc_ih; /* interrupt handle */
112 1.1 thorpej };
113 1.1 thorpej
114 1.1 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
115 1.1 thorpej int ec_probe __P((struct device *, void *, void *));
116 1.1 thorpej #else
117 1.1 thorpej int ec_probe __P((struct device *, struct cfdata *, void *));
118 1.1 thorpej #endif
119 1.1 thorpej void ec_attach __P((struct device *, struct device *, void *));
120 1.1 thorpej
121 1.1 thorpej struct cfattach ec_ca = {
122 1.1 thorpej sizeof(struct ec_softc), ec_probe, ec_attach
123 1.1 thorpej };
124 1.1 thorpej
125 1.1 thorpej int ec_set_media __P((struct ec_softc *, int));
126 1.1 thorpej
127 1.1 thorpej int ec_mediachange __P((struct dp8390_softc *));
128 1.1 thorpej void ec_mediastatus __P((struct dp8390_softc *, struct ifmediareq *));
129 1.1 thorpej
130 1.1 thorpej void ec_init_card __P((struct dp8390_softc *));
131 1.1 thorpej int ec_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
132 1.1 thorpej int ec_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
133 1.1 thorpej void ec_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
134 1.1 thorpej int ec_fake_test_mem __P((struct dp8390_softc *));
135 1.1 thorpej int ec_test_mem __P((struct dp8390_softc *));
136 1.1 thorpej
137 1.1 thorpej __inline void ec_writemem __P((struct ec_softc *, u_int8_t *, int, int));
138 1.1 thorpej __inline void ec_readmem __P((struct ec_softc *, int, u_int8_t *, int));
139 1.1 thorpej
140 1.1 thorpej static const int ec_iobase[] = {
141 1.1 thorpej 0x2e0, 0x2a0, 0x280, 0x250, 0x350, 0x330, 0x310, 0x300,
142 1.1 thorpej };
143 1.1 thorpej #define NEC_IOBASE (sizeof(ec_iobase) / sizeof(ec_iobase[0]))
144 1.1 thorpej
145 1.1 thorpej static const int ec_membase[] = {
146 1.1 thorpej MADDRUNK, MADDRUNK, MADDRUNK, MADDRUNK, 0xc8000, 0xcc000,
147 1.1 thorpej 0xd8000, 0xdc000,
148 1.1 thorpej };
149 1.1 thorpej #define NEC_MEMBASE (sizeof(ec_membase) / sizeof(ec_membase[0]))
150 1.1 thorpej
151 1.1 thorpej int ec_media[] = {
152 1.1 thorpej IFM_ETHER|IFM_10_2,
153 1.1 thorpej IFM_ETHER|IFM_10_5,
154 1.1 thorpej };
155 1.1 thorpej #define NEC_MEDIA (sizeof(ec_media) / sizeof(ec_media[0]))
156 1.1 thorpej #define EC_DEFMEDIA (IFM_ETHER|IFM_10_2)
157 1.1 thorpej
158 1.1 thorpej int
159 1.1 thorpej ec_probe(parent, match, aux)
160 1.1 thorpej struct device *parent;
161 1.1 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
162 1.1 thorpej void *match;
163 1.1 thorpej #else
164 1.1 thorpej struct cfdata *match;
165 1.1 thorpej #endif
166 1.1 thorpej void *aux;
167 1.1 thorpej {
168 1.1 thorpej struct isa_attach_args *ia = aux;
169 1.1 thorpej bus_space_tag_t nict, asict, memt;
170 1.1 thorpej bus_space_handle_t nich, asich, memh;
171 1.1 thorpej bus_size_t memsize;
172 1.1 thorpej int nich_valid, asich_valid, memh_valid;
173 1.1 thorpej int i, rv = 0;
174 1.1 thorpej u_int8_t x;
175 1.1 thorpej
176 1.1 thorpej nict = asict = ia->ia_iot;
177 1.1 thorpej memt = ia->ia_memt;
178 1.1 thorpej
179 1.1 thorpej nich_valid = asich_valid = memh_valid = 0;
180 1.1 thorpej
181 1.1 thorpej /*
182 1.1 thorpej * Hmm, a 16-bit card has 16k of memory, but only an 8k window
183 1.1 thorpej * to it.
184 1.1 thorpej */
185 1.1 thorpej memsize = 8192;
186 1.1 thorpej
187 1.1 thorpej /* Disallow wildcarded i/o addresses. */
188 1.1 thorpej if (ia->ia_iobase == ISACF_PORT_DEFAULT)
189 1.1 thorpej return (0);
190 1.1 thorpej
191 1.1 thorpej /* Disallow wildcarded mem address. */
192 1.1 thorpej if (ia->ia_maddr == ISACF_IOMEM_DEFAULT)
193 1.1 thorpej return (0);
194 1.1 thorpej
195 1.1 thorpej /* Validate the i/o base. */
196 1.1 thorpej for (i = 0; i < NEC_IOBASE; i++)
197 1.1 thorpej if (ia->ia_iobase == ec_iobase[i])
198 1.1 thorpej break;
199 1.1 thorpej if (i == NEC_IOBASE)
200 1.1 thorpej return (0);
201 1.1 thorpej
202 1.1 thorpej /* Validate the mem base. */
203 1.1 thorpej for (i = 0; i < NEC_MEMBASE; i++) {
204 1.1 thorpej if (ec_membase[i] == MADDRUNK)
205 1.1 thorpej continue;
206 1.1 thorpej if (ia->ia_maddr == ec_membase[i])
207 1.1 thorpej break;
208 1.1 thorpej }
209 1.1 thorpej if (i == NEC_MEMBASE)
210 1.1 thorpej return (0);
211 1.1 thorpej
212 1.1 thorpej /* Attempt to map the NIC space. */
213 1.1 thorpej if (bus_space_map(nict, ia->ia_iobase + ELINK2_NIC_OFFSET,
214 1.1 thorpej ELINK2_NIC_PORTS, 0, &nich))
215 1.1 thorpej goto out;
216 1.1 thorpej nich_valid = 1;
217 1.1 thorpej
218 1.1 thorpej /* Attempt to map the ASIC space. */
219 1.1 thorpej if (bus_space_map(asict, ia->ia_iobase + ELINK2_ASIC_OFFSET,
220 1.1 thorpej ELINK2_ASIC_PORTS, 0, &asich))
221 1.1 thorpej goto out;
222 1.1 thorpej asich_valid = 1;
223 1.1 thorpej
224 1.1 thorpej /* Attempt to map the memory space. */
225 1.1 thorpej if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh))
226 1.1 thorpej goto out;
227 1.1 thorpej memh_valid = 1;
228 1.1 thorpej
229 1.1 thorpej /*
230 1.1 thorpej * Verify that the kernel configured I/O address matches the
231 1.1 thorpej * board configured I/O address.
232 1.1 thorpej *
233 1.1 thorpej * This is really only useful to see if something that looks like
234 1.1 thorpej * the board is there; after all, we're already talking to it at
235 1.1 thorpej * this point.
236 1.1 thorpej */
237 1.1 thorpej x = bus_space_read_1(asict, asich, ELINK2_BCFR);
238 1.1 thorpej if (x == 0 || (x & (x - 1)) != 0)
239 1.1 thorpej goto out;
240 1.1 thorpej i = ffs(x) - 1;
241 1.1 thorpej if (ia->ia_iobase != ec_iobase[i])
242 1.1 thorpej goto out;
243 1.1 thorpej
244 1.1 thorpej /*
245 1.1 thorpej * ...and for the memory address. Note we do not support
246 1.1 thorpej * cards configured with shared memory disabled.
247 1.1 thorpej */
248 1.1 thorpej x = bus_space_read_1(asict, asich, ELINK2_PCFR);
249 1.1 thorpej if (x == 0 || (x & (x - 1)) != 0)
250 1.1 thorpej goto out;
251 1.1 thorpej i = ffs(x) - 1;
252 1.1 thorpej if (ia->ia_maddr != ec_membase[i])
253 1.1 thorpej goto out;
254 1.1 thorpej
255 1.1 thorpej /* So, we say we've found it! */
256 1.1 thorpej ia->ia_iosize = ELINK2_NIC_PORTS;
257 1.1 thorpej ia->ia_msize = memsize;
258 1.1 thorpej rv = 1;
259 1.1 thorpej
260 1.1 thorpej out:
261 1.1 thorpej if (nich_valid)
262 1.1 thorpej bus_space_unmap(nict, nich, ELINK2_NIC_PORTS);
263 1.1 thorpej if (asich_valid)
264 1.1 thorpej bus_space_unmap(asict, asich, ELINK2_ASIC_PORTS);
265 1.1 thorpej if (memh_valid)
266 1.1 thorpej bus_space_unmap(memt, memh, memsize);
267 1.1 thorpej return (rv);
268 1.1 thorpej }
269 1.1 thorpej
270 1.1 thorpej void
271 1.1 thorpej ec_attach(parent, self, aux)
272 1.1 thorpej struct device *parent, *self;
273 1.1 thorpej void *aux;
274 1.1 thorpej {
275 1.1 thorpej struct ec_softc *esc = (struct ec_softc *)self;
276 1.1 thorpej struct dp8390_softc *sc = &esc->sc_dp8390;
277 1.1 thorpej struct isa_attach_args *ia = aux;
278 1.1 thorpej bus_space_tag_t nict, asict, memt;
279 1.1 thorpej bus_space_handle_t nich, asich, memh;
280 1.1 thorpej bus_size_t memsize;
281 1.1 thorpej u_int8_t tmp;
282 1.1 thorpej int i;
283 1.1 thorpej
284 1.1 thorpej printf("\n");
285 1.1 thorpej
286 1.1 thorpej nict = asict = ia->ia_iot;
287 1.1 thorpej memt = ia->ia_memt;
288 1.1 thorpej
289 1.1 thorpej /*
290 1.1 thorpej * Hmm, a 16-bit card has 16k of memory, but only an 8k window
291 1.1 thorpej * to it.
292 1.1 thorpej */
293 1.1 thorpej memsize = 8192;
294 1.1 thorpej
295 1.1 thorpej /* Map the NIC space. */
296 1.1 thorpej if (bus_space_map(nict, ia->ia_iobase + ELINK2_NIC_OFFSET,
297 1.1 thorpej ELINK2_NIC_PORTS, 0, &nich)) {
298 1.1 thorpej printf("%s: can't map nic i/o space\n",
299 1.1 thorpej sc->sc_dev.dv_xname);
300 1.1 thorpej return;
301 1.1 thorpej }
302 1.1 thorpej
303 1.1 thorpej /* Map the ASIC space. */
304 1.1 thorpej if (bus_space_map(asict, ia->ia_iobase + ELINK2_ASIC_OFFSET,
305 1.1 thorpej ELINK2_ASIC_PORTS, 0, &asich)) {
306 1.1 thorpej printf("%s: can't map asic i/o space\n",
307 1.1 thorpej sc->sc_dev.dv_xname);
308 1.1 thorpej return;
309 1.1 thorpej }
310 1.1 thorpej
311 1.1 thorpej /* Map the memory space. */
312 1.1 thorpej if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh)) {
313 1.1 thorpej printf("%s: can't map shared memory\n",
314 1.1 thorpej sc->sc_dev.dv_xname);
315 1.1 thorpej return;
316 1.1 thorpej }
317 1.1 thorpej
318 1.1 thorpej esc->sc_asict = asict;
319 1.1 thorpej esc->sc_asich = asich;
320 1.1 thorpej
321 1.1 thorpej sc->sc_regt = nict;
322 1.1 thorpej sc->sc_regh = nich;
323 1.1 thorpej
324 1.1 thorpej sc->sc_buft = memt;
325 1.1 thorpej sc->sc_bufh = memh;
326 1.1 thorpej
327 1.1 thorpej /* Interface is always enabled. */
328 1.1 thorpej sc->sc_enabled = 1;
329 1.1 thorpej
330 1.1 thorpej /* Registers are linear. */
331 1.1 thorpej for (i = 0; i < 16; i++)
332 1.1 thorpej sc->sc_reg_map[i] = i;
333 1.1 thorpej
334 1.1 thorpej /* Now we can use the NIC_{GET,PUT}() macros. */
335 1.1 thorpej
336 1.1 thorpej /*
337 1.1 thorpej * Reset NIC and ASIC. Enable on-board transeiver throughout
338 1.1 thorpej * reset sequence since it will lock up if the cable isn't
339 1.1 thorpej * connected if we don't.
340 1.1 thorpej */
341 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_CR,
342 1.1 thorpej ELINK2_CR_RST | ELINK2_CR_XSEL);
343 1.1 thorpej
344 1.1 thorpej /* Wait for a while, then un-reset it. */
345 1.1 thorpej delay(50);
346 1.1 thorpej
347 1.1 thorpej /*
348 1.1 thorpej * The 3Com ASIC defaults to rather strange settings for the CR
349 1.1 thorpej * after a reset. It's important to set it again after the
350 1.1 thorpej * following write (this is done when we map the PROM below).
351 1.1 thorpej */
352 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
353 1.1 thorpej
354 1.1 thorpej /* Wait a bit for the NIC to recover from the reset. */
355 1.1 thorpej delay(5000);
356 1.1 thorpej
357 1.1 thorpej /*
358 1.1 thorpej * Get the station address from on-board ROM.
359 1.1 thorpej *
360 1.1 thorpej * First, map Ethernet address PROM over the top of where the NIC
361 1.1 thorpej * registers normally appear.
362 1.1 thorpej */
363 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_CR,
364 1.1 thorpej ELINK2_CR_XSEL | ELINK2_CR_EALO);
365 1.1 thorpej
366 1.1 thorpej for (i = 0; i < ETHER_ADDR_LEN; i++)
367 1.1 thorpej sc->sc_enaddr[i] = NIC_GET(nict, nich, i);
368 1.1 thorpej
369 1.1 thorpej /*
370 1.1 thorpej * Unmap PROM - select NIC registers. The proper setting of the
371 1.1 thorpej * transciever is set in later in ec_init_card() via dp8390_init().
372 1.1 thorpej */
373 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
374 1.1 thorpej
375 1.1 thorpej /* Determine if this is an 8-bit or 16-bit board. */
376 1.1 thorpej
377 1.1 thorpej /* Select page 0 registers. */
378 1.1 thorpej NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
379 1.1 thorpej
380 1.1 thorpej /*
381 1.1 thorpej * Attempt to clear WTS. If it doesn't clear, then this is a
382 1.1 thorpej * 16-bit board.
383 1.1 thorpej */
384 1.1 thorpej NIC_PUT(nict, nich, ED_P0_DCR, 0);
385 1.1 thorpej
386 1.1 thorpej /* Select page 2 registers. */
387 1.1 thorpej NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_2 | ED_CR_STP);
388 1.1 thorpej
389 1.1 thorpej /* The 3c503 forces the WTS bit to a one if this is a 16-bit board. */
390 1.1 thorpej if (NIC_GET(nict, nich, ED_P2_DCR) & ED_DCR_WTS)
391 1.1 thorpej esc->sc_16bitp = 1;
392 1.1 thorpej else
393 1.1 thorpej esc->sc_16bitp = 0;
394 1.1 thorpej
395 1.1 thorpej printf("%s: 3Com 3c503 Ethernet (%s-bit)\n",
396 1.1 thorpej sc->sc_dev.dv_xname, esc->sc_16bitp ? "16" : "8");
397 1.1 thorpej
398 1.1 thorpej /* Select page 0 registers. */
399 1.1 thorpej NIC_PUT(nict, nich, ED_P2_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
400 1.1 thorpej
401 1.1 thorpej sc->cr_proto = ED_CR_RD2;
402 1.1 thorpej
403 1.1 thorpej /*
404 1.1 thorpej * DCR gets:
405 1.1 thorpej *
406 1.1 thorpej * FIFO threshold to 8, No auto-init Remote DMA,
407 1.1 thorpej * byte order=80x86.
408 1.1 thorpej *
409 1.1 thorpej * 16-bit cards also get word-wide DMA transfers.
410 1.1 thorpej */
411 1.1 thorpej sc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
412 1.1 thorpej (esc->sc_16bitp ? ED_DCR_WTS : 0);
413 1.1 thorpej
414 1.1 thorpej sc->test_mem = ec_fake_test_mem;
415 1.1 thorpej sc->ring_copy = ec_ring_copy;
416 1.1 thorpej sc->write_mbuf = ec_write_mbuf;
417 1.1 thorpej sc->read_hdr = ec_read_hdr;
418 1.1 thorpej
419 1.1 thorpej sc->sc_mediachange = ec_mediachange;
420 1.1 thorpej sc->sc_mediastatus = ec_mediastatus;
421 1.1 thorpej
422 1.1 thorpej sc->mem_start = 0;
423 1.1 thorpej sc->mem_size = memsize;
424 1.1 thorpej
425 1.1 thorpej /* Do generic parts of attach. */
426 1.1 thorpej if (dp8390_config(sc, ec_media, NEC_MEDIA, EC_DEFMEDIA)) {
427 1.1 thorpej printf("%s: configuration failed\n", sc->sc_dev.dv_xname);
428 1.1 thorpej return;
429 1.1 thorpej }
430 1.1 thorpej
431 1.1 thorpej /*
432 1.1 thorpej * We need to override the way dp8390_config() set up our
433 1.1 thorpej * shared memory.
434 1.1 thorpej *
435 1.1 thorpej * We have an entire 8k window to put the transmit buffers on the
436 1.1 thorpej * 16-bit boards. But since the 16bit 3c503's shared memory is only
437 1.1 thorpej * fast enough to overlap the loading of one full-size packet, trying
438 1.1 thorpej * to load more than 2 buffers can actually leave the transmitter idle
439 1.1 thorpej * during the load. So 2 seems the best value. (Although a mix of
440 1.1 thorpej * variable-sized packets might change this assumption. Nonetheless,
441 1.1 thorpej * we optimize for linear transfers of same-size packets.)
442 1.1 thorpej */
443 1.1 thorpej if (esc->sc_16bitp) {
444 1.1 thorpej if (sc->sc_dev.dv_cfdata->cf_flags & DP8390_NO_MULTI_BUFFERING)
445 1.1 thorpej sc->txb_cnt = 1;
446 1.1 thorpej else
447 1.1 thorpej sc->txb_cnt = 2;
448 1.1 thorpej
449 1.1 thorpej sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_16BIT;
450 1.1 thorpej sc->rec_page_start = ELINK2_RX_PAGE_OFFSET_16BIT;
451 1.1 thorpej sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
452 1.1 thorpej sc->rec_page_start;
453 1.1 thorpej sc->mem_ring = sc->mem_start;
454 1.1 thorpej } else {
455 1.1 thorpej sc->txb_cnt = 1;
456 1.1 thorpej sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_8BIT;
457 1.1 thorpej sc->rec_page_start = sc->tx_page_start + ED_TXBUF_SIZE;
458 1.1 thorpej sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
459 1.1 thorpej sc->tx_page_start;
460 1.1 thorpej sc->mem_ring = sc->mem_start +
461 1.1 thorpej (ED_TXBUF_SIZE << ED_PAGE_SHIFT);
462 1.1 thorpej }
463 1.1 thorpej
464 1.1 thorpej /*
465 1.1 thorpej * Initialize CA page start/stop registers. Probably only needed
466 1.1 thorpej * if doing DMA, but what the Hell.
467 1.1 thorpej */
468 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_PSTR, sc->rec_page_start);
469 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_PSPR, sc->rec_page_stop);
470 1.1 thorpej
471 1.1 thorpej /*
472 1.1 thorpej * Program the IRQ.
473 1.1 thorpej */
474 1.1 thorpej switch (ia->ia_irq) {
475 1.1 thorpej case 9: tmp = ELINK2_IDCFR_IRQ2; break;
476 1.1 thorpej case 3: tmp = ELINK2_IDCFR_IRQ3; break;
477 1.1 thorpej case 4: tmp = ELINK2_IDCFR_IRQ4; break;
478 1.1 thorpej case 5: tmp = ELINK2_IDCFR_IRQ5; break;
479 1.1 thorpej break;
480 1.1 thorpej
481 1.1 thorpej case IRQUNK:
482 1.1 thorpej printf("%s: wildcarded IRQ is not allowed\n",
483 1.1 thorpej sc->sc_dev.dv_xname);
484 1.1 thorpej return;
485 1.1 thorpej
486 1.1 thorpej default:
487 1.1 thorpej printf("%s: invalid IRQ %d, must be 3, 4, 5, or 9\n",
488 1.1 thorpej sc->sc_dev.dv_xname, ia->ia_irq);
489 1.1 thorpej return;
490 1.1 thorpej }
491 1.1 thorpej
492 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_IDCFR, tmp);
493 1.1 thorpej
494 1.1 thorpej /*
495 1.1 thorpej * Initialize the GA configuration register. Set bank and enable
496 1.1 thorpej * shared memory.
497 1.1 thorpej */
498 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_GACFR,
499 1.1 thorpej ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
500 1.1 thorpej
501 1.1 thorpej /*
502 1.1 thorpej * Intialize "Vector Pointer" registers. These gawd-awful things
503 1.1 thorpej * are compared to 20 bits of the address on the ISA, and if they
504 1.1 thorpej * match, the shared memory is disabled. We se them to 0xffff0...
505 1.1 thorpej * allegedly the reset vector.
506 1.1 thorpej */
507 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_VPTR2, 0xff);
508 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_VPTR1, 0xff);
509 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_VPTR0, 0x00);
510 1.1 thorpej
511 1.1 thorpej /*
512 1.1 thorpej * Now run the real memory test.
513 1.1 thorpej */
514 1.1 thorpej if (ec_test_mem(sc)) {
515 1.1 thorpej printf("%s: memory test failed\n", sc->sc_dev.dv_xname);
516 1.1 thorpej return;
517 1.1 thorpej }
518 1.1 thorpej
519 1.1 thorpej /* Establish interrupt handler. */
520 1.1 thorpej esc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
521 1.1 thorpej IPL_NET, dp8390_intr, sc);
522 1.1 thorpej if (esc->sc_ih == NULL)
523 1.1 thorpej printf("%s: can't establish interrupt\n", sc->sc_dev.dv_xname);
524 1.1 thorpej }
525 1.1 thorpej
526 1.1 thorpej int
527 1.1 thorpej ec_fake_test_mem(sc)
528 1.1 thorpej struct dp8390_softc *sc;
529 1.1 thorpej {
530 1.1 thorpej
531 1.1 thorpej /*
532 1.1 thorpej * We have to do this after we initialize the GA, but we
533 1.1 thorpej * have to do that after calling dp8390_config(), which
534 1.1 thorpej * wants to test memory. Put this noop here, and then
535 1.1 thorpej * actually test memory later.
536 1.1 thorpej */
537 1.1 thorpej return (0);
538 1.1 thorpej }
539 1.1 thorpej
540 1.1 thorpej int
541 1.1 thorpej ec_test_mem(sc)
542 1.1 thorpej struct dp8390_softc *sc;
543 1.1 thorpej {
544 1.1 thorpej struct ec_softc *esc = (struct ec_softc *)sc;
545 1.1 thorpej bus_space_tag_t memt = sc->sc_buft;
546 1.1 thorpej bus_space_handle_t memh = sc->sc_bufh;
547 1.1 thorpej bus_size_t memsize = sc->mem_size;
548 1.1 thorpej int i;
549 1.1 thorpej
550 1.1 thorpej if (esc->sc_16bitp)
551 1.1 thorpej bus_space_set_region_2(memt, memh, 0, 0, memsize >> 1);
552 1.1 thorpej else
553 1.1 thorpej bus_space_set_region_1(memt, memh, 0, 0, memsize);
554 1.1 thorpej
555 1.1 thorpej if (esc->sc_16bitp) {
556 1.1 thorpej for (i = 0; i < memsize; i += 2) {
557 1.1 thorpej if (bus_space_read_2(memt, memh, i) != 0)
558 1.1 thorpej goto fail;
559 1.1 thorpej }
560 1.1 thorpej } else {
561 1.1 thorpej for (i = 0; i < memsize; i++) {
562 1.1 thorpej if (bus_space_read_1(memt, memh, i) != 0)
563 1.1 thorpej goto fail;
564 1.1 thorpej }
565 1.1 thorpej }
566 1.1 thorpej
567 1.1 thorpej return (0);
568 1.1 thorpej
569 1.1 thorpej fail:
570 1.1 thorpej printf("%s: failed to clear shared memory at offset 0x%x\n",
571 1.1 thorpej sc->sc_dev.dv_xname, i);
572 1.1 thorpej return (1);
573 1.1 thorpej }
574 1.1 thorpej
575 1.1 thorpej __inline void
576 1.1 thorpej ec_writemem(esc, from, to, len)
577 1.1 thorpej struct ec_softc *esc;
578 1.1 thorpej u_int8_t *from;
579 1.1 thorpej int to, len;
580 1.1 thorpej {
581 1.1 thorpej bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
582 1.1 thorpej bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
583 1.1 thorpej
584 1.1 thorpej if (esc->sc_16bitp) {
585 1.1 thorpej bus_space_write_region_2(memt, memh, to, (u_int16_t *)from,
586 1.1 thorpej len >> 1);
587 1.1 thorpej if (len & 1)
588 1.1 thorpej bus_space_write_2(memt, memh, to + (len & ~1),
589 1.1 thorpej (u_int16_t)(*(from + (len & ~1))));
590 1.1 thorpej } else
591 1.1 thorpej bus_space_write_region_1(memt, memh, to, from, len);
592 1.1 thorpej }
593 1.1 thorpej
594 1.1 thorpej __inline void
595 1.1 thorpej ec_readmem(esc, from, to, len)
596 1.1 thorpej struct ec_softc *esc;
597 1.1 thorpej int from;
598 1.1 thorpej u_int8_t *to;
599 1.1 thorpej int len;
600 1.1 thorpej {
601 1.1 thorpej bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
602 1.1 thorpej bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
603 1.1 thorpej
604 1.1 thorpej if (esc->sc_16bitp) {
605 1.1 thorpej bus_space_read_region_2(memt, memh, from, (u_int16_t *)to,
606 1.1 thorpej len >> 1);
607 1.1 thorpej if (len & 1)
608 1.1 thorpej *(to + (len & ~1)) = bus_space_read_2(memt,
609 1.1 thorpej memh, from + (len & ~1)) & 0xff;
610 1.1 thorpej } else
611 1.1 thorpej bus_space_read_region_1(memt, memh, from, to, len);
612 1.1 thorpej }
613 1.1 thorpej
614 1.1 thorpej int
615 1.1 thorpej ec_write_mbuf(sc, m, buf)
616 1.1 thorpej struct dp8390_softc *sc;
617 1.1 thorpej struct mbuf *m;
618 1.1 thorpej int buf;
619 1.1 thorpej {
620 1.1 thorpej struct ec_softc *esc = (struct ec_softc *)sc;
621 1.1 thorpej bus_space_tag_t asict = esc->sc_asict;
622 1.1 thorpej bus_space_handle_t asich = esc->sc_asich;
623 1.2 thorpej int savelen;
624 1.1 thorpej
625 1.1 thorpej savelen = m->m_pkthdr.len;
626 1.1 thorpej
627 1.1 thorpej /*
628 1.1 thorpej * If it's a 16-bit board, we have transmit buffers
629 1.1 thorpej * in a different page; switch to it.
630 1.1 thorpej */
631 1.1 thorpej if (esc->sc_16bitp)
632 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_GACFR,
633 1.1 thorpej ELINK2_GACFR_RSEL);
634 1.1 thorpej
635 1.2 thorpej for (; m != NULL; buf += m->m_len, m = m->m_next)
636 1.2 thorpej ec_writemem(esc, mtod(m, u_int8_t *), buf, m->m_len);
637 1.1 thorpej
638 1.1 thorpej /*
639 1.1 thorpej * Switch back to receive page.
640 1.1 thorpej */
641 1.1 thorpej if (esc->sc_16bitp)
642 1.1 thorpej bus_space_write_1(asict, asich, ELINK2_GACFR,
643 1.1 thorpej ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
644 1.1 thorpej
645 1.1 thorpej return (savelen);
646 1.1 thorpej }
647 1.1 thorpej
648 1.1 thorpej int
649 1.1 thorpej ec_ring_copy(sc, src, dst, amount)
650 1.1 thorpej struct dp8390_softc *sc;
651 1.1 thorpej int src;
652 1.1 thorpej caddr_t dst;
653 1.1 thorpej u_short amount;
654 1.1 thorpej {
655 1.1 thorpej struct ec_softc *esc = (struct ec_softc *)sc;
656 1.1 thorpej u_short tmp_amount;
657 1.1 thorpej
658 1.1 thorpej /* Does copy wrap to lower addr in ring buffer? */
659 1.1 thorpej if (src + amount > sc->mem_end) {
660 1.1 thorpej tmp_amount = sc->mem_end - src;
661 1.1 thorpej
662 1.1 thorpej /* Copy amount up to end of NIC memory. */
663 1.1 thorpej ec_readmem(esc, src, dst, tmp_amount);
664 1.1 thorpej
665 1.1 thorpej amount -= tmp_amount;
666 1.1 thorpej src = sc->mem_ring;
667 1.1 thorpej dst += tmp_amount;
668 1.1 thorpej }
669 1.1 thorpej
670 1.1 thorpej ec_readmem(esc, src, dst, amount);
671 1.1 thorpej
672 1.1 thorpej return (src + amount);
673 1.1 thorpej }
674 1.1 thorpej
675 1.1 thorpej void
676 1.1 thorpej ec_read_hdr(sc, packet_ptr, packet_hdrp)
677 1.1 thorpej struct dp8390_softc *sc;
678 1.1 thorpej int packet_ptr;
679 1.1 thorpej struct dp8390_ring *packet_hdrp;
680 1.1 thorpej {
681 1.1 thorpej struct ec_softc *esc = (struct ec_softc *)sc;
682 1.1 thorpej
683 1.1 thorpej ec_readmem(esc, packet_ptr, (u_int8_t *)packet_hdrp,
684 1.1 thorpej sizeof(struct dp8390_ring));
685 1.4 thorpej #if BYTE_ORDER == BIG_ENDIAN
686 1.4 thorpej packet_hdrp->count = bswap16(packet_hdrp->count);
687 1.4 thorpej #endif
688 1.1 thorpej }
689 1.1 thorpej
690 1.1 thorpej int
691 1.1 thorpej ec_mediachange(sc)
692 1.1 thorpej struct dp8390_softc *sc;
693 1.1 thorpej {
694 1.1 thorpej struct ec_softc *esc = (struct ec_softc *)sc;
695 1.1 thorpej struct ifmedia *ifm = &sc->sc_media;
696 1.1 thorpej
697 1.1 thorpej return (ec_set_media(esc, ifm->ifm_media));
698 1.1 thorpej }
699 1.1 thorpej
700 1.1 thorpej void
701 1.1 thorpej ec_mediastatus(sc, ifmr)
702 1.1 thorpej struct dp8390_softc *sc;
703 1.1 thorpej struct ifmediareq *ifmr;
704 1.1 thorpej {
705 1.1 thorpej struct ifmedia *ifm = &sc->sc_media;
706 1.1 thorpej
707 1.1 thorpej /*
708 1.1 thorpej * The currently selected media is always the active media.
709 1.1 thorpej */
710 1.1 thorpej ifmr->ifm_active = ifm->ifm_cur->ifm_media;
711 1.1 thorpej }
712 1.1 thorpej
713 1.1 thorpej void
714 1.1 thorpej ec_init_card(sc)
715 1.1 thorpej struct dp8390_softc *sc;
716 1.1 thorpej {
717 1.1 thorpej struct ec_softc *esc = (struct ec_softc *)sc;
718 1.1 thorpej struct ifmedia *ifm = &sc->sc_media;
719 1.1 thorpej
720 1.1 thorpej (void) ec_set_media(esc, ifm->ifm_cur->ifm_media);
721 1.1 thorpej }
722 1.1 thorpej
723 1.1 thorpej int
724 1.1 thorpej ec_set_media(esc, media)
725 1.1 thorpej struct ec_softc *esc;
726 1.1 thorpej int media;
727 1.1 thorpej {
728 1.1 thorpej u_int8_t new;
729 1.1 thorpej
730 1.1 thorpej if (IFM_TYPE(media) != IFM_ETHER)
731 1.1 thorpej return (EINVAL);
732 1.1 thorpej
733 1.1 thorpej switch (IFM_SUBTYPE(media)) {
734 1.1 thorpej case IFM_10_2:
735 1.1 thorpej new = ELINK2_CR_XSEL;
736 1.1 thorpej break;
737 1.1 thorpej
738 1.1 thorpej case IFM_10_5:
739 1.1 thorpej new = 0;
740 1.1 thorpej break;
741 1.1 thorpej
742 1.1 thorpej default:
743 1.1 thorpej return (EINVAL);
744 1.1 thorpej }
745 1.1 thorpej
746 1.1 thorpej bus_space_write_1(esc->sc_asict, esc->sc_asich, ELINK2_CR, new);
747 1.1 thorpej return (0);
748 1.1 thorpej }
749