fec_main.c 93 KB

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