fec_main.c 86 KB

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