powerpc/ftrace: Use pr_devel() in ftrace.c
[linux-2.6/cjktty.git] / drivers / net / fec_mpc52xx.c
blob8bbe7f6179949c30b167d82cb2236394af9332c4
1 /*
2 * Driver for the MPC5200 Fast Ethernet Controller
4 * Originally written by Dale Farnsworth <dfarnsworth@mvista.com> and
5 * now maintained by Sylvain Munaut <tnt@246tNt.com>
7 * Copyright (C) 2007 Domen Puncer, Telargo, Inc.
8 * Copyright (C) 2007 Sylvain Munaut <tnt@246tNt.com>
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 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/spinlock.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/crc32.h>
25 #include <linux/hardirq.h>
26 #include <linux/delay.h>
27 #include <linux/of_device.h>
28 #include <linux/of_platform.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/ethtool.h>
33 #include <linux/skbuff.h>
35 #include <asm/io.h>
36 #include <asm/delay.h>
37 #include <asm/mpc52xx.h>
39 #include <sysdev/bestcomm/bestcomm.h>
40 #include <sysdev/bestcomm/fec.h>
42 #include "fec_mpc52xx.h"
44 #define DRIVER_NAME "mpc52xx-fec"
46 #define FEC5200_PHYADDR_NONE (-1)
47 #define FEC5200_PHYADDR_7WIRE (-2)
49 /* Private driver data structure */
50 struct mpc52xx_fec_priv {
51 int duplex;
52 int speed;
53 int r_irq;
54 int t_irq;
55 struct mpc52xx_fec __iomem *fec;
56 struct bcom_task *rx_dmatsk;
57 struct bcom_task *tx_dmatsk;
58 spinlock_t lock;
59 int msg_enable;
61 /* MDIO link details */
62 int phy_addr;
63 unsigned int phy_speed;
64 struct phy_device *phydev;
65 enum phy_state link;
69 static irqreturn_t mpc52xx_fec_interrupt(int, void *);
70 static irqreturn_t mpc52xx_fec_rx_interrupt(int, void *);
71 static irqreturn_t mpc52xx_fec_tx_interrupt(int, void *);
72 static void mpc52xx_fec_stop(struct net_device *dev);
73 static void mpc52xx_fec_start(struct net_device *dev);
74 static void mpc52xx_fec_reset(struct net_device *dev);
76 static u8 mpc52xx_fec_mac_addr[6];
77 module_param_array_named(mac, mpc52xx_fec_mac_addr, byte, NULL, 0);
78 MODULE_PARM_DESC(mac, "six hex digits, ie. 0x1,0x2,0xc0,0x01,0xba,0xbe");
80 #define MPC52xx_MESSAGES_DEFAULT ( NETIF_MSG_DRV | NETIF_MSG_PROBE | \
81 NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
82 static int debug = -1; /* the above default */
83 module_param(debug, int, 0);
84 MODULE_PARM_DESC(debug, "debugging messages level");
86 static void mpc52xx_fec_tx_timeout(struct net_device *dev)
88 dev_warn(&dev->dev, "transmit timed out\n");
90 mpc52xx_fec_reset(dev);
92 dev->stats.tx_errors++;
94 netif_wake_queue(dev);
97 static void mpc52xx_fec_set_paddr(struct net_device *dev, u8 *mac)
99 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
100 struct mpc52xx_fec __iomem *fec = priv->fec;
102 out_be32(&fec->paddr1, *(u32 *)(&mac[0]));
103 out_be32(&fec->paddr2, (*(u16 *)(&mac[4]) << 16) | FEC_PADDR2_TYPE);
106 static void mpc52xx_fec_get_paddr(struct net_device *dev, u8 *mac)
108 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
109 struct mpc52xx_fec __iomem *fec = priv->fec;
111 *(u32 *)(&mac[0]) = in_be32(&fec->paddr1);
112 *(u16 *)(&mac[4]) = in_be32(&fec->paddr2) >> 16;
115 static int mpc52xx_fec_set_mac_address(struct net_device *dev, void *addr)
117 struct sockaddr *sock = addr;
119 memcpy(dev->dev_addr, sock->sa_data, dev->addr_len);
121 mpc52xx_fec_set_paddr(dev, sock->sa_data);
122 return 0;
125 static void mpc52xx_fec_free_rx_buffers(struct net_device *dev, struct bcom_task *s)
127 while (!bcom_queue_empty(s)) {
128 struct bcom_fec_bd *bd;
129 struct sk_buff *skb;
131 skb = bcom_retrieve_buffer(s, NULL, (struct bcom_bd **)&bd);
132 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
133 DMA_FROM_DEVICE);
134 kfree_skb(skb);
138 static int mpc52xx_fec_alloc_rx_buffers(struct net_device *dev, struct bcom_task *rxtsk)
140 while (!bcom_queue_full(rxtsk)) {
141 struct sk_buff *skb;
142 struct bcom_fec_bd *bd;
144 skb = dev_alloc_skb(FEC_RX_BUFFER_SIZE);
145 if (skb == NULL)
146 return -EAGAIN;
148 /* zero out the initial receive buffers to aid debugging */
149 memset(skb->data, 0, FEC_RX_BUFFER_SIZE);
151 bd = (struct bcom_fec_bd *)bcom_prepare_next_buffer(rxtsk);
153 bd->status = FEC_RX_BUFFER_SIZE;
154 bd->skb_pa = dma_map_single(dev->dev.parent, skb->data,
155 FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
157 bcom_submit_next_buffer(rxtsk, skb);
160 return 0;
163 /* based on generic_adjust_link from fs_enet-main.c */
164 static void mpc52xx_fec_adjust_link(struct net_device *dev)
166 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
167 struct phy_device *phydev = priv->phydev;
168 int new_state = 0;
170 if (phydev->link != PHY_DOWN) {
171 if (phydev->duplex != priv->duplex) {
172 struct mpc52xx_fec __iomem *fec = priv->fec;
173 u32 rcntrl;
174 u32 tcntrl;
176 new_state = 1;
177 priv->duplex = phydev->duplex;
179 rcntrl = in_be32(&fec->r_cntrl);
180 tcntrl = in_be32(&fec->x_cntrl);
182 rcntrl &= ~FEC_RCNTRL_DRT;
183 tcntrl &= ~FEC_TCNTRL_FDEN;
184 if (phydev->duplex == DUPLEX_FULL)
185 tcntrl |= FEC_TCNTRL_FDEN; /* FD enable */
186 else
187 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
189 out_be32(&fec->r_cntrl, rcntrl);
190 out_be32(&fec->x_cntrl, tcntrl);
193 if (phydev->speed != priv->speed) {
194 new_state = 1;
195 priv->speed = phydev->speed;
198 if (priv->link == PHY_DOWN) {
199 new_state = 1;
200 priv->link = phydev->link;
203 } else if (priv->link) {
204 new_state = 1;
205 priv->link = PHY_DOWN;
206 priv->speed = 0;
207 priv->duplex = -1;
210 if (new_state && netif_msg_link(priv))
211 phy_print_status(phydev);
214 static int mpc52xx_fec_init_phy(struct net_device *dev)
216 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
217 struct phy_device *phydev;
218 char phy_id[BUS_ID_SIZE];
220 snprintf(phy_id, sizeof(phy_id), "%x:%02x",
221 (unsigned int)dev->base_addr, priv->phy_addr);
223 priv->link = PHY_DOWN;
224 priv->speed = 0;
225 priv->duplex = -1;
227 phydev = phy_connect(dev, phy_id, &mpc52xx_fec_adjust_link, 0, PHY_INTERFACE_MODE_MII);
228 if (IS_ERR(phydev)) {
229 dev_err(&dev->dev, "phy_connect failed\n");
230 return PTR_ERR(phydev);
232 dev_info(&dev->dev, "attached phy %i to driver %s\n",
233 phydev->addr, phydev->drv->name);
235 priv->phydev = phydev;
237 return 0;
240 static int mpc52xx_fec_phy_start(struct net_device *dev)
242 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
243 int err;
245 if (priv->phy_addr < 0)
246 return 0;
248 err = mpc52xx_fec_init_phy(dev);
249 if (err) {
250 dev_err(&dev->dev, "mpc52xx_fec_init_phy failed\n");
251 return err;
254 /* reset phy - this also wakes it from PDOWN */
255 phy_write(priv->phydev, MII_BMCR, BMCR_RESET);
256 phy_start(priv->phydev);
258 return 0;
261 static void mpc52xx_fec_phy_stop(struct net_device *dev)
263 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
265 if (!priv->phydev)
266 return;
268 phy_disconnect(priv->phydev);
269 /* power down phy */
270 phy_stop(priv->phydev);
271 phy_write(priv->phydev, MII_BMCR, BMCR_PDOWN);
274 static void mpc52xx_fec_phy_hw_init(struct mpc52xx_fec_priv *priv)
276 struct mpc52xx_fec __iomem *fec = priv->fec;
278 if (priv->phydev)
279 return;
281 out_be32(&fec->mii_speed, priv->phy_speed);
284 static int mpc52xx_fec_open(struct net_device *dev)
286 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
287 int err = -EBUSY;
289 if (request_irq(dev->irq, &mpc52xx_fec_interrupt, IRQF_SHARED,
290 DRIVER_NAME "_ctrl", dev)) {
291 dev_err(&dev->dev, "ctrl interrupt request failed\n");
292 goto out;
294 if (request_irq(priv->r_irq, &mpc52xx_fec_rx_interrupt, 0,
295 DRIVER_NAME "_rx", dev)) {
296 dev_err(&dev->dev, "rx interrupt request failed\n");
297 goto free_ctrl_irq;
299 if (request_irq(priv->t_irq, &mpc52xx_fec_tx_interrupt, 0,
300 DRIVER_NAME "_tx", dev)) {
301 dev_err(&dev->dev, "tx interrupt request failed\n");
302 goto free_2irqs;
305 bcom_fec_rx_reset(priv->rx_dmatsk);
306 bcom_fec_tx_reset(priv->tx_dmatsk);
308 err = mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
309 if (err) {
310 dev_err(&dev->dev, "mpc52xx_fec_alloc_rx_buffers failed\n");
311 goto free_irqs;
314 err = mpc52xx_fec_phy_start(dev);
315 if (err)
316 goto free_skbs;
318 bcom_enable(priv->rx_dmatsk);
319 bcom_enable(priv->tx_dmatsk);
321 mpc52xx_fec_start(dev);
323 netif_start_queue(dev);
325 return 0;
327 free_skbs:
328 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
330 free_irqs:
331 free_irq(priv->t_irq, dev);
332 free_2irqs:
333 free_irq(priv->r_irq, dev);
334 free_ctrl_irq:
335 free_irq(dev->irq, dev);
336 out:
338 return err;
341 static int mpc52xx_fec_close(struct net_device *dev)
343 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
345 netif_stop_queue(dev);
347 mpc52xx_fec_stop(dev);
349 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
351 free_irq(dev->irq, dev);
352 free_irq(priv->r_irq, dev);
353 free_irq(priv->t_irq, dev);
355 mpc52xx_fec_phy_stop(dev);
357 return 0;
360 /* This will only be invoked if your driver is _not_ in XOFF state.
361 * What this means is that you need not check it, and that this
362 * invariant will hold if you make sure that the netif_*_queue()
363 * calls are done at the proper times.
365 static int mpc52xx_fec_start_xmit(struct sk_buff *skb, struct net_device *dev)
367 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
368 struct bcom_fec_bd *bd;
370 if (bcom_queue_full(priv->tx_dmatsk)) {
371 if (net_ratelimit())
372 dev_err(&dev->dev, "transmit queue overrun\n");
373 return NETDEV_TX_BUSY;
376 spin_lock_irq(&priv->lock);
377 dev->trans_start = jiffies;
379 bd = (struct bcom_fec_bd *)
380 bcom_prepare_next_buffer(priv->tx_dmatsk);
382 bd->status = skb->len | BCOM_FEC_TX_BD_TFD | BCOM_FEC_TX_BD_TC;
383 bd->skb_pa = dma_map_single(dev->dev.parent, skb->data, skb->len,
384 DMA_TO_DEVICE);
386 bcom_submit_next_buffer(priv->tx_dmatsk, skb);
388 if (bcom_queue_full(priv->tx_dmatsk)) {
389 netif_stop_queue(dev);
392 spin_unlock_irq(&priv->lock);
394 return NETDEV_TX_OK;
397 #ifdef CONFIG_NET_POLL_CONTROLLER
398 static void mpc52xx_fec_poll_controller(struct net_device *dev)
400 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
402 disable_irq(priv->t_irq);
403 mpc52xx_fec_tx_interrupt(priv->t_irq, dev);
404 enable_irq(priv->t_irq);
405 disable_irq(priv->r_irq);
406 mpc52xx_fec_rx_interrupt(priv->r_irq, dev);
407 enable_irq(priv->r_irq);
409 #endif
412 /* This handles BestComm transmit task interrupts
414 static irqreturn_t mpc52xx_fec_tx_interrupt(int irq, void *dev_id)
416 struct net_device *dev = dev_id;
417 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
419 spin_lock(&priv->lock);
421 while (bcom_buffer_done(priv->tx_dmatsk)) {
422 struct sk_buff *skb;
423 struct bcom_fec_bd *bd;
424 skb = bcom_retrieve_buffer(priv->tx_dmatsk, NULL,
425 (struct bcom_bd **)&bd);
426 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
427 DMA_TO_DEVICE);
429 dev_kfree_skb_irq(skb);
432 netif_wake_queue(dev);
434 spin_unlock(&priv->lock);
436 return IRQ_HANDLED;
439 static irqreturn_t mpc52xx_fec_rx_interrupt(int irq, void *dev_id)
441 struct net_device *dev = dev_id;
442 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
444 while (bcom_buffer_done(priv->rx_dmatsk)) {
445 struct sk_buff *skb;
446 struct sk_buff *rskb;
447 struct bcom_fec_bd *bd;
448 u32 status;
450 rskb = bcom_retrieve_buffer(priv->rx_dmatsk, &status,
451 (struct bcom_bd **)&bd);
452 dma_unmap_single(dev->dev.parent, bd->skb_pa, rskb->len,
453 DMA_FROM_DEVICE);
455 /* Test for errors in received frame */
456 if (status & BCOM_FEC_RX_BD_ERRORS) {
457 /* Drop packet and reuse the buffer */
458 bd = (struct bcom_fec_bd *)
459 bcom_prepare_next_buffer(priv->rx_dmatsk);
461 bd->status = FEC_RX_BUFFER_SIZE;
462 bd->skb_pa = dma_map_single(dev->dev.parent,
463 rskb->data,
464 FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
466 bcom_submit_next_buffer(priv->rx_dmatsk, rskb);
468 dev->stats.rx_dropped++;
470 continue;
473 /* skbs are allocated on open, so now we allocate a new one,
474 * and remove the old (with the packet) */
475 skb = dev_alloc_skb(FEC_RX_BUFFER_SIZE);
476 if (skb) {
477 /* Process the received skb */
478 int length = status & BCOM_FEC_RX_BD_LEN_MASK;
480 skb_put(rskb, length - 4); /* length without CRC32 */
482 rskb->dev = dev;
483 rskb->protocol = eth_type_trans(rskb, dev);
485 netif_rx(rskb);
486 } else {
487 /* Can't get a new one : reuse the same & drop pkt */
488 dev_notice(&dev->dev, "Memory squeeze, dropping packet.\n");
489 dev->stats.rx_dropped++;
491 skb = rskb;
494 bd = (struct bcom_fec_bd *)
495 bcom_prepare_next_buffer(priv->rx_dmatsk);
497 bd->status = FEC_RX_BUFFER_SIZE;
498 bd->skb_pa = dma_map_single(dev->dev.parent, skb->data,
499 FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
501 bcom_submit_next_buffer(priv->rx_dmatsk, skb);
504 return IRQ_HANDLED;
507 static irqreturn_t mpc52xx_fec_interrupt(int irq, void *dev_id)
509 struct net_device *dev = dev_id;
510 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
511 struct mpc52xx_fec __iomem *fec = priv->fec;
512 u32 ievent;
514 ievent = in_be32(&fec->ievent);
516 ievent &= ~FEC_IEVENT_MII; /* mii is handled separately */
517 if (!ievent)
518 return IRQ_NONE;
520 out_be32(&fec->ievent, ievent); /* clear pending events */
522 /* on fifo error, soft-reset fec */
523 if (ievent & (FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {
525 if (net_ratelimit() && (ievent & FEC_IEVENT_RFIFO_ERROR))
526 dev_warn(&dev->dev, "FEC_IEVENT_RFIFO_ERROR\n");
527 if (net_ratelimit() && (ievent & FEC_IEVENT_XFIFO_ERROR))
528 dev_warn(&dev->dev, "FEC_IEVENT_XFIFO_ERROR\n");
530 mpc52xx_fec_reset(dev);
532 netif_wake_queue(dev);
533 return IRQ_HANDLED;
536 if (ievent & ~FEC_IEVENT_TFINT)
537 dev_dbg(&dev->dev, "ievent: %08x\n", ievent);
539 return IRQ_HANDLED;
543 * Get the current statistics.
544 * This may be called with the card open or closed.
546 static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev)
548 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
549 struct net_device_stats *stats = &dev->stats;
550 struct mpc52xx_fec __iomem *fec = priv->fec;
552 stats->rx_bytes = in_be32(&fec->rmon_r_octets);
553 stats->rx_packets = in_be32(&fec->rmon_r_packets);
554 stats->rx_errors = in_be32(&fec->rmon_r_crc_align) +
555 in_be32(&fec->rmon_r_undersize) +
556 in_be32(&fec->rmon_r_oversize) +
557 in_be32(&fec->rmon_r_frag) +
558 in_be32(&fec->rmon_r_jab);
560 stats->tx_bytes = in_be32(&fec->rmon_t_octets);
561 stats->tx_packets = in_be32(&fec->rmon_t_packets);
562 stats->tx_errors = in_be32(&fec->rmon_t_crc_align) +
563 in_be32(&fec->rmon_t_undersize) +
564 in_be32(&fec->rmon_t_oversize) +
565 in_be32(&fec->rmon_t_frag) +
566 in_be32(&fec->rmon_t_jab);
568 stats->multicast = in_be32(&fec->rmon_r_mc_pkt);
569 stats->collisions = in_be32(&fec->rmon_t_col);
571 /* detailed rx_errors: */
572 stats->rx_length_errors = in_be32(&fec->rmon_r_undersize)
573 + in_be32(&fec->rmon_r_oversize)
574 + in_be32(&fec->rmon_r_frag)
575 + in_be32(&fec->rmon_r_jab);
576 stats->rx_over_errors = in_be32(&fec->r_macerr);
577 stats->rx_crc_errors = in_be32(&fec->ieee_r_crc);
578 stats->rx_frame_errors = in_be32(&fec->ieee_r_align);
579 stats->rx_fifo_errors = in_be32(&fec->rmon_r_drop);
580 stats->rx_missed_errors = in_be32(&fec->rmon_r_drop);
582 /* detailed tx_errors: */
583 stats->tx_aborted_errors = 0;
584 stats->tx_carrier_errors = in_be32(&fec->ieee_t_cserr);
585 stats->tx_fifo_errors = in_be32(&fec->rmon_t_drop);
586 stats->tx_heartbeat_errors = in_be32(&fec->ieee_t_sqe);
587 stats->tx_window_errors = in_be32(&fec->ieee_t_lcol);
589 return stats;
593 * Read MIB counters in order to reset them,
594 * then zero all the stats fields in memory
596 static void mpc52xx_fec_reset_stats(struct net_device *dev)
598 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
599 struct mpc52xx_fec __iomem *fec = priv->fec;
601 out_be32(&fec->mib_control, FEC_MIB_DISABLE);
602 memset_io(&fec->rmon_t_drop, 0,
603 offsetof(struct mpc52xx_fec, reserved10) -
604 offsetof(struct mpc52xx_fec, rmon_t_drop));
605 out_be32(&fec->mib_control, 0);
607 memset(&dev->stats, 0, sizeof(dev->stats));
611 * Set or clear the multicast filter for this adaptor.
613 static void mpc52xx_fec_set_multicast_list(struct net_device *dev)
615 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
616 struct mpc52xx_fec __iomem *fec = priv->fec;
617 u32 rx_control;
619 rx_control = in_be32(&fec->r_cntrl);
621 if (dev->flags & IFF_PROMISC) {
622 rx_control |= FEC_RCNTRL_PROM;
623 out_be32(&fec->r_cntrl, rx_control);
624 } else {
625 rx_control &= ~FEC_RCNTRL_PROM;
626 out_be32(&fec->r_cntrl, rx_control);
628 if (dev->flags & IFF_ALLMULTI) {
629 out_be32(&fec->gaddr1, 0xffffffff);
630 out_be32(&fec->gaddr2, 0xffffffff);
631 } else {
632 u32 crc;
633 int i;
634 struct dev_mc_list *dmi;
635 u32 gaddr1 = 0x00000000;
636 u32 gaddr2 = 0x00000000;
638 dmi = dev->mc_list;
639 for (i=0; i<dev->mc_count; i++) {
640 crc = ether_crc_le(6, dmi->dmi_addr) >> 26;
641 if (crc >= 32)
642 gaddr1 |= 1 << (crc-32);
643 else
644 gaddr2 |= 1 << crc;
645 dmi = dmi->next;
647 out_be32(&fec->gaddr1, gaddr1);
648 out_be32(&fec->gaddr2, gaddr2);
654 * mpc52xx_fec_hw_init
655 * @dev: network device
657 * Setup various hardware setting, only needed once on start
659 static void mpc52xx_fec_hw_init(struct net_device *dev)
661 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
662 struct mpc52xx_fec __iomem *fec = priv->fec;
663 int i;
665 /* Whack a reset. We should wait for this. */
666 out_be32(&fec->ecntrl, FEC_ECNTRL_RESET);
667 for (i = 0; i < FEC_RESET_DELAY; ++i) {
668 if ((in_be32(&fec->ecntrl) & FEC_ECNTRL_RESET) == 0)
669 break;
670 udelay(1);
672 if (i == FEC_RESET_DELAY)
673 dev_err(&dev->dev, "FEC Reset timeout!\n");
675 /* set pause to 0x20 frames */
676 out_be32(&fec->op_pause, FEC_OP_PAUSE_OPCODE | 0x20);
678 /* high service request will be deasserted when there's < 7 bytes in fifo
679 * low service request will be deasserted when there's < 4*7 bytes in fifo
681 out_be32(&fec->rfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
682 out_be32(&fec->tfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
684 /* alarm when <= x bytes in FIFO */
685 out_be32(&fec->rfifo_alarm, 0x0000030c);
686 out_be32(&fec->tfifo_alarm, 0x00000100);
688 /* begin transmittion when 256 bytes are in FIFO (or EOF or FIFO full) */
689 out_be32(&fec->x_wmrk, FEC_FIFO_WMRK_256B);
691 /* enable crc generation */
692 out_be32(&fec->xmit_fsm, FEC_XMIT_FSM_APPEND_CRC | FEC_XMIT_FSM_ENABLE_CRC);
693 out_be32(&fec->iaddr1, 0x00000000); /* No individual filter */
694 out_be32(&fec->iaddr2, 0x00000000); /* No individual filter */
696 /* set phy speed.
697 * this can't be done in phy driver, since it needs to be called
698 * before fec stuff (even on resume) */
699 mpc52xx_fec_phy_hw_init(priv);
703 * mpc52xx_fec_start
704 * @dev: network device
706 * This function is called to start or restart the FEC during a link
707 * change. This happens on fifo errors or when switching between half
708 * and full duplex.
710 static void mpc52xx_fec_start(struct net_device *dev)
712 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
713 struct mpc52xx_fec __iomem *fec = priv->fec;
714 u32 rcntrl;
715 u32 tcntrl;
716 u32 tmp;
718 /* clear sticky error bits */
719 tmp = FEC_FIFO_STATUS_ERR | FEC_FIFO_STATUS_UF | FEC_FIFO_STATUS_OF;
720 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status) & tmp);
721 out_be32(&fec->tfifo_status, in_be32(&fec->tfifo_status) & tmp);
723 /* FIFOs will reset on mpc52xx_fec_enable */
724 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_ENABLE_IS_RESET);
726 /* Set station address. */
727 mpc52xx_fec_set_paddr(dev, dev->dev_addr);
729 mpc52xx_fec_set_multicast_list(dev);
731 /* set max frame len, enable flow control, select mii mode */
732 rcntrl = FEC_RX_BUFFER_SIZE << 16; /* max frame length */
733 rcntrl |= FEC_RCNTRL_FCE;
735 if (priv->phy_addr != FEC5200_PHYADDR_7WIRE)
736 rcntrl |= FEC_RCNTRL_MII_MODE;
738 if (priv->duplex == DUPLEX_FULL)
739 tcntrl = FEC_TCNTRL_FDEN; /* FD enable */
740 else {
741 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
742 tcntrl = 0;
744 out_be32(&fec->r_cntrl, rcntrl);
745 out_be32(&fec->x_cntrl, tcntrl);
747 /* Clear any outstanding interrupt. */
748 out_be32(&fec->ievent, 0xffffffff);
750 /* Enable interrupts we wish to service. */
751 out_be32(&fec->imask, FEC_IMASK_ENABLE);
753 /* And last, enable the transmit and receive processing. */
754 out_be32(&fec->ecntrl, FEC_ECNTRL_ETHER_EN);
755 out_be32(&fec->r_des_active, 0x01000000);
759 * mpc52xx_fec_stop
760 * @dev: network device
762 * stop all activity on fec and empty dma buffers
764 static void mpc52xx_fec_stop(struct net_device *dev)
766 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
767 struct mpc52xx_fec __iomem *fec = priv->fec;
768 unsigned long timeout;
770 /* disable all interrupts */
771 out_be32(&fec->imask, 0);
773 /* Disable the rx task. */
774 bcom_disable(priv->rx_dmatsk);
776 /* Wait for tx queue to drain, but only if we're in process context */
777 if (!in_interrupt()) {
778 timeout = jiffies + msecs_to_jiffies(2000);
779 while (time_before(jiffies, timeout) &&
780 !bcom_queue_empty(priv->tx_dmatsk))
781 msleep(100);
783 if (time_after_eq(jiffies, timeout))
784 dev_err(&dev->dev, "queues didn't drain\n");
785 #if 1
786 if (time_after_eq(jiffies, timeout)) {
787 dev_err(&dev->dev, " tx: index: %i, outdex: %i\n",
788 priv->tx_dmatsk->index,
789 priv->tx_dmatsk->outdex);
790 dev_err(&dev->dev, " rx: index: %i, outdex: %i\n",
791 priv->rx_dmatsk->index,
792 priv->rx_dmatsk->outdex);
794 #endif
797 bcom_disable(priv->tx_dmatsk);
799 /* Stop FEC */
800 out_be32(&fec->ecntrl, in_be32(&fec->ecntrl) & ~FEC_ECNTRL_ETHER_EN);
802 return;
805 /* reset fec and bestcomm tasks */
806 static void mpc52xx_fec_reset(struct net_device *dev)
808 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
809 struct mpc52xx_fec __iomem *fec = priv->fec;
811 mpc52xx_fec_stop(dev);
813 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status));
814 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_RESET_FIFO);
816 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
818 mpc52xx_fec_hw_init(dev);
820 phy_stop(priv->phydev);
821 phy_write(priv->phydev, MII_BMCR, BMCR_RESET);
822 phy_start(priv->phydev);
824 bcom_fec_rx_reset(priv->rx_dmatsk);
825 bcom_fec_tx_reset(priv->tx_dmatsk);
827 mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
829 bcom_enable(priv->rx_dmatsk);
830 bcom_enable(priv->tx_dmatsk);
832 mpc52xx_fec_start(dev);
836 /* ethtool interface */
837 static void mpc52xx_fec_get_drvinfo(struct net_device *dev,
838 struct ethtool_drvinfo *info)
840 strcpy(info->driver, DRIVER_NAME);
843 static int mpc52xx_fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
845 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
847 if (!priv->phydev)
848 return -ENODEV;
850 return phy_ethtool_gset(priv->phydev, cmd);
853 static int mpc52xx_fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
855 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
857 if (!priv->phydev)
858 return -ENODEV;
860 return phy_ethtool_sset(priv->phydev, cmd);
863 static u32 mpc52xx_fec_get_msglevel(struct net_device *dev)
865 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
866 return priv->msg_enable;
869 static void mpc52xx_fec_set_msglevel(struct net_device *dev, u32 level)
871 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
872 priv->msg_enable = level;
875 static const struct ethtool_ops mpc52xx_fec_ethtool_ops = {
876 .get_drvinfo = mpc52xx_fec_get_drvinfo,
877 .get_settings = mpc52xx_fec_get_settings,
878 .set_settings = mpc52xx_fec_set_settings,
879 .get_link = ethtool_op_get_link,
880 .get_msglevel = mpc52xx_fec_get_msglevel,
881 .set_msglevel = mpc52xx_fec_set_msglevel,
885 static int mpc52xx_fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
887 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
889 if (!priv->phydev)
890 return -ENOTSUPP;
892 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
895 static const struct net_device_ops mpc52xx_fec_netdev_ops = {
896 .ndo_open = mpc52xx_fec_open,
897 .ndo_stop = mpc52xx_fec_close,
898 .ndo_start_xmit = mpc52xx_fec_start_xmit,
899 .ndo_set_multicast_list = mpc52xx_fec_set_multicast_list,
900 .ndo_set_mac_address = mpc52xx_fec_set_mac_address,
901 .ndo_validate_addr = eth_validate_addr,
902 .ndo_do_ioctl = mpc52xx_fec_ioctl,
903 .ndo_change_mtu = eth_change_mtu,
904 .ndo_tx_timeout = mpc52xx_fec_tx_timeout,
905 .ndo_get_stats = mpc52xx_fec_get_stats,
906 #ifdef CONFIG_NET_POLL_CONTROLLER
907 .ndo_poll_controller = mpc52xx_fec_poll_controller,
908 #endif
911 /* ======================================================================== */
912 /* OF Driver */
913 /* ======================================================================== */
915 static int __devinit
916 mpc52xx_fec_probe(struct of_device *op, const struct of_device_id *match)
918 int rv;
919 struct net_device *ndev;
920 struct mpc52xx_fec_priv *priv = NULL;
921 struct resource mem;
922 struct device_node *phy_node;
923 const phandle *phy_handle;
924 const u32 *prop;
925 int prop_size;
927 phys_addr_t rx_fifo;
928 phys_addr_t tx_fifo;
930 /* Get the ether ndev & it's private zone */
931 ndev = alloc_etherdev(sizeof(struct mpc52xx_fec_priv));
932 if (!ndev)
933 return -ENOMEM;
935 priv = netdev_priv(ndev);
937 /* Reserve FEC control zone */
938 rv = of_address_to_resource(op->node, 0, &mem);
939 if (rv) {
940 printk(KERN_ERR DRIVER_NAME ": "
941 "Error while parsing device node resource\n" );
942 return rv;
944 if ((mem.end - mem.start + 1) < sizeof(struct mpc52xx_fec)) {
945 printk(KERN_ERR DRIVER_NAME
946 " - invalid resource size (%lx < %x), check mpc52xx_devices.c\n",
947 (unsigned long)(mem.end - mem.start + 1), sizeof(struct mpc52xx_fec));
948 return -EINVAL;
951 if (!request_mem_region(mem.start, sizeof(struct mpc52xx_fec), DRIVER_NAME))
952 return -EBUSY;
954 /* Init ether ndev with what we have */
955 ndev->netdev_ops = &mpc52xx_fec_netdev_ops;
956 ndev->ethtool_ops = &mpc52xx_fec_ethtool_ops;
957 ndev->watchdog_timeo = FEC_WATCHDOG_TIMEOUT;
958 ndev->base_addr = mem.start;
960 spin_lock_init(&priv->lock);
962 /* ioremap the zones */
963 priv->fec = ioremap(mem.start, sizeof(struct mpc52xx_fec));
965 if (!priv->fec) {
966 rv = -ENOMEM;
967 goto probe_error;
970 /* Bestcomm init */
971 rx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, rfifo_data);
972 tx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, tfifo_data);
974 priv->rx_dmatsk = bcom_fec_rx_init(FEC_RX_NUM_BD, rx_fifo, FEC_RX_BUFFER_SIZE);
975 priv->tx_dmatsk = bcom_fec_tx_init(FEC_TX_NUM_BD, tx_fifo);
977 if (!priv->rx_dmatsk || !priv->tx_dmatsk) {
978 printk(KERN_ERR DRIVER_NAME ": Can not init SDMA tasks\n" );
979 rv = -ENOMEM;
980 goto probe_error;
983 /* Get the IRQ we need one by one */
984 /* Control */
985 ndev->irq = irq_of_parse_and_map(op->node, 0);
987 /* RX */
988 priv->r_irq = bcom_get_task_irq(priv->rx_dmatsk);
990 /* TX */
991 priv->t_irq = bcom_get_task_irq(priv->tx_dmatsk);
993 /* MAC address init */
994 if (!is_zero_ether_addr(mpc52xx_fec_mac_addr))
995 memcpy(ndev->dev_addr, mpc52xx_fec_mac_addr, 6);
996 else
997 mpc52xx_fec_get_paddr(ndev, ndev->dev_addr);
999 priv->msg_enable = netif_msg_init(debug, MPC52xx_MESSAGES_DEFAULT);
1002 * Link mode configuration
1005 /* Start with safe defaults for link connection */
1006 priv->phy_addr = FEC5200_PHYADDR_NONE;
1007 priv->speed = 100;
1008 priv->duplex = DUPLEX_HALF;
1009 priv->phy_speed = ((mpc52xx_find_ipb_freq(op->node) >> 20) / 5) << 1;
1011 /* the 7-wire property means don't use MII mode */
1012 if (of_find_property(op->node, "fsl,7-wire-mode", NULL))
1013 priv->phy_addr = FEC5200_PHYADDR_7WIRE;
1015 /* The current speed preconfigures the speed of the MII link */
1016 prop = of_get_property(op->node, "current-speed", &prop_size);
1017 if (prop && (prop_size >= sizeof(u32) * 2)) {
1018 priv->speed = prop[0];
1019 priv->duplex = prop[1] ? DUPLEX_FULL : DUPLEX_HALF;
1022 /* If there is a phy handle, setup link to that phy */
1023 phy_handle = of_get_property(op->node, "phy-handle", &prop_size);
1024 if (phy_handle && (prop_size >= sizeof(phandle))) {
1025 phy_node = of_find_node_by_phandle(*phy_handle);
1026 prop = of_get_property(phy_node, "reg", &prop_size);
1027 if (prop && (prop_size >= sizeof(u32)))
1028 if ((*prop >= 0) && (*prop < PHY_MAX_ADDR))
1029 priv->phy_addr = *prop;
1030 of_node_put(phy_node);
1033 /* Hardware init */
1034 mpc52xx_fec_hw_init(ndev);
1036 mpc52xx_fec_reset_stats(ndev);
1038 SET_NETDEV_DEV(ndev, &op->dev);
1040 /* Register the new network device */
1041 rv = register_netdev(ndev);
1042 if (rv < 0)
1043 goto probe_error;
1045 /* Now report the link setup */
1046 switch (priv->phy_addr) {
1047 case FEC5200_PHYADDR_NONE:
1048 dev_info(&ndev->dev, "Fixed speed MII link: %i%cD\n",
1049 priv->speed, priv->duplex ? 'F' : 'H');
1050 break;
1051 case FEC5200_PHYADDR_7WIRE:
1052 dev_info(&ndev->dev, "using 7-wire PHY mode\n");
1053 break;
1054 default:
1055 dev_info(&ndev->dev, "Using PHY at MDIO address %i\n",
1056 priv->phy_addr);
1059 /* We're done ! */
1060 dev_set_drvdata(&op->dev, ndev);
1062 return 0;
1065 /* Error handling - free everything that might be allocated */
1066 probe_error:
1068 irq_dispose_mapping(ndev->irq);
1070 if (priv->rx_dmatsk)
1071 bcom_fec_rx_release(priv->rx_dmatsk);
1072 if (priv->tx_dmatsk)
1073 bcom_fec_tx_release(priv->tx_dmatsk);
1075 if (priv->fec)
1076 iounmap(priv->fec);
1078 release_mem_region(mem.start, sizeof(struct mpc52xx_fec));
1080 free_netdev(ndev);
1082 return rv;
1085 static int
1086 mpc52xx_fec_remove(struct of_device *op)
1088 struct net_device *ndev;
1089 struct mpc52xx_fec_priv *priv;
1091 ndev = dev_get_drvdata(&op->dev);
1092 priv = netdev_priv(ndev);
1094 unregister_netdev(ndev);
1096 irq_dispose_mapping(ndev->irq);
1098 bcom_fec_rx_release(priv->rx_dmatsk);
1099 bcom_fec_tx_release(priv->tx_dmatsk);
1101 iounmap(priv->fec);
1103 release_mem_region(ndev->base_addr, sizeof(struct mpc52xx_fec));
1105 free_netdev(ndev);
1107 dev_set_drvdata(&op->dev, NULL);
1108 return 0;
1111 #ifdef CONFIG_PM
1112 static int mpc52xx_fec_of_suspend(struct of_device *op, pm_message_t state)
1114 struct net_device *dev = dev_get_drvdata(&op->dev);
1116 if (netif_running(dev))
1117 mpc52xx_fec_close(dev);
1119 return 0;
1122 static int mpc52xx_fec_of_resume(struct of_device *op)
1124 struct net_device *dev = dev_get_drvdata(&op->dev);
1126 mpc52xx_fec_hw_init(dev);
1127 mpc52xx_fec_reset_stats(dev);
1129 if (netif_running(dev))
1130 mpc52xx_fec_open(dev);
1132 return 0;
1134 #endif
1136 static struct of_device_id mpc52xx_fec_match[] = {
1137 { .compatible = "fsl,mpc5200b-fec", },
1138 { .compatible = "fsl,mpc5200-fec", },
1139 { .compatible = "mpc5200-fec", },
1143 MODULE_DEVICE_TABLE(of, mpc52xx_fec_match);
1145 static struct of_platform_driver mpc52xx_fec_driver = {
1146 .owner = THIS_MODULE,
1147 .name = DRIVER_NAME,
1148 .match_table = mpc52xx_fec_match,
1149 .probe = mpc52xx_fec_probe,
1150 .remove = mpc52xx_fec_remove,
1151 #ifdef CONFIG_PM
1152 .suspend = mpc52xx_fec_of_suspend,
1153 .resume = mpc52xx_fec_of_resume,
1154 #endif
1158 /* ======================================================================== */
1159 /* Module */
1160 /* ======================================================================== */
1162 static int __init
1163 mpc52xx_fec_init(void)
1165 #ifdef CONFIG_FEC_MPC52xx_MDIO
1166 int ret;
1167 ret = of_register_platform_driver(&mpc52xx_fec_mdio_driver);
1168 if (ret) {
1169 printk(KERN_ERR DRIVER_NAME ": failed to register mdio driver\n");
1170 return ret;
1172 #endif
1173 return of_register_platform_driver(&mpc52xx_fec_driver);
1176 static void __exit
1177 mpc52xx_fec_exit(void)
1179 of_unregister_platform_driver(&mpc52xx_fec_driver);
1180 #ifdef CONFIG_FEC_MPC52xx_MDIO
1181 of_unregister_platform_driver(&mpc52xx_fec_mdio_driver);
1182 #endif
1186 module_init(mpc52xx_fec_init);
1187 module_exit(mpc52xx_fec_exit);
1189 MODULE_LICENSE("GPL");
1190 MODULE_AUTHOR("Dale Farnsworth");
1191 MODULE_DESCRIPTION("Ethernet driver for the Freescale MPC52xx FEC");