macb.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346
  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/circ_buf.h>
  17. #include <linux/slab.h>
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/gpio.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/platform_data/macb.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/phy.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/of_mdio.h>
  31. #include <linux/of_net.h>
  32. #include <linux/pinctrl/consumer.h>
  33. #include "macb.h"
  34. #define MACB_RX_BUFFER_SIZE 128
  35. #define RX_BUFFER_MULTIPLE 64 /* bytes */
  36. #define RX_RING_SIZE 512 /* must be power of 2 */
  37. #define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
  38. #define TX_RING_SIZE 128 /* must be power of 2 */
  39. #define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
  40. /* level of occupied TX descriptors under which we wake up TX process */
  41. #define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
  42. #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
  43. | MACB_BIT(ISR_ROVR))
  44. #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
  45. | MACB_BIT(ISR_RLE) \
  46. | MACB_BIT(TXERR))
  47. #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
  48. #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
  49. #define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
  50. /*
  51. * Graceful stop timeouts in us. We should allow up to
  52. * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
  53. */
  54. #define MACB_HALT_TIMEOUT 1230
  55. /* Ring buffer accessors */
  56. static unsigned int macb_tx_ring_wrap(unsigned int index)
  57. {
  58. return index & (TX_RING_SIZE - 1);
  59. }
  60. static struct macb_dma_desc *macb_tx_desc(struct macb *bp, unsigned int index)
  61. {
  62. return &bp->tx_ring[macb_tx_ring_wrap(index)];
  63. }
  64. static struct macb_tx_skb *macb_tx_skb(struct macb *bp, unsigned int index)
  65. {
  66. return &bp->tx_skb[macb_tx_ring_wrap(index)];
  67. }
  68. static dma_addr_t macb_tx_dma(struct macb *bp, unsigned int index)
  69. {
  70. dma_addr_t offset;
  71. offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
  72. return bp->tx_ring_dma + offset;
  73. }
  74. static unsigned int macb_rx_ring_wrap(unsigned int index)
  75. {
  76. return index & (RX_RING_SIZE - 1);
  77. }
  78. static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
  79. {
  80. return &bp->rx_ring[macb_rx_ring_wrap(index)];
  81. }
  82. static void *macb_rx_buffer(struct macb *bp, unsigned int index)
  83. {
  84. return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
  85. }
  86. void macb_set_hwaddr(struct macb *bp)
  87. {
  88. u32 bottom;
  89. u16 top;
  90. bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
  91. macb_or_gem_writel(bp, SA1B, bottom);
  92. top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
  93. macb_or_gem_writel(bp, SA1T, top);
  94. /* Clear unused address register sets */
  95. macb_or_gem_writel(bp, SA2B, 0);
  96. macb_or_gem_writel(bp, SA2T, 0);
  97. macb_or_gem_writel(bp, SA3B, 0);
  98. macb_or_gem_writel(bp, SA3T, 0);
  99. macb_or_gem_writel(bp, SA4B, 0);
  100. macb_or_gem_writel(bp, SA4T, 0);
  101. }
  102. EXPORT_SYMBOL_GPL(macb_set_hwaddr);
  103. void macb_get_hwaddr(struct macb *bp)
  104. {
  105. struct macb_platform_data *pdata;
  106. u32 bottom;
  107. u16 top;
  108. u8 addr[6];
  109. int i;
  110. pdata = dev_get_platdata(&bp->pdev->dev);
  111. /* Check all 4 address register for vaild address */
  112. for (i = 0; i < 4; i++) {
  113. bottom = macb_or_gem_readl(bp, SA1B + i * 8);
  114. top = macb_or_gem_readl(bp, SA1T + i * 8);
  115. if (pdata && pdata->rev_eth_addr) {
  116. addr[5] = bottom & 0xff;
  117. addr[4] = (bottom >> 8) & 0xff;
  118. addr[3] = (bottom >> 16) & 0xff;
  119. addr[2] = (bottom >> 24) & 0xff;
  120. addr[1] = top & 0xff;
  121. addr[0] = (top & 0xff00) >> 8;
  122. } else {
  123. addr[0] = bottom & 0xff;
  124. addr[1] = (bottom >> 8) & 0xff;
  125. addr[2] = (bottom >> 16) & 0xff;
  126. addr[3] = (bottom >> 24) & 0xff;
  127. addr[4] = top & 0xff;
  128. addr[5] = (top >> 8) & 0xff;
  129. }
  130. if (is_valid_ether_addr(addr)) {
  131. memcpy(bp->dev->dev_addr, addr, sizeof(addr));
  132. return;
  133. }
  134. }
  135. netdev_info(bp->dev, "invalid hw address, using random\n");
  136. eth_hw_addr_random(bp->dev);
  137. }
  138. EXPORT_SYMBOL_GPL(macb_get_hwaddr);
  139. static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  140. {
  141. struct macb *bp = bus->priv;
  142. int value;
  143. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  144. | MACB_BF(RW, MACB_MAN_READ)
  145. | MACB_BF(PHYA, mii_id)
  146. | MACB_BF(REGA, regnum)
  147. | MACB_BF(CODE, MACB_MAN_CODE)));
  148. /* wait for end of transfer */
  149. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  150. cpu_relax();
  151. value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  152. return value;
  153. }
  154. static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  155. u16 value)
  156. {
  157. struct macb *bp = bus->priv;
  158. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  159. | MACB_BF(RW, MACB_MAN_WRITE)
  160. | MACB_BF(PHYA, mii_id)
  161. | MACB_BF(REGA, regnum)
  162. | MACB_BF(CODE, MACB_MAN_CODE)
  163. | MACB_BF(DATA, value)));
  164. /* wait for end of transfer */
  165. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  166. cpu_relax();
  167. return 0;
  168. }
  169. /**
  170. * macb_set_tx_clk() - Set a clock to a new frequency
  171. * @clk Pointer to the clock to change
  172. * @rate New frequency in Hz
  173. * @dev Pointer to the struct net_device
  174. */
  175. static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
  176. {
  177. long ferr, rate, rate_rounded;
  178. switch (speed) {
  179. case SPEED_10:
  180. rate = 2500000;
  181. break;
  182. case SPEED_100:
  183. rate = 25000000;
  184. break;
  185. case SPEED_1000:
  186. rate = 125000000;
  187. break;
  188. default:
  189. return;
  190. }
  191. rate_rounded = clk_round_rate(clk, rate);
  192. if (rate_rounded < 0)
  193. return;
  194. /* RGMII allows 50 ppm frequency error. Test and warn if this limit
  195. * is not satisfied.
  196. */
  197. ferr = abs(rate_rounded - rate);
  198. ferr = DIV_ROUND_UP(ferr, rate / 100000);
  199. if (ferr > 5)
  200. netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
  201. rate);
  202. if (clk_set_rate(clk, rate_rounded))
  203. netdev_err(dev, "adjusting tx_clk failed.\n");
  204. }
  205. static void macb_handle_link_change(struct net_device *dev)
  206. {
  207. struct macb *bp = netdev_priv(dev);
  208. struct phy_device *phydev = bp->phy_dev;
  209. unsigned long flags;
  210. int status_change = 0;
  211. spin_lock_irqsave(&bp->lock, flags);
  212. if (phydev->link) {
  213. if ((bp->speed != phydev->speed) ||
  214. (bp->duplex != phydev->duplex)) {
  215. u32 reg;
  216. reg = macb_readl(bp, NCFGR);
  217. reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  218. if (macb_is_gem(bp))
  219. reg &= ~GEM_BIT(GBE);
  220. if (phydev->duplex)
  221. reg |= MACB_BIT(FD);
  222. if (phydev->speed == SPEED_100)
  223. reg |= MACB_BIT(SPD);
  224. if (phydev->speed == SPEED_1000 &&
  225. bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
  226. reg |= GEM_BIT(GBE);
  227. macb_or_gem_writel(bp, NCFGR, reg);
  228. bp->speed = phydev->speed;
  229. bp->duplex = phydev->duplex;
  230. status_change = 1;
  231. }
  232. }
  233. if (phydev->link != bp->link) {
  234. if (!phydev->link) {
  235. bp->speed = 0;
  236. bp->duplex = -1;
  237. }
  238. bp->link = phydev->link;
  239. status_change = 1;
  240. }
  241. spin_unlock_irqrestore(&bp->lock, flags);
  242. if (!IS_ERR(bp->tx_clk))
  243. macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
  244. if (status_change) {
  245. if (phydev->link) {
  246. netif_carrier_on(dev);
  247. netdev_info(dev, "link up (%d/%s)\n",
  248. phydev->speed,
  249. phydev->duplex == DUPLEX_FULL ?
  250. "Full" : "Half");
  251. } else {
  252. netif_carrier_off(dev);
  253. netdev_info(dev, "link down\n");
  254. }
  255. }
  256. }
  257. /* based on au1000_eth. c*/
  258. static int macb_mii_probe(struct net_device *dev)
  259. {
  260. struct macb *bp = netdev_priv(dev);
  261. struct macb_platform_data *pdata;
  262. struct phy_device *phydev;
  263. int phy_irq;
  264. int ret;
  265. phydev = phy_find_first(bp->mii_bus);
  266. if (!phydev) {
  267. netdev_err(dev, "no PHY found\n");
  268. return -ENXIO;
  269. }
  270. pdata = dev_get_platdata(&bp->pdev->dev);
  271. if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
  272. ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
  273. if (!ret) {
  274. phy_irq = gpio_to_irq(pdata->phy_irq_pin);
  275. phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
  276. }
  277. }
  278. /* attach the mac to the phy */
  279. ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
  280. bp->phy_interface);
  281. if (ret) {
  282. netdev_err(dev, "Could not attach to PHY\n");
  283. return ret;
  284. }
  285. /* mask with MAC supported features */
  286. if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
  287. phydev->supported &= PHY_GBIT_FEATURES;
  288. else
  289. phydev->supported &= PHY_BASIC_FEATURES;
  290. phydev->advertising = phydev->supported;
  291. bp->link = 0;
  292. bp->speed = 0;
  293. bp->duplex = -1;
  294. bp->phy_dev = phydev;
  295. return 0;
  296. }
  297. int macb_mii_init(struct macb *bp)
  298. {
  299. struct macb_platform_data *pdata;
  300. struct device_node *np;
  301. int err = -ENXIO, i;
  302. /* Enable management port */
  303. macb_writel(bp, NCR, MACB_BIT(MPE));
  304. bp->mii_bus = mdiobus_alloc();
  305. if (bp->mii_bus == NULL) {
  306. err = -ENOMEM;
  307. goto err_out;
  308. }
  309. bp->mii_bus->name = "MACB_mii_bus";
  310. bp->mii_bus->read = &macb_mdio_read;
  311. bp->mii_bus->write = &macb_mdio_write;
  312. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  313. bp->pdev->name, bp->pdev->id);
  314. bp->mii_bus->priv = bp;
  315. bp->mii_bus->parent = &bp->dev->dev;
  316. pdata = dev_get_platdata(&bp->pdev->dev);
  317. bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  318. if (!bp->mii_bus->irq) {
  319. err = -ENOMEM;
  320. goto err_out_free_mdiobus;
  321. }
  322. dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
  323. np = bp->pdev->dev.of_node;
  324. if (np) {
  325. /* try dt phy registration */
  326. err = of_mdiobus_register(bp->mii_bus, np);
  327. /* fallback to standard phy registration if no phy were
  328. found during dt phy registration */
  329. if (!err && !phy_find_first(bp->mii_bus)) {
  330. for (i = 0; i < PHY_MAX_ADDR; i++) {
  331. struct phy_device *phydev;
  332. phydev = mdiobus_scan(bp->mii_bus, i);
  333. if (IS_ERR(phydev)) {
  334. err = PTR_ERR(phydev);
  335. break;
  336. }
  337. }
  338. if (err)
  339. goto err_out_unregister_bus;
  340. }
  341. } else {
  342. for (i = 0; i < PHY_MAX_ADDR; i++)
  343. bp->mii_bus->irq[i] = PHY_POLL;
  344. if (pdata)
  345. bp->mii_bus->phy_mask = pdata->phy_mask;
  346. err = mdiobus_register(bp->mii_bus);
  347. }
  348. if (err)
  349. goto err_out_free_mdio_irq;
  350. err = macb_mii_probe(bp->dev);
  351. if (err)
  352. goto err_out_unregister_bus;
  353. return 0;
  354. err_out_unregister_bus:
  355. mdiobus_unregister(bp->mii_bus);
  356. err_out_free_mdio_irq:
  357. kfree(bp->mii_bus->irq);
  358. err_out_free_mdiobus:
  359. mdiobus_free(bp->mii_bus);
  360. err_out:
  361. return err;
  362. }
  363. EXPORT_SYMBOL_GPL(macb_mii_init);
  364. static void macb_update_stats(struct macb *bp)
  365. {
  366. u32 __iomem *reg = bp->regs + MACB_PFR;
  367. u32 *p = &bp->hw_stats.macb.rx_pause_frames;
  368. u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
  369. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  370. for(; p < end; p++, reg++)
  371. *p += __raw_readl(reg);
  372. }
  373. static int macb_halt_tx(struct macb *bp)
  374. {
  375. unsigned long halt_time, timeout;
  376. u32 status;
  377. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
  378. timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
  379. do {
  380. halt_time = jiffies;
  381. status = macb_readl(bp, TSR);
  382. if (!(status & MACB_BIT(TGO)))
  383. return 0;
  384. usleep_range(10, 250);
  385. } while (time_before(halt_time, timeout));
  386. return -ETIMEDOUT;
  387. }
  388. static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
  389. {
  390. if (tx_skb->mapping) {
  391. if (tx_skb->mapped_as_page)
  392. dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
  393. tx_skb->size, DMA_TO_DEVICE);
  394. else
  395. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
  396. tx_skb->size, DMA_TO_DEVICE);
  397. tx_skb->mapping = 0;
  398. }
  399. if (tx_skb->skb) {
  400. dev_kfree_skb_any(tx_skb->skb);
  401. tx_skb->skb = NULL;
  402. }
  403. }
  404. static void macb_tx_error_task(struct work_struct *work)
  405. {
  406. struct macb *bp = container_of(work, struct macb, tx_error_task);
  407. struct macb_tx_skb *tx_skb;
  408. struct sk_buff *skb;
  409. unsigned int tail;
  410. netdev_vdbg(bp->dev, "macb_tx_error_task: t = %u, h = %u\n",
  411. bp->tx_tail, bp->tx_head);
  412. /* Make sure nobody is trying to queue up new packets */
  413. netif_stop_queue(bp->dev);
  414. /*
  415. * Stop transmission now
  416. * (in case we have just queued new packets)
  417. */
  418. if (macb_halt_tx(bp))
  419. /* Just complain for now, reinitializing TX path can be good */
  420. netdev_err(bp->dev, "BUG: halt tx timed out\n");
  421. /* No need for the lock here as nobody will interrupt us anymore */
  422. /*
  423. * Treat frames in TX queue including the ones that caused the error.
  424. * Free transmit buffers in upper layer.
  425. */
  426. for (tail = bp->tx_tail; tail != bp->tx_head; tail++) {
  427. struct macb_dma_desc *desc;
  428. u32 ctrl;
  429. desc = macb_tx_desc(bp, tail);
  430. ctrl = desc->ctrl;
  431. tx_skb = macb_tx_skb(bp, tail);
  432. skb = tx_skb->skb;
  433. if (ctrl & MACB_BIT(TX_USED)) {
  434. /* skb is set for the last buffer of the frame */
  435. while (!skb) {
  436. macb_tx_unmap(bp, tx_skb);
  437. tail++;
  438. tx_skb = macb_tx_skb(bp, tail);
  439. skb = tx_skb->skb;
  440. }
  441. /* ctrl still refers to the first buffer descriptor
  442. * since it's the only one written back by the hardware
  443. */
  444. if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
  445. netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
  446. macb_tx_ring_wrap(tail), skb->data);
  447. bp->stats.tx_packets++;
  448. bp->stats.tx_bytes += skb->len;
  449. }
  450. } else {
  451. /*
  452. * "Buffers exhausted mid-frame" errors may only happen
  453. * if the driver is buggy, so complain loudly about those.
  454. * Statistics are updated by hardware.
  455. */
  456. if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
  457. netdev_err(bp->dev,
  458. "BUG: TX buffers exhausted mid-frame\n");
  459. desc->ctrl = ctrl | MACB_BIT(TX_USED);
  460. }
  461. macb_tx_unmap(bp, tx_skb);
  462. }
  463. /* Make descriptor updates visible to hardware */
  464. wmb();
  465. /* Reinitialize the TX desc queue */
  466. macb_writel(bp, TBQP, bp->tx_ring_dma);
  467. /* Make TX ring reflect state of hardware */
  468. bp->tx_head = bp->tx_tail = 0;
  469. /* Now we are ready to start transmission again */
  470. netif_wake_queue(bp->dev);
  471. /* Housework before enabling TX IRQ */
  472. macb_writel(bp, TSR, macb_readl(bp, TSR));
  473. macb_writel(bp, IER, MACB_TX_INT_FLAGS);
  474. }
  475. static void macb_tx_interrupt(struct macb *bp)
  476. {
  477. unsigned int tail;
  478. unsigned int head;
  479. u32 status;
  480. status = macb_readl(bp, TSR);
  481. macb_writel(bp, TSR, status);
  482. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  483. macb_writel(bp, ISR, MACB_BIT(TCOMP));
  484. netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
  485. (unsigned long)status);
  486. head = bp->tx_head;
  487. for (tail = bp->tx_tail; tail != head; tail++) {
  488. struct macb_tx_skb *tx_skb;
  489. struct sk_buff *skb;
  490. struct macb_dma_desc *desc;
  491. u32 ctrl;
  492. desc = macb_tx_desc(bp, tail);
  493. /* Make hw descriptor updates visible to CPU */
  494. rmb();
  495. ctrl = desc->ctrl;
  496. /* TX_USED bit is only set by hardware on the very first buffer
  497. * descriptor of the transmitted frame.
  498. */
  499. if (!(ctrl & MACB_BIT(TX_USED)))
  500. break;
  501. /* Process all buffers of the current transmitted frame */
  502. for (;; tail++) {
  503. tx_skb = macb_tx_skb(bp, tail);
  504. skb = tx_skb->skb;
  505. /* First, update TX stats if needed */
  506. if (skb) {
  507. netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
  508. macb_tx_ring_wrap(tail), skb->data);
  509. bp->stats.tx_packets++;
  510. bp->stats.tx_bytes += skb->len;
  511. }
  512. /* Now we can safely release resources */
  513. macb_tx_unmap(bp, tx_skb);
  514. /* skb is set only for the last buffer of the frame.
  515. * WARNING: at this point skb has been freed by
  516. * macb_tx_unmap().
  517. */
  518. if (skb)
  519. break;
  520. }
  521. }
  522. bp->tx_tail = tail;
  523. if (netif_queue_stopped(bp->dev)
  524. && CIRC_CNT(bp->tx_head, bp->tx_tail,
  525. TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
  526. netif_wake_queue(bp->dev);
  527. }
  528. static void gem_rx_refill(struct macb *bp)
  529. {
  530. unsigned int entry;
  531. struct sk_buff *skb;
  532. dma_addr_t paddr;
  533. while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
  534. entry = macb_rx_ring_wrap(bp->rx_prepared_head);
  535. /* Make hw descriptor updates visible to CPU */
  536. rmb();
  537. bp->rx_prepared_head++;
  538. if (bp->rx_skbuff[entry] == NULL) {
  539. /* allocate sk_buff for this free entry in ring */
  540. skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
  541. if (unlikely(skb == NULL)) {
  542. netdev_err(bp->dev,
  543. "Unable to allocate sk_buff\n");
  544. break;
  545. }
  546. /* now fill corresponding descriptor entry */
  547. paddr = dma_map_single(&bp->pdev->dev, skb->data,
  548. bp->rx_buffer_size, DMA_FROM_DEVICE);
  549. if (dma_mapping_error(&bp->pdev->dev, paddr)) {
  550. dev_kfree_skb(skb);
  551. break;
  552. }
  553. bp->rx_skbuff[entry] = skb;
  554. if (entry == RX_RING_SIZE - 1)
  555. paddr |= MACB_BIT(RX_WRAP);
  556. bp->rx_ring[entry].addr = paddr;
  557. bp->rx_ring[entry].ctrl = 0;
  558. /* properly align Ethernet header */
  559. skb_reserve(skb, NET_IP_ALIGN);
  560. }
  561. }
  562. /* Make descriptor updates visible to hardware */
  563. wmb();
  564. netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
  565. bp->rx_prepared_head, bp->rx_tail);
  566. }
  567. /* Mark DMA descriptors from begin up to and not including end as unused */
  568. static void discard_partial_frame(struct macb *bp, unsigned int begin,
  569. unsigned int end)
  570. {
  571. unsigned int frag;
  572. for (frag = begin; frag != end; frag++) {
  573. struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
  574. desc->addr &= ~MACB_BIT(RX_USED);
  575. }
  576. /* Make descriptor updates visible to hardware */
  577. wmb();
  578. /*
  579. * When this happens, the hardware stats registers for
  580. * whatever caused this is updated, so we don't have to record
  581. * anything.
  582. */
  583. }
  584. static int gem_rx(struct macb *bp, int budget)
  585. {
  586. unsigned int len;
  587. unsigned int entry;
  588. struct sk_buff *skb;
  589. struct macb_dma_desc *desc;
  590. int count = 0;
  591. while (count < budget) {
  592. u32 addr, ctrl;
  593. entry = macb_rx_ring_wrap(bp->rx_tail);
  594. desc = &bp->rx_ring[entry];
  595. /* Make hw descriptor updates visible to CPU */
  596. rmb();
  597. addr = desc->addr;
  598. ctrl = desc->ctrl;
  599. if (!(addr & MACB_BIT(RX_USED)))
  600. break;
  601. bp->rx_tail++;
  602. count++;
  603. if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
  604. netdev_err(bp->dev,
  605. "not whole frame pointed by descriptor\n");
  606. bp->stats.rx_dropped++;
  607. break;
  608. }
  609. skb = bp->rx_skbuff[entry];
  610. if (unlikely(!skb)) {
  611. netdev_err(bp->dev,
  612. "inconsistent Rx descriptor chain\n");
  613. bp->stats.rx_dropped++;
  614. break;
  615. }
  616. /* now everything is ready for receiving packet */
  617. bp->rx_skbuff[entry] = NULL;
  618. len = MACB_BFEXT(RX_FRMLEN, ctrl);
  619. netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
  620. skb_put(skb, len);
  621. addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
  622. dma_unmap_single(&bp->pdev->dev, addr,
  623. bp->rx_buffer_size, DMA_FROM_DEVICE);
  624. skb->protocol = eth_type_trans(skb, bp->dev);
  625. skb_checksum_none_assert(skb);
  626. if (bp->dev->features & NETIF_F_RXCSUM &&
  627. !(bp->dev->flags & IFF_PROMISC) &&
  628. GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
  629. skb->ip_summed = CHECKSUM_UNNECESSARY;
  630. bp->stats.rx_packets++;
  631. bp->stats.rx_bytes += skb->len;
  632. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  633. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  634. skb->len, skb->csum);
  635. print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
  636. skb->mac_header, 16, true);
  637. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
  638. skb->data, 32, true);
  639. #endif
  640. netif_receive_skb(skb);
  641. }
  642. gem_rx_refill(bp);
  643. return count;
  644. }
  645. static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
  646. unsigned int last_frag)
  647. {
  648. unsigned int len;
  649. unsigned int frag;
  650. unsigned int offset;
  651. struct sk_buff *skb;
  652. struct macb_dma_desc *desc;
  653. desc = macb_rx_desc(bp, last_frag);
  654. len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
  655. netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  656. macb_rx_ring_wrap(first_frag),
  657. macb_rx_ring_wrap(last_frag), len);
  658. /*
  659. * The ethernet header starts NET_IP_ALIGN bytes into the
  660. * first buffer. Since the header is 14 bytes, this makes the
  661. * payload word-aligned.
  662. *
  663. * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
  664. * the two padding bytes into the skb so that we avoid hitting
  665. * the slowpath in memcpy(), and pull them off afterwards.
  666. */
  667. skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
  668. if (!skb) {
  669. bp->stats.rx_dropped++;
  670. for (frag = first_frag; ; frag++) {
  671. desc = macb_rx_desc(bp, frag);
  672. desc->addr &= ~MACB_BIT(RX_USED);
  673. if (frag == last_frag)
  674. break;
  675. }
  676. /* Make descriptor updates visible to hardware */
  677. wmb();
  678. return 1;
  679. }
  680. offset = 0;
  681. len += NET_IP_ALIGN;
  682. skb_checksum_none_assert(skb);
  683. skb_put(skb, len);
  684. for (frag = first_frag; ; frag++) {
  685. unsigned int frag_len = bp->rx_buffer_size;
  686. if (offset + frag_len > len) {
  687. BUG_ON(frag != last_frag);
  688. frag_len = len - offset;
  689. }
  690. skb_copy_to_linear_data_offset(skb, offset,
  691. macb_rx_buffer(bp, frag), frag_len);
  692. offset += bp->rx_buffer_size;
  693. desc = macb_rx_desc(bp, frag);
  694. desc->addr &= ~MACB_BIT(RX_USED);
  695. if (frag == last_frag)
  696. break;
  697. }
  698. /* Make descriptor updates visible to hardware */
  699. wmb();
  700. __skb_pull(skb, NET_IP_ALIGN);
  701. skb->protocol = eth_type_trans(skb, bp->dev);
  702. bp->stats.rx_packets++;
  703. bp->stats.rx_bytes += skb->len;
  704. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  705. skb->len, skb->csum);
  706. netif_receive_skb(skb);
  707. return 0;
  708. }
  709. static int macb_rx(struct macb *bp, int budget)
  710. {
  711. int received = 0;
  712. unsigned int tail;
  713. int first_frag = -1;
  714. for (tail = bp->rx_tail; budget > 0; tail++) {
  715. struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
  716. u32 addr, ctrl;
  717. /* Make hw descriptor updates visible to CPU */
  718. rmb();
  719. addr = desc->addr;
  720. ctrl = desc->ctrl;
  721. if (!(addr & MACB_BIT(RX_USED)))
  722. break;
  723. if (ctrl & MACB_BIT(RX_SOF)) {
  724. if (first_frag != -1)
  725. discard_partial_frame(bp, first_frag, tail);
  726. first_frag = tail;
  727. }
  728. if (ctrl & MACB_BIT(RX_EOF)) {
  729. int dropped;
  730. BUG_ON(first_frag == -1);
  731. dropped = macb_rx_frame(bp, first_frag, tail);
  732. first_frag = -1;
  733. if (!dropped) {
  734. received++;
  735. budget--;
  736. }
  737. }
  738. }
  739. if (first_frag != -1)
  740. bp->rx_tail = first_frag;
  741. else
  742. bp->rx_tail = tail;
  743. return received;
  744. }
  745. static int macb_poll(struct napi_struct *napi, int budget)
  746. {
  747. struct macb *bp = container_of(napi, struct macb, napi);
  748. int work_done;
  749. u32 status;
  750. status = macb_readl(bp, RSR);
  751. macb_writel(bp, RSR, status);
  752. work_done = 0;
  753. netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
  754. (unsigned long)status, budget);
  755. work_done = bp->macbgem_ops.mog_rx(bp, budget);
  756. if (work_done < budget) {
  757. napi_complete(napi);
  758. /* Packets received while interrupts were disabled */
  759. status = macb_readl(bp, RSR);
  760. if (status) {
  761. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  762. macb_writel(bp, ISR, MACB_BIT(RCOMP));
  763. napi_reschedule(napi);
  764. } else {
  765. macb_writel(bp, IER, MACB_RX_INT_FLAGS);
  766. }
  767. }
  768. /* TODO: Handle errors */
  769. return work_done;
  770. }
  771. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  772. {
  773. struct net_device *dev = dev_id;
  774. struct macb *bp = netdev_priv(dev);
  775. u32 status;
  776. status = macb_readl(bp, ISR);
  777. if (unlikely(!status))
  778. return IRQ_NONE;
  779. spin_lock(&bp->lock);
  780. while (status) {
  781. /* close possible race with dev_close */
  782. if (unlikely(!netif_running(dev))) {
  783. macb_writel(bp, IDR, -1);
  784. break;
  785. }
  786. netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status);
  787. if (status & MACB_RX_INT_FLAGS) {
  788. /*
  789. * There's no point taking any more interrupts
  790. * until we have processed the buffers. The
  791. * scheduling call may fail if the poll routine
  792. * is already scheduled, so disable interrupts
  793. * now.
  794. */
  795. macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
  796. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  797. macb_writel(bp, ISR, MACB_BIT(RCOMP));
  798. if (napi_schedule_prep(&bp->napi)) {
  799. netdev_vdbg(bp->dev, "scheduling RX softirq\n");
  800. __napi_schedule(&bp->napi);
  801. }
  802. }
  803. if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
  804. macb_writel(bp, IDR, MACB_TX_INT_FLAGS);
  805. schedule_work(&bp->tx_error_task);
  806. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  807. macb_writel(bp, ISR, MACB_TX_ERR_FLAGS);
  808. break;
  809. }
  810. if (status & MACB_BIT(TCOMP))
  811. macb_tx_interrupt(bp);
  812. /*
  813. * Link change detection isn't possible with RMII, so we'll
  814. * add that if/when we get our hands on a full-blown MII PHY.
  815. */
  816. if (status & MACB_BIT(ISR_ROVR)) {
  817. /* We missed at least one packet */
  818. if (macb_is_gem(bp))
  819. bp->hw_stats.gem.rx_overruns++;
  820. else
  821. bp->hw_stats.macb.rx_overruns++;
  822. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  823. macb_writel(bp, ISR, MACB_BIT(ISR_ROVR));
  824. }
  825. if (status & MACB_BIT(HRESP)) {
  826. /*
  827. * TODO: Reset the hardware, and maybe move the
  828. * netdev_err to a lower-priority context as well
  829. * (work queue?)
  830. */
  831. netdev_err(dev, "DMA bus error: HRESP not OK\n");
  832. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  833. macb_writel(bp, ISR, MACB_BIT(HRESP));
  834. }
  835. status = macb_readl(bp, ISR);
  836. }
  837. spin_unlock(&bp->lock);
  838. return IRQ_HANDLED;
  839. }
  840. #ifdef CONFIG_NET_POLL_CONTROLLER
  841. /*
  842. * Polling receive - used by netconsole and other diagnostic tools
  843. * to allow network i/o with interrupts disabled.
  844. */
  845. static void macb_poll_controller(struct net_device *dev)
  846. {
  847. unsigned long flags;
  848. local_irq_save(flags);
  849. macb_interrupt(dev->irq, dev);
  850. local_irq_restore(flags);
  851. }
  852. #endif
  853. static inline unsigned int macb_count_tx_descriptors(struct macb *bp,
  854. unsigned int len)
  855. {
  856. return (len + bp->max_tx_length - 1) / bp->max_tx_length;
  857. }
  858. static unsigned int macb_tx_map(struct macb *bp,
  859. struct sk_buff *skb)
  860. {
  861. dma_addr_t mapping;
  862. unsigned int len, entry, i, tx_head = bp->tx_head;
  863. struct macb_tx_skb *tx_skb = NULL;
  864. struct macb_dma_desc *desc;
  865. unsigned int offset, size, count = 0;
  866. unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
  867. unsigned int eof = 1;
  868. u32 ctrl;
  869. /* First, map non-paged data */
  870. len = skb_headlen(skb);
  871. offset = 0;
  872. while (len) {
  873. size = min(len, bp->max_tx_length);
  874. entry = macb_tx_ring_wrap(tx_head);
  875. tx_skb = &bp->tx_skb[entry];
  876. mapping = dma_map_single(&bp->pdev->dev,
  877. skb->data + offset,
  878. size, DMA_TO_DEVICE);
  879. if (dma_mapping_error(&bp->pdev->dev, mapping))
  880. goto dma_error;
  881. /* Save info to properly release resources */
  882. tx_skb->skb = NULL;
  883. tx_skb->mapping = mapping;
  884. tx_skb->size = size;
  885. tx_skb->mapped_as_page = false;
  886. len -= size;
  887. offset += size;
  888. count++;
  889. tx_head++;
  890. }
  891. /* Then, map paged data from fragments */
  892. for (f = 0; f < nr_frags; f++) {
  893. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  894. len = skb_frag_size(frag);
  895. offset = 0;
  896. while (len) {
  897. size = min(len, bp->max_tx_length);
  898. entry = macb_tx_ring_wrap(tx_head);
  899. tx_skb = &bp->tx_skb[entry];
  900. mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
  901. offset, size, DMA_TO_DEVICE);
  902. if (dma_mapping_error(&bp->pdev->dev, mapping))
  903. goto dma_error;
  904. /* Save info to properly release resources */
  905. tx_skb->skb = NULL;
  906. tx_skb->mapping = mapping;
  907. tx_skb->size = size;
  908. tx_skb->mapped_as_page = true;
  909. len -= size;
  910. offset += size;
  911. count++;
  912. tx_head++;
  913. }
  914. }
  915. /* Should never happen */
  916. if (unlikely(tx_skb == NULL)) {
  917. netdev_err(bp->dev, "BUG! empty skb!\n");
  918. return 0;
  919. }
  920. /* This is the last buffer of the frame: save socket buffer */
  921. tx_skb->skb = skb;
  922. /* Update TX ring: update buffer descriptors in reverse order
  923. * to avoid race condition
  924. */
  925. /* Set 'TX_USED' bit in buffer descriptor at tx_head position
  926. * to set the end of TX queue
  927. */
  928. i = tx_head;
  929. entry = macb_tx_ring_wrap(i);
  930. ctrl = MACB_BIT(TX_USED);
  931. desc = &bp->tx_ring[entry];
  932. desc->ctrl = ctrl;
  933. do {
  934. i--;
  935. entry = macb_tx_ring_wrap(i);
  936. tx_skb = &bp->tx_skb[entry];
  937. desc = &bp->tx_ring[entry];
  938. ctrl = (u32)tx_skb->size;
  939. if (eof) {
  940. ctrl |= MACB_BIT(TX_LAST);
  941. eof = 0;
  942. }
  943. if (unlikely(entry == (TX_RING_SIZE - 1)))
  944. ctrl |= MACB_BIT(TX_WRAP);
  945. /* Set TX buffer descriptor */
  946. desc->addr = tx_skb->mapping;
  947. /* desc->addr must be visible to hardware before clearing
  948. * 'TX_USED' bit in desc->ctrl.
  949. */
  950. wmb();
  951. desc->ctrl = ctrl;
  952. } while (i != bp->tx_head);
  953. bp->tx_head = tx_head;
  954. return count;
  955. dma_error:
  956. netdev_err(bp->dev, "TX DMA map failed\n");
  957. for (i = bp->tx_head; i != tx_head; i++) {
  958. tx_skb = macb_tx_skb(bp, i);
  959. macb_tx_unmap(bp, tx_skb);
  960. }
  961. return 0;
  962. }
  963. static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  964. {
  965. struct macb *bp = netdev_priv(dev);
  966. unsigned long flags;
  967. unsigned int count, nr_frags, frag_size, f;
  968. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  969. netdev_vdbg(bp->dev,
  970. "start_xmit: len %u head %p data %p tail %p end %p\n",
  971. skb->len, skb->head, skb->data,
  972. skb_tail_pointer(skb), skb_end_pointer(skb));
  973. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
  974. skb->data, 16, true);
  975. #endif
  976. /* Count how many TX buffer descriptors are needed to send this
  977. * socket buffer: skb fragments of jumbo frames may need to be
  978. * splitted into many buffer descriptors.
  979. */
  980. count = macb_count_tx_descriptors(bp, skb_headlen(skb));
  981. nr_frags = skb_shinfo(skb)->nr_frags;
  982. for (f = 0; f < nr_frags; f++) {
  983. frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
  984. count += macb_count_tx_descriptors(bp, frag_size);
  985. }
  986. spin_lock_irqsave(&bp->lock, flags);
  987. /* This is a hard error, log it. */
  988. if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < count) {
  989. netif_stop_queue(dev);
  990. spin_unlock_irqrestore(&bp->lock, flags);
  991. netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
  992. bp->tx_head, bp->tx_tail);
  993. return NETDEV_TX_BUSY;
  994. }
  995. /* Map socket buffer for DMA transfer */
  996. if (!macb_tx_map(bp, skb)) {
  997. dev_kfree_skb_any(skb);
  998. goto unlock;
  999. }
  1000. /* Make newly initialized descriptor visible to hardware */
  1001. wmb();
  1002. skb_tx_timestamp(skb);
  1003. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  1004. if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1)
  1005. netif_stop_queue(dev);
  1006. unlock:
  1007. spin_unlock_irqrestore(&bp->lock, flags);
  1008. return NETDEV_TX_OK;
  1009. }
  1010. static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
  1011. {
  1012. if (!macb_is_gem(bp)) {
  1013. bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
  1014. } else {
  1015. bp->rx_buffer_size = size;
  1016. if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
  1017. netdev_dbg(bp->dev,
  1018. "RX buffer must be multiple of %d bytes, expanding\n",
  1019. RX_BUFFER_MULTIPLE);
  1020. bp->rx_buffer_size =
  1021. roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
  1022. }
  1023. }
  1024. netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
  1025. bp->dev->mtu, bp->rx_buffer_size);
  1026. }
  1027. static void gem_free_rx_buffers(struct macb *bp)
  1028. {
  1029. struct sk_buff *skb;
  1030. struct macb_dma_desc *desc;
  1031. dma_addr_t addr;
  1032. int i;
  1033. if (!bp->rx_skbuff)
  1034. return;
  1035. for (i = 0; i < RX_RING_SIZE; i++) {
  1036. skb = bp->rx_skbuff[i];
  1037. if (skb == NULL)
  1038. continue;
  1039. desc = &bp->rx_ring[i];
  1040. addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
  1041. dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
  1042. DMA_FROM_DEVICE);
  1043. dev_kfree_skb_any(skb);
  1044. skb = NULL;
  1045. }
  1046. kfree(bp->rx_skbuff);
  1047. bp->rx_skbuff = NULL;
  1048. }
  1049. static void macb_free_rx_buffers(struct macb *bp)
  1050. {
  1051. if (bp->rx_buffers) {
  1052. dma_free_coherent(&bp->pdev->dev,
  1053. RX_RING_SIZE * bp->rx_buffer_size,
  1054. bp->rx_buffers, bp->rx_buffers_dma);
  1055. bp->rx_buffers = NULL;
  1056. }
  1057. }
  1058. static void macb_free_consistent(struct macb *bp)
  1059. {
  1060. if (bp->tx_skb) {
  1061. kfree(bp->tx_skb);
  1062. bp->tx_skb = NULL;
  1063. }
  1064. bp->macbgem_ops.mog_free_rx_buffers(bp);
  1065. if (bp->rx_ring) {
  1066. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
  1067. bp->rx_ring, bp->rx_ring_dma);
  1068. bp->rx_ring = NULL;
  1069. }
  1070. if (bp->tx_ring) {
  1071. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
  1072. bp->tx_ring, bp->tx_ring_dma);
  1073. bp->tx_ring = NULL;
  1074. }
  1075. }
  1076. static int gem_alloc_rx_buffers(struct macb *bp)
  1077. {
  1078. int size;
  1079. size = RX_RING_SIZE * sizeof(struct sk_buff *);
  1080. bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
  1081. if (!bp->rx_skbuff)
  1082. return -ENOMEM;
  1083. else
  1084. netdev_dbg(bp->dev,
  1085. "Allocated %d RX struct sk_buff entries at %p\n",
  1086. RX_RING_SIZE, bp->rx_skbuff);
  1087. return 0;
  1088. }
  1089. static int macb_alloc_rx_buffers(struct macb *bp)
  1090. {
  1091. int size;
  1092. size = RX_RING_SIZE * bp->rx_buffer_size;
  1093. bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  1094. &bp->rx_buffers_dma, GFP_KERNEL);
  1095. if (!bp->rx_buffers)
  1096. return -ENOMEM;
  1097. else
  1098. netdev_dbg(bp->dev,
  1099. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  1100. size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
  1101. return 0;
  1102. }
  1103. static int macb_alloc_consistent(struct macb *bp)
  1104. {
  1105. int size;
  1106. size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
  1107. bp->tx_skb = kmalloc(size, GFP_KERNEL);
  1108. if (!bp->tx_skb)
  1109. goto out_err;
  1110. size = RX_RING_BYTES;
  1111. bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1112. &bp->rx_ring_dma, GFP_KERNEL);
  1113. if (!bp->rx_ring)
  1114. goto out_err;
  1115. netdev_dbg(bp->dev,
  1116. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  1117. size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
  1118. size = TX_RING_BYTES;
  1119. bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1120. &bp->tx_ring_dma, GFP_KERNEL);
  1121. if (!bp->tx_ring)
  1122. goto out_err;
  1123. netdev_dbg(bp->dev,
  1124. "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
  1125. size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
  1126. if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
  1127. goto out_err;
  1128. return 0;
  1129. out_err:
  1130. macb_free_consistent(bp);
  1131. return -ENOMEM;
  1132. }
  1133. static void gem_init_rings(struct macb *bp)
  1134. {
  1135. int i;
  1136. for (i = 0; i < TX_RING_SIZE; i++) {
  1137. bp->tx_ring[i].addr = 0;
  1138. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  1139. }
  1140. bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  1141. bp->rx_tail = bp->rx_prepared_head = bp->tx_head = bp->tx_tail = 0;
  1142. gem_rx_refill(bp);
  1143. }
  1144. static void macb_init_rings(struct macb *bp)
  1145. {
  1146. int i;
  1147. dma_addr_t addr;
  1148. addr = bp->rx_buffers_dma;
  1149. for (i = 0; i < RX_RING_SIZE; i++) {
  1150. bp->rx_ring[i].addr = addr;
  1151. bp->rx_ring[i].ctrl = 0;
  1152. addr += bp->rx_buffer_size;
  1153. }
  1154. bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
  1155. for (i = 0; i < TX_RING_SIZE; i++) {
  1156. bp->tx_ring[i].addr = 0;
  1157. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  1158. }
  1159. bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  1160. bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
  1161. }
  1162. static void macb_reset_hw(struct macb *bp)
  1163. {
  1164. /*
  1165. * Disable RX and TX (XXX: Should we halt the transmission
  1166. * more gracefully?)
  1167. */
  1168. macb_writel(bp, NCR, 0);
  1169. /* Clear the stats registers (XXX: Update stats first?) */
  1170. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  1171. /* Clear all status flags */
  1172. macb_writel(bp, TSR, -1);
  1173. macb_writel(bp, RSR, -1);
  1174. /* Disable all interrupts */
  1175. macb_writel(bp, IDR, -1);
  1176. macb_readl(bp, ISR);
  1177. }
  1178. static u32 gem_mdc_clk_div(struct macb *bp)
  1179. {
  1180. u32 config;
  1181. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  1182. if (pclk_hz <= 20000000)
  1183. config = GEM_BF(CLK, GEM_CLK_DIV8);
  1184. else if (pclk_hz <= 40000000)
  1185. config = GEM_BF(CLK, GEM_CLK_DIV16);
  1186. else if (pclk_hz <= 80000000)
  1187. config = GEM_BF(CLK, GEM_CLK_DIV32);
  1188. else if (pclk_hz <= 120000000)
  1189. config = GEM_BF(CLK, GEM_CLK_DIV48);
  1190. else if (pclk_hz <= 160000000)
  1191. config = GEM_BF(CLK, GEM_CLK_DIV64);
  1192. else
  1193. config = GEM_BF(CLK, GEM_CLK_DIV96);
  1194. return config;
  1195. }
  1196. static u32 macb_mdc_clk_div(struct macb *bp)
  1197. {
  1198. u32 config;
  1199. unsigned long pclk_hz;
  1200. if (macb_is_gem(bp))
  1201. return gem_mdc_clk_div(bp);
  1202. pclk_hz = clk_get_rate(bp->pclk);
  1203. if (pclk_hz <= 20000000)
  1204. config = MACB_BF(CLK, MACB_CLK_DIV8);
  1205. else if (pclk_hz <= 40000000)
  1206. config = MACB_BF(CLK, MACB_CLK_DIV16);
  1207. else if (pclk_hz <= 80000000)
  1208. config = MACB_BF(CLK, MACB_CLK_DIV32);
  1209. else
  1210. config = MACB_BF(CLK, MACB_CLK_DIV64);
  1211. return config;
  1212. }
  1213. /*
  1214. * Get the DMA bus width field of the network configuration register that we
  1215. * should program. We find the width from decoding the design configuration
  1216. * register to find the maximum supported data bus width.
  1217. */
  1218. static u32 macb_dbw(struct macb *bp)
  1219. {
  1220. if (!macb_is_gem(bp))
  1221. return 0;
  1222. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  1223. case 4:
  1224. return GEM_BF(DBW, GEM_DBW128);
  1225. case 2:
  1226. return GEM_BF(DBW, GEM_DBW64);
  1227. case 1:
  1228. default:
  1229. return GEM_BF(DBW, GEM_DBW32);
  1230. }
  1231. }
  1232. /*
  1233. * Configure the receive DMA engine
  1234. * - use the correct receive buffer size
  1235. * - set best burst length for DMA operations
  1236. * (if not supported by FIFO, it will fallback to default)
  1237. * - set both rx/tx packet buffers to full memory size
  1238. * These are configurable parameters for GEM.
  1239. */
  1240. static void macb_configure_dma(struct macb *bp)
  1241. {
  1242. u32 dmacfg;
  1243. if (macb_is_gem(bp)) {
  1244. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  1245. dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
  1246. if (bp->dma_burst_length)
  1247. dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
  1248. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  1249. dmacfg &= ~GEM_BIT(ENDIA);
  1250. if (bp->dev->features & NETIF_F_HW_CSUM)
  1251. dmacfg |= GEM_BIT(TXCOEN);
  1252. else
  1253. dmacfg &= ~GEM_BIT(TXCOEN);
  1254. netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
  1255. dmacfg);
  1256. gem_writel(bp, DMACFG, dmacfg);
  1257. }
  1258. }
  1259. static void macb_init_hw(struct macb *bp)
  1260. {
  1261. u32 config;
  1262. macb_reset_hw(bp);
  1263. macb_set_hwaddr(bp);
  1264. config = macb_mdc_clk_div(bp);
  1265. config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
  1266. config |= MACB_BIT(PAE); /* PAuse Enable */
  1267. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  1268. config |= MACB_BIT(BIG); /* Receive oversized frames */
  1269. if (bp->dev->flags & IFF_PROMISC)
  1270. config |= MACB_BIT(CAF); /* Copy All Frames */
  1271. else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
  1272. config |= GEM_BIT(RXCOEN);
  1273. if (!(bp->dev->flags & IFF_BROADCAST))
  1274. config |= MACB_BIT(NBC); /* No BroadCast */
  1275. config |= macb_dbw(bp);
  1276. macb_writel(bp, NCFGR, config);
  1277. bp->speed = SPEED_10;
  1278. bp->duplex = DUPLEX_HALF;
  1279. macb_configure_dma(bp);
  1280. /* Initialize TX and RX buffers */
  1281. macb_writel(bp, RBQP, bp->rx_ring_dma);
  1282. macb_writel(bp, TBQP, bp->tx_ring_dma);
  1283. /* Enable TX and RX */
  1284. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  1285. /* Enable interrupts */
  1286. macb_writel(bp, IER, (MACB_RX_INT_FLAGS
  1287. | MACB_TX_INT_FLAGS
  1288. | MACB_BIT(HRESP)));
  1289. }
  1290. /*
  1291. * The hash address register is 64 bits long and takes up two
  1292. * locations in the memory map. The least significant bits are stored
  1293. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  1294. *
  1295. * The unicast hash enable and the multicast hash enable bits in the
  1296. * network configuration register enable the reception of hash matched
  1297. * frames. The destination address is reduced to a 6 bit index into
  1298. * the 64 bit hash register using the following hash function. The
  1299. * hash function is an exclusive or of every sixth bit of the
  1300. * destination address.
  1301. *
  1302. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  1303. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  1304. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  1305. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  1306. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  1307. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  1308. *
  1309. * da[0] represents the least significant bit of the first byte
  1310. * received, that is, the multicast/unicast indicator, and da[47]
  1311. * represents the most significant bit of the last byte received. If
  1312. * the hash index, hi[n], points to a bit that is set in the hash
  1313. * register then the frame will be matched according to whether the
  1314. * frame is multicast or unicast. A multicast match will be signalled
  1315. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  1316. * index points to a bit set in the hash register. A unicast match
  1317. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  1318. * and the hash index points to a bit set in the hash register. To
  1319. * receive all multicast frames, the hash register should be set with
  1320. * all ones and the multicast hash enable bit should be set in the
  1321. * network configuration register.
  1322. */
  1323. static inline int hash_bit_value(int bitnr, __u8 *addr)
  1324. {
  1325. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  1326. return 1;
  1327. return 0;
  1328. }
  1329. /*
  1330. * Return the hash index value for the specified address.
  1331. */
  1332. static int hash_get_index(__u8 *addr)
  1333. {
  1334. int i, j, bitval;
  1335. int hash_index = 0;
  1336. for (j = 0; j < 6; j++) {
  1337. for (i = 0, bitval = 0; i < 8; i++)
  1338. bitval ^= hash_bit_value(i*6 + j, addr);
  1339. hash_index |= (bitval << j);
  1340. }
  1341. return hash_index;
  1342. }
  1343. /*
  1344. * Add multicast addresses to the internal multicast-hash table.
  1345. */
  1346. static void macb_sethashtable(struct net_device *dev)
  1347. {
  1348. struct netdev_hw_addr *ha;
  1349. unsigned long mc_filter[2];
  1350. unsigned int bitnr;
  1351. struct macb *bp = netdev_priv(dev);
  1352. mc_filter[0] = mc_filter[1] = 0;
  1353. netdev_for_each_mc_addr(ha, dev) {
  1354. bitnr = hash_get_index(ha->addr);
  1355. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  1356. }
  1357. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  1358. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  1359. }
  1360. /*
  1361. * Enable/Disable promiscuous and multicast modes.
  1362. */
  1363. void macb_set_rx_mode(struct net_device *dev)
  1364. {
  1365. unsigned long cfg;
  1366. struct macb *bp = netdev_priv(dev);
  1367. cfg = macb_readl(bp, NCFGR);
  1368. if (dev->flags & IFF_PROMISC) {
  1369. /* Enable promiscuous mode */
  1370. cfg |= MACB_BIT(CAF);
  1371. /* Disable RX checksum offload */
  1372. if (macb_is_gem(bp))
  1373. cfg &= ~GEM_BIT(RXCOEN);
  1374. } else {
  1375. /* Disable promiscuous mode */
  1376. cfg &= ~MACB_BIT(CAF);
  1377. /* Enable RX checksum offload only if requested */
  1378. if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
  1379. cfg |= GEM_BIT(RXCOEN);
  1380. }
  1381. if (dev->flags & IFF_ALLMULTI) {
  1382. /* Enable all multicast mode */
  1383. macb_or_gem_writel(bp, HRB, -1);
  1384. macb_or_gem_writel(bp, HRT, -1);
  1385. cfg |= MACB_BIT(NCFGR_MTI);
  1386. } else if (!netdev_mc_empty(dev)) {
  1387. /* Enable specific multicasts */
  1388. macb_sethashtable(dev);
  1389. cfg |= MACB_BIT(NCFGR_MTI);
  1390. } else if (dev->flags & (~IFF_ALLMULTI)) {
  1391. /* Disable all multicast mode */
  1392. macb_or_gem_writel(bp, HRB, 0);
  1393. macb_or_gem_writel(bp, HRT, 0);
  1394. cfg &= ~MACB_BIT(NCFGR_MTI);
  1395. }
  1396. macb_writel(bp, NCFGR, cfg);
  1397. }
  1398. EXPORT_SYMBOL_GPL(macb_set_rx_mode);
  1399. static int macb_open(struct net_device *dev)
  1400. {
  1401. struct macb *bp = netdev_priv(dev);
  1402. size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
  1403. int err;
  1404. netdev_dbg(bp->dev, "open\n");
  1405. /* carrier starts down */
  1406. netif_carrier_off(dev);
  1407. /* if the phy is not yet register, retry later*/
  1408. if (!bp->phy_dev)
  1409. return -EAGAIN;
  1410. /* RX buffers initialization */
  1411. macb_init_rx_buffer_size(bp, bufsz);
  1412. err = macb_alloc_consistent(bp);
  1413. if (err) {
  1414. netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
  1415. err);
  1416. return err;
  1417. }
  1418. napi_enable(&bp->napi);
  1419. bp->macbgem_ops.mog_init_rings(bp);
  1420. macb_init_hw(bp);
  1421. /* schedule a link state check */
  1422. phy_start(bp->phy_dev);
  1423. netif_start_queue(dev);
  1424. return 0;
  1425. }
  1426. static int macb_close(struct net_device *dev)
  1427. {
  1428. struct macb *bp = netdev_priv(dev);
  1429. unsigned long flags;
  1430. netif_stop_queue(dev);
  1431. napi_disable(&bp->napi);
  1432. if (bp->phy_dev)
  1433. phy_stop(bp->phy_dev);
  1434. spin_lock_irqsave(&bp->lock, flags);
  1435. macb_reset_hw(bp);
  1436. netif_carrier_off(dev);
  1437. spin_unlock_irqrestore(&bp->lock, flags);
  1438. macb_free_consistent(bp);
  1439. return 0;
  1440. }
  1441. static void gem_update_stats(struct macb *bp)
  1442. {
  1443. u32 __iomem *reg = bp->regs + GEM_OTX;
  1444. u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
  1445. u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
  1446. for (; p < end; p++, reg++)
  1447. *p += __raw_readl(reg);
  1448. }
  1449. static struct net_device_stats *gem_get_stats(struct macb *bp)
  1450. {
  1451. struct gem_stats *hwstat = &bp->hw_stats.gem;
  1452. struct net_device_stats *nstat = &bp->stats;
  1453. gem_update_stats(bp);
  1454. nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
  1455. hwstat->rx_alignment_errors +
  1456. hwstat->rx_resource_errors +
  1457. hwstat->rx_overruns +
  1458. hwstat->rx_oversize_frames +
  1459. hwstat->rx_jabbers +
  1460. hwstat->rx_undersized_frames +
  1461. hwstat->rx_length_field_frame_errors);
  1462. nstat->tx_errors = (hwstat->tx_late_collisions +
  1463. hwstat->tx_excessive_collisions +
  1464. hwstat->tx_underrun +
  1465. hwstat->tx_carrier_sense_errors);
  1466. nstat->multicast = hwstat->rx_multicast_frames;
  1467. nstat->collisions = (hwstat->tx_single_collision_frames +
  1468. hwstat->tx_multiple_collision_frames +
  1469. hwstat->tx_excessive_collisions);
  1470. nstat->rx_length_errors = (hwstat->rx_oversize_frames +
  1471. hwstat->rx_jabbers +
  1472. hwstat->rx_undersized_frames +
  1473. hwstat->rx_length_field_frame_errors);
  1474. nstat->rx_over_errors = hwstat->rx_resource_errors;
  1475. nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
  1476. nstat->rx_frame_errors = hwstat->rx_alignment_errors;
  1477. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1478. nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
  1479. nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
  1480. nstat->tx_fifo_errors = hwstat->tx_underrun;
  1481. return nstat;
  1482. }
  1483. struct net_device_stats *macb_get_stats(struct net_device *dev)
  1484. {
  1485. struct macb *bp = netdev_priv(dev);
  1486. struct net_device_stats *nstat = &bp->stats;
  1487. struct macb_stats *hwstat = &bp->hw_stats.macb;
  1488. if (macb_is_gem(bp))
  1489. return gem_get_stats(bp);
  1490. /* read stats from hardware */
  1491. macb_update_stats(bp);
  1492. /* Convert HW stats into netdevice stats */
  1493. nstat->rx_errors = (hwstat->rx_fcs_errors +
  1494. hwstat->rx_align_errors +
  1495. hwstat->rx_resource_errors +
  1496. hwstat->rx_overruns +
  1497. hwstat->rx_oversize_pkts +
  1498. hwstat->rx_jabbers +
  1499. hwstat->rx_undersize_pkts +
  1500. hwstat->sqe_test_errors +
  1501. hwstat->rx_length_mismatch);
  1502. nstat->tx_errors = (hwstat->tx_late_cols +
  1503. hwstat->tx_excessive_cols +
  1504. hwstat->tx_underruns +
  1505. hwstat->tx_carrier_errors);
  1506. nstat->collisions = (hwstat->tx_single_cols +
  1507. hwstat->tx_multiple_cols +
  1508. hwstat->tx_excessive_cols);
  1509. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  1510. hwstat->rx_jabbers +
  1511. hwstat->rx_undersize_pkts +
  1512. hwstat->rx_length_mismatch);
  1513. nstat->rx_over_errors = hwstat->rx_resource_errors +
  1514. hwstat->rx_overruns;
  1515. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  1516. nstat->rx_frame_errors = hwstat->rx_align_errors;
  1517. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1518. /* XXX: What does "missed" mean? */
  1519. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  1520. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  1521. nstat->tx_fifo_errors = hwstat->tx_underruns;
  1522. /* Don't know about heartbeat or window errors... */
  1523. return nstat;
  1524. }
  1525. EXPORT_SYMBOL_GPL(macb_get_stats);
  1526. static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1527. {
  1528. struct macb *bp = netdev_priv(dev);
  1529. struct phy_device *phydev = bp->phy_dev;
  1530. if (!phydev)
  1531. return -ENODEV;
  1532. return phy_ethtool_gset(phydev, cmd);
  1533. }
  1534. static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1535. {
  1536. struct macb *bp = netdev_priv(dev);
  1537. struct phy_device *phydev = bp->phy_dev;
  1538. if (!phydev)
  1539. return -ENODEV;
  1540. return phy_ethtool_sset(phydev, cmd);
  1541. }
  1542. static int macb_get_regs_len(struct net_device *netdev)
  1543. {
  1544. return MACB_GREGS_NBR * sizeof(u32);
  1545. }
  1546. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1547. void *p)
  1548. {
  1549. struct macb *bp = netdev_priv(dev);
  1550. unsigned int tail, head;
  1551. u32 *regs_buff = p;
  1552. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  1553. | MACB_GREGS_VERSION;
  1554. tail = macb_tx_ring_wrap(bp->tx_tail);
  1555. head = macb_tx_ring_wrap(bp->tx_head);
  1556. regs_buff[0] = macb_readl(bp, NCR);
  1557. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  1558. regs_buff[2] = macb_readl(bp, NSR);
  1559. regs_buff[3] = macb_readl(bp, TSR);
  1560. regs_buff[4] = macb_readl(bp, RBQP);
  1561. regs_buff[5] = macb_readl(bp, TBQP);
  1562. regs_buff[6] = macb_readl(bp, RSR);
  1563. regs_buff[7] = macb_readl(bp, IMR);
  1564. regs_buff[8] = tail;
  1565. regs_buff[9] = head;
  1566. regs_buff[10] = macb_tx_dma(bp, tail);
  1567. regs_buff[11] = macb_tx_dma(bp, head);
  1568. if (macb_is_gem(bp)) {
  1569. regs_buff[12] = gem_readl(bp, USRIO);
  1570. regs_buff[13] = gem_readl(bp, DMACFG);
  1571. }
  1572. }
  1573. const struct ethtool_ops macb_ethtool_ops = {
  1574. .get_settings = macb_get_settings,
  1575. .set_settings = macb_set_settings,
  1576. .get_regs_len = macb_get_regs_len,
  1577. .get_regs = macb_get_regs,
  1578. .get_link = ethtool_op_get_link,
  1579. .get_ts_info = ethtool_op_get_ts_info,
  1580. };
  1581. EXPORT_SYMBOL_GPL(macb_ethtool_ops);
  1582. int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1583. {
  1584. struct macb *bp = netdev_priv(dev);
  1585. struct phy_device *phydev = bp->phy_dev;
  1586. if (!netif_running(dev))
  1587. return -EINVAL;
  1588. if (!phydev)
  1589. return -ENODEV;
  1590. return phy_mii_ioctl(phydev, rq, cmd);
  1591. }
  1592. EXPORT_SYMBOL_GPL(macb_ioctl);
  1593. static int macb_set_features(struct net_device *netdev,
  1594. netdev_features_t features)
  1595. {
  1596. struct macb *bp = netdev_priv(netdev);
  1597. netdev_features_t changed = features ^ netdev->features;
  1598. /* TX checksum offload */
  1599. if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
  1600. u32 dmacfg;
  1601. dmacfg = gem_readl(bp, DMACFG);
  1602. if (features & NETIF_F_HW_CSUM)
  1603. dmacfg |= GEM_BIT(TXCOEN);
  1604. else
  1605. dmacfg &= ~GEM_BIT(TXCOEN);
  1606. gem_writel(bp, DMACFG, dmacfg);
  1607. }
  1608. /* RX checksum offload */
  1609. if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
  1610. u32 netcfg;
  1611. netcfg = gem_readl(bp, NCFGR);
  1612. if (features & NETIF_F_RXCSUM &&
  1613. !(netdev->flags & IFF_PROMISC))
  1614. netcfg |= GEM_BIT(RXCOEN);
  1615. else
  1616. netcfg &= ~GEM_BIT(RXCOEN);
  1617. gem_writel(bp, NCFGR, netcfg);
  1618. }
  1619. return 0;
  1620. }
  1621. static const struct net_device_ops macb_netdev_ops = {
  1622. .ndo_open = macb_open,
  1623. .ndo_stop = macb_close,
  1624. .ndo_start_xmit = macb_start_xmit,
  1625. .ndo_set_rx_mode = macb_set_rx_mode,
  1626. .ndo_get_stats = macb_get_stats,
  1627. .ndo_do_ioctl = macb_ioctl,
  1628. .ndo_validate_addr = eth_validate_addr,
  1629. .ndo_change_mtu = eth_change_mtu,
  1630. .ndo_set_mac_address = eth_mac_addr,
  1631. #ifdef CONFIG_NET_POLL_CONTROLLER
  1632. .ndo_poll_controller = macb_poll_controller,
  1633. #endif
  1634. .ndo_set_features = macb_set_features,
  1635. };
  1636. #if defined(CONFIG_OF)
  1637. static struct macb_config pc302gem_config = {
  1638. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  1639. .dma_burst_length = 16,
  1640. };
  1641. static struct macb_config sama5d3_config = {
  1642. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  1643. .dma_burst_length = 16,
  1644. };
  1645. static struct macb_config sama5d4_config = {
  1646. .caps = 0,
  1647. .dma_burst_length = 4,
  1648. };
  1649. static const struct of_device_id macb_dt_ids[] = {
  1650. { .compatible = "cdns,at32ap7000-macb" },
  1651. { .compatible = "cdns,at91sam9260-macb" },
  1652. { .compatible = "cdns,macb" },
  1653. { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
  1654. { .compatible = "cdns,gem", .data = &pc302gem_config },
  1655. { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
  1656. { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
  1657. { /* sentinel */ }
  1658. };
  1659. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  1660. #endif
  1661. /*
  1662. * Configure peripheral capacities according to device tree
  1663. * and integration options used
  1664. */
  1665. static void macb_configure_caps(struct macb *bp)
  1666. {
  1667. u32 dcfg;
  1668. const struct of_device_id *match;
  1669. const struct macb_config *config;
  1670. if (bp->pdev->dev.of_node) {
  1671. match = of_match_node(macb_dt_ids, bp->pdev->dev.of_node);
  1672. if (match && match->data) {
  1673. config = (const struct macb_config *)match->data;
  1674. bp->caps = config->caps;
  1675. /*
  1676. * As we have access to the matching node, configure
  1677. * DMA burst length as well
  1678. */
  1679. bp->dma_burst_length = config->dma_burst_length;
  1680. }
  1681. }
  1682. if (MACB_BFEXT(IDNUM, macb_readl(bp, MID)) == 0x2)
  1683. bp->caps |= MACB_CAPS_MACB_IS_GEM;
  1684. if (macb_is_gem(bp)) {
  1685. dcfg = gem_readl(bp, DCFG1);
  1686. if (GEM_BFEXT(IRQCOR, dcfg) == 0)
  1687. bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
  1688. dcfg = gem_readl(bp, DCFG2);
  1689. if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
  1690. bp->caps |= MACB_CAPS_FIFO_MODE;
  1691. }
  1692. netdev_dbg(bp->dev, "Cadence caps 0x%08x\n", bp->caps);
  1693. }
  1694. static int __init macb_probe(struct platform_device *pdev)
  1695. {
  1696. struct macb_platform_data *pdata;
  1697. struct resource *regs;
  1698. struct net_device *dev;
  1699. struct macb *bp;
  1700. struct phy_device *phydev;
  1701. u32 config;
  1702. int err = -ENXIO;
  1703. struct pinctrl *pinctrl;
  1704. const char *mac;
  1705. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1706. if (!regs) {
  1707. dev_err(&pdev->dev, "no mmio resource defined\n");
  1708. goto err_out;
  1709. }
  1710. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  1711. if (IS_ERR(pinctrl)) {
  1712. err = PTR_ERR(pinctrl);
  1713. if (err == -EPROBE_DEFER)
  1714. goto err_out;
  1715. dev_warn(&pdev->dev, "No pinctrl provided\n");
  1716. }
  1717. err = -ENOMEM;
  1718. dev = alloc_etherdev(sizeof(*bp));
  1719. if (!dev)
  1720. goto err_out;
  1721. SET_NETDEV_DEV(dev, &pdev->dev);
  1722. bp = netdev_priv(dev);
  1723. bp->pdev = pdev;
  1724. bp->dev = dev;
  1725. spin_lock_init(&bp->lock);
  1726. INIT_WORK(&bp->tx_error_task, macb_tx_error_task);
  1727. bp->pclk = devm_clk_get(&pdev->dev, "pclk");
  1728. if (IS_ERR(bp->pclk)) {
  1729. err = PTR_ERR(bp->pclk);
  1730. dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
  1731. goto err_out_free_dev;
  1732. }
  1733. bp->hclk = devm_clk_get(&pdev->dev, "hclk");
  1734. if (IS_ERR(bp->hclk)) {
  1735. err = PTR_ERR(bp->hclk);
  1736. dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
  1737. goto err_out_free_dev;
  1738. }
  1739. bp->tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
  1740. err = clk_prepare_enable(bp->pclk);
  1741. if (err) {
  1742. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  1743. goto err_out_free_dev;
  1744. }
  1745. err = clk_prepare_enable(bp->hclk);
  1746. if (err) {
  1747. dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
  1748. goto err_out_disable_pclk;
  1749. }
  1750. if (!IS_ERR(bp->tx_clk)) {
  1751. err = clk_prepare_enable(bp->tx_clk);
  1752. if (err) {
  1753. dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n",
  1754. err);
  1755. goto err_out_disable_hclk;
  1756. }
  1757. }
  1758. bp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
  1759. if (!bp->regs) {
  1760. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  1761. err = -ENOMEM;
  1762. goto err_out_disable_clocks;
  1763. }
  1764. dev->irq = platform_get_irq(pdev, 0);
  1765. err = devm_request_irq(&pdev->dev, dev->irq, macb_interrupt, 0,
  1766. dev->name, dev);
  1767. if (err) {
  1768. dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
  1769. dev->irq, err);
  1770. goto err_out_disable_clocks;
  1771. }
  1772. dev->netdev_ops = &macb_netdev_ops;
  1773. netif_napi_add(dev, &bp->napi, macb_poll, 64);
  1774. dev->ethtool_ops = &macb_ethtool_ops;
  1775. dev->base_addr = regs->start;
  1776. /* setup capacities */
  1777. macb_configure_caps(bp);
  1778. /* setup appropriated routines according to adapter type */
  1779. if (macb_is_gem(bp)) {
  1780. bp->max_tx_length = GEM_MAX_TX_LEN;
  1781. bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
  1782. bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
  1783. bp->macbgem_ops.mog_init_rings = gem_init_rings;
  1784. bp->macbgem_ops.mog_rx = gem_rx;
  1785. } else {
  1786. bp->max_tx_length = MACB_MAX_TX_LEN;
  1787. bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
  1788. bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
  1789. bp->macbgem_ops.mog_init_rings = macb_init_rings;
  1790. bp->macbgem_ops.mog_rx = macb_rx;
  1791. }
  1792. /* Set features */
  1793. dev->hw_features = NETIF_F_SG;
  1794. /* Checksum offload is only available on gem with packet buffer */
  1795. if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
  1796. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1797. if (bp->caps & MACB_CAPS_SG_DISABLED)
  1798. dev->hw_features &= ~NETIF_F_SG;
  1799. dev->features = dev->hw_features;
  1800. /* Set MII management clock divider */
  1801. config = macb_mdc_clk_div(bp);
  1802. config |= macb_dbw(bp);
  1803. macb_writel(bp, NCFGR, config);
  1804. mac = of_get_mac_address(pdev->dev.of_node);
  1805. if (mac)
  1806. memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
  1807. else
  1808. macb_get_hwaddr(bp);
  1809. err = of_get_phy_mode(pdev->dev.of_node);
  1810. if (err < 0) {
  1811. pdata = dev_get_platdata(&pdev->dev);
  1812. if (pdata && pdata->is_rmii)
  1813. bp->phy_interface = PHY_INTERFACE_MODE_RMII;
  1814. else
  1815. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  1816. } else {
  1817. bp->phy_interface = err;
  1818. }
  1819. if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
  1820. macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
  1821. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
  1822. #if defined(CONFIG_ARCH_AT91)
  1823. macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
  1824. MACB_BIT(CLKEN)));
  1825. #else
  1826. macb_or_gem_writel(bp, USRIO, 0);
  1827. #endif
  1828. else
  1829. #if defined(CONFIG_ARCH_AT91)
  1830. macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
  1831. #else
  1832. macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
  1833. #endif
  1834. err = register_netdev(dev);
  1835. if (err) {
  1836. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  1837. goto err_out_disable_clocks;
  1838. }
  1839. err = macb_mii_init(bp);
  1840. if (err)
  1841. goto err_out_unregister_netdev;
  1842. platform_set_drvdata(pdev, dev);
  1843. netif_carrier_off(dev);
  1844. netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
  1845. macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
  1846. dev->irq, dev->dev_addr);
  1847. phydev = bp->phy_dev;
  1848. netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  1849. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  1850. return 0;
  1851. err_out_unregister_netdev:
  1852. unregister_netdev(dev);
  1853. err_out_disable_clocks:
  1854. if (!IS_ERR(bp->tx_clk))
  1855. clk_disable_unprepare(bp->tx_clk);
  1856. err_out_disable_hclk:
  1857. clk_disable_unprepare(bp->hclk);
  1858. err_out_disable_pclk:
  1859. clk_disable_unprepare(bp->pclk);
  1860. err_out_free_dev:
  1861. free_netdev(dev);
  1862. err_out:
  1863. return err;
  1864. }
  1865. static int __exit macb_remove(struct platform_device *pdev)
  1866. {
  1867. struct net_device *dev;
  1868. struct macb *bp;
  1869. dev = platform_get_drvdata(pdev);
  1870. if (dev) {
  1871. bp = netdev_priv(dev);
  1872. if (bp->phy_dev)
  1873. phy_disconnect(bp->phy_dev);
  1874. mdiobus_unregister(bp->mii_bus);
  1875. kfree(bp->mii_bus->irq);
  1876. mdiobus_free(bp->mii_bus);
  1877. unregister_netdev(dev);
  1878. if (!IS_ERR(bp->tx_clk))
  1879. clk_disable_unprepare(bp->tx_clk);
  1880. clk_disable_unprepare(bp->hclk);
  1881. clk_disable_unprepare(bp->pclk);
  1882. free_netdev(dev);
  1883. }
  1884. return 0;
  1885. }
  1886. #ifdef CONFIG_PM
  1887. static int macb_suspend(struct device *dev)
  1888. {
  1889. struct platform_device *pdev = to_platform_device(dev);
  1890. struct net_device *netdev = platform_get_drvdata(pdev);
  1891. struct macb *bp = netdev_priv(netdev);
  1892. netif_carrier_off(netdev);
  1893. netif_device_detach(netdev);
  1894. if (!IS_ERR(bp->tx_clk))
  1895. clk_disable_unprepare(bp->tx_clk);
  1896. clk_disable_unprepare(bp->hclk);
  1897. clk_disable_unprepare(bp->pclk);
  1898. return 0;
  1899. }
  1900. static int macb_resume(struct device *dev)
  1901. {
  1902. struct platform_device *pdev = to_platform_device(dev);
  1903. struct net_device *netdev = platform_get_drvdata(pdev);
  1904. struct macb *bp = netdev_priv(netdev);
  1905. clk_prepare_enable(bp->pclk);
  1906. clk_prepare_enable(bp->hclk);
  1907. if (!IS_ERR(bp->tx_clk))
  1908. clk_prepare_enable(bp->tx_clk);
  1909. netif_device_attach(netdev);
  1910. return 0;
  1911. }
  1912. #endif
  1913. static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
  1914. static struct platform_driver macb_driver = {
  1915. .remove = __exit_p(macb_remove),
  1916. .driver = {
  1917. .name = "macb",
  1918. .owner = THIS_MODULE,
  1919. .of_match_table = of_match_ptr(macb_dt_ids),
  1920. .pm = &macb_pm_ops,
  1921. },
  1922. };
  1923. module_platform_driver_probe(macb_driver, macb_probe);
  1924. MODULE_LICENSE("GPL");
  1925. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  1926. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1927. MODULE_ALIAS("platform:macb");