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