Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fec.c
Go to the documentation of this file.
1 /*
2  * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3  * Copyright (c) 1997 Dan Malek ([email protected])
4  *
5  * Right now, I am very wasteful with the buffers. I allocate memory
6  * pages and then divide them into 2K frame buffers. This way I know I
7  * have buffers large enough to hold one frame within one buffer descriptor.
8  * Once I get this working, I will use 64 or 128 byte CPM buffers, which
9  * will be much more memory efficient and will easily handle lots of
10  * small packets.
11  *
12  * Much better multiple PHY support by Magnus Damm.
13  * Copyright (c) 2000 Ericsson Radio Systems AB.
14  *
15  * Support for FEC controller of ColdFire processors.
16  * Copyright (c) 2001-2005 Greg Ungerer ([email protected])
17  *
18  * Bug fixes and cleanup by Philippe De Muyter ([email protected])
19  * Copyright (c) 2004-2006 Macq Electronique SA.
20  *
21  * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
22  */
23 
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/ptrace.h>
28 #include <linux/errno.h>
29 #include <linux/ioport.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/pci.h>
33 #include <linux/init.h>
34 #include <linux/delay.h>
35 #include <linux/netdevice.h>
36 #include <linux/etherdevice.h>
37 #include <linux/skbuff.h>
38 #include <linux/spinlock.h>
39 #include <linux/workqueue.h>
40 #include <linux/bitops.h>
41 #include <linux/io.h>
42 #include <linux/irq.h>
43 #include <linux/clk.h>
44 #include <linux/platform_device.h>
45 #include <linux/phy.h>
46 #include <linux/fec.h>
47 #include <linux/of.h>
48 #include <linux/of_device.h>
49 #include <linux/of_gpio.h>
50 #include <linux/of_net.h>
51 #include <linux/pinctrl/consumer.h>
53 
54 #include <asm/cacheflush.h>
55 
56 #ifndef CONFIG_ARM
57 #include <asm/coldfire.h>
58 #include <asm/mcfsim.h>
59 #endif
60 
61 #include "fec.h"
62 
63 #if defined(CONFIG_ARM)
64 #define FEC_ALIGNMENT 0xf
65 #else
66 #define FEC_ALIGNMENT 0x3
67 #endif
68 
69 #define DRIVER_NAME "fec"
70 
71 /* Controller is ENET-MAC */
72 #define FEC_QUIRK_ENET_MAC (1 << 0)
73 /* Controller needs driver to swap frame */
74 #define FEC_QUIRK_SWAP_FRAME (1 << 1)
75 /* Controller uses gasket */
76 #define FEC_QUIRK_USE_GASKET (1 << 2)
77 /* Controller has GBIT support */
78 #define FEC_QUIRK_HAS_GBIT (1 << 3)
79 
80 static struct platform_device_id fec_devtype[] = {
81  {
82  /* keep it for coldfire */
83  .name = DRIVER_NAME,
84  .driver_data = 0,
85  }, {
86  .name = "imx25-fec",
87  .driver_data = FEC_QUIRK_USE_GASKET,
88  }, {
89  .name = "imx27-fec",
90  .driver_data = 0,
91  }, {
92  .name = "imx28-fec",
94  }, {
95  .name = "imx6q-fec",
96  .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT,
97  }, {
98  /* sentinel */
99  }
100 };
101 MODULE_DEVICE_TABLE(platform, fec_devtype);
102 
104  IMX25_FEC = 1, /* runs on i.mx25/50/53 */
105  IMX27_FEC, /* runs on i.mx27/35/51 */
108 };
109 
110 static const struct of_device_id fec_dt_ids[] = {
111  { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
112  { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
113  { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
114  { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
115  { /* sentinel */ }
116 };
117 MODULE_DEVICE_TABLE(of, fec_dt_ids);
118 
119 static unsigned char macaddr[ETH_ALEN];
121 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
122 
123 #if defined(CONFIG_M5272)
124 /*
125  * Some hardware gets it MAC address out of local flash memory.
126  * if this is non-zero then assume it is the address to get MAC from.
127  */
128 #if defined(CONFIG_NETtel)
129 #define FEC_FLASHMAC 0xf0006006
130 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
131 #define FEC_FLASHMAC 0xf0006000
132 #elif defined(CONFIG_CANCam)
133 #define FEC_FLASHMAC 0xf0020000
134 #elif defined (CONFIG_M5272C3)
135 #define FEC_FLASHMAC (0xffe04000 + 4)
136 #elif defined(CONFIG_MOD5272)
137 #define FEC_FLASHMAC 0xffc0406b
138 #else
139 #define FEC_FLASHMAC 0
140 #endif
141 #endif /* CONFIG_M5272 */
142 
143 /* The number of Tx and Rx buffers. These are allocated from the page
144  * pool. The code may assume these are power of two, so it it best
145  * to keep them that size.
146  * We don't need to allocate pages for the transmitter. We just use
147  * the skbuffer directly.
148  */
149 #define FEC_ENET_RX_PAGES 8
150 #define FEC_ENET_RX_FRSIZE 2048
151 #define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
152 #define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
153 #define FEC_ENET_TX_FRSIZE 2048
154 #define FEC_ENET_TX_FRPPG (PAGE_SIZE / FEC_ENET_TX_FRSIZE)
155 #define TX_RING_SIZE 16 /* Must be power of two */
156 #define TX_RING_MOD_MASK 15 /* for this to work */
157 
158 #if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
159 #error "FEC: descriptor ring size constants too large"
160 #endif
161 
162 /* Interrupt events/masks. */
163 #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
164 #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
165 #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
166 #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
167 #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
168 #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
169 #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
170 #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
171 #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
172 #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
173 
174 #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
175 
176 /* The FEC stores dest/src/type, data, and checksum for receive packets.
177  */
178 #define PKT_MAXBUF_SIZE 1518
179 #define PKT_MINBUF_SIZE 64
180 #define PKT_MAXBLR_SIZE 1520
181 
182 /* This device has up to three irqs on some platforms */
183 #define FEC_IRQ_NUM 3
184 
185 /*
186  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
187  * size bits. Other FEC hardware does not, so we need to take that into
188  * account when setting it.
189  */
190 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
191  defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
192 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
193 #else
194 #define OPT_FRAME_SIZE 0
195 #endif
196 
197 /* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
198  * tx_bd_base always point to the base of the buffer descriptors. The
199  * cur_rx and cur_tx point to the currently available buffer.
200  * The dirty_tx tracks the current buffer that is being sent by the
201  * controller. The cur_tx and dirty_tx are equal under both completely
202  * empty and completely full conditions. The empty/ready indicator in
203  * the buffer descriptor determines the actual condition.
204  */
206  /* Hardware registers of the FEC device */
207  void __iomem *hwp;
208 
210 
211  struct clk *clk_ipg;
212  struct clk *clk_ahb;
213 
214  /* The saved address of a sent-in-place packet/buffer, for skfree(). */
215  unsigned char *tx_bounce[TX_RING_SIZE];
220 
221  /* CPM dual port RAM relative addresses */
223  /* Address of Rx and Tx buffers */
226  /* The next free ring entry */
227  struct bufdesc *cur_rx, *cur_tx;
228  /* The ring entries to be free()ed */
229  struct bufdesc *dirty_tx;
230 
232  /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
234 
236 
237  int opened;
238  int dev_id;
239 
240  /* Phylib and MDIO interface */
241  struct mii_bus *mii_bus;
246  int link;
250 };
251 
252 /* FEC MII MMFR bits definition */
253 #define FEC_MMFR_ST (1 << 30)
254 #define FEC_MMFR_OP_READ (2 << 28)
255 #define FEC_MMFR_OP_WRITE (1 << 28)
256 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
257 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
258 #define FEC_MMFR_TA (2 << 16)
259 #define FEC_MMFR_DATA(v) (v & 0xffff)
260 
261 #define FEC_MII_TIMEOUT 30000 /* us */
262 
263 /* Transmitter timeout */
264 #define TX_TIMEOUT (2 * HZ)
265 
266 static int mii_cnt;
267 
268 static void *swap_buffer(void *bufaddr, int len)
269 {
270  int i;
271  unsigned int *buf = bufaddr;
272 
273  for (i = 0; i < (len + 3) / 4; i++, buf++)
274  *buf = cpu_to_be32(*buf);
275 
276  return bufaddr;
277 }
278 
279 static netdev_tx_t
280 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
281 {
282  struct fec_enet_private *fep = netdev_priv(ndev);
283  const struct platform_device_id *id_entry =
285  struct bufdesc *bdp;
286  void *bufaddr;
287  unsigned short status;
288  unsigned long flags;
289 
290  if (!fep->link) {
291  /* Link is down or autonegotiation is in progress. */
292  return NETDEV_TX_BUSY;
293  }
294 
295  spin_lock_irqsave(&fep->hw_lock, flags);
296  /* Fill in a Tx ring entry */
297  bdp = fep->cur_tx;
298 
299  status = bdp->cbd_sc;
300 
301  if (status & BD_ENET_TX_READY) {
302  /* Ooops. All transmit buffers are full. Bail out.
303  * This should not happen, since ndev->tbusy should be set.
304  */
305  printk("%s: tx queue full!.\n", ndev->name);
306  spin_unlock_irqrestore(&fep->hw_lock, flags);
307  return NETDEV_TX_BUSY;
308  }
309 
310  /* Clear all of the status flags */
311  status &= ~BD_ENET_TX_STATS;
312 
313  /* Set buffer length and buffer pointer */
314  bufaddr = skb->data;
315  bdp->cbd_datlen = skb->len;
316 
317  /*
318  * On some FEC implementations data must be aligned on
319  * 4-byte boundaries. Use bounce buffers to copy data
320  * and get it aligned. Ugh.
321  */
322  if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
323  unsigned int index;
324  index = bdp - fep->tx_bd_base;
325  memcpy(fep->tx_bounce[index], skb->data, skb->len);
326  bufaddr = fep->tx_bounce[index];
327  }
328 
329  /*
330  * Some design made an incorrect assumption on endian mode of
331  * the system that it's running on. As the result, driver has to
332  * swap every frame going to and coming from the controller.
333  */
334  if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
335  swap_buffer(bufaddr, skb->len);
336 
337  /* Save skb pointer */
338  fep->tx_skbuff[fep->skb_cur] = skb;
339 
340  ndev->stats.tx_bytes += skb->len;
341  fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
342 
343  /* Push the data cache so the CPM does not get stale memory
344  * data.
345  */
346  bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
348 
349  /* Send it on its way. Tell FEC it's ready, interrupt when done,
350  * it's the last BD of the frame, and to put the CRC on the end.
351  */
352  status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
354  bdp->cbd_sc = status;
355 
356  /* Trigger transmission start */
357  writel(0, fep->hwp + FEC_X_DES_ACTIVE);
358 
359  /* If this was the last BD in the ring, start at the beginning again. */
360  if (status & BD_ENET_TX_WRAP)
361  bdp = fep->tx_bd_base;
362  else
363  bdp++;
364 
365  if (bdp == fep->dirty_tx) {
366  fep->tx_full = 1;
367  netif_stop_queue(ndev);
368  }
369 
370  fep->cur_tx = bdp;
371 
372  skb_tx_timestamp(skb);
373 
374  spin_unlock_irqrestore(&fep->hw_lock, flags);
375 
376  return NETDEV_TX_OK;
377 }
378 
379 /* This function is called to start or restart the FEC during a link
380  * change. This only happens when switching between half and full
381  * duplex.
382  */
383 static void
384 fec_restart(struct net_device *ndev, int duplex)
385 {
386  struct fec_enet_private *fep = netdev_priv(ndev);
387  const struct platform_device_id *id_entry =
389  int i;
390  u32 temp_mac[2];
391  u32 rcntl = OPT_FRAME_SIZE | 0x04;
392  u32 ecntl = 0x2; /* ETHEREN */
393 
394  /* Whack a reset. We should wait for this. */
395  writel(1, fep->hwp + FEC_ECNTRL);
396  udelay(10);
397 
398  /*
399  * enet-mac reset will reset mac address registers too,
400  * so need to reconfigure it.
401  */
402  if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
403  memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
404  writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
405  writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
406  }
407 
408  /* Clear any outstanding interrupt. */
409  writel(0xffc00000, fep->hwp + FEC_IEVENT);
410 
411  /* Reset all multicast. */
413  writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
414 #ifndef CONFIG_M5272
415  writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
416  writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
417 #endif
418 
419  /* Set maximum receive buffer size. */
421 
422  /* Set receive and transmit descriptor base. */
423  writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
424  writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
425  fep->hwp + FEC_X_DES_START);
426 
427  fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
428  fep->cur_rx = fep->rx_bd_base;
429 
430  /* Reset SKB transmit buffers. */
431  fep->skb_cur = fep->skb_dirty = 0;
432  for (i = 0; i <= TX_RING_MOD_MASK; i++) {
433  if (fep->tx_skbuff[i]) {
434  dev_kfree_skb_any(fep->tx_skbuff[i]);
435  fep->tx_skbuff[i] = NULL;
436  }
437  }
438 
439  /* Enable MII mode */
440  if (duplex) {
441  /* FD enable */
442  writel(0x04, fep->hwp + FEC_X_CNTRL);
443  } else {
444  /* No Rcv on Xmit */
445  rcntl |= 0x02;
446  writel(0x0, fep->hwp + FEC_X_CNTRL);
447  }
448 
449  fep->full_duplex = duplex;
450 
451  /* Set MII speed */
452  writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
453 
454  /*
455  * The phy interface and speed need to get configured
456  * differently on enet-mac.
457  */
458  if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
459  /* Enable flow control and length check */
460  rcntl |= 0x40000000 | 0x00000020;
461 
462  /* RGMII, RMII or MII */
464  rcntl |= (1 << 6);
465  else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
466  rcntl |= (1 << 8);
467  else
468  rcntl &= ~(1 << 8);
469 
470  /* 1G, 100M or 10M */
471  if (fep->phy_dev) {
472  if (fep->phy_dev->speed == SPEED_1000)
473  ecntl |= (1 << 5);
474  else if (fep->phy_dev->speed == SPEED_100)
475  rcntl &= ~(1 << 9);
476  else
477  rcntl |= (1 << 9);
478  }
479  } else {
480 #ifdef FEC_MIIGSK_ENR
481  if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
482  u32 cfgr;
483  /* disable the gasket and wait */
484  writel(0, fep->hwp + FEC_MIIGSK_ENR);
485  while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
486  udelay(1);
487 
488  /*
489  * configure the gasket:
490  * RMII, 50 MHz, no loopback, no echo
491  * MII, 25 MHz, no loopback, no echo
492  */
493  cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
494  ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
495  if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
496  cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
497  writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
498 
499  /* re-enable the gasket */
500  writel(2, fep->hwp + FEC_MIIGSK_ENR);
501  }
502 #endif
503  }
504  writel(rcntl, fep->hwp + FEC_R_CNTRL);
505 
506  if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
507  /* enable ENET endian swap */
508  ecntl |= (1 << 8);
509  /* enable ENET store and forward mode */
510  writel(1 << 8, fep->hwp + FEC_X_WMRK);
511  }
512 
513  /* And last, enable the transmit and receive processing */
514  writel(ecntl, fep->hwp + FEC_ECNTRL);
515  writel(0, fep->hwp + FEC_R_DES_ACTIVE);
516 
517  /* Enable interrupts we wish to service */
519 }
520 
521 static void
522 fec_stop(struct net_device *ndev)
523 {
524  struct fec_enet_private *fep = netdev_priv(ndev);
525  const struct platform_device_id *id_entry =
527  u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
528 
529  /* We cannot expect a graceful transmit stop without link !!! */
530  if (fep->link) {
531  writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
532  udelay(10);
533  if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
534  printk("fec_stop : Graceful transmit stop did not complete !\n");
535  }
536 
537  /* Whack a reset. We should wait for this. */
538  writel(1, fep->hwp + FEC_ECNTRL);
539  udelay(10);
540  writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
542 
543  /* We have to keep ENET enabled to have MII interrupt stay working */
544  if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
545  writel(2, fep->hwp + FEC_ECNTRL);
546  writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
547  }
548 }
549 
550 
551 static void
552 fec_timeout(struct net_device *ndev)
553 {
554  struct fec_enet_private *fep = netdev_priv(ndev);
555 
556  ndev->stats.tx_errors++;
557 
558  fec_restart(ndev, fep->full_duplex);
559  netif_wake_queue(ndev);
560 }
561 
562 static void
563 fec_enet_tx(struct net_device *ndev)
564 {
565  struct fec_enet_private *fep;
566  struct bufdesc *bdp;
567  unsigned short status;
568  struct sk_buff *skb;
569 
570  fep = netdev_priv(ndev);
571  spin_lock(&fep->hw_lock);
572  bdp = fep->dirty_tx;
573 
574  while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
575  if (bdp == fep->cur_tx && fep->tx_full == 0)
576  break;
577 
578  dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
580  bdp->cbd_bufaddr = 0;
581 
582  skb = fep->tx_skbuff[fep->skb_dirty];
583  /* Check for errors. */
584  if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
586  BD_ENET_TX_CSL)) {
587  ndev->stats.tx_errors++;
588  if (status & BD_ENET_TX_HB) /* No heartbeat */
589  ndev->stats.tx_heartbeat_errors++;
590  if (status & BD_ENET_TX_LC) /* Late collision */
591  ndev->stats.tx_window_errors++;
592  if (status & BD_ENET_TX_RL) /* Retrans limit */
593  ndev->stats.tx_aborted_errors++;
594  if (status & BD_ENET_TX_UN) /* Underrun */
595  ndev->stats.tx_fifo_errors++;
596  if (status & BD_ENET_TX_CSL) /* Carrier lost */
597  ndev->stats.tx_carrier_errors++;
598  } else {
599  ndev->stats.tx_packets++;
600  }
601 
602  if (status & BD_ENET_TX_READY)
603  printk("HEY! Enet xmit interrupt and TX_READY.\n");
604 
605  /* Deferred means some collisions occurred during transmit,
606  * but we eventually sent the packet OK.
607  */
608  if (status & BD_ENET_TX_DEF)
609  ndev->stats.collisions++;
610 
611  /* Free the sk buffer associated with this last transmit */
612  dev_kfree_skb_any(skb);
613  fep->tx_skbuff[fep->skb_dirty] = NULL;
614  fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
615 
616  /* Update pointer to next buffer descriptor to be transmitted */
617  if (status & BD_ENET_TX_WRAP)
618  bdp = fep->tx_bd_base;
619  else
620  bdp++;
621 
622  /* Since we have freed up a buffer, the ring is no longer full
623  */
624  if (fep->tx_full) {
625  fep->tx_full = 0;
626  if (netif_queue_stopped(ndev))
627  netif_wake_queue(ndev);
628  }
629  }
630  fep->dirty_tx = bdp;
631  spin_unlock(&fep->hw_lock);
632 }
633 
634 
635 /* During a receive, the cur_rx points to the current incoming buffer.
636  * When we update through the ring, if the next incoming buffer has
637  * not been given to the system, we just set the empty indicator,
638  * effectively tossing the packet.
639  */
640 static void
641 fec_enet_rx(struct net_device *ndev)
642 {
643  struct fec_enet_private *fep = netdev_priv(ndev);
644  const struct platform_device_id *id_entry =
646  struct bufdesc *bdp;
647  unsigned short status;
648  struct sk_buff *skb;
649  ushort pkt_len;
650  __u8 *data;
651 
652 #ifdef CONFIG_M532x
653  flush_cache_all();
654 #endif
655 
656  spin_lock(&fep->hw_lock);
657 
658  /* First, grab all of the stats for the incoming packet.
659  * These get messed up if we get called due to a busy condition.
660  */
661  bdp = fep->cur_rx;
662 
663  while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
664 
665  /* Since we have allocated space to hold a complete frame,
666  * the last indicator should be set.
667  */
668  if ((status & BD_ENET_RX_LAST) == 0)
669  printk("FEC ENET: rcv is not +last\n");
670 
671  if (!fep->opened)
672  goto rx_processing_done;
673 
674  /* Check for errors. */
675  if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
677  ndev->stats.rx_errors++;
678  if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
679  /* Frame too long or too short. */
680  ndev->stats.rx_length_errors++;
681  }
682  if (status & BD_ENET_RX_NO) /* Frame alignment */
683  ndev->stats.rx_frame_errors++;
684  if (status & BD_ENET_RX_CR) /* CRC Error */
685  ndev->stats.rx_crc_errors++;
686  if (status & BD_ENET_RX_OV) /* FIFO overrun */
687  ndev->stats.rx_fifo_errors++;
688  }
689 
690  /* Report late collisions as a frame error.
691  * On this error, the BD is closed, but we don't know what we
692  * have in the buffer. So, just drop this frame on the floor.
693  */
694  if (status & BD_ENET_RX_CL) {
695  ndev->stats.rx_errors++;
696  ndev->stats.rx_frame_errors++;
697  goto rx_processing_done;
698  }
699 
700  /* Process the incoming frame. */
701  ndev->stats.rx_packets++;
702  pkt_len = bdp->cbd_datlen;
703  ndev->stats.rx_bytes += pkt_len;
704  data = (__u8*)__va(bdp->cbd_bufaddr);
705 
706  dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
708 
709  if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
710  swap_buffer(data, pkt_len);
711 
712  /* This does 16 byte alignment, exactly what we need.
713  * The packet length includes FCS, but we don't want to
714  * include that when passing upstream as it messes up
715  * bridging applications.
716  */
717  skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
718 
719  if (unlikely(!skb)) {
720  printk("%s: Memory squeeze, dropping packet.\n",
721  ndev->name);
722  ndev->stats.rx_dropped++;
723  } else {
724  skb_reserve(skb, NET_IP_ALIGN);
725  skb_put(skb, pkt_len - 4); /* Make room */
726  skb_copy_to_linear_data(skb, data, pkt_len - 4);
727  skb->protocol = eth_type_trans(skb, ndev);
728  if (!skb_defer_rx_timestamp(skb))
729  netif_rx(skb);
730  }
731 
732  bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
734 rx_processing_done:
735  /* Clear the status flags for this buffer */
736  status &= ~BD_ENET_RX_STATS;
737 
738  /* Mark the buffer empty */
739  status |= BD_ENET_RX_EMPTY;
740  bdp->cbd_sc = status;
741 
742  /* Update BD pointer to next entry */
743  if (status & BD_ENET_RX_WRAP)
744  bdp = fep->rx_bd_base;
745  else
746  bdp++;
747  /* Doing this here will keep the FEC running while we process
748  * incoming frames. On a heavily loaded network, we should be
749  * able to keep up at the expense of system resources.
750  */
751  writel(0, fep->hwp + FEC_R_DES_ACTIVE);
752  }
753  fep->cur_rx = bdp;
754 
755  spin_unlock(&fep->hw_lock);
756 }
757 
758 static irqreturn_t
759 fec_enet_interrupt(int irq, void *dev_id)
760 {
761  struct net_device *ndev = dev_id;
762  struct fec_enet_private *fep = netdev_priv(ndev);
763  uint int_events;
765 
766  do {
767  int_events = readl(fep->hwp + FEC_IEVENT);
768  writel(int_events, fep->hwp + FEC_IEVENT);
769 
770  if (int_events & FEC_ENET_RXF) {
771  ret = IRQ_HANDLED;
772  fec_enet_rx(ndev);
773  }
774 
775  /* Transmit OK, or non-fatal error. Update the buffer
776  * descriptors. FEC handles all errors, we just discover
777  * them as part of the transmit process.
778  */
779  if (int_events & FEC_ENET_TXF) {
780  ret = IRQ_HANDLED;
781  fec_enet_tx(ndev);
782  }
783 
784  if (int_events & FEC_ENET_MII) {
785  ret = IRQ_HANDLED;
786  complete(&fep->mdio_done);
787  }
788  } while (int_events);
789 
790  return ret;
791 }
792 
793 
794 
795 /* ------------------------------------------------------------------------- */
796 static void __inline__ fec_get_mac(struct net_device *ndev)
797 {
798  struct fec_enet_private *fep = netdev_priv(ndev);
799  struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
800  unsigned char *iap, tmpaddr[ETH_ALEN];
801 
802  /*
803  * try to get mac address in following order:
804  *
805  * 1) module parameter via kernel command line in form
806  * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
807  */
808  iap = macaddr;
809 
810 #ifdef CONFIG_OF
811  /*
812  * 2) from device tree data
813  */
814  if (!is_valid_ether_addr(iap)) {
815  struct device_node *np = fep->pdev->dev.of_node;
816  if (np) {
817  const char *mac = of_get_mac_address(np);
818  if (mac)
819  iap = (unsigned char *) mac;
820  }
821  }
822 #endif
823 
824  /*
825  * 3) from flash or fuse (via platform data)
826  */
827  if (!is_valid_ether_addr(iap)) {
828 #ifdef CONFIG_M5272
829  if (FEC_FLASHMAC)
830  iap = (unsigned char *)FEC_FLASHMAC;
831 #else
832  if (pdata)
833  iap = (unsigned char *)&pdata->mac;
834 #endif
835  }
836 
837  /*
838  * 4) FEC mac registers set by bootloader
839  */
840  if (!is_valid_ether_addr(iap)) {
841  *((unsigned long *) &tmpaddr[0]) =
843  *((unsigned short *) &tmpaddr[4]) =
844  be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
845  iap = &tmpaddr[0];
846  }
847 
848  memcpy(ndev->dev_addr, iap, ETH_ALEN);
849 
850  /* Adjust MAC if using macaddr */
851  if (iap == macaddr)
852  ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
853 }
854 
855 /* ------------------------------------------------------------------------- */
856 
857 /*
858  * Phy section
859  */
860 static void fec_enet_adjust_link(struct net_device *ndev)
861 {
862  struct fec_enet_private *fep = netdev_priv(ndev);
863  struct phy_device *phy_dev = fep->phy_dev;
864  unsigned long flags;
865 
866  int status_change = 0;
867 
868  spin_lock_irqsave(&fep->hw_lock, flags);
869 
870  /* Prevent a state halted on mii error */
871  if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
872  phy_dev->state = PHY_RESUMING;
873  goto spin_unlock;
874  }
875 
876  /* Duplex link change */
877  if (phy_dev->link) {
878  if (fep->full_duplex != phy_dev->duplex) {
879  fec_restart(ndev, phy_dev->duplex);
880  /* prevent unnecessary second fec_restart() below */
881  fep->link = phy_dev->link;
882  status_change = 1;
883  }
884  }
885 
886  /* Link on or off change */
887  if (phy_dev->link != fep->link) {
888  fep->link = phy_dev->link;
889  if (phy_dev->link)
890  fec_restart(ndev, phy_dev->duplex);
891  else
892  fec_stop(ndev);
893  status_change = 1;
894  }
895 
896 spin_unlock:
897  spin_unlock_irqrestore(&fep->hw_lock, flags);
898 
899  if (status_change)
900  phy_print_status(phy_dev);
901 }
902 
903 static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
904 {
905  struct fec_enet_private *fep = bus->priv;
906  unsigned long time_left;
907 
908  fep->mii_timeout = 0;
909  init_completion(&fep->mdio_done);
910 
911  /* start a read op */
913  FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
914  FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
915 
916  /* wait for end of transfer */
917  time_left = wait_for_completion_timeout(&fep->mdio_done,
919  if (time_left == 0) {
920  fep->mii_timeout = 1;
921  printk(KERN_ERR "FEC: MDIO read timeout\n");
922  return -ETIMEDOUT;
923  }
924 
925  /* return value */
926  return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
927 }
928 
929 static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
930  u16 value)
931 {
932  struct fec_enet_private *fep = bus->priv;
933  unsigned long time_left;
934 
935  fep->mii_timeout = 0;
936  init_completion(&fep->mdio_done);
937 
938  /* start a write op */
940  FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
941  FEC_MMFR_TA | FEC_MMFR_DATA(value),
942  fep->hwp + FEC_MII_DATA);
943 
944  /* wait for end of transfer */
945  time_left = wait_for_completion_timeout(&fep->mdio_done,
947  if (time_left == 0) {
948  fep->mii_timeout = 1;
949  printk(KERN_ERR "FEC: MDIO write timeout\n");
950  return -ETIMEDOUT;
951  }
952 
953  return 0;
954 }
955 
956 static int fec_enet_mdio_reset(struct mii_bus *bus)
957 {
958  return 0;
959 }
960 
961 static int fec_enet_mii_probe(struct net_device *ndev)
962 {
963  struct fec_enet_private *fep = netdev_priv(ndev);
964  const struct platform_device_id *id_entry =
966  struct phy_device *phy_dev = NULL;
967  char mdio_bus_id[MII_BUS_ID_SIZE];
968  char phy_name[MII_BUS_ID_SIZE + 3];
969  int phy_id;
970  int dev_id = fep->dev_id;
971 
972  fep->phy_dev = NULL;
973 
974  /* check for attached phy */
975  for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
976  if ((fep->mii_bus->phy_mask & (1 << phy_id)))
977  continue;
978  if (fep->mii_bus->phy_map[phy_id] == NULL)
979  continue;
980  if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
981  continue;
982  if (dev_id--)
983  continue;
984  strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
985  break;
986  }
987 
988  if (phy_id >= PHY_MAX_ADDR) {
990  "%s: no PHY, assuming direct connection to switch\n",
991  ndev->name);
992  strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
993  phy_id = 0;
994  }
995 
996  snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
997  phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, 0,
998  fep->phy_interface);
999  if (IS_ERR(phy_dev)) {
1000  printk(KERN_ERR "%s: could not attach to PHY\n", ndev->name);
1001  return PTR_ERR(phy_dev);
1002  }
1003 
1004  /* mask with MAC supported features */
1005  if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT)
1006  phy_dev->supported &= PHY_GBIT_FEATURES;
1007  else
1008  phy_dev->supported &= PHY_BASIC_FEATURES;
1009 
1010  phy_dev->advertising = phy_dev->supported;
1011 
1012  fep->phy_dev = phy_dev;
1013  fep->link = 0;
1014  fep->full_duplex = 0;
1015 
1017  "%s: Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1018  ndev->name,
1019  fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1020  fep->phy_dev->irq);
1021 
1022  return 0;
1023 }
1024 
1025 static int fec_enet_mii_init(struct platform_device *pdev)
1026 {
1027  static struct mii_bus *fec0_mii_bus;
1028  struct net_device *ndev = platform_get_drvdata(pdev);
1029  struct fec_enet_private *fep = netdev_priv(ndev);
1030  const struct platform_device_id *id_entry =
1032  int err = -ENXIO, i;
1033 
1034  /*
1035  * The dual fec interfaces are not equivalent with enet-mac.
1036  * Here are the differences:
1037  *
1038  * - fec0 supports MII & RMII modes while fec1 only supports RMII
1039  * - fec0 acts as the 1588 time master while fec1 is slave
1040  * - external phys can only be configured by fec0
1041  *
1042  * That is to say fec1 can not work independently. It only works
1043  * when fec0 is working. The reason behind this design is that the
1044  * second interface is added primarily for Switch mode.
1045  *
1046  * Because of the last point above, both phys are attached on fec0
1047  * mdio interface in board design, and need to be configured by
1048  * fec0 mii_bus.
1049  */
1050  if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
1051  /* fec1 uses fec0 mii_bus */
1052  if (mii_cnt && fec0_mii_bus) {
1053  fep->mii_bus = fec0_mii_bus;
1054  mii_cnt++;
1055  return 0;
1056  }
1057  return -ENOENT;
1058  }
1059 
1060  fep->mii_timeout = 0;
1061 
1062  /*
1063  * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
1064  *
1065  * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1066  * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1067  * Reference Manual has an error on this, and gets fixed on i.MX6Q
1068  * document.
1069  */
1070  fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
1071  if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1072  fep->phy_speed--;
1073  fep->phy_speed <<= 1;
1074  writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1075 
1076  fep->mii_bus = mdiobus_alloc();
1077  if (fep->mii_bus == NULL) {
1078  err = -ENOMEM;
1079  goto err_out;
1080  }
1081 
1082  fep->mii_bus->name = "fec_enet_mii_bus";
1083  fep->mii_bus->read = fec_enet_mdio_read;
1084  fep->mii_bus->write = fec_enet_mdio_write;
1085  fep->mii_bus->reset = fec_enet_mdio_reset;
1086  snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1087  pdev->name, fep->dev_id + 1);
1088  fep->mii_bus->priv = fep;
1089  fep->mii_bus->parent = &pdev->dev;
1090 
1091  fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1092  if (!fep->mii_bus->irq) {
1093  err = -ENOMEM;
1094  goto err_out_free_mdiobus;
1095  }
1096 
1097  for (i = 0; i < PHY_MAX_ADDR; i++)
1098  fep->mii_bus->irq[i] = PHY_POLL;
1099 
1100  if (mdiobus_register(fep->mii_bus))
1101  goto err_out_free_mdio_irq;
1102 
1103  mii_cnt++;
1104 
1105  /* save fec0 mii_bus */
1106  if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1107  fec0_mii_bus = fep->mii_bus;
1108 
1109  return 0;
1110 
1111 err_out_free_mdio_irq:
1112  kfree(fep->mii_bus->irq);
1113 err_out_free_mdiobus:
1114  mdiobus_free(fep->mii_bus);
1115 err_out:
1116  return err;
1117 }
1118 
1119 static void fec_enet_mii_remove(struct fec_enet_private *fep)
1120 {
1121  if (--mii_cnt == 0) {
1123  kfree(fep->mii_bus->irq);
1124  mdiobus_free(fep->mii_bus);
1125  }
1126 }
1127 
1128 static int fec_enet_get_settings(struct net_device *ndev,
1129  struct ethtool_cmd *cmd)
1130 {
1131  struct fec_enet_private *fep = netdev_priv(ndev);
1132  struct phy_device *phydev = fep->phy_dev;
1133 
1134  if (!phydev)
1135  return -ENODEV;
1136 
1137  return phy_ethtool_gset(phydev, cmd);
1138 }
1139 
1140 static int fec_enet_set_settings(struct net_device *ndev,
1141  struct ethtool_cmd *cmd)
1142 {
1143  struct fec_enet_private *fep = netdev_priv(ndev);
1144  struct phy_device *phydev = fep->phy_dev;
1145 
1146  if (!phydev)
1147  return -ENODEV;
1148 
1149  return phy_ethtool_sset(phydev, cmd);
1150 }
1151 
1152 static void fec_enet_get_drvinfo(struct net_device *ndev,
1153  struct ethtool_drvinfo *info)
1154 {
1155  struct fec_enet_private *fep = netdev_priv(ndev);
1156 
1157  strcpy(info->driver, fep->pdev->dev.driver->name);
1158  strcpy(info->version, "Revision: 1.0");
1159  strcpy(info->bus_info, dev_name(&ndev->dev));
1160 }
1161 
1162 static const struct ethtool_ops fec_enet_ethtool_ops = {
1163  .get_settings = fec_enet_get_settings,
1164  .set_settings = fec_enet_set_settings,
1165  .get_drvinfo = fec_enet_get_drvinfo,
1166  .get_link = ethtool_op_get_link,
1167  .get_ts_info = ethtool_op_get_ts_info,
1168 };
1169 
1170 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
1171 {
1172  struct fec_enet_private *fep = netdev_priv(ndev);
1173  struct phy_device *phydev = fep->phy_dev;
1174 
1175  if (!netif_running(ndev))
1176  return -EINVAL;
1177 
1178  if (!phydev)
1179  return -ENODEV;
1180 
1181  return phy_mii_ioctl(phydev, rq, cmd);
1182 }
1183 
1184 static void fec_enet_free_buffers(struct net_device *ndev)
1185 {
1186  struct fec_enet_private *fep = netdev_priv(ndev);
1187  int i;
1188  struct sk_buff *skb;
1189  struct bufdesc *bdp;
1190 
1191  bdp = fep->rx_bd_base;
1192  for (i = 0; i < RX_RING_SIZE; i++) {
1193  skb = fep->rx_skbuff[i];
1194 
1195  if (bdp->cbd_bufaddr)
1196  dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1198  if (skb)
1199  dev_kfree_skb(skb);
1200  bdp++;
1201  }
1202 
1203  bdp = fep->tx_bd_base;
1204  for (i = 0; i < TX_RING_SIZE; i++)
1205  kfree(fep->tx_bounce[i]);
1206 }
1207 
1208 static int fec_enet_alloc_buffers(struct net_device *ndev)
1209 {
1210  struct fec_enet_private *fep = netdev_priv(ndev);
1211  int i;
1212  struct sk_buff *skb;
1213  struct bufdesc *bdp;
1214 
1215  bdp = fep->rx_bd_base;
1216  for (i = 0; i < RX_RING_SIZE; i++) {
1217  skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1218  if (!skb) {
1219  fec_enet_free_buffers(ndev);
1220  return -ENOMEM;
1221  }
1222  fep->rx_skbuff[i] = skb;
1223 
1224  bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1226  bdp->cbd_sc = BD_ENET_RX_EMPTY;
1227  bdp++;
1228  }
1229 
1230  /* Set the last buffer to wrap. */
1231  bdp--;
1232  bdp->cbd_sc |= BD_SC_WRAP;
1233 
1234  bdp = fep->tx_bd_base;
1235  for (i = 0; i < TX_RING_SIZE; i++) {
1237 
1238  bdp->cbd_sc = 0;
1239  bdp->cbd_bufaddr = 0;
1240  bdp++;
1241  }
1242 
1243  /* Set the last buffer to wrap. */
1244  bdp--;
1245  bdp->cbd_sc |= BD_SC_WRAP;
1246 
1247  return 0;
1248 }
1249 
1250 static int
1251 fec_enet_open(struct net_device *ndev)
1252 {
1253  struct fec_enet_private *fep = netdev_priv(ndev);
1254  int ret;
1255 
1256  /* I should reset the ring buffers here, but I don't yet know
1257  * a simple way to do that.
1258  */
1259 
1260  ret = fec_enet_alloc_buffers(ndev);
1261  if (ret)
1262  return ret;
1263 
1264  /* Probe and connect to PHY when open the interface */
1265  ret = fec_enet_mii_probe(ndev);
1266  if (ret) {
1267  fec_enet_free_buffers(ndev);
1268  return ret;
1269  }
1270  phy_start(fep->phy_dev);
1271  netif_start_queue(ndev);
1272  fep->opened = 1;
1273  return 0;
1274 }
1275 
1276 static int
1277 fec_enet_close(struct net_device *ndev)
1278 {
1279  struct fec_enet_private *fep = netdev_priv(ndev);
1280 
1281  /* Don't know what to do yet. */
1282  fep->opened = 0;
1283  netif_stop_queue(ndev);
1284  fec_stop(ndev);
1285 
1286  if (fep->phy_dev) {
1287  phy_stop(fep->phy_dev);
1288  phy_disconnect(fep->phy_dev);
1289  }
1290 
1291  fec_enet_free_buffers(ndev);
1292 
1293  return 0;
1294 }
1295 
1296 /* Set or clear the multicast filter for this adaptor.
1297  * Skeleton taken from sunlance driver.
1298  * The CPM Ethernet implementation allows Multicast as well as individual
1299  * MAC address filtering. Some of the drivers check to make sure it is
1300  * a group multicast address, and discard those that are not. I guess I
1301  * will do the same for now, but just remove the test if you want
1302  * individual filtering as well (do the upper net layers want or support
1303  * this kind of feature?).
1304  */
1305 
1306 #define HASH_BITS 6 /* #bits in hash */
1307 #define CRC32_POLY 0xEDB88320
1308 
1309 static void set_multicast_list(struct net_device *ndev)
1310 {
1311  struct fec_enet_private *fep = netdev_priv(ndev);
1312  struct netdev_hw_addr *ha;
1313  unsigned int i, bit, data, crc, tmp;
1314  unsigned char hash;
1315 
1316  if (ndev->flags & IFF_PROMISC) {
1317  tmp = readl(fep->hwp + FEC_R_CNTRL);
1318  tmp |= 0x8;
1319  writel(tmp, fep->hwp + FEC_R_CNTRL);
1320  return;
1321  }
1322 
1323  tmp = readl(fep->hwp + FEC_R_CNTRL);
1324  tmp &= ~0x8;
1325  writel(tmp, fep->hwp + FEC_R_CNTRL);
1326 
1327  if (ndev->flags & IFF_ALLMULTI) {
1328  /* Catch all multicast addresses, so set the
1329  * filter to all 1's
1330  */
1331  writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1332  writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1333 
1334  return;
1335  }
1336 
1337  /* Clear filter and add the addresses in hash register
1338  */
1339  writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1340  writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1341 
1342  netdev_for_each_mc_addr(ha, ndev) {
1343  /* calculate crc32 value of mac address */
1344  crc = 0xffffffff;
1345 
1346  for (i = 0; i < ndev->addr_len; i++) {
1347  data = ha->addr[i];
1348  for (bit = 0; bit < 8; bit++, data >>= 1) {
1349  crc = (crc >> 1) ^
1350  (((crc ^ data) & 1) ? CRC32_POLY : 0);
1351  }
1352  }
1353 
1354  /* only upper 6 bits (HASH_BITS) are used
1355  * which point to specific bit in he hash registers
1356  */
1357  hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1358 
1359  if (hash > 31) {
1360  tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1361  tmp |= 1 << (hash - 32);
1362  writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1363  } else {
1364  tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1365  tmp |= 1 << hash;
1366  writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1367  }
1368  }
1369 }
1370 
1371 /* Set a MAC change in hardware. */
1372 static int
1373 fec_set_mac_address(struct net_device *ndev, void *p)
1374 {
1375  struct fec_enet_private *fep = netdev_priv(ndev);
1376  struct sockaddr *addr = p;
1377 
1378  if (!is_valid_ether_addr(addr->sa_data))
1379  return -EADDRNOTAVAIL;
1380 
1381  memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
1382 
1383  writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1384  (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
1385  fep->hwp + FEC_ADDR_LOW);
1386  writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
1387  fep->hwp + FEC_ADDR_HIGH);
1388  return 0;
1389 }
1390 
1391 #ifdef CONFIG_NET_POLL_CONTROLLER
1392 
1399 void fec_poll_controller(struct net_device *dev)
1400 {
1401  int i;
1402  struct fec_enet_private *fep = netdev_priv(dev);
1403 
1404  for (i = 0; i < FEC_IRQ_NUM; i++) {
1405  if (fep->irq[i] > 0) {
1406  disable_irq(fep->irq[i]);
1407  fec_enet_interrupt(fep->irq[i], dev);
1408  enable_irq(fep->irq[i]);
1409  }
1410  }
1411 }
1412 #endif
1413 
1414 static const struct net_device_ops fec_netdev_ops = {
1415  .ndo_open = fec_enet_open,
1416  .ndo_stop = fec_enet_close,
1417  .ndo_start_xmit = fec_enet_start_xmit,
1418  .ndo_set_rx_mode = set_multicast_list,
1419  .ndo_change_mtu = eth_change_mtu,
1420  .ndo_validate_addr = eth_validate_addr,
1421  .ndo_tx_timeout = fec_timeout,
1422  .ndo_set_mac_address = fec_set_mac_address,
1423  .ndo_do_ioctl = fec_enet_ioctl,
1424 #ifdef CONFIG_NET_POLL_CONTROLLER
1425  .ndo_poll_controller = fec_poll_controller,
1426 #endif
1427 };
1428 
1429  /*
1430  * XXX: We need to clean up on failure exits here.
1431  *
1432  */
1433 static int fec_enet_init(struct net_device *ndev)
1434 {
1435  struct fec_enet_private *fep = netdev_priv(ndev);
1436  struct bufdesc *cbd_base;
1437  struct bufdesc *bdp;
1438  int i;
1439 
1440  /* Allocate memory for buffer descriptors. */
1441  cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1442  GFP_KERNEL);
1443  if (!cbd_base) {
1444  printk("FEC: allocate descriptor memory failed?\n");
1445  return -ENOMEM;
1446  }
1447 
1448  spin_lock_init(&fep->hw_lock);
1449 
1450  fep->netdev = ndev;
1451 
1452  /* Get the Ethernet address */
1453  fec_get_mac(ndev);
1454 
1455  /* Set receive and transmit descriptor base. */
1456  fep->rx_bd_base = cbd_base;
1457  fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1458 
1459  /* The FEC Ethernet specific entries in the device structure */
1460  ndev->watchdog_timeo = TX_TIMEOUT;
1461  ndev->netdev_ops = &fec_netdev_ops;
1462  ndev->ethtool_ops = &fec_enet_ethtool_ops;
1463 
1464  /* Initialize the receive buffer descriptors. */
1465  bdp = fep->rx_bd_base;
1466  for (i = 0; i < RX_RING_SIZE; i++) {
1467 
1468  /* Initialize the BD for every fragment in the page. */
1469  bdp->cbd_sc = 0;
1470  bdp++;
1471  }
1472 
1473  /* Set the last buffer to wrap */
1474  bdp--;
1475  bdp->cbd_sc |= BD_SC_WRAP;
1476 
1477  /* ...and the same for transmit */
1478  bdp = fep->tx_bd_base;
1479  for (i = 0; i < TX_RING_SIZE; i++) {
1480 
1481  /* Initialize the BD for every fragment in the page. */
1482  bdp->cbd_sc = 0;
1483  bdp->cbd_bufaddr = 0;
1484  bdp++;
1485  }
1486 
1487  /* Set the last buffer to wrap */
1488  bdp--;
1489  bdp->cbd_sc |= BD_SC_WRAP;
1490 
1491  fec_restart(ndev, 0);
1492 
1493  return 0;
1494 }
1495 
1496 #ifdef CONFIG_OF
1497 static int __devinit fec_get_phy_mode_dt(struct platform_device *pdev)
1498 {
1499  struct device_node *np = pdev->dev.of_node;
1500 
1501  if (np)
1502  return of_get_phy_mode(np);
1503 
1504  return -ENODEV;
1505 }
1506 
1507 static void __devinit fec_reset_phy(struct platform_device *pdev)
1508 {
1509  int err, phy_reset;
1510  int msec = 1;
1511  struct device_node *np = pdev->dev.of_node;
1512 
1513  if (!np)
1514  return;
1515 
1516  of_property_read_u32(np, "phy-reset-duration", &msec);
1517  /* A sane reset duration should not be longer than 1s */
1518  if (msec > 1000)
1519  msec = 1;
1520 
1521  phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
1522  err = devm_gpio_request_one(&pdev->dev, phy_reset,
1523  GPIOF_OUT_INIT_LOW, "phy-reset");
1524  if (err) {
1525  pr_debug("FEC: failed to get gpio phy-reset: %d\n", err);
1526  return;
1527  }
1528  msleep(msec);
1529  gpio_set_value(phy_reset, 1);
1530 }
1531 #else /* CONFIG_OF */
1532 static inline int fec_get_phy_mode_dt(struct platform_device *pdev)
1533 {
1534  return -ENODEV;
1535 }
1536 
1537 static inline void fec_reset_phy(struct platform_device *pdev)
1538 {
1539  /*
1540  * In case of platform probe, the reset has been done
1541  * by machine code.
1542  */
1543 }
1544 #endif /* CONFIG_OF */
1545 
1546 static int __devinit
1547 fec_probe(struct platform_device *pdev)
1548 {
1549  struct fec_enet_private *fep;
1550  struct fec_platform_data *pdata;
1551  struct net_device *ndev;
1552  int i, irq, ret = 0;
1553  struct resource *r;
1554  const struct of_device_id *of_id;
1555  static int dev_id;
1556  struct pinctrl *pinctrl;
1557  struct regulator *reg_phy;
1558 
1559  of_id = of_match_device(fec_dt_ids, &pdev->dev);
1560  if (of_id)
1561  pdev->id_entry = of_id->data;
1562 
1563  r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1564  if (!r)
1565  return -ENXIO;
1566 
1567  r = request_mem_region(r->start, resource_size(r), pdev->name);
1568  if (!r)
1569  return -EBUSY;
1570 
1571  /* Init network device */
1572  ndev = alloc_etherdev(sizeof(struct fec_enet_private));
1573  if (!ndev) {
1574  ret = -ENOMEM;
1575  goto failed_alloc_etherdev;
1576  }
1577 
1578  SET_NETDEV_DEV(ndev, &pdev->dev);
1579 
1580  /* setup board info structure */
1581  fep = netdev_priv(ndev);
1582 
1583  fep->hwp = ioremap(r->start, resource_size(r));
1584  fep->pdev = pdev;
1585  fep->dev_id = dev_id++;
1586 
1587  if (!fep->hwp) {
1588  ret = -ENOMEM;
1589  goto failed_ioremap;
1590  }
1591 
1592  platform_set_drvdata(pdev, ndev);
1593 
1594  ret = fec_get_phy_mode_dt(pdev);
1595  if (ret < 0) {
1596  pdata = pdev->dev.platform_data;
1597  if (pdata)
1598  fep->phy_interface = pdata->phy;
1599  else
1601  } else {
1602  fep->phy_interface = ret;
1603  }
1604 
1605  for (i = 0; i < FEC_IRQ_NUM; i++) {
1606  irq = platform_get_irq(pdev, i);
1607  if (irq < 0) {
1608  if (i)
1609  break;
1610  ret = irq;
1611  goto failed_irq;
1612  }
1613  ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1614  if (ret) {
1615  while (--i >= 0) {
1616  irq = platform_get_irq(pdev, i);
1617  free_irq(irq, ndev);
1618  }
1619  goto failed_irq;
1620  }
1621  }
1622 
1623  pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1624  if (IS_ERR(pinctrl)) {
1625  ret = PTR_ERR(pinctrl);
1626  goto failed_pin;
1627  }
1628 
1629  fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1630  if (IS_ERR(fep->clk_ipg)) {
1631  ret = PTR_ERR(fep->clk_ipg);
1632  goto failed_clk;
1633  }
1634 
1635  fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1636  if (IS_ERR(fep->clk_ahb)) {
1637  ret = PTR_ERR(fep->clk_ahb);
1638  goto failed_clk;
1639  }
1640 
1641  clk_prepare_enable(fep->clk_ahb);
1642  clk_prepare_enable(fep->clk_ipg);
1643 
1644  reg_phy = devm_regulator_get(&pdev->dev, "phy");
1645  if (!IS_ERR(reg_phy)) {
1646  ret = regulator_enable(reg_phy);
1647  if (ret) {
1648  dev_err(&pdev->dev,
1649  "Failed to enable phy regulator: %d\n", ret);
1650  goto failed_regulator;
1651  }
1652  }
1653 
1654  fec_reset_phy(pdev);
1655 
1656  ret = fec_enet_init(ndev);
1657  if (ret)
1658  goto failed_init;
1659 
1660  ret = fec_enet_mii_init(pdev);
1661  if (ret)
1662  goto failed_mii_init;
1663 
1664  /* Carrier starts down, phylib will bring it up */
1665  netif_carrier_off(ndev);
1666 
1667  ret = register_netdev(ndev);
1668  if (ret)
1669  goto failed_register;
1670 
1671  return 0;
1672 
1673 failed_register:
1674  fec_enet_mii_remove(fep);
1675 failed_mii_init:
1676 failed_init:
1677 failed_regulator:
1678  clk_disable_unprepare(fep->clk_ahb);
1679  clk_disable_unprepare(fep->clk_ipg);
1680 failed_pin:
1681 failed_clk:
1682  for (i = 0; i < FEC_IRQ_NUM; i++) {
1683  irq = platform_get_irq(pdev, i);
1684  if (irq > 0)
1685  free_irq(irq, ndev);
1686  }
1687 failed_irq:
1688  iounmap(fep->hwp);
1689 failed_ioremap:
1690  free_netdev(ndev);
1691 failed_alloc_etherdev:
1692  release_mem_region(r->start, resource_size(r));
1693 
1694  return ret;
1695 }
1696 
1697 static int __devexit
1698 fec_drv_remove(struct platform_device *pdev)
1699 {
1700  struct net_device *ndev = platform_get_drvdata(pdev);
1701  struct fec_enet_private *fep = netdev_priv(ndev);
1702  struct resource *r;
1703  int i;
1704 
1705  unregister_netdev(ndev);
1706  fec_enet_mii_remove(fep);
1707  for (i = 0; i < FEC_IRQ_NUM; i++) {
1708  int irq = platform_get_irq(pdev, i);
1709  if (irq > 0)
1710  free_irq(irq, ndev);
1711  }
1712  clk_disable_unprepare(fep->clk_ahb);
1713  clk_disable_unprepare(fep->clk_ipg);
1714  iounmap(fep->hwp);
1715  free_netdev(ndev);
1716 
1717  r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1718  BUG_ON(!r);
1719  release_mem_region(r->start, resource_size(r));
1720 
1721  platform_set_drvdata(pdev, NULL);
1722 
1723  return 0;
1724 }
1725 
1726 #ifdef CONFIG_PM
1727 static int
1728 fec_suspend(struct device *dev)
1729 {
1730  struct net_device *ndev = dev_get_drvdata(dev);
1731  struct fec_enet_private *fep = netdev_priv(ndev);
1732 
1733  if (netif_running(ndev)) {
1734  fec_stop(ndev);
1735  netif_device_detach(ndev);
1736  }
1737  clk_disable_unprepare(fep->clk_ahb);
1738  clk_disable_unprepare(fep->clk_ipg);
1739 
1740  return 0;
1741 }
1742 
1743 static int
1744 fec_resume(struct device *dev)
1745 {
1746  struct net_device *ndev = dev_get_drvdata(dev);
1747  struct fec_enet_private *fep = netdev_priv(ndev);
1748 
1749  clk_prepare_enable(fep->clk_ahb);
1750  clk_prepare_enable(fep->clk_ipg);
1751  if (netif_running(ndev)) {
1752  fec_restart(ndev, fep->full_duplex);
1753  netif_device_attach(ndev);
1754  }
1755 
1756  return 0;
1757 }
1758 
1759 static const struct dev_pm_ops fec_pm_ops = {
1760  .suspend = fec_suspend,
1761  .resume = fec_resume,
1762  .freeze = fec_suspend,
1763  .thaw = fec_resume,
1764  .poweroff = fec_suspend,
1765  .restore = fec_resume,
1766 };
1767 #endif
1768 
1769 static struct platform_driver fec_driver = {
1770  .driver = {
1771  .name = DRIVER_NAME,
1772  .owner = THIS_MODULE,
1773 #ifdef CONFIG_PM
1774  .pm = &fec_pm_ops,
1775 #endif
1776  .of_match_table = fec_dt_ids,
1777  },
1778  .id_table = fec_devtype,
1779  .probe = fec_probe,
1780  .remove = __devexit_p(fec_drv_remove),
1781 };
1782 
1783 module_platform_driver(fec_driver);
1784 
1785 MODULE_LICENSE("GPL");