blob: ed7459c6470d1ec03524892f7f9526ada590fd24 [file] [log] [blame]
TsiChung Liew8e585f02007-06-18 13:50:13 -05001/*
2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
TsiChungLiewf2208fb2007-07-05 23:13:58 -05005 * (C) Copyright 2007 Freescale Semiconductor, Inc.
TsiChung Liew8e585f02007-06-18 13:50:13 -05006 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
28#include <malloc.h>
29
TsiChung Liew8e585f02007-06-18 13:50:13 -050030#include <command.h>
TsiChung Liew8e585f02007-06-18 13:50:13 -050031#include <net.h>
Ben Warren89973f82008-08-31 22:22:04 -070032#include <netdev.h>
TsiChung Liew8e585f02007-06-18 13:50:13 -050033#include <miiphy.h>
34
TsiChung Liew54bdcc92008-10-23 16:27:24 +000035#include <asm/fec.h>
36#include <asm/immap.h>
37
TsiChung Liew8e585f02007-06-18 13:50:13 -050038#undef ET_DEBUG
39#undef MII_DEBUG
40
41/* Ethernet Transmit and Receive Buffers */
TsiChungLiewf2208fb2007-07-05 23:13:58 -050042#define DBUF_LENGTH 1520
43#define TX_BUF_CNT 2
TsiChung Liew8e585f02007-06-18 13:50:13 -050044#define PKT_MAXBUF_SIZE 1518
45#define PKT_MINBUF_SIZE 64
46#define PKT_MAXBLR_SIZE 1520
47#define LAST_PKTBUFSRX PKTBUFSRX - 1
48#define BD_ENET_RX_W_E (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY)
49#define BD_ENET_TX_RDY_LST (BD_ENET_TX_READY | BD_ENET_TX_LAST)
50
51DECLARE_GLOBAL_DATA_PTR;
52
TsiChung Liew8e585f02007-06-18 13:50:13 -050053struct fec_info_s fec_info[] = {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020054#ifdef CONFIG_SYS_FEC0_IOBASE
TsiChung Liew8e585f02007-06-18 13:50:13 -050055 {
56 0, /* index */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020057 CONFIG_SYS_FEC0_IOBASE, /* io base */
58 CONFIG_SYS_FEC0_PINMUX, /* gpio pin muxing */
59 CONFIG_SYS_FEC0_MIIBASE, /* mii base */
TsiChung Liew8e585f02007-06-18 13:50:13 -050060 -1, /* phy_addr */
61 0, /* duplex and speed */
62 0, /* phy name */
63 0, /* phyname init */
64 0, /* RX BD */
65 0, /* TX BD */
66 0, /* rx Index */
67 0, /* tx Index */
68 0, /* tx buffer */
69 0, /* initialized flag */
TsiChung Liew1803f7f2008-08-19 21:26:32 +000070 (struct fec_info_s *)-1,
TsiChung Liew8e585f02007-06-18 13:50:13 -050071 },
72#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020073#ifdef CONFIG_SYS_FEC1_IOBASE
TsiChung Liew8e585f02007-06-18 13:50:13 -050074 {
75 1, /* index */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020076 CONFIG_SYS_FEC1_IOBASE, /* io base */
77 CONFIG_SYS_FEC1_PINMUX, /* gpio pin muxing */
78 CONFIG_SYS_FEC1_MIIBASE, /* mii base */
TsiChung Liew8e585f02007-06-18 13:50:13 -050079 -1, /* phy_addr */
80 0, /* duplex and speed */
81 0, /* phy name */
82 0, /* phy name init */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020083#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
TsiChung Liew1803f7f2008-08-19 21:26:32 +000084 (cbd_t *)DBUF_LENGTH, /* RX BD */
85#else
TsiChung Liew8e585f02007-06-18 13:50:13 -050086 0, /* RX BD */
TsiChung Liew1803f7f2008-08-19 21:26:32 +000087#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -050088 0, /* TX BD */
89 0, /* rx Index */
90 0, /* tx Index */
91 0, /* tx buffer */
92 0, /* initialized flag */
TsiChung Liew1803f7f2008-08-19 21:26:32 +000093 (struct fec_info_s *)-1,
TsiChung Liew8e585f02007-06-18 13:50:13 -050094 }
95#endif
96};
97
TsiChung Liew8e585f02007-06-18 13:50:13 -050098int fec_recv(struct eth_device *dev);
99int fec_init(struct eth_device *dev, bd_t * bd);
100void fec_halt(struct eth_device *dev);
101void fec_reset(struct eth_device *dev);
102
TsiChung Liew8e585f02007-06-18 13:50:13 -0500103void setFecDuplexSpeed(volatile fec_t * fecp, bd_t * bd, int dup_spd)
104{
105 if ((dup_spd >> 16) == FULL) {
106 /* Set maximum frame length */
107 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
108 FEC_RCR_PROM | 0x100;
109 fecp->tcr = FEC_TCR_FDEN;
110 } else {
111 /* Half duplex mode */
112 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
113 FEC_RCR_MII_MODE | FEC_RCR_DRT;
114 fecp->tcr &= ~FEC_TCR_FDEN;
115 }
116
117 if ((dup_spd & 0xFFFF) == _100BASET) {
TsiChung Liewff36fbb2008-05-28 13:06:25 -0500118#ifdef CONFIG_MCF5445x
119 fecp->rcr &= ~0x200; /* disabled 10T base */
120#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500121#ifdef MII_DEBUG
122 printf("100Mbps\n");
123#endif
124 bd->bi_ethspeed = 100;
125 } else {
TsiChung Liewff36fbb2008-05-28 13:06:25 -0500126#ifdef CONFIG_MCF5445x
127 fecp->rcr |= 0x200; /* enabled 10T base */
128#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500129#ifdef MII_DEBUG
130 printf("10Mbps\n");
131#endif
132 bd->bi_ethspeed = 10;
133 }
134}
135
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000136static int fec_send(struct eth_device *dev, void *packet, int length)
TsiChung Liew8e585f02007-06-18 13:50:13 -0500137{
138 struct fec_info_s *info = dev->priv;
139 volatile fec_t *fecp = (fec_t *) (info->iobase);
140 int j, rc;
141 u16 phyStatus;
142
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500143 miiphy_read(dev->name, info->phy_addr, MII_BMSR, &phyStatus);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500144
145 /* section 16.9.23.3
146 * Wait for ready
147 */
148 j = 0;
149 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
150 (j < MCFFEC_TOUT_LOOP)) {
151 udelay(1);
152 j++;
153 }
154 if (j >= MCFFEC_TOUT_LOOP) {
155 printf("TX not ready\n");
156 }
TsiChung0dca8742007-07-10 15:45:43 -0500157
TsiChung Liew8e585f02007-06-18 13:50:13 -0500158 info->txbd[info->txIdx].cbd_bufaddr = (uint) packet;
159 info->txbd[info->txIdx].cbd_datlen = length;
160 info->txbd[info->txIdx].cbd_sc |= BD_ENET_TX_RDY_LST;
161
162 /* Activate transmit Buffer Descriptor polling */
163 fecp->tdar = 0x01000000; /* Descriptor polling active */
164
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200165#ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000166 /*
167 * FEC unable to initial transmit data packet.
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500168 * A nop will ensure the descriptor polling active completed.
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000169 * CF Internal RAM has shorter cycle access than DRAM. If use
170 * DRAM as Buffer descriptor and data, a nop is a must.
171 * Affect only V2 and V3.
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500172 */
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500173 __asm__ ("nop");
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000174
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500175#endif
176
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200177#ifdef CONFIG_SYS_UNIFY_CACHE
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500178 icache_invalid();
179#endif
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000180
TsiChung0dca8742007-07-10 15:45:43 -0500181 j = 0;
TsiChung Liew8e585f02007-06-18 13:50:13 -0500182 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
183 (j < MCFFEC_TOUT_LOOP)) {
184 udelay(1);
185 j++;
186 }
187 if (j >= MCFFEC_TOUT_LOOP) {
188 printf("TX timeout\n");
189 }
TsiChung0dca8742007-07-10 15:45:43 -0500190
TsiChung Liew8e585f02007-06-18 13:50:13 -0500191#ifdef ET_DEBUG
192 printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
193 __FILE__, __LINE__, __FUNCTION__, j,
194 info->txbd[info->txIdx].cbd_sc,
195 (info->txbd[info->txIdx].cbd_sc & 0x003C) >> 2);
196#endif
197
TsiChung0dca8742007-07-10 15:45:43 -0500198 /* return only status bits */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500199 rc = (info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_STATS);
200 info->txIdx = (info->txIdx + 1) % TX_BUF_CNT;
201
202 return rc;
203}
204
205int fec_recv(struct eth_device *dev)
206{
207 struct fec_info_s *info = dev->priv;
208 volatile fec_t *fecp = (fec_t *) (info->iobase);
209 int length;
210
211 for (;;) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200212#ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000213#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200214#ifdef CONFIG_SYS_UNIFY_CACHE
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200215 icache_invalid();
TsiChung0dca8742007-07-10 15:45:43 -0500216#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500217 /* section 16.9.23.2 */
218 if (info->rxbd[info->rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
219 length = -1;
220 break; /* nothing received - leave for() loop */
221 }
222
223 length = info->rxbd[info->rxIdx].cbd_datlen;
224
225 if (info->rxbd[info->rxIdx].cbd_sc & 0x003f) {
226 printf("%s[%d] err: %x\n",
227 __FUNCTION__, __LINE__,
228 info->rxbd[info->rxIdx].cbd_sc);
229#ifdef ET_DEBUG
230 printf("%s[%d] err: %x\n",
231 __FUNCTION__, __LINE__,
232 info->rxbd[info->rxIdx].cbd_sc);
233#endif
234 } else {
235
236 length -= 4;
237 /* Pass the packet up to the protocol layers. */
238 NetReceive(NetRxPackets[info->rxIdx], length);
239
240 fecp->eir |= FEC_EIR_RXF;
241 }
242
243 /* Give the buffer back to the FEC. */
244 info->rxbd[info->rxIdx].cbd_datlen = 0;
245
246 /* wrap around buffer index when necessary */
247 if (info->rxIdx == LAST_PKTBUFSRX) {
248 info->rxbd[PKTBUFSRX - 1].cbd_sc = BD_ENET_RX_W_E;
249 info->rxIdx = 0;
250 } else {
251 info->rxbd[info->rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
252 info->rxIdx++;
253 }
254
255 /* Try to fill Buffer Descriptors */
256 fecp->rdar = 0x01000000; /* Descriptor polling active */
257 }
258
259 return length;
260}
261
TsiChung Liew8e585f02007-06-18 13:50:13 -0500262#ifdef ET_DEBUG
263void dbgFecRegs(struct eth_device *dev)
264{
265 struct fec_info_s *info = dev->priv;
266 volatile fec_t *fecp = (fec_t *) (info->iobase);
267
268 printf("=====\n");
269 printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
270 printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
271 printf("r_des_active %x - %x\n", (int)&fecp->rdar, fecp->rdar);
272 printf("x_des_active %x - %x\n", (int)&fecp->tdar, fecp->tdar);
273 printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
274 printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
275 printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
276 printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
277 printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
278 printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
279 printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
280 printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
281 printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
282 printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
283 printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
284 printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
285 printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
286 printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
287 printf("r_bound %x - %x\n", (int)&fecp->frbr, fecp->frbr);
288 printf("r_fstart %x - %x\n", (int)&fecp->frsr, fecp->frsr);
289 printf("r_drng %x - %x\n", (int)&fecp->erdsr, fecp->erdsr);
290 printf("x_drng %x - %x\n", (int)&fecp->etdsr, fecp->etdsr);
291 printf("r_bufsz %x - %x\n", (int)&fecp->emrbr, fecp->emrbr);
292
293 printf("\n");
294 printf("rmon_t_drop %x - %x\n", (int)&fecp->rmon_t_drop,
295 fecp->rmon_t_drop);
296 printf("rmon_t_packets %x - %x\n", (int)&fecp->rmon_t_packets,
297 fecp->rmon_t_packets);
298 printf("rmon_t_bc_pkt %x - %x\n", (int)&fecp->rmon_t_bc_pkt,
299 fecp->rmon_t_bc_pkt);
300 printf("rmon_t_mc_pkt %x - %x\n", (int)&fecp->rmon_t_mc_pkt,
301 fecp->rmon_t_mc_pkt);
302 printf("rmon_t_crc_align %x - %x\n", (int)&fecp->rmon_t_crc_align,
303 fecp->rmon_t_crc_align);
304 printf("rmon_t_undersize %x - %x\n", (int)&fecp->rmon_t_undersize,
305 fecp->rmon_t_undersize);
306 printf("rmon_t_oversize %x - %x\n", (int)&fecp->rmon_t_oversize,
307 fecp->rmon_t_oversize);
308 printf("rmon_t_frag %x - %x\n", (int)&fecp->rmon_t_frag,
309 fecp->rmon_t_frag);
310 printf("rmon_t_jab %x - %x\n", (int)&fecp->rmon_t_jab,
311 fecp->rmon_t_jab);
312 printf("rmon_t_col %x - %x\n", (int)&fecp->rmon_t_col,
313 fecp->rmon_t_col);
314 printf("rmon_t_p64 %x - %x\n", (int)&fecp->rmon_t_p64,
315 fecp->rmon_t_p64);
316 printf("rmon_t_p65to127 %x - %x\n", (int)&fecp->rmon_t_p65to127,
317 fecp->rmon_t_p65to127);
318 printf("rmon_t_p128to255 %x - %x\n", (int)&fecp->rmon_t_p128to255,
319 fecp->rmon_t_p128to255);
320 printf("rmon_t_p256to511 %x - %x\n", (int)&fecp->rmon_t_p256to511,
321 fecp->rmon_t_p256to511);
322 printf("rmon_t_p512to1023 %x - %x\n", (int)&fecp->rmon_t_p512to1023,
323 fecp->rmon_t_p512to1023);
324 printf("rmon_t_p1024to2047 %x - %x\n", (int)&fecp->rmon_t_p1024to2047,
325 fecp->rmon_t_p1024to2047);
326 printf("rmon_t_p_gte2048 %x - %x\n", (int)&fecp->rmon_t_p_gte2048,
327 fecp->rmon_t_p_gte2048);
328 printf("rmon_t_octets %x - %x\n", (int)&fecp->rmon_t_octets,
329 fecp->rmon_t_octets);
330
331 printf("\n");
332 printf("ieee_t_drop %x - %x\n", (int)&fecp->ieee_t_drop,
333 fecp->ieee_t_drop);
334 printf("ieee_t_frame_ok %x - %x\n", (int)&fecp->ieee_t_frame_ok,
335 fecp->ieee_t_frame_ok);
336 printf("ieee_t_1col %x - %x\n", (int)&fecp->ieee_t_1col,
337 fecp->ieee_t_1col);
338 printf("ieee_t_mcol %x - %x\n", (int)&fecp->ieee_t_mcol,
339 fecp->ieee_t_mcol);
340 printf("ieee_t_def %x - %x\n", (int)&fecp->ieee_t_def,
341 fecp->ieee_t_def);
342 printf("ieee_t_lcol %x - %x\n", (int)&fecp->ieee_t_lcol,
343 fecp->ieee_t_lcol);
344 printf("ieee_t_excol %x - %x\n", (int)&fecp->ieee_t_excol,
345 fecp->ieee_t_excol);
346 printf("ieee_t_macerr %x - %x\n", (int)&fecp->ieee_t_macerr,
347 fecp->ieee_t_macerr);
348 printf("ieee_t_cserr %x - %x\n", (int)&fecp->ieee_t_cserr,
349 fecp->ieee_t_cserr);
350 printf("ieee_t_sqe %x - %x\n", (int)&fecp->ieee_t_sqe,
351 fecp->ieee_t_sqe);
352 printf("ieee_t_fdxfc %x - %x\n", (int)&fecp->ieee_t_fdxfc,
353 fecp->ieee_t_fdxfc);
354 printf("ieee_t_octets_ok %x - %x\n", (int)&fecp->ieee_t_octets_ok,
355 fecp->ieee_t_octets_ok);
356
357 printf("\n");
358 printf("rmon_r_drop %x - %x\n", (int)&fecp->rmon_r_drop,
359 fecp->rmon_r_drop);
360 printf("rmon_r_packets %x - %x\n", (int)&fecp->rmon_r_packets,
361 fecp->rmon_r_packets);
362 printf("rmon_r_bc_pkt %x - %x\n", (int)&fecp->rmon_r_bc_pkt,
363 fecp->rmon_r_bc_pkt);
364 printf("rmon_r_mc_pkt %x - %x\n", (int)&fecp->rmon_r_mc_pkt,
365 fecp->rmon_r_mc_pkt);
366 printf("rmon_r_crc_align %x - %x\n", (int)&fecp->rmon_r_crc_align,
367 fecp->rmon_r_crc_align);
368 printf("rmon_r_undersize %x - %x\n", (int)&fecp->rmon_r_undersize,
369 fecp->rmon_r_undersize);
370 printf("rmon_r_oversize %x - %x\n", (int)&fecp->rmon_r_oversize,
371 fecp->rmon_r_oversize);
372 printf("rmon_r_frag %x - %x\n", (int)&fecp->rmon_r_frag,
373 fecp->rmon_r_frag);
374 printf("rmon_r_jab %x - %x\n", (int)&fecp->rmon_r_jab,
375 fecp->rmon_r_jab);
376 printf("rmon_r_p64 %x - %x\n", (int)&fecp->rmon_r_p64,
377 fecp->rmon_r_p64);
378 printf("rmon_r_p65to127 %x - %x\n", (int)&fecp->rmon_r_p65to127,
379 fecp->rmon_r_p65to127);
380 printf("rmon_r_p128to255 %x - %x\n", (int)&fecp->rmon_r_p128to255,
381 fecp->rmon_r_p128to255);
382 printf("rmon_r_p256to511 %x - %x\n", (int)&fecp->rmon_r_p256to511,
383 fecp->rmon_r_p256to511);
384 printf("rmon_r_p512to1023 %x - %x\n", (int)&fecp->rmon_r_p512to1023,
385 fecp->rmon_r_p512to1023);
386 printf("rmon_r_p1024to2047 %x - %x\n", (int)&fecp->rmon_r_p1024to2047,
387 fecp->rmon_r_p1024to2047);
388 printf("rmon_r_p_gte2048 %x - %x\n", (int)&fecp->rmon_r_p_gte2048,
389 fecp->rmon_r_p_gte2048);
390 printf("rmon_r_octets %x - %x\n", (int)&fecp->rmon_r_octets,
391 fecp->rmon_r_octets);
392
393 printf("\n");
394 printf("ieee_r_drop %x - %x\n", (int)&fecp->ieee_r_drop,
395 fecp->ieee_r_drop);
396 printf("ieee_r_frame_ok %x - %x\n", (int)&fecp->ieee_r_frame_ok,
397 fecp->ieee_r_frame_ok);
398 printf("ieee_r_crc %x - %x\n", (int)&fecp->ieee_r_crc,
399 fecp->ieee_r_crc);
400 printf("ieee_r_align %x - %x\n", (int)&fecp->ieee_r_align,
401 fecp->ieee_r_align);
402 printf("ieee_r_macerr %x - %x\n", (int)&fecp->ieee_r_macerr,
403 fecp->ieee_r_macerr);
404 printf("ieee_r_fdxfc %x - %x\n", (int)&fecp->ieee_r_fdxfc,
405 fecp->ieee_r_fdxfc);
406 printf("ieee_r_octets_ok %x - %x\n", (int)&fecp->ieee_r_octets_ok,
407 fecp->ieee_r_octets_ok);
408
409 printf("\n\n\n");
410}
411#endif
412
413int fec_init(struct eth_device *dev, bd_t * bd)
414{
415 struct fec_info_s *info = dev->priv;
416 volatile fec_t *fecp = (fec_t *) (info->iobase);
417 int i;
Mike Frysingerd3f87142009-02-11 19:01:26 -0500418 uchar ea[6];
TsiChung Liew8e585f02007-06-18 13:50:13 -0500419
420 fecpin_setclear(dev, 1);
421
422 fec_reset(dev);
423
TsiChungLiewab77bc52007-08-15 15:39:17 -0500424#if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200425 defined (CONFIG_SYS_DISCOVER_PHY)
TsiChung Liew8e585f02007-06-18 13:50:13 -0500426
427 mii_init();
428
429 setFecDuplexSpeed(fecp, bd, info->dup_spd);
430#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200431#ifndef CONFIG_SYS_DISCOVER_PHY
TsiChung Liew8e585f02007-06-18 13:50:13 -0500432 setFecDuplexSpeed(fecp, bd, (FECDUPLEX << 16) | FECSPEED);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200433#endif /* ifndef CONFIG_SYS_DISCOVER_PHY */
TsiChungLiewab77bc52007-08-15 15:39:17 -0500434#endif /* CONFIG_CMD_MII || CONFIG_MII */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500435
436 /* We use strictly polling mode only */
437 fecp->eimr = 0;
438
439 /* Clear any pending interrupt */
440 fecp->eir = 0xffffffff;
441
442 /* Set station address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200443 if ((u32) fecp == CONFIG_SYS_FEC0_IOBASE) {
444#ifdef CONFIG_SYS_FEC1_IOBASE
445 volatile fec_t *fecp1 = (fec_t *) (CONFIG_SYS_FEC1_IOBASE);
Mike Frysingerd3f87142009-02-11 19:01:26 -0500446 eth_getenv_enetaddr("eth1addr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500447 fecp1->palr =
448 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
449 fecp1->paur = (ea[4] << 24) | (ea[5] << 16);
450#endif
Mike Frysingerd3f87142009-02-11 19:01:26 -0500451 eth_getenv_enetaddr("ethaddr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500452 fecp->palr =
453 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
454 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500455 } else {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200456#ifdef CONFIG_SYS_FEC0_IOBASE
457 volatile fec_t *fecp0 = (fec_t *) (CONFIG_SYS_FEC0_IOBASE);
Mike Frysingerd3f87142009-02-11 19:01:26 -0500458 eth_getenv_enetaddr("ethaddr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500459 fecp0->palr =
460 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
461 fecp0->paur = (ea[4] << 24) | (ea[5] << 16);
462#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200463#ifdef CONFIG_SYS_FEC1_IOBASE
Mike Frysingerd3f87142009-02-11 19:01:26 -0500464 eth_getenv_enetaddr("eth1addr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500465 fecp->palr =
466 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
467 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500468#endif
469 }
470
TsiChung Liew8e585f02007-06-18 13:50:13 -0500471 /* Clear unicast address hash table */
472 fecp->iaur = 0;
473 fecp->ialr = 0;
474
475 /* Clear multicast address hash table */
476 fecp->gaur = 0;
477 fecp->galr = 0;
478
479 /* Set maximum receive buffer size. */
480 fecp->emrbr = PKT_MAXBLR_SIZE;
481
482 /*
483 * Setup Buffers and Buffer Desriptors
484 */
485 info->rxIdx = 0;
486 info->txIdx = 0;
487
488 /*
489 * Setup Receiver Buffer Descriptors (13.14.24.18)
490 * Settings:
491 * Empty, Wrap
492 */
493 for (i = 0; i < PKTBUFSRX; i++) {
494 info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
495 info->rxbd[i].cbd_datlen = 0; /* Reset */
496 info->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
497 }
498 info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
499
500 /*
501 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
502 * Settings:
503 * Last, Tx CRC
504 */
505 for (i = 0; i < TX_BUF_CNT; i++) {
506 info->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
507 info->txbd[i].cbd_datlen = 0; /* Reset */
508 info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
509 }
510 info->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
511
512 /* Set receive and transmit descriptor base */
513 fecp->erdsr = (unsigned int)(&info->rxbd[0]);
514 fecp->etdsr = (unsigned int)(&info->txbd[0]);
515
516 /* Now enable the transmit and receive processing */
517 fecp->ecr |= FEC_ECR_ETHER_EN;
518
519 /* And last, try to fill Rx Buffer Descriptors */
520 fecp->rdar = 0x01000000; /* Descriptor polling active */
521
522 return 1;
523}
524
525void fec_reset(struct eth_device *dev)
526{
527 struct fec_info_s *info = dev->priv;
528 volatile fec_t *fecp = (fec_t *) (info->iobase);
529 int i;
530
531 fecp->ecr = FEC_ECR_RESET;
532 for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
533 udelay(1);
534 }
535 if (i == FEC_RESET_DELAY) {
536 printf("FEC_RESET_DELAY timeout\n");
537 }
538}
539
540void fec_halt(struct eth_device *dev)
541{
542 struct fec_info_s *info = dev->priv;
543
544 fec_reset(dev);
545
546 fecpin_setclear(dev, 0);
547
548 info->rxIdx = info->txIdx = 0;
549 memset(info->rxbd, 0, PKTBUFSRX * sizeof(cbd_t));
550 memset(info->txbd, 0, TX_BUF_CNT * sizeof(cbd_t));
551 memset(info->txbuf, 0, DBUF_LENGTH);
552}
553
554int mcffec_initialize(bd_t * bis)
555{
556 struct eth_device *dev;
557 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200558#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
559 u32 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000560#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500561
562 for (i = 0; i < sizeof(fec_info) / sizeof(fec_info[0]); i++) {
563
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500564 dev =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200565 (struct eth_device *)memalign(CONFIG_SYS_CACHELINE_SIZE,
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500566 sizeof *dev);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500567 if (dev == NULL)
568 hang();
569
570 memset(dev, 0, sizeof(*dev));
571
572 sprintf(dev->name, "FEC%d", fec_info[i].index);
573
574 dev->priv = &fec_info[i];
575 dev->init = fec_init;
576 dev->halt = fec_halt;
577 dev->send = fec_send;
578 dev->recv = fec_recv;
579
580 /* setup Receive and Transmit buffer descriptor */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200581#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000582 fec_info[i].rxbd = (cbd_t *)((u32)fec_info[i].rxbd + tmp);
583 tmp = (u32)fec_info[i].rxbd;
584 fec_info[i].txbd =
585 (cbd_t *)((u32)fec_info[i].txbd + tmp +
586 (PKTBUFSRX * sizeof(cbd_t)));
587 tmp = (u32)fec_info[i].txbd;
588 fec_info[i].txbuf =
589 (char *)((u32)fec_info[i].txbuf + tmp +
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200590 (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000591 tmp = (u32)fec_info[i].txbuf;
592#else
TsiChung Liew8e585f02007-06-18 13:50:13 -0500593 fec_info[i].rxbd =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200594 (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500595 (PKTBUFSRX * sizeof(cbd_t)));
TsiChung Liew8e585f02007-06-18 13:50:13 -0500596 fec_info[i].txbd =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200597 (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500598 (TX_BUF_CNT * sizeof(cbd_t)));
599 fec_info[i].txbuf =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200600 (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH);
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000601#endif
602
TsiChung Liew8e585f02007-06-18 13:50:13 -0500603#ifdef ET_DEBUG
604 printf("rxbd %x txbd %x\n",
605 (int)fec_info[i].rxbd, (int)fec_info[i].txbd);
606#endif
607
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200608 fec_info[i].phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500609
610 eth_register(dev);
611
TsiChungLiewab77bc52007-08-15 15:39:17 -0500612#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
TsiChung Liew8e585f02007-06-18 13:50:13 -0500613 miiphy_register(dev->name,
614 mcffec_miiphy_read, mcffec_miiphy_write);
615#endif
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000616 if (i > 0)
617 fec_info[i - 1].next = &fec_info[i];
TsiChung Liew8e585f02007-06-18 13:50:13 -0500618 }
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000619 fec_info[i - 1].next = &fec_info[0];
TsiChung Liew8e585f02007-06-18 13:50:13 -0500620
621 /* default speed */
622 bis->bi_ethspeed = 10;
623
Ben Warren86882b82008-08-26 22:16:25 -0700624 return 0;
TsiChung Liew8e585f02007-06-18 13:50:13 -0500625}