davinci_emac.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  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_done(napi, num_rx_pkts);
  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. if (dev->parent && dev->parent->of_node)
  1222. return of_device_is_compatible(dev->parent->of_node,
  1223. "ti,davinci_mdio");
  1224. return !strncmp(dev_name(dev), "davinci_mdio", 12);
  1225. }
  1226. /**
  1227. * emac_dev_open - EMAC device open
  1228. * @ndev: The DaVinci EMAC network adapter
  1229. *
  1230. * Called when system wants to start the interface. We init TX/RX channels
  1231. * and enable the hardware for packet reception/transmission and start the
  1232. * network queue.
  1233. *
  1234. * Returns 0 for a successful open, or appropriate error code
  1235. */
  1236. static int emac_dev_open(struct net_device *ndev)
  1237. {
  1238. struct device *emac_dev = &ndev->dev;
  1239. u32 cnt;
  1240. struct resource *res;
  1241. int q, m, ret;
  1242. int res_num = 0, irq_num = 0;
  1243. int i = 0;
  1244. struct emac_priv *priv = netdev_priv(ndev);
  1245. struct phy_device *phydev = NULL;
  1246. struct device *phy = NULL;
  1247. ret = pm_runtime_get_sync(&priv->pdev->dev);
  1248. if (ret < 0) {
  1249. pm_runtime_put_noidle(&priv->pdev->dev);
  1250. dev_err(&priv->pdev->dev, "%s: failed to get_sync(%d)\n",
  1251. __func__, ret);
  1252. return ret;
  1253. }
  1254. netif_carrier_off(ndev);
  1255. for (cnt = 0; cnt < ETH_ALEN; cnt++)
  1256. ndev->dev_addr[cnt] = priv->mac_addr[cnt];
  1257. /* Configuration items */
  1258. priv->rx_buf_size = EMAC_DEF_MAX_FRAME_SIZE + NET_IP_ALIGN;
  1259. priv->mac_hash1 = 0;
  1260. priv->mac_hash2 = 0;
  1261. emac_write(EMAC_MACHASH1, 0);
  1262. emac_write(EMAC_MACHASH2, 0);
  1263. for (i = 0; i < EMAC_DEF_RX_NUM_DESC; i++) {
  1264. struct sk_buff *skb = emac_rx_alloc(priv);
  1265. if (!skb)
  1266. break;
  1267. ret = cpdma_chan_submit(priv->rxchan, skb, skb->data,
  1268. skb_tailroom(skb), 0);
  1269. if (WARN_ON(ret < 0))
  1270. break;
  1271. }
  1272. /* Request IRQ */
  1273. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ,
  1274. res_num))) {
  1275. for (irq_num = res->start; irq_num <= res->end; irq_num++) {
  1276. if (request_irq(irq_num, emac_irq, 0, ndev->name,
  1277. ndev)) {
  1278. dev_err(emac_dev,
  1279. "DaVinci EMAC: request_irq() failed\n");
  1280. ret = -EBUSY;
  1281. goto rollback;
  1282. }
  1283. }
  1284. res_num++;
  1285. }
  1286. /* prepare counters for rollback in case of an error */
  1287. res_num--;
  1288. irq_num--;
  1289. /* Start/Enable EMAC hardware */
  1290. emac_hw_enable(priv);
  1291. /* Enable Interrupt pacing if configured */
  1292. if (priv->coal_intvl != 0) {
  1293. struct ethtool_coalesce coal;
  1294. coal.rx_coalesce_usecs = (priv->coal_intvl << 4);
  1295. emac_set_coalesce(ndev, &coal);
  1296. }
  1297. cpdma_ctlr_start(priv->dma);
  1298. if (priv->phy_node) {
  1299. phydev = of_phy_connect(ndev, priv->phy_node,
  1300. &emac_adjust_link, 0, 0);
  1301. if (!phydev) {
  1302. dev_err(emac_dev, "could not connect to phy %pOF\n",
  1303. priv->phy_node);
  1304. ret = -ENODEV;
  1305. goto err;
  1306. }
  1307. }
  1308. /* use the first phy on the bus if pdata did not give us a phy id */
  1309. if (!phydev && !priv->phy_id) {
  1310. /* NOTE: we can't use bus_find_device_by_name() here because
  1311. * the device name is not guaranteed to be 'davinci_mdio'. On
  1312. * some systems it can be 'davinci_mdio.0' so we need to use
  1313. * strncmp() against the first part of the string to correctly
  1314. * match it.
  1315. */
  1316. phy = bus_find_device(&mdio_bus_type, NULL, NULL,
  1317. match_first_device);
  1318. if (phy) {
  1319. priv->phy_id = dev_name(phy);
  1320. if (!priv->phy_id || !*priv->phy_id)
  1321. put_device(phy);
  1322. }
  1323. }
  1324. if (!phydev && priv->phy_id && *priv->phy_id) {
  1325. phydev = phy_connect(ndev, priv->phy_id,
  1326. &emac_adjust_link,
  1327. PHY_INTERFACE_MODE_MII);
  1328. put_device(phy); /* reference taken by bus_find_device */
  1329. if (IS_ERR(phydev)) {
  1330. dev_err(emac_dev, "could not connect to phy %s\n",
  1331. priv->phy_id);
  1332. ret = PTR_ERR(phydev);
  1333. goto err;
  1334. }
  1335. priv->link = 0;
  1336. priv->speed = 0;
  1337. priv->duplex = ~0;
  1338. phy_attached_info(phydev);
  1339. }
  1340. if (!phydev) {
  1341. /* No PHY , fix the link, speed and duplex settings */
  1342. dev_notice(emac_dev, "no phy, defaulting to 100/full\n");
  1343. priv->link = 1;
  1344. priv->speed = SPEED_100;
  1345. priv->duplex = DUPLEX_FULL;
  1346. emac_update_phystatus(priv);
  1347. }
  1348. if (netif_msg_drv(priv))
  1349. dev_notice(emac_dev, "DaVinci EMAC: Opened %s\n", ndev->name);
  1350. if (phydev)
  1351. phy_start(phydev);
  1352. return 0;
  1353. err:
  1354. emac_int_disable(priv);
  1355. napi_disable(&priv->napi);
  1356. rollback:
  1357. for (q = res_num; q >= 0; q--) {
  1358. res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, q);
  1359. /* at the first iteration, irq_num is already set to the
  1360. * right value
  1361. */
  1362. if (q != res_num)
  1363. irq_num = res->end;
  1364. for (m = irq_num; m >= res->start; m--)
  1365. free_irq(m, ndev);
  1366. }
  1367. cpdma_ctlr_stop(priv->dma);
  1368. pm_runtime_put(&priv->pdev->dev);
  1369. return ret;
  1370. }
  1371. /**
  1372. * emac_dev_stop - EMAC device stop
  1373. * @ndev: The DaVinci EMAC network adapter
  1374. *
  1375. * Called when system wants to stop or down the interface. We stop the network
  1376. * queue, disable interrupts and cleanup TX/RX channels.
  1377. *
  1378. * We return the statistics in net_device_stats structure pulled from emac
  1379. */
  1380. static int emac_dev_stop(struct net_device *ndev)
  1381. {
  1382. struct resource *res;
  1383. int i = 0;
  1384. int irq_num;
  1385. struct emac_priv *priv = netdev_priv(ndev);
  1386. struct device *emac_dev = &ndev->dev;
  1387. /* inform the upper layers. */
  1388. netif_stop_queue(ndev);
  1389. napi_disable(&priv->napi);
  1390. netif_carrier_off(ndev);
  1391. emac_int_disable(priv);
  1392. cpdma_ctlr_stop(priv->dma);
  1393. emac_write(EMAC_SOFTRESET, 1);
  1394. if (ndev->phydev)
  1395. phy_disconnect(ndev->phydev);
  1396. /* Free IRQ */
  1397. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, i))) {
  1398. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1399. free_irq(irq_num, priv->ndev);
  1400. i++;
  1401. }
  1402. if (netif_msg_drv(priv))
  1403. dev_notice(emac_dev, "DaVinci EMAC: %s stopped\n", ndev->name);
  1404. pm_runtime_put(&priv->pdev->dev);
  1405. return 0;
  1406. }
  1407. /**
  1408. * emac_dev_getnetstats - EMAC get statistics function
  1409. * @ndev: The DaVinci EMAC network adapter
  1410. *
  1411. * Called when system wants to get statistics from the device.
  1412. *
  1413. * We return the statistics in net_device_stats structure pulled from emac
  1414. */
  1415. static struct net_device_stats *emac_dev_getnetstats(struct net_device *ndev)
  1416. {
  1417. struct emac_priv *priv = netdev_priv(ndev);
  1418. u32 mac_control;
  1419. u32 stats_clear_mask;
  1420. int err;
  1421. err = pm_runtime_get_sync(&priv->pdev->dev);
  1422. if (err < 0) {
  1423. pm_runtime_put_noidle(&priv->pdev->dev);
  1424. dev_err(&priv->pdev->dev, "%s: failed to get_sync(%d)\n",
  1425. __func__, err);
  1426. return &ndev->stats;
  1427. }
  1428. /* update emac hardware stats and reset the registers*/
  1429. mac_control = emac_read(EMAC_MACCONTROL);
  1430. if (mac_control & EMAC_MACCONTROL_GMIIEN)
  1431. stats_clear_mask = EMAC_STATS_CLR_MASK;
  1432. else
  1433. stats_clear_mask = 0;
  1434. ndev->stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
  1435. emac_write(EMAC_RXMCASTFRAMES, stats_clear_mask);
  1436. ndev->stats.collisions += (emac_read(EMAC_TXCOLLISION) +
  1437. emac_read(EMAC_TXSINGLECOLL) +
  1438. emac_read(EMAC_TXMULTICOLL));
  1439. emac_write(EMAC_TXCOLLISION, stats_clear_mask);
  1440. emac_write(EMAC_TXSINGLECOLL, stats_clear_mask);
  1441. emac_write(EMAC_TXMULTICOLL, stats_clear_mask);
  1442. ndev->stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
  1443. emac_read(EMAC_RXJABBER) +
  1444. emac_read(EMAC_RXUNDERSIZED));
  1445. emac_write(EMAC_RXOVERSIZED, stats_clear_mask);
  1446. emac_write(EMAC_RXJABBER, stats_clear_mask);
  1447. emac_write(EMAC_RXUNDERSIZED, stats_clear_mask);
  1448. ndev->stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
  1449. emac_read(EMAC_RXMOFOVERRUNS));
  1450. emac_write(EMAC_RXSOFOVERRUNS, stats_clear_mask);
  1451. emac_write(EMAC_RXMOFOVERRUNS, stats_clear_mask);
  1452. ndev->stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
  1453. emac_write(EMAC_RXDMAOVERRUNS, stats_clear_mask);
  1454. ndev->stats.tx_carrier_errors +=
  1455. emac_read(EMAC_TXCARRIERSENSE);
  1456. emac_write(EMAC_TXCARRIERSENSE, stats_clear_mask);
  1457. ndev->stats.tx_fifo_errors += emac_read(EMAC_TXUNDERRUN);
  1458. emac_write(EMAC_TXUNDERRUN, stats_clear_mask);
  1459. pm_runtime_put(&priv->pdev->dev);
  1460. return &ndev->stats;
  1461. }
  1462. static const struct net_device_ops emac_netdev_ops = {
  1463. .ndo_open = emac_dev_open,
  1464. .ndo_stop = emac_dev_stop,
  1465. .ndo_start_xmit = emac_dev_xmit,
  1466. .ndo_set_rx_mode = emac_dev_mcast_set,
  1467. .ndo_set_mac_address = emac_dev_setmac_addr,
  1468. .ndo_do_ioctl = emac_devioctl,
  1469. .ndo_tx_timeout = emac_dev_tx_timeout,
  1470. .ndo_get_stats = emac_dev_getnetstats,
  1471. #ifdef CONFIG_NET_POLL_CONTROLLER
  1472. .ndo_poll_controller = emac_poll_controller,
  1473. #endif
  1474. };
  1475. static const struct of_device_id davinci_emac_of_match[];
  1476. static struct emac_platform_data *
  1477. davinci_emac_of_get_pdata(struct platform_device *pdev, struct emac_priv *priv)
  1478. {
  1479. struct device_node *np;
  1480. const struct of_device_id *match;
  1481. const struct emac_platform_data *auxdata;
  1482. struct emac_platform_data *pdata = NULL;
  1483. const u8 *mac_addr;
  1484. if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
  1485. return dev_get_platdata(&pdev->dev);
  1486. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1487. if (!pdata)
  1488. return NULL;
  1489. np = pdev->dev.of_node;
  1490. pdata->version = EMAC_VERSION_2;
  1491. if (!is_valid_ether_addr(pdata->mac_addr)) {
  1492. mac_addr = of_get_mac_address(np);
  1493. if (mac_addr)
  1494. ether_addr_copy(pdata->mac_addr, mac_addr);
  1495. }
  1496. of_property_read_u32(np, "ti,davinci-ctrl-reg-offset",
  1497. &pdata->ctrl_reg_offset);
  1498. of_property_read_u32(np, "ti,davinci-ctrl-mod-reg-offset",
  1499. &pdata->ctrl_mod_reg_offset);
  1500. of_property_read_u32(np, "ti,davinci-ctrl-ram-offset",
  1501. &pdata->ctrl_ram_offset);
  1502. of_property_read_u32(np, "ti,davinci-ctrl-ram-size",
  1503. &pdata->ctrl_ram_size);
  1504. of_property_read_u8(np, "ti,davinci-rmii-en", &pdata->rmii_en);
  1505. pdata->no_bd_ram = of_property_read_bool(np, "ti,davinci-no-bd-ram");
  1506. priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
  1507. if (!priv->phy_node) {
  1508. if (!of_phy_is_fixed_link(np))
  1509. pdata->phy_id = NULL;
  1510. else if (of_phy_register_fixed_link(np) >= 0)
  1511. priv->phy_node = of_node_get(np);
  1512. }
  1513. auxdata = pdev->dev.platform_data;
  1514. if (auxdata) {
  1515. pdata->interrupt_enable = auxdata->interrupt_enable;
  1516. pdata->interrupt_disable = auxdata->interrupt_disable;
  1517. }
  1518. match = of_match_device(davinci_emac_of_match, &pdev->dev);
  1519. if (match && match->data) {
  1520. auxdata = match->data;
  1521. pdata->version = auxdata->version;
  1522. pdata->hw_ram_addr = auxdata->hw_ram_addr;
  1523. }
  1524. return pdata;
  1525. }
  1526. static int davinci_emac_try_get_mac(struct platform_device *pdev,
  1527. int instance, u8 *mac_addr)
  1528. {
  1529. if (!pdev->dev.of_node)
  1530. return -EINVAL;
  1531. return ti_cm_get_macid(&pdev->dev, instance, mac_addr);
  1532. }
  1533. /**
  1534. * davinci_emac_probe - EMAC device probe
  1535. * @pdev: The DaVinci EMAC device that we are removing
  1536. *
  1537. * Called when probing for emac devicesr. We get details of instances and
  1538. * resource information from platform init and register a network device
  1539. * and allocate resources necessary for driver to perform
  1540. */
  1541. static int davinci_emac_probe(struct platform_device *pdev)
  1542. {
  1543. struct device_node *np = pdev->dev.of_node;
  1544. int rc = 0;
  1545. struct resource *res, *res_ctrl;
  1546. struct net_device *ndev;
  1547. struct emac_priv *priv;
  1548. unsigned long hw_ram_addr;
  1549. struct emac_platform_data *pdata;
  1550. struct cpdma_params dma_params;
  1551. struct clk *emac_clk;
  1552. unsigned long emac_bus_frequency;
  1553. /* obtain emac clock from kernel */
  1554. emac_clk = devm_clk_get(&pdev->dev, NULL);
  1555. if (IS_ERR(emac_clk)) {
  1556. dev_err(&pdev->dev, "failed to get EMAC clock\n");
  1557. return -EBUSY;
  1558. }
  1559. emac_bus_frequency = clk_get_rate(emac_clk);
  1560. devm_clk_put(&pdev->dev, emac_clk);
  1561. /* TODO: Probe PHY here if possible */
  1562. ndev = alloc_etherdev(sizeof(struct emac_priv));
  1563. if (!ndev)
  1564. return -ENOMEM;
  1565. platform_set_drvdata(pdev, ndev);
  1566. priv = netdev_priv(ndev);
  1567. priv->pdev = pdev;
  1568. priv->ndev = ndev;
  1569. priv->msg_enable = netif_msg_init(debug_level, DAVINCI_EMAC_DEBUG);
  1570. spin_lock_init(&priv->lock);
  1571. pdata = davinci_emac_of_get_pdata(pdev, priv);
  1572. if (!pdata) {
  1573. dev_err(&pdev->dev, "no platform data\n");
  1574. rc = -ENODEV;
  1575. goto err_free_netdev;
  1576. }
  1577. /* MAC addr and PHY mask , RMII enable info from platform_data */
  1578. memcpy(priv->mac_addr, pdata->mac_addr, ETH_ALEN);
  1579. priv->phy_id = pdata->phy_id;
  1580. priv->rmii_en = pdata->rmii_en;
  1581. priv->version = pdata->version;
  1582. priv->int_enable = pdata->interrupt_enable;
  1583. priv->int_disable = pdata->interrupt_disable;
  1584. priv->coal_intvl = 0;
  1585. priv->bus_freq_mhz = (u32)(emac_bus_frequency / 1000000);
  1586. /* Get EMAC platform data */
  1587. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1588. priv->emac_base_phys = res->start + pdata->ctrl_reg_offset;
  1589. priv->remap_addr = devm_ioremap_resource(&pdev->dev, res);
  1590. if (IS_ERR(priv->remap_addr)) {
  1591. rc = PTR_ERR(priv->remap_addr);
  1592. goto no_pdata;
  1593. }
  1594. res_ctrl = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1595. if (res_ctrl) {
  1596. priv->ctrl_base =
  1597. devm_ioremap_resource(&pdev->dev, res_ctrl);
  1598. if (IS_ERR(priv->ctrl_base)) {
  1599. rc = PTR_ERR(priv->ctrl_base);
  1600. goto no_pdata;
  1601. }
  1602. } else {
  1603. priv->ctrl_base = priv->remap_addr + pdata->ctrl_mod_reg_offset;
  1604. }
  1605. priv->emac_base = priv->remap_addr + pdata->ctrl_reg_offset;
  1606. ndev->base_addr = (unsigned long)priv->remap_addr;
  1607. hw_ram_addr = pdata->hw_ram_addr;
  1608. if (!hw_ram_addr)
  1609. hw_ram_addr = (u32 __force)res->start + pdata->ctrl_ram_offset;
  1610. memset(&dma_params, 0, sizeof(dma_params));
  1611. dma_params.dev = &pdev->dev;
  1612. dma_params.dmaregs = priv->emac_base;
  1613. dma_params.rxthresh = priv->emac_base + 0x120;
  1614. dma_params.rxfree = priv->emac_base + 0x140;
  1615. dma_params.txhdp = priv->emac_base + 0x600;
  1616. dma_params.rxhdp = priv->emac_base + 0x620;
  1617. dma_params.txcp = priv->emac_base + 0x640;
  1618. dma_params.rxcp = priv->emac_base + 0x660;
  1619. dma_params.num_chan = EMAC_MAX_TXRX_CHANNELS;
  1620. dma_params.min_packet_size = EMAC_DEF_MIN_ETHPKTSIZE;
  1621. dma_params.desc_hw_addr = hw_ram_addr;
  1622. dma_params.desc_mem_size = pdata->ctrl_ram_size;
  1623. dma_params.desc_align = 16;
  1624. dma_params.desc_mem_phys = pdata->no_bd_ram ? 0 :
  1625. (u32 __force)res->start + pdata->ctrl_ram_offset;
  1626. priv->dma = cpdma_ctlr_create(&dma_params);
  1627. if (!priv->dma) {
  1628. dev_err(&pdev->dev, "error initializing DMA\n");
  1629. rc = -ENOMEM;
  1630. goto no_pdata;
  1631. }
  1632. priv->txchan = cpdma_chan_create(priv->dma, EMAC_DEF_TX_CH,
  1633. emac_tx_handler, 0);
  1634. if (IS_ERR(priv->txchan)) {
  1635. dev_err(&pdev->dev, "error initializing tx dma channel\n");
  1636. rc = PTR_ERR(priv->txchan);
  1637. goto err_free_dma;
  1638. }
  1639. priv->rxchan = cpdma_chan_create(priv->dma, EMAC_DEF_RX_CH,
  1640. emac_rx_handler, 1);
  1641. if (IS_ERR(priv->rxchan)) {
  1642. dev_err(&pdev->dev, "error initializing rx dma channel\n");
  1643. rc = PTR_ERR(priv->rxchan);
  1644. goto err_free_txchan;
  1645. }
  1646. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1647. if (!res) {
  1648. dev_err(&pdev->dev, "error getting irq res\n");
  1649. rc = -ENOENT;
  1650. goto err_free_rxchan;
  1651. }
  1652. ndev->irq = res->start;
  1653. rc = davinci_emac_try_get_mac(pdev, res_ctrl ? 0 : 1, priv->mac_addr);
  1654. if (!rc)
  1655. ether_addr_copy(ndev->dev_addr, priv->mac_addr);
  1656. if (!is_valid_ether_addr(priv->mac_addr)) {
  1657. /* Use random MAC if none passed */
  1658. eth_hw_addr_random(ndev);
  1659. memcpy(priv->mac_addr, ndev->dev_addr, ndev->addr_len);
  1660. dev_warn(&pdev->dev, "using random MAC addr: %pM\n",
  1661. priv->mac_addr);
  1662. }
  1663. ndev->netdev_ops = &emac_netdev_ops;
  1664. ndev->ethtool_ops = &ethtool_ops;
  1665. netif_napi_add(ndev, &priv->napi, emac_poll, EMAC_POLL_WEIGHT);
  1666. pm_runtime_enable(&pdev->dev);
  1667. rc = pm_runtime_get_sync(&pdev->dev);
  1668. if (rc < 0) {
  1669. pm_runtime_put_noidle(&pdev->dev);
  1670. dev_err(&pdev->dev, "%s: failed to get_sync(%d)\n",
  1671. __func__, rc);
  1672. goto err_napi_del;
  1673. }
  1674. /* register the network device */
  1675. SET_NETDEV_DEV(ndev, &pdev->dev);
  1676. rc = register_netdev(ndev);
  1677. if (rc) {
  1678. dev_err(&pdev->dev, "error in register_netdev\n");
  1679. rc = -ENODEV;
  1680. pm_runtime_put(&pdev->dev);
  1681. goto err_napi_del;
  1682. }
  1683. if (netif_msg_probe(priv)) {
  1684. dev_notice(&pdev->dev, "DaVinci EMAC Probe found device "
  1685. "(regs: %pa, irq: %d)\n",
  1686. &priv->emac_base_phys, ndev->irq);
  1687. }
  1688. pm_runtime_put(&pdev->dev);
  1689. return 0;
  1690. err_napi_del:
  1691. netif_napi_del(&priv->napi);
  1692. err_free_rxchan:
  1693. cpdma_chan_destroy(priv->rxchan);
  1694. err_free_txchan:
  1695. cpdma_chan_destroy(priv->txchan);
  1696. err_free_dma:
  1697. cpdma_ctlr_destroy(priv->dma);
  1698. no_pdata:
  1699. if (of_phy_is_fixed_link(np))
  1700. of_phy_deregister_fixed_link(np);
  1701. of_node_put(priv->phy_node);
  1702. err_free_netdev:
  1703. free_netdev(ndev);
  1704. return rc;
  1705. }
  1706. /**
  1707. * davinci_emac_remove - EMAC device remove
  1708. * @pdev: The DaVinci EMAC device that we are removing
  1709. *
  1710. * Called when removing the device driver. We disable clock usage and release
  1711. * the resources taken up by the driver and unregister network device
  1712. */
  1713. static int davinci_emac_remove(struct platform_device *pdev)
  1714. {
  1715. struct net_device *ndev = platform_get_drvdata(pdev);
  1716. struct emac_priv *priv = netdev_priv(ndev);
  1717. struct device_node *np = pdev->dev.of_node;
  1718. dev_notice(&ndev->dev, "DaVinci EMAC: davinci_emac_remove()\n");
  1719. if (priv->txchan)
  1720. cpdma_chan_destroy(priv->txchan);
  1721. if (priv->rxchan)
  1722. cpdma_chan_destroy(priv->rxchan);
  1723. cpdma_ctlr_destroy(priv->dma);
  1724. unregister_netdev(ndev);
  1725. of_node_put(priv->phy_node);
  1726. pm_runtime_disable(&pdev->dev);
  1727. if (of_phy_is_fixed_link(np))
  1728. of_phy_deregister_fixed_link(np);
  1729. free_netdev(ndev);
  1730. return 0;
  1731. }
  1732. static int davinci_emac_suspend(struct device *dev)
  1733. {
  1734. struct platform_device *pdev = to_platform_device(dev);
  1735. struct net_device *ndev = platform_get_drvdata(pdev);
  1736. if (netif_running(ndev))
  1737. emac_dev_stop(ndev);
  1738. return 0;
  1739. }
  1740. static int davinci_emac_resume(struct device *dev)
  1741. {
  1742. struct platform_device *pdev = to_platform_device(dev);
  1743. struct net_device *ndev = platform_get_drvdata(pdev);
  1744. if (netif_running(ndev))
  1745. emac_dev_open(ndev);
  1746. return 0;
  1747. }
  1748. static const struct dev_pm_ops davinci_emac_pm_ops = {
  1749. .suspend = davinci_emac_suspend,
  1750. .resume = davinci_emac_resume,
  1751. };
  1752. #if IS_ENABLED(CONFIG_OF)
  1753. static const struct emac_platform_data am3517_emac_data = {
  1754. .version = EMAC_VERSION_2,
  1755. .hw_ram_addr = 0x01e20000,
  1756. };
  1757. static const struct emac_platform_data dm816_emac_data = {
  1758. .version = EMAC_VERSION_2,
  1759. };
  1760. static const struct of_device_id davinci_emac_of_match[] = {
  1761. {.compatible = "ti,davinci-dm6467-emac", },
  1762. {.compatible = "ti,am3517-emac", .data = &am3517_emac_data, },
  1763. {.compatible = "ti,dm816-emac", .data = &dm816_emac_data, },
  1764. {},
  1765. };
  1766. MODULE_DEVICE_TABLE(of, davinci_emac_of_match);
  1767. #endif
  1768. /* davinci_emac_driver: EMAC platform driver structure */
  1769. static struct platform_driver davinci_emac_driver = {
  1770. .driver = {
  1771. .name = "davinci_emac",
  1772. .pm = &davinci_emac_pm_ops,
  1773. .of_match_table = of_match_ptr(davinci_emac_of_match),
  1774. },
  1775. .probe = davinci_emac_probe,
  1776. .remove = davinci_emac_remove,
  1777. };
  1778. /**
  1779. * davinci_emac_init - EMAC driver module init
  1780. *
  1781. * Called when initializing the driver. We register the driver with
  1782. * the platform.
  1783. */
  1784. static int __init davinci_emac_init(void)
  1785. {
  1786. return platform_driver_register(&davinci_emac_driver);
  1787. }
  1788. late_initcall(davinci_emac_init);
  1789. /**
  1790. * davinci_emac_exit - EMAC driver module exit
  1791. *
  1792. * Called when exiting the driver completely. We unregister the driver with
  1793. * the platform and exit
  1794. */
  1795. static void __exit davinci_emac_exit(void)
  1796. {
  1797. platform_driver_unregister(&davinci_emac_driver);
  1798. }
  1799. module_exit(davinci_emac_exit);
  1800. MODULE_LICENSE("GPL");
  1801. MODULE_AUTHOR("DaVinci EMAC Maintainer: Anant Gole <anantgole@ti.com>");
  1802. MODULE_AUTHOR("DaVinci EMAC Maintainer: Chaithrika U S <chaithrika@ti.com>");
  1803. MODULE_DESCRIPTION("DaVinci EMAC Ethernet driver");