macb.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870
  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. static 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. static 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. static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  139. {
  140. struct macb *bp = bus->priv;
  141. int value;
  142. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  143. | MACB_BF(RW, MACB_MAN_READ)
  144. | MACB_BF(PHYA, mii_id)
  145. | MACB_BF(REGA, regnum)
  146. | MACB_BF(CODE, MACB_MAN_CODE)));
  147. /* wait for end of transfer */
  148. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  149. cpu_relax();
  150. value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  151. return value;
  152. }
  153. static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  154. u16 value)
  155. {
  156. struct macb *bp = bus->priv;
  157. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  158. | MACB_BF(RW, MACB_MAN_WRITE)
  159. | MACB_BF(PHYA, mii_id)
  160. | MACB_BF(REGA, regnum)
  161. | MACB_BF(CODE, MACB_MAN_CODE)
  162. | MACB_BF(DATA, value)));
  163. /* wait for end of transfer */
  164. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  165. cpu_relax();
  166. return 0;
  167. }
  168. /**
  169. * macb_set_tx_clk() - Set a clock to a new frequency
  170. * @clk Pointer to the clock to change
  171. * @rate New frequency in Hz
  172. * @dev Pointer to the struct net_device
  173. */
  174. static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
  175. {
  176. long ferr, rate, rate_rounded;
  177. if (!clk)
  178. return;
  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 (status_change) {
  244. if (phydev->link) {
  245. /* Update the TX clock rate if and only if the link is
  246. * up and there has been a link change.
  247. */
  248. macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
  249. netif_carrier_on(dev);
  250. netdev_info(dev, "link up (%d/%s)\n",
  251. phydev->speed,
  252. phydev->duplex == DUPLEX_FULL ?
  253. "Full" : "Half");
  254. } else {
  255. netif_carrier_off(dev);
  256. netdev_info(dev, "link down\n");
  257. }
  258. }
  259. }
  260. /* based on au1000_eth. c*/
  261. static int macb_mii_probe(struct net_device *dev)
  262. {
  263. struct macb *bp = netdev_priv(dev);
  264. struct macb_platform_data *pdata;
  265. struct phy_device *phydev;
  266. int phy_irq;
  267. int ret;
  268. phydev = phy_find_first(bp->mii_bus);
  269. if (!phydev) {
  270. netdev_err(dev, "no PHY found\n");
  271. return -ENXIO;
  272. }
  273. pdata = dev_get_platdata(&bp->pdev->dev);
  274. if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
  275. ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
  276. if (!ret) {
  277. phy_irq = gpio_to_irq(pdata->phy_irq_pin);
  278. phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
  279. }
  280. }
  281. /* attach the mac to the phy */
  282. ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
  283. bp->phy_interface);
  284. if (ret) {
  285. netdev_err(dev, "Could not attach to PHY\n");
  286. return ret;
  287. }
  288. /* mask with MAC supported features */
  289. if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
  290. phydev->supported &= PHY_GBIT_FEATURES;
  291. else
  292. phydev->supported &= PHY_BASIC_FEATURES;
  293. phydev->advertising = phydev->supported;
  294. bp->link = 0;
  295. bp->speed = 0;
  296. bp->duplex = -1;
  297. bp->phy_dev = phydev;
  298. return 0;
  299. }
  300. static int macb_mii_init(struct macb *bp)
  301. {
  302. struct macb_platform_data *pdata;
  303. struct device_node *np;
  304. int err = -ENXIO, i;
  305. /* Enable management port */
  306. macb_writel(bp, NCR, MACB_BIT(MPE));
  307. bp->mii_bus = mdiobus_alloc();
  308. if (bp->mii_bus == NULL) {
  309. err = -ENOMEM;
  310. goto err_out;
  311. }
  312. bp->mii_bus->name = "MACB_mii_bus";
  313. bp->mii_bus->read = &macb_mdio_read;
  314. bp->mii_bus->write = &macb_mdio_write;
  315. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  316. bp->pdev->name, bp->pdev->id);
  317. bp->mii_bus->priv = bp;
  318. bp->mii_bus->parent = &bp->dev->dev;
  319. pdata = dev_get_platdata(&bp->pdev->dev);
  320. bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  321. if (!bp->mii_bus->irq) {
  322. err = -ENOMEM;
  323. goto err_out_free_mdiobus;
  324. }
  325. dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
  326. np = bp->pdev->dev.of_node;
  327. if (np) {
  328. /* try dt phy registration */
  329. err = of_mdiobus_register(bp->mii_bus, np);
  330. /* fallback to standard phy registration if no phy were
  331. found during dt phy registration */
  332. if (!err && !phy_find_first(bp->mii_bus)) {
  333. for (i = 0; i < PHY_MAX_ADDR; i++) {
  334. struct phy_device *phydev;
  335. phydev = mdiobus_scan(bp->mii_bus, i);
  336. if (IS_ERR(phydev)) {
  337. err = PTR_ERR(phydev);
  338. break;
  339. }
  340. }
  341. if (err)
  342. goto err_out_unregister_bus;
  343. }
  344. } else {
  345. for (i = 0; i < PHY_MAX_ADDR; i++)
  346. bp->mii_bus->irq[i] = PHY_POLL;
  347. if (pdata)
  348. bp->mii_bus->phy_mask = pdata->phy_mask;
  349. err = mdiobus_register(bp->mii_bus);
  350. }
  351. if (err)
  352. goto err_out_free_mdio_irq;
  353. err = macb_mii_probe(bp->dev);
  354. if (err)
  355. goto err_out_unregister_bus;
  356. return 0;
  357. err_out_unregister_bus:
  358. mdiobus_unregister(bp->mii_bus);
  359. err_out_free_mdio_irq:
  360. kfree(bp->mii_bus->irq);
  361. err_out_free_mdiobus:
  362. mdiobus_free(bp->mii_bus);
  363. err_out:
  364. return err;
  365. }
  366. static void macb_update_stats(struct macb *bp)
  367. {
  368. u32 __iomem *reg = bp->regs + MACB_PFR;
  369. u32 *p = &bp->hw_stats.macb.rx_pause_frames;
  370. u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
  371. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  372. for(; p < end; p++, reg++)
  373. *p += readl_relaxed(reg);
  374. }
  375. static int macb_halt_tx(struct macb *bp)
  376. {
  377. unsigned long halt_time, timeout;
  378. u32 status;
  379. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
  380. timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
  381. do {
  382. halt_time = jiffies;
  383. status = macb_readl(bp, TSR);
  384. if (!(status & MACB_BIT(TGO)))
  385. return 0;
  386. usleep_range(10, 250);
  387. } while (time_before(halt_time, timeout));
  388. return -ETIMEDOUT;
  389. }
  390. static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
  391. {
  392. if (tx_skb->mapping) {
  393. if (tx_skb->mapped_as_page)
  394. dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
  395. tx_skb->size, DMA_TO_DEVICE);
  396. else
  397. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
  398. tx_skb->size, DMA_TO_DEVICE);
  399. tx_skb->mapping = 0;
  400. }
  401. if (tx_skb->skb) {
  402. dev_kfree_skb_any(tx_skb->skb);
  403. tx_skb->skb = NULL;
  404. }
  405. }
  406. static void macb_tx_error_task(struct work_struct *work)
  407. {
  408. struct macb_queue *queue = container_of(work, struct macb_queue,
  409. tx_error_task);
  410. struct macb *bp = queue->bp;
  411. struct macb_tx_skb *tx_skb;
  412. struct macb_dma_desc *desc;
  413. struct sk_buff *skb;
  414. unsigned int tail;
  415. unsigned long flags;
  416. netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
  417. (unsigned int)(queue - bp->queues),
  418. queue->tx_tail, queue->tx_head);
  419. /* Prevent the queue IRQ handlers from running: each of them may call
  420. * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
  421. * As explained below, we have to halt the transmission before updating
  422. * TBQP registers so we call netif_tx_stop_all_queues() to notify the
  423. * network engine about the macb/gem being halted.
  424. */
  425. spin_lock_irqsave(&bp->lock, flags);
  426. /* Make sure nobody is trying to queue up new packets */
  427. netif_tx_stop_all_queues(bp->dev);
  428. /*
  429. * Stop transmission now
  430. * (in case we have just queued new packets)
  431. * macb/gem must be halted to write TBQP register
  432. */
  433. if (macb_halt_tx(bp))
  434. /* Just complain for now, reinitializing TX path can be good */
  435. netdev_err(bp->dev, "BUG: halt tx timed out\n");
  436. /*
  437. * Treat frames in TX queue including the ones that caused the error.
  438. * Free transmit buffers in upper layer.
  439. */
  440. for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
  441. u32 ctrl;
  442. desc = macb_tx_desc(queue, tail);
  443. ctrl = desc->ctrl;
  444. tx_skb = macb_tx_skb(queue, tail);
  445. skb = tx_skb->skb;
  446. if (ctrl & MACB_BIT(TX_USED)) {
  447. /* skb is set for the last buffer of the frame */
  448. while (!skb) {
  449. macb_tx_unmap(bp, tx_skb);
  450. tail++;
  451. tx_skb = macb_tx_skb(queue, tail);
  452. skb = tx_skb->skb;
  453. }
  454. /* ctrl still refers to the first buffer descriptor
  455. * since it's the only one written back by the hardware
  456. */
  457. if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
  458. netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
  459. macb_tx_ring_wrap(tail), skb->data);
  460. bp->stats.tx_packets++;
  461. bp->stats.tx_bytes += skb->len;
  462. }
  463. } else {
  464. /*
  465. * "Buffers exhausted mid-frame" errors may only happen
  466. * if the driver is buggy, so complain loudly about those.
  467. * Statistics are updated by hardware.
  468. */
  469. if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
  470. netdev_err(bp->dev,
  471. "BUG: TX buffers exhausted mid-frame\n");
  472. desc->ctrl = ctrl | MACB_BIT(TX_USED);
  473. }
  474. macb_tx_unmap(bp, tx_skb);
  475. }
  476. /* Set end of TX queue */
  477. desc = macb_tx_desc(queue, 0);
  478. desc->addr = 0;
  479. desc->ctrl = MACB_BIT(TX_USED);
  480. /* Make descriptor updates visible to hardware */
  481. wmb();
  482. /* Reinitialize the TX desc queue */
  483. queue_writel(queue, TBQP, queue->tx_ring_dma);
  484. /* Make TX ring reflect state of hardware */
  485. queue->tx_head = 0;
  486. queue->tx_tail = 0;
  487. /* Housework before enabling TX IRQ */
  488. macb_writel(bp, TSR, macb_readl(bp, TSR));
  489. queue_writel(queue, IER, MACB_TX_INT_FLAGS);
  490. /* Now we are ready to start transmission again */
  491. netif_tx_start_all_queues(bp->dev);
  492. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  493. spin_unlock_irqrestore(&bp->lock, flags);
  494. }
  495. static void macb_tx_interrupt(struct macb_queue *queue)
  496. {
  497. unsigned int tail;
  498. unsigned int head;
  499. u32 status;
  500. struct macb *bp = queue->bp;
  501. u16 queue_index = queue - bp->queues;
  502. status = macb_readl(bp, TSR);
  503. macb_writel(bp, TSR, status);
  504. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  505. queue_writel(queue, ISR, MACB_BIT(TCOMP));
  506. netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
  507. (unsigned long)status);
  508. head = queue->tx_head;
  509. for (tail = queue->tx_tail; tail != head; tail++) {
  510. struct macb_tx_skb *tx_skb;
  511. struct sk_buff *skb;
  512. struct macb_dma_desc *desc;
  513. u32 ctrl;
  514. desc = macb_tx_desc(queue, tail);
  515. /* Make hw descriptor updates visible to CPU */
  516. rmb();
  517. ctrl = desc->ctrl;
  518. /* TX_USED bit is only set by hardware on the very first buffer
  519. * descriptor of the transmitted frame.
  520. */
  521. if (!(ctrl & MACB_BIT(TX_USED)))
  522. break;
  523. /* Process all buffers of the current transmitted frame */
  524. for (;; tail++) {
  525. tx_skb = macb_tx_skb(queue, tail);
  526. skb = tx_skb->skb;
  527. /* First, update TX stats if needed */
  528. if (skb) {
  529. netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
  530. macb_tx_ring_wrap(tail), skb->data);
  531. bp->stats.tx_packets++;
  532. bp->stats.tx_bytes += skb->len;
  533. }
  534. /* Now we can safely release resources */
  535. macb_tx_unmap(bp, tx_skb);
  536. /* skb is set only for the last buffer of the frame.
  537. * WARNING: at this point skb has been freed by
  538. * macb_tx_unmap().
  539. */
  540. if (skb)
  541. break;
  542. }
  543. }
  544. queue->tx_tail = tail;
  545. if (__netif_subqueue_stopped(bp->dev, queue_index) &&
  546. CIRC_CNT(queue->tx_head, queue->tx_tail,
  547. TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
  548. netif_wake_subqueue(bp->dev, queue_index);
  549. }
  550. static void gem_rx_refill(struct macb *bp)
  551. {
  552. unsigned int entry;
  553. struct sk_buff *skb;
  554. dma_addr_t paddr;
  555. while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
  556. entry = macb_rx_ring_wrap(bp->rx_prepared_head);
  557. /* Make hw descriptor updates visible to CPU */
  558. rmb();
  559. bp->rx_prepared_head++;
  560. if (bp->rx_skbuff[entry] == NULL) {
  561. /* allocate sk_buff for this free entry in ring */
  562. skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
  563. if (unlikely(skb == NULL)) {
  564. netdev_err(bp->dev,
  565. "Unable to allocate sk_buff\n");
  566. break;
  567. }
  568. /* now fill corresponding descriptor entry */
  569. paddr = dma_map_single(&bp->pdev->dev, skb->data,
  570. bp->rx_buffer_size, DMA_FROM_DEVICE);
  571. if (dma_mapping_error(&bp->pdev->dev, paddr)) {
  572. dev_kfree_skb(skb);
  573. break;
  574. }
  575. bp->rx_skbuff[entry] = skb;
  576. if (entry == RX_RING_SIZE - 1)
  577. paddr |= MACB_BIT(RX_WRAP);
  578. bp->rx_ring[entry].addr = paddr;
  579. bp->rx_ring[entry].ctrl = 0;
  580. /* properly align Ethernet header */
  581. skb_reserve(skb, NET_IP_ALIGN);
  582. }
  583. }
  584. /* Make descriptor updates visible to hardware */
  585. wmb();
  586. netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
  587. bp->rx_prepared_head, bp->rx_tail);
  588. }
  589. /* Mark DMA descriptors from begin up to and not including end as unused */
  590. static void discard_partial_frame(struct macb *bp, unsigned int begin,
  591. unsigned int end)
  592. {
  593. unsigned int frag;
  594. for (frag = begin; frag != end; frag++) {
  595. struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
  596. desc->addr &= ~MACB_BIT(RX_USED);
  597. }
  598. /* Make descriptor updates visible to hardware */
  599. wmb();
  600. /*
  601. * When this happens, the hardware stats registers for
  602. * whatever caused this is updated, so we don't have to record
  603. * anything.
  604. */
  605. }
  606. static int gem_rx(struct macb *bp, int budget)
  607. {
  608. unsigned int len;
  609. unsigned int entry;
  610. struct sk_buff *skb;
  611. struct macb_dma_desc *desc;
  612. int count = 0;
  613. while (count < budget) {
  614. u32 addr, ctrl;
  615. entry = macb_rx_ring_wrap(bp->rx_tail);
  616. desc = &bp->rx_ring[entry];
  617. /* Make hw descriptor updates visible to CPU */
  618. rmb();
  619. addr = desc->addr;
  620. ctrl = desc->ctrl;
  621. if (!(addr & MACB_BIT(RX_USED)))
  622. break;
  623. bp->rx_tail++;
  624. count++;
  625. if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
  626. netdev_err(bp->dev,
  627. "not whole frame pointed by descriptor\n");
  628. bp->stats.rx_dropped++;
  629. break;
  630. }
  631. skb = bp->rx_skbuff[entry];
  632. if (unlikely(!skb)) {
  633. netdev_err(bp->dev,
  634. "inconsistent Rx descriptor chain\n");
  635. bp->stats.rx_dropped++;
  636. break;
  637. }
  638. /* now everything is ready for receiving packet */
  639. bp->rx_skbuff[entry] = NULL;
  640. len = MACB_BFEXT(RX_FRMLEN, ctrl);
  641. netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
  642. skb_put(skb, len);
  643. addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
  644. dma_unmap_single(&bp->pdev->dev, addr,
  645. bp->rx_buffer_size, DMA_FROM_DEVICE);
  646. skb->protocol = eth_type_trans(skb, bp->dev);
  647. skb_checksum_none_assert(skb);
  648. if (bp->dev->features & NETIF_F_RXCSUM &&
  649. !(bp->dev->flags & IFF_PROMISC) &&
  650. GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
  651. skb->ip_summed = CHECKSUM_UNNECESSARY;
  652. bp->stats.rx_packets++;
  653. bp->stats.rx_bytes += skb->len;
  654. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  655. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  656. skb->len, skb->csum);
  657. print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
  658. skb_mac_header(skb), 16, true);
  659. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
  660. skb->data, 32, true);
  661. #endif
  662. netif_receive_skb(skb);
  663. }
  664. gem_rx_refill(bp);
  665. return count;
  666. }
  667. static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
  668. unsigned int last_frag)
  669. {
  670. unsigned int len;
  671. unsigned int frag;
  672. unsigned int offset;
  673. struct sk_buff *skb;
  674. struct macb_dma_desc *desc;
  675. desc = macb_rx_desc(bp, last_frag);
  676. len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
  677. netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  678. macb_rx_ring_wrap(first_frag),
  679. macb_rx_ring_wrap(last_frag), len);
  680. /*
  681. * The ethernet header starts NET_IP_ALIGN bytes into the
  682. * first buffer. Since the header is 14 bytes, this makes the
  683. * payload word-aligned.
  684. *
  685. * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
  686. * the two padding bytes into the skb so that we avoid hitting
  687. * the slowpath in memcpy(), and pull them off afterwards.
  688. */
  689. skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
  690. if (!skb) {
  691. bp->stats.rx_dropped++;
  692. for (frag = first_frag; ; frag++) {
  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. return 1;
  701. }
  702. offset = 0;
  703. len += NET_IP_ALIGN;
  704. skb_checksum_none_assert(skb);
  705. skb_put(skb, len);
  706. for (frag = first_frag; ; frag++) {
  707. unsigned int frag_len = bp->rx_buffer_size;
  708. if (offset + frag_len > len) {
  709. BUG_ON(frag != last_frag);
  710. frag_len = len - offset;
  711. }
  712. skb_copy_to_linear_data_offset(skb, offset,
  713. macb_rx_buffer(bp, frag), frag_len);
  714. offset += bp->rx_buffer_size;
  715. desc = macb_rx_desc(bp, frag);
  716. desc->addr &= ~MACB_BIT(RX_USED);
  717. if (frag == last_frag)
  718. break;
  719. }
  720. /* Make descriptor updates visible to hardware */
  721. wmb();
  722. __skb_pull(skb, NET_IP_ALIGN);
  723. skb->protocol = eth_type_trans(skb, bp->dev);
  724. bp->stats.rx_packets++;
  725. bp->stats.rx_bytes += skb->len;
  726. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  727. skb->len, skb->csum);
  728. netif_receive_skb(skb);
  729. return 0;
  730. }
  731. static int macb_rx(struct macb *bp, int budget)
  732. {
  733. int received = 0;
  734. unsigned int tail;
  735. int first_frag = -1;
  736. for (tail = bp->rx_tail; budget > 0; tail++) {
  737. struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
  738. u32 addr, ctrl;
  739. /* Make hw descriptor updates visible to CPU */
  740. rmb();
  741. addr = desc->addr;
  742. ctrl = desc->ctrl;
  743. if (!(addr & MACB_BIT(RX_USED)))
  744. break;
  745. if (ctrl & MACB_BIT(RX_SOF)) {
  746. if (first_frag != -1)
  747. discard_partial_frame(bp, first_frag, tail);
  748. first_frag = tail;
  749. }
  750. if (ctrl & MACB_BIT(RX_EOF)) {
  751. int dropped;
  752. BUG_ON(first_frag == -1);
  753. dropped = macb_rx_frame(bp, first_frag, tail);
  754. first_frag = -1;
  755. if (!dropped) {
  756. received++;
  757. budget--;
  758. }
  759. }
  760. }
  761. if (first_frag != -1)
  762. bp->rx_tail = first_frag;
  763. else
  764. bp->rx_tail = tail;
  765. return received;
  766. }
  767. static int macb_poll(struct napi_struct *napi, int budget)
  768. {
  769. struct macb *bp = container_of(napi, struct macb, napi);
  770. int work_done;
  771. u32 status;
  772. status = macb_readl(bp, RSR);
  773. macb_writel(bp, RSR, status);
  774. work_done = 0;
  775. netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
  776. (unsigned long)status, budget);
  777. work_done = bp->macbgem_ops.mog_rx(bp, budget);
  778. if (work_done < budget) {
  779. napi_complete(napi);
  780. /* Packets received while interrupts were disabled */
  781. status = macb_readl(bp, RSR);
  782. if (status) {
  783. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  784. macb_writel(bp, ISR, MACB_BIT(RCOMP));
  785. napi_reschedule(napi);
  786. } else {
  787. macb_writel(bp, IER, MACB_RX_INT_FLAGS);
  788. }
  789. }
  790. /* TODO: Handle errors */
  791. return work_done;
  792. }
  793. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  794. {
  795. struct macb_queue *queue = dev_id;
  796. struct macb *bp = queue->bp;
  797. struct net_device *dev = bp->dev;
  798. u32 status;
  799. status = queue_readl(queue, ISR);
  800. if (unlikely(!status))
  801. return IRQ_NONE;
  802. spin_lock(&bp->lock);
  803. while (status) {
  804. /* close possible race with dev_close */
  805. if (unlikely(!netif_running(dev))) {
  806. queue_writel(queue, IDR, -1);
  807. break;
  808. }
  809. netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
  810. (unsigned int)(queue - bp->queues),
  811. (unsigned long)status);
  812. if (status & MACB_RX_INT_FLAGS) {
  813. /*
  814. * There's no point taking any more interrupts
  815. * until we have processed the buffers. The
  816. * scheduling call may fail if the poll routine
  817. * is already scheduled, so disable interrupts
  818. * now.
  819. */
  820. queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
  821. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  822. queue_writel(queue, ISR, MACB_BIT(RCOMP));
  823. if (napi_schedule_prep(&bp->napi)) {
  824. netdev_vdbg(bp->dev, "scheduling RX softirq\n");
  825. __napi_schedule(&bp->napi);
  826. }
  827. }
  828. if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
  829. queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
  830. schedule_work(&queue->tx_error_task);
  831. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  832. queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
  833. break;
  834. }
  835. if (status & MACB_BIT(TCOMP))
  836. macb_tx_interrupt(queue);
  837. /*
  838. * Link change detection isn't possible with RMII, so we'll
  839. * add that if/when we get our hands on a full-blown MII PHY.
  840. */
  841. if (status & MACB_BIT(ISR_ROVR)) {
  842. /* We missed at least one packet */
  843. if (macb_is_gem(bp))
  844. bp->hw_stats.gem.rx_overruns++;
  845. else
  846. bp->hw_stats.macb.rx_overruns++;
  847. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  848. queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
  849. }
  850. if (status & MACB_BIT(HRESP)) {
  851. /*
  852. * TODO: Reset the hardware, and maybe move the
  853. * netdev_err to a lower-priority context as well
  854. * (work queue?)
  855. */
  856. netdev_err(dev, "DMA bus error: HRESP not OK\n");
  857. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  858. queue_writel(queue, ISR, MACB_BIT(HRESP));
  859. }
  860. status = queue_readl(queue, ISR);
  861. }
  862. spin_unlock(&bp->lock);
  863. return IRQ_HANDLED;
  864. }
  865. #ifdef CONFIG_NET_POLL_CONTROLLER
  866. /*
  867. * Polling receive - used by netconsole and other diagnostic tools
  868. * to allow network i/o with interrupts disabled.
  869. */
  870. static void macb_poll_controller(struct net_device *dev)
  871. {
  872. struct macb *bp = netdev_priv(dev);
  873. struct macb_queue *queue;
  874. unsigned long flags;
  875. unsigned int q;
  876. local_irq_save(flags);
  877. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  878. macb_interrupt(dev->irq, queue);
  879. local_irq_restore(flags);
  880. }
  881. #endif
  882. static inline unsigned int macb_count_tx_descriptors(struct macb *bp,
  883. unsigned int len)
  884. {
  885. return (len + bp->max_tx_length - 1) / bp->max_tx_length;
  886. }
  887. static unsigned int macb_tx_map(struct macb *bp,
  888. struct macb_queue *queue,
  889. struct sk_buff *skb)
  890. {
  891. dma_addr_t mapping;
  892. unsigned int len, entry, i, tx_head = queue->tx_head;
  893. struct macb_tx_skb *tx_skb = NULL;
  894. struct macb_dma_desc *desc;
  895. unsigned int offset, size, count = 0;
  896. unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
  897. unsigned int eof = 1;
  898. u32 ctrl;
  899. /* First, map non-paged data */
  900. len = skb_headlen(skb);
  901. offset = 0;
  902. while (len) {
  903. size = min(len, bp->max_tx_length);
  904. entry = macb_tx_ring_wrap(tx_head);
  905. tx_skb = &queue->tx_skb[entry];
  906. mapping = dma_map_single(&bp->pdev->dev,
  907. skb->data + offset,
  908. size, DMA_TO_DEVICE);
  909. if (dma_mapping_error(&bp->pdev->dev, mapping))
  910. goto dma_error;
  911. /* Save info to properly release resources */
  912. tx_skb->skb = NULL;
  913. tx_skb->mapping = mapping;
  914. tx_skb->size = size;
  915. tx_skb->mapped_as_page = false;
  916. len -= size;
  917. offset += size;
  918. count++;
  919. tx_head++;
  920. }
  921. /* Then, map paged data from fragments */
  922. for (f = 0; f < nr_frags; f++) {
  923. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  924. len = skb_frag_size(frag);
  925. offset = 0;
  926. while (len) {
  927. size = min(len, bp->max_tx_length);
  928. entry = macb_tx_ring_wrap(tx_head);
  929. tx_skb = &queue->tx_skb[entry];
  930. mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
  931. offset, size, DMA_TO_DEVICE);
  932. if (dma_mapping_error(&bp->pdev->dev, mapping))
  933. goto dma_error;
  934. /* Save info to properly release resources */
  935. tx_skb->skb = NULL;
  936. tx_skb->mapping = mapping;
  937. tx_skb->size = size;
  938. tx_skb->mapped_as_page = true;
  939. len -= size;
  940. offset += size;
  941. count++;
  942. tx_head++;
  943. }
  944. }
  945. /* Should never happen */
  946. if (unlikely(tx_skb == NULL)) {
  947. netdev_err(bp->dev, "BUG! empty skb!\n");
  948. return 0;
  949. }
  950. /* This is the last buffer of the frame: save socket buffer */
  951. tx_skb->skb = skb;
  952. /* Update TX ring: update buffer descriptors in reverse order
  953. * to avoid race condition
  954. */
  955. /* Set 'TX_USED' bit in buffer descriptor at tx_head position
  956. * to set the end of TX queue
  957. */
  958. i = tx_head;
  959. entry = macb_tx_ring_wrap(i);
  960. ctrl = MACB_BIT(TX_USED);
  961. desc = &queue->tx_ring[entry];
  962. desc->ctrl = ctrl;
  963. do {
  964. i--;
  965. entry = macb_tx_ring_wrap(i);
  966. tx_skb = &queue->tx_skb[entry];
  967. desc = &queue->tx_ring[entry];
  968. ctrl = (u32)tx_skb->size;
  969. if (eof) {
  970. ctrl |= MACB_BIT(TX_LAST);
  971. eof = 0;
  972. }
  973. if (unlikely(entry == (TX_RING_SIZE - 1)))
  974. ctrl |= MACB_BIT(TX_WRAP);
  975. /* Set TX buffer descriptor */
  976. desc->addr = tx_skb->mapping;
  977. /* desc->addr must be visible to hardware before clearing
  978. * 'TX_USED' bit in desc->ctrl.
  979. */
  980. wmb();
  981. desc->ctrl = ctrl;
  982. } while (i != queue->tx_head);
  983. queue->tx_head = tx_head;
  984. return count;
  985. dma_error:
  986. netdev_err(bp->dev, "TX DMA map failed\n");
  987. for (i = queue->tx_head; i != tx_head; i++) {
  988. tx_skb = macb_tx_skb(queue, i);
  989. macb_tx_unmap(bp, tx_skb);
  990. }
  991. return 0;
  992. }
  993. static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  994. {
  995. u16 queue_index = skb_get_queue_mapping(skb);
  996. struct macb *bp = netdev_priv(dev);
  997. struct macb_queue *queue = &bp->queues[queue_index];
  998. unsigned long flags;
  999. unsigned int count, nr_frags, frag_size, f;
  1000. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  1001. netdev_vdbg(bp->dev,
  1002. "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
  1003. queue_index, skb->len, skb->head, skb->data,
  1004. skb_tail_pointer(skb), skb_end_pointer(skb));
  1005. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
  1006. skb->data, 16, true);
  1007. #endif
  1008. /* Count how many TX buffer descriptors are needed to send this
  1009. * socket buffer: skb fragments of jumbo frames may need to be
  1010. * splitted into many buffer descriptors.
  1011. */
  1012. count = macb_count_tx_descriptors(bp, skb_headlen(skb));
  1013. nr_frags = skb_shinfo(skb)->nr_frags;
  1014. for (f = 0; f < nr_frags; f++) {
  1015. frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
  1016. count += macb_count_tx_descriptors(bp, frag_size);
  1017. }
  1018. spin_lock_irqsave(&bp->lock, flags);
  1019. /* This is a hard error, log it. */
  1020. if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
  1021. netif_stop_subqueue(dev, queue_index);
  1022. spin_unlock_irqrestore(&bp->lock, flags);
  1023. netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
  1024. queue->tx_head, queue->tx_tail);
  1025. return NETDEV_TX_BUSY;
  1026. }
  1027. /* Map socket buffer for DMA transfer */
  1028. if (!macb_tx_map(bp, queue, skb)) {
  1029. dev_kfree_skb_any(skb);
  1030. goto unlock;
  1031. }
  1032. /* Make newly initialized descriptor visible to hardware */
  1033. wmb();
  1034. skb_tx_timestamp(skb);
  1035. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  1036. if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
  1037. netif_stop_subqueue(dev, queue_index);
  1038. unlock:
  1039. spin_unlock_irqrestore(&bp->lock, flags);
  1040. return NETDEV_TX_OK;
  1041. }
  1042. static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
  1043. {
  1044. if (!macb_is_gem(bp)) {
  1045. bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
  1046. } else {
  1047. bp->rx_buffer_size = size;
  1048. if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
  1049. netdev_dbg(bp->dev,
  1050. "RX buffer must be multiple of %d bytes, expanding\n",
  1051. RX_BUFFER_MULTIPLE);
  1052. bp->rx_buffer_size =
  1053. roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
  1054. }
  1055. }
  1056. netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
  1057. bp->dev->mtu, bp->rx_buffer_size);
  1058. }
  1059. static void gem_free_rx_buffers(struct macb *bp)
  1060. {
  1061. struct sk_buff *skb;
  1062. struct macb_dma_desc *desc;
  1063. dma_addr_t addr;
  1064. int i;
  1065. if (!bp->rx_skbuff)
  1066. return;
  1067. for (i = 0; i < RX_RING_SIZE; i++) {
  1068. skb = bp->rx_skbuff[i];
  1069. if (skb == NULL)
  1070. continue;
  1071. desc = &bp->rx_ring[i];
  1072. addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
  1073. dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
  1074. DMA_FROM_DEVICE);
  1075. dev_kfree_skb_any(skb);
  1076. skb = NULL;
  1077. }
  1078. kfree(bp->rx_skbuff);
  1079. bp->rx_skbuff = NULL;
  1080. }
  1081. static void macb_free_rx_buffers(struct macb *bp)
  1082. {
  1083. if (bp->rx_buffers) {
  1084. dma_free_coherent(&bp->pdev->dev,
  1085. RX_RING_SIZE * bp->rx_buffer_size,
  1086. bp->rx_buffers, bp->rx_buffers_dma);
  1087. bp->rx_buffers = NULL;
  1088. }
  1089. }
  1090. static void macb_free_consistent(struct macb *bp)
  1091. {
  1092. struct macb_queue *queue;
  1093. unsigned int q;
  1094. bp->macbgem_ops.mog_free_rx_buffers(bp);
  1095. if (bp->rx_ring) {
  1096. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
  1097. bp->rx_ring, bp->rx_ring_dma);
  1098. bp->rx_ring = NULL;
  1099. }
  1100. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1101. kfree(queue->tx_skb);
  1102. queue->tx_skb = NULL;
  1103. if (queue->tx_ring) {
  1104. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
  1105. queue->tx_ring, queue->tx_ring_dma);
  1106. queue->tx_ring = NULL;
  1107. }
  1108. }
  1109. }
  1110. static int gem_alloc_rx_buffers(struct macb *bp)
  1111. {
  1112. int size;
  1113. size = RX_RING_SIZE * sizeof(struct sk_buff *);
  1114. bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
  1115. if (!bp->rx_skbuff)
  1116. return -ENOMEM;
  1117. else
  1118. netdev_dbg(bp->dev,
  1119. "Allocated %d RX struct sk_buff entries at %p\n",
  1120. RX_RING_SIZE, bp->rx_skbuff);
  1121. return 0;
  1122. }
  1123. static int macb_alloc_rx_buffers(struct macb *bp)
  1124. {
  1125. int size;
  1126. size = RX_RING_SIZE * bp->rx_buffer_size;
  1127. bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  1128. &bp->rx_buffers_dma, GFP_KERNEL);
  1129. if (!bp->rx_buffers)
  1130. return -ENOMEM;
  1131. else
  1132. netdev_dbg(bp->dev,
  1133. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  1134. size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
  1135. return 0;
  1136. }
  1137. static int macb_alloc_consistent(struct macb *bp)
  1138. {
  1139. struct macb_queue *queue;
  1140. unsigned int q;
  1141. int size;
  1142. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1143. size = TX_RING_BYTES;
  1144. queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1145. &queue->tx_ring_dma,
  1146. GFP_KERNEL);
  1147. if (!queue->tx_ring)
  1148. goto out_err;
  1149. netdev_dbg(bp->dev,
  1150. "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
  1151. q, size, (unsigned long)queue->tx_ring_dma,
  1152. queue->tx_ring);
  1153. size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
  1154. queue->tx_skb = kmalloc(size, GFP_KERNEL);
  1155. if (!queue->tx_skb)
  1156. goto out_err;
  1157. }
  1158. size = RX_RING_BYTES;
  1159. bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1160. &bp->rx_ring_dma, GFP_KERNEL);
  1161. if (!bp->rx_ring)
  1162. goto out_err;
  1163. netdev_dbg(bp->dev,
  1164. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  1165. size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
  1166. if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
  1167. goto out_err;
  1168. return 0;
  1169. out_err:
  1170. macb_free_consistent(bp);
  1171. return -ENOMEM;
  1172. }
  1173. static void gem_init_rings(struct macb *bp)
  1174. {
  1175. struct macb_queue *queue;
  1176. unsigned int q;
  1177. int i;
  1178. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1179. for (i = 0; i < TX_RING_SIZE; i++) {
  1180. queue->tx_ring[i].addr = 0;
  1181. queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  1182. }
  1183. queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  1184. queue->tx_head = 0;
  1185. queue->tx_tail = 0;
  1186. }
  1187. bp->rx_tail = 0;
  1188. bp->rx_prepared_head = 0;
  1189. gem_rx_refill(bp);
  1190. }
  1191. static void macb_init_rings(struct macb *bp)
  1192. {
  1193. int i;
  1194. dma_addr_t addr;
  1195. addr = bp->rx_buffers_dma;
  1196. for (i = 0; i < RX_RING_SIZE; i++) {
  1197. bp->rx_ring[i].addr = addr;
  1198. bp->rx_ring[i].ctrl = 0;
  1199. addr += bp->rx_buffer_size;
  1200. }
  1201. bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
  1202. for (i = 0; i < TX_RING_SIZE; i++) {
  1203. bp->queues[0].tx_ring[i].addr = 0;
  1204. bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
  1205. bp->queues[0].tx_head = 0;
  1206. bp->queues[0].tx_tail = 0;
  1207. }
  1208. bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  1209. bp->rx_tail = 0;
  1210. }
  1211. static void macb_reset_hw(struct macb *bp)
  1212. {
  1213. struct macb_queue *queue;
  1214. unsigned int q;
  1215. /*
  1216. * Disable RX and TX (XXX: Should we halt the transmission
  1217. * more gracefully?)
  1218. */
  1219. macb_writel(bp, NCR, 0);
  1220. /* Clear the stats registers (XXX: Update stats first?) */
  1221. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  1222. /* Clear all status flags */
  1223. macb_writel(bp, TSR, -1);
  1224. macb_writel(bp, RSR, -1);
  1225. /* Disable all interrupts */
  1226. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1227. queue_writel(queue, IDR, -1);
  1228. queue_readl(queue, ISR);
  1229. }
  1230. }
  1231. static u32 gem_mdc_clk_div(struct macb *bp)
  1232. {
  1233. u32 config;
  1234. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  1235. if (pclk_hz <= 20000000)
  1236. config = GEM_BF(CLK, GEM_CLK_DIV8);
  1237. else if (pclk_hz <= 40000000)
  1238. config = GEM_BF(CLK, GEM_CLK_DIV16);
  1239. else if (pclk_hz <= 80000000)
  1240. config = GEM_BF(CLK, GEM_CLK_DIV32);
  1241. else if (pclk_hz <= 120000000)
  1242. config = GEM_BF(CLK, GEM_CLK_DIV48);
  1243. else if (pclk_hz <= 160000000)
  1244. config = GEM_BF(CLK, GEM_CLK_DIV64);
  1245. else
  1246. config = GEM_BF(CLK, GEM_CLK_DIV96);
  1247. return config;
  1248. }
  1249. static u32 macb_mdc_clk_div(struct macb *bp)
  1250. {
  1251. u32 config;
  1252. unsigned long pclk_hz;
  1253. if (macb_is_gem(bp))
  1254. return gem_mdc_clk_div(bp);
  1255. pclk_hz = clk_get_rate(bp->pclk);
  1256. if (pclk_hz <= 20000000)
  1257. config = MACB_BF(CLK, MACB_CLK_DIV8);
  1258. else if (pclk_hz <= 40000000)
  1259. config = MACB_BF(CLK, MACB_CLK_DIV16);
  1260. else if (pclk_hz <= 80000000)
  1261. config = MACB_BF(CLK, MACB_CLK_DIV32);
  1262. else
  1263. config = MACB_BF(CLK, MACB_CLK_DIV64);
  1264. return config;
  1265. }
  1266. /*
  1267. * Get the DMA bus width field of the network configuration register that we
  1268. * should program. We find the width from decoding the design configuration
  1269. * register to find the maximum supported data bus width.
  1270. */
  1271. static u32 macb_dbw(struct macb *bp)
  1272. {
  1273. if (!macb_is_gem(bp))
  1274. return 0;
  1275. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  1276. case 4:
  1277. return GEM_BF(DBW, GEM_DBW128);
  1278. case 2:
  1279. return GEM_BF(DBW, GEM_DBW64);
  1280. case 1:
  1281. default:
  1282. return GEM_BF(DBW, GEM_DBW32);
  1283. }
  1284. }
  1285. /*
  1286. * Configure the receive DMA engine
  1287. * - use the correct receive buffer size
  1288. * - set best burst length for DMA operations
  1289. * (if not supported by FIFO, it will fallback to default)
  1290. * - set both rx/tx packet buffers to full memory size
  1291. * These are configurable parameters for GEM.
  1292. */
  1293. static void macb_configure_dma(struct macb *bp)
  1294. {
  1295. u32 dmacfg;
  1296. u32 tmp, ncr;
  1297. if (macb_is_gem(bp)) {
  1298. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  1299. dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
  1300. if (bp->dma_burst_length)
  1301. dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
  1302. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  1303. dmacfg &= ~GEM_BIT(ENDIA_PKT);
  1304. /* Find the CPU endianness by using the loopback bit of net_ctrl
  1305. * register. save it first. When the CPU is in big endian we
  1306. * need to program swaped mode for management descriptor access.
  1307. */
  1308. ncr = macb_readl(bp, NCR);
  1309. __raw_writel(MACB_BIT(LLB), bp->regs + MACB_NCR);
  1310. tmp = __raw_readl(bp->regs + MACB_NCR);
  1311. if (tmp == MACB_BIT(LLB))
  1312. dmacfg &= ~GEM_BIT(ENDIA_DESC);
  1313. else
  1314. dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
  1315. /* Restore net_ctrl */
  1316. macb_writel(bp, NCR, ncr);
  1317. if (bp->dev->features & NETIF_F_HW_CSUM)
  1318. dmacfg |= GEM_BIT(TXCOEN);
  1319. else
  1320. dmacfg &= ~GEM_BIT(TXCOEN);
  1321. netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
  1322. dmacfg);
  1323. gem_writel(bp, DMACFG, dmacfg);
  1324. }
  1325. }
  1326. static void macb_init_hw(struct macb *bp)
  1327. {
  1328. struct macb_queue *queue;
  1329. unsigned int q;
  1330. u32 config;
  1331. macb_reset_hw(bp);
  1332. macb_set_hwaddr(bp);
  1333. config = macb_mdc_clk_div(bp);
  1334. config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
  1335. config |= MACB_BIT(PAE); /* PAuse Enable */
  1336. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  1337. config |= MACB_BIT(BIG); /* Receive oversized frames */
  1338. if (bp->dev->flags & IFF_PROMISC)
  1339. config |= MACB_BIT(CAF); /* Copy All Frames */
  1340. else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
  1341. config |= GEM_BIT(RXCOEN);
  1342. if (!(bp->dev->flags & IFF_BROADCAST))
  1343. config |= MACB_BIT(NBC); /* No BroadCast */
  1344. config |= macb_dbw(bp);
  1345. macb_writel(bp, NCFGR, config);
  1346. bp->speed = SPEED_10;
  1347. bp->duplex = DUPLEX_HALF;
  1348. macb_configure_dma(bp);
  1349. /* Initialize TX and RX buffers */
  1350. macb_writel(bp, RBQP, bp->rx_ring_dma);
  1351. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1352. queue_writel(queue, TBQP, queue->tx_ring_dma);
  1353. /* Enable interrupts */
  1354. queue_writel(queue, IER,
  1355. MACB_RX_INT_FLAGS |
  1356. MACB_TX_INT_FLAGS |
  1357. MACB_BIT(HRESP));
  1358. }
  1359. /* Enable TX and RX */
  1360. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  1361. }
  1362. /*
  1363. * The hash address register is 64 bits long and takes up two
  1364. * locations in the memory map. The least significant bits are stored
  1365. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  1366. *
  1367. * The unicast hash enable and the multicast hash enable bits in the
  1368. * network configuration register enable the reception of hash matched
  1369. * frames. The destination address is reduced to a 6 bit index into
  1370. * the 64 bit hash register using the following hash function. The
  1371. * hash function is an exclusive or of every sixth bit of the
  1372. * destination address.
  1373. *
  1374. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  1375. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  1376. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  1377. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  1378. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  1379. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  1380. *
  1381. * da[0] represents the least significant bit of the first byte
  1382. * received, that is, the multicast/unicast indicator, and da[47]
  1383. * represents the most significant bit of the last byte received. If
  1384. * the hash index, hi[n], points to a bit that is set in the hash
  1385. * register then the frame will be matched according to whether the
  1386. * frame is multicast or unicast. A multicast match will be signalled
  1387. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  1388. * index points to a bit set in the hash register. A unicast match
  1389. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  1390. * and the hash index points to a bit set in the hash register. To
  1391. * receive all multicast frames, the hash register should be set with
  1392. * all ones and the multicast hash enable bit should be set in the
  1393. * network configuration register.
  1394. */
  1395. static inline int hash_bit_value(int bitnr, __u8 *addr)
  1396. {
  1397. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  1398. return 1;
  1399. return 0;
  1400. }
  1401. /*
  1402. * Return the hash index value for the specified address.
  1403. */
  1404. static int hash_get_index(__u8 *addr)
  1405. {
  1406. int i, j, bitval;
  1407. int hash_index = 0;
  1408. for (j = 0; j < 6; j++) {
  1409. for (i = 0, bitval = 0; i < 8; i++)
  1410. bitval ^= hash_bit_value(i * 6 + j, addr);
  1411. hash_index |= (bitval << j);
  1412. }
  1413. return hash_index;
  1414. }
  1415. /*
  1416. * Add multicast addresses to the internal multicast-hash table.
  1417. */
  1418. static void macb_sethashtable(struct net_device *dev)
  1419. {
  1420. struct netdev_hw_addr *ha;
  1421. unsigned long mc_filter[2];
  1422. unsigned int bitnr;
  1423. struct macb *bp = netdev_priv(dev);
  1424. mc_filter[0] = mc_filter[1] = 0;
  1425. netdev_for_each_mc_addr(ha, dev) {
  1426. bitnr = hash_get_index(ha->addr);
  1427. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  1428. }
  1429. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  1430. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  1431. }
  1432. /*
  1433. * Enable/Disable promiscuous and multicast modes.
  1434. */
  1435. static void macb_set_rx_mode(struct net_device *dev)
  1436. {
  1437. unsigned long cfg;
  1438. struct macb *bp = netdev_priv(dev);
  1439. cfg = macb_readl(bp, NCFGR);
  1440. if (dev->flags & IFF_PROMISC) {
  1441. /* Enable promiscuous mode */
  1442. cfg |= MACB_BIT(CAF);
  1443. /* Disable RX checksum offload */
  1444. if (macb_is_gem(bp))
  1445. cfg &= ~GEM_BIT(RXCOEN);
  1446. } else {
  1447. /* Disable promiscuous mode */
  1448. cfg &= ~MACB_BIT(CAF);
  1449. /* Enable RX checksum offload only if requested */
  1450. if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
  1451. cfg |= GEM_BIT(RXCOEN);
  1452. }
  1453. if (dev->flags & IFF_ALLMULTI) {
  1454. /* Enable all multicast mode */
  1455. macb_or_gem_writel(bp, HRB, -1);
  1456. macb_or_gem_writel(bp, HRT, -1);
  1457. cfg |= MACB_BIT(NCFGR_MTI);
  1458. } else if (!netdev_mc_empty(dev)) {
  1459. /* Enable specific multicasts */
  1460. macb_sethashtable(dev);
  1461. cfg |= MACB_BIT(NCFGR_MTI);
  1462. } else if (dev->flags & (~IFF_ALLMULTI)) {
  1463. /* Disable all multicast mode */
  1464. macb_or_gem_writel(bp, HRB, 0);
  1465. macb_or_gem_writel(bp, HRT, 0);
  1466. cfg &= ~MACB_BIT(NCFGR_MTI);
  1467. }
  1468. macb_writel(bp, NCFGR, cfg);
  1469. }
  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. static 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. static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1634. {
  1635. struct macb *bp = netdev_priv(dev);
  1636. struct phy_device *phydev = bp->phy_dev;
  1637. if (!phydev)
  1638. return -ENODEV;
  1639. return phy_ethtool_gset(phydev, cmd);
  1640. }
  1641. static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1642. {
  1643. struct macb *bp = netdev_priv(dev);
  1644. struct phy_device *phydev = bp->phy_dev;
  1645. if (!phydev)
  1646. return -ENODEV;
  1647. return phy_ethtool_sset(phydev, cmd);
  1648. }
  1649. static int macb_get_regs_len(struct net_device *netdev)
  1650. {
  1651. return MACB_GREGS_NBR * sizeof(u32);
  1652. }
  1653. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1654. void *p)
  1655. {
  1656. struct macb *bp = netdev_priv(dev);
  1657. unsigned int tail, head;
  1658. u32 *regs_buff = p;
  1659. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  1660. | MACB_GREGS_VERSION;
  1661. tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
  1662. head = macb_tx_ring_wrap(bp->queues[0].tx_head);
  1663. regs_buff[0] = macb_readl(bp, NCR);
  1664. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  1665. regs_buff[2] = macb_readl(bp, NSR);
  1666. regs_buff[3] = macb_readl(bp, TSR);
  1667. regs_buff[4] = macb_readl(bp, RBQP);
  1668. regs_buff[5] = macb_readl(bp, TBQP);
  1669. regs_buff[6] = macb_readl(bp, RSR);
  1670. regs_buff[7] = macb_readl(bp, IMR);
  1671. regs_buff[8] = tail;
  1672. regs_buff[9] = head;
  1673. regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
  1674. regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
  1675. if (macb_is_gem(bp)) {
  1676. regs_buff[12] = gem_readl(bp, USRIO);
  1677. regs_buff[13] = gem_readl(bp, DMACFG);
  1678. }
  1679. }
  1680. static const struct ethtool_ops macb_ethtool_ops = {
  1681. .get_settings = macb_get_settings,
  1682. .set_settings = macb_set_settings,
  1683. .get_regs_len = macb_get_regs_len,
  1684. .get_regs = macb_get_regs,
  1685. .get_link = ethtool_op_get_link,
  1686. .get_ts_info = ethtool_op_get_ts_info,
  1687. };
  1688. static const struct ethtool_ops gem_ethtool_ops = {
  1689. .get_settings = macb_get_settings,
  1690. .set_settings = macb_set_settings,
  1691. .get_regs_len = macb_get_regs_len,
  1692. .get_regs = macb_get_regs,
  1693. .get_link = ethtool_op_get_link,
  1694. .get_ts_info = ethtool_op_get_ts_info,
  1695. .get_ethtool_stats = gem_get_ethtool_stats,
  1696. .get_strings = gem_get_ethtool_strings,
  1697. .get_sset_count = gem_get_sset_count,
  1698. };
  1699. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1700. {
  1701. struct macb *bp = netdev_priv(dev);
  1702. struct phy_device *phydev = bp->phy_dev;
  1703. if (!netif_running(dev))
  1704. return -EINVAL;
  1705. if (!phydev)
  1706. return -ENODEV;
  1707. return phy_mii_ioctl(phydev, rq, cmd);
  1708. }
  1709. static int macb_set_features(struct net_device *netdev,
  1710. netdev_features_t features)
  1711. {
  1712. struct macb *bp = netdev_priv(netdev);
  1713. netdev_features_t changed = features ^ netdev->features;
  1714. /* TX checksum offload */
  1715. if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
  1716. u32 dmacfg;
  1717. dmacfg = gem_readl(bp, DMACFG);
  1718. if (features & NETIF_F_HW_CSUM)
  1719. dmacfg |= GEM_BIT(TXCOEN);
  1720. else
  1721. dmacfg &= ~GEM_BIT(TXCOEN);
  1722. gem_writel(bp, DMACFG, dmacfg);
  1723. }
  1724. /* RX checksum offload */
  1725. if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
  1726. u32 netcfg;
  1727. netcfg = gem_readl(bp, NCFGR);
  1728. if (features & NETIF_F_RXCSUM &&
  1729. !(netdev->flags & IFF_PROMISC))
  1730. netcfg |= GEM_BIT(RXCOEN);
  1731. else
  1732. netcfg &= ~GEM_BIT(RXCOEN);
  1733. gem_writel(bp, NCFGR, netcfg);
  1734. }
  1735. return 0;
  1736. }
  1737. static const struct net_device_ops macb_netdev_ops = {
  1738. .ndo_open = macb_open,
  1739. .ndo_stop = macb_close,
  1740. .ndo_start_xmit = macb_start_xmit,
  1741. .ndo_set_rx_mode = macb_set_rx_mode,
  1742. .ndo_get_stats = macb_get_stats,
  1743. .ndo_do_ioctl = macb_ioctl,
  1744. .ndo_validate_addr = eth_validate_addr,
  1745. .ndo_change_mtu = eth_change_mtu,
  1746. .ndo_set_mac_address = eth_mac_addr,
  1747. #ifdef CONFIG_NET_POLL_CONTROLLER
  1748. .ndo_poll_controller = macb_poll_controller,
  1749. #endif
  1750. .ndo_set_features = macb_set_features,
  1751. };
  1752. /*
  1753. * Configure peripheral capacities according to device tree
  1754. * and integration options used
  1755. */
  1756. static void macb_configure_caps(struct macb *bp)
  1757. {
  1758. u32 dcfg;
  1759. if (MACB_BFEXT(IDNUM, macb_readl(bp, MID)) == 0x2)
  1760. bp->caps |= MACB_CAPS_MACB_IS_GEM;
  1761. if (macb_is_gem(bp)) {
  1762. dcfg = gem_readl(bp, DCFG1);
  1763. if (GEM_BFEXT(IRQCOR, dcfg) == 0)
  1764. bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
  1765. dcfg = gem_readl(bp, DCFG2);
  1766. if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
  1767. bp->caps |= MACB_CAPS_FIFO_MODE;
  1768. }
  1769. netdev_dbg(bp->dev, "Cadence caps 0x%08x\n", bp->caps);
  1770. }
  1771. static void macb_probe_queues(void __iomem *mem,
  1772. unsigned int *queue_mask,
  1773. unsigned int *num_queues)
  1774. {
  1775. unsigned int hw_q;
  1776. u32 mid;
  1777. *queue_mask = 0x1;
  1778. *num_queues = 1;
  1779. /* is it macb or gem ? */
  1780. mid = readl_relaxed(mem + MACB_MID);
  1781. if (MACB_BFEXT(IDNUM, mid) < 0x2)
  1782. return;
  1783. /* bit 0 is never set but queue 0 always exists */
  1784. *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
  1785. *queue_mask |= 0x1;
  1786. for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
  1787. if (*queue_mask & (1 << hw_q))
  1788. (*num_queues)++;
  1789. }
  1790. static int macb_init(struct platform_device *pdev)
  1791. {
  1792. struct net_device *dev = platform_get_drvdata(pdev);
  1793. unsigned int hw_q, queue_mask, q, num_queues;
  1794. struct macb *bp = netdev_priv(dev);
  1795. struct macb_queue *queue;
  1796. int err;
  1797. u32 val;
  1798. bp->pclk = devm_clk_get(&pdev->dev, "pclk");
  1799. if (IS_ERR(bp->pclk)) {
  1800. err = PTR_ERR(bp->pclk);
  1801. dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
  1802. return err;
  1803. }
  1804. bp->hclk = devm_clk_get(&pdev->dev, "hclk");
  1805. if (IS_ERR(bp->hclk)) {
  1806. err = PTR_ERR(bp->hclk);
  1807. dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
  1808. return err;
  1809. }
  1810. bp->tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
  1811. if (IS_ERR(bp->tx_clk))
  1812. bp->tx_clk = NULL;
  1813. err = clk_prepare_enable(bp->pclk);
  1814. if (err) {
  1815. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  1816. return err;
  1817. }
  1818. err = clk_prepare_enable(bp->hclk);
  1819. if (err) {
  1820. dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
  1821. goto err_disable_pclk;
  1822. }
  1823. err = clk_prepare_enable(bp->tx_clk);
  1824. if (err) {
  1825. dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
  1826. goto err_disable_hclk;
  1827. }
  1828. /* set the queue register mapping once for all: queue0 has a special
  1829. * register mapping but we don't want to test the queue index then
  1830. * compute the corresponding register offset at run time.
  1831. */
  1832. macb_probe_queues(bp->regs, &queue_mask, &num_queues);
  1833. for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
  1834. if (!(queue_mask & (1 << hw_q)))
  1835. continue;
  1836. queue = &bp->queues[q];
  1837. queue->bp = bp;
  1838. if (hw_q) {
  1839. queue->ISR = GEM_ISR(hw_q - 1);
  1840. queue->IER = GEM_IER(hw_q - 1);
  1841. queue->IDR = GEM_IDR(hw_q - 1);
  1842. queue->IMR = GEM_IMR(hw_q - 1);
  1843. queue->TBQP = GEM_TBQP(hw_q - 1);
  1844. } else {
  1845. /* queue0 uses legacy registers */
  1846. queue->ISR = MACB_ISR;
  1847. queue->IER = MACB_IER;
  1848. queue->IDR = MACB_IDR;
  1849. queue->IMR = MACB_IMR;
  1850. queue->TBQP = MACB_TBQP;
  1851. }
  1852. /* get irq: here we use the linux queue index, not the hardware
  1853. * queue index. the queue irq definitions in the device tree
  1854. * must remove the optional gaps that could exist in the
  1855. * hardware queue mask.
  1856. */
  1857. queue->irq = platform_get_irq(pdev, q);
  1858. err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
  1859. IRQF_SHARED, dev->name, queue);
  1860. if (err) {
  1861. dev_err(&pdev->dev,
  1862. "Unable to request IRQ %d (error %d)\n",
  1863. queue->irq, err);
  1864. goto err_disable_tx_clk;
  1865. }
  1866. INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
  1867. q++;
  1868. }
  1869. dev->netdev_ops = &macb_netdev_ops;
  1870. netif_napi_add(dev, &bp->napi, macb_poll, 64);
  1871. /* setup appropriated routines according to adapter type */
  1872. if (macb_is_gem(bp)) {
  1873. bp->max_tx_length = GEM_MAX_TX_LEN;
  1874. bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
  1875. bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
  1876. bp->macbgem_ops.mog_init_rings = gem_init_rings;
  1877. bp->macbgem_ops.mog_rx = gem_rx;
  1878. dev->ethtool_ops = &gem_ethtool_ops;
  1879. } else {
  1880. bp->max_tx_length = MACB_MAX_TX_LEN;
  1881. bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
  1882. bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
  1883. bp->macbgem_ops.mog_init_rings = macb_init_rings;
  1884. bp->macbgem_ops.mog_rx = macb_rx;
  1885. dev->ethtool_ops = &macb_ethtool_ops;
  1886. }
  1887. /* Set features */
  1888. dev->hw_features = NETIF_F_SG;
  1889. /* Checksum offload is only available on gem with packet buffer */
  1890. if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
  1891. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1892. if (bp->caps & MACB_CAPS_SG_DISABLED)
  1893. dev->hw_features &= ~NETIF_F_SG;
  1894. dev->features = dev->hw_features;
  1895. val = 0;
  1896. if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
  1897. val = GEM_BIT(RGMII);
  1898. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
  1899. (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
  1900. val = MACB_BIT(RMII);
  1901. else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
  1902. val = MACB_BIT(MII);
  1903. if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
  1904. val |= MACB_BIT(CLKEN);
  1905. macb_or_gem_writel(bp, USRIO, val);
  1906. /* setup capacities */
  1907. macb_configure_caps(bp);
  1908. /* Set MII management clock divider */
  1909. val = macb_mdc_clk_div(bp);
  1910. val |= macb_dbw(bp);
  1911. macb_writel(bp, NCFGR, val);
  1912. return 0;
  1913. err_disable_tx_clk:
  1914. clk_disable_unprepare(bp->tx_clk);
  1915. err_disable_hclk:
  1916. clk_disable_unprepare(bp->hclk);
  1917. err_disable_pclk:
  1918. clk_disable_unprepare(bp->pclk);
  1919. return err;
  1920. }
  1921. #if defined(CONFIG_OF)
  1922. /* 1518 rounded up */
  1923. #define AT91ETHER_MAX_RBUFF_SZ 0x600
  1924. /* max number of receive buffers */
  1925. #define AT91ETHER_MAX_RX_DESCR 9
  1926. /* Initialize and start the Receiver and Transmit subsystems */
  1927. static int at91ether_start(struct net_device *dev)
  1928. {
  1929. struct macb *lp = netdev_priv(dev);
  1930. dma_addr_t addr;
  1931. u32 ctl;
  1932. int i;
  1933. lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
  1934. (AT91ETHER_MAX_RX_DESCR *
  1935. sizeof(struct macb_dma_desc)),
  1936. &lp->rx_ring_dma, GFP_KERNEL);
  1937. if (!lp->rx_ring)
  1938. return -ENOMEM;
  1939. lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
  1940. AT91ETHER_MAX_RX_DESCR *
  1941. AT91ETHER_MAX_RBUFF_SZ,
  1942. &lp->rx_buffers_dma, GFP_KERNEL);
  1943. if (!lp->rx_buffers) {
  1944. dma_free_coherent(&lp->pdev->dev,
  1945. AT91ETHER_MAX_RX_DESCR *
  1946. sizeof(struct macb_dma_desc),
  1947. lp->rx_ring, lp->rx_ring_dma);
  1948. lp->rx_ring = NULL;
  1949. return -ENOMEM;
  1950. }
  1951. addr = lp->rx_buffers_dma;
  1952. for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
  1953. lp->rx_ring[i].addr = addr;
  1954. lp->rx_ring[i].ctrl = 0;
  1955. addr += AT91ETHER_MAX_RBUFF_SZ;
  1956. }
  1957. /* Set the Wrap bit on the last descriptor */
  1958. lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
  1959. /* Reset buffer index */
  1960. lp->rx_tail = 0;
  1961. /* Program address of descriptor list in Rx Buffer Queue register */
  1962. macb_writel(lp, RBQP, lp->rx_ring_dma);
  1963. /* Enable Receive and Transmit */
  1964. ctl = macb_readl(lp, NCR);
  1965. macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
  1966. return 0;
  1967. }
  1968. /* Open the ethernet interface */
  1969. static int at91ether_open(struct net_device *dev)
  1970. {
  1971. struct macb *lp = netdev_priv(dev);
  1972. u32 ctl;
  1973. int ret;
  1974. /* Clear internal statistics */
  1975. ctl = macb_readl(lp, NCR);
  1976. macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
  1977. macb_set_hwaddr(lp);
  1978. ret = at91ether_start(dev);
  1979. if (ret)
  1980. return ret;
  1981. /* Enable MAC interrupts */
  1982. macb_writel(lp, IER, MACB_BIT(RCOMP) |
  1983. MACB_BIT(RXUBR) |
  1984. MACB_BIT(ISR_TUND) |
  1985. MACB_BIT(ISR_RLE) |
  1986. MACB_BIT(TCOMP) |
  1987. MACB_BIT(ISR_ROVR) |
  1988. MACB_BIT(HRESP));
  1989. /* schedule a link state check */
  1990. phy_start(lp->phy_dev);
  1991. netif_start_queue(dev);
  1992. return 0;
  1993. }
  1994. /* Close the interface */
  1995. static int at91ether_close(struct net_device *dev)
  1996. {
  1997. struct macb *lp = netdev_priv(dev);
  1998. u32 ctl;
  1999. /* Disable Receiver and Transmitter */
  2000. ctl = macb_readl(lp, NCR);
  2001. macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
  2002. /* Disable MAC interrupts */
  2003. macb_writel(lp, IDR, MACB_BIT(RCOMP) |
  2004. MACB_BIT(RXUBR) |
  2005. MACB_BIT(ISR_TUND) |
  2006. MACB_BIT(ISR_RLE) |
  2007. MACB_BIT(TCOMP) |
  2008. MACB_BIT(ISR_ROVR) |
  2009. MACB_BIT(HRESP));
  2010. netif_stop_queue(dev);
  2011. dma_free_coherent(&lp->pdev->dev,
  2012. AT91ETHER_MAX_RX_DESCR *
  2013. sizeof(struct macb_dma_desc),
  2014. lp->rx_ring, lp->rx_ring_dma);
  2015. lp->rx_ring = NULL;
  2016. dma_free_coherent(&lp->pdev->dev,
  2017. AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
  2018. lp->rx_buffers, lp->rx_buffers_dma);
  2019. lp->rx_buffers = NULL;
  2020. return 0;
  2021. }
  2022. /* Transmit packet */
  2023. static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2024. {
  2025. struct macb *lp = netdev_priv(dev);
  2026. if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
  2027. netif_stop_queue(dev);
  2028. /* Store packet information (to free when Tx completed) */
  2029. lp->skb = skb;
  2030. lp->skb_length = skb->len;
  2031. lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
  2032. DMA_TO_DEVICE);
  2033. /* Set address of the data in the Transmit Address register */
  2034. macb_writel(lp, TAR, lp->skb_physaddr);
  2035. /* Set length of the packet in the Transmit Control register */
  2036. macb_writel(lp, TCR, skb->len);
  2037. } else {
  2038. netdev_err(dev, "%s called, but device is busy!\n", __func__);
  2039. return NETDEV_TX_BUSY;
  2040. }
  2041. return NETDEV_TX_OK;
  2042. }
  2043. /* Extract received frame from buffer descriptors and sent to upper layers.
  2044. * (Called from interrupt context)
  2045. */
  2046. static void at91ether_rx(struct net_device *dev)
  2047. {
  2048. struct macb *lp = netdev_priv(dev);
  2049. unsigned char *p_recv;
  2050. struct sk_buff *skb;
  2051. unsigned int pktlen;
  2052. while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
  2053. p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
  2054. pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
  2055. skb = netdev_alloc_skb(dev, pktlen + 2);
  2056. if (skb) {
  2057. skb_reserve(skb, 2);
  2058. memcpy(skb_put(skb, pktlen), p_recv, pktlen);
  2059. skb->protocol = eth_type_trans(skb, dev);
  2060. lp->stats.rx_packets++;
  2061. lp->stats.rx_bytes += pktlen;
  2062. netif_rx(skb);
  2063. } else {
  2064. lp->stats.rx_dropped++;
  2065. }
  2066. if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
  2067. lp->stats.multicast++;
  2068. /* reset ownership bit */
  2069. lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
  2070. /* wrap after last buffer */
  2071. if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
  2072. lp->rx_tail = 0;
  2073. else
  2074. lp->rx_tail++;
  2075. }
  2076. }
  2077. /* MAC interrupt handler */
  2078. static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
  2079. {
  2080. struct net_device *dev = dev_id;
  2081. struct macb *lp = netdev_priv(dev);
  2082. u32 intstatus, ctl;
  2083. /* MAC Interrupt Status register indicates what interrupts are pending.
  2084. * It is automatically cleared once read.
  2085. */
  2086. intstatus = macb_readl(lp, ISR);
  2087. /* Receive complete */
  2088. if (intstatus & MACB_BIT(RCOMP))
  2089. at91ether_rx(dev);
  2090. /* Transmit complete */
  2091. if (intstatus & MACB_BIT(TCOMP)) {
  2092. /* The TCOM bit is set even if the transmission failed */
  2093. if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
  2094. lp->stats.tx_errors++;
  2095. if (lp->skb) {
  2096. dev_kfree_skb_irq(lp->skb);
  2097. lp->skb = NULL;
  2098. dma_unmap_single(NULL, lp->skb_physaddr,
  2099. lp->skb_length, DMA_TO_DEVICE);
  2100. lp->stats.tx_packets++;
  2101. lp->stats.tx_bytes += lp->skb_length;
  2102. }
  2103. netif_wake_queue(dev);
  2104. }
  2105. /* Work-around for EMAC Errata section 41.3.1 */
  2106. if (intstatus & MACB_BIT(RXUBR)) {
  2107. ctl = macb_readl(lp, NCR);
  2108. macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
  2109. macb_writel(lp, NCR, ctl | MACB_BIT(RE));
  2110. }
  2111. if (intstatus & MACB_BIT(ISR_ROVR))
  2112. netdev_err(dev, "ROVR error\n");
  2113. return IRQ_HANDLED;
  2114. }
  2115. #ifdef CONFIG_NET_POLL_CONTROLLER
  2116. static void at91ether_poll_controller(struct net_device *dev)
  2117. {
  2118. unsigned long flags;
  2119. local_irq_save(flags);
  2120. at91ether_interrupt(dev->irq, dev);
  2121. local_irq_restore(flags);
  2122. }
  2123. #endif
  2124. static const struct net_device_ops at91ether_netdev_ops = {
  2125. .ndo_open = at91ether_open,
  2126. .ndo_stop = at91ether_close,
  2127. .ndo_start_xmit = at91ether_start_xmit,
  2128. .ndo_get_stats = macb_get_stats,
  2129. .ndo_set_rx_mode = macb_set_rx_mode,
  2130. .ndo_set_mac_address = eth_mac_addr,
  2131. .ndo_do_ioctl = macb_ioctl,
  2132. .ndo_validate_addr = eth_validate_addr,
  2133. .ndo_change_mtu = eth_change_mtu,
  2134. #ifdef CONFIG_NET_POLL_CONTROLLER
  2135. .ndo_poll_controller = at91ether_poll_controller,
  2136. #endif
  2137. };
  2138. static int at91ether_init(struct platform_device *pdev)
  2139. {
  2140. struct net_device *dev = platform_get_drvdata(pdev);
  2141. struct macb *bp = netdev_priv(dev);
  2142. int err;
  2143. u32 reg;
  2144. bp->pclk = devm_clk_get(&pdev->dev, "ether_clk");
  2145. if (IS_ERR(bp->pclk))
  2146. return PTR_ERR(bp->pclk);
  2147. err = clk_prepare_enable(bp->pclk);
  2148. if (err) {
  2149. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  2150. return err;
  2151. }
  2152. dev->netdev_ops = &at91ether_netdev_ops;
  2153. dev->ethtool_ops = &macb_ethtool_ops;
  2154. err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
  2155. 0, dev->name, dev);
  2156. if (err)
  2157. goto err_disable_clk;
  2158. macb_writel(bp, NCR, 0);
  2159. reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
  2160. if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
  2161. reg |= MACB_BIT(RM9200_RMII);
  2162. macb_writel(bp, NCFGR, reg);
  2163. return 0;
  2164. err_disable_clk:
  2165. clk_disable_unprepare(bp->pclk);
  2166. return err;
  2167. }
  2168. static const struct macb_config at91sam9260_config = {
  2169. .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII,
  2170. .init = macb_init,
  2171. };
  2172. static const struct macb_config pc302gem_config = {
  2173. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  2174. .dma_burst_length = 16,
  2175. .init = macb_init,
  2176. };
  2177. static const struct macb_config sama5d3_config = {
  2178. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  2179. .dma_burst_length = 16,
  2180. .init = macb_init,
  2181. };
  2182. static const struct macb_config sama5d4_config = {
  2183. .caps = 0,
  2184. .dma_burst_length = 4,
  2185. .init = macb_init,
  2186. };
  2187. static const struct macb_config emac_config = {
  2188. .init = at91ether_init,
  2189. };
  2190. static const struct of_device_id macb_dt_ids[] = {
  2191. { .compatible = "cdns,at32ap7000-macb" },
  2192. { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
  2193. { .compatible = "cdns,macb" },
  2194. { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
  2195. { .compatible = "cdns,gem", .data = &pc302gem_config },
  2196. { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
  2197. { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
  2198. { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
  2199. { .compatible = "cdns,emac", .data = &emac_config },
  2200. { /* sentinel */ }
  2201. };
  2202. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  2203. #endif /* CONFIG_OF */
  2204. static int macb_probe(struct platform_device *pdev)
  2205. {
  2206. int (*init)(struct platform_device *) = macb_init;
  2207. struct device_node *np = pdev->dev.of_node;
  2208. const struct macb_config *macb_config = NULL;
  2209. unsigned int queue_mask, num_queues;
  2210. struct macb_platform_data *pdata;
  2211. struct phy_device *phydev;
  2212. struct net_device *dev;
  2213. struct resource *regs;
  2214. void __iomem *mem;
  2215. const char *mac;
  2216. struct macb *bp;
  2217. int err;
  2218. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2219. mem = devm_ioremap_resource(&pdev->dev, regs);
  2220. if (IS_ERR(mem))
  2221. return PTR_ERR(mem);
  2222. macb_probe_queues(mem, &queue_mask, &num_queues);
  2223. dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
  2224. if (!dev)
  2225. return -ENOMEM;
  2226. dev->base_addr = regs->start;
  2227. SET_NETDEV_DEV(dev, &pdev->dev);
  2228. bp = netdev_priv(dev);
  2229. bp->pdev = pdev;
  2230. bp->dev = dev;
  2231. bp->regs = mem;
  2232. bp->num_queues = num_queues;
  2233. spin_lock_init(&bp->lock);
  2234. platform_set_drvdata(pdev, dev);
  2235. dev->irq = platform_get_irq(pdev, 0);
  2236. if (dev->irq < 0)
  2237. return dev->irq;
  2238. mac = of_get_mac_address(np);
  2239. if (mac)
  2240. memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
  2241. else
  2242. macb_get_hwaddr(bp);
  2243. err = of_get_phy_mode(np);
  2244. if (err < 0) {
  2245. pdata = dev_get_platdata(&pdev->dev);
  2246. if (pdata && pdata->is_rmii)
  2247. bp->phy_interface = PHY_INTERFACE_MODE_RMII;
  2248. else
  2249. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  2250. } else {
  2251. bp->phy_interface = err;
  2252. }
  2253. if (np) {
  2254. const struct of_device_id *match;
  2255. match = of_match_node(macb_dt_ids, np);
  2256. if (match)
  2257. macb_config = match->data;
  2258. }
  2259. if (macb_config) {
  2260. bp->caps = macb_config->caps;
  2261. bp->dma_burst_length = macb_config->dma_burst_length;
  2262. init = macb_config->init;
  2263. }
  2264. /* IP specific init */
  2265. err = init(pdev);
  2266. if (err)
  2267. goto err_out_free_netdev;
  2268. err = register_netdev(dev);
  2269. if (err) {
  2270. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  2271. goto err_disable_clocks;
  2272. }
  2273. err = macb_mii_init(bp);
  2274. if (err)
  2275. goto err_out_unregister_netdev;
  2276. netif_carrier_off(dev);
  2277. netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
  2278. macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
  2279. dev->base_addr, dev->irq, dev->dev_addr);
  2280. phydev = bp->phy_dev;
  2281. netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  2282. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  2283. return 0;
  2284. err_out_unregister_netdev:
  2285. unregister_netdev(dev);
  2286. err_disable_clocks:
  2287. clk_disable_unprepare(bp->tx_clk);
  2288. clk_disable_unprepare(bp->hclk);
  2289. clk_disable_unprepare(bp->pclk);
  2290. err_out_free_netdev:
  2291. free_netdev(dev);
  2292. return err;
  2293. }
  2294. static int macb_remove(struct platform_device *pdev)
  2295. {
  2296. struct net_device *dev;
  2297. struct macb *bp;
  2298. dev = platform_get_drvdata(pdev);
  2299. if (dev) {
  2300. bp = netdev_priv(dev);
  2301. if (bp->phy_dev)
  2302. phy_disconnect(bp->phy_dev);
  2303. mdiobus_unregister(bp->mii_bus);
  2304. kfree(bp->mii_bus->irq);
  2305. mdiobus_free(bp->mii_bus);
  2306. unregister_netdev(dev);
  2307. clk_disable_unprepare(bp->tx_clk);
  2308. clk_disable_unprepare(bp->hclk);
  2309. clk_disable_unprepare(bp->pclk);
  2310. free_netdev(dev);
  2311. }
  2312. return 0;
  2313. }
  2314. static int __maybe_unused macb_suspend(struct device *dev)
  2315. {
  2316. struct platform_device *pdev = to_platform_device(dev);
  2317. struct net_device *netdev = platform_get_drvdata(pdev);
  2318. struct macb *bp = netdev_priv(netdev);
  2319. netif_carrier_off(netdev);
  2320. netif_device_detach(netdev);
  2321. clk_disable_unprepare(bp->tx_clk);
  2322. clk_disable_unprepare(bp->hclk);
  2323. clk_disable_unprepare(bp->pclk);
  2324. return 0;
  2325. }
  2326. static int __maybe_unused macb_resume(struct device *dev)
  2327. {
  2328. struct platform_device *pdev = to_platform_device(dev);
  2329. struct net_device *netdev = platform_get_drvdata(pdev);
  2330. struct macb *bp = netdev_priv(netdev);
  2331. clk_prepare_enable(bp->pclk);
  2332. clk_prepare_enable(bp->hclk);
  2333. clk_prepare_enable(bp->tx_clk);
  2334. netif_device_attach(netdev);
  2335. return 0;
  2336. }
  2337. static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
  2338. static struct platform_driver macb_driver = {
  2339. .probe = macb_probe,
  2340. .remove = macb_remove,
  2341. .driver = {
  2342. .name = "macb",
  2343. .of_match_table = of_match_ptr(macb_dt_ids),
  2344. .pm = &macb_pm_ops,
  2345. },
  2346. };
  2347. module_platform_driver(macb_driver);
  2348. MODULE_LICENSE("GPL");
  2349. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  2350. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  2351. MODULE_ALIAS("platform:macb");