12 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/mii.h>
20 #include <linux/ethtool.h>
27 #include <linux/slab.h>
29 #include <mach/hardware.h>
31 #define DRV_MODULE_NAME "ep93xx-eth"
32 #define DRV_MODULE_VERSION "0.1"
34 #define RX_QUEUE_ENTRIES 64
35 #define TX_QUEUE_ENTRIES 8
37 #define MAX_PKT_SIZE 2044
38 #define PKT_BUF_SIZE 2048
40 #define REG_RXCTL 0x0000
41 #define REG_RXCTL_DEFAULT 0x00073800
42 #define REG_TXCTL 0x0004
43 #define REG_TXCTL_ENABLE 0x00000001
44 #define REG_MIICMD 0x0010
45 #define REG_MIICMD_READ 0x00008000
46 #define REG_MIICMD_WRITE 0x00004000
47 #define REG_MIIDATA 0x0014
48 #define REG_MIISTS 0x0018
49 #define REG_MIISTS_BUSY 0x00000001
50 #define REG_SELFCTL 0x0020
51 #define REG_SELFCTL_RESET 0x00000001
52 #define REG_INTEN 0x0024
53 #define REG_INTEN_TX 0x00000008
54 #define REG_INTEN_RX 0x00000007
55 #define REG_INTSTSP 0x0028
56 #define REG_INTSTS_TX 0x00000008
57 #define REG_INTSTS_RX 0x00000004
58 #define REG_INTSTSC 0x002c
59 #define REG_AFP 0x004c
60 #define REG_INDAD0 0x0050
61 #define REG_INDAD1 0x0051
62 #define REG_INDAD2 0x0052
63 #define REG_INDAD3 0x0053
64 #define REG_INDAD4 0x0054
65 #define REG_INDAD5 0x0055
66 #define REG_GIINTMSK 0x0064
67 #define REG_GIINTMSK_ENABLE 0x00008000
68 #define REG_BMCTL 0x0080
69 #define REG_BMCTL_ENABLE_TX 0x00000100
70 #define REG_BMCTL_ENABLE_RX 0x00000001
71 #define REG_BMSTS 0x0084
72 #define REG_BMSTS_RX_ACTIVE 0x00000008
73 #define REG_RXDQBADD 0x0090
74 #define REG_RXDQBLEN 0x0094
75 #define REG_RXDCURADD 0x0098
76 #define REG_RXDENQ 0x009c
77 #define REG_RXSTSQBADD 0x00a0
78 #define REG_RXSTSQBLEN 0x00a4
79 #define REG_RXSTSQCURADD 0x00a8
80 #define REG_RXSTSENQ 0x00ac
81 #define REG_TXDQBADD 0x00b0
82 #define REG_TXDQBLEN 0x00b4
83 #define REG_TXDQCURADD 0x00b8
84 #define REG_TXDENQ 0x00bc
85 #define REG_TXSTSQBADD 0x00c0
86 #define REG_TXSTSQBLEN 0x00c4
87 #define REG_TXSTSQCURADD 0x00c8
88 #define REG_MAXFRMLEN 0x00e8
96 #define RDESC1_NSOF 0x80000000
97 #define RDESC1_BUFFER_INDEX 0x7fff0000
98 #define RDESC1_BUFFER_LENGTH 0x0000ffff
106 #define RSTAT0_RFP 0x80000000
107 #define RSTAT0_RWE 0x40000000
108 #define RSTAT0_EOF 0x20000000
109 #define RSTAT0_EOB 0x10000000
110 #define RSTAT0_AM 0x00c00000
111 #define RSTAT0_RX_ERR 0x00200000
112 #define RSTAT0_OE 0x00100000
113 #define RSTAT0_FE 0x00080000
114 #define RSTAT0_RUNT 0x00040000
115 #define RSTAT0_EDATA 0x00020000
116 #define RSTAT0_CRCE 0x00010000
117 #define RSTAT0_CRCI 0x00008000
118 #define RSTAT0_HTI 0x00003f00
119 #define RSTAT1_RFP 0x80000000
120 #define RSTAT1_BUFFER_INDEX 0x7fff0000
121 #define RSTAT1_FRAME_LENGTH 0x0000ffff
129 #define TDESC1_EOF 0x80000000
130 #define TDESC1_BUFFER_INDEX 0x7fff0000
131 #define TDESC1_BUFFER_ABORT 0x00008000
132 #define TDESC1_BUFFER_LENGTH 0x00000fff
139 #define TSTAT0_TXFP 0x80000000
140 #define TSTAT0_TXWE 0x40000000
141 #define TSTAT0_FA 0x20000000
142 #define TSTAT0_LCRS 0x10000000
143 #define TSTAT0_OW 0x04000000
144 #define TSTAT0_TXU 0x02000000
145 #define TSTAT0_ECOLL 0x01000000
146 #define TSTAT0_NCOLL 0x001f0000
147 #define TSTAT0_BUFFER_INDEX 0x00007fff
183 #define rdb(ep, off) __raw_readb((ep)->base_addr + (off))
184 #define rdw(ep, off) __raw_readw((ep)->base_addr + (off))
185 #define rdl(ep, off) __raw_readl((ep)->base_addr + (off))
186 #define wrb(ep, off, val) __raw_writeb((val), (ep)->base_addr + (off))
187 #define wrw(ep, off, val) __raw_writew((val), (ep)->base_addr + (off))
188 #define wrl(ep, off, val) __raw_writel((val), (ep)->base_addr + (off))
198 for (i = 0; i < 10; i++) {
205 pr_info(
"mdio read timed out\n");
222 for (i = 0; i < 10; i++) {
229 pr_info(
"mdio write timed out\n");
236 while (processed < budget) {
256 pr_crit(
"not end-of-frame %.8x %.8x\n", rstat0, rstat1);
258 pr_crit(
"not end-of-buffer %.8x %.8x\n", rstat0, rstat1);
260 pr_crit(
"entry mismatch %.8x %.8x\n", rstat0, rstat1);
263 dev->
stats.rx_errors++;
265 dev->
stats.rx_fifo_errors++;
267 dev->
stats.rx_frame_errors++;
269 dev->
stats.rx_length_errors++;
271 dev->
stats.rx_crc_errors++;
277 pr_notice(
"invalid length %.8x %.8x\n", rstat0, rstat1);
285 skb = netdev_alloc_skb(dev, length + 2);
291 skb_copy_to_linear_data(skb, ep->
rx_buf[entry], length);
300 dev->
stats.rx_packets++;
303 dev->
stats.rx_dropped++;
314 static int ep93xx_have_more_rx(
struct ep93xx_priv *ep)
327 rx = ep93xx_rx(dev, rx, budget);
334 if (ep93xx_have_more_rx(ep)) {
341 if (more && napi_reschedule(napi))
360 dev->
stats.tx_dropped++;
381 netif_stop_queue(dev);
389 static void ep93xx_tx_complete(
struct net_device *dev)
412 pr_crit(
"frame aborted %.8x\n", tstat0);
414 pr_crit(
"entry mismatch %.8x\n", tstat0);
417 int length = ep->
descs->tdesc[
entry].tdesc1 & 0xfff;
419 dev->
stats.tx_packets++;
422 dev->
stats.tx_errors++;
426 dev->
stats.tx_window_errors++;
428 dev->
stats.tx_fifo_errors++;
429 dev->
stats.collisions += (tstat0 >> 16) & 0x1f;
439 netif_wake_queue(dev);
462 ep93xx_tx_complete(dev);
467 static void ep93xx_free_buffers(
struct ep93xx_priv *ep)
469 struct device *dev = ep->
dev->dev.parent;
475 d = ep->
descs->rdesc[
i].buf_addr;
486 d = ep->
descs->tdesc[
i].buf_addr;
498 static int ep93xx_alloc_buffers(
struct ep93xx_priv *ep)
500 struct device *dev = ep->
dev->dev.parent;
523 ep->
descs->rdesc[
i].buf_addr =
d;
542 ep->
descs->tdesc[
i].buf_addr =
d;
548 ep93xx_free_buffers(ep);
552 static int ep93xx_start_hw(
struct net_device *dev)
559 for (i = 0; i < 10; i++) {
566 pr_crit(
"hw failed to reset\n");
573 if ((ep93xx_mdio_read(dev, ep->
mii.phy_id,
MII_BMSR) & 0x0040) != 0)
604 for (i = 0; i < 10; i++) {
611 pr_crit(
"hw failed to start\n");
634 static void ep93xx_stop_hw(
struct net_device *dev)
640 for (i = 0; i < 10; i++) {
647 pr_crit(
"hw failed to reset\n");
650 static int ep93xx_open(
struct net_device *dev)
655 if (ep93xx_alloc_buffers(ep))
658 napi_enable(&ep->
napi);
660 if (ep93xx_start_hw(dev)) {
661 napi_disable(&ep->
napi);
662 ep93xx_free_buffers(ep);
675 napi_disable(&ep->
napi);
677 ep93xx_free_buffers(ep);
683 netif_start_queue(dev);
688 static int ep93xx_close(
struct net_device *dev)
692 napi_disable(&ep->
napi);
693 netif_stop_queue(dev);
698 ep93xx_free_buffers(ep);
729 static int ep93xx_nway_reset(
struct net_device *dev)
741 static const struct ethtool_ops ep93xx_ethtool_ops = {
742 .get_drvinfo = ep93xx_get_drvinfo,
743 .get_settings = ep93xx_get_settings,
744 .set_settings = ep93xx_set_settings,
745 .nway_reset = ep93xx_nway_reset,
746 .get_link = ep93xx_get_link,
750 .ndo_open = ep93xx_open,
751 .ndo_stop = ep93xx_close,
752 .ndo_start_xmit = ep93xx_xmit,
753 .ndo_do_ioctl = ep93xx_ioctl,
783 dev = platform_get_drvdata(pdev);
786 platform_set_drvdata(pdev,
NULL);
788 ep = netdev_priv(dev);
792 ep93xx_free_buffers(ep);
818 data = pdev->
dev.platform_data;
825 dev = ep93xx_dev_alloc(data);
830 ep = netdev_priv(dev);
835 platform_set_drvdata(pdev, dev);
838 dev_name(&pdev->
dev));
840 dev_err(&pdev->
dev,
"Could not reserve memory region\n");
847 dev_err(&pdev->
dev,
"Failed to ioremap ethernet registers\n");
854 ep->
mii.phy_id_mask = 0x1f;
855 ep->
mii.reg_num_mask = 0x1f;
857 ep->
mii.mdio_read = ep93xx_mdio_read;
858 ep->
mii.mdio_write = ep93xx_mdio_write;
861 if (is_zero_ether_addr(dev->
dev_addr))
862 eth_hw_addr_random(dev);
866 dev_err(&pdev->
dev,
"Failed to register netdev\n");
876 ep93xx_eth_remove(pdev);
882 .probe = ep93xx_eth_probe,
883 .remove = ep93xx_eth_remove,
885 .name =
"ep93xx-eth",
890 static int __init ep93xx_eth_init_module(
void)
896 static void __exit ep93xx_eth_cleanup_module(
void)