fec_main.c 88 KB

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