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