macb.c 88 KB

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