macb.c 31 KB

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