fec_main.c 91 KB

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