macb.c 37 KB

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