davinci_emac.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848
  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. /** Pending Items in this driver:
  32. * 1. Use Linux cache infrastcture for DMA'ed memory (dma_xxx functions)
  33. */
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/sched.h>
  37. #include <linux/string.h>
  38. #include <linux/timer.h>
  39. #include <linux/errno.h>
  40. #include <linux/in.h>
  41. #include <linux/ioport.h>
  42. #include <linux/slab.h>
  43. #include <linux/mm.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/init.h>
  46. #include <linux/netdevice.h>
  47. #include <linux/etherdevice.h>
  48. #include <linux/skbuff.h>
  49. #include <linux/ethtool.h>
  50. #include <linux/highmem.h>
  51. #include <linux/proc_fs.h>
  52. #include <linux/ctype.h>
  53. #include <linux/version.h>
  54. #include <linux/spinlock.h>
  55. #include <linux/dma-mapping.h>
  56. #include <linux/clk.h>
  57. #include <linux/platform_device.h>
  58. #include <linux/semaphore.h>
  59. #include <linux/phy.h>
  60. #include <linux/bitops.h>
  61. #include <linux/io.h>
  62. #include <linux/uaccess.h>
  63. #include <asm/irq.h>
  64. #include <asm/page.h>
  65. #include <mach/emac.h>
  66. static int debug_level;
  67. module_param(debug_level, int, 0);
  68. MODULE_PARM_DESC(debug_level, "DaVinci EMAC debug level (NETIF_MSG bits)");
  69. /* Netif debug messages possible */
  70. #define DAVINCI_EMAC_DEBUG (NETIF_MSG_DRV | \
  71. NETIF_MSG_PROBE | \
  72. NETIF_MSG_LINK | \
  73. NETIF_MSG_TIMER | \
  74. NETIF_MSG_IFDOWN | \
  75. NETIF_MSG_IFUP | \
  76. NETIF_MSG_RX_ERR | \
  77. NETIF_MSG_TX_ERR | \
  78. NETIF_MSG_TX_QUEUED | \
  79. NETIF_MSG_INTR | \
  80. NETIF_MSG_TX_DONE | \
  81. NETIF_MSG_RX_STATUS | \
  82. NETIF_MSG_PKTDATA | \
  83. NETIF_MSG_HW | \
  84. NETIF_MSG_WOL)
  85. /* version info */
  86. #define EMAC_MAJOR_VERSION 6
  87. #define EMAC_MINOR_VERSION 1
  88. #define EMAC_MODULE_VERSION "6.1"
  89. MODULE_VERSION(EMAC_MODULE_VERSION);
  90. static const char emac_version_string[] = "TI DaVinci EMAC Linux v6.1";
  91. /* Configuration items */
  92. #define EMAC_DEF_PASS_CRC (0) /* Do not pass CRC upto frames */
  93. #define EMAC_DEF_QOS_EN (0) /* EMAC proprietary QoS disabled */
  94. #define EMAC_DEF_NO_BUFF_CHAIN (0) /* No buffer chain */
  95. #define EMAC_DEF_MACCTRL_FRAME_EN (0) /* Discard Maccontrol frames */
  96. #define EMAC_DEF_SHORT_FRAME_EN (0) /* Discard short frames */
  97. #define EMAC_DEF_ERROR_FRAME_EN (0) /* Discard error frames */
  98. #define EMAC_DEF_PROM_EN (0) /* Promiscous disabled */
  99. #define EMAC_DEF_PROM_CH (0) /* Promiscous channel is 0 */
  100. #define EMAC_DEF_BCAST_EN (1) /* Broadcast enabled */
  101. #define EMAC_DEF_BCAST_CH (0) /* Broadcast channel is 0 */
  102. #define EMAC_DEF_MCAST_EN (1) /* Multicast enabled */
  103. #define EMAC_DEF_MCAST_CH (0) /* Multicast channel is 0 */
  104. #define EMAC_DEF_TXPRIO_FIXED (1) /* TX Priority is fixed */
  105. #define EMAC_DEF_TXPACING_EN (0) /* TX pacing NOT supported*/
  106. #define EMAC_DEF_BUFFER_OFFSET (0) /* Buffer offset to DMA (future) */
  107. #define EMAC_DEF_MIN_ETHPKTSIZE (60) /* Minimum ethernet pkt size */
  108. #define EMAC_DEF_MAX_FRAME_SIZE (1500 + 14 + 4 + 4)
  109. #define EMAC_DEF_TX_CH (0) /* Default 0th channel */
  110. #define EMAC_DEF_RX_CH (0) /* Default 0th channel */
  111. #define EMAC_DEF_MDIO_TICK_MS (10) /* typically 1 tick=1 ms) */
  112. #define EMAC_DEF_MAX_TX_CH (1) /* Max TX channels configured */
  113. #define EMAC_DEF_MAX_RX_CH (1) /* Max RX channels configured */
  114. #define EMAC_POLL_WEIGHT (64) /* Default NAPI poll weight */
  115. /* Buffer descriptor parameters */
  116. #define EMAC_DEF_TX_MAX_SERVICE (32) /* TX max service BD's */
  117. #define EMAC_DEF_RX_MAX_SERVICE (64) /* should = netdev->weight */
  118. /* EMAC register related defines */
  119. #define EMAC_ALL_MULTI_REG_VALUE (0xFFFFFFFF)
  120. #define EMAC_NUM_MULTICAST_BITS (64)
  121. #define EMAC_TEARDOWN_VALUE (0xFFFFFFFC)
  122. #define EMAC_TX_CONTROL_TX_ENABLE_VAL (0x1)
  123. #define EMAC_RX_CONTROL_RX_ENABLE_VAL (0x1)
  124. #define EMAC_MAC_HOST_ERR_INTMASK_VAL (0x2)
  125. #define EMAC_RX_UNICAST_CLEAR_ALL (0xFF)
  126. #define EMAC_INT_MASK_CLEAR (0xFF)
  127. /* RX MBP register bit positions */
  128. #define EMAC_RXMBP_PASSCRC_MASK BIT(30)
  129. #define EMAC_RXMBP_QOSEN_MASK BIT(29)
  130. #define EMAC_RXMBP_NOCHAIN_MASK BIT(28)
  131. #define EMAC_RXMBP_CMFEN_MASK BIT(24)
  132. #define EMAC_RXMBP_CSFEN_MASK BIT(23)
  133. #define EMAC_RXMBP_CEFEN_MASK BIT(22)
  134. #define EMAC_RXMBP_CAFEN_MASK BIT(21)
  135. #define EMAC_RXMBP_PROMCH_SHIFT (16)
  136. #define EMAC_RXMBP_PROMCH_MASK (0x7 << 16)
  137. #define EMAC_RXMBP_BROADEN_MASK BIT(13)
  138. #define EMAC_RXMBP_BROADCH_SHIFT (8)
  139. #define EMAC_RXMBP_BROADCH_MASK (0x7 << 8)
  140. #define EMAC_RXMBP_MULTIEN_MASK BIT(5)
  141. #define EMAC_RXMBP_MULTICH_SHIFT (0)
  142. #define EMAC_RXMBP_MULTICH_MASK (0x7)
  143. #define EMAC_RXMBP_CHMASK (0x7)
  144. /* EMAC register definitions/bit maps used */
  145. # define EMAC_MBP_RXPROMISC (0x00200000)
  146. # define EMAC_MBP_PROMISCCH(ch) (((ch) & 0x7) << 16)
  147. # define EMAC_MBP_RXBCAST (0x00002000)
  148. # define EMAC_MBP_BCASTCHAN(ch) (((ch) & 0x7) << 8)
  149. # define EMAC_MBP_RXMCAST (0x00000020)
  150. # define EMAC_MBP_MCASTCHAN(ch) ((ch) & 0x7)
  151. /* EMAC mac_control register */
  152. #define EMAC_MACCONTROL_TXPTYPE BIT(9)
  153. #define EMAC_MACCONTROL_TXPACEEN BIT(6)
  154. #define EMAC_MACCONTROL_GMIIEN BIT(5)
  155. #define EMAC_MACCONTROL_GIGABITEN BIT(7)
  156. #define EMAC_MACCONTROL_FULLDUPLEXEN BIT(0)
  157. #define EMAC_MACCONTROL_RMIISPEED_MASK BIT(15)
  158. /* GIGABIT MODE related bits */
  159. #define EMAC_DM646X_MACCONTORL_GIG BIT(7)
  160. #define EMAC_DM646X_MACCONTORL_GIGFORCE BIT(17)
  161. /* EMAC mac_status register */
  162. #define EMAC_MACSTATUS_TXERRCODE_MASK (0xF00000)
  163. #define EMAC_MACSTATUS_TXERRCODE_SHIFT (20)
  164. #define EMAC_MACSTATUS_TXERRCH_MASK (0x7)
  165. #define EMAC_MACSTATUS_TXERRCH_SHIFT (16)
  166. #define EMAC_MACSTATUS_RXERRCODE_MASK (0xF000)
  167. #define EMAC_MACSTATUS_RXERRCODE_SHIFT (12)
  168. #define EMAC_MACSTATUS_RXERRCH_MASK (0x7)
  169. #define EMAC_MACSTATUS_RXERRCH_SHIFT (8)
  170. /* EMAC RX register masks */
  171. #define EMAC_RX_MAX_LEN_MASK (0xFFFF)
  172. #define EMAC_RX_BUFFER_OFFSET_MASK (0xFFFF)
  173. /* MAC_IN_VECTOR (0x180) register bit fields */
  174. #define EMAC_DM644X_MAC_IN_VECTOR_HOST_INT BIT(17)
  175. #define EMAC_DM644X_MAC_IN_VECTOR_STATPEND_INT BIT(16)
  176. #define EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC BIT(8)
  177. #define EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC BIT(0)
  178. /** NOTE:: For DM646x the IN_VECTOR has changed */
  179. #define EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC BIT(EMAC_DEF_RX_CH)
  180. #define EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC BIT(16 + EMAC_DEF_TX_CH)
  181. #define EMAC_DM646X_MAC_IN_VECTOR_HOST_INT BIT(26)
  182. #define EMAC_DM646X_MAC_IN_VECTOR_STATPEND_INT BIT(27)
  183. /* CPPI bit positions */
  184. #define EMAC_CPPI_SOP_BIT BIT(31)
  185. #define EMAC_CPPI_EOP_BIT BIT(30)
  186. #define EMAC_CPPI_OWNERSHIP_BIT BIT(29)
  187. #define EMAC_CPPI_EOQ_BIT BIT(28)
  188. #define EMAC_CPPI_TEARDOWN_COMPLETE_BIT BIT(27)
  189. #define EMAC_CPPI_PASS_CRC_BIT BIT(26)
  190. #define EMAC_RX_BD_BUF_SIZE (0xFFFF)
  191. #define EMAC_BD_LENGTH_FOR_CACHE (16) /* only CPPI bytes */
  192. #define EMAC_RX_BD_PKT_LENGTH_MASK (0xFFFF)
  193. /* Max hardware defines */
  194. #define EMAC_MAX_TXRX_CHANNELS (8) /* Max hardware channels */
  195. #define EMAC_DEF_MAX_MULTICAST_ADDRESSES (64) /* Max mcast addr's */
  196. /* EMAC Peripheral Device Register Memory Layout structure */
  197. #define EMAC_TXIDVER 0x0
  198. #define EMAC_TXCONTROL 0x4
  199. #define EMAC_TXTEARDOWN 0x8
  200. #define EMAC_RXIDVER 0x10
  201. #define EMAC_RXCONTROL 0x14
  202. #define EMAC_RXTEARDOWN 0x18
  203. #define EMAC_TXINTSTATRAW 0x80
  204. #define EMAC_TXINTSTATMASKED 0x84
  205. #define EMAC_TXINTMASKSET 0x88
  206. #define EMAC_TXINTMASKCLEAR 0x8C
  207. #define EMAC_MACINVECTOR 0x90
  208. #define EMAC_DM646X_MACEOIVECTOR 0x94
  209. #define EMAC_RXINTSTATRAW 0xA0
  210. #define EMAC_RXINTSTATMASKED 0xA4
  211. #define EMAC_RXINTMASKSET 0xA8
  212. #define EMAC_RXINTMASKCLEAR 0xAC
  213. #define EMAC_MACINTSTATRAW 0xB0
  214. #define EMAC_MACINTSTATMASKED 0xB4
  215. #define EMAC_MACINTMASKSET 0xB8
  216. #define EMAC_MACINTMASKCLEAR 0xBC
  217. #define EMAC_RXMBPENABLE 0x100
  218. #define EMAC_RXUNICASTSET 0x104
  219. #define EMAC_RXUNICASTCLEAR 0x108
  220. #define EMAC_RXMAXLEN 0x10C
  221. #define EMAC_RXBUFFEROFFSET 0x110
  222. #define EMAC_RXFILTERLOWTHRESH 0x114
  223. #define EMAC_MACCONTROL 0x160
  224. #define EMAC_MACSTATUS 0x164
  225. #define EMAC_EMCONTROL 0x168
  226. #define EMAC_FIFOCONTROL 0x16C
  227. #define EMAC_MACCONFIG 0x170
  228. #define EMAC_SOFTRESET 0x174
  229. #define EMAC_MACSRCADDRLO 0x1D0
  230. #define EMAC_MACSRCADDRHI 0x1D4
  231. #define EMAC_MACHASH1 0x1D8
  232. #define EMAC_MACHASH2 0x1DC
  233. #define EMAC_MACADDRLO 0x500
  234. #define EMAC_MACADDRHI 0x504
  235. #define EMAC_MACINDEX 0x508
  236. /* EMAC HDP and Completion registors */
  237. #define EMAC_TXHDP(ch) (0x600 + (ch * 4))
  238. #define EMAC_RXHDP(ch) (0x620 + (ch * 4))
  239. #define EMAC_TXCP(ch) (0x640 + (ch * 4))
  240. #define EMAC_RXCP(ch) (0x660 + (ch * 4))
  241. /* EMAC statistics registers */
  242. #define EMAC_RXGOODFRAMES 0x200
  243. #define EMAC_RXBCASTFRAMES 0x204
  244. #define EMAC_RXMCASTFRAMES 0x208
  245. #define EMAC_RXPAUSEFRAMES 0x20C
  246. #define EMAC_RXCRCERRORS 0x210
  247. #define EMAC_RXALIGNCODEERRORS 0x214
  248. #define EMAC_RXOVERSIZED 0x218
  249. #define EMAC_RXJABBER 0x21C
  250. #define EMAC_RXUNDERSIZED 0x220
  251. #define EMAC_RXFRAGMENTS 0x224
  252. #define EMAC_RXFILTERED 0x228
  253. #define EMAC_RXQOSFILTERED 0x22C
  254. #define EMAC_RXOCTETS 0x230
  255. #define EMAC_TXGOODFRAMES 0x234
  256. #define EMAC_TXBCASTFRAMES 0x238
  257. #define EMAC_TXMCASTFRAMES 0x23C
  258. #define EMAC_TXPAUSEFRAMES 0x240
  259. #define EMAC_TXDEFERRED 0x244
  260. #define EMAC_TXCOLLISION 0x248
  261. #define EMAC_TXSINGLECOLL 0x24C
  262. #define EMAC_TXMULTICOLL 0x250
  263. #define EMAC_TXEXCESSIVECOLL 0x254
  264. #define EMAC_TXLATECOLL 0x258
  265. #define EMAC_TXUNDERRUN 0x25C
  266. #define EMAC_TXCARRIERSENSE 0x260
  267. #define EMAC_TXOCTETS 0x264
  268. #define EMAC_NETOCTETS 0x280
  269. #define EMAC_RXSOFOVERRUNS 0x284
  270. #define EMAC_RXMOFOVERRUNS 0x288
  271. #define EMAC_RXDMAOVERRUNS 0x28C
  272. /* EMAC DM644x control registers */
  273. #define EMAC_CTRL_EWCTL (0x4)
  274. #define EMAC_CTRL_EWINTTCNT (0x8)
  275. /* EMAC MDIO related */
  276. /* Mask & Control defines */
  277. #define MDIO_CONTROL_CLKDIV (0xFF)
  278. #define MDIO_CONTROL_ENABLE BIT(30)
  279. #define MDIO_USERACCESS_GO BIT(31)
  280. #define MDIO_USERACCESS_WRITE BIT(30)
  281. #define MDIO_USERACCESS_READ (0)
  282. #define MDIO_USERACCESS_REGADR (0x1F << 21)
  283. #define MDIO_USERACCESS_PHYADR (0x1F << 16)
  284. #define MDIO_USERACCESS_DATA (0xFFFF)
  285. #define MDIO_USERPHYSEL_LINKSEL BIT(7)
  286. #define MDIO_VER_MODID (0xFFFF << 16)
  287. #define MDIO_VER_REVMAJ (0xFF << 8)
  288. #define MDIO_VER_REVMIN (0xFF)
  289. #define MDIO_USERACCESS(inst) (0x80 + (inst * 8))
  290. #define MDIO_USERPHYSEL(inst) (0x84 + (inst * 8))
  291. #define MDIO_CONTROL (0x04)
  292. /* EMAC DM646X control module registers */
  293. #define EMAC_DM646X_CMRXINTEN (0x14)
  294. #define EMAC_DM646X_CMTXINTEN (0x18)
  295. /* EMAC EOI codes for C0 */
  296. #define EMAC_DM646X_MAC_EOI_C0_RXEN (0x01)
  297. #define EMAC_DM646X_MAC_EOI_C0_TXEN (0x02)
  298. /* EMAC Stats Clear Mask */
  299. #define EMAC_STATS_CLR_MASK (0xFFFFFFFF)
  300. /** net_buf_obj: EMAC network bufferdata structure
  301. *
  302. * EMAC network buffer data structure
  303. */
  304. struct emac_netbufobj {
  305. void *buf_token;
  306. char *data_ptr;
  307. int length;
  308. };
  309. /** net_pkt_obj: EMAC network packet data structure
  310. *
  311. * EMAC network packet data structure - supports buffer list (for future)
  312. */
  313. struct emac_netpktobj {
  314. void *pkt_token; /* data token may hold tx/rx chan id */
  315. struct emac_netbufobj *buf_list; /* array of network buffer objects */
  316. int num_bufs;
  317. int pkt_length;
  318. };
  319. /** emac_tx_bd: EMAC TX Buffer descriptor data structure
  320. *
  321. * EMAC TX Buffer descriptor data structure
  322. */
  323. struct emac_tx_bd {
  324. int h_next;
  325. int buff_ptr;
  326. int off_b_len;
  327. int mode; /* SOP, EOP, ownership, EOQ, teardown,Qstarv, length */
  328. struct emac_tx_bd __iomem *next;
  329. void *buf_token;
  330. };
  331. /** emac_txch: EMAC TX Channel data structure
  332. *
  333. * EMAC TX Channel data structure
  334. */
  335. struct emac_txch {
  336. /* Config related */
  337. u32 num_bd;
  338. u32 service_max;
  339. /* CPPI specific */
  340. u32 alloc_size;
  341. void __iomem *bd_mem;
  342. struct emac_tx_bd __iomem *bd_pool_head;
  343. struct emac_tx_bd __iomem *active_queue_head;
  344. struct emac_tx_bd __iomem *active_queue_tail;
  345. struct emac_tx_bd __iomem *last_hw_bdprocessed;
  346. u32 queue_active;
  347. u32 teardown_pending;
  348. u32 *tx_complete;
  349. /** statistics */
  350. u32 proc_count; /* TX: # of times emac_tx_bdproc is called */
  351. u32 mis_queued_packets;
  352. u32 queue_reinit;
  353. u32 end_of_queue_add;
  354. u32 out_of_tx_bd;
  355. u32 no_active_pkts; /* IRQ when there were no packets to process */
  356. u32 active_queue_count;
  357. };
  358. /** emac_rx_bd: EMAC RX Buffer descriptor data structure
  359. *
  360. * EMAC RX Buffer descriptor data structure
  361. */
  362. struct emac_rx_bd {
  363. int h_next;
  364. int buff_ptr;
  365. int off_b_len;
  366. int mode;
  367. struct emac_rx_bd __iomem *next;
  368. void *data_ptr;
  369. void *buf_token;
  370. };
  371. /** emac_rxch: EMAC RX Channel data structure
  372. *
  373. * EMAC RX Channel data structure
  374. */
  375. struct emac_rxch {
  376. /* configuration info */
  377. u32 num_bd;
  378. u32 service_max;
  379. u32 buf_size;
  380. char mac_addr[6];
  381. /** CPPI specific */
  382. u32 alloc_size;
  383. void __iomem *bd_mem;
  384. struct emac_rx_bd __iomem *bd_pool_head;
  385. struct emac_rx_bd __iomem *active_queue_head;
  386. struct emac_rx_bd __iomem *active_queue_tail;
  387. u32 queue_active;
  388. u32 teardown_pending;
  389. /* packet and buffer objects */
  390. struct emac_netpktobj pkt_queue;
  391. struct emac_netbufobj buf_queue;
  392. /** statistics */
  393. u32 proc_count; /* number of times emac_rx_bdproc is called */
  394. u32 processed_bd;
  395. u32 recycled_bd;
  396. u32 out_of_rx_bd;
  397. u32 out_of_rx_buffers;
  398. u32 queue_reinit;
  399. u32 end_of_queue_add;
  400. u32 end_of_queue;
  401. u32 mis_queued_packets;
  402. };
  403. /* emac_priv: EMAC private data structure
  404. *
  405. * EMAC adapter private data structure
  406. */
  407. struct emac_priv {
  408. u32 msg_enable;
  409. struct net_device *ndev;
  410. struct platform_device *pdev;
  411. struct napi_struct napi;
  412. char mac_addr[6];
  413. spinlock_t tx_lock;
  414. spinlock_t rx_lock;
  415. void __iomem *remap_addr;
  416. u32 emac_base_phys;
  417. void __iomem *emac_base;
  418. void __iomem *ctrl_base;
  419. void __iomem *emac_ctrl_ram;
  420. u32 ctrl_ram_size;
  421. struct emac_txch *txch[EMAC_DEF_MAX_TX_CH];
  422. struct emac_rxch *rxch[EMAC_DEF_MAX_RX_CH];
  423. u32 link; /* 1=link on, 0=link off */
  424. u32 speed; /* 0=Auto Neg, 1=No PHY, 10,100, 1000 - mbps */
  425. u32 duplex; /* Link duplex: 0=Half, 1=Full */
  426. u32 rx_buf_size;
  427. u32 isr_count;
  428. u8 rmii_en;
  429. u8 version;
  430. struct net_device_stats net_dev_stats;
  431. u32 mac_hash1;
  432. u32 mac_hash2;
  433. u32 multicast_hash_cnt[EMAC_NUM_MULTICAST_BITS];
  434. u32 rx_addr_type;
  435. /* periodic timer required for MDIO polling */
  436. struct timer_list periodic_timer;
  437. u32 periodic_ticks;
  438. u32 timer_active;
  439. u32 phy_mask;
  440. /* mii_bus,phy members */
  441. struct mii_bus *mii_bus;
  442. struct phy_device *phydev;
  443. spinlock_t lock;
  444. };
  445. /* clock frequency for EMAC */
  446. static struct clk *emac_clk;
  447. static unsigned long emac_bus_frequency;
  448. static unsigned long mdio_max_freq;
  449. /* EMAC internal utility function */
  450. static inline u32 emac_virt_to_phys(void __iomem *addr)
  451. {
  452. return (u32 __force) io_v2p(addr);
  453. }
  454. /* Cache macros - Packet buffers would be from skb pool which is cached */
  455. #define EMAC_VIRT_NOCACHE(addr) (addr)
  456. #define EMAC_CACHE_INVALIDATE(addr, size) \
  457. dma_cache_maint((void *)addr, size, DMA_FROM_DEVICE)
  458. #define EMAC_CACHE_WRITEBACK(addr, size) \
  459. dma_cache_maint((void *)addr, size, DMA_TO_DEVICE)
  460. #define EMAC_CACHE_WRITEBACK_INVALIDATE(addr, size) \
  461. dma_cache_maint((void *)addr, size, DMA_BIDIRECTIONAL)
  462. /* DM644x does not have BD's in cached memory - so no cache functions */
  463. #define BD_CACHE_INVALIDATE(addr, size)
  464. #define BD_CACHE_WRITEBACK(addr, size)
  465. #define BD_CACHE_WRITEBACK_INVALIDATE(addr, size)
  466. /* EMAC TX Host Error description strings */
  467. static char *emac_txhost_errcodes[16] = {
  468. "No error", "SOP error", "Ownership bit not set in SOP buffer",
  469. "Zero Next Buffer Descriptor Pointer Without EOP",
  470. "Zero Buffer Pointer", "Zero Buffer Length", "Packet Length Error",
  471. "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
  472. "Reserved", "Reserved", "Reserved", "Reserved"
  473. };
  474. /* EMAC RX Host Error description strings */
  475. static char *emac_rxhost_errcodes[16] = {
  476. "No error", "Reserved", "Ownership bit not set in input buffer",
  477. "Reserved", "Zero Buffer Pointer", "Reserved", "Reserved",
  478. "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
  479. "Reserved", "Reserved", "Reserved", "Reserved"
  480. };
  481. /* Helper macros */
  482. #define emac_read(reg) ioread32(priv->emac_base + (reg))
  483. #define emac_write(reg, val) iowrite32(val, priv->emac_base + (reg))
  484. #define emac_ctrl_read(reg) ioread32((priv->ctrl_base + (reg)))
  485. #define emac_ctrl_write(reg, val) iowrite32(val, (priv->ctrl_base + (reg)))
  486. #define emac_mdio_read(reg) ioread32(bus->priv + (reg))
  487. #define emac_mdio_write(reg, val) iowrite32(val, (bus->priv + (reg)))
  488. /**
  489. * emac_dump_regs: Dump important EMAC registers to debug terminal
  490. * @priv: The DaVinci EMAC private adapter structure
  491. *
  492. * Executes ethtool set cmd & sets phy mode
  493. *
  494. */
  495. static void emac_dump_regs(struct emac_priv *priv)
  496. {
  497. struct device *emac_dev = &priv->ndev->dev;
  498. /* Print important registers in EMAC */
  499. dev_info(emac_dev, "EMAC Basic registers\n");
  500. dev_info(emac_dev, "EMAC: EWCTL: %08X, EWINTTCNT: %08X\n",
  501. emac_ctrl_read(EMAC_CTRL_EWCTL),
  502. emac_ctrl_read(EMAC_CTRL_EWINTTCNT));
  503. dev_info(emac_dev, "EMAC: TXID: %08X %s, RXID: %08X %s\n",
  504. emac_read(EMAC_TXIDVER),
  505. ((emac_read(EMAC_TXCONTROL)) ? "enabled" : "disabled"),
  506. emac_read(EMAC_RXIDVER),
  507. ((emac_read(EMAC_RXCONTROL)) ? "enabled" : "disabled"));
  508. dev_info(emac_dev, "EMAC: TXIntRaw:%08X, TxIntMasked: %08X, "\
  509. "TxIntMasSet: %08X\n", emac_read(EMAC_TXINTSTATRAW),
  510. emac_read(EMAC_TXINTSTATMASKED), emac_read(EMAC_TXINTMASKSET));
  511. dev_info(emac_dev, "EMAC: RXIntRaw:%08X, RxIntMasked: %08X, "\
  512. "RxIntMasSet: %08X\n", emac_read(EMAC_RXINTSTATRAW),
  513. emac_read(EMAC_RXINTSTATMASKED), emac_read(EMAC_RXINTMASKSET));
  514. dev_info(emac_dev, "EMAC: MacIntRaw:%08X, MacIntMasked: %08X, "\
  515. "MacInVector=%08X\n", emac_read(EMAC_MACINTSTATRAW),
  516. emac_read(EMAC_MACINTSTATMASKED), emac_read(EMAC_MACINVECTOR));
  517. dev_info(emac_dev, "EMAC: EmuControl:%08X, FifoControl: %08X\n",
  518. emac_read(EMAC_EMCONTROL), emac_read(EMAC_FIFOCONTROL));
  519. dev_info(emac_dev, "EMAC: MBPEnable:%08X, RXUnicastSet: %08X, "\
  520. "RXMaxLen=%08X\n", emac_read(EMAC_RXMBPENABLE),
  521. emac_read(EMAC_RXUNICASTSET), emac_read(EMAC_RXMAXLEN));
  522. dev_info(emac_dev, "EMAC: MacControl:%08X, MacStatus: %08X, "\
  523. "MacConfig=%08X\n", emac_read(EMAC_MACCONTROL),
  524. emac_read(EMAC_MACSTATUS), emac_read(EMAC_MACCONFIG));
  525. dev_info(emac_dev, "EMAC: TXHDP[0]:%08X, RXHDP[0]: %08X\n",
  526. emac_read(EMAC_TXHDP(0)), emac_read(EMAC_RXHDP(0)));
  527. dev_info(emac_dev, "EMAC Statistics\n");
  528. dev_info(emac_dev, "EMAC: rx_good_frames:%d\n",
  529. emac_read(EMAC_RXGOODFRAMES));
  530. dev_info(emac_dev, "EMAC: rx_broadcast_frames:%d\n",
  531. emac_read(EMAC_RXBCASTFRAMES));
  532. dev_info(emac_dev, "EMAC: rx_multicast_frames:%d\n",
  533. emac_read(EMAC_RXMCASTFRAMES));
  534. dev_info(emac_dev, "EMAC: rx_pause_frames:%d\n",
  535. emac_read(EMAC_RXPAUSEFRAMES));
  536. dev_info(emac_dev, "EMAC: rx_crcerrors:%d\n",
  537. emac_read(EMAC_RXCRCERRORS));
  538. dev_info(emac_dev, "EMAC: rx_align_code_errors:%d\n",
  539. emac_read(EMAC_RXALIGNCODEERRORS));
  540. dev_info(emac_dev, "EMAC: rx_oversized_frames:%d\n",
  541. emac_read(EMAC_RXOVERSIZED));
  542. dev_info(emac_dev, "EMAC: rx_jabber_frames:%d\n",
  543. emac_read(EMAC_RXJABBER));
  544. dev_info(emac_dev, "EMAC: rx_undersized_frames:%d\n",
  545. emac_read(EMAC_RXUNDERSIZED));
  546. dev_info(emac_dev, "EMAC: rx_fragments:%d\n",
  547. emac_read(EMAC_RXFRAGMENTS));
  548. dev_info(emac_dev, "EMAC: rx_filtered_frames:%d\n",
  549. emac_read(EMAC_RXFILTERED));
  550. dev_info(emac_dev, "EMAC: rx_qos_filtered_frames:%d\n",
  551. emac_read(EMAC_RXQOSFILTERED));
  552. dev_info(emac_dev, "EMAC: rx_octets:%d\n",
  553. emac_read(EMAC_RXOCTETS));
  554. dev_info(emac_dev, "EMAC: tx_goodframes:%d\n",
  555. emac_read(EMAC_TXGOODFRAMES));
  556. dev_info(emac_dev, "EMAC: tx_bcastframes:%d\n",
  557. emac_read(EMAC_TXBCASTFRAMES));
  558. dev_info(emac_dev, "EMAC: tx_mcastframes:%d\n",
  559. emac_read(EMAC_TXMCASTFRAMES));
  560. dev_info(emac_dev, "EMAC: tx_pause_frames:%d\n",
  561. emac_read(EMAC_TXPAUSEFRAMES));
  562. dev_info(emac_dev, "EMAC: tx_deferred_frames:%d\n",
  563. emac_read(EMAC_TXDEFERRED));
  564. dev_info(emac_dev, "EMAC: tx_collision_frames:%d\n",
  565. emac_read(EMAC_TXCOLLISION));
  566. dev_info(emac_dev, "EMAC: tx_single_coll_frames:%d\n",
  567. emac_read(EMAC_TXSINGLECOLL));
  568. dev_info(emac_dev, "EMAC: tx_mult_coll_frames:%d\n",
  569. emac_read(EMAC_TXMULTICOLL));
  570. dev_info(emac_dev, "EMAC: tx_excessive_collisions:%d\n",
  571. emac_read(EMAC_TXEXCESSIVECOLL));
  572. dev_info(emac_dev, "EMAC: tx_late_collisions:%d\n",
  573. emac_read(EMAC_TXLATECOLL));
  574. dev_info(emac_dev, "EMAC: tx_underrun:%d\n",
  575. emac_read(EMAC_TXUNDERRUN));
  576. dev_info(emac_dev, "EMAC: tx_carrier_sense_errors:%d\n",
  577. emac_read(EMAC_TXCARRIERSENSE));
  578. dev_info(emac_dev, "EMAC: tx_octets:%d\n",
  579. emac_read(EMAC_TXOCTETS));
  580. dev_info(emac_dev, "EMAC: net_octets:%d\n",
  581. emac_read(EMAC_NETOCTETS));
  582. dev_info(emac_dev, "EMAC: rx_sof_overruns:%d\n",
  583. emac_read(EMAC_RXSOFOVERRUNS));
  584. dev_info(emac_dev, "EMAC: rx_mof_overruns:%d\n",
  585. emac_read(EMAC_RXMOFOVERRUNS));
  586. dev_info(emac_dev, "EMAC: rx_dma_overruns:%d\n",
  587. emac_read(EMAC_RXDMAOVERRUNS));
  588. }
  589. /*************************************************************************
  590. * EMAC MDIO/Phy Functionality
  591. *************************************************************************/
  592. /**
  593. * emac_get_drvinfo: Get EMAC driver information
  594. * @ndev: The DaVinci EMAC network adapter
  595. * @info: ethtool info structure containing name and version
  596. *
  597. * Returns EMAC driver information (name and version)
  598. *
  599. */
  600. static void emac_get_drvinfo(struct net_device *ndev,
  601. struct ethtool_drvinfo *info)
  602. {
  603. strcpy(info->driver, emac_version_string);
  604. strcpy(info->version, EMAC_MODULE_VERSION);
  605. }
  606. /**
  607. * emac_get_settings: Get EMAC settings
  608. * @ndev: The DaVinci EMAC network adapter
  609. * @ecmd: ethtool command
  610. *
  611. * Executes ethool get command
  612. *
  613. */
  614. static int emac_get_settings(struct net_device *ndev,
  615. struct ethtool_cmd *ecmd)
  616. {
  617. struct emac_priv *priv = netdev_priv(ndev);
  618. if (priv->phy_mask)
  619. return phy_ethtool_gset(priv->phydev, ecmd);
  620. else
  621. return -EOPNOTSUPP;
  622. }
  623. /**
  624. * emac_set_settings: Set EMAC settings
  625. * @ndev: The DaVinci EMAC network adapter
  626. * @ecmd: ethtool command
  627. *
  628. * Executes ethool set command
  629. *
  630. */
  631. static int emac_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
  632. {
  633. struct emac_priv *priv = netdev_priv(ndev);
  634. if (priv->phy_mask)
  635. return phy_ethtool_sset(priv->phydev, ecmd);
  636. else
  637. return -EOPNOTSUPP;
  638. }
  639. /**
  640. * ethtool_ops: DaVinci EMAC Ethtool structure
  641. *
  642. * Ethtool support for EMAC adapter
  643. *
  644. */
  645. static const struct ethtool_ops ethtool_ops = {
  646. .get_drvinfo = emac_get_drvinfo,
  647. .get_settings = emac_get_settings,
  648. .set_settings = emac_set_settings,
  649. .get_link = ethtool_op_get_link,
  650. };
  651. /**
  652. * emac_update_phystatus: Update Phy status
  653. * @priv: The DaVinci EMAC private adapter structure
  654. *
  655. * Updates phy status and takes action for network queue if required
  656. * based upon link status
  657. *
  658. */
  659. static void emac_update_phystatus(struct emac_priv *priv)
  660. {
  661. u32 mac_control;
  662. u32 new_duplex;
  663. u32 cur_duplex;
  664. struct net_device *ndev = priv->ndev;
  665. mac_control = emac_read(EMAC_MACCONTROL);
  666. cur_duplex = (mac_control & EMAC_MACCONTROL_FULLDUPLEXEN) ?
  667. DUPLEX_FULL : DUPLEX_HALF;
  668. if (priv->phy_mask)
  669. new_duplex = priv->phydev->duplex;
  670. else
  671. new_duplex = DUPLEX_FULL;
  672. /* We get called only if link has changed (speed/duplex/status) */
  673. if ((priv->link) && (new_duplex != cur_duplex)) {
  674. priv->duplex = new_duplex;
  675. if (DUPLEX_FULL == priv->duplex)
  676. mac_control |= (EMAC_MACCONTROL_FULLDUPLEXEN);
  677. else
  678. mac_control &= ~(EMAC_MACCONTROL_FULLDUPLEXEN);
  679. }
  680. if (priv->speed == SPEED_1000 && (priv->version == EMAC_VERSION_2)) {
  681. mac_control = emac_read(EMAC_MACCONTROL);
  682. mac_control |= (EMAC_DM646X_MACCONTORL_GIG |
  683. EMAC_DM646X_MACCONTORL_GIGFORCE);
  684. } else {
  685. /* Clear the GIG bit and GIGFORCE bit */
  686. mac_control &= ~(EMAC_DM646X_MACCONTORL_GIGFORCE |
  687. EMAC_DM646X_MACCONTORL_GIG);
  688. if (priv->rmii_en && (priv->speed == SPEED_100))
  689. mac_control |= EMAC_MACCONTROL_RMIISPEED_MASK;
  690. else
  691. mac_control &= ~EMAC_MACCONTROL_RMIISPEED_MASK;
  692. }
  693. /* Update mac_control if changed */
  694. emac_write(EMAC_MACCONTROL, mac_control);
  695. if (priv->link) {
  696. /* link ON */
  697. if (!netif_carrier_ok(ndev))
  698. netif_carrier_on(ndev);
  699. /* reactivate the transmit queue if it is stopped */
  700. if (netif_running(ndev) && netif_queue_stopped(ndev))
  701. netif_wake_queue(ndev);
  702. } else {
  703. /* link OFF */
  704. if (netif_carrier_ok(ndev))
  705. netif_carrier_off(ndev);
  706. if (!netif_queue_stopped(ndev))
  707. netif_stop_queue(ndev);
  708. }
  709. }
  710. /**
  711. * hash_get: Calculate hash value from mac address
  712. * @addr: mac address to delete from hash table
  713. *
  714. * Calculates hash value from mac address
  715. *
  716. */
  717. static u32 hash_get(u8 *addr)
  718. {
  719. u32 hash;
  720. u8 tmpval;
  721. int cnt;
  722. hash = 0;
  723. for (cnt = 0; cnt < 2; cnt++) {
  724. tmpval = *addr++;
  725. hash ^= (tmpval >> 2) ^ (tmpval << 4);
  726. tmpval = *addr++;
  727. hash ^= (tmpval >> 4) ^ (tmpval << 2);
  728. tmpval = *addr++;
  729. hash ^= (tmpval >> 6) ^ (tmpval);
  730. }
  731. return hash & 0x3F;
  732. }
  733. /**
  734. * hash_add: Hash function to add mac addr from hash table
  735. * @priv: The DaVinci EMAC private adapter structure
  736. * mac_addr: mac address to delete from hash table
  737. *
  738. * Adds mac address to the internal hash table
  739. *
  740. */
  741. static int hash_add(struct emac_priv *priv, u8 *mac_addr)
  742. {
  743. struct device *emac_dev = &priv->ndev->dev;
  744. u32 rc = 0;
  745. u32 hash_bit;
  746. u32 hash_value = hash_get(mac_addr);
  747. if (hash_value >= EMAC_NUM_MULTICAST_BITS) {
  748. if (netif_msg_drv(priv)) {
  749. dev_err(emac_dev, "DaVinci EMAC: hash_add(): Invalid "\
  750. "Hash %08x, should not be greater than %08x",
  751. hash_value, (EMAC_NUM_MULTICAST_BITS - 1));
  752. }
  753. return -1;
  754. }
  755. /* set the hash bit only if not previously set */
  756. if (priv->multicast_hash_cnt[hash_value] == 0) {
  757. rc = 1; /* hash value changed */
  758. if (hash_value < 32) {
  759. hash_bit = BIT(hash_value);
  760. priv->mac_hash1 |= hash_bit;
  761. } else {
  762. hash_bit = BIT((hash_value - 32));
  763. priv->mac_hash2 |= hash_bit;
  764. }
  765. }
  766. /* incr counter for num of mcast addr's mapped to "this" hash bit */
  767. ++priv->multicast_hash_cnt[hash_value];
  768. return rc;
  769. }
  770. /**
  771. * hash_del: Hash function to delete mac addr from hash table
  772. * @priv: The DaVinci EMAC private adapter structure
  773. * mac_addr: mac address to delete from hash table
  774. *
  775. * Removes mac address from the internal hash table
  776. *
  777. */
  778. static int hash_del(struct emac_priv *priv, u8 *mac_addr)
  779. {
  780. u32 hash_value;
  781. u32 hash_bit;
  782. hash_value = hash_get(mac_addr);
  783. if (priv->multicast_hash_cnt[hash_value] > 0) {
  784. /* dec cntr for num of mcast addr's mapped to this hash bit */
  785. --priv->multicast_hash_cnt[hash_value];
  786. }
  787. /* if counter still > 0, at least one multicast address refers
  788. * to this hash bit. so return 0 */
  789. if (priv->multicast_hash_cnt[hash_value] > 0)
  790. return 0;
  791. if (hash_value < 32) {
  792. hash_bit = BIT(hash_value);
  793. priv->mac_hash1 &= ~hash_bit;
  794. } else {
  795. hash_bit = BIT((hash_value - 32));
  796. priv->mac_hash2 &= ~hash_bit;
  797. }
  798. /* return 1 to indicate change in mac_hash registers reqd */
  799. return 1;
  800. }
  801. /* EMAC multicast operation */
  802. #define EMAC_MULTICAST_ADD 0
  803. #define EMAC_MULTICAST_DEL 1
  804. #define EMAC_ALL_MULTI_SET 2
  805. #define EMAC_ALL_MULTI_CLR 3
  806. /**
  807. * emac_add_mcast: Set multicast address in the EMAC adapter (Internal)
  808. * @priv: The DaVinci EMAC private adapter structure
  809. * @action: multicast operation to perform
  810. * mac_addr: mac address to set
  811. *
  812. * Set multicast addresses in EMAC adapter - internal function
  813. *
  814. */
  815. static void emac_add_mcast(struct emac_priv *priv, u32 action, u8 *mac_addr)
  816. {
  817. struct device *emac_dev = &priv->ndev->dev;
  818. int update = -1;
  819. switch (action) {
  820. case EMAC_MULTICAST_ADD:
  821. update = hash_add(priv, mac_addr);
  822. break;
  823. case EMAC_MULTICAST_DEL:
  824. update = hash_del(priv, mac_addr);
  825. break;
  826. case EMAC_ALL_MULTI_SET:
  827. update = 1;
  828. priv->mac_hash1 = EMAC_ALL_MULTI_REG_VALUE;
  829. priv->mac_hash2 = EMAC_ALL_MULTI_REG_VALUE;
  830. break;
  831. case EMAC_ALL_MULTI_CLR:
  832. update = 1;
  833. priv->mac_hash1 = 0;
  834. priv->mac_hash2 = 0;
  835. memset(&(priv->multicast_hash_cnt[0]), 0,
  836. sizeof(priv->multicast_hash_cnt[0]) *
  837. EMAC_NUM_MULTICAST_BITS);
  838. break;
  839. default:
  840. if (netif_msg_drv(priv))
  841. dev_err(emac_dev, "DaVinci EMAC: add_mcast"\
  842. ": bad operation %d", action);
  843. break;
  844. }
  845. /* write to the hardware only if the register status chances */
  846. if (update > 0) {
  847. emac_write(EMAC_MACHASH1, priv->mac_hash1);
  848. emac_write(EMAC_MACHASH2, priv->mac_hash2);
  849. }
  850. }
  851. /**
  852. * emac_dev_mcast_set: Set multicast address in the EMAC adapter
  853. * @ndev: The DaVinci EMAC network adapter
  854. *
  855. * Set multicast addresses in EMAC adapter
  856. *
  857. */
  858. static void emac_dev_mcast_set(struct net_device *ndev)
  859. {
  860. u32 mbp_enable;
  861. struct emac_priv *priv = netdev_priv(ndev);
  862. mbp_enable = emac_read(EMAC_RXMBPENABLE);
  863. if (ndev->flags & IFF_PROMISC) {
  864. mbp_enable &= (~EMAC_MBP_PROMISCCH(EMAC_DEF_PROM_CH));
  865. mbp_enable |= (EMAC_MBP_RXPROMISC);
  866. } else {
  867. mbp_enable = (mbp_enable & ~EMAC_MBP_RXPROMISC);
  868. if ((ndev->flags & IFF_ALLMULTI) ||
  869. (ndev->mc_count > EMAC_DEF_MAX_MULTICAST_ADDRESSES)) {
  870. mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
  871. emac_add_mcast(priv, EMAC_ALL_MULTI_SET, NULL);
  872. }
  873. if (ndev->mc_count > 0) {
  874. struct dev_mc_list *mc_ptr;
  875. mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
  876. emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
  877. /* program multicast address list into EMAC hardware */
  878. for (mc_ptr = ndev->mc_list; mc_ptr;
  879. mc_ptr = mc_ptr->next) {
  880. emac_add_mcast(priv, EMAC_MULTICAST_ADD,
  881. (u8 *)mc_ptr->dmi_addr);
  882. }
  883. } else {
  884. mbp_enable = (mbp_enable & ~EMAC_MBP_RXMCAST);
  885. emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
  886. }
  887. }
  888. /* Set mbp config register */
  889. emac_write(EMAC_RXMBPENABLE, mbp_enable);
  890. }
  891. /*************************************************************************
  892. * EMAC Hardware manipulation
  893. *************************************************************************/
  894. /**
  895. * emac_int_disable: Disable EMAC module interrupt (from adapter)
  896. * @priv: The DaVinci EMAC private adapter structure
  897. *
  898. * Disable EMAC interrupt on the adapter
  899. *
  900. */
  901. static void emac_int_disable(struct emac_priv *priv)
  902. {
  903. if (priv->version == EMAC_VERSION_2) {
  904. unsigned long flags;
  905. local_irq_save(flags);
  906. /* Program C0_Int_En to zero to turn off
  907. * interrupts to the CPU */
  908. emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0x0);
  909. emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0x0);
  910. /* NOTE: Rx Threshold and Misc interrupts are not disabled */
  911. local_irq_restore(flags);
  912. } else {
  913. /* Set DM644x control registers for interrupt control */
  914. emac_ctrl_write(EMAC_CTRL_EWCTL, 0x0);
  915. }
  916. }
  917. /**
  918. * emac_int_enable: Enable EMAC module interrupt (from adapter)
  919. * @priv: The DaVinci EMAC private adapter structure
  920. *
  921. * Enable EMAC interrupt on the adapter
  922. *
  923. */
  924. static void emac_int_enable(struct emac_priv *priv)
  925. {
  926. if (priv->version == EMAC_VERSION_2) {
  927. emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0xff);
  928. emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0xff);
  929. /* In addition to turning on interrupt Enable, we need
  930. * ack by writing appropriate values to the EOI
  931. * register */
  932. /* NOTE: Rx Threshold and Misc interrupts are not enabled */
  933. /* ack rxen only then a new pulse will be generated */
  934. emac_write(EMAC_DM646X_MACEOIVECTOR,
  935. EMAC_DM646X_MAC_EOI_C0_RXEN);
  936. /* ack txen- only then a new pulse will be generated */
  937. emac_write(EMAC_DM646X_MACEOIVECTOR,
  938. EMAC_DM646X_MAC_EOI_C0_TXEN);
  939. } else {
  940. /* Set DM644x control registers for interrupt control */
  941. emac_ctrl_write(EMAC_CTRL_EWCTL, 0x1);
  942. }
  943. }
  944. /**
  945. * emac_irq: EMAC interrupt handler
  946. * @irq: interrupt number
  947. * @dev_id: EMAC network adapter data structure ptr
  948. *
  949. * EMAC Interrupt handler - we only schedule NAPI and not process any packets
  950. * here. EVen the interrupt status is checked (TX/RX/Err) in NAPI poll function
  951. *
  952. * Returns interrupt handled condition
  953. */
  954. static irqreturn_t emac_irq(int irq, void *dev_id)
  955. {
  956. struct net_device *ndev = (struct net_device *)dev_id;
  957. struct emac_priv *priv = netdev_priv(ndev);
  958. ++priv->isr_count;
  959. if (likely(netif_running(priv->ndev))) {
  960. emac_int_disable(priv);
  961. napi_schedule(&priv->napi);
  962. } else {
  963. /* we are closing down, so dont process anything */
  964. }
  965. return IRQ_HANDLED;
  966. }
  967. /** EMAC on-chip buffer descriptor memory
  968. *
  969. * WARNING: Please note that the on chip memory is used for both TX and RX
  970. * buffer descriptor queues and is equally divided between TX and RX desc's
  971. * If the number of TX or RX descriptors change this memory pointers need
  972. * to be adjusted. If external memory is allocated then these pointers can
  973. * pointer to the memory
  974. *
  975. */
  976. #define EMAC_TX_BD_MEM(priv) ((priv)->emac_ctrl_ram)
  977. #define EMAC_RX_BD_MEM(priv) ((priv)->emac_ctrl_ram + \
  978. (((priv)->ctrl_ram_size) >> 1))
  979. /**
  980. * emac_init_txch: TX channel initialization
  981. * @priv: The DaVinci EMAC private adapter structure
  982. * @ch: RX channel number
  983. *
  984. * Called during device init to setup a TX channel (allocate buffer desc
  985. * create free pool and keep ready for transmission
  986. *
  987. * Returns success(0) or mem alloc failures error code
  988. */
  989. static int emac_init_txch(struct emac_priv *priv, u32 ch)
  990. {
  991. struct device *emac_dev = &priv->ndev->dev;
  992. u32 cnt, bd_size;
  993. void __iomem *mem;
  994. struct emac_tx_bd __iomem *curr_bd;
  995. struct emac_txch *txch = NULL;
  996. txch = kzalloc(sizeof(struct emac_txch), GFP_KERNEL);
  997. if (NULL == txch) {
  998. dev_err(emac_dev, "DaVinci EMAC: TX Ch mem alloc failed");
  999. return -ENOMEM;
  1000. }
  1001. priv->txch[ch] = txch;
  1002. txch->service_max = EMAC_DEF_TX_MAX_SERVICE;
  1003. txch->active_queue_head = NULL;
  1004. txch->active_queue_tail = NULL;
  1005. txch->queue_active = 0;
  1006. txch->teardown_pending = 0;
  1007. /* allocate memory for TX CPPI channel on a 4 byte boundry */
  1008. txch->tx_complete = kzalloc(txch->service_max * sizeof(u32),
  1009. GFP_KERNEL);
  1010. if (NULL == txch->tx_complete) {
  1011. dev_err(emac_dev, "DaVinci EMAC: Tx service mem alloc failed");
  1012. kfree(txch);
  1013. return -ENOMEM;
  1014. }
  1015. /* allocate buffer descriptor pool align every BD on four word
  1016. * boundry for future requirements */
  1017. bd_size = (sizeof(struct emac_tx_bd) + 0xF) & ~0xF;
  1018. txch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
  1019. txch->alloc_size = (((bd_size * txch->num_bd) + 0xF) & ~0xF);
  1020. /* alloc TX BD memory */
  1021. txch->bd_mem = EMAC_TX_BD_MEM(priv);
  1022. __memzero((void __force *)txch->bd_mem, txch->alloc_size);
  1023. /* initialize the BD linked list */
  1024. mem = (void __force __iomem *)
  1025. (((u32 __force) txch->bd_mem + 0xF) & ~0xF);
  1026. txch->bd_pool_head = NULL;
  1027. for (cnt = 0; cnt < txch->num_bd; cnt++) {
  1028. curr_bd = mem + (cnt * bd_size);
  1029. curr_bd->next = txch->bd_pool_head;
  1030. txch->bd_pool_head = curr_bd;
  1031. }
  1032. /* reset statistics counters */
  1033. txch->out_of_tx_bd = 0;
  1034. txch->no_active_pkts = 0;
  1035. txch->active_queue_count = 0;
  1036. return 0;
  1037. }
  1038. /**
  1039. * emac_cleanup_txch: Book-keep function to clean TX channel resources
  1040. * @priv: The DaVinci EMAC private adapter structure
  1041. * @ch: TX channel number
  1042. *
  1043. * Called to clean up TX channel resources
  1044. *
  1045. */
  1046. static void emac_cleanup_txch(struct emac_priv *priv, u32 ch)
  1047. {
  1048. struct emac_txch *txch = priv->txch[ch];
  1049. if (txch) {
  1050. if (txch->bd_mem)
  1051. txch->bd_mem = NULL;
  1052. kfree(txch->tx_complete);
  1053. kfree(txch);
  1054. priv->txch[ch] = NULL;
  1055. }
  1056. }
  1057. /**
  1058. * emac_net_tx_complete: TX packet completion function
  1059. * @priv: The DaVinci EMAC private adapter structure
  1060. * @net_data_tokens: packet token - skb pointer
  1061. * @num_tokens: number of skb's to free
  1062. * @ch: TX channel number
  1063. *
  1064. * Frees the skb once packet is transmitted
  1065. *
  1066. */
  1067. static int emac_net_tx_complete(struct emac_priv *priv,
  1068. void **net_data_tokens,
  1069. int num_tokens, u32 ch)
  1070. {
  1071. u32 cnt;
  1072. if (unlikely(num_tokens && netif_queue_stopped(priv->ndev)))
  1073. netif_start_queue(priv->ndev);
  1074. for (cnt = 0; cnt < num_tokens; cnt++) {
  1075. struct sk_buff *skb = (struct sk_buff *)net_data_tokens[cnt];
  1076. if (skb == NULL)
  1077. continue;
  1078. priv->net_dev_stats.tx_packets++;
  1079. priv->net_dev_stats.tx_bytes += skb->len;
  1080. dev_kfree_skb_any(skb);
  1081. }
  1082. return 0;
  1083. }
  1084. /**
  1085. * emac_txch_teardown: TX channel teardown
  1086. * @priv: The DaVinci EMAC private adapter structure
  1087. * @ch: TX channel number
  1088. *
  1089. * Called to teardown TX channel
  1090. *
  1091. */
  1092. static void emac_txch_teardown(struct emac_priv *priv, u32 ch)
  1093. {
  1094. struct device *emac_dev = &priv->ndev->dev;
  1095. u32 teardown_cnt = 0xFFFFFFF0; /* Some high value */
  1096. struct emac_txch *txch = priv->txch[ch];
  1097. struct emac_tx_bd __iomem *curr_bd;
  1098. while ((emac_read(EMAC_TXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
  1099. EMAC_TEARDOWN_VALUE) {
  1100. /* wait till tx teardown complete */
  1101. cpu_relax(); /* TODO: check if this helps ... */
  1102. --teardown_cnt;
  1103. if (0 == teardown_cnt) {
  1104. dev_err(emac_dev, "EMAC: TX teardown aborted\n");
  1105. break;
  1106. }
  1107. }
  1108. emac_write(EMAC_TXCP(ch), EMAC_TEARDOWN_VALUE);
  1109. /* process sent packets and return skb's to upper layer */
  1110. if (1 == txch->queue_active) {
  1111. curr_bd = txch->active_queue_head;
  1112. while (curr_bd != NULL) {
  1113. emac_net_tx_complete(priv, (void __force *)
  1114. &curr_bd->buf_token, 1, ch);
  1115. if (curr_bd != txch->active_queue_tail)
  1116. curr_bd = curr_bd->next;
  1117. else
  1118. break;
  1119. }
  1120. txch->bd_pool_head = txch->active_queue_head;
  1121. txch->active_queue_head =
  1122. txch->active_queue_tail = NULL;
  1123. }
  1124. }
  1125. /**
  1126. * emac_stop_txch: Stop TX channel operation
  1127. * @priv: The DaVinci EMAC private adapter structure
  1128. * @ch: TX channel number
  1129. *
  1130. * Called to stop TX channel operation
  1131. *
  1132. */
  1133. static void emac_stop_txch(struct emac_priv *priv, u32 ch)
  1134. {
  1135. struct emac_txch *txch = priv->txch[ch];
  1136. if (txch) {
  1137. txch->teardown_pending = 1;
  1138. emac_write(EMAC_TXTEARDOWN, 0);
  1139. emac_txch_teardown(priv, ch);
  1140. txch->teardown_pending = 0;
  1141. emac_write(EMAC_TXINTMASKCLEAR, BIT(ch));
  1142. }
  1143. }
  1144. /**
  1145. * emac_tx_bdproc: TX buffer descriptor (packet) processing
  1146. * @priv: The DaVinci EMAC private adapter structure
  1147. * @ch: TX channel number to process buffer descriptors for
  1148. * @budget: number of packets allowed to process
  1149. * @pending: indication to caller that packets are pending to process
  1150. *
  1151. * Processes TX buffer descriptors after packets are transmitted - checks
  1152. * ownership bit on the TX * descriptor and requeues it to free pool & frees
  1153. * the SKB buffer. Only "budget" number of packets are processed and
  1154. * indication of pending packets provided to the caller
  1155. *
  1156. * Returns number of packets processed
  1157. */
  1158. static int emac_tx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
  1159. {
  1160. struct device *emac_dev = &priv->ndev->dev;
  1161. unsigned long flags;
  1162. u32 frame_status;
  1163. u32 pkts_processed = 0;
  1164. u32 tx_complete_cnt = 0;
  1165. struct emac_tx_bd __iomem *curr_bd;
  1166. struct emac_txch *txch = priv->txch[ch];
  1167. u32 *tx_complete_ptr = txch->tx_complete;
  1168. if (unlikely(1 == txch->teardown_pending)) {
  1169. if (netif_msg_tx_err(priv) && net_ratelimit()) {
  1170. dev_err(emac_dev, "DaVinci EMAC:emac_tx_bdproc: "\
  1171. "teardown pending\n");
  1172. }
  1173. return 0; /* dont handle any pkt completions */
  1174. }
  1175. ++txch->proc_count;
  1176. spin_lock_irqsave(&priv->tx_lock, flags);
  1177. curr_bd = txch->active_queue_head;
  1178. if (NULL == curr_bd) {
  1179. emac_write(EMAC_TXCP(ch),
  1180. emac_virt_to_phys(txch->last_hw_bdprocessed));
  1181. txch->no_active_pkts++;
  1182. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1183. return 0;
  1184. }
  1185. BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  1186. frame_status = curr_bd->mode;
  1187. while ((curr_bd) &&
  1188. ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
  1189. (pkts_processed < budget)) {
  1190. emac_write(EMAC_TXCP(ch), emac_virt_to_phys(curr_bd));
  1191. txch->active_queue_head = curr_bd->next;
  1192. if (frame_status & EMAC_CPPI_EOQ_BIT) {
  1193. if (curr_bd->next) { /* misqueued packet */
  1194. emac_write(EMAC_TXHDP(ch), curr_bd->h_next);
  1195. ++txch->mis_queued_packets;
  1196. } else {
  1197. txch->queue_active = 0; /* end of queue */
  1198. }
  1199. }
  1200. *tx_complete_ptr = (u32) curr_bd->buf_token;
  1201. ++tx_complete_ptr;
  1202. ++tx_complete_cnt;
  1203. curr_bd->next = txch->bd_pool_head;
  1204. txch->bd_pool_head = curr_bd;
  1205. --txch->active_queue_count;
  1206. pkts_processed++;
  1207. txch->last_hw_bdprocessed = curr_bd;
  1208. curr_bd = txch->active_queue_head;
  1209. if (curr_bd) {
  1210. BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  1211. frame_status = curr_bd->mode;
  1212. }
  1213. } /* end of pkt processing loop */
  1214. emac_net_tx_complete(priv,
  1215. (void *)&txch->tx_complete[0],
  1216. tx_complete_cnt, ch);
  1217. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1218. return pkts_processed;
  1219. }
  1220. #define EMAC_ERR_TX_OUT_OF_BD -1
  1221. /**
  1222. * emac_send: EMAC Transmit function (internal)
  1223. * @priv: The DaVinci EMAC private adapter structure
  1224. * @pkt: packet pointer (contains skb ptr)
  1225. * @ch: TX channel number
  1226. *
  1227. * Called by the transmit function to queue the packet in EMAC hardware queue
  1228. *
  1229. * Returns success(0) or error code (typically out of desc's)
  1230. */
  1231. static int emac_send(struct emac_priv *priv, struct emac_netpktobj *pkt, u32 ch)
  1232. {
  1233. unsigned long flags;
  1234. struct emac_tx_bd __iomem *curr_bd;
  1235. struct emac_txch *txch;
  1236. struct emac_netbufobj *buf_list;
  1237. txch = priv->txch[ch];
  1238. buf_list = pkt->buf_list; /* get handle to the buffer array */
  1239. /* check packet size and pad if short */
  1240. if (pkt->pkt_length < EMAC_DEF_MIN_ETHPKTSIZE) {
  1241. buf_list->length += (EMAC_DEF_MIN_ETHPKTSIZE - pkt->pkt_length);
  1242. pkt->pkt_length = EMAC_DEF_MIN_ETHPKTSIZE;
  1243. }
  1244. spin_lock_irqsave(&priv->tx_lock, flags);
  1245. curr_bd = txch->bd_pool_head;
  1246. if (curr_bd == NULL) {
  1247. txch->out_of_tx_bd++;
  1248. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1249. return EMAC_ERR_TX_OUT_OF_BD;
  1250. }
  1251. txch->bd_pool_head = curr_bd->next;
  1252. curr_bd->buf_token = buf_list->buf_token;
  1253. /* FIXME buff_ptr = dma_map_single(... data_ptr ...) */
  1254. curr_bd->buff_ptr = virt_to_phys(buf_list->data_ptr);
  1255. curr_bd->off_b_len = buf_list->length;
  1256. curr_bd->h_next = 0;
  1257. curr_bd->next = NULL;
  1258. curr_bd->mode = (EMAC_CPPI_SOP_BIT | EMAC_CPPI_OWNERSHIP_BIT |
  1259. EMAC_CPPI_EOP_BIT | pkt->pkt_length);
  1260. /* flush the packet from cache if write back cache is present */
  1261. BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  1262. /* send the packet */
  1263. if (txch->active_queue_head == NULL) {
  1264. txch->active_queue_head = curr_bd;
  1265. txch->active_queue_tail = curr_bd;
  1266. if (1 != txch->queue_active) {
  1267. emac_write(EMAC_TXHDP(ch),
  1268. emac_virt_to_phys(curr_bd));
  1269. txch->queue_active = 1;
  1270. }
  1271. ++txch->queue_reinit;
  1272. } else {
  1273. register struct emac_tx_bd __iomem *tail_bd;
  1274. register u32 frame_status;
  1275. tail_bd = txch->active_queue_tail;
  1276. tail_bd->next = curr_bd;
  1277. txch->active_queue_tail = curr_bd;
  1278. tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
  1279. tail_bd->h_next = (int)emac_virt_to_phys(curr_bd);
  1280. frame_status = tail_bd->mode;
  1281. if (frame_status & EMAC_CPPI_EOQ_BIT) {
  1282. emac_write(EMAC_TXHDP(ch), emac_virt_to_phys(curr_bd));
  1283. frame_status &= ~(EMAC_CPPI_EOQ_BIT);
  1284. tail_bd->mode = frame_status;
  1285. ++txch->end_of_queue_add;
  1286. }
  1287. }
  1288. txch->active_queue_count++;
  1289. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1290. return 0;
  1291. }
  1292. /**
  1293. * emac_dev_xmit: EMAC Transmit function
  1294. * @skb: SKB pointer
  1295. * @ndev: The DaVinci EMAC network adapter
  1296. *
  1297. * Called by the system to transmit a packet - we queue the packet in
  1298. * EMAC hardware transmit queue
  1299. *
  1300. * Returns success(NETDEV_TX_OK) or error code (typically out of desc's)
  1301. */
  1302. static int emac_dev_xmit(struct sk_buff *skb, struct net_device *ndev)
  1303. {
  1304. struct device *emac_dev = &ndev->dev;
  1305. int ret_code;
  1306. struct emac_netbufobj tx_buf; /* buffer obj-only single frame support */
  1307. struct emac_netpktobj tx_packet; /* packet object */
  1308. struct emac_priv *priv = netdev_priv(ndev);
  1309. /* If no link, return */
  1310. if (unlikely(!priv->link)) {
  1311. if (netif_msg_tx_err(priv) && net_ratelimit())
  1312. dev_err(emac_dev, "DaVinci EMAC: No link to transmit");
  1313. return NETDEV_TX_BUSY;
  1314. }
  1315. /* Build the buffer and packet objects - Since only single fragment is
  1316. * supported, need not set length and token in both packet & object.
  1317. * Doing so for completeness sake & to show that this needs to be done
  1318. * in multifragment case
  1319. */
  1320. tx_packet.buf_list = &tx_buf;
  1321. tx_packet.num_bufs = 1; /* only single fragment supported */
  1322. tx_packet.pkt_length = skb->len;
  1323. tx_packet.pkt_token = (void *)skb;
  1324. tx_buf.length = skb->len;
  1325. tx_buf.buf_token = (void *)skb;
  1326. tx_buf.data_ptr = skb->data;
  1327. EMAC_CACHE_WRITEBACK((unsigned long)skb->data, skb->len);
  1328. ndev->trans_start = jiffies;
  1329. ret_code = emac_send(priv, &tx_packet, EMAC_DEF_TX_CH);
  1330. if (unlikely(ret_code != 0)) {
  1331. if (ret_code == EMAC_ERR_TX_OUT_OF_BD) {
  1332. if (netif_msg_tx_err(priv) && net_ratelimit())
  1333. dev_err(emac_dev, "DaVinci EMAC: xmit() fatal"\
  1334. " err. Out of TX BD's");
  1335. netif_stop_queue(priv->ndev);
  1336. }
  1337. priv->net_dev_stats.tx_dropped++;
  1338. return NETDEV_TX_BUSY;
  1339. }
  1340. return NETDEV_TX_OK;
  1341. }
  1342. /**
  1343. * emac_dev_tx_timeout: EMAC Transmit timeout function
  1344. * @ndev: The DaVinci EMAC network adapter
  1345. *
  1346. * Called when system detects that a skb timeout period has expired
  1347. * potentially due to a fault in the adapter in not being able to send
  1348. * it out on the wire. We teardown the TX channel assuming a hardware
  1349. * error and re-initialize the TX channel for hardware operation
  1350. *
  1351. */
  1352. static void emac_dev_tx_timeout(struct net_device *ndev)
  1353. {
  1354. struct emac_priv *priv = netdev_priv(ndev);
  1355. struct device *emac_dev = &ndev->dev;
  1356. if (netif_msg_tx_err(priv))
  1357. dev_err(emac_dev, "DaVinci EMAC: xmit timeout, restarting TX");
  1358. priv->net_dev_stats.tx_errors++;
  1359. emac_int_disable(priv);
  1360. emac_stop_txch(priv, EMAC_DEF_TX_CH);
  1361. emac_cleanup_txch(priv, EMAC_DEF_TX_CH);
  1362. emac_init_txch(priv, EMAC_DEF_TX_CH);
  1363. emac_write(EMAC_TXHDP(0), 0);
  1364. emac_write(EMAC_TXINTMASKSET, BIT(EMAC_DEF_TX_CH));
  1365. emac_int_enable(priv);
  1366. }
  1367. /**
  1368. * emac_net_alloc_rx_buf: Allocate a skb for RX
  1369. * @priv: The DaVinci EMAC private adapter structure
  1370. * @buf_size: size of SKB data buffer to allocate
  1371. * @data_token: data token returned (skb handle for storing in buffer desc)
  1372. * @ch: RX channel number
  1373. *
  1374. * Called during RX channel setup - allocates skb buffer of required size
  1375. * and provides the skb handle and allocated buffer data pointer to caller
  1376. *
  1377. * Returns skb data pointer or 0 on failure to alloc skb
  1378. */
  1379. static void *emac_net_alloc_rx_buf(struct emac_priv *priv, int buf_size,
  1380. void **data_token, u32 ch)
  1381. {
  1382. struct net_device *ndev = priv->ndev;
  1383. struct device *emac_dev = &ndev->dev;
  1384. struct sk_buff *p_skb;
  1385. p_skb = dev_alloc_skb(buf_size);
  1386. if (unlikely(NULL == p_skb)) {
  1387. if (netif_msg_rx_err(priv) && net_ratelimit())
  1388. dev_err(emac_dev, "DaVinci EMAC: failed to alloc skb");
  1389. return NULL;
  1390. }
  1391. /* set device pointer in skb and reserve space for extra bytes */
  1392. p_skb->dev = ndev;
  1393. skb_reserve(p_skb, NET_IP_ALIGN);
  1394. *data_token = (void *) p_skb;
  1395. EMAC_CACHE_WRITEBACK_INVALIDATE((unsigned long)p_skb->data, buf_size);
  1396. return p_skb->data;
  1397. }
  1398. /**
  1399. * emac_init_rxch: RX channel initialization
  1400. * @priv: The DaVinci EMAC private adapter structure
  1401. * @ch: RX channel number
  1402. * @param: mac address for RX channel
  1403. *
  1404. * Called during device init to setup a RX channel (allocate buffers and
  1405. * buffer descriptors, create queue and keep ready for reception
  1406. *
  1407. * Returns success(0) or mem alloc failures error code
  1408. */
  1409. static int emac_init_rxch(struct emac_priv *priv, u32 ch, char *param)
  1410. {
  1411. struct device *emac_dev = &priv->ndev->dev;
  1412. u32 cnt, bd_size;
  1413. void __iomem *mem;
  1414. struct emac_rx_bd __iomem *curr_bd;
  1415. struct emac_rxch *rxch = NULL;
  1416. rxch = kzalloc(sizeof(struct emac_rxch), GFP_KERNEL);
  1417. if (NULL == rxch) {
  1418. dev_err(emac_dev, "DaVinci EMAC: RX Ch mem alloc failed");
  1419. return -ENOMEM;
  1420. }
  1421. priv->rxch[ch] = rxch;
  1422. rxch->buf_size = priv->rx_buf_size;
  1423. rxch->service_max = EMAC_DEF_RX_MAX_SERVICE;
  1424. rxch->queue_active = 0;
  1425. rxch->teardown_pending = 0;
  1426. /* save mac address */
  1427. for (cnt = 0; cnt < 6; cnt++)
  1428. rxch->mac_addr[cnt] = param[cnt];
  1429. /* allocate buffer descriptor pool align every BD on four word
  1430. * boundry for future requirements */
  1431. bd_size = (sizeof(struct emac_rx_bd) + 0xF) & ~0xF;
  1432. rxch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
  1433. rxch->alloc_size = (((bd_size * rxch->num_bd) + 0xF) & ~0xF);
  1434. rxch->bd_mem = EMAC_RX_BD_MEM(priv);
  1435. __memzero((void __force *)rxch->bd_mem, rxch->alloc_size);
  1436. rxch->pkt_queue.buf_list = &rxch->buf_queue;
  1437. /* allocate RX buffer and initialize the BD linked list */
  1438. mem = (void __force __iomem *)
  1439. (((u32 __force) rxch->bd_mem + 0xF) & ~0xF);
  1440. rxch->active_queue_head = NULL;
  1441. rxch->active_queue_tail = mem;
  1442. for (cnt = 0; cnt < rxch->num_bd; cnt++) {
  1443. curr_bd = mem + (cnt * bd_size);
  1444. /* for future use the last parameter contains the BD ptr */
  1445. curr_bd->data_ptr = emac_net_alloc_rx_buf(priv,
  1446. rxch->buf_size,
  1447. (void __force **)&curr_bd->buf_token,
  1448. EMAC_DEF_RX_CH);
  1449. if (curr_bd->data_ptr == NULL) {
  1450. dev_err(emac_dev, "DaVinci EMAC: RX buf mem alloc " \
  1451. "failed for ch %d\n", ch);
  1452. kfree(rxch);
  1453. return -ENOMEM;
  1454. }
  1455. /* populate the hardware descriptor */
  1456. curr_bd->h_next = emac_virt_to_phys(rxch->active_queue_head);
  1457. /* FIXME buff_ptr = dma_map_single(... data_ptr ...) */
  1458. curr_bd->buff_ptr = virt_to_phys(curr_bd->data_ptr);
  1459. curr_bd->off_b_len = rxch->buf_size;
  1460. curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
  1461. /* write back to hardware memory */
  1462. BD_CACHE_WRITEBACK_INVALIDATE((u32) curr_bd,
  1463. EMAC_BD_LENGTH_FOR_CACHE);
  1464. curr_bd->next = rxch->active_queue_head;
  1465. rxch->active_queue_head = curr_bd;
  1466. }
  1467. /* At this point rxCppi->activeQueueHead points to the first
  1468. RX BD ready to be given to RX HDP and rxch->active_queue_tail
  1469. points to the last RX BD
  1470. */
  1471. return 0;
  1472. }
  1473. /**
  1474. * emac_rxch_teardown: RX channel teardown
  1475. * @priv: The DaVinci EMAC private adapter structure
  1476. * @ch: RX channel number
  1477. *
  1478. * Called during device stop to teardown RX channel
  1479. *
  1480. */
  1481. static void emac_rxch_teardown(struct emac_priv *priv, u32 ch)
  1482. {
  1483. struct device *emac_dev = &priv->ndev->dev;
  1484. u32 teardown_cnt = 0xFFFFFFF0; /* Some high value */
  1485. while ((emac_read(EMAC_RXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
  1486. EMAC_TEARDOWN_VALUE) {
  1487. /* wait till tx teardown complete */
  1488. cpu_relax(); /* TODO: check if this helps ... */
  1489. --teardown_cnt;
  1490. if (0 == teardown_cnt) {
  1491. dev_err(emac_dev, "EMAC: RX teardown aborted\n");
  1492. break;
  1493. }
  1494. }
  1495. emac_write(EMAC_RXCP(ch), EMAC_TEARDOWN_VALUE);
  1496. }
  1497. /**
  1498. * emac_stop_rxch: Stop RX channel operation
  1499. * @priv: The DaVinci EMAC private adapter structure
  1500. * @ch: RX channel number
  1501. *
  1502. * Called during device stop to stop RX channel operation
  1503. *
  1504. */
  1505. static void emac_stop_rxch(struct emac_priv *priv, u32 ch)
  1506. {
  1507. struct emac_rxch *rxch = priv->rxch[ch];
  1508. if (rxch) {
  1509. rxch->teardown_pending = 1;
  1510. emac_write(EMAC_RXTEARDOWN, ch);
  1511. /* wait for teardown complete */
  1512. emac_rxch_teardown(priv, ch);
  1513. rxch->teardown_pending = 0;
  1514. emac_write(EMAC_RXINTMASKCLEAR, BIT(ch));
  1515. }
  1516. }
  1517. /**
  1518. * emac_cleanup_rxch: Book-keep function to clean RX channel resources
  1519. * @priv: The DaVinci EMAC private adapter structure
  1520. * @ch: RX channel number
  1521. *
  1522. * Called during device stop to clean up RX channel resources
  1523. *
  1524. */
  1525. static void emac_cleanup_rxch(struct emac_priv *priv, u32 ch)
  1526. {
  1527. struct emac_rxch *rxch = priv->rxch[ch];
  1528. struct emac_rx_bd __iomem *curr_bd;
  1529. if (rxch) {
  1530. /* free the receive buffers previously allocated */
  1531. curr_bd = rxch->active_queue_head;
  1532. while (curr_bd) {
  1533. if (curr_bd->buf_token) {
  1534. dev_kfree_skb_any((struct sk_buff *)\
  1535. curr_bd->buf_token);
  1536. }
  1537. curr_bd = curr_bd->next;
  1538. }
  1539. if (rxch->bd_mem)
  1540. rxch->bd_mem = NULL;
  1541. kfree(rxch);
  1542. priv->rxch[ch] = NULL;
  1543. }
  1544. }
  1545. /**
  1546. * emac_set_type0addr: Set EMAC Type0 mac address
  1547. * @priv: The DaVinci EMAC private adapter structure
  1548. * @ch: RX channel number
  1549. * @mac_addr: MAC address to set in device
  1550. *
  1551. * Called internally to set Type0 mac address of the adapter (Device)
  1552. *
  1553. * Returns success (0) or appropriate error code (none as of now)
  1554. */
  1555. static void emac_set_type0addr(struct emac_priv *priv, u32 ch, char *mac_addr)
  1556. {
  1557. u32 val;
  1558. val = ((mac_addr[5] << 8) | (mac_addr[4]));
  1559. emac_write(EMAC_MACSRCADDRLO, val);
  1560. val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
  1561. (mac_addr[1] << 8) | (mac_addr[0]));
  1562. emac_write(EMAC_MACSRCADDRHI, val);
  1563. val = emac_read(EMAC_RXUNICASTSET);
  1564. val |= BIT(ch);
  1565. emac_write(EMAC_RXUNICASTSET, val);
  1566. val = emac_read(EMAC_RXUNICASTCLEAR);
  1567. val &= ~BIT(ch);
  1568. emac_write(EMAC_RXUNICASTCLEAR, val);
  1569. }
  1570. /**
  1571. * emac_set_type1addr: Set EMAC Type1 mac address
  1572. * @priv: The DaVinci EMAC private adapter structure
  1573. * @ch: RX channel number
  1574. * @mac_addr: MAC address to set in device
  1575. *
  1576. * Called internally to set Type1 mac address of the adapter (Device)
  1577. *
  1578. * Returns success (0) or appropriate error code (none as of now)
  1579. */
  1580. static void emac_set_type1addr(struct emac_priv *priv, u32 ch, char *mac_addr)
  1581. {
  1582. u32 val;
  1583. emac_write(EMAC_MACINDEX, ch);
  1584. val = ((mac_addr[5] << 8) | mac_addr[4]);
  1585. emac_write(EMAC_MACADDRLO, val);
  1586. val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
  1587. (mac_addr[1] << 8) | (mac_addr[0]));
  1588. emac_write(EMAC_MACADDRHI, val);
  1589. emac_set_type0addr(priv, ch, mac_addr);
  1590. }
  1591. /**
  1592. * emac_set_type2addr: Set EMAC Type2 mac address
  1593. * @priv: The DaVinci EMAC private adapter structure
  1594. * @ch: RX channel number
  1595. * @mac_addr: MAC address to set in device
  1596. * @index: index into RX address entries
  1597. * @match: match parameter for RX address matching logic
  1598. *
  1599. * Called internally to set Type2 mac address of the adapter (Device)
  1600. *
  1601. * Returns success (0) or appropriate error code (none as of now)
  1602. */
  1603. static void emac_set_type2addr(struct emac_priv *priv, u32 ch,
  1604. char *mac_addr, int index, int match)
  1605. {
  1606. u32 val;
  1607. emac_write(EMAC_MACINDEX, index);
  1608. val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
  1609. (mac_addr[1] << 8) | (mac_addr[0]));
  1610. emac_write(EMAC_MACADDRHI, val);
  1611. val = ((mac_addr[5] << 8) | mac_addr[4] | ((ch & 0x7) << 16) | \
  1612. (match << 19) | BIT(20));
  1613. emac_write(EMAC_MACADDRLO, val);
  1614. emac_set_type0addr(priv, ch, mac_addr);
  1615. }
  1616. /**
  1617. * emac_setmac: Set mac address in the adapter (internal function)
  1618. * @priv: The DaVinci EMAC private adapter structure
  1619. * @ch: RX channel number
  1620. * @mac_addr: MAC address to set in device
  1621. *
  1622. * Called internally to set the mac address of the adapter (Device)
  1623. *
  1624. * Returns success (0) or appropriate error code (none as of now)
  1625. */
  1626. static void emac_setmac(struct emac_priv *priv, u32 ch, char *mac_addr)
  1627. {
  1628. struct device *emac_dev = &priv->ndev->dev;
  1629. if (priv->rx_addr_type == 0) {
  1630. emac_set_type0addr(priv, ch, mac_addr);
  1631. } else if (priv->rx_addr_type == 1) {
  1632. u32 cnt;
  1633. for (cnt = 0; cnt < EMAC_MAX_TXRX_CHANNELS; cnt++)
  1634. emac_set_type1addr(priv, ch, mac_addr);
  1635. } else if (priv->rx_addr_type == 2) {
  1636. emac_set_type2addr(priv, ch, mac_addr, ch, 1);
  1637. emac_set_type0addr(priv, ch, mac_addr);
  1638. } else {
  1639. if (netif_msg_drv(priv))
  1640. dev_err(emac_dev, "DaVinci EMAC: Wrong addressing\n");
  1641. }
  1642. }
  1643. /**
  1644. * emac_dev_setmac_addr: Set mac address in the adapter
  1645. * @ndev: The DaVinci EMAC network adapter
  1646. * @addr: MAC address to set in device
  1647. *
  1648. * Called by the system to set the mac address of the adapter (Device)
  1649. *
  1650. * Returns success (0) or appropriate error code (none as of now)
  1651. */
  1652. static int emac_dev_setmac_addr(struct net_device *ndev, void *addr)
  1653. {
  1654. struct emac_priv *priv = netdev_priv(ndev);
  1655. struct emac_rxch *rxch = priv->rxch[EMAC_DEF_RX_CH];
  1656. struct device *emac_dev = &priv->ndev->dev;
  1657. struct sockaddr *sa = addr;
  1658. if (!is_valid_ether_addr(sa->sa_data))
  1659. return -EINVAL;
  1660. /* Store mac addr in priv and rx channel and set it in EMAC hw */
  1661. memcpy(priv->mac_addr, sa->sa_data, ndev->addr_len);
  1662. memcpy(ndev->dev_addr, sa->sa_data, ndev->addr_len);
  1663. /* If the interface is down - rxch is NULL. */
  1664. /* MAC address is configured only after the interface is enabled. */
  1665. if (netif_running(ndev)) {
  1666. memcpy(rxch->mac_addr, sa->sa_data, ndev->addr_len);
  1667. emac_setmac(priv, EMAC_DEF_RX_CH, rxch->mac_addr);
  1668. }
  1669. if (netif_msg_drv(priv))
  1670. dev_notice(emac_dev, "DaVinci EMAC: emac_dev_setmac_addr %pM\n",
  1671. priv->mac_addr);
  1672. return 0;
  1673. }
  1674. /**
  1675. * emac_addbd_to_rx_queue: Recycle RX buffer descriptor
  1676. * @priv: The DaVinci EMAC private adapter structure
  1677. * @ch: RX channel number to process buffer descriptors for
  1678. * @curr_bd: current buffer descriptor
  1679. * @buffer: buffer pointer for descriptor
  1680. * @buf_token: buffer token (stores skb information)
  1681. *
  1682. * Prepares the recycled buffer descriptor and addes it to hardware
  1683. * receive queue - if queue empty this descriptor becomes the head
  1684. * else addes the descriptor to end of queue
  1685. *
  1686. */
  1687. static void emac_addbd_to_rx_queue(struct emac_priv *priv, u32 ch,
  1688. struct emac_rx_bd __iomem *curr_bd,
  1689. char *buffer, void *buf_token)
  1690. {
  1691. struct emac_rxch *rxch = priv->rxch[ch];
  1692. /* populate the hardware descriptor */
  1693. curr_bd->h_next = 0;
  1694. /* FIXME buff_ptr = dma_map_single(... buffer ...) */
  1695. curr_bd->buff_ptr = virt_to_phys(buffer);
  1696. curr_bd->off_b_len = rxch->buf_size;
  1697. curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
  1698. curr_bd->next = NULL;
  1699. curr_bd->data_ptr = buffer;
  1700. curr_bd->buf_token = buf_token;
  1701. /* write back */
  1702. BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  1703. if (rxch->active_queue_head == NULL) {
  1704. rxch->active_queue_head = curr_bd;
  1705. rxch->active_queue_tail = curr_bd;
  1706. if (0 != rxch->queue_active) {
  1707. emac_write(EMAC_RXHDP(ch),
  1708. emac_virt_to_phys(rxch->active_queue_head));
  1709. rxch->queue_active = 1;
  1710. }
  1711. } else {
  1712. struct emac_rx_bd __iomem *tail_bd;
  1713. u32 frame_status;
  1714. tail_bd = rxch->active_queue_tail;
  1715. rxch->active_queue_tail = curr_bd;
  1716. tail_bd->next = curr_bd;
  1717. tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
  1718. tail_bd->h_next = emac_virt_to_phys(curr_bd);
  1719. frame_status = tail_bd->mode;
  1720. if (frame_status & EMAC_CPPI_EOQ_BIT) {
  1721. emac_write(EMAC_RXHDP(ch),
  1722. emac_virt_to_phys(curr_bd));
  1723. frame_status &= ~(EMAC_CPPI_EOQ_BIT);
  1724. tail_bd->mode = frame_status;
  1725. ++rxch->end_of_queue_add;
  1726. }
  1727. }
  1728. ++rxch->recycled_bd;
  1729. }
  1730. /**
  1731. * emac_net_rx_cb: Prepares packet and sends to upper layer
  1732. * @priv: The DaVinci EMAC private adapter structure
  1733. * @net_pkt_list: Network packet list (received packets)
  1734. *
  1735. * Invalidates packet buffer memory and sends the received packet to upper
  1736. * layer
  1737. *
  1738. * Returns success or appropriate error code (none as of now)
  1739. */
  1740. static int emac_net_rx_cb(struct emac_priv *priv,
  1741. struct emac_netpktobj *net_pkt_list)
  1742. {
  1743. struct sk_buff *p_skb;
  1744. p_skb = (struct sk_buff *)net_pkt_list->pkt_token;
  1745. /* set length of packet */
  1746. skb_put(p_skb, net_pkt_list->pkt_length);
  1747. EMAC_CACHE_INVALIDATE((unsigned long)p_skb->data, p_skb->len);
  1748. p_skb->protocol = eth_type_trans(p_skb, priv->ndev);
  1749. netif_receive_skb(p_skb);
  1750. priv->net_dev_stats.rx_bytes += net_pkt_list->pkt_length;
  1751. priv->net_dev_stats.rx_packets++;
  1752. return 0;
  1753. }
  1754. /**
  1755. * emac_rx_bdproc: RX buffer descriptor (packet) processing
  1756. * @priv: The DaVinci EMAC private adapter structure
  1757. * @ch: RX channel number to process buffer descriptors for
  1758. * @budget: number of packets allowed to process
  1759. * @pending: indication to caller that packets are pending to process
  1760. *
  1761. * Processes RX buffer descriptors - checks ownership bit on the RX buffer
  1762. * descriptor, sends the receive packet to upper layer, allocates a new SKB
  1763. * and recycles the buffer descriptor (requeues it in hardware RX queue).
  1764. * Only "budget" number of packets are processed and indication of pending
  1765. * packets provided to the caller.
  1766. *
  1767. * Returns number of packets processed (and indication of pending packets)
  1768. */
  1769. static int emac_rx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
  1770. {
  1771. unsigned long flags;
  1772. u32 frame_status;
  1773. u32 pkts_processed = 0;
  1774. char *new_buffer;
  1775. struct emac_rx_bd __iomem *curr_bd;
  1776. struct emac_rx_bd __iomem *last_bd;
  1777. struct emac_netpktobj *curr_pkt, pkt_obj;
  1778. struct emac_netbufobj buf_obj;
  1779. struct emac_netbufobj *rx_buf_obj;
  1780. void *new_buf_token;
  1781. struct emac_rxch *rxch = priv->rxch[ch];
  1782. if (unlikely(1 == rxch->teardown_pending))
  1783. return 0;
  1784. ++rxch->proc_count;
  1785. spin_lock_irqsave(&priv->rx_lock, flags);
  1786. pkt_obj.buf_list = &buf_obj;
  1787. curr_pkt = &pkt_obj;
  1788. curr_bd = rxch->active_queue_head;
  1789. BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  1790. frame_status = curr_bd->mode;
  1791. while ((curr_bd) &&
  1792. ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
  1793. (pkts_processed < budget)) {
  1794. new_buffer = emac_net_alloc_rx_buf(priv, rxch->buf_size,
  1795. &new_buf_token, EMAC_DEF_RX_CH);
  1796. if (unlikely(NULL == new_buffer)) {
  1797. ++rxch->out_of_rx_buffers;
  1798. goto end_emac_rx_bdproc;
  1799. }
  1800. /* populate received packet data structure */
  1801. rx_buf_obj = &curr_pkt->buf_list[0];
  1802. rx_buf_obj->data_ptr = (char *)curr_bd->data_ptr;
  1803. rx_buf_obj->length = curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE;
  1804. rx_buf_obj->buf_token = curr_bd->buf_token;
  1805. curr_pkt->pkt_token = curr_pkt->buf_list->buf_token;
  1806. curr_pkt->num_bufs = 1;
  1807. curr_pkt->pkt_length =
  1808. (frame_status & EMAC_RX_BD_PKT_LENGTH_MASK);
  1809. emac_write(EMAC_RXCP(ch), emac_virt_to_phys(curr_bd));
  1810. ++rxch->processed_bd;
  1811. last_bd = curr_bd;
  1812. curr_bd = last_bd->next;
  1813. rxch->active_queue_head = curr_bd;
  1814. /* check if end of RX queue ? */
  1815. if (frame_status & EMAC_CPPI_EOQ_BIT) {
  1816. if (curr_bd) {
  1817. ++rxch->mis_queued_packets;
  1818. emac_write(EMAC_RXHDP(ch),
  1819. emac_virt_to_phys(curr_bd));
  1820. } else {
  1821. ++rxch->end_of_queue;
  1822. rxch->queue_active = 0;
  1823. }
  1824. }
  1825. /* recycle BD */
  1826. emac_addbd_to_rx_queue(priv, ch, last_bd, new_buffer,
  1827. new_buf_token);
  1828. /* return the packet to the user - BD ptr passed in
  1829. * last parameter for potential *future* use */
  1830. spin_unlock_irqrestore(&priv->rx_lock, flags);
  1831. emac_net_rx_cb(priv, curr_pkt);
  1832. spin_lock_irqsave(&priv->rx_lock, flags);
  1833. curr_bd = rxch->active_queue_head;
  1834. if (curr_bd) {
  1835. BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  1836. frame_status = curr_bd->mode;
  1837. }
  1838. ++pkts_processed;
  1839. }
  1840. end_emac_rx_bdproc:
  1841. spin_unlock_irqrestore(&priv->rx_lock, flags);
  1842. return pkts_processed;
  1843. }
  1844. /**
  1845. * emac_hw_enable: Enable EMAC hardware for packet transmission/reception
  1846. * @priv: The DaVinci EMAC private adapter structure
  1847. *
  1848. * Enables EMAC hardware for packet processing - enables PHY, enables RX
  1849. * for packet reception and enables device interrupts and then NAPI
  1850. *
  1851. * Returns success (0) or appropriate error code (none right now)
  1852. */
  1853. static int emac_hw_enable(struct emac_priv *priv)
  1854. {
  1855. u32 ch, val, mbp_enable, mac_control;
  1856. /* Soft reset */
  1857. emac_write(EMAC_SOFTRESET, 1);
  1858. while (emac_read(EMAC_SOFTRESET))
  1859. cpu_relax();
  1860. /* Disable interrupt & Set pacing for more interrupts initially */
  1861. emac_int_disable(priv);
  1862. /* Full duplex enable bit set when auto negotiation happens */
  1863. mac_control =
  1864. (((EMAC_DEF_TXPRIO_FIXED) ? (EMAC_MACCONTROL_TXPTYPE) : 0x0) |
  1865. ((priv->speed == 1000) ? EMAC_MACCONTROL_GIGABITEN : 0x0) |
  1866. ((EMAC_DEF_TXPACING_EN) ? (EMAC_MACCONTROL_TXPACEEN) : 0x0) |
  1867. ((priv->duplex == DUPLEX_FULL) ? 0x1 : 0));
  1868. emac_write(EMAC_MACCONTROL, mac_control);
  1869. mbp_enable =
  1870. (((EMAC_DEF_PASS_CRC) ? (EMAC_RXMBP_PASSCRC_MASK) : 0x0) |
  1871. ((EMAC_DEF_QOS_EN) ? (EMAC_RXMBP_QOSEN_MASK) : 0x0) |
  1872. ((EMAC_DEF_NO_BUFF_CHAIN) ? (EMAC_RXMBP_NOCHAIN_MASK) : 0x0) |
  1873. ((EMAC_DEF_MACCTRL_FRAME_EN) ? (EMAC_RXMBP_CMFEN_MASK) : 0x0) |
  1874. ((EMAC_DEF_SHORT_FRAME_EN) ? (EMAC_RXMBP_CSFEN_MASK) : 0x0) |
  1875. ((EMAC_DEF_ERROR_FRAME_EN) ? (EMAC_RXMBP_CEFEN_MASK) : 0x0) |
  1876. ((EMAC_DEF_PROM_EN) ? (EMAC_RXMBP_CAFEN_MASK) : 0x0) |
  1877. ((EMAC_DEF_PROM_CH & EMAC_RXMBP_CHMASK) << \
  1878. EMAC_RXMBP_PROMCH_SHIFT) |
  1879. ((EMAC_DEF_BCAST_EN) ? (EMAC_RXMBP_BROADEN_MASK) : 0x0) |
  1880. ((EMAC_DEF_BCAST_CH & EMAC_RXMBP_CHMASK) << \
  1881. EMAC_RXMBP_BROADCH_SHIFT) |
  1882. ((EMAC_DEF_MCAST_EN) ? (EMAC_RXMBP_MULTIEN_MASK) : 0x0) |
  1883. ((EMAC_DEF_MCAST_CH & EMAC_RXMBP_CHMASK) << \
  1884. EMAC_RXMBP_MULTICH_SHIFT));
  1885. emac_write(EMAC_RXMBPENABLE, mbp_enable);
  1886. emac_write(EMAC_RXMAXLEN, (EMAC_DEF_MAX_FRAME_SIZE &
  1887. EMAC_RX_MAX_LEN_MASK));
  1888. emac_write(EMAC_RXBUFFEROFFSET, (EMAC_DEF_BUFFER_OFFSET &
  1889. EMAC_RX_BUFFER_OFFSET_MASK));
  1890. emac_write(EMAC_RXFILTERLOWTHRESH, 0);
  1891. emac_write(EMAC_RXUNICASTCLEAR, EMAC_RX_UNICAST_CLEAR_ALL);
  1892. priv->rx_addr_type = (emac_read(EMAC_MACCONFIG) >> 8) & 0xFF;
  1893. val = emac_read(EMAC_TXCONTROL);
  1894. val |= EMAC_TX_CONTROL_TX_ENABLE_VAL;
  1895. emac_write(EMAC_TXCONTROL, val);
  1896. val = emac_read(EMAC_RXCONTROL);
  1897. val |= EMAC_RX_CONTROL_RX_ENABLE_VAL;
  1898. emac_write(EMAC_RXCONTROL, val);
  1899. emac_write(EMAC_MACINTMASKSET, EMAC_MAC_HOST_ERR_INTMASK_VAL);
  1900. for (ch = 0; ch < EMAC_DEF_MAX_TX_CH; ch++) {
  1901. emac_write(EMAC_TXHDP(ch), 0);
  1902. emac_write(EMAC_TXINTMASKSET, BIT(ch));
  1903. }
  1904. for (ch = 0; ch < EMAC_DEF_MAX_RX_CH; ch++) {
  1905. struct emac_rxch *rxch = priv->rxch[ch];
  1906. emac_setmac(priv, ch, rxch->mac_addr);
  1907. emac_write(EMAC_RXINTMASKSET, BIT(ch));
  1908. rxch->queue_active = 1;
  1909. emac_write(EMAC_RXHDP(ch),
  1910. emac_virt_to_phys(rxch->active_queue_head));
  1911. }
  1912. /* Enable MII */
  1913. val = emac_read(EMAC_MACCONTROL);
  1914. val |= (EMAC_MACCONTROL_GMIIEN);
  1915. emac_write(EMAC_MACCONTROL, val);
  1916. /* Enable NAPI and interrupts */
  1917. napi_enable(&priv->napi);
  1918. emac_int_enable(priv);
  1919. return 0;
  1920. }
  1921. /**
  1922. * emac_poll: EMAC NAPI Poll function
  1923. * @ndev: The DaVinci EMAC network adapter
  1924. * @budget: Number of receive packets to process (as told by NAPI layer)
  1925. *
  1926. * NAPI Poll function implemented to process packets as per budget. We check
  1927. * the type of interrupt on the device and accordingly call the TX or RX
  1928. * packet processing functions. We follow the budget for RX processing and
  1929. * also put a cap on number of TX pkts processed through config param. The
  1930. * NAPI schedule function is called if more packets pending.
  1931. *
  1932. * Returns number of packets received (in most cases; else TX pkts - rarely)
  1933. */
  1934. static int emac_poll(struct napi_struct *napi, int budget)
  1935. {
  1936. unsigned int mask;
  1937. struct emac_priv *priv = container_of(napi, struct emac_priv, napi);
  1938. struct net_device *ndev = priv->ndev;
  1939. struct device *emac_dev = &ndev->dev;
  1940. u32 status = 0;
  1941. u32 num_pkts = 0;
  1942. /* Check interrupt vectors and call packet processing */
  1943. status = emac_read(EMAC_MACINVECTOR);
  1944. mask = EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC;
  1945. if (priv->version == EMAC_VERSION_2)
  1946. mask = EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC;
  1947. if (status & mask) {
  1948. num_pkts = emac_tx_bdproc(priv, EMAC_DEF_TX_CH,
  1949. EMAC_DEF_TX_MAX_SERVICE);
  1950. } /* TX processing */
  1951. if (num_pkts)
  1952. return budget;
  1953. mask = EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC;
  1954. if (priv->version == EMAC_VERSION_2)
  1955. mask = EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC;
  1956. if (status & mask) {
  1957. num_pkts = emac_rx_bdproc(priv, EMAC_DEF_RX_CH, budget);
  1958. } /* RX processing */
  1959. if (num_pkts < budget) {
  1960. napi_complete(napi);
  1961. emac_int_enable(priv);
  1962. }
  1963. mask = EMAC_DM644X_MAC_IN_VECTOR_HOST_INT;
  1964. if (priv->version == EMAC_VERSION_2)
  1965. mask = EMAC_DM646X_MAC_IN_VECTOR_HOST_INT;
  1966. if (unlikely(status & mask)) {
  1967. u32 ch, cause;
  1968. dev_err(emac_dev, "DaVinci EMAC: Fatal Hardware Error\n");
  1969. netif_stop_queue(ndev);
  1970. napi_disable(&priv->napi);
  1971. status = emac_read(EMAC_MACSTATUS);
  1972. cause = ((status & EMAC_MACSTATUS_TXERRCODE_MASK) >>
  1973. EMAC_MACSTATUS_TXERRCODE_SHIFT);
  1974. if (cause) {
  1975. ch = ((status & EMAC_MACSTATUS_TXERRCH_MASK) >>
  1976. EMAC_MACSTATUS_TXERRCH_SHIFT);
  1977. if (net_ratelimit()) {
  1978. dev_err(emac_dev, "TX Host error %s on ch=%d\n",
  1979. &emac_txhost_errcodes[cause][0], ch);
  1980. }
  1981. }
  1982. cause = ((status & EMAC_MACSTATUS_RXERRCODE_MASK) >>
  1983. EMAC_MACSTATUS_RXERRCODE_SHIFT);
  1984. if (cause) {
  1985. ch = ((status & EMAC_MACSTATUS_RXERRCH_MASK) >>
  1986. EMAC_MACSTATUS_RXERRCH_SHIFT);
  1987. if (netif_msg_hw(priv) && net_ratelimit())
  1988. dev_err(emac_dev, "RX Host error %s on ch=%d\n",
  1989. &emac_rxhost_errcodes[cause][0], ch);
  1990. }
  1991. } /* Host error processing */
  1992. return num_pkts;
  1993. }
  1994. #ifdef CONFIG_NET_POLL_CONTROLLER
  1995. /**
  1996. * emac_poll_controller: EMAC Poll controller function
  1997. * @ndev: The DaVinci EMAC network adapter
  1998. *
  1999. * Polled functionality used by netconsole and others in non interrupt mode
  2000. *
  2001. */
  2002. void emac_poll_controller(struct net_device *ndev)
  2003. {
  2004. struct emac_priv *priv = netdev_priv(ndev);
  2005. emac_int_disable(priv);
  2006. emac_irq(ndev->irq, ndev);
  2007. emac_int_enable(priv);
  2008. }
  2009. #endif
  2010. /* PHY/MII bus related */
  2011. /* Wait until mdio is ready for next command */
  2012. #define MDIO_WAIT_FOR_USER_ACCESS\
  2013. while ((emac_mdio_read((MDIO_USERACCESS(0))) &\
  2014. MDIO_USERACCESS_GO) != 0)
  2015. static int emac_mii_read(struct mii_bus *bus, int phy_id, int phy_reg)
  2016. {
  2017. unsigned int phy_data = 0;
  2018. unsigned int phy_control;
  2019. /* Wait until mdio is ready for next command */
  2020. MDIO_WAIT_FOR_USER_ACCESS;
  2021. phy_control = (MDIO_USERACCESS_GO |
  2022. MDIO_USERACCESS_READ |
  2023. ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
  2024. ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
  2025. (phy_data & MDIO_USERACCESS_DATA));
  2026. emac_mdio_write(MDIO_USERACCESS(0), phy_control);
  2027. /* Wait until mdio is ready for next command */
  2028. MDIO_WAIT_FOR_USER_ACCESS;
  2029. return emac_mdio_read(MDIO_USERACCESS(0)) & MDIO_USERACCESS_DATA;
  2030. }
  2031. static int emac_mii_write(struct mii_bus *bus, int phy_id,
  2032. int phy_reg, u16 phy_data)
  2033. {
  2034. unsigned int control;
  2035. /* until mdio is ready for next command */
  2036. MDIO_WAIT_FOR_USER_ACCESS;
  2037. control = (MDIO_USERACCESS_GO |
  2038. MDIO_USERACCESS_WRITE |
  2039. ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
  2040. ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
  2041. (phy_data & MDIO_USERACCESS_DATA));
  2042. emac_mdio_write(MDIO_USERACCESS(0), control);
  2043. return 0;
  2044. }
  2045. static int emac_mii_reset(struct mii_bus *bus)
  2046. {
  2047. unsigned int clk_div;
  2048. int mdio_bus_freq = emac_bus_frequency;
  2049. if (mdio_max_freq & mdio_bus_freq)
  2050. clk_div = ((mdio_bus_freq / mdio_max_freq) - 1);
  2051. else
  2052. clk_div = 0xFF;
  2053. clk_div &= MDIO_CONTROL_CLKDIV;
  2054. /* Set enable and clock divider in MDIOControl */
  2055. emac_mdio_write(MDIO_CONTROL, (clk_div | MDIO_CONTROL_ENABLE));
  2056. return 0;
  2057. }
  2058. static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, PHY_POLL };
  2059. /* emac_driver: EMAC MII bus structure */
  2060. static struct mii_bus *emac_mii;
  2061. static void emac_adjust_link(struct net_device *ndev)
  2062. {
  2063. struct emac_priv *priv = netdev_priv(ndev);
  2064. struct phy_device *phydev = priv->phydev;
  2065. unsigned long flags;
  2066. int new_state = 0;
  2067. spin_lock_irqsave(&priv->lock, flags);
  2068. if (phydev->link) {
  2069. /* check the mode of operation - full/half duplex */
  2070. if (phydev->duplex != priv->duplex) {
  2071. new_state = 1;
  2072. priv->duplex = phydev->duplex;
  2073. }
  2074. if (phydev->speed != priv->speed) {
  2075. new_state = 1;
  2076. priv->speed = phydev->speed;
  2077. }
  2078. if (!priv->link) {
  2079. new_state = 1;
  2080. priv->link = 1;
  2081. }
  2082. } else if (priv->link) {
  2083. new_state = 1;
  2084. priv->link = 0;
  2085. priv->speed = 0;
  2086. priv->duplex = ~0;
  2087. }
  2088. if (new_state) {
  2089. emac_update_phystatus(priv);
  2090. phy_print_status(priv->phydev);
  2091. }
  2092. spin_unlock_irqrestore(&priv->lock, flags);
  2093. }
  2094. /*************************************************************************
  2095. * Linux Driver Model
  2096. *************************************************************************/
  2097. /**
  2098. * emac_devioctl: EMAC adapter ioctl
  2099. * @ndev: The DaVinci EMAC network adapter
  2100. * @ifrq: request parameter
  2101. * @cmd: command parameter
  2102. *
  2103. * EMAC driver ioctl function
  2104. *
  2105. * Returns success(0) or appropriate error code
  2106. */
  2107. static int emac_devioctl(struct net_device *ndev, struct ifreq *ifrq, int cmd)
  2108. {
  2109. dev_warn(&ndev->dev, "DaVinci EMAC: ioctl not supported\n");
  2110. if (!(netif_running(ndev)))
  2111. return -EINVAL;
  2112. /* TODO: Add phy read and write and private statistics get feature */
  2113. return -EOPNOTSUPP;
  2114. }
  2115. /**
  2116. * emac_dev_open: EMAC device open
  2117. * @ndev: The DaVinci EMAC network adapter
  2118. *
  2119. * Called when system wants to start the interface. We init TX/RX channels
  2120. * and enable the hardware for packet reception/transmission and start the
  2121. * network queue.
  2122. *
  2123. * Returns 0 for a successful open, or appropriate error code
  2124. */
  2125. static int emac_dev_open(struct net_device *ndev)
  2126. {
  2127. struct device *emac_dev = &ndev->dev;
  2128. u32 rc, cnt, ch;
  2129. int phy_addr;
  2130. struct resource *res;
  2131. int q, m;
  2132. int i = 0;
  2133. int k = 0;
  2134. struct emac_priv *priv = netdev_priv(ndev);
  2135. netif_carrier_off(ndev);
  2136. for (cnt = 0; cnt <= ETH_ALEN; cnt++)
  2137. ndev->dev_addr[cnt] = priv->mac_addr[cnt];
  2138. /* Configuration items */
  2139. priv->rx_buf_size = EMAC_DEF_MAX_FRAME_SIZE + NET_IP_ALIGN;
  2140. /* Clear basic hardware */
  2141. for (ch = 0; ch < EMAC_MAX_TXRX_CHANNELS; ch++) {
  2142. emac_write(EMAC_TXHDP(ch), 0);
  2143. emac_write(EMAC_RXHDP(ch), 0);
  2144. emac_write(EMAC_RXHDP(ch), 0);
  2145. emac_write(EMAC_RXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
  2146. emac_write(EMAC_TXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
  2147. }
  2148. priv->mac_hash1 = 0;
  2149. priv->mac_hash2 = 0;
  2150. emac_write(EMAC_MACHASH1, 0);
  2151. emac_write(EMAC_MACHASH2, 0);
  2152. /* multi ch not supported - open 1 TX, 1RX ch by default */
  2153. rc = emac_init_txch(priv, EMAC_DEF_TX_CH);
  2154. if (0 != rc) {
  2155. dev_err(emac_dev, "DaVinci EMAC: emac_init_txch() failed");
  2156. return rc;
  2157. }
  2158. rc = emac_init_rxch(priv, EMAC_DEF_RX_CH, priv->mac_addr);
  2159. if (0 != rc) {
  2160. dev_err(emac_dev, "DaVinci EMAC: emac_init_rxch() failed");
  2161. return rc;
  2162. }
  2163. /* Request IRQ */
  2164. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
  2165. for (i = res->start; i <= res->end; i++) {
  2166. if (request_irq(i, emac_irq, IRQF_DISABLED,
  2167. ndev->name, ndev))
  2168. goto rollback;
  2169. }
  2170. k++;
  2171. }
  2172. /* Start/Enable EMAC hardware */
  2173. emac_hw_enable(priv);
  2174. /* find the first phy */
  2175. priv->phydev = NULL;
  2176. if (priv->phy_mask) {
  2177. emac_mii_reset(priv->mii_bus);
  2178. for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
  2179. if (priv->mii_bus->phy_map[phy_addr]) {
  2180. priv->phydev = priv->mii_bus->phy_map[phy_addr];
  2181. break;
  2182. }
  2183. }
  2184. if (!priv->phydev) {
  2185. printk(KERN_ERR "%s: no PHY found\n", ndev->name);
  2186. return -1;
  2187. }
  2188. priv->phydev = phy_connect(ndev, dev_name(&priv->phydev->dev),
  2189. &emac_adjust_link, 0, PHY_INTERFACE_MODE_MII);
  2190. if (IS_ERR(priv->phydev)) {
  2191. printk(KERN_ERR "%s: Could not attach to PHY\n",
  2192. ndev->name);
  2193. return PTR_ERR(priv->phydev);
  2194. }
  2195. priv->link = 0;
  2196. priv->speed = 0;
  2197. priv->duplex = ~0;
  2198. printk(KERN_INFO "%s: attached PHY driver [%s] "
  2199. "(mii_bus:phy_addr=%s, id=%x)\n", ndev->name,
  2200. priv->phydev->drv->name, dev_name(&priv->phydev->dev),
  2201. priv->phydev->phy_id);
  2202. } else{
  2203. /* No PHY , fix the link, speed and duplex settings */
  2204. priv->link = 1;
  2205. priv->speed = SPEED_100;
  2206. priv->duplex = DUPLEX_FULL;
  2207. emac_update_phystatus(priv);
  2208. }
  2209. if (!netif_running(ndev)) /* debug only - to avoid compiler warning */
  2210. emac_dump_regs(priv);
  2211. if (netif_msg_drv(priv))
  2212. dev_notice(emac_dev, "DaVinci EMAC: Opened %s\n", ndev->name);
  2213. if (priv->phy_mask)
  2214. phy_start(priv->phydev);
  2215. return 0;
  2216. rollback:
  2217. dev_err(emac_dev, "DaVinci EMAC: request_irq() failed");
  2218. for (q = k; k >= 0; k--) {
  2219. for (m = i; m >= res->start; m--)
  2220. free_irq(m, ndev);
  2221. res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k-1);
  2222. m = res->end;
  2223. }
  2224. return -EBUSY;
  2225. }
  2226. /**
  2227. * emac_dev_stop: EMAC device stop
  2228. * @ndev: The DaVinci EMAC network adapter
  2229. *
  2230. * Called when system wants to stop or down the interface. We stop the network
  2231. * queue, disable interrupts and cleanup TX/RX channels.
  2232. *
  2233. * We return the statistics in net_device_stats structure pulled from emac
  2234. */
  2235. static int emac_dev_stop(struct net_device *ndev)
  2236. {
  2237. struct resource *res;
  2238. int i = 0;
  2239. int irq_num;
  2240. struct emac_priv *priv = netdev_priv(ndev);
  2241. struct device *emac_dev = &ndev->dev;
  2242. /* inform the upper layers. */
  2243. netif_stop_queue(ndev);
  2244. napi_disable(&priv->napi);
  2245. netif_carrier_off(ndev);
  2246. emac_int_disable(priv);
  2247. emac_stop_txch(priv, EMAC_DEF_TX_CH);
  2248. emac_stop_rxch(priv, EMAC_DEF_RX_CH);
  2249. emac_cleanup_txch(priv, EMAC_DEF_TX_CH);
  2250. emac_cleanup_rxch(priv, EMAC_DEF_RX_CH);
  2251. emac_write(EMAC_SOFTRESET, 1);
  2252. if (priv->phydev)
  2253. phy_disconnect(priv->phydev);
  2254. /* Free IRQ */
  2255. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, i))) {
  2256. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  2257. free_irq(irq_num, priv->ndev);
  2258. i++;
  2259. }
  2260. if (netif_msg_drv(priv))
  2261. dev_notice(emac_dev, "DaVinci EMAC: %s stopped\n", ndev->name);
  2262. return 0;
  2263. }
  2264. /**
  2265. * emac_dev_getnetstats: EMAC get statistics function
  2266. * @ndev: The DaVinci EMAC network adapter
  2267. *
  2268. * Called when system wants to get statistics from the device.
  2269. *
  2270. * We return the statistics in net_device_stats structure pulled from emac
  2271. */
  2272. static struct net_device_stats *emac_dev_getnetstats(struct net_device *ndev)
  2273. {
  2274. struct emac_priv *priv = netdev_priv(ndev);
  2275. u32 mac_control;
  2276. u32 stats_clear_mask;
  2277. /* update emac hardware stats and reset the registers*/
  2278. mac_control = emac_read(EMAC_MACCONTROL);
  2279. if (mac_control & EMAC_MACCONTROL_GMIIEN)
  2280. stats_clear_mask = EMAC_STATS_CLR_MASK;
  2281. else
  2282. stats_clear_mask = 0;
  2283. priv->net_dev_stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
  2284. emac_write(EMAC_RXMCASTFRAMES, stats_clear_mask);
  2285. priv->net_dev_stats.collisions += (emac_read(EMAC_TXCOLLISION) +
  2286. emac_read(EMAC_TXSINGLECOLL) +
  2287. emac_read(EMAC_TXMULTICOLL));
  2288. emac_write(EMAC_TXCOLLISION, stats_clear_mask);
  2289. emac_write(EMAC_TXSINGLECOLL, stats_clear_mask);
  2290. emac_write(EMAC_TXMULTICOLL, stats_clear_mask);
  2291. priv->net_dev_stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
  2292. emac_read(EMAC_RXJABBER) +
  2293. emac_read(EMAC_RXUNDERSIZED));
  2294. emac_write(EMAC_RXOVERSIZED, stats_clear_mask);
  2295. emac_write(EMAC_RXJABBER, stats_clear_mask);
  2296. emac_write(EMAC_RXUNDERSIZED, stats_clear_mask);
  2297. priv->net_dev_stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
  2298. emac_read(EMAC_RXMOFOVERRUNS));
  2299. emac_write(EMAC_RXSOFOVERRUNS, stats_clear_mask);
  2300. emac_write(EMAC_RXMOFOVERRUNS, stats_clear_mask);
  2301. priv->net_dev_stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
  2302. emac_write(EMAC_RXDMAOVERRUNS, stats_clear_mask);
  2303. priv->net_dev_stats.tx_carrier_errors +=
  2304. emac_read(EMAC_TXCARRIERSENSE);
  2305. emac_write(EMAC_TXCARRIERSENSE, stats_clear_mask);
  2306. priv->net_dev_stats.tx_fifo_errors = emac_read(EMAC_TXUNDERRUN);
  2307. emac_write(EMAC_TXUNDERRUN, stats_clear_mask);
  2308. return &priv->net_dev_stats;
  2309. }
  2310. static const struct net_device_ops emac_netdev_ops = {
  2311. .ndo_open = emac_dev_open,
  2312. .ndo_stop = emac_dev_stop,
  2313. .ndo_start_xmit = emac_dev_xmit,
  2314. .ndo_set_multicast_list = emac_dev_mcast_set,
  2315. .ndo_set_mac_address = emac_dev_setmac_addr,
  2316. .ndo_do_ioctl = emac_devioctl,
  2317. .ndo_tx_timeout = emac_dev_tx_timeout,
  2318. .ndo_get_stats = emac_dev_getnetstats,
  2319. #ifdef CONFIG_NET_POLL_CONTROLLER
  2320. .ndo_poll_controller = emac_poll_controller,
  2321. #endif
  2322. };
  2323. /**
  2324. * davinci_emac_probe: EMAC device probe
  2325. * @pdev: The DaVinci EMAC device that we are removing
  2326. *
  2327. * Called when probing for emac devicesr. We get details of instances and
  2328. * resource information from platform init and register a network device
  2329. * and allocate resources necessary for driver to perform
  2330. */
  2331. static int __devinit davinci_emac_probe(struct platform_device *pdev)
  2332. {
  2333. int rc = 0;
  2334. struct resource *res;
  2335. struct net_device *ndev;
  2336. struct emac_priv *priv;
  2337. unsigned long size;
  2338. struct emac_platform_data *pdata;
  2339. struct device *emac_dev;
  2340. /* obtain emac clock from kernel */
  2341. emac_clk = clk_get(&pdev->dev, NULL);
  2342. if (IS_ERR(emac_clk)) {
  2343. printk(KERN_ERR "DaVinci EMAC: Failed to get EMAC clock\n");
  2344. return -EBUSY;
  2345. }
  2346. emac_bus_frequency = clk_get_rate(emac_clk);
  2347. /* TODO: Probe PHY here if possible */
  2348. ndev = alloc_etherdev(sizeof(struct emac_priv));
  2349. if (!ndev) {
  2350. printk(KERN_ERR "DaVinci EMAC: Error allocating net_device\n");
  2351. clk_put(emac_clk);
  2352. return -ENOMEM;
  2353. }
  2354. platform_set_drvdata(pdev, ndev);
  2355. priv = netdev_priv(ndev);
  2356. priv->pdev = pdev;
  2357. priv->ndev = ndev;
  2358. priv->msg_enable = netif_msg_init(debug_level, DAVINCI_EMAC_DEBUG);
  2359. spin_lock_init(&priv->tx_lock);
  2360. spin_lock_init(&priv->rx_lock);
  2361. spin_lock_init(&priv->lock);
  2362. pdata = pdev->dev.platform_data;
  2363. if (!pdata) {
  2364. printk(KERN_ERR "DaVinci EMAC: No platfrom data\n");
  2365. return -ENODEV;
  2366. }
  2367. /* MAC addr and PHY mask , RMII enable info from platform_data */
  2368. memcpy(priv->mac_addr, pdata->mac_addr, 6);
  2369. priv->phy_mask = pdata->phy_mask;
  2370. priv->rmii_en = pdata->rmii_en;
  2371. priv->version = pdata->version;
  2372. emac_dev = &ndev->dev;
  2373. /* Get EMAC platform data */
  2374. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2375. if (!res) {
  2376. dev_err(emac_dev, "DaVinci EMAC: Error getting res\n");
  2377. rc = -ENOENT;
  2378. goto probe_quit;
  2379. }
  2380. priv->emac_base_phys = res->start + pdata->ctrl_reg_offset;
  2381. size = res->end - res->start + 1;
  2382. if (!request_mem_region(res->start, size, ndev->name)) {
  2383. dev_err(emac_dev, "DaVinci EMAC: failed request_mem_region() \
  2384. for regs\n");
  2385. rc = -ENXIO;
  2386. goto probe_quit;
  2387. }
  2388. priv->remap_addr = ioremap(res->start, size);
  2389. if (!priv->remap_addr) {
  2390. dev_err(emac_dev, "Unable to map IO\n");
  2391. rc = -ENOMEM;
  2392. release_mem_region(res->start, size);
  2393. goto probe_quit;
  2394. }
  2395. priv->emac_base = priv->remap_addr + pdata->ctrl_reg_offset;
  2396. ndev->base_addr = (unsigned long)priv->remap_addr;
  2397. priv->ctrl_base = priv->remap_addr + pdata->ctrl_mod_reg_offset;
  2398. priv->ctrl_ram_size = pdata->ctrl_ram_size;
  2399. priv->emac_ctrl_ram = priv->remap_addr + pdata->ctrl_ram_offset;
  2400. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2401. if (!res) {
  2402. dev_err(emac_dev, "DaVinci EMAC: Error getting irq res\n");
  2403. rc = -ENOENT;
  2404. goto no_irq_res;
  2405. }
  2406. ndev->irq = res->start;
  2407. if (!is_valid_ether_addr(priv->mac_addr)) {
  2408. /* Use random MAC if none passed */
  2409. random_ether_addr(priv->mac_addr);
  2410. printk(KERN_WARNING "%s: using random MAC addr: %pM\n",
  2411. __func__, priv->mac_addr);
  2412. }
  2413. ndev->netdev_ops = &emac_netdev_ops;
  2414. SET_ETHTOOL_OPS(ndev, &ethtool_ops);
  2415. netif_napi_add(ndev, &priv->napi, emac_poll, EMAC_POLL_WEIGHT);
  2416. /* register the network device */
  2417. SET_NETDEV_DEV(ndev, &pdev->dev);
  2418. rc = register_netdev(ndev);
  2419. if (rc) {
  2420. dev_err(emac_dev, "DaVinci EMAC: Error in register_netdev\n");
  2421. rc = -ENODEV;
  2422. goto netdev_reg_err;
  2423. }
  2424. clk_enable(emac_clk);
  2425. /* MII/Phy intialisation, mdio bus registration */
  2426. emac_mii = mdiobus_alloc();
  2427. if (emac_mii == NULL) {
  2428. dev_err(emac_dev, "DaVinci EMAC: Error allocating mii_bus\n");
  2429. rc = -ENOMEM;
  2430. goto mdio_alloc_err;
  2431. }
  2432. priv->mii_bus = emac_mii;
  2433. emac_mii->name = "emac-mii",
  2434. emac_mii->read = emac_mii_read,
  2435. emac_mii->write = emac_mii_write,
  2436. emac_mii->reset = emac_mii_reset,
  2437. emac_mii->irq = mii_irqs,
  2438. emac_mii->phy_mask = ~(priv->phy_mask);
  2439. emac_mii->parent = &pdev->dev;
  2440. emac_mii->priv = priv->remap_addr + pdata->mdio_reg_offset;
  2441. snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%x", priv->pdev->id);
  2442. mdio_max_freq = pdata->mdio_max_freq;
  2443. emac_mii->reset(emac_mii);
  2444. /* Register the MII bus */
  2445. rc = mdiobus_register(emac_mii);
  2446. if (rc)
  2447. goto mdiobus_quit;
  2448. if (netif_msg_probe(priv)) {
  2449. dev_notice(emac_dev, "DaVinci EMAC Probe found device "\
  2450. "(regs: %p, irq: %d)\n",
  2451. (void *)priv->emac_base_phys, ndev->irq);
  2452. }
  2453. return 0;
  2454. mdiobus_quit:
  2455. mdiobus_free(emac_mii);
  2456. netdev_reg_err:
  2457. mdio_alloc_err:
  2458. no_irq_res:
  2459. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2460. release_mem_region(res->start, res->end - res->start + 1);
  2461. iounmap(priv->remap_addr);
  2462. probe_quit:
  2463. clk_put(emac_clk);
  2464. free_netdev(ndev);
  2465. return rc;
  2466. }
  2467. /**
  2468. * davinci_emac_remove: EMAC device remove
  2469. * @pdev: The DaVinci EMAC device that we are removing
  2470. *
  2471. * Called when removing the device driver. We disable clock usage and release
  2472. * the resources taken up by the driver and unregister network device
  2473. */
  2474. static int __devexit davinci_emac_remove(struct platform_device *pdev)
  2475. {
  2476. struct resource *res;
  2477. struct net_device *ndev = platform_get_drvdata(pdev);
  2478. struct emac_priv *priv = netdev_priv(ndev);
  2479. dev_notice(&ndev->dev, "DaVinci EMAC: davinci_emac_remove()\n");
  2480. platform_set_drvdata(pdev, NULL);
  2481. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2482. mdiobus_unregister(priv->mii_bus);
  2483. mdiobus_free(priv->mii_bus);
  2484. release_mem_region(res->start, res->end - res->start + 1);
  2485. unregister_netdev(ndev);
  2486. free_netdev(ndev);
  2487. iounmap(priv->remap_addr);
  2488. clk_disable(emac_clk);
  2489. clk_put(emac_clk);
  2490. return 0;
  2491. }
  2492. /**
  2493. * davinci_emac_driver: EMAC platform driver structure
  2494. *
  2495. * We implement only probe and remove functions - suspend/resume and
  2496. * others not supported by this module
  2497. */
  2498. static struct platform_driver davinci_emac_driver = {
  2499. .driver = {
  2500. .name = "davinci_emac",
  2501. .owner = THIS_MODULE,
  2502. },
  2503. .probe = davinci_emac_probe,
  2504. .remove = __devexit_p(davinci_emac_remove),
  2505. };
  2506. /**
  2507. * davinci_emac_init: EMAC driver module init
  2508. *
  2509. * Called when initializing the driver. We register the driver with
  2510. * the platform.
  2511. */
  2512. static int __init davinci_emac_init(void)
  2513. {
  2514. return platform_driver_register(&davinci_emac_driver);
  2515. }
  2516. late_initcall(davinci_emac_init);
  2517. /**
  2518. * davinci_emac_exit: EMAC driver module exit
  2519. *
  2520. * Called when exiting the driver completely. We unregister the driver with
  2521. * the platform and exit
  2522. */
  2523. static void __exit davinci_emac_exit(void)
  2524. {
  2525. platform_driver_unregister(&davinci_emac_driver);
  2526. }
  2527. module_exit(davinci_emac_exit);
  2528. MODULE_LICENSE("GPL");
  2529. MODULE_AUTHOR("DaVinci EMAC Maintainer: Anant Gole <anantgole@ti.com>");
  2530. MODULE_AUTHOR("DaVinci EMAC Maintainer: Chaithrika U S <chaithrika@ti.com>");
  2531. MODULE_DESCRIPTION("DaVinci EMAC Ethernet driver");