common: Move ARM cache operations out of common.h
[oweals/u-boot.git] / drivers / net / rtl8169.c
index 163b9df55c9b164b75170826bf28d31030a4e085..53454f2f217737c771f1684635bc6a82a9e7037d 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * rtl8169.c : U-Boot driver for the RealTek RTL8169
  *
@@ -10,8 +11,6 @@
 /**************************************************************************
 *    r8169.c: Etherboot device driver for the RealTek RTL-8169 Gigabit
 *    Written 2003 by Timothy Legge <tlegge@rogers.com>
-*
- * SPDX-License-Identifier:    GPL-2.0+
 *
 *    Portions of this code based on:
 *      r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver
@@ -41,6 +40,7 @@
  * Modified to use le32_to_cpu and cpu_to_le32 properly
  */
 #include <common.h>
+#include <cpu_func.h>
 #include <dm.h>
 #include <errno.h>
 #include <malloc.h>
@@ -103,10 +103,6 @@ static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
 #define RTL_R16(reg)           readw(ioaddr + (reg))
 #define RTL_R32(reg)           readl(ioaddr + (reg))
 
-#define ETH_FRAME_LEN  MAX_ETH_FRAME_SIZE
-#define ETH_ALEN       MAC_ADDR_LEN
-#define ETH_ZLEN       60
-
 #define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)(unsigned long)dev->priv, \
        (pci_addr_t)(unsigned long)a)
 #define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)(unsigned long)dev->priv, \
@@ -262,6 +258,7 @@ static struct {
        {"RTL-8168/8111g",      0x4c, 0xff7e1880,},
        {"RTL-8101e",           0x34, 0xff7e1880,},
        {"RTL-8100e",           0x32, 0xff7e1880,},
+       {"RTL-8168h/8111h",     0x54, 0xff7e1880,},
 };
 
 enum _DescStatusBit {
@@ -306,7 +303,7 @@ static unsigned char rxdata[RX_BUF_LEN];
  */
 #if RTL8169_DESC_SIZE < ARCH_DMA_MINALIGN
 #if !defined(CONFIG_SYS_NONCACHED_MEMORY) && \
-       !defined(CONFIG_SYS_DCACHE_OFF) && !defined(CONFIG_X86)
+       !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_X86)
 #warning cache-line size is larger than descriptor size
 #endif
 #endif
@@ -339,9 +336,6 @@ struct rtl8169_private {
 
 static struct rtl8169_private *tpc;
 
-static const u16 rtl8169_intr_mask =
-    SYSErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver | TxErr |
-    TxOK | RxErr | RxOK;
 static const unsigned int rtl8169_rx_config =
     (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
 
@@ -629,11 +623,12 @@ static int rtl_send_common(pci_dev_t dev, unsigned long dev_iobase,
        /* point to the current txb incase multiple tx_rings are used */
        ptxb = tpc->Tx_skbuff[entry * MAX_ETH_FRAME_SIZE];
        memcpy(ptxb, (char *)packet, (int)length);
-       rtl_flush_buffer(ptxb, length);
 
        while (len < ETH_ZLEN)
                ptxb[len++] = '\0';
 
+       rtl_flush_buffer(ptxb, ALIGN(len, RTL8169_ALIGN));
+
        tpc->TxDescArray[entry].buf_Haddr = 0;
 #ifdef CONFIG_DM_ETH
        tpc->TxDescArray[entry].buf_addr = cpu_to_le32(
@@ -666,12 +661,12 @@ static int rtl_send_common(pci_dev_t dev, unsigned long dev_iobase,
                puts("tx timeout/error\n");
                printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
 #endif
-               ret = 0;
+               ret = -ETIMEDOUT;
        } else {
 #ifdef DEBUG_RTL8169_TX
                puts("tx done\n");
 #endif
-               ret = length;
+               ret = 0;
        }
        /* Delay to make net console (nc) work properly */
        udelay(20);
@@ -850,9 +845,11 @@ static void rtl8169_init_ring(pci_dev_t dev)
 }
 
 #ifdef CONFIG_DM_ETH
-static void rtl8169_common_start(struct udevice *dev, unsigned char *enetaddr)
+static void rtl8169_common_start(struct udevice *dev, unsigned char *enetaddr,
+                                unsigned long dev_iobase)
 #else
-static void rtl8169_common_start(pci_dev_t dev, unsigned char *enetaddr)
+static void rtl8169_common_start(pci_dev_t dev, unsigned char *enetaddr,
+                                unsigned long dev_iobase)
 #endif
 {
        int i;
@@ -862,6 +859,8 @@ static void rtl8169_common_start(pci_dev_t dev, unsigned char *enetaddr)
        printf ("%s\n", __FUNCTION__);
 #endif
 
+       ioaddr = dev_iobase;
+
        rtl8169_init_ring(dev);
        rtl8169_hw_start(dev);
        /* Construct a perfect filter frame with the mac address as first match
@@ -885,8 +884,9 @@ static void rtl8169_common_start(pci_dev_t dev, unsigned char *enetaddr)
 static int rtl8169_eth_start(struct udevice *dev)
 {
        struct eth_pdata *plat = dev_get_platdata(dev);
+       struct rtl8169_private *priv = dev_get_priv(dev);
 
-       rtl8169_common_start(dev, plat->enetaddr);
+       rtl8169_common_start(dev, plat->enetaddr, priv->iobase);
 
        return 0;
 }
@@ -897,7 +897,7 @@ RESET - Finish setting up the ethernet interface
 static int rtl_reset(struct eth_device *dev, bd_t *bis)
 {
        rtl8169_common_start((pci_dev_t)(unsigned long)dev->priv,
-                            dev->enetaddr);
+                            dev->enetaddr, dev->iobase);
 
        return 0;
 }
@@ -943,6 +943,23 @@ static void rtl_halt(struct eth_device *dev)
 }
 #endif
 
+#ifdef CONFIG_DM_ETH
+static int rtl8169_write_hwaddr(struct udevice *dev)
+{
+       struct eth_pdata *plat = dev_get_platdata(dev);
+       unsigned int i;
+
+       RTL_W8(Cfg9346, Cfg9346_Unlock);
+
+       for (i = 0; i < MAC_ADDR_LEN; i++)
+               RTL_W8(MAC0 + i, plat->enetaddr[i]);
+
+       RTL_W8(Cfg9346, Cfg9346_Lock);
+
+       return 0;
+}
+#endif
+
 /**************************************************************************
 INIT - Look for an adapter, this routine's visible to the outside
 ***************************************************************************/
@@ -1197,6 +1214,7 @@ static const struct eth_ops rtl8169_eth_ops = {
        .send   = rtl8169_eth_send,
        .recv   = rtl8169_eth_recv,
        .stop   = rtl8169_eth_stop,
+       .write_hwaddr = rtl8169_write_hwaddr,
 };
 
 static const struct udevice_id rtl8169_eth_ids[] = {