blob: 7e53492733ed1c99ceccb324c015fc35118f1fc9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChung Liewf6afe722007-06-18 13:50:13 -05002/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
TsiChungLiew32b06d02007-07-05 23:13:58 -05006 * (C) Copyright 2007 Freescale Semiconductor, Inc.
TsiChung Liewf6afe722007-06-18 13:50:13 -05007 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
Angelo Durgehellob0c70a32019-11-15 23:54:18 +01008 *
9 * Conversion to DM
10 * (C) 2019 Angelo Dureghello <angelo.dureghello@timesys.com>
TsiChung Liewf6afe722007-06-18 13:50:13 -050011 */
12
Tom Rinidec7ea02024-05-20 13:35:03 -060013#include <config.h>
Tom Rinif8628fa2024-06-19 15:27:54 -060014#include <cpu_func.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060015#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070016#include <hang.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050017#include <malloc.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050018#include <command.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050019#include <net.h>
20#include <miiphy.h>
TsiChung Liewe602f222008-10-23 16:27:24 +000021#include <asm/fec.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
TsiChung Liewe602f222008-10-23 16:27:24 +000023#include <asm/immap.h>
Simon Glassdbd79542020-05-10 11:40:11 -060024#include <linux/delay.h>
Simon Glasscaefa322019-11-14 12:57:31 -070025#include <linux/mii.h>
TsiChung Liewe602f222008-10-23 16:27:24 +000026
TsiChung Liewf6afe722007-06-18 13:50:13 -050027#undef ET_DEBUG
28#undef MII_DEBUG
29
30/* Ethernet Transmit and Receive Buffers */
TsiChungLiew32b06d02007-07-05 23:13:58 -050031#define DBUF_LENGTH 1520
32#define TX_BUF_CNT 2
TsiChung Liewf6afe722007-06-18 13:50:13 -050033#define PKT_MAXBUF_SIZE 1518
TsiChung Liewf6afe722007-06-18 13:50:13 -050034#define PKT_MAXBLR_SIZE 1520
35#define LAST_PKTBUFSRX PKTBUFSRX - 1
36#define BD_ENET_RX_W_E (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY)
37#define BD_ENET_TX_RDY_LST (BD_ENET_TX_READY | BD_ENET_TX_LAST)
38
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010039DECLARE_GLOBAL_DATA_PTR;
40
41static void init_eth_info(struct fec_info_s *info)
42{
Tom Rini6a5dccc2022-11-16 13:10:41 -050043#ifdef CFG_SYS_FEC_BUF_USE_SRAM
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010044 static u32 tmp;
45
46 if (info->index == 0)
Tom Rini6a5dccc2022-11-16 13:10:41 -050047 tmp = CFG_SYS_INIT_RAM_ADDR + 0x1000;
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010048 else
49 info->rxbd = (cbd_t *)DBUF_LENGTH;
50
51 /* setup Receive and Transmit buffer descriptor */
52 info->rxbd = (cbd_t *)((u32)info->rxbd + tmp);
53 tmp = (u32)info->rxbd;
54 info->txbd =
55 (cbd_t *)((u32)info->txbd + tmp +
56 (PKTBUFSRX * sizeof(cbd_t)));
57 tmp = (u32)info->txbd;
58 info->txbuf =
59 (char *)((u32)info->txbuf + tmp +
Tom Rini6a5dccc2022-11-16 13:10:41 -050060 (CFG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010061 tmp = (u32)info->txbuf;
TsiChung Liew85722d12008-08-19 21:26:32 +000062#else
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010063 info->rxbd =
64 (cbd_t *)memalign(CONFIG_SYS_CACHELINE_SIZE,
65 (PKTBUFSRX * sizeof(cbd_t)));
66 info->txbd =
67 (cbd_t *)memalign(CONFIG_SYS_CACHELINE_SIZE,
68 (TX_BUF_CNT * sizeof(cbd_t)));
69 info->txbuf =
70 (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH);
TsiChung Liew85722d12008-08-19 21:26:32 +000071#endif
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010072
73#ifdef ET_DEBUG
74 printf("rxbd %x txbd %x\n", (int)info->rxbd, (int)info->txbd);
TsiChung Liewf6afe722007-06-18 13:50:13 -050075#endif
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010076 info->phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32);
77}
78
79static void fec_reset(struct fec_info_s *info)
80{
81 volatile fec_t *fecp = (fec_t *)(info->iobase);
82 int i;
TsiChung Liewf6afe722007-06-18 13:50:13 -050083
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010084 fecp->ecr = FEC_ECR_RESET;
85 for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i)
86 udelay(1);
TsiChung Liewf6afe722007-06-18 13:50:13 -050087
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010088 if (i == FEC_RESET_DELAY)
89 printf("FEC_RESET_DELAY timeout\n");
90}
91
92static void set_fec_duplex_speed(volatile fec_t *fecp, int dup_spd)
TsiChung Liewf6afe722007-06-18 13:50:13 -050093{
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090094 struct bd_info *bd = gd->bd;
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010095
TsiChung Liewf6afe722007-06-18 13:50:13 -050096 if ((dup_spd >> 16) == FULL) {
97 /* Set maximum frame length */
98 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
99 FEC_RCR_PROM | 0x100;
100 fecp->tcr = FEC_TCR_FDEN;
101 } else {
102 /* Half duplex mode */
103 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
104 FEC_RCR_MII_MODE | FEC_RCR_DRT;
105 fecp->tcr &= ~FEC_TCR_FDEN;
106 }
107
108 if ((dup_spd & 0xFFFF) == _100BASET) {
109#ifdef MII_DEBUG
110 printf("100Mbps\n");
111#endif
112 bd->bi_ethspeed = 100;
113 } else {
114#ifdef MII_DEBUG
115 printf("10Mbps\n");
116#endif
117 bd->bi_ethspeed = 10;
118 }
TsiChung Liewf6afe722007-06-18 13:50:13 -0500119}
120
TsiChung Liewf6afe722007-06-18 13:50:13 -0500121#ifdef ET_DEBUG
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100122static void dbg_fec_regs(struct udevice *dev)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500123{
Simon Glass95588622020-12-22 19:30:28 -0700124 struct fec_info_s *info = dev_get_priv(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100125 volatile fec_t *fecp = (fec_t *)(info->iobase);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500126
127 printf("=====\n");
128 printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
129 printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
130 printf("r_des_active %x - %x\n", (int)&fecp->rdar, fecp->rdar);
131 printf("x_des_active %x - %x\n", (int)&fecp->tdar, fecp->tdar);
132 printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
133 printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
134 printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
135 printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
136 printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
137 printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
138 printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
139 printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
140 printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
141 printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
142 printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
143 printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
144 printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
145 printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
146 printf("r_bound %x - %x\n", (int)&fecp->frbr, fecp->frbr);
147 printf("r_fstart %x - %x\n", (int)&fecp->frsr, fecp->frsr);
148 printf("r_drng %x - %x\n", (int)&fecp->erdsr, fecp->erdsr);
149 printf("x_drng %x - %x\n", (int)&fecp->etdsr, fecp->etdsr);
150 printf("r_bufsz %x - %x\n", (int)&fecp->emrbr, fecp->emrbr);
151
152 printf("\n");
153 printf("rmon_t_drop %x - %x\n", (int)&fecp->rmon_t_drop,
154 fecp->rmon_t_drop);
155 printf("rmon_t_packets %x - %x\n", (int)&fecp->rmon_t_packets,
156 fecp->rmon_t_packets);
157 printf("rmon_t_bc_pkt %x - %x\n", (int)&fecp->rmon_t_bc_pkt,
158 fecp->rmon_t_bc_pkt);
159 printf("rmon_t_mc_pkt %x - %x\n", (int)&fecp->rmon_t_mc_pkt,
160 fecp->rmon_t_mc_pkt);
161 printf("rmon_t_crc_align %x - %x\n", (int)&fecp->rmon_t_crc_align,
162 fecp->rmon_t_crc_align);
163 printf("rmon_t_undersize %x - %x\n", (int)&fecp->rmon_t_undersize,
164 fecp->rmon_t_undersize);
165 printf("rmon_t_oversize %x - %x\n", (int)&fecp->rmon_t_oversize,
166 fecp->rmon_t_oversize);
167 printf("rmon_t_frag %x - %x\n", (int)&fecp->rmon_t_frag,
168 fecp->rmon_t_frag);
169 printf("rmon_t_jab %x - %x\n", (int)&fecp->rmon_t_jab,
170 fecp->rmon_t_jab);
171 printf("rmon_t_col %x - %x\n", (int)&fecp->rmon_t_col,
172 fecp->rmon_t_col);
173 printf("rmon_t_p64 %x - %x\n", (int)&fecp->rmon_t_p64,
174 fecp->rmon_t_p64);
175 printf("rmon_t_p65to127 %x - %x\n", (int)&fecp->rmon_t_p65to127,
176 fecp->rmon_t_p65to127);
177 printf("rmon_t_p128to255 %x - %x\n", (int)&fecp->rmon_t_p128to255,
178 fecp->rmon_t_p128to255);
179 printf("rmon_t_p256to511 %x - %x\n", (int)&fecp->rmon_t_p256to511,
180 fecp->rmon_t_p256to511);
181 printf("rmon_t_p512to1023 %x - %x\n", (int)&fecp->rmon_t_p512to1023,
182 fecp->rmon_t_p512to1023);
183 printf("rmon_t_p1024to2047 %x - %x\n", (int)&fecp->rmon_t_p1024to2047,
184 fecp->rmon_t_p1024to2047);
185 printf("rmon_t_p_gte2048 %x - %x\n", (int)&fecp->rmon_t_p_gte2048,
186 fecp->rmon_t_p_gte2048);
187 printf("rmon_t_octets %x - %x\n", (int)&fecp->rmon_t_octets,
188 fecp->rmon_t_octets);
189
190 printf("\n");
191 printf("ieee_t_drop %x - %x\n", (int)&fecp->ieee_t_drop,
192 fecp->ieee_t_drop);
193 printf("ieee_t_frame_ok %x - %x\n", (int)&fecp->ieee_t_frame_ok,
194 fecp->ieee_t_frame_ok);
195 printf("ieee_t_1col %x - %x\n", (int)&fecp->ieee_t_1col,
196 fecp->ieee_t_1col);
197 printf("ieee_t_mcol %x - %x\n", (int)&fecp->ieee_t_mcol,
198 fecp->ieee_t_mcol);
199 printf("ieee_t_def %x - %x\n", (int)&fecp->ieee_t_def,
200 fecp->ieee_t_def);
201 printf("ieee_t_lcol %x - %x\n", (int)&fecp->ieee_t_lcol,
202 fecp->ieee_t_lcol);
203 printf("ieee_t_excol %x - %x\n", (int)&fecp->ieee_t_excol,
204 fecp->ieee_t_excol);
205 printf("ieee_t_macerr %x - %x\n", (int)&fecp->ieee_t_macerr,
206 fecp->ieee_t_macerr);
207 printf("ieee_t_cserr %x - %x\n", (int)&fecp->ieee_t_cserr,
208 fecp->ieee_t_cserr);
209 printf("ieee_t_sqe %x - %x\n", (int)&fecp->ieee_t_sqe,
210 fecp->ieee_t_sqe);
211 printf("ieee_t_fdxfc %x - %x\n", (int)&fecp->ieee_t_fdxfc,
212 fecp->ieee_t_fdxfc);
213 printf("ieee_t_octets_ok %x - %x\n", (int)&fecp->ieee_t_octets_ok,
214 fecp->ieee_t_octets_ok);
215
216 printf("\n");
217 printf("rmon_r_drop %x - %x\n", (int)&fecp->rmon_r_drop,
218 fecp->rmon_r_drop);
219 printf("rmon_r_packets %x - %x\n", (int)&fecp->rmon_r_packets,
220 fecp->rmon_r_packets);
221 printf("rmon_r_bc_pkt %x - %x\n", (int)&fecp->rmon_r_bc_pkt,
222 fecp->rmon_r_bc_pkt);
223 printf("rmon_r_mc_pkt %x - %x\n", (int)&fecp->rmon_r_mc_pkt,
224 fecp->rmon_r_mc_pkt);
225 printf("rmon_r_crc_align %x - %x\n", (int)&fecp->rmon_r_crc_align,
226 fecp->rmon_r_crc_align);
227 printf("rmon_r_undersize %x - %x\n", (int)&fecp->rmon_r_undersize,
228 fecp->rmon_r_undersize);
229 printf("rmon_r_oversize %x - %x\n", (int)&fecp->rmon_r_oversize,
230 fecp->rmon_r_oversize);
231 printf("rmon_r_frag %x - %x\n", (int)&fecp->rmon_r_frag,
232 fecp->rmon_r_frag);
233 printf("rmon_r_jab %x - %x\n", (int)&fecp->rmon_r_jab,
234 fecp->rmon_r_jab);
235 printf("rmon_r_p64 %x - %x\n", (int)&fecp->rmon_r_p64,
236 fecp->rmon_r_p64);
237 printf("rmon_r_p65to127 %x - %x\n", (int)&fecp->rmon_r_p65to127,
238 fecp->rmon_r_p65to127);
239 printf("rmon_r_p128to255 %x - %x\n", (int)&fecp->rmon_r_p128to255,
240 fecp->rmon_r_p128to255);
241 printf("rmon_r_p256to511 %x - %x\n", (int)&fecp->rmon_r_p256to511,
242 fecp->rmon_r_p256to511);
243 printf("rmon_r_p512to1023 %x - %x\n", (int)&fecp->rmon_r_p512to1023,
244 fecp->rmon_r_p512to1023);
245 printf("rmon_r_p1024to2047 %x - %x\n", (int)&fecp->rmon_r_p1024to2047,
246 fecp->rmon_r_p1024to2047);
247 printf("rmon_r_p_gte2048 %x - %x\n", (int)&fecp->rmon_r_p_gte2048,
248 fecp->rmon_r_p_gte2048);
249 printf("rmon_r_octets %x - %x\n", (int)&fecp->rmon_r_octets,
250 fecp->rmon_r_octets);
251
252 printf("\n");
253 printf("ieee_r_drop %x - %x\n", (int)&fecp->ieee_r_drop,
254 fecp->ieee_r_drop);
255 printf("ieee_r_frame_ok %x - %x\n", (int)&fecp->ieee_r_frame_ok,
256 fecp->ieee_r_frame_ok);
257 printf("ieee_r_crc %x - %x\n", (int)&fecp->ieee_r_crc,
258 fecp->ieee_r_crc);
259 printf("ieee_r_align %x - %x\n", (int)&fecp->ieee_r_align,
260 fecp->ieee_r_align);
261 printf("ieee_r_macerr %x - %x\n", (int)&fecp->ieee_r_macerr,
262 fecp->ieee_r_macerr);
263 printf("ieee_r_fdxfc %x - %x\n", (int)&fecp->ieee_r_fdxfc,
264 fecp->ieee_r_fdxfc);
265 printf("ieee_r_octets_ok %x - %x\n", (int)&fecp->ieee_r_octets_ok,
266 fecp->ieee_r_octets_ok);
267
268 printf("\n\n\n");
269}
270#endif
271
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100272int mcffec_init(struct udevice *dev)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500273{
Simon Glass95588622020-12-22 19:30:28 -0700274 struct fec_info_s *info = dev_get_priv(dev);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500275 volatile fec_t *fecp = (fec_t *) (info->iobase);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100276 int rval, i;
Mike Frysingerb2039652009-02-11 19:01:26 -0500277 uchar ea[6];
TsiChung Liewf6afe722007-06-18 13:50:13 -0500278
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100279 fecpin_setclear(info, 1);
280 fec_reset(info);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500281
TsiChung Liewf6afe722007-06-18 13:50:13 -0500282 mii_init();
283
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100284 set_fec_duplex_speed(fecp, info->dup_spd);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500285
286 /* We use strictly polling mode only */
287 fecp->eimr = 0;
288
289 /* Clear any pending interrupt */
290 fecp->eir = 0xffffffff;
291
292 /* Set station address */
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100293 if (info->index == 0)
294 rval = eth_env_get_enetaddr("ethaddr", ea);
295 else
296 rval = eth_env_get_enetaddr("eth1addr", ea);
297
298 if (!rval) {
299 puts("Please set a valid MAC address\n");
300 return -EINVAL;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500301 }
302
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100303 fecp->palr =
304 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
305 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
306
TsiChung Liewf6afe722007-06-18 13:50:13 -0500307 /* Clear unicast address hash table */
308 fecp->iaur = 0;
309 fecp->ialr = 0;
310
311 /* Clear multicast address hash table */
312 fecp->gaur = 0;
313 fecp->galr = 0;
314
315 /* Set maximum receive buffer size. */
316 fecp->emrbr = PKT_MAXBLR_SIZE;
317
318 /*
Heinrich Schuchardt42376962017-08-29 18:44:37 +0200319 * Setup Buffers and Buffer Descriptors
TsiChung Liewf6afe722007-06-18 13:50:13 -0500320 */
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100321 info->rx_idx = 0;
322 info->tx_idx = 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500323
324 /*
325 * Setup Receiver Buffer Descriptors (13.14.24.18)
326 * Settings:
327 * Empty, Wrap
328 */
329 for (i = 0; i < PKTBUFSRX; i++) {
330 info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
331 info->rxbd[i].cbd_datlen = 0; /* Reset */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500332 info->rxbd[i].cbd_bufaddr = (uint) net_rx_packets[i];
TsiChung Liewf6afe722007-06-18 13:50:13 -0500333 }
334 info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
335
336 /*
337 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
338 * Settings:
339 * Last, Tx CRC
340 */
341 for (i = 0; i < TX_BUF_CNT; i++) {
342 info->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
343 info->txbd[i].cbd_datlen = 0; /* Reset */
344 info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
345 }
346 info->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
347
348 /* Set receive and transmit descriptor base */
349 fecp->erdsr = (unsigned int)(&info->rxbd[0]);
350 fecp->etdsr = (unsigned int)(&info->txbd[0]);
351
352 /* Now enable the transmit and receive processing */
353 fecp->ecr |= FEC_ECR_ETHER_EN;
354
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100355 /* And last, try to fill Rx Buffer Descriptors
356 * Descriptor polling active
357 */
358 fecp->rdar = 0x01000000;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500359
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100360 return 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500361}
362
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100363static int mcffec_send(struct udevice *dev, void *packet, int length)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500364{
Simon Glass95588622020-12-22 19:30:28 -0700365 struct fec_info_s *info = dev_get_priv(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100366 volatile fec_t *fecp = (fec_t *)info->iobase;
367 int j, rc;
368 u16 phy_status;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500369
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100370 miiphy_read(dev->name, info->phy_addr, MII_BMSR, &phy_status);
371
372 /* section 16.9.23.3
373 * Wait for ready
374 */
375 j = 0;
376 while ((info->txbd[info->tx_idx].cbd_sc & BD_ENET_TX_READY) &&
377 (j < info->to_loop)) {
TsiChung Liewf6afe722007-06-18 13:50:13 -0500378 udelay(1);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100379 j++;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500380 }
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100381 if (j >= info->to_loop)
382 printf("TX not ready\n");
383
384 info->txbd[info->tx_idx].cbd_bufaddr = (uint)packet;
385 info->txbd[info->tx_idx].cbd_datlen = length;
386 info->txbd[info->tx_idx].cbd_sc |= BD_ENET_TX_RDY_LST;
387
388 /* Activate transmit Buffer Descriptor polling */
389 fecp->tdar = 0x01000000; /* Descriptor polling active */
390
Tom Rini6a5dccc2022-11-16 13:10:41 -0500391#ifndef CFG_SYS_FEC_BUF_USE_SRAM
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100392 /*
393 * FEC unable to initial transmit data packet.
394 * A nop will ensure the descriptor polling active completed.
395 * CF Internal RAM has shorter cycle access than DRAM. If use
396 * DRAM as Buffer descriptor and data, a nop is a must.
397 * Affect only V2 and V3.
398 */
399 __asm__ ("nop");
400#endif
401
402#ifdef CONFIG_SYS_UNIFY_CACHE
Tom Rinif8628fa2024-06-19 15:27:54 -0600403 invalidate_icache_all();
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100404#endif
405
406 j = 0;
407 while ((info->txbd[info->tx_idx].cbd_sc & BD_ENET_TX_READY) &&
408 (j < info->to_loop)) {
409 udelay(1);
410 j++;
411 }
412 if (j >= info->to_loop)
413 printf("TX timeout\n");
414
415#ifdef ET_DEBUG
416 printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
417 __FILE__, __LINE__, __func__, j,
418 info->txbd[info->tx_idx].cbd_sc,
419 (info->txbd[info->tx_idx].cbd_sc & 0x003C) >> 2);
420#endif
421
422 /* return only status bits */
423 rc = (info->txbd[info->tx_idx].cbd_sc & BD_ENET_TX_STATS);
424 info->tx_idx = (info->tx_idx + 1) % TX_BUF_CNT;
425
426 return rc;
427}
428
429static int mcffec_recv(struct udevice *dev, int flags, uchar **packetp)
430{
Simon Glass95588622020-12-22 19:30:28 -0700431 struct fec_info_s *info = dev_get_priv(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100432 volatile fec_t *fecp = (fec_t *)info->iobase;
433 int length = -1;
434
435 for (;;) {
436#ifdef CONFIG_SYS_UNIFY_CACHE
Tom Rinif8628fa2024-06-19 15:27:54 -0600437 invalidate_icache_all();
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100438#endif
439 /* If nothing received - leave for() loop */
440 if (info->rxbd[info->rx_idx].cbd_sc & BD_ENET_RX_EMPTY)
441 break;
442
443 length = info->rxbd[info->rx_idx].cbd_datlen;
444
445 if (info->rxbd[info->rx_idx].cbd_sc & 0x003f) {
446 printf("%s[%d] err: %x\n",
447 __func__, __LINE__,
448 info->rxbd[info->rx_idx].cbd_sc);
449 } else {
450 length -= 4;
451
452 /*
453 * Pass the buffer ptr up to the protocol layers.
454 */
455 *packetp = net_rx_packets[info->rx_idx];
456
457 fecp->eir |= FEC_EIR_RXF;
458 }
459
460 /* Give the buffer back to the FEC. */
461 info->rxbd[info->rx_idx].cbd_datlen = 0;
462
463 /* wrap around buffer index when necessary */
464 if (info->rx_idx == LAST_PKTBUFSRX) {
465 info->rxbd[PKTBUFSRX - 1].cbd_sc = BD_ENET_RX_W_E;
466 info->rx_idx = 0;
467 } else {
468 info->rxbd[info->rx_idx].cbd_sc = BD_ENET_RX_EMPTY;
469 info->rx_idx++;
470 }
471
472 /* Try to fill Buffer Descriptors
473 * Descriptor polling active
474 */
475 fecp->rdar = 0x01000000;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500476 }
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100477
478 return length;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500479}
480
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100481static void mcffec_halt(struct udevice *dev)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500482{
Simon Glass95588622020-12-22 19:30:28 -0700483 struct fec_info_s *info = dev_get_priv(dev);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500484
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100485 fec_reset(info);
486 fecpin_setclear(info, 0);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500487
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100488 info->rx_idx = 0;
489 info->tx_idx = 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500490
TsiChung Liewf6afe722007-06-18 13:50:13 -0500491 memset(info->rxbd, 0, PKTBUFSRX * sizeof(cbd_t));
492 memset(info->txbd, 0, TX_BUF_CNT * sizeof(cbd_t));
493 memset(info->txbuf, 0, DBUF_LENGTH);
494}
495
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100496static const struct eth_ops mcffec_ops = {
497 .start = mcffec_init,
498 .send = mcffec_send,
499 .recv = mcffec_recv,
500 .stop = mcffec_halt,
501};
502
503/*
Simon Glassaad29ae2020-12-03 16:55:21 -0700504 * Boot sequence, called just after mcffec_of_to_plat,
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100505 * as DM way, it replaces old mcffec_initialize.
506 */
507static int mcffec_probe(struct udevice *dev)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500508{
Simon Glassfa20e932020-12-03 16:55:20 -0700509 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass95588622020-12-22 19:30:28 -0700510 struct fec_info_s *info = dev_get_priv(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100511 int node = dev_of_offset(dev);
512 int retval, fec_idx;
513 const u32 *val;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500514
Simon Glass75e534b2020-12-16 21:20:07 -0700515 info->index = dev_seq(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100516 info->iobase = pdata->iobase;
517 info->phy_addr = -1;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500518
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100519 val = fdt_getprop(gd->fdt_blob, node, "mii-base", NULL);
520 if (val) {
521 u32 fec_iobase;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500522
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100523 fec_idx = fdt32_to_cpu(*val);
524 if (fec_idx == info->index) {
525 fec_iobase = info->iobase;
526 } else {
527 printf("mii base != base address, fec_idx %d\n",
528 fec_idx);
529 retval = fec_get_base_addr(fec_idx, &fec_iobase);
530 if (retval)
531 return retval;
532 }
533 info->miibase = fec_iobase;
534 }
TsiChung Liewf6afe722007-06-18 13:50:13 -0500535
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100536 val = fdt_getprop(gd->fdt_blob, node, "phy-addr", NULL);
537 if (val)
538 info->phy_addr = fdt32_to_cpu(*val);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500539
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100540 val = fdt_getprop(gd->fdt_blob, node, "timeout-loop", NULL);
541 if (val)
542 info->to_loop = fdt32_to_cpu(*val);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500543
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100544 init_eth_info(info);
TsiChung Liew85722d12008-08-19 21:26:32 +0000545
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100546#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
547 info->bus = mdio_alloc();
548 if (!info->bus)
549 return -ENOMEM;
550 strcpy(info->bus->name, dev->name);
551 info->bus->read = mcffec_miiphy_read;
552 info->bus->write = mcffec_miiphy_write;
553
554 retval = mdio_register(info->bus);
555 if (retval < 0)
556 return retval;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500557#endif
558
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100559 return 0;
560}
561
562static int mcffec_remove(struct udevice *dev)
563{
564 struct fec_info_s *priv = dev_get_priv(dev);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500565
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100566 mdio_unregister(priv->bus);
567 mdio_free(priv->bus);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500568
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100569 return 0;
570}
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500571
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100572/*
573 * Boot sequence, called 1st
574 */
Simon Glassaad29ae2020-12-03 16:55:21 -0700575static int mcffec_of_to_plat(struct udevice *dev)
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100576{
Simon Glassfa20e932020-12-03 16:55:20 -0700577 struct eth_pdata *pdata = dev_get_plat(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100578 const u32 *val;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500579
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900580 pdata->iobase = dev_read_addr(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100581 /* Default to 10Mbit/s */
582 pdata->max_speed = 10;
583
584 val = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
585 "max-speed", NULL);
586 if (val)
587 pdata->max_speed = fdt32_to_cpu(*val);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500588
Ben Warren90c96db2008-08-26 22:16:25 -0700589 return 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500590}
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100591
592static const struct udevice_id mcffec_ids[] = {
593 { .compatible = "fsl,mcf-fec" },
594 { }
595};
596
597U_BOOT_DRIVER(mcffec) = {
598 .name = "mcffec",
599 .id = UCLASS_ETH,
600 .of_match = mcffec_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700601 .of_to_plat = mcffec_of_to_plat,
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100602 .probe = mcffec_probe,
603 .remove = mcffec_remove,
604 .ops = &mcffec_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700605 .priv_auto = sizeof(struct fec_info_s),
Simon Glass71fa5b42020-12-03 16:55:18 -0700606 .plat_auto = sizeof(struct eth_pdata),
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100607};