Lines Matching +full:tx +full:- +full:device

1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Xilinx EmacLite Linux driver for the Xilinx Ethernet MAC Lite device.
7 * Copyright (c) 2007 - 2013 Xilinx, Inc.
37 #define XEL_TSR_OFFSET 0x07FC /* Tx status */
38 #define XEL_TPLR_OFFSET 0x07F4 /* Tx packet length */
44 #define XEL_BUFFER_OFFSET 0x0800 /* Next Tx/Rx buffer's offset */
66 #define XEL_TSR_XMIT_BUSY_MASK 0x00000001 /* Tx complete */
68 #define XEL_TSR_XMIT_IE_MASK 0x00000008 /* Tx interrupt enable bit */
82 #define XEL_TPLR_LENGTH_MASK 0x0000FFFF /* Tx packet length */
94 #define TX_TIMEOUT (60 * HZ) /* Tx timeout is 60 seconds. */
105 * struct net_local - Our private per device data
106 * @ndev: instance of the network device
107 * @tx_ping_pong: indicates whether Tx Pong buffer is configured in HW
109 * @next_tx_buf_to_use: next Tx buffer to write to
111 * @base_addr: base address of the Emaclite device
114 * Tx buffer is not free
115 * @phy_dev: pointer to the PHY device
116 * @phy_node: pointer to the PHY device node
145 * xemaclite_enable_interrupts - Enable the interrupts for the EmacLite device
146 * @drvdata: Pointer to the Emaclite device private data
148 * This function enables the Tx and Rx interrupts for the Emaclite device along
155 /* Enable the Tx interrupts for the first Buffer */ in xemaclite_enable_interrupts()
156 reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET); in xemaclite_enable_interrupts()
158 drvdata->base_addr + XEL_TSR_OFFSET); in xemaclite_enable_interrupts()
161 xemaclite_writel(XEL_RSR_RECV_IE_MASK, drvdata->base_addr + XEL_RSR_OFFSET); in xemaclite_enable_interrupts()
164 xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET); in xemaclite_enable_interrupts()
168 * xemaclite_disable_interrupts - Disable the interrupts for the EmacLite device
169 * @drvdata: Pointer to the Emaclite device private data
171 * This function disables the Tx and Rx interrupts for the Emaclite device,
179 xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET); in xemaclite_disable_interrupts()
181 /* Disable the Tx interrupts for the first buffer */ in xemaclite_disable_interrupts()
182 reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET); in xemaclite_disable_interrupts()
184 drvdata->base_addr + XEL_TSR_OFFSET); in xemaclite_disable_interrupts()
187 reg_data = xemaclite_readl(drvdata->base_addr + XEL_RSR_OFFSET); in xemaclite_disable_interrupts()
189 drvdata->base_addr + XEL_RSR_OFFSET); in xemaclite_disable_interrupts()
193 * xemaclite_aligned_write - Write from 16-bit aligned to 32-bit aligned address
194 * @src_ptr: Void pointer to the 16-bit aligned source address
195 * @dest_ptr: Pointer to the 32-bit aligned destination address
198 * This function writes data from a 16-bit aligned buffer to a 32-bit aligned
199 * address in the EmacLite device.
213 for (; length > 3; length -= 4) { in xemaclite_aligned_write()
237 for (; length > 0; length--) in xemaclite_aligned_write()
251 * xemaclite_aligned_read - Read from 32-bit aligned to 16-bit aligned buffer
252 * @src_ptr: Pointer to the 32-bit aligned source address
253 * @dest_ptr: Pointer to the 16-bit aligned destination address
256 * This function reads data from a 32-bit aligned address in the EmacLite device
257 * to a 16-bit aligned buffer.
269 for (; length > 3; length -= 4) { in xemaclite_aligned_read()
288 for (; length > 0; length--) in xemaclite_aligned_read()
294 * xemaclite_send_data - Send an Ethernet frame
295 * @drvdata: Pointer to the Emaclite device private data
299 * This function checks if the Tx buffer of the Emaclite device is free to send
300 * data. If so, it fills the Tx buffer with data for transmission. Otherwise, it
303 * Return: 0 upon success or -1 if the buffer(s) are full.
305 * Note: The maximum Tx packet size can not be more than Ethernet header
314 /* Determine the expected Tx buffer address */ in xemaclite_send_data()
315 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use; in xemaclite_send_data()
326 if (drvdata->tx_ping_pong != 0) in xemaclite_send_data()
327 drvdata->next_tx_buf_to_use ^= XEL_BUFFER_OFFSET; in xemaclite_send_data()
328 } else if (drvdata->tx_ping_pong != 0) { in xemaclite_send_data()
339 return -1; /* Buffers were full, return failure */ in xemaclite_send_data()
341 return -1; /* Buffer was full, return failure */ in xemaclite_send_data()
350 /* Update the Tx Status Register to indicate that there is a in xemaclite_send_data()
363 * xemaclite_recv_data - Receive a frame
364 * @drvdata: Pointer to the Emaclite device private data
380 addr = (drvdata->base_addr + drvdata->next_rx_buf_to_use); in xemaclite_recv_data()
386 if (drvdata->rx_ping_pong != 0) in xemaclite_recv_data()
387 drvdata->next_rx_buf_to_use ^= XEL_BUFFER_OFFSET; in xemaclite_recv_data()
394 if (drvdata->rx_ping_pong != 0) in xemaclite_recv_data()
443 /* Read from the EmacLite device */ in xemaclite_recv_data()
456 * xemaclite_update_address - Update the MAC address in the device
457 * @drvdata: Pointer to the Emaclite device private data
458 * @address_ptr:Pointer to the MAC address (MAC address is a 48-bit value)
460 * Tx must be idle and Rx should be idle for deterministic results.
462 * initialization and before transmission of any packets from the device.
472 /* Determine the expected Tx buffer address */ in xemaclite_update_address()
473 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use; in xemaclite_update_address()
490 * xemaclite_set_mac_address - Set the MAC address for this device
491 * @dev: Pointer to the network device instance
497 * Return: Error if the net device is busy or 0 if the addr is set
506 return -EBUSY; in xemaclite_set_mac_address()
508 eth_hw_addr_set(dev, addr->sa_data); in xemaclite_set_mac_address()
509 xemaclite_update_address(lp, dev->dev_addr); in xemaclite_set_mac_address()
514 * xemaclite_tx_timeout - Callback for Tx Timeout
515 * @dev: Pointer to the network device
518 * This function is called when Tx time out occurs for Emaclite device.
525 dev_err(&lp->ndev->dev, "Exceeded transmit timeout of %lu ms\n", in xemaclite_tx_timeout()
528 dev->stats.tx_errors++; in xemaclite_tx_timeout()
530 /* Reset the device */ in xemaclite_tx_timeout()
531 spin_lock_irqsave(&lp->reset_lock, flags); in xemaclite_tx_timeout()
539 if (lp->deferred_skb) { in xemaclite_tx_timeout()
540 dev_kfree_skb_irq(lp->deferred_skb); in xemaclite_tx_timeout()
541 lp->deferred_skb = NULL; in xemaclite_tx_timeout()
542 dev->stats.tx_errors++; in xemaclite_tx_timeout()
545 /* To exclude tx timeout */ in xemaclite_tx_timeout()
546 netif_trans_update(dev); /* prevent tx timeout */ in xemaclite_tx_timeout()
550 spin_unlock_irqrestore(&lp->reset_lock, flags); in xemaclite_tx_timeout()
558 * xemaclite_tx_handler - Interrupt handler for frames sent
559 * @dev: Pointer to the network device
568 dev->stats.tx_packets++; in xemaclite_tx_handler()
570 if (!lp->deferred_skb) in xemaclite_tx_handler()
573 if (xemaclite_send_data(lp, (u8 *)lp->deferred_skb->data, in xemaclite_tx_handler()
574 lp->deferred_skb->len)) in xemaclite_tx_handler()
577 dev->stats.tx_bytes += lp->deferred_skb->len; in xemaclite_tx_handler()
578 dev_consume_skb_irq(lp->deferred_skb); in xemaclite_tx_handler()
579 lp->deferred_skb = NULL; in xemaclite_tx_handler()
580 netif_trans_update(dev); /* prevent tx timeout */ in xemaclite_tx_handler()
585 * xemaclite_rx_handler- Interrupt handler for frames received
586 * @dev: Pointer to the network device
601 dev->stats.rx_dropped++; in xemaclite_rx_handler()
602 dev_err(&lp->ndev->dev, "Could not allocate receive buffer\n"); in xemaclite_rx_handler()
608 len = xemaclite_recv_data(lp, (u8 *)skb->data, len); in xemaclite_rx_handler()
611 dev->stats.rx_errors++; in xemaclite_rx_handler()
618 skb->protocol = eth_type_trans(skb, dev); in xemaclite_rx_handler()
621 dev->stats.rx_packets++; in xemaclite_rx_handler()
622 dev->stats.rx_bytes += len; in xemaclite_rx_handler()
629 * xemaclite_interrupt - Interrupt handler for this driver
630 * @irq: Irq of the Emaclite device
631 * @dev_id: Void pointer to the network device instance used as callback
636 * This function handles the Tx and Rx interrupts of the EmacLite device.
643 void __iomem *base_addr = lp->base_addr; in xemaclite_interrupt()
675 /* If there was a Tx interrupt, call the Tx Handler */ in xemaclite_interrupt()
687 * xemaclite_mdio_wait - Wait for the MDIO to be ready to use
688 * @lp: Pointer to the Emaclite device private data
690 * This function waits till the device is ready to accept a new MDIO
704 lp->base_addr + XEL_MDIOCTRL_OFFSET, in xemaclite_mdio_wait()
710 * xemaclite_mdio_read - Read from a given MII management register
715 * This function waits till the device is ready to accept a new MDIO
723 struct net_local *lp = bus->priv; in xemaclite_mdio_read()
728 return -ETIMEDOUT; in xemaclite_mdio_read()
734 ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET); in xemaclite_mdio_read()
737 lp->base_addr + XEL_MDIOADDR_OFFSET); in xemaclite_mdio_read()
739 lp->base_addr + XEL_MDIOCTRL_OFFSET); in xemaclite_mdio_read()
742 return -ETIMEDOUT; in xemaclite_mdio_read()
744 rc = xemaclite_readl(lp->base_addr + XEL_MDIORD_OFFSET); in xemaclite_mdio_read()
746 dev_dbg(&lp->ndev->dev, in xemaclite_mdio_read()
754 * xemaclite_mdio_write - Write to a given MII management register
760 * This function waits till the device is ready to accept a new MDIO
768 struct net_local *lp = bus->priv; in xemaclite_mdio_write()
771 dev_dbg(&lp->ndev->dev, in xemaclite_mdio_write()
776 return -ETIMEDOUT; in xemaclite_mdio_write()
783 ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET); in xemaclite_mdio_write()
786 lp->base_addr + XEL_MDIOADDR_OFFSET); in xemaclite_mdio_write()
787 xemaclite_writel(val, lp->base_addr + XEL_MDIOWR_OFFSET); in xemaclite_mdio_write()
789 lp->base_addr + XEL_MDIOCTRL_OFFSET); in xemaclite_mdio_write()
795 * xemaclite_mdio_setup - Register mii_bus for the Emaclite device
796 * @lp: Pointer to the Emaclite device private data
797 * @dev: Pointer to OF device structure
799 * This function enables MDIO bus in the Emaclite device and registers a
804 static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev) in xemaclite_mdio_setup()
808 struct device_node *np = of_get_parent(lp->phy_node); in xemaclite_mdio_setup()
817 return -ENODEV; in xemaclite_mdio_setup()
824 dev->of_node->full_name); in xemaclite_mdio_setup()
828 if (lp->ndev->mem_start != res.start) { in xemaclite_mdio_setup()
831 phydev = of_phy_find_device(lp->phy_node); in xemaclite_mdio_setup()
836 put_device(&phydev->mdio.dev); in xemaclite_mdio_setup()
845 lp->base_addr + XEL_MDIOCTRL_OFFSET); in xemaclite_mdio_setup()
851 return -ENOMEM; in xemaclite_mdio_setup()
854 snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx", in xemaclite_mdio_setup()
856 bus->priv = lp; in xemaclite_mdio_setup()
857 bus->name = "Xilinx Emaclite MDIO"; in xemaclite_mdio_setup()
858 bus->read = xemaclite_mdio_read; in xemaclite_mdio_setup()
859 bus->write = xemaclite_mdio_write; in xemaclite_mdio_setup()
860 bus->parent = dev; in xemaclite_mdio_setup()
869 lp->mii_bus = bus; in xemaclite_mdio_setup()
879 * xemaclite_adjust_link - Link state callback for the Emaclite device
882 * There's nothing in the Emaclite device to be configured when the link
888 struct phy_device *phy = lp->phy_dev; in xemaclite_adjust_link()
892 link_state = phy->speed | (phy->duplex << 1) | phy->link; in xemaclite_adjust_link()
894 if (lp->last_link != link_state) { in xemaclite_adjust_link()
895 lp->last_link = link_state; in xemaclite_adjust_link()
901 * xemaclite_open - Open the network device
902 * @dev: Pointer to the network device
905 * for the Emaclite device and starts the Tx queue.
906 * It also connects to the phy device, if MDIO is included in Emaclite device.
908 * Return: 0 on success. -ENODEV, if PHY cannot be connected.
909 * Non-zero error value on failure.
916 /* Just to be safe, stop the device first */ in xemaclite_open()
919 if (lp->phy_node) { in xemaclite_open()
920 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node, in xemaclite_open()
923 if (!lp->phy_dev) { in xemaclite_open()
924 dev_err(&lp->ndev->dev, "of_phy_connect() failed\n"); in xemaclite_open()
925 return -ENODEV; in xemaclite_open()
928 /* EmacLite doesn't support giga-bit speeds */ in xemaclite_open()
929 phy_set_max_speed(lp->phy_dev, SPEED_100); in xemaclite_open()
930 phy_start(lp->phy_dev); in xemaclite_open()
934 xemaclite_update_address(lp, dev->dev_addr); in xemaclite_open()
937 retval = request_irq(dev->irq, xemaclite_interrupt, 0, dev->name, dev); in xemaclite_open()
939 dev_err(&lp->ndev->dev, "Could not allocate interrupt %d\n", in xemaclite_open()
940 dev->irq); in xemaclite_open()
941 if (lp->phy_dev) in xemaclite_open()
942 phy_disconnect(lp->phy_dev); in xemaclite_open()
943 lp->phy_dev = NULL; in xemaclite_open()
958 * xemaclite_close - Close the network device
959 * @dev: Pointer to the network device
961 * This function stops the Tx queue, disables interrupts and frees the IRQ for
962 * the Emaclite device.
963 * It also disconnects the phy device associated with the Emaclite device.
973 free_irq(dev->irq, dev); in xemaclite_close()
975 if (lp->phy_dev) in xemaclite_close()
976 phy_disconnect(lp->phy_dev); in xemaclite_close()
977 lp->phy_dev = NULL; in xemaclite_close()
983 * xemaclite_send - Transmit a frame
985 * @dev: Pointer to the network device
987 * This function checks if the Tx buffer of the Emaclite device is free to send
988 * data. If so, it fills the Tx buffer with data from socket buffer data,
989 * updates the stats and frees the socket buffer. The Tx completion is signaled
990 * by an interrupt. If the Tx buffer isn't free, then the socket buffer is
991 * deferred and the Tx queue is stopped so that the deferred socket buffer can
992 * be transmitted when the Emaclite device is free to transmit data.
1004 len = orig_skb->len; in xemaclite_send()
1008 spin_lock_irqsave(&lp->reset_lock, flags); in xemaclite_send()
1009 if (xemaclite_send_data(lp, (u8 *)new_skb->data, len) != 0) { in xemaclite_send()
1010 /* If the Emaclite Tx buffer is busy, stop the Tx queue and in xemaclite_send()
1015 lp->deferred_skb = new_skb; in xemaclite_send()
1018 spin_unlock_irqrestore(&lp->reset_lock, flags); in xemaclite_send()
1021 spin_unlock_irqrestore(&lp->reset_lock, flags); in xemaclite_send()
1025 dev->stats.tx_bytes += len; in xemaclite_send()
1032 * get_bool - Get a parameter from the OF device
1033 * @ofdev: Pointer to OF device structure
1036 * This function looks for a property in the device node and returns the value
1043 u32 *p = (u32 *)of_get_property(ofdev->dev.of_node, s, NULL); in get_bool()
1046 dev_warn(&ofdev->dev, "Parameter %s not found, defaulting to false\n", s); in get_bool()
1054 * xemaclite_ethtools_get_drvinfo - Get various Axi Emac Lite driver info
1059 * Issue "ethtool -i ethX" under linux prompt to execute this function.
1064 strscpy(ed->driver, DRIVER_NAME, sizeof(ed->driver)); in xemaclite_ethtools_get_drvinfo()
1077 * xemaclite_of_probe - Probe method for the Emaclite device.
1078 * @ofdev: Pointer to OF device structure
1080 * This function probes for the Emaclite device in the device tree.
1082 * address and registers the network device.
1083 * It also registers a mii_bus for the Emaclite device, if MDIO is included
1084 * in the device.
1086 * Return: 0, if the driver is bound to the Emaclite device, or
1094 struct device *dev = &ofdev->dev; in xemaclite_of_probe()
1099 dev_info(dev, "Device Tree Probing\n"); in xemaclite_of_probe()
1101 /* Create an ethernet device instance */ in xemaclite_of_probe()
1104 return -ENOMEM; in xemaclite_of_probe()
1107 SET_NETDEV_DEV(ndev, &ofdev->dev); in xemaclite_of_probe()
1110 lp->ndev = ndev; in xemaclite_of_probe()
1112 /* Get IRQ for the device */ in xemaclite_of_probe()
1117 ndev->irq = rc; in xemaclite_of_probe()
1119 lp->base_addr = devm_platform_get_and_ioremap_resource(ofdev, 0, &res); in xemaclite_of_probe()
1120 if (IS_ERR(lp->base_addr)) in xemaclite_of_probe()
1121 return PTR_ERR(lp->base_addr); in xemaclite_of_probe()
1123 ndev->mem_start = res->start; in xemaclite_of_probe()
1124 ndev->mem_end = res->end; in xemaclite_of_probe()
1126 spin_lock_init(&lp->reset_lock); in xemaclite_of_probe()
1127 lp->next_tx_buf_to_use = 0x0; in xemaclite_of_probe()
1128 lp->next_rx_buf_to_use = 0x0; in xemaclite_of_probe()
1129 lp->tx_ping_pong = get_bool(ofdev, "xlnx,tx-ping-pong"); in xemaclite_of_probe()
1130 lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong"); in xemaclite_of_probe()
1132 clkin = devm_clk_get_optional_enabled(&ofdev->dev, NULL); in xemaclite_of_probe()
1134 return dev_err_probe(&ofdev->dev, PTR_ERR(clkin), in xemaclite_of_probe()
1135 "Failed to get and enable clock from Device Tree\n"); in xemaclite_of_probe()
1137 rc = of_get_ethdev_address(ofdev->dev.of_node, ndev); in xemaclite_of_probe()
1143 /* Clear the Tx CSR's in case this is a restart */ in xemaclite_of_probe()
1144 xemaclite_writel(0, lp->base_addr + XEL_TSR_OFFSET); in xemaclite_of_probe()
1145 xemaclite_writel(0, lp->base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET); in xemaclite_of_probe()
1147 /* Set the MAC address in the EmacLite device */ in xemaclite_of_probe()
1148 xemaclite_update_address(lp, ndev->dev_addr); in xemaclite_of_probe()
1150 lp->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0); in xemaclite_of_probe()
1151 xemaclite_mdio_setup(lp, &ofdev->dev); in xemaclite_of_probe()
1153 dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr); in xemaclite_of_probe()
1155 ndev->netdev_ops = &xemaclite_netdev_ops; in xemaclite_of_probe()
1156 ndev->ethtool_ops = &xemaclite_ethtool_ops; in xemaclite_of_probe()
1157 ndev->flags &= ~IFF_MULTICAST; in xemaclite_of_probe()
1158 ndev->watchdog_timeo = TX_TIMEOUT; in xemaclite_of_probe()
1160 /* Finally, register the device */ in xemaclite_of_probe()
1164 "Cannot register network device, aborting\n"); in xemaclite_of_probe()
1170 (unsigned long __force)ndev->mem_start, lp->base_addr, ndev->irq); in xemaclite_of_probe()
1174 of_node_put(lp->phy_node); in xemaclite_of_probe()
1179 * xemaclite_of_remove - Unbind the driver from the Emaclite device.
1180 * @of_dev: Pointer to OF device structure
1182 * This function is called if a device is physically removed from the system or
1184 * the device.
1192 /* Un-register the mii_bus, if configured */ in xemaclite_of_remove()
1193 if (lp->mii_bus) { in xemaclite_of_remove()
1194 mdiobus_unregister(lp->mii_bus); in xemaclite_of_remove()
1195 mdiobus_free(lp->mii_bus); in xemaclite_of_remove()
1196 lp->mii_bus = NULL; in xemaclite_of_remove()
1201 of_node_put(lp->phy_node); in xemaclite_of_remove()
1202 lp->phy_node = NULL; in xemaclite_of_remove()
1209 disable_irq(ndev->irq); in xemaclite_poll_controller()
1210 xemaclite_interrupt(ndev->irq, ndev); in xemaclite_poll_controller()
1211 enable_irq(ndev->irq); in xemaclite_poll_controller()
1218 if (!dev->phydev || !netif_running(dev)) in xemaclite_ioctl()
1219 return -EINVAL; in xemaclite_ioctl()
1225 return phy_mii_ioctl(dev->phydev, rq, cmd); in xemaclite_ioctl()
1227 return -EOPNOTSUPP; in xemaclite_ioctl()
1245 { .compatible = "xlnx,opb-ethernetlite-1.01.a", },
1246 { .compatible = "xlnx,opb-ethernetlite-1.01.b", },
1247 { .compatible = "xlnx,xps-ethernetlite-1.00.a", },
1248 { .compatible = "xlnx,xps-ethernetlite-2.00.a", },
1249 { .compatible = "xlnx,xps-ethernetlite-2.01.a", },
1250 { .compatible = "xlnx,xps-ethernetlite-3.00.a", },