blob: c17f9379bf93a3ff663395f0f3b56c8aa5fa60c9 [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 *
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
41#undef DEBUG
42
43struct nbuf {
44 uint8_t data[1500]; /**< actual data */
45 int length; /**< actual length */
46 int used; /**< buffer in use or not */
47 uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
48};
49
50struct fec_priv gfec = {
51 .eth = (struct ethernet_regs *)IMX_FEC_BASE,
52 .xcv_type = MII100,
53 .rbd_base = NULL,
54 .rbd_index = 0,
55 .tbd_base = NULL,
56 .tbd_index = 0,
57 .bd = NULL,
javier Martine87eb992009-10-29 08:22:43 +010058 .rdb_ptr = NULL,
59 .base_ptr = NULL,
Ilya Yanoke93a4a52009-07-21 19:32:21 +040060};
61
62/*
63 * MII-interface related functions
64 */
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -040065static int fec_miiphy_read(const char *dev, uint8_t phyAddr, uint8_t regAddr,
Ilya Yanoke93a4a52009-07-21 19:32:21 +040066 uint16_t *retVal)
67{
68 struct eth_device *edev = eth_get_dev_by_name(dev);
69 struct fec_priv *fec = (struct fec_priv *)edev->priv;
70
71 uint32_t reg; /* convenient holder for the PHY register */
72 uint32_t phy; /* convenient holder for the PHY */
73 uint32_t start;
74
75 /*
76 * reading from any PHY's register is done by properly
77 * programming the FEC's MII data register.
78 */
79 writel(FEC_IEVENT_MII, &fec->eth->ievent);
80 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
81 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
82
83 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
84 phy | reg, &fec->eth->mii_data);
85
86 /*
87 * wait for the related interrupt
88 */
89 start = get_timer_masked();
90 while (!(readl(&fec->eth->ievent) & FEC_IEVENT_MII)) {
91 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
92 printf("Read MDIO failed...\n");
93 return -1;
94 }
95 }
96
97 /*
98 * clear mii interrupt bit
99 */
100 writel(FEC_IEVENT_MII, &fec->eth->ievent);
101
102 /*
103 * it's now safe to read the PHY's register
104 */
105 *retVal = readl(&fec->eth->mii_data);
106 debug("fec_miiphy_read: phy: %02x reg:%02x val:%#x\n", phyAddr,
107 regAddr, *retVal);
108 return 0;
109}
110
Stefano Babic889f2e22010-02-01 14:51:30 +0100111static void fec_mii_setspeed(struct fec_priv *fec)
112{
113 /*
114 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
115 * and do not drop the Preamble.
116 */
117 writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
118 &fec->eth->mii_speed);
119 debug("fec_init: mii_speed %#lx\n",
120 fec->eth->mii_speed);
121}
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -0400122static int fec_miiphy_write(const char *dev, uint8_t phyAddr, uint8_t regAddr,
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400123 uint16_t data)
124{
125 struct eth_device *edev = eth_get_dev_by_name(dev);
126 struct fec_priv *fec = (struct fec_priv *)edev->priv;
127
128 uint32_t reg; /* convenient holder for the PHY register */
129 uint32_t phy; /* convenient holder for the PHY */
130 uint32_t start;
131
132 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
133 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
134
135 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
136 FEC_MII_DATA_TA | phy | reg | data, &fec->eth->mii_data);
137
138 /*
139 * wait for the MII interrupt
140 */
141 start = get_timer_masked();
142 while (!(readl(&fec->eth->ievent) & FEC_IEVENT_MII)) {
143 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
144 printf("Write MDIO failed...\n");
145 return -1;
146 }
147 }
148
149 /*
150 * clear MII interrupt bit
151 */
152 writel(FEC_IEVENT_MII, &fec->eth->ievent);
153 debug("fec_miiphy_write: phy: %02x reg:%02x val:%#x\n", phyAddr,
154 regAddr, data);
155
156 return 0;
157}
158
159static int miiphy_restart_aneg(struct eth_device *dev)
160{
161 /*
162 * Wake up from sleep if necessary
163 * Reset PHY, then delay 300ns
164 */
John Rigbye650e492010-01-25 23:12:55 -0700165#ifdef CONFIG_MX27
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400166 miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, PHY_MIPGSR, 0x00FF);
John Rigbye650e492010-01-25 23:12:55 -0700167#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400168 miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, PHY_BMCR,
169 PHY_BMCR_RESET);
170 udelay(1000);
171
172 /*
173 * Set the auto-negotiation advertisement register bits
174 */
javier Martince2519a2009-10-29 08:18:34 +0100175 miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, PHY_ANAR,
176 PHY_ANLPAR_TXFD | PHY_ANLPAR_TX | PHY_ANLPAR_10FD |
177 PHY_ANLPAR_10 | PHY_ANLPAR_PSB_802_3);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400178 miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, PHY_BMCR,
179 PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
180
181 return 0;
182}
183
184static int miiphy_wait_aneg(struct eth_device *dev)
185{
186 uint32_t start;
187 uint16_t status;
188
189 /*
190 * Wait for AN completion
191 */
192 start = get_timer_masked();
193 do {
194 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
195 printf("%s: Autonegotiation timeout\n", dev->name);
196 return -1;
197 }
198
199 if (miiphy_read(dev->name, CONFIG_FEC_MXC_PHYADDR,
200 PHY_BMSR, &status)) {
201 printf("%s: Autonegotiation failed. status: 0x%04x\n",
202 dev->name, status);
203 return -1;
204 }
205 } while (!(status & PHY_BMSR_LS));
206
207 return 0;
208}
209static int fec_rx_task_enable(struct fec_priv *fec)
210{
211 writel(1 << 24, &fec->eth->r_des_active);
212 return 0;
213}
214
215static int fec_rx_task_disable(struct fec_priv *fec)
216{
217 return 0;
218}
219
220static int fec_tx_task_enable(struct fec_priv *fec)
221{
222 writel(1 << 24, &fec->eth->x_des_active);
223 return 0;
224}
225
226static int fec_tx_task_disable(struct fec_priv *fec)
227{
228 return 0;
229}
230
231/**
232 * Initialize receive task's buffer descriptors
233 * @param[in] fec all we know about the device yet
234 * @param[in] count receive buffer count to be allocated
235 * @param[in] size size of each receive buffer
236 * @return 0 on success
237 *
238 * For this task we need additional memory for the data buffers. And each
239 * data buffer requires some alignment. Thy must be aligned to a specific
240 * boundary each (DB_DATA_ALIGNMENT).
241 */
242static int fec_rbd_init(struct fec_priv *fec, int count, int size)
243{
244 int ix;
245 uint32_t p = 0;
246
247 /* reserve data memory and consider alignment */
javier Martine87eb992009-10-29 08:22:43 +0100248 if (fec->rdb_ptr == NULL)
249 fec->rdb_ptr = malloc(size * count + DB_DATA_ALIGNMENT);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400250 p = (uint32_t)fec->rdb_ptr;
251 if (!p) {
Stefano Babic889f2e22010-02-01 14:51:30 +0100252 puts("fec_mxc: not enough malloc memory\n");
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400253 return -ENOMEM;
254 }
255 memset((void *)p, 0, size * count + DB_DATA_ALIGNMENT);
256 p += DB_DATA_ALIGNMENT-1;
257 p &= ~(DB_DATA_ALIGNMENT-1);
258
259 for (ix = 0; ix < count; ix++) {
260 writel(p, &fec->rbd_base[ix].data_pointer);
261 p += size;
262 writew(FEC_RBD_EMPTY, &fec->rbd_base[ix].status);
263 writew(0, &fec->rbd_base[ix].data_length);
264 }
265 /*
266 * mark the last RBD to close the ring
267 */
268 writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[ix - 1].status);
269 fec->rbd_index = 0;
270
271 return 0;
272}
273
274/**
275 * Initialize transmit task's buffer descriptors
276 * @param[in] fec all we know about the device yet
277 *
278 * Transmit buffers are created externally. We only have to init the BDs here.\n
279 * Note: There is a race condition in the hardware. When only one BD is in
280 * use it must be marked with the WRAP bit to use it for every transmitt.
281 * This bit in combination with the READY bit results into double transmit
282 * of each data buffer. It seems the state machine checks READY earlier then
283 * resetting it after the first transfer.
284 * Using two BDs solves this issue.
285 */
286static void fec_tbd_init(struct fec_priv *fec)
287{
288 writew(0x0000, &fec->tbd_base[0].status);
289 writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
290 fec->tbd_index = 0;
291}
292
293/**
294 * Mark the given read buffer descriptor as free
295 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
296 * @param[in] pRbd buffer descriptor to mark free again
297 */
298static void fec_rbd_clean(int last, struct fec_bd *pRbd)
299{
300 /*
301 * Reset buffer descriptor as empty
302 */
303 if (last)
304 writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &pRbd->status);
305 else
306 writew(FEC_RBD_EMPTY, &pRbd->status);
307 /*
308 * no data in it
309 */
310 writew(0, &pRbd->data_length);
311}
312
313static int fec_get_hwaddr(struct eth_device *dev, unsigned char *mac)
314{
Stefano Babic889f2e22010-02-01 14:51:30 +0100315/*
316 * The MX27 can store the mac address in internal eeprom
John Rigby8afce9a2010-04-07 23:29:40 -0600317 * This mechanism is not supported now by MX51 or MX25
Stefano Babic889f2e22010-02-01 14:51:30 +0100318 */
John Rigby8afce9a2010-04-07 23:29:40 -0600319#if defined(CONFIG_MX51) || defined(CONFIG_MX25)
Stefano Babic889f2e22010-02-01 14:51:30 +0100320 return -1;
321#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400322 struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
323 int i;
324
325 for (i = 0; i < 6; i++)
326 mac[6-1-i] = readl(&iim->iim_bank_area0[IIM0_MAC + i]);
327
Eric Jarrigecc0d0d42010-04-16 00:03:19 +0200328 return !is_valid_ether_addr(mac);
Stefano Babic889f2e22010-02-01 14:51:30 +0100329#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400330}
331
Stefano Babic889f2e22010-02-01 14:51:30 +0100332static int fec_set_hwaddr(struct eth_device *dev)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400333{
Stefano Babic889f2e22010-02-01 14:51:30 +0100334 uchar *mac = dev->enetaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400335 struct fec_priv *fec = (struct fec_priv *)dev->priv;
336
337 writel(0, &fec->eth->iaddr1);
338 writel(0, &fec->eth->iaddr2);
339 writel(0, &fec->eth->gaddr1);
340 writel(0, &fec->eth->gaddr2);
341
342 /*
343 * Set physical address
344 */
345 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
346 &fec->eth->paddr1);
347 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
348
349 return 0;
350}
351
352/**
353 * Start the FEC engine
354 * @param[in] dev Our device to handle
355 */
356static int fec_open(struct eth_device *edev)
357{
358 struct fec_priv *fec = (struct fec_priv *)edev->priv;
359
360 debug("fec_open: fec_open(dev)\n");
361 /* full-duplex, heartbeat disabled */
362 writel(1 << 2, &fec->eth->x_cntrl);
363 fec->rbd_index = 0;
364
365 /*
366 * Enable FEC-Lite controller
367 */
John Rigbye650e492010-01-25 23:12:55 -0700368 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
369 &fec->eth->ecntrl);
John Rigby99d5fed2010-01-25 23:12:57 -0700370#ifdef CONFIG_MX25
371 udelay(100);
372 /*
373 * setup the MII gasket for RMII mode
374 */
375
376 /* disable the gasket */
377 writew(0, &fec->eth->miigsk_enr);
378
379 /* wait for the gasket to be disabled */
380 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
381 udelay(2);
382
383 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
384 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
385
386 /* re-enable the gasket */
387 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
388
389 /* wait until MII gasket is ready */
390 int max_loops = 10;
391 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
392 if (--max_loops <= 0) {
393 printf("WAIT for MII Gasket ready timed out\n");
394 break;
395 }
396 }
397#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400398
399 miiphy_wait_aneg(edev);
javier Martince2519a2009-10-29 08:18:34 +0100400 miiphy_speed(edev->name, CONFIG_FEC_MXC_PHYADDR);
401 miiphy_duplex(edev->name, CONFIG_FEC_MXC_PHYADDR);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400402
403 /*
404 * Enable SmartDMA receive task
405 */
406 fec_rx_task_enable(fec);
407
408 udelay(100000);
409 return 0;
410}
411
412static int fec_init(struct eth_device *dev, bd_t* bd)
413{
414 uint32_t base;
415 struct fec_priv *fec = (struct fec_priv *)dev->priv;
416
John Rigbya4a30552010-10-13 14:31:08 -0600417 /* Initialize MAC address */
418 fec_set_hwaddr(dev);
419
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400420 /*
421 * reserve memory for both buffer descriptor chains at once
422 * Datasheet forces the startaddress of each chain is 16 byte
423 * aligned
424 */
javier Martine87eb992009-10-29 08:22:43 +0100425 if (fec->base_ptr == NULL)
426 fec->base_ptr = malloc((2 + FEC_RBD_NUM) *
427 sizeof(struct fec_bd) + DB_ALIGNMENT);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400428 base = (uint32_t)fec->base_ptr;
429 if (!base) {
Stefano Babic889f2e22010-02-01 14:51:30 +0100430 puts("fec_mxc: not enough malloc memory\n");
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400431 return -ENOMEM;
432 }
433 memset((void *)base, 0, (2 + FEC_RBD_NUM) *
434 sizeof(struct fec_bd) + DB_ALIGNMENT);
435 base += (DB_ALIGNMENT-1);
436 base &= ~(DB_ALIGNMENT-1);
437
438 fec->rbd_base = (struct fec_bd *)base;
439
440 base += FEC_RBD_NUM * sizeof(struct fec_bd);
441
442 fec->tbd_base = (struct fec_bd *)base;
443
444 /*
445 * Set interrupt mask register
446 */
447 writel(0x00000000, &fec->eth->imask);
448
449 /*
450 * Clear FEC-Lite interrupt event register(IEVENT)
451 */
452 writel(0xffffffff, &fec->eth->ievent);
453
454
455 /*
456 * Set FEC-Lite receive control register(R_CNTRL):
457 */
458 if (fec->xcv_type == SEVENWIRE) {
459 /*
460 * Frame length=1518; 7-wire mode
461 */
462 writel(0x05ee0020, &fec->eth->r_cntrl); /* FIXME 0x05ee0000 */
463 } else {
464 /*
465 * Frame length=1518; MII mode;
466 */
467 writel(0x05ee0024, &fec->eth->r_cntrl); /* FIXME 0x05ee0004 */
Stefano Babic889f2e22010-02-01 14:51:30 +0100468
469 fec_mii_setspeed(fec);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400470 }
471 /*
472 * Set Opcode/Pause Duration Register
473 */
474 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
475 writel(0x2, &fec->eth->x_wmrk);
476 /*
477 * Set multicast address filter
478 */
479 writel(0x00000000, &fec->eth->gaddr1);
480 writel(0x00000000, &fec->eth->gaddr2);
481
482
483 /* clear MIB RAM */
484 long *mib_ptr = (long *)(IMX_FEC_BASE + 0x200);
485 while (mib_ptr <= (long *)(IMX_FEC_BASE + 0x2FC))
486 *mib_ptr++ = 0;
487
488 /* FIFO receive start register */
489 writel(0x520, &fec->eth->r_fstart);
490
491 /* size and address of each buffer */
492 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
493 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
494 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
495
496 /*
497 * Initialize RxBD/TxBD rings
498 */
499 if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
500 free(fec->base_ptr);
John Ogness331daa12009-12-11 09:47:28 +0100501 fec->base_ptr = NULL;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400502 return -ENOMEM;
503 }
504 fec_tbd_init(fec);
505
506
507 if (fec->xcv_type != SEVENWIRE)
508 miiphy_restart_aneg(dev);
509
510 fec_open(dev);
511 return 0;
512}
513
514/**
515 * Halt the FEC engine
516 * @param[in] dev Our device to handle
517 */
518static void fec_halt(struct eth_device *dev)
519{
520 struct fec_priv *fec = &gfec;
521 int counter = 0xffff;
522
523 /*
524 * issue graceful stop command to the FEC transmitter if necessary
525 */
John Rigbye650e492010-01-25 23:12:55 -0700526 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400527 &fec->eth->x_cntrl);
528
529 debug("eth_halt: wait for stop regs\n");
530 /*
531 * wait for graceful stop to register
532 */
533 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
John Rigbye650e492010-01-25 23:12:55 -0700534 udelay(1);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400535
536 /*
537 * Disable SmartDMA tasks
538 */
539 fec_tx_task_disable(fec);
540 fec_rx_task_disable(fec);
541
542 /*
543 * Disable the Ethernet Controller
544 * Note: this will also reset the BD index counter!
545 */
John Rigby99d5fed2010-01-25 23:12:57 -0700546 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
547 &fec->eth->ecntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400548 fec->rbd_index = 0;
549 fec->tbd_index = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400550 debug("eth_halt: done\n");
551}
552
553/**
554 * Transmit one frame
555 * @param[in] dev Our ethernet device to handle
556 * @param[in] packet Pointer to the data to be transmitted
557 * @param[in] length Data count in bytes
558 * @return 0 on success
559 */
560static int fec_send(struct eth_device *dev, volatile void* packet, int length)
561{
562 unsigned int status;
563
564 /*
565 * This routine transmits one frame. This routine only accepts
566 * 6-byte Ethernet addresses.
567 */
568 struct fec_priv *fec = (struct fec_priv *)dev->priv;
569
570 /*
571 * Check for valid length of data.
572 */
573 if ((length > 1500) || (length <= 0)) {
Stefano Babic889f2e22010-02-01 14:51:30 +0100574 printf("Payload (%d) too large\n", length);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400575 return -1;
576 }
577
578 /*
579 * Setup the transmit buffer
580 * Note: We are always using the first buffer for transmission,
581 * the second will be empty and only used to stop the DMA engine
582 */
583 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
584 writel((uint32_t)packet, &fec->tbd_base[fec->tbd_index].data_pointer);
585 /*
586 * update BD's status now
587 * This block:
588 * - is always the last in a chain (means no chain)
589 * - should transmitt the CRC
590 * - might be the last BD in the list, so the address counter should
591 * wrap (-> keep the WRAP flag)
592 */
593 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
594 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
595 writew(status, &fec->tbd_base[fec->tbd_index].status);
596
597 /*
598 * Enable SmartDMA transmit task
599 */
600 fec_tx_task_enable(fec);
601
602 /*
603 * wait until frame is sent .
604 */
605 while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
John Rigbye650e492010-01-25 23:12:55 -0700606 udelay(1);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400607 }
608 debug("fec_send: status 0x%x index %d\n",
609 readw(&fec->tbd_base[fec->tbd_index].status),
610 fec->tbd_index);
611 /* for next transmission use the other buffer */
612 if (fec->tbd_index)
613 fec->tbd_index = 0;
614 else
615 fec->tbd_index = 1;
616
617 return 0;
618}
619
620/**
621 * Pull one frame from the card
622 * @param[in] dev Our ethernet device to handle
623 * @return Length of packet read
624 */
625static int fec_recv(struct eth_device *dev)
626{
627 struct fec_priv *fec = (struct fec_priv *)dev->priv;
628 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
629 unsigned long ievent;
630 int frame_length, len = 0;
631 struct nbuf *frame;
632 uint16_t bd_status;
633 uchar buff[FEC_MAX_PKT_SIZE];
634
635 /*
636 * Check if any critical events have happened
637 */
638 ievent = readl(&fec->eth->ievent);
639 writel(ievent, &fec->eth->ievent);
640 debug("fec_recv: ievent 0x%x\n", ievent);
641 if (ievent & FEC_IEVENT_BABR) {
642 fec_halt(dev);
643 fec_init(dev, fec->bd);
644 printf("some error: 0x%08lx\n", ievent);
645 return 0;
646 }
647 if (ievent & FEC_IEVENT_HBERR) {
648 /* Heartbeat error */
649 writel(0x00000001 | readl(&fec->eth->x_cntrl),
650 &fec->eth->x_cntrl);
651 }
652 if (ievent & FEC_IEVENT_GRA) {
653 /* Graceful stop complete */
654 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
655 fec_halt(dev);
656 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
657 &fec->eth->x_cntrl);
658 fec_init(dev, fec->bd);
659 }
660 }
661
662 /*
663 * ensure reading the right buffer status
664 */
665 bd_status = readw(&rbd->status);
666 debug("fec_recv: status 0x%x\n", bd_status);
667
668 if (!(bd_status & FEC_RBD_EMPTY)) {
669 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
670 ((readw(&rbd->data_length) - 4) > 14)) {
671 /*
672 * Get buffer address and size
673 */
674 frame = (struct nbuf *)readl(&rbd->data_pointer);
675 frame_length = readw(&rbd->data_length) - 4;
676 /*
677 * Fill the buffer and pass it to upper layers
678 */
679 memcpy(buff, frame->data, frame_length);
680 NetReceive(buff, frame_length);
681 len = frame_length;
682 } else {
683 if (bd_status & FEC_RBD_ERR)
684 printf("error frame: 0x%08lx 0x%08x\n",
685 (ulong)rbd->data_pointer,
686 bd_status);
687 }
688 /*
689 * free the current buffer, restart the engine
690 * and move forward to the next buffer
691 */
692 fec_rbd_clean(fec->rbd_index == (FEC_RBD_NUM - 1) ? 1 : 0, rbd);
693 fec_rx_task_enable(fec);
694 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
695 }
696 debug("fec_recv: stop\n");
697
698 return len;
699}
700
701static int fec_probe(bd_t *bd)
702{
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400703 struct eth_device *edev;
704 struct fec_priv *fec = &gfec;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400705 unsigned char ethaddr[6];
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400706
707 /* create and fill edev struct */
708 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
709 if (!edev) {
Stefano Babic889f2e22010-02-01 14:51:30 +0100710 puts("fec_mxc: not enough malloc memory\n");
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400711 return -ENOMEM;
712 }
Nobuhiro Iwamatsu1843c5b2010-10-19 14:03:42 +0900713 memset(edev, 0, sizeof(*edev));
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400714 edev->priv = fec;
715 edev->init = fec_init;
716 edev->send = fec_send;
717 edev->recv = fec_recv;
718 edev->halt = fec_halt;
Heiko Schocher9ada5e62010-04-27 07:43:52 +0200719 edev->write_hwaddr = fec_set_hwaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400720
721 fec->eth = (struct ethernet_regs *)IMX_FEC_BASE;
722 fec->bd = bd;
723
724 fec->xcv_type = MII100;
725
726 /* Reset chip. */
John Rigbye650e492010-01-25 23:12:55 -0700727 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400728 while (readl(&fec->eth->ecntrl) & 1)
729 udelay(10);
730
731 /*
732 * Set interrupt mask register
733 */
734 writel(0x00000000, &fec->eth->imask);
735
736 /*
737 * Clear FEC-Lite interrupt event register(IEVENT)
738 */
739 writel(0xffffffff, &fec->eth->ievent);
740
741 /*
742 * Set FEC-Lite receive control register(R_CNTRL):
743 */
744 /*
745 * Frame length=1518; MII mode;
746 */
747 writel(0x05ee0024, &fec->eth->r_cntrl); /* FIXME 0x05ee0004 */
Stefano Babic889f2e22010-02-01 14:51:30 +0100748 fec_mii_setspeed(fec);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400749
Stefano Babic63469ba2010-07-20 07:57:07 +0200750 sprintf(edev->name, "FEC");
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400751
752 miiphy_register(edev->name, fec_miiphy_read, fec_miiphy_write);
753
754 eth_register(edev);
755
Stefano Babic889f2e22010-02-01 14:51:30 +0100756 if (fec_get_hwaddr(edev, ethaddr) == 0) {
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400757 printf("got MAC address from EEPROM: %pM\n", ethaddr);
Stefano Babic889f2e22010-02-01 14:51:30 +0100758 memcpy(edev->enetaddr, ethaddr, 6);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400759 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400760
761 return 0;
762}
763
764int fecmxc_initialize(bd_t *bd)
765{
766 int lout = 1;
767
768 debug("eth_init: fec_probe(bd)\n");
769 lout = fec_probe(bd);
770
771 return lout;
772}