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,
50 MAX_RX_DESCR * sizeof(struct macb_dma_desc),
51 &lp->rx_ring_dma, GFP_KERNEL);
53 netdev_err(dev, "unable to alloc rx ring DMA buffer\n");
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");
63 dma_free_coherent(&lp->pdev->dev,
64 MAX_RX_DESCR * sizeof(struct macb_dma_desc),
65 lp->rx_ring, lp->rx_ring_dma);
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;
77 /* Set the Wrap bit on the last descriptor */
78 lp->rx_ring[MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
80 /* Reset buffer index */
83 /* Program address of descriptor list in Rx Buffer Queue register */
84 macb_writel(lp, RBQP, lp->rx_ring_dma);
86 /* Enable Receive and Transmit */
87 ctl = macb_readl(lp, NCR);
88 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
93 /* Open the ethernet interface */
94 static int at91ether_open(struct net_device *dev)
96 struct macb *lp = netdev_priv(dev);
100 /* Clear internal statistics */
101 ctl = macb_readl(lp, NCR);
102 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
106 ret = at91ether_start(dev);
110 /* Enable MAC interrupts */
111 macb_writel(lp, IER, MACB_BIT(RCOMP) |
119 /* schedule a link state check */
120 phy_start(lp->phy_dev);
122 netif_start_queue(dev);
127 /* Close the interface */
128 static int at91ether_close(struct net_device *dev)
130 struct macb *lp = netdev_priv(dev);
133 /* Disable Receiver and Transmitter */
134 ctl = macb_readl(lp, NCR);
135 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
137 /* Disable MAC interrupts */
138 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
146 netif_stop_queue(dev);
148 dma_free_coherent(&lp->pdev->dev,
149 MAX_RX_DESCR * sizeof(struct macb_dma_desc),
150 lp->rx_ring, lp->rx_ring_dma);
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;
161 /* Transmit packet */
162 static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
164 struct macb *lp = netdev_priv(dev);
166 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
167 netif_stop_queue(dev);
169 /* Store packet information (to free when Tx completed) */
171 lp->skb_length = skb->len;
172 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
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);
181 netdev_err(dev, "%s called, but device is busy!\n", __func__);
182 return NETDEV_TX_BUSY;
188 /* Extract received frame from buffer descriptors and sent to upper layers.
189 * (Called from interrupt context)
191 static void at91ether_rx(struct net_device *dev)
193 struct macb *lp = netdev_priv(dev);
194 unsigned char *p_recv;
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);
204 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
206 skb->protocol = eth_type_trans(skb, dev);
207 lp->stats.rx_packets++;
208 lp->stats.rx_bytes += pktlen;
211 lp->stats.rx_dropped++;
214 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
215 lp->stats.multicast++;
217 /* reset ownership bit */
218 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
220 /* wrap after last buffer */
221 if (lp->rx_tail == MAX_RX_DESCR - 1)
228 /* MAC interrupt handler */
229 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
231 struct net_device *dev = dev_id;
232 struct macb *lp = netdev_priv(dev);
235 /* MAC Interrupt Status register indicates what interrupts are pending.
236 * It is automatically cleared once read.
238 intstatus = macb_readl(lp, ISR);
240 /* Receive complete */
241 if (intstatus & MACB_BIT(RCOMP))
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++;
251 dev_kfree_skb_irq(lp->skb);
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;
257 netif_wake_queue(dev);
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));
267 if (intstatus & MACB_BIT(ISR_ROVR))
268 netdev_err(dev, "ROVR error\n");
273 #ifdef CONFIG_NET_POLL_CONTROLLER
274 static void at91ether_poll_controller(struct net_device *dev)
278 local_irq_save(flags);
279 at91ether_interrupt(dev->irq, dev);
280 local_irq_restore(flags);
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,
299 #if defined(CONFIG_OF)
300 static const struct of_device_id at91ether_dt_ids[] = {
301 { .compatible = "cdns,at91rm9200-emac" },
302 { .compatible = "cdns,emac" },
306 MODULE_DEVICE_TABLE(of, at91ether_dt_ids);
308 static int at91ether_get_phy_mode_dt(struct platform_device *pdev)
310 struct device_node *np = pdev->dev.of_node;
313 return of_get_phy_mode(np);
318 static int at91ether_get_hwaddr_dt(struct macb *bp)
320 struct device_node *np = bp->pdev->dev.of_node;
323 const char *mac = of_get_mac_address(np);
325 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
333 static int at91ether_get_phy_mode_dt(struct platform_device *pdev)
337 static int at91ether_get_hwaddr_dt(struct macb *bp)
343 /* Detect MAC & PHY and perform ethernet interface initialization */
344 static int __init at91ether_probe(struct platform_device *pdev)
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;
355 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
359 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
360 if (IS_ERR(pinctrl)) {
361 res = PTR_ERR(pinctrl);
362 if (res == -EPROBE_DEFER)
365 dev_warn(&pdev->dev, "No pinctrl provided\n");
368 dev = alloc_etherdev(sizeof(struct macb));
372 lp = netdev_priv(dev);
375 spin_lock_init(&lp->lock);
377 /* physical base address */
378 dev->base_addr = regs->start;
379 lp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
386 lp->pclk = devm_clk_get(&pdev->dev, "ether_clk");
387 if (IS_ERR(lp->pclk)) {
388 res = PTR_ERR(lp->pclk);
391 clk_enable(lp->pclk);
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);
397 goto err_disable_clock;
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);
405 res = at91ether_get_hwaddr_dt(lp);
409 res = at91ether_get_phy_mode_dt(pdev);
411 if (board_data && board_data->is_rmii)
412 lp->phy_interface = PHY_INTERFACE_MODE_RMII;
414 lp->phy_interface = PHY_INTERFACE_MODE_MII;
416 lp->phy_interface = res;
419 macb_writel(lp, NCR, 0);
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);
425 macb_writel(lp, NCFGR, reg);
427 /* Register the network interface */
428 res = register_netdev(dev);
430 goto err_disable_clock;
432 if (macb_mii_init(lp) != 0)
433 goto err_out_unregister_netdev;
435 /* will be enabled in open() */
436 netif_carrier_off(dev);
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),
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);
449 err_out_unregister_netdev:
450 unregister_netdev(dev);
452 clk_disable(lp->pclk);
458 static int at91ether_remove(struct platform_device *pdev)
460 struct net_device *dev = platform_get_drvdata(pdev);
461 struct macb *lp = netdev_priv(dev);
464 phy_disconnect(lp->phy_dev);
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);
472 platform_set_drvdata(pdev, NULL);
478 static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
480 struct net_device *net_dev = platform_get_drvdata(pdev);
481 struct macb *lp = netdev_priv(net_dev);
483 if (netif_running(net_dev)) {
484 netif_stop_queue(net_dev);
485 netif_device_detach(net_dev);
487 clk_disable(lp->pclk);
492 static int at91ether_resume(struct platform_device *pdev)
494 struct net_device *net_dev = platform_get_drvdata(pdev);
495 struct macb *lp = netdev_priv(net_dev);
497 if (netif_running(net_dev)) {
498 clk_enable(lp->pclk);
500 netif_device_attach(net_dev);
501 netif_start_queue(net_dev);
506 #define at91ether_suspend NULL
507 #define at91ether_resume NULL
510 static struct platform_driver at91ether_driver = {
511 .remove = at91ether_remove,
512 .suspend = at91ether_suspend,
513 .resume = at91ether_resume,
515 .name = "at91_ether",
516 .owner = THIS_MODULE,
517 .of_match_table = of_match_ptr(at91ether_dt_ids),
521 module_platform_driver_probe(at91ether_driver, at91ether_probe);
523 MODULE_LICENSE("GPL");
524 MODULE_DESCRIPTION("AT91RM9200 EMAC Ethernet driver");
525 MODULE_AUTHOR("Andrew Victor");
526 MODULE_ALIAS("platform:at91_ether");