12 #include <linux/module.h>
14 #include <linux/mii.h>
15 #include <linux/netdevice.h>
18 #include <linux/ethtool.h>
23 #define DRV_MODULE_NAME "w90p910-emc"
24 #define DRV_MODULE_VERSION "0.1"
27 #define REG_CAMCMR 0x00
28 #define REG_CAMEN 0x04
29 #define REG_CAMM_BASE 0x08
30 #define REG_CAML_BASE 0x0c
31 #define REG_TXDLSA 0x88
32 #define REG_RXDLSA 0x8C
33 #define REG_MCMDR 0x90
35 #define REG_MIIDA 0x98
36 #define REG_FFTCR 0x9C
39 #define REG_DMARFC 0xa8
41 #define REG_MISTA 0xb0
42 #define REG_CTXDSA 0xcc
43 #define REG_CTXBSA 0xd0
44 #define REG_CRXDSA 0xd4
45 #define REG_CRXBSA 0xd8
48 #define MCMDR_RXON 0x01
49 #define MCMDR_ACP (0x01 << 3)
50 #define MCMDR_SPCRC (0x01 << 5)
51 #define MCMDR_TXON (0x01 << 8)
52 #define MCMDR_FDUP (0x01 << 18)
53 #define MCMDR_ENMDC (0x01 << 19)
54 #define MCMDR_OPMOD (0x01 << 20)
55 #define SWR (0x01 << 24)
58 #define CAMCMR_AUP 0x01
59 #define CAMCMR_AMP (0x01 << 1)
60 #define CAMCMR_ABP (0x01 << 2)
61 #define CAMCMR_CCAM (0x01 << 3)
62 #define CAMCMR_ECMP (0x01 << 4)
66 #define MDCCR (0x0a << 20)
67 #define PHYAD (0x01 << 8)
68 #define PHYWR (0x01 << 16)
69 #define PHYBUSY (0x01 << 17)
70 #define PHYPRESP (0x01 << 18)
71 #define CAM_ENTRY_SIZE 0x08
74 #define TXDS_TXCP (0x01 << 19)
75 #define RXDS_CRCE (0x01 << 17)
76 #define RXDS_PTLE (0x01 << 19)
77 #define RXDS_RXGD (0x01 << 20)
78 #define RXDS_ALIE (0x01 << 21)
79 #define RXDS_RP (0x01 << 22)
82 #define MISTA_EXDEF (0x01 << 19)
83 #define MISTA_TXBERR (0x01 << 24)
84 #define MISTA_TDU (0x01 << 23)
85 #define MISTA_RDU (0x01 << 10)
86 #define MISTA_RXBERR (0x01 << 11)
90 #define ENRXGD (0x01 << 4)
91 #define ENRXBERR (0x01 << 11)
92 #define ENTXINTR (0x01 << 16)
93 #define ENTXCP (0x01 << 18)
94 #define ENTXABT (0x01 << 21)
95 #define ENTXBERR (0x01 << 24)
96 #define ENMDC (0x01 << 19)
97 #define PHYBUSY (0x01 << 17)
98 #define MDCCR_VAL 0xa00000
101 #define RX_OWEN_DMA (0x01 << 31)
102 #define RX_OWEN_CPU (~(0x03 << 30))
103 #define TX_OWEN_DMA (0x01 << 31)
104 #define TX_OWEN_CPU (~(0x01 << 31))
107 #define MACTXINTEN 0x04
109 #define PADDINGMODE 0x01
112 #define TXTHD (0x03 << 8)
113 #define BLENGTH (0x01 << 20)
116 #define RX_DESC_SIZE 50
117 #define TX_DESC_SIZE 10
118 #define MAX_RBUFF_SZ 0x600
119 #define MAX_TBUFF_SZ 0x600
120 #define TX_TIMEOUT (HZ/2)
176 static void update_linkspeed_register(
struct net_device *
dev,
177 unsigned int speed,
unsigned int duplex)
205 static void update_linkspeed(
struct net_device *dev)
209 unsigned int bmsr, bmcr, lpa, speed,
duplex;
223 bmsr = w90p910_mdio_read(dev, ether->
mii.phy_id,
MII_BMSR);
224 bmcr = w90p910_mdio_read(dev, ether->
mii.phy_id,
MII_BMCR);
230 lpa = w90p910_mdio_read(dev, ether->
mii.phy_id,
MII_LPA);
237 if ((lpa & LPA_100FULL) || (lpa &
LPA_10FULL))
247 update_linkspeed_register(dev, speed, duplex);
250 (duplex ==
DUPLEX_FULL) ?
"FullDuplex" :
"HalfDuplex");
256 static void w90p910_check_link(
unsigned long dev_id)
261 update_linkspeed(dev);
265 static void w90p910_write_cam(
struct net_device *dev,
266 unsigned int x,
unsigned char *pval)
269 unsigned int msw, lsw;
271 msw = (pval[0] << 24) | (pval[1] << 16) | (pval[2] << 8) | pval[3];
273 lsw = (pval[4] << 24) | (pval[5] << 16);
279 static int w90p910_init_desc(
struct net_device *dev)
287 ether = netdev_priv(dev);
295 dev_err(&pdev->
dev,
"Failed to allocate memory for tx desc\n");
304 dev_err(&pdev->
dev,
"Failed to allocate memory for rx desc\n");
313 tdesc = &(ether->
tdesc->desclist[
i]);
315 if (i == TX_DESC_SIZE - 1)
332 rdesc = &(ether->
rdesc->desclist[
i]);
334 if (i == RX_DESC_SIZE - 1)
350 static void w90p910_set_fifo_threshold(
struct net_device *dev)
359 static void w90p910_return_default_idle(
struct net_device *dev)
369 static void w90p910_trigger_rx(
struct net_device *dev)
376 static void w90p910_trigger_tx(
struct net_device *dev)
383 static void w90p910_enable_mac_interrupt(
struct net_device *dev)
394 static void w90p910_get_and_clear_int(
struct net_device *dev,
403 static void w90p910_set_global_maccmd(
struct net_device *dev)
413 static void w90p910_enable_cam(
struct net_device *dev)
425 static void w90p910_enable_cam_command(
struct net_device *dev)
449 static void w90p910_enable_rx(
struct net_device *dev,
unsigned int enable)
464 static void w90p910_set_curdest(
struct net_device *dev)
472 static void w90p910_reset_mac(
struct net_device *dev)
476 w90p910_enable_tx(dev, 0);
477 w90p910_enable_rx(dev, 0);
478 w90p910_set_fifo_threshold(dev);
479 w90p910_return_default_idle(dev);
481 if (!netif_queue_stopped(dev))
482 netif_stop_queue(dev);
484 w90p910_init_desc(dev);
491 w90p910_set_curdest(dev);
492 w90p910_enable_cam(dev);
493 w90p910_enable_cam_command(dev);
494 w90p910_enable_mac_interrupt(dev);
495 w90p910_enable_tx(dev, 1);
496 w90p910_enable_rx(dev, 1);
497 w90p910_trigger_tx(dev);
498 w90p910_trigger_rx(dev);
502 if (netif_queue_stopped(dev))
503 netif_wake_queue(dev);
506 static void w90p910_mdio_write(
struct net_device *dev,
517 val = (phy_id << 0x08) | reg;
521 for (i = 0; i <
DELAY; i++) {
530 static int w90p910_mdio_read(
struct net_device *dev,
int phy_id,
int reg)
538 val = (phy_id << 0x08) | reg;
542 for (i = 0; i <
DELAY; i++) {
557 static int w90p910_set_mac_address(
struct net_device *dev,
void *
addr)
561 if (!is_valid_ether_addr(address->
sa_data))
570 static int w90p910_ether_close(
struct net_device *dev)
582 netif_stop_queue(dev);
598 ether = netdev_priv(dev);
600 return ðer->
stats;
603 static int w90p910_send_frame(
struct net_device *dev,
604 unsigned char *data,
int length)
611 ether = netdev_priv(dev);
618 dev_err(&pdev->
dev,
"send data %d bytes, check it\n", length);
622 txbd->
sl = length & 0xFFFF;
624 memcpy(buffer, data, length);
628 w90p910_enable_tx(dev, 1);
630 w90p910_trigger_tx(dev);
632 if (++ether->
cur_tx >= TX_DESC_SIZE)
638 netif_stop_queue(dev);
647 if (!(w90p910_send_frame(dev, skb->
data, skb->
len))) {
655 static irqreturn_t w90p910_tx_interrupt(
int irq,
void *dev_id)
664 ether = netdev_priv(dev);
667 w90p910_get_and_clear_int(dev, &status);
674 while (entry != cur_entry) {
681 ether->
stats.tx_packets++;
682 ether->
stats.tx_bytes += txbd->
sl & 0xFFFF;
684 ether->
stats.tx_errors++;
690 if (netif_queue_stopped(dev))
691 netif_wake_queue(dev);
698 dev_err(&pdev->
dev,
"emc defer exceed interrupt\n");
700 dev_err(&pdev->
dev,
"emc bus error interrupt\n");
701 w90p910_reset_mac(dev);
703 if (netif_queue_stopped(dev))
704 netif_wake_queue(dev);
719 ether = netdev_priv(dev);
734 length = status & 0xFFFF;
738 skb = netdev_alloc_skb(dev, length + 2);
740 dev_err(&pdev->
dev,
"get skb buffer error\n");
741 ether->
stats.rx_dropped++;
747 skb_copy_to_linear_data(skb, data, length);
749 ether->
stats.rx_packets++;
753 ether->
stats.rx_errors++;
757 ether->
stats.rx_length_errors++;
760 ether->
stats.rx_crc_errors++;
763 ether->
stats.rx_frame_errors++;
766 ether->
stats.rx_over_errors++;
773 if (++ether->
cur_rx >= RX_DESC_SIZE)
781 static irqreturn_t w90p910_rx_interrupt(
int irq,
void *dev_id)
789 ether = netdev_priv(dev);
792 w90p910_get_and_clear_int(dev, &status);
796 w90p910_trigger_rx(dev);
801 w90p910_reset_mac(dev);
808 static int w90p910_ether_open(
struct net_device *dev)
813 ether = netdev_priv(dev);
816 w90p910_reset_mac(dev);
817 w90p910_set_fifo_threshold(dev);
818 w90p910_set_curdest(dev);
819 w90p910_enable_cam(dev);
820 w90p910_enable_cam_command(dev);
821 w90p910_enable_mac_interrupt(dev);
822 w90p910_set_global_maccmd(dev);
823 w90p910_enable_rx(dev, 1);
832 0x0, pdev->
name, dev)) {
833 dev_err(&pdev->
dev,
"register irq tx failed\n");
838 0x0, pdev->
name, dev)) {
839 dev_err(&pdev->
dev,
"register irq rx failed\n");
845 netif_start_queue(dev);
846 w90p910_trigger_rx(dev);
853 static void w90p910_ether_set_multicast_list(
struct net_device *dev)
856 unsigned int rx_mode;
858 ether = netdev_priv(dev);
869 static int w90p910_ether_ioctl(
struct net_device *dev,
878 static void w90p910_get_drvinfo(
struct net_device *dev,
897 static int w90p910_nway_reset(
struct net_device *dev)
909 static const struct ethtool_ops w90p910_ether_ethtool_ops = {
910 .get_settings = w90p910_get_settings,
911 .set_settings = w90p910_set_settings,
912 .get_drvinfo = w90p910_get_drvinfo,
913 .nway_reset = w90p910_nway_reset,
914 .get_link = w90p910_get_link,
918 .ndo_open = w90p910_ether_open,
919 .ndo_stop = w90p910_ether_close,
920 .ndo_start_xmit = w90p910_ether_start_xmit,
921 .ndo_get_stats = w90p910_ether_stats,
922 .ndo_set_rx_mode = w90p910_ether_set_multicast_list,
923 .ndo_set_mac_address = w90p910_set_mac_address,
924 .ndo_do_ioctl = w90p910_ether_ioctl,
944 if (is_valid_ether_addr(addr))
950 static int w90p910_ether_setup(
struct net_device *dev)
962 get_mac_address(dev);
968 ether->
mii.phy_id = 0x01;
969 ether->
mii.phy_id_mask = 0x1f;
970 ether->
mii.reg_num_mask = 0x1f;
972 ether->
mii.mdio_read = w90p910_mdio_read;
973 ether->
mii.mdio_write = w90p910_mdio_write;
991 ether = netdev_priv(dev);
995 dev_err(&pdev->
dev,
"failed to get I/O memory\n");
1001 resource_size(ether->
res), pdev->
name)) {
1002 dev_err(&pdev->
dev,
"failed to request I/O memory\n");
1009 dev_err(&pdev->
dev,
"failed to remap I/O memory\n");
1011 goto failed_free_mem;
1015 if (ether->
txirq < 0) {
1016 dev_err(&pdev->
dev,
"failed to get ether tx irq\n");
1018 goto failed_free_io;
1022 if (ether->
rxirq < 0) {
1023 dev_err(&pdev->
dev,
"failed to get ether rx irq\n");
1025 goto failed_free_txirq;
1028 platform_set_drvdata(pdev, dev);
1031 if (IS_ERR(ether->
clk)) {
1032 dev_err(&pdev->
dev,
"failed to get ether clock\n");
1033 error = PTR_ERR(ether->
clk);
1034 goto failed_free_rxirq;
1039 dev_err(&pdev->
dev,
"failed to get ether clock\n");
1040 error = PTR_ERR(ether->
rmiiclk);
1041 goto failed_put_clk;
1046 w90p910_ether_setup(dev);
1050 dev_err(&pdev->
dev,
"Regiter EMC w90p910 FAILED\n");
1052 goto failed_put_rmiiclk;
1062 platform_set_drvdata(pdev,
NULL);
1076 struct net_device *dev = platform_get_drvdata(pdev);
1091 platform_set_drvdata(pdev,
NULL);
1098 .probe = w90p910_ether_probe,
1101 .name =
"nuc900-emc",