xref: /rk3399_rockchip-uboot/drivers/net/fec_mxc.c (revision 7df51fd8be413a6b831f0fa7b83cad76b5c4e951)
10b23fb36SIlya Yanok /*
20b23fb36SIlya Yanok  * (C) Copyright 2009 Ilya Yanok, Emcraft Systems Ltd <yanok@emcraft.com>
30b23fb36SIlya Yanok  * (C) Copyright 2008,2009 Eric Jarrige <eric.jarrige@armadeus.org>
40b23fb36SIlya Yanok  * (C) Copyright 2008 Armadeus Systems nc
50b23fb36SIlya Yanok  * (C) Copyright 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
60b23fb36SIlya Yanok  * (C) Copyright 2007 Pengutronix, Juergen Beisert <j.beisert@pengutronix.de>
70b23fb36SIlya Yanok  *
81a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
90b23fb36SIlya Yanok  */
100b23fb36SIlya Yanok 
110b23fb36SIlya Yanok #include <common.h>
120b23fb36SIlya Yanok #include <malloc.h>
130b23fb36SIlya Yanok #include <net.h>
140b23fb36SIlya Yanok #include <miiphy.h>
150b23fb36SIlya Yanok #include "fec_mxc.h"
160b23fb36SIlya Yanok 
170b23fb36SIlya Yanok #include <asm/arch/clock.h>
180b23fb36SIlya Yanok #include <asm/arch/imx-regs.h>
190b23fb36SIlya Yanok #include <asm/io.h>
200b23fb36SIlya Yanok #include <asm/errno.h>
21e2a66e60SMarek Vasut #include <linux/compiler.h>
220b23fb36SIlya Yanok 
230b23fb36SIlya Yanok DECLARE_GLOBAL_DATA_PTR;
240b23fb36SIlya Yanok 
25bc1ce150SMarek Vasut /*
26bc1ce150SMarek Vasut  * Timeout the transfer after 5 mS. This is usually a bit more, since
27bc1ce150SMarek Vasut  * the code in the tightloops this timeout is used in adds some overhead.
28bc1ce150SMarek Vasut  */
29bc1ce150SMarek Vasut #define FEC_XFER_TIMEOUT	5000
30bc1ce150SMarek Vasut 
310b23fb36SIlya Yanok #ifndef CONFIG_MII
320b23fb36SIlya Yanok #error "CONFIG_MII has to be defined!"
330b23fb36SIlya Yanok #endif
340b23fb36SIlya Yanok 
35392b8502SMarek Vasut #ifndef CONFIG_FEC_XCV_TYPE
36392b8502SMarek Vasut #define CONFIG_FEC_XCV_TYPE MII100
37392b8502SMarek Vasut #endif
38392b8502SMarek Vasut 
39be7e87e2SMarek Vasut /*
40be7e87e2SMarek Vasut  * The i.MX28 operates with packets in big endian. We need to swap them before
41be7e87e2SMarek Vasut  * sending and after receiving.
42be7e87e2SMarek Vasut  */
43be7e87e2SMarek Vasut #ifdef CONFIG_MX28
44be7e87e2SMarek Vasut #define CONFIG_FEC_MXC_SWAP_PACKET
45be7e87e2SMarek Vasut #endif
46be7e87e2SMarek Vasut 
475c1ad3e6SEric Nelson #define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
485c1ad3e6SEric Nelson 
495c1ad3e6SEric Nelson /* Check various alignment issues at compile time */
505c1ad3e6SEric Nelson #if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
515c1ad3e6SEric Nelson #error "ARCH_DMA_MINALIGN must be multiple of 16!"
525c1ad3e6SEric Nelson #endif
535c1ad3e6SEric Nelson 
545c1ad3e6SEric Nelson #if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
555c1ad3e6SEric Nelson 	(PKTALIGN % ARCH_DMA_MINALIGN != 0))
565c1ad3e6SEric Nelson #error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
575c1ad3e6SEric Nelson #endif
585c1ad3e6SEric Nelson 
590b23fb36SIlya Yanok #undef DEBUG
600b23fb36SIlya Yanok 
610b23fb36SIlya Yanok struct nbuf {
620b23fb36SIlya Yanok 	uint8_t data[1500];	/**< actual data */
630b23fb36SIlya Yanok 	int length;		/**< actual length */
640b23fb36SIlya Yanok 	int used;		/**< buffer in use or not */
650b23fb36SIlya Yanok 	uint8_t head[16];	/**< MAC header(6 + 6 + 2) + 2(aligned) */
660b23fb36SIlya Yanok };
670b23fb36SIlya Yanok 
68be7e87e2SMarek Vasut #ifdef CONFIG_FEC_MXC_SWAP_PACKET
69be7e87e2SMarek Vasut static void swap_packet(uint32_t *packet, int length)
70be7e87e2SMarek Vasut {
71be7e87e2SMarek Vasut 	int i;
72be7e87e2SMarek Vasut 
73be7e87e2SMarek Vasut 	for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
74be7e87e2SMarek Vasut 		packet[i] = __swab32(packet[i]);
75be7e87e2SMarek Vasut }
76be7e87e2SMarek Vasut #endif
77be7e87e2SMarek Vasut 
78be7e87e2SMarek Vasut /*
790b23fb36SIlya Yanok  * MII-interface related functions
800b23fb36SIlya Yanok  */
8113947f43STroy Kisky static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
8213947f43STroy Kisky 		uint8_t regAddr)
830b23fb36SIlya Yanok {
840b23fb36SIlya Yanok 	uint32_t reg;		/* convenient holder for the PHY register */
850b23fb36SIlya Yanok 	uint32_t phy;		/* convenient holder for the PHY */
860b23fb36SIlya Yanok 	uint32_t start;
8713947f43STroy Kisky 	int val;
880b23fb36SIlya Yanok 
890b23fb36SIlya Yanok 	/*
900b23fb36SIlya Yanok 	 * reading from any PHY's register is done by properly
910b23fb36SIlya Yanok 	 * programming the FEC's MII data register.
920b23fb36SIlya Yanok 	 */
93d133b881SMarek Vasut 	writel(FEC_IEVENT_MII, &eth->ievent);
940b23fb36SIlya Yanok 	reg = regAddr << FEC_MII_DATA_RA_SHIFT;
950b23fb36SIlya Yanok 	phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
960b23fb36SIlya Yanok 
970b23fb36SIlya Yanok 	writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
98d133b881SMarek Vasut 			phy | reg, &eth->mii_data);
990b23fb36SIlya Yanok 
1000b23fb36SIlya Yanok 	/*
1010b23fb36SIlya Yanok 	 * wait for the related interrupt
1020b23fb36SIlya Yanok 	 */
103a60d1e5bSGraeme Russ 	start = get_timer(0);
104d133b881SMarek Vasut 	while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
1050b23fb36SIlya Yanok 		if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
1060b23fb36SIlya Yanok 			printf("Read MDIO failed...\n");
1070b23fb36SIlya Yanok 			return -1;
1080b23fb36SIlya Yanok 		}
1090b23fb36SIlya Yanok 	}
1100b23fb36SIlya Yanok 
1110b23fb36SIlya Yanok 	/*
1120b23fb36SIlya Yanok 	 * clear mii interrupt bit
1130b23fb36SIlya Yanok 	 */
114d133b881SMarek Vasut 	writel(FEC_IEVENT_MII, &eth->ievent);
1150b23fb36SIlya Yanok 
1160b23fb36SIlya Yanok 	/*
1170b23fb36SIlya Yanok 	 * it's now safe to read the PHY's register
1180b23fb36SIlya Yanok 	 */
11913947f43STroy Kisky 	val = (unsigned short)readl(&eth->mii_data);
12013947f43STroy Kisky 	debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
12113947f43STroy Kisky 			regAddr, val);
12213947f43STroy Kisky 	return val;
1230b23fb36SIlya Yanok }
1240b23fb36SIlya Yanok 
125575c5cc0STroy Kisky static void fec_mii_setspeed(struct ethernet_regs *eth)
1264294b248SStefano Babic {
1274294b248SStefano Babic 	/*
1284294b248SStefano Babic 	 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
1294294b248SStefano Babic 	 * and do not drop the Preamble.
1304294b248SStefano Babic 	 */
1314294b248SStefano Babic 	writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
132575c5cc0STroy Kisky 			&eth->mii_speed);
133575c5cc0STroy Kisky 	debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
1344294b248SStefano Babic }
1350b23fb36SIlya Yanok 
13613947f43STroy Kisky static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
13713947f43STroy Kisky 		uint8_t regAddr, uint16_t data)
13813947f43STroy Kisky {
1390b23fb36SIlya Yanok 	uint32_t reg;		/* convenient holder for the PHY register */
1400b23fb36SIlya Yanok 	uint32_t phy;		/* convenient holder for the PHY */
1410b23fb36SIlya Yanok 	uint32_t start;
1420b23fb36SIlya Yanok 
1430b23fb36SIlya Yanok 	reg = regAddr << FEC_MII_DATA_RA_SHIFT;
1440b23fb36SIlya Yanok 	phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
1450b23fb36SIlya Yanok 
1460b23fb36SIlya Yanok 	writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
147d133b881SMarek Vasut 		FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
1480b23fb36SIlya Yanok 
1490b23fb36SIlya Yanok 	/*
1500b23fb36SIlya Yanok 	 * wait for the MII interrupt
1510b23fb36SIlya Yanok 	 */
152a60d1e5bSGraeme Russ 	start = get_timer(0);
153d133b881SMarek Vasut 	while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
1540b23fb36SIlya Yanok 		if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
1550b23fb36SIlya Yanok 			printf("Write MDIO failed...\n");
1560b23fb36SIlya Yanok 			return -1;
1570b23fb36SIlya Yanok 		}
1580b23fb36SIlya Yanok 	}
1590b23fb36SIlya Yanok 
1600b23fb36SIlya Yanok 	/*
1610b23fb36SIlya Yanok 	 * clear MII interrupt bit
1620b23fb36SIlya Yanok 	 */
163d133b881SMarek Vasut 	writel(FEC_IEVENT_MII, &eth->ievent);
16413947f43STroy Kisky 	debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
1650b23fb36SIlya Yanok 			regAddr, data);
1660b23fb36SIlya Yanok 
1670b23fb36SIlya Yanok 	return 0;
1680b23fb36SIlya Yanok }
1690b23fb36SIlya Yanok 
17013947f43STroy Kisky int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
17113947f43STroy Kisky {
17213947f43STroy Kisky 	return fec_mdio_read(bus->priv, phyAddr, regAddr);
17313947f43STroy Kisky }
17413947f43STroy Kisky 
17513947f43STroy Kisky int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
17613947f43STroy Kisky 		u16 data)
17713947f43STroy Kisky {
17813947f43STroy Kisky 	return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
17913947f43STroy Kisky }
18013947f43STroy Kisky 
18113947f43STroy Kisky #ifndef CONFIG_PHYLIB
1820b23fb36SIlya Yanok static int miiphy_restart_aneg(struct eth_device *dev)
1830b23fb36SIlya Yanok {
184b774fe9dSStefano Babic 	int ret = 0;
185b774fe9dSStefano Babic #if !defined(CONFIG_FEC_MXC_NO_ANEG)
1869e27e9dcSMarek Vasut 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
18713947f43STroy Kisky 	struct ethernet_regs *eth = fec->bus->priv;
1889e27e9dcSMarek Vasut 
1890b23fb36SIlya Yanok 	/*
1900b23fb36SIlya Yanok 	 * Wake up from sleep if necessary
1910b23fb36SIlya Yanok 	 * Reset PHY, then delay 300ns
1920b23fb36SIlya Yanok 	 */
193cb17b92dSJohn Rigby #ifdef CONFIG_MX27
19413947f43STroy Kisky 	fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
195cb17b92dSJohn Rigby #endif
19613947f43STroy Kisky 	fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
1970b23fb36SIlya Yanok 	udelay(1000);
1980b23fb36SIlya Yanok 
1990b23fb36SIlya Yanok 	/*
2000b23fb36SIlya Yanok 	 * Set the auto-negotiation advertisement register bits
2010b23fb36SIlya Yanok 	 */
20213947f43STroy Kisky 	fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
2038ef583a0SMike Frysinger 			LPA_100FULL | LPA_100HALF | LPA_10FULL |
2048ef583a0SMike Frysinger 			LPA_10HALF | PHY_ANLPAR_PSB_802_3);
20513947f43STroy Kisky 	fec_mdio_write(eth, fec->phy_id, MII_BMCR,
2068ef583a0SMike Frysinger 			BMCR_ANENABLE | BMCR_ANRESTART);
2072e5f4421SMarek Vasut 
2082e5f4421SMarek Vasut 	if (fec->mii_postcall)
2092e5f4421SMarek Vasut 		ret = fec->mii_postcall(fec->phy_id);
2102e5f4421SMarek Vasut 
211b774fe9dSStefano Babic #endif
2122e5f4421SMarek Vasut 	return ret;
2130b23fb36SIlya Yanok }
2140b23fb36SIlya Yanok 
2150b23fb36SIlya Yanok static int miiphy_wait_aneg(struct eth_device *dev)
2160b23fb36SIlya Yanok {
2170b23fb36SIlya Yanok 	uint32_t start;
21813947f43STroy Kisky 	int status;
2199e27e9dcSMarek Vasut 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
22013947f43STroy Kisky 	struct ethernet_regs *eth = fec->bus->priv;
2210b23fb36SIlya Yanok 
2220b23fb36SIlya Yanok 	/*
2230b23fb36SIlya Yanok 	 * Wait for AN completion
2240b23fb36SIlya Yanok 	 */
225a60d1e5bSGraeme Russ 	start = get_timer(0);
2260b23fb36SIlya Yanok 	do {
2270b23fb36SIlya Yanok 		if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
2280b23fb36SIlya Yanok 			printf("%s: Autonegotiation timeout\n", dev->name);
2290b23fb36SIlya Yanok 			return -1;
2300b23fb36SIlya Yanok 		}
2310b23fb36SIlya Yanok 
23213947f43STroy Kisky 		status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
23313947f43STroy Kisky 		if (status < 0) {
23413947f43STroy Kisky 			printf("%s: Autonegotiation failed. status: %d\n",
2350b23fb36SIlya Yanok 					dev->name, status);
2360b23fb36SIlya Yanok 			return -1;
2370b23fb36SIlya Yanok 		}
2388ef583a0SMike Frysinger 	} while (!(status & BMSR_LSTATUS));
2390b23fb36SIlya Yanok 
2400b23fb36SIlya Yanok 	return 0;
2410b23fb36SIlya Yanok }
24213947f43STroy Kisky #endif
24313947f43STroy Kisky 
2440b23fb36SIlya Yanok static int fec_rx_task_enable(struct fec_priv *fec)
2450b23fb36SIlya Yanok {
246c0b5a3bbSMarek Vasut 	writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
2470b23fb36SIlya Yanok 	return 0;
2480b23fb36SIlya Yanok }
2490b23fb36SIlya Yanok 
2500b23fb36SIlya Yanok static int fec_rx_task_disable(struct fec_priv *fec)
2510b23fb36SIlya Yanok {
2520b23fb36SIlya Yanok 	return 0;
2530b23fb36SIlya Yanok }
2540b23fb36SIlya Yanok 
2550b23fb36SIlya Yanok static int fec_tx_task_enable(struct fec_priv *fec)
2560b23fb36SIlya Yanok {
257c0b5a3bbSMarek Vasut 	writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
2580b23fb36SIlya Yanok 	return 0;
2590b23fb36SIlya Yanok }
2600b23fb36SIlya Yanok 
2610b23fb36SIlya Yanok static int fec_tx_task_disable(struct fec_priv *fec)
2620b23fb36SIlya Yanok {
2630b23fb36SIlya Yanok 	return 0;
2640b23fb36SIlya Yanok }
2650b23fb36SIlya Yanok 
2660b23fb36SIlya Yanok /**
2670b23fb36SIlya Yanok  * Initialize receive task's buffer descriptors
2680b23fb36SIlya Yanok  * @param[in] fec all we know about the device yet
2690b23fb36SIlya Yanok  * @param[in] count receive buffer count to be allocated
2705c1ad3e6SEric Nelson  * @param[in] dsize desired size of each receive buffer
2710b23fb36SIlya Yanok  * @return 0 on success
2720b23fb36SIlya Yanok  *
2730b23fb36SIlya Yanok  * For this task we need additional memory for the data buffers. And each
2740b23fb36SIlya Yanok  * data buffer requires some alignment. Thy must be aligned to a specific
2755c1ad3e6SEric Nelson  * boundary each.
2760b23fb36SIlya Yanok  */
2775c1ad3e6SEric Nelson static int fec_rbd_init(struct fec_priv *fec, int count, int dsize)
2780b23fb36SIlya Yanok {
2795c1ad3e6SEric Nelson 	uint32_t size;
2805c1ad3e6SEric Nelson 	int i;
2810b23fb36SIlya Yanok 
2820b23fb36SIlya Yanok 	/*
2835c1ad3e6SEric Nelson 	 * Allocate memory for the buffers. This allocation respects the
2845c1ad3e6SEric Nelson 	 * alignment
2850b23fb36SIlya Yanok 	 */
2865c1ad3e6SEric Nelson 	size = roundup(dsize, ARCH_DMA_MINALIGN);
2875c1ad3e6SEric Nelson 	for (i = 0; i < count; i++) {
2885c1ad3e6SEric Nelson 		uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
2895c1ad3e6SEric Nelson 		if (data_ptr == 0) {
2905c1ad3e6SEric Nelson 			uint8_t *data = memalign(ARCH_DMA_MINALIGN,
2915c1ad3e6SEric Nelson 						 size);
2925c1ad3e6SEric Nelson 			if (!data) {
2935c1ad3e6SEric Nelson 				printf("%s: error allocating rxbuf %d\n",
2945c1ad3e6SEric Nelson 				       __func__, i);
2955c1ad3e6SEric Nelson 				goto err;
2965c1ad3e6SEric Nelson 			}
2975c1ad3e6SEric Nelson 			writel((uint32_t)data, &fec->rbd_base[i].data_pointer);
2985c1ad3e6SEric Nelson 		} /* needs allocation */
2995c1ad3e6SEric Nelson 		writew(FEC_RBD_EMPTY, &fec->rbd_base[i].status);
3005c1ad3e6SEric Nelson 		writew(0, &fec->rbd_base[i].data_length);
3015c1ad3e6SEric Nelson 	}
3025c1ad3e6SEric Nelson 
3035c1ad3e6SEric Nelson 	/* Mark the last RBD to close the ring. */
3045c1ad3e6SEric Nelson 	writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[i - 1].status);
3050b23fb36SIlya Yanok 	fec->rbd_index = 0;
3060b23fb36SIlya Yanok 
3070b23fb36SIlya Yanok 	return 0;
3085c1ad3e6SEric Nelson 
3095c1ad3e6SEric Nelson err:
3105c1ad3e6SEric Nelson 	for (; i >= 0; i--) {
3115c1ad3e6SEric Nelson 		uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
3125c1ad3e6SEric Nelson 		free((void *)data_ptr);
3135c1ad3e6SEric Nelson 	}
3145c1ad3e6SEric Nelson 
3155c1ad3e6SEric Nelson 	return -ENOMEM;
3160b23fb36SIlya Yanok }
3170b23fb36SIlya Yanok 
3180b23fb36SIlya Yanok /**
3190b23fb36SIlya Yanok  * Initialize transmit task's buffer descriptors
3200b23fb36SIlya Yanok  * @param[in] fec all we know about the device yet
3210b23fb36SIlya Yanok  *
3220b23fb36SIlya Yanok  * Transmit buffers are created externally. We only have to init the BDs here.\n
3230b23fb36SIlya Yanok  * Note: There is a race condition in the hardware. When only one BD is in
3240b23fb36SIlya Yanok  * use it must be marked with the WRAP bit to use it for every transmitt.
3250b23fb36SIlya Yanok  * This bit in combination with the READY bit results into double transmit
3260b23fb36SIlya Yanok  * of each data buffer. It seems the state machine checks READY earlier then
3270b23fb36SIlya Yanok  * resetting it after the first transfer.
3280b23fb36SIlya Yanok  * Using two BDs solves this issue.
3290b23fb36SIlya Yanok  */
3300b23fb36SIlya Yanok static void fec_tbd_init(struct fec_priv *fec)
3310b23fb36SIlya Yanok {
3325c1ad3e6SEric Nelson 	unsigned addr = (unsigned)fec->tbd_base;
3335c1ad3e6SEric Nelson 	unsigned size = roundup(2 * sizeof(struct fec_bd),
3345c1ad3e6SEric Nelson 				ARCH_DMA_MINALIGN);
3350b23fb36SIlya Yanok 	writew(0x0000, &fec->tbd_base[0].status);
3360b23fb36SIlya Yanok 	writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
3370b23fb36SIlya Yanok 	fec->tbd_index = 0;
3385c1ad3e6SEric Nelson 	flush_dcache_range(addr, addr+size);
3390b23fb36SIlya Yanok }
3400b23fb36SIlya Yanok 
3410b23fb36SIlya Yanok /**
3420b23fb36SIlya Yanok  * Mark the given read buffer descriptor as free
3430b23fb36SIlya Yanok  * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
3440b23fb36SIlya Yanok  * @param[in] pRbd buffer descriptor to mark free again
3450b23fb36SIlya Yanok  */
3460b23fb36SIlya Yanok static void fec_rbd_clean(int last, struct fec_bd *pRbd)
3470b23fb36SIlya Yanok {
3485c1ad3e6SEric Nelson 	unsigned short flags = FEC_RBD_EMPTY;
3490b23fb36SIlya Yanok 	if (last)
3505c1ad3e6SEric Nelson 		flags |= FEC_RBD_WRAP;
3515c1ad3e6SEric Nelson 	writew(flags, &pRbd->status);
3520b23fb36SIlya Yanok 	writew(0, &pRbd->data_length);
3530b23fb36SIlya Yanok }
3540b23fb36SIlya Yanok 
355be252b65SFabio Estevam static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
356be252b65SFabio Estevam 						unsigned char *mac)
3570b23fb36SIlya Yanok {
358be252b65SFabio Estevam 	imx_get_mac_from_fuse(dev_id, mac);
3592e236bf2SEric Jarrige 	return !is_valid_ether_addr(mac);
3600b23fb36SIlya Yanok }
3610b23fb36SIlya Yanok 
3624294b248SStefano Babic static int fec_set_hwaddr(struct eth_device *dev)
3630b23fb36SIlya Yanok {
3644294b248SStefano Babic 	uchar *mac = dev->enetaddr;
3650b23fb36SIlya Yanok 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
3660b23fb36SIlya Yanok 
3670b23fb36SIlya Yanok 	writel(0, &fec->eth->iaddr1);
3680b23fb36SIlya Yanok 	writel(0, &fec->eth->iaddr2);
3690b23fb36SIlya Yanok 	writel(0, &fec->eth->gaddr1);
3700b23fb36SIlya Yanok 	writel(0, &fec->eth->gaddr2);
3710b23fb36SIlya Yanok 
3720b23fb36SIlya Yanok 	/*
3730b23fb36SIlya Yanok 	 * Set physical address
3740b23fb36SIlya Yanok 	 */
3750b23fb36SIlya Yanok 	writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
3760b23fb36SIlya Yanok 			&fec->eth->paddr1);
3770b23fb36SIlya Yanok 	writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
3780b23fb36SIlya Yanok 
3790b23fb36SIlya Yanok 	return 0;
3800b23fb36SIlya Yanok }
3810b23fb36SIlya Yanok 
382a5990b26SMarek Vasut /*
383a5990b26SMarek Vasut  * Do initial configuration of the FEC registers
384a5990b26SMarek Vasut  */
385a5990b26SMarek Vasut static void fec_reg_setup(struct fec_priv *fec)
386a5990b26SMarek Vasut {
387a5990b26SMarek Vasut 	uint32_t rcntrl;
388a5990b26SMarek Vasut 
389a5990b26SMarek Vasut 	/*
390a5990b26SMarek Vasut 	 * Set interrupt mask register
391a5990b26SMarek Vasut 	 */
392a5990b26SMarek Vasut 	writel(0x00000000, &fec->eth->imask);
393a5990b26SMarek Vasut 
394a5990b26SMarek Vasut 	/*
395a5990b26SMarek Vasut 	 * Clear FEC-Lite interrupt event register(IEVENT)
396a5990b26SMarek Vasut 	 */
397a5990b26SMarek Vasut 	writel(0xffffffff, &fec->eth->ievent);
398a5990b26SMarek Vasut 
399a5990b26SMarek Vasut 
400a5990b26SMarek Vasut 	/*
401a5990b26SMarek Vasut 	 * Set FEC-Lite receive control register(R_CNTRL):
402a5990b26SMarek Vasut 	 */
403a5990b26SMarek Vasut 
404a5990b26SMarek Vasut 	/* Start with frame length = 1518, common for all modes. */
405a5990b26SMarek Vasut 	rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
4069d2d924aSbenoit.thebaudeau@advans 	if (fec->xcv_type != SEVENWIRE)		/* xMII modes */
4079d2d924aSbenoit.thebaudeau@advans 		rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
4089d2d924aSbenoit.thebaudeau@advans 	if (fec->xcv_type == RGMII)
409a5990b26SMarek Vasut 		rcntrl |= FEC_RCNTRL_RGMII;
410a5990b26SMarek Vasut 	else if (fec->xcv_type == RMII)
411a5990b26SMarek Vasut 		rcntrl |= FEC_RCNTRL_RMII;
412a5990b26SMarek Vasut 
413a5990b26SMarek Vasut 	writel(rcntrl, &fec->eth->r_cntrl);
414a5990b26SMarek Vasut }
415a5990b26SMarek Vasut 
4160b23fb36SIlya Yanok /**
4170b23fb36SIlya Yanok  * Start the FEC engine
4180b23fb36SIlya Yanok  * @param[in] dev Our device to handle
4190b23fb36SIlya Yanok  */
4200b23fb36SIlya Yanok static int fec_open(struct eth_device *edev)
4210b23fb36SIlya Yanok {
4220b23fb36SIlya Yanok 	struct fec_priv *fec = (struct fec_priv *)edev->priv;
42328774cbaSTroy Kisky 	int speed;
4245c1ad3e6SEric Nelson 	uint32_t addr, size;
4255c1ad3e6SEric Nelson 	int i;
4260b23fb36SIlya Yanok 
4270b23fb36SIlya Yanok 	debug("fec_open: fec_open(dev)\n");
4280b23fb36SIlya Yanok 	/* full-duplex, heartbeat disabled */
4290b23fb36SIlya Yanok 	writel(1 << 2, &fec->eth->x_cntrl);
4300b23fb36SIlya Yanok 	fec->rbd_index = 0;
4310b23fb36SIlya Yanok 
4325c1ad3e6SEric Nelson 	/* Invalidate all descriptors */
4335c1ad3e6SEric Nelson 	for (i = 0; i < FEC_RBD_NUM - 1; i++)
4345c1ad3e6SEric Nelson 		fec_rbd_clean(0, &fec->rbd_base[i]);
4355c1ad3e6SEric Nelson 	fec_rbd_clean(1, &fec->rbd_base[i]);
4365c1ad3e6SEric Nelson 
4375c1ad3e6SEric Nelson 	/* Flush the descriptors into RAM */
4385c1ad3e6SEric Nelson 	size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
4395c1ad3e6SEric Nelson 			ARCH_DMA_MINALIGN);
4405c1ad3e6SEric Nelson 	addr = (uint32_t)fec->rbd_base;
4415c1ad3e6SEric Nelson 	flush_dcache_range(addr, addr + size);
4425c1ad3e6SEric Nelson 
44328774cbaSTroy Kisky #ifdef FEC_QUIRK_ENET_MAC
4442ef2b950SJason Liu 	/* Enable ENET HW endian SWAP */
4452ef2b950SJason Liu 	writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
4462ef2b950SJason Liu 		&fec->eth->ecntrl);
4472ef2b950SJason Liu 	/* Enable ENET store and forward mode */
4482ef2b950SJason Liu 	writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
4492ef2b950SJason Liu 		&fec->eth->x_wmrk);
4502ef2b950SJason Liu #endif
4510b23fb36SIlya Yanok 	/*
4520b23fb36SIlya Yanok 	 * Enable FEC-Lite controller
4530b23fb36SIlya Yanok 	 */
454cb17b92dSJohn Rigby 	writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
455cb17b92dSJohn Rigby 		&fec->eth->ecntrl);
456*7df51fd8SFabio Estevam #if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
457740d6ae5SJohn Rigby 	udelay(100);
458740d6ae5SJohn Rigby 	/*
459740d6ae5SJohn Rigby 	 * setup the MII gasket for RMII mode
460740d6ae5SJohn Rigby 	 */
461740d6ae5SJohn Rigby 
462740d6ae5SJohn Rigby 	/* disable the gasket */
463740d6ae5SJohn Rigby 	writew(0, &fec->eth->miigsk_enr);
464740d6ae5SJohn Rigby 
465740d6ae5SJohn Rigby 	/* wait for the gasket to be disabled */
466740d6ae5SJohn Rigby 	while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
467740d6ae5SJohn Rigby 		udelay(2);
468740d6ae5SJohn Rigby 
469740d6ae5SJohn Rigby 	/* configure gasket for RMII, 50 MHz, no loopback, and no echo */
470740d6ae5SJohn Rigby 	writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
471740d6ae5SJohn Rigby 
472740d6ae5SJohn Rigby 	/* re-enable the gasket */
473740d6ae5SJohn Rigby 	writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
474740d6ae5SJohn Rigby 
475740d6ae5SJohn Rigby 	/* wait until MII gasket is ready */
476740d6ae5SJohn Rigby 	int max_loops = 10;
477740d6ae5SJohn Rigby 	while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
478740d6ae5SJohn Rigby 		if (--max_loops <= 0) {
479740d6ae5SJohn Rigby 			printf("WAIT for MII Gasket ready timed out\n");
480740d6ae5SJohn Rigby 			break;
481740d6ae5SJohn Rigby 		}
482740d6ae5SJohn Rigby 	}
483740d6ae5SJohn Rigby #endif
4840b23fb36SIlya Yanok 
48513947f43STroy Kisky #ifdef CONFIG_PHYLIB
4864dc27eedSTroy Kisky 	{
48713947f43STroy Kisky 		/* Start up the PHY */
48811af8d65STimur Tabi 		int ret = phy_startup(fec->phydev);
48911af8d65STimur Tabi 
49011af8d65STimur Tabi 		if (ret) {
49111af8d65STimur Tabi 			printf("Could not initialize PHY %s\n",
49211af8d65STimur Tabi 			       fec->phydev->dev->name);
49311af8d65STimur Tabi 			return ret;
49411af8d65STimur Tabi 		}
49513947f43STroy Kisky 		speed = fec->phydev->speed;
49613947f43STroy Kisky 	}
49713947f43STroy Kisky #else
4980b23fb36SIlya Yanok 	miiphy_wait_aneg(edev);
49928774cbaSTroy Kisky 	speed = miiphy_speed(edev->name, fec->phy_id);
5009e27e9dcSMarek Vasut 	miiphy_duplex(edev->name, fec->phy_id);
50113947f43STroy Kisky #endif
5020b23fb36SIlya Yanok 
50328774cbaSTroy Kisky #ifdef FEC_QUIRK_ENET_MAC
50428774cbaSTroy Kisky 	{
50528774cbaSTroy Kisky 		u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
506bcb6e902SAlison Wang 		u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
50728774cbaSTroy Kisky 		if (speed == _1000BASET)
50828774cbaSTroy Kisky 			ecr |= FEC_ECNTRL_SPEED;
50928774cbaSTroy Kisky 		else if (speed != _100BASET)
51028774cbaSTroy Kisky 			rcr |= FEC_RCNTRL_RMII_10T;
51128774cbaSTroy Kisky 		writel(ecr, &fec->eth->ecntrl);
51228774cbaSTroy Kisky 		writel(rcr, &fec->eth->r_cntrl);
51328774cbaSTroy Kisky 	}
51428774cbaSTroy Kisky #endif
51528774cbaSTroy Kisky 	debug("%s:Speed=%i\n", __func__, speed);
51628774cbaSTroy Kisky 
5170b23fb36SIlya Yanok 	/*
5180b23fb36SIlya Yanok 	 * Enable SmartDMA receive task
5190b23fb36SIlya Yanok 	 */
5200b23fb36SIlya Yanok 	fec_rx_task_enable(fec);
5210b23fb36SIlya Yanok 
5220b23fb36SIlya Yanok 	udelay(100000);
5230b23fb36SIlya Yanok 	return 0;
5240b23fb36SIlya Yanok }
5250b23fb36SIlya Yanok 
5260b23fb36SIlya Yanok static int fec_init(struct eth_device *dev, bd_t* bd)
5270b23fb36SIlya Yanok {
5280b23fb36SIlya Yanok 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
5299e27e9dcSMarek Vasut 	uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
5305c1ad3e6SEric Nelson 	uint32_t size;
5315c1ad3e6SEric Nelson 	int i, ret;
5320b23fb36SIlya Yanok 
533e9319f11SJohn Rigby 	/* Initialize MAC address */
534e9319f11SJohn Rigby 	fec_set_hwaddr(dev);
535e9319f11SJohn Rigby 
5360b23fb36SIlya Yanok 	/*
5375c1ad3e6SEric Nelson 	 * Allocate transmit descriptors, there are two in total. This
5385c1ad3e6SEric Nelson 	 * allocation respects cache alignment.
5390b23fb36SIlya Yanok 	 */
5405c1ad3e6SEric Nelson 	if (!fec->tbd_base) {
5415c1ad3e6SEric Nelson 		size = roundup(2 * sizeof(struct fec_bd),
5425c1ad3e6SEric Nelson 				ARCH_DMA_MINALIGN);
5435c1ad3e6SEric Nelson 		fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
5445c1ad3e6SEric Nelson 		if (!fec->tbd_base) {
5455c1ad3e6SEric Nelson 			ret = -ENOMEM;
5465c1ad3e6SEric Nelson 			goto err1;
5470b23fb36SIlya Yanok 		}
5485c1ad3e6SEric Nelson 		memset(fec->tbd_base, 0, size);
5495c1ad3e6SEric Nelson 		fec_tbd_init(fec);
5505c1ad3e6SEric Nelson 	}
5510b23fb36SIlya Yanok 
5525c1ad3e6SEric Nelson 	/*
5535c1ad3e6SEric Nelson 	 * Allocate receive descriptors. This allocation respects cache
5545c1ad3e6SEric Nelson 	 * alignment.
5555c1ad3e6SEric Nelson 	 */
5565c1ad3e6SEric Nelson 	if (!fec->rbd_base) {
5575c1ad3e6SEric Nelson 		size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
5585c1ad3e6SEric Nelson 				ARCH_DMA_MINALIGN);
5595c1ad3e6SEric Nelson 		fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
5605c1ad3e6SEric Nelson 		if (!fec->rbd_base) {
5615c1ad3e6SEric Nelson 			ret = -ENOMEM;
5625c1ad3e6SEric Nelson 			goto err2;
5635c1ad3e6SEric Nelson 		}
5645c1ad3e6SEric Nelson 		memset(fec->rbd_base, 0, size);
5655c1ad3e6SEric Nelson 		/*
5665c1ad3e6SEric Nelson 		 * Initialize RxBD ring
5675c1ad3e6SEric Nelson 		 */
5685c1ad3e6SEric Nelson 		if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
5695c1ad3e6SEric Nelson 			ret = -ENOMEM;
5705c1ad3e6SEric Nelson 			goto err3;
5715c1ad3e6SEric Nelson 		}
5725c1ad3e6SEric Nelson 		flush_dcache_range((unsigned)fec->rbd_base,
5735c1ad3e6SEric Nelson 				   (unsigned)fec->rbd_base + size);
5745c1ad3e6SEric Nelson 	}
5750b23fb36SIlya Yanok 
576a5990b26SMarek Vasut 	fec_reg_setup(fec);
5779eb3770bSMarek Vasut 
578f41471e6Sbenoit.thebaudeau@advans 	if (fec->xcv_type != SEVENWIRE)
579575c5cc0STroy Kisky 		fec_mii_setspeed(fec->bus->priv);
5809eb3770bSMarek Vasut 
5810b23fb36SIlya Yanok 	/*
5820b23fb36SIlya Yanok 	 * Set Opcode/Pause Duration Register
5830b23fb36SIlya Yanok 	 */
5840b23fb36SIlya Yanok 	writel(0x00010020, &fec->eth->op_pause);	/* FIXME 0xffff0020; */
5850b23fb36SIlya Yanok 	writel(0x2, &fec->eth->x_wmrk);
5860b23fb36SIlya Yanok 	/*
5870b23fb36SIlya Yanok 	 * Set multicast address filter
5880b23fb36SIlya Yanok 	 */
5890b23fb36SIlya Yanok 	writel(0x00000000, &fec->eth->gaddr1);
5900b23fb36SIlya Yanok 	writel(0x00000000, &fec->eth->gaddr2);
5910b23fb36SIlya Yanok 
5920b23fb36SIlya Yanok 
5930b23fb36SIlya Yanok 	/* clear MIB RAM */
5949e27e9dcSMarek Vasut 	for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
5959e27e9dcSMarek Vasut 		writel(0, i);
5960b23fb36SIlya Yanok 
5970b23fb36SIlya Yanok 	/* FIFO receive start register */
5980b23fb36SIlya Yanok 	writel(0x520, &fec->eth->r_fstart);
5990b23fb36SIlya Yanok 
6000b23fb36SIlya Yanok 	/* size and address of each buffer */
6010b23fb36SIlya Yanok 	writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
6020b23fb36SIlya Yanok 	writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
6030b23fb36SIlya Yanok 	writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
6040b23fb36SIlya Yanok 
60513947f43STroy Kisky #ifndef CONFIG_PHYLIB
6060b23fb36SIlya Yanok 	if (fec->xcv_type != SEVENWIRE)
6070b23fb36SIlya Yanok 		miiphy_restart_aneg(dev);
60813947f43STroy Kisky #endif
6090b23fb36SIlya Yanok 	fec_open(dev);
6100b23fb36SIlya Yanok 	return 0;
6115c1ad3e6SEric Nelson 
6125c1ad3e6SEric Nelson err3:
6135c1ad3e6SEric Nelson 	free(fec->rbd_base);
6145c1ad3e6SEric Nelson err2:
6155c1ad3e6SEric Nelson 	free(fec->tbd_base);
6165c1ad3e6SEric Nelson err1:
6175c1ad3e6SEric Nelson 	return ret;
6180b23fb36SIlya Yanok }
6190b23fb36SIlya Yanok 
6200b23fb36SIlya Yanok /**
6210b23fb36SIlya Yanok  * Halt the FEC engine
6220b23fb36SIlya Yanok  * @param[in] dev Our device to handle
6230b23fb36SIlya Yanok  */
6240b23fb36SIlya Yanok static void fec_halt(struct eth_device *dev)
6250b23fb36SIlya Yanok {
6269e27e9dcSMarek Vasut 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
6270b23fb36SIlya Yanok 	int counter = 0xffff;
6280b23fb36SIlya Yanok 
6290b23fb36SIlya Yanok 	/*
6300b23fb36SIlya Yanok 	 * issue graceful stop command to the FEC transmitter if necessary
6310b23fb36SIlya Yanok 	 */
632cb17b92dSJohn Rigby 	writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
6330b23fb36SIlya Yanok 			&fec->eth->x_cntrl);
6340b23fb36SIlya Yanok 
6350b23fb36SIlya Yanok 	debug("eth_halt: wait for stop regs\n");
6360b23fb36SIlya Yanok 	/*
6370b23fb36SIlya Yanok 	 * wait for graceful stop to register
6380b23fb36SIlya Yanok 	 */
6390b23fb36SIlya Yanok 	while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
640cb17b92dSJohn Rigby 		udelay(1);
6410b23fb36SIlya Yanok 
6420b23fb36SIlya Yanok 	/*
6430b23fb36SIlya Yanok 	 * Disable SmartDMA tasks
6440b23fb36SIlya Yanok 	 */
6450b23fb36SIlya Yanok 	fec_tx_task_disable(fec);
6460b23fb36SIlya Yanok 	fec_rx_task_disable(fec);
6470b23fb36SIlya Yanok 
6480b23fb36SIlya Yanok 	/*
6490b23fb36SIlya Yanok 	 * Disable the Ethernet Controller
6500b23fb36SIlya Yanok 	 * Note: this will also reset the BD index counter!
6510b23fb36SIlya Yanok 	 */
652740d6ae5SJohn Rigby 	writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
653740d6ae5SJohn Rigby 			&fec->eth->ecntrl);
6540b23fb36SIlya Yanok 	fec->rbd_index = 0;
6550b23fb36SIlya Yanok 	fec->tbd_index = 0;
6560b23fb36SIlya Yanok 	debug("eth_halt: done\n");
6570b23fb36SIlya Yanok }
6580b23fb36SIlya Yanok 
6590b23fb36SIlya Yanok /**
6600b23fb36SIlya Yanok  * Transmit one frame
6610b23fb36SIlya Yanok  * @param[in] dev Our ethernet device to handle
6620b23fb36SIlya Yanok  * @param[in] packet Pointer to the data to be transmitted
6630b23fb36SIlya Yanok  * @param[in] length Data count in bytes
6640b23fb36SIlya Yanok  * @return 0 on success
6650b23fb36SIlya Yanok  */
666442dac4cSJoe Hershberger static int fec_send(struct eth_device *dev, void *packet, int length)
6670b23fb36SIlya Yanok {
6680b23fb36SIlya Yanok 	unsigned int status;
669efe24d2eSMarek Vasut 	uint32_t size, end;
6705c1ad3e6SEric Nelson 	uint32_t addr;
671bc1ce150SMarek Vasut 	int timeout = FEC_XFER_TIMEOUT;
672bc1ce150SMarek Vasut 	int ret = 0;
6730b23fb36SIlya Yanok 
6740b23fb36SIlya Yanok 	/*
6750b23fb36SIlya Yanok 	 * This routine transmits one frame.  This routine only accepts
6760b23fb36SIlya Yanok 	 * 6-byte Ethernet addresses.
6770b23fb36SIlya Yanok 	 */
6780b23fb36SIlya Yanok 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
6790b23fb36SIlya Yanok 
6800b23fb36SIlya Yanok 	/*
6810b23fb36SIlya Yanok 	 * Check for valid length of data.
6820b23fb36SIlya Yanok 	 */
6830b23fb36SIlya Yanok 	if ((length > 1500) || (length <= 0)) {
6844294b248SStefano Babic 		printf("Payload (%d) too large\n", length);
6850b23fb36SIlya Yanok 		return -1;
6860b23fb36SIlya Yanok 	}
6870b23fb36SIlya Yanok 
6880b23fb36SIlya Yanok 	/*
6895c1ad3e6SEric Nelson 	 * Setup the transmit buffer. We are always using the first buffer for
6905c1ad3e6SEric Nelson 	 * transmission, the second will be empty and only used to stop the DMA
6915c1ad3e6SEric Nelson 	 * engine. We also flush the packet to RAM here to avoid cache trouble.
6920b23fb36SIlya Yanok 	 */
693be7e87e2SMarek Vasut #ifdef CONFIG_FEC_MXC_SWAP_PACKET
694be7e87e2SMarek Vasut 	swap_packet((uint32_t *)packet, length);
695be7e87e2SMarek Vasut #endif
6965c1ad3e6SEric Nelson 
6975c1ad3e6SEric Nelson 	addr = (uint32_t)packet;
698efe24d2eSMarek Vasut 	end = roundup(addr + length, ARCH_DMA_MINALIGN);
699efe24d2eSMarek Vasut 	addr &= ~(ARCH_DMA_MINALIGN - 1);
700efe24d2eSMarek Vasut 	flush_dcache_range(addr, end);
7015c1ad3e6SEric Nelson 
7020b23fb36SIlya Yanok 	writew(length, &fec->tbd_base[fec->tbd_index].data_length);
7035c1ad3e6SEric Nelson 	writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
7045c1ad3e6SEric Nelson 
7050b23fb36SIlya Yanok 	/*
7060b23fb36SIlya Yanok 	 * update BD's status now
7070b23fb36SIlya Yanok 	 * This block:
7080b23fb36SIlya Yanok 	 * - is always the last in a chain (means no chain)
7090b23fb36SIlya Yanok 	 * - should transmitt the CRC
7100b23fb36SIlya Yanok 	 * - might be the last BD in the list, so the address counter should
7110b23fb36SIlya Yanok 	 *   wrap (-> keep the WRAP flag)
7120b23fb36SIlya Yanok 	 */
7130b23fb36SIlya Yanok 	status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
7140b23fb36SIlya Yanok 	status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
7150b23fb36SIlya Yanok 	writew(status, &fec->tbd_base[fec->tbd_index].status);
7160b23fb36SIlya Yanok 
7170b23fb36SIlya Yanok 	/*
7185c1ad3e6SEric Nelson 	 * Flush data cache. This code flushes both TX descriptors to RAM.
7195c1ad3e6SEric Nelson 	 * After this code, the descriptors will be safely in RAM and we
7205c1ad3e6SEric Nelson 	 * can start DMA.
7215c1ad3e6SEric Nelson 	 */
7225c1ad3e6SEric Nelson 	size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
7235c1ad3e6SEric Nelson 	addr = (uint32_t)fec->tbd_base;
7245c1ad3e6SEric Nelson 	flush_dcache_range(addr, addr + size);
7255c1ad3e6SEric Nelson 
7265c1ad3e6SEric Nelson 	/*
727ab94cd49SMarek Vasut 	 * Below we read the DMA descriptor's last four bytes back from the
728ab94cd49SMarek Vasut 	 * DRAM. This is important in order to make sure that all WRITE
729ab94cd49SMarek Vasut 	 * operations on the bus that were triggered by previous cache FLUSH
730ab94cd49SMarek Vasut 	 * have completed.
731ab94cd49SMarek Vasut 	 *
732ab94cd49SMarek Vasut 	 * Otherwise, on MX28, it is possible to observe a corruption of the
733ab94cd49SMarek Vasut 	 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
734ab94cd49SMarek Vasut 	 * for the bus structure of MX28. The scenario is as follows:
735ab94cd49SMarek Vasut 	 *
736ab94cd49SMarek Vasut 	 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
737ab94cd49SMarek Vasut 	 *    to DRAM due to flush_dcache_range()
738ab94cd49SMarek Vasut 	 * 2) ARM core writes the FEC registers via AHB_ARB2
739ab94cd49SMarek Vasut 	 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
740ab94cd49SMarek Vasut 	 *
741ab94cd49SMarek Vasut 	 * Note that 2) does sometimes finish before 1) due to reordering of
742ab94cd49SMarek Vasut 	 * WRITE accesses on the AHB bus, therefore triggering 3) before the
743ab94cd49SMarek Vasut 	 * DMA descriptor is fully written into DRAM. This results in occasional
744ab94cd49SMarek Vasut 	 * corruption of the DMA descriptor.
745ab94cd49SMarek Vasut 	 */
746ab94cd49SMarek Vasut 	readl(addr + size - 4);
747ab94cd49SMarek Vasut 
748ab94cd49SMarek Vasut 	/*
7490b23fb36SIlya Yanok 	 * Enable SmartDMA transmit task
7500b23fb36SIlya Yanok 	 */
7510b23fb36SIlya Yanok 	fec_tx_task_enable(fec);
7520b23fb36SIlya Yanok 
7530b23fb36SIlya Yanok 	/*
7545c1ad3e6SEric Nelson 	 * Wait until frame is sent. On each turn of the wait cycle, we must
7555c1ad3e6SEric Nelson 	 * invalidate data cache to see what's really in RAM. Also, we need
7565c1ad3e6SEric Nelson 	 * barrier here.
7570b23fb36SIlya Yanok 	 */
75867449098SMarek Vasut 	while (--timeout) {
759c0b5a3bbSMarek Vasut 		if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
760bc1ce150SMarek Vasut 			break;
761bc1ce150SMarek Vasut 	}
7625c1ad3e6SEric Nelson 
76367449098SMarek Vasut 	if (!timeout)
76467449098SMarek Vasut 		ret = -EINVAL;
76567449098SMarek Vasut 
76667449098SMarek Vasut 	invalidate_dcache_range(addr, addr + size);
76767449098SMarek Vasut 	if (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY)
76867449098SMarek Vasut 		ret = -EINVAL;
76967449098SMarek Vasut 
77067449098SMarek Vasut 	debug("fec_send: status 0x%x index %d ret %i\n",
7710b23fb36SIlya Yanok 			readw(&fec->tbd_base[fec->tbd_index].status),
77267449098SMarek Vasut 			fec->tbd_index, ret);
7730b23fb36SIlya Yanok 	/* for next transmission use the other buffer */
7740b23fb36SIlya Yanok 	if (fec->tbd_index)
7750b23fb36SIlya Yanok 		fec->tbd_index = 0;
7760b23fb36SIlya Yanok 	else
7770b23fb36SIlya Yanok 		fec->tbd_index = 1;
7780b23fb36SIlya Yanok 
779bc1ce150SMarek Vasut 	return ret;
7800b23fb36SIlya Yanok }
7810b23fb36SIlya Yanok 
7820b23fb36SIlya Yanok /**
7830b23fb36SIlya Yanok  * Pull one frame from the card
7840b23fb36SIlya Yanok  * @param[in] dev Our ethernet device to handle
7850b23fb36SIlya Yanok  * @return Length of packet read
7860b23fb36SIlya Yanok  */
7870b23fb36SIlya Yanok static int fec_recv(struct eth_device *dev)
7880b23fb36SIlya Yanok {
7890b23fb36SIlya Yanok 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
7900b23fb36SIlya Yanok 	struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
7910b23fb36SIlya Yanok 	unsigned long ievent;
7920b23fb36SIlya Yanok 	int frame_length, len = 0;
7930b23fb36SIlya Yanok 	struct nbuf *frame;
7940b23fb36SIlya Yanok 	uint16_t bd_status;
795efe24d2eSMarek Vasut 	uint32_t addr, size, end;
7965c1ad3e6SEric Nelson 	int i;
797e2a66e60SMarek Vasut 	uchar buff[FEC_MAX_PKT_SIZE] __aligned(ARCH_DMA_MINALIGN);
7980b23fb36SIlya Yanok 
7990b23fb36SIlya Yanok 	/*
8000b23fb36SIlya Yanok 	 * Check if any critical events have happened
8010b23fb36SIlya Yanok 	 */
8020b23fb36SIlya Yanok 	ievent = readl(&fec->eth->ievent);
8030b23fb36SIlya Yanok 	writel(ievent, &fec->eth->ievent);
804eda959f3SMarek Vasut 	debug("fec_recv: ievent 0x%lx\n", ievent);
8050b23fb36SIlya Yanok 	if (ievent & FEC_IEVENT_BABR) {
8060b23fb36SIlya Yanok 		fec_halt(dev);
8070b23fb36SIlya Yanok 		fec_init(dev, fec->bd);
8080b23fb36SIlya Yanok 		printf("some error: 0x%08lx\n", ievent);
8090b23fb36SIlya Yanok 		return 0;
8100b23fb36SIlya Yanok 	}
8110b23fb36SIlya Yanok 	if (ievent & FEC_IEVENT_HBERR) {
8120b23fb36SIlya Yanok 		/* Heartbeat error */
8130b23fb36SIlya Yanok 		writel(0x00000001 | readl(&fec->eth->x_cntrl),
8140b23fb36SIlya Yanok 				&fec->eth->x_cntrl);
8150b23fb36SIlya Yanok 	}
8160b23fb36SIlya Yanok 	if (ievent & FEC_IEVENT_GRA) {
8170b23fb36SIlya Yanok 		/* Graceful stop complete */
8180b23fb36SIlya Yanok 		if (readl(&fec->eth->x_cntrl) & 0x00000001) {
8190b23fb36SIlya Yanok 			fec_halt(dev);
8200b23fb36SIlya Yanok 			writel(~0x00000001 & readl(&fec->eth->x_cntrl),
8210b23fb36SIlya Yanok 					&fec->eth->x_cntrl);
8220b23fb36SIlya Yanok 			fec_init(dev, fec->bd);
8230b23fb36SIlya Yanok 		}
8240b23fb36SIlya Yanok 	}
8250b23fb36SIlya Yanok 
8260b23fb36SIlya Yanok 	/*
8275c1ad3e6SEric Nelson 	 * Read the buffer status. Before the status can be read, the data cache
8285c1ad3e6SEric Nelson 	 * must be invalidated, because the data in RAM might have been changed
8295c1ad3e6SEric Nelson 	 * by DMA. The descriptors are properly aligned to cachelines so there's
8305c1ad3e6SEric Nelson 	 * no need to worry they'd overlap.
8315c1ad3e6SEric Nelson 	 *
8325c1ad3e6SEric Nelson 	 * WARNING: By invalidating the descriptor here, we also invalidate
8335c1ad3e6SEric Nelson 	 * the descriptors surrounding this one. Therefore we can NOT change the
8345c1ad3e6SEric Nelson 	 * contents of this descriptor nor the surrounding ones. The problem is
8355c1ad3e6SEric Nelson 	 * that in order to mark the descriptor as processed, we need to change
8365c1ad3e6SEric Nelson 	 * the descriptor. The solution is to mark the whole cache line when all
8375c1ad3e6SEric Nelson 	 * descriptors in the cache line are processed.
8380b23fb36SIlya Yanok 	 */
8395c1ad3e6SEric Nelson 	addr = (uint32_t)rbd;
8405c1ad3e6SEric Nelson 	addr &= ~(ARCH_DMA_MINALIGN - 1);
8415c1ad3e6SEric Nelson 	size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
8425c1ad3e6SEric Nelson 	invalidate_dcache_range(addr, addr + size);
8435c1ad3e6SEric Nelson 
8440b23fb36SIlya Yanok 	bd_status = readw(&rbd->status);
8450b23fb36SIlya Yanok 	debug("fec_recv: status 0x%x\n", bd_status);
8460b23fb36SIlya Yanok 
8470b23fb36SIlya Yanok 	if (!(bd_status & FEC_RBD_EMPTY)) {
8480b23fb36SIlya Yanok 		if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
8490b23fb36SIlya Yanok 			((readw(&rbd->data_length) - 4) > 14)) {
8500b23fb36SIlya Yanok 			/*
8510b23fb36SIlya Yanok 			 * Get buffer address and size
8520b23fb36SIlya Yanok 			 */
8530b23fb36SIlya Yanok 			frame = (struct nbuf *)readl(&rbd->data_pointer);
8540b23fb36SIlya Yanok 			frame_length = readw(&rbd->data_length) - 4;
8550b23fb36SIlya Yanok 			/*
8565c1ad3e6SEric Nelson 			 * Invalidate data cache over the buffer
8575c1ad3e6SEric Nelson 			 */
8585c1ad3e6SEric Nelson 			addr = (uint32_t)frame;
859efe24d2eSMarek Vasut 			end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
860efe24d2eSMarek Vasut 			addr &= ~(ARCH_DMA_MINALIGN - 1);
861efe24d2eSMarek Vasut 			invalidate_dcache_range(addr, end);
8625c1ad3e6SEric Nelson 
8635c1ad3e6SEric Nelson 			/*
8640b23fb36SIlya Yanok 			 *  Fill the buffer and pass it to upper layers
8650b23fb36SIlya Yanok 			 */
866be7e87e2SMarek Vasut #ifdef CONFIG_FEC_MXC_SWAP_PACKET
867be7e87e2SMarek Vasut 			swap_packet((uint32_t *)frame->data, frame_length);
868be7e87e2SMarek Vasut #endif
8690b23fb36SIlya Yanok 			memcpy(buff, frame->data, frame_length);
8700b23fb36SIlya Yanok 			NetReceive(buff, frame_length);
8710b23fb36SIlya Yanok 			len = frame_length;
8720b23fb36SIlya Yanok 		} else {
8730b23fb36SIlya Yanok 			if (bd_status & FEC_RBD_ERR)
8740b23fb36SIlya Yanok 				printf("error frame: 0x%08lx 0x%08x\n",
8750b23fb36SIlya Yanok 						(ulong)rbd->data_pointer,
8760b23fb36SIlya Yanok 						bd_status);
8770b23fb36SIlya Yanok 		}
8785c1ad3e6SEric Nelson 
8790b23fb36SIlya Yanok 		/*
8805c1ad3e6SEric Nelson 		 * Free the current buffer, restart the engine and move forward
8815c1ad3e6SEric Nelson 		 * to the next buffer. Here we check if the whole cacheline of
8825c1ad3e6SEric Nelson 		 * descriptors was already processed and if so, we mark it free
8835c1ad3e6SEric Nelson 		 * as whole.
8840b23fb36SIlya Yanok 		 */
8855c1ad3e6SEric Nelson 		size = RXDESC_PER_CACHELINE - 1;
8865c1ad3e6SEric Nelson 		if ((fec->rbd_index & size) == size) {
8875c1ad3e6SEric Nelson 			i = fec->rbd_index - size;
8885c1ad3e6SEric Nelson 			addr = (uint32_t)&fec->rbd_base[i];
8895c1ad3e6SEric Nelson 			for (; i <= fec->rbd_index ; i++) {
8905c1ad3e6SEric Nelson 				fec_rbd_clean(i == (FEC_RBD_NUM - 1),
8915c1ad3e6SEric Nelson 					      &fec->rbd_base[i]);
8925c1ad3e6SEric Nelson 			}
8935c1ad3e6SEric Nelson 			flush_dcache_range(addr,
8945c1ad3e6SEric Nelson 				addr + ARCH_DMA_MINALIGN);
8955c1ad3e6SEric Nelson 		}
8965c1ad3e6SEric Nelson 
8970b23fb36SIlya Yanok 		fec_rx_task_enable(fec);
8980b23fb36SIlya Yanok 		fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
8990b23fb36SIlya Yanok 	}
9000b23fb36SIlya Yanok 	debug("fec_recv: stop\n");
9010b23fb36SIlya Yanok 
9020b23fb36SIlya Yanok 	return len;
9030b23fb36SIlya Yanok }
9040b23fb36SIlya Yanok 
905ef8e3a3bSTroy Kisky static void fec_set_dev_name(char *dest, int dev_id)
906ef8e3a3bSTroy Kisky {
907ef8e3a3bSTroy Kisky 	sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
908ef8e3a3bSTroy Kisky }
909ef8e3a3bSTroy Kisky 
910fe428b90STroy Kisky #ifdef CONFIG_PHYLIB
911fe428b90STroy Kisky int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
912fe428b90STroy Kisky 		struct mii_dev *bus, struct phy_device *phydev)
913fe428b90STroy Kisky #else
914fe428b90STroy Kisky static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
915fe428b90STroy Kisky 		struct mii_dev *bus, int phy_id)
916fe428b90STroy Kisky #endif
9170b23fb36SIlya Yanok {
9180b23fb36SIlya Yanok 	struct eth_device *edev;
9199e27e9dcSMarek Vasut 	struct fec_priv *fec;
9200b23fb36SIlya Yanok 	unsigned char ethaddr[6];
921e382fb48SMarek Vasut 	uint32_t start;
922e382fb48SMarek Vasut 	int ret = 0;
9230b23fb36SIlya Yanok 
9240b23fb36SIlya Yanok 	/* create and fill edev struct */
9250b23fb36SIlya Yanok 	edev = (struct eth_device *)malloc(sizeof(struct eth_device));
9260b23fb36SIlya Yanok 	if (!edev) {
9279e27e9dcSMarek Vasut 		puts("fec_mxc: not enough malloc memory for eth_device\n");
928e382fb48SMarek Vasut 		ret = -ENOMEM;
929e382fb48SMarek Vasut 		goto err1;
9300b23fb36SIlya Yanok 	}
9319e27e9dcSMarek Vasut 
9329e27e9dcSMarek Vasut 	fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
9339e27e9dcSMarek Vasut 	if (!fec) {
9349e27e9dcSMarek Vasut 		puts("fec_mxc: not enough malloc memory for fec_priv\n");
935e382fb48SMarek Vasut 		ret = -ENOMEM;
936e382fb48SMarek Vasut 		goto err2;
9379e27e9dcSMarek Vasut 	}
9389e27e9dcSMarek Vasut 
939de0b9576SNobuhiro Iwamatsu 	memset(edev, 0, sizeof(*edev));
9409e27e9dcSMarek Vasut 	memset(fec, 0, sizeof(*fec));
9419e27e9dcSMarek Vasut 
9420b23fb36SIlya Yanok 	edev->priv = fec;
9430b23fb36SIlya Yanok 	edev->init = fec_init;
9440b23fb36SIlya Yanok 	edev->send = fec_send;
9450b23fb36SIlya Yanok 	edev->recv = fec_recv;
9460b23fb36SIlya Yanok 	edev->halt = fec_halt;
947fb57ec97SHeiko Schocher 	edev->write_hwaddr = fec_set_hwaddr;
9480b23fb36SIlya Yanok 
9499e27e9dcSMarek Vasut 	fec->eth = (struct ethernet_regs *)base_addr;
9500b23fb36SIlya Yanok 	fec->bd = bd;
9510b23fb36SIlya Yanok 
952392b8502SMarek Vasut 	fec->xcv_type = CONFIG_FEC_XCV_TYPE;
9530b23fb36SIlya Yanok 
9540b23fb36SIlya Yanok 	/* Reset chip. */
955cb17b92dSJohn Rigby 	writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
956e382fb48SMarek Vasut 	start = get_timer(0);
957e382fb48SMarek Vasut 	while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
958e382fb48SMarek Vasut 		if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
959e382fb48SMarek Vasut 			printf("FEC MXC: Timeout reseting chip\n");
960e382fb48SMarek Vasut 			goto err3;
961e382fb48SMarek Vasut 		}
9620b23fb36SIlya Yanok 		udelay(10);
963e382fb48SMarek Vasut 	}
9640b23fb36SIlya Yanok 
965a5990b26SMarek Vasut 	fec_reg_setup(fec);
966ef8e3a3bSTroy Kisky 	fec_set_dev_name(edev->name, dev_id);
967ef8e3a3bSTroy Kisky 	fec->dev_id = (dev_id == -1) ? 0 : dev_id;
96813947f43STroy Kisky 	fec->bus = bus;
969fe428b90STroy Kisky 	fec_mii_setspeed(bus->priv);
970fe428b90STroy Kisky #ifdef CONFIG_PHYLIB
971fe428b90STroy Kisky 	fec->phydev = phydev;
972fe428b90STroy Kisky 	phy_connect_dev(phydev, edev);
973fe428b90STroy Kisky 	/* Configure phy */
974fe428b90STroy Kisky 	phy_config(phydev);
975fe428b90STroy Kisky #else
976fe428b90STroy Kisky 	fec->phy_id = phy_id;
977fe428b90STroy Kisky #endif
9780b23fb36SIlya Yanok 	eth_register(edev);
9790b23fb36SIlya Yanok 
980be252b65SFabio Estevam 	if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
981be252b65SFabio Estevam 		debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
9820b23fb36SIlya Yanok 		memcpy(edev->enetaddr, ethaddr, 6);
983ddb636bdSEric Nelson 		if (!getenv("ethaddr"))
984ddb636bdSEric Nelson 			eth_setenv_enetaddr("ethaddr", ethaddr);
9854294b248SStefano Babic 	}
986e382fb48SMarek Vasut 	return ret;
987e382fb48SMarek Vasut err3:
988e382fb48SMarek Vasut 	free(fec);
989e382fb48SMarek Vasut err2:
990e382fb48SMarek Vasut 	free(edev);
991e382fb48SMarek Vasut err1:
992e382fb48SMarek Vasut 	return ret;
9930b23fb36SIlya Yanok }
9940b23fb36SIlya Yanok 
995fe428b90STroy Kisky struct mii_dev *fec_get_miibus(uint32_t base_addr, int dev_id)
996fe428b90STroy Kisky {
997fe428b90STroy Kisky 	struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
998fe428b90STroy Kisky 	struct mii_dev *bus;
999fe428b90STroy Kisky 	int ret;
1000fe428b90STroy Kisky 
1001fe428b90STroy Kisky 	bus = mdio_alloc();
1002fe428b90STroy Kisky 	if (!bus) {
1003fe428b90STroy Kisky 		printf("mdio_alloc failed\n");
1004fe428b90STroy Kisky 		return NULL;
1005fe428b90STroy Kisky 	}
1006fe428b90STroy Kisky 	bus->read = fec_phy_read;
1007fe428b90STroy Kisky 	bus->write = fec_phy_write;
1008fe428b90STroy Kisky 	bus->priv = eth;
1009fe428b90STroy Kisky 	fec_set_dev_name(bus->name, dev_id);
1010fe428b90STroy Kisky 
1011fe428b90STroy Kisky 	ret = mdio_register(bus);
1012fe428b90STroy Kisky 	if (ret) {
1013fe428b90STroy Kisky 		printf("mdio_register failed\n");
1014fe428b90STroy Kisky 		free(bus);
1015fe428b90STroy Kisky 		return NULL;
1016fe428b90STroy Kisky 	}
1017fe428b90STroy Kisky 	fec_mii_setspeed(eth);
1018fe428b90STroy Kisky 	return bus;
1019fe428b90STroy Kisky }
1020fe428b90STroy Kisky 
1021eef24480STroy Kisky int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1022eef24480STroy Kisky {
1023fe428b90STroy Kisky 	uint32_t base_mii;
1024fe428b90STroy Kisky 	struct mii_dev *bus = NULL;
1025fe428b90STroy Kisky #ifdef CONFIG_PHYLIB
1026fe428b90STroy Kisky 	struct phy_device *phydev = NULL;
1027fe428b90STroy Kisky #endif
1028fe428b90STroy Kisky 	int ret;
1029fe428b90STroy Kisky 
1030fe428b90STroy Kisky #ifdef CONFIG_MX28
1031fe428b90STroy Kisky 	/*
1032fe428b90STroy Kisky 	 * The i.MX28 has two ethernet interfaces, but they are not equal.
1033fe428b90STroy Kisky 	 * Only the first one can access the MDIO bus.
1034fe428b90STroy Kisky 	 */
1035fe428b90STroy Kisky 	base_mii = MXS_ENET0_BASE;
1036fe428b90STroy Kisky #else
1037fe428b90STroy Kisky 	base_mii = addr;
1038fe428b90STroy Kisky #endif
1039eef24480STroy Kisky 	debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1040fe428b90STroy Kisky 	bus = fec_get_miibus(base_mii, dev_id);
1041fe428b90STroy Kisky 	if (!bus)
1042fe428b90STroy Kisky 		return -ENOMEM;
1043fe428b90STroy Kisky #ifdef CONFIG_PHYLIB
1044fe428b90STroy Kisky 	phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
1045fe428b90STroy Kisky 	if (!phydev) {
1046fe428b90STroy Kisky 		free(bus);
1047fe428b90STroy Kisky 		return -ENOMEM;
1048fe428b90STroy Kisky 	}
1049fe428b90STroy Kisky 	ret = fec_probe(bd, dev_id, addr, bus, phydev);
1050fe428b90STroy Kisky #else
1051fe428b90STroy Kisky 	ret = fec_probe(bd, dev_id, addr, bus, phy_id);
1052fe428b90STroy Kisky #endif
1053fe428b90STroy Kisky 	if (ret) {
1054fe428b90STroy Kisky #ifdef CONFIG_PHYLIB
1055fe428b90STroy Kisky 		free(phydev);
1056fe428b90STroy Kisky #endif
1057fe428b90STroy Kisky 		free(bus);
1058fe428b90STroy Kisky 	}
1059fe428b90STroy Kisky 	return ret;
1060eef24480STroy Kisky }
1061eef24480STroy Kisky 
106209439c31STroy Kisky #ifdef CONFIG_FEC_MXC_PHYADDR
10630b23fb36SIlya Yanok int fecmxc_initialize(bd_t *bd)
10640b23fb36SIlya Yanok {
1065eef24480STroy Kisky 	return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1066eef24480STroy Kisky 			IMX_FEC_BASE);
10679e27e9dcSMarek Vasut }
10689e27e9dcSMarek Vasut #endif
10699e27e9dcSMarek Vasut 
107013947f43STroy Kisky #ifndef CONFIG_PHYLIB
10712e5f4421SMarek Vasut int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
10722e5f4421SMarek Vasut {
10732e5f4421SMarek Vasut 	struct fec_priv *fec = (struct fec_priv *)dev->priv;
10742e5f4421SMarek Vasut 	fec->mii_postcall = cb;
10752e5f4421SMarek Vasut 	return 0;
10762e5f4421SMarek Vasut }
107713947f43STroy Kisky #endif
1078