macb_main.c 109 KB

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