]> Git Repo - J-u-boot.git/blame - drivers/net/fec_mxc.c
net: Move enetaddr env access code to env config instead of net config
[J-u-boot.git] / drivers / net / fec_mxc.c
CommitLineData
0b23fb36
IY
1/*
2 * (C) Copyright 2009 Ilya Yanok, Emcraft Systems Ltd <[email protected]>
3 * (C) Copyright 2008,2009 Eric Jarrige <[email protected]>
4 * (C) Copyright 2008 Armadeus Systems nc
5 * (C) Copyright 2007 Pengutronix, Sascha Hauer <[email protected]>
6 * (C) Copyright 2007 Pengutronix, Juergen Beisert <[email protected]>
7 *
1a459660 8 * SPDX-License-Identifier: GPL-2.0+
0b23fb36
IY
9 */
10
11#include <common.h>
60752ca8 12#include <dm.h>
9925f1db 13#include <environment.h>
0b23fb36 14#include <malloc.h>
cf92e05c 15#include <memalign.h>
567173a6 16#include <miiphy.h>
0b23fb36 17#include <net.h>
84f64c8b 18#include <netdev.h>
0b23fb36
IY
19#include "fec_mxc.h"
20
0b23fb36 21#include <asm/io.h>
1221ce45 22#include <linux/errno.h>
e2a66e60 23#include <linux/compiler.h>
0b23fb36 24
567173a6
JT
25#include <asm/arch/clock.h>
26#include <asm/arch/imx-regs.h>
552a848e 27#include <asm/mach-imx/sys_proto.h>
567173a6 28
0b23fb36
IY
29DECLARE_GLOBAL_DATA_PTR;
30
bc1ce150
MV
31/*
32 * Timeout the transfer after 5 mS. This is usually a bit more, since
33 * the code in the tightloops this timeout is used in adds some overhead.
34 */
35#define FEC_XFER_TIMEOUT 5000
36
db5b7f56
FE
37/*
38 * The standard 32-byte DMA alignment does not work on mx6solox, which requires
39 * 64-byte alignment in the DMA RX FEC buffer.
40 * Introduce the FEC_DMA_RX_MINALIGN which can cover mx6solox needs and also
41 * satisfies the alignment on other SoCs (32-bytes)
42 */
43#define FEC_DMA_RX_MINALIGN 64
44
0b23fb36
IY
45#ifndef CONFIG_MII
46#error "CONFIG_MII has to be defined!"
47#endif
48
5c1ad3e6
EN
49#ifndef CONFIG_FEC_XCV_TYPE
50#define CONFIG_FEC_XCV_TYPE MII100
392b8502
MV
51#endif
52
be7e87e2
MV
53/*
54 * The i.MX28 operates with packets in big endian. We need to swap them before
55 * sending and after receiving.
56 */
5c1ad3e6
EN
57#ifdef CONFIG_MX28
58#define CONFIG_FEC_MXC_SWAP_PACKET
59#endif
60
61#define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
62
63/* Check various alignment issues at compile time */
64#if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
65#error "ARCH_DMA_MINALIGN must be multiple of 16!"
66#endif
67
68#if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
69 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
70#error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
be7e87e2
MV
71#endif
72
0b23fb36
IY
73#undef DEBUG
74
5c1ad3e6 75#ifdef CONFIG_FEC_MXC_SWAP_PACKET
be7e87e2
MV
76static void swap_packet(uint32_t *packet, int length)
77{
78 int i;
79
80 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
81 packet[i] = __swab32(packet[i]);
82}
83#endif
84
567173a6
JT
85/* MII-interface related functions */
86static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyaddr,
87 uint8_t regaddr)
0b23fb36 88{
0b23fb36
IY
89 uint32_t reg; /* convenient holder for the PHY register */
90 uint32_t phy; /* convenient holder for the PHY */
91 uint32_t start;
13947f43 92 int val;
0b23fb36
IY
93
94 /*
95 * reading from any PHY's register is done by properly
96 * programming the FEC's MII data register.
97 */
d133b881 98 writel(FEC_IEVENT_MII, &eth->ievent);
567173a6
JT
99 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
100 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
0b23fb36
IY
101
102 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
d133b881 103 phy | reg, &eth->mii_data);
0b23fb36 104
567173a6 105 /* wait for the related interrupt */
a60d1e5b 106 start = get_timer(0);
d133b881 107 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
0b23fb36
IY
108 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
109 printf("Read MDIO failed...\n");
110 return -1;
111 }
112 }
113
567173a6 114 /* clear mii interrupt bit */
d133b881 115 writel(FEC_IEVENT_MII, &eth->ievent);
0b23fb36 116
567173a6 117 /* it's now safe to read the PHY's register */
13947f43 118 val = (unsigned short)readl(&eth->mii_data);
567173a6
JT
119 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
120 regaddr, val);
13947f43 121 return val;
0b23fb36
IY
122}
123
575c5cc0 124static void fec_mii_setspeed(struct ethernet_regs *eth)
4294b248
SB
125{
126 /*
127 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
128 * and do not drop the Preamble.
843a3e58
MR
129 *
130 * The i.MX28 and i.MX6 types have another field in the MSCR (aka
131 * MII_SPEED) register that defines the MDIO output hold time. Earlier
132 * versions are RAZ there, so just ignore the difference and write the
133 * register always.
134 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
135 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
136 * output.
137 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
138 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
139 * holdtime cannot result in a value greater than 3.
4294b248 140 */
843a3e58
MR
141 u32 pclk = imx_get_fecclk();
142 u32 speed = DIV_ROUND_UP(pclk, 5000000);
143 u32 hold = DIV_ROUND_UP(pclk, 100000000) - 1;
6ba45cc0
MN
144#ifdef FEC_QUIRK_ENET_MAC
145 speed--;
146#endif
843a3e58 147 writel(speed << 1 | hold << 8, &eth->mii_speed);
575c5cc0 148 debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
4294b248 149}
0b23fb36 150
567173a6
JT
151static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyaddr,
152 uint8_t regaddr, uint16_t data)
13947f43 153{
0b23fb36
IY
154 uint32_t reg; /* convenient holder for the PHY register */
155 uint32_t phy; /* convenient holder for the PHY */
156 uint32_t start;
157
567173a6
JT
158 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
159 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
0b23fb36
IY
160
161 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
d133b881 162 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
0b23fb36 163
567173a6 164 /* wait for the MII interrupt */
a60d1e5b 165 start = get_timer(0);
d133b881 166 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
0b23fb36
IY
167 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
168 printf("Write MDIO failed...\n");
169 return -1;
170 }
171 }
172
567173a6 173 /* clear MII interrupt bit */
d133b881 174 writel(FEC_IEVENT_MII, &eth->ievent);
567173a6
JT
175 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
176 regaddr, data);
0b23fb36
IY
177
178 return 0;
179}
180
567173a6
JT
181static int fec_phy_read(struct mii_dev *bus, int phyaddr, int dev_addr,
182 int regaddr)
13947f43 183{
567173a6 184 return fec_mdio_read(bus->priv, phyaddr, regaddr);
13947f43
TK
185}
186
567173a6
JT
187static int fec_phy_write(struct mii_dev *bus, int phyaddr, int dev_addr,
188 int regaddr, u16 data)
13947f43 189{
567173a6 190 return fec_mdio_write(bus->priv, phyaddr, regaddr, data);
13947f43
TK
191}
192
193#ifndef CONFIG_PHYLIB
0b23fb36
IY
194static int miiphy_restart_aneg(struct eth_device *dev)
195{
b774fe9d
SB
196 int ret = 0;
197#if !defined(CONFIG_FEC_MXC_NO_ANEG)
9e27e9dc 198 struct fec_priv *fec = (struct fec_priv *)dev->priv;
13947f43 199 struct ethernet_regs *eth = fec->bus->priv;
9e27e9dc 200
0b23fb36
IY
201 /*
202 * Wake up from sleep if necessary
203 * Reset PHY, then delay 300ns
204 */
cb17b92d 205#ifdef CONFIG_MX27
13947f43 206 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
cb17b92d 207#endif
13947f43 208 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
0b23fb36
IY
209 udelay(1000);
210
567173a6 211 /* Set the auto-negotiation advertisement register bits */
13947f43 212 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
567173a6
JT
213 LPA_100FULL | LPA_100HALF | LPA_10FULL |
214 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
13947f43 215 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
567173a6 216 BMCR_ANENABLE | BMCR_ANRESTART);
2e5f4421
MV
217
218 if (fec->mii_postcall)
219 ret = fec->mii_postcall(fec->phy_id);
220
b774fe9d 221#endif
2e5f4421 222 return ret;
0b23fb36
IY
223}
224
0750701a 225#ifndef CONFIG_FEC_FIXED_SPEED
0b23fb36
IY
226static int miiphy_wait_aneg(struct eth_device *dev)
227{
228 uint32_t start;
13947f43 229 int status;
9e27e9dc 230 struct fec_priv *fec = (struct fec_priv *)dev->priv;
13947f43 231 struct ethernet_regs *eth = fec->bus->priv;
0b23fb36 232
567173a6 233 /* Wait for AN completion */
a60d1e5b 234 start = get_timer(0);
0b23fb36
IY
235 do {
236 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
237 printf("%s: Autonegotiation timeout\n", dev->name);
238 return -1;
239 }
240
13947f43
TK
241 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
242 if (status < 0) {
243 printf("%s: Autonegotiation failed. status: %d\n",
567173a6 244 dev->name, status);
0b23fb36
IY
245 return -1;
246 }
8ef583a0 247 } while (!(status & BMSR_LSTATUS));
0b23fb36
IY
248
249 return 0;
250}
0750701a 251#endif /* CONFIG_FEC_FIXED_SPEED */
13947f43
TK
252#endif
253
0b23fb36
IY
254static int fec_rx_task_enable(struct fec_priv *fec)
255{
c0b5a3bb 256 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
0b23fb36
IY
257 return 0;
258}
259
260static int fec_rx_task_disable(struct fec_priv *fec)
261{
262 return 0;
263}
264
265static int fec_tx_task_enable(struct fec_priv *fec)
266{
c0b5a3bb 267 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
0b23fb36
IY
268 return 0;
269}
270
271static int fec_tx_task_disable(struct fec_priv *fec)
272{
273 return 0;
274}
275
276/**
277 * Initialize receive task's buffer descriptors
278 * @param[in] fec all we know about the device yet
279 * @param[in] count receive buffer count to be allocated
5c1ad3e6 280 * @param[in] dsize desired size of each receive buffer
0b23fb36
IY
281 * @return 0 on success
282 *
79e5f27b 283 * Init all RX descriptors to default values.
0b23fb36 284 */
79e5f27b 285static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
0b23fb36 286{
5c1ad3e6 287 uint32_t size;
f24e482a 288 ulong data;
5c1ad3e6
EN
289 int i;
290
0b23fb36 291 /*
79e5f27b
MV
292 * Reload the RX descriptors with default values and wipe
293 * the RX buffers.
0b23fb36 294 */
5c1ad3e6
EN
295 size = roundup(dsize, ARCH_DMA_MINALIGN);
296 for (i = 0; i < count; i++) {
f24e482a
YL
297 data = fec->rbd_base[i].data_pointer;
298 memset((void *)data, 0, dsize);
299 flush_dcache_range(data, data + size);
79e5f27b
MV
300
301 fec->rbd_base[i].status = FEC_RBD_EMPTY;
302 fec->rbd_base[i].data_length = 0;
5c1ad3e6
EN
303 }
304
305 /* Mark the last RBD to close the ring. */
79e5f27b 306 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
0b23fb36
IY
307 fec->rbd_index = 0;
308
f24e482a
YL
309 flush_dcache_range((ulong)fec->rbd_base,
310 (ulong)fec->rbd_base + size);
0b23fb36
IY
311}
312
313/**
314 * Initialize transmit task's buffer descriptors
315 * @param[in] fec all we know about the device yet
316 *
317 * Transmit buffers are created externally. We only have to init the BDs here.\n
318 * Note: There is a race condition in the hardware. When only one BD is in
319 * use it must be marked with the WRAP bit to use it for every transmitt.
320 * This bit in combination with the READY bit results into double transmit
321 * of each data buffer. It seems the state machine checks READY earlier then
322 * resetting it after the first transfer.
323 * Using two BDs solves this issue.
324 */
325static void fec_tbd_init(struct fec_priv *fec)
326{
f24e482a 327 ulong addr = (ulong)fec->tbd_base;
5c1ad3e6
EN
328 unsigned size = roundup(2 * sizeof(struct fec_bd),
329 ARCH_DMA_MINALIGN);
79e5f27b
MV
330
331 memset(fec->tbd_base, 0, size);
332 fec->tbd_base[0].status = 0;
333 fec->tbd_base[1].status = FEC_TBD_WRAP;
0b23fb36 334 fec->tbd_index = 0;
79e5f27b 335 flush_dcache_range(addr, addr + size);
0b23fb36
IY
336}
337
338/**
339 * Mark the given read buffer descriptor as free
340 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
567173a6 341 * @param[in] prbd buffer descriptor to mark free again
0b23fb36 342 */
567173a6 343static void fec_rbd_clean(int last, struct fec_bd *prbd)
0b23fb36 344{
5c1ad3e6 345 unsigned short flags = FEC_RBD_EMPTY;
0b23fb36 346 if (last)
5c1ad3e6 347 flags |= FEC_RBD_WRAP;
567173a6
JT
348 writew(flags, &prbd->status);
349 writew(0, &prbd->data_length);
0b23fb36
IY
350}
351
f54183e6 352static int fec_get_hwaddr(int dev_id, unsigned char *mac)
0b23fb36 353{
be252b65 354 imx_get_mac_from_fuse(dev_id, mac);
0adb5b76 355 return !is_valid_ethaddr(mac);
0b23fb36
IY
356}
357
60752ca8
JT
358#ifdef CONFIG_DM_ETH
359static int fecmxc_set_hwaddr(struct udevice *dev)
360#else
4294b248 361static int fec_set_hwaddr(struct eth_device *dev)
60752ca8 362#endif
0b23fb36 363{
60752ca8
JT
364#ifdef CONFIG_DM_ETH
365 struct fec_priv *fec = dev_get_priv(dev);
366 struct eth_pdata *pdata = dev_get_platdata(dev);
367 uchar *mac = pdata->enetaddr;
368#else
4294b248 369 uchar *mac = dev->enetaddr;
0b23fb36 370 struct fec_priv *fec = (struct fec_priv *)dev->priv;
60752ca8 371#endif
0b23fb36
IY
372
373 writel(0, &fec->eth->iaddr1);
374 writel(0, &fec->eth->iaddr2);
375 writel(0, &fec->eth->gaddr1);
376 writel(0, &fec->eth->gaddr2);
377
567173a6 378 /* Set physical address */
0b23fb36 379 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
567173a6 380 &fec->eth->paddr1);
0b23fb36
IY
381 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
382
383 return 0;
384}
385
567173a6 386/* Do initial configuration of the FEC registers */
a5990b26
MV
387static void fec_reg_setup(struct fec_priv *fec)
388{
389 uint32_t rcntrl;
390
567173a6 391 /* Set interrupt mask register */
a5990b26
MV
392 writel(0x00000000, &fec->eth->imask);
393
567173a6 394 /* Clear FEC-Lite interrupt event register(IEVENT) */
a5990b26
MV
395 writel(0xffffffff, &fec->eth->ievent);
396
567173a6 397 /* Set FEC-Lite receive control register(R_CNTRL): */
a5990b26
MV
398
399 /* Start with frame length = 1518, common for all modes. */
400 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
9d2d924a 401 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
402 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
403 if (fec->xcv_type == RGMII)
a5990b26
MV
404 rcntrl |= FEC_RCNTRL_RGMII;
405 else if (fec->xcv_type == RMII)
406 rcntrl |= FEC_RCNTRL_RMII;
a5990b26
MV
407
408 writel(rcntrl, &fec->eth->r_cntrl);
409}
410
0b23fb36
IY
411/**
412 * Start the FEC engine
413 * @param[in] dev Our device to handle
414 */
60752ca8
JT
415#ifdef CONFIG_DM_ETH
416static int fec_open(struct udevice *dev)
417#else
0b23fb36 418static int fec_open(struct eth_device *edev)
60752ca8 419#endif
0b23fb36 420{
60752ca8
JT
421#ifdef CONFIG_DM_ETH
422 struct fec_priv *fec = dev_get_priv(dev);
423#else
0b23fb36 424 struct fec_priv *fec = (struct fec_priv *)edev->priv;
60752ca8 425#endif
28774cba 426 int speed;
f24e482a 427 ulong addr, size;
5c1ad3e6 428 int i;
0b23fb36
IY
429
430 debug("fec_open: fec_open(dev)\n");
431 /* full-duplex, heartbeat disabled */
432 writel(1 << 2, &fec->eth->x_cntrl);
433 fec->rbd_index = 0;
434
5c1ad3e6
EN
435 /* Invalidate all descriptors */
436 for (i = 0; i < FEC_RBD_NUM - 1; i++)
437 fec_rbd_clean(0, &fec->rbd_base[i]);
438 fec_rbd_clean(1, &fec->rbd_base[i]);
439
440 /* Flush the descriptors into RAM */
441 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
442 ARCH_DMA_MINALIGN);
f24e482a 443 addr = (ulong)fec->rbd_base;
5c1ad3e6
EN
444 flush_dcache_range(addr, addr + size);
445
28774cba 446#ifdef FEC_QUIRK_ENET_MAC
2ef2b950
JL
447 /* Enable ENET HW endian SWAP */
448 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
567173a6 449 &fec->eth->ecntrl);
2ef2b950
JL
450 /* Enable ENET store and forward mode */
451 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
567173a6 452 &fec->eth->x_wmrk);
2ef2b950 453#endif
567173a6 454 /* Enable FEC-Lite controller */
cb17b92d 455 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
567173a6
JT
456 &fec->eth->ecntrl);
457
7df51fd8 458#if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
740d6ae5 459 udelay(100);
740d6ae5 460
567173a6 461 /* setup the MII gasket for RMII mode */
740d6ae5
JR
462 /* disable the gasket */
463 writew(0, &fec->eth->miigsk_enr);
464
465 /* wait for the gasket to be disabled */
466 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
467 udelay(2);
468
469 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
470 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
471
472 /* re-enable the gasket */
473 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
474
475 /* wait until MII gasket is ready */
476 int max_loops = 10;
477 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
478 if (--max_loops <= 0) {
479 printf("WAIT for MII Gasket ready timed out\n");
480 break;
481 }
482 }
483#endif
0b23fb36 484
13947f43 485#ifdef CONFIG_PHYLIB
4dc27eed 486 {
13947f43 487 /* Start up the PHY */
11af8d65
TT
488 int ret = phy_startup(fec->phydev);
489
490 if (ret) {
491 printf("Could not initialize PHY %s\n",
492 fec->phydev->dev->name);
493 return ret;
494 }
13947f43 495 speed = fec->phydev->speed;
13947f43 496 }
0750701a
HS
497#elif CONFIG_FEC_FIXED_SPEED
498 speed = CONFIG_FEC_FIXED_SPEED;
13947f43 499#else
0b23fb36 500 miiphy_wait_aneg(edev);
28774cba 501 speed = miiphy_speed(edev->name, fec->phy_id);
9e27e9dc 502 miiphy_duplex(edev->name, fec->phy_id);
13947f43 503#endif
0b23fb36 504
28774cba
TK
505#ifdef FEC_QUIRK_ENET_MAC
506 {
507 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
bcb6e902 508 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
28774cba
TK
509 if (speed == _1000BASET)
510 ecr |= FEC_ECNTRL_SPEED;
511 else if (speed != _100BASET)
512 rcr |= FEC_RCNTRL_RMII_10T;
513 writel(ecr, &fec->eth->ecntrl);
514 writel(rcr, &fec->eth->r_cntrl);
515 }
516#endif
517 debug("%s:Speed=%i\n", __func__, speed);
518
567173a6 519 /* Enable SmartDMA receive task */
0b23fb36
IY
520 fec_rx_task_enable(fec);
521
522 udelay(100000);
523 return 0;
524}
525
60752ca8
JT
526#ifdef CONFIG_DM_ETH
527static int fecmxc_init(struct udevice *dev)
528#else
567173a6 529static int fec_init(struct eth_device *dev, bd_t *bd)
60752ca8 530#endif
0b23fb36 531{
60752ca8
JT
532#ifdef CONFIG_DM_ETH
533 struct fec_priv *fec = dev_get_priv(dev);
534#else
0b23fb36 535 struct fec_priv *fec = (struct fec_priv *)dev->priv;
60752ca8 536#endif
f24e482a
YL
537 u8 *mib_ptr = (uint8_t *)&fec->eth->rmon_t_drop;
538 u8 *i;
539 ulong addr;
0b23fb36 540
e9319f11 541 /* Initialize MAC address */
60752ca8
JT
542#ifdef CONFIG_DM_ETH
543 fecmxc_set_hwaddr(dev);
544#else
e9319f11 545 fec_set_hwaddr(dev);
60752ca8 546#endif
e9319f11 547
567173a6 548 /* Setup transmit descriptors, there are two in total. */
79e5f27b 549 fec_tbd_init(fec);
0b23fb36 550
79e5f27b
MV
551 /* Setup receive descriptors. */
552 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
0b23fb36 553
a5990b26 554 fec_reg_setup(fec);
9eb3770b 555
f41471e6 556 if (fec->xcv_type != SEVENWIRE)
575c5cc0 557 fec_mii_setspeed(fec->bus->priv);
9eb3770b 558
567173a6 559 /* Set Opcode/Pause Duration Register */
0b23fb36
IY
560 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
561 writel(0x2, &fec->eth->x_wmrk);
567173a6
JT
562
563 /* Set multicast address filter */
0b23fb36
IY
564 writel(0x00000000, &fec->eth->gaddr1);
565 writel(0x00000000, &fec->eth->gaddr2);
566
238a53c7
PF
567 /* Do not access reserved register */
568 if (!is_mx6ul() && !is_mx6ull() && !is_mx8m()) {
fbecbaa1
PF
569 /* clear MIB RAM */
570 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
571 writel(0, i);
0b23fb36 572
fbecbaa1
PF
573 /* FIFO receive start register */
574 writel(0x520, &fec->eth->r_fstart);
575 }
0b23fb36
IY
576
577 /* size and address of each buffer */
578 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
f24e482a
YL
579
580 addr = (ulong)fec->tbd_base;
581 writel((uint32_t)addr, &fec->eth->etdsr);
582
583 addr = (ulong)fec->rbd_base;
584 writel((uint32_t)addr, &fec->eth->erdsr);
0b23fb36 585
13947f43 586#ifndef CONFIG_PHYLIB
0b23fb36
IY
587 if (fec->xcv_type != SEVENWIRE)
588 miiphy_restart_aneg(dev);
13947f43 589#endif
0b23fb36
IY
590 fec_open(dev);
591 return 0;
592}
593
594/**
595 * Halt the FEC engine
596 * @param[in] dev Our device to handle
597 */
60752ca8
JT
598#ifdef CONFIG_DM_ETH
599static void fecmxc_halt(struct udevice *dev)
600#else
0b23fb36 601static void fec_halt(struct eth_device *dev)
60752ca8 602#endif
0b23fb36 603{
60752ca8
JT
604#ifdef CONFIG_DM_ETH
605 struct fec_priv *fec = dev_get_priv(dev);
606#else
9e27e9dc 607 struct fec_priv *fec = (struct fec_priv *)dev->priv;
60752ca8 608#endif
0b23fb36
IY
609 int counter = 0xffff;
610
567173a6 611 /* issue graceful stop command to the FEC transmitter if necessary */
cb17b92d 612 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
567173a6 613 &fec->eth->x_cntrl);
0b23fb36
IY
614
615 debug("eth_halt: wait for stop regs\n");
567173a6 616 /* wait for graceful stop to register */
0b23fb36 617 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
cb17b92d 618 udelay(1);
0b23fb36 619
567173a6 620 /* Disable SmartDMA tasks */
0b23fb36
IY
621 fec_tx_task_disable(fec);
622 fec_rx_task_disable(fec);
623
624 /*
625 * Disable the Ethernet Controller
626 * Note: this will also reset the BD index counter!
627 */
740d6ae5 628 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
567173a6 629 &fec->eth->ecntrl);
0b23fb36
IY
630 fec->rbd_index = 0;
631 fec->tbd_index = 0;
0b23fb36
IY
632 debug("eth_halt: done\n");
633}
634
635/**
636 * Transmit one frame
637 * @param[in] dev Our ethernet device to handle
638 * @param[in] packet Pointer to the data to be transmitted
639 * @param[in] length Data count in bytes
640 * @return 0 on success
641 */
60752ca8
JT
642#ifdef CONFIG_DM_ETH
643static int fecmxc_send(struct udevice *dev, void *packet, int length)
644#else
442dac4c 645static int fec_send(struct eth_device *dev, void *packet, int length)
60752ca8 646#endif
0b23fb36
IY
647{
648 unsigned int status;
f24e482a
YL
649 u32 size;
650 ulong addr, end;
bc1ce150
MV
651 int timeout = FEC_XFER_TIMEOUT;
652 int ret = 0;
0b23fb36
IY
653
654 /*
655 * This routine transmits one frame. This routine only accepts
656 * 6-byte Ethernet addresses.
657 */
60752ca8
JT
658#ifdef CONFIG_DM_ETH
659 struct fec_priv *fec = dev_get_priv(dev);
660#else
0b23fb36 661 struct fec_priv *fec = (struct fec_priv *)dev->priv;
60752ca8 662#endif
0b23fb36
IY
663
664 /*
665 * Check for valid length of data.
666 */
667 if ((length > 1500) || (length <= 0)) {
4294b248 668 printf("Payload (%d) too large\n", length);
0b23fb36
IY
669 return -1;
670 }
671
672 /*
5c1ad3e6
EN
673 * Setup the transmit buffer. We are always using the first buffer for
674 * transmission, the second will be empty and only used to stop the DMA
675 * engine. We also flush the packet to RAM here to avoid cache trouble.
0b23fb36 676 */
5c1ad3e6 677#ifdef CONFIG_FEC_MXC_SWAP_PACKET
be7e87e2
MV
678 swap_packet((uint32_t *)packet, length);
679#endif
5c1ad3e6 680
f24e482a 681 addr = (ulong)packet;
efe24d2e
MV
682 end = roundup(addr + length, ARCH_DMA_MINALIGN);
683 addr &= ~(ARCH_DMA_MINALIGN - 1);
684 flush_dcache_range(addr, end);
5c1ad3e6 685
0b23fb36 686 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
f24e482a 687 writel((uint32_t)addr, &fec->tbd_base[fec->tbd_index].data_pointer);
5c1ad3e6 688
0b23fb36
IY
689 /*
690 * update BD's status now
691 * This block:
692 * - is always the last in a chain (means no chain)
693 * - should transmitt the CRC
694 * - might be the last BD in the list, so the address counter should
695 * wrap (-> keep the WRAP flag)
696 */
697 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
698 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
699 writew(status, &fec->tbd_base[fec->tbd_index].status);
700
5c1ad3e6
EN
701 /*
702 * Flush data cache. This code flushes both TX descriptors to RAM.
703 * After this code, the descriptors will be safely in RAM and we
704 * can start DMA.
705 */
706 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
f24e482a 707 addr = (ulong)fec->tbd_base;
5c1ad3e6
EN
708 flush_dcache_range(addr, addr + size);
709
ab94cd49
MV
710 /*
711 * Below we read the DMA descriptor's last four bytes back from the
712 * DRAM. This is important in order to make sure that all WRITE
713 * operations on the bus that were triggered by previous cache FLUSH
714 * have completed.
715 *
716 * Otherwise, on MX28, it is possible to observe a corruption of the
717 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
718 * for the bus structure of MX28. The scenario is as follows:
719 *
720 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
721 * to DRAM due to flush_dcache_range()
722 * 2) ARM core writes the FEC registers via AHB_ARB2
723 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
724 *
725 * Note that 2) does sometimes finish before 1) due to reordering of
726 * WRITE accesses on the AHB bus, therefore triggering 3) before the
727 * DMA descriptor is fully written into DRAM. This results in occasional
728 * corruption of the DMA descriptor.
729 */
730 readl(addr + size - 4);
731
567173a6 732 /* Enable SmartDMA transmit task */
0b23fb36
IY
733 fec_tx_task_enable(fec);
734
735 /*
5c1ad3e6
EN
736 * Wait until frame is sent. On each turn of the wait cycle, we must
737 * invalidate data cache to see what's really in RAM. Also, we need
738 * barrier here.
0b23fb36 739 */
67449098 740 while (--timeout) {
c0b5a3bb 741 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
bc1ce150 742 break;
0b23fb36 743 }
5c1ad3e6 744
f599288d 745 if (!timeout) {
67449098 746 ret = -EINVAL;
f599288d
FE
747 goto out;
748 }
749
750 /*
751 * The TDAR bit is cleared when the descriptors are all out from TX
752 * but on mx6solox we noticed that the READY bit is still not cleared
753 * right after TDAR.
754 * These are two distinct signals, and in IC simulation, we found that
755 * TDAR always gets cleared prior than the READY bit of last BD becomes
756 * cleared.
757 * In mx6solox, we use a later version of FEC IP. It looks like that
758 * this intrinsic behaviour of TDAR bit has changed in this newer FEC
759 * version.
760 *
761 * Fix this by polling the READY bit of BD after the TDAR polling,
762 * which covers the mx6solox case and does not harm the other SoCs.
763 */
764 timeout = FEC_XFER_TIMEOUT;
765 while (--timeout) {
766 invalidate_dcache_range(addr, addr + size);
767 if (!(readw(&fec->tbd_base[fec->tbd_index].status) &
768 FEC_TBD_READY))
769 break;
770 }
67449098 771
f599288d 772 if (!timeout)
67449098
MV
773 ret = -EINVAL;
774
f599288d 775out:
67449098 776 debug("fec_send: status 0x%x index %d ret %i\n",
567173a6
JT
777 readw(&fec->tbd_base[fec->tbd_index].status),
778 fec->tbd_index, ret);
0b23fb36
IY
779 /* for next transmission use the other buffer */
780 if (fec->tbd_index)
781 fec->tbd_index = 0;
782 else
783 fec->tbd_index = 1;
784
bc1ce150 785 return ret;
0b23fb36
IY
786}
787
788/**
789 * Pull one frame from the card
790 * @param[in] dev Our ethernet device to handle
791 * @return Length of packet read
792 */
60752ca8
JT
793#ifdef CONFIG_DM_ETH
794static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp)
795#else
0b23fb36 796static int fec_recv(struct eth_device *dev)
60752ca8 797#endif
0b23fb36 798{
60752ca8
JT
799#ifdef CONFIG_DM_ETH
800 struct fec_priv *fec = dev_get_priv(dev);
801#else
0b23fb36 802 struct fec_priv *fec = (struct fec_priv *)dev->priv;
60752ca8 803#endif
0b23fb36
IY
804 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
805 unsigned long ievent;
806 int frame_length, len = 0;
0b23fb36 807 uint16_t bd_status;
f24e482a 808 ulong addr, size, end;
5c1ad3e6 809 int i;
fd37f195 810 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
0b23fb36 811
567173a6 812 /* Check if any critical events have happened */
0b23fb36
IY
813 ievent = readl(&fec->eth->ievent);
814 writel(ievent, &fec->eth->ievent);
eda959f3 815 debug("fec_recv: ievent 0x%lx\n", ievent);
0b23fb36 816 if (ievent & FEC_IEVENT_BABR) {
60752ca8
JT
817#ifdef CONFIG_DM_ETH
818 fecmxc_halt(dev);
819 fecmxc_init(dev);
820#else
0b23fb36
IY
821 fec_halt(dev);
822 fec_init(dev, fec->bd);
60752ca8 823#endif
0b23fb36
IY
824 printf("some error: 0x%08lx\n", ievent);
825 return 0;
826 }
827 if (ievent & FEC_IEVENT_HBERR) {
828 /* Heartbeat error */
829 writel(0x00000001 | readl(&fec->eth->x_cntrl),
567173a6 830 &fec->eth->x_cntrl);
0b23fb36
IY
831 }
832 if (ievent & FEC_IEVENT_GRA) {
833 /* Graceful stop complete */
834 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
60752ca8
JT
835#ifdef CONFIG_DM_ETH
836 fecmxc_halt(dev);
837#else
0b23fb36 838 fec_halt(dev);
60752ca8 839#endif
0b23fb36 840 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
567173a6 841 &fec->eth->x_cntrl);
60752ca8
JT
842#ifdef CONFIG_DM_ETH
843 fecmxc_init(dev);
844#else
0b23fb36 845 fec_init(dev, fec->bd);
60752ca8 846#endif
0b23fb36
IY
847 }
848 }
849
850 /*
5c1ad3e6
EN
851 * Read the buffer status. Before the status can be read, the data cache
852 * must be invalidated, because the data in RAM might have been changed
853 * by DMA. The descriptors are properly aligned to cachelines so there's
854 * no need to worry they'd overlap.
855 *
856 * WARNING: By invalidating the descriptor here, we also invalidate
857 * the descriptors surrounding this one. Therefore we can NOT change the
858 * contents of this descriptor nor the surrounding ones. The problem is
859 * that in order to mark the descriptor as processed, we need to change
860 * the descriptor. The solution is to mark the whole cache line when all
861 * descriptors in the cache line are processed.
0b23fb36 862 */
f24e482a 863 addr = (ulong)rbd;
5c1ad3e6
EN
864 addr &= ~(ARCH_DMA_MINALIGN - 1);
865 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
866 invalidate_dcache_range(addr, addr + size);
867
0b23fb36
IY
868 bd_status = readw(&rbd->status);
869 debug("fec_recv: status 0x%x\n", bd_status);
870
871 if (!(bd_status & FEC_RBD_EMPTY)) {
872 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
567173a6
JT
873 ((readw(&rbd->data_length) - 4) > 14)) {
874 /* Get buffer address and size */
b189584b 875 addr = readl(&rbd->data_pointer);
0b23fb36 876 frame_length = readw(&rbd->data_length) - 4;
567173a6 877 /* Invalidate data cache over the buffer */
efe24d2e
MV
878 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
879 addr &= ~(ARCH_DMA_MINALIGN - 1);
880 invalidate_dcache_range(addr, end);
5c1ad3e6 881
567173a6 882 /* Fill the buffer and pass it to upper layers */
5c1ad3e6 883#ifdef CONFIG_FEC_MXC_SWAP_PACKET
b189584b 884 swap_packet((uint32_t *)addr, frame_length);
be7e87e2 885#endif
b189584b 886 memcpy(buff, (char *)addr, frame_length);
1fd92db8 887 net_process_received_packet(buff, frame_length);
0b23fb36
IY
888 len = frame_length;
889 } else {
890 if (bd_status & FEC_RBD_ERR)
f24e482a
YL
891 debug("error frame: 0x%08lx 0x%08x\n",
892 addr, bd_status);
0b23fb36 893 }
5c1ad3e6 894
0b23fb36 895 /*
5c1ad3e6
EN
896 * Free the current buffer, restart the engine and move forward
897 * to the next buffer. Here we check if the whole cacheline of
898 * descriptors was already processed and if so, we mark it free
899 * as whole.
0b23fb36 900 */
5c1ad3e6
EN
901 size = RXDESC_PER_CACHELINE - 1;
902 if ((fec->rbd_index & size) == size) {
903 i = fec->rbd_index - size;
f24e482a 904 addr = (ulong)&fec->rbd_base[i];
5c1ad3e6
EN
905 for (; i <= fec->rbd_index ; i++) {
906 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
907 &fec->rbd_base[i]);
908 }
909 flush_dcache_range(addr,
567173a6 910 addr + ARCH_DMA_MINALIGN);
5c1ad3e6
EN
911 }
912
0b23fb36
IY
913 fec_rx_task_enable(fec);
914 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
915 }
916 debug("fec_recv: stop\n");
917
918 return len;
919}
920
ef8e3a3b
TK
921static void fec_set_dev_name(char *dest, int dev_id)
922{
923 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
924}
925
79e5f27b
MV
926static int fec_alloc_descs(struct fec_priv *fec)
927{
928 unsigned int size;
929 int i;
930 uint8_t *data;
f24e482a 931 ulong addr;
79e5f27b
MV
932
933 /* Allocate TX descriptors. */
934 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
935 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
936 if (!fec->tbd_base)
937 goto err_tx;
938
939 /* Allocate RX descriptors. */
940 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
941 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
942 if (!fec->rbd_base)
943 goto err_rx;
944
945 memset(fec->rbd_base, 0, size);
946
947 /* Allocate RX buffers. */
948
949 /* Maximum RX buffer size. */
db5b7f56 950 size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN);
79e5f27b 951 for (i = 0; i < FEC_RBD_NUM; i++) {
db5b7f56 952 data = memalign(FEC_DMA_RX_MINALIGN, size);
79e5f27b
MV
953 if (!data) {
954 printf("%s: error allocating rxbuf %d\n", __func__, i);
955 goto err_ring;
956 }
957
958 memset(data, 0, size);
959
f24e482a
YL
960 addr = (ulong)data;
961 fec->rbd_base[i].data_pointer = (uint32_t)addr;
79e5f27b
MV
962 fec->rbd_base[i].status = FEC_RBD_EMPTY;
963 fec->rbd_base[i].data_length = 0;
964 /* Flush the buffer to memory. */
f24e482a 965 flush_dcache_range(addr, addr + size);
79e5f27b
MV
966 }
967
968 /* Mark the last RBD to close the ring. */
969 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
970
971 fec->rbd_index = 0;
972 fec->tbd_index = 0;
973
974 return 0;
975
976err_ring:
f24e482a
YL
977 for (; i >= 0; i--) {
978 addr = fec->rbd_base[i].data_pointer;
979 free((void *)addr);
980 }
79e5f27b
MV
981 free(fec->rbd_base);
982err_rx:
983 free(fec->tbd_base);
984err_tx:
985 return -ENOMEM;
986}
987
988static void fec_free_descs(struct fec_priv *fec)
989{
990 int i;
f24e482a 991 ulong addr;
79e5f27b 992
f24e482a
YL
993 for (i = 0; i < FEC_RBD_NUM; i++) {
994 addr = fec->rbd_base[i].data_pointer;
995 free((void *)addr);
996 }
79e5f27b
MV
997 free(fec->rbd_base);
998 free(fec->tbd_base);
999}
1000
cb5761f7
LW
1001#ifdef CONFIG_DM_ETH
1002struct mii_dev *fec_get_miibus(struct udevice *dev, int dev_id)
1003#else
60752ca8 1004struct mii_dev *fec_get_miibus(uint32_t base_addr, int dev_id)
cb5761f7 1005#endif
60752ca8 1006{
cb5761f7
LW
1007#ifdef CONFIG_DM_ETH
1008 struct fec_priv *priv = dev_get_priv(dev);
1009 struct ethernet_regs *eth = priv->eth;
1010#else
f24e482a 1011 struct ethernet_regs *eth = (struct ethernet_regs *)(ulong)base_addr;
cb5761f7 1012#endif
60752ca8
JT
1013 struct mii_dev *bus;
1014 int ret;
1015
1016 bus = mdio_alloc();
1017 if (!bus) {
1018 printf("mdio_alloc failed\n");
1019 return NULL;
1020 }
1021 bus->read = fec_phy_read;
1022 bus->write = fec_phy_write;
1023 bus->priv = eth;
1024 fec_set_dev_name(bus->name, dev_id);
1025
1026 ret = mdio_register(bus);
1027 if (ret) {
1028 printf("mdio_register failed\n");
1029 free(bus);
1030 return NULL;
1031 }
1032 fec_mii_setspeed(eth);
1033 return bus;
1034}
1035
1036#ifndef CONFIG_DM_ETH
fe428b90
TK
1037#ifdef CONFIG_PHYLIB
1038int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1039 struct mii_dev *bus, struct phy_device *phydev)
1040#else
1041static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1042 struct mii_dev *bus, int phy_id)
1043#endif
0b23fb36 1044{
0b23fb36 1045 struct eth_device *edev;
9e27e9dc 1046 struct fec_priv *fec;
0b23fb36 1047 unsigned char ethaddr[6];
979a5893 1048 char mac[16];
e382fb48
MV
1049 uint32_t start;
1050 int ret = 0;
0b23fb36
IY
1051
1052 /* create and fill edev struct */
1053 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
1054 if (!edev) {
9e27e9dc 1055 puts("fec_mxc: not enough malloc memory for eth_device\n");
e382fb48
MV
1056 ret = -ENOMEM;
1057 goto err1;
9e27e9dc
MV
1058 }
1059
1060 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
1061 if (!fec) {
1062 puts("fec_mxc: not enough malloc memory for fec_priv\n");
e382fb48
MV
1063 ret = -ENOMEM;
1064 goto err2;
0b23fb36 1065 }
9e27e9dc 1066
de0b9576 1067 memset(edev, 0, sizeof(*edev));
9e27e9dc
MV
1068 memset(fec, 0, sizeof(*fec));
1069
79e5f27b
MV
1070 ret = fec_alloc_descs(fec);
1071 if (ret)
1072 goto err3;
1073
0b23fb36
IY
1074 edev->priv = fec;
1075 edev->init = fec_init;
1076 edev->send = fec_send;
1077 edev->recv = fec_recv;
1078 edev->halt = fec_halt;
fb57ec97 1079 edev->write_hwaddr = fec_set_hwaddr;
0b23fb36 1080
f24e482a 1081 fec->eth = (struct ethernet_regs *)(ulong)base_addr;
0b23fb36
IY
1082 fec->bd = bd;
1083
392b8502 1084 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
0b23fb36
IY
1085
1086 /* Reset chip. */
cb17b92d 1087 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
e382fb48
MV
1088 start = get_timer(0);
1089 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
1090 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
3450a859 1091 printf("FEC MXC: Timeout resetting chip\n");
79e5f27b 1092 goto err4;
e382fb48 1093 }
0b23fb36 1094 udelay(10);
e382fb48 1095 }
0b23fb36 1096
a5990b26 1097 fec_reg_setup(fec);
ef8e3a3b
TK
1098 fec_set_dev_name(edev->name, dev_id);
1099 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
fe428b90
TK
1100 fec->bus = bus;
1101 fec_mii_setspeed(bus->priv);
1102#ifdef CONFIG_PHYLIB
1103 fec->phydev = phydev;
1104 phy_connect_dev(phydev, edev);
1105 /* Configure phy */
1106 phy_config(phydev);
1107#else
9e27e9dc 1108 fec->phy_id = phy_id;
fe428b90
TK
1109#endif
1110 eth_register(edev);
979a5893
AD
1111 /* only support one eth device, the index number pointed by dev_id */
1112 edev->index = fec->dev_id;
fe428b90 1113
f01e4e1e
AD
1114 if (fec_get_hwaddr(fec->dev_id, ethaddr) == 0) {
1115 debug("got MAC%d address from fuse: %pM\n", fec->dev_id, ethaddr);
fe428b90 1116 memcpy(edev->enetaddr, ethaddr, 6);
979a5893
AD
1117 if (fec->dev_id)
1118 sprintf(mac, "eth%daddr", fec->dev_id);
1119 else
1120 strcpy(mac, "ethaddr");
00caae6d 1121 if (!env_get(mac))
fd1e959e 1122 eth_env_set_enetaddr(mac, ethaddr);
fe428b90
TK
1123 }
1124 return ret;
79e5f27b
MV
1125err4:
1126 fec_free_descs(fec);
fe428b90
TK
1127err3:
1128 free(fec);
1129err2:
1130 free(edev);
1131err1:
1132 return ret;
1133}
1134
fe428b90
TK
1135int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1136{
1137 uint32_t base_mii;
1138 struct mii_dev *bus = NULL;
1139#ifdef CONFIG_PHYLIB
1140 struct phy_device *phydev = NULL;
1141#endif
1142 int ret;
1143
5c1ad3e6 1144#ifdef CONFIG_MX28
13947f43
TK
1145 /*
1146 * The i.MX28 has two ethernet interfaces, but they are not equal.
1147 * Only the first one can access the MDIO bus.
1148 */
fe428b90 1149 base_mii = MXS_ENET0_BASE;
13947f43 1150#else
fe428b90 1151 base_mii = addr;
13947f43 1152#endif
fe428b90
TK
1153 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1154 bus = fec_get_miibus(base_mii, dev_id);
1155 if (!bus)
1156 return -ENOMEM;
4dc27eed 1157#ifdef CONFIG_PHYLIB
fe428b90 1158 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
4dc27eed 1159 if (!phydev) {
845a57b4 1160 mdio_unregister(bus);
4dc27eed 1161 free(bus);
fe428b90 1162 return -ENOMEM;
4dc27eed 1163 }
fe428b90
TK
1164 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1165#else
1166 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
4dc27eed 1167#endif
fe428b90
TK
1168 if (ret) {
1169#ifdef CONFIG_PHYLIB
1170 free(phydev);
1171#endif
845a57b4 1172 mdio_unregister(bus);
fe428b90
TK
1173 free(bus);
1174 }
e382fb48 1175 return ret;
eef24480 1176}
0b23fb36 1177
eef24480
TK
1178#ifdef CONFIG_FEC_MXC_PHYADDR
1179int fecmxc_initialize(bd_t *bd)
1180{
1181 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1182 IMX_FEC_BASE);
0b23fb36 1183}
eef24480 1184#endif
2e5f4421 1185
13947f43 1186#ifndef CONFIG_PHYLIB
2e5f4421
MV
1187int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1188{
1189 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1190 fec->mii_postcall = cb;
1191 return 0;
1192}
13947f43 1193#endif
60752ca8
JT
1194
1195#else
1196
1ed2570f
JT
1197static int fecmxc_read_rom_hwaddr(struct udevice *dev)
1198{
1199 struct fec_priv *priv = dev_get_priv(dev);
1200 struct eth_pdata *pdata = dev_get_platdata(dev);
1201
1202 return fec_get_hwaddr(priv->dev_id, pdata->enetaddr);
1203}
1204
60752ca8
JT
1205static const struct eth_ops fecmxc_ops = {
1206 .start = fecmxc_init,
1207 .send = fecmxc_send,
1208 .recv = fecmxc_recv,
1209 .stop = fecmxc_halt,
1210 .write_hwaddr = fecmxc_set_hwaddr,
1ed2570f 1211 .read_rom_hwaddr = fecmxc_read_rom_hwaddr,
60752ca8
JT
1212};
1213
1214static int fec_phy_init(struct fec_priv *priv, struct udevice *dev)
1215{
1216 struct phy_device *phydev;
1217 int mask = 0xffffffff;
1218
1219#ifdef CONFIG_PHYLIB
1220 mask = 1 << CONFIG_FEC_MXC_PHYADDR;
1221#endif
1222
1223 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
1224 if (!phydev)
1225 return -ENODEV;
1226
1227 phy_connect_dev(phydev, dev);
1228
1229 priv->phydev = phydev;
1230 phy_config(phydev);
1231
1232 return 0;
1233}
1234
1235static int fecmxc_probe(struct udevice *dev)
1236{
1237 struct eth_pdata *pdata = dev_get_platdata(dev);
1238 struct fec_priv *priv = dev_get_priv(dev);
1239 struct mii_dev *bus = NULL;
1240 int dev_id = -1;
60752ca8
JT
1241 uint32_t start;
1242 int ret;
1243
1244 ret = fec_alloc_descs(priv);
1245 if (ret)
1246 return ret;
1247
60752ca8 1248 /* Reset chip. */
567173a6
JT
1249 writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET,
1250 &priv->eth->ecntrl);
60752ca8
JT
1251 start = get_timer(0);
1252 while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) {
1253 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1254 printf("FEC MXC: Timeout reseting chip\n");
1255 goto err_timeout;
1256 }
1257 udelay(10);
1258 }
1259
1260 fec_reg_setup(priv);
60752ca8
JT
1261 priv->dev_id = (dev_id == -1) ? 0 : dev_id;
1262
306dd7da
LW
1263 bus = fec_get_miibus(dev, dev_id);
1264 if (!bus) {
1265 ret = -ENOMEM;
1266 goto err_mii;
1267 }
1268
1269 priv->bus = bus;
1270 priv->xcv_type = CONFIG_FEC_XCV_TYPE;
1271 priv->interface = pdata->phy_interface;
1272 ret = fec_phy_init(priv, dev);
1273 if (ret)
1274 goto err_phy;
1275
60752ca8
JT
1276 return 0;
1277
1278err_timeout:
1279 free(priv->phydev);
1280err_phy:
1281 mdio_unregister(bus);
1282 free(bus);
1283err_mii:
1284 fec_free_descs(priv);
1285 return ret;
1286}
1287
1288static int fecmxc_remove(struct udevice *dev)
1289{
1290 struct fec_priv *priv = dev_get_priv(dev);
1291
1292 free(priv->phydev);
1293 fec_free_descs(priv);
1294 mdio_unregister(priv->bus);
1295 mdio_free(priv->bus);
1296
1297 return 0;
1298}
1299
1300static int fecmxc_ofdata_to_platdata(struct udevice *dev)
1301{
1302 struct eth_pdata *pdata = dev_get_platdata(dev);
1303 struct fec_priv *priv = dev_get_priv(dev);
1304 const char *phy_mode;
1305
a821c4af 1306 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
60752ca8
JT
1307 priv->eth = (struct ethernet_regs *)pdata->iobase;
1308
1309 pdata->phy_interface = -1;
e160f7d4
SG
1310 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1311 NULL);
60752ca8
JT
1312 if (phy_mode)
1313 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1314 if (pdata->phy_interface == -1) {
1315 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1316 return -EINVAL;
1317 }
1318
1319 /* TODO
1320 * Need to get the reset-gpio and related properties from DT
1321 * and implemet the enet reset code on .probe call
1322 */
1323
1324 return 0;
1325}
1326
1327static const struct udevice_id fecmxc_ids[] = {
1328 { .compatible = "fsl,imx6q-fec" },
1329 { }
1330};
1331
1332U_BOOT_DRIVER(fecmxc_gem) = {
1333 .name = "fecmxc",
1334 .id = UCLASS_ETH,
1335 .of_match = fecmxc_ids,
1336 .ofdata_to_platdata = fecmxc_ofdata_to_platdata,
1337 .probe = fecmxc_probe,
1338 .remove = fecmxc_remove,
1339 .ops = &fecmxc_ops,
1340 .priv_auto_alloc_size = sizeof(struct fec_priv),
1341 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1342};
1343#endif
This page took 0.626065 seconds and 4 git commands to generate.