bcmgenet.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433
  1. /*
  2. * Broadcom GENET (Gigabit Ethernet) controller driver
  3. *
  4. * Copyright (c) 2014 Broadcom Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) "bcmgenet: " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/types.h>
  15. #include <linux/fcntl.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/string.h>
  18. #include <linux/if_ether.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/delay.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/pm.h>
  25. #include <linux/clk.h>
  26. #include <linux/of.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/of_net.h>
  30. #include <linux/of_platform.h>
  31. #include <net/arp.h>
  32. #include <linux/mii.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/inetdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/in.h>
  39. #include <linux/ip.h>
  40. #include <linux/ipv6.h>
  41. #include <linux/phy.h>
  42. #include <linux/platform_data/bcmgenet.h>
  43. #include <asm/unaligned.h>
  44. #include "bcmgenet.h"
  45. /* Maximum number of hardware queues, downsized if needed */
  46. #define GENET_MAX_MQ_CNT 4
  47. /* Default highest priority queue for multi queue support */
  48. #define GENET_Q0_PRIORITY 0
  49. #define GENET_Q16_RX_BD_CNT \
  50. (TOTAL_DESC - priv->hw_params->rx_queues * priv->hw_params->rx_bds_per_q)
  51. #define GENET_Q16_TX_BD_CNT \
  52. (TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q)
  53. #define RX_BUF_LENGTH 2048
  54. #define SKB_ALIGNMENT 32
  55. /* Tx/Rx DMA register offset, skip 256 descriptors */
  56. #define WORDS_PER_BD(p) (p->hw_params->words_per_bd)
  57. #define DMA_DESC_SIZE (WORDS_PER_BD(priv) * sizeof(u32))
  58. #define GENET_TDMA_REG_OFF (priv->hw_params->tdma_offset + \
  59. TOTAL_DESC * DMA_DESC_SIZE)
  60. #define GENET_RDMA_REG_OFF (priv->hw_params->rdma_offset + \
  61. TOTAL_DESC * DMA_DESC_SIZE)
  62. static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv,
  63. void __iomem *d, u32 value)
  64. {
  65. __raw_writel(value, d + DMA_DESC_LENGTH_STATUS);
  66. }
  67. static inline u32 dmadesc_get_length_status(struct bcmgenet_priv *priv,
  68. void __iomem *d)
  69. {
  70. return __raw_readl(d + DMA_DESC_LENGTH_STATUS);
  71. }
  72. static inline void dmadesc_set_addr(struct bcmgenet_priv *priv,
  73. void __iomem *d,
  74. dma_addr_t addr)
  75. {
  76. __raw_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO);
  77. /* Register writes to GISB bus can take couple hundred nanoseconds
  78. * and are done for each packet, save these expensive writes unless
  79. * the platform is explicitly configured for 64-bits/LPAE.
  80. */
  81. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  82. if (priv->hw_params->flags & GENET_HAS_40BITS)
  83. __raw_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI);
  84. #endif
  85. }
  86. /* Combined address + length/status setter */
  87. static inline void dmadesc_set(struct bcmgenet_priv *priv,
  88. void __iomem *d, dma_addr_t addr, u32 val)
  89. {
  90. dmadesc_set_length_status(priv, d, val);
  91. dmadesc_set_addr(priv, d, addr);
  92. }
  93. static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv,
  94. void __iomem *d)
  95. {
  96. dma_addr_t addr;
  97. addr = __raw_readl(d + DMA_DESC_ADDRESS_LO);
  98. /* Register writes to GISB bus can take couple hundred nanoseconds
  99. * and are done for each packet, save these expensive writes unless
  100. * the platform is explicitly configured for 64-bits/LPAE.
  101. */
  102. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  103. if (priv->hw_params->flags & GENET_HAS_40BITS)
  104. addr |= (u64)__raw_readl(d + DMA_DESC_ADDRESS_HI) << 32;
  105. #endif
  106. return addr;
  107. }
  108. #define GENET_VER_FMT "%1d.%1d EPHY: 0x%04x"
  109. #define GENET_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
  110. NETIF_MSG_LINK)
  111. static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv)
  112. {
  113. if (GENET_IS_V1(priv))
  114. return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1);
  115. else
  116. return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL);
  117. }
  118. static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
  119. {
  120. if (GENET_IS_V1(priv))
  121. bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1);
  122. else
  123. bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL);
  124. }
  125. /* These macros are defined to deal with register map change
  126. * between GENET1.1 and GENET2. Only those currently being used
  127. * by driver are defined.
  128. */
  129. static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv)
  130. {
  131. if (GENET_IS_V1(priv))
  132. return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1);
  133. else
  134. return __raw_readl(priv->base +
  135. priv->hw_params->tbuf_offset + TBUF_CTRL);
  136. }
  137. static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
  138. {
  139. if (GENET_IS_V1(priv))
  140. bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1);
  141. else
  142. __raw_writel(val, priv->base +
  143. priv->hw_params->tbuf_offset + TBUF_CTRL);
  144. }
  145. static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv)
  146. {
  147. if (GENET_IS_V1(priv))
  148. return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1);
  149. else
  150. return __raw_readl(priv->base +
  151. priv->hw_params->tbuf_offset + TBUF_BP_MC);
  152. }
  153. static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val)
  154. {
  155. if (GENET_IS_V1(priv))
  156. bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1);
  157. else
  158. __raw_writel(val, priv->base +
  159. priv->hw_params->tbuf_offset + TBUF_BP_MC);
  160. }
  161. /* RX/TX DMA register accessors */
  162. enum dma_reg {
  163. DMA_RING_CFG = 0,
  164. DMA_CTRL,
  165. DMA_STATUS,
  166. DMA_SCB_BURST_SIZE,
  167. DMA_ARB_CTRL,
  168. DMA_PRIORITY_0,
  169. DMA_PRIORITY_1,
  170. DMA_PRIORITY_2,
  171. DMA_INDEX2RING_0,
  172. DMA_INDEX2RING_1,
  173. DMA_INDEX2RING_2,
  174. DMA_INDEX2RING_3,
  175. DMA_INDEX2RING_4,
  176. DMA_INDEX2RING_5,
  177. DMA_INDEX2RING_6,
  178. DMA_INDEX2RING_7,
  179. };
  180. static const u8 bcmgenet_dma_regs_v3plus[] = {
  181. [DMA_RING_CFG] = 0x00,
  182. [DMA_CTRL] = 0x04,
  183. [DMA_STATUS] = 0x08,
  184. [DMA_SCB_BURST_SIZE] = 0x0C,
  185. [DMA_ARB_CTRL] = 0x2C,
  186. [DMA_PRIORITY_0] = 0x30,
  187. [DMA_PRIORITY_1] = 0x34,
  188. [DMA_PRIORITY_2] = 0x38,
  189. [DMA_INDEX2RING_0] = 0x70,
  190. [DMA_INDEX2RING_1] = 0x74,
  191. [DMA_INDEX2RING_2] = 0x78,
  192. [DMA_INDEX2RING_3] = 0x7C,
  193. [DMA_INDEX2RING_4] = 0x80,
  194. [DMA_INDEX2RING_5] = 0x84,
  195. [DMA_INDEX2RING_6] = 0x88,
  196. [DMA_INDEX2RING_7] = 0x8C,
  197. };
  198. static const u8 bcmgenet_dma_regs_v2[] = {
  199. [DMA_RING_CFG] = 0x00,
  200. [DMA_CTRL] = 0x04,
  201. [DMA_STATUS] = 0x08,
  202. [DMA_SCB_BURST_SIZE] = 0x0C,
  203. [DMA_ARB_CTRL] = 0x30,
  204. [DMA_PRIORITY_0] = 0x34,
  205. [DMA_PRIORITY_1] = 0x38,
  206. [DMA_PRIORITY_2] = 0x3C,
  207. };
  208. static const u8 bcmgenet_dma_regs_v1[] = {
  209. [DMA_CTRL] = 0x00,
  210. [DMA_STATUS] = 0x04,
  211. [DMA_SCB_BURST_SIZE] = 0x0C,
  212. [DMA_ARB_CTRL] = 0x30,
  213. [DMA_PRIORITY_0] = 0x34,
  214. [DMA_PRIORITY_1] = 0x38,
  215. [DMA_PRIORITY_2] = 0x3C,
  216. };
  217. /* Set at runtime once bcmgenet version is known */
  218. static const u8 *bcmgenet_dma_regs;
  219. static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
  220. {
  221. return netdev_priv(dev_get_drvdata(dev));
  222. }
  223. static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
  224. enum dma_reg r)
  225. {
  226. return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
  227. DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
  228. }
  229. static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
  230. u32 val, enum dma_reg r)
  231. {
  232. __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
  233. DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
  234. }
  235. static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
  236. enum dma_reg r)
  237. {
  238. return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
  239. DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
  240. }
  241. static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
  242. u32 val, enum dma_reg r)
  243. {
  244. __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
  245. DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
  246. }
  247. /* RDMA/TDMA ring registers and accessors
  248. * we merge the common fields and just prefix with T/D the registers
  249. * having different meaning depending on the direction
  250. */
  251. enum dma_ring_reg {
  252. TDMA_READ_PTR = 0,
  253. RDMA_WRITE_PTR = TDMA_READ_PTR,
  254. TDMA_READ_PTR_HI,
  255. RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
  256. TDMA_CONS_INDEX,
  257. RDMA_PROD_INDEX = TDMA_CONS_INDEX,
  258. TDMA_PROD_INDEX,
  259. RDMA_CONS_INDEX = TDMA_PROD_INDEX,
  260. DMA_RING_BUF_SIZE,
  261. DMA_START_ADDR,
  262. DMA_START_ADDR_HI,
  263. DMA_END_ADDR,
  264. DMA_END_ADDR_HI,
  265. DMA_MBUF_DONE_THRESH,
  266. TDMA_FLOW_PERIOD,
  267. RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
  268. TDMA_WRITE_PTR,
  269. RDMA_READ_PTR = TDMA_WRITE_PTR,
  270. TDMA_WRITE_PTR_HI,
  271. RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
  272. };
  273. /* GENET v4 supports 40-bits pointer addressing
  274. * for obvious reasons the LO and HI word parts
  275. * are contiguous, but this offsets the other
  276. * registers.
  277. */
  278. static const u8 genet_dma_ring_regs_v4[] = {
  279. [TDMA_READ_PTR] = 0x00,
  280. [TDMA_READ_PTR_HI] = 0x04,
  281. [TDMA_CONS_INDEX] = 0x08,
  282. [TDMA_PROD_INDEX] = 0x0C,
  283. [DMA_RING_BUF_SIZE] = 0x10,
  284. [DMA_START_ADDR] = 0x14,
  285. [DMA_START_ADDR_HI] = 0x18,
  286. [DMA_END_ADDR] = 0x1C,
  287. [DMA_END_ADDR_HI] = 0x20,
  288. [DMA_MBUF_DONE_THRESH] = 0x24,
  289. [TDMA_FLOW_PERIOD] = 0x28,
  290. [TDMA_WRITE_PTR] = 0x2C,
  291. [TDMA_WRITE_PTR_HI] = 0x30,
  292. };
  293. static const u8 genet_dma_ring_regs_v123[] = {
  294. [TDMA_READ_PTR] = 0x00,
  295. [TDMA_CONS_INDEX] = 0x04,
  296. [TDMA_PROD_INDEX] = 0x08,
  297. [DMA_RING_BUF_SIZE] = 0x0C,
  298. [DMA_START_ADDR] = 0x10,
  299. [DMA_END_ADDR] = 0x14,
  300. [DMA_MBUF_DONE_THRESH] = 0x18,
  301. [TDMA_FLOW_PERIOD] = 0x1C,
  302. [TDMA_WRITE_PTR] = 0x20,
  303. };
  304. /* Set at runtime once GENET version is known */
  305. static const u8 *genet_dma_ring_regs;
  306. static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
  307. unsigned int ring,
  308. enum dma_ring_reg r)
  309. {
  310. return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
  311. (DMA_RING_SIZE * ring) +
  312. genet_dma_ring_regs[r]);
  313. }
  314. static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
  315. unsigned int ring, u32 val,
  316. enum dma_ring_reg r)
  317. {
  318. __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
  319. (DMA_RING_SIZE * ring) +
  320. genet_dma_ring_regs[r]);
  321. }
  322. static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv,
  323. unsigned int ring,
  324. enum dma_ring_reg r)
  325. {
  326. return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
  327. (DMA_RING_SIZE * ring) +
  328. genet_dma_ring_regs[r]);
  329. }
  330. static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv,
  331. unsigned int ring, u32 val,
  332. enum dma_ring_reg r)
  333. {
  334. __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
  335. (DMA_RING_SIZE * ring) +
  336. genet_dma_ring_regs[r]);
  337. }
  338. static int bcmgenet_get_settings(struct net_device *dev,
  339. struct ethtool_cmd *cmd)
  340. {
  341. struct bcmgenet_priv *priv = netdev_priv(dev);
  342. if (!netif_running(dev))
  343. return -EINVAL;
  344. if (!priv->phydev)
  345. return -ENODEV;
  346. return phy_ethtool_gset(priv->phydev, cmd);
  347. }
  348. static int bcmgenet_set_settings(struct net_device *dev,
  349. struct ethtool_cmd *cmd)
  350. {
  351. struct bcmgenet_priv *priv = netdev_priv(dev);
  352. if (!netif_running(dev))
  353. return -EINVAL;
  354. if (!priv->phydev)
  355. return -ENODEV;
  356. return phy_ethtool_sset(priv->phydev, cmd);
  357. }
  358. static int bcmgenet_set_rx_csum(struct net_device *dev,
  359. netdev_features_t wanted)
  360. {
  361. struct bcmgenet_priv *priv = netdev_priv(dev);
  362. u32 rbuf_chk_ctrl;
  363. bool rx_csum_en;
  364. rx_csum_en = !!(wanted & NETIF_F_RXCSUM);
  365. rbuf_chk_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
  366. /* enable rx checksumming */
  367. if (rx_csum_en)
  368. rbuf_chk_ctrl |= RBUF_RXCHK_EN;
  369. else
  370. rbuf_chk_ctrl &= ~RBUF_RXCHK_EN;
  371. priv->desc_rxchk_en = rx_csum_en;
  372. /* If UniMAC forwards CRC, we need to skip over it to get
  373. * a valid CHK bit to be set in the per-packet status word
  374. */
  375. if (rx_csum_en && priv->crc_fwd_en)
  376. rbuf_chk_ctrl |= RBUF_SKIP_FCS;
  377. else
  378. rbuf_chk_ctrl &= ~RBUF_SKIP_FCS;
  379. bcmgenet_rbuf_writel(priv, rbuf_chk_ctrl, RBUF_CHK_CTRL);
  380. return 0;
  381. }
  382. static int bcmgenet_set_tx_csum(struct net_device *dev,
  383. netdev_features_t wanted)
  384. {
  385. struct bcmgenet_priv *priv = netdev_priv(dev);
  386. bool desc_64b_en;
  387. u32 tbuf_ctrl, rbuf_ctrl;
  388. tbuf_ctrl = bcmgenet_tbuf_ctrl_get(priv);
  389. rbuf_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
  390. desc_64b_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
  391. /* enable 64 bytes descriptor in both directions (RBUF and TBUF) */
  392. if (desc_64b_en) {
  393. tbuf_ctrl |= RBUF_64B_EN;
  394. rbuf_ctrl |= RBUF_64B_EN;
  395. } else {
  396. tbuf_ctrl &= ~RBUF_64B_EN;
  397. rbuf_ctrl &= ~RBUF_64B_EN;
  398. }
  399. priv->desc_64b_en = desc_64b_en;
  400. bcmgenet_tbuf_ctrl_set(priv, tbuf_ctrl);
  401. bcmgenet_rbuf_writel(priv, rbuf_ctrl, RBUF_CTRL);
  402. return 0;
  403. }
  404. static int bcmgenet_set_features(struct net_device *dev,
  405. netdev_features_t features)
  406. {
  407. netdev_features_t changed = features ^ dev->features;
  408. netdev_features_t wanted = dev->wanted_features;
  409. int ret = 0;
  410. if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
  411. ret = bcmgenet_set_tx_csum(dev, wanted);
  412. if (changed & (NETIF_F_RXCSUM))
  413. ret = bcmgenet_set_rx_csum(dev, wanted);
  414. return ret;
  415. }
  416. static u32 bcmgenet_get_msglevel(struct net_device *dev)
  417. {
  418. struct bcmgenet_priv *priv = netdev_priv(dev);
  419. return priv->msg_enable;
  420. }
  421. static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
  422. {
  423. struct bcmgenet_priv *priv = netdev_priv(dev);
  424. priv->msg_enable = level;
  425. }
  426. /* standard ethtool support functions. */
  427. enum bcmgenet_stat_type {
  428. BCMGENET_STAT_NETDEV = -1,
  429. BCMGENET_STAT_MIB_RX,
  430. BCMGENET_STAT_MIB_TX,
  431. BCMGENET_STAT_RUNT,
  432. BCMGENET_STAT_MISC,
  433. BCMGENET_STAT_SOFT,
  434. };
  435. struct bcmgenet_stats {
  436. char stat_string[ETH_GSTRING_LEN];
  437. int stat_sizeof;
  438. int stat_offset;
  439. enum bcmgenet_stat_type type;
  440. /* reg offset from UMAC base for misc counters */
  441. u16 reg_offset;
  442. };
  443. #define STAT_NETDEV(m) { \
  444. .stat_string = __stringify(m), \
  445. .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
  446. .stat_offset = offsetof(struct net_device_stats, m), \
  447. .type = BCMGENET_STAT_NETDEV, \
  448. }
  449. #define STAT_GENET_MIB(str, m, _type) { \
  450. .stat_string = str, \
  451. .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
  452. .stat_offset = offsetof(struct bcmgenet_priv, m), \
  453. .type = _type, \
  454. }
  455. #define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
  456. #define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
  457. #define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
  458. #define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
  459. #define STAT_GENET_MISC(str, m, offset) { \
  460. .stat_string = str, \
  461. .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
  462. .stat_offset = offsetof(struct bcmgenet_priv, m), \
  463. .type = BCMGENET_STAT_MISC, \
  464. .reg_offset = offset, \
  465. }
  466. /* There is a 0xC gap between the end of RX and beginning of TX stats and then
  467. * between the end of TX stats and the beginning of the RX RUNT
  468. */
  469. #define BCMGENET_STAT_OFFSET 0xc
  470. /* Hardware counters must be kept in sync because the order/offset
  471. * is important here (order in structure declaration = order in hardware)
  472. */
  473. static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
  474. /* general stats */
  475. STAT_NETDEV(rx_packets),
  476. STAT_NETDEV(tx_packets),
  477. STAT_NETDEV(rx_bytes),
  478. STAT_NETDEV(tx_bytes),
  479. STAT_NETDEV(rx_errors),
  480. STAT_NETDEV(tx_errors),
  481. STAT_NETDEV(rx_dropped),
  482. STAT_NETDEV(tx_dropped),
  483. STAT_NETDEV(multicast),
  484. /* UniMAC RSV counters */
  485. STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
  486. STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
  487. STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
  488. STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
  489. STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
  490. STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
  491. STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
  492. STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
  493. STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
  494. STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
  495. STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
  496. STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
  497. STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
  498. STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
  499. STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
  500. STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
  501. STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
  502. STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
  503. STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
  504. STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
  505. STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
  506. STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
  507. STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
  508. STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
  509. STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
  510. STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
  511. STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
  512. STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
  513. STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
  514. /* UniMAC TSV counters */
  515. STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
  516. STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
  517. STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
  518. STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
  519. STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
  520. STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
  521. STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
  522. STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
  523. STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
  524. STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
  525. STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
  526. STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
  527. STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
  528. STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
  529. STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
  530. STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
  531. STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
  532. STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
  533. STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
  534. STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
  535. STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
  536. STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
  537. STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
  538. STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
  539. STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
  540. STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
  541. STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
  542. STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
  543. STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
  544. /* UniMAC RUNT counters */
  545. STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
  546. STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
  547. STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
  548. STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
  549. /* Misc UniMAC counters */
  550. STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
  551. UMAC_RBUF_OVFL_CNT),
  552. STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt, UMAC_RBUF_ERR_CNT),
  553. STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
  554. STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
  555. STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
  556. STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
  557. };
  558. #define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
  559. static void bcmgenet_get_drvinfo(struct net_device *dev,
  560. struct ethtool_drvinfo *info)
  561. {
  562. strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
  563. strlcpy(info->version, "v2.0", sizeof(info->version));
  564. info->n_stats = BCMGENET_STATS_LEN;
  565. }
  566. static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
  567. {
  568. switch (string_set) {
  569. case ETH_SS_STATS:
  570. return BCMGENET_STATS_LEN;
  571. default:
  572. return -EOPNOTSUPP;
  573. }
  574. }
  575. static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
  576. u8 *data)
  577. {
  578. int i;
  579. switch (stringset) {
  580. case ETH_SS_STATS:
  581. for (i = 0; i < BCMGENET_STATS_LEN; i++) {
  582. memcpy(data + i * ETH_GSTRING_LEN,
  583. bcmgenet_gstrings_stats[i].stat_string,
  584. ETH_GSTRING_LEN);
  585. }
  586. break;
  587. }
  588. }
  589. static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
  590. {
  591. int i, j = 0;
  592. for (i = 0; i < BCMGENET_STATS_LEN; i++) {
  593. const struct bcmgenet_stats *s;
  594. u8 offset = 0;
  595. u32 val = 0;
  596. char *p;
  597. s = &bcmgenet_gstrings_stats[i];
  598. switch (s->type) {
  599. case BCMGENET_STAT_NETDEV:
  600. case BCMGENET_STAT_SOFT:
  601. continue;
  602. case BCMGENET_STAT_MIB_RX:
  603. case BCMGENET_STAT_MIB_TX:
  604. case BCMGENET_STAT_RUNT:
  605. if (s->type != BCMGENET_STAT_MIB_RX)
  606. offset = BCMGENET_STAT_OFFSET;
  607. val = bcmgenet_umac_readl(priv,
  608. UMAC_MIB_START + j + offset);
  609. break;
  610. case BCMGENET_STAT_MISC:
  611. val = bcmgenet_umac_readl(priv, s->reg_offset);
  612. /* clear if overflowed */
  613. if (val == ~0)
  614. bcmgenet_umac_writel(priv, 0, s->reg_offset);
  615. break;
  616. }
  617. j += s->stat_sizeof;
  618. p = (char *)priv + s->stat_offset;
  619. *(u32 *)p = val;
  620. }
  621. }
  622. static void bcmgenet_get_ethtool_stats(struct net_device *dev,
  623. struct ethtool_stats *stats,
  624. u64 *data)
  625. {
  626. struct bcmgenet_priv *priv = netdev_priv(dev);
  627. int i;
  628. if (netif_running(dev))
  629. bcmgenet_update_mib_counters(priv);
  630. for (i = 0; i < BCMGENET_STATS_LEN; i++) {
  631. const struct bcmgenet_stats *s;
  632. char *p;
  633. s = &bcmgenet_gstrings_stats[i];
  634. if (s->type == BCMGENET_STAT_NETDEV)
  635. p = (char *)&dev->stats;
  636. else
  637. p = (char *)priv;
  638. p += s->stat_offset;
  639. data[i] = *(u32 *)p;
  640. }
  641. }
  642. static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
  643. {
  644. struct bcmgenet_priv *priv = netdev_priv(dev);
  645. u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
  646. u32 reg;
  647. if (enable && !priv->clk_eee_enabled) {
  648. clk_prepare_enable(priv->clk_eee);
  649. priv->clk_eee_enabled = true;
  650. }
  651. reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
  652. if (enable)
  653. reg |= EEE_EN;
  654. else
  655. reg &= ~EEE_EN;
  656. bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
  657. /* Enable EEE and switch to a 27Mhz clock automatically */
  658. reg = __raw_readl(priv->base + off);
  659. if (enable)
  660. reg |= TBUF_EEE_EN | TBUF_PM_EN;
  661. else
  662. reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
  663. __raw_writel(reg, priv->base + off);
  664. /* Do the same for thing for RBUF */
  665. reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
  666. if (enable)
  667. reg |= RBUF_EEE_EN | RBUF_PM_EN;
  668. else
  669. reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
  670. bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
  671. if (!enable && priv->clk_eee_enabled) {
  672. clk_disable_unprepare(priv->clk_eee);
  673. priv->clk_eee_enabled = false;
  674. }
  675. priv->eee.eee_enabled = enable;
  676. priv->eee.eee_active = enable;
  677. }
  678. static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
  679. {
  680. struct bcmgenet_priv *priv = netdev_priv(dev);
  681. struct ethtool_eee *p = &priv->eee;
  682. if (GENET_IS_V1(priv))
  683. return -EOPNOTSUPP;
  684. e->eee_enabled = p->eee_enabled;
  685. e->eee_active = p->eee_active;
  686. e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
  687. return phy_ethtool_get_eee(priv->phydev, e);
  688. }
  689. static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
  690. {
  691. struct bcmgenet_priv *priv = netdev_priv(dev);
  692. struct ethtool_eee *p = &priv->eee;
  693. int ret = 0;
  694. if (GENET_IS_V1(priv))
  695. return -EOPNOTSUPP;
  696. p->eee_enabled = e->eee_enabled;
  697. if (!p->eee_enabled) {
  698. bcmgenet_eee_enable_set(dev, false);
  699. } else {
  700. ret = phy_init_eee(priv->phydev, 0);
  701. if (ret) {
  702. netif_err(priv, hw, dev, "EEE initialization failed\n");
  703. return ret;
  704. }
  705. bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
  706. bcmgenet_eee_enable_set(dev, true);
  707. }
  708. return phy_ethtool_set_eee(priv->phydev, e);
  709. }
  710. static int bcmgenet_nway_reset(struct net_device *dev)
  711. {
  712. struct bcmgenet_priv *priv = netdev_priv(dev);
  713. return genphy_restart_aneg(priv->phydev);
  714. }
  715. /* standard ethtool support functions. */
  716. static struct ethtool_ops bcmgenet_ethtool_ops = {
  717. .get_strings = bcmgenet_get_strings,
  718. .get_sset_count = bcmgenet_get_sset_count,
  719. .get_ethtool_stats = bcmgenet_get_ethtool_stats,
  720. .get_settings = bcmgenet_get_settings,
  721. .set_settings = bcmgenet_set_settings,
  722. .get_drvinfo = bcmgenet_get_drvinfo,
  723. .get_link = ethtool_op_get_link,
  724. .get_msglevel = bcmgenet_get_msglevel,
  725. .set_msglevel = bcmgenet_set_msglevel,
  726. .get_wol = bcmgenet_get_wol,
  727. .set_wol = bcmgenet_set_wol,
  728. .get_eee = bcmgenet_get_eee,
  729. .set_eee = bcmgenet_set_eee,
  730. .nway_reset = bcmgenet_nway_reset,
  731. };
  732. /* Power down the unimac, based on mode. */
  733. static int bcmgenet_power_down(struct bcmgenet_priv *priv,
  734. enum bcmgenet_power_mode mode)
  735. {
  736. int ret = 0;
  737. u32 reg;
  738. switch (mode) {
  739. case GENET_POWER_CABLE_SENSE:
  740. phy_detach(priv->phydev);
  741. break;
  742. case GENET_POWER_WOL_MAGIC:
  743. ret = bcmgenet_wol_power_down_cfg(priv, mode);
  744. break;
  745. case GENET_POWER_PASSIVE:
  746. /* Power down LED */
  747. if (priv->hw_params->flags & GENET_HAS_EXT) {
  748. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  749. reg |= (EXT_PWR_DOWN_PHY |
  750. EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
  751. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  752. bcmgenet_phy_power_set(priv->dev, false);
  753. }
  754. break;
  755. default:
  756. break;
  757. }
  758. return 0;
  759. }
  760. static void bcmgenet_power_up(struct bcmgenet_priv *priv,
  761. enum bcmgenet_power_mode mode)
  762. {
  763. u32 reg;
  764. if (!(priv->hw_params->flags & GENET_HAS_EXT))
  765. return;
  766. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  767. switch (mode) {
  768. case GENET_POWER_PASSIVE:
  769. reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_PHY |
  770. EXT_PWR_DOWN_BIAS);
  771. /* fallthrough */
  772. case GENET_POWER_CABLE_SENSE:
  773. /* enable APD */
  774. reg |= EXT_PWR_DN_EN_LD;
  775. break;
  776. case GENET_POWER_WOL_MAGIC:
  777. bcmgenet_wol_power_up_cfg(priv, mode);
  778. return;
  779. default:
  780. break;
  781. }
  782. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  783. if (mode == GENET_POWER_PASSIVE)
  784. bcmgenet_mii_reset(priv->dev);
  785. }
  786. /* ioctl handle special commands that are not present in ethtool. */
  787. static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  788. {
  789. struct bcmgenet_priv *priv = netdev_priv(dev);
  790. int val = 0;
  791. if (!netif_running(dev))
  792. return -EINVAL;
  793. switch (cmd) {
  794. case SIOCGMIIPHY:
  795. case SIOCGMIIREG:
  796. case SIOCSMIIREG:
  797. if (!priv->phydev)
  798. val = -ENODEV;
  799. else
  800. val = phy_mii_ioctl(priv->phydev, rq, cmd);
  801. break;
  802. default:
  803. val = -EINVAL;
  804. break;
  805. }
  806. return val;
  807. }
  808. static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
  809. struct bcmgenet_tx_ring *ring)
  810. {
  811. struct enet_cb *tx_cb_ptr;
  812. tx_cb_ptr = ring->cbs;
  813. tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
  814. /* Advancing local write pointer */
  815. if (ring->write_ptr == ring->end_ptr)
  816. ring->write_ptr = ring->cb_ptr;
  817. else
  818. ring->write_ptr++;
  819. return tx_cb_ptr;
  820. }
  821. /* Simple helper to free a control block's resources */
  822. static void bcmgenet_free_cb(struct enet_cb *cb)
  823. {
  824. dev_kfree_skb_any(cb->skb);
  825. cb->skb = NULL;
  826. dma_unmap_addr_set(cb, dma_addr, 0);
  827. }
  828. static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
  829. {
  830. bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
  831. INTRL2_CPU_MASK_SET);
  832. }
  833. static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
  834. {
  835. bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
  836. INTRL2_CPU_MASK_CLEAR);
  837. }
  838. static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
  839. {
  840. bcmgenet_intrl2_1_writel(ring->priv,
  841. 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
  842. INTRL2_CPU_MASK_SET);
  843. }
  844. static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
  845. {
  846. bcmgenet_intrl2_1_writel(ring->priv,
  847. 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
  848. INTRL2_CPU_MASK_CLEAR);
  849. }
  850. static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
  851. {
  852. bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
  853. INTRL2_CPU_MASK_SET);
  854. }
  855. static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
  856. {
  857. bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
  858. INTRL2_CPU_MASK_CLEAR);
  859. }
  860. static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
  861. {
  862. bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
  863. INTRL2_CPU_MASK_CLEAR);
  864. }
  865. static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
  866. {
  867. bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
  868. INTRL2_CPU_MASK_SET);
  869. }
  870. /* Unlocked version of the reclaim routine */
  871. static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
  872. struct bcmgenet_tx_ring *ring)
  873. {
  874. struct bcmgenet_priv *priv = netdev_priv(dev);
  875. struct enet_cb *tx_cb_ptr;
  876. struct netdev_queue *txq;
  877. unsigned int pkts_compl = 0;
  878. unsigned int c_index;
  879. unsigned int txbds_ready;
  880. unsigned int txbds_processed = 0;
  881. /* Compute how many buffers are transmitted since last xmit call */
  882. c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
  883. c_index &= DMA_C_INDEX_MASK;
  884. if (likely(c_index >= ring->c_index))
  885. txbds_ready = c_index - ring->c_index;
  886. else
  887. txbds_ready = (DMA_C_INDEX_MASK + 1) - ring->c_index + c_index;
  888. netif_dbg(priv, tx_done, dev,
  889. "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
  890. __func__, ring->index, ring->c_index, c_index, txbds_ready);
  891. /* Reclaim transmitted buffers */
  892. while (txbds_processed < txbds_ready) {
  893. tx_cb_ptr = &priv->tx_cbs[ring->clean_ptr];
  894. if (tx_cb_ptr->skb) {
  895. pkts_compl++;
  896. dev->stats.tx_packets++;
  897. dev->stats.tx_bytes += tx_cb_ptr->skb->len;
  898. dma_unmap_single(&dev->dev,
  899. dma_unmap_addr(tx_cb_ptr, dma_addr),
  900. tx_cb_ptr->skb->len,
  901. DMA_TO_DEVICE);
  902. bcmgenet_free_cb(tx_cb_ptr);
  903. } else if (dma_unmap_addr(tx_cb_ptr, dma_addr)) {
  904. dev->stats.tx_bytes +=
  905. dma_unmap_len(tx_cb_ptr, dma_len);
  906. dma_unmap_page(&dev->dev,
  907. dma_unmap_addr(tx_cb_ptr, dma_addr),
  908. dma_unmap_len(tx_cb_ptr, dma_len),
  909. DMA_TO_DEVICE);
  910. dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
  911. }
  912. txbds_processed++;
  913. if (likely(ring->clean_ptr < ring->end_ptr))
  914. ring->clean_ptr++;
  915. else
  916. ring->clean_ptr = ring->cb_ptr;
  917. }
  918. ring->free_bds += txbds_processed;
  919. ring->c_index = (ring->c_index + txbds_processed) & DMA_C_INDEX_MASK;
  920. if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
  921. txq = netdev_get_tx_queue(dev, ring->queue);
  922. if (netif_tx_queue_stopped(txq))
  923. netif_tx_wake_queue(txq);
  924. }
  925. return pkts_compl;
  926. }
  927. static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
  928. struct bcmgenet_tx_ring *ring)
  929. {
  930. unsigned int released;
  931. unsigned long flags;
  932. spin_lock_irqsave(&ring->lock, flags);
  933. released = __bcmgenet_tx_reclaim(dev, ring);
  934. spin_unlock_irqrestore(&ring->lock, flags);
  935. return released;
  936. }
  937. static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
  938. {
  939. struct bcmgenet_tx_ring *ring =
  940. container_of(napi, struct bcmgenet_tx_ring, napi);
  941. unsigned int work_done = 0;
  942. work_done = bcmgenet_tx_reclaim(ring->priv->dev, ring);
  943. if (work_done == 0) {
  944. napi_complete(napi);
  945. ring->int_enable(ring);
  946. return 0;
  947. }
  948. return budget;
  949. }
  950. static void bcmgenet_tx_reclaim_all(struct net_device *dev)
  951. {
  952. struct bcmgenet_priv *priv = netdev_priv(dev);
  953. int i;
  954. if (netif_is_multiqueue(dev)) {
  955. for (i = 0; i < priv->hw_params->tx_queues; i++)
  956. bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
  957. }
  958. bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
  959. }
  960. /* Transmits a single SKB (either head of a fragment or a single SKB)
  961. * caller must hold priv->lock
  962. */
  963. static int bcmgenet_xmit_single(struct net_device *dev,
  964. struct sk_buff *skb,
  965. u16 dma_desc_flags,
  966. struct bcmgenet_tx_ring *ring)
  967. {
  968. struct bcmgenet_priv *priv = netdev_priv(dev);
  969. struct device *kdev = &priv->pdev->dev;
  970. struct enet_cb *tx_cb_ptr;
  971. unsigned int skb_len;
  972. dma_addr_t mapping;
  973. u32 length_status;
  974. int ret;
  975. tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
  976. if (unlikely(!tx_cb_ptr))
  977. BUG();
  978. tx_cb_ptr->skb = skb;
  979. skb_len = skb_headlen(skb) < ETH_ZLEN ? ETH_ZLEN : skb_headlen(skb);
  980. mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
  981. ret = dma_mapping_error(kdev, mapping);
  982. if (ret) {
  983. priv->mib.tx_dma_failed++;
  984. netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
  985. dev_kfree_skb(skb);
  986. return ret;
  987. }
  988. dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
  989. dma_unmap_len_set(tx_cb_ptr, dma_len, skb->len);
  990. length_status = (skb_len << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
  991. (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT) |
  992. DMA_TX_APPEND_CRC;
  993. if (skb->ip_summed == CHECKSUM_PARTIAL)
  994. length_status |= DMA_TX_DO_CSUM;
  995. dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, length_status);
  996. return 0;
  997. }
  998. /* Transmit a SKB fragment */
  999. static int bcmgenet_xmit_frag(struct net_device *dev,
  1000. skb_frag_t *frag,
  1001. u16 dma_desc_flags,
  1002. struct bcmgenet_tx_ring *ring)
  1003. {
  1004. struct bcmgenet_priv *priv = netdev_priv(dev);
  1005. struct device *kdev = &priv->pdev->dev;
  1006. struct enet_cb *tx_cb_ptr;
  1007. dma_addr_t mapping;
  1008. int ret;
  1009. tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
  1010. if (unlikely(!tx_cb_ptr))
  1011. BUG();
  1012. tx_cb_ptr->skb = NULL;
  1013. mapping = skb_frag_dma_map(kdev, frag, 0,
  1014. skb_frag_size(frag), DMA_TO_DEVICE);
  1015. ret = dma_mapping_error(kdev, mapping);
  1016. if (ret) {
  1017. priv->mib.tx_dma_failed++;
  1018. netif_err(priv, tx_err, dev, "%s: Tx DMA map failed\n",
  1019. __func__);
  1020. return ret;
  1021. }
  1022. dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
  1023. dma_unmap_len_set(tx_cb_ptr, dma_len, frag->size);
  1024. dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping,
  1025. (frag->size << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
  1026. (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT));
  1027. return 0;
  1028. }
  1029. /* Reallocate the SKB to put enough headroom in front of it and insert
  1030. * the transmit checksum offsets in the descriptors
  1031. */
  1032. static struct sk_buff *bcmgenet_put_tx_csum(struct net_device *dev,
  1033. struct sk_buff *skb)
  1034. {
  1035. struct status_64 *status = NULL;
  1036. struct sk_buff *new_skb;
  1037. u16 offset;
  1038. u8 ip_proto;
  1039. u16 ip_ver;
  1040. u32 tx_csum_info;
  1041. if (unlikely(skb_headroom(skb) < sizeof(*status))) {
  1042. /* If 64 byte status block enabled, must make sure skb has
  1043. * enough headroom for us to insert 64B status block.
  1044. */
  1045. new_skb = skb_realloc_headroom(skb, sizeof(*status));
  1046. dev_kfree_skb(skb);
  1047. if (!new_skb) {
  1048. dev->stats.tx_errors++;
  1049. dev->stats.tx_dropped++;
  1050. return NULL;
  1051. }
  1052. skb = new_skb;
  1053. }
  1054. skb_push(skb, sizeof(*status));
  1055. status = (struct status_64 *)skb->data;
  1056. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1057. ip_ver = htons(skb->protocol);
  1058. switch (ip_ver) {
  1059. case ETH_P_IP:
  1060. ip_proto = ip_hdr(skb)->protocol;
  1061. break;
  1062. case ETH_P_IPV6:
  1063. ip_proto = ipv6_hdr(skb)->nexthdr;
  1064. break;
  1065. default:
  1066. return skb;
  1067. }
  1068. offset = skb_checksum_start_offset(skb) - sizeof(*status);
  1069. tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
  1070. (offset + skb->csum_offset);
  1071. /* Set the length valid bit for TCP and UDP and just set
  1072. * the special UDP flag for IPv4, else just set to 0.
  1073. */
  1074. if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
  1075. tx_csum_info |= STATUS_TX_CSUM_LV;
  1076. if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
  1077. tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
  1078. } else {
  1079. tx_csum_info = 0;
  1080. }
  1081. status->tx_csum_info = tx_csum_info;
  1082. }
  1083. return skb;
  1084. }
  1085. static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
  1086. {
  1087. struct bcmgenet_priv *priv = netdev_priv(dev);
  1088. struct bcmgenet_tx_ring *ring = NULL;
  1089. struct netdev_queue *txq;
  1090. unsigned long flags = 0;
  1091. int nr_frags, index;
  1092. u16 dma_desc_flags;
  1093. int ret;
  1094. int i;
  1095. index = skb_get_queue_mapping(skb);
  1096. /* Mapping strategy:
  1097. * queue_mapping = 0, unclassified, packet xmited through ring16
  1098. * queue_mapping = 1, goes to ring 0. (highest priority queue
  1099. * queue_mapping = 2, goes to ring 1.
  1100. * queue_mapping = 3, goes to ring 2.
  1101. * queue_mapping = 4, goes to ring 3.
  1102. */
  1103. if (index == 0)
  1104. index = DESC_INDEX;
  1105. else
  1106. index -= 1;
  1107. nr_frags = skb_shinfo(skb)->nr_frags;
  1108. ring = &priv->tx_rings[index];
  1109. txq = netdev_get_tx_queue(dev, ring->queue);
  1110. spin_lock_irqsave(&ring->lock, flags);
  1111. if (ring->free_bds <= nr_frags + 1) {
  1112. netif_tx_stop_queue(txq);
  1113. netdev_err(dev, "%s: tx ring %d full when queue %d awake\n",
  1114. __func__, index, ring->queue);
  1115. ret = NETDEV_TX_BUSY;
  1116. goto out;
  1117. }
  1118. if (skb_padto(skb, ETH_ZLEN)) {
  1119. ret = NETDEV_TX_OK;
  1120. goto out;
  1121. }
  1122. /* set the SKB transmit checksum */
  1123. if (priv->desc_64b_en) {
  1124. skb = bcmgenet_put_tx_csum(dev, skb);
  1125. if (!skb) {
  1126. ret = NETDEV_TX_OK;
  1127. goto out;
  1128. }
  1129. }
  1130. dma_desc_flags = DMA_SOP;
  1131. if (nr_frags == 0)
  1132. dma_desc_flags |= DMA_EOP;
  1133. /* Transmit single SKB or head of fragment list */
  1134. ret = bcmgenet_xmit_single(dev, skb, dma_desc_flags, ring);
  1135. if (ret) {
  1136. ret = NETDEV_TX_OK;
  1137. goto out;
  1138. }
  1139. /* xmit fragment */
  1140. for (i = 0; i < nr_frags; i++) {
  1141. ret = bcmgenet_xmit_frag(dev,
  1142. &skb_shinfo(skb)->frags[i],
  1143. (i == nr_frags - 1) ? DMA_EOP : 0,
  1144. ring);
  1145. if (ret) {
  1146. ret = NETDEV_TX_OK;
  1147. goto out;
  1148. }
  1149. }
  1150. skb_tx_timestamp(skb);
  1151. /* Decrement total BD count and advance our write pointer */
  1152. ring->free_bds -= nr_frags + 1;
  1153. ring->prod_index += nr_frags + 1;
  1154. ring->prod_index &= DMA_P_INDEX_MASK;
  1155. if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
  1156. netif_tx_stop_queue(txq);
  1157. if (!skb->xmit_more || netif_xmit_stopped(txq))
  1158. /* Packets are ready, update producer index */
  1159. bcmgenet_tdma_ring_writel(priv, ring->index,
  1160. ring->prod_index, TDMA_PROD_INDEX);
  1161. out:
  1162. spin_unlock_irqrestore(&ring->lock, flags);
  1163. return ret;
  1164. }
  1165. static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
  1166. struct enet_cb *cb)
  1167. {
  1168. struct device *kdev = &priv->pdev->dev;
  1169. struct sk_buff *skb;
  1170. struct sk_buff *rx_skb;
  1171. dma_addr_t mapping;
  1172. /* Allocate a new Rx skb */
  1173. skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
  1174. if (!skb) {
  1175. priv->mib.alloc_rx_buff_failed++;
  1176. netif_err(priv, rx_err, priv->dev,
  1177. "%s: Rx skb allocation failed\n", __func__);
  1178. return NULL;
  1179. }
  1180. /* DMA-map the new Rx skb */
  1181. mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
  1182. DMA_FROM_DEVICE);
  1183. if (dma_mapping_error(kdev, mapping)) {
  1184. priv->mib.rx_dma_failed++;
  1185. dev_kfree_skb_any(skb);
  1186. netif_err(priv, rx_err, priv->dev,
  1187. "%s: Rx skb DMA mapping failed\n", __func__);
  1188. return NULL;
  1189. }
  1190. /* Grab the current Rx skb from the ring and DMA-unmap it */
  1191. rx_skb = cb->skb;
  1192. if (likely(rx_skb))
  1193. dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
  1194. priv->rx_buf_len, DMA_FROM_DEVICE);
  1195. /* Put the new Rx skb on the ring */
  1196. cb->skb = skb;
  1197. dma_unmap_addr_set(cb, dma_addr, mapping);
  1198. dmadesc_set_addr(priv, cb->bd_addr, mapping);
  1199. /* Return the current Rx skb to caller */
  1200. return rx_skb;
  1201. }
  1202. /* bcmgenet_desc_rx - descriptor based rx process.
  1203. * this could be called from bottom half, or from NAPI polling method.
  1204. */
  1205. static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
  1206. unsigned int budget)
  1207. {
  1208. struct bcmgenet_priv *priv = ring->priv;
  1209. struct net_device *dev = priv->dev;
  1210. struct enet_cb *cb;
  1211. struct sk_buff *skb;
  1212. u32 dma_length_status;
  1213. unsigned long dma_flag;
  1214. int len;
  1215. unsigned int rxpktprocessed = 0, rxpkttoprocess;
  1216. unsigned int p_index;
  1217. unsigned int discards;
  1218. unsigned int chksum_ok = 0;
  1219. p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
  1220. discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
  1221. DMA_P_INDEX_DISCARD_CNT_MASK;
  1222. if (discards > ring->old_discards) {
  1223. discards = discards - ring->old_discards;
  1224. dev->stats.rx_missed_errors += discards;
  1225. dev->stats.rx_errors += discards;
  1226. ring->old_discards += discards;
  1227. /* Clear HW register when we reach 75% of maximum 0xFFFF */
  1228. if (ring->old_discards >= 0xC000) {
  1229. ring->old_discards = 0;
  1230. bcmgenet_rdma_ring_writel(priv, ring->index, 0,
  1231. RDMA_PROD_INDEX);
  1232. }
  1233. }
  1234. p_index &= DMA_P_INDEX_MASK;
  1235. if (likely(p_index >= ring->c_index))
  1236. rxpkttoprocess = p_index - ring->c_index;
  1237. else
  1238. rxpkttoprocess = (DMA_C_INDEX_MASK + 1) - ring->c_index +
  1239. p_index;
  1240. netif_dbg(priv, rx_status, dev,
  1241. "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
  1242. while ((rxpktprocessed < rxpkttoprocess) &&
  1243. (rxpktprocessed < budget)) {
  1244. cb = &priv->rx_cbs[ring->read_ptr];
  1245. skb = bcmgenet_rx_refill(priv, cb);
  1246. if (unlikely(!skb)) {
  1247. dev->stats.rx_dropped++;
  1248. dev->stats.rx_errors++;
  1249. goto next;
  1250. }
  1251. if (!priv->desc_64b_en) {
  1252. dma_length_status =
  1253. dmadesc_get_length_status(priv, cb->bd_addr);
  1254. } else {
  1255. struct status_64 *status;
  1256. status = (struct status_64 *)skb->data;
  1257. dma_length_status = status->length_status;
  1258. }
  1259. /* DMA flags and length are still valid no matter how
  1260. * we got the Receive Status Vector (64B RSB or register)
  1261. */
  1262. dma_flag = dma_length_status & 0xffff;
  1263. len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
  1264. netif_dbg(priv, rx_status, dev,
  1265. "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
  1266. __func__, p_index, ring->c_index,
  1267. ring->read_ptr, dma_length_status);
  1268. if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
  1269. netif_err(priv, rx_status, dev,
  1270. "dropping fragmented packet!\n");
  1271. dev->stats.rx_dropped++;
  1272. dev->stats.rx_errors++;
  1273. dev_kfree_skb_any(skb);
  1274. goto next;
  1275. }
  1276. /* report errors */
  1277. if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
  1278. DMA_RX_OV |
  1279. DMA_RX_NO |
  1280. DMA_RX_LG |
  1281. DMA_RX_RXER))) {
  1282. netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
  1283. (unsigned int)dma_flag);
  1284. if (dma_flag & DMA_RX_CRC_ERROR)
  1285. dev->stats.rx_crc_errors++;
  1286. if (dma_flag & DMA_RX_OV)
  1287. dev->stats.rx_over_errors++;
  1288. if (dma_flag & DMA_RX_NO)
  1289. dev->stats.rx_frame_errors++;
  1290. if (dma_flag & DMA_RX_LG)
  1291. dev->stats.rx_length_errors++;
  1292. dev->stats.rx_dropped++;
  1293. dev->stats.rx_errors++;
  1294. dev_kfree_skb_any(skb);
  1295. goto next;
  1296. } /* error packet */
  1297. chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
  1298. priv->desc_rxchk_en;
  1299. skb_put(skb, len);
  1300. if (priv->desc_64b_en) {
  1301. skb_pull(skb, 64);
  1302. len -= 64;
  1303. }
  1304. if (likely(chksum_ok))
  1305. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1306. /* remove hardware 2bytes added for IP alignment */
  1307. skb_pull(skb, 2);
  1308. len -= 2;
  1309. if (priv->crc_fwd_en) {
  1310. skb_trim(skb, len - ETH_FCS_LEN);
  1311. len -= ETH_FCS_LEN;
  1312. }
  1313. /*Finish setting up the received SKB and send it to the kernel*/
  1314. skb->protocol = eth_type_trans(skb, priv->dev);
  1315. dev->stats.rx_packets++;
  1316. dev->stats.rx_bytes += len;
  1317. if (dma_flag & DMA_RX_MULT)
  1318. dev->stats.multicast++;
  1319. /* Notify kernel */
  1320. napi_gro_receive(&ring->napi, skb);
  1321. netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
  1322. next:
  1323. rxpktprocessed++;
  1324. if (likely(ring->read_ptr < ring->end_ptr))
  1325. ring->read_ptr++;
  1326. else
  1327. ring->read_ptr = ring->cb_ptr;
  1328. ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
  1329. bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
  1330. }
  1331. return rxpktprocessed;
  1332. }
  1333. /* Rx NAPI polling method */
  1334. static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
  1335. {
  1336. struct bcmgenet_rx_ring *ring = container_of(napi,
  1337. struct bcmgenet_rx_ring, napi);
  1338. unsigned int work_done;
  1339. work_done = bcmgenet_desc_rx(ring, budget);
  1340. if (work_done < budget) {
  1341. napi_complete(napi);
  1342. ring->int_enable(ring);
  1343. }
  1344. return work_done;
  1345. }
  1346. /* Assign skb to RX DMA descriptor. */
  1347. static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
  1348. struct bcmgenet_rx_ring *ring)
  1349. {
  1350. struct enet_cb *cb;
  1351. struct sk_buff *skb;
  1352. int i;
  1353. netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
  1354. /* loop here for each buffer needing assign */
  1355. for (i = 0; i < ring->size; i++) {
  1356. cb = ring->cbs + i;
  1357. skb = bcmgenet_rx_refill(priv, cb);
  1358. if (skb)
  1359. dev_kfree_skb_any(skb);
  1360. if (!cb->skb)
  1361. return -ENOMEM;
  1362. }
  1363. return 0;
  1364. }
  1365. static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
  1366. {
  1367. struct enet_cb *cb;
  1368. int i;
  1369. for (i = 0; i < priv->num_rx_bds; i++) {
  1370. cb = &priv->rx_cbs[i];
  1371. if (dma_unmap_addr(cb, dma_addr)) {
  1372. dma_unmap_single(&priv->dev->dev,
  1373. dma_unmap_addr(cb, dma_addr),
  1374. priv->rx_buf_len, DMA_FROM_DEVICE);
  1375. dma_unmap_addr_set(cb, dma_addr, 0);
  1376. }
  1377. if (cb->skb)
  1378. bcmgenet_free_cb(cb);
  1379. }
  1380. }
  1381. static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
  1382. {
  1383. u32 reg;
  1384. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  1385. if (enable)
  1386. reg |= mask;
  1387. else
  1388. reg &= ~mask;
  1389. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  1390. /* UniMAC stops on a packet boundary, wait for a full-size packet
  1391. * to be processed
  1392. */
  1393. if (enable == 0)
  1394. usleep_range(1000, 2000);
  1395. }
  1396. static int reset_umac(struct bcmgenet_priv *priv)
  1397. {
  1398. struct device *kdev = &priv->pdev->dev;
  1399. unsigned int timeout = 0;
  1400. u32 reg;
  1401. /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
  1402. bcmgenet_rbuf_ctrl_set(priv, 0);
  1403. udelay(10);
  1404. /* disable MAC while updating its registers */
  1405. bcmgenet_umac_writel(priv, 0, UMAC_CMD);
  1406. /* issue soft reset, wait for it to complete */
  1407. bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
  1408. while (timeout++ < 1000) {
  1409. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  1410. if (!(reg & CMD_SW_RESET))
  1411. return 0;
  1412. udelay(1);
  1413. }
  1414. if (timeout == 1000) {
  1415. dev_err(kdev,
  1416. "timeout waiting for MAC to come out of reset\n");
  1417. return -ETIMEDOUT;
  1418. }
  1419. return 0;
  1420. }
  1421. static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
  1422. {
  1423. /* Mask all interrupts.*/
  1424. bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
  1425. bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
  1426. bcmgenet_intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
  1427. bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
  1428. bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
  1429. bcmgenet_intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
  1430. }
  1431. static int init_umac(struct bcmgenet_priv *priv)
  1432. {
  1433. struct device *kdev = &priv->pdev->dev;
  1434. int ret;
  1435. u32 reg;
  1436. u32 int0_enable = 0;
  1437. u32 int1_enable = 0;
  1438. int i;
  1439. dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
  1440. ret = reset_umac(priv);
  1441. if (ret)
  1442. return ret;
  1443. bcmgenet_umac_writel(priv, 0, UMAC_CMD);
  1444. /* clear tx/rx counter */
  1445. bcmgenet_umac_writel(priv,
  1446. MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
  1447. UMAC_MIB_CTRL);
  1448. bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
  1449. bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
  1450. /* init rx registers, enable ip header optimization */
  1451. reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
  1452. reg |= RBUF_ALIGN_2B;
  1453. bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
  1454. if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
  1455. bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
  1456. bcmgenet_intr_disable(priv);
  1457. /* Enable Rx default queue 16 interrupts */
  1458. int0_enable |= UMAC_IRQ_RXDMA_DONE;
  1459. /* Enable Tx default queue 16 interrupts */
  1460. int0_enable |= UMAC_IRQ_TXDMA_DONE;
  1461. /* Monitor cable plug/unplugged event for internal PHY */
  1462. if (phy_is_internal(priv->phydev)) {
  1463. int0_enable |= UMAC_IRQ_LINK_EVENT;
  1464. } else if (priv->ext_phy) {
  1465. int0_enable |= UMAC_IRQ_LINK_EVENT;
  1466. } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
  1467. if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
  1468. int0_enable |= UMAC_IRQ_LINK_EVENT;
  1469. reg = bcmgenet_bp_mc_get(priv);
  1470. reg |= BIT(priv->hw_params->bp_in_en_shift);
  1471. /* bp_mask: back pressure mask */
  1472. if (netif_is_multiqueue(priv->dev))
  1473. reg |= priv->hw_params->bp_in_mask;
  1474. else
  1475. reg &= ~priv->hw_params->bp_in_mask;
  1476. bcmgenet_bp_mc_set(priv, reg);
  1477. }
  1478. /* Enable MDIO interrupts on GENET v3+ */
  1479. if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
  1480. int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
  1481. /* Enable Rx priority queue interrupts */
  1482. for (i = 0; i < priv->hw_params->rx_queues; ++i)
  1483. int1_enable |= (1 << (UMAC_IRQ1_RX_INTR_SHIFT + i));
  1484. /* Enable Tx priority queue interrupts */
  1485. for (i = 0; i < priv->hw_params->tx_queues; ++i)
  1486. int1_enable |= (1 << i);
  1487. bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
  1488. bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
  1489. /* Enable rx/tx engine.*/
  1490. dev_dbg(kdev, "done init umac\n");
  1491. return 0;
  1492. }
  1493. /* Initialize a Tx ring along with corresponding hardware registers */
  1494. static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
  1495. unsigned int index, unsigned int size,
  1496. unsigned int start_ptr, unsigned int end_ptr)
  1497. {
  1498. struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
  1499. u32 words_per_bd = WORDS_PER_BD(priv);
  1500. u32 flow_period_val = 0;
  1501. spin_lock_init(&ring->lock);
  1502. ring->priv = priv;
  1503. ring->index = index;
  1504. if (index == DESC_INDEX) {
  1505. ring->queue = 0;
  1506. ring->int_enable = bcmgenet_tx_ring16_int_enable;
  1507. ring->int_disable = bcmgenet_tx_ring16_int_disable;
  1508. } else {
  1509. ring->queue = index + 1;
  1510. ring->int_enable = bcmgenet_tx_ring_int_enable;
  1511. ring->int_disable = bcmgenet_tx_ring_int_disable;
  1512. }
  1513. ring->cbs = priv->tx_cbs + start_ptr;
  1514. ring->size = size;
  1515. ring->clean_ptr = start_ptr;
  1516. ring->c_index = 0;
  1517. ring->free_bds = size;
  1518. ring->write_ptr = start_ptr;
  1519. ring->cb_ptr = start_ptr;
  1520. ring->end_ptr = end_ptr - 1;
  1521. ring->prod_index = 0;
  1522. /* Set flow period for ring != 16 */
  1523. if (index != DESC_INDEX)
  1524. flow_period_val = ENET_MAX_MTU_SIZE << 16;
  1525. bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
  1526. bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
  1527. bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
  1528. /* Disable rate control for now */
  1529. bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
  1530. TDMA_FLOW_PERIOD);
  1531. bcmgenet_tdma_ring_writel(priv, index,
  1532. ((size << DMA_RING_SIZE_SHIFT) |
  1533. RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
  1534. /* Set start and end address, read and write pointers */
  1535. bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1536. DMA_START_ADDR);
  1537. bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1538. TDMA_READ_PTR);
  1539. bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1540. TDMA_WRITE_PTR);
  1541. bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
  1542. DMA_END_ADDR);
  1543. }
  1544. /* Initialize a RDMA ring */
  1545. static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
  1546. unsigned int index, unsigned int size,
  1547. unsigned int start_ptr, unsigned int end_ptr)
  1548. {
  1549. struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
  1550. u32 words_per_bd = WORDS_PER_BD(priv);
  1551. int ret;
  1552. ring->priv = priv;
  1553. ring->index = index;
  1554. if (index == DESC_INDEX) {
  1555. ring->int_enable = bcmgenet_rx_ring16_int_enable;
  1556. ring->int_disable = bcmgenet_rx_ring16_int_disable;
  1557. } else {
  1558. ring->int_enable = bcmgenet_rx_ring_int_enable;
  1559. ring->int_disable = bcmgenet_rx_ring_int_disable;
  1560. }
  1561. ring->cbs = priv->rx_cbs + start_ptr;
  1562. ring->size = size;
  1563. ring->c_index = 0;
  1564. ring->read_ptr = start_ptr;
  1565. ring->cb_ptr = start_ptr;
  1566. ring->end_ptr = end_ptr - 1;
  1567. ret = bcmgenet_alloc_rx_buffers(priv, ring);
  1568. if (ret)
  1569. return ret;
  1570. bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
  1571. bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
  1572. bcmgenet_rdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
  1573. bcmgenet_rdma_ring_writel(priv, index,
  1574. ((size << DMA_RING_SIZE_SHIFT) |
  1575. RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
  1576. bcmgenet_rdma_ring_writel(priv, index,
  1577. (DMA_FC_THRESH_LO <<
  1578. DMA_XOFF_THRESHOLD_SHIFT) |
  1579. DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
  1580. /* Set start and end address, read and write pointers */
  1581. bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1582. DMA_START_ADDR);
  1583. bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1584. RDMA_READ_PTR);
  1585. bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1586. RDMA_WRITE_PTR);
  1587. bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
  1588. DMA_END_ADDR);
  1589. return ret;
  1590. }
  1591. static void bcmgenet_init_tx_napi(struct bcmgenet_priv *priv)
  1592. {
  1593. unsigned int i;
  1594. struct bcmgenet_tx_ring *ring;
  1595. for (i = 0; i < priv->hw_params->tx_queues; ++i) {
  1596. ring = &priv->tx_rings[i];
  1597. netif_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
  1598. }
  1599. ring = &priv->tx_rings[DESC_INDEX];
  1600. netif_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
  1601. }
  1602. static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
  1603. {
  1604. unsigned int i;
  1605. struct bcmgenet_tx_ring *ring;
  1606. for (i = 0; i < priv->hw_params->tx_queues; ++i) {
  1607. ring = &priv->tx_rings[i];
  1608. napi_enable(&ring->napi);
  1609. }
  1610. ring = &priv->tx_rings[DESC_INDEX];
  1611. napi_enable(&ring->napi);
  1612. }
  1613. static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
  1614. {
  1615. unsigned int i;
  1616. struct bcmgenet_tx_ring *ring;
  1617. for (i = 0; i < priv->hw_params->tx_queues; ++i) {
  1618. ring = &priv->tx_rings[i];
  1619. napi_disable(&ring->napi);
  1620. }
  1621. ring = &priv->tx_rings[DESC_INDEX];
  1622. napi_disable(&ring->napi);
  1623. }
  1624. static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
  1625. {
  1626. unsigned int i;
  1627. struct bcmgenet_tx_ring *ring;
  1628. for (i = 0; i < priv->hw_params->tx_queues; ++i) {
  1629. ring = &priv->tx_rings[i];
  1630. netif_napi_del(&ring->napi);
  1631. }
  1632. ring = &priv->tx_rings[DESC_INDEX];
  1633. netif_napi_del(&ring->napi);
  1634. }
  1635. /* Initialize Tx queues
  1636. *
  1637. * Queues 0-3 are priority-based, each one has 32 descriptors,
  1638. * with queue 0 being the highest priority queue.
  1639. *
  1640. * Queue 16 is the default Tx queue with
  1641. * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
  1642. *
  1643. * The transmit control block pool is then partitioned as follows:
  1644. * - Tx queue 0 uses tx_cbs[0..31]
  1645. * - Tx queue 1 uses tx_cbs[32..63]
  1646. * - Tx queue 2 uses tx_cbs[64..95]
  1647. * - Tx queue 3 uses tx_cbs[96..127]
  1648. * - Tx queue 16 uses tx_cbs[128..255]
  1649. */
  1650. static void bcmgenet_init_tx_queues(struct net_device *dev)
  1651. {
  1652. struct bcmgenet_priv *priv = netdev_priv(dev);
  1653. u32 i, dma_enable;
  1654. u32 dma_ctrl, ring_cfg;
  1655. u32 dma_priority[3] = {0, 0, 0};
  1656. dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
  1657. dma_enable = dma_ctrl & DMA_EN;
  1658. dma_ctrl &= ~DMA_EN;
  1659. bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
  1660. dma_ctrl = 0;
  1661. ring_cfg = 0;
  1662. /* Enable strict priority arbiter mode */
  1663. bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
  1664. /* Initialize Tx priority queues */
  1665. for (i = 0; i < priv->hw_params->tx_queues; i++) {
  1666. bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
  1667. i * priv->hw_params->tx_bds_per_q,
  1668. (i + 1) * priv->hw_params->tx_bds_per_q);
  1669. ring_cfg |= (1 << i);
  1670. dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
  1671. dma_priority[DMA_PRIO_REG_INDEX(i)] |=
  1672. ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
  1673. }
  1674. /* Initialize Tx default queue 16 */
  1675. bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
  1676. priv->hw_params->tx_queues *
  1677. priv->hw_params->tx_bds_per_q,
  1678. TOTAL_DESC);
  1679. ring_cfg |= (1 << DESC_INDEX);
  1680. dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
  1681. dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
  1682. ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
  1683. DMA_PRIO_REG_SHIFT(DESC_INDEX));
  1684. /* Set Tx queue priorities */
  1685. bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
  1686. bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
  1687. bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
  1688. /* Initialize Tx NAPI */
  1689. bcmgenet_init_tx_napi(priv);
  1690. /* Enable Tx queues */
  1691. bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
  1692. /* Enable Tx DMA */
  1693. if (dma_enable)
  1694. dma_ctrl |= DMA_EN;
  1695. bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
  1696. }
  1697. static void bcmgenet_init_rx_napi(struct bcmgenet_priv *priv)
  1698. {
  1699. unsigned int i;
  1700. struct bcmgenet_rx_ring *ring;
  1701. for (i = 0; i < priv->hw_params->rx_queues; ++i) {
  1702. ring = &priv->rx_rings[i];
  1703. netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
  1704. }
  1705. ring = &priv->rx_rings[DESC_INDEX];
  1706. netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
  1707. }
  1708. static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
  1709. {
  1710. unsigned int i;
  1711. struct bcmgenet_rx_ring *ring;
  1712. for (i = 0; i < priv->hw_params->rx_queues; ++i) {
  1713. ring = &priv->rx_rings[i];
  1714. napi_enable(&ring->napi);
  1715. }
  1716. ring = &priv->rx_rings[DESC_INDEX];
  1717. napi_enable(&ring->napi);
  1718. }
  1719. static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
  1720. {
  1721. unsigned int i;
  1722. struct bcmgenet_rx_ring *ring;
  1723. for (i = 0; i < priv->hw_params->rx_queues; ++i) {
  1724. ring = &priv->rx_rings[i];
  1725. napi_disable(&ring->napi);
  1726. }
  1727. ring = &priv->rx_rings[DESC_INDEX];
  1728. napi_disable(&ring->napi);
  1729. }
  1730. static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
  1731. {
  1732. unsigned int i;
  1733. struct bcmgenet_rx_ring *ring;
  1734. for (i = 0; i < priv->hw_params->rx_queues; ++i) {
  1735. ring = &priv->rx_rings[i];
  1736. netif_napi_del(&ring->napi);
  1737. }
  1738. ring = &priv->rx_rings[DESC_INDEX];
  1739. netif_napi_del(&ring->napi);
  1740. }
  1741. /* Initialize Rx queues
  1742. *
  1743. * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
  1744. * used to direct traffic to these queues.
  1745. *
  1746. * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
  1747. */
  1748. static int bcmgenet_init_rx_queues(struct net_device *dev)
  1749. {
  1750. struct bcmgenet_priv *priv = netdev_priv(dev);
  1751. u32 i;
  1752. u32 dma_enable;
  1753. u32 dma_ctrl;
  1754. u32 ring_cfg;
  1755. int ret;
  1756. dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
  1757. dma_enable = dma_ctrl & DMA_EN;
  1758. dma_ctrl &= ~DMA_EN;
  1759. bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
  1760. dma_ctrl = 0;
  1761. ring_cfg = 0;
  1762. /* Initialize Rx priority queues */
  1763. for (i = 0; i < priv->hw_params->rx_queues; i++) {
  1764. ret = bcmgenet_init_rx_ring(priv, i,
  1765. priv->hw_params->rx_bds_per_q,
  1766. i * priv->hw_params->rx_bds_per_q,
  1767. (i + 1) *
  1768. priv->hw_params->rx_bds_per_q);
  1769. if (ret)
  1770. return ret;
  1771. ring_cfg |= (1 << i);
  1772. dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
  1773. }
  1774. /* Initialize Rx default queue 16 */
  1775. ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
  1776. priv->hw_params->rx_queues *
  1777. priv->hw_params->rx_bds_per_q,
  1778. TOTAL_DESC);
  1779. if (ret)
  1780. return ret;
  1781. ring_cfg |= (1 << DESC_INDEX);
  1782. dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
  1783. /* Initialize Rx NAPI */
  1784. bcmgenet_init_rx_napi(priv);
  1785. /* Enable rings */
  1786. bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
  1787. /* Configure ring as descriptor ring and re-enable DMA if enabled */
  1788. if (dma_enable)
  1789. dma_ctrl |= DMA_EN;
  1790. bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
  1791. return 0;
  1792. }
  1793. static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
  1794. {
  1795. int ret = 0;
  1796. int timeout = 0;
  1797. u32 reg;
  1798. /* Disable TDMA to stop add more frames in TX DMA */
  1799. reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
  1800. reg &= ~DMA_EN;
  1801. bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
  1802. /* Check TDMA status register to confirm TDMA is disabled */
  1803. while (timeout++ < DMA_TIMEOUT_VAL) {
  1804. reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
  1805. if (reg & DMA_DISABLED)
  1806. break;
  1807. udelay(1);
  1808. }
  1809. if (timeout == DMA_TIMEOUT_VAL) {
  1810. netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
  1811. ret = -ETIMEDOUT;
  1812. }
  1813. /* Wait 10ms for packet drain in both tx and rx dma */
  1814. usleep_range(10000, 20000);
  1815. /* Disable RDMA */
  1816. reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
  1817. reg &= ~DMA_EN;
  1818. bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
  1819. timeout = 0;
  1820. /* Check RDMA status register to confirm RDMA is disabled */
  1821. while (timeout++ < DMA_TIMEOUT_VAL) {
  1822. reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
  1823. if (reg & DMA_DISABLED)
  1824. break;
  1825. udelay(1);
  1826. }
  1827. if (timeout == DMA_TIMEOUT_VAL) {
  1828. netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
  1829. ret = -ETIMEDOUT;
  1830. }
  1831. return ret;
  1832. }
  1833. static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
  1834. {
  1835. int i;
  1836. bcmgenet_fini_rx_napi(priv);
  1837. bcmgenet_fini_tx_napi(priv);
  1838. /* disable DMA */
  1839. bcmgenet_dma_teardown(priv);
  1840. for (i = 0; i < priv->num_tx_bds; i++) {
  1841. if (priv->tx_cbs[i].skb != NULL) {
  1842. dev_kfree_skb(priv->tx_cbs[i].skb);
  1843. priv->tx_cbs[i].skb = NULL;
  1844. }
  1845. }
  1846. bcmgenet_free_rx_buffers(priv);
  1847. kfree(priv->rx_cbs);
  1848. kfree(priv->tx_cbs);
  1849. }
  1850. /* init_edma: Initialize DMA control register */
  1851. static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
  1852. {
  1853. int ret;
  1854. unsigned int i;
  1855. struct enet_cb *cb;
  1856. netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
  1857. /* Initialize common Rx ring structures */
  1858. priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
  1859. priv->num_rx_bds = TOTAL_DESC;
  1860. priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
  1861. GFP_KERNEL);
  1862. if (!priv->rx_cbs)
  1863. return -ENOMEM;
  1864. for (i = 0; i < priv->num_rx_bds; i++) {
  1865. cb = priv->rx_cbs + i;
  1866. cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
  1867. }
  1868. /* Initialize common TX ring structures */
  1869. priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
  1870. priv->num_tx_bds = TOTAL_DESC;
  1871. priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
  1872. GFP_KERNEL);
  1873. if (!priv->tx_cbs) {
  1874. kfree(priv->rx_cbs);
  1875. return -ENOMEM;
  1876. }
  1877. for (i = 0; i < priv->num_tx_bds; i++) {
  1878. cb = priv->tx_cbs + i;
  1879. cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
  1880. }
  1881. /* Init rDma */
  1882. bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
  1883. /* Initialize Rx queues */
  1884. ret = bcmgenet_init_rx_queues(priv->dev);
  1885. if (ret) {
  1886. netdev_err(priv->dev, "failed to initialize Rx queues\n");
  1887. bcmgenet_free_rx_buffers(priv);
  1888. kfree(priv->rx_cbs);
  1889. kfree(priv->tx_cbs);
  1890. return ret;
  1891. }
  1892. /* Init tDma */
  1893. bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
  1894. /* Initialize Tx queues */
  1895. bcmgenet_init_tx_queues(priv->dev);
  1896. return 0;
  1897. }
  1898. /* Interrupt bottom half */
  1899. static void bcmgenet_irq_task(struct work_struct *work)
  1900. {
  1901. struct bcmgenet_priv *priv = container_of(
  1902. work, struct bcmgenet_priv, bcmgenet_irq_work);
  1903. netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
  1904. if (priv->irq0_stat & UMAC_IRQ_MPD_R) {
  1905. priv->irq0_stat &= ~UMAC_IRQ_MPD_R;
  1906. netif_dbg(priv, wol, priv->dev,
  1907. "magic packet detected, waking up\n");
  1908. bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
  1909. }
  1910. /* Link UP/DOWN event */
  1911. if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
  1912. (priv->irq0_stat & UMAC_IRQ_LINK_EVENT)) {
  1913. phy_mac_interrupt(priv->phydev,
  1914. !!(priv->irq0_stat & UMAC_IRQ_LINK_UP));
  1915. priv->irq0_stat &= ~UMAC_IRQ_LINK_EVENT;
  1916. }
  1917. }
  1918. /* bcmgenet_isr1: handle Rx and Tx priority queues */
  1919. static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
  1920. {
  1921. struct bcmgenet_priv *priv = dev_id;
  1922. struct bcmgenet_rx_ring *rx_ring;
  1923. struct bcmgenet_tx_ring *tx_ring;
  1924. unsigned int index;
  1925. /* Save irq status for bottom-half processing. */
  1926. priv->irq1_stat =
  1927. bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
  1928. ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
  1929. /* clear interrupts */
  1930. bcmgenet_intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
  1931. netif_dbg(priv, intr, priv->dev,
  1932. "%s: IRQ=0x%x\n", __func__, priv->irq1_stat);
  1933. /* Check Rx priority queue interrupts */
  1934. for (index = 0; index < priv->hw_params->rx_queues; index++) {
  1935. if (!(priv->irq1_stat & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
  1936. continue;
  1937. rx_ring = &priv->rx_rings[index];
  1938. if (likely(napi_schedule_prep(&rx_ring->napi))) {
  1939. rx_ring->int_disable(rx_ring);
  1940. __napi_schedule(&rx_ring->napi);
  1941. }
  1942. }
  1943. /* Check Tx priority queue interrupts */
  1944. for (index = 0; index < priv->hw_params->tx_queues; index++) {
  1945. if (!(priv->irq1_stat & BIT(index)))
  1946. continue;
  1947. tx_ring = &priv->tx_rings[index];
  1948. if (likely(napi_schedule_prep(&tx_ring->napi))) {
  1949. tx_ring->int_disable(tx_ring);
  1950. __napi_schedule(&tx_ring->napi);
  1951. }
  1952. }
  1953. return IRQ_HANDLED;
  1954. }
  1955. /* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
  1956. static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
  1957. {
  1958. struct bcmgenet_priv *priv = dev_id;
  1959. struct bcmgenet_rx_ring *rx_ring;
  1960. struct bcmgenet_tx_ring *tx_ring;
  1961. /* Save irq status for bottom-half processing. */
  1962. priv->irq0_stat =
  1963. bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
  1964. ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
  1965. /* clear interrupts */
  1966. bcmgenet_intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
  1967. netif_dbg(priv, intr, priv->dev,
  1968. "IRQ=0x%x\n", priv->irq0_stat);
  1969. if (priv->irq0_stat & UMAC_IRQ_RXDMA_DONE) {
  1970. rx_ring = &priv->rx_rings[DESC_INDEX];
  1971. if (likely(napi_schedule_prep(&rx_ring->napi))) {
  1972. rx_ring->int_disable(rx_ring);
  1973. __napi_schedule(&rx_ring->napi);
  1974. }
  1975. }
  1976. if (priv->irq0_stat & UMAC_IRQ_TXDMA_DONE) {
  1977. tx_ring = &priv->tx_rings[DESC_INDEX];
  1978. if (likely(napi_schedule_prep(&tx_ring->napi))) {
  1979. tx_ring->int_disable(tx_ring);
  1980. __napi_schedule(&tx_ring->napi);
  1981. }
  1982. }
  1983. if (priv->irq0_stat & (UMAC_IRQ_PHY_DET_R |
  1984. UMAC_IRQ_PHY_DET_F |
  1985. UMAC_IRQ_LINK_EVENT |
  1986. UMAC_IRQ_HFB_SM |
  1987. UMAC_IRQ_HFB_MM |
  1988. UMAC_IRQ_MPD_R)) {
  1989. /* all other interested interrupts handled in bottom half */
  1990. schedule_work(&priv->bcmgenet_irq_work);
  1991. }
  1992. if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
  1993. priv->irq0_stat & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
  1994. priv->irq0_stat &= ~(UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
  1995. wake_up(&priv->wq);
  1996. }
  1997. return IRQ_HANDLED;
  1998. }
  1999. static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
  2000. {
  2001. struct bcmgenet_priv *priv = dev_id;
  2002. pm_wakeup_event(&priv->pdev->dev, 0);
  2003. return IRQ_HANDLED;
  2004. }
  2005. static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
  2006. {
  2007. u32 reg;
  2008. reg = bcmgenet_rbuf_ctrl_get(priv);
  2009. reg |= BIT(1);
  2010. bcmgenet_rbuf_ctrl_set(priv, reg);
  2011. udelay(10);
  2012. reg &= ~BIT(1);
  2013. bcmgenet_rbuf_ctrl_set(priv, reg);
  2014. udelay(10);
  2015. }
  2016. static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
  2017. unsigned char *addr)
  2018. {
  2019. bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
  2020. (addr[2] << 8) | addr[3], UMAC_MAC0);
  2021. bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
  2022. }
  2023. /* Returns a reusable dma control register value */
  2024. static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
  2025. {
  2026. u32 reg;
  2027. u32 dma_ctrl;
  2028. /* disable DMA */
  2029. dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
  2030. reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
  2031. reg &= ~dma_ctrl;
  2032. bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
  2033. reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
  2034. reg &= ~dma_ctrl;
  2035. bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
  2036. bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
  2037. udelay(10);
  2038. bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
  2039. return dma_ctrl;
  2040. }
  2041. static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
  2042. {
  2043. u32 reg;
  2044. reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
  2045. reg |= dma_ctrl;
  2046. bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
  2047. reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
  2048. reg |= dma_ctrl;
  2049. bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
  2050. }
  2051. static bool bcmgenet_hfb_is_filter_enabled(struct bcmgenet_priv *priv,
  2052. u32 f_index)
  2053. {
  2054. u32 offset;
  2055. u32 reg;
  2056. offset = HFB_FLT_ENABLE_V3PLUS + (f_index < 32) * sizeof(u32);
  2057. reg = bcmgenet_hfb_reg_readl(priv, offset);
  2058. return !!(reg & (1 << (f_index % 32)));
  2059. }
  2060. static void bcmgenet_hfb_enable_filter(struct bcmgenet_priv *priv, u32 f_index)
  2061. {
  2062. u32 offset;
  2063. u32 reg;
  2064. offset = HFB_FLT_ENABLE_V3PLUS + (f_index < 32) * sizeof(u32);
  2065. reg = bcmgenet_hfb_reg_readl(priv, offset);
  2066. reg |= (1 << (f_index % 32));
  2067. bcmgenet_hfb_reg_writel(priv, reg, offset);
  2068. }
  2069. static void bcmgenet_hfb_set_filter_rx_queue_mapping(struct bcmgenet_priv *priv,
  2070. u32 f_index, u32 rx_queue)
  2071. {
  2072. u32 offset;
  2073. u32 reg;
  2074. offset = f_index / 8;
  2075. reg = bcmgenet_rdma_readl(priv, DMA_INDEX2RING_0 + offset);
  2076. reg &= ~(0xF << (4 * (f_index % 8)));
  2077. reg |= ((rx_queue & 0xF) << (4 * (f_index % 8)));
  2078. bcmgenet_rdma_writel(priv, reg, DMA_INDEX2RING_0 + offset);
  2079. }
  2080. static void bcmgenet_hfb_set_filter_length(struct bcmgenet_priv *priv,
  2081. u32 f_index, u32 f_length)
  2082. {
  2083. u32 offset;
  2084. u32 reg;
  2085. offset = HFB_FLT_LEN_V3PLUS +
  2086. ((priv->hw_params->hfb_filter_cnt - 1 - f_index) / 4) *
  2087. sizeof(u32);
  2088. reg = bcmgenet_hfb_reg_readl(priv, offset);
  2089. reg &= ~(0xFF << (8 * (f_index % 4)));
  2090. reg |= ((f_length & 0xFF) << (8 * (f_index % 4)));
  2091. bcmgenet_hfb_reg_writel(priv, reg, offset);
  2092. }
  2093. static int bcmgenet_hfb_find_unused_filter(struct bcmgenet_priv *priv)
  2094. {
  2095. u32 f_index;
  2096. for (f_index = 0; f_index < priv->hw_params->hfb_filter_cnt; f_index++)
  2097. if (!bcmgenet_hfb_is_filter_enabled(priv, f_index))
  2098. return f_index;
  2099. return -ENOMEM;
  2100. }
  2101. /* bcmgenet_hfb_add_filter
  2102. *
  2103. * Add new filter to Hardware Filter Block to match and direct Rx traffic to
  2104. * desired Rx queue.
  2105. *
  2106. * f_data is an array of unsigned 32-bit integers where each 32-bit integer
  2107. * provides filter data for 2 bytes (4 nibbles) of Rx frame:
  2108. *
  2109. * bits 31:20 - unused
  2110. * bit 19 - nibble 0 match enable
  2111. * bit 18 - nibble 1 match enable
  2112. * bit 17 - nibble 2 match enable
  2113. * bit 16 - nibble 3 match enable
  2114. * bits 15:12 - nibble 0 data
  2115. * bits 11:8 - nibble 1 data
  2116. * bits 7:4 - nibble 2 data
  2117. * bits 3:0 - nibble 3 data
  2118. *
  2119. * Example:
  2120. * In order to match:
  2121. * - Ethernet frame type = 0x0800 (IP)
  2122. * - IP version field = 4
  2123. * - IP protocol field = 0x11 (UDP)
  2124. *
  2125. * The following filter is needed:
  2126. * u32 hfb_filter_ipv4_udp[] = {
  2127. * Rx frame offset 0x00: 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  2128. * Rx frame offset 0x08: 0x00000000, 0x00000000, 0x000F0800, 0x00084000,
  2129. * Rx frame offset 0x10: 0x00000000, 0x00000000, 0x00000000, 0x00030011,
  2130. * };
  2131. *
  2132. * To add the filter to HFB and direct the traffic to Rx queue 0, call:
  2133. * bcmgenet_hfb_add_filter(priv, hfb_filter_ipv4_udp,
  2134. * ARRAY_SIZE(hfb_filter_ipv4_udp), 0);
  2135. */
  2136. int bcmgenet_hfb_add_filter(struct bcmgenet_priv *priv, u32 *f_data,
  2137. u32 f_length, u32 rx_queue)
  2138. {
  2139. int f_index;
  2140. u32 i;
  2141. f_index = bcmgenet_hfb_find_unused_filter(priv);
  2142. if (f_index < 0)
  2143. return -ENOMEM;
  2144. if (f_length > priv->hw_params->hfb_filter_size)
  2145. return -EINVAL;
  2146. for (i = 0; i < f_length; i++)
  2147. bcmgenet_hfb_writel(priv, f_data[i],
  2148. (f_index * priv->hw_params->hfb_filter_size + i) *
  2149. sizeof(u32));
  2150. bcmgenet_hfb_set_filter_length(priv, f_index, 2 * f_length);
  2151. bcmgenet_hfb_set_filter_rx_queue_mapping(priv, f_index, rx_queue);
  2152. bcmgenet_hfb_enable_filter(priv, f_index);
  2153. bcmgenet_hfb_reg_writel(priv, 0x1, HFB_CTRL);
  2154. return 0;
  2155. }
  2156. /* bcmgenet_hfb_clear
  2157. *
  2158. * Clear Hardware Filter Block and disable all filtering.
  2159. */
  2160. static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
  2161. {
  2162. u32 i;
  2163. bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
  2164. bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
  2165. bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
  2166. for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
  2167. bcmgenet_rdma_writel(priv, 0x0, i);
  2168. for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
  2169. bcmgenet_hfb_reg_writel(priv, 0x0,
  2170. HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
  2171. for (i = 0; i < priv->hw_params->hfb_filter_cnt *
  2172. priv->hw_params->hfb_filter_size; i++)
  2173. bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
  2174. }
  2175. static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
  2176. {
  2177. if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
  2178. return;
  2179. bcmgenet_hfb_clear(priv);
  2180. }
  2181. static void bcmgenet_netif_start(struct net_device *dev)
  2182. {
  2183. struct bcmgenet_priv *priv = netdev_priv(dev);
  2184. /* Start the network engine */
  2185. bcmgenet_enable_rx_napi(priv);
  2186. bcmgenet_enable_tx_napi(priv);
  2187. umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
  2188. netif_tx_start_all_queues(dev);
  2189. phy_start(priv->phydev);
  2190. }
  2191. static int bcmgenet_open(struct net_device *dev)
  2192. {
  2193. struct bcmgenet_priv *priv = netdev_priv(dev);
  2194. unsigned long dma_ctrl;
  2195. u32 reg;
  2196. int ret;
  2197. netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
  2198. /* Turn on the clock */
  2199. if (!IS_ERR(priv->clk))
  2200. clk_prepare_enable(priv->clk);
  2201. /* If this is an internal GPHY, power it back on now, before UniMAC is
  2202. * brought out of reset as absolutely no UniMAC activity is allowed
  2203. */
  2204. if (phy_is_internal(priv->phydev))
  2205. bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
  2206. /* take MAC out of reset */
  2207. bcmgenet_umac_reset(priv);
  2208. ret = init_umac(priv);
  2209. if (ret)
  2210. goto err_clk_disable;
  2211. /* disable ethernet MAC while updating its registers */
  2212. umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
  2213. /* Make sure we reflect the value of CRC_CMD_FWD */
  2214. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  2215. priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
  2216. bcmgenet_set_hw_addr(priv, dev->dev_addr);
  2217. if (phy_is_internal(priv->phydev)) {
  2218. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  2219. reg |= EXT_ENERGY_DET_MASK;
  2220. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  2221. }
  2222. /* Disable RX/TX DMA and flush TX queues */
  2223. dma_ctrl = bcmgenet_dma_disable(priv);
  2224. /* Reinitialize TDMA and RDMA and SW housekeeping */
  2225. ret = bcmgenet_init_dma(priv);
  2226. if (ret) {
  2227. netdev_err(dev, "failed to initialize DMA\n");
  2228. goto err_clk_disable;
  2229. }
  2230. /* Always enable ring 16 - descriptor ring */
  2231. bcmgenet_enable_dma(priv, dma_ctrl);
  2232. /* HFB init */
  2233. bcmgenet_hfb_init(priv);
  2234. ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
  2235. dev->name, priv);
  2236. if (ret < 0) {
  2237. netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
  2238. goto err_fini_dma;
  2239. }
  2240. ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
  2241. dev->name, priv);
  2242. if (ret < 0) {
  2243. netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
  2244. goto err_irq0;
  2245. }
  2246. /* Re-configure the port multiplexer towards the PHY device */
  2247. bcmgenet_mii_config(priv->dev, false);
  2248. phy_connect_direct(dev, priv->phydev, bcmgenet_mii_setup,
  2249. priv->phy_interface);
  2250. bcmgenet_netif_start(dev);
  2251. return 0;
  2252. err_irq0:
  2253. free_irq(priv->irq0, dev);
  2254. err_fini_dma:
  2255. bcmgenet_fini_dma(priv);
  2256. err_clk_disable:
  2257. if (!IS_ERR(priv->clk))
  2258. clk_disable_unprepare(priv->clk);
  2259. return ret;
  2260. }
  2261. static void bcmgenet_netif_stop(struct net_device *dev)
  2262. {
  2263. struct bcmgenet_priv *priv = netdev_priv(dev);
  2264. netif_tx_stop_all_queues(dev);
  2265. phy_stop(priv->phydev);
  2266. bcmgenet_intr_disable(priv);
  2267. bcmgenet_disable_rx_napi(priv);
  2268. bcmgenet_disable_tx_napi(priv);
  2269. /* Wait for pending work items to complete. Since interrupts are
  2270. * disabled no new work will be scheduled.
  2271. */
  2272. cancel_work_sync(&priv->bcmgenet_irq_work);
  2273. priv->old_link = -1;
  2274. priv->old_speed = -1;
  2275. priv->old_duplex = -1;
  2276. priv->old_pause = -1;
  2277. }
  2278. static int bcmgenet_close(struct net_device *dev)
  2279. {
  2280. struct bcmgenet_priv *priv = netdev_priv(dev);
  2281. int ret;
  2282. netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
  2283. bcmgenet_netif_stop(dev);
  2284. /* Really kill the PHY state machine and disconnect from it */
  2285. phy_disconnect(priv->phydev);
  2286. /* Disable MAC receive */
  2287. umac_enable_set(priv, CMD_RX_EN, false);
  2288. ret = bcmgenet_dma_teardown(priv);
  2289. if (ret)
  2290. return ret;
  2291. /* Disable MAC transmit. TX DMA disabled have to done before this */
  2292. umac_enable_set(priv, CMD_TX_EN, false);
  2293. /* tx reclaim */
  2294. bcmgenet_tx_reclaim_all(dev);
  2295. bcmgenet_fini_dma(priv);
  2296. free_irq(priv->irq0, priv);
  2297. free_irq(priv->irq1, priv);
  2298. if (phy_is_internal(priv->phydev))
  2299. ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
  2300. if (!IS_ERR(priv->clk))
  2301. clk_disable_unprepare(priv->clk);
  2302. return ret;
  2303. }
  2304. static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
  2305. {
  2306. struct bcmgenet_priv *priv = ring->priv;
  2307. u32 p_index, c_index, intsts, intmsk;
  2308. struct netdev_queue *txq;
  2309. unsigned int free_bds;
  2310. unsigned long flags;
  2311. bool txq_stopped;
  2312. if (!netif_msg_tx_err(priv))
  2313. return;
  2314. txq = netdev_get_tx_queue(priv->dev, ring->queue);
  2315. spin_lock_irqsave(&ring->lock, flags);
  2316. if (ring->index == DESC_INDEX) {
  2317. intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
  2318. intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
  2319. } else {
  2320. intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
  2321. intmsk = 1 << ring->index;
  2322. }
  2323. c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
  2324. p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
  2325. txq_stopped = netif_tx_queue_stopped(txq);
  2326. free_bds = ring->free_bds;
  2327. spin_unlock_irqrestore(&ring->lock, flags);
  2328. netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
  2329. "TX queue status: %s, interrupts: %s\n"
  2330. "(sw)free_bds: %d (sw)size: %d\n"
  2331. "(sw)p_index: %d (hw)p_index: %d\n"
  2332. "(sw)c_index: %d (hw)c_index: %d\n"
  2333. "(sw)clean_p: %d (sw)write_p: %d\n"
  2334. "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
  2335. ring->index, ring->queue,
  2336. txq_stopped ? "stopped" : "active",
  2337. intsts & intmsk ? "enabled" : "disabled",
  2338. free_bds, ring->size,
  2339. ring->prod_index, p_index & DMA_P_INDEX_MASK,
  2340. ring->c_index, c_index & DMA_C_INDEX_MASK,
  2341. ring->clean_ptr, ring->write_ptr,
  2342. ring->cb_ptr, ring->end_ptr);
  2343. }
  2344. static void bcmgenet_timeout(struct net_device *dev)
  2345. {
  2346. struct bcmgenet_priv *priv = netdev_priv(dev);
  2347. u32 int0_enable = 0;
  2348. u32 int1_enable = 0;
  2349. unsigned int q;
  2350. netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
  2351. bcmgenet_disable_tx_napi(priv);
  2352. for (q = 0; q < priv->hw_params->tx_queues; q++)
  2353. bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
  2354. bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
  2355. bcmgenet_tx_reclaim_all(dev);
  2356. for (q = 0; q < priv->hw_params->tx_queues; q++)
  2357. int1_enable |= (1 << q);
  2358. int0_enable = UMAC_IRQ_TXDMA_DONE;
  2359. /* Re-enable TX interrupts if disabled */
  2360. bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
  2361. bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
  2362. bcmgenet_enable_tx_napi(priv);
  2363. dev->trans_start = jiffies;
  2364. dev->stats.tx_errors++;
  2365. netif_tx_wake_all_queues(dev);
  2366. }
  2367. #define MAX_MC_COUNT 16
  2368. static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
  2369. unsigned char *addr,
  2370. int *i,
  2371. int *mc)
  2372. {
  2373. u32 reg;
  2374. bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
  2375. UMAC_MDF_ADDR + (*i * 4));
  2376. bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
  2377. addr[4] << 8 | addr[5],
  2378. UMAC_MDF_ADDR + ((*i + 1) * 4));
  2379. reg = bcmgenet_umac_readl(priv, UMAC_MDF_CTRL);
  2380. reg |= (1 << (MAX_MC_COUNT - *mc));
  2381. bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
  2382. *i += 2;
  2383. (*mc)++;
  2384. }
  2385. static void bcmgenet_set_rx_mode(struct net_device *dev)
  2386. {
  2387. struct bcmgenet_priv *priv = netdev_priv(dev);
  2388. struct netdev_hw_addr *ha;
  2389. int i, mc;
  2390. u32 reg;
  2391. netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
  2392. /* Promiscuous mode */
  2393. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  2394. if (dev->flags & IFF_PROMISC) {
  2395. reg |= CMD_PROMISC;
  2396. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  2397. bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
  2398. return;
  2399. } else {
  2400. reg &= ~CMD_PROMISC;
  2401. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  2402. }
  2403. /* UniMac doesn't support ALLMULTI */
  2404. if (dev->flags & IFF_ALLMULTI) {
  2405. netdev_warn(dev, "ALLMULTI is not supported\n");
  2406. return;
  2407. }
  2408. /* update MDF filter */
  2409. i = 0;
  2410. mc = 0;
  2411. /* Broadcast */
  2412. bcmgenet_set_mdf_addr(priv, dev->broadcast, &i, &mc);
  2413. /* my own address.*/
  2414. bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i, &mc);
  2415. /* Unicast list*/
  2416. if (netdev_uc_count(dev) > (MAX_MC_COUNT - mc))
  2417. return;
  2418. if (!netdev_uc_empty(dev))
  2419. netdev_for_each_uc_addr(ha, dev)
  2420. bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
  2421. /* Multicast */
  2422. if (netdev_mc_empty(dev) || netdev_mc_count(dev) >= (MAX_MC_COUNT - mc))
  2423. return;
  2424. netdev_for_each_mc_addr(ha, dev)
  2425. bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
  2426. }
  2427. /* Set the hardware MAC address. */
  2428. static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
  2429. {
  2430. struct sockaddr *addr = p;
  2431. /* Setting the MAC address at the hardware level is not possible
  2432. * without disabling the UniMAC RX/TX enable bits.
  2433. */
  2434. if (netif_running(dev))
  2435. return -EBUSY;
  2436. ether_addr_copy(dev->dev_addr, addr->sa_data);
  2437. return 0;
  2438. }
  2439. static const struct net_device_ops bcmgenet_netdev_ops = {
  2440. .ndo_open = bcmgenet_open,
  2441. .ndo_stop = bcmgenet_close,
  2442. .ndo_start_xmit = bcmgenet_xmit,
  2443. .ndo_tx_timeout = bcmgenet_timeout,
  2444. .ndo_set_rx_mode = bcmgenet_set_rx_mode,
  2445. .ndo_set_mac_address = bcmgenet_set_mac_addr,
  2446. .ndo_do_ioctl = bcmgenet_ioctl,
  2447. .ndo_set_features = bcmgenet_set_features,
  2448. };
  2449. /* Array of GENET hardware parameters/characteristics */
  2450. static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
  2451. [GENET_V1] = {
  2452. .tx_queues = 0,
  2453. .tx_bds_per_q = 0,
  2454. .rx_queues = 0,
  2455. .rx_bds_per_q = 0,
  2456. .bp_in_en_shift = 16,
  2457. .bp_in_mask = 0xffff,
  2458. .hfb_filter_cnt = 16,
  2459. .qtag_mask = 0x1F,
  2460. .hfb_offset = 0x1000,
  2461. .rdma_offset = 0x2000,
  2462. .tdma_offset = 0x3000,
  2463. .words_per_bd = 2,
  2464. },
  2465. [GENET_V2] = {
  2466. .tx_queues = 4,
  2467. .tx_bds_per_q = 32,
  2468. .rx_queues = 0,
  2469. .rx_bds_per_q = 0,
  2470. .bp_in_en_shift = 16,
  2471. .bp_in_mask = 0xffff,
  2472. .hfb_filter_cnt = 16,
  2473. .qtag_mask = 0x1F,
  2474. .tbuf_offset = 0x0600,
  2475. .hfb_offset = 0x1000,
  2476. .hfb_reg_offset = 0x2000,
  2477. .rdma_offset = 0x3000,
  2478. .tdma_offset = 0x4000,
  2479. .words_per_bd = 2,
  2480. .flags = GENET_HAS_EXT,
  2481. },
  2482. [GENET_V3] = {
  2483. .tx_queues = 4,
  2484. .tx_bds_per_q = 32,
  2485. .rx_queues = 0,
  2486. .rx_bds_per_q = 0,
  2487. .bp_in_en_shift = 17,
  2488. .bp_in_mask = 0x1ffff,
  2489. .hfb_filter_cnt = 48,
  2490. .hfb_filter_size = 128,
  2491. .qtag_mask = 0x3F,
  2492. .tbuf_offset = 0x0600,
  2493. .hfb_offset = 0x8000,
  2494. .hfb_reg_offset = 0xfc00,
  2495. .rdma_offset = 0x10000,
  2496. .tdma_offset = 0x11000,
  2497. .words_per_bd = 2,
  2498. .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
  2499. GENET_HAS_MOCA_LINK_DET,
  2500. },
  2501. [GENET_V4] = {
  2502. .tx_queues = 4,
  2503. .tx_bds_per_q = 32,
  2504. .rx_queues = 0,
  2505. .rx_bds_per_q = 0,
  2506. .bp_in_en_shift = 17,
  2507. .bp_in_mask = 0x1ffff,
  2508. .hfb_filter_cnt = 48,
  2509. .hfb_filter_size = 128,
  2510. .qtag_mask = 0x3F,
  2511. .tbuf_offset = 0x0600,
  2512. .hfb_offset = 0x8000,
  2513. .hfb_reg_offset = 0xfc00,
  2514. .rdma_offset = 0x2000,
  2515. .tdma_offset = 0x4000,
  2516. .words_per_bd = 3,
  2517. .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
  2518. GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
  2519. },
  2520. };
  2521. /* Infer hardware parameters from the detected GENET version */
  2522. static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
  2523. {
  2524. struct bcmgenet_hw_params *params;
  2525. u32 reg;
  2526. u8 major;
  2527. u16 gphy_rev;
  2528. if (GENET_IS_V4(priv)) {
  2529. bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
  2530. genet_dma_ring_regs = genet_dma_ring_regs_v4;
  2531. priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
  2532. priv->version = GENET_V4;
  2533. } else if (GENET_IS_V3(priv)) {
  2534. bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
  2535. genet_dma_ring_regs = genet_dma_ring_regs_v123;
  2536. priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
  2537. priv->version = GENET_V3;
  2538. } else if (GENET_IS_V2(priv)) {
  2539. bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
  2540. genet_dma_ring_regs = genet_dma_ring_regs_v123;
  2541. priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
  2542. priv->version = GENET_V2;
  2543. } else if (GENET_IS_V1(priv)) {
  2544. bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
  2545. genet_dma_ring_regs = genet_dma_ring_regs_v123;
  2546. priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
  2547. priv->version = GENET_V1;
  2548. }
  2549. /* enum genet_version starts at 1 */
  2550. priv->hw_params = &bcmgenet_hw_params[priv->version];
  2551. params = priv->hw_params;
  2552. /* Read GENET HW version */
  2553. reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
  2554. major = (reg >> 24 & 0x0f);
  2555. if (major == 5)
  2556. major = 4;
  2557. else if (major == 0)
  2558. major = 1;
  2559. if (major != priv->version) {
  2560. dev_err(&priv->pdev->dev,
  2561. "GENET version mismatch, got: %d, configured for: %d\n",
  2562. major, priv->version);
  2563. }
  2564. /* Print the GENET core version */
  2565. dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
  2566. major, (reg >> 16) & 0x0f, reg & 0xffff);
  2567. /* Store the integrated PHY revision for the MDIO probing function
  2568. * to pass this information to the PHY driver. The PHY driver expects
  2569. * to find the PHY major revision in bits 15:8 while the GENET register
  2570. * stores that information in bits 7:0, account for that.
  2571. *
  2572. * On newer chips, starting with PHY revision G0, a new scheme is
  2573. * deployed similar to the Starfighter 2 switch with GPHY major
  2574. * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
  2575. * is reserved as well as special value 0x01ff, we have a small
  2576. * heuristic to check for the new GPHY revision and re-arrange things
  2577. * so the GPHY driver is happy.
  2578. */
  2579. gphy_rev = reg & 0xffff;
  2580. /* This is the good old scheme, just GPHY major, no minor nor patch */
  2581. if ((gphy_rev & 0xf0) != 0)
  2582. priv->gphy_rev = gphy_rev << 8;
  2583. /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
  2584. else if ((gphy_rev & 0xff00) != 0)
  2585. priv->gphy_rev = gphy_rev;
  2586. /* This is reserved so should require special treatment */
  2587. else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
  2588. pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
  2589. return;
  2590. }
  2591. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  2592. if (!(params->flags & GENET_HAS_40BITS))
  2593. pr_warn("GENET does not support 40-bits PA\n");
  2594. #endif
  2595. pr_debug("Configuration for version: %d\n"
  2596. "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
  2597. "BP << en: %2d, BP msk: 0x%05x\n"
  2598. "HFB count: %2d, QTAQ msk: 0x%05x\n"
  2599. "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
  2600. "RDMA: 0x%05x, TDMA: 0x%05x\n"
  2601. "Words/BD: %d\n",
  2602. priv->version,
  2603. params->tx_queues, params->tx_bds_per_q,
  2604. params->rx_queues, params->rx_bds_per_q,
  2605. params->bp_in_en_shift, params->bp_in_mask,
  2606. params->hfb_filter_cnt, params->qtag_mask,
  2607. params->tbuf_offset, params->hfb_offset,
  2608. params->hfb_reg_offset,
  2609. params->rdma_offset, params->tdma_offset,
  2610. params->words_per_bd);
  2611. }
  2612. static const struct of_device_id bcmgenet_match[] = {
  2613. { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
  2614. { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
  2615. { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
  2616. { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
  2617. { },
  2618. };
  2619. static int bcmgenet_probe(struct platform_device *pdev)
  2620. {
  2621. struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
  2622. struct device_node *dn = pdev->dev.of_node;
  2623. const struct of_device_id *of_id = NULL;
  2624. struct bcmgenet_priv *priv;
  2625. struct net_device *dev;
  2626. const void *macaddr;
  2627. struct resource *r;
  2628. int err = -EIO;
  2629. /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
  2630. dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
  2631. GENET_MAX_MQ_CNT + 1);
  2632. if (!dev) {
  2633. dev_err(&pdev->dev, "can't allocate net device\n");
  2634. return -ENOMEM;
  2635. }
  2636. if (dn) {
  2637. of_id = of_match_node(bcmgenet_match, dn);
  2638. if (!of_id)
  2639. return -EINVAL;
  2640. }
  2641. priv = netdev_priv(dev);
  2642. priv->irq0 = platform_get_irq(pdev, 0);
  2643. priv->irq1 = platform_get_irq(pdev, 1);
  2644. priv->wol_irq = platform_get_irq(pdev, 2);
  2645. if (!priv->irq0 || !priv->irq1) {
  2646. dev_err(&pdev->dev, "can't find IRQs\n");
  2647. err = -EINVAL;
  2648. goto err;
  2649. }
  2650. if (dn) {
  2651. macaddr = of_get_mac_address(dn);
  2652. if (!macaddr) {
  2653. dev_err(&pdev->dev, "can't find MAC address\n");
  2654. err = -EINVAL;
  2655. goto err;
  2656. }
  2657. } else {
  2658. macaddr = pd->mac_address;
  2659. }
  2660. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2661. priv->base = devm_ioremap_resource(&pdev->dev, r);
  2662. if (IS_ERR(priv->base)) {
  2663. err = PTR_ERR(priv->base);
  2664. goto err;
  2665. }
  2666. SET_NETDEV_DEV(dev, &pdev->dev);
  2667. dev_set_drvdata(&pdev->dev, dev);
  2668. ether_addr_copy(dev->dev_addr, macaddr);
  2669. dev->watchdog_timeo = 2 * HZ;
  2670. dev->ethtool_ops = &bcmgenet_ethtool_ops;
  2671. dev->netdev_ops = &bcmgenet_netdev_ops;
  2672. priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
  2673. /* Set hardware features */
  2674. dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
  2675. NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
  2676. /* Request the WOL interrupt and advertise suspend if available */
  2677. priv->wol_irq_disabled = true;
  2678. err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
  2679. dev->name, priv);
  2680. if (!err)
  2681. device_set_wakeup_capable(&pdev->dev, 1);
  2682. /* Set the needed headroom to account for any possible
  2683. * features enabling/disabling at runtime
  2684. */
  2685. dev->needed_headroom += 64;
  2686. netdev_boot_setup_check(dev);
  2687. priv->dev = dev;
  2688. priv->pdev = pdev;
  2689. if (of_id)
  2690. priv->version = (enum bcmgenet_version)of_id->data;
  2691. else
  2692. priv->version = pd->genet_version;
  2693. priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
  2694. if (IS_ERR(priv->clk))
  2695. dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
  2696. if (!IS_ERR(priv->clk))
  2697. clk_prepare_enable(priv->clk);
  2698. bcmgenet_set_hw_params(priv);
  2699. /* Mii wait queue */
  2700. init_waitqueue_head(&priv->wq);
  2701. /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
  2702. priv->rx_buf_len = RX_BUF_LENGTH;
  2703. INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
  2704. priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
  2705. if (IS_ERR(priv->clk_wol))
  2706. dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
  2707. priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
  2708. if (IS_ERR(priv->clk_eee)) {
  2709. dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
  2710. priv->clk_eee = NULL;
  2711. }
  2712. err = reset_umac(priv);
  2713. if (err)
  2714. goto err_clk_disable;
  2715. err = bcmgenet_mii_init(dev);
  2716. if (err)
  2717. goto err_clk_disable;
  2718. /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
  2719. * just the ring 16 descriptor based TX
  2720. */
  2721. netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
  2722. netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
  2723. /* libphy will determine the link state */
  2724. netif_carrier_off(dev);
  2725. /* Turn off the main clock, WOL clock is handled separately */
  2726. if (!IS_ERR(priv->clk))
  2727. clk_disable_unprepare(priv->clk);
  2728. err = register_netdev(dev);
  2729. if (err)
  2730. goto err;
  2731. return err;
  2732. err_clk_disable:
  2733. if (!IS_ERR(priv->clk))
  2734. clk_disable_unprepare(priv->clk);
  2735. err:
  2736. free_netdev(dev);
  2737. return err;
  2738. }
  2739. static int bcmgenet_remove(struct platform_device *pdev)
  2740. {
  2741. struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
  2742. dev_set_drvdata(&pdev->dev, NULL);
  2743. unregister_netdev(priv->dev);
  2744. bcmgenet_mii_exit(priv->dev);
  2745. free_netdev(priv->dev);
  2746. return 0;
  2747. }
  2748. #ifdef CONFIG_PM_SLEEP
  2749. static int bcmgenet_suspend(struct device *d)
  2750. {
  2751. struct net_device *dev = dev_get_drvdata(d);
  2752. struct bcmgenet_priv *priv = netdev_priv(dev);
  2753. int ret;
  2754. if (!netif_running(dev))
  2755. return 0;
  2756. bcmgenet_netif_stop(dev);
  2757. phy_suspend(priv->phydev);
  2758. netif_device_detach(dev);
  2759. /* Disable MAC receive */
  2760. umac_enable_set(priv, CMD_RX_EN, false);
  2761. ret = bcmgenet_dma_teardown(priv);
  2762. if (ret)
  2763. return ret;
  2764. /* Disable MAC transmit. TX DMA disabled have to done before this */
  2765. umac_enable_set(priv, CMD_TX_EN, false);
  2766. /* tx reclaim */
  2767. bcmgenet_tx_reclaim_all(dev);
  2768. bcmgenet_fini_dma(priv);
  2769. /* Prepare the device for Wake-on-LAN and switch to the slow clock */
  2770. if (device_may_wakeup(d) && priv->wolopts) {
  2771. ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
  2772. clk_prepare_enable(priv->clk_wol);
  2773. } else if (phy_is_internal(priv->phydev)) {
  2774. ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
  2775. }
  2776. /* Turn off the clocks */
  2777. clk_disable_unprepare(priv->clk);
  2778. return ret;
  2779. }
  2780. static int bcmgenet_resume(struct device *d)
  2781. {
  2782. struct net_device *dev = dev_get_drvdata(d);
  2783. struct bcmgenet_priv *priv = netdev_priv(dev);
  2784. unsigned long dma_ctrl;
  2785. int ret;
  2786. u32 reg;
  2787. if (!netif_running(dev))
  2788. return 0;
  2789. /* Turn on the clock */
  2790. ret = clk_prepare_enable(priv->clk);
  2791. if (ret)
  2792. return ret;
  2793. /* If this is an internal GPHY, power it back on now, before UniMAC is
  2794. * brought out of reset as absolutely no UniMAC activity is allowed
  2795. */
  2796. if (phy_is_internal(priv->phydev))
  2797. bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
  2798. bcmgenet_umac_reset(priv);
  2799. ret = init_umac(priv);
  2800. if (ret)
  2801. goto out_clk_disable;
  2802. /* From WOL-enabled suspend, switch to regular clock */
  2803. if (priv->wolopts)
  2804. clk_disable_unprepare(priv->clk_wol);
  2805. phy_init_hw(priv->phydev);
  2806. /* Speed settings must be restored */
  2807. bcmgenet_mii_config(priv->dev, false);
  2808. /* disable ethernet MAC while updating its registers */
  2809. umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
  2810. bcmgenet_set_hw_addr(priv, dev->dev_addr);
  2811. if (phy_is_internal(priv->phydev)) {
  2812. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  2813. reg |= EXT_ENERGY_DET_MASK;
  2814. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  2815. }
  2816. if (priv->wolopts)
  2817. bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
  2818. /* Disable RX/TX DMA and flush TX queues */
  2819. dma_ctrl = bcmgenet_dma_disable(priv);
  2820. /* Reinitialize TDMA and RDMA and SW housekeeping */
  2821. ret = bcmgenet_init_dma(priv);
  2822. if (ret) {
  2823. netdev_err(dev, "failed to initialize DMA\n");
  2824. goto out_clk_disable;
  2825. }
  2826. /* Always enable ring 16 - descriptor ring */
  2827. bcmgenet_enable_dma(priv, dma_ctrl);
  2828. netif_device_attach(dev);
  2829. phy_resume(priv->phydev);
  2830. if (priv->eee.eee_enabled)
  2831. bcmgenet_eee_enable_set(dev, true);
  2832. bcmgenet_netif_start(dev);
  2833. return 0;
  2834. out_clk_disable:
  2835. clk_disable_unprepare(priv->clk);
  2836. return ret;
  2837. }
  2838. #endif /* CONFIG_PM_SLEEP */
  2839. static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
  2840. static struct platform_driver bcmgenet_driver = {
  2841. .probe = bcmgenet_probe,
  2842. .remove = bcmgenet_remove,
  2843. .driver = {
  2844. .name = "bcmgenet",
  2845. .of_match_table = bcmgenet_match,
  2846. .pm = &bcmgenet_pm_ops,
  2847. },
  2848. };
  2849. module_platform_driver(bcmgenet_driver);
  2850. MODULE_AUTHOR("Broadcom Corporation");
  2851. MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
  2852. MODULE_ALIAS("platform:bcmgenet");
  2853. MODULE_LICENSE("GPL");