macb.c 85 KB

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