bcmgenet.c 98 KB

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