blob: ec81320a86d177bab164893a3ff5020eee8b5444 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChung Liew54bdcc92008-10-23 16:27:24 +00002/*
3 * Copyright (C) 2004-2008 Freescale Semiconductor, Inc.
4 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
TsiChung Liew54bdcc92008-10-23 16:27:24 +00005 */
6
7#include <common.h>
8#include <config.h>
9#include <net.h>
10#include <netdev.h>
Simon Glassc05ed002020-05-10 11:40:11 -060011#include <linux/delay.h>
TsiChung Liew54bdcc92008-10-23 16:27:24 +000012
13#ifdef CONFIG_MCF547x_8x
14#include <asm/fsl_mcdmafec.h>
15#else
16#include <asm/fec.h>
17#endif
18#include <asm/immap.h>
Simon Glass68a6aa82019-11-14 12:57:31 -070019#include <linux/mii.h>
TsiChung Liew54bdcc92008-10-23 16:27:24 +000020
21DECLARE_GLOBAL_DATA_PTR;
22
Mike Frysingere2a53452011-10-02 10:01:27 +000023#if defined(CONFIG_CMD_NET)
TsiChung Liew54bdcc92008-10-23 16:27:24 +000024#undef MII_DEBUG
25#undef ET_DEBUG
26
27/*extern int fecpin_setclear(struct eth_device *dev, int setclear);*/
28
29#if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_CMD_MII)
30#include <miiphy.h>
31
32/* Make MII read/write commands for the FEC. */
33#define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
34 (REG & 0x1f) << 18))
35#define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
36 (REG & 0x1f) << 18) | (VAL & 0xffff))
37
38#ifndef CONFIG_SYS_UNSPEC_PHYID
39# define CONFIG_SYS_UNSPEC_PHYID 0
40#endif
41#ifndef CONFIG_SYS_UNSPEC_STRID
42# define CONFIG_SYS_UNSPEC_STRID 0
43#endif
44
TsiChung Liew54bdcc92008-10-23 16:27:24 +000045typedef struct phy_info_struct {
46 u32 phyid;
47 char *strid;
48} phy_info_t;
49
50phy_info_t phyinfo[] = {
51 {0x0022561B, "AMD79C784VC"}, /* AMD 79C784VC */
52 {0x00406322, "BCM5222"}, /* Broadcom 5222 */
53 {0x02a80150, "Intel82555"}, /* Intel 82555 */
54 {0x0016f870, "LSI80225"}, /* LSI 80225 */
55 {0x0016f880, "LSI80225/B"}, /* LSI 80225/B */
56 {0x78100000, "LXT970"}, /* LXT970 */
57 {0x001378e0, "LXT971"}, /* LXT971 and 972 */
58 {0x00221619, "KS8721BL"}, /* Micrel KS8721BL/SL */
59 {0x00221512, "KSZ8041NL"}, /* Micrel KSZ8041NL */
60 {0x20005CE1, "N83640"}, /* National 83640 */
61 {0x20005C90, "N83848"}, /* National 83848 */
62 {0x20005CA2, "N83849"}, /* National 83849 */
63 {0x01814400, "QS6612"}, /* QS6612 */
64#if defined(CONFIG_SYS_UNSPEC_PHYID) && defined(CONFIG_SYS_UNSPEC_STRID)
65 {CONFIG_SYS_UNSPEC_PHYID, CONFIG_SYS_UNSPEC_STRID},
66#endif
67 {0, 0}
68};
69
70/*
71 * mii_init -- Initialize the MII for MII command without ethernet
72 * This function is a subset of eth_init
73 */
Angelo Durgehello48f885a2019-11-15 23:54:20 +010074void mii_reset(fec_info_t *info)
TsiChung Liew54bdcc92008-10-23 16:27:24 +000075{
76 volatile FEC_T *fecp = (FEC_T *) (info->miibase);
77 int i;
78
79 fecp->ecr = FEC_ECR_RESET;
80
81 for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
82 udelay(1);
83 }
84 if (i == FEC_RESET_DELAY)
85 printf("FEC_RESET_DELAY timeout\n");
86}
87
88/* send command to phy using mii, wait for result */
89uint mii_send(uint mii_cmd)
90{
Angelo Durgehello48f885a2019-11-15 23:54:20 +010091#ifdef CONFIG_DM_ETH
92 struct udevice *dev;
93#else
TsiChung Liew54bdcc92008-10-23 16:27:24 +000094 struct eth_device *dev;
Angelo Durgehello48f885a2019-11-15 23:54:20 +010095#endif
96 fec_info_t *info;
97 volatile FEC_T *ep;
TsiChung Liew54bdcc92008-10-23 16:27:24 +000098 uint mii_reply;
99 int j = 0;
100
101 /* retrieve from register structure */
102 dev = eth_get_dev();
Simon Glass0fd3d912020-12-22 19:30:28 -0700103#ifdef CONFIG_DM_ETH
104 info = dev_get_priv(dev);
105#else
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000106 info = dev->priv;
Simon Glass0fd3d912020-12-22 19:30:28 -0700107#endif
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000108
109 ep = (FEC_T *) info->miibase;
110
111 ep->mmfr = mii_cmd; /* command to phy */
112
113 /* wait for mii complete */
Angelo Durgehello48f885a2019-11-15 23:54:20 +0100114 while (!(ep->eir & FEC_EIR_MII) && (j < info->to_loop)) {
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000115 udelay(1);
116 j++;
117 }
Angelo Durgehello48f885a2019-11-15 23:54:20 +0100118 if (j >= info->to_loop) {
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000119 printf("MII not complete\n");
120 return -1;
121 }
122
123 mii_reply = ep->mmfr; /* result from phy */
124 ep->eir = FEC_EIR_MII; /* clear MII complete */
125#ifdef ET_DEBUG
126 printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
127 __FILE__, __LINE__, __FUNCTION__, mii_cmd, mii_reply);
128#endif
129
130 return (mii_reply & 0xffff); /* data read from phy */
131}
132#endif /* CONFIG_SYS_DISCOVER_PHY || (CONFIG_MII) */
133
134#if defined(CONFIG_SYS_DISCOVER_PHY)
Angelo Durgehello48f885a2019-11-15 23:54:20 +0100135int mii_discover_phy(fec_info_t *info)
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000136{
137#define MAX_PHY_PASSES 11
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000138 int phyaddr, pass;
139 uint phyno, phytype;
140 int i, found = 0;
141
142 if (info->phyname_init)
143 return info->phy_addr;
144
145 phyaddr = -1; /* didn't find a PHY yet */
146 for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
147 if (pass > 1) {
148 /* PHY may need more time to recover from reset.
149 * The LXT970 needs 50ms typical, no maximum is
150 * specified, so wait 10ms before try again.
151 * With 11 passes this gives it 100ms to wake up.
152 */
153 udelay(10000); /* wait 10ms */
154 }
155
156 for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
157
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500158 phytype = mii_send(mk_mii_read(phyno, MII_PHYSID1));
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000159#ifdef ET_DEBUG
Angelo Durgehello48f885a2019-11-15 23:54:20 +0100160 printf("PHY type 0x%x pass %d\n", phytype, pass);
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000161#endif
Wolfgang Wegner33f684d2010-04-06 11:13:02 +0200162 if (phytype == 0xffff)
163 continue;
164 phyaddr = phyno;
165 phytype <<= 16;
166 phytype |=
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500167 mii_send(mk_mii_read(phyno, MII_PHYSID2));
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000168
169#ifdef ET_DEBUG
Wolfgang Wegner33f684d2010-04-06 11:13:02 +0200170 printf("PHY @ 0x%x pass %d\n", phyno, pass);
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000171#endif
172
Axel Lina62cd292013-07-03 11:24:18 +0800173 for (i = 0; (i < ARRAY_SIZE(phyinfo))
Wolfgang Wegner33f684d2010-04-06 11:13:02 +0200174 && (phyinfo[i].phyid != 0); i++) {
175 if (phyinfo[i].phyid == phytype) {
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000176#ifdef ET_DEBUG
Wolfgang Wegner33f684d2010-04-06 11:13:02 +0200177 printf("phyid %x - %s\n",
178 phyinfo[i].phyid,
179 phyinfo[i].strid);
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000180#endif
Wolfgang Wegner33f684d2010-04-06 11:13:02 +0200181 strcpy(info->phy_name, phyinfo[i].strid);
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000182 info->phyname_init = 1;
Wolfgang Wegner33f684d2010-04-06 11:13:02 +0200183 found = 1;
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000184 break;
185 }
186 }
Wolfgang Wegner33f684d2010-04-06 11:13:02 +0200187
188 if (!found) {
189#ifdef ET_DEBUG
190 printf("0x%08x\n", phytype);
191#endif
192 strcpy(info->phy_name, "unknown");
193 info->phyname_init = 1;
194 break;
195 }
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000196 }
197 }
198
199 if (phyaddr < 0)
200 printf("No PHY device found.\n");
201
202 return phyaddr;
203}
204#endif /* CONFIG_SYS_DISCOVER_PHY */
205
206void mii_init(void) __attribute__((weak,alias("__mii_init")));
207
208void __mii_init(void)
209{
Angelo Durgehello48f885a2019-11-15 23:54:20 +0100210#ifdef CONFIG_DM_ETH
211 struct udevice *dev;
212#else
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000213 struct eth_device *dev;
Angelo Durgehello48f885a2019-11-15 23:54:20 +0100214#endif
215 fec_info_t *info;
216 volatile FEC_T *fecp;
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000217 int miispd = 0, i = 0;
Richard Retanubunc4ff77f2009-01-23 14:42:58 -0500218 u16 status = 0;
219 u16 linkgood = 0;
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000220
221 /* retrieve from register structure */
222 dev = eth_get_dev();
Simon Glass0fd3d912020-12-22 19:30:28 -0700223#ifdef CONFIG_DM_ETH
224 info = dev_get_priv(dev);
225#else
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000226 info = dev->priv;
Simon Glass0fd3d912020-12-22 19:30:28 -0700227#endif
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000228
229 fecp = (FEC_T *) info->miibase;
230
Angelo Durgehello48f885a2019-11-15 23:54:20 +0100231 fecpin_setclear(info, 1);
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000232
233 mii_reset(info);
234
235 /* We use strictly polling mode only */
236 fecp->eimr = 0;
237
238 /* Clear any pending interrupt */
239 fecp->eir = 0xffffffff;
240
241 /* Set MII speed */
242 miispd = (gd->bus_clk / 1000000) / 5;
243 fecp->mscr = miispd << 1;
244
Angelo Durgehello48f885a2019-11-15 23:54:20 +0100245#ifdef CONFIG_SYS_DISCOVER_PHY
246 info->phy_addr = mii_discover_phy(info);
247#endif
248 if (info->phy_addr == -1)
249 return;
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000250
Angelo Durgehello48f885a2019-11-15 23:54:20 +0100251 while (i < info->to_loop) {
Richard Retanubunc4ff77f2009-01-23 14:42:58 -0500252 status = 0;
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000253 i++;
Richard Retanubunc4ff77f2009-01-23 14:42:58 -0500254 /* Read PHY control register */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500255 miiphy_read(dev->name, info->phy_addr, MII_BMCR, &status);
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000256
Richard Retanubunc4ff77f2009-01-23 14:42:58 -0500257 /* If phy set to autonegotiate, wait for autonegotiation done,
258 * if phy is not autonegotiating, just wait for link up.
259 */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500260 if ((status & BMCR_ANENABLE) == BMCR_ANENABLE) {
261 linkgood = (BMSR_ANEGCOMPLETE | BMSR_LSTATUS);
Richard Retanubunc4ff77f2009-01-23 14:42:58 -0500262 } else {
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500263 linkgood = BMSR_LSTATUS;
Richard Retanubunc4ff77f2009-01-23 14:42:58 -0500264 }
265 /* Read PHY status register */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500266 miiphy_read(dev->name, info->phy_addr, MII_BMSR, &status);
Richard Retanubunc4ff77f2009-01-23 14:42:58 -0500267 if ((status & linkgood) == linkgood)
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000268 break;
269
Richard Retanubun44578be2009-05-26 08:29:29 -0400270 udelay(1);
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000271 }
Angelo Durgehello48f885a2019-11-15 23:54:20 +0100272 if (i >= info->to_loop)
Richard Retanubunc4ff77f2009-01-23 14:42:58 -0500273 printf("Link UP timeout\n");
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000274
Richard Retanubunc4ff77f2009-01-23 14:42:58 -0500275 /* adapt to the duplex and speed settings of the phy */
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000276 info->dup_spd = miiphy_duplex(dev->name, info->phy_addr) << 16;
277 info->dup_spd |= miiphy_speed(dev->name, info->phy_addr);
278}
279
280/*
281 * Read and write a MII PHY register, routines used by MII Utilities
282 *
283 * FIXME: These routines are expected to return 0 on success, but mii_send
284 * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
285 * no PHY connected...
286 * For now always return 0.
287 * FIXME: These routines only work after calling eth_init() at least once!
288 * Otherwise they hang in mii_send() !!! Sorry!
289 */
290
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500291int mcffec_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000292{
293 short rdreg; /* register working value */
294
295#ifdef MII_DEBUG
296 printf("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
297#endif
298 rdreg = mii_send(mk_mii_read(addr, reg));
299
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000300#ifdef MII_DEBUG
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500301 printf("0x%04x\n", rdreg);
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000302#endif
303
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500304 return rdreg;
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000305}
306
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500307int mcffec_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
308 u16 value)
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000309{
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000310#ifdef MII_DEBUG
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500311 printf("miiphy_write(0x%x) @ 0x%x = 0x%04x\n", reg, addr, value);
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000312#endif
313
Marek Vasut2b758ca2012-10-03 13:28:47 +0000314 mii_send(mk_mii_write(addr, reg, value));
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000315
TsiChung Liew54bdcc92008-10-23 16:27:24 +0000316 return 0;
317}
318
Mike Frysingere2a53452011-10-02 10:01:27 +0000319#endif /* CONFIG_CMD_NET */