macb.c 40 KB

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