Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Xilinx EmacLite Linux driver for the Xilinx Ethernet MAC Lite device.
0003  *
0004  * This is a new flat driver which is based on the original emac_lite
0005  * driver from John Williams <john.williams@xilinx.com>.
0006  *
0007  * Copyright (c) 2007 - 2013 Xilinx, Inc.
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/uaccess.h>
0012 #include <linux/netdevice.h>
0013 #include <linux/etherdevice.h>
0014 #include <linux/skbuff.h>
0015 #include <linux/ethtool.h>
0016 #include <linux/io.h>
0017 #include <linux/slab.h>
0018 #include <linux/of_address.h>
0019 #include <linux/of_device.h>
0020 #include <linux/of_platform.h>
0021 #include <linux/of_mdio.h>
0022 #include <linux/of_net.h>
0023 #include <linux/phy.h>
0024 #include <linux/interrupt.h>
0025 #include <linux/iopoll.h>
0026 
0027 #define DRIVER_NAME "xilinx_emaclite"
0028 
0029 /* Register offsets for the EmacLite Core */
0030 #define XEL_TXBUFF_OFFSET   0x0     /* Transmit Buffer */
0031 #define XEL_MDIOADDR_OFFSET 0x07E4      /* MDIO Address Register */
0032 #define XEL_MDIOWR_OFFSET   0x07E8      /* MDIO Write Data Register */
0033 #define XEL_MDIORD_OFFSET   0x07EC      /* MDIO Read Data Register */
0034 #define XEL_MDIOCTRL_OFFSET 0x07F0      /* MDIO Control Register */
0035 #define XEL_GIER_OFFSET     0x07F8      /* GIE Register */
0036 #define XEL_TSR_OFFSET      0x07FC      /* Tx status */
0037 #define XEL_TPLR_OFFSET     0x07F4      /* Tx packet length */
0038 
0039 #define XEL_RXBUFF_OFFSET   0x1000      /* Receive Buffer */
0040 #define XEL_RPLR_OFFSET     0x100C      /* Rx packet length */
0041 #define XEL_RSR_OFFSET      0x17FC      /* Rx status */
0042 
0043 #define XEL_BUFFER_OFFSET   0x0800      /* Next Tx/Rx buffer's offset */
0044 
0045 /* MDIO Address Register Bit Masks */
0046 #define XEL_MDIOADDR_REGADR_MASK  0x0000001F    /* Register Address */
0047 #define XEL_MDIOADDR_PHYADR_MASK  0x000003E0    /* PHY Address */
0048 #define XEL_MDIOADDR_PHYADR_SHIFT 5
0049 #define XEL_MDIOADDR_OP_MASK      0x00000400    /* RD/WR Operation */
0050 
0051 /* MDIO Write Data Register Bit Masks */
0052 #define XEL_MDIOWR_WRDATA_MASK    0x0000FFFF    /* Data to be Written */
0053 
0054 /* MDIO Read Data Register Bit Masks */
0055 #define XEL_MDIORD_RDDATA_MASK    0x0000FFFF    /* Data to be Read */
0056 
0057 /* MDIO Control Register Bit Masks */
0058 #define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001    /* MDIO Status Mask */
0059 #define XEL_MDIOCTRL_MDIOEN_MASK  0x00000008    /* MDIO Enable */
0060 
0061 /* Global Interrupt Enable Register (GIER) Bit Masks */
0062 #define XEL_GIER_GIE_MASK   0x80000000  /* Global Enable */
0063 
0064 /* Transmit Status Register (TSR) Bit Masks */
0065 #define XEL_TSR_XMIT_BUSY_MASK   0x00000001 /* Tx complete */
0066 #define XEL_TSR_PROGRAM_MASK     0x00000002 /* Program the MAC address */
0067 #define XEL_TSR_XMIT_IE_MASK     0x00000008 /* Tx interrupt enable bit */
0068 #define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000 /* Buffer is active, SW bit
0069                          * only. This is not documented
0070                          * in the HW spec
0071                          */
0072 
0073 /* Define for programming the MAC address into the EmacLite */
0074 #define XEL_TSR_PROG_MAC_ADDR   (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
0075 
0076 /* Receive Status Register (RSR) */
0077 #define XEL_RSR_RECV_DONE_MASK  0x00000001  /* Rx complete */
0078 #define XEL_RSR_RECV_IE_MASK    0x00000008  /* Rx interrupt enable bit */
0079 
0080 /* Transmit Packet Length Register (TPLR) */
0081 #define XEL_TPLR_LENGTH_MASK    0x0000FFFF  /* Tx packet length */
0082 
0083 /* Receive Packet Length Register (RPLR) */
0084 #define XEL_RPLR_LENGTH_MASK    0x0000FFFF  /* Rx packet length */
0085 
0086 #define XEL_HEADER_OFFSET   12      /* Offset to length field */
0087 #define XEL_HEADER_SHIFT    16      /* Shift value for length */
0088 
0089 /* General Ethernet Definitions */
0090 #define XEL_ARP_PACKET_SIZE     28  /* Max ARP packet size */
0091 #define XEL_HEADER_IP_LENGTH_OFFSET 16  /* IP Length Offset */
0092 
0093 #define TX_TIMEOUT      (60 * HZ)   /* Tx timeout is 60 seconds. */
0094 
0095 #ifdef __BIG_ENDIAN
0096 #define xemaclite_readl     ioread32be
0097 #define xemaclite_writel    iowrite32be
0098 #else
0099 #define xemaclite_readl     ioread32
0100 #define xemaclite_writel    iowrite32
0101 #endif
0102 
0103 /**
0104  * struct net_local - Our private per device data
0105  * @ndev:       instance of the network device
0106  * @tx_ping_pong:   indicates whether Tx Pong buffer is configured in HW
0107  * @rx_ping_pong:   indicates whether Rx Pong buffer is configured in HW
0108  * @next_tx_buf_to_use: next Tx buffer to write to
0109  * @next_rx_buf_to_use: next Rx buffer to read from
0110  * @base_addr:      base address of the Emaclite device
0111  * @reset_lock:     lock used for synchronization
0112  * @deferred_skb:   holds an skb (for transmission at a later time) when the
0113  *          Tx buffer is not free
0114  * @phy_dev:        pointer to the PHY device
0115  * @phy_node:       pointer to the PHY device node
0116  * @mii_bus:        pointer to the MII bus
0117  * @last_link:      last link status
0118  */
0119 struct net_local {
0120     struct net_device *ndev;
0121 
0122     bool tx_ping_pong;
0123     bool rx_ping_pong;
0124     u32 next_tx_buf_to_use;
0125     u32 next_rx_buf_to_use;
0126     void __iomem *base_addr;
0127 
0128     spinlock_t reset_lock; /* serialize xmit and tx_timeout execution */
0129     struct sk_buff *deferred_skb;
0130 
0131     struct phy_device *phy_dev;
0132     struct device_node *phy_node;
0133 
0134     struct mii_bus *mii_bus;
0135 
0136     int last_link;
0137 };
0138 
0139 /*************************/
0140 /* EmacLite driver calls */
0141 /*************************/
0142 
0143 /**
0144  * xemaclite_enable_interrupts - Enable the interrupts for the EmacLite device
0145  * @drvdata:    Pointer to the Emaclite device private data
0146  *
0147  * This function enables the Tx and Rx interrupts for the Emaclite device along
0148  * with the Global Interrupt Enable.
0149  */
0150 static void xemaclite_enable_interrupts(struct net_local *drvdata)
0151 {
0152     u32 reg_data;
0153 
0154     /* Enable the Tx interrupts for the first Buffer */
0155     reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
0156     xemaclite_writel(reg_data | XEL_TSR_XMIT_IE_MASK,
0157              drvdata->base_addr + XEL_TSR_OFFSET);
0158 
0159     /* Enable the Rx interrupts for the first buffer */
0160     xemaclite_writel(XEL_RSR_RECV_IE_MASK, drvdata->base_addr + XEL_RSR_OFFSET);
0161 
0162     /* Enable the Global Interrupt Enable */
0163     xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
0164 }
0165 
0166 /**
0167  * xemaclite_disable_interrupts - Disable the interrupts for the EmacLite device
0168  * @drvdata:    Pointer to the Emaclite device private data
0169  *
0170  * This function disables the Tx and Rx interrupts for the Emaclite device,
0171  * along with the Global Interrupt Enable.
0172  */
0173 static void xemaclite_disable_interrupts(struct net_local *drvdata)
0174 {
0175     u32 reg_data;
0176 
0177     /* Disable the Global Interrupt Enable */
0178     xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
0179 
0180     /* Disable the Tx interrupts for the first buffer */
0181     reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
0182     xemaclite_writel(reg_data & (~XEL_TSR_XMIT_IE_MASK),
0183              drvdata->base_addr + XEL_TSR_OFFSET);
0184 
0185     /* Disable the Rx interrupts for the first buffer */
0186     reg_data = xemaclite_readl(drvdata->base_addr + XEL_RSR_OFFSET);
0187     xemaclite_writel(reg_data & (~XEL_RSR_RECV_IE_MASK),
0188              drvdata->base_addr + XEL_RSR_OFFSET);
0189 }
0190 
0191 /**
0192  * xemaclite_aligned_write - Write from 16-bit aligned to 32-bit aligned address
0193  * @src_ptr:    Void pointer to the 16-bit aligned source address
0194  * @dest_ptr:   Pointer to the 32-bit aligned destination address
0195  * @length: Number bytes to write from source to destination
0196  *
0197  * This function writes data from a 16-bit aligned buffer to a 32-bit aligned
0198  * address in the EmacLite device.
0199  */
0200 static void xemaclite_aligned_write(const void *src_ptr, u32 *dest_ptr,
0201                     unsigned int length)
0202 {
0203     const u16 *from_u16_ptr;
0204     u32 align_buffer;
0205     u32 *to_u32_ptr;
0206     u16 *to_u16_ptr;
0207 
0208     to_u32_ptr = dest_ptr;
0209     from_u16_ptr = src_ptr;
0210     align_buffer = 0;
0211 
0212     for (; length > 3; length -= 4) {
0213         to_u16_ptr = (u16 *)&align_buffer;
0214         *to_u16_ptr++ = *from_u16_ptr++;
0215         *to_u16_ptr++ = *from_u16_ptr++;
0216 
0217         /* This barrier resolves occasional issues seen around
0218          * cases where the data is not properly flushed out
0219          * from the processor store buffers to the destination
0220          * memory locations.
0221          */
0222         wmb();
0223 
0224         /* Output a word */
0225         *to_u32_ptr++ = align_buffer;
0226     }
0227     if (length) {
0228         u8 *from_u8_ptr, *to_u8_ptr;
0229 
0230         /* Set up to output the remaining data */
0231         align_buffer = 0;
0232         to_u8_ptr = (u8 *)&align_buffer;
0233         from_u8_ptr = (u8 *)from_u16_ptr;
0234 
0235         /* Output the remaining data */
0236         for (; length > 0; length--)
0237             *to_u8_ptr++ = *from_u8_ptr++;
0238 
0239         /* This barrier resolves occasional issues seen around
0240          * cases where the data is not properly flushed out
0241          * from the processor store buffers to the destination
0242          * memory locations.
0243          */
0244         wmb();
0245         *to_u32_ptr = align_buffer;
0246     }
0247 }
0248 
0249 /**
0250  * xemaclite_aligned_read - Read from 32-bit aligned to 16-bit aligned buffer
0251  * @src_ptr:    Pointer to the 32-bit aligned source address
0252  * @dest_ptr:   Pointer to the 16-bit aligned destination address
0253  * @length: Number bytes to read from source to destination
0254  *
0255  * This function reads data from a 32-bit aligned address in the EmacLite device
0256  * to a 16-bit aligned buffer.
0257  */
0258 static void xemaclite_aligned_read(u32 *src_ptr, u8 *dest_ptr,
0259                    unsigned int length)
0260 {
0261     u16 *to_u16_ptr, *from_u16_ptr;
0262     u32 *from_u32_ptr;
0263     u32 align_buffer;
0264 
0265     from_u32_ptr = src_ptr;
0266     to_u16_ptr = (u16 *)dest_ptr;
0267 
0268     for (; length > 3; length -= 4) {
0269         /* Copy each word into the temporary buffer */
0270         align_buffer = *from_u32_ptr++;
0271         from_u16_ptr = (u16 *)&align_buffer;
0272 
0273         /* Read data from source */
0274         *to_u16_ptr++ = *from_u16_ptr++;
0275         *to_u16_ptr++ = *from_u16_ptr++;
0276     }
0277 
0278     if (length) {
0279         u8 *to_u8_ptr, *from_u8_ptr;
0280 
0281         /* Set up to read the remaining data */
0282         to_u8_ptr = (u8 *)to_u16_ptr;
0283         align_buffer = *from_u32_ptr++;
0284         from_u8_ptr = (u8 *)&align_buffer;
0285 
0286         /* Read the remaining data */
0287         for (; length > 0; length--)
0288             *to_u8_ptr = *from_u8_ptr;
0289     }
0290 }
0291 
0292 /**
0293  * xemaclite_send_data - Send an Ethernet frame
0294  * @drvdata:    Pointer to the Emaclite device private data
0295  * @data:   Pointer to the data to be sent
0296  * @byte_count: Total frame size, including header
0297  *
0298  * This function checks if the Tx buffer of the Emaclite device is free to send
0299  * data. If so, it fills the Tx buffer with data for transmission. Otherwise, it
0300  * returns an error.
0301  *
0302  * Return:  0 upon success or -1 if the buffer(s) are full.
0303  *
0304  * Note:    The maximum Tx packet size can not be more than Ethernet header
0305  *      (14 Bytes) + Maximum MTU (1500 bytes). This is excluding FCS.
0306  */
0307 static int xemaclite_send_data(struct net_local *drvdata, u8 *data,
0308                    unsigned int byte_count)
0309 {
0310     u32 reg_data;
0311     void __iomem *addr;
0312 
0313     /* Determine the expected Tx buffer address */
0314     addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
0315 
0316     /* If the length is too large, truncate it */
0317     if (byte_count > ETH_FRAME_LEN)
0318         byte_count = ETH_FRAME_LEN;
0319 
0320     /* Check if the expected buffer is available */
0321     reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
0322     if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
0323          XEL_TSR_XMIT_ACTIVE_MASK)) == 0) {
0324         /* Switch to next buffer if configured */
0325         if (drvdata->tx_ping_pong != 0)
0326             drvdata->next_tx_buf_to_use ^= XEL_BUFFER_OFFSET;
0327     } else if (drvdata->tx_ping_pong != 0) {
0328         /* If the expected buffer is full, try the other buffer,
0329          * if it is configured in HW
0330          */
0331 
0332         addr = (void __iomem __force *)((uintptr_t __force)addr ^
0333                          XEL_BUFFER_OFFSET);
0334         reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
0335 
0336         if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
0337              XEL_TSR_XMIT_ACTIVE_MASK)) != 0)
0338             return -1; /* Buffers were full, return failure */
0339     } else {
0340         return -1; /* Buffer was full, return failure */
0341     }
0342 
0343     /* Write the frame to the buffer */
0344     xemaclite_aligned_write(data, (u32 __force *)addr, byte_count);
0345 
0346     xemaclite_writel((byte_count & XEL_TPLR_LENGTH_MASK),
0347              addr + XEL_TPLR_OFFSET);
0348 
0349     /* Update the Tx Status Register to indicate that there is a
0350      * frame to send. Set the XEL_TSR_XMIT_ACTIVE_MASK flag which
0351      * is used by the interrupt handler to check whether a frame
0352      * has been transmitted
0353      */
0354     reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
0355     reg_data |= (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_XMIT_ACTIVE_MASK);
0356     xemaclite_writel(reg_data, addr + XEL_TSR_OFFSET);
0357 
0358     return 0;
0359 }
0360 
0361 /**
0362  * xemaclite_recv_data - Receive a frame
0363  * @drvdata:    Pointer to the Emaclite device private data
0364  * @data:   Address where the data is to be received
0365  * @maxlen:    Maximum supported ethernet packet length
0366  *
0367  * This function is intended to be called from the interrupt context or
0368  * with a wrapper which waits for the receive frame to be available.
0369  *
0370  * Return:  Total number of bytes received
0371  */
0372 static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data, int maxlen)
0373 {
0374     void __iomem *addr;
0375     u16 length, proto_type;
0376     u32 reg_data;
0377 
0378     /* Determine the expected buffer address */
0379     addr = (drvdata->base_addr + drvdata->next_rx_buf_to_use);
0380 
0381     /* Verify which buffer has valid data */
0382     reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
0383 
0384     if ((reg_data & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
0385         if (drvdata->rx_ping_pong != 0)
0386             drvdata->next_rx_buf_to_use ^= XEL_BUFFER_OFFSET;
0387     } else {
0388         /* The instance is out of sync, try other buffer if other
0389          * buffer is configured, return 0 otherwise. If the instance is
0390          * out of sync, do not update the 'next_rx_buf_to_use' since it
0391          * will correct on subsequent calls
0392          */
0393         if (drvdata->rx_ping_pong != 0)
0394             addr = (void __iomem __force *)
0395                 ((uintptr_t __force)addr ^
0396                  XEL_BUFFER_OFFSET);
0397         else
0398             return 0;   /* No data was available */
0399 
0400         /* Verify that buffer has valid data */
0401         reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
0402         if ((reg_data & XEL_RSR_RECV_DONE_MASK) !=
0403              XEL_RSR_RECV_DONE_MASK)
0404             return 0;   /* No data was available */
0405     }
0406 
0407     /* Get the protocol type of the ethernet frame that arrived
0408      */
0409     proto_type = ((ntohl(xemaclite_readl(addr + XEL_HEADER_OFFSET +
0410             XEL_RXBUFF_OFFSET)) >> XEL_HEADER_SHIFT) &
0411             XEL_RPLR_LENGTH_MASK);
0412 
0413     /* Check if received ethernet frame is a raw ethernet frame
0414      * or an IP packet or an ARP packet
0415      */
0416     if (proto_type > ETH_DATA_LEN) {
0417         if (proto_type == ETH_P_IP) {
0418             length = ((ntohl(xemaclite_readl(addr +
0419                     XEL_HEADER_IP_LENGTH_OFFSET +
0420                     XEL_RXBUFF_OFFSET)) >>
0421                     XEL_HEADER_SHIFT) &
0422                     XEL_RPLR_LENGTH_MASK);
0423             length = min_t(u16, length, ETH_DATA_LEN);
0424             length += ETH_HLEN + ETH_FCS_LEN;
0425 
0426         } else if (proto_type == ETH_P_ARP) {
0427             length = XEL_ARP_PACKET_SIZE + ETH_HLEN + ETH_FCS_LEN;
0428         } else {
0429             /* Field contains type other than IP or ARP, use max
0430              * frame size and let user parse it
0431              */
0432             length = ETH_FRAME_LEN + ETH_FCS_LEN;
0433         }
0434     } else {
0435         /* Use the length in the frame, plus the header and trailer */
0436         length = proto_type + ETH_HLEN + ETH_FCS_LEN;
0437     }
0438 
0439     if (WARN_ON(length > maxlen))
0440         length = maxlen;
0441 
0442     /* Read from the EmacLite device */
0443     xemaclite_aligned_read((u32 __force *)(addr + XEL_RXBUFF_OFFSET),
0444                    data, length);
0445 
0446     /* Acknowledge the frame */
0447     reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
0448     reg_data &= ~XEL_RSR_RECV_DONE_MASK;
0449     xemaclite_writel(reg_data, addr + XEL_RSR_OFFSET);
0450 
0451     return length;
0452 }
0453 
0454 /**
0455  * xemaclite_update_address - Update the MAC address in the device
0456  * @drvdata:    Pointer to the Emaclite device private data
0457  * @address_ptr:Pointer to the MAC address (MAC address is a 48-bit value)
0458  *
0459  * Tx must be idle and Rx should be idle for deterministic results.
0460  * It is recommended that this function should be called after the
0461  * initialization and before transmission of any packets from the device.
0462  * The MAC address can be programmed using any of the two transmit
0463  * buffers (if configured).
0464  */
0465 static void xemaclite_update_address(struct net_local *drvdata,
0466                      const u8 *address_ptr)
0467 {
0468     void __iomem *addr;
0469     u32 reg_data;
0470 
0471     /* Determine the expected Tx buffer address */
0472     addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
0473 
0474     xemaclite_aligned_write(address_ptr, (u32 __force *)addr, ETH_ALEN);
0475 
0476     xemaclite_writel(ETH_ALEN, addr + XEL_TPLR_OFFSET);
0477 
0478     /* Update the MAC address in the EmacLite */
0479     reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
0480     xemaclite_writel(reg_data | XEL_TSR_PROG_MAC_ADDR, addr + XEL_TSR_OFFSET);
0481 
0482     /* Wait for EmacLite to finish with the MAC address update */
0483     while ((xemaclite_readl(addr + XEL_TSR_OFFSET) &
0484         XEL_TSR_PROG_MAC_ADDR) != 0)
0485         ;
0486 }
0487 
0488 /**
0489  * xemaclite_set_mac_address - Set the MAC address for this device
0490  * @dev:    Pointer to the network device instance
0491  * @address:    Void pointer to the sockaddr structure
0492  *
0493  * This function copies the HW address from the sockaddr structure to the
0494  * net_device structure and updates the address in HW.
0495  *
0496  * Return:  Error if the net device is busy or 0 if the addr is set
0497  *      successfully
0498  */
0499 static int xemaclite_set_mac_address(struct net_device *dev, void *address)
0500 {
0501     struct net_local *lp = netdev_priv(dev);
0502     struct sockaddr *addr = address;
0503 
0504     if (netif_running(dev))
0505         return -EBUSY;
0506 
0507     eth_hw_addr_set(dev, addr->sa_data);
0508     xemaclite_update_address(lp, dev->dev_addr);
0509     return 0;
0510 }
0511 
0512 /**
0513  * xemaclite_tx_timeout - Callback for Tx Timeout
0514  * @dev:    Pointer to the network device
0515  * @txqueue:    Unused
0516  *
0517  * This function is called when Tx time out occurs for Emaclite device.
0518  */
0519 static void xemaclite_tx_timeout(struct net_device *dev, unsigned int txqueue)
0520 {
0521     struct net_local *lp = netdev_priv(dev);
0522     unsigned long flags;
0523 
0524     dev_err(&lp->ndev->dev, "Exceeded transmit timeout of %lu ms\n",
0525         TX_TIMEOUT * 1000UL / HZ);
0526 
0527     dev->stats.tx_errors++;
0528 
0529     /* Reset the device */
0530     spin_lock_irqsave(&lp->reset_lock, flags);
0531 
0532     /* Shouldn't really be necessary, but shouldn't hurt */
0533     netif_stop_queue(dev);
0534 
0535     xemaclite_disable_interrupts(lp);
0536     xemaclite_enable_interrupts(lp);
0537 
0538     if (lp->deferred_skb) {
0539         dev_kfree_skb(lp->deferred_skb);
0540         lp->deferred_skb = NULL;
0541         dev->stats.tx_errors++;
0542     }
0543 
0544     /* To exclude tx timeout */
0545     netif_trans_update(dev); /* prevent tx timeout */
0546 
0547     /* We're all ready to go. Start the queue */
0548     netif_wake_queue(dev);
0549     spin_unlock_irqrestore(&lp->reset_lock, flags);
0550 }
0551 
0552 /**********************/
0553 /* Interrupt Handlers */
0554 /**********************/
0555 
0556 /**
0557  * xemaclite_tx_handler - Interrupt handler for frames sent
0558  * @dev:    Pointer to the network device
0559  *
0560  * This function updates the number of packets transmitted and handles the
0561  * deferred skb, if there is one.
0562  */
0563 static void xemaclite_tx_handler(struct net_device *dev)
0564 {
0565     struct net_local *lp = netdev_priv(dev);
0566 
0567     dev->stats.tx_packets++;
0568 
0569     if (!lp->deferred_skb)
0570         return;
0571 
0572     if (xemaclite_send_data(lp, (u8 *)lp->deferred_skb->data,
0573                 lp->deferred_skb->len))
0574         return;
0575 
0576     dev->stats.tx_bytes += lp->deferred_skb->len;
0577     dev_consume_skb_irq(lp->deferred_skb);
0578     lp->deferred_skb = NULL;
0579     netif_trans_update(dev); /* prevent tx timeout */
0580     netif_wake_queue(dev);
0581 }
0582 
0583 /**
0584  * xemaclite_rx_handler- Interrupt handler for frames received
0585  * @dev:    Pointer to the network device
0586  *
0587  * This function allocates memory for a socket buffer, fills it with data
0588  * received and hands it over to the TCP/IP stack.
0589  */
0590 static void xemaclite_rx_handler(struct net_device *dev)
0591 {
0592     struct net_local *lp = netdev_priv(dev);
0593     struct sk_buff *skb;
0594     u32 len;
0595 
0596     len = ETH_FRAME_LEN + ETH_FCS_LEN;
0597     skb = netdev_alloc_skb(dev, len + NET_IP_ALIGN);
0598     if (!skb) {
0599         /* Couldn't get memory. */
0600         dev->stats.rx_dropped++;
0601         dev_err(&lp->ndev->dev, "Could not allocate receive buffer\n");
0602         return;
0603     }
0604 
0605     skb_reserve(skb, NET_IP_ALIGN);
0606 
0607     len = xemaclite_recv_data(lp, (u8 *)skb->data, len);
0608 
0609     if (!len) {
0610         dev->stats.rx_errors++;
0611         dev_kfree_skb_irq(skb);
0612         return;
0613     }
0614 
0615     skb_put(skb, len);  /* Tell the skb how much data we got */
0616 
0617     skb->protocol = eth_type_trans(skb, dev);
0618     skb_checksum_none_assert(skb);
0619 
0620     dev->stats.rx_packets++;
0621     dev->stats.rx_bytes += len;
0622 
0623     if (!skb_defer_rx_timestamp(skb))
0624         netif_rx(skb);  /* Send the packet upstream */
0625 }
0626 
0627 /**
0628  * xemaclite_interrupt - Interrupt handler for this driver
0629  * @irq:    Irq of the Emaclite device
0630  * @dev_id: Void pointer to the network device instance used as callback
0631  *      reference
0632  *
0633  * Return:  IRQ_HANDLED
0634  *
0635  * This function handles the Tx and Rx interrupts of the EmacLite device.
0636  */
0637 static irqreturn_t xemaclite_interrupt(int irq, void *dev_id)
0638 {
0639     bool tx_complete = false;
0640     struct net_device *dev = dev_id;
0641     struct net_local *lp = netdev_priv(dev);
0642     void __iomem *base_addr = lp->base_addr;
0643     u32 tx_status;
0644 
0645     /* Check if there is Rx Data available */
0646     if ((xemaclite_readl(base_addr + XEL_RSR_OFFSET) &
0647              XEL_RSR_RECV_DONE_MASK) ||
0648         (xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_RSR_OFFSET)
0649              & XEL_RSR_RECV_DONE_MASK))
0650 
0651         xemaclite_rx_handler(dev);
0652 
0653     /* Check if the Transmission for the first buffer is completed */
0654     tx_status = xemaclite_readl(base_addr + XEL_TSR_OFFSET);
0655     if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
0656         (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
0657         tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
0658         xemaclite_writel(tx_status, base_addr + XEL_TSR_OFFSET);
0659 
0660         tx_complete = true;
0661     }
0662 
0663     /* Check if the Transmission for the second buffer is completed */
0664     tx_status = xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
0665     if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
0666         (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
0667         tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
0668         xemaclite_writel(tx_status, base_addr + XEL_BUFFER_OFFSET +
0669                  XEL_TSR_OFFSET);
0670 
0671         tx_complete = true;
0672     }
0673 
0674     /* If there was a Tx interrupt, call the Tx Handler */
0675     if (tx_complete != 0)
0676         xemaclite_tx_handler(dev);
0677 
0678     return IRQ_HANDLED;
0679 }
0680 
0681 /**********************/
0682 /* MDIO Bus functions */
0683 /**********************/
0684 
0685 /**
0686  * xemaclite_mdio_wait - Wait for the MDIO to be ready to use
0687  * @lp:     Pointer to the Emaclite device private data
0688  *
0689  * This function waits till the device is ready to accept a new MDIO
0690  * request.
0691  *
0692  * Return:  0 for success or ETIMEDOUT for a timeout
0693  */
0694 
0695 static int xemaclite_mdio_wait(struct net_local *lp)
0696 {
0697     u32 val;
0698 
0699     /* wait for the MDIO interface to not be busy or timeout
0700      * after some time.
0701      */
0702     return readx_poll_timeout(xemaclite_readl,
0703                   lp->base_addr + XEL_MDIOCTRL_OFFSET,
0704                   val, !(val & XEL_MDIOCTRL_MDIOSTS_MASK),
0705                   1000, 20000);
0706 }
0707 
0708 /**
0709  * xemaclite_mdio_read - Read from a given MII management register
0710  * @bus:    the mii_bus struct
0711  * @phy_id: the phy address
0712  * @reg:    register number to read from
0713  *
0714  * This function waits till the device is ready to accept a new MDIO
0715  * request and then writes the phy address to the MDIO Address register
0716  * and reads data from MDIO Read Data register, when its available.
0717  *
0718  * Return:  Value read from the MII management register
0719  */
0720 static int xemaclite_mdio_read(struct mii_bus *bus, int phy_id, int reg)
0721 {
0722     struct net_local *lp = bus->priv;
0723     u32 ctrl_reg;
0724     u32 rc;
0725 
0726     if (xemaclite_mdio_wait(lp))
0727         return -ETIMEDOUT;
0728 
0729     /* Write the PHY address, register number and set the OP bit in the
0730      * MDIO Address register. Set the Status bit in the MDIO Control
0731      * register to start a MDIO read transaction.
0732      */
0733     ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
0734     xemaclite_writel(XEL_MDIOADDR_OP_MASK |
0735              ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
0736              lp->base_addr + XEL_MDIOADDR_OFFSET);
0737     xemaclite_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
0738              lp->base_addr + XEL_MDIOCTRL_OFFSET);
0739 
0740     if (xemaclite_mdio_wait(lp))
0741         return -ETIMEDOUT;
0742 
0743     rc = xemaclite_readl(lp->base_addr + XEL_MDIORD_OFFSET);
0744 
0745     dev_dbg(&lp->ndev->dev,
0746         "%s(phy_id=%i, reg=%x) == %x\n", __func__,
0747         phy_id, reg, rc);
0748 
0749     return rc;
0750 }
0751 
0752 /**
0753  * xemaclite_mdio_write - Write to a given MII management register
0754  * @bus:    the mii_bus struct
0755  * @phy_id: the phy address
0756  * @reg:    register number to write to
0757  * @val:    value to write to the register number specified by reg
0758  *
0759  * This function waits till the device is ready to accept a new MDIO
0760  * request and then writes the val to the MDIO Write Data register.
0761  *
0762  * Return:      0 upon success or a negative error upon failure
0763  */
0764 static int xemaclite_mdio_write(struct mii_bus *bus, int phy_id, int reg,
0765                 u16 val)
0766 {
0767     struct net_local *lp = bus->priv;
0768     u32 ctrl_reg;
0769 
0770     dev_dbg(&lp->ndev->dev,
0771         "%s(phy_id=%i, reg=%x, val=%x)\n", __func__,
0772         phy_id, reg, val);
0773 
0774     if (xemaclite_mdio_wait(lp))
0775         return -ETIMEDOUT;
0776 
0777     /* Write the PHY address, register number and clear the OP bit in the
0778      * MDIO Address register and then write the value into the MDIO Write
0779      * Data register. Finally, set the Status bit in the MDIO Control
0780      * register to start a MDIO write transaction.
0781      */
0782     ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
0783     xemaclite_writel(~XEL_MDIOADDR_OP_MASK &
0784              ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
0785              lp->base_addr + XEL_MDIOADDR_OFFSET);
0786     xemaclite_writel(val, lp->base_addr + XEL_MDIOWR_OFFSET);
0787     xemaclite_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
0788              lp->base_addr + XEL_MDIOCTRL_OFFSET);
0789 
0790     return 0;
0791 }
0792 
0793 /**
0794  * xemaclite_mdio_setup - Register mii_bus for the Emaclite device
0795  * @lp:     Pointer to the Emaclite device private data
0796  * @dev:    Pointer to OF device structure
0797  *
0798  * This function enables MDIO bus in the Emaclite device and registers a
0799  * mii_bus.
0800  *
0801  * Return:  0 upon success or a negative error upon failure
0802  */
0803 static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev)
0804 {
0805     struct mii_bus *bus;
0806     struct resource res;
0807     struct device_node *np = of_get_parent(lp->phy_node);
0808     struct device_node *npp;
0809     int rc, ret;
0810 
0811     /* Don't register the MDIO bus if the phy_node or its parent node
0812      * can't be found.
0813      */
0814     if (!np) {
0815         dev_err(dev, "Failed to register mdio bus.\n");
0816         return -ENODEV;
0817     }
0818     npp = of_get_parent(np);
0819     ret = of_address_to_resource(npp, 0, &res);
0820     of_node_put(npp);
0821     if (ret) {
0822         dev_err(dev, "%s resource error!\n",
0823             dev->of_node->full_name);
0824         of_node_put(np);
0825         return ret;
0826     }
0827     if (lp->ndev->mem_start != res.start) {
0828         struct phy_device *phydev;
0829 
0830         phydev = of_phy_find_device(lp->phy_node);
0831         if (!phydev)
0832             dev_info(dev,
0833                  "MDIO of the phy is not registered yet\n");
0834         else
0835             put_device(&phydev->mdio.dev);
0836         of_node_put(np);
0837         return 0;
0838     }
0839 
0840     /* Enable the MDIO bus by asserting the enable bit in MDIO Control
0841      * register.
0842      */
0843     xemaclite_writel(XEL_MDIOCTRL_MDIOEN_MASK,
0844              lp->base_addr + XEL_MDIOCTRL_OFFSET);
0845 
0846     bus = mdiobus_alloc();
0847     if (!bus) {
0848         dev_err(dev, "Failed to allocate mdiobus\n");
0849         of_node_put(np);
0850         return -ENOMEM;
0851     }
0852 
0853     snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
0854          (unsigned long long)res.start);
0855     bus->priv = lp;
0856     bus->name = "Xilinx Emaclite MDIO";
0857     bus->read = xemaclite_mdio_read;
0858     bus->write = xemaclite_mdio_write;
0859     bus->parent = dev;
0860 
0861     rc = of_mdiobus_register(bus, np);
0862     of_node_put(np);
0863     if (rc) {
0864         dev_err(dev, "Failed to register mdio bus.\n");
0865         goto err_register;
0866     }
0867 
0868     lp->mii_bus = bus;
0869 
0870     return 0;
0871 
0872 err_register:
0873     mdiobus_free(bus);
0874     return rc;
0875 }
0876 
0877 /**
0878  * xemaclite_adjust_link - Link state callback for the Emaclite device
0879  * @ndev: pointer to net_device struct
0880  *
0881  * There's nothing in the Emaclite device to be configured when the link
0882  * state changes. We just print the status.
0883  */
0884 static void xemaclite_adjust_link(struct net_device *ndev)
0885 {
0886     struct net_local *lp = netdev_priv(ndev);
0887     struct phy_device *phy = lp->phy_dev;
0888     int link_state;
0889 
0890     /* hash together the state values to decide if something has changed */
0891     link_state = phy->speed | (phy->duplex << 1) | phy->link;
0892 
0893     if (lp->last_link != link_state) {
0894         lp->last_link = link_state;
0895         phy_print_status(phy);
0896     }
0897 }
0898 
0899 /**
0900  * xemaclite_open - Open the network device
0901  * @dev:    Pointer to the network device
0902  *
0903  * This function sets the MAC address, requests an IRQ and enables interrupts
0904  * for the Emaclite device and starts the Tx queue.
0905  * It also connects to the phy device, if MDIO is included in Emaclite device.
0906  *
0907  * Return:  0 on success. -ENODEV, if PHY cannot be connected.
0908  *      Non-zero error value on failure.
0909  */
0910 static int xemaclite_open(struct net_device *dev)
0911 {
0912     struct net_local *lp = netdev_priv(dev);
0913     int retval;
0914 
0915     /* Just to be safe, stop the device first */
0916     xemaclite_disable_interrupts(lp);
0917 
0918     if (lp->phy_node) {
0919         lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
0920                          xemaclite_adjust_link, 0,
0921                          PHY_INTERFACE_MODE_MII);
0922         if (!lp->phy_dev) {
0923             dev_err(&lp->ndev->dev, "of_phy_connect() failed\n");
0924             return -ENODEV;
0925         }
0926 
0927         /* EmacLite doesn't support giga-bit speeds */
0928         phy_set_max_speed(lp->phy_dev, SPEED_100);
0929         phy_start(lp->phy_dev);
0930     }
0931 
0932     /* Set the MAC address each time opened */
0933     xemaclite_update_address(lp, dev->dev_addr);
0934 
0935     /* Grab the IRQ */
0936     retval = request_irq(dev->irq, xemaclite_interrupt, 0, dev->name, dev);
0937     if (retval) {
0938         dev_err(&lp->ndev->dev, "Could not allocate interrupt %d\n",
0939             dev->irq);
0940         if (lp->phy_dev)
0941             phy_disconnect(lp->phy_dev);
0942         lp->phy_dev = NULL;
0943 
0944         return retval;
0945     }
0946 
0947     /* Enable Interrupts */
0948     xemaclite_enable_interrupts(lp);
0949 
0950     /* We're ready to go */
0951     netif_start_queue(dev);
0952 
0953     return 0;
0954 }
0955 
0956 /**
0957  * xemaclite_close - Close the network device
0958  * @dev:    Pointer to the network device
0959  *
0960  * This function stops the Tx queue, disables interrupts and frees the IRQ for
0961  * the Emaclite device.
0962  * It also disconnects the phy device associated with the Emaclite device.
0963  *
0964  * Return:  0, always.
0965  */
0966 static int xemaclite_close(struct net_device *dev)
0967 {
0968     struct net_local *lp = netdev_priv(dev);
0969 
0970     netif_stop_queue(dev);
0971     xemaclite_disable_interrupts(lp);
0972     free_irq(dev->irq, dev);
0973 
0974     if (lp->phy_dev)
0975         phy_disconnect(lp->phy_dev);
0976     lp->phy_dev = NULL;
0977 
0978     return 0;
0979 }
0980 
0981 /**
0982  * xemaclite_send - Transmit a frame
0983  * @orig_skb:   Pointer to the socket buffer to be transmitted
0984  * @dev:    Pointer to the network device
0985  *
0986  * This function checks if the Tx buffer of the Emaclite device is free to send
0987  * data. If so, it fills the Tx buffer with data from socket buffer data,
0988  * updates the stats and frees the socket buffer. The Tx completion is signaled
0989  * by an interrupt. If the Tx buffer isn't free, then the socket buffer is
0990  * deferred and the Tx queue is stopped so that the deferred socket buffer can
0991  * be transmitted when the Emaclite device is free to transmit data.
0992  *
0993  * Return:  NETDEV_TX_OK, always.
0994  */
0995 static netdev_tx_t
0996 xemaclite_send(struct sk_buff *orig_skb, struct net_device *dev)
0997 {
0998     struct net_local *lp = netdev_priv(dev);
0999     struct sk_buff *new_skb;
1000     unsigned int len;
1001     unsigned long flags;
1002 
1003     len = orig_skb->len;
1004 
1005     new_skb = orig_skb;
1006 
1007     spin_lock_irqsave(&lp->reset_lock, flags);
1008     if (xemaclite_send_data(lp, (u8 *)new_skb->data, len) != 0) {
1009         /* If the Emaclite Tx buffer is busy, stop the Tx queue and
1010          * defer the skb for transmission during the ISR, after the
1011          * current transmission is complete
1012          */
1013         netif_stop_queue(dev);
1014         lp->deferred_skb = new_skb;
1015         /* Take the time stamp now, since we can't do this in an ISR. */
1016         skb_tx_timestamp(new_skb);
1017         spin_unlock_irqrestore(&lp->reset_lock, flags);
1018         return NETDEV_TX_OK;
1019     }
1020     spin_unlock_irqrestore(&lp->reset_lock, flags);
1021 
1022     skb_tx_timestamp(new_skb);
1023 
1024     dev->stats.tx_bytes += len;
1025     dev_consume_skb_any(new_skb);
1026 
1027     return NETDEV_TX_OK;
1028 }
1029 
1030 /**
1031  * get_bool - Get a parameter from the OF device
1032  * @ofdev:  Pointer to OF device structure
1033  * @s:      Property to be retrieved
1034  *
1035  * This function looks for a property in the device node and returns the value
1036  * of the property if its found or 0 if the property is not found.
1037  *
1038  * Return:  Value of the parameter if the parameter is found, or 0 otherwise
1039  */
1040 static bool get_bool(struct platform_device *ofdev, const char *s)
1041 {
1042     u32 *p = (u32 *)of_get_property(ofdev->dev.of_node, s, NULL);
1043 
1044     if (!p) {
1045         dev_warn(&ofdev->dev, "Parameter %s not found, defaulting to false\n", s);
1046         return false;
1047     }
1048 
1049     return (bool)*p;
1050 }
1051 
1052 /**
1053  * xemaclite_ethtools_get_drvinfo - Get various Axi Emac Lite driver info
1054  * @ndev:       Pointer to net_device structure
1055  * @ed:         Pointer to ethtool_drvinfo structure
1056  *
1057  * This implements ethtool command for getting the driver information.
1058  * Issue "ethtool -i ethX" under linux prompt to execute this function.
1059  */
1060 static void xemaclite_ethtools_get_drvinfo(struct net_device *ndev,
1061                        struct ethtool_drvinfo *ed)
1062 {
1063     strlcpy(ed->driver, DRIVER_NAME, sizeof(ed->driver));
1064 }
1065 
1066 static const struct ethtool_ops xemaclite_ethtool_ops = {
1067     .get_drvinfo    = xemaclite_ethtools_get_drvinfo,
1068     .get_link       = ethtool_op_get_link,
1069     .get_link_ksettings = phy_ethtool_get_link_ksettings,
1070     .set_link_ksettings = phy_ethtool_set_link_ksettings,
1071 };
1072 
1073 static const struct net_device_ops xemaclite_netdev_ops;
1074 
1075 /**
1076  * xemaclite_of_probe - Probe method for the Emaclite device.
1077  * @ofdev:  Pointer to OF device structure
1078  *
1079  * This function probes for the Emaclite device in the device tree.
1080  * It initializes the driver data structure and the hardware, sets the MAC
1081  * address and registers the network device.
1082  * It also registers a mii_bus for the Emaclite device, if MDIO is included
1083  * in the device.
1084  *
1085  * Return:  0, if the driver is bound to the Emaclite device, or
1086  *      a negative error if there is failure.
1087  */
1088 static int xemaclite_of_probe(struct platform_device *ofdev)
1089 {
1090     struct resource *res;
1091     struct net_device *ndev = NULL;
1092     struct net_local *lp = NULL;
1093     struct device *dev = &ofdev->dev;
1094 
1095     int rc = 0;
1096 
1097     dev_info(dev, "Device Tree Probing\n");
1098 
1099     /* Create an ethernet device instance */
1100     ndev = alloc_etherdev(sizeof(struct net_local));
1101     if (!ndev)
1102         return -ENOMEM;
1103 
1104     dev_set_drvdata(dev, ndev);
1105     SET_NETDEV_DEV(ndev, &ofdev->dev);
1106 
1107     lp = netdev_priv(ndev);
1108     lp->ndev = ndev;
1109 
1110     /* Get IRQ for the device */
1111     rc = platform_get_irq(ofdev, 0);
1112     if (rc < 0)
1113         goto error;
1114 
1115     ndev->irq = rc;
1116 
1117     res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
1118     lp->base_addr = devm_ioremap_resource(&ofdev->dev, res);
1119     if (IS_ERR(lp->base_addr)) {
1120         rc = PTR_ERR(lp->base_addr);
1121         goto error;
1122     }
1123 
1124     ndev->mem_start = res->start;
1125     ndev->mem_end = res->end;
1126 
1127     spin_lock_init(&lp->reset_lock);
1128     lp->next_tx_buf_to_use = 0x0;
1129     lp->next_rx_buf_to_use = 0x0;
1130     lp->tx_ping_pong = get_bool(ofdev, "xlnx,tx-ping-pong");
1131     lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong");
1132 
1133     rc = of_get_ethdev_address(ofdev->dev.of_node, ndev);
1134     if (rc) {
1135         dev_warn(dev, "No MAC address found, using random\n");
1136         eth_hw_addr_random(ndev);
1137     }
1138 
1139     /* Clear the Tx CSR's in case this is a restart */
1140     xemaclite_writel(0, lp->base_addr + XEL_TSR_OFFSET);
1141     xemaclite_writel(0, lp->base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
1142 
1143     /* Set the MAC address in the EmacLite device */
1144     xemaclite_update_address(lp, ndev->dev_addr);
1145 
1146     lp->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
1147     xemaclite_mdio_setup(lp, &ofdev->dev);
1148 
1149     dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
1150 
1151     ndev->netdev_ops = &xemaclite_netdev_ops;
1152     ndev->ethtool_ops = &xemaclite_ethtool_ops;
1153     ndev->flags &= ~IFF_MULTICAST;
1154     ndev->watchdog_timeo = TX_TIMEOUT;
1155 
1156     /* Finally, register the device */
1157     rc = register_netdev(ndev);
1158     if (rc) {
1159         dev_err(dev,
1160             "Cannot register network device, aborting\n");
1161         goto put_node;
1162     }
1163 
1164     dev_info(dev,
1165          "Xilinx EmacLite at 0x%08lX mapped to 0x%p, irq=%d\n",
1166          (unsigned long __force)ndev->mem_start, lp->base_addr, ndev->irq);
1167     return 0;
1168 
1169 put_node:
1170     of_node_put(lp->phy_node);
1171 error:
1172     free_netdev(ndev);
1173     return rc;
1174 }
1175 
1176 /**
1177  * xemaclite_of_remove - Unbind the driver from the Emaclite device.
1178  * @of_dev: Pointer to OF device structure
1179  *
1180  * This function is called if a device is physically removed from the system or
1181  * if the driver module is being unloaded. It frees any resources allocated to
1182  * the device.
1183  *
1184  * Return:  0, always.
1185  */
1186 static int xemaclite_of_remove(struct platform_device *of_dev)
1187 {
1188     struct net_device *ndev = platform_get_drvdata(of_dev);
1189 
1190     struct net_local *lp = netdev_priv(ndev);
1191 
1192     /* Un-register the mii_bus, if configured */
1193     if (lp->mii_bus) {
1194         mdiobus_unregister(lp->mii_bus);
1195         mdiobus_free(lp->mii_bus);
1196         lp->mii_bus = NULL;
1197     }
1198 
1199     unregister_netdev(ndev);
1200 
1201     of_node_put(lp->phy_node);
1202     lp->phy_node = NULL;
1203 
1204     free_netdev(ndev);
1205 
1206     return 0;
1207 }
1208 
1209 #ifdef CONFIG_NET_POLL_CONTROLLER
1210 static void
1211 xemaclite_poll_controller(struct net_device *ndev)
1212 {
1213     disable_irq(ndev->irq);
1214     xemaclite_interrupt(ndev->irq, ndev);
1215     enable_irq(ndev->irq);
1216 }
1217 #endif
1218 
1219 /* Ioctl MII Interface */
1220 static int xemaclite_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1221 {
1222     if (!dev->phydev || !netif_running(dev))
1223         return -EINVAL;
1224 
1225     switch (cmd) {
1226     case SIOCGMIIPHY:
1227     case SIOCGMIIREG:
1228     case SIOCSMIIREG:
1229         return phy_mii_ioctl(dev->phydev, rq, cmd);
1230     default:
1231         return -EOPNOTSUPP;
1232     }
1233 }
1234 
1235 static const struct net_device_ops xemaclite_netdev_ops = {
1236     .ndo_open       = xemaclite_open,
1237     .ndo_stop       = xemaclite_close,
1238     .ndo_start_xmit     = xemaclite_send,
1239     .ndo_set_mac_address    = xemaclite_set_mac_address,
1240     .ndo_tx_timeout     = xemaclite_tx_timeout,
1241     .ndo_eth_ioctl      = xemaclite_ioctl,
1242 #ifdef CONFIG_NET_POLL_CONTROLLER
1243     .ndo_poll_controller = xemaclite_poll_controller,
1244 #endif
1245 };
1246 
1247 /* Match table for OF platform binding */
1248 static const struct of_device_id xemaclite_of_match[] = {
1249     { .compatible = "xlnx,opb-ethernetlite-1.01.a", },
1250     { .compatible = "xlnx,opb-ethernetlite-1.01.b", },
1251     { .compatible = "xlnx,xps-ethernetlite-1.00.a", },
1252     { .compatible = "xlnx,xps-ethernetlite-2.00.a", },
1253     { .compatible = "xlnx,xps-ethernetlite-2.01.a", },
1254     { .compatible = "xlnx,xps-ethernetlite-3.00.a", },
1255     { /* end of list */ },
1256 };
1257 MODULE_DEVICE_TABLE(of, xemaclite_of_match);
1258 
1259 static struct platform_driver xemaclite_of_driver = {
1260     .driver = {
1261         .name = DRIVER_NAME,
1262         .of_match_table = xemaclite_of_match,
1263     },
1264     .probe      = xemaclite_of_probe,
1265     .remove     = xemaclite_of_remove,
1266 };
1267 
1268 module_platform_driver(xemaclite_of_driver);
1269 
1270 MODULE_AUTHOR("Xilinx, Inc.");
1271 MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
1272 MODULE_LICENSE("GPL");