davinci_emac.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. /*
  2. * DaVinci Ethernet Medium Access Controller
  3. *
  4. * DaVinci EMAC is based upon CPPI 3.0 TI DMA engine
  5. *
  6. * Copyright (C) 2009 Texas Instruments.
  7. *
  8. * ---------------------------------------------------------------------------
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. * ---------------------------------------------------------------------------
  24. * History:
  25. * 0-5 A number of folks worked on this driver in bits and pieces but the major
  26. * contribution came from Suraj Iyer and Anant Gole
  27. * 6.0 Anant Gole - rewrote the driver as per Linux conventions
  28. * 6.1 Chaithrika U S - added support for Gigabit and RMII features,
  29. * PHY layer usage
  30. */
  31. #include <linux/module.h>
  32. #include <linux/kernel.h>
  33. #include <linux/sched.h>
  34. #include <linux/string.h>
  35. #include <linux/timer.h>
  36. #include <linux/errno.h>
  37. #include <linux/in.h>
  38. #include <linux/ioport.h>
  39. #include <linux/slab.h>
  40. #include <linux/mm.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/init.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/etherdevice.h>
  45. #include <linux/skbuff.h>
  46. #include <linux/ethtool.h>
  47. #include <linux/highmem.h>
  48. #include <linux/proc_fs.h>
  49. #include <linux/ctype.h>
  50. #include <linux/spinlock.h>
  51. #include <linux/dma-mapping.h>
  52. #include <linux/clk.h>
  53. #include <linux/platform_device.h>
  54. #include <linux/semaphore.h>
  55. #include <linux/phy.h>
  56. #include <linux/bitops.h>
  57. #include <linux/io.h>
  58. #include <linux/uaccess.h>
  59. #include <linux/davinci_emac.h>
  60. #include <asm/irq.h>
  61. #include <asm/page.h>
  62. #include "davinci_cpdma.h"
  63. static int debug_level;
  64. module_param(debug_level, int, 0);
  65. MODULE_PARM_DESC(debug_level, "DaVinci EMAC debug level (NETIF_MSG bits)");
  66. /* Netif debug messages possible */
  67. #define DAVINCI_EMAC_DEBUG (NETIF_MSG_DRV | \
  68. NETIF_MSG_PROBE | \
  69. NETIF_MSG_LINK | \
  70. NETIF_MSG_TIMER | \
  71. NETIF_MSG_IFDOWN | \
  72. NETIF_MSG_IFUP | \
  73. NETIF_MSG_RX_ERR | \
  74. NETIF_MSG_TX_ERR | \
  75. NETIF_MSG_TX_QUEUED | \
  76. NETIF_MSG_INTR | \
  77. NETIF_MSG_TX_DONE | \
  78. NETIF_MSG_RX_STATUS | \
  79. NETIF_MSG_PKTDATA | \
  80. NETIF_MSG_HW | \
  81. NETIF_MSG_WOL)
  82. /* version info */
  83. #define EMAC_MAJOR_VERSION 6
  84. #define EMAC_MINOR_VERSION 1
  85. #define EMAC_MODULE_VERSION "6.1"
  86. MODULE_VERSION(EMAC_MODULE_VERSION);
  87. static const char emac_version_string[] = "TI DaVinci EMAC Linux v6.1";
  88. /* Configuration items */
  89. #define EMAC_DEF_PASS_CRC (0) /* Do not pass CRC up to frames */
  90. #define EMAC_DEF_QOS_EN (0) /* EMAC proprietary QoS disabled */
  91. #define EMAC_DEF_NO_BUFF_CHAIN (0) /* No buffer chain */
  92. #define EMAC_DEF_MACCTRL_FRAME_EN (0) /* Discard Maccontrol frames */
  93. #define EMAC_DEF_SHORT_FRAME_EN (0) /* Discard short frames */
  94. #define EMAC_DEF_ERROR_FRAME_EN (0) /* Discard error frames */
  95. #define EMAC_DEF_PROM_EN (0) /* Promiscuous disabled */
  96. #define EMAC_DEF_PROM_CH (0) /* Promiscuous channel is 0 */
  97. #define EMAC_DEF_BCAST_EN (1) /* Broadcast enabled */
  98. #define EMAC_DEF_BCAST_CH (0) /* Broadcast channel is 0 */
  99. #define EMAC_DEF_MCAST_EN (1) /* Multicast enabled */
  100. #define EMAC_DEF_MCAST_CH (0) /* Multicast channel is 0 */
  101. #define EMAC_DEF_TXPRIO_FIXED (1) /* TX Priority is fixed */
  102. #define EMAC_DEF_TXPACING_EN (0) /* TX pacing NOT supported*/
  103. #define EMAC_DEF_BUFFER_OFFSET (0) /* Buffer offset to DMA (future) */
  104. #define EMAC_DEF_MIN_ETHPKTSIZE (60) /* Minimum ethernet pkt size */
  105. #define EMAC_DEF_MAX_FRAME_SIZE (1500 + 14 + 4 + 4)
  106. #define EMAC_DEF_TX_CH (0) /* Default 0th channel */
  107. #define EMAC_DEF_RX_CH (0) /* Default 0th channel */
  108. #define EMAC_DEF_RX_NUM_DESC (128)
  109. #define EMAC_DEF_TX_NUM_DESC (128)
  110. #define EMAC_DEF_MAX_TX_CH (1) /* Max TX channels configured */
  111. #define EMAC_DEF_MAX_RX_CH (1) /* Max RX channels configured */
  112. #define EMAC_POLL_WEIGHT (64) /* Default NAPI poll weight */
  113. /* Buffer descriptor parameters */
  114. #define EMAC_DEF_TX_MAX_SERVICE (32) /* TX max service BD's */
  115. #define EMAC_DEF_RX_MAX_SERVICE (64) /* should = netdev->weight */
  116. /* EMAC register related defines */
  117. #define EMAC_ALL_MULTI_REG_VALUE (0xFFFFFFFF)
  118. #define EMAC_NUM_MULTICAST_BITS (64)
  119. #define EMAC_TX_CONTROL_TX_ENABLE_VAL (0x1)
  120. #define EMAC_RX_CONTROL_RX_ENABLE_VAL (0x1)
  121. #define EMAC_MAC_HOST_ERR_INTMASK_VAL (0x2)
  122. #define EMAC_RX_UNICAST_CLEAR_ALL (0xFF)
  123. #define EMAC_INT_MASK_CLEAR (0xFF)
  124. /* RX MBP register bit positions */
  125. #define EMAC_RXMBP_PASSCRC_MASK BIT(30)
  126. #define EMAC_RXMBP_QOSEN_MASK BIT(29)
  127. #define EMAC_RXMBP_NOCHAIN_MASK BIT(28)
  128. #define EMAC_RXMBP_CMFEN_MASK BIT(24)
  129. #define EMAC_RXMBP_CSFEN_MASK BIT(23)
  130. #define EMAC_RXMBP_CEFEN_MASK BIT(22)
  131. #define EMAC_RXMBP_CAFEN_MASK BIT(21)
  132. #define EMAC_RXMBP_PROMCH_SHIFT (16)
  133. #define EMAC_RXMBP_PROMCH_MASK (0x7 << 16)
  134. #define EMAC_RXMBP_BROADEN_MASK BIT(13)
  135. #define EMAC_RXMBP_BROADCH_SHIFT (8)
  136. #define EMAC_RXMBP_BROADCH_MASK (0x7 << 8)
  137. #define EMAC_RXMBP_MULTIEN_MASK BIT(5)
  138. #define EMAC_RXMBP_MULTICH_SHIFT (0)
  139. #define EMAC_RXMBP_MULTICH_MASK (0x7)
  140. #define EMAC_RXMBP_CHMASK (0x7)
  141. /* EMAC register definitions/bit maps used */
  142. # define EMAC_MBP_RXPROMISC (0x00200000)
  143. # define EMAC_MBP_PROMISCCH(ch) (((ch) & 0x7) << 16)
  144. # define EMAC_MBP_RXBCAST (0x00002000)
  145. # define EMAC_MBP_BCASTCHAN(ch) (((ch) & 0x7) << 8)
  146. # define EMAC_MBP_RXMCAST (0x00000020)
  147. # define EMAC_MBP_MCASTCHAN(ch) ((ch) & 0x7)
  148. /* EMAC mac_control register */
  149. #define EMAC_MACCONTROL_TXPTYPE BIT(9)
  150. #define EMAC_MACCONTROL_TXPACEEN BIT(6)
  151. #define EMAC_MACCONTROL_GMIIEN BIT(5)
  152. #define EMAC_MACCONTROL_GIGABITEN BIT(7)
  153. #define EMAC_MACCONTROL_FULLDUPLEXEN BIT(0)
  154. #define EMAC_MACCONTROL_RMIISPEED_MASK BIT(15)
  155. /* GIGABIT MODE related bits */
  156. #define EMAC_DM646X_MACCONTORL_GIG BIT(7)
  157. #define EMAC_DM646X_MACCONTORL_GIGFORCE BIT(17)
  158. /* EMAC mac_status register */
  159. #define EMAC_MACSTATUS_TXERRCODE_MASK (0xF00000)
  160. #define EMAC_MACSTATUS_TXERRCODE_SHIFT (20)
  161. #define EMAC_MACSTATUS_TXERRCH_MASK (0x7)
  162. #define EMAC_MACSTATUS_TXERRCH_SHIFT (16)
  163. #define EMAC_MACSTATUS_RXERRCODE_MASK (0xF000)
  164. #define EMAC_MACSTATUS_RXERRCODE_SHIFT (12)
  165. #define EMAC_MACSTATUS_RXERRCH_MASK (0x7)
  166. #define EMAC_MACSTATUS_RXERRCH_SHIFT (8)
  167. /* EMAC RX register masks */
  168. #define EMAC_RX_MAX_LEN_MASK (0xFFFF)
  169. #define EMAC_RX_BUFFER_OFFSET_MASK (0xFFFF)
  170. /* MAC_IN_VECTOR (0x180) register bit fields */
  171. #define EMAC_DM644X_MAC_IN_VECTOR_HOST_INT BIT(17)
  172. #define EMAC_DM644X_MAC_IN_VECTOR_STATPEND_INT BIT(16)
  173. #define EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC BIT(8)
  174. #define EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC BIT(0)
  175. /** NOTE:: For DM646x the IN_VECTOR has changed */
  176. #define EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC BIT(EMAC_DEF_RX_CH)
  177. #define EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC BIT(16 + EMAC_DEF_TX_CH)
  178. #define EMAC_DM646X_MAC_IN_VECTOR_HOST_INT BIT(26)
  179. #define EMAC_DM646X_MAC_IN_VECTOR_STATPEND_INT BIT(27)
  180. /* CPPI bit positions */
  181. #define EMAC_CPPI_SOP_BIT BIT(31)
  182. #define EMAC_CPPI_EOP_BIT BIT(30)
  183. #define EMAC_CPPI_OWNERSHIP_BIT BIT(29)
  184. #define EMAC_CPPI_EOQ_BIT BIT(28)
  185. #define EMAC_CPPI_TEARDOWN_COMPLETE_BIT BIT(27)
  186. #define EMAC_CPPI_PASS_CRC_BIT BIT(26)
  187. #define EMAC_RX_BD_BUF_SIZE (0xFFFF)
  188. #define EMAC_BD_LENGTH_FOR_CACHE (16) /* only CPPI bytes */
  189. #define EMAC_RX_BD_PKT_LENGTH_MASK (0xFFFF)
  190. /* Max hardware defines */
  191. #define EMAC_MAX_TXRX_CHANNELS (8) /* Max hardware channels */
  192. #define EMAC_DEF_MAX_MULTICAST_ADDRESSES (64) /* Max mcast addr's */
  193. /* EMAC Peripheral Device Register Memory Layout structure */
  194. #define EMAC_MACINVECTOR 0x90
  195. #define EMAC_DM646X_MACEOIVECTOR 0x94
  196. #define EMAC_MACINTSTATRAW 0xB0
  197. #define EMAC_MACINTSTATMASKED 0xB4
  198. #define EMAC_MACINTMASKSET 0xB8
  199. #define EMAC_MACINTMASKCLEAR 0xBC
  200. #define EMAC_RXMBPENABLE 0x100
  201. #define EMAC_RXUNICASTSET 0x104
  202. #define EMAC_RXUNICASTCLEAR 0x108
  203. #define EMAC_RXMAXLEN 0x10C
  204. #define EMAC_RXBUFFEROFFSET 0x110
  205. #define EMAC_RXFILTERLOWTHRESH 0x114
  206. #define EMAC_MACCONTROL 0x160
  207. #define EMAC_MACSTATUS 0x164
  208. #define EMAC_EMCONTROL 0x168
  209. #define EMAC_FIFOCONTROL 0x16C
  210. #define EMAC_MACCONFIG 0x170
  211. #define EMAC_SOFTRESET 0x174
  212. #define EMAC_MACSRCADDRLO 0x1D0
  213. #define EMAC_MACSRCADDRHI 0x1D4
  214. #define EMAC_MACHASH1 0x1D8
  215. #define EMAC_MACHASH2 0x1DC
  216. #define EMAC_MACADDRLO 0x500
  217. #define EMAC_MACADDRHI 0x504
  218. #define EMAC_MACINDEX 0x508
  219. /* EMAC statistics registers */
  220. #define EMAC_RXGOODFRAMES 0x200
  221. #define EMAC_RXBCASTFRAMES 0x204
  222. #define EMAC_RXMCASTFRAMES 0x208
  223. #define EMAC_RXPAUSEFRAMES 0x20C
  224. #define EMAC_RXCRCERRORS 0x210
  225. #define EMAC_RXALIGNCODEERRORS 0x214
  226. #define EMAC_RXOVERSIZED 0x218
  227. #define EMAC_RXJABBER 0x21C
  228. #define EMAC_RXUNDERSIZED 0x220
  229. #define EMAC_RXFRAGMENTS 0x224
  230. #define EMAC_RXFILTERED 0x228
  231. #define EMAC_RXQOSFILTERED 0x22C
  232. #define EMAC_RXOCTETS 0x230
  233. #define EMAC_TXGOODFRAMES 0x234
  234. #define EMAC_TXBCASTFRAMES 0x238
  235. #define EMAC_TXMCASTFRAMES 0x23C
  236. #define EMAC_TXPAUSEFRAMES 0x240
  237. #define EMAC_TXDEFERRED 0x244
  238. #define EMAC_TXCOLLISION 0x248
  239. #define EMAC_TXSINGLECOLL 0x24C
  240. #define EMAC_TXMULTICOLL 0x250
  241. #define EMAC_TXEXCESSIVECOLL 0x254
  242. #define EMAC_TXLATECOLL 0x258
  243. #define EMAC_TXUNDERRUN 0x25C
  244. #define EMAC_TXCARRIERSENSE 0x260
  245. #define EMAC_TXOCTETS 0x264
  246. #define EMAC_NETOCTETS 0x280
  247. #define EMAC_RXSOFOVERRUNS 0x284
  248. #define EMAC_RXMOFOVERRUNS 0x288
  249. #define EMAC_RXDMAOVERRUNS 0x28C
  250. /* EMAC DM644x control registers */
  251. #define EMAC_CTRL_EWCTL (0x4)
  252. #define EMAC_CTRL_EWINTTCNT (0x8)
  253. /* EMAC DM644x control module masks */
  254. #define EMAC_DM644X_EWINTCNT_MASK 0x1FFFF
  255. #define EMAC_DM644X_INTMIN_INTVL 0x1
  256. #define EMAC_DM644X_INTMAX_INTVL (EMAC_DM644X_EWINTCNT_MASK)
  257. /* EMAC DM646X control module registers */
  258. #define EMAC_DM646X_CMINTCTRL 0x0C
  259. #define EMAC_DM646X_CMRXINTEN 0x14
  260. #define EMAC_DM646X_CMTXINTEN 0x18
  261. #define EMAC_DM646X_CMRXINTMAX 0x70
  262. #define EMAC_DM646X_CMTXINTMAX 0x74
  263. /* EMAC DM646X control module masks */
  264. #define EMAC_DM646X_INTPACEEN (0x3 << 16)
  265. #define EMAC_DM646X_INTPRESCALE_MASK (0x7FF << 0)
  266. #define EMAC_DM646X_CMINTMAX_CNT 63
  267. #define EMAC_DM646X_CMINTMIN_CNT 2
  268. #define EMAC_DM646X_CMINTMAX_INTVL (1000 / EMAC_DM646X_CMINTMIN_CNT)
  269. #define EMAC_DM646X_CMINTMIN_INTVL ((1000 / EMAC_DM646X_CMINTMAX_CNT) + 1)
  270. /* EMAC EOI codes for C0 */
  271. #define EMAC_DM646X_MAC_EOI_C0_RXEN (0x01)
  272. #define EMAC_DM646X_MAC_EOI_C0_TXEN (0x02)
  273. /* EMAC Stats Clear Mask */
  274. #define EMAC_STATS_CLR_MASK (0xFFFFFFFF)
  275. /* emac_priv: EMAC private data structure
  276. *
  277. * EMAC adapter private data structure
  278. */
  279. struct emac_priv {
  280. u32 msg_enable;
  281. struct net_device *ndev;
  282. struct platform_device *pdev;
  283. struct napi_struct napi;
  284. char mac_addr[6];
  285. void __iomem *remap_addr;
  286. u32 emac_base_phys;
  287. void __iomem *emac_base;
  288. void __iomem *ctrl_base;
  289. struct cpdma_ctlr *dma;
  290. struct cpdma_chan *txchan;
  291. struct cpdma_chan *rxchan;
  292. u32 link; /* 1=link on, 0=link off */
  293. u32 speed; /* 0=Auto Neg, 1=No PHY, 10,100, 1000 - mbps */
  294. u32 duplex; /* Link duplex: 0=Half, 1=Full */
  295. u32 rx_buf_size;
  296. u32 isr_count;
  297. u32 coal_intvl;
  298. u32 bus_freq_mhz;
  299. u8 rmii_en;
  300. u8 version;
  301. u32 mac_hash1;
  302. u32 mac_hash2;
  303. u32 multicast_hash_cnt[EMAC_NUM_MULTICAST_BITS];
  304. u32 rx_addr_type;
  305. atomic_t cur_tx;
  306. const char *phy_id;
  307. struct phy_device *phydev;
  308. spinlock_t lock;
  309. /*platform specific members*/
  310. void (*int_enable) (void);
  311. void (*int_disable) (void);
  312. };
  313. /* clock frequency for EMAC */
  314. static struct clk *emac_clk;
  315. static unsigned long emac_bus_frequency;
  316. /* EMAC TX Host Error description strings */
  317. static char *emac_txhost_errcodes[16] = {
  318. "No error", "SOP error", "Ownership bit not set in SOP buffer",
  319. "Zero Next Buffer Descriptor Pointer Without EOP",
  320. "Zero Buffer Pointer", "Zero Buffer Length", "Packet Length Error",
  321. "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
  322. "Reserved", "Reserved", "Reserved", "Reserved"
  323. };
  324. /* EMAC RX Host Error description strings */
  325. static char *emac_rxhost_errcodes[16] = {
  326. "No error", "Reserved", "Ownership bit not set in input buffer",
  327. "Reserved", "Zero Buffer Pointer", "Reserved", "Reserved",
  328. "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
  329. "Reserved", "Reserved", "Reserved", "Reserved"
  330. };
  331. /* Helper macros */
  332. #define emac_read(reg) ioread32(priv->emac_base + (reg))
  333. #define emac_write(reg, val) iowrite32(val, priv->emac_base + (reg))
  334. #define emac_ctrl_read(reg) ioread32((priv->ctrl_base + (reg)))
  335. #define emac_ctrl_write(reg, val) iowrite32(val, (priv->ctrl_base + (reg)))
  336. /**
  337. * emac_dump_regs: Dump important EMAC registers to debug terminal
  338. * @priv: The DaVinci EMAC private adapter structure
  339. *
  340. * Executes ethtool set cmd & sets phy mode
  341. *
  342. */
  343. static void emac_dump_regs(struct emac_priv *priv)
  344. {
  345. struct device *emac_dev = &priv->ndev->dev;
  346. /* Print important registers in EMAC */
  347. dev_info(emac_dev, "EMAC Basic registers\n");
  348. if (priv->version == EMAC_VERSION_1) {
  349. dev_info(emac_dev, "EMAC: EWCTL: %08X, EWINTTCNT: %08X\n",
  350. emac_ctrl_read(EMAC_CTRL_EWCTL),
  351. emac_ctrl_read(EMAC_CTRL_EWINTTCNT));
  352. }
  353. dev_info(emac_dev, "EMAC: EmuControl:%08X, FifoControl: %08X\n",
  354. emac_read(EMAC_EMCONTROL), emac_read(EMAC_FIFOCONTROL));
  355. dev_info(emac_dev, "EMAC: MBPEnable:%08X, RXUnicastSet: %08X, "\
  356. "RXMaxLen=%08X\n", emac_read(EMAC_RXMBPENABLE),
  357. emac_read(EMAC_RXUNICASTSET), emac_read(EMAC_RXMAXLEN));
  358. dev_info(emac_dev, "EMAC: MacControl:%08X, MacStatus: %08X, "\
  359. "MacConfig=%08X\n", emac_read(EMAC_MACCONTROL),
  360. emac_read(EMAC_MACSTATUS), emac_read(EMAC_MACCONFIG));
  361. dev_info(emac_dev, "EMAC Statistics\n");
  362. dev_info(emac_dev, "EMAC: rx_good_frames:%d\n",
  363. emac_read(EMAC_RXGOODFRAMES));
  364. dev_info(emac_dev, "EMAC: rx_broadcast_frames:%d\n",
  365. emac_read(EMAC_RXBCASTFRAMES));
  366. dev_info(emac_dev, "EMAC: rx_multicast_frames:%d\n",
  367. emac_read(EMAC_RXMCASTFRAMES));
  368. dev_info(emac_dev, "EMAC: rx_pause_frames:%d\n",
  369. emac_read(EMAC_RXPAUSEFRAMES));
  370. dev_info(emac_dev, "EMAC: rx_crcerrors:%d\n",
  371. emac_read(EMAC_RXCRCERRORS));
  372. dev_info(emac_dev, "EMAC: rx_align_code_errors:%d\n",
  373. emac_read(EMAC_RXALIGNCODEERRORS));
  374. dev_info(emac_dev, "EMAC: rx_oversized_frames:%d\n",
  375. emac_read(EMAC_RXOVERSIZED));
  376. dev_info(emac_dev, "EMAC: rx_jabber_frames:%d\n",
  377. emac_read(EMAC_RXJABBER));
  378. dev_info(emac_dev, "EMAC: rx_undersized_frames:%d\n",
  379. emac_read(EMAC_RXUNDERSIZED));
  380. dev_info(emac_dev, "EMAC: rx_fragments:%d\n",
  381. emac_read(EMAC_RXFRAGMENTS));
  382. dev_info(emac_dev, "EMAC: rx_filtered_frames:%d\n",
  383. emac_read(EMAC_RXFILTERED));
  384. dev_info(emac_dev, "EMAC: rx_qos_filtered_frames:%d\n",
  385. emac_read(EMAC_RXQOSFILTERED));
  386. dev_info(emac_dev, "EMAC: rx_octets:%d\n",
  387. emac_read(EMAC_RXOCTETS));
  388. dev_info(emac_dev, "EMAC: tx_goodframes:%d\n",
  389. emac_read(EMAC_TXGOODFRAMES));
  390. dev_info(emac_dev, "EMAC: tx_bcastframes:%d\n",
  391. emac_read(EMAC_TXBCASTFRAMES));
  392. dev_info(emac_dev, "EMAC: tx_mcastframes:%d\n",
  393. emac_read(EMAC_TXMCASTFRAMES));
  394. dev_info(emac_dev, "EMAC: tx_pause_frames:%d\n",
  395. emac_read(EMAC_TXPAUSEFRAMES));
  396. dev_info(emac_dev, "EMAC: tx_deferred_frames:%d\n",
  397. emac_read(EMAC_TXDEFERRED));
  398. dev_info(emac_dev, "EMAC: tx_collision_frames:%d\n",
  399. emac_read(EMAC_TXCOLLISION));
  400. dev_info(emac_dev, "EMAC: tx_single_coll_frames:%d\n",
  401. emac_read(EMAC_TXSINGLECOLL));
  402. dev_info(emac_dev, "EMAC: tx_mult_coll_frames:%d\n",
  403. emac_read(EMAC_TXMULTICOLL));
  404. dev_info(emac_dev, "EMAC: tx_excessive_collisions:%d\n",
  405. emac_read(EMAC_TXEXCESSIVECOLL));
  406. dev_info(emac_dev, "EMAC: tx_late_collisions:%d\n",
  407. emac_read(EMAC_TXLATECOLL));
  408. dev_info(emac_dev, "EMAC: tx_underrun:%d\n",
  409. emac_read(EMAC_TXUNDERRUN));
  410. dev_info(emac_dev, "EMAC: tx_carrier_sense_errors:%d\n",
  411. emac_read(EMAC_TXCARRIERSENSE));
  412. dev_info(emac_dev, "EMAC: tx_octets:%d\n",
  413. emac_read(EMAC_TXOCTETS));
  414. dev_info(emac_dev, "EMAC: net_octets:%d\n",
  415. emac_read(EMAC_NETOCTETS));
  416. dev_info(emac_dev, "EMAC: rx_sof_overruns:%d\n",
  417. emac_read(EMAC_RXSOFOVERRUNS));
  418. dev_info(emac_dev, "EMAC: rx_mof_overruns:%d\n",
  419. emac_read(EMAC_RXMOFOVERRUNS));
  420. dev_info(emac_dev, "EMAC: rx_dma_overruns:%d\n",
  421. emac_read(EMAC_RXDMAOVERRUNS));
  422. cpdma_ctlr_dump(priv->dma);
  423. }
  424. /**
  425. * emac_get_drvinfo: Get EMAC driver information
  426. * @ndev: The DaVinci EMAC network adapter
  427. * @info: ethtool info structure containing name and version
  428. *
  429. * Returns EMAC driver information (name and version)
  430. *
  431. */
  432. static void emac_get_drvinfo(struct net_device *ndev,
  433. struct ethtool_drvinfo *info)
  434. {
  435. strcpy(info->driver, emac_version_string);
  436. strcpy(info->version, EMAC_MODULE_VERSION);
  437. }
  438. /**
  439. * emac_get_settings: Get EMAC settings
  440. * @ndev: The DaVinci EMAC network adapter
  441. * @ecmd: ethtool command
  442. *
  443. * Executes ethool get command
  444. *
  445. */
  446. static int emac_get_settings(struct net_device *ndev,
  447. struct ethtool_cmd *ecmd)
  448. {
  449. struct emac_priv *priv = netdev_priv(ndev);
  450. if (priv->phydev)
  451. return phy_ethtool_gset(priv->phydev, ecmd);
  452. else
  453. return -EOPNOTSUPP;
  454. }
  455. /**
  456. * emac_set_settings: Set EMAC settings
  457. * @ndev: The DaVinci EMAC network adapter
  458. * @ecmd: ethtool command
  459. *
  460. * Executes ethool set command
  461. *
  462. */
  463. static int emac_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
  464. {
  465. struct emac_priv *priv = netdev_priv(ndev);
  466. if (priv->phydev)
  467. return phy_ethtool_sset(priv->phydev, ecmd);
  468. else
  469. return -EOPNOTSUPP;
  470. }
  471. /**
  472. * emac_get_coalesce : Get interrupt coalesce settings for this device
  473. * @ndev : The DaVinci EMAC network adapter
  474. * @coal : ethtool coalesce settings structure
  475. *
  476. * Fetch the current interrupt coalesce settings
  477. *
  478. */
  479. static int emac_get_coalesce(struct net_device *ndev,
  480. struct ethtool_coalesce *coal)
  481. {
  482. struct emac_priv *priv = netdev_priv(ndev);
  483. coal->rx_coalesce_usecs = priv->coal_intvl;
  484. return 0;
  485. }
  486. /**
  487. * emac_set_coalesce : Set interrupt coalesce settings for this device
  488. * @ndev : The DaVinci EMAC network adapter
  489. * @coal : ethtool coalesce settings structure
  490. *
  491. * Set interrupt coalesce parameters
  492. *
  493. */
  494. static int emac_set_coalesce(struct net_device *ndev,
  495. struct ethtool_coalesce *coal)
  496. {
  497. struct emac_priv *priv = netdev_priv(ndev);
  498. u32 int_ctrl, num_interrupts = 0;
  499. u32 prescale = 0, addnl_dvdr = 1, coal_intvl = 0;
  500. if (!coal->rx_coalesce_usecs)
  501. return -EINVAL;
  502. coal_intvl = coal->rx_coalesce_usecs;
  503. switch (priv->version) {
  504. case EMAC_VERSION_2:
  505. int_ctrl = emac_ctrl_read(EMAC_DM646X_CMINTCTRL);
  506. prescale = priv->bus_freq_mhz * 4;
  507. if (coal_intvl < EMAC_DM646X_CMINTMIN_INTVL)
  508. coal_intvl = EMAC_DM646X_CMINTMIN_INTVL;
  509. if (coal_intvl > EMAC_DM646X_CMINTMAX_INTVL) {
  510. /*
  511. * Interrupt pacer works with 4us Pulse, we can
  512. * throttle further by dilating the 4us pulse.
  513. */
  514. addnl_dvdr = EMAC_DM646X_INTPRESCALE_MASK / prescale;
  515. if (addnl_dvdr > 1) {
  516. prescale *= addnl_dvdr;
  517. if (coal_intvl > (EMAC_DM646X_CMINTMAX_INTVL
  518. * addnl_dvdr))
  519. coal_intvl = (EMAC_DM646X_CMINTMAX_INTVL
  520. * addnl_dvdr);
  521. } else {
  522. addnl_dvdr = 1;
  523. coal_intvl = EMAC_DM646X_CMINTMAX_INTVL;
  524. }
  525. }
  526. num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
  527. int_ctrl |= EMAC_DM646X_INTPACEEN;
  528. int_ctrl &= (~EMAC_DM646X_INTPRESCALE_MASK);
  529. int_ctrl |= (prescale & EMAC_DM646X_INTPRESCALE_MASK);
  530. emac_ctrl_write(EMAC_DM646X_CMINTCTRL, int_ctrl);
  531. emac_ctrl_write(EMAC_DM646X_CMRXINTMAX, num_interrupts);
  532. emac_ctrl_write(EMAC_DM646X_CMTXINTMAX, num_interrupts);
  533. break;
  534. default:
  535. int_ctrl = emac_ctrl_read(EMAC_CTRL_EWINTTCNT);
  536. int_ctrl &= (~EMAC_DM644X_EWINTCNT_MASK);
  537. prescale = coal_intvl * priv->bus_freq_mhz;
  538. if (prescale > EMAC_DM644X_EWINTCNT_MASK) {
  539. prescale = EMAC_DM644X_EWINTCNT_MASK;
  540. coal_intvl = prescale / priv->bus_freq_mhz;
  541. }
  542. emac_ctrl_write(EMAC_CTRL_EWINTTCNT, (int_ctrl | prescale));
  543. break;
  544. }
  545. printk(KERN_INFO"Set coalesce to %d usecs.\n", coal_intvl);
  546. priv->coal_intvl = coal_intvl;
  547. return 0;
  548. }
  549. /**
  550. * ethtool_ops: DaVinci EMAC Ethtool structure
  551. *
  552. * Ethtool support for EMAC adapter
  553. *
  554. */
  555. static const struct ethtool_ops ethtool_ops = {
  556. .get_drvinfo = emac_get_drvinfo,
  557. .get_settings = emac_get_settings,
  558. .set_settings = emac_set_settings,
  559. .get_link = ethtool_op_get_link,
  560. .get_coalesce = emac_get_coalesce,
  561. .set_coalesce = emac_set_coalesce,
  562. };
  563. /**
  564. * emac_update_phystatus: Update Phy status
  565. * @priv: The DaVinci EMAC private adapter structure
  566. *
  567. * Updates phy status and takes action for network queue if required
  568. * based upon link status
  569. *
  570. */
  571. static void emac_update_phystatus(struct emac_priv *priv)
  572. {
  573. u32 mac_control;
  574. u32 new_duplex;
  575. u32 cur_duplex;
  576. struct net_device *ndev = priv->ndev;
  577. mac_control = emac_read(EMAC_MACCONTROL);
  578. cur_duplex = (mac_control & EMAC_MACCONTROL_FULLDUPLEXEN) ?
  579. DUPLEX_FULL : DUPLEX_HALF;
  580. if (priv->phydev)
  581. new_duplex = priv->phydev->duplex;
  582. else
  583. new_duplex = DUPLEX_FULL;
  584. /* We get called only if link has changed (speed/duplex/status) */
  585. if ((priv->link) && (new_duplex != cur_duplex)) {
  586. priv->duplex = new_duplex;
  587. if (DUPLEX_FULL == priv->duplex)
  588. mac_control |= (EMAC_MACCONTROL_FULLDUPLEXEN);
  589. else
  590. mac_control &= ~(EMAC_MACCONTROL_FULLDUPLEXEN);
  591. }
  592. if (priv->speed == SPEED_1000 && (priv->version == EMAC_VERSION_2)) {
  593. mac_control = emac_read(EMAC_MACCONTROL);
  594. mac_control |= (EMAC_DM646X_MACCONTORL_GIG |
  595. EMAC_DM646X_MACCONTORL_GIGFORCE);
  596. } else {
  597. /* Clear the GIG bit and GIGFORCE bit */
  598. mac_control &= ~(EMAC_DM646X_MACCONTORL_GIGFORCE |
  599. EMAC_DM646X_MACCONTORL_GIG);
  600. if (priv->rmii_en && (priv->speed == SPEED_100))
  601. mac_control |= EMAC_MACCONTROL_RMIISPEED_MASK;
  602. else
  603. mac_control &= ~EMAC_MACCONTROL_RMIISPEED_MASK;
  604. }
  605. /* Update mac_control if changed */
  606. emac_write(EMAC_MACCONTROL, mac_control);
  607. if (priv->link) {
  608. /* link ON */
  609. if (!netif_carrier_ok(ndev))
  610. netif_carrier_on(ndev);
  611. /* reactivate the transmit queue if it is stopped */
  612. if (netif_running(ndev) && netif_queue_stopped(ndev))
  613. netif_wake_queue(ndev);
  614. } else {
  615. /* link OFF */
  616. if (netif_carrier_ok(ndev))
  617. netif_carrier_off(ndev);
  618. if (!netif_queue_stopped(ndev))
  619. netif_stop_queue(ndev);
  620. }
  621. }
  622. /**
  623. * hash_get: Calculate hash value from mac address
  624. * @addr: mac address to delete from hash table
  625. *
  626. * Calculates hash value from mac address
  627. *
  628. */
  629. static u32 hash_get(u8 *addr)
  630. {
  631. u32 hash;
  632. u8 tmpval;
  633. int cnt;
  634. hash = 0;
  635. for (cnt = 0; cnt < 2; cnt++) {
  636. tmpval = *addr++;
  637. hash ^= (tmpval >> 2) ^ (tmpval << 4);
  638. tmpval = *addr++;
  639. hash ^= (tmpval >> 4) ^ (tmpval << 2);
  640. tmpval = *addr++;
  641. hash ^= (tmpval >> 6) ^ (tmpval);
  642. }
  643. return hash & 0x3F;
  644. }
  645. /**
  646. * hash_add: Hash function to add mac addr from hash table
  647. * @priv: The DaVinci EMAC private adapter structure
  648. * mac_addr: mac address to delete from hash table
  649. *
  650. * Adds mac address to the internal hash table
  651. *
  652. */
  653. static int hash_add(struct emac_priv *priv, u8 *mac_addr)
  654. {
  655. struct device *emac_dev = &priv->ndev->dev;
  656. u32 rc = 0;
  657. u32 hash_bit;
  658. u32 hash_value = hash_get(mac_addr);
  659. if (hash_value >= EMAC_NUM_MULTICAST_BITS) {
  660. if (netif_msg_drv(priv)) {
  661. dev_err(emac_dev, "DaVinci EMAC: hash_add(): Invalid "\
  662. "Hash %08x, should not be greater than %08x",
  663. hash_value, (EMAC_NUM_MULTICAST_BITS - 1));
  664. }
  665. return -1;
  666. }
  667. /* set the hash bit only if not previously set */
  668. if (priv->multicast_hash_cnt[hash_value] == 0) {
  669. rc = 1; /* hash value changed */
  670. if (hash_value < 32) {
  671. hash_bit = BIT(hash_value);
  672. priv->mac_hash1 |= hash_bit;
  673. } else {
  674. hash_bit = BIT((hash_value - 32));
  675. priv->mac_hash2 |= hash_bit;
  676. }
  677. }
  678. /* incr counter for num of mcast addr's mapped to "this" hash bit */
  679. ++priv->multicast_hash_cnt[hash_value];
  680. return rc;
  681. }
  682. /**
  683. * hash_del: Hash function to delete mac addr from hash table
  684. * @priv: The DaVinci EMAC private adapter structure
  685. * mac_addr: mac address to delete from hash table
  686. *
  687. * Removes mac address from the internal hash table
  688. *
  689. */
  690. static int hash_del(struct emac_priv *priv, u8 *mac_addr)
  691. {
  692. u32 hash_value;
  693. u32 hash_bit;
  694. hash_value = hash_get(mac_addr);
  695. if (priv->multicast_hash_cnt[hash_value] > 0) {
  696. /* dec cntr for num of mcast addr's mapped to this hash bit */
  697. --priv->multicast_hash_cnt[hash_value];
  698. }
  699. /* if counter still > 0, at least one multicast address refers
  700. * to this hash bit. so return 0 */
  701. if (priv->multicast_hash_cnt[hash_value] > 0)
  702. return 0;
  703. if (hash_value < 32) {
  704. hash_bit = BIT(hash_value);
  705. priv->mac_hash1 &= ~hash_bit;
  706. } else {
  707. hash_bit = BIT((hash_value - 32));
  708. priv->mac_hash2 &= ~hash_bit;
  709. }
  710. /* return 1 to indicate change in mac_hash registers reqd */
  711. return 1;
  712. }
  713. /* EMAC multicast operation */
  714. #define EMAC_MULTICAST_ADD 0
  715. #define EMAC_MULTICAST_DEL 1
  716. #define EMAC_ALL_MULTI_SET 2
  717. #define EMAC_ALL_MULTI_CLR 3
  718. /**
  719. * emac_add_mcast: Set multicast address in the EMAC adapter (Internal)
  720. * @priv: The DaVinci EMAC private adapter structure
  721. * @action: multicast operation to perform
  722. * mac_addr: mac address to set
  723. *
  724. * Set multicast addresses in EMAC adapter - internal function
  725. *
  726. */
  727. static void emac_add_mcast(struct emac_priv *priv, u32 action, u8 *mac_addr)
  728. {
  729. struct device *emac_dev = &priv->ndev->dev;
  730. int update = -1;
  731. switch (action) {
  732. case EMAC_MULTICAST_ADD:
  733. update = hash_add(priv, mac_addr);
  734. break;
  735. case EMAC_MULTICAST_DEL:
  736. update = hash_del(priv, mac_addr);
  737. break;
  738. case EMAC_ALL_MULTI_SET:
  739. update = 1;
  740. priv->mac_hash1 = EMAC_ALL_MULTI_REG_VALUE;
  741. priv->mac_hash2 = EMAC_ALL_MULTI_REG_VALUE;
  742. break;
  743. case EMAC_ALL_MULTI_CLR:
  744. update = 1;
  745. priv->mac_hash1 = 0;
  746. priv->mac_hash2 = 0;
  747. memset(&(priv->multicast_hash_cnt[0]), 0,
  748. sizeof(priv->multicast_hash_cnt[0]) *
  749. EMAC_NUM_MULTICAST_BITS);
  750. break;
  751. default:
  752. if (netif_msg_drv(priv))
  753. dev_err(emac_dev, "DaVinci EMAC: add_mcast"\
  754. ": bad operation %d", action);
  755. break;
  756. }
  757. /* write to the hardware only if the register status chances */
  758. if (update > 0) {
  759. emac_write(EMAC_MACHASH1, priv->mac_hash1);
  760. emac_write(EMAC_MACHASH2, priv->mac_hash2);
  761. }
  762. }
  763. /**
  764. * emac_dev_mcast_set: Set multicast address in the EMAC adapter
  765. * @ndev: The DaVinci EMAC network adapter
  766. *
  767. * Set multicast addresses in EMAC adapter
  768. *
  769. */
  770. static void emac_dev_mcast_set(struct net_device *ndev)
  771. {
  772. u32 mbp_enable;
  773. struct emac_priv *priv = netdev_priv(ndev);
  774. mbp_enable = emac_read(EMAC_RXMBPENABLE);
  775. if (ndev->flags & IFF_PROMISC) {
  776. mbp_enable &= (~EMAC_MBP_PROMISCCH(EMAC_DEF_PROM_CH));
  777. mbp_enable |= (EMAC_MBP_RXPROMISC);
  778. } else {
  779. mbp_enable = (mbp_enable & ~EMAC_MBP_RXPROMISC);
  780. if ((ndev->flags & IFF_ALLMULTI) ||
  781. netdev_mc_count(ndev) > EMAC_DEF_MAX_MULTICAST_ADDRESSES) {
  782. mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
  783. emac_add_mcast(priv, EMAC_ALL_MULTI_SET, NULL);
  784. } else if (!netdev_mc_empty(ndev)) {
  785. struct netdev_hw_addr *ha;
  786. mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
  787. emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
  788. /* program multicast address list into EMAC hardware */
  789. netdev_for_each_mc_addr(ha, ndev) {
  790. emac_add_mcast(priv, EMAC_MULTICAST_ADD,
  791. (u8 *) ha->addr);
  792. }
  793. } else {
  794. mbp_enable = (mbp_enable & ~EMAC_MBP_RXMCAST);
  795. emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
  796. }
  797. }
  798. /* Set mbp config register */
  799. emac_write(EMAC_RXMBPENABLE, mbp_enable);
  800. }
  801. /*************************************************************************
  802. * EMAC Hardware manipulation
  803. *************************************************************************/
  804. /**
  805. * emac_int_disable: Disable EMAC module interrupt (from adapter)
  806. * @priv: The DaVinci EMAC private adapter structure
  807. *
  808. * Disable EMAC interrupt on the adapter
  809. *
  810. */
  811. static void emac_int_disable(struct emac_priv *priv)
  812. {
  813. if (priv->version == EMAC_VERSION_2) {
  814. unsigned long flags;
  815. local_irq_save(flags);
  816. /* Program C0_Int_En to zero to turn off
  817. * interrupts to the CPU */
  818. emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0x0);
  819. emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0x0);
  820. /* NOTE: Rx Threshold and Misc interrupts are not disabled */
  821. if (priv->int_disable)
  822. priv->int_disable();
  823. local_irq_restore(flags);
  824. } else {
  825. /* Set DM644x control registers for interrupt control */
  826. emac_ctrl_write(EMAC_CTRL_EWCTL, 0x0);
  827. }
  828. }
  829. /**
  830. * emac_int_enable: Enable EMAC module interrupt (from adapter)
  831. * @priv: The DaVinci EMAC private adapter structure
  832. *
  833. * Enable EMAC interrupt on the adapter
  834. *
  835. */
  836. static void emac_int_enable(struct emac_priv *priv)
  837. {
  838. if (priv->version == EMAC_VERSION_2) {
  839. if (priv->int_enable)
  840. priv->int_enable();
  841. emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0xff);
  842. emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0xff);
  843. /* In addition to turning on interrupt Enable, we need
  844. * ack by writing appropriate values to the EOI
  845. * register */
  846. /* NOTE: Rx Threshold and Misc interrupts are not enabled */
  847. /* ack rxen only then a new pulse will be generated */
  848. emac_write(EMAC_DM646X_MACEOIVECTOR,
  849. EMAC_DM646X_MAC_EOI_C0_RXEN);
  850. /* ack txen- only then a new pulse will be generated */
  851. emac_write(EMAC_DM646X_MACEOIVECTOR,
  852. EMAC_DM646X_MAC_EOI_C0_TXEN);
  853. } else {
  854. /* Set DM644x control registers for interrupt control */
  855. emac_ctrl_write(EMAC_CTRL_EWCTL, 0x1);
  856. }
  857. }
  858. /**
  859. * emac_irq: EMAC interrupt handler
  860. * @irq: interrupt number
  861. * @dev_id: EMAC network adapter data structure ptr
  862. *
  863. * EMAC Interrupt handler - we only schedule NAPI and not process any packets
  864. * here. EVen the interrupt status is checked (TX/RX/Err) in NAPI poll function
  865. *
  866. * Returns interrupt handled condition
  867. */
  868. static irqreturn_t emac_irq(int irq, void *dev_id)
  869. {
  870. struct net_device *ndev = (struct net_device *)dev_id;
  871. struct emac_priv *priv = netdev_priv(ndev);
  872. ++priv->isr_count;
  873. if (likely(netif_running(priv->ndev))) {
  874. emac_int_disable(priv);
  875. napi_schedule(&priv->napi);
  876. } else {
  877. /* we are closing down, so dont process anything */
  878. }
  879. return IRQ_HANDLED;
  880. }
  881. static struct sk_buff *emac_rx_alloc(struct emac_priv *priv)
  882. {
  883. struct sk_buff *skb = netdev_alloc_skb(priv->ndev, priv->rx_buf_size);
  884. if (WARN_ON(!skb))
  885. return NULL;
  886. skb_reserve(skb, NET_IP_ALIGN);
  887. return skb;
  888. }
  889. static void emac_rx_handler(void *token, int len, int status)
  890. {
  891. struct sk_buff *skb = token;
  892. struct net_device *ndev = skb->dev;
  893. struct emac_priv *priv = netdev_priv(ndev);
  894. struct device *emac_dev = &ndev->dev;
  895. int ret;
  896. /* free and bail if we are shutting down */
  897. if (unlikely(!netif_running(ndev))) {
  898. dev_kfree_skb_any(skb);
  899. return;
  900. }
  901. /* recycle on receive error */
  902. if (status < 0) {
  903. ndev->stats.rx_errors++;
  904. goto recycle;
  905. }
  906. /* feed received packet up the stack */
  907. skb_put(skb, len);
  908. skb->protocol = eth_type_trans(skb, ndev);
  909. netif_receive_skb(skb);
  910. ndev->stats.rx_bytes += len;
  911. ndev->stats.rx_packets++;
  912. /* alloc a new packet for receive */
  913. skb = emac_rx_alloc(priv);
  914. if (!skb) {
  915. if (netif_msg_rx_err(priv) && net_ratelimit())
  916. dev_err(emac_dev, "failed rx buffer alloc\n");
  917. return;
  918. }
  919. recycle:
  920. ret = cpdma_chan_submit(priv->rxchan, skb, skb->data,
  921. skb_tailroom(skb), GFP_KERNEL);
  922. WARN_ON(ret == -ENOMEM);
  923. if (unlikely(ret < 0))
  924. dev_kfree_skb_any(skb);
  925. }
  926. static void emac_tx_handler(void *token, int len, int status)
  927. {
  928. struct sk_buff *skb = token;
  929. struct net_device *ndev = skb->dev;
  930. struct emac_priv *priv = netdev_priv(ndev);
  931. atomic_dec(&priv->cur_tx);
  932. if (unlikely(netif_queue_stopped(ndev)))
  933. netif_wake_queue(ndev);
  934. ndev->stats.tx_packets++;
  935. ndev->stats.tx_bytes += len;
  936. dev_kfree_skb_any(skb);
  937. }
  938. /**
  939. * emac_dev_xmit: EMAC Transmit function
  940. * @skb: SKB pointer
  941. * @ndev: The DaVinci EMAC network adapter
  942. *
  943. * Called by the system to transmit a packet - we queue the packet in
  944. * EMAC hardware transmit queue
  945. *
  946. * Returns success(NETDEV_TX_OK) or error code (typically out of desc's)
  947. */
  948. static int emac_dev_xmit(struct sk_buff *skb, struct net_device *ndev)
  949. {
  950. struct device *emac_dev = &ndev->dev;
  951. int ret_code;
  952. struct emac_priv *priv = netdev_priv(ndev);
  953. /* If no link, return */
  954. if (unlikely(!priv->link)) {
  955. if (netif_msg_tx_err(priv) && net_ratelimit())
  956. dev_err(emac_dev, "DaVinci EMAC: No link to transmit");
  957. goto fail_tx;
  958. }
  959. ret_code = skb_padto(skb, EMAC_DEF_MIN_ETHPKTSIZE);
  960. if (unlikely(ret_code < 0)) {
  961. if (netif_msg_tx_err(priv) && net_ratelimit())
  962. dev_err(emac_dev, "DaVinci EMAC: packet pad failed");
  963. goto fail_tx;
  964. }
  965. skb_tx_timestamp(skb);
  966. ret_code = cpdma_chan_submit(priv->txchan, skb, skb->data, skb->len,
  967. GFP_KERNEL);
  968. if (unlikely(ret_code != 0)) {
  969. if (netif_msg_tx_err(priv) && net_ratelimit())
  970. dev_err(emac_dev, "DaVinci EMAC: desc submit failed");
  971. goto fail_tx;
  972. }
  973. if (atomic_inc_return(&priv->cur_tx) >= EMAC_DEF_TX_NUM_DESC)
  974. netif_stop_queue(ndev);
  975. return NETDEV_TX_OK;
  976. fail_tx:
  977. ndev->stats.tx_dropped++;
  978. netif_stop_queue(ndev);
  979. return NETDEV_TX_BUSY;
  980. }
  981. /**
  982. * emac_dev_tx_timeout: EMAC Transmit timeout function
  983. * @ndev: The DaVinci EMAC network adapter
  984. *
  985. * Called when system detects that a skb timeout period has expired
  986. * potentially due to a fault in the adapter in not being able to send
  987. * it out on the wire. We teardown the TX channel assuming a hardware
  988. * error and re-initialize the TX channel for hardware operation
  989. *
  990. */
  991. static void emac_dev_tx_timeout(struct net_device *ndev)
  992. {
  993. struct emac_priv *priv = netdev_priv(ndev);
  994. struct device *emac_dev = &ndev->dev;
  995. if (netif_msg_tx_err(priv))
  996. dev_err(emac_dev, "DaVinci EMAC: xmit timeout, restarting TX");
  997. emac_dump_regs(priv);
  998. ndev->stats.tx_errors++;
  999. emac_int_disable(priv);
  1000. cpdma_chan_stop(priv->txchan);
  1001. cpdma_chan_start(priv->txchan);
  1002. emac_int_enable(priv);
  1003. }
  1004. /**
  1005. * emac_set_type0addr: Set EMAC Type0 mac address
  1006. * @priv: The DaVinci EMAC private adapter structure
  1007. * @ch: RX channel number
  1008. * @mac_addr: MAC address to set in device
  1009. *
  1010. * Called internally to set Type0 mac address of the adapter (Device)
  1011. *
  1012. * Returns success (0) or appropriate error code (none as of now)
  1013. */
  1014. static void emac_set_type0addr(struct emac_priv *priv, u32 ch, char *mac_addr)
  1015. {
  1016. u32 val;
  1017. val = ((mac_addr[5] << 8) | (mac_addr[4]));
  1018. emac_write(EMAC_MACSRCADDRLO, val);
  1019. val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
  1020. (mac_addr[1] << 8) | (mac_addr[0]));
  1021. emac_write(EMAC_MACSRCADDRHI, val);
  1022. val = emac_read(EMAC_RXUNICASTSET);
  1023. val |= BIT(ch);
  1024. emac_write(EMAC_RXUNICASTSET, val);
  1025. val = emac_read(EMAC_RXUNICASTCLEAR);
  1026. val &= ~BIT(ch);
  1027. emac_write(EMAC_RXUNICASTCLEAR, val);
  1028. }
  1029. /**
  1030. * emac_set_type1addr: Set EMAC Type1 mac address
  1031. * @priv: The DaVinci EMAC private adapter structure
  1032. * @ch: RX channel number
  1033. * @mac_addr: MAC address to set in device
  1034. *
  1035. * Called internally to set Type1 mac address of the adapter (Device)
  1036. *
  1037. * Returns success (0) or appropriate error code (none as of now)
  1038. */
  1039. static void emac_set_type1addr(struct emac_priv *priv, u32 ch, char *mac_addr)
  1040. {
  1041. u32 val;
  1042. emac_write(EMAC_MACINDEX, ch);
  1043. val = ((mac_addr[5] << 8) | mac_addr[4]);
  1044. emac_write(EMAC_MACADDRLO, val);
  1045. val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
  1046. (mac_addr[1] << 8) | (mac_addr[0]));
  1047. emac_write(EMAC_MACADDRHI, val);
  1048. emac_set_type0addr(priv, ch, mac_addr);
  1049. }
  1050. /**
  1051. * emac_set_type2addr: Set EMAC Type2 mac address
  1052. * @priv: The DaVinci EMAC private adapter structure
  1053. * @ch: RX channel number
  1054. * @mac_addr: MAC address to set in device
  1055. * @index: index into RX address entries
  1056. * @match: match parameter for RX address matching logic
  1057. *
  1058. * Called internally to set Type2 mac address of the adapter (Device)
  1059. *
  1060. * Returns success (0) or appropriate error code (none as of now)
  1061. */
  1062. static void emac_set_type2addr(struct emac_priv *priv, u32 ch,
  1063. char *mac_addr, int index, int match)
  1064. {
  1065. u32 val;
  1066. emac_write(EMAC_MACINDEX, index);
  1067. val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
  1068. (mac_addr[1] << 8) | (mac_addr[0]));
  1069. emac_write(EMAC_MACADDRHI, val);
  1070. val = ((mac_addr[5] << 8) | mac_addr[4] | ((ch & 0x7) << 16) | \
  1071. (match << 19) | BIT(20));
  1072. emac_write(EMAC_MACADDRLO, val);
  1073. emac_set_type0addr(priv, ch, mac_addr);
  1074. }
  1075. /**
  1076. * emac_setmac: Set mac address in the adapter (internal function)
  1077. * @priv: The DaVinci EMAC private adapter structure
  1078. * @ch: RX channel number
  1079. * @mac_addr: MAC address to set in device
  1080. *
  1081. * Called internally to set the mac address of the adapter (Device)
  1082. *
  1083. * Returns success (0) or appropriate error code (none as of now)
  1084. */
  1085. static void emac_setmac(struct emac_priv *priv, u32 ch, char *mac_addr)
  1086. {
  1087. struct device *emac_dev = &priv->ndev->dev;
  1088. if (priv->rx_addr_type == 0) {
  1089. emac_set_type0addr(priv, ch, mac_addr);
  1090. } else if (priv->rx_addr_type == 1) {
  1091. u32 cnt;
  1092. for (cnt = 0; cnt < EMAC_MAX_TXRX_CHANNELS; cnt++)
  1093. emac_set_type1addr(priv, ch, mac_addr);
  1094. } else if (priv->rx_addr_type == 2) {
  1095. emac_set_type2addr(priv, ch, mac_addr, ch, 1);
  1096. emac_set_type0addr(priv, ch, mac_addr);
  1097. } else {
  1098. if (netif_msg_drv(priv))
  1099. dev_err(emac_dev, "DaVinci EMAC: Wrong addressing\n");
  1100. }
  1101. }
  1102. /**
  1103. * emac_dev_setmac_addr: Set mac address in the adapter
  1104. * @ndev: The DaVinci EMAC network adapter
  1105. * @addr: MAC address to set in device
  1106. *
  1107. * Called by the system to set the mac address of the adapter (Device)
  1108. *
  1109. * Returns success (0) or appropriate error code (none as of now)
  1110. */
  1111. static int emac_dev_setmac_addr(struct net_device *ndev, void *addr)
  1112. {
  1113. struct emac_priv *priv = netdev_priv(ndev);
  1114. struct device *emac_dev = &priv->ndev->dev;
  1115. struct sockaddr *sa = addr;
  1116. if (!is_valid_ether_addr(sa->sa_data))
  1117. return -EADDRNOTAVAIL;
  1118. /* Store mac addr in priv and rx channel and set it in EMAC hw */
  1119. memcpy(priv->mac_addr, sa->sa_data, ndev->addr_len);
  1120. memcpy(ndev->dev_addr, sa->sa_data, ndev->addr_len);
  1121. ndev->addr_assign_type &= ~NET_ADDR_RANDOM;
  1122. /* MAC address is configured only after the interface is enabled. */
  1123. if (netif_running(ndev)) {
  1124. emac_setmac(priv, EMAC_DEF_RX_CH, priv->mac_addr);
  1125. }
  1126. if (netif_msg_drv(priv))
  1127. dev_notice(emac_dev, "DaVinci EMAC: emac_dev_setmac_addr %pM\n",
  1128. priv->mac_addr);
  1129. return 0;
  1130. }
  1131. /**
  1132. * emac_hw_enable: Enable EMAC hardware for packet transmission/reception
  1133. * @priv: The DaVinci EMAC private adapter structure
  1134. *
  1135. * Enables EMAC hardware for packet processing - enables PHY, enables RX
  1136. * for packet reception and enables device interrupts and then NAPI
  1137. *
  1138. * Returns success (0) or appropriate error code (none right now)
  1139. */
  1140. static int emac_hw_enable(struct emac_priv *priv)
  1141. {
  1142. u32 val, mbp_enable, mac_control;
  1143. /* Soft reset */
  1144. emac_write(EMAC_SOFTRESET, 1);
  1145. while (emac_read(EMAC_SOFTRESET))
  1146. cpu_relax();
  1147. /* Disable interrupt & Set pacing for more interrupts initially */
  1148. emac_int_disable(priv);
  1149. /* Full duplex enable bit set when auto negotiation happens */
  1150. mac_control =
  1151. (((EMAC_DEF_TXPRIO_FIXED) ? (EMAC_MACCONTROL_TXPTYPE) : 0x0) |
  1152. ((priv->speed == 1000) ? EMAC_MACCONTROL_GIGABITEN : 0x0) |
  1153. ((EMAC_DEF_TXPACING_EN) ? (EMAC_MACCONTROL_TXPACEEN) : 0x0) |
  1154. ((priv->duplex == DUPLEX_FULL) ? 0x1 : 0));
  1155. emac_write(EMAC_MACCONTROL, mac_control);
  1156. mbp_enable =
  1157. (((EMAC_DEF_PASS_CRC) ? (EMAC_RXMBP_PASSCRC_MASK) : 0x0) |
  1158. ((EMAC_DEF_QOS_EN) ? (EMAC_RXMBP_QOSEN_MASK) : 0x0) |
  1159. ((EMAC_DEF_NO_BUFF_CHAIN) ? (EMAC_RXMBP_NOCHAIN_MASK) : 0x0) |
  1160. ((EMAC_DEF_MACCTRL_FRAME_EN) ? (EMAC_RXMBP_CMFEN_MASK) : 0x0) |
  1161. ((EMAC_DEF_SHORT_FRAME_EN) ? (EMAC_RXMBP_CSFEN_MASK) : 0x0) |
  1162. ((EMAC_DEF_ERROR_FRAME_EN) ? (EMAC_RXMBP_CEFEN_MASK) : 0x0) |
  1163. ((EMAC_DEF_PROM_EN) ? (EMAC_RXMBP_CAFEN_MASK) : 0x0) |
  1164. ((EMAC_DEF_PROM_CH & EMAC_RXMBP_CHMASK) << \
  1165. EMAC_RXMBP_PROMCH_SHIFT) |
  1166. ((EMAC_DEF_BCAST_EN) ? (EMAC_RXMBP_BROADEN_MASK) : 0x0) |
  1167. ((EMAC_DEF_BCAST_CH & EMAC_RXMBP_CHMASK) << \
  1168. EMAC_RXMBP_BROADCH_SHIFT) |
  1169. ((EMAC_DEF_MCAST_EN) ? (EMAC_RXMBP_MULTIEN_MASK) : 0x0) |
  1170. ((EMAC_DEF_MCAST_CH & EMAC_RXMBP_CHMASK) << \
  1171. EMAC_RXMBP_MULTICH_SHIFT));
  1172. emac_write(EMAC_RXMBPENABLE, mbp_enable);
  1173. emac_write(EMAC_RXMAXLEN, (EMAC_DEF_MAX_FRAME_SIZE &
  1174. EMAC_RX_MAX_LEN_MASK));
  1175. emac_write(EMAC_RXBUFFEROFFSET, (EMAC_DEF_BUFFER_OFFSET &
  1176. EMAC_RX_BUFFER_OFFSET_MASK));
  1177. emac_write(EMAC_RXFILTERLOWTHRESH, 0);
  1178. emac_write(EMAC_RXUNICASTCLEAR, EMAC_RX_UNICAST_CLEAR_ALL);
  1179. priv->rx_addr_type = (emac_read(EMAC_MACCONFIG) >> 8) & 0xFF;
  1180. emac_write(EMAC_MACINTMASKSET, EMAC_MAC_HOST_ERR_INTMASK_VAL);
  1181. emac_setmac(priv, EMAC_DEF_RX_CH, priv->mac_addr);
  1182. /* Enable MII */
  1183. val = emac_read(EMAC_MACCONTROL);
  1184. val |= (EMAC_MACCONTROL_GMIIEN);
  1185. emac_write(EMAC_MACCONTROL, val);
  1186. /* Enable NAPI and interrupts */
  1187. napi_enable(&priv->napi);
  1188. emac_int_enable(priv);
  1189. return 0;
  1190. }
  1191. /**
  1192. * emac_poll: EMAC NAPI Poll function
  1193. * @ndev: The DaVinci EMAC network adapter
  1194. * @budget: Number of receive packets to process (as told by NAPI layer)
  1195. *
  1196. * NAPI Poll function implemented to process packets as per budget. We check
  1197. * the type of interrupt on the device and accordingly call the TX or RX
  1198. * packet processing functions. We follow the budget for RX processing and
  1199. * also put a cap on number of TX pkts processed through config param. The
  1200. * NAPI schedule function is called if more packets pending.
  1201. *
  1202. * Returns number of packets received (in most cases; else TX pkts - rarely)
  1203. */
  1204. static int emac_poll(struct napi_struct *napi, int budget)
  1205. {
  1206. unsigned int mask;
  1207. struct emac_priv *priv = container_of(napi, struct emac_priv, napi);
  1208. struct net_device *ndev = priv->ndev;
  1209. struct device *emac_dev = &ndev->dev;
  1210. u32 status = 0;
  1211. u32 num_tx_pkts = 0, num_rx_pkts = 0;
  1212. /* Check interrupt vectors and call packet processing */
  1213. status = emac_read(EMAC_MACINVECTOR);
  1214. mask = EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC;
  1215. if (priv->version == EMAC_VERSION_2)
  1216. mask = EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC;
  1217. if (status & mask) {
  1218. num_tx_pkts = cpdma_chan_process(priv->txchan,
  1219. EMAC_DEF_TX_MAX_SERVICE);
  1220. } /* TX processing */
  1221. mask = EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC;
  1222. if (priv->version == EMAC_VERSION_2)
  1223. mask = EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC;
  1224. if (status & mask) {
  1225. num_rx_pkts = cpdma_chan_process(priv->rxchan, budget);
  1226. } /* RX processing */
  1227. mask = EMAC_DM644X_MAC_IN_VECTOR_HOST_INT;
  1228. if (priv->version == EMAC_VERSION_2)
  1229. mask = EMAC_DM646X_MAC_IN_VECTOR_HOST_INT;
  1230. if (unlikely(status & mask)) {
  1231. u32 ch, cause;
  1232. dev_err(emac_dev, "DaVinci EMAC: Fatal Hardware Error\n");
  1233. netif_stop_queue(ndev);
  1234. napi_disable(&priv->napi);
  1235. status = emac_read(EMAC_MACSTATUS);
  1236. cause = ((status & EMAC_MACSTATUS_TXERRCODE_MASK) >>
  1237. EMAC_MACSTATUS_TXERRCODE_SHIFT);
  1238. if (cause) {
  1239. ch = ((status & EMAC_MACSTATUS_TXERRCH_MASK) >>
  1240. EMAC_MACSTATUS_TXERRCH_SHIFT);
  1241. if (net_ratelimit()) {
  1242. dev_err(emac_dev, "TX Host error %s on ch=%d\n",
  1243. &emac_txhost_errcodes[cause][0], ch);
  1244. }
  1245. }
  1246. cause = ((status & EMAC_MACSTATUS_RXERRCODE_MASK) >>
  1247. EMAC_MACSTATUS_RXERRCODE_SHIFT);
  1248. if (cause) {
  1249. ch = ((status & EMAC_MACSTATUS_RXERRCH_MASK) >>
  1250. EMAC_MACSTATUS_RXERRCH_SHIFT);
  1251. if (netif_msg_hw(priv) && net_ratelimit())
  1252. dev_err(emac_dev, "RX Host error %s on ch=%d\n",
  1253. &emac_rxhost_errcodes[cause][0], ch);
  1254. }
  1255. } else if (num_rx_pkts < budget) {
  1256. napi_complete(napi);
  1257. emac_int_enable(priv);
  1258. }
  1259. return num_rx_pkts;
  1260. }
  1261. #ifdef CONFIG_NET_POLL_CONTROLLER
  1262. /**
  1263. * emac_poll_controller: EMAC Poll controller function
  1264. * @ndev: The DaVinci EMAC network adapter
  1265. *
  1266. * Polled functionality used by netconsole and others in non interrupt mode
  1267. *
  1268. */
  1269. void emac_poll_controller(struct net_device *ndev)
  1270. {
  1271. struct emac_priv *priv = netdev_priv(ndev);
  1272. emac_int_disable(priv);
  1273. emac_irq(ndev->irq, ndev);
  1274. emac_int_enable(priv);
  1275. }
  1276. #endif
  1277. static void emac_adjust_link(struct net_device *ndev)
  1278. {
  1279. struct emac_priv *priv = netdev_priv(ndev);
  1280. struct phy_device *phydev = priv->phydev;
  1281. unsigned long flags;
  1282. int new_state = 0;
  1283. spin_lock_irqsave(&priv->lock, flags);
  1284. if (phydev->link) {
  1285. /* check the mode of operation - full/half duplex */
  1286. if (phydev->duplex != priv->duplex) {
  1287. new_state = 1;
  1288. priv->duplex = phydev->duplex;
  1289. }
  1290. if (phydev->speed != priv->speed) {
  1291. new_state = 1;
  1292. priv->speed = phydev->speed;
  1293. }
  1294. if (!priv->link) {
  1295. new_state = 1;
  1296. priv->link = 1;
  1297. }
  1298. } else if (priv->link) {
  1299. new_state = 1;
  1300. priv->link = 0;
  1301. priv->speed = 0;
  1302. priv->duplex = ~0;
  1303. }
  1304. if (new_state) {
  1305. emac_update_phystatus(priv);
  1306. phy_print_status(priv->phydev);
  1307. }
  1308. spin_unlock_irqrestore(&priv->lock, flags);
  1309. }
  1310. /*************************************************************************
  1311. * Linux Driver Model
  1312. *************************************************************************/
  1313. /**
  1314. * emac_devioctl: EMAC adapter ioctl
  1315. * @ndev: The DaVinci EMAC network adapter
  1316. * @ifrq: request parameter
  1317. * @cmd: command parameter
  1318. *
  1319. * EMAC driver ioctl function
  1320. *
  1321. * Returns success(0) or appropriate error code
  1322. */
  1323. static int emac_devioctl(struct net_device *ndev, struct ifreq *ifrq, int cmd)
  1324. {
  1325. struct emac_priv *priv = netdev_priv(ndev);
  1326. if (!(netif_running(ndev)))
  1327. return -EINVAL;
  1328. /* TODO: Add phy read and write and private statistics get feature */
  1329. return phy_mii_ioctl(priv->phydev, ifrq, cmd);
  1330. }
  1331. static int match_first_device(struct device *dev, void *data)
  1332. {
  1333. return !strncmp(dev_name(dev), "davinci_mdio", 12);
  1334. }
  1335. /**
  1336. * emac_dev_open: EMAC device open
  1337. * @ndev: The DaVinci EMAC network adapter
  1338. *
  1339. * Called when system wants to start the interface. We init TX/RX channels
  1340. * and enable the hardware for packet reception/transmission and start the
  1341. * network queue.
  1342. *
  1343. * Returns 0 for a successful open, or appropriate error code
  1344. */
  1345. static int emac_dev_open(struct net_device *ndev)
  1346. {
  1347. struct device *emac_dev = &ndev->dev;
  1348. u32 cnt;
  1349. struct resource *res;
  1350. int q, m, ret;
  1351. int i = 0;
  1352. int k = 0;
  1353. struct emac_priv *priv = netdev_priv(ndev);
  1354. netif_carrier_off(ndev);
  1355. for (cnt = 0; cnt < ETH_ALEN; cnt++)
  1356. ndev->dev_addr[cnt] = priv->mac_addr[cnt];
  1357. /* Configuration items */
  1358. priv->rx_buf_size = EMAC_DEF_MAX_FRAME_SIZE + NET_IP_ALIGN;
  1359. priv->mac_hash1 = 0;
  1360. priv->mac_hash2 = 0;
  1361. emac_write(EMAC_MACHASH1, 0);
  1362. emac_write(EMAC_MACHASH2, 0);
  1363. for (i = 0; i < EMAC_DEF_RX_NUM_DESC; i++) {
  1364. struct sk_buff *skb = emac_rx_alloc(priv);
  1365. if (!skb)
  1366. break;
  1367. ret = cpdma_chan_submit(priv->rxchan, skb, skb->data,
  1368. skb_tailroom(skb), GFP_KERNEL);
  1369. if (WARN_ON(ret < 0))
  1370. break;
  1371. }
  1372. /* Request IRQ */
  1373. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
  1374. for (i = res->start; i <= res->end; i++) {
  1375. if (request_irq(i, emac_irq, IRQF_DISABLED,
  1376. ndev->name, ndev))
  1377. goto rollback;
  1378. }
  1379. k++;
  1380. }
  1381. /* Start/Enable EMAC hardware */
  1382. emac_hw_enable(priv);
  1383. /* Enable Interrupt pacing if configured */
  1384. if (priv->coal_intvl != 0) {
  1385. struct ethtool_coalesce coal;
  1386. coal.rx_coalesce_usecs = (priv->coal_intvl << 4);
  1387. emac_set_coalesce(ndev, &coal);
  1388. }
  1389. cpdma_ctlr_start(priv->dma);
  1390. priv->phydev = NULL;
  1391. /* use the first phy on the bus if pdata did not give us a phy id */
  1392. if (!priv->phy_id) {
  1393. struct device *phy;
  1394. phy = bus_find_device(&mdio_bus_type, NULL, NULL,
  1395. match_first_device);
  1396. if (phy)
  1397. priv->phy_id = dev_name(phy);
  1398. }
  1399. if (priv->phy_id && *priv->phy_id) {
  1400. priv->phydev = phy_connect(ndev, priv->phy_id,
  1401. &emac_adjust_link, 0,
  1402. PHY_INTERFACE_MODE_MII);
  1403. if (IS_ERR(priv->phydev)) {
  1404. dev_err(emac_dev, "could not connect to phy %s\n",
  1405. priv->phy_id);
  1406. ret = PTR_ERR(priv->phydev);
  1407. priv->phydev = NULL;
  1408. return ret;
  1409. }
  1410. priv->link = 0;
  1411. priv->speed = 0;
  1412. priv->duplex = ~0;
  1413. dev_info(emac_dev, "attached PHY driver [%s] "
  1414. "(mii_bus:phy_addr=%s, id=%x)\n",
  1415. priv->phydev->drv->name, dev_name(&priv->phydev->dev),
  1416. priv->phydev->phy_id);
  1417. } else {
  1418. /* No PHY , fix the link, speed and duplex settings */
  1419. dev_notice(emac_dev, "no phy, defaulting to 100/full\n");
  1420. priv->link = 1;
  1421. priv->speed = SPEED_100;
  1422. priv->duplex = DUPLEX_FULL;
  1423. emac_update_phystatus(priv);
  1424. }
  1425. if (!netif_running(ndev)) /* debug only - to avoid compiler warning */
  1426. emac_dump_regs(priv);
  1427. if (netif_msg_drv(priv))
  1428. dev_notice(emac_dev, "DaVinci EMAC: Opened %s\n", ndev->name);
  1429. if (priv->phydev)
  1430. phy_start(priv->phydev);
  1431. return 0;
  1432. rollback:
  1433. dev_err(emac_dev, "DaVinci EMAC: request_irq() failed");
  1434. for (q = k; k >= 0; k--) {
  1435. for (m = i; m >= res->start; m--)
  1436. free_irq(m, ndev);
  1437. res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k-1);
  1438. m = res->end;
  1439. }
  1440. return -EBUSY;
  1441. }
  1442. /**
  1443. * emac_dev_stop: EMAC device stop
  1444. * @ndev: The DaVinci EMAC network adapter
  1445. *
  1446. * Called when system wants to stop or down the interface. We stop the network
  1447. * queue, disable interrupts and cleanup TX/RX channels.
  1448. *
  1449. * We return the statistics in net_device_stats structure pulled from emac
  1450. */
  1451. static int emac_dev_stop(struct net_device *ndev)
  1452. {
  1453. struct resource *res;
  1454. int i = 0;
  1455. int irq_num;
  1456. struct emac_priv *priv = netdev_priv(ndev);
  1457. struct device *emac_dev = &ndev->dev;
  1458. /* inform the upper layers. */
  1459. netif_stop_queue(ndev);
  1460. napi_disable(&priv->napi);
  1461. netif_carrier_off(ndev);
  1462. emac_int_disable(priv);
  1463. cpdma_ctlr_stop(priv->dma);
  1464. emac_write(EMAC_SOFTRESET, 1);
  1465. if (priv->phydev)
  1466. phy_disconnect(priv->phydev);
  1467. /* Free IRQ */
  1468. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, i))) {
  1469. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1470. free_irq(irq_num, priv->ndev);
  1471. i++;
  1472. }
  1473. if (netif_msg_drv(priv))
  1474. dev_notice(emac_dev, "DaVinci EMAC: %s stopped\n", ndev->name);
  1475. return 0;
  1476. }
  1477. /**
  1478. * emac_dev_getnetstats: EMAC get statistics function
  1479. * @ndev: The DaVinci EMAC network adapter
  1480. *
  1481. * Called when system wants to get statistics from the device.
  1482. *
  1483. * We return the statistics in net_device_stats structure pulled from emac
  1484. */
  1485. static struct net_device_stats *emac_dev_getnetstats(struct net_device *ndev)
  1486. {
  1487. struct emac_priv *priv = netdev_priv(ndev);
  1488. u32 mac_control;
  1489. u32 stats_clear_mask;
  1490. /* update emac hardware stats and reset the registers*/
  1491. mac_control = emac_read(EMAC_MACCONTROL);
  1492. if (mac_control & EMAC_MACCONTROL_GMIIEN)
  1493. stats_clear_mask = EMAC_STATS_CLR_MASK;
  1494. else
  1495. stats_clear_mask = 0;
  1496. ndev->stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
  1497. emac_write(EMAC_RXMCASTFRAMES, stats_clear_mask);
  1498. ndev->stats.collisions += (emac_read(EMAC_TXCOLLISION) +
  1499. emac_read(EMAC_TXSINGLECOLL) +
  1500. emac_read(EMAC_TXMULTICOLL));
  1501. emac_write(EMAC_TXCOLLISION, stats_clear_mask);
  1502. emac_write(EMAC_TXSINGLECOLL, stats_clear_mask);
  1503. emac_write(EMAC_TXMULTICOLL, stats_clear_mask);
  1504. ndev->stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
  1505. emac_read(EMAC_RXJABBER) +
  1506. emac_read(EMAC_RXUNDERSIZED));
  1507. emac_write(EMAC_RXOVERSIZED, stats_clear_mask);
  1508. emac_write(EMAC_RXJABBER, stats_clear_mask);
  1509. emac_write(EMAC_RXUNDERSIZED, stats_clear_mask);
  1510. ndev->stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
  1511. emac_read(EMAC_RXMOFOVERRUNS));
  1512. emac_write(EMAC_RXSOFOVERRUNS, stats_clear_mask);
  1513. emac_write(EMAC_RXMOFOVERRUNS, stats_clear_mask);
  1514. ndev->stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
  1515. emac_write(EMAC_RXDMAOVERRUNS, stats_clear_mask);
  1516. ndev->stats.tx_carrier_errors +=
  1517. emac_read(EMAC_TXCARRIERSENSE);
  1518. emac_write(EMAC_TXCARRIERSENSE, stats_clear_mask);
  1519. ndev->stats.tx_fifo_errors += emac_read(EMAC_TXUNDERRUN);
  1520. emac_write(EMAC_TXUNDERRUN, stats_clear_mask);
  1521. return &ndev->stats;
  1522. }
  1523. static const struct net_device_ops emac_netdev_ops = {
  1524. .ndo_open = emac_dev_open,
  1525. .ndo_stop = emac_dev_stop,
  1526. .ndo_start_xmit = emac_dev_xmit,
  1527. .ndo_set_rx_mode = emac_dev_mcast_set,
  1528. .ndo_set_mac_address = emac_dev_setmac_addr,
  1529. .ndo_do_ioctl = emac_devioctl,
  1530. .ndo_tx_timeout = emac_dev_tx_timeout,
  1531. .ndo_get_stats = emac_dev_getnetstats,
  1532. #ifdef CONFIG_NET_POLL_CONTROLLER
  1533. .ndo_poll_controller = emac_poll_controller,
  1534. #endif
  1535. };
  1536. /**
  1537. * davinci_emac_probe: EMAC device probe
  1538. * @pdev: The DaVinci EMAC device that we are removing
  1539. *
  1540. * Called when probing for emac devicesr. We get details of instances and
  1541. * resource information from platform init and register a network device
  1542. * and allocate resources necessary for driver to perform
  1543. */
  1544. static int __devinit davinci_emac_probe(struct platform_device *pdev)
  1545. {
  1546. int rc = 0;
  1547. struct resource *res;
  1548. struct net_device *ndev;
  1549. struct emac_priv *priv;
  1550. unsigned long size, hw_ram_addr;
  1551. struct emac_platform_data *pdata;
  1552. struct device *emac_dev;
  1553. struct cpdma_params dma_params;
  1554. /* obtain emac clock from kernel */
  1555. emac_clk = clk_get(&pdev->dev, NULL);
  1556. if (IS_ERR(emac_clk)) {
  1557. dev_err(&pdev->dev, "failed to get EMAC clock\n");
  1558. return -EBUSY;
  1559. }
  1560. emac_bus_frequency = clk_get_rate(emac_clk);
  1561. /* TODO: Probe PHY here if possible */
  1562. ndev = alloc_etherdev(sizeof(struct emac_priv));
  1563. if (!ndev) {
  1564. rc = -ENOMEM;
  1565. goto free_clk;
  1566. }
  1567. platform_set_drvdata(pdev, ndev);
  1568. priv = netdev_priv(ndev);
  1569. priv->pdev = pdev;
  1570. priv->ndev = ndev;
  1571. priv->msg_enable = netif_msg_init(debug_level, DAVINCI_EMAC_DEBUG);
  1572. spin_lock_init(&priv->lock);
  1573. pdata = pdev->dev.platform_data;
  1574. if (!pdata) {
  1575. dev_err(&pdev->dev, "no platform data\n");
  1576. rc = -ENODEV;
  1577. goto probe_quit;
  1578. }
  1579. /* MAC addr and PHY mask , RMII enable info from platform_data */
  1580. memcpy(priv->mac_addr, pdata->mac_addr, 6);
  1581. priv->phy_id = pdata->phy_id;
  1582. priv->rmii_en = pdata->rmii_en;
  1583. priv->version = pdata->version;
  1584. priv->int_enable = pdata->interrupt_enable;
  1585. priv->int_disable = pdata->interrupt_disable;
  1586. priv->coal_intvl = 0;
  1587. priv->bus_freq_mhz = (u32)(emac_bus_frequency / 1000000);
  1588. emac_dev = &ndev->dev;
  1589. /* Get EMAC platform data */
  1590. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1591. if (!res) {
  1592. dev_err(&pdev->dev,"error getting res\n");
  1593. rc = -ENOENT;
  1594. goto probe_quit;
  1595. }
  1596. priv->emac_base_phys = res->start + pdata->ctrl_reg_offset;
  1597. size = resource_size(res);
  1598. if (!request_mem_region(res->start, size, ndev->name)) {
  1599. dev_err(&pdev->dev, "failed request_mem_region() for regs\n");
  1600. rc = -ENXIO;
  1601. goto probe_quit;
  1602. }
  1603. priv->remap_addr = ioremap(res->start, size);
  1604. if (!priv->remap_addr) {
  1605. dev_err(&pdev->dev, "unable to map IO\n");
  1606. rc = -ENOMEM;
  1607. release_mem_region(res->start, size);
  1608. goto probe_quit;
  1609. }
  1610. priv->emac_base = priv->remap_addr + pdata->ctrl_reg_offset;
  1611. ndev->base_addr = (unsigned long)priv->remap_addr;
  1612. priv->ctrl_base = priv->remap_addr + pdata->ctrl_mod_reg_offset;
  1613. hw_ram_addr = pdata->hw_ram_addr;
  1614. if (!hw_ram_addr)
  1615. hw_ram_addr = (u32 __force)res->start + pdata->ctrl_ram_offset;
  1616. memset(&dma_params, 0, sizeof(dma_params));
  1617. dma_params.dev = emac_dev;
  1618. dma_params.dmaregs = priv->emac_base;
  1619. dma_params.rxthresh = priv->emac_base + 0x120;
  1620. dma_params.rxfree = priv->emac_base + 0x140;
  1621. dma_params.txhdp = priv->emac_base + 0x600;
  1622. dma_params.rxhdp = priv->emac_base + 0x620;
  1623. dma_params.txcp = priv->emac_base + 0x640;
  1624. dma_params.rxcp = priv->emac_base + 0x660;
  1625. dma_params.num_chan = EMAC_MAX_TXRX_CHANNELS;
  1626. dma_params.min_packet_size = EMAC_DEF_MIN_ETHPKTSIZE;
  1627. dma_params.desc_hw_addr = hw_ram_addr;
  1628. dma_params.desc_mem_size = pdata->ctrl_ram_size;
  1629. dma_params.desc_align = 16;
  1630. dma_params.desc_mem_phys = pdata->no_bd_ram ? 0 :
  1631. (u32 __force)res->start + pdata->ctrl_ram_offset;
  1632. priv->dma = cpdma_ctlr_create(&dma_params);
  1633. if (!priv->dma) {
  1634. dev_err(&pdev->dev, "error initializing DMA\n");
  1635. rc = -ENOMEM;
  1636. goto no_dma;
  1637. }
  1638. priv->txchan = cpdma_chan_create(priv->dma, tx_chan_num(EMAC_DEF_TX_CH),
  1639. emac_tx_handler);
  1640. priv->rxchan = cpdma_chan_create(priv->dma, rx_chan_num(EMAC_DEF_RX_CH),
  1641. emac_rx_handler);
  1642. if (WARN_ON(!priv->txchan || !priv->rxchan)) {
  1643. rc = -ENOMEM;
  1644. goto no_irq_res;
  1645. }
  1646. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1647. if (!res) {
  1648. dev_err(&pdev->dev, "error getting irq res\n");
  1649. rc = -ENOENT;
  1650. goto no_irq_res;
  1651. }
  1652. ndev->irq = res->start;
  1653. if (!is_valid_ether_addr(priv->mac_addr)) {
  1654. /* Use random MAC if none passed */
  1655. eth_hw_addr_random(ndev);
  1656. memcpy(priv->mac_addr, ndev->dev_addr, ndev->addr_len);
  1657. dev_warn(&pdev->dev, "using random MAC addr: %pM\n",
  1658. priv->mac_addr);
  1659. }
  1660. ndev->netdev_ops = &emac_netdev_ops;
  1661. SET_ETHTOOL_OPS(ndev, &ethtool_ops);
  1662. netif_napi_add(ndev, &priv->napi, emac_poll, EMAC_POLL_WEIGHT);
  1663. clk_enable(emac_clk);
  1664. /* register the network device */
  1665. SET_NETDEV_DEV(ndev, &pdev->dev);
  1666. rc = register_netdev(ndev);
  1667. if (rc) {
  1668. dev_err(&pdev->dev, "error in register_netdev\n");
  1669. rc = -ENODEV;
  1670. goto netdev_reg_err;
  1671. }
  1672. if (netif_msg_probe(priv)) {
  1673. dev_notice(emac_dev, "DaVinci EMAC Probe found device "\
  1674. "(regs: %p, irq: %d)\n",
  1675. (void *)priv->emac_base_phys, ndev->irq);
  1676. }
  1677. return 0;
  1678. netdev_reg_err:
  1679. clk_disable(emac_clk);
  1680. no_irq_res:
  1681. if (priv->txchan)
  1682. cpdma_chan_destroy(priv->txchan);
  1683. if (priv->rxchan)
  1684. cpdma_chan_destroy(priv->rxchan);
  1685. cpdma_ctlr_destroy(priv->dma);
  1686. no_dma:
  1687. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1688. release_mem_region(res->start, resource_size(res));
  1689. iounmap(priv->remap_addr);
  1690. probe_quit:
  1691. free_netdev(ndev);
  1692. free_clk:
  1693. clk_put(emac_clk);
  1694. return rc;
  1695. }
  1696. /**
  1697. * davinci_emac_remove: EMAC device remove
  1698. * @pdev: The DaVinci EMAC device that we are removing
  1699. *
  1700. * Called when removing the device driver. We disable clock usage and release
  1701. * the resources taken up by the driver and unregister network device
  1702. */
  1703. static int __devexit davinci_emac_remove(struct platform_device *pdev)
  1704. {
  1705. struct resource *res;
  1706. struct net_device *ndev = platform_get_drvdata(pdev);
  1707. struct emac_priv *priv = netdev_priv(ndev);
  1708. dev_notice(&ndev->dev, "DaVinci EMAC: davinci_emac_remove()\n");
  1709. platform_set_drvdata(pdev, NULL);
  1710. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1711. if (priv->txchan)
  1712. cpdma_chan_destroy(priv->txchan);
  1713. if (priv->rxchan)
  1714. cpdma_chan_destroy(priv->rxchan);
  1715. cpdma_ctlr_destroy(priv->dma);
  1716. release_mem_region(res->start, resource_size(res));
  1717. unregister_netdev(ndev);
  1718. iounmap(priv->remap_addr);
  1719. free_netdev(ndev);
  1720. clk_disable(emac_clk);
  1721. clk_put(emac_clk);
  1722. return 0;
  1723. }
  1724. static int davinci_emac_suspend(struct device *dev)
  1725. {
  1726. struct platform_device *pdev = to_platform_device(dev);
  1727. struct net_device *ndev = platform_get_drvdata(pdev);
  1728. if (netif_running(ndev))
  1729. emac_dev_stop(ndev);
  1730. clk_disable(emac_clk);
  1731. return 0;
  1732. }
  1733. static int davinci_emac_resume(struct device *dev)
  1734. {
  1735. struct platform_device *pdev = to_platform_device(dev);
  1736. struct net_device *ndev = platform_get_drvdata(pdev);
  1737. clk_enable(emac_clk);
  1738. if (netif_running(ndev))
  1739. emac_dev_open(ndev);
  1740. return 0;
  1741. }
  1742. static const struct dev_pm_ops davinci_emac_pm_ops = {
  1743. .suspend = davinci_emac_suspend,
  1744. .resume = davinci_emac_resume,
  1745. };
  1746. /**
  1747. * davinci_emac_driver: EMAC platform driver structure
  1748. */
  1749. static struct platform_driver davinci_emac_driver = {
  1750. .driver = {
  1751. .name = "davinci_emac",
  1752. .owner = THIS_MODULE,
  1753. .pm = &davinci_emac_pm_ops,
  1754. },
  1755. .probe = davinci_emac_probe,
  1756. .remove = __devexit_p(davinci_emac_remove),
  1757. };
  1758. /**
  1759. * davinci_emac_init: EMAC driver module init
  1760. *
  1761. * Called when initializing the driver. We register the driver with
  1762. * the platform.
  1763. */
  1764. static int __init davinci_emac_init(void)
  1765. {
  1766. return platform_driver_register(&davinci_emac_driver);
  1767. }
  1768. late_initcall(davinci_emac_init);
  1769. /**
  1770. * davinci_emac_exit: EMAC driver module exit
  1771. *
  1772. * Called when exiting the driver completely. We unregister the driver with
  1773. * the platform and exit
  1774. */
  1775. static void __exit davinci_emac_exit(void)
  1776. {
  1777. platform_driver_unregister(&davinci_emac_driver);
  1778. }
  1779. module_exit(davinci_emac_exit);
  1780. MODULE_LICENSE("GPL");
  1781. MODULE_AUTHOR("DaVinci EMAC Maintainer: Anant Gole <anantgole@ti.com>");
  1782. MODULE_AUTHOR("DaVinci EMAC Maintainer: Chaithrika U S <chaithrika@ti.com>");
  1783. MODULE_DESCRIPTION("DaVinci EMAC Ethernet driver");