fec_main.c 85 KB

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