fec_main.c 86 KB

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