dwc_eth_qos.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999
  1. /* Synopsys DWC Ethernet Quality-of-Service v4.10a linux driver
  2. *
  3. * This is a driver for the Synopsys DWC Ethernet QoS IP version 4.10a (GMAC).
  4. * This version introduced a lot of changes which breaks backwards
  5. * compatibility the non-QoS IP from Synopsys (used in the ST Micro drivers).
  6. * Some fields differ between version 4.00a and 4.10a, mainly the interrupt
  7. * bit fields. The driver could be made compatible with 4.00, if all relevant
  8. * HW erratas are handled.
  9. *
  10. * The GMAC is highly configurable at synthesis time. This driver has been
  11. * developed for a subset of the total available feature set. Currently
  12. * it supports:
  13. * - TSO
  14. * - Checksum offload for RX and TX.
  15. * - Energy efficient ethernet.
  16. * - GMII phy interface.
  17. * - The statistics module.
  18. * - Single RX and TX queue.
  19. *
  20. * Copyright (C) 2015 Axis Communications AB.
  21. *
  22. * This program is free software; you can redistribute it and/or modify it
  23. * under the terms and conditions of the GNU General Public License,
  24. * version 2, as published by the Free Software Foundation.
  25. */
  26. #include <linux/clk.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/init.h>
  30. #include <linux/io.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/stat.h>
  33. #include <linux/types.h>
  34. #include <linux/slab.h>
  35. #include <linux/delay.h>
  36. #include <linux/mm.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/etherdevice.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/phy.h>
  41. #include <linux/mii.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/vmalloc.h>
  44. #include <linux/device.h>
  45. #include <linux/bitrev.h>
  46. #include <linux/crc32.h>
  47. #include <linux/of.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/clocksource.h>
  50. #include <linux/net_tstamp.h>
  51. #include <linux/pm_runtime.h>
  52. #include <linux/of_net.h>
  53. #include <linux/of_address.h>
  54. #include <linux/of_mdio.h>
  55. #include <linux/timer.h>
  56. #include <linux/tcp.h>
  57. #define DRIVER_NAME "dwceqos"
  58. #define DRIVER_DESCRIPTION "Synopsys DWC Ethernet QoS driver"
  59. #define DRIVER_VERSION "0.9"
  60. #define DWCEQOS_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
  61. NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
  62. #define DWCEQOS_TX_TIMEOUT 5 /* Seconds */
  63. #define DWCEQOS_LPI_TIMER_MIN 8
  64. #define DWCEQOS_LPI_TIMER_MAX ((1 << 20) - 1)
  65. #define DWCEQOS_RX_BUF_SIZE 2048
  66. #define DWCEQOS_RX_DCNT 256
  67. #define DWCEQOS_TX_DCNT 256
  68. #define DWCEQOS_HASH_TABLE_SIZE 64
  69. /* The size field in the DMA descriptor is 14 bits */
  70. #define BYTES_PER_DMA_DESC 16376
  71. /* Hardware registers */
  72. #define START_MAC_REG_OFFSET 0x0000
  73. #define MAX_MAC_REG_OFFSET 0x0bd0
  74. #define START_MTL_REG_OFFSET 0x0c00
  75. #define MAX_MTL_REG_OFFSET 0x0d7c
  76. #define START_DMA_REG_OFFSET 0x1000
  77. #define MAX_DMA_REG_OFFSET 0x117C
  78. #define REG_SPACE_SIZE 0x1800
  79. /* DMA */
  80. #define REG_DWCEQOS_DMA_MODE 0x1000
  81. #define REG_DWCEQOS_DMA_SYSBUS_MODE 0x1004
  82. #define REG_DWCEQOS_DMA_IS 0x1008
  83. #define REG_DWCEQOS_DMA_DEBUG_ST0 0x100c
  84. /* DMA channel registers */
  85. #define REG_DWCEQOS_DMA_CH0_CTRL 0x1100
  86. #define REG_DWCEQOS_DMA_CH0_TX_CTRL 0x1104
  87. #define REG_DWCEQOS_DMA_CH0_RX_CTRL 0x1108
  88. #define REG_DWCEQOS_DMA_CH0_TXDESC_LIST 0x1114
  89. #define REG_DWCEQOS_DMA_CH0_RXDESC_LIST 0x111c
  90. #define REG_DWCEQOS_DMA_CH0_TXDESC_TAIL 0x1120
  91. #define REG_DWCEQOS_DMA_CH0_RXDESC_TAIL 0x1128
  92. #define REG_DWCEQOS_DMA_CH0_TXDESC_LEN 0x112c
  93. #define REG_DWCEQOS_DMA_CH0_RXDESC_LEN 0x1130
  94. #define REG_DWCEQOS_DMA_CH0_IE 0x1134
  95. #define REG_DWCEQOS_DMA_CH0_CUR_TXDESC 0x1144
  96. #define REG_DWCEQOS_DMA_CH0_CUR_RXDESC 0x114c
  97. #define REG_DWCEQOS_DMA_CH0_CUR_TXBUF 0x1154
  98. #define REG_DWCEQOS_DMA_CH0_CUR_RXBUG 0x115c
  99. #define REG_DWCEQOS_DMA_CH0_STA 0x1160
  100. #define DWCEQOS_DMA_MODE_TXPR BIT(11)
  101. #define DWCEQOS_DMA_MODE_DA BIT(1)
  102. #define DWCEQOS_DMA_SYSBUS_MODE_EN_LPI BIT(31)
  103. #define DWCEQOS_DMA_SYSBUS_MODE_FB BIT(0)
  104. #define DWCEQOS_DMA_SYSBUS_MODE_AAL BIT(12)
  105. #define DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT(x) \
  106. (((x) << 16) & 0x000F0000)
  107. #define DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT_DEFAULT 3
  108. #define DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT_MASK GENMASK(19, 16)
  109. #define DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT(x) \
  110. (((x) << 24) & 0x0F000000)
  111. #define DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT_DEFAULT 3
  112. #define DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT_MASK GENMASK(27, 24)
  113. #define DWCEQOS_DMA_SYSBUS_MODE_BURST_MASK GENMASK(7, 1)
  114. #define DWCEQOS_DMA_SYSBUS_MODE_BURST(x) \
  115. (((x) << 1) & DWCEQOS_DMA_SYSBUS_MODE_BURST_MASK)
  116. #define DWCEQOS_DMA_SYSBUS_MODE_BURST_DEFAULT GENMASK(3, 1)
  117. #define DWCEQOS_DMA_CH_CTRL_PBLX8 BIT(16)
  118. #define DWCEQOS_DMA_CH_CTRL_DSL(x) ((x) << 18)
  119. #define DWCEQOS_DMA_CH_CTRL_PBL(x) ((x) << 16)
  120. #define DWCEQOS_DMA_CH_CTRL_START BIT(0)
  121. #define DWCEQOS_DMA_CH_RX_CTRL_BUFSIZE(x) ((x) << 1)
  122. #define DWCEQOS_DMA_CH_TX_OSP BIT(4)
  123. #define DWCEQOS_DMA_CH_TX_TSE BIT(12)
  124. #define DWCEQOS_DMA_CH0_IE_NIE BIT(15)
  125. #define DWCEQOS_DMA_CH0_IE_AIE BIT(14)
  126. #define DWCEQOS_DMA_CH0_IE_RIE BIT(6)
  127. #define DWCEQOS_DMA_CH0_IE_TIE BIT(0)
  128. #define DWCEQOS_DMA_CH0_IE_FBEE BIT(12)
  129. #define DWCEQOS_DMA_CH0_IE_RBUE BIT(7)
  130. #define DWCEQOS_DMA_IS_DC0IS BIT(0)
  131. #define DWCEQOS_DMA_IS_MTLIS BIT(16)
  132. #define DWCEQOS_DMA_IS_MACIS BIT(17)
  133. #define DWCEQOS_DMA_CH0_IS_TI BIT(0)
  134. #define DWCEQOS_DMA_CH0_IS_RI BIT(6)
  135. #define DWCEQOS_DMA_CH0_IS_RBU BIT(7)
  136. #define DWCEQOS_DMA_CH0_IS_FBE BIT(12)
  137. #define DWCEQOS_DMA_CH0_IS_CDE BIT(13)
  138. #define DWCEQOS_DMA_CH0_IS_AIS BIT(14)
  139. #define DWCEQOS_DMA_CH0_IS_TEB GENMASK(18, 16)
  140. #define DWCEQOS_DMA_CH0_IS_TX_ERR_READ BIT(16)
  141. #define DWCEQOS_DMA_CH0_IS_TX_ERR_DESCR BIT(17)
  142. #define DWCEQOS_DMA_CH0_IS_REB GENMASK(21, 19)
  143. #define DWCEQOS_DMA_CH0_IS_RX_ERR_READ BIT(19)
  144. #define DWCEQOS_DMA_CH0_IS_RX_ERR_DESCR BIT(20)
  145. /* DMA descriptor bits for RX normal descriptor (read format) */
  146. #define DWCEQOS_DMA_RDES3_OWN BIT(31)
  147. #define DWCEQOS_DMA_RDES3_INTE BIT(30)
  148. #define DWCEQOS_DMA_RDES3_BUF2V BIT(25)
  149. #define DWCEQOS_DMA_RDES3_BUF1V BIT(24)
  150. /* DMA descriptor bits for RX normal descriptor (write back format) */
  151. #define DWCEQOS_DMA_RDES1_IPCE BIT(7)
  152. #define DWCEQOS_DMA_RDES3_ES BIT(15)
  153. #define DWCEQOS_DMA_RDES3_E_JT BIT(14)
  154. #define DWCEQOS_DMA_RDES3_PL(x) ((x) & 0x7fff)
  155. #define DWCEQOS_DMA_RDES1_PT 0x00000007
  156. #define DWCEQOS_DMA_RDES1_PT_UDP BIT(0)
  157. #define DWCEQOS_DMA_RDES1_PT_TCP BIT(1)
  158. #define DWCEQOS_DMA_RDES1_PT_ICMP 0x00000003
  159. /* DMA descriptor bits for TX normal descriptor (read format) */
  160. #define DWCEQOS_DMA_TDES2_IOC BIT(31)
  161. #define DWCEQOS_DMA_TDES3_OWN BIT(31)
  162. #define DWCEQOS_DMA_TDES3_CTXT BIT(30)
  163. #define DWCEQOS_DMA_TDES3_FD BIT(29)
  164. #define DWCEQOS_DMA_TDES3_LD BIT(28)
  165. #define DWCEQOS_DMA_TDES3_CIPH BIT(16)
  166. #define DWCEQOS_DMA_TDES3_CIPP BIT(17)
  167. #define DWCEQOS_DMA_TDES3_CA 0x00030000
  168. #define DWCEQOS_DMA_TDES3_TSE BIT(18)
  169. #define DWCEQOS_DMA_DES3_THL(x) ((x) << 19)
  170. #define DWCEQOS_DMA_DES2_B2L(x) ((x) << 16)
  171. #define DWCEQOS_DMA_TDES3_TCMSSV BIT(26)
  172. /* DMA channel states */
  173. #define DMA_TX_CH_STOPPED 0
  174. #define DMA_TX_CH_SUSPENDED 6
  175. #define DMA_GET_TX_STATE_CH0(status0) ((status0 & 0xF000) >> 12)
  176. /* MTL */
  177. #define REG_DWCEQOS_MTL_OPER 0x0c00
  178. #define REG_DWCEQOS_MTL_DEBUG_ST 0x0c0c
  179. #define REG_DWCEQOS_MTL_TXQ0_DEBUG_ST 0x0d08
  180. #define REG_DWCEQOS_MTL_RXQ0_DEBUG_ST 0x0d38
  181. #define REG_DWCEQOS_MTL_IS 0x0c20
  182. #define REG_DWCEQOS_MTL_TXQ0_OPER 0x0d00
  183. #define REG_DWCEQOS_MTL_RXQ0_OPER 0x0d30
  184. #define REG_DWCEQOS_MTL_RXQ0_MIS_CNT 0x0d34
  185. #define REG_DWCEQOS_MTL_RXQ0_CTRL 0x0d3c
  186. #define REG_DWCEQOS_MTL_Q0_ISCTRL 0x0d2c
  187. #define DWCEQOS_MTL_SCHALG_STRICT 0x00000060
  188. #define DWCEQOS_MTL_TXQ_TXQEN BIT(3)
  189. #define DWCEQOS_MTL_TXQ_TSF BIT(1)
  190. #define DWCEQOS_MTL_TXQ_FTQ BIT(0)
  191. #define DWCEQOS_MTL_TXQ_TTC512 0x00000070
  192. #define DWCEQOS_MTL_TXQ_SIZE(x) ((((x) - 256) & 0xff00) << 8)
  193. #define DWCEQOS_MTL_RXQ_SIZE(x) ((((x) - 256) & 0xff00) << 12)
  194. #define DWCEQOS_MTL_RXQ_EHFC BIT(7)
  195. #define DWCEQOS_MTL_RXQ_DIS_TCP_EF BIT(6)
  196. #define DWCEQOS_MTL_RXQ_FEP BIT(4)
  197. #define DWCEQOS_MTL_RXQ_FUP BIT(3)
  198. #define DWCEQOS_MTL_RXQ_RSF BIT(5)
  199. #define DWCEQOS_MTL_RXQ_RTC32 BIT(0)
  200. /* MAC */
  201. #define REG_DWCEQOS_MAC_CFG 0x0000
  202. #define REG_DWCEQOS_MAC_EXT_CFG 0x0004
  203. #define REG_DWCEQOS_MAC_PKT_FILT 0x0008
  204. #define REG_DWCEQOS_MAC_WD_TO 0x000c
  205. #define REG_DWCEQOS_HASTABLE_LO 0x0010
  206. #define REG_DWCEQOS_HASTABLE_HI 0x0014
  207. #define REG_DWCEQOS_MAC_IS 0x00b0
  208. #define REG_DWCEQOS_MAC_IE 0x00b4
  209. #define REG_DWCEQOS_MAC_STAT 0x00b8
  210. #define REG_DWCEQOS_MAC_MDIO_ADDR 0x0200
  211. #define REG_DWCEQOS_MAC_MDIO_DATA 0x0204
  212. #define REG_DWCEQOS_MAC_MAC_ADDR0_HI 0x0300
  213. #define REG_DWCEQOS_MAC_MAC_ADDR0_LO 0x0304
  214. #define REG_DWCEQOS_MAC_RXQ0_CTRL0 0x00a0
  215. #define REG_DWCEQOS_MAC_HW_FEATURE0 0x011c
  216. #define REG_DWCEQOS_MAC_HW_FEATURE1 0x0120
  217. #define REG_DWCEQOS_MAC_HW_FEATURE2 0x0124
  218. #define REG_DWCEQOS_MAC_HASHTABLE_LO 0x0010
  219. #define REG_DWCEQOS_MAC_HASHTABLE_HI 0x0014
  220. #define REG_DWCEQOS_MAC_LPI_CTRL_STATUS 0x00d0
  221. #define REG_DWCEQOS_MAC_LPI_TIMERS_CTRL 0x00d4
  222. #define REG_DWCEQOS_MAC_LPI_ENTRY_TIMER 0x00d8
  223. #define REG_DWCEQOS_MAC_1US_TIC_COUNTER 0x00dc
  224. #define REG_DWCEQOS_MAC_RX_FLOW_CTRL 0x0090
  225. #define REG_DWCEQOS_MAC_Q0_TX_FLOW 0x0070
  226. #define DWCEQOS_MAC_CFG_ACS BIT(20)
  227. #define DWCEQOS_MAC_CFG_JD BIT(17)
  228. #define DWCEQOS_MAC_CFG_JE BIT(16)
  229. #define DWCEQOS_MAC_CFG_PS BIT(15)
  230. #define DWCEQOS_MAC_CFG_FES BIT(14)
  231. #define DWCEQOS_MAC_CFG_DM BIT(13)
  232. #define DWCEQOS_MAC_CFG_DO BIT(10)
  233. #define DWCEQOS_MAC_CFG_TE BIT(1)
  234. #define DWCEQOS_MAC_CFG_IPC BIT(27)
  235. #define DWCEQOS_MAC_CFG_RE BIT(0)
  236. #define DWCEQOS_ADDR_HIGH(reg) (0x00000300 + (reg * 8))
  237. #define DWCEQOS_ADDR_LOW(reg) (0x00000304 + (reg * 8))
  238. #define DWCEQOS_MAC_IS_LPI_INT BIT(5)
  239. #define DWCEQOS_MAC_IS_MMC_INT BIT(8)
  240. #define DWCEQOS_MAC_RXQ_EN BIT(1)
  241. #define DWCEQOS_MAC_MAC_ADDR_HI_EN BIT(31)
  242. #define DWCEQOS_MAC_PKT_FILT_RA BIT(31)
  243. #define DWCEQOS_MAC_PKT_FILT_HPF BIT(10)
  244. #define DWCEQOS_MAC_PKT_FILT_SAF BIT(9)
  245. #define DWCEQOS_MAC_PKT_FILT_SAIF BIT(8)
  246. #define DWCEQOS_MAC_PKT_FILT_DBF BIT(5)
  247. #define DWCEQOS_MAC_PKT_FILT_PM BIT(4)
  248. #define DWCEQOS_MAC_PKT_FILT_DAIF BIT(3)
  249. #define DWCEQOS_MAC_PKT_FILT_HMC BIT(2)
  250. #define DWCEQOS_MAC_PKT_FILT_HUC BIT(1)
  251. #define DWCEQOS_MAC_PKT_FILT_PR BIT(0)
  252. #define DWCEQOS_MAC_MDIO_ADDR_CR(x) (((x & 15)) << 8)
  253. #define DWCEQOS_MAC_MDIO_ADDR_CR_20 2
  254. #define DWCEQOS_MAC_MDIO_ADDR_CR_35 3
  255. #define DWCEQOS_MAC_MDIO_ADDR_CR_60 0
  256. #define DWCEQOS_MAC_MDIO_ADDR_CR_100 1
  257. #define DWCEQOS_MAC_MDIO_ADDR_CR_150 4
  258. #define DWCEQOS_MAC_MDIO_ADDR_CR_250 5
  259. #define DWCEQOS_MAC_MDIO_ADDR_GOC_READ 0x0000000c
  260. #define DWCEQOS_MAC_MDIO_ADDR_GOC_WRITE BIT(2)
  261. #define DWCEQOS_MAC_MDIO_ADDR_GB BIT(0)
  262. #define DWCEQOS_MAC_LPI_CTRL_STATUS_TLPIEN BIT(0)
  263. #define DWCEQOS_MAC_LPI_CTRL_STATUS_TLPIEX BIT(1)
  264. #define DWCEQOS_MAC_LPI_CTRL_STATUS_RLPIEN BIT(2)
  265. #define DWCEQOS_MAC_LPI_CTRL_STATUS_RLPIEX BIT(3)
  266. #define DWCEQOS_MAC_LPI_CTRL_STATUS_TLPIST BIT(8)
  267. #define DWCEQOS_MAC_LPI_CTRL_STATUS_RLPIST BIT(9)
  268. #define DWCEQOS_MAC_LPI_CTRL_STATUS_LPIEN BIT(16)
  269. #define DWCEQOS_MAC_LPI_CTRL_STATUS_PLS BIT(17)
  270. #define DWCEQOS_MAC_LPI_CTRL_STATUS_PLSEN BIT(18)
  271. #define DWCEQOS_MAC_LPI_CTRL_STATUS_LIPTXA BIT(19)
  272. #define DWCEQOS_MAC_LPI_CTRL_STATUS_LPITE BIT(20)
  273. #define DWCEQOS_MAC_LPI_CTRL_STATUS_LPITCSE BIT(21)
  274. #define DWCEQOS_MAC_1US_TIC_COUNTER_VAL(x) ((x) & GENMASK(11, 0))
  275. #define DWCEQOS_LPI_CTRL_ENABLE_EEE (DWCEQOS_MAC_LPI_CTRL_STATUS_LPITE | \
  276. DWCEQOS_MAC_LPI_CTRL_STATUS_LIPTXA | \
  277. DWCEQOS_MAC_LPI_CTRL_STATUS_LPIEN)
  278. #define DWCEQOS_MAC_RX_FLOW_CTRL_RFE BIT(0)
  279. #define DWCEQOS_MAC_Q0_TX_FLOW_TFE BIT(1)
  280. #define DWCEQOS_MAC_Q0_TX_FLOW_PT(time) ((time) << 16)
  281. #define DWCEQOS_MAC_Q0_TX_FLOW_PLT_4_SLOTS (0 << 4)
  282. /* Features */
  283. #define DWCEQOS_MAC_HW_FEATURE0_RXCOESEL BIT(16)
  284. #define DWCEQOS_MAC_HW_FEATURE0_TXCOESEL BIT(14)
  285. #define DWCEQOS_MAC_HW_FEATURE0_HDSEL BIT(2)
  286. #define DWCEQOS_MAC_HW_FEATURE0_EEESEL BIT(13)
  287. #define DWCEQOS_MAC_HW_FEATURE0_GMIISEL BIT(1)
  288. #define DWCEQOS_MAC_HW_FEATURE0_MIISEL BIT(0)
  289. #define DWCEQOS_MAC_HW_FEATURE1_TSOEN BIT(18)
  290. #define DWCEQOS_MAC_HW_FEATURE1_TXFIFOSIZE(x) ((128 << ((x) & 0x7c0)) >> 6)
  291. #define DWCEQOS_MAC_HW_FEATURE1_RXFIFOSIZE(x) (128 << ((x) & 0x1f))
  292. #define DWCEQOS_MAX_PERFECT_ADDRESSES(feature1) \
  293. (1 + (((feature1) & 0x1fc0000) >> 18))
  294. #define DWCEQOS_MDIO_PHYADDR(x) (((x) & 0x1f) << 21)
  295. #define DWCEQOS_MDIO_PHYREG(x) (((x) & 0x1f) << 16)
  296. #define DWCEQOS_DMA_MODE_SWR BIT(0)
  297. #define DWCEQOS_DWCEQOS_RX_BUF_SIZE 2048
  298. /* Mac Management Counters */
  299. #define REG_DWCEQOS_MMC_CTRL 0x0700
  300. #define REG_DWCEQOS_MMC_RXIRQ 0x0704
  301. #define REG_DWCEQOS_MMC_TXIRQ 0x0708
  302. #define REG_DWCEQOS_MMC_RXIRQMASK 0x070c
  303. #define REG_DWCEQOS_MMC_TXIRQMASK 0x0710
  304. #define DWCEQOS_MMC_CTRL_CNTRST BIT(0)
  305. #define DWCEQOS_MMC_CTRL_RSTONRD BIT(2)
  306. #define DWC_MMC_TXLPITRANSCNTR 0x07F0
  307. #define DWC_MMC_TXLPIUSCNTR 0x07EC
  308. #define DWC_MMC_TXOVERSIZE_G 0x0778
  309. #define DWC_MMC_TXVLANPACKETS_G 0x0774
  310. #define DWC_MMC_TXPAUSEPACKETS 0x0770
  311. #define DWC_MMC_TXEXCESSDEF 0x076C
  312. #define DWC_MMC_TXPACKETCOUNT_G 0x0768
  313. #define DWC_MMC_TXOCTETCOUNT_G 0x0764
  314. #define DWC_MMC_TXCARRIERERROR 0x0760
  315. #define DWC_MMC_TXEXCESSCOL 0x075C
  316. #define DWC_MMC_TXLATECOL 0x0758
  317. #define DWC_MMC_TXDEFERRED 0x0754
  318. #define DWC_MMC_TXMULTICOL_G 0x0750
  319. #define DWC_MMC_TXSINGLECOL_G 0x074C
  320. #define DWC_MMC_TXUNDERFLOWERROR 0x0748
  321. #define DWC_MMC_TXBROADCASTPACKETS_GB 0x0744
  322. #define DWC_MMC_TXMULTICASTPACKETS_GB 0x0740
  323. #define DWC_MMC_TXUNICASTPACKETS_GB 0x073C
  324. #define DWC_MMC_TX1024TOMAXOCTETS_GB 0x0738
  325. #define DWC_MMC_TX512TO1023OCTETS_GB 0x0734
  326. #define DWC_MMC_TX256TO511OCTETS_GB 0x0730
  327. #define DWC_MMC_TX128TO255OCTETS_GB 0x072C
  328. #define DWC_MMC_TX65TO127OCTETS_GB 0x0728
  329. #define DWC_MMC_TX64OCTETS_GB 0x0724
  330. #define DWC_MMC_TXMULTICASTPACKETS_G 0x0720
  331. #define DWC_MMC_TXBROADCASTPACKETS_G 0x071C
  332. #define DWC_MMC_TXPACKETCOUNT_GB 0x0718
  333. #define DWC_MMC_TXOCTETCOUNT_GB 0x0714
  334. #define DWC_MMC_RXLPITRANSCNTR 0x07F8
  335. #define DWC_MMC_RXLPIUSCNTR 0x07F4
  336. #define DWC_MMC_RXCTRLPACKETS_G 0x07E4
  337. #define DWC_MMC_RXRCVERROR 0x07E0
  338. #define DWC_MMC_RXWATCHDOG 0x07DC
  339. #define DWC_MMC_RXVLANPACKETS_GB 0x07D8
  340. #define DWC_MMC_RXFIFOOVERFLOW 0x07D4
  341. #define DWC_MMC_RXPAUSEPACKETS 0x07D0
  342. #define DWC_MMC_RXOUTOFRANGETYPE 0x07CC
  343. #define DWC_MMC_RXLENGTHERROR 0x07C8
  344. #define DWC_MMC_RXUNICASTPACKETS_G 0x07C4
  345. #define DWC_MMC_RX1024TOMAXOCTETS_GB 0x07C0
  346. #define DWC_MMC_RX512TO1023OCTETS_GB 0x07BC
  347. #define DWC_MMC_RX256TO511OCTETS_GB 0x07B8
  348. #define DWC_MMC_RX128TO255OCTETS_GB 0x07B4
  349. #define DWC_MMC_RX65TO127OCTETS_GB 0x07B0
  350. #define DWC_MMC_RX64OCTETS_GB 0x07AC
  351. #define DWC_MMC_RXOVERSIZE_G 0x07A8
  352. #define DWC_MMC_RXUNDERSIZE_G 0x07A4
  353. #define DWC_MMC_RXJABBERERROR 0x07A0
  354. #define DWC_MMC_RXRUNTERROR 0x079C
  355. #define DWC_MMC_RXALIGNMENTERROR 0x0798
  356. #define DWC_MMC_RXCRCERROR 0x0794
  357. #define DWC_MMC_RXMULTICASTPACKETS_G 0x0790
  358. #define DWC_MMC_RXBROADCASTPACKETS_G 0x078C
  359. #define DWC_MMC_RXOCTETCOUNT_G 0x0788
  360. #define DWC_MMC_RXOCTETCOUNT_GB 0x0784
  361. #define DWC_MMC_RXPACKETCOUNT_GB 0x0780
  362. static int debug = -1;
  363. module_param(debug, int, 0);
  364. MODULE_PARM_DESC(debug, "DWC_eth_qos debug level (0=none,...,16=all)");
  365. /* DMA ring descriptor. These are used as support descriptors for the HW DMA */
  366. struct ring_desc {
  367. struct sk_buff *skb;
  368. dma_addr_t mapping;
  369. size_t len;
  370. };
  371. /* DMA hardware descriptor */
  372. struct dwceqos_dma_desc {
  373. u32 des0;
  374. u32 des1;
  375. u32 des2;
  376. u32 des3;
  377. } ____cacheline_aligned;
  378. struct dwceqos_mmc_counters {
  379. __u64 txlpitranscntr;
  380. __u64 txpiuscntr;
  381. __u64 txoversize_g;
  382. __u64 txvlanpackets_g;
  383. __u64 txpausepackets;
  384. __u64 txexcessdef;
  385. __u64 txpacketcount_g;
  386. __u64 txoctetcount_g;
  387. __u64 txcarriererror;
  388. __u64 txexcesscol;
  389. __u64 txlatecol;
  390. __u64 txdeferred;
  391. __u64 txmulticol_g;
  392. __u64 txsinglecol_g;
  393. __u64 txunderflowerror;
  394. __u64 txbroadcastpackets_gb;
  395. __u64 txmulticastpackets_gb;
  396. __u64 txunicastpackets_gb;
  397. __u64 tx1024tomaxoctets_gb;
  398. __u64 tx512to1023octets_gb;
  399. __u64 tx256to511octets_gb;
  400. __u64 tx128to255octets_gb;
  401. __u64 tx65to127octets_gb;
  402. __u64 tx64octets_gb;
  403. __u64 txmulticastpackets_g;
  404. __u64 txbroadcastpackets_g;
  405. __u64 txpacketcount_gb;
  406. __u64 txoctetcount_gb;
  407. __u64 rxlpitranscntr;
  408. __u64 rxlpiuscntr;
  409. __u64 rxctrlpackets_g;
  410. __u64 rxrcverror;
  411. __u64 rxwatchdog;
  412. __u64 rxvlanpackets_gb;
  413. __u64 rxfifooverflow;
  414. __u64 rxpausepackets;
  415. __u64 rxoutofrangetype;
  416. __u64 rxlengtherror;
  417. __u64 rxunicastpackets_g;
  418. __u64 rx1024tomaxoctets_gb;
  419. __u64 rx512to1023octets_gb;
  420. __u64 rx256to511octets_gb;
  421. __u64 rx128to255octets_gb;
  422. __u64 rx65to127octets_gb;
  423. __u64 rx64octets_gb;
  424. __u64 rxoversize_g;
  425. __u64 rxundersize_g;
  426. __u64 rxjabbererror;
  427. __u64 rxrunterror;
  428. __u64 rxalignmenterror;
  429. __u64 rxcrcerror;
  430. __u64 rxmulticastpackets_g;
  431. __u64 rxbroadcastpackets_g;
  432. __u64 rxoctetcount_g;
  433. __u64 rxoctetcount_gb;
  434. __u64 rxpacketcount_gb;
  435. };
  436. /* Ethtool statistics */
  437. struct dwceqos_stat {
  438. const char stat_name[ETH_GSTRING_LEN];
  439. int offset;
  440. };
  441. #define STAT_ITEM(name, var) \
  442. {\
  443. name,\
  444. offsetof(struct dwceqos_mmc_counters, var),\
  445. }
  446. static const struct dwceqos_stat dwceqos_ethtool_stats[] = {
  447. STAT_ITEM("tx_bytes", txoctetcount_gb),
  448. STAT_ITEM("tx_packets", txpacketcount_gb),
  449. STAT_ITEM("tx_unicst_packets", txunicastpackets_gb),
  450. STAT_ITEM("tx_broadcast_packets", txbroadcastpackets_gb),
  451. STAT_ITEM("tx_multicast_packets", txmulticastpackets_gb),
  452. STAT_ITEM("tx_pause_packets", txpausepackets),
  453. STAT_ITEM("tx_up_to_64_byte_packets", tx64octets_gb),
  454. STAT_ITEM("tx_65_to_127_byte_packets", tx65to127octets_gb),
  455. STAT_ITEM("tx_128_to_255_byte_packets", tx128to255octets_gb),
  456. STAT_ITEM("tx_256_to_511_byte_packets", tx256to511octets_gb),
  457. STAT_ITEM("tx_512_to_1023_byte_packets", tx512to1023octets_gb),
  458. STAT_ITEM("tx_1024_to_maxsize_packets", tx1024tomaxoctets_gb),
  459. STAT_ITEM("tx_underflow_errors", txunderflowerror),
  460. STAT_ITEM("tx_lpi_count", txlpitranscntr),
  461. STAT_ITEM("rx_bytes", rxoctetcount_gb),
  462. STAT_ITEM("rx_packets", rxpacketcount_gb),
  463. STAT_ITEM("rx_unicast_packets", rxunicastpackets_g),
  464. STAT_ITEM("rx_broadcast_packets", rxbroadcastpackets_g),
  465. STAT_ITEM("rx_multicast_packets", rxmulticastpackets_g),
  466. STAT_ITEM("rx_vlan_packets", rxvlanpackets_gb),
  467. STAT_ITEM("rx_pause_packets", rxpausepackets),
  468. STAT_ITEM("rx_up_to_64_byte_packets", rx64octets_gb),
  469. STAT_ITEM("rx_65_to_127_byte_packets", rx65to127octets_gb),
  470. STAT_ITEM("rx_128_to_255_byte_packets", rx128to255octets_gb),
  471. STAT_ITEM("rx_256_to_511_byte_packets", rx256to511octets_gb),
  472. STAT_ITEM("rx_512_to_1023_byte_packets", rx512to1023octets_gb),
  473. STAT_ITEM("rx_1024_to_maxsize_packets", rx1024tomaxoctets_gb),
  474. STAT_ITEM("rx_fifo_overflow_errors", rxfifooverflow),
  475. STAT_ITEM("rx_oversize_packets", rxoversize_g),
  476. STAT_ITEM("rx_undersize_packets", rxundersize_g),
  477. STAT_ITEM("rx_jabbers", rxjabbererror),
  478. STAT_ITEM("rx_align_errors", rxalignmenterror),
  479. STAT_ITEM("rx_crc_errors", rxcrcerror),
  480. STAT_ITEM("rx_lpi_count", rxlpitranscntr),
  481. };
  482. /* Configuration of AXI bus parameters.
  483. * These values depend on the parameters set on the MAC core as well
  484. * as the AXI interconnect.
  485. */
  486. struct dwceqos_bus_cfg {
  487. /* Enable AXI low-power interface. */
  488. bool en_lpi;
  489. /* Limit on number of outstanding AXI write requests. */
  490. u32 write_requests;
  491. /* Limit on number of outstanding AXI read requests. */
  492. u32 read_requests;
  493. /* Bitmap of allowed AXI burst lengths, 4-256 beats. */
  494. u32 burst_map;
  495. /* DMA Programmable burst length*/
  496. u32 tx_pbl;
  497. u32 rx_pbl;
  498. };
  499. struct dwceqos_flowcontrol {
  500. int autoneg;
  501. int rx;
  502. int rx_current;
  503. int tx;
  504. int tx_current;
  505. };
  506. struct net_local {
  507. void __iomem *baseaddr;
  508. struct clk *phy_ref_clk;
  509. struct clk *apb_pclk;
  510. struct device_node *phy_node;
  511. struct net_device *ndev;
  512. struct platform_device *pdev;
  513. u32 msg_enable;
  514. struct tasklet_struct tx_bdreclaim_tasklet;
  515. struct workqueue_struct *txtimeout_handler_wq;
  516. struct work_struct txtimeout_reinit;
  517. phy_interface_t phy_interface;
  518. struct mii_bus *mii_bus;
  519. unsigned int link;
  520. unsigned int speed;
  521. unsigned int duplex;
  522. struct napi_struct napi;
  523. /* DMA Descriptor Areas */
  524. struct ring_desc *rx_skb;
  525. struct ring_desc *tx_skb;
  526. struct dwceqos_dma_desc *tx_descs;
  527. struct dwceqos_dma_desc *rx_descs;
  528. /* DMA Mapped Descriptor areas*/
  529. dma_addr_t tx_descs_addr;
  530. dma_addr_t rx_descs_addr;
  531. dma_addr_t tx_descs_tail_addr;
  532. dma_addr_t rx_descs_tail_addr;
  533. size_t tx_free;
  534. size_t tx_next;
  535. size_t rx_cur;
  536. size_t tx_cur;
  537. /* Spinlocks for accessing DMA Descriptors */
  538. spinlock_t tx_lock;
  539. /* Spinlock for register read-modify-writes. */
  540. spinlock_t hw_lock;
  541. u32 feature0;
  542. u32 feature1;
  543. u32 feature2;
  544. struct dwceqos_bus_cfg bus_cfg;
  545. bool en_tx_lpi_clockgating;
  546. int eee_enabled;
  547. int eee_active;
  548. int csr_val;
  549. u32 gso_size;
  550. struct dwceqos_mmc_counters mmc_counters;
  551. /* Protect the mmc_counter updates. */
  552. spinlock_t stats_lock;
  553. u32 mmc_rx_counters_mask;
  554. u32 mmc_tx_counters_mask;
  555. struct dwceqos_flowcontrol flowcontrol;
  556. /* Tracks the intermediate state of phy started but hardware
  557. * init not finished yet.
  558. */
  559. bool phy_defer;
  560. };
  561. static void dwceqos_read_mmc_counters(struct net_local *lp, u32 rx_mask,
  562. u32 tx_mask);
  563. static void dwceqos_set_umac_addr(struct net_local *lp, unsigned char *addr,
  564. unsigned int reg_n);
  565. static int dwceqos_stop(struct net_device *ndev);
  566. static int dwceqos_open(struct net_device *ndev);
  567. static void dwceqos_tx_poll_demand(struct net_local *lp);
  568. static void dwceqos_set_rx_flowcontrol(struct net_local *lp, bool enable);
  569. static void dwceqos_set_tx_flowcontrol(struct net_local *lp, bool enable);
  570. static void dwceqos_reset_state(struct net_local *lp);
  571. #define dwceqos_read(lp, reg) \
  572. readl_relaxed(((void __iomem *)((lp)->baseaddr)) + (reg))
  573. #define dwceqos_write(lp, reg, val) \
  574. writel_relaxed((val), ((void __iomem *)((lp)->baseaddr)) + (reg))
  575. static void dwceqos_reset_state(struct net_local *lp)
  576. {
  577. lp->link = 0;
  578. lp->speed = 0;
  579. lp->duplex = DUPLEX_UNKNOWN;
  580. lp->flowcontrol.rx_current = 0;
  581. lp->flowcontrol.tx_current = 0;
  582. lp->eee_active = 0;
  583. lp->eee_enabled = 0;
  584. }
  585. static void print_descriptor(struct net_local *lp, int index, int tx)
  586. {
  587. struct dwceqos_dma_desc *dd;
  588. if (tx)
  589. dd = (struct dwceqos_dma_desc *)&lp->tx_descs[index];
  590. else
  591. dd = (struct dwceqos_dma_desc *)&lp->rx_descs[index];
  592. pr_info("%s DMA Descriptor #%d@%p Contents:\n", tx ? "TX" : "RX",
  593. index, dd);
  594. pr_info("0x%08x 0x%08x 0x%08x 0x%08x\n", dd->des0, dd->des1, dd->des2,
  595. dd->des3);
  596. }
  597. static void print_status(struct net_local *lp)
  598. {
  599. size_t desci, i;
  600. pr_info("tx_free %zu, tx_cur %zu, tx_next %zu\n", lp->tx_free,
  601. lp->tx_cur, lp->tx_next);
  602. print_descriptor(lp, lp->rx_cur, 0);
  603. for (desci = (lp->tx_cur - 10) % DWCEQOS_TX_DCNT, i = 0;
  604. i < DWCEQOS_TX_DCNT;
  605. ++i) {
  606. print_descriptor(lp, desci, 1);
  607. desci = (desci + 1) % DWCEQOS_TX_DCNT;
  608. }
  609. pr_info("DMA_Debug_Status0: 0x%08x\n",
  610. dwceqos_read(lp, REG_DWCEQOS_DMA_DEBUG_ST0));
  611. pr_info("DMA_CH0_Status: 0x%08x\n",
  612. dwceqos_read(lp, REG_DWCEQOS_DMA_IS));
  613. pr_info("DMA_CH0_Current_App_TxDesc: 0x%08x\n",
  614. dwceqos_read(lp, 0x1144));
  615. pr_info("DMA_CH0_Current_App_TxBuff: 0x%08x\n",
  616. dwceqos_read(lp, 0x1154));
  617. pr_info("MTL_Debug_Status: 0x%08x\n",
  618. dwceqos_read(lp, REG_DWCEQOS_MTL_DEBUG_ST));
  619. pr_info("MTL_TXQ0_Debug_Status: 0x%08x\n",
  620. dwceqos_read(lp, REG_DWCEQOS_MTL_TXQ0_DEBUG_ST));
  621. pr_info("MTL_RXQ0_Debug_Status: 0x%08x\n",
  622. dwceqos_read(lp, REG_DWCEQOS_MTL_RXQ0_DEBUG_ST));
  623. pr_info("Current TX DMA: 0x%08x, RX DMA: 0x%08x\n",
  624. dwceqos_read(lp, REG_DWCEQOS_DMA_CH0_CUR_TXDESC),
  625. dwceqos_read(lp, REG_DWCEQOS_DMA_CH0_CUR_RXDESC));
  626. }
  627. static void dwceqos_mdio_set_csr(struct net_local *lp)
  628. {
  629. int rate = clk_get_rate(lp->apb_pclk);
  630. if (rate <= 20000000)
  631. lp->csr_val = DWCEQOS_MAC_MDIO_ADDR_CR_20;
  632. else if (rate <= 35000000)
  633. lp->csr_val = DWCEQOS_MAC_MDIO_ADDR_CR_35;
  634. else if (rate <= 60000000)
  635. lp->csr_val = DWCEQOS_MAC_MDIO_ADDR_CR_60;
  636. else if (rate <= 100000000)
  637. lp->csr_val = DWCEQOS_MAC_MDIO_ADDR_CR_100;
  638. else if (rate <= 150000000)
  639. lp->csr_val = DWCEQOS_MAC_MDIO_ADDR_CR_150;
  640. else if (rate <= 250000000)
  641. lp->csr_val = DWCEQOS_MAC_MDIO_ADDR_CR_250;
  642. }
  643. /* Simple MDIO functions implementing mii_bus */
  644. static int dwceqos_mdio_read(struct mii_bus *bus, int mii_id, int phyreg)
  645. {
  646. struct net_local *lp = bus->priv;
  647. u32 regval;
  648. int i;
  649. int data;
  650. regval = DWCEQOS_MDIO_PHYADDR(mii_id) |
  651. DWCEQOS_MDIO_PHYREG(phyreg) |
  652. DWCEQOS_MAC_MDIO_ADDR_CR(lp->csr_val) |
  653. DWCEQOS_MAC_MDIO_ADDR_GB |
  654. DWCEQOS_MAC_MDIO_ADDR_GOC_READ;
  655. dwceqos_write(lp, REG_DWCEQOS_MAC_MDIO_ADDR, regval);
  656. for (i = 0; i < 5; ++i) {
  657. usleep_range(64, 128);
  658. if (!(dwceqos_read(lp, REG_DWCEQOS_MAC_MDIO_ADDR) &
  659. DWCEQOS_MAC_MDIO_ADDR_GB))
  660. break;
  661. }
  662. data = dwceqos_read(lp, REG_DWCEQOS_MAC_MDIO_DATA);
  663. if (i == 5) {
  664. netdev_warn(lp->ndev, "MDIO read timed out\n");
  665. data = 0xffff;
  666. }
  667. return data & 0xffff;
  668. }
  669. static int dwceqos_mdio_write(struct mii_bus *bus, int mii_id, int phyreg,
  670. u16 value)
  671. {
  672. struct net_local *lp = bus->priv;
  673. u32 regval;
  674. int i;
  675. dwceqos_write(lp, REG_DWCEQOS_MAC_MDIO_DATA, value);
  676. regval = DWCEQOS_MDIO_PHYADDR(mii_id) |
  677. DWCEQOS_MDIO_PHYREG(phyreg) |
  678. DWCEQOS_MAC_MDIO_ADDR_CR(lp->csr_val) |
  679. DWCEQOS_MAC_MDIO_ADDR_GB |
  680. DWCEQOS_MAC_MDIO_ADDR_GOC_WRITE;
  681. dwceqos_write(lp, REG_DWCEQOS_MAC_MDIO_ADDR, regval);
  682. for (i = 0; i < 5; ++i) {
  683. usleep_range(64, 128);
  684. if (!(dwceqos_read(lp, REG_DWCEQOS_MAC_MDIO_ADDR) &
  685. DWCEQOS_MAC_MDIO_ADDR_GB))
  686. break;
  687. }
  688. if (i == 5)
  689. netdev_warn(lp->ndev, "MDIO write timed out\n");
  690. return 0;
  691. }
  692. static int dwceqos_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  693. {
  694. struct net_local *lp = netdev_priv(ndev);
  695. struct phy_device *phydev = ndev->phydev;
  696. if (!netif_running(ndev))
  697. return -EINVAL;
  698. if (!phydev)
  699. return -ENODEV;
  700. switch (cmd) {
  701. case SIOCGMIIPHY:
  702. case SIOCGMIIREG:
  703. case SIOCSMIIREG:
  704. return phy_mii_ioctl(phydev, rq, cmd);
  705. default:
  706. dev_info(&lp->pdev->dev, "ioctl %X not implemented.\n", cmd);
  707. return -EOPNOTSUPP;
  708. }
  709. }
  710. static void dwceqos_link_down(struct net_local *lp)
  711. {
  712. u32 regval;
  713. unsigned long flags;
  714. /* Indicate link down to the LPI state machine */
  715. spin_lock_irqsave(&lp->hw_lock, flags);
  716. regval = dwceqos_read(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS);
  717. regval &= ~DWCEQOS_MAC_LPI_CTRL_STATUS_PLS;
  718. dwceqos_write(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS, regval);
  719. spin_unlock_irqrestore(&lp->hw_lock, flags);
  720. }
  721. static void dwceqos_link_up(struct net_local *lp)
  722. {
  723. struct net_device *ndev = lp->ndev;
  724. u32 regval;
  725. unsigned long flags;
  726. /* Indicate link up to the LPI state machine */
  727. spin_lock_irqsave(&lp->hw_lock, flags);
  728. regval = dwceqos_read(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS);
  729. regval |= DWCEQOS_MAC_LPI_CTRL_STATUS_PLS;
  730. dwceqos_write(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS, regval);
  731. spin_unlock_irqrestore(&lp->hw_lock, flags);
  732. lp->eee_active = !phy_init_eee(ndev->phydev, 0);
  733. /* Check for changed EEE capability */
  734. if (!lp->eee_active && lp->eee_enabled) {
  735. lp->eee_enabled = 0;
  736. spin_lock_irqsave(&lp->hw_lock, flags);
  737. regval = dwceqos_read(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS);
  738. regval &= ~DWCEQOS_LPI_CTRL_ENABLE_EEE;
  739. dwceqos_write(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS, regval);
  740. spin_unlock_irqrestore(&lp->hw_lock, flags);
  741. }
  742. }
  743. static void dwceqos_set_speed(struct net_local *lp)
  744. {
  745. struct net_device *ndev = lp->ndev;
  746. struct phy_device *phydev = ndev->phydev;
  747. u32 regval;
  748. regval = dwceqos_read(lp, REG_DWCEQOS_MAC_CFG);
  749. regval &= ~(DWCEQOS_MAC_CFG_PS | DWCEQOS_MAC_CFG_FES |
  750. DWCEQOS_MAC_CFG_DM);
  751. if (phydev->duplex)
  752. regval |= DWCEQOS_MAC_CFG_DM;
  753. if (phydev->speed == SPEED_10) {
  754. regval |= DWCEQOS_MAC_CFG_PS;
  755. } else if (phydev->speed == SPEED_100) {
  756. regval |= DWCEQOS_MAC_CFG_PS |
  757. DWCEQOS_MAC_CFG_FES;
  758. } else if (phydev->speed != SPEED_1000) {
  759. netdev_err(lp->ndev,
  760. "unknown PHY speed %d\n",
  761. phydev->speed);
  762. return;
  763. }
  764. dwceqos_write(lp, REG_DWCEQOS_MAC_CFG, regval);
  765. }
  766. static void dwceqos_adjust_link(struct net_device *ndev)
  767. {
  768. struct net_local *lp = netdev_priv(ndev);
  769. struct phy_device *phydev = ndev->phydev;
  770. int status_change = 0;
  771. if (lp->phy_defer)
  772. return;
  773. if (phydev->link) {
  774. if ((lp->speed != phydev->speed) ||
  775. (lp->duplex != phydev->duplex)) {
  776. dwceqos_set_speed(lp);
  777. lp->speed = phydev->speed;
  778. lp->duplex = phydev->duplex;
  779. status_change = 1;
  780. }
  781. if (lp->flowcontrol.autoneg) {
  782. lp->flowcontrol.rx = phydev->pause ||
  783. phydev->asym_pause;
  784. lp->flowcontrol.tx = phydev->pause ||
  785. phydev->asym_pause;
  786. }
  787. if (lp->flowcontrol.rx != lp->flowcontrol.rx_current) {
  788. if (netif_msg_link(lp))
  789. netdev_dbg(ndev, "set rx flow to %d\n",
  790. lp->flowcontrol.rx);
  791. dwceqos_set_rx_flowcontrol(lp, lp->flowcontrol.rx);
  792. lp->flowcontrol.rx_current = lp->flowcontrol.rx;
  793. }
  794. if (lp->flowcontrol.tx != lp->flowcontrol.tx_current) {
  795. if (netif_msg_link(lp))
  796. netdev_dbg(ndev, "set tx flow to %d\n",
  797. lp->flowcontrol.tx);
  798. dwceqos_set_tx_flowcontrol(lp, lp->flowcontrol.tx);
  799. lp->flowcontrol.tx_current = lp->flowcontrol.tx;
  800. }
  801. }
  802. if (phydev->link != lp->link) {
  803. lp->link = phydev->link;
  804. status_change = 1;
  805. }
  806. if (status_change) {
  807. if (phydev->link) {
  808. netif_trans_update(lp->ndev);
  809. dwceqos_link_up(lp);
  810. } else {
  811. dwceqos_link_down(lp);
  812. }
  813. phy_print_status(phydev);
  814. }
  815. }
  816. static int dwceqos_mii_probe(struct net_device *ndev)
  817. {
  818. struct net_local *lp = netdev_priv(ndev);
  819. struct phy_device *phydev = NULL;
  820. if (lp->phy_node) {
  821. phydev = of_phy_connect(lp->ndev,
  822. lp->phy_node,
  823. &dwceqos_adjust_link,
  824. 0,
  825. lp->phy_interface);
  826. if (!phydev) {
  827. netdev_err(ndev, "no PHY found\n");
  828. return -1;
  829. }
  830. } else {
  831. netdev_err(ndev, "no PHY configured\n");
  832. return -ENODEV;
  833. }
  834. if (netif_msg_probe(lp))
  835. phy_attached_info(phydev);
  836. phydev->supported &= PHY_GBIT_FEATURES | SUPPORTED_Pause |
  837. SUPPORTED_Asym_Pause;
  838. lp->link = 0;
  839. lp->speed = 0;
  840. lp->duplex = DUPLEX_UNKNOWN;
  841. lp->flowcontrol.autoneg = AUTONEG_ENABLE;
  842. return 0;
  843. }
  844. static void dwceqos_alloc_rxring_desc(struct net_local *lp, int index)
  845. {
  846. struct sk_buff *new_skb;
  847. dma_addr_t new_skb_baddr = 0;
  848. new_skb = netdev_alloc_skb(lp->ndev, DWCEQOS_RX_BUF_SIZE);
  849. if (!new_skb) {
  850. netdev_err(lp->ndev, "alloc_skb error for desc %d\n", index);
  851. goto err_out;
  852. }
  853. new_skb_baddr = dma_map_single(lp->ndev->dev.parent,
  854. new_skb->data, DWCEQOS_RX_BUF_SIZE,
  855. DMA_FROM_DEVICE);
  856. if (dma_mapping_error(lp->ndev->dev.parent, new_skb_baddr)) {
  857. netdev_err(lp->ndev, "DMA map error\n");
  858. dev_kfree_skb(new_skb);
  859. new_skb = NULL;
  860. goto err_out;
  861. }
  862. lp->rx_descs[index].des0 = new_skb_baddr;
  863. lp->rx_descs[index].des1 = 0;
  864. lp->rx_descs[index].des2 = 0;
  865. lp->rx_descs[index].des3 = DWCEQOS_DMA_RDES3_INTE |
  866. DWCEQOS_DMA_RDES3_BUF1V |
  867. DWCEQOS_DMA_RDES3_OWN;
  868. lp->rx_skb[index].mapping = new_skb_baddr;
  869. lp->rx_skb[index].len = DWCEQOS_RX_BUF_SIZE;
  870. err_out:
  871. lp->rx_skb[index].skb = new_skb;
  872. }
  873. static void dwceqos_clean_rings(struct net_local *lp)
  874. {
  875. int i;
  876. if (lp->rx_skb) {
  877. for (i = 0; i < DWCEQOS_RX_DCNT; i++) {
  878. if (lp->rx_skb[i].skb) {
  879. dma_unmap_single(lp->ndev->dev.parent,
  880. lp->rx_skb[i].mapping,
  881. lp->rx_skb[i].len,
  882. DMA_FROM_DEVICE);
  883. dev_kfree_skb(lp->rx_skb[i].skb);
  884. lp->rx_skb[i].skb = NULL;
  885. lp->rx_skb[i].mapping = 0;
  886. }
  887. }
  888. }
  889. if (lp->tx_skb) {
  890. for (i = 0; i < DWCEQOS_TX_DCNT; i++) {
  891. if (lp->tx_skb[i].skb) {
  892. dev_kfree_skb(lp->tx_skb[i].skb);
  893. lp->tx_skb[i].skb = NULL;
  894. }
  895. if (lp->tx_skb[i].mapping) {
  896. dma_unmap_single(lp->ndev->dev.parent,
  897. lp->tx_skb[i].mapping,
  898. lp->tx_skb[i].len,
  899. DMA_TO_DEVICE);
  900. lp->tx_skb[i].mapping = 0;
  901. }
  902. }
  903. }
  904. }
  905. static void dwceqos_descriptor_free(struct net_local *lp)
  906. {
  907. int size;
  908. dwceqos_clean_rings(lp);
  909. kfree(lp->tx_skb);
  910. lp->tx_skb = NULL;
  911. kfree(lp->rx_skb);
  912. lp->rx_skb = NULL;
  913. size = DWCEQOS_RX_DCNT * sizeof(struct dwceqos_dma_desc);
  914. if (lp->rx_descs) {
  915. dma_free_coherent(lp->ndev->dev.parent, size,
  916. (void *)(lp->rx_descs), lp->rx_descs_addr);
  917. lp->rx_descs = NULL;
  918. }
  919. size = DWCEQOS_TX_DCNT * sizeof(struct dwceqos_dma_desc);
  920. if (lp->tx_descs) {
  921. dma_free_coherent(lp->ndev->dev.parent, size,
  922. (void *)(lp->tx_descs), lp->tx_descs_addr);
  923. lp->tx_descs = NULL;
  924. }
  925. }
  926. static int dwceqos_descriptor_init(struct net_local *lp)
  927. {
  928. int size;
  929. u32 i;
  930. lp->gso_size = 0;
  931. lp->tx_skb = NULL;
  932. lp->rx_skb = NULL;
  933. lp->rx_descs = NULL;
  934. lp->tx_descs = NULL;
  935. /* Reset the DMA indexes */
  936. lp->rx_cur = 0;
  937. lp->tx_cur = 0;
  938. lp->tx_next = 0;
  939. lp->tx_free = DWCEQOS_TX_DCNT;
  940. /* Allocate Ring descriptors */
  941. size = DWCEQOS_RX_DCNT * sizeof(struct ring_desc);
  942. lp->rx_skb = kzalloc(size, GFP_KERNEL);
  943. if (!lp->rx_skb)
  944. goto err_out;
  945. size = DWCEQOS_TX_DCNT * sizeof(struct ring_desc);
  946. lp->tx_skb = kzalloc(size, GFP_KERNEL);
  947. if (!lp->tx_skb)
  948. goto err_out;
  949. /* Allocate DMA descriptors */
  950. size = DWCEQOS_RX_DCNT * sizeof(struct dwceqos_dma_desc);
  951. lp->rx_descs = dma_alloc_coherent(lp->ndev->dev.parent, size,
  952. &lp->rx_descs_addr, GFP_KERNEL);
  953. if (!lp->rx_descs)
  954. goto err_out;
  955. lp->rx_descs_tail_addr = lp->rx_descs_addr +
  956. sizeof(struct dwceqos_dma_desc) * DWCEQOS_RX_DCNT;
  957. size = DWCEQOS_TX_DCNT * sizeof(struct dwceqos_dma_desc);
  958. lp->tx_descs = dma_alloc_coherent(lp->ndev->dev.parent, size,
  959. &lp->tx_descs_addr, GFP_KERNEL);
  960. if (!lp->tx_descs)
  961. goto err_out;
  962. lp->tx_descs_tail_addr = lp->tx_descs_addr +
  963. sizeof(struct dwceqos_dma_desc) * DWCEQOS_TX_DCNT;
  964. /* Initialize RX Ring Descriptors and buffers */
  965. for (i = 0; i < DWCEQOS_RX_DCNT; ++i) {
  966. dwceqos_alloc_rxring_desc(lp, i);
  967. if (!(lp->rx_skb[lp->rx_cur].skb))
  968. goto err_out;
  969. }
  970. /* Initialize TX Descriptors */
  971. for (i = 0; i < DWCEQOS_TX_DCNT; ++i) {
  972. lp->tx_descs[i].des0 = 0;
  973. lp->tx_descs[i].des1 = 0;
  974. lp->tx_descs[i].des2 = 0;
  975. lp->tx_descs[i].des3 = 0;
  976. }
  977. /* Make descriptor writes visible to the DMA. */
  978. wmb();
  979. return 0;
  980. err_out:
  981. dwceqos_descriptor_free(lp);
  982. return -ENOMEM;
  983. }
  984. static int dwceqos_packet_avail(struct net_local *lp)
  985. {
  986. return !(lp->rx_descs[lp->rx_cur].des3 & DWCEQOS_DMA_RDES3_OWN);
  987. }
  988. static void dwceqos_get_hwfeatures(struct net_local *lp)
  989. {
  990. lp->feature0 = dwceqos_read(lp, REG_DWCEQOS_MAC_HW_FEATURE0);
  991. lp->feature1 = dwceqos_read(lp, REG_DWCEQOS_MAC_HW_FEATURE1);
  992. lp->feature2 = dwceqos_read(lp, REG_DWCEQOS_MAC_HW_FEATURE2);
  993. }
  994. static void dwceqos_dma_enable_txirq(struct net_local *lp)
  995. {
  996. u32 regval;
  997. unsigned long flags;
  998. spin_lock_irqsave(&lp->hw_lock, flags);
  999. regval = dwceqos_read(lp, REG_DWCEQOS_DMA_CH0_IE);
  1000. regval |= DWCEQOS_DMA_CH0_IE_TIE;
  1001. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_IE, regval);
  1002. spin_unlock_irqrestore(&lp->hw_lock, flags);
  1003. }
  1004. static void dwceqos_dma_disable_txirq(struct net_local *lp)
  1005. {
  1006. u32 regval;
  1007. unsigned long flags;
  1008. spin_lock_irqsave(&lp->hw_lock, flags);
  1009. regval = dwceqos_read(lp, REG_DWCEQOS_DMA_CH0_IE);
  1010. regval &= ~DWCEQOS_DMA_CH0_IE_TIE;
  1011. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_IE, regval);
  1012. spin_unlock_irqrestore(&lp->hw_lock, flags);
  1013. }
  1014. static void dwceqos_dma_enable_rxirq(struct net_local *lp)
  1015. {
  1016. u32 regval;
  1017. unsigned long flags;
  1018. spin_lock_irqsave(&lp->hw_lock, flags);
  1019. regval = dwceqos_read(lp, REG_DWCEQOS_DMA_CH0_IE);
  1020. regval |= DWCEQOS_DMA_CH0_IE_RIE;
  1021. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_IE, regval);
  1022. spin_unlock_irqrestore(&lp->hw_lock, flags);
  1023. }
  1024. static void dwceqos_dma_disable_rxirq(struct net_local *lp)
  1025. {
  1026. u32 regval;
  1027. unsigned long flags;
  1028. spin_lock_irqsave(&lp->hw_lock, flags);
  1029. regval = dwceqos_read(lp, REG_DWCEQOS_DMA_CH0_IE);
  1030. regval &= ~DWCEQOS_DMA_CH0_IE_RIE;
  1031. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_IE, regval);
  1032. spin_unlock_irqrestore(&lp->hw_lock, flags);
  1033. }
  1034. static void dwceqos_enable_mmc_interrupt(struct net_local *lp)
  1035. {
  1036. dwceqos_write(lp, REG_DWCEQOS_MMC_RXIRQMASK, 0);
  1037. dwceqos_write(lp, REG_DWCEQOS_MMC_TXIRQMASK, 0);
  1038. }
  1039. static int dwceqos_mii_init(struct net_local *lp)
  1040. {
  1041. int ret = -ENXIO;
  1042. struct resource res;
  1043. struct device_node *mdionode;
  1044. mdionode = of_get_child_by_name(lp->pdev->dev.of_node, "mdio");
  1045. if (!mdionode)
  1046. return 0;
  1047. lp->mii_bus = mdiobus_alloc();
  1048. if (!lp->mii_bus) {
  1049. ret = -ENOMEM;
  1050. goto err_out;
  1051. }
  1052. lp->mii_bus->name = "DWCEQOS MII bus";
  1053. lp->mii_bus->read = &dwceqos_mdio_read;
  1054. lp->mii_bus->write = &dwceqos_mdio_write;
  1055. lp->mii_bus->priv = lp;
  1056. lp->mii_bus->parent = &lp->pdev->dev;
  1057. of_address_to_resource(lp->pdev->dev.of_node, 0, &res);
  1058. snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%.8llx",
  1059. (unsigned long long)res.start);
  1060. if (of_mdiobus_register(lp->mii_bus, mdionode))
  1061. goto err_out_free_mdiobus;
  1062. return 0;
  1063. err_out_free_mdiobus:
  1064. mdiobus_free(lp->mii_bus);
  1065. err_out:
  1066. of_node_put(mdionode);
  1067. return ret;
  1068. }
  1069. /* DMA reset. When issued also resets all MTL and MAC registers as well */
  1070. static void dwceqos_reset_hw(struct net_local *lp)
  1071. {
  1072. /* Wait (at most) 0.5 seconds for DMA reset*/
  1073. int i = 5000;
  1074. u32 reg;
  1075. /* Force gigabit to guarantee a TX clock for GMII. */
  1076. reg = dwceqos_read(lp, REG_DWCEQOS_MAC_CFG);
  1077. reg &= ~(DWCEQOS_MAC_CFG_PS | DWCEQOS_MAC_CFG_FES);
  1078. reg |= DWCEQOS_MAC_CFG_DM;
  1079. dwceqos_write(lp, REG_DWCEQOS_MAC_CFG, reg);
  1080. dwceqos_write(lp, REG_DWCEQOS_DMA_MODE, DWCEQOS_DMA_MODE_SWR);
  1081. do {
  1082. udelay(100);
  1083. i--;
  1084. reg = dwceqos_read(lp, REG_DWCEQOS_DMA_MODE);
  1085. } while ((reg & DWCEQOS_DMA_MODE_SWR) && i);
  1086. /* We might experience a timeout if the chip clock mux is broken */
  1087. if (!i)
  1088. netdev_err(lp->ndev, "DMA reset timed out!\n");
  1089. }
  1090. static void dwceqos_fatal_bus_error(struct net_local *lp, u32 dma_status)
  1091. {
  1092. if (dma_status & DWCEQOS_DMA_CH0_IS_TEB) {
  1093. netdev_err(lp->ndev, "txdma bus error %s %s (status=%08x)\n",
  1094. dma_status & DWCEQOS_DMA_CH0_IS_TX_ERR_READ ?
  1095. "read" : "write",
  1096. dma_status & DWCEQOS_DMA_CH0_IS_TX_ERR_DESCR ?
  1097. "descr" : "data",
  1098. dma_status);
  1099. print_status(lp);
  1100. }
  1101. if (dma_status & DWCEQOS_DMA_CH0_IS_REB) {
  1102. netdev_err(lp->ndev, "rxdma bus error %s %s (status=%08x)\n",
  1103. dma_status & DWCEQOS_DMA_CH0_IS_RX_ERR_READ ?
  1104. "read" : "write",
  1105. dma_status & DWCEQOS_DMA_CH0_IS_RX_ERR_DESCR ?
  1106. "descr" : "data",
  1107. dma_status);
  1108. print_status(lp);
  1109. }
  1110. }
  1111. static void dwceqos_mmc_interrupt(struct net_local *lp)
  1112. {
  1113. unsigned long flags;
  1114. spin_lock_irqsave(&lp->stats_lock, flags);
  1115. /* A latched mmc interrupt can not be masked, we must read
  1116. * all the counters with an interrupt pending.
  1117. */
  1118. dwceqos_read_mmc_counters(lp,
  1119. dwceqos_read(lp, REG_DWCEQOS_MMC_RXIRQ),
  1120. dwceqos_read(lp, REG_DWCEQOS_MMC_TXIRQ));
  1121. spin_unlock_irqrestore(&lp->stats_lock, flags);
  1122. }
  1123. static void dwceqos_mac_interrupt(struct net_local *lp)
  1124. {
  1125. u32 cause;
  1126. cause = dwceqos_read(lp, REG_DWCEQOS_MAC_IS);
  1127. if (cause & DWCEQOS_MAC_IS_MMC_INT)
  1128. dwceqos_mmc_interrupt(lp);
  1129. }
  1130. static irqreturn_t dwceqos_interrupt(int irq, void *dev_id)
  1131. {
  1132. struct net_device *ndev = dev_id;
  1133. struct net_local *lp = netdev_priv(ndev);
  1134. u32 cause;
  1135. u32 dma_status;
  1136. irqreturn_t ret = IRQ_NONE;
  1137. cause = dwceqos_read(lp, REG_DWCEQOS_DMA_IS);
  1138. /* DMA Channel 0 Interrupt */
  1139. if (cause & DWCEQOS_DMA_IS_DC0IS) {
  1140. dma_status = dwceqos_read(lp, REG_DWCEQOS_DMA_CH0_STA);
  1141. /* Transmit Interrupt */
  1142. if (dma_status & DWCEQOS_DMA_CH0_IS_TI) {
  1143. tasklet_schedule(&lp->tx_bdreclaim_tasklet);
  1144. dwceqos_dma_disable_txirq(lp);
  1145. }
  1146. /* Receive Interrupt */
  1147. if (dma_status & DWCEQOS_DMA_CH0_IS_RI) {
  1148. /* Disable RX IRQs */
  1149. dwceqos_dma_disable_rxirq(lp);
  1150. napi_schedule(&lp->napi);
  1151. }
  1152. /* Fatal Bus Error interrupt */
  1153. if (unlikely(dma_status & DWCEQOS_DMA_CH0_IS_FBE)) {
  1154. dwceqos_fatal_bus_error(lp, dma_status);
  1155. /* errata 9000831707 */
  1156. dma_status |= DWCEQOS_DMA_CH0_IS_TEB |
  1157. DWCEQOS_DMA_CH0_IS_REB;
  1158. }
  1159. /* Ack all DMA Channel 0 IRQs */
  1160. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_STA, dma_status);
  1161. ret = IRQ_HANDLED;
  1162. }
  1163. if (cause & DWCEQOS_DMA_IS_MTLIS) {
  1164. u32 val = dwceqos_read(lp, REG_DWCEQOS_MTL_Q0_ISCTRL);
  1165. dwceqos_write(lp, REG_DWCEQOS_MTL_Q0_ISCTRL, val);
  1166. ret = IRQ_HANDLED;
  1167. }
  1168. if (cause & DWCEQOS_DMA_IS_MACIS) {
  1169. dwceqos_mac_interrupt(lp);
  1170. ret = IRQ_HANDLED;
  1171. }
  1172. return ret;
  1173. }
  1174. static void dwceqos_set_rx_flowcontrol(struct net_local *lp, bool enable)
  1175. {
  1176. u32 regval;
  1177. unsigned long flags;
  1178. spin_lock_irqsave(&lp->hw_lock, flags);
  1179. regval = dwceqos_read(lp, REG_DWCEQOS_MAC_RX_FLOW_CTRL);
  1180. if (enable)
  1181. regval |= DWCEQOS_MAC_RX_FLOW_CTRL_RFE;
  1182. else
  1183. regval &= ~DWCEQOS_MAC_RX_FLOW_CTRL_RFE;
  1184. dwceqos_write(lp, REG_DWCEQOS_MAC_RX_FLOW_CTRL, regval);
  1185. spin_unlock_irqrestore(&lp->hw_lock, flags);
  1186. }
  1187. static void dwceqos_set_tx_flowcontrol(struct net_local *lp, bool enable)
  1188. {
  1189. u32 regval;
  1190. unsigned long flags;
  1191. spin_lock_irqsave(&lp->hw_lock, flags);
  1192. /* MTL flow control */
  1193. regval = dwceqos_read(lp, REG_DWCEQOS_MTL_RXQ0_OPER);
  1194. if (enable)
  1195. regval |= DWCEQOS_MTL_RXQ_EHFC;
  1196. else
  1197. regval &= ~DWCEQOS_MTL_RXQ_EHFC;
  1198. dwceqos_write(lp, REG_DWCEQOS_MTL_RXQ0_OPER, regval);
  1199. /* MAC flow control */
  1200. regval = dwceqos_read(lp, REG_DWCEQOS_MAC_Q0_TX_FLOW);
  1201. if (enable)
  1202. regval |= DWCEQOS_MAC_Q0_TX_FLOW_TFE;
  1203. else
  1204. regval &= ~DWCEQOS_MAC_Q0_TX_FLOW_TFE;
  1205. dwceqos_write(lp, REG_DWCEQOS_MAC_Q0_TX_FLOW, regval);
  1206. spin_unlock_irqrestore(&lp->hw_lock, flags);
  1207. }
  1208. static void dwceqos_configure_flow_control(struct net_local *lp)
  1209. {
  1210. u32 regval;
  1211. unsigned long flags;
  1212. int RQS, RFD, RFA;
  1213. spin_lock_irqsave(&lp->hw_lock, flags);
  1214. regval = dwceqos_read(lp, REG_DWCEQOS_MTL_RXQ0_OPER);
  1215. /* The queue size is in units of 256 bytes. We want 512 bytes units for
  1216. * the threshold fields.
  1217. */
  1218. RQS = ((regval >> 20) & 0x3FF) + 1;
  1219. RQS /= 2;
  1220. /* The thresholds are relative to a full queue, with a bias
  1221. * of 1 KiByte below full.
  1222. */
  1223. RFD = RQS / 2 - 2;
  1224. RFA = RQS / 8 - 2;
  1225. regval = (regval & 0xFFF000FF) | (RFD << 14) | (RFA << 8);
  1226. if (RFD >= 0 && RFA >= 0) {
  1227. dwceqos_write(lp, REG_DWCEQOS_MTL_RXQ0_OPER, regval);
  1228. } else {
  1229. netdev_warn(lp->ndev,
  1230. "FIFO too small for flow control.");
  1231. }
  1232. regval = DWCEQOS_MAC_Q0_TX_FLOW_PT(256) |
  1233. DWCEQOS_MAC_Q0_TX_FLOW_PLT_4_SLOTS;
  1234. dwceqos_write(lp, REG_DWCEQOS_MAC_Q0_TX_FLOW, regval);
  1235. spin_unlock_irqrestore(&lp->hw_lock, flags);
  1236. }
  1237. static void dwceqos_configure_clock(struct net_local *lp)
  1238. {
  1239. unsigned long rate_mhz = clk_get_rate(lp->apb_pclk) / 1000000;
  1240. BUG_ON(!rate_mhz);
  1241. dwceqos_write(lp,
  1242. REG_DWCEQOS_MAC_1US_TIC_COUNTER,
  1243. DWCEQOS_MAC_1US_TIC_COUNTER_VAL(rate_mhz - 1));
  1244. }
  1245. static void dwceqos_configure_bus(struct net_local *lp)
  1246. {
  1247. u32 sysbus_reg;
  1248. /* N.B. We do not support the Fixed Burst mode because it
  1249. * opens a race window by making HW access to DMA descriptors
  1250. * non-atomic.
  1251. */
  1252. sysbus_reg = DWCEQOS_DMA_SYSBUS_MODE_AAL;
  1253. if (lp->bus_cfg.en_lpi)
  1254. sysbus_reg |= DWCEQOS_DMA_SYSBUS_MODE_EN_LPI;
  1255. if (lp->bus_cfg.burst_map)
  1256. sysbus_reg |= DWCEQOS_DMA_SYSBUS_MODE_BURST(
  1257. lp->bus_cfg.burst_map);
  1258. else
  1259. sysbus_reg |= DWCEQOS_DMA_SYSBUS_MODE_BURST(
  1260. DWCEQOS_DMA_SYSBUS_MODE_BURST_DEFAULT);
  1261. if (lp->bus_cfg.read_requests)
  1262. sysbus_reg |= DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT(
  1263. lp->bus_cfg.read_requests - 1);
  1264. else
  1265. sysbus_reg |= DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT(
  1266. DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT_DEFAULT);
  1267. if (lp->bus_cfg.write_requests)
  1268. sysbus_reg |= DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT(
  1269. lp->bus_cfg.write_requests - 1);
  1270. else
  1271. sysbus_reg |= DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT(
  1272. DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT_DEFAULT);
  1273. if (netif_msg_hw(lp))
  1274. netdev_dbg(lp->ndev, "SysbusMode %#X\n", sysbus_reg);
  1275. dwceqos_write(lp, REG_DWCEQOS_DMA_SYSBUS_MODE, sysbus_reg);
  1276. }
  1277. static void dwceqos_init_hw(struct net_local *lp)
  1278. {
  1279. struct net_device *ndev = lp->ndev;
  1280. u32 regval;
  1281. u32 buswidth;
  1282. u32 dma_skip;
  1283. /* Software reset */
  1284. dwceqos_reset_hw(lp);
  1285. dwceqos_configure_bus(lp);
  1286. /* Probe data bus width, 32/64/128 bits. */
  1287. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_TXDESC_TAIL, 0xF);
  1288. regval = dwceqos_read(lp, REG_DWCEQOS_DMA_CH0_TXDESC_TAIL);
  1289. buswidth = (regval ^ 0xF) + 1;
  1290. /* Cache-align dma descriptors. */
  1291. dma_skip = (sizeof(struct dwceqos_dma_desc) - 16) / buswidth;
  1292. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_CTRL,
  1293. DWCEQOS_DMA_CH_CTRL_DSL(dma_skip) |
  1294. DWCEQOS_DMA_CH_CTRL_PBLX8);
  1295. /* Initialize DMA Channel 0 */
  1296. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_TXDESC_LEN, DWCEQOS_TX_DCNT - 1);
  1297. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_RXDESC_LEN, DWCEQOS_RX_DCNT - 1);
  1298. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_TXDESC_LIST,
  1299. (u32)lp->tx_descs_addr);
  1300. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_RXDESC_LIST,
  1301. (u32)lp->rx_descs_addr);
  1302. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_TXDESC_TAIL,
  1303. lp->tx_descs_tail_addr);
  1304. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_RXDESC_TAIL,
  1305. lp->rx_descs_tail_addr);
  1306. if (lp->bus_cfg.tx_pbl)
  1307. regval = DWCEQOS_DMA_CH_CTRL_PBL(lp->bus_cfg.tx_pbl);
  1308. else
  1309. regval = DWCEQOS_DMA_CH_CTRL_PBL(2);
  1310. /* Enable TSO if the HW support it */
  1311. if (lp->feature1 & DWCEQOS_MAC_HW_FEATURE1_TSOEN)
  1312. regval |= DWCEQOS_DMA_CH_TX_TSE;
  1313. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_TX_CTRL, regval);
  1314. if (lp->bus_cfg.rx_pbl)
  1315. regval = DWCEQOS_DMA_CH_CTRL_PBL(lp->bus_cfg.rx_pbl);
  1316. else
  1317. regval = DWCEQOS_DMA_CH_CTRL_PBL(2);
  1318. regval |= DWCEQOS_DMA_CH_RX_CTRL_BUFSIZE(DWCEQOS_DWCEQOS_RX_BUF_SIZE);
  1319. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_RX_CTRL, regval);
  1320. regval |= DWCEQOS_DMA_CH_CTRL_START;
  1321. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_RX_CTRL, regval);
  1322. /* Initialize MTL Queues */
  1323. regval = DWCEQOS_MTL_SCHALG_STRICT;
  1324. dwceqos_write(lp, REG_DWCEQOS_MTL_OPER, regval);
  1325. regval = DWCEQOS_MTL_TXQ_SIZE(
  1326. DWCEQOS_MAC_HW_FEATURE1_TXFIFOSIZE(lp->feature1)) |
  1327. DWCEQOS_MTL_TXQ_TXQEN | DWCEQOS_MTL_TXQ_TSF |
  1328. DWCEQOS_MTL_TXQ_TTC512;
  1329. dwceqos_write(lp, REG_DWCEQOS_MTL_TXQ0_OPER, regval);
  1330. regval = DWCEQOS_MTL_RXQ_SIZE(
  1331. DWCEQOS_MAC_HW_FEATURE1_RXFIFOSIZE(lp->feature1)) |
  1332. DWCEQOS_MTL_RXQ_FUP | DWCEQOS_MTL_RXQ_FEP | DWCEQOS_MTL_RXQ_RSF;
  1333. dwceqos_write(lp, REG_DWCEQOS_MTL_RXQ0_OPER, regval);
  1334. dwceqos_configure_flow_control(lp);
  1335. /* Initialize MAC */
  1336. dwceqos_set_umac_addr(lp, lp->ndev->dev_addr, 0);
  1337. lp->eee_enabled = 0;
  1338. dwceqos_configure_clock(lp);
  1339. /* MMC counters */
  1340. /* probe implemented counters */
  1341. dwceqos_write(lp, REG_DWCEQOS_MMC_RXIRQMASK, ~0u);
  1342. dwceqos_write(lp, REG_DWCEQOS_MMC_TXIRQMASK, ~0u);
  1343. lp->mmc_rx_counters_mask = dwceqos_read(lp, REG_DWCEQOS_MMC_RXIRQMASK);
  1344. lp->mmc_tx_counters_mask = dwceqos_read(lp, REG_DWCEQOS_MMC_TXIRQMASK);
  1345. dwceqos_write(lp, REG_DWCEQOS_MMC_CTRL, DWCEQOS_MMC_CTRL_CNTRST |
  1346. DWCEQOS_MMC_CTRL_RSTONRD);
  1347. dwceqos_enable_mmc_interrupt(lp);
  1348. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_IE, 0);
  1349. dwceqos_write(lp, REG_DWCEQOS_MAC_IE, 0);
  1350. dwceqos_write(lp, REG_DWCEQOS_MAC_CFG, DWCEQOS_MAC_CFG_IPC |
  1351. DWCEQOS_MAC_CFG_DM | DWCEQOS_MAC_CFG_TE | DWCEQOS_MAC_CFG_RE);
  1352. /* Start TX DMA */
  1353. regval = dwceqos_read(lp, REG_DWCEQOS_DMA_CH0_TX_CTRL);
  1354. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_TX_CTRL,
  1355. regval | DWCEQOS_DMA_CH_CTRL_START);
  1356. /* Enable MAC TX/RX */
  1357. regval = dwceqos_read(lp, REG_DWCEQOS_MAC_CFG);
  1358. dwceqos_write(lp, REG_DWCEQOS_MAC_CFG,
  1359. regval | DWCEQOS_MAC_CFG_TE | DWCEQOS_MAC_CFG_RE);
  1360. lp->phy_defer = false;
  1361. mutex_lock(&ndev->phydev->lock);
  1362. phy_read_status(ndev->phydev);
  1363. dwceqos_adjust_link(lp->ndev);
  1364. mutex_unlock(&ndev->phydev->lock);
  1365. }
  1366. static void dwceqos_tx_reclaim(unsigned long data)
  1367. {
  1368. struct net_device *ndev = (struct net_device *)data;
  1369. struct net_local *lp = netdev_priv(ndev);
  1370. unsigned int tx_bytes = 0;
  1371. unsigned int tx_packets = 0;
  1372. spin_lock(&lp->tx_lock);
  1373. while (lp->tx_free < DWCEQOS_TX_DCNT) {
  1374. struct dwceqos_dma_desc *dd = &lp->tx_descs[lp->tx_cur];
  1375. struct ring_desc *rd = &lp->tx_skb[lp->tx_cur];
  1376. /* Descriptor still being held by DMA ? */
  1377. if (dd->des3 & DWCEQOS_DMA_TDES3_OWN)
  1378. break;
  1379. if (rd->mapping)
  1380. dma_unmap_single(ndev->dev.parent, rd->mapping, rd->len,
  1381. DMA_TO_DEVICE);
  1382. if (unlikely(rd->skb)) {
  1383. ++tx_packets;
  1384. tx_bytes += rd->skb->len;
  1385. dev_consume_skb_any(rd->skb);
  1386. }
  1387. rd->skb = NULL;
  1388. rd->mapping = 0;
  1389. lp->tx_free++;
  1390. lp->tx_cur = (lp->tx_cur + 1) % DWCEQOS_TX_DCNT;
  1391. if ((dd->des3 & DWCEQOS_DMA_TDES3_LD) &&
  1392. (dd->des3 & DWCEQOS_DMA_RDES3_ES)) {
  1393. if (netif_msg_tx_err(lp))
  1394. netdev_err(ndev, "TX Error, TDES3 = 0x%x\n",
  1395. dd->des3);
  1396. if (netif_msg_hw(lp))
  1397. print_status(lp);
  1398. }
  1399. }
  1400. spin_unlock(&lp->tx_lock);
  1401. netdev_completed_queue(ndev, tx_packets, tx_bytes);
  1402. dwceqos_dma_enable_txirq(lp);
  1403. netif_wake_queue(ndev);
  1404. }
  1405. static int dwceqos_rx(struct net_local *lp, int budget)
  1406. {
  1407. struct sk_buff *skb;
  1408. u32 tot_size = 0;
  1409. unsigned int n_packets = 0;
  1410. unsigned int n_descs = 0;
  1411. u32 len;
  1412. struct dwceqos_dma_desc *dd;
  1413. struct sk_buff *new_skb;
  1414. dma_addr_t new_skb_baddr = 0;
  1415. while (n_descs < budget) {
  1416. if (!dwceqos_packet_avail(lp))
  1417. break;
  1418. new_skb = netdev_alloc_skb(lp->ndev, DWCEQOS_RX_BUF_SIZE);
  1419. if (!new_skb) {
  1420. netdev_err(lp->ndev, "no memory for new sk_buff\n");
  1421. break;
  1422. }
  1423. /* Get dma handle of skb->data */
  1424. new_skb_baddr = (u32)dma_map_single(lp->ndev->dev.parent,
  1425. new_skb->data,
  1426. DWCEQOS_RX_BUF_SIZE,
  1427. DMA_FROM_DEVICE);
  1428. if (dma_mapping_error(lp->ndev->dev.parent, new_skb_baddr)) {
  1429. netdev_err(lp->ndev, "DMA map error\n");
  1430. dev_kfree_skb(new_skb);
  1431. break;
  1432. }
  1433. /* Read descriptor data after reading owner bit. */
  1434. dma_rmb();
  1435. dd = &lp->rx_descs[lp->rx_cur];
  1436. len = DWCEQOS_DMA_RDES3_PL(dd->des3);
  1437. skb = lp->rx_skb[lp->rx_cur].skb;
  1438. /* Unmap old buffer */
  1439. dma_unmap_single(lp->ndev->dev.parent,
  1440. lp->rx_skb[lp->rx_cur].mapping,
  1441. lp->rx_skb[lp->rx_cur].len, DMA_FROM_DEVICE);
  1442. /* Discard packet on reception error or bad checksum */
  1443. if ((dd->des3 & DWCEQOS_DMA_RDES3_ES) ||
  1444. (dd->des1 & DWCEQOS_DMA_RDES1_IPCE)) {
  1445. dev_kfree_skb(skb);
  1446. skb = NULL;
  1447. } else {
  1448. skb_put(skb, len);
  1449. skb->protocol = eth_type_trans(skb, lp->ndev);
  1450. switch (dd->des1 & DWCEQOS_DMA_RDES1_PT) {
  1451. case DWCEQOS_DMA_RDES1_PT_UDP:
  1452. case DWCEQOS_DMA_RDES1_PT_TCP:
  1453. case DWCEQOS_DMA_RDES1_PT_ICMP:
  1454. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1455. break;
  1456. default:
  1457. skb->ip_summed = CHECKSUM_NONE;
  1458. break;
  1459. }
  1460. }
  1461. if (unlikely(!skb)) {
  1462. if (netif_msg_rx_err(lp))
  1463. netdev_dbg(lp->ndev, "rx error: des3=%X\n",
  1464. lp->rx_descs[lp->rx_cur].des3);
  1465. } else {
  1466. tot_size += skb->len;
  1467. n_packets++;
  1468. netif_receive_skb(skb);
  1469. }
  1470. lp->rx_descs[lp->rx_cur].des0 = new_skb_baddr;
  1471. lp->rx_descs[lp->rx_cur].des1 = 0;
  1472. lp->rx_descs[lp->rx_cur].des2 = 0;
  1473. /* The DMA must observe des0/1/2 written before des3. */
  1474. wmb();
  1475. lp->rx_descs[lp->rx_cur].des3 = DWCEQOS_DMA_RDES3_INTE |
  1476. DWCEQOS_DMA_RDES3_OWN |
  1477. DWCEQOS_DMA_RDES3_BUF1V;
  1478. lp->rx_skb[lp->rx_cur].mapping = new_skb_baddr;
  1479. lp->rx_skb[lp->rx_cur].len = DWCEQOS_RX_BUF_SIZE;
  1480. lp->rx_skb[lp->rx_cur].skb = new_skb;
  1481. n_descs++;
  1482. lp->rx_cur = (lp->rx_cur + 1) % DWCEQOS_RX_DCNT;
  1483. }
  1484. /* Make sure any ownership update is written to the descriptors before
  1485. * DMA wakeup.
  1486. */
  1487. wmb();
  1488. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_STA, DWCEQOS_DMA_CH0_IS_RI);
  1489. /* Wake up RX by writing tail pointer */
  1490. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_RXDESC_TAIL,
  1491. lp->rx_descs_tail_addr);
  1492. return n_descs;
  1493. }
  1494. static int dwceqos_rx_poll(struct napi_struct *napi, int budget)
  1495. {
  1496. struct net_local *lp = container_of(napi, struct net_local, napi);
  1497. int work_done = 0;
  1498. work_done = dwceqos_rx(lp, budget - work_done);
  1499. if (!dwceqos_packet_avail(lp) && work_done < budget) {
  1500. napi_complete(napi);
  1501. dwceqos_dma_enable_rxirq(lp);
  1502. } else {
  1503. work_done = budget;
  1504. }
  1505. return work_done;
  1506. }
  1507. /* Reinitialize function if a TX timed out */
  1508. static void dwceqos_reinit_for_txtimeout(struct work_struct *data)
  1509. {
  1510. struct net_local *lp = container_of(data, struct net_local,
  1511. txtimeout_reinit);
  1512. netdev_err(lp->ndev, "transmit timeout %d s, resetting...\n",
  1513. DWCEQOS_TX_TIMEOUT);
  1514. if (netif_msg_hw(lp))
  1515. print_status(lp);
  1516. rtnl_lock();
  1517. dwceqos_stop(lp->ndev);
  1518. dwceqos_open(lp->ndev);
  1519. rtnl_unlock();
  1520. }
  1521. /* DT Probing function called by main probe */
  1522. static inline int dwceqos_probe_config_dt(struct platform_device *pdev)
  1523. {
  1524. struct net_device *ndev;
  1525. struct net_local *lp;
  1526. const void *mac_address;
  1527. struct dwceqos_bus_cfg *bus_cfg;
  1528. struct device_node *np = pdev->dev.of_node;
  1529. ndev = platform_get_drvdata(pdev);
  1530. lp = netdev_priv(ndev);
  1531. bus_cfg = &lp->bus_cfg;
  1532. /* Set the MAC address. */
  1533. mac_address = of_get_mac_address(pdev->dev.of_node);
  1534. if (mac_address)
  1535. ether_addr_copy(ndev->dev_addr, mac_address);
  1536. /* These are all optional parameters */
  1537. lp->en_tx_lpi_clockgating = of_property_read_bool(np,
  1538. "snps,en-tx-lpi-clockgating");
  1539. bus_cfg->en_lpi = of_property_read_bool(np, "snps,en-lpi");
  1540. of_property_read_u32(np, "snps,write-requests",
  1541. &bus_cfg->write_requests);
  1542. of_property_read_u32(np, "snps,read-requests", &bus_cfg->read_requests);
  1543. of_property_read_u32(np, "snps,burst-map", &bus_cfg->burst_map);
  1544. of_property_read_u32(np, "snps,txpbl", &bus_cfg->tx_pbl);
  1545. of_property_read_u32(np, "snps,rxpbl", &bus_cfg->rx_pbl);
  1546. netdev_dbg(ndev, "BusCfg: lpi:%u wr:%u rr:%u bm:%X rxpbl:%u txpbl:%d\n",
  1547. bus_cfg->en_lpi,
  1548. bus_cfg->write_requests,
  1549. bus_cfg->read_requests,
  1550. bus_cfg->burst_map,
  1551. bus_cfg->rx_pbl,
  1552. bus_cfg->tx_pbl);
  1553. return 0;
  1554. }
  1555. static int dwceqos_open(struct net_device *ndev)
  1556. {
  1557. struct net_local *lp = netdev_priv(ndev);
  1558. int res;
  1559. dwceqos_reset_state(lp);
  1560. res = dwceqos_descriptor_init(lp);
  1561. if (res) {
  1562. netdev_err(ndev, "Unable to allocate DMA memory, rc %d\n", res);
  1563. return res;
  1564. }
  1565. netdev_reset_queue(ndev);
  1566. /* The dwceqos reset state machine requires all phy clocks to complete,
  1567. * hence the unusual init order with phy_start first.
  1568. */
  1569. lp->phy_defer = true;
  1570. phy_start(ndev->phydev);
  1571. dwceqos_init_hw(lp);
  1572. napi_enable(&lp->napi);
  1573. netif_start_queue(ndev);
  1574. tasklet_enable(&lp->tx_bdreclaim_tasklet);
  1575. /* Enable Interrupts -- do this only after we enable NAPI and the
  1576. * tasklet.
  1577. */
  1578. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_IE,
  1579. DWCEQOS_DMA_CH0_IE_NIE |
  1580. DWCEQOS_DMA_CH0_IE_RIE | DWCEQOS_DMA_CH0_IE_TIE |
  1581. DWCEQOS_DMA_CH0_IE_AIE |
  1582. DWCEQOS_DMA_CH0_IE_FBEE);
  1583. return 0;
  1584. }
  1585. static bool dweqos_is_tx_dma_suspended(struct net_local *lp)
  1586. {
  1587. u32 reg;
  1588. reg = dwceqos_read(lp, REG_DWCEQOS_DMA_DEBUG_ST0);
  1589. reg = DMA_GET_TX_STATE_CH0(reg);
  1590. return reg == DMA_TX_CH_SUSPENDED;
  1591. }
  1592. static void dwceqos_drain_dma(struct net_local *lp)
  1593. {
  1594. /* Wait for all pending TX buffers to be sent. Upper limit based
  1595. * on max frame size on a 10 Mbit link.
  1596. */
  1597. size_t limit = (DWCEQOS_TX_DCNT * 1250) / 100;
  1598. while (!dweqos_is_tx_dma_suspended(lp) && limit--)
  1599. usleep_range(100, 200);
  1600. }
  1601. static int dwceqos_stop(struct net_device *ndev)
  1602. {
  1603. struct net_local *lp = netdev_priv(ndev);
  1604. tasklet_disable(&lp->tx_bdreclaim_tasklet);
  1605. napi_disable(&lp->napi);
  1606. /* Stop all tx before we drain the tx dma. */
  1607. netif_tx_lock_bh(lp->ndev);
  1608. netif_stop_queue(ndev);
  1609. netif_tx_unlock_bh(lp->ndev);
  1610. dwceqos_drain_dma(lp);
  1611. dwceqos_reset_hw(lp);
  1612. phy_stop(ndev->phydev);
  1613. dwceqos_descriptor_free(lp);
  1614. return 0;
  1615. }
  1616. static void dwceqos_dmadesc_set_ctx(struct net_local *lp,
  1617. unsigned short gso_size)
  1618. {
  1619. struct dwceqos_dma_desc *dd = &lp->tx_descs[lp->tx_next];
  1620. dd->des0 = 0;
  1621. dd->des1 = 0;
  1622. dd->des2 = gso_size;
  1623. dd->des3 = DWCEQOS_DMA_TDES3_CTXT | DWCEQOS_DMA_TDES3_TCMSSV;
  1624. lp->tx_next = (lp->tx_next + 1) % DWCEQOS_TX_DCNT;
  1625. }
  1626. static void dwceqos_tx_poll_demand(struct net_local *lp)
  1627. {
  1628. dwceqos_write(lp, REG_DWCEQOS_DMA_CH0_TXDESC_TAIL,
  1629. lp->tx_descs_tail_addr);
  1630. }
  1631. struct dwceqos_tx {
  1632. size_t nr_descriptors;
  1633. size_t initial_descriptor;
  1634. size_t last_descriptor;
  1635. size_t prev_gso_size;
  1636. size_t network_header_len;
  1637. };
  1638. static void dwceqos_tx_prepare(struct sk_buff *skb, struct net_local *lp,
  1639. struct dwceqos_tx *tx)
  1640. {
  1641. size_t n = 1;
  1642. size_t i;
  1643. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_size != lp->gso_size)
  1644. ++n;
  1645. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  1646. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1647. n += (skb_frag_size(frag) + BYTES_PER_DMA_DESC - 1) /
  1648. BYTES_PER_DMA_DESC;
  1649. }
  1650. tx->nr_descriptors = n;
  1651. tx->initial_descriptor = lp->tx_next;
  1652. tx->last_descriptor = lp->tx_next;
  1653. tx->prev_gso_size = lp->gso_size;
  1654. tx->network_header_len = skb_transport_offset(skb);
  1655. if (skb_is_gso(skb))
  1656. tx->network_header_len += tcp_hdrlen(skb);
  1657. }
  1658. static int dwceqos_tx_linear(struct sk_buff *skb, struct net_local *lp,
  1659. struct dwceqos_tx *tx)
  1660. {
  1661. struct ring_desc *rd;
  1662. struct dwceqos_dma_desc *dd;
  1663. size_t payload_len;
  1664. dma_addr_t dma_handle;
  1665. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_size != lp->gso_size) {
  1666. dwceqos_dmadesc_set_ctx(lp, skb_shinfo(skb)->gso_size);
  1667. lp->gso_size = skb_shinfo(skb)->gso_size;
  1668. }
  1669. dma_handle = dma_map_single(lp->ndev->dev.parent, skb->data,
  1670. skb_headlen(skb), DMA_TO_DEVICE);
  1671. if (dma_mapping_error(lp->ndev->dev.parent, dma_handle)) {
  1672. netdev_err(lp->ndev, "TX DMA Mapping error\n");
  1673. return -ENOMEM;
  1674. }
  1675. rd = &lp->tx_skb[lp->tx_next];
  1676. dd = &lp->tx_descs[lp->tx_next];
  1677. rd->skb = NULL;
  1678. rd->len = skb_headlen(skb);
  1679. rd->mapping = dma_handle;
  1680. /* Set up DMA Descriptor */
  1681. dd->des0 = dma_handle;
  1682. if (skb_is_gso(skb)) {
  1683. payload_len = skb_headlen(skb) - tx->network_header_len;
  1684. if (payload_len)
  1685. dd->des1 = dma_handle + tx->network_header_len;
  1686. dd->des2 = tx->network_header_len |
  1687. DWCEQOS_DMA_DES2_B2L(payload_len);
  1688. dd->des3 = DWCEQOS_DMA_TDES3_TSE |
  1689. DWCEQOS_DMA_DES3_THL((tcp_hdrlen(skb) / 4)) |
  1690. (skb->len - tx->network_header_len);
  1691. } else {
  1692. dd->des1 = 0;
  1693. dd->des2 = skb_headlen(skb);
  1694. dd->des3 = skb->len;
  1695. switch (skb->ip_summed) {
  1696. case CHECKSUM_PARTIAL:
  1697. dd->des3 |= DWCEQOS_DMA_TDES3_CA;
  1698. case CHECKSUM_NONE:
  1699. case CHECKSUM_UNNECESSARY:
  1700. case CHECKSUM_COMPLETE:
  1701. default:
  1702. break;
  1703. }
  1704. }
  1705. dd->des3 |= DWCEQOS_DMA_TDES3_FD;
  1706. if (lp->tx_next != tx->initial_descriptor)
  1707. dd->des3 |= DWCEQOS_DMA_TDES3_OWN;
  1708. tx->last_descriptor = lp->tx_next;
  1709. lp->tx_next = (lp->tx_next + 1) % DWCEQOS_TX_DCNT;
  1710. return 0;
  1711. }
  1712. static int dwceqos_tx_frags(struct sk_buff *skb, struct net_local *lp,
  1713. struct dwceqos_tx *tx)
  1714. {
  1715. struct ring_desc *rd = NULL;
  1716. struct dwceqos_dma_desc *dd;
  1717. dma_addr_t dma_handle;
  1718. size_t i;
  1719. /* Setup more ring and DMA descriptor if the packet is fragmented */
  1720. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  1721. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1722. size_t frag_size;
  1723. size_t consumed_size;
  1724. /* Map DMA Area */
  1725. dma_handle = skb_frag_dma_map(lp->ndev->dev.parent, frag, 0,
  1726. skb_frag_size(frag),
  1727. DMA_TO_DEVICE);
  1728. if (dma_mapping_error(lp->ndev->dev.parent, dma_handle)) {
  1729. netdev_err(lp->ndev, "DMA Mapping error\n");
  1730. return -ENOMEM;
  1731. }
  1732. /* order-3 fragments span more than one descriptor. */
  1733. frag_size = skb_frag_size(frag);
  1734. consumed_size = 0;
  1735. while (consumed_size < frag_size) {
  1736. size_t dma_size = min_t(size_t, 16376,
  1737. frag_size - consumed_size);
  1738. rd = &lp->tx_skb[lp->tx_next];
  1739. memset(rd, 0, sizeof(*rd));
  1740. dd = &lp->tx_descs[lp->tx_next];
  1741. /* Set DMA Descriptor fields */
  1742. dd->des0 = dma_handle + consumed_size;
  1743. dd->des1 = 0;
  1744. dd->des2 = dma_size;
  1745. if (skb_is_gso(skb))
  1746. dd->des3 = (skb->len - tx->network_header_len);
  1747. else
  1748. dd->des3 = skb->len;
  1749. dd->des3 |= DWCEQOS_DMA_TDES3_OWN;
  1750. tx->last_descriptor = lp->tx_next;
  1751. lp->tx_next = (lp->tx_next + 1) % DWCEQOS_TX_DCNT;
  1752. consumed_size += dma_size;
  1753. }
  1754. rd->len = skb_frag_size(frag);
  1755. rd->mapping = dma_handle;
  1756. }
  1757. return 0;
  1758. }
  1759. static void dwceqos_tx_finalize(struct sk_buff *skb, struct net_local *lp,
  1760. struct dwceqos_tx *tx)
  1761. {
  1762. lp->tx_descs[tx->last_descriptor].des3 |= DWCEQOS_DMA_TDES3_LD;
  1763. lp->tx_descs[tx->last_descriptor].des2 |= DWCEQOS_DMA_TDES2_IOC;
  1764. lp->tx_skb[tx->last_descriptor].skb = skb;
  1765. /* Make all descriptor updates visible to the DMA before setting the
  1766. * owner bit.
  1767. */
  1768. wmb();
  1769. lp->tx_descs[tx->initial_descriptor].des3 |= DWCEQOS_DMA_TDES3_OWN;
  1770. /* Make the owner bit visible before TX wakeup. */
  1771. wmb();
  1772. dwceqos_tx_poll_demand(lp);
  1773. }
  1774. static void dwceqos_tx_rollback(struct net_local *lp, struct dwceqos_tx *tx)
  1775. {
  1776. size_t i = tx->initial_descriptor;
  1777. while (i != lp->tx_next) {
  1778. if (lp->tx_skb[i].mapping)
  1779. dma_unmap_single(lp->ndev->dev.parent,
  1780. lp->tx_skb[i].mapping,
  1781. lp->tx_skb[i].len,
  1782. DMA_TO_DEVICE);
  1783. lp->tx_skb[i].mapping = 0;
  1784. lp->tx_skb[i].skb = NULL;
  1785. memset(&lp->tx_descs[i], 0, sizeof(lp->tx_descs[i]));
  1786. i = (i + 1) % DWCEQOS_TX_DCNT;
  1787. }
  1788. lp->tx_next = tx->initial_descriptor;
  1789. lp->gso_size = tx->prev_gso_size;
  1790. }
  1791. static int dwceqos_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  1792. {
  1793. struct net_local *lp = netdev_priv(ndev);
  1794. struct dwceqos_tx trans;
  1795. int err;
  1796. dwceqos_tx_prepare(skb, lp, &trans);
  1797. if (lp->tx_free < trans.nr_descriptors) {
  1798. netif_stop_queue(ndev);
  1799. return NETDEV_TX_BUSY;
  1800. }
  1801. err = dwceqos_tx_linear(skb, lp, &trans);
  1802. if (err)
  1803. goto tx_error;
  1804. err = dwceqos_tx_frags(skb, lp, &trans);
  1805. if (err)
  1806. goto tx_error;
  1807. WARN_ON(lp->tx_next !=
  1808. ((trans.initial_descriptor + trans.nr_descriptors) %
  1809. DWCEQOS_TX_DCNT));
  1810. spin_lock_bh(&lp->tx_lock);
  1811. lp->tx_free -= trans.nr_descriptors;
  1812. dwceqos_tx_finalize(skb, lp, &trans);
  1813. netdev_sent_queue(ndev, skb->len);
  1814. spin_unlock_bh(&lp->tx_lock);
  1815. netif_trans_update(ndev);
  1816. return 0;
  1817. tx_error:
  1818. dwceqos_tx_rollback(lp, &trans);
  1819. dev_kfree_skb(skb);
  1820. return 0;
  1821. }
  1822. /* Set MAC address and then update HW accordingly */
  1823. static int dwceqos_set_mac_address(struct net_device *ndev, void *addr)
  1824. {
  1825. struct net_local *lp = netdev_priv(ndev);
  1826. struct sockaddr *hwaddr = (struct sockaddr *)addr;
  1827. if (netif_running(ndev))
  1828. return -EBUSY;
  1829. if (!is_valid_ether_addr(hwaddr->sa_data))
  1830. return -EADDRNOTAVAIL;
  1831. memcpy(ndev->dev_addr, hwaddr->sa_data, ndev->addr_len);
  1832. dwceqos_set_umac_addr(lp, lp->ndev->dev_addr, 0);
  1833. return 0;
  1834. }
  1835. static void dwceqos_tx_timeout(struct net_device *ndev)
  1836. {
  1837. struct net_local *lp = netdev_priv(ndev);
  1838. queue_work(lp->txtimeout_handler_wq, &lp->txtimeout_reinit);
  1839. }
  1840. static void dwceqos_set_umac_addr(struct net_local *lp, unsigned char *addr,
  1841. unsigned int reg_n)
  1842. {
  1843. unsigned long data;
  1844. data = (addr[5] << 8) | addr[4];
  1845. dwceqos_write(lp, DWCEQOS_ADDR_HIGH(reg_n),
  1846. data | DWCEQOS_MAC_MAC_ADDR_HI_EN);
  1847. data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  1848. dwceqos_write(lp, DWCEQOS_ADDR_LOW(reg_n), data);
  1849. }
  1850. static void dwceqos_disable_umac_addr(struct net_local *lp, unsigned int reg_n)
  1851. {
  1852. /* Do not disable MAC address 0 */
  1853. if (reg_n != 0)
  1854. dwceqos_write(lp, DWCEQOS_ADDR_HIGH(reg_n), 0);
  1855. }
  1856. static void dwceqos_set_rx_mode(struct net_device *ndev)
  1857. {
  1858. struct net_local *lp = netdev_priv(ndev);
  1859. u32 regval = 0;
  1860. u32 mc_filter[2];
  1861. int reg = 1;
  1862. struct netdev_hw_addr *ha;
  1863. unsigned int max_mac_addr;
  1864. max_mac_addr = DWCEQOS_MAX_PERFECT_ADDRESSES(lp->feature1);
  1865. if (ndev->flags & IFF_PROMISC) {
  1866. regval = DWCEQOS_MAC_PKT_FILT_PR;
  1867. } else if (((netdev_mc_count(ndev) > DWCEQOS_HASH_TABLE_SIZE) ||
  1868. (ndev->flags & IFF_ALLMULTI))) {
  1869. regval = DWCEQOS_MAC_PKT_FILT_PM;
  1870. dwceqos_write(lp, REG_DWCEQOS_HASTABLE_LO, 0xffffffff);
  1871. dwceqos_write(lp, REG_DWCEQOS_HASTABLE_HI, 0xffffffff);
  1872. } else if (!netdev_mc_empty(ndev)) {
  1873. regval = DWCEQOS_MAC_PKT_FILT_HMC;
  1874. memset(mc_filter, 0, sizeof(mc_filter));
  1875. netdev_for_each_mc_addr(ha, ndev) {
  1876. /* The upper 6 bits of the calculated CRC are used to
  1877. * index the contens of the hash table
  1878. */
  1879. int bit_nr = bitrev32(~crc32_le(~0, ha->addr, 6)) >> 26;
  1880. /* The most significant bit determines the register
  1881. * to use (H/L) while the other 5 bits determine
  1882. * the bit within the register.
  1883. */
  1884. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1885. }
  1886. dwceqos_write(lp, REG_DWCEQOS_HASTABLE_LO, mc_filter[0]);
  1887. dwceqos_write(lp, REG_DWCEQOS_HASTABLE_HI, mc_filter[1]);
  1888. }
  1889. if (netdev_uc_count(ndev) > max_mac_addr) {
  1890. regval |= DWCEQOS_MAC_PKT_FILT_PR;
  1891. } else {
  1892. netdev_for_each_uc_addr(ha, ndev) {
  1893. dwceqos_set_umac_addr(lp, ha->addr, reg);
  1894. reg++;
  1895. }
  1896. for (; reg < DWCEQOS_MAX_PERFECT_ADDRESSES(lp->feature1); reg++)
  1897. dwceqos_disable_umac_addr(lp, reg);
  1898. }
  1899. dwceqos_write(lp, REG_DWCEQOS_MAC_PKT_FILT, regval);
  1900. }
  1901. #ifdef CONFIG_NET_POLL_CONTROLLER
  1902. static void dwceqos_poll_controller(struct net_device *ndev)
  1903. {
  1904. disable_irq(ndev->irq);
  1905. dwceqos_interrupt(ndev->irq, ndev);
  1906. enable_irq(ndev->irq);
  1907. }
  1908. #endif
  1909. static void dwceqos_read_mmc_counters(struct net_local *lp, u32 rx_mask,
  1910. u32 tx_mask)
  1911. {
  1912. if (tx_mask & BIT(27))
  1913. lp->mmc_counters.txlpitranscntr +=
  1914. dwceqos_read(lp, DWC_MMC_TXLPITRANSCNTR);
  1915. if (tx_mask & BIT(26))
  1916. lp->mmc_counters.txpiuscntr +=
  1917. dwceqos_read(lp, DWC_MMC_TXLPIUSCNTR);
  1918. if (tx_mask & BIT(25))
  1919. lp->mmc_counters.txoversize_g +=
  1920. dwceqos_read(lp, DWC_MMC_TXOVERSIZE_G);
  1921. if (tx_mask & BIT(24))
  1922. lp->mmc_counters.txvlanpackets_g +=
  1923. dwceqos_read(lp, DWC_MMC_TXVLANPACKETS_G);
  1924. if (tx_mask & BIT(23))
  1925. lp->mmc_counters.txpausepackets +=
  1926. dwceqos_read(lp, DWC_MMC_TXPAUSEPACKETS);
  1927. if (tx_mask & BIT(22))
  1928. lp->mmc_counters.txexcessdef +=
  1929. dwceqos_read(lp, DWC_MMC_TXEXCESSDEF);
  1930. if (tx_mask & BIT(21))
  1931. lp->mmc_counters.txpacketcount_g +=
  1932. dwceqos_read(lp, DWC_MMC_TXPACKETCOUNT_G);
  1933. if (tx_mask & BIT(20))
  1934. lp->mmc_counters.txoctetcount_g +=
  1935. dwceqos_read(lp, DWC_MMC_TXOCTETCOUNT_G);
  1936. if (tx_mask & BIT(19))
  1937. lp->mmc_counters.txcarriererror +=
  1938. dwceqos_read(lp, DWC_MMC_TXCARRIERERROR);
  1939. if (tx_mask & BIT(18))
  1940. lp->mmc_counters.txexcesscol +=
  1941. dwceqos_read(lp, DWC_MMC_TXEXCESSCOL);
  1942. if (tx_mask & BIT(17))
  1943. lp->mmc_counters.txlatecol +=
  1944. dwceqos_read(lp, DWC_MMC_TXLATECOL);
  1945. if (tx_mask & BIT(16))
  1946. lp->mmc_counters.txdeferred +=
  1947. dwceqos_read(lp, DWC_MMC_TXDEFERRED);
  1948. if (tx_mask & BIT(15))
  1949. lp->mmc_counters.txmulticol_g +=
  1950. dwceqos_read(lp, DWC_MMC_TXMULTICOL_G);
  1951. if (tx_mask & BIT(14))
  1952. lp->mmc_counters.txsinglecol_g +=
  1953. dwceqos_read(lp, DWC_MMC_TXSINGLECOL_G);
  1954. if (tx_mask & BIT(13))
  1955. lp->mmc_counters.txunderflowerror +=
  1956. dwceqos_read(lp, DWC_MMC_TXUNDERFLOWERROR);
  1957. if (tx_mask & BIT(12))
  1958. lp->mmc_counters.txbroadcastpackets_gb +=
  1959. dwceqos_read(lp, DWC_MMC_TXBROADCASTPACKETS_GB);
  1960. if (tx_mask & BIT(11))
  1961. lp->mmc_counters.txmulticastpackets_gb +=
  1962. dwceqos_read(lp, DWC_MMC_TXMULTICASTPACKETS_GB);
  1963. if (tx_mask & BIT(10))
  1964. lp->mmc_counters.txunicastpackets_gb +=
  1965. dwceqos_read(lp, DWC_MMC_TXUNICASTPACKETS_GB);
  1966. if (tx_mask & BIT(9))
  1967. lp->mmc_counters.tx1024tomaxoctets_gb +=
  1968. dwceqos_read(lp, DWC_MMC_TX1024TOMAXOCTETS_GB);
  1969. if (tx_mask & BIT(8))
  1970. lp->mmc_counters.tx512to1023octets_gb +=
  1971. dwceqos_read(lp, DWC_MMC_TX512TO1023OCTETS_GB);
  1972. if (tx_mask & BIT(7))
  1973. lp->mmc_counters.tx256to511octets_gb +=
  1974. dwceqos_read(lp, DWC_MMC_TX256TO511OCTETS_GB);
  1975. if (tx_mask & BIT(6))
  1976. lp->mmc_counters.tx128to255octets_gb +=
  1977. dwceqos_read(lp, DWC_MMC_TX128TO255OCTETS_GB);
  1978. if (tx_mask & BIT(5))
  1979. lp->mmc_counters.tx65to127octets_gb +=
  1980. dwceqos_read(lp, DWC_MMC_TX65TO127OCTETS_GB);
  1981. if (tx_mask & BIT(4))
  1982. lp->mmc_counters.tx64octets_gb +=
  1983. dwceqos_read(lp, DWC_MMC_TX64OCTETS_GB);
  1984. if (tx_mask & BIT(3))
  1985. lp->mmc_counters.txmulticastpackets_g +=
  1986. dwceqos_read(lp, DWC_MMC_TXMULTICASTPACKETS_G);
  1987. if (tx_mask & BIT(2))
  1988. lp->mmc_counters.txbroadcastpackets_g +=
  1989. dwceqos_read(lp, DWC_MMC_TXBROADCASTPACKETS_G);
  1990. if (tx_mask & BIT(1))
  1991. lp->mmc_counters.txpacketcount_gb +=
  1992. dwceqos_read(lp, DWC_MMC_TXPACKETCOUNT_GB);
  1993. if (tx_mask & BIT(0))
  1994. lp->mmc_counters.txoctetcount_gb +=
  1995. dwceqos_read(lp, DWC_MMC_TXOCTETCOUNT_GB);
  1996. if (rx_mask & BIT(27))
  1997. lp->mmc_counters.rxlpitranscntr +=
  1998. dwceqos_read(lp, DWC_MMC_RXLPITRANSCNTR);
  1999. if (rx_mask & BIT(26))
  2000. lp->mmc_counters.rxlpiuscntr +=
  2001. dwceqos_read(lp, DWC_MMC_RXLPIUSCNTR);
  2002. if (rx_mask & BIT(25))
  2003. lp->mmc_counters.rxctrlpackets_g +=
  2004. dwceqos_read(lp, DWC_MMC_RXCTRLPACKETS_G);
  2005. if (rx_mask & BIT(24))
  2006. lp->mmc_counters.rxrcverror +=
  2007. dwceqos_read(lp, DWC_MMC_RXRCVERROR);
  2008. if (rx_mask & BIT(23))
  2009. lp->mmc_counters.rxwatchdog +=
  2010. dwceqos_read(lp, DWC_MMC_RXWATCHDOG);
  2011. if (rx_mask & BIT(22))
  2012. lp->mmc_counters.rxvlanpackets_gb +=
  2013. dwceqos_read(lp, DWC_MMC_RXVLANPACKETS_GB);
  2014. if (rx_mask & BIT(21))
  2015. lp->mmc_counters.rxfifooverflow +=
  2016. dwceqos_read(lp, DWC_MMC_RXFIFOOVERFLOW);
  2017. if (rx_mask & BIT(20))
  2018. lp->mmc_counters.rxpausepackets +=
  2019. dwceqos_read(lp, DWC_MMC_RXPAUSEPACKETS);
  2020. if (rx_mask & BIT(19))
  2021. lp->mmc_counters.rxoutofrangetype +=
  2022. dwceqos_read(lp, DWC_MMC_RXOUTOFRANGETYPE);
  2023. if (rx_mask & BIT(18))
  2024. lp->mmc_counters.rxlengtherror +=
  2025. dwceqos_read(lp, DWC_MMC_RXLENGTHERROR);
  2026. if (rx_mask & BIT(17))
  2027. lp->mmc_counters.rxunicastpackets_g +=
  2028. dwceqos_read(lp, DWC_MMC_RXUNICASTPACKETS_G);
  2029. if (rx_mask & BIT(16))
  2030. lp->mmc_counters.rx1024tomaxoctets_gb +=
  2031. dwceqos_read(lp, DWC_MMC_RX1024TOMAXOCTETS_GB);
  2032. if (rx_mask & BIT(15))
  2033. lp->mmc_counters.rx512to1023octets_gb +=
  2034. dwceqos_read(lp, DWC_MMC_RX512TO1023OCTETS_GB);
  2035. if (rx_mask & BIT(14))
  2036. lp->mmc_counters.rx256to511octets_gb +=
  2037. dwceqos_read(lp, DWC_MMC_RX256TO511OCTETS_GB);
  2038. if (rx_mask & BIT(13))
  2039. lp->mmc_counters.rx128to255octets_gb +=
  2040. dwceqos_read(lp, DWC_MMC_RX128TO255OCTETS_GB);
  2041. if (rx_mask & BIT(12))
  2042. lp->mmc_counters.rx65to127octets_gb +=
  2043. dwceqos_read(lp, DWC_MMC_RX65TO127OCTETS_GB);
  2044. if (rx_mask & BIT(11))
  2045. lp->mmc_counters.rx64octets_gb +=
  2046. dwceqos_read(lp, DWC_MMC_RX64OCTETS_GB);
  2047. if (rx_mask & BIT(10))
  2048. lp->mmc_counters.rxoversize_g +=
  2049. dwceqos_read(lp, DWC_MMC_RXOVERSIZE_G);
  2050. if (rx_mask & BIT(9))
  2051. lp->mmc_counters.rxundersize_g +=
  2052. dwceqos_read(lp, DWC_MMC_RXUNDERSIZE_G);
  2053. if (rx_mask & BIT(8))
  2054. lp->mmc_counters.rxjabbererror +=
  2055. dwceqos_read(lp, DWC_MMC_RXJABBERERROR);
  2056. if (rx_mask & BIT(7))
  2057. lp->mmc_counters.rxrunterror +=
  2058. dwceqos_read(lp, DWC_MMC_RXRUNTERROR);
  2059. if (rx_mask & BIT(6))
  2060. lp->mmc_counters.rxalignmenterror +=
  2061. dwceqos_read(lp, DWC_MMC_RXALIGNMENTERROR);
  2062. if (rx_mask & BIT(5))
  2063. lp->mmc_counters.rxcrcerror +=
  2064. dwceqos_read(lp, DWC_MMC_RXCRCERROR);
  2065. if (rx_mask & BIT(4))
  2066. lp->mmc_counters.rxmulticastpackets_g +=
  2067. dwceqos_read(lp, DWC_MMC_RXMULTICASTPACKETS_G);
  2068. if (rx_mask & BIT(3))
  2069. lp->mmc_counters.rxbroadcastpackets_g +=
  2070. dwceqos_read(lp, DWC_MMC_RXBROADCASTPACKETS_G);
  2071. if (rx_mask & BIT(2))
  2072. lp->mmc_counters.rxoctetcount_g +=
  2073. dwceqos_read(lp, DWC_MMC_RXOCTETCOUNT_G);
  2074. if (rx_mask & BIT(1))
  2075. lp->mmc_counters.rxoctetcount_gb +=
  2076. dwceqos_read(lp, DWC_MMC_RXOCTETCOUNT_GB);
  2077. if (rx_mask & BIT(0))
  2078. lp->mmc_counters.rxpacketcount_gb +=
  2079. dwceqos_read(lp, DWC_MMC_RXPACKETCOUNT_GB);
  2080. }
  2081. static struct rtnl_link_stats64*
  2082. dwceqos_get_stats64(struct net_device *ndev, struct rtnl_link_stats64 *s)
  2083. {
  2084. unsigned long flags;
  2085. struct net_local *lp = netdev_priv(ndev);
  2086. struct dwceqos_mmc_counters *hwstats = &lp->mmc_counters;
  2087. spin_lock_irqsave(&lp->stats_lock, flags);
  2088. dwceqos_read_mmc_counters(lp, lp->mmc_rx_counters_mask,
  2089. lp->mmc_tx_counters_mask);
  2090. spin_unlock_irqrestore(&lp->stats_lock, flags);
  2091. s->rx_packets = hwstats->rxpacketcount_gb;
  2092. s->rx_bytes = hwstats->rxoctetcount_gb;
  2093. s->rx_errors = hwstats->rxpacketcount_gb -
  2094. hwstats->rxbroadcastpackets_g -
  2095. hwstats->rxmulticastpackets_g -
  2096. hwstats->rxunicastpackets_g;
  2097. s->multicast = hwstats->rxmulticastpackets_g;
  2098. s->rx_length_errors = hwstats->rxlengtherror;
  2099. s->rx_crc_errors = hwstats->rxcrcerror;
  2100. s->rx_fifo_errors = hwstats->rxfifooverflow;
  2101. s->tx_packets = hwstats->txpacketcount_gb;
  2102. s->tx_bytes = hwstats->txoctetcount_gb;
  2103. if (lp->mmc_tx_counters_mask & BIT(21))
  2104. s->tx_errors = hwstats->txpacketcount_gb -
  2105. hwstats->txpacketcount_g;
  2106. else
  2107. s->tx_errors = hwstats->txunderflowerror +
  2108. hwstats->txcarriererror;
  2109. return s;
  2110. }
  2111. static void
  2112. dwceqos_get_drvinfo(struct net_device *ndev, struct ethtool_drvinfo *ed)
  2113. {
  2114. const struct net_local *lp = netdev_priv(ndev);
  2115. strcpy(ed->driver, lp->pdev->dev.driver->name);
  2116. strcpy(ed->version, DRIVER_VERSION);
  2117. }
  2118. static void dwceqos_get_pauseparam(struct net_device *ndev,
  2119. struct ethtool_pauseparam *pp)
  2120. {
  2121. const struct net_local *lp = netdev_priv(ndev);
  2122. pp->autoneg = lp->flowcontrol.autoneg;
  2123. pp->tx_pause = lp->flowcontrol.tx;
  2124. pp->rx_pause = lp->flowcontrol.rx;
  2125. }
  2126. static int dwceqos_set_pauseparam(struct net_device *ndev,
  2127. struct ethtool_pauseparam *pp)
  2128. {
  2129. struct net_local *lp = netdev_priv(ndev);
  2130. int ret = 0;
  2131. lp->flowcontrol.autoneg = pp->autoneg;
  2132. if (pp->autoneg) {
  2133. ndev->phydev->advertising |= ADVERTISED_Pause;
  2134. ndev->phydev->advertising |= ADVERTISED_Asym_Pause;
  2135. } else {
  2136. ndev->phydev->advertising &= ~ADVERTISED_Pause;
  2137. ndev->phydev->advertising &= ~ADVERTISED_Asym_Pause;
  2138. lp->flowcontrol.rx = pp->rx_pause;
  2139. lp->flowcontrol.tx = pp->tx_pause;
  2140. }
  2141. if (netif_running(ndev))
  2142. ret = phy_start_aneg(ndev->phydev);
  2143. return ret;
  2144. }
  2145. static void dwceqos_get_strings(struct net_device *ndev, u32 stringset,
  2146. u8 *data)
  2147. {
  2148. size_t i;
  2149. if (stringset != ETH_SS_STATS)
  2150. return;
  2151. for (i = 0; i < ARRAY_SIZE(dwceqos_ethtool_stats); ++i) {
  2152. memcpy(data, dwceqos_ethtool_stats[i].stat_name,
  2153. ETH_GSTRING_LEN);
  2154. data += ETH_GSTRING_LEN;
  2155. }
  2156. }
  2157. static void dwceqos_get_ethtool_stats(struct net_device *ndev,
  2158. struct ethtool_stats *stats, u64 *data)
  2159. {
  2160. struct net_local *lp = netdev_priv(ndev);
  2161. unsigned long flags;
  2162. size_t i;
  2163. u8 *mmcstat = (u8 *)&lp->mmc_counters;
  2164. spin_lock_irqsave(&lp->stats_lock, flags);
  2165. dwceqos_read_mmc_counters(lp, lp->mmc_rx_counters_mask,
  2166. lp->mmc_tx_counters_mask);
  2167. spin_unlock_irqrestore(&lp->stats_lock, flags);
  2168. for (i = 0; i < ARRAY_SIZE(dwceqos_ethtool_stats); ++i) {
  2169. memcpy(data,
  2170. mmcstat + dwceqos_ethtool_stats[i].offset,
  2171. sizeof(u64));
  2172. data++;
  2173. }
  2174. }
  2175. static int dwceqos_get_sset_count(struct net_device *ndev, int sset)
  2176. {
  2177. if (sset == ETH_SS_STATS)
  2178. return ARRAY_SIZE(dwceqos_ethtool_stats);
  2179. return -EOPNOTSUPP;
  2180. }
  2181. static void dwceqos_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  2182. void *space)
  2183. {
  2184. const struct net_local *lp = netdev_priv(dev);
  2185. u32 *reg_space = (u32 *)space;
  2186. int reg_offset;
  2187. int reg_ix = 0;
  2188. /* MAC registers */
  2189. for (reg_offset = START_MAC_REG_OFFSET;
  2190. reg_offset <= MAX_DMA_REG_OFFSET; reg_offset += 4) {
  2191. reg_space[reg_ix] = dwceqos_read(lp, reg_offset);
  2192. reg_ix++;
  2193. }
  2194. /* MTL registers */
  2195. for (reg_offset = START_MTL_REG_OFFSET;
  2196. reg_offset <= MAX_MTL_REG_OFFSET; reg_offset += 4) {
  2197. reg_space[reg_ix] = dwceqos_read(lp, reg_offset);
  2198. reg_ix++;
  2199. }
  2200. /* DMA registers */
  2201. for (reg_offset = START_DMA_REG_OFFSET;
  2202. reg_offset <= MAX_DMA_REG_OFFSET; reg_offset += 4) {
  2203. reg_space[reg_ix] = dwceqos_read(lp, reg_offset);
  2204. reg_ix++;
  2205. }
  2206. BUG_ON(4 * reg_ix > REG_SPACE_SIZE);
  2207. }
  2208. static int dwceqos_get_regs_len(struct net_device *dev)
  2209. {
  2210. return REG_SPACE_SIZE;
  2211. }
  2212. static inline const char *dwceqos_get_rx_lpi_state(u32 lpi_ctrl)
  2213. {
  2214. return (lpi_ctrl & DWCEQOS_MAC_LPI_CTRL_STATUS_RLPIST) ? "on" : "off";
  2215. }
  2216. static inline const char *dwceqos_get_tx_lpi_state(u32 lpi_ctrl)
  2217. {
  2218. return (lpi_ctrl & DWCEQOS_MAC_LPI_CTRL_STATUS_TLPIST) ? "on" : "off";
  2219. }
  2220. static int dwceqos_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
  2221. {
  2222. struct net_local *lp = netdev_priv(ndev);
  2223. u32 lpi_status;
  2224. u32 lpi_enabled;
  2225. if (!(lp->feature0 & DWCEQOS_MAC_HW_FEATURE0_EEESEL))
  2226. return -EOPNOTSUPP;
  2227. edata->eee_active = lp->eee_active;
  2228. edata->eee_enabled = lp->eee_enabled;
  2229. edata->tx_lpi_timer = dwceqos_read(lp, REG_DWCEQOS_MAC_LPI_ENTRY_TIMER);
  2230. lpi_status = dwceqos_read(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS);
  2231. lpi_enabled = !!(lpi_status & DWCEQOS_MAC_LPI_CTRL_STATUS_LIPTXA);
  2232. edata->tx_lpi_enabled = lpi_enabled;
  2233. if (netif_msg_hw(lp)) {
  2234. u32 regval;
  2235. regval = dwceqos_read(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS);
  2236. netdev_info(lp->ndev, "MAC LPI State: RX:%s TX:%s\n",
  2237. dwceqos_get_rx_lpi_state(regval),
  2238. dwceqos_get_tx_lpi_state(regval));
  2239. }
  2240. return phy_ethtool_get_eee(ndev->phydev, edata);
  2241. }
  2242. static int dwceqos_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
  2243. {
  2244. struct net_local *lp = netdev_priv(ndev);
  2245. u32 regval;
  2246. unsigned long flags;
  2247. if (!(lp->feature0 & DWCEQOS_MAC_HW_FEATURE0_EEESEL))
  2248. return -EOPNOTSUPP;
  2249. if (edata->eee_enabled && !lp->eee_active)
  2250. return -EOPNOTSUPP;
  2251. if (edata->tx_lpi_enabled) {
  2252. if (edata->tx_lpi_timer < DWCEQOS_LPI_TIMER_MIN ||
  2253. edata->tx_lpi_timer > DWCEQOS_LPI_TIMER_MAX)
  2254. return -EINVAL;
  2255. }
  2256. lp->eee_enabled = edata->eee_enabled;
  2257. if (edata->eee_enabled && edata->tx_lpi_enabled) {
  2258. dwceqos_write(lp, REG_DWCEQOS_MAC_LPI_ENTRY_TIMER,
  2259. edata->tx_lpi_timer);
  2260. spin_lock_irqsave(&lp->hw_lock, flags);
  2261. regval = dwceqos_read(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS);
  2262. regval |= DWCEQOS_LPI_CTRL_ENABLE_EEE;
  2263. if (lp->en_tx_lpi_clockgating)
  2264. regval |= DWCEQOS_MAC_LPI_CTRL_STATUS_LPITCSE;
  2265. dwceqos_write(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS, regval);
  2266. spin_unlock_irqrestore(&lp->hw_lock, flags);
  2267. } else {
  2268. spin_lock_irqsave(&lp->hw_lock, flags);
  2269. regval = dwceqos_read(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS);
  2270. regval &= ~DWCEQOS_LPI_CTRL_ENABLE_EEE;
  2271. dwceqos_write(lp, REG_DWCEQOS_MAC_LPI_CTRL_STATUS, regval);
  2272. spin_unlock_irqrestore(&lp->hw_lock, flags);
  2273. }
  2274. return phy_ethtool_set_eee(ndev->phydev, edata);
  2275. }
  2276. static u32 dwceqos_get_msglevel(struct net_device *ndev)
  2277. {
  2278. const struct net_local *lp = netdev_priv(ndev);
  2279. return lp->msg_enable;
  2280. }
  2281. static void dwceqos_set_msglevel(struct net_device *ndev, u32 msglevel)
  2282. {
  2283. struct net_local *lp = netdev_priv(ndev);
  2284. lp->msg_enable = msglevel;
  2285. }
  2286. static const struct ethtool_ops dwceqos_ethtool_ops = {
  2287. .get_drvinfo = dwceqos_get_drvinfo,
  2288. .get_link = ethtool_op_get_link,
  2289. .get_pauseparam = dwceqos_get_pauseparam,
  2290. .set_pauseparam = dwceqos_set_pauseparam,
  2291. .get_strings = dwceqos_get_strings,
  2292. .get_ethtool_stats = dwceqos_get_ethtool_stats,
  2293. .get_sset_count = dwceqos_get_sset_count,
  2294. .get_regs = dwceqos_get_regs,
  2295. .get_regs_len = dwceqos_get_regs_len,
  2296. .get_eee = dwceqos_get_eee,
  2297. .set_eee = dwceqos_set_eee,
  2298. .get_msglevel = dwceqos_get_msglevel,
  2299. .set_msglevel = dwceqos_set_msglevel,
  2300. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2301. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2302. };
  2303. static const struct net_device_ops netdev_ops = {
  2304. .ndo_open = dwceqos_open,
  2305. .ndo_stop = dwceqos_stop,
  2306. .ndo_start_xmit = dwceqos_start_xmit,
  2307. .ndo_set_rx_mode = dwceqos_set_rx_mode,
  2308. .ndo_set_mac_address = dwceqos_set_mac_address,
  2309. #ifdef CONFIG_NET_POLL_CONTROLLER
  2310. .ndo_poll_controller = dwceqos_poll_controller,
  2311. #endif
  2312. .ndo_do_ioctl = dwceqos_ioctl,
  2313. .ndo_tx_timeout = dwceqos_tx_timeout,
  2314. .ndo_get_stats64 = dwceqos_get_stats64,
  2315. };
  2316. static const struct of_device_id dwceq_of_match[] = {
  2317. { .compatible = "snps,dwc-qos-ethernet-4.10", },
  2318. {}
  2319. };
  2320. MODULE_DEVICE_TABLE(of, dwceq_of_match);
  2321. static int dwceqos_probe(struct platform_device *pdev)
  2322. {
  2323. struct resource *r_mem = NULL;
  2324. struct net_device *ndev;
  2325. struct net_local *lp;
  2326. int ret = -ENXIO;
  2327. r_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2328. if (!r_mem) {
  2329. dev_err(&pdev->dev, "no IO resource defined.\n");
  2330. return -ENXIO;
  2331. }
  2332. ndev = alloc_etherdev(sizeof(*lp));
  2333. if (!ndev) {
  2334. dev_err(&pdev->dev, "etherdev allocation failed.\n");
  2335. return -ENOMEM;
  2336. }
  2337. SET_NETDEV_DEV(ndev, &pdev->dev);
  2338. lp = netdev_priv(ndev);
  2339. lp->ndev = ndev;
  2340. lp->pdev = pdev;
  2341. lp->msg_enable = netif_msg_init(debug, DWCEQOS_MSG_DEFAULT);
  2342. spin_lock_init(&lp->tx_lock);
  2343. spin_lock_init(&lp->hw_lock);
  2344. spin_lock_init(&lp->stats_lock);
  2345. lp->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
  2346. if (IS_ERR(lp->apb_pclk)) {
  2347. dev_err(&pdev->dev, "apb_pclk clock not found.\n");
  2348. ret = PTR_ERR(lp->apb_pclk);
  2349. goto err_out_free_netdev;
  2350. }
  2351. ret = clk_prepare_enable(lp->apb_pclk);
  2352. if (ret) {
  2353. dev_err(&pdev->dev, "Unable to enable APER clock.\n");
  2354. goto err_out_free_netdev;
  2355. }
  2356. lp->baseaddr = devm_ioremap_resource(&pdev->dev, r_mem);
  2357. if (IS_ERR(lp->baseaddr)) {
  2358. dev_err(&pdev->dev, "failed to map baseaddress.\n");
  2359. ret = PTR_ERR(lp->baseaddr);
  2360. goto err_out_clk_dis_aper;
  2361. }
  2362. ndev->irq = platform_get_irq(pdev, 0);
  2363. ndev->watchdog_timeo = DWCEQOS_TX_TIMEOUT * HZ;
  2364. ndev->netdev_ops = &netdev_ops;
  2365. ndev->ethtool_ops = &dwceqos_ethtool_ops;
  2366. ndev->base_addr = r_mem->start;
  2367. dwceqos_get_hwfeatures(lp);
  2368. dwceqos_mdio_set_csr(lp);
  2369. ndev->hw_features = NETIF_F_SG;
  2370. if (lp->feature1 & DWCEQOS_MAC_HW_FEATURE1_TSOEN)
  2371. ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
  2372. if (lp->feature0 & DWCEQOS_MAC_HW_FEATURE0_TXCOESEL)
  2373. ndev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  2374. if (lp->feature0 & DWCEQOS_MAC_HW_FEATURE0_RXCOESEL)
  2375. ndev->hw_features |= NETIF_F_RXCSUM;
  2376. ndev->features = ndev->hw_features;
  2377. lp->phy_ref_clk = devm_clk_get(&pdev->dev, "phy_ref_clk");
  2378. if (IS_ERR(lp->phy_ref_clk)) {
  2379. dev_err(&pdev->dev, "phy_ref_clk clock not found.\n");
  2380. ret = PTR_ERR(lp->phy_ref_clk);
  2381. goto err_out_clk_dis_aper;
  2382. }
  2383. ret = clk_prepare_enable(lp->phy_ref_clk);
  2384. if (ret) {
  2385. dev_err(&pdev->dev, "Unable to enable device clock.\n");
  2386. goto err_out_clk_dis_aper;
  2387. }
  2388. lp->phy_node = of_parse_phandle(lp->pdev->dev.of_node,
  2389. "phy-handle", 0);
  2390. if (!lp->phy_node && of_phy_is_fixed_link(lp->pdev->dev.of_node)) {
  2391. ret = of_phy_register_fixed_link(lp->pdev->dev.of_node);
  2392. if (ret < 0) {
  2393. dev_err(&pdev->dev, "invalid fixed-link");
  2394. goto err_out_clk_dis_phy;
  2395. }
  2396. lp->phy_node = of_node_get(lp->pdev->dev.of_node);
  2397. }
  2398. ret = of_get_phy_mode(lp->pdev->dev.of_node);
  2399. if (ret < 0) {
  2400. dev_err(&lp->pdev->dev, "error in getting phy i/f\n");
  2401. goto err_out_deregister_fixed_link;
  2402. }
  2403. lp->phy_interface = ret;
  2404. ret = dwceqos_mii_init(lp);
  2405. if (ret) {
  2406. dev_err(&lp->pdev->dev, "error in dwceqos_mii_init\n");
  2407. goto err_out_deregister_fixed_link;
  2408. }
  2409. ret = dwceqos_mii_probe(ndev);
  2410. if (ret != 0) {
  2411. netdev_err(ndev, "mii_probe fail.\n");
  2412. ret = -ENXIO;
  2413. goto err_out_deregister_fixed_link;
  2414. }
  2415. dwceqos_set_umac_addr(lp, lp->ndev->dev_addr, 0);
  2416. tasklet_init(&lp->tx_bdreclaim_tasklet, dwceqos_tx_reclaim,
  2417. (unsigned long)ndev);
  2418. tasklet_disable(&lp->tx_bdreclaim_tasklet);
  2419. lp->txtimeout_handler_wq = alloc_workqueue(DRIVER_NAME,
  2420. WQ_MEM_RECLAIM, 0);
  2421. INIT_WORK(&lp->txtimeout_reinit, dwceqos_reinit_for_txtimeout);
  2422. platform_set_drvdata(pdev, ndev);
  2423. ret = dwceqos_probe_config_dt(pdev);
  2424. if (ret) {
  2425. dev_err(&lp->pdev->dev, "Unable to retrieve DT, error %d\n",
  2426. ret);
  2427. goto err_out_deregister_fixed_link;
  2428. }
  2429. dev_info(&lp->pdev->dev, "pdev->id %d, baseaddr 0x%08lx, irq %d\n",
  2430. pdev->id, ndev->base_addr, ndev->irq);
  2431. ret = devm_request_irq(&pdev->dev, ndev->irq, &dwceqos_interrupt, 0,
  2432. ndev->name, ndev);
  2433. if (ret) {
  2434. dev_err(&lp->pdev->dev, "Unable to request IRQ %d, error %d\n",
  2435. ndev->irq, ret);
  2436. goto err_out_deregister_fixed_link;
  2437. }
  2438. if (netif_msg_probe(lp))
  2439. netdev_dbg(ndev, "net_local@%p\n", lp);
  2440. netif_napi_add(ndev, &lp->napi, dwceqos_rx_poll, NAPI_POLL_WEIGHT);
  2441. ret = register_netdev(ndev);
  2442. if (ret) {
  2443. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  2444. goto err_out_deregister_fixed_link;
  2445. }
  2446. return 0;
  2447. err_out_deregister_fixed_link:
  2448. if (of_phy_is_fixed_link(pdev->dev.of_node))
  2449. of_phy_deregister_fixed_link(pdev->dev.of_node);
  2450. err_out_clk_dis_phy:
  2451. clk_disable_unprepare(lp->phy_ref_clk);
  2452. err_out_clk_dis_aper:
  2453. clk_disable_unprepare(lp->apb_pclk);
  2454. err_out_free_netdev:
  2455. of_node_put(lp->phy_node);
  2456. free_netdev(ndev);
  2457. platform_set_drvdata(pdev, NULL);
  2458. return ret;
  2459. }
  2460. static int dwceqos_remove(struct platform_device *pdev)
  2461. {
  2462. struct net_device *ndev = platform_get_drvdata(pdev);
  2463. struct net_local *lp;
  2464. if (ndev) {
  2465. lp = netdev_priv(ndev);
  2466. if (ndev->phydev) {
  2467. phy_disconnect(ndev->phydev);
  2468. if (of_phy_is_fixed_link(pdev->dev.of_node))
  2469. of_phy_deregister_fixed_link(pdev->dev.of_node);
  2470. }
  2471. mdiobus_unregister(lp->mii_bus);
  2472. mdiobus_free(lp->mii_bus);
  2473. unregister_netdev(ndev);
  2474. clk_disable_unprepare(lp->phy_ref_clk);
  2475. clk_disable_unprepare(lp->apb_pclk);
  2476. free_netdev(ndev);
  2477. }
  2478. return 0;
  2479. }
  2480. static struct platform_driver dwceqos_driver = {
  2481. .probe = dwceqos_probe,
  2482. .remove = dwceqos_remove,
  2483. .driver = {
  2484. .name = DRIVER_NAME,
  2485. .of_match_table = dwceq_of_match,
  2486. },
  2487. };
  2488. module_platform_driver(dwceqos_driver);
  2489. MODULE_DESCRIPTION("DWC Ethernet QoS v4.10a driver");
  2490. MODULE_LICENSE("GPL v2");
  2491. MODULE_AUTHOR("Andreas Irestaal <andreas.irestal@axis.com>");
  2492. MODULE_AUTHOR("Lars Persson <lars.persson@axis.com>");