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