1139749Simp/*-
251673Smdodd * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
351673Smdodd * All rights reserved.
451673Smdodd *
551673Smdodd * Redistribution and use in source and binary forms, with or without
651673Smdodd * modification, are permitted provided that the following conditions
751673Smdodd * are met:
851673Smdodd * 1. Redistributions of source code must retain the above copyright
951673Smdodd *    notice, this list of conditions and the following disclaimer.
1051673Smdodd * 2. Redistributions in binary form must reproduce the above copyright
1151673Smdodd *    notice, this list of conditions and the following disclaimer in the
1251673Smdodd *    documentation and/or other materials provided with the distribution.
1351673Smdodd * 3. All advertising materials mentioning features or use of this software
1451673Smdodd *    must display the following acknowledgement:
1551673Smdodd *      This product includes software developed by Herb Peyerl.
1651673Smdodd * 4. The name of Herb Peyerl may not be used to endorse or promote products
1751673Smdodd *    derived from this software without specific prior written permission.
1851673Smdodd *
1951673Smdodd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2051673Smdodd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2151673Smdodd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2251673Smdodd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2351673Smdodd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2451673Smdodd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2551673Smdodd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2651673Smdodd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2751673Smdodd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2851673Smdodd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2951673Smdodd */
3051673Smdodd
3151673Smdodd/*
3251673Smdodd * Pccard support for 3C589 by:
3351673Smdodd *		HAMADA Naoki
3451673Smdodd *		nao@tom-yam.or.jp
3551673Smdodd */
3651673Smdodd
37117700Smarkm#include <sys/cdefs.h>
38117700Smarkm__FBSDID("$FreeBSD: releng/10.3/sys/dev/ep/if_ep_pccard.c 246128 2013-01-30 18:01:20Z sbz $");
39117700Smarkm
4051673Smdodd#include <sys/param.h>
4152549Smdodd#include <sys/systm.h>
4251673Smdodd#include <sys/kernel.h>
4351673Smdodd#include <sys/socket.h>
4452549Smdodd#include <sys/module.h>
4552472Simp#include <sys/bus.h>
4652549Smdodd
4752472Simp#include <machine/bus.h>
4852549Smdodd#include <machine/resource.h>
49117700Smarkm
5063090Sarchie#include <net/ethernet.h>
51117700Smarkm#include <net/if.h>
5252549Smdodd#include <net/if_arp.h>
5352549Smdodd#include <net/if_media.h>
5451673Smdodd
5552472Simp#include <dev/ep/if_epreg.h>
5652472Simp#include <dev/ep/if_epvar.h>
5752472Simp
5866058Simp#include <dev/pccard/pccardvar.h>
59140522Simp#include <dev/pccard/pccard_cis.h>
60117700Smarkm
61129764Simp#include "pccarddevs.h"
6266058Simp
63147649Simpstruct ep_pccard_product
6451673Smdodd{
65147649Simp	struct pccard_product prod;
66147649Simp	int chipset;
67147649Simp};
6851673Smdodd
69147649Simp#define EP_CHIP_589	1	/* Classic 3c5x9 chipset */
70147649Simp#define EP_CHIP_574	2	/* Roadrunner */
71190908Simp#define EP_CHIP_C1	3	/* 3c1 */
7252549Smdodd
73147649Simpstatic const struct ep_pccard_product ep_pccard_products[] = {
74190908Simp	{ PCMCIA_CARD(3COM, 3C1),		EP_CHIP_C1 },
75147649Simp	{ PCMCIA_CARD(3COM, 3C562),		EP_CHIP_589 },
76147649Simp	{ PCMCIA_CARD(3COM, 3C589),		EP_CHIP_589 },
77147649Simp	{ PCMCIA_CARD(3COM, 3CXEM556),		EP_CHIP_589 },
78147649Simp	{ PCMCIA_CARD(3COM, 3CXEM556INT),	EP_CHIP_589 },
79147649Simp	{ PCMCIA_CARD(3COM, 3C574),		EP_CHIP_574 },
80147649Simp	{ PCMCIA_CARD(3COM, 3CCFEM556BI),	EP_CHIP_574 },
81147649Simp	{ { NULL } }
82147649Simp};
8351673Smdodd
84147649Simpstatic const struct ep_pccard_product *
85147649Simpep_pccard_lookup(device_t dev)
8652472Simp{
87147649Simp	return ((const struct ep_pccard_product *)pccard_product_lookup(dev,
88147649Simp	    (const struct pccard_product *)ep_pccard_products,
89147649Simp	    sizeof(ep_pccard_products[0]), NULL));
9051673Smdodd}
9151673Smdodd
9251673Smdoddstatic int
93147649Simpep_pccard_probe(device_t dev)
9451673Smdodd{
95147649Simp	const struct ep_pccard_product *pp;
96147649Simp	int		error;
97147649Simp	uint32_t	fcn = PCCARD_FUNCTION_UNSPEC;
98147649Simp
99147649Simp	/* Make sure we're a network function */
100147649Simp	error = pccard_get_function(dev, &fcn);
101147649Simp	if (error != 0)
102147649Simp		return (error);
103147649Simp	if (fcn != PCCARD_FUNCTION_NETWORK)
104147649Simp		return (ENXIO);
105147649Simp
106147649Simp	/* Check to see if we know about this card */
107147649Simp	if ((pp = ep_pccard_lookup(dev)) == NULL)
108147649Simp		return EIO;
109147649Simp	if (pp->prod.pp_name != NULL)
110147649Simp		device_set_desc(dev, pp->prod.pp_name);
111147649Simp
112147649Simp	return 0;
11351673Smdodd}
11451673Smdodd
11551673Smdoddstatic int
116147729Simpep_pccard_mac(const struct pccard_tuple *tuple, void *argp)
117147715Simp{
118147715Simp	uint8_t *enaddr = argp;
119147715Simp	int i;
120147715Simp
121147715Simp	/* Code 0x88 is 3com's special cis node contianing the MAC */
122147715Simp	if (tuple->code != 0x88)
123147715Simp		return (0);
124147715Simp
125147715Simp	/* Make sure this is a sane node */
126147715Simp	if (tuple->length < ETHER_ADDR_LEN)
127147715Simp		return (0);
128147715Simp
129147715Simp	/* Copy the MAC ADDR and return success */
130147715Simp	for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
131147715Simp		enaddr[i] = pccard_tuple_read_1(tuple, i + 1);
132147715Simp		enaddr[i + 1] = pccard_tuple_read_1(tuple, i);
133147715Simp	}
134147715Simp	return (1);
135147715Simp}
136147715Simp
137147715Simpstatic int
13852472Simpep_pccard_attach(device_t dev)
13951673Smdodd{
140117700Smarkm	struct ep_softc *sc = device_get_softc(dev);
141140523Simp	uint16_t result;
142117700Smarkm	int error = 0;
143147649Simp	const struct ep_pccard_product *pp;
14451673Smdodd
145147649Simp	if ((pp = ep_pccard_lookup(dev)) == NULL)
146147649Simp		panic("ep_pccard_attach: can't find product in attach.");
147147649Simp
148190908Simp	if (pp->chipset == EP_CHIP_574) {
149190908Simp		sc->epb.mii_trans = 1;
150190908Simp		sc->epb.cmd_off = 2;
151190908Simp	} else {
152147649Simp		sc->epb.mii_trans = 0;
153147649Simp		sc->epb.cmd_off = 0;
154147649Simp	}
155147715Simp	if ((error = ep_alloc(dev))) {
156147715Simp		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
157147715Simp		goto bad;
158147715Simp	}
159112822Smdodd
160190908Simp	if (pp->chipset == EP_CHIP_C1)
161190908Simp		sc->stat |= F_HAS_TX_PLL;
162190908Simp
16352472Simp	/* ROM size = 0, ROM base = 0 */
16452472Simp	/* For now, ignore AUTO SELECT feature of 3C589B and later. */
165147649Simp	error = ep_get_e(sc, EEPROM_ADDR_CFG, &result);
166121492Simp	CSR_WRITE_2(sc, EP_W0_ADDRESS_CFG, result & 0xc000);
167121904Simp
168121904Simp	/*
169121904Simp	 * Fake IRQ must be 3 for 3C589 and 3C589B.  3C589D and newer
170121904Simp	 * ignore this value.  3C589C is unknown, as are the other
171121904Simp	 * cards supported by this driver, but it appears to never hurt
172121904Simp	 * and always helps.
173121904Simp	 */
174121588Simp	SET_IRQ(sc, 3);
175121492Simp	CSR_WRITE_2(sc, EP_W0_PRODUCT_ID, sc->epb.prod_id);
17652472Simp
17752549Smdodd	if (sc->epb.mii_trans) {
17852472Simp		/*
17952472Simp		 * turn on the MII transciever
18052472Simp		 */
181121588Simp		GO_WINDOW(sc, 3);
182121492Simp		CSR_WRITE_2(sc, EP_W3_OPTIONS, 0x8040);
18352472Simp		DELAY(1000);
184121492Simp		CSR_WRITE_2(sc, EP_W3_OPTIONS, 0xc040);
185121492Simp		CSR_WRITE_2(sc, EP_COMMAND, RX_RESET);
186121492Simp		CSR_WRITE_2(sc, EP_COMMAND, TX_RESET);
187121515Simp		EP_BUSY_WAIT(sc);
18852472Simp		DELAY(1000);
189121492Simp		CSR_WRITE_2(sc, EP_W3_OPTIONS, 0x8040);
190117700Smarkm	} else
19152549Smdodd		ep_get_media(sc);
19252472Simp
193147715Simp	/*
194147715Simp	 * The 3C562 (a-c revisions) stores the MAC in the CIS in a
195147715Simp	 * way that's unique to 3com.  If we have one of these cards,
196147715Simp	 * scan the CIS for that MAC address, and use it if we find
197147715Simp	 * it.  The NetBSD driver says that the ROADRUNNER chips also
198147715Simp	 * do this, which may be true, but none of the cards that I
199147715Simp	 * have include this TUPLE.  Always prefer the MAC addr in the
200147715Simp	 * CIS tuple to the one returned by the card, as it appears that
201147715Simp	 * only those cards that need it have this special tuple.
202147715Simp	 */
203150110Simp	if (pccard_cis_scan(dev, ep_pccard_mac, sc->eaddr))
204147715Simp		sc->stat |= F_ENADDR_SKIP;
20552549Smdodd	if ((error = ep_attach(sc))) {
20652549Smdodd		device_printf(dev, "ep_attach() failed! (%d)\n", error);
20752472Simp		goto bad;
20852472Simp	}
209147649Simp	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
210166901Spiso	    NULL, ep_intr, sc, &sc->ep_intrhand))) {
21152549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
21252472Simp		goto bad;
21352472Simp	}
21452549Smdodd	return (0);
21552472Simpbad:
21652585Simp	ep_free(dev);
21752549Smdodd	return (error);
21851673Smdodd}
21951673Smdodd
22052472Simpstatic device_method_t ep_pccard_methods[] = {
22152472Simp	/* Device interface */
222147649Simp	DEVMETHOD(device_probe, ep_pccard_probe),
223147649Simp	DEVMETHOD(device_attach, ep_pccard_attach),
224117700Smarkm	DEVMETHOD(device_detach, ep_detach),
22551673Smdodd
226246128Ssbz	DEVMETHOD_END
22752472Simp};
22851673Smdodd
22952472Simpstatic driver_t ep_pccard_driver = {
23052472Simp	"ep",
23152472Simp	ep_pccard_methods,
23252549Smdodd	sizeof(struct ep_softc),
23352472Simp};
23451673Smdodd
23552472Simpextern devclass_t ep_devclass;
23652472Simp
23752472SimpDRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0);
238