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>
35 DECLARE_GLOBAL_DATA_PTR;
38 #error "CONFIG_MII has to be defined!"
41 #ifndef CONFIG_FEC_XCV_TYPE
42 #define CONFIG_FEC_XCV_TYPE MII100
46 * The i.MX28 operates with packets in big endian. We need to swap them before
47 * sending and after receiving.
50 #define CONFIG_FEC_MXC_SWAP_PACKET
53 #define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
55 /* Check various alignment issues at compile time */
56 #if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
57 #error "ARCH_DMA_MINALIGN must be multiple of 16!"
60 #if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
61 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
62 #error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
68 uint8_t data[1500]; /**< actual data */
69 int length; /**< actual length */
70 int used; /**< buffer in use or not */
71 uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
74 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
75 static void swap_packet(uint32_t *packet, int length)
79 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
80 packet[i] = __swab32(packet[i]);
85 * MII-interface related functions
87 static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
90 uint32_t reg; /* convenient holder for the PHY register */
91 uint32_t phy; /* convenient holder for the PHY */
96 * reading from any PHY's register is done by properly
97 * programming the FEC's MII data register.
99 writel(FEC_IEVENT_MII, ð->ievent);
100 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
101 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
103 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
104 phy | reg, ð->mii_data);
107 * wait for the related interrupt
109 start = get_timer(0);
110 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
111 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
112 printf("Read MDIO failed...\n");
118 * clear mii interrupt bit
120 writel(FEC_IEVENT_MII, ð->ievent);
123 * it's now safe to read the PHY's register
125 val = (unsigned short)readl(ð->mii_data);
126 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
131 static void fec_mii_setspeed(struct fec_priv *fec)
134 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
135 * and do not drop the Preamble.
137 writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
138 &fec->eth->mii_speed);
139 debug("%s: mii_speed %08x\n", __func__, readl(&fec->eth->mii_speed));
142 static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
143 uint8_t regAddr, uint16_t data)
145 uint32_t reg; /* convenient holder for the PHY register */
146 uint32_t phy; /* convenient holder for the PHY */
149 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
150 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
152 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
153 FEC_MII_DATA_TA | phy | reg | data, ð->mii_data);
156 * wait for the MII interrupt
158 start = get_timer(0);
159 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
160 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
161 printf("Write MDIO failed...\n");
167 * clear MII interrupt bit
169 writel(FEC_IEVENT_MII, ð->ievent);
170 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
176 int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
178 return fec_mdio_read(bus->priv, phyAddr, regAddr);
181 int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
184 return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
187 #ifndef CONFIG_PHYLIB
188 static int miiphy_restart_aneg(struct eth_device *dev)
190 struct fec_priv *fec = (struct fec_priv *)dev->priv;
191 struct ethernet_regs *eth = fec->bus->priv;
195 * Wake up from sleep if necessary
196 * Reset PHY, then delay 300ns
199 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
201 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
205 * Set the auto-negotiation advertisement register bits
207 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
208 LPA_100FULL | LPA_100HALF | LPA_10FULL |
209 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
210 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
211 BMCR_ANENABLE | BMCR_ANRESTART);
213 if (fec->mii_postcall)
214 ret = fec->mii_postcall(fec->phy_id);
219 static int miiphy_wait_aneg(struct eth_device *dev)
223 struct fec_priv *fec = (struct fec_priv *)dev->priv;
224 struct ethernet_regs *eth = fec->bus->priv;
227 * Wait for AN completion
229 start = get_timer(0);
231 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
232 printf("%s: Autonegotiation timeout\n", dev->name);
236 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
238 printf("%s: Autonegotiation failed. status: %d\n",
242 } while (!(status & BMSR_LSTATUS));
248 static int fec_rx_task_enable(struct fec_priv *fec)
250 writel(1 << 24, &fec->eth->r_des_active);
254 static int fec_rx_task_disable(struct fec_priv *fec)
259 static int fec_tx_task_enable(struct fec_priv *fec)
261 writel(1 << 24, &fec->eth->x_des_active);
265 static int fec_tx_task_disable(struct fec_priv *fec)
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
277 * For this task we need additional memory for the data buffers. And each
278 * data buffer requires some alignment. Thy must be aligned to a specific
281 static int fec_rbd_init(struct fec_priv *fec, int count, int dsize)
287 * Allocate memory for the buffers. This allocation respects the
290 size = roundup(dsize, ARCH_DMA_MINALIGN);
291 for (i = 0; i < count; i++) {
292 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
294 uint8_t *data = memalign(ARCH_DMA_MINALIGN,
297 printf("%s: error allocating rxbuf %d\n",
301 writel((uint32_t)data, &fec->rbd_base[i].data_pointer);
302 } /* needs allocation */
303 writew(FEC_RBD_EMPTY, &fec->rbd_base[i].status);
304 writew(0, &fec->rbd_base[i].data_length);
307 /* Mark the last RBD to close the ring. */
308 writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[i - 1].status);
314 for (; i >= 0; i--) {
315 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
316 free((void *)data_ptr);
323 * Initialize transmit task's buffer descriptors
324 * @param[in] fec all we know about the device yet
326 * Transmit buffers are created externally. We only have to init the BDs here.\n
327 * Note: There is a race condition in the hardware. When only one BD is in
328 * use it must be marked with the WRAP bit to use it for every transmitt.
329 * This bit in combination with the READY bit results into double transmit
330 * of each data buffer. It seems the state machine checks READY earlier then
331 * resetting it after the first transfer.
332 * Using two BDs solves this issue.
334 static void fec_tbd_init(struct fec_priv *fec)
336 unsigned addr = (unsigned)fec->tbd_base;
337 unsigned size = roundup(2 * sizeof(struct fec_bd),
339 writew(0x0000, &fec->tbd_base[0].status);
340 writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
342 flush_dcache_range(addr, addr+size);
346 * Mark the given read buffer descriptor as free
347 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
348 * @param[in] pRbd buffer descriptor to mark free again
350 static void fec_rbd_clean(int last, struct fec_bd *pRbd)
352 unsigned short flags = FEC_RBD_EMPTY;
354 flags |= FEC_RBD_WRAP;
355 writew(flags, &pRbd->status);
356 writew(0, &pRbd->data_length);
359 static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
362 imx_get_mac_from_fuse(dev_id, mac);
363 return !is_valid_ether_addr(mac);
366 static int fec_set_hwaddr(struct eth_device *dev)
368 uchar *mac = dev->enetaddr;
369 struct fec_priv *fec = (struct fec_priv *)dev->priv;
371 writel(0, &fec->eth->iaddr1);
372 writel(0, &fec->eth->iaddr2);
373 writel(0, &fec->eth->gaddr1);
374 writel(0, &fec->eth->gaddr2);
377 * Set physical address
379 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
381 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
386 static void fec_eth_phy_config(struct eth_device *dev)
389 struct fec_priv *fec = (struct fec_priv *)dev->priv;
390 struct phy_device *phydev;
392 phydev = phy_connect(fec->bus, fec->phy_id, dev,
393 PHY_INTERFACE_MODE_RGMII);
395 fec->phydev = phydev;
402 * Start the FEC engine
403 * @param[in] dev Our device to handle
405 static int fec_open(struct eth_device *edev)
407 struct fec_priv *fec = (struct fec_priv *)edev->priv;
412 debug("fec_open: fec_open(dev)\n");
413 /* full-duplex, heartbeat disabled */
414 writel(1 << 2, &fec->eth->x_cntrl);
417 /* Invalidate all descriptors */
418 for (i = 0; i < FEC_RBD_NUM - 1; i++)
419 fec_rbd_clean(0, &fec->rbd_base[i]);
420 fec_rbd_clean(1, &fec->rbd_base[i]);
422 /* Flush the descriptors into RAM */
423 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
425 addr = (uint32_t)fec->rbd_base;
426 flush_dcache_range(addr, addr + size);
428 #ifdef FEC_QUIRK_ENET_MAC
429 /* Enable ENET HW endian SWAP */
430 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
432 /* Enable ENET store and forward mode */
433 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
437 * Enable FEC-Lite controller
439 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
441 #if defined(CONFIG_MX25) || defined(CONFIG_MX53)
444 * setup the MII gasket for RMII mode
447 /* disable the gasket */
448 writew(0, &fec->eth->miigsk_enr);
450 /* wait for the gasket to be disabled */
451 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
454 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
455 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
457 /* re-enable the gasket */
458 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
460 /* wait until MII gasket is ready */
462 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
463 if (--max_loops <= 0) {
464 printf("WAIT for MII Gasket ready timed out\n");
472 fec_eth_phy_config(edev);
474 /* Start up the PHY */
475 phy_startup(fec->phydev);
476 speed = fec->phydev->speed;
481 miiphy_wait_aneg(edev);
482 speed = miiphy_speed(edev->name, fec->phy_id);
483 miiphy_duplex(edev->name, fec->phy_id);
486 #ifdef FEC_QUIRK_ENET_MAC
488 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
489 u32 rcr = (readl(&fec->eth->r_cntrl) &
490 ~(FEC_RCNTRL_RMII | FEC_RCNTRL_RMII_10T)) |
491 FEC_RCNTRL_RGMII | FEC_RCNTRL_MII_MODE;
492 if (speed == _1000BASET)
493 ecr |= FEC_ECNTRL_SPEED;
494 else if (speed != _100BASET)
495 rcr |= FEC_RCNTRL_RMII_10T;
496 writel(ecr, &fec->eth->ecntrl);
497 writel(rcr, &fec->eth->r_cntrl);
500 debug("%s:Speed=%i\n", __func__, speed);
503 * Enable SmartDMA receive task
505 fec_rx_task_enable(fec);
511 static int fec_init(struct eth_device *dev, bd_t* bd)
513 struct fec_priv *fec = (struct fec_priv *)dev->priv;
514 uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
519 /* Initialize MAC address */
523 * Allocate transmit descriptors, there are two in total. This
524 * allocation respects cache alignment.
526 if (!fec->tbd_base) {
527 size = roundup(2 * sizeof(struct fec_bd),
529 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
530 if (!fec->tbd_base) {
534 memset(fec->tbd_base, 0, size);
536 flush_dcache_range((unsigned)fec->tbd_base, size);
540 * Allocate receive descriptors. This allocation respects cache
543 if (!fec->rbd_base) {
544 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
546 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
547 if (!fec->rbd_base) {
551 memset(fec->rbd_base, 0, size);
553 * Initialize RxBD ring
555 if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
559 flush_dcache_range((unsigned)fec->rbd_base,
560 (unsigned)fec->rbd_base + size);
564 * Set interrupt mask register
566 writel(0x00000000, &fec->eth->imask);
569 * Clear FEC-Lite interrupt event register(IEVENT)
571 writel(0xffffffff, &fec->eth->ievent);
575 * Set FEC-Lite receive control register(R_CNTRL):
578 /* Start with frame length = 1518, common for all modes. */
579 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
580 if (fec->xcv_type == SEVENWIRE)
581 rcntrl |= FEC_RCNTRL_FCE;
582 else if (fec->xcv_type == RGMII)
583 rcntrl |= FEC_RCNTRL_RGMII;
584 else if (fec->xcv_type == RMII)
585 rcntrl |= FEC_RCNTRL_RMII;
587 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
589 writel(rcntrl, &fec->eth->r_cntrl);
591 if (fec->xcv_type == MII10 || fec->xcv_type == MII100)
592 fec_mii_setspeed(fec);
595 * Set Opcode/Pause Duration Register
597 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
598 writel(0x2, &fec->eth->x_wmrk);
600 * Set multicast address filter
602 writel(0x00000000, &fec->eth->gaddr1);
603 writel(0x00000000, &fec->eth->gaddr2);
607 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
610 /* FIFO receive start register */
611 writel(0x520, &fec->eth->r_fstart);
613 /* size and address of each buffer */
614 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
615 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
616 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
618 #ifndef CONFIG_PHYLIB
619 if (fec->xcv_type != SEVENWIRE)
620 miiphy_restart_aneg(dev);
634 * Halt the FEC engine
635 * @param[in] dev Our device to handle
637 static void fec_halt(struct eth_device *dev)
639 struct fec_priv *fec = (struct fec_priv *)dev->priv;
640 int counter = 0xffff;
643 * issue graceful stop command to the FEC transmitter if necessary
645 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
648 debug("eth_halt: wait for stop regs\n");
650 * wait for graceful stop to register
652 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
656 * Disable SmartDMA tasks
658 fec_tx_task_disable(fec);
659 fec_rx_task_disable(fec);
662 * Disable the Ethernet Controller
663 * Note: this will also reset the BD index counter!
665 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
669 debug("eth_halt: done\n");
674 * @param[in] dev Our ethernet device to handle
675 * @param[in] packet Pointer to the data to be transmitted
676 * @param[in] length Data count in bytes
677 * @return 0 on success
679 static int fec_send(struct eth_device *dev, volatile void *packet, int length)
686 * This routine transmits one frame. This routine only accepts
687 * 6-byte Ethernet addresses.
689 struct fec_priv *fec = (struct fec_priv *)dev->priv;
692 * Check for valid length of data.
694 if ((length > 1500) || (length <= 0)) {
695 printf("Payload (%d) too large\n", length);
700 * Setup the transmit buffer. We are always using the first buffer for
701 * transmission, the second will be empty and only used to stop the DMA
702 * engine. We also flush the packet to RAM here to avoid cache trouble.
704 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
705 swap_packet((uint32_t *)packet, length);
708 addr = (uint32_t)packet;
709 size = roundup(length, ARCH_DMA_MINALIGN);
710 flush_dcache_range(addr, addr + size);
712 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
713 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
716 * update BD's status now
718 * - is always the last in a chain (means no chain)
719 * - should transmitt the CRC
720 * - might be the last BD in the list, so the address counter should
721 * wrap (-> keep the WRAP flag)
723 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
724 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
725 writew(status, &fec->tbd_base[fec->tbd_index].status);
728 * Flush data cache. This code flushes both TX descriptors to RAM.
729 * After this code, the descriptors will be safely in RAM and we
732 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
733 addr = (uint32_t)fec->tbd_base;
734 flush_dcache_range(addr, addr + size);
737 * Enable SmartDMA transmit task
739 fec_tx_task_enable(fec);
742 * Wait until frame is sent. On each turn of the wait cycle, we must
743 * invalidate data cache to see what's really in RAM. Also, we need
746 invalidate_dcache_range(addr, addr + size);
747 while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
749 invalidate_dcache_range(addr, addr + size);
752 debug("fec_send: status 0x%x index %d\n",
753 readw(&fec->tbd_base[fec->tbd_index].status),
755 /* for next transmission use the other buffer */
765 * Pull one frame from the card
766 * @param[in] dev Our ethernet device to handle
767 * @return Length of packet read
769 static int fec_recv(struct eth_device *dev)
771 struct fec_priv *fec = (struct fec_priv *)dev->priv;
772 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
773 unsigned long ievent;
774 int frame_length, len = 0;
779 uchar buff[FEC_MAX_PKT_SIZE];
782 * Check if any critical events have happened
784 ievent = readl(&fec->eth->ievent);
785 writel(ievent, &fec->eth->ievent);
786 debug("fec_recv: ievent 0x%lx\n", ievent);
787 if (ievent & FEC_IEVENT_BABR) {
789 fec_init(dev, fec->bd);
790 printf("some error: 0x%08lx\n", ievent);
793 if (ievent & FEC_IEVENT_HBERR) {
794 /* Heartbeat error */
795 writel(0x00000001 | readl(&fec->eth->x_cntrl),
798 if (ievent & FEC_IEVENT_GRA) {
799 /* Graceful stop complete */
800 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
802 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
804 fec_init(dev, fec->bd);
809 * Read the buffer status. Before the status can be read, the data cache
810 * must be invalidated, because the data in RAM might have been changed
811 * by DMA. The descriptors are properly aligned to cachelines so there's
812 * no need to worry they'd overlap.
814 * WARNING: By invalidating the descriptor here, we also invalidate
815 * the descriptors surrounding this one. Therefore we can NOT change the
816 * contents of this descriptor nor the surrounding ones. The problem is
817 * that in order to mark the descriptor as processed, we need to change
818 * the descriptor. The solution is to mark the whole cache line when all
819 * descriptors in the cache line are processed.
821 addr = (uint32_t)rbd;
822 addr &= ~(ARCH_DMA_MINALIGN - 1);
823 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
824 invalidate_dcache_range(addr, addr + size);
826 bd_status = readw(&rbd->status);
827 debug("fec_recv: status 0x%x\n", bd_status);
829 if (!(bd_status & FEC_RBD_EMPTY)) {
830 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
831 ((readw(&rbd->data_length) - 4) > 14)) {
833 * Get buffer address and size
835 frame = (struct nbuf *)readl(&rbd->data_pointer);
836 frame_length = readw(&rbd->data_length) - 4;
838 * Invalidate data cache over the buffer
840 addr = (uint32_t)frame;
841 size = roundup(frame_length, ARCH_DMA_MINALIGN);
842 invalidate_dcache_range(addr, addr + size);
845 * Fill the buffer and pass it to upper layers
847 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
848 swap_packet((uint32_t *)frame->data, frame_length);
850 memcpy(buff, frame->data, frame_length);
851 NetReceive(buff, frame_length);
854 if (bd_status & FEC_RBD_ERR)
855 printf("error frame: 0x%08lx 0x%08x\n",
856 (ulong)rbd->data_pointer,
861 * Free the current buffer, restart the engine and move forward
862 * to the next buffer. Here we check if the whole cacheline of
863 * descriptors was already processed and if so, we mark it free
866 size = RXDESC_PER_CACHELINE - 1;
867 if ((fec->rbd_index & size) == size) {
868 i = fec->rbd_index - size;
869 addr = (uint32_t)&fec->rbd_base[i];
870 for (; i <= fec->rbd_index ; i++) {
871 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
874 flush_dcache_range(addr,
875 addr + ARCH_DMA_MINALIGN);
878 fec_rx_task_enable(fec);
879 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
881 debug("fec_recv: stop\n");
886 static int fec_probe(bd_t *bd, int dev_id, int phy_id, uint32_t base_addr)
888 struct eth_device *edev;
889 struct fec_priv *fec;
891 unsigned char ethaddr[6];
895 /* create and fill edev struct */
896 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
898 puts("fec_mxc: not enough malloc memory for eth_device\n");
903 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
905 puts("fec_mxc: not enough malloc memory for fec_priv\n");
910 memset(edev, 0, sizeof(*edev));
911 memset(fec, 0, sizeof(*fec));
914 edev->init = fec_init;
915 edev->send = fec_send;
916 edev->recv = fec_recv;
917 edev->halt = fec_halt;
918 edev->write_hwaddr = fec_set_hwaddr;
920 fec->eth = (struct ethernet_regs *)base_addr;
923 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
926 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
927 start = get_timer(0);
928 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
929 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
930 printf("FEC MXC: Timeout reseting chip\n");
937 * Set interrupt mask register
939 writel(0x00000000, &fec->eth->imask);
942 * Clear FEC-Lite interrupt event register(IEVENT)
944 writel(0xffffffff, &fec->eth->ievent);
947 * Set FEC-Lite receive control register(R_CNTRL):
950 * Frame length=1518; MII mode;
952 writel((PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT) | FEC_RCNTRL_FCE |
953 FEC_RCNTRL_MII_MODE, &fec->eth->r_cntrl);
954 fec_mii_setspeed(fec);
957 sprintf(edev->name, "FEC");
960 sprintf(edev->name, "FEC%i", dev_id);
961 fec->dev_id = dev_id;
963 fec->phy_id = phy_id;
967 printf("mdio_alloc failed\n");
971 bus->read = fec_phy_read;
972 bus->write = fec_phy_write;
973 sprintf(bus->name, edev->name);
976 * The i.MX28 has two ethernet interfaces, but they are not equal.
977 * Only the first one can access the MDIO bus.
979 bus->priv = (struct ethernet_regs *)MXS_ENET0_BASE;
981 bus->priv = fec->eth;
983 ret = mdio_register(bus);
985 printf("mdio_register failed\n");
993 if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
994 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
995 memcpy(edev->enetaddr, ethaddr, 6);
998 fec_eth_phy_config(edev);
1009 #ifndef CONFIG_FEC_MXC_MULTI
1010 int fecmxc_initialize(bd_t *bd)
1014 debug("eth_init: fec_probe(bd)\n");
1015 lout = fec_probe(bd, -1, CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
1021 int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1025 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1026 lout = fec_probe(bd, dev_id, phy_id, addr);
1031 #ifndef CONFIG_PHYLIB
1032 int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1034 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1035 fec->mii_postcall = cb;