davinci_emac.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140
  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/pm_runtime.h>
  60. #include <linux/davinci_emac.h>
  61. #include <linux/of.h>
  62. #include <linux/of_address.h>
  63. #include <linux/of_irq.h>
  64. #include <linux/of_net.h>
  65. #include <asm/irq.h>
  66. #include <asm/page.h>
  67. #include "davinci_cpdma.h"
  68. static int debug_level;
  69. module_param(debug_level, int, 0);
  70. MODULE_PARM_DESC(debug_level, "DaVinci EMAC debug level (NETIF_MSG bits)");
  71. /* Netif debug messages possible */
  72. #define DAVINCI_EMAC_DEBUG (NETIF_MSG_DRV | \
  73. NETIF_MSG_PROBE | \
  74. NETIF_MSG_LINK | \
  75. NETIF_MSG_TIMER | \
  76. NETIF_MSG_IFDOWN | \
  77. NETIF_MSG_IFUP | \
  78. NETIF_MSG_RX_ERR | \
  79. NETIF_MSG_TX_ERR | \
  80. NETIF_MSG_TX_QUEUED | \
  81. NETIF_MSG_INTR | \
  82. NETIF_MSG_TX_DONE | \
  83. NETIF_MSG_RX_STATUS | \
  84. NETIF_MSG_PKTDATA | \
  85. NETIF_MSG_HW | \
  86. NETIF_MSG_WOL)
  87. /* version info */
  88. #define EMAC_MAJOR_VERSION 6
  89. #define EMAC_MINOR_VERSION 1
  90. #define EMAC_MODULE_VERSION "6.1"
  91. MODULE_VERSION(EMAC_MODULE_VERSION);
  92. static const char emac_version_string[] = "TI DaVinci EMAC Linux v6.1";
  93. /* Configuration items */
  94. #define EMAC_DEF_PASS_CRC (0) /* Do not pass CRC up to frames */
  95. #define EMAC_DEF_QOS_EN (0) /* EMAC proprietary QoS disabled */
  96. #define EMAC_DEF_NO_BUFF_CHAIN (0) /* No buffer chain */
  97. #define EMAC_DEF_MACCTRL_FRAME_EN (0) /* Discard Maccontrol frames */
  98. #define EMAC_DEF_SHORT_FRAME_EN (0) /* Discard short frames */
  99. #define EMAC_DEF_ERROR_FRAME_EN (0) /* Discard error frames */
  100. #define EMAC_DEF_PROM_EN (0) /* Promiscuous disabled */
  101. #define EMAC_DEF_PROM_CH (0) /* Promiscuous channel is 0 */
  102. #define EMAC_DEF_BCAST_EN (1) /* Broadcast enabled */
  103. #define EMAC_DEF_BCAST_CH (0) /* Broadcast channel is 0 */
  104. #define EMAC_DEF_MCAST_EN (1) /* Multicast enabled */
  105. #define EMAC_DEF_MCAST_CH (0) /* Multicast channel is 0 */
  106. #define EMAC_DEF_TXPRIO_FIXED (1) /* TX Priority is fixed */
  107. #define EMAC_DEF_TXPACING_EN (0) /* TX pacing NOT supported*/
  108. #define EMAC_DEF_BUFFER_OFFSET (0) /* Buffer offset to DMA (future) */
  109. #define EMAC_DEF_MIN_ETHPKTSIZE (60) /* Minimum ethernet pkt size */
  110. #define EMAC_DEF_MAX_FRAME_SIZE (1500 + 14 + 4 + 4)
  111. #define EMAC_DEF_TX_CH (0) /* Default 0th channel */
  112. #define EMAC_DEF_RX_CH (0) /* Default 0th channel */
  113. #define EMAC_DEF_RX_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. const char *phy_id;
  310. #ifdef CONFIG_OF
  311. struct device_node *phy_node;
  312. #endif
  313. struct phy_device *phydev;
  314. spinlock_t lock;
  315. /*platform specific members*/
  316. void (*int_enable) (void);
  317. void (*int_disable) (void);
  318. };
  319. /* EMAC TX Host Error description strings */
  320. static char *emac_txhost_errcodes[16] = {
  321. "No error", "SOP error", "Ownership bit not set in SOP buffer",
  322. "Zero Next Buffer Descriptor Pointer Without EOP",
  323. "Zero Buffer Pointer", "Zero Buffer Length", "Packet Length Error",
  324. "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
  325. "Reserved", "Reserved", "Reserved", "Reserved"
  326. };
  327. /* EMAC RX Host Error description strings */
  328. static char *emac_rxhost_errcodes[16] = {
  329. "No error", "Reserved", "Ownership bit not set in input buffer",
  330. "Reserved", "Zero Buffer Pointer", "Reserved", "Reserved",
  331. "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
  332. "Reserved", "Reserved", "Reserved", "Reserved"
  333. };
  334. /* Helper macros */
  335. #define emac_read(reg) ioread32(priv->emac_base + (reg))
  336. #define emac_write(reg, val) iowrite32(val, priv->emac_base + (reg))
  337. #define emac_ctrl_read(reg) ioread32((priv->ctrl_base + (reg)))
  338. #define emac_ctrl_write(reg, val) iowrite32(val, (priv->ctrl_base + (reg)))
  339. /**
  340. * emac_dump_regs - Dump important EMAC registers to debug terminal
  341. * @priv: The DaVinci EMAC private adapter structure
  342. *
  343. * Executes ethtool set cmd & sets phy mode
  344. *
  345. */
  346. static void emac_dump_regs(struct emac_priv *priv)
  347. {
  348. struct device *emac_dev = &priv->ndev->dev;
  349. /* Print important registers in EMAC */
  350. dev_info(emac_dev, "EMAC Basic registers\n");
  351. if (priv->version == EMAC_VERSION_1) {
  352. dev_info(emac_dev, "EMAC: EWCTL: %08X, EWINTTCNT: %08X\n",
  353. emac_ctrl_read(EMAC_CTRL_EWCTL),
  354. emac_ctrl_read(EMAC_CTRL_EWINTTCNT));
  355. }
  356. dev_info(emac_dev, "EMAC: EmuControl:%08X, FifoControl: %08X\n",
  357. emac_read(EMAC_EMCONTROL), emac_read(EMAC_FIFOCONTROL));
  358. dev_info(emac_dev, "EMAC: MBPEnable:%08X, RXUnicastSet: %08X, "\
  359. "RXMaxLen=%08X\n", emac_read(EMAC_RXMBPENABLE),
  360. emac_read(EMAC_RXUNICASTSET), emac_read(EMAC_RXMAXLEN));
  361. dev_info(emac_dev, "EMAC: MacControl:%08X, MacStatus: %08X, "\
  362. "MacConfig=%08X\n", emac_read(EMAC_MACCONTROL),
  363. emac_read(EMAC_MACSTATUS), emac_read(EMAC_MACCONFIG));
  364. dev_info(emac_dev, "EMAC Statistics\n");
  365. dev_info(emac_dev, "EMAC: rx_good_frames:%d\n",
  366. emac_read(EMAC_RXGOODFRAMES));
  367. dev_info(emac_dev, "EMAC: rx_broadcast_frames:%d\n",
  368. emac_read(EMAC_RXBCASTFRAMES));
  369. dev_info(emac_dev, "EMAC: rx_multicast_frames:%d\n",
  370. emac_read(EMAC_RXMCASTFRAMES));
  371. dev_info(emac_dev, "EMAC: rx_pause_frames:%d\n",
  372. emac_read(EMAC_RXPAUSEFRAMES));
  373. dev_info(emac_dev, "EMAC: rx_crcerrors:%d\n",
  374. emac_read(EMAC_RXCRCERRORS));
  375. dev_info(emac_dev, "EMAC: rx_align_code_errors:%d\n",
  376. emac_read(EMAC_RXALIGNCODEERRORS));
  377. dev_info(emac_dev, "EMAC: rx_oversized_frames:%d\n",
  378. emac_read(EMAC_RXOVERSIZED));
  379. dev_info(emac_dev, "EMAC: rx_jabber_frames:%d\n",
  380. emac_read(EMAC_RXJABBER));
  381. dev_info(emac_dev, "EMAC: rx_undersized_frames:%d\n",
  382. emac_read(EMAC_RXUNDERSIZED));
  383. dev_info(emac_dev, "EMAC: rx_fragments:%d\n",
  384. emac_read(EMAC_RXFRAGMENTS));
  385. dev_info(emac_dev, "EMAC: rx_filtered_frames:%d\n",
  386. emac_read(EMAC_RXFILTERED));
  387. dev_info(emac_dev, "EMAC: rx_qos_filtered_frames:%d\n",
  388. emac_read(EMAC_RXQOSFILTERED));
  389. dev_info(emac_dev, "EMAC: rx_octets:%d\n",
  390. emac_read(EMAC_RXOCTETS));
  391. dev_info(emac_dev, "EMAC: tx_goodframes:%d\n",
  392. emac_read(EMAC_TXGOODFRAMES));
  393. dev_info(emac_dev, "EMAC: tx_bcastframes:%d\n",
  394. emac_read(EMAC_TXBCASTFRAMES));
  395. dev_info(emac_dev, "EMAC: tx_mcastframes:%d\n",
  396. emac_read(EMAC_TXMCASTFRAMES));
  397. dev_info(emac_dev, "EMAC: tx_pause_frames:%d\n",
  398. emac_read(EMAC_TXPAUSEFRAMES));
  399. dev_info(emac_dev, "EMAC: tx_deferred_frames:%d\n",
  400. emac_read(EMAC_TXDEFERRED));
  401. dev_info(emac_dev, "EMAC: tx_collision_frames:%d\n",
  402. emac_read(EMAC_TXCOLLISION));
  403. dev_info(emac_dev, "EMAC: tx_single_coll_frames:%d\n",
  404. emac_read(EMAC_TXSINGLECOLL));
  405. dev_info(emac_dev, "EMAC: tx_mult_coll_frames:%d\n",
  406. emac_read(EMAC_TXMULTICOLL));
  407. dev_info(emac_dev, "EMAC: tx_excessive_collisions:%d\n",
  408. emac_read(EMAC_TXEXCESSIVECOLL));
  409. dev_info(emac_dev, "EMAC: tx_late_collisions:%d\n",
  410. emac_read(EMAC_TXLATECOLL));
  411. dev_info(emac_dev, "EMAC: tx_underrun:%d\n",
  412. emac_read(EMAC_TXUNDERRUN));
  413. dev_info(emac_dev, "EMAC: tx_carrier_sense_errors:%d\n",
  414. emac_read(EMAC_TXCARRIERSENSE));
  415. dev_info(emac_dev, "EMAC: tx_octets:%d\n",
  416. emac_read(EMAC_TXOCTETS));
  417. dev_info(emac_dev, "EMAC: net_octets:%d\n",
  418. emac_read(EMAC_NETOCTETS));
  419. dev_info(emac_dev, "EMAC: rx_sof_overruns:%d\n",
  420. emac_read(EMAC_RXSOFOVERRUNS));
  421. dev_info(emac_dev, "EMAC: rx_mof_overruns:%d\n",
  422. emac_read(EMAC_RXMOFOVERRUNS));
  423. dev_info(emac_dev, "EMAC: rx_dma_overruns:%d\n",
  424. emac_read(EMAC_RXDMAOVERRUNS));
  425. cpdma_ctlr_dump(priv->dma);
  426. }
  427. /**
  428. * emac_get_drvinfo - Get EMAC driver information
  429. * @ndev: The DaVinci EMAC network adapter
  430. * @info: ethtool info structure containing name and version
  431. *
  432. * Returns EMAC driver information (name and version)
  433. *
  434. */
  435. static void emac_get_drvinfo(struct net_device *ndev,
  436. struct ethtool_drvinfo *info)
  437. {
  438. strlcpy(info->driver, emac_version_string, sizeof(info->driver));
  439. strlcpy(info->version, EMAC_MODULE_VERSION, sizeof(info->version));
  440. }
  441. /**
  442. * emac_get_settings - Get EMAC settings
  443. * @ndev: The DaVinci EMAC network adapter
  444. * @ecmd: ethtool command
  445. *
  446. * Executes ethool get command
  447. *
  448. */
  449. static int emac_get_settings(struct net_device *ndev,
  450. struct ethtool_cmd *ecmd)
  451. {
  452. struct emac_priv *priv = netdev_priv(ndev);
  453. if (priv->phydev)
  454. return phy_ethtool_gset(priv->phydev, ecmd);
  455. else
  456. return -EOPNOTSUPP;
  457. }
  458. /**
  459. * emac_set_settings - Set EMAC settings
  460. * @ndev: The DaVinci EMAC network adapter
  461. * @ecmd: ethtool command
  462. *
  463. * Executes ethool set command
  464. *
  465. */
  466. static int emac_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
  467. {
  468. struct emac_priv *priv = netdev_priv(ndev);
  469. if (priv->phydev)
  470. return phy_ethtool_sset(priv->phydev, ecmd);
  471. else
  472. return -EOPNOTSUPP;
  473. }
  474. /**
  475. * emac_get_coalesce - Get interrupt coalesce settings for this device
  476. * @ndev : The DaVinci EMAC network adapter
  477. * @coal : ethtool coalesce settings structure
  478. *
  479. * Fetch the current interrupt coalesce settings
  480. *
  481. */
  482. static int emac_get_coalesce(struct net_device *ndev,
  483. struct ethtool_coalesce *coal)
  484. {
  485. struct emac_priv *priv = netdev_priv(ndev);
  486. coal->rx_coalesce_usecs = priv->coal_intvl;
  487. return 0;
  488. }
  489. /**
  490. * emac_set_coalesce - Set interrupt coalesce settings for this device
  491. * @ndev : The DaVinci EMAC network adapter
  492. * @coal : ethtool coalesce settings structure
  493. *
  494. * Set interrupt coalesce parameters
  495. *
  496. */
  497. static int emac_set_coalesce(struct net_device *ndev,
  498. struct ethtool_coalesce *coal)
  499. {
  500. struct emac_priv *priv = netdev_priv(ndev);
  501. u32 int_ctrl, num_interrupts = 0;
  502. u32 prescale = 0, addnl_dvdr = 1, coal_intvl = 0;
  503. if (!coal->rx_coalesce_usecs)
  504. return -EINVAL;
  505. coal_intvl = coal->rx_coalesce_usecs;
  506. switch (priv->version) {
  507. case EMAC_VERSION_2:
  508. int_ctrl = emac_ctrl_read(EMAC_DM646X_CMINTCTRL);
  509. prescale = priv->bus_freq_mhz * 4;
  510. if (coal_intvl < EMAC_DM646X_CMINTMIN_INTVL)
  511. coal_intvl = EMAC_DM646X_CMINTMIN_INTVL;
  512. if (coal_intvl > EMAC_DM646X_CMINTMAX_INTVL) {
  513. /*
  514. * Interrupt pacer works with 4us Pulse, we can
  515. * throttle further by dilating the 4us pulse.
  516. */
  517. addnl_dvdr = EMAC_DM646X_INTPRESCALE_MASK / prescale;
  518. if (addnl_dvdr > 1) {
  519. prescale *= addnl_dvdr;
  520. if (coal_intvl > (EMAC_DM646X_CMINTMAX_INTVL
  521. * addnl_dvdr))
  522. coal_intvl = (EMAC_DM646X_CMINTMAX_INTVL
  523. * addnl_dvdr);
  524. } else {
  525. addnl_dvdr = 1;
  526. coal_intvl = EMAC_DM646X_CMINTMAX_INTVL;
  527. }
  528. }
  529. num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
  530. int_ctrl |= EMAC_DM646X_INTPACEEN;
  531. int_ctrl &= (~EMAC_DM646X_INTPRESCALE_MASK);
  532. int_ctrl |= (prescale & EMAC_DM646X_INTPRESCALE_MASK);
  533. emac_ctrl_write(EMAC_DM646X_CMINTCTRL, int_ctrl);
  534. emac_ctrl_write(EMAC_DM646X_CMRXINTMAX, num_interrupts);
  535. emac_ctrl_write(EMAC_DM646X_CMTXINTMAX, num_interrupts);
  536. break;
  537. default:
  538. int_ctrl = emac_ctrl_read(EMAC_CTRL_EWINTTCNT);
  539. int_ctrl &= (~EMAC_DM644X_EWINTCNT_MASK);
  540. prescale = coal_intvl * priv->bus_freq_mhz;
  541. if (prescale > EMAC_DM644X_EWINTCNT_MASK) {
  542. prescale = EMAC_DM644X_EWINTCNT_MASK;
  543. coal_intvl = prescale / priv->bus_freq_mhz;
  544. }
  545. emac_ctrl_write(EMAC_CTRL_EWINTTCNT, (int_ctrl | prescale));
  546. break;
  547. }
  548. printk(KERN_INFO"Set coalesce to %d usecs.\n", coal_intvl);
  549. priv->coal_intvl = coal_intvl;
  550. return 0;
  551. }
  552. /* ethtool_ops: DaVinci EMAC Ethtool structure
  553. *
  554. * Ethtool support for EMAC adapter
  555. */
  556. static const struct ethtool_ops ethtool_ops = {
  557. .get_drvinfo = emac_get_drvinfo,
  558. .get_settings = emac_get_settings,
  559. .set_settings = emac_set_settings,
  560. .get_link = ethtool_op_get_link,
  561. .get_coalesce = emac_get_coalesce,
  562. .set_coalesce = emac_set_coalesce,
  563. .get_ts_info = ethtool_op_get_ts_info,
  564. };
  565. /**
  566. * emac_update_phystatus - Update Phy status
  567. * @priv: The DaVinci EMAC private adapter structure
  568. *
  569. * Updates phy status and takes action for network queue if required
  570. * based upon link status
  571. *
  572. */
  573. static void emac_update_phystatus(struct emac_priv *priv)
  574. {
  575. u32 mac_control;
  576. u32 new_duplex;
  577. u32 cur_duplex;
  578. struct net_device *ndev = priv->ndev;
  579. mac_control = emac_read(EMAC_MACCONTROL);
  580. cur_duplex = (mac_control & EMAC_MACCONTROL_FULLDUPLEXEN) ?
  581. DUPLEX_FULL : DUPLEX_HALF;
  582. if (priv->phydev)
  583. new_duplex = priv->phydev->duplex;
  584. else
  585. new_duplex = DUPLEX_FULL;
  586. /* We get called only if link has changed (speed/duplex/status) */
  587. if ((priv->link) && (new_duplex != cur_duplex)) {
  588. priv->duplex = new_duplex;
  589. if (DUPLEX_FULL == priv->duplex)
  590. mac_control |= (EMAC_MACCONTROL_FULLDUPLEXEN);
  591. else
  592. mac_control &= ~(EMAC_MACCONTROL_FULLDUPLEXEN);
  593. }
  594. if (priv->speed == SPEED_1000 && (priv->version == EMAC_VERSION_2)) {
  595. mac_control = emac_read(EMAC_MACCONTROL);
  596. mac_control |= (EMAC_DM646X_MACCONTORL_GIG |
  597. EMAC_DM646X_MACCONTORL_GIGFORCE);
  598. } else {
  599. /* Clear the GIG bit and GIGFORCE bit */
  600. mac_control &= ~(EMAC_DM646X_MACCONTORL_GIGFORCE |
  601. EMAC_DM646X_MACCONTORL_GIG);
  602. if (priv->rmii_en && (priv->speed == SPEED_100))
  603. mac_control |= EMAC_MACCONTROL_RMIISPEED_MASK;
  604. else
  605. mac_control &= ~EMAC_MACCONTROL_RMIISPEED_MASK;
  606. }
  607. /* Update mac_control if changed */
  608. emac_write(EMAC_MACCONTROL, mac_control);
  609. if (priv->link) {
  610. /* link ON */
  611. if (!netif_carrier_ok(ndev))
  612. netif_carrier_on(ndev);
  613. /* reactivate the transmit queue if it is stopped */
  614. if (netif_running(ndev) && netif_queue_stopped(ndev))
  615. netif_wake_queue(ndev);
  616. } else {
  617. /* link OFF */
  618. if (netif_carrier_ok(ndev))
  619. netif_carrier_off(ndev);
  620. if (!netif_queue_stopped(ndev))
  621. netif_stop_queue(ndev);
  622. }
  623. }
  624. /**
  625. * hash_get - Calculate hash value from mac address
  626. * @addr: mac address to delete from hash table
  627. *
  628. * Calculates hash value from mac address
  629. *
  630. */
  631. static u32 hash_get(u8 *addr)
  632. {
  633. u32 hash;
  634. u8 tmpval;
  635. int cnt;
  636. hash = 0;
  637. for (cnt = 0; cnt < 2; cnt++) {
  638. tmpval = *addr++;
  639. hash ^= (tmpval >> 2) ^ (tmpval << 4);
  640. tmpval = *addr++;
  641. hash ^= (tmpval >> 4) ^ (tmpval << 2);
  642. tmpval = *addr++;
  643. hash ^= (tmpval >> 6) ^ (tmpval);
  644. }
  645. return hash & 0x3F;
  646. }
  647. /**
  648. * hash_add - Hash function to add mac addr from hash table
  649. * @priv: The DaVinci EMAC private adapter structure
  650. * @mac_addr: mac address to delete from hash table
  651. *
  652. * Adds mac address to the internal hash table
  653. *
  654. */
  655. static int hash_add(struct emac_priv *priv, u8 *mac_addr)
  656. {
  657. struct device *emac_dev = &priv->ndev->dev;
  658. u32 rc = 0;
  659. u32 hash_bit;
  660. u32 hash_value = hash_get(mac_addr);
  661. if (hash_value >= EMAC_NUM_MULTICAST_BITS) {
  662. if (netif_msg_drv(priv)) {
  663. dev_err(emac_dev, "DaVinci EMAC: hash_add(): Invalid "\
  664. "Hash %08x, should not be greater than %08x",
  665. hash_value, (EMAC_NUM_MULTICAST_BITS - 1));
  666. }
  667. return -1;
  668. }
  669. /* set the hash bit only if not previously set */
  670. if (priv->multicast_hash_cnt[hash_value] == 0) {
  671. rc = 1; /* hash value changed */
  672. if (hash_value < 32) {
  673. hash_bit = BIT(hash_value);
  674. priv->mac_hash1 |= hash_bit;
  675. } else {
  676. hash_bit = BIT((hash_value - 32));
  677. priv->mac_hash2 |= hash_bit;
  678. }
  679. }
  680. /* incr counter for num of mcast addr's mapped to "this" hash bit */
  681. ++priv->multicast_hash_cnt[hash_value];
  682. return rc;
  683. }
  684. /**
  685. * hash_del - Hash function to delete mac addr from hash table
  686. * @priv: The DaVinci EMAC private adapter structure
  687. * @mac_addr: mac address to delete from hash table
  688. *
  689. * Removes mac address from the internal hash table
  690. *
  691. */
  692. static int hash_del(struct emac_priv *priv, u8 *mac_addr)
  693. {
  694. u32 hash_value;
  695. u32 hash_bit;
  696. hash_value = hash_get(mac_addr);
  697. if (priv->multicast_hash_cnt[hash_value] > 0) {
  698. /* dec cntr for num of mcast addr's mapped to this hash bit */
  699. --priv->multicast_hash_cnt[hash_value];
  700. }
  701. /* if counter still > 0, at least one multicast address refers
  702. * to this hash bit. so return 0 */
  703. if (priv->multicast_hash_cnt[hash_value] > 0)
  704. return 0;
  705. if (hash_value < 32) {
  706. hash_bit = BIT(hash_value);
  707. priv->mac_hash1 &= ~hash_bit;
  708. } else {
  709. hash_bit = BIT((hash_value - 32));
  710. priv->mac_hash2 &= ~hash_bit;
  711. }
  712. /* return 1 to indicate change in mac_hash registers reqd */
  713. return 1;
  714. }
  715. /* EMAC multicast operation */
  716. #define EMAC_MULTICAST_ADD 0
  717. #define EMAC_MULTICAST_DEL 1
  718. #define EMAC_ALL_MULTI_SET 2
  719. #define EMAC_ALL_MULTI_CLR 3
  720. /**
  721. * emac_add_mcast - Set multicast address in the EMAC adapter (Internal)
  722. * @priv: The DaVinci EMAC private adapter structure
  723. * @action: multicast operation to perform
  724. * mac_addr: mac address to set
  725. *
  726. * Set multicast addresses in EMAC adapter - internal function
  727. *
  728. */
  729. static void emac_add_mcast(struct emac_priv *priv, u32 action, u8 *mac_addr)
  730. {
  731. struct device *emac_dev = &priv->ndev->dev;
  732. int update = -1;
  733. switch (action) {
  734. case EMAC_MULTICAST_ADD:
  735. update = hash_add(priv, mac_addr);
  736. break;
  737. case EMAC_MULTICAST_DEL:
  738. update = hash_del(priv, mac_addr);
  739. break;
  740. case EMAC_ALL_MULTI_SET:
  741. update = 1;
  742. priv->mac_hash1 = EMAC_ALL_MULTI_REG_VALUE;
  743. priv->mac_hash2 = EMAC_ALL_MULTI_REG_VALUE;
  744. break;
  745. case EMAC_ALL_MULTI_CLR:
  746. update = 1;
  747. priv->mac_hash1 = 0;
  748. priv->mac_hash2 = 0;
  749. memset(&(priv->multicast_hash_cnt[0]), 0,
  750. sizeof(priv->multicast_hash_cnt[0]) *
  751. EMAC_NUM_MULTICAST_BITS);
  752. break;
  753. default:
  754. if (netif_msg_drv(priv))
  755. dev_err(emac_dev, "DaVinci EMAC: add_mcast"\
  756. ": bad operation %d", action);
  757. break;
  758. }
  759. /* write to the hardware only if the register status chances */
  760. if (update > 0) {
  761. emac_write(EMAC_MACHASH1, priv->mac_hash1);
  762. emac_write(EMAC_MACHASH2, priv->mac_hash2);
  763. }
  764. }
  765. /**
  766. * emac_dev_mcast_set - Set multicast address in the EMAC adapter
  767. * @ndev: The DaVinci EMAC network adapter
  768. *
  769. * Set multicast addresses in EMAC adapter
  770. *
  771. */
  772. static void emac_dev_mcast_set(struct net_device *ndev)
  773. {
  774. u32 mbp_enable;
  775. struct emac_priv *priv = netdev_priv(ndev);
  776. mbp_enable = emac_read(EMAC_RXMBPENABLE);
  777. if (ndev->flags & IFF_PROMISC) {
  778. mbp_enable &= (~EMAC_MBP_PROMISCCH(EMAC_DEF_PROM_CH));
  779. mbp_enable |= (EMAC_MBP_RXPROMISC);
  780. } else {
  781. mbp_enable = (mbp_enable & ~EMAC_MBP_RXPROMISC);
  782. if ((ndev->flags & IFF_ALLMULTI) ||
  783. netdev_mc_count(ndev) > EMAC_DEF_MAX_MULTICAST_ADDRESSES) {
  784. mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
  785. emac_add_mcast(priv, EMAC_ALL_MULTI_SET, NULL);
  786. }
  787. if (!netdev_mc_empty(ndev)) {
  788. struct netdev_hw_addr *ha;
  789. mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
  790. emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
  791. /* program multicast address list into EMAC hardware */
  792. netdev_for_each_mc_addr(ha, ndev) {
  793. emac_add_mcast(priv, EMAC_MULTICAST_ADD,
  794. (u8 *) ha->addr);
  795. }
  796. } else {
  797. mbp_enable = (mbp_enable & ~EMAC_MBP_RXMCAST);
  798. emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
  799. }
  800. }
  801. /* Set mbp config register */
  802. emac_write(EMAC_RXMBPENABLE, mbp_enable);
  803. }
  804. /*************************************************************************
  805. * EMAC Hardware manipulation
  806. *************************************************************************/
  807. /**
  808. * emac_int_disable - Disable EMAC module interrupt (from adapter)
  809. * @priv: The DaVinci EMAC private adapter structure
  810. *
  811. * Disable EMAC interrupt on the adapter
  812. *
  813. */
  814. static void emac_int_disable(struct emac_priv *priv)
  815. {
  816. if (priv->version == EMAC_VERSION_2) {
  817. unsigned long flags;
  818. local_irq_save(flags);
  819. /* Program C0_Int_En to zero to turn off
  820. * interrupts to the CPU */
  821. emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0x0);
  822. emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0x0);
  823. /* NOTE: Rx Threshold and Misc interrupts are not disabled */
  824. if (priv->int_disable)
  825. priv->int_disable();
  826. local_irq_restore(flags);
  827. } else {
  828. /* Set DM644x control registers for interrupt control */
  829. emac_ctrl_write(EMAC_CTRL_EWCTL, 0x0);
  830. }
  831. }
  832. /**
  833. * emac_int_enable - Enable EMAC module interrupt (from adapter)
  834. * @priv: The DaVinci EMAC private adapter structure
  835. *
  836. * Enable EMAC interrupt on the adapter
  837. *
  838. */
  839. static void emac_int_enable(struct emac_priv *priv)
  840. {
  841. if (priv->version == EMAC_VERSION_2) {
  842. if (priv->int_enable)
  843. priv->int_enable();
  844. emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0xff);
  845. emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0xff);
  846. /* In addition to turning on interrupt Enable, we need
  847. * ack by writing appropriate values to the EOI
  848. * register */
  849. /* NOTE: Rx Threshold and Misc interrupts are not enabled */
  850. /* ack rxen only then a new pulse will be generated */
  851. emac_write(EMAC_DM646X_MACEOIVECTOR,
  852. EMAC_DM646X_MAC_EOI_C0_RXEN);
  853. /* ack txen- only then a new pulse will be generated */
  854. emac_write(EMAC_DM646X_MACEOIVECTOR,
  855. EMAC_DM646X_MAC_EOI_C0_TXEN);
  856. } else {
  857. /* Set DM644x control registers for interrupt control */
  858. emac_ctrl_write(EMAC_CTRL_EWCTL, 0x1);
  859. }
  860. }
  861. /**
  862. * emac_irq - EMAC interrupt handler
  863. * @irq: interrupt number
  864. * @dev_id: EMAC network adapter data structure ptr
  865. *
  866. * EMAC Interrupt handler - we only schedule NAPI and not process any packets
  867. * here. EVen the interrupt status is checked (TX/RX/Err) in NAPI poll function
  868. *
  869. * Returns interrupt handled condition
  870. */
  871. static irqreturn_t emac_irq(int irq, void *dev_id)
  872. {
  873. struct net_device *ndev = (struct net_device *)dev_id;
  874. struct emac_priv *priv = netdev_priv(ndev);
  875. ++priv->isr_count;
  876. if (likely(netif_running(priv->ndev))) {
  877. emac_int_disable(priv);
  878. napi_schedule(&priv->napi);
  879. } else {
  880. /* we are closing down, so dont process anything */
  881. }
  882. return IRQ_HANDLED;
  883. }
  884. static struct sk_buff *emac_rx_alloc(struct emac_priv *priv)
  885. {
  886. struct sk_buff *skb = netdev_alloc_skb(priv->ndev, priv->rx_buf_size);
  887. if (WARN_ON(!skb))
  888. return NULL;
  889. skb_reserve(skb, NET_IP_ALIGN);
  890. return skb;
  891. }
  892. static void emac_rx_handler(void *token, int len, int status)
  893. {
  894. struct sk_buff *skb = token;
  895. struct net_device *ndev = skb->dev;
  896. struct emac_priv *priv = netdev_priv(ndev);
  897. struct device *emac_dev = &ndev->dev;
  898. int ret;
  899. /* free and bail if we are shutting down */
  900. if (unlikely(!netif_running(ndev))) {
  901. dev_kfree_skb_any(skb);
  902. return;
  903. }
  904. /* recycle on receive error */
  905. if (status < 0) {
  906. ndev->stats.rx_errors++;
  907. goto recycle;
  908. }
  909. /* feed received packet up the stack */
  910. skb_put(skb, len);
  911. skb->protocol = eth_type_trans(skb, ndev);
  912. netif_receive_skb(skb);
  913. ndev->stats.rx_bytes += len;
  914. ndev->stats.rx_packets++;
  915. /* alloc a new packet for receive */
  916. skb = emac_rx_alloc(priv);
  917. if (!skb) {
  918. if (netif_msg_rx_err(priv) && net_ratelimit())
  919. dev_err(emac_dev, "failed rx buffer alloc\n");
  920. return;
  921. }
  922. recycle:
  923. ret = cpdma_chan_submit(priv->rxchan, skb, skb->data,
  924. skb_tailroom(skb), 0, GFP_KERNEL);
  925. WARN_ON(ret == -ENOMEM);
  926. if (unlikely(ret < 0))
  927. dev_kfree_skb_any(skb);
  928. }
  929. static void emac_tx_handler(void *token, int len, int status)
  930. {
  931. struct sk_buff *skb = token;
  932. struct net_device *ndev = skb->dev;
  933. /* Check whether the queue is stopped due to stalled tx dma, if the
  934. * queue is stopped then start the queue as we have free desc for tx
  935. */
  936. if (unlikely(netif_queue_stopped(ndev)))
  937. netif_start_queue(ndev);
  938. ndev->stats.tx_packets++;
  939. ndev->stats.tx_bytes += len;
  940. dev_kfree_skb_any(skb);
  941. }
  942. /**
  943. * emac_dev_xmit - EMAC Transmit function
  944. * @skb: SKB pointer
  945. * @ndev: The DaVinci EMAC network adapter
  946. *
  947. * Called by the system to transmit a packet - we queue the packet in
  948. * EMAC hardware transmit queue
  949. *
  950. * Returns success(NETDEV_TX_OK) or error code (typically out of desc's)
  951. */
  952. static int emac_dev_xmit(struct sk_buff *skb, struct net_device *ndev)
  953. {
  954. struct device *emac_dev = &ndev->dev;
  955. int ret_code;
  956. struct emac_priv *priv = netdev_priv(ndev);
  957. /* If no link, return */
  958. if (unlikely(!priv->link)) {
  959. if (netif_msg_tx_err(priv) && net_ratelimit())
  960. dev_err(emac_dev, "DaVinci EMAC: No link to transmit");
  961. goto fail_tx;
  962. }
  963. ret_code = skb_padto(skb, EMAC_DEF_MIN_ETHPKTSIZE);
  964. if (unlikely(ret_code < 0)) {
  965. if (netif_msg_tx_err(priv) && net_ratelimit())
  966. dev_err(emac_dev, "DaVinci EMAC: packet pad failed");
  967. goto fail_tx;
  968. }
  969. skb_tx_timestamp(skb);
  970. ret_code = cpdma_chan_submit(priv->txchan, skb, skb->data, skb->len,
  971. 0, GFP_KERNEL);
  972. if (unlikely(ret_code != 0)) {
  973. if (netif_msg_tx_err(priv) && net_ratelimit())
  974. dev_err(emac_dev, "DaVinci EMAC: desc submit failed");
  975. goto fail_tx;
  976. }
  977. /* If there is no more tx desc left free then we need to
  978. * tell the kernel to stop sending us tx frames.
  979. */
  980. if (unlikely(!cpdma_check_free_tx_desc(priv->txchan)))
  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. /* MAC address is configured only after the interface is enabled. */
  1129. if (netif_running(ndev)) {
  1130. emac_setmac(priv, EMAC_DEF_RX_CH, priv->mac_addr);
  1131. }
  1132. if (netif_msg_drv(priv))
  1133. dev_notice(emac_dev, "DaVinci EMAC: emac_dev_setmac_addr %pM\n",
  1134. priv->mac_addr);
  1135. return 0;
  1136. }
  1137. /**
  1138. * emac_hw_enable - Enable EMAC hardware for packet transmission/reception
  1139. * @priv: The DaVinci EMAC private adapter structure
  1140. *
  1141. * Enables EMAC hardware for packet processing - enables PHY, enables RX
  1142. * for packet reception and enables device interrupts and then NAPI
  1143. *
  1144. * Returns success (0) or appropriate error code (none right now)
  1145. */
  1146. static int emac_hw_enable(struct emac_priv *priv)
  1147. {
  1148. u32 val, mbp_enable, mac_control;
  1149. /* Soft reset */
  1150. emac_write(EMAC_SOFTRESET, 1);
  1151. while (emac_read(EMAC_SOFTRESET))
  1152. cpu_relax();
  1153. /* Disable interrupt & Set pacing for more interrupts initially */
  1154. emac_int_disable(priv);
  1155. /* Full duplex enable bit set when auto negotiation happens */
  1156. mac_control =
  1157. (((EMAC_DEF_TXPRIO_FIXED) ? (EMAC_MACCONTROL_TXPTYPE) : 0x0) |
  1158. ((priv->speed == 1000) ? EMAC_MACCONTROL_GIGABITEN : 0x0) |
  1159. ((EMAC_DEF_TXPACING_EN) ? (EMAC_MACCONTROL_TXPACEEN) : 0x0) |
  1160. ((priv->duplex == DUPLEX_FULL) ? 0x1 : 0));
  1161. emac_write(EMAC_MACCONTROL, mac_control);
  1162. mbp_enable =
  1163. (((EMAC_DEF_PASS_CRC) ? (EMAC_RXMBP_PASSCRC_MASK) : 0x0) |
  1164. ((EMAC_DEF_QOS_EN) ? (EMAC_RXMBP_QOSEN_MASK) : 0x0) |
  1165. ((EMAC_DEF_NO_BUFF_CHAIN) ? (EMAC_RXMBP_NOCHAIN_MASK) : 0x0) |
  1166. ((EMAC_DEF_MACCTRL_FRAME_EN) ? (EMAC_RXMBP_CMFEN_MASK) : 0x0) |
  1167. ((EMAC_DEF_SHORT_FRAME_EN) ? (EMAC_RXMBP_CSFEN_MASK) : 0x0) |
  1168. ((EMAC_DEF_ERROR_FRAME_EN) ? (EMAC_RXMBP_CEFEN_MASK) : 0x0) |
  1169. ((EMAC_DEF_PROM_EN) ? (EMAC_RXMBP_CAFEN_MASK) : 0x0) |
  1170. ((EMAC_DEF_PROM_CH & EMAC_RXMBP_CHMASK) << \
  1171. EMAC_RXMBP_PROMCH_SHIFT) |
  1172. ((EMAC_DEF_BCAST_EN) ? (EMAC_RXMBP_BROADEN_MASK) : 0x0) |
  1173. ((EMAC_DEF_BCAST_CH & EMAC_RXMBP_CHMASK) << \
  1174. EMAC_RXMBP_BROADCH_SHIFT) |
  1175. ((EMAC_DEF_MCAST_EN) ? (EMAC_RXMBP_MULTIEN_MASK) : 0x0) |
  1176. ((EMAC_DEF_MCAST_CH & EMAC_RXMBP_CHMASK) << \
  1177. EMAC_RXMBP_MULTICH_SHIFT));
  1178. emac_write(EMAC_RXMBPENABLE, mbp_enable);
  1179. emac_write(EMAC_RXMAXLEN, (EMAC_DEF_MAX_FRAME_SIZE &
  1180. EMAC_RX_MAX_LEN_MASK));
  1181. emac_write(EMAC_RXBUFFEROFFSET, (EMAC_DEF_BUFFER_OFFSET &
  1182. EMAC_RX_BUFFER_OFFSET_MASK));
  1183. emac_write(EMAC_RXFILTERLOWTHRESH, 0);
  1184. emac_write(EMAC_RXUNICASTCLEAR, EMAC_RX_UNICAST_CLEAR_ALL);
  1185. priv->rx_addr_type = (emac_read(EMAC_MACCONFIG) >> 8) & 0xFF;
  1186. emac_write(EMAC_MACINTMASKSET, EMAC_MAC_HOST_ERR_INTMASK_VAL);
  1187. emac_setmac(priv, EMAC_DEF_RX_CH, priv->mac_addr);
  1188. /* Enable MII */
  1189. val = emac_read(EMAC_MACCONTROL);
  1190. val |= (EMAC_MACCONTROL_GMIIEN);
  1191. emac_write(EMAC_MACCONTROL, val);
  1192. /* Enable NAPI and interrupts */
  1193. napi_enable(&priv->napi);
  1194. emac_int_enable(priv);
  1195. return 0;
  1196. }
  1197. /**
  1198. * emac_poll - EMAC NAPI Poll function
  1199. * @ndev: The DaVinci EMAC network adapter
  1200. * @budget: Number of receive packets to process (as told by NAPI layer)
  1201. *
  1202. * NAPI Poll function implemented to process packets as per budget. We check
  1203. * the type of interrupt on the device and accordingly call the TX or RX
  1204. * packet processing functions. We follow the budget for RX processing and
  1205. * also put a cap on number of TX pkts processed through config param. The
  1206. * NAPI schedule function is called if more packets pending.
  1207. *
  1208. * Returns number of packets received (in most cases; else TX pkts - rarely)
  1209. */
  1210. static int emac_poll(struct napi_struct *napi, int budget)
  1211. {
  1212. unsigned int mask;
  1213. struct emac_priv *priv = container_of(napi, struct emac_priv, napi);
  1214. struct net_device *ndev = priv->ndev;
  1215. struct device *emac_dev = &ndev->dev;
  1216. u32 status = 0;
  1217. u32 num_tx_pkts = 0, num_rx_pkts = 0;
  1218. /* Check interrupt vectors and call packet processing */
  1219. status = emac_read(EMAC_MACINVECTOR);
  1220. mask = EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC;
  1221. if (priv->version == EMAC_VERSION_2)
  1222. mask = EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC;
  1223. if (status & mask) {
  1224. num_tx_pkts = cpdma_chan_process(priv->txchan,
  1225. EMAC_DEF_TX_MAX_SERVICE);
  1226. } /* TX processing */
  1227. mask = EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC;
  1228. if (priv->version == EMAC_VERSION_2)
  1229. mask = EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC;
  1230. if (status & mask) {
  1231. num_rx_pkts = cpdma_chan_process(priv->rxchan, budget);
  1232. } /* RX processing */
  1233. mask = EMAC_DM644X_MAC_IN_VECTOR_HOST_INT;
  1234. if (priv->version == EMAC_VERSION_2)
  1235. mask = EMAC_DM646X_MAC_IN_VECTOR_HOST_INT;
  1236. if (unlikely(status & mask)) {
  1237. u32 ch, cause;
  1238. dev_err(emac_dev, "DaVinci EMAC: Fatal Hardware Error\n");
  1239. netif_stop_queue(ndev);
  1240. napi_disable(&priv->napi);
  1241. status = emac_read(EMAC_MACSTATUS);
  1242. cause = ((status & EMAC_MACSTATUS_TXERRCODE_MASK) >>
  1243. EMAC_MACSTATUS_TXERRCODE_SHIFT);
  1244. if (cause) {
  1245. ch = ((status & EMAC_MACSTATUS_TXERRCH_MASK) >>
  1246. EMAC_MACSTATUS_TXERRCH_SHIFT);
  1247. if (net_ratelimit()) {
  1248. dev_err(emac_dev, "TX Host error %s on ch=%d\n",
  1249. &emac_txhost_errcodes[cause][0], ch);
  1250. }
  1251. }
  1252. cause = ((status & EMAC_MACSTATUS_RXERRCODE_MASK) >>
  1253. EMAC_MACSTATUS_RXERRCODE_SHIFT);
  1254. if (cause) {
  1255. ch = ((status & EMAC_MACSTATUS_RXERRCH_MASK) >>
  1256. EMAC_MACSTATUS_RXERRCH_SHIFT);
  1257. if (netif_msg_hw(priv) && net_ratelimit())
  1258. dev_err(emac_dev, "RX Host error %s on ch=%d\n",
  1259. &emac_rxhost_errcodes[cause][0], ch);
  1260. }
  1261. } else if (num_rx_pkts < budget) {
  1262. napi_complete(napi);
  1263. emac_int_enable(priv);
  1264. }
  1265. return num_rx_pkts;
  1266. }
  1267. #ifdef CONFIG_NET_POLL_CONTROLLER
  1268. /**
  1269. * emac_poll_controller - EMAC Poll controller function
  1270. * @ndev: The DaVinci EMAC network adapter
  1271. *
  1272. * Polled functionality used by netconsole and others in non interrupt mode
  1273. *
  1274. */
  1275. void emac_poll_controller(struct net_device *ndev)
  1276. {
  1277. struct emac_priv *priv = netdev_priv(ndev);
  1278. emac_int_disable(priv);
  1279. emac_irq(ndev->irq, ndev);
  1280. emac_int_enable(priv);
  1281. }
  1282. #endif
  1283. static void emac_adjust_link(struct net_device *ndev)
  1284. {
  1285. struct emac_priv *priv = netdev_priv(ndev);
  1286. struct phy_device *phydev = priv->phydev;
  1287. unsigned long flags;
  1288. int new_state = 0;
  1289. spin_lock_irqsave(&priv->lock, flags);
  1290. if (phydev->link) {
  1291. /* check the mode of operation - full/half duplex */
  1292. if (phydev->duplex != priv->duplex) {
  1293. new_state = 1;
  1294. priv->duplex = phydev->duplex;
  1295. }
  1296. if (phydev->speed != priv->speed) {
  1297. new_state = 1;
  1298. priv->speed = phydev->speed;
  1299. }
  1300. if (!priv->link) {
  1301. new_state = 1;
  1302. priv->link = 1;
  1303. }
  1304. } else if (priv->link) {
  1305. new_state = 1;
  1306. priv->link = 0;
  1307. priv->speed = 0;
  1308. priv->duplex = ~0;
  1309. }
  1310. if (new_state) {
  1311. emac_update_phystatus(priv);
  1312. phy_print_status(priv->phydev);
  1313. }
  1314. spin_unlock_irqrestore(&priv->lock, flags);
  1315. }
  1316. /*************************************************************************
  1317. * Linux Driver Model
  1318. *************************************************************************/
  1319. /**
  1320. * emac_devioctl - EMAC adapter ioctl
  1321. * @ndev: The DaVinci EMAC network adapter
  1322. * @ifrq: request parameter
  1323. * @cmd: command parameter
  1324. *
  1325. * EMAC driver ioctl function
  1326. *
  1327. * Returns success(0) or appropriate error code
  1328. */
  1329. static int emac_devioctl(struct net_device *ndev, struct ifreq *ifrq, int cmd)
  1330. {
  1331. struct emac_priv *priv = netdev_priv(ndev);
  1332. if (!(netif_running(ndev)))
  1333. return -EINVAL;
  1334. /* TODO: Add phy read and write and private statistics get feature */
  1335. return phy_mii_ioctl(priv->phydev, ifrq, cmd);
  1336. }
  1337. static int match_first_device(struct device *dev, void *data)
  1338. {
  1339. return !strncmp(dev_name(dev), "davinci_mdio", 12);
  1340. }
  1341. /**
  1342. * emac_dev_open - EMAC device open
  1343. * @ndev: The DaVinci EMAC network adapter
  1344. *
  1345. * Called when system wants to start the interface. We init TX/RX channels
  1346. * and enable the hardware for packet reception/transmission and start the
  1347. * network queue.
  1348. *
  1349. * Returns 0 for a successful open, or appropriate error code
  1350. */
  1351. static int emac_dev_open(struct net_device *ndev)
  1352. {
  1353. struct device *emac_dev = &ndev->dev;
  1354. u32 cnt;
  1355. struct resource *res;
  1356. int q, m, ret;
  1357. int i = 0;
  1358. int k = 0;
  1359. struct emac_priv *priv = netdev_priv(ndev);
  1360. pm_runtime_get(&priv->pdev->dev);
  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), 0, 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,
  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. goto err;
  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. ret = -EBUSY;
  1448. err:
  1449. pm_runtime_put(&priv->pdev->dev);
  1450. return ret;
  1451. }
  1452. /**
  1453. * emac_dev_stop - EMAC device stop
  1454. * @ndev: The DaVinci EMAC network adapter
  1455. *
  1456. * Called when system wants to stop or down the interface. We stop the network
  1457. * queue, disable interrupts and cleanup TX/RX channels.
  1458. *
  1459. * We return the statistics in net_device_stats structure pulled from emac
  1460. */
  1461. static int emac_dev_stop(struct net_device *ndev)
  1462. {
  1463. struct resource *res;
  1464. int i = 0;
  1465. int irq_num;
  1466. struct emac_priv *priv = netdev_priv(ndev);
  1467. struct device *emac_dev = &ndev->dev;
  1468. /* inform the upper layers. */
  1469. netif_stop_queue(ndev);
  1470. napi_disable(&priv->napi);
  1471. netif_carrier_off(ndev);
  1472. emac_int_disable(priv);
  1473. cpdma_ctlr_stop(priv->dma);
  1474. emac_write(EMAC_SOFTRESET, 1);
  1475. if (priv->phydev)
  1476. phy_disconnect(priv->phydev);
  1477. /* Free IRQ */
  1478. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, i))) {
  1479. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1480. free_irq(irq_num, priv->ndev);
  1481. i++;
  1482. }
  1483. if (netif_msg_drv(priv))
  1484. dev_notice(emac_dev, "DaVinci EMAC: %s stopped\n", ndev->name);
  1485. pm_runtime_put(&priv->pdev->dev);
  1486. return 0;
  1487. }
  1488. /**
  1489. * emac_dev_getnetstats - EMAC get statistics function
  1490. * @ndev: The DaVinci EMAC network adapter
  1491. *
  1492. * Called when system wants to get statistics from the device.
  1493. *
  1494. * We return the statistics in net_device_stats structure pulled from emac
  1495. */
  1496. static struct net_device_stats *emac_dev_getnetstats(struct net_device *ndev)
  1497. {
  1498. struct emac_priv *priv = netdev_priv(ndev);
  1499. u32 mac_control;
  1500. u32 stats_clear_mask;
  1501. /* update emac hardware stats and reset the registers*/
  1502. mac_control = emac_read(EMAC_MACCONTROL);
  1503. if (mac_control & EMAC_MACCONTROL_GMIIEN)
  1504. stats_clear_mask = EMAC_STATS_CLR_MASK;
  1505. else
  1506. stats_clear_mask = 0;
  1507. ndev->stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
  1508. emac_write(EMAC_RXMCASTFRAMES, stats_clear_mask);
  1509. ndev->stats.collisions += (emac_read(EMAC_TXCOLLISION) +
  1510. emac_read(EMAC_TXSINGLECOLL) +
  1511. emac_read(EMAC_TXMULTICOLL));
  1512. emac_write(EMAC_TXCOLLISION, stats_clear_mask);
  1513. emac_write(EMAC_TXSINGLECOLL, stats_clear_mask);
  1514. emac_write(EMAC_TXMULTICOLL, stats_clear_mask);
  1515. ndev->stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
  1516. emac_read(EMAC_RXJABBER) +
  1517. emac_read(EMAC_RXUNDERSIZED));
  1518. emac_write(EMAC_RXOVERSIZED, stats_clear_mask);
  1519. emac_write(EMAC_RXJABBER, stats_clear_mask);
  1520. emac_write(EMAC_RXUNDERSIZED, stats_clear_mask);
  1521. ndev->stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
  1522. emac_read(EMAC_RXMOFOVERRUNS));
  1523. emac_write(EMAC_RXSOFOVERRUNS, stats_clear_mask);
  1524. emac_write(EMAC_RXMOFOVERRUNS, stats_clear_mask);
  1525. ndev->stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
  1526. emac_write(EMAC_RXDMAOVERRUNS, stats_clear_mask);
  1527. ndev->stats.tx_carrier_errors +=
  1528. emac_read(EMAC_TXCARRIERSENSE);
  1529. emac_write(EMAC_TXCARRIERSENSE, stats_clear_mask);
  1530. ndev->stats.tx_fifo_errors += emac_read(EMAC_TXUNDERRUN);
  1531. emac_write(EMAC_TXUNDERRUN, stats_clear_mask);
  1532. return &ndev->stats;
  1533. }
  1534. static const struct net_device_ops emac_netdev_ops = {
  1535. .ndo_open = emac_dev_open,
  1536. .ndo_stop = emac_dev_stop,
  1537. .ndo_start_xmit = emac_dev_xmit,
  1538. .ndo_set_rx_mode = emac_dev_mcast_set,
  1539. .ndo_set_mac_address = emac_dev_setmac_addr,
  1540. .ndo_do_ioctl = emac_devioctl,
  1541. .ndo_tx_timeout = emac_dev_tx_timeout,
  1542. .ndo_get_stats = emac_dev_getnetstats,
  1543. #ifdef CONFIG_NET_POLL_CONTROLLER
  1544. .ndo_poll_controller = emac_poll_controller,
  1545. #endif
  1546. };
  1547. #ifdef CONFIG_OF
  1548. static struct emac_platform_data
  1549. *davinci_emac_of_get_pdata(struct platform_device *pdev,
  1550. struct emac_priv *priv)
  1551. {
  1552. struct device_node *np;
  1553. struct emac_platform_data *pdata = NULL;
  1554. const u8 *mac_addr;
  1555. u32 data;
  1556. int ret;
  1557. pdata = pdev->dev.platform_data;
  1558. if (!pdata) {
  1559. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1560. if (!pdata)
  1561. goto nodata;
  1562. }
  1563. np = pdev->dev.of_node;
  1564. if (!np)
  1565. goto nodata;
  1566. else
  1567. pdata->version = EMAC_VERSION_2;
  1568. if (!is_valid_ether_addr(pdata->mac_addr)) {
  1569. mac_addr = of_get_mac_address(np);
  1570. if (mac_addr)
  1571. memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
  1572. }
  1573. ret = of_property_read_u32(np, "ti,davinci-ctrl-reg-offset", &data);
  1574. if (!ret)
  1575. pdata->ctrl_reg_offset = data;
  1576. ret = of_property_read_u32(np, "ti,davinci-ctrl-mod-reg-offset",
  1577. &data);
  1578. if (!ret)
  1579. pdata->ctrl_mod_reg_offset = data;
  1580. ret = of_property_read_u32(np, "ti,davinci-ctrl-ram-offset", &data);
  1581. if (!ret)
  1582. pdata->ctrl_ram_offset = data;
  1583. ret = of_property_read_u32(np, "ti,davinci-ctrl-ram-size", &data);
  1584. if (!ret)
  1585. pdata->ctrl_ram_size = data;
  1586. ret = of_property_read_u32(np, "ti,davinci-rmii-en", &data);
  1587. if (!ret)
  1588. pdata->rmii_en = data;
  1589. ret = of_property_read_u32(np, "ti,davinci-no-bd-ram", &data);
  1590. if (!ret)
  1591. pdata->no_bd_ram = data;
  1592. priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
  1593. if (!priv->phy_node)
  1594. pdata->phy_id = "";
  1595. pdev->dev.platform_data = pdata;
  1596. nodata:
  1597. return pdata;
  1598. }
  1599. #else
  1600. static struct emac_platform_data
  1601. *davinci_emac_of_get_pdata(struct platform_device *pdev,
  1602. struct emac_priv *priv)
  1603. {
  1604. return pdev->dev.platform_data;
  1605. }
  1606. #endif
  1607. /**
  1608. * davinci_emac_probe - EMAC device probe
  1609. * @pdev: The DaVinci EMAC device that we are removing
  1610. *
  1611. * Called when probing for emac devicesr. We get details of instances and
  1612. * resource information from platform init and register a network device
  1613. * and allocate resources necessary for driver to perform
  1614. */
  1615. static int davinci_emac_probe(struct platform_device *pdev)
  1616. {
  1617. int rc = 0;
  1618. struct resource *res;
  1619. struct net_device *ndev;
  1620. struct emac_priv *priv;
  1621. unsigned long size, hw_ram_addr;
  1622. struct emac_platform_data *pdata;
  1623. struct device *emac_dev;
  1624. struct cpdma_params dma_params;
  1625. struct clk *emac_clk;
  1626. unsigned long emac_bus_frequency;
  1627. /* obtain emac clock from kernel */
  1628. emac_clk = clk_get(&pdev->dev, NULL);
  1629. if (IS_ERR(emac_clk)) {
  1630. dev_err(&pdev->dev, "failed to get EMAC clock\n");
  1631. return -EBUSY;
  1632. }
  1633. emac_bus_frequency = clk_get_rate(emac_clk);
  1634. clk_put(emac_clk);
  1635. /* TODO: Probe PHY here if possible */
  1636. ndev = alloc_etherdev(sizeof(struct emac_priv));
  1637. if (!ndev) {
  1638. rc = -ENOMEM;
  1639. goto no_ndev;
  1640. }
  1641. platform_set_drvdata(pdev, ndev);
  1642. priv = netdev_priv(ndev);
  1643. priv->pdev = pdev;
  1644. priv->ndev = ndev;
  1645. priv->msg_enable = netif_msg_init(debug_level, DAVINCI_EMAC_DEBUG);
  1646. spin_lock_init(&priv->lock);
  1647. pdata = davinci_emac_of_get_pdata(pdev, priv);
  1648. if (!pdata) {
  1649. dev_err(&pdev->dev, "no platform data\n");
  1650. rc = -ENODEV;
  1651. goto probe_quit;
  1652. }
  1653. /* MAC addr and PHY mask , RMII enable info from platform_data */
  1654. memcpy(priv->mac_addr, pdata->mac_addr, 6);
  1655. priv->phy_id = pdata->phy_id;
  1656. priv->rmii_en = pdata->rmii_en;
  1657. priv->version = pdata->version;
  1658. priv->int_enable = pdata->interrupt_enable;
  1659. priv->int_disable = pdata->interrupt_disable;
  1660. priv->coal_intvl = 0;
  1661. priv->bus_freq_mhz = (u32)(emac_bus_frequency / 1000000);
  1662. emac_dev = &ndev->dev;
  1663. /* Get EMAC platform data */
  1664. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1665. if (!res) {
  1666. dev_err(&pdev->dev,"error getting res\n");
  1667. rc = -ENOENT;
  1668. goto probe_quit;
  1669. }
  1670. priv->emac_base_phys = res->start + pdata->ctrl_reg_offset;
  1671. size = resource_size(res);
  1672. if (!request_mem_region(res->start, size, ndev->name)) {
  1673. dev_err(&pdev->dev, "failed request_mem_region() for regs\n");
  1674. rc = -ENXIO;
  1675. goto probe_quit;
  1676. }
  1677. priv->remap_addr = ioremap(res->start, size);
  1678. if (!priv->remap_addr) {
  1679. dev_err(&pdev->dev, "unable to map IO\n");
  1680. rc = -ENOMEM;
  1681. release_mem_region(res->start, size);
  1682. goto probe_quit;
  1683. }
  1684. priv->emac_base = priv->remap_addr + pdata->ctrl_reg_offset;
  1685. ndev->base_addr = (unsigned long)priv->remap_addr;
  1686. priv->ctrl_base = priv->remap_addr + pdata->ctrl_mod_reg_offset;
  1687. hw_ram_addr = pdata->hw_ram_addr;
  1688. if (!hw_ram_addr)
  1689. hw_ram_addr = (u32 __force)res->start + pdata->ctrl_ram_offset;
  1690. memset(&dma_params, 0, sizeof(dma_params));
  1691. dma_params.dev = emac_dev;
  1692. dma_params.dmaregs = priv->emac_base;
  1693. dma_params.rxthresh = priv->emac_base + 0x120;
  1694. dma_params.rxfree = priv->emac_base + 0x140;
  1695. dma_params.txhdp = priv->emac_base + 0x600;
  1696. dma_params.rxhdp = priv->emac_base + 0x620;
  1697. dma_params.txcp = priv->emac_base + 0x640;
  1698. dma_params.rxcp = priv->emac_base + 0x660;
  1699. dma_params.num_chan = EMAC_MAX_TXRX_CHANNELS;
  1700. dma_params.min_packet_size = EMAC_DEF_MIN_ETHPKTSIZE;
  1701. dma_params.desc_hw_addr = hw_ram_addr;
  1702. dma_params.desc_mem_size = pdata->ctrl_ram_size;
  1703. dma_params.desc_align = 16;
  1704. dma_params.desc_mem_phys = pdata->no_bd_ram ? 0 :
  1705. (u32 __force)res->start + pdata->ctrl_ram_offset;
  1706. priv->dma = cpdma_ctlr_create(&dma_params);
  1707. if (!priv->dma) {
  1708. dev_err(&pdev->dev, "error initializing DMA\n");
  1709. rc = -ENOMEM;
  1710. goto no_dma;
  1711. }
  1712. priv->txchan = cpdma_chan_create(priv->dma, tx_chan_num(EMAC_DEF_TX_CH),
  1713. emac_tx_handler);
  1714. priv->rxchan = cpdma_chan_create(priv->dma, rx_chan_num(EMAC_DEF_RX_CH),
  1715. emac_rx_handler);
  1716. if (WARN_ON(!priv->txchan || !priv->rxchan)) {
  1717. rc = -ENOMEM;
  1718. goto no_irq_res;
  1719. }
  1720. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1721. if (!res) {
  1722. dev_err(&pdev->dev, "error getting irq res\n");
  1723. rc = -ENOENT;
  1724. goto no_irq_res;
  1725. }
  1726. ndev->irq = res->start;
  1727. if (!is_valid_ether_addr(priv->mac_addr)) {
  1728. /* Use random MAC if none passed */
  1729. eth_hw_addr_random(ndev);
  1730. memcpy(priv->mac_addr, ndev->dev_addr, ndev->addr_len);
  1731. dev_warn(&pdev->dev, "using random MAC addr: %pM\n",
  1732. priv->mac_addr);
  1733. }
  1734. ndev->netdev_ops = &emac_netdev_ops;
  1735. SET_ETHTOOL_OPS(ndev, &ethtool_ops);
  1736. netif_napi_add(ndev, &priv->napi, emac_poll, EMAC_POLL_WEIGHT);
  1737. /* register the network device */
  1738. SET_NETDEV_DEV(ndev, &pdev->dev);
  1739. rc = register_netdev(ndev);
  1740. if (rc) {
  1741. dev_err(&pdev->dev, "error in register_netdev\n");
  1742. rc = -ENODEV;
  1743. goto no_irq_res;
  1744. }
  1745. if (netif_msg_probe(priv)) {
  1746. dev_notice(emac_dev, "DaVinci EMAC Probe found device "\
  1747. "(regs: %p, irq: %d)\n",
  1748. (void *)priv->emac_base_phys, ndev->irq);
  1749. }
  1750. pm_runtime_enable(&pdev->dev);
  1751. pm_runtime_resume(&pdev->dev);
  1752. return 0;
  1753. no_irq_res:
  1754. if (priv->txchan)
  1755. cpdma_chan_destroy(priv->txchan);
  1756. if (priv->rxchan)
  1757. cpdma_chan_destroy(priv->rxchan);
  1758. cpdma_ctlr_destroy(priv->dma);
  1759. no_dma:
  1760. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1761. release_mem_region(res->start, resource_size(res));
  1762. iounmap(priv->remap_addr);
  1763. probe_quit:
  1764. free_netdev(ndev);
  1765. no_ndev:
  1766. return rc;
  1767. }
  1768. /**
  1769. * davinci_emac_remove - EMAC device remove
  1770. * @pdev: The DaVinci EMAC device that we are removing
  1771. *
  1772. * Called when removing the device driver. We disable clock usage and release
  1773. * the resources taken up by the driver and unregister network device
  1774. */
  1775. static int davinci_emac_remove(struct platform_device *pdev)
  1776. {
  1777. struct resource *res;
  1778. struct net_device *ndev = platform_get_drvdata(pdev);
  1779. struct emac_priv *priv = netdev_priv(ndev);
  1780. dev_notice(&ndev->dev, "DaVinci EMAC: davinci_emac_remove()\n");
  1781. platform_set_drvdata(pdev, NULL);
  1782. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1783. if (priv->txchan)
  1784. cpdma_chan_destroy(priv->txchan);
  1785. if (priv->rxchan)
  1786. cpdma_chan_destroy(priv->rxchan);
  1787. cpdma_ctlr_destroy(priv->dma);
  1788. release_mem_region(res->start, resource_size(res));
  1789. unregister_netdev(ndev);
  1790. iounmap(priv->remap_addr);
  1791. free_netdev(ndev);
  1792. return 0;
  1793. }
  1794. static int davinci_emac_suspend(struct device *dev)
  1795. {
  1796. struct platform_device *pdev = to_platform_device(dev);
  1797. struct net_device *ndev = platform_get_drvdata(pdev);
  1798. if (netif_running(ndev))
  1799. emac_dev_stop(ndev);
  1800. return 0;
  1801. }
  1802. static int davinci_emac_resume(struct device *dev)
  1803. {
  1804. struct platform_device *pdev = to_platform_device(dev);
  1805. struct net_device *ndev = platform_get_drvdata(pdev);
  1806. if (netif_running(ndev))
  1807. emac_dev_open(ndev);
  1808. return 0;
  1809. }
  1810. static const struct dev_pm_ops davinci_emac_pm_ops = {
  1811. .suspend = davinci_emac_suspend,
  1812. .resume = davinci_emac_resume,
  1813. };
  1814. static const struct of_device_id davinci_emac_of_match[] = {
  1815. {.compatible = "ti,davinci-dm6467-emac", },
  1816. {},
  1817. };
  1818. MODULE_DEVICE_TABLE(of, davinci_emac_of_match);
  1819. /* davinci_emac_driver: EMAC platform driver structure */
  1820. static struct platform_driver davinci_emac_driver = {
  1821. .driver = {
  1822. .name = "davinci_emac",
  1823. .owner = THIS_MODULE,
  1824. .pm = &davinci_emac_pm_ops,
  1825. .of_match_table = of_match_ptr(davinci_emac_of_match),
  1826. },
  1827. .probe = davinci_emac_probe,
  1828. .remove = davinci_emac_remove,
  1829. };
  1830. /**
  1831. * davinci_emac_init - EMAC driver module init
  1832. *
  1833. * Called when initializing the driver. We register the driver with
  1834. * the platform.
  1835. */
  1836. static int __init davinci_emac_init(void)
  1837. {
  1838. return platform_driver_register(&davinci_emac_driver);
  1839. }
  1840. late_initcall(davinci_emac_init);
  1841. /**
  1842. * davinci_emac_exit - EMAC driver module exit
  1843. *
  1844. * Called when exiting the driver completely. We unregister the driver with
  1845. * the platform and exit
  1846. */
  1847. static void __exit davinci_emac_exit(void)
  1848. {
  1849. platform_driver_unregister(&davinci_emac_driver);
  1850. }
  1851. module_exit(davinci_emac_exit);
  1852. MODULE_LICENSE("GPL");
  1853. MODULE_AUTHOR("DaVinci EMAC Maintainer: Anant Gole <anantgole@ti.com>");
  1854. MODULE_AUTHOR("DaVinci EMAC Maintainer: Chaithrika U S <chaithrika@ti.com>");
  1855. MODULE_DESCRIPTION("DaVinci EMAC Ethernet driver");