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
45 #if !defined(CONDIF_SYS_DCACHE_OFF) && !defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
46 /* Due to multiple RX and TX buffer descriptors sharing a cache line
47 * the driver can only work with DMA coherent memory.
48 * Since U-Boot does not provide this, cache must be disabled or
51 #error This driver cannot be used with Writeback DCACHE
54 * The i.MX28 operates with packets in big endian. We need to swap them before
55 * sending and after receiving.
58 #define CONFIG_FEC_MXC_SWAP_PACKET
61 #define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
63 /* Check various alignment issues at compile time */
64 #if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
65 #error "ARCH_DMA_MINALIGN must be multiple of 16!"
68 #if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
69 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
70 #error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
76 uint8_t data[1500]; /**< actual data */
77 int length; /**< actual length */
78 int used; /**< buffer in use or not */
79 uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
82 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
83 static void swap_packet(uint32_t *packet, int length)
87 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
88 packet[i] = __swab32(packet[i]);
93 * MII-interface related functions
95 static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
98 uint32_t reg; /* convenient holder for the PHY register */
99 uint32_t phy; /* convenient holder for the PHY */
104 * reading from any PHY's register is done by properly
105 * programming the FEC's MII data register.
107 writel(FEC_IEVENT_MII, ð->ievent);
108 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
109 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
111 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
112 phy | reg, ð->mii_data);
115 * wait for the related interrupt
117 start = get_timer(0);
118 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
119 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
120 printf("Read MDIO failed...\n");
126 * clear mii interrupt bit
128 writel(FEC_IEVENT_MII, ð->ievent);
131 * it's now safe to read the PHY's register
133 val = (unsigned short)readl(ð->mii_data);
134 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
139 static void fec_mii_setspeed(struct fec_priv *fec)
142 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
143 * and do not drop the Preamble.
145 writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
146 &fec->eth->mii_speed);
147 debug("%s: mii_speed %08x\n", __func__, readl(&fec->eth->mii_speed));
150 static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
151 uint8_t regAddr, uint16_t data)
153 uint32_t reg; /* convenient holder for the PHY register */
154 uint32_t phy; /* convenient holder for the PHY */
157 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
158 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
160 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
161 FEC_MII_DATA_TA | phy | reg | data, ð->mii_data);
164 * wait for the MII interrupt
166 start = get_timer(0);
167 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
168 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
169 printf("Write MDIO failed...\n");
175 * clear MII interrupt bit
177 writel(FEC_IEVENT_MII, ð->ievent);
178 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
184 int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
186 return fec_mdio_read(bus->priv, phyAddr, regAddr);
189 int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
192 return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
195 #ifndef CONFIG_PHYLIB
196 static int miiphy_restart_aneg(struct eth_device *dev)
199 #if !defined(CONFIG_FEC_MXC_NO_ANEG)
200 struct fec_priv *fec = (struct fec_priv *)dev->priv;
201 struct ethernet_regs *eth = fec->bus->priv;
204 * Wake up from sleep if necessary
205 * Reset PHY, then delay 300ns
208 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
210 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
214 * Set the auto-negotiation advertisement register bits
216 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
217 LPA_100FULL | LPA_100HALF | LPA_10FULL |
218 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
219 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
220 BMCR_ANENABLE | BMCR_ANRESTART);
222 if (fec->mii_postcall)
223 ret = fec->mii_postcall(fec->phy_id);
229 static int miiphy_wait_aneg(struct eth_device *dev)
233 struct fec_priv *fec = (struct fec_priv *)dev->priv;
234 struct ethernet_regs *eth = fec->bus->priv;
237 * Wait for AN completion
239 start = get_timer(0);
241 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
242 printf("%s: Autonegotiation timeout\n", dev->name);
246 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
248 printf("%s: Autonegotiation failed. status: %d\n",
252 } while (!(status & BMSR_LSTATUS));
258 static inline void fec_rx_task_enable(struct fec_priv *fec)
260 writel(1 << 24, &fec->eth->r_des_active);
263 static inline void fec_rx_task_disable(struct fec_priv *fec)
267 static inline void fec_tx_task_enable(struct fec_priv *fec)
269 writel(1 << 24, &fec->eth->x_des_active);
272 static inline void fec_tx_task_disable(struct fec_priv *fec)
276 static inline void fec_invalidate_bd(struct fec_bd *bd)
278 invalidate_dcache_range((unsigned long)bd,
279 (unsigned long)bd + sizeof(*bd));
282 static inline void fec_flush_bd(struct fec_bd *bd)
284 flush_dcache_range((unsigned long)bd,
285 (unsigned long)bd + sizeof(*bd));
289 * Initialize receive task's buffer descriptors
290 * @param[in] fec all we know about the device yet
291 * @param[in] count receive buffer count to be allocated
292 * @param[in] dsize desired size of each receive buffer
293 * @return 0 on success
295 * For this task we need additional memory for the data buffers. And each
296 * data buffer requires some alignment. Thy must be aligned to a specific
299 static int fec_rbd_init(struct fec_priv *fec, int count, int dsize)
305 * Allocate memory for the buffers. This allocation respects the
308 size = roundup(dsize, ARCH_DMA_MINALIGN);
309 for (i = 0; i < count; i++) {
310 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
312 uint8_t *data = memalign(ARCH_DMA_MINALIGN,
315 printf("%s: error allocating rxbuf %d\n",
319 writel((uint32_t)data, &fec->rbd_base[i].data_pointer);
320 } /* needs allocation */
321 writew(FEC_RBD_EMPTY, &fec->rbd_base[i].status);
322 writew(0, &fec->rbd_base[i].data_length);
325 /* Mark the last RBD to close the ring. */
326 writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[i - 1].status);
332 for (; i >= 0; i--) {
333 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
334 free((void *)data_ptr);
341 * Initialize transmit task's buffer descriptors
342 * @param[in] fec all we know about the device yet
344 * Transmit buffers are created externally. We only have to init the BDs here.\n
345 * Note: There is a race condition in the hardware. When only one BD is in
346 * use it must be marked with the WRAP bit to use it for every transmitt.
347 * This bit in combination with the READY bit results into double transmit
348 * of each data buffer. It seems the state machine checks READY earlier then
349 * resetting it after the first transfer.
350 * Using two BDs solves this issue.
352 static void fec_tbd_init(struct fec_priv *fec)
354 unsigned addr = (unsigned)fec->tbd_base;
355 unsigned size = roundup(2 * sizeof(struct fec_bd),
357 writew(0x0000, &fec->tbd_base[0].status);
358 fec_flush_bd(&fec->tbd_base[0]);
359 writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
360 fec_flush_bd(&fec->tbd_base[1]);
362 flush_dcache_range(addr, addr+size);
366 * Mark the given read buffer descriptor as free
367 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
368 * @param[in] pRbd buffer descriptor to mark free again
370 static void fec_rbd_clean(int last, struct fec_bd *pRbd)
372 unsigned short flags = FEC_RBD_EMPTY;
374 flags |= FEC_RBD_WRAP;
375 writew(flags, &pRbd->status);
376 writew(0, &pRbd->data_length);
379 static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
382 imx_get_mac_from_fuse(dev_id, mac);
383 return !is_valid_ether_addr(mac);
386 static int fec_set_hwaddr(struct eth_device *dev)
388 uchar *mac = dev->enetaddr;
389 struct fec_priv *fec = (struct fec_priv *)dev->priv;
391 writel(0, &fec->eth->iaddr1);
392 writel(0, &fec->eth->iaddr2);
393 writel(0, &fec->eth->gaddr1);
394 writel(0, &fec->eth->gaddr2);
397 * Set physical address
399 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
401 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
406 static void fec_eth_phy_config(struct eth_device *dev)
409 struct fec_priv *fec = (struct fec_priv *)dev->priv;
410 struct phy_device *phydev;
412 phydev = phy_connect(fec->bus, fec->phy_id, dev,
413 PHY_INTERFACE_MODE_RGMII);
415 fec->phydev = phydev;
422 * Do initial configuration of the FEC registers
424 static void fec_reg_setup(struct fec_priv *fec)
429 * Set interrupt mask register
431 writel(0x00000000, &fec->eth->imask);
434 * Clear FEC-Lite interrupt event register(IEVENT)
436 writel(0xffffffff, &fec->eth->ievent);
440 * Set FEC-Lite receive control register(R_CNTRL):
443 /* Start with frame length = 1518, common for all modes. */
444 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
445 if (fec->xcv_type == SEVENWIRE)
446 rcntrl |= FEC_RCNTRL_FCE;
447 else if (fec->xcv_type == RGMII)
448 rcntrl |= FEC_RCNTRL_RGMII;
449 else if (fec->xcv_type == RMII)
450 rcntrl |= FEC_RCNTRL_RMII;
452 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
454 writel(rcntrl, &fec->eth->r_cntrl);
458 * Start the FEC engine
459 * @param[in] dev Our device to handle
461 static int fec_open(struct eth_device *edev)
463 struct fec_priv *fec = edev->priv;
468 debug("fec_open: fec_open(dev)\n");
469 /* full-duplex, heartbeat disabled */
470 writel(1 << 2, &fec->eth->x_cntrl);
473 /* Invalidate all descriptors */
474 for (i = 0; i < FEC_RBD_NUM - 1; i++)
475 fec_rbd_clean(0, &fec->rbd_base[i]);
476 fec_rbd_clean(1, &fec->rbd_base[i]);
478 /* Flush the descriptors into RAM */
479 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
481 addr = (uint32_t)fec->rbd_base;
482 flush_dcache_range(addr, addr + size);
484 #ifdef FEC_QUIRK_ENET_MAC
485 /* Enable ENET HW endian SWAP */
486 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
488 /* Enable ENET store and forward mode */
489 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
493 * Enable FEC-Lite controller
495 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
497 #if defined(CONFIG_MX25) || defined(CONFIG_MX53)
500 * setup the MII gasket for RMII mode
503 /* disable the gasket */
504 writew(0, &fec->eth->miigsk_enr);
506 /* wait for the gasket to be disabled */
507 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
510 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
511 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
513 /* re-enable the gasket */
514 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
516 /* wait until MII gasket is ready */
518 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
519 if (--max_loops <= 0) {
520 printf("WAIT for MII Gasket ready timed out\n");
528 fec_eth_phy_config(edev);
530 /* Start up the PHY */
531 phy_startup(fec->phydev);
532 speed = fec->phydev->speed;
537 miiphy_wait_aneg(edev);
538 speed = miiphy_speed(edev->name, fec->phy_id);
539 miiphy_duplex(edev->name, fec->phy_id);
542 #ifdef FEC_QUIRK_ENET_MAC
544 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
545 u32 rcr = (readl(&fec->eth->r_cntrl) &
546 ~(FEC_RCNTRL_RMII | FEC_RCNTRL_RMII_10T)) |
547 FEC_RCNTRL_RGMII | FEC_RCNTRL_MII_MODE;
548 if (speed == _1000BASET)
549 ecr |= FEC_ECNTRL_SPEED;
550 else if (speed != _100BASET)
551 rcr |= FEC_RCNTRL_RMII_10T;
552 writel(ecr, &fec->eth->ecntrl);
553 writel(rcr, &fec->eth->r_cntrl);
556 debug("%s:Speed=%i\n", __func__, speed);
559 * Enable SmartDMA receive task
561 fec_rx_task_enable(fec);
567 static int fec_init(struct eth_device *dev, bd_t* bd)
569 struct fec_priv *fec = (struct fec_priv *)dev->priv;
570 uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
574 /* Initialize MAC address */
578 * Allocate transmit descriptors, there are two in total. This
579 * allocation respects cache alignment.
581 if (!fec->tbd_base) {
582 size = roundup(2 * sizeof(struct fec_bd),
584 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
585 if (!fec->tbd_base) {
589 memset(fec->tbd_base, 0, size);
591 flush_dcache_range((unsigned)fec->tbd_base, size);
595 * Allocate receive descriptors. This allocation respects cache
598 if (!fec->rbd_base) {
599 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
601 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
602 if (!fec->rbd_base) {
606 memset(fec->rbd_base, 0, size);
608 * Initialize RxBD ring
610 if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
614 flush_dcache_range((unsigned)fec->rbd_base,
615 (unsigned)fec->rbd_base + size);
620 if (fec->xcv_type == MII10 || fec->xcv_type == MII100)
621 fec_mii_setspeed(fec);
624 * Set Opcode/Pause Duration Register
626 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
627 writel(0x2, &fec->eth->x_wmrk);
629 * Set multicast address filter
631 writel(0x00000000, &fec->eth->gaddr1);
632 writel(0x00000000, &fec->eth->gaddr2);
636 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
639 /* FIFO receive start register */
640 writel(0x520, &fec->eth->r_fstart);
642 /* size and address of each buffer */
643 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
644 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
645 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
647 #ifndef CONFIG_PHYLIB
648 if (fec->xcv_type != SEVENWIRE)
649 miiphy_restart_aneg(dev);
663 * Halt the FEC engine
664 * @param[in] dev Our device to handle
666 static void fec_halt(struct eth_device *dev)
668 struct fec_priv *fec = (struct fec_priv *)dev->priv;
669 int counter = 0xffff;
672 * issue graceful stop command to the FEC transmitter if necessary
674 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
677 debug("eth_halt: wait for stop regs\n");
679 * wait for graceful stop to register
681 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
685 * Disable SmartDMA tasks
687 fec_tx_task_disable(fec);
688 fec_rx_task_disable(fec);
691 * Disable the Ethernet Controller
692 * Note: this will also reset the BD index counter!
694 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
698 debug("eth_halt: done\n");
703 * @param[in] dev Our ethernet device to handle
704 * @param[in] packet Pointer to the data to be transmitted
705 * @param[in] length Data count in bytes
706 * @return 0 on success
708 static int fec_send(struct eth_device *dev, volatile void *packet, int length)
716 * This routine transmits one frame. This routine only accepts
717 * 6-byte Ethernet addresses.
719 struct fec_priv *fec = dev->priv;
722 * Check for valid length of data.
724 if ((length > 1500) || (length <= 0)) {
725 printf("Payload (%d) too large\n", length);
730 * Setup the transmit buffer. We are always using the first buffer for
731 * transmission, the second will be empty and only used to stop the DMA
732 * engine. We also flush the packet to RAM here to avoid cache trouble.
734 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
735 swap_packet((uint32_t *)packet, length);
738 addr = (uint32_t)packet;
739 size = roundup(length, ARCH_DMA_MINALIGN);
740 flush_dcache_range(addr, addr + size);
742 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
743 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
746 * update BD's status now
748 * - is always the last in a chain (means no chain)
749 * - should transmit the CRC
750 * - might be the last BD in the list, so the address counter should
751 * wrap (-> keep the WRAP flag)
753 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
754 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
755 writew(status, &fec->tbd_base[fec->tbd_index].status);
756 fec_flush_bd(&fec->tbd_base[fec->tbd_index]);
759 * Flush data cache. This code flushes both TX descriptors to RAM.
760 * After this code, the descriptors will be safely in RAM and we
763 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
764 addr = (uint32_t)fec->tbd_base;
765 flush_dcache_range(addr, addr + size);
768 * Enable SmartDMA transmit task
770 fec_tx_task_enable(fec);
773 * Wait until frame is sent. On each turn of the wait cycle, we must
774 * invalidate data cache to see what's really in RAM. Also, we need
777 invalidate_dcache_range(addr, addr + size);
778 while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
782 invalidate_dcache_range(addr, addr + size);
785 debug("fec_send: status 0x%x index %d\n",
786 readw(&fec->tbd_base[fec->tbd_index].status),
788 /* for next transmission use the other buffer */
798 * Pull one frame from the card
799 * @param[in] dev Our ethernet device to handle
800 * @return Length of packet read
802 static int fec_recv(struct eth_device *dev)
804 struct fec_priv *fec = (struct fec_priv *)dev->priv;
805 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
806 unsigned long ievent;
807 int frame_length, len = 0;
812 uchar buff[FEC_MAX_PKT_SIZE];
815 * Check if any critical events have happened
817 ievent = readl(&fec->eth->ievent);
818 writel(ievent, &fec->eth->ievent);
819 debug("fec_recv: ievent 0x%lx\n", ievent);
820 if (ievent & FEC_IEVENT_BABR) {
822 fec_init(dev, fec->bd);
823 printf("some error: 0x%08lx\n", ievent);
826 if (ievent & FEC_IEVENT_HBERR) {
827 /* Heartbeat error */
828 writel(0x00000001 | readl(&fec->eth->x_cntrl),
831 if (ievent & FEC_IEVENT_GRA) {
832 /* Graceful stop complete */
833 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
835 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
837 fec_init(dev, fec->bd);
842 * Read the buffer status. Before the status can be read, the data cache
843 * must be invalidated, because the data in RAM might have been changed
844 * by DMA. The descriptors are properly aligned to cachelines so there's
845 * no need to worry they'd overlap.
847 * WARNING: By invalidating the descriptor here, we also invalidate
848 * the descriptors surrounding this one. Therefore we can NOT change the
849 * contents of this descriptor nor the surrounding ones. The problem is
850 * that in order to mark the descriptor as processed, we need to change
851 * the descriptor. The solution is to mark the whole cache line when all
852 * descriptors in the cache line are processed.
854 addr = (uint32_t)rbd;
855 addr &= ~(ARCH_DMA_MINALIGN - 1);
856 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
857 invalidate_dcache_range(addr, addr + size);
859 bd_status = readw(&rbd->status);
860 debug("fec_recv: status 0x%x\n", bd_status);
862 if (!(bd_status & FEC_RBD_EMPTY)) {
863 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
864 ((readw(&rbd->data_length) - 4) > 14)) {
866 * Get buffer address and size
868 frame = (struct nbuf *)readl(&rbd->data_pointer);
869 frame_length = readw(&rbd->data_length) - 4;
871 invalidate_dcache_range((unsigned long)frame,
872 (unsigned long)frame +
876 * Invalidate data cache over the buffer
878 addr = (uint32_t)frame;
879 size = roundup(frame_length, ARCH_DMA_MINALIGN);
880 invalidate_dcache_range(addr, addr + size);
883 * Fill the buffer and pass it to upper layers
885 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
886 swap_packet((uint32_t *)frame->data, frame_length);
888 memcpy(buff, frame->data, frame_length);
889 NetReceive(buff, frame_length);
892 if (bd_status & FEC_RBD_ERR)
893 printf("error frame: 0x%08lx 0x%08x\n",
894 (ulong)rbd->data_pointer,
899 * Free the current buffer, restart the engine and move forward
900 * to the next buffer. Here we check if the whole cacheline of
901 * descriptors was already processed and if so, we mark it free
904 size = RXDESC_PER_CACHELINE - 1;
905 if ((fec->rbd_index & size) == size) {
906 i = fec->rbd_index - size;
907 addr = (uint32_t)&fec->rbd_base[i];
908 for (; i <= fec->rbd_index ; i++) {
909 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
912 flush_dcache_range(addr,
913 addr + ARCH_DMA_MINALIGN);
916 fec_rx_task_enable(fec);
917 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
919 debug("fec_recv: stop\n");
924 static int fec_probe(bd_t *bd, int dev_id, int phy_id, uint32_t base_addr)
926 struct eth_device *edev;
927 struct fec_priv *fec;
929 unsigned char ethaddr[6];
933 /* create and fill edev struct */
934 edev = calloc(sizeof(struct eth_device), 1);
936 puts("fec_mxc: not enough malloc memory for eth_device\n");
941 fec = calloc(sizeof(struct fec_priv), 1);
943 puts("fec_mxc: not enough malloc memory for fec_priv\n");
949 edev->init = fec_init;
950 edev->send = fec_send;
951 edev->recv = fec_recv;
952 edev->halt = fec_halt;
953 edev->write_hwaddr = fec_set_hwaddr;
955 fec->eth = (struct ethernet_regs *)base_addr;
958 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
961 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
962 start = get_timer(0);
963 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
964 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
965 printf("FEC MXC: Timeout reseting chip\n");
972 fec_mii_setspeed(fec);
975 sprintf(edev->name, "FEC");
978 sprintf(edev->name, "FEC%i", dev_id);
979 fec->dev_id = dev_id;
981 fec->phy_id = phy_id;
985 printf("mdio_alloc failed\n");
989 bus->read = fec_phy_read;
990 bus->write = fec_phy_write;
991 sprintf(bus->name, edev->name);
994 * The i.MX28 has two ethernet interfaces, but they are not equal.
995 * Only the first one can access the MDIO bus.
997 bus->priv = (struct ethernet_regs *)MXS_ENET0_BASE;
999 bus->priv = fec->eth;
1001 ret = mdio_register(bus);
1003 printf("mdio_register failed\n");
1011 if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
1012 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
1013 memcpy(edev->enetaddr, ethaddr, 6);
1016 fec_eth_phy_config(edev);
1027 #ifndef CONFIG_FEC_MXC_MULTI
1028 int fecmxc_initialize(bd_t *bd)
1032 debug("eth_init: fec_probe(bd)\n");
1033 lout = fec_probe(bd, -1, CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
1039 int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1043 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1044 lout = fec_probe(bd, dev_id, phy_id, addr);
1049 #ifndef CONFIG_PHYLIB
1050 int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1052 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1053 fec->mii_postcall = cb;