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