macb.c 42 KB

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