if_ed_pccard.c revision 150104
1/*-
2 * Copyright (c) 1995, David Greenman
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/dev/ed/if_ed_pccard.c 150104 2005-09-13 19:09:57Z imp $
28 */
29
30#include "opt_ed.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/socket.h>
35#include <sys/kernel.h>
36#include <sys/conf.h>
37#include <sys/uio.h>
38
39#include <sys/module.h>
40#include <sys/bus.h>
41#include <machine/bus.h>
42#include <sys/rman.h>
43#include <machine/resource.h>
44
45#include <net/ethernet.h>
46#include <net/if.h>
47#include <net/if_arp.h>
48#include <net/if_mib.h>
49#include <net/if_media.h>
50
51#include <dev/ed/if_edreg.h>
52#include <dev/ed/if_edvar.h>
53#include <dev/pccard/pccardvar.h>
54#include <dev/pccard/pccard_cis.h>
55#ifndef ED_NO_MIIBUS
56#include <dev/mii/mii.h>
57#include <dev/mii/miivar.h>
58#endif
59
60#include "card_if.h"
61#ifndef ED_NO_MIIBUS
62/* "device miibus" required.  See GENERIC if you get errors here. */
63#include "miibus_if.h"
64#endif
65#include "pccarddevs.h"
66
67#ifndef ED_NO_MIIBUS
68MODULE_DEPEND(ed, miibus, 1, 1, 1);
69#endif
70MODULE_DEPEND(ed, ether, 1, 1, 1);
71
72/*
73 * PC Cards should be using a network specific FUNCE in the CIS to
74 * communicate their MAC address to the driver.  However, there are a
75 * large number of NE-2000ish PC Cards that don't do this.  Nearly all
76 * of them store the MAC address at a fixed offset into attribute
77 * memory, without any reference at all appearing in the CIS.  And
78 * nearly all of those store it at the same location.
79 *
80 * This applies only to the older, NE-2000 compatbile cards.  The newer
81 * cards based on the AX88x90 or DL100XX chipsets have a specific place
82 * to look for MAC information.
83 */
84#define ED_DEFAULT_MAC_OFFSET	0xff0
85
86static const struct ed_product {
87	struct pccard_product	prod;
88	int flags;
89#define	NE2000DVF_DL100XX	0x0001		/* chip is D-Link DL10019/22 */
90#define	NE2000DVF_AX88X90	0x0002		/* chip is ASIX AX88[17]90 */
91#define NE2000DVF_ENADDR	0x0004		/* Get MAC from attr mem */
92#define NE2000DVF_ANYFUNC	0x0008		/* Allow any function type */
93#define NE2000DVF_MODEM		0x0010		/* Has a modem/serial */
94	int enoff;
95} ed_pccard_products[] = {
96	{ PCMCIA_CARD(ACCTON, EN2212), 0},
97	{ PCMCIA_CARD(ACCTON, EN2216), 0},
98	{ PCMCIA_CARD(ALLIEDTELESIS, LA_PCM), 0},
99	{ PCMCIA_CARD(AMBICOM, AMB8002T), 0},
100	{ PCMCIA_CARD(BILLIONTON, LNT10TN), 0},
101	{ PCMCIA_CARD(BILLIONTON, CFLT10N), 0},
102	{ PCMCIA_CARD(BROMAX, IPORT), 0},
103	{ PCMCIA_CARD(BROMAX, IPORT2), 0},
104	{ PCMCIA_CARD(BUFFALO, LPC2_CLT), 0},
105	{ PCMCIA_CARD(BUFFALO, LPC3_CLT), 0},
106	{ PCMCIA_CARD(BUFFALO, LPC3_CLX), NE2000DVF_AX88X90},
107	{ PCMCIA_CARD(BUFFALO, LPC4_TX), NE2000DVF_AX88X90},
108	{ PCMCIA_CARD(BUFFALO, LPC_CF_CLT), 0},
109	{ PCMCIA_CARD(CNET, NE2000), 0},
110	{ PCMCIA_CARD(COMPEX, AX88190), NE2000DVF_AX88X90},
111	{ PCMCIA_CARD(COMPEX, LANMODEM), 0},
112	{ PCMCIA_CARD(COMPEX, LINKPORT_ENET_B), 0},
113	{ PCMCIA_CARD(COREGA, ETHER_II_PCC_T), 0},
114	{ PCMCIA_CARD(COREGA, ETHER_II_PCC_TD), 0},
115	{ PCMCIA_CARD(COREGA, ETHER_PCC_T), 0},
116	{ PCMCIA_CARD(COREGA, ETHER_PCC_TD), 0},
117	{ PCMCIA_CARD(COREGA, FAST_ETHER_PCC_TX), NE2000DVF_DL100XX},
118	{ PCMCIA_CARD(COREGA, FETHER_PCC_TXD), NE2000DVF_AX88X90},
119	{ PCMCIA_CARD(COREGA, FETHER_PCC_TXF), NE2000DVF_DL100XX},
120	{ PCMCIA_CARD(DAYNA, COMMUNICARD_E_1), 0},
121	{ PCMCIA_CARD(DAYNA, COMMUNICARD_E_2), 0},
122	{ PCMCIA_CARD(DLINK, DE650), 0},
123	{ PCMCIA_CARD(DLINK, DE660), 0 },
124	{ PCMCIA_CARD(DLINK, DE660PLUS), 0},
125	{ PCMCIA_CARD(DYNALINK, L10C), 0},
126	{ PCMCIA_CARD(EDIMAX, EP4000A), 0},
127	{ PCMCIA_CARD(EPSON, EEN10B), 0},
128	{ PCMCIA_CARD(EXP, THINLANCOMBO), 0},
129	{ PCMCIA_CARD(GREY_CELL, TDK3000), 0},
130	{ PCMCIA_CARD(GREY_CELL, DMF650TX),
131	    NE2000DVF_ANYFUNC | NE2000DVF_DL100XX | NE2000DVF_MODEM},
132	{ PCMCIA_CARD(IBM, HOME_AND_AWAY), 0},
133	{ PCMCIA_CARD(IBM, INFOMOVER), NE2000DVF_ENADDR, 0xff0},
134	{ PCMCIA_CARD(IODATA3, PCLAT), 0},
135	{ PCMCIA_CARD(KINGSTON, CIO10T), 0},
136	{ PCMCIA_CARD(KINGSTON, KNE2), 0},
137	{ PCMCIA_CARD(LANTECH, FASTNETTX), NE2000DVF_AX88X90},
138	{ PCMCIA_CARD(LINKSYS, COMBO_ECARD),
139	    NE2000DVF_DL100XX | NE2000DVF_AX88X90},
140	{ PCMCIA_CARD(LINKSYS, ECARD_1), 0},
141	{ PCMCIA_CARD(LINKSYS, ECARD_2), 0},
142	{ PCMCIA_CARD(LINKSYS, ETHERFAST), NE2000DVF_DL100XX},
143	{ PCMCIA_CARD(LINKSYS, TRUST_COMBO_ECARD), 0},
144	{ PCMCIA_CARD(MACNICA, ME1_JEIDA), 0},
145	{ PCMCIA_CARD(MAGICRAM, ETHER), 0},
146	{ PCMCIA_CARD(MELCO, LPC3_CLX), NE2000DVF_AX88X90},
147	{ PCMCIA_CARD(MELCO, LPC3_TX), NE2000DVF_AX88X90},
148	{ PCMCIA_CARD(NDC, ND5100_E), 0},
149	{ PCMCIA_CARD(NETGEAR, FA410TXC), NE2000DVF_DL100XX},
150	/* Same ID as DLINK DFE-670TXD.  670 has DL10022, fa411 has ax88790 */
151	{ PCMCIA_CARD(NETGEAR, FA411), NE2000DVF_AX88X90 | NE2000DVF_DL100XX},
152	{ PCMCIA_CARD(NEXTCOM, NEXTHAWK), 0},
153	{ PCMCIA_CARD(NEWMEDIA, LANSURFER), 0},
154	{ PCMCIA_CARD(OEM2, ETHERNET), 0},
155	{ PCMCIA_CARD(OEM2, NE2000), 0},
156	{ PCMCIA_CARD(PLANET, SMARTCOM2000), 0 },
157	{ PCMCIA_CARD(PREMAX, PE200), 0},
158	{ PCMCIA_CARD(PSION, LANGLOBAL), 0},
159	{ PCMCIA_CARD(RACORE, ETHERNET), 0},
160	{ PCMCIA_CARD(RACORE, FASTENET), NE2000DVF_AX88X90},
161	{ PCMCIA_CARD(RACORE, 8041TX), NE2000DVF_AX88X90},
162	{ PCMCIA_CARD(RELIA, COMBO), 0},
163	{ PCMCIA_CARD(RPTI, EP400), 0},
164	{ PCMCIA_CARD(RPTI, EP401), 0},
165	{ PCMCIA_CARD(SMC, EZCARD), 0},
166	{ PCMCIA_CARD(SOCKET, EA_ETHER), 0},
167	{ PCMCIA_CARD(SOCKET, ES_1000), 0},
168	{ PCMCIA_CARD(SOCKET, LP_ETHER), 0},
169	{ PCMCIA_CARD(SOCKET, LP_ETHER_CF), 0},
170	{ PCMCIA_CARD(SOCKET, LP_ETH_10_100_CF), NE2000DVF_DL100XX},
171	{ PCMCIA_CARD(SVEC, COMBOCARD), 0},
172	{ PCMCIA_CARD(SVEC, LANCARD), 0},
173	{ PCMCIA_CARD(TAMARACK, ETHERNET), 0},
174	{ PCMCIA_CARD(TDK, CFE_10), 0},
175	{ PCMCIA_CARD(TDK, LAK_CD031), 0},
176	{ PCMCIA_CARD(TDK, DFL5610WS), 0},
177	{ PCMCIA_CARD(TELECOMDEVICE, LM5LT), 0 },
178	{ PCMCIA_CARD(TELECOMDEVICE, TCD_HPC100), NE2000DVF_AX88X90},
179	{ PCMCIA_CARD(ZONET, ZEN), 0},
180	{ { NULL } }
181};
182
183/*
184 *      PC Card (PCMCIA) specific code.
185 */
186static int	ed_pccard_probe(device_t);
187static int	ed_pccard_attach(device_t);
188
189static int	ed_pccard_memread(device_t dev, off_t offset, u_char *byte);
190static int	ed_pccard_memwrite(device_t dev, off_t offset, u_char byte);
191
192static int	ed_pccard_dl100xx(device_t dev, const struct ed_product *);
193#ifndef ED_NO_MIIBUS
194static void	ed_pccard_dl10xx_mii_reset(struct ed_softc *sc);
195static u_int	ed_pccard_dl10xx_mii_readbits(struct ed_softc *sc, int nbits);
196static void	ed_pccard_dl10xx_mii_writebits(struct ed_softc *sc, u_int val,
197    int nbits);
198#endif
199
200static int	ed_pccard_ax88x90(device_t dev, const struct ed_product *);
201
202static int
203ed_pccard_probe(device_t dev)
204{
205	const struct ed_product *pp;
206	int		error;
207	uint32_t	fcn = PCCARD_FUNCTION_UNSPEC;
208
209	/* Make sure we're a network function */
210	error = pccard_get_function(dev, &fcn);
211	if (error != 0)
212		return (error);
213
214	if ((pp = (const struct ed_product *) pccard_product_lookup(dev,
215	    (const struct pccard_product *) ed_pccard_products,
216	    sizeof(ed_pccard_products[0]), NULL)) != NULL) {
217		if (pp->prod.pp_name != NULL)
218			device_set_desc(dev, pp->prod.pp_name);
219		/*
220		 * Some devices don't ID themselves as network, but
221		 * that's OK if the flags say so.
222		 */
223		if (!(pp->flags & NE2000DVF_ANYFUNC) &&
224		    fcn != PCCARD_FUNCTION_NETWORK)
225			return (ENXIO);
226		return (0);
227	}
228	return (ENXIO);
229}
230
231static int
232ed_pccard_rom_mac(device_t dev, uint8_t *enaddr)
233{
234	struct ed_softc *sc = device_get_softc(dev);
235	uint8_t romdata[16];
236	int i;
237
238	/*
239	 * Read in the rom data at location 0.  We should see one of
240	 * two patterns.  Either you'll see odd locations 0xff and
241	 * even locations data, or you'll see odd and even locations
242	 * mirror each other.  In addition, the last two even locations
243	 * should be 0.  If they aren't 0, then we'll assume that
244	 * there's no valid ROM data on this card and try another method
245	 * to recover the MAC address.  Since there are no NE-1000
246	 * based PC Card devices, we'll assume we're 16-bit.
247	 */
248	ed_pio_readmem(sc, 0, romdata, 16);
249	if (romdata[12] != 0 || romdata[14] != 0)
250		return 0;
251	for (i = 0; i < ETHER_ADDR_LEN; i++)
252		enaddr[i] = romdata[i * 2];
253	return 1;
254}
255
256static int
257ed_pccard_add_modem(device_t dev)
258{
259	struct ed_softc *sc = device_get_softc(dev);
260
261	device_printf(dev, "Need to write this code: modem rid is %d\n",
262	    sc->modem_rid);
263	return 0;
264}
265
266static int
267ed_pccard_attach(device_t dev)
268{
269	u_char sum;
270	u_char enaddr[ETHER_ADDR_LEN];
271	const struct ed_product *pp;
272	int	error, i;
273	struct ed_softc *sc = device_get_softc(dev);
274	u_long size;
275
276	if ((pp = (const struct ed_product *) pccard_product_lookup(dev,
277	    (const struct pccard_product *) ed_pccard_products,
278	    sizeof(ed_pccard_products[0]), NULL)) == NULL)
279		return (ENXIO);
280	sc->modem_rid = -1;
281	if (pp->flags & NE2000DVF_MODEM) {
282		sc->port_rid = -1;
283		for (i = 0; i < 4; i++) {
284			size = bus_get_resource_count(dev, SYS_RES_IOPORT, i);
285			if (size == ED_NOVELL_IO_PORTS)
286				sc->port_rid = i;
287			else if (size == 8)
288				sc->modem_rid = i;
289		}
290		if (sc->port_rid == -1) {
291			device_printf(dev, "Cannot locate my ports!\n");
292			return (ENXIO);
293		}
294	} else {
295		sc->port_rid = 0;
296	}
297	/* Allocate the port resource during setup. */
298	error = ed_alloc_port(dev, sc->port_rid, ED_NOVELL_IO_PORTS);
299	if (error)
300		return (error);
301	error = ed_alloc_irq(dev, 0, 0);
302	if (error)
303		goto bad;
304
305	/*
306	 * Determine which chipset we are.  All the PC Card chipsets have the
307	 * ASIC and NIC offsets in the same place.
308	 */
309	sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
310	sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
311	error = ENXIO;
312	if (error != 0)
313		error = ed_pccard_dl100xx(dev, pp);
314	if (error != 0 && pp->flags & NE2000DVF_AX88X90)
315		error = ed_pccard_ax88x90(dev, pp);
316	if (error != 0)
317		error = ed_probe_Novell_generic(dev, device_get_flags(dev));
318	if (error)
319		goto bad;
320
321	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
322	    edintr, sc, &sc->irq_handle);
323	if (error) {
324		device_printf(dev, "setup intr failed %d \n", error);
325		goto bad;
326	}
327
328	/*
329	 * For the older cards, we have to get the MAC address from
330	 * the card in some way.  Let's try the standard PCMCIA way
331	 * first.  If that fails, then check to see if we have valid
332	 * data from the standard NE-2000 data roms.  If that fails,
333	 * check to see if the card has a hint about where to look in
334	 * its CIS.  If that fails, maybe we should look at some
335	 * default value.  In all fails, we should fail the attach,
336	 * but don't right now.
337	 */
338	if (sc->chip_type == ED_CHIP_TYPE_DP8390) {
339		pccard_get_ether(dev, enaddr);
340		if (bootverbose)
341			device_printf(dev, "CIS MAC %6D\n", enaddr, ":");
342		for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
343			sum |= enaddr[i];
344		if (sum == 0 && ed_pccard_rom_mac(dev, enaddr)) {
345			if (bootverbose)
346				device_printf(dev, "ROM mac %6D\n", enaddr,
347				    ":");
348			sum++;
349		}
350		if (sum == 0 && pp->flags & NE2000DVF_ENADDR) {
351			for (i = 0; i < ETHER_ADDR_LEN; i++) {
352				ed_pccard_memread(dev, pp->enoff + i * 2,
353				    enaddr + i);
354				sum |= enaddr[i];
355			}
356			if (bootverbose)
357				device_printf(dev, "Hint MAC %6D\n", enaddr,
358				    ":");
359		}
360		if (sum == 0) {
361			for (i = 0; i < ETHER_ADDR_LEN; i++) {
362				ed_pccard_memread(dev, ED_DEFAULT_MAC_OFFSET +
363				    i * 2, enaddr + i);
364				sum |= enaddr[i];
365			}
366			if (bootverbose)
367				device_printf(dev, "Fallback MAC %6D\n",
368				    enaddr, ":");
369		}
370		if (sum == 0) {
371			device_printf(dev, "Cannot extract MAC address.\n");
372			ed_release_resources(dev);
373			return (ENXIO);
374		}
375		bcopy(enaddr, sc->enaddr, ETHER_ADDR_LEN);
376	}
377
378	error = ed_attach(dev);
379	if (error)
380		goto bad;
381#ifndef ED_NO_MIIBUS
382 	if (sc->chip_type == ED_CHIP_TYPE_DL10019 ||
383	    sc->chip_type == ED_CHIP_TYPE_DL10022) {
384		/* Probe for an MII bus, but ignore errors. */
385		ed_pccard_dl10xx_mii_reset(sc);
386		sc->mii_readbits = ed_pccard_dl10xx_mii_readbits;
387		sc->mii_writebits = ed_pccard_dl10xx_mii_writebits;
388		mii_phy_probe(dev, &sc->miibus, ed_ifmedia_upd,
389		    ed_ifmedia_sts);
390	}
391#endif
392	if (sc->modem_rid != -1)
393		ed_pccard_add_modem(dev);
394	return (0);
395bad:
396	ed_release_resources(dev);
397	return (error);
398}
399
400static int
401ed_pccard_memwrite(device_t dev, off_t offset, u_char byte)
402{
403	int cis_rid;
404	struct resource *cis;
405
406	cis_rid = 0;
407	cis = bus_alloc_resource(dev, SYS_RES_MEMORY, &cis_rid, 0, ~0,
408	    4 << 10, RF_ACTIVE | RF_SHAREABLE);
409	if (cis == NULL)
410		return (ENXIO);
411	CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
412	    cis_rid, PCCARD_A_MEM_ATTR);
413
414	bus_space_write_1(rman_get_bustag(cis), rman_get_bushandle(cis),
415	    offset, byte);
416
417	bus_deactivate_resource(dev, SYS_RES_MEMORY, cis_rid, cis);
418	bus_release_resource(dev, SYS_RES_MEMORY, cis_rid, cis);
419
420	return (0);
421}
422
423static int
424ed_pccard_memread(device_t dev, off_t offset, u_char *byte)
425{
426	int cis_rid;
427	struct resource *cis;
428
429	cis_rid = 0;
430	cis = bus_alloc_resource(dev, SYS_RES_MEMORY, &cis_rid, 0, ~0,
431	    4 << 10, RF_ACTIVE | RF_SHAREABLE);
432	if (cis == NULL)
433		return (ENXIO);
434	CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
435	    cis_rid, PCCARD_A_MEM_ATTR);
436
437	*byte = bus_space_read_1(rman_get_bustag(cis), rman_get_bushandle(cis),
438	    offset);
439
440	bus_deactivate_resource(dev, SYS_RES_MEMORY, cis_rid, cis);
441	bus_release_resource(dev, SYS_RES_MEMORY, cis_rid, cis);
442
443	return (0);
444}
445
446/*
447 * Probe the Ethernet MAC addrees for PCMCIA Linksys EtherFast 10/100
448 * and compatible cards (DL10019C Ethernet controller).
449 *
450 * Note: The PAO patches try to use more memory for the card, but that
451 * seems to fail for my card.  A future optimization would add this back
452 * conditionally.
453 */
454static int
455ed_pccard_dl100xx(device_t dev, const struct ed_product *pp)
456{
457	struct ed_softc *sc = device_get_softc(dev);
458	u_char sum;
459	uint8_t id;
460	int i, error;
461
462	if (!pp->flags & NE2000DVF_DL100XX)
463		return (ENXIO);
464	error = ed_probe_Novell_generic(dev, device_get_flags(dev));
465	if (error != 0)
466		return (error);
467
468	/*
469	 * Linksys registers(offset from ASIC base)
470	 *
471	 * 0x04-0x09 : Physical Address Register 0-5 (PAR0-PAR5)
472	 * 0x0A      : Card ID Register (CIR)
473	 * 0x0B      : Check Sum Register (SR)
474	 */
475	for (sum = 0, i = 0x04; i < 0x0c; i++)
476		sum += ed_asic_inb(sc, i);
477	if (sum != 0xff)
478		return (ENXIO);		/* invalid DL10019C */
479	for (i = 0; i < ETHER_ADDR_LEN; i++)
480		sc->enaddr[i] = ed_asic_inb(sc, 0x04 + i);
481	ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
482	id = ed_asic_inb(sc, 0xf);
483	sc->isa16bit = 1;
484	sc->vendor = ED_VENDOR_NOVELL;
485	sc->type = ED_TYPE_NE2000;
486	sc->chip_type = (id & 0x90) == 0x90 ?
487	    ED_CHIP_TYPE_DL10022 : ED_CHIP_TYPE_DL10019;
488	sc->type_str = ((id & 0x90) == 0x90) ? "DL10022" : "DL10019";
489	return (0);
490}
491
492#ifndef ED_NO_MIIBUS
493/* MII bit-twiddling routines for cards using Dlink chipset */
494#define DL10XX_MIISET(sc, x) ed_asic_outb(sc, ED_DL10XX_MIIBUS, \
495    ed_asic_inb(sc, ED_DL10XX_MIIBUS) | (x))
496#define DL10XX_MIICLR(sc, x) ed_asic_outb(sc, ED_DL10XX_MIIBUS, \
497    ed_asic_inb(sc, ED_DL10XX_MIIBUS) & ~(x))
498
499static void
500ed_pccard_dl10xx_mii_reset(struct ed_softc *sc)
501{
502	if (sc->chip_type != ED_CHIP_TYPE_DL10022)
503		return;
504
505	ed_asic_outb(sc, ED_DL10XX_MIIBUS, ED_DL10XX_MII_RESET2);
506	DELAY(10);
507	ed_asic_outb(sc, ED_DL10XX_MIIBUS,
508	    ED_DL10XX_MII_RESET2 | ED_DL10XX_MII_RESET1);
509	DELAY(10);
510	ed_asic_outb(sc, ED_DL10XX_MIIBUS, ED_DL10XX_MII_RESET2);
511	DELAY(10);
512	ed_asic_outb(sc, ED_DL10XX_MIIBUS,
513	    ED_DL10XX_MII_RESET2 | ED_DL10XX_MII_RESET1);
514	DELAY(10);
515	ed_asic_outb(sc, ED_DL10XX_MIIBUS, 0);
516}
517
518static void
519ed_pccard_dl10xx_mii_writebits(struct ed_softc *sc, u_int val, int nbits)
520{
521	int i;
522
523	if (sc->chip_type == ED_CHIP_TYPE_DL10022)
524		DL10XX_MIISET(sc, ED_DL10XX_MII_DIROUT_22);
525	else
526		DL10XX_MIISET(sc, ED_DL10XX_MII_DIROUT_19);
527
528	for (i = nbits - 1; i >= 0; i--) {
529		if ((val >> i) & 1)
530			DL10XX_MIISET(sc, ED_DL10XX_MII_DATAOUT);
531		else
532			DL10XX_MIICLR(sc, ED_DL10XX_MII_DATAOUT);
533		DELAY(10);
534		DL10XX_MIISET(sc, ED_DL10XX_MII_CLK);
535		DELAY(10);
536		DL10XX_MIICLR(sc, ED_DL10XX_MII_CLK);
537		DELAY(10);
538	}
539}
540
541static u_int
542ed_pccard_dl10xx_mii_readbits(struct ed_softc *sc, int nbits)
543{
544	int i;
545	u_int val = 0;
546
547	if (sc->chip_type == ED_CHIP_TYPE_DL10022)
548		DL10XX_MIICLR(sc, ED_DL10XX_MII_DIROUT_22);
549	else
550		DL10XX_MIICLR(sc, ED_DL10XX_MII_DIROUT_19);
551
552	for (i = nbits - 1; i >= 0; i--) {
553		DL10XX_MIISET(sc, ED_DL10XX_MII_CLK);
554		DELAY(10);
555		val <<= 1;
556		if (ed_asic_inb(sc, ED_DL10XX_MIIBUS) & ED_DL10XX_MII_DATATIN)
557			val++;
558		DL10XX_MIICLR(sc, ED_DL10XX_MII_CLK);
559		DELAY(10);
560	}
561	return val;
562}
563#endif
564
565static void
566ed_pccard_ax88x90_geteprom(struct ed_softc *sc)
567{
568	int prom[16],i;
569	u_char tmp;
570	struct {
571		unsigned char offset, value;
572	} pg_seq[] = {
573						/* Select Page0 */
574		{ED_P0_CR, ED_CR_RD2 | ED_CR_STP | ED_CR_PAGE_0},
575		{ED_P0_DCR, 0x01},
576		{ED_P0_RBCR0, 0x00},		/* Clear the count regs. */
577		{ED_P0_RBCR1, 0x00},
578		{ED_P0_IMR, 0x00},		/* Mask completion irq. */
579		{ED_P0_ISR, 0xff},
580		{ED_P0_RCR, ED_RCR_MON | ED_RCR_INTT}, /* Set To Monitor */
581		{ED_P0_TCR, ED_TCR_LB0},	/* loopback mode. */
582		{ED_P0_RBCR0, 32},
583		{ED_P0_RBCR1, 0x00},
584		{ED_P0_RSAR0, 0x00},
585		{ED_P0_RSAR1, 0x04},
586		{ED_P0_CR, ED_CR_RD0 | ED_CR_STA | ED_CR_PAGE_0},
587	};
588
589	/* Reset Card */
590	tmp = ed_asic_inb(sc, ED_NOVELL_RESET);
591	ed_asic_outb(sc, ED_NOVELL_RESET, tmp);
592	DELAY(5000);
593	ed_asic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP | ED_CR_PAGE_0);
594	DELAY(5000);
595
596	/* Card Settings */
597	for (i = 0; i < sizeof(pg_seq) / sizeof(pg_seq[0]); i++)
598		ed_nic_outb(sc, pg_seq[i].offset, pg_seq[i].value);
599
600	/* Get Data */
601	for (i = 0; i < 16; i++)
602		prom[i] = ed_asic_inw(sc, 0);
603
604	/*
605	 * Work around a bug I've seen on Linksys EC2T cards.  On
606	 * these cards, the node address is contained in the low order
607	 * bytes of the prom, with the upper byte being 0.  On other
608	 * cards, the bytes are packed two per word.  I'm unsure why
609	 * this is the case, and why no other open source OS has a
610	 * similar workaround.  The Linksys EC2T card is still extremely
611	 * popular on E-Bay, fetching way more than any other 10Mbps
612	 * only card.  I might be able to test to see if prom[7] and
613	 * prom[15] == 0x5757, since that appears to be a reliable
614	 * test.  On the EC2T cards, I get 0x0057 in prom[14,15] instead.
615	 */
616	for (i = 0; i < ETHER_ADDR_LEN; i++)
617		if (prom[i] & 0xff00)
618			break;
619	if (i == ETHER_ADDR_LEN) {
620		for (i = 0; i < ETHER_ADDR_LEN; i++)
621			sc->enaddr[i] = prom[i] & 0xff;
622	} else {
623		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
624			sc->enaddr[i] = prom[i / 2] & 0xff;
625			sc->enaddr[i + 1] = (prom[i / 2] >> 8) & 0xff;
626		}
627	}
628}
629
630/*
631 * Special setup for AX88[17]90
632 */
633static int
634ed_pccard_ax88x90(device_t dev, const struct ed_product *pp)
635{
636	int	error;
637	int	iobase;
638	char *ts;
639	struct	ed_softc *sc = device_get_softc(dev);
640
641	if (!pp->flags & NE2000DVF_AX88X90)
642		return (ENXIO);
643
644	/* XXX
645	 * Set Attribute Memory IOBASE Register.  Is this a deficiency in
646	 * the PC Card layer, or an ax88190 specific issue?  The card
647	 * definitely doesn't work without it.
648	 */
649	iobase = rman_get_start(sc->port_res);
650	ed_pccard_memwrite(dev, ED_AX88190_IOBASE0, iobase & 0xff);
651	ed_pccard_memwrite(dev, ED_AX88190_IOBASE1, (iobase >> 8) & 0xff);
652	ts = "AX88190";
653	if (ed_asic_inb(sc, ED_ASIX_TEST) != 0) {
654		ed_pccard_memwrite(dev, ED_AX88790_CSR, ED_AX88790_CSR_PWRDWN);
655		ts = "AX88790";
656	}
657	sc->chip_type = ED_CHIP_TYPE_AX88190;
658	ed_pccard_ax88x90_geteprom(sc);
659	error = ed_probe_Novell_generic(dev, device_get_flags(dev));
660	if (error == 0) {
661		sc->vendor = ED_VENDOR_NOVELL;
662		sc->type = ED_TYPE_NE2000;
663		sc->chip_type = ED_CHIP_TYPE_AX88190;
664		sc->type_str = ts;
665	}
666	return (error);
667}
668
669static device_method_t ed_pccard_methods[] = {
670	/* Device interface */
671	DEVMETHOD(device_probe,		ed_pccard_probe),
672	DEVMETHOD(device_attach,	ed_pccard_attach),
673	DEVMETHOD(device_detach,	ed_detach),
674
675#ifndef ED_NO_MIIBUS
676	/* Bus interface */
677	DEVMETHOD(bus_child_detached,	ed_child_detached),
678
679	/* MII interface */
680	DEVMETHOD(miibus_readreg,	ed_miibus_readreg),
681	DEVMETHOD(miibus_writereg,	ed_miibus_writereg),
682#endif
683
684	{ 0, 0 }
685};
686
687static driver_t ed_pccard_driver = {
688	"ed",
689	ed_pccard_methods,
690	sizeof(struct ed_softc)
691};
692
693DRIVER_MODULE(ed, pccard, ed_pccard_driver, ed_devclass, 0, 0);
694#ifndef ED_NO_MIIBUS
695DRIVER_MODULE(miibus, ed, miibus_driver, miibus_devclass, 0, 0);
696#endif
697