xgmac.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. /*
  2. * Copyright 2010-2011 Calxeda, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/circ_buf.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/if.h>
  26. #include <linux/crc32.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/slab.h>
  29. /* XGMAC Register definitions */
  30. #define XGMAC_CONTROL 0x00000000 /* MAC Configuration */
  31. #define XGMAC_FRAME_FILTER 0x00000004 /* MAC Frame Filter */
  32. #define XGMAC_FLOW_CTRL 0x00000018 /* MAC Flow Control */
  33. #define XGMAC_VLAN_TAG 0x0000001C /* VLAN Tags */
  34. #define XGMAC_VERSION 0x00000020 /* Version */
  35. #define XGMAC_VLAN_INCL 0x00000024 /* VLAN tag for tx frames */
  36. #define XGMAC_LPI_CTRL 0x00000028 /* LPI Control and Status */
  37. #define XGMAC_LPI_TIMER 0x0000002C /* LPI Timers Control */
  38. #define XGMAC_TX_PACE 0x00000030 /* Transmit Pace and Stretch */
  39. #define XGMAC_VLAN_HASH 0x00000034 /* VLAN Hash Table */
  40. #define XGMAC_DEBUG 0x00000038 /* Debug */
  41. #define XGMAC_INT_STAT 0x0000003C /* Interrupt and Control */
  42. #define XGMAC_ADDR_HIGH(reg) (0x00000040 + ((reg) * 8))
  43. #define XGMAC_ADDR_LOW(reg) (0x00000044 + ((reg) * 8))
  44. #define XGMAC_HASH(n) (0x00000300 + (n) * 4) /* HASH table regs */
  45. #define XGMAC_NUM_HASH 16
  46. #define XGMAC_OMR 0x00000400
  47. #define XGMAC_REMOTE_WAKE 0x00000700 /* Remote Wake-Up Frm Filter */
  48. #define XGMAC_PMT 0x00000704 /* PMT Control and Status */
  49. #define XGMAC_MMC_CTRL 0x00000800 /* XGMAC MMC Control */
  50. #define XGMAC_MMC_INTR_RX 0x00000804 /* Recieve Interrupt */
  51. #define XGMAC_MMC_INTR_TX 0x00000808 /* Transmit Interrupt */
  52. #define XGMAC_MMC_INTR_MASK_RX 0x0000080c /* Recieve Interrupt Mask */
  53. #define XGMAC_MMC_INTR_MASK_TX 0x00000810 /* Transmit Interrupt Mask */
  54. /* Hardware TX Statistics Counters */
  55. #define XGMAC_MMC_TXOCTET_GB_LO 0x00000814
  56. #define XGMAC_MMC_TXOCTET_GB_HI 0x00000818
  57. #define XGMAC_MMC_TXFRAME_GB_LO 0x0000081C
  58. #define XGMAC_MMC_TXFRAME_GB_HI 0x00000820
  59. #define XGMAC_MMC_TXBCFRAME_G 0x00000824
  60. #define XGMAC_MMC_TXMCFRAME_G 0x0000082C
  61. #define XGMAC_MMC_TXUCFRAME_GB 0x00000864
  62. #define XGMAC_MMC_TXMCFRAME_GB 0x0000086C
  63. #define XGMAC_MMC_TXBCFRAME_GB 0x00000874
  64. #define XGMAC_MMC_TXUNDERFLOW 0x0000087C
  65. #define XGMAC_MMC_TXOCTET_G_LO 0x00000884
  66. #define XGMAC_MMC_TXOCTET_G_HI 0x00000888
  67. #define XGMAC_MMC_TXFRAME_G_LO 0x0000088C
  68. #define XGMAC_MMC_TXFRAME_G_HI 0x00000890
  69. #define XGMAC_MMC_TXPAUSEFRAME 0x00000894
  70. #define XGMAC_MMC_TXVLANFRAME 0x0000089C
  71. /* Hardware RX Statistics Counters */
  72. #define XGMAC_MMC_RXFRAME_GB_LO 0x00000900
  73. #define XGMAC_MMC_RXFRAME_GB_HI 0x00000904
  74. #define XGMAC_MMC_RXOCTET_GB_LO 0x00000908
  75. #define XGMAC_MMC_RXOCTET_GB_HI 0x0000090C
  76. #define XGMAC_MMC_RXOCTET_G_LO 0x00000910
  77. #define XGMAC_MMC_RXOCTET_G_HI 0x00000914
  78. #define XGMAC_MMC_RXBCFRAME_G 0x00000918
  79. #define XGMAC_MMC_RXMCFRAME_G 0x00000920
  80. #define XGMAC_MMC_RXCRCERR 0x00000928
  81. #define XGMAC_MMC_RXRUNT 0x00000930
  82. #define XGMAC_MMC_RXJABBER 0x00000934
  83. #define XGMAC_MMC_RXUCFRAME_G 0x00000970
  84. #define XGMAC_MMC_RXLENGTHERR 0x00000978
  85. #define XGMAC_MMC_RXPAUSEFRAME 0x00000988
  86. #define XGMAC_MMC_RXOVERFLOW 0x00000990
  87. #define XGMAC_MMC_RXVLANFRAME 0x00000998
  88. #define XGMAC_MMC_RXWATCHDOG 0x000009a0
  89. /* DMA Control and Status Registers */
  90. #define XGMAC_DMA_BUS_MODE 0x00000f00 /* Bus Mode */
  91. #define XGMAC_DMA_TX_POLL 0x00000f04 /* Transmit Poll Demand */
  92. #define XGMAC_DMA_RX_POLL 0x00000f08 /* Received Poll Demand */
  93. #define XGMAC_DMA_RX_BASE_ADDR 0x00000f0c /* Receive List Base */
  94. #define XGMAC_DMA_TX_BASE_ADDR 0x00000f10 /* Transmit List Base */
  95. #define XGMAC_DMA_STATUS 0x00000f14 /* Status Register */
  96. #define XGMAC_DMA_CONTROL 0x00000f18 /* Ctrl (Operational Mode) */
  97. #define XGMAC_DMA_INTR_ENA 0x00000f1c /* Interrupt Enable */
  98. #define XGMAC_DMA_MISS_FRAME_CTR 0x00000f20 /* Missed Frame Counter */
  99. #define XGMAC_DMA_RI_WDOG_TIMER 0x00000f24 /* RX Intr Watchdog Timer */
  100. #define XGMAC_DMA_AXI_BUS 0x00000f28 /* AXI Bus Mode */
  101. #define XGMAC_DMA_AXI_STATUS 0x00000f2C /* AXI Status */
  102. #define XGMAC_DMA_HW_FEATURE 0x00000f58 /* Enabled Hardware Features */
  103. #define XGMAC_ADDR_AE 0x80000000
  104. #define XGMAC_MAX_FILTER_ADDR 31
  105. /* PMT Control and Status */
  106. #define XGMAC_PMT_POINTER_RESET 0x80000000
  107. #define XGMAC_PMT_GLBL_UNICAST 0x00000200
  108. #define XGMAC_PMT_WAKEUP_RX_FRM 0x00000040
  109. #define XGMAC_PMT_MAGIC_PKT 0x00000020
  110. #define XGMAC_PMT_WAKEUP_FRM_EN 0x00000004
  111. #define XGMAC_PMT_MAGIC_PKT_EN 0x00000002
  112. #define XGMAC_PMT_POWERDOWN 0x00000001
  113. #define XGMAC_CONTROL_SPD 0x40000000 /* Speed control */
  114. #define XGMAC_CONTROL_SPD_MASK 0x60000000
  115. #define XGMAC_CONTROL_SPD_1G 0x60000000
  116. #define XGMAC_CONTROL_SPD_2_5G 0x40000000
  117. #define XGMAC_CONTROL_SPD_10G 0x00000000
  118. #define XGMAC_CONTROL_SARC 0x10000000 /* Source Addr Insert/Replace */
  119. #define XGMAC_CONTROL_SARK_MASK 0x18000000
  120. #define XGMAC_CONTROL_CAR 0x04000000 /* CRC Addition/Replacement */
  121. #define XGMAC_CONTROL_CAR_MASK 0x06000000
  122. #define XGMAC_CONTROL_DP 0x01000000 /* Disable Padding */
  123. #define XGMAC_CONTROL_WD 0x00800000 /* Disable Watchdog on rx */
  124. #define XGMAC_CONTROL_JD 0x00400000 /* Jabber disable */
  125. #define XGMAC_CONTROL_JE 0x00100000 /* Jumbo frame */
  126. #define XGMAC_CONTROL_LM 0x00001000 /* Loop-back mode */
  127. #define XGMAC_CONTROL_IPC 0x00000400 /* Checksum Offload */
  128. #define XGMAC_CONTROL_ACS 0x00000080 /* Automatic Pad/FCS Strip */
  129. #define XGMAC_CONTROL_DDIC 0x00000010 /* Disable Deficit Idle Count */
  130. #define XGMAC_CONTROL_TE 0x00000008 /* Transmitter Enable */
  131. #define XGMAC_CONTROL_RE 0x00000004 /* Receiver Enable */
  132. /* XGMAC Frame Filter defines */
  133. #define XGMAC_FRAME_FILTER_PR 0x00000001 /* Promiscuous Mode */
  134. #define XGMAC_FRAME_FILTER_HUC 0x00000002 /* Hash Unicast */
  135. #define XGMAC_FRAME_FILTER_HMC 0x00000004 /* Hash Multicast */
  136. #define XGMAC_FRAME_FILTER_DAIF 0x00000008 /* DA Inverse Filtering */
  137. #define XGMAC_FRAME_FILTER_PM 0x00000010 /* Pass all multicast */
  138. #define XGMAC_FRAME_FILTER_DBF 0x00000020 /* Disable Broadcast frames */
  139. #define XGMAC_FRAME_FILTER_SAIF 0x00000100 /* Inverse Filtering */
  140. #define XGMAC_FRAME_FILTER_SAF 0x00000200 /* Source Address Filter */
  141. #define XGMAC_FRAME_FILTER_HPF 0x00000400 /* Hash or perfect Filter */
  142. #define XGMAC_FRAME_FILTER_VHF 0x00000800 /* VLAN Hash Filter */
  143. #define XGMAC_FRAME_FILTER_VPF 0x00001000 /* VLAN Perfect Filter */
  144. #define XGMAC_FRAME_FILTER_RA 0x80000000 /* Receive all mode */
  145. /* XGMAC FLOW CTRL defines */
  146. #define XGMAC_FLOW_CTRL_PT_MASK 0xffff0000 /* Pause Time Mask */
  147. #define XGMAC_FLOW_CTRL_PT_SHIFT 16
  148. #define XGMAC_FLOW_CTRL_DZQP 0x00000080 /* Disable Zero-Quanta Phase */
  149. #define XGMAC_FLOW_CTRL_PLT 0x00000020 /* Pause Low Threshhold */
  150. #define XGMAC_FLOW_CTRL_PLT_MASK 0x00000030 /* PLT MASK */
  151. #define XGMAC_FLOW_CTRL_UP 0x00000008 /* Unicast Pause Frame Detect */
  152. #define XGMAC_FLOW_CTRL_RFE 0x00000004 /* Rx Flow Control Enable */
  153. #define XGMAC_FLOW_CTRL_TFE 0x00000002 /* Tx Flow Control Enable */
  154. #define XGMAC_FLOW_CTRL_FCB_BPA 0x00000001 /* Flow Control Busy ... */
  155. /* XGMAC_INT_STAT reg */
  156. #define XGMAC_INT_STAT_PMTIM 0x00800000 /* PMT Interrupt Mask */
  157. #define XGMAC_INT_STAT_PMT 0x0080 /* PMT Interrupt Status */
  158. #define XGMAC_INT_STAT_LPI 0x0040 /* LPI Interrupt Status */
  159. /* DMA Bus Mode register defines */
  160. #define DMA_BUS_MODE_SFT_RESET 0x00000001 /* Software Reset */
  161. #define DMA_BUS_MODE_DSL_MASK 0x0000007c /* Descriptor Skip Length */
  162. #define DMA_BUS_MODE_DSL_SHIFT 2 /* (in DWORDS) */
  163. #define DMA_BUS_MODE_ATDS 0x00000080 /* Alternate Descriptor Size */
  164. /* Programmable burst length */
  165. #define DMA_BUS_MODE_PBL_MASK 0x00003f00 /* Programmable Burst Len */
  166. #define DMA_BUS_MODE_PBL_SHIFT 8
  167. #define DMA_BUS_MODE_FB 0x00010000 /* Fixed burst */
  168. #define DMA_BUS_MODE_RPBL_MASK 0x003e0000 /* Rx-Programmable Burst Len */
  169. #define DMA_BUS_MODE_RPBL_SHIFT 17
  170. #define DMA_BUS_MODE_USP 0x00800000
  171. #define DMA_BUS_MODE_8PBL 0x01000000
  172. #define DMA_BUS_MODE_AAL 0x02000000
  173. /* DMA Bus Mode register defines */
  174. #define DMA_BUS_PR_RATIO_MASK 0x0000c000 /* Rx/Tx priority ratio */
  175. #define DMA_BUS_PR_RATIO_SHIFT 14
  176. #define DMA_BUS_FB 0x00010000 /* Fixed Burst */
  177. /* DMA Control register defines */
  178. #define DMA_CONTROL_ST 0x00002000 /* Start/Stop Transmission */
  179. #define DMA_CONTROL_SR 0x00000002 /* Start/Stop Receive */
  180. #define DMA_CONTROL_DFF 0x01000000 /* Disable flush of rx frames */
  181. #define DMA_CONTROL_OSF 0x00000004 /* Operate on 2nd tx frame */
  182. /* DMA Normal interrupt */
  183. #define DMA_INTR_ENA_NIE 0x00010000 /* Normal Summary */
  184. #define DMA_INTR_ENA_AIE 0x00008000 /* Abnormal Summary */
  185. #define DMA_INTR_ENA_ERE 0x00004000 /* Early Receive */
  186. #define DMA_INTR_ENA_FBE 0x00002000 /* Fatal Bus Error */
  187. #define DMA_INTR_ENA_ETE 0x00000400 /* Early Transmit */
  188. #define DMA_INTR_ENA_RWE 0x00000200 /* Receive Watchdog */
  189. #define DMA_INTR_ENA_RSE 0x00000100 /* Receive Stopped */
  190. #define DMA_INTR_ENA_RUE 0x00000080 /* Receive Buffer Unavailable */
  191. #define DMA_INTR_ENA_RIE 0x00000040 /* Receive Interrupt */
  192. #define DMA_INTR_ENA_UNE 0x00000020 /* Tx Underflow */
  193. #define DMA_INTR_ENA_OVE 0x00000010 /* Receive Overflow */
  194. #define DMA_INTR_ENA_TJE 0x00000008 /* Transmit Jabber */
  195. #define DMA_INTR_ENA_TUE 0x00000004 /* Transmit Buffer Unavail */
  196. #define DMA_INTR_ENA_TSE 0x00000002 /* Transmit Stopped */
  197. #define DMA_INTR_ENA_TIE 0x00000001 /* Transmit Interrupt */
  198. #define DMA_INTR_NORMAL (DMA_INTR_ENA_NIE | DMA_INTR_ENA_RIE | \
  199. DMA_INTR_ENA_TUE | DMA_INTR_ENA_TIE)
  200. #define DMA_INTR_ABNORMAL (DMA_INTR_ENA_AIE | DMA_INTR_ENA_FBE | \
  201. DMA_INTR_ENA_RWE | DMA_INTR_ENA_RSE | \
  202. DMA_INTR_ENA_RUE | DMA_INTR_ENA_UNE | \
  203. DMA_INTR_ENA_OVE | DMA_INTR_ENA_TJE | \
  204. DMA_INTR_ENA_TSE)
  205. /* DMA default interrupt mask */
  206. #define DMA_INTR_DEFAULT_MASK (DMA_INTR_NORMAL | DMA_INTR_ABNORMAL)
  207. /* DMA Status register defines */
  208. #define DMA_STATUS_GMI 0x08000000 /* MMC interrupt */
  209. #define DMA_STATUS_GLI 0x04000000 /* GMAC Line interface int */
  210. #define DMA_STATUS_EB_MASK 0x00380000 /* Error Bits Mask */
  211. #define DMA_STATUS_EB_TX_ABORT 0x00080000 /* Error Bits - TX Abort */
  212. #define DMA_STATUS_EB_RX_ABORT 0x00100000 /* Error Bits - RX Abort */
  213. #define DMA_STATUS_TS_MASK 0x00700000 /* Transmit Process State */
  214. #define DMA_STATUS_TS_SHIFT 20
  215. #define DMA_STATUS_RS_MASK 0x000e0000 /* Receive Process State */
  216. #define DMA_STATUS_RS_SHIFT 17
  217. #define DMA_STATUS_NIS 0x00010000 /* Normal Interrupt Summary */
  218. #define DMA_STATUS_AIS 0x00008000 /* Abnormal Interrupt Summary */
  219. #define DMA_STATUS_ERI 0x00004000 /* Early Receive Interrupt */
  220. #define DMA_STATUS_FBI 0x00002000 /* Fatal Bus Error Interrupt */
  221. #define DMA_STATUS_ETI 0x00000400 /* Early Transmit Interrupt */
  222. #define DMA_STATUS_RWT 0x00000200 /* Receive Watchdog Timeout */
  223. #define DMA_STATUS_RPS 0x00000100 /* Receive Process Stopped */
  224. #define DMA_STATUS_RU 0x00000080 /* Receive Buffer Unavailable */
  225. #define DMA_STATUS_RI 0x00000040 /* Receive Interrupt */
  226. #define DMA_STATUS_UNF 0x00000020 /* Transmit Underflow */
  227. #define DMA_STATUS_OVF 0x00000010 /* Receive Overflow */
  228. #define DMA_STATUS_TJT 0x00000008 /* Transmit Jabber Timeout */
  229. #define DMA_STATUS_TU 0x00000004 /* Transmit Buffer Unavail */
  230. #define DMA_STATUS_TPS 0x00000002 /* Transmit Process Stopped */
  231. #define DMA_STATUS_TI 0x00000001 /* Transmit Interrupt */
  232. /* Common MAC defines */
  233. #define MAC_ENABLE_TX 0x00000008 /* Transmitter Enable */
  234. #define MAC_ENABLE_RX 0x00000004 /* Receiver Enable */
  235. /* XGMAC Operation Mode Register */
  236. #define XGMAC_OMR_TSF 0x00200000 /* TX FIFO Store and Forward */
  237. #define XGMAC_OMR_FTF 0x00100000 /* Flush Transmit FIFO */
  238. #define XGMAC_OMR_TTC 0x00020000 /* Transmit Threshhold Ctrl */
  239. #define XGMAC_OMR_TTC_MASK 0x00030000
  240. #define XGMAC_OMR_RFD 0x00006000 /* FC Deactivation Threshhold */
  241. #define XGMAC_OMR_RFD_MASK 0x00007000 /* FC Deact Threshhold MASK */
  242. #define XGMAC_OMR_RFA 0x00000600 /* FC Activation Threshhold */
  243. #define XGMAC_OMR_RFA_MASK 0x00000E00 /* FC Act Threshhold MASK */
  244. #define XGMAC_OMR_EFC 0x00000100 /* Enable Hardware FC */
  245. #define XGMAC_OMR_FEF 0x00000080 /* Forward Error Frames */
  246. #define XGMAC_OMR_DT 0x00000040 /* Drop TCP/IP csum Errors */
  247. #define XGMAC_OMR_RSF 0x00000020 /* RX FIFO Store and Forward */
  248. #define XGMAC_OMR_RTC_256 0x00000018 /* RX Threshhold Ctrl */
  249. #define XGMAC_OMR_RTC_MASK 0x00000018 /* RX Threshhold Ctrl MASK */
  250. /* XGMAC HW Features Register */
  251. #define DMA_HW_FEAT_TXCOESEL 0x00010000 /* TX Checksum offload */
  252. #define XGMAC_MMC_CTRL_CNT_FRZ 0x00000008
  253. /* XGMAC Descriptor Defines */
  254. #define MAX_DESC_BUF_SZ (0x2000 - 8)
  255. #define RXDESC_EXT_STATUS 0x00000001
  256. #define RXDESC_CRC_ERR 0x00000002
  257. #define RXDESC_RX_ERR 0x00000008
  258. #define RXDESC_RX_WDOG 0x00000010
  259. #define RXDESC_FRAME_TYPE 0x00000020
  260. #define RXDESC_GIANT_FRAME 0x00000080
  261. #define RXDESC_LAST_SEG 0x00000100
  262. #define RXDESC_FIRST_SEG 0x00000200
  263. #define RXDESC_VLAN_FRAME 0x00000400
  264. #define RXDESC_OVERFLOW_ERR 0x00000800
  265. #define RXDESC_LENGTH_ERR 0x00001000
  266. #define RXDESC_SA_FILTER_FAIL 0x00002000
  267. #define RXDESC_DESCRIPTOR_ERR 0x00004000
  268. #define RXDESC_ERROR_SUMMARY 0x00008000
  269. #define RXDESC_FRAME_LEN_OFFSET 16
  270. #define RXDESC_FRAME_LEN_MASK 0x3fff0000
  271. #define RXDESC_DA_FILTER_FAIL 0x40000000
  272. #define RXDESC1_END_RING 0x00008000
  273. #define RXDESC_IP_PAYLOAD_MASK 0x00000003
  274. #define RXDESC_IP_PAYLOAD_UDP 0x00000001
  275. #define RXDESC_IP_PAYLOAD_TCP 0x00000002
  276. #define RXDESC_IP_PAYLOAD_ICMP 0x00000003
  277. #define RXDESC_IP_HEADER_ERR 0x00000008
  278. #define RXDESC_IP_PAYLOAD_ERR 0x00000010
  279. #define RXDESC_IPV4_PACKET 0x00000040
  280. #define RXDESC_IPV6_PACKET 0x00000080
  281. #define TXDESC_UNDERFLOW_ERR 0x00000001
  282. #define TXDESC_JABBER_TIMEOUT 0x00000002
  283. #define TXDESC_LOCAL_FAULT 0x00000004
  284. #define TXDESC_REMOTE_FAULT 0x00000008
  285. #define TXDESC_VLAN_FRAME 0x00000010
  286. #define TXDESC_FRAME_FLUSHED 0x00000020
  287. #define TXDESC_IP_HEADER_ERR 0x00000040
  288. #define TXDESC_PAYLOAD_CSUM_ERR 0x00000080
  289. #define TXDESC_ERROR_SUMMARY 0x00008000
  290. #define TXDESC_SA_CTRL_INSERT 0x00040000
  291. #define TXDESC_SA_CTRL_REPLACE 0x00080000
  292. #define TXDESC_2ND_ADDR_CHAINED 0x00100000
  293. #define TXDESC_END_RING 0x00200000
  294. #define TXDESC_CSUM_IP 0x00400000
  295. #define TXDESC_CSUM_IP_PAYLD 0x00800000
  296. #define TXDESC_CSUM_ALL 0x00C00000
  297. #define TXDESC_CRC_EN_REPLACE 0x01000000
  298. #define TXDESC_CRC_EN_APPEND 0x02000000
  299. #define TXDESC_DISABLE_PAD 0x04000000
  300. #define TXDESC_FIRST_SEG 0x10000000
  301. #define TXDESC_LAST_SEG 0x20000000
  302. #define TXDESC_INTERRUPT 0x40000000
  303. #define DESC_OWN 0x80000000
  304. #define DESC_BUFFER1_SZ_MASK 0x00001fff
  305. #define DESC_BUFFER2_SZ_MASK 0x1fff0000
  306. #define DESC_BUFFER2_SZ_OFFSET 16
  307. struct xgmac_dma_desc {
  308. __le32 flags;
  309. __le32 buf_size;
  310. __le32 buf1_addr; /* Buffer 1 Address Pointer */
  311. __le32 buf2_addr; /* Buffer 2 Address Pointer */
  312. __le32 ext_status;
  313. __le32 res[3];
  314. };
  315. struct xgmac_extra_stats {
  316. /* Transmit errors */
  317. unsigned long tx_jabber;
  318. unsigned long tx_frame_flushed;
  319. unsigned long tx_payload_error;
  320. unsigned long tx_ip_header_error;
  321. unsigned long tx_local_fault;
  322. unsigned long tx_remote_fault;
  323. /* Receive errors */
  324. unsigned long rx_watchdog;
  325. unsigned long rx_da_filter_fail;
  326. unsigned long rx_sa_filter_fail;
  327. unsigned long rx_payload_error;
  328. unsigned long rx_ip_header_error;
  329. /* Tx/Rx IRQ errors */
  330. unsigned long tx_undeflow;
  331. unsigned long tx_process_stopped;
  332. unsigned long rx_buf_unav;
  333. unsigned long rx_process_stopped;
  334. unsigned long tx_early;
  335. unsigned long fatal_bus_error;
  336. };
  337. struct xgmac_priv {
  338. struct xgmac_dma_desc *dma_rx;
  339. struct sk_buff **rx_skbuff;
  340. unsigned int rx_tail;
  341. unsigned int rx_head;
  342. struct xgmac_dma_desc *dma_tx;
  343. struct sk_buff **tx_skbuff;
  344. unsigned int tx_head;
  345. unsigned int tx_tail;
  346. int tx_irq_cnt;
  347. void __iomem *base;
  348. unsigned int dma_buf_sz;
  349. dma_addr_t dma_rx_phy;
  350. dma_addr_t dma_tx_phy;
  351. struct net_device *dev;
  352. struct device *device;
  353. struct napi_struct napi;
  354. struct xgmac_extra_stats xstats;
  355. spinlock_t stats_lock;
  356. int pmt_irq;
  357. char rx_pause;
  358. char tx_pause;
  359. int wolopts;
  360. };
  361. /* XGMAC Configuration Settings */
  362. #define MAX_MTU 9000
  363. #define PAUSE_TIME 0x400
  364. #define DMA_RX_RING_SZ 256
  365. #define DMA_TX_RING_SZ 128
  366. /* minimum number of free TX descriptors required to wake up TX process */
  367. #define TX_THRESH (DMA_TX_RING_SZ/4)
  368. /* DMA descriptor ring helpers */
  369. #define dma_ring_incr(n, s) (((n) + 1) & ((s) - 1))
  370. #define dma_ring_space(h, t, s) CIRC_SPACE(h, t, s)
  371. #define dma_ring_cnt(h, t, s) CIRC_CNT(h, t, s)
  372. /* XGMAC Descriptor Access Helpers */
  373. static inline void desc_set_buf_len(struct xgmac_dma_desc *p, u32 buf_sz)
  374. {
  375. if (buf_sz > MAX_DESC_BUF_SZ)
  376. p->buf_size = cpu_to_le32(MAX_DESC_BUF_SZ |
  377. (buf_sz - MAX_DESC_BUF_SZ) << DESC_BUFFER2_SZ_OFFSET);
  378. else
  379. p->buf_size = cpu_to_le32(buf_sz);
  380. }
  381. static inline int desc_get_buf_len(struct xgmac_dma_desc *p)
  382. {
  383. u32 len = cpu_to_le32(p->flags);
  384. return (len & DESC_BUFFER1_SZ_MASK) +
  385. ((len & DESC_BUFFER2_SZ_MASK) >> DESC_BUFFER2_SZ_OFFSET);
  386. }
  387. static inline void desc_init_rx_desc(struct xgmac_dma_desc *p, int ring_size,
  388. int buf_sz)
  389. {
  390. struct xgmac_dma_desc *end = p + ring_size - 1;
  391. memset(p, 0, sizeof(*p) * ring_size);
  392. for (; p <= end; p++)
  393. desc_set_buf_len(p, buf_sz);
  394. end->buf_size |= cpu_to_le32(RXDESC1_END_RING);
  395. }
  396. static inline void desc_init_tx_desc(struct xgmac_dma_desc *p, u32 ring_size)
  397. {
  398. memset(p, 0, sizeof(*p) * ring_size);
  399. p[ring_size - 1].flags = cpu_to_le32(TXDESC_END_RING);
  400. }
  401. static inline int desc_get_owner(struct xgmac_dma_desc *p)
  402. {
  403. return le32_to_cpu(p->flags) & DESC_OWN;
  404. }
  405. static inline void desc_set_rx_owner(struct xgmac_dma_desc *p)
  406. {
  407. /* Clear all fields and set the owner */
  408. p->flags = cpu_to_le32(DESC_OWN);
  409. }
  410. static inline void desc_set_tx_owner(struct xgmac_dma_desc *p, u32 flags)
  411. {
  412. u32 tmpflags = le32_to_cpu(p->flags);
  413. tmpflags &= TXDESC_END_RING;
  414. tmpflags |= flags | DESC_OWN;
  415. p->flags = cpu_to_le32(tmpflags);
  416. }
  417. static inline int desc_get_tx_ls(struct xgmac_dma_desc *p)
  418. {
  419. return le32_to_cpu(p->flags) & TXDESC_LAST_SEG;
  420. }
  421. static inline u32 desc_get_buf_addr(struct xgmac_dma_desc *p)
  422. {
  423. return le32_to_cpu(p->buf1_addr);
  424. }
  425. static inline void desc_set_buf_addr(struct xgmac_dma_desc *p,
  426. u32 paddr, int len)
  427. {
  428. p->buf1_addr = cpu_to_le32(paddr);
  429. if (len > MAX_DESC_BUF_SZ)
  430. p->buf2_addr = cpu_to_le32(paddr + MAX_DESC_BUF_SZ);
  431. }
  432. static inline void desc_set_buf_addr_and_size(struct xgmac_dma_desc *p,
  433. u32 paddr, int len)
  434. {
  435. desc_set_buf_len(p, len);
  436. desc_set_buf_addr(p, paddr, len);
  437. }
  438. static inline int desc_get_rx_frame_len(struct xgmac_dma_desc *p)
  439. {
  440. u32 data = le32_to_cpu(p->flags);
  441. u32 len = (data & RXDESC_FRAME_LEN_MASK) >> RXDESC_FRAME_LEN_OFFSET;
  442. if (data & RXDESC_FRAME_TYPE)
  443. len -= ETH_FCS_LEN;
  444. return len;
  445. }
  446. static void xgmac_dma_flush_tx_fifo(void __iomem *ioaddr)
  447. {
  448. int timeout = 1000;
  449. u32 reg = readl(ioaddr + XGMAC_OMR);
  450. writel(reg | XGMAC_OMR_FTF, ioaddr + XGMAC_OMR);
  451. while ((timeout-- > 0) && readl(ioaddr + XGMAC_OMR) & XGMAC_OMR_FTF)
  452. udelay(1);
  453. }
  454. static int desc_get_tx_status(struct xgmac_priv *priv, struct xgmac_dma_desc *p)
  455. {
  456. struct xgmac_extra_stats *x = &priv->xstats;
  457. u32 status = le32_to_cpu(p->flags);
  458. if (!(status & TXDESC_ERROR_SUMMARY))
  459. return 0;
  460. netdev_dbg(priv->dev, "tx desc error = 0x%08x\n", status);
  461. if (status & TXDESC_JABBER_TIMEOUT)
  462. x->tx_jabber++;
  463. if (status & TXDESC_FRAME_FLUSHED)
  464. x->tx_frame_flushed++;
  465. if (status & TXDESC_UNDERFLOW_ERR)
  466. xgmac_dma_flush_tx_fifo(priv->base);
  467. if (status & TXDESC_IP_HEADER_ERR)
  468. x->tx_ip_header_error++;
  469. if (status & TXDESC_LOCAL_FAULT)
  470. x->tx_local_fault++;
  471. if (status & TXDESC_REMOTE_FAULT)
  472. x->tx_remote_fault++;
  473. if (status & TXDESC_PAYLOAD_CSUM_ERR)
  474. x->tx_payload_error++;
  475. return -1;
  476. }
  477. static int desc_get_rx_status(struct xgmac_priv *priv, struct xgmac_dma_desc *p)
  478. {
  479. struct xgmac_extra_stats *x = &priv->xstats;
  480. int ret = CHECKSUM_UNNECESSARY;
  481. u32 status = le32_to_cpu(p->flags);
  482. u32 ext_status = le32_to_cpu(p->ext_status);
  483. if (status & RXDESC_DA_FILTER_FAIL) {
  484. netdev_dbg(priv->dev, "XGMAC RX : Dest Address filter fail\n");
  485. x->rx_da_filter_fail++;
  486. return -1;
  487. }
  488. /* All frames should fit into a single buffer */
  489. if (!(status & RXDESC_FIRST_SEG) || !(status & RXDESC_LAST_SEG))
  490. return -1;
  491. /* Check if packet has checksum already */
  492. if ((status & RXDESC_FRAME_TYPE) && (status & RXDESC_EXT_STATUS) &&
  493. !(ext_status & RXDESC_IP_PAYLOAD_MASK))
  494. ret = CHECKSUM_NONE;
  495. netdev_dbg(priv->dev, "rx status - frame type=%d, csum = %d, ext stat %08x\n",
  496. (status & RXDESC_FRAME_TYPE) ? 1 : 0, ret, ext_status);
  497. if (!(status & RXDESC_ERROR_SUMMARY))
  498. return ret;
  499. /* Handle any errors */
  500. if (status & (RXDESC_DESCRIPTOR_ERR | RXDESC_OVERFLOW_ERR |
  501. RXDESC_GIANT_FRAME | RXDESC_LENGTH_ERR | RXDESC_CRC_ERR))
  502. return -1;
  503. if (status & RXDESC_EXT_STATUS) {
  504. if (ext_status & RXDESC_IP_HEADER_ERR)
  505. x->rx_ip_header_error++;
  506. if (ext_status & RXDESC_IP_PAYLOAD_ERR)
  507. x->rx_payload_error++;
  508. netdev_dbg(priv->dev, "IP checksum error - stat %08x\n",
  509. ext_status);
  510. return CHECKSUM_NONE;
  511. }
  512. return ret;
  513. }
  514. static inline void xgmac_mac_enable(void __iomem *ioaddr)
  515. {
  516. u32 value = readl(ioaddr + XGMAC_CONTROL);
  517. value |= MAC_ENABLE_RX | MAC_ENABLE_TX;
  518. writel(value, ioaddr + XGMAC_CONTROL);
  519. value = readl(ioaddr + XGMAC_DMA_CONTROL);
  520. value |= DMA_CONTROL_ST | DMA_CONTROL_SR;
  521. writel(value, ioaddr + XGMAC_DMA_CONTROL);
  522. }
  523. static inline void xgmac_mac_disable(void __iomem *ioaddr)
  524. {
  525. u32 value = readl(ioaddr + XGMAC_DMA_CONTROL);
  526. value &= ~(DMA_CONTROL_ST | DMA_CONTROL_SR);
  527. writel(value, ioaddr + XGMAC_DMA_CONTROL);
  528. value = readl(ioaddr + XGMAC_CONTROL);
  529. value &= ~(MAC_ENABLE_TX | MAC_ENABLE_RX);
  530. writel(value, ioaddr + XGMAC_CONTROL);
  531. }
  532. static void xgmac_set_mac_addr(void __iomem *ioaddr, unsigned char *addr,
  533. int num)
  534. {
  535. u32 data;
  536. data = (addr[5] << 8) | addr[4] | (num ? XGMAC_ADDR_AE : 0);
  537. writel(data, ioaddr + XGMAC_ADDR_HIGH(num));
  538. data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  539. writel(data, ioaddr + XGMAC_ADDR_LOW(num));
  540. }
  541. static void xgmac_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
  542. int num)
  543. {
  544. u32 hi_addr, lo_addr;
  545. /* Read the MAC address from the hardware */
  546. hi_addr = readl(ioaddr + XGMAC_ADDR_HIGH(num));
  547. lo_addr = readl(ioaddr + XGMAC_ADDR_LOW(num));
  548. /* Extract the MAC address from the high and low words */
  549. addr[0] = lo_addr & 0xff;
  550. addr[1] = (lo_addr >> 8) & 0xff;
  551. addr[2] = (lo_addr >> 16) & 0xff;
  552. addr[3] = (lo_addr >> 24) & 0xff;
  553. addr[4] = hi_addr & 0xff;
  554. addr[5] = (hi_addr >> 8) & 0xff;
  555. }
  556. static int xgmac_set_flow_ctrl(struct xgmac_priv *priv, int rx, int tx)
  557. {
  558. u32 reg;
  559. unsigned int flow = 0;
  560. priv->rx_pause = rx;
  561. priv->tx_pause = tx;
  562. if (rx || tx) {
  563. if (rx)
  564. flow |= XGMAC_FLOW_CTRL_RFE;
  565. if (tx)
  566. flow |= XGMAC_FLOW_CTRL_TFE;
  567. flow |= XGMAC_FLOW_CTRL_PLT | XGMAC_FLOW_CTRL_UP;
  568. flow |= (PAUSE_TIME << XGMAC_FLOW_CTRL_PT_SHIFT);
  569. writel(flow, priv->base + XGMAC_FLOW_CTRL);
  570. reg = readl(priv->base + XGMAC_OMR);
  571. reg |= XGMAC_OMR_EFC;
  572. writel(reg, priv->base + XGMAC_OMR);
  573. } else {
  574. writel(0, priv->base + XGMAC_FLOW_CTRL);
  575. reg = readl(priv->base + XGMAC_OMR);
  576. reg &= ~XGMAC_OMR_EFC;
  577. writel(reg, priv->base + XGMAC_OMR);
  578. }
  579. return 0;
  580. }
  581. static void xgmac_rx_refill(struct xgmac_priv *priv)
  582. {
  583. struct xgmac_dma_desc *p;
  584. dma_addr_t paddr;
  585. int bufsz = priv->dev->mtu + ETH_HLEN + ETH_FCS_LEN;
  586. while (dma_ring_space(priv->rx_head, priv->rx_tail, DMA_RX_RING_SZ) > 1) {
  587. int entry = priv->rx_head;
  588. struct sk_buff *skb;
  589. p = priv->dma_rx + entry;
  590. if (priv->rx_skbuff[entry] == NULL) {
  591. skb = netdev_alloc_skb_ip_align(priv->dev, bufsz);
  592. if (unlikely(skb == NULL))
  593. break;
  594. priv->rx_skbuff[entry] = skb;
  595. paddr = dma_map_single(priv->device, skb->data,
  596. bufsz, DMA_FROM_DEVICE);
  597. desc_set_buf_addr(p, paddr, priv->dma_buf_sz);
  598. }
  599. netdev_dbg(priv->dev, "rx ring: head %d, tail %d\n",
  600. priv->rx_head, priv->rx_tail);
  601. priv->rx_head = dma_ring_incr(priv->rx_head, DMA_RX_RING_SZ);
  602. desc_set_rx_owner(p);
  603. }
  604. }
  605. /**
  606. * init_xgmac_dma_desc_rings - init the RX/TX descriptor rings
  607. * @dev: net device structure
  608. * Description: this function initializes the DMA RX/TX descriptors
  609. * and allocates the socket buffers.
  610. */
  611. static int xgmac_dma_desc_rings_init(struct net_device *dev)
  612. {
  613. struct xgmac_priv *priv = netdev_priv(dev);
  614. unsigned int bfsize;
  615. /* Set the Buffer size according to the MTU;
  616. * The total buffer size including any IP offset must be a multiple
  617. * of 8 bytes.
  618. */
  619. bfsize = ALIGN(dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN, 8);
  620. netdev_dbg(priv->dev, "mtu [%d] bfsize [%d]\n", dev->mtu, bfsize);
  621. priv->rx_skbuff = kzalloc(sizeof(struct sk_buff *) * DMA_RX_RING_SZ,
  622. GFP_KERNEL);
  623. if (!priv->rx_skbuff)
  624. return -ENOMEM;
  625. priv->dma_rx = dma_alloc_coherent(priv->device,
  626. DMA_RX_RING_SZ *
  627. sizeof(struct xgmac_dma_desc),
  628. &priv->dma_rx_phy,
  629. GFP_KERNEL);
  630. if (!priv->dma_rx)
  631. goto err_dma_rx;
  632. priv->tx_skbuff = kzalloc(sizeof(struct sk_buff *) * DMA_TX_RING_SZ,
  633. GFP_KERNEL);
  634. if (!priv->tx_skbuff)
  635. goto err_tx_skb;
  636. priv->dma_tx = dma_alloc_coherent(priv->device,
  637. DMA_TX_RING_SZ *
  638. sizeof(struct xgmac_dma_desc),
  639. &priv->dma_tx_phy,
  640. GFP_KERNEL);
  641. if (!priv->dma_tx)
  642. goto err_dma_tx;
  643. netdev_dbg(priv->dev, "DMA desc rings: virt addr (Rx %p, "
  644. "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
  645. priv->dma_rx, priv->dma_tx,
  646. (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
  647. priv->rx_tail = 0;
  648. priv->rx_head = 0;
  649. priv->dma_buf_sz = bfsize;
  650. desc_init_rx_desc(priv->dma_rx, DMA_RX_RING_SZ, priv->dma_buf_sz);
  651. xgmac_rx_refill(priv);
  652. priv->tx_tail = 0;
  653. priv->tx_head = 0;
  654. desc_init_tx_desc(priv->dma_tx, DMA_TX_RING_SZ);
  655. writel(priv->dma_tx_phy, priv->base + XGMAC_DMA_TX_BASE_ADDR);
  656. writel(priv->dma_rx_phy, priv->base + XGMAC_DMA_RX_BASE_ADDR);
  657. return 0;
  658. err_dma_tx:
  659. kfree(priv->tx_skbuff);
  660. err_tx_skb:
  661. dma_free_coherent(priv->device,
  662. DMA_RX_RING_SZ * sizeof(struct xgmac_dma_desc),
  663. priv->dma_rx, priv->dma_rx_phy);
  664. err_dma_rx:
  665. kfree(priv->rx_skbuff);
  666. return -ENOMEM;
  667. }
  668. static void xgmac_free_rx_skbufs(struct xgmac_priv *priv)
  669. {
  670. int i;
  671. struct xgmac_dma_desc *p;
  672. if (!priv->rx_skbuff)
  673. return;
  674. for (i = 0; i < DMA_RX_RING_SZ; i++) {
  675. if (priv->rx_skbuff[i] == NULL)
  676. continue;
  677. p = priv->dma_rx + i;
  678. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  679. priv->dma_buf_sz, DMA_FROM_DEVICE);
  680. dev_kfree_skb_any(priv->rx_skbuff[i]);
  681. priv->rx_skbuff[i] = NULL;
  682. }
  683. }
  684. static void xgmac_free_tx_skbufs(struct xgmac_priv *priv)
  685. {
  686. int i, f;
  687. struct xgmac_dma_desc *p;
  688. if (!priv->tx_skbuff)
  689. return;
  690. for (i = 0; i < DMA_TX_RING_SZ; i++) {
  691. if (priv->tx_skbuff[i] == NULL)
  692. continue;
  693. p = priv->dma_tx + i;
  694. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  695. desc_get_buf_len(p), DMA_TO_DEVICE);
  696. for (f = 0; f < skb_shinfo(priv->tx_skbuff[i])->nr_frags; f++) {
  697. p = priv->dma_tx + i++;
  698. dma_unmap_page(priv->device, desc_get_buf_addr(p),
  699. desc_get_buf_len(p), DMA_TO_DEVICE);
  700. }
  701. dev_kfree_skb_any(priv->tx_skbuff[i]);
  702. priv->tx_skbuff[i] = NULL;
  703. }
  704. }
  705. static void xgmac_free_dma_desc_rings(struct xgmac_priv *priv)
  706. {
  707. /* Release the DMA TX/RX socket buffers */
  708. xgmac_free_rx_skbufs(priv);
  709. xgmac_free_tx_skbufs(priv);
  710. /* Free the consistent memory allocated for descriptor rings */
  711. if (priv->dma_tx) {
  712. dma_free_coherent(priv->device,
  713. DMA_TX_RING_SZ * sizeof(struct xgmac_dma_desc),
  714. priv->dma_tx, priv->dma_tx_phy);
  715. priv->dma_tx = NULL;
  716. }
  717. if (priv->dma_rx) {
  718. dma_free_coherent(priv->device,
  719. DMA_RX_RING_SZ * sizeof(struct xgmac_dma_desc),
  720. priv->dma_rx, priv->dma_rx_phy);
  721. priv->dma_rx = NULL;
  722. }
  723. kfree(priv->rx_skbuff);
  724. priv->rx_skbuff = NULL;
  725. kfree(priv->tx_skbuff);
  726. priv->tx_skbuff = NULL;
  727. }
  728. /**
  729. * xgmac_tx:
  730. * @priv: private driver structure
  731. * Description: it reclaims resources after transmission completes.
  732. */
  733. static void xgmac_tx_complete(struct xgmac_priv *priv)
  734. {
  735. int i;
  736. while (dma_ring_cnt(priv->tx_head, priv->tx_tail, DMA_TX_RING_SZ)) {
  737. unsigned int entry = priv->tx_tail;
  738. struct sk_buff *skb = priv->tx_skbuff[entry];
  739. struct xgmac_dma_desc *p = priv->dma_tx + entry;
  740. /* Check if the descriptor is owned by the DMA. */
  741. if (desc_get_owner(p))
  742. break;
  743. /* Verify tx error by looking at the last segment */
  744. if (desc_get_tx_ls(p))
  745. desc_get_tx_status(priv, p);
  746. netdev_dbg(priv->dev, "tx ring: curr %d, dirty %d\n",
  747. priv->tx_head, priv->tx_tail);
  748. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  749. desc_get_buf_len(p), DMA_TO_DEVICE);
  750. priv->tx_skbuff[entry] = NULL;
  751. priv->tx_tail = dma_ring_incr(entry, DMA_TX_RING_SZ);
  752. if (!skb) {
  753. continue;
  754. }
  755. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  756. entry = priv->tx_tail = dma_ring_incr(priv->tx_tail,
  757. DMA_TX_RING_SZ);
  758. p = priv->dma_tx + priv->tx_tail;
  759. dma_unmap_page(priv->device, desc_get_buf_addr(p),
  760. desc_get_buf_len(p), DMA_TO_DEVICE);
  761. }
  762. dev_kfree_skb(skb);
  763. }
  764. if (dma_ring_space(priv->tx_head, priv->tx_tail, DMA_TX_RING_SZ) >
  765. MAX_SKB_FRAGS)
  766. netif_wake_queue(priv->dev);
  767. }
  768. /**
  769. * xgmac_tx_err:
  770. * @priv: pointer to the private device structure
  771. * Description: it cleans the descriptors and restarts the transmission
  772. * in case of errors.
  773. */
  774. static void xgmac_tx_err(struct xgmac_priv *priv)
  775. {
  776. u32 reg, value, inten;
  777. netif_stop_queue(priv->dev);
  778. inten = readl(priv->base + XGMAC_DMA_INTR_ENA);
  779. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  780. reg = readl(priv->base + XGMAC_DMA_CONTROL);
  781. writel(reg & ~DMA_CONTROL_ST, priv->base + XGMAC_DMA_CONTROL);
  782. do {
  783. value = readl(priv->base + XGMAC_DMA_STATUS) & 0x700000;
  784. } while (value && (value != 0x600000));
  785. xgmac_free_tx_skbufs(priv);
  786. desc_init_tx_desc(priv->dma_tx, DMA_TX_RING_SZ);
  787. priv->tx_tail = 0;
  788. priv->tx_head = 0;
  789. writel(priv->dma_tx_phy, priv->base + XGMAC_DMA_TX_BASE_ADDR);
  790. writel(reg | DMA_CONTROL_ST, priv->base + XGMAC_DMA_CONTROL);
  791. writel(DMA_STATUS_TU | DMA_STATUS_TPS | DMA_STATUS_NIS | DMA_STATUS_AIS,
  792. priv->base + XGMAC_DMA_STATUS);
  793. writel(inten, priv->base + XGMAC_DMA_INTR_ENA);
  794. netif_wake_queue(priv->dev);
  795. }
  796. static int xgmac_hw_init(struct net_device *dev)
  797. {
  798. u32 value, ctrl;
  799. int limit;
  800. struct xgmac_priv *priv = netdev_priv(dev);
  801. void __iomem *ioaddr = priv->base;
  802. /* Save the ctrl register value */
  803. ctrl = readl(ioaddr + XGMAC_CONTROL) & XGMAC_CONTROL_SPD_MASK;
  804. /* SW reset */
  805. value = DMA_BUS_MODE_SFT_RESET;
  806. writel(value, ioaddr + XGMAC_DMA_BUS_MODE);
  807. limit = 15000;
  808. while (limit-- &&
  809. (readl(ioaddr + XGMAC_DMA_BUS_MODE) & DMA_BUS_MODE_SFT_RESET))
  810. cpu_relax();
  811. if (limit < 0)
  812. return -EBUSY;
  813. value = (0x10 << DMA_BUS_MODE_PBL_SHIFT) |
  814. (0x10 << DMA_BUS_MODE_RPBL_SHIFT) |
  815. DMA_BUS_MODE_FB | DMA_BUS_MODE_ATDS | DMA_BUS_MODE_AAL;
  816. writel(value, ioaddr + XGMAC_DMA_BUS_MODE);
  817. /* Enable interrupts */
  818. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_STATUS);
  819. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_INTR_ENA);
  820. /* Mask power mgt interrupt */
  821. writel(XGMAC_INT_STAT_PMTIM, ioaddr + XGMAC_INT_STAT);
  822. /* XGMAC requires AXI bus init. This is a 'magic number' for now */
  823. writel(0x0077000E, ioaddr + XGMAC_DMA_AXI_BUS);
  824. ctrl |= XGMAC_CONTROL_DDIC | XGMAC_CONTROL_JE | XGMAC_CONTROL_ACS |
  825. XGMAC_CONTROL_CAR;
  826. if (dev->features & NETIF_F_RXCSUM)
  827. ctrl |= XGMAC_CONTROL_IPC;
  828. writel(ctrl, ioaddr + XGMAC_CONTROL);
  829. writel(DMA_CONTROL_OSF, ioaddr + XGMAC_DMA_CONTROL);
  830. /* Set the HW DMA mode and the COE */
  831. writel(XGMAC_OMR_TSF | XGMAC_OMR_RFD | XGMAC_OMR_RFA |
  832. XGMAC_OMR_RTC_256,
  833. ioaddr + XGMAC_OMR);
  834. /* Reset the MMC counters */
  835. writel(1, ioaddr + XGMAC_MMC_CTRL);
  836. return 0;
  837. }
  838. /**
  839. * xgmac_open - open entry point of the driver
  840. * @dev : pointer to the device structure.
  841. * Description:
  842. * This function is the open entry point of the driver.
  843. * Return value:
  844. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  845. * file on failure.
  846. */
  847. static int xgmac_open(struct net_device *dev)
  848. {
  849. int ret;
  850. struct xgmac_priv *priv = netdev_priv(dev);
  851. void __iomem *ioaddr = priv->base;
  852. /* Check that the MAC address is valid. If its not, refuse
  853. * to bring the device up. The user must specify an
  854. * address using the following linux command:
  855. * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
  856. if (!is_valid_ether_addr(dev->dev_addr)) {
  857. eth_hw_addr_random(dev);
  858. netdev_dbg(priv->dev, "generated random MAC address %pM\n",
  859. dev->dev_addr);
  860. }
  861. memset(&priv->xstats, 0, sizeof(struct xgmac_extra_stats));
  862. /* Initialize the XGMAC and descriptors */
  863. xgmac_hw_init(dev);
  864. xgmac_set_mac_addr(ioaddr, dev->dev_addr, 0);
  865. xgmac_set_flow_ctrl(priv, priv->rx_pause, priv->tx_pause);
  866. ret = xgmac_dma_desc_rings_init(dev);
  867. if (ret < 0)
  868. return ret;
  869. /* Enable the MAC Rx/Tx */
  870. xgmac_mac_enable(ioaddr);
  871. napi_enable(&priv->napi);
  872. netif_start_queue(dev);
  873. return 0;
  874. }
  875. /**
  876. * xgmac_release - close entry point of the driver
  877. * @dev : device pointer.
  878. * Description:
  879. * This is the stop entry point of the driver.
  880. */
  881. static int xgmac_stop(struct net_device *dev)
  882. {
  883. struct xgmac_priv *priv = netdev_priv(dev);
  884. netif_stop_queue(dev);
  885. if (readl(priv->base + XGMAC_DMA_INTR_ENA))
  886. napi_disable(&priv->napi);
  887. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  888. /* Disable the MAC core */
  889. xgmac_mac_disable(priv->base);
  890. /* Release and free the Rx/Tx resources */
  891. xgmac_free_dma_desc_rings(priv);
  892. return 0;
  893. }
  894. /**
  895. * xgmac_xmit:
  896. * @skb : the socket buffer
  897. * @dev : device pointer
  898. * Description : Tx entry point of the driver.
  899. */
  900. static netdev_tx_t xgmac_xmit(struct sk_buff *skb, struct net_device *dev)
  901. {
  902. struct xgmac_priv *priv = netdev_priv(dev);
  903. unsigned int entry;
  904. int i;
  905. u32 irq_flag;
  906. int nfrags = skb_shinfo(skb)->nr_frags;
  907. struct xgmac_dma_desc *desc, *first;
  908. unsigned int desc_flags;
  909. unsigned int len;
  910. dma_addr_t paddr;
  911. priv->tx_irq_cnt = (priv->tx_irq_cnt + 1) & (DMA_TX_RING_SZ/4 - 1);
  912. irq_flag = priv->tx_irq_cnt ? 0 : TXDESC_INTERRUPT;
  913. desc_flags = (skb->ip_summed == CHECKSUM_PARTIAL) ?
  914. TXDESC_CSUM_ALL : 0;
  915. entry = priv->tx_head;
  916. desc = priv->dma_tx + entry;
  917. first = desc;
  918. len = skb_headlen(skb);
  919. paddr = dma_map_single(priv->device, skb->data, len, DMA_TO_DEVICE);
  920. if (dma_mapping_error(priv->device, paddr)) {
  921. dev_kfree_skb(skb);
  922. return -EIO;
  923. }
  924. priv->tx_skbuff[entry] = skb;
  925. desc_set_buf_addr_and_size(desc, paddr, len);
  926. for (i = 0; i < nfrags; i++) {
  927. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  928. len = frag->size;
  929. paddr = skb_frag_dma_map(priv->device, frag, 0, len,
  930. DMA_TO_DEVICE);
  931. if (dma_mapping_error(priv->device, paddr)) {
  932. dev_kfree_skb(skb);
  933. return -EIO;
  934. }
  935. entry = dma_ring_incr(entry, DMA_TX_RING_SZ);
  936. desc = priv->dma_tx + entry;
  937. priv->tx_skbuff[entry] = NULL;
  938. desc_set_buf_addr_and_size(desc, paddr, len);
  939. if (i < (nfrags - 1))
  940. desc_set_tx_owner(desc, desc_flags);
  941. }
  942. /* Interrupt on completition only for the latest segment */
  943. if (desc != first)
  944. desc_set_tx_owner(desc, desc_flags |
  945. TXDESC_LAST_SEG | irq_flag);
  946. else
  947. desc_flags |= TXDESC_LAST_SEG | irq_flag;
  948. /* Set owner on first desc last to avoid race condition */
  949. wmb();
  950. desc_set_tx_owner(first, desc_flags | TXDESC_FIRST_SEG);
  951. priv->tx_head = dma_ring_incr(entry, DMA_TX_RING_SZ);
  952. writel(1, priv->base + XGMAC_DMA_TX_POLL);
  953. if (dma_ring_space(priv->tx_head, priv->tx_tail, DMA_TX_RING_SZ) <
  954. MAX_SKB_FRAGS)
  955. netif_stop_queue(dev);
  956. return NETDEV_TX_OK;
  957. }
  958. static int xgmac_rx(struct xgmac_priv *priv, int limit)
  959. {
  960. unsigned int entry;
  961. unsigned int count = 0;
  962. struct xgmac_dma_desc *p;
  963. while (count < limit) {
  964. int ip_checksum;
  965. struct sk_buff *skb;
  966. int frame_len;
  967. if (!dma_ring_cnt(priv->rx_head, priv->rx_tail, DMA_RX_RING_SZ))
  968. break;
  969. entry = priv->rx_tail;
  970. p = priv->dma_rx + entry;
  971. if (desc_get_owner(p))
  972. break;
  973. count++;
  974. priv->rx_tail = dma_ring_incr(priv->rx_tail, DMA_RX_RING_SZ);
  975. /* read the status of the incoming frame */
  976. ip_checksum = desc_get_rx_status(priv, p);
  977. if (ip_checksum < 0)
  978. continue;
  979. skb = priv->rx_skbuff[entry];
  980. if (unlikely(!skb)) {
  981. netdev_err(priv->dev, "Inconsistent Rx descriptor chain\n");
  982. break;
  983. }
  984. priv->rx_skbuff[entry] = NULL;
  985. frame_len = desc_get_rx_frame_len(p);
  986. netdev_dbg(priv->dev, "RX frame size %d, COE status: %d\n",
  987. frame_len, ip_checksum);
  988. skb_put(skb, frame_len);
  989. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  990. frame_len, DMA_FROM_DEVICE);
  991. skb->protocol = eth_type_trans(skb, priv->dev);
  992. skb->ip_summed = ip_checksum;
  993. if (ip_checksum == CHECKSUM_NONE)
  994. netif_receive_skb(skb);
  995. else
  996. napi_gro_receive(&priv->napi, skb);
  997. }
  998. xgmac_rx_refill(priv);
  999. return count;
  1000. }
  1001. /**
  1002. * xgmac_poll - xgmac poll method (NAPI)
  1003. * @napi : pointer to the napi structure.
  1004. * @budget : maximum number of packets that the current CPU can receive from
  1005. * all interfaces.
  1006. * Description :
  1007. * This function implements the the reception process.
  1008. * Also it runs the TX completion thread
  1009. */
  1010. static int xgmac_poll(struct napi_struct *napi, int budget)
  1011. {
  1012. struct xgmac_priv *priv = container_of(napi,
  1013. struct xgmac_priv, napi);
  1014. int work_done = 0;
  1015. xgmac_tx_complete(priv);
  1016. work_done = xgmac_rx(priv, budget);
  1017. if (work_done < budget) {
  1018. napi_complete(napi);
  1019. __raw_writel(DMA_INTR_DEFAULT_MASK, priv->base + XGMAC_DMA_INTR_ENA);
  1020. }
  1021. return work_done;
  1022. }
  1023. /**
  1024. * xgmac_tx_timeout
  1025. * @dev : Pointer to net device structure
  1026. * Description: this function is called when a packet transmission fails to
  1027. * complete within a reasonable tmrate. The driver will mark the error in the
  1028. * netdev structure and arrange for the device to be reset to a sane state
  1029. * in order to transmit a new packet.
  1030. */
  1031. static void xgmac_tx_timeout(struct net_device *dev)
  1032. {
  1033. struct xgmac_priv *priv = netdev_priv(dev);
  1034. /* Clear Tx resources and restart transmitting again */
  1035. xgmac_tx_err(priv);
  1036. }
  1037. /**
  1038. * xgmac_set_rx_mode - entry point for multicast addressing
  1039. * @dev : pointer to the device structure
  1040. * Description:
  1041. * This function is a driver entry point which gets called by the kernel
  1042. * whenever multicast addresses must be enabled/disabled.
  1043. * Return value:
  1044. * void.
  1045. */
  1046. static void xgmac_set_rx_mode(struct net_device *dev)
  1047. {
  1048. int i;
  1049. struct xgmac_priv *priv = netdev_priv(dev);
  1050. void __iomem *ioaddr = priv->base;
  1051. unsigned int value = 0;
  1052. u32 hash_filter[XGMAC_NUM_HASH];
  1053. int reg = 1;
  1054. struct netdev_hw_addr *ha;
  1055. bool use_hash = false;
  1056. netdev_dbg(priv->dev, "# mcasts %d, # unicast %d\n",
  1057. netdev_mc_count(dev), netdev_uc_count(dev));
  1058. if (dev->flags & IFF_PROMISC) {
  1059. writel(XGMAC_FRAME_FILTER_PR, ioaddr + XGMAC_FRAME_FILTER);
  1060. return;
  1061. }
  1062. memset(hash_filter, 0, sizeof(hash_filter));
  1063. if (netdev_uc_count(dev) > XGMAC_MAX_FILTER_ADDR) {
  1064. use_hash = true;
  1065. value |= XGMAC_FRAME_FILTER_HUC | XGMAC_FRAME_FILTER_HPF;
  1066. }
  1067. netdev_for_each_uc_addr(ha, dev) {
  1068. if (use_hash) {
  1069. u32 bit_nr = ~ether_crc(ETH_ALEN, ha->addr) >> 23;
  1070. /* The most significant 4 bits determine the register to
  1071. * use (H/L) while the other 5 bits determine the bit
  1072. * within the register. */
  1073. hash_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1074. } else {
  1075. xgmac_set_mac_addr(ioaddr, ha->addr, reg);
  1076. reg++;
  1077. }
  1078. }
  1079. if (dev->flags & IFF_ALLMULTI) {
  1080. value |= XGMAC_FRAME_FILTER_PM;
  1081. goto out;
  1082. }
  1083. if ((netdev_mc_count(dev) + reg - 1) > XGMAC_MAX_FILTER_ADDR) {
  1084. use_hash = true;
  1085. value |= XGMAC_FRAME_FILTER_HMC | XGMAC_FRAME_FILTER_HPF;
  1086. }
  1087. netdev_for_each_mc_addr(ha, dev) {
  1088. if (use_hash) {
  1089. u32 bit_nr = ~ether_crc(ETH_ALEN, ha->addr) >> 23;
  1090. /* The most significant 4 bits determine the register to
  1091. * use (H/L) while the other 5 bits determine the bit
  1092. * within the register. */
  1093. hash_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1094. } else {
  1095. xgmac_set_mac_addr(ioaddr, ha->addr, reg);
  1096. reg++;
  1097. }
  1098. }
  1099. out:
  1100. for (i = 0; i < XGMAC_NUM_HASH; i++)
  1101. writel(hash_filter[i], ioaddr + XGMAC_HASH(i));
  1102. writel(value, ioaddr + XGMAC_FRAME_FILTER);
  1103. }
  1104. /**
  1105. * xgmac_change_mtu - entry point to change MTU size for the device.
  1106. * @dev : device pointer.
  1107. * @new_mtu : the new MTU size for the device.
  1108. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  1109. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  1110. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  1111. * Return value:
  1112. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1113. * file on failure.
  1114. */
  1115. static int xgmac_change_mtu(struct net_device *dev, int new_mtu)
  1116. {
  1117. struct xgmac_priv *priv = netdev_priv(dev);
  1118. int old_mtu;
  1119. if ((new_mtu < 46) || (new_mtu > MAX_MTU)) {
  1120. netdev_err(priv->dev, "invalid MTU, max MTU is: %d\n", MAX_MTU);
  1121. return -EINVAL;
  1122. }
  1123. old_mtu = dev->mtu;
  1124. dev->mtu = new_mtu;
  1125. /* return early if the buffer sizes will not change */
  1126. if (old_mtu <= ETH_DATA_LEN && new_mtu <= ETH_DATA_LEN)
  1127. return 0;
  1128. if (old_mtu == new_mtu)
  1129. return 0;
  1130. /* Stop everything, get ready to change the MTU */
  1131. if (!netif_running(dev))
  1132. return 0;
  1133. /* Bring the interface down and then back up */
  1134. xgmac_stop(dev);
  1135. return xgmac_open(dev);
  1136. }
  1137. static irqreturn_t xgmac_pmt_interrupt(int irq, void *dev_id)
  1138. {
  1139. u32 intr_status;
  1140. struct net_device *dev = (struct net_device *)dev_id;
  1141. struct xgmac_priv *priv = netdev_priv(dev);
  1142. void __iomem *ioaddr = priv->base;
  1143. intr_status = __raw_readl(ioaddr + XGMAC_INT_STAT);
  1144. if (intr_status & XGMAC_INT_STAT_PMT) {
  1145. netdev_dbg(priv->dev, "received Magic frame\n");
  1146. /* clear the PMT bits 5 and 6 by reading the PMT */
  1147. readl(ioaddr + XGMAC_PMT);
  1148. }
  1149. return IRQ_HANDLED;
  1150. }
  1151. static irqreturn_t xgmac_interrupt(int irq, void *dev_id)
  1152. {
  1153. u32 intr_status;
  1154. bool tx_err = false;
  1155. struct net_device *dev = (struct net_device *)dev_id;
  1156. struct xgmac_priv *priv = netdev_priv(dev);
  1157. struct xgmac_extra_stats *x = &priv->xstats;
  1158. /* read the status register (CSR5) */
  1159. intr_status = __raw_readl(priv->base + XGMAC_DMA_STATUS);
  1160. intr_status &= __raw_readl(priv->base + XGMAC_DMA_INTR_ENA);
  1161. __raw_writel(intr_status, priv->base + XGMAC_DMA_STATUS);
  1162. /* It displays the DMA process states (CSR5 register) */
  1163. /* ABNORMAL interrupts */
  1164. if (unlikely(intr_status & DMA_STATUS_AIS)) {
  1165. if (intr_status & DMA_STATUS_TJT) {
  1166. netdev_err(priv->dev, "transmit jabber\n");
  1167. x->tx_jabber++;
  1168. }
  1169. if (intr_status & DMA_STATUS_RU)
  1170. x->rx_buf_unav++;
  1171. if (intr_status & DMA_STATUS_RPS) {
  1172. netdev_err(priv->dev, "receive process stopped\n");
  1173. x->rx_process_stopped++;
  1174. }
  1175. if (intr_status & DMA_STATUS_ETI) {
  1176. netdev_err(priv->dev, "transmit early interrupt\n");
  1177. x->tx_early++;
  1178. }
  1179. if (intr_status & DMA_STATUS_TPS) {
  1180. netdev_err(priv->dev, "transmit process stopped\n");
  1181. x->tx_process_stopped++;
  1182. tx_err = true;
  1183. }
  1184. if (intr_status & DMA_STATUS_FBI) {
  1185. netdev_err(priv->dev, "fatal bus error\n");
  1186. x->fatal_bus_error++;
  1187. tx_err = true;
  1188. }
  1189. if (tx_err)
  1190. xgmac_tx_err(priv);
  1191. }
  1192. /* TX/RX NORMAL interrupts */
  1193. if (intr_status & (DMA_STATUS_RI | DMA_STATUS_TU | DMA_STATUS_TI)) {
  1194. __raw_writel(DMA_INTR_ABNORMAL, priv->base + XGMAC_DMA_INTR_ENA);
  1195. napi_schedule(&priv->napi);
  1196. }
  1197. return IRQ_HANDLED;
  1198. }
  1199. #ifdef CONFIG_NET_POLL_CONTROLLER
  1200. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  1201. * to allow network I/O with interrupts disabled. */
  1202. static void xgmac_poll_controller(struct net_device *dev)
  1203. {
  1204. disable_irq(dev->irq);
  1205. xgmac_interrupt(dev->irq, dev);
  1206. enable_irq(dev->irq);
  1207. }
  1208. #endif
  1209. static struct rtnl_link_stats64 *
  1210. xgmac_get_stats64(struct net_device *dev,
  1211. struct rtnl_link_stats64 *storage)
  1212. {
  1213. struct xgmac_priv *priv = netdev_priv(dev);
  1214. void __iomem *base = priv->base;
  1215. u32 count;
  1216. spin_lock_bh(&priv->stats_lock);
  1217. writel(XGMAC_MMC_CTRL_CNT_FRZ, base + XGMAC_MMC_CTRL);
  1218. storage->rx_bytes = readl(base + XGMAC_MMC_RXOCTET_G_LO);
  1219. storage->rx_bytes |= (u64)(readl(base + XGMAC_MMC_RXOCTET_G_HI)) << 32;
  1220. storage->rx_packets = readl(base + XGMAC_MMC_RXFRAME_GB_LO);
  1221. storage->multicast = readl(base + XGMAC_MMC_RXMCFRAME_G);
  1222. storage->rx_crc_errors = readl(base + XGMAC_MMC_RXCRCERR);
  1223. storage->rx_length_errors = readl(base + XGMAC_MMC_RXLENGTHERR);
  1224. storage->rx_missed_errors = readl(base + XGMAC_MMC_RXOVERFLOW);
  1225. storage->tx_bytes = readl(base + XGMAC_MMC_TXOCTET_G_LO);
  1226. storage->tx_bytes |= (u64)(readl(base + XGMAC_MMC_TXOCTET_G_HI)) << 32;
  1227. count = readl(base + XGMAC_MMC_TXFRAME_GB_LO);
  1228. storage->tx_errors = count - readl(base + XGMAC_MMC_TXFRAME_G_LO);
  1229. storage->tx_packets = count;
  1230. storage->tx_fifo_errors = readl(base + XGMAC_MMC_TXUNDERFLOW);
  1231. writel(0, base + XGMAC_MMC_CTRL);
  1232. spin_unlock_bh(&priv->stats_lock);
  1233. return storage;
  1234. }
  1235. static int xgmac_set_mac_address(struct net_device *dev, void *p)
  1236. {
  1237. struct xgmac_priv *priv = netdev_priv(dev);
  1238. void __iomem *ioaddr = priv->base;
  1239. struct sockaddr *addr = p;
  1240. if (!is_valid_ether_addr(addr->sa_data))
  1241. return -EADDRNOTAVAIL;
  1242. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1243. xgmac_set_mac_addr(ioaddr, dev->dev_addr, 0);
  1244. return 0;
  1245. }
  1246. static int xgmac_set_features(struct net_device *dev, netdev_features_t features)
  1247. {
  1248. u32 ctrl;
  1249. struct xgmac_priv *priv = netdev_priv(dev);
  1250. void __iomem *ioaddr = priv->base;
  1251. u32 changed = dev->features ^ features;
  1252. if (!(changed & NETIF_F_RXCSUM))
  1253. return 0;
  1254. ctrl = readl(ioaddr + XGMAC_CONTROL);
  1255. if (features & NETIF_F_RXCSUM)
  1256. ctrl |= XGMAC_CONTROL_IPC;
  1257. else
  1258. ctrl &= ~XGMAC_CONTROL_IPC;
  1259. writel(ctrl, ioaddr + XGMAC_CONTROL);
  1260. return 0;
  1261. }
  1262. static const struct net_device_ops xgmac_netdev_ops = {
  1263. .ndo_open = xgmac_open,
  1264. .ndo_start_xmit = xgmac_xmit,
  1265. .ndo_stop = xgmac_stop,
  1266. .ndo_change_mtu = xgmac_change_mtu,
  1267. .ndo_set_rx_mode = xgmac_set_rx_mode,
  1268. .ndo_tx_timeout = xgmac_tx_timeout,
  1269. .ndo_get_stats64 = xgmac_get_stats64,
  1270. #ifdef CONFIG_NET_POLL_CONTROLLER
  1271. .ndo_poll_controller = xgmac_poll_controller,
  1272. #endif
  1273. .ndo_set_mac_address = xgmac_set_mac_address,
  1274. .ndo_set_features = xgmac_set_features,
  1275. };
  1276. static int xgmac_ethtool_getsettings(struct net_device *dev,
  1277. struct ethtool_cmd *cmd)
  1278. {
  1279. cmd->autoneg = 0;
  1280. cmd->duplex = DUPLEX_FULL;
  1281. ethtool_cmd_speed_set(cmd, 10000);
  1282. cmd->supported = 0;
  1283. cmd->advertising = 0;
  1284. cmd->transceiver = XCVR_INTERNAL;
  1285. return 0;
  1286. }
  1287. static void xgmac_get_pauseparam(struct net_device *netdev,
  1288. struct ethtool_pauseparam *pause)
  1289. {
  1290. struct xgmac_priv *priv = netdev_priv(netdev);
  1291. pause->rx_pause = priv->rx_pause;
  1292. pause->tx_pause = priv->tx_pause;
  1293. }
  1294. static int xgmac_set_pauseparam(struct net_device *netdev,
  1295. struct ethtool_pauseparam *pause)
  1296. {
  1297. struct xgmac_priv *priv = netdev_priv(netdev);
  1298. if (pause->autoneg)
  1299. return -EINVAL;
  1300. return xgmac_set_flow_ctrl(priv, pause->rx_pause, pause->tx_pause);
  1301. }
  1302. struct xgmac_stats {
  1303. char stat_string[ETH_GSTRING_LEN];
  1304. int stat_offset;
  1305. bool is_reg;
  1306. };
  1307. #define XGMAC_STAT(m) \
  1308. { #m, offsetof(struct xgmac_priv, xstats.m), false }
  1309. #define XGMAC_HW_STAT(m, reg_offset) \
  1310. { #m, reg_offset, true }
  1311. static const struct xgmac_stats xgmac_gstrings_stats[] = {
  1312. XGMAC_STAT(tx_frame_flushed),
  1313. XGMAC_STAT(tx_payload_error),
  1314. XGMAC_STAT(tx_ip_header_error),
  1315. XGMAC_STAT(tx_local_fault),
  1316. XGMAC_STAT(tx_remote_fault),
  1317. XGMAC_STAT(tx_early),
  1318. XGMAC_STAT(tx_process_stopped),
  1319. XGMAC_STAT(tx_jabber),
  1320. XGMAC_STAT(rx_buf_unav),
  1321. XGMAC_STAT(rx_process_stopped),
  1322. XGMAC_STAT(rx_payload_error),
  1323. XGMAC_STAT(rx_ip_header_error),
  1324. XGMAC_STAT(rx_da_filter_fail),
  1325. XGMAC_STAT(rx_sa_filter_fail),
  1326. XGMAC_STAT(fatal_bus_error),
  1327. XGMAC_HW_STAT(rx_watchdog, XGMAC_MMC_RXWATCHDOG),
  1328. XGMAC_HW_STAT(tx_vlan, XGMAC_MMC_TXVLANFRAME),
  1329. XGMAC_HW_STAT(rx_vlan, XGMAC_MMC_RXVLANFRAME),
  1330. XGMAC_HW_STAT(tx_pause, XGMAC_MMC_TXPAUSEFRAME),
  1331. XGMAC_HW_STAT(rx_pause, XGMAC_MMC_RXPAUSEFRAME),
  1332. };
  1333. #define XGMAC_STATS_LEN ARRAY_SIZE(xgmac_gstrings_stats)
  1334. static void xgmac_get_ethtool_stats(struct net_device *dev,
  1335. struct ethtool_stats *dummy,
  1336. u64 *data)
  1337. {
  1338. struct xgmac_priv *priv = netdev_priv(dev);
  1339. void *p = priv;
  1340. int i;
  1341. for (i = 0; i < XGMAC_STATS_LEN; i++) {
  1342. if (xgmac_gstrings_stats[i].is_reg)
  1343. *data++ = readl(priv->base +
  1344. xgmac_gstrings_stats[i].stat_offset);
  1345. else
  1346. *data++ = *(u32 *)(p +
  1347. xgmac_gstrings_stats[i].stat_offset);
  1348. }
  1349. }
  1350. static int xgmac_get_sset_count(struct net_device *netdev, int sset)
  1351. {
  1352. switch (sset) {
  1353. case ETH_SS_STATS:
  1354. return XGMAC_STATS_LEN;
  1355. default:
  1356. return -EINVAL;
  1357. }
  1358. }
  1359. static void xgmac_get_strings(struct net_device *dev, u32 stringset,
  1360. u8 *data)
  1361. {
  1362. int i;
  1363. u8 *p = data;
  1364. switch (stringset) {
  1365. case ETH_SS_STATS:
  1366. for (i = 0; i < XGMAC_STATS_LEN; i++) {
  1367. memcpy(p, xgmac_gstrings_stats[i].stat_string,
  1368. ETH_GSTRING_LEN);
  1369. p += ETH_GSTRING_LEN;
  1370. }
  1371. break;
  1372. default:
  1373. WARN_ON(1);
  1374. break;
  1375. }
  1376. }
  1377. static void xgmac_get_wol(struct net_device *dev,
  1378. struct ethtool_wolinfo *wol)
  1379. {
  1380. struct xgmac_priv *priv = netdev_priv(dev);
  1381. if (device_can_wakeup(priv->device)) {
  1382. wol->supported = WAKE_MAGIC | WAKE_UCAST;
  1383. wol->wolopts = priv->wolopts;
  1384. }
  1385. }
  1386. static int xgmac_set_wol(struct net_device *dev,
  1387. struct ethtool_wolinfo *wol)
  1388. {
  1389. struct xgmac_priv *priv = netdev_priv(dev);
  1390. u32 support = WAKE_MAGIC | WAKE_UCAST;
  1391. if (!device_can_wakeup(priv->device))
  1392. return -ENOTSUPP;
  1393. if (wol->wolopts & ~support)
  1394. return -EINVAL;
  1395. priv->wolopts = wol->wolopts;
  1396. if (wol->wolopts) {
  1397. device_set_wakeup_enable(priv->device, 1);
  1398. enable_irq_wake(dev->irq);
  1399. } else {
  1400. device_set_wakeup_enable(priv->device, 0);
  1401. disable_irq_wake(dev->irq);
  1402. }
  1403. return 0;
  1404. }
  1405. static const struct ethtool_ops xgmac_ethtool_ops = {
  1406. .get_settings = xgmac_ethtool_getsettings,
  1407. .get_link = ethtool_op_get_link,
  1408. .get_pauseparam = xgmac_get_pauseparam,
  1409. .set_pauseparam = xgmac_set_pauseparam,
  1410. .get_ethtool_stats = xgmac_get_ethtool_stats,
  1411. .get_strings = xgmac_get_strings,
  1412. .get_wol = xgmac_get_wol,
  1413. .set_wol = xgmac_set_wol,
  1414. .get_sset_count = xgmac_get_sset_count,
  1415. };
  1416. /**
  1417. * xgmac_probe
  1418. * @pdev: platform device pointer
  1419. * Description: the driver is initialized through platform_device.
  1420. */
  1421. static int xgmac_probe(struct platform_device *pdev)
  1422. {
  1423. int ret = 0;
  1424. struct resource *res;
  1425. struct net_device *ndev = NULL;
  1426. struct xgmac_priv *priv = NULL;
  1427. u32 uid;
  1428. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1429. if (!res)
  1430. return -ENODEV;
  1431. if (!request_mem_region(res->start, resource_size(res), pdev->name))
  1432. return -EBUSY;
  1433. ndev = alloc_etherdev(sizeof(struct xgmac_priv));
  1434. if (!ndev) {
  1435. ret = -ENOMEM;
  1436. goto err_alloc;
  1437. }
  1438. SET_NETDEV_DEV(ndev, &pdev->dev);
  1439. priv = netdev_priv(ndev);
  1440. platform_set_drvdata(pdev, ndev);
  1441. ether_setup(ndev);
  1442. ndev->netdev_ops = &xgmac_netdev_ops;
  1443. SET_ETHTOOL_OPS(ndev, &xgmac_ethtool_ops);
  1444. spin_lock_init(&priv->stats_lock);
  1445. priv->device = &pdev->dev;
  1446. priv->dev = ndev;
  1447. priv->rx_pause = 1;
  1448. priv->tx_pause = 1;
  1449. priv->base = ioremap(res->start, resource_size(res));
  1450. if (!priv->base) {
  1451. netdev_err(ndev, "ioremap failed\n");
  1452. ret = -ENOMEM;
  1453. goto err_io;
  1454. }
  1455. uid = readl(priv->base + XGMAC_VERSION);
  1456. netdev_info(ndev, "h/w version is 0x%x\n", uid);
  1457. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  1458. ndev->irq = platform_get_irq(pdev, 0);
  1459. if (ndev->irq == -ENXIO) {
  1460. netdev_err(ndev, "No irq resource\n");
  1461. ret = ndev->irq;
  1462. goto err_irq;
  1463. }
  1464. ret = request_irq(ndev->irq, xgmac_interrupt, 0,
  1465. dev_name(&pdev->dev), ndev);
  1466. if (ret < 0) {
  1467. netdev_err(ndev, "Could not request irq %d - ret %d)\n",
  1468. ndev->irq, ret);
  1469. goto err_irq;
  1470. }
  1471. priv->pmt_irq = platform_get_irq(pdev, 1);
  1472. if (priv->pmt_irq == -ENXIO) {
  1473. netdev_err(ndev, "No pmt irq resource\n");
  1474. ret = priv->pmt_irq;
  1475. goto err_pmt_irq;
  1476. }
  1477. ret = request_irq(priv->pmt_irq, xgmac_pmt_interrupt, 0,
  1478. dev_name(&pdev->dev), ndev);
  1479. if (ret < 0) {
  1480. netdev_err(ndev, "Could not request irq %d - ret %d)\n",
  1481. priv->pmt_irq, ret);
  1482. goto err_pmt_irq;
  1483. }
  1484. device_set_wakeup_capable(&pdev->dev, 1);
  1485. if (device_can_wakeup(priv->device))
  1486. priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
  1487. ndev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA;
  1488. if (readl(priv->base + XGMAC_DMA_HW_FEATURE) & DMA_HW_FEAT_TXCOESEL)
  1489. ndev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1490. NETIF_F_RXCSUM;
  1491. ndev->features |= ndev->hw_features;
  1492. ndev->priv_flags |= IFF_UNICAST_FLT;
  1493. /* Get the MAC address */
  1494. xgmac_get_mac_addr(priv->base, ndev->dev_addr, 0);
  1495. if (!is_valid_ether_addr(ndev->dev_addr))
  1496. netdev_warn(ndev, "MAC address %pM not valid",
  1497. ndev->dev_addr);
  1498. netif_napi_add(ndev, &priv->napi, xgmac_poll, 64);
  1499. ret = register_netdev(ndev);
  1500. if (ret)
  1501. goto err_reg;
  1502. return 0;
  1503. err_reg:
  1504. netif_napi_del(&priv->napi);
  1505. free_irq(priv->pmt_irq, ndev);
  1506. err_pmt_irq:
  1507. free_irq(ndev->irq, ndev);
  1508. err_irq:
  1509. iounmap(priv->base);
  1510. err_io:
  1511. free_netdev(ndev);
  1512. err_alloc:
  1513. release_mem_region(res->start, resource_size(res));
  1514. platform_set_drvdata(pdev, NULL);
  1515. return ret;
  1516. }
  1517. /**
  1518. * xgmac_dvr_remove
  1519. * @pdev: platform device pointer
  1520. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  1521. * changes the link status, releases the DMA descriptor rings,
  1522. * unregisters the MDIO bus and unmaps the allocated memory.
  1523. */
  1524. static int xgmac_remove(struct platform_device *pdev)
  1525. {
  1526. struct net_device *ndev = platform_get_drvdata(pdev);
  1527. struct xgmac_priv *priv = netdev_priv(ndev);
  1528. struct resource *res;
  1529. xgmac_mac_disable(priv->base);
  1530. /* Free the IRQ lines */
  1531. free_irq(ndev->irq, ndev);
  1532. free_irq(priv->pmt_irq, ndev);
  1533. platform_set_drvdata(pdev, NULL);
  1534. unregister_netdev(ndev);
  1535. netif_napi_del(&priv->napi);
  1536. iounmap(priv->base);
  1537. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1538. release_mem_region(res->start, resource_size(res));
  1539. free_netdev(ndev);
  1540. return 0;
  1541. }
  1542. #ifdef CONFIG_PM_SLEEP
  1543. static void xgmac_pmt(void __iomem *ioaddr, unsigned long mode)
  1544. {
  1545. unsigned int pmt = 0;
  1546. if (mode & WAKE_MAGIC)
  1547. pmt |= XGMAC_PMT_POWERDOWN | XGMAC_PMT_MAGIC_PKT_EN;
  1548. if (mode & WAKE_UCAST)
  1549. pmt |= XGMAC_PMT_POWERDOWN | XGMAC_PMT_GLBL_UNICAST;
  1550. writel(pmt, ioaddr + XGMAC_PMT);
  1551. }
  1552. static int xgmac_suspend(struct device *dev)
  1553. {
  1554. struct net_device *ndev = platform_get_drvdata(to_platform_device(dev));
  1555. struct xgmac_priv *priv = netdev_priv(ndev);
  1556. u32 value;
  1557. if (!ndev || !netif_running(ndev))
  1558. return 0;
  1559. netif_device_detach(ndev);
  1560. napi_disable(&priv->napi);
  1561. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  1562. if (device_may_wakeup(priv->device)) {
  1563. /* Stop TX/RX DMA Only */
  1564. value = readl(priv->base + XGMAC_DMA_CONTROL);
  1565. value &= ~(DMA_CONTROL_ST | DMA_CONTROL_SR);
  1566. writel(value, priv->base + XGMAC_DMA_CONTROL);
  1567. xgmac_pmt(priv->base, priv->wolopts);
  1568. } else
  1569. xgmac_mac_disable(priv->base);
  1570. return 0;
  1571. }
  1572. static int xgmac_resume(struct device *dev)
  1573. {
  1574. struct net_device *ndev = platform_get_drvdata(to_platform_device(dev));
  1575. struct xgmac_priv *priv = netdev_priv(ndev);
  1576. void __iomem *ioaddr = priv->base;
  1577. if (!netif_running(ndev))
  1578. return 0;
  1579. xgmac_pmt(ioaddr, 0);
  1580. /* Enable the MAC and DMA */
  1581. xgmac_mac_enable(ioaddr);
  1582. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_STATUS);
  1583. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_INTR_ENA);
  1584. netif_device_attach(ndev);
  1585. napi_enable(&priv->napi);
  1586. return 0;
  1587. }
  1588. #endif /* CONFIG_PM_SLEEP */
  1589. static SIMPLE_DEV_PM_OPS(xgmac_pm_ops, xgmac_suspend, xgmac_resume);
  1590. static const struct of_device_id xgmac_of_match[] = {
  1591. { .compatible = "calxeda,hb-xgmac", },
  1592. {},
  1593. };
  1594. MODULE_DEVICE_TABLE(of, xgmac_of_match);
  1595. static struct platform_driver xgmac_driver = {
  1596. .driver = {
  1597. .name = "calxedaxgmac",
  1598. .of_match_table = xgmac_of_match,
  1599. },
  1600. .probe = xgmac_probe,
  1601. .remove = xgmac_remove,
  1602. .driver.pm = &xgmac_pm_ops,
  1603. };
  1604. module_platform_driver(xgmac_driver);
  1605. MODULE_AUTHOR("Calxeda, Inc.");
  1606. MODULE_DESCRIPTION("Calxeda 10G XGMAC driver");
  1607. MODULE_LICENSE("GPL v2");