macb.c 38 KB

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