fec_main.c 92 KB

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