注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

fancye的博客

 
 
 

日志

 
 

LAN921x驱动 for linux 2.4 part2  

2009-11-11 07:52:25|  分类: 嵌入式 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
来源:http://blog.chinaunix.net/u/3063/showart_265202.html

/* Increments the Rx error counters */
static void
smsc911x_rx_counterrors(struct smsc911x_data *pdata, unsigned int rxstat)
{
int crc_err = 0;

if (unlikely(rxstat & 0x00008000)) {
pdata->stats.rx_errors++;
if (unlikely(rxstat & 0x00000002)) {
pdata->stats.rx_crc_errors++;
crc_err = 1;
}
}
if (likely(!crc_err)) {
if (unlikely((rxstat & 0x00001020) == 0x00001020)) {
/* Frame type indicates length,
* and length error is set */
pdata->stats.rx_length_errors++;
}
if (rxstat & RX_STS_MCAST_)
pdata->stats.multicast++;
}
}

/* Quickly dumps bad packets */
static void
smsc911x_rx_fastforward(struct smsc911x_data *pdata, unsigned int count)
{
unsigned int temp;

if (likely(count >= 4)) {
unsigned int timeout = 500;
unsigned int val;
smsc911x_reg_write(RX_DP_CTRL_RX_FFWD_, pdata, RX_DP_CTRL);
do {
udelay(1);
val = smsc911x_reg_read(pdata, RX_DP_CTRL);
} while (timeout-- && (val & RX_DP_CTRL_RX_FFWD_));

if (unlikely(timeout == 0))
SMSC_WARNING("Timed out waiting for RX FFWD "
"to finish, RX_DP_CTRL: 0x%08X", val);
} else {
while (count--)
temp = smsc911x_reg_read(pdata, RX_DATA_FIFO);
}
}

/* NAPI poll function */
static int smsc911x_poll(struct net_device *dev, int *budget)
{
struct smsc911x_data *pdata = netdev_priv(dev);
int npackets = 0;
int quota = min(dev->quota, *budget);

while (npackets < quota) {
unsigned int pktlength;
unsigned int pktwords;
unsigned int rxstat = smsc911x_rx_get_rxstatus(pdata);

/* break out of while loop if there are no more packets waiting */
if (!rxstat)
break;

pktlength = ((rxstat & 0x3FFF0000) >> 16);
pktwords = (pktlength + NET_IP_ALIGN + 3) >> 2;
smsc911x_rx_counterrors(pdata, rxstat);

if (likely((rxstat & RX_STS_ES_) == 0)) {
struct sk_buff *skb;
skb = dev_alloc_skb(pktlength + NET_IP_ALIGN);
if (likely(skb)) {
skb->data = skb->head;
skb->tail = skb->head;
/* Align IP on 16B boundary */
skb_reserve(skb, NET_IP_ALIGN);
skb_put(skb, pktlength - 4);
smsc911x_rx_readfifo(pdata,
(unsigned int *)skb->head,
pktwords);
skb->dev = dev;
skb->protocol = eth_type_trans(skb, dev);
skb->ip_summed = CHECKSUM_NONE;
netif_receive_skb(skb);

/* Update counters */
pdata->stats.rx_packets++;
pdata->stats.rx_bytes += (pktlength - 4);
dev->last_rx = jiffies;
npackets++;
continue;
} else {
SMSC_WARNING("Unable to allocate sk_buff "
"for rx packet, in PIO path");
pdata->stats.rx_dropped++;
}
}
/* At this point, the packet is to be read out
* of the fifo and discarded */
smsc911x_rx_fastforward(pdata, pktwords);
}

pdata->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
smsc911x_reg_write(INT_STS_RSFL_, pdata, INT_STS);

*budget -= npackets;
dev->quota -= npackets;

if (npackets < quota) {
unsigned int temp;
/* We processed all packets available.  Tell NAPI it can
* stop polling then re-enable rx interrupts */
netif_rx_complete(dev);
temp = smsc911x_reg_read(pdata, INT_EN);
temp |= INT_EN_RSFL_EN_;
smsc911x_reg_write(temp, pdata, INT_EN);
return 0;
}

/* There are still packets waiting */
return 1;
}

/* Returns hash bit number for given MAC address
* Example:
* 01 00 5E 00 00 01 -> returns bit number 31 */
static unsigned int smsc911x_hash(char addr[ETH_ALEN])
{
unsigned int crc;
unsigned int result;

crc = ether_crc(ETH_ALEN, addr);
result = (crc >> 26) & 0x3f;

return result;
}

static void smsc911x_rx_multicast_update(struct smsc911x_data *pdata)
{
/* Performs the multicast & mac_cr update.  This is called when
* safe on the current hardware, and with the phy_lock held */
unsigned int mac_cr = smsc911x_mac_read(pdata, MAC_CR);
mac_cr |= pdata->set_bits_mask;
mac_cr &= ~(pdata->clear_bits_mask);
smsc911x_mac_write(pdata, MAC_CR, mac_cr);
smsc911x_mac_write(pdata, HASHH, pdata->hashhi);
smsc911x_mac_write(pdata, HASHL, pdata->hashlo);
SMSC_TRACE("maccr 0x%08X, HASHH 0x%08X, HASHL 0x%08X", mac_cr,
pdata->hashhi, pdata->hashlo);
}

static void smsc911x_rx_multicast_update_workaround(struct smsc911x_data *pdata)
{
unsigned int mac_cr;

/* This function is only called for older LAN911x devices
* (revA or revB), where MAC_CR, HASHH and HASHL should not
* be modified during Rx - newer devices immediately update the
* registers.
*
* This is called from interrupt context */

spin_lock(&pdata->phy_lock);

/* Check Rx has stopped */
if (smsc911x_mac_read(pdata, MAC_CR) & MAC_CR_RXEN_)
SMSC_WARNING("Rx not stopped\n");

/* Perform the update - safe to do now Rx has stopped */
smsc911x_rx_multicast_update(pdata);

/* Re-enable Rx */
mac_cr = smsc911x_mac_read(pdata, MAC_CR);
mac_cr |= MAC_CR_RXEN_;
smsc911x_mac_write(pdata, MAC_CR, mac_cr);

pdata->multicast_update_pending = 0;

spin_unlock(&pdata->phy_lock);
}

static int smsc911x_soft_reset(struct smsc911x_data *pdata)
{
unsigned int timeout;
unsigned int temp;

/* Reset the LAN911x */
smsc911x_reg_write(HW_CFG_SRST_, pdata, HW_CFG);
timeout = 10;
do {
udelay(10);
temp = smsc911x_reg_read(pdata, HW_CFG);
} while ((--timeout) && (temp & HW_CFG_SRST_));

if (unlikely(temp & HW_CFG_SRST_)) {
SMSC_WARNING("Failed to complete reset");
return -ENODEV;
}
return 0;
}

static int smsc911x_open(struct net_device *dev)
{
struct smsc911x_data *pdata = netdev_priv(dev);
unsigned int mac_high16;
unsigned int mac_low32;
unsigned int timeout;
unsigned int temp;

spin_lock_init(&pdata->phy_lock);

/* Reset the LAN911x */
if (smsc911x_soft_reset(pdata))
return -ENODEV;

smsc911x_reg_write(0x00050000, pdata, HW_CFG);
smsc911x_reg_write(0x006E3740, pdata, AFC_CFG);

/* Make sure EEPROM has finished loading before setting GPIO_CFG */
timeout = 50;
while ((timeout--) &&
(smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_)) {
udelay(10);
}

if (unlikely(timeout == 0)) {
SMSC_WARNING("Timed out waiting for EEPROM "
"busy bit to clear\n");
}
#if USE_DEBUG >= 1
smsc911x_reg_write(0x00670700, pdata, GPIO_CFG);
#else
smsc911x_reg_write(0x70070000, pdata, GPIO_CFG);
#endif

/* Initialise irqs, but leave all sources disabled */
smsc911x_reg_write(0, pdata, INT_EN);
smsc911x_reg_write(0xFFFFFFFF, pdata, INT_STS);
/* Set interrupt deassertion to 100uS */
smsc911x_reg_write(((10 << 24) | INT_CFG_IRQ_EN_), pdata, INT_CFG);

/*
* intcfg |= INT_CFG_IRQ_POL_;  use this to set IRQ_POL bit
* intcfg |= INT_CFG_IRQ_TYPE_;  use this to set IRQ_TYPE bit
*/

SMSC_TRACE("Testing irq handler using IRQ %d", dev->irq);
pdata->software_irq_signal = 0;
smp_wmb();

temp = smsc911x_reg_read(pdata, INT_EN);
temp |= INT_EN_SW_INT_EN_;
smsc911x_reg_write(temp, pdata, INT_EN);

timeout = 1000;
while (timeout--) {
smp_rmb();
if (pdata->software_irq_signal)
break;
//msleep(1);
mdelay(1);
}

if (!pdata->software_irq_signal) {
printk(KERN_WARNING "%s: ISR failed signaling test (IRQ %d)\n",
dev->name, dev->irq);
return -ENODEV;
}
SMSC_TRACE("IRQ handler passed test using IRQ %d", dev->irq);

printk(KERN_INFO "%s: SMSC911x/921x identified at %#08lx, IRQ: %d\n",
dev->name, (unsigned long)pdata->ioaddr, dev->irq);

spin_lock_irq(&pdata->phy_lock);

/* Read mac address from EEPROM */
mac_high16 = smsc911x_mac_read(pdata, ADDRH);
mac_low32 = smsc911x_mac_read(pdata, ADDRL);

/* Generate random MAC address if eeprom values are invalid */
if ((mac_high16 == 0x0000FFFF) && (mac_low32 == 0xFFFFFFFF)) {
u8 random_mac[6];
random_ether_addr(random_mac);
mac_high16 = (random_mac[5] << 8) | random_mac[4];
mac_low32 = (random_mac[3] << 24) | (random_mac[2] << 16) |
(random_mac[1] << 8) | random_mac[0];

smsc911x_mac_write(pdata, ADDRH, mac_high16);
smsc911x_mac_write(pdata, ADDRL, mac_low32);
SMSC_TRACE("MAC Address is set to random_ether_addr");
} else {
SMSC_TRACE("Mac Address is read from LAN911x EEPROM");
}

spin_unlock_irq(&pdata->phy_lock);

dev->dev_addr[0] = (u8)(mac_low32);
dev->dev_addr[1] = (u8)(mac_low32 >> 8);
dev->dev_addr[2] = (u8)(mac_low32 >> 16);
dev->dev_addr[3] = (u8)(mac_low32 >> 24);
dev->dev_addr[4] = (u8)(mac_high16);
dev->dev_addr[5] = (u8)(mac_high16 >> 8);
printk(KERN_INFO
"%s: SMSC911x MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
dev->name, dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);

netif_carrier_off(dev);
if (!smsc911x_phy_initialise(dev)) {
SMSC_WARNING("Failed to initialize PHY");
return -ENODEV;
}

temp = smsc911x_reg_read(pdata, HW_CFG);
/* Preserve TX FIFO size and external PHY configuration */
temp &= (HW_CFG_TX_FIF_SZ_|0x00000FFF);
temp |= HW_CFG_SF_;
smsc911x_reg_write(temp, pdata, HW_CFG);

temp = smsc911x_reg_read(pdata, FIFO_INT);
temp |= FIFO_INT_TX_AVAIL_LEVEL_;
temp &= ~(FIFO_INT_RX_STS_LEVEL_);
smsc911x_reg_write(temp, pdata, FIFO_INT);

/* set RX Data offset to 2 bytes for alignment */
smsc911x_reg_write((2 << 8), pdata, RX_CFG);

temp = smsc911x_reg_read(pdata, INT_EN);
temp |= (INT_EN_TDFA_EN_ | INT_EN_RSFL_EN_ | INT_EN_PHY_INT_EN_);
smsc911x_reg_write(temp, pdata, INT_EN);

spin_lock_irq(&pdata->phy_lock);
temp = smsc911x_mac_read(pdata, MAC_CR);
temp |= (MAC_CR_TXEN_ | MAC_CR_RXEN_ | MAC_CR_HBDIS_);
smsc911x_mac_write(pdata, MAC_CR, temp);
spin_unlock_irq(&pdata->phy_lock);

smsc911x_reg_write(TX_CFG_TX_ON_, pdata, TX_CFG);

netif_start_queue(dev);
return 0;
}

/* Entry point for stopping the interface */
static int smsc911x_stop(struct net_device *dev)
{
struct smsc911x_data *pdata = netdev_priv(dev);

pdata->stop_link_poll = 1;
del_timer_sync(&pdata->link_poll_timer);

smsc911x_reg_write((smsc911x_reg_read(pdata, INT_CFG) &
(~INT_CFG_IRQ_EN_)), pdata, INT_CFG);
netif_stop_queue(dev);

/* At this point all Rx and Tx activity is stopped */
pdata->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
smsc911x_tx_update_txcounters(pdata);

SMSC_TRACE("<--Simp911x_stop");
return 0;
}

/* Entry point for transmitting a packet */
static int smsc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
struct smsc911x_data *pdata = netdev_priv(dev);
unsigned int freespace;
unsigned int tx_cmd_a;
unsigned int tx_cmd_b;
unsigned int temp;
u32 wrsz;
u32 bufp;

freespace = smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TDFREE_;

if (unlikely(freespace < TX_FIFO_LOW_THRESHOLD))
SMSC_WARNING("Tx data fifo low, space available: %d",
freespace);

/* Word alignment adjustment */
tx_cmd_a = ((((unsigned int)(skb->data)) & 0x03) << 16);
tx_cmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
tx_cmd_a |= (unsigned int)skb->len;

tx_cmd_b = ((unsigned int)skb->len) << 16;
tx_cmd_b |= (unsigned int)skb->len;

smsc911x_reg_write(tx_cmd_a, pdata, TX_DATA_FIFO);
smsc911x_reg_write(tx_cmd_b, pdata, TX_DATA_FIFO);

bufp = ((u32)skb->data) & 0xFFFFFFFC;
wrsz = (u32)skb->len + 3;
wrsz += ((u32)skb->data) & 0x3;
wrsz >>= 2;

smsc911x_tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
dev_kfree_skb(skb);
freespace -= (skb->len + 32);
dev->trans_start = jiffies;

if (unlikely(smsc911x_tx_get_txstatcount(pdata) >= 30))
smsc911x_tx_update_txcounters(pdata);

if (freespace < TX_FIFO_LOW_THRESHOLD) {
netif_stop_queue(dev);
temp = smsc911x_reg_read(pdata, FIFO_INT);
temp &= 0x00FFFFFF;
temp |= 0x32000000;
smsc911x_reg_write(temp, pdata, FIFO_INT);
}

return NETDEV_TX_OK;
}

/* Entry point for getting status counters */
static struct net_device_stats *smsc911x_get_stats(struct net_device *dev)
{
struct smsc911x_data *pdata = netdev_priv(dev);
smsc911x_tx_update_txcounters(pdata);
return &pdata->stats;
}

/* Entry point for setting addressing modes */
static void smsc911x_set_multicast_list(struct net_device *dev)
{
struct smsc911x_data *pdata = netdev_priv(dev);
unsigned long flags;

if (dev->flags & IFF_PROMISC) {
/* Enabling promiscuous mode */
pdata->set_bits_mask = MAC_CR_PRMS_;
pdata->clear_bits_mask = (MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
pdata->hashhi = 0;
pdata->hashlo = 0;
} else if (dev->flags & IFF_ALLMULTI) {
/* Enabling all multicast mode */
pdata->set_bits_mask = MAC_CR_MCPAS_;
pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_HPFILT_);
pdata->hashhi = 0;
pdata->hashlo = 0;
} else if (dev->mc_count > 0) {
/* Enabling specific multicast addresses */
unsigned int hash_high = 0;
unsigned int hash_low = 0;
unsigned int count = 0;
struct dev_mc_list *mc_list = dev->mc_list;

pdata->set_bits_mask = MAC_CR_HPFILT_;
pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_MCPAS_);

while (mc_list) {
count++;
if ((mc_list->dmi_addrlen) == ETH_ALEN) {
unsigned int bitnum =
smsc911x_hash(mc_list->dmi_addr);
unsigned int mask = 0x01 << (bitnum & 0x1F);
if (bitnum & 0x20)
hash_high |= mask;
else
hash_low |= mask;
} else {
SMSC_WARNING("dmi_addrlen != 6");
}
mc_list = mc_list->next;
}
if (count != (unsigned int)dev->mc_count)
SMSC_WARNING("mc_count != dev->mc_count");

pdata->hashhi = hash_high;
pdata->hashlo = hash_low;
} else {
/* Enabling local MAC address only */
pdata->set_bits_mask = 0;
pdata->clear_bits_mask =
(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
pdata->hashhi = 0;
pdata->hashlo = 0;
}

spin_lock_irqsave(&pdata->phy_lock, flags);

if (pdata->generation <= 1) {
/* Older hardware revision - cannot change these flags while
* receiving data */
if (pdata->multicast_update_pending == 0) {
unsigned int temp;
SMSC_TRACE("scheduling mcast update");
pdata->multicast_update_pending = 1;

/* Request the hardware to stop, then perform the
* update when we get an RX_STOP interrupt */
smsc911x_reg_write(INT_STS_RXSTOP_INT_, pdata, INT_STS);
temp = smsc911x_reg_read(pdata, INT_EN);
temp |= INT_EN_RXSTOP_INT_EN_;
smsc911x_reg_write(temp, pdata, INT_EN);

temp = smsc911x_mac_read(pdata, MAC_CR);
temp &= ~(MAC_CR_RXEN_);
smsc911x_mac_write(pdata, MAC_CR, temp);
} else {
/* There is another update pending, this should now
* use the newer values */
}
} else {
/* Newer hardware revision - can write immediately */
smsc911x_rx_multicast_update(pdata);
}

spin_unlock_irqrestore(&pdata->phy_lock, flags);
}

static void smsc911x_irqhandler(int irq, void *dev_id, struct pt_regs *regs)
{
struct net_device *dev = dev_id;
struct smsc911x_data *pdata = netdev_priv(dev);
unsigned int intsts;
unsigned int inten;
unsigned int temp;
int serviced = 0;

intsts = smsc911x_reg_read(pdata, INT_STS);
inten = smsc911x_reg_read(pdata, INT_EN);

if (unlikely(intsts & inten & INT_STS_SW_INT_)) {
temp = smsc911x_reg_read(pdata, INT_EN);
temp &= (~INT_EN_SW_INT_EN_);
smsc911x_reg_write(temp, pdata, INT_EN);
smsc911x_reg_write(INT_STS_SW_INT_, pdata, INT_STS);
pdata->software_irq_signal = 1;
smp_wmb();
serviced = 1;
}

if (unlikely(intsts & inten & INT_STS_RXSTOP_INT_)) {
/* Called when there is a multicast update scheduled and
* it is now safe to complete the update */
SMSC_TRACE("RX Stop interrupt");
temp = smsc911x_reg_read(pdata, INT_EN);
temp &= (~INT_EN_RXSTOP_INT_EN_);
smsc911x_reg_write(temp, pdata, INT_EN);
smsc911x_reg_write(INT_STS_RXSTOP_INT_, pdata, INT_STS);
smsc911x_rx_multicast_update_workaround(pdata);
serviced = 1;
}

if (intsts & inten & INT_STS_TDFA_) {
temp = smsc911x_reg_read(pdata, FIFO_INT);
temp |= FIFO_INT_TX_AVAIL_LEVEL_;
smsc911x_reg_write(temp, pdata, FIFO_INT);
smsc911x_reg_write(INT_STS_TDFA_, pdata, INT_STS);
netif_wake_queue(dev);
serviced = 1;
}

if (unlikely(intsts & inten & INT_STS_RXE_)) {
smsc911x_reg_write(INT_STS_RXE_, pdata, INT_STS);
serviced = 1;
}

if (likely(intsts & inten & INT_STS_RSFL_)) {
/* Disable Rx interrupts and schedule NAPI poll */
temp = smsc911x_reg_read(pdata, INT_EN);
temp &= (~INT_EN_RSFL_EN_);
smsc911x_reg_write(temp, pdata, INT_EN);
netif_rx_schedule(dev);
serviced = 1;
}

if (unlikely(intsts & inten & INT_STS_PHY_INT_)) {
smsc911x_reg_write(INT_STS_PHY_INT_, pdata, INT_STS);
spin_lock(&pdata->phy_lock);
temp = smsc911x_phy_read(pdata, MII_INTSTS);
spin_unlock(&pdata->phy_lock);
SMSC_TRACE("PHY interrupt, sts 0x%04X", (u16)temp);
smsc911x_phy_update_linkmode(dev, 0);
serviced = 1;
}
}

#ifdef CONFIG_NET_POLL_CONTROLLER
void smsc911x_poll_controller(struct net_device *dev)
{
disable_irq(dev->irq);
smsc911x_irqhandler(0, dev, NULL);
enable_irq(dev->irq);
}
#endif                /* CONFIG_NET_POLL_CONTROLLER */

/* Standard ioctls for mii-tool */
static int smsc911x_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
struct smsc911x_data *pdata = netdev_priv(dev);
struct mii_ioctl_data *data = if_mii(ifr);
unsigned long flags;

SMSC_TRACE("ioctl cmd 0x%x", cmd);
switch (cmd) {
case SIOCGMIIPHY:
data->phy_id = pdata->mii.phy_id;
return 0;
case SIOCGMIIREG:
spin_lock_irqsave(&pdata->phy_lock, flags);
data->val_out = smsc911x_phy_read(pdata, data->reg_num);
spin_unlock_irqrestore(&pdata->phy_lock, flags);
return 0;
case SIOCSMIIREG:
spin_lock_irqsave(&pdata->phy_lock, flags);
smsc911x_phy_write(pdata, data->reg_num, data->val_in);
spin_unlock_irqrestore(&pdata->phy_lock, flags);
return 0;
}

SMSC_TRACE("unsupported ioctl cmd");
return -1;
}

static int
smsc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
{
struct smsc911x_data *pdata = netdev_priv(dev);

cmd->maxtxpkt = 1;
cmd->maxrxpkt = 1;
return mii_ethtool_gset(&pdata->mii, cmd);
}

static int
smsc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
{
struct smsc911x_data *pdata = netdev_priv(dev);

return mii_ethtool_sset(&pdata->mii, cmd);
}
/*
static void smsc911x_ethtool_getdrvinfo(struct net_device *dev,
struct ethtool_drvinfo *info)
{
strncpy(info->driver, SMSC_CHIPNAME, sizeof(info->driver));
strncpy(info->version, SMSC_DRV_VERSION, sizeof(info->version));
strncpy(info->bus_info, dev->class_dev.dev->bus_id,
sizeof(info->bus_info));
}
*/
static int smsc911x_ethtool_nwayreset(struct net_device *dev)
{
struct smsc911x_data *pdata = netdev_priv(dev);

return mii_nway_restart(&pdata->mii);
}

static u32 smsc911x_ethtool_getmsglevel(struct net_device *dev)
{
struct smsc911x_data *pdata = netdev_priv(dev);
return pdata->msg_enable;
}

static void smsc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
{
struct smsc911x_data *pdata = netdev_priv(dev);
pdata->msg_enable = level;
}

static int smsc911x_ethtool_getregslen(struct net_device *dev)
{
return (((E2P_DATA - ID_REV) / 4) + 1 + (WUCSR - MAC_CR) + 1 + 32) *
sizeof(u32);
}

static void
smsc911x_ethtool_getregs(struct net_device *dev, struct ethtool_regs *regs,
void *buf)
{
struct smsc911x_data *pdata = netdev_priv(dev);
unsigned long flags;
unsigned int i;
unsigned int j = 0;
u32 *data = buf;

regs->version = pdata->idrev;
for (i = ID_REV; i <= E2P_DATA; i += (sizeof(u32)))
data[j++] = smsc911x_reg_read(pdata, i);

spin_lock_irqsave(&pdata->phy_lock, flags);
for (i = MAC_CR; i <= WUCSR; i++)
data[j++] = smsc911x_mac_read(pdata, i);
for (i = 0; i <= 31; i++)
data[j++] = smsc911x_phy_read(pdata, i);
spin_unlock_irqrestore(&pdata->phy_lock, flags);
}

static void smsc911x_eeprom_enable_access(struct smsc911x_data *pdata)
{
unsigned int temp;
temp = smsc911x_reg_read(pdata, GPIO_CFG);
temp &= ~GPIO_CFG_EEPR_EN_;
smsc911x_reg_write(temp, pdata, GPIO_CFG);
//msleep(1);
mdelay(1);
}

static int smsc911x_eeprom_send_cmd(struct smsc911x_data *pdata, u32 op)
{
int timeout = 100;
u32 e2cmd;

SMSC_TRACE("op 0x%08x", op);
if (smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
SMSC_WARNING("Busy at start");
return -EBUSY;
}

e2cmd = op | E2P_CMD_EPC_BUSY_;
smsc911x_reg_write(e2cmd, pdata, E2P_CMD);

do {
//msleep(1);
mdelay(1);
e2cmd = smsc911x_reg_read(pdata, E2P_CMD);
} while ((e2cmd & E2P_CMD_EPC_BUSY_) && (timeout--));

if (!timeout) {
SMSC_TRACE("TIMED OUT");
return -EAGAIN;
}

if (e2cmd & E2P_CMD_EPC_TIMEOUT_) {
SMSC_TRACE("Error occured during eeprom operation");
return -EINVAL;
}

return 0;
}

static int smsc911x_eeprom_read_location(struct smsc911x_data *pdata,
u8 address, u8 *data)
{
u32 op = E2P_CMD_EPC_CMD_READ_ | address;
int ret;

SMSC_TRACE("address 0x%x", address);
ret = smsc911x_eeprom_send_cmd(pdata, op);

if (!ret)
data[address] = smsc911x_reg_read(pdata, E2P_DATA);

return ret;
}

static int smsc911x_eeprom_write_location(struct smsc911x_data *pdata,
u8 address, u8 data)
{
u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
int ret;

SMSC_TRACE("address 0x%x, data 0x%x", address, data);
ret = smsc911x_eeprom_send_cmd(pdata, op);

if (!ret) {
op = E2P_CMD_EPC_CMD_WRITE_ | address;
smsc911x_reg_write((u32)data, pdata, E2P_DATA);
ret = smsc911x_eeprom_send_cmd(pdata, op);
}

return ret;
}

static int smsc911x_ethtool_get_eeprom_len(struct net_device *dev)
{
return SMSC911X_EEPROM_SIZE;
}

static int smsc911x_ethtool_get_eeprom(struct net_device *dev,
struct ethtool_eeprom *eeprom, u8 *data)
{
struct smsc911x_data *pdata = netdev_priv(dev);
u8 eeprom_data[SMSC911X_EEPROM_SIZE];
int len;
int i;

smsc911x_eeprom_enable_access(pdata);

len = min(eeprom->len, SMSC911X_EEPROM_SIZE);
for (i = 0; i < len; i++) {
int ret = smsc911x_eeprom_read_location(pdata, i, eeprom_data);
if (ret < 0) {
eeprom->len = 0;
return ret;
}
}

memcpy(data, &eeprom_data[eeprom->offset], len);
eeprom->len = len;
return 0;
}

static int smsc911x_ethtool_set_eeprom(struct net_device *dev,
struct ethtool_eeprom *eeprom, u8 *data)
{
int ret;
struct smsc911x_data *pdata = netdev_priv(dev);

smsc911x_eeprom_enable_access(pdata);
smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWEN_);
ret = smsc911x_eeprom_write_location(pdata, eeprom->offset, *data);
smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWDS_);

/* Single byte write, according to man page */
eeprom->len = 1;

return ret;
}
/*
static struct ethtool_ops smsc911x_ethtool_ops = {
.get_settings = smsc911x_ethtool_getsettings,
.set_settings = smsc911x_ethtool_setsettings,
.get_link = ethtool_op_get_link,
.get_drvinfo = smsc911x_ethtool_getdrvinfo,
.nway_reset = smsc911x_ethtool_nwayreset,
.get_msglevel = smsc911x_ethtool_getmsglevel,
.set_msglevel = smsc911x_ethtool_setmsglevel,
.get_regs_len = smsc911x_ethtool_getregslen,
.get_regs = smsc911x_ethtool_getregs,
.get_eeprom_len = smsc911x_ethtool_get_eeprom_len,
.get_eeprom = smsc911x_ethtool_get_eeprom,
.set_eeprom = smsc911x_ethtool_set_eeprom,
};
*/
/* Initializing private device structures */
static int smsc911x_init(struct net_device *dev)
{
struct smsc911x_data *pdata = netdev_priv(dev);

SMSC_TRACE("Driver Parameters:");
SMSC_TRACE("LAN base: 0x%08lX", (unsigned long)pdata->ioaddr);
SMSC_TRACE("IRQ: %d", dev->irq);
SMSC_TRACE("PHY will be autodetected.");

if (pdata->ioaddr == 0) {
SMSC_WARNING("pdata->ioaddr: 0x00000000");
return -ENODEV;
}

/* Default generation to zero (all workarounds apply) */
pdata->generation = 0;

pdata->idrev = smsc911x_reg_read(pdata, ID_REV);
if (((pdata->idrev >> 16) & 0xFFFF) == (pdata->idrev & 0xFFFF)) {
SMSC_WARNING("idrev top 16 bits equal to bottom 16 bits, "
"idrev: 0x%08X", pdata->idrev);
SMSC_TRACE("This may mean the chip is set for 32 bit while "
"the bus is reading as 16 bit");
return -ENODEV;
}
switch (pdata->idrev & 0xFFFF0000) {
case 0x01180000:
switch (pdata->idrev & 0x0000FFFF) {
case 0UL:
SMSC_TRACE("LAN9118 Beacon identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 0;
break;
case 1UL:
SMSC_TRACE
("LAN9118 Concord A0 identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 1;
break;
case 2UL:
SMSC_TRACE
("LAN9118 Concord A1 identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 2;
break;
default:
SMSC_TRACE
("LAN9118 Concord A1 identified (NEW), idrev: 0x%08X",
pdata->idrev);
pdata->generation = 2;
break;
}
break;

case 0x01170000:
switch (pdata->idrev & 0x0000FFFF) {
case 0UL:
SMSC_TRACE("LAN9117 Beacon identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 0;
break;
case 1UL:
SMSC_TRACE
("LAN9117 Concord A0 identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 1;
break;
case 2UL:
SMSC_TRACE
("LAN9117 Concord A1 identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 2;
break;
default:
SMSC_TRACE
("LAN9117 Concord A1 identified (NEW), idrev: 0x%08X",
pdata->idrev);
pdata->generation = 2;
break;
}
break;

case 0x01160000:
switch (pdata->idrev & 0x0000FFFF) {
case 0UL:
SMSC_WARNING("LAN911x not identified, idrev: 0x%08X",
pdata->idrev);
return -ENODEV;
case 1UL:
SMSC_TRACE
("LAN9116 Concord A0 identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 1;
break;
case 2UL:
SMSC_TRACE
("LAN9116 Concord A1 identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 2;
break;
default:
SMSC_TRACE
("LAN9116 Concord A1 identified (NEW), idrev: 0x%08X",
pdata->idrev);
pdata->generation = 2;
break;
}
break;

case 0x01150000:
switch (pdata->idrev & 0x0000FFFF) {
case 0UL:
SMSC_WARNING("LAN911x not identified, idrev: 0x%08X",
pdata->idrev);
return -ENODEV;
case 1UL:
SMSC_TRACE
("LAN9115 Concord A0 identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 1;
break;
case 2UL:
SMSC_TRACE
("LAN9115 Concord A1 identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 2;
break;
default:
SMSC_TRACE
("LAN9115 Concord A1 identified (NEW), idrev: 0x%08X",
pdata->idrev);
pdata->generation = 2;
break;
}
break;

case 0x118A0000:
switch (pdata->idrev & 0x0000FFFF) {
case 0UL:
SMSC_TRACE
("LAN9218 Boylston identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 3;
break;
default:
SMSC_TRACE
("LAN9218 Boylston identified (NEW), idrev: 0x%08X",
pdata->idrev);
pdata->generation = 3;
break;
}
break;

case 0x117A0000:
switch (pdata->idrev & 0x0000FFFF) {
case 0UL:
SMSC_TRACE
("LAN9217 Boylston identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 3;
break;
default:
SMSC_TRACE
("LAN9217 Boylston identified (NEW), idrev: 0x%08X",
pdata->idrev);
pdata->generation = 3;
break;
}
break;

case 0x116A0000:
switch (pdata->idrev & 0x0000FFFF) {
case 0UL:
SMSC_TRACE
("LAN9216 Boylston identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 3;
break;
default:
SMSC_TRACE
("LAN9216 Boylston identified (NEW), idrev: 0x%08X",
pdata->idrev);
pdata->generation = 3;
break;
}
break;

case 0x115A0000:
switch (pdata->idrev & 0x0000FFFF) {
case 0UL:
SMSC_TRACE
("LAN9215 Boylston identified, idrev: 0x%08X",
pdata->idrev);
pdata->generation = 3;
break;
default:
SMSC_TRACE
("LAN9215 Boylston identified (NEW), idrev: 0x%08X",
pdata->idrev);
pdata->generation = 3;
break;
}
break;

default:
SMSC_WARNING("LAN911x not identified, idrev: 0x%08X",
pdata->idrev);
return -ENODEV;
}

if (pdata->generation == 0)
SMSC_WARNING("This driver is not intended "
"for this chip revision");

/* Reset the LAN911x */
if (smsc911x_soft_reset(pdata))
return -ENODEV;

/* Disable all interrupt sources until we bring the device up */
smsc911x_reg_write(0, pdata, INT_EN);

ether_setup(dev);
dev->open = smsc911x_open;
dev->stop = smsc911x_stop;
dev->hard_start_xmit = smsc911x_hard_start_xmit;
dev->get_stats = smsc911x_get_stats;
dev->set_multicast_list = smsc911x_set_multicast_list;
dev->flags |= IFF_MULTICAST;
dev->do_ioctl = smsc911x_do_ioctl;
dev->poll = smsc911x_poll;
dev->weight = 64;
//dev->ethtool_ops = &smsc911x_ethtool_ops;
#ifdef CONFIG_NET_POLL_CONTROLLER
dev->poll_controller = smsc911x_poll_controller;
#endif                /* CONFIG_NET_POLL_CONTROLLER */

pdata->mii.phy_id_mask = 0x1f;
pdata->mii.reg_num_mask = 0x1f;
pdata->mii.force_media = 0;
pdata->mii.full_duplex = 0;
pdata->mii.dev = dev;
pdata->mii.mdio_read = smsc911x_mdio_read;
pdata->mii.mdio_write = smsc911x_mdio_write;

pdata->msg_enable = NETIF_MSG_LINK;

return 0;
}
/*
static int smsc911x_drv_remove(struct platform_device *pdev)
{
struct net_device *dev;
struct smsc911x_data *pdata;
struct resource *res;

dev = platform_get_drvdata(pdev);
BUG_ON(!dev);
pdata = netdev_priv(dev);
BUG_ON(!pdata);
BUG_ON(!pdata->ioaddr);

SMSC_TRACE("Stopping driver.");
platform_set_drvdata(pdev, NULL);
unregister_netdev(dev);
free_irq(dev->irq, dev);
res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
"smsc911x-memory");
if (!res)
platform_get_resource(pdev, IORESOURCE_MEM, 0);

release_mem_region(res->start, res->end - res->start);

iounmap(pdata->ioaddr);

free_netdev(dev);

return 0;
}
*/
int __init smsc911x_drv_probe(struct net_device *dev)
{
struct smsc911x_data *pdata;
int retval;

/* when not module
if ( strcmp("eth1" , dev->name) == 0  )
return -ENODEV;
dev = init_etherdev(dev, 0);
*/
if (!request_mem_region(LAN9215_IO_BASE, LAN9215_IO_SIZE, SMSC_CHIPNAME)) {
retval = -EBUSY;
goto out_0;
}
pdata->ioaddr = ioremap_nocache(LAN9215_IO_BASE, LAN9215_IO_SIZE);
if (pdata->ioaddr == NULL)
{
SMSC_WARNING("Error smsc911x base address invalid");
retval = -ENOMEM;
goto out_free_netdev_2;
}

/* HW ID detect */
if( smsc911x_reg_read( pdata, ID_REV ) == LAN9215_PHY_ID )
{
printk("OK,LAN9215 Found!!\n");
}

pdata = (void *)(kmalloc(sizeof(*pdata), GFP_KERNEL));
memset(pdata, 0, sizeof(struct smsc911x_data));
dev->priv = pdata;
if ((retval = smsc911x_init(dev)) < 0) // call ether_setup here
goto out_unmap_io_3;
SET_MODULE_OWNER(dev);
dev->irq = 0;
retval = request_irq(dev->irq, smsc911x_irqhandler, SA_INTERRUPT,
SMSC_CHIPNAME, dev);
if (retval) {
SMSC_WARNING("Unable to claim requested irq: %d", dev->irq);
goto out_unmap_io_3;
}

return 0;

out_unmap_io_3:
iounmap(pdata->ioaddr);
kree( pdata );
out_free_netdev_2:
release_mem_region(LAN9215_IO_BASE, LAN9215_IO_SIZE);
out_0:
kfree(dev);
return retval;
}
/*
static struct platform_driver smsc911x_driver = {
.probe = smsc911x_drv_probe,
.remove = smsc911x_drv_remove,
.suspend = 0,    
.resume = 0,
.driver = {.name = SMSC_CHIPNAME,},
};
*/

/* Entry point for loading the module */
int smsc911x_init_module(void)
{
int retval;

gl_dev = alloc_etherdev(sizeof(struct smsc911x_data));
if (!gl_dev) {
printk(KERN_WARNING "%s: Could not allocate device.\n",
SMSC_CHIPNAME);
return -ENOMEM;
}

smsc911x_drv_probe(gl_dev);

retval = register_netdev(gl_dev);
if (retval) {
printk("Error %i registering device", retval);
} else {
printk("Network interface: \"%s\"", gl_dev->name);
}
return retval;
}

/* entry point for unloading the module */
void smsc911x_cleanup_module(void)
{
printk("smsc911x cleanup module\n");
}

module_init(smsc911x_init_module);
module_exit(smsc911x_cleanup_module);
  评论这张
 
阅读(194)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018