sk_mca.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,225 行 · 第 1/2 页
C
1,225 行
/* net-3-driver for the SKNET MCA-based cardsThis is an extension to the Linux operating system, and is covered by thesame GNU General Public License that covers that work.Copyright 1999 by Alfred Arnold (alfred@ccac.rwth-aachen.de, alfred.arnold@lancom.de)This driver is based both on the 3C523 driver and the SK_G16 driver.paper sources: 'PC Hardware: Aufbau, Funktionsweise, Programmierung' by Hans-Peter Messmer for the basic Microchannel stuff 'Linux Geraetetreiber' by Allesandro Rubini, Kalle Dalheimer for help on Ethernet driver programming 'Ethernet/IEEE 802.3 Family 1992 World Network Data Book/Handbook' by AMD for documentation on the AM7990 LANCE 'SKNET Personal Technisches Manual', Version 1.2 by Schneider&Koch for documentation on the Junior board 'SK-NET MC2+ Technical Manual", Version 1.1 by Schneider&Koch for documentation on the MC2 bord A big thank you to the S&K support for providing me so quickly with documentation! Also see http://www.syskonnect.com/ Missing things: -> set debug level via ioctl instead of compile-time switches -> I didn't follow the development of the 2.1.x kernels, so my assumptions about which things changed with which kernel version are probably nonsenseHistory: May 16th, 1999 startup May 22st, 1999 added private structure, methods begun building data structures in RAM May 23nd, 1999 can receive frames, send frames May 24th, 1999 modularized initialization of LANCE loadable as module still Tx problem :-( May 26th, 1999 MC2 works support for multiple devices display media type for MC2+ May 28th, 1999 fixed problem in GetLANCE leaving interrupts turned off increase TX queue to 4 packets to improve send performance May 29th, 1999 a few corrections in statistics, caught rcvr overruns reinitialization of LANCE/board in critical situations MCA info implemented implemented LANCE multicast filter Jun 6th, 1999 additions for Linux 2.2 Dec 25th, 1999 unfortunately there seem to be newer MC2+ boards that react on IRQ 3/5/9/10 instead of 3/5/10/11, so we have to autoprobe in questionable cases... Dec 28th, 1999 integrated patches from David Weinehall & Bill Wendling for 2.3 kernels (isa_...functions). Things are defined in a way that it still works with 2.0.x 8-) Dec 30th, 1999 added handling of the remaining interrupt conditions. That should cure the spurious hangs. Jan 30th, 2000 newer kernels automatically probe more than one board, so the 'startslot' as a variable is also needed here June 1st, 2000 added changes for recent 2.3 kernels *************************************************************************/#include <linux/kernel.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/ioport.h>#include <linux/slab.h>#include <linux/interrupt.h>#include <linux/delay.h>#include <linux/time.h>#include <linux/mca-legacy.h>#include <linux/init.h>#include <linux/module.h>#include <linux/version.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <asm/processor.h>#include <asm/bitops.h>#include <asm/io.h>#define _SK_MCA_DRIVER_#include "sk_mca.h"/* ------------------------------------------------------------------------ * global static data - not more since we can handle multiple boards and * have to pack all state info into the device struct! * ------------------------------------------------------------------------ */static char *MediaNames[Media_Count] = { "10Base2", "10BaseT", "10Base5", "Unknown" };static unsigned char poly[] = { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0};/* ------------------------------------------------------------------------ * private subfunctions * ------------------------------------------------------------------------ *//* dump parts of shared memory - only needed during debugging */#ifdef DEBUGstatic void dumpmem(struct net_device *dev, u32 start, u32 len){ int z; for (z = 0; z < len; z++) { if ((z & 15) == 0) printk("%04x:", z); printk(" %02x", SKMCA_READB(dev->mem_start + start + z)); if ((z & 15) == 15) printk("\n"); }}/* print exact time - ditto */static void PrTime(void){ struct timeval tv; do_gettimeofday(&tv); printk("%9d:%06d: ", tv.tv_sec, tv.tv_usec);}#endif/* deduce resources out of POS registers */static void __init getaddrs(int slot, int junior, int *base, int *irq, skmca_medium * medium){ u_char pos0, pos1, pos2; if (junior) { pos0 = mca_read_stored_pos(slot, 2); *base = ((pos0 & 0x0e) << 13) + 0xc0000; *irq = ((pos0 & 0x10) >> 4) + 10; *medium = Media_Unknown; } else { /* reset POS 104 Bits 0+1 so the shared memory region goes to the configured area between 640K and 1M. Afterwards, enable the MC2. I really don't know what rode SK to do this... */ mca_write_pos(slot, 4, mca_read_stored_pos(slot, 4) & 0xfc); mca_write_pos(slot, 2, mca_read_stored_pos(slot, 2) | 0x01); pos1 = mca_read_stored_pos(slot, 3); pos2 = mca_read_stored_pos(slot, 4); *base = ((pos1 & 0x07) << 14) + 0xc0000; switch (pos2 & 0x0c) { case 0: *irq = 3; break; case 4: *irq = 5; break; case 8: *irq = -10; break; case 12: *irq = -11; break; } *medium = (pos2 >> 6) & 3; }}/* check for both cards: When the MC2 is turned off, it was configured for more than 15MB RAM, is disabled and won't get detected using the standard probe. We therefore have to scan the slots manually :-( */static int __init dofind(int *junior, int firstslot){ int slot; unsigned int id; for (slot = firstslot; slot < MCA_MAX_SLOT_NR; slot++) { id = mca_read_stored_pos(slot, 0) + (((unsigned int) mca_read_stored_pos(slot, 1)) << 8); *junior = 0; if (id == SKNET_MCA_ID) return slot; *junior = 1; if (id == SKNET_JUNIOR_MCA_ID) return slot; } return MCA_NOTFOUND;}/* reset the whole board */static void ResetBoard(struct net_device *dev){ skmca_priv *priv = (skmca_priv *) dev->priv; SKMCA_WRITEB(CTRL_RESET_ON, priv->ctrladdr); udelay(10); SKMCA_WRITEB(CTRL_RESET_OFF, priv->ctrladdr);}/* wait for LANCE interface to become not busy */static int WaitLANCE(struct net_device *dev){ skmca_priv *priv = (skmca_priv *) dev->priv; int t = 0; while ((SKMCA_READB(priv->ctrladdr) & STAT_IO_BUSY) == STAT_IO_BUSY) { udelay(1); if (++t > 1000) { printk("%s: LANCE access timeout", dev->name); return 0; } } return 1;}/* set LANCE register - must be atomic */static void SetLANCE(struct net_device *dev, u16 addr, u16 value){ skmca_priv *priv = (skmca_priv *) dev->priv; unsigned long flags; /* disable interrupts */ spin_lock_irqsave(&priv->lock, flags); /* wait until no transfer is pending */ WaitLANCE(dev); /* transfer register address to RAP */ SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP, priv->ctrladdr); SKMCA_WRITEW(addr, priv->ioregaddr); SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr); udelay(1); WaitLANCE(dev); /* transfer data to register */ SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_DATA, priv->ctrladdr); SKMCA_WRITEW(value, priv->ioregaddr); SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr); udelay(1); WaitLANCE(dev); /* reenable interrupts */ spin_unlock_irqrestore(&priv->lock, flags);}/* get LANCE register */static u16 GetLANCE(struct net_device *dev, u16 addr){ skmca_priv *priv = (skmca_priv *) dev->priv; unsigned long flags; unsigned int res; /* disable interrupts */ spin_lock_irqsave(&priv->lock, flags); /* wait until no transfer is pending */ WaitLANCE(dev); /* transfer register address to RAP */ SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP, priv->ctrladdr); SKMCA_WRITEW(addr, priv->ioregaddr); SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr); udelay(1); WaitLANCE(dev); /* transfer data from register */ SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_READ | CTRL_ADR_DATA, priv->ctrladdr); SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr); udelay(1); WaitLANCE(dev); res = SKMCA_READW(priv->ioregaddr); /* reenable interrupts */ spin_unlock_irqrestore(&priv->lock, flags); return res;}/* build up descriptors in shared RAM */static void InitDscrs(struct net_device *dev){ u32 bufaddr; /* Set up Tx descriptors. The board has only 16K RAM so bits 16..23 are always 0. */ bufaddr = RAM_DATABASE; { LANCE_TxDescr descr; int z; for (z = 0; z < TXCOUNT; z++) { descr.LowAddr = bufaddr; descr.Flags = 0; descr.Len = 0xf000; descr.Status = 0; SKMCA_TOIO(dev->mem_start + RAM_TXBASE + (z * sizeof(LANCE_TxDescr)), &descr, sizeof(LANCE_TxDescr)); SKMCA_SETIO(dev->mem_start + bufaddr, 0, RAM_BUFSIZE); bufaddr += RAM_BUFSIZE; } } /* do the same for the Rx descriptors */ { LANCE_RxDescr descr; int z; for (z = 0; z < RXCOUNT; z++) { descr.LowAddr = bufaddr; descr.Flags = RXDSCR_FLAGS_OWN; descr.MaxLen = -RAM_BUFSIZE; descr.Len = 0; SKMCA_TOIO(dev->mem_start + RAM_RXBASE + (z * sizeof(LANCE_RxDescr)), &descr, sizeof(LANCE_RxDescr)); SKMCA_SETIO(dev->mem_start + bufaddr, 0, RAM_BUFSIZE); bufaddr += RAM_BUFSIZE; } }}/* calculate the hash bit position for a given multicast address taken more or less directly from the AMD datasheet... */static void UpdateCRC(unsigned char *CRC, int bit){ int j; /* shift CRC one bit */ memmove(CRC + 1, CRC, 32 * sizeof(unsigned char)); CRC[0] = 0; /* if bit XOR controlbit = 1, set CRC = CRC XOR polynomial */ if (bit ^ CRC[32]) for (j = 0; j < 32; j++) CRC[j] ^= poly[j];}static unsigned int GetHash(char *address){ unsigned char CRC[33]; int i, byte, hashcode; /* a multicast address has bit 0 in the first byte set */ if ((address[0] & 1) == 0) return -1; /* initialize CRC */ memset(CRC, 1, sizeof(CRC)); /* loop through address bits */ for (byte = 0; byte < 6; byte++) for (i = 0; i < 8; i++) UpdateCRC(CRC, (address[byte] >> i) & 1); /* hashcode is the 6 least significant bits of the CRC */ hashcode = 0; for (i = 0; i < 6; i++) hashcode = (hashcode << 1) + CRC[i]; return hashcode;}/* feed ready-built initialization block into LANCE */static void InitLANCE(struct net_device *dev){ skmca_priv *priv = (skmca_priv *) dev->priv; /* build up descriptors. */ InitDscrs(dev); /* next RX descriptor to be read is the first one. Since the LANCE will start from the beginning after initialization, we have to reset out pointers too. */ priv->nextrx = 0; /* no TX descriptors active */ priv->nexttxput = priv->nexttxdone = priv->txbusy = 0; /* set up the LANCE bus control register - constant for SKnet boards */ SetLANCE(dev, LANCE_CSR3, CSR3_BSWAP_OFF | CSR3_ALE_LOW | CSR3_BCON_HOLD); /* write address of initialization block into LANCE */ SetLANCE(dev, LANCE_CSR1, RAM_INITBASE & 0xffff); SetLANCE(dev, LANCE_CSR2, (RAM_INITBASE >> 16) & 0xff); /* we don't get ready until the LANCE has read the init block */ netif_stop_queue(dev); /* let LANCE read the initialization block. LANCE is ready when we receive the corresponding interrupt. */ SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_INIT);}/* stop the LANCE so we can reinitialize it */static void StopLANCE(struct net_device *dev){ /* can't take frames any more */ netif_stop_queue(dev); /* disable interrupts, stop it */ SetLANCE(dev, LANCE_CSR0, CSR0_STOP);}/* initialize card and LANCE for proper operation */static void InitBoard(struct net_device *dev){ LANCE_InitBlock block; /* Lay out the shared RAM - first we create the init block for the LANCE. We do not overwrite it later because we need it again when we switch promiscous mode on/off. */ block.Mode = 0; if (dev->flags & IFF_PROMISC) block.Mode |= LANCE_INIT_PROM; memcpy(block.PAdr, dev->dev_addr, 6); memset(block.LAdrF, 0, sizeof(block.LAdrF)); block.RdrP = (RAM_RXBASE & 0xffffff) | (LRXCOUNT << 29); block.TdrP = (RAM_TXBASE & 0xffffff) | (LTXCOUNT << 29); SKMCA_TOIO(dev->mem_start + RAM_INITBASE, &block, sizeof(block)); /* initialize LANCE. Implicitly sets up other structures in RAM. */ InitLANCE(dev);}/* deinitialize card and LANCE */static void DeinitBoard(struct net_device *dev){ /* stop LANCE */ StopLANCE(dev); /* reset board */ ResetBoard(dev);}/* probe for device's irq */static int __init ProbeIRQ(struct net_device *dev){ unsigned long imaskval, njiffies, irq; u16 csr0val; /* enable all interrupts */ imaskval = probe_irq_on(); /* initialize the board. Wait for interrupt 'Initialization done'. */ ResetBoard(dev); InitBoard(dev); njiffies = jiffies + HZ; do { csr0val = GetLANCE(dev, LANCE_CSR0); } while (((csr0val & CSR0_IDON) == 0) && (jiffies != njiffies)); /* turn of interrupts again */ irq = probe_irq_off(imaskval); /* if we found something, ack the interrupt */ if (irq) SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_IDON); /* back to idle state */ DeinitBoard(dev); return irq;}/* ------------------------------------------------------------------------ * interrupt handler(s) * ------------------------------------------------------------------------ *//* LANCE has read initialization block -> start it */static u16 irqstart_handler(struct net_device *dev, u16 oldcsr0){ /* now we're ready to transmit */ netif_wake_queue(dev); /* reset IDON bit, start LANCE */ SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_IDON | CSR0_STRT); return GetLANCE(dev, LANCE_CSR0);}/* did we lose blocks due to a FIFO overrun ? */static u16 irqmiss_handler(struct net_device *dev, u16 oldcsr0){ skmca_priv *priv = (skmca_priv *) dev->priv; /* update statistics */ priv->stat.rx_fifo_errors++; /* reset MISS bit */ SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_MISS); return GetLANCE(dev, LANCE_CSR0);}/* receive interrupt */static u16 irqrx_handler(struct net_device *dev, u16 oldcsr0){ skmca_priv *priv = (skmca_priv *) dev->priv; LANCE_RxDescr descr; unsigned int descraddr; /* run through queue until we reach a descriptor we do not own */ descraddr = RAM_RXBASE + (priv->nextrx * sizeof(LANCE_RxDescr)); while (1) { /* read descriptor */ SKMCA_FROMIO(&descr, dev->mem_start + descraddr, sizeof(LANCE_RxDescr)); /* if we reach a descriptor we do not own, we're done */ if ((descr.Flags & RXDSCR_FLAGS_OWN) != 0) break;#ifdef DEBUG PrTime(); printk("Receive packet on descr %d len %d\n", priv->nextrx, descr.Len);#endif /* erroneous packet ? */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?