macb.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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/gpio.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/platform_data/macb.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/phy.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of_net.h>
  30. #include <linux/pinctrl/consumer.h>
  31. #include "macb.h"
  32. #define RX_BUFFER_SIZE 128
  33. #define RX_RING_SIZE 512 /* must be power of 2 */
  34. #define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
  35. #define TX_RING_SIZE 128 /* must be power of 2 */
  36. #define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
  37. /* level of occupied TX descriptors under which we wake up TX process */
  38. #define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
  39. #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
  40. | MACB_BIT(ISR_ROVR))
  41. #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
  42. | MACB_BIT(ISR_RLE) \
  43. | MACB_BIT(TXERR))
  44. #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
  45. /*
  46. * Graceful stop timeouts in us. We should allow up to
  47. * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
  48. */
  49. #define MACB_HALT_TIMEOUT 1230
  50. /* Ring buffer accessors */
  51. static unsigned int macb_tx_ring_wrap(unsigned int index)
  52. {
  53. return index & (TX_RING_SIZE - 1);
  54. }
  55. static struct macb_dma_desc *macb_tx_desc(struct macb *bp, unsigned int index)
  56. {
  57. return &bp->tx_ring[macb_tx_ring_wrap(index)];
  58. }
  59. static struct macb_tx_skb *macb_tx_skb(struct macb *bp, unsigned int index)
  60. {
  61. return &bp->tx_skb[macb_tx_ring_wrap(index)];
  62. }
  63. static dma_addr_t macb_tx_dma(struct macb *bp, unsigned int index)
  64. {
  65. dma_addr_t offset;
  66. offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
  67. return bp->tx_ring_dma + offset;
  68. }
  69. static unsigned int macb_rx_ring_wrap(unsigned int index)
  70. {
  71. return index & (RX_RING_SIZE - 1);
  72. }
  73. static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
  74. {
  75. return &bp->rx_ring[macb_rx_ring_wrap(index)];
  76. }
  77. static void *macb_rx_buffer(struct macb *bp, unsigned int index)
  78. {
  79. return bp->rx_buffers + RX_BUFFER_SIZE * macb_rx_ring_wrap(index);
  80. }
  81. void macb_set_hwaddr(struct macb *bp)
  82. {
  83. u32 bottom;
  84. u16 top;
  85. bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
  86. macb_or_gem_writel(bp, SA1B, bottom);
  87. top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
  88. macb_or_gem_writel(bp, SA1T, top);
  89. /* Clear unused address register sets */
  90. macb_or_gem_writel(bp, SA2B, 0);
  91. macb_or_gem_writel(bp, SA2T, 0);
  92. macb_or_gem_writel(bp, SA3B, 0);
  93. macb_or_gem_writel(bp, SA3T, 0);
  94. macb_or_gem_writel(bp, SA4B, 0);
  95. macb_or_gem_writel(bp, SA4T, 0);
  96. }
  97. EXPORT_SYMBOL_GPL(macb_set_hwaddr);
  98. void macb_get_hwaddr(struct macb *bp)
  99. {
  100. struct macb_platform_data *pdata;
  101. u32 bottom;
  102. u16 top;
  103. u8 addr[6];
  104. int i;
  105. pdata = bp->pdev->dev.platform_data;
  106. /* Check all 4 address register for vaild address */
  107. for (i = 0; i < 4; i++) {
  108. bottom = macb_or_gem_readl(bp, SA1B + i * 8);
  109. top = macb_or_gem_readl(bp, SA1T + i * 8);
  110. if (pdata && pdata->rev_eth_addr) {
  111. addr[5] = bottom & 0xff;
  112. addr[4] = (bottom >> 8) & 0xff;
  113. addr[3] = (bottom >> 16) & 0xff;
  114. addr[2] = (bottom >> 24) & 0xff;
  115. addr[1] = top & 0xff;
  116. addr[0] = (top & 0xff00) >> 8;
  117. } else {
  118. addr[0] = bottom & 0xff;
  119. addr[1] = (bottom >> 8) & 0xff;
  120. addr[2] = (bottom >> 16) & 0xff;
  121. addr[3] = (bottom >> 24) & 0xff;
  122. addr[4] = top & 0xff;
  123. addr[5] = (top >> 8) & 0xff;
  124. }
  125. if (is_valid_ether_addr(addr)) {
  126. memcpy(bp->dev->dev_addr, addr, sizeof(addr));
  127. return;
  128. }
  129. }
  130. netdev_info(bp->dev, "invalid hw address, using random\n");
  131. eth_hw_addr_random(bp->dev);
  132. }
  133. EXPORT_SYMBOL_GPL(macb_get_hwaddr);
  134. static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  135. {
  136. struct macb *bp = bus->priv;
  137. int value;
  138. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  139. | MACB_BF(RW, MACB_MAN_READ)
  140. | MACB_BF(PHYA, mii_id)
  141. | MACB_BF(REGA, regnum)
  142. | MACB_BF(CODE, MACB_MAN_CODE)));
  143. /* wait for end of transfer */
  144. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  145. cpu_relax();
  146. value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  147. return value;
  148. }
  149. static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  150. u16 value)
  151. {
  152. struct macb *bp = bus->priv;
  153. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  154. | MACB_BF(RW, MACB_MAN_WRITE)
  155. | MACB_BF(PHYA, mii_id)
  156. | MACB_BF(REGA, regnum)
  157. | MACB_BF(CODE, MACB_MAN_CODE)
  158. | MACB_BF(DATA, value)));
  159. /* wait for end of transfer */
  160. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  161. cpu_relax();
  162. return 0;
  163. }
  164. static int macb_mdio_reset(struct mii_bus *bus)
  165. {
  166. return 0;
  167. }
  168. static void macb_handle_link_change(struct net_device *dev)
  169. {
  170. struct macb *bp = netdev_priv(dev);
  171. struct phy_device *phydev = bp->phy_dev;
  172. unsigned long flags;
  173. int status_change = 0;
  174. spin_lock_irqsave(&bp->lock, flags);
  175. if (phydev->link) {
  176. if ((bp->speed != phydev->speed) ||
  177. (bp->duplex != phydev->duplex)) {
  178. u32 reg;
  179. reg = macb_readl(bp, NCFGR);
  180. reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  181. if (macb_is_gem(bp))
  182. reg &= ~GEM_BIT(GBE);
  183. if (phydev->duplex)
  184. reg |= MACB_BIT(FD);
  185. if (phydev->speed == SPEED_100)
  186. reg |= MACB_BIT(SPD);
  187. if (phydev->speed == SPEED_1000)
  188. reg |= GEM_BIT(GBE);
  189. macb_or_gem_writel(bp, NCFGR, reg);
  190. bp->speed = phydev->speed;
  191. bp->duplex = phydev->duplex;
  192. status_change = 1;
  193. }
  194. }
  195. if (phydev->link != bp->link) {
  196. if (!phydev->link) {
  197. bp->speed = 0;
  198. bp->duplex = -1;
  199. }
  200. bp->link = phydev->link;
  201. status_change = 1;
  202. }
  203. spin_unlock_irqrestore(&bp->lock, flags);
  204. if (status_change) {
  205. if (phydev->link) {
  206. netif_carrier_on(dev);
  207. netdev_info(dev, "link up (%d/%s)\n",
  208. phydev->speed,
  209. phydev->duplex == DUPLEX_FULL ?
  210. "Full" : "Half");
  211. } else {
  212. netif_carrier_off(dev);
  213. netdev_info(dev, "link down\n");
  214. }
  215. }
  216. }
  217. /* based on au1000_eth. c*/
  218. static int macb_mii_probe(struct net_device *dev)
  219. {
  220. struct macb *bp = netdev_priv(dev);
  221. struct macb_platform_data *pdata;
  222. struct phy_device *phydev;
  223. int phy_irq;
  224. int ret;
  225. phydev = phy_find_first(bp->mii_bus);
  226. if (!phydev) {
  227. netdev_err(dev, "no PHY found\n");
  228. return -1;
  229. }
  230. pdata = dev_get_platdata(&bp->pdev->dev);
  231. if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
  232. ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
  233. if (!ret) {
  234. phy_irq = gpio_to_irq(pdata->phy_irq_pin);
  235. phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
  236. }
  237. }
  238. /* attach the mac to the phy */
  239. ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
  240. bp->phy_interface);
  241. if (ret) {
  242. netdev_err(dev, "Could not attach to PHY\n");
  243. return ret;
  244. }
  245. /* mask with MAC supported features */
  246. if (macb_is_gem(bp))
  247. phydev->supported &= PHY_GBIT_FEATURES;
  248. else
  249. phydev->supported &= PHY_BASIC_FEATURES;
  250. phydev->advertising = phydev->supported;
  251. bp->link = 0;
  252. bp->speed = 0;
  253. bp->duplex = -1;
  254. bp->phy_dev = phydev;
  255. return 0;
  256. }
  257. int macb_mii_init(struct macb *bp)
  258. {
  259. struct macb_platform_data *pdata;
  260. int err = -ENXIO, i;
  261. /* Enable management port */
  262. macb_writel(bp, NCR, MACB_BIT(MPE));
  263. bp->mii_bus = mdiobus_alloc();
  264. if (bp->mii_bus == NULL) {
  265. err = -ENOMEM;
  266. goto err_out;
  267. }
  268. bp->mii_bus->name = "MACB_mii_bus";
  269. bp->mii_bus->read = &macb_mdio_read;
  270. bp->mii_bus->write = &macb_mdio_write;
  271. bp->mii_bus->reset = &macb_mdio_reset;
  272. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  273. bp->pdev->name, bp->pdev->id);
  274. bp->mii_bus->priv = bp;
  275. bp->mii_bus->parent = &bp->dev->dev;
  276. pdata = bp->pdev->dev.platform_data;
  277. if (pdata)
  278. bp->mii_bus->phy_mask = pdata->phy_mask;
  279. bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  280. if (!bp->mii_bus->irq) {
  281. err = -ENOMEM;
  282. goto err_out_free_mdiobus;
  283. }
  284. for (i = 0; i < PHY_MAX_ADDR; i++)
  285. bp->mii_bus->irq[i] = PHY_POLL;
  286. dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
  287. if (mdiobus_register(bp->mii_bus))
  288. goto err_out_free_mdio_irq;
  289. if (macb_mii_probe(bp->dev) != 0) {
  290. goto err_out_unregister_bus;
  291. }
  292. return 0;
  293. err_out_unregister_bus:
  294. mdiobus_unregister(bp->mii_bus);
  295. err_out_free_mdio_irq:
  296. kfree(bp->mii_bus->irq);
  297. err_out_free_mdiobus:
  298. mdiobus_free(bp->mii_bus);
  299. err_out:
  300. return err;
  301. }
  302. EXPORT_SYMBOL_GPL(macb_mii_init);
  303. static void macb_update_stats(struct macb *bp)
  304. {
  305. u32 __iomem *reg = bp->regs + MACB_PFR;
  306. u32 *p = &bp->hw_stats.macb.rx_pause_frames;
  307. u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
  308. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  309. for(; p < end; p++, reg++)
  310. *p += __raw_readl(reg);
  311. }
  312. static int macb_halt_tx(struct macb *bp)
  313. {
  314. unsigned long halt_time, timeout;
  315. u32 status;
  316. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
  317. timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
  318. do {
  319. halt_time = jiffies;
  320. status = macb_readl(bp, TSR);
  321. if (!(status & MACB_BIT(TGO)))
  322. return 0;
  323. usleep_range(10, 250);
  324. } while (time_before(halt_time, timeout));
  325. return -ETIMEDOUT;
  326. }
  327. static void macb_tx_error_task(struct work_struct *work)
  328. {
  329. struct macb *bp = container_of(work, struct macb, tx_error_task);
  330. struct macb_tx_skb *tx_skb;
  331. struct sk_buff *skb;
  332. unsigned int tail;
  333. netdev_vdbg(bp->dev, "macb_tx_error_task: t = %u, h = %u\n",
  334. bp->tx_tail, bp->tx_head);
  335. /* Make sure nobody is trying to queue up new packets */
  336. netif_stop_queue(bp->dev);
  337. /*
  338. * Stop transmission now
  339. * (in case we have just queued new packets)
  340. */
  341. if (macb_halt_tx(bp))
  342. /* Just complain for now, reinitializing TX path can be good */
  343. netdev_err(bp->dev, "BUG: halt tx timed out\n");
  344. /* No need for the lock here as nobody will interrupt us anymore */
  345. /*
  346. * Treat frames in TX queue including the ones that caused the error.
  347. * Free transmit buffers in upper layer.
  348. */
  349. for (tail = bp->tx_tail; tail != bp->tx_head; tail++) {
  350. struct macb_dma_desc *desc;
  351. u32 ctrl;
  352. desc = macb_tx_desc(bp, tail);
  353. ctrl = desc->ctrl;
  354. tx_skb = macb_tx_skb(bp, tail);
  355. skb = tx_skb->skb;
  356. if (ctrl & MACB_BIT(TX_USED)) {
  357. netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
  358. macb_tx_ring_wrap(tail), skb->data);
  359. bp->stats.tx_packets++;
  360. bp->stats.tx_bytes += skb->len;
  361. } else {
  362. /*
  363. * "Buffers exhausted mid-frame" errors may only happen
  364. * if the driver is buggy, so complain loudly about those.
  365. * Statistics are updated by hardware.
  366. */
  367. if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
  368. netdev_err(bp->dev,
  369. "BUG: TX buffers exhausted mid-frame\n");
  370. desc->ctrl = ctrl | MACB_BIT(TX_USED);
  371. }
  372. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
  373. DMA_TO_DEVICE);
  374. tx_skb->skb = NULL;
  375. dev_kfree_skb(skb);
  376. }
  377. /* Make descriptor updates visible to hardware */
  378. wmb();
  379. /* Reinitialize the TX desc queue */
  380. macb_writel(bp, TBQP, bp->tx_ring_dma);
  381. /* Make TX ring reflect state of hardware */
  382. bp->tx_head = bp->tx_tail = 0;
  383. /* Now we are ready to start transmission again */
  384. netif_wake_queue(bp->dev);
  385. /* Housework before enabling TX IRQ */
  386. macb_writel(bp, TSR, macb_readl(bp, TSR));
  387. macb_writel(bp, IER, MACB_TX_INT_FLAGS);
  388. }
  389. static void macb_tx_interrupt(struct macb *bp)
  390. {
  391. unsigned int tail;
  392. unsigned int head;
  393. u32 status;
  394. status = macb_readl(bp, TSR);
  395. macb_writel(bp, TSR, status);
  396. macb_writel(bp, ISR, MACB_BIT(TCOMP));
  397. netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
  398. (unsigned long)status);
  399. head = bp->tx_head;
  400. for (tail = bp->tx_tail; tail != head; tail++) {
  401. struct macb_tx_skb *tx_skb;
  402. struct sk_buff *skb;
  403. struct macb_dma_desc *desc;
  404. u32 ctrl;
  405. desc = macb_tx_desc(bp, tail);
  406. /* Make hw descriptor updates visible to CPU */
  407. rmb();
  408. ctrl = desc->ctrl;
  409. if (!(ctrl & MACB_BIT(TX_USED)))
  410. break;
  411. tx_skb = macb_tx_skb(bp, tail);
  412. skb = tx_skb->skb;
  413. netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
  414. macb_tx_ring_wrap(tail), skb->data);
  415. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
  416. DMA_TO_DEVICE);
  417. bp->stats.tx_packets++;
  418. bp->stats.tx_bytes += skb->len;
  419. tx_skb->skb = NULL;
  420. dev_kfree_skb_irq(skb);
  421. }
  422. bp->tx_tail = tail;
  423. if (netif_queue_stopped(bp->dev)
  424. && CIRC_CNT(bp->tx_head, bp->tx_tail,
  425. TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
  426. netif_wake_queue(bp->dev);
  427. }
  428. static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
  429. unsigned int last_frag)
  430. {
  431. unsigned int len;
  432. unsigned int frag;
  433. unsigned int offset;
  434. struct sk_buff *skb;
  435. struct macb_dma_desc *desc;
  436. desc = macb_rx_desc(bp, last_frag);
  437. len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
  438. netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  439. macb_rx_ring_wrap(first_frag),
  440. macb_rx_ring_wrap(last_frag), len);
  441. /*
  442. * The ethernet header starts NET_IP_ALIGN bytes into the
  443. * first buffer. Since the header is 14 bytes, this makes the
  444. * payload word-aligned.
  445. *
  446. * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
  447. * the two padding bytes into the skb so that we avoid hitting
  448. * the slowpath in memcpy(), and pull them off afterwards.
  449. */
  450. skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
  451. if (!skb) {
  452. bp->stats.rx_dropped++;
  453. for (frag = first_frag; ; frag++) {
  454. desc = macb_rx_desc(bp, frag);
  455. desc->addr &= ~MACB_BIT(RX_USED);
  456. if (frag == last_frag)
  457. break;
  458. }
  459. /* Make descriptor updates visible to hardware */
  460. wmb();
  461. return 1;
  462. }
  463. offset = 0;
  464. len += NET_IP_ALIGN;
  465. skb_checksum_none_assert(skb);
  466. skb_put(skb, len);
  467. for (frag = first_frag; ; frag++) {
  468. unsigned int frag_len = RX_BUFFER_SIZE;
  469. if (offset + frag_len > len) {
  470. BUG_ON(frag != last_frag);
  471. frag_len = len - offset;
  472. }
  473. skb_copy_to_linear_data_offset(skb, offset,
  474. macb_rx_buffer(bp, frag), frag_len);
  475. offset += RX_BUFFER_SIZE;
  476. desc = macb_rx_desc(bp, frag);
  477. desc->addr &= ~MACB_BIT(RX_USED);
  478. if (frag == last_frag)
  479. break;
  480. }
  481. /* Make descriptor updates visible to hardware */
  482. wmb();
  483. __skb_pull(skb, NET_IP_ALIGN);
  484. skb->protocol = eth_type_trans(skb, bp->dev);
  485. bp->stats.rx_packets++;
  486. bp->stats.rx_bytes += skb->len;
  487. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  488. skb->len, skb->csum);
  489. netif_receive_skb(skb);
  490. return 0;
  491. }
  492. /* Mark DMA descriptors from begin up to and not including end as unused */
  493. static void discard_partial_frame(struct macb *bp, unsigned int begin,
  494. unsigned int end)
  495. {
  496. unsigned int frag;
  497. for (frag = begin; frag != end; frag++) {
  498. struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
  499. desc->addr &= ~MACB_BIT(RX_USED);
  500. }
  501. /* Make descriptor updates visible to hardware */
  502. wmb();
  503. /*
  504. * When this happens, the hardware stats registers for
  505. * whatever caused this is updated, so we don't have to record
  506. * anything.
  507. */
  508. }
  509. static int macb_rx(struct macb *bp, int budget)
  510. {
  511. int received = 0;
  512. unsigned int tail;
  513. int first_frag = -1;
  514. for (tail = bp->rx_tail; budget > 0; tail++) {
  515. struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
  516. u32 addr, ctrl;
  517. /* Make hw descriptor updates visible to CPU */
  518. rmb();
  519. addr = desc->addr;
  520. ctrl = desc->ctrl;
  521. if (!(addr & MACB_BIT(RX_USED)))
  522. break;
  523. if (ctrl & MACB_BIT(RX_SOF)) {
  524. if (first_frag != -1)
  525. discard_partial_frame(bp, first_frag, tail);
  526. first_frag = tail;
  527. }
  528. if (ctrl & MACB_BIT(RX_EOF)) {
  529. int dropped;
  530. BUG_ON(first_frag == -1);
  531. dropped = macb_rx_frame(bp, first_frag, tail);
  532. first_frag = -1;
  533. if (!dropped) {
  534. received++;
  535. budget--;
  536. }
  537. }
  538. }
  539. if (first_frag != -1)
  540. bp->rx_tail = first_frag;
  541. else
  542. bp->rx_tail = tail;
  543. return received;
  544. }
  545. static int macb_poll(struct napi_struct *napi, int budget)
  546. {
  547. struct macb *bp = container_of(napi, struct macb, napi);
  548. int work_done;
  549. u32 status;
  550. status = macb_readl(bp, RSR);
  551. macb_writel(bp, RSR, status);
  552. work_done = 0;
  553. netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
  554. (unsigned long)status, budget);
  555. work_done = macb_rx(bp, budget);
  556. if (work_done < budget) {
  557. napi_complete(napi);
  558. /*
  559. * We've done what we can to clean the buffers. Make sure we
  560. * get notified when new packets arrive.
  561. */
  562. macb_writel(bp, IER, MACB_RX_INT_FLAGS);
  563. /* Packets received while interrupts were disabled */
  564. status = macb_readl(bp, RSR);
  565. if (unlikely(status))
  566. napi_reschedule(napi);
  567. }
  568. /* TODO: Handle errors */
  569. return work_done;
  570. }
  571. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  572. {
  573. struct net_device *dev = dev_id;
  574. struct macb *bp = netdev_priv(dev);
  575. u32 status;
  576. status = macb_readl(bp, ISR);
  577. if (unlikely(!status))
  578. return IRQ_NONE;
  579. spin_lock(&bp->lock);
  580. while (status) {
  581. /* close possible race with dev_close */
  582. if (unlikely(!netif_running(dev))) {
  583. macb_writel(bp, IDR, -1);
  584. break;
  585. }
  586. netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status);
  587. if (status & MACB_RX_INT_FLAGS) {
  588. /*
  589. * There's no point taking any more interrupts
  590. * until we have processed the buffers. The
  591. * scheduling call may fail if the poll routine
  592. * is already scheduled, so disable interrupts
  593. * now.
  594. */
  595. macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
  596. macb_writel(bp, ISR, MACB_BIT(RCOMP));
  597. if (napi_schedule_prep(&bp->napi)) {
  598. netdev_vdbg(bp->dev, "scheduling RX softirq\n");
  599. __napi_schedule(&bp->napi);
  600. }
  601. }
  602. if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
  603. macb_writel(bp, IDR, MACB_TX_INT_FLAGS);
  604. schedule_work(&bp->tx_error_task);
  605. break;
  606. }
  607. if (status & MACB_BIT(TCOMP))
  608. macb_tx_interrupt(bp);
  609. /*
  610. * Link change detection isn't possible with RMII, so we'll
  611. * add that if/when we get our hands on a full-blown MII PHY.
  612. */
  613. if (status & MACB_BIT(ISR_ROVR)) {
  614. /* We missed at least one packet */
  615. if (macb_is_gem(bp))
  616. bp->hw_stats.gem.rx_overruns++;
  617. else
  618. bp->hw_stats.macb.rx_overruns++;
  619. }
  620. if (status & MACB_BIT(HRESP)) {
  621. /*
  622. * TODO: Reset the hardware, and maybe move the
  623. * netdev_err to a lower-priority context as well
  624. * (work queue?)
  625. */
  626. netdev_err(dev, "DMA bus error: HRESP not OK\n");
  627. }
  628. status = macb_readl(bp, ISR);
  629. }
  630. spin_unlock(&bp->lock);
  631. return IRQ_HANDLED;
  632. }
  633. #ifdef CONFIG_NET_POLL_CONTROLLER
  634. /*
  635. * Polling receive - used by netconsole and other diagnostic tools
  636. * to allow network i/o with interrupts disabled.
  637. */
  638. static void macb_poll_controller(struct net_device *dev)
  639. {
  640. unsigned long flags;
  641. local_irq_save(flags);
  642. macb_interrupt(dev->irq, dev);
  643. local_irq_restore(flags);
  644. }
  645. #endif
  646. static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  647. {
  648. struct macb *bp = netdev_priv(dev);
  649. dma_addr_t mapping;
  650. unsigned int len, entry;
  651. struct macb_dma_desc *desc;
  652. struct macb_tx_skb *tx_skb;
  653. u32 ctrl;
  654. unsigned long flags;
  655. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  656. netdev_vdbg(bp->dev,
  657. "start_xmit: len %u head %p data %p tail %p end %p\n",
  658. skb->len, skb->head, skb->data,
  659. skb_tail_pointer(skb), skb_end_pointer(skb));
  660. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
  661. skb->data, 16, true);
  662. #endif
  663. len = skb->len;
  664. spin_lock_irqsave(&bp->lock, flags);
  665. /* This is a hard error, log it. */
  666. if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1) {
  667. netif_stop_queue(dev);
  668. spin_unlock_irqrestore(&bp->lock, flags);
  669. netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
  670. netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
  671. bp->tx_head, bp->tx_tail);
  672. return NETDEV_TX_BUSY;
  673. }
  674. entry = macb_tx_ring_wrap(bp->tx_head);
  675. bp->tx_head++;
  676. netdev_vdbg(bp->dev, "Allocated ring entry %u\n", entry);
  677. mapping = dma_map_single(&bp->pdev->dev, skb->data,
  678. len, DMA_TO_DEVICE);
  679. tx_skb = &bp->tx_skb[entry];
  680. tx_skb->skb = skb;
  681. tx_skb->mapping = mapping;
  682. netdev_vdbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
  683. skb->data, (unsigned long)mapping);
  684. ctrl = MACB_BF(TX_FRMLEN, len);
  685. ctrl |= MACB_BIT(TX_LAST);
  686. if (entry == (TX_RING_SIZE - 1))
  687. ctrl |= MACB_BIT(TX_WRAP);
  688. desc = &bp->tx_ring[entry];
  689. desc->addr = mapping;
  690. desc->ctrl = ctrl;
  691. /* Make newly initialized descriptor visible to hardware */
  692. wmb();
  693. skb_tx_timestamp(skb);
  694. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  695. if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1)
  696. netif_stop_queue(dev);
  697. spin_unlock_irqrestore(&bp->lock, flags);
  698. return NETDEV_TX_OK;
  699. }
  700. static void macb_free_consistent(struct macb *bp)
  701. {
  702. if (bp->tx_skb) {
  703. kfree(bp->tx_skb);
  704. bp->tx_skb = NULL;
  705. }
  706. if (bp->rx_ring) {
  707. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
  708. bp->rx_ring, bp->rx_ring_dma);
  709. bp->rx_ring = NULL;
  710. }
  711. if (bp->tx_ring) {
  712. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
  713. bp->tx_ring, bp->tx_ring_dma);
  714. bp->tx_ring = NULL;
  715. }
  716. if (bp->rx_buffers) {
  717. dma_free_coherent(&bp->pdev->dev,
  718. RX_RING_SIZE * RX_BUFFER_SIZE,
  719. bp->rx_buffers, bp->rx_buffers_dma);
  720. bp->rx_buffers = NULL;
  721. }
  722. }
  723. static int macb_alloc_consistent(struct macb *bp)
  724. {
  725. int size;
  726. size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
  727. bp->tx_skb = kmalloc(size, GFP_KERNEL);
  728. if (!bp->tx_skb)
  729. goto out_err;
  730. size = RX_RING_BYTES;
  731. bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  732. &bp->rx_ring_dma, GFP_KERNEL);
  733. if (!bp->rx_ring)
  734. goto out_err;
  735. netdev_dbg(bp->dev,
  736. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  737. size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
  738. size = TX_RING_BYTES;
  739. bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  740. &bp->tx_ring_dma, GFP_KERNEL);
  741. if (!bp->tx_ring)
  742. goto out_err;
  743. netdev_dbg(bp->dev,
  744. "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
  745. size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
  746. size = RX_RING_SIZE * RX_BUFFER_SIZE;
  747. bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  748. &bp->rx_buffers_dma, GFP_KERNEL);
  749. if (!bp->rx_buffers)
  750. goto out_err;
  751. netdev_dbg(bp->dev,
  752. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  753. size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
  754. return 0;
  755. out_err:
  756. macb_free_consistent(bp);
  757. return -ENOMEM;
  758. }
  759. static void macb_init_rings(struct macb *bp)
  760. {
  761. int i;
  762. dma_addr_t addr;
  763. addr = bp->rx_buffers_dma;
  764. for (i = 0; i < RX_RING_SIZE; i++) {
  765. bp->rx_ring[i].addr = addr;
  766. bp->rx_ring[i].ctrl = 0;
  767. addr += RX_BUFFER_SIZE;
  768. }
  769. bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
  770. for (i = 0; i < TX_RING_SIZE; i++) {
  771. bp->tx_ring[i].addr = 0;
  772. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  773. }
  774. bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  775. bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
  776. }
  777. static void macb_reset_hw(struct macb *bp)
  778. {
  779. /*
  780. * Disable RX and TX (XXX: Should we halt the transmission
  781. * more gracefully?)
  782. */
  783. macb_writel(bp, NCR, 0);
  784. /* Clear the stats registers (XXX: Update stats first?) */
  785. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  786. /* Clear all status flags */
  787. macb_writel(bp, TSR, -1);
  788. macb_writel(bp, RSR, -1);
  789. /* Disable all interrupts */
  790. macb_writel(bp, IDR, -1);
  791. macb_readl(bp, ISR);
  792. }
  793. static u32 gem_mdc_clk_div(struct macb *bp)
  794. {
  795. u32 config;
  796. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  797. if (pclk_hz <= 20000000)
  798. config = GEM_BF(CLK, GEM_CLK_DIV8);
  799. else if (pclk_hz <= 40000000)
  800. config = GEM_BF(CLK, GEM_CLK_DIV16);
  801. else if (pclk_hz <= 80000000)
  802. config = GEM_BF(CLK, GEM_CLK_DIV32);
  803. else if (pclk_hz <= 120000000)
  804. config = GEM_BF(CLK, GEM_CLK_DIV48);
  805. else if (pclk_hz <= 160000000)
  806. config = GEM_BF(CLK, GEM_CLK_DIV64);
  807. else
  808. config = GEM_BF(CLK, GEM_CLK_DIV96);
  809. return config;
  810. }
  811. static u32 macb_mdc_clk_div(struct macb *bp)
  812. {
  813. u32 config;
  814. unsigned long pclk_hz;
  815. if (macb_is_gem(bp))
  816. return gem_mdc_clk_div(bp);
  817. pclk_hz = clk_get_rate(bp->pclk);
  818. if (pclk_hz <= 20000000)
  819. config = MACB_BF(CLK, MACB_CLK_DIV8);
  820. else if (pclk_hz <= 40000000)
  821. config = MACB_BF(CLK, MACB_CLK_DIV16);
  822. else if (pclk_hz <= 80000000)
  823. config = MACB_BF(CLK, MACB_CLK_DIV32);
  824. else
  825. config = MACB_BF(CLK, MACB_CLK_DIV64);
  826. return config;
  827. }
  828. /*
  829. * Get the DMA bus width field of the network configuration register that we
  830. * should program. We find the width from decoding the design configuration
  831. * register to find the maximum supported data bus width.
  832. */
  833. static u32 macb_dbw(struct macb *bp)
  834. {
  835. if (!macb_is_gem(bp))
  836. return 0;
  837. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  838. case 4:
  839. return GEM_BF(DBW, GEM_DBW128);
  840. case 2:
  841. return GEM_BF(DBW, GEM_DBW64);
  842. case 1:
  843. default:
  844. return GEM_BF(DBW, GEM_DBW32);
  845. }
  846. }
  847. /*
  848. * Configure the receive DMA engine
  849. * - use the correct receive buffer size
  850. * - set the possibility to use INCR16 bursts
  851. * (if not supported by FIFO, it will fallback to default)
  852. * - set both rx/tx packet buffers to full memory size
  853. * These are configurable parameters for GEM.
  854. */
  855. static void macb_configure_dma(struct macb *bp)
  856. {
  857. u32 dmacfg;
  858. if (macb_is_gem(bp)) {
  859. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  860. dmacfg |= GEM_BF(RXBS, RX_BUFFER_SIZE / 64);
  861. dmacfg |= GEM_BF(FBLDO, 16);
  862. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  863. dmacfg &= ~GEM_BIT(ENDIA);
  864. gem_writel(bp, DMACFG, dmacfg);
  865. }
  866. }
  867. static void macb_init_hw(struct macb *bp)
  868. {
  869. u32 config;
  870. macb_reset_hw(bp);
  871. macb_set_hwaddr(bp);
  872. config = macb_mdc_clk_div(bp);
  873. config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
  874. config |= MACB_BIT(PAE); /* PAuse Enable */
  875. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  876. config |= MACB_BIT(BIG); /* Receive oversized frames */
  877. if (bp->dev->flags & IFF_PROMISC)
  878. config |= MACB_BIT(CAF); /* Copy All Frames */
  879. if (!(bp->dev->flags & IFF_BROADCAST))
  880. config |= MACB_BIT(NBC); /* No BroadCast */
  881. config |= macb_dbw(bp);
  882. macb_writel(bp, NCFGR, config);
  883. bp->speed = SPEED_10;
  884. bp->duplex = DUPLEX_HALF;
  885. macb_configure_dma(bp);
  886. /* Initialize TX and RX buffers */
  887. macb_writel(bp, RBQP, bp->rx_ring_dma);
  888. macb_writel(bp, TBQP, bp->tx_ring_dma);
  889. /* Enable TX and RX */
  890. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  891. /* Enable interrupts */
  892. macb_writel(bp, IER, (MACB_RX_INT_FLAGS
  893. | MACB_TX_INT_FLAGS
  894. | MACB_BIT(HRESP)));
  895. }
  896. /*
  897. * The hash address register is 64 bits long and takes up two
  898. * locations in the memory map. The least significant bits are stored
  899. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  900. *
  901. * The unicast hash enable and the multicast hash enable bits in the
  902. * network configuration register enable the reception of hash matched
  903. * frames. The destination address is reduced to a 6 bit index into
  904. * the 64 bit hash register using the following hash function. The
  905. * hash function is an exclusive or of every sixth bit of the
  906. * destination address.
  907. *
  908. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  909. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  910. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  911. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  912. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  913. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  914. *
  915. * da[0] represents the least significant bit of the first byte
  916. * received, that is, the multicast/unicast indicator, and da[47]
  917. * represents the most significant bit of the last byte received. If
  918. * the hash index, hi[n], points to a bit that is set in the hash
  919. * register then the frame will be matched according to whether the
  920. * frame is multicast or unicast. A multicast match will be signalled
  921. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  922. * index points to a bit set in the hash register. A unicast match
  923. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  924. * and the hash index points to a bit set in the hash register. To
  925. * receive all multicast frames, the hash register should be set with
  926. * all ones and the multicast hash enable bit should be set in the
  927. * network configuration register.
  928. */
  929. static inline int hash_bit_value(int bitnr, __u8 *addr)
  930. {
  931. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  932. return 1;
  933. return 0;
  934. }
  935. /*
  936. * Return the hash index value for the specified address.
  937. */
  938. static int hash_get_index(__u8 *addr)
  939. {
  940. int i, j, bitval;
  941. int hash_index = 0;
  942. for (j = 0; j < 6; j++) {
  943. for (i = 0, bitval = 0; i < 8; i++)
  944. bitval ^= hash_bit_value(i*6 + j, addr);
  945. hash_index |= (bitval << j);
  946. }
  947. return hash_index;
  948. }
  949. /*
  950. * Add multicast addresses to the internal multicast-hash table.
  951. */
  952. static void macb_sethashtable(struct net_device *dev)
  953. {
  954. struct netdev_hw_addr *ha;
  955. unsigned long mc_filter[2];
  956. unsigned int bitnr;
  957. struct macb *bp = netdev_priv(dev);
  958. mc_filter[0] = mc_filter[1] = 0;
  959. netdev_for_each_mc_addr(ha, dev) {
  960. bitnr = hash_get_index(ha->addr);
  961. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  962. }
  963. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  964. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  965. }
  966. /*
  967. * Enable/Disable promiscuous and multicast modes.
  968. */
  969. void macb_set_rx_mode(struct net_device *dev)
  970. {
  971. unsigned long cfg;
  972. struct macb *bp = netdev_priv(dev);
  973. cfg = macb_readl(bp, NCFGR);
  974. if (dev->flags & IFF_PROMISC)
  975. /* Enable promiscuous mode */
  976. cfg |= MACB_BIT(CAF);
  977. else if (dev->flags & (~IFF_PROMISC))
  978. /* Disable promiscuous mode */
  979. cfg &= ~MACB_BIT(CAF);
  980. if (dev->flags & IFF_ALLMULTI) {
  981. /* Enable all multicast mode */
  982. macb_or_gem_writel(bp, HRB, -1);
  983. macb_or_gem_writel(bp, HRT, -1);
  984. cfg |= MACB_BIT(NCFGR_MTI);
  985. } else if (!netdev_mc_empty(dev)) {
  986. /* Enable specific multicasts */
  987. macb_sethashtable(dev);
  988. cfg |= MACB_BIT(NCFGR_MTI);
  989. } else if (dev->flags & (~IFF_ALLMULTI)) {
  990. /* Disable all multicast mode */
  991. macb_or_gem_writel(bp, HRB, 0);
  992. macb_or_gem_writel(bp, HRT, 0);
  993. cfg &= ~MACB_BIT(NCFGR_MTI);
  994. }
  995. macb_writel(bp, NCFGR, cfg);
  996. }
  997. EXPORT_SYMBOL_GPL(macb_set_rx_mode);
  998. static int macb_open(struct net_device *dev)
  999. {
  1000. struct macb *bp = netdev_priv(dev);
  1001. int err;
  1002. netdev_dbg(bp->dev, "open\n");
  1003. /* carrier starts down */
  1004. netif_carrier_off(dev);
  1005. /* if the phy is not yet register, retry later*/
  1006. if (!bp->phy_dev)
  1007. return -EAGAIN;
  1008. err = macb_alloc_consistent(bp);
  1009. if (err) {
  1010. netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
  1011. err);
  1012. return err;
  1013. }
  1014. napi_enable(&bp->napi);
  1015. macb_init_rings(bp);
  1016. macb_init_hw(bp);
  1017. /* schedule a link state check */
  1018. phy_start(bp->phy_dev);
  1019. netif_start_queue(dev);
  1020. return 0;
  1021. }
  1022. static int macb_close(struct net_device *dev)
  1023. {
  1024. struct macb *bp = netdev_priv(dev);
  1025. unsigned long flags;
  1026. netif_stop_queue(dev);
  1027. napi_disable(&bp->napi);
  1028. if (bp->phy_dev)
  1029. phy_stop(bp->phy_dev);
  1030. spin_lock_irqsave(&bp->lock, flags);
  1031. macb_reset_hw(bp);
  1032. netif_carrier_off(dev);
  1033. spin_unlock_irqrestore(&bp->lock, flags);
  1034. macb_free_consistent(bp);
  1035. return 0;
  1036. }
  1037. static void gem_update_stats(struct macb *bp)
  1038. {
  1039. u32 __iomem *reg = bp->regs + GEM_OTX;
  1040. u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
  1041. u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
  1042. for (; p < end; p++, reg++)
  1043. *p += __raw_readl(reg);
  1044. }
  1045. static struct net_device_stats *gem_get_stats(struct macb *bp)
  1046. {
  1047. struct gem_stats *hwstat = &bp->hw_stats.gem;
  1048. struct net_device_stats *nstat = &bp->stats;
  1049. gem_update_stats(bp);
  1050. nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
  1051. hwstat->rx_alignment_errors +
  1052. hwstat->rx_resource_errors +
  1053. hwstat->rx_overruns +
  1054. hwstat->rx_oversize_frames +
  1055. hwstat->rx_jabbers +
  1056. hwstat->rx_undersized_frames +
  1057. hwstat->rx_length_field_frame_errors);
  1058. nstat->tx_errors = (hwstat->tx_late_collisions +
  1059. hwstat->tx_excessive_collisions +
  1060. hwstat->tx_underrun +
  1061. hwstat->tx_carrier_sense_errors);
  1062. nstat->multicast = hwstat->rx_multicast_frames;
  1063. nstat->collisions = (hwstat->tx_single_collision_frames +
  1064. hwstat->tx_multiple_collision_frames +
  1065. hwstat->tx_excessive_collisions);
  1066. nstat->rx_length_errors = (hwstat->rx_oversize_frames +
  1067. hwstat->rx_jabbers +
  1068. hwstat->rx_undersized_frames +
  1069. hwstat->rx_length_field_frame_errors);
  1070. nstat->rx_over_errors = hwstat->rx_resource_errors;
  1071. nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
  1072. nstat->rx_frame_errors = hwstat->rx_alignment_errors;
  1073. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1074. nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
  1075. nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
  1076. nstat->tx_fifo_errors = hwstat->tx_underrun;
  1077. return nstat;
  1078. }
  1079. struct net_device_stats *macb_get_stats(struct net_device *dev)
  1080. {
  1081. struct macb *bp = netdev_priv(dev);
  1082. struct net_device_stats *nstat = &bp->stats;
  1083. struct macb_stats *hwstat = &bp->hw_stats.macb;
  1084. if (macb_is_gem(bp))
  1085. return gem_get_stats(bp);
  1086. /* read stats from hardware */
  1087. macb_update_stats(bp);
  1088. /* Convert HW stats into netdevice stats */
  1089. nstat->rx_errors = (hwstat->rx_fcs_errors +
  1090. hwstat->rx_align_errors +
  1091. hwstat->rx_resource_errors +
  1092. hwstat->rx_overruns +
  1093. hwstat->rx_oversize_pkts +
  1094. hwstat->rx_jabbers +
  1095. hwstat->rx_undersize_pkts +
  1096. hwstat->sqe_test_errors +
  1097. hwstat->rx_length_mismatch);
  1098. nstat->tx_errors = (hwstat->tx_late_cols +
  1099. hwstat->tx_excessive_cols +
  1100. hwstat->tx_underruns +
  1101. hwstat->tx_carrier_errors);
  1102. nstat->collisions = (hwstat->tx_single_cols +
  1103. hwstat->tx_multiple_cols +
  1104. hwstat->tx_excessive_cols);
  1105. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  1106. hwstat->rx_jabbers +
  1107. hwstat->rx_undersize_pkts +
  1108. hwstat->rx_length_mismatch);
  1109. nstat->rx_over_errors = hwstat->rx_resource_errors +
  1110. hwstat->rx_overruns;
  1111. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  1112. nstat->rx_frame_errors = hwstat->rx_align_errors;
  1113. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1114. /* XXX: What does "missed" mean? */
  1115. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  1116. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  1117. nstat->tx_fifo_errors = hwstat->tx_underruns;
  1118. /* Don't know about heartbeat or window errors... */
  1119. return nstat;
  1120. }
  1121. EXPORT_SYMBOL_GPL(macb_get_stats);
  1122. static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1123. {
  1124. struct macb *bp = netdev_priv(dev);
  1125. struct phy_device *phydev = bp->phy_dev;
  1126. if (!phydev)
  1127. return -ENODEV;
  1128. return phy_ethtool_gset(phydev, cmd);
  1129. }
  1130. static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1131. {
  1132. struct macb *bp = netdev_priv(dev);
  1133. struct phy_device *phydev = bp->phy_dev;
  1134. if (!phydev)
  1135. return -ENODEV;
  1136. return phy_ethtool_sset(phydev, cmd);
  1137. }
  1138. static int macb_get_regs_len(struct net_device *netdev)
  1139. {
  1140. return MACB_GREGS_NBR * sizeof(u32);
  1141. }
  1142. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1143. void *p)
  1144. {
  1145. struct macb *bp = netdev_priv(dev);
  1146. unsigned int tail, head;
  1147. u32 *regs_buff = p;
  1148. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  1149. | MACB_GREGS_VERSION;
  1150. tail = macb_tx_ring_wrap(bp->tx_tail);
  1151. head = macb_tx_ring_wrap(bp->tx_head);
  1152. regs_buff[0] = macb_readl(bp, NCR);
  1153. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  1154. regs_buff[2] = macb_readl(bp, NSR);
  1155. regs_buff[3] = macb_readl(bp, TSR);
  1156. regs_buff[4] = macb_readl(bp, RBQP);
  1157. regs_buff[5] = macb_readl(bp, TBQP);
  1158. regs_buff[6] = macb_readl(bp, RSR);
  1159. regs_buff[7] = macb_readl(bp, IMR);
  1160. regs_buff[8] = tail;
  1161. regs_buff[9] = head;
  1162. regs_buff[10] = macb_tx_dma(bp, tail);
  1163. regs_buff[11] = macb_tx_dma(bp, head);
  1164. if (macb_is_gem(bp)) {
  1165. regs_buff[12] = gem_readl(bp, USRIO);
  1166. regs_buff[13] = gem_readl(bp, DMACFG);
  1167. }
  1168. }
  1169. const struct ethtool_ops macb_ethtool_ops = {
  1170. .get_settings = macb_get_settings,
  1171. .set_settings = macb_set_settings,
  1172. .get_regs_len = macb_get_regs_len,
  1173. .get_regs = macb_get_regs,
  1174. .get_link = ethtool_op_get_link,
  1175. .get_ts_info = ethtool_op_get_ts_info,
  1176. };
  1177. EXPORT_SYMBOL_GPL(macb_ethtool_ops);
  1178. int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1179. {
  1180. struct macb *bp = netdev_priv(dev);
  1181. struct phy_device *phydev = bp->phy_dev;
  1182. if (!netif_running(dev))
  1183. return -EINVAL;
  1184. if (!phydev)
  1185. return -ENODEV;
  1186. return phy_mii_ioctl(phydev, rq, cmd);
  1187. }
  1188. EXPORT_SYMBOL_GPL(macb_ioctl);
  1189. static const struct net_device_ops macb_netdev_ops = {
  1190. .ndo_open = macb_open,
  1191. .ndo_stop = macb_close,
  1192. .ndo_start_xmit = macb_start_xmit,
  1193. .ndo_set_rx_mode = macb_set_rx_mode,
  1194. .ndo_get_stats = macb_get_stats,
  1195. .ndo_do_ioctl = macb_ioctl,
  1196. .ndo_validate_addr = eth_validate_addr,
  1197. .ndo_change_mtu = eth_change_mtu,
  1198. .ndo_set_mac_address = eth_mac_addr,
  1199. #ifdef CONFIG_NET_POLL_CONTROLLER
  1200. .ndo_poll_controller = macb_poll_controller,
  1201. #endif
  1202. };
  1203. #if defined(CONFIG_OF)
  1204. static const struct of_device_id macb_dt_ids[] = {
  1205. { .compatible = "cdns,at32ap7000-macb" },
  1206. { .compatible = "cdns,at91sam9260-macb" },
  1207. { .compatible = "cdns,macb" },
  1208. { .compatible = "cdns,pc302-gem" },
  1209. { .compatible = "cdns,gem" },
  1210. { /* sentinel */ }
  1211. };
  1212. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  1213. #endif
  1214. static int __init macb_probe(struct platform_device *pdev)
  1215. {
  1216. struct macb_platform_data *pdata;
  1217. struct resource *regs;
  1218. struct net_device *dev;
  1219. struct macb *bp;
  1220. struct phy_device *phydev;
  1221. u32 config;
  1222. int err = -ENXIO;
  1223. struct pinctrl *pinctrl;
  1224. const char *mac;
  1225. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1226. if (!regs) {
  1227. dev_err(&pdev->dev, "no mmio resource defined\n");
  1228. goto err_out;
  1229. }
  1230. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  1231. if (IS_ERR(pinctrl)) {
  1232. err = PTR_ERR(pinctrl);
  1233. if (err == -EPROBE_DEFER)
  1234. goto err_out;
  1235. dev_warn(&pdev->dev, "No pinctrl provided\n");
  1236. }
  1237. err = -ENOMEM;
  1238. dev = alloc_etherdev(sizeof(*bp));
  1239. if (!dev)
  1240. goto err_out;
  1241. SET_NETDEV_DEV(dev, &pdev->dev);
  1242. /* TODO: Actually, we have some interesting features... */
  1243. dev->features |= 0;
  1244. bp = netdev_priv(dev);
  1245. bp->pdev = pdev;
  1246. bp->dev = dev;
  1247. spin_lock_init(&bp->lock);
  1248. INIT_WORK(&bp->tx_error_task, macb_tx_error_task);
  1249. bp->pclk = clk_get(&pdev->dev, "pclk");
  1250. if (IS_ERR(bp->pclk)) {
  1251. dev_err(&pdev->dev, "failed to get macb_clk\n");
  1252. goto err_out_free_dev;
  1253. }
  1254. clk_prepare_enable(bp->pclk);
  1255. bp->hclk = clk_get(&pdev->dev, "hclk");
  1256. if (IS_ERR(bp->hclk)) {
  1257. dev_err(&pdev->dev, "failed to get hclk\n");
  1258. goto err_out_put_pclk;
  1259. }
  1260. clk_prepare_enable(bp->hclk);
  1261. bp->regs = ioremap(regs->start, resource_size(regs));
  1262. if (!bp->regs) {
  1263. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  1264. err = -ENOMEM;
  1265. goto err_out_disable_clocks;
  1266. }
  1267. dev->irq = platform_get_irq(pdev, 0);
  1268. err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
  1269. if (err) {
  1270. dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
  1271. dev->irq, err);
  1272. goto err_out_iounmap;
  1273. }
  1274. dev->netdev_ops = &macb_netdev_ops;
  1275. netif_napi_add(dev, &bp->napi, macb_poll, 64);
  1276. dev->ethtool_ops = &macb_ethtool_ops;
  1277. dev->base_addr = regs->start;
  1278. /* Set MII management clock divider */
  1279. config = macb_mdc_clk_div(bp);
  1280. config |= macb_dbw(bp);
  1281. macb_writel(bp, NCFGR, config);
  1282. mac = of_get_mac_address(pdev->dev.of_node);
  1283. if (mac)
  1284. memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
  1285. else
  1286. macb_get_hwaddr(bp);
  1287. err = of_get_phy_mode(pdev->dev.of_node);
  1288. if (err < 0) {
  1289. pdata = pdev->dev.platform_data;
  1290. if (pdata && pdata->is_rmii)
  1291. bp->phy_interface = PHY_INTERFACE_MODE_RMII;
  1292. else
  1293. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  1294. } else {
  1295. bp->phy_interface = err;
  1296. }
  1297. if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
  1298. macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
  1299. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
  1300. #if defined(CONFIG_ARCH_AT91)
  1301. macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
  1302. MACB_BIT(CLKEN)));
  1303. #else
  1304. macb_or_gem_writel(bp, USRIO, 0);
  1305. #endif
  1306. else
  1307. #if defined(CONFIG_ARCH_AT91)
  1308. macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
  1309. #else
  1310. macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
  1311. #endif
  1312. err = register_netdev(dev);
  1313. if (err) {
  1314. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  1315. goto err_out_free_irq;
  1316. }
  1317. err = macb_mii_init(bp);
  1318. if (err)
  1319. goto err_out_unregister_netdev;
  1320. platform_set_drvdata(pdev, dev);
  1321. netif_carrier_off(dev);
  1322. netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
  1323. macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
  1324. dev->irq, dev->dev_addr);
  1325. phydev = bp->phy_dev;
  1326. netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  1327. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  1328. return 0;
  1329. err_out_unregister_netdev:
  1330. unregister_netdev(dev);
  1331. err_out_free_irq:
  1332. free_irq(dev->irq, dev);
  1333. err_out_iounmap:
  1334. iounmap(bp->regs);
  1335. err_out_disable_clocks:
  1336. clk_disable_unprepare(bp->hclk);
  1337. clk_put(bp->hclk);
  1338. clk_disable_unprepare(bp->pclk);
  1339. err_out_put_pclk:
  1340. clk_put(bp->pclk);
  1341. err_out_free_dev:
  1342. free_netdev(dev);
  1343. err_out:
  1344. platform_set_drvdata(pdev, NULL);
  1345. return err;
  1346. }
  1347. static int __exit macb_remove(struct platform_device *pdev)
  1348. {
  1349. struct net_device *dev;
  1350. struct macb *bp;
  1351. dev = platform_get_drvdata(pdev);
  1352. if (dev) {
  1353. bp = netdev_priv(dev);
  1354. if (bp->phy_dev)
  1355. phy_disconnect(bp->phy_dev);
  1356. mdiobus_unregister(bp->mii_bus);
  1357. kfree(bp->mii_bus->irq);
  1358. mdiobus_free(bp->mii_bus);
  1359. unregister_netdev(dev);
  1360. free_irq(dev->irq, dev);
  1361. iounmap(bp->regs);
  1362. clk_disable_unprepare(bp->hclk);
  1363. clk_put(bp->hclk);
  1364. clk_disable_unprepare(bp->pclk);
  1365. clk_put(bp->pclk);
  1366. free_netdev(dev);
  1367. platform_set_drvdata(pdev, NULL);
  1368. }
  1369. return 0;
  1370. }
  1371. #ifdef CONFIG_PM
  1372. static int macb_suspend(struct platform_device *pdev, pm_message_t state)
  1373. {
  1374. struct net_device *netdev = platform_get_drvdata(pdev);
  1375. struct macb *bp = netdev_priv(netdev);
  1376. netif_carrier_off(netdev);
  1377. netif_device_detach(netdev);
  1378. clk_disable_unprepare(bp->hclk);
  1379. clk_disable_unprepare(bp->pclk);
  1380. return 0;
  1381. }
  1382. static int macb_resume(struct platform_device *pdev)
  1383. {
  1384. struct net_device *netdev = platform_get_drvdata(pdev);
  1385. struct macb *bp = netdev_priv(netdev);
  1386. clk_prepare_enable(bp->pclk);
  1387. clk_prepare_enable(bp->hclk);
  1388. netif_device_attach(netdev);
  1389. return 0;
  1390. }
  1391. #else
  1392. #define macb_suspend NULL
  1393. #define macb_resume NULL
  1394. #endif
  1395. static struct platform_driver macb_driver = {
  1396. .remove = __exit_p(macb_remove),
  1397. .suspend = macb_suspend,
  1398. .resume = macb_resume,
  1399. .driver = {
  1400. .name = "macb",
  1401. .owner = THIS_MODULE,
  1402. .of_match_table = of_match_ptr(macb_dt_ids),
  1403. },
  1404. };
  1405. module_platform_driver_probe(macb_driver, macb_probe);
  1406. MODULE_LICENSE("GPL");
  1407. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  1408. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1409. MODULE_ALIAS("platform:macb");