macb.c 32 KB

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