Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2004-2008 Freescale Semiconductor, Inc. |
| 4 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <config.h> |
| 9 | #include <net.h> |
| 10 | #include <netdev.h> |
| 11 | |
| 12 | #ifdef CONFIG_MCF547x_8x |
| 13 | #include <asm/fsl_mcdmafec.h> |
| 14 | #else |
| 15 | #include <asm/fec.h> |
| 16 | #endif |
| 17 | #include <asm/immap.h> |
Simon Glass | caefa32 | 2019-11-14 12:57:31 -0700 | [diff] [blame] | 18 | #include <linux/mii.h> |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Mike Frysinger | a23230c | 2011-10-02 10:01:27 +0000 | [diff] [blame] | 22 | #if defined(CONFIG_CMD_NET) |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 23 | #undef MII_DEBUG |
| 24 | #undef ET_DEBUG |
| 25 | |
| 26 | /*extern int fecpin_setclear(struct eth_device *dev, int setclear);*/ |
| 27 | |
| 28 | #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_CMD_MII) |
| 29 | #include <miiphy.h> |
| 30 | |
| 31 | /* Make MII read/write commands for the FEC. */ |
| 32 | #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \ |
| 33 | (REG & 0x1f) << 18)) |
| 34 | #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \ |
| 35 | (REG & 0x1f) << 18) | (VAL & 0xffff)) |
| 36 | |
| 37 | #ifndef CONFIG_SYS_UNSPEC_PHYID |
| 38 | # define CONFIG_SYS_UNSPEC_PHYID 0 |
| 39 | #endif |
| 40 | #ifndef CONFIG_SYS_UNSPEC_STRID |
| 41 | # define CONFIG_SYS_UNSPEC_STRID 0 |
| 42 | #endif |
| 43 | |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 44 | typedef struct phy_info_struct { |
| 45 | u32 phyid; |
| 46 | char *strid; |
| 47 | } phy_info_t; |
| 48 | |
| 49 | phy_info_t phyinfo[] = { |
| 50 | {0x0022561B, "AMD79C784VC"}, /* AMD 79C784VC */ |
| 51 | {0x00406322, "BCM5222"}, /* Broadcom 5222 */ |
| 52 | {0x02a80150, "Intel82555"}, /* Intel 82555 */ |
| 53 | {0x0016f870, "LSI80225"}, /* LSI 80225 */ |
| 54 | {0x0016f880, "LSI80225/B"}, /* LSI 80225/B */ |
| 55 | {0x78100000, "LXT970"}, /* LXT970 */ |
| 56 | {0x001378e0, "LXT971"}, /* LXT971 and 972 */ |
| 57 | {0x00221619, "KS8721BL"}, /* Micrel KS8721BL/SL */ |
| 58 | {0x00221512, "KSZ8041NL"}, /* Micrel KSZ8041NL */ |
| 59 | {0x20005CE1, "N83640"}, /* National 83640 */ |
| 60 | {0x20005C90, "N83848"}, /* National 83848 */ |
| 61 | {0x20005CA2, "N83849"}, /* National 83849 */ |
| 62 | {0x01814400, "QS6612"}, /* QS6612 */ |
| 63 | #if defined(CONFIG_SYS_UNSPEC_PHYID) && defined(CONFIG_SYS_UNSPEC_STRID) |
| 64 | {CONFIG_SYS_UNSPEC_PHYID, CONFIG_SYS_UNSPEC_STRID}, |
| 65 | #endif |
| 66 | {0, 0} |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | * mii_init -- Initialize the MII for MII command without ethernet |
| 71 | * This function is a subset of eth_init |
| 72 | */ |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 73 | void mii_reset(fec_info_t *info) |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 74 | { |
| 75 | volatile FEC_T *fecp = (FEC_T *) (info->miibase); |
| 76 | int i; |
| 77 | |
| 78 | fecp->ecr = FEC_ECR_RESET; |
| 79 | |
| 80 | for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) { |
| 81 | udelay(1); |
| 82 | } |
| 83 | if (i == FEC_RESET_DELAY) |
| 84 | printf("FEC_RESET_DELAY timeout\n"); |
| 85 | } |
| 86 | |
| 87 | /* send command to phy using mii, wait for result */ |
| 88 | uint mii_send(uint mii_cmd) |
| 89 | { |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 90 | #ifdef CONFIG_DM_ETH |
| 91 | struct udevice *dev; |
| 92 | #else |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 93 | struct eth_device *dev; |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 94 | #endif |
| 95 | fec_info_t *info; |
| 96 | volatile FEC_T *ep; |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 97 | uint mii_reply; |
| 98 | int j = 0; |
| 99 | |
| 100 | /* retrieve from register structure */ |
| 101 | dev = eth_get_dev(); |
| 102 | info = dev->priv; |
| 103 | |
| 104 | ep = (FEC_T *) info->miibase; |
| 105 | |
| 106 | ep->mmfr = mii_cmd; /* command to phy */ |
| 107 | |
| 108 | /* wait for mii complete */ |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 109 | while (!(ep->eir & FEC_EIR_MII) && (j < info->to_loop)) { |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 110 | udelay(1); |
| 111 | j++; |
| 112 | } |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 113 | if (j >= info->to_loop) { |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 114 | printf("MII not complete\n"); |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | mii_reply = ep->mmfr; /* result from phy */ |
| 119 | ep->eir = FEC_EIR_MII; /* clear MII complete */ |
| 120 | #ifdef ET_DEBUG |
| 121 | printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n", |
| 122 | __FILE__, __LINE__, __FUNCTION__, mii_cmd, mii_reply); |
| 123 | #endif |
| 124 | |
| 125 | return (mii_reply & 0xffff); /* data read from phy */ |
| 126 | } |
| 127 | #endif /* CONFIG_SYS_DISCOVER_PHY || (CONFIG_MII) */ |
| 128 | |
| 129 | #if defined(CONFIG_SYS_DISCOVER_PHY) |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 130 | int mii_discover_phy(fec_info_t *info) |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 131 | { |
| 132 | #define MAX_PHY_PASSES 11 |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 133 | int phyaddr, pass; |
| 134 | uint phyno, phytype; |
| 135 | int i, found = 0; |
| 136 | |
| 137 | if (info->phyname_init) |
| 138 | return info->phy_addr; |
| 139 | |
| 140 | phyaddr = -1; /* didn't find a PHY yet */ |
| 141 | for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) { |
| 142 | if (pass > 1) { |
| 143 | /* PHY may need more time to recover from reset. |
| 144 | * The LXT970 needs 50ms typical, no maximum is |
| 145 | * specified, so wait 10ms before try again. |
| 146 | * With 11 passes this gives it 100ms to wake up. |
| 147 | */ |
| 148 | udelay(10000); /* wait 10ms */ |
| 149 | } |
| 150 | |
| 151 | for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) { |
| 152 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 153 | phytype = mii_send(mk_mii_read(phyno, MII_PHYSID1)); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 154 | #ifdef ET_DEBUG |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 155 | printf("PHY type 0x%x pass %d\n", phytype, pass); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 156 | #endif |
Wolfgang Wegner | 41610e5 | 2010-04-06 11:13:02 +0200 | [diff] [blame] | 157 | if (phytype == 0xffff) |
| 158 | continue; |
| 159 | phyaddr = phyno; |
| 160 | phytype <<= 16; |
| 161 | phytype |= |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 162 | mii_send(mk_mii_read(phyno, MII_PHYSID2)); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 163 | |
| 164 | #ifdef ET_DEBUG |
Wolfgang Wegner | 41610e5 | 2010-04-06 11:13:02 +0200 | [diff] [blame] | 165 | printf("PHY @ 0x%x pass %d\n", phyno, pass); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 166 | #endif |
| 167 | |
Axel Lin | 10a72b6 | 2013-07-03 11:24:18 +0800 | [diff] [blame] | 168 | for (i = 0; (i < ARRAY_SIZE(phyinfo)) |
Wolfgang Wegner | 41610e5 | 2010-04-06 11:13:02 +0200 | [diff] [blame] | 169 | && (phyinfo[i].phyid != 0); i++) { |
| 170 | if (phyinfo[i].phyid == phytype) { |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 171 | #ifdef ET_DEBUG |
Wolfgang Wegner | 41610e5 | 2010-04-06 11:13:02 +0200 | [diff] [blame] | 172 | printf("phyid %x - %s\n", |
| 173 | phyinfo[i].phyid, |
| 174 | phyinfo[i].strid); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 175 | #endif |
Wolfgang Wegner | 41610e5 | 2010-04-06 11:13:02 +0200 | [diff] [blame] | 176 | strcpy(info->phy_name, phyinfo[i].strid); |
| 177 | info->phyname_init = 1; |
| 178 | found = 1; |
| 179 | break; |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 180 | } |
Wolfgang Wegner | 41610e5 | 2010-04-06 11:13:02 +0200 | [diff] [blame] | 181 | } |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 182 | |
Wolfgang Wegner | 41610e5 | 2010-04-06 11:13:02 +0200 | [diff] [blame] | 183 | if (!found) { |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 184 | #ifdef ET_DEBUG |
Wolfgang Wegner | 41610e5 | 2010-04-06 11:13:02 +0200 | [diff] [blame] | 185 | printf("0x%08x\n", phytype); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 186 | #endif |
Wolfgang Wegner | 41610e5 | 2010-04-06 11:13:02 +0200 | [diff] [blame] | 187 | strcpy(info->phy_name, "unknown"); |
| 188 | info->phyname_init = 1; |
| 189 | break; |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (phyaddr < 0) |
| 195 | printf("No PHY device found.\n"); |
| 196 | |
| 197 | return phyaddr; |
| 198 | } |
| 199 | #endif /* CONFIG_SYS_DISCOVER_PHY */ |
| 200 | |
| 201 | void mii_init(void) __attribute__((weak,alias("__mii_init"))); |
| 202 | |
| 203 | void __mii_init(void) |
| 204 | { |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 205 | #ifdef CONFIG_DM_ETH |
| 206 | struct udevice *dev; |
| 207 | #else |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 208 | struct eth_device *dev; |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 209 | #endif |
| 210 | fec_info_t *info; |
| 211 | volatile FEC_T *fecp; |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 212 | int miispd = 0, i = 0; |
Richard Retanubun | cb265cd | 2009-01-23 14:42:58 -0500 | [diff] [blame] | 213 | u16 status = 0; |
| 214 | u16 linkgood = 0; |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 215 | |
| 216 | /* retrieve from register structure */ |
| 217 | dev = eth_get_dev(); |
| 218 | info = dev->priv; |
| 219 | |
| 220 | fecp = (FEC_T *) info->miibase; |
| 221 | |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 222 | fecpin_setclear(info, 1); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 223 | |
| 224 | mii_reset(info); |
| 225 | |
| 226 | /* We use strictly polling mode only */ |
| 227 | fecp->eimr = 0; |
| 228 | |
| 229 | /* Clear any pending interrupt */ |
| 230 | fecp->eir = 0xffffffff; |
| 231 | |
| 232 | /* Set MII speed */ |
| 233 | miispd = (gd->bus_clk / 1000000) / 5; |
| 234 | fecp->mscr = miispd << 1; |
| 235 | |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 236 | #ifdef CONFIG_SYS_DISCOVER_PHY |
| 237 | info->phy_addr = mii_discover_phy(info); |
| 238 | #endif |
| 239 | if (info->phy_addr == -1) |
| 240 | return; |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 241 | |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 242 | while (i < info->to_loop) { |
Richard Retanubun | cb265cd | 2009-01-23 14:42:58 -0500 | [diff] [blame] | 243 | status = 0; |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 244 | i++; |
Richard Retanubun | cb265cd | 2009-01-23 14:42:58 -0500 | [diff] [blame] | 245 | /* Read PHY control register */ |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 246 | miiphy_read(dev->name, info->phy_addr, MII_BMCR, &status); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 247 | |
Richard Retanubun | cb265cd | 2009-01-23 14:42:58 -0500 | [diff] [blame] | 248 | /* If phy set to autonegotiate, wait for autonegotiation done, |
| 249 | * if phy is not autonegotiating, just wait for link up. |
| 250 | */ |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 251 | if ((status & BMCR_ANENABLE) == BMCR_ANENABLE) { |
| 252 | linkgood = (BMSR_ANEGCOMPLETE | BMSR_LSTATUS); |
Richard Retanubun | cb265cd | 2009-01-23 14:42:58 -0500 | [diff] [blame] | 253 | } else { |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 254 | linkgood = BMSR_LSTATUS; |
Richard Retanubun | cb265cd | 2009-01-23 14:42:58 -0500 | [diff] [blame] | 255 | } |
| 256 | /* Read PHY status register */ |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 257 | miiphy_read(dev->name, info->phy_addr, MII_BMSR, &status); |
Richard Retanubun | cb265cd | 2009-01-23 14:42:58 -0500 | [diff] [blame] | 258 | if ((status & linkgood) == linkgood) |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 259 | break; |
| 260 | |
Richard Retanubun | 881a1d6 | 2009-05-26 08:29:29 -0400 | [diff] [blame] | 261 | udelay(1); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 262 | } |
Angelo Durgehello | 9251edc | 2019-11-15 23:54:20 +0100 | [diff] [blame] | 263 | if (i >= info->to_loop) |
Richard Retanubun | cb265cd | 2009-01-23 14:42:58 -0500 | [diff] [blame] | 264 | printf("Link UP timeout\n"); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 265 | |
Richard Retanubun | cb265cd | 2009-01-23 14:42:58 -0500 | [diff] [blame] | 266 | /* adapt to the duplex and speed settings of the phy */ |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 267 | info->dup_spd = miiphy_duplex(dev->name, info->phy_addr) << 16; |
| 268 | info->dup_spd |= miiphy_speed(dev->name, info->phy_addr); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Read and write a MII PHY register, routines used by MII Utilities |
| 273 | * |
| 274 | * FIXME: These routines are expected to return 0 on success, but mii_send |
| 275 | * does _not_ return an error code. Maybe 0xFFFF means error, i.e. |
| 276 | * no PHY connected... |
| 277 | * For now always return 0. |
| 278 | * FIXME: These routines only work after calling eth_init() at least once! |
| 279 | * Otherwise they hang in mii_send() !!! Sorry! |
| 280 | */ |
| 281 | |
Joe Hershberger | 0c33319 | 2016-08-08 11:28:39 -0500 | [diff] [blame] | 282 | int mcffec_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg) |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 283 | { |
| 284 | short rdreg; /* register working value */ |
| 285 | |
| 286 | #ifdef MII_DEBUG |
| 287 | printf("miiphy_read(0x%x) @ 0x%x = ", reg, addr); |
| 288 | #endif |
| 289 | rdreg = mii_send(mk_mii_read(addr, reg)); |
| 290 | |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 291 | #ifdef MII_DEBUG |
Joe Hershberger | 0c33319 | 2016-08-08 11:28:39 -0500 | [diff] [blame] | 292 | printf("0x%04x\n", rdreg); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 293 | #endif |
| 294 | |
Joe Hershberger | 0c33319 | 2016-08-08 11:28:39 -0500 | [diff] [blame] | 295 | return rdreg; |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Joe Hershberger | 0c33319 | 2016-08-08 11:28:39 -0500 | [diff] [blame] | 298 | int mcffec_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg, |
| 299 | u16 value) |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 300 | { |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 301 | #ifdef MII_DEBUG |
Joe Hershberger | 0c33319 | 2016-08-08 11:28:39 -0500 | [diff] [blame] | 302 | printf("miiphy_write(0x%x) @ 0x%x = 0x%04x\n", reg, addr, value); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 303 | #endif |
| 304 | |
Marek Vasut | 6aaba55 | 2012-10-03 13:28:47 +0000 | [diff] [blame] | 305 | mii_send(mk_mii_write(addr, reg, value)); |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 306 | |
TsiChung Liew | e602f22 | 2008-10-23 16:27:24 +0000 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
Mike Frysinger | a23230c | 2011-10-02 10:01:27 +0000 | [diff] [blame] | 310 | #endif /* CONFIG_CMD_NET */ |