at91_ether.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 "macb.h"
  37. #define DRV_NAME "at91_ether"
  38. #define DRV_VERSION "1.0"
  39. /* 1518 rounded up */
  40. #define MAX_RBUFF_SZ 0x600
  41. /* max number of receive buffers */
  42. #define MAX_RX_DESCR 9
  43. /* ......................... ADDRESS MANAGEMENT ........................ */
  44. /*
  45. * NOTE: Your bootloader must always set the MAC address correctly before
  46. * booting into Linux.
  47. *
  48. * - It must always set the MAC address after reset, even if it doesn't
  49. * happen to access the Ethernet while it's booting. Some versions of
  50. * U-Boot on the AT91RM9200-DK do not do this.
  51. *
  52. * - Likewise it must store the addresses in the correct byte order.
  53. * MicroMonitor (uMon) on the CSB337 does this incorrectly (and
  54. * continues to do so, for bug-compatibility).
  55. */
  56. static short __init unpack_mac_address(struct net_device *dev, unsigned int hi, unsigned int lo)
  57. {
  58. struct macb *lp = netdev_priv(dev);
  59. char addr[6];
  60. if (lp->board_data.rev_eth_addr) {
  61. addr[5] = (lo & 0xff); /* The CSB337 bootloader stores the MAC the wrong-way around */
  62. addr[4] = (lo & 0xff00) >> 8;
  63. addr[3] = (lo & 0xff0000) >> 16;
  64. addr[2] = (lo & 0xff000000) >> 24;
  65. addr[1] = (hi & 0xff);
  66. addr[0] = (hi & 0xff00) >> 8;
  67. }
  68. else {
  69. addr[0] = (lo & 0xff);
  70. addr[1] = (lo & 0xff00) >> 8;
  71. addr[2] = (lo & 0xff0000) >> 16;
  72. addr[3] = (lo & 0xff000000) >> 24;
  73. addr[4] = (hi & 0xff);
  74. addr[5] = (hi & 0xff00) >> 8;
  75. }
  76. if (is_valid_ether_addr(addr)) {
  77. memcpy(dev->dev_addr, &addr, 6);
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. /*
  83. * Set the ethernet MAC address in dev->dev_addr
  84. */
  85. static void __init get_mac_address(struct net_device *dev)
  86. {
  87. struct macb *lp = netdev_priv(dev);
  88. /* Check Specific-Address 1 */
  89. if (unpack_mac_address(dev, macb_readl(lp, SA1T), macb_readl(lp, SA1B)))
  90. return;
  91. /* Check Specific-Address 2 */
  92. if (unpack_mac_address(dev, macb_readl(lp, SA2T), macb_readl(lp, SA2B)))
  93. return;
  94. /* Check Specific-Address 3 */
  95. if (unpack_mac_address(dev, macb_readl(lp, SA3T), macb_readl(lp, SA3B)))
  96. return;
  97. /* Check Specific-Address 4 */
  98. if (unpack_mac_address(dev, macb_readl(lp, SA4T), macb_readl(lp, SA4B)))
  99. return;
  100. printk(KERN_ERR "at91_ether: Your bootloader did not configure a MAC address.\n");
  101. }
  102. /*
  103. * Program the hardware MAC address from dev->dev_addr.
  104. */
  105. static void update_mac_address(struct net_device *dev)
  106. {
  107. struct macb *lp = netdev_priv(dev);
  108. macb_writel(lp, SA1B, (dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16)
  109. | (dev->dev_addr[1] << 8) | (dev->dev_addr[0]));
  110. macb_writel(lp, SA1T, (dev->dev_addr[5] << 8) | (dev->dev_addr[4]));
  111. macb_writel(lp, SA2B, 0);
  112. macb_writel(lp, SA2T, 0);
  113. }
  114. /*
  115. * Store the new hardware address in dev->dev_addr, and update the MAC.
  116. */
  117. static int set_mac_address(struct net_device *dev, void* addr)
  118. {
  119. struct sockaddr *address = addr;
  120. if (!is_valid_ether_addr(address->sa_data))
  121. return -EADDRNOTAVAIL;
  122. memcpy(dev->dev_addr, address->sa_data, dev->addr_len);
  123. update_mac_address(dev);
  124. printk("%s: Setting MAC address to %pM\n", dev->name,
  125. dev->dev_addr);
  126. return 0;
  127. }
  128. /* ................................ MAC ................................ */
  129. /*
  130. * Initialize and start the Receiver and Transmit subsystems
  131. */
  132. static int at91ether_start(struct net_device *dev)
  133. {
  134. struct macb *lp = netdev_priv(dev);
  135. unsigned long ctl;
  136. dma_addr_t addr;
  137. int i;
  138. lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
  139. MAX_RX_DESCR * sizeof(struct macb_dma_desc),
  140. &lp->rx_ring_dma, GFP_KERNEL);
  141. if (!lp->rx_ring) {
  142. netdev_err(lp->dev, "unable to alloc rx ring DMA buffer\n");
  143. return -ENOMEM;
  144. }
  145. lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
  146. MAX_RX_DESCR * MAX_RBUFF_SZ,
  147. &lp->rx_buffers_dma, GFP_KERNEL);
  148. if (!lp->rx_buffers) {
  149. netdev_err(lp->dev, "unable to alloc rx data DMA buffer\n");
  150. dma_free_coherent(&lp->pdev->dev,
  151. MAX_RX_DESCR * sizeof(struct macb_dma_desc),
  152. lp->rx_ring, lp->rx_ring_dma);
  153. lp->rx_ring = NULL;
  154. return -ENOMEM;
  155. }
  156. addr = lp->rx_buffers_dma;
  157. for (i = 0; i < MAX_RX_DESCR; i++) {
  158. lp->rx_ring[i].addr = addr;
  159. lp->rx_ring[i].ctrl = 0;
  160. addr += MAX_RBUFF_SZ;
  161. }
  162. /* Set the Wrap bit on the last descriptor */
  163. lp->rx_ring[MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
  164. /* Reset buffer index */
  165. lp->rx_tail = 0;
  166. /* Program address of descriptor list in Rx Buffer Queue register */
  167. macb_writel(lp, RBQP, lp->rx_ring_dma);
  168. /* Enable Receive and Transmit */
  169. ctl = macb_readl(lp, NCR);
  170. macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
  171. return 0;
  172. }
  173. /*
  174. * Open the ethernet interface
  175. */
  176. static int at91ether_open(struct net_device *dev)
  177. {
  178. struct macb *lp = netdev_priv(dev);
  179. unsigned long ctl;
  180. int ret;
  181. if (!is_valid_ether_addr(dev->dev_addr))
  182. return -EADDRNOTAVAIL;
  183. /* Clear internal statistics */
  184. ctl = macb_readl(lp, NCR);
  185. macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
  186. /* Update the MAC address (incase user has changed it) */
  187. update_mac_address(dev);
  188. ret = at91ether_start(dev);
  189. if (ret)
  190. return ret;
  191. /* Enable MAC interrupts */
  192. macb_writel(lp, IER, MACB_BIT(RCOMP) | MACB_BIT(RXUBR)
  193. | MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE) | MACB_BIT(TCOMP)
  194. | MACB_BIT(ISR_ROVR) | MACB_BIT(HRESP));
  195. /* schedule a link state check */
  196. phy_start(lp->phy_dev);
  197. netif_start_queue(dev);
  198. return 0;
  199. }
  200. /*
  201. * Close the interface
  202. */
  203. static int at91ether_close(struct net_device *dev)
  204. {
  205. struct macb *lp = netdev_priv(dev);
  206. unsigned long ctl;
  207. /* Disable Receiver and Transmitter */
  208. ctl = macb_readl(lp, NCR);
  209. macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
  210. /* Disable MAC interrupts */
  211. macb_writel(lp, IDR, MACB_BIT(RCOMP) | MACB_BIT(RXUBR)
  212. | MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)
  213. | MACB_BIT(TCOMP) | MACB_BIT(ISR_ROVR)
  214. | MACB_BIT(HRESP));
  215. netif_stop_queue(dev);
  216. dma_free_coherent(&lp->pdev->dev,
  217. MAX_RX_DESCR * sizeof(struct macb_dma_desc),
  218. lp->rx_ring, lp->rx_ring_dma);
  219. lp->rx_ring = NULL;
  220. dma_free_coherent(&lp->pdev->dev,
  221. MAX_RX_DESCR * MAX_RBUFF_SZ,
  222. lp->rx_buffers, lp->rx_buffers_dma);
  223. lp->rx_buffers = NULL;
  224. return 0;
  225. }
  226. /*
  227. * Transmit packet.
  228. */
  229. static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
  230. {
  231. struct macb *lp = netdev_priv(dev);
  232. if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
  233. netif_stop_queue(dev);
  234. /* Store packet information (to free when Tx completed) */
  235. lp->skb = skb;
  236. lp->skb_length = skb->len;
  237. lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
  238. dev->stats.tx_bytes += skb->len;
  239. /* Set address of the data in the Transmit Address register */
  240. macb_writel(lp, TAR, lp->skb_physaddr);
  241. /* Set length of the packet in the Transmit Control register */
  242. macb_writel(lp, TCR, skb->len);
  243. } else {
  244. printk(KERN_ERR "at91_ether.c: at91ether_start_xmit() called, but device is busy!\n");
  245. return NETDEV_TX_BUSY; /* if we return anything but zero, dev.c:1055 calls kfree_skb(skb)
  246. on this skb, he also reports -ENETDOWN and printk's, so either
  247. we free and return(0) or don't free and return 1 */
  248. }
  249. return NETDEV_TX_OK;
  250. }
  251. /*
  252. * Update the current statistics from the internal statistics registers.
  253. */
  254. static struct net_device_stats *at91ether_stats(struct net_device *dev)
  255. {
  256. struct macb *lp = netdev_priv(dev);
  257. int ale, lenerr, seqe, lcol, ecol;
  258. if (netif_running(dev)) {
  259. dev->stats.rx_packets += macb_readl(lp, FRO); /* Good frames received */
  260. ale = macb_readl(lp, ALE);
  261. dev->stats.rx_frame_errors += ale; /* Alignment errors */
  262. lenerr = macb_readl(lp, ELE) + macb_readl(lp, USF);
  263. dev->stats.rx_length_errors += lenerr; /* Excessive Length or Undersize Frame error */
  264. seqe = macb_readl(lp, FCSE);
  265. dev->stats.rx_crc_errors += seqe; /* CRC error */
  266. dev->stats.rx_fifo_errors += macb_readl(lp, RRE);/* Receive buffer not available */
  267. dev->stats.rx_errors += (ale + lenerr + seqe
  268. + macb_readl(lp, RSE) + macb_readl(lp, RJA));
  269. dev->stats.tx_packets += macb_readl(lp, FTO); /* Frames successfully transmitted */
  270. dev->stats.tx_fifo_errors += macb_readl(lp, TUND); /* Transmit FIFO underruns */
  271. dev->stats.tx_carrier_errors += macb_readl(lp, CSE); /* Carrier Sense errors */
  272. dev->stats.tx_heartbeat_errors += macb_readl(lp, STE);/* Heartbeat error */
  273. lcol = macb_readl(lp, LCOL);
  274. ecol = macb_readl(lp, EXCOL);
  275. dev->stats.tx_window_errors += lcol; /* Late collisions */
  276. dev->stats.tx_aborted_errors += ecol; /* 16 collisions */
  277. dev->stats.collisions += (macb_readl(lp, SCF) + macb_readl(lp, MCF) + lcol + ecol);
  278. }
  279. return &dev->stats;
  280. }
  281. /*
  282. * Extract received frame from buffer descriptors and sent to upper layers.
  283. * (Called from interrupt context)
  284. */
  285. static void at91ether_rx(struct net_device *dev)
  286. {
  287. struct macb *lp = netdev_priv(dev);
  288. unsigned char *p_recv;
  289. struct sk_buff *skb;
  290. unsigned int pktlen;
  291. while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
  292. p_recv = lp->rx_buffers + lp->rx_tail * MAX_RBUFF_SZ;
  293. pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
  294. skb = netdev_alloc_skb(dev, pktlen + 2);
  295. if (skb) {
  296. skb_reserve(skb, 2);
  297. memcpy(skb_put(skb, pktlen), p_recv, pktlen);
  298. skb->protocol = eth_type_trans(skb, dev);
  299. dev->stats.rx_bytes += pktlen;
  300. netif_rx(skb);
  301. } else {
  302. dev->stats.rx_dropped += 1;
  303. netdev_notice(dev, "Memory squeeze, dropping packet.\n");
  304. }
  305. if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
  306. dev->stats.multicast++;
  307. /* reset ownership bit */
  308. lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
  309. /* wrap after last buffer */
  310. if (lp->rx_tail == MAX_RX_DESCR - 1)
  311. lp->rx_tail = 0;
  312. else
  313. lp->rx_tail++;
  314. }
  315. }
  316. /*
  317. * MAC interrupt handler
  318. */
  319. static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
  320. {
  321. struct net_device *dev = (struct net_device *) dev_id;
  322. struct macb *lp = netdev_priv(dev);
  323. unsigned long intstatus, ctl;
  324. /* MAC Interrupt Status register indicates what interrupts are pending.
  325. It is automatically cleared once read. */
  326. intstatus = macb_readl(lp, ISR);
  327. if (intstatus & MACB_BIT(RCOMP)) /* Receive complete */
  328. at91ether_rx(dev);
  329. if (intstatus & MACB_BIT(TCOMP)) { /* Transmit complete */
  330. /* The TCOM bit is set even if the transmission failed. */
  331. if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
  332. dev->stats.tx_errors += 1;
  333. if (lp->skb) {
  334. dev_kfree_skb_irq(lp->skb);
  335. lp->skb = NULL;
  336. dma_unmap_single(NULL, lp->skb_physaddr, lp->skb_length, DMA_TO_DEVICE);
  337. }
  338. netif_wake_queue(dev);
  339. }
  340. /* Work-around for Errata #11 */
  341. if (intstatus & MACB_BIT(RXUBR)) {
  342. ctl = macb_readl(lp, NCR);
  343. macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
  344. macb_writel(lp, NCR, ctl | MACB_BIT(RE));
  345. }
  346. if (intstatus & MACB_BIT(ISR_ROVR))
  347. printk("%s: ROVR error\n", dev->name);
  348. return IRQ_HANDLED;
  349. }
  350. #ifdef CONFIG_NET_POLL_CONTROLLER
  351. static void at91ether_poll_controller(struct net_device *dev)
  352. {
  353. unsigned long flags;
  354. local_irq_save(flags);
  355. at91ether_interrupt(dev->irq, dev);
  356. local_irq_restore(flags);
  357. }
  358. #endif
  359. static const struct net_device_ops at91ether_netdev_ops = {
  360. .ndo_open = at91ether_open,
  361. .ndo_stop = at91ether_close,
  362. .ndo_start_xmit = at91ether_start_xmit,
  363. .ndo_get_stats = at91ether_stats,
  364. .ndo_set_rx_mode = macb_set_rx_mode,
  365. .ndo_set_mac_address = set_mac_address,
  366. .ndo_do_ioctl = macb_ioctl,
  367. .ndo_validate_addr = eth_validate_addr,
  368. .ndo_change_mtu = eth_change_mtu,
  369. #ifdef CONFIG_NET_POLL_CONTROLLER
  370. .ndo_poll_controller = at91ether_poll_controller,
  371. #endif
  372. };
  373. #if defined(CONFIG_OF)
  374. static const struct of_device_id at91ether_dt_ids[] = {
  375. { .compatible = "cdns,at91rm9200-emac" },
  376. { .compatible = "cdns,emac" },
  377. { /* sentinel */ }
  378. };
  379. MODULE_DEVICE_TABLE(of, at91ether_dt_ids);
  380. static int at91ether_get_phy_mode_dt(struct platform_device *pdev)
  381. {
  382. struct device_node *np = pdev->dev.of_node;
  383. if (np)
  384. return of_get_phy_mode(np);
  385. return -ENODEV;
  386. }
  387. static int at91ether_get_hwaddr_dt(struct macb *bp)
  388. {
  389. struct device_node *np = bp->pdev->dev.of_node;
  390. if (np) {
  391. const char *mac = of_get_mac_address(np);
  392. if (mac) {
  393. memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
  394. return 0;
  395. }
  396. }
  397. return -ENODEV;
  398. }
  399. #else
  400. static int at91ether_get_phy_mode_dt(struct platform_device *pdev)
  401. {
  402. return -ENODEV;
  403. }
  404. static int at91ether_get_hwaddr_dt(struct macb *bp)
  405. {
  406. return -ENODEV;
  407. }
  408. #endif
  409. /*
  410. * Detect MAC & PHY and perform ethernet interface initialization
  411. */
  412. static int __init at91ether_probe(struct platform_device *pdev)
  413. {
  414. struct macb_platform_data *board_data = pdev->dev.platform_data;
  415. struct resource *regs;
  416. struct net_device *dev;
  417. struct phy_device *phydev;
  418. struct macb *lp;
  419. int res;
  420. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  421. if (!regs)
  422. return -ENOENT;
  423. dev = alloc_etherdev(sizeof(struct macb));
  424. if (!dev)
  425. return -ENOMEM;
  426. lp = netdev_priv(dev);
  427. lp->pdev = pdev;
  428. lp->dev = dev;
  429. if (board_data)
  430. lp->board_data = *board_data;
  431. spin_lock_init(&lp->lock);
  432. dev->base_addr = regs->start; /* physical base address */
  433. lp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
  434. if (!lp->regs) {
  435. res = -ENOMEM;
  436. goto err_free_dev;
  437. }
  438. /* Clock */
  439. lp->pclk = devm_clk_get(&pdev->dev, "ether_clk");
  440. if (IS_ERR(lp->pclk)) {
  441. res = PTR_ERR(lp->pclk);
  442. goto err_free_dev;
  443. }
  444. clk_enable(lp->pclk);
  445. /* Install the interrupt handler */
  446. dev->irq = platform_get_irq(pdev, 0);
  447. res = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt, 0, dev->name, dev);
  448. if (res)
  449. goto err_disable_clock;
  450. ether_setup(dev);
  451. dev->netdev_ops = &at91ether_netdev_ops;
  452. dev->ethtool_ops = &macb_ethtool_ops;
  453. platform_set_drvdata(pdev, dev);
  454. SET_NETDEV_DEV(dev, &pdev->dev);
  455. res = at91ether_get_hwaddr_dt(lp);
  456. if (res < 0)
  457. get_mac_address(dev); /* Get ethernet address and store it in dev->dev_addr */
  458. update_mac_address(dev); /* Program ethernet address into MAC */
  459. res = at91ether_get_phy_mode_dt(pdev);
  460. if (res < 0) {
  461. if (board_data && board_data->is_rmii)
  462. lp->phy_interface = PHY_INTERFACE_MODE_RMII;
  463. else
  464. lp->phy_interface = PHY_INTERFACE_MODE_MII;
  465. } else {
  466. lp->phy_interface = res;
  467. }
  468. macb_writel(lp, NCR, 0);
  469. if (lp->phy_interface == PHY_INTERFACE_MODE_RMII)
  470. macb_writel(lp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG) | MACB_BIT(RM9200_RMII));
  471. else
  472. macb_writel(lp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
  473. /* Register the network interface */
  474. res = register_netdev(dev);
  475. if (res)
  476. goto err_disable_clock;
  477. if (macb_mii_init(lp) != 0)
  478. goto err_out_unregister_netdev;
  479. netif_carrier_off(dev); /* will be enabled in open() */
  480. phydev = lp->phy_dev;
  481. netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  482. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  483. /* Display ethernet banner */
  484. printk(KERN_INFO "%s: AT91 ethernet at 0x%08x int=%d %s%s (%pM)\n",
  485. dev->name, (uint) dev->base_addr, dev->irq,
  486. macb_readl(lp, NCFGR) & MACB_BIT(SPD) ? "100-" : "10-",
  487. macb_readl(lp, NCFGR) & MACB_BIT(FD) ? "FullDuplex" : "HalfDuplex",
  488. dev->dev_addr);
  489. return 0;
  490. err_out_unregister_netdev:
  491. unregister_netdev(dev);
  492. err_disable_clock:
  493. clk_disable(lp->pclk);
  494. err_free_dev:
  495. free_netdev(dev);
  496. return res;
  497. }
  498. static int __devexit at91ether_remove(struct platform_device *pdev)
  499. {
  500. struct net_device *dev = platform_get_drvdata(pdev);
  501. struct macb *lp = netdev_priv(dev);
  502. if (lp->phy_dev)
  503. phy_disconnect(lp->phy_dev);
  504. mdiobus_unregister(lp->mii_bus);
  505. kfree(lp->mii_bus->irq);
  506. mdiobus_free(lp->mii_bus);
  507. unregister_netdev(dev);
  508. clk_disable(lp->pclk);
  509. free_netdev(dev);
  510. platform_set_drvdata(pdev, NULL);
  511. return 0;
  512. }
  513. #ifdef CONFIG_PM
  514. static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
  515. {
  516. struct net_device *net_dev = platform_get_drvdata(pdev);
  517. struct macb *lp = netdev_priv(net_dev);
  518. if (netif_running(net_dev)) {
  519. netif_stop_queue(net_dev);
  520. netif_device_detach(net_dev);
  521. clk_disable(lp->pclk);
  522. }
  523. return 0;
  524. }
  525. static int at91ether_resume(struct platform_device *pdev)
  526. {
  527. struct net_device *net_dev = platform_get_drvdata(pdev);
  528. struct macb *lp = netdev_priv(net_dev);
  529. if (netif_running(net_dev)) {
  530. clk_enable(lp->pclk);
  531. netif_device_attach(net_dev);
  532. netif_start_queue(net_dev);
  533. }
  534. return 0;
  535. }
  536. #else
  537. #define at91ether_suspend NULL
  538. #define at91ether_resume NULL
  539. #endif
  540. static struct platform_driver at91ether_driver = {
  541. .remove = __devexit_p(at91ether_remove),
  542. .suspend = at91ether_suspend,
  543. .resume = at91ether_resume,
  544. .driver = {
  545. .name = DRV_NAME,
  546. .owner = THIS_MODULE,
  547. .of_match_table = of_match_ptr(at91ether_dt_ids),
  548. },
  549. };
  550. static int __init at91ether_init(void)
  551. {
  552. return platform_driver_probe(&at91ether_driver, at91ether_probe);
  553. }
  554. static void __exit at91ether_exit(void)
  555. {
  556. platform_driver_unregister(&at91ether_driver);
  557. }
  558. module_init(at91ether_init)
  559. module_exit(at91ether_exit)
  560. MODULE_LICENSE("GPL");
  561. MODULE_DESCRIPTION("AT91RM9200 EMAC Ethernet driver");
  562. MODULE_AUTHOR("Andrew Victor");
  563. MODULE_ALIAS("platform:" DRV_NAME);