macb.c 32 KB

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