]> Pileus Git - ~andy/linux/blob - drivers/net/ethernet/cadence/at91_ether.c
net/at91_ether: add pdata flag for reverse Eth addr
[~andy/linux] / drivers / net / ethernet / cadence / at91_ether.c
1 /*
2  * Ethernet driver for the Atmel AT91RM9200 (Thunder)
3  *
4  *  Copyright (C) 2003 SAN People (Pty) Ltd
5  *
6  * Based on an earlier Atmel EMAC macrocell driver by Atmel and Lineo Inc.
7  * Initial version by Rick Bronson 01/11/2003
8  *
9  * Intel LXT971A PHY support by Christopher Bahns & David Knickerbocker
10  *   (Polaroid Corporation)
11  *
12  * Realtek RTL8201(B)L PHY support by Roman Avramenko <roman@imsystems.ru>
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version
17  * 2 of the License, or (at your option) any later version.
18  */
19
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/skbuff.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/ethtool.h>
28 #include <linux/platform_data/macb.h>
29 #include <linux/platform_device.h>
30 #include <linux/clk.h>
31 #include <linux/gfp.h>
32 #include <linux/phy.h>
33 #include <linux/io.h>
34
35 #include "macb.h"
36
37 #define DRV_NAME        "at91_ether"
38 #define DRV_VERSION     "1.0"
39
40 /* 1518 rounded up */
41 #define MAX_RBUFF_SZ    0x600
42 /* max number of receive buffers */
43 #define MAX_RX_DESCR    9
44
45 /* ......................... ADDRESS MANAGEMENT ........................ */
46
47 /*
48  * NOTE: Your bootloader must always set the MAC address correctly before
49  * booting into Linux.
50  *
51  * - It must always set the MAC address after reset, even if it doesn't
52  *   happen to access the Ethernet while it's booting.  Some versions of
53  *   U-Boot on the AT91RM9200-DK do not do this.
54  *
55  * - Likewise it must store the addresses in the correct byte order.
56  *   MicroMonitor (uMon) on the CSB337 does this incorrectly (and
57  *   continues to do so, for bug-compatibility).
58  */
59
60 static short __init unpack_mac_address(struct net_device *dev, unsigned int hi, unsigned int lo)
61 {
62         struct macb *lp = netdev_priv(dev);
63         char addr[6];
64
65         if (lp->board_data.rev_eth_addr) {
66                 addr[5] = (lo & 0xff);                  /* The CSB337 bootloader stores the MAC the wrong-way around */
67                 addr[4] = (lo & 0xff00) >> 8;
68                 addr[3] = (lo & 0xff0000) >> 16;
69                 addr[2] = (lo & 0xff000000) >> 24;
70                 addr[1] = (hi & 0xff);
71                 addr[0] = (hi & 0xff00) >> 8;
72         }
73         else {
74                 addr[0] = (lo & 0xff);
75                 addr[1] = (lo & 0xff00) >> 8;
76                 addr[2] = (lo & 0xff0000) >> 16;
77                 addr[3] = (lo & 0xff000000) >> 24;
78                 addr[4] = (hi & 0xff);
79                 addr[5] = (hi & 0xff00) >> 8;
80         }
81
82         if (is_valid_ether_addr(addr)) {
83                 memcpy(dev->dev_addr, &addr, 6);
84                 return 1;
85         }
86         return 0;
87 }
88
89 /*
90  * Set the ethernet MAC address in dev->dev_addr
91  */
92 static void __init get_mac_address(struct net_device *dev)
93 {
94         struct macb *lp = netdev_priv(dev);
95
96         /* Check Specific-Address 1 */
97         if (unpack_mac_address(dev, macb_readl(lp, SA1T), macb_readl(lp, SA1B)))
98                 return;
99         /* Check Specific-Address 2 */
100         if (unpack_mac_address(dev, macb_readl(lp, SA2T), macb_readl(lp, SA2B)))
101                 return;
102         /* Check Specific-Address 3 */
103         if (unpack_mac_address(dev, macb_readl(lp, SA3T), macb_readl(lp, SA3B)))
104                 return;
105         /* Check Specific-Address 4 */
106         if (unpack_mac_address(dev, macb_readl(lp, SA4T), macb_readl(lp, SA4B)))
107                 return;
108
109         printk(KERN_ERR "at91_ether: Your bootloader did not configure a MAC address.\n");
110 }
111
112 /*
113  * Program the hardware MAC address from dev->dev_addr.
114  */
115 static void update_mac_address(struct net_device *dev)
116 {
117         struct macb *lp = netdev_priv(dev);
118
119         macb_writel(lp, SA1B, (dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16)
120                                         | (dev->dev_addr[1] << 8) | (dev->dev_addr[0]));
121         macb_writel(lp, SA1T, (dev->dev_addr[5] << 8) | (dev->dev_addr[4]));
122
123         macb_writel(lp, SA2B, 0);
124         macb_writel(lp, SA2T, 0);
125 }
126
127 /*
128  * Store the new hardware address in dev->dev_addr, and update the MAC.
129  */
130 static int set_mac_address(struct net_device *dev, void* addr)
131 {
132         struct sockaddr *address = addr;
133
134         if (!is_valid_ether_addr(address->sa_data))
135                 return -EADDRNOTAVAIL;
136
137         memcpy(dev->dev_addr, address->sa_data, dev->addr_len);
138         update_mac_address(dev);
139
140         printk("%s: Setting MAC address to %pM\n", dev->name,
141                dev->dev_addr);
142
143         return 0;
144 }
145
146 /* ................................ MAC ................................ */
147
148 /*
149  * Initialize and start the Receiver and Transmit subsystems
150  */
151 static int at91ether_start(struct net_device *dev)
152 {
153         struct macb *lp = netdev_priv(dev);
154         unsigned long ctl;
155         dma_addr_t addr;
156         int i;
157
158         lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
159                                         MAX_RX_DESCR * sizeof(struct dma_desc),
160                                         &lp->rx_ring_dma, GFP_KERNEL);
161         if (!lp->rx_ring) {
162                 netdev_err(lp->dev, "unable to alloc rx ring DMA buffer\n");
163                 return -ENOMEM;
164         }
165
166         lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
167                                         MAX_RX_DESCR * MAX_RBUFF_SZ,
168                                         &lp->rx_buffers_dma, GFP_KERNEL);
169         if (!lp->rx_buffers) {
170                 netdev_err(lp->dev, "unable to alloc rx data DMA buffer\n");
171
172                 dma_free_coherent(&lp->pdev->dev,
173                                         MAX_RX_DESCR * sizeof(struct dma_desc),
174                                         lp->rx_ring, lp->rx_ring_dma);
175                 lp->rx_ring = NULL;
176                 return -ENOMEM;
177         }
178
179         addr = lp->rx_buffers_dma;
180         for (i = 0; i < MAX_RX_DESCR; i++) {
181                 lp->rx_ring[i].addr = addr;
182                 lp->rx_ring[i].ctrl = 0;
183                 addr += MAX_RBUFF_SZ;
184         }
185
186         /* Set the Wrap bit on the last descriptor */
187         lp->rx_ring[MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
188
189         /* Reset buffer index */
190         lp->rx_tail = 0;
191
192         /* Program address of descriptor list in Rx Buffer Queue register */
193         macb_writel(lp, RBQP, lp->rx_ring_dma);
194
195         /* Enable Receive and Transmit */
196         ctl = macb_readl(lp, NCR);
197         macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
198
199         return 0;
200 }
201
202 /*
203  * Open the ethernet interface
204  */
205 static int at91ether_open(struct net_device *dev)
206 {
207         struct macb *lp = netdev_priv(dev);
208         unsigned long ctl;
209         int ret;
210
211         if (!is_valid_ether_addr(dev->dev_addr))
212                 return -EADDRNOTAVAIL;
213
214         /* Clear internal statistics */
215         ctl = macb_readl(lp, NCR);
216         macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
217
218         /* Update the MAC address (incase user has changed it) */
219         update_mac_address(dev);
220
221         ret = at91ether_start(dev);
222         if (ret)
223                 return ret;
224
225         /* Enable MAC interrupts */
226         macb_writel(lp, IER, MACB_BIT(RCOMP) | MACB_BIT(RXUBR)
227                                 | MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE) | MACB_BIT(TCOMP)
228                                 | MACB_BIT(ISR_ROVR) | MACB_BIT(HRESP));
229
230         /* schedule a link state check */
231         phy_start(lp->phy_dev);
232
233         netif_start_queue(dev);
234
235         return 0;
236 }
237
238 /*
239  * Close the interface
240  */
241 static int at91ether_close(struct net_device *dev)
242 {
243         struct macb *lp = netdev_priv(dev);
244         unsigned long ctl;
245
246         /* Disable Receiver and Transmitter */
247         ctl = macb_readl(lp, NCR);
248         macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
249
250         /* Disable MAC interrupts */
251         macb_writel(lp, IDR, MACB_BIT(RCOMP) | MACB_BIT(RXUBR)
252                                 | MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)
253                                 | MACB_BIT(TCOMP) | MACB_BIT(ISR_ROVR)
254                                 | MACB_BIT(HRESP));
255
256         netif_stop_queue(dev);
257
258         dma_free_coherent(&lp->pdev->dev,
259                                 MAX_RX_DESCR * sizeof(struct dma_desc),
260                                 lp->rx_ring, lp->rx_ring_dma);
261         lp->rx_ring = NULL;
262
263         dma_free_coherent(&lp->pdev->dev,
264                                 MAX_RX_DESCR * MAX_RBUFF_SZ,
265                                 lp->rx_buffers, lp->rx_buffers_dma);
266         lp->rx_buffers = NULL;
267
268         return 0;
269 }
270
271 /*
272  * Transmit packet.
273  */
274 static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
275 {
276         struct macb *lp = netdev_priv(dev);
277
278         if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
279                 netif_stop_queue(dev);
280
281                 /* Store packet information (to free when Tx completed) */
282                 lp->skb = skb;
283                 lp->skb_length = skb->len;
284                 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
285                 dev->stats.tx_bytes += skb->len;
286
287                 /* Set address of the data in the Transmit Address register */
288                 macb_writel(lp, TAR, lp->skb_physaddr);
289                 /* Set length of the packet in the Transmit Control register */
290                 macb_writel(lp, TCR, skb->len);
291
292         } else {
293                 printk(KERN_ERR "at91_ether.c: at91ether_start_xmit() called, but device is busy!\n");
294                 return NETDEV_TX_BUSY;  /* if we return anything but zero, dev.c:1055 calls kfree_skb(skb)
295                                 on this skb, he also reports -ENETDOWN and printk's, so either
296                                 we free and return(0) or don't free and return 1 */
297         }
298
299         return NETDEV_TX_OK;
300 }
301
302 /*
303  * Update the current statistics from the internal statistics registers.
304  */
305 static struct net_device_stats *at91ether_stats(struct net_device *dev)
306 {
307         struct macb *lp = netdev_priv(dev);
308         int ale, lenerr, seqe, lcol, ecol;
309
310         if (netif_running(dev)) {
311                 dev->stats.rx_packets += macb_readl(lp, FRO);   /* Good frames received */
312                 ale = macb_readl(lp, ALE);
313                 dev->stats.rx_frame_errors += ale;                              /* Alignment errors */
314                 lenerr = macb_readl(lp, ELE) + macb_readl(lp, USF);
315                 dev->stats.rx_length_errors += lenerr;                          /* Excessive Length or Undersize Frame error */
316                 seqe = macb_readl(lp, FCSE);
317                 dev->stats.rx_crc_errors += seqe;                               /* CRC error */
318                 dev->stats.rx_fifo_errors += macb_readl(lp, RRE);/* Receive buffer not available */
319                 dev->stats.rx_errors += (ale + lenerr + seqe
320                         + macb_readl(lp, RSE) + macb_readl(lp, RJA));
321
322                 dev->stats.tx_packets += macb_readl(lp, FTO);   /* Frames successfully transmitted */
323                 dev->stats.tx_fifo_errors += macb_readl(lp, TUND);      /* Transmit FIFO underruns */
324                 dev->stats.tx_carrier_errors += macb_readl(lp, CSE);    /* Carrier Sense errors */
325                 dev->stats.tx_heartbeat_errors += macb_readl(lp, STE);/* Heartbeat error */
326
327                 lcol = macb_readl(lp, LCOL);
328                 ecol = macb_readl(lp, EXCOL);
329                 dev->stats.tx_window_errors += lcol;                    /* Late collisions */
330                 dev->stats.tx_aborted_errors += ecol;                   /* 16 collisions */
331
332                 dev->stats.collisions += (macb_readl(lp, SCF) + macb_readl(lp, MCF) + lcol + ecol);
333         }
334         return &dev->stats;
335 }
336
337 /*
338  * Extract received frame from buffer descriptors and sent to upper layers.
339  * (Called from interrupt context)
340  */
341 static void at91ether_rx(struct net_device *dev)
342 {
343         struct macb *lp = netdev_priv(dev);
344         unsigned char *p_recv;
345         struct sk_buff *skb;
346         unsigned int pktlen;
347
348         while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
349                 p_recv = lp->rx_buffers + lp->rx_tail * MAX_RBUFF_SZ;
350                 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
351                 skb = netdev_alloc_skb(dev, pktlen + 2);
352                 if (skb) {
353                         skb_reserve(skb, 2);
354                         memcpy(skb_put(skb, pktlen), p_recv, pktlen);
355
356                         skb->protocol = eth_type_trans(skb, dev);
357                         dev->stats.rx_bytes += pktlen;
358                         netif_rx(skb);
359                 } else {
360                         dev->stats.rx_dropped += 1;
361                         netdev_notice(dev, "Memory squeeze, dropping packet.\n");
362                 }
363
364                 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
365                         dev->stats.multicast++;
366
367                 /* reset ownership bit */
368                 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
369
370                 /* wrap after last buffer */
371                 if (lp->rx_tail == MAX_RX_DESCR - 1)
372                         lp->rx_tail = 0;
373                 else
374                         lp->rx_tail++;
375         }
376 }
377
378 /*
379  * MAC interrupt handler
380  */
381 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
382 {
383         struct net_device *dev = (struct net_device *) dev_id;
384         struct macb *lp = netdev_priv(dev);
385         unsigned long intstatus, ctl;
386
387         /* MAC Interrupt Status register indicates what interrupts are pending.
388            It is automatically cleared once read. */
389         intstatus = macb_readl(lp, ISR);
390
391         if (intstatus & MACB_BIT(RCOMP))                /* Receive complete */
392                 at91ether_rx(dev);
393
394         if (intstatus & MACB_BIT(TCOMP)) {      /* Transmit complete */
395                 /* The TCOM bit is set even if the transmission failed. */
396                 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
397                         dev->stats.tx_errors += 1;
398
399                 if (lp->skb) {
400                         dev_kfree_skb_irq(lp->skb);
401                         lp->skb = NULL;
402                         dma_unmap_single(NULL, lp->skb_physaddr, lp->skb_length, DMA_TO_DEVICE);
403                 }
404                 netif_wake_queue(dev);
405         }
406
407         /* Work-around for Errata #11 */
408         if (intstatus & MACB_BIT(RXUBR)) {
409                 ctl = macb_readl(lp, NCR);
410                 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
411                 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
412         }
413
414         if (intstatus & MACB_BIT(ISR_ROVR))
415                 printk("%s: ROVR error\n", dev->name);
416
417         return IRQ_HANDLED;
418 }
419
420 #ifdef CONFIG_NET_POLL_CONTROLLER
421 static void at91ether_poll_controller(struct net_device *dev)
422 {
423         unsigned long flags;
424
425         local_irq_save(flags);
426         at91ether_interrupt(dev->irq, dev);
427         local_irq_restore(flags);
428 }
429 #endif
430
431 static const struct net_device_ops at91ether_netdev_ops = {
432         .ndo_open               = at91ether_open,
433         .ndo_stop               = at91ether_close,
434         .ndo_start_xmit         = at91ether_start_xmit,
435         .ndo_get_stats          = at91ether_stats,
436         .ndo_set_rx_mode        = macb_set_rx_mode,
437         .ndo_set_mac_address    = set_mac_address,
438         .ndo_do_ioctl           = macb_ioctl,
439         .ndo_validate_addr      = eth_validate_addr,
440         .ndo_change_mtu         = eth_change_mtu,
441 #ifdef CONFIG_NET_POLL_CONTROLLER
442         .ndo_poll_controller    = at91ether_poll_controller,
443 #endif
444 };
445
446 /*
447  * Detect MAC & PHY and perform ethernet interface initialization
448  */
449 static int __init at91ether_probe(struct platform_device *pdev)
450 {
451         struct macb_platform_data *board_data = pdev->dev.platform_data;
452         struct resource *regs;
453         struct net_device *dev;
454         struct phy_device *phydev;
455         struct macb *lp;
456         int res;
457
458         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
459         if (!regs)
460                 return -ENOENT;
461
462         dev = alloc_etherdev(sizeof(struct macb));
463         if (!dev)
464                 return -ENOMEM;
465
466         lp = netdev_priv(dev);
467         lp->pdev = pdev;
468         lp->dev = dev;
469         lp->board_data = *board_data;
470         spin_lock_init(&lp->lock);
471
472         dev->base_addr = regs->start;           /* physical base address */
473         lp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
474         if (!lp->regs) {
475                 res = -ENOMEM;
476                 goto err_free_dev;
477         }
478
479         /* Clock */
480         lp->pclk = devm_clk_get(&pdev->dev, "ether_clk");
481         if (IS_ERR(lp->pclk)) {
482                 res = PTR_ERR(lp->pclk);
483                 goto err_free_dev;
484         }
485         clk_enable(lp->pclk);
486
487         /* Install the interrupt handler */
488         dev->irq = platform_get_irq(pdev, 0);
489         res = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt, 0, dev->name, dev);
490         if (res)
491                 goto err_disable_clock;
492
493         ether_setup(dev);
494         dev->netdev_ops = &at91ether_netdev_ops;
495         dev->ethtool_ops = &macb_ethtool_ops;
496         platform_set_drvdata(pdev, dev);
497         SET_NETDEV_DEV(dev, &pdev->dev);
498
499         get_mac_address(dev);           /* Get ethernet address and store it in dev->dev_addr */
500         update_mac_address(dev);        /* Program ethernet address into MAC */
501
502         macb_writel(lp, NCR, 0);
503
504         if (board_data->is_rmii) {
505                 macb_writel(lp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG) | MACB_BIT(RM9200_RMII));
506                 lp->phy_interface = PHY_INTERFACE_MODE_RMII;
507         } else {
508                 macb_writel(lp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
509                 lp->phy_interface = PHY_INTERFACE_MODE_MII;
510         }
511
512         /* Register the network interface */
513         res = register_netdev(dev);
514         if (res)
515                 goto err_disable_clock;
516
517         if (macb_mii_init(lp) != 0)
518                 goto err_out_unregister_netdev;
519
520         netif_carrier_off(dev);         /* will be enabled in open() */
521
522         phydev = lp->phy_dev;
523         netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
524                 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
525
526         /* Display ethernet banner */
527         printk(KERN_INFO "%s: AT91 ethernet at 0x%08x int=%d %s%s (%pM)\n",
528                dev->name, (uint) dev->base_addr, dev->irq,
529                macb_readl(lp, NCFGR) & MACB_BIT(SPD) ? "100-" : "10-",
530                macb_readl(lp, NCFGR) & MACB_BIT(FD) ? "FullDuplex" : "HalfDuplex",
531                dev->dev_addr);
532
533         return 0;
534
535 err_out_unregister_netdev:
536         unregister_netdev(dev);
537 err_disable_clock:
538         clk_disable(lp->pclk);
539 err_free_dev:
540         free_netdev(dev);
541         return res;
542 }
543
544 static int __devexit at91ether_remove(struct platform_device *pdev)
545 {
546         struct net_device *dev = platform_get_drvdata(pdev);
547         struct macb *lp = netdev_priv(dev);
548
549         if (lp->phy_dev)
550                 phy_disconnect(lp->phy_dev);
551
552         mdiobus_unregister(lp->mii_bus);
553         kfree(lp->mii_bus->irq);
554         mdiobus_free(lp->mii_bus);
555         unregister_netdev(dev);
556         clk_disable(lp->pclk);
557         free_netdev(dev);
558         platform_set_drvdata(pdev, NULL);
559
560         return 0;
561 }
562
563 #ifdef CONFIG_PM
564
565 static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
566 {
567         struct net_device *net_dev = platform_get_drvdata(pdev);
568         struct macb *lp = netdev_priv(net_dev);
569
570         if (netif_running(net_dev)) {
571                 netif_stop_queue(net_dev);
572                 netif_device_detach(net_dev);
573
574                 clk_disable(lp->pclk);
575         }
576         return 0;
577 }
578
579 static int at91ether_resume(struct platform_device *pdev)
580 {
581         struct net_device *net_dev = platform_get_drvdata(pdev);
582         struct macb *lp = netdev_priv(net_dev);
583
584         if (netif_running(net_dev)) {
585                 clk_enable(lp->pclk);
586
587                 netif_device_attach(net_dev);
588                 netif_start_queue(net_dev);
589         }
590         return 0;
591 }
592
593 #else
594 #define at91ether_suspend       NULL
595 #define at91ether_resume        NULL
596 #endif
597
598 static struct platform_driver at91ether_driver = {
599         .remove         = __devexit_p(at91ether_remove),
600         .suspend        = at91ether_suspend,
601         .resume         = at91ether_resume,
602         .driver         = {
603                 .name   = DRV_NAME,
604                 .owner  = THIS_MODULE,
605         },
606 };
607
608 static int __init at91ether_init(void)
609 {
610         return platform_driver_probe(&at91ether_driver, at91ether_probe);
611 }
612
613 static void __exit at91ether_exit(void)
614 {
615         platform_driver_unregister(&at91ether_driver);
616 }
617
618 module_init(at91ether_init)
619 module_exit(at91ether_exit)
620
621 MODULE_LICENSE("GPL");
622 MODULE_DESCRIPTION("AT91RM9200 EMAC Ethernet driver");
623 MODULE_AUTHOR("Andrew Victor");
624 MODULE_ALIAS("platform:" DRV_NAME);