fec_main.c 88 KB

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