if_ed_pccard.c revision 190801
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 190801 2009-04-07 15:34:26Z 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/*
110 * NE-2000 based PC Cards have a number of ways to get the MAC address.
111 * Some cards encode this as a FUNCE.  Others have this in the ROMs the
112 * same way that ISA cards do.  Some have it encoded in the attribute
113 * memory somewhere that isn't in the CIS.  Some new chipsets have it
114 * in special registers in the ASIC part of the chip.
115 *
116 * For those cards that have the MAC adress stored in attribute memory
117 * outside of a FUNCE entry in the CIS, nearly all of them have it at
118 * a fixed offset (0xff0).  We use that offset as a source of last
119 * resource if other offsets have failed.  This is the address of the
120 * National Semiconductor DP83903A, which is the only chip's datasheet
121 * I've found.
122 */
123#define ED_DEFAULT_MAC_OFFSET	0xff0
124
125static const struct ed_product {
126	struct pccard_product	prod;
127	int flags;
128#define	NE2000DVF_DL100XX	0x0001		/* chip is D-Link DL10019/22 */
129#define	NE2000DVF_AX88X90	0x0002		/* chip is ASIX AX88[17]90 */
130#define NE2000DVF_TC5299J	0x0004		/* chip is Tamarack TC5299J */
131#define NE2000DVF_TOSHIBA	0x0008		/* Toshiba DP83902A */
132#define NE2000DVF_ENADDR	0x0100		/* Get MAC from attr mem */
133#define NE2000DVF_ANYFUNC	0x0200		/* Allow any function type */
134#define NE2000DVF_MODEM		0x0400		/* Has a modem/serial */
135	int enoff;
136} ed_pccard_products[] = {
137	{ PCMCIA_CARD(ACCTON, EN2212), 0},
138	{ PCMCIA_CARD(ACCTON, EN2216), 0},
139	{ PCMCIA_CARD(ALLIEDTELESIS, LA_PCM), 0},
140	{ PCMCIA_CARD(AMBICOM, AMB8002T), 0},
141	{ PCMCIA_CARD(BILLIONTON, CFLT10N), 0},
142	{ PCMCIA_CARD(BILLIONTON, LNA100B), NE2000DVF_AX88X90},
143	{ PCMCIA_CARD(BILLIONTON, LNT10TN), 0},
144	{ PCMCIA_CARD(BROMAX, AXNET), NE2000DVF_AX88X90},
145	{ PCMCIA_CARD(BROMAX, IPORT), 0},
146	{ PCMCIA_CARD(BROMAX, IPORT2), 0},
147	{ PCMCIA_CARD(BUFFALO, LPC2_CLT), 0},
148	{ PCMCIA_CARD(BUFFALO, LPC3_CLT), 0},
149	{ PCMCIA_CARD(BUFFALO, LPC3_CLX), NE2000DVF_AX88X90},
150	{ PCMCIA_CARD(BUFFALO, LPC4_TX), NE2000DVF_AX88X90},
151	{ PCMCIA_CARD(BUFFALO, LPC4_CLX), NE2000DVF_AX88X90},
152	{ PCMCIA_CARD(BUFFALO, LPC_CF_CLT), 0},
153	{ PCMCIA_CARD(CNET, NE2000), 0},
154	{ PCMCIA_CARD(COMPEX, AX88190), NE2000DVF_AX88X90},
155	{ PCMCIA_CARD(COMPEX, LANMODEM), 0},
156	{ PCMCIA_CARD(COMPEX, LINKPORT_ENET_B), 0},
157	{ PCMCIA_CARD(COREGA, ETHER_II_PCC_T), 0},
158	{ PCMCIA_CARD(COREGA, ETHER_II_PCC_TD), 0},
159	{ PCMCIA_CARD(COREGA, ETHER_PCC_T), 0},
160	{ PCMCIA_CARD(COREGA, ETHER_PCC_TD), 0},
161	{ PCMCIA_CARD(COREGA, FAST_ETHER_PCC_TX), NE2000DVF_DL100XX},
162	{ PCMCIA_CARD(COREGA, FETHER_PCC_TXD), NE2000DVF_AX88X90},
163	{ PCMCIA_CARD(COREGA, FETHER_PCC_TXF), NE2000DVF_DL100XX},
164	{ PCMCIA_CARD(COREGA, FETHER_II_PCC_TXD), NE2000DVF_AX88X90},
165	{ PCMCIA_CARD(COREGA, LAPCCTXD), 0},
166	{ PCMCIA_CARD(DAYNA, COMMUNICARD_E_1), 0},
167	{ PCMCIA_CARD(DAYNA, COMMUNICARD_E_2), 0},
168	{ PCMCIA_CARD(DLINK, DE650), 0 },
169	{ PCMCIA_CARD(DLINK, DE660), 0 },
170	{ PCMCIA_CARD(DLINK, DE660PLUS), 0},
171	{ PCMCIA_CARD(DYNALINK, L10C), 0},
172	{ PCMCIA_CARD(EDIMAX, EP4000A), 0},
173	{ PCMCIA_CARD(EPSON, EEN10B), NE2000DVF_ENADDR, 0xff0},
174	{ PCMCIA_CARD(EXP, THINLANCOMBO), 0},
175	{ PCMCIA_CARD(GLOBALVILLAGE, LANMODEM), 0},
176	{ PCMCIA_CARD(GREY_CELL, TDK3000), 0},
177	{ PCMCIA_CARD(GREY_CELL, DMF650TX),
178	    NE2000DVF_ANYFUNC | NE2000DVF_DL100XX | NE2000DVF_MODEM},
179	{ PCMCIA_CARD(IBM, HOME_AND_AWAY), 0},
180	{ PCMCIA_CARD(IBM, INFOMOVER), NE2000DVF_ENADDR, 0xff0},
181	{ PCMCIA_CARD(IODATA3, PCLAT), 0},
182	{ PCMCIA_CARD(KINGSTON, CIO10T), 0},
183	{ PCMCIA_CARD(KINGSTON, KNE2), 0},
184	{ PCMCIA_CARD(LANTECH, FASTNETTX), NE2000DVF_AX88X90},
185	{ PCMCIA_CARD(LINKSYS, COMBO_ECARD),
186	    NE2000DVF_DL100XX | NE2000DVF_AX88X90},
187	{ PCMCIA_CARD(LINKSYS, ECARD_1), 0},
188	{ PCMCIA_CARD(LINKSYS, ECARD_2), 0},
189	{ PCMCIA_CARD(LINKSYS, ETHERFAST), NE2000DVF_DL100XX},
190	{ PCMCIA_CARD(LINKSYS, TRUST_COMBO_ECARD), 0},
191	{ PCMCIA_CARD(MACNICA, ME1_JEIDA), 0},
192	{ PCMCIA_CARD(MAGICRAM, ETHER), 0},
193	{ PCMCIA_CARD(MELCO, LPC3_CLX), NE2000DVF_AX88X90},
194	{ PCMCIA_CARD(MELCO, LPC3_TX), NE2000DVF_AX88X90},
195	{ PCMCIA_CARD(MITSUBISHI, B8895), NE2000DVF_ANYFUNC}, /* NG */
196	{ PCMCIA_CARD(MICRORESEARCH, MR10TPC), 0},
197	{ PCMCIA_CARD(NDC, ND5100_E), 0},
198	{ PCMCIA_CARD(NETGEAR, FA410TXC), NE2000DVF_DL100XX},
199	/* Same ID as DLINK DFE-670TXD.  670 has DL10022, fa411 has ax88790 */
200	{ PCMCIA_CARD(NETGEAR, FA411), NE2000DVF_AX88X90 | NE2000DVF_DL100XX},
201	{ PCMCIA_CARD(NEXTCOM, NEXTHAWK), 0},
202	{ PCMCIA_CARD(NEWMEDIA, LANSURFER), 0},
203	{ PCMCIA_CARD(OEM2, ETHERNET), 0},
204	{ PCMCIA_CARD(OEM2, FAST_ETHERNET), NE2000DVF_AX88X90 },
205	{ PCMCIA_CARD(OEM2, NE2000), 0},
206	{ PCMCIA_CARD(PLANET, SMARTCOM2000), 0 },
207	{ PCMCIA_CARD(PREMAX, PE200), 0},
208	{ PCMCIA_CARD(PSION, LANGLOBAL),
209	    NE2000DVF_ANYFUNC | NE2000DVF_AX88X90 | NE2000DVF_MODEM},
210	{ PCMCIA_CARD(RACORE, ETHERNET), 0},
211	{ PCMCIA_CARD(RACORE, FASTENET), NE2000DVF_AX88X90},
212	{ PCMCIA_CARD(RACORE, 8041TX), NE2000DVF_AX88X90 | NE2000DVF_TC5299J},
213	{ PCMCIA_CARD(RELIA, COMBO), 0},
214	{ PCMCIA_CARD(RIOS, PCCARD3), 0},
215	{ PCMCIA_CARD(RPTI, EP400), 0},
216	{ PCMCIA_CARD(RPTI, EP401), 0},
217	{ PCMCIA_CARD(SMC, EZCARD), 0},
218	{ PCMCIA_CARD(SOCKET, EA_ETHER), 0},
219	{ PCMCIA_CARD(SOCKET, ES_1000), 0},
220	{ PCMCIA_CARD(SOCKET, LP_ETHER), 0},
221	{ PCMCIA_CARD(SOCKET, LP_ETHER_CF), 0},
222	{ PCMCIA_CARD(SOCKET, LP_ETH_10_100_CF), NE2000DVF_DL100XX},
223	{ PCMCIA_CARD(SVEC, COMBOCARD), 0},
224	{ PCMCIA_CARD(SVEC, LANCARD), 0},
225	{ PCMCIA_CARD(TAMARACK, ETHERNET), 0},
226	{ PCMCIA_CARD(TDK, CFE_10), 0},
227	{ PCMCIA_CARD(TDK, LAK_CD031), 0},
228	{ PCMCIA_CARD(TDK, DFL5610WS), 0},
229	{ PCMCIA_CARD(TELECOMDEVICE, LM5LT), 0 },
230	{ PCMCIA_CARD(TELECOMDEVICE, TCD_HPC100), NE2000DVF_AX88X90},
231	{ PCMCIA_CARD(TJ, PTJ_LAN_T), 0 },
232	{ PCMCIA_CARD(TOSHIBA2, LANCT00A), NE2000DVF_ANYFUNC | NE2000DVF_TOSHIBA},
233	{ PCMCIA_CARD(ZONET, ZEN), 0},
234	{ { NULL } }
235};
236
237/*
238 *      PC Card (PCMCIA) specific code.
239 */
240static int	ed_pccard_probe(device_t);
241static int	ed_pccard_attach(device_t);
242
243static int	ed_pccard_dl100xx(device_t dev, const struct ed_product *);
244static void	ed_pccard_dl100xx_mii_reset(struct ed_softc *sc);
245static u_int	ed_pccard_dl100xx_mii_readbits(struct ed_softc *sc, int nbits);
246static void	ed_pccard_dl100xx_mii_writebits(struct ed_softc *sc, u_int val,
247    int nbits);
248
249static int	ed_pccard_ax88x90(device_t dev, const struct ed_product *);
250static u_int	ed_pccard_ax88x90_mii_readbits(struct ed_softc *sc, int nbits);
251static void	ed_pccard_ax88x90_mii_writebits(struct ed_softc *sc, u_int val,
252    int nbits);
253
254static int	ed_miibus_readreg(device_t dev, int phy, int reg);
255static int	ed_ifmedia_upd(struct ifnet *);
256static void	ed_ifmedia_sts(struct ifnet *, struct ifmediareq *);
257
258static int	ed_pccard_tc5299j(device_t dev, const struct ed_product *);
259static void	ed_pccard_tc5299j_mii_reset(struct ed_softc *sc);
260static u_int	ed_pccard_tc5299j_mii_readbits(struct ed_softc *sc, int nbits);
261static void	ed_pccard_tc5299j_mii_writebits(struct ed_softc *sc, u_int val,
262    int nbits);
263
264static void
265ed_pccard_print_entry(const struct ed_product *pp)
266{
267	int i;
268
269	printf("Product entry: ");
270	if (pp->prod.pp_name)
271		printf("name='%s',", pp->prod.pp_name);
272	printf("vendor=%#x,product=%#x", pp->prod.pp_vendor,
273	    pp->prod.pp_product);
274	for (i = 0; i < 4; i++)
275		if (pp->prod.pp_cis[i])
276			printf(",CIS%d='%s'", i, pp->prod.pp_cis[i]);
277	printf("\n");
278}
279
280static int
281ed_pccard_probe(device_t dev)
282{
283	const struct ed_product *pp, *pp2;
284	int		error, first = 1;
285	uint32_t	fcn = PCCARD_FUNCTION_UNSPEC;
286
287	/* Make sure we're a network function */
288	error = pccard_get_function(dev, &fcn);
289	if (error != 0)
290		return (error);
291
292	if ((pp = (const struct ed_product *) pccard_product_lookup(dev,
293	    (const struct pccard_product *) ed_pccard_products,
294	    sizeof(ed_pccard_products[0]), NULL)) != NULL) {
295		if (pp->prod.pp_name != NULL)
296			device_set_desc(dev, pp->prod.pp_name);
297		/*
298		 * Some devices don't ID themselves as network, but
299		 * that's OK if the flags say so.
300		 */
301		if (!(pp->flags & NE2000DVF_ANYFUNC) &&
302		    fcn != PCCARD_FUNCTION_NETWORK)
303			return (ENXIO);
304		/*
305		 * Some devices match multiple entries.  Report that
306		 * as a warning to help cull the table
307		 */
308		pp2 = pp;
309		while ((pp2 = (const struct ed_product *)pccard_product_lookup(
310		    dev, (const struct pccard_product *)(pp2 + 1),
311		    sizeof(ed_pccard_products[0]), NULL)) != NULL) {
312			if (first) {
313				device_printf(dev,
314    "Warning: card matches multiple entries.  Report to imp@freebsd.org\n");
315				ed_pccard_print_entry(pp);
316				first = 0;
317			}
318			ed_pccard_print_entry(pp2);
319		}
320
321		return (0);
322	}
323	return (ENXIO);
324}
325
326static int
327ed_pccard_rom_mac(device_t dev, uint8_t *enaddr)
328{
329	struct ed_softc *sc = device_get_softc(dev);
330	uint8_t romdata[32];
331	int i;
332
333	/*
334	 * Read in the rom data at location 0.  Since there are no
335	 * NE-1000 based PC Card devices, we'll assume we're 16-bit.
336	 *
337	 * In researching what format this takes, I've found that the
338	 * following appears to be true for multiple cards based on
339	 * observation as well as datasheet digging.
340	 *
341	 * Data is stored in some ROM and is copied out 8 bits at a time
342	 * into 16-bit wide locations.  This means that the odd locations
343	 * of the ROM are not used (and can be either 0 or ff).
344	 *
345	 * The contents appears to be as follows:
346	 * PROM   RAM
347	 * Offset Offset	What
348	 *  0      0	ENETADDR 0
349	 *  1      2	ENETADDR 1
350	 *  2      4	ENETADDR 2
351	 *  3      6	ENETADDR 3
352	 *  4      8	ENETADDR 4
353	 *  5     10	ENETADDR 5
354	 *  6-13  12-26 Reserved (varies by manufacturer)
355	 * 14     28	0x57
356	 * 15     30    0x57
357	 *
358	 * Some manufacturers have another image of enetaddr from
359	 * PROM offset 0x10 to 0x15 with 0x42 in 0x1e and 0x1f, but
360	 * this doesn't appear to be universally documented in the
361	 * datasheets.  Some manufactuers have a card type, card config
362	 * checksums, etc encoded into PROM offset 6-13, but deciphering it
363	 * requires more knowledge about the exact underlying chipset than
364	 * we possess (and maybe can possess).
365	 */
366	ed_pio_readmem(sc, 0, romdata, 32);
367	if (bootverbose)
368		device_printf(dev, "ROM DATA: %32D\n", romdata, " ");
369	if (romdata[28] != 0x57 || romdata[30] != 0x57)
370		return (0);
371	for (i = 0; i < ETHER_ADDR_LEN; i++)
372		enaddr[i] = romdata[i * 2];
373	return (1);
374}
375
376static int
377ed_pccard_add_modem(device_t dev)
378{
379	struct ed_softc *sc = device_get_softc(dev);
380
381	device_printf(dev, "Need to write this code: modem rid is %d\n",
382	    sc->modem_rid);
383	return 0;
384}
385
386static int
387ed_pccard_kick_phy(struct ed_softc *sc)
388{
389	struct mii_softc *miisc;
390	struct mii_data *mii;
391
392	/*
393	 * Many of the PHYs that wind up on PC Cards are weird in
394	 * this way.  Generally, we don't need to worry so much about
395	 * the Isolation protocol since there's only one PHY in
396	 * these designs, so this workaround is reasonable.
397	 */
398	mii = device_get_softc(sc->miibus);
399	LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
400		miisc->mii_flags |= MIIF_FORCEANEG;
401		mii_phy_reset(miisc);
402	}
403	return (mii_mediachg(mii));
404}
405
406static int
407ed_pccard_media_ioctl(struct ed_softc *sc, struct ifreq *ifr, u_long command)
408{
409	struct mii_data *mii;
410
411	if (sc->miibus == NULL)
412		return (EINVAL);
413	mii = device_get_softc(sc->miibus);
414	return (ifmedia_ioctl(sc->ifp, ifr, &mii->mii_media, command));
415}
416
417
418static void
419ed_pccard_mediachg(struct ed_softc *sc)
420{
421	struct mii_data *mii;
422
423	if (sc->miibus == NULL)
424		return;
425	mii = device_get_softc(sc->miibus);
426	mii_mediachg(mii);
427}
428
429static void
430ed_pccard_tick(void *arg)
431{
432	struct ed_softc *sc = arg;
433	struct mii_data *mii;
434	int media = 0;
435
436	ED_ASSERT_LOCKED(sc);
437	if (sc->miibus != NULL) {
438		mii = device_get_softc(sc->miibus);
439		media = mii->mii_media_status;
440		mii_tick(mii);
441		if (mii->mii_media_status & IFM_ACTIVE &&
442		    media != mii->mii_media_status && 0 &&
443		    sc->chip_type == ED_CHIP_TYPE_DL10022) {
444			ed_asic_outb(sc, ED_DL10022_DIAG,
445			    (mii->mii_media_active & IFM_FDX) ?
446			    ED_DL10022_COLLISON_DIS : 0);
447		}
448
449	}
450	callout_reset(&sc->tick_ch, hz, ed_pccard_tick, sc);
451}
452
453static int
454ed_pccard_attach(device_t dev)
455{
456	u_char sum;
457	u_char enaddr[ETHER_ADDR_LEN];
458	const struct ed_product *pp;
459	int	error, i, flags;
460	struct ed_softc *sc = device_get_softc(dev);
461	u_long size;
462	static uint16_t *intr_vals[] = {NULL, NULL};
463
464	sc->dev = dev;
465	if ((pp = (const struct ed_product *) pccard_product_lookup(dev,
466	    (const struct pccard_product *) ed_pccard_products,
467	    sizeof(ed_pccard_products[0]), NULL)) == NULL)
468		return (ENXIO);
469	sc->modem_rid = -1;
470	if (pp->flags & NE2000DVF_MODEM) {
471		sc->port_rid = -1;
472		for (i = 0; i < 4; i++) {
473			size = bus_get_resource_count(dev, SYS_RES_IOPORT, i);
474			if (size == ED_NOVELL_IO_PORTS)
475				sc->port_rid = i;
476			else if (size == 8)
477				sc->modem_rid = i;
478		}
479		if (sc->port_rid == -1) {
480			device_printf(dev, "Cannot locate my ports!\n");
481			return (ENXIO);
482		}
483	} else {
484		sc->port_rid = 0;
485	}
486	/* Allocate the port resource during setup. */
487	error = ed_alloc_port(dev, sc->port_rid, ED_NOVELL_IO_PORTS);
488	if (error)
489		return (error);
490	error = ed_alloc_irq(dev, 0, 0);
491	if (error)
492		goto bad;
493
494	/*
495	 * Determine which chipset we are.  All the PC Card chipsets have the
496	 * ASIC and NIC offsets in the same place.
497	 */
498	sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
499	sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
500	error = ENXIO;
501	flags = device_get_flags(dev);
502	if (error != 0)
503		error = ed_pccard_dl100xx(dev, pp);
504	if (error != 0)
505		error = ed_pccard_ax88x90(dev, pp);
506	if (error != 0)
507		error = ed_pccard_tc5299j(dev, pp);
508	if (error != 0)
509		error = ed_probe_Novell_generic(dev, flags);
510	if (error != 0 && (pp->flags & NE2000DVF_TOSHIBA)) {
511		flags |= ED_FLAGS_TOSH_ETHER;
512		flags |= ED_FLAGS_PCCARD;
513		sc->asic_offset = ED_WD_ASIC_OFFSET;
514		sc->nic_offset  = ED_WD_NIC_OFFSET;
515		error = ed_probe_WD80x3_generic(dev, flags, intr_vals);
516	}
517	if (error)
518		goto bad;
519
520	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
521	    NULL, edintr, sc, &sc->irq_handle);
522	if (error) {
523		device_printf(dev, "setup intr failed %d \n", error);
524		goto bad;
525	}
526
527	/*
528	 * For the older cards, we have to get the MAC address from
529	 * the card in some way.  Let's try the standard PCMCIA way
530	 * first.  If that fails, then check to see if we have valid
531	 * data from the standard NE-2000 data roms.  If that fails,
532	 * check to see if the card has a hint about where to look in
533	 * its CIS.  If that fails, maybe we should look at some
534	 * default value.  In all fails, we should fail the attach,
535	 * but don't right now.
536	 */
537	for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
538		sum |= sc->enaddr[i];
539	if (sc->chip_type == ED_CHIP_TYPE_DP8390 && sum == 0) {
540		pccard_get_ether(dev, enaddr);
541		if (bootverbose)
542			device_printf(dev, "CIS MAC %6D\n", enaddr, ":");
543		for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
544			sum |= enaddr[i];
545		if (sum == 0 && ed_pccard_rom_mac(dev, enaddr)) {
546			if (bootverbose)
547				device_printf(dev, "ROM mac %6D\n", enaddr,
548				    ":");
549			sum++;
550		}
551		if (sum == 0 && pp->flags & NE2000DVF_ENADDR) {
552			for (i = 0; i < ETHER_ADDR_LEN; i++) {
553				pccard_attr_read_1(dev, pp->enoff + i * 2,
554				    enaddr + i);
555				sum |= enaddr[i];
556			}
557			if (bootverbose)
558				device_printf(dev, "Hint %x MAC %6D\n",
559				    pp->enoff, enaddr, ":");
560		}
561		if (sum == 0) {
562			for (i = 0; i < ETHER_ADDR_LEN; i++) {
563				pccard_attr_read_1(dev, ED_DEFAULT_MAC_OFFSET +
564				    i * 2, enaddr + i);
565				sum |= enaddr[i];
566			}
567			if (bootverbose)
568				device_printf(dev, "Fallback MAC %6D\n",
569				    enaddr, ":");
570		}
571		if (sum == 0) {
572			device_printf(dev, "Cannot extract MAC address.\n");
573			ed_release_resources(dev);
574			return (ENXIO);
575		}
576		bcopy(enaddr, sc->enaddr, ETHER_ADDR_LEN);
577	}
578
579	error = ed_attach(dev);
580	if (error)
581		goto bad;
582 	if (sc->chip_type == ED_CHIP_TYPE_DL10019 ||
583	    sc->chip_type == ED_CHIP_TYPE_DL10022) {
584		/* Probe for an MII bus, but ignore errors. */
585		ed_pccard_dl100xx_mii_reset(sc);
586		(void)mii_phy_probe(dev, &sc->miibus, ed_ifmedia_upd,
587		    ed_ifmedia_sts);
588	} else if (sc->chip_type == ED_CHIP_TYPE_AX88190 ||
589	    sc->chip_type == ED_CHIP_TYPE_AX88790) {
590		if ((error = mii_phy_probe(dev, &sc->miibus, ed_ifmedia_upd,
591		     ed_ifmedia_sts)) != 0) {
592			device_printf(dev, "Missing mii %d!\n", error);
593			goto bad;
594		}
595
596	} else if (sc->chip_type == ED_CHIP_TYPE_TC5299J) {
597		ed_pccard_tc5299j_mii_reset(sc);
598		if ((error = mii_phy_probe(dev, &sc->miibus, ed_ifmedia_upd,
599		     ed_ifmedia_sts)) != 0) {
600			device_printf(dev, "Missing mii!\n");
601			goto bad;
602		}
603
604	}
605	if (sc->miibus != NULL) {
606		sc->sc_tick = ed_pccard_tick;
607		sc->sc_mediachg = ed_pccard_mediachg;
608		sc->sc_media_ioctl = ed_pccard_media_ioctl;
609		ed_pccard_kick_phy(sc);
610	}
611	if (sc->modem_rid != -1)
612		ed_pccard_add_modem(dev);
613	return (0);
614bad:
615	ed_detach(dev);
616	return (error);
617}
618
619/*
620 * Probe the Ethernet MAC addrees for PCMCIA Linksys EtherFast 10/100
621 * and compatible cards (DL10019C Ethernet controller).
622 *
623 * Note: The PAO patches try to use more memory for the card, but that
624 * seems to fail for my card.  A future optimization would add this back
625 * conditionally.
626 */
627static int
628ed_pccard_dl100xx(device_t dev, const struct ed_product *pp)
629{
630	struct ed_softc *sc = device_get_softc(dev);
631	u_char sum;
632	uint8_t id;
633	int i, error;
634
635	if (!(pp->flags & NE2000DVF_DL100XX))
636		return (ENXIO);
637	if (bootverbose)
638		device_printf(dev, "Trying DL100xx probing\n");
639	error = ed_probe_Novell_generic(dev, device_get_flags(dev));
640	if (bootverbose && error)
641		device_printf(dev, "Novell generic probe failed: %d\n", error);
642	if (error != 0)
643		return (error);
644
645	/*
646	 * Linksys registers(offset from ASIC base)
647	 *
648	 * 0x04-0x09 : Physical Address Register 0-5 (PAR0-PAR5)
649	 * 0x0A      : Card ID Register (CIR)
650	 * 0x0B      : Check Sum Register (SR)
651	 */
652	for (sum = 0, i = 0x04; i < 0x0c; i++)
653		sum += ed_asic_inb(sc, i);
654	if (sum != 0xff) {
655		if (bootverbose)
656			device_printf(dev, "Bad checksum %#x\n", sum);
657		return (ENXIO);		/* invalid DL10019C */
658	}
659	if (bootverbose)
660		device_printf(dev, "CIR is %d\n", ed_asic_inb(sc, 0xa));
661	for (i = 0; i < ETHER_ADDR_LEN; i++)
662		sc->enaddr[i] = ed_asic_inb(sc, 0x04 + i);
663	ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
664	id = ed_asic_inb(sc, 0xf);
665	sc->isa16bit = 1;
666	sc->vendor = ED_VENDOR_NOVELL;
667	sc->type = ED_TYPE_NE2000;
668	sc->chip_type = (id & 0x90) == 0x90 ?
669	    ED_CHIP_TYPE_DL10022 : ED_CHIP_TYPE_DL10019;
670	sc->type_str = ((id & 0x90) == 0x90) ? "DL10022" : "DL10019";
671	sc->mii_readbits = ed_pccard_dl100xx_mii_readbits;
672	sc->mii_writebits = ed_pccard_dl100xx_mii_writebits;
673	return (0);
674}
675
676/* MII bit-twiddling routines for cards using Dlink chipset */
677#define DL100XX_MIISET(sc, x) ed_asic_outb(sc, ED_DL100XX_MIIBUS, \
678    ed_asic_inb(sc, ED_DL100XX_MIIBUS) | (x))
679#define DL100XX_MIICLR(sc, x) ed_asic_outb(sc, ED_DL100XX_MIIBUS, \
680    ed_asic_inb(sc, ED_DL100XX_MIIBUS) & ~(x))
681
682static void
683ed_pccard_dl100xx_mii_reset(struct ed_softc *sc)
684{
685	if (sc->chip_type != ED_CHIP_TYPE_DL10022)
686		return;
687
688	ed_asic_outb(sc, ED_DL100XX_MIIBUS, ED_DL10022_MII_RESET2);
689	DELAY(10);
690	ed_asic_outb(sc, ED_DL100XX_MIIBUS,
691	    ED_DL10022_MII_RESET2 | ED_DL10022_MII_RESET1);
692	DELAY(10);
693	ed_asic_outb(sc, ED_DL100XX_MIIBUS, ED_DL10022_MII_RESET2);
694	DELAY(10);
695	ed_asic_outb(sc, ED_DL100XX_MIIBUS,
696	    ED_DL10022_MII_RESET2 | ED_DL10022_MII_RESET1);
697	DELAY(10);
698	ed_asic_outb(sc, ED_DL100XX_MIIBUS, 0);
699}
700
701static void
702ed_pccard_dl100xx_mii_writebits(struct ed_softc *sc, u_int val, int nbits)
703{
704	int i;
705
706	DL100XX_MIISET(sc, ED_DL100XX_MII_DIROUT);
707	for (i = nbits - 1; i >= 0; i--) {
708		if ((val >> i) & 1)
709			DL100XX_MIISET(sc, ED_DL100XX_MII_DATAOUT);
710		else
711			DL100XX_MIICLR(sc, ED_DL100XX_MII_DATAOUT);
712		DL100XX_MIISET(sc, ED_DL100XX_MII_CLK);
713		DL100XX_MIICLR(sc, ED_DL100XX_MII_CLK);
714	}
715}
716
717static u_int
718ed_pccard_dl100xx_mii_readbits(struct ed_softc *sc, int nbits)
719{
720	int i;
721	u_int val = 0;
722
723	DL100XX_MIICLR(sc, ED_DL100XX_MII_DIROUT);
724	for (i = nbits - 1; i >= 0; i--) {
725		DL100XX_MIISET(sc, ED_DL100XX_MII_CLK);
726		val <<= 1;
727		if (ed_asic_inb(sc, ED_DL100XX_MIIBUS) & ED_DL100XX_MII_DATAIN)
728			val++;
729		DL100XX_MIICLR(sc, ED_DL100XX_MII_CLK);
730	}
731	return val;
732}
733
734static void
735ed_pccard_ax88x90_reset(struct ed_softc *sc)
736{
737	int i;
738
739	/* Reset Card */
740	ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP | ED_CR_PAGE_0);
741	ed_asic_outb(sc, ED_NOVELL_RESET, ed_asic_inb(sc, ED_NOVELL_RESET));
742
743	/* Wait for the RST bit to assert, but cap it at 10ms */
744	for (i = 10000; !(ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST) && i > 0;
745	     i--)
746		continue;
747	ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RST);	/* ACK INTR */
748	if (i == 0)
749		device_printf(sc->dev, "Reset didn't finish\n");
750}
751
752/*
753 * Probe and vendor-specific initialization routine for ax88x90 boards
754 */
755static int
756ed_probe_ax88x90_generic(device_t dev, int flags)
757{
758	struct ed_softc *sc = device_get_softc(dev);
759	u_int   memsize;
760	static char test_pattern[32] = "THIS is A memory TEST pattern";
761	char    test_buffer[32];
762
763	ed_pccard_ax88x90_reset(sc);
764	DELAY(10*1000);
765
766	/* Make sure that we really have an 8390 based board */
767	if (!ed_probe_generic8390(sc))
768		return (ENXIO);
769
770	sc->vendor = ED_VENDOR_NOVELL;
771	sc->mem_shared = 0;
772	sc->cr_proto = ED_CR_RD2;
773
774	/*
775	 * This prevents packets from being stored in the NIC memory when the
776	 * readmem routine turns on the start bit in the CR.  We write some
777	 * bytes in word mode and verify we can read them back.  If we can't
778	 * then we don't have an AX88x90 chip here.
779	 */
780	sc->isa16bit = 1;
781	ed_nic_outb(sc, ED_P0_RCR, ED_RCR_MON);
782	ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
783	ed_pio_writemem(sc, test_pattern, 16384, sizeof(test_pattern));
784	ed_pio_readmem(sc, 16384, test_buffer, sizeof(test_pattern));
785	if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)) != 0)
786		return (ENXIO);
787
788	/*
789	 * Hard code values based on the datasheet.  We're NE-2000 compatible
790	 * NIC with 16kb of packet memory starting at 16k offset.
791	 */
792	sc->type = ED_TYPE_NE2000;
793	memsize = sc->mem_size = 16*1024;
794	sc->mem_start = 16 * 1024;
795	if (ed_asic_inb(sc, ED_AX88X90_TEST) != 0)
796		sc->chip_type = ED_CHIP_TYPE_AX88790;
797	else {
798		sc->chip_type = ED_CHIP_TYPE_AX88190;
799		/*
800		 * The AX88190 (not A) has external 64k SRAM.  Probe for this
801		 * here.  Most of the cards I have either use the AX88190A
802		 * part, or have only 32k SRAM for some reason, so I don't
803		 * know if this works or not.
804		 */
805		ed_pio_writemem(sc, test_pattern, 32768, sizeof(test_pattern));
806		ed_pio_readmem(sc, 32768, test_buffer, sizeof(test_pattern));
807		if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)) == 0) {
808			sc->mem_start = 2*1024;
809			memsize = sc->mem_size = 62 * 1024;
810		}
811	}
812	sc->mem_end = sc->mem_start + memsize;
813	sc->tx_page_start = memsize / ED_PAGE_SIZE;
814	if (sc->mem_size > 16 * 1024)
815		sc->txb_cnt = 3;
816	else
817		sc->txb_cnt = 2;
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
826	/* Get the mac before we go -- It's just at 0x400 in "SRAM" */
827	ed_pio_readmem(sc, 0x400, sc->enaddr, ETHER_ADDR_LEN);
828
829	/* clear any pending interrupts that might have occurred above */
830	ed_nic_outb(sc, ED_P0_ISR, 0xff);
831	sc->sc_write_mbufs = ed_pio_write_mbufs;
832	return (0);
833}
834
835static int
836ed_pccard_ax88x90_check_mii(device_t dev, struct ed_softc *sc)
837{
838	int	i, id;
839
840	/*
841	 * All AX88x90 devices have MII and a PHY, so we use this to weed out
842	 * chips that would otherwise make it through the tests we have after
843	 * this point.
844	 */
845	for (i = 0; i < 32; i++) {
846		id = ed_miibus_readreg(dev, i, MII_BMSR);
847		if (id != 0 && id != 0xffff)
848			break;
849	}
850	/*
851	 * Found one, we're good.
852	 */
853	if (i != 32)
854		return (0);
855	/*
856	 * Didn't find anything, so try to power up and try again.  The PHY
857	 * may be not responding because we're in power down mode.
858	 */
859	if (sc->chip_type == ED_CHIP_TYPE_AX88190)
860		return (ENXIO);
861	pccard_ccr_write_1(dev, PCCARD_CCR_STATUS, PCCARD_CCR_STATUS_PWRDWN);
862	for (i = 0; i < 32; i++) {
863		id = ed_miibus_readreg(dev, i, MII_BMSR);
864		if (id != 0 && id != 0xffff)
865			break;
866	}
867	/*
868	 * Still no joy?  We're AFU, punt.
869	 */
870	if (i == 32)
871		return (ENXIO);
872	return (0);
873
874}
875
876/*
877 * Special setup for AX88[17]90
878 */
879static int
880ed_pccard_ax88x90(device_t dev, const struct ed_product *pp)
881{
882	int	error;
883	int iobase;
884	struct	ed_softc *sc = device_get_softc(dev);
885
886	if (!(pp->flags & NE2000DVF_AX88X90))
887		return (ENXIO);
888
889	if (bootverbose)
890		device_printf(dev, "Checking AX88x90\n");
891
892	/*
893	 * Set the IOBASE Register.  The AX88x90 cards are potentially
894	 * multifunction cards, and thus requires a slight workaround.
895	 * We write the address the card is at, on the off chance that this
896	 * card is not MFC.
897	 * XXX I'm not sure that this is still needed...
898	 */
899	iobase = rman_get_start(sc->port_res);
900	pccard_ccr_write_1(dev, PCCARD_CCR_IOBASE0, iobase & 0xff);
901	pccard_ccr_write_1(dev, PCCARD_CCR_IOBASE1, (iobase >> 8) & 0xff);
902
903	sc->mii_readbits = ed_pccard_ax88x90_mii_readbits;
904	sc->mii_writebits = ed_pccard_ax88x90_mii_writebits;
905	error = ed_probe_ax88x90_generic(dev, device_get_flags(dev));
906	if (error) {
907		if (bootverbose)
908			device_printf(dev, "probe ax88x90 failed %d\n",
909			    error);
910		goto fail;
911	}
912	error = ed_pccard_ax88x90_check_mii(dev, sc);
913	if (error)
914		goto fail;
915	sc->vendor = ED_VENDOR_NOVELL;
916	sc->type = ED_TYPE_NE2000;
917	if (sc->chip_type == ED_CHIP_TYPE_AX88190)
918		sc->type_str = "AX88190";
919	else
920		sc->type_str = "AX88790";
921	return (0);
922fail:;
923	sc->mii_readbits = 0;
924	sc->mii_writebits = 0;
925	return (error);
926}
927
928static void
929ed_pccard_ax88x90_mii_writebits(struct ed_softc *sc, u_int val, int nbits)
930{
931	int i, data;
932
933	for (i = nbits - 1; i >= 0; i--) {
934		data = (val >> i) & 1 ? ED_AX88X90_MII_DATAOUT : 0;
935		ed_asic_outb(sc, ED_AX88X90_MIIBUS, data);
936		ed_asic_outb(sc, ED_AX88X90_MIIBUS, data | ED_AX88X90_MII_CLK);
937	}
938}
939
940static u_int
941ed_pccard_ax88x90_mii_readbits(struct ed_softc *sc, int nbits)
942{
943	int i;
944	u_int val = 0;
945	uint8_t mdio;
946
947	mdio = ED_AX88X90_MII_DIRIN;
948	for (i = nbits - 1; i >= 0; i--) {
949		ed_asic_outb(sc, ED_AX88X90_MIIBUS, mdio);
950		val <<= 1;
951		if (ed_asic_inb(sc, ED_AX88X90_MIIBUS) & ED_AX88X90_MII_DATAIN)
952			val++;
953		ed_asic_outb(sc, ED_AX88X90_MIIBUS, mdio | ED_AX88X90_MII_CLK);
954	}
955	return val;
956}
957
958/*
959 * Special setup for TC5299J
960 */
961static int
962ed_pccard_tc5299j(device_t dev, const struct ed_product *pp)
963{
964	int	error, i, id;
965	char *ts;
966	struct	ed_softc *sc = device_get_softc(dev);
967
968	if (!(pp->flags & NE2000DVF_TC5299J))
969		return (ENXIO);
970
971	if (bootverbose)
972		device_printf(dev, "Checking Tc5299j\n");
973
974	/*
975	 * Check to see if we have a MII PHY ID at any of the first 32
976	 * locations.  All TC5299J devices have MII and a PHY, so we use
977	 * this to weed out chips that would otherwise make it through
978	 * the tests we have after this point.
979	 */
980	sc->mii_readbits = ed_pccard_tc5299j_mii_readbits;
981	sc->mii_writebits = ed_pccard_tc5299j_mii_writebits;
982	for (i = 0; i < 32; i++) {
983		id = ed_miibus_readreg(dev, i, MII_PHYIDR1);
984		if (id != 0 && id != 0xffff)
985			break;
986	}
987	if (i == 32) {
988		sc->mii_readbits = 0;
989		sc->mii_writebits = 0;
990		return (ENXIO);
991	}
992
993	ts = "TC5299J";
994	error = ed_probe_Novell_generic(dev, device_get_flags(dev));
995	if (bootverbose)
996		device_printf(dev, "probe novel returns %d\n", error);
997	if (error != 0) {
998		sc->mii_readbits = 0;
999		sc->mii_writebits = 0;
1000		return (error);
1001	}
1002	if (ed_pccard_rom_mac(dev, sc->enaddr) == 0) {
1003		sc->mii_readbits = 0;
1004		sc->mii_writebits = 0;
1005		return (ENXIO);
1006	}
1007	sc->vendor = ED_VENDOR_NOVELL;
1008	sc->type = ED_TYPE_NE2000;
1009	sc->chip_type = ED_CHIP_TYPE_TC5299J;
1010	sc->type_str = ts;
1011	return (0);
1012}
1013
1014/* MII bit-twiddling routines for cards using TC5299J chipset */
1015#define TC5299J_MIISET(sc, x) ed_nic_outb(sc, ED_TC5299J_MIIBUS, \
1016    ed_nic_inb(sc, ED_TC5299J_MIIBUS) | (x))
1017#define TC5299J_MIICLR(sc, x) ed_nic_outb(sc, ED_TC5299J_MIIBUS, \
1018    ed_nic_inb(sc, ED_TC5299J_MIIBUS) & ~(x))
1019
1020static void
1021ed_pccard_tc5299j_mii_reset(struct ed_softc *sc)
1022{
1023	/* Do nothing! */
1024}
1025
1026static void
1027ed_pccard_tc5299j_mii_writebits(struct ed_softc *sc, u_int val, int nbits)
1028{
1029	int i;
1030	uint8_t cr;
1031
1032	cr = ed_nic_inb(sc, ED_P0_CR);
1033	ed_nic_outb(sc, ED_P0_CR, cr | ED_CR_PAGE_3);
1034
1035	TC5299J_MIICLR(sc, ED_TC5299J_MII_DIROUT);
1036	for (i = nbits - 1; i >= 0; i--) {
1037		if ((val >> i) & 1)
1038			TC5299J_MIISET(sc, ED_TC5299J_MII_DATAOUT);
1039		else
1040			TC5299J_MIICLR(sc, ED_TC5299J_MII_DATAOUT);
1041		TC5299J_MIISET(sc, ED_TC5299J_MII_CLK);
1042		TC5299J_MIICLR(sc, ED_TC5299J_MII_CLK);
1043	}
1044	ed_nic_outb(sc, ED_P0_CR, cr);
1045}
1046
1047static u_int
1048ed_pccard_tc5299j_mii_readbits(struct ed_softc *sc, int nbits)
1049{
1050	int i;
1051	u_int val = 0;
1052	uint8_t cr;
1053
1054	cr = ed_nic_inb(sc, ED_P0_CR);
1055	ed_nic_outb(sc, ED_P0_CR, cr | ED_CR_PAGE_3);
1056
1057	TC5299J_MIISET(sc, ED_TC5299J_MII_DIROUT);
1058	for (i = nbits - 1; i >= 0; i--) {
1059		TC5299J_MIISET(sc, ED_TC5299J_MII_CLK);
1060		val <<= 1;
1061		if (ed_nic_inb(sc, ED_TC5299J_MIIBUS) & ED_TC5299J_MII_DATAIN)
1062			val++;
1063		TC5299J_MIICLR(sc, ED_TC5299J_MII_CLK);
1064	}
1065	ed_nic_outb(sc, ED_P0_CR, cr);
1066	return val;
1067}
1068
1069/*
1070 * MII bus support routines.
1071 */
1072static int
1073ed_miibus_readreg(device_t dev, int phy, int reg)
1074{
1075	struct ed_softc *sc;
1076	int failed, val;
1077
1078	sc = device_get_softc(dev);
1079	/*
1080	 * The AX88790 has an interesting quirk.  It has an internal phy that
1081	 * needs a special bit set to access, but can also have additional
1082	 * external PHYs set for things like HomeNET media.  When accessing
1083	 * the internal PHY, a bit has to be set, when accessing the external
1084	 * PHYs, it must be clear.  See Errata 1, page 51, in the AX88790
1085	 * datasheet for more details.
1086	 *
1087	 * Also, PHYs above 16 appear to be phantoms on some cards, but not
1088	 * others.  Registers read for this are often the same as prior values
1089	 * read.  Filter all register requests to 17-31.
1090	 *
1091	 * I can't explain it, since I don't have the DL100xx data sheets, but
1092	 * the DL100xx chips do 13-bits before the 'ACK' but, but the AX88x90
1093	 * chips have 14.  The linux pcnet and axnet drivers confirm this.
1094	 */
1095	if (sc->chip_type == ED_CHIP_TYPE_AX88790) {
1096		if (phy > 0x10)
1097			return (0);
1098		if (phy == 0x10)
1099			ed_asic_outb(sc, ED_AX88X90_GPIO,
1100			    ED_AX88X90_GPIO_INT_PHY);
1101		else
1102			ed_asic_outb(sc, ED_AX88X90_GPIO, 0);
1103	}
1104
1105	(*sc->mii_writebits)(sc, 0xffffffff, 32);
1106	(*sc->mii_writebits)(sc, ED_MII_STARTDELIM, ED_MII_STARTDELIM_BITS);
1107	(*sc->mii_writebits)(sc, ED_MII_READOP, ED_MII_OP_BITS);
1108	(*sc->mii_writebits)(sc, phy, ED_MII_PHY_BITS);
1109	(*sc->mii_writebits)(sc, reg, ED_MII_REG_BITS);
1110	if (sc->chip_type == ED_CHIP_TYPE_AX88790 ||
1111	    sc->chip_type == ED_CHIP_TYPE_AX88190)
1112		(*sc->mii_readbits)(sc, ED_MII_ACK_BITS);
1113	failed = (*sc->mii_readbits)(sc, ED_MII_ACK_BITS);
1114	val = (*sc->mii_readbits)(sc, ED_MII_DATA_BITS);
1115	(*sc->mii_writebits)(sc, ED_MII_IDLE, ED_MII_IDLE_BITS);
1116/*	printf("Reading phy %d reg %#x returning %#x (valid %d)\n", phy, reg, val, !failed); */
1117	return (failed ? 0 : val);
1118}
1119
1120static int
1121ed_miibus_writereg(device_t dev, int phy, int reg, int data)
1122{
1123	struct ed_softc *sc;
1124
1125/*	printf("Writing phy %d reg %#x data %#x\n", phy, reg, data); */
1126	sc = device_get_softc(dev);
1127	/* See ed_miibus_readreg for details */
1128	if (sc->chip_type == ED_CHIP_TYPE_AX88790) {
1129		if (phy > 0x10)
1130			return (0);
1131		if (phy == 0x10)
1132			ed_asic_outb(sc, ED_AX88X90_GPIO,
1133			    ED_AX88X90_GPIO_INT_PHY);
1134		else
1135			ed_asic_outb(sc, ED_AX88X90_GPIO, 0);
1136	}
1137	(*sc->mii_writebits)(sc, 0xffffffff, 32);
1138	(*sc->mii_writebits)(sc, ED_MII_STARTDELIM, ED_MII_STARTDELIM_BITS);
1139	(*sc->mii_writebits)(sc, ED_MII_WRITEOP, ED_MII_OP_BITS);
1140	(*sc->mii_writebits)(sc, phy, ED_MII_PHY_BITS);
1141	(*sc->mii_writebits)(sc, reg, ED_MII_REG_BITS);
1142	(*sc->mii_writebits)(sc, ED_MII_TURNAROUND, ED_MII_TURNAROUND_BITS);
1143	(*sc->mii_writebits)(sc, data, ED_MII_DATA_BITS);
1144	(*sc->mii_writebits)(sc, ED_MII_IDLE, ED_MII_IDLE_BITS);
1145	return (0);
1146}
1147
1148static int
1149ed_ifmedia_upd(struct ifnet *ifp)
1150{
1151	struct ed_softc *sc;
1152	int error;
1153
1154	sc = ifp->if_softc;
1155	if (sc->miibus == NULL)
1156		return (ENXIO);
1157	ED_LOCK(sc);
1158	error = ed_pccard_kick_phy(sc);
1159	ED_UNLOCK(sc);
1160	return (error);
1161}
1162
1163static void
1164ed_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1165{
1166	struct ed_softc *sc;
1167	struct mii_data *mii;
1168
1169	sc = ifp->if_softc;
1170	if (sc->miibus == NULL)
1171		return;
1172
1173	mii = device_get_softc(sc->miibus);
1174	mii_pollstat(mii);
1175	ifmr->ifm_active = mii->mii_media_active;
1176	ifmr->ifm_status = mii->mii_media_status;
1177}
1178
1179static void
1180ed_child_detached(device_t dev, device_t child)
1181{
1182	struct ed_softc *sc;
1183
1184	sc = device_get_softc(dev);
1185	if (child == sc->miibus)
1186		sc->miibus = NULL;
1187}
1188
1189static device_method_t ed_pccard_methods[] = {
1190	/* Device interface */
1191	DEVMETHOD(device_probe,		ed_pccard_probe),
1192	DEVMETHOD(device_attach,	ed_pccard_attach),
1193	DEVMETHOD(device_detach,	ed_detach),
1194
1195	/* Bus interface */
1196	DEVMETHOD(bus_child_detached,	ed_child_detached),
1197
1198	/* MII interface */
1199	DEVMETHOD(miibus_readreg,	ed_miibus_readreg),
1200	DEVMETHOD(miibus_writereg,	ed_miibus_writereg),
1201
1202	{ 0, 0 }
1203};
1204
1205static driver_t ed_pccard_driver = {
1206	"ed",
1207	ed_pccard_methods,
1208	sizeof(struct ed_softc)
1209};
1210
1211DRIVER_MODULE(ed, pccard, ed_pccard_driver, ed_devclass, 0, 0);
1212DRIVER_MODULE(miibus, ed, miibus_driver, miibus_devclass, 0, 0);
1213MODULE_DEPEND(ed, miibus, 1, 1, 1);
1214MODULE_DEPEND(ed, ether, 1, 1, 1);
1215