2 * Ethernet driver for the Atmel AT91RM9200 (Thunder)
4 * Copyright (C) 2003 SAN People (Pty) Ltd
6 * Based on an earlier Atmel EMAC macrocell driver by Atmel and Lineo Inc.
7 * Initial version by Rick Bronson 01/11/2003
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.
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>
30 #include <linux/of_device.h>
31 #include <linux/of_net.h>
32 #include <linux/pinctrl/consumer.h>
37 #define MAX_RBUFF_SZ 0x600
38 /* max number of receive buffers */
39 #define MAX_RX_DESCR 9
41 /* Initialize and start the Receiver and Transmit subsystems */
42 static int at91ether_start(struct net_device *dev)
44 struct macb *lp = netdev_priv(dev);
49 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
51 sizeof(struct macb_dma_desc)),
52 &lp->rx_ring_dma, GFP_KERNEL);
56 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
57 MAX_RX_DESCR * MAX_RBUFF_SZ,
58 &lp->rx_buffers_dma, GFP_KERNEL);
59 if (!lp->rx_buffers) {
60 dma_free_coherent(&lp->pdev->dev,
61 MAX_RX_DESCR * sizeof(struct macb_dma_desc),
62 lp->rx_ring, lp->rx_ring_dma);
67 addr = lp->rx_buffers_dma;
68 for (i = 0; i < MAX_RX_DESCR; i++) {
69 lp->rx_ring[i].addr = addr;
70 lp->rx_ring[i].ctrl = 0;
74 /* Set the Wrap bit on the last descriptor */
75 lp->rx_ring[MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
77 /* Reset buffer index */
80 /* Program address of descriptor list in Rx Buffer Queue register */
81 macb_writel(lp, RBQP, lp->rx_ring_dma);
83 /* Enable Receive and Transmit */
84 ctl = macb_readl(lp, NCR);
85 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
90 /* Open the ethernet interface */
91 static int at91ether_open(struct net_device *dev)
93 struct macb *lp = netdev_priv(dev);
97 /* Clear internal statistics */
98 ctl = macb_readl(lp, NCR);
99 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
103 ret = at91ether_start(dev);
107 /* Enable MAC interrupts */
108 macb_writel(lp, IER, MACB_BIT(RCOMP) |
116 /* schedule a link state check */
117 phy_start(lp->phy_dev);
119 netif_start_queue(dev);
124 /* Close the interface */
125 static int at91ether_close(struct net_device *dev)
127 struct macb *lp = netdev_priv(dev);
130 /* Disable Receiver and Transmitter */
131 ctl = macb_readl(lp, NCR);
132 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
134 /* Disable MAC interrupts */
135 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
143 netif_stop_queue(dev);
145 dma_free_coherent(&lp->pdev->dev,
146 MAX_RX_DESCR * sizeof(struct macb_dma_desc),
147 lp->rx_ring, lp->rx_ring_dma);
150 dma_free_coherent(&lp->pdev->dev,
151 MAX_RX_DESCR * MAX_RBUFF_SZ,
152 lp->rx_buffers, lp->rx_buffers_dma);
153 lp->rx_buffers = NULL;
158 /* Transmit packet */
159 static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
161 struct macb *lp = netdev_priv(dev);
163 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
164 netif_stop_queue(dev);
166 /* Store packet information (to free when Tx completed) */
168 lp->skb_length = skb->len;
169 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
172 /* Set address of the data in the Transmit Address register */
173 macb_writel(lp, TAR, lp->skb_physaddr);
174 /* Set length of the packet in the Transmit Control register */
175 macb_writel(lp, TCR, skb->len);
178 netdev_err(dev, "%s called, but device is busy!\n", __func__);
179 return NETDEV_TX_BUSY;
185 /* Extract received frame from buffer descriptors and sent to upper layers.
186 * (Called from interrupt context)
188 static void at91ether_rx(struct net_device *dev)
190 struct macb *lp = netdev_priv(dev);
191 unsigned char *p_recv;
195 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
196 p_recv = lp->rx_buffers + lp->rx_tail * MAX_RBUFF_SZ;
197 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
198 skb = netdev_alloc_skb(dev, pktlen + 2);
201 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
203 skb->protocol = eth_type_trans(skb, dev);
204 lp->stats.rx_packets++;
205 lp->stats.rx_bytes += pktlen;
208 lp->stats.rx_dropped++;
211 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
212 lp->stats.multicast++;
214 /* reset ownership bit */
215 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
217 /* wrap after last buffer */
218 if (lp->rx_tail == MAX_RX_DESCR - 1)
225 /* MAC interrupt handler */
226 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
228 struct net_device *dev = dev_id;
229 struct macb *lp = netdev_priv(dev);
232 /* MAC Interrupt Status register indicates what interrupts are pending.
233 * It is automatically cleared once read.
235 intstatus = macb_readl(lp, ISR);
237 /* Receive complete */
238 if (intstatus & MACB_BIT(RCOMP))
241 /* Transmit complete */
242 if (intstatus & MACB_BIT(TCOMP)) {
243 /* The TCOM bit is set even if the transmission failed */
244 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
245 lp->stats.tx_errors++;
248 dev_kfree_skb_irq(lp->skb);
250 dma_unmap_single(NULL, lp->skb_physaddr, lp->skb_length, DMA_TO_DEVICE);
251 lp->stats.tx_packets++;
252 lp->stats.tx_bytes += lp->skb_length;
254 netif_wake_queue(dev);
257 /* Work-around for EMAC Errata section 41.3.1 */
258 if (intstatus & MACB_BIT(RXUBR)) {
259 ctl = macb_readl(lp, NCR);
260 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
261 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
264 if (intstatus & MACB_BIT(ISR_ROVR))
265 netdev_err(dev, "ROVR error\n");
270 #ifdef CONFIG_NET_POLL_CONTROLLER
271 static void at91ether_poll_controller(struct net_device *dev)
275 local_irq_save(flags);
276 at91ether_interrupt(dev->irq, dev);
277 local_irq_restore(flags);
281 static const struct net_device_ops at91ether_netdev_ops = {
282 .ndo_open = at91ether_open,
283 .ndo_stop = at91ether_close,
284 .ndo_start_xmit = at91ether_start_xmit,
285 .ndo_get_stats = macb_get_stats,
286 .ndo_set_rx_mode = macb_set_rx_mode,
287 .ndo_set_mac_address = eth_mac_addr,
288 .ndo_do_ioctl = macb_ioctl,
289 .ndo_validate_addr = eth_validate_addr,
290 .ndo_change_mtu = eth_change_mtu,
291 #ifdef CONFIG_NET_POLL_CONTROLLER
292 .ndo_poll_controller = at91ether_poll_controller,
296 #if defined(CONFIG_OF)
297 static const struct of_device_id at91ether_dt_ids[] = {
298 { .compatible = "cdns,at91rm9200-emac" },
299 { .compatible = "cdns,emac" },
303 MODULE_DEVICE_TABLE(of, at91ether_dt_ids);
305 static int at91ether_get_phy_mode_dt(struct platform_device *pdev)
307 struct device_node *np = pdev->dev.of_node;
310 return of_get_phy_mode(np);
315 static int at91ether_get_hwaddr_dt(struct macb *bp)
317 struct device_node *np = bp->pdev->dev.of_node;
320 const char *mac = of_get_mac_address(np);
322 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
330 static int at91ether_get_phy_mode_dt(struct platform_device *pdev)
334 static int at91ether_get_hwaddr_dt(struct macb *bp)
340 /* Detect MAC & PHY and perform ethernet interface initialization */
341 static int __init at91ether_probe(struct platform_device *pdev)
343 struct macb_platform_data *board_data = pdev->dev.platform_data;
344 struct resource *regs;
345 struct net_device *dev;
346 struct phy_device *phydev;
347 struct pinctrl *pinctrl;
352 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
356 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
357 if (IS_ERR(pinctrl)) {
358 res = PTR_ERR(pinctrl);
359 if (res == -EPROBE_DEFER)
362 dev_warn(&pdev->dev, "No pinctrl provided\n");
365 dev = alloc_etherdev(sizeof(struct macb));
369 lp = netdev_priv(dev);
372 spin_lock_init(&lp->lock);
374 /* physical base address */
375 dev->base_addr = regs->start;
376 lp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
383 lp->pclk = devm_clk_get(&pdev->dev, "ether_clk");
384 if (IS_ERR(lp->pclk)) {
385 res = PTR_ERR(lp->pclk);
388 clk_enable(lp->pclk);
390 /* Install the interrupt handler */
391 dev->irq = platform_get_irq(pdev, 0);
392 res = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt, 0, dev->name, dev);
394 goto err_disable_clock;
397 dev->netdev_ops = &at91ether_netdev_ops;
398 dev->ethtool_ops = &macb_ethtool_ops;
399 platform_set_drvdata(pdev, dev);
400 SET_NETDEV_DEV(dev, &pdev->dev);
402 res = at91ether_get_hwaddr_dt(lp);
406 res = at91ether_get_phy_mode_dt(pdev);
408 if (board_data && board_data->is_rmii)
409 lp->phy_interface = PHY_INTERFACE_MODE_RMII;
411 lp->phy_interface = PHY_INTERFACE_MODE_MII;
413 lp->phy_interface = res;
416 macb_writel(lp, NCR, 0);
418 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
419 if (lp->phy_interface == PHY_INTERFACE_MODE_RMII)
420 reg |= MACB_BIT(RM9200_RMII);
422 macb_writel(lp, NCFGR, reg);
424 /* Register the network interface */
425 res = register_netdev(dev);
427 goto err_disable_clock;
429 if (macb_mii_init(lp) != 0)
430 goto err_out_unregister_netdev;
432 /* will be enabled in open() */
433 netif_carrier_off(dev);
435 phydev = lp->phy_dev;
436 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
437 phydev->drv->name, dev_name(&phydev->dev),
440 /* Display ethernet banner */
441 netdev_info(dev, "AT91 ethernet at 0x%08lx int=%d (%pM)\n",
442 dev->base_addr, dev->irq, dev->dev_addr);
446 err_out_unregister_netdev:
447 unregister_netdev(dev);
449 clk_disable(lp->pclk);
455 static int at91ether_remove(struct platform_device *pdev)
457 struct net_device *dev = platform_get_drvdata(pdev);
458 struct macb *lp = netdev_priv(dev);
461 phy_disconnect(lp->phy_dev);
463 mdiobus_unregister(lp->mii_bus);
464 kfree(lp->mii_bus->irq);
465 mdiobus_free(lp->mii_bus);
466 unregister_netdev(dev);
467 clk_disable(lp->pclk);
469 platform_set_drvdata(pdev, NULL);
475 static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
477 struct net_device *net_dev = platform_get_drvdata(pdev);
478 struct macb *lp = netdev_priv(net_dev);
480 if (netif_running(net_dev)) {
481 netif_stop_queue(net_dev);
482 netif_device_detach(net_dev);
484 clk_disable(lp->pclk);
489 static int at91ether_resume(struct platform_device *pdev)
491 struct net_device *net_dev = platform_get_drvdata(pdev);
492 struct macb *lp = netdev_priv(net_dev);
494 if (netif_running(net_dev)) {
495 clk_enable(lp->pclk);
497 netif_device_attach(net_dev);
498 netif_start_queue(net_dev);
503 #define at91ether_suspend NULL
504 #define at91ether_resume NULL
507 static struct platform_driver at91ether_driver = {
508 .remove = at91ether_remove,
509 .suspend = at91ether_suspend,
510 .resume = at91ether_resume,
512 .name = "at91_ether",
513 .owner = THIS_MODULE,
514 .of_match_table = of_match_ptr(at91ether_dt_ids),
518 module_platform_driver_probe(at91ether_driver, at91ether_probe);
520 MODULE_LICENSE("GPL");
521 MODULE_DESCRIPTION("AT91RM9200 EMAC Ethernet driver");
522 MODULE_AUTHOR("Andrew Victor");
523 MODULE_ALIAS("platform:at91_ether");