2 * Driver for the MPC5200 Fast Ethernet Controller
7 * Copyright (C) 2007 Domen Puncer, Telargo, Inc.
9 * Copyright (C) 2003-2004 MontaVista, Software, Inc.
11 * This file is licensed under the terms of the GNU General Public License
12 * version 2. This program is licensed "as is" without any warranty of any
13 * kind, whether express or implied.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/dma-mapping.h>
20 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/types.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/errno.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
29 #include <linux/crc32.h>
30 #include <linux/hardirq.h>
31 #include <linux/delay.h>
33 #include <linux/of_address.h>
34 #include <linux/of_irq.h>
35 #include <linux/of_mdio.h>
36 #include <linux/of_net.h>
37 #include <linux/platform_device.h>
39 #include <linux/netdevice.h>
40 #include <linux/etherdevice.h>
41 #include <linux/ethtool.h>
42 #include <linux/skbuff.h>
45 #include <asm/delay.h>
46 #include <asm/mpc52xx.h>
48 #include <linux/fsl/bestcomm/bestcomm.h>
49 #include <linux/fsl/bestcomm/fec.h>
51 #include "fec_mpc52xx.h"
53 #define DRIVER_NAME "mpc52xx-fec"
55 /* Private driver data structure */
56 struct mpc52xx_fec_priv {
57 struct net_device *ndev;
62 struct mpc52xx_fec __iomem *fec;
63 struct bcom_task *rx_dmatsk;
64 struct bcom_task *tx_dmatsk;
68 /* MDIO link details */
69 unsigned int mdio_speed;
70 struct device_node *phy_node;
76 static irqreturn_t mpc52xx_fec_interrupt(int, void *);
77 static irqreturn_t mpc52xx_fec_rx_interrupt(int, void *);
78 static irqreturn_t mpc52xx_fec_tx_interrupt(int, void *);
79 static void mpc52xx_fec_stop(struct net_device *dev, bool may_sleep);
80 static void mpc52xx_fec_start(struct net_device *dev);
81 static void mpc52xx_fec_reset(struct net_device *dev);
83 #define MPC52xx_MESSAGES_DEFAULT ( NETIF_MSG_DRV | NETIF_MSG_PROBE | \
84 NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
85 static int debug = -1; /* the above default */
86 module_param(debug, int, 0);
87 MODULE_PARM_DESC(debug, "debugging messages level");
89 static void mpc52xx_fec_tx_timeout(struct net_device *dev, unsigned int txqueue)
91 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
94 dev_warn(&dev->dev, "transmit timed out\n");
96 spin_lock_irqsave(&priv->lock, flags);
97 mpc52xx_fec_reset(dev);
98 dev->stats.tx_errors++;
99 spin_unlock_irqrestore(&priv->lock, flags);
101 netif_wake_queue(dev);
104 static void mpc52xx_fec_set_paddr(struct net_device *dev, const u8 *mac)
106 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
107 struct mpc52xx_fec __iomem *fec = priv->fec;
109 out_be32(&fec->paddr1, *(const u32 *)(&mac[0]));
110 out_be32(&fec->paddr2, (*(const u16 *)(&mac[4]) << 16) | FEC_PADDR2_TYPE);
113 static int mpc52xx_fec_set_mac_address(struct net_device *dev, void *addr)
115 struct sockaddr *sock = addr;
117 eth_hw_addr_set(dev, sock->sa_data);
119 mpc52xx_fec_set_paddr(dev, sock->sa_data);
123 static void mpc52xx_fec_free_rx_buffers(struct net_device *dev, struct bcom_task *s)
125 while (!bcom_queue_empty(s)) {
126 struct bcom_fec_bd *bd;
129 skb = bcom_retrieve_buffer(s, NULL, (struct bcom_bd **)&bd);
130 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
137 mpc52xx_fec_rx_submit(struct net_device *dev, struct sk_buff *rskb)
139 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
140 struct bcom_fec_bd *bd;
142 bd = (struct bcom_fec_bd *) bcom_prepare_next_buffer(priv->rx_dmatsk);
143 bd->status = FEC_RX_BUFFER_SIZE;
144 bd->skb_pa = dma_map_single(dev->dev.parent, rskb->data,
145 FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
146 bcom_submit_next_buffer(priv->rx_dmatsk, rskb);
149 static int mpc52xx_fec_alloc_rx_buffers(struct net_device *dev, struct bcom_task *rxtsk)
153 while (!bcom_queue_full(rxtsk)) {
154 skb = netdev_alloc_skb(dev, FEC_RX_BUFFER_SIZE);
158 /* zero out the initial receive buffers to aid debugging */
159 memset(skb->data, 0, FEC_RX_BUFFER_SIZE);
160 mpc52xx_fec_rx_submit(dev, skb);
165 /* based on generic_adjust_link from fs_enet-main.c */
166 static void mpc52xx_fec_adjust_link(struct net_device *dev)
168 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
169 struct phy_device *phydev = dev->phydev;
172 if (phydev->link != PHY_DOWN) {
173 if (phydev->duplex != priv->duplex) {
174 struct mpc52xx_fec __iomem *fec = priv->fec;
179 priv->duplex = phydev->duplex;
181 rcntrl = in_be32(&fec->r_cntrl);
182 tcntrl = in_be32(&fec->x_cntrl);
184 rcntrl &= ~FEC_RCNTRL_DRT;
185 tcntrl &= ~FEC_TCNTRL_FDEN;
186 if (phydev->duplex == DUPLEX_FULL)
187 tcntrl |= FEC_TCNTRL_FDEN; /* FD enable */
189 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
191 out_be32(&fec->r_cntrl, rcntrl);
192 out_be32(&fec->x_cntrl, tcntrl);
195 if (phydev->speed != priv->speed) {
197 priv->speed = phydev->speed;
200 if (priv->link == PHY_DOWN) {
202 priv->link = phydev->link;
205 } else if (priv->link) {
207 priv->link = PHY_DOWN;
212 if (new_state && netif_msg_link(priv))
213 phy_print_status(phydev);
216 static int mpc52xx_fec_open(struct net_device *dev)
218 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
219 struct phy_device *phydev = NULL;
222 if (priv->phy_node) {
223 phydev = of_phy_connect(priv->ndev, priv->phy_node,
224 mpc52xx_fec_adjust_link, 0, 0);
226 dev_err(&dev->dev, "of_phy_connect failed\n");
232 if (request_irq(dev->irq, mpc52xx_fec_interrupt, IRQF_SHARED,
233 DRIVER_NAME "_ctrl", dev)) {
234 dev_err(&dev->dev, "ctrl interrupt request failed\n");
237 if (request_irq(priv->r_irq, mpc52xx_fec_rx_interrupt, 0,
238 DRIVER_NAME "_rx", dev)) {
239 dev_err(&dev->dev, "rx interrupt request failed\n");
242 if (request_irq(priv->t_irq, mpc52xx_fec_tx_interrupt, 0,
243 DRIVER_NAME "_tx", dev)) {
244 dev_err(&dev->dev, "tx interrupt request failed\n");
248 bcom_fec_rx_reset(priv->rx_dmatsk);
249 bcom_fec_tx_reset(priv->tx_dmatsk);
251 err = mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
253 dev_err(&dev->dev, "mpc52xx_fec_alloc_rx_buffers failed\n");
257 bcom_enable(priv->rx_dmatsk);
258 bcom_enable(priv->tx_dmatsk);
260 mpc52xx_fec_start(dev);
262 netif_start_queue(dev);
267 free_irq(priv->t_irq, dev);
269 free_irq(priv->r_irq, dev);
271 free_irq(dev->irq, dev);
275 phy_disconnect(phydev);
281 static int mpc52xx_fec_close(struct net_device *dev)
283 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
284 struct phy_device *phydev = dev->phydev;
286 netif_stop_queue(dev);
288 mpc52xx_fec_stop(dev, true);
290 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
292 free_irq(dev->irq, dev);
293 free_irq(priv->r_irq, dev);
294 free_irq(priv->t_irq, dev);
299 phy_disconnect(phydev);
305 /* This will only be invoked if your driver is _not_ in XOFF state.
306 * What this means is that you need not check it, and that this
307 * invariant will hold if you make sure that the netif_*_queue()
308 * calls are done at the proper times.
311 mpc52xx_fec_start_xmit(struct sk_buff *skb, struct net_device *dev)
313 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
314 struct bcom_fec_bd *bd;
317 if (bcom_queue_full(priv->tx_dmatsk)) {
319 dev_err(&dev->dev, "transmit queue overrun\n");
320 return NETDEV_TX_BUSY;
323 spin_lock_irqsave(&priv->lock, flags);
325 bd = (struct bcom_fec_bd *)
326 bcom_prepare_next_buffer(priv->tx_dmatsk);
328 bd->status = skb->len | BCOM_FEC_TX_BD_TFD | BCOM_FEC_TX_BD_TC;
329 bd->skb_pa = dma_map_single(dev->dev.parent, skb->data, skb->len,
332 skb_tx_timestamp(skb);
333 bcom_submit_next_buffer(priv->tx_dmatsk, skb);
334 spin_unlock_irqrestore(&priv->lock, flags);
336 if (bcom_queue_full(priv->tx_dmatsk)) {
337 netif_stop_queue(dev);
343 #ifdef CONFIG_NET_POLL_CONTROLLER
344 static void mpc52xx_fec_poll_controller(struct net_device *dev)
346 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
348 disable_irq(priv->t_irq);
349 mpc52xx_fec_tx_interrupt(priv->t_irq, dev);
350 enable_irq(priv->t_irq);
351 disable_irq(priv->r_irq);
352 mpc52xx_fec_rx_interrupt(priv->r_irq, dev);
353 enable_irq(priv->r_irq);
358 /* This handles BestComm transmit task interrupts
360 static irqreturn_t mpc52xx_fec_tx_interrupt(int irq, void *dev_id)
362 struct net_device *dev = dev_id;
363 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
365 spin_lock(&priv->lock);
366 while (bcom_buffer_done(priv->tx_dmatsk)) {
368 struct bcom_fec_bd *bd;
369 skb = bcom_retrieve_buffer(priv->tx_dmatsk, NULL,
370 (struct bcom_bd **)&bd);
371 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
374 dev_consume_skb_irq(skb);
376 spin_unlock(&priv->lock);
378 netif_wake_queue(dev);
383 static irqreturn_t mpc52xx_fec_rx_interrupt(int irq, void *dev_id)
385 struct net_device *dev = dev_id;
386 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
387 struct sk_buff *rskb; /* received sk_buff */
388 struct sk_buff *skb; /* new sk_buff to enqueue in its place */
389 struct bcom_fec_bd *bd;
390 u32 status, physaddr;
393 spin_lock(&priv->lock);
395 while (bcom_buffer_done(priv->rx_dmatsk)) {
397 rskb = bcom_retrieve_buffer(priv->rx_dmatsk, &status,
398 (struct bcom_bd **)&bd);
399 physaddr = bd->skb_pa;
401 /* Test for errors in received frame */
402 if (status & BCOM_FEC_RX_BD_ERRORS) {
403 /* Drop packet and reuse the buffer */
404 mpc52xx_fec_rx_submit(dev, rskb);
405 dev->stats.rx_dropped++;
409 /* skbs are allocated on open, so now we allocate a new one,
410 * and remove the old (with the packet) */
411 skb = netdev_alloc_skb(dev, FEC_RX_BUFFER_SIZE);
413 /* Can't get a new one : reuse the same & drop pkt */
414 dev_notice(&dev->dev, "Low memory - dropped packet.\n");
415 mpc52xx_fec_rx_submit(dev, rskb);
416 dev->stats.rx_dropped++;
420 /* Enqueue the new sk_buff back on the hardware */
421 mpc52xx_fec_rx_submit(dev, skb);
423 /* Process the received skb - Drop the spin lock while
424 * calling into the network stack */
425 spin_unlock(&priv->lock);
427 dma_unmap_single(dev->dev.parent, physaddr, rskb->len,
429 length = status & BCOM_FEC_RX_BD_LEN_MASK;
430 skb_put(rskb, length - 4); /* length without CRC32 */
431 rskb->protocol = eth_type_trans(rskb, dev);
432 if (!skb_defer_rx_timestamp(rskb))
435 spin_lock(&priv->lock);
438 spin_unlock(&priv->lock);
443 static irqreturn_t mpc52xx_fec_interrupt(int irq, void *dev_id)
445 struct net_device *dev = dev_id;
446 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
447 struct mpc52xx_fec __iomem *fec = priv->fec;
450 ievent = in_be32(&fec->ievent);
452 ievent &= ~FEC_IEVENT_MII; /* mii is handled separately */
456 out_be32(&fec->ievent, ievent); /* clear pending events */
458 /* on fifo error, soft-reset fec */
459 if (ievent & (FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {
461 if (net_ratelimit() && (ievent & FEC_IEVENT_RFIFO_ERROR))
462 dev_warn(&dev->dev, "FEC_IEVENT_RFIFO_ERROR\n");
463 if (net_ratelimit() && (ievent & FEC_IEVENT_XFIFO_ERROR))
464 dev_warn(&dev->dev, "FEC_IEVENT_XFIFO_ERROR\n");
466 spin_lock(&priv->lock);
467 mpc52xx_fec_reset(dev);
468 spin_unlock(&priv->lock);
473 if (ievent & ~FEC_IEVENT_TFINT)
474 dev_dbg(&dev->dev, "ievent: %08x\n", ievent);
480 * Get the current statistics.
481 * This may be called with the card open or closed.
483 static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev)
485 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
486 struct net_device_stats *stats = &dev->stats;
487 struct mpc52xx_fec __iomem *fec = priv->fec;
489 stats->rx_bytes = in_be32(&fec->rmon_r_octets);
490 stats->rx_packets = in_be32(&fec->rmon_r_packets);
491 stats->rx_errors = in_be32(&fec->rmon_r_crc_align) +
492 in_be32(&fec->rmon_r_undersize) +
493 in_be32(&fec->rmon_r_oversize) +
494 in_be32(&fec->rmon_r_frag) +
495 in_be32(&fec->rmon_r_jab);
497 stats->tx_bytes = in_be32(&fec->rmon_t_octets);
498 stats->tx_packets = in_be32(&fec->rmon_t_packets);
499 stats->tx_errors = in_be32(&fec->rmon_t_crc_align) +
500 in_be32(&fec->rmon_t_undersize) +
501 in_be32(&fec->rmon_t_oversize) +
502 in_be32(&fec->rmon_t_frag) +
503 in_be32(&fec->rmon_t_jab);
505 stats->multicast = in_be32(&fec->rmon_r_mc_pkt);
506 stats->collisions = in_be32(&fec->rmon_t_col);
508 /* detailed rx_errors: */
509 stats->rx_length_errors = in_be32(&fec->rmon_r_undersize)
510 + in_be32(&fec->rmon_r_oversize)
511 + in_be32(&fec->rmon_r_frag)
512 + in_be32(&fec->rmon_r_jab);
513 stats->rx_over_errors = in_be32(&fec->r_macerr);
514 stats->rx_crc_errors = in_be32(&fec->ieee_r_crc);
515 stats->rx_frame_errors = in_be32(&fec->ieee_r_align);
516 stats->rx_fifo_errors = in_be32(&fec->rmon_r_drop);
517 stats->rx_missed_errors = in_be32(&fec->rmon_r_drop);
519 /* detailed tx_errors: */
520 stats->tx_aborted_errors = 0;
521 stats->tx_carrier_errors = in_be32(&fec->ieee_t_cserr);
522 stats->tx_fifo_errors = in_be32(&fec->rmon_t_drop);
523 stats->tx_heartbeat_errors = in_be32(&fec->ieee_t_sqe);
524 stats->tx_window_errors = in_be32(&fec->ieee_t_lcol);
530 * Read MIB counters in order to reset them,
531 * then zero all the stats fields in memory
533 static void mpc52xx_fec_reset_stats(struct net_device *dev)
535 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
536 struct mpc52xx_fec __iomem *fec = priv->fec;
538 out_be32(&fec->mib_control, FEC_MIB_DISABLE);
539 memset_io(&fec->rmon_t_drop, 0,
540 offsetof(struct mpc52xx_fec, reserved10) -
541 offsetof(struct mpc52xx_fec, rmon_t_drop));
542 out_be32(&fec->mib_control, 0);
544 memset(&dev->stats, 0, sizeof(dev->stats));
548 * Set or clear the multicast filter for this adaptor.
550 static void mpc52xx_fec_set_multicast_list(struct net_device *dev)
552 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
553 struct mpc52xx_fec __iomem *fec = priv->fec;
556 rx_control = in_be32(&fec->r_cntrl);
558 if (dev->flags & IFF_PROMISC) {
559 rx_control |= FEC_RCNTRL_PROM;
560 out_be32(&fec->r_cntrl, rx_control);
562 rx_control &= ~FEC_RCNTRL_PROM;
563 out_be32(&fec->r_cntrl, rx_control);
565 if (dev->flags & IFF_ALLMULTI) {
566 out_be32(&fec->gaddr1, 0xffffffff);
567 out_be32(&fec->gaddr2, 0xffffffff);
570 struct netdev_hw_addr *ha;
571 u32 gaddr1 = 0x00000000;
572 u32 gaddr2 = 0x00000000;
574 netdev_for_each_mc_addr(ha, dev) {
575 crc = ether_crc_le(6, ha->addr) >> 26;
577 gaddr1 |= 1 << (crc-32);
581 out_be32(&fec->gaddr1, gaddr1);
582 out_be32(&fec->gaddr2, gaddr2);
588 * mpc52xx_fec_hw_init
589 * @dev: network device
591 * Setup various hardware setting, only needed once on start
593 static void mpc52xx_fec_hw_init(struct net_device *dev)
595 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
596 struct mpc52xx_fec __iomem *fec = priv->fec;
599 /* Whack a reset. We should wait for this. */
600 out_be32(&fec->ecntrl, FEC_ECNTRL_RESET);
601 for (i = 0; i < FEC_RESET_DELAY; ++i) {
602 if ((in_be32(&fec->ecntrl) & FEC_ECNTRL_RESET) == 0)
606 if (i == FEC_RESET_DELAY)
607 dev_err(&dev->dev, "FEC Reset timeout!\n");
609 /* set pause to 0x20 frames */
610 out_be32(&fec->op_pause, FEC_OP_PAUSE_OPCODE | 0x20);
612 /* high service request will be deasserted when there's < 7 bytes in fifo
613 * low service request will be deasserted when there's < 4*7 bytes in fifo
615 out_be32(&fec->rfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
616 out_be32(&fec->tfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
618 /* alarm when <= x bytes in FIFO */
619 out_be32(&fec->rfifo_alarm, 0x0000030c);
620 out_be32(&fec->tfifo_alarm, 0x00000100);
622 /* begin transmittion when 256 bytes are in FIFO (or EOF or FIFO full) */
623 out_be32(&fec->x_wmrk, FEC_FIFO_WMRK_256B);
625 /* enable crc generation */
626 out_be32(&fec->xmit_fsm, FEC_XMIT_FSM_APPEND_CRC | FEC_XMIT_FSM_ENABLE_CRC);
627 out_be32(&fec->iaddr1, 0x00000000); /* No individual filter */
628 out_be32(&fec->iaddr2, 0x00000000); /* No individual filter */
631 * this can't be done in phy driver, since it needs to be called
632 * before fec stuff (even on resume) */
633 out_be32(&fec->mii_speed, priv->mdio_speed);
638 * @dev: network device
640 * This function is called to start or restart the FEC during a link
641 * change. This happens on fifo errors or when switching between half
644 static void mpc52xx_fec_start(struct net_device *dev)
646 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
647 struct mpc52xx_fec __iomem *fec = priv->fec;
652 /* clear sticky error bits */
653 tmp = FEC_FIFO_STATUS_ERR | FEC_FIFO_STATUS_UF | FEC_FIFO_STATUS_OF;
654 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status) & tmp);
655 out_be32(&fec->tfifo_status, in_be32(&fec->tfifo_status) & tmp);
657 /* FIFOs will reset on mpc52xx_fec_enable */
658 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_ENABLE_IS_RESET);
660 /* Set station address. */
661 mpc52xx_fec_set_paddr(dev, dev->dev_addr);
663 mpc52xx_fec_set_multicast_list(dev);
665 /* set max frame len, enable flow control, select mii mode */
666 rcntrl = FEC_RX_BUFFER_SIZE << 16; /* max frame length */
667 rcntrl |= FEC_RCNTRL_FCE;
669 if (!priv->seven_wire_mode)
670 rcntrl |= FEC_RCNTRL_MII_MODE;
672 if (priv->duplex == DUPLEX_FULL)
673 tcntrl = FEC_TCNTRL_FDEN; /* FD enable */
675 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
678 out_be32(&fec->r_cntrl, rcntrl);
679 out_be32(&fec->x_cntrl, tcntrl);
681 /* Clear any outstanding interrupt. */
682 out_be32(&fec->ievent, 0xffffffff);
684 /* Enable interrupts we wish to service. */
685 out_be32(&fec->imask, FEC_IMASK_ENABLE);
687 /* And last, enable the transmit and receive processing. */
688 out_be32(&fec->ecntrl, FEC_ECNTRL_ETHER_EN);
689 out_be32(&fec->r_des_active, 0x01000000);
694 * @dev: network device
696 * stop all activity on fec and empty dma buffers
698 static void mpc52xx_fec_stop(struct net_device *dev, bool may_sleep)
700 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
701 struct mpc52xx_fec __iomem *fec = priv->fec;
702 unsigned long timeout;
704 /* disable all interrupts */
705 out_be32(&fec->imask, 0);
707 /* Disable the rx task. */
708 bcom_disable(priv->rx_dmatsk);
710 /* Wait for tx queue to drain, but only if we're in process context */
712 timeout = jiffies + msecs_to_jiffies(2000);
713 while (time_before(jiffies, timeout) &&
714 !bcom_queue_empty(priv->tx_dmatsk))
717 if (time_after_eq(jiffies, timeout))
718 dev_err(&dev->dev, "queues didn't drain\n");
720 if (time_after_eq(jiffies, timeout)) {
721 dev_err(&dev->dev, " tx: index: %i, outdex: %i\n",
722 priv->tx_dmatsk->index,
723 priv->tx_dmatsk->outdex);
724 dev_err(&dev->dev, " rx: index: %i, outdex: %i\n",
725 priv->rx_dmatsk->index,
726 priv->rx_dmatsk->outdex);
731 bcom_disable(priv->tx_dmatsk);
734 out_be32(&fec->ecntrl, in_be32(&fec->ecntrl) & ~FEC_ECNTRL_ETHER_EN);
737 /* reset fec and bestcomm tasks */
738 static void mpc52xx_fec_reset(struct net_device *dev)
740 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
741 struct mpc52xx_fec __iomem *fec = priv->fec;
743 mpc52xx_fec_stop(dev, false);
745 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status));
746 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_RESET_FIFO);
748 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
750 mpc52xx_fec_hw_init(dev);
752 bcom_fec_rx_reset(priv->rx_dmatsk);
753 bcom_fec_tx_reset(priv->tx_dmatsk);
755 mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
757 bcom_enable(priv->rx_dmatsk);
758 bcom_enable(priv->tx_dmatsk);
760 mpc52xx_fec_start(dev);
762 netif_wake_queue(dev);
766 /* ethtool interface */
768 static u32 mpc52xx_fec_get_msglevel(struct net_device *dev)
770 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
771 return priv->msg_enable;
774 static void mpc52xx_fec_set_msglevel(struct net_device *dev, u32 level)
776 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
777 priv->msg_enable = level;
780 static const struct ethtool_ops mpc52xx_fec_ethtool_ops = {
781 .get_link = ethtool_op_get_link,
782 .get_msglevel = mpc52xx_fec_get_msglevel,
783 .set_msglevel = mpc52xx_fec_set_msglevel,
784 .get_ts_info = ethtool_op_get_ts_info,
785 .get_link_ksettings = phy_ethtool_get_link_ksettings,
786 .set_link_ksettings = phy_ethtool_set_link_ksettings,
790 static const struct net_device_ops mpc52xx_fec_netdev_ops = {
791 .ndo_open = mpc52xx_fec_open,
792 .ndo_stop = mpc52xx_fec_close,
793 .ndo_start_xmit = mpc52xx_fec_start_xmit,
794 .ndo_set_rx_mode = mpc52xx_fec_set_multicast_list,
795 .ndo_set_mac_address = mpc52xx_fec_set_mac_address,
796 .ndo_validate_addr = eth_validate_addr,
797 .ndo_eth_ioctl = phy_do_ioctl,
798 .ndo_tx_timeout = mpc52xx_fec_tx_timeout,
799 .ndo_get_stats = mpc52xx_fec_get_stats,
800 #ifdef CONFIG_NET_POLL_CONTROLLER
801 .ndo_poll_controller = mpc52xx_fec_poll_controller,
805 /* ======================================================================== */
807 /* ======================================================================== */
809 static int mpc52xx_fec_probe(struct platform_device *op)
812 struct net_device *ndev;
813 struct mpc52xx_fec_priv *priv = NULL;
817 struct device_node *np = op->dev.of_node;
822 /* Get the ether ndev & it's private zone */
823 ndev = alloc_etherdev(sizeof(struct mpc52xx_fec_priv));
827 priv = netdev_priv(ndev);
830 /* Reserve FEC control zone */
831 rv = of_address_to_resource(np, 0, &mem);
833 pr_err("Error while parsing device node resource\n");
836 if (resource_size(&mem) < sizeof(struct mpc52xx_fec)) {
837 pr_err("invalid resource size (%lx < %x), check mpc52xx_devices.c\n",
838 (unsigned long)resource_size(&mem),
839 sizeof(struct mpc52xx_fec));
844 if (!request_mem_region(mem.start, sizeof(struct mpc52xx_fec),
850 /* Init ether ndev with what we have */
851 ndev->netdev_ops = &mpc52xx_fec_netdev_ops;
852 ndev->ethtool_ops = &mpc52xx_fec_ethtool_ops;
853 ndev->watchdog_timeo = FEC_WATCHDOG_TIMEOUT;
854 ndev->base_addr = mem.start;
855 SET_NETDEV_DEV(ndev, &op->dev);
857 spin_lock_init(&priv->lock);
859 /* ioremap the zones */
860 priv->fec = ioremap(mem.start, sizeof(struct mpc52xx_fec));
868 rx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, rfifo_data);
869 tx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, tfifo_data);
871 priv->rx_dmatsk = bcom_fec_rx_init(FEC_RX_NUM_BD, rx_fifo, FEC_RX_BUFFER_SIZE);
872 priv->tx_dmatsk = bcom_fec_tx_init(FEC_TX_NUM_BD, tx_fifo);
874 if (!priv->rx_dmatsk || !priv->tx_dmatsk) {
875 pr_err("Can not init SDMA tasks\n");
877 goto err_rx_tx_dmatsk;
880 /* Get the IRQ we need one by one */
882 ndev->irq = irq_of_parse_and_map(np, 0);
885 priv->r_irq = bcom_get_task_irq(priv->rx_dmatsk);
888 priv->t_irq = bcom_get_task_irq(priv->tx_dmatsk);
893 * First try to read MAC address from DT
895 rv = of_get_ethdev_address(np, ndev);
897 struct mpc52xx_fec __iomem *fec = priv->fec;
898 u8 addr[ETH_ALEN] __aligned(4);
901 * If the MAC addresse is not provided via DT then read
902 * it back from the controller regs
904 *(u32 *)(&addr[0]) = in_be32(&fec->paddr1);
905 *(u16 *)(&addr[4]) = in_be32(&fec->paddr2) >> 16;
906 eth_hw_addr_set(ndev, addr);
910 * Check if the MAC address is valid, if not get a random one
912 if (!is_valid_ether_addr(ndev->dev_addr)) {
913 eth_hw_addr_random(ndev);
914 dev_warn(&ndev->dev, "using random MAC address %pM\n",
918 priv->msg_enable = netif_msg_init(debug, MPC52xx_MESSAGES_DEFAULT);
921 * Link mode configuration
924 /* Start with safe defaults for link connection */
926 priv->duplex = DUPLEX_HALF;
927 priv->mdio_speed = ((mpc5xxx_get_bus_frequency(&op->dev) >> 20) / 5) << 1;
929 /* The current speed preconfigures the speed of the MII link */
930 prop = of_get_property(np, "current-speed", &prop_size);
931 if (prop && (prop_size >= sizeof(u32) * 2)) {
932 priv->speed = prop[0];
933 priv->duplex = prop[1] ? DUPLEX_FULL : DUPLEX_HALF;
936 /* If there is a phy handle, then get the PHY node */
937 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
939 /* the 7-wire property means don't use MII mode */
940 if (of_property_read_bool(np, "fsl,7-wire-mode")) {
941 priv->seven_wire_mode = 1;
942 dev_info(&ndev->dev, "using 7-wire PHY mode\n");
946 mpc52xx_fec_hw_init(ndev);
947 mpc52xx_fec_reset_stats(ndev);
949 rv = register_netdev(ndev);
954 platform_set_drvdata(op, ndev);
955 netdev_info(ndev, "%pOF MAC %pM\n",
956 op->dev.of_node, ndev->dev_addr);
961 of_node_put(priv->phy_node);
962 irq_dispose_mapping(ndev->irq);
965 bcom_fec_rx_release(priv->rx_dmatsk);
967 bcom_fec_tx_release(priv->tx_dmatsk);
970 release_mem_region(mem.start, sizeof(struct mpc52xx_fec));
978 mpc52xx_fec_remove(struct platform_device *op)
980 struct net_device *ndev;
981 struct mpc52xx_fec_priv *priv;
983 ndev = platform_get_drvdata(op);
984 priv = netdev_priv(ndev);
986 unregister_netdev(ndev);
988 of_node_put(priv->phy_node);
989 priv->phy_node = NULL;
991 irq_dispose_mapping(ndev->irq);
993 bcom_fec_rx_release(priv->rx_dmatsk);
994 bcom_fec_tx_release(priv->tx_dmatsk);
998 release_mem_region(ndev->base_addr, sizeof(struct mpc52xx_fec));
1004 static int mpc52xx_fec_of_suspend(struct platform_device *op, pm_message_t state)
1006 struct net_device *dev = platform_get_drvdata(op);
1008 if (netif_running(dev))
1009 mpc52xx_fec_close(dev);
1014 static int mpc52xx_fec_of_resume(struct platform_device *op)
1016 struct net_device *dev = platform_get_drvdata(op);
1018 mpc52xx_fec_hw_init(dev);
1019 mpc52xx_fec_reset_stats(dev);
1021 if (netif_running(dev))
1022 mpc52xx_fec_open(dev);
1028 static const struct of_device_id mpc52xx_fec_match[] = {
1029 { .compatible = "fsl,mpc5200b-fec", },
1030 { .compatible = "fsl,mpc5200-fec", },
1031 { .compatible = "mpc5200-fec", },
1035 MODULE_DEVICE_TABLE(of, mpc52xx_fec_match);
1037 static struct platform_driver mpc52xx_fec_driver = {
1039 .name = DRIVER_NAME,
1040 .of_match_table = mpc52xx_fec_match,
1042 .probe = mpc52xx_fec_probe,
1043 .remove = mpc52xx_fec_remove,
1045 .suspend = mpc52xx_fec_of_suspend,
1046 .resume = mpc52xx_fec_of_resume,
1051 /* ======================================================================== */
1053 /* ======================================================================== */
1055 static struct platform_driver * const drivers[] = {
1056 #ifdef CONFIG_FEC_MPC52xx_MDIO
1057 &mpc52xx_fec_mdio_driver,
1059 &mpc52xx_fec_driver,
1063 mpc52xx_fec_init(void)
1065 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
1069 mpc52xx_fec_exit(void)
1071 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
1075 module_init(mpc52xx_fec_init);
1076 module_exit(mpc52xx_fec_exit);
1078 MODULE_LICENSE("GPL");
1079 MODULE_AUTHOR("Dale Farnsworth");
1080 MODULE_DESCRIPTION("Ethernet driver for the Freescale MPC52xx FEC");