macb.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507
  1. /*
  2. * Cadence MACB/GEM Ethernet Controller driver
  3. *
  4. * Copyright (C) 2004-2006 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/clk.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/circ_buf.h>
  17. #include <linux/slab.h>
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/gpio.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/platform_data/macb.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/phy.h>
  29. #include <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include <linux/of_gpio.h>
  32. #include <linux/of_mdio.h>
  33. #include <linux/of_net.h>
  34. #include <linux/ip.h>
  35. #include <linux/udp.h>
  36. #include <linux/tcp.h>
  37. #include "macb.h"
  38. #define MACB_RX_BUFFER_SIZE 128
  39. #define RX_BUFFER_MULTIPLE 64 /* bytes */
  40. #define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
  41. #define MIN_RX_RING_SIZE 64
  42. #define MAX_RX_RING_SIZE 8192
  43. #define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
  44. * (bp)->rx_ring_size)
  45. #define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
  46. #define MIN_TX_RING_SIZE 64
  47. #define MAX_TX_RING_SIZE 4096
  48. #define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
  49. * (bp)->tx_ring_size)
  50. /* level of occupied TX descriptors under which we wake up TX process */
  51. #define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
  52. #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
  53. | MACB_BIT(ISR_ROVR))
  54. #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
  55. | MACB_BIT(ISR_RLE) \
  56. | MACB_BIT(TXERR))
  57. #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
  58. /* Max length of transmit frame must be a multiple of 8 bytes */
  59. #define MACB_TX_LEN_ALIGN 8
  60. #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
  61. #define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
  62. #define GEM_MTU_MIN_SIZE ETH_MIN_MTU
  63. #define MACB_NETIF_LSO (NETIF_F_TSO | NETIF_F_UFO)
  64. #define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
  65. #define MACB_WOL_ENABLED (0x1 << 1)
  66. /* Graceful stop timeouts in us. We should allow up to
  67. * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
  68. */
  69. #define MACB_HALT_TIMEOUT 1230
  70. /* DMA buffer descriptor might be different size
  71. * depends on hardware configuration.
  72. */
  73. static unsigned int macb_dma_desc_get_size(struct macb *bp)
  74. {
  75. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  76. if (bp->hw_dma_cap == HW_DMA_CAP_64B)
  77. return sizeof(struct macb_dma_desc) + sizeof(struct macb_dma_desc_64);
  78. #endif
  79. return sizeof(struct macb_dma_desc);
  80. }
  81. static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int idx)
  82. {
  83. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  84. /* Dma buffer descriptor is 4 words length (instead of 2 words)
  85. * for 64b GEM.
  86. */
  87. if (bp->hw_dma_cap == HW_DMA_CAP_64B)
  88. idx <<= 1;
  89. #endif
  90. return idx;
  91. }
  92. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  93. static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
  94. {
  95. return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
  96. }
  97. #endif
  98. /* Ring buffer accessors */
  99. static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
  100. {
  101. return index & (bp->tx_ring_size - 1);
  102. }
  103. static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
  104. unsigned int index)
  105. {
  106. index = macb_tx_ring_wrap(queue->bp, index);
  107. index = macb_adj_dma_desc_idx(queue->bp, index);
  108. return &queue->tx_ring[index];
  109. }
  110. static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
  111. unsigned int index)
  112. {
  113. return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
  114. }
  115. static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
  116. {
  117. dma_addr_t offset;
  118. offset = macb_tx_ring_wrap(queue->bp, index) *
  119. macb_dma_desc_get_size(queue->bp);
  120. return queue->tx_ring_dma + offset;
  121. }
  122. static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
  123. {
  124. return index & (bp->rx_ring_size - 1);
  125. }
  126. static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
  127. {
  128. index = macb_rx_ring_wrap(bp, index);
  129. index = macb_adj_dma_desc_idx(bp, index);
  130. return &bp->rx_ring[index];
  131. }
  132. static void *macb_rx_buffer(struct macb *bp, unsigned int index)
  133. {
  134. return bp->rx_buffers + bp->rx_buffer_size *
  135. macb_rx_ring_wrap(bp, index);
  136. }
  137. /* I/O accessors */
  138. static u32 hw_readl_native(struct macb *bp, int offset)
  139. {
  140. return __raw_readl(bp->regs + offset);
  141. }
  142. static void hw_writel_native(struct macb *bp, int offset, u32 value)
  143. {
  144. __raw_writel(value, bp->regs + offset);
  145. }
  146. static u32 hw_readl(struct macb *bp, int offset)
  147. {
  148. return readl_relaxed(bp->regs + offset);
  149. }
  150. static void hw_writel(struct macb *bp, int offset, u32 value)
  151. {
  152. writel_relaxed(value, bp->regs + offset);
  153. }
  154. /* Find the CPU endianness by using the loopback bit of NCR register. When the
  155. * CPU is in big endian we need to program swapped mode for management
  156. * descriptor access.
  157. */
  158. static bool hw_is_native_io(void __iomem *addr)
  159. {
  160. u32 value = MACB_BIT(LLB);
  161. __raw_writel(value, addr + MACB_NCR);
  162. value = __raw_readl(addr + MACB_NCR);
  163. /* Write 0 back to disable everything */
  164. __raw_writel(0, addr + MACB_NCR);
  165. return value == MACB_BIT(LLB);
  166. }
  167. static bool hw_is_gem(void __iomem *addr, bool native_io)
  168. {
  169. u32 id;
  170. if (native_io)
  171. id = __raw_readl(addr + MACB_MID);
  172. else
  173. id = readl_relaxed(addr + MACB_MID);
  174. return MACB_BFEXT(IDNUM, id) >= 0x2;
  175. }
  176. static void macb_set_hwaddr(struct macb *bp)
  177. {
  178. u32 bottom;
  179. u16 top;
  180. bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
  181. macb_or_gem_writel(bp, SA1B, bottom);
  182. top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
  183. macb_or_gem_writel(bp, SA1T, top);
  184. /* Clear unused address register sets */
  185. macb_or_gem_writel(bp, SA2B, 0);
  186. macb_or_gem_writel(bp, SA2T, 0);
  187. macb_or_gem_writel(bp, SA3B, 0);
  188. macb_or_gem_writel(bp, SA3T, 0);
  189. macb_or_gem_writel(bp, SA4B, 0);
  190. macb_or_gem_writel(bp, SA4T, 0);
  191. }
  192. static void macb_get_hwaddr(struct macb *bp)
  193. {
  194. struct macb_platform_data *pdata;
  195. u32 bottom;
  196. u16 top;
  197. u8 addr[6];
  198. int i;
  199. pdata = dev_get_platdata(&bp->pdev->dev);
  200. /* Check all 4 address register for valid address */
  201. for (i = 0; i < 4; i++) {
  202. bottom = macb_or_gem_readl(bp, SA1B + i * 8);
  203. top = macb_or_gem_readl(bp, SA1T + i * 8);
  204. if (pdata && pdata->rev_eth_addr) {
  205. addr[5] = bottom & 0xff;
  206. addr[4] = (bottom >> 8) & 0xff;
  207. addr[3] = (bottom >> 16) & 0xff;
  208. addr[2] = (bottom >> 24) & 0xff;
  209. addr[1] = top & 0xff;
  210. addr[0] = (top & 0xff00) >> 8;
  211. } else {
  212. addr[0] = bottom & 0xff;
  213. addr[1] = (bottom >> 8) & 0xff;
  214. addr[2] = (bottom >> 16) & 0xff;
  215. addr[3] = (bottom >> 24) & 0xff;
  216. addr[4] = top & 0xff;
  217. addr[5] = (top >> 8) & 0xff;
  218. }
  219. if (is_valid_ether_addr(addr)) {
  220. memcpy(bp->dev->dev_addr, addr, sizeof(addr));
  221. return;
  222. }
  223. }
  224. dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
  225. eth_hw_addr_random(bp->dev);
  226. }
  227. static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  228. {
  229. struct macb *bp = bus->priv;
  230. int value;
  231. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  232. | MACB_BF(RW, MACB_MAN_READ)
  233. | MACB_BF(PHYA, mii_id)
  234. | MACB_BF(REGA, regnum)
  235. | MACB_BF(CODE, MACB_MAN_CODE)));
  236. /* wait for end of transfer */
  237. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  238. cpu_relax();
  239. value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  240. return value;
  241. }
  242. static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  243. u16 value)
  244. {
  245. struct macb *bp = bus->priv;
  246. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  247. | MACB_BF(RW, MACB_MAN_WRITE)
  248. | MACB_BF(PHYA, mii_id)
  249. | MACB_BF(REGA, regnum)
  250. | MACB_BF(CODE, MACB_MAN_CODE)
  251. | MACB_BF(DATA, value)));
  252. /* wait for end of transfer */
  253. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  254. cpu_relax();
  255. return 0;
  256. }
  257. /**
  258. * macb_set_tx_clk() - Set a clock to a new frequency
  259. * @clk Pointer to the clock to change
  260. * @rate New frequency in Hz
  261. * @dev Pointer to the struct net_device
  262. */
  263. static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
  264. {
  265. long ferr, rate, rate_rounded;
  266. if (!clk)
  267. return;
  268. switch (speed) {
  269. case SPEED_10:
  270. rate = 2500000;
  271. break;
  272. case SPEED_100:
  273. rate = 25000000;
  274. break;
  275. case SPEED_1000:
  276. rate = 125000000;
  277. break;
  278. default:
  279. return;
  280. }
  281. rate_rounded = clk_round_rate(clk, rate);
  282. if (rate_rounded < 0)
  283. return;
  284. /* RGMII allows 50 ppm frequency error. Test and warn if this limit
  285. * is not satisfied.
  286. */
  287. ferr = abs(rate_rounded - rate);
  288. ferr = DIV_ROUND_UP(ferr, rate / 100000);
  289. if (ferr > 5)
  290. netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
  291. rate);
  292. if (clk_set_rate(clk, rate_rounded))
  293. netdev_err(dev, "adjusting tx_clk failed.\n");
  294. }
  295. static void macb_handle_link_change(struct net_device *dev)
  296. {
  297. struct macb *bp = netdev_priv(dev);
  298. struct phy_device *phydev = dev->phydev;
  299. unsigned long flags;
  300. int status_change = 0;
  301. spin_lock_irqsave(&bp->lock, flags);
  302. if (phydev->link) {
  303. if ((bp->speed != phydev->speed) ||
  304. (bp->duplex != phydev->duplex)) {
  305. u32 reg;
  306. reg = macb_readl(bp, NCFGR);
  307. reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  308. if (macb_is_gem(bp))
  309. reg &= ~GEM_BIT(GBE);
  310. if (phydev->duplex)
  311. reg |= MACB_BIT(FD);
  312. if (phydev->speed == SPEED_100)
  313. reg |= MACB_BIT(SPD);
  314. if (phydev->speed == SPEED_1000 &&
  315. bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
  316. reg |= GEM_BIT(GBE);
  317. macb_or_gem_writel(bp, NCFGR, reg);
  318. bp->speed = phydev->speed;
  319. bp->duplex = phydev->duplex;
  320. status_change = 1;
  321. }
  322. }
  323. if (phydev->link != bp->link) {
  324. if (!phydev->link) {
  325. bp->speed = 0;
  326. bp->duplex = -1;
  327. }
  328. bp->link = phydev->link;
  329. status_change = 1;
  330. }
  331. spin_unlock_irqrestore(&bp->lock, flags);
  332. if (status_change) {
  333. if (phydev->link) {
  334. /* Update the TX clock rate if and only if the link is
  335. * up and there has been a link change.
  336. */
  337. macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
  338. netif_carrier_on(dev);
  339. netdev_info(dev, "link up (%d/%s)\n",
  340. phydev->speed,
  341. phydev->duplex == DUPLEX_FULL ?
  342. "Full" : "Half");
  343. } else {
  344. netif_carrier_off(dev);
  345. netdev_info(dev, "link down\n");
  346. }
  347. }
  348. }
  349. /* based on au1000_eth. c*/
  350. static int macb_mii_probe(struct net_device *dev)
  351. {
  352. struct macb *bp = netdev_priv(dev);
  353. struct macb_platform_data *pdata;
  354. struct phy_device *phydev;
  355. int phy_irq;
  356. int ret;
  357. phydev = phy_find_first(bp->mii_bus);
  358. if (!phydev) {
  359. netdev_err(dev, "no PHY found\n");
  360. return -ENXIO;
  361. }
  362. pdata = dev_get_platdata(&bp->pdev->dev);
  363. if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
  364. ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin,
  365. "phy int");
  366. if (!ret) {
  367. phy_irq = gpio_to_irq(pdata->phy_irq_pin);
  368. phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
  369. }
  370. } else {
  371. phydev->irq = PHY_POLL;
  372. }
  373. /* attach the mac to the phy */
  374. ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
  375. bp->phy_interface);
  376. if (ret) {
  377. netdev_err(dev, "Could not attach to PHY\n");
  378. return ret;
  379. }
  380. /* mask with MAC supported features */
  381. if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
  382. phydev->supported &= PHY_GBIT_FEATURES;
  383. else
  384. phydev->supported &= PHY_BASIC_FEATURES;
  385. if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
  386. phydev->supported &= ~SUPPORTED_1000baseT_Half;
  387. phydev->advertising = phydev->supported;
  388. bp->link = 0;
  389. bp->speed = 0;
  390. bp->duplex = -1;
  391. return 0;
  392. }
  393. static int macb_mii_init(struct macb *bp)
  394. {
  395. struct macb_platform_data *pdata;
  396. struct device_node *np;
  397. int err = -ENXIO, i;
  398. /* Enable management port */
  399. macb_writel(bp, NCR, MACB_BIT(MPE));
  400. bp->mii_bus = mdiobus_alloc();
  401. if (!bp->mii_bus) {
  402. err = -ENOMEM;
  403. goto err_out;
  404. }
  405. bp->mii_bus->name = "MACB_mii_bus";
  406. bp->mii_bus->read = &macb_mdio_read;
  407. bp->mii_bus->write = &macb_mdio_write;
  408. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  409. bp->pdev->name, bp->pdev->id);
  410. bp->mii_bus->priv = bp;
  411. bp->mii_bus->parent = &bp->pdev->dev;
  412. pdata = dev_get_platdata(&bp->pdev->dev);
  413. dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
  414. np = bp->pdev->dev.of_node;
  415. if (np) {
  416. /* try dt phy registration */
  417. err = of_mdiobus_register(bp->mii_bus, np);
  418. /* fallback to standard phy registration if no phy were
  419. * found during dt phy registration
  420. */
  421. if (!err && !phy_find_first(bp->mii_bus)) {
  422. for (i = 0; i < PHY_MAX_ADDR; i++) {
  423. struct phy_device *phydev;
  424. phydev = mdiobus_scan(bp->mii_bus, i);
  425. if (IS_ERR(phydev) &&
  426. PTR_ERR(phydev) != -ENODEV) {
  427. err = PTR_ERR(phydev);
  428. break;
  429. }
  430. }
  431. if (err)
  432. goto err_out_unregister_bus;
  433. }
  434. } else {
  435. for (i = 0; i < PHY_MAX_ADDR; i++)
  436. bp->mii_bus->irq[i] = PHY_POLL;
  437. if (pdata)
  438. bp->mii_bus->phy_mask = pdata->phy_mask;
  439. err = mdiobus_register(bp->mii_bus);
  440. }
  441. if (err)
  442. goto err_out_free_mdiobus;
  443. err = macb_mii_probe(bp->dev);
  444. if (err)
  445. goto err_out_unregister_bus;
  446. return 0;
  447. err_out_unregister_bus:
  448. mdiobus_unregister(bp->mii_bus);
  449. err_out_free_mdiobus:
  450. mdiobus_free(bp->mii_bus);
  451. err_out:
  452. return err;
  453. }
  454. static void macb_update_stats(struct macb *bp)
  455. {
  456. u32 *p = &bp->hw_stats.macb.rx_pause_frames;
  457. u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
  458. int offset = MACB_PFR;
  459. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  460. for (; p < end; p++, offset += 4)
  461. *p += bp->macb_reg_readl(bp, offset);
  462. }
  463. static int macb_halt_tx(struct macb *bp)
  464. {
  465. unsigned long halt_time, timeout;
  466. u32 status;
  467. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
  468. timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
  469. do {
  470. halt_time = jiffies;
  471. status = macb_readl(bp, TSR);
  472. if (!(status & MACB_BIT(TGO)))
  473. return 0;
  474. usleep_range(10, 250);
  475. } while (time_before(halt_time, timeout));
  476. return -ETIMEDOUT;
  477. }
  478. static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
  479. {
  480. if (tx_skb->mapping) {
  481. if (tx_skb->mapped_as_page)
  482. dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
  483. tx_skb->size, DMA_TO_DEVICE);
  484. else
  485. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
  486. tx_skb->size, DMA_TO_DEVICE);
  487. tx_skb->mapping = 0;
  488. }
  489. if (tx_skb->skb) {
  490. dev_kfree_skb_any(tx_skb->skb);
  491. tx_skb->skb = NULL;
  492. }
  493. }
  494. static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
  495. {
  496. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  497. struct macb_dma_desc_64 *desc_64;
  498. if (bp->hw_dma_cap == HW_DMA_CAP_64B) {
  499. desc_64 = macb_64b_desc(bp, desc);
  500. desc_64->addrh = upper_32_bits(addr);
  501. }
  502. #endif
  503. desc->addr = lower_32_bits(addr);
  504. }
  505. static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
  506. {
  507. dma_addr_t addr = 0;
  508. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  509. struct macb_dma_desc_64 *desc_64;
  510. if (bp->hw_dma_cap == HW_DMA_CAP_64B) {
  511. desc_64 = macb_64b_desc(bp, desc);
  512. addr = ((u64)(desc_64->addrh) << 32);
  513. }
  514. #endif
  515. addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
  516. return addr;
  517. }
  518. static void macb_tx_error_task(struct work_struct *work)
  519. {
  520. struct macb_queue *queue = container_of(work, struct macb_queue,
  521. tx_error_task);
  522. struct macb *bp = queue->bp;
  523. struct macb_tx_skb *tx_skb;
  524. struct macb_dma_desc *desc;
  525. struct sk_buff *skb;
  526. unsigned int tail;
  527. unsigned long flags;
  528. netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
  529. (unsigned int)(queue - bp->queues),
  530. queue->tx_tail, queue->tx_head);
  531. /* Prevent the queue IRQ handlers from running: each of them may call
  532. * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
  533. * As explained below, we have to halt the transmission before updating
  534. * TBQP registers so we call netif_tx_stop_all_queues() to notify the
  535. * network engine about the macb/gem being halted.
  536. */
  537. spin_lock_irqsave(&bp->lock, flags);
  538. /* Make sure nobody is trying to queue up new packets */
  539. netif_tx_stop_all_queues(bp->dev);
  540. /* Stop transmission now
  541. * (in case we have just queued new packets)
  542. * macb/gem must be halted to write TBQP register
  543. */
  544. if (macb_halt_tx(bp))
  545. /* Just complain for now, reinitializing TX path can be good */
  546. netdev_err(bp->dev, "BUG: halt tx timed out\n");
  547. /* Treat frames in TX queue including the ones that caused the error.
  548. * Free transmit buffers in upper layer.
  549. */
  550. for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
  551. u32 ctrl;
  552. desc = macb_tx_desc(queue, tail);
  553. ctrl = desc->ctrl;
  554. tx_skb = macb_tx_skb(queue, tail);
  555. skb = tx_skb->skb;
  556. if (ctrl & MACB_BIT(TX_USED)) {
  557. /* skb is set for the last buffer of the frame */
  558. while (!skb) {
  559. macb_tx_unmap(bp, tx_skb);
  560. tail++;
  561. tx_skb = macb_tx_skb(queue, tail);
  562. skb = tx_skb->skb;
  563. }
  564. /* ctrl still refers to the first buffer descriptor
  565. * since it's the only one written back by the hardware
  566. */
  567. if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
  568. netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
  569. macb_tx_ring_wrap(bp, tail),
  570. skb->data);
  571. bp->stats.tx_packets++;
  572. bp->stats.tx_bytes += skb->len;
  573. }
  574. } else {
  575. /* "Buffers exhausted mid-frame" errors may only happen
  576. * if the driver is buggy, so complain loudly about
  577. * those. Statistics are updated by hardware.
  578. */
  579. if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
  580. netdev_err(bp->dev,
  581. "BUG: TX buffers exhausted mid-frame\n");
  582. desc->ctrl = ctrl | MACB_BIT(TX_USED);
  583. }
  584. macb_tx_unmap(bp, tx_skb);
  585. }
  586. /* Set end of TX queue */
  587. desc = macb_tx_desc(queue, 0);
  588. macb_set_addr(bp, desc, 0);
  589. desc->ctrl = MACB_BIT(TX_USED);
  590. /* Make descriptor updates visible to hardware */
  591. wmb();
  592. /* Reinitialize the TX desc queue */
  593. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  594. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  595. if (bp->hw_dma_cap == HW_DMA_CAP_64B)
  596. queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
  597. #endif
  598. /* Make TX ring reflect state of hardware */
  599. queue->tx_head = 0;
  600. queue->tx_tail = 0;
  601. /* Housework before enabling TX IRQ */
  602. macb_writel(bp, TSR, macb_readl(bp, TSR));
  603. queue_writel(queue, IER, MACB_TX_INT_FLAGS);
  604. /* Now we are ready to start transmission again */
  605. netif_tx_start_all_queues(bp->dev);
  606. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  607. spin_unlock_irqrestore(&bp->lock, flags);
  608. }
  609. static void macb_tx_interrupt(struct macb_queue *queue)
  610. {
  611. unsigned int tail;
  612. unsigned int head;
  613. u32 status;
  614. struct macb *bp = queue->bp;
  615. u16 queue_index = queue - bp->queues;
  616. status = macb_readl(bp, TSR);
  617. macb_writel(bp, TSR, status);
  618. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  619. queue_writel(queue, ISR, MACB_BIT(TCOMP));
  620. netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
  621. (unsigned long)status);
  622. head = queue->tx_head;
  623. for (tail = queue->tx_tail; tail != head; tail++) {
  624. struct macb_tx_skb *tx_skb;
  625. struct sk_buff *skb;
  626. struct macb_dma_desc *desc;
  627. u32 ctrl;
  628. desc = macb_tx_desc(queue, tail);
  629. /* Make hw descriptor updates visible to CPU */
  630. rmb();
  631. ctrl = desc->ctrl;
  632. /* TX_USED bit is only set by hardware on the very first buffer
  633. * descriptor of the transmitted frame.
  634. */
  635. if (!(ctrl & MACB_BIT(TX_USED)))
  636. break;
  637. /* Process all buffers of the current transmitted frame */
  638. for (;; tail++) {
  639. tx_skb = macb_tx_skb(queue, tail);
  640. skb = tx_skb->skb;
  641. /* First, update TX stats if needed */
  642. if (skb) {
  643. netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
  644. macb_tx_ring_wrap(bp, tail),
  645. skb->data);
  646. bp->stats.tx_packets++;
  647. bp->stats.tx_bytes += skb->len;
  648. }
  649. /* Now we can safely release resources */
  650. macb_tx_unmap(bp, tx_skb);
  651. /* skb is set only for the last buffer of the frame.
  652. * WARNING: at this point skb has been freed by
  653. * macb_tx_unmap().
  654. */
  655. if (skb)
  656. break;
  657. }
  658. }
  659. queue->tx_tail = tail;
  660. if (__netif_subqueue_stopped(bp->dev, queue_index) &&
  661. CIRC_CNT(queue->tx_head, queue->tx_tail,
  662. bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
  663. netif_wake_subqueue(bp->dev, queue_index);
  664. }
  665. static void gem_rx_refill(struct macb *bp)
  666. {
  667. unsigned int entry;
  668. struct sk_buff *skb;
  669. dma_addr_t paddr;
  670. struct macb_dma_desc *desc;
  671. while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
  672. bp->rx_ring_size) > 0) {
  673. entry = macb_rx_ring_wrap(bp, bp->rx_prepared_head);
  674. /* Make hw descriptor updates visible to CPU */
  675. rmb();
  676. bp->rx_prepared_head++;
  677. desc = macb_rx_desc(bp, entry);
  678. if (!bp->rx_skbuff[entry]) {
  679. /* allocate sk_buff for this free entry in ring */
  680. skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
  681. if (unlikely(!skb)) {
  682. netdev_err(bp->dev,
  683. "Unable to allocate sk_buff\n");
  684. break;
  685. }
  686. /* now fill corresponding descriptor entry */
  687. paddr = dma_map_single(&bp->pdev->dev, skb->data,
  688. bp->rx_buffer_size,
  689. DMA_FROM_DEVICE);
  690. if (dma_mapping_error(&bp->pdev->dev, paddr)) {
  691. dev_kfree_skb(skb);
  692. break;
  693. }
  694. bp->rx_skbuff[entry] = skb;
  695. if (entry == bp->rx_ring_size - 1)
  696. paddr |= MACB_BIT(RX_WRAP);
  697. macb_set_addr(bp, desc, paddr);
  698. desc->ctrl = 0;
  699. /* properly align Ethernet header */
  700. skb_reserve(skb, NET_IP_ALIGN);
  701. } else {
  702. desc->addr &= ~MACB_BIT(RX_USED);
  703. desc->ctrl = 0;
  704. }
  705. }
  706. /* Make descriptor updates visible to hardware */
  707. wmb();
  708. netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
  709. bp->rx_prepared_head, bp->rx_tail);
  710. }
  711. /* Mark DMA descriptors from begin up to and not including end as unused */
  712. static void discard_partial_frame(struct macb *bp, unsigned int begin,
  713. unsigned int end)
  714. {
  715. unsigned int frag;
  716. for (frag = begin; frag != end; frag++) {
  717. struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
  718. desc->addr &= ~MACB_BIT(RX_USED);
  719. }
  720. /* Make descriptor updates visible to hardware */
  721. wmb();
  722. /* When this happens, the hardware stats registers for
  723. * whatever caused this is updated, so we don't have to record
  724. * anything.
  725. */
  726. }
  727. static int gem_rx(struct macb *bp, int budget)
  728. {
  729. unsigned int len;
  730. unsigned int entry;
  731. struct sk_buff *skb;
  732. struct macb_dma_desc *desc;
  733. int count = 0;
  734. while (count < budget) {
  735. u32 ctrl;
  736. dma_addr_t addr;
  737. bool rxused;
  738. entry = macb_rx_ring_wrap(bp, bp->rx_tail);
  739. desc = macb_rx_desc(bp, entry);
  740. /* Make hw descriptor updates visible to CPU */
  741. rmb();
  742. rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
  743. addr = macb_get_addr(bp, desc);
  744. ctrl = desc->ctrl;
  745. if (!rxused)
  746. break;
  747. bp->rx_tail++;
  748. count++;
  749. if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
  750. netdev_err(bp->dev,
  751. "not whole frame pointed by descriptor\n");
  752. bp->stats.rx_dropped++;
  753. break;
  754. }
  755. skb = bp->rx_skbuff[entry];
  756. if (unlikely(!skb)) {
  757. netdev_err(bp->dev,
  758. "inconsistent Rx descriptor chain\n");
  759. bp->stats.rx_dropped++;
  760. break;
  761. }
  762. /* now everything is ready for receiving packet */
  763. bp->rx_skbuff[entry] = NULL;
  764. len = ctrl & bp->rx_frm_len_mask;
  765. netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
  766. skb_put(skb, len);
  767. dma_unmap_single(&bp->pdev->dev, addr,
  768. bp->rx_buffer_size, DMA_FROM_DEVICE);
  769. skb->protocol = eth_type_trans(skb, bp->dev);
  770. skb_checksum_none_assert(skb);
  771. if (bp->dev->features & NETIF_F_RXCSUM &&
  772. !(bp->dev->flags & IFF_PROMISC) &&
  773. GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
  774. skb->ip_summed = CHECKSUM_UNNECESSARY;
  775. bp->stats.rx_packets++;
  776. bp->stats.rx_bytes += skb->len;
  777. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  778. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  779. skb->len, skb->csum);
  780. print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
  781. skb_mac_header(skb), 16, true);
  782. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
  783. skb->data, 32, true);
  784. #endif
  785. netif_receive_skb(skb);
  786. }
  787. gem_rx_refill(bp);
  788. return count;
  789. }
  790. static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
  791. unsigned int last_frag)
  792. {
  793. unsigned int len;
  794. unsigned int frag;
  795. unsigned int offset;
  796. struct sk_buff *skb;
  797. struct macb_dma_desc *desc;
  798. desc = macb_rx_desc(bp, last_frag);
  799. len = desc->ctrl & bp->rx_frm_len_mask;
  800. netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  801. macb_rx_ring_wrap(bp, first_frag),
  802. macb_rx_ring_wrap(bp, last_frag), len);
  803. /* The ethernet header starts NET_IP_ALIGN bytes into the
  804. * first buffer. Since the header is 14 bytes, this makes the
  805. * payload word-aligned.
  806. *
  807. * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
  808. * the two padding bytes into the skb so that we avoid hitting
  809. * the slowpath in memcpy(), and pull them off afterwards.
  810. */
  811. skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
  812. if (!skb) {
  813. bp->stats.rx_dropped++;
  814. for (frag = first_frag; ; frag++) {
  815. desc = macb_rx_desc(bp, frag);
  816. desc->addr &= ~MACB_BIT(RX_USED);
  817. if (frag == last_frag)
  818. break;
  819. }
  820. /* Make descriptor updates visible to hardware */
  821. wmb();
  822. return 1;
  823. }
  824. offset = 0;
  825. len += NET_IP_ALIGN;
  826. skb_checksum_none_assert(skb);
  827. skb_put(skb, len);
  828. for (frag = first_frag; ; frag++) {
  829. unsigned int frag_len = bp->rx_buffer_size;
  830. if (offset + frag_len > len) {
  831. if (unlikely(frag != last_frag)) {
  832. dev_kfree_skb_any(skb);
  833. return -1;
  834. }
  835. frag_len = len - offset;
  836. }
  837. skb_copy_to_linear_data_offset(skb, offset,
  838. macb_rx_buffer(bp, frag),
  839. frag_len);
  840. offset += bp->rx_buffer_size;
  841. desc = macb_rx_desc(bp, frag);
  842. desc->addr &= ~MACB_BIT(RX_USED);
  843. if (frag == last_frag)
  844. break;
  845. }
  846. /* Make descriptor updates visible to hardware */
  847. wmb();
  848. __skb_pull(skb, NET_IP_ALIGN);
  849. skb->protocol = eth_type_trans(skb, bp->dev);
  850. bp->stats.rx_packets++;
  851. bp->stats.rx_bytes += skb->len;
  852. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  853. skb->len, skb->csum);
  854. netif_receive_skb(skb);
  855. return 0;
  856. }
  857. static inline void macb_init_rx_ring(struct macb *bp)
  858. {
  859. dma_addr_t addr;
  860. struct macb_dma_desc *desc = NULL;
  861. int i;
  862. addr = bp->rx_buffers_dma;
  863. for (i = 0; i < bp->rx_ring_size; i++) {
  864. desc = macb_rx_desc(bp, i);
  865. macb_set_addr(bp, desc, addr);
  866. desc->ctrl = 0;
  867. addr += bp->rx_buffer_size;
  868. }
  869. desc->addr |= MACB_BIT(RX_WRAP);
  870. bp->rx_tail = 0;
  871. }
  872. static int macb_rx(struct macb *bp, int budget)
  873. {
  874. bool reset_rx_queue = false;
  875. int received = 0;
  876. unsigned int tail;
  877. int first_frag = -1;
  878. for (tail = bp->rx_tail; budget > 0; tail++) {
  879. struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
  880. u32 ctrl;
  881. /* Make hw descriptor updates visible to CPU */
  882. rmb();
  883. ctrl = desc->ctrl;
  884. if (!(desc->addr & MACB_BIT(RX_USED)))
  885. break;
  886. if (ctrl & MACB_BIT(RX_SOF)) {
  887. if (first_frag != -1)
  888. discard_partial_frame(bp, first_frag, tail);
  889. first_frag = tail;
  890. }
  891. if (ctrl & MACB_BIT(RX_EOF)) {
  892. int dropped;
  893. if (unlikely(first_frag == -1)) {
  894. reset_rx_queue = true;
  895. continue;
  896. }
  897. dropped = macb_rx_frame(bp, first_frag, tail);
  898. first_frag = -1;
  899. if (unlikely(dropped < 0)) {
  900. reset_rx_queue = true;
  901. continue;
  902. }
  903. if (!dropped) {
  904. received++;
  905. budget--;
  906. }
  907. }
  908. }
  909. if (unlikely(reset_rx_queue)) {
  910. unsigned long flags;
  911. u32 ctrl;
  912. netdev_err(bp->dev, "RX queue corruption: reset it\n");
  913. spin_lock_irqsave(&bp->lock, flags);
  914. ctrl = macb_readl(bp, NCR);
  915. macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
  916. macb_init_rx_ring(bp);
  917. macb_writel(bp, RBQP, bp->rx_ring_dma);
  918. macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
  919. spin_unlock_irqrestore(&bp->lock, flags);
  920. return received;
  921. }
  922. if (first_frag != -1)
  923. bp->rx_tail = first_frag;
  924. else
  925. bp->rx_tail = tail;
  926. return received;
  927. }
  928. static int macb_poll(struct napi_struct *napi, int budget)
  929. {
  930. struct macb *bp = container_of(napi, struct macb, napi);
  931. int work_done;
  932. u32 status;
  933. status = macb_readl(bp, RSR);
  934. macb_writel(bp, RSR, status);
  935. work_done = 0;
  936. netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
  937. (unsigned long)status, budget);
  938. work_done = bp->macbgem_ops.mog_rx(bp, budget);
  939. if (work_done < budget) {
  940. napi_complete_done(napi, work_done);
  941. /* Packets received while interrupts were disabled */
  942. status = macb_readl(bp, RSR);
  943. if (status) {
  944. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  945. macb_writel(bp, ISR, MACB_BIT(RCOMP));
  946. napi_reschedule(napi);
  947. } else {
  948. macb_writel(bp, IER, MACB_RX_INT_FLAGS);
  949. }
  950. }
  951. /* TODO: Handle errors */
  952. return work_done;
  953. }
  954. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  955. {
  956. struct macb_queue *queue = dev_id;
  957. struct macb *bp = queue->bp;
  958. struct net_device *dev = bp->dev;
  959. u32 status, ctrl;
  960. status = queue_readl(queue, ISR);
  961. if (unlikely(!status))
  962. return IRQ_NONE;
  963. spin_lock(&bp->lock);
  964. while (status) {
  965. /* close possible race with dev_close */
  966. if (unlikely(!netif_running(dev))) {
  967. queue_writel(queue, IDR, -1);
  968. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  969. queue_writel(queue, ISR, -1);
  970. break;
  971. }
  972. netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
  973. (unsigned int)(queue - bp->queues),
  974. (unsigned long)status);
  975. if (status & MACB_RX_INT_FLAGS) {
  976. /* There's no point taking any more interrupts
  977. * until we have processed the buffers. The
  978. * scheduling call may fail if the poll routine
  979. * is already scheduled, so disable interrupts
  980. * now.
  981. */
  982. queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
  983. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  984. queue_writel(queue, ISR, MACB_BIT(RCOMP));
  985. if (napi_schedule_prep(&bp->napi)) {
  986. netdev_vdbg(bp->dev, "scheduling RX softirq\n");
  987. __napi_schedule(&bp->napi);
  988. }
  989. }
  990. if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
  991. queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
  992. schedule_work(&queue->tx_error_task);
  993. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  994. queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
  995. break;
  996. }
  997. if (status & MACB_BIT(TCOMP))
  998. macb_tx_interrupt(queue);
  999. /* Link change detection isn't possible with RMII, so we'll
  1000. * add that if/when we get our hands on a full-blown MII PHY.
  1001. */
  1002. /* There is a hardware issue under heavy load where DMA can
  1003. * stop, this causes endless "used buffer descriptor read"
  1004. * interrupts but it can be cleared by re-enabling RX. See
  1005. * the at91 manual, section 41.3.1 or the Zynq manual
  1006. * section 16.7.4 for details.
  1007. */
  1008. if (status & MACB_BIT(RXUBR)) {
  1009. ctrl = macb_readl(bp, NCR);
  1010. macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
  1011. wmb();
  1012. macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
  1013. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1014. queue_writel(queue, ISR, MACB_BIT(RXUBR));
  1015. }
  1016. if (status & MACB_BIT(ISR_ROVR)) {
  1017. /* We missed at least one packet */
  1018. if (macb_is_gem(bp))
  1019. bp->hw_stats.gem.rx_overruns++;
  1020. else
  1021. bp->hw_stats.macb.rx_overruns++;
  1022. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1023. queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
  1024. }
  1025. if (status & MACB_BIT(HRESP)) {
  1026. /* TODO: Reset the hardware, and maybe move the
  1027. * netdev_err to a lower-priority context as well
  1028. * (work queue?)
  1029. */
  1030. netdev_err(dev, "DMA bus error: HRESP not OK\n");
  1031. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1032. queue_writel(queue, ISR, MACB_BIT(HRESP));
  1033. }
  1034. status = queue_readl(queue, ISR);
  1035. }
  1036. spin_unlock(&bp->lock);
  1037. return IRQ_HANDLED;
  1038. }
  1039. #ifdef CONFIG_NET_POLL_CONTROLLER
  1040. /* Polling receive - used by netconsole and other diagnostic tools
  1041. * to allow network i/o with interrupts disabled.
  1042. */
  1043. static void macb_poll_controller(struct net_device *dev)
  1044. {
  1045. struct macb *bp = netdev_priv(dev);
  1046. struct macb_queue *queue;
  1047. unsigned long flags;
  1048. unsigned int q;
  1049. local_irq_save(flags);
  1050. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1051. macb_interrupt(dev->irq, queue);
  1052. local_irq_restore(flags);
  1053. }
  1054. #endif
  1055. static unsigned int macb_tx_map(struct macb *bp,
  1056. struct macb_queue *queue,
  1057. struct sk_buff *skb,
  1058. unsigned int hdrlen)
  1059. {
  1060. dma_addr_t mapping;
  1061. unsigned int len, entry, i, tx_head = queue->tx_head;
  1062. struct macb_tx_skb *tx_skb = NULL;
  1063. struct macb_dma_desc *desc;
  1064. unsigned int offset, size, count = 0;
  1065. unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
  1066. unsigned int eof = 1, mss_mfs = 0;
  1067. u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
  1068. /* LSO */
  1069. if (skb_shinfo(skb)->gso_size != 0) {
  1070. if (ip_hdr(skb)->protocol == IPPROTO_UDP)
  1071. /* UDP - UFO */
  1072. lso_ctrl = MACB_LSO_UFO_ENABLE;
  1073. else
  1074. /* TCP - TSO */
  1075. lso_ctrl = MACB_LSO_TSO_ENABLE;
  1076. }
  1077. /* First, map non-paged data */
  1078. len = skb_headlen(skb);
  1079. /* first buffer length */
  1080. size = hdrlen;
  1081. offset = 0;
  1082. while (len) {
  1083. entry = macb_tx_ring_wrap(bp, tx_head);
  1084. tx_skb = &queue->tx_skb[entry];
  1085. mapping = dma_map_single(&bp->pdev->dev,
  1086. skb->data + offset,
  1087. size, DMA_TO_DEVICE);
  1088. if (dma_mapping_error(&bp->pdev->dev, mapping))
  1089. goto dma_error;
  1090. /* Save info to properly release resources */
  1091. tx_skb->skb = NULL;
  1092. tx_skb->mapping = mapping;
  1093. tx_skb->size = size;
  1094. tx_skb->mapped_as_page = false;
  1095. len -= size;
  1096. offset += size;
  1097. count++;
  1098. tx_head++;
  1099. size = min(len, bp->max_tx_length);
  1100. }
  1101. /* Then, map paged data from fragments */
  1102. for (f = 0; f < nr_frags; f++) {
  1103. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  1104. len = skb_frag_size(frag);
  1105. offset = 0;
  1106. while (len) {
  1107. size = min(len, bp->max_tx_length);
  1108. entry = macb_tx_ring_wrap(bp, tx_head);
  1109. tx_skb = &queue->tx_skb[entry];
  1110. mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
  1111. offset, size, DMA_TO_DEVICE);
  1112. if (dma_mapping_error(&bp->pdev->dev, mapping))
  1113. goto dma_error;
  1114. /* Save info to properly release resources */
  1115. tx_skb->skb = NULL;
  1116. tx_skb->mapping = mapping;
  1117. tx_skb->size = size;
  1118. tx_skb->mapped_as_page = true;
  1119. len -= size;
  1120. offset += size;
  1121. count++;
  1122. tx_head++;
  1123. }
  1124. }
  1125. /* Should never happen */
  1126. if (unlikely(!tx_skb)) {
  1127. netdev_err(bp->dev, "BUG! empty skb!\n");
  1128. return 0;
  1129. }
  1130. /* This is the last buffer of the frame: save socket buffer */
  1131. tx_skb->skb = skb;
  1132. /* Update TX ring: update buffer descriptors in reverse order
  1133. * to avoid race condition
  1134. */
  1135. /* Set 'TX_USED' bit in buffer descriptor at tx_head position
  1136. * to set the end of TX queue
  1137. */
  1138. i = tx_head;
  1139. entry = macb_tx_ring_wrap(bp, i);
  1140. ctrl = MACB_BIT(TX_USED);
  1141. desc = macb_tx_desc(queue, entry);
  1142. desc->ctrl = ctrl;
  1143. if (lso_ctrl) {
  1144. if (lso_ctrl == MACB_LSO_UFO_ENABLE)
  1145. /* include header and FCS in value given to h/w */
  1146. mss_mfs = skb_shinfo(skb)->gso_size +
  1147. skb_transport_offset(skb) +
  1148. ETH_FCS_LEN;
  1149. else /* TSO */ {
  1150. mss_mfs = skb_shinfo(skb)->gso_size;
  1151. /* TCP Sequence Number Source Select
  1152. * can be set only for TSO
  1153. */
  1154. seq_ctrl = 0;
  1155. }
  1156. }
  1157. do {
  1158. i--;
  1159. entry = macb_tx_ring_wrap(bp, i);
  1160. tx_skb = &queue->tx_skb[entry];
  1161. desc = macb_tx_desc(queue, entry);
  1162. ctrl = (u32)tx_skb->size;
  1163. if (eof) {
  1164. ctrl |= MACB_BIT(TX_LAST);
  1165. eof = 0;
  1166. }
  1167. if (unlikely(entry == (bp->tx_ring_size - 1)))
  1168. ctrl |= MACB_BIT(TX_WRAP);
  1169. /* First descriptor is header descriptor */
  1170. if (i == queue->tx_head) {
  1171. ctrl |= MACB_BF(TX_LSO, lso_ctrl);
  1172. ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
  1173. } else
  1174. /* Only set MSS/MFS on payload descriptors
  1175. * (second or later descriptor)
  1176. */
  1177. ctrl |= MACB_BF(MSS_MFS, mss_mfs);
  1178. /* Set TX buffer descriptor */
  1179. macb_set_addr(bp, desc, tx_skb->mapping);
  1180. /* desc->addr must be visible to hardware before clearing
  1181. * 'TX_USED' bit in desc->ctrl.
  1182. */
  1183. wmb();
  1184. desc->ctrl = ctrl;
  1185. } while (i != queue->tx_head);
  1186. queue->tx_head = tx_head;
  1187. return count;
  1188. dma_error:
  1189. netdev_err(bp->dev, "TX DMA map failed\n");
  1190. for (i = queue->tx_head; i != tx_head; i++) {
  1191. tx_skb = macb_tx_skb(queue, i);
  1192. macb_tx_unmap(bp, tx_skb);
  1193. }
  1194. return 0;
  1195. }
  1196. static netdev_features_t macb_features_check(struct sk_buff *skb,
  1197. struct net_device *dev,
  1198. netdev_features_t features)
  1199. {
  1200. unsigned int nr_frags, f;
  1201. unsigned int hdrlen;
  1202. /* Validate LSO compatibility */
  1203. /* there is only one buffer */
  1204. if (!skb_is_nonlinear(skb))
  1205. return features;
  1206. /* length of header */
  1207. hdrlen = skb_transport_offset(skb);
  1208. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  1209. hdrlen += tcp_hdrlen(skb);
  1210. /* For LSO:
  1211. * When software supplies two or more payload buffers all payload buffers
  1212. * apart from the last must be a multiple of 8 bytes in size.
  1213. */
  1214. if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
  1215. return features & ~MACB_NETIF_LSO;
  1216. nr_frags = skb_shinfo(skb)->nr_frags;
  1217. /* No need to check last fragment */
  1218. nr_frags--;
  1219. for (f = 0; f < nr_frags; f++) {
  1220. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  1221. if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
  1222. return features & ~MACB_NETIF_LSO;
  1223. }
  1224. return features;
  1225. }
  1226. static inline int macb_clear_csum(struct sk_buff *skb)
  1227. {
  1228. /* no change for packets without checksum offloading */
  1229. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1230. return 0;
  1231. /* make sure we can modify the header */
  1232. if (unlikely(skb_cow_head(skb, 0)))
  1233. return -1;
  1234. /* initialize checksum field
  1235. * This is required - at least for Zynq, which otherwise calculates
  1236. * wrong UDP header checksums for UDP packets with UDP data len <=2
  1237. */
  1238. *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
  1239. return 0;
  1240. }
  1241. static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1242. {
  1243. u16 queue_index = skb_get_queue_mapping(skb);
  1244. struct macb *bp = netdev_priv(dev);
  1245. struct macb_queue *queue = &bp->queues[queue_index];
  1246. unsigned long flags;
  1247. unsigned int desc_cnt, nr_frags, frag_size, f;
  1248. unsigned int hdrlen;
  1249. bool is_lso, is_udp = 0;
  1250. is_lso = (skb_shinfo(skb)->gso_size != 0);
  1251. if (is_lso) {
  1252. is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
  1253. /* length of headers */
  1254. if (is_udp)
  1255. /* only queue eth + ip headers separately for UDP */
  1256. hdrlen = skb_transport_offset(skb);
  1257. else
  1258. hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1259. if (skb_headlen(skb) < hdrlen) {
  1260. netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
  1261. /* if this is required, would need to copy to single buffer */
  1262. return NETDEV_TX_BUSY;
  1263. }
  1264. } else
  1265. hdrlen = min(skb_headlen(skb), bp->max_tx_length);
  1266. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  1267. netdev_vdbg(bp->dev,
  1268. "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
  1269. queue_index, skb->len, skb->head, skb->data,
  1270. skb_tail_pointer(skb), skb_end_pointer(skb));
  1271. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
  1272. skb->data, 16, true);
  1273. #endif
  1274. /* Count how many TX buffer descriptors are needed to send this
  1275. * socket buffer: skb fragments of jumbo frames may need to be
  1276. * split into many buffer descriptors.
  1277. */
  1278. if (is_lso && (skb_headlen(skb) > hdrlen))
  1279. /* extra header descriptor if also payload in first buffer */
  1280. desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
  1281. else
  1282. desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
  1283. nr_frags = skb_shinfo(skb)->nr_frags;
  1284. for (f = 0; f < nr_frags; f++) {
  1285. frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
  1286. desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
  1287. }
  1288. spin_lock_irqsave(&bp->lock, flags);
  1289. /* This is a hard error, log it. */
  1290. if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
  1291. bp->tx_ring_size) < desc_cnt) {
  1292. netif_stop_subqueue(dev, queue_index);
  1293. spin_unlock_irqrestore(&bp->lock, flags);
  1294. netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
  1295. queue->tx_head, queue->tx_tail);
  1296. return NETDEV_TX_BUSY;
  1297. }
  1298. if (macb_clear_csum(skb)) {
  1299. dev_kfree_skb_any(skb);
  1300. goto unlock;
  1301. }
  1302. /* Map socket buffer for DMA transfer */
  1303. if (!macb_tx_map(bp, queue, skb, hdrlen)) {
  1304. dev_kfree_skb_any(skb);
  1305. goto unlock;
  1306. }
  1307. /* Make newly initialized descriptor visible to hardware */
  1308. wmb();
  1309. skb_tx_timestamp(skb);
  1310. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  1311. if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
  1312. netif_stop_subqueue(dev, queue_index);
  1313. unlock:
  1314. spin_unlock_irqrestore(&bp->lock, flags);
  1315. return NETDEV_TX_OK;
  1316. }
  1317. static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
  1318. {
  1319. if (!macb_is_gem(bp)) {
  1320. bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
  1321. } else {
  1322. bp->rx_buffer_size = size;
  1323. if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
  1324. netdev_dbg(bp->dev,
  1325. "RX buffer must be multiple of %d bytes, expanding\n",
  1326. RX_BUFFER_MULTIPLE);
  1327. bp->rx_buffer_size =
  1328. roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
  1329. }
  1330. }
  1331. netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
  1332. bp->dev->mtu, bp->rx_buffer_size);
  1333. }
  1334. static void gem_free_rx_buffers(struct macb *bp)
  1335. {
  1336. struct sk_buff *skb;
  1337. struct macb_dma_desc *desc;
  1338. dma_addr_t addr;
  1339. int i;
  1340. if (!bp->rx_skbuff)
  1341. return;
  1342. for (i = 0; i < bp->rx_ring_size; i++) {
  1343. skb = bp->rx_skbuff[i];
  1344. if (!skb)
  1345. continue;
  1346. desc = macb_rx_desc(bp, i);
  1347. addr = macb_get_addr(bp, desc);
  1348. dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
  1349. DMA_FROM_DEVICE);
  1350. dev_kfree_skb_any(skb);
  1351. skb = NULL;
  1352. }
  1353. kfree(bp->rx_skbuff);
  1354. bp->rx_skbuff = NULL;
  1355. }
  1356. static void macb_free_rx_buffers(struct macb *bp)
  1357. {
  1358. if (bp->rx_buffers) {
  1359. dma_free_coherent(&bp->pdev->dev,
  1360. bp->rx_ring_size * bp->rx_buffer_size,
  1361. bp->rx_buffers, bp->rx_buffers_dma);
  1362. bp->rx_buffers = NULL;
  1363. }
  1364. }
  1365. static void macb_free_consistent(struct macb *bp)
  1366. {
  1367. struct macb_queue *queue;
  1368. unsigned int q;
  1369. bp->macbgem_ops.mog_free_rx_buffers(bp);
  1370. if (bp->rx_ring) {
  1371. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
  1372. bp->rx_ring, bp->rx_ring_dma);
  1373. bp->rx_ring = NULL;
  1374. }
  1375. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1376. kfree(queue->tx_skb);
  1377. queue->tx_skb = NULL;
  1378. if (queue->tx_ring) {
  1379. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
  1380. queue->tx_ring, queue->tx_ring_dma);
  1381. queue->tx_ring = NULL;
  1382. }
  1383. }
  1384. }
  1385. static int gem_alloc_rx_buffers(struct macb *bp)
  1386. {
  1387. int size;
  1388. size = bp->rx_ring_size * sizeof(struct sk_buff *);
  1389. bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
  1390. if (!bp->rx_skbuff)
  1391. return -ENOMEM;
  1392. else
  1393. netdev_dbg(bp->dev,
  1394. "Allocated %d RX struct sk_buff entries at %p\n",
  1395. bp->rx_ring_size, bp->rx_skbuff);
  1396. return 0;
  1397. }
  1398. static int macb_alloc_rx_buffers(struct macb *bp)
  1399. {
  1400. int size;
  1401. size = bp->rx_ring_size * bp->rx_buffer_size;
  1402. bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  1403. &bp->rx_buffers_dma, GFP_KERNEL);
  1404. if (!bp->rx_buffers)
  1405. return -ENOMEM;
  1406. netdev_dbg(bp->dev,
  1407. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  1408. size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
  1409. return 0;
  1410. }
  1411. static int macb_alloc_consistent(struct macb *bp)
  1412. {
  1413. struct macb_queue *queue;
  1414. unsigned int q;
  1415. int size;
  1416. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1417. size = TX_RING_BYTES(bp);
  1418. queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1419. &queue->tx_ring_dma,
  1420. GFP_KERNEL);
  1421. if (!queue->tx_ring)
  1422. goto out_err;
  1423. netdev_dbg(bp->dev,
  1424. "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
  1425. q, size, (unsigned long)queue->tx_ring_dma,
  1426. queue->tx_ring);
  1427. size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
  1428. queue->tx_skb = kmalloc(size, GFP_KERNEL);
  1429. if (!queue->tx_skb)
  1430. goto out_err;
  1431. }
  1432. size = RX_RING_BYTES(bp);
  1433. bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1434. &bp->rx_ring_dma, GFP_KERNEL);
  1435. if (!bp->rx_ring)
  1436. goto out_err;
  1437. netdev_dbg(bp->dev,
  1438. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  1439. size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
  1440. if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
  1441. goto out_err;
  1442. return 0;
  1443. out_err:
  1444. macb_free_consistent(bp);
  1445. return -ENOMEM;
  1446. }
  1447. static void gem_init_rings(struct macb *bp)
  1448. {
  1449. struct macb_queue *queue;
  1450. struct macb_dma_desc *desc = NULL;
  1451. unsigned int q;
  1452. int i;
  1453. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1454. for (i = 0; i < bp->tx_ring_size; i++) {
  1455. desc = macb_tx_desc(queue, i);
  1456. macb_set_addr(bp, desc, 0);
  1457. desc->ctrl = MACB_BIT(TX_USED);
  1458. }
  1459. desc->ctrl |= MACB_BIT(TX_WRAP);
  1460. queue->tx_head = 0;
  1461. queue->tx_tail = 0;
  1462. }
  1463. bp->rx_tail = 0;
  1464. bp->rx_prepared_head = 0;
  1465. gem_rx_refill(bp);
  1466. }
  1467. static void macb_init_rings(struct macb *bp)
  1468. {
  1469. int i;
  1470. struct macb_dma_desc *desc = NULL;
  1471. macb_init_rx_ring(bp);
  1472. for (i = 0; i < bp->tx_ring_size; i++) {
  1473. desc = macb_tx_desc(&bp->queues[0], i);
  1474. macb_set_addr(bp, desc, 0);
  1475. desc->ctrl = MACB_BIT(TX_USED);
  1476. }
  1477. bp->queues[0].tx_head = 0;
  1478. bp->queues[0].tx_tail = 0;
  1479. desc->ctrl |= MACB_BIT(TX_WRAP);
  1480. }
  1481. static void macb_reset_hw(struct macb *bp)
  1482. {
  1483. struct macb_queue *queue;
  1484. unsigned int q;
  1485. /* Disable RX and TX (XXX: Should we halt the transmission
  1486. * more gracefully?)
  1487. */
  1488. macb_writel(bp, NCR, 0);
  1489. /* Clear the stats registers (XXX: Update stats first?) */
  1490. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  1491. /* Clear all status flags */
  1492. macb_writel(bp, TSR, -1);
  1493. macb_writel(bp, RSR, -1);
  1494. /* Disable all interrupts */
  1495. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1496. queue_writel(queue, IDR, -1);
  1497. queue_readl(queue, ISR);
  1498. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1499. queue_writel(queue, ISR, -1);
  1500. }
  1501. }
  1502. static u32 gem_mdc_clk_div(struct macb *bp)
  1503. {
  1504. u32 config;
  1505. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  1506. if (pclk_hz <= 20000000)
  1507. config = GEM_BF(CLK, GEM_CLK_DIV8);
  1508. else if (pclk_hz <= 40000000)
  1509. config = GEM_BF(CLK, GEM_CLK_DIV16);
  1510. else if (pclk_hz <= 80000000)
  1511. config = GEM_BF(CLK, GEM_CLK_DIV32);
  1512. else if (pclk_hz <= 120000000)
  1513. config = GEM_BF(CLK, GEM_CLK_DIV48);
  1514. else if (pclk_hz <= 160000000)
  1515. config = GEM_BF(CLK, GEM_CLK_DIV64);
  1516. else
  1517. config = GEM_BF(CLK, GEM_CLK_DIV96);
  1518. return config;
  1519. }
  1520. static u32 macb_mdc_clk_div(struct macb *bp)
  1521. {
  1522. u32 config;
  1523. unsigned long pclk_hz;
  1524. if (macb_is_gem(bp))
  1525. return gem_mdc_clk_div(bp);
  1526. pclk_hz = clk_get_rate(bp->pclk);
  1527. if (pclk_hz <= 20000000)
  1528. config = MACB_BF(CLK, MACB_CLK_DIV8);
  1529. else if (pclk_hz <= 40000000)
  1530. config = MACB_BF(CLK, MACB_CLK_DIV16);
  1531. else if (pclk_hz <= 80000000)
  1532. config = MACB_BF(CLK, MACB_CLK_DIV32);
  1533. else
  1534. config = MACB_BF(CLK, MACB_CLK_DIV64);
  1535. return config;
  1536. }
  1537. /* Get the DMA bus width field of the network configuration register that we
  1538. * should program. We find the width from decoding the design configuration
  1539. * register to find the maximum supported data bus width.
  1540. */
  1541. static u32 macb_dbw(struct macb *bp)
  1542. {
  1543. if (!macb_is_gem(bp))
  1544. return 0;
  1545. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  1546. case 4:
  1547. return GEM_BF(DBW, GEM_DBW128);
  1548. case 2:
  1549. return GEM_BF(DBW, GEM_DBW64);
  1550. case 1:
  1551. default:
  1552. return GEM_BF(DBW, GEM_DBW32);
  1553. }
  1554. }
  1555. /* Configure the receive DMA engine
  1556. * - use the correct receive buffer size
  1557. * - set best burst length for DMA operations
  1558. * (if not supported by FIFO, it will fallback to default)
  1559. * - set both rx/tx packet buffers to full memory size
  1560. * These are configurable parameters for GEM.
  1561. */
  1562. static void macb_configure_dma(struct macb *bp)
  1563. {
  1564. u32 dmacfg;
  1565. if (macb_is_gem(bp)) {
  1566. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  1567. dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
  1568. if (bp->dma_burst_length)
  1569. dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
  1570. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  1571. dmacfg &= ~GEM_BIT(ENDIA_PKT);
  1572. if (bp->native_io)
  1573. dmacfg &= ~GEM_BIT(ENDIA_DESC);
  1574. else
  1575. dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
  1576. if (bp->dev->features & NETIF_F_HW_CSUM)
  1577. dmacfg |= GEM_BIT(TXCOEN);
  1578. else
  1579. dmacfg &= ~GEM_BIT(TXCOEN);
  1580. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1581. if (bp->hw_dma_cap == HW_DMA_CAP_64B)
  1582. dmacfg |= GEM_BIT(ADDR64);
  1583. #endif
  1584. netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
  1585. dmacfg);
  1586. gem_writel(bp, DMACFG, dmacfg);
  1587. }
  1588. }
  1589. static void macb_init_hw(struct macb *bp)
  1590. {
  1591. struct macb_queue *queue;
  1592. unsigned int q;
  1593. u32 config;
  1594. macb_reset_hw(bp);
  1595. macb_set_hwaddr(bp);
  1596. config = macb_mdc_clk_div(bp);
  1597. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
  1598. config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  1599. config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
  1600. config |= MACB_BIT(PAE); /* PAuse Enable */
  1601. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  1602. if (bp->caps & MACB_CAPS_JUMBO)
  1603. config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
  1604. else
  1605. config |= MACB_BIT(BIG); /* Receive oversized frames */
  1606. if (bp->dev->flags & IFF_PROMISC)
  1607. config |= MACB_BIT(CAF); /* Copy All Frames */
  1608. else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
  1609. config |= GEM_BIT(RXCOEN);
  1610. if (!(bp->dev->flags & IFF_BROADCAST))
  1611. config |= MACB_BIT(NBC); /* No BroadCast */
  1612. config |= macb_dbw(bp);
  1613. macb_writel(bp, NCFGR, config);
  1614. if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
  1615. gem_writel(bp, JML, bp->jumbo_max_len);
  1616. bp->speed = SPEED_10;
  1617. bp->duplex = DUPLEX_HALF;
  1618. bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
  1619. if (bp->caps & MACB_CAPS_JUMBO)
  1620. bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
  1621. macb_configure_dma(bp);
  1622. /* Initialize TX and RX buffers */
  1623. macb_writel(bp, RBQP, lower_32_bits(bp->rx_ring_dma));
  1624. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1625. if (bp->hw_dma_cap == HW_DMA_CAP_64B)
  1626. macb_writel(bp, RBQPH, upper_32_bits(bp->rx_ring_dma));
  1627. #endif
  1628. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1629. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  1630. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1631. if (bp->hw_dma_cap == HW_DMA_CAP_64B)
  1632. queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
  1633. #endif
  1634. /* Enable interrupts */
  1635. queue_writel(queue, IER,
  1636. MACB_RX_INT_FLAGS |
  1637. MACB_TX_INT_FLAGS |
  1638. MACB_BIT(HRESP));
  1639. }
  1640. /* Enable TX and RX */
  1641. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  1642. }
  1643. /* The hash address register is 64 bits long and takes up two
  1644. * locations in the memory map. The least significant bits are stored
  1645. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  1646. *
  1647. * The unicast hash enable and the multicast hash enable bits in the
  1648. * network configuration register enable the reception of hash matched
  1649. * frames. The destination address is reduced to a 6 bit index into
  1650. * the 64 bit hash register using the following hash function. The
  1651. * hash function is an exclusive or of every sixth bit of the
  1652. * destination address.
  1653. *
  1654. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  1655. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  1656. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  1657. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  1658. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  1659. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  1660. *
  1661. * da[0] represents the least significant bit of the first byte
  1662. * received, that is, the multicast/unicast indicator, and da[47]
  1663. * represents the most significant bit of the last byte received. If
  1664. * the hash index, hi[n], points to a bit that is set in the hash
  1665. * register then the frame will be matched according to whether the
  1666. * frame is multicast or unicast. A multicast match will be signalled
  1667. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  1668. * index points to a bit set in the hash register. A unicast match
  1669. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  1670. * and the hash index points to a bit set in the hash register. To
  1671. * receive all multicast frames, the hash register should be set with
  1672. * all ones and the multicast hash enable bit should be set in the
  1673. * network configuration register.
  1674. */
  1675. static inline int hash_bit_value(int bitnr, __u8 *addr)
  1676. {
  1677. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  1678. return 1;
  1679. return 0;
  1680. }
  1681. /* Return the hash index value for the specified address. */
  1682. static int hash_get_index(__u8 *addr)
  1683. {
  1684. int i, j, bitval;
  1685. int hash_index = 0;
  1686. for (j = 0; j < 6; j++) {
  1687. for (i = 0, bitval = 0; i < 8; i++)
  1688. bitval ^= hash_bit_value(i * 6 + j, addr);
  1689. hash_index |= (bitval << j);
  1690. }
  1691. return hash_index;
  1692. }
  1693. /* Add multicast addresses to the internal multicast-hash table. */
  1694. static void macb_sethashtable(struct net_device *dev)
  1695. {
  1696. struct netdev_hw_addr *ha;
  1697. unsigned long mc_filter[2];
  1698. unsigned int bitnr;
  1699. struct macb *bp = netdev_priv(dev);
  1700. mc_filter[0] = 0;
  1701. mc_filter[1] = 0;
  1702. netdev_for_each_mc_addr(ha, dev) {
  1703. bitnr = hash_get_index(ha->addr);
  1704. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  1705. }
  1706. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  1707. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  1708. }
  1709. /* Enable/Disable promiscuous and multicast modes. */
  1710. static void macb_set_rx_mode(struct net_device *dev)
  1711. {
  1712. unsigned long cfg;
  1713. struct macb *bp = netdev_priv(dev);
  1714. cfg = macb_readl(bp, NCFGR);
  1715. if (dev->flags & IFF_PROMISC) {
  1716. /* Enable promiscuous mode */
  1717. cfg |= MACB_BIT(CAF);
  1718. /* Disable RX checksum offload */
  1719. if (macb_is_gem(bp))
  1720. cfg &= ~GEM_BIT(RXCOEN);
  1721. } else {
  1722. /* Disable promiscuous mode */
  1723. cfg &= ~MACB_BIT(CAF);
  1724. /* Enable RX checksum offload only if requested */
  1725. if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
  1726. cfg |= GEM_BIT(RXCOEN);
  1727. }
  1728. if (dev->flags & IFF_ALLMULTI) {
  1729. /* Enable all multicast mode */
  1730. macb_or_gem_writel(bp, HRB, -1);
  1731. macb_or_gem_writel(bp, HRT, -1);
  1732. cfg |= MACB_BIT(NCFGR_MTI);
  1733. } else if (!netdev_mc_empty(dev)) {
  1734. /* Enable specific multicasts */
  1735. macb_sethashtable(dev);
  1736. cfg |= MACB_BIT(NCFGR_MTI);
  1737. } else if (dev->flags & (~IFF_ALLMULTI)) {
  1738. /* Disable all multicast mode */
  1739. macb_or_gem_writel(bp, HRB, 0);
  1740. macb_or_gem_writel(bp, HRT, 0);
  1741. cfg &= ~MACB_BIT(NCFGR_MTI);
  1742. }
  1743. macb_writel(bp, NCFGR, cfg);
  1744. }
  1745. static int macb_open(struct net_device *dev)
  1746. {
  1747. struct macb *bp = netdev_priv(dev);
  1748. size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
  1749. int err;
  1750. netdev_dbg(bp->dev, "open\n");
  1751. /* carrier starts down */
  1752. netif_carrier_off(dev);
  1753. /* if the phy is not yet register, retry later*/
  1754. if (!dev->phydev)
  1755. return -EAGAIN;
  1756. /* RX buffers initialization */
  1757. macb_init_rx_buffer_size(bp, bufsz);
  1758. err = macb_alloc_consistent(bp);
  1759. if (err) {
  1760. netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
  1761. err);
  1762. return err;
  1763. }
  1764. napi_enable(&bp->napi);
  1765. bp->macbgem_ops.mog_init_rings(bp);
  1766. macb_init_hw(bp);
  1767. /* schedule a link state check */
  1768. phy_start(dev->phydev);
  1769. netif_tx_start_all_queues(dev);
  1770. if (bp->ptp_info)
  1771. bp->ptp_info->ptp_init(dev);
  1772. return 0;
  1773. }
  1774. static int macb_close(struct net_device *dev)
  1775. {
  1776. struct macb *bp = netdev_priv(dev);
  1777. unsigned long flags;
  1778. netif_tx_stop_all_queues(dev);
  1779. napi_disable(&bp->napi);
  1780. if (dev->phydev)
  1781. phy_stop(dev->phydev);
  1782. spin_lock_irqsave(&bp->lock, flags);
  1783. macb_reset_hw(bp);
  1784. netif_carrier_off(dev);
  1785. spin_unlock_irqrestore(&bp->lock, flags);
  1786. macb_free_consistent(bp);
  1787. if (bp->ptp_info)
  1788. bp->ptp_info->ptp_remove(dev);
  1789. return 0;
  1790. }
  1791. static int macb_change_mtu(struct net_device *dev, int new_mtu)
  1792. {
  1793. if (netif_running(dev))
  1794. return -EBUSY;
  1795. dev->mtu = new_mtu;
  1796. return 0;
  1797. }
  1798. static void gem_update_stats(struct macb *bp)
  1799. {
  1800. unsigned int i;
  1801. u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
  1802. for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
  1803. u32 offset = gem_statistics[i].offset;
  1804. u64 val = bp->macb_reg_readl(bp, offset);
  1805. bp->ethtool_stats[i] += val;
  1806. *p += val;
  1807. if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
  1808. /* Add GEM_OCTTXH, GEM_OCTRXH */
  1809. val = bp->macb_reg_readl(bp, offset + 4);
  1810. bp->ethtool_stats[i] += ((u64)val) << 32;
  1811. *(++p) += val;
  1812. }
  1813. }
  1814. }
  1815. static struct net_device_stats *gem_get_stats(struct macb *bp)
  1816. {
  1817. struct gem_stats *hwstat = &bp->hw_stats.gem;
  1818. struct net_device_stats *nstat = &bp->stats;
  1819. gem_update_stats(bp);
  1820. nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
  1821. hwstat->rx_alignment_errors +
  1822. hwstat->rx_resource_errors +
  1823. hwstat->rx_overruns +
  1824. hwstat->rx_oversize_frames +
  1825. hwstat->rx_jabbers +
  1826. hwstat->rx_undersized_frames +
  1827. hwstat->rx_length_field_frame_errors);
  1828. nstat->tx_errors = (hwstat->tx_late_collisions +
  1829. hwstat->tx_excessive_collisions +
  1830. hwstat->tx_underrun +
  1831. hwstat->tx_carrier_sense_errors);
  1832. nstat->multicast = hwstat->rx_multicast_frames;
  1833. nstat->collisions = (hwstat->tx_single_collision_frames +
  1834. hwstat->tx_multiple_collision_frames +
  1835. hwstat->tx_excessive_collisions);
  1836. nstat->rx_length_errors = (hwstat->rx_oversize_frames +
  1837. hwstat->rx_jabbers +
  1838. hwstat->rx_undersized_frames +
  1839. hwstat->rx_length_field_frame_errors);
  1840. nstat->rx_over_errors = hwstat->rx_resource_errors;
  1841. nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
  1842. nstat->rx_frame_errors = hwstat->rx_alignment_errors;
  1843. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1844. nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
  1845. nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
  1846. nstat->tx_fifo_errors = hwstat->tx_underrun;
  1847. return nstat;
  1848. }
  1849. static void gem_get_ethtool_stats(struct net_device *dev,
  1850. struct ethtool_stats *stats, u64 *data)
  1851. {
  1852. struct macb *bp;
  1853. bp = netdev_priv(dev);
  1854. gem_update_stats(bp);
  1855. memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
  1856. }
  1857. static int gem_get_sset_count(struct net_device *dev, int sset)
  1858. {
  1859. switch (sset) {
  1860. case ETH_SS_STATS:
  1861. return GEM_STATS_LEN;
  1862. default:
  1863. return -EOPNOTSUPP;
  1864. }
  1865. }
  1866. static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
  1867. {
  1868. unsigned int i;
  1869. switch (sset) {
  1870. case ETH_SS_STATS:
  1871. for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
  1872. memcpy(p, gem_statistics[i].stat_string,
  1873. ETH_GSTRING_LEN);
  1874. break;
  1875. }
  1876. }
  1877. static struct net_device_stats *macb_get_stats(struct net_device *dev)
  1878. {
  1879. struct macb *bp = netdev_priv(dev);
  1880. struct net_device_stats *nstat = &bp->stats;
  1881. struct macb_stats *hwstat = &bp->hw_stats.macb;
  1882. if (macb_is_gem(bp))
  1883. return gem_get_stats(bp);
  1884. /* read stats from hardware */
  1885. macb_update_stats(bp);
  1886. /* Convert HW stats into netdevice stats */
  1887. nstat->rx_errors = (hwstat->rx_fcs_errors +
  1888. hwstat->rx_align_errors +
  1889. hwstat->rx_resource_errors +
  1890. hwstat->rx_overruns +
  1891. hwstat->rx_oversize_pkts +
  1892. hwstat->rx_jabbers +
  1893. hwstat->rx_undersize_pkts +
  1894. hwstat->rx_length_mismatch);
  1895. nstat->tx_errors = (hwstat->tx_late_cols +
  1896. hwstat->tx_excessive_cols +
  1897. hwstat->tx_underruns +
  1898. hwstat->tx_carrier_errors +
  1899. hwstat->sqe_test_errors);
  1900. nstat->collisions = (hwstat->tx_single_cols +
  1901. hwstat->tx_multiple_cols +
  1902. hwstat->tx_excessive_cols);
  1903. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  1904. hwstat->rx_jabbers +
  1905. hwstat->rx_undersize_pkts +
  1906. hwstat->rx_length_mismatch);
  1907. nstat->rx_over_errors = hwstat->rx_resource_errors +
  1908. hwstat->rx_overruns;
  1909. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  1910. nstat->rx_frame_errors = hwstat->rx_align_errors;
  1911. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1912. /* XXX: What does "missed" mean? */
  1913. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  1914. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  1915. nstat->tx_fifo_errors = hwstat->tx_underruns;
  1916. /* Don't know about heartbeat or window errors... */
  1917. return nstat;
  1918. }
  1919. static int macb_get_regs_len(struct net_device *netdev)
  1920. {
  1921. return MACB_GREGS_NBR * sizeof(u32);
  1922. }
  1923. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1924. void *p)
  1925. {
  1926. struct macb *bp = netdev_priv(dev);
  1927. unsigned int tail, head;
  1928. u32 *regs_buff = p;
  1929. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  1930. | MACB_GREGS_VERSION;
  1931. tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
  1932. head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
  1933. regs_buff[0] = macb_readl(bp, NCR);
  1934. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  1935. regs_buff[2] = macb_readl(bp, NSR);
  1936. regs_buff[3] = macb_readl(bp, TSR);
  1937. regs_buff[4] = macb_readl(bp, RBQP);
  1938. regs_buff[5] = macb_readl(bp, TBQP);
  1939. regs_buff[6] = macb_readl(bp, RSR);
  1940. regs_buff[7] = macb_readl(bp, IMR);
  1941. regs_buff[8] = tail;
  1942. regs_buff[9] = head;
  1943. regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
  1944. regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
  1945. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
  1946. regs_buff[12] = macb_or_gem_readl(bp, USRIO);
  1947. if (macb_is_gem(bp))
  1948. regs_buff[13] = gem_readl(bp, DMACFG);
  1949. }
  1950. static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  1951. {
  1952. struct macb *bp = netdev_priv(netdev);
  1953. wol->supported = 0;
  1954. wol->wolopts = 0;
  1955. if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
  1956. wol->supported = WAKE_MAGIC;
  1957. if (bp->wol & MACB_WOL_ENABLED)
  1958. wol->wolopts |= WAKE_MAGIC;
  1959. }
  1960. }
  1961. static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  1962. {
  1963. struct macb *bp = netdev_priv(netdev);
  1964. if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
  1965. (wol->wolopts & ~WAKE_MAGIC))
  1966. return -EOPNOTSUPP;
  1967. if (wol->wolopts & WAKE_MAGIC)
  1968. bp->wol |= MACB_WOL_ENABLED;
  1969. else
  1970. bp->wol &= ~MACB_WOL_ENABLED;
  1971. device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
  1972. return 0;
  1973. }
  1974. static void macb_get_ringparam(struct net_device *netdev,
  1975. struct ethtool_ringparam *ring)
  1976. {
  1977. struct macb *bp = netdev_priv(netdev);
  1978. ring->rx_max_pending = MAX_RX_RING_SIZE;
  1979. ring->tx_max_pending = MAX_TX_RING_SIZE;
  1980. ring->rx_pending = bp->rx_ring_size;
  1981. ring->tx_pending = bp->tx_ring_size;
  1982. }
  1983. static int macb_set_ringparam(struct net_device *netdev,
  1984. struct ethtool_ringparam *ring)
  1985. {
  1986. struct macb *bp = netdev_priv(netdev);
  1987. u32 new_rx_size, new_tx_size;
  1988. unsigned int reset = 0;
  1989. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  1990. return -EINVAL;
  1991. new_rx_size = clamp_t(u32, ring->rx_pending,
  1992. MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
  1993. new_rx_size = roundup_pow_of_two(new_rx_size);
  1994. new_tx_size = clamp_t(u32, ring->tx_pending,
  1995. MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
  1996. new_tx_size = roundup_pow_of_two(new_tx_size);
  1997. if ((new_tx_size == bp->tx_ring_size) &&
  1998. (new_rx_size == bp->rx_ring_size)) {
  1999. /* nothing to do */
  2000. return 0;
  2001. }
  2002. if (netif_running(bp->dev)) {
  2003. reset = 1;
  2004. macb_close(bp->dev);
  2005. }
  2006. bp->rx_ring_size = new_rx_size;
  2007. bp->tx_ring_size = new_tx_size;
  2008. if (reset)
  2009. macb_open(bp->dev);
  2010. return 0;
  2011. }
  2012. static int macb_get_ts_info(struct net_device *netdev,
  2013. struct ethtool_ts_info *info)
  2014. {
  2015. struct macb *bp = netdev_priv(netdev);
  2016. if (bp->ptp_info)
  2017. return bp->ptp_info->get_ts_info(netdev, info);
  2018. return ethtool_op_get_ts_info(netdev, info);
  2019. }
  2020. static const struct ethtool_ops macb_ethtool_ops = {
  2021. .get_regs_len = macb_get_regs_len,
  2022. .get_regs = macb_get_regs,
  2023. .get_link = ethtool_op_get_link,
  2024. .get_ts_info = ethtool_op_get_ts_info,
  2025. .get_wol = macb_get_wol,
  2026. .set_wol = macb_set_wol,
  2027. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2028. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2029. .get_ringparam = macb_get_ringparam,
  2030. .set_ringparam = macb_set_ringparam,
  2031. };
  2032. static const struct ethtool_ops gem_ethtool_ops = {
  2033. .get_regs_len = macb_get_regs_len,
  2034. .get_regs = macb_get_regs,
  2035. .get_link = ethtool_op_get_link,
  2036. .get_ts_info = macb_get_ts_info,
  2037. .get_ethtool_stats = gem_get_ethtool_stats,
  2038. .get_strings = gem_get_ethtool_strings,
  2039. .get_sset_count = gem_get_sset_count,
  2040. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2041. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2042. .get_ringparam = macb_get_ringparam,
  2043. .set_ringparam = macb_set_ringparam,
  2044. };
  2045. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2046. {
  2047. struct phy_device *phydev = dev->phydev;
  2048. struct macb *bp = netdev_priv(dev);
  2049. if (!netif_running(dev))
  2050. return -EINVAL;
  2051. if (!phydev)
  2052. return -ENODEV;
  2053. if (!bp->ptp_info)
  2054. return phy_mii_ioctl(phydev, rq, cmd);
  2055. switch (cmd) {
  2056. case SIOCSHWTSTAMP:
  2057. return bp->ptp_info->set_hwtst(dev, rq, cmd);
  2058. case SIOCGHWTSTAMP:
  2059. return bp->ptp_info->get_hwtst(dev, rq);
  2060. default:
  2061. return phy_mii_ioctl(phydev, rq, cmd);
  2062. }
  2063. }
  2064. static int macb_set_features(struct net_device *netdev,
  2065. netdev_features_t features)
  2066. {
  2067. struct macb *bp = netdev_priv(netdev);
  2068. netdev_features_t changed = features ^ netdev->features;
  2069. /* TX checksum offload */
  2070. if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
  2071. u32 dmacfg;
  2072. dmacfg = gem_readl(bp, DMACFG);
  2073. if (features & NETIF_F_HW_CSUM)
  2074. dmacfg |= GEM_BIT(TXCOEN);
  2075. else
  2076. dmacfg &= ~GEM_BIT(TXCOEN);
  2077. gem_writel(bp, DMACFG, dmacfg);
  2078. }
  2079. /* RX checksum offload */
  2080. if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
  2081. u32 netcfg;
  2082. netcfg = gem_readl(bp, NCFGR);
  2083. if (features & NETIF_F_RXCSUM &&
  2084. !(netdev->flags & IFF_PROMISC))
  2085. netcfg |= GEM_BIT(RXCOEN);
  2086. else
  2087. netcfg &= ~GEM_BIT(RXCOEN);
  2088. gem_writel(bp, NCFGR, netcfg);
  2089. }
  2090. return 0;
  2091. }
  2092. static const struct net_device_ops macb_netdev_ops = {
  2093. .ndo_open = macb_open,
  2094. .ndo_stop = macb_close,
  2095. .ndo_start_xmit = macb_start_xmit,
  2096. .ndo_set_rx_mode = macb_set_rx_mode,
  2097. .ndo_get_stats = macb_get_stats,
  2098. .ndo_do_ioctl = macb_ioctl,
  2099. .ndo_validate_addr = eth_validate_addr,
  2100. .ndo_change_mtu = macb_change_mtu,
  2101. .ndo_set_mac_address = eth_mac_addr,
  2102. #ifdef CONFIG_NET_POLL_CONTROLLER
  2103. .ndo_poll_controller = macb_poll_controller,
  2104. #endif
  2105. .ndo_set_features = macb_set_features,
  2106. .ndo_features_check = macb_features_check,
  2107. };
  2108. /* Configure peripheral capabilities according to device tree
  2109. * and integration options used
  2110. */
  2111. static void macb_configure_caps(struct macb *bp,
  2112. const struct macb_config *dt_conf)
  2113. {
  2114. u32 dcfg;
  2115. if (dt_conf)
  2116. bp->caps = dt_conf->caps;
  2117. if (hw_is_gem(bp->regs, bp->native_io)) {
  2118. bp->caps |= MACB_CAPS_MACB_IS_GEM;
  2119. dcfg = gem_readl(bp, DCFG1);
  2120. if (GEM_BFEXT(IRQCOR, dcfg) == 0)
  2121. bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
  2122. dcfg = gem_readl(bp, DCFG2);
  2123. if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
  2124. bp->caps |= MACB_CAPS_FIFO_MODE;
  2125. }
  2126. dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
  2127. }
  2128. static void macb_probe_queues(void __iomem *mem,
  2129. bool native_io,
  2130. unsigned int *queue_mask,
  2131. unsigned int *num_queues)
  2132. {
  2133. unsigned int hw_q;
  2134. *queue_mask = 0x1;
  2135. *num_queues = 1;
  2136. /* is it macb or gem ?
  2137. *
  2138. * We need to read directly from the hardware here because
  2139. * we are early in the probe process and don't have the
  2140. * MACB_CAPS_MACB_IS_GEM flag positioned
  2141. */
  2142. if (!hw_is_gem(mem, native_io))
  2143. return;
  2144. /* bit 0 is never set but queue 0 always exists */
  2145. *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
  2146. *queue_mask |= 0x1;
  2147. for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
  2148. if (*queue_mask & (1 << hw_q))
  2149. (*num_queues)++;
  2150. }
  2151. static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
  2152. struct clk **hclk, struct clk **tx_clk,
  2153. struct clk **rx_clk)
  2154. {
  2155. struct macb_platform_data *pdata;
  2156. int err;
  2157. pdata = dev_get_platdata(&pdev->dev);
  2158. if (pdata) {
  2159. *pclk = pdata->pclk;
  2160. *hclk = pdata->hclk;
  2161. } else {
  2162. *pclk = devm_clk_get(&pdev->dev, "pclk");
  2163. *hclk = devm_clk_get(&pdev->dev, "hclk");
  2164. }
  2165. if (IS_ERR(*pclk)) {
  2166. err = PTR_ERR(*pclk);
  2167. dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
  2168. return err;
  2169. }
  2170. if (IS_ERR(*hclk)) {
  2171. err = PTR_ERR(*hclk);
  2172. dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
  2173. return err;
  2174. }
  2175. *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
  2176. if (IS_ERR(*tx_clk))
  2177. *tx_clk = NULL;
  2178. *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
  2179. if (IS_ERR(*rx_clk))
  2180. *rx_clk = NULL;
  2181. err = clk_prepare_enable(*pclk);
  2182. if (err) {
  2183. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  2184. return err;
  2185. }
  2186. err = clk_prepare_enable(*hclk);
  2187. if (err) {
  2188. dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
  2189. goto err_disable_pclk;
  2190. }
  2191. err = clk_prepare_enable(*tx_clk);
  2192. if (err) {
  2193. dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
  2194. goto err_disable_hclk;
  2195. }
  2196. err = clk_prepare_enable(*rx_clk);
  2197. if (err) {
  2198. dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
  2199. goto err_disable_txclk;
  2200. }
  2201. return 0;
  2202. err_disable_txclk:
  2203. clk_disable_unprepare(*tx_clk);
  2204. err_disable_hclk:
  2205. clk_disable_unprepare(*hclk);
  2206. err_disable_pclk:
  2207. clk_disable_unprepare(*pclk);
  2208. return err;
  2209. }
  2210. static int macb_init(struct platform_device *pdev)
  2211. {
  2212. struct net_device *dev = platform_get_drvdata(pdev);
  2213. unsigned int hw_q, q;
  2214. struct macb *bp = netdev_priv(dev);
  2215. struct macb_queue *queue;
  2216. int err;
  2217. u32 val;
  2218. bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
  2219. bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
  2220. /* set the queue register mapping once for all: queue0 has a special
  2221. * register mapping but we don't want to test the queue index then
  2222. * compute the corresponding register offset at run time.
  2223. */
  2224. for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
  2225. if (!(bp->queue_mask & (1 << hw_q)))
  2226. continue;
  2227. queue = &bp->queues[q];
  2228. queue->bp = bp;
  2229. if (hw_q) {
  2230. queue->ISR = GEM_ISR(hw_q - 1);
  2231. queue->IER = GEM_IER(hw_q - 1);
  2232. queue->IDR = GEM_IDR(hw_q - 1);
  2233. queue->IMR = GEM_IMR(hw_q - 1);
  2234. queue->TBQP = GEM_TBQP(hw_q - 1);
  2235. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  2236. if (bp->hw_dma_cap == HW_DMA_CAP_64B)
  2237. queue->TBQPH = GEM_TBQPH(hw_q - 1);
  2238. #endif
  2239. } else {
  2240. /* queue0 uses legacy registers */
  2241. queue->ISR = MACB_ISR;
  2242. queue->IER = MACB_IER;
  2243. queue->IDR = MACB_IDR;
  2244. queue->IMR = MACB_IMR;
  2245. queue->TBQP = MACB_TBQP;
  2246. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  2247. if (bp->hw_dma_cap == HW_DMA_CAP_64B)
  2248. queue->TBQPH = MACB_TBQPH;
  2249. #endif
  2250. }
  2251. /* get irq: here we use the linux queue index, not the hardware
  2252. * queue index. the queue irq definitions in the device tree
  2253. * must remove the optional gaps that could exist in the
  2254. * hardware queue mask.
  2255. */
  2256. queue->irq = platform_get_irq(pdev, q);
  2257. err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
  2258. IRQF_SHARED, dev->name, queue);
  2259. if (err) {
  2260. dev_err(&pdev->dev,
  2261. "Unable to request IRQ %d (error %d)\n",
  2262. queue->irq, err);
  2263. return err;
  2264. }
  2265. INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
  2266. q++;
  2267. }
  2268. dev->netdev_ops = &macb_netdev_ops;
  2269. netif_napi_add(dev, &bp->napi, macb_poll, 64);
  2270. /* setup appropriated routines according to adapter type */
  2271. if (macb_is_gem(bp)) {
  2272. bp->max_tx_length = GEM_MAX_TX_LEN;
  2273. bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
  2274. bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
  2275. bp->macbgem_ops.mog_init_rings = gem_init_rings;
  2276. bp->macbgem_ops.mog_rx = gem_rx;
  2277. dev->ethtool_ops = &gem_ethtool_ops;
  2278. } else {
  2279. bp->max_tx_length = MACB_MAX_TX_LEN;
  2280. bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
  2281. bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
  2282. bp->macbgem_ops.mog_init_rings = macb_init_rings;
  2283. bp->macbgem_ops.mog_rx = macb_rx;
  2284. dev->ethtool_ops = &macb_ethtool_ops;
  2285. }
  2286. /* Set features */
  2287. dev->hw_features = NETIF_F_SG;
  2288. /* Check LSO capability */
  2289. if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
  2290. dev->hw_features |= MACB_NETIF_LSO;
  2291. /* Checksum offload is only available on gem with packet buffer */
  2292. if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
  2293. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  2294. if (bp->caps & MACB_CAPS_SG_DISABLED)
  2295. dev->hw_features &= ~NETIF_F_SG;
  2296. dev->features = dev->hw_features;
  2297. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
  2298. val = 0;
  2299. if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
  2300. val = GEM_BIT(RGMII);
  2301. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
  2302. (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  2303. val = MACB_BIT(RMII);
  2304. else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  2305. val = MACB_BIT(MII);
  2306. if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
  2307. val |= MACB_BIT(CLKEN);
  2308. macb_or_gem_writel(bp, USRIO, val);
  2309. }
  2310. /* Set MII management clock divider */
  2311. val = macb_mdc_clk_div(bp);
  2312. val |= macb_dbw(bp);
  2313. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
  2314. val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  2315. macb_writel(bp, NCFGR, val);
  2316. return 0;
  2317. }
  2318. #if defined(CONFIG_OF)
  2319. /* 1518 rounded up */
  2320. #define AT91ETHER_MAX_RBUFF_SZ 0x600
  2321. /* max number of receive buffers */
  2322. #define AT91ETHER_MAX_RX_DESCR 9
  2323. /* Initialize and start the Receiver and Transmit subsystems */
  2324. static int at91ether_start(struct net_device *dev)
  2325. {
  2326. struct macb *lp = netdev_priv(dev);
  2327. struct macb_dma_desc *desc;
  2328. dma_addr_t addr;
  2329. u32 ctl;
  2330. int i;
  2331. lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
  2332. (AT91ETHER_MAX_RX_DESCR *
  2333. macb_dma_desc_get_size(lp)),
  2334. &lp->rx_ring_dma, GFP_KERNEL);
  2335. if (!lp->rx_ring)
  2336. return -ENOMEM;
  2337. lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
  2338. AT91ETHER_MAX_RX_DESCR *
  2339. AT91ETHER_MAX_RBUFF_SZ,
  2340. &lp->rx_buffers_dma, GFP_KERNEL);
  2341. if (!lp->rx_buffers) {
  2342. dma_free_coherent(&lp->pdev->dev,
  2343. AT91ETHER_MAX_RX_DESCR *
  2344. macb_dma_desc_get_size(lp),
  2345. lp->rx_ring, lp->rx_ring_dma);
  2346. lp->rx_ring = NULL;
  2347. return -ENOMEM;
  2348. }
  2349. addr = lp->rx_buffers_dma;
  2350. for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
  2351. desc = macb_rx_desc(lp, i);
  2352. macb_set_addr(lp, desc, addr);
  2353. desc->ctrl = 0;
  2354. addr += AT91ETHER_MAX_RBUFF_SZ;
  2355. }
  2356. /* Set the Wrap bit on the last descriptor */
  2357. desc->addr |= MACB_BIT(RX_WRAP);
  2358. /* Reset buffer index */
  2359. lp->rx_tail = 0;
  2360. /* Program address of descriptor list in Rx Buffer Queue register */
  2361. macb_writel(lp, RBQP, lp->rx_ring_dma);
  2362. /* Enable Receive and Transmit */
  2363. ctl = macb_readl(lp, NCR);
  2364. macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
  2365. return 0;
  2366. }
  2367. /* Open the ethernet interface */
  2368. static int at91ether_open(struct net_device *dev)
  2369. {
  2370. struct macb *lp = netdev_priv(dev);
  2371. u32 ctl;
  2372. int ret;
  2373. /* Clear internal statistics */
  2374. ctl = macb_readl(lp, NCR);
  2375. macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
  2376. macb_set_hwaddr(lp);
  2377. ret = at91ether_start(dev);
  2378. if (ret)
  2379. return ret;
  2380. /* Enable MAC interrupts */
  2381. macb_writel(lp, IER, MACB_BIT(RCOMP) |
  2382. MACB_BIT(RXUBR) |
  2383. MACB_BIT(ISR_TUND) |
  2384. MACB_BIT(ISR_RLE) |
  2385. MACB_BIT(TCOMP) |
  2386. MACB_BIT(ISR_ROVR) |
  2387. MACB_BIT(HRESP));
  2388. /* schedule a link state check */
  2389. phy_start(dev->phydev);
  2390. netif_start_queue(dev);
  2391. return 0;
  2392. }
  2393. /* Close the interface */
  2394. static int at91ether_close(struct net_device *dev)
  2395. {
  2396. struct macb *lp = netdev_priv(dev);
  2397. u32 ctl;
  2398. /* Disable Receiver and Transmitter */
  2399. ctl = macb_readl(lp, NCR);
  2400. macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
  2401. /* Disable MAC interrupts */
  2402. macb_writel(lp, IDR, MACB_BIT(RCOMP) |
  2403. MACB_BIT(RXUBR) |
  2404. MACB_BIT(ISR_TUND) |
  2405. MACB_BIT(ISR_RLE) |
  2406. MACB_BIT(TCOMP) |
  2407. MACB_BIT(ISR_ROVR) |
  2408. MACB_BIT(HRESP));
  2409. netif_stop_queue(dev);
  2410. dma_free_coherent(&lp->pdev->dev,
  2411. AT91ETHER_MAX_RX_DESCR *
  2412. macb_dma_desc_get_size(lp),
  2413. lp->rx_ring, lp->rx_ring_dma);
  2414. lp->rx_ring = NULL;
  2415. dma_free_coherent(&lp->pdev->dev,
  2416. AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
  2417. lp->rx_buffers, lp->rx_buffers_dma);
  2418. lp->rx_buffers = NULL;
  2419. return 0;
  2420. }
  2421. /* Transmit packet */
  2422. static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2423. {
  2424. struct macb *lp = netdev_priv(dev);
  2425. if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
  2426. netif_stop_queue(dev);
  2427. /* Store packet information (to free when Tx completed) */
  2428. lp->skb = skb;
  2429. lp->skb_length = skb->len;
  2430. lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
  2431. DMA_TO_DEVICE);
  2432. if (dma_mapping_error(NULL, lp->skb_physaddr)) {
  2433. dev_kfree_skb_any(skb);
  2434. dev->stats.tx_dropped++;
  2435. netdev_err(dev, "%s: DMA mapping error\n", __func__);
  2436. return NETDEV_TX_OK;
  2437. }
  2438. /* Set address of the data in the Transmit Address register */
  2439. macb_writel(lp, TAR, lp->skb_physaddr);
  2440. /* Set length of the packet in the Transmit Control register */
  2441. macb_writel(lp, TCR, skb->len);
  2442. } else {
  2443. netdev_err(dev, "%s called, but device is busy!\n", __func__);
  2444. return NETDEV_TX_BUSY;
  2445. }
  2446. return NETDEV_TX_OK;
  2447. }
  2448. /* Extract received frame from buffer descriptors and sent to upper layers.
  2449. * (Called from interrupt context)
  2450. */
  2451. static void at91ether_rx(struct net_device *dev)
  2452. {
  2453. struct macb *lp = netdev_priv(dev);
  2454. struct macb_dma_desc *desc;
  2455. unsigned char *p_recv;
  2456. struct sk_buff *skb;
  2457. unsigned int pktlen;
  2458. desc = macb_rx_desc(lp, lp->rx_tail);
  2459. while (desc->addr & MACB_BIT(RX_USED)) {
  2460. p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
  2461. pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
  2462. skb = netdev_alloc_skb(dev, pktlen + 2);
  2463. if (skb) {
  2464. skb_reserve(skb, 2);
  2465. memcpy(skb_put(skb, pktlen), p_recv, pktlen);
  2466. skb->protocol = eth_type_trans(skb, dev);
  2467. lp->stats.rx_packets++;
  2468. lp->stats.rx_bytes += pktlen;
  2469. netif_rx(skb);
  2470. } else {
  2471. lp->stats.rx_dropped++;
  2472. }
  2473. if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
  2474. lp->stats.multicast++;
  2475. /* reset ownership bit */
  2476. desc->addr &= ~MACB_BIT(RX_USED);
  2477. /* wrap after last buffer */
  2478. if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
  2479. lp->rx_tail = 0;
  2480. else
  2481. lp->rx_tail++;
  2482. desc = macb_rx_desc(lp, lp->rx_tail);
  2483. }
  2484. }
  2485. /* MAC interrupt handler */
  2486. static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
  2487. {
  2488. struct net_device *dev = dev_id;
  2489. struct macb *lp = netdev_priv(dev);
  2490. u32 intstatus, ctl;
  2491. /* MAC Interrupt Status register indicates what interrupts are pending.
  2492. * It is automatically cleared once read.
  2493. */
  2494. intstatus = macb_readl(lp, ISR);
  2495. /* Receive complete */
  2496. if (intstatus & MACB_BIT(RCOMP))
  2497. at91ether_rx(dev);
  2498. /* Transmit complete */
  2499. if (intstatus & MACB_BIT(TCOMP)) {
  2500. /* The TCOM bit is set even if the transmission failed */
  2501. if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
  2502. lp->stats.tx_errors++;
  2503. if (lp->skb) {
  2504. dev_kfree_skb_irq(lp->skb);
  2505. lp->skb = NULL;
  2506. dma_unmap_single(NULL, lp->skb_physaddr,
  2507. lp->skb_length, DMA_TO_DEVICE);
  2508. lp->stats.tx_packets++;
  2509. lp->stats.tx_bytes += lp->skb_length;
  2510. }
  2511. netif_wake_queue(dev);
  2512. }
  2513. /* Work-around for EMAC Errata section 41.3.1 */
  2514. if (intstatus & MACB_BIT(RXUBR)) {
  2515. ctl = macb_readl(lp, NCR);
  2516. macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
  2517. wmb();
  2518. macb_writel(lp, NCR, ctl | MACB_BIT(RE));
  2519. }
  2520. if (intstatus & MACB_BIT(ISR_ROVR))
  2521. netdev_err(dev, "ROVR error\n");
  2522. return IRQ_HANDLED;
  2523. }
  2524. #ifdef CONFIG_NET_POLL_CONTROLLER
  2525. static void at91ether_poll_controller(struct net_device *dev)
  2526. {
  2527. unsigned long flags;
  2528. local_irq_save(flags);
  2529. at91ether_interrupt(dev->irq, dev);
  2530. local_irq_restore(flags);
  2531. }
  2532. #endif
  2533. static const struct net_device_ops at91ether_netdev_ops = {
  2534. .ndo_open = at91ether_open,
  2535. .ndo_stop = at91ether_close,
  2536. .ndo_start_xmit = at91ether_start_xmit,
  2537. .ndo_get_stats = macb_get_stats,
  2538. .ndo_set_rx_mode = macb_set_rx_mode,
  2539. .ndo_set_mac_address = eth_mac_addr,
  2540. .ndo_do_ioctl = macb_ioctl,
  2541. .ndo_validate_addr = eth_validate_addr,
  2542. #ifdef CONFIG_NET_POLL_CONTROLLER
  2543. .ndo_poll_controller = at91ether_poll_controller,
  2544. #endif
  2545. };
  2546. static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
  2547. struct clk **hclk, struct clk **tx_clk,
  2548. struct clk **rx_clk)
  2549. {
  2550. int err;
  2551. *hclk = NULL;
  2552. *tx_clk = NULL;
  2553. *rx_clk = NULL;
  2554. *pclk = devm_clk_get(&pdev->dev, "ether_clk");
  2555. if (IS_ERR(*pclk))
  2556. return PTR_ERR(*pclk);
  2557. err = clk_prepare_enable(*pclk);
  2558. if (err) {
  2559. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  2560. return err;
  2561. }
  2562. return 0;
  2563. }
  2564. static int at91ether_init(struct platform_device *pdev)
  2565. {
  2566. struct net_device *dev = platform_get_drvdata(pdev);
  2567. struct macb *bp = netdev_priv(dev);
  2568. int err;
  2569. u32 reg;
  2570. dev->netdev_ops = &at91ether_netdev_ops;
  2571. dev->ethtool_ops = &macb_ethtool_ops;
  2572. err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
  2573. 0, dev->name, dev);
  2574. if (err)
  2575. return err;
  2576. macb_writel(bp, NCR, 0);
  2577. reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
  2578. if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
  2579. reg |= MACB_BIT(RM9200_RMII);
  2580. macb_writel(bp, NCFGR, reg);
  2581. return 0;
  2582. }
  2583. static const struct macb_config at91sam9260_config = {
  2584. .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  2585. .clk_init = macb_clk_init,
  2586. .init = macb_init,
  2587. };
  2588. static const struct macb_config pc302gem_config = {
  2589. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  2590. .dma_burst_length = 16,
  2591. .clk_init = macb_clk_init,
  2592. .init = macb_init,
  2593. };
  2594. static const struct macb_config sama5d2_config = {
  2595. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  2596. .dma_burst_length = 16,
  2597. .clk_init = macb_clk_init,
  2598. .init = macb_init,
  2599. };
  2600. static const struct macb_config sama5d3_config = {
  2601. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
  2602. | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  2603. .dma_burst_length = 16,
  2604. .clk_init = macb_clk_init,
  2605. .init = macb_init,
  2606. };
  2607. static const struct macb_config sama5d4_config = {
  2608. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  2609. .dma_burst_length = 4,
  2610. .clk_init = macb_clk_init,
  2611. .init = macb_init,
  2612. };
  2613. static const struct macb_config emac_config = {
  2614. .clk_init = at91ether_clk_init,
  2615. .init = at91ether_init,
  2616. };
  2617. static const struct macb_config np4_config = {
  2618. .caps = MACB_CAPS_USRIO_DISABLED,
  2619. .clk_init = macb_clk_init,
  2620. .init = macb_init,
  2621. };
  2622. static const struct macb_config zynqmp_config = {
  2623. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
  2624. .dma_burst_length = 16,
  2625. .clk_init = macb_clk_init,
  2626. .init = macb_init,
  2627. .jumbo_max_len = 10240,
  2628. };
  2629. static const struct macb_config zynq_config = {
  2630. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
  2631. .dma_burst_length = 16,
  2632. .clk_init = macb_clk_init,
  2633. .init = macb_init,
  2634. };
  2635. static const struct of_device_id macb_dt_ids[] = {
  2636. { .compatible = "cdns,at32ap7000-macb" },
  2637. { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
  2638. { .compatible = "cdns,macb" },
  2639. { .compatible = "cdns,np4-macb", .data = &np4_config },
  2640. { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
  2641. { .compatible = "cdns,gem", .data = &pc302gem_config },
  2642. { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
  2643. { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
  2644. { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
  2645. { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
  2646. { .compatible = "cdns,emac", .data = &emac_config },
  2647. { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
  2648. { .compatible = "cdns,zynq-gem", .data = &zynq_config },
  2649. { /* sentinel */ }
  2650. };
  2651. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  2652. #endif /* CONFIG_OF */
  2653. static const struct macb_config default_gem_config = {
  2654. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
  2655. .dma_burst_length = 16,
  2656. .clk_init = macb_clk_init,
  2657. .init = macb_init,
  2658. .jumbo_max_len = 10240,
  2659. };
  2660. static int macb_probe(struct platform_device *pdev)
  2661. {
  2662. const struct macb_config *macb_config = &default_gem_config;
  2663. int (*clk_init)(struct platform_device *, struct clk **,
  2664. struct clk **, struct clk **, struct clk **)
  2665. = macb_config->clk_init;
  2666. int (*init)(struct platform_device *) = macb_config->init;
  2667. struct device_node *np = pdev->dev.of_node;
  2668. struct device_node *phy_node;
  2669. struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
  2670. unsigned int queue_mask, num_queues;
  2671. struct macb_platform_data *pdata;
  2672. bool native_io;
  2673. struct phy_device *phydev;
  2674. struct net_device *dev;
  2675. struct resource *regs;
  2676. void __iomem *mem;
  2677. const char *mac;
  2678. struct macb *bp;
  2679. int err;
  2680. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2681. mem = devm_ioremap_resource(&pdev->dev, regs);
  2682. if (IS_ERR(mem))
  2683. return PTR_ERR(mem);
  2684. if (np) {
  2685. const struct of_device_id *match;
  2686. match = of_match_node(macb_dt_ids, np);
  2687. if (match && match->data) {
  2688. macb_config = match->data;
  2689. clk_init = macb_config->clk_init;
  2690. init = macb_config->init;
  2691. }
  2692. }
  2693. err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
  2694. if (err)
  2695. return err;
  2696. native_io = hw_is_native_io(mem);
  2697. macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
  2698. dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
  2699. if (!dev) {
  2700. err = -ENOMEM;
  2701. goto err_disable_clocks;
  2702. }
  2703. dev->base_addr = regs->start;
  2704. SET_NETDEV_DEV(dev, &pdev->dev);
  2705. bp = netdev_priv(dev);
  2706. bp->pdev = pdev;
  2707. bp->dev = dev;
  2708. bp->regs = mem;
  2709. bp->native_io = native_io;
  2710. if (native_io) {
  2711. bp->macb_reg_readl = hw_readl_native;
  2712. bp->macb_reg_writel = hw_writel_native;
  2713. } else {
  2714. bp->macb_reg_readl = hw_readl;
  2715. bp->macb_reg_writel = hw_writel;
  2716. }
  2717. bp->num_queues = num_queues;
  2718. bp->queue_mask = queue_mask;
  2719. if (macb_config)
  2720. bp->dma_burst_length = macb_config->dma_burst_length;
  2721. bp->pclk = pclk;
  2722. bp->hclk = hclk;
  2723. bp->tx_clk = tx_clk;
  2724. bp->rx_clk = rx_clk;
  2725. if (macb_config)
  2726. bp->jumbo_max_len = macb_config->jumbo_max_len;
  2727. bp->wol = 0;
  2728. if (of_get_property(np, "magic-packet", NULL))
  2729. bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
  2730. device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
  2731. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  2732. if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
  2733. dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
  2734. bp->hw_dma_cap = HW_DMA_CAP_64B;
  2735. } else
  2736. bp->hw_dma_cap = HW_DMA_CAP_32B;
  2737. #endif
  2738. spin_lock_init(&bp->lock);
  2739. /* setup capabilities */
  2740. macb_configure_caps(bp, macb_config);
  2741. platform_set_drvdata(pdev, dev);
  2742. dev->irq = platform_get_irq(pdev, 0);
  2743. if (dev->irq < 0) {
  2744. err = dev->irq;
  2745. goto err_out_free_netdev;
  2746. }
  2747. /* MTU range: 68 - 1500 or 10240 */
  2748. dev->min_mtu = GEM_MTU_MIN_SIZE;
  2749. if (bp->caps & MACB_CAPS_JUMBO)
  2750. dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
  2751. else
  2752. dev->max_mtu = ETH_DATA_LEN;
  2753. mac = of_get_mac_address(np);
  2754. if (mac)
  2755. ether_addr_copy(bp->dev->dev_addr, mac);
  2756. else
  2757. macb_get_hwaddr(bp);
  2758. /* Power up the PHY if there is a GPIO reset */
  2759. phy_node = of_get_next_available_child(np, NULL);
  2760. if (phy_node) {
  2761. int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
  2762. if (gpio_is_valid(gpio)) {
  2763. bp->reset_gpio = gpio_to_desc(gpio);
  2764. gpiod_direction_output(bp->reset_gpio, 1);
  2765. }
  2766. }
  2767. of_node_put(phy_node);
  2768. err = of_get_phy_mode(np);
  2769. if (err < 0) {
  2770. pdata = dev_get_platdata(&pdev->dev);
  2771. if (pdata && pdata->is_rmii)
  2772. bp->phy_interface = PHY_INTERFACE_MODE_RMII;
  2773. else
  2774. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  2775. } else {
  2776. bp->phy_interface = err;
  2777. }
  2778. /* IP specific init */
  2779. err = init(pdev);
  2780. if (err)
  2781. goto err_out_free_netdev;
  2782. err = macb_mii_init(bp);
  2783. if (err)
  2784. goto err_out_free_netdev;
  2785. phydev = dev->phydev;
  2786. netif_carrier_off(dev);
  2787. err = register_netdev(dev);
  2788. if (err) {
  2789. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  2790. goto err_out_unregister_mdio;
  2791. }
  2792. phy_attached_info(phydev);
  2793. netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
  2794. macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
  2795. dev->base_addr, dev->irq, dev->dev_addr);
  2796. return 0;
  2797. err_out_unregister_mdio:
  2798. phy_disconnect(dev->phydev);
  2799. mdiobus_unregister(bp->mii_bus);
  2800. mdiobus_free(bp->mii_bus);
  2801. /* Shutdown the PHY if there is a GPIO reset */
  2802. if (bp->reset_gpio)
  2803. gpiod_set_value(bp->reset_gpio, 0);
  2804. err_out_free_netdev:
  2805. free_netdev(dev);
  2806. err_disable_clocks:
  2807. clk_disable_unprepare(tx_clk);
  2808. clk_disable_unprepare(hclk);
  2809. clk_disable_unprepare(pclk);
  2810. clk_disable_unprepare(rx_clk);
  2811. return err;
  2812. }
  2813. static int macb_remove(struct platform_device *pdev)
  2814. {
  2815. struct net_device *dev;
  2816. struct macb *bp;
  2817. dev = platform_get_drvdata(pdev);
  2818. if (dev) {
  2819. bp = netdev_priv(dev);
  2820. if (dev->phydev)
  2821. phy_disconnect(dev->phydev);
  2822. mdiobus_unregister(bp->mii_bus);
  2823. dev->phydev = NULL;
  2824. mdiobus_free(bp->mii_bus);
  2825. /* Shutdown the PHY if there is a GPIO reset */
  2826. if (bp->reset_gpio)
  2827. gpiod_set_value(bp->reset_gpio, 0);
  2828. unregister_netdev(dev);
  2829. clk_disable_unprepare(bp->tx_clk);
  2830. clk_disable_unprepare(bp->hclk);
  2831. clk_disable_unprepare(bp->pclk);
  2832. clk_disable_unprepare(bp->rx_clk);
  2833. free_netdev(dev);
  2834. }
  2835. return 0;
  2836. }
  2837. static int __maybe_unused macb_suspend(struct device *dev)
  2838. {
  2839. struct platform_device *pdev = to_platform_device(dev);
  2840. struct net_device *netdev = platform_get_drvdata(pdev);
  2841. struct macb *bp = netdev_priv(netdev);
  2842. netif_carrier_off(netdev);
  2843. netif_device_detach(netdev);
  2844. if (bp->wol & MACB_WOL_ENABLED) {
  2845. macb_writel(bp, IER, MACB_BIT(WOL));
  2846. macb_writel(bp, WOL, MACB_BIT(MAG));
  2847. enable_irq_wake(bp->queues[0].irq);
  2848. } else {
  2849. clk_disable_unprepare(bp->tx_clk);
  2850. clk_disable_unprepare(bp->hclk);
  2851. clk_disable_unprepare(bp->pclk);
  2852. clk_disable_unprepare(bp->rx_clk);
  2853. }
  2854. return 0;
  2855. }
  2856. static int __maybe_unused macb_resume(struct device *dev)
  2857. {
  2858. struct platform_device *pdev = to_platform_device(dev);
  2859. struct net_device *netdev = platform_get_drvdata(pdev);
  2860. struct macb *bp = netdev_priv(netdev);
  2861. if (bp->wol & MACB_WOL_ENABLED) {
  2862. macb_writel(bp, IDR, MACB_BIT(WOL));
  2863. macb_writel(bp, WOL, 0);
  2864. disable_irq_wake(bp->queues[0].irq);
  2865. } else {
  2866. clk_prepare_enable(bp->pclk);
  2867. clk_prepare_enable(bp->hclk);
  2868. clk_prepare_enable(bp->tx_clk);
  2869. clk_prepare_enable(bp->rx_clk);
  2870. }
  2871. netif_device_attach(netdev);
  2872. return 0;
  2873. }
  2874. static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
  2875. static struct platform_driver macb_driver = {
  2876. .probe = macb_probe,
  2877. .remove = macb_remove,
  2878. .driver = {
  2879. .name = "macb",
  2880. .of_match_table = of_match_ptr(macb_dt_ids),
  2881. .pm = &macb_pm_ops,
  2882. },
  2883. };
  2884. module_platform_driver(macb_driver);
  2885. MODULE_LICENSE("GPL");
  2886. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  2887. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  2888. MODULE_ALIAS("platform:macb");