]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/net/rtl8169.c
net: dm9000: random mac address support
[karo-tx-uboot.git] / drivers / net / rtl8169.c
index 7423bc0eb8e01565f779d9b35c0d03e2390409a5..290c9887c013105b96576be0cc8dec37acb0e28f 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
@@ -55,6 +43,7 @@
 #include <common.h>
 #include <malloc.h>
 #include <net.h>
+#include <netdev.h>
 #include <asm/io.h>
 #include <pci.h>
 
@@ -109,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,
@@ -138,7 +132,8 @@ enum RTL8169_registers {
        PHYstatus = 0x6C,
        RxMaxSize = 0xDA,
        CPlusCmd = 0xE0,
-       RxDescStartAddr = 0xE4,
+       RxDescStartAddrLow = 0xE4,
+       RxDescStartAddrHigh = 0xE8,
        EarlyTxThres = 0xEC,
        FuncEvent = 0xF0,
        FuncEventMask = 0xF4,
@@ -251,6 +246,7 @@ static struct {
        {"RTL-8169sc/8110sc",   0x18, 0xff7e1880,},
        {"RTL-8168b/8111sb",    0x30, 0xff7e1880,},
        {"RTL-8168b/8111sb",    0x38, 0xff7e1880,},
+       {"RTL-8168evl/8111evl", 0x2e, 0xff7e1880,},
        {"RTL-8101e",           0x34, 0xff7e1880,},
        {"RTL-8100e",           0x32, 0xff7e1880,},
 };
@@ -392,7 +388,7 @@ 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:
@@ -416,6 +412,8 @@ static int rtl_recv(struct eth_device *dev)
        ioaddr = dev->iobase;
 
        cur_rx = tpc->cur_rx;
+       flush_cache((unsigned long)&tpc->RxDescArray[cur_rx],
+                       sizeof(struct RxDesc));
        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];
@@ -432,7 +430,9 @@ static int rtl_recv(struct eth_device *dev)
                                tpc->RxDescArray[cur_rx].status =
                                        cpu_to_le32(OWNbit + RX_BUF_SIZE);
                        tpc->RxDescArray[cur_rx].buf_addr =
-                               cpu_to_le32((unsigned long)tpc->RxBufferRing[cur_rx]);
+                               cpu_to_le32(bus_to_phys(tpc->RxBufferRing[cur_rx]));
+                       flush_cache((unsigned long)tpc->RxBufferRing[cur_rx],
+                                       RX_BUF_SIZE);
                } else {
                        puts("Error Rx");
                }
@@ -453,7 +453,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 */
 
@@ -474,11 +474,13 @@ 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);
+       flush_cache((unsigned long)ptxb, length);
 
        while (len < ETH_ZLEN)
                ptxb[len++] = '\0';
 
-       tpc->TxDescArray[entry].buf_addr = cpu_to_le32((unsigned long)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 =
                        cpu_to_le32((OWNbit | FSbit | LSbit) |
@@ -492,13 +494,16 @@ static int rtl_send(struct eth_device *dev, volatile void *packet, int length)
 
        tpc->cur_tx++;
        to = currticks() + TX_TIMEOUT;
-       while ((le32_to_cpu(tpc->TxDescArray[entry].status) & OWNbit)
+       do {
+               flush_cache((unsigned long)&tpc->TxDescArray[entry],
+                               sizeof(struct TxDesc));
+       } 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
                ret = 0;
        } else {
@@ -558,7 +563,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 */
@@ -576,8 +585,15 @@ static void rtl8169_hw_start(struct eth_device *dev)
 
        tpc->cur_rx = 0;
 
-       RTL_W32(TxDescStartAddr, (unsigned long)tpc->TxDescArray);
-       RTL_W32(RxDescStartAddr, (unsigned long)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);
 
@@ -589,7 +605,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
 }
 
@@ -622,11 +638,12 @@ static void rtl8169_init_ring(struct eth_device *dev)
 
                tpc->RxBufferRing[i] = &rxb[i * RX_BUF_SIZE];
                tpc->RxDescArray[i].buf_addr =
-                       cpu_to_le32((unsigned long)tpc->RxBufferRing[i]);
+                       cpu_to_le32(bus_to_phys(tpc->RxBufferRing[i]));
+               flush_cache((unsigned long)tpc->RxBufferRing[i], RX_BUF_SIZE);
        }
 
 #ifdef DEBUG_RTL8169
-       printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
+       printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
 #endif
 }
 
@@ -667,7 +684,7 @@ static int 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;
 }
@@ -711,7 +728,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;
 
@@ -723,8 +739,6 @@ static int rtl_init(struct eth_device *dev, bd_t *bis)
 
        board_idx++;
 
-       printed_version = 1;
-
        /* point to private storage */
        tpc = &tpx;
 
@@ -734,9 +748,10 @@ static int rtl_init(struct eth_device *dev, bd_t *bis)
 
        /* Get MAC address.  FIXME: read EEPROM */
        for (i = 0; i < MAC_ADDR_LEN; i++)
-               bis->bi_enetaddr[i] = dev->enetaddr[i] = RTL_R8(MAC0 + i);
+               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]);
@@ -865,7 +880,12 @@ int rtl8169_initialize(bd_t *bis)
                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;