if_ep_pccard.c revision 166901
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: head/sys/dev/ep/if_ep_pccard.c 166901 2007-02-23 12:19:07Z piso $"); 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 */ 7152549Smdodd 72147649Simpstatic const struct ep_pccard_product ep_pccard_products[] = { 73147649Simp { PCMCIA_CARD(3COM, 3C1), EP_CHIP_589 }, 74147649Simp { PCMCIA_CARD(3COM, 3C562), EP_CHIP_589 }, 75147649Simp { PCMCIA_CARD(3COM, 3C589), EP_CHIP_589 }, 76147649Simp { PCMCIA_CARD(3COM, 3CXEM556), EP_CHIP_589 }, 77147649Simp { PCMCIA_CARD(3COM, 3CXEM556INT), EP_CHIP_589 }, 78147649Simp { PCMCIA_CARD(3COM, 3C574), EP_CHIP_574 }, 79147649Simp { PCMCIA_CARD(3COM, 3CCFEM556BI), EP_CHIP_574 }, 80147649Simp { { NULL } } 81147649Simp}; 8251673Smdodd 83147649Simpstatic const struct ep_pccard_product * 84147649Simpep_pccard_lookup(device_t dev) 8552472Simp{ 86147649Simp return ((const struct ep_pccard_product *)pccard_product_lookup(dev, 87147649Simp (const struct pccard_product *)ep_pccard_products, 88147649Simp sizeof(ep_pccard_products[0]), NULL)); 8951673Smdodd} 9051673Smdodd 9151673Smdoddstatic int 92147649Simpep_pccard_probe(device_t dev) 9351673Smdodd{ 94147649Simp const struct ep_pccard_product *pp; 95147649Simp int error; 96147649Simp uint32_t fcn = PCCARD_FUNCTION_UNSPEC; 97147649Simp 98147649Simp /* Make sure we're a network function */ 99147649Simp error = pccard_get_function(dev, &fcn); 100147649Simp if (error != 0) 101147649Simp return (error); 102147649Simp if (fcn != PCCARD_FUNCTION_NETWORK) 103147649Simp return (ENXIO); 104147649Simp 105147649Simp /* Check to see if we know about this card */ 106147649Simp if ((pp = ep_pccard_lookup(dev)) == NULL) 107147649Simp return EIO; 108147649Simp if (pp->prod.pp_name != NULL) 109147649Simp device_set_desc(dev, pp->prod.pp_name); 110147649Simp 111147649Simp return 0; 11251673Smdodd} 11351673Smdodd 11451673Smdoddstatic int 115147729Simpep_pccard_mac(const struct pccard_tuple *tuple, void *argp) 116147715Simp{ 117147715Simp uint8_t *enaddr = argp; 118147715Simp int i; 119147715Simp 120147715Simp /* Code 0x88 is 3com's special cis node contianing the MAC */ 121147715Simp if (tuple->code != 0x88) 122147715Simp return (0); 123147715Simp 124147715Simp /* Make sure this is a sane node */ 125147715Simp if (tuple->length < ETHER_ADDR_LEN) 126147715Simp return (0); 127147715Simp 128147715Simp /* Copy the MAC ADDR and return success */ 129147715Simp for (i = 0; i < ETHER_ADDR_LEN; i += 2) { 130147715Simp enaddr[i] = pccard_tuple_read_1(tuple, i + 1); 131147715Simp enaddr[i + 1] = pccard_tuple_read_1(tuple, i); 132147715Simp } 133147715Simp return (1); 134147715Simp} 135147715Simp 136147715Simpstatic int 13752472Simpep_pccard_attach(device_t dev) 13851673Smdodd{ 139117700Smarkm struct ep_softc *sc = device_get_softc(dev); 140140523Simp uint16_t result; 141117700Smarkm int error = 0; 142147649Simp const struct ep_pccard_product *pp; 14351673Smdodd 144147649Simp if ((pp = ep_pccard_lookup(dev)) == NULL) 145147649Simp panic("ep_pccard_attach: can't find product in attach."); 146147649Simp 147147649Simp if (pp->chipset == EP_CHIP_589) { 148147649Simp sc->epb.mii_trans = 0; 149147649Simp sc->epb.cmd_off = 0; 150147649Simp } else { 151147649Simp sc->epb.mii_trans = 1; 152147649Simp sc->epb.cmd_off = 2; 153147649Simp } 154147649Simp 155147715Simp if ((error = ep_alloc(dev))) { 156147715Simp device_printf(dev, "ep_alloc() failed! (%d)\n", error); 157147715Simp goto bad; 158147715Simp } 159112822Smdodd 16052472Simp /* ROM size = 0, ROM base = 0 */ 16152472Simp /* For now, ignore AUTO SELECT feature of 3C589B and later. */ 162147649Simp error = ep_get_e(sc, EEPROM_ADDR_CFG, &result); 163121492Simp CSR_WRITE_2(sc, EP_W0_ADDRESS_CFG, result & 0xc000); 164121904Simp 165121904Simp /* 166121904Simp * Fake IRQ must be 3 for 3C589 and 3C589B. 3C589D and newer 167121904Simp * ignore this value. 3C589C is unknown, as are the other 168121904Simp * cards supported by this driver, but it appears to never hurt 169121904Simp * and always helps. 170121904Simp */ 171121588Simp SET_IRQ(sc, 3); 172121492Simp CSR_WRITE_2(sc, EP_W0_PRODUCT_ID, sc->epb.prod_id); 17352472Simp 17452549Smdodd if (sc->epb.mii_trans) { 17552472Simp /* 17652472Simp * turn on the MII transciever 17752472Simp */ 178121588Simp GO_WINDOW(sc, 3); 179121492Simp CSR_WRITE_2(sc, EP_W3_OPTIONS, 0x8040); 18052472Simp DELAY(1000); 181121492Simp CSR_WRITE_2(sc, EP_W3_OPTIONS, 0xc040); 182121492Simp CSR_WRITE_2(sc, EP_COMMAND, RX_RESET); 183121492Simp CSR_WRITE_2(sc, EP_COMMAND, TX_RESET); 184121515Simp EP_BUSY_WAIT(sc); 18552472Simp DELAY(1000); 186121492Simp CSR_WRITE_2(sc, EP_W3_OPTIONS, 0x8040); 187117700Smarkm } else 18852549Smdodd ep_get_media(sc); 18952472Simp 190147715Simp /* 191147715Simp * The 3C562 (a-c revisions) stores the MAC in the CIS in a 192147715Simp * way that's unique to 3com. If we have one of these cards, 193147715Simp * scan the CIS for that MAC address, and use it if we find 194147715Simp * it. The NetBSD driver says that the ROADRUNNER chips also 195147715Simp * do this, which may be true, but none of the cards that I 196147715Simp * have include this TUPLE. Always prefer the MAC addr in the 197147715Simp * CIS tuple to the one returned by the card, as it appears that 198147715Simp * only those cards that need it have this special tuple. 199147715Simp */ 200150110Simp if (pccard_cis_scan(dev, ep_pccard_mac, sc->eaddr)) 201147715Simp sc->stat |= F_ENADDR_SKIP; 20252549Smdodd if ((error = ep_attach(sc))) { 20352549Smdodd device_printf(dev, "ep_attach() failed! (%d)\n", error); 20452472Simp goto bad; 20552472Simp } 206147649Simp if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 207166901Spiso NULL, ep_intr, sc, &sc->ep_intrhand))) { 20852549Smdodd device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 20952472Simp goto bad; 21052472Simp } 21152549Smdodd return (0); 21252472Simpbad: 21352585Simp ep_free(dev); 21452549Smdodd return (error); 21551673Smdodd} 21651673Smdodd 21752472Simpstatic device_method_t ep_pccard_methods[] = { 21852472Simp /* Device interface */ 219147649Simp DEVMETHOD(device_probe, ep_pccard_probe), 220147649Simp DEVMETHOD(device_attach, ep_pccard_attach), 221117700Smarkm DEVMETHOD(device_detach, ep_detach), 22251673Smdodd 223117700Smarkm {0, 0} 22452472Simp}; 22551673Smdodd 22652472Simpstatic driver_t ep_pccard_driver = { 22752472Simp "ep", 22852472Simp ep_pccard_methods, 22952549Smdodd sizeof(struct ep_softc), 23052472Simp}; 23151673Smdodd 23252472Simpextern devclass_t ep_devclass; 23352472Simp 23452472SimpDRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0); 235