From: Kuo-Jung Su <dant...@faraday-tech.com>

Signed-off-by: Kuo-Jung Su <dant...@faraday-tech.com>
---
 drivers/net/ftgmac100.c |   83 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 56 insertions(+), 27 deletions(-)

diff --git a/drivers/net/ftgmac100.c b/drivers/net/ftgmac100.c
index 69ba57d..0169a42 100644
--- a/drivers/net/ftgmac100.c
+++ b/drivers/net/ftgmac100.c
@@ -7,6 +7,9 @@
  * (C) Copyright 2010 Andes Technology
  * Macpaul Lin <macp...@andestech.com>
  *
+ * (C) Copyright 2010 Faraday Technology
+ * Dante Su <dant...@faraday-tech.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
@@ -28,20 +31,25 @@
 #include <net.h>
 #include <asm/io.h>
 #include <linux/mii.h>
+#include <asm/dma-mapping.h>

 #include "ftgmac100.h"

-#define ETH_ZLEN       60
+#define CFG_XBUF_SIZE          1536
+
+#define ETH_ZLEN                       60

 /* RBSR - hw default init value is also 0x640 */
 #define RBSR_DEFAULT_VALUE     0x640

 /* PKTBUFSTX/PKTBUFSRX must both be power of 2 */
-#define PKTBUFSTX      4       /* must be power of 2 */
+#define PKTBUFSTX                      4       /* must be power of 2 */

 struct ftgmac100_data {
-       struct ftgmac100_txdes txdes[PKTBUFSTX];
-       struct ftgmac100_rxdes rxdes[PKTBUFSRX];
+       ulong txdes_dma;
+       struct ftgmac100_txdes *txdes;
+       ulong rxdes_dma;
+       struct ftgmac100_rxdes *rxdes;
        int tx_index;
        int rx_index;
        int phy_addr;
@@ -375,13 +383,32 @@ static int ftgmac100_init(struct eth_device *dev, bd_t 
*bd)
 {
        struct ftgmac100 *ftgmac100 = (struct ftgmac100 *)dev->iobase;
        struct ftgmac100_data *priv = dev->priv;
-       struct ftgmac100_txdes *txdes = priv->txdes;
-       struct ftgmac100_rxdes *rxdes = priv->rxdes;
+       struct ftgmac100_txdes *txdes;
+       struct ftgmac100_rxdes *rxdes;
        unsigned int maccr;
+       void *buf;
        int i;

        debug("%s()\n", __func__);

+       if (!priv->txdes) {
+               txdes = dma_alloc_coherent(sizeof(*txdes) * PKTBUFSTX, 
&priv->txdes_dma);
+               if (!txdes)
+                       panic("ftgmac100: out of memory\n");
+               memset(txdes, 0, sizeof(*txdes) * PKTBUFSTX);
+               priv->txdes = txdes;
+       }
+       txdes = priv->txdes;
+
+       if (!priv->rxdes) {
+               rxdes = dma_alloc_coherent(sizeof(*rxdes) * PKTBUFSRX, 
&priv->rxdes_dma);
+               if (!rxdes)
+                       panic("ftgmac100: out of memory\n");
+               memset(rxdes, 0, sizeof(*rxdes) * PKTBUFSRX);
+               priv->rxdes = rxdes;
+       }
+       rxdes = priv->rxdes;
+
        /* set the ethernet address */
        ftgmac100_set_mac_from_env(dev);

@@ -397,21 +424,31 @@ static int ftgmac100_init(struct eth_device *dev, bd_t 
*bd)

        for (i = 0; i < PKTBUFSTX; i++) {
                /* TXBUF_BADR */
-               txdes[i].txdes3 = 0;
+               if (!txdes[i].txdes2) {
+                       buf = memalign(64, CFG_XBUF_SIZE);
+                       if (!buf)
+                               panic("ftgmac100: out of memory\n");
+                       txdes[i].txdes3 = virt_to_phys(buf);
+                       txdes[i].txdes2 = (uint)buf;
+               }
                txdes[i].txdes1 = 0;
        }

        for (i = 0; i < PKTBUFSRX; i++) {
                /* RXBUF_BADR */
-               rxdes[i].rxdes3 = (unsigned int)NetRxPackets[i];
+               if (!rxdes[i].rxdes2) {
+                       buf = NetRxPackets[i];
+                       rxdes[i].rxdes3 = virt_to_phys(buf);
+                       rxdes[i].rxdes2 = (uint)buf;
+               }
                rxdes[i].rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
        }

        /* transmit ring */
-       writel((unsigned int)txdes, &ftgmac100->txr_badr);
+       writel(priv->txdes_dma, &ftgmac100->txr_badr);

        /* receive ring */
-       writel((unsigned int)rxdes, &ftgmac100->rxr_badr);
+       writel(priv->rxdes_dma, &ftgmac100->rxr_badr);

        /* poll receive descriptor automatically */
        writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc);
@@ -466,8 +503,11 @@ static int ftgmac100_recv(struct eth_device *dev)
        debug("%s(): RX buffer %d, %x received\n",
               __func__, priv->rx_index, rxlen);

+       /* invalidate d-cache */
+       dma_map_single((void *)curr_des->rxdes2, rxlen, DMA_FROM_DEVICE);
+
        /* pass the packet up to the protocol layers. */
-       NetReceive((void *)curr_des->rxdes3, rxlen);
+       NetReceive((void *)curr_des->rxdes2, rxlen);

        /* release buffer to DMA */
        curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
@@ -485,7 +525,6 @@ static int ftgmac100_send(struct eth_device *dev, void 
*packet, int length)
        struct ftgmac100 *ftgmac100 = (struct ftgmac100 *)dev->iobase;
        struct ftgmac100_data *priv = dev->priv;
        struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index];
-       int start;

        if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
                debug("%s(): no TX descriptor available\n", __func__);
@@ -496,28 +535,18 @@ static int ftgmac100_send(struct eth_device *dev, void 
*packet, int length)

        length = (length < ETH_ZLEN) ? ETH_ZLEN : length;

-       /* initiate a transmit sequence */
-       curr_des->txdes3 = (unsigned int)packet;        /* TXBUF_BADR */
+       memcpy((void *)curr_des->txdes2, (void *)packet, length);
+       dma_map_single((void *)curr_des->txdes2, length, DMA_TO_DEVICE);

-       /* only one descriptor on TXBUF */
        curr_des->txdes0 &= FTGMAC100_TXDES0_EDOTR;
        curr_des->txdes0 |= FTGMAC100_TXDES0_FTS |
-                           FTGMAC100_TXDES0_LTS |
-                           FTGMAC100_TXDES0_TXBUF_SIZE(length) |
-                           FTGMAC100_TXDES0_TXDMA_OWN ;
+                               FTGMAC100_TXDES0_LTS |
+                               FTGMAC100_TXDES0_TXBUF_SIZE(length) |
+                               FTGMAC100_TXDES0_TXDMA_OWN ;

        /* start transmit */
        writel(1, &ftgmac100->txpd);

-       /* wait for transfer to succeed */
-       start = get_timer(0);
-       while (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
-               if (get_timer(0) >= 5) {
-                       debug("%s(): timed out\n", __func__);
-                       return -1;
-               }
-       }
-
        debug("%s(): packet sent\n", __func__);

        priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX;
--
1.7.9.5

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Reply via email to