20 #include <linux/module.h>
23 #include <linux/mii.h>
24 #include <linux/netdevice.h>
28 #include <linux/ethtool.h>
36 #include <asm/uaccess.h>
41 #include <mach/board.h>
45 #define DRV_NAME "at91_ether"
46 #define DRV_VERSION "1.0"
48 #define LINK_POLL_INTERVAL (HZ)
55 static inline unsigned long at91_emac_read(
struct at91_private *
lp,
unsigned int reg)
103 printk(
"at91_ether: MIO timeout\n");
117 | ((phy_addr & 0x1f) << 23) | (address << 18) | (value &
AT91_EMAC_DATA));
127 static void read_phy(
struct at91_private *lp,
unsigned char phy_addr,
unsigned char address,
unsigned int *value)
130 | ((phy_addr & 0x1f) << 23) | (address << 18));
145 static void update_linkspeed(
struct net_device *
dev,
int silent)
148 unsigned int bmsr, bmcr, lpa, mac_cfg;
211 if (!(phy & (1 << 0)))
216 if (!(phy & (1 << 2)))
221 if (!(phy & (1 << 0)))
226 if (!(phy & ((1 << 2) | 1)))
231 if (!(phy & ((1 << 2) | 1)))
236 if (!(phy & (1 << 7)))
240 update_linkspeed(dev, 0);
251 static void enable_phyirq(
struct net_device *dev)
254 unsigned int dsintr, irq_number;
257 if (!gpio_is_valid(lp->
board_data.phy_irq_pin)) {
267 status =
request_irq(irq_number, at91ether_phy_interrupt, 0, dev->
name, dev);
269 printk(
KERN_ERR "at91_ether: PHY IRQ %d request failed - status %d!\n", irq_number, status);
273 spin_lock_irq(&lp->
lock);
278 dsintr = dsintr & ~0xf00;
283 dsintr = dsintr | 0xf2;
287 dsintr = (1 << 15) | ( 1 << 14);
291 dsintr = (1 << 10) | ( 1 << 8);
296 dsintr = dsintr | 0x500;
301 dsintr = dsintr | 0x3c;
304 dsintr = dsintr | 0x3;
309 spin_unlock_irq(&lp->
lock);
315 static void disable_phyirq(
struct net_device *dev)
319 unsigned int irq_number;
321 if (!gpio_is_valid(lp->
board_data.phy_irq_pin)) {
326 spin_lock_irq(&lp->
lock);
331 dsintr = dsintr | 0xf00;
336 dsintr = dsintr & ~0xf2;
346 dsintr = ~((1 << 10) | (1 << 8));
351 dsintr = dsintr & ~0x500;
356 dsintr = dsintr & ~0x3;
359 dsintr = dsintr & ~0x3c;
364 spin_unlock_irq(&lp->
lock);
379 spin_lock_irq(&lp->
lock);
391 spin_unlock_irq(&lp->
lock);
395 static void at91ether_check_link(
unsigned long dev_id)
401 update_linkspeed(dev, 1);
414 spin_lock_irq(&lp->
lock);
419 if ((val & (1 << 10)) == 0)
421 }
else if (machine_is_csb337()) {
424 }
else if (machine_is_ecbat91())
428 spin_unlock_irq(&lp->
lock);
446 static short __init unpack_mac_address(
struct net_device *dev,
unsigned int hi,
unsigned int lo)
450 if (machine_is_csb337()) {
451 addr[5] = (lo & 0xff);
452 addr[4] = (lo & 0xff00) >> 8;
453 addr[3] = (lo & 0xff0000) >> 16;
454 addr[2] = (lo & 0xff000000) >> 24;
455 addr[1] = (hi & 0xff);
456 addr[0] = (hi & 0xff00) >> 8;
459 addr[0] = (lo & 0xff);
460 addr[1] = (lo & 0xff00) >> 8;
461 addr[2] = (lo & 0xff0000) >> 16;
462 addr[3] = (lo & 0xff000000) >> 24;
463 addr[4] = (hi & 0xff);
464 addr[5] = (hi & 0xff00) >> 8;
467 if (is_valid_ether_addr(addr)) {
494 printk(
KERN_ERR "at91_ether: Your bootloader did not configure a MAC address.\n");
500 static void update_mac_address(
struct net_device *dev)
514 static int set_mac_address(
struct net_device *dev,
void* addr)
518 if (!is_valid_ether_addr(address->
sa_data))
522 update_mac_address(dev);
524 printk(
"%s: Setting MAC address to %pM\n", dev->
name,
530 static int inline hash_bit_value(
int bitnr,
__u8 *addr)
532 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
568 static int hash_get_index(
__u8 *addr)
573 for (j = 0; j < 6; j++) {
574 for (i = 0, bitval = 0; i < 8; i++)
575 bitval ^= hash_bit_value(i*6 + j, addr);
577 hash_index |= (bitval <<
j);
586 static void at91ether_sethashtable(
struct net_device *dev)
590 unsigned long mc_filter[2];
593 mc_filter[0] = mc_filter[1] = 0;
596 bitnr = hash_get_index(ha->
addr);
597 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
607 static void at91ether_set_multicast_list(
struct net_device *dev)
624 at91ether_sethashtable(dev);
642 read_phy(lp, phy_id, location, &value);
650 write_phy(lp, phy_id, location, value);
658 spin_lock_irq(&lp->
lock);
664 spin_unlock_irq(&lp->
lock);
679 spin_lock_irq(&lp->
lock);
685 spin_unlock_irq(&lp->
lock);
690 static int at91ether_nwayreset(
struct net_device *dev)
695 spin_lock_irq(&lp->
lock);
701 spin_unlock_irq(&lp->
lock);
713 static const struct ethtool_ops at91ether_ethtool_ops = {
714 .get_settings = at91ether_get_settings,
715 .set_settings = at91ether_set_settings,
716 .get_drvinfo = at91ether_get_drvinfo,
717 .nway_reset = at91ether_nwayreset,
726 if (!netif_running(dev))
729 spin_lock_irq(&lp->
lock);
733 spin_unlock_irq(&lp->
lock);
743 static void at91ether_start(
struct net_device *dev)
775 static int at91ether_open(
struct net_device *dev)
780 if (!is_valid_ether_addr(dev->
dev_addr))
790 update_mac_address(dev);
801 spin_lock_irq(&lp->
lock);
803 update_linkspeed(dev, 0);
805 spin_unlock_irq(&lp->
lock);
807 at91ether_start(dev);
808 netif_start_queue(dev);
815 static int at91ether_close(
struct net_device *dev)
832 netif_stop_queue(dev);
847 netif_stop_queue(dev);
861 printk(
KERN_ERR "at91_ether.c: at91ether_start_xmit() called, but device is busy!\n");
876 int ale, lenerr, seqe, lcol, ecol;
878 if (netif_running(dev)) {
881 dev->
stats.rx_frame_errors += ale;
883 dev->
stats.rx_length_errors += lenerr;
885 dev->
stats.rx_crc_errors += seqe;
887 dev->
stats.rx_errors += (ale + lenerr + seqe
897 dev->
stats.tx_window_errors += lcol;
898 dev->
stats.tx_aborted_errors += ecol;
909 static void at91ether_rx(
struct net_device *dev)
913 unsigned char *p_recv;
921 skb = netdev_alloc_skb(dev, pktlen + 2);
931 dev->
stats.rx_dropped += 1;
936 dev->
stats.multicast++;
949 static irqreturn_t at91ether_interrupt(
int irq,
void *dev_id)
953 unsigned long intstatus,
ctl;
965 dev->
stats.tx_errors += 1;
972 netif_wake_queue(dev);
988 #ifdef CONFIG_NET_POLL_CONTROLLER
989 static void at91ether_poll_controller(
struct net_device *dev)
994 at91ether_interrupt(dev->
irq, dev);
1000 .ndo_open = at91ether_open,
1001 .ndo_stop = at91ether_close,
1002 .ndo_start_xmit = at91ether_start_xmit,
1003 .ndo_get_stats = at91ether_stats,
1004 .ndo_set_rx_mode = at91ether_set_multicast_list,
1005 .ndo_set_mac_address = set_mac_address,
1006 .ndo_do_ioctl = at91ether_ioctl,
1009 #ifdef CONFIG_NET_POLL_CONTROLLER
1010 .ndo_poll_controller = at91ether_poll_controller,
1019 unsigned int phyid1, phyid2;
1021 unsigned short phy_address = 0;
1030 phy_id = (phyid1 << 16) | (phyid2 & 0xfff0);
1075 lp = netdev_priv(dev);
1098 goto err_disable_clock;
1111 platform_set_drvdata(pdev, dev);
1114 get_mac_address(dev);
1115 update_mac_address(dev);
1125 if (!at91ether_phy_detect(lp)) {
1128 goto err_free_dmamem;
1134 lp->
mii.mdio_read = mdio_read;
1135 lp->
mii.mdio_write = mdio_write;
1137 lp->
mii.phy_id_mask = 0x1f;
1138 lp->
mii.reg_num_mask = 0x1f;
1143 goto err_free_dmamem;
1146 spin_lock_irq(&lp->
lock);
1148 update_linkspeed(dev, 0);
1150 spin_unlock_irq(&lp->
lock);
1154 if (gpio_is_valid(lp->
board_data.phy_irq_pin)) {
1196 platform_set_drvdata(pdev,
NULL);
1212 struct net_device *dev = platform_get_drvdata(pdev);
1215 if (gpio_is_valid(lp->
board_data.phy_irq_pin))
1223 platform_set_drvdata(pdev,
NULL);
1232 struct net_device *net_dev = platform_get_drvdata(pdev);
1235 if (netif_running(net_dev)) {
1236 if (gpio_is_valid(lp->
board_data.phy_irq_pin)) {
1241 netif_stop_queue(net_dev);
1251 struct net_device *net_dev = platform_get_drvdata(pdev);
1254 if (netif_running(net_dev)) {
1258 netif_start_queue(net_dev);
1260 if (gpio_is_valid(lp->
board_data.phy_irq_pin)) {
1269 #define at91ether_suspend NULL
1270 #define at91ether_resume NULL
1283 static int __init at91ether_init(
void)
1288 static void __exit at91ether_exit(
void)