macb.c 49 KB

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