davinci_emac.c 58 KB

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