macb.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  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. netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
  397. (unsigned long)status);
  398. head = bp->tx_head;
  399. for (tail = bp->tx_tail; tail != head; tail++) {
  400. struct macb_tx_skb *tx_skb;
  401. struct sk_buff *skb;
  402. struct macb_dma_desc *desc;
  403. u32 ctrl;
  404. desc = macb_tx_desc(bp, tail);
  405. /* Make hw descriptor updates visible to CPU */
  406. rmb();
  407. ctrl = desc->ctrl;
  408. if (!(ctrl & MACB_BIT(TX_USED)))
  409. break;
  410. tx_skb = macb_tx_skb(bp, tail);
  411. skb = tx_skb->skb;
  412. netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
  413. macb_tx_ring_wrap(tail), skb->data);
  414. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
  415. DMA_TO_DEVICE);
  416. bp->stats.tx_packets++;
  417. bp->stats.tx_bytes += skb->len;
  418. tx_skb->skb = NULL;
  419. dev_kfree_skb_irq(skb);
  420. }
  421. bp->tx_tail = tail;
  422. if (netif_queue_stopped(bp->dev)
  423. && CIRC_CNT(bp->tx_head, bp->tx_tail,
  424. TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
  425. netif_wake_queue(bp->dev);
  426. }
  427. static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
  428. unsigned int last_frag)
  429. {
  430. unsigned int len;
  431. unsigned int frag;
  432. unsigned int offset;
  433. struct sk_buff *skb;
  434. struct macb_dma_desc *desc;
  435. desc = macb_rx_desc(bp, last_frag);
  436. len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
  437. netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  438. macb_rx_ring_wrap(first_frag),
  439. macb_rx_ring_wrap(last_frag), len);
  440. /*
  441. * The ethernet header starts NET_IP_ALIGN bytes into the
  442. * first buffer. Since the header is 14 bytes, this makes the
  443. * payload word-aligned.
  444. *
  445. * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
  446. * the two padding bytes into the skb so that we avoid hitting
  447. * the slowpath in memcpy(), and pull them off afterwards.
  448. */
  449. skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
  450. if (!skb) {
  451. bp->stats.rx_dropped++;
  452. for (frag = first_frag; ; frag++) {
  453. desc = macb_rx_desc(bp, frag);
  454. desc->addr &= ~MACB_BIT(RX_USED);
  455. if (frag == last_frag)
  456. break;
  457. }
  458. /* Make descriptor updates visible to hardware */
  459. wmb();
  460. return 1;
  461. }
  462. offset = 0;
  463. len += NET_IP_ALIGN;
  464. skb_checksum_none_assert(skb);
  465. skb_put(skb, len);
  466. for (frag = first_frag; ; frag++) {
  467. unsigned int frag_len = RX_BUFFER_SIZE;
  468. if (offset + frag_len > len) {
  469. BUG_ON(frag != last_frag);
  470. frag_len = len - offset;
  471. }
  472. skb_copy_to_linear_data_offset(skb, offset,
  473. macb_rx_buffer(bp, frag), frag_len);
  474. offset += RX_BUFFER_SIZE;
  475. desc = macb_rx_desc(bp, frag);
  476. desc->addr &= ~MACB_BIT(RX_USED);
  477. if (frag == last_frag)
  478. break;
  479. }
  480. /* Make descriptor updates visible to hardware */
  481. wmb();
  482. __skb_pull(skb, NET_IP_ALIGN);
  483. skb->protocol = eth_type_trans(skb, bp->dev);
  484. bp->stats.rx_packets++;
  485. bp->stats.rx_bytes += skb->len;
  486. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  487. skb->len, skb->csum);
  488. netif_receive_skb(skb);
  489. return 0;
  490. }
  491. /* Mark DMA descriptors from begin up to and not including end as unused */
  492. static void discard_partial_frame(struct macb *bp, unsigned int begin,
  493. unsigned int end)
  494. {
  495. unsigned int frag;
  496. for (frag = begin; frag != end; frag++) {
  497. struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
  498. desc->addr &= ~MACB_BIT(RX_USED);
  499. }
  500. /* Make descriptor updates visible to hardware */
  501. wmb();
  502. /*
  503. * When this happens, the hardware stats registers for
  504. * whatever caused this is updated, so we don't have to record
  505. * anything.
  506. */
  507. }
  508. static int macb_rx(struct macb *bp, int budget)
  509. {
  510. int received = 0;
  511. unsigned int tail;
  512. int first_frag = -1;
  513. for (tail = bp->rx_tail; budget > 0; tail++) {
  514. struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
  515. u32 addr, ctrl;
  516. /* Make hw descriptor updates visible to CPU */
  517. rmb();
  518. addr = desc->addr;
  519. ctrl = desc->ctrl;
  520. if (!(addr & MACB_BIT(RX_USED)))
  521. break;
  522. if (ctrl & MACB_BIT(RX_SOF)) {
  523. if (first_frag != -1)
  524. discard_partial_frame(bp, first_frag, tail);
  525. first_frag = tail;
  526. }
  527. if (ctrl & MACB_BIT(RX_EOF)) {
  528. int dropped;
  529. BUG_ON(first_frag == -1);
  530. dropped = macb_rx_frame(bp, first_frag, tail);
  531. first_frag = -1;
  532. if (!dropped) {
  533. received++;
  534. budget--;
  535. }
  536. }
  537. }
  538. if (first_frag != -1)
  539. bp->rx_tail = first_frag;
  540. else
  541. bp->rx_tail = tail;
  542. return received;
  543. }
  544. static int macb_poll(struct napi_struct *napi, int budget)
  545. {
  546. struct macb *bp = container_of(napi, struct macb, napi);
  547. int work_done;
  548. u32 status;
  549. status = macb_readl(bp, RSR);
  550. macb_writel(bp, RSR, status);
  551. work_done = 0;
  552. netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
  553. (unsigned long)status, budget);
  554. work_done = macb_rx(bp, budget);
  555. if (work_done < budget) {
  556. napi_complete(napi);
  557. /*
  558. * We've done what we can to clean the buffers. Make sure we
  559. * get notified when new packets arrive.
  560. */
  561. macb_writel(bp, IER, MACB_RX_INT_FLAGS);
  562. }
  563. /* TODO: Handle errors */
  564. return work_done;
  565. }
  566. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  567. {
  568. struct net_device *dev = dev_id;
  569. struct macb *bp = netdev_priv(dev);
  570. u32 status;
  571. status = macb_readl(bp, ISR);
  572. if (unlikely(!status))
  573. return IRQ_NONE;
  574. spin_lock(&bp->lock);
  575. while (status) {
  576. /* close possible race with dev_close */
  577. if (unlikely(!netif_running(dev))) {
  578. macb_writel(bp, IDR, -1);
  579. break;
  580. }
  581. netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status);
  582. if (status & MACB_RX_INT_FLAGS) {
  583. /*
  584. * There's no point taking any more interrupts
  585. * until we have processed the buffers. The
  586. * scheduling call may fail if the poll routine
  587. * is already scheduled, so disable interrupts
  588. * now.
  589. */
  590. macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
  591. if (napi_schedule_prep(&bp->napi)) {
  592. netdev_vdbg(bp->dev, "scheduling RX softirq\n");
  593. __napi_schedule(&bp->napi);
  594. }
  595. }
  596. if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
  597. macb_writel(bp, IDR, MACB_TX_INT_FLAGS);
  598. schedule_work(&bp->tx_error_task);
  599. break;
  600. }
  601. if (status & MACB_BIT(TCOMP))
  602. macb_tx_interrupt(bp);
  603. /*
  604. * Link change detection isn't possible with RMII, so we'll
  605. * add that if/when we get our hands on a full-blown MII PHY.
  606. */
  607. if (status & MACB_BIT(ISR_ROVR)) {
  608. /* We missed at least one packet */
  609. if (macb_is_gem(bp))
  610. bp->hw_stats.gem.rx_overruns++;
  611. else
  612. bp->hw_stats.macb.rx_overruns++;
  613. }
  614. if (status & MACB_BIT(HRESP)) {
  615. /*
  616. * TODO: Reset the hardware, and maybe move the
  617. * netdev_err to a lower-priority context as well
  618. * (work queue?)
  619. */
  620. netdev_err(dev, "DMA bus error: HRESP not OK\n");
  621. }
  622. status = macb_readl(bp, ISR);
  623. }
  624. spin_unlock(&bp->lock);
  625. return IRQ_HANDLED;
  626. }
  627. #ifdef CONFIG_NET_POLL_CONTROLLER
  628. /*
  629. * Polling receive - used by netconsole and other diagnostic tools
  630. * to allow network i/o with interrupts disabled.
  631. */
  632. static void macb_poll_controller(struct net_device *dev)
  633. {
  634. unsigned long flags;
  635. local_irq_save(flags);
  636. macb_interrupt(dev->irq, dev);
  637. local_irq_restore(flags);
  638. }
  639. #endif
  640. static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  641. {
  642. struct macb *bp = netdev_priv(dev);
  643. dma_addr_t mapping;
  644. unsigned int len, entry;
  645. struct macb_dma_desc *desc;
  646. struct macb_tx_skb *tx_skb;
  647. u32 ctrl;
  648. unsigned long flags;
  649. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  650. netdev_vdbg(bp->dev,
  651. "start_xmit: len %u head %p data %p tail %p end %p\n",
  652. skb->len, skb->head, skb->data,
  653. skb_tail_pointer(skb), skb_end_pointer(skb));
  654. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
  655. skb->data, 16, true);
  656. #endif
  657. len = skb->len;
  658. spin_lock_irqsave(&bp->lock, flags);
  659. /* This is a hard error, log it. */
  660. if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1) {
  661. netif_stop_queue(dev);
  662. spin_unlock_irqrestore(&bp->lock, flags);
  663. netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
  664. netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
  665. bp->tx_head, bp->tx_tail);
  666. return NETDEV_TX_BUSY;
  667. }
  668. entry = macb_tx_ring_wrap(bp->tx_head);
  669. bp->tx_head++;
  670. netdev_vdbg(bp->dev, "Allocated ring entry %u\n", entry);
  671. mapping = dma_map_single(&bp->pdev->dev, skb->data,
  672. len, DMA_TO_DEVICE);
  673. tx_skb = &bp->tx_skb[entry];
  674. tx_skb->skb = skb;
  675. tx_skb->mapping = mapping;
  676. netdev_vdbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
  677. skb->data, (unsigned long)mapping);
  678. ctrl = MACB_BF(TX_FRMLEN, len);
  679. ctrl |= MACB_BIT(TX_LAST);
  680. if (entry == (TX_RING_SIZE - 1))
  681. ctrl |= MACB_BIT(TX_WRAP);
  682. desc = &bp->tx_ring[entry];
  683. desc->addr = mapping;
  684. desc->ctrl = ctrl;
  685. /* Make newly initialized descriptor visible to hardware */
  686. wmb();
  687. skb_tx_timestamp(skb);
  688. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  689. if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1)
  690. netif_stop_queue(dev);
  691. spin_unlock_irqrestore(&bp->lock, flags);
  692. return NETDEV_TX_OK;
  693. }
  694. static void macb_free_consistent(struct macb *bp)
  695. {
  696. if (bp->tx_skb) {
  697. kfree(bp->tx_skb);
  698. bp->tx_skb = NULL;
  699. }
  700. if (bp->rx_ring) {
  701. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
  702. bp->rx_ring, bp->rx_ring_dma);
  703. bp->rx_ring = NULL;
  704. }
  705. if (bp->tx_ring) {
  706. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
  707. bp->tx_ring, bp->tx_ring_dma);
  708. bp->tx_ring = NULL;
  709. }
  710. if (bp->rx_buffers) {
  711. dma_free_coherent(&bp->pdev->dev,
  712. RX_RING_SIZE * RX_BUFFER_SIZE,
  713. bp->rx_buffers, bp->rx_buffers_dma);
  714. bp->rx_buffers = NULL;
  715. }
  716. }
  717. static int macb_alloc_consistent(struct macb *bp)
  718. {
  719. int size;
  720. size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
  721. bp->tx_skb = kmalloc(size, GFP_KERNEL);
  722. if (!bp->tx_skb)
  723. goto out_err;
  724. size = RX_RING_BYTES;
  725. bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  726. &bp->rx_ring_dma, GFP_KERNEL);
  727. if (!bp->rx_ring)
  728. goto out_err;
  729. netdev_dbg(bp->dev,
  730. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  731. size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
  732. size = TX_RING_BYTES;
  733. bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  734. &bp->tx_ring_dma, GFP_KERNEL);
  735. if (!bp->tx_ring)
  736. goto out_err;
  737. netdev_dbg(bp->dev,
  738. "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
  739. size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
  740. size = RX_RING_SIZE * RX_BUFFER_SIZE;
  741. bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  742. &bp->rx_buffers_dma, GFP_KERNEL);
  743. if (!bp->rx_buffers)
  744. goto out_err;
  745. netdev_dbg(bp->dev,
  746. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  747. size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
  748. return 0;
  749. out_err:
  750. macb_free_consistent(bp);
  751. return -ENOMEM;
  752. }
  753. static void macb_init_rings(struct macb *bp)
  754. {
  755. int i;
  756. dma_addr_t addr;
  757. addr = bp->rx_buffers_dma;
  758. for (i = 0; i < RX_RING_SIZE; i++) {
  759. bp->rx_ring[i].addr = addr;
  760. bp->rx_ring[i].ctrl = 0;
  761. addr += RX_BUFFER_SIZE;
  762. }
  763. bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
  764. for (i = 0; i < TX_RING_SIZE; i++) {
  765. bp->tx_ring[i].addr = 0;
  766. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  767. }
  768. bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  769. bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
  770. }
  771. static void macb_reset_hw(struct macb *bp)
  772. {
  773. /*
  774. * Disable RX and TX (XXX: Should we halt the transmission
  775. * more gracefully?)
  776. */
  777. macb_writel(bp, NCR, 0);
  778. /* Clear the stats registers (XXX: Update stats first?) */
  779. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  780. /* Clear all status flags */
  781. macb_writel(bp, TSR, -1);
  782. macb_writel(bp, RSR, -1);
  783. /* Disable all interrupts */
  784. macb_writel(bp, IDR, -1);
  785. macb_readl(bp, ISR);
  786. }
  787. static u32 gem_mdc_clk_div(struct macb *bp)
  788. {
  789. u32 config;
  790. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  791. if (pclk_hz <= 20000000)
  792. config = GEM_BF(CLK, GEM_CLK_DIV8);
  793. else if (pclk_hz <= 40000000)
  794. config = GEM_BF(CLK, GEM_CLK_DIV16);
  795. else if (pclk_hz <= 80000000)
  796. config = GEM_BF(CLK, GEM_CLK_DIV32);
  797. else if (pclk_hz <= 120000000)
  798. config = GEM_BF(CLK, GEM_CLK_DIV48);
  799. else if (pclk_hz <= 160000000)
  800. config = GEM_BF(CLK, GEM_CLK_DIV64);
  801. else
  802. config = GEM_BF(CLK, GEM_CLK_DIV96);
  803. return config;
  804. }
  805. static u32 macb_mdc_clk_div(struct macb *bp)
  806. {
  807. u32 config;
  808. unsigned long pclk_hz;
  809. if (macb_is_gem(bp))
  810. return gem_mdc_clk_div(bp);
  811. pclk_hz = clk_get_rate(bp->pclk);
  812. if (pclk_hz <= 20000000)
  813. config = MACB_BF(CLK, MACB_CLK_DIV8);
  814. else if (pclk_hz <= 40000000)
  815. config = MACB_BF(CLK, MACB_CLK_DIV16);
  816. else if (pclk_hz <= 80000000)
  817. config = MACB_BF(CLK, MACB_CLK_DIV32);
  818. else
  819. config = MACB_BF(CLK, MACB_CLK_DIV64);
  820. return config;
  821. }
  822. /*
  823. * Get the DMA bus width field of the network configuration register that we
  824. * should program. We find the width from decoding the design configuration
  825. * register to find the maximum supported data bus width.
  826. */
  827. static u32 macb_dbw(struct macb *bp)
  828. {
  829. if (!macb_is_gem(bp))
  830. return 0;
  831. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  832. case 4:
  833. return GEM_BF(DBW, GEM_DBW128);
  834. case 2:
  835. return GEM_BF(DBW, GEM_DBW64);
  836. case 1:
  837. default:
  838. return GEM_BF(DBW, GEM_DBW32);
  839. }
  840. }
  841. /*
  842. * Configure the receive DMA engine
  843. * - use the correct receive buffer size
  844. * - set the possibility to use INCR16 bursts
  845. * (if not supported by FIFO, it will fallback to default)
  846. * - set both rx/tx packet buffers to full memory size
  847. * These are configurable parameters for GEM.
  848. */
  849. static void macb_configure_dma(struct macb *bp)
  850. {
  851. u32 dmacfg;
  852. if (macb_is_gem(bp)) {
  853. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  854. dmacfg |= GEM_BF(RXBS, RX_BUFFER_SIZE / 64);
  855. dmacfg |= GEM_BF(FBLDO, 16);
  856. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  857. gem_writel(bp, DMACFG, dmacfg);
  858. }
  859. }
  860. static void macb_init_hw(struct macb *bp)
  861. {
  862. u32 config;
  863. macb_reset_hw(bp);
  864. macb_set_hwaddr(bp);
  865. config = macb_mdc_clk_div(bp);
  866. config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
  867. config |= MACB_BIT(PAE); /* PAuse Enable */
  868. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  869. config |= MACB_BIT(BIG); /* Receive oversized frames */
  870. if (bp->dev->flags & IFF_PROMISC)
  871. config |= MACB_BIT(CAF); /* Copy All Frames */
  872. if (!(bp->dev->flags & IFF_BROADCAST))
  873. config |= MACB_BIT(NBC); /* No BroadCast */
  874. config |= macb_dbw(bp);
  875. macb_writel(bp, NCFGR, config);
  876. bp->speed = SPEED_10;
  877. bp->duplex = DUPLEX_HALF;
  878. macb_configure_dma(bp);
  879. /* Initialize TX and RX buffers */
  880. macb_writel(bp, RBQP, bp->rx_ring_dma);
  881. macb_writel(bp, TBQP, bp->tx_ring_dma);
  882. /* Enable TX and RX */
  883. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  884. /* Enable interrupts */
  885. macb_writel(bp, IER, (MACB_RX_INT_FLAGS
  886. | MACB_TX_INT_FLAGS
  887. | MACB_BIT(HRESP)));
  888. }
  889. /*
  890. * The hash address register is 64 bits long and takes up two
  891. * locations in the memory map. The least significant bits are stored
  892. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  893. *
  894. * The unicast hash enable and the multicast hash enable bits in the
  895. * network configuration register enable the reception of hash matched
  896. * frames. The destination address is reduced to a 6 bit index into
  897. * the 64 bit hash register using the following hash function. The
  898. * hash function is an exclusive or of every sixth bit of the
  899. * destination address.
  900. *
  901. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  902. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  903. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  904. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  905. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  906. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  907. *
  908. * da[0] represents the least significant bit of the first byte
  909. * received, that is, the multicast/unicast indicator, and da[47]
  910. * represents the most significant bit of the last byte received. If
  911. * the hash index, hi[n], points to a bit that is set in the hash
  912. * register then the frame will be matched according to whether the
  913. * frame is multicast or unicast. A multicast match will be signalled
  914. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  915. * index points to a bit set in the hash register. A unicast match
  916. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  917. * and the hash index points to a bit set in the hash register. To
  918. * receive all multicast frames, the hash register should be set with
  919. * all ones and the multicast hash enable bit should be set in the
  920. * network configuration register.
  921. */
  922. static inline int hash_bit_value(int bitnr, __u8 *addr)
  923. {
  924. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  925. return 1;
  926. return 0;
  927. }
  928. /*
  929. * Return the hash index value for the specified address.
  930. */
  931. static int hash_get_index(__u8 *addr)
  932. {
  933. int i, j, bitval;
  934. int hash_index = 0;
  935. for (j = 0; j < 6; j++) {
  936. for (i = 0, bitval = 0; i < 8; i++)
  937. bitval ^= hash_bit_value(i*6 + j, addr);
  938. hash_index |= (bitval << j);
  939. }
  940. return hash_index;
  941. }
  942. /*
  943. * Add multicast addresses to the internal multicast-hash table.
  944. */
  945. static void macb_sethashtable(struct net_device *dev)
  946. {
  947. struct netdev_hw_addr *ha;
  948. unsigned long mc_filter[2];
  949. unsigned int bitnr;
  950. struct macb *bp = netdev_priv(dev);
  951. mc_filter[0] = mc_filter[1] = 0;
  952. netdev_for_each_mc_addr(ha, dev) {
  953. bitnr = hash_get_index(ha->addr);
  954. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  955. }
  956. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  957. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  958. }
  959. /*
  960. * Enable/Disable promiscuous and multicast modes.
  961. */
  962. void macb_set_rx_mode(struct net_device *dev)
  963. {
  964. unsigned long cfg;
  965. struct macb *bp = netdev_priv(dev);
  966. cfg = macb_readl(bp, NCFGR);
  967. if (dev->flags & IFF_PROMISC)
  968. /* Enable promiscuous mode */
  969. cfg |= MACB_BIT(CAF);
  970. else if (dev->flags & (~IFF_PROMISC))
  971. /* Disable promiscuous mode */
  972. cfg &= ~MACB_BIT(CAF);
  973. if (dev->flags & IFF_ALLMULTI) {
  974. /* Enable all multicast mode */
  975. macb_or_gem_writel(bp, HRB, -1);
  976. macb_or_gem_writel(bp, HRT, -1);
  977. cfg |= MACB_BIT(NCFGR_MTI);
  978. } else if (!netdev_mc_empty(dev)) {
  979. /* Enable specific multicasts */
  980. macb_sethashtable(dev);
  981. cfg |= MACB_BIT(NCFGR_MTI);
  982. } else if (dev->flags & (~IFF_ALLMULTI)) {
  983. /* Disable all multicast mode */
  984. macb_or_gem_writel(bp, HRB, 0);
  985. macb_or_gem_writel(bp, HRT, 0);
  986. cfg &= ~MACB_BIT(NCFGR_MTI);
  987. }
  988. macb_writel(bp, NCFGR, cfg);
  989. }
  990. EXPORT_SYMBOL_GPL(macb_set_rx_mode);
  991. static int macb_open(struct net_device *dev)
  992. {
  993. struct macb *bp = netdev_priv(dev);
  994. int err;
  995. netdev_dbg(bp->dev, "open\n");
  996. /* carrier starts down */
  997. netif_carrier_off(dev);
  998. /* if the phy is not yet register, retry later*/
  999. if (!bp->phy_dev)
  1000. return -EAGAIN;
  1001. err = macb_alloc_consistent(bp);
  1002. if (err) {
  1003. netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
  1004. err);
  1005. return err;
  1006. }
  1007. napi_enable(&bp->napi);
  1008. macb_init_rings(bp);
  1009. macb_init_hw(bp);
  1010. /* schedule a link state check */
  1011. phy_start(bp->phy_dev);
  1012. netif_start_queue(dev);
  1013. return 0;
  1014. }
  1015. static int macb_close(struct net_device *dev)
  1016. {
  1017. struct macb *bp = netdev_priv(dev);
  1018. unsigned long flags;
  1019. netif_stop_queue(dev);
  1020. napi_disable(&bp->napi);
  1021. if (bp->phy_dev)
  1022. phy_stop(bp->phy_dev);
  1023. spin_lock_irqsave(&bp->lock, flags);
  1024. macb_reset_hw(bp);
  1025. netif_carrier_off(dev);
  1026. spin_unlock_irqrestore(&bp->lock, flags);
  1027. macb_free_consistent(bp);
  1028. return 0;
  1029. }
  1030. static void gem_update_stats(struct macb *bp)
  1031. {
  1032. u32 __iomem *reg = bp->regs + GEM_OTX;
  1033. u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
  1034. u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
  1035. for (; p < end; p++, reg++)
  1036. *p += __raw_readl(reg);
  1037. }
  1038. static struct net_device_stats *gem_get_stats(struct macb *bp)
  1039. {
  1040. struct gem_stats *hwstat = &bp->hw_stats.gem;
  1041. struct net_device_stats *nstat = &bp->stats;
  1042. gem_update_stats(bp);
  1043. nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
  1044. hwstat->rx_alignment_errors +
  1045. hwstat->rx_resource_errors +
  1046. hwstat->rx_overruns +
  1047. hwstat->rx_oversize_frames +
  1048. hwstat->rx_jabbers +
  1049. hwstat->rx_undersized_frames +
  1050. hwstat->rx_length_field_frame_errors);
  1051. nstat->tx_errors = (hwstat->tx_late_collisions +
  1052. hwstat->tx_excessive_collisions +
  1053. hwstat->tx_underrun +
  1054. hwstat->tx_carrier_sense_errors);
  1055. nstat->multicast = hwstat->rx_multicast_frames;
  1056. nstat->collisions = (hwstat->tx_single_collision_frames +
  1057. hwstat->tx_multiple_collision_frames +
  1058. hwstat->tx_excessive_collisions);
  1059. nstat->rx_length_errors = (hwstat->rx_oversize_frames +
  1060. hwstat->rx_jabbers +
  1061. hwstat->rx_undersized_frames +
  1062. hwstat->rx_length_field_frame_errors);
  1063. nstat->rx_over_errors = hwstat->rx_resource_errors;
  1064. nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
  1065. nstat->rx_frame_errors = hwstat->rx_alignment_errors;
  1066. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1067. nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
  1068. nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
  1069. nstat->tx_fifo_errors = hwstat->tx_underrun;
  1070. return nstat;
  1071. }
  1072. struct net_device_stats *macb_get_stats(struct net_device *dev)
  1073. {
  1074. struct macb *bp = netdev_priv(dev);
  1075. struct net_device_stats *nstat = &bp->stats;
  1076. struct macb_stats *hwstat = &bp->hw_stats.macb;
  1077. if (macb_is_gem(bp))
  1078. return gem_get_stats(bp);
  1079. /* read stats from hardware */
  1080. macb_update_stats(bp);
  1081. /* Convert HW stats into netdevice stats */
  1082. nstat->rx_errors = (hwstat->rx_fcs_errors +
  1083. hwstat->rx_align_errors +
  1084. hwstat->rx_resource_errors +
  1085. hwstat->rx_overruns +
  1086. hwstat->rx_oversize_pkts +
  1087. hwstat->rx_jabbers +
  1088. hwstat->rx_undersize_pkts +
  1089. hwstat->sqe_test_errors +
  1090. hwstat->rx_length_mismatch);
  1091. nstat->tx_errors = (hwstat->tx_late_cols +
  1092. hwstat->tx_excessive_cols +
  1093. hwstat->tx_underruns +
  1094. hwstat->tx_carrier_errors);
  1095. nstat->collisions = (hwstat->tx_single_cols +
  1096. hwstat->tx_multiple_cols +
  1097. hwstat->tx_excessive_cols);
  1098. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  1099. hwstat->rx_jabbers +
  1100. hwstat->rx_undersize_pkts +
  1101. hwstat->rx_length_mismatch);
  1102. nstat->rx_over_errors = hwstat->rx_resource_errors +
  1103. hwstat->rx_overruns;
  1104. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  1105. nstat->rx_frame_errors = hwstat->rx_align_errors;
  1106. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1107. /* XXX: What does "missed" mean? */
  1108. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  1109. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  1110. nstat->tx_fifo_errors = hwstat->tx_underruns;
  1111. /* Don't know about heartbeat or window errors... */
  1112. return nstat;
  1113. }
  1114. EXPORT_SYMBOL_GPL(macb_get_stats);
  1115. static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1116. {
  1117. struct macb *bp = netdev_priv(dev);
  1118. struct phy_device *phydev = bp->phy_dev;
  1119. if (!phydev)
  1120. return -ENODEV;
  1121. return phy_ethtool_gset(phydev, cmd);
  1122. }
  1123. static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1124. {
  1125. struct macb *bp = netdev_priv(dev);
  1126. struct phy_device *phydev = bp->phy_dev;
  1127. if (!phydev)
  1128. return -ENODEV;
  1129. return phy_ethtool_sset(phydev, cmd);
  1130. }
  1131. static int macb_get_regs_len(struct net_device *netdev)
  1132. {
  1133. return MACB_GREGS_NBR * sizeof(u32);
  1134. }
  1135. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1136. void *p)
  1137. {
  1138. struct macb *bp = netdev_priv(dev);
  1139. unsigned int tail, head;
  1140. u32 *regs_buff = p;
  1141. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  1142. | MACB_GREGS_VERSION;
  1143. tail = macb_tx_ring_wrap(bp->tx_tail);
  1144. head = macb_tx_ring_wrap(bp->tx_head);
  1145. regs_buff[0] = macb_readl(bp, NCR);
  1146. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  1147. regs_buff[2] = macb_readl(bp, NSR);
  1148. regs_buff[3] = macb_readl(bp, TSR);
  1149. regs_buff[4] = macb_readl(bp, RBQP);
  1150. regs_buff[5] = macb_readl(bp, TBQP);
  1151. regs_buff[6] = macb_readl(bp, RSR);
  1152. regs_buff[7] = macb_readl(bp, IMR);
  1153. regs_buff[8] = tail;
  1154. regs_buff[9] = head;
  1155. regs_buff[10] = macb_tx_dma(bp, tail);
  1156. regs_buff[11] = macb_tx_dma(bp, head);
  1157. if (macb_is_gem(bp)) {
  1158. regs_buff[12] = gem_readl(bp, USRIO);
  1159. regs_buff[13] = gem_readl(bp, DMACFG);
  1160. }
  1161. }
  1162. const struct ethtool_ops macb_ethtool_ops = {
  1163. .get_settings = macb_get_settings,
  1164. .set_settings = macb_set_settings,
  1165. .get_regs_len = macb_get_regs_len,
  1166. .get_regs = macb_get_regs,
  1167. .get_link = ethtool_op_get_link,
  1168. .get_ts_info = ethtool_op_get_ts_info,
  1169. };
  1170. EXPORT_SYMBOL_GPL(macb_ethtool_ops);
  1171. int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1172. {
  1173. struct macb *bp = netdev_priv(dev);
  1174. struct phy_device *phydev = bp->phy_dev;
  1175. if (!netif_running(dev))
  1176. return -EINVAL;
  1177. if (!phydev)
  1178. return -ENODEV;
  1179. return phy_mii_ioctl(phydev, rq, cmd);
  1180. }
  1181. EXPORT_SYMBOL_GPL(macb_ioctl);
  1182. static const struct net_device_ops macb_netdev_ops = {
  1183. .ndo_open = macb_open,
  1184. .ndo_stop = macb_close,
  1185. .ndo_start_xmit = macb_start_xmit,
  1186. .ndo_set_rx_mode = macb_set_rx_mode,
  1187. .ndo_get_stats = macb_get_stats,
  1188. .ndo_do_ioctl = macb_ioctl,
  1189. .ndo_validate_addr = eth_validate_addr,
  1190. .ndo_change_mtu = eth_change_mtu,
  1191. .ndo_set_mac_address = eth_mac_addr,
  1192. #ifdef CONFIG_NET_POLL_CONTROLLER
  1193. .ndo_poll_controller = macb_poll_controller,
  1194. #endif
  1195. };
  1196. #if defined(CONFIG_OF)
  1197. static const struct of_device_id macb_dt_ids[] = {
  1198. { .compatible = "cdns,at32ap7000-macb" },
  1199. { .compatible = "cdns,at91sam9260-macb" },
  1200. { .compatible = "cdns,macb" },
  1201. { .compatible = "cdns,pc302-gem" },
  1202. { .compatible = "cdns,gem" },
  1203. { /* sentinel */ }
  1204. };
  1205. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  1206. static int macb_get_phy_mode_dt(struct platform_device *pdev)
  1207. {
  1208. struct device_node *np = pdev->dev.of_node;
  1209. if (np)
  1210. return of_get_phy_mode(np);
  1211. return -ENODEV;
  1212. }
  1213. static int macb_get_hwaddr_dt(struct macb *bp)
  1214. {
  1215. struct device_node *np = bp->pdev->dev.of_node;
  1216. if (np) {
  1217. const char *mac = of_get_mac_address(np);
  1218. if (mac) {
  1219. memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
  1220. return 0;
  1221. }
  1222. }
  1223. return -ENODEV;
  1224. }
  1225. #else
  1226. static int macb_get_phy_mode_dt(struct platform_device *pdev)
  1227. {
  1228. return -ENODEV;
  1229. }
  1230. static int macb_get_hwaddr_dt(struct macb *bp)
  1231. {
  1232. return -ENODEV;
  1233. }
  1234. #endif
  1235. static int __init macb_probe(struct platform_device *pdev)
  1236. {
  1237. struct macb_platform_data *pdata;
  1238. struct resource *regs;
  1239. struct net_device *dev;
  1240. struct macb *bp;
  1241. struct phy_device *phydev;
  1242. u32 config;
  1243. int err = -ENXIO;
  1244. struct pinctrl *pinctrl;
  1245. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1246. if (!regs) {
  1247. dev_err(&pdev->dev, "no mmio resource defined\n");
  1248. goto err_out;
  1249. }
  1250. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  1251. if (IS_ERR(pinctrl)) {
  1252. err = PTR_ERR(pinctrl);
  1253. if (err == -EPROBE_DEFER)
  1254. goto err_out;
  1255. dev_warn(&pdev->dev, "No pinctrl provided\n");
  1256. }
  1257. err = -ENOMEM;
  1258. dev = alloc_etherdev(sizeof(*bp));
  1259. if (!dev)
  1260. goto err_out;
  1261. SET_NETDEV_DEV(dev, &pdev->dev);
  1262. /* TODO: Actually, we have some interesting features... */
  1263. dev->features |= 0;
  1264. bp = netdev_priv(dev);
  1265. bp->pdev = pdev;
  1266. bp->dev = dev;
  1267. spin_lock_init(&bp->lock);
  1268. INIT_WORK(&bp->tx_error_task, macb_tx_error_task);
  1269. bp->pclk = clk_get(&pdev->dev, "pclk");
  1270. if (IS_ERR(bp->pclk)) {
  1271. dev_err(&pdev->dev, "failed to get macb_clk\n");
  1272. goto err_out_free_dev;
  1273. }
  1274. clk_enable(bp->pclk);
  1275. bp->hclk = clk_get(&pdev->dev, "hclk");
  1276. if (IS_ERR(bp->hclk)) {
  1277. dev_err(&pdev->dev, "failed to get hclk\n");
  1278. goto err_out_put_pclk;
  1279. }
  1280. clk_enable(bp->hclk);
  1281. bp->regs = ioremap(regs->start, resource_size(regs));
  1282. if (!bp->regs) {
  1283. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  1284. err = -ENOMEM;
  1285. goto err_out_disable_clocks;
  1286. }
  1287. dev->irq = platform_get_irq(pdev, 0);
  1288. err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
  1289. if (err) {
  1290. dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
  1291. dev->irq, err);
  1292. goto err_out_iounmap;
  1293. }
  1294. dev->netdev_ops = &macb_netdev_ops;
  1295. netif_napi_add(dev, &bp->napi, macb_poll, 64);
  1296. dev->ethtool_ops = &macb_ethtool_ops;
  1297. dev->base_addr = regs->start;
  1298. /* Set MII management clock divider */
  1299. config = macb_mdc_clk_div(bp);
  1300. config |= macb_dbw(bp);
  1301. macb_writel(bp, NCFGR, config);
  1302. err = macb_get_hwaddr_dt(bp);
  1303. if (err < 0)
  1304. macb_get_hwaddr(bp);
  1305. err = macb_get_phy_mode_dt(pdev);
  1306. if (err < 0) {
  1307. pdata = pdev->dev.platform_data;
  1308. if (pdata && pdata->is_rmii)
  1309. bp->phy_interface = PHY_INTERFACE_MODE_RMII;
  1310. else
  1311. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  1312. } else {
  1313. bp->phy_interface = err;
  1314. }
  1315. if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
  1316. macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
  1317. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
  1318. #if defined(CONFIG_ARCH_AT91)
  1319. macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
  1320. MACB_BIT(CLKEN)));
  1321. #else
  1322. macb_or_gem_writel(bp, USRIO, 0);
  1323. #endif
  1324. else
  1325. #if defined(CONFIG_ARCH_AT91)
  1326. macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
  1327. #else
  1328. macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
  1329. #endif
  1330. err = register_netdev(dev);
  1331. if (err) {
  1332. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  1333. goto err_out_free_irq;
  1334. }
  1335. if (macb_mii_init(bp) != 0) {
  1336. goto err_out_unregister_netdev;
  1337. }
  1338. platform_set_drvdata(pdev, dev);
  1339. netif_carrier_off(dev);
  1340. netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
  1341. macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
  1342. dev->irq, dev->dev_addr);
  1343. phydev = bp->phy_dev;
  1344. netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  1345. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  1346. return 0;
  1347. err_out_unregister_netdev:
  1348. unregister_netdev(dev);
  1349. err_out_free_irq:
  1350. free_irq(dev->irq, dev);
  1351. err_out_iounmap:
  1352. iounmap(bp->regs);
  1353. err_out_disable_clocks:
  1354. clk_disable(bp->hclk);
  1355. clk_put(bp->hclk);
  1356. clk_disable(bp->pclk);
  1357. err_out_put_pclk:
  1358. clk_put(bp->pclk);
  1359. err_out_free_dev:
  1360. free_netdev(dev);
  1361. err_out:
  1362. platform_set_drvdata(pdev, NULL);
  1363. return err;
  1364. }
  1365. static int __exit macb_remove(struct platform_device *pdev)
  1366. {
  1367. struct net_device *dev;
  1368. struct macb *bp;
  1369. dev = platform_get_drvdata(pdev);
  1370. if (dev) {
  1371. bp = netdev_priv(dev);
  1372. if (bp->phy_dev)
  1373. phy_disconnect(bp->phy_dev);
  1374. mdiobus_unregister(bp->mii_bus);
  1375. kfree(bp->mii_bus->irq);
  1376. mdiobus_free(bp->mii_bus);
  1377. unregister_netdev(dev);
  1378. free_irq(dev->irq, dev);
  1379. iounmap(bp->regs);
  1380. clk_disable(bp->hclk);
  1381. clk_put(bp->hclk);
  1382. clk_disable(bp->pclk);
  1383. clk_put(bp->pclk);
  1384. free_netdev(dev);
  1385. platform_set_drvdata(pdev, NULL);
  1386. }
  1387. return 0;
  1388. }
  1389. #ifdef CONFIG_PM
  1390. static int macb_suspend(struct platform_device *pdev, pm_message_t state)
  1391. {
  1392. struct net_device *netdev = platform_get_drvdata(pdev);
  1393. struct macb *bp = netdev_priv(netdev);
  1394. netif_carrier_off(netdev);
  1395. netif_device_detach(netdev);
  1396. clk_disable(bp->hclk);
  1397. clk_disable(bp->pclk);
  1398. return 0;
  1399. }
  1400. static int macb_resume(struct platform_device *pdev)
  1401. {
  1402. struct net_device *netdev = platform_get_drvdata(pdev);
  1403. struct macb *bp = netdev_priv(netdev);
  1404. clk_enable(bp->pclk);
  1405. clk_enable(bp->hclk);
  1406. netif_device_attach(netdev);
  1407. return 0;
  1408. }
  1409. #else
  1410. #define macb_suspend NULL
  1411. #define macb_resume NULL
  1412. #endif
  1413. static struct platform_driver macb_driver = {
  1414. .remove = __exit_p(macb_remove),
  1415. .suspend = macb_suspend,
  1416. .resume = macb_resume,
  1417. .driver = {
  1418. .name = "macb",
  1419. .owner = THIS_MODULE,
  1420. .of_match_table = of_match_ptr(macb_dt_ids),
  1421. },
  1422. };
  1423. static int __init macb_init(void)
  1424. {
  1425. return platform_driver_probe(&macb_driver, macb_probe);
  1426. }
  1427. static void __exit macb_exit(void)
  1428. {
  1429. platform_driver_unregister(&macb_driver);
  1430. }
  1431. module_init(macb_init);
  1432. module_exit(macb_exit);
  1433. MODULE_LICENSE("GPL");
  1434. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  1435. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1436. MODULE_ALIAS("platform:macb");