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