fec_main.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481
  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;
  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 entries_free;
  1013. fep = netdev_priv(ndev);
  1014. queue_id = FEC_ENET_GET_QUQUE(queue_id);
  1015. txq = fep->tx_queue[queue_id];
  1016. /* get next bdp of dirty_tx */
  1017. nq = netdev_get_tx_queue(ndev, queue_id);
  1018. bdp = txq->dirty_tx;
  1019. /* get next bdp of dirty_tx */
  1020. bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
  1021. while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
  1022. /* current queue is empty */
  1023. if (bdp == txq->cur_tx)
  1024. break;
  1025. index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
  1026. skb = txq->tx_skbuff[index];
  1027. txq->tx_skbuff[index] = NULL;
  1028. if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
  1029. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  1030. bdp->cbd_datlen, DMA_TO_DEVICE);
  1031. bdp->cbd_bufaddr = 0;
  1032. if (!skb) {
  1033. bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
  1034. continue;
  1035. }
  1036. /* Check for errors. */
  1037. if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  1038. BD_ENET_TX_RL | BD_ENET_TX_UN |
  1039. BD_ENET_TX_CSL)) {
  1040. ndev->stats.tx_errors++;
  1041. if (status & BD_ENET_TX_HB) /* No heartbeat */
  1042. ndev->stats.tx_heartbeat_errors++;
  1043. if (status & BD_ENET_TX_LC) /* Late collision */
  1044. ndev->stats.tx_window_errors++;
  1045. if (status & BD_ENET_TX_RL) /* Retrans limit */
  1046. ndev->stats.tx_aborted_errors++;
  1047. if (status & BD_ENET_TX_UN) /* Underrun */
  1048. ndev->stats.tx_fifo_errors++;
  1049. if (status & BD_ENET_TX_CSL) /* Carrier lost */
  1050. ndev->stats.tx_carrier_errors++;
  1051. } else {
  1052. ndev->stats.tx_packets++;
  1053. ndev->stats.tx_bytes += skb->len;
  1054. }
  1055. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
  1056. fep->bufdesc_ex) {
  1057. struct skb_shared_hwtstamps shhwtstamps;
  1058. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1059. fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
  1060. skb_tstamp_tx(skb, &shhwtstamps);
  1061. }
  1062. /* Deferred means some collisions occurred during transmit,
  1063. * but we eventually sent the packet OK.
  1064. */
  1065. if (status & BD_ENET_TX_DEF)
  1066. ndev->stats.collisions++;
  1067. /* Free the sk buffer associated with this last transmit */
  1068. dev_kfree_skb_any(skb);
  1069. txq->dirty_tx = bdp;
  1070. /* Update pointer to next buffer descriptor to be transmitted */
  1071. bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
  1072. /* Since we have freed up a buffer, the ring is no longer full
  1073. */
  1074. if (netif_queue_stopped(ndev)) {
  1075. entries_free = fec_enet_get_free_txdesc_num(fep, txq);
  1076. if (entries_free >= txq->tx_wake_threshold)
  1077. netif_tx_wake_queue(nq);
  1078. }
  1079. }
  1080. /* ERR006538: Keep the transmitter going */
  1081. if (bdp != txq->cur_tx &&
  1082. readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
  1083. writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
  1084. }
  1085. static void
  1086. fec_enet_tx(struct net_device *ndev)
  1087. {
  1088. struct fec_enet_private *fep = netdev_priv(ndev);
  1089. u16 queue_id;
  1090. /* First process class A queue, then Class B and Best Effort queue */
  1091. for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
  1092. clear_bit(queue_id, &fep->work_tx);
  1093. fec_enet_tx_queue(ndev, queue_id);
  1094. }
  1095. return;
  1096. }
  1097. static int
  1098. fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
  1099. {
  1100. struct fec_enet_private *fep = netdev_priv(ndev);
  1101. int off;
  1102. off = ((unsigned long)skb->data) & fep->rx_align;
  1103. if (off)
  1104. skb_reserve(skb, fep->rx_align + 1 - off);
  1105. bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
  1106. FEC_ENET_RX_FRSIZE - fep->rx_align,
  1107. DMA_FROM_DEVICE);
  1108. if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
  1109. if (net_ratelimit())
  1110. netdev_err(ndev, "Rx DMA memory map failed\n");
  1111. return -ENOMEM;
  1112. }
  1113. return 0;
  1114. }
  1115. static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
  1116. struct bufdesc *bdp, u32 length, bool swap)
  1117. {
  1118. struct fec_enet_private *fep = netdev_priv(ndev);
  1119. struct sk_buff *new_skb;
  1120. if (length > fep->rx_copybreak)
  1121. return false;
  1122. new_skb = netdev_alloc_skb(ndev, length);
  1123. if (!new_skb)
  1124. return false;
  1125. dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
  1126. FEC_ENET_RX_FRSIZE - fep->rx_align,
  1127. DMA_FROM_DEVICE);
  1128. if (!swap)
  1129. memcpy(new_skb->data, (*skb)->data, length);
  1130. else
  1131. swap_buffer2(new_skb->data, (*skb)->data, length);
  1132. *skb = new_skb;
  1133. return true;
  1134. }
  1135. /* During a receive, the cur_rx points to the current incoming buffer.
  1136. * When we update through the ring, if the next incoming buffer has
  1137. * not been given to the system, we just set the empty indicator,
  1138. * effectively tossing the packet.
  1139. */
  1140. static int
  1141. fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
  1142. {
  1143. struct fec_enet_private *fep = netdev_priv(ndev);
  1144. struct fec_enet_priv_rx_q *rxq;
  1145. struct bufdesc *bdp;
  1146. unsigned short status;
  1147. struct sk_buff *skb_new = NULL;
  1148. struct sk_buff *skb;
  1149. ushort pkt_len;
  1150. __u8 *data;
  1151. int pkt_received = 0;
  1152. struct bufdesc_ex *ebdp = NULL;
  1153. bool vlan_packet_rcvd = false;
  1154. u16 vlan_tag;
  1155. int index = 0;
  1156. bool is_copybreak;
  1157. bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
  1158. #ifdef CONFIG_M532x
  1159. flush_cache_all();
  1160. #endif
  1161. queue_id = FEC_ENET_GET_QUQUE(queue_id);
  1162. rxq = fep->rx_queue[queue_id];
  1163. /* First, grab all of the stats for the incoming packet.
  1164. * These get messed up if we get called due to a busy condition.
  1165. */
  1166. bdp = rxq->cur_rx;
  1167. while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
  1168. if (pkt_received >= budget)
  1169. break;
  1170. pkt_received++;
  1171. /* Since we have allocated space to hold a complete frame,
  1172. * the last indicator should be set.
  1173. */
  1174. if ((status & BD_ENET_RX_LAST) == 0)
  1175. netdev_err(ndev, "rcv is not +last\n");
  1176. /* Check for errors. */
  1177. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
  1178. BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  1179. ndev->stats.rx_errors++;
  1180. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
  1181. /* Frame too long or too short. */
  1182. ndev->stats.rx_length_errors++;
  1183. }
  1184. if (status & BD_ENET_RX_NO) /* Frame alignment */
  1185. ndev->stats.rx_frame_errors++;
  1186. if (status & BD_ENET_RX_CR) /* CRC Error */
  1187. ndev->stats.rx_crc_errors++;
  1188. if (status & BD_ENET_RX_OV) /* FIFO overrun */
  1189. ndev->stats.rx_fifo_errors++;
  1190. }
  1191. /* Report late collisions as a frame error.
  1192. * On this error, the BD is closed, but we don't know what we
  1193. * have in the buffer. So, just drop this frame on the floor.
  1194. */
  1195. if (status & BD_ENET_RX_CL) {
  1196. ndev->stats.rx_errors++;
  1197. ndev->stats.rx_frame_errors++;
  1198. goto rx_processing_done;
  1199. }
  1200. /* Process the incoming frame. */
  1201. ndev->stats.rx_packets++;
  1202. pkt_len = bdp->cbd_datlen;
  1203. ndev->stats.rx_bytes += pkt_len;
  1204. index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
  1205. skb = rxq->rx_skbuff[index];
  1206. /* The packet length includes FCS, but we don't want to
  1207. * include that when passing upstream as it messes up
  1208. * bridging applications.
  1209. */
  1210. is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
  1211. need_swap);
  1212. if (!is_copybreak) {
  1213. skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
  1214. if (unlikely(!skb_new)) {
  1215. ndev->stats.rx_dropped++;
  1216. goto rx_processing_done;
  1217. }
  1218. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  1219. FEC_ENET_RX_FRSIZE - fep->rx_align,
  1220. DMA_FROM_DEVICE);
  1221. }
  1222. prefetch(skb->data - NET_IP_ALIGN);
  1223. skb_put(skb, pkt_len - 4);
  1224. data = skb->data;
  1225. if (!is_copybreak && need_swap)
  1226. swap_buffer(data, pkt_len);
  1227. /* Extract the enhanced buffer descriptor */
  1228. ebdp = NULL;
  1229. if (fep->bufdesc_ex)
  1230. ebdp = (struct bufdesc_ex *)bdp;
  1231. /* If this is a VLAN packet remove the VLAN Tag */
  1232. vlan_packet_rcvd = false;
  1233. if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  1234. fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
  1235. /* Push and remove the vlan tag */
  1236. struct vlan_hdr *vlan_header =
  1237. (struct vlan_hdr *) (data + ETH_HLEN);
  1238. vlan_tag = ntohs(vlan_header->h_vlan_TCI);
  1239. vlan_packet_rcvd = true;
  1240. skb_copy_to_linear_data_offset(skb, VLAN_HLEN,
  1241. data, (2 * ETH_ALEN));
  1242. skb_pull(skb, VLAN_HLEN);
  1243. }
  1244. skb->protocol = eth_type_trans(skb, ndev);
  1245. /* Get receive timestamp from the skb */
  1246. if (fep->hwts_rx_en && fep->bufdesc_ex)
  1247. fec_enet_hwtstamp(fep, ebdp->ts,
  1248. skb_hwtstamps(skb));
  1249. if (fep->bufdesc_ex &&
  1250. (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
  1251. if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
  1252. /* don't check it */
  1253. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1254. } else {
  1255. skb_checksum_none_assert(skb);
  1256. }
  1257. }
  1258. /* Handle received VLAN packets */
  1259. if (vlan_packet_rcvd)
  1260. __vlan_hwaccel_put_tag(skb,
  1261. htons(ETH_P_8021Q),
  1262. vlan_tag);
  1263. napi_gro_receive(&fep->napi, skb);
  1264. if (is_copybreak) {
  1265. dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
  1266. FEC_ENET_RX_FRSIZE - fep->rx_align,
  1267. DMA_FROM_DEVICE);
  1268. } else {
  1269. rxq->rx_skbuff[index] = skb_new;
  1270. fec_enet_new_rxbdp(ndev, bdp, skb_new);
  1271. }
  1272. rx_processing_done:
  1273. /* Clear the status flags for this buffer */
  1274. status &= ~BD_ENET_RX_STATS;
  1275. /* Mark the buffer empty */
  1276. status |= BD_ENET_RX_EMPTY;
  1277. bdp->cbd_sc = status;
  1278. if (fep->bufdesc_ex) {
  1279. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1280. ebdp->cbd_esc = BD_ENET_RX_INT;
  1281. ebdp->cbd_prot = 0;
  1282. ebdp->cbd_bdu = 0;
  1283. }
  1284. /* Update BD pointer to next entry */
  1285. bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
  1286. /* Doing this here will keep the FEC running while we process
  1287. * incoming frames. On a heavily loaded network, we should be
  1288. * able to keep up at the expense of system resources.
  1289. */
  1290. writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
  1291. }
  1292. rxq->cur_rx = bdp;
  1293. return pkt_received;
  1294. }
  1295. static int
  1296. fec_enet_rx(struct net_device *ndev, int budget)
  1297. {
  1298. int pkt_received = 0;
  1299. u16 queue_id;
  1300. struct fec_enet_private *fep = netdev_priv(ndev);
  1301. for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
  1302. clear_bit(queue_id, &fep->work_rx);
  1303. pkt_received += fec_enet_rx_queue(ndev,
  1304. budget - pkt_received, queue_id);
  1305. }
  1306. return pkt_received;
  1307. }
  1308. static bool
  1309. fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
  1310. {
  1311. if (int_events == 0)
  1312. return false;
  1313. if (int_events & FEC_ENET_RXF)
  1314. fep->work_rx |= (1 << 2);
  1315. if (int_events & FEC_ENET_RXF_1)
  1316. fep->work_rx |= (1 << 0);
  1317. if (int_events & FEC_ENET_RXF_2)
  1318. fep->work_rx |= (1 << 1);
  1319. if (int_events & FEC_ENET_TXF)
  1320. fep->work_tx |= (1 << 2);
  1321. if (int_events & FEC_ENET_TXF_1)
  1322. fep->work_tx |= (1 << 0);
  1323. if (int_events & FEC_ENET_TXF_2)
  1324. fep->work_tx |= (1 << 1);
  1325. return true;
  1326. }
  1327. static irqreturn_t
  1328. fec_enet_interrupt(int irq, void *dev_id)
  1329. {
  1330. struct net_device *ndev = dev_id;
  1331. struct fec_enet_private *fep = netdev_priv(ndev);
  1332. uint int_events;
  1333. irqreturn_t ret = IRQ_NONE;
  1334. int_events = readl(fep->hwp + FEC_IEVENT);
  1335. writel(int_events, fep->hwp + FEC_IEVENT);
  1336. fec_enet_collect_events(fep, int_events);
  1337. if (fep->work_tx || fep->work_rx) {
  1338. ret = IRQ_HANDLED;
  1339. if (napi_schedule_prep(&fep->napi)) {
  1340. /* Disable the NAPI interrupts */
  1341. writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
  1342. __napi_schedule(&fep->napi);
  1343. }
  1344. }
  1345. if (int_events & FEC_ENET_MII) {
  1346. ret = IRQ_HANDLED;
  1347. complete(&fep->mdio_done);
  1348. }
  1349. if (fep->ptp_clock)
  1350. fec_ptp_check_pps_event(fep);
  1351. return ret;
  1352. }
  1353. static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
  1354. {
  1355. struct net_device *ndev = napi->dev;
  1356. struct fec_enet_private *fep = netdev_priv(ndev);
  1357. int pkts;
  1358. pkts = fec_enet_rx(ndev, budget);
  1359. fec_enet_tx(ndev);
  1360. if (pkts < budget) {
  1361. napi_complete(napi);
  1362. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  1363. }
  1364. return pkts;
  1365. }
  1366. /* ------------------------------------------------------------------------- */
  1367. static void fec_get_mac(struct net_device *ndev)
  1368. {
  1369. struct fec_enet_private *fep = netdev_priv(ndev);
  1370. struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
  1371. unsigned char *iap, tmpaddr[ETH_ALEN];
  1372. /*
  1373. * try to get mac address in following order:
  1374. *
  1375. * 1) module parameter via kernel command line in form
  1376. * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
  1377. */
  1378. iap = macaddr;
  1379. /*
  1380. * 2) from device tree data
  1381. */
  1382. if (!is_valid_ether_addr(iap)) {
  1383. struct device_node *np = fep->pdev->dev.of_node;
  1384. if (np) {
  1385. const char *mac = of_get_mac_address(np);
  1386. if (mac)
  1387. iap = (unsigned char *) mac;
  1388. }
  1389. }
  1390. /*
  1391. * 3) from flash or fuse (via platform data)
  1392. */
  1393. if (!is_valid_ether_addr(iap)) {
  1394. #ifdef CONFIG_M5272
  1395. if (FEC_FLASHMAC)
  1396. iap = (unsigned char *)FEC_FLASHMAC;
  1397. #else
  1398. if (pdata)
  1399. iap = (unsigned char *)&pdata->mac;
  1400. #endif
  1401. }
  1402. /*
  1403. * 4) FEC mac registers set by bootloader
  1404. */
  1405. if (!is_valid_ether_addr(iap)) {
  1406. *((__be32 *) &tmpaddr[0]) =
  1407. cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
  1408. *((__be16 *) &tmpaddr[4]) =
  1409. cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
  1410. iap = &tmpaddr[0];
  1411. }
  1412. /*
  1413. * 5) random mac address
  1414. */
  1415. if (!is_valid_ether_addr(iap)) {
  1416. /* Report it and use a random ethernet address instead */
  1417. netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
  1418. eth_hw_addr_random(ndev);
  1419. netdev_info(ndev, "Using random MAC address: %pM\n",
  1420. ndev->dev_addr);
  1421. return;
  1422. }
  1423. memcpy(ndev->dev_addr, iap, ETH_ALEN);
  1424. /* Adjust MAC if using macaddr */
  1425. if (iap == macaddr)
  1426. ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
  1427. }
  1428. /* ------------------------------------------------------------------------- */
  1429. /*
  1430. * Phy section
  1431. */
  1432. static void fec_enet_adjust_link(struct net_device *ndev)
  1433. {
  1434. struct fec_enet_private *fep = netdev_priv(ndev);
  1435. struct phy_device *phy_dev = fep->phy_dev;
  1436. int status_change = 0;
  1437. /* Prevent a state halted on mii error */
  1438. if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
  1439. phy_dev->state = PHY_RESUMING;
  1440. return;
  1441. }
  1442. /*
  1443. * If the netdev is down, or is going down, we're not interested
  1444. * in link state events, so just mark our idea of the link as down
  1445. * and ignore the event.
  1446. */
  1447. if (!netif_running(ndev) || !netif_device_present(ndev)) {
  1448. fep->link = 0;
  1449. } else if (phy_dev->link) {
  1450. if (!fep->link) {
  1451. fep->link = phy_dev->link;
  1452. status_change = 1;
  1453. }
  1454. if (fep->full_duplex != phy_dev->duplex) {
  1455. fep->full_duplex = phy_dev->duplex;
  1456. status_change = 1;
  1457. }
  1458. if (phy_dev->speed != fep->speed) {
  1459. fep->speed = phy_dev->speed;
  1460. status_change = 1;
  1461. }
  1462. /* if any of the above changed restart the FEC */
  1463. if (status_change) {
  1464. napi_disable(&fep->napi);
  1465. netif_tx_lock_bh(ndev);
  1466. fec_restart(ndev);
  1467. netif_wake_queue(ndev);
  1468. netif_tx_unlock_bh(ndev);
  1469. napi_enable(&fep->napi);
  1470. }
  1471. } else {
  1472. if (fep->link) {
  1473. napi_disable(&fep->napi);
  1474. netif_tx_lock_bh(ndev);
  1475. fec_stop(ndev);
  1476. netif_tx_unlock_bh(ndev);
  1477. napi_enable(&fep->napi);
  1478. fep->link = phy_dev->link;
  1479. status_change = 1;
  1480. }
  1481. }
  1482. if (status_change)
  1483. phy_print_status(phy_dev);
  1484. }
  1485. static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  1486. {
  1487. struct fec_enet_private *fep = bus->priv;
  1488. unsigned long time_left;
  1489. fep->mii_timeout = 0;
  1490. init_completion(&fep->mdio_done);
  1491. /* start a read op */
  1492. writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
  1493. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  1494. FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
  1495. /* wait for end of transfer */
  1496. time_left = wait_for_completion_timeout(&fep->mdio_done,
  1497. usecs_to_jiffies(FEC_MII_TIMEOUT));
  1498. if (time_left == 0) {
  1499. fep->mii_timeout = 1;
  1500. netdev_err(fep->netdev, "MDIO read timeout\n");
  1501. return -ETIMEDOUT;
  1502. }
  1503. /* return value */
  1504. return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
  1505. }
  1506. static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  1507. u16 value)
  1508. {
  1509. struct fec_enet_private *fep = bus->priv;
  1510. unsigned long time_left;
  1511. fep->mii_timeout = 0;
  1512. init_completion(&fep->mdio_done);
  1513. /* start a write op */
  1514. writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
  1515. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  1516. FEC_MMFR_TA | FEC_MMFR_DATA(value),
  1517. fep->hwp + FEC_MII_DATA);
  1518. /* wait for end of transfer */
  1519. time_left = wait_for_completion_timeout(&fep->mdio_done,
  1520. usecs_to_jiffies(FEC_MII_TIMEOUT));
  1521. if (time_left == 0) {
  1522. fep->mii_timeout = 1;
  1523. netdev_err(fep->netdev, "MDIO write timeout\n");
  1524. return -ETIMEDOUT;
  1525. }
  1526. return 0;
  1527. }
  1528. static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
  1529. {
  1530. struct fec_enet_private *fep = netdev_priv(ndev);
  1531. int ret;
  1532. if (enable) {
  1533. ret = clk_prepare_enable(fep->clk_ahb);
  1534. if (ret)
  1535. return ret;
  1536. ret = clk_prepare_enable(fep->clk_ipg);
  1537. if (ret)
  1538. goto failed_clk_ipg;
  1539. if (fep->clk_enet_out) {
  1540. ret = clk_prepare_enable(fep->clk_enet_out);
  1541. if (ret)
  1542. goto failed_clk_enet_out;
  1543. }
  1544. if (fep->clk_ptp) {
  1545. mutex_lock(&fep->ptp_clk_mutex);
  1546. ret = clk_prepare_enable(fep->clk_ptp);
  1547. if (ret) {
  1548. mutex_unlock(&fep->ptp_clk_mutex);
  1549. goto failed_clk_ptp;
  1550. } else {
  1551. fep->ptp_clk_on = true;
  1552. }
  1553. mutex_unlock(&fep->ptp_clk_mutex);
  1554. }
  1555. if (fep->clk_ref) {
  1556. ret = clk_prepare_enable(fep->clk_ref);
  1557. if (ret)
  1558. goto failed_clk_ref;
  1559. }
  1560. } else {
  1561. clk_disable_unprepare(fep->clk_ahb);
  1562. clk_disable_unprepare(fep->clk_ipg);
  1563. if (fep->clk_enet_out)
  1564. clk_disable_unprepare(fep->clk_enet_out);
  1565. if (fep->clk_ptp) {
  1566. mutex_lock(&fep->ptp_clk_mutex);
  1567. clk_disable_unprepare(fep->clk_ptp);
  1568. fep->ptp_clk_on = false;
  1569. mutex_unlock(&fep->ptp_clk_mutex);
  1570. }
  1571. if (fep->clk_ref)
  1572. clk_disable_unprepare(fep->clk_ref);
  1573. }
  1574. return 0;
  1575. failed_clk_ref:
  1576. if (fep->clk_ref)
  1577. clk_disable_unprepare(fep->clk_ref);
  1578. failed_clk_ptp:
  1579. if (fep->clk_enet_out)
  1580. clk_disable_unprepare(fep->clk_enet_out);
  1581. failed_clk_enet_out:
  1582. clk_disable_unprepare(fep->clk_ipg);
  1583. failed_clk_ipg:
  1584. clk_disable_unprepare(fep->clk_ahb);
  1585. return ret;
  1586. }
  1587. static int fec_enet_mii_probe(struct net_device *ndev)
  1588. {
  1589. struct fec_enet_private *fep = netdev_priv(ndev);
  1590. struct phy_device *phy_dev = NULL;
  1591. char mdio_bus_id[MII_BUS_ID_SIZE];
  1592. char phy_name[MII_BUS_ID_SIZE + 3];
  1593. int phy_id;
  1594. int dev_id = fep->dev_id;
  1595. fep->phy_dev = NULL;
  1596. if (fep->phy_node) {
  1597. phy_dev = of_phy_connect(ndev, fep->phy_node,
  1598. &fec_enet_adjust_link, 0,
  1599. fep->phy_interface);
  1600. if (!phy_dev)
  1601. return -ENODEV;
  1602. } else {
  1603. /* check for attached phy */
  1604. for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
  1605. if ((fep->mii_bus->phy_mask & (1 << phy_id)))
  1606. continue;
  1607. if (fep->mii_bus->phy_map[phy_id] == NULL)
  1608. continue;
  1609. if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
  1610. continue;
  1611. if (dev_id--)
  1612. continue;
  1613. strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
  1614. break;
  1615. }
  1616. if (phy_id >= PHY_MAX_ADDR) {
  1617. netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
  1618. strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
  1619. phy_id = 0;
  1620. }
  1621. snprintf(phy_name, sizeof(phy_name),
  1622. PHY_ID_FMT, mdio_bus_id, phy_id);
  1623. phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
  1624. fep->phy_interface);
  1625. }
  1626. if (IS_ERR(phy_dev)) {
  1627. netdev_err(ndev, "could not attach to PHY\n");
  1628. return PTR_ERR(phy_dev);
  1629. }
  1630. /* mask with MAC supported features */
  1631. if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
  1632. phy_dev->supported &= PHY_GBIT_FEATURES;
  1633. phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
  1634. #if !defined(CONFIG_M5272)
  1635. phy_dev->supported |= SUPPORTED_Pause;
  1636. #endif
  1637. }
  1638. else
  1639. phy_dev->supported &= PHY_BASIC_FEATURES;
  1640. phy_dev->advertising = phy_dev->supported;
  1641. fep->phy_dev = phy_dev;
  1642. fep->link = 0;
  1643. fep->full_duplex = 0;
  1644. netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  1645. fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
  1646. fep->phy_dev->irq);
  1647. return 0;
  1648. }
  1649. static int fec_enet_mii_init(struct platform_device *pdev)
  1650. {
  1651. static struct mii_bus *fec0_mii_bus;
  1652. struct net_device *ndev = platform_get_drvdata(pdev);
  1653. struct fec_enet_private *fep = netdev_priv(ndev);
  1654. struct device_node *node;
  1655. int err = -ENXIO, i;
  1656. /*
  1657. * The i.MX28 dual fec interfaces are not equal.
  1658. * Here are the differences:
  1659. *
  1660. * - fec0 supports MII & RMII modes while fec1 only supports RMII
  1661. * - fec0 acts as the 1588 time master while fec1 is slave
  1662. * - external phys can only be configured by fec0
  1663. *
  1664. * That is to say fec1 can not work independently. It only works
  1665. * when fec0 is working. The reason behind this design is that the
  1666. * second interface is added primarily for Switch mode.
  1667. *
  1668. * Because of the last point above, both phys are attached on fec0
  1669. * mdio interface in board design, and need to be configured by
  1670. * fec0 mii_bus.
  1671. */
  1672. if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
  1673. /* fec1 uses fec0 mii_bus */
  1674. if (mii_cnt && fec0_mii_bus) {
  1675. fep->mii_bus = fec0_mii_bus;
  1676. mii_cnt++;
  1677. return 0;
  1678. }
  1679. return -ENOENT;
  1680. }
  1681. fep->mii_timeout = 0;
  1682. /*
  1683. * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
  1684. *
  1685. * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
  1686. * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
  1687. * Reference Manual has an error on this, and gets fixed on i.MX6Q
  1688. * document.
  1689. */
  1690. fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
  1691. if (fep->quirks & FEC_QUIRK_ENET_MAC)
  1692. fep->phy_speed--;
  1693. fep->phy_speed <<= 1;
  1694. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1695. fep->mii_bus = mdiobus_alloc();
  1696. if (fep->mii_bus == NULL) {
  1697. err = -ENOMEM;
  1698. goto err_out;
  1699. }
  1700. fep->mii_bus->name = "fec_enet_mii_bus";
  1701. fep->mii_bus->read = fec_enet_mdio_read;
  1702. fep->mii_bus->write = fec_enet_mdio_write;
  1703. snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  1704. pdev->name, fep->dev_id + 1);
  1705. fep->mii_bus->priv = fep;
  1706. fep->mii_bus->parent = &pdev->dev;
  1707. fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  1708. if (!fep->mii_bus->irq) {
  1709. err = -ENOMEM;
  1710. goto err_out_free_mdiobus;
  1711. }
  1712. for (i = 0; i < PHY_MAX_ADDR; i++)
  1713. fep->mii_bus->irq[i] = PHY_POLL;
  1714. node = of_get_child_by_name(pdev->dev.of_node, "mdio");
  1715. if (node) {
  1716. err = of_mdiobus_register(fep->mii_bus, node);
  1717. of_node_put(node);
  1718. } else {
  1719. err = mdiobus_register(fep->mii_bus);
  1720. }
  1721. if (err)
  1722. goto err_out_free_mdio_irq;
  1723. mii_cnt++;
  1724. /* save fec0 mii_bus */
  1725. if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
  1726. fec0_mii_bus = fep->mii_bus;
  1727. return 0;
  1728. err_out_free_mdio_irq:
  1729. kfree(fep->mii_bus->irq);
  1730. err_out_free_mdiobus:
  1731. mdiobus_free(fep->mii_bus);
  1732. err_out:
  1733. return err;
  1734. }
  1735. static void fec_enet_mii_remove(struct fec_enet_private *fep)
  1736. {
  1737. if (--mii_cnt == 0) {
  1738. mdiobus_unregister(fep->mii_bus);
  1739. kfree(fep->mii_bus->irq);
  1740. mdiobus_free(fep->mii_bus);
  1741. }
  1742. }
  1743. static int fec_enet_get_settings(struct net_device *ndev,
  1744. struct ethtool_cmd *cmd)
  1745. {
  1746. struct fec_enet_private *fep = netdev_priv(ndev);
  1747. struct phy_device *phydev = fep->phy_dev;
  1748. if (!phydev)
  1749. return -ENODEV;
  1750. return phy_ethtool_gset(phydev, cmd);
  1751. }
  1752. static int fec_enet_set_settings(struct net_device *ndev,
  1753. struct ethtool_cmd *cmd)
  1754. {
  1755. struct fec_enet_private *fep = netdev_priv(ndev);
  1756. struct phy_device *phydev = fep->phy_dev;
  1757. if (!phydev)
  1758. return -ENODEV;
  1759. return phy_ethtool_sset(phydev, cmd);
  1760. }
  1761. static void fec_enet_get_drvinfo(struct net_device *ndev,
  1762. struct ethtool_drvinfo *info)
  1763. {
  1764. struct fec_enet_private *fep = netdev_priv(ndev);
  1765. strlcpy(info->driver, fep->pdev->dev.driver->name,
  1766. sizeof(info->driver));
  1767. strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
  1768. strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
  1769. }
  1770. static int fec_enet_get_ts_info(struct net_device *ndev,
  1771. struct ethtool_ts_info *info)
  1772. {
  1773. struct fec_enet_private *fep = netdev_priv(ndev);
  1774. if (fep->bufdesc_ex) {
  1775. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  1776. SOF_TIMESTAMPING_RX_SOFTWARE |
  1777. SOF_TIMESTAMPING_SOFTWARE |
  1778. SOF_TIMESTAMPING_TX_HARDWARE |
  1779. SOF_TIMESTAMPING_RX_HARDWARE |
  1780. SOF_TIMESTAMPING_RAW_HARDWARE;
  1781. if (fep->ptp_clock)
  1782. info->phc_index = ptp_clock_index(fep->ptp_clock);
  1783. else
  1784. info->phc_index = -1;
  1785. info->tx_types = (1 << HWTSTAMP_TX_OFF) |
  1786. (1 << HWTSTAMP_TX_ON);
  1787. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  1788. (1 << HWTSTAMP_FILTER_ALL);
  1789. return 0;
  1790. } else {
  1791. return ethtool_op_get_ts_info(ndev, info);
  1792. }
  1793. }
  1794. #if !defined(CONFIG_M5272)
  1795. static void fec_enet_get_pauseparam(struct net_device *ndev,
  1796. struct ethtool_pauseparam *pause)
  1797. {
  1798. struct fec_enet_private *fep = netdev_priv(ndev);
  1799. pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
  1800. pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
  1801. pause->rx_pause = pause->tx_pause;
  1802. }
  1803. static int fec_enet_set_pauseparam(struct net_device *ndev,
  1804. struct ethtool_pauseparam *pause)
  1805. {
  1806. struct fec_enet_private *fep = netdev_priv(ndev);
  1807. if (!fep->phy_dev)
  1808. return -ENODEV;
  1809. if (pause->tx_pause != pause->rx_pause) {
  1810. netdev_info(ndev,
  1811. "hardware only support enable/disable both tx and rx");
  1812. return -EINVAL;
  1813. }
  1814. fep->pause_flag = 0;
  1815. /* tx pause must be same as rx pause */
  1816. fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
  1817. fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
  1818. if (pause->rx_pause || pause->autoneg) {
  1819. fep->phy_dev->supported |= ADVERTISED_Pause;
  1820. fep->phy_dev->advertising |= ADVERTISED_Pause;
  1821. } else {
  1822. fep->phy_dev->supported &= ~ADVERTISED_Pause;
  1823. fep->phy_dev->advertising &= ~ADVERTISED_Pause;
  1824. }
  1825. if (pause->autoneg) {
  1826. if (netif_running(ndev))
  1827. fec_stop(ndev);
  1828. phy_start_aneg(fep->phy_dev);
  1829. }
  1830. if (netif_running(ndev)) {
  1831. napi_disable(&fep->napi);
  1832. netif_tx_lock_bh(ndev);
  1833. fec_restart(ndev);
  1834. netif_wake_queue(ndev);
  1835. netif_tx_unlock_bh(ndev);
  1836. napi_enable(&fep->napi);
  1837. }
  1838. return 0;
  1839. }
  1840. static const struct fec_stat {
  1841. char name[ETH_GSTRING_LEN];
  1842. u16 offset;
  1843. } fec_stats[] = {
  1844. /* RMON TX */
  1845. { "tx_dropped", RMON_T_DROP },
  1846. { "tx_packets", RMON_T_PACKETS },
  1847. { "tx_broadcast", RMON_T_BC_PKT },
  1848. { "tx_multicast", RMON_T_MC_PKT },
  1849. { "tx_crc_errors", RMON_T_CRC_ALIGN },
  1850. { "tx_undersize", RMON_T_UNDERSIZE },
  1851. { "tx_oversize", RMON_T_OVERSIZE },
  1852. { "tx_fragment", RMON_T_FRAG },
  1853. { "tx_jabber", RMON_T_JAB },
  1854. { "tx_collision", RMON_T_COL },
  1855. { "tx_64byte", RMON_T_P64 },
  1856. { "tx_65to127byte", RMON_T_P65TO127 },
  1857. { "tx_128to255byte", RMON_T_P128TO255 },
  1858. { "tx_256to511byte", RMON_T_P256TO511 },
  1859. { "tx_512to1023byte", RMON_T_P512TO1023 },
  1860. { "tx_1024to2047byte", RMON_T_P1024TO2047 },
  1861. { "tx_GTE2048byte", RMON_T_P_GTE2048 },
  1862. { "tx_octets", RMON_T_OCTETS },
  1863. /* IEEE TX */
  1864. { "IEEE_tx_drop", IEEE_T_DROP },
  1865. { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
  1866. { "IEEE_tx_1col", IEEE_T_1COL },
  1867. { "IEEE_tx_mcol", IEEE_T_MCOL },
  1868. { "IEEE_tx_def", IEEE_T_DEF },
  1869. { "IEEE_tx_lcol", IEEE_T_LCOL },
  1870. { "IEEE_tx_excol", IEEE_T_EXCOL },
  1871. { "IEEE_tx_macerr", IEEE_T_MACERR },
  1872. { "IEEE_tx_cserr", IEEE_T_CSERR },
  1873. { "IEEE_tx_sqe", IEEE_T_SQE },
  1874. { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
  1875. { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
  1876. /* RMON RX */
  1877. { "rx_packets", RMON_R_PACKETS },
  1878. { "rx_broadcast", RMON_R_BC_PKT },
  1879. { "rx_multicast", RMON_R_MC_PKT },
  1880. { "rx_crc_errors", RMON_R_CRC_ALIGN },
  1881. { "rx_undersize", RMON_R_UNDERSIZE },
  1882. { "rx_oversize", RMON_R_OVERSIZE },
  1883. { "rx_fragment", RMON_R_FRAG },
  1884. { "rx_jabber", RMON_R_JAB },
  1885. { "rx_64byte", RMON_R_P64 },
  1886. { "rx_65to127byte", RMON_R_P65TO127 },
  1887. { "rx_128to255byte", RMON_R_P128TO255 },
  1888. { "rx_256to511byte", RMON_R_P256TO511 },
  1889. { "rx_512to1023byte", RMON_R_P512TO1023 },
  1890. { "rx_1024to2047byte", RMON_R_P1024TO2047 },
  1891. { "rx_GTE2048byte", RMON_R_P_GTE2048 },
  1892. { "rx_octets", RMON_R_OCTETS },
  1893. /* IEEE RX */
  1894. { "IEEE_rx_drop", IEEE_R_DROP },
  1895. { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
  1896. { "IEEE_rx_crc", IEEE_R_CRC },
  1897. { "IEEE_rx_align", IEEE_R_ALIGN },
  1898. { "IEEE_rx_macerr", IEEE_R_MACERR },
  1899. { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
  1900. { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
  1901. };
  1902. static void fec_enet_get_ethtool_stats(struct net_device *dev,
  1903. struct ethtool_stats *stats, u64 *data)
  1904. {
  1905. struct fec_enet_private *fep = netdev_priv(dev);
  1906. int i;
  1907. for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
  1908. data[i] = readl(fep->hwp + fec_stats[i].offset);
  1909. }
  1910. static void fec_enet_get_strings(struct net_device *netdev,
  1911. u32 stringset, u8 *data)
  1912. {
  1913. int i;
  1914. switch (stringset) {
  1915. case ETH_SS_STATS:
  1916. for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
  1917. memcpy(data + i * ETH_GSTRING_LEN,
  1918. fec_stats[i].name, ETH_GSTRING_LEN);
  1919. break;
  1920. }
  1921. }
  1922. static int fec_enet_get_sset_count(struct net_device *dev, int sset)
  1923. {
  1924. switch (sset) {
  1925. case ETH_SS_STATS:
  1926. return ARRAY_SIZE(fec_stats);
  1927. default:
  1928. return -EOPNOTSUPP;
  1929. }
  1930. }
  1931. #endif /* !defined(CONFIG_M5272) */
  1932. static int fec_enet_nway_reset(struct net_device *dev)
  1933. {
  1934. struct fec_enet_private *fep = netdev_priv(dev);
  1935. struct phy_device *phydev = fep->phy_dev;
  1936. if (!phydev)
  1937. return -ENODEV;
  1938. return genphy_restart_aneg(phydev);
  1939. }
  1940. /* ITR clock source is enet system clock (clk_ahb).
  1941. * TCTT unit is cycle_ns * 64 cycle
  1942. * So, the ICTT value = X us / (cycle_ns * 64)
  1943. */
  1944. static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
  1945. {
  1946. struct fec_enet_private *fep = netdev_priv(ndev);
  1947. return us * (fep->itr_clk_rate / 64000) / 1000;
  1948. }
  1949. /* Set threshold for interrupt coalescing */
  1950. static void fec_enet_itr_coal_set(struct net_device *ndev)
  1951. {
  1952. struct fec_enet_private *fep = netdev_priv(ndev);
  1953. int rx_itr, tx_itr;
  1954. if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
  1955. return;
  1956. /* Must be greater than zero to avoid unpredictable behavior */
  1957. if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
  1958. !fep->tx_time_itr || !fep->tx_pkts_itr)
  1959. return;
  1960. /* Select enet system clock as Interrupt Coalescing
  1961. * timer Clock Source
  1962. */
  1963. rx_itr = FEC_ITR_CLK_SEL;
  1964. tx_itr = FEC_ITR_CLK_SEL;
  1965. /* set ICFT and ICTT */
  1966. rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
  1967. rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
  1968. tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
  1969. tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
  1970. rx_itr |= FEC_ITR_EN;
  1971. tx_itr |= FEC_ITR_EN;
  1972. writel(tx_itr, fep->hwp + FEC_TXIC0);
  1973. writel(rx_itr, fep->hwp + FEC_RXIC0);
  1974. writel(tx_itr, fep->hwp + FEC_TXIC1);
  1975. writel(rx_itr, fep->hwp + FEC_RXIC1);
  1976. writel(tx_itr, fep->hwp + FEC_TXIC2);
  1977. writel(rx_itr, fep->hwp + FEC_RXIC2);
  1978. }
  1979. static int
  1980. fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
  1981. {
  1982. struct fec_enet_private *fep = netdev_priv(ndev);
  1983. if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
  1984. return -EOPNOTSUPP;
  1985. ec->rx_coalesce_usecs = fep->rx_time_itr;
  1986. ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
  1987. ec->tx_coalesce_usecs = fep->tx_time_itr;
  1988. ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
  1989. return 0;
  1990. }
  1991. static int
  1992. fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
  1993. {
  1994. struct fec_enet_private *fep = netdev_priv(ndev);
  1995. unsigned int cycle;
  1996. if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
  1997. return -EOPNOTSUPP;
  1998. if (ec->rx_max_coalesced_frames > 255) {
  1999. pr_err("Rx coalesced frames exceed hardware limiation");
  2000. return -EINVAL;
  2001. }
  2002. if (ec->tx_max_coalesced_frames > 255) {
  2003. pr_err("Tx coalesced frame exceed hardware limiation");
  2004. return -EINVAL;
  2005. }
  2006. cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
  2007. if (cycle > 0xFFFF) {
  2008. pr_err("Rx coalesed usec exceeed hardware limiation");
  2009. return -EINVAL;
  2010. }
  2011. cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
  2012. if (cycle > 0xFFFF) {
  2013. pr_err("Rx coalesed usec exceeed hardware limiation");
  2014. return -EINVAL;
  2015. }
  2016. fep->rx_time_itr = ec->rx_coalesce_usecs;
  2017. fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
  2018. fep->tx_time_itr = ec->tx_coalesce_usecs;
  2019. fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
  2020. fec_enet_itr_coal_set(ndev);
  2021. return 0;
  2022. }
  2023. static void fec_enet_itr_coal_init(struct net_device *ndev)
  2024. {
  2025. struct ethtool_coalesce ec;
  2026. ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
  2027. ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
  2028. ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
  2029. ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
  2030. fec_enet_set_coalesce(ndev, &ec);
  2031. }
  2032. static int fec_enet_get_tunable(struct net_device *netdev,
  2033. const struct ethtool_tunable *tuna,
  2034. void *data)
  2035. {
  2036. struct fec_enet_private *fep = netdev_priv(netdev);
  2037. int ret = 0;
  2038. switch (tuna->id) {
  2039. case ETHTOOL_RX_COPYBREAK:
  2040. *(u32 *)data = fep->rx_copybreak;
  2041. break;
  2042. default:
  2043. ret = -EINVAL;
  2044. break;
  2045. }
  2046. return ret;
  2047. }
  2048. static int fec_enet_set_tunable(struct net_device *netdev,
  2049. const struct ethtool_tunable *tuna,
  2050. const void *data)
  2051. {
  2052. struct fec_enet_private *fep = netdev_priv(netdev);
  2053. int ret = 0;
  2054. switch (tuna->id) {
  2055. case ETHTOOL_RX_COPYBREAK:
  2056. fep->rx_copybreak = *(u32 *)data;
  2057. break;
  2058. default:
  2059. ret = -EINVAL;
  2060. break;
  2061. }
  2062. return ret;
  2063. }
  2064. static void
  2065. fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  2066. {
  2067. struct fec_enet_private *fep = netdev_priv(ndev);
  2068. if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
  2069. wol->supported = WAKE_MAGIC;
  2070. wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
  2071. } else {
  2072. wol->supported = wol->wolopts = 0;
  2073. }
  2074. }
  2075. static int
  2076. fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  2077. {
  2078. struct fec_enet_private *fep = netdev_priv(ndev);
  2079. if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
  2080. return -EINVAL;
  2081. if (wol->wolopts & ~WAKE_MAGIC)
  2082. return -EINVAL;
  2083. device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
  2084. if (device_may_wakeup(&ndev->dev)) {
  2085. fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
  2086. if (fep->irq[0] > 0)
  2087. enable_irq_wake(fep->irq[0]);
  2088. } else {
  2089. fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
  2090. if (fep->irq[0] > 0)
  2091. disable_irq_wake(fep->irq[0]);
  2092. }
  2093. return 0;
  2094. }
  2095. static const struct ethtool_ops fec_enet_ethtool_ops = {
  2096. .get_settings = fec_enet_get_settings,
  2097. .set_settings = fec_enet_set_settings,
  2098. .get_drvinfo = fec_enet_get_drvinfo,
  2099. .nway_reset = fec_enet_nway_reset,
  2100. .get_link = ethtool_op_get_link,
  2101. .get_coalesce = fec_enet_get_coalesce,
  2102. .set_coalesce = fec_enet_set_coalesce,
  2103. #ifndef CONFIG_M5272
  2104. .get_pauseparam = fec_enet_get_pauseparam,
  2105. .set_pauseparam = fec_enet_set_pauseparam,
  2106. .get_strings = fec_enet_get_strings,
  2107. .get_ethtool_stats = fec_enet_get_ethtool_stats,
  2108. .get_sset_count = fec_enet_get_sset_count,
  2109. #endif
  2110. .get_ts_info = fec_enet_get_ts_info,
  2111. .get_tunable = fec_enet_get_tunable,
  2112. .set_tunable = fec_enet_set_tunable,
  2113. .get_wol = fec_enet_get_wol,
  2114. .set_wol = fec_enet_set_wol,
  2115. };
  2116. static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  2117. {
  2118. struct fec_enet_private *fep = netdev_priv(ndev);
  2119. struct phy_device *phydev = fep->phy_dev;
  2120. if (!netif_running(ndev))
  2121. return -EINVAL;
  2122. if (!phydev)
  2123. return -ENODEV;
  2124. if (fep->bufdesc_ex) {
  2125. if (cmd == SIOCSHWTSTAMP)
  2126. return fec_ptp_set(ndev, rq);
  2127. if (cmd == SIOCGHWTSTAMP)
  2128. return fec_ptp_get(ndev, rq);
  2129. }
  2130. return phy_mii_ioctl(phydev, rq, cmd);
  2131. }
  2132. static void fec_enet_free_buffers(struct net_device *ndev)
  2133. {
  2134. struct fec_enet_private *fep = netdev_priv(ndev);
  2135. unsigned int i;
  2136. struct sk_buff *skb;
  2137. struct bufdesc *bdp;
  2138. struct fec_enet_priv_tx_q *txq;
  2139. struct fec_enet_priv_rx_q *rxq;
  2140. unsigned int q;
  2141. for (q = 0; q < fep->num_rx_queues; q++) {
  2142. rxq = fep->rx_queue[q];
  2143. bdp = rxq->rx_bd_base;
  2144. for (i = 0; i < rxq->rx_ring_size; i++) {
  2145. skb = rxq->rx_skbuff[i];
  2146. rxq->rx_skbuff[i] = NULL;
  2147. if (skb) {
  2148. dma_unmap_single(&fep->pdev->dev,
  2149. bdp->cbd_bufaddr,
  2150. FEC_ENET_RX_FRSIZE - fep->rx_align,
  2151. DMA_FROM_DEVICE);
  2152. dev_kfree_skb(skb);
  2153. }
  2154. bdp = fec_enet_get_nextdesc(bdp, fep, q);
  2155. }
  2156. }
  2157. for (q = 0; q < fep->num_tx_queues; q++) {
  2158. txq = fep->tx_queue[q];
  2159. bdp = txq->tx_bd_base;
  2160. for (i = 0; i < txq->tx_ring_size; i++) {
  2161. kfree(txq->tx_bounce[i]);
  2162. txq->tx_bounce[i] = NULL;
  2163. skb = txq->tx_skbuff[i];
  2164. txq->tx_skbuff[i] = NULL;
  2165. dev_kfree_skb(skb);
  2166. }
  2167. }
  2168. }
  2169. static void fec_enet_free_queue(struct net_device *ndev)
  2170. {
  2171. struct fec_enet_private *fep = netdev_priv(ndev);
  2172. int i;
  2173. struct fec_enet_priv_tx_q *txq;
  2174. for (i = 0; i < fep->num_tx_queues; i++)
  2175. if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
  2176. txq = fep->tx_queue[i];
  2177. dma_free_coherent(NULL,
  2178. txq->tx_ring_size * TSO_HEADER_SIZE,
  2179. txq->tso_hdrs,
  2180. txq->tso_hdrs_dma);
  2181. }
  2182. for (i = 0; i < fep->num_rx_queues; i++)
  2183. if (fep->rx_queue[i])
  2184. kfree(fep->rx_queue[i]);
  2185. for (i = 0; i < fep->num_tx_queues; i++)
  2186. if (fep->tx_queue[i])
  2187. kfree(fep->tx_queue[i]);
  2188. }
  2189. static int fec_enet_alloc_queue(struct net_device *ndev)
  2190. {
  2191. struct fec_enet_private *fep = netdev_priv(ndev);
  2192. int i;
  2193. int ret = 0;
  2194. struct fec_enet_priv_tx_q *txq;
  2195. for (i = 0; i < fep->num_tx_queues; i++) {
  2196. txq = kzalloc(sizeof(*txq), GFP_KERNEL);
  2197. if (!txq) {
  2198. ret = -ENOMEM;
  2199. goto alloc_failed;
  2200. }
  2201. fep->tx_queue[i] = txq;
  2202. txq->tx_ring_size = TX_RING_SIZE;
  2203. fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
  2204. txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
  2205. txq->tx_wake_threshold =
  2206. (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
  2207. txq->tso_hdrs = dma_alloc_coherent(NULL,
  2208. txq->tx_ring_size * TSO_HEADER_SIZE,
  2209. &txq->tso_hdrs_dma,
  2210. GFP_KERNEL);
  2211. if (!txq->tso_hdrs) {
  2212. ret = -ENOMEM;
  2213. goto alloc_failed;
  2214. }
  2215. }
  2216. for (i = 0; i < fep->num_rx_queues; i++) {
  2217. fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
  2218. GFP_KERNEL);
  2219. if (!fep->rx_queue[i]) {
  2220. ret = -ENOMEM;
  2221. goto alloc_failed;
  2222. }
  2223. fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
  2224. fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
  2225. }
  2226. return ret;
  2227. alloc_failed:
  2228. fec_enet_free_queue(ndev);
  2229. return ret;
  2230. }
  2231. static int
  2232. fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
  2233. {
  2234. struct fec_enet_private *fep = netdev_priv(ndev);
  2235. unsigned int i;
  2236. struct sk_buff *skb;
  2237. struct bufdesc *bdp;
  2238. struct fec_enet_priv_rx_q *rxq;
  2239. rxq = fep->rx_queue[queue];
  2240. bdp = rxq->rx_bd_base;
  2241. for (i = 0; i < rxq->rx_ring_size; i++) {
  2242. skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
  2243. if (!skb)
  2244. goto err_alloc;
  2245. if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
  2246. dev_kfree_skb(skb);
  2247. goto err_alloc;
  2248. }
  2249. rxq->rx_skbuff[i] = skb;
  2250. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  2251. if (fep->bufdesc_ex) {
  2252. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  2253. ebdp->cbd_esc = BD_ENET_RX_INT;
  2254. }
  2255. bdp = fec_enet_get_nextdesc(bdp, fep, queue);
  2256. }
  2257. /* Set the last buffer to wrap. */
  2258. bdp = fec_enet_get_prevdesc(bdp, fep, queue);
  2259. bdp->cbd_sc |= BD_SC_WRAP;
  2260. return 0;
  2261. err_alloc:
  2262. fec_enet_free_buffers(ndev);
  2263. return -ENOMEM;
  2264. }
  2265. static int
  2266. fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
  2267. {
  2268. struct fec_enet_private *fep = netdev_priv(ndev);
  2269. unsigned int i;
  2270. struct bufdesc *bdp;
  2271. struct fec_enet_priv_tx_q *txq;
  2272. txq = fep->tx_queue[queue];
  2273. bdp = txq->tx_bd_base;
  2274. for (i = 0; i < txq->tx_ring_size; i++) {
  2275. txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
  2276. if (!txq->tx_bounce[i])
  2277. goto err_alloc;
  2278. bdp->cbd_sc = 0;
  2279. bdp->cbd_bufaddr = 0;
  2280. if (fep->bufdesc_ex) {
  2281. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  2282. ebdp->cbd_esc = BD_ENET_TX_INT;
  2283. }
  2284. bdp = fec_enet_get_nextdesc(bdp, fep, queue);
  2285. }
  2286. /* Set the last buffer to wrap. */
  2287. bdp = fec_enet_get_prevdesc(bdp, fep, queue);
  2288. bdp->cbd_sc |= BD_SC_WRAP;
  2289. return 0;
  2290. err_alloc:
  2291. fec_enet_free_buffers(ndev);
  2292. return -ENOMEM;
  2293. }
  2294. static int fec_enet_alloc_buffers(struct net_device *ndev)
  2295. {
  2296. struct fec_enet_private *fep = netdev_priv(ndev);
  2297. unsigned int i;
  2298. for (i = 0; i < fep->num_rx_queues; i++)
  2299. if (fec_enet_alloc_rxq_buffers(ndev, i))
  2300. return -ENOMEM;
  2301. for (i = 0; i < fep->num_tx_queues; i++)
  2302. if (fec_enet_alloc_txq_buffers(ndev, i))
  2303. return -ENOMEM;
  2304. return 0;
  2305. }
  2306. static int
  2307. fec_enet_open(struct net_device *ndev)
  2308. {
  2309. struct fec_enet_private *fep = netdev_priv(ndev);
  2310. int ret;
  2311. pinctrl_pm_select_default_state(&fep->pdev->dev);
  2312. ret = fec_enet_clk_enable(ndev, true);
  2313. if (ret)
  2314. return ret;
  2315. /* I should reset the ring buffers here, but I don't yet know
  2316. * a simple way to do that.
  2317. */
  2318. ret = fec_enet_alloc_buffers(ndev);
  2319. if (ret)
  2320. goto err_enet_alloc;
  2321. /* Probe and connect to PHY when open the interface */
  2322. ret = fec_enet_mii_probe(ndev);
  2323. if (ret)
  2324. goto err_enet_mii_probe;
  2325. fec_restart(ndev);
  2326. napi_enable(&fep->napi);
  2327. phy_start(fep->phy_dev);
  2328. netif_tx_start_all_queues(ndev);
  2329. device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
  2330. FEC_WOL_FLAG_ENABLE);
  2331. return 0;
  2332. err_enet_mii_probe:
  2333. fec_enet_free_buffers(ndev);
  2334. err_enet_alloc:
  2335. fec_enet_clk_enable(ndev, false);
  2336. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  2337. return ret;
  2338. }
  2339. static int
  2340. fec_enet_close(struct net_device *ndev)
  2341. {
  2342. struct fec_enet_private *fep = netdev_priv(ndev);
  2343. phy_stop(fep->phy_dev);
  2344. if (netif_device_present(ndev)) {
  2345. napi_disable(&fep->napi);
  2346. netif_tx_disable(ndev);
  2347. fec_stop(ndev);
  2348. }
  2349. phy_disconnect(fep->phy_dev);
  2350. fep->phy_dev = NULL;
  2351. fec_enet_clk_enable(ndev, false);
  2352. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  2353. fec_enet_free_buffers(ndev);
  2354. return 0;
  2355. }
  2356. /* Set or clear the multicast filter for this adaptor.
  2357. * Skeleton taken from sunlance driver.
  2358. * The CPM Ethernet implementation allows Multicast as well as individual
  2359. * MAC address filtering. Some of the drivers check to make sure it is
  2360. * a group multicast address, and discard those that are not. I guess I
  2361. * will do the same for now, but just remove the test if you want
  2362. * individual filtering as well (do the upper net layers want or support
  2363. * this kind of feature?).
  2364. */
  2365. #define HASH_BITS 6 /* #bits in hash */
  2366. #define CRC32_POLY 0xEDB88320
  2367. static void set_multicast_list(struct net_device *ndev)
  2368. {
  2369. struct fec_enet_private *fep = netdev_priv(ndev);
  2370. struct netdev_hw_addr *ha;
  2371. unsigned int i, bit, data, crc, tmp;
  2372. unsigned char hash;
  2373. if (ndev->flags & IFF_PROMISC) {
  2374. tmp = readl(fep->hwp + FEC_R_CNTRL);
  2375. tmp |= 0x8;
  2376. writel(tmp, fep->hwp + FEC_R_CNTRL);
  2377. return;
  2378. }
  2379. tmp = readl(fep->hwp + FEC_R_CNTRL);
  2380. tmp &= ~0x8;
  2381. writel(tmp, fep->hwp + FEC_R_CNTRL);
  2382. if (ndev->flags & IFF_ALLMULTI) {
  2383. /* Catch all multicast addresses, so set the
  2384. * filter to all 1's
  2385. */
  2386. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  2387. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  2388. return;
  2389. }
  2390. /* Clear filter and add the addresses in hash register
  2391. */
  2392. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  2393. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  2394. netdev_for_each_mc_addr(ha, ndev) {
  2395. /* calculate crc32 value of mac address */
  2396. crc = 0xffffffff;
  2397. for (i = 0; i < ndev->addr_len; i++) {
  2398. data = ha->addr[i];
  2399. for (bit = 0; bit < 8; bit++, data >>= 1) {
  2400. crc = (crc >> 1) ^
  2401. (((crc ^ data) & 1) ? CRC32_POLY : 0);
  2402. }
  2403. }
  2404. /* only upper 6 bits (HASH_BITS) are used
  2405. * which point to specific bit in he hash registers
  2406. */
  2407. hash = (crc >> (32 - HASH_BITS)) & 0x3f;
  2408. if (hash > 31) {
  2409. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  2410. tmp |= 1 << (hash - 32);
  2411. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  2412. } else {
  2413. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  2414. tmp |= 1 << hash;
  2415. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  2416. }
  2417. }
  2418. }
  2419. /* Set a MAC change in hardware. */
  2420. static int
  2421. fec_set_mac_address(struct net_device *ndev, void *p)
  2422. {
  2423. struct fec_enet_private *fep = netdev_priv(ndev);
  2424. struct sockaddr *addr = p;
  2425. if (addr) {
  2426. if (!is_valid_ether_addr(addr->sa_data))
  2427. return -EADDRNOTAVAIL;
  2428. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  2429. }
  2430. writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
  2431. (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
  2432. fep->hwp + FEC_ADDR_LOW);
  2433. writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
  2434. fep->hwp + FEC_ADDR_HIGH);
  2435. return 0;
  2436. }
  2437. #ifdef CONFIG_NET_POLL_CONTROLLER
  2438. /**
  2439. * fec_poll_controller - FEC Poll controller function
  2440. * @dev: The FEC network adapter
  2441. *
  2442. * Polled functionality used by netconsole and others in non interrupt mode
  2443. *
  2444. */
  2445. static void fec_poll_controller(struct net_device *dev)
  2446. {
  2447. int i;
  2448. struct fec_enet_private *fep = netdev_priv(dev);
  2449. for (i = 0; i < FEC_IRQ_NUM; i++) {
  2450. if (fep->irq[i] > 0) {
  2451. disable_irq(fep->irq[i]);
  2452. fec_enet_interrupt(fep->irq[i], dev);
  2453. enable_irq(fep->irq[i]);
  2454. }
  2455. }
  2456. }
  2457. #endif
  2458. #define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
  2459. static inline void fec_enet_set_netdev_features(struct net_device *netdev,
  2460. netdev_features_t features)
  2461. {
  2462. struct fec_enet_private *fep = netdev_priv(netdev);
  2463. netdev_features_t changed = features ^ netdev->features;
  2464. netdev->features = features;
  2465. /* Receive checksum has been changed */
  2466. if (changed & NETIF_F_RXCSUM) {
  2467. if (features & NETIF_F_RXCSUM)
  2468. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  2469. else
  2470. fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
  2471. }
  2472. }
  2473. static int fec_set_features(struct net_device *netdev,
  2474. netdev_features_t features)
  2475. {
  2476. struct fec_enet_private *fep = netdev_priv(netdev);
  2477. netdev_features_t changed = features ^ netdev->features;
  2478. if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
  2479. napi_disable(&fep->napi);
  2480. netif_tx_lock_bh(netdev);
  2481. fec_stop(netdev);
  2482. fec_enet_set_netdev_features(netdev, features);
  2483. fec_restart(netdev);
  2484. netif_tx_wake_all_queues(netdev);
  2485. netif_tx_unlock_bh(netdev);
  2486. napi_enable(&fep->napi);
  2487. } else {
  2488. fec_enet_set_netdev_features(netdev, features);
  2489. }
  2490. return 0;
  2491. }
  2492. static const struct net_device_ops fec_netdev_ops = {
  2493. .ndo_open = fec_enet_open,
  2494. .ndo_stop = fec_enet_close,
  2495. .ndo_start_xmit = fec_enet_start_xmit,
  2496. .ndo_set_rx_mode = set_multicast_list,
  2497. .ndo_change_mtu = eth_change_mtu,
  2498. .ndo_validate_addr = eth_validate_addr,
  2499. .ndo_tx_timeout = fec_timeout,
  2500. .ndo_set_mac_address = fec_set_mac_address,
  2501. .ndo_do_ioctl = fec_enet_ioctl,
  2502. #ifdef CONFIG_NET_POLL_CONTROLLER
  2503. .ndo_poll_controller = fec_poll_controller,
  2504. #endif
  2505. .ndo_set_features = fec_set_features,
  2506. };
  2507. /*
  2508. * XXX: We need to clean up on failure exits here.
  2509. *
  2510. */
  2511. static int fec_enet_init(struct net_device *ndev)
  2512. {
  2513. struct fec_enet_private *fep = netdev_priv(ndev);
  2514. struct fec_enet_priv_tx_q *txq;
  2515. struct fec_enet_priv_rx_q *rxq;
  2516. struct bufdesc *cbd_base;
  2517. dma_addr_t bd_dma;
  2518. int bd_size;
  2519. unsigned int i;
  2520. #if defined(CONFIG_ARM)
  2521. fep->rx_align = 0xf;
  2522. fep->tx_align = 0xf;
  2523. #else
  2524. fep->rx_align = 0x3;
  2525. fep->tx_align = 0x3;
  2526. #endif
  2527. fec_enet_alloc_queue(ndev);
  2528. if (fep->bufdesc_ex)
  2529. fep->bufdesc_size = sizeof(struct bufdesc_ex);
  2530. else
  2531. fep->bufdesc_size = sizeof(struct bufdesc);
  2532. bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
  2533. fep->bufdesc_size;
  2534. /* Allocate memory for buffer descriptors. */
  2535. cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
  2536. GFP_KERNEL);
  2537. if (!cbd_base) {
  2538. return -ENOMEM;
  2539. }
  2540. memset(cbd_base, 0, bd_size);
  2541. /* Get the Ethernet address */
  2542. fec_get_mac(ndev);
  2543. /* make sure MAC we just acquired is programmed into the hw */
  2544. fec_set_mac_address(ndev, NULL);
  2545. /* Set receive and transmit descriptor base. */
  2546. for (i = 0; i < fep->num_rx_queues; i++) {
  2547. rxq = fep->rx_queue[i];
  2548. rxq->index = i;
  2549. rxq->rx_bd_base = (struct bufdesc *)cbd_base;
  2550. rxq->bd_dma = bd_dma;
  2551. if (fep->bufdesc_ex) {
  2552. bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
  2553. cbd_base = (struct bufdesc *)
  2554. (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
  2555. } else {
  2556. bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
  2557. cbd_base += rxq->rx_ring_size;
  2558. }
  2559. }
  2560. for (i = 0; i < fep->num_tx_queues; i++) {
  2561. txq = fep->tx_queue[i];
  2562. txq->index = i;
  2563. txq->tx_bd_base = (struct bufdesc *)cbd_base;
  2564. txq->bd_dma = bd_dma;
  2565. if (fep->bufdesc_ex) {
  2566. bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
  2567. cbd_base = (struct bufdesc *)
  2568. (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
  2569. } else {
  2570. bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
  2571. cbd_base += txq->tx_ring_size;
  2572. }
  2573. }
  2574. /* The FEC Ethernet specific entries in the device structure */
  2575. ndev->watchdog_timeo = TX_TIMEOUT;
  2576. ndev->netdev_ops = &fec_netdev_ops;
  2577. ndev->ethtool_ops = &fec_enet_ethtool_ops;
  2578. writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
  2579. netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
  2580. if (fep->quirks & FEC_QUIRK_HAS_VLAN)
  2581. /* enable hw VLAN support */
  2582. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  2583. if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
  2584. ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
  2585. /* enable hw accelerator */
  2586. ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
  2587. | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
  2588. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  2589. }
  2590. if (fep->quirks & FEC_QUIRK_HAS_AVB) {
  2591. fep->tx_align = 0;
  2592. fep->rx_align = 0x3f;
  2593. }
  2594. ndev->hw_features = ndev->features;
  2595. fec_restart(ndev);
  2596. return 0;
  2597. }
  2598. #ifdef CONFIG_OF
  2599. static void fec_reset_phy(struct platform_device *pdev)
  2600. {
  2601. int err, phy_reset;
  2602. int msec = 1;
  2603. struct device_node *np = pdev->dev.of_node;
  2604. if (!np)
  2605. return;
  2606. of_property_read_u32(np, "phy-reset-duration", &msec);
  2607. /* A sane reset duration should not be longer than 1s */
  2608. if (msec > 1000)
  2609. msec = 1;
  2610. phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
  2611. if (!gpio_is_valid(phy_reset))
  2612. return;
  2613. err = devm_gpio_request_one(&pdev->dev, phy_reset,
  2614. GPIOF_OUT_INIT_LOW, "phy-reset");
  2615. if (err) {
  2616. dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
  2617. return;
  2618. }
  2619. msleep(msec);
  2620. gpio_set_value(phy_reset, 1);
  2621. }
  2622. #else /* CONFIG_OF */
  2623. static void fec_reset_phy(struct platform_device *pdev)
  2624. {
  2625. /*
  2626. * In case of platform probe, the reset has been done
  2627. * by machine code.
  2628. */
  2629. }
  2630. #endif /* CONFIG_OF */
  2631. static void
  2632. fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
  2633. {
  2634. struct device_node *np = pdev->dev.of_node;
  2635. int err;
  2636. *num_tx = *num_rx = 1;
  2637. if (!np || !of_device_is_available(np))
  2638. return;
  2639. /* parse the num of tx and rx queues */
  2640. err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
  2641. if (err)
  2642. *num_tx = 1;
  2643. err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
  2644. if (err)
  2645. *num_rx = 1;
  2646. if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
  2647. dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
  2648. *num_tx);
  2649. *num_tx = 1;
  2650. return;
  2651. }
  2652. if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
  2653. dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
  2654. *num_rx);
  2655. *num_rx = 1;
  2656. return;
  2657. }
  2658. }
  2659. static int
  2660. fec_probe(struct platform_device *pdev)
  2661. {
  2662. struct fec_enet_private *fep;
  2663. struct fec_platform_data *pdata;
  2664. struct net_device *ndev;
  2665. int i, irq, ret = 0;
  2666. struct resource *r;
  2667. const struct of_device_id *of_id;
  2668. static int dev_id;
  2669. struct device_node *np = pdev->dev.of_node, *phy_node;
  2670. int num_tx_qs;
  2671. int num_rx_qs;
  2672. fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
  2673. /* Init network device */
  2674. ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
  2675. num_tx_qs, num_rx_qs);
  2676. if (!ndev)
  2677. return -ENOMEM;
  2678. SET_NETDEV_DEV(ndev, &pdev->dev);
  2679. /* setup board info structure */
  2680. fep = netdev_priv(ndev);
  2681. of_id = of_match_device(fec_dt_ids, &pdev->dev);
  2682. if (of_id)
  2683. pdev->id_entry = of_id->data;
  2684. fep->quirks = pdev->id_entry->driver_data;
  2685. fep->netdev = ndev;
  2686. fep->num_rx_queues = num_rx_qs;
  2687. fep->num_tx_queues = num_tx_qs;
  2688. #if !defined(CONFIG_M5272)
  2689. /* default enable pause frame auto negotiation */
  2690. if (fep->quirks & FEC_QUIRK_HAS_GBIT)
  2691. fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
  2692. #endif
  2693. /* Select default pin state */
  2694. pinctrl_pm_select_default_state(&pdev->dev);
  2695. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2696. fep->hwp = devm_ioremap_resource(&pdev->dev, r);
  2697. if (IS_ERR(fep->hwp)) {
  2698. ret = PTR_ERR(fep->hwp);
  2699. goto failed_ioremap;
  2700. }
  2701. fep->pdev = pdev;
  2702. fep->dev_id = dev_id++;
  2703. platform_set_drvdata(pdev, ndev);
  2704. if (of_get_property(np, "fsl,magic-packet", NULL))
  2705. fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
  2706. phy_node = of_parse_phandle(np, "phy-handle", 0);
  2707. if (!phy_node && of_phy_is_fixed_link(np)) {
  2708. ret = of_phy_register_fixed_link(np);
  2709. if (ret < 0) {
  2710. dev_err(&pdev->dev,
  2711. "broken fixed-link specification\n");
  2712. goto failed_phy;
  2713. }
  2714. phy_node = of_node_get(np);
  2715. }
  2716. fep->phy_node = phy_node;
  2717. ret = of_get_phy_mode(pdev->dev.of_node);
  2718. if (ret < 0) {
  2719. pdata = dev_get_platdata(&pdev->dev);
  2720. if (pdata)
  2721. fep->phy_interface = pdata->phy;
  2722. else
  2723. fep->phy_interface = PHY_INTERFACE_MODE_MII;
  2724. } else {
  2725. fep->phy_interface = ret;
  2726. }
  2727. fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  2728. if (IS_ERR(fep->clk_ipg)) {
  2729. ret = PTR_ERR(fep->clk_ipg);
  2730. goto failed_clk;
  2731. }
  2732. fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  2733. if (IS_ERR(fep->clk_ahb)) {
  2734. ret = PTR_ERR(fep->clk_ahb);
  2735. goto failed_clk;
  2736. }
  2737. fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
  2738. /* enet_out is optional, depends on board */
  2739. fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
  2740. if (IS_ERR(fep->clk_enet_out))
  2741. fep->clk_enet_out = NULL;
  2742. fep->ptp_clk_on = false;
  2743. mutex_init(&fep->ptp_clk_mutex);
  2744. /* clk_ref is optional, depends on board */
  2745. fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
  2746. if (IS_ERR(fep->clk_ref))
  2747. fep->clk_ref = NULL;
  2748. fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
  2749. fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
  2750. if (IS_ERR(fep->clk_ptp)) {
  2751. fep->clk_ptp = NULL;
  2752. fep->bufdesc_ex = false;
  2753. }
  2754. ret = fec_enet_clk_enable(ndev, true);
  2755. if (ret)
  2756. goto failed_clk;
  2757. fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
  2758. if (!IS_ERR(fep->reg_phy)) {
  2759. ret = regulator_enable(fep->reg_phy);
  2760. if (ret) {
  2761. dev_err(&pdev->dev,
  2762. "Failed to enable phy regulator: %d\n", ret);
  2763. goto failed_regulator;
  2764. }
  2765. } else {
  2766. fep->reg_phy = NULL;
  2767. }
  2768. fec_reset_phy(pdev);
  2769. if (fep->bufdesc_ex)
  2770. fec_ptp_init(pdev);
  2771. ret = fec_enet_init(ndev);
  2772. if (ret)
  2773. goto failed_init;
  2774. for (i = 0; i < FEC_IRQ_NUM; i++) {
  2775. irq = platform_get_irq(pdev, i);
  2776. if (irq < 0) {
  2777. if (i)
  2778. break;
  2779. ret = irq;
  2780. goto failed_irq;
  2781. }
  2782. ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
  2783. 0, pdev->name, ndev);
  2784. if (ret)
  2785. goto failed_irq;
  2786. fep->irq[i] = irq;
  2787. }
  2788. init_completion(&fep->mdio_done);
  2789. ret = fec_enet_mii_init(pdev);
  2790. if (ret)
  2791. goto failed_mii_init;
  2792. /* Carrier starts down, phylib will bring it up */
  2793. netif_carrier_off(ndev);
  2794. fec_enet_clk_enable(ndev, false);
  2795. pinctrl_pm_select_sleep_state(&pdev->dev);
  2796. ret = register_netdev(ndev);
  2797. if (ret)
  2798. goto failed_register;
  2799. device_init_wakeup(&ndev->dev, fep->wol_flag &
  2800. FEC_WOL_HAS_MAGIC_PACKET);
  2801. if (fep->bufdesc_ex && fep->ptp_clock)
  2802. netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
  2803. fep->rx_copybreak = COPYBREAK_DEFAULT;
  2804. INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
  2805. return 0;
  2806. failed_register:
  2807. fec_enet_mii_remove(fep);
  2808. failed_mii_init:
  2809. failed_irq:
  2810. failed_init:
  2811. if (fep->reg_phy)
  2812. regulator_disable(fep->reg_phy);
  2813. failed_regulator:
  2814. fec_enet_clk_enable(ndev, false);
  2815. failed_clk:
  2816. failed_phy:
  2817. of_node_put(phy_node);
  2818. failed_ioremap:
  2819. free_netdev(ndev);
  2820. return ret;
  2821. }
  2822. static int
  2823. fec_drv_remove(struct platform_device *pdev)
  2824. {
  2825. struct net_device *ndev = platform_get_drvdata(pdev);
  2826. struct fec_enet_private *fep = netdev_priv(ndev);
  2827. cancel_delayed_work_sync(&fep->time_keep);
  2828. cancel_work_sync(&fep->tx_timeout_work);
  2829. unregister_netdev(ndev);
  2830. fec_enet_mii_remove(fep);
  2831. if (fep->reg_phy)
  2832. regulator_disable(fep->reg_phy);
  2833. if (fep->ptp_clock)
  2834. ptp_clock_unregister(fep->ptp_clock);
  2835. fec_enet_clk_enable(ndev, false);
  2836. of_node_put(fep->phy_node);
  2837. free_netdev(ndev);
  2838. return 0;
  2839. }
  2840. static int __maybe_unused fec_suspend(struct device *dev)
  2841. {
  2842. struct net_device *ndev = dev_get_drvdata(dev);
  2843. struct fec_enet_private *fep = netdev_priv(ndev);
  2844. rtnl_lock();
  2845. if (netif_running(ndev)) {
  2846. if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
  2847. fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
  2848. phy_stop(fep->phy_dev);
  2849. napi_disable(&fep->napi);
  2850. netif_tx_lock_bh(ndev);
  2851. netif_device_detach(ndev);
  2852. netif_tx_unlock_bh(ndev);
  2853. fec_stop(ndev);
  2854. fec_enet_clk_enable(ndev, false);
  2855. if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
  2856. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  2857. }
  2858. rtnl_unlock();
  2859. if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
  2860. regulator_disable(fep->reg_phy);
  2861. /* SOC supply clock to phy, when clock is disabled, phy link down
  2862. * SOC control phy regulator, when regulator is disabled, phy link down
  2863. */
  2864. if (fep->clk_enet_out || fep->reg_phy)
  2865. fep->link = 0;
  2866. return 0;
  2867. }
  2868. static int __maybe_unused fec_resume(struct device *dev)
  2869. {
  2870. struct net_device *ndev = dev_get_drvdata(dev);
  2871. struct fec_enet_private *fep = netdev_priv(ndev);
  2872. struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
  2873. int ret;
  2874. int val;
  2875. if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
  2876. ret = regulator_enable(fep->reg_phy);
  2877. if (ret)
  2878. return ret;
  2879. }
  2880. rtnl_lock();
  2881. if (netif_running(ndev)) {
  2882. ret = fec_enet_clk_enable(ndev, true);
  2883. if (ret) {
  2884. rtnl_unlock();
  2885. goto failed_clk;
  2886. }
  2887. if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
  2888. if (pdata && pdata->sleep_mode_enable)
  2889. pdata->sleep_mode_enable(false);
  2890. val = readl(fep->hwp + FEC_ECNTRL);
  2891. val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
  2892. writel(val, fep->hwp + FEC_ECNTRL);
  2893. fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
  2894. } else {
  2895. pinctrl_pm_select_default_state(&fep->pdev->dev);
  2896. }
  2897. fec_restart(ndev);
  2898. netif_tx_lock_bh(ndev);
  2899. netif_device_attach(ndev);
  2900. netif_tx_unlock_bh(ndev);
  2901. napi_enable(&fep->napi);
  2902. phy_start(fep->phy_dev);
  2903. }
  2904. rtnl_unlock();
  2905. return 0;
  2906. failed_clk:
  2907. if (fep->reg_phy)
  2908. regulator_disable(fep->reg_phy);
  2909. return ret;
  2910. }
  2911. static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
  2912. static struct platform_driver fec_driver = {
  2913. .driver = {
  2914. .name = DRIVER_NAME,
  2915. .pm = &fec_pm_ops,
  2916. .of_match_table = fec_dt_ids,
  2917. },
  2918. .id_table = fec_devtype,
  2919. .probe = fec_probe,
  2920. .remove = fec_drv_remove,
  2921. };
  2922. module_platform_driver(fec_driver);
  2923. MODULE_ALIAS("platform:"DRIVER_NAME);
  2924. MODULE_LICENSE("GPL");