davinci_emac.c 60 KB

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