fec_main.c 87 KB

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