macb.c 42 KB

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