macb.c 63 KB

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