macb.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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 = NULL;
  154. struct eth_platform_data *pdata;
  155. int phy_addr;
  156. /* find the first phy */
  157. for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
  158. if (bp->mii_bus->phy_map[phy_addr]) {
  159. phydev = bp->mii_bus->phy_map[phy_addr];
  160. break;
  161. }
  162. }
  163. if (!phydev) {
  164. printk (KERN_ERR "%s: no PHY found\n", dev->name);
  165. return -1;
  166. }
  167. pdata = bp->pdev->dev.platform_data;
  168. /* TODO : add pin_irq */
  169. /* attach the mac to the phy */
  170. if (pdata && pdata->is_rmii) {
  171. phydev = phy_connect(dev, dev_name(&phydev->dev),
  172. &macb_handle_link_change, 0, PHY_INTERFACE_MODE_RMII);
  173. } else {
  174. phydev = phy_connect(dev, dev_name(&phydev->dev),
  175. &macb_handle_link_change, 0, PHY_INTERFACE_MODE_MII);
  176. }
  177. if (IS_ERR(phydev)) {
  178. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  179. return PTR_ERR(phydev);
  180. }
  181. /* mask with MAC supported features */
  182. phydev->supported &= PHY_BASIC_FEATURES;
  183. phydev->advertising = phydev->supported;
  184. bp->link = 0;
  185. bp->speed = 0;
  186. bp->duplex = -1;
  187. bp->phy_dev = phydev;
  188. return 0;
  189. }
  190. static int macb_mii_init(struct macb *bp)
  191. {
  192. struct eth_platform_data *pdata;
  193. int err = -ENXIO, i;
  194. /* Enable managment port */
  195. macb_writel(bp, NCR, MACB_BIT(MPE));
  196. bp->mii_bus = mdiobus_alloc();
  197. if (bp->mii_bus == NULL) {
  198. err = -ENOMEM;
  199. goto err_out;
  200. }
  201. bp->mii_bus->name = "MACB_mii_bus";
  202. bp->mii_bus->read = &macb_mdio_read;
  203. bp->mii_bus->write = &macb_mdio_write;
  204. bp->mii_bus->reset = &macb_mdio_reset;
  205. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%x", bp->pdev->id);
  206. bp->mii_bus->priv = bp;
  207. bp->mii_bus->parent = &bp->dev->dev;
  208. pdata = bp->pdev->dev.platform_data;
  209. if (pdata)
  210. bp->mii_bus->phy_mask = pdata->phy_mask;
  211. bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  212. if (!bp->mii_bus->irq) {
  213. err = -ENOMEM;
  214. goto err_out_free_mdiobus;
  215. }
  216. for (i = 0; i < PHY_MAX_ADDR; i++)
  217. bp->mii_bus->irq[i] = PHY_POLL;
  218. platform_set_drvdata(bp->dev, bp->mii_bus);
  219. if (mdiobus_register(bp->mii_bus))
  220. goto err_out_free_mdio_irq;
  221. if (macb_mii_probe(bp->dev) != 0) {
  222. goto err_out_unregister_bus;
  223. }
  224. return 0;
  225. err_out_unregister_bus:
  226. mdiobus_unregister(bp->mii_bus);
  227. err_out_free_mdio_irq:
  228. kfree(bp->mii_bus->irq);
  229. err_out_free_mdiobus:
  230. mdiobus_free(bp->mii_bus);
  231. err_out:
  232. return err;
  233. }
  234. static void macb_update_stats(struct macb *bp)
  235. {
  236. u32 __iomem *reg = bp->regs + MACB_PFR;
  237. u32 *p = &bp->hw_stats.rx_pause_frames;
  238. u32 *end = &bp->hw_stats.tx_pause_frames + 1;
  239. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  240. for(; p < end; p++, reg++)
  241. *p += __raw_readl(reg);
  242. }
  243. static void macb_tx(struct macb *bp)
  244. {
  245. unsigned int tail;
  246. unsigned int head;
  247. u32 status;
  248. status = macb_readl(bp, TSR);
  249. macb_writel(bp, TSR, status);
  250. dev_dbg(&bp->pdev->dev, "macb_tx status = %02lx\n",
  251. (unsigned long)status);
  252. if (status & MACB_BIT(UND)) {
  253. int i;
  254. printk(KERN_ERR "%s: TX underrun, resetting buffers\n",
  255. bp->dev->name);
  256. /* Transfer ongoing, disable transmitter, to avoid confusion */
  257. if (status & MACB_BIT(TGO))
  258. macb_writel(bp, NCR, macb_readl(bp, NCR) & ~MACB_BIT(TE));
  259. head = bp->tx_head;
  260. /*Mark all the buffer as used to avoid sending a lost buffer*/
  261. for (i = 0; i < TX_RING_SIZE; i++)
  262. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  263. /* free transmit buffer in upper layer*/
  264. for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
  265. struct ring_info *rp = &bp->tx_skb[tail];
  266. struct sk_buff *skb = rp->skb;
  267. BUG_ON(skb == NULL);
  268. rmb();
  269. dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
  270. DMA_TO_DEVICE);
  271. rp->skb = NULL;
  272. dev_kfree_skb_irq(skb);
  273. }
  274. bp->tx_head = bp->tx_tail = 0;
  275. /* Enable the transmitter again */
  276. if (status & MACB_BIT(TGO))
  277. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE));
  278. }
  279. if (!(status & MACB_BIT(COMP)))
  280. /*
  281. * This may happen when a buffer becomes complete
  282. * between reading the ISR and scanning the
  283. * descriptors. Nothing to worry about.
  284. */
  285. return;
  286. head = bp->tx_head;
  287. for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
  288. struct ring_info *rp = &bp->tx_skb[tail];
  289. struct sk_buff *skb = rp->skb;
  290. u32 bufstat;
  291. BUG_ON(skb == NULL);
  292. rmb();
  293. bufstat = bp->tx_ring[tail].ctrl;
  294. if (!(bufstat & MACB_BIT(TX_USED)))
  295. break;
  296. dev_dbg(&bp->pdev->dev, "skb %u (data %p) TX complete\n",
  297. tail, skb->data);
  298. dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
  299. DMA_TO_DEVICE);
  300. bp->stats.tx_packets++;
  301. bp->stats.tx_bytes += skb->len;
  302. rp->skb = NULL;
  303. dev_kfree_skb_irq(skb);
  304. }
  305. bp->tx_tail = tail;
  306. if (netif_queue_stopped(bp->dev) &&
  307. TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH)
  308. netif_wake_queue(bp->dev);
  309. }
  310. static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
  311. unsigned int last_frag)
  312. {
  313. unsigned int len;
  314. unsigned int frag;
  315. unsigned int offset = 0;
  316. struct sk_buff *skb;
  317. len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl);
  318. dev_dbg(&bp->pdev->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  319. first_frag, last_frag, len);
  320. skb = dev_alloc_skb(len + RX_OFFSET);
  321. if (!skb) {
  322. bp->stats.rx_dropped++;
  323. for (frag = first_frag; ; frag = NEXT_RX(frag)) {
  324. bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
  325. if (frag == last_frag)
  326. break;
  327. }
  328. wmb();
  329. return 1;
  330. }
  331. skb_reserve(skb, RX_OFFSET);
  332. skb->ip_summed = CHECKSUM_NONE;
  333. skb_put(skb, len);
  334. for (frag = first_frag; ; frag = NEXT_RX(frag)) {
  335. unsigned int frag_len = RX_BUFFER_SIZE;
  336. if (offset + frag_len > len) {
  337. BUG_ON(frag != last_frag);
  338. frag_len = len - offset;
  339. }
  340. skb_copy_to_linear_data_offset(skb, offset,
  341. (bp->rx_buffers +
  342. (RX_BUFFER_SIZE * frag)),
  343. frag_len);
  344. offset += RX_BUFFER_SIZE;
  345. bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
  346. wmb();
  347. if (frag == last_frag)
  348. break;
  349. }
  350. skb->protocol = eth_type_trans(skb, bp->dev);
  351. bp->stats.rx_packets++;
  352. bp->stats.rx_bytes += len;
  353. dev_dbg(&bp->pdev->dev, "received skb of length %u, csum: %08x\n",
  354. skb->len, skb->csum);
  355. netif_receive_skb(skb);
  356. return 0;
  357. }
  358. /* Mark DMA descriptors from begin up to and not including end as unused */
  359. static void discard_partial_frame(struct macb *bp, unsigned int begin,
  360. unsigned int end)
  361. {
  362. unsigned int frag;
  363. for (frag = begin; frag != end; frag = NEXT_RX(frag))
  364. bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
  365. wmb();
  366. /*
  367. * When this happens, the hardware stats registers for
  368. * whatever caused this is updated, so we don't have to record
  369. * anything.
  370. */
  371. }
  372. static int macb_rx(struct macb *bp, int budget)
  373. {
  374. int received = 0;
  375. unsigned int tail = bp->rx_tail;
  376. int first_frag = -1;
  377. for (; budget > 0; tail = NEXT_RX(tail)) {
  378. u32 addr, ctrl;
  379. rmb();
  380. addr = bp->rx_ring[tail].addr;
  381. ctrl = bp->rx_ring[tail].ctrl;
  382. if (!(addr & MACB_BIT(RX_USED)))
  383. break;
  384. if (ctrl & MACB_BIT(RX_SOF)) {
  385. if (first_frag != -1)
  386. discard_partial_frame(bp, first_frag, tail);
  387. first_frag = tail;
  388. }
  389. if (ctrl & MACB_BIT(RX_EOF)) {
  390. int dropped;
  391. BUG_ON(first_frag == -1);
  392. dropped = macb_rx_frame(bp, first_frag, tail);
  393. first_frag = -1;
  394. if (!dropped) {
  395. received++;
  396. budget--;
  397. }
  398. }
  399. }
  400. if (first_frag != -1)
  401. bp->rx_tail = first_frag;
  402. else
  403. bp->rx_tail = tail;
  404. return received;
  405. }
  406. static int macb_poll(struct napi_struct *napi, int budget)
  407. {
  408. struct macb *bp = container_of(napi, struct macb, napi);
  409. int work_done;
  410. u32 status;
  411. status = macb_readl(bp, RSR);
  412. macb_writel(bp, RSR, status);
  413. work_done = 0;
  414. if (!status) {
  415. /*
  416. * This may happen if an interrupt was pending before
  417. * this function was called last time, and no packets
  418. * have been received since.
  419. */
  420. napi_complete(napi);
  421. goto out;
  422. }
  423. dev_dbg(&bp->pdev->dev, "poll: status = %08lx, budget = %d\n",
  424. (unsigned long)status, budget);
  425. if (!(status & MACB_BIT(REC))) {
  426. dev_warn(&bp->pdev->dev,
  427. "No RX buffers complete, status = %02lx\n",
  428. (unsigned long)status);
  429. napi_complete(napi);
  430. goto out;
  431. }
  432. work_done = macb_rx(bp, budget);
  433. if (work_done < budget)
  434. napi_complete(napi);
  435. /*
  436. * We've done what we can to clean the buffers. Make sure we
  437. * get notified when new packets arrive.
  438. */
  439. out:
  440. macb_writel(bp, IER, MACB_RX_INT_FLAGS);
  441. /* TODO: Handle errors */
  442. return work_done;
  443. }
  444. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  445. {
  446. struct net_device *dev = dev_id;
  447. struct macb *bp = netdev_priv(dev);
  448. u32 status;
  449. status = macb_readl(bp, ISR);
  450. if (unlikely(!status))
  451. return IRQ_NONE;
  452. spin_lock(&bp->lock);
  453. while (status) {
  454. /* close possible race with dev_close */
  455. if (unlikely(!netif_running(dev))) {
  456. macb_writel(bp, IDR, ~0UL);
  457. break;
  458. }
  459. if (status & MACB_RX_INT_FLAGS) {
  460. if (napi_schedule_prep(&bp->napi)) {
  461. /*
  462. * There's no point taking any more interrupts
  463. * until we have processed the buffers
  464. */
  465. macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
  466. dev_dbg(&bp->pdev->dev,
  467. "scheduling RX softirq\n");
  468. __napi_schedule(&bp->napi);
  469. }
  470. }
  471. if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND)))
  472. macb_tx(bp);
  473. /*
  474. * Link change detection isn't possible with RMII, so we'll
  475. * add that if/when we get our hands on a full-blown MII PHY.
  476. */
  477. if (status & MACB_BIT(HRESP)) {
  478. /*
  479. * TODO: Reset the hardware, and maybe move the printk
  480. * to a lower-priority context as well (work queue?)
  481. */
  482. printk(KERN_ERR "%s: DMA bus error: HRESP not OK\n",
  483. dev->name);
  484. }
  485. status = macb_readl(bp, ISR);
  486. }
  487. spin_unlock(&bp->lock);
  488. return IRQ_HANDLED;
  489. }
  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. #ifdef DEBUG
  497. int i;
  498. dev_dbg(&bp->pdev->dev,
  499. "start_xmit: len %u head %p data %p tail %p end %p\n",
  500. skb->len, skb->head, skb->data,
  501. skb_tail_pointer(skb), skb_end_pointer(skb));
  502. dev_dbg(&bp->pdev->dev,
  503. "data:");
  504. for (i = 0; i < 16; i++)
  505. printk(" %02x", (unsigned int)skb->data[i]);
  506. printk("\n");
  507. #endif
  508. len = skb->len;
  509. spin_lock_irq(&bp->lock);
  510. /* This is a hard error, log it. */
  511. if (TX_BUFFS_AVAIL(bp) < 1) {
  512. netif_stop_queue(dev);
  513. spin_unlock_irq(&bp->lock);
  514. dev_err(&bp->pdev->dev,
  515. "BUG! Tx Ring full when queue awake!\n");
  516. dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
  517. bp->tx_head, bp->tx_tail);
  518. return 1;
  519. }
  520. entry = bp->tx_head;
  521. dev_dbg(&bp->pdev->dev, "Allocated ring entry %u\n", entry);
  522. mapping = dma_map_single(&bp->pdev->dev, skb->data,
  523. len, DMA_TO_DEVICE);
  524. bp->tx_skb[entry].skb = skb;
  525. bp->tx_skb[entry].mapping = mapping;
  526. dev_dbg(&bp->pdev->dev, "Mapped skb data %p to DMA addr %08lx\n",
  527. skb->data, (unsigned long)mapping);
  528. ctrl = MACB_BF(TX_FRMLEN, len);
  529. ctrl |= MACB_BIT(TX_LAST);
  530. if (entry == (TX_RING_SIZE - 1))
  531. ctrl |= MACB_BIT(TX_WRAP);
  532. bp->tx_ring[entry].addr = mapping;
  533. bp->tx_ring[entry].ctrl = ctrl;
  534. wmb();
  535. entry = NEXT_TX(entry);
  536. bp->tx_head = entry;
  537. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  538. if (TX_BUFFS_AVAIL(bp) < 1)
  539. netif_stop_queue(dev);
  540. spin_unlock_irq(&bp->lock);
  541. dev->trans_start = jiffies;
  542. return 0;
  543. }
  544. static void macb_free_consistent(struct macb *bp)
  545. {
  546. if (bp->tx_skb) {
  547. kfree(bp->tx_skb);
  548. bp->tx_skb = NULL;
  549. }
  550. if (bp->rx_ring) {
  551. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
  552. bp->rx_ring, bp->rx_ring_dma);
  553. bp->rx_ring = NULL;
  554. }
  555. if (bp->tx_ring) {
  556. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
  557. bp->tx_ring, bp->tx_ring_dma);
  558. bp->tx_ring = NULL;
  559. }
  560. if (bp->rx_buffers) {
  561. dma_free_coherent(&bp->pdev->dev,
  562. RX_RING_SIZE * RX_BUFFER_SIZE,
  563. bp->rx_buffers, bp->rx_buffers_dma);
  564. bp->rx_buffers = NULL;
  565. }
  566. }
  567. static int macb_alloc_consistent(struct macb *bp)
  568. {
  569. int size;
  570. size = TX_RING_SIZE * sizeof(struct ring_info);
  571. bp->tx_skb = kmalloc(size, GFP_KERNEL);
  572. if (!bp->tx_skb)
  573. goto out_err;
  574. size = RX_RING_BYTES;
  575. bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  576. &bp->rx_ring_dma, GFP_KERNEL);
  577. if (!bp->rx_ring)
  578. goto out_err;
  579. dev_dbg(&bp->pdev->dev,
  580. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  581. size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
  582. size = TX_RING_BYTES;
  583. bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  584. &bp->tx_ring_dma, GFP_KERNEL);
  585. if (!bp->tx_ring)
  586. goto out_err;
  587. dev_dbg(&bp->pdev->dev,
  588. "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
  589. size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
  590. size = RX_RING_SIZE * RX_BUFFER_SIZE;
  591. bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  592. &bp->rx_buffers_dma, GFP_KERNEL);
  593. if (!bp->rx_buffers)
  594. goto out_err;
  595. dev_dbg(&bp->pdev->dev,
  596. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  597. size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
  598. return 0;
  599. out_err:
  600. macb_free_consistent(bp);
  601. return -ENOMEM;
  602. }
  603. static void macb_init_rings(struct macb *bp)
  604. {
  605. int i;
  606. dma_addr_t addr;
  607. addr = bp->rx_buffers_dma;
  608. for (i = 0; i < RX_RING_SIZE; i++) {
  609. bp->rx_ring[i].addr = addr;
  610. bp->rx_ring[i].ctrl = 0;
  611. addr += RX_BUFFER_SIZE;
  612. }
  613. bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
  614. for (i = 0; i < TX_RING_SIZE; i++) {
  615. bp->tx_ring[i].addr = 0;
  616. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  617. }
  618. bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  619. bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
  620. }
  621. static void macb_reset_hw(struct macb *bp)
  622. {
  623. /* Make sure we have the write buffer for ourselves */
  624. wmb();
  625. /*
  626. * Disable RX and TX (XXX: Should we halt the transmission
  627. * more gracefully?)
  628. */
  629. macb_writel(bp, NCR, 0);
  630. /* Clear the stats registers (XXX: Update stats first?) */
  631. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  632. /* Clear all status flags */
  633. macb_writel(bp, TSR, ~0UL);
  634. macb_writel(bp, RSR, ~0UL);
  635. /* Disable all interrupts */
  636. macb_writel(bp, IDR, ~0UL);
  637. macb_readl(bp, ISR);
  638. }
  639. static void macb_init_hw(struct macb *bp)
  640. {
  641. u32 config;
  642. macb_reset_hw(bp);
  643. __macb_set_hwaddr(bp);
  644. config = macb_readl(bp, NCFGR) & MACB_BF(CLK, -1L);
  645. config |= MACB_BIT(PAE); /* PAuse Enable */
  646. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  647. if (bp->dev->flags & IFF_PROMISC)
  648. config |= MACB_BIT(CAF); /* Copy All Frames */
  649. if (!(bp->dev->flags & IFF_BROADCAST))
  650. config |= MACB_BIT(NBC); /* No BroadCast */
  651. macb_writel(bp, NCFGR, config);
  652. /* Initialize TX and RX buffers */
  653. macb_writel(bp, RBQP, bp->rx_ring_dma);
  654. macb_writel(bp, TBQP, bp->tx_ring_dma);
  655. /* Enable TX and RX */
  656. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  657. /* Enable interrupts */
  658. macb_writel(bp, IER, (MACB_BIT(RCOMP)
  659. | MACB_BIT(RXUBR)
  660. | MACB_BIT(ISR_TUND)
  661. | MACB_BIT(ISR_RLE)
  662. | MACB_BIT(TXERR)
  663. | MACB_BIT(TCOMP)
  664. | MACB_BIT(ISR_ROVR)
  665. | MACB_BIT(HRESP)));
  666. }
  667. /*
  668. * The hash address register is 64 bits long and takes up two
  669. * locations in the memory map. The least significant bits are stored
  670. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  671. *
  672. * The unicast hash enable and the multicast hash enable bits in the
  673. * network configuration register enable the reception of hash matched
  674. * frames. The destination address is reduced to a 6 bit index into
  675. * the 64 bit hash register using the following hash function. The
  676. * hash function is an exclusive or of every sixth bit of the
  677. * destination address.
  678. *
  679. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  680. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  681. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  682. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  683. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  684. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  685. *
  686. * da[0] represents the least significant bit of the first byte
  687. * received, that is, the multicast/unicast indicator, and da[47]
  688. * represents the most significant bit of the last byte received. If
  689. * the hash index, hi[n], points to a bit that is set in the hash
  690. * register then the frame will be matched according to whether the
  691. * frame is multicast or unicast. A multicast match will be signalled
  692. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  693. * index points to a bit set in the hash register. A unicast match
  694. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  695. * and the hash index points to a bit set in the hash register. To
  696. * receive all multicast frames, the hash register should be set with
  697. * all ones and the multicast hash enable bit should be set in the
  698. * network configuration register.
  699. */
  700. static inline int hash_bit_value(int bitnr, __u8 *addr)
  701. {
  702. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  703. return 1;
  704. return 0;
  705. }
  706. /*
  707. * Return the hash index value for the specified address.
  708. */
  709. static int hash_get_index(__u8 *addr)
  710. {
  711. int i, j, bitval;
  712. int hash_index = 0;
  713. for (j = 0; j < 6; j++) {
  714. for (i = 0, bitval = 0; i < 8; i++)
  715. bitval ^= hash_bit_value(i*6 + j, addr);
  716. hash_index |= (bitval << j);
  717. }
  718. return hash_index;
  719. }
  720. /*
  721. * Add multicast addresses to the internal multicast-hash table.
  722. */
  723. static void macb_sethashtable(struct net_device *dev)
  724. {
  725. struct dev_mc_list *curr;
  726. unsigned long mc_filter[2];
  727. unsigned int i, bitnr;
  728. struct macb *bp = netdev_priv(dev);
  729. mc_filter[0] = mc_filter[1] = 0;
  730. curr = dev->mc_list;
  731. for (i = 0; i < dev->mc_count; i++, curr = curr->next) {
  732. if (!curr) break; /* unexpected end of list */
  733. bitnr = hash_get_index(curr->dmi_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 (dev->mc_count > 0) {
  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. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  840. nstat->rx_frame_errors = hwstat->rx_align_errors;
  841. nstat->rx_fifo_errors = hwstat->rx_overruns;
  842. /* XXX: What does "missed" mean? */
  843. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  844. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  845. nstat->tx_fifo_errors = hwstat->tx_underruns;
  846. /* Don't know about heartbeat or window errors... */
  847. return nstat;
  848. }
  849. static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  850. {
  851. struct macb *bp = netdev_priv(dev);
  852. struct phy_device *phydev = bp->phy_dev;
  853. if (!phydev)
  854. return -ENODEV;
  855. return phy_ethtool_gset(phydev, cmd);
  856. }
  857. static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  858. {
  859. struct macb *bp = netdev_priv(dev);
  860. struct phy_device *phydev = bp->phy_dev;
  861. if (!phydev)
  862. return -ENODEV;
  863. return phy_ethtool_sset(phydev, cmd);
  864. }
  865. static void macb_get_drvinfo(struct net_device *dev,
  866. struct ethtool_drvinfo *info)
  867. {
  868. struct macb *bp = netdev_priv(dev);
  869. strcpy(info->driver, bp->pdev->dev.driver->name);
  870. strcpy(info->version, "$Revision: 1.14 $");
  871. strcpy(info->bus_info, dev_name(&bp->pdev->dev));
  872. }
  873. static struct ethtool_ops macb_ethtool_ops = {
  874. .get_settings = macb_get_settings,
  875. .set_settings = macb_set_settings,
  876. .get_drvinfo = macb_get_drvinfo,
  877. .get_link = ethtool_op_get_link,
  878. };
  879. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  880. {
  881. struct macb *bp = netdev_priv(dev);
  882. struct phy_device *phydev = bp->phy_dev;
  883. if (!netif_running(dev))
  884. return -EINVAL;
  885. if (!phydev)
  886. return -ENODEV;
  887. return phy_mii_ioctl(phydev, if_mii(rq), cmd);
  888. }
  889. static int __init macb_probe(struct platform_device *pdev)
  890. {
  891. struct eth_platform_data *pdata;
  892. struct resource *regs;
  893. struct net_device *dev;
  894. struct macb *bp;
  895. struct phy_device *phydev;
  896. unsigned long pclk_hz;
  897. u32 config;
  898. int err = -ENXIO;
  899. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  900. if (!regs) {
  901. dev_err(&pdev->dev, "no mmio resource defined\n");
  902. goto err_out;
  903. }
  904. err = -ENOMEM;
  905. dev = alloc_etherdev(sizeof(*bp));
  906. if (!dev) {
  907. dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
  908. goto err_out;
  909. }
  910. SET_NETDEV_DEV(dev, &pdev->dev);
  911. /* TODO: Actually, we have some interesting features... */
  912. dev->features |= 0;
  913. bp = netdev_priv(dev);
  914. bp->pdev = pdev;
  915. bp->dev = dev;
  916. spin_lock_init(&bp->lock);
  917. #if defined(CONFIG_ARCH_AT91)
  918. bp->pclk = clk_get(&pdev->dev, "macb_clk");
  919. if (IS_ERR(bp->pclk)) {
  920. dev_err(&pdev->dev, "failed to get macb_clk\n");
  921. goto err_out_free_dev;
  922. }
  923. clk_enable(bp->pclk);
  924. #else
  925. bp->pclk = clk_get(&pdev->dev, "pclk");
  926. if (IS_ERR(bp->pclk)) {
  927. dev_err(&pdev->dev, "failed to get pclk\n");
  928. goto err_out_free_dev;
  929. }
  930. bp->hclk = clk_get(&pdev->dev, "hclk");
  931. if (IS_ERR(bp->hclk)) {
  932. dev_err(&pdev->dev, "failed to get hclk\n");
  933. goto err_out_put_pclk;
  934. }
  935. clk_enable(bp->pclk);
  936. clk_enable(bp->hclk);
  937. #endif
  938. bp->regs = ioremap(regs->start, regs->end - regs->start + 1);
  939. if (!bp->regs) {
  940. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  941. err = -ENOMEM;
  942. goto err_out_disable_clocks;
  943. }
  944. dev->irq = platform_get_irq(pdev, 0);
  945. err = request_irq(dev->irq, macb_interrupt, IRQF_SAMPLE_RANDOM,
  946. dev->name, dev);
  947. if (err) {
  948. printk(KERN_ERR
  949. "%s: Unable to request IRQ %d (error %d)\n",
  950. dev->name, dev->irq, err);
  951. goto err_out_iounmap;
  952. }
  953. dev->open = macb_open;
  954. dev->stop = macb_close;
  955. dev->hard_start_xmit = macb_start_xmit;
  956. dev->get_stats = macb_get_stats;
  957. dev->set_multicast_list = macb_set_rx_mode;
  958. dev->do_ioctl = macb_ioctl;
  959. netif_napi_add(dev, &bp->napi, macb_poll, 64);
  960. dev->ethtool_ops = &macb_ethtool_ops;
  961. dev->base_addr = regs->start;
  962. /* Set MII management clock divider */
  963. pclk_hz = clk_get_rate(bp->pclk);
  964. if (pclk_hz <= 20000000)
  965. config = MACB_BF(CLK, MACB_CLK_DIV8);
  966. else if (pclk_hz <= 40000000)
  967. config = MACB_BF(CLK, MACB_CLK_DIV16);
  968. else if (pclk_hz <= 80000000)
  969. config = MACB_BF(CLK, MACB_CLK_DIV32);
  970. else
  971. config = MACB_BF(CLK, MACB_CLK_DIV64);
  972. macb_writel(bp, NCFGR, config);
  973. macb_get_hwaddr(bp);
  974. pdata = pdev->dev.platform_data;
  975. if (pdata && pdata->is_rmii)
  976. #if defined(CONFIG_ARCH_AT91)
  977. macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) );
  978. #else
  979. macb_writel(bp, USRIO, 0);
  980. #endif
  981. else
  982. #if defined(CONFIG_ARCH_AT91)
  983. macb_writel(bp, USRIO, MACB_BIT(CLKEN));
  984. #else
  985. macb_writel(bp, USRIO, MACB_BIT(MII));
  986. #endif
  987. bp->tx_pending = DEF_TX_RING_PENDING;
  988. err = register_netdev(dev);
  989. if (err) {
  990. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  991. goto err_out_free_irq;
  992. }
  993. if (macb_mii_init(bp) != 0) {
  994. goto err_out_unregister_netdev;
  995. }
  996. platform_set_drvdata(pdev, dev);
  997. printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d (%pM)\n",
  998. dev->name, dev->base_addr, dev->irq, dev->dev_addr);
  999. phydev = bp->phy_dev;
  1000. printk(KERN_INFO "%s: attached PHY driver [%s] "
  1001. "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
  1002. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  1003. return 0;
  1004. err_out_unregister_netdev:
  1005. unregister_netdev(dev);
  1006. err_out_free_irq:
  1007. free_irq(dev->irq, dev);
  1008. err_out_iounmap:
  1009. iounmap(bp->regs);
  1010. err_out_disable_clocks:
  1011. #ifndef CONFIG_ARCH_AT91
  1012. clk_disable(bp->hclk);
  1013. clk_put(bp->hclk);
  1014. #endif
  1015. clk_disable(bp->pclk);
  1016. #ifndef CONFIG_ARCH_AT91
  1017. err_out_put_pclk:
  1018. #endif
  1019. clk_put(bp->pclk);
  1020. err_out_free_dev:
  1021. free_netdev(dev);
  1022. err_out:
  1023. platform_set_drvdata(pdev, NULL);
  1024. return err;
  1025. }
  1026. static int __exit macb_remove(struct platform_device *pdev)
  1027. {
  1028. struct net_device *dev;
  1029. struct macb *bp;
  1030. dev = platform_get_drvdata(pdev);
  1031. if (dev) {
  1032. bp = netdev_priv(dev);
  1033. if (bp->phy_dev)
  1034. phy_disconnect(bp->phy_dev);
  1035. mdiobus_unregister(bp->mii_bus);
  1036. kfree(bp->mii_bus->irq);
  1037. mdiobus_free(bp->mii_bus);
  1038. unregister_netdev(dev);
  1039. free_irq(dev->irq, dev);
  1040. iounmap(bp->regs);
  1041. #ifndef CONFIG_ARCH_AT91
  1042. clk_disable(bp->hclk);
  1043. clk_put(bp->hclk);
  1044. #endif
  1045. clk_disable(bp->pclk);
  1046. clk_put(bp->pclk);
  1047. free_netdev(dev);
  1048. platform_set_drvdata(pdev, NULL);
  1049. }
  1050. return 0;
  1051. }
  1052. #ifdef CONFIG_PM
  1053. static int macb_suspend(struct platform_device *pdev, pm_message_t state)
  1054. {
  1055. struct net_device *netdev = platform_get_drvdata(pdev);
  1056. struct macb *bp = netdev_priv(netdev);
  1057. netif_device_detach(netdev);
  1058. #ifndef CONFIG_ARCH_AT91
  1059. clk_disable(bp->hclk);
  1060. #endif
  1061. clk_disable(bp->pclk);
  1062. return 0;
  1063. }
  1064. static int macb_resume(struct platform_device *pdev)
  1065. {
  1066. struct net_device *netdev = platform_get_drvdata(pdev);
  1067. struct macb *bp = netdev_priv(netdev);
  1068. clk_enable(bp->pclk);
  1069. #ifndef CONFIG_ARCH_AT91
  1070. clk_enable(bp->hclk);
  1071. #endif
  1072. netif_device_attach(netdev);
  1073. return 0;
  1074. }
  1075. #else
  1076. #define macb_suspend NULL
  1077. #define macb_resume NULL
  1078. #endif
  1079. static struct platform_driver macb_driver = {
  1080. .remove = __exit_p(macb_remove),
  1081. .suspend = macb_suspend,
  1082. .resume = macb_resume,
  1083. .driver = {
  1084. .name = "macb",
  1085. .owner = THIS_MODULE,
  1086. },
  1087. };
  1088. static int __init macb_init(void)
  1089. {
  1090. return platform_driver_probe(&macb_driver, macb_probe);
  1091. }
  1092. static void __exit macb_exit(void)
  1093. {
  1094. platform_driver_unregister(&macb_driver);
  1095. }
  1096. module_init(macb_init);
  1097. module_exit(macb_exit);
  1098. MODULE_LICENSE("GPL");
  1099. MODULE_DESCRIPTION("Atmel MACB Ethernet driver");
  1100. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  1101. MODULE_ALIAS("platform:macb");