fec_main.c 92 KB

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