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