Version:  2.0.40 2.2.26 2.4.37 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17

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  * This program is free software; you can redistribute it and/or
 10  * modify it under the terms of the GNU General Public License
 11  * as published by the Free Software Foundation; either version
 12  * 2 of the License, or (at your option) any later version.
 13  */
 14 
 15 #include <linux/module.h>
 16 #include <linux/init.h>
 17 #include <linux/interrupt.h>
 18 #include <linux/netdevice.h>
 19 #include <linux/etherdevice.h>
 20 #include <linux/skbuff.h>
 21 #include <linux/dma-mapping.h>
 22 #include <linux/ethtool.h>
 23 #include <linux/platform_data/macb.h>
 24 #include <linux/platform_device.h>
 25 #include <linux/clk.h>
 26 #include <linux/gfp.h>
 27 #include <linux/phy.h>
 28 #include <linux/io.h>
 29 #include <linux/of.h>
 30 #include <linux/of_device.h>
 31 #include <linux/of_net.h>
 32 
 33 #include "macb.h"
 34 
 35 /* 1518 rounded up */
 36 #define MAX_RBUFF_SZ    0x600
 37 /* max number of receive buffers */
 38 #define MAX_RX_DESCR    9
 39 
 40 /* Initialize and start the Receiver and Transmit subsystems */
 41 static int at91ether_start(struct net_device *dev)
 42 {
 43         struct macb *lp = netdev_priv(dev);
 44         dma_addr_t addr;
 45         u32 ctl;
 46         int i;
 47 
 48         lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
 49                                          (MAX_RX_DESCR *
 50                                           sizeof(struct macb_dma_desc)),
 51                                          &lp->rx_ring_dma, GFP_KERNEL);
 52         if (!lp->rx_ring)
 53                 return -ENOMEM;
 54 
 55         lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
 56                                             MAX_RX_DESCR * MAX_RBUFF_SZ,
 57                                             &lp->rx_buffers_dma, GFP_KERNEL);
 58         if (!lp->rx_buffers) {
 59                 dma_free_coherent(&lp->pdev->dev,
 60                                   MAX_RX_DESCR * sizeof(struct macb_dma_desc),
 61                                   lp->rx_ring, lp->rx_ring_dma);
 62                 lp->rx_ring = NULL;
 63                 return -ENOMEM;
 64         }
 65 
 66         addr = lp->rx_buffers_dma;
 67         for (i = 0; i < MAX_RX_DESCR; i++) {
 68                 lp->rx_ring[i].addr = addr;
 69                 lp->rx_ring[i].ctrl = 0;
 70                 addr += MAX_RBUFF_SZ;
 71         }
 72 
 73         /* Set the Wrap bit on the last descriptor */
 74         lp->rx_ring[MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
 75 
 76         /* Reset buffer index */
 77         lp->rx_tail = 0;
 78 
 79         /* Program address of descriptor list in Rx Buffer Queue register */
 80         macb_writel(lp, RBQP, lp->rx_ring_dma);
 81 
 82         /* Enable Receive and Transmit */
 83         ctl = macb_readl(lp, NCR);
 84         macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
 85 
 86         return 0;
 87 }
 88 
 89 /* Open the ethernet interface */
 90 static int at91ether_open(struct net_device *dev)
 91 {
 92         struct macb *lp = netdev_priv(dev);
 93         u32 ctl;
 94         int ret;
 95 
 96         /* Clear internal statistics */
 97         ctl = macb_readl(lp, NCR);
 98         macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
 99 
100         macb_set_hwaddr(lp);
101 
102         ret = at91ether_start(dev);
103         if (ret)
104                 return ret;
105 
106         /* Enable MAC interrupts */
107         macb_writel(lp, IER, MACB_BIT(RCOMP)    |
108                              MACB_BIT(RXUBR)    |
109                              MACB_BIT(ISR_TUND) |
110                              MACB_BIT(ISR_RLE)  |
111                              MACB_BIT(TCOMP)    |
112                              MACB_BIT(ISR_ROVR) |
113                              MACB_BIT(HRESP));
114 
115         /* schedule a link state check */
116         phy_start(lp->phy_dev);
117 
118         netif_start_queue(dev);
119 
120         return 0;
121 }
122 
123 /* Close the interface */
124 static int at91ether_close(struct net_device *dev)
125 {
126         struct macb *lp = netdev_priv(dev);
127         u32 ctl;
128 
129         /* Disable Receiver and Transmitter */
130         ctl = macb_readl(lp, NCR);
131         macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
132 
133         /* Disable MAC interrupts */
134         macb_writel(lp, IDR, MACB_BIT(RCOMP)    |
135                              MACB_BIT(RXUBR)    |
136                              MACB_BIT(ISR_TUND) |
137                              MACB_BIT(ISR_RLE)  |
138                              MACB_BIT(TCOMP)    |
139                              MACB_BIT(ISR_ROVR) |
140                              MACB_BIT(HRESP));
141 
142         netif_stop_queue(dev);
143 
144         dma_free_coherent(&lp->pdev->dev,
145                                 MAX_RX_DESCR * sizeof(struct macb_dma_desc),
146                                 lp->rx_ring, lp->rx_ring_dma);
147         lp->rx_ring = NULL;
148 
149         dma_free_coherent(&lp->pdev->dev,
150                                 MAX_RX_DESCR * MAX_RBUFF_SZ,
151                                 lp->rx_buffers, lp->rx_buffers_dma);
152         lp->rx_buffers = NULL;
153 
154         return 0;
155 }
156 
157 /* Transmit packet */
158 static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
159 {
160         struct macb *lp = netdev_priv(dev);
161 
162         if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
163                 netif_stop_queue(dev);
164 
165                 /* Store packet information (to free when Tx completed) */
166                 lp->skb = skb;
167                 lp->skb_length = skb->len;
168                 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
169                                                         DMA_TO_DEVICE);
170 
171                 /* Set address of the data in the Transmit Address register */
172                 macb_writel(lp, TAR, lp->skb_physaddr);
173                 /* Set length of the packet in the Transmit Control register */
174                 macb_writel(lp, TCR, skb->len);
175 
176         } else {
177                 netdev_err(dev, "%s called, but device is busy!\n", __func__);
178                 return NETDEV_TX_BUSY;
179         }
180 
181         return NETDEV_TX_OK;
182 }
183 
184 /* Extract received frame from buffer descriptors and sent to upper layers.
185  * (Called from interrupt context)
186  */
187 static void at91ether_rx(struct net_device *dev)
188 {
189         struct macb *lp = netdev_priv(dev);
190         unsigned char *p_recv;
191         struct sk_buff *skb;
192         unsigned int pktlen;
193 
194         while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
195                 p_recv = lp->rx_buffers + lp->rx_tail * MAX_RBUFF_SZ;
196                 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
197                 skb = netdev_alloc_skb(dev, pktlen + 2);
198                 if (skb) {
199                         skb_reserve(skb, 2);
200                         memcpy(skb_put(skb, pktlen), p_recv, pktlen);
201 
202                         skb->protocol = eth_type_trans(skb, dev);
203                         lp->stats.rx_packets++;
204                         lp->stats.rx_bytes += pktlen;
205                         netif_rx(skb);
206                 } else {
207                         lp->stats.rx_dropped++;
208                 }
209 
210                 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
211                         lp->stats.multicast++;
212 
213                 /* reset ownership bit */
214                 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
215 
216                 /* wrap after last buffer */
217                 if (lp->rx_tail == MAX_RX_DESCR - 1)
218                         lp->rx_tail = 0;
219                 else
220                         lp->rx_tail++;
221         }
222 }
223 
224 /* MAC interrupt handler */
225 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
226 {
227         struct net_device *dev = dev_id;
228         struct macb *lp = netdev_priv(dev);
229         u32 intstatus, ctl;
230 
231         /* MAC Interrupt Status register indicates what interrupts are pending.
232          * It is automatically cleared once read.
233          */
234         intstatus = macb_readl(lp, ISR);
235 
236         /* Receive complete */
237         if (intstatus & MACB_BIT(RCOMP))
238                 at91ether_rx(dev);
239 
240         /* Transmit complete */
241         if (intstatus & MACB_BIT(TCOMP)) {
242                 /* The TCOM bit is set even if the transmission failed */
243                 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
244                         lp->stats.tx_errors++;
245 
246                 if (lp->skb) {
247                         dev_kfree_skb_irq(lp->skb);
248                         lp->skb = NULL;
249                         dma_unmap_single(NULL, lp->skb_physaddr, lp->skb_length, DMA_TO_DEVICE);
250                         lp->stats.tx_packets++;
251                         lp->stats.tx_bytes += lp->skb_length;
252                 }
253                 netif_wake_queue(dev);
254         }
255 
256         /* Work-around for EMAC Errata section 41.3.1 */
257         if (intstatus & MACB_BIT(RXUBR)) {
258                 ctl = macb_readl(lp, NCR);
259                 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
260                 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
261         }
262 
263         if (intstatus & MACB_BIT(ISR_ROVR))
264                 netdev_err(dev, "ROVR error\n");
265 
266         return IRQ_HANDLED;
267 }
268 
269 #ifdef CONFIG_NET_POLL_CONTROLLER
270 static void at91ether_poll_controller(struct net_device *dev)
271 {
272         unsigned long flags;
273 
274         local_irq_save(flags);
275         at91ether_interrupt(dev->irq, dev);
276         local_irq_restore(flags);
277 }
278 #endif
279 
280 static const struct net_device_ops at91ether_netdev_ops = {
281         .ndo_open               = at91ether_open,
282         .ndo_stop               = at91ether_close,
283         .ndo_start_xmit         = at91ether_start_xmit,
284         .ndo_get_stats          = macb_get_stats,
285         .ndo_set_rx_mode        = macb_set_rx_mode,
286         .ndo_set_mac_address    = eth_mac_addr,
287         .ndo_do_ioctl           = macb_ioctl,
288         .ndo_validate_addr      = eth_validate_addr,
289         .ndo_change_mtu         = eth_change_mtu,
290 #ifdef CONFIG_NET_POLL_CONTROLLER
291         .ndo_poll_controller    = at91ether_poll_controller,
292 #endif
293 };
294 
295 #if defined(CONFIG_OF)
296 static const struct of_device_id at91ether_dt_ids[] = {
297         { .compatible = "cdns,at91rm9200-emac" },
298         { .compatible = "cdns,emac" },
299         { /* sentinel */ }
300 };
301 MODULE_DEVICE_TABLE(of, at91ether_dt_ids);
302 #endif
303 
304 /* Detect MAC & PHY and perform ethernet interface initialization */
305 static int __init at91ether_probe(struct platform_device *pdev)
306 {
307         struct macb_platform_data *board_data = dev_get_platdata(&pdev->dev);
308         struct resource *regs;
309         struct net_device *dev;
310         struct phy_device *phydev;
311         struct macb *lp;
312         int res;
313         u32 reg;
314         const char *mac;
315 
316         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
317         if (!regs)
318                 return -ENOENT;
319 
320         dev = alloc_etherdev(sizeof(struct macb));
321         if (!dev)
322                 return -ENOMEM;
323 
324         lp = netdev_priv(dev);
325         lp->pdev = pdev;
326         lp->dev = dev;
327         spin_lock_init(&lp->lock);
328 
329         /* physical base address */
330         dev->base_addr = regs->start;
331         lp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
332         if (!lp->regs) {
333                 res = -ENOMEM;
334                 goto err_free_dev;
335         }
336 
337         /* Clock */
338         lp->pclk = devm_clk_get(&pdev->dev, "ether_clk");
339         if (IS_ERR(lp->pclk)) {
340                 res = PTR_ERR(lp->pclk);
341                 goto err_free_dev;
342         }
343         clk_enable(lp->pclk);
344 
345         lp->hclk = ERR_PTR(-ENOENT);
346         lp->tx_clk = ERR_PTR(-ENOENT);
347 
348         /* Install the interrupt handler */
349         dev->irq = platform_get_irq(pdev, 0);
350         res = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt, 0, dev->name, dev);
351         if (res)
352                 goto err_disable_clock;
353 
354         ether_setup(dev);
355         dev->netdev_ops = &at91ether_netdev_ops;
356         dev->ethtool_ops = &macb_ethtool_ops;
357         platform_set_drvdata(pdev, dev);
358         SET_NETDEV_DEV(dev, &pdev->dev);
359 
360         mac = of_get_mac_address(pdev->dev.of_node);
361         if (mac)
362                 memcpy(lp->dev->dev_addr, mac, ETH_ALEN);
363         else
364                 macb_get_hwaddr(lp);
365 
366         res = of_get_phy_mode(pdev->dev.of_node);
367         if (res < 0) {
368                 if (board_data && board_data->is_rmii)
369                         lp->phy_interface = PHY_INTERFACE_MODE_RMII;
370                 else
371                         lp->phy_interface = PHY_INTERFACE_MODE_MII;
372         } else {
373                 lp->phy_interface = res;
374         }
375 
376         macb_writel(lp, NCR, 0);
377 
378         reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
379         if (lp->phy_interface == PHY_INTERFACE_MODE_RMII)
380                 reg |= MACB_BIT(RM9200_RMII);
381 
382         macb_writel(lp, NCFGR, reg);
383 
384         /* Register the network interface */
385         res = register_netdev(dev);
386         if (res)
387                 goto err_disable_clock;
388 
389         res = macb_mii_init(lp);
390         if (res)
391                 goto err_out_unregister_netdev;
392 
393         /* will be enabled in open() */
394         netif_carrier_off(dev);
395 
396         phydev = lp->phy_dev;
397         netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
398                                 phydev->drv->name, dev_name(&phydev->dev),
399                                 phydev->irq);
400 
401         /* Display ethernet banner */
402         netdev_info(dev, "AT91 ethernet at 0x%08lx int=%d (%pM)\n",
403                                 dev->base_addr, dev->irq, dev->dev_addr);
404 
405         return 0;
406 
407 err_out_unregister_netdev:
408         unregister_netdev(dev);
409 err_disable_clock:
410         clk_disable(lp->pclk);
411 err_free_dev:
412         free_netdev(dev);
413         return res;
414 }
415 
416 static int at91ether_remove(struct platform_device *pdev)
417 {
418         struct net_device *dev = platform_get_drvdata(pdev);
419         struct macb *lp = netdev_priv(dev);
420 
421         if (lp->phy_dev)
422                 phy_disconnect(lp->phy_dev);
423 
424         mdiobus_unregister(lp->mii_bus);
425         kfree(lp->mii_bus->irq);
426         mdiobus_free(lp->mii_bus);
427         unregister_netdev(dev);
428         clk_disable(lp->pclk);
429         free_netdev(dev);
430 
431         return 0;
432 }
433 
434 #ifdef CONFIG_PM
435 static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
436 {
437         struct net_device *net_dev = platform_get_drvdata(pdev);
438         struct macb *lp = netdev_priv(net_dev);
439 
440         if (netif_running(net_dev)) {
441                 netif_stop_queue(net_dev);
442                 netif_device_detach(net_dev);
443 
444                 clk_disable(lp->pclk);
445         }
446         return 0;
447 }
448 
449 static int at91ether_resume(struct platform_device *pdev)
450 {
451         struct net_device *net_dev = platform_get_drvdata(pdev);
452         struct macb *lp = netdev_priv(net_dev);
453 
454         if (netif_running(net_dev)) {
455                 clk_enable(lp->pclk);
456 
457                 netif_device_attach(net_dev);
458                 netif_start_queue(net_dev);
459         }
460         return 0;
461 }
462 #else
463 #define at91ether_suspend       NULL
464 #define at91ether_resume        NULL
465 #endif
466 
467 static struct platform_driver at91ether_driver = {
468         .remove         = at91ether_remove,
469         .suspend        = at91ether_suspend,
470         .resume         = at91ether_resume,
471         .driver         = {
472                 .name   = "at91_ether",
473                 .owner  = THIS_MODULE,
474                 .of_match_table = of_match_ptr(at91ether_dt_ids),
475         },
476 };
477 
478 module_platform_driver_probe(at91ether_driver, at91ether_probe);
479 
480 MODULE_LICENSE("GPL");
481 MODULE_DESCRIPTION("AT91RM9200 EMAC Ethernet driver");
482 MODULE_AUTHOR("Andrew Victor");
483 MODULE_ALIAS("platform:at91_ether");
484 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us