macb.c 31 KB

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