common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / net / mcfmii.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2004-2008 Freescale Semiconductor, Inc.
4  * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
5  */
6
7 #include <common.h>
8 #include <config.h>
9 #include <net.h>
10 #include <netdev.h>
11 #include <linux/delay.h>
12
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>
19 #include <linux/mii.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 #if defined(CONFIG_CMD_NET)
24 #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
45 typedef struct phy_info_struct {
46         u32 phyid;
47         char *strid;
48 } phy_info_t;
49
50 phy_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  */
74 void mii_reset(fec_info_t *info)
75 {
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 */
89 uint mii_send(uint mii_cmd)
90 {
91 #ifdef CONFIG_DM_ETH
92         struct udevice *dev;
93 #else
94         struct eth_device *dev;
95 #endif
96         fec_info_t *info;
97         volatile FEC_T *ep;
98         uint mii_reply;
99         int j = 0;
100
101         /* retrieve from register structure */
102         dev = eth_get_dev();
103         info = dev->priv;
104
105         ep = (FEC_T *) info->miibase;
106
107         ep->mmfr = mii_cmd;     /* command to phy */
108
109         /* wait for mii complete */
110         while (!(ep->eir & FEC_EIR_MII) && (j < info->to_loop)) {
111                 udelay(1);
112                 j++;
113         }
114         if (j >= info->to_loop) {
115                 printf("MII not complete\n");
116                 return -1;
117         }
118
119         mii_reply = ep->mmfr;   /* result from phy */
120         ep->eir = FEC_EIR_MII;  /* clear MII complete */
121 #ifdef ET_DEBUG
122         printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
123                __FILE__, __LINE__, __FUNCTION__, mii_cmd, mii_reply);
124 #endif
125
126         return (mii_reply & 0xffff);    /* data read from phy */
127 }
128 #endif                          /* CONFIG_SYS_DISCOVER_PHY || (CONFIG_MII) */
129
130 #if defined(CONFIG_SYS_DISCOVER_PHY)
131 int mii_discover_phy(fec_info_t *info)
132 {
133 #define MAX_PHY_PASSES 11
134         int phyaddr, pass;
135         uint phyno, phytype;
136         int i, found = 0;
137
138         if (info->phyname_init)
139                 return info->phy_addr;
140
141         phyaddr = -1;           /* didn't find a PHY yet */
142         for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
143                 if (pass > 1) {
144                         /* PHY may need more time to recover from reset.
145                          * The LXT970 needs 50ms typical, no maximum is
146                          * specified, so wait 10ms before try again.
147                          * With 11 passes this gives it 100ms to wake up.
148                          */
149                         udelay(10000);  /* wait 10ms */
150                 }
151
152                 for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
153
154                         phytype = mii_send(mk_mii_read(phyno, MII_PHYSID1));
155 #ifdef ET_DEBUG
156                         printf("PHY type 0x%x pass %d\n", phytype, pass);
157 #endif
158                         if (phytype == 0xffff)
159                                 continue;
160                         phyaddr = phyno;
161                         phytype <<= 16;
162                         phytype |=
163                             mii_send(mk_mii_read(phyno, MII_PHYSID2));
164
165 #ifdef ET_DEBUG
166                         printf("PHY @ 0x%x pass %d\n", phyno, pass);
167 #endif
168
169                         for (i = 0; (i < ARRAY_SIZE(phyinfo))
170                                 && (phyinfo[i].phyid != 0); i++) {
171                                 if (phyinfo[i].phyid == phytype) {
172 #ifdef ET_DEBUG
173                                         printf("phyid %x - %s\n",
174                                                phyinfo[i].phyid,
175                                                phyinfo[i].strid);
176 #endif
177                                         strcpy(info->phy_name, phyinfo[i].strid);
178                                         info->phyname_init = 1;
179                                         found = 1;
180                                         break;
181                                 }
182                         }
183
184                         if (!found) {
185 #ifdef ET_DEBUG
186                                 printf("0x%08x\n", phytype);
187 #endif
188                                 strcpy(info->phy_name, "unknown");
189                                 info->phyname_init = 1;
190                                 break;
191                         }
192                 }
193         }
194
195         if (phyaddr < 0)
196                 printf("No PHY device found.\n");
197
198         return phyaddr;
199 }
200 #endif                          /* CONFIG_SYS_DISCOVER_PHY */
201
202 void mii_init(void) __attribute__((weak,alias("__mii_init")));
203
204 void __mii_init(void)
205 {
206 #ifdef CONFIG_DM_ETH
207         struct udevice *dev;
208 #else
209         struct eth_device *dev;
210 #endif
211         fec_info_t *info;
212         volatile FEC_T *fecp;
213         int miispd = 0, i = 0;
214         u16 status = 0;
215         u16 linkgood = 0;
216
217         /* retrieve from register structure */
218         dev = eth_get_dev();
219         info = dev->priv;
220
221         fecp = (FEC_T *) info->miibase;
222
223         fecpin_setclear(info, 1);
224
225         mii_reset(info);
226
227         /* We use strictly polling mode only */
228         fecp->eimr = 0;
229
230         /* Clear any pending interrupt */
231         fecp->eir = 0xffffffff;
232
233         /* Set MII speed */
234         miispd = (gd->bus_clk / 1000000) / 5;
235         fecp->mscr = miispd << 1;
236
237 #ifdef CONFIG_SYS_DISCOVER_PHY
238         info->phy_addr = mii_discover_phy(info);
239 #endif
240         if (info->phy_addr == -1)
241                 return;
242
243         while (i < info->to_loop) {
244                 status = 0;
245                 i++;
246                 /* Read PHY control register */
247                 miiphy_read(dev->name, info->phy_addr, MII_BMCR, &status);
248
249                 /* If phy set to autonegotiate, wait for autonegotiation done,
250                  * if phy is not autonegotiating, just wait for link up.
251                  */
252                 if ((status & BMCR_ANENABLE) == BMCR_ANENABLE) {
253                         linkgood = (BMSR_ANEGCOMPLETE | BMSR_LSTATUS);
254                 } else {
255                         linkgood = BMSR_LSTATUS;
256                 }
257                 /* Read PHY status register */
258                 miiphy_read(dev->name, info->phy_addr, MII_BMSR, &status);
259                 if ((status & linkgood) == linkgood)
260                         break;
261
262                 udelay(1);
263         }
264         if (i >= info->to_loop)
265                 printf("Link UP timeout\n");
266
267         /* adapt to the duplex and speed settings of the phy */
268         info->dup_spd = miiphy_duplex(dev->name, info->phy_addr) << 16;
269         info->dup_spd |= miiphy_speed(dev->name, info->phy_addr);
270 }
271
272 /*
273  * Read and write a MII PHY register, routines used by MII Utilities
274  *
275  * FIXME: These routines are expected to return 0 on success, but mii_send
276  *        does _not_ return an error code. Maybe 0xFFFF means error, i.e.
277  *        no PHY connected...
278  *        For now always return 0.
279  * FIXME: These routines only work after calling eth_init() at least once!
280  *        Otherwise they hang in mii_send() !!! Sorry!
281  */
282
283 int mcffec_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
284 {
285         short rdreg;            /* register working value */
286
287 #ifdef MII_DEBUG
288         printf("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
289 #endif
290         rdreg = mii_send(mk_mii_read(addr, reg));
291
292 #ifdef MII_DEBUG
293         printf("0x%04x\n", rdreg);
294 #endif
295
296         return rdreg;
297 }
298
299 int mcffec_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
300                         u16 value)
301 {
302 #ifdef MII_DEBUG
303         printf("miiphy_write(0x%x) @ 0x%x = 0x%04x\n", reg, addr, value);
304 #endif
305
306         mii_send(mk_mii_write(addr, reg, value));
307
308         return 0;
309 }
310
311 #endif                          /* CONFIG_CMD_NET */