Loading...
1/*
2 * Driver for the MPC5200 Fast Ethernet Controller
3 *
4 * Originally written by Dale Farnsworth <dfarnsworth@mvista.com> and
5 * now maintained by Sylvain Munaut <tnt@246tNt.com>
6 *
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.
10 *
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.
14 *
15 */
16
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19#include <linux/dma-mapping.h>
20#include <linux/module.h>
21
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>
32#include <linux/of.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>
38
39#include <linux/netdevice.h>
40#include <linux/etherdevice.h>
41#include <linux/ethtool.h>
42#include <linux/skbuff.h>
43
44#include <asm/io.h>
45#include <asm/delay.h>
46#include <asm/mpc52xx.h>
47
48#include <linux/fsl/bestcomm/bestcomm.h>
49#include <linux/fsl/bestcomm/fec.h>
50
51#include "fec_mpc52xx.h"
52
53#define DRIVER_NAME "mpc52xx-fec"
54
55/* Private driver data structure */
56struct mpc52xx_fec_priv {
57 struct net_device *ndev;
58 int duplex;
59 int speed;
60 int r_irq;
61 int t_irq;
62 struct mpc52xx_fec __iomem *fec;
63 struct bcom_task *rx_dmatsk;
64 struct bcom_task *tx_dmatsk;
65 spinlock_t lock;
66 int msg_enable;
67
68 /* MDIO link details */
69 unsigned int mdio_speed;
70 struct device_node *phy_node;
71 enum phy_state link;
72 int seven_wire_mode;
73};
74
75
76static irqreturn_t mpc52xx_fec_interrupt(int, void *);
77static irqreturn_t mpc52xx_fec_rx_interrupt(int, void *);
78static irqreturn_t mpc52xx_fec_tx_interrupt(int, void *);
79static void mpc52xx_fec_stop(struct net_device *dev, bool may_sleep);
80static void mpc52xx_fec_start(struct net_device *dev);
81static void mpc52xx_fec_reset(struct net_device *dev);
82
83#define MPC52xx_MESSAGES_DEFAULT ( NETIF_MSG_DRV | NETIF_MSG_PROBE | \
84 NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
85static int debug = -1; /* the above default */
86module_param(debug, int, 0);
87MODULE_PARM_DESC(debug, "debugging messages level");
88
89static void mpc52xx_fec_tx_timeout(struct net_device *dev, unsigned int txqueue)
90{
91 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
92 unsigned long flags;
93
94 dev_warn(&dev->dev, "transmit timed out\n");
95
96 spin_lock_irqsave(&priv->lock, flags);
97 mpc52xx_fec_reset(dev);
98 dev->stats.tx_errors++;
99 spin_unlock_irqrestore(&priv->lock, flags);
100
101 netif_wake_queue(dev);
102}
103
104static void mpc52xx_fec_set_paddr(struct net_device *dev, const u8 *mac)
105{
106 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
107 struct mpc52xx_fec __iomem *fec = priv->fec;
108
109 out_be32(&fec->paddr1, *(const u32 *)(&mac[0]));
110 out_be32(&fec->paddr2, (*(const u16 *)(&mac[4]) << 16) | FEC_PADDR2_TYPE);
111}
112
113static int mpc52xx_fec_set_mac_address(struct net_device *dev, void *addr)
114{
115 struct sockaddr *sock = addr;
116
117 eth_hw_addr_set(dev, sock->sa_data);
118
119 mpc52xx_fec_set_paddr(dev, sock->sa_data);
120 return 0;
121}
122
123static void mpc52xx_fec_free_rx_buffers(struct net_device *dev, struct bcom_task *s)
124{
125 while (!bcom_queue_empty(s)) {
126 struct bcom_fec_bd *bd;
127 struct sk_buff *skb;
128
129 skb = bcom_retrieve_buffer(s, NULL, (struct bcom_bd **)&bd);
130 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
131 DMA_FROM_DEVICE);
132 kfree_skb(skb);
133 }
134}
135
136static void
137mpc52xx_fec_rx_submit(struct net_device *dev, struct sk_buff *rskb)
138{
139 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
140 struct bcom_fec_bd *bd;
141
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);
147}
148
149static int mpc52xx_fec_alloc_rx_buffers(struct net_device *dev, struct bcom_task *rxtsk)
150{
151 struct sk_buff *skb;
152
153 while (!bcom_queue_full(rxtsk)) {
154 skb = netdev_alloc_skb(dev, FEC_RX_BUFFER_SIZE);
155 if (!skb)
156 return -EAGAIN;
157
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);
161 }
162 return 0;
163}
164
165/* based on generic_adjust_link from fs_enet-main.c */
166static void mpc52xx_fec_adjust_link(struct net_device *dev)
167{
168 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
169 struct phy_device *phydev = dev->phydev;
170 int new_state = 0;
171
172 if (phydev->link != PHY_DOWN) {
173 if (phydev->duplex != priv->duplex) {
174 struct mpc52xx_fec __iomem *fec = priv->fec;
175 u32 rcntrl;
176 u32 tcntrl;
177
178 new_state = 1;
179 priv->duplex = phydev->duplex;
180
181 rcntrl = in_be32(&fec->r_cntrl);
182 tcntrl = in_be32(&fec->x_cntrl);
183
184 rcntrl &= ~FEC_RCNTRL_DRT;
185 tcntrl &= ~FEC_TCNTRL_FDEN;
186 if (phydev->duplex == DUPLEX_FULL)
187 tcntrl |= FEC_TCNTRL_FDEN; /* FD enable */
188 else
189 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
190
191 out_be32(&fec->r_cntrl, rcntrl);
192 out_be32(&fec->x_cntrl, tcntrl);
193 }
194
195 if (phydev->speed != priv->speed) {
196 new_state = 1;
197 priv->speed = phydev->speed;
198 }
199
200 if (priv->link == PHY_DOWN) {
201 new_state = 1;
202 priv->link = phydev->link;
203 }
204
205 } else if (priv->link) {
206 new_state = 1;
207 priv->link = PHY_DOWN;
208 priv->speed = 0;
209 priv->duplex = -1;
210 }
211
212 if (new_state && netif_msg_link(priv))
213 phy_print_status(phydev);
214}
215
216static int mpc52xx_fec_open(struct net_device *dev)
217{
218 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
219 struct phy_device *phydev = NULL;
220 int err = -EBUSY;
221
222 if (priv->phy_node) {
223 phydev = of_phy_connect(priv->ndev, priv->phy_node,
224 mpc52xx_fec_adjust_link, 0, 0);
225 if (!phydev) {
226 dev_err(&dev->dev, "of_phy_connect failed\n");
227 return -ENODEV;
228 }
229 phy_start(phydev);
230 }
231
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");
235 goto free_phy;
236 }
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");
240 goto free_ctrl_irq;
241 }
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");
245 goto free_2irqs;
246 }
247
248 bcom_fec_rx_reset(priv->rx_dmatsk);
249 bcom_fec_tx_reset(priv->tx_dmatsk);
250
251 err = mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
252 if (err) {
253 dev_err(&dev->dev, "mpc52xx_fec_alloc_rx_buffers failed\n");
254 goto free_irqs;
255 }
256
257 bcom_enable(priv->rx_dmatsk);
258 bcom_enable(priv->tx_dmatsk);
259
260 mpc52xx_fec_start(dev);
261
262 netif_start_queue(dev);
263
264 return 0;
265
266 free_irqs:
267 free_irq(priv->t_irq, dev);
268 free_2irqs:
269 free_irq(priv->r_irq, dev);
270 free_ctrl_irq:
271 free_irq(dev->irq, dev);
272 free_phy:
273 if (phydev) {
274 phy_stop(phydev);
275 phy_disconnect(phydev);
276 }
277
278 return err;
279}
280
281static int mpc52xx_fec_close(struct net_device *dev)
282{
283 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
284 struct phy_device *phydev = dev->phydev;
285
286 netif_stop_queue(dev);
287
288 mpc52xx_fec_stop(dev, true);
289
290 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
291
292 free_irq(dev->irq, dev);
293 free_irq(priv->r_irq, dev);
294 free_irq(priv->t_irq, dev);
295
296 if (phydev) {
297 /* power down phy */
298 phy_stop(phydev);
299 phy_disconnect(phydev);
300 }
301
302 return 0;
303}
304
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.
309 */
310static netdev_tx_t
311mpc52xx_fec_start_xmit(struct sk_buff *skb, struct net_device *dev)
312{
313 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
314 struct bcom_fec_bd *bd;
315 unsigned long flags;
316
317 if (bcom_queue_full(priv->tx_dmatsk)) {
318 if (net_ratelimit())
319 dev_err(&dev->dev, "transmit queue overrun\n");
320 return NETDEV_TX_BUSY;
321 }
322
323 spin_lock_irqsave(&priv->lock, flags);
324
325 bd = (struct bcom_fec_bd *)
326 bcom_prepare_next_buffer(priv->tx_dmatsk);
327
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,
330 DMA_TO_DEVICE);
331
332 skb_tx_timestamp(skb);
333 bcom_submit_next_buffer(priv->tx_dmatsk, skb);
334 spin_unlock_irqrestore(&priv->lock, flags);
335
336 if (bcom_queue_full(priv->tx_dmatsk)) {
337 netif_stop_queue(dev);
338 }
339
340 return NETDEV_TX_OK;
341}
342
343#ifdef CONFIG_NET_POLL_CONTROLLER
344static void mpc52xx_fec_poll_controller(struct net_device *dev)
345{
346 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
347
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);
354}
355#endif
356
357
358/* This handles BestComm transmit task interrupts
359 */
360static irqreturn_t mpc52xx_fec_tx_interrupt(int irq, void *dev_id)
361{
362 struct net_device *dev = dev_id;
363 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
364
365 spin_lock(&priv->lock);
366 while (bcom_buffer_done(priv->tx_dmatsk)) {
367 struct sk_buff *skb;
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,
372 DMA_TO_DEVICE);
373
374 dev_consume_skb_irq(skb);
375 }
376 spin_unlock(&priv->lock);
377
378 netif_wake_queue(dev);
379
380 return IRQ_HANDLED;
381}
382
383static irqreturn_t mpc52xx_fec_rx_interrupt(int irq, void *dev_id)
384{
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;
391 int length;
392
393 spin_lock(&priv->lock);
394
395 while (bcom_buffer_done(priv->rx_dmatsk)) {
396
397 rskb = bcom_retrieve_buffer(priv->rx_dmatsk, &status,
398 (struct bcom_bd **)&bd);
399 physaddr = bd->skb_pa;
400
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++;
406 continue;
407 }
408
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);
412 if (!skb) {
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++;
417 continue;
418 }
419
420 /* Enqueue the new sk_buff back on the hardware */
421 mpc52xx_fec_rx_submit(dev, skb);
422
423 /* Process the received skb - Drop the spin lock while
424 * calling into the network stack */
425 spin_unlock(&priv->lock);
426
427 dma_unmap_single(dev->dev.parent, physaddr, rskb->len,
428 DMA_FROM_DEVICE);
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))
433 netif_rx(rskb);
434
435 spin_lock(&priv->lock);
436 }
437
438 spin_unlock(&priv->lock);
439
440 return IRQ_HANDLED;
441}
442
443static irqreturn_t mpc52xx_fec_interrupt(int irq, void *dev_id)
444{
445 struct net_device *dev = dev_id;
446 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
447 struct mpc52xx_fec __iomem *fec = priv->fec;
448 u32 ievent;
449
450 ievent = in_be32(&fec->ievent);
451
452 ievent &= ~FEC_IEVENT_MII; /* mii is handled separately */
453 if (!ievent)
454 return IRQ_NONE;
455
456 out_be32(&fec->ievent, ievent); /* clear pending events */
457
458 /* on fifo error, soft-reset fec */
459 if (ievent & (FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {
460
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");
465
466 spin_lock(&priv->lock);
467 mpc52xx_fec_reset(dev);
468 spin_unlock(&priv->lock);
469
470 return IRQ_HANDLED;
471 }
472
473 if (ievent & ~FEC_IEVENT_TFINT)
474 dev_dbg(&dev->dev, "ievent: %08x\n", ievent);
475
476 return IRQ_HANDLED;
477}
478
479/*
480 * Get the current statistics.
481 * This may be called with the card open or closed.
482 */
483static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev)
484{
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;
488
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);
496
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);
504
505 stats->multicast = in_be32(&fec->rmon_r_mc_pkt);
506 stats->collisions = in_be32(&fec->rmon_t_col);
507
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);
518
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);
525
526 return stats;
527}
528
529/*
530 * Read MIB counters in order to reset them,
531 * then zero all the stats fields in memory
532 */
533static void mpc52xx_fec_reset_stats(struct net_device *dev)
534{
535 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
536 struct mpc52xx_fec __iomem *fec = priv->fec;
537
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);
543
544 memset(&dev->stats, 0, sizeof(dev->stats));
545}
546
547/*
548 * Set or clear the multicast filter for this adaptor.
549 */
550static void mpc52xx_fec_set_multicast_list(struct net_device *dev)
551{
552 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
553 struct mpc52xx_fec __iomem *fec = priv->fec;
554 u32 rx_control;
555
556 rx_control = in_be32(&fec->r_cntrl);
557
558 if (dev->flags & IFF_PROMISC) {
559 rx_control |= FEC_RCNTRL_PROM;
560 out_be32(&fec->r_cntrl, rx_control);
561 } else {
562 rx_control &= ~FEC_RCNTRL_PROM;
563 out_be32(&fec->r_cntrl, rx_control);
564
565 if (dev->flags & IFF_ALLMULTI) {
566 out_be32(&fec->gaddr1, 0xffffffff);
567 out_be32(&fec->gaddr2, 0xffffffff);
568 } else {
569 u32 crc;
570 struct netdev_hw_addr *ha;
571 u32 gaddr1 = 0x00000000;
572 u32 gaddr2 = 0x00000000;
573
574 netdev_for_each_mc_addr(ha, dev) {
575 crc = ether_crc_le(6, ha->addr) >> 26;
576 if (crc >= 32)
577 gaddr1 |= 1 << (crc-32);
578 else
579 gaddr2 |= 1 << crc;
580 }
581 out_be32(&fec->gaddr1, gaddr1);
582 out_be32(&fec->gaddr2, gaddr2);
583 }
584 }
585}
586
587/**
588 * mpc52xx_fec_hw_init
589 * @dev: network device
590 *
591 * Setup various hardware setting, only needed once on start
592 */
593static void mpc52xx_fec_hw_init(struct net_device *dev)
594{
595 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
596 struct mpc52xx_fec __iomem *fec = priv->fec;
597 int i;
598
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)
603 break;
604 udelay(1);
605 }
606 if (i == FEC_RESET_DELAY)
607 dev_err(&dev->dev, "FEC Reset timeout!\n");
608
609 /* set pause to 0x20 frames */
610 out_be32(&fec->op_pause, FEC_OP_PAUSE_OPCODE | 0x20);
611
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
614 */
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);
617
618 /* alarm when <= x bytes in FIFO */
619 out_be32(&fec->rfifo_alarm, 0x0000030c);
620 out_be32(&fec->tfifo_alarm, 0x00000100);
621
622 /* begin transmittion when 256 bytes are in FIFO (or EOF or FIFO full) */
623 out_be32(&fec->x_wmrk, FEC_FIFO_WMRK_256B);
624
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 */
629
630 /* set phy speed.
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);
634}
635
636/**
637 * mpc52xx_fec_start
638 * @dev: network device
639 *
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
642 * and full duplex.
643 */
644static void mpc52xx_fec_start(struct net_device *dev)
645{
646 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
647 struct mpc52xx_fec __iomem *fec = priv->fec;
648 u32 rcntrl;
649 u32 tcntrl;
650 u32 tmp;
651
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);
656
657 /* FIFOs will reset on mpc52xx_fec_enable */
658 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_ENABLE_IS_RESET);
659
660 /* Set station address. */
661 mpc52xx_fec_set_paddr(dev, dev->dev_addr);
662
663 mpc52xx_fec_set_multicast_list(dev);
664
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;
668
669 if (!priv->seven_wire_mode)
670 rcntrl |= FEC_RCNTRL_MII_MODE;
671
672 if (priv->duplex == DUPLEX_FULL)
673 tcntrl = FEC_TCNTRL_FDEN; /* FD enable */
674 else {
675 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
676 tcntrl = 0;
677 }
678 out_be32(&fec->r_cntrl, rcntrl);
679 out_be32(&fec->x_cntrl, tcntrl);
680
681 /* Clear any outstanding interrupt. */
682 out_be32(&fec->ievent, 0xffffffff);
683
684 /* Enable interrupts we wish to service. */
685 out_be32(&fec->imask, FEC_IMASK_ENABLE);
686
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);
690}
691
692/**
693 * mpc52xx_fec_stop
694 * @dev: network device
695 *
696 * stop all activity on fec and empty dma buffers
697 */
698static void mpc52xx_fec_stop(struct net_device *dev, bool may_sleep)
699{
700 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
701 struct mpc52xx_fec __iomem *fec = priv->fec;
702 unsigned long timeout;
703
704 /* disable all interrupts */
705 out_be32(&fec->imask, 0);
706
707 /* Disable the rx task. */
708 bcom_disable(priv->rx_dmatsk);
709
710 /* Wait for tx queue to drain, but only if we're in process context */
711 if (may_sleep) {
712 timeout = jiffies + msecs_to_jiffies(2000);
713 while (time_before(jiffies, timeout) &&
714 !bcom_queue_empty(priv->tx_dmatsk))
715 msleep(100);
716
717 if (time_after_eq(jiffies, timeout))
718 dev_err(&dev->dev, "queues didn't drain\n");
719#if 1
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);
727 }
728#endif
729 }
730
731 bcom_disable(priv->tx_dmatsk);
732
733 /* Stop FEC */
734 out_be32(&fec->ecntrl, in_be32(&fec->ecntrl) & ~FEC_ECNTRL_ETHER_EN);
735}
736
737/* reset fec and bestcomm tasks */
738static void mpc52xx_fec_reset(struct net_device *dev)
739{
740 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
741 struct mpc52xx_fec __iomem *fec = priv->fec;
742
743 mpc52xx_fec_stop(dev, false);
744
745 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status));
746 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_RESET_FIFO);
747
748 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
749
750 mpc52xx_fec_hw_init(dev);
751
752 bcom_fec_rx_reset(priv->rx_dmatsk);
753 bcom_fec_tx_reset(priv->tx_dmatsk);
754
755 mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
756
757 bcom_enable(priv->rx_dmatsk);
758 bcom_enable(priv->tx_dmatsk);
759
760 mpc52xx_fec_start(dev);
761
762 netif_wake_queue(dev);
763}
764
765
766/* ethtool interface */
767
768static u32 mpc52xx_fec_get_msglevel(struct net_device *dev)
769{
770 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
771 return priv->msg_enable;
772}
773
774static void mpc52xx_fec_set_msglevel(struct net_device *dev, u32 level)
775{
776 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
777 priv->msg_enable = level;
778}
779
780static 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,
787};
788
789
790static 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,
802#endif
803};
804
805/* ======================================================================== */
806/* OF Driver */
807/* ======================================================================== */
808
809static int mpc52xx_fec_probe(struct platform_device *op)
810{
811 int rv;
812 struct net_device *ndev;
813 struct mpc52xx_fec_priv *priv = NULL;
814 struct resource mem;
815 const u32 *prop;
816 int prop_size;
817 struct device_node *np = op->dev.of_node;
818
819 phys_addr_t rx_fifo;
820 phys_addr_t tx_fifo;
821
822 /* Get the ether ndev & it's private zone */
823 ndev = alloc_etherdev(sizeof(struct mpc52xx_fec_priv));
824 if (!ndev)
825 return -ENOMEM;
826
827 priv = netdev_priv(ndev);
828 priv->ndev = ndev;
829
830 /* Reserve FEC control zone */
831 rv = of_address_to_resource(np, 0, &mem);
832 if (rv) {
833 pr_err("Error while parsing device node resource\n");
834 goto err_netdev;
835 }
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));
840 rv = -EINVAL;
841 goto err_netdev;
842 }
843
844 if (!request_mem_region(mem.start, sizeof(struct mpc52xx_fec),
845 DRIVER_NAME)) {
846 rv = -EBUSY;
847 goto err_netdev;
848 }
849
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);
856
857 spin_lock_init(&priv->lock);
858
859 /* ioremap the zones */
860 priv->fec = ioremap(mem.start, sizeof(struct mpc52xx_fec));
861
862 if (!priv->fec) {
863 rv = -ENOMEM;
864 goto err_mem_region;
865 }
866
867 /* Bestcomm init */
868 rx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, rfifo_data);
869 tx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, tfifo_data);
870
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);
873
874 if (!priv->rx_dmatsk || !priv->tx_dmatsk) {
875 pr_err("Can not init SDMA tasks\n");
876 rv = -ENOMEM;
877 goto err_rx_tx_dmatsk;
878 }
879
880 /* Get the IRQ we need one by one */
881 /* Control */
882 ndev->irq = irq_of_parse_and_map(np, 0);
883
884 /* RX */
885 priv->r_irq = bcom_get_task_irq(priv->rx_dmatsk);
886
887 /* TX */
888 priv->t_irq = bcom_get_task_irq(priv->tx_dmatsk);
889
890 /*
891 * MAC address init:
892 *
893 * First try to read MAC address from DT
894 */
895 rv = of_get_ethdev_address(np, ndev);
896 if (rv) {
897 struct mpc52xx_fec __iomem *fec = priv->fec;
898 u8 addr[ETH_ALEN] __aligned(4);
899
900 /*
901 * If the MAC addresse is not provided via DT then read
902 * it back from the controller regs
903 */
904 *(u32 *)(&addr[0]) = in_be32(&fec->paddr1);
905 *(u16 *)(&addr[4]) = in_be32(&fec->paddr2) >> 16;
906 eth_hw_addr_set(ndev, addr);
907 }
908
909 /*
910 * Check if the MAC address is valid, if not get a random one
911 */
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",
915 ndev->dev_addr);
916 }
917
918 priv->msg_enable = netif_msg_init(debug, MPC52xx_MESSAGES_DEFAULT);
919
920 /*
921 * Link mode configuration
922 */
923
924 /* Start with safe defaults for link connection */
925 priv->speed = 100;
926 priv->duplex = DUPLEX_HALF;
927 priv->mdio_speed = ((mpc5xxx_get_bus_frequency(&op->dev) >> 20) / 5) << 1;
928
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;
934 }
935
936 /* If there is a phy handle, then get the PHY node */
937 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
938
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");
943 }
944
945 /* Hardware init */
946 mpc52xx_fec_hw_init(ndev);
947 mpc52xx_fec_reset_stats(ndev);
948
949 rv = register_netdev(ndev);
950 if (rv < 0)
951 goto err_node;
952
953 /* We're done ! */
954 platform_set_drvdata(op, ndev);
955 netdev_info(ndev, "%pOF MAC %pM\n",
956 op->dev.of_node, ndev->dev_addr);
957
958 return 0;
959
960err_node:
961 of_node_put(priv->phy_node);
962 irq_dispose_mapping(ndev->irq);
963err_rx_tx_dmatsk:
964 if (priv->rx_dmatsk)
965 bcom_fec_rx_release(priv->rx_dmatsk);
966 if (priv->tx_dmatsk)
967 bcom_fec_tx_release(priv->tx_dmatsk);
968 iounmap(priv->fec);
969err_mem_region:
970 release_mem_region(mem.start, sizeof(struct mpc52xx_fec));
971err_netdev:
972 free_netdev(ndev);
973
974 return rv;
975}
976
977static void
978mpc52xx_fec_remove(struct platform_device *op)
979{
980 struct net_device *ndev;
981 struct mpc52xx_fec_priv *priv;
982
983 ndev = platform_get_drvdata(op);
984 priv = netdev_priv(ndev);
985
986 unregister_netdev(ndev);
987
988 of_node_put(priv->phy_node);
989 priv->phy_node = NULL;
990
991 irq_dispose_mapping(ndev->irq);
992
993 bcom_fec_rx_release(priv->rx_dmatsk);
994 bcom_fec_tx_release(priv->tx_dmatsk);
995
996 iounmap(priv->fec);
997
998 release_mem_region(ndev->base_addr, sizeof(struct mpc52xx_fec));
999
1000 free_netdev(ndev);
1001}
1002
1003#ifdef CONFIG_PM
1004static int mpc52xx_fec_of_suspend(struct platform_device *op, pm_message_t state)
1005{
1006 struct net_device *dev = platform_get_drvdata(op);
1007
1008 if (netif_running(dev))
1009 mpc52xx_fec_close(dev);
1010
1011 return 0;
1012}
1013
1014static int mpc52xx_fec_of_resume(struct platform_device *op)
1015{
1016 struct net_device *dev = platform_get_drvdata(op);
1017
1018 mpc52xx_fec_hw_init(dev);
1019 mpc52xx_fec_reset_stats(dev);
1020
1021 if (netif_running(dev))
1022 mpc52xx_fec_open(dev);
1023
1024 return 0;
1025}
1026#endif
1027
1028static const struct of_device_id mpc52xx_fec_match[] = {
1029 { .compatible = "fsl,mpc5200b-fec", },
1030 { .compatible = "fsl,mpc5200-fec", },
1031 { .compatible = "mpc5200-fec", },
1032 { }
1033};
1034
1035MODULE_DEVICE_TABLE(of, mpc52xx_fec_match);
1036
1037static struct platform_driver mpc52xx_fec_driver = {
1038 .driver = {
1039 .name = DRIVER_NAME,
1040 .of_match_table = mpc52xx_fec_match,
1041 },
1042 .probe = mpc52xx_fec_probe,
1043 .remove = mpc52xx_fec_remove,
1044#ifdef CONFIG_PM
1045 .suspend = mpc52xx_fec_of_suspend,
1046 .resume = mpc52xx_fec_of_resume,
1047#endif
1048};
1049
1050
1051/* ======================================================================== */
1052/* Module */
1053/* ======================================================================== */
1054
1055static struct platform_driver * const drivers[] = {
1056#ifdef CONFIG_FEC_MPC52xx_MDIO
1057 &mpc52xx_fec_mdio_driver,
1058#endif
1059 &mpc52xx_fec_driver,
1060};
1061
1062static int __init
1063mpc52xx_fec_init(void)
1064{
1065 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
1066}
1067
1068static void __exit
1069mpc52xx_fec_exit(void)
1070{
1071 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
1072}
1073
1074
1075module_init(mpc52xx_fec_init);
1076module_exit(mpc52xx_fec_exit);
1077
1078MODULE_LICENSE("GPL");
1079MODULE_AUTHOR("Dale Farnsworth");
1080MODULE_DESCRIPTION("Ethernet driver for the Freescale MPC52xx FEC");
1/*
2 * Driver for the MPC5200 Fast Ethernet Controller
3 *
4 * Originally written by Dale Farnsworth <dfarnsworth@mvista.com> and
5 * now maintained by Sylvain Munaut <tnt@246tNt.com>
6 *
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.
10 *
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.
14 *
15 */
16
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19#include <linux/dma-mapping.h>
20#include <linux/module.h>
21
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>
32#include <linux/of_device.h>
33#include <linux/of_mdio.h>
34#include <linux/of_net.h>
35#include <linux/of_platform.h>
36
37#include <linux/netdevice.h>
38#include <linux/etherdevice.h>
39#include <linux/ethtool.h>
40#include <linux/skbuff.h>
41
42#include <asm/io.h>
43#include <asm/delay.h>
44#include <asm/mpc52xx.h>
45
46#include <linux/fsl/bestcomm/bestcomm.h>
47#include <linux/fsl/bestcomm/fec.h>
48
49#include "fec_mpc52xx.h"
50
51#define DRIVER_NAME "mpc52xx-fec"
52
53/* Private driver data structure */
54struct mpc52xx_fec_priv {
55 struct net_device *ndev;
56 int duplex;
57 int speed;
58 int r_irq;
59 int t_irq;
60 struct mpc52xx_fec __iomem *fec;
61 struct bcom_task *rx_dmatsk;
62 struct bcom_task *tx_dmatsk;
63 spinlock_t lock;
64 int msg_enable;
65
66 /* MDIO link details */
67 unsigned int mdio_speed;
68 struct device_node *phy_node;
69 enum phy_state link;
70 int seven_wire_mode;
71};
72
73
74static irqreturn_t mpc52xx_fec_interrupt(int, void *);
75static irqreturn_t mpc52xx_fec_rx_interrupt(int, void *);
76static irqreturn_t mpc52xx_fec_tx_interrupt(int, void *);
77static void mpc52xx_fec_stop(struct net_device *dev);
78static void mpc52xx_fec_start(struct net_device *dev);
79static void mpc52xx_fec_reset(struct net_device *dev);
80
81#define MPC52xx_MESSAGES_DEFAULT ( NETIF_MSG_DRV | NETIF_MSG_PROBE | \
82 NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
83static int debug = -1; /* the above default */
84module_param(debug, int, 0);
85MODULE_PARM_DESC(debug, "debugging messages level");
86
87static void mpc52xx_fec_tx_timeout(struct net_device *dev, unsigned int txqueue)
88{
89 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
90 unsigned long flags;
91
92 dev_warn(&dev->dev, "transmit timed out\n");
93
94 spin_lock_irqsave(&priv->lock, flags);
95 mpc52xx_fec_reset(dev);
96 dev->stats.tx_errors++;
97 spin_unlock_irqrestore(&priv->lock, flags);
98
99 netif_wake_queue(dev);
100}
101
102static void mpc52xx_fec_set_paddr(struct net_device *dev, u8 *mac)
103{
104 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
105 struct mpc52xx_fec __iomem *fec = priv->fec;
106
107 out_be32(&fec->paddr1, *(u32 *)(&mac[0]));
108 out_be32(&fec->paddr2, (*(u16 *)(&mac[4]) << 16) | FEC_PADDR2_TYPE);
109}
110
111static int mpc52xx_fec_set_mac_address(struct net_device *dev, void *addr)
112{
113 struct sockaddr *sock = addr;
114
115 memcpy(dev->dev_addr, sock->sa_data, dev->addr_len);
116
117 mpc52xx_fec_set_paddr(dev, sock->sa_data);
118 return 0;
119}
120
121static void mpc52xx_fec_free_rx_buffers(struct net_device *dev, struct bcom_task *s)
122{
123 while (!bcom_queue_empty(s)) {
124 struct bcom_fec_bd *bd;
125 struct sk_buff *skb;
126
127 skb = bcom_retrieve_buffer(s, NULL, (struct bcom_bd **)&bd);
128 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
129 DMA_FROM_DEVICE);
130 kfree_skb(skb);
131 }
132}
133
134static void
135mpc52xx_fec_rx_submit(struct net_device *dev, struct sk_buff *rskb)
136{
137 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
138 struct bcom_fec_bd *bd;
139
140 bd = (struct bcom_fec_bd *) bcom_prepare_next_buffer(priv->rx_dmatsk);
141 bd->status = FEC_RX_BUFFER_SIZE;
142 bd->skb_pa = dma_map_single(dev->dev.parent, rskb->data,
143 FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
144 bcom_submit_next_buffer(priv->rx_dmatsk, rskb);
145}
146
147static int mpc52xx_fec_alloc_rx_buffers(struct net_device *dev, struct bcom_task *rxtsk)
148{
149 struct sk_buff *skb;
150
151 while (!bcom_queue_full(rxtsk)) {
152 skb = netdev_alloc_skb(dev, FEC_RX_BUFFER_SIZE);
153 if (!skb)
154 return -EAGAIN;
155
156 /* zero out the initial receive buffers to aid debugging */
157 memset(skb->data, 0, FEC_RX_BUFFER_SIZE);
158 mpc52xx_fec_rx_submit(dev, skb);
159 }
160 return 0;
161}
162
163/* based on generic_adjust_link from fs_enet-main.c */
164static void mpc52xx_fec_adjust_link(struct net_device *dev)
165{
166 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
167 struct phy_device *phydev = dev->phydev;
168 int new_state = 0;
169
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;
175
176 new_state = 1;
177 priv->duplex = phydev->duplex;
178
179 rcntrl = in_be32(&fec->r_cntrl);
180 tcntrl = in_be32(&fec->x_cntrl);
181
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) */
188
189 out_be32(&fec->r_cntrl, rcntrl);
190 out_be32(&fec->x_cntrl, tcntrl);
191 }
192
193 if (phydev->speed != priv->speed) {
194 new_state = 1;
195 priv->speed = phydev->speed;
196 }
197
198 if (priv->link == PHY_DOWN) {
199 new_state = 1;
200 priv->link = phydev->link;
201 }
202
203 } else if (priv->link) {
204 new_state = 1;
205 priv->link = PHY_DOWN;
206 priv->speed = 0;
207 priv->duplex = -1;
208 }
209
210 if (new_state && netif_msg_link(priv))
211 phy_print_status(phydev);
212}
213
214static int mpc52xx_fec_open(struct net_device *dev)
215{
216 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
217 struct phy_device *phydev = NULL;
218 int err = -EBUSY;
219
220 if (priv->phy_node) {
221 phydev = of_phy_connect(priv->ndev, priv->phy_node,
222 mpc52xx_fec_adjust_link, 0, 0);
223 if (!phydev) {
224 dev_err(&dev->dev, "of_phy_connect failed\n");
225 return -ENODEV;
226 }
227 phy_start(phydev);
228 }
229
230 if (request_irq(dev->irq, mpc52xx_fec_interrupt, IRQF_SHARED,
231 DRIVER_NAME "_ctrl", dev)) {
232 dev_err(&dev->dev, "ctrl interrupt request failed\n");
233 goto free_phy;
234 }
235 if (request_irq(priv->r_irq, mpc52xx_fec_rx_interrupt, 0,
236 DRIVER_NAME "_rx", dev)) {
237 dev_err(&dev->dev, "rx interrupt request failed\n");
238 goto free_ctrl_irq;
239 }
240 if (request_irq(priv->t_irq, mpc52xx_fec_tx_interrupt, 0,
241 DRIVER_NAME "_tx", dev)) {
242 dev_err(&dev->dev, "tx interrupt request failed\n");
243 goto free_2irqs;
244 }
245
246 bcom_fec_rx_reset(priv->rx_dmatsk);
247 bcom_fec_tx_reset(priv->tx_dmatsk);
248
249 err = mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
250 if (err) {
251 dev_err(&dev->dev, "mpc52xx_fec_alloc_rx_buffers failed\n");
252 goto free_irqs;
253 }
254
255 bcom_enable(priv->rx_dmatsk);
256 bcom_enable(priv->tx_dmatsk);
257
258 mpc52xx_fec_start(dev);
259
260 netif_start_queue(dev);
261
262 return 0;
263
264 free_irqs:
265 free_irq(priv->t_irq, dev);
266 free_2irqs:
267 free_irq(priv->r_irq, dev);
268 free_ctrl_irq:
269 free_irq(dev->irq, dev);
270 free_phy:
271 if (phydev) {
272 phy_stop(phydev);
273 phy_disconnect(phydev);
274 }
275
276 return err;
277}
278
279static int mpc52xx_fec_close(struct net_device *dev)
280{
281 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
282 struct phy_device *phydev = dev->phydev;
283
284 netif_stop_queue(dev);
285
286 mpc52xx_fec_stop(dev);
287
288 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
289
290 free_irq(dev->irq, dev);
291 free_irq(priv->r_irq, dev);
292 free_irq(priv->t_irq, dev);
293
294 if (phydev) {
295 /* power down phy */
296 phy_stop(phydev);
297 phy_disconnect(phydev);
298 }
299
300 return 0;
301}
302
303/* This will only be invoked if your driver is _not_ in XOFF state.
304 * What this means is that you need not check it, and that this
305 * invariant will hold if you make sure that the netif_*_queue()
306 * calls are done at the proper times.
307 */
308static netdev_tx_t
309mpc52xx_fec_start_xmit(struct sk_buff *skb, struct net_device *dev)
310{
311 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
312 struct bcom_fec_bd *bd;
313 unsigned long flags;
314
315 if (bcom_queue_full(priv->tx_dmatsk)) {
316 if (net_ratelimit())
317 dev_err(&dev->dev, "transmit queue overrun\n");
318 return NETDEV_TX_BUSY;
319 }
320
321 spin_lock_irqsave(&priv->lock, flags);
322
323 bd = (struct bcom_fec_bd *)
324 bcom_prepare_next_buffer(priv->tx_dmatsk);
325
326 bd->status = skb->len | BCOM_FEC_TX_BD_TFD | BCOM_FEC_TX_BD_TC;
327 bd->skb_pa = dma_map_single(dev->dev.parent, skb->data, skb->len,
328 DMA_TO_DEVICE);
329
330 skb_tx_timestamp(skb);
331 bcom_submit_next_buffer(priv->tx_dmatsk, skb);
332 spin_unlock_irqrestore(&priv->lock, flags);
333
334 if (bcom_queue_full(priv->tx_dmatsk)) {
335 netif_stop_queue(dev);
336 }
337
338 return NETDEV_TX_OK;
339}
340
341#ifdef CONFIG_NET_POLL_CONTROLLER
342static void mpc52xx_fec_poll_controller(struct net_device *dev)
343{
344 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
345
346 disable_irq(priv->t_irq);
347 mpc52xx_fec_tx_interrupt(priv->t_irq, dev);
348 enable_irq(priv->t_irq);
349 disable_irq(priv->r_irq);
350 mpc52xx_fec_rx_interrupt(priv->r_irq, dev);
351 enable_irq(priv->r_irq);
352}
353#endif
354
355
356/* This handles BestComm transmit task interrupts
357 */
358static irqreturn_t mpc52xx_fec_tx_interrupt(int irq, void *dev_id)
359{
360 struct net_device *dev = dev_id;
361 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
362
363 spin_lock(&priv->lock);
364 while (bcom_buffer_done(priv->tx_dmatsk)) {
365 struct sk_buff *skb;
366 struct bcom_fec_bd *bd;
367 skb = bcom_retrieve_buffer(priv->tx_dmatsk, NULL,
368 (struct bcom_bd **)&bd);
369 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
370 DMA_TO_DEVICE);
371
372 dev_consume_skb_irq(skb);
373 }
374 spin_unlock(&priv->lock);
375
376 netif_wake_queue(dev);
377
378 return IRQ_HANDLED;
379}
380
381static irqreturn_t mpc52xx_fec_rx_interrupt(int irq, void *dev_id)
382{
383 struct net_device *dev = dev_id;
384 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
385 struct sk_buff *rskb; /* received sk_buff */
386 struct sk_buff *skb; /* new sk_buff to enqueue in its place */
387 struct bcom_fec_bd *bd;
388 u32 status, physaddr;
389 int length;
390
391 spin_lock(&priv->lock);
392
393 while (bcom_buffer_done(priv->rx_dmatsk)) {
394
395 rskb = bcom_retrieve_buffer(priv->rx_dmatsk, &status,
396 (struct bcom_bd **)&bd);
397 physaddr = bd->skb_pa;
398
399 /* Test for errors in received frame */
400 if (status & BCOM_FEC_RX_BD_ERRORS) {
401 /* Drop packet and reuse the buffer */
402 mpc52xx_fec_rx_submit(dev, rskb);
403 dev->stats.rx_dropped++;
404 continue;
405 }
406
407 /* skbs are allocated on open, so now we allocate a new one,
408 * and remove the old (with the packet) */
409 skb = netdev_alloc_skb(dev, FEC_RX_BUFFER_SIZE);
410 if (!skb) {
411 /* Can't get a new one : reuse the same & drop pkt */
412 dev_notice(&dev->dev, "Low memory - dropped packet.\n");
413 mpc52xx_fec_rx_submit(dev, rskb);
414 dev->stats.rx_dropped++;
415 continue;
416 }
417
418 /* Enqueue the new sk_buff back on the hardware */
419 mpc52xx_fec_rx_submit(dev, skb);
420
421 /* Process the received skb - Drop the spin lock while
422 * calling into the network stack */
423 spin_unlock(&priv->lock);
424
425 dma_unmap_single(dev->dev.parent, physaddr, rskb->len,
426 DMA_FROM_DEVICE);
427 length = status & BCOM_FEC_RX_BD_LEN_MASK;
428 skb_put(rskb, length - 4); /* length without CRC32 */
429 rskb->protocol = eth_type_trans(rskb, dev);
430 if (!skb_defer_rx_timestamp(rskb))
431 netif_rx(rskb);
432
433 spin_lock(&priv->lock);
434 }
435
436 spin_unlock(&priv->lock);
437
438 return IRQ_HANDLED;
439}
440
441static irqreturn_t mpc52xx_fec_interrupt(int irq, void *dev_id)
442{
443 struct net_device *dev = dev_id;
444 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
445 struct mpc52xx_fec __iomem *fec = priv->fec;
446 u32 ievent;
447
448 ievent = in_be32(&fec->ievent);
449
450 ievent &= ~FEC_IEVENT_MII; /* mii is handled separately */
451 if (!ievent)
452 return IRQ_NONE;
453
454 out_be32(&fec->ievent, ievent); /* clear pending events */
455
456 /* on fifo error, soft-reset fec */
457 if (ievent & (FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {
458
459 if (net_ratelimit() && (ievent & FEC_IEVENT_RFIFO_ERROR))
460 dev_warn(&dev->dev, "FEC_IEVENT_RFIFO_ERROR\n");
461 if (net_ratelimit() && (ievent & FEC_IEVENT_XFIFO_ERROR))
462 dev_warn(&dev->dev, "FEC_IEVENT_XFIFO_ERROR\n");
463
464 spin_lock(&priv->lock);
465 mpc52xx_fec_reset(dev);
466 spin_unlock(&priv->lock);
467
468 return IRQ_HANDLED;
469 }
470
471 if (ievent & ~FEC_IEVENT_TFINT)
472 dev_dbg(&dev->dev, "ievent: %08x\n", ievent);
473
474 return IRQ_HANDLED;
475}
476
477/*
478 * Get the current statistics.
479 * This may be called with the card open or closed.
480 */
481static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev)
482{
483 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
484 struct net_device_stats *stats = &dev->stats;
485 struct mpc52xx_fec __iomem *fec = priv->fec;
486
487 stats->rx_bytes = in_be32(&fec->rmon_r_octets);
488 stats->rx_packets = in_be32(&fec->rmon_r_packets);
489 stats->rx_errors = in_be32(&fec->rmon_r_crc_align) +
490 in_be32(&fec->rmon_r_undersize) +
491 in_be32(&fec->rmon_r_oversize) +
492 in_be32(&fec->rmon_r_frag) +
493 in_be32(&fec->rmon_r_jab);
494
495 stats->tx_bytes = in_be32(&fec->rmon_t_octets);
496 stats->tx_packets = in_be32(&fec->rmon_t_packets);
497 stats->tx_errors = in_be32(&fec->rmon_t_crc_align) +
498 in_be32(&fec->rmon_t_undersize) +
499 in_be32(&fec->rmon_t_oversize) +
500 in_be32(&fec->rmon_t_frag) +
501 in_be32(&fec->rmon_t_jab);
502
503 stats->multicast = in_be32(&fec->rmon_r_mc_pkt);
504 stats->collisions = in_be32(&fec->rmon_t_col);
505
506 /* detailed rx_errors: */
507 stats->rx_length_errors = in_be32(&fec->rmon_r_undersize)
508 + in_be32(&fec->rmon_r_oversize)
509 + in_be32(&fec->rmon_r_frag)
510 + in_be32(&fec->rmon_r_jab);
511 stats->rx_over_errors = in_be32(&fec->r_macerr);
512 stats->rx_crc_errors = in_be32(&fec->ieee_r_crc);
513 stats->rx_frame_errors = in_be32(&fec->ieee_r_align);
514 stats->rx_fifo_errors = in_be32(&fec->rmon_r_drop);
515 stats->rx_missed_errors = in_be32(&fec->rmon_r_drop);
516
517 /* detailed tx_errors: */
518 stats->tx_aborted_errors = 0;
519 stats->tx_carrier_errors = in_be32(&fec->ieee_t_cserr);
520 stats->tx_fifo_errors = in_be32(&fec->rmon_t_drop);
521 stats->tx_heartbeat_errors = in_be32(&fec->ieee_t_sqe);
522 stats->tx_window_errors = in_be32(&fec->ieee_t_lcol);
523
524 return stats;
525}
526
527/*
528 * Read MIB counters in order to reset them,
529 * then zero all the stats fields in memory
530 */
531static void mpc52xx_fec_reset_stats(struct net_device *dev)
532{
533 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
534 struct mpc52xx_fec __iomem *fec = priv->fec;
535
536 out_be32(&fec->mib_control, FEC_MIB_DISABLE);
537 memset_io(&fec->rmon_t_drop, 0,
538 offsetof(struct mpc52xx_fec, reserved10) -
539 offsetof(struct mpc52xx_fec, rmon_t_drop));
540 out_be32(&fec->mib_control, 0);
541
542 memset(&dev->stats, 0, sizeof(dev->stats));
543}
544
545/*
546 * Set or clear the multicast filter for this adaptor.
547 */
548static void mpc52xx_fec_set_multicast_list(struct net_device *dev)
549{
550 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
551 struct mpc52xx_fec __iomem *fec = priv->fec;
552 u32 rx_control;
553
554 rx_control = in_be32(&fec->r_cntrl);
555
556 if (dev->flags & IFF_PROMISC) {
557 rx_control |= FEC_RCNTRL_PROM;
558 out_be32(&fec->r_cntrl, rx_control);
559 } else {
560 rx_control &= ~FEC_RCNTRL_PROM;
561 out_be32(&fec->r_cntrl, rx_control);
562
563 if (dev->flags & IFF_ALLMULTI) {
564 out_be32(&fec->gaddr1, 0xffffffff);
565 out_be32(&fec->gaddr2, 0xffffffff);
566 } else {
567 u32 crc;
568 struct netdev_hw_addr *ha;
569 u32 gaddr1 = 0x00000000;
570 u32 gaddr2 = 0x00000000;
571
572 netdev_for_each_mc_addr(ha, dev) {
573 crc = ether_crc_le(6, ha->addr) >> 26;
574 if (crc >= 32)
575 gaddr1 |= 1 << (crc-32);
576 else
577 gaddr2 |= 1 << crc;
578 }
579 out_be32(&fec->gaddr1, gaddr1);
580 out_be32(&fec->gaddr2, gaddr2);
581 }
582 }
583}
584
585/**
586 * mpc52xx_fec_hw_init
587 * @dev: network device
588 *
589 * Setup various hardware setting, only needed once on start
590 */
591static void mpc52xx_fec_hw_init(struct net_device *dev)
592{
593 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
594 struct mpc52xx_fec __iomem *fec = priv->fec;
595 int i;
596
597 /* Whack a reset. We should wait for this. */
598 out_be32(&fec->ecntrl, FEC_ECNTRL_RESET);
599 for (i = 0; i < FEC_RESET_DELAY; ++i) {
600 if ((in_be32(&fec->ecntrl) & FEC_ECNTRL_RESET) == 0)
601 break;
602 udelay(1);
603 }
604 if (i == FEC_RESET_DELAY)
605 dev_err(&dev->dev, "FEC Reset timeout!\n");
606
607 /* set pause to 0x20 frames */
608 out_be32(&fec->op_pause, FEC_OP_PAUSE_OPCODE | 0x20);
609
610 /* high service request will be deasserted when there's < 7 bytes in fifo
611 * low service request will be deasserted when there's < 4*7 bytes in fifo
612 */
613 out_be32(&fec->rfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
614 out_be32(&fec->tfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
615
616 /* alarm when <= x bytes in FIFO */
617 out_be32(&fec->rfifo_alarm, 0x0000030c);
618 out_be32(&fec->tfifo_alarm, 0x00000100);
619
620 /* begin transmittion when 256 bytes are in FIFO (or EOF or FIFO full) */
621 out_be32(&fec->x_wmrk, FEC_FIFO_WMRK_256B);
622
623 /* enable crc generation */
624 out_be32(&fec->xmit_fsm, FEC_XMIT_FSM_APPEND_CRC | FEC_XMIT_FSM_ENABLE_CRC);
625 out_be32(&fec->iaddr1, 0x00000000); /* No individual filter */
626 out_be32(&fec->iaddr2, 0x00000000); /* No individual filter */
627
628 /* set phy speed.
629 * this can't be done in phy driver, since it needs to be called
630 * before fec stuff (even on resume) */
631 out_be32(&fec->mii_speed, priv->mdio_speed);
632}
633
634/**
635 * mpc52xx_fec_start
636 * @dev: network device
637 *
638 * This function is called to start or restart the FEC during a link
639 * change. This happens on fifo errors or when switching between half
640 * and full duplex.
641 */
642static void mpc52xx_fec_start(struct net_device *dev)
643{
644 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
645 struct mpc52xx_fec __iomem *fec = priv->fec;
646 u32 rcntrl;
647 u32 tcntrl;
648 u32 tmp;
649
650 /* clear sticky error bits */
651 tmp = FEC_FIFO_STATUS_ERR | FEC_FIFO_STATUS_UF | FEC_FIFO_STATUS_OF;
652 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status) & tmp);
653 out_be32(&fec->tfifo_status, in_be32(&fec->tfifo_status) & tmp);
654
655 /* FIFOs will reset on mpc52xx_fec_enable */
656 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_ENABLE_IS_RESET);
657
658 /* Set station address. */
659 mpc52xx_fec_set_paddr(dev, dev->dev_addr);
660
661 mpc52xx_fec_set_multicast_list(dev);
662
663 /* set max frame len, enable flow control, select mii mode */
664 rcntrl = FEC_RX_BUFFER_SIZE << 16; /* max frame length */
665 rcntrl |= FEC_RCNTRL_FCE;
666
667 if (!priv->seven_wire_mode)
668 rcntrl |= FEC_RCNTRL_MII_MODE;
669
670 if (priv->duplex == DUPLEX_FULL)
671 tcntrl = FEC_TCNTRL_FDEN; /* FD enable */
672 else {
673 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
674 tcntrl = 0;
675 }
676 out_be32(&fec->r_cntrl, rcntrl);
677 out_be32(&fec->x_cntrl, tcntrl);
678
679 /* Clear any outstanding interrupt. */
680 out_be32(&fec->ievent, 0xffffffff);
681
682 /* Enable interrupts we wish to service. */
683 out_be32(&fec->imask, FEC_IMASK_ENABLE);
684
685 /* And last, enable the transmit and receive processing. */
686 out_be32(&fec->ecntrl, FEC_ECNTRL_ETHER_EN);
687 out_be32(&fec->r_des_active, 0x01000000);
688}
689
690/**
691 * mpc52xx_fec_stop
692 * @dev: network device
693 *
694 * stop all activity on fec and empty dma buffers
695 */
696static void mpc52xx_fec_stop(struct net_device *dev)
697{
698 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
699 struct mpc52xx_fec __iomem *fec = priv->fec;
700 unsigned long timeout;
701
702 /* disable all interrupts */
703 out_be32(&fec->imask, 0);
704
705 /* Disable the rx task. */
706 bcom_disable(priv->rx_dmatsk);
707
708 /* Wait for tx queue to drain, but only if we're in process context */
709 if (!in_interrupt()) {
710 timeout = jiffies + msecs_to_jiffies(2000);
711 while (time_before(jiffies, timeout) &&
712 !bcom_queue_empty(priv->tx_dmatsk))
713 msleep(100);
714
715 if (time_after_eq(jiffies, timeout))
716 dev_err(&dev->dev, "queues didn't drain\n");
717#if 1
718 if (time_after_eq(jiffies, timeout)) {
719 dev_err(&dev->dev, " tx: index: %i, outdex: %i\n",
720 priv->tx_dmatsk->index,
721 priv->tx_dmatsk->outdex);
722 dev_err(&dev->dev, " rx: index: %i, outdex: %i\n",
723 priv->rx_dmatsk->index,
724 priv->rx_dmatsk->outdex);
725 }
726#endif
727 }
728
729 bcom_disable(priv->tx_dmatsk);
730
731 /* Stop FEC */
732 out_be32(&fec->ecntrl, in_be32(&fec->ecntrl) & ~FEC_ECNTRL_ETHER_EN);
733}
734
735/* reset fec and bestcomm tasks */
736static void mpc52xx_fec_reset(struct net_device *dev)
737{
738 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
739 struct mpc52xx_fec __iomem *fec = priv->fec;
740
741 mpc52xx_fec_stop(dev);
742
743 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status));
744 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_RESET_FIFO);
745
746 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
747
748 mpc52xx_fec_hw_init(dev);
749
750 bcom_fec_rx_reset(priv->rx_dmatsk);
751 bcom_fec_tx_reset(priv->tx_dmatsk);
752
753 mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
754
755 bcom_enable(priv->rx_dmatsk);
756 bcom_enable(priv->tx_dmatsk);
757
758 mpc52xx_fec_start(dev);
759
760 netif_wake_queue(dev);
761}
762
763
764/* ethtool interface */
765
766static u32 mpc52xx_fec_get_msglevel(struct net_device *dev)
767{
768 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
769 return priv->msg_enable;
770}
771
772static void mpc52xx_fec_set_msglevel(struct net_device *dev, u32 level)
773{
774 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
775 priv->msg_enable = level;
776}
777
778static const struct ethtool_ops mpc52xx_fec_ethtool_ops = {
779 .get_link = ethtool_op_get_link,
780 .get_msglevel = mpc52xx_fec_get_msglevel,
781 .set_msglevel = mpc52xx_fec_set_msglevel,
782 .get_ts_info = ethtool_op_get_ts_info,
783 .get_link_ksettings = phy_ethtool_get_link_ksettings,
784 .set_link_ksettings = phy_ethtool_set_link_ksettings,
785};
786
787
788static const struct net_device_ops mpc52xx_fec_netdev_ops = {
789 .ndo_open = mpc52xx_fec_open,
790 .ndo_stop = mpc52xx_fec_close,
791 .ndo_start_xmit = mpc52xx_fec_start_xmit,
792 .ndo_set_rx_mode = mpc52xx_fec_set_multicast_list,
793 .ndo_set_mac_address = mpc52xx_fec_set_mac_address,
794 .ndo_validate_addr = eth_validate_addr,
795 .ndo_do_ioctl = phy_do_ioctl,
796 .ndo_tx_timeout = mpc52xx_fec_tx_timeout,
797 .ndo_get_stats = mpc52xx_fec_get_stats,
798#ifdef CONFIG_NET_POLL_CONTROLLER
799 .ndo_poll_controller = mpc52xx_fec_poll_controller,
800#endif
801};
802
803/* ======================================================================== */
804/* OF Driver */
805/* ======================================================================== */
806
807static int mpc52xx_fec_probe(struct platform_device *op)
808{
809 int rv;
810 struct net_device *ndev;
811 struct mpc52xx_fec_priv *priv = NULL;
812 struct resource mem;
813 const u32 *prop;
814 int prop_size;
815 struct device_node *np = op->dev.of_node;
816 const char *mac_addr;
817
818 phys_addr_t rx_fifo;
819 phys_addr_t tx_fifo;
820
821 /* Get the ether ndev & it's private zone */
822 ndev = alloc_etherdev(sizeof(struct mpc52xx_fec_priv));
823 if (!ndev)
824 return -ENOMEM;
825
826 priv = netdev_priv(ndev);
827 priv->ndev = ndev;
828
829 /* Reserve FEC control zone */
830 rv = of_address_to_resource(np, 0, &mem);
831 if (rv) {
832 pr_err("Error while parsing device node resource\n");
833 goto err_netdev;
834 }
835 if (resource_size(&mem) < sizeof(struct mpc52xx_fec)) {
836 pr_err("invalid resource size (%lx < %x), check mpc52xx_devices.c\n",
837 (unsigned long)resource_size(&mem),
838 sizeof(struct mpc52xx_fec));
839 rv = -EINVAL;
840 goto err_netdev;
841 }
842
843 if (!request_mem_region(mem.start, sizeof(struct mpc52xx_fec),
844 DRIVER_NAME)) {
845 rv = -EBUSY;
846 goto err_netdev;
847 }
848
849 /* Init ether ndev with what we have */
850 ndev->netdev_ops = &mpc52xx_fec_netdev_ops;
851 ndev->ethtool_ops = &mpc52xx_fec_ethtool_ops;
852 ndev->watchdog_timeo = FEC_WATCHDOG_TIMEOUT;
853 ndev->base_addr = mem.start;
854 SET_NETDEV_DEV(ndev, &op->dev);
855
856 spin_lock_init(&priv->lock);
857
858 /* ioremap the zones */
859 priv->fec = ioremap(mem.start, sizeof(struct mpc52xx_fec));
860
861 if (!priv->fec) {
862 rv = -ENOMEM;
863 goto err_mem_region;
864 }
865
866 /* Bestcomm init */
867 rx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, rfifo_data);
868 tx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, tfifo_data);
869
870 priv->rx_dmatsk = bcom_fec_rx_init(FEC_RX_NUM_BD, rx_fifo, FEC_RX_BUFFER_SIZE);
871 priv->tx_dmatsk = bcom_fec_tx_init(FEC_TX_NUM_BD, tx_fifo);
872
873 if (!priv->rx_dmatsk || !priv->tx_dmatsk) {
874 pr_err("Can not init SDMA tasks\n");
875 rv = -ENOMEM;
876 goto err_rx_tx_dmatsk;
877 }
878
879 /* Get the IRQ we need one by one */
880 /* Control */
881 ndev->irq = irq_of_parse_and_map(np, 0);
882
883 /* RX */
884 priv->r_irq = bcom_get_task_irq(priv->rx_dmatsk);
885
886 /* TX */
887 priv->t_irq = bcom_get_task_irq(priv->tx_dmatsk);
888
889 /*
890 * MAC address init:
891 *
892 * First try to read MAC address from DT
893 */
894 mac_addr = of_get_mac_address(np);
895 if (!IS_ERR(mac_addr)) {
896 ether_addr_copy(ndev->dev_addr, mac_addr);
897 } else {
898 struct mpc52xx_fec __iomem *fec = priv->fec;
899
900 /*
901 * If the MAC addresse is not provided via DT then read
902 * it back from the controller regs
903 */
904 *(u32 *)(&ndev->dev_addr[0]) = in_be32(&fec->paddr1);
905 *(u16 *)(&ndev->dev_addr[4]) = in_be32(&fec->paddr2) >> 16;
906 }
907
908 /*
909 * Check if the MAC address is valid, if not get a random one
910 */
911 if (!is_valid_ether_addr(ndev->dev_addr)) {
912 eth_hw_addr_random(ndev);
913 dev_warn(&ndev->dev, "using random MAC address %pM\n",
914 ndev->dev_addr);
915 }
916
917 priv->msg_enable = netif_msg_init(debug, MPC52xx_MESSAGES_DEFAULT);
918
919 /*
920 * Link mode configuration
921 */
922
923 /* Start with safe defaults for link connection */
924 priv->speed = 100;
925 priv->duplex = DUPLEX_HALF;
926 priv->mdio_speed = ((mpc5xxx_get_bus_frequency(np) >> 20) / 5) << 1;
927
928 /* The current speed preconfigures the speed of the MII link */
929 prop = of_get_property(np, "current-speed", &prop_size);
930 if (prop && (prop_size >= sizeof(u32) * 2)) {
931 priv->speed = prop[0];
932 priv->duplex = prop[1] ? DUPLEX_FULL : DUPLEX_HALF;
933 }
934
935 /* If there is a phy handle, then get the PHY node */
936 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
937
938 /* the 7-wire property means don't use MII mode */
939 if (of_find_property(np, "fsl,7-wire-mode", NULL)) {
940 priv->seven_wire_mode = 1;
941 dev_info(&ndev->dev, "using 7-wire PHY mode\n");
942 }
943
944 /* Hardware init */
945 mpc52xx_fec_hw_init(ndev);
946 mpc52xx_fec_reset_stats(ndev);
947
948 rv = register_netdev(ndev);
949 if (rv < 0)
950 goto err_node;
951
952 /* We're done ! */
953 platform_set_drvdata(op, ndev);
954 netdev_info(ndev, "%pOF MAC %pM\n",
955 op->dev.of_node, ndev->dev_addr);
956
957 return 0;
958
959err_node:
960 of_node_put(priv->phy_node);
961 irq_dispose_mapping(ndev->irq);
962err_rx_tx_dmatsk:
963 if (priv->rx_dmatsk)
964 bcom_fec_rx_release(priv->rx_dmatsk);
965 if (priv->tx_dmatsk)
966 bcom_fec_tx_release(priv->tx_dmatsk);
967 iounmap(priv->fec);
968err_mem_region:
969 release_mem_region(mem.start, sizeof(struct mpc52xx_fec));
970err_netdev:
971 free_netdev(ndev);
972
973 return rv;
974}
975
976static int
977mpc52xx_fec_remove(struct platform_device *op)
978{
979 struct net_device *ndev;
980 struct mpc52xx_fec_priv *priv;
981
982 ndev = platform_get_drvdata(op);
983 priv = netdev_priv(ndev);
984
985 unregister_netdev(ndev);
986
987 of_node_put(priv->phy_node);
988 priv->phy_node = NULL;
989
990 irq_dispose_mapping(ndev->irq);
991
992 bcom_fec_rx_release(priv->rx_dmatsk);
993 bcom_fec_tx_release(priv->tx_dmatsk);
994
995 iounmap(priv->fec);
996
997 release_mem_region(ndev->base_addr, sizeof(struct mpc52xx_fec));
998
999 free_netdev(ndev);
1000
1001 return 0;
1002}
1003
1004#ifdef CONFIG_PM
1005static int mpc52xx_fec_of_suspend(struct platform_device *op, pm_message_t state)
1006{
1007 struct net_device *dev = platform_get_drvdata(op);
1008
1009 if (netif_running(dev))
1010 mpc52xx_fec_close(dev);
1011
1012 return 0;
1013}
1014
1015static int mpc52xx_fec_of_resume(struct platform_device *op)
1016{
1017 struct net_device *dev = platform_get_drvdata(op);
1018
1019 mpc52xx_fec_hw_init(dev);
1020 mpc52xx_fec_reset_stats(dev);
1021
1022 if (netif_running(dev))
1023 mpc52xx_fec_open(dev);
1024
1025 return 0;
1026}
1027#endif
1028
1029static const struct of_device_id mpc52xx_fec_match[] = {
1030 { .compatible = "fsl,mpc5200b-fec", },
1031 { .compatible = "fsl,mpc5200-fec", },
1032 { .compatible = "mpc5200-fec", },
1033 { }
1034};
1035
1036MODULE_DEVICE_TABLE(of, mpc52xx_fec_match);
1037
1038static struct platform_driver mpc52xx_fec_driver = {
1039 .driver = {
1040 .name = DRIVER_NAME,
1041 .of_match_table = mpc52xx_fec_match,
1042 },
1043 .probe = mpc52xx_fec_probe,
1044 .remove = mpc52xx_fec_remove,
1045#ifdef CONFIG_PM
1046 .suspend = mpc52xx_fec_of_suspend,
1047 .resume = mpc52xx_fec_of_resume,
1048#endif
1049};
1050
1051
1052/* ======================================================================== */
1053/* Module */
1054/* ======================================================================== */
1055
1056static struct platform_driver * const drivers[] = {
1057#ifdef CONFIG_FEC_MPC52xx_MDIO
1058 &mpc52xx_fec_mdio_driver,
1059#endif
1060 &mpc52xx_fec_driver,
1061};
1062
1063static int __init
1064mpc52xx_fec_init(void)
1065{
1066 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
1067}
1068
1069static void __exit
1070mpc52xx_fec_exit(void)
1071{
1072 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
1073}
1074
1075
1076module_init(mpc52xx_fec_init);
1077module_exit(mpc52xx_fec_exit);
1078
1079MODULE_LICENSE("GPL");
1080MODULE_AUTHOR("Dale Farnsworth");
1081MODULE_DESCRIPTION("Ethernet driver for the Freescale MPC52xx FEC");