macb_main.c 107 KB

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