macb.c 31 KB

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