fec_main.c 93 KB

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