]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/net/zynq_gem.c
Merge branch 'u-boot/master'
[karo-tx-uboot.git] / drivers / net / zynq_gem.c
index eac9b6f4584466e56d1b9646fb7ff9bae88e53ed..3cadd23bb46aa77529b9a15909f20409588eb4e9 100644 (file)
@@ -6,28 +6,15 @@
  * Based on Xilinx gmac driver:
  * (C) Copyright 2011 Xilinx
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * 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., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <net.h>
+#include <netdev.h>
 #include <config.h>
+#include <fdtdec.h>
+#include <libfdt.h>
 #include <malloc.h>
 #include <asm/io.h>
 #include <phy.h>
 #define ZYNQ_GEM_TXBUF_WRAP_MASK       0x40000000
 #define ZYNQ_GEM_TXBUF_LAST_MASK       0x00008000 /* Last buffer */
 
-#define ZYNQ_GEM_TXSR_HRESPNOK_MASK    0x00000100 /* Transmit hresp not OK */
-#define ZYNQ_GEM_TXSR_URUN_MASK                0x00000040 /* Transmit underrun */
-/* Transmit buffs exhausted mid frame */
-#define ZYNQ_GEM_TXSR_BUFEXH_MASK      0x00000010
-
 #define ZYNQ_GEM_NWCTRL_TXEN_MASK      0x00000008 /* Enable transmit */
 #define ZYNQ_GEM_NWCTRL_RXEN_MASK      0x00000004 /* Enable receive */
 #define ZYNQ_GEM_NWCTRL_MDEN_MASK      0x00000010 /* Enable MDIO port */
  */
 #define PHY_DETECT_MASK 0x1808
 
+/* TX BD status masks */
+#define ZYNQ_GEM_TXBUF_FRMLEN_MASK     0x000007ff
+#define ZYNQ_GEM_TXBUF_EXHAUSTED       0x08000000
+#define ZYNQ_GEM_TXBUF_UNDERRUN                0x10000000
+
+/* Clock frequencies for different speeds */
+#define ZYNQ_GEM_FREQUENCY_10  2500000UL
+#define ZYNQ_GEM_FREQUENCY_100 25000000UL
+#define ZYNQ_GEM_FREQUENCY_1000        125000000UL
+
 /* Device registers */
 struct zynq_gem_regs {
        u32 nwctrl; /* Network Control reg */
@@ -139,12 +131,18 @@ struct emac_bd {
 };
 
 #define RX_BUF 3
+/* Page table entries are set to 1MB, or multiples of 1MB
+ * (not < 1MB). driver uses less bd's so use 1MB bdspace.
+ */
+#define BD_SPACE       0x100000
+/* BD separation space */
+#define BD_SEPRN_SPACE 64
 
 /* Initialized, rxbd_current, rx_first_buf must be 0 after init */
 struct zynq_gem_priv {
-       struct emac_bd tx_bd;
-       struct emac_bd rx_bd[RX_BUF];
-       char rxbuffers[RX_BUF * PKTSIZE_ALIGN];
+       struct emac_bd *tx_bd;
+       struct emac_bd *rx_bd;
+       char *rxbuffers;
        u32 rxbd_current;
        u32 rx_first_buf;
        int phyaddr;
@@ -280,7 +278,8 @@ static int zynq_gem_setup_mac(struct eth_device *dev)
 
 static int zynq_gem_init(struct eth_device *dev, bd_t * bis)
 {
-       u32 i, rclk, clk = 0;
+       u32 i;
+       unsigned long clk_rate = 0;
        struct phy_device *phydev;
        const u32 stat_size = (sizeof(struct zynq_gem_regs) -
                                offsetof(struct zynq_gem_regs, stat)) / 4;
@@ -315,20 +314,18 @@ static int zynq_gem_init(struct eth_device *dev, bd_t * bis)
                        readl(&regs->stat[i]);
 
                /* Setup RxBD space */
-               memset(&(priv->rx_bd), 0, sizeof(priv->rx_bd));
-               /* Create the RxBD ring */
-               memset(&(priv->rxbuffers), 0, sizeof(priv->rxbuffers));
+               memset(priv->rx_bd, 0, RX_BUF * sizeof(struct emac_bd));
 
                for (i = 0; i < RX_BUF; i++) {
                        priv->rx_bd[i].status = 0xF0000000;
                        priv->rx_bd[i].addr =
-                                       (u32)((char *)&(priv->rxbuffers) +
+                                       ((u32)(priv->rxbuffers) +
                                                        (i * PKTSIZE_ALIGN));
                }
                /* WRAP bit to last BD */
                priv->rx_bd[--i].addr |= ZYNQ_GEM_RXBUF_WRAP_MASK;
                /* Write RxBDs to IP */
-               writel((u32)&(priv->rx_bd), &regs->rxqbase);
+               writel((u32)priv->rx_bd, &regs->rxqbase);
 
                /* Setup for DMA Configuration register */
                writel(ZYNQ_GEM_DMACR_INIT, &regs->dmacr);
@@ -342,7 +339,8 @@ static int zynq_gem_init(struct eth_device *dev, bd_t * bis)
        phy_detection(dev);
 
        /* interface - look at tsec */
-       phydev = phy_connect(priv->bus, priv->phyaddr, dev, 0);
+       phydev = phy_connect(priv->bus, priv->phyaddr, dev,
+                            PHY_INTERFACE_MODE_MII);
 
        phydev->supported = supported | ADVERTISED_Pause |
                            ADVERTISED_Asym_Pause;
@@ -351,30 +349,31 @@ static int zynq_gem_init(struct eth_device *dev, bd_t * bis)
        phy_config(phydev);
        phy_startup(phydev);
 
+       if (!phydev->link) {
+               printf("%s: No link.\n", phydev->dev->name);
+               return -1;
+       }
+
        switch (phydev->speed) {
        case SPEED_1000:
                writel(ZYNQ_GEM_NWCFG_INIT | ZYNQ_GEM_NWCFG_SPEED1000,
                       &regs->nwcfg);
-               rclk = (0 << 4) | (1 << 0);
-               clk = (1 << 20) | (8 << 8) | (0 << 4) | (1 << 0);
+               clk_rate = ZYNQ_GEM_FREQUENCY_1000;
                break;
        case SPEED_100:
                clrsetbits_le32(&regs->nwcfg, ZYNQ_GEM_NWCFG_SPEED1000,
                                ZYNQ_GEM_NWCFG_INIT | ZYNQ_GEM_NWCFG_SPEED100);
-               rclk = 1 << 0;
-               clk = (5 << 20) | (8 << 8) | (0 << 4) | (1 << 0);
+               clk_rate = ZYNQ_GEM_FREQUENCY_100;
                break;
        case SPEED_10:
-               rclk = 1 << 0;
-               /* FIXME untested */
-               clk = (5 << 20) | (8 << 8) | (0 << 4) | (1 << 0);
+               clk_rate = ZYNQ_GEM_FREQUENCY_10;
                break;
        }
 
        /* Change the rclk and clk only not using EMIO interface */
        if (!priv->emio)
                zynq_slcr_gem_clk_setup(dev->iobase !=
-                                       ZYNQ_GEM_BASEADDR0, rclk, clk);
+                                       ZYNQ_GEM_BASEADDR0, clk_rate);
 
        setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK |
                                        ZYNQ_GEM_NWCTRL_TXEN_MASK);
@@ -384,32 +383,35 @@ static int zynq_gem_init(struct eth_device *dev, bd_t * bis)
 
 static int zynq_gem_send(struct eth_device *dev, void *ptr, int len)
 {
-       u32 status;
+       u32 addr, size;
        struct zynq_gem_priv *priv = dev->priv;
        struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
-       const u32 mask = ZYNQ_GEM_TXSR_HRESPNOK_MASK | \
-                       ZYNQ_GEM_TXSR_URUN_MASK | ZYNQ_GEM_TXSR_BUFEXH_MASK;
 
        /* setup BD */
-       writel((u32)&(priv->tx_bd), &regs->txqbase);
+       writel((u32)priv->tx_bd, &regs->txqbase);
 
        /* Setup Tx BD */
-       memset((void *)&(priv->tx_bd), 0, sizeof(struct emac_bd));
+       memset(priv->tx_bd, 0, sizeof(struct emac_bd));
+
+       priv->tx_bd->addr = (u32)ptr;
+       priv->tx_bd->status = (len & ZYNQ_GEM_TXBUF_FRMLEN_MASK) |
+                               ZYNQ_GEM_TXBUF_LAST_MASK;
 
-       priv->tx_bd.addr = (u32)ptr;
-       priv->tx_bd.status = len | ZYNQ_GEM_TXBUF_LAST_MASK;
+       addr = (u32) ptr;
+       addr &= ~(ARCH_DMA_MINALIGN - 1);
+       size = roundup(len, ARCH_DMA_MINALIGN);
+       flush_dcache_range(addr, addr + size);
+       barrier();
 
        /* Start transmit */
        setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_STARTTX_MASK);
 
-       /* Read the stat register to know if the packet has been transmitted */
-       status = readl(&regs->txsr);
-       if (status & mask)
-               printf("Something has gone wrong here!? Status is 0x%x.\n",
-                      status);
+       /* Read TX BD status */
+       if (priv->tx_bd->status & ZYNQ_GEM_TXBUF_UNDERRUN)
+               printf("TX underrun\n");
+       if (priv->tx_bd->status & ZYNQ_GEM_TXBUF_EXHAUSTED)
+               printf("TX buffers exhausted in mid frame\n");
 
-       /* Clear Tx status register before leaving . */
-       writel(status, &regs->txsr);
        return 0;
 }
 
@@ -432,8 +434,12 @@ static int zynq_gem_recv(struct eth_device *dev)
 
        frame_len = current_bd->status & ZYNQ_GEM_RXBUF_LEN_MASK;
        if (frame_len) {
-               NetReceive((u8 *) (current_bd->addr &
-                                       ZYNQ_GEM_RXBUF_ADD_MASK), frame_len);
+               u32 addr = current_bd->addr & ZYNQ_GEM_RXBUF_ADD_MASK;
+               addr &= ~(ARCH_DMA_MINALIGN - 1);
+               u32 size = roundup(frame_len, ARCH_DMA_MINALIGN);
+               invalidate_dcache_range(addr, addr + size);
+
+               NetReceive((u8 *)addr, frame_len);
 
                if (current_bd->status & ZYNQ_GEM_RXBUF_SOF_MASK)
                        priv->rx_first_buf = priv->rxbd_current;
@@ -487,6 +493,7 @@ int zynq_gem_initialize(bd_t *bis, int base_addr, int phy_addr, u32 emio)
 {
        struct eth_device *dev;
        struct zynq_gem_priv *priv;
+       void *bd_space;
 
        dev = calloc(1, sizeof(*dev));
        if (dev == NULL)
@@ -499,6 +506,18 @@ int zynq_gem_initialize(bd_t *bis, int base_addr, int phy_addr, u32 emio)
        }
        priv = dev->priv;
 
+       /* Align rxbuffers to ARCH_DMA_MINALIGN */
+       priv->rxbuffers = memalign(ARCH_DMA_MINALIGN, RX_BUF * PKTSIZE_ALIGN);
+       memset(priv->rxbuffers, 0, RX_BUF * PKTSIZE_ALIGN);
+
+       /* Align bd_space to 1MB */
+       bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
+       mmu_set_region_dcache_behaviour((u32)bd_space, BD_SPACE, DCACHE_OFF);
+
+       /* Initialize the bd spaces for tx and rx bd's */
+       priv->tx_bd = (struct emac_bd *)bd_space;
+       priv->rx_bd = (struct emac_bd *)((u32)bd_space + BD_SEPRN_SPACE);
+
        priv->phyaddr = phy_addr;
        priv->emio = emio;
 
@@ -519,3 +538,43 @@ int zynq_gem_initialize(bd_t *bis, int base_addr, int phy_addr, u32 emio)
 
        return 1;
 }
+
+#ifdef CONFIG_OF_CONTROL
+int zynq_gem_of_init(const void *blob)
+{
+       int offset = 0;
+       u32 ret = 0;
+       u32 reg, phy_reg;
+
+       debug("ZYNQ GEM: Initialization\n");
+
+       do {
+               offset = fdt_node_offset_by_compatible(blob, offset,
+                                       "xlnx,ps7-ethernet-1.00.a");
+               if (offset != -1) {
+                       reg = fdtdec_get_addr(blob, offset, "reg");
+                       if (reg != FDT_ADDR_T_NONE) {
+                               offset = fdtdec_lookup_phandle(blob, offset,
+                                                              "phy-handle");
+                               if (offset != -1)
+                                       phy_reg = fdtdec_get_addr(blob, offset,
+                                                                 "reg");
+                               else
+                                       phy_reg = 0;
+
+                               debug("ZYNQ GEM: addr %x, phyaddr %x\n",
+                                     reg, phy_reg);
+
+                               ret |= zynq_gem_initialize(NULL, reg,
+                                                          phy_reg, 0);
+
+                       } else {
+                               debug("ZYNQ GEM: Can't get base address\n");
+                               return -1;
+                       }
+               }
+       } while (offset != -1);
+
+       return ret;
+}
+#endif