macb.c 57 KB

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