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