Merge branch 'u-boot/master' into 'u-boot-arm/master'
[oweals/u-boot.git] / drivers / net / rtl8139.c
index 23671800579a4119fafc9a13f42b7a35ed182850..208ce5ccc45426ea65e7443cd957318d076d0f53 100644 (file)
 #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_RTL8139)
-
-#define TICKS_PER_SEC  CFG_HZ
-#define TICKS_PER_MS   (TICKS_PER_SEC/1000)
-
-#define RTL_TIMEOUT    (1*TICKS_PER_SEC)
+#define RTL_TIMEOUT    100000
 
 #define ETH_FRAME_LEN          1514
 #define ETH_ALEN               6
 #define RX_BUF_LEN_IDX 0       /* 0, 1, 2 is allowed - 8,16,32K rx buffer */
 #define RX_BUF_LEN (8192 << RX_BUF_LEN_IDX)
 
-#undef DEBUG_TX
-#undef DEBUG_RX
+#define DEBUG_TX       0       /* set to 1 to enable debug code */
+#define DEBUG_RX       0       /* set to 1 to enable debug code */
 
-#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)
 
@@ -190,11 +184,11 @@ static unsigned char rx_ring[RX_BUF_LEN+16] __attribute__((aligned(4)));
 static int rtl8139_probe(struct eth_device *dev, bd_t *bis);
 static int read_eeprom(int location, int addr_len);
 static void rtl_reset(struct eth_device *dev);
-static int rtl_transmit(struct eth_device *dev, volatile void *packet, int length);
+static int rtl_transmit(struct eth_device *dev, void *packet, int length);
 static int rtl_poll(struct eth_device *dev);
 static void rtl_disable(struct eth_device *dev);
 #ifdef CONFIG_MCAST_TFTP/*  This driver already accepts all b/mcast */
-static int rtl_bcast_addr (struct eth_device *dev, u8 bcast_mac, u8 set)
+static int rtl_bcast_addr(struct eth_device *dev, const u8 *bcast_mac, u8 set)
 {
        return (0);
 }
@@ -225,6 +219,11 @@ int rtl8139_initialize(bd_t *bis)
                debug ("rtl8139: REALTEK RTL8139 @0x%x\n", iobase);
 
                dev = (struct eth_device *)malloc(sizeof *dev);
+               if (!dev) {
+                       printf("Can not allocate memory of rtl8139\n");
+                       break;
+               }
+               memset(dev, 0, sizeof(*dev));
 
                sprintf (dev->name, "RTL8139#%d", card_number);
 
@@ -253,7 +252,6 @@ int rtl8139_initialize(bd_t *bis)
 static int rtl8139_probe(struct eth_device *dev, bd_t *bis)
 {
        int i;
-       int speed10, fullduplex;
        int addr_len;
        unsigned short *ap = (unsigned short *)dev->enetaddr;
 
@@ -266,17 +264,14 @@ static int rtl8139_probe(struct eth_device *dev, bd_t *bis)
        for (i = 0; i < 3; i++)
                *ap++ = le16_to_cpu (read_eeprom(i + 7, addr_len));
 
-       speed10 = inb(ioaddr + MediaStatus) & MSRSpeed10;
-       fullduplex = inw(ioaddr + MII_BMCR) & BMCRDuplex;
-
        rtl_reset(dev);
 
        if (inb(ioaddr + MediaStatus) & MSRLinkFail) {
                printf("Cable not connected or other link failure\n");
-               return(0);
+               return -1 ;
        }
 
-       return 1;
+       return 0;
 }
 
 /* Serial EEPROM section. */
@@ -292,7 +287,7 @@ static int rtl8139_probe(struct eth_device *dev, bd_t *bis)
 
 /*
        Delay between EEPROM clock transitions.
-       No extra delay is needed with 33Mhz PCI, but 66Mhz may change this.
+       No extra delay is needed with 33MHz PCI, but 66MHz may change this.
 */
 
 #define eeprom_delay() inl(ee_addr)
@@ -389,9 +384,9 @@ static void rtl_reset(struct eth_device *dev)
         * from the configuration EEPROM default, because the card manufacturer
         * should have set that to match the card.  */
 
-#ifdef DEBUG_RX
-       printf("rx ring address is %X\n",(unsigned long)rx_ring);
-#endif
+       debug_cond(DEBUG_RX,
+               "rx ring address is %lX\n",(unsigned long)rx_ring);
+       flush_cache((unsigned long)rx_ring, RX_BUF_LEN);
        outl(phys_to_bus((int)rx_ring), ioaddr + RxBuf);
 
        /* If we add multicast support, the MAR0 register would have to be
@@ -412,19 +407,18 @@ static void rtl_reset(struct eth_device *dev)
        outw(0, ioaddr + IntrMask);
 }
 
-static int rtl_transmit(struct eth_device *dev, volatile void *packet, int length)
+static int rtl_transmit(struct eth_device *dev, void *packet, int length)
 {
-       unsigned int status, to;
+       unsigned int status;
        unsigned long txstatus;
        unsigned int len = length;
+       int i = 0;
 
        ioaddr = dev->iobase;
 
        memcpy((char *)tx_buffer, (char *)packet, (int)length);
 
-#ifdef DEBUG_TX
-       printf("sending %d bytes\n", len);
-#endif
+       debug_cond(DEBUG_TX, "sending %d bytes\n", len);
 
        /* Note: RTL8139 doesn't auto-pad, send minimum payload (another 4
         * bytes are sent automatically for the FCS, totalling to 64 bytes). */
@@ -432,12 +426,11 @@ static int rtl_transmit(struct eth_device *dev, volatile void *packet, int lengt
                tx_buffer[len++] = '\0';
        }
 
+       flush_cache((unsigned long)tx_buffer, length);
        outl(phys_to_bus((int)tx_buffer), ioaddr + TxAddr0 + cur_tx*4);
        outl(((TX_FIFO_THRESH<<11) & 0x003f0000) | len,
                ioaddr + TxStatus0 + cur_tx*4);
 
-       to = currticks() + RTL_TIMEOUT;
-
        do {
                status = inw(ioaddr + IntrStatus);
                /* Only acknlowledge interrupt sources we can properly handle
@@ -445,22 +438,25 @@ static int rtl_transmit(struct eth_device *dev, volatile void *packet, int lengt
                 * rtl_poll() function.  */
                outw(status & (TxOK | TxErr | PCIErr), ioaddr + IntrStatus);
                if ((status & (TxOK | TxErr | PCIErr)) != 0) break;
-       } while (currticks() < to);
+               udelay(10);
+       } while (i++ < RTL_TIMEOUT);
 
        txstatus = inl(ioaddr + TxStatus0 + cur_tx*4);
 
        if (status & TxOK) {
                cur_tx = (cur_tx + 1) % NUM_TX_DESC;
-#ifdef DEBUG_TX
-               printf("tx done (%d ticks), status %hX txstatus %X\n",
-                       to-currticks(), status, txstatus);
-#endif
+
+               debug_cond(DEBUG_TX,
+                       "tx done, status %hX txstatus %lX\n",
+                       status, txstatus);
+
                return length;
        } else {
-#ifdef DEBUG_TX
-               printf("tx timeout/error (%d ticks), status %hX txstatus %X\n",
-                       currticks()-to, status, txstatus);
-#endif
+
+               debug_cond(DEBUG_TX,
+                       "tx timeout/error (%d usecs), status %hX txstatus %lX\n",
+                       10*i, status, txstatus);
+
                rtl_reset(dev);
 
                return 0;
@@ -484,12 +480,11 @@ static int rtl_poll(struct eth_device *dev)
        /* See below for the rest of the interrupt acknowledges.  */
        outw(status & ~(RxFIFOOver | RxOverflow | RxOK), ioaddr + IntrStatus);
 
-#ifdef DEBUG_RX
-       printf("rtl_poll: int %hX ", status);
-#endif
+       debug_cond(DEBUG_RX, "rtl_poll: int %hX ", status);
 
        ring_offs = cur_rx % RX_BUF_LEN;
-       rx_status = *(unsigned int*)KSEG1ADDR((rx_ring + ring_offs));
+       /* ring_offs is guaranteed being 4-byte aligned */
+       rx_status = le32_to_cpu(*(unsigned int *)(rx_ring + ring_offs));
        rx_size = rx_status >> 16;
        rx_status &= 0xffff;
 
@@ -510,15 +505,13 @@ static int rtl_poll(struct eth_device *dev)
                memcpy(&(rxdata[semi_count]), rx_ring, rx_size-4-semi_count);
 
                NetReceive(rxdata, length);
-#ifdef DEBUG_RX
-               printf("rx packet %d+%d bytes", semi_count,rx_size-4-semi_count);
-#endif
+               debug_cond(DEBUG_RX, "rx packet %d+%d bytes",
+                       semi_count, rx_size-4-semi_count);
        } else {
                NetReceive(rx_ring + ring_offs + 4, length);
-#ifdef DEBUG_RX
-               printf("rx packet %d bytes", rx_size-4);
-#endif
+               debug_cond(DEBUG_RX, "rx packet %d bytes", rx_size-4);
        }
+       flush_cache((unsigned long)rx_ring, RX_BUF_LEN);
 
        cur_rx = (cur_rx + rx_size + 4 + 3) & ~3;
        outw(cur_rx - 16, ioaddr + RxBufPtr);
@@ -544,4 +537,3 @@ static void rtl_disable(struct eth_device *dev)
                udelay (100); /* wait 100us */
        }
 }
-#endif