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