]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/net/fec_mxc.c
Define UART4 and UART5 base addresses
[people/ms/u-boot.git] / drivers / net / fec_mxc.c
CommitLineData
0b23fb36
IY
1/*
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 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <malloc.h>
26#include <net.h>
27#include <miiphy.h>
28#include "fec_mxc.h"
29
30#include <asm/arch/clock.h>
31#include <asm/arch/imx-regs.h>
32#include <asm/io.h>
33#include <asm/errno.h>
34
35DECLARE_GLOBAL_DATA_PTR;
36
37#ifndef CONFIG_MII
38#error "CONFIG_MII has to be defined!"
39#endif
40
5c1ad3e6
EN
41#ifndef CONFIG_FEC_XCV_TYPE
42#define CONFIG_FEC_XCV_TYPE MII100
392b8502
MV
43#endif
44
be7e87e2
MV
45/*
46 * The i.MX28 operates with packets in big endian. We need to swap them before
47 * sending and after receiving.
48 */
5c1ad3e6
EN
49#ifdef CONFIG_MX28
50#define CONFIG_FEC_MXC_SWAP_PACKET
51#endif
52
53#define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
54
55/* Check various alignment issues at compile time */
56#if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
57#error "ARCH_DMA_MINALIGN must be multiple of 16!"
58#endif
59
60#if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
61 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
62#error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
be7e87e2
MV
63#endif
64
0b23fb36
IY
65#undef DEBUG
66
67struct nbuf {
68 uint8_t data[1500]; /**< actual data */
69 int length; /**< actual length */
70 int used; /**< buffer in use or not */
71 uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
72};
73
5c1ad3e6 74#ifdef CONFIG_FEC_MXC_SWAP_PACKET
be7e87e2
MV
75static void swap_packet(uint32_t *packet, int length)
76{
77 int i;
78
79 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
80 packet[i] = __swab32(packet[i]);
81}
82#endif
83
0b23fb36
IY
84/*
85 * MII-interface related functions
86 */
13947f43
TK
87static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
88 uint8_t regAddr)
0b23fb36 89{
0b23fb36
IY
90 uint32_t reg; /* convenient holder for the PHY register */
91 uint32_t phy; /* convenient holder for the PHY */
92 uint32_t start;
13947f43 93 int val;
0b23fb36
IY
94
95 /*
96 * reading from any PHY's register is done by properly
97 * programming the FEC's MII data register.
98 */
d133b881 99 writel(FEC_IEVENT_MII, &eth->ievent);
0b23fb36
IY
100 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
101 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
102
103 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
d133b881 104 phy | reg, &eth->mii_data);
0b23fb36
IY
105
106 /*
107 * wait for the related interrupt
108 */
a60d1e5b 109 start = get_timer(0);
d133b881 110 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
0b23fb36
IY
111 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
112 printf("Read MDIO failed...\n");
113 return -1;
114 }
115 }
116
117 /*
118 * clear mii interrupt bit
119 */
d133b881 120 writel(FEC_IEVENT_MII, &eth->ievent);
0b23fb36
IY
121
122 /*
123 * it's now safe to read the PHY's register
124 */
13947f43
TK
125 val = (unsigned short)readl(&eth->mii_data);
126 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
127 regAddr, val);
128 return val;
0b23fb36
IY
129}
130
4294b248
SB
131static void fec_mii_setspeed(struct fec_priv *fec)
132{
133 /*
134 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
135 * and do not drop the Preamble.
136 */
137 writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
138 &fec->eth->mii_speed);
13947f43 139 debug("%s: mii_speed %08x\n", __func__, readl(&fec->eth->mii_speed));
4294b248 140}
0b23fb36 141
13947f43
TK
142static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
143 uint8_t regAddr, uint16_t data)
144{
0b23fb36
IY
145 uint32_t reg; /* convenient holder for the PHY register */
146 uint32_t phy; /* convenient holder for the PHY */
147 uint32_t start;
148
149 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
150 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
151
152 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
d133b881 153 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
0b23fb36
IY
154
155 /*
156 * wait for the MII interrupt
157 */
a60d1e5b 158 start = get_timer(0);
d133b881 159 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
0b23fb36
IY
160 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
161 printf("Write MDIO failed...\n");
162 return -1;
163 }
164 }
165
166 /*
167 * clear MII interrupt bit
168 */
d133b881 169 writel(FEC_IEVENT_MII, &eth->ievent);
13947f43 170 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
0b23fb36
IY
171 regAddr, data);
172
173 return 0;
174}
175
13947f43
TK
176int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
177{
178 return fec_mdio_read(bus->priv, phyAddr, regAddr);
179}
180
181int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
182 u16 data)
183{
184 return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
185}
186
187#ifndef CONFIG_PHYLIB
0b23fb36
IY
188static int miiphy_restart_aneg(struct eth_device *dev)
189{
9e27e9dc 190 struct fec_priv *fec = (struct fec_priv *)dev->priv;
13947f43 191 struct ethernet_regs *eth = fec->bus->priv;
2e5f4421 192 int ret = 0;
9e27e9dc 193
0b23fb36
IY
194 /*
195 * Wake up from sleep if necessary
196 * Reset PHY, then delay 300ns
197 */
cb17b92d 198#ifdef CONFIG_MX27
13947f43 199 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
cb17b92d 200#endif
13947f43 201 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
0b23fb36
IY
202 udelay(1000);
203
204 /*
205 * Set the auto-negotiation advertisement register bits
206 */
13947f43 207 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
8ef583a0
MF
208 LPA_100FULL | LPA_100HALF | LPA_10FULL |
209 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
13947f43 210 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
8ef583a0 211 BMCR_ANENABLE | BMCR_ANRESTART);
2e5f4421
MV
212
213 if (fec->mii_postcall)
214 ret = fec->mii_postcall(fec->phy_id);
215
216 return ret;
0b23fb36
IY
217}
218
219static int miiphy_wait_aneg(struct eth_device *dev)
220{
221 uint32_t start;
13947f43 222 int status;
9e27e9dc 223 struct fec_priv *fec = (struct fec_priv *)dev->priv;
13947f43 224 struct ethernet_regs *eth = fec->bus->priv;
0b23fb36
IY
225
226 /*
227 * Wait for AN completion
228 */
a60d1e5b 229 start = get_timer(0);
0b23fb36
IY
230 do {
231 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
232 printf("%s: Autonegotiation timeout\n", dev->name);
233 return -1;
234 }
235
13947f43
TK
236 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
237 if (status < 0) {
238 printf("%s: Autonegotiation failed. status: %d\n",
0b23fb36
IY
239 dev->name, status);
240 return -1;
241 }
8ef583a0 242 } while (!(status & BMSR_LSTATUS));
0b23fb36
IY
243
244 return 0;
245}
13947f43
TK
246#endif
247
0b23fb36
IY
248static int fec_rx_task_enable(struct fec_priv *fec)
249{
250 writel(1 << 24, &fec->eth->r_des_active);
251 return 0;
252}
253
254static int fec_rx_task_disable(struct fec_priv *fec)
255{
256 return 0;
257}
258
259static int fec_tx_task_enable(struct fec_priv *fec)
260{
261 writel(1 << 24, &fec->eth->x_des_active);
262 return 0;
263}
264
265static int fec_tx_task_disable(struct fec_priv *fec)
266{
267 return 0;
268}
269
270/**
271 * Initialize receive task's buffer descriptors
272 * @param[in] fec all we know about the device yet
273 * @param[in] count receive buffer count to be allocated
5c1ad3e6 274 * @param[in] dsize desired size of each receive buffer
0b23fb36
IY
275 * @return 0 on success
276 *
277 * For this task we need additional memory for the data buffers. And each
278 * data buffer requires some alignment. Thy must be aligned to a specific
5c1ad3e6 279 * boundary each.
0b23fb36 280 */
5c1ad3e6 281static int fec_rbd_init(struct fec_priv *fec, int count, int dsize)
0b23fb36 282{
5c1ad3e6
EN
283 uint32_t size;
284 int i;
285
0b23fb36 286 /*
5c1ad3e6
EN
287 * Allocate memory for the buffers. This allocation respects the
288 * alignment
0b23fb36 289 */
5c1ad3e6
EN
290 size = roundup(dsize, ARCH_DMA_MINALIGN);
291 for (i = 0; i < count; i++) {
292 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
293 if (data_ptr == 0) {
294 uint8_t *data = memalign(ARCH_DMA_MINALIGN,
295 size);
296 if (!data) {
297 printf("%s: error allocating rxbuf %d\n",
298 __func__, i);
299 goto err;
300 }
301 writel((uint32_t)data, &fec->rbd_base[i].data_pointer);
302 } /* needs allocation */
303 writew(FEC_RBD_EMPTY, &fec->rbd_base[i].status);
304 writew(0, &fec->rbd_base[i].data_length);
305 }
306
307 /* Mark the last RBD to close the ring. */
308 writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[i - 1].status);
0b23fb36
IY
309 fec->rbd_index = 0;
310
311 return 0;
5c1ad3e6
EN
312
313err:
314 for (; i >= 0; i--) {
315 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
316 free((void *)data_ptr);
317 }
318
319 return -ENOMEM;
0b23fb36
IY
320}
321
322/**
323 * Initialize transmit task's buffer descriptors
324 * @param[in] fec all we know about the device yet
325 *
326 * Transmit buffers are created externally. We only have to init the BDs here.\n
327 * Note: There is a race condition in the hardware. When only one BD is in
328 * use it must be marked with the WRAP bit to use it for every transmitt.
329 * This bit in combination with the READY bit results into double transmit
330 * of each data buffer. It seems the state machine checks READY earlier then
331 * resetting it after the first transfer.
332 * Using two BDs solves this issue.
333 */
334static void fec_tbd_init(struct fec_priv *fec)
335{
5c1ad3e6
EN
336 unsigned addr = (unsigned)fec->tbd_base;
337 unsigned size = roundup(2 * sizeof(struct fec_bd),
338 ARCH_DMA_MINALIGN);
0b23fb36
IY
339 writew(0x0000, &fec->tbd_base[0].status);
340 writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
341 fec->tbd_index = 0;
5c1ad3e6 342 flush_dcache_range(addr, addr+size);
0b23fb36
IY
343}
344
345/**
346 * Mark the given read buffer descriptor as free
347 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
348 * @param[in] pRbd buffer descriptor to mark free again
349 */
350static void fec_rbd_clean(int last, struct fec_bd *pRbd)
351{
5c1ad3e6 352 unsigned short flags = FEC_RBD_EMPTY;
0b23fb36 353 if (last)
5c1ad3e6
EN
354 flags |= FEC_RBD_WRAP;
355 writew(flags, &pRbd->status);
0b23fb36
IY
356 writew(0, &pRbd->data_length);
357}
358
be252b65
FE
359static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
360 unsigned char *mac)
0b23fb36 361{
be252b65 362 imx_get_mac_from_fuse(dev_id, mac);
2e236bf2 363 return !is_valid_ether_addr(mac);
0b23fb36
IY
364}
365
4294b248 366static int fec_set_hwaddr(struct eth_device *dev)
0b23fb36 367{
4294b248 368 uchar *mac = dev->enetaddr;
0b23fb36
IY
369 struct fec_priv *fec = (struct fec_priv *)dev->priv;
370
371 writel(0, &fec->eth->iaddr1);
372 writel(0, &fec->eth->iaddr2);
373 writel(0, &fec->eth->gaddr1);
374 writel(0, &fec->eth->gaddr2);
375
376 /*
377 * Set physical address
378 */
379 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
380 &fec->eth->paddr1);
381 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
382
383 return 0;
384}
385
13947f43
TK
386static void fec_eth_phy_config(struct eth_device *dev)
387{
388#ifdef CONFIG_PHYLIB
389 struct fec_priv *fec = (struct fec_priv *)dev->priv;
390 struct phy_device *phydev;
391
392 phydev = phy_connect(fec->bus, fec->phy_id, dev,
393 PHY_INTERFACE_MODE_RGMII);
394 if (phydev) {
395 fec->phydev = phydev;
396 phy_config(phydev);
397 }
398#endif
399}
400
0b23fb36
IY
401/**
402 * Start the FEC engine
403 * @param[in] dev Our device to handle
404 */
405static int fec_open(struct eth_device *edev)
406{
407 struct fec_priv *fec = (struct fec_priv *)edev->priv;
28774cba 408 int speed;
5c1ad3e6
EN
409 uint32_t addr, size;
410 int i;
0b23fb36
IY
411
412 debug("fec_open: fec_open(dev)\n");
413 /* full-duplex, heartbeat disabled */
414 writel(1 << 2, &fec->eth->x_cntrl);
415 fec->rbd_index = 0;
416
5c1ad3e6
EN
417 /* Invalidate all descriptors */
418 for (i = 0; i < FEC_RBD_NUM - 1; i++)
419 fec_rbd_clean(0, &fec->rbd_base[i]);
420 fec_rbd_clean(1, &fec->rbd_base[i]);
421
422 /* Flush the descriptors into RAM */
423 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
424 ARCH_DMA_MINALIGN);
425 addr = (uint32_t)fec->rbd_base;
426 flush_dcache_range(addr, addr + size);
427
28774cba 428#ifdef FEC_QUIRK_ENET_MAC
2ef2b950
JL
429 /* Enable ENET HW endian SWAP */
430 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
431 &fec->eth->ecntrl);
432 /* Enable ENET store and forward mode */
433 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
434 &fec->eth->x_wmrk);
435#endif
0b23fb36
IY
436 /*
437 * Enable FEC-Lite controller
438 */
cb17b92d
JR
439 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
440 &fec->eth->ecntrl);
96912453 441#if defined(CONFIG_MX25) || defined(CONFIG_MX53)
740d6ae5
JR
442 udelay(100);
443 /*
444 * setup the MII gasket for RMII mode
445 */
446
447 /* disable the gasket */
448 writew(0, &fec->eth->miigsk_enr);
449
450 /* wait for the gasket to be disabled */
451 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
452 udelay(2);
453
454 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
455 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
456
457 /* re-enable the gasket */
458 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
459
460 /* wait until MII gasket is ready */
461 int max_loops = 10;
462 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
463 if (--max_loops <= 0) {
464 printf("WAIT for MII Gasket ready timed out\n");
465 break;
466 }
467 }
468#endif
0b23fb36 469
13947f43
TK
470#ifdef CONFIG_PHYLIB
471 if (!fec->phydev)
472 fec_eth_phy_config(edev);
473 if (fec->phydev) {
474 /* Start up the PHY */
475 phy_startup(fec->phydev);
476 speed = fec->phydev->speed;
477 } else {
478 speed = _100BASET;
479 }
480#else
0b23fb36 481 miiphy_wait_aneg(edev);
28774cba 482 speed = miiphy_speed(edev->name, fec->phy_id);
9e27e9dc 483 miiphy_duplex(edev->name, fec->phy_id);
13947f43 484#endif
0b23fb36 485
28774cba
TK
486#ifdef FEC_QUIRK_ENET_MAC
487 {
488 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
489 u32 rcr = (readl(&fec->eth->r_cntrl) &
490 ~(FEC_RCNTRL_RMII | FEC_RCNTRL_RMII_10T)) |
491 FEC_RCNTRL_RGMII | FEC_RCNTRL_MII_MODE;
492 if (speed == _1000BASET)
493 ecr |= FEC_ECNTRL_SPEED;
494 else if (speed != _100BASET)
495 rcr |= FEC_RCNTRL_RMII_10T;
496 writel(ecr, &fec->eth->ecntrl);
497 writel(rcr, &fec->eth->r_cntrl);
498 }
499#endif
500 debug("%s:Speed=%i\n", __func__, speed);
501
0b23fb36
IY
502 /*
503 * Enable SmartDMA receive task
504 */
505 fec_rx_task_enable(fec);
506
507 udelay(100000);
508 return 0;
509}
510
511static int fec_init(struct eth_device *dev, bd_t* bd)
512{
0b23fb36 513 struct fec_priv *fec = (struct fec_priv *)dev->priv;
9e27e9dc 514 uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
9eb3770b 515 uint32_t rcntrl;
5c1ad3e6
EN
516 uint32_t size;
517 int i, ret;
0b23fb36 518
e9319f11
JR
519 /* Initialize MAC address */
520 fec_set_hwaddr(dev);
521
0b23fb36 522 /*
5c1ad3e6
EN
523 * Allocate transmit descriptors, there are two in total. This
524 * allocation respects cache alignment.
0b23fb36 525 */
5c1ad3e6
EN
526 if (!fec->tbd_base) {
527 size = roundup(2 * sizeof(struct fec_bd),
528 ARCH_DMA_MINALIGN);
529 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
530 if (!fec->tbd_base) {
531 ret = -ENOMEM;
532 goto err1;
533 }
534 memset(fec->tbd_base, 0, size);
535 fec_tbd_init(fec);
536 flush_dcache_range((unsigned)fec->tbd_base, size);
0b23fb36 537 }
0b23fb36 538
5c1ad3e6
EN
539 /*
540 * Allocate receive descriptors. This allocation respects cache
541 * alignment.
542 */
543 if (!fec->rbd_base) {
544 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
545 ARCH_DMA_MINALIGN);
546 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
547 if (!fec->rbd_base) {
548 ret = -ENOMEM;
549 goto err2;
550 }
551 memset(fec->rbd_base, 0, size);
552 /*
553 * Initialize RxBD ring
554 */
555 if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
556 ret = -ENOMEM;
557 goto err3;
558 }
559 flush_dcache_range((unsigned)fec->rbd_base,
560 (unsigned)fec->rbd_base + size);
561 }
0b23fb36
IY
562
563 /*
564 * Set interrupt mask register
565 */
566 writel(0x00000000, &fec->eth->imask);
567
568 /*
569 * Clear FEC-Lite interrupt event register(IEVENT)
570 */
571 writel(0xffffffff, &fec->eth->ievent);
572
573
574 /*
575 * Set FEC-Lite receive control register(R_CNTRL):
576 */
4294b248 577
9eb3770b
MV
578 /* Start with frame length = 1518, common for all modes. */
579 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
580 if (fec->xcv_type == SEVENWIRE)
581 rcntrl |= FEC_RCNTRL_FCE;
2ef2b950
JL
582 else if (fec->xcv_type == RGMII)
583 rcntrl |= FEC_RCNTRL_RGMII;
a50a90c9
MV
584 else if (fec->xcv_type == RMII)
585 rcntrl |= FEC_RCNTRL_RMII;
9eb3770b
MV
586 else /* MII mode */
587 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
588
589 writel(rcntrl, &fec->eth->r_cntrl);
590
591 if (fec->xcv_type == MII10 || fec->xcv_type == MII100)
4294b248 592 fec_mii_setspeed(fec);
9eb3770b 593
0b23fb36
IY
594 /*
595 * Set Opcode/Pause Duration Register
596 */
597 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
598 writel(0x2, &fec->eth->x_wmrk);
599 /*
600 * Set multicast address filter
601 */
602 writel(0x00000000, &fec->eth->gaddr1);
603 writel(0x00000000, &fec->eth->gaddr2);
604
605
606 /* clear MIB RAM */
9e27e9dc
MV
607 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
608 writel(0, i);
0b23fb36
IY
609
610 /* FIFO receive start register */
611 writel(0x520, &fec->eth->r_fstart);
612
613 /* size and address of each buffer */
614 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
615 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
616 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
617
13947f43 618#ifndef CONFIG_PHYLIB
0b23fb36
IY
619 if (fec->xcv_type != SEVENWIRE)
620 miiphy_restart_aneg(dev);
13947f43 621#endif
0b23fb36
IY
622 fec_open(dev);
623 return 0;
5c1ad3e6
EN
624
625err3:
626 free(fec->rbd_base);
627err2:
628 free(fec->tbd_base);
629err1:
630 return ret;
0b23fb36
IY
631}
632
633/**
634 * Halt the FEC engine
635 * @param[in] dev Our device to handle
636 */
637static void fec_halt(struct eth_device *dev)
638{
9e27e9dc 639 struct fec_priv *fec = (struct fec_priv *)dev->priv;
0b23fb36
IY
640 int counter = 0xffff;
641
642 /*
643 * issue graceful stop command to the FEC transmitter if necessary
644 */
cb17b92d 645 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
0b23fb36
IY
646 &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)))
cb17b92d 653 udelay(1);
0b23fb36
IY
654
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 */
740d6ae5
JR
665 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
666 &fec->eth->ecntrl);
0b23fb36
IY
667 fec->rbd_index = 0;
668 fec->tbd_index = 0;
0b23fb36
IY
669 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 */
5c1ad3e6 679static int fec_send(struct eth_device *dev, volatile void *packet, int length)
0b23fb36
IY
680{
681 unsigned int status;
5c1ad3e6
EN
682 uint32_t size;
683 uint32_t addr;
0b23fb36
IY
684
685 /*
686 * This routine transmits one frame. This routine only accepts
687 * 6-byte Ethernet addresses.
688 */
689 struct fec_priv *fec = (struct fec_priv *)dev->priv;
690
691 /*
692 * Check for valid length of data.
693 */
694 if ((length > 1500) || (length <= 0)) {
4294b248 695 printf("Payload (%d) too large\n", length);
0b23fb36
IY
696 return -1;
697 }
698
699 /*
5c1ad3e6
EN
700 * Setup the transmit buffer. We are always using the first buffer for
701 * transmission, the second will be empty and only used to stop the DMA
702 * engine. We also flush the packet to RAM here to avoid cache trouble.
0b23fb36 703 */
5c1ad3e6 704#ifdef CONFIG_FEC_MXC_SWAP_PACKET
be7e87e2
MV
705 swap_packet((uint32_t *)packet, length);
706#endif
5c1ad3e6
EN
707
708 addr = (uint32_t)packet;
709 size = roundup(length, ARCH_DMA_MINALIGN);
710 flush_dcache_range(addr, addr + size);
711
0b23fb36 712 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
5c1ad3e6
EN
713 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
714
0b23fb36
IY
715 /*
716 * update BD's status now
717 * This block:
718 * - is always the last in a chain (means no chain)
719 * - should transmitt the CRC
720 * - might be the last BD in the list, so the address counter should
721 * wrap (-> keep the WRAP flag)
722 */
723 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
724 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
725 writew(status, &fec->tbd_base[fec->tbd_index].status);
726
5c1ad3e6
EN
727 /*
728 * Flush data cache. This code flushes both TX descriptors to RAM.
729 * After this code, the descriptors will be safely in RAM and we
730 * can start DMA.
731 */
732 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
733 addr = (uint32_t)fec->tbd_base;
734 flush_dcache_range(addr, addr + size);
735
0b23fb36
IY
736 /*
737 * Enable SmartDMA transmit task
738 */
739 fec_tx_task_enable(fec);
740
741 /*
5c1ad3e6
EN
742 * Wait until frame is sent. On each turn of the wait cycle, we must
743 * invalidate data cache to see what's really in RAM. Also, we need
744 * barrier here.
0b23fb36 745 */
5c1ad3e6 746 invalidate_dcache_range(addr, addr + size);
0b23fb36 747 while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
cb17b92d 748 udelay(1);
5c1ad3e6 749 invalidate_dcache_range(addr, addr + size);
0b23fb36 750 }
5c1ad3e6 751
0b23fb36
IY
752 debug("fec_send: status 0x%x index %d\n",
753 readw(&fec->tbd_base[fec->tbd_index].status),
754 fec->tbd_index);
755 /* for next transmission use the other buffer */
756 if (fec->tbd_index)
757 fec->tbd_index = 0;
758 else
759 fec->tbd_index = 1;
760
761 return 0;
762}
763
764/**
765 * Pull one frame from the card
766 * @param[in] dev Our ethernet device to handle
767 * @return Length of packet read
768 */
769static int fec_recv(struct eth_device *dev)
770{
771 struct fec_priv *fec = (struct fec_priv *)dev->priv;
772 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
773 unsigned long ievent;
774 int frame_length, len = 0;
775 struct nbuf *frame;
776 uint16_t bd_status;
5c1ad3e6
EN
777 uint32_t addr, size;
778 int i;
0b23fb36
IY
779 uchar buff[FEC_MAX_PKT_SIZE];
780
781 /*
782 * Check if any critical events have happened
783 */
784 ievent = readl(&fec->eth->ievent);
785 writel(ievent, &fec->eth->ievent);
eda959f3 786 debug("fec_recv: ievent 0x%lx\n", ievent);
0b23fb36
IY
787 if (ievent & FEC_IEVENT_BABR) {
788 fec_halt(dev);
789 fec_init(dev, fec->bd);
790 printf("some error: 0x%08lx\n", ievent);
791 return 0;
792 }
793 if (ievent & FEC_IEVENT_HBERR) {
794 /* Heartbeat error */
795 writel(0x00000001 | readl(&fec->eth->x_cntrl),
796 &fec->eth->x_cntrl);
797 }
798 if (ievent & FEC_IEVENT_GRA) {
799 /* Graceful stop complete */
800 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
801 fec_halt(dev);
802 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
803 &fec->eth->x_cntrl);
804 fec_init(dev, fec->bd);
805 }
806 }
807
808 /*
5c1ad3e6
EN
809 * Read the buffer status. Before the status can be read, the data cache
810 * must be invalidated, because the data in RAM might have been changed
811 * by DMA. The descriptors are properly aligned to cachelines so there's
812 * no need to worry they'd overlap.
813 *
814 * WARNING: By invalidating the descriptor here, we also invalidate
815 * the descriptors surrounding this one. Therefore we can NOT change the
816 * contents of this descriptor nor the surrounding ones. The problem is
817 * that in order to mark the descriptor as processed, we need to change
818 * the descriptor. The solution is to mark the whole cache line when all
819 * descriptors in the cache line are processed.
0b23fb36 820 */
5c1ad3e6
EN
821 addr = (uint32_t)rbd;
822 addr &= ~(ARCH_DMA_MINALIGN - 1);
823 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
824 invalidate_dcache_range(addr, addr + size);
825
0b23fb36
IY
826 bd_status = readw(&rbd->status);
827 debug("fec_recv: status 0x%x\n", bd_status);
828
829 if (!(bd_status & FEC_RBD_EMPTY)) {
830 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
831 ((readw(&rbd->data_length) - 4) > 14)) {
832 /*
833 * Get buffer address and size
834 */
835 frame = (struct nbuf *)readl(&rbd->data_pointer);
836 frame_length = readw(&rbd->data_length) - 4;
5c1ad3e6
EN
837 /*
838 * Invalidate data cache over the buffer
839 */
840 addr = (uint32_t)frame;
841 size = roundup(frame_length, ARCH_DMA_MINALIGN);
842 invalidate_dcache_range(addr, addr + size);
843
0b23fb36
IY
844 /*
845 * Fill the buffer and pass it to upper layers
846 */
5c1ad3e6 847#ifdef CONFIG_FEC_MXC_SWAP_PACKET
be7e87e2
MV
848 swap_packet((uint32_t *)frame->data, frame_length);
849#endif
0b23fb36
IY
850 memcpy(buff, frame->data, frame_length);
851 NetReceive(buff, frame_length);
852 len = frame_length;
853 } else {
854 if (bd_status & FEC_RBD_ERR)
855 printf("error frame: 0x%08lx 0x%08x\n",
856 (ulong)rbd->data_pointer,
857 bd_status);
858 }
5c1ad3e6 859
0b23fb36 860 /*
5c1ad3e6
EN
861 * Free the current buffer, restart the engine and move forward
862 * to the next buffer. Here we check if the whole cacheline of
863 * descriptors was already processed and if so, we mark it free
864 * as whole.
0b23fb36 865 */
5c1ad3e6
EN
866 size = RXDESC_PER_CACHELINE - 1;
867 if ((fec->rbd_index & size) == size) {
868 i = fec->rbd_index - size;
869 addr = (uint32_t)&fec->rbd_base[i];
870 for (; i <= fec->rbd_index ; i++) {
871 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
872 &fec->rbd_base[i]);
873 }
874 flush_dcache_range(addr,
875 addr + ARCH_DMA_MINALIGN);
876 }
877
0b23fb36
IY
878 fec_rx_task_enable(fec);
879 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
880 }
881 debug("fec_recv: stop\n");
882
883 return len;
884}
885
9e27e9dc 886static int fec_probe(bd_t *bd, int dev_id, int phy_id, uint32_t base_addr)
0b23fb36 887{
0b23fb36 888 struct eth_device *edev;
9e27e9dc 889 struct fec_priv *fec;
13947f43 890 struct mii_dev *bus;
0b23fb36 891 unsigned char ethaddr[6];
e382fb48
MV
892 uint32_t start;
893 int ret = 0;
0b23fb36
IY
894
895 /* create and fill edev struct */
896 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
897 if (!edev) {
9e27e9dc 898 puts("fec_mxc: not enough malloc memory for eth_device\n");
e382fb48
MV
899 ret = -ENOMEM;
900 goto err1;
9e27e9dc
MV
901 }
902
903 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
904 if (!fec) {
905 puts("fec_mxc: not enough malloc memory for fec_priv\n");
e382fb48
MV
906 ret = -ENOMEM;
907 goto err2;
0b23fb36 908 }
9e27e9dc 909
de0b9576 910 memset(edev, 0, sizeof(*edev));
9e27e9dc
MV
911 memset(fec, 0, sizeof(*fec));
912
0b23fb36
IY
913 edev->priv = fec;
914 edev->init = fec_init;
915 edev->send = fec_send;
916 edev->recv = fec_recv;
917 edev->halt = fec_halt;
fb57ec97 918 edev->write_hwaddr = fec_set_hwaddr;
0b23fb36 919
9e27e9dc 920 fec->eth = (struct ethernet_regs *)base_addr;
0b23fb36
IY
921 fec->bd = bd;
922
392b8502 923 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
0b23fb36
IY
924
925 /* Reset chip. */
cb17b92d 926 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
e382fb48
MV
927 start = get_timer(0);
928 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
929 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
930 printf("FEC MXC: Timeout reseting chip\n");
931 goto err3;
932 }
0b23fb36 933 udelay(10);
e382fb48 934 }
0b23fb36
IY
935
936 /*
937 * Set interrupt mask register
938 */
939 writel(0x00000000, &fec->eth->imask);
940
941 /*
942 * Clear FEC-Lite interrupt event register(IEVENT)
943 */
944 writel(0xffffffff, &fec->eth->ievent);
945
946 /*
947 * Set FEC-Lite receive control register(R_CNTRL):
948 */
949 /*
950 * Frame length=1518; MII mode;
951 */
9eb3770b
MV
952 writel((PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT) | FEC_RCNTRL_FCE |
953 FEC_RCNTRL_MII_MODE, &fec->eth->r_cntrl);
4294b248 954 fec_mii_setspeed(fec);
0b23fb36 955
9e27e9dc
MV
956 if (dev_id == -1) {
957 sprintf(edev->name, "FEC");
958 fec->dev_id = 0;
959 } else {
960 sprintf(edev->name, "FEC%i", dev_id);
961 fec->dev_id = dev_id;
962 }
963 fec->phy_id = phy_id;
0b23fb36 964
13947f43
TK
965 bus = mdio_alloc();
966 if (!bus) {
967 printf("mdio_alloc failed\n");
968 ret = -ENOMEM;
969 goto err3;
970 }
971 bus->read = fec_phy_read;
972 bus->write = fec_phy_write;
973 sprintf(bus->name, edev->name);
5c1ad3e6 974#ifdef CONFIG_MX28
13947f43
TK
975 /*
976 * The i.MX28 has two ethernet interfaces, but they are not equal.
977 * Only the first one can access the MDIO bus.
978 */
979 bus->priv = (struct ethernet_regs *)MXS_ENET0_BASE;
980#else
981 bus->priv = fec->eth;
982#endif
983 ret = mdio_register(bus);
984 if (ret) {
985 printf("mdio_register failed\n");
986 free(bus);
987 ret = -ENOMEM;
988 goto err3;
989 }
990 fec->bus = bus;
0b23fb36
IY
991 eth_register(edev);
992
be252b65
FE
993 if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
994 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
4294b248 995 memcpy(edev->enetaddr, ethaddr, 6);
0b23fb36 996 }
13947f43
TK
997 /* Configure phy */
998 fec_eth_phy_config(edev);
e382fb48
MV
999 return ret;
1000
1001err3:
1002 free(fec);
1003err2:
1004 free(edev);
1005err1:
1006 return ret;
0b23fb36
IY
1007}
1008
5c1ad3e6 1009#ifndef CONFIG_FEC_MXC_MULTI
0b23fb36
IY
1010int fecmxc_initialize(bd_t *bd)
1011{
1012 int lout = 1;
1013
1014 debug("eth_init: fec_probe(bd)\n");
9e27e9dc
MV
1015 lout = fec_probe(bd, -1, CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
1016
1017 return lout;
1018}
1019#endif
1020
1021int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1022{
1023 int lout = 1;
1024
1025 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1026 lout = fec_probe(bd, dev_id, phy_id, addr);
0b23fb36
IY
1027
1028 return lout;
1029}
2e5f4421 1030
13947f43 1031#ifndef CONFIG_PHYLIB
2e5f4421
MV
1032int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1033{
1034 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1035 fec->mii_postcall = cb;
1036 return 0;
1037}
13947f43 1038#endif