fec_main.c 72 KB

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