macb.c 41 KB

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