m68k-coldfire-fec.patch 95 KB

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