1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * (C) Copyright 2000-2004
3*4882a593Smuzhiyun * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * (C) Copyright 2007 Freescale Semiconductor, Inc.
6*4882a593Smuzhiyun * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <common.h>
12*4882a593Smuzhiyun #include <malloc.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <command.h>
15*4882a593Smuzhiyun #include <net.h>
16*4882a593Smuzhiyun #include <netdev.h>
17*4882a593Smuzhiyun #include <miiphy.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <asm/fec.h>
20*4882a593Smuzhiyun #include <asm/immap.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #undef ET_DEBUG
23*4882a593Smuzhiyun #undef MII_DEBUG
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /* Ethernet Transmit and Receive Buffers */
26*4882a593Smuzhiyun #define DBUF_LENGTH 1520
27*4882a593Smuzhiyun #define TX_BUF_CNT 2
28*4882a593Smuzhiyun #define PKT_MAXBUF_SIZE 1518
29*4882a593Smuzhiyun #define PKT_MINBUF_SIZE 64
30*4882a593Smuzhiyun #define PKT_MAXBLR_SIZE 1520
31*4882a593Smuzhiyun #define LAST_PKTBUFSRX PKTBUFSRX - 1
32*4882a593Smuzhiyun #define BD_ENET_RX_W_E (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY)
33*4882a593Smuzhiyun #define BD_ENET_TX_RDY_LST (BD_ENET_TX_READY | BD_ENET_TX_LAST)
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun struct fec_info_s fec_info[] = {
38*4882a593Smuzhiyun #ifdef CONFIG_SYS_FEC0_IOBASE
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 0, /* index */
41*4882a593Smuzhiyun CONFIG_SYS_FEC0_IOBASE, /* io base */
42*4882a593Smuzhiyun CONFIG_SYS_FEC0_PINMUX, /* gpio pin muxing */
43*4882a593Smuzhiyun CONFIG_SYS_FEC0_MIIBASE, /* mii base */
44*4882a593Smuzhiyun -1, /* phy_addr */
45*4882a593Smuzhiyun 0, /* duplex and speed */
46*4882a593Smuzhiyun 0, /* phy name */
47*4882a593Smuzhiyun 0, /* phyname init */
48*4882a593Smuzhiyun 0, /* RX BD */
49*4882a593Smuzhiyun 0, /* TX BD */
50*4882a593Smuzhiyun 0, /* rx Index */
51*4882a593Smuzhiyun 0, /* tx Index */
52*4882a593Smuzhiyun 0, /* tx buffer */
53*4882a593Smuzhiyun 0, /* initialized flag */
54*4882a593Smuzhiyun (struct fec_info_s *)-1,
55*4882a593Smuzhiyun },
56*4882a593Smuzhiyun #endif
57*4882a593Smuzhiyun #ifdef CONFIG_SYS_FEC1_IOBASE
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 1, /* index */
60*4882a593Smuzhiyun CONFIG_SYS_FEC1_IOBASE, /* io base */
61*4882a593Smuzhiyun CONFIG_SYS_FEC1_PINMUX, /* gpio pin muxing */
62*4882a593Smuzhiyun CONFIG_SYS_FEC1_MIIBASE, /* mii base */
63*4882a593Smuzhiyun -1, /* phy_addr */
64*4882a593Smuzhiyun 0, /* duplex and speed */
65*4882a593Smuzhiyun 0, /* phy name */
66*4882a593Smuzhiyun 0, /* phy name init */
67*4882a593Smuzhiyun #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
68*4882a593Smuzhiyun (cbd_t *)DBUF_LENGTH, /* RX BD */
69*4882a593Smuzhiyun #else
70*4882a593Smuzhiyun 0, /* RX BD */
71*4882a593Smuzhiyun #endif
72*4882a593Smuzhiyun 0, /* TX BD */
73*4882a593Smuzhiyun 0, /* rx Index */
74*4882a593Smuzhiyun 0, /* tx Index */
75*4882a593Smuzhiyun 0, /* tx buffer */
76*4882a593Smuzhiyun 0, /* initialized flag */
77*4882a593Smuzhiyun (struct fec_info_s *)-1,
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun #endif
80*4882a593Smuzhiyun };
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun int fec_recv(struct eth_device *dev);
83*4882a593Smuzhiyun int fec_init(struct eth_device *dev, bd_t * bd);
84*4882a593Smuzhiyun void fec_halt(struct eth_device *dev);
85*4882a593Smuzhiyun void fec_reset(struct eth_device *dev);
86*4882a593Smuzhiyun
setFecDuplexSpeed(volatile fec_t * fecp,bd_t * bd,int dup_spd)87*4882a593Smuzhiyun void setFecDuplexSpeed(volatile fec_t * fecp, bd_t * bd, int dup_spd)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun if ((dup_spd >> 16) == FULL) {
90*4882a593Smuzhiyun /* Set maximum frame length */
91*4882a593Smuzhiyun fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
92*4882a593Smuzhiyun FEC_RCR_PROM | 0x100;
93*4882a593Smuzhiyun fecp->tcr = FEC_TCR_FDEN;
94*4882a593Smuzhiyun } else {
95*4882a593Smuzhiyun /* Half duplex mode */
96*4882a593Smuzhiyun fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
97*4882a593Smuzhiyun FEC_RCR_MII_MODE | FEC_RCR_DRT;
98*4882a593Smuzhiyun fecp->tcr &= ~FEC_TCR_FDEN;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun if ((dup_spd & 0xFFFF) == _100BASET) {
102*4882a593Smuzhiyun #ifdef CONFIG_MCF5445x
103*4882a593Smuzhiyun fecp->rcr &= ~0x200; /* disabled 10T base */
104*4882a593Smuzhiyun #endif
105*4882a593Smuzhiyun #ifdef MII_DEBUG
106*4882a593Smuzhiyun printf("100Mbps\n");
107*4882a593Smuzhiyun #endif
108*4882a593Smuzhiyun bd->bi_ethspeed = 100;
109*4882a593Smuzhiyun } else {
110*4882a593Smuzhiyun #ifdef CONFIG_MCF5445x
111*4882a593Smuzhiyun fecp->rcr |= 0x200; /* enabled 10T base */
112*4882a593Smuzhiyun #endif
113*4882a593Smuzhiyun #ifdef MII_DEBUG
114*4882a593Smuzhiyun printf("10Mbps\n");
115*4882a593Smuzhiyun #endif
116*4882a593Smuzhiyun bd->bi_ethspeed = 10;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
fec_send(struct eth_device * dev,void * packet,int length)120*4882a593Smuzhiyun static int fec_send(struct eth_device *dev, void *packet, int length)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun struct fec_info_s *info = dev->priv;
123*4882a593Smuzhiyun volatile fec_t *fecp = (fec_t *) (info->iobase);
124*4882a593Smuzhiyun int j, rc;
125*4882a593Smuzhiyun u16 phyStatus;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun miiphy_read(dev->name, info->phy_addr, MII_BMSR, &phyStatus);
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /* section 16.9.23.3
130*4882a593Smuzhiyun * Wait for ready
131*4882a593Smuzhiyun */
132*4882a593Smuzhiyun j = 0;
133*4882a593Smuzhiyun while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
134*4882a593Smuzhiyun (j < MCFFEC_TOUT_LOOP)) {
135*4882a593Smuzhiyun udelay(1);
136*4882a593Smuzhiyun j++;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun if (j >= MCFFEC_TOUT_LOOP) {
139*4882a593Smuzhiyun printf("TX not ready\n");
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun info->txbd[info->txIdx].cbd_bufaddr = (uint) packet;
143*4882a593Smuzhiyun info->txbd[info->txIdx].cbd_datlen = length;
144*4882a593Smuzhiyun info->txbd[info->txIdx].cbd_sc |= BD_ENET_TX_RDY_LST;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /* Activate transmit Buffer Descriptor polling */
147*4882a593Smuzhiyun fecp->tdar = 0x01000000; /* Descriptor polling active */
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun #ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
150*4882a593Smuzhiyun /*
151*4882a593Smuzhiyun * FEC unable to initial transmit data packet.
152*4882a593Smuzhiyun * A nop will ensure the descriptor polling active completed.
153*4882a593Smuzhiyun * CF Internal RAM has shorter cycle access than DRAM. If use
154*4882a593Smuzhiyun * DRAM as Buffer descriptor and data, a nop is a must.
155*4882a593Smuzhiyun * Affect only V2 and V3.
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun __asm__ ("nop");
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun #endif
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun #ifdef CONFIG_SYS_UNIFY_CACHE
162*4882a593Smuzhiyun icache_invalid();
163*4882a593Smuzhiyun #endif
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun j = 0;
166*4882a593Smuzhiyun while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
167*4882a593Smuzhiyun (j < MCFFEC_TOUT_LOOP)) {
168*4882a593Smuzhiyun udelay(1);
169*4882a593Smuzhiyun j++;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun if (j >= MCFFEC_TOUT_LOOP) {
172*4882a593Smuzhiyun printf("TX timeout\n");
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun #ifdef ET_DEBUG
176*4882a593Smuzhiyun printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
177*4882a593Smuzhiyun __FILE__, __LINE__, __FUNCTION__, j,
178*4882a593Smuzhiyun info->txbd[info->txIdx].cbd_sc,
179*4882a593Smuzhiyun (info->txbd[info->txIdx].cbd_sc & 0x003C) >> 2);
180*4882a593Smuzhiyun #endif
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /* return only status bits */
183*4882a593Smuzhiyun rc = (info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_STATS);
184*4882a593Smuzhiyun info->txIdx = (info->txIdx + 1) % TX_BUF_CNT;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun return rc;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
fec_recv(struct eth_device * dev)189*4882a593Smuzhiyun int fec_recv(struct eth_device *dev)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun struct fec_info_s *info = dev->priv;
192*4882a593Smuzhiyun volatile fec_t *fecp = (fec_t *) (info->iobase);
193*4882a593Smuzhiyun int length;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun for (;;) {
196*4882a593Smuzhiyun #ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
197*4882a593Smuzhiyun #endif
198*4882a593Smuzhiyun #ifdef CONFIG_SYS_UNIFY_CACHE
199*4882a593Smuzhiyun icache_invalid();
200*4882a593Smuzhiyun #endif
201*4882a593Smuzhiyun /* section 16.9.23.2 */
202*4882a593Smuzhiyun if (info->rxbd[info->rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
203*4882a593Smuzhiyun length = -1;
204*4882a593Smuzhiyun break; /* nothing received - leave for() loop */
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun length = info->rxbd[info->rxIdx].cbd_datlen;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (info->rxbd[info->rxIdx].cbd_sc & 0x003f) {
210*4882a593Smuzhiyun printf("%s[%d] err: %x\n",
211*4882a593Smuzhiyun __FUNCTION__, __LINE__,
212*4882a593Smuzhiyun info->rxbd[info->rxIdx].cbd_sc);
213*4882a593Smuzhiyun #ifdef ET_DEBUG
214*4882a593Smuzhiyun printf("%s[%d] err: %x\n",
215*4882a593Smuzhiyun __FUNCTION__, __LINE__,
216*4882a593Smuzhiyun info->rxbd[info->rxIdx].cbd_sc);
217*4882a593Smuzhiyun #endif
218*4882a593Smuzhiyun } else {
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun length -= 4;
221*4882a593Smuzhiyun /* Pass the packet up to the protocol layers. */
222*4882a593Smuzhiyun net_process_received_packet(net_rx_packets[info->rxIdx],
223*4882a593Smuzhiyun length);
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun fecp->eir |= FEC_EIR_RXF;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /* Give the buffer back to the FEC. */
229*4882a593Smuzhiyun info->rxbd[info->rxIdx].cbd_datlen = 0;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /* wrap around buffer index when necessary */
232*4882a593Smuzhiyun if (info->rxIdx == LAST_PKTBUFSRX) {
233*4882a593Smuzhiyun info->rxbd[PKTBUFSRX - 1].cbd_sc = BD_ENET_RX_W_E;
234*4882a593Smuzhiyun info->rxIdx = 0;
235*4882a593Smuzhiyun } else {
236*4882a593Smuzhiyun info->rxbd[info->rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
237*4882a593Smuzhiyun info->rxIdx++;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun /* Try to fill Buffer Descriptors */
241*4882a593Smuzhiyun fecp->rdar = 0x01000000; /* Descriptor polling active */
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun return length;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun #ifdef ET_DEBUG
dbgFecRegs(struct eth_device * dev)248*4882a593Smuzhiyun void dbgFecRegs(struct eth_device *dev)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun struct fec_info_s *info = dev->priv;
251*4882a593Smuzhiyun volatile fec_t *fecp = (fec_t *) (info->iobase);
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun printf("=====\n");
254*4882a593Smuzhiyun printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
255*4882a593Smuzhiyun printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
256*4882a593Smuzhiyun printf("r_des_active %x - %x\n", (int)&fecp->rdar, fecp->rdar);
257*4882a593Smuzhiyun printf("x_des_active %x - %x\n", (int)&fecp->tdar, fecp->tdar);
258*4882a593Smuzhiyun printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
259*4882a593Smuzhiyun printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
260*4882a593Smuzhiyun printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
261*4882a593Smuzhiyun printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
262*4882a593Smuzhiyun printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
263*4882a593Smuzhiyun printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
264*4882a593Smuzhiyun printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
265*4882a593Smuzhiyun printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
266*4882a593Smuzhiyun printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
267*4882a593Smuzhiyun printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
268*4882a593Smuzhiyun printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
269*4882a593Smuzhiyun printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
270*4882a593Smuzhiyun printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
271*4882a593Smuzhiyun printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
272*4882a593Smuzhiyun printf("r_bound %x - %x\n", (int)&fecp->frbr, fecp->frbr);
273*4882a593Smuzhiyun printf("r_fstart %x - %x\n", (int)&fecp->frsr, fecp->frsr);
274*4882a593Smuzhiyun printf("r_drng %x - %x\n", (int)&fecp->erdsr, fecp->erdsr);
275*4882a593Smuzhiyun printf("x_drng %x - %x\n", (int)&fecp->etdsr, fecp->etdsr);
276*4882a593Smuzhiyun printf("r_bufsz %x - %x\n", (int)&fecp->emrbr, fecp->emrbr);
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun printf("\n");
279*4882a593Smuzhiyun printf("rmon_t_drop %x - %x\n", (int)&fecp->rmon_t_drop,
280*4882a593Smuzhiyun fecp->rmon_t_drop);
281*4882a593Smuzhiyun printf("rmon_t_packets %x - %x\n", (int)&fecp->rmon_t_packets,
282*4882a593Smuzhiyun fecp->rmon_t_packets);
283*4882a593Smuzhiyun printf("rmon_t_bc_pkt %x - %x\n", (int)&fecp->rmon_t_bc_pkt,
284*4882a593Smuzhiyun fecp->rmon_t_bc_pkt);
285*4882a593Smuzhiyun printf("rmon_t_mc_pkt %x - %x\n", (int)&fecp->rmon_t_mc_pkt,
286*4882a593Smuzhiyun fecp->rmon_t_mc_pkt);
287*4882a593Smuzhiyun printf("rmon_t_crc_align %x - %x\n", (int)&fecp->rmon_t_crc_align,
288*4882a593Smuzhiyun fecp->rmon_t_crc_align);
289*4882a593Smuzhiyun printf("rmon_t_undersize %x - %x\n", (int)&fecp->rmon_t_undersize,
290*4882a593Smuzhiyun fecp->rmon_t_undersize);
291*4882a593Smuzhiyun printf("rmon_t_oversize %x - %x\n", (int)&fecp->rmon_t_oversize,
292*4882a593Smuzhiyun fecp->rmon_t_oversize);
293*4882a593Smuzhiyun printf("rmon_t_frag %x - %x\n", (int)&fecp->rmon_t_frag,
294*4882a593Smuzhiyun fecp->rmon_t_frag);
295*4882a593Smuzhiyun printf("rmon_t_jab %x - %x\n", (int)&fecp->rmon_t_jab,
296*4882a593Smuzhiyun fecp->rmon_t_jab);
297*4882a593Smuzhiyun printf("rmon_t_col %x - %x\n", (int)&fecp->rmon_t_col,
298*4882a593Smuzhiyun fecp->rmon_t_col);
299*4882a593Smuzhiyun printf("rmon_t_p64 %x - %x\n", (int)&fecp->rmon_t_p64,
300*4882a593Smuzhiyun fecp->rmon_t_p64);
301*4882a593Smuzhiyun printf("rmon_t_p65to127 %x - %x\n", (int)&fecp->rmon_t_p65to127,
302*4882a593Smuzhiyun fecp->rmon_t_p65to127);
303*4882a593Smuzhiyun printf("rmon_t_p128to255 %x - %x\n", (int)&fecp->rmon_t_p128to255,
304*4882a593Smuzhiyun fecp->rmon_t_p128to255);
305*4882a593Smuzhiyun printf("rmon_t_p256to511 %x - %x\n", (int)&fecp->rmon_t_p256to511,
306*4882a593Smuzhiyun fecp->rmon_t_p256to511);
307*4882a593Smuzhiyun printf("rmon_t_p512to1023 %x - %x\n", (int)&fecp->rmon_t_p512to1023,
308*4882a593Smuzhiyun fecp->rmon_t_p512to1023);
309*4882a593Smuzhiyun printf("rmon_t_p1024to2047 %x - %x\n", (int)&fecp->rmon_t_p1024to2047,
310*4882a593Smuzhiyun fecp->rmon_t_p1024to2047);
311*4882a593Smuzhiyun printf("rmon_t_p_gte2048 %x - %x\n", (int)&fecp->rmon_t_p_gte2048,
312*4882a593Smuzhiyun fecp->rmon_t_p_gte2048);
313*4882a593Smuzhiyun printf("rmon_t_octets %x - %x\n", (int)&fecp->rmon_t_octets,
314*4882a593Smuzhiyun fecp->rmon_t_octets);
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun printf("\n");
317*4882a593Smuzhiyun printf("ieee_t_drop %x - %x\n", (int)&fecp->ieee_t_drop,
318*4882a593Smuzhiyun fecp->ieee_t_drop);
319*4882a593Smuzhiyun printf("ieee_t_frame_ok %x - %x\n", (int)&fecp->ieee_t_frame_ok,
320*4882a593Smuzhiyun fecp->ieee_t_frame_ok);
321*4882a593Smuzhiyun printf("ieee_t_1col %x - %x\n", (int)&fecp->ieee_t_1col,
322*4882a593Smuzhiyun fecp->ieee_t_1col);
323*4882a593Smuzhiyun printf("ieee_t_mcol %x - %x\n", (int)&fecp->ieee_t_mcol,
324*4882a593Smuzhiyun fecp->ieee_t_mcol);
325*4882a593Smuzhiyun printf("ieee_t_def %x - %x\n", (int)&fecp->ieee_t_def,
326*4882a593Smuzhiyun fecp->ieee_t_def);
327*4882a593Smuzhiyun printf("ieee_t_lcol %x - %x\n", (int)&fecp->ieee_t_lcol,
328*4882a593Smuzhiyun fecp->ieee_t_lcol);
329*4882a593Smuzhiyun printf("ieee_t_excol %x - %x\n", (int)&fecp->ieee_t_excol,
330*4882a593Smuzhiyun fecp->ieee_t_excol);
331*4882a593Smuzhiyun printf("ieee_t_macerr %x - %x\n", (int)&fecp->ieee_t_macerr,
332*4882a593Smuzhiyun fecp->ieee_t_macerr);
333*4882a593Smuzhiyun printf("ieee_t_cserr %x - %x\n", (int)&fecp->ieee_t_cserr,
334*4882a593Smuzhiyun fecp->ieee_t_cserr);
335*4882a593Smuzhiyun printf("ieee_t_sqe %x - %x\n", (int)&fecp->ieee_t_sqe,
336*4882a593Smuzhiyun fecp->ieee_t_sqe);
337*4882a593Smuzhiyun printf("ieee_t_fdxfc %x - %x\n", (int)&fecp->ieee_t_fdxfc,
338*4882a593Smuzhiyun fecp->ieee_t_fdxfc);
339*4882a593Smuzhiyun printf("ieee_t_octets_ok %x - %x\n", (int)&fecp->ieee_t_octets_ok,
340*4882a593Smuzhiyun fecp->ieee_t_octets_ok);
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun printf("\n");
343*4882a593Smuzhiyun printf("rmon_r_drop %x - %x\n", (int)&fecp->rmon_r_drop,
344*4882a593Smuzhiyun fecp->rmon_r_drop);
345*4882a593Smuzhiyun printf("rmon_r_packets %x - %x\n", (int)&fecp->rmon_r_packets,
346*4882a593Smuzhiyun fecp->rmon_r_packets);
347*4882a593Smuzhiyun printf("rmon_r_bc_pkt %x - %x\n", (int)&fecp->rmon_r_bc_pkt,
348*4882a593Smuzhiyun fecp->rmon_r_bc_pkt);
349*4882a593Smuzhiyun printf("rmon_r_mc_pkt %x - %x\n", (int)&fecp->rmon_r_mc_pkt,
350*4882a593Smuzhiyun fecp->rmon_r_mc_pkt);
351*4882a593Smuzhiyun printf("rmon_r_crc_align %x - %x\n", (int)&fecp->rmon_r_crc_align,
352*4882a593Smuzhiyun fecp->rmon_r_crc_align);
353*4882a593Smuzhiyun printf("rmon_r_undersize %x - %x\n", (int)&fecp->rmon_r_undersize,
354*4882a593Smuzhiyun fecp->rmon_r_undersize);
355*4882a593Smuzhiyun printf("rmon_r_oversize %x - %x\n", (int)&fecp->rmon_r_oversize,
356*4882a593Smuzhiyun fecp->rmon_r_oversize);
357*4882a593Smuzhiyun printf("rmon_r_frag %x - %x\n", (int)&fecp->rmon_r_frag,
358*4882a593Smuzhiyun fecp->rmon_r_frag);
359*4882a593Smuzhiyun printf("rmon_r_jab %x - %x\n", (int)&fecp->rmon_r_jab,
360*4882a593Smuzhiyun fecp->rmon_r_jab);
361*4882a593Smuzhiyun printf("rmon_r_p64 %x - %x\n", (int)&fecp->rmon_r_p64,
362*4882a593Smuzhiyun fecp->rmon_r_p64);
363*4882a593Smuzhiyun printf("rmon_r_p65to127 %x - %x\n", (int)&fecp->rmon_r_p65to127,
364*4882a593Smuzhiyun fecp->rmon_r_p65to127);
365*4882a593Smuzhiyun printf("rmon_r_p128to255 %x - %x\n", (int)&fecp->rmon_r_p128to255,
366*4882a593Smuzhiyun fecp->rmon_r_p128to255);
367*4882a593Smuzhiyun printf("rmon_r_p256to511 %x - %x\n", (int)&fecp->rmon_r_p256to511,
368*4882a593Smuzhiyun fecp->rmon_r_p256to511);
369*4882a593Smuzhiyun printf("rmon_r_p512to1023 %x - %x\n", (int)&fecp->rmon_r_p512to1023,
370*4882a593Smuzhiyun fecp->rmon_r_p512to1023);
371*4882a593Smuzhiyun printf("rmon_r_p1024to2047 %x - %x\n", (int)&fecp->rmon_r_p1024to2047,
372*4882a593Smuzhiyun fecp->rmon_r_p1024to2047);
373*4882a593Smuzhiyun printf("rmon_r_p_gte2048 %x - %x\n", (int)&fecp->rmon_r_p_gte2048,
374*4882a593Smuzhiyun fecp->rmon_r_p_gte2048);
375*4882a593Smuzhiyun printf("rmon_r_octets %x - %x\n", (int)&fecp->rmon_r_octets,
376*4882a593Smuzhiyun fecp->rmon_r_octets);
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun printf("\n");
379*4882a593Smuzhiyun printf("ieee_r_drop %x - %x\n", (int)&fecp->ieee_r_drop,
380*4882a593Smuzhiyun fecp->ieee_r_drop);
381*4882a593Smuzhiyun printf("ieee_r_frame_ok %x - %x\n", (int)&fecp->ieee_r_frame_ok,
382*4882a593Smuzhiyun fecp->ieee_r_frame_ok);
383*4882a593Smuzhiyun printf("ieee_r_crc %x - %x\n", (int)&fecp->ieee_r_crc,
384*4882a593Smuzhiyun fecp->ieee_r_crc);
385*4882a593Smuzhiyun printf("ieee_r_align %x - %x\n", (int)&fecp->ieee_r_align,
386*4882a593Smuzhiyun fecp->ieee_r_align);
387*4882a593Smuzhiyun printf("ieee_r_macerr %x - %x\n", (int)&fecp->ieee_r_macerr,
388*4882a593Smuzhiyun fecp->ieee_r_macerr);
389*4882a593Smuzhiyun printf("ieee_r_fdxfc %x - %x\n", (int)&fecp->ieee_r_fdxfc,
390*4882a593Smuzhiyun fecp->ieee_r_fdxfc);
391*4882a593Smuzhiyun printf("ieee_r_octets_ok %x - %x\n", (int)&fecp->ieee_r_octets_ok,
392*4882a593Smuzhiyun fecp->ieee_r_octets_ok);
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun printf("\n\n\n");
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun #endif
397*4882a593Smuzhiyun
fec_init(struct eth_device * dev,bd_t * bd)398*4882a593Smuzhiyun int fec_init(struct eth_device *dev, bd_t * bd)
399*4882a593Smuzhiyun {
400*4882a593Smuzhiyun struct fec_info_s *info = dev->priv;
401*4882a593Smuzhiyun volatile fec_t *fecp = (fec_t *) (info->iobase);
402*4882a593Smuzhiyun int i;
403*4882a593Smuzhiyun uchar ea[6];
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun fecpin_setclear(dev, 1);
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun fec_reset(dev);
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun #if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
410*4882a593Smuzhiyun defined (CONFIG_SYS_DISCOVER_PHY)
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun mii_init();
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun setFecDuplexSpeed(fecp, bd, info->dup_spd);
415*4882a593Smuzhiyun #else
416*4882a593Smuzhiyun #ifndef CONFIG_SYS_DISCOVER_PHY
417*4882a593Smuzhiyun setFecDuplexSpeed(fecp, bd, (FECDUPLEX << 16) | FECSPEED);
418*4882a593Smuzhiyun #endif /* ifndef CONFIG_SYS_DISCOVER_PHY */
419*4882a593Smuzhiyun #endif /* CONFIG_CMD_MII || CONFIG_MII */
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun /* We use strictly polling mode only */
422*4882a593Smuzhiyun fecp->eimr = 0;
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun /* Clear any pending interrupt */
425*4882a593Smuzhiyun fecp->eir = 0xffffffff;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun /* Set station address */
428*4882a593Smuzhiyun if ((u32) fecp == CONFIG_SYS_FEC0_IOBASE) {
429*4882a593Smuzhiyun #ifdef CONFIG_SYS_FEC1_IOBASE
430*4882a593Smuzhiyun volatile fec_t *fecp1 = (fec_t *) (CONFIG_SYS_FEC1_IOBASE);
431*4882a593Smuzhiyun eth_env_get_enetaddr("eth1addr", ea);
432*4882a593Smuzhiyun fecp1->palr =
433*4882a593Smuzhiyun (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
434*4882a593Smuzhiyun fecp1->paur = (ea[4] << 24) | (ea[5] << 16);
435*4882a593Smuzhiyun #endif
436*4882a593Smuzhiyun eth_env_get_enetaddr("ethaddr", ea);
437*4882a593Smuzhiyun fecp->palr =
438*4882a593Smuzhiyun (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
439*4882a593Smuzhiyun fecp->paur = (ea[4] << 24) | (ea[5] << 16);
440*4882a593Smuzhiyun } else {
441*4882a593Smuzhiyun #ifdef CONFIG_SYS_FEC0_IOBASE
442*4882a593Smuzhiyun volatile fec_t *fecp0 = (fec_t *) (CONFIG_SYS_FEC0_IOBASE);
443*4882a593Smuzhiyun eth_env_get_enetaddr("ethaddr", ea);
444*4882a593Smuzhiyun fecp0->palr =
445*4882a593Smuzhiyun (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
446*4882a593Smuzhiyun fecp0->paur = (ea[4] << 24) | (ea[5] << 16);
447*4882a593Smuzhiyun #endif
448*4882a593Smuzhiyun #ifdef CONFIG_SYS_FEC1_IOBASE
449*4882a593Smuzhiyun eth_env_get_enetaddr("eth1addr", ea);
450*4882a593Smuzhiyun fecp->palr =
451*4882a593Smuzhiyun (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
452*4882a593Smuzhiyun fecp->paur = (ea[4] << 24) | (ea[5] << 16);
453*4882a593Smuzhiyun #endif
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun /* Clear unicast address hash table */
457*4882a593Smuzhiyun fecp->iaur = 0;
458*4882a593Smuzhiyun fecp->ialr = 0;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun /* Clear multicast address hash table */
461*4882a593Smuzhiyun fecp->gaur = 0;
462*4882a593Smuzhiyun fecp->galr = 0;
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun /* Set maximum receive buffer size. */
465*4882a593Smuzhiyun fecp->emrbr = PKT_MAXBLR_SIZE;
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun /*
468*4882a593Smuzhiyun * Setup Buffers and Buffer Descriptors
469*4882a593Smuzhiyun */
470*4882a593Smuzhiyun info->rxIdx = 0;
471*4882a593Smuzhiyun info->txIdx = 0;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /*
474*4882a593Smuzhiyun * Setup Receiver Buffer Descriptors (13.14.24.18)
475*4882a593Smuzhiyun * Settings:
476*4882a593Smuzhiyun * Empty, Wrap
477*4882a593Smuzhiyun */
478*4882a593Smuzhiyun for (i = 0; i < PKTBUFSRX; i++) {
479*4882a593Smuzhiyun info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
480*4882a593Smuzhiyun info->rxbd[i].cbd_datlen = 0; /* Reset */
481*4882a593Smuzhiyun info->rxbd[i].cbd_bufaddr = (uint) net_rx_packets[i];
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun /*
486*4882a593Smuzhiyun * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
487*4882a593Smuzhiyun * Settings:
488*4882a593Smuzhiyun * Last, Tx CRC
489*4882a593Smuzhiyun */
490*4882a593Smuzhiyun for (i = 0; i < TX_BUF_CNT; i++) {
491*4882a593Smuzhiyun info->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
492*4882a593Smuzhiyun info->txbd[i].cbd_datlen = 0; /* Reset */
493*4882a593Smuzhiyun info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun info->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun /* Set receive and transmit descriptor base */
498*4882a593Smuzhiyun fecp->erdsr = (unsigned int)(&info->rxbd[0]);
499*4882a593Smuzhiyun fecp->etdsr = (unsigned int)(&info->txbd[0]);
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun /* Now enable the transmit and receive processing */
502*4882a593Smuzhiyun fecp->ecr |= FEC_ECR_ETHER_EN;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun /* And last, try to fill Rx Buffer Descriptors */
505*4882a593Smuzhiyun fecp->rdar = 0x01000000; /* Descriptor polling active */
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun return 1;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun
fec_reset(struct eth_device * dev)510*4882a593Smuzhiyun void fec_reset(struct eth_device *dev)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun struct fec_info_s *info = dev->priv;
513*4882a593Smuzhiyun volatile fec_t *fecp = (fec_t *) (info->iobase);
514*4882a593Smuzhiyun int i;
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun fecp->ecr = FEC_ECR_RESET;
517*4882a593Smuzhiyun for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
518*4882a593Smuzhiyun udelay(1);
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun if (i == FEC_RESET_DELAY) {
521*4882a593Smuzhiyun printf("FEC_RESET_DELAY timeout\n");
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun
fec_halt(struct eth_device * dev)525*4882a593Smuzhiyun void fec_halt(struct eth_device *dev)
526*4882a593Smuzhiyun {
527*4882a593Smuzhiyun struct fec_info_s *info = dev->priv;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun fec_reset(dev);
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun fecpin_setclear(dev, 0);
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun info->rxIdx = info->txIdx = 0;
534*4882a593Smuzhiyun memset(info->rxbd, 0, PKTBUFSRX * sizeof(cbd_t));
535*4882a593Smuzhiyun memset(info->txbd, 0, TX_BUF_CNT * sizeof(cbd_t));
536*4882a593Smuzhiyun memset(info->txbuf, 0, DBUF_LENGTH);
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun
mcffec_initialize(bd_t * bis)539*4882a593Smuzhiyun int mcffec_initialize(bd_t * bis)
540*4882a593Smuzhiyun {
541*4882a593Smuzhiyun struct eth_device *dev;
542*4882a593Smuzhiyun int i;
543*4882a593Smuzhiyun #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
544*4882a593Smuzhiyun u32 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
545*4882a593Smuzhiyun #endif
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(fec_info); i++) {
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun dev =
550*4882a593Smuzhiyun (struct eth_device *)memalign(CONFIG_SYS_CACHELINE_SIZE,
551*4882a593Smuzhiyun sizeof *dev);
552*4882a593Smuzhiyun if (dev == NULL)
553*4882a593Smuzhiyun hang();
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun memset(dev, 0, sizeof(*dev));
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun sprintf(dev->name, "FEC%d", fec_info[i].index);
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun dev->priv = &fec_info[i];
560*4882a593Smuzhiyun dev->init = fec_init;
561*4882a593Smuzhiyun dev->halt = fec_halt;
562*4882a593Smuzhiyun dev->send = fec_send;
563*4882a593Smuzhiyun dev->recv = fec_recv;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun /* setup Receive and Transmit buffer descriptor */
566*4882a593Smuzhiyun #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
567*4882a593Smuzhiyun fec_info[i].rxbd = (cbd_t *)((u32)fec_info[i].rxbd + tmp);
568*4882a593Smuzhiyun tmp = (u32)fec_info[i].rxbd;
569*4882a593Smuzhiyun fec_info[i].txbd =
570*4882a593Smuzhiyun (cbd_t *)((u32)fec_info[i].txbd + tmp +
571*4882a593Smuzhiyun (PKTBUFSRX * sizeof(cbd_t)));
572*4882a593Smuzhiyun tmp = (u32)fec_info[i].txbd;
573*4882a593Smuzhiyun fec_info[i].txbuf =
574*4882a593Smuzhiyun (char *)((u32)fec_info[i].txbuf + tmp +
575*4882a593Smuzhiyun (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
576*4882a593Smuzhiyun tmp = (u32)fec_info[i].txbuf;
577*4882a593Smuzhiyun #else
578*4882a593Smuzhiyun fec_info[i].rxbd =
579*4882a593Smuzhiyun (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
580*4882a593Smuzhiyun (PKTBUFSRX * sizeof(cbd_t)));
581*4882a593Smuzhiyun fec_info[i].txbd =
582*4882a593Smuzhiyun (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
583*4882a593Smuzhiyun (TX_BUF_CNT * sizeof(cbd_t)));
584*4882a593Smuzhiyun fec_info[i].txbuf =
585*4882a593Smuzhiyun (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH);
586*4882a593Smuzhiyun #endif
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun #ifdef ET_DEBUG
589*4882a593Smuzhiyun printf("rxbd %x txbd %x\n",
590*4882a593Smuzhiyun (int)fec_info[i].rxbd, (int)fec_info[i].txbd);
591*4882a593Smuzhiyun #endif
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun fec_info[i].phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32);
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun eth_register(dev);
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
598*4882a593Smuzhiyun int retval;
599*4882a593Smuzhiyun struct mii_dev *mdiodev = mdio_alloc();
600*4882a593Smuzhiyun if (!mdiodev)
601*4882a593Smuzhiyun return -ENOMEM;
602*4882a593Smuzhiyun strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
603*4882a593Smuzhiyun mdiodev->read = mcffec_miiphy_read;
604*4882a593Smuzhiyun mdiodev->write = mcffec_miiphy_write;
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun retval = mdio_register(mdiodev);
607*4882a593Smuzhiyun if (retval < 0)
608*4882a593Smuzhiyun return retval;
609*4882a593Smuzhiyun #endif
610*4882a593Smuzhiyun if (i > 0)
611*4882a593Smuzhiyun fec_info[i - 1].next = &fec_info[i];
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun fec_info[i - 1].next = &fec_info[0];
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun /* default speed */
616*4882a593Smuzhiyun bis->bi_ethspeed = 10;
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun return 0;
619*4882a593Smuzhiyun }
620