macb.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /*
  2. * Atmel MACB Ethernet Controller driver
  3. *
  4. * Copyright (C) 2004-2006 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/phy.h>
  22. #include <mach/board.h>
  23. #include <mach/cpu.h>
  24. #include "macb.h"
  25. #define RX_BUFFER_SIZE 128
  26. #define RX_RING_SIZE 512
  27. #define RX_RING_BYTES (sizeof(struct dma_desc) * RX_RING_SIZE)
  28. /* Make the IP header word-aligned (the ethernet header is 14 bytes) */
  29. #define RX_OFFSET 2
  30. #define TX_RING_SIZE 128
  31. #define DEF_TX_RING_PENDING (TX_RING_SIZE - 1)
  32. #define TX_RING_BYTES (sizeof(struct dma_desc) * TX_RING_SIZE)
  33. #define TX_RING_GAP(bp) \
  34. (TX_RING_SIZE - (bp)->tx_pending)
  35. #define TX_BUFFS_AVAIL(bp) \
  36. (((bp)->tx_tail <= (bp)->tx_head) ? \
  37. (bp)->tx_tail + (bp)->tx_pending - (bp)->tx_head : \
  38. (bp)->tx_tail - (bp)->tx_head - TX_RING_GAP(bp))
  39. #define NEXT_TX(n) (((n) + 1) & (TX_RING_SIZE - 1))
  40. #define NEXT_RX(n) (((n) + 1) & (RX_RING_SIZE - 1))
  41. /* minimum number of free TX descriptors before waking up TX process */
  42. #define MACB_TX_WAKEUP_THRESH (TX_RING_SIZE / 4)
  43. #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
  44. | MACB_BIT(ISR_ROVR))
  45. static void __macb_set_hwaddr(struct macb *bp)
  46. {
  47. u32 bottom;
  48. u16 top;
  49. bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
  50. macb_writel(bp, SA1B, bottom);
  51. top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
  52. macb_writel(bp, SA1T, top);
  53. }
  54. static void __init macb_get_hwaddr(struct macb *bp)
  55. {
  56. u32 bottom;
  57. u16 top;
  58. u8 addr[6];
  59. bottom = macb_readl(bp, SA1B);
  60. top = macb_readl(bp, SA1T);
  61. addr[0] = bottom & 0xff;
  62. addr[1] = (bottom >> 8) & 0xff;
  63. addr[2] = (bottom >> 16) & 0xff;
  64. addr[3] = (bottom >> 24) & 0xff;
  65. addr[4] = top & 0xff;
  66. addr[5] = (top >> 8) & 0xff;
  67. if (is_valid_ether_addr(addr)) {
  68. memcpy(bp->dev->dev_addr, addr, sizeof(addr));
  69. } else {
  70. dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
  71. random_ether_addr(bp->dev->dev_addr);
  72. }
  73. }
  74. static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  75. {
  76. struct macb *bp = bus->priv;
  77. int value;
  78. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  79. | MACB_BF(RW, MACB_MAN_READ)
  80. | MACB_BF(PHYA, mii_id)
  81. | MACB_BF(REGA, regnum)
  82. | MACB_BF(CODE, MACB_MAN_CODE)));
  83. /* wait for end of transfer */
  84. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  85. cpu_relax();
  86. value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  87. return value;
  88. }
  89. static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  90. u16 value)
  91. {
  92. struct macb *bp = bus->priv;
  93. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  94. | MACB_BF(RW, MACB_MAN_WRITE)
  95. | MACB_BF(PHYA, mii_id)
  96. | MACB_BF(REGA, regnum)
  97. | MACB_BF(CODE, MACB_MAN_CODE)
  98. | MACB_BF(DATA, value)));
  99. /* wait for end of transfer */
  100. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  101. cpu_relax();
  102. return 0;
  103. }
  104. static int macb_mdio_reset(struct mii_bus *bus)
  105. {
  106. return 0;
  107. }
  108. static void macb_handle_link_change(struct net_device *dev)
  109. {
  110. struct macb *bp = netdev_priv(dev);
  111. struct phy_device *phydev = bp->phy_dev;
  112. unsigned long flags;
  113. int status_change = 0;
  114. spin_lock_irqsave(&bp->lock, flags);
  115. if (phydev->link) {
  116. if ((bp->speed != phydev->speed) ||
  117. (bp->duplex != phydev->duplex)) {
  118. u32 reg;
  119. reg = macb_readl(bp, NCFGR);
  120. reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  121. if (phydev->duplex)
  122. reg |= MACB_BIT(FD);
  123. if (phydev->speed == SPEED_100)
  124. reg |= MACB_BIT(SPD);
  125. macb_writel(bp, NCFGR, reg);
  126. bp->speed = phydev->speed;
  127. bp->duplex = phydev->duplex;
  128. status_change = 1;
  129. }
  130. }
  131. if (phydev->link != bp->link) {
  132. if (!phydev->link) {
  133. bp->speed = 0;
  134. bp->duplex = -1;
  135. }
  136. bp->link = phydev->link;
  137. status_change = 1;
  138. }
  139. spin_unlock_irqrestore(&bp->lock, flags);
  140. if (status_change) {
  141. if (phydev->link)
  142. printk(KERN_INFO "%s: link up (%d/%s)\n",
  143. dev->name, phydev->speed,
  144. DUPLEX_FULL == phydev->duplex ? "Full":"Half");
  145. else
  146. printk(KERN_INFO "%s: link down\n", dev->name);
  147. }
  148. }
  149. /* based on au1000_eth. c*/
  150. static int macb_mii_probe(struct net_device *dev)
  151. {
  152. struct macb *bp = netdev_priv(dev);
  153. struct phy_device *phydev = NULL;
  154. struct eth_platform_data *pdata;
  155. int phy_addr;
  156. /* find the first phy */
  157. for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
  158. if (bp->mii_bus->phy_map[phy_addr]) {
  159. phydev = bp->mii_bus->phy_map[phy_addr];
  160. break;
  161. }
  162. }
  163. if (!phydev) {
  164. printk (KERN_ERR "%s: no PHY found\n", dev->name);
  165. return -1;
  166. }
  167. pdata = bp->pdev->dev.platform_data;
  168. /* TODO : add pin_irq */
  169. /* attach the mac to the phy */
  170. if (pdata && pdata->is_rmii) {
  171. phydev = phy_connect(dev, phydev->dev.bus_id,
  172. &macb_handle_link_change, 0, PHY_INTERFACE_MODE_RMII);
  173. } else {
  174. phydev = phy_connect(dev, phydev->dev.bus_id,
  175. &macb_handle_link_change, 0, PHY_INTERFACE_MODE_MII);
  176. }
  177. if (IS_ERR(phydev)) {
  178. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  179. return PTR_ERR(phydev);
  180. }
  181. /* mask with MAC supported features */
  182. phydev->supported &= PHY_BASIC_FEATURES;
  183. phydev->advertising = phydev->supported;
  184. bp->link = 0;
  185. bp->speed = 0;
  186. bp->duplex = -1;
  187. bp->phy_dev = phydev;
  188. return 0;
  189. }
  190. static int macb_mii_init(struct macb *bp)
  191. {
  192. struct eth_platform_data *pdata;
  193. int err = -ENXIO, i;
  194. /* Enable managment port */
  195. macb_writel(bp, NCR, MACB_BIT(MPE));
  196. bp->mii_bus = mdiobus_alloc();
  197. if (bp->mii_bus == NULL) {
  198. err = -ENOMEM;
  199. goto err_out;
  200. }
  201. bp->mii_bus->name = "MACB_mii_bus";
  202. bp->mii_bus->read = &macb_mdio_read;
  203. bp->mii_bus->write = &macb_mdio_write;
  204. bp->mii_bus->reset = &macb_mdio_reset;
  205. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%x", bp->pdev->id);
  206. bp->mii_bus->priv = bp;
  207. bp->mii_bus->parent = &bp->dev->dev;
  208. pdata = bp->pdev->dev.platform_data;
  209. if (pdata)
  210. bp->mii_bus->phy_mask = pdata->phy_mask;
  211. bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  212. if (!bp->mii_bus->irq) {
  213. err = -ENOMEM;
  214. goto err_out_free_mdiobus;
  215. }
  216. for (i = 0; i < PHY_MAX_ADDR; i++)
  217. bp->mii_bus->irq[i] = PHY_POLL;
  218. platform_set_drvdata(bp->dev, bp->mii_bus);
  219. if (mdiobus_register(bp->mii_bus))
  220. goto err_out_free_mdio_irq;
  221. if (macb_mii_probe(bp->dev) != 0) {
  222. goto err_out_unregister_bus;
  223. }
  224. return 0;
  225. err_out_unregister_bus:
  226. mdiobus_unregister(bp->mii_bus);
  227. err_out_free_mdio_irq:
  228. kfree(bp->mii_bus->irq);
  229. err_out_free_mdiobus:
  230. mdiobus_free(bp->mii_bus);
  231. err_out:
  232. return err;
  233. }
  234. static void macb_update_stats(struct macb *bp)
  235. {
  236. u32 __iomem *reg = bp->regs + MACB_PFR;
  237. u32 *p = &bp->hw_stats.rx_pause_frames;
  238. u32 *end = &bp->hw_stats.tx_pause_frames + 1;
  239. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  240. for(; p < end; p++, reg++)
  241. *p += __raw_readl(reg);
  242. }
  243. static void macb_tx(struct macb *bp)
  244. {
  245. unsigned int tail;
  246. unsigned int head;
  247. u32 status;
  248. status = macb_readl(bp, TSR);
  249. macb_writel(bp, TSR, status);
  250. dev_dbg(&bp->pdev->dev, "macb_tx status = %02lx\n",
  251. (unsigned long)status);
  252. if (status & MACB_BIT(UND)) {
  253. int i;
  254. printk(KERN_ERR "%s: TX underrun, resetting buffers\n",
  255. bp->dev->name);
  256. head = bp->tx_head;
  257. /*Mark all the buffer as used to avoid sending a lost buffer*/
  258. for (i = 0; i < TX_RING_SIZE; i++)
  259. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  260. /* free transmit buffer in upper layer*/
  261. for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
  262. struct ring_info *rp = &bp->tx_skb[tail];
  263. struct sk_buff *skb = rp->skb;
  264. BUG_ON(skb == NULL);
  265. rmb();
  266. dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
  267. DMA_TO_DEVICE);
  268. rp->skb = NULL;
  269. dev_kfree_skb_irq(skb);
  270. }
  271. bp->tx_head = bp->tx_tail = 0;
  272. }
  273. if (!(status & MACB_BIT(COMP)))
  274. /*
  275. * This may happen when a buffer becomes complete
  276. * between reading the ISR and scanning the
  277. * descriptors. Nothing to worry about.
  278. */
  279. return;
  280. head = bp->tx_head;
  281. for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
  282. struct ring_info *rp = &bp->tx_skb[tail];
  283. struct sk_buff *skb = rp->skb;
  284. u32 bufstat;
  285. BUG_ON(skb == NULL);
  286. rmb();
  287. bufstat = bp->tx_ring[tail].ctrl;
  288. if (!(bufstat & MACB_BIT(TX_USED)))
  289. break;
  290. dev_dbg(&bp->pdev->dev, "skb %u (data %p) TX complete\n",
  291. tail, skb->data);
  292. dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
  293. DMA_TO_DEVICE);
  294. bp->stats.tx_packets++;
  295. bp->stats.tx_bytes += skb->len;
  296. rp->skb = NULL;
  297. dev_kfree_skb_irq(skb);
  298. }
  299. bp->tx_tail = tail;
  300. if (netif_queue_stopped(bp->dev) &&
  301. TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH)
  302. netif_wake_queue(bp->dev);
  303. }
  304. static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
  305. unsigned int last_frag)
  306. {
  307. unsigned int len;
  308. unsigned int frag;
  309. unsigned int offset = 0;
  310. struct sk_buff *skb;
  311. len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl);
  312. dev_dbg(&bp->pdev->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  313. first_frag, last_frag, len);
  314. skb = dev_alloc_skb(len + RX_OFFSET);
  315. if (!skb) {
  316. bp->stats.rx_dropped++;
  317. for (frag = first_frag; ; frag = NEXT_RX(frag)) {
  318. bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
  319. if (frag == last_frag)
  320. break;
  321. }
  322. wmb();
  323. return 1;
  324. }
  325. skb_reserve(skb, RX_OFFSET);
  326. skb->ip_summed = CHECKSUM_NONE;
  327. skb_put(skb, len);
  328. for (frag = first_frag; ; frag = NEXT_RX(frag)) {
  329. unsigned int frag_len = RX_BUFFER_SIZE;
  330. if (offset + frag_len > len) {
  331. BUG_ON(frag != last_frag);
  332. frag_len = len - offset;
  333. }
  334. skb_copy_to_linear_data_offset(skb, offset,
  335. (bp->rx_buffers +
  336. (RX_BUFFER_SIZE * frag)),
  337. frag_len);
  338. offset += RX_BUFFER_SIZE;
  339. bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
  340. wmb();
  341. if (frag == last_frag)
  342. break;
  343. }
  344. skb->protocol = eth_type_trans(skb, bp->dev);
  345. bp->stats.rx_packets++;
  346. bp->stats.rx_bytes += len;
  347. dev_dbg(&bp->pdev->dev, "received skb of length %u, csum: %08x\n",
  348. skb->len, skb->csum);
  349. netif_receive_skb(skb);
  350. return 0;
  351. }
  352. /* Mark DMA descriptors from begin up to and not including end as unused */
  353. static void discard_partial_frame(struct macb *bp, unsigned int begin,
  354. unsigned int end)
  355. {
  356. unsigned int frag;
  357. for (frag = begin; frag != end; frag = NEXT_RX(frag))
  358. bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
  359. wmb();
  360. /*
  361. * When this happens, the hardware stats registers for
  362. * whatever caused this is updated, so we don't have to record
  363. * anything.
  364. */
  365. }
  366. static int macb_rx(struct macb *bp, int budget)
  367. {
  368. int received = 0;
  369. unsigned int tail = bp->rx_tail;
  370. int first_frag = -1;
  371. for (; budget > 0; tail = NEXT_RX(tail)) {
  372. u32 addr, ctrl;
  373. rmb();
  374. addr = bp->rx_ring[tail].addr;
  375. ctrl = bp->rx_ring[tail].ctrl;
  376. if (!(addr & MACB_BIT(RX_USED)))
  377. break;
  378. if (ctrl & MACB_BIT(RX_SOF)) {
  379. if (first_frag != -1)
  380. discard_partial_frame(bp, first_frag, tail);
  381. first_frag = tail;
  382. }
  383. if (ctrl & MACB_BIT(RX_EOF)) {
  384. int dropped;
  385. BUG_ON(first_frag == -1);
  386. dropped = macb_rx_frame(bp, first_frag, tail);
  387. first_frag = -1;
  388. if (!dropped) {
  389. received++;
  390. budget--;
  391. }
  392. }
  393. }
  394. if (first_frag != -1)
  395. bp->rx_tail = first_frag;
  396. else
  397. bp->rx_tail = tail;
  398. return received;
  399. }
  400. static int macb_poll(struct napi_struct *napi, int budget)
  401. {
  402. struct macb *bp = container_of(napi, struct macb, napi);
  403. struct net_device *dev = bp->dev;
  404. int work_done;
  405. u32 status;
  406. status = macb_readl(bp, RSR);
  407. macb_writel(bp, RSR, status);
  408. work_done = 0;
  409. if (!status) {
  410. /*
  411. * This may happen if an interrupt was pending before
  412. * this function was called last time, and no packets
  413. * have been received since.
  414. */
  415. netif_rx_complete(napi);
  416. goto out;
  417. }
  418. dev_dbg(&bp->pdev->dev, "poll: status = %08lx, budget = %d\n",
  419. (unsigned long)status, budget);
  420. if (!(status & MACB_BIT(REC))) {
  421. dev_warn(&bp->pdev->dev,
  422. "No RX buffers complete, status = %02lx\n",
  423. (unsigned long)status);
  424. netif_rx_complete(napi);
  425. goto out;
  426. }
  427. work_done = macb_rx(bp, budget);
  428. if (work_done < budget)
  429. netif_rx_complete(napi);
  430. /*
  431. * We've done what we can to clean the buffers. Make sure we
  432. * get notified when new packets arrive.
  433. */
  434. out:
  435. macb_writel(bp, IER, MACB_RX_INT_FLAGS);
  436. /* TODO: Handle errors */
  437. return work_done;
  438. }
  439. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  440. {
  441. struct net_device *dev = dev_id;
  442. struct macb *bp = netdev_priv(dev);
  443. u32 status;
  444. status = macb_readl(bp, ISR);
  445. if (unlikely(!status))
  446. return IRQ_NONE;
  447. spin_lock(&bp->lock);
  448. while (status) {
  449. /* close possible race with dev_close */
  450. if (unlikely(!netif_running(dev))) {
  451. macb_writel(bp, IDR, ~0UL);
  452. break;
  453. }
  454. if (status & MACB_RX_INT_FLAGS) {
  455. if (netif_rx_schedule_prep(&bp->napi)) {
  456. /*
  457. * There's no point taking any more interrupts
  458. * until we have processed the buffers
  459. */
  460. macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
  461. dev_dbg(&bp->pdev->dev,
  462. "scheduling RX softirq\n");
  463. __netif_rx_schedule(&bp->napi);
  464. }
  465. }
  466. if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND)))
  467. macb_tx(bp);
  468. /*
  469. * Link change detection isn't possible with RMII, so we'll
  470. * add that if/when we get our hands on a full-blown MII PHY.
  471. */
  472. if (status & MACB_BIT(HRESP)) {
  473. /*
  474. * TODO: Reset the hardware, and maybe move the printk
  475. * to a lower-priority context as well (work queue?)
  476. */
  477. printk(KERN_ERR "%s: DMA bus error: HRESP not OK\n",
  478. dev->name);
  479. }
  480. status = macb_readl(bp, ISR);
  481. }
  482. spin_unlock(&bp->lock);
  483. return IRQ_HANDLED;
  484. }
  485. static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  486. {
  487. struct macb *bp = netdev_priv(dev);
  488. dma_addr_t mapping;
  489. unsigned int len, entry;
  490. u32 ctrl;
  491. #ifdef DEBUG
  492. int i;
  493. dev_dbg(&bp->pdev->dev,
  494. "start_xmit: len %u head %p data %p tail %p end %p\n",
  495. skb->len, skb->head, skb->data,
  496. skb_tail_pointer(skb), skb_end_pointer(skb));
  497. dev_dbg(&bp->pdev->dev,
  498. "data:");
  499. for (i = 0; i < 16; i++)
  500. printk(" %02x", (unsigned int)skb->data[i]);
  501. printk("\n");
  502. #endif
  503. len = skb->len;
  504. spin_lock_irq(&bp->lock);
  505. /* This is a hard error, log it. */
  506. if (TX_BUFFS_AVAIL(bp) < 1) {
  507. netif_stop_queue(dev);
  508. spin_unlock_irq(&bp->lock);
  509. dev_err(&bp->pdev->dev,
  510. "BUG! Tx Ring full when queue awake!\n");
  511. dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
  512. bp->tx_head, bp->tx_tail);
  513. return 1;
  514. }
  515. entry = bp->tx_head;
  516. dev_dbg(&bp->pdev->dev, "Allocated ring entry %u\n", entry);
  517. mapping = dma_map_single(&bp->pdev->dev, skb->data,
  518. len, DMA_TO_DEVICE);
  519. bp->tx_skb[entry].skb = skb;
  520. bp->tx_skb[entry].mapping = mapping;
  521. dev_dbg(&bp->pdev->dev, "Mapped skb data %p to DMA addr %08lx\n",
  522. skb->data, (unsigned long)mapping);
  523. ctrl = MACB_BF(TX_FRMLEN, len);
  524. ctrl |= MACB_BIT(TX_LAST);
  525. if (entry == (TX_RING_SIZE - 1))
  526. ctrl |= MACB_BIT(TX_WRAP);
  527. bp->tx_ring[entry].addr = mapping;
  528. bp->tx_ring[entry].ctrl = ctrl;
  529. wmb();
  530. entry = NEXT_TX(entry);
  531. bp->tx_head = entry;
  532. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  533. if (TX_BUFFS_AVAIL(bp) < 1)
  534. netif_stop_queue(dev);
  535. spin_unlock_irq(&bp->lock);
  536. dev->trans_start = jiffies;
  537. return 0;
  538. }
  539. static void macb_free_consistent(struct macb *bp)
  540. {
  541. if (bp->tx_skb) {
  542. kfree(bp->tx_skb);
  543. bp->tx_skb = NULL;
  544. }
  545. if (bp->rx_ring) {
  546. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
  547. bp->rx_ring, bp->rx_ring_dma);
  548. bp->rx_ring = NULL;
  549. }
  550. if (bp->tx_ring) {
  551. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
  552. bp->tx_ring, bp->tx_ring_dma);
  553. bp->tx_ring = NULL;
  554. }
  555. if (bp->rx_buffers) {
  556. dma_free_coherent(&bp->pdev->dev,
  557. RX_RING_SIZE * RX_BUFFER_SIZE,
  558. bp->rx_buffers, bp->rx_buffers_dma);
  559. bp->rx_buffers = NULL;
  560. }
  561. }
  562. static int macb_alloc_consistent(struct macb *bp)
  563. {
  564. int size;
  565. size = TX_RING_SIZE * sizeof(struct ring_info);
  566. bp->tx_skb = kmalloc(size, GFP_KERNEL);
  567. if (!bp->tx_skb)
  568. goto out_err;
  569. size = RX_RING_BYTES;
  570. bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  571. &bp->rx_ring_dma, GFP_KERNEL);
  572. if (!bp->rx_ring)
  573. goto out_err;
  574. dev_dbg(&bp->pdev->dev,
  575. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  576. size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
  577. size = TX_RING_BYTES;
  578. bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  579. &bp->tx_ring_dma, GFP_KERNEL);
  580. if (!bp->tx_ring)
  581. goto out_err;
  582. dev_dbg(&bp->pdev->dev,
  583. "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
  584. size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
  585. size = RX_RING_SIZE * RX_BUFFER_SIZE;
  586. bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  587. &bp->rx_buffers_dma, GFP_KERNEL);
  588. if (!bp->rx_buffers)
  589. goto out_err;
  590. dev_dbg(&bp->pdev->dev,
  591. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  592. size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
  593. return 0;
  594. out_err:
  595. macb_free_consistent(bp);
  596. return -ENOMEM;
  597. }
  598. static void macb_init_rings(struct macb *bp)
  599. {
  600. int i;
  601. dma_addr_t addr;
  602. addr = bp->rx_buffers_dma;
  603. for (i = 0; i < RX_RING_SIZE; i++) {
  604. bp->rx_ring[i].addr = addr;
  605. bp->rx_ring[i].ctrl = 0;
  606. addr += RX_BUFFER_SIZE;
  607. }
  608. bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
  609. for (i = 0; i < TX_RING_SIZE; i++) {
  610. bp->tx_ring[i].addr = 0;
  611. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  612. }
  613. bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  614. bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
  615. }
  616. static void macb_reset_hw(struct macb *bp)
  617. {
  618. /* Make sure we have the write buffer for ourselves */
  619. wmb();
  620. /*
  621. * Disable RX and TX (XXX: Should we halt the transmission
  622. * more gracefully?)
  623. */
  624. macb_writel(bp, NCR, 0);
  625. /* Clear the stats registers (XXX: Update stats first?) */
  626. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  627. /* Clear all status flags */
  628. macb_writel(bp, TSR, ~0UL);
  629. macb_writel(bp, RSR, ~0UL);
  630. /* Disable all interrupts */
  631. macb_writel(bp, IDR, ~0UL);
  632. macb_readl(bp, ISR);
  633. }
  634. static void macb_init_hw(struct macb *bp)
  635. {
  636. u32 config;
  637. macb_reset_hw(bp);
  638. __macb_set_hwaddr(bp);
  639. config = macb_readl(bp, NCFGR) & MACB_BF(CLK, -1L);
  640. config |= MACB_BIT(PAE); /* PAuse Enable */
  641. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  642. if (bp->dev->flags & IFF_PROMISC)
  643. config |= MACB_BIT(CAF); /* Copy All Frames */
  644. if (!(bp->dev->flags & IFF_BROADCAST))
  645. config |= MACB_BIT(NBC); /* No BroadCast */
  646. macb_writel(bp, NCFGR, config);
  647. /* Initialize TX and RX buffers */
  648. macb_writel(bp, RBQP, bp->rx_ring_dma);
  649. macb_writel(bp, TBQP, bp->tx_ring_dma);
  650. /* Enable TX and RX */
  651. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  652. /* Enable interrupts */
  653. macb_writel(bp, IER, (MACB_BIT(RCOMP)
  654. | MACB_BIT(RXUBR)
  655. | MACB_BIT(ISR_TUND)
  656. | MACB_BIT(ISR_RLE)
  657. | MACB_BIT(TXERR)
  658. | MACB_BIT(TCOMP)
  659. | MACB_BIT(ISR_ROVR)
  660. | MACB_BIT(HRESP)));
  661. }
  662. /*
  663. * The hash address register is 64 bits long and takes up two
  664. * locations in the memory map. The least significant bits are stored
  665. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  666. *
  667. * The unicast hash enable and the multicast hash enable bits in the
  668. * network configuration register enable the reception of hash matched
  669. * frames. The destination address is reduced to a 6 bit index into
  670. * the 64 bit hash register using the following hash function. The
  671. * hash function is an exclusive or of every sixth bit of the
  672. * destination address.
  673. *
  674. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  675. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  676. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  677. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  678. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  679. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  680. *
  681. * da[0] represents the least significant bit of the first byte
  682. * received, that is, the multicast/unicast indicator, and da[47]
  683. * represents the most significant bit of the last byte received. If
  684. * the hash index, hi[n], points to a bit that is set in the hash
  685. * register then the frame will be matched according to whether the
  686. * frame is multicast or unicast. A multicast match will be signalled
  687. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  688. * index points to a bit set in the hash register. A unicast match
  689. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  690. * and the hash index points to a bit set in the hash register. To
  691. * receive all multicast frames, the hash register should be set with
  692. * all ones and the multicast hash enable bit should be set in the
  693. * network configuration register.
  694. */
  695. static inline int hash_bit_value(int bitnr, __u8 *addr)
  696. {
  697. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  698. return 1;
  699. return 0;
  700. }
  701. /*
  702. * Return the hash index value for the specified address.
  703. */
  704. static int hash_get_index(__u8 *addr)
  705. {
  706. int i, j, bitval;
  707. int hash_index = 0;
  708. for (j = 0; j < 6; j++) {
  709. for (i = 0, bitval = 0; i < 8; i++)
  710. bitval ^= hash_bit_value(i*6 + j, addr);
  711. hash_index |= (bitval << j);
  712. }
  713. return hash_index;
  714. }
  715. /*
  716. * Add multicast addresses to the internal multicast-hash table.
  717. */
  718. static void macb_sethashtable(struct net_device *dev)
  719. {
  720. struct dev_mc_list *curr;
  721. unsigned long mc_filter[2];
  722. unsigned int i, bitnr;
  723. struct macb *bp = netdev_priv(dev);
  724. mc_filter[0] = mc_filter[1] = 0;
  725. curr = dev->mc_list;
  726. for (i = 0; i < dev->mc_count; i++, curr = curr->next) {
  727. if (!curr) break; /* unexpected end of list */
  728. bitnr = hash_get_index(curr->dmi_addr);
  729. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  730. }
  731. macb_writel(bp, HRB, mc_filter[0]);
  732. macb_writel(bp, HRT, mc_filter[1]);
  733. }
  734. /*
  735. * Enable/Disable promiscuous and multicast modes.
  736. */
  737. static void macb_set_rx_mode(struct net_device *dev)
  738. {
  739. unsigned long cfg;
  740. struct macb *bp = netdev_priv(dev);
  741. cfg = macb_readl(bp, NCFGR);
  742. if (dev->flags & IFF_PROMISC)
  743. /* Enable promiscuous mode */
  744. cfg |= MACB_BIT(CAF);
  745. else if (dev->flags & (~IFF_PROMISC))
  746. /* Disable promiscuous mode */
  747. cfg &= ~MACB_BIT(CAF);
  748. if (dev->flags & IFF_ALLMULTI) {
  749. /* Enable all multicast mode */
  750. macb_writel(bp, HRB, -1);
  751. macb_writel(bp, HRT, -1);
  752. cfg |= MACB_BIT(NCFGR_MTI);
  753. } else if (dev->mc_count > 0) {
  754. /* Enable specific multicasts */
  755. macb_sethashtable(dev);
  756. cfg |= MACB_BIT(NCFGR_MTI);
  757. } else if (dev->flags & (~IFF_ALLMULTI)) {
  758. /* Disable all multicast mode */
  759. macb_writel(bp, HRB, 0);
  760. macb_writel(bp, HRT, 0);
  761. cfg &= ~MACB_BIT(NCFGR_MTI);
  762. }
  763. macb_writel(bp, NCFGR, cfg);
  764. }
  765. static int macb_open(struct net_device *dev)
  766. {
  767. struct macb *bp = netdev_priv(dev);
  768. int err;
  769. dev_dbg(&bp->pdev->dev, "open\n");
  770. /* if the phy is not yet register, retry later*/
  771. if (!bp->phy_dev)
  772. return -EAGAIN;
  773. if (!is_valid_ether_addr(dev->dev_addr))
  774. return -EADDRNOTAVAIL;
  775. err = macb_alloc_consistent(bp);
  776. if (err) {
  777. printk(KERN_ERR
  778. "%s: Unable to allocate DMA memory (error %d)\n",
  779. dev->name, err);
  780. return err;
  781. }
  782. napi_enable(&bp->napi);
  783. macb_init_rings(bp);
  784. macb_init_hw(bp);
  785. /* schedule a link state check */
  786. phy_start(bp->phy_dev);
  787. netif_start_queue(dev);
  788. return 0;
  789. }
  790. static int macb_close(struct net_device *dev)
  791. {
  792. struct macb *bp = netdev_priv(dev);
  793. unsigned long flags;
  794. netif_stop_queue(dev);
  795. napi_disable(&bp->napi);
  796. if (bp->phy_dev)
  797. phy_stop(bp->phy_dev);
  798. spin_lock_irqsave(&bp->lock, flags);
  799. macb_reset_hw(bp);
  800. netif_carrier_off(dev);
  801. spin_unlock_irqrestore(&bp->lock, flags);
  802. macb_free_consistent(bp);
  803. return 0;
  804. }
  805. static struct net_device_stats *macb_get_stats(struct net_device *dev)
  806. {
  807. struct macb *bp = netdev_priv(dev);
  808. struct net_device_stats *nstat = &bp->stats;
  809. struct macb_stats *hwstat = &bp->hw_stats;
  810. /* read stats from hardware */
  811. macb_update_stats(bp);
  812. /* Convert HW stats into netdevice stats */
  813. nstat->rx_errors = (hwstat->rx_fcs_errors +
  814. hwstat->rx_align_errors +
  815. hwstat->rx_resource_errors +
  816. hwstat->rx_overruns +
  817. hwstat->rx_oversize_pkts +
  818. hwstat->rx_jabbers +
  819. hwstat->rx_undersize_pkts +
  820. hwstat->sqe_test_errors +
  821. hwstat->rx_length_mismatch);
  822. nstat->tx_errors = (hwstat->tx_late_cols +
  823. hwstat->tx_excessive_cols +
  824. hwstat->tx_underruns +
  825. hwstat->tx_carrier_errors);
  826. nstat->collisions = (hwstat->tx_single_cols +
  827. hwstat->tx_multiple_cols +
  828. hwstat->tx_excessive_cols);
  829. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  830. hwstat->rx_jabbers +
  831. hwstat->rx_undersize_pkts +
  832. hwstat->rx_length_mismatch);
  833. nstat->rx_over_errors = hwstat->rx_resource_errors;
  834. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  835. nstat->rx_frame_errors = hwstat->rx_align_errors;
  836. nstat->rx_fifo_errors = hwstat->rx_overruns;
  837. /* XXX: What does "missed" mean? */
  838. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  839. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  840. nstat->tx_fifo_errors = hwstat->tx_underruns;
  841. /* Don't know about heartbeat or window errors... */
  842. return nstat;
  843. }
  844. static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  845. {
  846. struct macb *bp = netdev_priv(dev);
  847. struct phy_device *phydev = bp->phy_dev;
  848. if (!phydev)
  849. return -ENODEV;
  850. return phy_ethtool_gset(phydev, cmd);
  851. }
  852. static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  853. {
  854. struct macb *bp = netdev_priv(dev);
  855. struct phy_device *phydev = bp->phy_dev;
  856. if (!phydev)
  857. return -ENODEV;
  858. return phy_ethtool_sset(phydev, cmd);
  859. }
  860. static void macb_get_drvinfo(struct net_device *dev,
  861. struct ethtool_drvinfo *info)
  862. {
  863. struct macb *bp = netdev_priv(dev);
  864. strcpy(info->driver, bp->pdev->dev.driver->name);
  865. strcpy(info->version, "$Revision: 1.14 $");
  866. strcpy(info->bus_info, bp->pdev->dev.bus_id);
  867. }
  868. static struct ethtool_ops macb_ethtool_ops = {
  869. .get_settings = macb_get_settings,
  870. .set_settings = macb_set_settings,
  871. .get_drvinfo = macb_get_drvinfo,
  872. .get_link = ethtool_op_get_link,
  873. };
  874. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  875. {
  876. struct macb *bp = netdev_priv(dev);
  877. struct phy_device *phydev = bp->phy_dev;
  878. if (!netif_running(dev))
  879. return -EINVAL;
  880. if (!phydev)
  881. return -ENODEV;
  882. return phy_mii_ioctl(phydev, if_mii(rq), cmd);
  883. }
  884. static int __init macb_probe(struct platform_device *pdev)
  885. {
  886. struct eth_platform_data *pdata;
  887. struct resource *regs;
  888. struct net_device *dev;
  889. struct macb *bp;
  890. struct phy_device *phydev;
  891. unsigned long pclk_hz;
  892. u32 config;
  893. int err = -ENXIO;
  894. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  895. if (!regs) {
  896. dev_err(&pdev->dev, "no mmio resource defined\n");
  897. goto err_out;
  898. }
  899. err = -ENOMEM;
  900. dev = alloc_etherdev(sizeof(*bp));
  901. if (!dev) {
  902. dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
  903. goto err_out;
  904. }
  905. SET_NETDEV_DEV(dev, &pdev->dev);
  906. /* TODO: Actually, we have some interesting features... */
  907. dev->features |= 0;
  908. bp = netdev_priv(dev);
  909. bp->pdev = pdev;
  910. bp->dev = dev;
  911. spin_lock_init(&bp->lock);
  912. #if defined(CONFIG_ARCH_AT91)
  913. bp->pclk = clk_get(&pdev->dev, "macb_clk");
  914. if (IS_ERR(bp->pclk)) {
  915. dev_err(&pdev->dev, "failed to get macb_clk\n");
  916. goto err_out_free_dev;
  917. }
  918. clk_enable(bp->pclk);
  919. #else
  920. bp->pclk = clk_get(&pdev->dev, "pclk");
  921. if (IS_ERR(bp->pclk)) {
  922. dev_err(&pdev->dev, "failed to get pclk\n");
  923. goto err_out_free_dev;
  924. }
  925. bp->hclk = clk_get(&pdev->dev, "hclk");
  926. if (IS_ERR(bp->hclk)) {
  927. dev_err(&pdev->dev, "failed to get hclk\n");
  928. goto err_out_put_pclk;
  929. }
  930. clk_enable(bp->pclk);
  931. clk_enable(bp->hclk);
  932. #endif
  933. bp->regs = ioremap(regs->start, regs->end - regs->start + 1);
  934. if (!bp->regs) {
  935. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  936. err = -ENOMEM;
  937. goto err_out_disable_clocks;
  938. }
  939. dev->irq = platform_get_irq(pdev, 0);
  940. err = request_irq(dev->irq, macb_interrupt, IRQF_SAMPLE_RANDOM,
  941. dev->name, dev);
  942. if (err) {
  943. printk(KERN_ERR
  944. "%s: Unable to request IRQ %d (error %d)\n",
  945. dev->name, dev->irq, err);
  946. goto err_out_iounmap;
  947. }
  948. dev->open = macb_open;
  949. dev->stop = macb_close;
  950. dev->hard_start_xmit = macb_start_xmit;
  951. dev->get_stats = macb_get_stats;
  952. dev->set_multicast_list = macb_set_rx_mode;
  953. dev->do_ioctl = macb_ioctl;
  954. netif_napi_add(dev, &bp->napi, macb_poll, 64);
  955. dev->ethtool_ops = &macb_ethtool_ops;
  956. dev->base_addr = regs->start;
  957. /* Set MII management clock divider */
  958. pclk_hz = clk_get_rate(bp->pclk);
  959. if (pclk_hz <= 20000000)
  960. config = MACB_BF(CLK, MACB_CLK_DIV8);
  961. else if (pclk_hz <= 40000000)
  962. config = MACB_BF(CLK, MACB_CLK_DIV16);
  963. else if (pclk_hz <= 80000000)
  964. config = MACB_BF(CLK, MACB_CLK_DIV32);
  965. else
  966. config = MACB_BF(CLK, MACB_CLK_DIV64);
  967. macb_writel(bp, NCFGR, config);
  968. macb_get_hwaddr(bp);
  969. pdata = pdev->dev.platform_data;
  970. if (pdata && pdata->is_rmii)
  971. #if defined(CONFIG_ARCH_AT91)
  972. macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) );
  973. #else
  974. macb_writel(bp, USRIO, 0);
  975. #endif
  976. else
  977. #if defined(CONFIG_ARCH_AT91)
  978. macb_writel(bp, USRIO, MACB_BIT(CLKEN));
  979. #else
  980. macb_writel(bp, USRIO, MACB_BIT(MII));
  981. #endif
  982. bp->tx_pending = DEF_TX_RING_PENDING;
  983. err = register_netdev(dev);
  984. if (err) {
  985. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  986. goto err_out_free_irq;
  987. }
  988. if (macb_mii_init(bp) != 0) {
  989. goto err_out_unregister_netdev;
  990. }
  991. platform_set_drvdata(pdev, dev);
  992. printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d (%pM)\n",
  993. dev->name, dev->base_addr, dev->irq, dev->dev_addr);
  994. phydev = bp->phy_dev;
  995. printk(KERN_INFO "%s: attached PHY driver [%s] "
  996. "(mii_bus:phy_addr=%s, irq=%d)\n",
  997. dev->name, phydev->drv->name, phydev->dev.bus_id, phydev->irq);
  998. return 0;
  999. err_out_unregister_netdev:
  1000. unregister_netdev(dev);
  1001. err_out_free_irq:
  1002. free_irq(dev->irq, dev);
  1003. err_out_iounmap:
  1004. iounmap(bp->regs);
  1005. err_out_disable_clocks:
  1006. #ifndef CONFIG_ARCH_AT91
  1007. clk_disable(bp->hclk);
  1008. clk_put(bp->hclk);
  1009. #endif
  1010. clk_disable(bp->pclk);
  1011. #ifndef CONFIG_ARCH_AT91
  1012. err_out_put_pclk:
  1013. #endif
  1014. clk_put(bp->pclk);
  1015. err_out_free_dev:
  1016. free_netdev(dev);
  1017. err_out:
  1018. platform_set_drvdata(pdev, NULL);
  1019. return err;
  1020. }
  1021. static int __exit macb_remove(struct platform_device *pdev)
  1022. {
  1023. struct net_device *dev;
  1024. struct macb *bp;
  1025. dev = platform_get_drvdata(pdev);
  1026. if (dev) {
  1027. bp = netdev_priv(dev);
  1028. if (bp->phy_dev)
  1029. phy_disconnect(bp->phy_dev);
  1030. mdiobus_unregister(bp->mii_bus);
  1031. kfree(bp->mii_bus->irq);
  1032. mdiobus_free(bp->mii_bus);
  1033. unregister_netdev(dev);
  1034. free_irq(dev->irq, dev);
  1035. iounmap(bp->regs);
  1036. #ifndef CONFIG_ARCH_AT91
  1037. clk_disable(bp->hclk);
  1038. clk_put(bp->hclk);
  1039. #endif
  1040. clk_disable(bp->pclk);
  1041. clk_put(bp->pclk);
  1042. free_netdev(dev);
  1043. platform_set_drvdata(pdev, NULL);
  1044. }
  1045. return 0;
  1046. }
  1047. #ifdef CONFIG_PM
  1048. static int macb_suspend(struct platform_device *pdev, pm_message_t state)
  1049. {
  1050. struct net_device *netdev = platform_get_drvdata(pdev);
  1051. struct macb *bp = netdev_priv(netdev);
  1052. netif_device_detach(netdev);
  1053. #ifndef CONFIG_ARCH_AT91
  1054. clk_disable(bp->hclk);
  1055. #endif
  1056. clk_disable(bp->pclk);
  1057. return 0;
  1058. }
  1059. static int macb_resume(struct platform_device *pdev)
  1060. {
  1061. struct net_device *netdev = platform_get_drvdata(pdev);
  1062. struct macb *bp = netdev_priv(netdev);
  1063. clk_enable(bp->pclk);
  1064. #ifndef CONFIG_ARCH_AT91
  1065. clk_enable(bp->hclk);
  1066. #endif
  1067. netif_device_attach(netdev);
  1068. return 0;
  1069. }
  1070. #else
  1071. #define macb_suspend NULL
  1072. #define macb_resume NULL
  1073. #endif
  1074. static struct platform_driver macb_driver = {
  1075. .remove = __exit_p(macb_remove),
  1076. .suspend = macb_suspend,
  1077. .resume = macb_resume,
  1078. .driver = {
  1079. .name = "macb",
  1080. .owner = THIS_MODULE,
  1081. },
  1082. };
  1083. static int __init macb_init(void)
  1084. {
  1085. return platform_driver_probe(&macb_driver, macb_probe);
  1086. }
  1087. static void __exit macb_exit(void)
  1088. {
  1089. platform_driver_unregister(&macb_driver);
  1090. }
  1091. module_init(macb_init);
  1092. module_exit(macb_exit);
  1093. MODULE_LICENSE("GPL");
  1094. MODULE_DESCRIPTION("Atmel MACB Ethernet driver");
  1095. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  1096. MODULE_ALIAS("platform:macb");