fec_main.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
  4. * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
  5. *
  6. * Right now, I am very wasteful with the buffers. I allocate memory
  7. * pages and then divide them into 2K frame buffers. This way I know I
  8. * have buffers large enough to hold one frame within one buffer descriptor.
  9. * Once I get this working, I will use 64 or 128 byte CPM buffers, which
  10. * will be much more memory efficient and will easily handle lots of
  11. * small packets.
  12. *
  13. * Much better multiple PHY support by Magnus Damm.
  14. * Copyright (c) 2000 Ericsson Radio Systems AB.
  15. *
  16. * Support for FEC controller of ColdFire processors.
  17. * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
  18. *
  19. * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
  20. * Copyright (c) 2004-2006 Macq Electronique SA.
  21. *
  22. * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/string.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/errno.h>
  30. #include <linux/ioport.h>
  31. #include <linux/slab.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/delay.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/in.h>
  38. #include <linux/ip.h>
  39. #include <net/ip.h>
  40. #include <net/tso.h>
  41. #include <linux/tcp.h>
  42. #include <linux/udp.h>
  43. #include <linux/icmp.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/workqueue.h>
  46. #include <linux/bitops.h>
  47. #include <linux/io.h>
  48. #include <linux/irq.h>
  49. #include <linux/clk.h>
  50. #include <linux/crc32.h>
  51. #include <linux/platform_device.h>
  52. #include <linux/mdio.h>
  53. #include <linux/phy.h>
  54. #include <linux/fec.h>
  55. #include <linux/of.h>
  56. #include <linux/of_device.h>
  57. #include <linux/of_gpio.h>
  58. #include <linux/of_mdio.h>
  59. #include <linux/of_net.h>
  60. #include <linux/regulator/consumer.h>
  61. #include <linux/if_vlan.h>
  62. #include <linux/pinctrl/consumer.h>
  63. #include <linux/prefetch.h>
  64. #include <linux/mfd/syscon.h>
  65. #include <linux/regmap.h>
  66. #include <soc/imx/cpuidle.h>
  67. #include <asm/cacheflush.h>
  68. #include "fec.h"
  69. static void set_multicast_list(struct net_device *ndev);
  70. static void fec_enet_itr_coal_init(struct net_device *ndev);
  71. #define DRIVER_NAME "fec"
  72. #define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
  73. /* Pause frame feild and FIFO threshold */
  74. #define FEC_ENET_FCE (1 << 5)
  75. #define FEC_ENET_RSEM_V 0x84
  76. #define FEC_ENET_RSFL_V 16
  77. #define FEC_ENET_RAEM_V 0x8
  78. #define FEC_ENET_RAFL_V 0x8
  79. #define FEC_ENET_OPD_V 0xFFF0
  80. #define FEC_MDIO_PM_TIMEOUT 100 /* ms */
  81. struct fec_devinfo {
  82. u32 quirks;
  83. u8 stop_gpr_reg;
  84. u8 stop_gpr_bit;
  85. };
  86. static const struct fec_devinfo fec_imx25_info = {
  87. .quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR |
  88. FEC_QUIRK_HAS_FRREG,
  89. };
  90. static const struct fec_devinfo fec_imx27_info = {
  91. .quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG,
  92. };
  93. static const struct fec_devinfo fec_imx28_info = {
  94. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
  95. FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC |
  96. FEC_QUIRK_HAS_FRREG,
  97. };
  98. static const struct fec_devinfo fec_imx6q_info = {
  99. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  100. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  101. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
  102. FEC_QUIRK_HAS_RACC,
  103. .stop_gpr_reg = 0x34,
  104. .stop_gpr_bit = 27,
  105. };
  106. static const struct fec_devinfo fec_mvf600_info = {
  107. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
  108. };
  109. static const struct fec_devinfo fec_imx6x_info = {
  110. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  111. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  112. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
  113. FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
  114. FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE,
  115. };
  116. static const struct fec_devinfo fec_imx6ul_info = {
  117. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  118. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  119. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
  120. FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
  121. FEC_QUIRK_HAS_COALESCE,
  122. };
  123. static struct platform_device_id fec_devtype[] = {
  124. {
  125. /* keep it for coldfire */
  126. .name = DRIVER_NAME,
  127. .driver_data = 0,
  128. }, {
  129. .name = "imx25-fec",
  130. .driver_data = (kernel_ulong_t)&fec_imx25_info,
  131. }, {
  132. .name = "imx27-fec",
  133. .driver_data = (kernel_ulong_t)&fec_imx27_info,
  134. }, {
  135. .name = "imx28-fec",
  136. .driver_data = (kernel_ulong_t)&fec_imx28_info,
  137. }, {
  138. .name = "imx6q-fec",
  139. .driver_data = (kernel_ulong_t)&fec_imx6q_info,
  140. }, {
  141. .name = "mvf600-fec",
  142. .driver_data = (kernel_ulong_t)&fec_mvf600_info,
  143. }, {
  144. .name = "imx6sx-fec",
  145. .driver_data = (kernel_ulong_t)&fec_imx6x_info,
  146. }, {
  147. .name = "imx6ul-fec",
  148. .driver_data = (kernel_ulong_t)&fec_imx6ul_info,
  149. }, {
  150. /* sentinel */
  151. }
  152. };
  153. MODULE_DEVICE_TABLE(platform, fec_devtype);
  154. enum imx_fec_type {
  155. IMX25_FEC = 1, /* runs on i.mx25/50/53 */
  156. IMX27_FEC, /* runs on i.mx27/35/51 */
  157. IMX28_FEC,
  158. IMX6Q_FEC,
  159. MVF600_FEC,
  160. IMX6SX_FEC,
  161. IMX6UL_FEC,
  162. };
  163. static const struct of_device_id fec_dt_ids[] = {
  164. { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
  165. { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
  166. { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
  167. { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
  168. { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
  169. { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
  170. { .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], },
  171. { /* sentinel */ }
  172. };
  173. MODULE_DEVICE_TABLE(of, fec_dt_ids);
  174. static unsigned char macaddr[ETH_ALEN];
  175. module_param_array(macaddr, byte, NULL, 0);
  176. MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
  177. #if defined(CONFIG_M5272)
  178. /*
  179. * Some hardware gets it MAC address out of local flash memory.
  180. * if this is non-zero then assume it is the address to get MAC from.
  181. */
  182. #if defined(CONFIG_NETtel)
  183. #define FEC_FLASHMAC 0xf0006006
  184. #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
  185. #define FEC_FLASHMAC 0xf0006000
  186. #elif defined(CONFIG_CANCam)
  187. #define FEC_FLASHMAC 0xf0020000
  188. #elif defined (CONFIG_M5272C3)
  189. #define FEC_FLASHMAC (0xffe04000 + 4)
  190. #elif defined(CONFIG_MOD5272)
  191. #define FEC_FLASHMAC 0xffc0406b
  192. #else
  193. #define FEC_FLASHMAC 0
  194. #endif
  195. #endif /* CONFIG_M5272 */
  196. /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
  197. *
  198. * 2048 byte skbufs are allocated. However, alignment requirements
  199. * varies between FEC variants. Worst case is 64, so round down by 64.
  200. */
  201. #define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64))
  202. #define PKT_MINBUF_SIZE 64
  203. /* FEC receive acceleration */
  204. #define FEC_RACC_IPDIS (1 << 1)
  205. #define FEC_RACC_PRODIS (1 << 2)
  206. #define FEC_RACC_SHIFT16 BIT(7)
  207. #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
  208. /* MIB Control Register */
  209. #define FEC_MIB_CTRLSTAT_DISABLE BIT(31)
  210. /*
  211. * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
  212. * size bits. Other FEC hardware does not, so we need to take that into
  213. * account when setting it.
  214. */
  215. #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  216. defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
  217. defined(CONFIG_ARM64)
  218. #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
  219. #else
  220. #define OPT_FRAME_SIZE 0
  221. #endif
  222. /* FEC MII MMFR bits definition */
  223. #define FEC_MMFR_ST (1 << 30)
  224. #define FEC_MMFR_OP_READ (2 << 28)
  225. #define FEC_MMFR_OP_WRITE (1 << 28)
  226. #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
  227. #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
  228. #define FEC_MMFR_TA (2 << 16)
  229. #define FEC_MMFR_DATA(v) (v & 0xffff)
  230. /* FEC ECR bits definition */
  231. #define FEC_ECR_MAGICEN (1 << 2)
  232. #define FEC_ECR_SLEEP (1 << 3)
  233. #define FEC_MII_TIMEOUT 30000 /* us */
  234. /* Transmitter timeout */
  235. #define TX_TIMEOUT (2 * HZ)
  236. #define FEC_PAUSE_FLAG_AUTONEG 0x1
  237. #define FEC_PAUSE_FLAG_ENABLE 0x2
  238. #define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
  239. #define FEC_WOL_FLAG_ENABLE (0x1 << 1)
  240. #define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
  241. #define COPYBREAK_DEFAULT 256
  242. /* Max number of allowed TCP segments for software TSO */
  243. #define FEC_MAX_TSO_SEGS 100
  244. #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
  245. #define IS_TSO_HEADER(txq, addr) \
  246. ((addr >= txq->tso_hdrs_dma) && \
  247. (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
  248. static int mii_cnt;
  249. static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
  250. struct bufdesc_prop *bd)
  251. {
  252. return (bdp >= bd->last) ? bd->base
  253. : (struct bufdesc *)(((void *)bdp) + bd->dsize);
  254. }
  255. static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
  256. struct bufdesc_prop *bd)
  257. {
  258. return (bdp <= bd->base) ? bd->last
  259. : (struct bufdesc *)(((void *)bdp) - bd->dsize);
  260. }
  261. static int fec_enet_get_bd_index(struct bufdesc *bdp,
  262. struct bufdesc_prop *bd)
  263. {
  264. return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
  265. }
  266. static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
  267. {
  268. int entries;
  269. entries = (((const char *)txq->dirty_tx -
  270. (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
  271. return entries >= 0 ? entries : entries + txq->bd.ring_size;
  272. }
  273. static void swap_buffer(void *bufaddr, int len)
  274. {
  275. int i;
  276. unsigned int *buf = bufaddr;
  277. for (i = 0; i < len; i += 4, buf++)
  278. swab32s(buf);
  279. }
  280. static void swap_buffer2(void *dst_buf, void *src_buf, int len)
  281. {
  282. int i;
  283. unsigned int *src = src_buf;
  284. unsigned int *dst = dst_buf;
  285. for (i = 0; i < len; i += 4, src++, dst++)
  286. *dst = swab32p(src);
  287. }
  288. static void fec_dump(struct net_device *ndev)
  289. {
  290. struct fec_enet_private *fep = netdev_priv(ndev);
  291. struct bufdesc *bdp;
  292. struct fec_enet_priv_tx_q *txq;
  293. int index = 0;
  294. netdev_info(ndev, "TX ring dump\n");
  295. pr_info("Nr SC addr len SKB\n");
  296. txq = fep->tx_queue[0];
  297. bdp = txq->bd.base;
  298. do {
  299. pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
  300. index,
  301. bdp == txq->bd.cur ? 'S' : ' ',
  302. bdp == txq->dirty_tx ? 'H' : ' ',
  303. fec16_to_cpu(bdp->cbd_sc),
  304. fec32_to_cpu(bdp->cbd_bufaddr),
  305. fec16_to_cpu(bdp->cbd_datlen),
  306. txq->tx_skbuff[index]);
  307. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  308. index++;
  309. } while (bdp != txq->bd.base);
  310. }
  311. static inline bool is_ipv4_pkt(struct sk_buff *skb)
  312. {
  313. return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
  314. }
  315. static int
  316. fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
  317. {
  318. /* Only run for packets requiring a checksum. */
  319. if (skb->ip_summed != CHECKSUM_PARTIAL)
  320. return 0;
  321. if (unlikely(skb_cow_head(skb, 0)))
  322. return -1;
  323. if (is_ipv4_pkt(skb))
  324. ip_hdr(skb)->check = 0;
  325. *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
  326. return 0;
  327. }
  328. static struct bufdesc *
  329. fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
  330. struct sk_buff *skb,
  331. struct net_device *ndev)
  332. {
  333. struct fec_enet_private *fep = netdev_priv(ndev);
  334. struct bufdesc *bdp = txq->bd.cur;
  335. struct bufdesc_ex *ebdp;
  336. int nr_frags = skb_shinfo(skb)->nr_frags;
  337. int frag, frag_len;
  338. unsigned short status;
  339. unsigned int estatus = 0;
  340. skb_frag_t *this_frag;
  341. unsigned int index;
  342. void *bufaddr;
  343. dma_addr_t addr;
  344. int i;
  345. for (frag = 0; frag < nr_frags; frag++) {
  346. this_frag = &skb_shinfo(skb)->frags[frag];
  347. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  348. ebdp = (struct bufdesc_ex *)bdp;
  349. status = fec16_to_cpu(bdp->cbd_sc);
  350. status &= ~BD_ENET_TX_STATS;
  351. status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
  352. frag_len = skb_shinfo(skb)->frags[frag].size;
  353. /* Handle the last BD specially */
  354. if (frag == nr_frags - 1) {
  355. status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
  356. if (fep->bufdesc_ex) {
  357. estatus |= BD_ENET_TX_INT;
  358. if (unlikely(skb_shinfo(skb)->tx_flags &
  359. SKBTX_HW_TSTAMP && fep->hwts_tx_en))
  360. estatus |= BD_ENET_TX_TS;
  361. }
  362. }
  363. if (fep->bufdesc_ex) {
  364. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  365. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  366. if (skb->ip_summed == CHECKSUM_PARTIAL)
  367. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  368. ebdp->cbd_bdu = 0;
  369. ebdp->cbd_esc = cpu_to_fec32(estatus);
  370. }
  371. bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
  372. index = fec_enet_get_bd_index(bdp, &txq->bd);
  373. if (((unsigned long) bufaddr) & fep->tx_align ||
  374. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  375. memcpy(txq->tx_bounce[index], bufaddr, frag_len);
  376. bufaddr = txq->tx_bounce[index];
  377. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  378. swap_buffer(bufaddr, frag_len);
  379. }
  380. addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
  381. DMA_TO_DEVICE);
  382. if (dma_mapping_error(&fep->pdev->dev, addr)) {
  383. if (net_ratelimit())
  384. netdev_err(ndev, "Tx DMA memory map failed\n");
  385. goto dma_mapping_error;
  386. }
  387. bdp->cbd_bufaddr = cpu_to_fec32(addr);
  388. bdp->cbd_datlen = cpu_to_fec16(frag_len);
  389. /* Make sure the updates to rest of the descriptor are
  390. * performed before transferring ownership.
  391. */
  392. wmb();
  393. bdp->cbd_sc = cpu_to_fec16(status);
  394. }
  395. return bdp;
  396. dma_mapping_error:
  397. bdp = txq->bd.cur;
  398. for (i = 0; i < frag; i++) {
  399. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  400. dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
  401. fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
  402. }
  403. return ERR_PTR(-ENOMEM);
  404. }
  405. static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
  406. struct sk_buff *skb, struct net_device *ndev)
  407. {
  408. struct fec_enet_private *fep = netdev_priv(ndev);
  409. int nr_frags = skb_shinfo(skb)->nr_frags;
  410. struct bufdesc *bdp, *last_bdp;
  411. void *bufaddr;
  412. dma_addr_t addr;
  413. unsigned short status;
  414. unsigned short buflen;
  415. unsigned int estatus = 0;
  416. unsigned int index;
  417. int entries_free;
  418. entries_free = fec_enet_get_free_txdesc_num(txq);
  419. if (entries_free < MAX_SKB_FRAGS + 1) {
  420. dev_kfree_skb_any(skb);
  421. if (net_ratelimit())
  422. netdev_err(ndev, "NOT enough BD for SG!\n");
  423. return NETDEV_TX_OK;
  424. }
  425. /* Protocol checksum off-load for TCP and UDP. */
  426. if (fec_enet_clear_csum(skb, ndev)) {
  427. dev_kfree_skb_any(skb);
  428. return NETDEV_TX_OK;
  429. }
  430. /* Fill in a Tx ring entry */
  431. bdp = txq->bd.cur;
  432. last_bdp = bdp;
  433. status = fec16_to_cpu(bdp->cbd_sc);
  434. status &= ~BD_ENET_TX_STATS;
  435. /* Set buffer length and buffer pointer */
  436. bufaddr = skb->data;
  437. buflen = skb_headlen(skb);
  438. index = fec_enet_get_bd_index(bdp, &txq->bd);
  439. if (((unsigned long) bufaddr) & fep->tx_align ||
  440. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  441. memcpy(txq->tx_bounce[index], skb->data, buflen);
  442. bufaddr = txq->tx_bounce[index];
  443. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  444. swap_buffer(bufaddr, buflen);
  445. }
  446. /* Push the data cache so the CPM does not get stale memory data. */
  447. addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
  448. if (dma_mapping_error(&fep->pdev->dev, addr)) {
  449. dev_kfree_skb_any(skb);
  450. if (net_ratelimit())
  451. netdev_err(ndev, "Tx DMA memory map failed\n");
  452. return NETDEV_TX_OK;
  453. }
  454. if (nr_frags) {
  455. last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
  456. if (IS_ERR(last_bdp)) {
  457. dma_unmap_single(&fep->pdev->dev, addr,
  458. buflen, DMA_TO_DEVICE);
  459. dev_kfree_skb_any(skb);
  460. return NETDEV_TX_OK;
  461. }
  462. } else {
  463. status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
  464. if (fep->bufdesc_ex) {
  465. estatus = BD_ENET_TX_INT;
  466. if (unlikely(skb_shinfo(skb)->tx_flags &
  467. SKBTX_HW_TSTAMP && fep->hwts_tx_en))
  468. estatus |= BD_ENET_TX_TS;
  469. }
  470. }
  471. bdp->cbd_bufaddr = cpu_to_fec32(addr);
  472. bdp->cbd_datlen = cpu_to_fec16(buflen);
  473. if (fep->bufdesc_ex) {
  474. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  475. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
  476. fep->hwts_tx_en))
  477. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  478. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  479. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  480. if (skb->ip_summed == CHECKSUM_PARTIAL)
  481. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  482. ebdp->cbd_bdu = 0;
  483. ebdp->cbd_esc = cpu_to_fec32(estatus);
  484. }
  485. index = fec_enet_get_bd_index(last_bdp, &txq->bd);
  486. /* Save skb pointer */
  487. txq->tx_skbuff[index] = skb;
  488. /* Make sure the updates to rest of the descriptor are performed before
  489. * transferring ownership.
  490. */
  491. wmb();
  492. /* Send it on its way. Tell FEC it's ready, interrupt when done,
  493. * it's the last BD of the frame, and to put the CRC on the end.
  494. */
  495. status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
  496. bdp->cbd_sc = cpu_to_fec16(status);
  497. /* If this was the last BD in the ring, start at the beginning again. */
  498. bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
  499. skb_tx_timestamp(skb);
  500. /* Make sure the update to bdp and tx_skbuff are performed before
  501. * txq->bd.cur.
  502. */
  503. wmb();
  504. txq->bd.cur = bdp;
  505. /* Trigger transmission start */
  506. writel(0, txq->bd.reg_desc_active);
  507. return 0;
  508. }
  509. static int
  510. fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
  511. struct net_device *ndev,
  512. struct bufdesc *bdp, int index, char *data,
  513. int size, bool last_tcp, bool is_last)
  514. {
  515. struct fec_enet_private *fep = netdev_priv(ndev);
  516. struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
  517. unsigned short status;
  518. unsigned int estatus = 0;
  519. dma_addr_t addr;
  520. status = fec16_to_cpu(bdp->cbd_sc);
  521. status &= ~BD_ENET_TX_STATS;
  522. status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
  523. if (((unsigned long) data) & fep->tx_align ||
  524. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  525. memcpy(txq->tx_bounce[index], data, size);
  526. data = txq->tx_bounce[index];
  527. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  528. swap_buffer(data, size);
  529. }
  530. addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
  531. if (dma_mapping_error(&fep->pdev->dev, addr)) {
  532. dev_kfree_skb_any(skb);
  533. if (net_ratelimit())
  534. netdev_err(ndev, "Tx DMA memory map failed\n");
  535. return NETDEV_TX_BUSY;
  536. }
  537. bdp->cbd_datlen = cpu_to_fec16(size);
  538. bdp->cbd_bufaddr = cpu_to_fec32(addr);
  539. if (fep->bufdesc_ex) {
  540. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  541. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  542. if (skb->ip_summed == CHECKSUM_PARTIAL)
  543. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  544. ebdp->cbd_bdu = 0;
  545. ebdp->cbd_esc = cpu_to_fec32(estatus);
  546. }
  547. /* Handle the last BD specially */
  548. if (last_tcp)
  549. status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
  550. if (is_last) {
  551. status |= BD_ENET_TX_INTR;
  552. if (fep->bufdesc_ex)
  553. ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
  554. }
  555. bdp->cbd_sc = cpu_to_fec16(status);
  556. return 0;
  557. }
  558. static int
  559. fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
  560. struct sk_buff *skb, struct net_device *ndev,
  561. struct bufdesc *bdp, int index)
  562. {
  563. struct fec_enet_private *fep = netdev_priv(ndev);
  564. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  565. struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
  566. void *bufaddr;
  567. unsigned long dmabuf;
  568. unsigned short status;
  569. unsigned int estatus = 0;
  570. status = fec16_to_cpu(bdp->cbd_sc);
  571. status &= ~BD_ENET_TX_STATS;
  572. status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
  573. bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
  574. dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
  575. if (((unsigned long)bufaddr) & fep->tx_align ||
  576. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  577. memcpy(txq->tx_bounce[index], skb->data, hdr_len);
  578. bufaddr = txq->tx_bounce[index];
  579. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  580. swap_buffer(bufaddr, hdr_len);
  581. dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
  582. hdr_len, DMA_TO_DEVICE);
  583. if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
  584. dev_kfree_skb_any(skb);
  585. if (net_ratelimit())
  586. netdev_err(ndev, "Tx DMA memory map failed\n");
  587. return NETDEV_TX_BUSY;
  588. }
  589. }
  590. bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
  591. bdp->cbd_datlen = cpu_to_fec16(hdr_len);
  592. if (fep->bufdesc_ex) {
  593. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  594. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  595. if (skb->ip_summed == CHECKSUM_PARTIAL)
  596. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  597. ebdp->cbd_bdu = 0;
  598. ebdp->cbd_esc = cpu_to_fec32(estatus);
  599. }
  600. bdp->cbd_sc = cpu_to_fec16(status);
  601. return 0;
  602. }
  603. static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
  604. struct sk_buff *skb,
  605. struct net_device *ndev)
  606. {
  607. struct fec_enet_private *fep = netdev_priv(ndev);
  608. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  609. int total_len, data_left;
  610. struct bufdesc *bdp = txq->bd.cur;
  611. struct tso_t tso;
  612. unsigned int index = 0;
  613. int ret;
  614. if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
  615. dev_kfree_skb_any(skb);
  616. if (net_ratelimit())
  617. netdev_err(ndev, "NOT enough BD for TSO!\n");
  618. return NETDEV_TX_OK;
  619. }
  620. /* Protocol checksum off-load for TCP and UDP. */
  621. if (fec_enet_clear_csum(skb, ndev)) {
  622. dev_kfree_skb_any(skb);
  623. return NETDEV_TX_OK;
  624. }
  625. /* Initialize the TSO handler, and prepare the first payload */
  626. tso_start(skb, &tso);
  627. total_len = skb->len - hdr_len;
  628. while (total_len > 0) {
  629. char *hdr;
  630. index = fec_enet_get_bd_index(bdp, &txq->bd);
  631. data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
  632. total_len -= data_left;
  633. /* prepare packet headers: MAC + IP + TCP */
  634. hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
  635. tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
  636. ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
  637. if (ret)
  638. goto err_release;
  639. while (data_left > 0) {
  640. int size;
  641. size = min_t(int, tso.size, data_left);
  642. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  643. index = fec_enet_get_bd_index(bdp, &txq->bd);
  644. ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
  645. bdp, index,
  646. tso.data, size,
  647. size == data_left,
  648. total_len == 0);
  649. if (ret)
  650. goto err_release;
  651. data_left -= size;
  652. tso_build_data(skb, &tso, size);
  653. }
  654. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  655. }
  656. /* Save skb pointer */
  657. txq->tx_skbuff[index] = skb;
  658. skb_tx_timestamp(skb);
  659. txq->bd.cur = bdp;
  660. /* Trigger transmission start */
  661. if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
  662. !readl(txq->bd.reg_desc_active) ||
  663. !readl(txq->bd.reg_desc_active) ||
  664. !readl(txq->bd.reg_desc_active) ||
  665. !readl(txq->bd.reg_desc_active))
  666. writel(0, txq->bd.reg_desc_active);
  667. return 0;
  668. err_release:
  669. /* TODO: Release all used data descriptors for TSO */
  670. return ret;
  671. }
  672. static netdev_tx_t
  673. fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  674. {
  675. struct fec_enet_private *fep = netdev_priv(ndev);
  676. int entries_free;
  677. unsigned short queue;
  678. struct fec_enet_priv_tx_q *txq;
  679. struct netdev_queue *nq;
  680. int ret;
  681. queue = skb_get_queue_mapping(skb);
  682. txq = fep->tx_queue[queue];
  683. nq = netdev_get_tx_queue(ndev, queue);
  684. if (skb_is_gso(skb))
  685. ret = fec_enet_txq_submit_tso(txq, skb, ndev);
  686. else
  687. ret = fec_enet_txq_submit_skb(txq, skb, ndev);
  688. if (ret)
  689. return ret;
  690. entries_free = fec_enet_get_free_txdesc_num(txq);
  691. if (entries_free <= txq->tx_stop_threshold)
  692. netif_tx_stop_queue(nq);
  693. return NETDEV_TX_OK;
  694. }
  695. /* Init RX & TX buffer descriptors
  696. */
  697. static void fec_enet_bd_init(struct net_device *dev)
  698. {
  699. struct fec_enet_private *fep = netdev_priv(dev);
  700. struct fec_enet_priv_tx_q *txq;
  701. struct fec_enet_priv_rx_q *rxq;
  702. struct bufdesc *bdp;
  703. unsigned int i;
  704. unsigned int q;
  705. for (q = 0; q < fep->num_rx_queues; q++) {
  706. /* Initialize the receive buffer descriptors. */
  707. rxq = fep->rx_queue[q];
  708. bdp = rxq->bd.base;
  709. for (i = 0; i < rxq->bd.ring_size; i++) {
  710. /* Initialize the BD for every fragment in the page. */
  711. if (bdp->cbd_bufaddr)
  712. bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
  713. else
  714. bdp->cbd_sc = cpu_to_fec16(0);
  715. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  716. }
  717. /* Set the last buffer to wrap */
  718. bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
  719. bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
  720. rxq->bd.cur = rxq->bd.base;
  721. }
  722. for (q = 0; q < fep->num_tx_queues; q++) {
  723. /* ...and the same for transmit */
  724. txq = fep->tx_queue[q];
  725. bdp = txq->bd.base;
  726. txq->bd.cur = bdp;
  727. for (i = 0; i < txq->bd.ring_size; i++) {
  728. /* Initialize the BD for every fragment in the page. */
  729. bdp->cbd_sc = cpu_to_fec16(0);
  730. if (bdp->cbd_bufaddr &&
  731. !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
  732. dma_unmap_single(&fep->pdev->dev,
  733. fec32_to_cpu(bdp->cbd_bufaddr),
  734. fec16_to_cpu(bdp->cbd_datlen),
  735. DMA_TO_DEVICE);
  736. if (txq->tx_skbuff[i]) {
  737. dev_kfree_skb_any(txq->tx_skbuff[i]);
  738. txq->tx_skbuff[i] = NULL;
  739. }
  740. bdp->cbd_bufaddr = cpu_to_fec32(0);
  741. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  742. }
  743. /* Set the last buffer to wrap */
  744. bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
  745. bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
  746. txq->dirty_tx = bdp;
  747. }
  748. }
  749. static void fec_enet_active_rxring(struct net_device *ndev)
  750. {
  751. struct fec_enet_private *fep = netdev_priv(ndev);
  752. int i;
  753. for (i = 0; i < fep->num_rx_queues; i++)
  754. writel(0, fep->rx_queue[i]->bd.reg_desc_active);
  755. }
  756. static void fec_enet_enable_ring(struct net_device *ndev)
  757. {
  758. struct fec_enet_private *fep = netdev_priv(ndev);
  759. struct fec_enet_priv_tx_q *txq;
  760. struct fec_enet_priv_rx_q *rxq;
  761. int i;
  762. for (i = 0; i < fep->num_rx_queues; i++) {
  763. rxq = fep->rx_queue[i];
  764. writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
  765. writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
  766. /* enable DMA1/2 */
  767. if (i)
  768. writel(RCMR_MATCHEN | RCMR_CMP(i),
  769. fep->hwp + FEC_RCMR(i));
  770. }
  771. for (i = 0; i < fep->num_tx_queues; i++) {
  772. txq = fep->tx_queue[i];
  773. writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
  774. /* enable DMA1/2 */
  775. if (i)
  776. writel(DMA_CLASS_EN | IDLE_SLOPE(i),
  777. fep->hwp + FEC_DMA_CFG(i));
  778. }
  779. }
  780. static void fec_enet_reset_skb(struct net_device *ndev)
  781. {
  782. struct fec_enet_private *fep = netdev_priv(ndev);
  783. struct fec_enet_priv_tx_q *txq;
  784. int i, j;
  785. for (i = 0; i < fep->num_tx_queues; i++) {
  786. txq = fep->tx_queue[i];
  787. for (j = 0; j < txq->bd.ring_size; j++) {
  788. if (txq->tx_skbuff[j]) {
  789. dev_kfree_skb_any(txq->tx_skbuff[j]);
  790. txq->tx_skbuff[j] = NULL;
  791. }
  792. }
  793. }
  794. }
  795. /*
  796. * This function is called to start or restart the FEC during a link
  797. * change, transmit timeout, or to reconfigure the FEC. The network
  798. * packet processing for this device must be stopped before this call.
  799. */
  800. static void
  801. fec_restart(struct net_device *ndev)
  802. {
  803. struct fec_enet_private *fep = netdev_priv(ndev);
  804. u32 val;
  805. u32 temp_mac[2];
  806. u32 rcntl = OPT_FRAME_SIZE | 0x04;
  807. u32 ecntl = 0x2; /* ETHEREN */
  808. /* Whack a reset. We should wait for this.
  809. * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
  810. * instead of reset MAC itself.
  811. */
  812. if (fep->quirks & FEC_QUIRK_HAS_AVB) {
  813. writel(0, fep->hwp + FEC_ECNTRL);
  814. } else {
  815. writel(1, fep->hwp + FEC_ECNTRL);
  816. udelay(10);
  817. }
  818. /*
  819. * enet-mac reset will reset mac address registers too,
  820. * so need to reconfigure it.
  821. */
  822. memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
  823. writel((__force u32)cpu_to_be32(temp_mac[0]),
  824. fep->hwp + FEC_ADDR_LOW);
  825. writel((__force u32)cpu_to_be32(temp_mac[1]),
  826. fep->hwp + FEC_ADDR_HIGH);
  827. /* Clear any outstanding interrupt. */
  828. writel(0xffffffff, fep->hwp + FEC_IEVENT);
  829. fec_enet_bd_init(ndev);
  830. fec_enet_enable_ring(ndev);
  831. /* Reset tx SKB buffers. */
  832. fec_enet_reset_skb(ndev);
  833. /* Enable MII mode */
  834. if (fep->full_duplex == DUPLEX_FULL) {
  835. /* FD enable */
  836. writel(0x04, fep->hwp + FEC_X_CNTRL);
  837. } else {
  838. /* No Rcv on Xmit */
  839. rcntl |= 0x02;
  840. writel(0x0, fep->hwp + FEC_X_CNTRL);
  841. }
  842. /* Set MII speed */
  843. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  844. #if !defined(CONFIG_M5272)
  845. if (fep->quirks & FEC_QUIRK_HAS_RACC) {
  846. val = readl(fep->hwp + FEC_RACC);
  847. /* align IP header */
  848. val |= FEC_RACC_SHIFT16;
  849. if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
  850. /* set RX checksum */
  851. val |= FEC_RACC_OPTIONS;
  852. else
  853. val &= ~FEC_RACC_OPTIONS;
  854. writel(val, fep->hwp + FEC_RACC);
  855. writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
  856. }
  857. #endif
  858. /*
  859. * The phy interface and speed need to get configured
  860. * differently on enet-mac.
  861. */
  862. if (fep->quirks & FEC_QUIRK_ENET_MAC) {
  863. /* Enable flow control and length check */
  864. rcntl |= 0x40000000 | 0x00000020;
  865. /* RGMII, RMII or MII */
  866. if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
  867. fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
  868. fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
  869. fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
  870. rcntl |= (1 << 6);
  871. else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
  872. rcntl |= (1 << 8);
  873. else
  874. rcntl &= ~(1 << 8);
  875. /* 1G, 100M or 10M */
  876. if (ndev->phydev) {
  877. if (ndev->phydev->speed == SPEED_1000)
  878. ecntl |= (1 << 5);
  879. else if (ndev->phydev->speed == SPEED_100)
  880. rcntl &= ~(1 << 9);
  881. else
  882. rcntl |= (1 << 9);
  883. }
  884. } else {
  885. #ifdef FEC_MIIGSK_ENR
  886. if (fep->quirks & FEC_QUIRK_USE_GASKET) {
  887. u32 cfgr;
  888. /* disable the gasket and wait */
  889. writel(0, fep->hwp + FEC_MIIGSK_ENR);
  890. while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
  891. udelay(1);
  892. /*
  893. * configure the gasket:
  894. * RMII, 50 MHz, no loopback, no echo
  895. * MII, 25 MHz, no loopback, no echo
  896. */
  897. cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
  898. ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
  899. if (ndev->phydev && ndev->phydev->speed == SPEED_10)
  900. cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
  901. writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
  902. /* re-enable the gasket */
  903. writel(2, fep->hwp + FEC_MIIGSK_ENR);
  904. }
  905. #endif
  906. }
  907. #if !defined(CONFIG_M5272)
  908. /* enable pause frame*/
  909. if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
  910. ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
  911. ndev->phydev && ndev->phydev->pause)) {
  912. rcntl |= FEC_ENET_FCE;
  913. /* set FIFO threshold parameter to reduce overrun */
  914. writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
  915. writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
  916. writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
  917. writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
  918. /* OPD */
  919. writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
  920. } else {
  921. rcntl &= ~FEC_ENET_FCE;
  922. }
  923. #endif /* !defined(CONFIG_M5272) */
  924. writel(rcntl, fep->hwp + FEC_R_CNTRL);
  925. /* Setup multicast filter. */
  926. set_multicast_list(ndev);
  927. #ifndef CONFIG_M5272
  928. writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
  929. writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
  930. #endif
  931. if (fep->quirks & FEC_QUIRK_ENET_MAC) {
  932. /* enable ENET endian swap */
  933. ecntl |= (1 << 8);
  934. /* enable ENET store and forward mode */
  935. writel(1 << 8, fep->hwp + FEC_X_WMRK);
  936. }
  937. if (fep->bufdesc_ex)
  938. ecntl |= (1 << 4);
  939. #ifndef CONFIG_M5272
  940. /* Enable the MIB statistic event counters */
  941. writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
  942. #endif
  943. /* And last, enable the transmit and receive processing */
  944. writel(ecntl, fep->hwp + FEC_ECNTRL);
  945. fec_enet_active_rxring(ndev);
  946. if (fep->bufdesc_ex)
  947. fec_ptp_start_cyclecounter(ndev);
  948. /* Enable interrupts we wish to service */
  949. if (fep->link)
  950. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  951. else
  952. writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
  953. /* Init the interrupt coalescing */
  954. fec_enet_itr_coal_init(ndev);
  955. }
  956. static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled)
  957. {
  958. struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
  959. struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr;
  960. if (stop_gpr->gpr) {
  961. if (enabled)
  962. regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
  963. BIT(stop_gpr->bit),
  964. BIT(stop_gpr->bit));
  965. else
  966. regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
  967. BIT(stop_gpr->bit), 0);
  968. } else if (pdata && pdata->sleep_mode_enable) {
  969. pdata->sleep_mode_enable(enabled);
  970. }
  971. }
  972. static void
  973. fec_stop(struct net_device *ndev)
  974. {
  975. struct fec_enet_private *fep = netdev_priv(ndev);
  976. u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
  977. u32 val;
  978. /* We cannot expect a graceful transmit stop without link !!! */
  979. if (fep->link) {
  980. writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
  981. udelay(10);
  982. if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
  983. netdev_err(ndev, "Graceful transmit stop did not complete!\n");
  984. }
  985. /* Whack a reset. We should wait for this.
  986. * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
  987. * instead of reset MAC itself.
  988. */
  989. if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
  990. if (fep->quirks & FEC_QUIRK_HAS_AVB) {
  991. writel(0, fep->hwp + FEC_ECNTRL);
  992. } else {
  993. writel(1, fep->hwp + FEC_ECNTRL);
  994. udelay(10);
  995. }
  996. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  997. } else {
  998. writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
  999. val = readl(fep->hwp + FEC_ECNTRL);
  1000. val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
  1001. writel(val, fep->hwp + FEC_ECNTRL);
  1002. fec_enet_stop_mode(fep, true);
  1003. }
  1004. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1005. /* We have to keep ENET enabled to have MII interrupt stay working */
  1006. if (fep->quirks & FEC_QUIRK_ENET_MAC &&
  1007. !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
  1008. writel(2, fep->hwp + FEC_ECNTRL);
  1009. writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
  1010. }
  1011. }
  1012. static void
  1013. fec_timeout(struct net_device *ndev)
  1014. {
  1015. struct fec_enet_private *fep = netdev_priv(ndev);
  1016. fec_dump(ndev);
  1017. ndev->stats.tx_errors++;
  1018. schedule_work(&fep->tx_timeout_work);
  1019. }
  1020. static void fec_enet_timeout_work(struct work_struct *work)
  1021. {
  1022. struct fec_enet_private *fep =
  1023. container_of(work, struct fec_enet_private, tx_timeout_work);
  1024. struct net_device *ndev = fep->netdev;
  1025. rtnl_lock();
  1026. if (netif_device_present(ndev) || netif_running(ndev)) {
  1027. napi_disable(&fep->napi);
  1028. netif_tx_lock_bh(ndev);
  1029. fec_restart(ndev);
  1030. netif_tx_wake_all_queues(ndev);
  1031. netif_tx_unlock_bh(ndev);
  1032. napi_enable(&fep->napi);
  1033. }
  1034. rtnl_unlock();
  1035. }
  1036. static void
  1037. fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
  1038. struct skb_shared_hwtstamps *hwtstamps)
  1039. {
  1040. unsigned long flags;
  1041. u64 ns;
  1042. spin_lock_irqsave(&fep->tmreg_lock, flags);
  1043. ns = timecounter_cyc2time(&fep->tc, ts);
  1044. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  1045. memset(hwtstamps, 0, sizeof(*hwtstamps));
  1046. hwtstamps->hwtstamp = ns_to_ktime(ns);
  1047. }
  1048. static void
  1049. fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
  1050. {
  1051. struct fec_enet_private *fep;
  1052. struct bufdesc *bdp;
  1053. unsigned short status;
  1054. struct sk_buff *skb;
  1055. struct fec_enet_priv_tx_q *txq;
  1056. struct netdev_queue *nq;
  1057. int index = 0;
  1058. int entries_free;
  1059. fep = netdev_priv(ndev);
  1060. queue_id = FEC_ENET_GET_QUQUE(queue_id);
  1061. txq = fep->tx_queue[queue_id];
  1062. /* get next bdp of dirty_tx */
  1063. nq = netdev_get_tx_queue(ndev, queue_id);
  1064. bdp = txq->dirty_tx;
  1065. /* get next bdp of dirty_tx */
  1066. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  1067. while (bdp != READ_ONCE(txq->bd.cur)) {
  1068. /* Order the load of bd.cur and cbd_sc */
  1069. rmb();
  1070. status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
  1071. if (status & BD_ENET_TX_READY)
  1072. break;
  1073. index = fec_enet_get_bd_index(bdp, &txq->bd);
  1074. skb = txq->tx_skbuff[index];
  1075. txq->tx_skbuff[index] = NULL;
  1076. if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
  1077. dma_unmap_single(&fep->pdev->dev,
  1078. fec32_to_cpu(bdp->cbd_bufaddr),
  1079. fec16_to_cpu(bdp->cbd_datlen),
  1080. DMA_TO_DEVICE);
  1081. bdp->cbd_bufaddr = cpu_to_fec32(0);
  1082. if (!skb)
  1083. goto skb_done;
  1084. /* Check for errors. */
  1085. if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  1086. BD_ENET_TX_RL | BD_ENET_TX_UN |
  1087. BD_ENET_TX_CSL)) {
  1088. ndev->stats.tx_errors++;
  1089. if (status & BD_ENET_TX_HB) /* No heartbeat */
  1090. ndev->stats.tx_heartbeat_errors++;
  1091. if (status & BD_ENET_TX_LC) /* Late collision */
  1092. ndev->stats.tx_window_errors++;
  1093. if (status & BD_ENET_TX_RL) /* Retrans limit */
  1094. ndev->stats.tx_aborted_errors++;
  1095. if (status & BD_ENET_TX_UN) /* Underrun */
  1096. ndev->stats.tx_fifo_errors++;
  1097. if (status & BD_ENET_TX_CSL) /* Carrier lost */
  1098. ndev->stats.tx_carrier_errors++;
  1099. } else {
  1100. ndev->stats.tx_packets++;
  1101. ndev->stats.tx_bytes += skb->len;
  1102. }
  1103. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
  1104. fep->bufdesc_ex) {
  1105. struct skb_shared_hwtstamps shhwtstamps;
  1106. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1107. fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
  1108. skb_tstamp_tx(skb, &shhwtstamps);
  1109. }
  1110. /* Deferred means some collisions occurred during transmit,
  1111. * but we eventually sent the packet OK.
  1112. */
  1113. if (status & BD_ENET_TX_DEF)
  1114. ndev->stats.collisions++;
  1115. /* Free the sk buffer associated with this last transmit */
  1116. dev_kfree_skb_any(skb);
  1117. skb_done:
  1118. /* Make sure the update to bdp and tx_skbuff are performed
  1119. * before dirty_tx
  1120. */
  1121. wmb();
  1122. txq->dirty_tx = bdp;
  1123. /* Update pointer to next buffer descriptor to be transmitted */
  1124. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  1125. /* Since we have freed up a buffer, the ring is no longer full
  1126. */
  1127. if (netif_tx_queue_stopped(nq)) {
  1128. entries_free = fec_enet_get_free_txdesc_num(txq);
  1129. if (entries_free >= txq->tx_wake_threshold)
  1130. netif_tx_wake_queue(nq);
  1131. }
  1132. }
  1133. /* ERR006358: Keep the transmitter going */
  1134. if (bdp != txq->bd.cur &&
  1135. readl(txq->bd.reg_desc_active) == 0)
  1136. writel(0, txq->bd.reg_desc_active);
  1137. }
  1138. static void
  1139. fec_enet_tx(struct net_device *ndev)
  1140. {
  1141. struct fec_enet_private *fep = netdev_priv(ndev);
  1142. u16 queue_id;
  1143. /* First process class A queue, then Class B and Best Effort queue */
  1144. for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
  1145. clear_bit(queue_id, &fep->work_tx);
  1146. fec_enet_tx_queue(ndev, queue_id);
  1147. }
  1148. return;
  1149. }
  1150. static int
  1151. fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
  1152. {
  1153. struct fec_enet_private *fep = netdev_priv(ndev);
  1154. int off;
  1155. off = ((unsigned long)skb->data) & fep->rx_align;
  1156. if (off)
  1157. skb_reserve(skb, fep->rx_align + 1 - off);
  1158. bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
  1159. if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
  1160. if (net_ratelimit())
  1161. netdev_err(ndev, "Rx DMA memory map failed\n");
  1162. return -ENOMEM;
  1163. }
  1164. return 0;
  1165. }
  1166. static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
  1167. struct bufdesc *bdp, u32 length, bool swap)
  1168. {
  1169. struct fec_enet_private *fep = netdev_priv(ndev);
  1170. struct sk_buff *new_skb;
  1171. if (length > fep->rx_copybreak)
  1172. return false;
  1173. new_skb = netdev_alloc_skb(ndev, length);
  1174. if (!new_skb)
  1175. return false;
  1176. dma_sync_single_for_cpu(&fep->pdev->dev,
  1177. fec32_to_cpu(bdp->cbd_bufaddr),
  1178. FEC_ENET_RX_FRSIZE - fep->rx_align,
  1179. DMA_FROM_DEVICE);
  1180. if (!swap)
  1181. memcpy(new_skb->data, (*skb)->data, length);
  1182. else
  1183. swap_buffer2(new_skb->data, (*skb)->data, length);
  1184. *skb = new_skb;
  1185. return true;
  1186. }
  1187. /* During a receive, the bd_rx.cur points to the current incoming buffer.
  1188. * When we update through the ring, if the next incoming buffer has
  1189. * not been given to the system, we just set the empty indicator,
  1190. * effectively tossing the packet.
  1191. */
  1192. static int
  1193. fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
  1194. {
  1195. struct fec_enet_private *fep = netdev_priv(ndev);
  1196. struct fec_enet_priv_rx_q *rxq;
  1197. struct bufdesc *bdp;
  1198. unsigned short status;
  1199. struct sk_buff *skb_new = NULL;
  1200. struct sk_buff *skb;
  1201. ushort pkt_len;
  1202. __u8 *data;
  1203. int pkt_received = 0;
  1204. struct bufdesc_ex *ebdp = NULL;
  1205. bool vlan_packet_rcvd = false;
  1206. u16 vlan_tag;
  1207. int index = 0;
  1208. bool is_copybreak;
  1209. bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
  1210. #ifdef CONFIG_M532x
  1211. flush_cache_all();
  1212. #endif
  1213. queue_id = FEC_ENET_GET_QUQUE(queue_id);
  1214. rxq = fep->rx_queue[queue_id];
  1215. /* First, grab all of the stats for the incoming packet.
  1216. * These get messed up if we get called due to a busy condition.
  1217. */
  1218. bdp = rxq->bd.cur;
  1219. while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
  1220. if (pkt_received >= budget)
  1221. break;
  1222. pkt_received++;
  1223. writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
  1224. /* Check for errors. */
  1225. status ^= BD_ENET_RX_LAST;
  1226. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
  1227. BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
  1228. BD_ENET_RX_CL)) {
  1229. ndev->stats.rx_errors++;
  1230. if (status & BD_ENET_RX_OV) {
  1231. /* FIFO overrun */
  1232. ndev->stats.rx_fifo_errors++;
  1233. goto rx_processing_done;
  1234. }
  1235. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
  1236. | BD_ENET_RX_LAST)) {
  1237. /* Frame too long or too short. */
  1238. ndev->stats.rx_length_errors++;
  1239. if (status & BD_ENET_RX_LAST)
  1240. netdev_err(ndev, "rcv is not +last\n");
  1241. }
  1242. if (status & BD_ENET_RX_CR) /* CRC Error */
  1243. ndev->stats.rx_crc_errors++;
  1244. /* Report late collisions as a frame error. */
  1245. if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
  1246. ndev->stats.rx_frame_errors++;
  1247. goto rx_processing_done;
  1248. }
  1249. /* Process the incoming frame. */
  1250. ndev->stats.rx_packets++;
  1251. pkt_len = fec16_to_cpu(bdp->cbd_datlen);
  1252. ndev->stats.rx_bytes += pkt_len;
  1253. index = fec_enet_get_bd_index(bdp, &rxq->bd);
  1254. skb = rxq->rx_skbuff[index];
  1255. /* The packet length includes FCS, but we don't want to
  1256. * include that when passing upstream as it messes up
  1257. * bridging applications.
  1258. */
  1259. is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
  1260. need_swap);
  1261. if (!is_copybreak) {
  1262. skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
  1263. if (unlikely(!skb_new)) {
  1264. ndev->stats.rx_dropped++;
  1265. goto rx_processing_done;
  1266. }
  1267. dma_unmap_single(&fep->pdev->dev,
  1268. fec32_to_cpu(bdp->cbd_bufaddr),
  1269. FEC_ENET_RX_FRSIZE - fep->rx_align,
  1270. DMA_FROM_DEVICE);
  1271. }
  1272. prefetch(skb->data - NET_IP_ALIGN);
  1273. skb_put(skb, pkt_len - 4);
  1274. data = skb->data;
  1275. if (!is_copybreak && need_swap)
  1276. swap_buffer(data, pkt_len);
  1277. #if !defined(CONFIG_M5272)
  1278. if (fep->quirks & FEC_QUIRK_HAS_RACC)
  1279. data = skb_pull_inline(skb, 2);
  1280. #endif
  1281. /* Extract the enhanced buffer descriptor */
  1282. ebdp = NULL;
  1283. if (fep->bufdesc_ex)
  1284. ebdp = (struct bufdesc_ex *)bdp;
  1285. /* If this is a VLAN packet remove the VLAN Tag */
  1286. vlan_packet_rcvd = false;
  1287. if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  1288. fep->bufdesc_ex &&
  1289. (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
  1290. /* Push and remove the vlan tag */
  1291. struct vlan_hdr *vlan_header =
  1292. (struct vlan_hdr *) (data + ETH_HLEN);
  1293. vlan_tag = ntohs(vlan_header->h_vlan_TCI);
  1294. vlan_packet_rcvd = true;
  1295. memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
  1296. skb_pull(skb, VLAN_HLEN);
  1297. }
  1298. skb->protocol = eth_type_trans(skb, ndev);
  1299. /* Get receive timestamp from the skb */
  1300. if (fep->hwts_rx_en && fep->bufdesc_ex)
  1301. fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
  1302. skb_hwtstamps(skb));
  1303. if (fep->bufdesc_ex &&
  1304. (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
  1305. if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
  1306. /* don't check it */
  1307. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1308. } else {
  1309. skb_checksum_none_assert(skb);
  1310. }
  1311. }
  1312. /* Handle received VLAN packets */
  1313. if (vlan_packet_rcvd)
  1314. __vlan_hwaccel_put_tag(skb,
  1315. htons(ETH_P_8021Q),
  1316. vlan_tag);
  1317. napi_gro_receive(&fep->napi, skb);
  1318. if (is_copybreak) {
  1319. dma_sync_single_for_device(&fep->pdev->dev,
  1320. fec32_to_cpu(bdp->cbd_bufaddr),
  1321. FEC_ENET_RX_FRSIZE - fep->rx_align,
  1322. DMA_FROM_DEVICE);
  1323. } else {
  1324. rxq->rx_skbuff[index] = skb_new;
  1325. fec_enet_new_rxbdp(ndev, bdp, skb_new);
  1326. }
  1327. rx_processing_done:
  1328. /* Clear the status flags for this buffer */
  1329. status &= ~BD_ENET_RX_STATS;
  1330. /* Mark the buffer empty */
  1331. status |= BD_ENET_RX_EMPTY;
  1332. if (fep->bufdesc_ex) {
  1333. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1334. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
  1335. ebdp->cbd_prot = 0;
  1336. ebdp->cbd_bdu = 0;
  1337. }
  1338. /* Make sure the updates to rest of the descriptor are
  1339. * performed before transferring ownership.
  1340. */
  1341. wmb();
  1342. bdp->cbd_sc = cpu_to_fec16(status);
  1343. /* Update BD pointer to next entry */
  1344. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  1345. /* Doing this here will keep the FEC running while we process
  1346. * incoming frames. On a heavily loaded network, we should be
  1347. * able to keep up at the expense of system resources.
  1348. */
  1349. writel(0, rxq->bd.reg_desc_active);
  1350. }
  1351. rxq->bd.cur = bdp;
  1352. return pkt_received;
  1353. }
  1354. static int
  1355. fec_enet_rx(struct net_device *ndev, int budget)
  1356. {
  1357. int pkt_received = 0;
  1358. u16 queue_id;
  1359. struct fec_enet_private *fep = netdev_priv(ndev);
  1360. for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
  1361. int ret;
  1362. ret = fec_enet_rx_queue(ndev,
  1363. budget - pkt_received, queue_id);
  1364. if (ret < budget - pkt_received)
  1365. clear_bit(queue_id, &fep->work_rx);
  1366. pkt_received += ret;
  1367. }
  1368. return pkt_received;
  1369. }
  1370. static bool
  1371. fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
  1372. {
  1373. if (int_events == 0)
  1374. return false;
  1375. if (int_events & FEC_ENET_RXF_0)
  1376. fep->work_rx |= (1 << 2);
  1377. if (int_events & FEC_ENET_RXF_1)
  1378. fep->work_rx |= (1 << 0);
  1379. if (int_events & FEC_ENET_RXF_2)
  1380. fep->work_rx |= (1 << 1);
  1381. if (int_events & FEC_ENET_TXF_0)
  1382. fep->work_tx |= (1 << 2);
  1383. if (int_events & FEC_ENET_TXF_1)
  1384. fep->work_tx |= (1 << 0);
  1385. if (int_events & FEC_ENET_TXF_2)
  1386. fep->work_tx |= (1 << 1);
  1387. return true;
  1388. }
  1389. static irqreturn_t
  1390. fec_enet_interrupt(int irq, void *dev_id)
  1391. {
  1392. struct net_device *ndev = dev_id;
  1393. struct fec_enet_private *fep = netdev_priv(ndev);
  1394. uint int_events;
  1395. irqreturn_t ret = IRQ_NONE;
  1396. int_events = readl(fep->hwp + FEC_IEVENT);
  1397. writel(int_events, fep->hwp + FEC_IEVENT);
  1398. fec_enet_collect_events(fep, int_events);
  1399. if ((fep->work_tx || fep->work_rx) && fep->link) {
  1400. ret = IRQ_HANDLED;
  1401. if (napi_schedule_prep(&fep->napi)) {
  1402. /* Disable the NAPI interrupts */
  1403. writel(FEC_NAPI_IMASK, fep->hwp + FEC_IMASK);
  1404. __napi_schedule(&fep->napi);
  1405. }
  1406. }
  1407. if (int_events & FEC_ENET_MII) {
  1408. ret = IRQ_HANDLED;
  1409. complete(&fep->mdio_done);
  1410. }
  1411. return ret;
  1412. }
  1413. static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
  1414. {
  1415. struct net_device *ndev = napi->dev;
  1416. struct fec_enet_private *fep = netdev_priv(ndev);
  1417. int pkts;
  1418. pkts = fec_enet_rx(ndev, budget);
  1419. fec_enet_tx(ndev);
  1420. if (pkts < budget) {
  1421. napi_complete_done(napi, pkts);
  1422. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  1423. }
  1424. return pkts;
  1425. }
  1426. /* ------------------------------------------------------------------------- */
  1427. static void fec_get_mac(struct net_device *ndev)
  1428. {
  1429. struct fec_enet_private *fep = netdev_priv(ndev);
  1430. struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
  1431. unsigned char *iap, tmpaddr[ETH_ALEN];
  1432. /*
  1433. * try to get mac address in following order:
  1434. *
  1435. * 1) module parameter via kernel command line in form
  1436. * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
  1437. */
  1438. iap = macaddr;
  1439. /*
  1440. * 2) from device tree data
  1441. */
  1442. if (!is_valid_ether_addr(iap)) {
  1443. struct device_node *np = fep->pdev->dev.of_node;
  1444. if (np) {
  1445. const char *mac = of_get_mac_address(np);
  1446. if (mac)
  1447. iap = (unsigned char *) mac;
  1448. }
  1449. }
  1450. /*
  1451. * 3) from flash or fuse (via platform data)
  1452. */
  1453. if (!is_valid_ether_addr(iap)) {
  1454. #ifdef CONFIG_M5272
  1455. if (FEC_FLASHMAC)
  1456. iap = (unsigned char *)FEC_FLASHMAC;
  1457. #else
  1458. if (pdata)
  1459. iap = (unsigned char *)&pdata->mac;
  1460. #endif
  1461. }
  1462. /*
  1463. * 4) FEC mac registers set by bootloader
  1464. */
  1465. if (!is_valid_ether_addr(iap)) {
  1466. *((__be32 *) &tmpaddr[0]) =
  1467. cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
  1468. *((__be16 *) &tmpaddr[4]) =
  1469. cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
  1470. iap = &tmpaddr[0];
  1471. }
  1472. /*
  1473. * 5) random mac address
  1474. */
  1475. if (!is_valid_ether_addr(iap)) {
  1476. /* Report it and use a random ethernet address instead */
  1477. dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap);
  1478. eth_hw_addr_random(ndev);
  1479. dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n",
  1480. ndev->dev_addr);
  1481. return;
  1482. }
  1483. memcpy(ndev->dev_addr, iap, ETH_ALEN);
  1484. /* Adjust MAC if using macaddr */
  1485. if (iap == macaddr)
  1486. ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
  1487. }
  1488. /* ------------------------------------------------------------------------- */
  1489. /*
  1490. * Phy section
  1491. */
  1492. static void fec_enet_adjust_link(struct net_device *ndev)
  1493. {
  1494. struct fec_enet_private *fep = netdev_priv(ndev);
  1495. struct phy_device *phy_dev = ndev->phydev;
  1496. int status_change = 0;
  1497. /* Prevent a state halted on mii error */
  1498. if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
  1499. phy_dev->state = PHY_RESUMING;
  1500. return;
  1501. }
  1502. /*
  1503. * If the netdev is down, or is going down, we're not interested
  1504. * in link state events, so just mark our idea of the link as down
  1505. * and ignore the event.
  1506. */
  1507. if (!netif_running(ndev) || !netif_device_present(ndev)) {
  1508. fep->link = 0;
  1509. } else if (phy_dev->link) {
  1510. if (!fep->link) {
  1511. fep->link = phy_dev->link;
  1512. status_change = 1;
  1513. }
  1514. if (fep->full_duplex != phy_dev->duplex) {
  1515. fep->full_duplex = phy_dev->duplex;
  1516. status_change = 1;
  1517. }
  1518. if (phy_dev->speed != fep->speed) {
  1519. fep->speed = phy_dev->speed;
  1520. status_change = 1;
  1521. }
  1522. /* if any of the above changed restart the FEC */
  1523. if (status_change) {
  1524. napi_disable(&fep->napi);
  1525. netif_tx_lock_bh(ndev);
  1526. fec_restart(ndev);
  1527. netif_tx_wake_all_queues(ndev);
  1528. netif_tx_unlock_bh(ndev);
  1529. napi_enable(&fep->napi);
  1530. }
  1531. } else {
  1532. if (fep->link) {
  1533. napi_disable(&fep->napi);
  1534. netif_tx_lock_bh(ndev);
  1535. fec_stop(ndev);
  1536. netif_tx_unlock_bh(ndev);
  1537. napi_enable(&fep->napi);
  1538. fep->link = phy_dev->link;
  1539. status_change = 1;
  1540. }
  1541. }
  1542. if (status_change)
  1543. phy_print_status(phy_dev);
  1544. }
  1545. static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  1546. {
  1547. struct fec_enet_private *fep = bus->priv;
  1548. struct device *dev = &fep->pdev->dev;
  1549. unsigned long time_left;
  1550. int ret = 0;
  1551. ret = pm_runtime_get_sync(dev);
  1552. if (ret < 0)
  1553. return ret;
  1554. fep->mii_timeout = 0;
  1555. reinit_completion(&fep->mdio_done);
  1556. /* start a read op */
  1557. writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
  1558. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  1559. FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
  1560. /* wait for end of transfer */
  1561. time_left = wait_for_completion_timeout(&fep->mdio_done,
  1562. usecs_to_jiffies(FEC_MII_TIMEOUT));
  1563. if (time_left == 0) {
  1564. fep->mii_timeout = 1;
  1565. netdev_err(fep->netdev, "MDIO read timeout\n");
  1566. ret = -ETIMEDOUT;
  1567. goto out;
  1568. }
  1569. ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
  1570. out:
  1571. pm_runtime_mark_last_busy(dev);
  1572. pm_runtime_put_autosuspend(dev);
  1573. return ret;
  1574. }
  1575. static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  1576. u16 value)
  1577. {
  1578. struct fec_enet_private *fep = bus->priv;
  1579. struct device *dev = &fep->pdev->dev;
  1580. unsigned long time_left;
  1581. int ret;
  1582. ret = pm_runtime_get_sync(dev);
  1583. if (ret < 0)
  1584. return ret;
  1585. else
  1586. ret = 0;
  1587. fep->mii_timeout = 0;
  1588. reinit_completion(&fep->mdio_done);
  1589. /* start a write op */
  1590. writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
  1591. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  1592. FEC_MMFR_TA | FEC_MMFR_DATA(value),
  1593. fep->hwp + FEC_MII_DATA);
  1594. /* wait for end of transfer */
  1595. time_left = wait_for_completion_timeout(&fep->mdio_done,
  1596. usecs_to_jiffies(FEC_MII_TIMEOUT));
  1597. if (time_left == 0) {
  1598. fep->mii_timeout = 1;
  1599. netdev_err(fep->netdev, "MDIO write timeout\n");
  1600. ret = -ETIMEDOUT;
  1601. }
  1602. pm_runtime_mark_last_busy(dev);
  1603. pm_runtime_put_autosuspend(dev);
  1604. return ret;
  1605. }
  1606. static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev)
  1607. {
  1608. struct fec_enet_private *fep = netdev_priv(ndev);
  1609. struct phy_device *phy_dev = ndev->phydev;
  1610. if (phy_dev) {
  1611. phy_reset_after_clk_enable(phy_dev);
  1612. } else if (fep->phy_node) {
  1613. /*
  1614. * If the PHY still is not bound to the MAC, but there is
  1615. * OF PHY node and a matching PHY device instance already,
  1616. * use the OF PHY node to obtain the PHY device instance,
  1617. * and then use that PHY device instance when triggering
  1618. * the PHY reset.
  1619. */
  1620. phy_dev = of_phy_find_device(fep->phy_node);
  1621. phy_reset_after_clk_enable(phy_dev);
  1622. put_device(&phy_dev->mdio.dev);
  1623. }
  1624. }
  1625. static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
  1626. {
  1627. struct fec_enet_private *fep = netdev_priv(ndev);
  1628. int ret;
  1629. if (enable) {
  1630. ret = clk_prepare_enable(fep->clk_enet_out);
  1631. if (ret)
  1632. return ret;
  1633. if (fep->clk_ptp) {
  1634. mutex_lock(&fep->ptp_clk_mutex);
  1635. ret = clk_prepare_enable(fep->clk_ptp);
  1636. if (ret) {
  1637. mutex_unlock(&fep->ptp_clk_mutex);
  1638. goto failed_clk_ptp;
  1639. } else {
  1640. fep->ptp_clk_on = true;
  1641. }
  1642. mutex_unlock(&fep->ptp_clk_mutex);
  1643. }
  1644. ret = clk_prepare_enable(fep->clk_ref);
  1645. if (ret)
  1646. goto failed_clk_ref;
  1647. fec_enet_phy_reset_after_clk_enable(ndev);
  1648. } else {
  1649. clk_disable_unprepare(fep->clk_enet_out);
  1650. if (fep->clk_ptp) {
  1651. mutex_lock(&fep->ptp_clk_mutex);
  1652. clk_disable_unprepare(fep->clk_ptp);
  1653. fep->ptp_clk_on = false;
  1654. mutex_unlock(&fep->ptp_clk_mutex);
  1655. }
  1656. clk_disable_unprepare(fep->clk_ref);
  1657. }
  1658. return 0;
  1659. failed_clk_ref:
  1660. if (fep->clk_ref)
  1661. clk_disable_unprepare(fep->clk_ref);
  1662. failed_clk_ptp:
  1663. if (fep->clk_enet_out)
  1664. clk_disable_unprepare(fep->clk_enet_out);
  1665. return ret;
  1666. }
  1667. static int fec_enet_mii_probe(struct net_device *ndev)
  1668. {
  1669. struct fec_enet_private *fep = netdev_priv(ndev);
  1670. struct phy_device *phy_dev = NULL;
  1671. char mdio_bus_id[MII_BUS_ID_SIZE];
  1672. char phy_name[MII_BUS_ID_SIZE + 3];
  1673. int phy_id;
  1674. int dev_id = fep->dev_id;
  1675. if (fep->phy_node) {
  1676. phy_dev = of_phy_connect(ndev, fep->phy_node,
  1677. &fec_enet_adjust_link, 0,
  1678. fep->phy_interface);
  1679. if (!phy_dev) {
  1680. netdev_err(ndev, "Unable to connect to phy\n");
  1681. return -ENODEV;
  1682. }
  1683. } else {
  1684. /* check for attached phy */
  1685. for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
  1686. if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
  1687. continue;
  1688. if (dev_id--)
  1689. continue;
  1690. strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
  1691. break;
  1692. }
  1693. if (phy_id >= PHY_MAX_ADDR) {
  1694. netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
  1695. strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
  1696. phy_id = 0;
  1697. }
  1698. snprintf(phy_name, sizeof(phy_name),
  1699. PHY_ID_FMT, mdio_bus_id, phy_id);
  1700. phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
  1701. fep->phy_interface);
  1702. }
  1703. if (IS_ERR(phy_dev)) {
  1704. netdev_err(ndev, "could not attach to PHY\n");
  1705. return PTR_ERR(phy_dev);
  1706. }
  1707. /* mask with MAC supported features */
  1708. if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
  1709. phy_dev->supported &= PHY_GBIT_FEATURES;
  1710. phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
  1711. #if !defined(CONFIG_M5272)
  1712. phy_dev->supported |= SUPPORTED_Pause;
  1713. #endif
  1714. }
  1715. else
  1716. phy_dev->supported &= PHY_BASIC_FEATURES;
  1717. phy_dev->advertising = phy_dev->supported;
  1718. fep->link = 0;
  1719. fep->full_duplex = 0;
  1720. phy_attached_info(phy_dev);
  1721. return 0;
  1722. }
  1723. static int fec_enet_mii_init(struct platform_device *pdev)
  1724. {
  1725. static struct mii_bus *fec0_mii_bus;
  1726. struct net_device *ndev = platform_get_drvdata(pdev);
  1727. struct fec_enet_private *fep = netdev_priv(ndev);
  1728. struct device_node *node;
  1729. int err = -ENXIO;
  1730. u32 mii_speed, holdtime;
  1731. /*
  1732. * The i.MX28 dual fec interfaces are not equal.
  1733. * Here are the differences:
  1734. *
  1735. * - fec0 supports MII & RMII modes while fec1 only supports RMII
  1736. * - fec0 acts as the 1588 time master while fec1 is slave
  1737. * - external phys can only be configured by fec0
  1738. *
  1739. * That is to say fec1 can not work independently. It only works
  1740. * when fec0 is working. The reason behind this design is that the
  1741. * second interface is added primarily for Switch mode.
  1742. *
  1743. * Because of the last point above, both phys are attached on fec0
  1744. * mdio interface in board design, and need to be configured by
  1745. * fec0 mii_bus.
  1746. */
  1747. if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
  1748. /* fec1 uses fec0 mii_bus */
  1749. if (mii_cnt && fec0_mii_bus) {
  1750. fep->mii_bus = fec0_mii_bus;
  1751. mii_cnt++;
  1752. return 0;
  1753. }
  1754. return -ENOENT;
  1755. }
  1756. fep->mii_timeout = 0;
  1757. /*
  1758. * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
  1759. *
  1760. * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
  1761. * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
  1762. * Reference Manual has an error on this, and gets fixed on i.MX6Q
  1763. * document.
  1764. */
  1765. mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
  1766. if (fep->quirks & FEC_QUIRK_ENET_MAC)
  1767. mii_speed--;
  1768. if (mii_speed > 63) {
  1769. dev_err(&pdev->dev,
  1770. "fec clock (%lu) too fast to get right mii speed\n",
  1771. clk_get_rate(fep->clk_ipg));
  1772. err = -EINVAL;
  1773. goto err_out;
  1774. }
  1775. /*
  1776. * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
  1777. * MII_SPEED) register that defines the MDIO output hold time. Earlier
  1778. * versions are RAZ there, so just ignore the difference and write the
  1779. * register always.
  1780. * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
  1781. * HOLDTIME + 1 is the number of clk cycles the fec is holding the
  1782. * output.
  1783. * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
  1784. * Given that ceil(clkrate / 5000000) <= 64, the calculation for
  1785. * holdtime cannot result in a value greater than 3.
  1786. */
  1787. holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
  1788. fep->phy_speed = mii_speed << 1 | holdtime << 8;
  1789. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1790. fep->mii_bus = mdiobus_alloc();
  1791. if (fep->mii_bus == NULL) {
  1792. err = -ENOMEM;
  1793. goto err_out;
  1794. }
  1795. fep->mii_bus->name = "fec_enet_mii_bus";
  1796. fep->mii_bus->read = fec_enet_mdio_read;
  1797. fep->mii_bus->write = fec_enet_mdio_write;
  1798. snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  1799. pdev->name, fep->dev_id + 1);
  1800. fep->mii_bus->priv = fep;
  1801. fep->mii_bus->parent = &pdev->dev;
  1802. node = of_get_child_by_name(pdev->dev.of_node, "mdio");
  1803. err = of_mdiobus_register(fep->mii_bus, node);
  1804. if (node)
  1805. of_node_put(node);
  1806. if (err)
  1807. goto err_out_free_mdiobus;
  1808. mii_cnt++;
  1809. /* save fec0 mii_bus */
  1810. if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
  1811. fec0_mii_bus = fep->mii_bus;
  1812. return 0;
  1813. err_out_free_mdiobus:
  1814. mdiobus_free(fep->mii_bus);
  1815. err_out:
  1816. return err;
  1817. }
  1818. static void fec_enet_mii_remove(struct fec_enet_private *fep)
  1819. {
  1820. if (--mii_cnt == 0) {
  1821. mdiobus_unregister(fep->mii_bus);
  1822. mdiobus_free(fep->mii_bus);
  1823. }
  1824. }
  1825. static void fec_enet_get_drvinfo(struct net_device *ndev,
  1826. struct ethtool_drvinfo *info)
  1827. {
  1828. struct fec_enet_private *fep = netdev_priv(ndev);
  1829. strlcpy(info->driver, fep->pdev->dev.driver->name,
  1830. sizeof(info->driver));
  1831. strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
  1832. strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
  1833. }
  1834. static int fec_enet_get_regs_len(struct net_device *ndev)
  1835. {
  1836. struct fec_enet_private *fep = netdev_priv(ndev);
  1837. struct resource *r;
  1838. int s = 0;
  1839. r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
  1840. if (r)
  1841. s = resource_size(r);
  1842. return s;
  1843. }
  1844. /* List of registers that can be safety be read to dump them with ethtool */
  1845. #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  1846. defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
  1847. defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST)
  1848. static u32 fec_enet_register_offset[] = {
  1849. FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
  1850. FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
  1851. FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
  1852. FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
  1853. FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
  1854. FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
  1855. FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
  1856. FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
  1857. FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
  1858. FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
  1859. FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
  1860. FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
  1861. RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
  1862. RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
  1863. RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
  1864. RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
  1865. RMON_T_P_GTE2048, RMON_T_OCTETS,
  1866. IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
  1867. IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
  1868. IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
  1869. RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
  1870. RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
  1871. RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
  1872. RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
  1873. RMON_R_P_GTE2048, RMON_R_OCTETS,
  1874. IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
  1875. IEEE_R_FDXFC, IEEE_R_OCTETS_OK
  1876. };
  1877. #else
  1878. static u32 fec_enet_register_offset[] = {
  1879. FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
  1880. FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
  1881. FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
  1882. FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
  1883. FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
  1884. FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
  1885. FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
  1886. FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
  1887. FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
  1888. };
  1889. #endif
  1890. static void fec_enet_get_regs(struct net_device *ndev,
  1891. struct ethtool_regs *regs, void *regbuf)
  1892. {
  1893. struct fec_enet_private *fep = netdev_priv(ndev);
  1894. u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
  1895. u32 *buf = (u32 *)regbuf;
  1896. u32 i, off;
  1897. memset(buf, 0, regs->len);
  1898. for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
  1899. off = fec_enet_register_offset[i];
  1900. if ((off == FEC_R_BOUND || off == FEC_R_FSTART) &&
  1901. !(fep->quirks & FEC_QUIRK_HAS_FRREG))
  1902. continue;
  1903. off >>= 2;
  1904. buf[off] = readl(&theregs[off]);
  1905. }
  1906. }
  1907. static int fec_enet_get_ts_info(struct net_device *ndev,
  1908. struct ethtool_ts_info *info)
  1909. {
  1910. struct fec_enet_private *fep = netdev_priv(ndev);
  1911. if (fep->bufdesc_ex) {
  1912. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  1913. SOF_TIMESTAMPING_RX_SOFTWARE |
  1914. SOF_TIMESTAMPING_SOFTWARE |
  1915. SOF_TIMESTAMPING_TX_HARDWARE |
  1916. SOF_TIMESTAMPING_RX_HARDWARE |
  1917. SOF_TIMESTAMPING_RAW_HARDWARE;
  1918. if (fep->ptp_clock)
  1919. info->phc_index = ptp_clock_index(fep->ptp_clock);
  1920. else
  1921. info->phc_index = -1;
  1922. info->tx_types = (1 << HWTSTAMP_TX_OFF) |
  1923. (1 << HWTSTAMP_TX_ON);
  1924. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  1925. (1 << HWTSTAMP_FILTER_ALL);
  1926. return 0;
  1927. } else {
  1928. return ethtool_op_get_ts_info(ndev, info);
  1929. }
  1930. }
  1931. #if !defined(CONFIG_M5272)
  1932. static void fec_enet_get_pauseparam(struct net_device *ndev,
  1933. struct ethtool_pauseparam *pause)
  1934. {
  1935. struct fec_enet_private *fep = netdev_priv(ndev);
  1936. pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
  1937. pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
  1938. pause->rx_pause = pause->tx_pause;
  1939. }
  1940. static int fec_enet_set_pauseparam(struct net_device *ndev,
  1941. struct ethtool_pauseparam *pause)
  1942. {
  1943. struct fec_enet_private *fep = netdev_priv(ndev);
  1944. if (!ndev->phydev)
  1945. return -ENODEV;
  1946. if (pause->tx_pause != pause->rx_pause) {
  1947. netdev_info(ndev,
  1948. "hardware only support enable/disable both tx and rx");
  1949. return -EINVAL;
  1950. }
  1951. fep->pause_flag = 0;
  1952. /* tx pause must be same as rx pause */
  1953. fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
  1954. fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
  1955. if (pause->rx_pause || pause->autoneg) {
  1956. ndev->phydev->supported |= ADVERTISED_Pause;
  1957. ndev->phydev->advertising |= ADVERTISED_Pause;
  1958. } else {
  1959. ndev->phydev->supported &= ~ADVERTISED_Pause;
  1960. ndev->phydev->advertising &= ~ADVERTISED_Pause;
  1961. }
  1962. if (pause->autoneg) {
  1963. if (netif_running(ndev))
  1964. fec_stop(ndev);
  1965. phy_start_aneg(ndev->phydev);
  1966. }
  1967. if (netif_running(ndev)) {
  1968. napi_disable(&fep->napi);
  1969. netif_tx_lock_bh(ndev);
  1970. fec_restart(ndev);
  1971. netif_tx_wake_all_queues(ndev);
  1972. netif_tx_unlock_bh(ndev);
  1973. napi_enable(&fep->napi);
  1974. }
  1975. return 0;
  1976. }
  1977. static const struct fec_stat {
  1978. char name[ETH_GSTRING_LEN];
  1979. u16 offset;
  1980. } fec_stats[] = {
  1981. /* RMON TX */
  1982. { "tx_dropped", RMON_T_DROP },
  1983. { "tx_packets", RMON_T_PACKETS },
  1984. { "tx_broadcast", RMON_T_BC_PKT },
  1985. { "tx_multicast", RMON_T_MC_PKT },
  1986. { "tx_crc_errors", RMON_T_CRC_ALIGN },
  1987. { "tx_undersize", RMON_T_UNDERSIZE },
  1988. { "tx_oversize", RMON_T_OVERSIZE },
  1989. { "tx_fragment", RMON_T_FRAG },
  1990. { "tx_jabber", RMON_T_JAB },
  1991. { "tx_collision", RMON_T_COL },
  1992. { "tx_64byte", RMON_T_P64 },
  1993. { "tx_65to127byte", RMON_T_P65TO127 },
  1994. { "tx_128to255byte", RMON_T_P128TO255 },
  1995. { "tx_256to511byte", RMON_T_P256TO511 },
  1996. { "tx_512to1023byte", RMON_T_P512TO1023 },
  1997. { "tx_1024to2047byte", RMON_T_P1024TO2047 },
  1998. { "tx_GTE2048byte", RMON_T_P_GTE2048 },
  1999. { "tx_octets", RMON_T_OCTETS },
  2000. /* IEEE TX */
  2001. { "IEEE_tx_drop", IEEE_T_DROP },
  2002. { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
  2003. { "IEEE_tx_1col", IEEE_T_1COL },
  2004. { "IEEE_tx_mcol", IEEE_T_MCOL },
  2005. { "IEEE_tx_def", IEEE_T_DEF },
  2006. { "IEEE_tx_lcol", IEEE_T_LCOL },
  2007. { "IEEE_tx_excol", IEEE_T_EXCOL },
  2008. { "IEEE_tx_macerr", IEEE_T_MACERR },
  2009. { "IEEE_tx_cserr", IEEE_T_CSERR },
  2010. { "IEEE_tx_sqe", IEEE_T_SQE },
  2011. { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
  2012. { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
  2013. /* RMON RX */
  2014. { "rx_packets", RMON_R_PACKETS },
  2015. { "rx_broadcast", RMON_R_BC_PKT },
  2016. { "rx_multicast", RMON_R_MC_PKT },
  2017. { "rx_crc_errors", RMON_R_CRC_ALIGN },
  2018. { "rx_undersize", RMON_R_UNDERSIZE },
  2019. { "rx_oversize", RMON_R_OVERSIZE },
  2020. { "rx_fragment", RMON_R_FRAG },
  2021. { "rx_jabber", RMON_R_JAB },
  2022. { "rx_64byte", RMON_R_P64 },
  2023. { "rx_65to127byte", RMON_R_P65TO127 },
  2024. { "rx_128to255byte", RMON_R_P128TO255 },
  2025. { "rx_256to511byte", RMON_R_P256TO511 },
  2026. { "rx_512to1023byte", RMON_R_P512TO1023 },
  2027. { "rx_1024to2047byte", RMON_R_P1024TO2047 },
  2028. { "rx_GTE2048byte", RMON_R_P_GTE2048 },
  2029. { "rx_octets", RMON_R_OCTETS },
  2030. /* IEEE RX */
  2031. { "IEEE_rx_drop", IEEE_R_DROP },
  2032. { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
  2033. { "IEEE_rx_crc", IEEE_R_CRC },
  2034. { "IEEE_rx_align", IEEE_R_ALIGN },
  2035. { "IEEE_rx_macerr", IEEE_R_MACERR },
  2036. { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
  2037. { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
  2038. };
  2039. #define FEC_STATS_SIZE (ARRAY_SIZE(fec_stats) * sizeof(u64))
  2040. static void fec_enet_update_ethtool_stats(struct net_device *dev)
  2041. {
  2042. struct fec_enet_private *fep = netdev_priv(dev);
  2043. int i;
  2044. for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
  2045. fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
  2046. }
  2047. static void fec_enet_get_ethtool_stats(struct net_device *dev,
  2048. struct ethtool_stats *stats, u64 *data)
  2049. {
  2050. struct fec_enet_private *fep = netdev_priv(dev);
  2051. if (netif_running(dev))
  2052. fec_enet_update_ethtool_stats(dev);
  2053. memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
  2054. }
  2055. static void fec_enet_get_strings(struct net_device *netdev,
  2056. u32 stringset, u8 *data)
  2057. {
  2058. int i;
  2059. switch (stringset) {
  2060. case ETH_SS_STATS:
  2061. for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
  2062. memcpy(data + i * ETH_GSTRING_LEN,
  2063. fec_stats[i].name, ETH_GSTRING_LEN);
  2064. break;
  2065. }
  2066. }
  2067. static int fec_enet_get_sset_count(struct net_device *dev, int sset)
  2068. {
  2069. switch (sset) {
  2070. case ETH_SS_STATS:
  2071. return ARRAY_SIZE(fec_stats);
  2072. default:
  2073. return -EOPNOTSUPP;
  2074. }
  2075. }
  2076. static void fec_enet_clear_ethtool_stats(struct net_device *dev)
  2077. {
  2078. struct fec_enet_private *fep = netdev_priv(dev);
  2079. int i;
  2080. /* Disable MIB statistics counters */
  2081. writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
  2082. for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
  2083. writel(0, fep->hwp + fec_stats[i].offset);
  2084. /* Don't disable MIB statistics counters */
  2085. writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
  2086. }
  2087. #else /* !defined(CONFIG_M5272) */
  2088. #define FEC_STATS_SIZE 0
  2089. static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
  2090. {
  2091. }
  2092. static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
  2093. {
  2094. }
  2095. #endif /* !defined(CONFIG_M5272) */
  2096. /* ITR clock source is enet system clock (clk_ahb).
  2097. * TCTT unit is cycle_ns * 64 cycle
  2098. * So, the ICTT value = X us / (cycle_ns * 64)
  2099. */
  2100. static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
  2101. {
  2102. struct fec_enet_private *fep = netdev_priv(ndev);
  2103. return us * (fep->itr_clk_rate / 64000) / 1000;
  2104. }
  2105. /* Set threshold for interrupt coalescing */
  2106. static void fec_enet_itr_coal_set(struct net_device *ndev)
  2107. {
  2108. struct fec_enet_private *fep = netdev_priv(ndev);
  2109. int rx_itr, tx_itr;
  2110. /* Must be greater than zero to avoid unpredictable behavior */
  2111. if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
  2112. !fep->tx_time_itr || !fep->tx_pkts_itr)
  2113. return;
  2114. /* Select enet system clock as Interrupt Coalescing
  2115. * timer Clock Source
  2116. */
  2117. rx_itr = FEC_ITR_CLK_SEL;
  2118. tx_itr = FEC_ITR_CLK_SEL;
  2119. /* set ICFT and ICTT */
  2120. rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
  2121. rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
  2122. tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
  2123. tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
  2124. rx_itr |= FEC_ITR_EN;
  2125. tx_itr |= FEC_ITR_EN;
  2126. writel(tx_itr, fep->hwp + FEC_TXIC0);
  2127. writel(rx_itr, fep->hwp + FEC_RXIC0);
  2128. if (fep->quirks & FEC_QUIRK_HAS_AVB) {
  2129. writel(tx_itr, fep->hwp + FEC_TXIC1);
  2130. writel(rx_itr, fep->hwp + FEC_RXIC1);
  2131. writel(tx_itr, fep->hwp + FEC_TXIC2);
  2132. writel(rx_itr, fep->hwp + FEC_RXIC2);
  2133. }
  2134. }
  2135. static int
  2136. fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
  2137. {
  2138. struct fec_enet_private *fep = netdev_priv(ndev);
  2139. if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
  2140. return -EOPNOTSUPP;
  2141. ec->rx_coalesce_usecs = fep->rx_time_itr;
  2142. ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
  2143. ec->tx_coalesce_usecs = fep->tx_time_itr;
  2144. ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
  2145. return 0;
  2146. }
  2147. static int
  2148. fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
  2149. {
  2150. struct fec_enet_private *fep = netdev_priv(ndev);
  2151. unsigned int cycle;
  2152. if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
  2153. return -EOPNOTSUPP;
  2154. if (ec->rx_max_coalesced_frames > 255) {
  2155. pr_err("Rx coalesced frames exceed hardware limitation\n");
  2156. return -EINVAL;
  2157. }
  2158. if (ec->tx_max_coalesced_frames > 255) {
  2159. pr_err("Tx coalesced frame exceed hardware limitation\n");
  2160. return -EINVAL;
  2161. }
  2162. cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs);
  2163. if (cycle > 0xFFFF) {
  2164. pr_err("Rx coalesced usec exceed hardware limitation\n");
  2165. return -EINVAL;
  2166. }
  2167. cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs);
  2168. if (cycle > 0xFFFF) {
  2169. pr_err("Tx coalesced usec exceed hardware limitation\n");
  2170. return -EINVAL;
  2171. }
  2172. fep->rx_time_itr = ec->rx_coalesce_usecs;
  2173. fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
  2174. fep->tx_time_itr = ec->tx_coalesce_usecs;
  2175. fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
  2176. fec_enet_itr_coal_set(ndev);
  2177. return 0;
  2178. }
  2179. static void fec_enet_itr_coal_init(struct net_device *ndev)
  2180. {
  2181. struct ethtool_coalesce ec;
  2182. ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
  2183. ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
  2184. ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
  2185. ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
  2186. fec_enet_set_coalesce(ndev, &ec);
  2187. }
  2188. static int fec_enet_get_tunable(struct net_device *netdev,
  2189. const struct ethtool_tunable *tuna,
  2190. void *data)
  2191. {
  2192. struct fec_enet_private *fep = netdev_priv(netdev);
  2193. int ret = 0;
  2194. switch (tuna->id) {
  2195. case ETHTOOL_RX_COPYBREAK:
  2196. *(u32 *)data = fep->rx_copybreak;
  2197. break;
  2198. default:
  2199. ret = -EINVAL;
  2200. break;
  2201. }
  2202. return ret;
  2203. }
  2204. static int fec_enet_set_tunable(struct net_device *netdev,
  2205. const struct ethtool_tunable *tuna,
  2206. const void *data)
  2207. {
  2208. struct fec_enet_private *fep = netdev_priv(netdev);
  2209. int ret = 0;
  2210. switch (tuna->id) {
  2211. case ETHTOOL_RX_COPYBREAK:
  2212. fep->rx_copybreak = *(u32 *)data;
  2213. break;
  2214. default:
  2215. ret = -EINVAL;
  2216. break;
  2217. }
  2218. return ret;
  2219. }
  2220. static void
  2221. fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  2222. {
  2223. struct fec_enet_private *fep = netdev_priv(ndev);
  2224. if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
  2225. wol->supported = WAKE_MAGIC;
  2226. wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
  2227. } else {
  2228. wol->supported = wol->wolopts = 0;
  2229. }
  2230. }
  2231. static int
  2232. fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  2233. {
  2234. struct fec_enet_private *fep = netdev_priv(ndev);
  2235. if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
  2236. return -EINVAL;
  2237. if (wol->wolopts & ~WAKE_MAGIC)
  2238. return -EINVAL;
  2239. device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
  2240. if (device_may_wakeup(&ndev->dev)) {
  2241. fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
  2242. if (fep->irq[0] > 0)
  2243. enable_irq_wake(fep->irq[0]);
  2244. } else {
  2245. fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
  2246. if (fep->irq[0] > 0)
  2247. disable_irq_wake(fep->irq[0]);
  2248. }
  2249. return 0;
  2250. }
  2251. static const struct ethtool_ops fec_enet_ethtool_ops = {
  2252. .get_drvinfo = fec_enet_get_drvinfo,
  2253. .get_regs_len = fec_enet_get_regs_len,
  2254. .get_regs = fec_enet_get_regs,
  2255. .nway_reset = phy_ethtool_nway_reset,
  2256. .get_link = ethtool_op_get_link,
  2257. .get_coalesce = fec_enet_get_coalesce,
  2258. .set_coalesce = fec_enet_set_coalesce,
  2259. #ifndef CONFIG_M5272
  2260. .get_pauseparam = fec_enet_get_pauseparam,
  2261. .set_pauseparam = fec_enet_set_pauseparam,
  2262. .get_strings = fec_enet_get_strings,
  2263. .get_ethtool_stats = fec_enet_get_ethtool_stats,
  2264. .get_sset_count = fec_enet_get_sset_count,
  2265. #endif
  2266. .get_ts_info = fec_enet_get_ts_info,
  2267. .get_tunable = fec_enet_get_tunable,
  2268. .set_tunable = fec_enet_set_tunable,
  2269. .get_wol = fec_enet_get_wol,
  2270. .set_wol = fec_enet_set_wol,
  2271. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2272. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2273. };
  2274. static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  2275. {
  2276. struct fec_enet_private *fep = netdev_priv(ndev);
  2277. struct phy_device *phydev = ndev->phydev;
  2278. if (!netif_running(ndev))
  2279. return -EINVAL;
  2280. if (!phydev)
  2281. return -ENODEV;
  2282. if (fep->bufdesc_ex) {
  2283. if (cmd == SIOCSHWTSTAMP)
  2284. return fec_ptp_set(ndev, rq);
  2285. if (cmd == SIOCGHWTSTAMP)
  2286. return fec_ptp_get(ndev, rq);
  2287. }
  2288. return phy_mii_ioctl(phydev, rq, cmd);
  2289. }
  2290. static void fec_enet_free_buffers(struct net_device *ndev)
  2291. {
  2292. struct fec_enet_private *fep = netdev_priv(ndev);
  2293. unsigned int i;
  2294. struct sk_buff *skb;
  2295. struct bufdesc *bdp;
  2296. struct fec_enet_priv_tx_q *txq;
  2297. struct fec_enet_priv_rx_q *rxq;
  2298. unsigned int q;
  2299. for (q = 0; q < fep->num_rx_queues; q++) {
  2300. rxq = fep->rx_queue[q];
  2301. bdp = rxq->bd.base;
  2302. for (i = 0; i < rxq->bd.ring_size; i++) {
  2303. skb = rxq->rx_skbuff[i];
  2304. rxq->rx_skbuff[i] = NULL;
  2305. if (skb) {
  2306. dma_unmap_single(&fep->pdev->dev,
  2307. fec32_to_cpu(bdp->cbd_bufaddr),
  2308. FEC_ENET_RX_FRSIZE - fep->rx_align,
  2309. DMA_FROM_DEVICE);
  2310. dev_kfree_skb(skb);
  2311. }
  2312. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  2313. }
  2314. }
  2315. for (q = 0; q < fep->num_tx_queues; q++) {
  2316. txq = fep->tx_queue[q];
  2317. bdp = txq->bd.base;
  2318. for (i = 0; i < txq->bd.ring_size; i++) {
  2319. kfree(txq->tx_bounce[i]);
  2320. txq->tx_bounce[i] = NULL;
  2321. skb = txq->tx_skbuff[i];
  2322. txq->tx_skbuff[i] = NULL;
  2323. dev_kfree_skb(skb);
  2324. }
  2325. }
  2326. }
  2327. static void fec_enet_free_queue(struct net_device *ndev)
  2328. {
  2329. struct fec_enet_private *fep = netdev_priv(ndev);
  2330. int i;
  2331. struct fec_enet_priv_tx_q *txq;
  2332. for (i = 0; i < fep->num_tx_queues; i++)
  2333. if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
  2334. txq = fep->tx_queue[i];
  2335. dma_free_coherent(&fep->pdev->dev,
  2336. txq->bd.ring_size * TSO_HEADER_SIZE,
  2337. txq->tso_hdrs,
  2338. txq->tso_hdrs_dma);
  2339. }
  2340. for (i = 0; i < fep->num_rx_queues; i++)
  2341. kfree(fep->rx_queue[i]);
  2342. for (i = 0; i < fep->num_tx_queues; i++)
  2343. kfree(fep->tx_queue[i]);
  2344. }
  2345. static int fec_enet_alloc_queue(struct net_device *ndev)
  2346. {
  2347. struct fec_enet_private *fep = netdev_priv(ndev);
  2348. int i;
  2349. int ret = 0;
  2350. struct fec_enet_priv_tx_q *txq;
  2351. for (i = 0; i < fep->num_tx_queues; i++) {
  2352. txq = kzalloc(sizeof(*txq), GFP_KERNEL);
  2353. if (!txq) {
  2354. ret = -ENOMEM;
  2355. goto alloc_failed;
  2356. }
  2357. fep->tx_queue[i] = txq;
  2358. txq->bd.ring_size = TX_RING_SIZE;
  2359. fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
  2360. txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
  2361. txq->tx_wake_threshold =
  2362. (txq->bd.ring_size - txq->tx_stop_threshold) / 2;
  2363. txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
  2364. txq->bd.ring_size * TSO_HEADER_SIZE,
  2365. &txq->tso_hdrs_dma,
  2366. GFP_KERNEL);
  2367. if (!txq->tso_hdrs) {
  2368. ret = -ENOMEM;
  2369. goto alloc_failed;
  2370. }
  2371. }
  2372. for (i = 0; i < fep->num_rx_queues; i++) {
  2373. fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
  2374. GFP_KERNEL);
  2375. if (!fep->rx_queue[i]) {
  2376. ret = -ENOMEM;
  2377. goto alloc_failed;
  2378. }
  2379. fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
  2380. fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
  2381. }
  2382. return ret;
  2383. alloc_failed:
  2384. fec_enet_free_queue(ndev);
  2385. return ret;
  2386. }
  2387. static int
  2388. fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
  2389. {
  2390. struct fec_enet_private *fep = netdev_priv(ndev);
  2391. unsigned int i;
  2392. struct sk_buff *skb;
  2393. struct bufdesc *bdp;
  2394. struct fec_enet_priv_rx_q *rxq;
  2395. rxq = fep->rx_queue[queue];
  2396. bdp = rxq->bd.base;
  2397. for (i = 0; i < rxq->bd.ring_size; i++) {
  2398. skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
  2399. if (!skb)
  2400. goto err_alloc;
  2401. if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
  2402. dev_kfree_skb(skb);
  2403. goto err_alloc;
  2404. }
  2405. rxq->rx_skbuff[i] = skb;
  2406. bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
  2407. if (fep->bufdesc_ex) {
  2408. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  2409. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
  2410. }
  2411. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  2412. }
  2413. /* Set the last buffer to wrap. */
  2414. bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
  2415. bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
  2416. return 0;
  2417. err_alloc:
  2418. fec_enet_free_buffers(ndev);
  2419. return -ENOMEM;
  2420. }
  2421. static int
  2422. fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
  2423. {
  2424. struct fec_enet_private *fep = netdev_priv(ndev);
  2425. unsigned int i;
  2426. struct bufdesc *bdp;
  2427. struct fec_enet_priv_tx_q *txq;
  2428. txq = fep->tx_queue[queue];
  2429. bdp = txq->bd.base;
  2430. for (i = 0; i < txq->bd.ring_size; i++) {
  2431. txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
  2432. if (!txq->tx_bounce[i])
  2433. goto err_alloc;
  2434. bdp->cbd_sc = cpu_to_fec16(0);
  2435. bdp->cbd_bufaddr = cpu_to_fec32(0);
  2436. if (fep->bufdesc_ex) {
  2437. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  2438. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
  2439. }
  2440. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  2441. }
  2442. /* Set the last buffer to wrap. */
  2443. bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
  2444. bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
  2445. return 0;
  2446. err_alloc:
  2447. fec_enet_free_buffers(ndev);
  2448. return -ENOMEM;
  2449. }
  2450. static int fec_enet_alloc_buffers(struct net_device *ndev)
  2451. {
  2452. struct fec_enet_private *fep = netdev_priv(ndev);
  2453. unsigned int i;
  2454. for (i = 0; i < fep->num_rx_queues; i++)
  2455. if (fec_enet_alloc_rxq_buffers(ndev, i))
  2456. return -ENOMEM;
  2457. for (i = 0; i < fep->num_tx_queues; i++)
  2458. if (fec_enet_alloc_txq_buffers(ndev, i))
  2459. return -ENOMEM;
  2460. return 0;
  2461. }
  2462. static int
  2463. fec_enet_open(struct net_device *ndev)
  2464. {
  2465. struct fec_enet_private *fep = netdev_priv(ndev);
  2466. int ret;
  2467. bool reset_again;
  2468. ret = pm_runtime_get_sync(&fep->pdev->dev);
  2469. if (ret < 0)
  2470. return ret;
  2471. pinctrl_pm_select_default_state(&fep->pdev->dev);
  2472. ret = fec_enet_clk_enable(ndev, true);
  2473. if (ret)
  2474. goto clk_enable;
  2475. /* During the first fec_enet_open call the PHY isn't probed at this
  2476. * point. Therefore the phy_reset_after_clk_enable() call within
  2477. * fec_enet_clk_enable() fails. As we need this reset in order to be
  2478. * sure the PHY is working correctly we check if we need to reset again
  2479. * later when the PHY is probed
  2480. */
  2481. if (ndev->phydev && ndev->phydev->drv)
  2482. reset_again = false;
  2483. else
  2484. reset_again = true;
  2485. /* I should reset the ring buffers here, but I don't yet know
  2486. * a simple way to do that.
  2487. */
  2488. ret = fec_enet_alloc_buffers(ndev);
  2489. if (ret)
  2490. goto err_enet_alloc;
  2491. /* Init MAC prior to mii bus probe */
  2492. fec_restart(ndev);
  2493. /* Call phy_reset_after_clk_enable() again if it failed during
  2494. * phy_reset_after_clk_enable() before because the PHY wasn't probed.
  2495. */
  2496. if (reset_again)
  2497. fec_enet_phy_reset_after_clk_enable(ndev);
  2498. /* Probe and connect to PHY when open the interface */
  2499. ret = fec_enet_mii_probe(ndev);
  2500. if (ret)
  2501. goto err_enet_mii_probe;
  2502. if (fep->quirks & FEC_QUIRK_ERR006687)
  2503. imx6q_cpuidle_fec_irqs_used();
  2504. napi_enable(&fep->napi);
  2505. phy_start(ndev->phydev);
  2506. netif_tx_start_all_queues(ndev);
  2507. device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
  2508. FEC_WOL_FLAG_ENABLE);
  2509. return 0;
  2510. err_enet_mii_probe:
  2511. fec_enet_free_buffers(ndev);
  2512. err_enet_alloc:
  2513. fec_enet_clk_enable(ndev, false);
  2514. clk_enable:
  2515. pm_runtime_mark_last_busy(&fep->pdev->dev);
  2516. pm_runtime_put_autosuspend(&fep->pdev->dev);
  2517. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  2518. return ret;
  2519. }
  2520. static int
  2521. fec_enet_close(struct net_device *ndev)
  2522. {
  2523. struct fec_enet_private *fep = netdev_priv(ndev);
  2524. phy_stop(ndev->phydev);
  2525. if (netif_device_present(ndev)) {
  2526. napi_disable(&fep->napi);
  2527. netif_tx_disable(ndev);
  2528. fec_stop(ndev);
  2529. }
  2530. phy_disconnect(ndev->phydev);
  2531. if (fep->quirks & FEC_QUIRK_ERR006687)
  2532. imx6q_cpuidle_fec_irqs_unused();
  2533. fec_enet_update_ethtool_stats(ndev);
  2534. fec_enet_clk_enable(ndev, false);
  2535. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  2536. pm_runtime_mark_last_busy(&fep->pdev->dev);
  2537. pm_runtime_put_autosuspend(&fep->pdev->dev);
  2538. fec_enet_free_buffers(ndev);
  2539. return 0;
  2540. }
  2541. /* Set or clear the multicast filter for this adaptor.
  2542. * Skeleton taken from sunlance driver.
  2543. * The CPM Ethernet implementation allows Multicast as well as individual
  2544. * MAC address filtering. Some of the drivers check to make sure it is
  2545. * a group multicast address, and discard those that are not. I guess I
  2546. * will do the same for now, but just remove the test if you want
  2547. * individual filtering as well (do the upper net layers want or support
  2548. * this kind of feature?).
  2549. */
  2550. #define FEC_HASH_BITS 6 /* #bits in hash */
  2551. static void set_multicast_list(struct net_device *ndev)
  2552. {
  2553. struct fec_enet_private *fep = netdev_priv(ndev);
  2554. struct netdev_hw_addr *ha;
  2555. unsigned int crc, tmp;
  2556. unsigned char hash;
  2557. unsigned int hash_high = 0, hash_low = 0;
  2558. if (ndev->flags & IFF_PROMISC) {
  2559. tmp = readl(fep->hwp + FEC_R_CNTRL);
  2560. tmp |= 0x8;
  2561. writel(tmp, fep->hwp + FEC_R_CNTRL);
  2562. return;
  2563. }
  2564. tmp = readl(fep->hwp + FEC_R_CNTRL);
  2565. tmp &= ~0x8;
  2566. writel(tmp, fep->hwp + FEC_R_CNTRL);
  2567. if (ndev->flags & IFF_ALLMULTI) {
  2568. /* Catch all multicast addresses, so set the
  2569. * filter to all 1's
  2570. */
  2571. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  2572. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  2573. return;
  2574. }
  2575. /* Add the addresses in hash register */
  2576. netdev_for_each_mc_addr(ha, ndev) {
  2577. /* calculate crc32 value of mac address */
  2578. crc = ether_crc_le(ndev->addr_len, ha->addr);
  2579. /* only upper 6 bits (FEC_HASH_BITS) are used
  2580. * which point to specific bit in the hash registers
  2581. */
  2582. hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
  2583. if (hash > 31)
  2584. hash_high |= 1 << (hash - 32);
  2585. else
  2586. hash_low |= 1 << hash;
  2587. }
  2588. writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  2589. writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  2590. }
  2591. /* Set a MAC change in hardware. */
  2592. static int
  2593. fec_set_mac_address(struct net_device *ndev, void *p)
  2594. {
  2595. struct fec_enet_private *fep = netdev_priv(ndev);
  2596. struct sockaddr *addr = p;
  2597. if (addr) {
  2598. if (!is_valid_ether_addr(addr->sa_data))
  2599. return -EADDRNOTAVAIL;
  2600. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  2601. }
  2602. /* Add netif status check here to avoid system hang in below case:
  2603. * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
  2604. * After ethx down, fec all clocks are gated off and then register
  2605. * access causes system hang.
  2606. */
  2607. if (!netif_running(ndev))
  2608. return 0;
  2609. writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
  2610. (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
  2611. fep->hwp + FEC_ADDR_LOW);
  2612. writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
  2613. fep->hwp + FEC_ADDR_HIGH);
  2614. return 0;
  2615. }
  2616. #ifdef CONFIG_NET_POLL_CONTROLLER
  2617. /**
  2618. * fec_poll_controller - FEC Poll controller function
  2619. * @dev: The FEC network adapter
  2620. *
  2621. * Polled functionality used by netconsole and others in non interrupt mode
  2622. *
  2623. */
  2624. static void fec_poll_controller(struct net_device *dev)
  2625. {
  2626. int i;
  2627. struct fec_enet_private *fep = netdev_priv(dev);
  2628. for (i = 0; i < FEC_IRQ_NUM; i++) {
  2629. if (fep->irq[i] > 0) {
  2630. disable_irq(fep->irq[i]);
  2631. fec_enet_interrupt(fep->irq[i], dev);
  2632. enable_irq(fep->irq[i]);
  2633. }
  2634. }
  2635. }
  2636. #endif
  2637. static inline void fec_enet_set_netdev_features(struct net_device *netdev,
  2638. netdev_features_t features)
  2639. {
  2640. struct fec_enet_private *fep = netdev_priv(netdev);
  2641. netdev_features_t changed = features ^ netdev->features;
  2642. netdev->features = features;
  2643. /* Receive checksum has been changed */
  2644. if (changed & NETIF_F_RXCSUM) {
  2645. if (features & NETIF_F_RXCSUM)
  2646. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  2647. else
  2648. fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
  2649. }
  2650. }
  2651. static int fec_set_features(struct net_device *netdev,
  2652. netdev_features_t features)
  2653. {
  2654. struct fec_enet_private *fep = netdev_priv(netdev);
  2655. netdev_features_t changed = features ^ netdev->features;
  2656. if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
  2657. napi_disable(&fep->napi);
  2658. netif_tx_lock_bh(netdev);
  2659. fec_stop(netdev);
  2660. fec_enet_set_netdev_features(netdev, features);
  2661. fec_restart(netdev);
  2662. netif_tx_wake_all_queues(netdev);
  2663. netif_tx_unlock_bh(netdev);
  2664. napi_enable(&fep->napi);
  2665. } else {
  2666. fec_enet_set_netdev_features(netdev, features);
  2667. }
  2668. return 0;
  2669. }
  2670. static const struct net_device_ops fec_netdev_ops = {
  2671. .ndo_open = fec_enet_open,
  2672. .ndo_stop = fec_enet_close,
  2673. .ndo_start_xmit = fec_enet_start_xmit,
  2674. .ndo_set_rx_mode = set_multicast_list,
  2675. .ndo_validate_addr = eth_validate_addr,
  2676. .ndo_tx_timeout = fec_timeout,
  2677. .ndo_set_mac_address = fec_set_mac_address,
  2678. .ndo_do_ioctl = fec_enet_ioctl,
  2679. #ifdef CONFIG_NET_POLL_CONTROLLER
  2680. .ndo_poll_controller = fec_poll_controller,
  2681. #endif
  2682. .ndo_set_features = fec_set_features,
  2683. };
  2684. static const unsigned short offset_des_active_rxq[] = {
  2685. FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
  2686. };
  2687. static const unsigned short offset_des_active_txq[] = {
  2688. FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
  2689. };
  2690. /*
  2691. * XXX: We need to clean up on failure exits here.
  2692. *
  2693. */
  2694. static int fec_enet_init(struct net_device *ndev)
  2695. {
  2696. struct fec_enet_private *fep = netdev_priv(ndev);
  2697. struct bufdesc *cbd_base;
  2698. dma_addr_t bd_dma;
  2699. int bd_size;
  2700. unsigned int i;
  2701. unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
  2702. sizeof(struct bufdesc);
  2703. unsigned dsize_log2 = __fls(dsize);
  2704. int ret;
  2705. WARN_ON(dsize != (1 << dsize_log2));
  2706. #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
  2707. fep->rx_align = 0xf;
  2708. fep->tx_align = 0xf;
  2709. #else
  2710. fep->rx_align = 0x3;
  2711. fep->tx_align = 0x3;
  2712. #endif
  2713. /* Check mask of the streaming and coherent API */
  2714. ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32));
  2715. if (ret < 0) {
  2716. dev_warn(&fep->pdev->dev, "No suitable DMA available\n");
  2717. return ret;
  2718. }
  2719. fec_enet_alloc_queue(ndev);
  2720. bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
  2721. /* Allocate memory for buffer descriptors. */
  2722. cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
  2723. GFP_KERNEL);
  2724. if (!cbd_base) {
  2725. return -ENOMEM;
  2726. }
  2727. memset(cbd_base, 0, bd_size);
  2728. /* Get the Ethernet address */
  2729. fec_get_mac(ndev);
  2730. /* make sure MAC we just acquired is programmed into the hw */
  2731. fec_set_mac_address(ndev, NULL);
  2732. /* Set receive and transmit descriptor base. */
  2733. for (i = 0; i < fep->num_rx_queues; i++) {
  2734. struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
  2735. unsigned size = dsize * rxq->bd.ring_size;
  2736. rxq->bd.qid = i;
  2737. rxq->bd.base = cbd_base;
  2738. rxq->bd.cur = cbd_base;
  2739. rxq->bd.dma = bd_dma;
  2740. rxq->bd.dsize = dsize;
  2741. rxq->bd.dsize_log2 = dsize_log2;
  2742. rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
  2743. bd_dma += size;
  2744. cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
  2745. rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
  2746. }
  2747. for (i = 0; i < fep->num_tx_queues; i++) {
  2748. struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
  2749. unsigned size = dsize * txq->bd.ring_size;
  2750. txq->bd.qid = i;
  2751. txq->bd.base = cbd_base;
  2752. txq->bd.cur = cbd_base;
  2753. txq->bd.dma = bd_dma;
  2754. txq->bd.dsize = dsize;
  2755. txq->bd.dsize_log2 = dsize_log2;
  2756. txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
  2757. bd_dma += size;
  2758. cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
  2759. txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
  2760. }
  2761. /* The FEC Ethernet specific entries in the device structure */
  2762. ndev->watchdog_timeo = TX_TIMEOUT;
  2763. ndev->netdev_ops = &fec_netdev_ops;
  2764. ndev->ethtool_ops = &fec_enet_ethtool_ops;
  2765. writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
  2766. netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
  2767. if (fep->quirks & FEC_QUIRK_HAS_VLAN)
  2768. /* enable hw VLAN support */
  2769. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  2770. if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
  2771. ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
  2772. /* enable hw accelerator */
  2773. ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
  2774. | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
  2775. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  2776. }
  2777. if (fep->quirks & FEC_QUIRK_HAS_AVB) {
  2778. fep->tx_align = 0;
  2779. fep->rx_align = 0x3f;
  2780. }
  2781. ndev->hw_features = ndev->features;
  2782. fec_restart(ndev);
  2783. if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
  2784. fec_enet_clear_ethtool_stats(ndev);
  2785. else
  2786. fec_enet_update_ethtool_stats(ndev);
  2787. return 0;
  2788. }
  2789. #ifdef CONFIG_OF
  2790. static int fec_reset_phy(struct platform_device *pdev)
  2791. {
  2792. int err, phy_reset;
  2793. bool active_high = false;
  2794. int msec = 1, phy_post_delay = 0;
  2795. struct device_node *np = pdev->dev.of_node;
  2796. if (!np)
  2797. return 0;
  2798. err = of_property_read_u32(np, "phy-reset-duration", &msec);
  2799. /* A sane reset duration should not be longer than 1s */
  2800. if (!err && msec > 1000)
  2801. msec = 1;
  2802. phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
  2803. if (phy_reset == -EPROBE_DEFER)
  2804. return phy_reset;
  2805. else if (!gpio_is_valid(phy_reset))
  2806. return 0;
  2807. err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
  2808. /* valid reset duration should be less than 1s */
  2809. if (!err && phy_post_delay > 1000)
  2810. return -EINVAL;
  2811. active_high = of_property_read_bool(np, "phy-reset-active-high");
  2812. err = devm_gpio_request_one(&pdev->dev, phy_reset,
  2813. active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
  2814. "phy-reset");
  2815. if (err) {
  2816. dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
  2817. return err;
  2818. }
  2819. if (msec > 20)
  2820. msleep(msec);
  2821. else
  2822. usleep_range(msec * 1000, msec * 1000 + 1000);
  2823. gpio_set_value_cansleep(phy_reset, !active_high);
  2824. if (!phy_post_delay)
  2825. return 0;
  2826. if (phy_post_delay > 20)
  2827. msleep(phy_post_delay);
  2828. else
  2829. usleep_range(phy_post_delay * 1000,
  2830. phy_post_delay * 1000 + 1000);
  2831. return 0;
  2832. }
  2833. #else /* CONFIG_OF */
  2834. static int fec_reset_phy(struct platform_device *pdev)
  2835. {
  2836. /*
  2837. * In case of platform probe, the reset has been done
  2838. * by machine code.
  2839. */
  2840. return 0;
  2841. }
  2842. #endif /* CONFIG_OF */
  2843. static void
  2844. fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
  2845. {
  2846. struct device_node *np = pdev->dev.of_node;
  2847. *num_tx = *num_rx = 1;
  2848. if (!np || !of_device_is_available(np))
  2849. return;
  2850. /* parse the num of tx and rx queues */
  2851. of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
  2852. of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
  2853. if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
  2854. dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
  2855. *num_tx);
  2856. *num_tx = 1;
  2857. return;
  2858. }
  2859. if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
  2860. dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
  2861. *num_rx);
  2862. *num_rx = 1;
  2863. return;
  2864. }
  2865. }
  2866. static int fec_enet_get_irq_cnt(struct platform_device *pdev)
  2867. {
  2868. int irq_cnt = platform_irq_count(pdev);
  2869. if (irq_cnt > FEC_IRQ_NUM)
  2870. irq_cnt = FEC_IRQ_NUM; /* last for pps */
  2871. else if (irq_cnt == 2)
  2872. irq_cnt = 1; /* last for pps */
  2873. else if (irq_cnt <= 0)
  2874. irq_cnt = 1; /* At least 1 irq is needed */
  2875. return irq_cnt;
  2876. }
  2877. static int fec_enet_init_stop_mode(struct fec_enet_private *fep,
  2878. struct fec_devinfo *dev_info,
  2879. struct device_node *np)
  2880. {
  2881. struct device_node *gpr_np;
  2882. int ret = 0;
  2883. if (!dev_info)
  2884. return 0;
  2885. gpr_np = of_parse_phandle(np, "gpr", 0);
  2886. if (!gpr_np)
  2887. return 0;
  2888. fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np);
  2889. if (IS_ERR(fep->stop_gpr.gpr)) {
  2890. dev_err(&fep->pdev->dev, "could not find gpr regmap\n");
  2891. ret = PTR_ERR(fep->stop_gpr.gpr);
  2892. fep->stop_gpr.gpr = NULL;
  2893. goto out;
  2894. }
  2895. fep->stop_gpr.reg = dev_info->stop_gpr_reg;
  2896. fep->stop_gpr.bit = dev_info->stop_gpr_bit;
  2897. out:
  2898. of_node_put(gpr_np);
  2899. return ret;
  2900. }
  2901. static int
  2902. fec_probe(struct platform_device *pdev)
  2903. {
  2904. struct fec_enet_private *fep;
  2905. struct fec_platform_data *pdata;
  2906. struct net_device *ndev;
  2907. int i, irq, ret = 0;
  2908. struct resource *r;
  2909. const struct of_device_id *of_id;
  2910. static int dev_id;
  2911. struct device_node *np = pdev->dev.of_node, *phy_node;
  2912. int num_tx_qs;
  2913. int num_rx_qs;
  2914. char irq_name[8];
  2915. int irq_cnt;
  2916. struct fec_devinfo *dev_info;
  2917. fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
  2918. /* Init network device */
  2919. ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
  2920. FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
  2921. if (!ndev)
  2922. return -ENOMEM;
  2923. SET_NETDEV_DEV(ndev, &pdev->dev);
  2924. /* setup board info structure */
  2925. fep = netdev_priv(ndev);
  2926. of_id = of_match_device(fec_dt_ids, &pdev->dev);
  2927. if (of_id)
  2928. pdev->id_entry = of_id->data;
  2929. dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data;
  2930. if (dev_info)
  2931. fep->quirks = dev_info->quirks;
  2932. fep->netdev = ndev;
  2933. fep->num_rx_queues = num_rx_qs;
  2934. fep->num_tx_queues = num_tx_qs;
  2935. #if !defined(CONFIG_M5272)
  2936. /* default enable pause frame auto negotiation */
  2937. if (fep->quirks & FEC_QUIRK_HAS_GBIT)
  2938. fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
  2939. #endif
  2940. /* Select default pin state */
  2941. pinctrl_pm_select_default_state(&pdev->dev);
  2942. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2943. fep->hwp = devm_ioremap_resource(&pdev->dev, r);
  2944. if (IS_ERR(fep->hwp)) {
  2945. ret = PTR_ERR(fep->hwp);
  2946. goto failed_ioremap;
  2947. }
  2948. fep->pdev = pdev;
  2949. fep->dev_id = dev_id++;
  2950. platform_set_drvdata(pdev, ndev);
  2951. if ((of_machine_is_compatible("fsl,imx6q") ||
  2952. of_machine_is_compatible("fsl,imx6dl")) &&
  2953. !of_property_read_bool(np, "fsl,err006687-workaround-present"))
  2954. fep->quirks |= FEC_QUIRK_ERR006687;
  2955. if (of_get_property(np, "fsl,magic-packet", NULL))
  2956. fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
  2957. ret = fec_enet_init_stop_mode(fep, dev_info, np);
  2958. if (ret)
  2959. goto failed_stop_mode;
  2960. phy_node = of_parse_phandle(np, "phy-handle", 0);
  2961. if (!phy_node && of_phy_is_fixed_link(np)) {
  2962. ret = of_phy_register_fixed_link(np);
  2963. if (ret < 0) {
  2964. dev_err(&pdev->dev,
  2965. "broken fixed-link specification\n");
  2966. goto failed_phy;
  2967. }
  2968. phy_node = of_node_get(np);
  2969. }
  2970. fep->phy_node = phy_node;
  2971. ret = of_get_phy_mode(pdev->dev.of_node);
  2972. if (ret < 0) {
  2973. pdata = dev_get_platdata(&pdev->dev);
  2974. if (pdata)
  2975. fep->phy_interface = pdata->phy;
  2976. else
  2977. fep->phy_interface = PHY_INTERFACE_MODE_MII;
  2978. } else {
  2979. fep->phy_interface = ret;
  2980. }
  2981. fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  2982. if (IS_ERR(fep->clk_ipg)) {
  2983. ret = PTR_ERR(fep->clk_ipg);
  2984. goto failed_clk;
  2985. }
  2986. fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  2987. if (IS_ERR(fep->clk_ahb)) {
  2988. ret = PTR_ERR(fep->clk_ahb);
  2989. goto failed_clk;
  2990. }
  2991. fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
  2992. /* enet_out is optional, depends on board */
  2993. fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
  2994. if (IS_ERR(fep->clk_enet_out))
  2995. fep->clk_enet_out = NULL;
  2996. fep->ptp_clk_on = false;
  2997. mutex_init(&fep->ptp_clk_mutex);
  2998. /* clk_ref is optional, depends on board */
  2999. fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
  3000. if (IS_ERR(fep->clk_ref))
  3001. fep->clk_ref = NULL;
  3002. fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
  3003. fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
  3004. if (IS_ERR(fep->clk_ptp)) {
  3005. fep->clk_ptp = NULL;
  3006. fep->bufdesc_ex = false;
  3007. }
  3008. ret = fec_enet_clk_enable(ndev, true);
  3009. if (ret)
  3010. goto failed_clk;
  3011. ret = clk_prepare_enable(fep->clk_ipg);
  3012. if (ret)
  3013. goto failed_clk_ipg;
  3014. ret = clk_prepare_enable(fep->clk_ahb);
  3015. if (ret)
  3016. goto failed_clk_ahb;
  3017. fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
  3018. if (!IS_ERR(fep->reg_phy)) {
  3019. ret = regulator_enable(fep->reg_phy);
  3020. if (ret) {
  3021. dev_err(&pdev->dev,
  3022. "Failed to enable phy regulator: %d\n", ret);
  3023. clk_disable_unprepare(fep->clk_ipg);
  3024. goto failed_regulator;
  3025. }
  3026. } else {
  3027. if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
  3028. ret = -EPROBE_DEFER;
  3029. goto failed_regulator;
  3030. }
  3031. fep->reg_phy = NULL;
  3032. }
  3033. pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
  3034. pm_runtime_use_autosuspend(&pdev->dev);
  3035. pm_runtime_get_noresume(&pdev->dev);
  3036. pm_runtime_set_active(&pdev->dev);
  3037. pm_runtime_enable(&pdev->dev);
  3038. ret = fec_reset_phy(pdev);
  3039. if (ret)
  3040. goto failed_reset;
  3041. irq_cnt = fec_enet_get_irq_cnt(pdev);
  3042. if (fep->bufdesc_ex)
  3043. fec_ptp_init(pdev, irq_cnt);
  3044. ret = fec_enet_init(ndev);
  3045. if (ret)
  3046. goto failed_init;
  3047. for (i = 0; i < irq_cnt; i++) {
  3048. snprintf(irq_name, sizeof(irq_name), "int%d", i);
  3049. irq = platform_get_irq_byname(pdev, irq_name);
  3050. if (irq < 0)
  3051. irq = platform_get_irq(pdev, i);
  3052. if (irq < 0) {
  3053. ret = irq;
  3054. goto failed_irq;
  3055. }
  3056. ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
  3057. 0, pdev->name, ndev);
  3058. if (ret)
  3059. goto failed_irq;
  3060. fep->irq[i] = irq;
  3061. }
  3062. init_completion(&fep->mdio_done);
  3063. ret = fec_enet_mii_init(pdev);
  3064. if (ret)
  3065. goto failed_mii_init;
  3066. /* Carrier starts down, phylib will bring it up */
  3067. netif_carrier_off(ndev);
  3068. fec_enet_clk_enable(ndev, false);
  3069. pinctrl_pm_select_sleep_state(&pdev->dev);
  3070. ret = register_netdev(ndev);
  3071. if (ret)
  3072. goto failed_register;
  3073. device_init_wakeup(&ndev->dev, fep->wol_flag &
  3074. FEC_WOL_HAS_MAGIC_PACKET);
  3075. if (fep->bufdesc_ex && fep->ptp_clock)
  3076. netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
  3077. fep->rx_copybreak = COPYBREAK_DEFAULT;
  3078. INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
  3079. pm_runtime_mark_last_busy(&pdev->dev);
  3080. pm_runtime_put_autosuspend(&pdev->dev);
  3081. return 0;
  3082. failed_register:
  3083. fec_enet_mii_remove(fep);
  3084. failed_mii_init:
  3085. failed_irq:
  3086. failed_init:
  3087. fec_ptp_stop(pdev);
  3088. failed_reset:
  3089. pm_runtime_put_noidle(&pdev->dev);
  3090. pm_runtime_disable(&pdev->dev);
  3091. if (fep->reg_phy)
  3092. regulator_disable(fep->reg_phy);
  3093. failed_regulator:
  3094. clk_disable_unprepare(fep->clk_ahb);
  3095. failed_clk_ahb:
  3096. clk_disable_unprepare(fep->clk_ipg);
  3097. failed_clk_ipg:
  3098. fec_enet_clk_enable(ndev, false);
  3099. failed_clk:
  3100. if (of_phy_is_fixed_link(np))
  3101. of_phy_deregister_fixed_link(np);
  3102. of_node_put(phy_node);
  3103. failed_stop_mode:
  3104. failed_phy:
  3105. dev_id--;
  3106. failed_ioremap:
  3107. free_netdev(ndev);
  3108. return ret;
  3109. }
  3110. static int
  3111. fec_drv_remove(struct platform_device *pdev)
  3112. {
  3113. struct net_device *ndev = platform_get_drvdata(pdev);
  3114. struct fec_enet_private *fep = netdev_priv(ndev);
  3115. struct device_node *np = pdev->dev.of_node;
  3116. int ret;
  3117. ret = pm_runtime_get_sync(&pdev->dev);
  3118. if (ret < 0)
  3119. return ret;
  3120. cancel_work_sync(&fep->tx_timeout_work);
  3121. fec_ptp_stop(pdev);
  3122. unregister_netdev(ndev);
  3123. fec_enet_mii_remove(fep);
  3124. if (fep->reg_phy)
  3125. regulator_disable(fep->reg_phy);
  3126. if (of_phy_is_fixed_link(np))
  3127. of_phy_deregister_fixed_link(np);
  3128. of_node_put(fep->phy_node);
  3129. free_netdev(ndev);
  3130. clk_disable_unprepare(fep->clk_ahb);
  3131. clk_disable_unprepare(fep->clk_ipg);
  3132. pm_runtime_put_noidle(&pdev->dev);
  3133. pm_runtime_disable(&pdev->dev);
  3134. return 0;
  3135. }
  3136. static int __maybe_unused fec_suspend(struct device *dev)
  3137. {
  3138. struct net_device *ndev = dev_get_drvdata(dev);
  3139. struct fec_enet_private *fep = netdev_priv(ndev);
  3140. rtnl_lock();
  3141. if (netif_running(ndev)) {
  3142. if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
  3143. fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
  3144. phy_stop(ndev->phydev);
  3145. napi_disable(&fep->napi);
  3146. netif_tx_lock_bh(ndev);
  3147. netif_device_detach(ndev);
  3148. netif_tx_unlock_bh(ndev);
  3149. fec_stop(ndev);
  3150. fec_enet_clk_enable(ndev, false);
  3151. if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
  3152. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  3153. }
  3154. rtnl_unlock();
  3155. if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
  3156. regulator_disable(fep->reg_phy);
  3157. /* SOC supply clock to phy, when clock is disabled, phy link down
  3158. * SOC control phy regulator, when regulator is disabled, phy link down
  3159. */
  3160. if (fep->clk_enet_out || fep->reg_phy)
  3161. fep->link = 0;
  3162. return 0;
  3163. }
  3164. static int __maybe_unused fec_resume(struct device *dev)
  3165. {
  3166. struct net_device *ndev = dev_get_drvdata(dev);
  3167. struct fec_enet_private *fep = netdev_priv(ndev);
  3168. int ret;
  3169. int val;
  3170. if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
  3171. ret = regulator_enable(fep->reg_phy);
  3172. if (ret)
  3173. return ret;
  3174. }
  3175. rtnl_lock();
  3176. if (netif_running(ndev)) {
  3177. ret = fec_enet_clk_enable(ndev, true);
  3178. if (ret) {
  3179. rtnl_unlock();
  3180. goto failed_clk;
  3181. }
  3182. if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
  3183. fec_enet_stop_mode(fep, false);
  3184. val = readl(fep->hwp + FEC_ECNTRL);
  3185. val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
  3186. writel(val, fep->hwp + FEC_ECNTRL);
  3187. fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
  3188. } else {
  3189. pinctrl_pm_select_default_state(&fep->pdev->dev);
  3190. }
  3191. fec_restart(ndev);
  3192. netif_tx_lock_bh(ndev);
  3193. netif_device_attach(ndev);
  3194. netif_tx_unlock_bh(ndev);
  3195. napi_enable(&fep->napi);
  3196. phy_start(ndev->phydev);
  3197. }
  3198. rtnl_unlock();
  3199. return 0;
  3200. failed_clk:
  3201. if (fep->reg_phy)
  3202. regulator_disable(fep->reg_phy);
  3203. return ret;
  3204. }
  3205. static int __maybe_unused fec_runtime_suspend(struct device *dev)
  3206. {
  3207. struct net_device *ndev = dev_get_drvdata(dev);
  3208. struct fec_enet_private *fep = netdev_priv(ndev);
  3209. clk_disable_unprepare(fep->clk_ahb);
  3210. clk_disable_unprepare(fep->clk_ipg);
  3211. return 0;
  3212. }
  3213. static int __maybe_unused fec_runtime_resume(struct device *dev)
  3214. {
  3215. struct net_device *ndev = dev_get_drvdata(dev);
  3216. struct fec_enet_private *fep = netdev_priv(ndev);
  3217. int ret;
  3218. ret = clk_prepare_enable(fep->clk_ahb);
  3219. if (ret)
  3220. return ret;
  3221. ret = clk_prepare_enable(fep->clk_ipg);
  3222. if (ret)
  3223. goto failed_clk_ipg;
  3224. return 0;
  3225. failed_clk_ipg:
  3226. clk_disable_unprepare(fep->clk_ahb);
  3227. return ret;
  3228. }
  3229. static const struct dev_pm_ops fec_pm_ops = {
  3230. SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
  3231. SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
  3232. };
  3233. static struct platform_driver fec_driver = {
  3234. .driver = {
  3235. .name = DRIVER_NAME,
  3236. .pm = &fec_pm_ops,
  3237. .of_match_table = fec_dt_ids,
  3238. },
  3239. .id_table = fec_devtype,
  3240. .probe = fec_probe,
  3241. .remove = fec_drv_remove,
  3242. };
  3243. module_platform_driver(fec_driver);
  3244. MODULE_ALIAS("platform:"DRIVER_NAME);
  3245. MODULE_LICENSE("GPL");