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>
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.
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.
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,
30 #include <asm/arch/clock.h>
31 #include <asm/arch/imx-regs.h>
33 #include <asm/errno.h>
35 DECLARE_GLOBAL_DATA_PTR;
38 #error "CONFIG_MII has to be defined!"
41 #ifndef CONFIG_FEC_XCV_TYPE
42 #define CONFIG_FEC_XCV_TYPE MII100
48 uint8_t data[1500]; /**< actual data */
49 int length; /**< actual length */
50 int used; /**< buffer in use or not */
51 uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
54 struct fec_priv gfec = {
55 .eth = (struct ethernet_regs *)IMX_FEC_BASE,
67 * MII-interface related functions
69 static int fec_miiphy_read(const char *dev, uint8_t phyAddr, uint8_t regAddr,
72 struct eth_device *edev = eth_get_dev_by_name(dev);
73 struct fec_priv *fec = (struct fec_priv *)edev->priv;
74 struct ethernet_regs *eth = fec->eth;
76 uint32_t reg; /* convenient holder for the PHY register */
77 uint32_t phy; /* convenient holder for the PHY */
81 * reading from any PHY's register is done by properly
82 * programming the FEC's MII data register.
84 writel(FEC_IEVENT_MII, ð->ievent);
85 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
86 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
88 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
89 phy | reg, ð->mii_data);
92 * wait for the related interrupt
95 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
96 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
97 printf("Read MDIO failed...\n");
103 * clear mii interrupt bit
105 writel(FEC_IEVENT_MII, ð->ievent);
108 * it's now safe to read the PHY's register
110 *retVal = readl(ð->mii_data);
111 debug("fec_miiphy_read: phy: %02x reg:%02x val:%#x\n", phyAddr,
116 static void fec_mii_setspeed(struct fec_priv *fec)
119 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
120 * and do not drop the Preamble.
122 writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
123 &fec->eth->mii_speed);
124 debug("fec_init: mii_speed %#lx\n",
125 readl(&fec->eth->mii_speed));
127 static int fec_miiphy_write(const char *dev, uint8_t phyAddr, uint8_t regAddr,
130 struct eth_device *edev = eth_get_dev_by_name(dev);
131 struct fec_priv *fec = (struct fec_priv *)edev->priv;
132 struct ethernet_regs *eth = fec->eth;
134 uint32_t reg; /* convenient holder for the PHY register */
135 uint32_t phy; /* convenient holder for the PHY */
138 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
139 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
141 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
142 FEC_MII_DATA_TA | phy | reg | data, ð->mii_data);
145 * wait for the MII interrupt
147 start = get_timer(0);
148 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
149 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
150 printf("Write MDIO failed...\n");
156 * clear MII interrupt bit
158 writel(FEC_IEVENT_MII, ð->ievent);
159 debug("fec_miiphy_write: phy: %02x reg:%02x val:%#x\n", phyAddr,
165 static int miiphy_restart_aneg(struct eth_device *dev)
168 * Wake up from sleep if necessary
169 * Reset PHY, then delay 300ns
172 miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, MII_DCOUNTER, 0x00FF);
174 miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, MII_BMCR,
179 * Set the auto-negotiation advertisement register bits
181 miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, MII_ADVERTISE,
182 LPA_100FULL | LPA_100HALF | LPA_10FULL |
183 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
184 miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, MII_BMCR,
185 BMCR_ANENABLE | BMCR_ANRESTART);
190 static int miiphy_wait_aneg(struct eth_device *dev)
196 * Wait for AN completion
198 start = get_timer(0);
200 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
201 printf("%s: Autonegotiation timeout\n", dev->name);
205 if (miiphy_read(dev->name, CONFIG_FEC_MXC_PHYADDR,
206 MII_BMSR, &status)) {
207 printf("%s: Autonegotiation failed. status: 0x%04x\n",
211 } while (!(status & BMSR_LSTATUS));
215 static int fec_rx_task_enable(struct fec_priv *fec)
217 writel(1 << 24, &fec->eth->r_des_active);
221 static int fec_rx_task_disable(struct fec_priv *fec)
226 static int fec_tx_task_enable(struct fec_priv *fec)
228 writel(1 << 24, &fec->eth->x_des_active);
232 static int fec_tx_task_disable(struct fec_priv *fec)
238 * Initialize receive task's buffer descriptors
239 * @param[in] fec all we know about the device yet
240 * @param[in] count receive buffer count to be allocated
241 * @param[in] size size of each receive buffer
242 * @return 0 on success
244 * For this task we need additional memory for the data buffers. And each
245 * data buffer requires some alignment. Thy must be aligned to a specific
246 * boundary each (DB_DATA_ALIGNMENT).
248 static int fec_rbd_init(struct fec_priv *fec, int count, int size)
253 /* reserve data memory and consider alignment */
254 if (fec->rdb_ptr == NULL)
255 fec->rdb_ptr = malloc(size * count + DB_DATA_ALIGNMENT);
256 p = (uint32_t)fec->rdb_ptr;
258 puts("fec_mxc: not enough malloc memory\n");
261 memset((void *)p, 0, size * count + DB_DATA_ALIGNMENT);
262 p += DB_DATA_ALIGNMENT-1;
263 p &= ~(DB_DATA_ALIGNMENT-1);
265 for (ix = 0; ix < count; ix++) {
266 writel(p, &fec->rbd_base[ix].data_pointer);
268 writew(FEC_RBD_EMPTY, &fec->rbd_base[ix].status);
269 writew(0, &fec->rbd_base[ix].data_length);
272 * mark the last RBD to close the ring
274 writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[ix - 1].status);
281 * Initialize transmit task's buffer descriptors
282 * @param[in] fec all we know about the device yet
284 * Transmit buffers are created externally. We only have to init the BDs here.\n
285 * Note: There is a race condition in the hardware. When only one BD is in
286 * use it must be marked with the WRAP bit to use it for every transmitt.
287 * This bit in combination with the READY bit results into double transmit
288 * of each data buffer. It seems the state machine checks READY earlier then
289 * resetting it after the first transfer.
290 * Using two BDs solves this issue.
292 static void fec_tbd_init(struct fec_priv *fec)
294 writew(0x0000, &fec->tbd_base[0].status);
295 writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
300 * Mark the given read buffer descriptor as free
301 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
302 * @param[in] pRbd buffer descriptor to mark free again
304 static void fec_rbd_clean(int last, struct fec_bd *pRbd)
307 * Reset buffer descriptor as empty
310 writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &pRbd->status);
312 writew(FEC_RBD_EMPTY, &pRbd->status);
316 writew(0, &pRbd->data_length);
319 static int fec_get_hwaddr(struct eth_device *dev, unsigned char *mac)
321 imx_get_mac_from_fuse(mac);
322 return !is_valid_ether_addr(mac);
325 static int fec_set_hwaddr(struct eth_device *dev)
327 uchar *mac = dev->enetaddr;
328 struct fec_priv *fec = (struct fec_priv *)dev->priv;
330 writel(0, &fec->eth->iaddr1);
331 writel(0, &fec->eth->iaddr2);
332 writel(0, &fec->eth->gaddr1);
333 writel(0, &fec->eth->gaddr2);
336 * Set physical address
338 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
340 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
346 * Start the FEC engine
347 * @param[in] dev Our device to handle
349 static int fec_open(struct eth_device *edev)
351 struct fec_priv *fec = (struct fec_priv *)edev->priv;
353 debug("fec_open: fec_open(dev)\n");
354 /* full-duplex, heartbeat disabled */
355 writel(1 << 2, &fec->eth->x_cntrl);
359 * Enable FEC-Lite controller
361 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
363 #if defined(CONFIG_MX25) || defined(CONFIG_MX53)
366 * setup the MII gasket for RMII mode
369 /* disable the gasket */
370 writew(0, &fec->eth->miigsk_enr);
372 /* wait for the gasket to be disabled */
373 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
376 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
377 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
379 /* re-enable the gasket */
380 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
382 /* wait until MII gasket is ready */
384 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
385 if (--max_loops <= 0) {
386 printf("WAIT for MII Gasket ready timed out\n");
392 miiphy_wait_aneg(edev);
393 miiphy_speed(edev->name, CONFIG_FEC_MXC_PHYADDR);
394 miiphy_duplex(edev->name, CONFIG_FEC_MXC_PHYADDR);
397 * Enable SmartDMA receive task
399 fec_rx_task_enable(fec);
405 static int fec_init(struct eth_device *dev, bd_t* bd)
408 struct fec_priv *fec = (struct fec_priv *)dev->priv;
411 /* Initialize MAC address */
415 * reserve memory for both buffer descriptor chains at once
416 * Datasheet forces the startaddress of each chain is 16 byte
419 if (fec->base_ptr == NULL)
420 fec->base_ptr = malloc((2 + FEC_RBD_NUM) *
421 sizeof(struct fec_bd) + DB_ALIGNMENT);
422 base = (uint32_t)fec->base_ptr;
424 puts("fec_mxc: not enough malloc memory\n");
427 memset((void *)base, 0, (2 + FEC_RBD_NUM) *
428 sizeof(struct fec_bd) + DB_ALIGNMENT);
429 base += (DB_ALIGNMENT-1);
430 base &= ~(DB_ALIGNMENT-1);
432 fec->rbd_base = (struct fec_bd *)base;
434 base += FEC_RBD_NUM * sizeof(struct fec_bd);
436 fec->tbd_base = (struct fec_bd *)base;
439 * Set interrupt mask register
441 writel(0x00000000, &fec->eth->imask);
444 * Clear FEC-Lite interrupt event register(IEVENT)
446 writel(0xffffffff, &fec->eth->ievent);
450 * Set FEC-Lite receive control register(R_CNTRL):
453 /* Start with frame length = 1518, common for all modes. */
454 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
455 if (fec->xcv_type == SEVENWIRE)
456 rcntrl |= FEC_RCNTRL_FCE;
457 else if (fec->xcv_type == RMII)
458 rcntrl |= FEC_RCNTRL_RMII;
460 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
462 writel(rcntrl, &fec->eth->r_cntrl);
464 if (fec->xcv_type == MII10 || fec->xcv_type == MII100)
465 fec_mii_setspeed(fec);
468 * Set Opcode/Pause Duration Register
470 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
471 writel(0x2, &fec->eth->x_wmrk);
473 * Set multicast address filter
475 writel(0x00000000, &fec->eth->gaddr1);
476 writel(0x00000000, &fec->eth->gaddr2);
480 long *mib_ptr = (long *)(IMX_FEC_BASE + 0x200);
481 while (mib_ptr <= (long *)(IMX_FEC_BASE + 0x2FC))
484 /* FIFO receive start register */
485 writel(0x520, &fec->eth->r_fstart);
487 /* size and address of each buffer */
488 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
489 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
490 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
493 * Initialize RxBD/TxBD rings
495 if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
497 fec->base_ptr = NULL;
503 if (fec->xcv_type != SEVENWIRE)
504 miiphy_restart_aneg(dev);
511 * Halt the FEC engine
512 * @param[in] dev Our device to handle
514 static void fec_halt(struct eth_device *dev)
516 struct fec_priv *fec = &gfec;
517 int counter = 0xffff;
520 * issue graceful stop command to the FEC transmitter if necessary
522 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
525 debug("eth_halt: wait for stop regs\n");
527 * wait for graceful stop to register
529 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
533 * Disable SmartDMA tasks
535 fec_tx_task_disable(fec);
536 fec_rx_task_disable(fec);
539 * Disable the Ethernet Controller
540 * Note: this will also reset the BD index counter!
542 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
546 debug("eth_halt: done\n");
551 * @param[in] dev Our ethernet device to handle
552 * @param[in] packet Pointer to the data to be transmitted
553 * @param[in] length Data count in bytes
554 * @return 0 on success
556 static int fec_send(struct eth_device *dev, volatile void* packet, int length)
561 * This routine transmits one frame. This routine only accepts
562 * 6-byte Ethernet addresses.
564 struct fec_priv *fec = (struct fec_priv *)dev->priv;
567 * Check for valid length of data.
569 if ((length > 1500) || (length <= 0)) {
570 printf("Payload (%d) too large\n", length);
575 * Setup the transmit buffer
576 * Note: We are always using the first buffer for transmission,
577 * the second will be empty and only used to stop the DMA engine
579 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
580 writel((uint32_t)packet, &fec->tbd_base[fec->tbd_index].data_pointer);
582 * update BD's status now
584 * - is always the last in a chain (means no chain)
585 * - should transmitt the CRC
586 * - might be the last BD in the list, so the address counter should
587 * wrap (-> keep the WRAP flag)
589 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
590 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
591 writew(status, &fec->tbd_base[fec->tbd_index].status);
594 * Enable SmartDMA transmit task
596 fec_tx_task_enable(fec);
599 * wait until frame is sent .
601 while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
604 debug("fec_send: status 0x%x index %d\n",
605 readw(&fec->tbd_base[fec->tbd_index].status),
607 /* for next transmission use the other buffer */
617 * Pull one frame from the card
618 * @param[in] dev Our ethernet device to handle
619 * @return Length of packet read
621 static int fec_recv(struct eth_device *dev)
623 struct fec_priv *fec = (struct fec_priv *)dev->priv;
624 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
625 unsigned long ievent;
626 int frame_length, len = 0;
629 uchar buff[FEC_MAX_PKT_SIZE];
632 * Check if any critical events have happened
634 ievent = readl(&fec->eth->ievent);
635 writel(ievent, &fec->eth->ievent);
636 debug("fec_recv: ievent 0x%x\n", ievent);
637 if (ievent & FEC_IEVENT_BABR) {
639 fec_init(dev, fec->bd);
640 printf("some error: 0x%08lx\n", ievent);
643 if (ievent & FEC_IEVENT_HBERR) {
644 /* Heartbeat error */
645 writel(0x00000001 | readl(&fec->eth->x_cntrl),
648 if (ievent & FEC_IEVENT_GRA) {
649 /* Graceful stop complete */
650 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
652 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
654 fec_init(dev, fec->bd);
659 * ensure reading the right buffer status
661 bd_status = readw(&rbd->status);
662 debug("fec_recv: status 0x%x\n", bd_status);
664 if (!(bd_status & FEC_RBD_EMPTY)) {
665 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
666 ((readw(&rbd->data_length) - 4) > 14)) {
668 * Get buffer address and size
670 frame = (struct nbuf *)readl(&rbd->data_pointer);
671 frame_length = readw(&rbd->data_length) - 4;
673 * Fill the buffer and pass it to upper layers
675 memcpy(buff, frame->data, frame_length);
676 NetReceive(buff, frame_length);
679 if (bd_status & FEC_RBD_ERR)
680 printf("error frame: 0x%08lx 0x%08x\n",
681 (ulong)rbd->data_pointer,
685 * free the current buffer, restart the engine
686 * and move forward to the next buffer
688 fec_rbd_clean(fec->rbd_index == (FEC_RBD_NUM - 1) ? 1 : 0, rbd);
689 fec_rx_task_enable(fec);
690 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
692 debug("fec_recv: stop\n");
697 static int fec_probe(bd_t *bd)
699 struct eth_device *edev;
700 struct fec_priv *fec = &gfec;
701 unsigned char ethaddr[6];
703 /* create and fill edev struct */
704 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
706 puts("fec_mxc: not enough malloc memory\n");
709 memset(edev, 0, sizeof(*edev));
711 edev->init = fec_init;
712 edev->send = fec_send;
713 edev->recv = fec_recv;
714 edev->halt = fec_halt;
715 edev->write_hwaddr = fec_set_hwaddr;
717 fec->eth = (struct ethernet_regs *)IMX_FEC_BASE;
720 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
723 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
724 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET)
728 * Set interrupt mask register
730 writel(0x00000000, &fec->eth->imask);
733 * Clear FEC-Lite interrupt event register(IEVENT)
735 writel(0xffffffff, &fec->eth->ievent);
738 * Set FEC-Lite receive control register(R_CNTRL):
741 * Frame length=1518; MII mode;
743 writel((PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT) | FEC_RCNTRL_FCE |
744 FEC_RCNTRL_MII_MODE, &fec->eth->r_cntrl);
745 fec_mii_setspeed(fec);
747 sprintf(edev->name, "FEC");
749 miiphy_register(edev->name, fec_miiphy_read, fec_miiphy_write);
753 if (fec_get_hwaddr(edev, ethaddr) == 0) {
754 printf("got MAC address from fuse: %pM\n", ethaddr);
755 memcpy(edev->enetaddr, ethaddr, 6);
761 int fecmxc_initialize(bd_t *bd)
765 debug("eth_init: fec_probe(bd)\n");
766 lout = fec_probe(bd);