drivers: mcfmii: add dm support
[oweals/u-boot.git] / drivers / net / mcfmii.c
index 5e64dbdd177548c40a8913ffdc481b02120ee4a1..3b8ee07c13a073ae75115b9a338d2d42e1947e7e 100644 (file)
@@ -1,24 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2004-2008 Freescale Semiconductor, Inc.
  * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
- *
- * 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>
@@ -32,6 +15,7 @@
 #include <asm/fec.h>
 #endif
 #include <asm/immap.h>
+#include <linux/mii.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -57,14 +41,6 @@ DECLARE_GLOBAL_DATA_PTR;
 #      define CONFIG_SYS_UNSPEC_STRID          0
 #endif
 
-#ifdef CONFIG_MCF547x_8x
-typedef struct fec_info_dma FEC_INFO_T;
-#define FEC_T fecdma_t
-#else
-typedef struct fec_info_s FEC_INFO_T;
-#define FEC_T fec_t
-#endif
-
 typedef struct phy_info_struct {
        u32 phyid;
        char *strid;
@@ -94,7 +70,7 @@ phy_info_t phyinfo[] = {
  * mii_init -- Initialize the MII for MII command without ethernet
  * This function is a subset of eth_init
  */
-void mii_reset(FEC_INFO_T *info)
+void mii_reset(fec_info_t *info)
 {
        volatile FEC_T *fecp = (FEC_T *) (info->miibase);
        int i;
@@ -111,9 +87,13 @@ void mii_reset(FEC_INFO_T *info)
 /* send command to phy using mii, wait for result */
 uint mii_send(uint mii_cmd)
 {
-       FEC_INFO_T *info;
-       volatile FEC_T *ep;
+#ifdef CONFIG_DM_ETH
+       struct udevice *dev;
+#else
        struct eth_device *dev;
+#endif
+       fec_info_t *info;
+       volatile FEC_T *ep;
        uint mii_reply;
        int j = 0;
 
@@ -126,11 +106,11 @@ uint mii_send(uint mii_cmd)
        ep->mmfr = mii_cmd;     /* command to phy */
 
        /* wait for mii complete */
-       while (!(ep->eir & FEC_EIR_MII) && (j < MCFFEC_TOUT_LOOP)) {
+       while (!(ep->eir & FEC_EIR_MII) && (j < info->to_loop)) {
                udelay(1);
                j++;
        }
-       if (j >= MCFFEC_TOUT_LOOP) {
+       if (j >= info->to_loop) {
                printf("MII not complete\n");
                return -1;
        }
@@ -147,10 +127,9 @@ uint mii_send(uint mii_cmd)
 #endif                         /* CONFIG_SYS_DISCOVER_PHY || (CONFIG_MII) */
 
 #if defined(CONFIG_SYS_DISCOVER_PHY)
-int mii_discover_phy(struct eth_device *dev)
+int mii_discover_phy(fec_info_t *info)
 {
 #define MAX_PHY_PASSES 11
-       FEC_INFO_T *info = dev->priv;
        int phyaddr, pass;
        uint phyno, phytype;
        int i, found = 0;
@@ -173,7 +152,7 @@ int mii_discover_phy(struct eth_device *dev)
 
                        phytype = mii_send(mk_mii_read(phyno, MII_PHYSID1));
 #ifdef ET_DEBUG
-                       printf("PHY type 0x%x pass %d type\n", phytype, pass);
+                       printf("PHY type 0x%x pass %d\n", phytype, pass);
 #endif
                        if (phytype == 0xffff)
                                continue;
@@ -186,7 +165,7 @@ int mii_discover_phy(struct eth_device *dev)
                        printf("PHY @ 0x%x pass %d\n", phyno, pass);
 #endif
 
-                       for (i = 0; (i < (sizeof(phyinfo) / sizeof(phy_info_t)))
+                       for (i = 0; (i < ARRAY_SIZE(phyinfo))
                                && (phyinfo[i].phyid != 0); i++) {
                                if (phyinfo[i].phyid == phytype) {
 #ifdef ET_DEBUG
@@ -223,9 +202,13 @@ void mii_init(void) __attribute__((weak,alias("__mii_init")));
 
 void __mii_init(void)
 {
-       FEC_INFO_T *info;
-       volatile FEC_T *fecp;
+#ifdef CONFIG_DM_ETH
+       struct udevice *dev;
+#else
        struct eth_device *dev;
+#endif
+       fec_info_t *info;
+       volatile FEC_T *fecp;
        int miispd = 0, i = 0;
        u16 status = 0;
        u16 linkgood = 0;
@@ -236,7 +219,7 @@ void __mii_init(void)
 
        fecp = (FEC_T *) info->miibase;
 
-       fecpin_setclear(dev, 1);
+       fecpin_setclear(info, 1);
 
        mii_reset(info);
 
@@ -250,9 +233,13 @@ void __mii_init(void)
        miispd = (gd->bus_clk / 1000000) / 5;
        fecp->mscr = miispd << 1;
 
-       info->phy_addr = mii_discover_phy(dev);
+#ifdef CONFIG_SYS_DISCOVER_PHY
+       info->phy_addr = mii_discover_phy(info);
+#endif
+       if (info->phy_addr == -1)
+               return;
 
-       while (i < MCFFEC_TOUT_LOOP) {
+       while (i < info->to_loop) {
                status = 0;
                i++;
                /* Read PHY control register */
@@ -273,9 +260,8 @@ void __mii_init(void)
 
                udelay(1);
        }
-       if (i >= MCFFEC_TOUT_LOOP) {
+       if (i >= info->to_loop)
                printf("Link UP timeout\n");
-       }
 
        /* adapt to the duplex and speed settings of the phy */
        info->dup_spd = miiphy_duplex(dev->name, info->phy_addr) << 16;
@@ -293,8 +279,7 @@ void __mii_init(void)
  *       Otherwise they hang in mii_send() !!! Sorry!
  */
 
-int mcffec_miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
-                      unsigned short *value)
+int mcffec_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
 {
        short rdreg;            /* register working value */
 
@@ -303,28 +288,22 @@ int mcffec_miiphy_read(const char *devname, unsigned char addr, unsigned char re
 #endif
        rdreg = mii_send(mk_mii_read(addr, reg));
 
-       *value = rdreg;
-
 #ifdef MII_DEBUG
-       printf("0x%04x\n", *value);
+       printf("0x%04x\n", rdreg);
 #endif
 
-       return 0;
+       return rdreg;
 }
 
-int mcffec_miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
-                       unsigned short value)
+int mcffec_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
+                       u16 value)
 {
 #ifdef MII_DEBUG
-       printf("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
+       printf("miiphy_write(0x%x) @ 0x%x = 0x%04x\n", reg, addr, value);
 #endif
 
        mii_send(mk_mii_write(addr, reg, value));
 
-#ifdef MII_DEBUG
-       printf("0x%04x\n", value);
-#endif
-
        return 0;
 }