at91_ether.c 15 KB

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