Merge git://git.denx.de/u-boot-sh
[oweals/u-boot.git] / drivers / net / tsec.c
index 6829e32dae5f4f071cdf93761a81d176186f9459..c2e755f2b1e343de5817e207e9dfabe9979df8ac 100644 (file)
@@ -1,11 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Freescale Three Speed Ethernet Controller driver
  *
  * Copyright 2004-2011, 2013 Freescale Semiconductor, Inc.
  * (C) Copyright 2003, Motorola, Inc.
  * author Andy Fleming
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <config.h>
@@ -16,6 +15,8 @@
 #include <command.h>
 #include <tsec.h>
 #include <fsl_mdio.h>
+#include <linux/bitops.h>
+#include <linux/delay.h>
 #include <linux/errno.h>
 #include <asm/processor.h>
 #include <asm/io.h>
@@ -79,7 +80,30 @@ static void tsec_configure_serdes(struct tsec_private *priv)
                              0, TBI_CR, CONFIG_TSEC_TBICR_SETTINGS);
 }
 
-#ifdef CONFIG_MCAST_TFTP
+/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
+ * and this is the ethernet-crc method needed for TSEC -- and perhaps
+ * some other adapter -- hash tables
+ */
+#define CRCPOLY_LE 0xedb88320
+static u32 ether_crc(size_t len, unsigned char const *p)
+{
+       int i;
+       u32 crc;
+
+       crc = ~0;
+       while (len--) {
+               crc ^= *p++;
+               for (i = 0; i < 8; i++)
+                       crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+       }
+       /* an reverse the bits, cuz of way they arrive -- last-first */
+       crc = (crc >> 16) | (crc << 16);
+       crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
+       crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
+       crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
+       crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
+       return crc;
+}
 
 /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
 
@@ -100,9 +124,10 @@ static void tsec_configure_serdes(struct tsec_private *priv)
  * the entry.
  */
 #ifndef CONFIG_DM_ETH
-static int tsec_mcast_addr(struct eth_device *dev, const u8 *mcast_mac, u8 set)
+static int tsec_mcast_addr(struct eth_device *dev, const u8 *mcast_mac,
+                          int join)
 #else
-static int tsec_mcast_addr(struct udevice *dev, const u8 *mcast_mac, int set)
+static int tsec_mcast_addr(struct udevice *dev, const u8 *mcast_mac, int join)
 #endif
 {
        struct tsec_private *priv = (struct tsec_private *)dev->priv;
@@ -116,14 +141,13 @@ static int tsec_mcast_addr(struct udevice *dev, const u8 *mcast_mac, int set)
 
        value = BIT(31 - whichbit);
 
-       if (set)
+       if (join)
                setbits_be32(&regs->hash.gaddr0 + whichreg, value);
        else
                clrbits_be32(&regs->hash.gaddr0 + whichreg, value);
 
        return 0;
 }
-#endif /* Multicast TFTP ? */
 
 /*
  * Initialized required registers to appropriate values, zeroing
@@ -237,8 +261,8 @@ static int tsec_send(struct udevice *dev, void *packet, int length)
 {
        struct tsec_private *priv = (struct tsec_private *)dev->priv;
        struct tsec __iomem *regs = priv->regs;
-       u16 status;
        int result = 0;
+       u16 status;
        int i;
 
        /* Find an empty buffer descriptor */
@@ -246,7 +270,7 @@ static int tsec_send(struct udevice *dev, void *packet, int length)
             in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
             i++) {
                if (i >= TOUT_LOOP) {
-                       debug("%s: tsec: tx buffers full\n", dev->name);
+                       printf("%s: tsec: tx buffers full\n", dev->name);
                        return result;
                }
        }
@@ -265,7 +289,7 @@ static int tsec_send(struct udevice *dev, void *packet, int length)
             in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
             i++) {
                if (i >= TOUT_LOOP) {
-                       debug("%s: tsec: tx error\n", dev->name);
+                       printf("%s: tsec: tx error\n", dev->name);
                        return result;
                }
        }
@@ -538,6 +562,8 @@ static int tsec_init(struct udevice *dev)
        struct tsec_private *priv = (struct tsec_private *)dev->priv;
 #ifdef CONFIG_DM_ETH
        struct eth_pdata *pdata = dev_get_platdata(dev);
+#else
+       struct eth_device *pdata = dev;
 #endif
        struct tsec __iomem *regs = priv->regs;
        u32 tempval;
@@ -558,21 +584,12 @@ static int tsec_init(struct udevice *dev)
         * order (BE), MACnADDR1 is set to 0xCDAB7856 and
         * MACnADDR2 is set to 0x34120000.
         */
-#ifndef CONFIG_DM_ETH
-       tempval = (dev->enetaddr[5] << 24) | (dev->enetaddr[4] << 16) |
-                 (dev->enetaddr[3] << 8)  |  dev->enetaddr[2];
-#else
        tempval = (pdata->enetaddr[5] << 24) | (pdata->enetaddr[4] << 16) |
                  (pdata->enetaddr[3] << 8)  |  pdata->enetaddr[2];
-#endif
 
        out_be32(&regs->macstnaddr1, tempval);
 
-#ifndef CONFIG_DM_ETH
-       tempval = (dev->enetaddr[1] << 24) | (dev->enetaddr[0] << 16);
-#else
        tempval = (pdata->enetaddr[1] << 24) | (pdata->enetaddr[0] << 16);
-#endif
 
        out_be32(&regs->macstnaddr2, tempval);
 
@@ -686,9 +703,9 @@ static int init_phy(struct tsec_private *priv)
  */
 static int tsec_initialize(bd_t *bis, struct tsec_info_struct *tsec_info)
 {
+       struct tsec_private *priv;
        struct eth_device *dev;
        int i;
-       struct tsec_private *priv;
 
        dev = (struct eth_device *)malloc(sizeof(*dev));
 
@@ -721,9 +738,7 @@ static int tsec_initialize(bd_t *bis, struct tsec_info_struct *tsec_info)
        dev->halt = tsec_halt;
        dev->send = tsec_send;
        dev->recv = tsec_recv;
-#ifdef CONFIG_MCAST_TFTP
        dev->mcast = tsec_mcast_addr;
-#endif
 
        /* Tell U-Boot to get the addr from the env */
        for (i = 0; i < 6; i++)
@@ -774,12 +789,14 @@ int tsec_standard_init(bd_t *bis)
 #else /* CONFIG_DM_ETH */
 int tsec_probe(struct udevice *dev)
 {
-       struct tsec_private *priv = dev_get_priv(dev);
        struct eth_pdata *pdata = dev_get_platdata(dev);
-       struct fsl_pq_mdio_info mdio_info;
+       struct tsec_private *priv = dev_get_priv(dev);
        struct ofnode_phandle_args phandle_args;
-       ofnode parent;
+       u32 tbiaddr = CONFIG_SYS_TBIPA_VALUE;
+       struct fsl_pq_mdio_info mdio_info;
        const char *phy_mode;
+       fdt_addr_t reg;
+       ofnode parent;
        int ret;
 
        pdata->iobase = (phys_addr_t)dev_read_addr(dev);
@@ -787,7 +804,7 @@ int tsec_probe(struct udevice *dev)
 
        if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
                                       &phandle_args)) {
-               debug("phy-handle does not exist under tsec %s\n", dev->name);
+               printf("phy-handle does not exist under tsec %s\n", dev->name);
                return -ENOENT;
        } else {
                int reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
@@ -796,28 +813,27 @@ int tsec_probe(struct udevice *dev)
        }
 
        parent = ofnode_get_parent(phandle_args.node);
-       if (ofnode_valid(parent)) {
-               int reg = ofnode_read_u32_default(parent, "reg", 0);
-               priv->phyregs_sgmii = (struct tsec_mii_mng *)(reg + 0x520);
-       } else {
-               debug("No parent node for PHY?\n");
+       if (!ofnode_valid(parent)) {
+               printf("No parent node for PHY?\n");
                return -ENOENT;
        }
 
-       if (dev_read_phandle_with_args(dev, "tbi-handle", NULL, 0, 0,
-                                      &phandle_args)) {
-               priv->tbiaddr = CONFIG_SYS_TBIPA_VALUE;
-       } else {
-               int reg = ofnode_read_u32_default(phandle_args.node, "reg",
-                                                 CONFIG_SYS_TBIPA_VALUE);
-               priv->tbiaddr = reg;
-       }
+       reg = ofnode_get_addr_index(parent, 0);
+       priv->phyregs_sgmii = (struct tsec_mii_mng *)
+                       (reg + TSEC_MDIO_REGS_OFFSET);
+
+       ret = dev_read_phandle_with_args(dev, "tbi-handle", NULL, 0, 0,
+                                        &phandle_args);
+       if (ret == 0)
+               ofnode_read_u32(phandle_args.node, "reg", &tbiaddr);
+
+       priv->tbiaddr = tbiaddr;
 
        phy_mode = dev_read_prop(dev, "phy-connection-type", NULL);
        if (phy_mode)
                pdata->phy_interface = phy_get_interface_by_name(phy_mode);
        if (pdata->phy_interface == -1) {
-               debug("Invalid PHY interface '%s'\n", phy_mode);
+               printf("Invalid PHY interface '%s'\n", phy_mode);
                return -EINVAL;
        }
        priv->interface = pdata->phy_interface;
@@ -862,13 +878,11 @@ static const struct eth_ops tsec_ops = {
        .recv = tsec_recv,
        .free_pkt = tsec_free_pkt,
        .stop = tsec_halt,
-#ifdef CONFIG_MCAST_TFTP
        .mcast = tsec_mcast_addr,
-#endif
 };
 
 static const struct udevice_id tsec_ids[] = {
-       { .compatible = "fsl,tsec" },
+       { .compatible = "fsl,etsec2" },
        { }
 };