Back to home page

OSCL-LXR

 
 

    


0001 /* mac89x0.c: A Crystal Semiconductor CS89[02]0 driver for linux. */
0002 /*
0003     Written 1996 by Russell Nelson, with reference to skeleton.c
0004     written 1993-1994 by Donald Becker.
0005 
0006     This software may be used and distributed according to the terms
0007     of the GNU General Public License, incorporated herein by reference.
0008 
0009     The author may be reached at nelson@crynwr.com, Crynwr
0010     Software, 11 Grant St., Potsdam, NY 13676
0011 
0012   Changelog:
0013 
0014   Mike Cruse        : mcruse@cti-ltd.com
0015                     : Changes for Linux 2.0 compatibility.
0016                     : Added dev_id parameter in net_interrupt(),
0017                     : request_irq() and free_irq(). Just NULL for now.
0018 
0019   Mike Cruse        : Added MOD_INC_USE_COUNT and MOD_DEC_USE_COUNT macros
0020                     : in net_open() and net_close() so kerneld would know
0021                     : that the module is in use and wouldn't eject the
0022                     : driver prematurely.
0023 
0024   Mike Cruse        : Rewrote init_module() and cleanup_module using 8390.c
0025                     : as an example. Disabled autoprobing in init_module(),
0026                     : not a good thing to do to other devices while Linux
0027                     : is running from all accounts.
0028 
0029   Alan Cox          : Removed 1.2 support, added 2.1 extra counters.
0030 
0031   David Huggins-Daines <dhd@debian.org>
0032 
0033   Split this off into mac89x0.c, and gutted it of all parts which are
0034   not relevant to the existing CS8900 cards on the Macintosh
0035   (i.e. basically the Daynaport CS and LC cards).  To be precise:
0036 
0037     * Removed all the media-detection stuff, because these cards are
0038     TP-only.
0039 
0040     * Lobotomized the ISA interrupt bogosity, because these cards use
0041     a hardwired NuBus interrupt and a magic ISAIRQ value in the card.
0042 
0043     * Basically eliminated everything not relevant to getting the
0044     cards minimally functioning on the Macintosh.
0045 
0046   I might add that these cards are badly designed even from the Mac
0047   standpoint, in that Dayna, in their infinite wisdom, used NuBus slot
0048   I/O space and NuBus interrupts for these cards, but neglected to
0049   provide anything even remotely resembling a NuBus ROM.  Therefore we
0050   have to probe for them in a brain-damaged ISA-like fashion.
0051 
0052   Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 11/01/2001
0053   check kmalloc and release the allocated memory on failure in
0054   mac89x0_probe and in init_module
0055   use local_irq_{save,restore}(flags) in net_get_stat, not just
0056   local_irq_{dis,en}able()
0057 */
0058 
0059 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0060 
0061 static const char version[] =
0062 "cs89x0.c:v1.02 11/26/96 Russell Nelson <nelson@crynwr.com>\n";
0063 
0064 #include <linux/module.h>
0065 
0066 /*
0067   Sources:
0068 
0069     Crynwr packet driver epktisa.
0070 
0071     Crystal Semiconductor data sheets.
0072 
0073 */
0074 
0075 #include <linux/kernel.h>
0076 #include <linux/types.h>
0077 #include <linux/fcntl.h>
0078 #include <linux/interrupt.h>
0079 #include <linux/ioport.h>
0080 #include <linux/in.h>
0081 #include <linux/string.h>
0082 #include <linux/nubus.h>
0083 #include <linux/errno.h>
0084 #include <linux/init.h>
0085 #include <linux/netdevice.h>
0086 #include <linux/platform_device.h>
0087 #include <linux/etherdevice.h>
0088 #include <linux/skbuff.h>
0089 #include <linux/delay.h>
0090 #include <linux/bitops.h>
0091 #include <linux/gfp.h>
0092 
0093 #include <asm/io.h>
0094 #include <asm/hwtest.h>
0095 #include <asm/macints.h>
0096 
0097 #include "cs89x0.h"
0098 
0099 static int debug = -1;
0100 module_param(debug, int, 0);
0101 MODULE_PARM_DESC(debug, "debug message level");
0102 
0103 /* Information that need to be kept for each board. */
0104 struct net_local {
0105     int msg_enable;
0106     int chip_type;      /* one of: CS8900, CS8920, CS8920M */
0107     char chip_revision; /* revision letter of the chip ('A'...) */
0108     int send_cmd;       /* the propercommand used to send a packet. */
0109     int rx_mode;
0110     int curr_rx_cfg;
0111         int send_underrun;      /* keep track of how many underruns in a row we get */
0112 };
0113 
0114 /* Index to functions, as function prototypes. */
0115 static int net_open(struct net_device *dev);
0116 static netdev_tx_t net_send_packet(struct sk_buff *skb, struct net_device *dev);
0117 static irqreturn_t net_interrupt(int irq, void *dev_id);
0118 static void set_multicast_list(struct net_device *dev);
0119 static void net_rx(struct net_device *dev);
0120 static int net_close(struct net_device *dev);
0121 static struct net_device_stats *net_get_stats(struct net_device *dev);
0122 static int set_mac_address(struct net_device *dev, void *addr);
0123 
0124 /* For reading/writing registers ISA-style */
0125 static inline int
0126 readreg_io(struct net_device *dev, int portno)
0127 {
0128     nubus_writew(swab16(portno), dev->base_addr + ADD_PORT);
0129     return swab16(nubus_readw(dev->base_addr + DATA_PORT));
0130 }
0131 
0132 static inline void
0133 writereg_io(struct net_device *dev, int portno, int value)
0134 {
0135     nubus_writew(swab16(portno), dev->base_addr + ADD_PORT);
0136     nubus_writew(swab16(value), dev->base_addr + DATA_PORT);
0137 }
0138 
0139 /* These are for reading/writing registers in shared memory */
0140 static inline int
0141 readreg(struct net_device *dev, int portno)
0142 {
0143     return swab16(nubus_readw(dev->mem_start + portno));
0144 }
0145 
0146 static inline void
0147 writereg(struct net_device *dev, int portno, int value)
0148 {
0149     nubus_writew(swab16(value), dev->mem_start + portno);
0150 }
0151 
0152 static const struct net_device_ops mac89x0_netdev_ops = {
0153     .ndo_open       = net_open,
0154     .ndo_stop       = net_close,
0155     .ndo_start_xmit     = net_send_packet,
0156     .ndo_get_stats      = net_get_stats,
0157     .ndo_set_rx_mode    = set_multicast_list,
0158     .ndo_set_mac_address    = set_mac_address,
0159     .ndo_validate_addr  = eth_validate_addr,
0160 };
0161 
0162 /* Probe for the CS8900 card in slot E.  We won't bother looking
0163    anywhere else until we have a really good reason to do so. */
0164 static int mac89x0_device_probe(struct platform_device *pdev)
0165 {
0166     struct net_device *dev;
0167     struct net_local *lp;
0168     int i, slot;
0169     unsigned rev_type = 0;
0170     unsigned long ioaddr;
0171     unsigned short sig;
0172     int err = -ENODEV;
0173     struct nubus_rsrc *fres;
0174 
0175     dev = alloc_etherdev(sizeof(struct net_local));
0176     if (!dev)
0177         return -ENOMEM;
0178 
0179     /* We might have to parameterize this later */
0180     slot = 0xE;
0181     /* Get out now if there's a real NuBus card in slot E */
0182     for_each_func_rsrc(fres)
0183         if (fres->board->slot == slot)
0184             goto out;
0185 
0186     /* The pseudo-ISA bits always live at offset 0x300 (gee,
0187            wonder why...) */
0188     ioaddr = (unsigned long)
0189         nubus_slot_addr(slot) | (((slot&0xf) << 20) + DEFAULTIOBASE);
0190     {
0191         int card_present;
0192 
0193         card_present = (hwreg_present((void *)ioaddr + 4) &&
0194                 hwreg_present((void *)ioaddr + DATA_PORT));
0195         if (!card_present)
0196             goto out;
0197     }
0198 
0199     nubus_writew(0, ioaddr + ADD_PORT);
0200     sig = nubus_readw(ioaddr + DATA_PORT);
0201     if (sig != swab16(CHIP_EISA_ID_SIG))
0202         goto out;
0203 
0204     SET_NETDEV_DEV(dev, &pdev->dev);
0205 
0206     /* Initialize the net_device structure. */
0207     lp = netdev_priv(dev);
0208 
0209     lp->msg_enable = netif_msg_init(debug, 0);
0210 
0211     /* Fill in the 'dev' fields. */
0212     dev->base_addr = ioaddr;
0213     dev->mem_start = (unsigned long)
0214         nubus_slot_addr(slot) | (((slot&0xf) << 20) + MMIOBASE);
0215     dev->mem_end = dev->mem_start + 0x1000;
0216 
0217     /* Turn on shared memory */
0218     writereg_io(dev, PP_BusCTL, MEMORY_ON);
0219 
0220     /* get the chip type */
0221     rev_type = readreg(dev, PRODUCT_ID_ADD);
0222     lp->chip_type = rev_type &~ REVISON_BITS;
0223     lp->chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
0224 
0225     /* Check the chip type and revision in order to set the correct send command
0226     CS8920 revision C and CS8900 revision F can use the faster send. */
0227     lp->send_cmd = TX_AFTER_381;
0228     if (lp->chip_type == CS8900 && lp->chip_revision >= 'F')
0229         lp->send_cmd = TX_NOW;
0230     if (lp->chip_type != CS8900 && lp->chip_revision >= 'C')
0231         lp->send_cmd = TX_NOW;
0232 
0233     netif_dbg(lp, drv, dev, "%s", version);
0234 
0235     pr_info("cs89%c0%s rev %c found at %#8lx\n",
0236         lp->chip_type == CS8900 ? '0' : '2',
0237         lp->chip_type == CS8920M ? "M" : "",
0238         lp->chip_revision, dev->base_addr);
0239 
0240     /* Try to read the MAC address */
0241     if ((readreg(dev, PP_SelfST) & (EEPROM_PRESENT | EEPROM_OK)) == 0) {
0242         pr_info("No EEPROM, giving up now.\n");
0243         goto out1;
0244         } else {
0245         u8 addr[ETH_ALEN];
0246 
0247                 for (i = 0; i < ETH_ALEN; i += 2) {
0248             /* Big-endian (why??!) */
0249             unsigned short s = readreg(dev, PP_IA + i);
0250             addr[i] = s >> 8;
0251             addr[i+1] = s & 0xff;
0252                 }
0253         eth_hw_addr_set(dev, addr);
0254         }
0255 
0256     dev->irq = SLOT2IRQ(slot);
0257 
0258     /* print the IRQ and ethernet address. */
0259 
0260     pr_info("MAC %pM, IRQ %d\n", dev->dev_addr, dev->irq);
0261 
0262     dev->netdev_ops     = &mac89x0_netdev_ops;
0263 
0264     err = register_netdev(dev);
0265     if (err)
0266         goto out1;
0267 
0268     platform_set_drvdata(pdev, dev);
0269     return 0;
0270 out1:
0271     nubus_writew(0, dev->base_addr + ADD_PORT);
0272 out:
0273     free_netdev(dev);
0274     return err;
0275 }
0276 
0277 /* Open/initialize the board.  This is called (in the current kernel)
0278    sometime after booting when the 'ifconfig' program is run.
0279 
0280    This routine should set everything up anew at each open, even
0281    registers that "should" only need to be set once at boot, so that
0282    there is non-reboot way to recover if something goes wrong.
0283    */
0284 static int
0285 net_open(struct net_device *dev)
0286 {
0287     struct net_local *lp = netdev_priv(dev);
0288     int i;
0289 
0290     /* Disable the interrupt for now */
0291     writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL) & ~ENABLE_IRQ);
0292 
0293     /* Grab the interrupt */
0294     if (request_irq(dev->irq, net_interrupt, 0, "cs89x0", dev))
0295         return -EAGAIN;
0296 
0297     /* Set up the IRQ - Apparently magic */
0298     if (lp->chip_type == CS8900)
0299         writereg(dev, PP_CS8900_ISAINT, 0);
0300     else
0301         writereg(dev, PP_CS8920_ISAINT, 0);
0302 
0303     /* set the Ethernet address */
0304     for (i=0; i < ETH_ALEN/2; i++)
0305         writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
0306 
0307     /* Turn on both receive and transmit operations */
0308     writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
0309 
0310     /* Receive only error free packets addressed to this card */
0311     lp->rx_mode = 0;
0312     writereg(dev, PP_RxCTL, DEF_RX_ACCEPT);
0313 
0314     lp->curr_rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL;
0315 
0316     writereg(dev, PP_RxCFG, lp->curr_rx_cfg);
0317 
0318     writereg(dev, PP_TxCFG, TX_LOST_CRS_ENBL | TX_SQE_ERROR_ENBL | TX_OK_ENBL |
0319            TX_LATE_COL_ENBL | TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
0320 
0321     writereg(dev, PP_BufCFG, READY_FOR_TX_ENBL | RX_MISS_COUNT_OVRFLOW_ENBL |
0322          TX_COL_COUNT_OVRFLOW_ENBL | TX_UNDERRUN_ENBL);
0323 
0324     /* now that we've got our act together, enable everything */
0325     writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL) | ENABLE_IRQ);
0326     netif_start_queue(dev);
0327     return 0;
0328 }
0329 
0330 static netdev_tx_t
0331 net_send_packet(struct sk_buff *skb, struct net_device *dev)
0332 {
0333     struct net_local *lp = netdev_priv(dev);
0334     unsigned long flags;
0335 
0336     netif_dbg(lp, tx_queued, dev, "sent %d byte packet of type %x\n",
0337           skb->len, skb->data[ETH_ALEN + ETH_ALEN] << 8 |
0338           skb->data[ETH_ALEN + ETH_ALEN + 1]);
0339 
0340     /* keep the upload from being interrupted, since we
0341        ask the chip to start transmitting before the
0342        whole packet has been completely uploaded. */
0343     local_irq_save(flags);
0344     netif_stop_queue(dev);
0345 
0346     /* initiate a transmit sequence */
0347     writereg(dev, PP_TxCMD, lp->send_cmd);
0348     writereg(dev, PP_TxLength, skb->len);
0349 
0350     /* Test to see if the chip has allocated memory for the packet */
0351     if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
0352         /* Gasp!  It hasn't.  But that shouldn't happen since
0353            we're waiting for TxOk, so return 1 and requeue this packet. */
0354         local_irq_restore(flags);
0355         return NETDEV_TX_BUSY;
0356     }
0357 
0358     /* Write the contents of the packet */
0359     skb_copy_from_linear_data(skb, (void *)(dev->mem_start + PP_TxFrame),
0360                   skb->len+1);
0361 
0362     local_irq_restore(flags);
0363     dev_kfree_skb (skb);
0364 
0365     return NETDEV_TX_OK;
0366 }
0367 
0368 /* The typical workload of the driver:
0369    Handle the network interface interrupts. */
0370 static irqreturn_t net_interrupt(int irq, void *dev_id)
0371 {
0372     struct net_device *dev = dev_id;
0373     struct net_local *lp;
0374     int ioaddr, status;
0375 
0376     ioaddr = dev->base_addr;
0377     lp = netdev_priv(dev);
0378 
0379     /* we MUST read all the events out of the ISQ, otherwise we'll never
0380            get interrupted again.  As a consequence, we can't have any limit
0381            on the number of times we loop in the interrupt handler.  The
0382            hardware guarantees that eventually we'll run out of events.  Of
0383            course, if you're on a slow machine, and packets are arriving
0384            faster than you can read them off, you're screwed.  Hasta la
0385            vista, baby!  */
0386     while ((status = swab16(nubus_readw(dev->base_addr + ISQ_PORT)))) {
0387         netif_dbg(lp, intr, dev, "status=%04x\n", status);
0388         switch(status & ISQ_EVENT_MASK) {
0389         case ISQ_RECEIVER_EVENT:
0390             /* Got a packet(s). */
0391             net_rx(dev);
0392             break;
0393         case ISQ_TRANSMITTER_EVENT:
0394             dev->stats.tx_packets++;
0395             netif_wake_queue(dev);
0396             if ((status & TX_OK) == 0)
0397                 dev->stats.tx_errors++;
0398             if (status & TX_LOST_CRS)
0399                 dev->stats.tx_carrier_errors++;
0400             if (status & TX_SQE_ERROR)
0401                 dev->stats.tx_heartbeat_errors++;
0402             if (status & TX_LATE_COL)
0403                 dev->stats.tx_window_errors++;
0404             if (status & TX_16_COL)
0405                 dev->stats.tx_aborted_errors++;
0406             break;
0407         case ISQ_BUFFER_EVENT:
0408             if (status & READY_FOR_TX) {
0409                 /* we tried to transmit a packet earlier,
0410                                    but inexplicably ran out of buffers.
0411                                    That shouldn't happen since we only ever
0412                                    load one packet.  Shrug.  Do the right
0413                                    thing anyway. */
0414                 netif_wake_queue(dev);
0415             }
0416             if (status & TX_UNDERRUN) {
0417                 netif_dbg(lp, tx_err, dev, "transmit underrun\n");
0418                                 lp->send_underrun++;
0419                                 if (lp->send_underrun == 3) lp->send_cmd = TX_AFTER_381;
0420                                 else if (lp->send_underrun == 6) lp->send_cmd = TX_AFTER_ALL;
0421                         }
0422             break;
0423         case ISQ_RX_MISS_EVENT:
0424             dev->stats.rx_missed_errors += (status >> 6);
0425             break;
0426         case ISQ_TX_COL_EVENT:
0427             dev->stats.collisions += (status >> 6);
0428             break;
0429         }
0430     }
0431     return IRQ_HANDLED;
0432 }
0433 
0434 /* We have a good packet(s), get it/them out of the buffers. */
0435 static void
0436 net_rx(struct net_device *dev)
0437 {
0438     struct net_local *lp = netdev_priv(dev);
0439     struct sk_buff *skb;
0440     int status, length;
0441 
0442     status = readreg(dev, PP_RxStatus);
0443     if ((status & RX_OK) == 0) {
0444         dev->stats.rx_errors++;
0445         if (status & RX_RUNT)
0446                 dev->stats.rx_length_errors++;
0447         if (status & RX_EXTRA_DATA)
0448                 dev->stats.rx_length_errors++;
0449         if ((status & RX_CRC_ERROR) &&
0450             !(status & (RX_EXTRA_DATA|RX_RUNT)))
0451             /* per str 172 */
0452             dev->stats.rx_crc_errors++;
0453         if (status & RX_DRIBBLE)
0454                 dev->stats.rx_frame_errors++;
0455         return;
0456     }
0457 
0458     length = readreg(dev, PP_RxLength);
0459     /* Malloc up new buffer. */
0460     skb = alloc_skb(length, GFP_ATOMIC);
0461     if (skb == NULL) {
0462         dev->stats.rx_dropped++;
0463         return;
0464     }
0465     skb_put(skb, length);
0466 
0467     skb_copy_to_linear_data(skb, (void *)(dev->mem_start + PP_RxFrame),
0468                 length);
0469 
0470     netif_dbg(lp, rx_status, dev, "received %d byte packet of type %x\n",
0471           length, skb->data[ETH_ALEN + ETH_ALEN] << 8 |
0472           skb->data[ETH_ALEN + ETH_ALEN + 1]);
0473 
0474         skb->protocol=eth_type_trans(skb,dev);
0475     netif_rx(skb);
0476     dev->stats.rx_packets++;
0477     dev->stats.rx_bytes += length;
0478 }
0479 
0480 /* The inverse routine to net_open(). */
0481 static int
0482 net_close(struct net_device *dev)
0483 {
0484 
0485     writereg(dev, PP_RxCFG, 0);
0486     writereg(dev, PP_TxCFG, 0);
0487     writereg(dev, PP_BufCFG, 0);
0488     writereg(dev, PP_BusCTL, 0);
0489 
0490     netif_stop_queue(dev);
0491 
0492     free_irq(dev->irq, dev);
0493 
0494     /* Update the statistics here. */
0495 
0496     return 0;
0497 
0498 }
0499 
0500 /* Get the current statistics.  This may be called with the card open or
0501    closed. */
0502 static struct net_device_stats *
0503 net_get_stats(struct net_device *dev)
0504 {
0505     unsigned long flags;
0506 
0507     local_irq_save(flags);
0508     /* Update the statistics from the device registers. */
0509     dev->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6);
0510     dev->stats.collisions += (readreg(dev, PP_TxCol) >> 6);
0511     local_irq_restore(flags);
0512 
0513     return &dev->stats;
0514 }
0515 
0516 static void set_multicast_list(struct net_device *dev)
0517 {
0518     struct net_local *lp = netdev_priv(dev);
0519 
0520     if(dev->flags&IFF_PROMISC)
0521     {
0522         lp->rx_mode = RX_ALL_ACCEPT;
0523     } else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev)) {
0524         /* The multicast-accept list is initialized to accept-all, and we
0525            rely on higher-level filtering for now. */
0526         lp->rx_mode = RX_MULTCAST_ACCEPT;
0527     }
0528     else
0529         lp->rx_mode = 0;
0530 
0531     writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode);
0532 
0533     /* in promiscuous mode, we accept errored packets, so we have to enable interrupts on them also */
0534     writereg(dev, PP_RxCFG, lp->curr_rx_cfg |
0535          (lp->rx_mode == RX_ALL_ACCEPT? (RX_CRC_ERROR_ENBL|RX_RUNT_ENBL|RX_EXTRA_DATA_ENBL) : 0));
0536 }
0537 
0538 
0539 static int set_mac_address(struct net_device *dev, void *addr)
0540 {
0541     struct sockaddr *saddr = addr;
0542     int i;
0543 
0544     if (!is_valid_ether_addr(saddr->sa_data))
0545         return -EADDRNOTAVAIL;
0546 
0547     eth_hw_addr_set(dev, saddr->sa_data);
0548     netdev_info(dev, "Setting MAC address to %pM\n", dev->dev_addr);
0549 
0550     /* set the Ethernet address */
0551     for (i=0; i < ETH_ALEN/2; i++)
0552         writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
0553 
0554     return 0;
0555 }
0556 
0557 MODULE_LICENSE("GPL");
0558 
0559 static int mac89x0_device_remove(struct platform_device *pdev)
0560 {
0561     struct net_device *dev = platform_get_drvdata(pdev);
0562 
0563     unregister_netdev(dev);
0564     nubus_writew(0, dev->base_addr + ADD_PORT);
0565     free_netdev(dev);
0566     return 0;
0567 }
0568 
0569 static struct platform_driver mac89x0_platform_driver = {
0570     .probe = mac89x0_device_probe,
0571     .remove = mac89x0_device_remove,
0572     .driver = {
0573         .name = "mac89x0",
0574     },
0575 };
0576 
0577 module_platform_driver(mac89x0_platform_driver);