blob: 48dd558405c5d5ab5883b7a162b79f7f0c6a6468 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChung Liewe602f222008-10-23 16:27:24 +00002/*
3 * Copyright (C) 2004-2008 Freescale Semiconductor, Inc.
4 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
TsiChung Liewe602f222008-10-23 16:27:24 +00005 */
6
7#include <common.h>
8#include <config.h>
9#include <net.h>
10#include <netdev.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
TsiChung Liewe602f222008-10-23 16:27:24 +000013
TsiChung Liewe602f222008-10-23 16:27:24 +000014#include <asm/fec.h>
TsiChung Liewe602f222008-10-23 16:27:24 +000015#include <asm/immap.h>
Simon Glasscaefa322019-11-14 12:57:31 -070016#include <linux/mii.h>
TsiChung Liewe602f222008-10-23 16:27:24 +000017
18DECLARE_GLOBAL_DATA_PTR;
19
Mike Frysingera23230c2011-10-02 10:01:27 +000020#if defined(CONFIG_CMD_NET)
TsiChung Liewe602f222008-10-23 16:27:24 +000021#undef MII_DEBUG
22#undef ET_DEBUG
23
24/*extern int fecpin_setclear(struct eth_device *dev, int setclear);*/
25
26#if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_CMD_MII)
27#include <miiphy.h>
28
29/* Make MII read/write commands for the FEC. */
30#define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
31 (REG & 0x1f) << 18))
32#define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
33 (REG & 0x1f) << 18) | (VAL & 0xffff))
34
35#ifndef CONFIG_SYS_UNSPEC_PHYID
36# define CONFIG_SYS_UNSPEC_PHYID 0
37#endif
38#ifndef CONFIG_SYS_UNSPEC_STRID
39# define CONFIG_SYS_UNSPEC_STRID 0
40#endif
41
TsiChung Liewe602f222008-10-23 16:27:24 +000042typedef struct phy_info_struct {
43 u32 phyid;
44 char *strid;
45} phy_info_t;
46
47phy_info_t phyinfo[] = {
48 {0x0022561B, "AMD79C784VC"}, /* AMD 79C784VC */
49 {0x00406322, "BCM5222"}, /* Broadcom 5222 */
50 {0x02a80150, "Intel82555"}, /* Intel 82555 */
51 {0x0016f870, "LSI80225"}, /* LSI 80225 */
52 {0x0016f880, "LSI80225/B"}, /* LSI 80225/B */
53 {0x78100000, "LXT970"}, /* LXT970 */
54 {0x001378e0, "LXT971"}, /* LXT971 and 972 */
55 {0x00221619, "KS8721BL"}, /* Micrel KS8721BL/SL */
56 {0x00221512, "KSZ8041NL"}, /* Micrel KSZ8041NL */
57 {0x20005CE1, "N83640"}, /* National 83640 */
58 {0x20005C90, "N83848"}, /* National 83848 */
59 {0x20005CA2, "N83849"}, /* National 83849 */
60 {0x01814400, "QS6612"}, /* QS6612 */
61#if defined(CONFIG_SYS_UNSPEC_PHYID) && defined(CONFIG_SYS_UNSPEC_STRID)
62 {CONFIG_SYS_UNSPEC_PHYID, CONFIG_SYS_UNSPEC_STRID},
63#endif
64 {0, 0}
65};
66
67/*
68 * mii_init -- Initialize the MII for MII command without ethernet
69 * This function is a subset of eth_init
70 */
Angelo Durgehello9251edc2019-11-15 23:54:20 +010071void mii_reset(fec_info_t *info)
TsiChung Liewe602f222008-10-23 16:27:24 +000072{
73 volatile FEC_T *fecp = (FEC_T *) (info->miibase);
74 int i;
75
76 fecp->ecr = FEC_ECR_RESET;
77
78 for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
79 udelay(1);
80 }
81 if (i == FEC_RESET_DELAY)
82 printf("FEC_RESET_DELAY timeout\n");
83}
84
85/* send command to phy using mii, wait for result */
86uint mii_send(uint mii_cmd)
87{
Angelo Durgehello9251edc2019-11-15 23:54:20 +010088 struct udevice *dev;
Angelo Durgehello9251edc2019-11-15 23:54:20 +010089 fec_info_t *info;
90 volatile FEC_T *ep;
TsiChung Liewe602f222008-10-23 16:27:24 +000091 uint mii_reply;
92 int j = 0;
93
94 /* retrieve from register structure */
95 dev = eth_get_dev();
Simon Glass95588622020-12-22 19:30:28 -070096 info = dev_get_priv(dev);
TsiChung Liewe602f222008-10-23 16:27:24 +000097
98 ep = (FEC_T *) info->miibase;
99
100 ep->mmfr = mii_cmd; /* command to phy */
101
102 /* wait for mii complete */
Angelo Durgehello9251edc2019-11-15 23:54:20 +0100103 while (!(ep->eir & FEC_EIR_MII) && (j < info->to_loop)) {
TsiChung Liewe602f222008-10-23 16:27:24 +0000104 udelay(1);
105 j++;
106 }
Angelo Durgehello9251edc2019-11-15 23:54:20 +0100107 if (j >= info->to_loop) {
TsiChung Liewe602f222008-10-23 16:27:24 +0000108 printf("MII not complete\n");
109 return -1;
110 }
111
112 mii_reply = ep->mmfr; /* result from phy */
113 ep->eir = FEC_EIR_MII; /* clear MII complete */
114#ifdef ET_DEBUG
115 printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
116 __FILE__, __LINE__, __FUNCTION__, mii_cmd, mii_reply);
117#endif
118
119 return (mii_reply & 0xffff); /* data read from phy */
120}
121#endif /* CONFIG_SYS_DISCOVER_PHY || (CONFIG_MII) */
122
123#if defined(CONFIG_SYS_DISCOVER_PHY)
Angelo Durgehello9251edc2019-11-15 23:54:20 +0100124int mii_discover_phy(fec_info_t *info)
TsiChung Liewe602f222008-10-23 16:27:24 +0000125{
126#define MAX_PHY_PASSES 11
TsiChung Liewe602f222008-10-23 16:27:24 +0000127 int phyaddr, pass;
128 uint phyno, phytype;
129 int i, found = 0;
130
131 if (info->phyname_init)
132 return info->phy_addr;
133
134 phyaddr = -1; /* didn't find a PHY yet */
135 for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
136 if (pass > 1) {
137 /* PHY may need more time to recover from reset.
138 * The LXT970 needs 50ms typical, no maximum is
139 * specified, so wait 10ms before try again.
140 * With 11 passes this gives it 100ms to wake up.
141 */
142 udelay(10000); /* wait 10ms */
143 }
144
145 for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
146
Mike Frysingerd63ee712010-12-23 15:40:12 -0500147 phytype = mii_send(mk_mii_read(phyno, MII_PHYSID1));
TsiChung Liewe602f222008-10-23 16:27:24 +0000148#ifdef ET_DEBUG
Angelo Durgehello9251edc2019-11-15 23:54:20 +0100149 printf("PHY type 0x%x pass %d\n", phytype, pass);
TsiChung Liewe602f222008-10-23 16:27:24 +0000150#endif
Wolfgang Wegner41610e52010-04-06 11:13:02 +0200151 if (phytype == 0xffff)
152 continue;
153 phyaddr = phyno;
154 phytype <<= 16;
155 phytype |=
Mike Frysingerd63ee712010-12-23 15:40:12 -0500156 mii_send(mk_mii_read(phyno, MII_PHYSID2));
TsiChung Liewe602f222008-10-23 16:27:24 +0000157
158#ifdef ET_DEBUG
Wolfgang Wegner41610e52010-04-06 11:13:02 +0200159 printf("PHY @ 0x%x pass %d\n", phyno, pass);
TsiChung Liewe602f222008-10-23 16:27:24 +0000160#endif
161
Axel Lin10a72b62013-07-03 11:24:18 +0800162 for (i = 0; (i < ARRAY_SIZE(phyinfo))
Wolfgang Wegner41610e52010-04-06 11:13:02 +0200163 && (phyinfo[i].phyid != 0); i++) {
164 if (phyinfo[i].phyid == phytype) {
TsiChung Liewe602f222008-10-23 16:27:24 +0000165#ifdef ET_DEBUG
Wolfgang Wegner41610e52010-04-06 11:13:02 +0200166 printf("phyid %x - %s\n",
167 phyinfo[i].phyid,
168 phyinfo[i].strid);
TsiChung Liewe602f222008-10-23 16:27:24 +0000169#endif
Wolfgang Wegner41610e52010-04-06 11:13:02 +0200170 strcpy(info->phy_name, phyinfo[i].strid);
171 info->phyname_init = 1;
172 found = 1;
173 break;
TsiChung Liewe602f222008-10-23 16:27:24 +0000174 }
Wolfgang Wegner41610e52010-04-06 11:13:02 +0200175 }
TsiChung Liewe602f222008-10-23 16:27:24 +0000176
Wolfgang Wegner41610e52010-04-06 11:13:02 +0200177 if (!found) {
TsiChung Liewe602f222008-10-23 16:27:24 +0000178#ifdef ET_DEBUG
Wolfgang Wegner41610e52010-04-06 11:13:02 +0200179 printf("0x%08x\n", phytype);
TsiChung Liewe602f222008-10-23 16:27:24 +0000180#endif
Wolfgang Wegner41610e52010-04-06 11:13:02 +0200181 strcpy(info->phy_name, "unknown");
182 info->phyname_init = 1;
183 break;
TsiChung Liewe602f222008-10-23 16:27:24 +0000184 }
185 }
186 }
187
188 if (phyaddr < 0)
189 printf("No PHY device found.\n");
190
191 return phyaddr;
192}
193#endif /* CONFIG_SYS_DISCOVER_PHY */
194
Tom Rini1a0c7dc2022-03-18 08:38:27 -0400195__weak void mii_init(void)
TsiChung Liewe602f222008-10-23 16:27:24 +0000196{
Angelo Durgehello9251edc2019-11-15 23:54:20 +0100197 struct udevice *dev;
Angelo Durgehello9251edc2019-11-15 23:54:20 +0100198 fec_info_t *info;
199 volatile FEC_T *fecp;
TsiChung Liewe602f222008-10-23 16:27:24 +0000200 int miispd = 0, i = 0;
Richard Retanubuncb265cd2009-01-23 14:42:58 -0500201 u16 status = 0;
202 u16 linkgood = 0;
TsiChung Liewe602f222008-10-23 16:27:24 +0000203
204 /* retrieve from register structure */
205 dev = eth_get_dev();
Simon Glass95588622020-12-22 19:30:28 -0700206 info = dev_get_priv(dev);
TsiChung Liewe602f222008-10-23 16:27:24 +0000207
208 fecp = (FEC_T *) info->miibase;
209
Angelo Durgehello9251edc2019-11-15 23:54:20 +0100210 fecpin_setclear(info, 1);
TsiChung Liewe602f222008-10-23 16:27:24 +0000211
212 mii_reset(info);
213
214 /* We use strictly polling mode only */
215 fecp->eimr = 0;
216
217 /* Clear any pending interrupt */
218 fecp->eir = 0xffffffff;
219
220 /* Set MII speed */
221 miispd = (gd->bus_clk / 1000000) / 5;
222 fecp->mscr = miispd << 1;
223
Angelo Durgehello9251edc2019-11-15 23:54:20 +0100224#ifdef CONFIG_SYS_DISCOVER_PHY
225 info->phy_addr = mii_discover_phy(info);
226#endif
227 if (info->phy_addr == -1)
228 return;
TsiChung Liewe602f222008-10-23 16:27:24 +0000229
Angelo Durgehello9251edc2019-11-15 23:54:20 +0100230 while (i < info->to_loop) {
Richard Retanubuncb265cd2009-01-23 14:42:58 -0500231 status = 0;
TsiChung Liewe602f222008-10-23 16:27:24 +0000232 i++;
Richard Retanubuncb265cd2009-01-23 14:42:58 -0500233 /* Read PHY control register */
Mike Frysingerd63ee712010-12-23 15:40:12 -0500234 miiphy_read(dev->name, info->phy_addr, MII_BMCR, &status);
TsiChung Liewe602f222008-10-23 16:27:24 +0000235
Richard Retanubuncb265cd2009-01-23 14:42:58 -0500236 /* If phy set to autonegotiate, wait for autonegotiation done,
237 * if phy is not autonegotiating, just wait for link up.
238 */
Mike Frysingerd63ee712010-12-23 15:40:12 -0500239 if ((status & BMCR_ANENABLE) == BMCR_ANENABLE) {
240 linkgood = (BMSR_ANEGCOMPLETE | BMSR_LSTATUS);
Richard Retanubuncb265cd2009-01-23 14:42:58 -0500241 } else {
Mike Frysingerd63ee712010-12-23 15:40:12 -0500242 linkgood = BMSR_LSTATUS;
Richard Retanubuncb265cd2009-01-23 14:42:58 -0500243 }
244 /* Read PHY status register */
Mike Frysingerd63ee712010-12-23 15:40:12 -0500245 miiphy_read(dev->name, info->phy_addr, MII_BMSR, &status);
Richard Retanubuncb265cd2009-01-23 14:42:58 -0500246 if ((status & linkgood) == linkgood)
TsiChung Liewe602f222008-10-23 16:27:24 +0000247 break;
248
Richard Retanubun881a1d62009-05-26 08:29:29 -0400249 udelay(1);
TsiChung Liewe602f222008-10-23 16:27:24 +0000250 }
Angelo Durgehello9251edc2019-11-15 23:54:20 +0100251 if (i >= info->to_loop)
Richard Retanubuncb265cd2009-01-23 14:42:58 -0500252 printf("Link UP timeout\n");
TsiChung Liewe602f222008-10-23 16:27:24 +0000253
Richard Retanubuncb265cd2009-01-23 14:42:58 -0500254 /* adapt to the duplex and speed settings of the phy */
TsiChung Liewe602f222008-10-23 16:27:24 +0000255 info->dup_spd = miiphy_duplex(dev->name, info->phy_addr) << 16;
256 info->dup_spd |= miiphy_speed(dev->name, info->phy_addr);
257}
258
259/*
260 * Read and write a MII PHY register, routines used by MII Utilities
261 *
262 * FIXME: These routines are expected to return 0 on success, but mii_send
263 * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
264 * no PHY connected...
265 * For now always return 0.
266 * FIXME: These routines only work after calling eth_init() at least once!
267 * Otherwise they hang in mii_send() !!! Sorry!
268 */
269
Joe Hershberger0c333192016-08-08 11:28:39 -0500270int mcffec_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
TsiChung Liewe602f222008-10-23 16:27:24 +0000271{
272 short rdreg; /* register working value */
273
274#ifdef MII_DEBUG
275 printf("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
276#endif
277 rdreg = mii_send(mk_mii_read(addr, reg));
278
TsiChung Liewe602f222008-10-23 16:27:24 +0000279#ifdef MII_DEBUG
Joe Hershberger0c333192016-08-08 11:28:39 -0500280 printf("0x%04x\n", rdreg);
TsiChung Liewe602f222008-10-23 16:27:24 +0000281#endif
282
Joe Hershberger0c333192016-08-08 11:28:39 -0500283 return rdreg;
TsiChung Liewe602f222008-10-23 16:27:24 +0000284}
285
Joe Hershberger0c333192016-08-08 11:28:39 -0500286int mcffec_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
287 u16 value)
TsiChung Liewe602f222008-10-23 16:27:24 +0000288{
TsiChung Liewe602f222008-10-23 16:27:24 +0000289#ifdef MII_DEBUG
Joe Hershberger0c333192016-08-08 11:28:39 -0500290 printf("miiphy_write(0x%x) @ 0x%x = 0x%04x\n", reg, addr, value);
TsiChung Liewe602f222008-10-23 16:27:24 +0000291#endif
292
Marek Vasut6aaba552012-10-03 13:28:47 +0000293 mii_send(mk_mii_write(addr, reg, value));
TsiChung Liewe602f222008-10-23 16:27:24 +0000294
TsiChung Liewe602f222008-10-23 16:27:24 +0000295 return 0;
296}
297
Mike Frysingera23230c2011-10-02 10:01:27 +0000298#endif /* CONFIG_CMD_NET */