common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / net / at91_emac.c
index 97d273999af0514375557ae1de9bfd32603588ec..4e4ad61da4ef1ccb66c5c8f2f51237cc8a49bfa2 100644 (file)
@@ -1,48 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2009 BuS Elektronik GmbH & Co. KG
  * Jens Scharsig (esw@bus-elektronik.de)
  *
  * (C) Copyright 2003
  * Author : Hamid Ikdoumi (Atmel)
-
- * 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
  */
 
 #include <common.h>
+#include <log.h>
 #include <asm/io.h>
-#ifndef CONFIG_AT91_LEGACY
 #include <asm/arch/hardware.h>
 #include <asm/arch/at91_emac.h>
-#include <asm/arch/at91_pmc.h>
+#include <asm/arch/clk.h>
 #include <asm/arch/at91_pio.h>
-#else
-/* remove next 5 lines, if all RM9200 boards convert to at91 arch */
-#include <asm/arch-at91/at91rm9200.h>
-#include <asm/arch-at91/hardware.h>
-#include <asm/arch-at91/at91_emac.h>
-#include <asm/arch-at91/at91_pmc.h>
-#include <asm/arch-at91/at91_pio.h>
-#endif
 #include <net.h>
 #include <netdev.h>
 #include <malloc.h>
 #include <miiphy.h>
+#include <linux/delay.h>
 #include <linux/mii.h>
 
 #undef MII_DEBUG
 #endif
 
 #ifdef ET_DEBUG
-#define DEBUG_AT91EMAC(...)    printf(__VA_ARGS__);
+#define DEBUG_AT91EMAC 1
 #else
-#define DEBUG_AT91EMAC(...)
+#define DEBUG_AT91EMAC 0
 #endif
 
 #ifdef MII_DEBUG
-#define DEBUG_AT91PHY(...)     printf(__VA_ARGS__);
+#define DEBUG_AT91PHY  1
 #else
-#define DEBUG_AT91PHY(...)
+#define DEBUG_AT91PHY  0
 #endif
 
 #ifndef CONFIG_DRIVER_AT91EMAC_QUIET
-#define VERBOSEP(...)  printf(__VA_ARGS__);
+#define VERBOSEP       1
 #else
-#define VERBOSEP(...)
+#define VERBOSEP       0
 #endif
 
 #define RBF_ADDR      0xfffffffc
@@ -137,14 +113,15 @@ int  at91emac_read(at91_emac_t *at91mac, unsigned char addr,
 
        do {
                netstat = readl(&at91mac->sr);
-               DEBUG_AT91PHY("poll SR %08lx\n", netstat);
+               debug_cond(DEBUG_AT91PHY, "poll SR %08lx\n", netstat);
        } while (!(netstat & AT91_EMAC_SR_IDLE));
 
        *value = readl(&at91mac->man) & AT91_EMAC_MAN_DATA_MASK;
 
        at91emac_DisableMDIO(at91mac);
 
-       DEBUG_AT91PHY("AT91PHY read %x REG(%d)=%x\n", at91mac, reg, *value)
+       debug_cond(DEBUG_AT91PHY,
+               "AT91PHY read %p REG(%d)=%x\n", at91mac, reg, *value);
 
        return 0;
 }
@@ -153,7 +130,8 @@ int  at91emac_write(at91_emac_t *at91mac, unsigned char addr,
                unsigned char reg, unsigned short value)
 {
        unsigned long netstat;
-       DEBUG_AT91PHY("AT91PHY write %x REG(%d)=%x\n", at91mac, reg, &value)
+       debug_cond(DEBUG_AT91PHY,
+               "AT91PHY write %p REG(%d)=%p\n", at91mac, reg, &value);
 
        at91emac_EnableMDIO(at91mac);
 
@@ -164,7 +142,7 @@ int  at91emac_write(at91_emac_t *at91mac, unsigned char addr,
 
        do {
                netstat = readl(&at91mac->sr);
-               DEBUG_AT91PHY("poll SR %08lx\n", netstat);
+               debug_cond(DEBUG_AT91PHY, "poll SR %08lx\n", netstat);
        } while (!(netstat & AT91_EMAC_SR_IDLE));
 
        at91emac_DisableMDIO(at91mac);
@@ -182,23 +160,23 @@ at91_emac_t *get_emacbase_by_name(const char *devname)
        return (at91_emac_t *) netdev->iobase;
 }
 
-int  at91emac_mii_read(const char *devname, unsigned char addr,
-               unsigned char reg, unsigned short *value)
+int at91emac_mii_read(struct mii_dev *bus, int addr, int devad, int reg)
 {
+       unsigned short value = 0;
        at91_emac_t *emac;
 
-       emac = get_emacbase_by_name(devname);
-       at91emac_read(emac , addr, reg, value);
-       return 0;
+       emac = get_emacbase_by_name(bus->name);
+       at91emac_read(emac , addr, reg, &value);
+       return value;
 }
 
 
-int  at91emac_mii_write(const char *devname, unsigned char addr,
-               unsigned char reg, unsigned short value)
+int at91emac_mii_write(struct mii_dev *bus, int addr, int devad, int reg,
+                      u16 value)
 {
        at91_emac_t *emac;
 
-       emac = get_emacbase_by_name(devname);
+       emac = get_emacbase_by_name(bus->name);
        at91emac_write(emac, addr, reg, value);
        return 0;
 }
@@ -216,7 +194,7 @@ static int at91emac_phy_reset(struct eth_device *netdev)
        adv = ADVERTISE_CSMA | ADVERTISE_ALL;
        at91emac_write(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
                MII_ADVERTISE, adv);
-       VERBOSEP("%s: Starting autonegotiation...\n", netdev->name);
+       debug_cond(VERBOSEP, "%s: Starting autonegotiation...\n", netdev->name);
        at91emac_write(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR, MII_BMCR,
                (BMCR_ANENABLE | BMCR_ANRESTART));
 
@@ -229,7 +207,8 @@ static int at91emac_phy_reset(struct eth_device *netdev)
        }
 
        if (status & BMSR_ANEGCOMPLETE) {
-               VERBOSEP("%s: Autonegotiation complete\n", netdev->name);
+               debug_cond(VERBOSEP,
+                       "%s: Autonegotiation complete\n", netdev->name);
        } else {
                printf("%s: Autonegotiation timed out (status=0x%04x)\n",
                       netdev->name, status);
@@ -272,7 +251,7 @@ static int at91emac_phy_init(struct eth_device *netdev)
                }
        }
        if (!(status & BMSR_LSTATUS)) {
-               VERBOSEP("%s: link down\n", netdev->name);
+               debug_cond(VERBOSEP, "%s: link down\n", netdev->name);
                return -3;
        } else {
                at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
@@ -283,7 +262,7 @@ static int at91emac_phy_init(struct eth_device *netdev)
                speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
                         ? 1 : 0);
                duplex = (media & ADVERTISE_FULL) ? 1 : 0;
-               VERBOSEP("%s: link up, %sMbps %s-duplex\n",
+               debug_cond(VERBOSEP, "%s: link up, %sMbps %s-duplex\n",
                       netdev->name,
                       speed ? "100" : "10",
                       duplex ? "full" : "half");
@@ -343,7 +322,6 @@ static int at91emac_init(struct eth_device *netdev, bd_t *bd)
        emac_device *dev;
        at91_emac_t *emac;
        at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIO;
-       at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
 
        emac = (at91_emac_t *) netdev->iobase;
        dev = (emac_device *) netdev->priv;
@@ -356,7 +334,7 @@ static int at91emac_init(struct eth_device *netdev, bd_t *bd)
                ATMEL_PMX_AA_ETXEN |    ATMEL_PMX_AA_EREFCK;
 
        writel(value, &pio->pioa.pdr);
-       writel(value, &pio->pioa.asr);
+       writel(value, &pio->pioa.mux.pio2.asr);
 
 #ifdef CONFIG_RMII
        value = ATMEL_PMX_BA_ERXCK;
@@ -367,14 +345,15 @@ static int at91emac_init(struct eth_device *netdev, bd_t *bd)
                ATMEL_PMX_BA_ETX3 |     ATMEL_PMX_BA_ETX2;
 #endif
        writel(value, &pio->piob.pdr);
-       writel(value, &pio->piob.bsr);
+       writel(value, &pio->piob.mux.pio2.bsr);
+
+       at91_periph_clk_enable(ATMEL_ID_EMAC);
 
-       writel(1 << ATMEL_ID_EMAC, &pmc->pcer);
        writel(readl(&emac->ctl) | AT91_EMAC_CTL_CSR, &emac->ctl);
 
        /* Init Ethernet buffers */
        for (i = 0; i < RBF_FRAMEMAX; i++) {
-               dev->rbfdt[i].addr = (unsigned long) NetRxPackets[i];
+               dev->rbfdt[i].addr = (unsigned long) net_rx_packets[i];
                dev->rbfdt[i].size = 0;
        }
        dev->rbfdt[RBF_FRAMEMAX - 1].addr |= RBF_WRAP;
@@ -409,11 +388,10 @@ static void at91emac_halt(struct eth_device *netdev)
        emac = (at91_emac_t *) netdev->iobase;
        writel(readl(&emac->ctl) & ~(AT91_EMAC_CTL_TE | AT91_EMAC_CTL_RE),
                &emac->ctl);
-       DEBUG_AT91EMAC("halt MAC\n");
+       debug_cond(DEBUG_AT91EMAC, "halt MAC\n");
 }
 
-static int at91emac_send(struct eth_device *netdev, volatile void *packet,
-                    int length)
+static int at91emac_send(struct eth_device *netdev, void *packet, int length)
 {
        at91_emac_t *emac;
 
@@ -425,7 +403,7 @@ static int at91emac_send(struct eth_device *netdev, volatile void *packet,
        writel(AT91_EMAC_TCR_LEN(length), &emac->tcr);
        while (AT91_EMAC_TCR_LEN(readl(&emac->tcr)))
                ;
-       DEBUG_AT91EMAC("Send %d \n", length);
+       debug_cond(DEBUG_AT91EMAC, "Send %d\n", length);
        writel(readl(&emac->tsr) | AT91_EMAC_TSR_COMP, &emac->tsr);
        return 0;
 }
@@ -443,9 +421,9 @@ static int at91emac_recv(struct eth_device *netdev)
        rbfp = &dev->rbfdt[dev->rbindex];
        while (rbfp->addr & RBF_OWNER)  {
                size = rbfp->size & RBF_SIZE;
-               NetReceive(NetRxPackets[dev->rbindex], size);
+               net_process_received_packet(net_rx_packets[dev->rbindex], size);
 
-               DEBUG_AT91EMAC("Recv[%d]: %d bytes @ %x \n",
+               debug_cond(DEBUG_AT91EMAC, "Recv[%ld]: %d bytes @ %lx\n",
                        dev->rbindex, size, rbfp->addr);
 
                rbfp->addr &= ~RBF_OWNER;
@@ -474,21 +452,20 @@ static int at91emac_recv(struct eth_device *netdev)
 
 static int at91emac_write_hwaddr(struct eth_device *netdev)
 {
-       emac_device *dev;
        at91_emac_t *emac;
-       at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
        emac = (at91_emac_t *) netdev->iobase;
-       dev = (emac_device *) netdev->priv;
 
-       writel(1 << ATMEL_ID_EMAC, &pmc->pcer);
-       DEBUG_AT91EMAC("init MAC-ADDR %02x:%02x:%02x:%02x:%02x:%02x\n",
+       at91_periph_clk_enable(ATMEL_ID_EMAC);
+
+       debug_cond(DEBUG_AT91EMAC,
+               "init MAC-ADDR %02x:%02x:%02x:%02x:%02x:%02x\n",
                netdev->enetaddr[5], netdev->enetaddr[4], netdev->enetaddr[3],
                netdev->enetaddr[2], netdev->enetaddr[1], netdev->enetaddr[0]);
        writel( (netdev->enetaddr[0] | netdev->enetaddr[1] << 8 |
                        netdev->enetaddr[2] << 16 | netdev->enetaddr[3] << 24),
                        &emac->sa2l);
        writel((netdev->enetaddr[4] | netdev->enetaddr[5] << 8), &emac->sa2h);
-       DEBUG_AT91EMAC("init MAC-ADDR %x%x \n",
+       debug_cond(DEBUG_AT91EMAC, "init MAC-ADDR %x%x\n",
                readl(&emac->sa2h), readl(&emac->sa2l));
        return 0;
 }
@@ -514,7 +491,7 @@ int at91emac_register(bd_t *bis, unsigned long iobase)
        memset(emacfix, 0, sizeof(emac_device));
 
        memset(dev, 0, sizeof(*dev));
-       sprintf(dev->name, "emac");
+       strcpy(dev->name, "emac");
        dev->iobase = iobase;
        dev->priv = emacfix;
        dev->init = at91emac_init;
@@ -526,7 +503,17 @@ int at91emac_register(bd_t *bis, unsigned long iobase)
        eth_register(dev);
 
 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
-       miiphy_register(dev->name, at91emac_mii_read, at91emac_mii_write);
+       int retval;
+       struct mii_dev *mdiodev = mdio_alloc();
+       if (!mdiodev)
+               return -ENOMEM;
+       strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
+       mdiodev->read = at91emac_mii_read;
+       mdiodev->write = at91emac_mii_write;
+
+       retval = mdio_register(mdiodev);
+       if (retval < 0)
+               return retval;
 #endif
        return 1;
 }