blob: 2b54e3a5499f86f7a81b30b0dcd6aff3918ffc51 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChung Liew8e585f02007-06-18 13:50:13 -05002/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
TsiChungLiewf2208fb2007-07-05 23:13:58 -05006 * (C) Copyright 2007 Freescale Semiconductor, Inc.
TsiChung Liew8e585f02007-06-18 13:50:13 -05007 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
TsiChung Liew8e585f02007-06-18 13:50:13 -05008 */
9
10#include <common.h>
Alex Kiernan9925f1d2018-04-01 09:22:38 +000011#include <environment.h>
TsiChung Liew8e585f02007-06-18 13:50:13 -050012#include <malloc.h>
13
TsiChung Liew8e585f02007-06-18 13:50:13 -050014#include <command.h>
TsiChung Liew8e585f02007-06-18 13:50:13 -050015#include <net.h>
Ben Warren89973f82008-08-31 22:22:04 -070016#include <netdev.h>
TsiChung Liew8e585f02007-06-18 13:50:13 -050017#include <miiphy.h>
18
TsiChung Liew54bdcc92008-10-23 16:27:24 +000019#include <asm/fec.h>
20#include <asm/immap.h>
21
TsiChung Liew8e585f02007-06-18 13:50:13 -050022#undef ET_DEBUG
23#undef MII_DEBUG
24
25/* Ethernet Transmit and Receive Buffers */
TsiChungLiewf2208fb2007-07-05 23:13:58 -050026#define DBUF_LENGTH 1520
27#define TX_BUF_CNT 2
TsiChung Liew8e585f02007-06-18 13:50:13 -050028#define PKT_MAXBUF_SIZE 1518
29#define PKT_MINBUF_SIZE 64
30#define PKT_MAXBLR_SIZE 1520
31#define LAST_PKTBUFSRX PKTBUFSRX - 1
32#define BD_ENET_RX_W_E (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY)
33#define BD_ENET_TX_RDY_LST (BD_ENET_TX_READY | BD_ENET_TX_LAST)
34
TsiChung Liew8e585f02007-06-18 13:50:13 -050035struct fec_info_s fec_info[] = {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020036#ifdef CONFIG_SYS_FEC0_IOBASE
TsiChung Liew8e585f02007-06-18 13:50:13 -050037 {
38 0, /* index */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020039 CONFIG_SYS_FEC0_IOBASE, /* io base */
40 CONFIG_SYS_FEC0_PINMUX, /* gpio pin muxing */
41 CONFIG_SYS_FEC0_MIIBASE, /* mii base */
TsiChung Liew8e585f02007-06-18 13:50:13 -050042 -1, /* phy_addr */
43 0, /* duplex and speed */
44 0, /* phy name */
45 0, /* phyname init */
46 0, /* RX BD */
47 0, /* TX BD */
48 0, /* rx Index */
49 0, /* tx Index */
50 0, /* tx buffer */
51 0, /* initialized flag */
TsiChung Liew1803f7f2008-08-19 21:26:32 +000052 (struct fec_info_s *)-1,
TsiChung Liew8e585f02007-06-18 13:50:13 -050053 },
54#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020055#ifdef CONFIG_SYS_FEC1_IOBASE
TsiChung Liew8e585f02007-06-18 13:50:13 -050056 {
57 1, /* index */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020058 CONFIG_SYS_FEC1_IOBASE, /* io base */
59 CONFIG_SYS_FEC1_PINMUX, /* gpio pin muxing */
60 CONFIG_SYS_FEC1_MIIBASE, /* mii base */
TsiChung Liew8e585f02007-06-18 13:50:13 -050061 -1, /* phy_addr */
62 0, /* duplex and speed */
63 0, /* phy name */
64 0, /* phy name init */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020065#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
TsiChung Liew1803f7f2008-08-19 21:26:32 +000066 (cbd_t *)DBUF_LENGTH, /* RX BD */
67#else
TsiChung Liew8e585f02007-06-18 13:50:13 -050068 0, /* RX BD */
TsiChung Liew1803f7f2008-08-19 21:26:32 +000069#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -050070 0, /* TX BD */
71 0, /* rx Index */
72 0, /* tx Index */
73 0, /* tx buffer */
74 0, /* initialized flag */
TsiChung Liew1803f7f2008-08-19 21:26:32 +000075 (struct fec_info_s *)-1,
TsiChung Liew8e585f02007-06-18 13:50:13 -050076 }
77#endif
78};
79
TsiChung Liew8e585f02007-06-18 13:50:13 -050080int fec_recv(struct eth_device *dev);
81int fec_init(struct eth_device *dev, bd_t * bd);
82void fec_halt(struct eth_device *dev);
83void fec_reset(struct eth_device *dev);
84
TsiChung Liew8e585f02007-06-18 13:50:13 -050085void setFecDuplexSpeed(volatile fec_t * fecp, bd_t * bd, int dup_spd)
86{
87 if ((dup_spd >> 16) == FULL) {
88 /* Set maximum frame length */
89 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
90 FEC_RCR_PROM | 0x100;
91 fecp->tcr = FEC_TCR_FDEN;
92 } else {
93 /* Half duplex mode */
94 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
95 FEC_RCR_MII_MODE | FEC_RCR_DRT;
96 fecp->tcr &= ~FEC_TCR_FDEN;
97 }
98
99 if ((dup_spd & 0xFFFF) == _100BASET) {
TsiChung Liewff36fbb2008-05-28 13:06:25 -0500100#ifdef CONFIG_MCF5445x
101 fecp->rcr &= ~0x200; /* disabled 10T base */
102#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500103#ifdef MII_DEBUG
104 printf("100Mbps\n");
105#endif
106 bd->bi_ethspeed = 100;
107 } else {
TsiChung Liewff36fbb2008-05-28 13:06:25 -0500108#ifdef CONFIG_MCF5445x
109 fecp->rcr |= 0x200; /* enabled 10T base */
110#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500111#ifdef MII_DEBUG
112 printf("10Mbps\n");
113#endif
114 bd->bi_ethspeed = 10;
115 }
116}
117
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000118static int fec_send(struct eth_device *dev, void *packet, int length)
TsiChung Liew8e585f02007-06-18 13:50:13 -0500119{
120 struct fec_info_s *info = dev->priv;
121 volatile fec_t *fecp = (fec_t *) (info->iobase);
122 int j, rc;
123 u16 phyStatus;
124
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500125 miiphy_read(dev->name, info->phy_addr, MII_BMSR, &phyStatus);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500126
127 /* section 16.9.23.3
128 * Wait for ready
129 */
130 j = 0;
131 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
132 (j < MCFFEC_TOUT_LOOP)) {
133 udelay(1);
134 j++;
135 }
136 if (j >= MCFFEC_TOUT_LOOP) {
137 printf("TX not ready\n");
138 }
TsiChung0dca8742007-07-10 15:45:43 -0500139
TsiChung Liew8e585f02007-06-18 13:50:13 -0500140 info->txbd[info->txIdx].cbd_bufaddr = (uint) packet;
141 info->txbd[info->txIdx].cbd_datlen = length;
142 info->txbd[info->txIdx].cbd_sc |= BD_ENET_TX_RDY_LST;
143
144 /* Activate transmit Buffer Descriptor polling */
145 fecp->tdar = 0x01000000; /* Descriptor polling active */
146
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200147#ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000148 /*
149 * FEC unable to initial transmit data packet.
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500150 * A nop will ensure the descriptor polling active completed.
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000151 * CF Internal RAM has shorter cycle access than DRAM. If use
152 * DRAM as Buffer descriptor and data, a nop is a must.
153 * Affect only V2 and V3.
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500154 */
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500155 __asm__ ("nop");
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000156
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500157#endif
158
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200159#ifdef CONFIG_SYS_UNIFY_CACHE
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500160 icache_invalid();
161#endif
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000162
TsiChung0dca8742007-07-10 15:45:43 -0500163 j = 0;
TsiChung Liew8e585f02007-06-18 13:50:13 -0500164 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
165 (j < MCFFEC_TOUT_LOOP)) {
166 udelay(1);
167 j++;
168 }
169 if (j >= MCFFEC_TOUT_LOOP) {
170 printf("TX timeout\n");
171 }
TsiChung0dca8742007-07-10 15:45:43 -0500172
TsiChung Liew8e585f02007-06-18 13:50:13 -0500173#ifdef ET_DEBUG
174 printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
175 __FILE__, __LINE__, __FUNCTION__, j,
176 info->txbd[info->txIdx].cbd_sc,
177 (info->txbd[info->txIdx].cbd_sc & 0x003C) >> 2);
178#endif
179
TsiChung0dca8742007-07-10 15:45:43 -0500180 /* return only status bits */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500181 rc = (info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_STATS);
182 info->txIdx = (info->txIdx + 1) % TX_BUF_CNT;
183
184 return rc;
185}
186
187int fec_recv(struct eth_device *dev)
188{
189 struct fec_info_s *info = dev->priv;
190 volatile fec_t *fecp = (fec_t *) (info->iobase);
191 int length;
192
193 for (;;) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200194#ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000195#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200196#ifdef CONFIG_SYS_UNIFY_CACHE
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200197 icache_invalid();
TsiChung0dca8742007-07-10 15:45:43 -0500198#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500199 /* section 16.9.23.2 */
200 if (info->rxbd[info->rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
201 length = -1;
202 break; /* nothing received - leave for() loop */
203 }
204
205 length = info->rxbd[info->rxIdx].cbd_datlen;
206
207 if (info->rxbd[info->rxIdx].cbd_sc & 0x003f) {
208 printf("%s[%d] err: %x\n",
209 __FUNCTION__, __LINE__,
210 info->rxbd[info->rxIdx].cbd_sc);
211#ifdef ET_DEBUG
212 printf("%s[%d] err: %x\n",
213 __FUNCTION__, __LINE__,
214 info->rxbd[info->rxIdx].cbd_sc);
215#endif
216 } else {
217
218 length -= 4;
219 /* Pass the packet up to the protocol layers. */
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500220 net_process_received_packet(net_rx_packets[info->rxIdx],
221 length);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500222
223 fecp->eir |= FEC_EIR_RXF;
224 }
225
226 /* Give the buffer back to the FEC. */
227 info->rxbd[info->rxIdx].cbd_datlen = 0;
228
229 /* wrap around buffer index when necessary */
230 if (info->rxIdx == LAST_PKTBUFSRX) {
231 info->rxbd[PKTBUFSRX - 1].cbd_sc = BD_ENET_RX_W_E;
232 info->rxIdx = 0;
233 } else {
234 info->rxbd[info->rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
235 info->rxIdx++;
236 }
237
238 /* Try to fill Buffer Descriptors */
239 fecp->rdar = 0x01000000; /* Descriptor polling active */
240 }
241
242 return length;
243}
244
TsiChung Liew8e585f02007-06-18 13:50:13 -0500245#ifdef ET_DEBUG
246void dbgFecRegs(struct eth_device *dev)
247{
248 struct fec_info_s *info = dev->priv;
249 volatile fec_t *fecp = (fec_t *) (info->iobase);
250
251 printf("=====\n");
252 printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
253 printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
254 printf("r_des_active %x - %x\n", (int)&fecp->rdar, fecp->rdar);
255 printf("x_des_active %x - %x\n", (int)&fecp->tdar, fecp->tdar);
256 printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
257 printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
258 printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
259 printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
260 printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
261 printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
262 printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
263 printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
264 printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
265 printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
266 printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
267 printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
268 printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
269 printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
270 printf("r_bound %x - %x\n", (int)&fecp->frbr, fecp->frbr);
271 printf("r_fstart %x - %x\n", (int)&fecp->frsr, fecp->frsr);
272 printf("r_drng %x - %x\n", (int)&fecp->erdsr, fecp->erdsr);
273 printf("x_drng %x - %x\n", (int)&fecp->etdsr, fecp->etdsr);
274 printf("r_bufsz %x - %x\n", (int)&fecp->emrbr, fecp->emrbr);
275
276 printf("\n");
277 printf("rmon_t_drop %x - %x\n", (int)&fecp->rmon_t_drop,
278 fecp->rmon_t_drop);
279 printf("rmon_t_packets %x - %x\n", (int)&fecp->rmon_t_packets,
280 fecp->rmon_t_packets);
281 printf("rmon_t_bc_pkt %x - %x\n", (int)&fecp->rmon_t_bc_pkt,
282 fecp->rmon_t_bc_pkt);
283 printf("rmon_t_mc_pkt %x - %x\n", (int)&fecp->rmon_t_mc_pkt,
284 fecp->rmon_t_mc_pkt);
285 printf("rmon_t_crc_align %x - %x\n", (int)&fecp->rmon_t_crc_align,
286 fecp->rmon_t_crc_align);
287 printf("rmon_t_undersize %x - %x\n", (int)&fecp->rmon_t_undersize,
288 fecp->rmon_t_undersize);
289 printf("rmon_t_oversize %x - %x\n", (int)&fecp->rmon_t_oversize,
290 fecp->rmon_t_oversize);
291 printf("rmon_t_frag %x - %x\n", (int)&fecp->rmon_t_frag,
292 fecp->rmon_t_frag);
293 printf("rmon_t_jab %x - %x\n", (int)&fecp->rmon_t_jab,
294 fecp->rmon_t_jab);
295 printf("rmon_t_col %x - %x\n", (int)&fecp->rmon_t_col,
296 fecp->rmon_t_col);
297 printf("rmon_t_p64 %x - %x\n", (int)&fecp->rmon_t_p64,
298 fecp->rmon_t_p64);
299 printf("rmon_t_p65to127 %x - %x\n", (int)&fecp->rmon_t_p65to127,
300 fecp->rmon_t_p65to127);
301 printf("rmon_t_p128to255 %x - %x\n", (int)&fecp->rmon_t_p128to255,
302 fecp->rmon_t_p128to255);
303 printf("rmon_t_p256to511 %x - %x\n", (int)&fecp->rmon_t_p256to511,
304 fecp->rmon_t_p256to511);
305 printf("rmon_t_p512to1023 %x - %x\n", (int)&fecp->rmon_t_p512to1023,
306 fecp->rmon_t_p512to1023);
307 printf("rmon_t_p1024to2047 %x - %x\n", (int)&fecp->rmon_t_p1024to2047,
308 fecp->rmon_t_p1024to2047);
309 printf("rmon_t_p_gte2048 %x - %x\n", (int)&fecp->rmon_t_p_gte2048,
310 fecp->rmon_t_p_gte2048);
311 printf("rmon_t_octets %x - %x\n", (int)&fecp->rmon_t_octets,
312 fecp->rmon_t_octets);
313
314 printf("\n");
315 printf("ieee_t_drop %x - %x\n", (int)&fecp->ieee_t_drop,
316 fecp->ieee_t_drop);
317 printf("ieee_t_frame_ok %x - %x\n", (int)&fecp->ieee_t_frame_ok,
318 fecp->ieee_t_frame_ok);
319 printf("ieee_t_1col %x - %x\n", (int)&fecp->ieee_t_1col,
320 fecp->ieee_t_1col);
321 printf("ieee_t_mcol %x - %x\n", (int)&fecp->ieee_t_mcol,
322 fecp->ieee_t_mcol);
323 printf("ieee_t_def %x - %x\n", (int)&fecp->ieee_t_def,
324 fecp->ieee_t_def);
325 printf("ieee_t_lcol %x - %x\n", (int)&fecp->ieee_t_lcol,
326 fecp->ieee_t_lcol);
327 printf("ieee_t_excol %x - %x\n", (int)&fecp->ieee_t_excol,
328 fecp->ieee_t_excol);
329 printf("ieee_t_macerr %x - %x\n", (int)&fecp->ieee_t_macerr,
330 fecp->ieee_t_macerr);
331 printf("ieee_t_cserr %x - %x\n", (int)&fecp->ieee_t_cserr,
332 fecp->ieee_t_cserr);
333 printf("ieee_t_sqe %x - %x\n", (int)&fecp->ieee_t_sqe,
334 fecp->ieee_t_sqe);
335 printf("ieee_t_fdxfc %x - %x\n", (int)&fecp->ieee_t_fdxfc,
336 fecp->ieee_t_fdxfc);
337 printf("ieee_t_octets_ok %x - %x\n", (int)&fecp->ieee_t_octets_ok,
338 fecp->ieee_t_octets_ok);
339
340 printf("\n");
341 printf("rmon_r_drop %x - %x\n", (int)&fecp->rmon_r_drop,
342 fecp->rmon_r_drop);
343 printf("rmon_r_packets %x - %x\n", (int)&fecp->rmon_r_packets,
344 fecp->rmon_r_packets);
345 printf("rmon_r_bc_pkt %x - %x\n", (int)&fecp->rmon_r_bc_pkt,
346 fecp->rmon_r_bc_pkt);
347 printf("rmon_r_mc_pkt %x - %x\n", (int)&fecp->rmon_r_mc_pkt,
348 fecp->rmon_r_mc_pkt);
349 printf("rmon_r_crc_align %x - %x\n", (int)&fecp->rmon_r_crc_align,
350 fecp->rmon_r_crc_align);
351 printf("rmon_r_undersize %x - %x\n", (int)&fecp->rmon_r_undersize,
352 fecp->rmon_r_undersize);
353 printf("rmon_r_oversize %x - %x\n", (int)&fecp->rmon_r_oversize,
354 fecp->rmon_r_oversize);
355 printf("rmon_r_frag %x - %x\n", (int)&fecp->rmon_r_frag,
356 fecp->rmon_r_frag);
357 printf("rmon_r_jab %x - %x\n", (int)&fecp->rmon_r_jab,
358 fecp->rmon_r_jab);
359 printf("rmon_r_p64 %x - %x\n", (int)&fecp->rmon_r_p64,
360 fecp->rmon_r_p64);
361 printf("rmon_r_p65to127 %x - %x\n", (int)&fecp->rmon_r_p65to127,
362 fecp->rmon_r_p65to127);
363 printf("rmon_r_p128to255 %x - %x\n", (int)&fecp->rmon_r_p128to255,
364 fecp->rmon_r_p128to255);
365 printf("rmon_r_p256to511 %x - %x\n", (int)&fecp->rmon_r_p256to511,
366 fecp->rmon_r_p256to511);
367 printf("rmon_r_p512to1023 %x - %x\n", (int)&fecp->rmon_r_p512to1023,
368 fecp->rmon_r_p512to1023);
369 printf("rmon_r_p1024to2047 %x - %x\n", (int)&fecp->rmon_r_p1024to2047,
370 fecp->rmon_r_p1024to2047);
371 printf("rmon_r_p_gte2048 %x - %x\n", (int)&fecp->rmon_r_p_gte2048,
372 fecp->rmon_r_p_gte2048);
373 printf("rmon_r_octets %x - %x\n", (int)&fecp->rmon_r_octets,
374 fecp->rmon_r_octets);
375
376 printf("\n");
377 printf("ieee_r_drop %x - %x\n", (int)&fecp->ieee_r_drop,
378 fecp->ieee_r_drop);
379 printf("ieee_r_frame_ok %x - %x\n", (int)&fecp->ieee_r_frame_ok,
380 fecp->ieee_r_frame_ok);
381 printf("ieee_r_crc %x - %x\n", (int)&fecp->ieee_r_crc,
382 fecp->ieee_r_crc);
383 printf("ieee_r_align %x - %x\n", (int)&fecp->ieee_r_align,
384 fecp->ieee_r_align);
385 printf("ieee_r_macerr %x - %x\n", (int)&fecp->ieee_r_macerr,
386 fecp->ieee_r_macerr);
387 printf("ieee_r_fdxfc %x - %x\n", (int)&fecp->ieee_r_fdxfc,
388 fecp->ieee_r_fdxfc);
389 printf("ieee_r_octets_ok %x - %x\n", (int)&fecp->ieee_r_octets_ok,
390 fecp->ieee_r_octets_ok);
391
392 printf("\n\n\n");
393}
394#endif
395
396int fec_init(struct eth_device *dev, bd_t * bd)
397{
398 struct fec_info_s *info = dev->priv;
399 volatile fec_t *fecp = (fec_t *) (info->iobase);
400 int i;
Mike Frysingerd3f87142009-02-11 19:01:26 -0500401 uchar ea[6];
TsiChung Liew8e585f02007-06-18 13:50:13 -0500402
403 fecpin_setclear(dev, 1);
404
405 fec_reset(dev);
406
TsiChungLiewab77bc52007-08-15 15:39:17 -0500407#if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200408 defined (CONFIG_SYS_DISCOVER_PHY)
TsiChung Liew8e585f02007-06-18 13:50:13 -0500409
410 mii_init();
411
412 setFecDuplexSpeed(fecp, bd, info->dup_spd);
413#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200414#ifndef CONFIG_SYS_DISCOVER_PHY
TsiChung Liew8e585f02007-06-18 13:50:13 -0500415 setFecDuplexSpeed(fecp, bd, (FECDUPLEX << 16) | FECSPEED);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200416#endif /* ifndef CONFIG_SYS_DISCOVER_PHY */
TsiChungLiewab77bc52007-08-15 15:39:17 -0500417#endif /* CONFIG_CMD_MII || CONFIG_MII */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500418
419 /* We use strictly polling mode only */
420 fecp->eimr = 0;
421
422 /* Clear any pending interrupt */
423 fecp->eir = 0xffffffff;
424
425 /* Set station address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200426 if ((u32) fecp == CONFIG_SYS_FEC0_IOBASE) {
427#ifdef CONFIG_SYS_FEC1_IOBASE
428 volatile fec_t *fecp1 = (fec_t *) (CONFIG_SYS_FEC1_IOBASE);
Simon Glass35affd72017-08-03 12:22:14 -0600429 eth_env_get_enetaddr("eth1addr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500430 fecp1->palr =
431 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
432 fecp1->paur = (ea[4] << 24) | (ea[5] << 16);
433#endif
Simon Glass35affd72017-08-03 12:22:14 -0600434 eth_env_get_enetaddr("ethaddr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500435 fecp->palr =
436 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
437 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500438 } else {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200439#ifdef CONFIG_SYS_FEC0_IOBASE
440 volatile fec_t *fecp0 = (fec_t *) (CONFIG_SYS_FEC0_IOBASE);
Simon Glass35affd72017-08-03 12:22:14 -0600441 eth_env_get_enetaddr("ethaddr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500442 fecp0->palr =
443 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
444 fecp0->paur = (ea[4] << 24) | (ea[5] << 16);
445#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200446#ifdef CONFIG_SYS_FEC1_IOBASE
Simon Glass35affd72017-08-03 12:22:14 -0600447 eth_env_get_enetaddr("eth1addr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500448 fecp->palr =
449 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
450 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500451#endif
452 }
453
TsiChung Liew8e585f02007-06-18 13:50:13 -0500454 /* Clear unicast address hash table */
455 fecp->iaur = 0;
456 fecp->ialr = 0;
457
458 /* Clear multicast address hash table */
459 fecp->gaur = 0;
460 fecp->galr = 0;
461
462 /* Set maximum receive buffer size. */
463 fecp->emrbr = PKT_MAXBLR_SIZE;
464
465 /*
Heinrich Schuchardte4691562017-08-29 18:44:37 +0200466 * Setup Buffers and Buffer Descriptors
TsiChung Liew8e585f02007-06-18 13:50:13 -0500467 */
468 info->rxIdx = 0;
469 info->txIdx = 0;
470
471 /*
472 * Setup Receiver Buffer Descriptors (13.14.24.18)
473 * Settings:
474 * Empty, Wrap
475 */
476 for (i = 0; i < PKTBUFSRX; i++) {
477 info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
478 info->rxbd[i].cbd_datlen = 0; /* Reset */
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500479 info->rxbd[i].cbd_bufaddr = (uint) net_rx_packets[i];
TsiChung Liew8e585f02007-06-18 13:50:13 -0500480 }
481 info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
482
483 /*
484 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
485 * Settings:
486 * Last, Tx CRC
487 */
488 for (i = 0; i < TX_BUF_CNT; i++) {
489 info->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
490 info->txbd[i].cbd_datlen = 0; /* Reset */
491 info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
492 }
493 info->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
494
495 /* Set receive and transmit descriptor base */
496 fecp->erdsr = (unsigned int)(&info->rxbd[0]);
497 fecp->etdsr = (unsigned int)(&info->txbd[0]);
498
499 /* Now enable the transmit and receive processing */
500 fecp->ecr |= FEC_ECR_ETHER_EN;
501
502 /* And last, try to fill Rx Buffer Descriptors */
503 fecp->rdar = 0x01000000; /* Descriptor polling active */
504
505 return 1;
506}
507
508void fec_reset(struct eth_device *dev)
509{
510 struct fec_info_s *info = dev->priv;
511 volatile fec_t *fecp = (fec_t *) (info->iobase);
512 int i;
513
514 fecp->ecr = FEC_ECR_RESET;
515 for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
516 udelay(1);
517 }
518 if (i == FEC_RESET_DELAY) {
519 printf("FEC_RESET_DELAY timeout\n");
520 }
521}
522
523void fec_halt(struct eth_device *dev)
524{
525 struct fec_info_s *info = dev->priv;
526
527 fec_reset(dev);
528
529 fecpin_setclear(dev, 0);
530
531 info->rxIdx = info->txIdx = 0;
532 memset(info->rxbd, 0, PKTBUFSRX * sizeof(cbd_t));
533 memset(info->txbd, 0, TX_BUF_CNT * sizeof(cbd_t));
534 memset(info->txbuf, 0, DBUF_LENGTH);
535}
536
537int mcffec_initialize(bd_t * bis)
538{
539 struct eth_device *dev;
540 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200541#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
542 u32 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000543#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500544
Axel Lina62cd292013-07-03 11:24:18 +0800545 for (i = 0; i < ARRAY_SIZE(fec_info); i++) {
TsiChung Liew8e585f02007-06-18 13:50:13 -0500546
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500547 dev =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200548 (struct eth_device *)memalign(CONFIG_SYS_CACHELINE_SIZE,
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500549 sizeof *dev);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500550 if (dev == NULL)
551 hang();
552
553 memset(dev, 0, sizeof(*dev));
554
555 sprintf(dev->name, "FEC%d", fec_info[i].index);
556
557 dev->priv = &fec_info[i];
558 dev->init = fec_init;
559 dev->halt = fec_halt;
560 dev->send = fec_send;
561 dev->recv = fec_recv;
562
563 /* setup Receive and Transmit buffer descriptor */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200564#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000565 fec_info[i].rxbd = (cbd_t *)((u32)fec_info[i].rxbd + tmp);
566 tmp = (u32)fec_info[i].rxbd;
567 fec_info[i].txbd =
568 (cbd_t *)((u32)fec_info[i].txbd + tmp +
569 (PKTBUFSRX * sizeof(cbd_t)));
570 tmp = (u32)fec_info[i].txbd;
571 fec_info[i].txbuf =
572 (char *)((u32)fec_info[i].txbuf + tmp +
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200573 (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000574 tmp = (u32)fec_info[i].txbuf;
575#else
TsiChung Liew8e585f02007-06-18 13:50:13 -0500576 fec_info[i].rxbd =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200577 (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500578 (PKTBUFSRX * sizeof(cbd_t)));
TsiChung Liew8e585f02007-06-18 13:50:13 -0500579 fec_info[i].txbd =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200580 (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500581 (TX_BUF_CNT * sizeof(cbd_t)));
582 fec_info[i].txbuf =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200583 (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH);
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000584#endif
585
TsiChung Liew8e585f02007-06-18 13:50:13 -0500586#ifdef ET_DEBUG
587 printf("rxbd %x txbd %x\n",
588 (int)fec_info[i].rxbd, (int)fec_info[i].txbd);
589#endif
590
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200591 fec_info[i].phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500592
593 eth_register(dev);
594
TsiChungLiewab77bc52007-08-15 15:39:17 -0500595#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger5a49f172016-08-08 11:28:38 -0500596 int retval;
597 struct mii_dev *mdiodev = mdio_alloc();
598 if (!mdiodev)
599 return -ENOMEM;
600 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
601 mdiodev->read = mcffec_miiphy_read;
602 mdiodev->write = mcffec_miiphy_write;
603
604 retval = mdio_register(mdiodev);
605 if (retval < 0)
606 return retval;
TsiChung Liew8e585f02007-06-18 13:50:13 -0500607#endif
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000608 if (i > 0)
609 fec_info[i - 1].next = &fec_info[i];
TsiChung Liew8e585f02007-06-18 13:50:13 -0500610 }
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000611 fec_info[i - 1].next = &fec_info[0];
TsiChung Liew8e585f02007-06-18 13:50:13 -0500612
613 /* default speed */
614 bis->bi_ethspeed = 10;
615
Ben Warren86882b82008-08-26 22:16:25 -0700616 return 0;
TsiChung Liew8e585f02007-06-18 13:50:13 -0500617}