xref: /openbmc/u-boot/drivers/net/fec_mxc.c (revision 909ea9aa)
1 /*
2  * (C) Copyright 2009 Ilya Yanok, Emcraft Systems Ltd <yanok@emcraft.com>
3  * (C) Copyright 2008,2009 Eric Jarrige <eric.jarrige@armadeus.org>
4  * (C) Copyright 2008 Armadeus Systems nc
5  * (C) Copyright 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
6  * (C) Copyright 2007 Pengutronix, Juergen Beisert <j.beisert@pengutronix.de>
7  *
8  * SPDX-License-Identifier:	GPL-2.0+
9  */
10 
11 #include <common.h>
12 #include <malloc.h>
13 #include <net.h>
14 #include <miiphy.h>
15 #include "fec_mxc.h"
16 
17 #include <asm/arch/clock.h>
18 #include <asm/arch/imx-regs.h>
19 #include <asm/io.h>
20 #include <asm/errno.h>
21 #include <linux/compiler.h>
22 
23 DECLARE_GLOBAL_DATA_PTR;
24 
25 /*
26  * Timeout the transfer after 5 mS. This is usually a bit more, since
27  * the code in the tightloops this timeout is used in adds some overhead.
28  */
29 #define FEC_XFER_TIMEOUT	5000
30 
31 #ifndef CONFIG_MII
32 #error "CONFIG_MII has to be defined!"
33 #endif
34 
35 #ifndef CONFIG_FEC_XCV_TYPE
36 #define CONFIG_FEC_XCV_TYPE MII100
37 #endif
38 
39 /*
40  * The i.MX28 operates with packets in big endian. We need to swap them before
41  * sending and after receiving.
42  */
43 #ifdef CONFIG_MX28
44 #define CONFIG_FEC_MXC_SWAP_PACKET
45 #endif
46 
47 #define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
48 
49 /* Check various alignment issues at compile time */
50 #if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
51 #error "ARCH_DMA_MINALIGN must be multiple of 16!"
52 #endif
53 
54 #if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
55 	(PKTALIGN % ARCH_DMA_MINALIGN != 0))
56 #error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
57 #endif
58 
59 #undef DEBUG
60 
61 struct nbuf {
62 	uint8_t data[1500];	/**< actual data */
63 	int length;		/**< actual length */
64 	int used;		/**< buffer in use or not */
65 	uint8_t head[16];	/**< MAC header(6 + 6 + 2) + 2(aligned) */
66 };
67 
68 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
69 static void swap_packet(uint32_t *packet, int length)
70 {
71 	int i;
72 
73 	for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
74 		packet[i] = __swab32(packet[i]);
75 }
76 #endif
77 
78 /*
79  * MII-interface related functions
80  */
81 static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
82 		uint8_t regAddr)
83 {
84 	uint32_t reg;		/* convenient holder for the PHY register */
85 	uint32_t phy;		/* convenient holder for the PHY */
86 	uint32_t start;
87 	int val;
88 
89 	/*
90 	 * reading from any PHY's register is done by properly
91 	 * programming the FEC's MII data register.
92 	 */
93 	writel(FEC_IEVENT_MII, &eth->ievent);
94 	reg = regAddr << FEC_MII_DATA_RA_SHIFT;
95 	phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
96 
97 	writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
98 			phy | reg, &eth->mii_data);
99 
100 	/*
101 	 * wait for the related interrupt
102 	 */
103 	start = get_timer(0);
104 	while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
105 		if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
106 			printf("Read MDIO failed...\n");
107 			return -1;
108 		}
109 	}
110 
111 	/*
112 	 * clear mii interrupt bit
113 	 */
114 	writel(FEC_IEVENT_MII, &eth->ievent);
115 
116 	/*
117 	 * it's now safe to read the PHY's register
118 	 */
119 	val = (unsigned short)readl(&eth->mii_data);
120 	debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
121 			regAddr, val);
122 	return val;
123 }
124 
125 static void fec_mii_setspeed(struct ethernet_regs *eth)
126 {
127 	/*
128 	 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
129 	 * and do not drop the Preamble.
130 	 */
131 	register u32 speed = DIV_ROUND_UP(imx_get_fecclk(), 5000000);
132 #ifdef FEC_QUIRK_ENET_MAC
133 	speed--;
134 #endif
135 	speed <<= 1;
136 	writel(speed, &eth->mii_speed);
137 	debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
138 }
139 
140 static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
141 		uint8_t regAddr, uint16_t data)
142 {
143 	uint32_t reg;		/* convenient holder for the PHY register */
144 	uint32_t phy;		/* convenient holder for the PHY */
145 	uint32_t start;
146 
147 	reg = regAddr << FEC_MII_DATA_RA_SHIFT;
148 	phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
149 
150 	writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
151 		FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
152 
153 	/*
154 	 * wait for the MII interrupt
155 	 */
156 	start = get_timer(0);
157 	while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
158 		if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
159 			printf("Write MDIO failed...\n");
160 			return -1;
161 		}
162 	}
163 
164 	/*
165 	 * clear MII interrupt bit
166 	 */
167 	writel(FEC_IEVENT_MII, &eth->ievent);
168 	debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
169 			regAddr, data);
170 
171 	return 0;
172 }
173 
174 int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
175 {
176 	return fec_mdio_read(bus->priv, phyAddr, regAddr);
177 }
178 
179 int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
180 		u16 data)
181 {
182 	return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
183 }
184 
185 #ifndef CONFIG_PHYLIB
186 static int miiphy_restart_aneg(struct eth_device *dev)
187 {
188 	int ret = 0;
189 #if !defined(CONFIG_FEC_MXC_NO_ANEG)
190 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
191 	struct ethernet_regs *eth = fec->bus->priv;
192 
193 	/*
194 	 * Wake up from sleep if necessary
195 	 * Reset PHY, then delay 300ns
196 	 */
197 #ifdef CONFIG_MX27
198 	fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
199 #endif
200 	fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
201 	udelay(1000);
202 
203 	/*
204 	 * Set the auto-negotiation advertisement register bits
205 	 */
206 	fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
207 			LPA_100FULL | LPA_100HALF | LPA_10FULL |
208 			LPA_10HALF | PHY_ANLPAR_PSB_802_3);
209 	fec_mdio_write(eth, fec->phy_id, MII_BMCR,
210 			BMCR_ANENABLE | BMCR_ANRESTART);
211 
212 	if (fec->mii_postcall)
213 		ret = fec->mii_postcall(fec->phy_id);
214 
215 #endif
216 	return ret;
217 }
218 
219 static int miiphy_wait_aneg(struct eth_device *dev)
220 {
221 	uint32_t start;
222 	int status;
223 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
224 	struct ethernet_regs *eth = fec->bus->priv;
225 
226 	/*
227 	 * Wait for AN completion
228 	 */
229 	start = get_timer(0);
230 	do {
231 		if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
232 			printf("%s: Autonegotiation timeout\n", dev->name);
233 			return -1;
234 		}
235 
236 		status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
237 		if (status < 0) {
238 			printf("%s: Autonegotiation failed. status: %d\n",
239 					dev->name, status);
240 			return -1;
241 		}
242 	} while (!(status & BMSR_LSTATUS));
243 
244 	return 0;
245 }
246 #endif
247 
248 static int fec_rx_task_enable(struct fec_priv *fec)
249 {
250 	writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
251 	return 0;
252 }
253 
254 static int fec_rx_task_disable(struct fec_priv *fec)
255 {
256 	return 0;
257 }
258 
259 static int fec_tx_task_enable(struct fec_priv *fec)
260 {
261 	writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
262 	return 0;
263 }
264 
265 static int fec_tx_task_disable(struct fec_priv *fec)
266 {
267 	return 0;
268 }
269 
270 /**
271  * Initialize receive task's buffer descriptors
272  * @param[in] fec all we know about the device yet
273  * @param[in] count receive buffer count to be allocated
274  * @param[in] dsize desired size of each receive buffer
275  * @return 0 on success
276  *
277  * Init all RX descriptors to default values.
278  */
279 static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
280 {
281 	uint32_t size;
282 	uint8_t *data;
283 	int i;
284 
285 	/*
286 	 * Reload the RX descriptors with default values and wipe
287 	 * the RX buffers.
288 	 */
289 	size = roundup(dsize, ARCH_DMA_MINALIGN);
290 	for (i = 0; i < count; i++) {
291 		data = (uint8_t *)fec->rbd_base[i].data_pointer;
292 		memset(data, 0, dsize);
293 		flush_dcache_range((uint32_t)data, (uint32_t)data + size);
294 
295 		fec->rbd_base[i].status = FEC_RBD_EMPTY;
296 		fec->rbd_base[i].data_length = 0;
297 	}
298 
299 	/* Mark the last RBD to close the ring. */
300 	fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
301 	fec->rbd_index = 0;
302 
303 	flush_dcache_range((unsigned)fec->rbd_base,
304 			   (unsigned)fec->rbd_base + size);
305 }
306 
307 /**
308  * Initialize transmit task's buffer descriptors
309  * @param[in] fec all we know about the device yet
310  *
311  * Transmit buffers are created externally. We only have to init the BDs here.\n
312  * Note: There is a race condition in the hardware. When only one BD is in
313  * use it must be marked with the WRAP bit to use it for every transmitt.
314  * This bit in combination with the READY bit results into double transmit
315  * of each data buffer. It seems the state machine checks READY earlier then
316  * resetting it after the first transfer.
317  * Using two BDs solves this issue.
318  */
319 static void fec_tbd_init(struct fec_priv *fec)
320 {
321 	unsigned addr = (unsigned)fec->tbd_base;
322 	unsigned size = roundup(2 * sizeof(struct fec_bd),
323 				ARCH_DMA_MINALIGN);
324 
325 	memset(fec->tbd_base, 0, size);
326 	fec->tbd_base[0].status = 0;
327 	fec->tbd_base[1].status = FEC_TBD_WRAP;
328 	fec->tbd_index = 0;
329 	flush_dcache_range(addr, addr + size);
330 }
331 
332 /**
333  * Mark the given read buffer descriptor as free
334  * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
335  * @param[in] pRbd buffer descriptor to mark free again
336  */
337 static void fec_rbd_clean(int last, struct fec_bd *pRbd)
338 {
339 	unsigned short flags = FEC_RBD_EMPTY;
340 	if (last)
341 		flags |= FEC_RBD_WRAP;
342 	writew(flags, &pRbd->status);
343 	writew(0, &pRbd->data_length);
344 }
345 
346 static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
347 						unsigned char *mac)
348 {
349 	imx_get_mac_from_fuse(dev_id, mac);
350 	return !is_valid_ether_addr(mac);
351 }
352 
353 static int fec_set_hwaddr(struct eth_device *dev)
354 {
355 	uchar *mac = dev->enetaddr;
356 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
357 
358 	writel(0, &fec->eth->iaddr1);
359 	writel(0, &fec->eth->iaddr2);
360 	writel(0, &fec->eth->gaddr1);
361 	writel(0, &fec->eth->gaddr2);
362 
363 	/*
364 	 * Set physical address
365 	 */
366 	writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
367 			&fec->eth->paddr1);
368 	writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
369 
370 	return 0;
371 }
372 
373 /*
374  * Do initial configuration of the FEC registers
375  */
376 static void fec_reg_setup(struct fec_priv *fec)
377 {
378 	uint32_t rcntrl;
379 
380 	/*
381 	 * Set interrupt mask register
382 	 */
383 	writel(0x00000000, &fec->eth->imask);
384 
385 	/*
386 	 * Clear FEC-Lite interrupt event register(IEVENT)
387 	 */
388 	writel(0xffffffff, &fec->eth->ievent);
389 
390 
391 	/*
392 	 * Set FEC-Lite receive control register(R_CNTRL):
393 	 */
394 
395 	/* Start with frame length = 1518, common for all modes. */
396 	rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
397 	if (fec->xcv_type != SEVENWIRE)		/* xMII modes */
398 		rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
399 	if (fec->xcv_type == RGMII)
400 		rcntrl |= FEC_RCNTRL_RGMII;
401 	else if (fec->xcv_type == RMII)
402 		rcntrl |= FEC_RCNTRL_RMII;
403 
404 	writel(rcntrl, &fec->eth->r_cntrl);
405 }
406 
407 /**
408  * Start the FEC engine
409  * @param[in] dev Our device to handle
410  */
411 static int fec_open(struct eth_device *edev)
412 {
413 	struct fec_priv *fec = (struct fec_priv *)edev->priv;
414 	int speed;
415 	uint32_t addr, size;
416 	int i;
417 
418 	debug("fec_open: fec_open(dev)\n");
419 	/* full-duplex, heartbeat disabled */
420 	writel(1 << 2, &fec->eth->x_cntrl);
421 	fec->rbd_index = 0;
422 
423 	/* Invalidate all descriptors */
424 	for (i = 0; i < FEC_RBD_NUM - 1; i++)
425 		fec_rbd_clean(0, &fec->rbd_base[i]);
426 	fec_rbd_clean(1, &fec->rbd_base[i]);
427 
428 	/* Flush the descriptors into RAM */
429 	size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
430 			ARCH_DMA_MINALIGN);
431 	addr = (uint32_t)fec->rbd_base;
432 	flush_dcache_range(addr, addr + size);
433 
434 #ifdef FEC_QUIRK_ENET_MAC
435 	/* Enable ENET HW endian SWAP */
436 	writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
437 		&fec->eth->ecntrl);
438 	/* Enable ENET store and forward mode */
439 	writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
440 		&fec->eth->x_wmrk);
441 #endif
442 	/*
443 	 * Enable FEC-Lite controller
444 	 */
445 	writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
446 		&fec->eth->ecntrl);
447 #if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
448 	udelay(100);
449 	/*
450 	 * setup the MII gasket for RMII mode
451 	 */
452 
453 	/* disable the gasket */
454 	writew(0, &fec->eth->miigsk_enr);
455 
456 	/* wait for the gasket to be disabled */
457 	while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
458 		udelay(2);
459 
460 	/* configure gasket for RMII, 50 MHz, no loopback, and no echo */
461 	writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
462 
463 	/* re-enable the gasket */
464 	writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
465 
466 	/* wait until MII gasket is ready */
467 	int max_loops = 10;
468 	while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
469 		if (--max_loops <= 0) {
470 			printf("WAIT for MII Gasket ready timed out\n");
471 			break;
472 		}
473 	}
474 #endif
475 
476 #ifdef CONFIG_PHYLIB
477 	{
478 		/* Start up the PHY */
479 		int ret = phy_startup(fec->phydev);
480 
481 		if (ret) {
482 			printf("Could not initialize PHY %s\n",
483 			       fec->phydev->dev->name);
484 			return ret;
485 		}
486 		speed = fec->phydev->speed;
487 	}
488 #else
489 	miiphy_wait_aneg(edev);
490 	speed = miiphy_speed(edev->name, fec->phy_id);
491 	miiphy_duplex(edev->name, fec->phy_id);
492 #endif
493 
494 #ifdef FEC_QUIRK_ENET_MAC
495 	{
496 		u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
497 		u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
498 		if (speed == _1000BASET)
499 			ecr |= FEC_ECNTRL_SPEED;
500 		else if (speed != _100BASET)
501 			rcr |= FEC_RCNTRL_RMII_10T;
502 		writel(ecr, &fec->eth->ecntrl);
503 		writel(rcr, &fec->eth->r_cntrl);
504 	}
505 #endif
506 	debug("%s:Speed=%i\n", __func__, speed);
507 
508 	/*
509 	 * Enable SmartDMA receive task
510 	 */
511 	fec_rx_task_enable(fec);
512 
513 	udelay(100000);
514 	return 0;
515 }
516 
517 static int fec_init(struct eth_device *dev, bd_t* bd)
518 {
519 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
520 	uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
521 	int i;
522 
523 	/* Initialize MAC address */
524 	fec_set_hwaddr(dev);
525 
526 	/*
527 	 * Setup transmit descriptors, there are two in total.
528 	 */
529 	fec_tbd_init(fec);
530 
531 	/* Setup receive descriptors. */
532 	fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
533 
534 	fec_reg_setup(fec);
535 
536 	if (fec->xcv_type != SEVENWIRE)
537 		fec_mii_setspeed(fec->bus->priv);
538 
539 	/*
540 	 * Set Opcode/Pause Duration Register
541 	 */
542 	writel(0x00010020, &fec->eth->op_pause);	/* FIXME 0xffff0020; */
543 	writel(0x2, &fec->eth->x_wmrk);
544 	/*
545 	 * Set multicast address filter
546 	 */
547 	writel(0x00000000, &fec->eth->gaddr1);
548 	writel(0x00000000, &fec->eth->gaddr2);
549 
550 
551 	/* clear MIB RAM */
552 	for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
553 		writel(0, i);
554 
555 	/* FIFO receive start register */
556 	writel(0x520, &fec->eth->r_fstart);
557 
558 	/* size and address of each buffer */
559 	writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
560 	writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
561 	writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
562 
563 #ifndef CONFIG_PHYLIB
564 	if (fec->xcv_type != SEVENWIRE)
565 		miiphy_restart_aneg(dev);
566 #endif
567 	fec_open(dev);
568 	return 0;
569 }
570 
571 /**
572  * Halt the FEC engine
573  * @param[in] dev Our device to handle
574  */
575 static void fec_halt(struct eth_device *dev)
576 {
577 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
578 	int counter = 0xffff;
579 
580 	/*
581 	 * issue graceful stop command to the FEC transmitter if necessary
582 	 */
583 	writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
584 			&fec->eth->x_cntrl);
585 
586 	debug("eth_halt: wait for stop regs\n");
587 	/*
588 	 * wait for graceful stop to register
589 	 */
590 	while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
591 		udelay(1);
592 
593 	/*
594 	 * Disable SmartDMA tasks
595 	 */
596 	fec_tx_task_disable(fec);
597 	fec_rx_task_disable(fec);
598 
599 	/*
600 	 * Disable the Ethernet Controller
601 	 * Note: this will also reset the BD index counter!
602 	 */
603 	writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
604 			&fec->eth->ecntrl);
605 	fec->rbd_index = 0;
606 	fec->tbd_index = 0;
607 	debug("eth_halt: done\n");
608 }
609 
610 /**
611  * Transmit one frame
612  * @param[in] dev Our ethernet device to handle
613  * @param[in] packet Pointer to the data to be transmitted
614  * @param[in] length Data count in bytes
615  * @return 0 on success
616  */
617 static int fec_send(struct eth_device *dev, void *packet, int length)
618 {
619 	unsigned int status;
620 	uint32_t size, end;
621 	uint32_t addr;
622 	int timeout = FEC_XFER_TIMEOUT;
623 	int ret = 0;
624 
625 	/*
626 	 * This routine transmits one frame.  This routine only accepts
627 	 * 6-byte Ethernet addresses.
628 	 */
629 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
630 
631 	/*
632 	 * Check for valid length of data.
633 	 */
634 	if ((length > 1500) || (length <= 0)) {
635 		printf("Payload (%d) too large\n", length);
636 		return -1;
637 	}
638 
639 	/*
640 	 * Setup the transmit buffer. We are always using the first buffer for
641 	 * transmission, the second will be empty and only used to stop the DMA
642 	 * engine. We also flush the packet to RAM here to avoid cache trouble.
643 	 */
644 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
645 	swap_packet((uint32_t *)packet, length);
646 #endif
647 
648 	addr = (uint32_t)packet;
649 	end = roundup(addr + length, ARCH_DMA_MINALIGN);
650 	addr &= ~(ARCH_DMA_MINALIGN - 1);
651 	flush_dcache_range(addr, end);
652 
653 	writew(length, &fec->tbd_base[fec->tbd_index].data_length);
654 	writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
655 
656 	/*
657 	 * update BD's status now
658 	 * This block:
659 	 * - is always the last in a chain (means no chain)
660 	 * - should transmitt the CRC
661 	 * - might be the last BD in the list, so the address counter should
662 	 *   wrap (-> keep the WRAP flag)
663 	 */
664 	status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
665 	status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
666 	writew(status, &fec->tbd_base[fec->tbd_index].status);
667 
668 	/*
669 	 * Flush data cache. This code flushes both TX descriptors to RAM.
670 	 * After this code, the descriptors will be safely in RAM and we
671 	 * can start DMA.
672 	 */
673 	size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
674 	addr = (uint32_t)fec->tbd_base;
675 	flush_dcache_range(addr, addr + size);
676 
677 	/*
678 	 * Below we read the DMA descriptor's last four bytes back from the
679 	 * DRAM. This is important in order to make sure that all WRITE
680 	 * operations on the bus that were triggered by previous cache FLUSH
681 	 * have completed.
682 	 *
683 	 * Otherwise, on MX28, it is possible to observe a corruption of the
684 	 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
685 	 * for the bus structure of MX28. The scenario is as follows:
686 	 *
687 	 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
688 	 *    to DRAM due to flush_dcache_range()
689 	 * 2) ARM core writes the FEC registers via AHB_ARB2
690 	 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
691 	 *
692 	 * Note that 2) does sometimes finish before 1) due to reordering of
693 	 * WRITE accesses on the AHB bus, therefore triggering 3) before the
694 	 * DMA descriptor is fully written into DRAM. This results in occasional
695 	 * corruption of the DMA descriptor.
696 	 */
697 	readl(addr + size - 4);
698 
699 	/*
700 	 * Enable SmartDMA transmit task
701 	 */
702 	fec_tx_task_enable(fec);
703 
704 	/*
705 	 * Wait until frame is sent. On each turn of the wait cycle, we must
706 	 * invalidate data cache to see what's really in RAM. Also, we need
707 	 * barrier here.
708 	 */
709 	while (--timeout) {
710 		if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
711 			break;
712 	}
713 
714 	if (!timeout)
715 		ret = -EINVAL;
716 
717 	invalidate_dcache_range(addr, addr + size);
718 	if (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY)
719 		ret = -EINVAL;
720 
721 	debug("fec_send: status 0x%x index %d ret %i\n",
722 			readw(&fec->tbd_base[fec->tbd_index].status),
723 			fec->tbd_index, ret);
724 	/* for next transmission use the other buffer */
725 	if (fec->tbd_index)
726 		fec->tbd_index = 0;
727 	else
728 		fec->tbd_index = 1;
729 
730 	return ret;
731 }
732 
733 /**
734  * Pull one frame from the card
735  * @param[in] dev Our ethernet device to handle
736  * @return Length of packet read
737  */
738 static int fec_recv(struct eth_device *dev)
739 {
740 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
741 	struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
742 	unsigned long ievent;
743 	int frame_length, len = 0;
744 	struct nbuf *frame;
745 	uint16_t bd_status;
746 	uint32_t addr, size, end;
747 	int i;
748 	ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
749 
750 	/*
751 	 * Check if any critical events have happened
752 	 */
753 	ievent = readl(&fec->eth->ievent);
754 	writel(ievent, &fec->eth->ievent);
755 	debug("fec_recv: ievent 0x%lx\n", ievent);
756 	if (ievent & FEC_IEVENT_BABR) {
757 		fec_halt(dev);
758 		fec_init(dev, fec->bd);
759 		printf("some error: 0x%08lx\n", ievent);
760 		return 0;
761 	}
762 	if (ievent & FEC_IEVENT_HBERR) {
763 		/* Heartbeat error */
764 		writel(0x00000001 | readl(&fec->eth->x_cntrl),
765 				&fec->eth->x_cntrl);
766 	}
767 	if (ievent & FEC_IEVENT_GRA) {
768 		/* Graceful stop complete */
769 		if (readl(&fec->eth->x_cntrl) & 0x00000001) {
770 			fec_halt(dev);
771 			writel(~0x00000001 & readl(&fec->eth->x_cntrl),
772 					&fec->eth->x_cntrl);
773 			fec_init(dev, fec->bd);
774 		}
775 	}
776 
777 	/*
778 	 * Read the buffer status. Before the status can be read, the data cache
779 	 * must be invalidated, because the data in RAM might have been changed
780 	 * by DMA. The descriptors are properly aligned to cachelines so there's
781 	 * no need to worry they'd overlap.
782 	 *
783 	 * WARNING: By invalidating the descriptor here, we also invalidate
784 	 * the descriptors surrounding this one. Therefore we can NOT change the
785 	 * contents of this descriptor nor the surrounding ones. The problem is
786 	 * that in order to mark the descriptor as processed, we need to change
787 	 * the descriptor. The solution is to mark the whole cache line when all
788 	 * descriptors in the cache line are processed.
789 	 */
790 	addr = (uint32_t)rbd;
791 	addr &= ~(ARCH_DMA_MINALIGN - 1);
792 	size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
793 	invalidate_dcache_range(addr, addr + size);
794 
795 	bd_status = readw(&rbd->status);
796 	debug("fec_recv: status 0x%x\n", bd_status);
797 
798 	if (!(bd_status & FEC_RBD_EMPTY)) {
799 		if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
800 			((readw(&rbd->data_length) - 4) > 14)) {
801 			/*
802 			 * Get buffer address and size
803 			 */
804 			frame = (struct nbuf *)readl(&rbd->data_pointer);
805 			frame_length = readw(&rbd->data_length) - 4;
806 			/*
807 			 * Invalidate data cache over the buffer
808 			 */
809 			addr = (uint32_t)frame;
810 			end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
811 			addr &= ~(ARCH_DMA_MINALIGN - 1);
812 			invalidate_dcache_range(addr, end);
813 
814 			/*
815 			 *  Fill the buffer and pass it to upper layers
816 			 */
817 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
818 			swap_packet((uint32_t *)frame->data, frame_length);
819 #endif
820 			memcpy(buff, frame->data, frame_length);
821 			NetReceive(buff, frame_length);
822 			len = frame_length;
823 		} else {
824 			if (bd_status & FEC_RBD_ERR)
825 				printf("error frame: 0x%08lx 0x%08x\n",
826 						(ulong)rbd->data_pointer,
827 						bd_status);
828 		}
829 
830 		/*
831 		 * Free the current buffer, restart the engine and move forward
832 		 * to the next buffer. Here we check if the whole cacheline of
833 		 * descriptors was already processed and if so, we mark it free
834 		 * as whole.
835 		 */
836 		size = RXDESC_PER_CACHELINE - 1;
837 		if ((fec->rbd_index & size) == size) {
838 			i = fec->rbd_index - size;
839 			addr = (uint32_t)&fec->rbd_base[i];
840 			for (; i <= fec->rbd_index ; i++) {
841 				fec_rbd_clean(i == (FEC_RBD_NUM - 1),
842 					      &fec->rbd_base[i]);
843 			}
844 			flush_dcache_range(addr,
845 				addr + ARCH_DMA_MINALIGN);
846 		}
847 
848 		fec_rx_task_enable(fec);
849 		fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
850 	}
851 	debug("fec_recv: stop\n");
852 
853 	return len;
854 }
855 
856 static void fec_set_dev_name(char *dest, int dev_id)
857 {
858 	sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
859 }
860 
861 static int fec_alloc_descs(struct fec_priv *fec)
862 {
863 	unsigned int size;
864 	int i;
865 	uint8_t *data;
866 
867 	/* Allocate TX descriptors. */
868 	size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
869 	fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
870 	if (!fec->tbd_base)
871 		goto err_tx;
872 
873 	/* Allocate RX descriptors. */
874 	size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
875 	fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
876 	if (!fec->rbd_base)
877 		goto err_rx;
878 
879 	memset(fec->rbd_base, 0, size);
880 
881 	/* Allocate RX buffers. */
882 
883 	/* Maximum RX buffer size. */
884 	size = roundup(FEC_MAX_PKT_SIZE, ARCH_DMA_MINALIGN);
885 	for (i = 0; i < FEC_RBD_NUM; i++) {
886 		data = memalign(ARCH_DMA_MINALIGN, size);
887 		if (!data) {
888 			printf("%s: error allocating rxbuf %d\n", __func__, i);
889 			goto err_ring;
890 		}
891 
892 		memset(data, 0, size);
893 
894 		fec->rbd_base[i].data_pointer = (uint32_t)data;
895 		fec->rbd_base[i].status = FEC_RBD_EMPTY;
896 		fec->rbd_base[i].data_length = 0;
897 		/* Flush the buffer to memory. */
898 		flush_dcache_range((uint32_t)data, (uint32_t)data + size);
899 	}
900 
901 	/* Mark the last RBD to close the ring. */
902 	fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
903 
904 	fec->rbd_index = 0;
905 	fec->tbd_index = 0;
906 
907 	return 0;
908 
909 err_ring:
910 	for (; i >= 0; i--)
911 		free((void *)fec->rbd_base[i].data_pointer);
912 	free(fec->rbd_base);
913 err_rx:
914 	free(fec->tbd_base);
915 err_tx:
916 	return -ENOMEM;
917 }
918 
919 static void fec_free_descs(struct fec_priv *fec)
920 {
921 	int i;
922 
923 	for (i = 0; i < FEC_RBD_NUM; i++)
924 		free((void *)fec->rbd_base[i].data_pointer);
925 	free(fec->rbd_base);
926 	free(fec->tbd_base);
927 }
928 
929 #ifdef CONFIG_PHYLIB
930 int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
931 		struct mii_dev *bus, struct phy_device *phydev)
932 #else
933 static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
934 		struct mii_dev *bus, int phy_id)
935 #endif
936 {
937 	struct eth_device *edev;
938 	struct fec_priv *fec;
939 	unsigned char ethaddr[6];
940 	uint32_t start;
941 	int ret = 0;
942 
943 	/* create and fill edev struct */
944 	edev = (struct eth_device *)malloc(sizeof(struct eth_device));
945 	if (!edev) {
946 		puts("fec_mxc: not enough malloc memory for eth_device\n");
947 		ret = -ENOMEM;
948 		goto err1;
949 	}
950 
951 	fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
952 	if (!fec) {
953 		puts("fec_mxc: not enough malloc memory for fec_priv\n");
954 		ret = -ENOMEM;
955 		goto err2;
956 	}
957 
958 	memset(edev, 0, sizeof(*edev));
959 	memset(fec, 0, sizeof(*fec));
960 
961 	ret = fec_alloc_descs(fec);
962 	if (ret)
963 		goto err3;
964 
965 	edev->priv = fec;
966 	edev->init = fec_init;
967 	edev->send = fec_send;
968 	edev->recv = fec_recv;
969 	edev->halt = fec_halt;
970 	edev->write_hwaddr = fec_set_hwaddr;
971 
972 	fec->eth = (struct ethernet_regs *)base_addr;
973 	fec->bd = bd;
974 
975 	fec->xcv_type = CONFIG_FEC_XCV_TYPE;
976 
977 	/* Reset chip. */
978 	writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
979 	start = get_timer(0);
980 	while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
981 		if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
982 			printf("FEC MXC: Timeout reseting chip\n");
983 			goto err4;
984 		}
985 		udelay(10);
986 	}
987 
988 	fec_reg_setup(fec);
989 	fec_set_dev_name(edev->name, dev_id);
990 	fec->dev_id = (dev_id == -1) ? 0 : dev_id;
991 	fec->bus = bus;
992 	fec_mii_setspeed(bus->priv);
993 #ifdef CONFIG_PHYLIB
994 	fec->phydev = phydev;
995 	phy_connect_dev(phydev, edev);
996 	/* Configure phy */
997 	phy_config(phydev);
998 #else
999 	fec->phy_id = phy_id;
1000 #endif
1001 	eth_register(edev);
1002 
1003 	if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
1004 		debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
1005 		memcpy(edev->enetaddr, ethaddr, 6);
1006 		if (!getenv("ethaddr"))
1007 			eth_setenv_enetaddr("ethaddr", ethaddr);
1008 	}
1009 	return ret;
1010 err4:
1011 	fec_free_descs(fec);
1012 err3:
1013 	free(fec);
1014 err2:
1015 	free(edev);
1016 err1:
1017 	return ret;
1018 }
1019 
1020 struct mii_dev *fec_get_miibus(uint32_t base_addr, int dev_id)
1021 {
1022 	struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
1023 	struct mii_dev *bus;
1024 	int ret;
1025 
1026 	bus = mdio_alloc();
1027 	if (!bus) {
1028 		printf("mdio_alloc failed\n");
1029 		return NULL;
1030 	}
1031 	bus->read = fec_phy_read;
1032 	bus->write = fec_phy_write;
1033 	bus->priv = eth;
1034 	fec_set_dev_name(bus->name, dev_id);
1035 
1036 	ret = mdio_register(bus);
1037 	if (ret) {
1038 		printf("mdio_register failed\n");
1039 		free(bus);
1040 		return NULL;
1041 	}
1042 	fec_mii_setspeed(eth);
1043 	return bus;
1044 }
1045 
1046 int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1047 {
1048 	uint32_t base_mii;
1049 	struct mii_dev *bus = NULL;
1050 #ifdef CONFIG_PHYLIB
1051 	struct phy_device *phydev = NULL;
1052 #endif
1053 	int ret;
1054 
1055 #ifdef CONFIG_MX28
1056 	/*
1057 	 * The i.MX28 has two ethernet interfaces, but they are not equal.
1058 	 * Only the first one can access the MDIO bus.
1059 	 */
1060 	base_mii = MXS_ENET0_BASE;
1061 #else
1062 	base_mii = addr;
1063 #endif
1064 	debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1065 	bus = fec_get_miibus(base_mii, dev_id);
1066 	if (!bus)
1067 		return -ENOMEM;
1068 #ifdef CONFIG_PHYLIB
1069 	phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
1070 	if (!phydev) {
1071 		free(bus);
1072 		return -ENOMEM;
1073 	}
1074 	ret = fec_probe(bd, dev_id, addr, bus, phydev);
1075 #else
1076 	ret = fec_probe(bd, dev_id, addr, bus, phy_id);
1077 #endif
1078 	if (ret) {
1079 #ifdef CONFIG_PHYLIB
1080 		free(phydev);
1081 #endif
1082 		free(bus);
1083 	}
1084 	return ret;
1085 }
1086 
1087 #ifdef CONFIG_FEC_MXC_PHYADDR
1088 int fecmxc_initialize(bd_t *bd)
1089 {
1090 	return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1091 			IMX_FEC_BASE);
1092 }
1093 #endif
1094 
1095 #ifndef CONFIG_PHYLIB
1096 int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1097 {
1098 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
1099 	fec->mii_postcall = cb;
1100 	return 0;
1101 }
1102 #endif
1103