macb.c 32 KB

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