davinci_emac.c 59 KB

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