15 #include <linux/module.h>
18 #include <linux/netdevice.h>
22 #include <linux/slab.h>
31 #define DRIVER_NAME "xilinx_emaclite"
34 #define XEL_TXBUFF_OFFSET 0x0
35 #define XEL_MDIOADDR_OFFSET 0x07E4
36 #define XEL_MDIOWR_OFFSET 0x07E8
37 #define XEL_MDIORD_OFFSET 0x07EC
38 #define XEL_MDIOCTRL_OFFSET 0x07F0
39 #define XEL_GIER_OFFSET 0x07F8
40 #define XEL_TSR_OFFSET 0x07FC
41 #define XEL_TPLR_OFFSET 0x07F4
43 #define XEL_RXBUFF_OFFSET 0x1000
44 #define XEL_RPLR_OFFSET 0x100C
45 #define XEL_RSR_OFFSET 0x17FC
47 #define XEL_BUFFER_OFFSET 0x0800
50 #define XEL_MDIOADDR_REGADR_MASK 0x0000001F
51 #define XEL_MDIOADDR_PHYADR_MASK 0x000003E0
52 #define XEL_MDIOADDR_PHYADR_SHIFT 5
53 #define XEL_MDIOADDR_OP_MASK 0x00000400
56 #define XEL_MDIOWR_WRDATA_MASK 0x0000FFFF
59 #define XEL_MDIORD_RDDATA_MASK 0x0000FFFF
62 #define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001
63 #define XEL_MDIOCTRL_MDIOEN_MASK 0x00000008
66 #define XEL_GIER_GIE_MASK 0x80000000
69 #define XEL_TSR_XMIT_BUSY_MASK 0x00000001
70 #define XEL_TSR_PROGRAM_MASK 0x00000002
71 #define XEL_TSR_XMIT_IE_MASK 0x00000008
72 #define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000
77 #define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
80 #define XEL_RSR_RECV_DONE_MASK 0x00000001
81 #define XEL_RSR_RECV_IE_MASK 0x00000008
84 #define XEL_TPLR_LENGTH_MASK 0x0000FFFF
87 #define XEL_RPLR_LENGTH_MASK 0x0000FFFF
89 #define XEL_HEADER_OFFSET 12
90 #define XEL_HEADER_SHIFT 16
93 #define XEL_ARP_PACKET_SIZE 28
94 #define XEL_HEADER_IP_LENGTH_OFFSET 16
98 #define TX_TIMEOUT (60*HZ)
102 #define BUFFER_ALIGN(adr) ((ALIGNMENT - ((u32) adr)) % ALIGNMENT)
199 static void xemaclite_disable_interrupts(
struct net_local *drvdata)
247 static void xemaclite_aligned_write(
void *src_ptr,
u32 *dest_ptr,
252 u16 *from_u16_ptr, *to_u16_ptr;
254 to_u32_ptr = dest_ptr;
255 from_u16_ptr = src_ptr;
258 for (; length > 3; length -= 4) {
259 to_u16_ptr = (
u16 *)&align_buffer;
260 *to_u16_ptr++ = *from_u16_ptr++;
261 *to_u16_ptr++ = *from_u16_ptr++;
264 *to_u32_ptr++ = align_buffer;
267 u8 *from_u8_ptr, *to_u8_ptr;
271 to_u8_ptr = (
u8 *) &align_buffer;
272 from_u8_ptr = (
u8 *) from_u16_ptr;
275 for (; length > 0; length--)
276 *to_u8_ptr++ = *from_u8_ptr++;
278 *to_u32_ptr = align_buffer;
291 static void xemaclite_aligned_read(
u32 *src_ptr,
u8 *dest_ptr,
294 u16 *to_u16_ptr, *from_u16_ptr;
298 from_u32_ptr = src_ptr;
299 to_u16_ptr = (
u16 *) dest_ptr;
301 for (; length > 3; length -= 4) {
303 align_buffer = *from_u32_ptr++;
304 from_u16_ptr = (
u16 *)&align_buffer;
307 *to_u16_ptr++ = *from_u16_ptr++;
308 *to_u16_ptr++ = *from_u16_ptr++;
312 u8 *to_u8_ptr, *from_u8_ptr;
315 to_u8_ptr = (
u8 *) to_u16_ptr;
316 align_buffer = *from_u32_ptr++;
317 from_u8_ptr = (
u8 *) &align_buffer;
320 for (; length > 0; length--)
321 *to_u8_ptr = *from_u8_ptr;
341 unsigned int byte_count)
376 xemaclite_aligned_write(data, (
u32 __force *) addr, byte_count);
401 static u16 xemaclite_recv_data(
struct net_local *drvdata,
u8 *data)
422 addr = (
void __iomem __force *)((
u32 __force)addr ^
429 if ((reg_data & XEL_RSR_RECV_DONE_MASK) !=
430 XEL_RSR_RECV_DONE_MASK)
467 reg_data &= ~XEL_RSR_RECV_DONE_MASK;
484 static void xemaclite_update_address(
struct net_local *drvdata,
493 xemaclite_aligned_write(address_ptr, (
u32 __force *) addr,
ETH_ALEN);
523 if (netif_running(dev))
527 xemaclite_update_address(lp, dev->
dev_addr);
537 static void xemaclite_tx_timeout(
struct net_device *dev)
542 dev_err(&lp->
ndev->dev,
"Exceeded transmit timeout of %lu ms\n",
545 dev->
stats.tx_errors++;
551 netif_stop_queue(dev);
553 xemaclite_disable_interrupts(lp);
554 xemaclite_enable_interrupts(lp);
559 dev->
stats.tx_errors++;
566 netif_wake_queue(dev);
567 spin_unlock_irqrestore(&lp->
reset_lock, flags);
581 static void xemaclite_tx_handler(
struct net_device *dev)
585 dev->
stats.tx_packets++;
587 if (xemaclite_send_data(lp,
596 netif_wake_queue(dev);
608 static void xemaclite_rx_handler(
struct net_device *dev)
616 skb = netdev_alloc_skb(dev, len +
ALIGNMENT);
619 dev->
stats.rx_dropped++;
620 dev_err(&lp->
ndev->dev,
"Could not allocate receive buffer\n");
631 skb_reserve(skb, align);
635 len = xemaclite_recv_data(lp, (
u8 *) skb->
data);
638 dev->
stats.rx_errors++;
646 skb_checksum_none_assert(skb);
648 dev->
stats.rx_packets++;
665 bool tx_complete =
false;
674 & XEL_RSR_RECV_DONE_MASK))
676 xemaclite_rx_handler(dev);
683 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
691 if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
692 (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
694 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
702 if (tx_complete != 0)
703 xemaclite_tx_handler(dev);
722 static int xemaclite_mdio_wait(
struct net_local *lp)
731 if (end - jiffies <= 0) {
758 if (xemaclite_mdio_wait(lp))
770 ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK);
772 if (xemaclite_mdio_wait(lp))
778 "xemaclite_mdio_read(phy_id=%i, reg=%x) == %x\n",
794 static int xemaclite_mdio_write(
struct mii_bus *bus,
int phy_id,
int reg,
801 "xemaclite_mdio_write(phy_id=%i, reg=%x, val=%x)\n",
804 if (xemaclite_mdio_wait(lp))
818 ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK);
830 static int xemaclite_mdio_reset(
struct mii_bus *bus)
845 static int xemaclite_mdio_setup(
struct net_local *lp,
struct device *dev)
864 bus = mdiobus_alloc();
870 (
unsigned long long)
res.start);
872 bus->
name =
"Xilinx Emaclite MDIO";
873 bus->
read = xemaclite_mdio_read;
874 bus->
write = xemaclite_mdio_write;
875 bus->
reset = xemaclite_mdio_reset;
901 struct net_local *lp = netdev_priv(ndev);
922 static int xemaclite_open(
struct net_device *dev)
928 xemaclite_disable_interrupts(lp);
937 dev_err(&lp->
ndev->dev,
"of_phy_connect() failed\n");
960 xemaclite_update_address(lp, dev->
dev_addr);
965 dev_err(&lp->
ndev->dev,
"Could not allocate interrupt %d\n",
975 xemaclite_enable_interrupts(lp);
978 netif_start_queue(dev);
991 static int xemaclite_close(
struct net_device *dev)
995 netif_stop_queue(dev);
996 xemaclite_disable_interrupts(lp);
1022 struct net_local *lp = netdev_priv(dev);
1025 unsigned long flags;
1027 len = orig_skb->
len;
1032 if (xemaclite_send_data(lp, (
u8 *) new_skb->
data, len) != 0) {
1036 netif_stop_queue(dev);
1039 skb_tx_timestamp(new_skb);
1040 spin_unlock_irqrestore(&lp->
reset_lock, flags);
1043 spin_unlock_irqrestore(&lp->
reset_lock, flags);
1045 skb_tx_timestamp(new_skb);
1048 dev_kfree_skb(new_skb);
1060 static void xemaclite_remove_ndev(
struct net_device *ndev)
1063 struct net_local *lp = netdev_priv(ndev);
1089 "defaulting to false\n", s);
1121 dev_info(dev,
"Device Tree Probing\n");
1126 dev_err(dev,
"invalid address\n");
1133 dev_err(dev,
"no IRQ found\n");
1138 ndev = alloc_etherdev(
sizeof(
struct net_local));
1145 ndev->
irq = r_irq.start;
1149 lp = netdev_priv(ndev);
1155 dev_err(dev,
"Couldn't lock memory region at %p\n",
1164 dev_err(dev,
"EmacLite: Could not allocate iomem\n");
1172 lp->
tx_ping_pong = get_bool(ofdev,
"xlnx,tx-ping-pong");
1173 lp->
rx_ping_pong = get_bool(ofdev,
"xlnx,rx-ping-pong");
1180 dev_warn(dev,
"No MAC address found\n");
1187 xemaclite_update_address(lp, ndev->
dev_addr);
1190 rc = xemaclite_mdio_setup(lp, &ofdev->
dev);
1192 dev_warn(&ofdev->
dev,
"error registering MDIO bus\n");
1204 "Cannot register network device, aborting\n");
1209 "Xilinx EmacLite at 0x%08X mapped to 0x%08X, irq=%d\n",
1218 xemaclite_remove_ndev(ndev);
1237 struct net_local *lp = netdev_priv(ndev);
1255 xemaclite_remove_ndev(ndev);
1261 #ifdef CONFIG_NET_POLL_CONTROLLER
1263 xemaclite_poll_controller(
struct net_device *ndev)
1266 xemaclite_interrupt(ndev->
irq, ndev);
1272 .ndo_open = xemaclite_open,
1273 .ndo_stop = xemaclite_close,
1274 .ndo_start_xmit = xemaclite_send,
1275 .ndo_set_mac_address = xemaclite_set_mac_address,
1276 .ndo_tx_timeout = xemaclite_tx_timeout,
1277 #ifdef CONFIG_NET_POLL_CONTROLLER
1278 .ndo_poll_controller = xemaclite_poll_controller,
1284 { .compatible =
"xlnx,opb-ethernetlite-1.01.a", },
1285 { .compatible =
"xlnx,opb-ethernetlite-1.01.b", },
1286 { .compatible =
"xlnx,xps-ethernetlite-1.00.a", },
1287 { .compatible =
"xlnx,xps-ethernetlite-2.00.a", },
1288 { .compatible =
"xlnx,xps-ethernetlite-2.01.a", },
1289 { .compatible =
"xlnx,xps-ethernetlite-3.00.a", },
1298 .of_match_table = xemaclite_of_match,
1300 .probe = xemaclite_of_probe,