macb.c 61 KB

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