1 /***************************************************************************
3 * Copyright (C) 2007,2008 SMSC
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 ***************************************************************************
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/interrupt.h>
24 #include <linux/kernel.h>
25 #include <linux/netdevice.h>
26 #include <linux/phy.h>
27 #include <linux/pci.h>
28 #include <linux/if_vlan.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/crc32.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <asm/unaligned.h>
36 #define DRV_NAME "smsc9420"
37 #define DRV_MDIONAME "smsc9420-mdio"
38 #define DRV_DESCRIPTION "SMSC LAN9420 driver"
39 #define DRV_VERSION "1.01"
41 MODULE_LICENSE("GPL");
42 MODULE_VERSION(DRV_VERSION
);
44 struct smsc9420_dma_desc
{
51 struct smsc9420_ring_info
{
56 struct smsc9420_pdata
{
59 struct net_device
*dev
;
61 struct smsc9420_dma_desc
*rx_ring
;
62 struct smsc9420_dma_desc
*tx_ring
;
63 struct smsc9420_ring_info
*tx_buffers
;
64 struct smsc9420_ring_info
*rx_buffers
;
65 dma_addr_t rx_dma_addr
;
66 dma_addr_t tx_dma_addr
;
67 int tx_ring_head
, tx_ring_tail
;
68 int rx_ring_head
, rx_ring_tail
;
73 struct napi_struct napi
;
75 bool software_irq_signal
;
79 struct mii_bus
*mii_bus
;
84 static const struct pci_device_id smsc9420_id_table
[] = {
85 { PCI_VENDOR_ID_9420
, PCI_DEVICE_ID_9420
, PCI_ANY_ID
, PCI_ANY_ID
, },
89 MODULE_DEVICE_TABLE(pci
, smsc9420_id_table
);
91 #define SMSC_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
93 static uint smsc_debug
;
94 static uint debug
= -1;
95 module_param(debug
, uint
, 0);
96 MODULE_PARM_DESC(debug
, "debug level");
98 static inline u32
smsc9420_reg_read(struct smsc9420_pdata
*pd
, u32 offset
)
100 return ioread32(pd
->ioaddr
+ offset
);
104 smsc9420_reg_write(struct smsc9420_pdata
*pd
, u32 offset
, u32 value
)
106 iowrite32(value
, pd
->ioaddr
+ offset
);
109 static inline void smsc9420_pci_flush_write(struct smsc9420_pdata
*pd
)
111 /* to ensure PCI write completion, we must perform a PCI read */
112 smsc9420_reg_read(pd
, ID_REV
);
115 static int smsc9420_mii_read(struct mii_bus
*bus
, int phyaddr
, int regidx
)
117 struct smsc9420_pdata
*pd
= (struct smsc9420_pdata
*)bus
->priv
;
122 spin_lock_irqsave(&pd
->phy_lock
, flags
);
124 /* confirm MII not busy */
125 if ((smsc9420_reg_read(pd
, MII_ACCESS
) & MII_ACCESS_MII_BUSY_
)) {
126 netif_warn(pd
, drv
, pd
->dev
, "MII is busy???\n");
130 /* set the address, index & direction (read from PHY) */
131 addr
= ((phyaddr
& 0x1F) << 11) | ((regidx
& 0x1F) << 6) |
132 MII_ACCESS_MII_READ_
;
133 smsc9420_reg_write(pd
, MII_ACCESS
, addr
);
135 /* wait for read to complete with 50us timeout */
136 for (i
= 0; i
< 5; i
++) {
137 if (!(smsc9420_reg_read(pd
, MII_ACCESS
) &
138 MII_ACCESS_MII_BUSY_
)) {
139 reg
= (u16
)smsc9420_reg_read(pd
, MII_DATA
);
145 netif_warn(pd
, drv
, pd
->dev
, "MII busy timeout!\n");
148 spin_unlock_irqrestore(&pd
->phy_lock
, flags
);
152 static int smsc9420_mii_write(struct mii_bus
*bus
, int phyaddr
, int regidx
,
155 struct smsc9420_pdata
*pd
= (struct smsc9420_pdata
*)bus
->priv
;
160 spin_lock_irqsave(&pd
->phy_lock
, flags
);
162 /* confirm MII not busy */
163 if ((smsc9420_reg_read(pd
, MII_ACCESS
) & MII_ACCESS_MII_BUSY_
)) {
164 netif_warn(pd
, drv
, pd
->dev
, "MII is busy???\n");
168 /* put the data to write in the MAC */
169 smsc9420_reg_write(pd
, MII_DATA
, (u32
)val
);
171 /* set the address, index & direction (write to PHY) */
172 addr
= ((phyaddr
& 0x1F) << 11) | ((regidx
& 0x1F) << 6) |
173 MII_ACCESS_MII_WRITE_
;
174 smsc9420_reg_write(pd
, MII_ACCESS
, addr
);
176 /* wait for write to complete with 50us timeout */
177 for (i
= 0; i
< 5; i
++) {
178 if (!(smsc9420_reg_read(pd
, MII_ACCESS
) &
179 MII_ACCESS_MII_BUSY_
)) {
186 netif_warn(pd
, drv
, pd
->dev
, "MII busy timeout!\n");
189 spin_unlock_irqrestore(&pd
->phy_lock
, flags
);
193 /* Returns hash bit number for given MAC address
195 * 01 00 5E 00 00 01 -> returns bit number 31 */
196 static u32
smsc9420_hash(u8 addr
[ETH_ALEN
])
198 return (ether_crc(ETH_ALEN
, addr
) >> 26) & 0x3f;
201 static int smsc9420_eeprom_reload(struct smsc9420_pdata
*pd
)
203 int timeout
= 100000;
207 if (smsc9420_reg_read(pd
, E2P_CMD
) & E2P_CMD_EPC_BUSY_
) {
208 netif_dbg(pd
, drv
, pd
->dev
, "%s: Eeprom busy\n", __func__
);
212 smsc9420_reg_write(pd
, E2P_CMD
,
213 (E2P_CMD_EPC_BUSY_
| E2P_CMD_EPC_CMD_RELOAD_
));
217 if (!(smsc9420_reg_read(pd
, E2P_CMD
) & E2P_CMD_EPC_BUSY_
))
221 netif_warn(pd
, drv
, pd
->dev
, "%s: Eeprom timed out\n", __func__
);
225 /* Standard ioctls for mii-tool */
226 static int smsc9420_do_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
228 if (!netif_running(dev
) || !dev
->phydev
)
231 return phy_mii_ioctl(dev
->phydev
, ifr
, cmd
);
234 static void smsc9420_ethtool_get_drvinfo(struct net_device
*netdev
,
235 struct ethtool_drvinfo
*drvinfo
)
237 struct smsc9420_pdata
*pd
= netdev_priv(netdev
);
239 strlcpy(drvinfo
->driver
, DRV_NAME
, sizeof(drvinfo
->driver
));
240 strlcpy(drvinfo
->bus_info
, pci_name(pd
->pdev
),
241 sizeof(drvinfo
->bus_info
));
242 strlcpy(drvinfo
->version
, DRV_VERSION
, sizeof(drvinfo
->version
));
245 static u32
smsc9420_ethtool_get_msglevel(struct net_device
*netdev
)
247 struct smsc9420_pdata
*pd
= netdev_priv(netdev
);
248 return pd
->msg_enable
;
251 static void smsc9420_ethtool_set_msglevel(struct net_device
*netdev
, u32 data
)
253 struct smsc9420_pdata
*pd
= netdev_priv(netdev
);
254 pd
->msg_enable
= data
;
257 static int smsc9420_ethtool_getregslen(struct net_device
*dev
)
259 /* all smsc9420 registers plus all phy registers */
260 return 0x100 + (32 * sizeof(u32
));
264 smsc9420_ethtool_getregs(struct net_device
*dev
, struct ethtool_regs
*regs
,
267 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
268 struct phy_device
*phy_dev
= dev
->phydev
;
269 unsigned int i
, j
= 0;
272 regs
->version
= smsc9420_reg_read(pd
, ID_REV
);
273 for (i
= 0; i
< 0x100; i
+= (sizeof(u32
)))
274 data
[j
++] = smsc9420_reg_read(pd
, i
);
276 // cannot read phy registers if the net device is down
280 for (i
= 0; i
<= 31; i
++)
281 data
[j
++] = smsc9420_mii_read(phy_dev
->mdio
.bus
,
282 phy_dev
->mdio
.addr
, i
);
285 static void smsc9420_eeprom_enable_access(struct smsc9420_pdata
*pd
)
287 unsigned int temp
= smsc9420_reg_read(pd
, GPIO_CFG
);
288 temp
&= ~GPIO_CFG_EEPR_EN_
;
289 smsc9420_reg_write(pd
, GPIO_CFG
, temp
);
293 static int smsc9420_eeprom_send_cmd(struct smsc9420_pdata
*pd
, u32 op
)
298 netif_dbg(pd
, hw
, pd
->dev
, "op 0x%08x\n", op
);
299 if (smsc9420_reg_read(pd
, E2P_CMD
) & E2P_CMD_EPC_BUSY_
) {
300 netif_warn(pd
, hw
, pd
->dev
, "Busy at start\n");
304 e2cmd
= op
| E2P_CMD_EPC_BUSY_
;
305 smsc9420_reg_write(pd
, E2P_CMD
, e2cmd
);
309 e2cmd
= smsc9420_reg_read(pd
, E2P_CMD
);
310 } while ((e2cmd
& E2P_CMD_EPC_BUSY_
) && (--timeout
));
313 netif_info(pd
, hw
, pd
->dev
, "TIMED OUT\n");
317 if (e2cmd
& E2P_CMD_EPC_TIMEOUT_
) {
318 netif_info(pd
, hw
, pd
->dev
,
319 "Error occurred during eeprom operation\n");
326 static int smsc9420_eeprom_read_location(struct smsc9420_pdata
*pd
,
327 u8 address
, u8
*data
)
329 u32 op
= E2P_CMD_EPC_CMD_READ_
| address
;
332 netif_dbg(pd
, hw
, pd
->dev
, "address 0x%x\n", address
);
333 ret
= smsc9420_eeprom_send_cmd(pd
, op
);
336 data
[address
] = smsc9420_reg_read(pd
, E2P_DATA
);
341 static int smsc9420_eeprom_write_location(struct smsc9420_pdata
*pd
,
344 u32 op
= E2P_CMD_EPC_CMD_ERASE_
| address
;
347 netif_dbg(pd
, hw
, pd
->dev
, "address 0x%x, data 0x%x\n", address
, data
);
348 ret
= smsc9420_eeprom_send_cmd(pd
, op
);
351 op
= E2P_CMD_EPC_CMD_WRITE_
| address
;
352 smsc9420_reg_write(pd
, E2P_DATA
, (u32
)data
);
353 ret
= smsc9420_eeprom_send_cmd(pd
, op
);
359 static int smsc9420_ethtool_get_eeprom_len(struct net_device
*dev
)
361 return SMSC9420_EEPROM_SIZE
;
364 static int smsc9420_ethtool_get_eeprom(struct net_device
*dev
,
365 struct ethtool_eeprom
*eeprom
, u8
*data
)
367 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
368 u8 eeprom_data
[SMSC9420_EEPROM_SIZE
];
371 smsc9420_eeprom_enable_access(pd
);
373 len
= min(eeprom
->len
, SMSC9420_EEPROM_SIZE
);
374 for (i
= 0; i
< len
; i
++) {
375 int ret
= smsc9420_eeprom_read_location(pd
, i
, eeprom_data
);
382 memcpy(data
, &eeprom_data
[eeprom
->offset
], len
);
383 eeprom
->magic
= SMSC9420_EEPROM_MAGIC
;
388 static int smsc9420_ethtool_set_eeprom(struct net_device
*dev
,
389 struct ethtool_eeprom
*eeprom
, u8
*data
)
391 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
394 if (eeprom
->magic
!= SMSC9420_EEPROM_MAGIC
)
397 smsc9420_eeprom_enable_access(pd
);
398 smsc9420_eeprom_send_cmd(pd
, E2P_CMD_EPC_CMD_EWEN_
);
399 ret
= smsc9420_eeprom_write_location(pd
, eeprom
->offset
, *data
);
400 smsc9420_eeprom_send_cmd(pd
, E2P_CMD_EPC_CMD_EWDS_
);
402 /* Single byte write, according to man page */
408 static const struct ethtool_ops smsc9420_ethtool_ops
= {
409 .get_drvinfo
= smsc9420_ethtool_get_drvinfo
,
410 .get_msglevel
= smsc9420_ethtool_get_msglevel
,
411 .set_msglevel
= smsc9420_ethtool_set_msglevel
,
412 .nway_reset
= phy_ethtool_nway_reset
,
413 .get_link
= ethtool_op_get_link
,
414 .get_eeprom_len
= smsc9420_ethtool_get_eeprom_len
,
415 .get_eeprom
= smsc9420_ethtool_get_eeprom
,
416 .set_eeprom
= smsc9420_ethtool_set_eeprom
,
417 .get_regs_len
= smsc9420_ethtool_getregslen
,
418 .get_regs
= smsc9420_ethtool_getregs
,
419 .get_ts_info
= ethtool_op_get_ts_info
,
420 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
421 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
424 /* Sets the device MAC address to dev_addr */
425 static void smsc9420_set_mac_address(struct net_device
*dev
)
427 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
428 u8
*dev_addr
= dev
->dev_addr
;
429 u32 mac_high16
= (dev_addr
[5] << 8) | dev_addr
[4];
430 u32 mac_low32
= (dev_addr
[3] << 24) | (dev_addr
[2] << 16) |
431 (dev_addr
[1] << 8) | dev_addr
[0];
433 smsc9420_reg_write(pd
, ADDRH
, mac_high16
);
434 smsc9420_reg_write(pd
, ADDRL
, mac_low32
);
437 static void smsc9420_check_mac_address(struct net_device
*dev
)
439 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
441 /* Check if mac address has been specified when bringing interface up */
442 if (is_valid_ether_addr(dev
->dev_addr
)) {
443 smsc9420_set_mac_address(dev
);
444 netif_dbg(pd
, probe
, pd
->dev
,
445 "MAC Address is specified by configuration\n");
447 /* Try reading mac address from device. if EEPROM is present
448 * it will already have been set */
449 u32 mac_high16
= smsc9420_reg_read(pd
, ADDRH
);
450 u32 mac_low32
= smsc9420_reg_read(pd
, ADDRL
);
451 dev
->dev_addr
[0] = (u8
)(mac_low32
);
452 dev
->dev_addr
[1] = (u8
)(mac_low32
>> 8);
453 dev
->dev_addr
[2] = (u8
)(mac_low32
>> 16);
454 dev
->dev_addr
[3] = (u8
)(mac_low32
>> 24);
455 dev
->dev_addr
[4] = (u8
)(mac_high16
);
456 dev
->dev_addr
[5] = (u8
)(mac_high16
>> 8);
458 if (is_valid_ether_addr(dev
->dev_addr
)) {
459 /* eeprom values are valid so use them */
460 netif_dbg(pd
, probe
, pd
->dev
,
461 "Mac Address is read from EEPROM\n");
463 /* eeprom values are invalid, generate random MAC */
464 eth_hw_addr_random(dev
);
465 smsc9420_set_mac_address(dev
);
466 netif_dbg(pd
, probe
, pd
->dev
,
467 "MAC Address is set to random\n");
472 static void smsc9420_stop_tx(struct smsc9420_pdata
*pd
)
474 u32 dmac_control
, mac_cr
, dma_intr_ena
;
477 /* disable TX DMAC */
478 dmac_control
= smsc9420_reg_read(pd
, DMAC_CONTROL
);
479 dmac_control
&= (~DMAC_CONTROL_ST_
);
480 smsc9420_reg_write(pd
, DMAC_CONTROL
, dmac_control
);
482 /* Wait max 10ms for transmit process to stop */
484 if (smsc9420_reg_read(pd
, DMAC_STATUS
) & DMAC_STS_TS_
)
490 netif_warn(pd
, ifdown
, pd
->dev
, "TX DMAC failed to stop\n");
492 /* ACK Tx DMAC stop bit */
493 smsc9420_reg_write(pd
, DMAC_STATUS
, DMAC_STS_TXPS_
);
495 /* mask TX DMAC interrupts */
496 dma_intr_ena
= smsc9420_reg_read(pd
, DMAC_INTR_ENA
);
497 dma_intr_ena
&= ~(DMAC_INTR_ENA_TX_
);
498 smsc9420_reg_write(pd
, DMAC_INTR_ENA
, dma_intr_ena
);
499 smsc9420_pci_flush_write(pd
);
502 mac_cr
= smsc9420_reg_read(pd
, MAC_CR
) & (~MAC_CR_TXEN_
);
503 smsc9420_reg_write(pd
, MAC_CR
, mac_cr
);
504 smsc9420_pci_flush_write(pd
);
507 static void smsc9420_free_tx_ring(struct smsc9420_pdata
*pd
)
511 BUG_ON(!pd
->tx_ring
);
516 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
517 struct sk_buff
*skb
= pd
->tx_buffers
[i
].skb
;
520 BUG_ON(!pd
->tx_buffers
[i
].mapping
);
521 pci_unmap_single(pd
->pdev
, pd
->tx_buffers
[i
].mapping
,
522 skb
->len
, PCI_DMA_TODEVICE
);
523 dev_kfree_skb_any(skb
);
526 pd
->tx_ring
[i
].status
= 0;
527 pd
->tx_ring
[i
].length
= 0;
528 pd
->tx_ring
[i
].buffer1
= 0;
529 pd
->tx_ring
[i
].buffer2
= 0;
533 kfree(pd
->tx_buffers
);
534 pd
->tx_buffers
= NULL
;
536 pd
->tx_ring_head
= 0;
537 pd
->tx_ring_tail
= 0;
540 static void smsc9420_free_rx_ring(struct smsc9420_pdata
*pd
)
544 BUG_ON(!pd
->rx_ring
);
549 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
550 if (pd
->rx_buffers
[i
].skb
)
551 dev_kfree_skb_any(pd
->rx_buffers
[i
].skb
);
553 if (pd
->rx_buffers
[i
].mapping
)
554 pci_unmap_single(pd
->pdev
, pd
->rx_buffers
[i
].mapping
,
555 PKT_BUF_SZ
, PCI_DMA_FROMDEVICE
);
557 pd
->rx_ring
[i
].status
= 0;
558 pd
->rx_ring
[i
].length
= 0;
559 pd
->rx_ring
[i
].buffer1
= 0;
560 pd
->rx_ring
[i
].buffer2
= 0;
564 kfree(pd
->rx_buffers
);
565 pd
->rx_buffers
= NULL
;
567 pd
->rx_ring_head
= 0;
568 pd
->rx_ring_tail
= 0;
571 static void smsc9420_stop_rx(struct smsc9420_pdata
*pd
)
574 u32 mac_cr
, dmac_control
, dma_intr_ena
;
576 /* mask RX DMAC interrupts */
577 dma_intr_ena
= smsc9420_reg_read(pd
, DMAC_INTR_ENA
);
578 dma_intr_ena
&= (~DMAC_INTR_ENA_RX_
);
579 smsc9420_reg_write(pd
, DMAC_INTR_ENA
, dma_intr_ena
);
580 smsc9420_pci_flush_write(pd
);
582 /* stop RX MAC prior to stoping DMA */
583 mac_cr
= smsc9420_reg_read(pd
, MAC_CR
) & (~MAC_CR_RXEN_
);
584 smsc9420_reg_write(pd
, MAC_CR
, mac_cr
);
585 smsc9420_pci_flush_write(pd
);
588 dmac_control
= smsc9420_reg_read(pd
, DMAC_CONTROL
);
589 dmac_control
&= (~DMAC_CONTROL_SR_
);
590 smsc9420_reg_write(pd
, DMAC_CONTROL
, dmac_control
);
591 smsc9420_pci_flush_write(pd
);
593 /* wait up to 10ms for receive to stop */
595 if (smsc9420_reg_read(pd
, DMAC_STATUS
) & DMAC_STS_RS_
)
601 netif_warn(pd
, ifdown
, pd
->dev
,
602 "RX DMAC did not stop! timeout\n");
604 /* ACK the Rx DMAC stop bit */
605 smsc9420_reg_write(pd
, DMAC_STATUS
, DMAC_STS_RXPS_
);
608 static irqreturn_t
smsc9420_isr(int irq
, void *dev_id
)
610 struct smsc9420_pdata
*pd
= dev_id
;
611 u32 int_cfg
, int_sts
, int_ctl
;
612 irqreturn_t ret
= IRQ_NONE
;
618 int_cfg
= smsc9420_reg_read(pd
, INT_CFG
);
620 /* check if it's our interrupt */
621 if ((int_cfg
& (INT_CFG_IRQ_EN_
| INT_CFG_IRQ_INT_
)) !=
622 (INT_CFG_IRQ_EN_
| INT_CFG_IRQ_INT_
))
625 int_sts
= smsc9420_reg_read(pd
, INT_STAT
);
627 if (likely(INT_STAT_DMAC_INT_
& int_sts
)) {
628 u32 status
= smsc9420_reg_read(pd
, DMAC_STATUS
);
629 u32 ints_to_clear
= 0;
631 if (status
& DMAC_STS_TX_
) {
632 ints_to_clear
|= (DMAC_STS_TX_
| DMAC_STS_NIS_
);
633 netif_wake_queue(pd
->dev
);
636 if (status
& DMAC_STS_RX_
) {
637 /* mask RX DMAC interrupts */
638 u32 dma_intr_ena
= smsc9420_reg_read(pd
, DMAC_INTR_ENA
);
639 dma_intr_ena
&= (~DMAC_INTR_ENA_RX_
);
640 smsc9420_reg_write(pd
, DMAC_INTR_ENA
, dma_intr_ena
);
641 smsc9420_pci_flush_write(pd
);
643 ints_to_clear
|= (DMAC_STS_RX_
| DMAC_STS_NIS_
);
644 napi_schedule(&pd
->napi
);
648 smsc9420_reg_write(pd
, DMAC_STATUS
, ints_to_clear
);
653 if (unlikely(INT_STAT_SW_INT_
& int_sts
)) {
654 /* mask software interrupt */
655 spin_lock_irqsave(&pd
->int_lock
, flags
);
656 int_ctl
= smsc9420_reg_read(pd
, INT_CTL
);
657 int_ctl
&= (~INT_CTL_SW_INT_EN_
);
658 smsc9420_reg_write(pd
, INT_CTL
, int_ctl
);
659 spin_unlock_irqrestore(&pd
->int_lock
, flags
);
661 smsc9420_reg_write(pd
, INT_STAT
, INT_STAT_SW_INT_
);
662 pd
->software_irq_signal
= true;
668 /* to ensure PCI write completion, we must perform a PCI read */
669 smsc9420_pci_flush_write(pd
);
674 #ifdef CONFIG_NET_POLL_CONTROLLER
675 static void smsc9420_poll_controller(struct net_device
*dev
)
677 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
678 const int irq
= pd
->pdev
->irq
;
681 smsc9420_isr(0, dev
);
684 #endif /* CONFIG_NET_POLL_CONTROLLER */
686 static void smsc9420_dmac_soft_reset(struct smsc9420_pdata
*pd
)
688 smsc9420_reg_write(pd
, BUS_MODE
, BUS_MODE_SWR_
);
689 smsc9420_reg_read(pd
, BUS_MODE
);
691 if (smsc9420_reg_read(pd
, BUS_MODE
) & BUS_MODE_SWR_
)
692 netif_warn(pd
, drv
, pd
->dev
, "Software reset not cleared\n");
695 static int smsc9420_stop(struct net_device
*dev
)
697 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
702 BUG_ON(!dev
->phydev
);
704 /* disable master interrupt */
705 spin_lock_irqsave(&pd
->int_lock
, flags
);
706 int_cfg
= smsc9420_reg_read(pd
, INT_CFG
) & (~INT_CFG_IRQ_EN_
);
707 smsc9420_reg_write(pd
, INT_CFG
, int_cfg
);
708 spin_unlock_irqrestore(&pd
->int_lock
, flags
);
710 netif_tx_disable(dev
);
711 napi_disable(&pd
->napi
);
713 smsc9420_stop_tx(pd
);
714 smsc9420_free_tx_ring(pd
);
716 smsc9420_stop_rx(pd
);
717 smsc9420_free_rx_ring(pd
);
719 free_irq(pd
->pdev
->irq
, pd
);
721 smsc9420_dmac_soft_reset(pd
);
723 phy_stop(dev
->phydev
);
725 phy_disconnect(dev
->phydev
);
726 mdiobus_unregister(pd
->mii_bus
);
727 mdiobus_free(pd
->mii_bus
);
732 static void smsc9420_rx_count_stats(struct net_device
*dev
, u32 desc_status
)
734 if (unlikely(desc_status
& RDES0_ERROR_SUMMARY_
)) {
735 dev
->stats
.rx_errors
++;
736 if (desc_status
& RDES0_DESCRIPTOR_ERROR_
)
737 dev
->stats
.rx_over_errors
++;
738 else if (desc_status
& (RDES0_FRAME_TOO_LONG_
|
739 RDES0_RUNT_FRAME_
| RDES0_COLLISION_SEEN_
))
740 dev
->stats
.rx_frame_errors
++;
741 else if (desc_status
& RDES0_CRC_ERROR_
)
742 dev
->stats
.rx_crc_errors
++;
745 if (unlikely(desc_status
& RDES0_LENGTH_ERROR_
))
746 dev
->stats
.rx_length_errors
++;
748 if (unlikely(!((desc_status
& RDES0_LAST_DESCRIPTOR_
) &&
749 (desc_status
& RDES0_FIRST_DESCRIPTOR_
))))
750 dev
->stats
.rx_length_errors
++;
752 if (desc_status
& RDES0_MULTICAST_FRAME_
)
753 dev
->stats
.multicast
++;
756 static void smsc9420_rx_handoff(struct smsc9420_pdata
*pd
, const int index
,
759 struct net_device
*dev
= pd
->dev
;
761 u16 packet_length
= (status
& RDES0_FRAME_LENGTH_MASK_
)
762 >> RDES0_FRAME_LENGTH_SHFT_
;
764 /* remove crc from packet lendth */
770 dev
->stats
.rx_packets
++;
771 dev
->stats
.rx_bytes
+= packet_length
;
773 pci_unmap_single(pd
->pdev
, pd
->rx_buffers
[index
].mapping
,
774 PKT_BUF_SZ
, PCI_DMA_FROMDEVICE
);
775 pd
->rx_buffers
[index
].mapping
= 0;
777 skb
= pd
->rx_buffers
[index
].skb
;
778 pd
->rx_buffers
[index
].skb
= NULL
;
781 u16 hw_csum
= get_unaligned_le16(skb_tail_pointer(skb
) +
782 NET_IP_ALIGN
+ packet_length
+ 4);
783 put_unaligned_le16(hw_csum
, &skb
->csum
);
784 skb
->ip_summed
= CHECKSUM_COMPLETE
;
787 skb_reserve(skb
, NET_IP_ALIGN
);
788 skb_put(skb
, packet_length
);
790 skb
->protocol
= eth_type_trans(skb
, dev
);
792 netif_receive_skb(skb
);
795 static int smsc9420_alloc_rx_buffer(struct smsc9420_pdata
*pd
, int index
)
797 struct sk_buff
*skb
= netdev_alloc_skb(pd
->dev
, PKT_BUF_SZ
);
800 BUG_ON(pd
->rx_buffers
[index
].skb
);
801 BUG_ON(pd
->rx_buffers
[index
].mapping
);
806 mapping
= pci_map_single(pd
->pdev
, skb_tail_pointer(skb
),
807 PKT_BUF_SZ
, PCI_DMA_FROMDEVICE
);
808 if (pci_dma_mapping_error(pd
->pdev
, mapping
)) {
809 dev_kfree_skb_any(skb
);
810 netif_warn(pd
, rx_err
, pd
->dev
, "pci_map_single failed!\n");
814 pd
->rx_buffers
[index
].skb
= skb
;
815 pd
->rx_buffers
[index
].mapping
= mapping
;
816 pd
->rx_ring
[index
].buffer1
= mapping
+ NET_IP_ALIGN
;
817 pd
->rx_ring
[index
].status
= RDES0_OWN_
;
823 static void smsc9420_alloc_new_rx_buffers(struct smsc9420_pdata
*pd
)
825 while (pd
->rx_ring_tail
!= pd
->rx_ring_head
) {
826 if (smsc9420_alloc_rx_buffer(pd
, pd
->rx_ring_tail
))
829 pd
->rx_ring_tail
= (pd
->rx_ring_tail
+ 1) % RX_RING_SIZE
;
833 static int smsc9420_rx_poll(struct napi_struct
*napi
, int budget
)
835 struct smsc9420_pdata
*pd
=
836 container_of(napi
, struct smsc9420_pdata
, napi
);
837 struct net_device
*dev
= pd
->dev
;
838 u32 drop_frame_cnt
, dma_intr_ena
, status
;
841 for (work_done
= 0; work_done
< budget
; work_done
++) {
843 status
= pd
->rx_ring
[pd
->rx_ring_head
].status
;
845 /* stop if DMAC owns this dma descriptor */
846 if (status
& RDES0_OWN_
)
849 smsc9420_rx_count_stats(dev
, status
);
850 smsc9420_rx_handoff(pd
, pd
->rx_ring_head
, status
);
851 pd
->rx_ring_head
= (pd
->rx_ring_head
+ 1) % RX_RING_SIZE
;
852 smsc9420_alloc_new_rx_buffers(pd
);
855 drop_frame_cnt
= smsc9420_reg_read(pd
, MISS_FRAME_CNTR
);
856 dev
->stats
.rx_dropped
+=
857 (drop_frame_cnt
& 0xFFFF) + ((drop_frame_cnt
>> 17) & 0x3FF);
860 smsc9420_reg_write(pd
, RX_POLL_DEMAND
, 1);
861 smsc9420_pci_flush_write(pd
);
863 if (work_done
< budget
) {
864 napi_complete_done(&pd
->napi
, work_done
);
866 /* re-enable RX DMA interrupts */
867 dma_intr_ena
= smsc9420_reg_read(pd
, DMAC_INTR_ENA
);
868 dma_intr_ena
|= (DMAC_INTR_ENA_RX_
| DMAC_INTR_ENA_NIS_
);
869 smsc9420_reg_write(pd
, DMAC_INTR_ENA
, dma_intr_ena
);
870 smsc9420_pci_flush_write(pd
);
876 smsc9420_tx_update_stats(struct net_device
*dev
, u32 status
, u32 length
)
878 if (unlikely(status
& TDES0_ERROR_SUMMARY_
)) {
879 dev
->stats
.tx_errors
++;
880 if (status
& (TDES0_EXCESSIVE_DEFERRAL_
|
881 TDES0_EXCESSIVE_COLLISIONS_
))
882 dev
->stats
.tx_aborted_errors
++;
884 if (status
& (TDES0_LOSS_OF_CARRIER_
| TDES0_NO_CARRIER_
))
885 dev
->stats
.tx_carrier_errors
++;
887 dev
->stats
.tx_packets
++;
888 dev
->stats
.tx_bytes
+= (length
& 0x7FF);
891 if (unlikely(status
& TDES0_EXCESSIVE_COLLISIONS_
)) {
892 dev
->stats
.collisions
+= 16;
894 dev
->stats
.collisions
+=
895 (status
& TDES0_COLLISION_COUNT_MASK_
) >>
896 TDES0_COLLISION_COUNT_SHFT_
;
899 if (unlikely(status
& TDES0_HEARTBEAT_FAIL_
))
900 dev
->stats
.tx_heartbeat_errors
++;
903 /* Check for completed dma transfers, update stats and free skbs */
904 static void smsc9420_complete_tx(struct net_device
*dev
)
906 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
908 while (pd
->tx_ring_tail
!= pd
->tx_ring_head
) {
909 int index
= pd
->tx_ring_tail
;
913 status
= pd
->tx_ring
[index
].status
;
914 length
= pd
->tx_ring
[index
].length
;
916 /* Check if DMA still owns this descriptor */
917 if (unlikely(TDES0_OWN_
& status
))
920 smsc9420_tx_update_stats(dev
, status
, length
);
922 BUG_ON(!pd
->tx_buffers
[index
].skb
);
923 BUG_ON(!pd
->tx_buffers
[index
].mapping
);
925 pci_unmap_single(pd
->pdev
, pd
->tx_buffers
[index
].mapping
,
926 pd
->tx_buffers
[index
].skb
->len
, PCI_DMA_TODEVICE
);
927 pd
->tx_buffers
[index
].mapping
= 0;
929 dev_kfree_skb_any(pd
->tx_buffers
[index
].skb
);
930 pd
->tx_buffers
[index
].skb
= NULL
;
932 pd
->tx_ring
[index
].buffer1
= 0;
935 pd
->tx_ring_tail
= (pd
->tx_ring_tail
+ 1) % TX_RING_SIZE
;
939 static netdev_tx_t
smsc9420_hard_start_xmit(struct sk_buff
*skb
,
940 struct net_device
*dev
)
942 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
944 int index
= pd
->tx_ring_head
;
946 bool about_to_take_last_desc
=
947 (((pd
->tx_ring_head
+ 2) % TX_RING_SIZE
) == pd
->tx_ring_tail
);
949 smsc9420_complete_tx(dev
);
952 BUG_ON(pd
->tx_ring
[index
].status
& TDES0_OWN_
);
953 BUG_ON(pd
->tx_buffers
[index
].skb
);
954 BUG_ON(pd
->tx_buffers
[index
].mapping
);
956 mapping
= pci_map_single(pd
->pdev
, skb
->data
,
957 skb
->len
, PCI_DMA_TODEVICE
);
958 if (pci_dma_mapping_error(pd
->pdev
, mapping
)) {
959 netif_warn(pd
, tx_err
, pd
->dev
,
960 "pci_map_single failed, dropping packet\n");
961 return NETDEV_TX_BUSY
;
964 pd
->tx_buffers
[index
].skb
= skb
;
965 pd
->tx_buffers
[index
].mapping
= mapping
;
967 tmp_desc1
= (TDES1_LS_
| ((u32
)skb
->len
& 0x7FF));
968 if (unlikely(about_to_take_last_desc
)) {
969 tmp_desc1
|= TDES1_IC_
;
970 netif_stop_queue(pd
->dev
);
973 /* check if we are at the last descriptor and need to set EOR */
974 if (unlikely(index
== (TX_RING_SIZE
- 1)))
975 tmp_desc1
|= TDES1_TER_
;
977 pd
->tx_ring
[index
].buffer1
= mapping
;
978 pd
->tx_ring
[index
].length
= tmp_desc1
;
982 pd
->tx_ring_head
= (pd
->tx_ring_head
+ 1) % TX_RING_SIZE
;
984 /* assign ownership to DMAC */
985 pd
->tx_ring
[index
].status
= TDES0_OWN_
;
988 skb_tx_timestamp(skb
);
991 smsc9420_reg_write(pd
, TX_POLL_DEMAND
, 1);
992 smsc9420_pci_flush_write(pd
);
997 static struct net_device_stats
*smsc9420_get_stats(struct net_device
*dev
)
999 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
1000 u32 counter
= smsc9420_reg_read(pd
, MISS_FRAME_CNTR
);
1001 dev
->stats
.rx_dropped
+=
1002 (counter
& 0x0000FFFF) + ((counter
>> 17) & 0x000003FF);
1006 static void smsc9420_set_multicast_list(struct net_device
*dev
)
1008 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
1009 u32 mac_cr
= smsc9420_reg_read(pd
, MAC_CR
);
1011 if (dev
->flags
& IFF_PROMISC
) {
1012 netif_dbg(pd
, hw
, pd
->dev
, "Promiscuous Mode Enabled\n");
1013 mac_cr
|= MAC_CR_PRMS_
;
1014 mac_cr
&= (~MAC_CR_MCPAS_
);
1015 mac_cr
&= (~MAC_CR_HPFILT_
);
1016 } else if (dev
->flags
& IFF_ALLMULTI
) {
1017 netif_dbg(pd
, hw
, pd
->dev
, "Receive all Multicast Enabled\n");
1018 mac_cr
&= (~MAC_CR_PRMS_
);
1019 mac_cr
|= MAC_CR_MCPAS_
;
1020 mac_cr
&= (~MAC_CR_HPFILT_
);
1021 } else if (!netdev_mc_empty(dev
)) {
1022 struct netdev_hw_addr
*ha
;
1023 u32 hash_lo
= 0, hash_hi
= 0;
1025 netif_dbg(pd
, hw
, pd
->dev
, "Multicast filter enabled\n");
1026 netdev_for_each_mc_addr(ha
, dev
) {
1027 u32 bit_num
= smsc9420_hash(ha
->addr
);
1028 u32 mask
= 1 << (bit_num
& 0x1F);
1036 smsc9420_reg_write(pd
, HASHH
, hash_hi
);
1037 smsc9420_reg_write(pd
, HASHL
, hash_lo
);
1039 mac_cr
&= (~MAC_CR_PRMS_
);
1040 mac_cr
&= (~MAC_CR_MCPAS_
);
1041 mac_cr
|= MAC_CR_HPFILT_
;
1043 netif_dbg(pd
, hw
, pd
->dev
, "Receive own packets only\n");
1044 smsc9420_reg_write(pd
, HASHH
, 0);
1045 smsc9420_reg_write(pd
, HASHL
, 0);
1047 mac_cr
&= (~MAC_CR_PRMS_
);
1048 mac_cr
&= (~MAC_CR_MCPAS_
);
1049 mac_cr
&= (~MAC_CR_HPFILT_
);
1052 smsc9420_reg_write(pd
, MAC_CR
, mac_cr
);
1053 smsc9420_pci_flush_write(pd
);
1056 static void smsc9420_phy_update_flowcontrol(struct smsc9420_pdata
*pd
)
1058 struct net_device
*dev
= pd
->dev
;
1059 struct phy_device
*phy_dev
= dev
->phydev
;
1062 if (phy_dev
->duplex
== DUPLEX_FULL
) {
1063 u16 lcladv
= phy_read(phy_dev
, MII_ADVERTISE
);
1064 u16 rmtadv
= phy_read(phy_dev
, MII_LPA
);
1065 u8 cap
= mii_resolve_flowctrl_fdx(lcladv
, rmtadv
);
1067 if (cap
& FLOW_CTRL_RX
)
1072 netif_info(pd
, link
, pd
->dev
, "rx pause %s, tx pause %s\n",
1073 cap
& FLOW_CTRL_RX
? "enabled" : "disabled",
1074 cap
& FLOW_CTRL_TX
? "enabled" : "disabled");
1076 netif_info(pd
, link
, pd
->dev
, "half duplex\n");
1080 smsc9420_reg_write(pd
, FLOW
, flow
);
1083 /* Update link mode if anything has changed. Called periodically when the
1084 * PHY is in polling mode, even if nothing has changed. */
1085 static void smsc9420_phy_adjust_link(struct net_device
*dev
)
1087 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
1088 struct phy_device
*phy_dev
= dev
->phydev
;
1091 if (phy_dev
->duplex
!= pd
->last_duplex
) {
1092 u32 mac_cr
= smsc9420_reg_read(pd
, MAC_CR
);
1093 if (phy_dev
->duplex
) {
1094 netif_dbg(pd
, link
, pd
->dev
, "full duplex mode\n");
1095 mac_cr
|= MAC_CR_FDPX_
;
1097 netif_dbg(pd
, link
, pd
->dev
, "half duplex mode\n");
1098 mac_cr
&= ~MAC_CR_FDPX_
;
1100 smsc9420_reg_write(pd
, MAC_CR
, mac_cr
);
1102 smsc9420_phy_update_flowcontrol(pd
);
1103 pd
->last_duplex
= phy_dev
->duplex
;
1106 carrier
= netif_carrier_ok(dev
);
1107 if (carrier
!= pd
->last_carrier
) {
1109 netif_dbg(pd
, link
, pd
->dev
, "carrier OK\n");
1111 netif_dbg(pd
, link
, pd
->dev
, "no carrier\n");
1112 pd
->last_carrier
= carrier
;
1116 static int smsc9420_mii_probe(struct net_device
*dev
)
1118 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
1119 struct phy_device
*phydev
= NULL
;
1121 BUG_ON(dev
->phydev
);
1123 /* Device only supports internal PHY at address 1 */
1124 phydev
= mdiobus_get_phy(pd
->mii_bus
, 1);
1126 netdev_err(dev
, "no PHY found at address 1\n");
1130 phydev
= phy_connect(dev
, phydev_name(phydev
),
1131 smsc9420_phy_adjust_link
, PHY_INTERFACE_MODE_MII
);
1133 if (IS_ERR(phydev
)) {
1134 netdev_err(dev
, "Could not attach to PHY\n");
1135 return PTR_ERR(phydev
);
1138 /* mask with MAC supported features */
1139 phydev
->supported
&= (PHY_BASIC_FEATURES
| SUPPORTED_Pause
|
1140 SUPPORTED_Asym_Pause
);
1141 phydev
->advertising
= phydev
->supported
;
1143 phy_attached_info(phydev
);
1145 pd
->last_duplex
= -1;
1146 pd
->last_carrier
= -1;
1151 static int smsc9420_mii_init(struct net_device
*dev
)
1153 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
1156 pd
->mii_bus
= mdiobus_alloc();
1161 pd
->mii_bus
->name
= DRV_MDIONAME
;
1162 snprintf(pd
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%x",
1163 (pd
->pdev
->bus
->number
<< 8) | pd
->pdev
->devfn
);
1164 pd
->mii_bus
->priv
= pd
;
1165 pd
->mii_bus
->read
= smsc9420_mii_read
;
1166 pd
->mii_bus
->write
= smsc9420_mii_write
;
1168 /* Mask all PHYs except ID 1 (internal) */
1169 pd
->mii_bus
->phy_mask
= ~(1 << 1);
1171 if (mdiobus_register(pd
->mii_bus
)) {
1172 netif_warn(pd
, probe
, pd
->dev
, "Error registering mii bus\n");
1173 goto err_out_free_bus_2
;
1176 if (smsc9420_mii_probe(dev
) < 0) {
1177 netif_warn(pd
, probe
, pd
->dev
, "Error probing mii bus\n");
1178 goto err_out_unregister_bus_3
;
1183 err_out_unregister_bus_3
:
1184 mdiobus_unregister(pd
->mii_bus
);
1186 mdiobus_free(pd
->mii_bus
);
1191 static int smsc9420_alloc_tx_ring(struct smsc9420_pdata
*pd
)
1195 BUG_ON(!pd
->tx_ring
);
1197 pd
->tx_buffers
= kmalloc_array(TX_RING_SIZE
,
1198 sizeof(struct smsc9420_ring_info
),
1200 if (!pd
->tx_buffers
)
1203 /* Initialize the TX Ring */
1204 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
1205 pd
->tx_buffers
[i
].skb
= NULL
;
1206 pd
->tx_buffers
[i
].mapping
= 0;
1207 pd
->tx_ring
[i
].status
= 0;
1208 pd
->tx_ring
[i
].length
= 0;
1209 pd
->tx_ring
[i
].buffer1
= 0;
1210 pd
->tx_ring
[i
].buffer2
= 0;
1212 pd
->tx_ring
[TX_RING_SIZE
- 1].length
= TDES1_TER_
;
1215 pd
->tx_ring_head
= 0;
1216 pd
->tx_ring_tail
= 0;
1218 smsc9420_reg_write(pd
, TX_BASE_ADDR
, pd
->tx_dma_addr
);
1219 smsc9420_pci_flush_write(pd
);
1224 static int smsc9420_alloc_rx_ring(struct smsc9420_pdata
*pd
)
1228 BUG_ON(!pd
->rx_ring
);
1230 pd
->rx_buffers
= kmalloc_array(RX_RING_SIZE
,
1231 sizeof(struct smsc9420_ring_info
),
1233 if (pd
->rx_buffers
== NULL
)
1236 /* initialize the rx ring */
1237 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
1238 pd
->rx_ring
[i
].status
= 0;
1239 pd
->rx_ring
[i
].length
= PKT_BUF_SZ
;
1240 pd
->rx_ring
[i
].buffer2
= 0;
1241 pd
->rx_buffers
[i
].skb
= NULL
;
1242 pd
->rx_buffers
[i
].mapping
= 0;
1244 pd
->rx_ring
[RX_RING_SIZE
- 1].length
= (PKT_BUF_SZ
| RDES1_RER_
);
1246 /* now allocate the entire ring of skbs */
1247 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
1248 if (smsc9420_alloc_rx_buffer(pd
, i
)) {
1249 netif_warn(pd
, ifup
, pd
->dev
,
1250 "failed to allocate rx skb %d\n", i
);
1251 goto out_free_rx_skbs
;
1255 pd
->rx_ring_head
= 0;
1256 pd
->rx_ring_tail
= 0;
1258 smsc9420_reg_write(pd
, VLAN1
, ETH_P_8021Q
);
1259 netif_dbg(pd
, ifup
, pd
->dev
, "VLAN1 = 0x%08x\n",
1260 smsc9420_reg_read(pd
, VLAN1
));
1264 u32 coe
= smsc9420_reg_read(pd
, COE_CR
) | RX_COE_EN
;
1265 smsc9420_reg_write(pd
, COE_CR
, coe
);
1266 netif_dbg(pd
, ifup
, pd
->dev
, "COE_CR = 0x%08x\n", coe
);
1269 smsc9420_reg_write(pd
, RX_BASE_ADDR
, pd
->rx_dma_addr
);
1270 smsc9420_pci_flush_write(pd
);
1275 smsc9420_free_rx_ring(pd
);
1280 static int smsc9420_open(struct net_device
*dev
)
1282 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
1283 u32 bus_mode
, mac_cr
, dmac_control
, int_cfg
, dma_intr_ena
, int_ctl
;
1284 const int irq
= pd
->pdev
->irq
;
1285 unsigned long flags
;
1286 int result
= 0, timeout
;
1288 if (!is_valid_ether_addr(dev
->dev_addr
)) {
1289 netif_warn(pd
, ifup
, pd
->dev
,
1290 "dev_addr is not a valid MAC address\n");
1291 result
= -EADDRNOTAVAIL
;
1295 netif_carrier_off(dev
);
1297 /* disable, mask and acknowledge all interrupts */
1298 spin_lock_irqsave(&pd
->int_lock
, flags
);
1299 int_cfg
= smsc9420_reg_read(pd
, INT_CFG
) & (~INT_CFG_IRQ_EN_
);
1300 smsc9420_reg_write(pd
, INT_CFG
, int_cfg
);
1301 smsc9420_reg_write(pd
, INT_CTL
, 0);
1302 spin_unlock_irqrestore(&pd
->int_lock
, flags
);
1303 smsc9420_reg_write(pd
, DMAC_INTR_ENA
, 0);
1304 smsc9420_reg_write(pd
, INT_STAT
, 0xFFFFFFFF);
1305 smsc9420_pci_flush_write(pd
);
1307 result
= request_irq(irq
, smsc9420_isr
, IRQF_SHARED
, DRV_NAME
, pd
);
1309 netif_warn(pd
, ifup
, pd
->dev
, "Unable to use IRQ = %d\n", irq
);
1314 smsc9420_dmac_soft_reset(pd
);
1316 /* make sure MAC_CR is sane */
1317 smsc9420_reg_write(pd
, MAC_CR
, 0);
1319 smsc9420_set_mac_address(dev
);
1321 /* Configure GPIO pins to drive LEDs */
1322 smsc9420_reg_write(pd
, GPIO_CFG
,
1323 (GPIO_CFG_LED_3_
| GPIO_CFG_LED_2_
| GPIO_CFG_LED_1_
));
1325 bus_mode
= BUS_MODE_DMA_BURST_LENGTH_16
;
1328 bus_mode
|= BUS_MODE_DBO_
;
1331 smsc9420_reg_write(pd
, BUS_MODE
, bus_mode
);
1333 smsc9420_pci_flush_write(pd
);
1335 /* set bus master bridge arbitration priority for Rx and TX DMA */
1336 smsc9420_reg_write(pd
, BUS_CFG
, BUS_CFG_RXTXWEIGHT_4_1
);
1338 smsc9420_reg_write(pd
, DMAC_CONTROL
,
1339 (DMAC_CONTROL_SF_
| DMAC_CONTROL_OSF_
));
1341 smsc9420_pci_flush_write(pd
);
1343 /* test the IRQ connection to the ISR */
1344 netif_dbg(pd
, ifup
, pd
->dev
, "Testing ISR using IRQ %d\n", irq
);
1345 pd
->software_irq_signal
= false;
1347 spin_lock_irqsave(&pd
->int_lock
, flags
);
1348 /* configure interrupt deassertion timer and enable interrupts */
1349 int_cfg
= smsc9420_reg_read(pd
, INT_CFG
) | INT_CFG_IRQ_EN_
;
1350 int_cfg
&= ~(INT_CFG_INT_DEAS_MASK
);
1351 int_cfg
|= (INT_DEAS_TIME
& INT_CFG_INT_DEAS_MASK
);
1352 smsc9420_reg_write(pd
, INT_CFG
, int_cfg
);
1354 /* unmask software interrupt */
1355 int_ctl
= smsc9420_reg_read(pd
, INT_CTL
) | INT_CTL_SW_INT_EN_
;
1356 smsc9420_reg_write(pd
, INT_CTL
, int_ctl
);
1357 spin_unlock_irqrestore(&pd
->int_lock
, flags
);
1358 smsc9420_pci_flush_write(pd
);
1362 if (pd
->software_irq_signal
)
1367 /* disable interrupts */
1368 spin_lock_irqsave(&pd
->int_lock
, flags
);
1369 int_cfg
= smsc9420_reg_read(pd
, INT_CFG
) & (~INT_CFG_IRQ_EN_
);
1370 smsc9420_reg_write(pd
, INT_CFG
, int_cfg
);
1371 spin_unlock_irqrestore(&pd
->int_lock
, flags
);
1373 if (!pd
->software_irq_signal
) {
1374 netif_warn(pd
, ifup
, pd
->dev
, "ISR failed signaling test\n");
1376 goto out_free_irq_1
;
1379 netif_dbg(pd
, ifup
, pd
->dev
, "ISR passed test using IRQ %d\n", irq
);
1381 result
= smsc9420_alloc_tx_ring(pd
);
1383 netif_warn(pd
, ifup
, pd
->dev
,
1384 "Failed to Initialize tx dma ring\n");
1386 goto out_free_irq_1
;
1389 result
= smsc9420_alloc_rx_ring(pd
);
1391 netif_warn(pd
, ifup
, pd
->dev
,
1392 "Failed to Initialize rx dma ring\n");
1394 goto out_free_tx_ring_2
;
1397 result
= smsc9420_mii_init(dev
);
1399 netif_warn(pd
, ifup
, pd
->dev
, "Failed to initialize Phy\n");
1401 goto out_free_rx_ring_3
;
1404 /* Bring the PHY up */
1405 phy_start(dev
->phydev
);
1407 napi_enable(&pd
->napi
);
1409 /* start tx and rx */
1410 mac_cr
= smsc9420_reg_read(pd
, MAC_CR
) | MAC_CR_TXEN_
| MAC_CR_RXEN_
;
1411 smsc9420_reg_write(pd
, MAC_CR
, mac_cr
);
1413 dmac_control
= smsc9420_reg_read(pd
, DMAC_CONTROL
);
1414 dmac_control
|= DMAC_CONTROL_ST_
| DMAC_CONTROL_SR_
;
1415 smsc9420_reg_write(pd
, DMAC_CONTROL
, dmac_control
);
1416 smsc9420_pci_flush_write(pd
);
1418 dma_intr_ena
= smsc9420_reg_read(pd
, DMAC_INTR_ENA
);
1420 (DMAC_INTR_ENA_TX_
| DMAC_INTR_ENA_RX_
| DMAC_INTR_ENA_NIS_
);
1421 smsc9420_reg_write(pd
, DMAC_INTR_ENA
, dma_intr_ena
);
1422 smsc9420_pci_flush_write(pd
);
1424 netif_wake_queue(dev
);
1426 smsc9420_reg_write(pd
, RX_POLL_DEMAND
, 1);
1428 /* enable interrupts */
1429 spin_lock_irqsave(&pd
->int_lock
, flags
);
1430 int_cfg
= smsc9420_reg_read(pd
, INT_CFG
) | INT_CFG_IRQ_EN_
;
1431 smsc9420_reg_write(pd
, INT_CFG
, int_cfg
);
1432 spin_unlock_irqrestore(&pd
->int_lock
, flags
);
1437 smsc9420_free_rx_ring(pd
);
1439 smsc9420_free_tx_ring(pd
);
1448 static int smsc9420_suspend(struct pci_dev
*pdev
, pm_message_t state
)
1450 struct net_device
*dev
= pci_get_drvdata(pdev
);
1451 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
1455 /* disable interrupts */
1456 spin_lock_irqsave(&pd
->int_lock
, flags
);
1457 int_cfg
= smsc9420_reg_read(pd
, INT_CFG
) & (~INT_CFG_IRQ_EN_
);
1458 smsc9420_reg_write(pd
, INT_CFG
, int_cfg
);
1459 spin_unlock_irqrestore(&pd
->int_lock
, flags
);
1461 if (netif_running(dev
)) {
1462 netif_tx_disable(dev
);
1463 smsc9420_stop_tx(pd
);
1464 smsc9420_free_tx_ring(pd
);
1466 napi_disable(&pd
->napi
);
1467 smsc9420_stop_rx(pd
);
1468 smsc9420_free_rx_ring(pd
);
1470 free_irq(pd
->pdev
->irq
, pd
);
1472 netif_device_detach(dev
);
1475 pci_save_state(pdev
);
1476 pci_enable_wake(pdev
, pci_choose_state(pdev
, state
), 0);
1477 pci_disable_device(pdev
);
1478 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
1483 static int smsc9420_resume(struct pci_dev
*pdev
)
1485 struct net_device
*dev
= pci_get_drvdata(pdev
);
1486 struct smsc9420_pdata
*pd
= netdev_priv(dev
);
1489 pci_set_power_state(pdev
, PCI_D0
);
1490 pci_restore_state(pdev
);
1492 err
= pci_enable_device(pdev
);
1496 pci_set_master(pdev
);
1498 err
= pci_enable_wake(pdev
, PCI_D0
, 0);
1500 netif_warn(pd
, ifup
, pd
->dev
, "pci_enable_wake failed: %d\n",
1503 if (netif_running(dev
)) {
1504 /* FIXME: gross. It looks like ancient PM relic.*/
1505 err
= smsc9420_open(dev
);
1506 netif_device_attach(dev
);
1511 #endif /* CONFIG_PM */
1513 static const struct net_device_ops smsc9420_netdev_ops
= {
1514 .ndo_open
= smsc9420_open
,
1515 .ndo_stop
= smsc9420_stop
,
1516 .ndo_start_xmit
= smsc9420_hard_start_xmit
,
1517 .ndo_get_stats
= smsc9420_get_stats
,
1518 .ndo_set_rx_mode
= smsc9420_set_multicast_list
,
1519 .ndo_do_ioctl
= smsc9420_do_ioctl
,
1520 .ndo_validate_addr
= eth_validate_addr
,
1521 .ndo_set_mac_address
= eth_mac_addr
,
1522 #ifdef CONFIG_NET_POLL_CONTROLLER
1523 .ndo_poll_controller
= smsc9420_poll_controller
,
1524 #endif /* CONFIG_NET_POLL_CONTROLLER */
1528 smsc9420_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1530 struct net_device
*dev
;
1531 struct smsc9420_pdata
*pd
;
1532 void __iomem
*virt_addr
;
1536 pr_info("%s version %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
1538 /* First do the PCI initialisation */
1539 result
= pci_enable_device(pdev
);
1540 if (unlikely(result
)) {
1541 pr_err("Cannot enable smsc9420\n");
1545 pci_set_master(pdev
);
1547 dev
= alloc_etherdev(sizeof(*pd
));
1549 goto out_disable_pci_device_1
;
1551 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1553 if (!(pci_resource_flags(pdev
, SMSC_BAR
) & IORESOURCE_MEM
)) {
1554 netdev_err(dev
, "Cannot find PCI device base address\n");
1555 goto out_free_netdev_2
;
1558 if ((pci_request_regions(pdev
, DRV_NAME
))) {
1559 netdev_err(dev
, "Cannot obtain PCI resources, aborting\n");
1560 goto out_free_netdev_2
;
1563 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))) {
1564 netdev_err(dev
, "No usable DMA configuration, aborting\n");
1565 goto out_free_regions_3
;
1568 virt_addr
= ioremap(pci_resource_start(pdev
, SMSC_BAR
),
1569 pci_resource_len(pdev
, SMSC_BAR
));
1571 netdev_err(dev
, "Cannot map device registers, aborting\n");
1572 goto out_free_regions_3
;
1575 /* registers are double mapped with 0 offset for LE and 0x200 for BE */
1576 virt_addr
+= LAN9420_CPSR_ENDIAN_OFFSET
;
1578 pd
= netdev_priv(dev
);
1580 /* pci descriptors are created in the PCI consistent area */
1581 pd
->rx_ring
= pci_alloc_consistent(pdev
,
1582 sizeof(struct smsc9420_dma_desc
) * RX_RING_SIZE
+
1583 sizeof(struct smsc9420_dma_desc
) * TX_RING_SIZE
,
1589 /* descriptors are aligned due to the nature of pci_alloc_consistent */
1590 pd
->tx_ring
= (pd
->rx_ring
+ RX_RING_SIZE
);
1591 pd
->tx_dma_addr
= pd
->rx_dma_addr
+
1592 sizeof(struct smsc9420_dma_desc
) * RX_RING_SIZE
;
1596 pd
->ioaddr
= virt_addr
;
1597 pd
->msg_enable
= smsc_debug
;
1600 netif_dbg(pd
, probe
, pd
->dev
, "lan_base=0x%08lx\n", (ulong
)virt_addr
);
1602 id_rev
= smsc9420_reg_read(pd
, ID_REV
);
1603 switch (id_rev
& 0xFFFF0000) {
1605 netif_info(pd
, probe
, pd
->dev
,
1606 "LAN9420 identified, ID_REV=0x%08X\n", id_rev
);
1609 netif_warn(pd
, probe
, pd
->dev
, "LAN9420 NOT identified\n");
1610 netif_warn(pd
, probe
, pd
->dev
, "ID_REV=0x%08X\n", id_rev
);
1611 goto out_free_dmadesc_5
;
1614 smsc9420_dmac_soft_reset(pd
);
1615 smsc9420_eeprom_reload(pd
);
1616 smsc9420_check_mac_address(dev
);
1618 dev
->netdev_ops
= &smsc9420_netdev_ops
;
1619 dev
->ethtool_ops
= &smsc9420_ethtool_ops
;
1621 netif_napi_add(dev
, &pd
->napi
, smsc9420_rx_poll
, NAPI_WEIGHT
);
1623 result
= register_netdev(dev
);
1625 netif_warn(pd
, probe
, pd
->dev
, "error %i registering device\n",
1627 goto out_free_dmadesc_5
;
1630 pci_set_drvdata(pdev
, dev
);
1632 spin_lock_init(&pd
->int_lock
);
1633 spin_lock_init(&pd
->phy_lock
);
1635 dev_info(&dev
->dev
, "MAC Address: %pM\n", dev
->dev_addr
);
1640 pci_free_consistent(pdev
, sizeof(struct smsc9420_dma_desc
) *
1641 (RX_RING_SIZE
+ TX_RING_SIZE
), pd
->rx_ring
, pd
->rx_dma_addr
);
1643 iounmap(virt_addr
- LAN9420_CPSR_ENDIAN_OFFSET
);
1645 pci_release_regions(pdev
);
1648 out_disable_pci_device_1
:
1649 pci_disable_device(pdev
);
1654 static void smsc9420_remove(struct pci_dev
*pdev
)
1656 struct net_device
*dev
;
1657 struct smsc9420_pdata
*pd
;
1659 dev
= pci_get_drvdata(pdev
);
1663 pd
= netdev_priv(dev
);
1664 unregister_netdev(dev
);
1666 /* tx_buffers and rx_buffers are freed in stop */
1667 BUG_ON(pd
->tx_buffers
);
1668 BUG_ON(pd
->rx_buffers
);
1670 BUG_ON(!pd
->tx_ring
);
1671 BUG_ON(!pd
->rx_ring
);
1673 pci_free_consistent(pdev
, sizeof(struct smsc9420_dma_desc
) *
1674 (RX_RING_SIZE
+ TX_RING_SIZE
), pd
->rx_ring
, pd
->rx_dma_addr
);
1676 iounmap(pd
->ioaddr
- LAN9420_CPSR_ENDIAN_OFFSET
);
1677 pci_release_regions(pdev
);
1679 pci_disable_device(pdev
);
1682 static struct pci_driver smsc9420_driver
= {
1684 .id_table
= smsc9420_id_table
,
1685 .probe
= smsc9420_probe
,
1686 .remove
= smsc9420_remove
,
1688 .suspend
= smsc9420_suspend
,
1689 .resume
= smsc9420_resume
,
1690 #endif /* CONFIG_PM */
1693 static int __init
smsc9420_init_module(void)
1695 smsc_debug
= netif_msg_init(debug
, SMSC_MSG_DEFAULT
);
1697 return pci_register_driver(&smsc9420_driver
);
1700 static void __exit
smsc9420_exit_module(void)
1702 pci_unregister_driver(&smsc9420_driver
);
1705 module_init(smsc9420_init_module
);
1706 module_exit(smsc9420_exit_module
);