macb_main.c 91 KB

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