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