19 #include <linux/mii.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
25 #include <linux/module.h>
28 static int buffer_size = 0x8000;
34 #define INT_SOURCE 0x04
39 #define PACKETLEN 0x18
41 #define TX_BD_NUM 0x20
42 #define CTRLMODER 0x24
44 #define MIICOMMAND 0x2c
45 #define MIIADDRESS 0x30
46 #define MIITX_DATA 0x34
47 #define MIIRX_DATA 0x38
48 #define MIISTATUS 0x3c
49 #define MAC_ADDR0 0x40
50 #define MAC_ADDR1 0x44
51 #define ETH_HASH0 0x48
52 #define ETH_HASH1 0x4c
53 #define ETH_TXCTRL 0x50
56 #define MODER_RXEN (1 << 0)
57 #define MODER_TXEN (1 << 1)
58 #define MODER_NOPRE (1 << 2)
59 #define MODER_BRO (1 << 3)
60 #define MODER_IAM (1 << 4)
61 #define MODER_PRO (1 << 5)
62 #define MODER_IFG (1 << 6)
63 #define MODER_LOOP (1 << 7)
64 #define MODER_NBO (1 << 8)
65 #define MODER_EDE (1 << 9)
66 #define MODER_FULLD (1 << 10)
67 #define MODER_RESET (1 << 11)
68 #define MODER_DCRC (1 << 12)
69 #define MODER_CRC (1 << 13)
70 #define MODER_HUGE (1 << 14)
71 #define MODER_PAD (1 << 15)
72 #define MODER_RSM (1 << 16)
75 #define INT_MASK_TXF (1 << 0)
76 #define INT_MASK_TXE (1 << 1)
77 #define INT_MASK_RXF (1 << 2)
78 #define INT_MASK_RXE (1 << 3)
79 #define INT_MASK_BUSY (1 << 4)
80 #define INT_MASK_TXC (1 << 5)
81 #define INT_MASK_RXC (1 << 6)
83 #define INT_MASK_TX (INT_MASK_TXF | INT_MASK_TXE)
84 #define INT_MASK_RX (INT_MASK_RXF | INT_MASK_RXE)
86 #define INT_MASK_ALL ( \
87 INT_MASK_TXF | INT_MASK_TXE | \
88 INT_MASK_RXF | INT_MASK_RXE | \
89 INT_MASK_TXC | INT_MASK_RXC | \
94 #define PACKETLEN_MIN(min) (((min) & 0xffff) << 16)
95 #define PACKETLEN_MAX(max) (((max) & 0xffff) << 0)
96 #define PACKETLEN_MIN_MAX(min, max) (PACKETLEN_MIN(min) | \
100 #define TX_BD_NUM_VAL(x) (((x) <= 0x80) ? (x) : 0x80)
103 #define CTRLMODER_PASSALL (1 << 0)
104 #define CTRLMODER_RXFLOW (1 << 1)
105 #define CTRLMODER_TXFLOW (1 << 2)
108 #define MIIMODER_CLKDIV(x) ((x) & 0xfe)
109 #define MIIMODER_NOPRE (1 << 8)
112 #define MIICOMMAND_SCAN (1 << 0)
113 #define MIICOMMAND_READ (1 << 1)
114 #define MIICOMMAND_WRITE (1 << 2)
117 #define MIIADDRESS_FIAD(x) (((x) & 0x1f) << 0)
118 #define MIIADDRESS_RGAD(x) (((x) & 0x1f) << 8)
119 #define MIIADDRESS_ADDR(phy, reg) (MIIADDRESS_FIAD(phy) | \
120 MIIADDRESS_RGAD(reg))
123 #define MIITX_DATA_VAL(x) ((x) & 0xffff)
126 #define MIIRX_DATA_VAL(x) ((x) & 0xffff)
129 #define MIISTATUS_LINKFAIL (1 << 0)
130 #define MIISTATUS_BUSY (1 << 1)
131 #define MIISTATUS_INVALID (1 << 2)
134 #define TX_BD_CS (1 << 0)
135 #define TX_BD_DF (1 << 1)
136 #define TX_BD_LC (1 << 2)
137 #define TX_BD_RL (1 << 3)
138 #define TX_BD_RETRY_MASK (0x00f0)
139 #define TX_BD_RETRY(x) (((x) & 0x00f0) >> 4)
140 #define TX_BD_UR (1 << 8)
141 #define TX_BD_CRC (1 << 11)
142 #define TX_BD_PAD (1 << 12)
143 #define TX_BD_WRAP (1 << 13)
144 #define TX_BD_IRQ (1 << 14)
145 #define TX_BD_READY (1 << 15)
146 #define TX_BD_LEN(x) (((x) & 0xffff) << 16)
147 #define TX_BD_LEN_MASK (0xffff << 16)
149 #define TX_BD_STATS (TX_BD_CS | TX_BD_DF | TX_BD_LC | \
150 TX_BD_RL | TX_BD_RETRY_MASK | TX_BD_UR)
153 #define RX_BD_LC (1 << 0)
154 #define RX_BD_CRC (1 << 1)
155 #define RX_BD_SF (1 << 2)
156 #define RX_BD_TL (1 << 3)
157 #define RX_BD_DN (1 << 4)
158 #define RX_BD_IS (1 << 5)
159 #define RX_BD_OR (1 << 6)
160 #define RX_BD_MISS (1 << 7)
161 #define RX_BD_CF (1 << 8)
162 #define RX_BD_WRAP (1 << 13)
163 #define RX_BD_IRQ (1 << 14)
164 #define RX_BD_EMPTY (1 << 15)
165 #define RX_BD_LEN(x) (((x) & 0xffff) << 16)
167 #define RX_BD_STATS (RX_BD_LC | RX_BD_CRC | RX_BD_SF | RX_BD_TL | \
168 RX_BD_DN | RX_BD_IS | RX_BD_OR | RX_BD_MISS)
170 #define ETHOC_BUFSIZ 1536
171 #define ETHOC_ZLEN 64
172 #define ETHOC_BD_BASE 0x400
173 #define ETHOC_TIMEOUT (HZ / 2)
174 #define ETHOC_MII_TIMEOUT (1 + (HZ / 5))
242 static inline void ethoc_read_bd(
struct ethoc *
dev,
int index,
246 bd->
stat = ethoc_read(dev, offset + 0);
247 bd->
addr = ethoc_read(dev, offset + 4);
250 static inline void ethoc_write_bd(
struct ethoc *
dev,
int index,
254 ethoc_write(dev, offset + 0, bd->
stat);
255 ethoc_write(dev, offset + 4, bd->
addr);
277 static inline void ethoc_enable_rx_and_tx(
struct ethoc *
dev)
281 ethoc_write(dev,
MODER, mode);
284 static inline void ethoc_disable_rx_and_tx(
struct ethoc *
dev)
288 ethoc_write(dev,
MODER, mode);
308 for (i = 0; i < dev->
num_tx; i++) {
312 ethoc_write_bd(dev, i, &bd);
321 for (i = 0; i < dev->
num_rx; i++) {
325 ethoc_write_bd(dev, dev->
num_tx + i, &bd);
335 static int ethoc_reset(
struct ethoc *dev)
341 ethoc_disable_rx_and_tx(dev);
346 mode = ethoc_read(dev,
MODER);
348 ethoc_write(dev,
MODER, mode);
351 mode = ethoc_read(dev,
MODER);
353 ethoc_write(dev,
MODER, mode);
354 ethoc_write(dev,
IPGT, 0x15);
358 ethoc_enable_rx_and_tx(dev);
362 static unsigned int ethoc_update_rx_stats(
struct ethoc *dev,
366 unsigned int ret = 0;
369 dev_err(&netdev->
dev,
"RX: frame too long\n");
370 netdev->
stats.rx_length_errors++;
375 dev_err(&netdev->
dev,
"RX: frame too short\n");
376 netdev->
stats.rx_length_errors++;
381 dev_err(&netdev->
dev,
"RX: dribble nibble\n");
382 netdev->
stats.rx_frame_errors++;
387 netdev->
stats.rx_crc_errors++;
393 netdev->
stats.rx_over_errors++;
398 netdev->
stats.rx_missed_errors++;
401 dev_err(&netdev->
dev,
"RX: late collision\n");
402 netdev->
stats.collisions++;
419 ethoc_read_bd(priv, entry, &bd);
429 ethoc_read_bd(priv, entry, &bd);
434 if (ethoc_update_rx_stats(priv, &bd) == 0) {
439 skb = netdev_alloc_skb_ip_align(dev, size);
445 dev->
stats.rx_packets++;
453 dev->
stats.rx_dropped++;
461 ethoc_write_bd(priv, entry, &bd);
469 static void ethoc_update_tx_stats(
struct ethoc *dev,
struct ethoc_bd *bd)
474 dev_err(&netdev->
dev,
"TX: late collision\n");
475 netdev->
stats.tx_window_errors++;
479 dev_err(&netdev->
dev,
"TX: retransmit limit\n");
480 netdev->
stats.tx_aborted_errors++;
485 netdev->
stats.tx_fifo_errors++;
489 dev_err(&netdev->
dev,
"TX: carrier sense lost\n");
490 netdev->
stats.tx_carrier_errors++;
494 netdev->
stats.tx_errors++;
496 netdev->
stats.collisions += (bd->
stat >> 4) & 0xf;
497 netdev->
stats.tx_bytes += bd->
stat >> 16;
498 netdev->
stats.tx_packets++;
501 static int ethoc_tx(
struct net_device *dev,
int limit)
503 struct ethoc *priv = netdev_priv(dev);
512 ethoc_read_bd(priv, entry, &bd);
523 ethoc_read_bd(priv, entry, &bd);
529 ethoc_update_tx_stats(priv, &bd);
534 netif_wake_queue(dev);
542 struct ethoc *priv = netdev_priv(dev);
562 ethoc_ack_irq(priv, pending);
567 dev->
stats.rx_dropped++;
573 napi_schedule(&priv->
napi);
579 static int ethoc_get_mac_address(
struct net_device *dev,
void *
addr)
581 struct ethoc *priv = netdev_priv(dev);
586 mac[2] = (reg >> 24) & 0xff;
587 mac[3] = (reg >> 16) & 0xff;
588 mac[4] = (reg >> 8) & 0xff;
589 mac[5] = (reg >> 0) & 0xff;
592 mac[0] = (reg >> 8) & 0xff;
593 mac[1] = (reg >> 0) & 0xff;
601 int rx_work_done = 0;
602 int tx_work_done = 0;
604 rx_work_done = ethoc_rx(priv->
netdev, budget);
605 tx_work_done = ethoc_tx(priv->
netdev, budget);
607 if (rx_work_done < budget && tx_work_done < budget) {
623 for (i=0; i < 5; i++) {
637 static int ethoc_mdio_write(
struct mii_bus *bus,
int phy,
int reg,
u16 val)
646 for (i=0; i < 5; i++) {
659 static int ethoc_mdio_reset(
struct mii_bus *bus)
664 static void ethoc_mdio_poll(
struct net_device *dev)
670 struct ethoc *priv = netdev_priv(dev);
688 dev_err(&dev->
dev,
"could not attach to PHY\n");
698 struct ethoc *priv = netdev_priv(dev);
709 if (netif_queue_stopped(dev)) {
711 netif_wake_queue(dev);
714 netif_start_queue(dev);
718 napi_enable(&priv->
napi);
721 dev_info(&dev->
dev,
"I/O: %08lx Memory: %08lx-%08lx\n",
730 struct ethoc *priv = netdev_priv(dev);
732 napi_disable(&priv->
napi);
737 ethoc_disable_rx_and_tx(priv);
740 if (!netif_queue_stopped(dev))
741 netif_stop_queue(dev);
748 struct ethoc *priv = netdev_priv(dev);
752 if (!netif_running(dev))
774 static int ethoc_set_mac_address(
struct net_device *dev,
void *
addr)
776 struct ethoc *priv = netdev_priv(dev);
779 if (!is_valid_ether_addr(mac))
782 ethoc_write(priv,
MAC_ADDR0, (mac[2] << 24) | (mac[3] << 16) |
783 (mac[4] << 8) | (mac[5] << 0));
784 ethoc_write(priv,
MAC_ADDR1, (mac[0] << 8) | (mac[1] << 0));
792 static void ethoc_set_multicast_list(
struct net_device *dev)
794 struct ethoc *priv = netdev_priv(dev);
817 ethoc_write(priv,
MODER, mode);
821 hash[0] = 0xffffffff;
822 hash[1] = 0xffffffff;
826 int bit = (crc >> 26) & 0x3f;
827 hash[bit >> 5] |= 1 << (bit & 0x1f);
835 static int ethoc_change_mtu(
struct net_device *dev,
int new_mtu)
840 static void ethoc_tx_timeout(
struct net_device *dev)
842 struct ethoc *priv = netdev_priv(dev);
845 ethoc_interrupt(dev->
irq, dev);
850 struct ethoc *priv = netdev_priv(dev);
856 dev->
stats.tx_errors++;
861 spin_lock_irq(&priv->
lock);
864 ethoc_read_bd(priv, entry, &bd);
875 ethoc_write_bd(priv, entry, &bd);
878 ethoc_write_bd(priv, entry, &bd);
882 netif_stop_queue(dev);
885 spin_unlock_irq(&priv->
lock);
886 skb_tx_timestamp(skb);
893 .ndo_open = ethoc_open,
894 .ndo_stop = ethoc_stop,
895 .ndo_do_ioctl = ethoc_ioctl,
896 .ndo_set_config = ethoc_config,
897 .ndo_set_mac_address = ethoc_set_mac_address,
898 .ndo_set_rx_mode = ethoc_set_multicast_list,
899 .ndo_change_mtu = ethoc_change_mtu,
900 .ndo_tx_timeout = ethoc_tx_timeout,
901 .ndo_start_xmit = ethoc_start_xmit,
918 bool random_mac =
false;
921 netdev = alloc_etherdev(
sizeof(
struct ethoc));
928 platform_set_drvdata(pdev, netdev);
933 dev_err(&pdev->
dev,
"cannot obtain I/O memory space\n");
939 resource_size(res), res->
name);
941 dev_err(&pdev->
dev,
"cannot request I/O memory space\n");
952 resource_size(res), res->
name);
954 dev_err(&pdev->
dev,
"cannot request memory space\n");
975 priv = netdev_priv(netdev);
981 resource_size(mmio));
983 dev_err(&pdev->
dev,
"cannot remap I/O memory space\n");
992 dev_err(&pdev->
dev,
"cannot remap memory space\n");
1002 dev_err(&pdev->
dev,
"cannot allocate %dB buffer\n",
1012 num_bd =
min_t(
unsigned int,
1022 dev_dbg(&pdev->
dev,
"ethoc: num_tx: %d num_rx: %d\n",
1032 if (pdev->
dev.platform_data) {
1044 "local-mac-address",
1054 if (!is_valid_ether_addr(netdev->
dev_addr))
1055 ethoc_get_mac_address(netdev, netdev->
dev_addr);
1059 if (!is_valid_ether_addr(netdev->
dev_addr)) {
1064 ret = ethoc_set_mac_address(netdev, netdev->
dev_addr);
1066 dev_err(&netdev->
dev,
"failed to set MAC address\n");
1074 priv->
mdio = mdiobus_alloc();
1080 priv->
mdio->name =
"ethoc-mdio";
1082 priv->
mdio->name, pdev->
id);
1083 priv->
mdio->read = ethoc_mdio_read;
1084 priv->
mdio->write = ethoc_mdio_write;
1085 priv->
mdio->reset = ethoc_mdio_reset;
1089 if (!priv->
mdio->irq) {
1099 dev_err(&netdev->
dev,
"failed to register MDIO bus\n");
1103 ret = ethoc_mdio_probe(netdev);
1105 dev_err(&netdev->
dev,
"failed to probe MDIO bus\n");
1123 dev_err(&netdev->
dev,
"failed to register interface\n");
1148 struct net_device *netdev = platform_get_drvdata(pdev);
1149 struct ethoc *priv = netdev_priv(netdev);
1151 platform_set_drvdata(pdev,
NULL);
1181 # define ethoc_suspend NULL
1182 # define ethoc_resume NULL
1186 { .compatible =
"opencores,ethoc", },
1192 .probe = ethoc_probe,
1199 .of_match_table = ethoc_match,