]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/net/sunxi_emac.c
nand, atmel: remove udelay in spl_nand_erase_one()
[karo-tx-uboot.git] / drivers / net / sunxi_emac.c
index b9fd1b854a4c1fa391decc45ba5c6df3952d0a43..11cd0ea06888ba8e271b0c10376306df5291a3e2 100644 (file)
@@ -7,6 +7,7 @@
  */
 
 #include <common.h>
+#include <dm.h>
 #include <linux/err.h>
 #include <malloc.h>
 #include <miiphy.h>
@@ -153,13 +154,16 @@ struct sunxi_sramc_regs {
 #define EMAC_CRCERR            (0x1 << 4)
 #define EMAC_LENERR            (0x3 << 5)
 
-#define DMA_CPU_TRRESHOLD      2000
+#define EMAC_RX_BUFSIZE                2000
 
 struct emac_eth_dev {
        struct emac_regs *regs;
        struct mii_dev *bus;
        struct phy_device *phydev;
        int link_printed;
+#ifdef CONFIG_DM_ETH
+       uchar rx_buf[EMAC_RX_BUFSIZE];
+#endif
 };
 
 struct emac_rxhdr {
@@ -309,9 +313,9 @@ static void emac_setup(struct emac_eth_dev *priv)
        writel(EMAC_MAC_MFL, &regs->mac_maxf);
 }
 
-static void emac_reset(struct eth_device *dev)
+static void emac_reset(struct emac_eth_dev *priv)
 {
-       struct emac_regs *regs = (struct emac_regs *)dev->iobase;
+       struct emac_regs *regs = priv->regs;
 
        debug("resetting device\n");
 
@@ -323,10 +327,9 @@ static void emac_reset(struct eth_device *dev)
        udelay(200);
 }
 
-static int sunxi_emac_eth_init(struct eth_device *dev, bd_t *bd)
+static int _sunxi_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
 {
-       struct emac_regs *regs = (struct emac_regs *)dev->iobase;
-       struct emac_eth_dev *priv = dev->priv;
+       struct emac_regs *regs = priv->regs;
        int ret;
 
        /* Init EMAC */
@@ -347,14 +350,14 @@ static int sunxi_emac_eth_init(struct eth_device *dev, bd_t *bd)
        /* Set up EMAC */
        emac_setup(priv);
 
-       writel(dev->enetaddr[0] << 16 | dev->enetaddr[1] << 8 |
-              dev->enetaddr[2], &regs->mac_a1);
-       writel(dev->enetaddr[3] << 16 | dev->enetaddr[4] << 8 |
-              dev->enetaddr[5], &regs->mac_a0);
+       writel(enetaddr[0] << 16 | enetaddr[1] << 8 | enetaddr[2],
+              &regs->mac_a1);
+       writel(enetaddr[3] << 16 | enetaddr[4] << 8 | enetaddr[5],
+              &regs->mac_a0);
 
        mdelay(1);
 
-       emac_reset(dev);
+       emac_reset(priv);
 
        /* PHY POWER UP */
        ret = phy_startup(priv->phydev);
@@ -390,14 +393,9 @@ static int sunxi_emac_eth_init(struct eth_device *dev, bd_t *bd)
        return 0;
 }
 
-static void sunxi_emac_eth_halt(struct eth_device *dev)
-{
-       /* Nothing to do here */
-}
-
-static int sunxi_emac_eth_recv(struct eth_device *dev)
+static int _sunxi_emac_eth_recv(struct emac_eth_dev *priv, void *packet)
 {
-       struct emac_regs *regs = (struct emac_regs *)dev->iobase;
+       struct emac_regs *regs = priv->regs;
        struct emac_rxhdr rxhdr;
        u32 rxcount;
        u32 reg_val;
@@ -415,7 +413,7 @@ static int sunxi_emac_eth_recv(struct eth_device *dev)
                /* Had one stuck? */
                rxcount = readl(&regs->rx_fbc);
                if (!rxcount)
-                       return 0;
+                       return -EAGAIN;
        }
 
        reg_val = readl(&regs->rx_io_data);
@@ -431,7 +429,7 @@ static int sunxi_emac_eth_recv(struct eth_device *dev)
                /* Enable RX */
                setbits_le32(&regs->ctl, 0x1 << 2);
 
-               return 0;
+               return -EAGAIN;
        }
 
        /* A packet ready now
@@ -461,24 +459,21 @@ static int sunxi_emac_eth_recv(struct eth_device *dev)
 
        /* Move data from EMAC */
        if (good_packet) {
-               if (rx_len > DMA_CPU_TRRESHOLD) {
+               if (rx_len > EMAC_RX_BUFSIZE) {
                        printf("Received packet is too big (len=%d)\n", rx_len);
-               } else {
-                       emac_inblk_32bit((void *)&regs->rx_io_data,
-                                        net_rx_packets[0], rx_len);
-
-                       /* Pass to upper layer */
-                       net_process_received_packet(net_rx_packets[0], rx_len);
-                       return rx_len;
+                       return -EMSGSIZE;
                }
+               emac_inblk_32bit((void *)&regs->rx_io_data, packet, rx_len);
+               return rx_len;
        }
 
-       return 0;
+       return -EIO; /* Bad packet */
 }
 
-static int sunxi_emac_eth_send(struct eth_device *dev, void *packet, int len)
+static int _sunxi_emac_eth_send(struct emac_eth_dev *priv, void *packet,
+                               int len)
 {
-       struct emac_regs *regs = (struct emac_regs *)dev->iobase;
+       struct emac_regs *regs = priv->regs;
 
        /* Select channel 0 */
        writel(0, &regs->tx_ins);
@@ -495,31 +490,15 @@ static int sunxi_emac_eth_send(struct eth_device *dev, void *packet, int len)
        return 0;
 }
 
-int sunxi_emac_initialize(void)
+static void sunxi_emac_board_setup(struct emac_eth_dev *priv)
 {
        struct sunxi_ccm_reg *const ccm =
                (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
        struct sunxi_sramc_regs *sram =
                (struct sunxi_sramc_regs *)SUNXI_SRAMC_BASE;
-       struct emac_regs *regs =
-               (struct emac_regs *)SUNXI_EMAC_BASE;
-       struct eth_device *dev;
-       struct emac_eth_dev *priv;
+       struct emac_regs *regs = priv->regs;
        int pin;
 
-       dev = malloc(sizeof(*dev));
-       if (dev == NULL)
-               return -ENOMEM;
-
-       priv = (struct emac_eth_dev *)malloc(sizeof(struct emac_eth_dev));
-       if (!priv) {
-               free(dev);
-               return -ENOMEM;
-       }
-
-       memset(dev, 0, sizeof(*dev));
-       memset(priv, 0, sizeof(struct emac_eth_dev));
-
        /* Map SRAM to EMAC */
        setbits_le32(&sram->ctrl1, 0x5 << 2);
 
@@ -532,17 +511,77 @@ int sunxi_emac_initialize(void)
 
        /* Set MII clock */
        clrsetbits_le32(&regs->mac_mcfg, 0xf << 2, 0xd << 2);
+}
+
+static int sunxi_emac_eth_start(struct udevice *dev)
+{
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+
+       return _sunxi_emac_eth_init(dev->priv, pdata->enetaddr);
+}
+
+static int sunxi_emac_eth_send(struct udevice *dev, void *packet, int length)
+{
+       struct emac_eth_dev *priv = dev_get_priv(dev);
 
-       priv->regs = regs;
-       dev->iobase = (int)regs;
-       dev->priv = priv;
-       dev->init = sunxi_emac_eth_init;
-       dev->halt = sunxi_emac_eth_halt;
-       dev->send = sunxi_emac_eth_send;
-       dev->recv = sunxi_emac_eth_recv;
-       strcpy(dev->name, "emac");
+       return _sunxi_emac_eth_send(priv, packet, length);
+}
+
+static int sunxi_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
+{
+       struct emac_eth_dev *priv = dev_get_priv(dev);
+       int rx_len;
+
+       rx_len = _sunxi_emac_eth_recv(priv, priv->rx_buf);
+       *packetp = priv->rx_buf;
+
+       return rx_len;
+}
+
+static void sunxi_emac_eth_stop(struct udevice *dev)
+{
+       /* Nothing to do here */
+}
+
+static int sunxi_emac_eth_probe(struct udevice *dev)
+{
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+       struct emac_eth_dev *priv = dev_get_priv(dev);
 
-       eth_register(dev);
+       priv->regs = (struct emac_regs *)pdata->iobase;
+       sunxi_emac_board_setup(priv);
 
        return sunxi_emac_init_phy(priv, dev);
 }
+
+static const struct eth_ops sunxi_emac_eth_ops = {
+       .start                  = sunxi_emac_eth_start,
+       .send                   = sunxi_emac_eth_send,
+       .recv                   = sunxi_emac_eth_recv,
+       .stop                   = sunxi_emac_eth_stop,
+};
+
+static int sunxi_emac_eth_ofdata_to_platdata(struct udevice *dev)
+{
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+
+       pdata->iobase = dev_get_addr(dev);
+
+       return 0;
+}
+
+static const struct udevice_id sunxi_emac_eth_ids[] = {
+       { .compatible = "allwinner,sun4i-a10-emac" },
+       { }
+};
+
+U_BOOT_DRIVER(eth_sunxi_emac) = {
+       .name   = "eth_sunxi_emac",
+       .id     = UCLASS_ETH,
+       .of_match = sunxi_emac_eth_ids,
+       .ofdata_to_platdata = sunxi_emac_eth_ofdata_to_platdata,
+       .probe  = sunxi_emac_eth_probe,
+       .ops    = &sunxi_emac_eth_ops,
+       .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
+       .platdata_auto_alloc_size = sizeof(struct eth_pdata),
+};