macb.c 37 KB

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