macb.c 32 KB

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