fec_main.c 93 KB

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