macb.c 77 KB

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