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