fec_main.c 70 KB

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