macb.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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/interrupt.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/platform_data/macb.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/phy.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;
  154. struct macb_platform_data *pdata;
  155. int ret;
  156. phydev = phy_find_first(bp->mii_bus);
  157. if (!phydev) {
  158. printk (KERN_ERR "%s: no PHY found\n", dev->name);
  159. return -1;
  160. }
  161. pdata = bp->pdev->dev.platform_data;
  162. /* TODO : add pin_irq */
  163. /* attach the mac to the phy */
  164. ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0,
  165. pdata && pdata->is_rmii ?
  166. PHY_INTERFACE_MODE_RMII :
  167. PHY_INTERFACE_MODE_MII);
  168. if (ret) {
  169. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  170. return ret;
  171. }
  172. /* mask with MAC supported features */
  173. phydev->supported &= PHY_BASIC_FEATURES;
  174. phydev->advertising = phydev->supported;
  175. bp->link = 0;
  176. bp->speed = 0;
  177. bp->duplex = -1;
  178. bp->phy_dev = phydev;
  179. return 0;
  180. }
  181. static int macb_mii_init(struct macb *bp)
  182. {
  183. struct macb_platform_data *pdata;
  184. int err = -ENXIO, i;
  185. /* Enable management port */
  186. macb_writel(bp, NCR, MACB_BIT(MPE));
  187. bp->mii_bus = mdiobus_alloc();
  188. if (bp->mii_bus == NULL) {
  189. err = -ENOMEM;
  190. goto err_out;
  191. }
  192. bp->mii_bus->name = "MACB_mii_bus";
  193. bp->mii_bus->read = &macb_mdio_read;
  194. bp->mii_bus->write = &macb_mdio_write;
  195. bp->mii_bus->reset = &macb_mdio_reset;
  196. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%x", bp->pdev->id);
  197. bp->mii_bus->priv = bp;
  198. bp->mii_bus->parent = &bp->dev->dev;
  199. pdata = bp->pdev->dev.platform_data;
  200. if (pdata)
  201. bp->mii_bus->phy_mask = pdata->phy_mask;
  202. bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  203. if (!bp->mii_bus->irq) {
  204. err = -ENOMEM;
  205. goto err_out_free_mdiobus;
  206. }
  207. for (i = 0; i < PHY_MAX_ADDR; i++)
  208. bp->mii_bus->irq[i] = PHY_POLL;
  209. dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
  210. if (mdiobus_register(bp->mii_bus))
  211. goto err_out_free_mdio_irq;
  212. if (macb_mii_probe(bp->dev) != 0) {
  213. goto err_out_unregister_bus;
  214. }
  215. return 0;
  216. err_out_unregister_bus:
  217. mdiobus_unregister(bp->mii_bus);
  218. err_out_free_mdio_irq:
  219. kfree(bp->mii_bus->irq);
  220. err_out_free_mdiobus:
  221. mdiobus_free(bp->mii_bus);
  222. err_out:
  223. return err;
  224. }
  225. static void macb_update_stats(struct macb *bp)
  226. {
  227. u32 __iomem *reg = bp->regs + MACB_PFR;
  228. u32 *p = &bp->hw_stats.rx_pause_frames;
  229. u32 *end = &bp->hw_stats.tx_pause_frames + 1;
  230. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  231. for(; p < end; p++, reg++)
  232. *p += __raw_readl(reg);
  233. }
  234. static void macb_tx(struct macb *bp)
  235. {
  236. unsigned int tail;
  237. unsigned int head;
  238. u32 status;
  239. status = macb_readl(bp, TSR);
  240. macb_writel(bp, TSR, status);
  241. dev_dbg(&bp->pdev->dev, "macb_tx status = %02lx\n",
  242. (unsigned long)status);
  243. if (status & (MACB_BIT(UND) | MACB_BIT(TSR_RLE))) {
  244. int i;
  245. printk(KERN_ERR "%s: TX %s, resetting buffers\n",
  246. bp->dev->name, status & MACB_BIT(UND) ?
  247. "underrun" : "retry limit exceeded");
  248. /* Transfer ongoing, disable transmitter, to avoid confusion */
  249. if (status & MACB_BIT(TGO))
  250. macb_writel(bp, NCR, macb_readl(bp, NCR) & ~MACB_BIT(TE));
  251. head = bp->tx_head;
  252. /*Mark all the buffer as used to avoid sending a lost buffer*/
  253. for (i = 0; i < TX_RING_SIZE; i++)
  254. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  255. /* Add wrap bit */
  256. bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  257. /* free transmit buffer in upper layer*/
  258. for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
  259. struct ring_info *rp = &bp->tx_skb[tail];
  260. struct sk_buff *skb = rp->skb;
  261. BUG_ON(skb == NULL);
  262. rmb();
  263. dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
  264. DMA_TO_DEVICE);
  265. rp->skb = NULL;
  266. dev_kfree_skb_irq(skb);
  267. }
  268. bp->tx_head = bp->tx_tail = 0;
  269. /* Enable the transmitter again */
  270. if (status & MACB_BIT(TGO))
  271. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE));
  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_checksum_none_assert(skb);
  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. int work_done;
  404. u32 status;
  405. status = macb_readl(bp, RSR);
  406. macb_writel(bp, RSR, status);
  407. work_done = 0;
  408. dev_dbg(&bp->pdev->dev, "poll: status = %08lx, budget = %d\n",
  409. (unsigned long)status, budget);
  410. work_done = macb_rx(bp, budget);
  411. if (work_done < budget) {
  412. napi_complete(napi);
  413. /*
  414. * We've done what we can to clean the buffers. Make sure we
  415. * get notified when new packets arrive.
  416. */
  417. macb_writel(bp, IER, MACB_RX_INT_FLAGS);
  418. }
  419. /* TODO: Handle errors */
  420. return work_done;
  421. }
  422. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  423. {
  424. struct net_device *dev = dev_id;
  425. struct macb *bp = netdev_priv(dev);
  426. u32 status;
  427. status = macb_readl(bp, ISR);
  428. if (unlikely(!status))
  429. return IRQ_NONE;
  430. spin_lock(&bp->lock);
  431. while (status) {
  432. /* close possible race with dev_close */
  433. if (unlikely(!netif_running(dev))) {
  434. macb_writel(bp, IDR, ~0UL);
  435. break;
  436. }
  437. if (status & MACB_RX_INT_FLAGS) {
  438. /*
  439. * There's no point taking any more interrupts
  440. * until we have processed the buffers. The
  441. * scheduling call may fail if the poll routine
  442. * is already scheduled, so disable interrupts
  443. * now.
  444. */
  445. macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
  446. if (napi_schedule_prep(&bp->napi)) {
  447. dev_dbg(&bp->pdev->dev,
  448. "scheduling RX softirq\n");
  449. __napi_schedule(&bp->napi);
  450. }
  451. }
  452. if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND) |
  453. MACB_BIT(ISR_RLE)))
  454. macb_tx(bp);
  455. /*
  456. * Link change detection isn't possible with RMII, so we'll
  457. * add that if/when we get our hands on a full-blown MII PHY.
  458. */
  459. if (status & MACB_BIT(ISR_ROVR)) {
  460. /* We missed at least one packet */
  461. bp->hw_stats.rx_overruns++;
  462. }
  463. if (status & MACB_BIT(HRESP)) {
  464. /*
  465. * TODO: Reset the hardware, and maybe move the printk
  466. * to a lower-priority context as well (work queue?)
  467. */
  468. printk(KERN_ERR "%s: DMA bus error: HRESP not OK\n",
  469. dev->name);
  470. }
  471. status = macb_readl(bp, ISR);
  472. }
  473. spin_unlock(&bp->lock);
  474. return IRQ_HANDLED;
  475. }
  476. #ifdef CONFIG_NET_POLL_CONTROLLER
  477. /*
  478. * Polling receive - used by netconsole and other diagnostic tools
  479. * to allow network i/o with interrupts disabled.
  480. */
  481. static void macb_poll_controller(struct net_device *dev)
  482. {
  483. unsigned long flags;
  484. local_irq_save(flags);
  485. macb_interrupt(dev->irq, dev);
  486. local_irq_restore(flags);
  487. }
  488. #endif
  489. static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  490. {
  491. struct macb *bp = netdev_priv(dev);
  492. dma_addr_t mapping;
  493. unsigned int len, entry;
  494. u32 ctrl;
  495. unsigned long flags;
  496. #ifdef DEBUG
  497. int i;
  498. dev_dbg(&bp->pdev->dev,
  499. "start_xmit: len %u head %p data %p tail %p end %p\n",
  500. skb->len, skb->head, skb->data,
  501. skb_tail_pointer(skb), skb_end_pointer(skb));
  502. dev_dbg(&bp->pdev->dev,
  503. "data:");
  504. for (i = 0; i < 16; i++)
  505. printk(" %02x", (unsigned int)skb->data[i]);
  506. printk("\n");
  507. #endif
  508. len = skb->len;
  509. spin_lock_irqsave(&bp->lock, flags);
  510. /* This is a hard error, log it. */
  511. if (TX_BUFFS_AVAIL(bp) < 1) {
  512. netif_stop_queue(dev);
  513. spin_unlock_irqrestore(&bp->lock, flags);
  514. dev_err(&bp->pdev->dev,
  515. "BUG! Tx Ring full when queue awake!\n");
  516. dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
  517. bp->tx_head, bp->tx_tail);
  518. return NETDEV_TX_BUSY;
  519. }
  520. entry = bp->tx_head;
  521. dev_dbg(&bp->pdev->dev, "Allocated ring entry %u\n", entry);
  522. mapping = dma_map_single(&bp->pdev->dev, skb->data,
  523. len, DMA_TO_DEVICE);
  524. bp->tx_skb[entry].skb = skb;
  525. bp->tx_skb[entry].mapping = mapping;
  526. dev_dbg(&bp->pdev->dev, "Mapped skb data %p to DMA addr %08lx\n",
  527. skb->data, (unsigned long)mapping);
  528. ctrl = MACB_BF(TX_FRMLEN, len);
  529. ctrl |= MACB_BIT(TX_LAST);
  530. if (entry == (TX_RING_SIZE - 1))
  531. ctrl |= MACB_BIT(TX_WRAP);
  532. bp->tx_ring[entry].addr = mapping;
  533. bp->tx_ring[entry].ctrl = ctrl;
  534. wmb();
  535. entry = NEXT_TX(entry);
  536. bp->tx_head = entry;
  537. skb_tx_timestamp(skb);
  538. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  539. if (TX_BUFFS_AVAIL(bp) < 1)
  540. netif_stop_queue(dev);
  541. spin_unlock_irqrestore(&bp->lock, flags);
  542. return NETDEV_TX_OK;
  543. }
  544. static void macb_free_consistent(struct macb *bp)
  545. {
  546. if (bp->tx_skb) {
  547. kfree(bp->tx_skb);
  548. bp->tx_skb = NULL;
  549. }
  550. if (bp->rx_ring) {
  551. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
  552. bp->rx_ring, bp->rx_ring_dma);
  553. bp->rx_ring = NULL;
  554. }
  555. if (bp->tx_ring) {
  556. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
  557. bp->tx_ring, bp->tx_ring_dma);
  558. bp->tx_ring = NULL;
  559. }
  560. if (bp->rx_buffers) {
  561. dma_free_coherent(&bp->pdev->dev,
  562. RX_RING_SIZE * RX_BUFFER_SIZE,
  563. bp->rx_buffers, bp->rx_buffers_dma);
  564. bp->rx_buffers = NULL;
  565. }
  566. }
  567. static int macb_alloc_consistent(struct macb *bp)
  568. {
  569. int size;
  570. size = TX_RING_SIZE * sizeof(struct ring_info);
  571. bp->tx_skb = kmalloc(size, GFP_KERNEL);
  572. if (!bp->tx_skb)
  573. goto out_err;
  574. size = RX_RING_BYTES;
  575. bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  576. &bp->rx_ring_dma, GFP_KERNEL);
  577. if (!bp->rx_ring)
  578. goto out_err;
  579. dev_dbg(&bp->pdev->dev,
  580. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  581. size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
  582. size = TX_RING_BYTES;
  583. bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  584. &bp->tx_ring_dma, GFP_KERNEL);
  585. if (!bp->tx_ring)
  586. goto out_err;
  587. dev_dbg(&bp->pdev->dev,
  588. "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
  589. size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
  590. size = RX_RING_SIZE * RX_BUFFER_SIZE;
  591. bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  592. &bp->rx_buffers_dma, GFP_KERNEL);
  593. if (!bp->rx_buffers)
  594. goto out_err;
  595. dev_dbg(&bp->pdev->dev,
  596. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  597. size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
  598. return 0;
  599. out_err:
  600. macb_free_consistent(bp);
  601. return -ENOMEM;
  602. }
  603. static void macb_init_rings(struct macb *bp)
  604. {
  605. int i;
  606. dma_addr_t addr;
  607. addr = bp->rx_buffers_dma;
  608. for (i = 0; i < RX_RING_SIZE; i++) {
  609. bp->rx_ring[i].addr = addr;
  610. bp->rx_ring[i].ctrl = 0;
  611. addr += RX_BUFFER_SIZE;
  612. }
  613. bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
  614. for (i = 0; i < TX_RING_SIZE; i++) {
  615. bp->tx_ring[i].addr = 0;
  616. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  617. }
  618. bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  619. bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
  620. }
  621. static void macb_reset_hw(struct macb *bp)
  622. {
  623. /* Make sure we have the write buffer for ourselves */
  624. wmb();
  625. /*
  626. * Disable RX and TX (XXX: Should we halt the transmission
  627. * more gracefully?)
  628. */
  629. macb_writel(bp, NCR, 0);
  630. /* Clear the stats registers (XXX: Update stats first?) */
  631. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  632. /* Clear all status flags */
  633. macb_writel(bp, TSR, ~0UL);
  634. macb_writel(bp, RSR, ~0UL);
  635. /* Disable all interrupts */
  636. macb_writel(bp, IDR, ~0UL);
  637. macb_readl(bp, ISR);
  638. }
  639. static void macb_init_hw(struct macb *bp)
  640. {
  641. u32 config;
  642. macb_reset_hw(bp);
  643. __macb_set_hwaddr(bp);
  644. config = macb_readl(bp, NCFGR) & MACB_BF(CLK, -1L);
  645. config |= MACB_BIT(PAE); /* PAuse Enable */
  646. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  647. config |= MACB_BIT(BIG); /* Receive oversized frames */
  648. if (bp->dev->flags & IFF_PROMISC)
  649. config |= MACB_BIT(CAF); /* Copy All Frames */
  650. if (!(bp->dev->flags & IFF_BROADCAST))
  651. config |= MACB_BIT(NBC); /* No BroadCast */
  652. macb_writel(bp, NCFGR, config);
  653. /* Initialize TX and RX buffers */
  654. macb_writel(bp, RBQP, bp->rx_ring_dma);
  655. macb_writel(bp, TBQP, bp->tx_ring_dma);
  656. /* Enable TX and RX */
  657. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  658. /* Enable interrupts */
  659. macb_writel(bp, IER, (MACB_BIT(RCOMP)
  660. | MACB_BIT(RXUBR)
  661. | MACB_BIT(ISR_TUND)
  662. | MACB_BIT(ISR_RLE)
  663. | MACB_BIT(TXERR)
  664. | MACB_BIT(TCOMP)
  665. | MACB_BIT(ISR_ROVR)
  666. | MACB_BIT(HRESP)));
  667. }
  668. /*
  669. * The hash address register is 64 bits long and takes up two
  670. * locations in the memory map. The least significant bits are stored
  671. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  672. *
  673. * The unicast hash enable and the multicast hash enable bits in the
  674. * network configuration register enable the reception of hash matched
  675. * frames. The destination address is reduced to a 6 bit index into
  676. * the 64 bit hash register using the following hash function. The
  677. * hash function is an exclusive or of every sixth bit of the
  678. * destination address.
  679. *
  680. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  681. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  682. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  683. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  684. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  685. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  686. *
  687. * da[0] represents the least significant bit of the first byte
  688. * received, that is, the multicast/unicast indicator, and da[47]
  689. * represents the most significant bit of the last byte received. If
  690. * the hash index, hi[n], points to a bit that is set in the hash
  691. * register then the frame will be matched according to whether the
  692. * frame is multicast or unicast. A multicast match will be signalled
  693. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  694. * index points to a bit set in the hash register. A unicast match
  695. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  696. * and the hash index points to a bit set in the hash register. To
  697. * receive all multicast frames, the hash register should be set with
  698. * all ones and the multicast hash enable bit should be set in the
  699. * network configuration register.
  700. */
  701. static inline int hash_bit_value(int bitnr, __u8 *addr)
  702. {
  703. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  704. return 1;
  705. return 0;
  706. }
  707. /*
  708. * Return the hash index value for the specified address.
  709. */
  710. static int hash_get_index(__u8 *addr)
  711. {
  712. int i, j, bitval;
  713. int hash_index = 0;
  714. for (j = 0; j < 6; j++) {
  715. for (i = 0, bitval = 0; i < 8; i++)
  716. bitval ^= hash_bit_value(i*6 + j, addr);
  717. hash_index |= (bitval << j);
  718. }
  719. return hash_index;
  720. }
  721. /*
  722. * Add multicast addresses to the internal multicast-hash table.
  723. */
  724. static void macb_sethashtable(struct net_device *dev)
  725. {
  726. struct netdev_hw_addr *ha;
  727. unsigned long mc_filter[2];
  728. unsigned int bitnr;
  729. struct macb *bp = netdev_priv(dev);
  730. mc_filter[0] = mc_filter[1] = 0;
  731. netdev_for_each_mc_addr(ha, dev) {
  732. bitnr = hash_get_index(ha->addr);
  733. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  734. }
  735. macb_writel(bp, HRB, mc_filter[0]);
  736. macb_writel(bp, HRT, mc_filter[1]);
  737. }
  738. /*
  739. * Enable/Disable promiscuous and multicast modes.
  740. */
  741. static void macb_set_rx_mode(struct net_device *dev)
  742. {
  743. unsigned long cfg;
  744. struct macb *bp = netdev_priv(dev);
  745. cfg = macb_readl(bp, NCFGR);
  746. if (dev->flags & IFF_PROMISC)
  747. /* Enable promiscuous mode */
  748. cfg |= MACB_BIT(CAF);
  749. else if (dev->flags & (~IFF_PROMISC))
  750. /* Disable promiscuous mode */
  751. cfg &= ~MACB_BIT(CAF);
  752. if (dev->flags & IFF_ALLMULTI) {
  753. /* Enable all multicast mode */
  754. macb_writel(bp, HRB, -1);
  755. macb_writel(bp, HRT, -1);
  756. cfg |= MACB_BIT(NCFGR_MTI);
  757. } else if (!netdev_mc_empty(dev)) {
  758. /* Enable specific multicasts */
  759. macb_sethashtable(dev);
  760. cfg |= MACB_BIT(NCFGR_MTI);
  761. } else if (dev->flags & (~IFF_ALLMULTI)) {
  762. /* Disable all multicast mode */
  763. macb_writel(bp, HRB, 0);
  764. macb_writel(bp, HRT, 0);
  765. cfg &= ~MACB_BIT(NCFGR_MTI);
  766. }
  767. macb_writel(bp, NCFGR, cfg);
  768. }
  769. static int macb_open(struct net_device *dev)
  770. {
  771. struct macb *bp = netdev_priv(dev);
  772. int err;
  773. dev_dbg(&bp->pdev->dev, "open\n");
  774. /* if the phy is not yet register, retry later*/
  775. if (!bp->phy_dev)
  776. return -EAGAIN;
  777. if (!is_valid_ether_addr(dev->dev_addr))
  778. return -EADDRNOTAVAIL;
  779. err = macb_alloc_consistent(bp);
  780. if (err) {
  781. printk(KERN_ERR
  782. "%s: Unable to allocate DMA memory (error %d)\n",
  783. dev->name, err);
  784. return err;
  785. }
  786. napi_enable(&bp->napi);
  787. macb_init_rings(bp);
  788. macb_init_hw(bp);
  789. /* schedule a link state check */
  790. phy_start(bp->phy_dev);
  791. netif_start_queue(dev);
  792. return 0;
  793. }
  794. static int macb_close(struct net_device *dev)
  795. {
  796. struct macb *bp = netdev_priv(dev);
  797. unsigned long flags;
  798. netif_stop_queue(dev);
  799. napi_disable(&bp->napi);
  800. if (bp->phy_dev)
  801. phy_stop(bp->phy_dev);
  802. spin_lock_irqsave(&bp->lock, flags);
  803. macb_reset_hw(bp);
  804. netif_carrier_off(dev);
  805. spin_unlock_irqrestore(&bp->lock, flags);
  806. macb_free_consistent(bp);
  807. return 0;
  808. }
  809. static struct net_device_stats *macb_get_stats(struct net_device *dev)
  810. {
  811. struct macb *bp = netdev_priv(dev);
  812. struct net_device_stats *nstat = &bp->stats;
  813. struct macb_stats *hwstat = &bp->hw_stats;
  814. /* read stats from hardware */
  815. macb_update_stats(bp);
  816. /* Convert HW stats into netdevice stats */
  817. nstat->rx_errors = (hwstat->rx_fcs_errors +
  818. hwstat->rx_align_errors +
  819. hwstat->rx_resource_errors +
  820. hwstat->rx_overruns +
  821. hwstat->rx_oversize_pkts +
  822. hwstat->rx_jabbers +
  823. hwstat->rx_undersize_pkts +
  824. hwstat->sqe_test_errors +
  825. hwstat->rx_length_mismatch);
  826. nstat->tx_errors = (hwstat->tx_late_cols +
  827. hwstat->tx_excessive_cols +
  828. hwstat->tx_underruns +
  829. hwstat->tx_carrier_errors);
  830. nstat->collisions = (hwstat->tx_single_cols +
  831. hwstat->tx_multiple_cols +
  832. hwstat->tx_excessive_cols);
  833. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  834. hwstat->rx_jabbers +
  835. hwstat->rx_undersize_pkts +
  836. hwstat->rx_length_mismatch);
  837. nstat->rx_over_errors = hwstat->rx_resource_errors +
  838. hwstat->rx_overruns;
  839. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  840. nstat->rx_frame_errors = hwstat->rx_align_errors;
  841. nstat->rx_fifo_errors = hwstat->rx_overruns;
  842. /* XXX: What does "missed" mean? */
  843. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  844. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  845. nstat->tx_fifo_errors = hwstat->tx_underruns;
  846. /* Don't know about heartbeat or window errors... */
  847. return nstat;
  848. }
  849. static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  850. {
  851. struct macb *bp = netdev_priv(dev);
  852. struct phy_device *phydev = bp->phy_dev;
  853. if (!phydev)
  854. return -ENODEV;
  855. return phy_ethtool_gset(phydev, cmd);
  856. }
  857. static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  858. {
  859. struct macb *bp = netdev_priv(dev);
  860. struct phy_device *phydev = bp->phy_dev;
  861. if (!phydev)
  862. return -ENODEV;
  863. return phy_ethtool_sset(phydev, cmd);
  864. }
  865. static void macb_get_drvinfo(struct net_device *dev,
  866. struct ethtool_drvinfo *info)
  867. {
  868. struct macb *bp = netdev_priv(dev);
  869. strcpy(info->driver, bp->pdev->dev.driver->name);
  870. strcpy(info->version, "$Revision: 1.14 $");
  871. strcpy(info->bus_info, dev_name(&bp->pdev->dev));
  872. }
  873. static const struct ethtool_ops macb_ethtool_ops = {
  874. .get_settings = macb_get_settings,
  875. .set_settings = macb_set_settings,
  876. .get_drvinfo = macb_get_drvinfo,
  877. .get_link = ethtool_op_get_link,
  878. };
  879. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  880. {
  881. struct macb *bp = netdev_priv(dev);
  882. struct phy_device *phydev = bp->phy_dev;
  883. if (!netif_running(dev))
  884. return -EINVAL;
  885. if (!phydev)
  886. return -ENODEV;
  887. return phy_mii_ioctl(phydev, rq, cmd);
  888. }
  889. static const struct net_device_ops macb_netdev_ops = {
  890. .ndo_open = macb_open,
  891. .ndo_stop = macb_close,
  892. .ndo_start_xmit = macb_start_xmit,
  893. .ndo_set_rx_mode = macb_set_rx_mode,
  894. .ndo_get_stats = macb_get_stats,
  895. .ndo_do_ioctl = macb_ioctl,
  896. .ndo_validate_addr = eth_validate_addr,
  897. .ndo_change_mtu = eth_change_mtu,
  898. .ndo_set_mac_address = eth_mac_addr,
  899. #ifdef CONFIG_NET_POLL_CONTROLLER
  900. .ndo_poll_controller = macb_poll_controller,
  901. #endif
  902. };
  903. static int __init macb_probe(struct platform_device *pdev)
  904. {
  905. struct macb_platform_data *pdata;
  906. struct resource *regs;
  907. struct net_device *dev;
  908. struct macb *bp;
  909. struct phy_device *phydev;
  910. unsigned long pclk_hz;
  911. u32 config;
  912. int err = -ENXIO;
  913. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  914. if (!regs) {
  915. dev_err(&pdev->dev, "no mmio resource defined\n");
  916. goto err_out;
  917. }
  918. err = -ENOMEM;
  919. dev = alloc_etherdev(sizeof(*bp));
  920. if (!dev) {
  921. dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
  922. goto err_out;
  923. }
  924. SET_NETDEV_DEV(dev, &pdev->dev);
  925. /* TODO: Actually, we have some interesting features... */
  926. dev->features |= 0;
  927. bp = netdev_priv(dev);
  928. bp->pdev = pdev;
  929. bp->dev = dev;
  930. spin_lock_init(&bp->lock);
  931. bp->pclk = clk_get(&pdev->dev, "pclk");
  932. if (IS_ERR(bp->pclk)) {
  933. dev_err(&pdev->dev, "failed to get macb_clk\n");
  934. goto err_out_free_dev;
  935. }
  936. clk_enable(bp->pclk);
  937. bp->hclk = clk_get(&pdev->dev, "hclk");
  938. if (IS_ERR(bp->hclk)) {
  939. dev_err(&pdev->dev, "failed to get hclk\n");
  940. goto err_out_put_pclk;
  941. }
  942. clk_enable(bp->hclk);
  943. bp->regs = ioremap(regs->start, resource_size(regs));
  944. if (!bp->regs) {
  945. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  946. err = -ENOMEM;
  947. goto err_out_disable_clocks;
  948. }
  949. dev->irq = platform_get_irq(pdev, 0);
  950. err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
  951. if (err) {
  952. printk(KERN_ERR
  953. "%s: Unable to request IRQ %d (error %d)\n",
  954. dev->name, dev->irq, err);
  955. goto err_out_iounmap;
  956. }
  957. dev->netdev_ops = &macb_netdev_ops;
  958. netif_napi_add(dev, &bp->napi, macb_poll, 64);
  959. dev->ethtool_ops = &macb_ethtool_ops;
  960. dev->base_addr = regs->start;
  961. /* Set MII management clock divider */
  962. pclk_hz = clk_get_rate(bp->pclk);
  963. if (pclk_hz <= 20000000)
  964. config = MACB_BF(CLK, MACB_CLK_DIV8);
  965. else if (pclk_hz <= 40000000)
  966. config = MACB_BF(CLK, MACB_CLK_DIV16);
  967. else if (pclk_hz <= 80000000)
  968. config = MACB_BF(CLK, MACB_CLK_DIV32);
  969. else
  970. config = MACB_BF(CLK, MACB_CLK_DIV64);
  971. macb_writel(bp, NCFGR, config);
  972. macb_get_hwaddr(bp);
  973. pdata = pdev->dev.platform_data;
  974. if (pdata && pdata->is_rmii)
  975. #if defined(CONFIG_ARCH_AT91)
  976. macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) );
  977. #else
  978. macb_writel(bp, USRIO, 0);
  979. #endif
  980. else
  981. #if defined(CONFIG_ARCH_AT91)
  982. macb_writel(bp, USRIO, MACB_BIT(CLKEN));
  983. #else
  984. macb_writel(bp, USRIO, MACB_BIT(MII));
  985. #endif
  986. bp->tx_pending = DEF_TX_RING_PENDING;
  987. err = register_netdev(dev);
  988. if (err) {
  989. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  990. goto err_out_free_irq;
  991. }
  992. if (macb_mii_init(bp) != 0) {
  993. goto err_out_unregister_netdev;
  994. }
  995. platform_set_drvdata(pdev, dev);
  996. printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d (%pM)\n",
  997. dev->name, dev->base_addr, dev->irq, dev->dev_addr);
  998. phydev = bp->phy_dev;
  999. printk(KERN_INFO "%s: attached PHY driver [%s] "
  1000. "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
  1001. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  1002. return 0;
  1003. err_out_unregister_netdev:
  1004. unregister_netdev(dev);
  1005. err_out_free_irq:
  1006. free_irq(dev->irq, dev);
  1007. err_out_iounmap:
  1008. iounmap(bp->regs);
  1009. err_out_disable_clocks:
  1010. clk_disable(bp->hclk);
  1011. clk_put(bp->hclk);
  1012. clk_disable(bp->pclk);
  1013. err_out_put_pclk:
  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. clk_disable(bp->hclk);
  1037. clk_put(bp->hclk);
  1038. clk_disable(bp->pclk);
  1039. clk_put(bp->pclk);
  1040. free_netdev(dev);
  1041. platform_set_drvdata(pdev, NULL);
  1042. }
  1043. return 0;
  1044. }
  1045. #ifdef CONFIG_PM
  1046. static int macb_suspend(struct platform_device *pdev, pm_message_t state)
  1047. {
  1048. struct net_device *netdev = platform_get_drvdata(pdev);
  1049. struct macb *bp = netdev_priv(netdev);
  1050. netif_device_detach(netdev);
  1051. clk_disable(bp->hclk);
  1052. clk_disable(bp->pclk);
  1053. return 0;
  1054. }
  1055. static int macb_resume(struct platform_device *pdev)
  1056. {
  1057. struct net_device *netdev = platform_get_drvdata(pdev);
  1058. struct macb *bp = netdev_priv(netdev);
  1059. clk_enable(bp->pclk);
  1060. clk_enable(bp->hclk);
  1061. netif_device_attach(netdev);
  1062. return 0;
  1063. }
  1064. #else
  1065. #define macb_suspend NULL
  1066. #define macb_resume NULL
  1067. #endif
  1068. static struct platform_driver macb_driver = {
  1069. .remove = __exit_p(macb_remove),
  1070. .suspend = macb_suspend,
  1071. .resume = macb_resume,
  1072. .driver = {
  1073. .name = "macb",
  1074. .owner = THIS_MODULE,
  1075. },
  1076. };
  1077. static int __init macb_init(void)
  1078. {
  1079. return platform_driver_probe(&macb_driver, macb_probe);
  1080. }
  1081. static void __exit macb_exit(void)
  1082. {
  1083. platform_driver_unregister(&macb_driver);
  1084. }
  1085. module_init(macb_init);
  1086. module_exit(macb_exit);
  1087. MODULE_LICENSE("GPL");
  1088. MODULE_DESCRIPTION("Atmel MACB Ethernet driver");
  1089. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1090. MODULE_ALIAS("platform:macb");