blob: cb343b446f59e5d4dfae3cf46b243c19d5c1f18c [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
13#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060014#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070015#include <hang.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050016#include <malloc.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050017#include <command.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050018#include <net.h>
19#include <miiphy.h>
TsiChung Liewe602f222008-10-23 16:27:24 +000020#include <asm/fec.h>
21#include <asm/immap.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Simon Glasscaefa322019-11-14 12:57:31 -070023#include <linux/mii.h>
TsiChung Liewe602f222008-10-23 16:27:24 +000024
TsiChung Liewf6afe722007-06-18 13:50:13 -050025#undef ET_DEBUG
26#undef MII_DEBUG
27
28/* Ethernet Transmit and Receive Buffers */
TsiChungLiew32b06d02007-07-05 23:13:58 -050029#define DBUF_LENGTH 1520
30#define TX_BUF_CNT 2
TsiChung Liewf6afe722007-06-18 13:50:13 -050031#define PKT_MAXBUF_SIZE 1518
TsiChung Liewf6afe722007-06-18 13:50:13 -050032#define PKT_MAXBLR_SIZE 1520
33#define LAST_PKTBUFSRX PKTBUFSRX - 1
34#define BD_ENET_RX_W_E (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY)
35#define BD_ENET_TX_RDY_LST (BD_ENET_TX_READY | BD_ENET_TX_LAST)
36
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010037DECLARE_GLOBAL_DATA_PTR;
38
39static void init_eth_info(struct fec_info_s *info)
40{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020041#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010042 static u32 tmp;
43
44 if (info->index == 0)
45 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
46 else
47 info->rxbd = (cbd_t *)DBUF_LENGTH;
48
49 /* setup Receive and Transmit buffer descriptor */
50 info->rxbd = (cbd_t *)((u32)info->rxbd + tmp);
51 tmp = (u32)info->rxbd;
52 info->txbd =
53 (cbd_t *)((u32)info->txbd + tmp +
54 (PKTBUFSRX * sizeof(cbd_t)));
55 tmp = (u32)info->txbd;
56 info->txbuf =
57 (char *)((u32)info->txbuf + tmp +
58 (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
59 tmp = (u32)info->txbuf;
TsiChung Liew85722d12008-08-19 21:26:32 +000060#else
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010061 info->rxbd =
62 (cbd_t *)memalign(CONFIG_SYS_CACHELINE_SIZE,
63 (PKTBUFSRX * sizeof(cbd_t)));
64 info->txbd =
65 (cbd_t *)memalign(CONFIG_SYS_CACHELINE_SIZE,
66 (TX_BUF_CNT * sizeof(cbd_t)));
67 info->txbuf =
68 (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH);
TsiChung Liew85722d12008-08-19 21:26:32 +000069#endif
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010070
71#ifdef ET_DEBUG
72 printf("rxbd %x txbd %x\n", (int)info->rxbd, (int)info->txbd);
TsiChung Liewf6afe722007-06-18 13:50:13 -050073#endif
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010074 info->phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32);
75}
76
77static void fec_reset(struct fec_info_s *info)
78{
79 volatile fec_t *fecp = (fec_t *)(info->iobase);
80 int i;
TsiChung Liewf6afe722007-06-18 13:50:13 -050081
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010082 fecp->ecr = FEC_ECR_RESET;
83 for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i)
84 udelay(1);
TsiChung Liewf6afe722007-06-18 13:50:13 -050085
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010086 if (i == FEC_RESET_DELAY)
87 printf("FEC_RESET_DELAY timeout\n");
88}
89
90static void set_fec_duplex_speed(volatile fec_t *fecp, int dup_spd)
TsiChung Liewf6afe722007-06-18 13:50:13 -050091{
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090092 struct bd_info *bd = gd->bd;
Angelo Durgehellob0c70a32019-11-15 23:54:18 +010093
TsiChung Liewf6afe722007-06-18 13:50:13 -050094 if ((dup_spd >> 16) == FULL) {
95 /* Set maximum frame length */
96 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
97 FEC_RCR_PROM | 0x100;
98 fecp->tcr = FEC_TCR_FDEN;
99 } else {
100 /* Half duplex mode */
101 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
102 FEC_RCR_MII_MODE | FEC_RCR_DRT;
103 fecp->tcr &= ~FEC_TCR_FDEN;
104 }
105
106 if ((dup_spd & 0xFFFF) == _100BASET) {
TsiChung Liew6346d022008-05-28 13:06:25 -0500107#ifdef CONFIG_MCF5445x
108 fecp->rcr &= ~0x200; /* disabled 10T base */
109#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -0500110#ifdef MII_DEBUG
111 printf("100Mbps\n");
112#endif
113 bd->bi_ethspeed = 100;
114 } else {
TsiChung Liew6346d022008-05-28 13:06:25 -0500115#ifdef CONFIG_MCF5445x
116 fecp->rcr |= 0x200; /* enabled 10T base */
117#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -0500118#ifdef MII_DEBUG
119 printf("10Mbps\n");
120#endif
121 bd->bi_ethspeed = 10;
122 }
TsiChung Liewf6afe722007-06-18 13:50:13 -0500123}
124
TsiChung Liewf6afe722007-06-18 13:50:13 -0500125#ifdef ET_DEBUG
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100126static void dbg_fec_regs(struct udevice *dev)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500127{
Simon Glass95588622020-12-22 19:30:28 -0700128 struct fec_info_s *info = dev_get_priv(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100129 volatile fec_t *fecp = (fec_t *)(info->iobase);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500130
131 printf("=====\n");
132 printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
133 printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
134 printf("r_des_active %x - %x\n", (int)&fecp->rdar, fecp->rdar);
135 printf("x_des_active %x - %x\n", (int)&fecp->tdar, fecp->tdar);
136 printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
137 printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
138 printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
139 printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
140 printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
141 printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
142 printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
143 printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
144 printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
145 printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
146 printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
147 printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
148 printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
149 printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
150 printf("r_bound %x - %x\n", (int)&fecp->frbr, fecp->frbr);
151 printf("r_fstart %x - %x\n", (int)&fecp->frsr, fecp->frsr);
152 printf("r_drng %x - %x\n", (int)&fecp->erdsr, fecp->erdsr);
153 printf("x_drng %x - %x\n", (int)&fecp->etdsr, fecp->etdsr);
154 printf("r_bufsz %x - %x\n", (int)&fecp->emrbr, fecp->emrbr);
155
156 printf("\n");
157 printf("rmon_t_drop %x - %x\n", (int)&fecp->rmon_t_drop,
158 fecp->rmon_t_drop);
159 printf("rmon_t_packets %x - %x\n", (int)&fecp->rmon_t_packets,
160 fecp->rmon_t_packets);
161 printf("rmon_t_bc_pkt %x - %x\n", (int)&fecp->rmon_t_bc_pkt,
162 fecp->rmon_t_bc_pkt);
163 printf("rmon_t_mc_pkt %x - %x\n", (int)&fecp->rmon_t_mc_pkt,
164 fecp->rmon_t_mc_pkt);
165 printf("rmon_t_crc_align %x - %x\n", (int)&fecp->rmon_t_crc_align,
166 fecp->rmon_t_crc_align);
167 printf("rmon_t_undersize %x - %x\n", (int)&fecp->rmon_t_undersize,
168 fecp->rmon_t_undersize);
169 printf("rmon_t_oversize %x - %x\n", (int)&fecp->rmon_t_oversize,
170 fecp->rmon_t_oversize);
171 printf("rmon_t_frag %x - %x\n", (int)&fecp->rmon_t_frag,
172 fecp->rmon_t_frag);
173 printf("rmon_t_jab %x - %x\n", (int)&fecp->rmon_t_jab,
174 fecp->rmon_t_jab);
175 printf("rmon_t_col %x - %x\n", (int)&fecp->rmon_t_col,
176 fecp->rmon_t_col);
177 printf("rmon_t_p64 %x - %x\n", (int)&fecp->rmon_t_p64,
178 fecp->rmon_t_p64);
179 printf("rmon_t_p65to127 %x - %x\n", (int)&fecp->rmon_t_p65to127,
180 fecp->rmon_t_p65to127);
181 printf("rmon_t_p128to255 %x - %x\n", (int)&fecp->rmon_t_p128to255,
182 fecp->rmon_t_p128to255);
183 printf("rmon_t_p256to511 %x - %x\n", (int)&fecp->rmon_t_p256to511,
184 fecp->rmon_t_p256to511);
185 printf("rmon_t_p512to1023 %x - %x\n", (int)&fecp->rmon_t_p512to1023,
186 fecp->rmon_t_p512to1023);
187 printf("rmon_t_p1024to2047 %x - %x\n", (int)&fecp->rmon_t_p1024to2047,
188 fecp->rmon_t_p1024to2047);
189 printf("rmon_t_p_gte2048 %x - %x\n", (int)&fecp->rmon_t_p_gte2048,
190 fecp->rmon_t_p_gte2048);
191 printf("rmon_t_octets %x - %x\n", (int)&fecp->rmon_t_octets,
192 fecp->rmon_t_octets);
193
194 printf("\n");
195 printf("ieee_t_drop %x - %x\n", (int)&fecp->ieee_t_drop,
196 fecp->ieee_t_drop);
197 printf("ieee_t_frame_ok %x - %x\n", (int)&fecp->ieee_t_frame_ok,
198 fecp->ieee_t_frame_ok);
199 printf("ieee_t_1col %x - %x\n", (int)&fecp->ieee_t_1col,
200 fecp->ieee_t_1col);
201 printf("ieee_t_mcol %x - %x\n", (int)&fecp->ieee_t_mcol,
202 fecp->ieee_t_mcol);
203 printf("ieee_t_def %x - %x\n", (int)&fecp->ieee_t_def,
204 fecp->ieee_t_def);
205 printf("ieee_t_lcol %x - %x\n", (int)&fecp->ieee_t_lcol,
206 fecp->ieee_t_lcol);
207 printf("ieee_t_excol %x - %x\n", (int)&fecp->ieee_t_excol,
208 fecp->ieee_t_excol);
209 printf("ieee_t_macerr %x - %x\n", (int)&fecp->ieee_t_macerr,
210 fecp->ieee_t_macerr);
211 printf("ieee_t_cserr %x - %x\n", (int)&fecp->ieee_t_cserr,
212 fecp->ieee_t_cserr);
213 printf("ieee_t_sqe %x - %x\n", (int)&fecp->ieee_t_sqe,
214 fecp->ieee_t_sqe);
215 printf("ieee_t_fdxfc %x - %x\n", (int)&fecp->ieee_t_fdxfc,
216 fecp->ieee_t_fdxfc);
217 printf("ieee_t_octets_ok %x - %x\n", (int)&fecp->ieee_t_octets_ok,
218 fecp->ieee_t_octets_ok);
219
220 printf("\n");
221 printf("rmon_r_drop %x - %x\n", (int)&fecp->rmon_r_drop,
222 fecp->rmon_r_drop);
223 printf("rmon_r_packets %x - %x\n", (int)&fecp->rmon_r_packets,
224 fecp->rmon_r_packets);
225 printf("rmon_r_bc_pkt %x - %x\n", (int)&fecp->rmon_r_bc_pkt,
226 fecp->rmon_r_bc_pkt);
227 printf("rmon_r_mc_pkt %x - %x\n", (int)&fecp->rmon_r_mc_pkt,
228 fecp->rmon_r_mc_pkt);
229 printf("rmon_r_crc_align %x - %x\n", (int)&fecp->rmon_r_crc_align,
230 fecp->rmon_r_crc_align);
231 printf("rmon_r_undersize %x - %x\n", (int)&fecp->rmon_r_undersize,
232 fecp->rmon_r_undersize);
233 printf("rmon_r_oversize %x - %x\n", (int)&fecp->rmon_r_oversize,
234 fecp->rmon_r_oversize);
235 printf("rmon_r_frag %x - %x\n", (int)&fecp->rmon_r_frag,
236 fecp->rmon_r_frag);
237 printf("rmon_r_jab %x - %x\n", (int)&fecp->rmon_r_jab,
238 fecp->rmon_r_jab);
239 printf("rmon_r_p64 %x - %x\n", (int)&fecp->rmon_r_p64,
240 fecp->rmon_r_p64);
241 printf("rmon_r_p65to127 %x - %x\n", (int)&fecp->rmon_r_p65to127,
242 fecp->rmon_r_p65to127);
243 printf("rmon_r_p128to255 %x - %x\n", (int)&fecp->rmon_r_p128to255,
244 fecp->rmon_r_p128to255);
245 printf("rmon_r_p256to511 %x - %x\n", (int)&fecp->rmon_r_p256to511,
246 fecp->rmon_r_p256to511);
247 printf("rmon_r_p512to1023 %x - %x\n", (int)&fecp->rmon_r_p512to1023,
248 fecp->rmon_r_p512to1023);
249 printf("rmon_r_p1024to2047 %x - %x\n", (int)&fecp->rmon_r_p1024to2047,
250 fecp->rmon_r_p1024to2047);
251 printf("rmon_r_p_gte2048 %x - %x\n", (int)&fecp->rmon_r_p_gte2048,
252 fecp->rmon_r_p_gte2048);
253 printf("rmon_r_octets %x - %x\n", (int)&fecp->rmon_r_octets,
254 fecp->rmon_r_octets);
255
256 printf("\n");
257 printf("ieee_r_drop %x - %x\n", (int)&fecp->ieee_r_drop,
258 fecp->ieee_r_drop);
259 printf("ieee_r_frame_ok %x - %x\n", (int)&fecp->ieee_r_frame_ok,
260 fecp->ieee_r_frame_ok);
261 printf("ieee_r_crc %x - %x\n", (int)&fecp->ieee_r_crc,
262 fecp->ieee_r_crc);
263 printf("ieee_r_align %x - %x\n", (int)&fecp->ieee_r_align,
264 fecp->ieee_r_align);
265 printf("ieee_r_macerr %x - %x\n", (int)&fecp->ieee_r_macerr,
266 fecp->ieee_r_macerr);
267 printf("ieee_r_fdxfc %x - %x\n", (int)&fecp->ieee_r_fdxfc,
268 fecp->ieee_r_fdxfc);
269 printf("ieee_r_octets_ok %x - %x\n", (int)&fecp->ieee_r_octets_ok,
270 fecp->ieee_r_octets_ok);
271
272 printf("\n\n\n");
273}
274#endif
275
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100276int mcffec_init(struct udevice *dev)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500277{
Simon Glass95588622020-12-22 19:30:28 -0700278 struct fec_info_s *info = dev_get_priv(dev);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500279 volatile fec_t *fecp = (fec_t *) (info->iobase);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100280 int rval, i;
Mike Frysingerb2039652009-02-11 19:01:26 -0500281 uchar ea[6];
TsiChung Liewf6afe722007-06-18 13:50:13 -0500282
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100283 fecpin_setclear(info, 1);
284 fec_reset(info);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500285
TsiChungLiewaedd3d72007-08-15 15:39:17 -0500286#if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200287 defined (CONFIG_SYS_DISCOVER_PHY)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500288
289 mii_init();
290
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100291 set_fec_duplex_speed(fecp, info->dup_spd);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500292#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200293#ifndef CONFIG_SYS_DISCOVER_PHY
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100294 set_fec_duplex_speed(fecp, (FECDUPLEX << 16) | FECSPEED);
295#endif /* ifndef CONFIG_SYS_DISCOVER_PHY */
296#endif /* CONFIG_CMD_MII || CONFIG_MII */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500297
298 /* We use strictly polling mode only */
299 fecp->eimr = 0;
300
301 /* Clear any pending interrupt */
302 fecp->eir = 0xffffffff;
303
304 /* Set station address */
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100305 if (info->index == 0)
306 rval = eth_env_get_enetaddr("ethaddr", ea);
307 else
308 rval = eth_env_get_enetaddr("eth1addr", ea);
309
310 if (!rval) {
311 puts("Please set a valid MAC address\n");
312 return -EINVAL;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500313 }
314
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100315 fecp->palr =
316 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
317 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
318
TsiChung Liewf6afe722007-06-18 13:50:13 -0500319 /* Clear unicast address hash table */
320 fecp->iaur = 0;
321 fecp->ialr = 0;
322
323 /* Clear multicast address hash table */
324 fecp->gaur = 0;
325 fecp->galr = 0;
326
327 /* Set maximum receive buffer size. */
328 fecp->emrbr = PKT_MAXBLR_SIZE;
329
330 /*
Heinrich Schuchardt42376962017-08-29 18:44:37 +0200331 * Setup Buffers and Buffer Descriptors
TsiChung Liewf6afe722007-06-18 13:50:13 -0500332 */
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100333 info->rx_idx = 0;
334 info->tx_idx = 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500335
336 /*
337 * Setup Receiver Buffer Descriptors (13.14.24.18)
338 * Settings:
339 * Empty, Wrap
340 */
341 for (i = 0; i < PKTBUFSRX; i++) {
342 info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
343 info->rxbd[i].cbd_datlen = 0; /* Reset */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500344 info->rxbd[i].cbd_bufaddr = (uint) net_rx_packets[i];
TsiChung Liewf6afe722007-06-18 13:50:13 -0500345 }
346 info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
347
348 /*
349 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
350 * Settings:
351 * Last, Tx CRC
352 */
353 for (i = 0; i < TX_BUF_CNT; i++) {
354 info->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
355 info->txbd[i].cbd_datlen = 0; /* Reset */
356 info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
357 }
358 info->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
359
360 /* Set receive and transmit descriptor base */
361 fecp->erdsr = (unsigned int)(&info->rxbd[0]);
362 fecp->etdsr = (unsigned int)(&info->txbd[0]);
363
364 /* Now enable the transmit and receive processing */
365 fecp->ecr |= FEC_ECR_ETHER_EN;
366
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100367 /* And last, try to fill Rx Buffer Descriptors
368 * Descriptor polling active
369 */
370 fecp->rdar = 0x01000000;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500371
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100372 return 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500373}
374
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100375static int mcffec_send(struct udevice *dev, void *packet, int length)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500376{
Simon Glass95588622020-12-22 19:30:28 -0700377 struct fec_info_s *info = dev_get_priv(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100378 volatile fec_t *fecp = (fec_t *)info->iobase;
379 int j, rc;
380 u16 phy_status;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500381
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100382 miiphy_read(dev->name, info->phy_addr, MII_BMSR, &phy_status);
383
384 /* section 16.9.23.3
385 * Wait for ready
386 */
387 j = 0;
388 while ((info->txbd[info->tx_idx].cbd_sc & BD_ENET_TX_READY) &&
389 (j < info->to_loop)) {
TsiChung Liewf6afe722007-06-18 13:50:13 -0500390 udelay(1);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100391 j++;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500392 }
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100393 if (j >= info->to_loop)
394 printf("TX not ready\n");
395
396 info->txbd[info->tx_idx].cbd_bufaddr = (uint)packet;
397 info->txbd[info->tx_idx].cbd_datlen = length;
398 info->txbd[info->tx_idx].cbd_sc |= BD_ENET_TX_RDY_LST;
399
400 /* Activate transmit Buffer Descriptor polling */
401 fecp->tdar = 0x01000000; /* Descriptor polling active */
402
403#ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
404 /*
405 * FEC unable to initial transmit data packet.
406 * A nop will ensure the descriptor polling active completed.
407 * CF Internal RAM has shorter cycle access than DRAM. If use
408 * DRAM as Buffer descriptor and data, a nop is a must.
409 * Affect only V2 and V3.
410 */
411 __asm__ ("nop");
412#endif
413
414#ifdef CONFIG_SYS_UNIFY_CACHE
415 icache_invalid();
416#endif
417
418 j = 0;
419 while ((info->txbd[info->tx_idx].cbd_sc & BD_ENET_TX_READY) &&
420 (j < info->to_loop)) {
421 udelay(1);
422 j++;
423 }
424 if (j >= info->to_loop)
425 printf("TX timeout\n");
426
427#ifdef ET_DEBUG
428 printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
429 __FILE__, __LINE__, __func__, j,
430 info->txbd[info->tx_idx].cbd_sc,
431 (info->txbd[info->tx_idx].cbd_sc & 0x003C) >> 2);
432#endif
433
434 /* return only status bits */
435 rc = (info->txbd[info->tx_idx].cbd_sc & BD_ENET_TX_STATS);
436 info->tx_idx = (info->tx_idx + 1) % TX_BUF_CNT;
437
438 return rc;
439}
440
441static int mcffec_recv(struct udevice *dev, int flags, uchar **packetp)
442{
Simon Glass95588622020-12-22 19:30:28 -0700443 struct fec_info_s *info = dev_get_priv(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100444 volatile fec_t *fecp = (fec_t *)info->iobase;
445 int length = -1;
446
447 for (;;) {
448#ifdef CONFIG_SYS_UNIFY_CACHE
449 icache_invalid();
450#endif
451 /* If nothing received - leave for() loop */
452 if (info->rxbd[info->rx_idx].cbd_sc & BD_ENET_RX_EMPTY)
453 break;
454
455 length = info->rxbd[info->rx_idx].cbd_datlen;
456
457 if (info->rxbd[info->rx_idx].cbd_sc & 0x003f) {
458 printf("%s[%d] err: %x\n",
459 __func__, __LINE__,
460 info->rxbd[info->rx_idx].cbd_sc);
461 } else {
462 length -= 4;
463
464 /*
465 * Pass the buffer ptr up to the protocol layers.
466 */
467 *packetp = net_rx_packets[info->rx_idx];
468
469 fecp->eir |= FEC_EIR_RXF;
470 }
471
472 /* Give the buffer back to the FEC. */
473 info->rxbd[info->rx_idx].cbd_datlen = 0;
474
475 /* wrap around buffer index when necessary */
476 if (info->rx_idx == LAST_PKTBUFSRX) {
477 info->rxbd[PKTBUFSRX - 1].cbd_sc = BD_ENET_RX_W_E;
478 info->rx_idx = 0;
479 } else {
480 info->rxbd[info->rx_idx].cbd_sc = BD_ENET_RX_EMPTY;
481 info->rx_idx++;
482 }
483
484 /* Try to fill Buffer Descriptors
485 * Descriptor polling active
486 */
487 fecp->rdar = 0x01000000;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500488 }
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100489
490 return length;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500491}
492
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100493static void mcffec_halt(struct udevice *dev)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500494{
Simon Glass95588622020-12-22 19:30:28 -0700495 struct fec_info_s *info = dev_get_priv(dev);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500496
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100497 fec_reset(info);
498 fecpin_setclear(info, 0);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500499
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100500 info->rx_idx = 0;
501 info->tx_idx = 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500502
TsiChung Liewf6afe722007-06-18 13:50:13 -0500503 memset(info->rxbd, 0, PKTBUFSRX * sizeof(cbd_t));
504 memset(info->txbd, 0, TX_BUF_CNT * sizeof(cbd_t));
505 memset(info->txbuf, 0, DBUF_LENGTH);
506}
507
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100508static const struct eth_ops mcffec_ops = {
509 .start = mcffec_init,
510 .send = mcffec_send,
511 .recv = mcffec_recv,
512 .stop = mcffec_halt,
513};
514
515/*
Simon Glassaad29ae2020-12-03 16:55:21 -0700516 * Boot sequence, called just after mcffec_of_to_plat,
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100517 * as DM way, it replaces old mcffec_initialize.
518 */
519static int mcffec_probe(struct udevice *dev)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500520{
Simon Glassfa20e932020-12-03 16:55:20 -0700521 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass95588622020-12-22 19:30:28 -0700522 struct fec_info_s *info = dev_get_priv(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100523 int node = dev_of_offset(dev);
524 int retval, fec_idx;
525 const u32 *val;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500526
Simon Glass75e534b2020-12-16 21:20:07 -0700527 info->index = dev_seq(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100528 info->iobase = pdata->iobase;
529 info->phy_addr = -1;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500530
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100531 val = fdt_getprop(gd->fdt_blob, node, "mii-base", NULL);
532 if (val) {
533 u32 fec_iobase;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500534
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100535 fec_idx = fdt32_to_cpu(*val);
536 if (fec_idx == info->index) {
537 fec_iobase = info->iobase;
538 } else {
539 printf("mii base != base address, fec_idx %d\n",
540 fec_idx);
541 retval = fec_get_base_addr(fec_idx, &fec_iobase);
542 if (retval)
543 return retval;
544 }
545 info->miibase = fec_iobase;
546 }
TsiChung Liewf6afe722007-06-18 13:50:13 -0500547
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100548 val = fdt_getprop(gd->fdt_blob, node, "phy-addr", NULL);
549 if (val)
550 info->phy_addr = fdt32_to_cpu(*val);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500551
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100552 val = fdt_getprop(gd->fdt_blob, node, "timeout-loop", NULL);
553 if (val)
554 info->to_loop = fdt32_to_cpu(*val);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500555
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100556 init_eth_info(info);
TsiChung Liew85722d12008-08-19 21:26:32 +0000557
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100558#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
559 info->bus = mdio_alloc();
560 if (!info->bus)
561 return -ENOMEM;
562 strcpy(info->bus->name, dev->name);
563 info->bus->read = mcffec_miiphy_read;
564 info->bus->write = mcffec_miiphy_write;
565
566 retval = mdio_register(info->bus);
567 if (retval < 0)
568 return retval;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500569#endif
570
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100571 return 0;
572}
573
574static int mcffec_remove(struct udevice *dev)
575{
576 struct fec_info_s *priv = dev_get_priv(dev);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500577
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100578 mdio_unregister(priv->bus);
579 mdio_free(priv->bus);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500580
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100581 return 0;
582}
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500583
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100584/*
585 * Boot sequence, called 1st
586 */
Simon Glassaad29ae2020-12-03 16:55:21 -0700587static int mcffec_of_to_plat(struct udevice *dev)
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100588{
Simon Glassfa20e932020-12-03 16:55:20 -0700589 struct eth_pdata *pdata = dev_get_plat(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100590 const u32 *val;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500591
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900592 pdata->iobase = dev_read_addr(dev);
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100593 /* Default to 10Mbit/s */
594 pdata->max_speed = 10;
595
596 val = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
597 "max-speed", NULL);
598 if (val)
599 pdata->max_speed = fdt32_to_cpu(*val);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500600
Ben Warren90c96db2008-08-26 22:16:25 -0700601 return 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500602}
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100603
604static const struct udevice_id mcffec_ids[] = {
605 { .compatible = "fsl,mcf-fec" },
606 { }
607};
608
609U_BOOT_DRIVER(mcffec) = {
610 .name = "mcffec",
611 .id = UCLASS_ETH,
612 .of_match = mcffec_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700613 .of_to_plat = mcffec_of_to_plat,
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100614 .probe = mcffec_probe,
615 .remove = mcffec_remove,
616 .ops = &mcffec_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700617 .priv_auto = sizeof(struct fec_info_s),
Simon Glass71fa5b42020-12-03 16:55:18 -0700618 .plat_auto = sizeof(struct eth_pdata),
Angelo Durgehellob0c70a32019-11-15 23:54:18 +0100619};