blob: a08ff278bfdbd764bf8f6980f690a0892e66076e [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_send(struct eth_device *dev, volatile void *packet, int length);
99int fec_recv(struct eth_device *dev);
100int fec_init(struct eth_device *dev, bd_t * bd);
101void fec_halt(struct eth_device *dev);
102void fec_reset(struct eth_device *dev);
103
TsiChung Liew8e585f02007-06-18 13:50:13 -0500104void setFecDuplexSpeed(volatile fec_t * fecp, bd_t * bd, int dup_spd)
105{
106 if ((dup_spd >> 16) == FULL) {
107 /* Set maximum frame length */
108 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
109 FEC_RCR_PROM | 0x100;
110 fecp->tcr = FEC_TCR_FDEN;
111 } else {
112 /* Half duplex mode */
113 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
114 FEC_RCR_MII_MODE | FEC_RCR_DRT;
115 fecp->tcr &= ~FEC_TCR_FDEN;
116 }
117
118 if ((dup_spd & 0xFFFF) == _100BASET) {
TsiChung Liewff36fbb2008-05-28 13:06:25 -0500119#ifdef CONFIG_MCF5445x
120 fecp->rcr &= ~0x200; /* disabled 10T base */
121#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500122#ifdef MII_DEBUG
123 printf("100Mbps\n");
124#endif
125 bd->bi_ethspeed = 100;
126 } else {
TsiChung Liewff36fbb2008-05-28 13:06:25 -0500127#ifdef CONFIG_MCF5445x
128 fecp->rcr |= 0x200; /* enabled 10T base */
129#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500130#ifdef MII_DEBUG
131 printf("10Mbps\n");
132#endif
133 bd->bi_ethspeed = 10;
134 }
135}
136
137int fec_send(struct eth_device *dev, volatile void *packet, int length)
138{
139 struct fec_info_s *info = dev->priv;
140 volatile fec_t *fecp = (fec_t *) (info->iobase);
141 int j, rc;
142 u16 phyStatus;
143
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500144 miiphy_read(dev->name, info->phy_addr, MII_BMSR, &phyStatus);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500145
146 /* section 16.9.23.3
147 * Wait for ready
148 */
149 j = 0;
150 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
151 (j < MCFFEC_TOUT_LOOP)) {
152 udelay(1);
153 j++;
154 }
155 if (j >= MCFFEC_TOUT_LOOP) {
156 printf("TX not ready\n");
157 }
TsiChung0dca8742007-07-10 15:45:43 -0500158
TsiChung Liew8e585f02007-06-18 13:50:13 -0500159 info->txbd[info->txIdx].cbd_bufaddr = (uint) packet;
160 info->txbd[info->txIdx].cbd_datlen = length;
161 info->txbd[info->txIdx].cbd_sc |= BD_ENET_TX_RDY_LST;
162
163 /* Activate transmit Buffer Descriptor polling */
164 fecp->tdar = 0x01000000; /* Descriptor polling active */
165
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200166#ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000167 /*
168 * FEC unable to initial transmit data packet.
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500169 * A nop will ensure the descriptor polling active completed.
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000170 * CF Internal RAM has shorter cycle access than DRAM. If use
171 * DRAM as Buffer descriptor and data, a nop is a must.
172 * Affect only V2 and V3.
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500173 */
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500174 __asm__ ("nop");
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000175
Tsi-Chung Liewf6054792008-03-17 17:08:16 -0500176#endif
177
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200178#ifdef CONFIG_SYS_UNIFY_CACHE
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500179 icache_invalid();
180#endif
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000181
TsiChung0dca8742007-07-10 15:45:43 -0500182 j = 0;
TsiChung Liew8e585f02007-06-18 13:50:13 -0500183 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
184 (j < MCFFEC_TOUT_LOOP)) {
185 udelay(1);
186 j++;
187 }
188 if (j >= MCFFEC_TOUT_LOOP) {
189 printf("TX timeout\n");
190 }
TsiChung0dca8742007-07-10 15:45:43 -0500191
TsiChung Liew8e585f02007-06-18 13:50:13 -0500192#ifdef ET_DEBUG
193 printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
194 __FILE__, __LINE__, __FUNCTION__, j,
195 info->txbd[info->txIdx].cbd_sc,
196 (info->txbd[info->txIdx].cbd_sc & 0x003C) >> 2);
197#endif
198
TsiChung0dca8742007-07-10 15:45:43 -0500199 /* return only status bits */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500200 rc = (info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_STATS);
201 info->txIdx = (info->txIdx + 1) % TX_BUF_CNT;
202
203 return rc;
204}
205
206int fec_recv(struct eth_device *dev)
207{
208 struct fec_info_s *info = dev->priv;
209 volatile fec_t *fecp = (fec_t *) (info->iobase);
210 int length;
211
212 for (;;) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200213#ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000214#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200215#ifdef CONFIG_SYS_UNIFY_CACHE
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200216 icache_invalid();
TsiChung0dca8742007-07-10 15:45:43 -0500217#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500218 /* section 16.9.23.2 */
219 if (info->rxbd[info->rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
220 length = -1;
221 break; /* nothing received - leave for() loop */
222 }
223
224 length = info->rxbd[info->rxIdx].cbd_datlen;
225
226 if (info->rxbd[info->rxIdx].cbd_sc & 0x003f) {
227 printf("%s[%d] err: %x\n",
228 __FUNCTION__, __LINE__,
229 info->rxbd[info->rxIdx].cbd_sc);
230#ifdef ET_DEBUG
231 printf("%s[%d] err: %x\n",
232 __FUNCTION__, __LINE__,
233 info->rxbd[info->rxIdx].cbd_sc);
234#endif
235 } else {
236
237 length -= 4;
238 /* Pass the packet up to the protocol layers. */
239 NetReceive(NetRxPackets[info->rxIdx], length);
240
241 fecp->eir |= FEC_EIR_RXF;
242 }
243
244 /* Give the buffer back to the FEC. */
245 info->rxbd[info->rxIdx].cbd_datlen = 0;
246
247 /* wrap around buffer index when necessary */
248 if (info->rxIdx == LAST_PKTBUFSRX) {
249 info->rxbd[PKTBUFSRX - 1].cbd_sc = BD_ENET_RX_W_E;
250 info->rxIdx = 0;
251 } else {
252 info->rxbd[info->rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
253 info->rxIdx++;
254 }
255
256 /* Try to fill Buffer Descriptors */
257 fecp->rdar = 0x01000000; /* Descriptor polling active */
258 }
259
260 return length;
261}
262
TsiChung Liew8e585f02007-06-18 13:50:13 -0500263#ifdef ET_DEBUG
264void dbgFecRegs(struct eth_device *dev)
265{
266 struct fec_info_s *info = dev->priv;
267 volatile fec_t *fecp = (fec_t *) (info->iobase);
268
269 printf("=====\n");
270 printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
271 printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
272 printf("r_des_active %x - %x\n", (int)&fecp->rdar, fecp->rdar);
273 printf("x_des_active %x - %x\n", (int)&fecp->tdar, fecp->tdar);
274 printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
275 printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
276 printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
277 printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
278 printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
279 printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
280 printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
281 printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
282 printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
283 printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
284 printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
285 printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
286 printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
287 printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
288 printf("r_bound %x - %x\n", (int)&fecp->frbr, fecp->frbr);
289 printf("r_fstart %x - %x\n", (int)&fecp->frsr, fecp->frsr);
290 printf("r_drng %x - %x\n", (int)&fecp->erdsr, fecp->erdsr);
291 printf("x_drng %x - %x\n", (int)&fecp->etdsr, fecp->etdsr);
292 printf("r_bufsz %x - %x\n", (int)&fecp->emrbr, fecp->emrbr);
293
294 printf("\n");
295 printf("rmon_t_drop %x - %x\n", (int)&fecp->rmon_t_drop,
296 fecp->rmon_t_drop);
297 printf("rmon_t_packets %x - %x\n", (int)&fecp->rmon_t_packets,
298 fecp->rmon_t_packets);
299 printf("rmon_t_bc_pkt %x - %x\n", (int)&fecp->rmon_t_bc_pkt,
300 fecp->rmon_t_bc_pkt);
301 printf("rmon_t_mc_pkt %x - %x\n", (int)&fecp->rmon_t_mc_pkt,
302 fecp->rmon_t_mc_pkt);
303 printf("rmon_t_crc_align %x - %x\n", (int)&fecp->rmon_t_crc_align,
304 fecp->rmon_t_crc_align);
305 printf("rmon_t_undersize %x - %x\n", (int)&fecp->rmon_t_undersize,
306 fecp->rmon_t_undersize);
307 printf("rmon_t_oversize %x - %x\n", (int)&fecp->rmon_t_oversize,
308 fecp->rmon_t_oversize);
309 printf("rmon_t_frag %x - %x\n", (int)&fecp->rmon_t_frag,
310 fecp->rmon_t_frag);
311 printf("rmon_t_jab %x - %x\n", (int)&fecp->rmon_t_jab,
312 fecp->rmon_t_jab);
313 printf("rmon_t_col %x - %x\n", (int)&fecp->rmon_t_col,
314 fecp->rmon_t_col);
315 printf("rmon_t_p64 %x - %x\n", (int)&fecp->rmon_t_p64,
316 fecp->rmon_t_p64);
317 printf("rmon_t_p65to127 %x - %x\n", (int)&fecp->rmon_t_p65to127,
318 fecp->rmon_t_p65to127);
319 printf("rmon_t_p128to255 %x - %x\n", (int)&fecp->rmon_t_p128to255,
320 fecp->rmon_t_p128to255);
321 printf("rmon_t_p256to511 %x - %x\n", (int)&fecp->rmon_t_p256to511,
322 fecp->rmon_t_p256to511);
323 printf("rmon_t_p512to1023 %x - %x\n", (int)&fecp->rmon_t_p512to1023,
324 fecp->rmon_t_p512to1023);
325 printf("rmon_t_p1024to2047 %x - %x\n", (int)&fecp->rmon_t_p1024to2047,
326 fecp->rmon_t_p1024to2047);
327 printf("rmon_t_p_gte2048 %x - %x\n", (int)&fecp->rmon_t_p_gte2048,
328 fecp->rmon_t_p_gte2048);
329 printf("rmon_t_octets %x - %x\n", (int)&fecp->rmon_t_octets,
330 fecp->rmon_t_octets);
331
332 printf("\n");
333 printf("ieee_t_drop %x - %x\n", (int)&fecp->ieee_t_drop,
334 fecp->ieee_t_drop);
335 printf("ieee_t_frame_ok %x - %x\n", (int)&fecp->ieee_t_frame_ok,
336 fecp->ieee_t_frame_ok);
337 printf("ieee_t_1col %x - %x\n", (int)&fecp->ieee_t_1col,
338 fecp->ieee_t_1col);
339 printf("ieee_t_mcol %x - %x\n", (int)&fecp->ieee_t_mcol,
340 fecp->ieee_t_mcol);
341 printf("ieee_t_def %x - %x\n", (int)&fecp->ieee_t_def,
342 fecp->ieee_t_def);
343 printf("ieee_t_lcol %x - %x\n", (int)&fecp->ieee_t_lcol,
344 fecp->ieee_t_lcol);
345 printf("ieee_t_excol %x - %x\n", (int)&fecp->ieee_t_excol,
346 fecp->ieee_t_excol);
347 printf("ieee_t_macerr %x - %x\n", (int)&fecp->ieee_t_macerr,
348 fecp->ieee_t_macerr);
349 printf("ieee_t_cserr %x - %x\n", (int)&fecp->ieee_t_cserr,
350 fecp->ieee_t_cserr);
351 printf("ieee_t_sqe %x - %x\n", (int)&fecp->ieee_t_sqe,
352 fecp->ieee_t_sqe);
353 printf("ieee_t_fdxfc %x - %x\n", (int)&fecp->ieee_t_fdxfc,
354 fecp->ieee_t_fdxfc);
355 printf("ieee_t_octets_ok %x - %x\n", (int)&fecp->ieee_t_octets_ok,
356 fecp->ieee_t_octets_ok);
357
358 printf("\n");
359 printf("rmon_r_drop %x - %x\n", (int)&fecp->rmon_r_drop,
360 fecp->rmon_r_drop);
361 printf("rmon_r_packets %x - %x\n", (int)&fecp->rmon_r_packets,
362 fecp->rmon_r_packets);
363 printf("rmon_r_bc_pkt %x - %x\n", (int)&fecp->rmon_r_bc_pkt,
364 fecp->rmon_r_bc_pkt);
365 printf("rmon_r_mc_pkt %x - %x\n", (int)&fecp->rmon_r_mc_pkt,
366 fecp->rmon_r_mc_pkt);
367 printf("rmon_r_crc_align %x - %x\n", (int)&fecp->rmon_r_crc_align,
368 fecp->rmon_r_crc_align);
369 printf("rmon_r_undersize %x - %x\n", (int)&fecp->rmon_r_undersize,
370 fecp->rmon_r_undersize);
371 printf("rmon_r_oversize %x - %x\n", (int)&fecp->rmon_r_oversize,
372 fecp->rmon_r_oversize);
373 printf("rmon_r_frag %x - %x\n", (int)&fecp->rmon_r_frag,
374 fecp->rmon_r_frag);
375 printf("rmon_r_jab %x - %x\n", (int)&fecp->rmon_r_jab,
376 fecp->rmon_r_jab);
377 printf("rmon_r_p64 %x - %x\n", (int)&fecp->rmon_r_p64,
378 fecp->rmon_r_p64);
379 printf("rmon_r_p65to127 %x - %x\n", (int)&fecp->rmon_r_p65to127,
380 fecp->rmon_r_p65to127);
381 printf("rmon_r_p128to255 %x - %x\n", (int)&fecp->rmon_r_p128to255,
382 fecp->rmon_r_p128to255);
383 printf("rmon_r_p256to511 %x - %x\n", (int)&fecp->rmon_r_p256to511,
384 fecp->rmon_r_p256to511);
385 printf("rmon_r_p512to1023 %x - %x\n", (int)&fecp->rmon_r_p512to1023,
386 fecp->rmon_r_p512to1023);
387 printf("rmon_r_p1024to2047 %x - %x\n", (int)&fecp->rmon_r_p1024to2047,
388 fecp->rmon_r_p1024to2047);
389 printf("rmon_r_p_gte2048 %x - %x\n", (int)&fecp->rmon_r_p_gte2048,
390 fecp->rmon_r_p_gte2048);
391 printf("rmon_r_octets %x - %x\n", (int)&fecp->rmon_r_octets,
392 fecp->rmon_r_octets);
393
394 printf("\n");
395 printf("ieee_r_drop %x - %x\n", (int)&fecp->ieee_r_drop,
396 fecp->ieee_r_drop);
397 printf("ieee_r_frame_ok %x - %x\n", (int)&fecp->ieee_r_frame_ok,
398 fecp->ieee_r_frame_ok);
399 printf("ieee_r_crc %x - %x\n", (int)&fecp->ieee_r_crc,
400 fecp->ieee_r_crc);
401 printf("ieee_r_align %x - %x\n", (int)&fecp->ieee_r_align,
402 fecp->ieee_r_align);
403 printf("ieee_r_macerr %x - %x\n", (int)&fecp->ieee_r_macerr,
404 fecp->ieee_r_macerr);
405 printf("ieee_r_fdxfc %x - %x\n", (int)&fecp->ieee_r_fdxfc,
406 fecp->ieee_r_fdxfc);
407 printf("ieee_r_octets_ok %x - %x\n", (int)&fecp->ieee_r_octets_ok,
408 fecp->ieee_r_octets_ok);
409
410 printf("\n\n\n");
411}
412#endif
413
414int fec_init(struct eth_device *dev, bd_t * bd)
415{
416 struct fec_info_s *info = dev->priv;
417 volatile fec_t *fecp = (fec_t *) (info->iobase);
418 int i;
Mike Frysingerd3f87142009-02-11 19:01:26 -0500419 uchar ea[6];
TsiChung Liew8e585f02007-06-18 13:50:13 -0500420
421 fecpin_setclear(dev, 1);
422
423 fec_reset(dev);
424
TsiChungLiewab77bc52007-08-15 15:39:17 -0500425#if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200426 defined (CONFIG_SYS_DISCOVER_PHY)
TsiChung Liew8e585f02007-06-18 13:50:13 -0500427
428 mii_init();
429
430 setFecDuplexSpeed(fecp, bd, info->dup_spd);
431#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200432#ifndef CONFIG_SYS_DISCOVER_PHY
TsiChung Liew8e585f02007-06-18 13:50:13 -0500433 setFecDuplexSpeed(fecp, bd, (FECDUPLEX << 16) | FECSPEED);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200434#endif /* ifndef CONFIG_SYS_DISCOVER_PHY */
TsiChungLiewab77bc52007-08-15 15:39:17 -0500435#endif /* CONFIG_CMD_MII || CONFIG_MII */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500436
437 /* We use strictly polling mode only */
438 fecp->eimr = 0;
439
440 /* Clear any pending interrupt */
441 fecp->eir = 0xffffffff;
442
443 /* Set station address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200444 if ((u32) fecp == CONFIG_SYS_FEC0_IOBASE) {
445#ifdef CONFIG_SYS_FEC1_IOBASE
446 volatile fec_t *fecp1 = (fec_t *) (CONFIG_SYS_FEC1_IOBASE);
Mike Frysingerd3f87142009-02-11 19:01:26 -0500447 eth_getenv_enetaddr("eth1addr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500448 fecp1->palr =
449 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
450 fecp1->paur = (ea[4] << 24) | (ea[5] << 16);
451#endif
Mike Frysingerd3f87142009-02-11 19:01:26 -0500452 eth_getenv_enetaddr("ethaddr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500453 fecp->palr =
454 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
455 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500456 } else {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200457#ifdef CONFIG_SYS_FEC0_IOBASE
458 volatile fec_t *fecp0 = (fec_t *) (CONFIG_SYS_FEC0_IOBASE);
Mike Frysingerd3f87142009-02-11 19:01:26 -0500459 eth_getenv_enetaddr("ethaddr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500460 fecp0->palr =
461 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
462 fecp0->paur = (ea[4] << 24) | (ea[5] << 16);
463#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200464#ifdef CONFIG_SYS_FEC1_IOBASE
Mike Frysingerd3f87142009-02-11 19:01:26 -0500465 eth_getenv_enetaddr("eth1addr", ea);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500466 fecp->palr =
467 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
468 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500469#endif
470 }
471
TsiChung Liew8e585f02007-06-18 13:50:13 -0500472 /* Clear unicast address hash table */
473 fecp->iaur = 0;
474 fecp->ialr = 0;
475
476 /* Clear multicast address hash table */
477 fecp->gaur = 0;
478 fecp->galr = 0;
479
480 /* Set maximum receive buffer size. */
481 fecp->emrbr = PKT_MAXBLR_SIZE;
482
483 /*
484 * Setup Buffers and Buffer Desriptors
485 */
486 info->rxIdx = 0;
487 info->txIdx = 0;
488
489 /*
490 * Setup Receiver Buffer Descriptors (13.14.24.18)
491 * Settings:
492 * Empty, Wrap
493 */
494 for (i = 0; i < PKTBUFSRX; i++) {
495 info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
496 info->rxbd[i].cbd_datlen = 0; /* Reset */
497 info->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
498 }
499 info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
500
501 /*
502 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
503 * Settings:
504 * Last, Tx CRC
505 */
506 for (i = 0; i < TX_BUF_CNT; i++) {
507 info->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
508 info->txbd[i].cbd_datlen = 0; /* Reset */
509 info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
510 }
511 info->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
512
513 /* Set receive and transmit descriptor base */
514 fecp->erdsr = (unsigned int)(&info->rxbd[0]);
515 fecp->etdsr = (unsigned int)(&info->txbd[0]);
516
517 /* Now enable the transmit and receive processing */
518 fecp->ecr |= FEC_ECR_ETHER_EN;
519
520 /* And last, try to fill Rx Buffer Descriptors */
521 fecp->rdar = 0x01000000; /* Descriptor polling active */
522
523 return 1;
524}
525
526void fec_reset(struct eth_device *dev)
527{
528 struct fec_info_s *info = dev->priv;
529 volatile fec_t *fecp = (fec_t *) (info->iobase);
530 int i;
531
532 fecp->ecr = FEC_ECR_RESET;
533 for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
534 udelay(1);
535 }
536 if (i == FEC_RESET_DELAY) {
537 printf("FEC_RESET_DELAY timeout\n");
538 }
539}
540
541void fec_halt(struct eth_device *dev)
542{
543 struct fec_info_s *info = dev->priv;
544
545 fec_reset(dev);
546
547 fecpin_setclear(dev, 0);
548
549 info->rxIdx = info->txIdx = 0;
550 memset(info->rxbd, 0, PKTBUFSRX * sizeof(cbd_t));
551 memset(info->txbd, 0, TX_BUF_CNT * sizeof(cbd_t));
552 memset(info->txbuf, 0, DBUF_LENGTH);
553}
554
555int mcffec_initialize(bd_t * bis)
556{
557 struct eth_device *dev;
558 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200559#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
560 u32 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000561#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500562
563 for (i = 0; i < sizeof(fec_info) / sizeof(fec_info[0]); i++) {
564
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500565 dev =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200566 (struct eth_device *)memalign(CONFIG_SYS_CACHELINE_SIZE,
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500567 sizeof *dev);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500568 if (dev == NULL)
569 hang();
570
571 memset(dev, 0, sizeof(*dev));
572
573 sprintf(dev->name, "FEC%d", fec_info[i].index);
574
575 dev->priv = &fec_info[i];
576 dev->init = fec_init;
577 dev->halt = fec_halt;
578 dev->send = fec_send;
579 dev->recv = fec_recv;
580
581 /* setup Receive and Transmit buffer descriptor */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200582#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000583 fec_info[i].rxbd = (cbd_t *)((u32)fec_info[i].rxbd + tmp);
584 tmp = (u32)fec_info[i].rxbd;
585 fec_info[i].txbd =
586 (cbd_t *)((u32)fec_info[i].txbd + tmp +
587 (PKTBUFSRX * sizeof(cbd_t)));
588 tmp = (u32)fec_info[i].txbd;
589 fec_info[i].txbuf =
590 (char *)((u32)fec_info[i].txbuf + tmp +
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200591 (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000592 tmp = (u32)fec_info[i].txbuf;
593#else
TsiChung Liew8e585f02007-06-18 13:50:13 -0500594 fec_info[i].rxbd =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200595 (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500596 (PKTBUFSRX * sizeof(cbd_t)));
TsiChung Liew8e585f02007-06-18 13:50:13 -0500597 fec_info[i].txbd =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200598 (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
TsiChungLiewf2208fb2007-07-05 23:13:58 -0500599 (TX_BUF_CNT * sizeof(cbd_t)));
600 fec_info[i].txbuf =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200601 (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH);
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000602#endif
603
TsiChung Liew8e585f02007-06-18 13:50:13 -0500604#ifdef ET_DEBUG
605 printf("rxbd %x txbd %x\n",
606 (int)fec_info[i].rxbd, (int)fec_info[i].txbd);
607#endif
608
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200609 fec_info[i].phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500610
611 eth_register(dev);
612
TsiChungLiewab77bc52007-08-15 15:39:17 -0500613#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
TsiChung Liew8e585f02007-06-18 13:50:13 -0500614 miiphy_register(dev->name,
615 mcffec_miiphy_read, mcffec_miiphy_write);
616#endif
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000617 if (i > 0)
618 fec_info[i - 1].next = &fec_info[i];
TsiChung Liew8e585f02007-06-18 13:50:13 -0500619 }
TsiChung Liew1803f7f2008-08-19 21:26:32 +0000620 fec_info[i - 1].next = &fec_info[0];
TsiChung Liew8e585f02007-06-18 13:50:13 -0500621
622 /* default speed */
623 bis->bi_ethspeed = 10;
624
Ben Warren86882b82008-08-26 22:16:25 -0700625 return 0;
TsiChung Liew8e585f02007-06-18 13:50:13 -0500626}