xgmac.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  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. netdev_features_t 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. return ret;
  1515. }
  1516. /**
  1517. * xgmac_dvr_remove
  1518. * @pdev: platform device pointer
  1519. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  1520. * changes the link status, releases the DMA descriptor rings,
  1521. * unregisters the MDIO bus and unmaps the allocated memory.
  1522. */
  1523. static int xgmac_remove(struct platform_device *pdev)
  1524. {
  1525. struct net_device *ndev = platform_get_drvdata(pdev);
  1526. struct xgmac_priv *priv = netdev_priv(ndev);
  1527. struct resource *res;
  1528. xgmac_mac_disable(priv->base);
  1529. /* Free the IRQ lines */
  1530. free_irq(ndev->irq, ndev);
  1531. free_irq(priv->pmt_irq, ndev);
  1532. unregister_netdev(ndev);
  1533. netif_napi_del(&priv->napi);
  1534. iounmap(priv->base);
  1535. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1536. release_mem_region(res->start, resource_size(res));
  1537. free_netdev(ndev);
  1538. return 0;
  1539. }
  1540. #ifdef CONFIG_PM_SLEEP
  1541. static void xgmac_pmt(void __iomem *ioaddr, unsigned long mode)
  1542. {
  1543. unsigned int pmt = 0;
  1544. if (mode & WAKE_MAGIC)
  1545. pmt |= XGMAC_PMT_POWERDOWN | XGMAC_PMT_MAGIC_PKT_EN;
  1546. if (mode & WAKE_UCAST)
  1547. pmt |= XGMAC_PMT_POWERDOWN | XGMAC_PMT_GLBL_UNICAST;
  1548. writel(pmt, ioaddr + XGMAC_PMT);
  1549. }
  1550. static int xgmac_suspend(struct device *dev)
  1551. {
  1552. struct net_device *ndev = platform_get_drvdata(to_platform_device(dev));
  1553. struct xgmac_priv *priv = netdev_priv(ndev);
  1554. u32 value;
  1555. if (!ndev || !netif_running(ndev))
  1556. return 0;
  1557. netif_device_detach(ndev);
  1558. napi_disable(&priv->napi);
  1559. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  1560. if (device_may_wakeup(priv->device)) {
  1561. /* Stop TX/RX DMA Only */
  1562. value = readl(priv->base + XGMAC_DMA_CONTROL);
  1563. value &= ~(DMA_CONTROL_ST | DMA_CONTROL_SR);
  1564. writel(value, priv->base + XGMAC_DMA_CONTROL);
  1565. xgmac_pmt(priv->base, priv->wolopts);
  1566. } else
  1567. xgmac_mac_disable(priv->base);
  1568. return 0;
  1569. }
  1570. static int xgmac_resume(struct device *dev)
  1571. {
  1572. struct net_device *ndev = platform_get_drvdata(to_platform_device(dev));
  1573. struct xgmac_priv *priv = netdev_priv(ndev);
  1574. void __iomem *ioaddr = priv->base;
  1575. if (!netif_running(ndev))
  1576. return 0;
  1577. xgmac_pmt(ioaddr, 0);
  1578. /* Enable the MAC and DMA */
  1579. xgmac_mac_enable(ioaddr);
  1580. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_STATUS);
  1581. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_INTR_ENA);
  1582. netif_device_attach(ndev);
  1583. napi_enable(&priv->napi);
  1584. return 0;
  1585. }
  1586. #endif /* CONFIG_PM_SLEEP */
  1587. static SIMPLE_DEV_PM_OPS(xgmac_pm_ops, xgmac_suspend, xgmac_resume);
  1588. static const struct of_device_id xgmac_of_match[] = {
  1589. { .compatible = "calxeda,hb-xgmac", },
  1590. {},
  1591. };
  1592. MODULE_DEVICE_TABLE(of, xgmac_of_match);
  1593. static struct platform_driver xgmac_driver = {
  1594. .driver = {
  1595. .name = "calxedaxgmac",
  1596. .of_match_table = xgmac_of_match,
  1597. },
  1598. .probe = xgmac_probe,
  1599. .remove = xgmac_remove,
  1600. .driver.pm = &xgmac_pm_ops,
  1601. };
  1602. module_platform_driver(xgmac_driver);
  1603. MODULE_AUTHOR("Calxeda, Inc.");
  1604. MODULE_DESCRIPTION("Calxeda 10G XGMAC driver");
  1605. MODULE_LICENSE("GPL v2");