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>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include <asm/arch/clock.h>
31 #include <asm/arch/imx-regs.h>
33 #include <asm/errno.h>
34 #include <linux/compiler.h>
36 DECLARE_GLOBAL_DATA_PTR;
39 * Timeout the transfer after 5 mS. This is usually a bit more, since
40 * the code in the tightloops this timeout is used in adds some overhead.
42 #define FEC_XFER_TIMEOUT 5000
45 #error "CONFIG_MII has to be defined!"
48 #ifndef CONFIG_FEC_XCV_TYPE
49 #define CONFIG_FEC_XCV_TYPE MII100
53 * The i.MX28 operates with packets in big endian. We need to swap them before
54 * sending and after receiving.
57 #define CONFIG_FEC_MXC_SWAP_PACKET
60 #define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
62 /* Check various alignment issues at compile time */
63 #if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
64 #error "ARCH_DMA_MINALIGN must be multiple of 16!"
67 #if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
68 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
69 #error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
75 uint8_t data[1500]; /**< actual data */
76 int length; /**< actual length */
77 int used; /**< buffer in use or not */
78 uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
81 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
82 static void swap_packet(uint32_t *packet, int length)
86 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
87 packet[i] = __swab32(packet[i]);
92 * MII-interface related functions
94 static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
97 uint32_t reg; /* convenient holder for the PHY register */
98 uint32_t phy; /* convenient holder for the PHY */
103 * reading from any PHY's register is done by properly
104 * programming the FEC's MII data register.
106 writel(FEC_IEVENT_MII, ð->ievent);
107 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
108 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
110 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
111 phy | reg, ð->mii_data);
114 * wait for the related interrupt
116 start = get_timer(0);
117 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
118 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
119 printf("Read MDIO failed...\n");
125 * clear mii interrupt bit
127 writel(FEC_IEVENT_MII, ð->ievent);
130 * it's now safe to read the PHY's register
132 val = (unsigned short)readl(ð->mii_data);
133 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
138 static void fec_mii_setspeed(struct ethernet_regs *eth)
141 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
142 * and do not drop the Preamble.
144 writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
146 debug("%s: mii_speed %08x\n", __func__, readl(ð->mii_speed));
149 static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
150 uint8_t regAddr, uint16_t data)
152 uint32_t reg; /* convenient holder for the PHY register */
153 uint32_t phy; /* convenient holder for the PHY */
156 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
157 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
159 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
160 FEC_MII_DATA_TA | phy | reg | data, ð->mii_data);
163 * wait for the MII interrupt
165 start = get_timer(0);
166 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
167 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
168 printf("Write MDIO failed...\n");
174 * clear MII interrupt bit
176 writel(FEC_IEVENT_MII, ð->ievent);
177 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
183 int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
185 return fec_mdio_read(bus->priv, phyAddr, regAddr);
188 int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
191 return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
194 #ifndef CONFIG_PHYLIB
195 static int miiphy_restart_aneg(struct eth_device *dev)
198 #if !defined(CONFIG_FEC_MXC_NO_ANEG)
199 struct fec_priv *fec = (struct fec_priv *)dev->priv;
200 struct ethernet_regs *eth = fec->bus->priv;
203 * Wake up from sleep if necessary
204 * Reset PHY, then delay 300ns
207 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
209 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
213 * Set the auto-negotiation advertisement register bits
215 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
216 LPA_100FULL | LPA_100HALF | LPA_10FULL |
217 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
218 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
219 BMCR_ANENABLE | BMCR_ANRESTART);
221 if (fec->mii_postcall)
222 ret = fec->mii_postcall(fec->phy_id);
228 static int miiphy_wait_aneg(struct eth_device *dev)
232 struct fec_priv *fec = (struct fec_priv *)dev->priv;
233 struct ethernet_regs *eth = fec->bus->priv;
236 * Wait for AN completion
238 start = get_timer(0);
240 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
241 printf("%s: Autonegotiation timeout\n", dev->name);
245 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
247 printf("%s: Autonegotiation failed. status: %d\n",
251 } while (!(status & BMSR_LSTATUS));
257 static int fec_rx_task_enable(struct fec_priv *fec)
259 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
263 static int fec_rx_task_disable(struct fec_priv *fec)
268 static int fec_tx_task_enable(struct fec_priv *fec)
270 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
274 static int fec_tx_task_disable(struct fec_priv *fec)
280 * Initialize receive task's buffer descriptors
281 * @param[in] fec all we know about the device yet
282 * @param[in] count receive buffer count to be allocated
283 * @param[in] dsize desired size of each receive buffer
284 * @return 0 on success
286 * For this task we need additional memory for the data buffers. And each
287 * data buffer requires some alignment. Thy must be aligned to a specific
290 static int fec_rbd_init(struct fec_priv *fec, int count, int dsize)
296 * Allocate memory for the buffers. This allocation respects the
299 size = roundup(dsize, ARCH_DMA_MINALIGN);
300 for (i = 0; i < count; i++) {
301 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
303 uint8_t *data = memalign(ARCH_DMA_MINALIGN,
306 printf("%s: error allocating rxbuf %d\n",
310 writel((uint32_t)data, &fec->rbd_base[i].data_pointer);
311 } /* needs allocation */
312 writew(FEC_RBD_EMPTY, &fec->rbd_base[i].status);
313 writew(0, &fec->rbd_base[i].data_length);
316 /* Mark the last RBD to close the ring. */
317 writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[i - 1].status);
323 for (; i >= 0; i--) {
324 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
325 free((void *)data_ptr);
332 * Initialize transmit task's buffer descriptors
333 * @param[in] fec all we know about the device yet
335 * Transmit buffers are created externally. We only have to init the BDs here.\n
336 * Note: There is a race condition in the hardware. When only one BD is in
337 * use it must be marked with the WRAP bit to use it for every transmitt.
338 * This bit in combination with the READY bit results into double transmit
339 * of each data buffer. It seems the state machine checks READY earlier then
340 * resetting it after the first transfer.
341 * Using two BDs solves this issue.
343 static void fec_tbd_init(struct fec_priv *fec)
345 unsigned addr = (unsigned)fec->tbd_base;
346 unsigned size = roundup(2 * sizeof(struct fec_bd),
348 writew(0x0000, &fec->tbd_base[0].status);
349 writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
351 flush_dcache_range(addr, addr+size);
355 * Mark the given read buffer descriptor as free
356 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
357 * @param[in] pRbd buffer descriptor to mark free again
359 static void fec_rbd_clean(int last, struct fec_bd *pRbd)
361 unsigned short flags = FEC_RBD_EMPTY;
363 flags |= FEC_RBD_WRAP;
364 writew(flags, &pRbd->status);
365 writew(0, &pRbd->data_length);
368 static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
371 imx_get_mac_from_fuse(dev_id, mac);
372 return !is_valid_ether_addr(mac);
375 static int fec_set_hwaddr(struct eth_device *dev)
377 uchar *mac = dev->enetaddr;
378 struct fec_priv *fec = (struct fec_priv *)dev->priv;
380 writel(0, &fec->eth->iaddr1);
381 writel(0, &fec->eth->iaddr2);
382 writel(0, &fec->eth->gaddr1);
383 writel(0, &fec->eth->gaddr2);
386 * Set physical address
388 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
390 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
396 * Do initial configuration of the FEC registers
398 static void fec_reg_setup(struct fec_priv *fec)
403 * Set interrupt mask register
405 writel(0x00000000, &fec->eth->imask);
408 * Clear FEC-Lite interrupt event register(IEVENT)
410 writel(0xffffffff, &fec->eth->ievent);
414 * Set FEC-Lite receive control register(R_CNTRL):
417 /* Start with frame length = 1518, common for all modes. */
418 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
419 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
420 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
421 if (fec->xcv_type == RGMII)
422 rcntrl |= FEC_RCNTRL_RGMII;
423 else if (fec->xcv_type == RMII)
424 rcntrl |= FEC_RCNTRL_RMII;
426 writel(rcntrl, &fec->eth->r_cntrl);
430 * Start the FEC engine
431 * @param[in] dev Our device to handle
433 static int fec_open(struct eth_device *edev)
435 struct fec_priv *fec = (struct fec_priv *)edev->priv;
440 debug("fec_open: fec_open(dev)\n");
441 /* full-duplex, heartbeat disabled */
442 writel(1 << 2, &fec->eth->x_cntrl);
445 /* Invalidate all descriptors */
446 for (i = 0; i < FEC_RBD_NUM - 1; i++)
447 fec_rbd_clean(0, &fec->rbd_base[i]);
448 fec_rbd_clean(1, &fec->rbd_base[i]);
450 /* Flush the descriptors into RAM */
451 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
453 addr = (uint32_t)fec->rbd_base;
454 flush_dcache_range(addr, addr + size);
456 #ifdef FEC_QUIRK_ENET_MAC
457 /* Enable ENET HW endian SWAP */
458 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
460 /* Enable ENET store and forward mode */
461 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
465 * Enable FEC-Lite controller
467 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
469 #if defined(CONFIG_MX25) || defined(CONFIG_MX53)
472 * setup the MII gasket for RMII mode
475 /* disable the gasket */
476 writew(0, &fec->eth->miigsk_enr);
478 /* wait for the gasket to be disabled */
479 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
482 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
483 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
485 /* re-enable the gasket */
486 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
488 /* wait until MII gasket is ready */
490 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
491 if (--max_loops <= 0) {
492 printf("WAIT for MII Gasket ready timed out\n");
500 /* Start up the PHY */
501 int ret = phy_startup(fec->phydev);
504 printf("Could not initialize PHY %s\n",
505 fec->phydev->dev->name);
508 speed = fec->phydev->speed;
511 miiphy_wait_aneg(edev);
512 speed = miiphy_speed(edev->name, fec->phy_id);
513 miiphy_duplex(edev->name, fec->phy_id);
516 #ifdef FEC_QUIRK_ENET_MAC
518 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
519 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
520 if (speed == _1000BASET)
521 ecr |= FEC_ECNTRL_SPEED;
522 else if (speed != _100BASET)
523 rcr |= FEC_RCNTRL_RMII_10T;
524 writel(ecr, &fec->eth->ecntrl);
525 writel(rcr, &fec->eth->r_cntrl);
528 debug("%s:Speed=%i\n", __func__, speed);
531 * Enable SmartDMA receive task
533 fec_rx_task_enable(fec);
539 static int fec_init(struct eth_device *dev, bd_t* bd)
541 struct fec_priv *fec = (struct fec_priv *)dev->priv;
542 uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
546 /* Initialize MAC address */
550 * Allocate transmit descriptors, there are two in total. This
551 * allocation respects cache alignment.
553 if (!fec->tbd_base) {
554 size = roundup(2 * sizeof(struct fec_bd),
556 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
557 if (!fec->tbd_base) {
561 memset(fec->tbd_base, 0, size);
563 flush_dcache_range((unsigned)fec->tbd_base, size);
567 * Allocate receive descriptors. This allocation respects cache
570 if (!fec->rbd_base) {
571 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
573 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
574 if (!fec->rbd_base) {
578 memset(fec->rbd_base, 0, size);
580 * Initialize RxBD ring
582 if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
586 flush_dcache_range((unsigned)fec->rbd_base,
587 (unsigned)fec->rbd_base + size);
592 if (fec->xcv_type != SEVENWIRE)
593 fec_mii_setspeed(fec->bus->priv);
596 * Set Opcode/Pause Duration Register
598 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
599 writel(0x2, &fec->eth->x_wmrk);
601 * Set multicast address filter
603 writel(0x00000000, &fec->eth->gaddr1);
604 writel(0x00000000, &fec->eth->gaddr2);
608 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
611 /* FIFO receive start register */
612 writel(0x520, &fec->eth->r_fstart);
614 /* size and address of each buffer */
615 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
616 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
617 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
619 #ifndef CONFIG_PHYLIB
620 if (fec->xcv_type != SEVENWIRE)
621 miiphy_restart_aneg(dev);
635 * Halt the FEC engine
636 * @param[in] dev Our device to handle
638 static void fec_halt(struct eth_device *dev)
640 struct fec_priv *fec = (struct fec_priv *)dev->priv;
641 int counter = 0xffff;
644 * issue graceful stop command to the FEC transmitter if necessary
646 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
649 debug("eth_halt: wait for stop regs\n");
651 * wait for graceful stop to register
653 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
657 * Disable SmartDMA tasks
659 fec_tx_task_disable(fec);
660 fec_rx_task_disable(fec);
663 * Disable the Ethernet Controller
664 * Note: this will also reset the BD index counter!
666 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
670 debug("eth_halt: done\n");
675 * @param[in] dev Our ethernet device to handle
676 * @param[in] packet Pointer to the data to be transmitted
677 * @param[in] length Data count in bytes
678 * @return 0 on success
680 static int fec_send(struct eth_device *dev, void *packet, int length)
685 int timeout = FEC_XFER_TIMEOUT;
689 * This routine transmits one frame. This routine only accepts
690 * 6-byte Ethernet addresses.
692 struct fec_priv *fec = (struct fec_priv *)dev->priv;
695 * Check for valid length of data.
697 if ((length > 1500) || (length <= 0)) {
698 printf("Payload (%d) too large\n", length);
703 * Setup the transmit buffer. We are always using the first buffer for
704 * transmission, the second will be empty and only used to stop the DMA
705 * engine. We also flush the packet to RAM here to avoid cache trouble.
707 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
708 swap_packet((uint32_t *)packet, length);
711 addr = (uint32_t)packet;
712 end = roundup(addr + length, ARCH_DMA_MINALIGN);
713 addr &= ~(ARCH_DMA_MINALIGN - 1);
714 flush_dcache_range(addr, end);
716 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
717 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
720 * update BD's status now
722 * - is always the last in a chain (means no chain)
723 * - should transmitt the CRC
724 * - might be the last BD in the list, so the address counter should
725 * wrap (-> keep the WRAP flag)
727 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
728 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
729 writew(status, &fec->tbd_base[fec->tbd_index].status);
732 * Flush data cache. This code flushes both TX descriptors to RAM.
733 * After this code, the descriptors will be safely in RAM and we
736 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
737 addr = (uint32_t)fec->tbd_base;
738 flush_dcache_range(addr, addr + size);
741 * Enable SmartDMA transmit task
743 fec_tx_task_enable(fec);
746 * Wait until frame is sent. On each turn of the wait cycle, we must
747 * invalidate data cache to see what's really in RAM. Also, we need
751 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
758 invalidate_dcache_range(addr, addr + size);
759 if (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY)
762 debug("fec_send: status 0x%x index %d ret %i\n",
763 readw(&fec->tbd_base[fec->tbd_index].status),
764 fec->tbd_index, ret);
765 /* for next transmission use the other buffer */
775 * Pull one frame from the card
776 * @param[in] dev Our ethernet device to handle
777 * @return Length of packet read
779 static int fec_recv(struct eth_device *dev)
781 struct fec_priv *fec = (struct fec_priv *)dev->priv;
782 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
783 unsigned long ievent;
784 int frame_length, len = 0;
787 uint32_t addr, size, end;
789 uchar buff[FEC_MAX_PKT_SIZE] __aligned(ARCH_DMA_MINALIGN);
792 * Check if any critical events have happened
794 ievent = readl(&fec->eth->ievent);
795 writel(ievent, &fec->eth->ievent);
796 debug("fec_recv: ievent 0x%lx\n", ievent);
797 if (ievent & FEC_IEVENT_BABR) {
799 fec_init(dev, fec->bd);
800 printf("some error: 0x%08lx\n", ievent);
803 if (ievent & FEC_IEVENT_HBERR) {
804 /* Heartbeat error */
805 writel(0x00000001 | readl(&fec->eth->x_cntrl),
808 if (ievent & FEC_IEVENT_GRA) {
809 /* Graceful stop complete */
810 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
812 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
814 fec_init(dev, fec->bd);
819 * Read the buffer status. Before the status can be read, the data cache
820 * must be invalidated, because the data in RAM might have been changed
821 * by DMA. The descriptors are properly aligned to cachelines so there's
822 * no need to worry they'd overlap.
824 * WARNING: By invalidating the descriptor here, we also invalidate
825 * the descriptors surrounding this one. Therefore we can NOT change the
826 * contents of this descriptor nor the surrounding ones. The problem is
827 * that in order to mark the descriptor as processed, we need to change
828 * the descriptor. The solution is to mark the whole cache line when all
829 * descriptors in the cache line are processed.
831 addr = (uint32_t)rbd;
832 addr &= ~(ARCH_DMA_MINALIGN - 1);
833 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
834 invalidate_dcache_range(addr, addr + size);
836 bd_status = readw(&rbd->status);
837 debug("fec_recv: status 0x%x\n", bd_status);
839 if (!(bd_status & FEC_RBD_EMPTY)) {
840 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
841 ((readw(&rbd->data_length) - 4) > 14)) {
843 * Get buffer address and size
845 frame = (struct nbuf *)readl(&rbd->data_pointer);
846 frame_length = readw(&rbd->data_length) - 4;
848 * Invalidate data cache over the buffer
850 addr = (uint32_t)frame;
851 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
852 addr &= ~(ARCH_DMA_MINALIGN - 1);
853 invalidate_dcache_range(addr, end);
856 * Fill the buffer and pass it to upper layers
858 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
859 swap_packet((uint32_t *)frame->data, frame_length);
861 memcpy(buff, frame->data, frame_length);
862 NetReceive(buff, frame_length);
865 if (bd_status & FEC_RBD_ERR)
866 printf("error frame: 0x%08lx 0x%08x\n",
867 (ulong)rbd->data_pointer,
872 * Free the current buffer, restart the engine and move forward
873 * to the next buffer. Here we check if the whole cacheline of
874 * descriptors was already processed and if so, we mark it free
877 size = RXDESC_PER_CACHELINE - 1;
878 if ((fec->rbd_index & size) == size) {
879 i = fec->rbd_index - size;
880 addr = (uint32_t)&fec->rbd_base[i];
881 for (; i <= fec->rbd_index ; i++) {
882 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
885 flush_dcache_range(addr,
886 addr + ARCH_DMA_MINALIGN);
889 fec_rx_task_enable(fec);
890 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
892 debug("fec_recv: stop\n");
897 static void fec_set_dev_name(char *dest, int dev_id)
899 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
903 int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
904 struct mii_dev *bus, struct phy_device *phydev)
906 static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
907 struct mii_dev *bus, int phy_id)
910 struct eth_device *edev;
911 struct fec_priv *fec;
912 unsigned char ethaddr[6];
916 /* create and fill edev struct */
917 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
919 puts("fec_mxc: not enough malloc memory for eth_device\n");
924 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
926 puts("fec_mxc: not enough malloc memory for fec_priv\n");
931 memset(edev, 0, sizeof(*edev));
932 memset(fec, 0, sizeof(*fec));
935 edev->init = fec_init;
936 edev->send = fec_send;
937 edev->recv = fec_recv;
938 edev->halt = fec_halt;
939 edev->write_hwaddr = fec_set_hwaddr;
941 fec->eth = (struct ethernet_regs *)base_addr;
944 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
947 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
948 start = get_timer(0);
949 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
950 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
951 printf("FEC MXC: Timeout reseting chip\n");
958 fec_set_dev_name(edev->name, dev_id);
959 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
961 fec_mii_setspeed(bus->priv);
963 fec->phydev = phydev;
964 phy_connect_dev(phydev, edev);
968 fec->phy_id = phy_id;
972 if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
973 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
974 memcpy(edev->enetaddr, ethaddr, 6);
985 struct mii_dev *fec_get_miibus(uint32_t base_addr, int dev_id)
987 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
993 printf("mdio_alloc failed\n");
996 bus->read = fec_phy_read;
997 bus->write = fec_phy_write;
999 fec_set_dev_name(bus->name, dev_id);
1001 ret = mdio_register(bus);
1003 printf("mdio_register failed\n");
1007 fec_mii_setspeed(eth);
1011 int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1014 struct mii_dev *bus = NULL;
1015 #ifdef CONFIG_PHYLIB
1016 struct phy_device *phydev = NULL;
1022 * The i.MX28 has two ethernet interfaces, but they are not equal.
1023 * Only the first one can access the MDIO bus.
1025 base_mii = MXS_ENET0_BASE;
1029 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1030 bus = fec_get_miibus(base_mii, dev_id);
1033 #ifdef CONFIG_PHYLIB
1034 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
1039 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1041 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
1044 #ifdef CONFIG_PHYLIB
1052 #ifdef CONFIG_FEC_MXC_PHYADDR
1053 int fecmxc_initialize(bd_t *bd)
1055 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1060 #ifndef CONFIG_PHYLIB
1061 int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1063 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1064 fec->mii_postcall = cb;