xgmac.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  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_payload_error;
  327. unsigned long rx_ip_header_error;
  328. /* Tx/Rx IRQ errors */
  329. unsigned long tx_process_stopped;
  330. unsigned long rx_buf_unav;
  331. unsigned long rx_process_stopped;
  332. unsigned long tx_early;
  333. unsigned long fatal_bus_error;
  334. };
  335. struct xgmac_priv {
  336. struct xgmac_dma_desc *dma_rx;
  337. struct sk_buff **rx_skbuff;
  338. unsigned int rx_tail;
  339. unsigned int rx_head;
  340. struct xgmac_dma_desc *dma_tx;
  341. struct sk_buff **tx_skbuff;
  342. unsigned int tx_head;
  343. unsigned int tx_tail;
  344. int tx_irq_cnt;
  345. void __iomem *base;
  346. unsigned int dma_buf_sz;
  347. dma_addr_t dma_rx_phy;
  348. dma_addr_t dma_tx_phy;
  349. struct net_device *dev;
  350. struct device *device;
  351. struct napi_struct napi;
  352. struct xgmac_extra_stats xstats;
  353. spinlock_t stats_lock;
  354. int pmt_irq;
  355. char rx_pause;
  356. char tx_pause;
  357. int wolopts;
  358. struct work_struct tx_timeout_work;
  359. };
  360. /* XGMAC Configuration Settings */
  361. #define MAX_MTU 9000
  362. #define PAUSE_TIME 0x400
  363. #define DMA_RX_RING_SZ 256
  364. #define DMA_TX_RING_SZ 128
  365. /* minimum number of free TX descriptors required to wake up TX process */
  366. #define TX_THRESH (DMA_TX_RING_SZ/4)
  367. /* DMA descriptor ring helpers */
  368. #define dma_ring_incr(n, s) (((n) + 1) & ((s) - 1))
  369. #define dma_ring_space(h, t, s) CIRC_SPACE(h, t, s)
  370. #define dma_ring_cnt(h, t, s) CIRC_CNT(h, t, s)
  371. #define tx_dma_ring_space(p) \
  372. dma_ring_space((p)->tx_head, (p)->tx_tail, DMA_TX_RING_SZ)
  373. /* XGMAC Descriptor Access Helpers */
  374. static inline void desc_set_buf_len(struct xgmac_dma_desc *p, u32 buf_sz)
  375. {
  376. if (buf_sz > MAX_DESC_BUF_SZ)
  377. p->buf_size = cpu_to_le32(MAX_DESC_BUF_SZ |
  378. (buf_sz - MAX_DESC_BUF_SZ) << DESC_BUFFER2_SZ_OFFSET);
  379. else
  380. p->buf_size = cpu_to_le32(buf_sz);
  381. }
  382. static inline int desc_get_buf_len(struct xgmac_dma_desc *p)
  383. {
  384. u32 len = le32_to_cpu(p->buf_size);
  385. return (len & DESC_BUFFER1_SZ_MASK) +
  386. ((len & DESC_BUFFER2_SZ_MASK) >> DESC_BUFFER2_SZ_OFFSET);
  387. }
  388. static inline void desc_init_rx_desc(struct xgmac_dma_desc *p, int ring_size,
  389. int buf_sz)
  390. {
  391. struct xgmac_dma_desc *end = p + ring_size - 1;
  392. memset(p, 0, sizeof(*p) * ring_size);
  393. for (; p <= end; p++)
  394. desc_set_buf_len(p, buf_sz);
  395. end->buf_size |= cpu_to_le32(RXDESC1_END_RING);
  396. }
  397. static inline void desc_init_tx_desc(struct xgmac_dma_desc *p, u32 ring_size)
  398. {
  399. memset(p, 0, sizeof(*p) * ring_size);
  400. p[ring_size - 1].flags = cpu_to_le32(TXDESC_END_RING);
  401. }
  402. static inline int desc_get_owner(struct xgmac_dma_desc *p)
  403. {
  404. return le32_to_cpu(p->flags) & DESC_OWN;
  405. }
  406. static inline void desc_set_rx_owner(struct xgmac_dma_desc *p)
  407. {
  408. /* Clear all fields and set the owner */
  409. p->flags = cpu_to_le32(DESC_OWN);
  410. }
  411. static inline void desc_set_tx_owner(struct xgmac_dma_desc *p, u32 flags)
  412. {
  413. u32 tmpflags = le32_to_cpu(p->flags);
  414. tmpflags &= TXDESC_END_RING;
  415. tmpflags |= flags | DESC_OWN;
  416. p->flags = cpu_to_le32(tmpflags);
  417. }
  418. static inline int desc_get_tx_ls(struct xgmac_dma_desc *p)
  419. {
  420. return le32_to_cpu(p->flags) & TXDESC_LAST_SEG;
  421. }
  422. static inline int desc_get_tx_fs(struct xgmac_dma_desc *p)
  423. {
  424. return le32_to_cpu(p->flags) & TXDESC_FIRST_SEG;
  425. }
  426. static inline u32 desc_get_buf_addr(struct xgmac_dma_desc *p)
  427. {
  428. return le32_to_cpu(p->buf1_addr);
  429. }
  430. static inline void desc_set_buf_addr(struct xgmac_dma_desc *p,
  431. u32 paddr, int len)
  432. {
  433. p->buf1_addr = cpu_to_le32(paddr);
  434. if (len > MAX_DESC_BUF_SZ)
  435. p->buf2_addr = cpu_to_le32(paddr + MAX_DESC_BUF_SZ);
  436. }
  437. static inline void desc_set_buf_addr_and_size(struct xgmac_dma_desc *p,
  438. u32 paddr, int len)
  439. {
  440. desc_set_buf_len(p, len);
  441. desc_set_buf_addr(p, paddr, len);
  442. }
  443. static inline int desc_get_rx_frame_len(struct xgmac_dma_desc *p)
  444. {
  445. u32 data = le32_to_cpu(p->flags);
  446. u32 len = (data & RXDESC_FRAME_LEN_MASK) >> RXDESC_FRAME_LEN_OFFSET;
  447. if (data & RXDESC_FRAME_TYPE)
  448. len -= ETH_FCS_LEN;
  449. return len;
  450. }
  451. static void xgmac_dma_flush_tx_fifo(void __iomem *ioaddr)
  452. {
  453. int timeout = 1000;
  454. u32 reg = readl(ioaddr + XGMAC_OMR);
  455. writel(reg | XGMAC_OMR_FTF, ioaddr + XGMAC_OMR);
  456. while ((timeout-- > 0) && readl(ioaddr + XGMAC_OMR) & XGMAC_OMR_FTF)
  457. udelay(1);
  458. }
  459. static int desc_get_tx_status(struct xgmac_priv *priv, struct xgmac_dma_desc *p)
  460. {
  461. struct xgmac_extra_stats *x = &priv->xstats;
  462. u32 status = le32_to_cpu(p->flags);
  463. if (!(status & TXDESC_ERROR_SUMMARY))
  464. return 0;
  465. netdev_dbg(priv->dev, "tx desc error = 0x%08x\n", status);
  466. if (status & TXDESC_JABBER_TIMEOUT)
  467. x->tx_jabber++;
  468. if (status & TXDESC_FRAME_FLUSHED)
  469. x->tx_frame_flushed++;
  470. if (status & TXDESC_UNDERFLOW_ERR)
  471. xgmac_dma_flush_tx_fifo(priv->base);
  472. if (status & TXDESC_IP_HEADER_ERR)
  473. x->tx_ip_header_error++;
  474. if (status & TXDESC_LOCAL_FAULT)
  475. x->tx_local_fault++;
  476. if (status & TXDESC_REMOTE_FAULT)
  477. x->tx_remote_fault++;
  478. if (status & TXDESC_PAYLOAD_CSUM_ERR)
  479. x->tx_payload_error++;
  480. return -1;
  481. }
  482. static int desc_get_rx_status(struct xgmac_priv *priv, struct xgmac_dma_desc *p)
  483. {
  484. struct xgmac_extra_stats *x = &priv->xstats;
  485. int ret = CHECKSUM_UNNECESSARY;
  486. u32 status = le32_to_cpu(p->flags);
  487. u32 ext_status = le32_to_cpu(p->ext_status);
  488. if (status & RXDESC_DA_FILTER_FAIL) {
  489. netdev_dbg(priv->dev, "XGMAC RX : Dest Address filter fail\n");
  490. x->rx_da_filter_fail++;
  491. return -1;
  492. }
  493. /* All frames should fit into a single buffer */
  494. if (!(status & RXDESC_FIRST_SEG) || !(status & RXDESC_LAST_SEG))
  495. return -1;
  496. /* Check if packet has checksum already */
  497. if ((status & RXDESC_FRAME_TYPE) && (status & RXDESC_EXT_STATUS) &&
  498. !(ext_status & RXDESC_IP_PAYLOAD_MASK))
  499. ret = CHECKSUM_NONE;
  500. netdev_dbg(priv->dev, "rx status - frame type=%d, csum = %d, ext stat %08x\n",
  501. (status & RXDESC_FRAME_TYPE) ? 1 : 0, ret, ext_status);
  502. if (!(status & RXDESC_ERROR_SUMMARY))
  503. return ret;
  504. /* Handle any errors */
  505. if (status & (RXDESC_DESCRIPTOR_ERR | RXDESC_OVERFLOW_ERR |
  506. RXDESC_GIANT_FRAME | RXDESC_LENGTH_ERR | RXDESC_CRC_ERR))
  507. return -1;
  508. if (status & RXDESC_EXT_STATUS) {
  509. if (ext_status & RXDESC_IP_HEADER_ERR)
  510. x->rx_ip_header_error++;
  511. if (ext_status & RXDESC_IP_PAYLOAD_ERR)
  512. x->rx_payload_error++;
  513. netdev_dbg(priv->dev, "IP checksum error - stat %08x\n",
  514. ext_status);
  515. return CHECKSUM_NONE;
  516. }
  517. return ret;
  518. }
  519. static inline void xgmac_mac_enable(void __iomem *ioaddr)
  520. {
  521. u32 value = readl(ioaddr + XGMAC_CONTROL);
  522. value |= MAC_ENABLE_RX | MAC_ENABLE_TX;
  523. writel(value, ioaddr + XGMAC_CONTROL);
  524. value = readl(ioaddr + XGMAC_DMA_CONTROL);
  525. value |= DMA_CONTROL_ST | DMA_CONTROL_SR;
  526. writel(value, ioaddr + XGMAC_DMA_CONTROL);
  527. }
  528. static inline void xgmac_mac_disable(void __iomem *ioaddr)
  529. {
  530. u32 value = readl(ioaddr + XGMAC_DMA_CONTROL);
  531. value &= ~(DMA_CONTROL_ST | DMA_CONTROL_SR);
  532. writel(value, ioaddr + XGMAC_DMA_CONTROL);
  533. value = readl(ioaddr + XGMAC_CONTROL);
  534. value &= ~(MAC_ENABLE_TX | MAC_ENABLE_RX);
  535. writel(value, ioaddr + XGMAC_CONTROL);
  536. }
  537. static void xgmac_set_mac_addr(void __iomem *ioaddr, unsigned char *addr,
  538. int num)
  539. {
  540. u32 data;
  541. if (addr) {
  542. data = (addr[5] << 8) | addr[4] | (num ? XGMAC_ADDR_AE : 0);
  543. writel(data, ioaddr + XGMAC_ADDR_HIGH(num));
  544. data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  545. writel(data, ioaddr + XGMAC_ADDR_LOW(num));
  546. } else {
  547. writel(0, ioaddr + XGMAC_ADDR_HIGH(num));
  548. writel(0, ioaddr + XGMAC_ADDR_LOW(num));
  549. }
  550. }
  551. static void xgmac_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
  552. int num)
  553. {
  554. u32 hi_addr, lo_addr;
  555. /* Read the MAC address from the hardware */
  556. hi_addr = readl(ioaddr + XGMAC_ADDR_HIGH(num));
  557. lo_addr = readl(ioaddr + XGMAC_ADDR_LOW(num));
  558. /* Extract the MAC address from the high and low words */
  559. addr[0] = lo_addr & 0xff;
  560. addr[1] = (lo_addr >> 8) & 0xff;
  561. addr[2] = (lo_addr >> 16) & 0xff;
  562. addr[3] = (lo_addr >> 24) & 0xff;
  563. addr[4] = hi_addr & 0xff;
  564. addr[5] = (hi_addr >> 8) & 0xff;
  565. }
  566. static int xgmac_set_flow_ctrl(struct xgmac_priv *priv, int rx, int tx)
  567. {
  568. u32 reg;
  569. unsigned int flow = 0;
  570. priv->rx_pause = rx;
  571. priv->tx_pause = tx;
  572. if (rx || tx) {
  573. if (rx)
  574. flow |= XGMAC_FLOW_CTRL_RFE;
  575. if (tx)
  576. flow |= XGMAC_FLOW_CTRL_TFE;
  577. flow |= XGMAC_FLOW_CTRL_PLT | XGMAC_FLOW_CTRL_UP;
  578. flow |= (PAUSE_TIME << XGMAC_FLOW_CTRL_PT_SHIFT);
  579. writel(flow, priv->base + XGMAC_FLOW_CTRL);
  580. reg = readl(priv->base + XGMAC_OMR);
  581. reg |= XGMAC_OMR_EFC;
  582. writel(reg, priv->base + XGMAC_OMR);
  583. } else {
  584. writel(0, priv->base + XGMAC_FLOW_CTRL);
  585. reg = readl(priv->base + XGMAC_OMR);
  586. reg &= ~XGMAC_OMR_EFC;
  587. writel(reg, priv->base + XGMAC_OMR);
  588. }
  589. return 0;
  590. }
  591. static void xgmac_rx_refill(struct xgmac_priv *priv)
  592. {
  593. struct xgmac_dma_desc *p;
  594. dma_addr_t paddr;
  595. int bufsz = priv->dev->mtu + ETH_HLEN + ETH_FCS_LEN;
  596. while (dma_ring_space(priv->rx_head, priv->rx_tail, DMA_RX_RING_SZ) > 1) {
  597. int entry = priv->rx_head;
  598. struct sk_buff *skb;
  599. p = priv->dma_rx + entry;
  600. if (priv->rx_skbuff[entry] == NULL) {
  601. skb = netdev_alloc_skb_ip_align(priv->dev, bufsz);
  602. if (unlikely(skb == NULL))
  603. break;
  604. paddr = dma_map_single(priv->device, skb->data,
  605. priv->dma_buf_sz - NET_IP_ALIGN,
  606. DMA_FROM_DEVICE);
  607. if (dma_mapping_error(priv->device, paddr)) {
  608. dev_kfree_skb_any(skb);
  609. break;
  610. }
  611. priv->rx_skbuff[entry] = skb;
  612. desc_set_buf_addr(p, paddr, priv->dma_buf_sz);
  613. }
  614. netdev_dbg(priv->dev, "rx ring: head %d, tail %d\n",
  615. priv->rx_head, priv->rx_tail);
  616. priv->rx_head = dma_ring_incr(priv->rx_head, DMA_RX_RING_SZ);
  617. desc_set_rx_owner(p);
  618. }
  619. }
  620. /**
  621. * init_xgmac_dma_desc_rings - init the RX/TX descriptor rings
  622. * @dev: net device structure
  623. * Description: this function initializes the DMA RX/TX descriptors
  624. * and allocates the socket buffers.
  625. */
  626. static int xgmac_dma_desc_rings_init(struct net_device *dev)
  627. {
  628. struct xgmac_priv *priv = netdev_priv(dev);
  629. unsigned int bfsize;
  630. /* Set the Buffer size according to the MTU;
  631. * The total buffer size including any IP offset must be a multiple
  632. * of 8 bytes.
  633. */
  634. bfsize = ALIGN(dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN, 8);
  635. netdev_dbg(priv->dev, "mtu [%d] bfsize [%d]\n", dev->mtu, bfsize);
  636. priv->rx_skbuff = kzalloc(sizeof(struct sk_buff *) * DMA_RX_RING_SZ,
  637. GFP_KERNEL);
  638. if (!priv->rx_skbuff)
  639. return -ENOMEM;
  640. priv->dma_rx = dma_alloc_coherent(priv->device,
  641. DMA_RX_RING_SZ *
  642. sizeof(struct xgmac_dma_desc),
  643. &priv->dma_rx_phy,
  644. GFP_KERNEL);
  645. if (!priv->dma_rx)
  646. goto err_dma_rx;
  647. priv->tx_skbuff = kzalloc(sizeof(struct sk_buff *) * DMA_TX_RING_SZ,
  648. GFP_KERNEL);
  649. if (!priv->tx_skbuff)
  650. goto err_tx_skb;
  651. priv->dma_tx = dma_alloc_coherent(priv->device,
  652. DMA_TX_RING_SZ *
  653. sizeof(struct xgmac_dma_desc),
  654. &priv->dma_tx_phy,
  655. GFP_KERNEL);
  656. if (!priv->dma_tx)
  657. goto err_dma_tx;
  658. netdev_dbg(priv->dev, "DMA desc rings: virt addr (Rx %p, "
  659. "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
  660. priv->dma_rx, priv->dma_tx,
  661. (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
  662. priv->rx_tail = 0;
  663. priv->rx_head = 0;
  664. priv->dma_buf_sz = bfsize;
  665. desc_init_rx_desc(priv->dma_rx, DMA_RX_RING_SZ, priv->dma_buf_sz);
  666. xgmac_rx_refill(priv);
  667. priv->tx_tail = 0;
  668. priv->tx_head = 0;
  669. desc_init_tx_desc(priv->dma_tx, DMA_TX_RING_SZ);
  670. writel(priv->dma_tx_phy, priv->base + XGMAC_DMA_TX_BASE_ADDR);
  671. writel(priv->dma_rx_phy, priv->base + XGMAC_DMA_RX_BASE_ADDR);
  672. return 0;
  673. err_dma_tx:
  674. kfree(priv->tx_skbuff);
  675. err_tx_skb:
  676. dma_free_coherent(priv->device,
  677. DMA_RX_RING_SZ * sizeof(struct xgmac_dma_desc),
  678. priv->dma_rx, priv->dma_rx_phy);
  679. err_dma_rx:
  680. kfree(priv->rx_skbuff);
  681. return -ENOMEM;
  682. }
  683. static void xgmac_free_rx_skbufs(struct xgmac_priv *priv)
  684. {
  685. int i;
  686. struct xgmac_dma_desc *p;
  687. if (!priv->rx_skbuff)
  688. return;
  689. for (i = 0; i < DMA_RX_RING_SZ; i++) {
  690. struct sk_buff *skb = priv->rx_skbuff[i];
  691. if (skb == NULL)
  692. continue;
  693. p = priv->dma_rx + i;
  694. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  695. priv->dma_buf_sz - NET_IP_ALIGN, DMA_FROM_DEVICE);
  696. dev_kfree_skb_any(skb);
  697. priv->rx_skbuff[i] = NULL;
  698. }
  699. }
  700. static void xgmac_free_tx_skbufs(struct xgmac_priv *priv)
  701. {
  702. int i;
  703. struct xgmac_dma_desc *p;
  704. if (!priv->tx_skbuff)
  705. return;
  706. for (i = 0; i < DMA_TX_RING_SZ; i++) {
  707. if (priv->tx_skbuff[i] == NULL)
  708. continue;
  709. p = priv->dma_tx + i;
  710. if (desc_get_tx_fs(p))
  711. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  712. desc_get_buf_len(p), DMA_TO_DEVICE);
  713. else
  714. dma_unmap_page(priv->device, desc_get_buf_addr(p),
  715. desc_get_buf_len(p), DMA_TO_DEVICE);
  716. if (desc_get_tx_ls(p))
  717. dev_kfree_skb_any(priv->tx_skbuff[i]);
  718. priv->tx_skbuff[i] = NULL;
  719. }
  720. }
  721. static void xgmac_free_dma_desc_rings(struct xgmac_priv *priv)
  722. {
  723. /* Release the DMA TX/RX socket buffers */
  724. xgmac_free_rx_skbufs(priv);
  725. xgmac_free_tx_skbufs(priv);
  726. /* Free the consistent memory allocated for descriptor rings */
  727. if (priv->dma_tx) {
  728. dma_free_coherent(priv->device,
  729. DMA_TX_RING_SZ * sizeof(struct xgmac_dma_desc),
  730. priv->dma_tx, priv->dma_tx_phy);
  731. priv->dma_tx = NULL;
  732. }
  733. if (priv->dma_rx) {
  734. dma_free_coherent(priv->device,
  735. DMA_RX_RING_SZ * sizeof(struct xgmac_dma_desc),
  736. priv->dma_rx, priv->dma_rx_phy);
  737. priv->dma_rx = NULL;
  738. }
  739. kfree(priv->rx_skbuff);
  740. priv->rx_skbuff = NULL;
  741. kfree(priv->tx_skbuff);
  742. priv->tx_skbuff = NULL;
  743. }
  744. /**
  745. * xgmac_tx:
  746. * @priv: private driver structure
  747. * Description: it reclaims resources after transmission completes.
  748. */
  749. static void xgmac_tx_complete(struct xgmac_priv *priv)
  750. {
  751. while (dma_ring_cnt(priv->tx_head, priv->tx_tail, DMA_TX_RING_SZ)) {
  752. unsigned int entry = priv->tx_tail;
  753. struct sk_buff *skb = priv->tx_skbuff[entry];
  754. struct xgmac_dma_desc *p = priv->dma_tx + entry;
  755. /* Check if the descriptor is owned by the DMA. */
  756. if (desc_get_owner(p))
  757. break;
  758. netdev_dbg(priv->dev, "tx ring: curr %d, dirty %d\n",
  759. priv->tx_head, priv->tx_tail);
  760. if (desc_get_tx_fs(p))
  761. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  762. desc_get_buf_len(p), DMA_TO_DEVICE);
  763. else
  764. dma_unmap_page(priv->device, desc_get_buf_addr(p),
  765. desc_get_buf_len(p), DMA_TO_DEVICE);
  766. /* Check tx error on the last segment */
  767. if (desc_get_tx_ls(p)) {
  768. desc_get_tx_status(priv, p);
  769. dev_kfree_skb(skb);
  770. }
  771. priv->tx_skbuff[entry] = NULL;
  772. priv->tx_tail = dma_ring_incr(entry, DMA_TX_RING_SZ);
  773. }
  774. /* Ensure tx_tail is visible to xgmac_xmit */
  775. smp_mb();
  776. if (unlikely(netif_queue_stopped(priv->dev) &&
  777. (tx_dma_ring_space(priv) > MAX_SKB_FRAGS)))
  778. netif_wake_queue(priv->dev);
  779. }
  780. static void xgmac_tx_timeout_work(struct work_struct *work)
  781. {
  782. u32 reg, value;
  783. struct xgmac_priv *priv =
  784. container_of(work, struct xgmac_priv, tx_timeout_work);
  785. napi_disable(&priv->napi);
  786. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  787. netif_tx_lock(priv->dev);
  788. reg = readl(priv->base + XGMAC_DMA_CONTROL);
  789. writel(reg & ~DMA_CONTROL_ST, priv->base + XGMAC_DMA_CONTROL);
  790. do {
  791. value = readl(priv->base + XGMAC_DMA_STATUS) & 0x700000;
  792. } while (value && (value != 0x600000));
  793. xgmac_free_tx_skbufs(priv);
  794. desc_init_tx_desc(priv->dma_tx, DMA_TX_RING_SZ);
  795. priv->tx_tail = 0;
  796. priv->tx_head = 0;
  797. writel(priv->dma_tx_phy, priv->base + XGMAC_DMA_TX_BASE_ADDR);
  798. writel(reg | DMA_CONTROL_ST, priv->base + XGMAC_DMA_CONTROL);
  799. writel(DMA_STATUS_TU | DMA_STATUS_TPS | DMA_STATUS_NIS | DMA_STATUS_AIS,
  800. priv->base + XGMAC_DMA_STATUS);
  801. netif_tx_unlock(priv->dev);
  802. netif_wake_queue(priv->dev);
  803. napi_enable(&priv->napi);
  804. /* Enable interrupts */
  805. writel(DMA_INTR_DEFAULT_MASK, priv->base + XGMAC_DMA_STATUS);
  806. writel(DMA_INTR_DEFAULT_MASK, priv->base + XGMAC_DMA_INTR_ENA);
  807. }
  808. static int xgmac_hw_init(struct net_device *dev)
  809. {
  810. u32 value, ctrl;
  811. int limit;
  812. struct xgmac_priv *priv = netdev_priv(dev);
  813. void __iomem *ioaddr = priv->base;
  814. /* Save the ctrl register value */
  815. ctrl = readl(ioaddr + XGMAC_CONTROL) & XGMAC_CONTROL_SPD_MASK;
  816. /* SW reset */
  817. value = DMA_BUS_MODE_SFT_RESET;
  818. writel(value, ioaddr + XGMAC_DMA_BUS_MODE);
  819. limit = 15000;
  820. while (limit-- &&
  821. (readl(ioaddr + XGMAC_DMA_BUS_MODE) & DMA_BUS_MODE_SFT_RESET))
  822. cpu_relax();
  823. if (limit < 0)
  824. return -EBUSY;
  825. value = (0x10 << DMA_BUS_MODE_PBL_SHIFT) |
  826. (0x10 << DMA_BUS_MODE_RPBL_SHIFT) |
  827. DMA_BUS_MODE_FB | DMA_BUS_MODE_ATDS | DMA_BUS_MODE_AAL;
  828. writel(value, ioaddr + XGMAC_DMA_BUS_MODE);
  829. writel(0, ioaddr + XGMAC_DMA_INTR_ENA);
  830. /* Mask power mgt interrupt */
  831. writel(XGMAC_INT_STAT_PMTIM, ioaddr + XGMAC_INT_STAT);
  832. /* XGMAC requires AXI bus init. This is a 'magic number' for now */
  833. writel(0x0077000E, ioaddr + XGMAC_DMA_AXI_BUS);
  834. ctrl |= XGMAC_CONTROL_DDIC | XGMAC_CONTROL_JE | XGMAC_CONTROL_ACS |
  835. XGMAC_CONTROL_CAR;
  836. if (dev->features & NETIF_F_RXCSUM)
  837. ctrl |= XGMAC_CONTROL_IPC;
  838. writel(ctrl, ioaddr + XGMAC_CONTROL);
  839. writel(DMA_CONTROL_OSF, ioaddr + XGMAC_DMA_CONTROL);
  840. /* Set the HW DMA mode and the COE */
  841. writel(XGMAC_OMR_TSF | XGMAC_OMR_RFD | XGMAC_OMR_RFA |
  842. XGMAC_OMR_RTC_256,
  843. ioaddr + XGMAC_OMR);
  844. /* Reset the MMC counters */
  845. writel(1, ioaddr + XGMAC_MMC_CTRL);
  846. return 0;
  847. }
  848. /**
  849. * xgmac_open - open entry point of the driver
  850. * @dev : pointer to the device structure.
  851. * Description:
  852. * This function is the open entry point of the driver.
  853. * Return value:
  854. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  855. * file on failure.
  856. */
  857. static int xgmac_open(struct net_device *dev)
  858. {
  859. int ret;
  860. struct xgmac_priv *priv = netdev_priv(dev);
  861. void __iomem *ioaddr = priv->base;
  862. /* Check that the MAC address is valid. If its not, refuse
  863. * to bring the device up. The user must specify an
  864. * address using the following linux command:
  865. * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
  866. if (!is_valid_ether_addr(dev->dev_addr)) {
  867. eth_hw_addr_random(dev);
  868. netdev_dbg(priv->dev, "generated random MAC address %pM\n",
  869. dev->dev_addr);
  870. }
  871. memset(&priv->xstats, 0, sizeof(struct xgmac_extra_stats));
  872. /* Initialize the XGMAC and descriptors */
  873. xgmac_hw_init(dev);
  874. xgmac_set_mac_addr(ioaddr, dev->dev_addr, 0);
  875. xgmac_set_flow_ctrl(priv, priv->rx_pause, priv->tx_pause);
  876. ret = xgmac_dma_desc_rings_init(dev);
  877. if (ret < 0)
  878. return ret;
  879. /* Enable the MAC Rx/Tx */
  880. xgmac_mac_enable(ioaddr);
  881. napi_enable(&priv->napi);
  882. netif_start_queue(dev);
  883. /* Enable interrupts */
  884. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_STATUS);
  885. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_INTR_ENA);
  886. return 0;
  887. }
  888. /**
  889. * xgmac_release - close entry point of the driver
  890. * @dev : device pointer.
  891. * Description:
  892. * This is the stop entry point of the driver.
  893. */
  894. static int xgmac_stop(struct net_device *dev)
  895. {
  896. struct xgmac_priv *priv = netdev_priv(dev);
  897. netif_stop_queue(dev);
  898. if (readl(priv->base + XGMAC_DMA_INTR_ENA))
  899. napi_disable(&priv->napi);
  900. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  901. /* Disable the MAC core */
  902. xgmac_mac_disable(priv->base);
  903. /* Release and free the Rx/Tx resources */
  904. xgmac_free_dma_desc_rings(priv);
  905. return 0;
  906. }
  907. /**
  908. * xgmac_xmit:
  909. * @skb : the socket buffer
  910. * @dev : device pointer
  911. * Description : Tx entry point of the driver.
  912. */
  913. static netdev_tx_t xgmac_xmit(struct sk_buff *skb, struct net_device *dev)
  914. {
  915. struct xgmac_priv *priv = netdev_priv(dev);
  916. unsigned int entry;
  917. int i;
  918. u32 irq_flag;
  919. int nfrags = skb_shinfo(skb)->nr_frags;
  920. struct xgmac_dma_desc *desc, *first;
  921. unsigned int desc_flags;
  922. unsigned int len;
  923. dma_addr_t paddr;
  924. priv->tx_irq_cnt = (priv->tx_irq_cnt + 1) & (DMA_TX_RING_SZ/4 - 1);
  925. irq_flag = priv->tx_irq_cnt ? 0 : TXDESC_INTERRUPT;
  926. desc_flags = (skb->ip_summed == CHECKSUM_PARTIAL) ?
  927. TXDESC_CSUM_ALL : 0;
  928. entry = priv->tx_head;
  929. desc = priv->dma_tx + entry;
  930. first = desc;
  931. len = skb_headlen(skb);
  932. paddr = dma_map_single(priv->device, skb->data, len, DMA_TO_DEVICE);
  933. if (dma_mapping_error(priv->device, paddr)) {
  934. dev_kfree_skb(skb);
  935. return -EIO;
  936. }
  937. priv->tx_skbuff[entry] = skb;
  938. desc_set_buf_addr_and_size(desc, paddr, len);
  939. for (i = 0; i < nfrags; i++) {
  940. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  941. len = frag->size;
  942. paddr = skb_frag_dma_map(priv->device, frag, 0, len,
  943. DMA_TO_DEVICE);
  944. if (dma_mapping_error(priv->device, paddr)) {
  945. dev_kfree_skb(skb);
  946. return -EIO;
  947. }
  948. entry = dma_ring_incr(entry, DMA_TX_RING_SZ);
  949. desc = priv->dma_tx + entry;
  950. priv->tx_skbuff[entry] = skb;
  951. desc_set_buf_addr_and_size(desc, paddr, len);
  952. if (i < (nfrags - 1))
  953. desc_set_tx_owner(desc, desc_flags);
  954. }
  955. /* Interrupt on completition only for the latest segment */
  956. if (desc != first)
  957. desc_set_tx_owner(desc, desc_flags |
  958. TXDESC_LAST_SEG | irq_flag);
  959. else
  960. desc_flags |= TXDESC_LAST_SEG | irq_flag;
  961. /* Set owner on first desc last to avoid race condition */
  962. wmb();
  963. desc_set_tx_owner(first, desc_flags | TXDESC_FIRST_SEG);
  964. writel(1, priv->base + XGMAC_DMA_TX_POLL);
  965. priv->tx_head = dma_ring_incr(entry, DMA_TX_RING_SZ);
  966. /* Ensure tx_head update is visible to tx completion */
  967. smp_mb();
  968. if (unlikely(tx_dma_ring_space(priv) <= MAX_SKB_FRAGS)) {
  969. netif_stop_queue(dev);
  970. /* Ensure netif_stop_queue is visible to tx completion */
  971. smp_mb();
  972. if (tx_dma_ring_space(priv) > MAX_SKB_FRAGS)
  973. netif_start_queue(dev);
  974. }
  975. return NETDEV_TX_OK;
  976. }
  977. static int xgmac_rx(struct xgmac_priv *priv, int limit)
  978. {
  979. unsigned int entry;
  980. unsigned int count = 0;
  981. struct xgmac_dma_desc *p;
  982. while (count < limit) {
  983. int ip_checksum;
  984. struct sk_buff *skb;
  985. int frame_len;
  986. if (!dma_ring_cnt(priv->rx_head, priv->rx_tail, DMA_RX_RING_SZ))
  987. break;
  988. entry = priv->rx_tail;
  989. p = priv->dma_rx + entry;
  990. if (desc_get_owner(p))
  991. break;
  992. count++;
  993. priv->rx_tail = dma_ring_incr(priv->rx_tail, DMA_RX_RING_SZ);
  994. /* read the status of the incoming frame */
  995. ip_checksum = desc_get_rx_status(priv, p);
  996. if (ip_checksum < 0)
  997. continue;
  998. skb = priv->rx_skbuff[entry];
  999. if (unlikely(!skb)) {
  1000. netdev_err(priv->dev, "Inconsistent Rx descriptor chain\n");
  1001. break;
  1002. }
  1003. priv->rx_skbuff[entry] = NULL;
  1004. frame_len = desc_get_rx_frame_len(p);
  1005. netdev_dbg(priv->dev, "RX frame size %d, COE status: %d\n",
  1006. frame_len, ip_checksum);
  1007. skb_put(skb, frame_len);
  1008. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  1009. priv->dma_buf_sz - NET_IP_ALIGN, DMA_FROM_DEVICE);
  1010. skb->protocol = eth_type_trans(skb, priv->dev);
  1011. skb->ip_summed = ip_checksum;
  1012. if (ip_checksum == CHECKSUM_NONE)
  1013. netif_receive_skb(skb);
  1014. else
  1015. napi_gro_receive(&priv->napi, skb);
  1016. }
  1017. xgmac_rx_refill(priv);
  1018. return count;
  1019. }
  1020. /**
  1021. * xgmac_poll - xgmac poll method (NAPI)
  1022. * @napi : pointer to the napi structure.
  1023. * @budget : maximum number of packets that the current CPU can receive from
  1024. * all interfaces.
  1025. * Description :
  1026. * This function implements the the reception process.
  1027. * Also it runs the TX completion thread
  1028. */
  1029. static int xgmac_poll(struct napi_struct *napi, int budget)
  1030. {
  1031. struct xgmac_priv *priv = container_of(napi,
  1032. struct xgmac_priv, napi);
  1033. int work_done = 0;
  1034. xgmac_tx_complete(priv);
  1035. work_done = xgmac_rx(priv, budget);
  1036. if (work_done < budget) {
  1037. napi_complete(napi);
  1038. __raw_writel(DMA_INTR_DEFAULT_MASK, priv->base + XGMAC_DMA_INTR_ENA);
  1039. }
  1040. return work_done;
  1041. }
  1042. /**
  1043. * xgmac_tx_timeout
  1044. * @dev : Pointer to net device structure
  1045. * Description: this function is called when a packet transmission fails to
  1046. * complete within a reasonable tmrate. The driver will mark the error in the
  1047. * netdev structure and arrange for the device to be reset to a sane state
  1048. * in order to transmit a new packet.
  1049. */
  1050. static void xgmac_tx_timeout(struct net_device *dev)
  1051. {
  1052. struct xgmac_priv *priv = netdev_priv(dev);
  1053. schedule_work(&priv->tx_timeout_work);
  1054. }
  1055. /**
  1056. * xgmac_set_rx_mode - entry point for multicast addressing
  1057. * @dev : pointer to the device structure
  1058. * Description:
  1059. * This function is a driver entry point which gets called by the kernel
  1060. * whenever multicast addresses must be enabled/disabled.
  1061. * Return value:
  1062. * void.
  1063. */
  1064. static void xgmac_set_rx_mode(struct net_device *dev)
  1065. {
  1066. int i;
  1067. struct xgmac_priv *priv = netdev_priv(dev);
  1068. void __iomem *ioaddr = priv->base;
  1069. unsigned int value = 0;
  1070. u32 hash_filter[XGMAC_NUM_HASH];
  1071. int reg = 1;
  1072. struct netdev_hw_addr *ha;
  1073. bool use_hash = false;
  1074. netdev_dbg(priv->dev, "# mcasts %d, # unicast %d\n",
  1075. netdev_mc_count(dev), netdev_uc_count(dev));
  1076. if (dev->flags & IFF_PROMISC) {
  1077. writel(XGMAC_FRAME_FILTER_PR, ioaddr + XGMAC_FRAME_FILTER);
  1078. return;
  1079. }
  1080. memset(hash_filter, 0, sizeof(hash_filter));
  1081. if (netdev_uc_count(dev) > XGMAC_MAX_FILTER_ADDR) {
  1082. use_hash = true;
  1083. value |= XGMAC_FRAME_FILTER_HUC | XGMAC_FRAME_FILTER_HPF;
  1084. }
  1085. netdev_for_each_uc_addr(ha, dev) {
  1086. if (use_hash) {
  1087. u32 bit_nr = ~ether_crc(ETH_ALEN, ha->addr) >> 23;
  1088. /* The most significant 4 bits determine the register to
  1089. * use (H/L) while the other 5 bits determine the bit
  1090. * within the register. */
  1091. hash_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1092. } else {
  1093. xgmac_set_mac_addr(ioaddr, ha->addr, reg);
  1094. reg++;
  1095. }
  1096. }
  1097. if (dev->flags & IFF_ALLMULTI) {
  1098. value |= XGMAC_FRAME_FILTER_PM;
  1099. goto out;
  1100. }
  1101. if ((netdev_mc_count(dev) + reg - 1) > XGMAC_MAX_FILTER_ADDR) {
  1102. use_hash = true;
  1103. value |= XGMAC_FRAME_FILTER_HMC | XGMAC_FRAME_FILTER_HPF;
  1104. } else {
  1105. use_hash = false;
  1106. }
  1107. netdev_for_each_mc_addr(ha, dev) {
  1108. if (use_hash) {
  1109. u32 bit_nr = ~ether_crc(ETH_ALEN, ha->addr) >> 23;
  1110. /* The most significant 4 bits determine the register to
  1111. * use (H/L) while the other 5 bits determine the bit
  1112. * within the register. */
  1113. hash_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1114. } else {
  1115. xgmac_set_mac_addr(ioaddr, ha->addr, reg);
  1116. reg++;
  1117. }
  1118. }
  1119. out:
  1120. for (i = reg; i < XGMAC_MAX_FILTER_ADDR; i++)
  1121. xgmac_set_mac_addr(ioaddr, NULL, reg);
  1122. for (i = 0; i < XGMAC_NUM_HASH; i++)
  1123. writel(hash_filter[i], ioaddr + XGMAC_HASH(i));
  1124. writel(value, ioaddr + XGMAC_FRAME_FILTER);
  1125. }
  1126. /**
  1127. * xgmac_change_mtu - entry point to change MTU size for the device.
  1128. * @dev : device pointer.
  1129. * @new_mtu : the new MTU size for the device.
  1130. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  1131. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  1132. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  1133. * Return value:
  1134. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1135. * file on failure.
  1136. */
  1137. static int xgmac_change_mtu(struct net_device *dev, int new_mtu)
  1138. {
  1139. struct xgmac_priv *priv = netdev_priv(dev);
  1140. int old_mtu;
  1141. if ((new_mtu < 46) || (new_mtu > MAX_MTU)) {
  1142. netdev_err(priv->dev, "invalid MTU, max MTU is: %d\n", MAX_MTU);
  1143. return -EINVAL;
  1144. }
  1145. old_mtu = dev->mtu;
  1146. dev->mtu = new_mtu;
  1147. /* return early if the buffer sizes will not change */
  1148. if (old_mtu <= ETH_DATA_LEN && new_mtu <= ETH_DATA_LEN)
  1149. return 0;
  1150. if (old_mtu == new_mtu)
  1151. return 0;
  1152. /* Stop everything, get ready to change the MTU */
  1153. if (!netif_running(dev))
  1154. return 0;
  1155. /* Bring the interface down and then back up */
  1156. xgmac_stop(dev);
  1157. return xgmac_open(dev);
  1158. }
  1159. static irqreturn_t xgmac_pmt_interrupt(int irq, void *dev_id)
  1160. {
  1161. u32 intr_status;
  1162. struct net_device *dev = (struct net_device *)dev_id;
  1163. struct xgmac_priv *priv = netdev_priv(dev);
  1164. void __iomem *ioaddr = priv->base;
  1165. intr_status = __raw_readl(ioaddr + XGMAC_INT_STAT);
  1166. if (intr_status & XGMAC_INT_STAT_PMT) {
  1167. netdev_dbg(priv->dev, "received Magic frame\n");
  1168. /* clear the PMT bits 5 and 6 by reading the PMT */
  1169. readl(ioaddr + XGMAC_PMT);
  1170. }
  1171. return IRQ_HANDLED;
  1172. }
  1173. static irqreturn_t xgmac_interrupt(int irq, void *dev_id)
  1174. {
  1175. u32 intr_status;
  1176. struct net_device *dev = (struct net_device *)dev_id;
  1177. struct xgmac_priv *priv = netdev_priv(dev);
  1178. struct xgmac_extra_stats *x = &priv->xstats;
  1179. /* read the status register (CSR5) */
  1180. intr_status = __raw_readl(priv->base + XGMAC_DMA_STATUS);
  1181. intr_status &= __raw_readl(priv->base + XGMAC_DMA_INTR_ENA);
  1182. __raw_writel(intr_status, priv->base + XGMAC_DMA_STATUS);
  1183. /* It displays the DMA process states (CSR5 register) */
  1184. /* ABNORMAL interrupts */
  1185. if (unlikely(intr_status & DMA_STATUS_AIS)) {
  1186. if (intr_status & DMA_STATUS_TJT) {
  1187. netdev_err(priv->dev, "transmit jabber\n");
  1188. x->tx_jabber++;
  1189. }
  1190. if (intr_status & DMA_STATUS_RU)
  1191. x->rx_buf_unav++;
  1192. if (intr_status & DMA_STATUS_RPS) {
  1193. netdev_err(priv->dev, "receive process stopped\n");
  1194. x->rx_process_stopped++;
  1195. }
  1196. if (intr_status & DMA_STATUS_ETI) {
  1197. netdev_err(priv->dev, "transmit early interrupt\n");
  1198. x->tx_early++;
  1199. }
  1200. if (intr_status & DMA_STATUS_TPS) {
  1201. netdev_err(priv->dev, "transmit process stopped\n");
  1202. x->tx_process_stopped++;
  1203. schedule_work(&priv->tx_timeout_work);
  1204. }
  1205. if (intr_status & DMA_STATUS_FBI) {
  1206. netdev_err(priv->dev, "fatal bus error\n");
  1207. x->fatal_bus_error++;
  1208. }
  1209. }
  1210. /* TX/RX NORMAL interrupts */
  1211. if (intr_status & (DMA_STATUS_RI | DMA_STATUS_TU | DMA_STATUS_TI)) {
  1212. __raw_writel(DMA_INTR_ABNORMAL, priv->base + XGMAC_DMA_INTR_ENA);
  1213. napi_schedule(&priv->napi);
  1214. }
  1215. return IRQ_HANDLED;
  1216. }
  1217. #ifdef CONFIG_NET_POLL_CONTROLLER
  1218. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  1219. * to allow network I/O with interrupts disabled. */
  1220. static void xgmac_poll_controller(struct net_device *dev)
  1221. {
  1222. disable_irq(dev->irq);
  1223. xgmac_interrupt(dev->irq, dev);
  1224. enable_irq(dev->irq);
  1225. }
  1226. #endif
  1227. static struct rtnl_link_stats64 *
  1228. xgmac_get_stats64(struct net_device *dev,
  1229. struct rtnl_link_stats64 *storage)
  1230. {
  1231. struct xgmac_priv *priv = netdev_priv(dev);
  1232. void __iomem *base = priv->base;
  1233. u32 count;
  1234. spin_lock_bh(&priv->stats_lock);
  1235. writel(XGMAC_MMC_CTRL_CNT_FRZ, base + XGMAC_MMC_CTRL);
  1236. storage->rx_bytes = readl(base + XGMAC_MMC_RXOCTET_G_LO);
  1237. storage->rx_bytes |= (u64)(readl(base + XGMAC_MMC_RXOCTET_G_HI)) << 32;
  1238. storage->rx_packets = readl(base + XGMAC_MMC_RXFRAME_GB_LO);
  1239. storage->multicast = readl(base + XGMAC_MMC_RXMCFRAME_G);
  1240. storage->rx_crc_errors = readl(base + XGMAC_MMC_RXCRCERR);
  1241. storage->rx_length_errors = readl(base + XGMAC_MMC_RXLENGTHERR);
  1242. storage->rx_missed_errors = readl(base + XGMAC_MMC_RXOVERFLOW);
  1243. storage->tx_bytes = readl(base + XGMAC_MMC_TXOCTET_G_LO);
  1244. storage->tx_bytes |= (u64)(readl(base + XGMAC_MMC_TXOCTET_G_HI)) << 32;
  1245. count = readl(base + XGMAC_MMC_TXFRAME_GB_LO);
  1246. storage->tx_errors = count - readl(base + XGMAC_MMC_TXFRAME_G_LO);
  1247. storage->tx_packets = count;
  1248. storage->tx_fifo_errors = readl(base + XGMAC_MMC_TXUNDERFLOW);
  1249. writel(0, base + XGMAC_MMC_CTRL);
  1250. spin_unlock_bh(&priv->stats_lock);
  1251. return storage;
  1252. }
  1253. static int xgmac_set_mac_address(struct net_device *dev, void *p)
  1254. {
  1255. struct xgmac_priv *priv = netdev_priv(dev);
  1256. void __iomem *ioaddr = priv->base;
  1257. struct sockaddr *addr = p;
  1258. if (!is_valid_ether_addr(addr->sa_data))
  1259. return -EADDRNOTAVAIL;
  1260. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1261. xgmac_set_mac_addr(ioaddr, dev->dev_addr, 0);
  1262. return 0;
  1263. }
  1264. static int xgmac_set_features(struct net_device *dev, netdev_features_t features)
  1265. {
  1266. u32 ctrl;
  1267. struct xgmac_priv *priv = netdev_priv(dev);
  1268. void __iomem *ioaddr = priv->base;
  1269. netdev_features_t changed = dev->features ^ features;
  1270. if (!(changed & NETIF_F_RXCSUM))
  1271. return 0;
  1272. ctrl = readl(ioaddr + XGMAC_CONTROL);
  1273. if (features & NETIF_F_RXCSUM)
  1274. ctrl |= XGMAC_CONTROL_IPC;
  1275. else
  1276. ctrl &= ~XGMAC_CONTROL_IPC;
  1277. writel(ctrl, ioaddr + XGMAC_CONTROL);
  1278. return 0;
  1279. }
  1280. static const struct net_device_ops xgmac_netdev_ops = {
  1281. .ndo_open = xgmac_open,
  1282. .ndo_start_xmit = xgmac_xmit,
  1283. .ndo_stop = xgmac_stop,
  1284. .ndo_change_mtu = xgmac_change_mtu,
  1285. .ndo_set_rx_mode = xgmac_set_rx_mode,
  1286. .ndo_tx_timeout = xgmac_tx_timeout,
  1287. .ndo_get_stats64 = xgmac_get_stats64,
  1288. #ifdef CONFIG_NET_POLL_CONTROLLER
  1289. .ndo_poll_controller = xgmac_poll_controller,
  1290. #endif
  1291. .ndo_set_mac_address = xgmac_set_mac_address,
  1292. .ndo_set_features = xgmac_set_features,
  1293. };
  1294. static int xgmac_ethtool_getsettings(struct net_device *dev,
  1295. struct ethtool_cmd *cmd)
  1296. {
  1297. cmd->autoneg = 0;
  1298. cmd->duplex = DUPLEX_FULL;
  1299. ethtool_cmd_speed_set(cmd, 10000);
  1300. cmd->supported = 0;
  1301. cmd->advertising = 0;
  1302. cmd->transceiver = XCVR_INTERNAL;
  1303. return 0;
  1304. }
  1305. static void xgmac_get_pauseparam(struct net_device *netdev,
  1306. struct ethtool_pauseparam *pause)
  1307. {
  1308. struct xgmac_priv *priv = netdev_priv(netdev);
  1309. pause->rx_pause = priv->rx_pause;
  1310. pause->tx_pause = priv->tx_pause;
  1311. }
  1312. static int xgmac_set_pauseparam(struct net_device *netdev,
  1313. struct ethtool_pauseparam *pause)
  1314. {
  1315. struct xgmac_priv *priv = netdev_priv(netdev);
  1316. if (pause->autoneg)
  1317. return -EINVAL;
  1318. return xgmac_set_flow_ctrl(priv, pause->rx_pause, pause->tx_pause);
  1319. }
  1320. struct xgmac_stats {
  1321. char stat_string[ETH_GSTRING_LEN];
  1322. int stat_offset;
  1323. bool is_reg;
  1324. };
  1325. #define XGMAC_STAT(m) \
  1326. { #m, offsetof(struct xgmac_priv, xstats.m), false }
  1327. #define XGMAC_HW_STAT(m, reg_offset) \
  1328. { #m, reg_offset, true }
  1329. static const struct xgmac_stats xgmac_gstrings_stats[] = {
  1330. XGMAC_STAT(tx_frame_flushed),
  1331. XGMAC_STAT(tx_payload_error),
  1332. XGMAC_STAT(tx_ip_header_error),
  1333. XGMAC_STAT(tx_local_fault),
  1334. XGMAC_STAT(tx_remote_fault),
  1335. XGMAC_STAT(tx_early),
  1336. XGMAC_STAT(tx_process_stopped),
  1337. XGMAC_STAT(tx_jabber),
  1338. XGMAC_STAT(rx_buf_unav),
  1339. XGMAC_STAT(rx_process_stopped),
  1340. XGMAC_STAT(rx_payload_error),
  1341. XGMAC_STAT(rx_ip_header_error),
  1342. XGMAC_STAT(rx_da_filter_fail),
  1343. XGMAC_STAT(fatal_bus_error),
  1344. XGMAC_HW_STAT(rx_watchdog, XGMAC_MMC_RXWATCHDOG),
  1345. XGMAC_HW_STAT(tx_vlan, XGMAC_MMC_TXVLANFRAME),
  1346. XGMAC_HW_STAT(rx_vlan, XGMAC_MMC_RXVLANFRAME),
  1347. XGMAC_HW_STAT(tx_pause, XGMAC_MMC_TXPAUSEFRAME),
  1348. XGMAC_HW_STAT(rx_pause, XGMAC_MMC_RXPAUSEFRAME),
  1349. };
  1350. #define XGMAC_STATS_LEN ARRAY_SIZE(xgmac_gstrings_stats)
  1351. static void xgmac_get_ethtool_stats(struct net_device *dev,
  1352. struct ethtool_stats *dummy,
  1353. u64 *data)
  1354. {
  1355. struct xgmac_priv *priv = netdev_priv(dev);
  1356. void *p = priv;
  1357. int i;
  1358. for (i = 0; i < XGMAC_STATS_LEN; i++) {
  1359. if (xgmac_gstrings_stats[i].is_reg)
  1360. *data++ = readl(priv->base +
  1361. xgmac_gstrings_stats[i].stat_offset);
  1362. else
  1363. *data++ = *(u32 *)(p +
  1364. xgmac_gstrings_stats[i].stat_offset);
  1365. }
  1366. }
  1367. static int xgmac_get_sset_count(struct net_device *netdev, int sset)
  1368. {
  1369. switch (sset) {
  1370. case ETH_SS_STATS:
  1371. return XGMAC_STATS_LEN;
  1372. default:
  1373. return -EINVAL;
  1374. }
  1375. }
  1376. static void xgmac_get_strings(struct net_device *dev, u32 stringset,
  1377. u8 *data)
  1378. {
  1379. int i;
  1380. u8 *p = data;
  1381. switch (stringset) {
  1382. case ETH_SS_STATS:
  1383. for (i = 0; i < XGMAC_STATS_LEN; i++) {
  1384. memcpy(p, xgmac_gstrings_stats[i].stat_string,
  1385. ETH_GSTRING_LEN);
  1386. p += ETH_GSTRING_LEN;
  1387. }
  1388. break;
  1389. default:
  1390. WARN_ON(1);
  1391. break;
  1392. }
  1393. }
  1394. static void xgmac_get_wol(struct net_device *dev,
  1395. struct ethtool_wolinfo *wol)
  1396. {
  1397. struct xgmac_priv *priv = netdev_priv(dev);
  1398. if (device_can_wakeup(priv->device)) {
  1399. wol->supported = WAKE_MAGIC | WAKE_UCAST;
  1400. wol->wolopts = priv->wolopts;
  1401. }
  1402. }
  1403. static int xgmac_set_wol(struct net_device *dev,
  1404. struct ethtool_wolinfo *wol)
  1405. {
  1406. struct xgmac_priv *priv = netdev_priv(dev);
  1407. u32 support = WAKE_MAGIC | WAKE_UCAST;
  1408. if (!device_can_wakeup(priv->device))
  1409. return -ENOTSUPP;
  1410. if (wol->wolopts & ~support)
  1411. return -EINVAL;
  1412. priv->wolopts = wol->wolopts;
  1413. if (wol->wolopts) {
  1414. device_set_wakeup_enable(priv->device, 1);
  1415. enable_irq_wake(dev->irq);
  1416. } else {
  1417. device_set_wakeup_enable(priv->device, 0);
  1418. disable_irq_wake(dev->irq);
  1419. }
  1420. return 0;
  1421. }
  1422. static const struct ethtool_ops xgmac_ethtool_ops = {
  1423. .get_settings = xgmac_ethtool_getsettings,
  1424. .get_link = ethtool_op_get_link,
  1425. .get_pauseparam = xgmac_get_pauseparam,
  1426. .set_pauseparam = xgmac_set_pauseparam,
  1427. .get_ethtool_stats = xgmac_get_ethtool_stats,
  1428. .get_strings = xgmac_get_strings,
  1429. .get_wol = xgmac_get_wol,
  1430. .set_wol = xgmac_set_wol,
  1431. .get_sset_count = xgmac_get_sset_count,
  1432. };
  1433. /**
  1434. * xgmac_probe
  1435. * @pdev: platform device pointer
  1436. * Description: the driver is initialized through platform_device.
  1437. */
  1438. static int xgmac_probe(struct platform_device *pdev)
  1439. {
  1440. int ret = 0;
  1441. struct resource *res;
  1442. struct net_device *ndev = NULL;
  1443. struct xgmac_priv *priv = NULL;
  1444. u32 uid;
  1445. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1446. if (!res)
  1447. return -ENODEV;
  1448. if (!request_mem_region(res->start, resource_size(res), pdev->name))
  1449. return -EBUSY;
  1450. ndev = alloc_etherdev(sizeof(struct xgmac_priv));
  1451. if (!ndev) {
  1452. ret = -ENOMEM;
  1453. goto err_alloc;
  1454. }
  1455. SET_NETDEV_DEV(ndev, &pdev->dev);
  1456. priv = netdev_priv(ndev);
  1457. platform_set_drvdata(pdev, ndev);
  1458. ether_setup(ndev);
  1459. ndev->netdev_ops = &xgmac_netdev_ops;
  1460. SET_ETHTOOL_OPS(ndev, &xgmac_ethtool_ops);
  1461. spin_lock_init(&priv->stats_lock);
  1462. INIT_WORK(&priv->tx_timeout_work, xgmac_tx_timeout_work);
  1463. priv->device = &pdev->dev;
  1464. priv->dev = ndev;
  1465. priv->rx_pause = 1;
  1466. priv->tx_pause = 1;
  1467. priv->base = ioremap(res->start, resource_size(res));
  1468. if (!priv->base) {
  1469. netdev_err(ndev, "ioremap failed\n");
  1470. ret = -ENOMEM;
  1471. goto err_io;
  1472. }
  1473. uid = readl(priv->base + XGMAC_VERSION);
  1474. netdev_info(ndev, "h/w version is 0x%x\n", uid);
  1475. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  1476. ndev->irq = platform_get_irq(pdev, 0);
  1477. if (ndev->irq == -ENXIO) {
  1478. netdev_err(ndev, "No irq resource\n");
  1479. ret = ndev->irq;
  1480. goto err_irq;
  1481. }
  1482. ret = request_irq(ndev->irq, xgmac_interrupt, 0,
  1483. dev_name(&pdev->dev), ndev);
  1484. if (ret < 0) {
  1485. netdev_err(ndev, "Could not request irq %d - ret %d)\n",
  1486. ndev->irq, ret);
  1487. goto err_irq;
  1488. }
  1489. priv->pmt_irq = platform_get_irq(pdev, 1);
  1490. if (priv->pmt_irq == -ENXIO) {
  1491. netdev_err(ndev, "No pmt irq resource\n");
  1492. ret = priv->pmt_irq;
  1493. goto err_pmt_irq;
  1494. }
  1495. ret = request_irq(priv->pmt_irq, xgmac_pmt_interrupt, 0,
  1496. dev_name(&pdev->dev), ndev);
  1497. if (ret < 0) {
  1498. netdev_err(ndev, "Could not request irq %d - ret %d)\n",
  1499. priv->pmt_irq, ret);
  1500. goto err_pmt_irq;
  1501. }
  1502. device_set_wakeup_capable(&pdev->dev, 1);
  1503. if (device_can_wakeup(priv->device))
  1504. priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
  1505. ndev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA;
  1506. if (readl(priv->base + XGMAC_DMA_HW_FEATURE) & DMA_HW_FEAT_TXCOESEL)
  1507. ndev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1508. NETIF_F_RXCSUM;
  1509. ndev->features |= ndev->hw_features;
  1510. ndev->priv_flags |= IFF_UNICAST_FLT;
  1511. /* Get the MAC address */
  1512. xgmac_get_mac_addr(priv->base, ndev->dev_addr, 0);
  1513. if (!is_valid_ether_addr(ndev->dev_addr))
  1514. netdev_warn(ndev, "MAC address %pM not valid",
  1515. ndev->dev_addr);
  1516. netif_napi_add(ndev, &priv->napi, xgmac_poll, 64);
  1517. ret = register_netdev(ndev);
  1518. if (ret)
  1519. goto err_reg;
  1520. return 0;
  1521. err_reg:
  1522. netif_napi_del(&priv->napi);
  1523. free_irq(priv->pmt_irq, ndev);
  1524. err_pmt_irq:
  1525. free_irq(ndev->irq, ndev);
  1526. err_irq:
  1527. iounmap(priv->base);
  1528. err_io:
  1529. free_netdev(ndev);
  1530. err_alloc:
  1531. release_mem_region(res->start, resource_size(res));
  1532. return ret;
  1533. }
  1534. /**
  1535. * xgmac_dvr_remove
  1536. * @pdev: platform device pointer
  1537. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  1538. * changes the link status, releases the DMA descriptor rings,
  1539. * unregisters the MDIO bus and unmaps the allocated memory.
  1540. */
  1541. static int xgmac_remove(struct platform_device *pdev)
  1542. {
  1543. struct net_device *ndev = platform_get_drvdata(pdev);
  1544. struct xgmac_priv *priv = netdev_priv(ndev);
  1545. struct resource *res;
  1546. xgmac_mac_disable(priv->base);
  1547. /* Free the IRQ lines */
  1548. free_irq(ndev->irq, ndev);
  1549. free_irq(priv->pmt_irq, ndev);
  1550. unregister_netdev(ndev);
  1551. netif_napi_del(&priv->napi);
  1552. iounmap(priv->base);
  1553. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1554. release_mem_region(res->start, resource_size(res));
  1555. free_netdev(ndev);
  1556. return 0;
  1557. }
  1558. #ifdef CONFIG_PM_SLEEP
  1559. static void xgmac_pmt(void __iomem *ioaddr, unsigned long mode)
  1560. {
  1561. unsigned int pmt = 0;
  1562. if (mode & WAKE_MAGIC)
  1563. pmt |= XGMAC_PMT_POWERDOWN | XGMAC_PMT_MAGIC_PKT_EN;
  1564. if (mode & WAKE_UCAST)
  1565. pmt |= XGMAC_PMT_POWERDOWN | XGMAC_PMT_GLBL_UNICAST;
  1566. writel(pmt, ioaddr + XGMAC_PMT);
  1567. }
  1568. static int xgmac_suspend(struct device *dev)
  1569. {
  1570. struct net_device *ndev = platform_get_drvdata(to_platform_device(dev));
  1571. struct xgmac_priv *priv = netdev_priv(ndev);
  1572. u32 value;
  1573. if (!ndev || !netif_running(ndev))
  1574. return 0;
  1575. netif_device_detach(ndev);
  1576. napi_disable(&priv->napi);
  1577. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  1578. if (device_may_wakeup(priv->device)) {
  1579. /* Stop TX/RX DMA Only */
  1580. value = readl(priv->base + XGMAC_DMA_CONTROL);
  1581. value &= ~(DMA_CONTROL_ST | DMA_CONTROL_SR);
  1582. writel(value, priv->base + XGMAC_DMA_CONTROL);
  1583. xgmac_pmt(priv->base, priv->wolopts);
  1584. } else
  1585. xgmac_mac_disable(priv->base);
  1586. return 0;
  1587. }
  1588. static int xgmac_resume(struct device *dev)
  1589. {
  1590. struct net_device *ndev = platform_get_drvdata(to_platform_device(dev));
  1591. struct xgmac_priv *priv = netdev_priv(ndev);
  1592. void __iomem *ioaddr = priv->base;
  1593. if (!netif_running(ndev))
  1594. return 0;
  1595. xgmac_pmt(ioaddr, 0);
  1596. /* Enable the MAC and DMA */
  1597. xgmac_mac_enable(ioaddr);
  1598. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_STATUS);
  1599. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_INTR_ENA);
  1600. netif_device_attach(ndev);
  1601. napi_enable(&priv->napi);
  1602. return 0;
  1603. }
  1604. #endif /* CONFIG_PM_SLEEP */
  1605. static SIMPLE_DEV_PM_OPS(xgmac_pm_ops, xgmac_suspend, xgmac_resume);
  1606. static const struct of_device_id xgmac_of_match[] = {
  1607. { .compatible = "calxeda,hb-xgmac", },
  1608. {},
  1609. };
  1610. MODULE_DEVICE_TABLE(of, xgmac_of_match);
  1611. static struct platform_driver xgmac_driver = {
  1612. .driver = {
  1613. .name = "calxedaxgmac",
  1614. .of_match_table = xgmac_of_match,
  1615. },
  1616. .probe = xgmac_probe,
  1617. .remove = xgmac_remove,
  1618. .driver.pm = &xgmac_pm_ops,
  1619. };
  1620. module_platform_driver(xgmac_driver);
  1621. MODULE_AUTHOR("Calxeda, Inc.");
  1622. MODULE_DESCRIPTION("Calxeda 10G XGMAC driver");
  1623. MODULE_LICENSE("GPL v2");