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