blob: fd96c435fa015afb893238b640f73079d2c2fbf6 [file] [log] [blame]
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001/*
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>
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Ilya Yanoke93a4a52009-07-21 19:32:21 +04009 */
10
11#include <common.h>
Jagan Teki484f0212016-12-06 00:00:49 +010012#include <dm.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040013#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060014#include <memalign.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040015#include <net.h>
Jeroen Hofstee120f43f2014-10-08 22:57:40 +020016#include <netdev.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040017#include <miiphy.h>
18#include "fec_mxc.h"
19
20#include <asm/arch/clock.h>
21#include <asm/arch/imx-regs.h>
Peng Fan13433fd2015-08-12 17:46:51 +080022#include <asm/imx-common/sys_proto.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040023#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090024#include <linux/errno.h>
Marek Vasut4d85b032012-08-26 10:19:20 +000025#include <linux/compiler.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040026
27DECLARE_GLOBAL_DATA_PTR;
28
Marek Vasut5f1631d2012-08-29 03:49:49 +000029/*
30 * Timeout the transfer after 5 mS. This is usually a bit more, since
31 * the code in the tightloops this timeout is used in adds some overhead.
32 */
33#define FEC_XFER_TIMEOUT 5000
34
Fabio Estevam8b798b22014-08-25 13:34:16 -030035/*
36 * The standard 32-byte DMA alignment does not work on mx6solox, which requires
37 * 64-byte alignment in the DMA RX FEC buffer.
38 * Introduce the FEC_DMA_RX_MINALIGN which can cover mx6solox needs and also
39 * satisfies the alignment on other SoCs (32-bytes)
40 */
41#define FEC_DMA_RX_MINALIGN 64
42
Ilya Yanoke93a4a52009-07-21 19:32:21 +040043#ifndef CONFIG_MII
44#error "CONFIG_MII has to be defined!"
45#endif
46
Eric Nelson3d2f7272012-03-15 18:33:25 +000047#ifndef CONFIG_FEC_XCV_TYPE
48#define CONFIG_FEC_XCV_TYPE MII100
Marek Vasutdbb4fce2011-09-11 18:05:33 +000049#endif
50
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000051/*
52 * The i.MX28 operates with packets in big endian. We need to swap them before
53 * sending and after receiving.
54 */
Eric Nelson3d2f7272012-03-15 18:33:25 +000055#ifdef CONFIG_MX28
56#define CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000057#endif
58
Eric Nelson3d2f7272012-03-15 18:33:25 +000059#define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
60
61/* Check various alignment issues at compile time */
62#if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
63#error "ARCH_DMA_MINALIGN must be multiple of 16!"
64#endif
65
66#if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
67 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
68#error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
69#endif
70
Ilya Yanoke93a4a52009-07-21 19:32:21 +040071#undef DEBUG
72
Eric Nelson3d2f7272012-03-15 18:33:25 +000073#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000074static void swap_packet(uint32_t *packet, int length)
75{
76 int i;
77
78 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
79 packet[i] = __swab32(packet[i]);
80}
81#endif
82
Ilya Yanoke93a4a52009-07-21 19:32:21 +040083/*
84 * MII-interface related functions
85 */
Troy Kisky2000c662012-02-07 14:08:47 +000086static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
87 uint8_t regAddr)
Ilya Yanoke93a4a52009-07-21 19:32:21 +040088{
Ilya Yanoke93a4a52009-07-21 19:32:21 +040089 uint32_t reg; /* convenient holder for the PHY register */
90 uint32_t phy; /* convenient holder for the PHY */
91 uint32_t start;
Troy Kisky2000c662012-02-07 14:08:47 +000092 int val;
Ilya Yanoke93a4a52009-07-21 19:32:21 +040093
94 /*
95 * reading from any PHY's register is done by properly
96 * programming the FEC's MII data register.
97 */
Marek Vasutbf2386b2011-09-11 18:05:34 +000098 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +040099 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
100 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
101
102 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
Marek Vasutbf2386b2011-09-11 18:05:34 +0000103 phy | reg, &eth->mii_data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400104
105 /*
106 * wait for the related interrupt
107 */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000108 start = get_timer(0);
Marek Vasutbf2386b2011-09-11 18:05:34 +0000109 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400110 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
111 printf("Read MDIO failed...\n");
112 return -1;
113 }
114 }
115
116 /*
117 * clear mii interrupt bit
118 */
Marek Vasutbf2386b2011-09-11 18:05:34 +0000119 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400120
121 /*
122 * it's now safe to read the PHY's register
123 */
Troy Kisky2000c662012-02-07 14:08:47 +0000124 val = (unsigned short)readl(&eth->mii_data);
125 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
126 regAddr, val);
127 return val;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400128}
129
Troy Kisky5e762652012-10-22 16:40:41 +0000130static void fec_mii_setspeed(struct ethernet_regs *eth)
Stefano Babic889f2e22010-02-01 14:51:30 +0100131{
132 /*
133 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
134 * and do not drop the Preamble.
Måns Rullgård4aeddb72015-12-08 15:38:45 +0000135 *
136 * The i.MX28 and i.MX6 types have another field in the MSCR (aka
137 * MII_SPEED) register that defines the MDIO output hold time. Earlier
138 * versions are RAZ there, so just ignore the difference and write the
139 * register always.
140 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
141 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
142 * output.
143 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
144 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
145 * holdtime cannot result in a value greater than 3.
Stefano Babic889f2e22010-02-01 14:51:30 +0100146 */
Måns Rullgård4aeddb72015-12-08 15:38:45 +0000147 u32 pclk = imx_get_fecclk();
148 u32 speed = DIV_ROUND_UP(pclk, 5000000);
149 u32 hold = DIV_ROUND_UP(pclk, 100000000) - 1;
Markus Niebel1af82742014-02-05 10:54:11 +0100150#ifdef FEC_QUIRK_ENET_MAC
151 speed--;
152#endif
Måns Rullgård4aeddb72015-12-08 15:38:45 +0000153 writel(speed << 1 | hold << 8, &eth->mii_speed);
Troy Kisky5e762652012-10-22 16:40:41 +0000154 debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
Stefano Babic889f2e22010-02-01 14:51:30 +0100155}
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400156
Troy Kisky2000c662012-02-07 14:08:47 +0000157static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
158 uint8_t regAddr, uint16_t data)
159{
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400160 uint32_t reg; /* convenient holder for the PHY register */
161 uint32_t phy; /* convenient holder for the PHY */
162 uint32_t start;
163
164 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
165 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
166
167 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
Marek Vasutbf2386b2011-09-11 18:05:34 +0000168 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400169
170 /*
171 * wait for the MII interrupt
172 */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000173 start = get_timer(0);
Marek Vasutbf2386b2011-09-11 18:05:34 +0000174 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400175 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
176 printf("Write MDIO failed...\n");
177 return -1;
178 }
179 }
180
181 /*
182 * clear MII interrupt bit
183 */
Marek Vasutbf2386b2011-09-11 18:05:34 +0000184 writel(FEC_IEVENT_MII, &eth->ievent);
Troy Kisky2000c662012-02-07 14:08:47 +0000185 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400186 regAddr, data);
187
188 return 0;
189}
190
Jeroen Hofstee120f43f2014-10-08 22:57:40 +0200191static int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr,
192 int regAddr)
Troy Kisky2000c662012-02-07 14:08:47 +0000193{
194 return fec_mdio_read(bus->priv, phyAddr, regAddr);
195}
196
Jeroen Hofstee120f43f2014-10-08 22:57:40 +0200197static int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr,
198 int regAddr, u16 data)
Troy Kisky2000c662012-02-07 14:08:47 +0000199{
200 return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
201}
202
203#ifndef CONFIG_PHYLIB
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400204static int miiphy_restart_aneg(struct eth_device *dev)
205{
Stefano Babicd6228172012-02-22 00:24:35 +0000206 int ret = 0;
207#if !defined(CONFIG_FEC_MXC_NO_ANEG)
Marek Vasutedcd6c02011-09-16 01:13:47 +0200208 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000209 struct ethernet_regs *eth = fec->bus->priv;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200210
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400211 /*
212 * Wake up from sleep if necessary
213 * Reset PHY, then delay 300ns
214 */
John Rigbye650e492010-01-25 23:12:55 -0700215#ifdef CONFIG_MX27
Troy Kisky2000c662012-02-07 14:08:47 +0000216 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
John Rigbye650e492010-01-25 23:12:55 -0700217#endif
Troy Kisky2000c662012-02-07 14:08:47 +0000218 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400219 udelay(1000);
220
221 /*
222 * Set the auto-negotiation advertisement register bits
223 */
Troy Kisky2000c662012-02-07 14:08:47 +0000224 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
Mike Frysingerd63ee712010-12-23 15:40:12 -0500225 LPA_100FULL | LPA_100HALF | LPA_10FULL |
226 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
Troy Kisky2000c662012-02-07 14:08:47 +0000227 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
Mike Frysingerd63ee712010-12-23 15:40:12 -0500228 BMCR_ANENABLE | BMCR_ANRESTART);
Marek Vasut539ecee2011-09-11 18:05:36 +0000229
230 if (fec->mii_postcall)
231 ret = fec->mii_postcall(fec->phy_id);
232
Stefano Babicd6228172012-02-22 00:24:35 +0000233#endif
Marek Vasut539ecee2011-09-11 18:05:36 +0000234 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400235}
236
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200237#ifndef CONFIG_FEC_FIXED_SPEED
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400238static int miiphy_wait_aneg(struct eth_device *dev)
239{
240 uint32_t start;
Troy Kisky2000c662012-02-07 14:08:47 +0000241 int status;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200242 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000243 struct ethernet_regs *eth = fec->bus->priv;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400244
245 /*
246 * Wait for AN completion
247 */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000248 start = get_timer(0);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400249 do {
250 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
251 printf("%s: Autonegotiation timeout\n", dev->name);
252 return -1;
253 }
254
Troy Kisky2000c662012-02-07 14:08:47 +0000255 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
256 if (status < 0) {
257 printf("%s: Autonegotiation failed. status: %d\n",
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400258 dev->name, status);
259 return -1;
260 }
Mike Frysingerd63ee712010-12-23 15:40:12 -0500261 } while (!(status & BMSR_LSTATUS));
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400262
263 return 0;
264}
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200265#endif /* CONFIG_FEC_FIXED_SPEED */
Troy Kisky2000c662012-02-07 14:08:47 +0000266#endif
267
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400268static int fec_rx_task_enable(struct fec_priv *fec)
269{
Marek Vasutc1582c02012-08-29 03:49:51 +0000270 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400271 return 0;
272}
273
274static int fec_rx_task_disable(struct fec_priv *fec)
275{
276 return 0;
277}
278
279static int fec_tx_task_enable(struct fec_priv *fec)
280{
Marek Vasutc1582c02012-08-29 03:49:51 +0000281 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400282 return 0;
283}
284
285static int fec_tx_task_disable(struct fec_priv *fec)
286{
287 return 0;
288}
289
290/**
291 * Initialize receive task's buffer descriptors
292 * @param[in] fec all we know about the device yet
293 * @param[in] count receive buffer count to be allocated
Eric Nelson3d2f7272012-03-15 18:33:25 +0000294 * @param[in] dsize desired size of each receive buffer
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400295 * @return 0 on success
296 *
Marek Vasut03880452013-10-12 20:36:25 +0200297 * Init all RX descriptors to default values.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400298 */
Marek Vasut03880452013-10-12 20:36:25 +0200299static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400300{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000301 uint32_t size;
Marek Vasut03880452013-10-12 20:36:25 +0200302 uint8_t *data;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000303 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400304
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400305 /*
Marek Vasut03880452013-10-12 20:36:25 +0200306 * Reload the RX descriptors with default values and wipe
307 * the RX buffers.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400308 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000309 size = roundup(dsize, ARCH_DMA_MINALIGN);
310 for (i = 0; i < count; i++) {
Marek Vasut03880452013-10-12 20:36:25 +0200311 data = (uint8_t *)fec->rbd_base[i].data_pointer;
312 memset(data, 0, dsize);
313 flush_dcache_range((uint32_t)data, (uint32_t)data + size);
314
315 fec->rbd_base[i].status = FEC_RBD_EMPTY;
316 fec->rbd_base[i].data_length = 0;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000317 }
318
319 /* Mark the last RBD to close the ring. */
Marek Vasut03880452013-10-12 20:36:25 +0200320 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400321 fec->rbd_index = 0;
322
Marek Vasut03880452013-10-12 20:36:25 +0200323 flush_dcache_range((unsigned)fec->rbd_base,
324 (unsigned)fec->rbd_base + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400325}
326
327/**
328 * Initialize transmit task's buffer descriptors
329 * @param[in] fec all we know about the device yet
330 *
331 * Transmit buffers are created externally. We only have to init the BDs here.\n
332 * Note: There is a race condition in the hardware. When only one BD is in
333 * use it must be marked with the WRAP bit to use it for every transmitt.
334 * This bit in combination with the READY bit results into double transmit
335 * of each data buffer. It seems the state machine checks READY earlier then
336 * resetting it after the first transfer.
337 * Using two BDs solves this issue.
338 */
339static void fec_tbd_init(struct fec_priv *fec)
340{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000341 unsigned addr = (unsigned)fec->tbd_base;
342 unsigned size = roundup(2 * sizeof(struct fec_bd),
343 ARCH_DMA_MINALIGN);
Marek Vasut03880452013-10-12 20:36:25 +0200344
345 memset(fec->tbd_base, 0, size);
346 fec->tbd_base[0].status = 0;
347 fec->tbd_base[1].status = FEC_TBD_WRAP;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400348 fec->tbd_index = 0;
Marek Vasut03880452013-10-12 20:36:25 +0200349 flush_dcache_range(addr, addr + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400350}
351
352/**
353 * Mark the given read buffer descriptor as free
354 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
355 * @param[in] pRbd buffer descriptor to mark free again
356 */
357static void fec_rbd_clean(int last, struct fec_bd *pRbd)
358{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000359 unsigned short flags = FEC_RBD_EMPTY;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400360 if (last)
Eric Nelson3d2f7272012-03-15 18:33:25 +0000361 flags |= FEC_RBD_WRAP;
362 writew(flags, &pRbd->status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400363 writew(0, &pRbd->data_length);
364}
365
Jagan Tekibc5fb462016-12-06 00:00:48 +0100366static int fec_get_hwaddr(int dev_id, unsigned char *mac)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400367{
Fabio Estevam04fc1282011-12-20 05:46:31 +0000368 imx_get_mac_from_fuse(dev_id, mac);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500369 return !is_valid_ethaddr(mac);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400370}
371
Jagan Teki484f0212016-12-06 00:00:49 +0100372#ifdef CONFIG_DM_ETH
373static int fecmxc_set_hwaddr(struct udevice *dev)
374#else
Stefano Babic889f2e22010-02-01 14:51:30 +0100375static int fec_set_hwaddr(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100376#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400377{
Jagan Teki484f0212016-12-06 00:00:49 +0100378#ifdef CONFIG_DM_ETH
379 struct fec_priv *fec = dev_get_priv(dev);
380 struct eth_pdata *pdata = dev_get_platdata(dev);
381 uchar *mac = pdata->enetaddr;
382#else
Stefano Babic889f2e22010-02-01 14:51:30 +0100383 uchar *mac = dev->enetaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400384 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100385#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400386
387 writel(0, &fec->eth->iaddr1);
388 writel(0, &fec->eth->iaddr2);
389 writel(0, &fec->eth->gaddr1);
390 writel(0, &fec->eth->gaddr2);
391
392 /*
393 * Set physical address
394 */
395 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
396 &fec->eth->paddr1);
397 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
398
399 return 0;
400}
401
Marek Vasut335cbd22012-05-01 11:09:41 +0000402/*
403 * Do initial configuration of the FEC registers
404 */
405static void fec_reg_setup(struct fec_priv *fec)
406{
407 uint32_t rcntrl;
408
409 /*
410 * Set interrupt mask register
411 */
412 writel(0x00000000, &fec->eth->imask);
413
414 /*
415 * Clear FEC-Lite interrupt event register(IEVENT)
416 */
417 writel(0xffffffff, &fec->eth->ievent);
418
419
420 /*
421 * Set FEC-Lite receive control register(R_CNTRL):
422 */
423
424 /* Start with frame length = 1518, common for all modes. */
425 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
benoit.thebaudeau@advansacc7a282012-07-19 02:12:46 +0000426 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
427 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
428 if (fec->xcv_type == RGMII)
Marek Vasut335cbd22012-05-01 11:09:41 +0000429 rcntrl |= FEC_RCNTRL_RGMII;
430 else if (fec->xcv_type == RMII)
431 rcntrl |= FEC_RCNTRL_RMII;
Marek Vasut335cbd22012-05-01 11:09:41 +0000432
433 writel(rcntrl, &fec->eth->r_cntrl);
434}
435
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400436/**
437 * Start the FEC engine
438 * @param[in] dev Our device to handle
439 */
Jagan Teki484f0212016-12-06 00:00:49 +0100440#ifdef CONFIG_DM_ETH
441static int fec_open(struct udevice *dev)
442#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400443static int fec_open(struct eth_device *edev)
Jagan Teki484f0212016-12-06 00:00:49 +0100444#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400445{
Jagan Teki484f0212016-12-06 00:00:49 +0100446#ifdef CONFIG_DM_ETH
447 struct fec_priv *fec = dev_get_priv(dev);
448#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400449 struct fec_priv *fec = (struct fec_priv *)edev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100450#endif
Troy Kisky01112132012-02-07 14:08:46 +0000451 int speed;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000452 uint32_t addr, size;
453 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400454
455 debug("fec_open: fec_open(dev)\n");
456 /* full-duplex, heartbeat disabled */
457 writel(1 << 2, &fec->eth->x_cntrl);
458 fec->rbd_index = 0;
459
Eric Nelson3d2f7272012-03-15 18:33:25 +0000460 /* Invalidate all descriptors */
461 for (i = 0; i < FEC_RBD_NUM - 1; i++)
462 fec_rbd_clean(0, &fec->rbd_base[i]);
463 fec_rbd_clean(1, &fec->rbd_base[i]);
464
465 /* Flush the descriptors into RAM */
466 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
467 ARCH_DMA_MINALIGN);
468 addr = (uint32_t)fec->rbd_base;
469 flush_dcache_range(addr, addr + size);
470
Troy Kisky01112132012-02-07 14:08:46 +0000471#ifdef FEC_QUIRK_ENET_MAC
Jason Liubbcef6c2011-12-16 05:17:07 +0000472 /* Enable ENET HW endian SWAP */
473 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
474 &fec->eth->ecntrl);
475 /* Enable ENET store and forward mode */
476 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
477 &fec->eth->x_wmrk);
478#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400479 /*
480 * Enable FEC-Lite controller
481 */
John Rigbye650e492010-01-25 23:12:55 -0700482 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
483 &fec->eth->ecntrl);
Fabio Estevam84c1f522013-09-13 00:36:27 -0300484#if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
John Rigby99d5fed2010-01-25 23:12:57 -0700485 udelay(100);
486 /*
487 * setup the MII gasket for RMII mode
488 */
489
490 /* disable the gasket */
491 writew(0, &fec->eth->miigsk_enr);
492
493 /* wait for the gasket to be disabled */
494 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
495 udelay(2);
496
497 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
498 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
499
500 /* re-enable the gasket */
501 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
502
503 /* wait until MII gasket is ready */
504 int max_loops = 10;
505 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
506 if (--max_loops <= 0) {
507 printf("WAIT for MII Gasket ready timed out\n");
508 break;
509 }
510 }
511#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400512
Troy Kisky2000c662012-02-07 14:08:47 +0000513#ifdef CONFIG_PHYLIB
Troy Kisky2c42b3c2012-10-22 16:40:45 +0000514 {
Troy Kisky2000c662012-02-07 14:08:47 +0000515 /* Start up the PHY */
Timur Tabi42387462012-07-09 08:52:43 +0000516 int ret = phy_startup(fec->phydev);
517
518 if (ret) {
519 printf("Could not initialize PHY %s\n",
520 fec->phydev->dev->name);
521 return ret;
522 }
Troy Kisky2000c662012-02-07 14:08:47 +0000523 speed = fec->phydev->speed;
Troy Kisky2000c662012-02-07 14:08:47 +0000524 }
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200525#elif CONFIG_FEC_FIXED_SPEED
526 speed = CONFIG_FEC_FIXED_SPEED;
Troy Kisky2000c662012-02-07 14:08:47 +0000527#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400528 miiphy_wait_aneg(edev);
Troy Kisky01112132012-02-07 14:08:46 +0000529 speed = miiphy_speed(edev->name, fec->phy_id);
Marek Vasutedcd6c02011-09-16 01:13:47 +0200530 miiphy_duplex(edev->name, fec->phy_id);
Troy Kisky2000c662012-02-07 14:08:47 +0000531#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400532
Troy Kisky01112132012-02-07 14:08:46 +0000533#ifdef FEC_QUIRK_ENET_MAC
534 {
535 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
Alison Wang89d932a2013-05-27 22:55:43 +0000536 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
Troy Kisky01112132012-02-07 14:08:46 +0000537 if (speed == _1000BASET)
538 ecr |= FEC_ECNTRL_SPEED;
539 else if (speed != _100BASET)
540 rcr |= FEC_RCNTRL_RMII_10T;
541 writel(ecr, &fec->eth->ecntrl);
542 writel(rcr, &fec->eth->r_cntrl);
543 }
544#endif
545 debug("%s:Speed=%i\n", __func__, speed);
546
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400547 /*
548 * Enable SmartDMA receive task
549 */
550 fec_rx_task_enable(fec);
551
552 udelay(100000);
553 return 0;
554}
555
Jagan Teki484f0212016-12-06 00:00:49 +0100556#ifdef CONFIG_DM_ETH
557static int fecmxc_init(struct udevice *dev)
558#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400559static int fec_init(struct eth_device *dev, bd_t* bd)
Jagan Teki484f0212016-12-06 00:00:49 +0100560#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400561{
Jagan Teki484f0212016-12-06 00:00:49 +0100562#ifdef CONFIG_DM_ETH
563 struct fec_priv *fec = dev_get_priv(dev);
564#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400565 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100566#endif
Marek Vasutedcd6c02011-09-16 01:13:47 +0200567 uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
Marek Vasut03880452013-10-12 20:36:25 +0200568 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400569
John Rigbya4a30552010-10-13 14:31:08 -0600570 /* Initialize MAC address */
Jagan Teki484f0212016-12-06 00:00:49 +0100571#ifdef CONFIG_DM_ETH
572 fecmxc_set_hwaddr(dev);
573#else
John Rigbya4a30552010-10-13 14:31:08 -0600574 fec_set_hwaddr(dev);
Jagan Teki484f0212016-12-06 00:00:49 +0100575#endif
John Rigbya4a30552010-10-13 14:31:08 -0600576
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400577 /*
Marek Vasut03880452013-10-12 20:36:25 +0200578 * Setup transmit descriptors, there are two in total.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400579 */
Marek Vasut03880452013-10-12 20:36:25 +0200580 fec_tbd_init(fec);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400581
Marek Vasut03880452013-10-12 20:36:25 +0200582 /* Setup receive descriptors. */
583 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400584
Marek Vasut335cbd22012-05-01 11:09:41 +0000585 fec_reg_setup(fec);
Marek Vasutb8f88562011-09-11 18:05:31 +0000586
benoit.thebaudeau@advans551bb362012-07-19 02:12:58 +0000587 if (fec->xcv_type != SEVENWIRE)
Troy Kisky5e762652012-10-22 16:40:41 +0000588 fec_mii_setspeed(fec->bus->priv);
Marek Vasutb8f88562011-09-11 18:05:31 +0000589
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400590 /*
591 * Set Opcode/Pause Duration Register
592 */
593 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
594 writel(0x2, &fec->eth->x_wmrk);
595 /*
596 * Set multicast address filter
597 */
598 writel(0x00000000, &fec->eth->gaddr1);
599 writel(0x00000000, &fec->eth->gaddr2);
600
601
Peng Fan13433fd2015-08-12 17:46:51 +0800602 /* Do not access reserved register for i.MX6UL */
Peng Fan53cd7bb2016-05-23 18:36:04 +0800603 if (!is_mx6ul()) {
Peng Fan13433fd2015-08-12 17:46:51 +0800604 /* clear MIB RAM */
605 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
606 writel(0, i);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400607
Peng Fan13433fd2015-08-12 17:46:51 +0800608 /* FIFO receive start register */
609 writel(0x520, &fec->eth->r_fstart);
610 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400611
612 /* size and address of each buffer */
613 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
614 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
615 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
616
Troy Kisky2000c662012-02-07 14:08:47 +0000617#ifndef CONFIG_PHYLIB
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400618 if (fec->xcv_type != SEVENWIRE)
619 miiphy_restart_aneg(dev);
Troy Kisky2000c662012-02-07 14:08:47 +0000620#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400621 fec_open(dev);
622 return 0;
623}
624
625/**
626 * Halt the FEC engine
627 * @param[in] dev Our device to handle
628 */
Jagan Teki484f0212016-12-06 00:00:49 +0100629#ifdef CONFIG_DM_ETH
630static void fecmxc_halt(struct udevice *dev)
631#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400632static void fec_halt(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100633#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400634{
Jagan Teki484f0212016-12-06 00:00:49 +0100635#ifdef CONFIG_DM_ETH
636 struct fec_priv *fec = dev_get_priv(dev);
637#else
Marek Vasutedcd6c02011-09-16 01:13:47 +0200638 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100639#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400640 int counter = 0xffff;
641
642 /*
643 * issue graceful stop command to the FEC transmitter if necessary
644 */
John Rigbye650e492010-01-25 23:12:55 -0700645 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400646 &fec->eth->x_cntrl);
647
648 debug("eth_halt: wait for stop regs\n");
649 /*
650 * wait for graceful stop to register
651 */
652 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
John Rigbye650e492010-01-25 23:12:55 -0700653 udelay(1);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400654
655 /*
656 * Disable SmartDMA tasks
657 */
658 fec_tx_task_disable(fec);
659 fec_rx_task_disable(fec);
660
661 /*
662 * Disable the Ethernet Controller
663 * Note: this will also reset the BD index counter!
664 */
John Rigby99d5fed2010-01-25 23:12:57 -0700665 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
666 &fec->eth->ecntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400667 fec->rbd_index = 0;
668 fec->tbd_index = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400669 debug("eth_halt: done\n");
670}
671
672/**
673 * Transmit one frame
674 * @param[in] dev Our ethernet device to handle
675 * @param[in] packet Pointer to the data to be transmitted
676 * @param[in] length Data count in bytes
677 * @return 0 on success
678 */
Jagan Teki484f0212016-12-06 00:00:49 +0100679#ifdef CONFIG_DM_ETH
680static int fecmxc_send(struct udevice *dev, void *packet, int length)
681#else
Joe Hershberger7c31bd12012-05-21 14:45:27 +0000682static int fec_send(struct eth_device *dev, void *packet, int length)
Jagan Teki484f0212016-12-06 00:00:49 +0100683#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400684{
685 unsigned int status;
Marek Vasut4325d242012-08-26 10:19:21 +0000686 uint32_t size, end;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000687 uint32_t addr;
Marek Vasut5f1631d2012-08-29 03:49:49 +0000688 int timeout = FEC_XFER_TIMEOUT;
689 int ret = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400690
691 /*
692 * This routine transmits one frame. This routine only accepts
693 * 6-byte Ethernet addresses.
694 */
Jagan Teki484f0212016-12-06 00:00:49 +0100695#ifdef CONFIG_DM_ETH
696 struct fec_priv *fec = dev_get_priv(dev);
697#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400698 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100699#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400700
701 /*
702 * Check for valid length of data.
703 */
704 if ((length > 1500) || (length <= 0)) {
Stefano Babic889f2e22010-02-01 14:51:30 +0100705 printf("Payload (%d) too large\n", length);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400706 return -1;
707 }
708
709 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000710 * Setup the transmit buffer. We are always using the first buffer for
711 * transmission, the second will be empty and only used to stop the DMA
712 * engine. We also flush the packet to RAM here to avoid cache trouble.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400713 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000714#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000715 swap_packet((uint32_t *)packet, length);
716#endif
Eric Nelson3d2f7272012-03-15 18:33:25 +0000717
718 addr = (uint32_t)packet;
Marek Vasut4325d242012-08-26 10:19:21 +0000719 end = roundup(addr + length, ARCH_DMA_MINALIGN);
720 addr &= ~(ARCH_DMA_MINALIGN - 1);
721 flush_dcache_range(addr, end);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000722
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400723 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000724 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
725
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400726 /*
727 * update BD's status now
728 * This block:
729 * - is always the last in a chain (means no chain)
730 * - should transmitt the CRC
731 * - might be the last BD in the list, so the address counter should
732 * wrap (-> keep the WRAP flag)
733 */
734 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
735 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
736 writew(status, &fec->tbd_base[fec->tbd_index].status);
737
738 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000739 * Flush data cache. This code flushes both TX descriptors to RAM.
740 * After this code, the descriptors will be safely in RAM and we
741 * can start DMA.
742 */
743 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
744 addr = (uint32_t)fec->tbd_base;
745 flush_dcache_range(addr, addr + size);
746
747 /*
Marek Vasutd521b3c2013-07-12 01:03:04 +0200748 * Below we read the DMA descriptor's last four bytes back from the
749 * DRAM. This is important in order to make sure that all WRITE
750 * operations on the bus that were triggered by previous cache FLUSH
751 * have completed.
752 *
753 * Otherwise, on MX28, it is possible to observe a corruption of the
754 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
755 * for the bus structure of MX28. The scenario is as follows:
756 *
757 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
758 * to DRAM due to flush_dcache_range()
759 * 2) ARM core writes the FEC registers via AHB_ARB2
760 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
761 *
762 * Note that 2) does sometimes finish before 1) due to reordering of
763 * WRITE accesses on the AHB bus, therefore triggering 3) before the
764 * DMA descriptor is fully written into DRAM. This results in occasional
765 * corruption of the DMA descriptor.
766 */
767 readl(addr + size - 4);
768
769 /*
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400770 * Enable SmartDMA transmit task
771 */
772 fec_tx_task_enable(fec);
773
774 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000775 * Wait until frame is sent. On each turn of the wait cycle, we must
776 * invalidate data cache to see what's really in RAM. Also, we need
777 * barrier here.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400778 */
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000779 while (--timeout) {
Marek Vasutc1582c02012-08-29 03:49:51 +0000780 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
Marek Vasut5f1631d2012-08-29 03:49:49 +0000781 break;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400782 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000783
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300784 if (!timeout) {
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000785 ret = -EINVAL;
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300786 goto out;
787 }
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000788
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300789 /*
790 * The TDAR bit is cleared when the descriptors are all out from TX
791 * but on mx6solox we noticed that the READY bit is still not cleared
792 * right after TDAR.
793 * These are two distinct signals, and in IC simulation, we found that
794 * TDAR always gets cleared prior than the READY bit of last BD becomes
795 * cleared.
796 * In mx6solox, we use a later version of FEC IP. It looks like that
797 * this intrinsic behaviour of TDAR bit has changed in this newer FEC
798 * version.
799 *
800 * Fix this by polling the READY bit of BD after the TDAR polling,
801 * which covers the mx6solox case and does not harm the other SoCs.
802 */
803 timeout = FEC_XFER_TIMEOUT;
804 while (--timeout) {
805 invalidate_dcache_range(addr, addr + size);
806 if (!(readw(&fec->tbd_base[fec->tbd_index].status) &
807 FEC_TBD_READY))
808 break;
809 }
810
811 if (!timeout)
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000812 ret = -EINVAL;
813
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300814out:
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000815 debug("fec_send: status 0x%x index %d ret %i\n",
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400816 readw(&fec->tbd_base[fec->tbd_index].status),
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000817 fec->tbd_index, ret);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400818 /* for next transmission use the other buffer */
819 if (fec->tbd_index)
820 fec->tbd_index = 0;
821 else
822 fec->tbd_index = 1;
823
Marek Vasut5f1631d2012-08-29 03:49:49 +0000824 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400825}
826
827/**
828 * Pull one frame from the card
829 * @param[in] dev Our ethernet device to handle
830 * @return Length of packet read
831 */
Jagan Teki484f0212016-12-06 00:00:49 +0100832#ifdef CONFIG_DM_ETH
833static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp)
834#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400835static int fec_recv(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100836#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400837{
Jagan Teki484f0212016-12-06 00:00:49 +0100838#ifdef CONFIG_DM_ETH
839 struct fec_priv *fec = dev_get_priv(dev);
840#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400841 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100842#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400843 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
844 unsigned long ievent;
845 int frame_length, len = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400846 uint16_t bd_status;
Marek Vasut4325d242012-08-26 10:19:21 +0000847 uint32_t addr, size, end;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000848 int i;
Fabio Estevamcc956082013-09-17 23:13:10 -0300849 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400850
851 /*
852 * Check if any critical events have happened
853 */
854 ievent = readl(&fec->eth->ievent);
855 writel(ievent, &fec->eth->ievent);
Marek Vasut478e2d02011-10-24 23:40:03 +0000856 debug("fec_recv: ievent 0x%lx\n", ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400857 if (ievent & FEC_IEVENT_BABR) {
Jagan Teki484f0212016-12-06 00:00:49 +0100858#ifdef CONFIG_DM_ETH
859 fecmxc_halt(dev);
860 fecmxc_init(dev);
861#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400862 fec_halt(dev);
863 fec_init(dev, fec->bd);
Jagan Teki484f0212016-12-06 00:00:49 +0100864#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400865 printf("some error: 0x%08lx\n", ievent);
866 return 0;
867 }
868 if (ievent & FEC_IEVENT_HBERR) {
869 /* Heartbeat error */
870 writel(0x00000001 | readl(&fec->eth->x_cntrl),
871 &fec->eth->x_cntrl);
872 }
873 if (ievent & FEC_IEVENT_GRA) {
874 /* Graceful stop complete */
875 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
Jagan Teki484f0212016-12-06 00:00:49 +0100876#ifdef CONFIG_DM_ETH
877 fecmxc_halt(dev);
878#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400879 fec_halt(dev);
Jagan Teki484f0212016-12-06 00:00:49 +0100880#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400881 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
882 &fec->eth->x_cntrl);
Jagan Teki484f0212016-12-06 00:00:49 +0100883#ifdef CONFIG_DM_ETH
884 fecmxc_init(dev);
885#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400886 fec_init(dev, fec->bd);
Jagan Teki484f0212016-12-06 00:00:49 +0100887#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400888 }
889 }
890
891 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000892 * Read the buffer status. Before the status can be read, the data cache
893 * must be invalidated, because the data in RAM might have been changed
894 * by DMA. The descriptors are properly aligned to cachelines so there's
895 * no need to worry they'd overlap.
896 *
897 * WARNING: By invalidating the descriptor here, we also invalidate
898 * the descriptors surrounding this one. Therefore we can NOT change the
899 * contents of this descriptor nor the surrounding ones. The problem is
900 * that in order to mark the descriptor as processed, we need to change
901 * the descriptor. The solution is to mark the whole cache line when all
902 * descriptors in the cache line are processed.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400903 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000904 addr = (uint32_t)rbd;
905 addr &= ~(ARCH_DMA_MINALIGN - 1);
906 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
907 invalidate_dcache_range(addr, addr + size);
908
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400909 bd_status = readw(&rbd->status);
910 debug("fec_recv: status 0x%x\n", bd_status);
911
912 if (!(bd_status & FEC_RBD_EMPTY)) {
913 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
914 ((readw(&rbd->data_length) - 4) > 14)) {
915 /*
916 * Get buffer address and size
917 */
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200918 addr = readl(&rbd->data_pointer);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400919 frame_length = readw(&rbd->data_length) - 4;
920 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000921 * Invalidate data cache over the buffer
922 */
Marek Vasut4325d242012-08-26 10:19:21 +0000923 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
924 addr &= ~(ARCH_DMA_MINALIGN - 1);
925 invalidate_dcache_range(addr, end);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000926
927 /*
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400928 * Fill the buffer and pass it to upper layers
929 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000930#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200931 swap_packet((uint32_t *)addr, frame_length);
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000932#endif
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200933 memcpy(buff, (char *)addr, frame_length);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500934 net_process_received_packet(buff, frame_length);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400935 len = frame_length;
936 } else {
937 if (bd_status & FEC_RBD_ERR)
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200938 printf("error frame: 0x%08x 0x%08x\n",
939 addr, bd_status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400940 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000941
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400942 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000943 * Free the current buffer, restart the engine and move forward
944 * to the next buffer. Here we check if the whole cacheline of
945 * descriptors was already processed and if so, we mark it free
946 * as whole.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400947 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000948 size = RXDESC_PER_CACHELINE - 1;
949 if ((fec->rbd_index & size) == size) {
950 i = fec->rbd_index - size;
951 addr = (uint32_t)&fec->rbd_base[i];
952 for (; i <= fec->rbd_index ; i++) {
953 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
954 &fec->rbd_base[i]);
955 }
956 flush_dcache_range(addr,
957 addr + ARCH_DMA_MINALIGN);
958 }
959
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400960 fec_rx_task_enable(fec);
961 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
962 }
963 debug("fec_recv: stop\n");
964
965 return len;
966}
967
Troy Kisky4c2ddec2012-10-22 16:40:44 +0000968static void fec_set_dev_name(char *dest, int dev_id)
969{
970 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
971}
972
Marek Vasut03880452013-10-12 20:36:25 +0200973static int fec_alloc_descs(struct fec_priv *fec)
974{
975 unsigned int size;
976 int i;
977 uint8_t *data;
978
979 /* Allocate TX descriptors. */
980 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
981 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
982 if (!fec->tbd_base)
983 goto err_tx;
984
985 /* Allocate RX descriptors. */
986 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
987 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
988 if (!fec->rbd_base)
989 goto err_rx;
990
991 memset(fec->rbd_base, 0, size);
992
993 /* Allocate RX buffers. */
994
995 /* Maximum RX buffer size. */
Fabio Estevam8b798b22014-08-25 13:34:16 -0300996 size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN);
Marek Vasut03880452013-10-12 20:36:25 +0200997 for (i = 0; i < FEC_RBD_NUM; i++) {
Fabio Estevam8b798b22014-08-25 13:34:16 -0300998 data = memalign(FEC_DMA_RX_MINALIGN, size);
Marek Vasut03880452013-10-12 20:36:25 +0200999 if (!data) {
1000 printf("%s: error allocating rxbuf %d\n", __func__, i);
1001 goto err_ring;
1002 }
1003
1004 memset(data, 0, size);
1005
1006 fec->rbd_base[i].data_pointer = (uint32_t)data;
1007 fec->rbd_base[i].status = FEC_RBD_EMPTY;
1008 fec->rbd_base[i].data_length = 0;
1009 /* Flush the buffer to memory. */
1010 flush_dcache_range((uint32_t)data, (uint32_t)data + size);
1011 }
1012
1013 /* Mark the last RBD to close the ring. */
1014 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
1015
1016 fec->rbd_index = 0;
1017 fec->tbd_index = 0;
1018
1019 return 0;
1020
1021err_ring:
1022 for (; i >= 0; i--)
1023 free((void *)fec->rbd_base[i].data_pointer);
1024 free(fec->rbd_base);
1025err_rx:
1026 free(fec->tbd_base);
1027err_tx:
1028 return -ENOMEM;
1029}
1030
1031static void fec_free_descs(struct fec_priv *fec)
1032{
1033 int i;
1034
1035 for (i = 0; i < FEC_RBD_NUM; i++)
1036 free((void *)fec->rbd_base[i].data_pointer);
1037 free(fec->rbd_base);
1038 free(fec->tbd_base);
1039}
1040
Jagan Teki484f0212016-12-06 00:00:49 +01001041struct mii_dev *fec_get_miibus(uint32_t base_addr, int dev_id)
1042{
1043 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
1044 struct mii_dev *bus;
1045 int ret;
1046
1047 bus = mdio_alloc();
1048 if (!bus) {
1049 printf("mdio_alloc failed\n");
1050 return NULL;
1051 }
1052 bus->read = fec_phy_read;
1053 bus->write = fec_phy_write;
1054 bus->priv = eth;
1055 fec_set_dev_name(bus->name, dev_id);
1056
1057 ret = mdio_register(bus);
1058 if (ret) {
1059 printf("mdio_register failed\n");
1060 free(bus);
1061 return NULL;
1062 }
1063 fec_mii_setspeed(eth);
1064 return bus;
1065}
1066
1067#ifndef CONFIG_DM_ETH
Troy Kiskydce4def2012-10-22 16:40:46 +00001068#ifdef CONFIG_PHYLIB
1069int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1070 struct mii_dev *bus, struct phy_device *phydev)
1071#else
1072static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1073 struct mii_dev *bus, int phy_id)
1074#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001075{
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001076 struct eth_device *edev;
Marek Vasutedcd6c02011-09-16 01:13:47 +02001077 struct fec_priv *fec;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001078 unsigned char ethaddr[6];
Marek Vasut43b10302011-09-11 18:05:37 +00001079 uint32_t start;
1080 int ret = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001081
1082 /* create and fill edev struct */
1083 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
1084 if (!edev) {
Marek Vasutedcd6c02011-09-16 01:13:47 +02001085 puts("fec_mxc: not enough malloc memory for eth_device\n");
Marek Vasut43b10302011-09-11 18:05:37 +00001086 ret = -ENOMEM;
1087 goto err1;
Marek Vasutedcd6c02011-09-16 01:13:47 +02001088 }
1089
1090 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
1091 if (!fec) {
1092 puts("fec_mxc: not enough malloc memory for fec_priv\n");
Marek Vasut43b10302011-09-11 18:05:37 +00001093 ret = -ENOMEM;
1094 goto err2;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001095 }
Marek Vasutedcd6c02011-09-16 01:13:47 +02001096
Nobuhiro Iwamatsu1843c5b2010-10-19 14:03:42 +09001097 memset(edev, 0, sizeof(*edev));
Marek Vasutedcd6c02011-09-16 01:13:47 +02001098 memset(fec, 0, sizeof(*fec));
1099
Marek Vasut03880452013-10-12 20:36:25 +02001100 ret = fec_alloc_descs(fec);
1101 if (ret)
1102 goto err3;
1103
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001104 edev->priv = fec;
1105 edev->init = fec_init;
1106 edev->send = fec_send;
1107 edev->recv = fec_recv;
1108 edev->halt = fec_halt;
Heiko Schocher9ada5e62010-04-27 07:43:52 +02001109 edev->write_hwaddr = fec_set_hwaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001110
Marek Vasutedcd6c02011-09-16 01:13:47 +02001111 fec->eth = (struct ethernet_regs *)base_addr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001112 fec->bd = bd;
1113
Marek Vasutdbb4fce2011-09-11 18:05:33 +00001114 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001115
1116 /* Reset chip. */
John Rigbye650e492010-01-25 23:12:55 -07001117 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
Marek Vasut43b10302011-09-11 18:05:37 +00001118 start = get_timer(0);
1119 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
1120 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -07001121 printf("FEC MXC: Timeout resetting chip\n");
Marek Vasut03880452013-10-12 20:36:25 +02001122 goto err4;
Marek Vasut43b10302011-09-11 18:05:37 +00001123 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001124 udelay(10);
Marek Vasut43b10302011-09-11 18:05:37 +00001125 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001126
Marek Vasut335cbd22012-05-01 11:09:41 +00001127 fec_reg_setup(fec);
Troy Kisky4c2ddec2012-10-22 16:40:44 +00001128 fec_set_dev_name(edev->name, dev_id);
1129 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
Troy Kiskydce4def2012-10-22 16:40:46 +00001130 fec->bus = bus;
1131 fec_mii_setspeed(bus->priv);
1132#ifdef CONFIG_PHYLIB
1133 fec->phydev = phydev;
1134 phy_connect_dev(phydev, edev);
1135 /* Configure phy */
1136 phy_config(phydev);
1137#else
Marek Vasutedcd6c02011-09-16 01:13:47 +02001138 fec->phy_id = phy_id;
Troy Kiskydce4def2012-10-22 16:40:46 +00001139#endif
1140 eth_register(edev);
1141
Jagan Tekibc5fb462016-12-06 00:00:48 +01001142 if (fec_get_hwaddr(dev_id, ethaddr) == 0) {
Troy Kiskydce4def2012-10-22 16:40:46 +00001143 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
1144 memcpy(edev->enetaddr, ethaddr, 6);
Eric Nelson3abc8142013-08-02 10:37:00 -07001145 if (!getenv("ethaddr"))
1146 eth_setenv_enetaddr("ethaddr", ethaddr);
Troy Kiskydce4def2012-10-22 16:40:46 +00001147 }
1148 return ret;
Marek Vasut03880452013-10-12 20:36:25 +02001149err4:
1150 fec_free_descs(fec);
Troy Kiskydce4def2012-10-22 16:40:46 +00001151err3:
1152 free(fec);
1153err2:
1154 free(edev);
1155err1:
1156 return ret;
1157}
1158
Troy Kiskydce4def2012-10-22 16:40:46 +00001159int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1160{
1161 uint32_t base_mii;
1162 struct mii_dev *bus = NULL;
1163#ifdef CONFIG_PHYLIB
1164 struct phy_device *phydev = NULL;
1165#endif
1166 int ret;
1167
Eric Nelson3d2f7272012-03-15 18:33:25 +00001168#ifdef CONFIG_MX28
Troy Kisky2000c662012-02-07 14:08:47 +00001169 /*
1170 * The i.MX28 has two ethernet interfaces, but they are not equal.
1171 * Only the first one can access the MDIO bus.
1172 */
Troy Kiskydce4def2012-10-22 16:40:46 +00001173 base_mii = MXS_ENET0_BASE;
Troy Kisky2000c662012-02-07 14:08:47 +00001174#else
Troy Kiskydce4def2012-10-22 16:40:46 +00001175 base_mii = addr;
Troy Kisky2000c662012-02-07 14:08:47 +00001176#endif
Troy Kiskydce4def2012-10-22 16:40:46 +00001177 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1178 bus = fec_get_miibus(base_mii, dev_id);
1179 if (!bus)
1180 return -ENOMEM;
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001181#ifdef CONFIG_PHYLIB
Troy Kiskydce4def2012-10-22 16:40:46 +00001182 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001183 if (!phydev) {
Måns Rullgårdc6e4a862015-12-08 15:38:46 +00001184 mdio_unregister(bus);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001185 free(bus);
Troy Kiskydce4def2012-10-22 16:40:46 +00001186 return -ENOMEM;
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001187 }
Troy Kiskydce4def2012-10-22 16:40:46 +00001188 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1189#else
1190 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001191#endif
Troy Kiskydce4def2012-10-22 16:40:46 +00001192 if (ret) {
1193#ifdef CONFIG_PHYLIB
1194 free(phydev);
1195#endif
Måns Rullgårdc6e4a862015-12-08 15:38:46 +00001196 mdio_unregister(bus);
Troy Kiskydce4def2012-10-22 16:40:46 +00001197 free(bus);
1198 }
Marek Vasut43b10302011-09-11 18:05:37 +00001199 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001200}
1201
Troy Kisky4e0eae62012-10-22 16:40:42 +00001202#ifdef CONFIG_FEC_MXC_PHYADDR
1203int fecmxc_initialize(bd_t *bd)
1204{
1205 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1206 IMX_FEC_BASE);
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001207}
Troy Kisky4e0eae62012-10-22 16:40:42 +00001208#endif
Marek Vasut539ecee2011-09-11 18:05:36 +00001209
Troy Kisky2000c662012-02-07 14:08:47 +00001210#ifndef CONFIG_PHYLIB
Marek Vasut539ecee2011-09-11 18:05:36 +00001211int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1212{
1213 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1214 fec->mii_postcall = cb;
1215 return 0;
Jagan Teki484f0212016-12-06 00:00:49 +01001216}
1217#endif
1218
1219#else
1220
1221static const struct eth_ops fecmxc_ops = {
1222 .start = fecmxc_init,
1223 .send = fecmxc_send,
1224 .recv = fecmxc_recv,
1225 .stop = fecmxc_halt,
1226 .write_hwaddr = fecmxc_set_hwaddr,
1227};
1228
1229static int fec_phy_init(struct fec_priv *priv, struct udevice *dev)
1230{
1231 struct phy_device *phydev;
1232 int mask = 0xffffffff;
1233
1234#ifdef CONFIG_PHYLIB
1235 mask = 1 << CONFIG_FEC_MXC_PHYADDR;
1236#endif
1237
1238 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
1239 if (!phydev)
1240 return -ENODEV;
1241
1242 phy_connect_dev(phydev, dev);
1243
1244 priv->phydev = phydev;
1245 phy_config(phydev);
1246
1247 return 0;
1248}
1249
1250static int fecmxc_probe(struct udevice *dev)
1251{
1252 struct eth_pdata *pdata = dev_get_platdata(dev);
1253 struct fec_priv *priv = dev_get_priv(dev);
1254 struct mii_dev *bus = NULL;
1255 int dev_id = -1;
1256 unsigned char ethaddr[6];
1257 uint32_t start;
1258 int ret;
1259
1260 ret = fec_alloc_descs(priv);
1261 if (ret)
1262 return ret;
1263
1264 bus = fec_get_miibus((uint32_t)priv->eth, dev_id);
1265 if (!bus)
1266 goto err_mii;
1267
1268 priv->bus = bus;
1269 priv->xcv_type = CONFIG_FEC_XCV_TYPE;
1270 priv->interface = pdata->phy_interface;
1271 ret = fec_phy_init(priv, dev);
1272 if (ret)
1273 goto err_phy;
1274
1275 /* Reset chip. */
1276 writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET, &priv->eth->ecntrl);
1277 start = get_timer(0);
1278 while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) {
1279 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1280 printf("FEC MXC: Timeout reseting chip\n");
1281 goto err_timeout;
1282 }
1283 udelay(10);
1284 }
1285
1286 fec_reg_setup(priv);
1287 fec_set_dev_name((char *)dev->name, dev_id);
1288 priv->dev_id = (dev_id == -1) ? 0 : dev_id;
1289
1290 ret = fec_get_hwaddr(dev_id, ethaddr);
1291 if (!ret) {
1292 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
1293 memcpy(pdata->enetaddr, ethaddr, 6);
1294 if (!getenv("ethaddr"))
1295 eth_setenv_enetaddr("ethaddr", ethaddr);
1296 }
1297
1298 return 0;
1299
1300err_timeout:
1301 free(priv->phydev);
1302err_phy:
1303 mdio_unregister(bus);
1304 free(bus);
1305err_mii:
1306 fec_free_descs(priv);
1307 return ret;
Marek Vasut539ecee2011-09-11 18:05:36 +00001308}
Jagan Teki484f0212016-12-06 00:00:49 +01001309
1310static int fecmxc_remove(struct udevice *dev)
1311{
1312 struct fec_priv *priv = dev_get_priv(dev);
1313
1314 free(priv->phydev);
1315 fec_free_descs(priv);
1316 mdio_unregister(priv->bus);
1317 mdio_free(priv->bus);
1318
1319 return 0;
1320}
1321
1322static int fecmxc_ofdata_to_platdata(struct udevice *dev)
1323{
1324 struct eth_pdata *pdata = dev_get_platdata(dev);
1325 struct fec_priv *priv = dev_get_priv(dev);
1326 const char *phy_mode;
1327
1328 pdata->iobase = (phys_addr_t)dev_get_addr(dev);
1329 priv->eth = (struct ethernet_regs *)pdata->iobase;
1330
1331 pdata->phy_interface = -1;
1332 phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
1333 if (phy_mode)
1334 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1335 if (pdata->phy_interface == -1) {
1336 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1337 return -EINVAL;
1338 }
1339
1340 /* TODO
1341 * Need to get the reset-gpio and related properties from DT
1342 * and implemet the enet reset code on .probe call
1343 */
1344
1345 return 0;
1346}
1347
1348static const struct udevice_id fecmxc_ids[] = {
1349 { .compatible = "fsl,imx6q-fec" },
1350 { }
1351};
1352
1353U_BOOT_DRIVER(fecmxc_gem) = {
1354 .name = "fecmxc",
1355 .id = UCLASS_ETH,
1356 .of_match = fecmxc_ids,
1357 .ofdata_to_platdata = fecmxc_ofdata_to_platdata,
1358 .probe = fecmxc_probe,
1359 .remove = fecmxc_remove,
1360 .ops = &fecmxc_ops,
1361 .priv_auto_alloc_size = sizeof(struct fec_priv),
1362 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1363};
Troy Kisky2000c662012-02-07 14:08:47 +00001364#endif