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,
29 #include <asm/arch/sys_proto.h>
30 #include <asm/arch/clock.h>
31 #include <asm/arch/imx-regs.h>
33 #include <asm/errno.h>
34 #include <linux/compiler.h>
38 DECLARE_GLOBAL_DATA_PTR;
41 * Timeout the transfer after 5 mS. This is usually a bit more, since
42 * the code in the tightloops this timeout is used in adds some overhead.
44 #define FEC_XFER_TIMEOUT 5000
47 #error "CONFIG_MII has to be defined!"
50 #ifndef CONFIG_FEC_XCV_TYPE
51 #define CONFIG_FEC_XCV_TYPE MII100
55 * The i.MX28 operates with packets in big endian. We need to swap them before
56 * sending and after receiving.
59 #define CONFIG_FEC_MXC_SWAP_PACKET
62 #define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
64 /* Check various alignment issues at compile time */
65 #if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
66 #error "ARCH_DMA_MINALIGN must be multiple of 16!"
69 #if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
70 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
71 #error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
77 uint8_t data[1500]; /**< actual data */
78 int length; /**< actual length */
79 int used; /**< buffer in use or not */
80 uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
85 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
86 static void swap_packet(uint32_t *packet, int length)
90 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
91 packet[i] = __swab32(packet[i]);
96 * MII-interface related functions
98 static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
101 uint32_t reg; /* convenient holder for the PHY register */
102 uint32_t phy; /* convenient holder for the PHY */
107 * reading from any PHY's register is done by properly
108 * programming the FEC's MII data register.
110 writel(FEC_IEVENT_MII, ð->ievent);
111 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
112 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
114 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
115 phy | reg, ð->mii_data);
118 * wait for the related interrupt
120 start = get_timer(0);
121 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
122 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
123 if (readl(ð->ievent) & FEC_IEVENT_MII)
125 printf("Read MDIO failed...\n");
131 * clear mii interrupt bit
133 writel(FEC_IEVENT_MII, ð->ievent);
136 * it's now safe to read the PHY's register
138 val = (unsigned short)readl(ð->mii_data);
139 debug("%s: phy: %02x reg:%02x val:%#06x\n", __func__, phyAddr,
144 static void fec_mii_setspeed(struct fec_priv *fec)
147 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
148 * and do not drop the Preamble.
150 writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
151 &fec->eth->mii_speed);
152 debug("%s: mii_speed %08x\n", __func__, readl(&fec->eth->mii_speed));
155 static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
156 uint8_t regAddr, uint16_t data)
158 uint32_t reg; /* convenient holder for the PHY register */
159 uint32_t phy; /* convenient holder for the PHY */
162 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
163 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
165 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
166 FEC_MII_DATA_TA | phy | reg | data, ð->mii_data);
169 * wait for the MII interrupt
171 start = get_timer(0);
172 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
173 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
174 if (readl(ð->ievent) & FEC_IEVENT_MII)
176 printf("Write MDIO failed...\n");
182 * clear MII interrupt bit
184 writel(FEC_IEVENT_MII, ð->ievent);
185 debug("%s: phy: %02x reg:%02x val:%#06x\n", __func__, phyAddr,
191 int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
193 return fec_mdio_read(bus->priv, phyAddr, regAddr);
196 int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
199 return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
202 #ifndef CONFIG_PHYLIB
203 static int miiphy_restart_aneg(struct eth_device *dev)
206 #if !defined(CONFIG_FEC_MXC_NO_ANEG)
207 struct fec_priv *fec = (struct fec_priv *)dev->priv;
208 struct ethernet_regs *eth = fec->bus->priv;
211 * Wake up from sleep if necessary
212 * Reset PHY, then delay 300ns
215 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
217 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
221 * Set the auto-negotiation advertisement register bits
223 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
224 LPA_100FULL | LPA_100HALF | LPA_10FULL |
225 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
226 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
227 BMCR_ANENABLE | BMCR_ANRESTART);
229 if (fec->mii_postcall)
230 ret = fec->mii_postcall(fec->phy_id);
236 static int miiphy_wait_aneg(struct eth_device *dev)
240 struct fec_priv *fec = (struct fec_priv *)dev->priv;
241 struct ethernet_regs *eth = fec->bus->priv;
244 * Wait for AN completion
246 start = get_timer(0);
248 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
249 printf("%s: Autonegotiation timeout\n", dev->name);
253 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
255 printf("%s: Autonegotiation failed. status: %d\n",
259 } while (!(status & BMSR_LSTATUS));
265 static inline void fec_rx_task_enable(struct fec_priv *fec)
267 writel(1 << 24, &fec->eth->r_des_active);
270 static inline void fec_rx_task_disable(struct fec_priv *fec)
274 static inline void fec_tx_task_enable(struct fec_priv *fec)
276 writel(1 << 24, &fec->eth->x_des_active);
279 static inline void fec_tx_task_disable(struct fec_priv *fec)
284 * Initialize receive task's buffer descriptors
285 * @param[in] fec all we know about the device yet
286 * @param[in] count receive buffer count to be allocated
287 * @param[in] dsize desired size of each receive buffer
288 * @return 0 on success
290 * For this task we need additional memory for the data buffers. And each
291 * data buffer requires some alignment. Thy must be aligned to a specific
294 static int fec_rbd_init(struct fec_priv *fec, int count, int dsize)
300 * Allocate memory for the buffers. This allocation respects the
303 size = roundup(dsize, ARCH_DMA_MINALIGN);
304 for (i = 0; i < count; i++) {
305 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
307 uint8_t *data = memalign(ARCH_DMA_MINALIGN,
310 printf("%s: error allocating rxbuf %d\n",
314 writel((uint32_t)data, &fec->rbd_base[i].data_pointer);
315 } /* needs allocation */
316 writew(FEC_RBD_EMPTY, &fec->rbd_base[i].status);
317 writew(0, &fec->rbd_base[i].data_length);
320 /* Mark the last RBD to close the ring. */
321 writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[i - 1].status);
327 for (; i >= 0; i--) {
328 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
329 free((void *)data_ptr);
336 * Initialize transmit task's buffer descriptors
337 * @param[in] fec all we know about the device yet
339 * Transmit buffers are created externally. We only have to init the BDs here.\n
340 * Note: There is a race condition in the hardware. When only one BD is in
341 * use it must be marked with the WRAP bit to use it for every transmitt.
342 * This bit in combination with the READY bit results into double transmit
343 * of each data buffer. It seems the state machine checks READY earlier then
344 * resetting it after the first transfer.
345 * Using two BDs solves this issue.
347 static void fec_tbd_init(struct fec_priv *fec)
349 unsigned addr = (unsigned)fec->tbd_base;
350 unsigned size = roundup(2 * sizeof(struct fec_bd),
352 writew(0x0000, &fec->tbd_base[0].status);
353 writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
355 flush_dcache_range(addr, addr + size);
359 * Mark the given read buffer descriptor as free
360 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
361 * @param[in] pRbd buffer descriptor to mark free again
363 static void fec_rbd_clean(int last, struct fec_bd *pRbd)
365 unsigned short flags = FEC_RBD_EMPTY;
367 flags |= FEC_RBD_WRAP;
368 writew(flags, &pRbd->status);
369 writew(0, &pRbd->data_length);
372 static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
375 imx_get_mac_from_fuse(dev_id, mac);
376 return !is_valid_ether_addr(mac);
379 static int fec_set_hwaddr(struct eth_device *dev)
381 uchar *mac = dev->enetaddr;
382 struct fec_priv *fec = (struct fec_priv *)dev->priv;
384 writel(0, &fec->eth->iaddr1);
385 writel(0, &fec->eth->iaddr2);
386 writel(0, &fec->eth->gaddr1);
387 writel(0, &fec->eth->gaddr2);
390 * Set physical address
392 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
394 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
399 static void fec_eth_phy_config(struct eth_device *dev)
402 struct fec_priv *fec = (struct fec_priv *)dev->priv;
403 struct phy_device *phydev;
405 if (fec->phy_id < 0) {
408 for (phy_id = 0; phy_id < 32; phy_id++) {
409 debug("%s: Probing PHY ID %02x\n", __func__, phy_id);
410 phydev = phy_connect(fec->bus, phy_id, dev,
411 PHY_INTERFACE_MODE_RGMII);
417 phydev = phy_connect(fec->bus, fec->phy_id, dev,
418 PHY_INTERFACE_MODE_RGMII);
421 fec->phydev = phydev;
428 * Do initial configuration of the FEC registers
430 static void fec_reg_setup(struct fec_priv *fec)
435 * Set interrupt mask register
437 writel(0x00000000, &fec->eth->imask);
440 * Clear FEC-Lite interrupt event register(IEVENT)
442 writel(0xffffffff, &fec->eth->ievent);
446 * Set FEC-Lite receive control register(R_CNTRL):
449 /* Start with frame length = 1518, common for all modes. */
450 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
451 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
452 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
453 if (fec->xcv_type == RGMII)
454 rcntrl |= FEC_RCNTRL_RGMII;
455 else if (fec->xcv_type == RMII)
456 rcntrl |= FEC_RCNTRL_RMII;
458 writel(rcntrl, &fec->eth->r_cntrl);
462 * Start the FEC engine
463 * @param[in] dev Our device to handle
465 static int fec_open(struct eth_device *edev)
467 struct fec_priv *fec = edev->priv;
472 debug("fec_open: fec_open(dev)\n");
473 /* full-duplex, heartbeat disabled */
474 writel(1 << 2, &fec->eth->x_cntrl);
477 /* Invalidate all descriptors */
478 for (i = 0; i < FEC_RBD_NUM - 1; i++)
479 fec_rbd_clean(0, &fec->rbd_base[i]);
480 fec_rbd_clean(1, &fec->rbd_base[i]);
482 /* Flush the descriptors into RAM */
483 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
485 addr = (uint32_t)fec->rbd_base;
486 flush_dcache_range(addr, addr + size);
488 #ifdef FEC_QUIRK_ENET_MAC
489 /* Enable ENET HW endian SWAP */
490 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
492 /* Enable ENET store and forward mode */
493 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
497 * Enable FEC-Lite controller
499 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
501 #if defined(CONFIG_MX25) || defined(CONFIG_MX53)
504 * setup the MII gasket for RMII mode
507 /* disable the gasket */
508 writew(0, &fec->eth->miigsk_enr);
510 /* wait for the gasket to be disabled */
511 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
514 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
515 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
517 /* re-enable the gasket */
518 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
520 /* wait until MII gasket is ready */
522 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
523 if (--max_loops <= 0) {
524 printf("WAIT for MII Gasket ready timed out\n");
532 fec_eth_phy_config(edev);
534 /* Start up the PHY */
535 int ret = phy_startup(fec->phydev);
538 printf("Could not initialize PHY %s\n",
539 fec->phydev->dev->name);
542 speed = fec->phydev->speed;
547 miiphy_wait_aneg(edev);
548 speed = miiphy_speed(edev->name, fec->phy_id);
549 miiphy_duplex(edev->name, fec->phy_id);
552 #ifdef FEC_QUIRK_ENET_MAC
554 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
555 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
557 if (speed == _1000BASET)
558 ecr |= FEC_ECNTRL_SPEED;
559 else if (speed != _100BASET)
560 rcr |= FEC_RCNTRL_RMII_10T;
561 writel(ecr, &fec->eth->ecntrl);
562 writel(rcr, &fec->eth->r_cntrl);
565 debug("%s:Speed=%i\n", __func__, speed);
568 * Enable SmartDMA receive task
570 fec_rx_task_enable(fec);
576 static int fec_init(struct eth_device *dev, bd_t* bd)
578 struct fec_priv *fec = dev->priv;
579 uint32_t *mib_ptr = (uint32_t *)&fec->eth->rmon_t_drop;
583 /* Initialize MAC address */
587 * Allocate transmit descriptors, there are two in total. This
588 * allocation respects cache alignment.
590 if (!fec->tbd_base) {
591 size = roundup(2 * sizeof(struct fec_bd),
593 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
594 if (!fec->tbd_base) {
598 memset(fec->tbd_base, 0, size);
600 flush_dcache_range((unsigned)fec->tbd_base, size);
604 * Allocate receive descriptors. This allocation respects cache
607 if (!fec->rbd_base) {
608 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
610 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
611 if (!fec->rbd_base) {
615 memset(fec->rbd_base, 0, size);
617 * Initialize RxBD ring
619 if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
623 flush_dcache_range((unsigned)fec->rbd_base,
624 (unsigned)fec->rbd_base + size);
629 if (fec->xcv_type != SEVENWIRE)
630 fec_mii_setspeed(fec);
633 * Set Opcode/Pause Duration Register
635 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
636 writel(0x2, &fec->eth->x_wmrk);
638 * Set multicast address filter
640 writel(0x00000000, &fec->eth->gaddr1);
641 writel(0x00000000, &fec->eth->gaddr2);
645 for (i = 0; i <= 0xfc >> 2; i++)
646 writel(0, &mib_ptr[i]);
648 /* FIFO receive start register */
649 writel(0x520, &fec->eth->r_fstart);
651 /* size and address of each buffer */
652 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
653 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
654 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
656 #ifndef CONFIG_PHYLIB
657 if (fec->xcv_type != SEVENWIRE)
658 miiphy_restart_aneg(dev);
672 * Halt the FEC engine
673 * @param[in] dev Our device to handle
675 static void fec_halt(struct eth_device *dev)
677 struct fec_priv *fec = (struct fec_priv *)dev->priv;
681 * issue graceful stop command to the FEC transmitter if necessary
683 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
686 debug("eth_halt: wait for stop regs\n");
688 * wait for graceful stop to register
690 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
694 * Disable SmartDMA tasks
696 fec_tx_task_disable(fec);
697 fec_rx_task_disable(fec);
700 * Disable the Ethernet Controller
701 * Note: this will also reset the BD index counter!
703 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
707 debug("eth_halt: done\n");
712 * @param[in] dev Our ethernet device to handle
713 * @param[in] packet Pointer to the data to be transmitted
714 * @param[in] length Data count in bytes
715 * @return 0 on success
717 static int fec_send(struct eth_device *dev, void *packet, int length)
722 int timeout = FEC_XFER_TIMEOUT;
725 * This routine transmits one frame. This routine only accepts
726 * 6-byte Ethernet addresses.
728 struct fec_priv *fec = dev->priv;
731 * Check for valid length of data.
733 if ((length > 1500) || (length <= 0)) {
734 printf("Payload (%d) too large\n", length);
739 * Setup the transmit buffer. We are always using the first buffer for
740 * transmission, the second will be empty and only used to stop the DMA
741 * engine. We also flush the packet to RAM here to avoid cache trouble.
743 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
744 swap_packet((uint32_t *)packet, length);
747 addr = (uint32_t)packet;
748 end = roundup(addr + length, ARCH_DMA_MINALIGN);
749 addr &= ~(ARCH_DMA_MINALIGN - 1);
750 flush_dcache_range(addr, end);
752 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
753 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
756 * update BD's status now
758 * - is always the last in a chain (means no chain)
759 * - should transmit the CRC
760 * - might be the last BD in the list, so the address counter should
761 * wrap (-> keep the WRAP flag)
763 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
764 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
765 writew(status, &fec->tbd_base[fec->tbd_index].status);
768 * Flush data cache. This code flushes both TX descriptors to RAM.
769 * After this code, the descriptors will be safely in RAM and we
772 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
773 addr = (uint32_t)fec->tbd_base;
774 flush_dcache_range(addr, addr + size);
777 * Enable SmartDMA transmit task
779 fec_tx_task_enable(fec);
782 * Wait until frame is sent. On each turn of the wait cycle, we must
783 * invalidate data cache to see what's really in RAM. Also, we need
786 invalidate_dcache_range(addr, addr + size);
787 while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
791 invalidate_dcache_range(addr, addr + size);
794 debug("fec_send: status 0x%04x index %d\n",
795 readw(&fec->tbd_base[fec->tbd_index].status),
797 /* for next transmission use the other buffer */
807 * Pull one frame from the card
808 * @param[in] dev Our ethernet device to handle
809 * @return Length of packet read
811 static int fec_recv(struct eth_device *dev)
813 struct fec_priv *fec = (struct fec_priv *)dev->priv;
814 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
815 unsigned long ievent;
816 int frame_length, len = 0;
819 uint32_t addr, size, end;
823 * Check if any critical events have happened
825 ievent = readl(&fec->eth->ievent);
827 writel(ievent, &fec->eth->ievent);
830 debug("fec_recv: ievent 0x%lx\n", ievent);
831 if (ievent & FEC_IEVENT_BABR) {
833 fec_init(dev, fec->bd);
834 printf("some error: 0x%08lx\n", ievent);
837 if (ievent & FEC_IEVENT_HBERR) {
838 /* Heartbeat error */
839 writel(0x00000001 | readl(&fec->eth->x_cntrl),
842 if (ievent & FEC_IEVENT_GRA) {
843 /* Graceful stop complete */
844 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
846 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
848 fec_init(dev, fec->bd);
853 * Read the buffer status. Before the status can be read, the data cache
854 * must be invalidated, because the data in RAM might have been changed
855 * by DMA. The descriptors are properly aligned to cachelines so there's
856 * no need to worry they'd overlap.
858 * WARNING: By invalidating the descriptor here, we also invalidate
859 * the descriptors surrounding this one. Therefore we can NOT change the
860 * contents of this descriptor nor the surrounding ones. The problem is
861 * that in order to mark the descriptor as processed, we need to change
862 * the descriptor. The solution is to mark the whole cache line when all
863 * descriptors in the cache line are processed.
865 addr = (uint32_t)rbd;
866 addr &= ~(ARCH_DMA_MINALIGN - 1);
867 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
868 invalidate_dcache_range(addr, addr + size);
870 bd_status = readw(&rbd->status);
871 if (!(bd_status & FEC_RBD_EMPTY)) {
872 debug("fec_recv: status 0x%04x len %u\n", bd_status,
873 readw(&rbd->data_length) - 4);
874 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
875 ((readw(&rbd->data_length) - 4) > 14)) {
877 * Get buffer address and size
879 frame = (struct nbuf *)readl(&rbd->data_pointer);
880 frame_length = readw(&rbd->data_length) - 4;
883 * Invalidate data cache over the buffer
885 addr = (uint32_t)frame;
886 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
887 addr &= ~(ARCH_DMA_MINALIGN - 1);
888 invalidate_dcache_range(addr, end);
891 * Fill the buffer and pass it to upper layers
893 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
894 swap_packet((uint32_t *)frame->data, frame_length);
896 memcpy((void *)NetRxPackets[rx_idx], frame->data, frame_length);
897 NetReceive(NetRxPackets[rx_idx], frame_length);
898 rx_idx = (rx_idx + 1) % PKTBUFSRX;
901 if (bd_status & FEC_RBD_ERR)
902 printf("error frame: 0x%08lx 0x%08x\n",
903 (ulong)rbd->data_pointer,
908 * Free the current buffer, restart the engine and move forward
909 * to the next buffer. Here we check if the whole cacheline of
910 * descriptors was already processed and if so, we mark it free
913 size = RXDESC_PER_CACHELINE - 1;
914 if ((fec->rbd_index & size) == size) {
915 i = fec->rbd_index - size;
916 addr = (uint32_t)&fec->rbd_base[i];
917 for (; i <= fec->rbd_index ; i++) {
918 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
921 flush_dcache_range(addr,
922 addr + ARCH_DMA_MINALIGN);
925 fec_rx_task_enable(fec);
926 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
927 debug("fec_recv: stop\n");
933 static int fec_probe(bd_t *bd, int dev_id, int phy_id, uint32_t base_addr)
935 struct eth_device *edev;
936 struct fec_priv *fec;
938 unsigned char ethaddr[6];
942 /* create and fill edev struct */
943 edev = calloc(sizeof(struct eth_device), 1);
945 puts("fec_mxc: not enough malloc memory for eth_device\n");
950 fec = calloc(sizeof(struct fec_priv), 1);
952 puts("fec_mxc: not enough malloc memory for fec_priv\n");
958 edev->init = fec_init;
959 edev->send = fec_send;
960 edev->recv = fec_recv;
961 edev->halt = fec_halt;
962 edev->write_hwaddr = fec_set_hwaddr;
964 fec->eth = (struct ethernet_regs *)base_addr;
967 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
970 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
971 start = get_timer(0);
972 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
973 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
974 printf("FEC MXC: Timeout reseting chip\n");
981 fec_mii_setspeed(fec);
984 sprintf(edev->name, "FEC");
987 sprintf(edev->name, "FEC%i", dev_id);
988 fec->dev_id = dev_id;
990 fec->phy_id = phy_id;
994 printf("mdio_alloc failed\n");
998 bus->read = fec_phy_read;
999 bus->write = fec_phy_write;
1000 sprintf(bus->name, edev->name);
1003 * The i.MX28 has two ethernet interfaces, but they are not equal.
1004 * Only the first one can access the MDIO bus.
1006 bus->priv = (struct ethernet_regs *)MXS_ENET0_BASE;
1008 bus->priv = fec->eth;
1010 ret = mdio_register(bus);
1012 printf("mdio_register failed\n");
1020 if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
1022 debug("got MAC address from fuse: %pM\n", ethaddr);
1024 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
1025 memcpy(edev->enetaddr, ethaddr, 6);
1028 fec_eth_phy_config(edev);
1039 #ifndef CONFIG_FEC_MXC_MULTI
1040 int fecmxc_initialize(bd_t *bd)
1044 debug("eth_init: fec_probe(PHY %02x FEC: %08x)\n",
1045 CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
1046 lout = fec_probe(bd, -1, CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
1052 int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1056 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1057 lout = fec_probe(bd, dev_id, phy_id, addr);
1062 #ifndef CONFIG_PHYLIB
1063 int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1065 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1066 fec->mii_postcall = cb;