macb.c 75 KB

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