fec_main.c 81 KB

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