4 * (C) Copyright 2008 Armadeus Systems nc
8 * SPDX-License-Identifier: GPL-2.0+
17 #include <asm/arch/clock.h>
18 #include <asm/arch/imx-regs.h>
20 #include <asm/errno.h>
21 #include <linux/compiler.h>
23 DECLARE_GLOBAL_DATA_PTR;
26 * Timeout the transfer after 5 mS. This is usually a bit more, since
27 * the code in the tightloops this timeout is used in adds some overhead.
29 #define FEC_XFER_TIMEOUT 5000
32 #error "CONFIG_MII has to be defined!"
35 #ifndef CONFIG_FEC_XCV_TYPE
36 #define CONFIG_FEC_XCV_TYPE MII100
40 * The i.MX28 operates with packets in big endian. We need to swap them before
41 * sending and after receiving.
44 #define CONFIG_FEC_MXC_SWAP_PACKET
47 #define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
49 /* Check various alignment issues at compile time */
50 #if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
51 #error "ARCH_DMA_MINALIGN must be multiple of 16!"
54 #if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
55 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
56 #error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
62 uint8_t data[1500]; /**< actual data */
63 int length; /**< actual length */
64 int used; /**< buffer in use or not */
65 uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
68 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
69 static void swap_packet(uint32_t *packet, int length)
73 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
74 packet[i] = __swab32(packet[i]);
79 * MII-interface related functions
81 static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
84 uint32_t reg; /* convenient holder for the PHY register */
85 uint32_t phy; /* convenient holder for the PHY */
90 * reading from any PHY's register is done by properly
91 * programming the FEC's MII data register.
93 writel(FEC_IEVENT_MII, ð->ievent);
94 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
95 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
97 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
98 phy | reg, ð->mii_data);
101 * wait for the related interrupt
103 start = get_timer(0);
104 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
105 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
106 printf("Read MDIO failed...\n");
112 * clear mii interrupt bit
114 writel(FEC_IEVENT_MII, ð->ievent);
117 * it's now safe to read the PHY's register
119 val = (unsigned short)readl(ð->mii_data);
120 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
125 static void fec_mii_setspeed(struct ethernet_regs *eth)
128 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
129 * and do not drop the Preamble.
131 register u32 speed = DIV_ROUND_UP(imx_get_fecclk(), 5000000);
132 #ifdef FEC_QUIRK_ENET_MAC
136 writel(speed, ð->mii_speed);
137 debug("%s: mii_speed %08x\n", __func__, readl(ð->mii_speed));
140 static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
141 uint8_t regAddr, uint16_t data)
143 uint32_t reg; /* convenient holder for the PHY register */
144 uint32_t phy; /* convenient holder for the PHY */
147 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
148 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
150 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
151 FEC_MII_DATA_TA | phy | reg | data, ð->mii_data);
154 * wait for the MII interrupt
156 start = get_timer(0);
157 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
158 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
159 printf("Write MDIO failed...\n");
165 * clear MII interrupt bit
167 writel(FEC_IEVENT_MII, ð->ievent);
168 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
174 int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
176 return fec_mdio_read(bus->priv, phyAddr, regAddr);
179 int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
182 return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
185 #ifndef CONFIG_PHYLIB
186 static int miiphy_restart_aneg(struct eth_device *dev)
189 #if !defined(CONFIG_FEC_MXC_NO_ANEG)
190 struct fec_priv *fec = (struct fec_priv *)dev->priv;
191 struct ethernet_regs *eth = fec->bus->priv;
194 * Wake up from sleep if necessary
195 * Reset PHY, then delay 300ns
198 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
200 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
204 * Set the auto-negotiation advertisement register bits
206 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
207 LPA_100FULL | LPA_100HALF | LPA_10FULL |
208 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
209 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
210 BMCR_ANENABLE | BMCR_ANRESTART);
212 if (fec->mii_postcall)
213 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(FEC_R_DES_ACTIVE_RDAR, &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(FEC_X_DES_ACTIVE_TDAR, &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 * Init all RX descriptors to default values.
279 static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
286 * Reload the RX descriptors with default values and wipe
289 size = roundup(dsize, ARCH_DMA_MINALIGN);
290 for (i = 0; i < count; i++) {
291 data = (uint8_t *)fec->rbd_base[i].data_pointer;
292 memset(data, 0, dsize);
293 flush_dcache_range((uint32_t)data, (uint32_t)data + size);
295 fec->rbd_base[i].status = FEC_RBD_EMPTY;
296 fec->rbd_base[i].data_length = 0;
299 /* Mark the last RBD to close the ring. */
300 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
303 flush_dcache_range((unsigned)fec->rbd_base,
304 (unsigned)fec->rbd_base + size);
308 * Initialize transmit task's buffer descriptors
309 * @param[in] fec all we know about the device yet
311 * Transmit buffers are created externally. We only have to init the BDs here.\n
312 * Note: There is a race condition in the hardware. When only one BD is in
313 * use it must be marked with the WRAP bit to use it for every transmitt.
314 * This bit in combination with the READY bit results into double transmit
315 * of each data buffer. It seems the state machine checks READY earlier then
316 * resetting it after the first transfer.
317 * Using two BDs solves this issue.
319 static void fec_tbd_init(struct fec_priv *fec)
321 unsigned addr = (unsigned)fec->tbd_base;
322 unsigned size = roundup(2 * sizeof(struct fec_bd),
325 memset(fec->tbd_base, 0, size);
326 fec->tbd_base[0].status = 0;
327 fec->tbd_base[1].status = FEC_TBD_WRAP;
329 flush_dcache_range(addr, addr + size);
333 * Mark the given read buffer descriptor as free
334 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
335 * @param[in] pRbd buffer descriptor to mark free again
337 static void fec_rbd_clean(int last, struct fec_bd *pRbd)
339 unsigned short flags = FEC_RBD_EMPTY;
341 flags |= FEC_RBD_WRAP;
342 writew(flags, &pRbd->status);
343 writew(0, &pRbd->data_length);
346 static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
349 imx_get_mac_from_fuse(dev_id, mac);
350 return !is_valid_ether_addr(mac);
353 static int fec_set_hwaddr(struct eth_device *dev)
355 uchar *mac = dev->enetaddr;
356 struct fec_priv *fec = (struct fec_priv *)dev->priv;
358 writel(0, &fec->eth->iaddr1);
359 writel(0, &fec->eth->iaddr2);
360 writel(0, &fec->eth->gaddr1);
361 writel(0, &fec->eth->gaddr2);
364 * Set physical address
366 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
368 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
374 * Do initial configuration of the FEC registers
376 static void fec_reg_setup(struct fec_priv *fec)
381 * Set interrupt mask register
383 writel(0x00000000, &fec->eth->imask);
386 * Clear FEC-Lite interrupt event register(IEVENT)
388 writel(0xffffffff, &fec->eth->ievent);
392 * Set FEC-Lite receive control register(R_CNTRL):
395 /* Start with frame length = 1518, common for all modes. */
396 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
397 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
398 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
399 if (fec->xcv_type == RGMII)
400 rcntrl |= FEC_RCNTRL_RGMII;
401 else if (fec->xcv_type == RMII)
402 rcntrl |= FEC_RCNTRL_RMII;
404 writel(rcntrl, &fec->eth->r_cntrl);
408 * Start the FEC engine
409 * @param[in] dev Our device to handle
411 static int fec_open(struct eth_device *edev)
413 struct fec_priv *fec = (struct fec_priv *)edev->priv;
418 debug("fec_open: fec_open(dev)\n");
419 /* full-duplex, heartbeat disabled */
420 writel(1 << 2, &fec->eth->x_cntrl);
423 /* Invalidate all descriptors */
424 for (i = 0; i < FEC_RBD_NUM - 1; i++)
425 fec_rbd_clean(0, &fec->rbd_base[i]);
426 fec_rbd_clean(1, &fec->rbd_base[i]);
428 /* Flush the descriptors into RAM */
429 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
431 addr = (uint32_t)fec->rbd_base;
432 flush_dcache_range(addr, addr + size);
434 #ifdef FEC_QUIRK_ENET_MAC
435 /* Enable ENET HW endian SWAP */
436 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
438 /* Enable ENET store and forward mode */
439 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
443 * Enable FEC-Lite controller
445 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
447 #if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
450 * setup the MII gasket for RMII mode
453 /* disable the gasket */
454 writew(0, &fec->eth->miigsk_enr);
456 /* wait for the gasket to be disabled */
457 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
460 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
461 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
463 /* re-enable the gasket */
464 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
466 /* wait until MII gasket is ready */
468 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
469 if (--max_loops <= 0) {
470 printf("WAIT for MII Gasket ready timed out\n");
478 /* Start up the PHY */
479 int ret = phy_startup(fec->phydev);
482 printf("Could not initialize PHY %s\n",
483 fec->phydev->dev->name);
486 speed = fec->phydev->speed;
489 miiphy_wait_aneg(edev);
490 speed = miiphy_speed(edev->name, fec->phy_id);
491 miiphy_duplex(edev->name, fec->phy_id);
494 #ifdef FEC_QUIRK_ENET_MAC
496 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
497 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
498 if (speed == _1000BASET)
499 ecr |= FEC_ECNTRL_SPEED;
500 else if (speed != _100BASET)
501 rcr |= FEC_RCNTRL_RMII_10T;
502 writel(ecr, &fec->eth->ecntrl);
503 writel(rcr, &fec->eth->r_cntrl);
506 debug("%s:Speed=%i\n", __func__, speed);
509 * Enable SmartDMA receive task
511 fec_rx_task_enable(fec);
517 static int fec_init(struct eth_device *dev, bd_t* bd)
519 struct fec_priv *fec = (struct fec_priv *)dev->priv;
520 uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
523 /* Initialize MAC address */
527 * Setup transmit descriptors, there are two in total.
531 /* Setup receive descriptors. */
532 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
536 if (fec->xcv_type != SEVENWIRE)
537 fec_mii_setspeed(fec->bus->priv);
540 * Set Opcode/Pause Duration Register
542 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
543 writel(0x2, &fec->eth->x_wmrk);
545 * Set multicast address filter
547 writel(0x00000000, &fec->eth->gaddr1);
548 writel(0x00000000, &fec->eth->gaddr2);
552 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
555 /* FIFO receive start register */
556 writel(0x520, &fec->eth->r_fstart);
558 /* size and address of each buffer */
559 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
560 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
561 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
563 #ifndef CONFIG_PHYLIB
564 if (fec->xcv_type != SEVENWIRE)
565 miiphy_restart_aneg(dev);
572 * Halt the FEC engine
573 * @param[in] dev Our device to handle
575 static void fec_halt(struct eth_device *dev)
577 struct fec_priv *fec = (struct fec_priv *)dev->priv;
578 int counter = 0xffff;
581 * issue graceful stop command to the FEC transmitter if necessary
583 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
586 debug("eth_halt: wait for stop regs\n");
588 * wait for graceful stop to register
590 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
594 * Disable SmartDMA tasks
596 fec_tx_task_disable(fec);
597 fec_rx_task_disable(fec);
600 * Disable the Ethernet Controller
601 * Note: this will also reset the BD index counter!
603 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
607 debug("eth_halt: done\n");
612 * @param[in] dev Our ethernet device to handle
613 * @param[in] packet Pointer to the data to be transmitted
614 * @param[in] length Data count in bytes
615 * @return 0 on success
617 static int fec_send(struct eth_device *dev, void *packet, int length)
622 int timeout = FEC_XFER_TIMEOUT;
626 * This routine transmits one frame. This routine only accepts
627 * 6-byte Ethernet addresses.
629 struct fec_priv *fec = (struct fec_priv *)dev->priv;
632 * Check for valid length of data.
634 if ((length > 1500) || (length <= 0)) {
635 printf("Payload (%d) too large\n", length);
640 * Setup the transmit buffer. We are always using the first buffer for
641 * transmission, the second will be empty and only used to stop the DMA
642 * engine. We also flush the packet to RAM here to avoid cache trouble.
644 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
645 swap_packet((uint32_t *)packet, length);
648 addr = (uint32_t)packet;
649 end = roundup(addr + length, ARCH_DMA_MINALIGN);
650 addr &= ~(ARCH_DMA_MINALIGN - 1);
651 flush_dcache_range(addr, end);
653 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
654 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
657 * update BD's status now
659 * - is always the last in a chain (means no chain)
660 * - should transmitt the CRC
661 * - might be the last BD in the list, so the address counter should
662 * wrap (-> keep the WRAP flag)
664 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
665 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
666 writew(status, &fec->tbd_base[fec->tbd_index].status);
669 * Flush data cache. This code flushes both TX descriptors to RAM.
670 * After this code, the descriptors will be safely in RAM and we
673 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
674 addr = (uint32_t)fec->tbd_base;
675 flush_dcache_range(addr, addr + size);
678 * Below we read the DMA descriptor's last four bytes back from the
679 * DRAM. This is important in order to make sure that all WRITE
680 * operations on the bus that were triggered by previous cache FLUSH
683 * Otherwise, on MX28, it is possible to observe a corruption of the
684 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
685 * for the bus structure of MX28. The scenario is as follows:
687 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
688 * to DRAM due to flush_dcache_range()
689 * 2) ARM core writes the FEC registers via AHB_ARB2
690 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
692 * Note that 2) does sometimes finish before 1) due to reordering of
693 * WRITE accesses on the AHB bus, therefore triggering 3) before the
694 * DMA descriptor is fully written into DRAM. This results in occasional
695 * corruption of the DMA descriptor.
697 readl(addr + size - 4);
700 * Enable SmartDMA transmit task
702 fec_tx_task_enable(fec);
705 * Wait until frame is sent. On each turn of the wait cycle, we must
706 * invalidate data cache to see what's really in RAM. Also, we need
710 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
717 invalidate_dcache_range(addr, addr + size);
718 if (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY)
721 debug("fec_send: status 0x%x index %d ret %i\n",
722 readw(&fec->tbd_base[fec->tbd_index].status),
723 fec->tbd_index, ret);
724 /* for next transmission use the other buffer */
734 * Pull one frame from the card
735 * @param[in] dev Our ethernet device to handle
736 * @return Length of packet read
738 static int fec_recv(struct eth_device *dev)
740 struct fec_priv *fec = (struct fec_priv *)dev->priv;
741 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
742 unsigned long ievent;
743 int frame_length, len = 0;
746 uint32_t addr, size, end;
748 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
751 * Check if any critical events have happened
753 ievent = readl(&fec->eth->ievent);
754 writel(ievent, &fec->eth->ievent);
755 debug("fec_recv: ievent 0x%lx\n", ievent);
756 if (ievent & FEC_IEVENT_BABR) {
758 fec_init(dev, fec->bd);
759 printf("some error: 0x%08lx\n", ievent);
762 if (ievent & FEC_IEVENT_HBERR) {
763 /* Heartbeat error */
764 writel(0x00000001 | readl(&fec->eth->x_cntrl),
767 if (ievent & FEC_IEVENT_GRA) {
768 /* Graceful stop complete */
769 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
771 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
773 fec_init(dev, fec->bd);
778 * Read the buffer status. Before the status can be read, the data cache
779 * must be invalidated, because the data in RAM might have been changed
780 * by DMA. The descriptors are properly aligned to cachelines so there's
781 * no need to worry they'd overlap.
783 * WARNING: By invalidating the descriptor here, we also invalidate
784 * the descriptors surrounding this one. Therefore we can NOT change the
785 * contents of this descriptor nor the surrounding ones. The problem is
786 * that in order to mark the descriptor as processed, we need to change
787 * the descriptor. The solution is to mark the whole cache line when all
788 * descriptors in the cache line are processed.
790 addr = (uint32_t)rbd;
791 addr &= ~(ARCH_DMA_MINALIGN - 1);
792 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
793 invalidate_dcache_range(addr, addr + size);
795 bd_status = readw(&rbd->status);
796 debug("fec_recv: status 0x%x\n", bd_status);
798 if (!(bd_status & FEC_RBD_EMPTY)) {
799 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
800 ((readw(&rbd->data_length) - 4) > 14)) {
802 * Get buffer address and size
804 frame = (struct nbuf *)readl(&rbd->data_pointer);
805 frame_length = readw(&rbd->data_length) - 4;
807 * Invalidate data cache over the buffer
809 addr = (uint32_t)frame;
810 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
811 addr &= ~(ARCH_DMA_MINALIGN - 1);
812 invalidate_dcache_range(addr, end);
815 * Fill the buffer and pass it to upper layers
817 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
818 swap_packet((uint32_t *)frame->data, frame_length);
820 memcpy(buff, frame->data, frame_length);
821 NetReceive(buff, frame_length);
824 if (bd_status & FEC_RBD_ERR)
825 printf("error frame: 0x%08lx 0x%08x\n",
826 (ulong)rbd->data_pointer,
831 * Free the current buffer, restart the engine and move forward
832 * to the next buffer. Here we check if the whole cacheline of
833 * descriptors was already processed and if so, we mark it free
836 size = RXDESC_PER_CACHELINE - 1;
837 if ((fec->rbd_index & size) == size) {
838 i = fec->rbd_index - size;
839 addr = (uint32_t)&fec->rbd_base[i];
840 for (; i <= fec->rbd_index ; i++) {
841 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
844 flush_dcache_range(addr,
845 addr + ARCH_DMA_MINALIGN);
848 fec_rx_task_enable(fec);
849 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
851 debug("fec_recv: stop\n");
856 static void fec_set_dev_name(char *dest, int dev_id)
858 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
861 static int fec_alloc_descs(struct fec_priv *fec)
867 /* Allocate TX descriptors. */
868 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
869 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
873 /* Allocate RX descriptors. */
874 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
875 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
879 memset(fec->rbd_base, 0, size);
881 /* Allocate RX buffers. */
883 /* Maximum RX buffer size. */
884 size = roundup(FEC_MAX_PKT_SIZE, ARCH_DMA_MINALIGN);
885 for (i = 0; i < FEC_RBD_NUM; i++) {
886 data = memalign(ARCH_DMA_MINALIGN, size);
888 printf("%s: error allocating rxbuf %d\n", __func__, i);
892 memset(data, 0, size);
894 fec->rbd_base[i].data_pointer = (uint32_t)data;
895 fec->rbd_base[i].status = FEC_RBD_EMPTY;
896 fec->rbd_base[i].data_length = 0;
897 /* Flush the buffer to memory. */
898 flush_dcache_range((uint32_t)data, (uint32_t)data + size);
901 /* Mark the last RBD to close the ring. */
902 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
911 free((void *)fec->rbd_base[i].data_pointer);
919 static void fec_free_descs(struct fec_priv *fec)
923 for (i = 0; i < FEC_RBD_NUM; i++)
924 free((void *)fec->rbd_base[i].data_pointer);
930 int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
931 struct mii_dev *bus, struct phy_device *phydev)
933 static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
934 struct mii_dev *bus, int phy_id)
937 struct eth_device *edev;
938 struct fec_priv *fec;
939 unsigned char ethaddr[6];
943 /* create and fill edev struct */
944 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
946 puts("fec_mxc: not enough malloc memory for eth_device\n");
951 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
953 puts("fec_mxc: not enough malloc memory for fec_priv\n");
958 memset(edev, 0, sizeof(*edev));
959 memset(fec, 0, sizeof(*fec));
961 ret = fec_alloc_descs(fec);
966 edev->init = fec_init;
967 edev->send = fec_send;
968 edev->recv = fec_recv;
969 edev->halt = fec_halt;
970 edev->write_hwaddr = fec_set_hwaddr;
972 fec->eth = (struct ethernet_regs *)base_addr;
975 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
978 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
979 start = get_timer(0);
980 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
981 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
982 printf("FEC MXC: Timeout reseting chip\n");
989 fec_set_dev_name(edev->name, dev_id);
990 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
992 fec_mii_setspeed(bus->priv);
994 fec->phydev = phydev;
995 phy_connect_dev(phydev, edev);
999 fec->phy_id = phy_id;
1003 if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
1004 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
1005 memcpy(edev->enetaddr, ethaddr, 6);
1006 if (!getenv("ethaddr"))
1007 eth_setenv_enetaddr("ethaddr", ethaddr);
1011 fec_free_descs(fec);
1020 struct mii_dev *fec_get_miibus(uint32_t base_addr, int dev_id)
1022 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
1023 struct mii_dev *bus;
1028 printf("mdio_alloc failed\n");
1031 bus->read = fec_phy_read;
1032 bus->write = fec_phy_write;
1034 fec_set_dev_name(bus->name, dev_id);
1036 ret = mdio_register(bus);
1038 printf("mdio_register failed\n");
1042 fec_mii_setspeed(eth);
1046 int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1049 struct mii_dev *bus = NULL;
1050 #ifdef CONFIG_PHYLIB
1051 struct phy_device *phydev = NULL;
1057 * The i.MX28 has two ethernet interfaces, but they are not equal.
1058 * Only the first one can access the MDIO bus.
1060 base_mii = MXS_ENET0_BASE;
1064 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1065 bus = fec_get_miibus(base_mii, dev_id);
1068 #ifdef CONFIG_PHYLIB
1069 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
1074 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1076 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
1079 #ifdef CONFIG_PHYLIB
1087 #ifdef CONFIG_FEC_MXC_PHYADDR
1088 int fecmxc_initialize(bd_t *bd)
1090 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1095 #ifndef CONFIG_PHYLIB
1096 int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1098 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1099 fec->mii_postcall = cb;