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