BKL: Remove BKL from JFS
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / fec_mpc52xx.c
blobe3e10b4add9ce3e2f1f55adf54707190a6622ea9
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/slab.h>
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/crc32.h>
26 #include <linux/hardirq.h>
27 #include <linux/delay.h>
28 #include <linux/of_device.h>
29 #include <linux/of_mdio.h>
30 #include <linux/of_platform.h>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/ethtool.h>
35 #include <linux/skbuff.h>
37 #include <asm/io.h>
38 #include <asm/delay.h>
39 #include <asm/mpc52xx.h>
41 #include <sysdev/bestcomm/bestcomm.h>
42 #include <sysdev/bestcomm/fec.h>
44 #include "fec_mpc52xx.h"
46 #define DRIVER_NAME "mpc52xx-fec"
48 /* Private driver data structure */
49 struct mpc52xx_fec_priv {
50 struct net_device *ndev;
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 unsigned int mdio_speed;
63 struct device_node *phy_node;
64 struct phy_device *phydev;
65 enum phy_state link;
66 int seven_wire_mode;
70 static irqreturn_t mpc52xx_fec_interrupt(int, void *);
71 static irqreturn_t mpc52xx_fec_rx_interrupt(int, void *);
72 static irqreturn_t mpc52xx_fec_tx_interrupt(int, void *);
73 static void mpc52xx_fec_stop(struct net_device *dev);
74 static void mpc52xx_fec_start(struct net_device *dev);
75 static void mpc52xx_fec_reset(struct net_device *dev);
77 static u8 mpc52xx_fec_mac_addr[6];
78 module_param_array_named(mac, mpc52xx_fec_mac_addr, byte, NULL, 0);
79 MODULE_PARM_DESC(mac, "six hex digits, ie. 0x1,0x2,0xc0,0x01,0xba,0xbe");
81 #define MPC52xx_MESSAGES_DEFAULT ( NETIF_MSG_DRV | NETIF_MSG_PROBE | \
82 NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
83 static int debug = -1; /* the above default */
84 module_param(debug, int, 0);
85 MODULE_PARM_DESC(debug, "debugging messages level");
87 static void mpc52xx_fec_tx_timeout(struct net_device *dev)
89 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
90 unsigned long flags;
92 dev_warn(&dev->dev, "transmit timed out\n");
94 spin_lock_irqsave(&priv->lock, flags);
95 mpc52xx_fec_reset(dev);
96 dev->stats.tx_errors++;
97 spin_unlock_irqrestore(&priv->lock, flags);
99 netif_wake_queue(dev);
102 static void mpc52xx_fec_set_paddr(struct net_device *dev, u8 *mac)
104 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
105 struct mpc52xx_fec __iomem *fec = priv->fec;
107 out_be32(&fec->paddr1, *(u32 *)(&mac[0]));
108 out_be32(&fec->paddr2, (*(u16 *)(&mac[4]) << 16) | FEC_PADDR2_TYPE);
111 static void mpc52xx_fec_get_paddr(struct net_device *dev, u8 *mac)
113 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
114 struct mpc52xx_fec __iomem *fec = priv->fec;
116 *(u32 *)(&mac[0]) = in_be32(&fec->paddr1);
117 *(u16 *)(&mac[4]) = in_be32(&fec->paddr2) >> 16;
120 static int mpc52xx_fec_set_mac_address(struct net_device *dev, void *addr)
122 struct sockaddr *sock = addr;
124 memcpy(dev->dev_addr, sock->sa_data, dev->addr_len);
126 mpc52xx_fec_set_paddr(dev, sock->sa_data);
127 return 0;
130 static void mpc52xx_fec_free_rx_buffers(struct net_device *dev, struct bcom_task *s)
132 while (!bcom_queue_empty(s)) {
133 struct bcom_fec_bd *bd;
134 struct sk_buff *skb;
136 skb = bcom_retrieve_buffer(s, NULL, (struct bcom_bd **)&bd);
137 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
138 DMA_FROM_DEVICE);
139 kfree_skb(skb);
143 static void
144 mpc52xx_fec_rx_submit(struct net_device *dev, struct sk_buff *rskb)
146 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
147 struct bcom_fec_bd *bd;
149 bd = (struct bcom_fec_bd *) bcom_prepare_next_buffer(priv->rx_dmatsk);
150 bd->status = FEC_RX_BUFFER_SIZE;
151 bd->skb_pa = dma_map_single(dev->dev.parent, rskb->data,
152 FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
153 bcom_submit_next_buffer(priv->rx_dmatsk, rskb);
156 static int mpc52xx_fec_alloc_rx_buffers(struct net_device *dev, struct bcom_task *rxtsk)
158 struct sk_buff *skb;
160 while (!bcom_queue_full(rxtsk)) {
161 skb = dev_alloc_skb(FEC_RX_BUFFER_SIZE);
162 if (!skb)
163 return -EAGAIN;
165 /* zero out the initial receive buffers to aid debugging */
166 memset(skb->data, 0, FEC_RX_BUFFER_SIZE);
167 mpc52xx_fec_rx_submit(dev, skb);
169 return 0;
172 /* based on generic_adjust_link from fs_enet-main.c */
173 static void mpc52xx_fec_adjust_link(struct net_device *dev)
175 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
176 struct phy_device *phydev = priv->phydev;
177 int new_state = 0;
179 if (phydev->link != PHY_DOWN) {
180 if (phydev->duplex != priv->duplex) {
181 struct mpc52xx_fec __iomem *fec = priv->fec;
182 u32 rcntrl;
183 u32 tcntrl;
185 new_state = 1;
186 priv->duplex = phydev->duplex;
188 rcntrl = in_be32(&fec->r_cntrl);
189 tcntrl = in_be32(&fec->x_cntrl);
191 rcntrl &= ~FEC_RCNTRL_DRT;
192 tcntrl &= ~FEC_TCNTRL_FDEN;
193 if (phydev->duplex == DUPLEX_FULL)
194 tcntrl |= FEC_TCNTRL_FDEN; /* FD enable */
195 else
196 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
198 out_be32(&fec->r_cntrl, rcntrl);
199 out_be32(&fec->x_cntrl, tcntrl);
202 if (phydev->speed != priv->speed) {
203 new_state = 1;
204 priv->speed = phydev->speed;
207 if (priv->link == PHY_DOWN) {
208 new_state = 1;
209 priv->link = phydev->link;
212 } else if (priv->link) {
213 new_state = 1;
214 priv->link = PHY_DOWN;
215 priv->speed = 0;
216 priv->duplex = -1;
219 if (new_state && netif_msg_link(priv))
220 phy_print_status(phydev);
223 static int mpc52xx_fec_open(struct net_device *dev)
225 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
226 int err = -EBUSY;
228 if (priv->phy_node) {
229 priv->phydev = of_phy_connect(priv->ndev, priv->phy_node,
230 mpc52xx_fec_adjust_link, 0, 0);
231 if (!priv->phydev) {
232 dev_err(&dev->dev, "of_phy_connect failed\n");
233 return -ENODEV;
235 phy_start(priv->phydev);
238 if (request_irq(dev->irq, mpc52xx_fec_interrupt, IRQF_SHARED,
239 DRIVER_NAME "_ctrl", dev)) {
240 dev_err(&dev->dev, "ctrl interrupt request failed\n");
241 goto free_phy;
243 if (request_irq(priv->r_irq, mpc52xx_fec_rx_interrupt, 0,
244 DRIVER_NAME "_rx", dev)) {
245 dev_err(&dev->dev, "rx interrupt request failed\n");
246 goto free_ctrl_irq;
248 if (request_irq(priv->t_irq, mpc52xx_fec_tx_interrupt, 0,
249 DRIVER_NAME "_tx", dev)) {
250 dev_err(&dev->dev, "tx interrupt request failed\n");
251 goto free_2irqs;
254 bcom_fec_rx_reset(priv->rx_dmatsk);
255 bcom_fec_tx_reset(priv->tx_dmatsk);
257 err = mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
258 if (err) {
259 dev_err(&dev->dev, "mpc52xx_fec_alloc_rx_buffers failed\n");
260 goto free_irqs;
263 bcom_enable(priv->rx_dmatsk);
264 bcom_enable(priv->tx_dmatsk);
266 mpc52xx_fec_start(dev);
268 netif_start_queue(dev);
270 return 0;
272 free_irqs:
273 free_irq(priv->t_irq, dev);
274 free_2irqs:
275 free_irq(priv->r_irq, dev);
276 free_ctrl_irq:
277 free_irq(dev->irq, dev);
278 free_phy:
279 if (priv->phydev) {
280 phy_stop(priv->phydev);
281 phy_disconnect(priv->phydev);
282 priv->phydev = NULL;
285 return err;
288 static int mpc52xx_fec_close(struct net_device *dev)
290 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
292 netif_stop_queue(dev);
294 mpc52xx_fec_stop(dev);
296 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
298 free_irq(dev->irq, dev);
299 free_irq(priv->r_irq, dev);
300 free_irq(priv->t_irq, dev);
302 if (priv->phydev) {
303 /* power down phy */
304 phy_stop(priv->phydev);
305 phy_disconnect(priv->phydev);
306 priv->phydev = NULL;
309 return 0;
312 /* This will only be invoked if your driver is _not_ in XOFF state.
313 * What this means is that you need not check it, and that this
314 * invariant will hold if you make sure that the netif_*_queue()
315 * calls are done at the proper times.
317 static int mpc52xx_fec_start_xmit(struct sk_buff *skb, struct net_device *dev)
319 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
320 struct bcom_fec_bd *bd;
321 unsigned long flags;
323 if (bcom_queue_full(priv->tx_dmatsk)) {
324 if (net_ratelimit())
325 dev_err(&dev->dev, "transmit queue overrun\n");
326 return NETDEV_TX_BUSY;
329 spin_lock_irqsave(&priv->lock, flags);
331 bd = (struct bcom_fec_bd *)
332 bcom_prepare_next_buffer(priv->tx_dmatsk);
334 bd->status = skb->len | BCOM_FEC_TX_BD_TFD | BCOM_FEC_TX_BD_TC;
335 bd->skb_pa = dma_map_single(dev->dev.parent, skb->data, skb->len,
336 DMA_TO_DEVICE);
338 bcom_submit_next_buffer(priv->tx_dmatsk, skb);
339 spin_unlock_irqrestore(&priv->lock, flags);
341 if (bcom_queue_full(priv->tx_dmatsk)) {
342 netif_stop_queue(dev);
345 return NETDEV_TX_OK;
348 #ifdef CONFIG_NET_POLL_CONTROLLER
349 static void mpc52xx_fec_poll_controller(struct net_device *dev)
351 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
353 disable_irq(priv->t_irq);
354 mpc52xx_fec_tx_interrupt(priv->t_irq, dev);
355 enable_irq(priv->t_irq);
356 disable_irq(priv->r_irq);
357 mpc52xx_fec_rx_interrupt(priv->r_irq, dev);
358 enable_irq(priv->r_irq);
360 #endif
363 /* This handles BestComm transmit task interrupts
365 static irqreturn_t mpc52xx_fec_tx_interrupt(int irq, void *dev_id)
367 struct net_device *dev = dev_id;
368 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
369 unsigned long flags;
371 spin_lock_irqsave(&priv->lock, flags);
372 while (bcom_buffer_done(priv->tx_dmatsk)) {
373 struct sk_buff *skb;
374 struct bcom_fec_bd *bd;
375 skb = bcom_retrieve_buffer(priv->tx_dmatsk, NULL,
376 (struct bcom_bd **)&bd);
377 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
378 DMA_TO_DEVICE);
380 dev_kfree_skb_irq(skb);
382 spin_unlock_irqrestore(&priv->lock, flags);
384 netif_wake_queue(dev);
386 return IRQ_HANDLED;
389 static irqreturn_t mpc52xx_fec_rx_interrupt(int irq, void *dev_id)
391 struct net_device *dev = dev_id;
392 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
393 struct sk_buff *rskb; /* received sk_buff */
394 struct sk_buff *skb; /* new sk_buff to enqueue in its place */
395 struct bcom_fec_bd *bd;
396 u32 status, physaddr;
397 int length;
398 unsigned long flags;
400 spin_lock_irqsave(&priv->lock, flags);
402 while (bcom_buffer_done(priv->rx_dmatsk)) {
404 rskb = bcom_retrieve_buffer(priv->rx_dmatsk, &status,
405 (struct bcom_bd **)&bd);
406 physaddr = bd->skb_pa;
408 /* Test for errors in received frame */
409 if (status & BCOM_FEC_RX_BD_ERRORS) {
410 /* Drop packet and reuse the buffer */
411 mpc52xx_fec_rx_submit(dev, rskb);
412 dev->stats.rx_dropped++;
413 continue;
416 /* skbs are allocated on open, so now we allocate a new one,
417 * and remove the old (with the packet) */
418 skb = dev_alloc_skb(FEC_RX_BUFFER_SIZE);
419 if (!skb) {
420 /* Can't get a new one : reuse the same & drop pkt */
421 dev_notice(&dev->dev, "Low memory - dropped packet.\n");
422 mpc52xx_fec_rx_submit(dev, rskb);
423 dev->stats.rx_dropped++;
424 continue;
427 /* Enqueue the new sk_buff back on the hardware */
428 mpc52xx_fec_rx_submit(dev, skb);
430 /* Process the received skb - Drop the spin lock while
431 * calling into the network stack */
432 spin_unlock_irqrestore(&priv->lock, flags);
434 dma_unmap_single(dev->dev.parent, physaddr, rskb->len,
435 DMA_FROM_DEVICE);
436 length = status & BCOM_FEC_RX_BD_LEN_MASK;
437 skb_put(rskb, length - 4); /* length without CRC32 */
438 rskb->protocol = eth_type_trans(rskb, dev);
439 netif_rx(rskb);
441 spin_lock_irqsave(&priv->lock, flags);
444 spin_unlock_irqrestore(&priv->lock, flags);
446 return IRQ_HANDLED;
449 static irqreturn_t mpc52xx_fec_interrupt(int irq, void *dev_id)
451 struct net_device *dev = dev_id;
452 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
453 struct mpc52xx_fec __iomem *fec = priv->fec;
454 u32 ievent;
455 unsigned long flags;
457 ievent = in_be32(&fec->ievent);
459 ievent &= ~FEC_IEVENT_MII; /* mii is handled separately */
460 if (!ievent)
461 return IRQ_NONE;
463 out_be32(&fec->ievent, ievent); /* clear pending events */
465 /* on fifo error, soft-reset fec */
466 if (ievent & (FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {
468 if (net_ratelimit() && (ievent & FEC_IEVENT_RFIFO_ERROR))
469 dev_warn(&dev->dev, "FEC_IEVENT_RFIFO_ERROR\n");
470 if (net_ratelimit() && (ievent & FEC_IEVENT_XFIFO_ERROR))
471 dev_warn(&dev->dev, "FEC_IEVENT_XFIFO_ERROR\n");
473 spin_lock_irqsave(&priv->lock, flags);
474 mpc52xx_fec_reset(dev);
475 spin_unlock_irqrestore(&priv->lock, flags);
477 return IRQ_HANDLED;
480 if (ievent & ~FEC_IEVENT_TFINT)
481 dev_dbg(&dev->dev, "ievent: %08x\n", ievent);
483 return IRQ_HANDLED;
487 * Get the current statistics.
488 * This may be called with the card open or closed.
490 static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev)
492 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
493 struct net_device_stats *stats = &dev->stats;
494 struct mpc52xx_fec __iomem *fec = priv->fec;
496 stats->rx_bytes = in_be32(&fec->rmon_r_octets);
497 stats->rx_packets = in_be32(&fec->rmon_r_packets);
498 stats->rx_errors = in_be32(&fec->rmon_r_crc_align) +
499 in_be32(&fec->rmon_r_undersize) +
500 in_be32(&fec->rmon_r_oversize) +
501 in_be32(&fec->rmon_r_frag) +
502 in_be32(&fec->rmon_r_jab);
504 stats->tx_bytes = in_be32(&fec->rmon_t_octets);
505 stats->tx_packets = in_be32(&fec->rmon_t_packets);
506 stats->tx_errors = in_be32(&fec->rmon_t_crc_align) +
507 in_be32(&fec->rmon_t_undersize) +
508 in_be32(&fec->rmon_t_oversize) +
509 in_be32(&fec->rmon_t_frag) +
510 in_be32(&fec->rmon_t_jab);
512 stats->multicast = in_be32(&fec->rmon_r_mc_pkt);
513 stats->collisions = in_be32(&fec->rmon_t_col);
515 /* detailed rx_errors: */
516 stats->rx_length_errors = in_be32(&fec->rmon_r_undersize)
517 + in_be32(&fec->rmon_r_oversize)
518 + in_be32(&fec->rmon_r_frag)
519 + in_be32(&fec->rmon_r_jab);
520 stats->rx_over_errors = in_be32(&fec->r_macerr);
521 stats->rx_crc_errors = in_be32(&fec->ieee_r_crc);
522 stats->rx_frame_errors = in_be32(&fec->ieee_r_align);
523 stats->rx_fifo_errors = in_be32(&fec->rmon_r_drop);
524 stats->rx_missed_errors = in_be32(&fec->rmon_r_drop);
526 /* detailed tx_errors: */
527 stats->tx_aborted_errors = 0;
528 stats->tx_carrier_errors = in_be32(&fec->ieee_t_cserr);
529 stats->tx_fifo_errors = in_be32(&fec->rmon_t_drop);
530 stats->tx_heartbeat_errors = in_be32(&fec->ieee_t_sqe);
531 stats->tx_window_errors = in_be32(&fec->ieee_t_lcol);
533 return stats;
537 * Read MIB counters in order to reset them,
538 * then zero all the stats fields in memory
540 static void mpc52xx_fec_reset_stats(struct net_device *dev)
542 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
543 struct mpc52xx_fec __iomem *fec = priv->fec;
545 out_be32(&fec->mib_control, FEC_MIB_DISABLE);
546 memset_io(&fec->rmon_t_drop, 0,
547 offsetof(struct mpc52xx_fec, reserved10) -
548 offsetof(struct mpc52xx_fec, rmon_t_drop));
549 out_be32(&fec->mib_control, 0);
551 memset(&dev->stats, 0, sizeof(dev->stats));
555 * Set or clear the multicast filter for this adaptor.
557 static void mpc52xx_fec_set_multicast_list(struct net_device *dev)
559 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
560 struct mpc52xx_fec __iomem *fec = priv->fec;
561 u32 rx_control;
563 rx_control = in_be32(&fec->r_cntrl);
565 if (dev->flags & IFF_PROMISC) {
566 rx_control |= FEC_RCNTRL_PROM;
567 out_be32(&fec->r_cntrl, rx_control);
568 } else {
569 rx_control &= ~FEC_RCNTRL_PROM;
570 out_be32(&fec->r_cntrl, rx_control);
572 if (dev->flags & IFF_ALLMULTI) {
573 out_be32(&fec->gaddr1, 0xffffffff);
574 out_be32(&fec->gaddr2, 0xffffffff);
575 } else {
576 u32 crc;
577 struct netdev_hw_addr *ha;
578 u32 gaddr1 = 0x00000000;
579 u32 gaddr2 = 0x00000000;
581 netdev_for_each_mc_addr(ha, dev) {
582 crc = ether_crc_le(6, ha->addr) >> 26;
583 if (crc >= 32)
584 gaddr1 |= 1 << (crc-32);
585 else
586 gaddr2 |= 1 << crc;
588 out_be32(&fec->gaddr1, gaddr1);
589 out_be32(&fec->gaddr2, gaddr2);
595 * mpc52xx_fec_hw_init
596 * @dev: network device
598 * Setup various hardware setting, only needed once on start
600 static void mpc52xx_fec_hw_init(struct net_device *dev)
602 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
603 struct mpc52xx_fec __iomem *fec = priv->fec;
604 int i;
606 /* Whack a reset. We should wait for this. */
607 out_be32(&fec->ecntrl, FEC_ECNTRL_RESET);
608 for (i = 0; i < FEC_RESET_DELAY; ++i) {
609 if ((in_be32(&fec->ecntrl) & FEC_ECNTRL_RESET) == 0)
610 break;
611 udelay(1);
613 if (i == FEC_RESET_DELAY)
614 dev_err(&dev->dev, "FEC Reset timeout!\n");
616 /* set pause to 0x20 frames */
617 out_be32(&fec->op_pause, FEC_OP_PAUSE_OPCODE | 0x20);
619 /* high service request will be deasserted when there's < 7 bytes in fifo
620 * low service request will be deasserted when there's < 4*7 bytes in fifo
622 out_be32(&fec->rfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
623 out_be32(&fec->tfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
625 /* alarm when <= x bytes in FIFO */
626 out_be32(&fec->rfifo_alarm, 0x0000030c);
627 out_be32(&fec->tfifo_alarm, 0x00000100);
629 /* begin transmittion when 256 bytes are in FIFO (or EOF or FIFO full) */
630 out_be32(&fec->x_wmrk, FEC_FIFO_WMRK_256B);
632 /* enable crc generation */
633 out_be32(&fec->xmit_fsm, FEC_XMIT_FSM_APPEND_CRC | FEC_XMIT_FSM_ENABLE_CRC);
634 out_be32(&fec->iaddr1, 0x00000000); /* No individual filter */
635 out_be32(&fec->iaddr2, 0x00000000); /* No individual filter */
637 /* set phy speed.
638 * this can't be done in phy driver, since it needs to be called
639 * before fec stuff (even on resume) */
640 out_be32(&fec->mii_speed, priv->mdio_speed);
644 * mpc52xx_fec_start
645 * @dev: network device
647 * This function is called to start or restart the FEC during a link
648 * change. This happens on fifo errors or when switching between half
649 * and full duplex.
651 static void mpc52xx_fec_start(struct net_device *dev)
653 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
654 struct mpc52xx_fec __iomem *fec = priv->fec;
655 u32 rcntrl;
656 u32 tcntrl;
657 u32 tmp;
659 /* clear sticky error bits */
660 tmp = FEC_FIFO_STATUS_ERR | FEC_FIFO_STATUS_UF | FEC_FIFO_STATUS_OF;
661 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status) & tmp);
662 out_be32(&fec->tfifo_status, in_be32(&fec->tfifo_status) & tmp);
664 /* FIFOs will reset on mpc52xx_fec_enable */
665 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_ENABLE_IS_RESET);
667 /* Set station address. */
668 mpc52xx_fec_set_paddr(dev, dev->dev_addr);
670 mpc52xx_fec_set_multicast_list(dev);
672 /* set max frame len, enable flow control, select mii mode */
673 rcntrl = FEC_RX_BUFFER_SIZE << 16; /* max frame length */
674 rcntrl |= FEC_RCNTRL_FCE;
676 if (!priv->seven_wire_mode)
677 rcntrl |= FEC_RCNTRL_MII_MODE;
679 if (priv->duplex == DUPLEX_FULL)
680 tcntrl = FEC_TCNTRL_FDEN; /* FD enable */
681 else {
682 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
683 tcntrl = 0;
685 out_be32(&fec->r_cntrl, rcntrl);
686 out_be32(&fec->x_cntrl, tcntrl);
688 /* Clear any outstanding interrupt. */
689 out_be32(&fec->ievent, 0xffffffff);
691 /* Enable interrupts we wish to service. */
692 out_be32(&fec->imask, FEC_IMASK_ENABLE);
694 /* And last, enable the transmit and receive processing. */
695 out_be32(&fec->ecntrl, FEC_ECNTRL_ETHER_EN);
696 out_be32(&fec->r_des_active, 0x01000000);
700 * mpc52xx_fec_stop
701 * @dev: network device
703 * stop all activity on fec and empty dma buffers
705 static void mpc52xx_fec_stop(struct net_device *dev)
707 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
708 struct mpc52xx_fec __iomem *fec = priv->fec;
709 unsigned long timeout;
711 /* disable all interrupts */
712 out_be32(&fec->imask, 0);
714 /* Disable the rx task. */
715 bcom_disable(priv->rx_dmatsk);
717 /* Wait for tx queue to drain, but only if we're in process context */
718 if (!in_interrupt()) {
719 timeout = jiffies + msecs_to_jiffies(2000);
720 while (time_before(jiffies, timeout) &&
721 !bcom_queue_empty(priv->tx_dmatsk))
722 msleep(100);
724 if (time_after_eq(jiffies, timeout))
725 dev_err(&dev->dev, "queues didn't drain\n");
726 #if 1
727 if (time_after_eq(jiffies, timeout)) {
728 dev_err(&dev->dev, " tx: index: %i, outdex: %i\n",
729 priv->tx_dmatsk->index,
730 priv->tx_dmatsk->outdex);
731 dev_err(&dev->dev, " rx: index: %i, outdex: %i\n",
732 priv->rx_dmatsk->index,
733 priv->rx_dmatsk->outdex);
735 #endif
738 bcom_disable(priv->tx_dmatsk);
740 /* Stop FEC */
741 out_be32(&fec->ecntrl, in_be32(&fec->ecntrl) & ~FEC_ECNTRL_ETHER_EN);
744 /* reset fec and bestcomm tasks */
745 static void mpc52xx_fec_reset(struct net_device *dev)
747 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
748 struct mpc52xx_fec __iomem *fec = priv->fec;
750 mpc52xx_fec_stop(dev);
752 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status));
753 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_RESET_FIFO);
755 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
757 mpc52xx_fec_hw_init(dev);
759 bcom_fec_rx_reset(priv->rx_dmatsk);
760 bcom_fec_tx_reset(priv->tx_dmatsk);
762 mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
764 bcom_enable(priv->rx_dmatsk);
765 bcom_enable(priv->tx_dmatsk);
767 mpc52xx_fec_start(dev);
769 netif_wake_queue(dev);
773 /* ethtool interface */
774 static void mpc52xx_fec_get_drvinfo(struct net_device *dev,
775 struct ethtool_drvinfo *info)
777 strcpy(info->driver, DRIVER_NAME);
780 static int mpc52xx_fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
782 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
784 if (!priv->phydev)
785 return -ENODEV;
787 return phy_ethtool_gset(priv->phydev, cmd);
790 static int mpc52xx_fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
792 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
794 if (!priv->phydev)
795 return -ENODEV;
797 return phy_ethtool_sset(priv->phydev, cmd);
800 static u32 mpc52xx_fec_get_msglevel(struct net_device *dev)
802 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
803 return priv->msg_enable;
806 static void mpc52xx_fec_set_msglevel(struct net_device *dev, u32 level)
808 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
809 priv->msg_enable = level;
812 static const struct ethtool_ops mpc52xx_fec_ethtool_ops = {
813 .get_drvinfo = mpc52xx_fec_get_drvinfo,
814 .get_settings = mpc52xx_fec_get_settings,
815 .set_settings = mpc52xx_fec_set_settings,
816 .get_link = ethtool_op_get_link,
817 .get_msglevel = mpc52xx_fec_get_msglevel,
818 .set_msglevel = mpc52xx_fec_set_msglevel,
822 static int mpc52xx_fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
824 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
826 if (!priv->phydev)
827 return -ENOTSUPP;
829 return phy_mii_ioctl(priv->phydev, rq, cmd);
832 static const struct net_device_ops mpc52xx_fec_netdev_ops = {
833 .ndo_open = mpc52xx_fec_open,
834 .ndo_stop = mpc52xx_fec_close,
835 .ndo_start_xmit = mpc52xx_fec_start_xmit,
836 .ndo_set_multicast_list = mpc52xx_fec_set_multicast_list,
837 .ndo_set_mac_address = mpc52xx_fec_set_mac_address,
838 .ndo_validate_addr = eth_validate_addr,
839 .ndo_do_ioctl = mpc52xx_fec_ioctl,
840 .ndo_change_mtu = eth_change_mtu,
841 .ndo_tx_timeout = mpc52xx_fec_tx_timeout,
842 .ndo_get_stats = mpc52xx_fec_get_stats,
843 #ifdef CONFIG_NET_POLL_CONTROLLER
844 .ndo_poll_controller = mpc52xx_fec_poll_controller,
845 #endif
848 /* ======================================================================== */
849 /* OF Driver */
850 /* ======================================================================== */
852 static int __devinit
853 mpc52xx_fec_probe(struct platform_device *op, const struct of_device_id *match)
855 int rv;
856 struct net_device *ndev;
857 struct mpc52xx_fec_priv *priv = NULL;
858 struct resource mem;
859 const u32 *prop;
860 int prop_size;
862 phys_addr_t rx_fifo;
863 phys_addr_t tx_fifo;
865 /* Get the ether ndev & it's private zone */
866 ndev = alloc_etherdev(sizeof(struct mpc52xx_fec_priv));
867 if (!ndev)
868 return -ENOMEM;
870 priv = netdev_priv(ndev);
871 priv->ndev = ndev;
873 /* Reserve FEC control zone */
874 rv = of_address_to_resource(op->dev.of_node, 0, &mem);
875 if (rv) {
876 printk(KERN_ERR DRIVER_NAME ": "
877 "Error while parsing device node resource\n" );
878 goto err_netdev;
880 if ((mem.end - mem.start + 1) < sizeof(struct mpc52xx_fec)) {
881 printk(KERN_ERR DRIVER_NAME
882 " - invalid resource size (%lx < %x), check mpc52xx_devices.c\n",
883 (unsigned long)(mem.end - mem.start + 1), sizeof(struct mpc52xx_fec));
884 rv = -EINVAL;
885 goto err_netdev;
888 if (!request_mem_region(mem.start, sizeof(struct mpc52xx_fec),
889 DRIVER_NAME)) {
890 rv = -EBUSY;
891 goto err_netdev;
894 /* Init ether ndev with what we have */
895 ndev->netdev_ops = &mpc52xx_fec_netdev_ops;
896 ndev->ethtool_ops = &mpc52xx_fec_ethtool_ops;
897 ndev->watchdog_timeo = FEC_WATCHDOG_TIMEOUT;
898 ndev->base_addr = mem.start;
899 SET_NETDEV_DEV(ndev, &op->dev);
901 spin_lock_init(&priv->lock);
903 /* ioremap the zones */
904 priv->fec = ioremap(mem.start, sizeof(struct mpc52xx_fec));
906 if (!priv->fec) {
907 rv = -ENOMEM;
908 goto err_mem_region;
911 /* Bestcomm init */
912 rx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, rfifo_data);
913 tx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, tfifo_data);
915 priv->rx_dmatsk = bcom_fec_rx_init(FEC_RX_NUM_BD, rx_fifo, FEC_RX_BUFFER_SIZE);
916 priv->tx_dmatsk = bcom_fec_tx_init(FEC_TX_NUM_BD, tx_fifo);
918 if (!priv->rx_dmatsk || !priv->tx_dmatsk) {
919 printk(KERN_ERR DRIVER_NAME ": Can not init SDMA tasks\n" );
920 rv = -ENOMEM;
921 goto err_rx_tx_dmatsk;
924 /* Get the IRQ we need one by one */
925 /* Control */
926 ndev->irq = irq_of_parse_and_map(op->dev.of_node, 0);
928 /* RX */
929 priv->r_irq = bcom_get_task_irq(priv->rx_dmatsk);
931 /* TX */
932 priv->t_irq = bcom_get_task_irq(priv->tx_dmatsk);
934 /* MAC address init */
935 if (!is_zero_ether_addr(mpc52xx_fec_mac_addr))
936 memcpy(ndev->dev_addr, mpc52xx_fec_mac_addr, 6);
937 else
938 mpc52xx_fec_get_paddr(ndev, ndev->dev_addr);
940 priv->msg_enable = netif_msg_init(debug, MPC52xx_MESSAGES_DEFAULT);
943 * Link mode configuration
946 /* Start with safe defaults for link connection */
947 priv->speed = 100;
948 priv->duplex = DUPLEX_HALF;
949 priv->mdio_speed = ((mpc5xxx_get_bus_frequency(op->dev.of_node) >> 20) / 5) << 1;
951 /* The current speed preconfigures the speed of the MII link */
952 prop = of_get_property(op->dev.of_node, "current-speed", &prop_size);
953 if (prop && (prop_size >= sizeof(u32) * 2)) {
954 priv->speed = prop[0];
955 priv->duplex = prop[1] ? DUPLEX_FULL : DUPLEX_HALF;
958 /* If there is a phy handle, then get the PHY node */
959 priv->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
961 /* the 7-wire property means don't use MII mode */
962 if (of_find_property(op->dev.of_node, "fsl,7-wire-mode", NULL)) {
963 priv->seven_wire_mode = 1;
964 dev_info(&ndev->dev, "using 7-wire PHY mode\n");
967 /* Hardware init */
968 mpc52xx_fec_hw_init(ndev);
969 mpc52xx_fec_reset_stats(ndev);
971 rv = register_netdev(ndev);
972 if (rv < 0)
973 goto err_node;
975 /* We're done ! */
976 dev_set_drvdata(&op->dev, ndev);
978 return 0;
980 err_node:
981 of_node_put(priv->phy_node);
982 irq_dispose_mapping(ndev->irq);
983 err_rx_tx_dmatsk:
984 if (priv->rx_dmatsk)
985 bcom_fec_rx_release(priv->rx_dmatsk);
986 if (priv->tx_dmatsk)
987 bcom_fec_tx_release(priv->tx_dmatsk);
988 iounmap(priv->fec);
989 err_mem_region:
990 release_mem_region(mem.start, sizeof(struct mpc52xx_fec));
991 err_netdev:
992 free_netdev(ndev);
994 return rv;
997 static int
998 mpc52xx_fec_remove(struct platform_device *op)
1000 struct net_device *ndev;
1001 struct mpc52xx_fec_priv *priv;
1003 ndev = dev_get_drvdata(&op->dev);
1004 priv = netdev_priv(ndev);
1006 unregister_netdev(ndev);
1008 if (priv->phy_node)
1009 of_node_put(priv->phy_node);
1010 priv->phy_node = NULL;
1012 irq_dispose_mapping(ndev->irq);
1014 bcom_fec_rx_release(priv->rx_dmatsk);
1015 bcom_fec_tx_release(priv->tx_dmatsk);
1017 iounmap(priv->fec);
1019 release_mem_region(ndev->base_addr, sizeof(struct mpc52xx_fec));
1021 free_netdev(ndev);
1023 dev_set_drvdata(&op->dev, NULL);
1024 return 0;
1027 #ifdef CONFIG_PM
1028 static int mpc52xx_fec_of_suspend(struct platform_device *op, pm_message_t state)
1030 struct net_device *dev = dev_get_drvdata(&op->dev);
1032 if (netif_running(dev))
1033 mpc52xx_fec_close(dev);
1035 return 0;
1038 static int mpc52xx_fec_of_resume(struct platform_device *op)
1040 struct net_device *dev = dev_get_drvdata(&op->dev);
1042 mpc52xx_fec_hw_init(dev);
1043 mpc52xx_fec_reset_stats(dev);
1045 if (netif_running(dev))
1046 mpc52xx_fec_open(dev);
1048 return 0;
1050 #endif
1052 static struct of_device_id mpc52xx_fec_match[] = {
1053 { .compatible = "fsl,mpc5200b-fec", },
1054 { .compatible = "fsl,mpc5200-fec", },
1055 { .compatible = "mpc5200-fec", },
1059 MODULE_DEVICE_TABLE(of, mpc52xx_fec_match);
1061 static struct of_platform_driver mpc52xx_fec_driver = {
1062 .driver = {
1063 .name = DRIVER_NAME,
1064 .owner = THIS_MODULE,
1065 .of_match_table = mpc52xx_fec_match,
1067 .probe = mpc52xx_fec_probe,
1068 .remove = mpc52xx_fec_remove,
1069 #ifdef CONFIG_PM
1070 .suspend = mpc52xx_fec_of_suspend,
1071 .resume = mpc52xx_fec_of_resume,
1072 #endif
1076 /* ======================================================================== */
1077 /* Module */
1078 /* ======================================================================== */
1080 static int __init
1081 mpc52xx_fec_init(void)
1083 #ifdef CONFIG_FEC_MPC52xx_MDIO
1084 int ret;
1085 ret = of_register_platform_driver(&mpc52xx_fec_mdio_driver);
1086 if (ret) {
1087 printk(KERN_ERR DRIVER_NAME ": failed to register mdio driver\n");
1088 return ret;
1090 #endif
1091 return of_register_platform_driver(&mpc52xx_fec_driver);
1094 static void __exit
1095 mpc52xx_fec_exit(void)
1097 of_unregister_platform_driver(&mpc52xx_fec_driver);
1098 #ifdef CONFIG_FEC_MPC52xx_MDIO
1099 of_unregister_platform_driver(&mpc52xx_fec_mdio_driver);
1100 #endif
1104 module_init(mpc52xx_fec_init);
1105 module_exit(mpc52xx_fec_exit);
1107 MODULE_LICENSE("GPL");
1108 MODULE_AUTHOR("Dale Farnsworth");
1109 MODULE_DESCRIPTION("Ethernet driver for the Freescale MPC52xx FEC");