macb.c 36 KB

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