]> git.sur5r.net Git - u-boot/blobdiff - drivers/net/rtl8169.c
ARM: sun6i: Add support for the power reset control module found on the A31
[u-boot] / drivers / net / rtl8169.c
index 63ea2cca9b1090e8945d46ec35458d1da10d6535..c3ce17516c7407b0f189084bda4cbecb515122ac 100644 (file)
 *    r8169.c: Etherboot device driver for the RealTek RTL-8169 Gigabit
 *    Written 2003 by Timothy Legge <tlegge@rogers.com>
 *
-*    This program is free software; you can redistribute it and/or modify
-*    it under the terms of the GNU General Public License as published by
-*    the Free Software Foundation; either version 2 of the License, or
-*    (at your option) any later version.
-*
-*    This program is distributed in the hope that it will be useful,
-*    but WITHOUT ANY WARRANTY; without even the implied warranty of
-*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-*    GNU General Public License for more details.
-*
-*    You should have received a copy of the GNU General Public License
-*    along with this program; if not, write to the Free Software
-*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * SPDX-License-Identifier:    GPL-2.0+
 *
 *    Portions of this code based on:
 *      r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver
 *
 *    Indent Options: indent -kr -i8
 ***************************************************************************/
-
+/*
+ * 26 August 2006 Mihai Georgian <u-boot@linuxnotincluded.org.uk>
+ * Modified to use le32_to_cpu and cpu_to_le32 properly
+ */
 #include <common.h>
 #include <malloc.h>
 #include <net.h>
+#include <netdev.h>
 #include <asm/io.h>
 #include <pci.h>
 
-#if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
-       defined(CONFIG_RTL8169)
-
 #undef DEBUG_RTL8169
 #undef DEBUG_RTL8169_TX
 #undef DEBUG_RTL8169_RX
 static u32 ioaddr;
 
 /* Condensed operations for readability. */
-#define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
-#define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
-
 #define currticks()    get_timer(0)
-#define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)dev->priv, a)
-#define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)dev->priv, a)
 
 /* media options */
 #define MAX_UNITS 8
@@ -102,7 +86,7 @@ static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
 #define RTL_MIN_IO_SIZE 0x80
 #define TX_TIMEOUT  (6*HZ)
 
-/* write/read MMIO register */
+/* write/read MMIO register. Notice: {read,write}[wl] do the necessary swapping */
 #define RTL_W8(reg, val8)      writeb ((val8), ioaddr + (reg))
 #define RTL_W16(reg, val16)    writew ((val16), ioaddr + (reg))
 #define RTL_W32(reg, val32)    writel ((val32), ioaddr + (reg))
@@ -114,11 +98,16 @@ static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
 #define ETH_ALEN       MAC_ADDR_LEN
 #define ETH_ZLEN       60
 
+#define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)dev->priv, (pci_addr_t)a)
+#define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)dev->priv, (phys_addr_t)a)
+
 enum RTL8169_registers {
        MAC0 = 0,               /* Ethernet hardware address. */
        MAR0 = 8,               /* Multicast filter. */
-       TxDescStartAddr = 0x20,
-       TxHDescStartAddr = 0x28,
+       TxDescStartAddrLow = 0x20,
+       TxDescStartAddrHigh = 0x24,
+       TxHDescStartAddrLow = 0x28,
+       TxHDescStartAddrHigh = 0x2c,
        FLASH = 0x30,
        ERSR = 0x36,
        ChipCmd = 0x37,
@@ -143,7 +132,8 @@ enum RTL8169_registers {
        PHYstatus = 0x6C,
        RxMaxSize = 0xDA,
        CPlusCmd = 0xE0,
-       RxDescStartAddr = 0xE4,
+       RxDescStartAddrLow = 0xE4,
+       RxDescStartAddrHigh = 0xE8,
        EarlyTxThres = 0xEC,
        FuncEvent = 0xF0,
        FuncEventMask = 0xF4,
@@ -218,7 +208,7 @@ enum RTL8169_register_content {
        PHY_Enable_Auto_Nego = 0x1000,
 
        /* PHY_STAT_REG = 1; */
-       PHY_Auto_Neco_Comp = 0x0020,
+       PHY_Auto_Nego_Comp = 0x0020,
 
        /* PHY_AUTO_NEGO_REG = 4; */
        PHY_Cap_10_Half = 0x0020,
@@ -249,6 +239,17 @@ static struct {
 } rtl_chip_info[] = {
        {"RTL-8169", 0x00, 0xff7e1880,},
        {"RTL-8169", 0x04, 0xff7e1880,},
+       {"RTL-8169", 0x00, 0xff7e1880,},
+       {"RTL-8169s/8110s",     0x02, 0xff7e1880,},
+       {"RTL-8169s/8110s",     0x04, 0xff7e1880,},
+       {"RTL-8169sb/8110sb",   0x10, 0xff7e1880,},
+       {"RTL-8169sc/8110sc",   0x18, 0xff7e1880,},
+       {"RTL-8168b/8111sb",    0x30, 0xff7e1880,},
+       {"RTL-8168b/8111sb",    0x38, 0xff7e1880,},
+       {"RTL-8168d/8111d",     0x28, 0xff7e1880,},
+       {"RTL-8168evl/8111evl", 0x2e, 0xff7e1880,},
+       {"RTL-8101e",           0x34, 0xff7e1880,},
+       {"RTL-8100e",           0x32, 0xff7e1880,},
 };
 
 enum _DescStatusBit {
@@ -314,6 +315,8 @@ static const unsigned int rtl8169_rx_config =
     (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
 
 static struct pci_device_id supported[] = {
+       {PCI_VENDOR_ID_REALTEK, 0x8167},
+       {PCI_VENDOR_ID_REALTEK, 0x8168},
        {PCI_VENDOR_ID_REALTEK, 0x8169},
        {}
 };
@@ -354,8 +357,6 @@ int mdio_read(int RegAddr)
        return value;
 }
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
 static int rtl8169_init_board(struct eth_device *dev)
 {
        int i;
@@ -389,13 +390,57 @@ static int rtl8169_init_board(struct eth_device *dev)
 
        /* if unknown chip, assume array element #0, original RTL-8169 in this case */
        printf("PCI device %s: unknown chip version, assuming RTL-8169\n", dev->name);
-       printf("PCI device: TxConfig = 0x%hX\n", (unsigned long) RTL_R32(TxConfig));
+       printf("PCI device: TxConfig = 0x%lX\n", (unsigned long) RTL_R32(TxConfig));
        tpc->chipset = 0;
 
 match:
        return 0;
 }
 
+/*
+ * Cache maintenance functions. These are simple wrappers around the more
+ * general purpose flush_cache() and invalidate_dcache_range() functions.
+ */
+
+static void rtl_inval_rx_desc(struct RxDesc *desc)
+{
+       unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
+       unsigned long end = ALIGN(start + sizeof(*desc), ARCH_DMA_MINALIGN);
+
+       invalidate_dcache_range(start, end);
+}
+
+static void rtl_flush_rx_desc(struct RxDesc *desc)
+{
+       flush_cache((unsigned long)desc, sizeof(*desc));
+}
+
+static void rtl_inval_tx_desc(struct TxDesc *desc)
+{
+       unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
+       unsigned long end = ALIGN(start + sizeof(*desc), ARCH_DMA_MINALIGN);
+
+       invalidate_dcache_range(start, end);
+}
+
+static void rtl_flush_tx_desc(struct TxDesc *desc)
+{
+       flush_cache((unsigned long)desc, sizeof(*desc));
+}
+
+static void rtl_inval_buffer(void *buf, size_t size)
+{
+       unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
+       unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
+
+       invalidate_dcache_range(start, end);
+}
+
+static void rtl_flush_buffer(void *buf, size_t size)
+{
+       flush_cache((unsigned long)buf, size);
+}
+
 /**************************************************************************
 RECV - Receive a frame
 ***************************************************************************/
@@ -413,23 +458,29 @@ static int rtl_recv(struct eth_device *dev)
        ioaddr = dev->iobase;
 
        cur_rx = tpc->cur_rx;
-       if ((tpc->RxDescArray[cur_rx].status & OWNbit) == 0) {
-               if (!(tpc->RxDescArray[cur_rx].status & RxRES)) {
+
+       rtl_inval_rx_desc(&tpc->RxDescArray[cur_rx]);
+
+       if ((le32_to_cpu(tpc->RxDescArray[cur_rx].status) & OWNbit) == 0) {
+               if (!(le32_to_cpu(tpc->RxDescArray[cur_rx].status) & RxRES)) {
                        unsigned char rxdata[RX_BUF_LEN];
-                       length = (int) (tpc->RxDescArray[cur_rx].
-                                               status & 0x00001FFF) - 4;
+                       length = (int) (le32_to_cpu(tpc->RxDescArray[cur_rx].
+                                               status) & 0x00001FFF) - 4;
 
+                       rtl_inval_buffer(tpc->RxBufferRing[cur_rx], length);
                        memcpy(rxdata, tpc->RxBufferRing[cur_rx], length);
-                       NetReceive(rxdata, length);
 
                        if (cur_rx == NUM_RX_DESC - 1)
                                tpc->RxDescArray[cur_rx].status =
-                                   (OWNbit | EORbit) + RX_BUF_SIZE;
+                                       cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE);
                        else
                                tpc->RxDescArray[cur_rx].status =
-                                   OWNbit + RX_BUF_SIZE;
+                                       cpu_to_le32(OWNbit + RX_BUF_SIZE);
                        tpc->RxDescArray[cur_rx].buf_addr =
-                           virt_to_bus(tpc->RxBufferRing[cur_rx]);
+                               cpu_to_le32(bus_to_phys(tpc->RxBufferRing[cur_rx]));
+                       rtl_flush_rx_desc(&tpc->RxDescArray[cur_rx]);
+
+                       NetReceive(rxdata, length);
                } else {
                        puts("Error Rx");
                }
@@ -437,6 +488,10 @@ static int rtl_recv(struct eth_device *dev)
                tpc->cur_rx = cur_rx;
                return 1;
 
+       } else {
+               ushort sts = RTL_R8(IntrStatus);
+               RTL_W8(IntrStatus, sts & ~(TxErr | RxErr | SYSErr));
+               udelay(100);    /* wait */
        }
        tpc->cur_rx = cur_rx;
        return (0);             /* initially as this is called to flush the input */
@@ -446,7 +501,7 @@ static int rtl_recv(struct eth_device *dev)
 /**************************************************************************
 SEND - Transmit a frame
 ***************************************************************************/
-static int rtl_send(struct eth_device *dev, volatile void *packet, int length)
+static int rtl_send(struct eth_device *dev, void *packet, int length)
 {
        /* send the packet to destination */
 
@@ -454,6 +509,7 @@ static int rtl_send(struct eth_device *dev, volatile void *packet, int length)
        u8 *ptxb;
        int entry = tpc->cur_tx % NUM_TX_DESC;
        u32 len = length;
+       int ret;
 
 #ifdef DEBUG_RTL8169_TX
        int stime = currticks();
@@ -466,38 +522,47 @@ static int rtl_send(struct eth_device *dev, volatile void *packet, int length)
        /* 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';
 
-       tpc->TxDescArray[entry].buf_addr = virt_to_bus(ptxb);
+       tpc->TxDescArray[entry].buf_Haddr = 0;
+       tpc->TxDescArray[entry].buf_addr = cpu_to_le32(bus_to_phys(ptxb));
        if (entry != (NUM_TX_DESC - 1)) {
                tpc->TxDescArray[entry].status =
-                   (OWNbit | FSbit | LSbit) | ((len > ETH_ZLEN) ?
-                                               len : ETH_ZLEN);
+                       cpu_to_le32((OWNbit | FSbit | LSbit) |
+                                   ((len > ETH_ZLEN) ? len : ETH_ZLEN));
        } else {
                tpc->TxDescArray[entry].status =
-                   (OWNbit | EORbit | FSbit | LSbit) |
-                   ((len > ETH_ZLEN) ? length : ETH_ZLEN);
+                       cpu_to_le32((OWNbit | EORbit | FSbit | LSbit) |
+                                   ((len > ETH_ZLEN) ? len : ETH_ZLEN));
        }
+       rtl_flush_tx_desc(&tpc->TxDescArray[entry]);
        RTL_W8(TxPoll, 0x40);   /* set polling bit */
 
        tpc->cur_tx++;
        to = currticks() + TX_TIMEOUT;
-       while ((tpc->TxDescArray[entry].status & OWNbit) && (currticks() < to));        /* wait */
+       do {
+               rtl_inval_tx_desc(&tpc->TxDescArray[entry]);
+       } while ((le32_to_cpu(tpc->TxDescArray[entry].status) & OWNbit)
+                               && (currticks() < to)); /* wait */
 
        if (currticks() >= to) {
 #ifdef DEBUG_RTL8169_TX
-               puts ("tx timeout/error\n");
-               printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
+               puts("tx timeout/error\n");
+               printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
 #endif
-               return 0;
+               ret = 0;
        } else {
 #ifdef DEBUG_RTL8169_TX
                puts("tx done\n");
 #endif
-               return length;
+               ret = length;
        }
+       /* Delay to make net console (nc) work properly */
+       udelay(20);
+       return ret;
 }
 
 static void rtl8169_set_rx_mode(struct eth_device *dev)
@@ -546,7 +611,11 @@ static void rtl8169_hw_start(struct eth_device *dev)
 #endif
 
        RTL_W8(Cfg9346, Cfg9346_Unlock);
-       RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
+
+       /* RTL-8169sb/8110sb or previous version */
+       if (tpc->chipset <= 5)
+               RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
+
        RTL_W8(EarlyTxThres, EarlyTxThld);
 
        /* For gigabit rtl8169 */
@@ -564,8 +633,15 @@ static void rtl8169_hw_start(struct eth_device *dev)
 
        tpc->cur_rx = 0;
 
-       RTL_W32(TxDescStartAddr, virt_to_le32desc(tpc->TxDescArray));
-       RTL_W32(RxDescStartAddr, virt_to_le32desc(tpc->RxDescArray));
+       RTL_W32(TxDescStartAddrLow, bus_to_phys(tpc->TxDescArray));
+       RTL_W32(TxDescStartAddrHigh, (unsigned long)0);
+       RTL_W32(RxDescStartAddrLow, bus_to_phys(tpc->RxDescArray));
+       RTL_W32(RxDescStartAddrHigh, (unsigned long)0);
+
+       /* RTL-8169sc/8110sc or later version */
+       if (tpc->chipset > 5)
+               RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
+
        RTL_W8(Cfg9346, Cfg9346_Lock);
        udelay(10);
 
@@ -577,7 +653,7 @@ static void rtl8169_hw_start(struct eth_device *dev)
        RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
 
 #ifdef DEBUG_RTL8169
-       printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
+       printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
 #endif
 }
 
@@ -603,28 +679,28 @@ static void rtl8169_init_ring(struct eth_device *dev)
        for (i = 0; i < NUM_RX_DESC; i++) {
                if (i == (NUM_RX_DESC - 1))
                        tpc->RxDescArray[i].status =
-                           (OWNbit | EORbit) + RX_BUF_SIZE;
+                               cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE);
                else
-                       tpc->RxDescArray[i].status = OWNbit + RX_BUF_SIZE;
+                       tpc->RxDescArray[i].status =
+                               cpu_to_le32(OWNbit + RX_BUF_SIZE);
 
                tpc->RxBufferRing[i] = &rxb[i * RX_BUF_SIZE];
                tpc->RxDescArray[i].buf_addr =
-                   virt_to_bus(tpc->RxBufferRing[i]);
+                       cpu_to_le32(bus_to_phys(tpc->RxBufferRing[i]));
+               rtl_flush_rx_desc(&tpc->RxDescArray[i]);
        }
 
 #ifdef DEBUG_RTL8169
-       printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
+       printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
 #endif
 }
 
 /**************************************************************************
 RESET - Finish setting up the ethernet interface
 ***************************************************************************/
-static void rtl_reset(struct eth_device *dev, bd_t *bis)
+static int rtl_reset(struct eth_device *dev, bd_t *bis)
 {
        int i;
-       u8 diff;
-       u32 TxPhyAddr, RxPhyAddr;
 
 #ifdef DEBUG_RTL8169
        int stime = currticks();
@@ -632,25 +708,14 @@ static void rtl_reset(struct eth_device *dev, bd_t *bis)
 #endif
 
        tpc->TxDescArrays = tx_ring;
-       if (tpc->TxDescArrays == 0)
-               puts("Allot Error");
        /* Tx Desscriptor needs 256 bytes alignment; */
-       TxPhyAddr = virt_to_bus(tpc->TxDescArrays);
-       diff = 256 - (TxPhyAddr - ((TxPhyAddr >> 8) << 8));
-       TxPhyAddr += diff;
-       tpc->TxDescArray = (struct TxDesc *) (tpc->TxDescArrays + diff);
+       tpc->TxDescArray = (struct TxDesc *) ((unsigned long)(tpc->TxDescArrays +
+                                                             255) & ~255);
 
        tpc->RxDescArrays = rx_ring;
        /* Rx Desscriptor needs 256 bytes alignment; */
-       RxPhyAddr = virt_to_bus(tpc->RxDescArrays);
-       diff = 256 - (RxPhyAddr - ((RxPhyAddr >> 8) << 8));
-       RxPhyAddr += diff;
-       tpc->RxDescArray = (struct RxDesc *) (tpc->RxDescArrays + diff);
-
-       if (tpc->TxDescArrays == NULL || tpc->RxDescArrays == NULL) {
-               puts("Allocate RxDescArray or TxDescArray failed\n");
-               return;
-       }
+       tpc->RxDescArray = (struct RxDesc *) ((unsigned long)(tpc->RxDescArrays +
+                                                             255) & ~255);
 
        rtl8169_init_ring(dev);
        rtl8169_hw_start(dev);
@@ -667,8 +732,9 @@ static void rtl_reset(struct eth_device *dev, bd_t *bis)
        txb[5] = dev->enetaddr[5];
 
 #ifdef DEBUG_RTL8169
-       printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
+       printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
 #endif
+       return 0;
 }
 
 /**************************************************************************
@@ -710,7 +776,6 @@ INIT - Look for an adapter, this routine's visible to the outside
 static int rtl_init(struct eth_device *dev, bd_t *bis)
 {
        static int board_idx = -1;
-       static int printed_version = 0;
        int i, rc;
        int option = -1, Cap10_100 = 0, Cap1000 = 0;
 
@@ -722,8 +787,6 @@ static int rtl_init(struct eth_device *dev, bd_t *bis)
 
        board_idx++;
 
-       printed_version = 1;
-
        /* point to private storage */
        tpc = &tpx;
 
@@ -736,6 +799,7 @@ static int rtl_init(struct eth_device *dev, bd_t *bis)
                dev->enetaddr[i] = RTL_R8(MAC0 + i);
 
 #ifdef DEBUG_RTL8169
+       printf("chipset = %d\n", tpc->chipset);
        printf("MAC Address");
        for (i = 0; i < MAC_ADDR_LEN; i++)
                printf(":%02x", dev->enetaddr[i]);
@@ -808,7 +872,7 @@ static int rtl_init(struct eth_device *dev, bd_t *bis)
                /* wait for auto-negotiation process */
                for (i = 10000; i > 0; i--) {
                        /* check if auto-negotiation complete */
-                       if (mdio_read(PHY_STAT_REG) & PHY_Auto_Neco_Comp) {
+                       if (mdio_read(PHY_STAT_REG) & PHY_Auto_Nego_Comp) {
                                udelay(100);
                                option = RTL_R8(PHYstatus);
                                if (option & _1000bpsF) {
@@ -818,13 +882,12 @@ static int rtl_init(struct eth_device *dev, bd_t *bis)
 #endif
                                } else {
 #ifdef DEBUG_RTL8169
-                                       printf
-                                           ("%s: %sMbps %s-duplex operation.\n",
-                                            dev->name,
-                                            (option & _100bps) ? "100" :
-                                            "10",
-                                            (option & FullDup) ? "Full" :
-                                            "Half");
+                                       printf("%s: %sMbps %s-duplex operation.\n",
+                                              dev->name,
+                                              (option & _100bps) ? "100" :
+                                              "10",
+                                              (option & FullDup) ? "Full" :
+                                              "Half");
 #endif
                                }
                                break;
@@ -855,21 +918,40 @@ int rtl8169_initialize(bd_t *bis)
        int idx=0;
 
        while(1){
+               unsigned int region;
+               u16 device;
+
                /* Find RTL8169 */
                if ((devno = pci_find_devices(supported, idx++)) < 0)
                        break;
 
-               pci_read_config_dword(devno, PCI_BASE_ADDRESS_1, &iobase);
+               pci_read_config_word(devno, PCI_DEVICE_ID, &device);
+               switch (device) {
+               case 0x8168:
+                       region = 2;
+                       break;
+
+               default:
+                       region = 1;
+                       break;
+               }
+
+               pci_read_config_dword(devno, PCI_BASE_ADDRESS_0 + (region * 4), &iobase);
                iobase &= ~0xf;
 
                debug ("rtl8169: REALTEK RTL8169 @0x%x\n", iobase);
 
                dev = (struct eth_device *)malloc(sizeof *dev);
+               if (!dev) {
+                       printf("Can not allocate memory of rtl8169\n");
+                       break;
+               }
 
+               memset(dev, 0, sizeof(*dev));
                sprintf (dev->name, "RTL8169#%d", card_number);
 
                dev->priv = (void *) devno;
-               dev->iobase = (int)bus_to_phys(iobase);
+               dev->iobase = (int)pci_mem_to_phys(devno, iobase);
 
                dev->init = rtl_reset;
                dev->halt = rtl_halt;
@@ -884,5 +966,3 @@ int rtl8169_initialize(bd_t *bis)
        }
        return card_number;
 }
-
-#endif