if_ed_pccard.c revision 191370
1/*-
2 * Copyright (c) 2005, M. Warner Losh
3 * Copyright (c) 1995, David Greenman
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice unmodified, this list of conditions, and the following
11 *    disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/dev/ed/if_ed_pccard.c 191370 2009-04-22 06:30:24Z imp $
29 */
30
31/*
32 * Notes for adding media support.  Each chipset is somewhat different
33 * from the others.  Linux has a table of OIDs that it uses to see what
34 * supports the misc register of the NS83903.  But a sampling of datasheets
35 * I could dig up on cards I own paints a different picture.
36 *
37 * Chipset specific details:
38 * NS 83903/902A paired
39 *    ccr base 0x1020
40 *    id register at 0x1000: 7-3 = 0, 2-0 = 1.
41 *	(maybe this test is too week)
42 *    misc register at 0x018:
43 *	6 WAIT_TOUTENABLE enable watchdog timeout
44 *	3 AUI/TPI 1 AUX, 0 TPI
45 *	2 loopback
46 *      1 gdlink (tpi mode only) 1 tp good, 0 tp bad
47 *	0 0-no mam, 1 mam connected
48 *
49 * NS83926 appears to be a NS pcmcia glue chip used on the IBM Ethernet II
50 * and the NEC PC9801N-J12 ccr base 0x2000!
51 *
52 * winbond 289c926
53 *    ccr base 0xfd0
54 *    cfb (am 0xff2):
55 *	0-1 PHY01	00 TPI, 01 10B2, 10 10B5, 11 TPI (reduced squ)
56 *	2 LNKEN		0 - enable link and auto switch, 1 disable
57 *	3 LNKSTS	TPI + LNKEN=0 + link good == 1, else 0
58 *    sr (am 0xff4)
59 *	88 00 88 00 88 00, etc
60 *
61 * TMI tc3299a (cr PHY01 == 0)
62 *    ccr base 0x3f8
63 *    cra (io 0xa)
64 *    crb (io 0xb)
65 *	0-1 PHY01	00 auto, 01 res, 10 10B5, 11 TPI
66 *	2 GDLINK	1 disable checking of link
67 *	6 LINK		0 bad link, 1 good link
68 *
69 * EN5017A, EN5020	no data, but very popular
70 * Other chips?
71 * NetBSD supports RTL8019, but none have surfaced that I can see
72 */
73
74#include <sys/param.h>
75#include <sys/systm.h>
76#include <sys/socket.h>
77#include <sys/kernel.h>
78#include <sys/conf.h>
79#include <sys/uio.h>
80
81#include <sys/module.h>
82#include <sys/bus.h>
83#include <machine/bus.h>
84#include <sys/rman.h>
85#include <machine/resource.h>
86
87#include <net/ethernet.h>
88#include <net/if.h>
89#include <net/if_arp.h>
90#include <net/if_mib.h>
91#include <net/if_media.h>
92
93#include <dev/ed/if_edreg.h>
94#include <dev/ed/if_edvar.h>
95#include <dev/ed/ax88x90reg.h>
96#include <dev/ed/dl100xxreg.h>
97#include <dev/ed/tc5299jreg.h>
98#include <dev/pccard/pccardvar.h>
99#include <dev/pccard/pccardreg.h>
100#include <dev/pccard/pccard_cis.h>
101#include <dev/mii/mii.h>
102#include <dev/mii/miivar.h>
103
104#include "card_if.h"
105/* "device miibus" required.  See GENERIC if you get errors here. */
106#include "miibus_if.h"
107#include "pccarddevs.h"
108
109#define bootverbose 1
110
111/*
112 * NE-2000 based PC Cards have a number of ways to get the MAC address.
113 * Some cards encode this as a FUNCE.  Others have this in the ROMs the
114 * same way that ISA cards do.  Some have it encoded in the attribute
115 * memory somewhere that isn't in the CIS.  Some new chipsets have it
116 * in special registers in the ASIC part of the chip.
117 *
118 * For those cards that have the MAC adress stored in attribute memory
119 * outside of a FUNCE entry in the CIS, nearly all of them have it at
120 * a fixed offset (0xff0).  We use that offset as a source of last
121 * resource if other offsets have failed.  This is the address of the
122 * National Semiconductor DP83903A, which is the only chip's datasheet
123 * I've found.
124 */
125#define ED_DEFAULT_MAC_OFFSET	0xff0
126
127static const struct ed_product {
128	struct pccard_product	prod;
129	int flags;
130#define	NE2000DVF_DL100XX	0x0001		/* chip is D-Link DL10019/22 */
131#define	NE2000DVF_AX88X90	0x0002		/* chip is ASIX AX88[17]90 */
132#define NE2000DVF_TC5299J	0x0004		/* chip is Tamarack TC5299J */
133#define NE2000DVF_TOSHIBA	0x0008		/* Toshiba DP83902A */
134#define NE2000DVF_ENADDR	0x0100		/* Get MAC from attr mem */
135#define NE2000DVF_ANYFUNC	0x0200		/* Allow any function type */
136#define NE2000DVF_MODEM		0x0400		/* Has a modem/serial */
137	int enoff;
138} ed_pccard_products[] = {
139	{ PCMCIA_CARD(ACCTON, EN2212), 0},
140	{ PCMCIA_CARD(ACCTON, EN2216), 0},
141	{ PCMCIA_CARD(ALLIEDTELESIS, LA_PCM), 0},
142	{ PCMCIA_CARD(AMBICOM, AMB8002T), 0},
143	{ PCMCIA_CARD(BILLIONTON, CFLT10N), 0},
144	{ PCMCIA_CARD(BILLIONTON, LNA100B), NE2000DVF_AX88X90},
145	{ PCMCIA_CARD(BILLIONTON, LNT10TN), 0},
146	{ PCMCIA_CARD(BROMAX, AXNET), NE2000DVF_AX88X90},
147	{ PCMCIA_CARD(BROMAX, IPORT), 0},
148	{ PCMCIA_CARD(BROMAX, IPORT2), 0},
149	{ PCMCIA_CARD(BUFFALO, LPC2_CLT), 0},
150	{ PCMCIA_CARD(BUFFALO, LPC3_CLT), 0},
151	{ PCMCIA_CARD(BUFFALO, LPC3_CLX), NE2000DVF_AX88X90},
152	{ PCMCIA_CARD(BUFFALO, LPC4_TX), NE2000DVF_AX88X90},
153	{ PCMCIA_CARD(BUFFALO, LPC4_CLX), NE2000DVF_AX88X90},
154	{ PCMCIA_CARD(BUFFALO, LPC_CF_CLT), 0},
155	{ PCMCIA_CARD(CNET, NE2000), 0},
156	{ PCMCIA_CARD(COMPEX, AX88190), NE2000DVF_AX88X90},
157	{ PCMCIA_CARD(COMPEX, LANMODEM), 0},
158	{ PCMCIA_CARD(COMPEX, LINKPORT_ENET_B), 0},
159	{ PCMCIA_CARD(COREGA, ETHER_II_PCC_T), 0},
160	{ PCMCIA_CARD(COREGA, ETHER_II_PCC_TD), 0},
161	{ PCMCIA_CARD(COREGA, ETHER_PCC_T), 0},
162	{ PCMCIA_CARD(COREGA, ETHER_PCC_TD), 0},
163	{ PCMCIA_CARD(COREGA, FAST_ETHER_PCC_TX), NE2000DVF_DL100XX},
164	{ PCMCIA_CARD(COREGA, FETHER_PCC_TXD), NE2000DVF_AX88X90},
165	{ PCMCIA_CARD(COREGA, FETHER_PCC_TXF), NE2000DVF_DL100XX},
166	{ PCMCIA_CARD(COREGA, FETHER_II_PCC_TXD), NE2000DVF_AX88X90},
167	{ PCMCIA_CARD(COREGA, LAPCCTXD), 0},
168	{ PCMCIA_CARD(DAYNA, COMMUNICARD_E_1), 0},
169	{ PCMCIA_CARD(DAYNA, COMMUNICARD_E_2), 0},
170	{ PCMCIA_CARD(DLINK, DE650), NE2000DVF_ANYFUNC },
171	{ PCMCIA_CARD(DLINK, DE660), 0 },
172	{ PCMCIA_CARD(DLINK, DE660PLUS), 0},
173	{ PCMCIA_CARD(DYNALINK, L10C), 0},
174	{ PCMCIA_CARD(EDIMAX, EP4000A), 0},
175	{ PCMCIA_CARD(EPSON, EEN10B), 0},
176	{ PCMCIA_CARD(EXP, THINLANCOMBO), 0},
177	{ PCMCIA_CARD(GLOBALVILLAGE, LANMODEM), 0},
178	{ PCMCIA_CARD(GREY_CELL, TDK3000), 0},
179	{ PCMCIA_CARD(GREY_CELL, DMF650TX),
180	    NE2000DVF_ANYFUNC | NE2000DVF_DL100XX | NE2000DVF_MODEM},
181	{ PCMCIA_CARD(GVC, NIC_2000P), 0},
182	{ PCMCIA_CARD(IBM, HOME_AND_AWAY), 0},
183	{ PCMCIA_CARD(IBM, INFOMOVER), 0},
184	{ PCMCIA_CARD(IODATA3, PCLAT), 0},
185	{ PCMCIA_CARD(KINGSTON, CIO10T), 0},
186	{ PCMCIA_CARD(KINGSTON, KNE2), 0},
187	{ PCMCIA_CARD(LANTECH, FASTNETTX), NE2000DVF_AX88X90},
188	/* Same ID for many different cards, including generic NE2000 */
189	{ PCMCIA_CARD(LINKSYS, COMBO_ECARD),
190	    NE2000DVF_DL100XX | NE2000DVF_AX88X90},
191	{ PCMCIA_CARD(LINKSYS, ECARD_1), 0},
192	{ PCMCIA_CARD(LINKSYS, ECARD_2), 0},
193	{ PCMCIA_CARD(LINKSYS, ETHERFAST), NE2000DVF_DL100XX},
194	{ PCMCIA_CARD(LINKSYS, TRUST_COMBO_ECARD), 0},
195	{ PCMCIA_CARD(MACNICA, ME1_JEIDA), 0},
196	{ PCMCIA_CARD(MAGICRAM, ETHER), 0},
197	{ PCMCIA_CARD(MELCO, LPC3_CLX), NE2000DVF_AX88X90},
198	{ PCMCIA_CARD(MELCO, LPC3_TX), NE2000DVF_AX88X90},
199	{ PCMCIA_CARD(MITSUBISHI, B8895), NE2000DVF_ANYFUNC}, /* NG */
200	{ PCMCIA_CARD(MICRORESEARCH, MR10TPC), 0},
201	{ PCMCIA_CARD(NDC, ND5100_E), 0},
202	{ PCMCIA_CARD(NETGEAR, FA410TXC), NE2000DVF_DL100XX},
203	/* Same ID as DLINK DFE-670TXD.  670 has DL10022, fa411 has ax88790 */
204	{ PCMCIA_CARD(NETGEAR, FA411), NE2000DVF_AX88X90 | NE2000DVF_DL100XX},
205	{ PCMCIA_CARD(NEXTCOM, NEXTHAWK), 0},
206	{ PCMCIA_CARD(NEWMEDIA, LANSURFER), NE2000DVF_ANYFUNC},
207	{ PCMCIA_CARD(NEWMEDIA, LIVEWIRE), 0},
208	{ PCMCIA_CARD(OEM2, ETHERNET), 0},
209	{ PCMCIA_CARD(OEM2, FAST_ETHERNET), NE2000DVF_AX88X90 },
210	{ PCMCIA_CARD(OEM2, NE2000), 0},
211	{ PCMCIA_CARD(PLANET, SMARTCOM2000), 0 },
212	{ PCMCIA_CARD(PREMAX, PE200), 0},
213	{ PCMCIA_CARD(PSION, LANGLOBAL),
214	    NE2000DVF_ANYFUNC | NE2000DVF_AX88X90 | NE2000DVF_MODEM},
215	{ PCMCIA_CARD(RACORE, ETHERNET), 0},
216	{ PCMCIA_CARD(RACORE, FASTENET), NE2000DVF_AX88X90},
217	{ PCMCIA_CARD(RACORE, 8041TX), NE2000DVF_AX88X90 | NE2000DVF_TC5299J},
218	{ PCMCIA_CARD(RELIA, COMBO), 0},
219	{ PCMCIA_CARD(RIOS, PCCARD3), 0},
220	{ PCMCIA_CARD(RPTI, EP400), 0},
221	{ PCMCIA_CARD(RPTI, EP401), 0},
222	{ PCMCIA_CARD(SMC, EZCARD), 0},
223	{ PCMCIA_CARD(SOCKET, EA_ETHER), 0},
224	{ PCMCIA_CARD(SOCKET, ES_1000), 0},
225	{ PCMCIA_CARD(SOCKET, LP_ETHER), 0},
226	{ PCMCIA_CARD(SOCKET, LP_ETHER_CF), 0},
227	{ PCMCIA_CARD(SOCKET, LP_ETH_10_100_CF), NE2000DVF_DL100XX},
228	{ PCMCIA_CARD(SVEC, COMBOCARD), 0},
229	{ PCMCIA_CARD(SVEC, LANCARD), 0},
230	{ PCMCIA_CARD(TAMARACK, ETHERNET), 0},
231	{ PCMCIA_CARD(TDK, CFE_10), 0},
232	{ PCMCIA_CARD(TDK, LAK_CD031), 0},
233	{ PCMCIA_CARD(TDK, DFL5610WS), 0},
234	{ PCMCIA_CARD(TELECOMDEVICE, LM5LT), 0 },
235	{ PCMCIA_CARD(TELECOMDEVICE, TCD_HPC100), NE2000DVF_AX88X90},
236	{ PCMCIA_CARD(TJ, PTJ_LAN_T), 0 },
237	{ PCMCIA_CARD(TOSHIBA2, LANCT00A), NE2000DVF_ANYFUNC | NE2000DVF_TOSHIBA},
238	{ PCMCIA_CARD(ZONET, ZEN), 0},
239	{ { NULL } }
240};
241
242/*
243	PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x08a1, 0xc0ab),
244	PCMCIA_DEVICE_MANF_CARD(0x0213, 0x2452),
245
246	PCMCIA_PFC_DEVICE_PROD_ID12(0, "AnyCom", "Fast Ethernet + 56K COMBO", 0x578ba6e7, 0xb0ac62c4),
247	PCMCIA_PFC_DEVICE_PROD_ID12(0, "D-Link", "DME336T", 0x1a424a1c, 0xb23897ff),
248	PCMCIA_PFC_DEVICE_PROD_ID12(0, "Grey Cell", "GCS3000", 0x2a151fac, 0x48b932ae),
249	PCMCIA_PFC_DEVICE_PROD_ID12(0, "Linksys", "EtherFast 10&100 + 56K PC Card (PCMLM56)", 0x0733cc81, 0xb3765033),
250	PCMCIA_PFC_DEVICE_PROD_ID12(0, "LINKSYS", "PCMLM336", 0xf7cb0b07, 0x7a821b58),
251	PCMCIA_PFC_DEVICE_PROD_ID12(0, "PCMCIAs", "ComboCard", 0xdcfe12d3, 0xcd8906cc),
252	PCMCIA_PFC_DEVICE_PROD_ID12(0, "PCMCIAs", "LanModem", 0xdcfe12d3, 0xc67c648f),
253	PCMCIA_MFC_DEVICE_PROD_ID123(0, "APEX DATA", "MULTICARD", "ETHERNET-MODEM", 0x11c2da09, 0x7289dc5d, 0xaad95e1f),
254	PCMCIA_MFC_DEVICE_PROD_ID2(0, "FAX/Modem/Ethernet Combo Card ", 0x1ed59302),
255	PCMCIA_DEVICE_PROD_ID12("2408LAN", "Ethernet", 0x352fff7f, 0x00b2e941),
256	PCMCIA_DEVICE_PROD_ID1234("Socket", "CF 10/100 Ethernet Card", "Revision B", "05/11/06", 0xb38bcc2e, 0x4de88352, 0xeaca6c8d, 0x7e57c22e),
257	PCMCIA_DEVICE_PROD_ID123("Cardwell", "PCMCIA", "ETHERNET", 0x9533672e, 0x281f1c5d, 0x3ff7175b),
258	PCMCIA_DEVICE_PROD_ID123("CNet  ", "CN30BC", "ETHERNET", 0x9fe55d3d, 0x85601198, 0x3ff7175b),
259	PCMCIA_DEVICE_PROD_ID123("Digital", "Ethernet", "Adapter", 0x9999ab35, 0x00b2e941, 0x4b0d829e),
260	PCMCIA_DEVICE_PROD_ID123("Edimax Technology Inc.", "PCMCIA", "Ethernet Card", 0x738a0019, 0x281f1c5d, 0x5e9d92c0),
261	PCMCIA_DEVICE_PROD_ID123("EFA   ", "EFA207", "ETHERNET", 0x3d294be4, 0xeb9aab6c, 0x3ff7175b),
262	PCMCIA_DEVICE_PROD_ID123("I-O DATA", "PCLA", "ETHERNET", 0x1d55d7ec, 0xe4c64d34, 0x3ff7175b),
263	PCMCIA_DEVICE_PROD_ID123("IO DATA", "PCLATE", "ETHERNET", 0x547e66dc, 0x6b260753, 0x3ff7175b),
264	PCMCIA_DEVICE_PROD_ID123("KingMax Technology Inc.", "EN10-T2", "PCMCIA Ethernet Card", 0x932b7189, 0x699e4436, 0x6f6652e0),
265	PCMCIA_DEVICE_PROD_ID123("PCMCIA", "PCMCIA-ETHERNET-CARD", "UE2216", 0x281f1c5d, 0xd4cd2f20, 0xb87add82),
266	PCMCIA_DEVICE_PROD_ID123("PCMCIA", "PCMCIA-ETHERNET-CARD", "UE2620", 0x281f1c5d, 0xd4cd2f20, 0x7d3d83a8),
267	PCMCIA_DEVICE_PROD_ID1("2412LAN", 0x67f236ab),
268	PCMCIA_DEVICE_PROD_ID12("Allied Telesis, K.K.", "CentreCOM LA100-PCM-T V2 100/10M LAN PC Card", 0xbb7fbdd7, 0xcd91cc68),
269	PCMCIA_DEVICE_PROD_ID12("Allied Telesis K.K.", "LA100-PCM V2", 0x36634a66, 0xc6d05997),
270  	PCMCIA_DEVICE_PROD_ID12("Allied Telesis, K.K.", "CentreCOM LA-PCM_V2", 0xbb7fBdd7, 0x28e299f8),
271	PCMCIA_DEVICE_PROD_ID12("Allied Telesis K.K.", "LA-PCM V3", 0x36634a66, 0x62241d96),
272	PCMCIA_DEVICE_PROD_ID12("AmbiCom", "AMB8010", 0x5070a7f9, 0x82f96e96),
273	PCMCIA_DEVICE_PROD_ID12("AmbiCom", "AMB8610", 0x5070a7f9, 0x86741224),
274	PCMCIA_DEVICE_PROD_ID12("AmbiCom Inc", "AMB8002", 0x93b15570, 0x75ec3efb),
275	PCMCIA_DEVICE_PROD_ID12("AmbiCom Inc", "AMB8002T", 0x93b15570, 0x461c5247),
276	PCMCIA_DEVICE_PROD_ID12("AmbiCom Inc", "AMB8010", 0x93b15570, 0x82f96e96),
277	PCMCIA_DEVICE_PROD_ID12("AnyCom", "ECO Ethernet", 0x578ba6e7, 0x0a9888c1),
278	PCMCIA_DEVICE_PROD_ID12("AnyCom", "ECO Ethernet 10/100", 0x578ba6e7, 0x939fedbd),
279	PCMCIA_DEVICE_PROD_ID12("AROWANA", "PCMCIA Ethernet LAN Card", 0x313adbc8, 0x08d9f190),
280	PCMCIA_DEVICE_PROD_ID12("ASANTE", "FriendlyNet PC Card", 0x3a7ade0f, 0x41c64504),
281	PCMCIA_DEVICE_PROD_ID12("Billionton", "LNT-10TB", 0x552ab682, 0xeeb1ba6a),
282	PCMCIA_DEVICE_PROD_ID12("CF", "10Base-Ethernet", 0x44ebf863, 0x93ae4d79),
283	PCMCIA_DEVICE_PROD_ID12("COMPU-SHACK", "BASEline PCMCIA 10 MBit Ethernetadapter", 0xfa2e424d, 0xe9190d8a),
284	PCMCIA_DEVICE_PROD_ID12("COMPU-SHACK", "FASTline PCMCIA 10/100 Fast-Ethernet", 0xfa2e424d, 0x3953d9b9),
285	PCMCIA_DEVICE_PROD_ID12("CONTEC", "C-NET(PC)C-10L", 0x21cab552, 0xf6f90722),
286	PCMCIA_DEVICE_PROD_ID12("Corega,K.K.", "Ethernet LAN Card", 0x110d26d9, 0x9fd2f0a2),
287	PCMCIA_DEVICE_PROD_ID12("corega,K.K.", "Ethernet LAN Card", 0x9791a90e, 0x9fd2f0a2),
288	PCMCIA_DEVICE_PROD_ID12("CouplerlessPCMCIA", "100BASE", 0xee5af0ad, 0x7c2add04),
289	PCMCIA_DEVICE_PROD_ID12("CyQ've", "ELA-110E 10/100M LAN Card", 0x77008979, 0xfd184814),
290	PCMCIA_DEVICE_PROD_ID12("DataTrek.", "NetCard ", 0x5cd66d9d, 0x84697ce0),
291	PCMCIA_DEVICE_PROD_ID12("Dayna Communications, Inc.", "CommuniCard E", 0x0c629325, 0xb4e7dbaf),
292	PCMCIA_DEVICE_PROD_ID12("Digicom", "Palladio LAN 10/100", 0x697403d8, 0xe160b995),
293	PCMCIA_DEVICE_PROD_ID12("Digicom", "Palladio LAN 10/100 Dongless", 0x697403d8, 0xa6d3b233),
294	PCMCIA_DEVICE_PROD_ID12("D-Link", "DFE-650", 0x1a424a1c, 0x0f0073f9),
295	PCMCIA_DEVICE_PROD_ID12("Dual Speed", "10/100 PC Card", 0x725b842d, 0xf1efee84),
296	PCMCIA_DEVICE_PROD_ID12("Dual Speed", "10/100 Port Attached PC Card", 0x725b842d, 0x2db1f8e9),
297	PCMCIA_DEVICE_PROD_ID12("Dynalink", "L10BC", 0x55632fd5, 0xdc65f2b1),
298	PCMCIA_DEVICE_PROD_ID12("DYNALINK", "L10BC", 0x6a26d1cf, 0xdc65f2b1),
299	PCMCIA_DEVICE_PROD_ID12("E-CARD", "E-CARD", 0x6701da11, 0x6701da11),
300	PCMCIA_DEVICE_PROD_ID12("EIGER Labs Inc.", "Ethernet 10BaseT card", 0x53c864c6, 0xedd059f6),
301	PCMCIA_DEVICE_PROD_ID12("EIGER Labs Inc.", "Ethernet Combo card", 0x53c864c6, 0x929c486c),
302	PCMCIA_DEVICE_PROD_ID12("Ethernet", "Adapter", 0x00b2e941, 0x4b0d829e),
303	PCMCIA_DEVICE_PROD_ID12("Ethernet Adapter", "E2000 PCMCIA Ethernet", 0x96767301, 0x71fbbc61),
304	PCMCIA_DEVICE_PROD_ID12("Ethernet PCMCIA adapter", "EP-210", 0x8dd86181, 0xf2b52517),
305	PCMCIA_DEVICE_PROD_ID12("Fast Ethernet", "Adapter", 0xb4be14e3, 0x4b0d829e),
306	PCMCIA_DEVICE_PROD_ID12("Grey Cell", "GCS2000", 0x2a151fac, 0xf00555cb),
307	PCMCIA_DEVICE_PROD_ID12("Grey Cell", "GCS2220", 0x2a151fac, 0xc1b7e327),
308	PCMCIA_DEVICE_PROD_ID12("GVC", "NIC-2000p", 0x76e171bd, 0x6eb1c947),
309	PCMCIA_DEVICE_PROD_ID12("IBM Corp.", "Ethernet", 0xe3736c88, 0x00b2e941),
310	PCMCIA_DEVICE_PROD_ID12("IC-CARD", "IC-CARD", 0x60cb09a6, 0x60cb09a6),
311	PCMCIA_DEVICE_PROD_ID12("IC-CARD+", "IC-CARD+", 0x93693494, 0x93693494),
312	PCMCIA_DEVICE_PROD_ID12("IO DATA", "PCETTX", 0x547e66dc, 0x6fc5459b),
313	PCMCIA_DEVICE_PROD_ID12("iPort", "10/100 Ethernet Card", 0x56c538d2, 0x11b0ffc0),
314	PCMCIA_DEVICE_PROD_ID12("KANSAI ELECTRIC CO.,LTD", "KLA-PCM/T", 0xb18dc3b4, 0xcc51a956),
315	PCMCIA_DEVICE_PROD_ID12("KCI", "PE520 PCMCIA Ethernet Adapter", 0xa89b87d3, 0x1eb88e64),
316	PCMCIA_DEVICE_PROD_ID12("KINGMAX", "EN10T2T", 0x7bcb459a, 0xa5c81fa5),
317	PCMCIA_DEVICE_PROD_ID12("Kingston", "KNE-PC2", 0x1128e633, 0xce2a89b3),
318	PCMCIA_DEVICE_PROD_ID12("Kingston Technology Corp.", "EtheRx PC Card Ethernet Adapter", 0x313c7be3, 0x0afb54a2),
319	PCMCIA_DEVICE_PROD_ID12("Laneed", "LD-10/100CD", 0x1b7827b2, 0xcda71d1c),
320	PCMCIA_DEVICE_PROD_ID12("Laneed", "LD-CDF", 0x1b7827b2, 0xfec71e40),
321	PCMCIA_DEVICE_PROD_ID12("Laneed", "LD-CDL/T", 0x1b7827b2, 0x79fba4f7),
322	PCMCIA_DEVICE_PROD_ID12("Laneed", "LD-CDS", 0x1b7827b2, 0x931afaab),
323	PCMCIA_DEVICE_PROD_ID12("LEMEL", "LM-N89TX PRO", 0xbbefb52f, 0xd2897a97),
324	PCMCIA_DEVICE_PROD_ID12("Linksys", "Combo PCMCIA EthernetCard (EC2T)", 0x0733cc81, 0x32ee8c78),
325	PCMCIA_DEVICE_PROD_ID12("Linksys", "EtherFast 10/100 Integrated PC Card (PCM100)", 0x0733cc81, 0x453c3f9d),
326	PCMCIA_DEVICE_PROD_ID12("Linksys", "EtherFast 10/100 PC Card (PCMPC100)", 0x0733cc81, 0x66c5a389),
327	PCMCIA_DEVICE_PROD_ID12("Linksys", "EtherFast 10/100 PC Card (PCMPC100 V2)", 0x0733cc81, 0x3a3b28e9),
328	PCMCIA_DEVICE_PROD_ID12("Linksys", "HomeLink Phoneline + 10/100 Network PC Card (PCM100H1)", 0x733cc81, 0x7a3e5c3a),
329	PCMCIA_DEVICE_PROD_ID12("Logitec", "LPM-LN100TX", 0x88fcdeda, 0x6d772737),
330	PCMCIA_DEVICE_PROD_ID12("Logitec", "LPM-LN100TE", 0x88fcdeda, 0x0e714bee),
331	PCMCIA_DEVICE_PROD_ID12("Logitec", "LPM-LN20T", 0x88fcdeda, 0x81090922),
332	PCMCIA_DEVICE_PROD_ID12("Logitec", "LPM-LN10TE", 0x88fcdeda, 0xc1e2521c),
333	PCMCIA_DEVICE_PROD_ID12("LONGSHINE", "PCMCIA Ethernet Card", 0xf866b0b0, 0x6f6652e0),
334	PCMCIA_DEVICE_PROD_ID12("MACNICA", "ME1-JEIDA", 0x20841b68, 0xaf8a3578),
335	PCMCIA_DEVICE_PROD_ID12("Macsense", "MPC-10", 0xd830297f, 0xd265c307),
336	PCMCIA_DEVICE_PROD_ID12("Matsushita Electric Industrial Co.,LTD.", "CF-VEL211", 0x44445376, 0x8ded41d4),
337	PCMCIA_DEVICE_PROD_ID12("MAXTECH", "PCN2000", 0x78d64bc0, 0xca0ca4b8),
338	PCMCIA_DEVICE_PROD_ID12("MELCO", "LPC2-T", 0x481e0094, 0xa2eb0cf3),
339	PCMCIA_DEVICE_PROD_ID12("Microcom C.E.", "Travel Card LAN 10/100", 0x4b91cec7, 0xe70220d6),
340	PCMCIA_DEVICE_PROD_ID12("Microdyne", "NE4200", 0x2e6da59b, 0x0478e472),
341	PCMCIA_DEVICE_PROD_ID12("MIDORI ELEC.", "LT-PCMT", 0x648d55c1, 0xbde526c7),
342	PCMCIA_DEVICE_PROD_ID12("National Semiconductor", "InfoMover 4100", 0x36e1191f, 0x60c229b9),
343	PCMCIA_DEVICE_PROD_ID12("National Semiconductor", "InfoMover NE4100", 0x36e1191f, 0xa6617ec8),
344	PCMCIA_DEVICE_PROD_ID12("Network Everywhere", "Fast Ethernet 10/100 PC Card", 0x820a67b6, 0x31ed1a5f),
345	PCMCIA_DEVICE_PROD_ID12("NextCom K.K.", "Next Hawk", 0xaedaec74, 0xad050ef1),
346	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "10/100Mbps Ethernet Card", 0x281f1c5d, 0x6e41773b),
347	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "ETHERNET", 0x281f1c5d, 0x3ff7175b),
348	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "Ethernet 10BaseT Card", 0x281f1c5d, 0x4de2f6c8),
349	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "Ethernet Card", 0x281f1c5d, 0x5e9d92c0),
350	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "Ethernet Combo card", 0x281f1c5d, 0x929c486c),
351	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "ETHERNET V1.0", 0x281f1c5d, 0x4d8817c8),
352	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "FastEthernet", 0x281f1c5d, 0xfe871eeb),
353	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "Fast-Ethernet", 0x281f1c5d, 0x45f1f3b4),
354	PCMCIA_DEVICE_PROD_ID12("PCMCIA LAN", "Ethernet", 0x7500e246, 0x00b2e941),
355	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "LNT-10TN", 0x281f1c5d, 0xe707f641),
356	PCMCIA_DEVICE_PROD_ID12("PCMCIAs", "ComboCard", 0xdcfe12d3, 0xcd8906cc),
357	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "UE2212", 0x281f1c5d, 0xbf17199b),
358	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "    Ethernet NE2000 Compatible", 0x281f1c5d, 0x42d5d7e1),
359	PCMCIA_DEVICE_PROD_ID12("PRETEC", "Ethernet CompactLAN 10baseT 3.3V", 0xebf91155, 0x30074c80),
360	PCMCIA_DEVICE_PROD_ID12("PRETEC", "Ethernet CompactLAN 10BaseT 3.3V", 0xebf91155, 0x7f5a4f50),
361	PCMCIA_DEVICE_PROD_ID12("Psion Dacom", "Gold Card Ethernet", 0xf5f025c2, 0x3a30e110),
362	PCMCIA_DEVICE_PROD_ID12("=RELIA==", "Ethernet", 0xcdd0644a, 0x00b2e941),
363	PCMCIA_DEVICE_PROD_ID12("RP", "1625B Ethernet NE2000 Compatible", 0xe3e66e22, 0xb96150df),
364	PCMCIA_DEVICE_PROD_ID12("RPTI", "EP400 Ethernet NE2000 Compatible", 0xdc6f88fd, 0x4a7e2ae0),
365	PCMCIA_DEVICE_PROD_ID12("RPTI", "EP401 Ethernet NE2000 Compatible", 0xdc6f88fd, 0x4bcbd7fd),
366	PCMCIA_DEVICE_PROD_ID12("RPTI LTD.", "EP400", 0xc53ac515, 0x81e39388),
367	PCMCIA_DEVICE_PROD_ID12("SCM", "Ethernet Combo card", 0xbdc3b102, 0x929c486c),
368	PCMCIA_DEVICE_PROD_ID12("Seiko Epson Corp.", "Ethernet", 0x09928730, 0x00b2e941),
369	PCMCIA_DEVICE_PROD_ID12("SMC", "EZCard-10-PCMCIA", 0xc4f8b18b, 0xfb21d265),
370	PCMCIA_DEVICE_PROD_ID12("Telecom Device K.K.", "SuperSocket RE450T", 0x466b05f0, 0x8b74bc4f),
371	PCMCIA_DEVICE_PROD_ID12("Telecom Device K.K.", "SuperSocket RE550T", 0x466b05f0, 0x33c8db2a),
372	PCMCIA_DEVICE_PROD_ID13("Hypertec",  "EP401", 0x8787bec7, 0xf6e4a31e),
373	PCMCIA_DEVICE_PROD_ID13("KingMax Technology Inc.", "Ethernet Card", 0x932b7189, 0x5e9d92c0),
374	PCMCIA_DEVICE_PROD_ID13("LONGSHINE", "EP401", 0xf866b0b0, 0xf6e4a31e),
375	PCMCIA_DEVICE_PROD_ID1("CyQ've 10 Base-T LAN CARD", 0x94faf360),
376	PCMCIA_DEVICE_PROD_ID1("EP-210 PCMCIA LAN CARD.", 0x8850b4de),
377	PCMCIA_DEVICE_PROD_ID1("ETHER-C16", 0x06a8514f),
378	PCMCIA_DEVICE_PROD_ID1("NE2000 Compatible", 0x75b8ad5a),
379	PCMCIA_DEVICE_PROD_ID2("EN-6200P2", 0xa996d078),
380
381
382	PCMCIA_PFC_DEVICE_PROD_ID12(0, "MICRO RESEARCH", "COMBO-L/M-336", 0xb2ced065, 0x3ced0555),
383 */
384
385/*
386 *      PC Card (PCMCIA) specific code.
387 */
388static int	ed_pccard_probe(device_t);
389static int	ed_pccard_attach(device_t);
390static void	ed_pccard_tick(void *);
391
392static int	ed_pccard_dl100xx(device_t dev, const struct ed_product *);
393static void	ed_pccard_dl100xx_mii_reset(struct ed_softc *sc);
394static u_int	ed_pccard_dl100xx_mii_readbits(struct ed_softc *sc, int nbits);
395static void	ed_pccard_dl100xx_mii_writebits(struct ed_softc *sc, u_int val,
396    int nbits);
397
398static int	ed_pccard_ax88x90(device_t dev, const struct ed_product *);
399static u_int	ed_pccard_ax88x90_mii_readbits(struct ed_softc *sc, int nbits);
400static void	ed_pccard_ax88x90_mii_writebits(struct ed_softc *sc, u_int val,
401    int nbits);
402
403static int	ed_miibus_readreg(device_t dev, int phy, int reg);
404static int	ed_ifmedia_upd(struct ifnet *);
405static void	ed_ifmedia_sts(struct ifnet *, struct ifmediareq *);
406
407static int	ed_pccard_tc5299j(device_t dev, const struct ed_product *);
408static u_int	ed_pccard_tc5299j_mii_readbits(struct ed_softc *sc, int nbits);
409static void	ed_pccard_tc5299j_mii_writebits(struct ed_softc *sc, u_int val,
410    int nbits);
411
412static void
413ed_pccard_print_entry(const struct ed_product *pp)
414{
415	int i;
416
417	printf("Product entry: ");
418	if (pp->prod.pp_name)
419		printf("name='%s',", pp->prod.pp_name);
420	printf("vendor=%#x,product=%#x", pp->prod.pp_vendor,
421	    pp->prod.pp_product);
422	for (i = 0; i < 4; i++)
423		if (pp->prod.pp_cis[i])
424			printf(",CIS%d='%s'", i, pp->prod.pp_cis[i]);
425	printf("\n");
426}
427
428static int
429ed_pccard_probe(device_t dev)
430{
431	const struct ed_product *pp, *pp2;
432	int		error, first = 1;
433	uint32_t	fcn = PCCARD_FUNCTION_UNSPEC;
434
435	/* Make sure we're a network function */
436	error = pccard_get_function(dev, &fcn);
437	if (error != 0)
438		return (error);
439
440	if ((pp = (const struct ed_product *) pccard_product_lookup(dev,
441	    (const struct pccard_product *) ed_pccard_products,
442	    sizeof(ed_pccard_products[0]), NULL)) != NULL) {
443		if (pp->prod.pp_name != NULL)
444			device_set_desc(dev, pp->prod.pp_name);
445		/*
446		 * Some devices don't ID themselves as network, but
447		 * that's OK if the flags say so.
448		 */
449		if (!(pp->flags & NE2000DVF_ANYFUNC) &&
450		    fcn != PCCARD_FUNCTION_NETWORK)
451			return (ENXIO);
452		/*
453		 * Some devices match multiple entries.  Report that
454		 * as a warning to help cull the table
455		 */
456		pp2 = pp;
457		while ((pp2 = (const struct ed_product *)pccard_product_lookup(
458		    dev, (const struct pccard_product *)(pp2 + 1),
459		    sizeof(ed_pccard_products[0]), NULL)) != NULL) {
460			if (first) {
461				device_printf(dev,
462    "Warning: card matches multiple entries.  Report to imp@freebsd.org\n");
463				ed_pccard_print_entry(pp);
464				first = 0;
465			}
466			ed_pccard_print_entry(pp2);
467		}
468
469		return (0);
470	}
471	return (ENXIO);
472}
473
474static int
475ed_pccard_rom_mac(device_t dev, uint8_t *enaddr)
476{
477	struct ed_softc *sc = device_get_softc(dev);
478	uint8_t romdata[32], sum;
479	int i;
480
481	/*
482	 * Read in the rom data at location 0.  Since there are no
483	 * NE-1000 based PC Card devices, we'll assume we're 16-bit.
484	 *
485	 * In researching what format this takes, I've found that the
486	 * following appears to be true for multiple cards based on
487	 * observation as well as datasheet digging.
488	 *
489	 * Data is stored in some ROM and is copied out 8 bits at a time
490	 * into 16-bit wide locations.  This means that the odd locations
491	 * of the ROM are not used (and can be either 0 or ff).
492	 *
493	 * The contents appears to be as follows:
494	 * PROM   RAM
495	 * Offset Offset	What
496	 *  0      0	ENETADDR 0
497	 *  1      2	ENETADDR 1
498	 *  2      4	ENETADDR 2
499	 *  3      6	ENETADDR 3
500	 *  4      8	ENETADDR 4
501	 *  5     10	ENETADDR 5
502	 *  6-13  12-26 Reserved (varies by manufacturer)
503	 * 14     28	0x57
504	 * 15     30    0x57
505	 *
506	 * Some manufacturers have another image of enetaddr from
507	 * PROM offset 0x10 to 0x15 with 0x42 in 0x1e and 0x1f, but
508	 * this doesn't appear to be universally documented in the
509	 * datasheets.  Some manufactuers have a card type, card config
510	 * checksums, etc encoded into PROM offset 6-13, but deciphering it
511	 * requires more knowledge about the exact underlying chipset than
512	 * we possess (and maybe can possess).
513	 */
514	ed_pio_readmem(sc, 0, romdata, 32);
515	if (bootverbose)
516		device_printf(dev, "ROM DATA: %32D\n", romdata, " ");
517	if (romdata[28] != 0x57 || romdata[30] != 0x57)
518		return (0);
519	for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
520		sum |= romdata[i * 2];
521	if (sum == 0)
522		return (0);
523	for (i = 0; i < ETHER_ADDR_LEN; i++)
524		enaddr[i] = romdata[i * 2];
525	return (1);
526}
527
528static int
529ed_pccard_add_modem(device_t dev)
530{
531	device_printf(dev, "Need to write this code\n");
532	return 0;
533}
534
535static int
536ed_pccard_kick_phy(struct ed_softc *sc)
537{
538	struct mii_softc *miisc;
539	struct mii_data *mii;
540
541	/*
542	 * Many of the PHYs that wind up on PC Cards are weird in
543	 * this way.  Generally, we don't need to worry so much about
544	 * the Isolation protocol since there's only one PHY in
545	 * these designs, so this workaround is reasonable.
546	 */
547	mii = device_get_softc(sc->miibus);
548	LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
549		miisc->mii_flags |= MIIF_FORCEANEG;
550		mii_phy_reset(miisc);
551	}
552	return (mii_mediachg(mii));
553}
554
555static int
556ed_pccard_media_ioctl(struct ed_softc *sc, struct ifreq *ifr, u_long command)
557{
558	struct mii_data *mii;
559
560	if (sc->miibus == NULL)
561		return (EINVAL);
562	mii = device_get_softc(sc->miibus);
563	return (ifmedia_ioctl(sc->ifp, ifr, &mii->mii_media, command));
564}
565
566
567static void
568ed_pccard_mediachg(struct ed_softc *sc)
569{
570	struct mii_data *mii;
571
572	if (sc->miibus == NULL)
573		return;
574	mii = device_get_softc(sc->miibus);
575	mii_mediachg(mii);
576}
577
578static int
579ed_pccard_attach(device_t dev)
580{
581	u_char sum;
582	u_char enaddr[ETHER_ADDR_LEN];
583	const struct ed_product *pp;
584	int	error, i, flags, port_rid, modem_rid;
585	struct ed_softc *sc = device_get_softc(dev);
586	u_long size;
587	static uint16_t *intr_vals[] = {NULL, NULL};
588
589	sc->dev = dev;
590	if ((pp = (const struct ed_product *) pccard_product_lookup(dev,
591	    (const struct pccard_product *) ed_pccard_products,
592		 sizeof(ed_pccard_products[0]), NULL)) == NULL) {
593		printf("Can't find\n");
594		return (ENXIO);
595	}
596	modem_rid = port_rid = -1;
597	if (pp->flags & NE2000DVF_MODEM) {
598		for (i = 0; i < 4; i++) {
599			size = bus_get_resource_count(dev, SYS_RES_IOPORT, i);
600			if (size == ED_NOVELL_IO_PORTS)
601				port_rid = i;
602			else if (size == 8)
603				modem_rid = i;
604		}
605		if (port_rid == -1) {
606			device_printf(dev, "Cannot locate my ports!\n");
607			return (ENXIO);
608		}
609	} else {
610		port_rid = 0;
611	}
612	/* Allocate the port resource during setup. */
613	error = ed_alloc_port(dev, port_rid, ED_NOVELL_IO_PORTS);
614	if (error) {
615		printf("alloc_port failed\n");
616		return (error);
617	}
618	if (rman_get_size(sc->port_res) == ED_NOVELL_IO_PORTS / 2) {
619		port_rid++;
620		sc->port_res2 = bus_alloc_resource(dev, SYS_RES_IOPORT,
621		    &port_rid, 0ul, ~0ul, 1, RF_ACTIVE);
622		if (sc->port_res2 == NULL ||
623		    rman_get_size(sc->port_res2) != ED_NOVELL_IO_PORTS / 2) {
624			error = ENXIO;
625			goto bad;
626		}
627	}
628	error = ed_alloc_irq(dev, 0, 0);
629	if (error)
630		goto bad;
631
632	/*
633	 * Determine which chipset we are.  Almost all the PC Card chipsets
634	 * have the Novel ASIC and NIC offsets.  There's 2 known cards that
635	 * follow the WD80x3 conventions, which are handled as a special case.
636	 */
637	sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
638	sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
639	error = ENXIO;
640	flags = device_get_flags(dev);
641	if (error != 0)
642		error = ed_pccard_dl100xx(dev, pp);
643	if (error != 0)
644		error = ed_pccard_ax88x90(dev, pp);
645	if (error != 0)
646		error = ed_pccard_tc5299j(dev, pp);
647	if (error != 0) {
648		error = ed_probe_Novell_generic(dev, flags);
649		printf("Novell probe generic %d\n", error);
650	}
651	if (error != 0 && (pp->flags & NE2000DVF_TOSHIBA)) {
652		flags |= ED_FLAGS_TOSH_ETHER;
653		flags |= ED_FLAGS_PCCARD;
654		sc->asic_offset = ED_WD_ASIC_OFFSET;
655		sc->nic_offset  = ED_WD_NIC_OFFSET;
656		error = ed_probe_WD80x3_generic(dev, flags, intr_vals);
657	}
658	if (error)
659		goto bad;
660
661	/*
662	 * There are several ways to get the MAC address for the card.
663	 * Some of the above probe routines can fill in the enaddr.  If
664	 * not, we run through a number of 'well known' locations:
665	 *	(1) From the PC Card FUNCE
666	 *	(2) From offset 0 in the shared memory
667	 *	(3) From a hinted offset in attribute memory
668	 *	(4) From 0xff0 in attribute memory
669	 * If we can't get a non-zero MAC address from this list, we fail.
670	 */
671	for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
672		sum |= sc->enaddr[i];
673	if (sum == 0) {
674		pccard_get_ether(dev, enaddr);
675		if (bootverbose)
676			device_printf(dev, "CIS MAC %6D\n", enaddr, ":");
677		for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
678			sum |= enaddr[i];
679		if (sum == 0 && ed_pccard_rom_mac(dev, enaddr)) {
680			if (bootverbose)
681				device_printf(dev, "ROM mac %6D\n", enaddr,
682				    ":");
683			sum++;
684		}
685		if (sum == 0 && pp->flags & NE2000DVF_ENADDR) {
686			for (i = 0; i < ETHER_ADDR_LEN; i++) {
687				pccard_attr_read_1(dev, pp->enoff + i * 2,
688				    enaddr + i);
689				sum |= enaddr[i];
690			}
691			if (bootverbose)
692				device_printf(dev, "Hint %x MAC %6D\n",
693				    pp->enoff, enaddr, ":");
694		}
695		if (sum == 0) {
696			for (i = 0; i < ETHER_ADDR_LEN; i++) {
697				pccard_attr_read_1(dev, ED_DEFAULT_MAC_OFFSET +
698				    i * 2, enaddr + i);
699				sum |= enaddr[i];
700			}
701			if (bootverbose)
702				device_printf(dev, "Fallback MAC %6D\n",
703				    enaddr, ":");
704		}
705		if (sum == 0) {
706			device_printf(dev, "Cannot extract MAC address.\n");
707			ed_release_resources(dev);
708			return (ENXIO);
709		}
710		bcopy(enaddr, sc->enaddr, ETHER_ADDR_LEN);
711	}
712
713	error = ed_attach(dev);
714	if (error)
715		goto bad;
716 	if (sc->chip_type == ED_CHIP_TYPE_DL10019 ||
717	    sc->chip_type == ED_CHIP_TYPE_DL10022) {
718		/* Probe for an MII bus, but ignore errors. */
719		ed_pccard_dl100xx_mii_reset(sc);
720		(void)mii_phy_probe(dev, &sc->miibus, ed_ifmedia_upd,
721		    ed_ifmedia_sts);
722	} else if (sc->chip_type == ED_CHIP_TYPE_AX88190 ||
723	    sc->chip_type == ED_CHIP_TYPE_AX88790) {
724		if ((error = mii_phy_probe(dev, &sc->miibus, ed_ifmedia_upd,
725		     ed_ifmedia_sts)) != 0) {
726			device_printf(dev, "Missing mii %d!\n", error);
727			goto bad;
728		}
729
730	} else if (sc->chip_type == ED_CHIP_TYPE_TC5299J) {
731		if ((error = mii_phy_probe(dev, &sc->miibus, ed_ifmedia_upd,
732		     ed_ifmedia_sts)) != 0) {
733			device_printf(dev, "Missing mii!\n");
734			goto bad;
735		}
736
737	}
738	if (sc->miibus != NULL) {
739		sc->sc_tick = ed_pccard_tick;
740		sc->sc_mediachg = ed_pccard_mediachg;
741		sc->sc_media_ioctl = ed_pccard_media_ioctl;
742		ed_pccard_kick_phy(sc);
743	} else {
744		ed_gen_ifmedia_init(sc);
745	}
746	if (modem_rid != -1)
747		ed_pccard_add_modem(dev);
748
749	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
750	    NULL, edintr, sc, &sc->irq_handle);
751	if (error) {
752		device_printf(dev, "setup intr failed %d \n", error);
753		goto bad;
754	}
755
756	return (0);
757bad:
758	ed_detach(dev);
759	return (error);
760}
761
762/*
763 * Probe the Ethernet MAC addrees for PCMCIA Linksys EtherFast 10/100
764 * and compatible cards (DL10019C Ethernet controller).
765 */
766static int
767ed_pccard_dl100xx(device_t dev, const struct ed_product *pp)
768{
769	struct ed_softc *sc = device_get_softc(dev);
770	u_char sum;
771	uint8_t id;
772	u_int   memsize;
773	int i, error;
774
775	if (!(pp->flags & NE2000DVF_DL100XX))
776		return (ENXIO);
777	if (bootverbose)
778		device_printf(dev, "Trying DL100xx probing\n");
779	error = ed_probe_Novell_generic(dev, device_get_flags(dev));
780	if (bootverbose && error)
781		device_printf(dev, "Novell generic probe failed: %d\n", error);
782	if (error != 0)
783		return (error);
784
785	/*
786	 * Linksys registers(offset from ASIC base)
787	 *
788	 * 0x04-0x09 : Physical Address Register 0-5 (PAR0-PAR5)
789	 * 0x0A      : Card ID Register (CIR)
790	 * 0x0B      : Check Sum Register (SR)
791	 */
792	for (sum = 0, i = 0x04; i < 0x0c; i++)
793		sum += ed_asic_inb(sc, i);
794	if (sum != 0xff) {
795		if (bootverbose)
796			device_printf(dev, "Bad checksum %#x\n", sum);
797		return (ENXIO);		/* invalid DL10019C */
798	}
799	if (bootverbose)
800		device_printf(dev, "CIR is %d\n", ed_asic_inb(sc, 0xa));
801	for (i = 0; i < ETHER_ADDR_LEN; i++)
802		sc->enaddr[i] = ed_asic_inb(sc, 0x04 + i);
803	ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
804	id = ed_asic_inb(sc, 0xf);
805	sc->isa16bit = 1;
806	/*
807	 * Hard code values based on the datasheet.  We're NE-2000 compatible
808	 * NIC with 24kb of packet memory starting at 24k offset.  These
809	 * cards also work with 16k at 16k, but don't work with 24k at 16k
810	 * or 32k at 16k.
811	 */
812	sc->type = ED_TYPE_NE2000;
813	sc->mem_start = 24 * 1024;
814	memsize = sc->mem_size = 24 * 1024;
815	sc->mem_end = sc->mem_start + memsize;
816	sc->tx_page_start = memsize / ED_PAGE_SIZE;
817	sc->txb_cnt = 3;
818	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
819	sc->rec_page_stop = sc->tx_page_start + memsize / ED_PAGE_SIZE;
820
821	sc->mem_ring = sc->mem_start + sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE;
822
823	ed_nic_outb(sc, ED_P0_PSTART, sc->mem_start / ED_PAGE_SIZE);
824	ed_nic_outb(sc, ED_P0_PSTOP, sc->mem_end / ED_PAGE_SIZE);
825	sc->vendor = ED_VENDOR_NOVELL;
826	sc->chip_type = (id & 0x90) == 0x90 ?
827	    ED_CHIP_TYPE_DL10022 : ED_CHIP_TYPE_DL10019;
828	sc->type_str = ((id & 0x90) == 0x90) ? "DL10022" : "DL10019";
829	sc->mii_readbits = ed_pccard_dl100xx_mii_readbits;
830	sc->mii_writebits = ed_pccard_dl100xx_mii_writebits;
831	return (0);
832}
833
834/* MII bit-twiddling routines for cards using Dlink chipset */
835#define DL100XX_MIISET(sc, x) ed_asic_outb(sc, ED_DL100XX_MIIBUS, \
836    ed_asic_inb(sc, ED_DL100XX_MIIBUS) | (x))
837#define DL100XX_MIICLR(sc, x) ed_asic_outb(sc, ED_DL100XX_MIIBUS, \
838    ed_asic_inb(sc, ED_DL100XX_MIIBUS) & ~(x))
839
840static void
841ed_pccard_dl100xx_mii_reset(struct ed_softc *sc)
842{
843	if (sc->chip_type != ED_CHIP_TYPE_DL10022)
844		return;
845
846	ed_asic_outb(sc, ED_DL100XX_MIIBUS, ED_DL10022_MII_RESET2);
847	DELAY(10);
848	ed_asic_outb(sc, ED_DL100XX_MIIBUS,
849	    ED_DL10022_MII_RESET2 | ED_DL10022_MII_RESET1);
850	DELAY(10);
851	ed_asic_outb(sc, ED_DL100XX_MIIBUS, ED_DL10022_MII_RESET2);
852	DELAY(10);
853	ed_asic_outb(sc, ED_DL100XX_MIIBUS,
854	    ED_DL10022_MII_RESET2 | ED_DL10022_MII_RESET1);
855	DELAY(10);
856	ed_asic_outb(sc, ED_DL100XX_MIIBUS, 0);
857}
858
859static void
860ed_pccard_dl100xx_mii_writebits(struct ed_softc *sc, u_int val, int nbits)
861{
862	int i;
863
864	DL100XX_MIISET(sc, ED_DL100XX_MII_DIROUT);
865	for (i = nbits - 1; i >= 0; i--) {
866		if ((val >> i) & 1)
867			DL100XX_MIISET(sc, ED_DL100XX_MII_DATAOUT);
868		else
869			DL100XX_MIICLR(sc, ED_DL100XX_MII_DATAOUT);
870		DL100XX_MIISET(sc, ED_DL100XX_MII_CLK);
871		DL100XX_MIICLR(sc, ED_DL100XX_MII_CLK);
872	}
873}
874
875static u_int
876ed_pccard_dl100xx_mii_readbits(struct ed_softc *sc, int nbits)
877{
878	int i;
879	u_int val = 0;
880
881	DL100XX_MIICLR(sc, ED_DL100XX_MII_DIROUT);
882	for (i = nbits - 1; i >= 0; i--) {
883		DL100XX_MIISET(sc, ED_DL100XX_MII_CLK);
884		val <<= 1;
885		if (ed_asic_inb(sc, ED_DL100XX_MIIBUS) & ED_DL100XX_MII_DATAIN)
886			val++;
887		DL100XX_MIICLR(sc, ED_DL100XX_MII_CLK);
888	}
889	return val;
890}
891
892static void
893ed_pccard_ax88x90_reset(struct ed_softc *sc)
894{
895	int i;
896
897	/* Reset Card */
898	ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP | ED_CR_PAGE_0);
899	ed_asic_outb(sc, ED_NOVELL_RESET, ed_asic_inb(sc, ED_NOVELL_RESET));
900
901	/* Wait for the RST bit to assert, but cap it at 10ms */
902	for (i = 10000; !(ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST) && i > 0;
903	     i--)
904		continue;
905	ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RST);	/* ACK INTR */
906	if (i == 0)
907		device_printf(sc->dev, "Reset didn't finish\n");
908}
909
910/*
911 * Probe and vendor-specific initialization routine for ax88x90 boards
912 */
913static int
914ed_probe_ax88x90_generic(device_t dev, int flags)
915{
916	struct ed_softc *sc = device_get_softc(dev);
917	u_int   memsize;
918	static char test_pattern[32] = "THIS is A memory TEST pattern";
919	char    test_buffer[32];
920
921	ed_pccard_ax88x90_reset(sc);
922	DELAY(10*1000);
923
924	/* Make sure that we really have an 8390 based board */
925	if (!ed_probe_generic8390(sc))
926		return (ENXIO);
927
928	sc->vendor = ED_VENDOR_NOVELL;
929	sc->mem_shared = 0;
930	sc->cr_proto = ED_CR_RD2;
931
932	/*
933	 * This prevents packets from being stored in the NIC memory when the
934	 * readmem routine turns on the start bit in the CR.  We write some
935	 * bytes in word mode and verify we can read them back.  If we can't
936	 * then we don't have an AX88x90 chip here.
937	 */
938	sc->isa16bit = 1;
939	ed_nic_outb(sc, ED_P0_RCR, ED_RCR_MON);
940	ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
941	ed_pio_writemem(sc, test_pattern, 16384, sizeof(test_pattern));
942	ed_pio_readmem(sc, 16384, test_buffer, sizeof(test_pattern));
943	if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)) != 0)
944		return (ENXIO);
945
946	/*
947	 * Hard code values based on the datasheet.  We're NE-2000 compatible
948	 * NIC with 16kb of packet memory starting at 16k offset.
949	 */
950	sc->type = ED_TYPE_NE2000;
951	memsize = sc->mem_size = 16*1024;
952	sc->mem_start = 16 * 1024;
953	if (ed_asic_inb(sc, ED_AX88X90_TEST) != 0)
954		sc->chip_type = ED_CHIP_TYPE_AX88790;
955	else {
956		sc->chip_type = ED_CHIP_TYPE_AX88190;
957		/*
958		 * The AX88190 (not A) has external 64k SRAM.  Probe for this
959		 * here.  Most of the cards I have either use the AX88190A
960		 * part, or have only 32k SRAM for some reason, so I don't
961		 * know if this works or not.
962		 */
963		ed_pio_writemem(sc, test_pattern, 32768, sizeof(test_pattern));
964		ed_pio_readmem(sc, 32768, test_buffer, sizeof(test_pattern));
965		if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)) == 0) {
966			sc->mem_start = 2*1024;
967			memsize = sc->mem_size = 62 * 1024;
968		}
969	}
970	sc->mem_end = sc->mem_start + memsize;
971	sc->tx_page_start = memsize / ED_PAGE_SIZE;
972	if (sc->mem_size > 16 * 1024)
973		sc->txb_cnt = 3;
974	else
975		sc->txb_cnt = 2;
976	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
977	sc->rec_page_stop = sc->tx_page_start + memsize / ED_PAGE_SIZE;
978
979	sc->mem_ring = sc->mem_start + sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE;
980
981	ed_nic_outb(sc, ED_P0_PSTART, sc->mem_start / ED_PAGE_SIZE);
982	ed_nic_outb(sc, ED_P0_PSTOP, sc->mem_end / ED_PAGE_SIZE);
983
984	/* Get the mac before we go -- It's just at 0x400 in "SRAM" */
985	ed_pio_readmem(sc, 0x400, sc->enaddr, ETHER_ADDR_LEN);
986
987	/* clear any pending interrupts that might have occurred above */
988	ed_nic_outb(sc, ED_P0_ISR, 0xff);
989	sc->sc_write_mbufs = ed_pio_write_mbufs;
990	return (0);
991}
992
993static int
994ed_pccard_ax88x90_check_mii(device_t dev, struct ed_softc *sc)
995{
996	int	i, id;
997
998	/*
999	 * All AX88x90 devices have MII and a PHY, so we use this to weed out
1000	 * chips that would otherwise make it through the tests we have after
1001	 * this point.
1002	 */
1003	for (i = 0; i < 32; i++) {
1004		id = ed_miibus_readreg(dev, i, MII_BMSR);
1005		if (id != 0 && id != 0xffff)
1006			break;
1007	}
1008	/*
1009	 * Found one, we're good.
1010	 */
1011	if (i != 32)
1012		return (0);
1013	/*
1014	 * Didn't find anything, so try to power up and try again.  The PHY
1015	 * may be not responding because we're in power down mode.
1016	 */
1017	if (sc->chip_type == ED_CHIP_TYPE_AX88190)
1018		return (ENXIO);
1019	pccard_ccr_write_1(dev, PCCARD_CCR_STATUS, PCCARD_CCR_STATUS_PWRDWN);
1020	for (i = 0; i < 32; i++) {
1021		id = ed_miibus_readreg(dev, i, MII_BMSR);
1022		if (id != 0 && id != 0xffff)
1023			break;
1024	}
1025	/*
1026	 * Still no joy?  We're AFU, punt.
1027	 */
1028	if (i == 32)
1029		return (ENXIO);
1030	return (0);
1031
1032}
1033
1034/*
1035 * Special setup for AX88[17]90
1036 */
1037static int
1038ed_pccard_ax88x90(device_t dev, const struct ed_product *pp)
1039{
1040	int	error;
1041	int iobase;
1042	struct	ed_softc *sc = device_get_softc(dev);
1043
1044	if (!(pp->flags & NE2000DVF_AX88X90))
1045		return (ENXIO);
1046
1047	if (bootverbose)
1048		device_printf(dev, "Checking AX88x90\n");
1049
1050	/*
1051	 * Set the IOBASE Register.  The AX88x90 cards are potentially
1052	 * multifunction cards, and thus requires a slight workaround.
1053	 * We write the address the card is at, on the off chance that this
1054	 * card is not MFC.
1055	 * XXX I'm not sure that this is still needed...
1056	 */
1057	iobase = rman_get_start(sc->port_res);
1058	pccard_ccr_write_1(dev, PCCARD_CCR_IOBASE0, iobase & 0xff);
1059	pccard_ccr_write_1(dev, PCCARD_CCR_IOBASE1, (iobase >> 8) & 0xff);
1060
1061	sc->mii_readbits = ed_pccard_ax88x90_mii_readbits;
1062	sc->mii_writebits = ed_pccard_ax88x90_mii_writebits;
1063	error = ed_probe_ax88x90_generic(dev, device_get_flags(dev));
1064	if (error) {
1065		if (bootverbose)
1066			device_printf(dev, "probe ax88x90 failed %d\n",
1067			    error);
1068		goto fail;
1069	}
1070	error = ed_pccard_ax88x90_check_mii(dev, sc);
1071	if (error)
1072		goto fail;
1073	sc->vendor = ED_VENDOR_NOVELL;
1074	sc->type = ED_TYPE_NE2000;
1075	if (sc->chip_type == ED_CHIP_TYPE_AX88190)
1076		sc->type_str = "AX88190";
1077	else
1078		sc->type_str = "AX88790";
1079	return (0);
1080fail:;
1081	sc->mii_readbits = 0;
1082	sc->mii_writebits = 0;
1083	return (error);
1084}
1085
1086static void
1087ed_pccard_ax88x90_mii_writebits(struct ed_softc *sc, u_int val, int nbits)
1088{
1089	int i, data;
1090
1091	for (i = nbits - 1; i >= 0; i--) {
1092		data = (val >> i) & 1 ? ED_AX88X90_MII_DATAOUT : 0;
1093		ed_asic_outb(sc, ED_AX88X90_MIIBUS, data);
1094		ed_asic_outb(sc, ED_AX88X90_MIIBUS, data | ED_AX88X90_MII_CLK);
1095	}
1096}
1097
1098static u_int
1099ed_pccard_ax88x90_mii_readbits(struct ed_softc *sc, int nbits)
1100{
1101	int i;
1102	u_int val = 0;
1103	uint8_t mdio;
1104
1105	mdio = ED_AX88X90_MII_DIRIN;
1106	for (i = nbits - 1; i >= 0; i--) {
1107		ed_asic_outb(sc, ED_AX88X90_MIIBUS, mdio);
1108		val <<= 1;
1109		if (ed_asic_inb(sc, ED_AX88X90_MIIBUS) & ED_AX88X90_MII_DATAIN)
1110			val++;
1111		ed_asic_outb(sc, ED_AX88X90_MIIBUS, mdio | ED_AX88X90_MII_CLK);
1112	}
1113	return val;
1114}
1115
1116/*
1117 * Special setup for TC5299J
1118 */
1119static int
1120ed_pccard_tc5299j(device_t dev, const struct ed_product *pp)
1121{
1122	int	error, i, id;
1123	char *ts;
1124	struct	ed_softc *sc = device_get_softc(dev);
1125
1126	if (!(pp->flags & NE2000DVF_TC5299J))
1127		return (ENXIO);
1128
1129	if (bootverbose)
1130		device_printf(dev, "Checking Tc5299j\n");
1131
1132	error = ed_probe_Novell_generic(dev, device_get_flags(dev));
1133	if (bootverbose)
1134		device_printf(dev, "probe novel returns %d\n", error);
1135	if (error != 0)
1136		return (error);
1137
1138	/*
1139	 * Check to see if we have a MII PHY ID at any address.  All TC5299J
1140	 * devices have MII and a PHY, so we use this to weed out chips that
1141	 * would otherwise make it through the tests we have after this point.
1142	 */
1143	sc->mii_readbits = ed_pccard_tc5299j_mii_readbits;
1144	sc->mii_writebits = ed_pccard_tc5299j_mii_writebits;
1145	for (i = 0; i < 32; i++) {
1146		id = ed_miibus_readreg(dev, i, MII_PHYIDR1);
1147		if (id != 0 && id != 0xffff)
1148			break;
1149	}
1150	if (i == 32) {
1151		sc->mii_readbits = 0;
1152		sc->mii_writebits = 0;
1153		return (ENXIO);
1154	}
1155	ts = "TC5299J";
1156	if (ed_pccard_rom_mac(dev, sc->enaddr) == 0) {
1157		sc->mii_readbits = 0;
1158		sc->mii_writebits = 0;
1159		return (ENXIO);
1160	}
1161	sc->vendor = ED_VENDOR_NOVELL;
1162	sc->type = ED_TYPE_NE2000;
1163	sc->chip_type = ED_CHIP_TYPE_TC5299J;
1164	sc->type_str = ts;
1165	return (0);
1166}
1167
1168static void
1169ed_pccard_tc5299j_mii_writebits(struct ed_softc *sc, u_int val, int nbits)
1170{
1171	int i;
1172	uint8_t cr, data;
1173
1174	/* Select page 3 */
1175	cr = ed_nic_inb(sc, ED_P0_CR);
1176	ed_nic_outb(sc, ED_P0_CR, cr | ED_CR_PAGE_3);
1177
1178	for (i = nbits - 1; i >= 0; i--) {
1179		data = (val >> i) & 1 ? ED_TC5299J_MII_DATAOUT : 0;
1180		ed_nic_outb(sc, ED_TC5299J_MIIBUS, data);
1181		ed_nic_outb(sc, ED_TC5299J_MIIBUS, data | ED_TC5299J_MII_CLK);
1182	}
1183	ed_nic_outb(sc, ED_TC5299J_MIIBUS, 0);
1184
1185	/* Restore prior page */
1186	ed_nic_outb(sc, ED_P0_CR, cr);
1187}
1188
1189static u_int
1190ed_pccard_tc5299j_mii_readbits(struct ed_softc *sc, int nbits)
1191{
1192	int i;
1193	u_int val = 0;
1194	uint8_t cr;
1195
1196	/* Select page 3 */
1197	cr = ed_nic_inb(sc, ED_P0_CR);
1198	ed_nic_outb(sc, ED_P0_CR, cr | ED_CR_PAGE_3);
1199
1200	ed_asic_outb(sc, ED_TC5299J_MIIBUS, ED_TC5299J_MII_DIROUT);
1201	for (i = nbits - 1; i >= 0; i--) {
1202		ed_nic_outb(sc, ED_TC5299J_MIIBUS,
1203		    ED_TC5299J_MII_CLK | ED_TC5299J_MII_DIROUT);
1204		val <<= 1;
1205		if (ed_nic_inb(sc, ED_TC5299J_MIIBUS) & ED_TC5299J_MII_DATAIN)
1206			val++;
1207		ed_nic_outb(sc, ED_TC5299J_MIIBUS, ED_TC5299J_MII_DIROUT);
1208	}
1209
1210	/* Restore prior page */
1211	ed_nic_outb(sc, ED_P0_CR, cr);
1212	return val;
1213}
1214
1215/*
1216 * MII bus support routines.
1217 */
1218static int
1219ed_miibus_readreg(device_t dev, int phy, int reg)
1220{
1221	struct ed_softc *sc;
1222	int failed, val;
1223
1224	sc = device_get_softc(dev);
1225	/*
1226	 * The AX88790 has an interesting quirk.  It has an internal phy that
1227	 * needs a special bit set to access, but can also have additional
1228	 * external PHYs set for things like HomeNET media.  When accessing
1229	 * the internal PHY, a bit has to be set, when accessing the external
1230	 * PHYs, it must be clear.  See Errata 1, page 51, in the AX88790
1231	 * datasheet for more details.
1232	 *
1233	 * Also, PHYs above 16 appear to be phantoms on some cards, but not
1234	 * others.  Registers read for this are often the same as prior values
1235	 * read.  Filter all register requests to 17-31.
1236	 *
1237	 * I can't explain it, since I don't have the DL100xx data sheets, but
1238	 * the DL100xx chips do 13-bits before the 'ACK' but, but the AX88x90
1239	 * chips have 14.  The linux pcnet and axnet drivers confirm this.
1240	 */
1241	if (sc->chip_type == ED_CHIP_TYPE_AX88790) {
1242		if (phy > 0x10)
1243			return (0);
1244		if (phy == 0x10)
1245			ed_asic_outb(sc, ED_AX88X90_GPIO,
1246			    ED_AX88X90_GPIO_INT_PHY);
1247		else
1248			ed_asic_outb(sc, ED_AX88X90_GPIO, 0);
1249	}
1250
1251	(*sc->mii_writebits)(sc, 0xffffffff, 32);
1252	(*sc->mii_writebits)(sc, ED_MII_STARTDELIM, ED_MII_STARTDELIM_BITS);
1253	(*sc->mii_writebits)(sc, ED_MII_READOP, ED_MII_OP_BITS);
1254	(*sc->mii_writebits)(sc, phy, ED_MII_PHY_BITS);
1255	(*sc->mii_writebits)(sc, reg, ED_MII_REG_BITS);
1256	if (sc->chip_type == ED_CHIP_TYPE_AX88790 ||
1257	    sc->chip_type == ED_CHIP_TYPE_AX88190)
1258		(*sc->mii_readbits)(sc, ED_MII_ACK_BITS);
1259	failed = (*sc->mii_readbits)(sc, ED_MII_ACK_BITS);
1260	val = (*sc->mii_readbits)(sc, ED_MII_DATA_BITS);
1261	(*sc->mii_writebits)(sc, ED_MII_IDLE, ED_MII_IDLE_BITS);
1262/*	printf("Reading phy %d reg %#x returning %#x (valid %d)\n", phy, reg, val, !failed); */
1263	return (failed ? 0 : val);
1264}
1265
1266static int
1267ed_miibus_writereg(device_t dev, int phy, int reg, int data)
1268{
1269	struct ed_softc *sc;
1270
1271/*	printf("Writing phy %d reg %#x data %#x\n", phy, reg, data); */
1272	sc = device_get_softc(dev);
1273	/* See ed_miibus_readreg for details */
1274	if (sc->chip_type == ED_CHIP_TYPE_AX88790) {
1275		if (phy > 0x10)
1276			return (0);
1277		if (phy == 0x10)
1278			ed_asic_outb(sc, ED_AX88X90_GPIO,
1279			    ED_AX88X90_GPIO_INT_PHY);
1280		else
1281			ed_asic_outb(sc, ED_AX88X90_GPIO, 0);
1282	}
1283	(*sc->mii_writebits)(sc, 0xffffffff, 32);
1284	(*sc->mii_writebits)(sc, ED_MII_STARTDELIM, ED_MII_STARTDELIM_BITS);
1285	(*sc->mii_writebits)(sc, ED_MII_WRITEOP, ED_MII_OP_BITS);
1286	(*sc->mii_writebits)(sc, phy, ED_MII_PHY_BITS);
1287	(*sc->mii_writebits)(sc, reg, ED_MII_REG_BITS);
1288	(*sc->mii_writebits)(sc, ED_MII_TURNAROUND, ED_MII_TURNAROUND_BITS);
1289	(*sc->mii_writebits)(sc, data, ED_MII_DATA_BITS);
1290	(*sc->mii_writebits)(sc, ED_MII_IDLE, ED_MII_IDLE_BITS);
1291	return (0);
1292}
1293
1294static int
1295ed_ifmedia_upd(struct ifnet *ifp)
1296{
1297	struct ed_softc *sc;
1298	int error;
1299
1300	sc = ifp->if_softc;
1301	if (sc->miibus == NULL)
1302		return (ENXIO);
1303	ED_LOCK(sc);
1304	error = ed_pccard_kick_phy(sc);
1305	ED_UNLOCK(sc);
1306	return (error);
1307}
1308
1309static void
1310ed_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1311{
1312	struct ed_softc *sc;
1313	struct mii_data *mii;
1314
1315	sc = ifp->if_softc;
1316	if (sc->miibus == NULL)
1317		return;
1318
1319	mii = device_get_softc(sc->miibus);
1320	mii_pollstat(mii);
1321	ifmr->ifm_active = mii->mii_media_active;
1322	ifmr->ifm_status = mii->mii_media_status;
1323}
1324
1325static void
1326ed_child_detached(device_t dev, device_t child)
1327{
1328	struct ed_softc *sc;
1329
1330	sc = device_get_softc(dev);
1331	if (child == sc->miibus)
1332		sc->miibus = NULL;
1333}
1334
1335static void
1336ed_pccard_tick(void *arg)
1337{
1338	struct ed_softc *sc = arg;
1339	struct mii_data *mii;
1340	int media = 0;
1341
1342	ED_ASSERT_LOCKED(sc);
1343	if (sc->miibus != NULL) {
1344		mii = device_get_softc(sc->miibus);
1345		media = mii->mii_media_status;
1346		mii_tick(mii);
1347		if (mii->mii_media_status & IFM_ACTIVE &&
1348		    media != mii->mii_media_status) {
1349			if (sc->chip_type == ED_CHIP_TYPE_DL10022) {
1350				ed_asic_outb(sc, ED_DL10022_DIAG,
1351				    (mii->mii_media_active & IFM_FDX) ?
1352				    ED_DL10022_COLLISON_DIS : 0);
1353#ifdef notyet
1354			} else if (sc->chip_type == ED_CHIP_TYPE_DL10019) {
1355				write_asic(sc, ED_DL10019_MAGIC,
1356				    (mii->mii_media_active & IFM_FDX) ?
1357				    DL19FDUPLX : 0);
1358#endif
1359			}
1360		}
1361
1362	}
1363	callout_reset(&sc->tick_ch, hz, ed_pccard_tick, sc);
1364}
1365
1366static device_method_t ed_pccard_methods[] = {
1367	/* Device interface */
1368	DEVMETHOD(device_probe,		ed_pccard_probe),
1369	DEVMETHOD(device_attach,	ed_pccard_attach),
1370	DEVMETHOD(device_detach,	ed_detach),
1371
1372	/* Bus interface */
1373	DEVMETHOD(bus_child_detached,	ed_child_detached),
1374
1375	/* MII interface */
1376	DEVMETHOD(miibus_readreg,	ed_miibus_readreg),
1377	DEVMETHOD(miibus_writereg,	ed_miibus_writereg),
1378
1379	{ 0, 0 }
1380};
1381
1382static driver_t ed_pccard_driver = {
1383	"ed",
1384	ed_pccard_methods,
1385	sizeof(struct ed_softc)
1386};
1387
1388DRIVER_MODULE(ed, pccard, ed_pccard_driver, ed_devclass, 0, 0);
1389DRIVER_MODULE(miibus, ed, miibus_driver, miibus_devclass, 0, 0);
1390MODULE_DEPEND(ed, miibus, 1, 1, 1);
1391MODULE_DEPEND(ed, ether, 1, 1, 1);
1392