macb.c 37 KB

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