macb.c 36 KB

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