macb.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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. platform_set_drvdata(bp->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->ip_summed = CHECKSUM_NONE;
  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. /* TODO: Handle errors */
  417. return work_done;
  418. }
  419. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  420. {
  421. struct net_device *dev = dev_id;
  422. struct macb *bp = netdev_priv(dev);
  423. u32 status;
  424. status = macb_readl(bp, ISR);
  425. if (unlikely(!status))
  426. return IRQ_NONE;
  427. spin_lock(&bp->lock);
  428. while (status) {
  429. /* close possible race with dev_close */
  430. if (unlikely(!netif_running(dev))) {
  431. macb_writel(bp, IDR, ~0UL);
  432. break;
  433. }
  434. if (status & MACB_RX_INT_FLAGS) {
  435. if (napi_schedule_prep(&bp->napi)) {
  436. /*
  437. * There's no point taking any more interrupts
  438. * until we have processed the buffers
  439. */
  440. macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
  441. dev_dbg(&bp->pdev->dev,
  442. "scheduling RX softirq\n");
  443. __napi_schedule(&bp->napi);
  444. }
  445. }
  446. if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND) |
  447. MACB_BIT(ISR_RLE)))
  448. macb_tx(bp);
  449. /*
  450. * Link change detection isn't possible with RMII, so we'll
  451. * add that if/when we get our hands on a full-blown MII PHY.
  452. */
  453. if (status & MACB_BIT(HRESP)) {
  454. /*
  455. * TODO: Reset the hardware, and maybe move the printk
  456. * to a lower-priority context as well (work queue?)
  457. */
  458. printk(KERN_ERR "%s: DMA bus error: HRESP not OK\n",
  459. dev->name);
  460. }
  461. status = macb_readl(bp, ISR);
  462. }
  463. spin_unlock(&bp->lock);
  464. return IRQ_HANDLED;
  465. }
  466. #ifdef CONFIG_NET_POLL_CONTROLLER
  467. /*
  468. * Polling receive - used by netconsole and other diagnostic tools
  469. * to allow network i/o with interrupts disabled.
  470. */
  471. static void macb_poll_controller(struct net_device *dev)
  472. {
  473. unsigned long flags;
  474. local_irq_save(flags);
  475. macb_interrupt(dev->irq, dev);
  476. local_irq_restore(flags);
  477. }
  478. #endif
  479. static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  480. {
  481. struct macb *bp = netdev_priv(dev);
  482. dma_addr_t mapping;
  483. unsigned int len, entry;
  484. u32 ctrl;
  485. unsigned long flags;
  486. #ifdef DEBUG
  487. int i;
  488. dev_dbg(&bp->pdev->dev,
  489. "start_xmit: len %u head %p data %p tail %p end %p\n",
  490. skb->len, skb->head, skb->data,
  491. skb_tail_pointer(skb), skb_end_pointer(skb));
  492. dev_dbg(&bp->pdev->dev,
  493. "data:");
  494. for (i = 0; i < 16; i++)
  495. printk(" %02x", (unsigned int)skb->data[i]);
  496. printk("\n");
  497. #endif
  498. len = skb->len;
  499. spin_lock_irqsave(&bp->lock, flags);
  500. /* This is a hard error, log it. */
  501. if (TX_BUFFS_AVAIL(bp) < 1) {
  502. netif_stop_queue(dev);
  503. spin_unlock_irqrestore(&bp->lock, flags);
  504. dev_err(&bp->pdev->dev,
  505. "BUG! Tx Ring full when queue awake!\n");
  506. dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
  507. bp->tx_head, bp->tx_tail);
  508. return NETDEV_TX_BUSY;
  509. }
  510. entry = bp->tx_head;
  511. dev_dbg(&bp->pdev->dev, "Allocated ring entry %u\n", entry);
  512. mapping = dma_map_single(&bp->pdev->dev, skb->data,
  513. len, DMA_TO_DEVICE);
  514. bp->tx_skb[entry].skb = skb;
  515. bp->tx_skb[entry].mapping = mapping;
  516. dev_dbg(&bp->pdev->dev, "Mapped skb data %p to DMA addr %08lx\n",
  517. skb->data, (unsigned long)mapping);
  518. ctrl = MACB_BF(TX_FRMLEN, len);
  519. ctrl |= MACB_BIT(TX_LAST);
  520. if (entry == (TX_RING_SIZE - 1))
  521. ctrl |= MACB_BIT(TX_WRAP);
  522. bp->tx_ring[entry].addr = mapping;
  523. bp->tx_ring[entry].ctrl = ctrl;
  524. wmb();
  525. entry = NEXT_TX(entry);
  526. bp->tx_head = entry;
  527. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  528. if (TX_BUFFS_AVAIL(bp) < 1)
  529. netif_stop_queue(dev);
  530. spin_unlock_irqrestore(&bp->lock, flags);
  531. dev->trans_start = jiffies;
  532. return NETDEV_TX_OK;
  533. }
  534. static void macb_free_consistent(struct macb *bp)
  535. {
  536. if (bp->tx_skb) {
  537. kfree(bp->tx_skb);
  538. bp->tx_skb = NULL;
  539. }
  540. if (bp->rx_ring) {
  541. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
  542. bp->rx_ring, bp->rx_ring_dma);
  543. bp->rx_ring = NULL;
  544. }
  545. if (bp->tx_ring) {
  546. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
  547. bp->tx_ring, bp->tx_ring_dma);
  548. bp->tx_ring = NULL;
  549. }
  550. if (bp->rx_buffers) {
  551. dma_free_coherent(&bp->pdev->dev,
  552. RX_RING_SIZE * RX_BUFFER_SIZE,
  553. bp->rx_buffers, bp->rx_buffers_dma);
  554. bp->rx_buffers = NULL;
  555. }
  556. }
  557. static int macb_alloc_consistent(struct macb *bp)
  558. {
  559. int size;
  560. size = TX_RING_SIZE * sizeof(struct ring_info);
  561. bp->tx_skb = kmalloc(size, GFP_KERNEL);
  562. if (!bp->tx_skb)
  563. goto out_err;
  564. size = RX_RING_BYTES;
  565. bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  566. &bp->rx_ring_dma, GFP_KERNEL);
  567. if (!bp->rx_ring)
  568. goto out_err;
  569. dev_dbg(&bp->pdev->dev,
  570. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  571. size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
  572. size = TX_RING_BYTES;
  573. bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  574. &bp->tx_ring_dma, GFP_KERNEL);
  575. if (!bp->tx_ring)
  576. goto out_err;
  577. dev_dbg(&bp->pdev->dev,
  578. "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
  579. size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
  580. size = RX_RING_SIZE * RX_BUFFER_SIZE;
  581. bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  582. &bp->rx_buffers_dma, GFP_KERNEL);
  583. if (!bp->rx_buffers)
  584. goto out_err;
  585. dev_dbg(&bp->pdev->dev,
  586. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  587. size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
  588. return 0;
  589. out_err:
  590. macb_free_consistent(bp);
  591. return -ENOMEM;
  592. }
  593. static void macb_init_rings(struct macb *bp)
  594. {
  595. int i;
  596. dma_addr_t addr;
  597. addr = bp->rx_buffers_dma;
  598. for (i = 0; i < RX_RING_SIZE; i++) {
  599. bp->rx_ring[i].addr = addr;
  600. bp->rx_ring[i].ctrl = 0;
  601. addr += RX_BUFFER_SIZE;
  602. }
  603. bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
  604. for (i = 0; i < TX_RING_SIZE; i++) {
  605. bp->tx_ring[i].addr = 0;
  606. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  607. }
  608. bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  609. bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
  610. }
  611. static void macb_reset_hw(struct macb *bp)
  612. {
  613. /* Make sure we have the write buffer for ourselves */
  614. wmb();
  615. /*
  616. * Disable RX and TX (XXX: Should we halt the transmission
  617. * more gracefully?)
  618. */
  619. macb_writel(bp, NCR, 0);
  620. /* Clear the stats registers (XXX: Update stats first?) */
  621. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  622. /* Clear all status flags */
  623. macb_writel(bp, TSR, ~0UL);
  624. macb_writel(bp, RSR, ~0UL);
  625. /* Disable all interrupts */
  626. macb_writel(bp, IDR, ~0UL);
  627. macb_readl(bp, ISR);
  628. }
  629. static void macb_init_hw(struct macb *bp)
  630. {
  631. u32 config;
  632. macb_reset_hw(bp);
  633. __macb_set_hwaddr(bp);
  634. config = macb_readl(bp, NCFGR) & MACB_BF(CLK, -1L);
  635. config |= MACB_BIT(PAE); /* PAuse Enable */
  636. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  637. if (bp->dev->flags & IFF_PROMISC)
  638. config |= MACB_BIT(CAF); /* Copy All Frames */
  639. if (!(bp->dev->flags & IFF_BROADCAST))
  640. config |= MACB_BIT(NBC); /* No BroadCast */
  641. macb_writel(bp, NCFGR, config);
  642. /* Initialize TX and RX buffers */
  643. macb_writel(bp, RBQP, bp->rx_ring_dma);
  644. macb_writel(bp, TBQP, bp->tx_ring_dma);
  645. /* Enable TX and RX */
  646. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  647. /* Enable interrupts */
  648. macb_writel(bp, IER, (MACB_BIT(RCOMP)
  649. | MACB_BIT(RXUBR)
  650. | MACB_BIT(ISR_TUND)
  651. | MACB_BIT(ISR_RLE)
  652. | MACB_BIT(TXERR)
  653. | MACB_BIT(TCOMP)
  654. | MACB_BIT(ISR_ROVR)
  655. | MACB_BIT(HRESP)));
  656. }
  657. /*
  658. * The hash address register is 64 bits long and takes up two
  659. * locations in the memory map. The least significant bits are stored
  660. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  661. *
  662. * The unicast hash enable and the multicast hash enable bits in the
  663. * network configuration register enable the reception of hash matched
  664. * frames. The destination address is reduced to a 6 bit index into
  665. * the 64 bit hash register using the following hash function. The
  666. * hash function is an exclusive or of every sixth bit of the
  667. * destination address.
  668. *
  669. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  670. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  671. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  672. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  673. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  674. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  675. *
  676. * da[0] represents the least significant bit of the first byte
  677. * received, that is, the multicast/unicast indicator, and da[47]
  678. * represents the most significant bit of the last byte received. If
  679. * the hash index, hi[n], points to a bit that is set in the hash
  680. * register then the frame will be matched according to whether the
  681. * frame is multicast or unicast. A multicast match will be signalled
  682. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  683. * index points to a bit set in the hash register. A unicast match
  684. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  685. * and the hash index points to a bit set in the hash register. To
  686. * receive all multicast frames, the hash register should be set with
  687. * all ones and the multicast hash enable bit should be set in the
  688. * network configuration register.
  689. */
  690. static inline int hash_bit_value(int bitnr, __u8 *addr)
  691. {
  692. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  693. return 1;
  694. return 0;
  695. }
  696. /*
  697. * Return the hash index value for the specified address.
  698. */
  699. static int hash_get_index(__u8 *addr)
  700. {
  701. int i, j, bitval;
  702. int hash_index = 0;
  703. for (j = 0; j < 6; j++) {
  704. for (i = 0, bitval = 0; i < 8; i++)
  705. bitval ^= hash_bit_value(i*6 + j, addr);
  706. hash_index |= (bitval << j);
  707. }
  708. return hash_index;
  709. }
  710. /*
  711. * Add multicast addresses to the internal multicast-hash table.
  712. */
  713. static void macb_sethashtable(struct net_device *dev)
  714. {
  715. struct dev_mc_list *curr;
  716. unsigned long mc_filter[2];
  717. unsigned int i, bitnr;
  718. struct macb *bp = netdev_priv(dev);
  719. mc_filter[0] = mc_filter[1] = 0;
  720. curr = dev->mc_list;
  721. for (i = 0; i < netdev_mc_count(dev); i++, curr = curr->next) {
  722. if (!curr) break; /* unexpected end of list */
  723. bitnr = hash_get_index(curr->dmi_addr);
  724. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  725. }
  726. macb_writel(bp, HRB, mc_filter[0]);
  727. macb_writel(bp, HRT, mc_filter[1]);
  728. }
  729. /*
  730. * Enable/Disable promiscuous and multicast modes.
  731. */
  732. static void macb_set_rx_mode(struct net_device *dev)
  733. {
  734. unsigned long cfg;
  735. struct macb *bp = netdev_priv(dev);
  736. cfg = macb_readl(bp, NCFGR);
  737. if (dev->flags & IFF_PROMISC)
  738. /* Enable promiscuous mode */
  739. cfg |= MACB_BIT(CAF);
  740. else if (dev->flags & (~IFF_PROMISC))
  741. /* Disable promiscuous mode */
  742. cfg &= ~MACB_BIT(CAF);
  743. if (dev->flags & IFF_ALLMULTI) {
  744. /* Enable all multicast mode */
  745. macb_writel(bp, HRB, -1);
  746. macb_writel(bp, HRT, -1);
  747. cfg |= MACB_BIT(NCFGR_MTI);
  748. } else if (!netdev_mc_empty(dev)) {
  749. /* Enable specific multicasts */
  750. macb_sethashtable(dev);
  751. cfg |= MACB_BIT(NCFGR_MTI);
  752. } else if (dev->flags & (~IFF_ALLMULTI)) {
  753. /* Disable all multicast mode */
  754. macb_writel(bp, HRB, 0);
  755. macb_writel(bp, HRT, 0);
  756. cfg &= ~MACB_BIT(NCFGR_MTI);
  757. }
  758. macb_writel(bp, NCFGR, cfg);
  759. }
  760. static int macb_open(struct net_device *dev)
  761. {
  762. struct macb *bp = netdev_priv(dev);
  763. int err;
  764. dev_dbg(&bp->pdev->dev, "open\n");
  765. /* if the phy is not yet register, retry later*/
  766. if (!bp->phy_dev)
  767. return -EAGAIN;
  768. if (!is_valid_ether_addr(dev->dev_addr))
  769. return -EADDRNOTAVAIL;
  770. err = macb_alloc_consistent(bp);
  771. if (err) {
  772. printk(KERN_ERR
  773. "%s: Unable to allocate DMA memory (error %d)\n",
  774. dev->name, err);
  775. return err;
  776. }
  777. napi_enable(&bp->napi);
  778. macb_init_rings(bp);
  779. macb_init_hw(bp);
  780. /* schedule a link state check */
  781. phy_start(bp->phy_dev);
  782. netif_start_queue(dev);
  783. return 0;
  784. }
  785. static int macb_close(struct net_device *dev)
  786. {
  787. struct macb *bp = netdev_priv(dev);
  788. unsigned long flags;
  789. netif_stop_queue(dev);
  790. napi_disable(&bp->napi);
  791. if (bp->phy_dev)
  792. phy_stop(bp->phy_dev);
  793. spin_lock_irqsave(&bp->lock, flags);
  794. macb_reset_hw(bp);
  795. netif_carrier_off(dev);
  796. spin_unlock_irqrestore(&bp->lock, flags);
  797. macb_free_consistent(bp);
  798. return 0;
  799. }
  800. static struct net_device_stats *macb_get_stats(struct net_device *dev)
  801. {
  802. struct macb *bp = netdev_priv(dev);
  803. struct net_device_stats *nstat = &bp->stats;
  804. struct macb_stats *hwstat = &bp->hw_stats;
  805. /* read stats from hardware */
  806. macb_update_stats(bp);
  807. /* Convert HW stats into netdevice stats */
  808. nstat->rx_errors = (hwstat->rx_fcs_errors +
  809. hwstat->rx_align_errors +
  810. hwstat->rx_resource_errors +
  811. hwstat->rx_overruns +
  812. hwstat->rx_oversize_pkts +
  813. hwstat->rx_jabbers +
  814. hwstat->rx_undersize_pkts +
  815. hwstat->sqe_test_errors +
  816. hwstat->rx_length_mismatch);
  817. nstat->tx_errors = (hwstat->tx_late_cols +
  818. hwstat->tx_excessive_cols +
  819. hwstat->tx_underruns +
  820. hwstat->tx_carrier_errors);
  821. nstat->collisions = (hwstat->tx_single_cols +
  822. hwstat->tx_multiple_cols +
  823. hwstat->tx_excessive_cols);
  824. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  825. hwstat->rx_jabbers +
  826. hwstat->rx_undersize_pkts +
  827. hwstat->rx_length_mismatch);
  828. nstat->rx_over_errors = hwstat->rx_resource_errors;
  829. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  830. nstat->rx_frame_errors = hwstat->rx_align_errors;
  831. nstat->rx_fifo_errors = hwstat->rx_overruns;
  832. /* XXX: What does "missed" mean? */
  833. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  834. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  835. nstat->tx_fifo_errors = hwstat->tx_underruns;
  836. /* Don't know about heartbeat or window errors... */
  837. return nstat;
  838. }
  839. static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  840. {
  841. struct macb *bp = netdev_priv(dev);
  842. struct phy_device *phydev = bp->phy_dev;
  843. if (!phydev)
  844. return -ENODEV;
  845. return phy_ethtool_gset(phydev, cmd);
  846. }
  847. static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  848. {
  849. struct macb *bp = netdev_priv(dev);
  850. struct phy_device *phydev = bp->phy_dev;
  851. if (!phydev)
  852. return -ENODEV;
  853. return phy_ethtool_sset(phydev, cmd);
  854. }
  855. static void macb_get_drvinfo(struct net_device *dev,
  856. struct ethtool_drvinfo *info)
  857. {
  858. struct macb *bp = netdev_priv(dev);
  859. strcpy(info->driver, bp->pdev->dev.driver->name);
  860. strcpy(info->version, "$Revision: 1.14 $");
  861. strcpy(info->bus_info, dev_name(&bp->pdev->dev));
  862. }
  863. static const struct ethtool_ops macb_ethtool_ops = {
  864. .get_settings = macb_get_settings,
  865. .set_settings = macb_set_settings,
  866. .get_drvinfo = macb_get_drvinfo,
  867. .get_link = ethtool_op_get_link,
  868. };
  869. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  870. {
  871. struct macb *bp = netdev_priv(dev);
  872. struct phy_device *phydev = bp->phy_dev;
  873. if (!netif_running(dev))
  874. return -EINVAL;
  875. if (!phydev)
  876. return -ENODEV;
  877. return phy_mii_ioctl(phydev, if_mii(rq), cmd);
  878. }
  879. static const struct net_device_ops macb_netdev_ops = {
  880. .ndo_open = macb_open,
  881. .ndo_stop = macb_close,
  882. .ndo_start_xmit = macb_start_xmit,
  883. .ndo_set_multicast_list = macb_set_rx_mode,
  884. .ndo_get_stats = macb_get_stats,
  885. .ndo_do_ioctl = macb_ioctl,
  886. .ndo_validate_addr = eth_validate_addr,
  887. .ndo_change_mtu = eth_change_mtu,
  888. .ndo_set_mac_address = eth_mac_addr,
  889. #ifdef CONFIG_NET_POLL_CONTROLLER
  890. .ndo_poll_controller = macb_poll_controller,
  891. #endif
  892. };
  893. static int __init macb_probe(struct platform_device *pdev)
  894. {
  895. struct eth_platform_data *pdata;
  896. struct resource *regs;
  897. struct net_device *dev;
  898. struct macb *bp;
  899. struct phy_device *phydev;
  900. unsigned long pclk_hz;
  901. u32 config;
  902. int err = -ENXIO;
  903. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  904. if (!regs) {
  905. dev_err(&pdev->dev, "no mmio resource defined\n");
  906. goto err_out;
  907. }
  908. err = -ENOMEM;
  909. dev = alloc_etherdev(sizeof(*bp));
  910. if (!dev) {
  911. dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
  912. goto err_out;
  913. }
  914. SET_NETDEV_DEV(dev, &pdev->dev);
  915. /* TODO: Actually, we have some interesting features... */
  916. dev->features |= 0;
  917. bp = netdev_priv(dev);
  918. bp->pdev = pdev;
  919. bp->dev = dev;
  920. spin_lock_init(&bp->lock);
  921. #if defined(CONFIG_ARCH_AT91)
  922. bp->pclk = clk_get(&pdev->dev, "macb_clk");
  923. if (IS_ERR(bp->pclk)) {
  924. dev_err(&pdev->dev, "failed to get macb_clk\n");
  925. goto err_out_free_dev;
  926. }
  927. clk_enable(bp->pclk);
  928. #else
  929. bp->pclk = clk_get(&pdev->dev, "pclk");
  930. if (IS_ERR(bp->pclk)) {
  931. dev_err(&pdev->dev, "failed to get pclk\n");
  932. goto err_out_free_dev;
  933. }
  934. bp->hclk = clk_get(&pdev->dev, "hclk");
  935. if (IS_ERR(bp->hclk)) {
  936. dev_err(&pdev->dev, "failed to get hclk\n");
  937. goto err_out_put_pclk;
  938. }
  939. clk_enable(bp->pclk);
  940. clk_enable(bp->hclk);
  941. #endif
  942. bp->regs = ioremap(regs->start, regs->end - regs->start + 1);
  943. if (!bp->regs) {
  944. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  945. err = -ENOMEM;
  946. goto err_out_disable_clocks;
  947. }
  948. dev->irq = platform_get_irq(pdev, 0);
  949. err = request_irq(dev->irq, macb_interrupt, IRQF_SAMPLE_RANDOM,
  950. 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. #ifndef CONFIG_ARCH_AT91
  1011. clk_disable(bp->hclk);
  1012. clk_put(bp->hclk);
  1013. #endif
  1014. clk_disable(bp->pclk);
  1015. #ifndef CONFIG_ARCH_AT91
  1016. err_out_put_pclk:
  1017. #endif
  1018. clk_put(bp->pclk);
  1019. err_out_free_dev:
  1020. free_netdev(dev);
  1021. err_out:
  1022. platform_set_drvdata(pdev, NULL);
  1023. return err;
  1024. }
  1025. static int __exit macb_remove(struct platform_device *pdev)
  1026. {
  1027. struct net_device *dev;
  1028. struct macb *bp;
  1029. dev = platform_get_drvdata(pdev);
  1030. if (dev) {
  1031. bp = netdev_priv(dev);
  1032. if (bp->phy_dev)
  1033. phy_disconnect(bp->phy_dev);
  1034. mdiobus_unregister(bp->mii_bus);
  1035. kfree(bp->mii_bus->irq);
  1036. mdiobus_free(bp->mii_bus);
  1037. unregister_netdev(dev);
  1038. free_irq(dev->irq, dev);
  1039. iounmap(bp->regs);
  1040. #ifndef CONFIG_ARCH_AT91
  1041. clk_disable(bp->hclk);
  1042. clk_put(bp->hclk);
  1043. #endif
  1044. clk_disable(bp->pclk);
  1045. clk_put(bp->pclk);
  1046. free_netdev(dev);
  1047. platform_set_drvdata(pdev, NULL);
  1048. }
  1049. return 0;
  1050. }
  1051. #ifdef CONFIG_PM
  1052. static int macb_suspend(struct platform_device *pdev, pm_message_t state)
  1053. {
  1054. struct net_device *netdev = platform_get_drvdata(pdev);
  1055. struct macb *bp = netdev_priv(netdev);
  1056. netif_device_detach(netdev);
  1057. #ifndef CONFIG_ARCH_AT91
  1058. clk_disable(bp->hclk);
  1059. #endif
  1060. clk_disable(bp->pclk);
  1061. return 0;
  1062. }
  1063. static int macb_resume(struct platform_device *pdev)
  1064. {
  1065. struct net_device *netdev = platform_get_drvdata(pdev);
  1066. struct macb *bp = netdev_priv(netdev);
  1067. clk_enable(bp->pclk);
  1068. #ifndef CONFIG_ARCH_AT91
  1069. clk_enable(bp->hclk);
  1070. #endif
  1071. netif_device_attach(netdev);
  1072. return 0;
  1073. }
  1074. #else
  1075. #define macb_suspend NULL
  1076. #define macb_resume NULL
  1077. #endif
  1078. static struct platform_driver macb_driver = {
  1079. .remove = __exit_p(macb_remove),
  1080. .suspend = macb_suspend,
  1081. .resume = macb_resume,
  1082. .driver = {
  1083. .name = "macb",
  1084. .owner = THIS_MODULE,
  1085. },
  1086. };
  1087. static int __init macb_init(void)
  1088. {
  1089. return platform_driver_probe(&macb_driver, macb_probe);
  1090. }
  1091. static void __exit macb_exit(void)
  1092. {
  1093. platform_driver_unregister(&macb_driver);
  1094. }
  1095. module_init(macb_init);
  1096. module_exit(macb_exit);
  1097. MODULE_LICENSE("GPL");
  1098. MODULE_DESCRIPTION("Atmel MACB Ethernet driver");
  1099. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  1100. MODULE_ALIAS("platform:macb");