macb.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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. platform_set_drvdata(bp->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->ip_summed = CHECKSUM_NONE;
  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. /* TODO: Handle errors */
  417. return work_done;
  418. }
  419. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  420. {
  421. struct net_device *dev = dev_id;
  422. struct macb *bp = netdev_priv(dev);
  423. u32 status;
  424. status = macb_readl(bp, ISR);
  425. if (unlikely(!status))
  426. return IRQ_NONE;
  427. spin_lock(&bp->lock);
  428. while (status) {
  429. /* close possible race with dev_close */
  430. if (unlikely(!netif_running(dev))) {
  431. macb_writel(bp, IDR, ~0UL);
  432. break;
  433. }
  434. if (status & MACB_RX_INT_FLAGS) {
  435. if (napi_schedule_prep(&bp->napi)) {
  436. /*
  437. * There's no point taking any more interrupts
  438. * until we have processed the buffers
  439. */
  440. macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
  441. dev_dbg(&bp->pdev->dev,
  442. "scheduling RX softirq\n");
  443. __napi_schedule(&bp->napi);
  444. }
  445. }
  446. if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND) |
  447. MACB_BIT(ISR_RLE)))
  448. macb_tx(bp);
  449. /*
  450. * Link change detection isn't possible with RMII, so we'll
  451. * add that if/when we get our hands on a full-blown MII PHY.
  452. */
  453. if (status & MACB_BIT(HRESP)) {
  454. /*
  455. * TODO: Reset the hardware, and maybe move the printk
  456. * to a lower-priority context as well (work queue?)
  457. */
  458. printk(KERN_ERR "%s: DMA bus error: HRESP not OK\n",
  459. dev->name);
  460. }
  461. status = macb_readl(bp, ISR);
  462. }
  463. spin_unlock(&bp->lock);
  464. return IRQ_HANDLED;
  465. }
  466. #ifdef CONFIG_NET_POLL_CONTROLLER
  467. /*
  468. * Polling receive - used by netconsole and other diagnostic tools
  469. * to allow network i/o with interrupts disabled.
  470. */
  471. static void macb_poll_controller(struct net_device *dev)
  472. {
  473. unsigned long flags;
  474. local_irq_save(flags);
  475. macb_interrupt(dev->irq, dev);
  476. local_irq_restore(flags);
  477. }
  478. #endif
  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. unsigned long flags;
  486. #ifdef DEBUG
  487. int i;
  488. dev_dbg(&bp->pdev->dev,
  489. "start_xmit: len %u head %p data %p tail %p end %p\n",
  490. skb->len, skb->head, skb->data,
  491. skb_tail_pointer(skb), skb_end_pointer(skb));
  492. dev_dbg(&bp->pdev->dev,
  493. "data:");
  494. for (i = 0; i < 16; i++)
  495. printk(" %02x", (unsigned int)skb->data[i]);
  496. printk("\n");
  497. #endif
  498. len = skb->len;
  499. spin_lock_irqsave(&bp->lock, flags);
  500. /* This is a hard error, log it. */
  501. if (TX_BUFFS_AVAIL(bp) < 1) {
  502. netif_stop_queue(dev);
  503. spin_unlock_irqrestore(&bp->lock, flags);
  504. dev_err(&bp->pdev->dev,
  505. "BUG! Tx Ring full when queue awake!\n");
  506. dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
  507. bp->tx_head, bp->tx_tail);
  508. return NETDEV_TX_BUSY;
  509. }
  510. entry = bp->tx_head;
  511. dev_dbg(&bp->pdev->dev, "Allocated ring entry %u\n", entry);
  512. mapping = dma_map_single(&bp->pdev->dev, skb->data,
  513. len, DMA_TO_DEVICE);
  514. bp->tx_skb[entry].skb = skb;
  515. bp->tx_skb[entry].mapping = mapping;
  516. dev_dbg(&bp->pdev->dev, "Mapped skb data %p to DMA addr %08lx\n",
  517. skb->data, (unsigned long)mapping);
  518. ctrl = MACB_BF(TX_FRMLEN, len);
  519. ctrl |= MACB_BIT(TX_LAST);
  520. if (entry == (TX_RING_SIZE - 1))
  521. ctrl |= MACB_BIT(TX_WRAP);
  522. bp->tx_ring[entry].addr = mapping;
  523. bp->tx_ring[entry].ctrl = ctrl;
  524. wmb();
  525. entry = NEXT_TX(entry);
  526. bp->tx_head = entry;
  527. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  528. if (TX_BUFFS_AVAIL(bp) < 1)
  529. netif_stop_queue(dev);
  530. spin_unlock_irqrestore(&bp->lock, flags);
  531. return NETDEV_TX_OK;
  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. config |= MACB_BIT(BIG); /* Receive oversized frames */
  637. if (bp->dev->flags & IFF_PROMISC)
  638. config |= MACB_BIT(CAF); /* Copy All Frames */
  639. if (!(bp->dev->flags & IFF_BROADCAST))
  640. config |= MACB_BIT(NBC); /* No BroadCast */
  641. macb_writel(bp, NCFGR, config);
  642. /* Initialize TX and RX buffers */
  643. macb_writel(bp, RBQP, bp->rx_ring_dma);
  644. macb_writel(bp, TBQP, bp->tx_ring_dma);
  645. /* Enable TX and RX */
  646. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  647. /* Enable interrupts */
  648. macb_writel(bp, IER, (MACB_BIT(RCOMP)
  649. | MACB_BIT(RXUBR)
  650. | MACB_BIT(ISR_TUND)
  651. | MACB_BIT(ISR_RLE)
  652. | MACB_BIT(TXERR)
  653. | MACB_BIT(TCOMP)
  654. | MACB_BIT(ISR_ROVR)
  655. | MACB_BIT(HRESP)));
  656. }
  657. /*
  658. * The hash address register is 64 bits long and takes up two
  659. * locations in the memory map. The least significant bits are stored
  660. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  661. *
  662. * The unicast hash enable and the multicast hash enable bits in the
  663. * network configuration register enable the reception of hash matched
  664. * frames. The destination address is reduced to a 6 bit index into
  665. * the 64 bit hash register using the following hash function. The
  666. * hash function is an exclusive or of every sixth bit of the
  667. * destination address.
  668. *
  669. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  670. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  671. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  672. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  673. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  674. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  675. *
  676. * da[0] represents the least significant bit of the first byte
  677. * received, that is, the multicast/unicast indicator, and da[47]
  678. * represents the most significant bit of the last byte received. If
  679. * the hash index, hi[n], points to a bit that is set in the hash
  680. * register then the frame will be matched according to whether the
  681. * frame is multicast or unicast. A multicast match will be signalled
  682. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  683. * index points to a bit set in the hash register. A unicast match
  684. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  685. * and the hash index points to a bit set in the hash register. To
  686. * receive all multicast frames, the hash register should be set with
  687. * all ones and the multicast hash enable bit should be set in the
  688. * network configuration register.
  689. */
  690. static inline int hash_bit_value(int bitnr, __u8 *addr)
  691. {
  692. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  693. return 1;
  694. return 0;
  695. }
  696. /*
  697. * Return the hash index value for the specified address.
  698. */
  699. static int hash_get_index(__u8 *addr)
  700. {
  701. int i, j, bitval;
  702. int hash_index = 0;
  703. for (j = 0; j < 6; j++) {
  704. for (i = 0, bitval = 0; i < 8; i++)
  705. bitval ^= hash_bit_value(i*6 + j, addr);
  706. hash_index |= (bitval << j);
  707. }
  708. return hash_index;
  709. }
  710. /*
  711. * Add multicast addresses to the internal multicast-hash table.
  712. */
  713. static void macb_sethashtable(struct net_device *dev)
  714. {
  715. struct netdev_hw_addr *ha;
  716. unsigned long mc_filter[2];
  717. unsigned int bitnr;
  718. struct macb *bp = netdev_priv(dev);
  719. mc_filter[0] = mc_filter[1] = 0;
  720. netdev_for_each_mc_addr(ha, dev) {
  721. bitnr = hash_get_index(ha->addr);
  722. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  723. }
  724. macb_writel(bp, HRB, mc_filter[0]);
  725. macb_writel(bp, HRT, mc_filter[1]);
  726. }
  727. /*
  728. * Enable/Disable promiscuous and multicast modes.
  729. */
  730. static void macb_set_rx_mode(struct net_device *dev)
  731. {
  732. unsigned long cfg;
  733. struct macb *bp = netdev_priv(dev);
  734. cfg = macb_readl(bp, NCFGR);
  735. if (dev->flags & IFF_PROMISC)
  736. /* Enable promiscuous mode */
  737. cfg |= MACB_BIT(CAF);
  738. else if (dev->flags & (~IFF_PROMISC))
  739. /* Disable promiscuous mode */
  740. cfg &= ~MACB_BIT(CAF);
  741. if (dev->flags & IFF_ALLMULTI) {
  742. /* Enable all multicast mode */
  743. macb_writel(bp, HRB, -1);
  744. macb_writel(bp, HRT, -1);
  745. cfg |= MACB_BIT(NCFGR_MTI);
  746. } else if (!netdev_mc_empty(dev)) {
  747. /* Enable specific multicasts */
  748. macb_sethashtable(dev);
  749. cfg |= MACB_BIT(NCFGR_MTI);
  750. } else if (dev->flags & (~IFF_ALLMULTI)) {
  751. /* Disable all multicast mode */
  752. macb_writel(bp, HRB, 0);
  753. macb_writel(bp, HRT, 0);
  754. cfg &= ~MACB_BIT(NCFGR_MTI);
  755. }
  756. macb_writel(bp, NCFGR, cfg);
  757. }
  758. static int macb_open(struct net_device *dev)
  759. {
  760. struct macb *bp = netdev_priv(dev);
  761. int err;
  762. dev_dbg(&bp->pdev->dev, "open\n");
  763. /* if the phy is not yet register, retry later*/
  764. if (!bp->phy_dev)
  765. return -EAGAIN;
  766. if (!is_valid_ether_addr(dev->dev_addr))
  767. return -EADDRNOTAVAIL;
  768. err = macb_alloc_consistent(bp);
  769. if (err) {
  770. printk(KERN_ERR
  771. "%s: Unable to allocate DMA memory (error %d)\n",
  772. dev->name, err);
  773. return err;
  774. }
  775. napi_enable(&bp->napi);
  776. macb_init_rings(bp);
  777. macb_init_hw(bp);
  778. /* schedule a link state check */
  779. phy_start(bp->phy_dev);
  780. netif_start_queue(dev);
  781. return 0;
  782. }
  783. static int macb_close(struct net_device *dev)
  784. {
  785. struct macb *bp = netdev_priv(dev);
  786. unsigned long flags;
  787. netif_stop_queue(dev);
  788. napi_disable(&bp->napi);
  789. if (bp->phy_dev)
  790. phy_stop(bp->phy_dev);
  791. spin_lock_irqsave(&bp->lock, flags);
  792. macb_reset_hw(bp);
  793. netif_carrier_off(dev);
  794. spin_unlock_irqrestore(&bp->lock, flags);
  795. macb_free_consistent(bp);
  796. return 0;
  797. }
  798. static struct net_device_stats *macb_get_stats(struct net_device *dev)
  799. {
  800. struct macb *bp = netdev_priv(dev);
  801. struct net_device_stats *nstat = &bp->stats;
  802. struct macb_stats *hwstat = &bp->hw_stats;
  803. /* read stats from hardware */
  804. macb_update_stats(bp);
  805. /* Convert HW stats into netdevice stats */
  806. nstat->rx_errors = (hwstat->rx_fcs_errors +
  807. hwstat->rx_align_errors +
  808. hwstat->rx_resource_errors +
  809. hwstat->rx_overruns +
  810. hwstat->rx_oversize_pkts +
  811. hwstat->rx_jabbers +
  812. hwstat->rx_undersize_pkts +
  813. hwstat->sqe_test_errors +
  814. hwstat->rx_length_mismatch);
  815. nstat->tx_errors = (hwstat->tx_late_cols +
  816. hwstat->tx_excessive_cols +
  817. hwstat->tx_underruns +
  818. hwstat->tx_carrier_errors);
  819. nstat->collisions = (hwstat->tx_single_cols +
  820. hwstat->tx_multiple_cols +
  821. hwstat->tx_excessive_cols);
  822. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  823. hwstat->rx_jabbers +
  824. hwstat->rx_undersize_pkts +
  825. hwstat->rx_length_mismatch);
  826. nstat->rx_over_errors = hwstat->rx_resource_errors;
  827. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  828. nstat->rx_frame_errors = hwstat->rx_align_errors;
  829. nstat->rx_fifo_errors = hwstat->rx_overruns;
  830. /* XXX: What does "missed" mean? */
  831. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  832. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  833. nstat->tx_fifo_errors = hwstat->tx_underruns;
  834. /* Don't know about heartbeat or window errors... */
  835. return nstat;
  836. }
  837. static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  838. {
  839. struct macb *bp = netdev_priv(dev);
  840. struct phy_device *phydev = bp->phy_dev;
  841. if (!phydev)
  842. return -ENODEV;
  843. return phy_ethtool_gset(phydev, cmd);
  844. }
  845. static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  846. {
  847. struct macb *bp = netdev_priv(dev);
  848. struct phy_device *phydev = bp->phy_dev;
  849. if (!phydev)
  850. return -ENODEV;
  851. return phy_ethtool_sset(phydev, cmd);
  852. }
  853. static void macb_get_drvinfo(struct net_device *dev,
  854. struct ethtool_drvinfo *info)
  855. {
  856. struct macb *bp = netdev_priv(dev);
  857. strcpy(info->driver, bp->pdev->dev.driver->name);
  858. strcpy(info->version, "$Revision: 1.14 $");
  859. strcpy(info->bus_info, dev_name(&bp->pdev->dev));
  860. }
  861. static const struct ethtool_ops macb_ethtool_ops = {
  862. .get_settings = macb_get_settings,
  863. .set_settings = macb_set_settings,
  864. .get_drvinfo = macb_get_drvinfo,
  865. .get_link = ethtool_op_get_link,
  866. };
  867. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  868. {
  869. struct macb *bp = netdev_priv(dev);
  870. struct phy_device *phydev = bp->phy_dev;
  871. if (!netif_running(dev))
  872. return -EINVAL;
  873. if (!phydev)
  874. return -ENODEV;
  875. return phy_mii_ioctl(phydev, rq, cmd);
  876. }
  877. static const struct net_device_ops macb_netdev_ops = {
  878. .ndo_open = macb_open,
  879. .ndo_stop = macb_close,
  880. .ndo_start_xmit = macb_start_xmit,
  881. .ndo_set_multicast_list = macb_set_rx_mode,
  882. .ndo_get_stats = macb_get_stats,
  883. .ndo_do_ioctl = macb_ioctl,
  884. .ndo_validate_addr = eth_validate_addr,
  885. .ndo_change_mtu = eth_change_mtu,
  886. .ndo_set_mac_address = eth_mac_addr,
  887. #ifdef CONFIG_NET_POLL_CONTROLLER
  888. .ndo_poll_controller = macb_poll_controller,
  889. #endif
  890. };
  891. static int __init macb_probe(struct platform_device *pdev)
  892. {
  893. struct eth_platform_data *pdata;
  894. struct resource *regs;
  895. struct net_device *dev;
  896. struct macb *bp;
  897. struct phy_device *phydev;
  898. unsigned long pclk_hz;
  899. u32 config;
  900. int err = -ENXIO;
  901. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  902. if (!regs) {
  903. dev_err(&pdev->dev, "no mmio resource defined\n");
  904. goto err_out;
  905. }
  906. err = -ENOMEM;
  907. dev = alloc_etherdev(sizeof(*bp));
  908. if (!dev) {
  909. dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
  910. goto err_out;
  911. }
  912. SET_NETDEV_DEV(dev, &pdev->dev);
  913. /* TODO: Actually, we have some interesting features... */
  914. dev->features |= 0;
  915. bp = netdev_priv(dev);
  916. bp->pdev = pdev;
  917. bp->dev = dev;
  918. spin_lock_init(&bp->lock);
  919. #if defined(CONFIG_ARCH_AT91)
  920. bp->pclk = clk_get(&pdev->dev, "macb_clk");
  921. if (IS_ERR(bp->pclk)) {
  922. dev_err(&pdev->dev, "failed to get macb_clk\n");
  923. goto err_out_free_dev;
  924. }
  925. clk_enable(bp->pclk);
  926. #else
  927. bp->pclk = clk_get(&pdev->dev, "pclk");
  928. if (IS_ERR(bp->pclk)) {
  929. dev_err(&pdev->dev, "failed to get pclk\n");
  930. goto err_out_free_dev;
  931. }
  932. bp->hclk = clk_get(&pdev->dev, "hclk");
  933. if (IS_ERR(bp->hclk)) {
  934. dev_err(&pdev->dev, "failed to get hclk\n");
  935. goto err_out_put_pclk;
  936. }
  937. clk_enable(bp->pclk);
  938. clk_enable(bp->hclk);
  939. #endif
  940. bp->regs = ioremap(regs->start, regs->end - regs->start + 1);
  941. if (!bp->regs) {
  942. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  943. err = -ENOMEM;
  944. goto err_out_disable_clocks;
  945. }
  946. dev->irq = platform_get_irq(pdev, 0);
  947. err = request_irq(dev->irq, macb_interrupt, IRQF_SAMPLE_RANDOM,
  948. dev->name, dev);
  949. if (err) {
  950. printk(KERN_ERR
  951. "%s: Unable to request IRQ %d (error %d)\n",
  952. dev->name, dev->irq, err);
  953. goto err_out_iounmap;
  954. }
  955. dev->netdev_ops = &macb_netdev_ops;
  956. netif_napi_add(dev, &bp->napi, macb_poll, 64);
  957. dev->ethtool_ops = &macb_ethtool_ops;
  958. dev->base_addr = regs->start;
  959. /* Set MII management clock divider */
  960. pclk_hz = clk_get_rate(bp->pclk);
  961. if (pclk_hz <= 20000000)
  962. config = MACB_BF(CLK, MACB_CLK_DIV8);
  963. else if (pclk_hz <= 40000000)
  964. config = MACB_BF(CLK, MACB_CLK_DIV16);
  965. else if (pclk_hz <= 80000000)
  966. config = MACB_BF(CLK, MACB_CLK_DIV32);
  967. else
  968. config = MACB_BF(CLK, MACB_CLK_DIV64);
  969. macb_writel(bp, NCFGR, config);
  970. macb_get_hwaddr(bp);
  971. pdata = pdev->dev.platform_data;
  972. if (pdata && pdata->is_rmii)
  973. #if defined(CONFIG_ARCH_AT91)
  974. macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) );
  975. #else
  976. macb_writel(bp, USRIO, 0);
  977. #endif
  978. else
  979. #if defined(CONFIG_ARCH_AT91)
  980. macb_writel(bp, USRIO, MACB_BIT(CLKEN));
  981. #else
  982. macb_writel(bp, USRIO, MACB_BIT(MII));
  983. #endif
  984. bp->tx_pending = DEF_TX_RING_PENDING;
  985. err = register_netdev(dev);
  986. if (err) {
  987. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  988. goto err_out_free_irq;
  989. }
  990. if (macb_mii_init(bp) != 0) {
  991. goto err_out_unregister_netdev;
  992. }
  993. platform_set_drvdata(pdev, dev);
  994. printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d (%pM)\n",
  995. dev->name, dev->base_addr, dev->irq, dev->dev_addr);
  996. phydev = bp->phy_dev;
  997. printk(KERN_INFO "%s: attached PHY driver [%s] "
  998. "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
  999. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  1000. return 0;
  1001. err_out_unregister_netdev:
  1002. unregister_netdev(dev);
  1003. err_out_free_irq:
  1004. free_irq(dev->irq, dev);
  1005. err_out_iounmap:
  1006. iounmap(bp->regs);
  1007. err_out_disable_clocks:
  1008. #ifndef CONFIG_ARCH_AT91
  1009. clk_disable(bp->hclk);
  1010. clk_put(bp->hclk);
  1011. #endif
  1012. clk_disable(bp->pclk);
  1013. #ifndef CONFIG_ARCH_AT91
  1014. err_out_put_pclk:
  1015. #endif
  1016. clk_put(bp->pclk);
  1017. err_out_free_dev:
  1018. free_netdev(dev);
  1019. err_out:
  1020. platform_set_drvdata(pdev, NULL);
  1021. return err;
  1022. }
  1023. static int __exit macb_remove(struct platform_device *pdev)
  1024. {
  1025. struct net_device *dev;
  1026. struct macb *bp;
  1027. dev = platform_get_drvdata(pdev);
  1028. if (dev) {
  1029. bp = netdev_priv(dev);
  1030. if (bp->phy_dev)
  1031. phy_disconnect(bp->phy_dev);
  1032. mdiobus_unregister(bp->mii_bus);
  1033. kfree(bp->mii_bus->irq);
  1034. mdiobus_free(bp->mii_bus);
  1035. unregister_netdev(dev);
  1036. free_irq(dev->irq, dev);
  1037. iounmap(bp->regs);
  1038. #ifndef CONFIG_ARCH_AT91
  1039. clk_disable(bp->hclk);
  1040. clk_put(bp->hclk);
  1041. #endif
  1042. clk_disable(bp->pclk);
  1043. clk_put(bp->pclk);
  1044. free_netdev(dev);
  1045. platform_set_drvdata(pdev, NULL);
  1046. }
  1047. return 0;
  1048. }
  1049. #ifdef CONFIG_PM
  1050. static int macb_suspend(struct platform_device *pdev, pm_message_t state)
  1051. {
  1052. struct net_device *netdev = platform_get_drvdata(pdev);
  1053. struct macb *bp = netdev_priv(netdev);
  1054. netif_device_detach(netdev);
  1055. #ifndef CONFIG_ARCH_AT91
  1056. clk_disable(bp->hclk);
  1057. #endif
  1058. clk_disable(bp->pclk);
  1059. return 0;
  1060. }
  1061. static int macb_resume(struct platform_device *pdev)
  1062. {
  1063. struct net_device *netdev = platform_get_drvdata(pdev);
  1064. struct macb *bp = netdev_priv(netdev);
  1065. clk_enable(bp->pclk);
  1066. #ifndef CONFIG_ARCH_AT91
  1067. clk_enable(bp->hclk);
  1068. #endif
  1069. netif_device_attach(netdev);
  1070. return 0;
  1071. }
  1072. #else
  1073. #define macb_suspend NULL
  1074. #define macb_resume NULL
  1075. #endif
  1076. static struct platform_driver macb_driver = {
  1077. .remove = __exit_p(macb_remove),
  1078. .suspend = macb_suspend,
  1079. .resume = macb_resume,
  1080. .driver = {
  1081. .name = "macb",
  1082. .owner = THIS_MODULE,
  1083. },
  1084. };
  1085. static int __init macb_init(void)
  1086. {
  1087. return platform_driver_probe(&macb_driver, macb_probe);
  1088. }
  1089. static void __exit macb_exit(void)
  1090. {
  1091. platform_driver_unregister(&macb_driver);
  1092. }
  1093. module_init(macb_init);
  1094. module_exit(macb_exit);
  1095. MODULE_LICENSE("GPL");
  1096. MODULE_DESCRIPTION("Atmel MACB Ethernet driver");
  1097. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  1098. MODULE_ALIAS("platform:macb");