fec.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  1. /*
  2. * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
  3. * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
  4. *
  5. * Right now, I am very wasteful with the buffers. I allocate memory
  6. * pages and then divide them into 2K frame buffers. This way I know I
  7. * have buffers large enough to hold one frame within one buffer descriptor.
  8. * Once I get this working, I will use 64 or 128 byte CPM buffers, which
  9. * will be much more memory efficient and will easily handle lots of
  10. * small packets.
  11. *
  12. * Much better multiple PHY support by Magnus Damm.
  13. * Copyright (c) 2000 Ericsson Radio Systems AB.
  14. *
  15. * Support for FEC controller of ColdFire processors.
  16. * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
  17. *
  18. * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
  19. * Copyright (c) 2004-2006 Macq Electronique SA.
  20. *
  21. * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/string.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/errno.h>
  28. #include <linux/ioport.h>
  29. #include <linux/slab.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/pci.h>
  32. #include <linux/init.h>
  33. #include <linux/delay.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/workqueue.h>
  39. #include <linux/bitops.h>
  40. #include <linux/io.h>
  41. #include <linux/irq.h>
  42. #include <linux/clk.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/phy.h>
  45. #include <linux/fec.h>
  46. #include <linux/of.h>
  47. #include <linux/of_device.h>
  48. #include <linux/of_gpio.h>
  49. #include <linux/of_net.h>
  50. #include <asm/cacheflush.h>
  51. #ifndef CONFIG_ARM
  52. #include <asm/coldfire.h>
  53. #include <asm/mcfsim.h>
  54. #endif
  55. #include "fec.h"
  56. #if defined(CONFIG_ARM)
  57. #define FEC_ALIGNMENT 0xf
  58. #else
  59. #define FEC_ALIGNMENT 0x3
  60. #endif
  61. #define DRIVER_NAME "fec"
  62. /* Controller is ENET-MAC */
  63. #define FEC_QUIRK_ENET_MAC (1 << 0)
  64. /* Controller needs driver to swap frame */
  65. #define FEC_QUIRK_SWAP_FRAME (1 << 1)
  66. /* Controller uses gasket */
  67. #define FEC_QUIRK_USE_GASKET (1 << 2)
  68. /* Controller has GBIT support */
  69. #define FEC_QUIRK_HAS_GBIT (1 << 3)
  70. static struct platform_device_id fec_devtype[] = {
  71. {
  72. /* keep it for coldfire */
  73. .name = DRIVER_NAME,
  74. .driver_data = 0,
  75. }, {
  76. .name = "imx25-fec",
  77. .driver_data = FEC_QUIRK_USE_GASKET,
  78. }, {
  79. .name = "imx27-fec",
  80. .driver_data = 0,
  81. }, {
  82. .name = "imx28-fec",
  83. .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
  84. }, {
  85. .name = "imx6q-fec",
  86. .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT,
  87. }, {
  88. /* sentinel */
  89. }
  90. };
  91. MODULE_DEVICE_TABLE(platform, fec_devtype);
  92. enum imx_fec_type {
  93. IMX25_FEC = 1, /* runs on i.mx25/50/53 */
  94. IMX27_FEC, /* runs on i.mx27/35/51 */
  95. IMX28_FEC,
  96. IMX6Q_FEC,
  97. };
  98. static const struct of_device_id fec_dt_ids[] = {
  99. { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
  100. { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
  101. { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
  102. { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
  103. { /* sentinel */ }
  104. };
  105. MODULE_DEVICE_TABLE(of, fec_dt_ids);
  106. static unsigned char macaddr[ETH_ALEN];
  107. module_param_array(macaddr, byte, NULL, 0);
  108. MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
  109. #if defined(CONFIG_M5272)
  110. /*
  111. * Some hardware gets it MAC address out of local flash memory.
  112. * if this is non-zero then assume it is the address to get MAC from.
  113. */
  114. #if defined(CONFIG_NETtel)
  115. #define FEC_FLASHMAC 0xf0006006
  116. #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
  117. #define FEC_FLASHMAC 0xf0006000
  118. #elif defined(CONFIG_CANCam)
  119. #define FEC_FLASHMAC 0xf0020000
  120. #elif defined (CONFIG_M5272C3)
  121. #define FEC_FLASHMAC (0xffe04000 + 4)
  122. #elif defined(CONFIG_MOD5272)
  123. #define FEC_FLASHMAC 0xffc0406b
  124. #else
  125. #define FEC_FLASHMAC 0
  126. #endif
  127. #endif /* CONFIG_M5272 */
  128. /* The number of Tx and Rx buffers. These are allocated from the page
  129. * pool. The code may assume these are power of two, so it it best
  130. * to keep them that size.
  131. * We don't need to allocate pages for the transmitter. We just use
  132. * the skbuffer directly.
  133. */
  134. #define FEC_ENET_RX_PAGES 8
  135. #define FEC_ENET_RX_FRSIZE 2048
  136. #define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
  137. #define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
  138. #define FEC_ENET_TX_FRSIZE 2048
  139. #define FEC_ENET_TX_FRPPG (PAGE_SIZE / FEC_ENET_TX_FRSIZE)
  140. #define TX_RING_SIZE 16 /* Must be power of two */
  141. #define TX_RING_MOD_MASK 15 /* for this to work */
  142. #if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
  143. #error "FEC: descriptor ring size constants too large"
  144. #endif
  145. /* Interrupt events/masks. */
  146. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  147. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  148. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  149. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  150. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  151. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  152. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  153. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  154. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  155. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  156. #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
  157. /* The FEC stores dest/src/type, data, and checksum for receive packets.
  158. */
  159. #define PKT_MAXBUF_SIZE 1518
  160. #define PKT_MINBUF_SIZE 64
  161. #define PKT_MAXBLR_SIZE 1520
  162. /* This device has up to three irqs on some platforms */
  163. #define FEC_IRQ_NUM 3
  164. /*
  165. * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
  166. * size bits. Other FEC hardware does not, so we need to take that into
  167. * account when setting it.
  168. */
  169. #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  170. defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
  171. #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
  172. #else
  173. #define OPT_FRAME_SIZE 0
  174. #endif
  175. /* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
  176. * tx_bd_base always point to the base of the buffer descriptors. The
  177. * cur_rx and cur_tx point to the currently available buffer.
  178. * The dirty_tx tracks the current buffer that is being sent by the
  179. * controller. The cur_tx and dirty_tx are equal under both completely
  180. * empty and completely full conditions. The empty/ready indicator in
  181. * the buffer descriptor determines the actual condition.
  182. */
  183. struct fec_enet_private {
  184. /* Hardware registers of the FEC device */
  185. void __iomem *hwp;
  186. struct net_device *netdev;
  187. struct clk *clk;
  188. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  189. unsigned char *tx_bounce[TX_RING_SIZE];
  190. struct sk_buff* tx_skbuff[TX_RING_SIZE];
  191. struct sk_buff* rx_skbuff[RX_RING_SIZE];
  192. ushort skb_cur;
  193. ushort skb_dirty;
  194. /* CPM dual port RAM relative addresses */
  195. dma_addr_t bd_dma;
  196. /* Address of Rx and Tx buffers */
  197. struct bufdesc *rx_bd_base;
  198. struct bufdesc *tx_bd_base;
  199. /* The next free ring entry */
  200. struct bufdesc *cur_rx, *cur_tx;
  201. /* The ring entries to be free()ed */
  202. struct bufdesc *dirty_tx;
  203. uint tx_full;
  204. /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
  205. spinlock_t hw_lock;
  206. struct platform_device *pdev;
  207. int opened;
  208. /* Phylib and MDIO interface */
  209. struct mii_bus *mii_bus;
  210. struct phy_device *phy_dev;
  211. int mii_timeout;
  212. uint phy_speed;
  213. phy_interface_t phy_interface;
  214. int link;
  215. int full_duplex;
  216. struct completion mdio_done;
  217. int irq[FEC_IRQ_NUM];
  218. };
  219. /* FEC MII MMFR bits definition */
  220. #define FEC_MMFR_ST (1 << 30)
  221. #define FEC_MMFR_OP_READ (2 << 28)
  222. #define FEC_MMFR_OP_WRITE (1 << 28)
  223. #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
  224. #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
  225. #define FEC_MMFR_TA (2 << 16)
  226. #define FEC_MMFR_DATA(v) (v & 0xffff)
  227. #define FEC_MII_TIMEOUT 1000 /* us */
  228. /* Transmitter timeout */
  229. #define TX_TIMEOUT (2 * HZ)
  230. static void *swap_buffer(void *bufaddr, int len)
  231. {
  232. int i;
  233. unsigned int *buf = bufaddr;
  234. for (i = 0; i < (len + 3) / 4; i++, buf++)
  235. *buf = cpu_to_be32(*buf);
  236. return bufaddr;
  237. }
  238. static netdev_tx_t
  239. fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  240. {
  241. struct fec_enet_private *fep = netdev_priv(ndev);
  242. const struct platform_device_id *id_entry =
  243. platform_get_device_id(fep->pdev);
  244. struct bufdesc *bdp;
  245. void *bufaddr;
  246. unsigned short status;
  247. unsigned long flags;
  248. if (!fep->link) {
  249. /* Link is down or autonegotiation is in progress. */
  250. return NETDEV_TX_BUSY;
  251. }
  252. spin_lock_irqsave(&fep->hw_lock, flags);
  253. /* Fill in a Tx ring entry */
  254. bdp = fep->cur_tx;
  255. status = bdp->cbd_sc;
  256. if (status & BD_ENET_TX_READY) {
  257. /* Ooops. All transmit buffers are full. Bail out.
  258. * This should not happen, since ndev->tbusy should be set.
  259. */
  260. printk("%s: tx queue full!.\n", ndev->name);
  261. spin_unlock_irqrestore(&fep->hw_lock, flags);
  262. return NETDEV_TX_BUSY;
  263. }
  264. /* Clear all of the status flags */
  265. status &= ~BD_ENET_TX_STATS;
  266. /* Set buffer length and buffer pointer */
  267. bufaddr = skb->data;
  268. bdp->cbd_datlen = skb->len;
  269. /*
  270. * On some FEC implementations data must be aligned on
  271. * 4-byte boundaries. Use bounce buffers to copy data
  272. * and get it aligned. Ugh.
  273. */
  274. if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
  275. unsigned int index;
  276. index = bdp - fep->tx_bd_base;
  277. memcpy(fep->tx_bounce[index], skb->data, skb->len);
  278. bufaddr = fep->tx_bounce[index];
  279. }
  280. /*
  281. * Some design made an incorrect assumption on endian mode of
  282. * the system that it's running on. As the result, driver has to
  283. * swap every frame going to and coming from the controller.
  284. */
  285. if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
  286. swap_buffer(bufaddr, skb->len);
  287. /* Save skb pointer */
  288. fep->tx_skbuff[fep->skb_cur] = skb;
  289. ndev->stats.tx_bytes += skb->len;
  290. fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
  291. /* Push the data cache so the CPM does not get stale memory
  292. * data.
  293. */
  294. bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
  295. FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
  296. /* Send it on its way. Tell FEC it's ready, interrupt when done,
  297. * it's the last BD of the frame, and to put the CRC on the end.
  298. */
  299. status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
  300. | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  301. bdp->cbd_sc = status;
  302. /* Trigger transmission start */
  303. writel(0, fep->hwp + FEC_X_DES_ACTIVE);
  304. /* If this was the last BD in the ring, start at the beginning again. */
  305. if (status & BD_ENET_TX_WRAP)
  306. bdp = fep->tx_bd_base;
  307. else
  308. bdp++;
  309. if (bdp == fep->dirty_tx) {
  310. fep->tx_full = 1;
  311. netif_stop_queue(ndev);
  312. }
  313. fep->cur_tx = bdp;
  314. skb_tx_timestamp(skb);
  315. spin_unlock_irqrestore(&fep->hw_lock, flags);
  316. return NETDEV_TX_OK;
  317. }
  318. /* This function is called to start or restart the FEC during a link
  319. * change. This only happens when switching between half and full
  320. * duplex.
  321. */
  322. static void
  323. fec_restart(struct net_device *ndev, int duplex)
  324. {
  325. struct fec_enet_private *fep = netdev_priv(ndev);
  326. const struct platform_device_id *id_entry =
  327. platform_get_device_id(fep->pdev);
  328. int i;
  329. u32 temp_mac[2];
  330. u32 rcntl = OPT_FRAME_SIZE | 0x04;
  331. u32 ecntl = 0x2; /* ETHEREN */
  332. /* Whack a reset. We should wait for this. */
  333. writel(1, fep->hwp + FEC_ECNTRL);
  334. udelay(10);
  335. /*
  336. * enet-mac reset will reset mac address registers too,
  337. * so need to reconfigure it.
  338. */
  339. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
  340. memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
  341. writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
  342. writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
  343. }
  344. /* Clear any outstanding interrupt. */
  345. writel(0xffc00000, fep->hwp + FEC_IEVENT);
  346. /* Reset all multicast. */
  347. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  348. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  349. #ifndef CONFIG_M5272
  350. writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
  351. writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
  352. #endif
  353. /* Set maximum receive buffer size. */
  354. writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
  355. /* Set receive and transmit descriptor base. */
  356. writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
  357. writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
  358. fep->hwp + FEC_X_DES_START);
  359. fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
  360. fep->cur_rx = fep->rx_bd_base;
  361. /* Reset SKB transmit buffers. */
  362. fep->skb_cur = fep->skb_dirty = 0;
  363. for (i = 0; i <= TX_RING_MOD_MASK; i++) {
  364. if (fep->tx_skbuff[i]) {
  365. dev_kfree_skb_any(fep->tx_skbuff[i]);
  366. fep->tx_skbuff[i] = NULL;
  367. }
  368. }
  369. /* Enable MII mode */
  370. if (duplex) {
  371. /* FD enable */
  372. writel(0x04, fep->hwp + FEC_X_CNTRL);
  373. } else {
  374. /* No Rcv on Xmit */
  375. rcntl |= 0x02;
  376. writel(0x0, fep->hwp + FEC_X_CNTRL);
  377. }
  378. fep->full_duplex = duplex;
  379. /* Set MII speed */
  380. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  381. /*
  382. * The phy interface and speed need to get configured
  383. * differently on enet-mac.
  384. */
  385. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
  386. /* Enable flow control and length check */
  387. rcntl |= 0x40000000 | 0x00000020;
  388. /* RGMII, RMII or MII */
  389. if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
  390. rcntl |= (1 << 6);
  391. else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
  392. rcntl |= (1 << 8);
  393. else
  394. rcntl &= ~(1 << 8);
  395. /* 1G, 100M or 10M */
  396. if (fep->phy_dev) {
  397. if (fep->phy_dev->speed == SPEED_1000)
  398. ecntl |= (1 << 5);
  399. else if (fep->phy_dev->speed == SPEED_100)
  400. rcntl &= ~(1 << 9);
  401. else
  402. rcntl |= (1 << 9);
  403. }
  404. } else {
  405. #ifdef FEC_MIIGSK_ENR
  406. if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
  407. /* disable the gasket and wait */
  408. writel(0, fep->hwp + FEC_MIIGSK_ENR);
  409. while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
  410. udelay(1);
  411. /*
  412. * configure the gasket:
  413. * RMII, 50 MHz, no loopback, no echo
  414. * MII, 25 MHz, no loopback, no echo
  415. */
  416. writel((fep->phy_interface == PHY_INTERFACE_MODE_RMII) ?
  417. 1 : 0, fep->hwp + FEC_MIIGSK_CFGR);
  418. /* re-enable the gasket */
  419. writel(2, fep->hwp + FEC_MIIGSK_ENR);
  420. }
  421. #endif
  422. }
  423. writel(rcntl, fep->hwp + FEC_R_CNTRL);
  424. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
  425. /* enable ENET endian swap */
  426. ecntl |= (1 << 8);
  427. /* enable ENET store and forward mode */
  428. writel(1 << 8, fep->hwp + FEC_X_WMRK);
  429. }
  430. /* And last, enable the transmit and receive processing */
  431. writel(ecntl, fep->hwp + FEC_ECNTRL);
  432. writel(0, fep->hwp + FEC_R_DES_ACTIVE);
  433. /* Enable interrupts we wish to service */
  434. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  435. }
  436. static void
  437. fec_stop(struct net_device *ndev)
  438. {
  439. struct fec_enet_private *fep = netdev_priv(ndev);
  440. const struct platform_device_id *id_entry =
  441. platform_get_device_id(fep->pdev);
  442. /* We cannot expect a graceful transmit stop without link !!! */
  443. if (fep->link) {
  444. writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
  445. udelay(10);
  446. if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
  447. printk("fec_stop : Graceful transmit stop did not complete !\n");
  448. }
  449. /* Whack a reset. We should wait for this. */
  450. writel(1, fep->hwp + FEC_ECNTRL);
  451. udelay(10);
  452. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  453. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  454. /* We have to keep ENET enabled to have MII interrupt stay working */
  455. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
  456. writel(2, fep->hwp + FEC_ECNTRL);
  457. }
  458. static void
  459. fec_timeout(struct net_device *ndev)
  460. {
  461. struct fec_enet_private *fep = netdev_priv(ndev);
  462. ndev->stats.tx_errors++;
  463. fec_restart(ndev, fep->full_duplex);
  464. netif_wake_queue(ndev);
  465. }
  466. static void
  467. fec_enet_tx(struct net_device *ndev)
  468. {
  469. struct fec_enet_private *fep;
  470. struct bufdesc *bdp;
  471. unsigned short status;
  472. struct sk_buff *skb;
  473. fep = netdev_priv(ndev);
  474. spin_lock(&fep->hw_lock);
  475. bdp = fep->dirty_tx;
  476. while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
  477. if (bdp == fep->cur_tx && fep->tx_full == 0)
  478. break;
  479. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  480. FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
  481. bdp->cbd_bufaddr = 0;
  482. skb = fep->tx_skbuff[fep->skb_dirty];
  483. /* Check for errors. */
  484. if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  485. BD_ENET_TX_RL | BD_ENET_TX_UN |
  486. BD_ENET_TX_CSL)) {
  487. ndev->stats.tx_errors++;
  488. if (status & BD_ENET_TX_HB) /* No heartbeat */
  489. ndev->stats.tx_heartbeat_errors++;
  490. if (status & BD_ENET_TX_LC) /* Late collision */
  491. ndev->stats.tx_window_errors++;
  492. if (status & BD_ENET_TX_RL) /* Retrans limit */
  493. ndev->stats.tx_aborted_errors++;
  494. if (status & BD_ENET_TX_UN) /* Underrun */
  495. ndev->stats.tx_fifo_errors++;
  496. if (status & BD_ENET_TX_CSL) /* Carrier lost */
  497. ndev->stats.tx_carrier_errors++;
  498. } else {
  499. ndev->stats.tx_packets++;
  500. }
  501. if (status & BD_ENET_TX_READY)
  502. printk("HEY! Enet xmit interrupt and TX_READY.\n");
  503. /* Deferred means some collisions occurred during transmit,
  504. * but we eventually sent the packet OK.
  505. */
  506. if (status & BD_ENET_TX_DEF)
  507. ndev->stats.collisions++;
  508. /* Free the sk buffer associated with this last transmit */
  509. dev_kfree_skb_any(skb);
  510. fep->tx_skbuff[fep->skb_dirty] = NULL;
  511. fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
  512. /* Update pointer to next buffer descriptor to be transmitted */
  513. if (status & BD_ENET_TX_WRAP)
  514. bdp = fep->tx_bd_base;
  515. else
  516. bdp++;
  517. /* Since we have freed up a buffer, the ring is no longer full
  518. */
  519. if (fep->tx_full) {
  520. fep->tx_full = 0;
  521. if (netif_queue_stopped(ndev))
  522. netif_wake_queue(ndev);
  523. }
  524. }
  525. fep->dirty_tx = bdp;
  526. spin_unlock(&fep->hw_lock);
  527. }
  528. /* During a receive, the cur_rx points to the current incoming buffer.
  529. * When we update through the ring, if the next incoming buffer has
  530. * not been given to the system, we just set the empty indicator,
  531. * effectively tossing the packet.
  532. */
  533. static void
  534. fec_enet_rx(struct net_device *ndev)
  535. {
  536. struct fec_enet_private *fep = netdev_priv(ndev);
  537. const struct platform_device_id *id_entry =
  538. platform_get_device_id(fep->pdev);
  539. struct bufdesc *bdp;
  540. unsigned short status;
  541. struct sk_buff *skb;
  542. ushort pkt_len;
  543. __u8 *data;
  544. #ifdef CONFIG_M532x
  545. flush_cache_all();
  546. #endif
  547. spin_lock(&fep->hw_lock);
  548. /* First, grab all of the stats for the incoming packet.
  549. * These get messed up if we get called due to a busy condition.
  550. */
  551. bdp = fep->cur_rx;
  552. while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
  553. /* Since we have allocated space to hold a complete frame,
  554. * the last indicator should be set.
  555. */
  556. if ((status & BD_ENET_RX_LAST) == 0)
  557. printk("FEC ENET: rcv is not +last\n");
  558. if (!fep->opened)
  559. goto rx_processing_done;
  560. /* Check for errors. */
  561. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
  562. BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  563. ndev->stats.rx_errors++;
  564. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
  565. /* Frame too long or too short. */
  566. ndev->stats.rx_length_errors++;
  567. }
  568. if (status & BD_ENET_RX_NO) /* Frame alignment */
  569. ndev->stats.rx_frame_errors++;
  570. if (status & BD_ENET_RX_CR) /* CRC Error */
  571. ndev->stats.rx_crc_errors++;
  572. if (status & BD_ENET_RX_OV) /* FIFO overrun */
  573. ndev->stats.rx_fifo_errors++;
  574. }
  575. /* Report late collisions as a frame error.
  576. * On this error, the BD is closed, but we don't know what we
  577. * have in the buffer. So, just drop this frame on the floor.
  578. */
  579. if (status & BD_ENET_RX_CL) {
  580. ndev->stats.rx_errors++;
  581. ndev->stats.rx_frame_errors++;
  582. goto rx_processing_done;
  583. }
  584. /* Process the incoming frame. */
  585. ndev->stats.rx_packets++;
  586. pkt_len = bdp->cbd_datlen;
  587. ndev->stats.rx_bytes += pkt_len;
  588. data = (__u8*)__va(bdp->cbd_bufaddr);
  589. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  590. FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
  591. if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
  592. swap_buffer(data, pkt_len);
  593. /* This does 16 byte alignment, exactly what we need.
  594. * The packet length includes FCS, but we don't want to
  595. * include that when passing upstream as it messes up
  596. * bridging applications.
  597. */
  598. skb = dev_alloc_skb(pkt_len - 4 + NET_IP_ALIGN);
  599. if (unlikely(!skb)) {
  600. printk("%s: Memory squeeze, dropping packet.\n",
  601. ndev->name);
  602. ndev->stats.rx_dropped++;
  603. } else {
  604. skb_reserve(skb, NET_IP_ALIGN);
  605. skb_put(skb, pkt_len - 4); /* Make room */
  606. skb_copy_to_linear_data(skb, data, pkt_len - 4);
  607. skb->protocol = eth_type_trans(skb, ndev);
  608. if (!skb_defer_rx_timestamp(skb))
  609. netif_rx(skb);
  610. }
  611. bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
  612. FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
  613. rx_processing_done:
  614. /* Clear the status flags for this buffer */
  615. status &= ~BD_ENET_RX_STATS;
  616. /* Mark the buffer empty */
  617. status |= BD_ENET_RX_EMPTY;
  618. bdp->cbd_sc = status;
  619. /* Update BD pointer to next entry */
  620. if (status & BD_ENET_RX_WRAP)
  621. bdp = fep->rx_bd_base;
  622. else
  623. bdp++;
  624. /* Doing this here will keep the FEC running while we process
  625. * incoming frames. On a heavily loaded network, we should be
  626. * able to keep up at the expense of system resources.
  627. */
  628. writel(0, fep->hwp + FEC_R_DES_ACTIVE);
  629. }
  630. fep->cur_rx = bdp;
  631. spin_unlock(&fep->hw_lock);
  632. }
  633. static irqreturn_t
  634. fec_enet_interrupt(int irq, void *dev_id)
  635. {
  636. struct net_device *ndev = dev_id;
  637. struct fec_enet_private *fep = netdev_priv(ndev);
  638. uint int_events;
  639. irqreturn_t ret = IRQ_NONE;
  640. do {
  641. int_events = readl(fep->hwp + FEC_IEVENT);
  642. writel(int_events, fep->hwp + FEC_IEVENT);
  643. if (int_events & FEC_ENET_RXF) {
  644. ret = IRQ_HANDLED;
  645. fec_enet_rx(ndev);
  646. }
  647. /* Transmit OK, or non-fatal error. Update the buffer
  648. * descriptors. FEC handles all errors, we just discover
  649. * them as part of the transmit process.
  650. */
  651. if (int_events & FEC_ENET_TXF) {
  652. ret = IRQ_HANDLED;
  653. fec_enet_tx(ndev);
  654. }
  655. if (int_events & FEC_ENET_MII) {
  656. ret = IRQ_HANDLED;
  657. complete(&fep->mdio_done);
  658. }
  659. } while (int_events);
  660. return ret;
  661. }
  662. /* ------------------------------------------------------------------------- */
  663. static void __inline__ fec_get_mac(struct net_device *ndev)
  664. {
  665. struct fec_enet_private *fep = netdev_priv(ndev);
  666. struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
  667. unsigned char *iap, tmpaddr[ETH_ALEN];
  668. /*
  669. * try to get mac address in following order:
  670. *
  671. * 1) module parameter via kernel command line in form
  672. * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
  673. */
  674. iap = macaddr;
  675. #ifdef CONFIG_OF
  676. /*
  677. * 2) from device tree data
  678. */
  679. if (!is_valid_ether_addr(iap)) {
  680. struct device_node *np = fep->pdev->dev.of_node;
  681. if (np) {
  682. const char *mac = of_get_mac_address(np);
  683. if (mac)
  684. iap = (unsigned char *) mac;
  685. }
  686. }
  687. #endif
  688. /*
  689. * 3) from flash or fuse (via platform data)
  690. */
  691. if (!is_valid_ether_addr(iap)) {
  692. #ifdef CONFIG_M5272
  693. if (FEC_FLASHMAC)
  694. iap = (unsigned char *)FEC_FLASHMAC;
  695. #else
  696. if (pdata)
  697. memcpy(iap, pdata->mac, ETH_ALEN);
  698. #endif
  699. }
  700. /*
  701. * 4) FEC mac registers set by bootloader
  702. */
  703. if (!is_valid_ether_addr(iap)) {
  704. *((unsigned long *) &tmpaddr[0]) =
  705. be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
  706. *((unsigned short *) &tmpaddr[4]) =
  707. be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
  708. iap = &tmpaddr[0];
  709. }
  710. memcpy(ndev->dev_addr, iap, ETH_ALEN);
  711. /* Adjust MAC if using macaddr */
  712. if (iap == macaddr)
  713. ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->pdev->id;
  714. }
  715. /* ------------------------------------------------------------------------- */
  716. /*
  717. * Phy section
  718. */
  719. static void fec_enet_adjust_link(struct net_device *ndev)
  720. {
  721. struct fec_enet_private *fep = netdev_priv(ndev);
  722. struct phy_device *phy_dev = fep->phy_dev;
  723. unsigned long flags;
  724. int status_change = 0;
  725. spin_lock_irqsave(&fep->hw_lock, flags);
  726. /* Prevent a state halted on mii error */
  727. if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
  728. phy_dev->state = PHY_RESUMING;
  729. goto spin_unlock;
  730. }
  731. /* Duplex link change */
  732. if (phy_dev->link) {
  733. if (fep->full_duplex != phy_dev->duplex) {
  734. fec_restart(ndev, phy_dev->duplex);
  735. status_change = 1;
  736. }
  737. }
  738. /* Link on or off change */
  739. if (phy_dev->link != fep->link) {
  740. fep->link = phy_dev->link;
  741. if (phy_dev->link)
  742. fec_restart(ndev, phy_dev->duplex);
  743. else
  744. fec_stop(ndev);
  745. status_change = 1;
  746. }
  747. spin_unlock:
  748. spin_unlock_irqrestore(&fep->hw_lock, flags);
  749. if (status_change)
  750. phy_print_status(phy_dev);
  751. }
  752. static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  753. {
  754. struct fec_enet_private *fep = bus->priv;
  755. unsigned long time_left;
  756. fep->mii_timeout = 0;
  757. init_completion(&fep->mdio_done);
  758. /* start a read op */
  759. writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
  760. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  761. FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
  762. /* wait for end of transfer */
  763. time_left = wait_for_completion_timeout(&fep->mdio_done,
  764. usecs_to_jiffies(FEC_MII_TIMEOUT));
  765. if (time_left == 0) {
  766. fep->mii_timeout = 1;
  767. printk(KERN_ERR "FEC: MDIO read timeout\n");
  768. return -ETIMEDOUT;
  769. }
  770. /* return value */
  771. return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
  772. }
  773. static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  774. u16 value)
  775. {
  776. struct fec_enet_private *fep = bus->priv;
  777. unsigned long time_left;
  778. fep->mii_timeout = 0;
  779. init_completion(&fep->mdio_done);
  780. /* start a write op */
  781. writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
  782. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  783. FEC_MMFR_TA | FEC_MMFR_DATA(value),
  784. fep->hwp + FEC_MII_DATA);
  785. /* wait for end of transfer */
  786. time_left = wait_for_completion_timeout(&fep->mdio_done,
  787. usecs_to_jiffies(FEC_MII_TIMEOUT));
  788. if (time_left == 0) {
  789. fep->mii_timeout = 1;
  790. printk(KERN_ERR "FEC: MDIO write timeout\n");
  791. return -ETIMEDOUT;
  792. }
  793. return 0;
  794. }
  795. static int fec_enet_mdio_reset(struct mii_bus *bus)
  796. {
  797. return 0;
  798. }
  799. static int fec_enet_mii_probe(struct net_device *ndev)
  800. {
  801. struct fec_enet_private *fep = netdev_priv(ndev);
  802. const struct platform_device_id *id_entry =
  803. platform_get_device_id(fep->pdev);
  804. struct phy_device *phy_dev = NULL;
  805. char mdio_bus_id[MII_BUS_ID_SIZE];
  806. char phy_name[MII_BUS_ID_SIZE + 3];
  807. int phy_id;
  808. int dev_id = fep->pdev->id;
  809. fep->phy_dev = NULL;
  810. /* check for attached phy */
  811. for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
  812. if ((fep->mii_bus->phy_mask & (1 << phy_id)))
  813. continue;
  814. if (fep->mii_bus->phy_map[phy_id] == NULL)
  815. continue;
  816. if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
  817. continue;
  818. if (dev_id--)
  819. continue;
  820. strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
  821. break;
  822. }
  823. if (phy_id >= PHY_MAX_ADDR) {
  824. printk(KERN_INFO
  825. "%s: no PHY, assuming direct connection to switch\n",
  826. ndev->name);
  827. strncpy(mdio_bus_id, "0", MII_BUS_ID_SIZE);
  828. phy_id = 0;
  829. }
  830. snprintf(phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT, mdio_bus_id, phy_id);
  831. phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, 0,
  832. fep->phy_interface);
  833. if (IS_ERR(phy_dev)) {
  834. printk(KERN_ERR "%s: could not attach to PHY\n", ndev->name);
  835. return PTR_ERR(phy_dev);
  836. }
  837. /* mask with MAC supported features */
  838. if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT)
  839. phy_dev->supported &= PHY_GBIT_FEATURES;
  840. else
  841. phy_dev->supported &= PHY_BASIC_FEATURES;
  842. phy_dev->advertising = phy_dev->supported;
  843. fep->phy_dev = phy_dev;
  844. fep->link = 0;
  845. fep->full_duplex = 0;
  846. printk(KERN_INFO
  847. "%s: Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  848. ndev->name,
  849. fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
  850. fep->phy_dev->irq);
  851. return 0;
  852. }
  853. static int fec_enet_mii_init(struct platform_device *pdev)
  854. {
  855. static struct mii_bus *fec0_mii_bus;
  856. struct net_device *ndev = platform_get_drvdata(pdev);
  857. struct fec_enet_private *fep = netdev_priv(ndev);
  858. const struct platform_device_id *id_entry =
  859. platform_get_device_id(fep->pdev);
  860. int err = -ENXIO, i;
  861. /*
  862. * The dual fec interfaces are not equivalent with enet-mac.
  863. * Here are the differences:
  864. *
  865. * - fec0 supports MII & RMII modes while fec1 only supports RMII
  866. * - fec0 acts as the 1588 time master while fec1 is slave
  867. * - external phys can only be configured by fec0
  868. *
  869. * That is to say fec1 can not work independently. It only works
  870. * when fec0 is working. The reason behind this design is that the
  871. * second interface is added primarily for Switch mode.
  872. *
  873. * Because of the last point above, both phys are attached on fec0
  874. * mdio interface in board design, and need to be configured by
  875. * fec0 mii_bus.
  876. */
  877. if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && pdev->id > 0) {
  878. /* fec1 uses fec0 mii_bus */
  879. fep->mii_bus = fec0_mii_bus;
  880. return 0;
  881. }
  882. fep->mii_timeout = 0;
  883. /*
  884. * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
  885. *
  886. * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
  887. * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
  888. * Reference Manual has an error on this, and gets fixed on i.MX6Q
  889. * document.
  890. */
  891. fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk), 5000000);
  892. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
  893. fep->phy_speed--;
  894. fep->phy_speed <<= 1;
  895. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  896. fep->mii_bus = mdiobus_alloc();
  897. if (fep->mii_bus == NULL) {
  898. err = -ENOMEM;
  899. goto err_out;
  900. }
  901. fep->mii_bus->name = "fec_enet_mii_bus";
  902. fep->mii_bus->read = fec_enet_mdio_read;
  903. fep->mii_bus->write = fec_enet_mdio_write;
  904. fep->mii_bus->reset = fec_enet_mdio_reset;
  905. snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id + 1);
  906. fep->mii_bus->priv = fep;
  907. fep->mii_bus->parent = &pdev->dev;
  908. fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  909. if (!fep->mii_bus->irq) {
  910. err = -ENOMEM;
  911. goto err_out_free_mdiobus;
  912. }
  913. for (i = 0; i < PHY_MAX_ADDR; i++)
  914. fep->mii_bus->irq[i] = PHY_POLL;
  915. if (mdiobus_register(fep->mii_bus))
  916. goto err_out_free_mdio_irq;
  917. /* save fec0 mii_bus */
  918. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
  919. fec0_mii_bus = fep->mii_bus;
  920. return 0;
  921. err_out_free_mdio_irq:
  922. kfree(fep->mii_bus->irq);
  923. err_out_free_mdiobus:
  924. mdiobus_free(fep->mii_bus);
  925. err_out:
  926. return err;
  927. }
  928. static void fec_enet_mii_remove(struct fec_enet_private *fep)
  929. {
  930. if (fep->phy_dev)
  931. phy_disconnect(fep->phy_dev);
  932. mdiobus_unregister(fep->mii_bus);
  933. kfree(fep->mii_bus->irq);
  934. mdiobus_free(fep->mii_bus);
  935. }
  936. static int fec_enet_get_settings(struct net_device *ndev,
  937. struct ethtool_cmd *cmd)
  938. {
  939. struct fec_enet_private *fep = netdev_priv(ndev);
  940. struct phy_device *phydev = fep->phy_dev;
  941. if (!phydev)
  942. return -ENODEV;
  943. return phy_ethtool_gset(phydev, cmd);
  944. }
  945. static int fec_enet_set_settings(struct net_device *ndev,
  946. struct ethtool_cmd *cmd)
  947. {
  948. struct fec_enet_private *fep = netdev_priv(ndev);
  949. struct phy_device *phydev = fep->phy_dev;
  950. if (!phydev)
  951. return -ENODEV;
  952. return phy_ethtool_sset(phydev, cmd);
  953. }
  954. static void fec_enet_get_drvinfo(struct net_device *ndev,
  955. struct ethtool_drvinfo *info)
  956. {
  957. struct fec_enet_private *fep = netdev_priv(ndev);
  958. strcpy(info->driver, fep->pdev->dev.driver->name);
  959. strcpy(info->version, "Revision: 1.0");
  960. strcpy(info->bus_info, dev_name(&ndev->dev));
  961. }
  962. static struct ethtool_ops fec_enet_ethtool_ops = {
  963. .get_settings = fec_enet_get_settings,
  964. .set_settings = fec_enet_set_settings,
  965. .get_drvinfo = fec_enet_get_drvinfo,
  966. .get_link = ethtool_op_get_link,
  967. };
  968. static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  969. {
  970. struct fec_enet_private *fep = netdev_priv(ndev);
  971. struct phy_device *phydev = fep->phy_dev;
  972. if (!netif_running(ndev))
  973. return -EINVAL;
  974. if (!phydev)
  975. return -ENODEV;
  976. return phy_mii_ioctl(phydev, rq, cmd);
  977. }
  978. static void fec_enet_free_buffers(struct net_device *ndev)
  979. {
  980. struct fec_enet_private *fep = netdev_priv(ndev);
  981. int i;
  982. struct sk_buff *skb;
  983. struct bufdesc *bdp;
  984. bdp = fep->rx_bd_base;
  985. for (i = 0; i < RX_RING_SIZE; i++) {
  986. skb = fep->rx_skbuff[i];
  987. if (bdp->cbd_bufaddr)
  988. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  989. FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
  990. if (skb)
  991. dev_kfree_skb(skb);
  992. bdp++;
  993. }
  994. bdp = fep->tx_bd_base;
  995. for (i = 0; i < TX_RING_SIZE; i++)
  996. kfree(fep->tx_bounce[i]);
  997. }
  998. static int fec_enet_alloc_buffers(struct net_device *ndev)
  999. {
  1000. struct fec_enet_private *fep = netdev_priv(ndev);
  1001. int i;
  1002. struct sk_buff *skb;
  1003. struct bufdesc *bdp;
  1004. bdp = fep->rx_bd_base;
  1005. for (i = 0; i < RX_RING_SIZE; i++) {
  1006. skb = dev_alloc_skb(FEC_ENET_RX_FRSIZE);
  1007. if (!skb) {
  1008. fec_enet_free_buffers(ndev);
  1009. return -ENOMEM;
  1010. }
  1011. fep->rx_skbuff[i] = skb;
  1012. bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
  1013. FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
  1014. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  1015. bdp++;
  1016. }
  1017. /* Set the last buffer to wrap. */
  1018. bdp--;
  1019. bdp->cbd_sc |= BD_SC_WRAP;
  1020. bdp = fep->tx_bd_base;
  1021. for (i = 0; i < TX_RING_SIZE; i++) {
  1022. fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
  1023. bdp->cbd_sc = 0;
  1024. bdp->cbd_bufaddr = 0;
  1025. bdp++;
  1026. }
  1027. /* Set the last buffer to wrap. */
  1028. bdp--;
  1029. bdp->cbd_sc |= BD_SC_WRAP;
  1030. return 0;
  1031. }
  1032. static int
  1033. fec_enet_open(struct net_device *ndev)
  1034. {
  1035. struct fec_enet_private *fep = netdev_priv(ndev);
  1036. int ret;
  1037. /* I should reset the ring buffers here, but I don't yet know
  1038. * a simple way to do that.
  1039. */
  1040. ret = fec_enet_alloc_buffers(ndev);
  1041. if (ret)
  1042. return ret;
  1043. /* Probe and connect to PHY when open the interface */
  1044. ret = fec_enet_mii_probe(ndev);
  1045. if (ret) {
  1046. fec_enet_free_buffers(ndev);
  1047. return ret;
  1048. }
  1049. phy_start(fep->phy_dev);
  1050. netif_start_queue(ndev);
  1051. fep->opened = 1;
  1052. return 0;
  1053. }
  1054. static int
  1055. fec_enet_close(struct net_device *ndev)
  1056. {
  1057. struct fec_enet_private *fep = netdev_priv(ndev);
  1058. /* Don't know what to do yet. */
  1059. fep->opened = 0;
  1060. netif_stop_queue(ndev);
  1061. fec_stop(ndev);
  1062. if (fep->phy_dev) {
  1063. phy_stop(fep->phy_dev);
  1064. phy_disconnect(fep->phy_dev);
  1065. }
  1066. fec_enet_free_buffers(ndev);
  1067. return 0;
  1068. }
  1069. /* Set or clear the multicast filter for this adaptor.
  1070. * Skeleton taken from sunlance driver.
  1071. * The CPM Ethernet implementation allows Multicast as well as individual
  1072. * MAC address filtering. Some of the drivers check to make sure it is
  1073. * a group multicast address, and discard those that are not. I guess I
  1074. * will do the same for now, but just remove the test if you want
  1075. * individual filtering as well (do the upper net layers want or support
  1076. * this kind of feature?).
  1077. */
  1078. #define HASH_BITS 6 /* #bits in hash */
  1079. #define CRC32_POLY 0xEDB88320
  1080. static void set_multicast_list(struct net_device *ndev)
  1081. {
  1082. struct fec_enet_private *fep = netdev_priv(ndev);
  1083. struct netdev_hw_addr *ha;
  1084. unsigned int i, bit, data, crc, tmp;
  1085. unsigned char hash;
  1086. if (ndev->flags & IFF_PROMISC) {
  1087. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1088. tmp |= 0x8;
  1089. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1090. return;
  1091. }
  1092. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1093. tmp &= ~0x8;
  1094. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1095. if (ndev->flags & IFF_ALLMULTI) {
  1096. /* Catch all multicast addresses, so set the
  1097. * filter to all 1's
  1098. */
  1099. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1100. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1101. return;
  1102. }
  1103. /* Clear filter and add the addresses in hash register
  1104. */
  1105. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1106. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1107. netdev_for_each_mc_addr(ha, ndev) {
  1108. /* calculate crc32 value of mac address */
  1109. crc = 0xffffffff;
  1110. for (i = 0; i < ndev->addr_len; i++) {
  1111. data = ha->addr[i];
  1112. for (bit = 0; bit < 8; bit++, data >>= 1) {
  1113. crc = (crc >> 1) ^
  1114. (((crc ^ data) & 1) ? CRC32_POLY : 0);
  1115. }
  1116. }
  1117. /* only upper 6 bits (HASH_BITS) are used
  1118. * which point to specific bit in he hash registers
  1119. */
  1120. hash = (crc >> (32 - HASH_BITS)) & 0x3f;
  1121. if (hash > 31) {
  1122. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1123. tmp |= 1 << (hash - 32);
  1124. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1125. } else {
  1126. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1127. tmp |= 1 << hash;
  1128. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1129. }
  1130. }
  1131. }
  1132. /* Set a MAC change in hardware. */
  1133. static int
  1134. fec_set_mac_address(struct net_device *ndev, void *p)
  1135. {
  1136. struct fec_enet_private *fep = netdev_priv(ndev);
  1137. struct sockaddr *addr = p;
  1138. if (!is_valid_ether_addr(addr->sa_data))
  1139. return -EADDRNOTAVAIL;
  1140. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  1141. writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
  1142. (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
  1143. fep->hwp + FEC_ADDR_LOW);
  1144. writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
  1145. fep->hwp + FEC_ADDR_HIGH);
  1146. return 0;
  1147. }
  1148. #ifdef CONFIG_NET_POLL_CONTROLLER
  1149. /*
  1150. * fec_poll_controller: FEC Poll controller function
  1151. * @dev: The FEC network adapter
  1152. *
  1153. * Polled functionality used by netconsole and others in non interrupt mode
  1154. *
  1155. */
  1156. void fec_poll_controller(struct net_device *dev)
  1157. {
  1158. int i;
  1159. struct fec_enet_private *fep = netdev_priv(dev);
  1160. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1161. if (fep->irq[i] > 0) {
  1162. disable_irq(fep->irq[i]);
  1163. fec_enet_interrupt(fep->irq[i], dev);
  1164. enable_irq(fep->irq[i]);
  1165. }
  1166. }
  1167. }
  1168. #endif
  1169. static const struct net_device_ops fec_netdev_ops = {
  1170. .ndo_open = fec_enet_open,
  1171. .ndo_stop = fec_enet_close,
  1172. .ndo_start_xmit = fec_enet_start_xmit,
  1173. .ndo_set_rx_mode = set_multicast_list,
  1174. .ndo_change_mtu = eth_change_mtu,
  1175. .ndo_validate_addr = eth_validate_addr,
  1176. .ndo_tx_timeout = fec_timeout,
  1177. .ndo_set_mac_address = fec_set_mac_address,
  1178. .ndo_do_ioctl = fec_enet_ioctl,
  1179. #ifdef CONFIG_NET_POLL_CONTROLLER
  1180. .ndo_poll_controller = fec_poll_controller,
  1181. #endif
  1182. };
  1183. /*
  1184. * XXX: We need to clean up on failure exits here.
  1185. *
  1186. */
  1187. static int fec_enet_init(struct net_device *ndev)
  1188. {
  1189. struct fec_enet_private *fep = netdev_priv(ndev);
  1190. struct bufdesc *cbd_base;
  1191. struct bufdesc *bdp;
  1192. int i;
  1193. /* Allocate memory for buffer descriptors. */
  1194. cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
  1195. GFP_KERNEL);
  1196. if (!cbd_base) {
  1197. printk("FEC: allocate descriptor memory failed?\n");
  1198. return -ENOMEM;
  1199. }
  1200. spin_lock_init(&fep->hw_lock);
  1201. fep->netdev = ndev;
  1202. /* Get the Ethernet address */
  1203. fec_get_mac(ndev);
  1204. /* Set receive and transmit descriptor base. */
  1205. fep->rx_bd_base = cbd_base;
  1206. fep->tx_bd_base = cbd_base + RX_RING_SIZE;
  1207. /* The FEC Ethernet specific entries in the device structure */
  1208. ndev->watchdog_timeo = TX_TIMEOUT;
  1209. ndev->netdev_ops = &fec_netdev_ops;
  1210. ndev->ethtool_ops = &fec_enet_ethtool_ops;
  1211. /* Initialize the receive buffer descriptors. */
  1212. bdp = fep->rx_bd_base;
  1213. for (i = 0; i < RX_RING_SIZE; i++) {
  1214. /* Initialize the BD for every fragment in the page. */
  1215. bdp->cbd_sc = 0;
  1216. bdp++;
  1217. }
  1218. /* Set the last buffer to wrap */
  1219. bdp--;
  1220. bdp->cbd_sc |= BD_SC_WRAP;
  1221. /* ...and the same for transmit */
  1222. bdp = fep->tx_bd_base;
  1223. for (i = 0; i < TX_RING_SIZE; i++) {
  1224. /* Initialize the BD for every fragment in the page. */
  1225. bdp->cbd_sc = 0;
  1226. bdp->cbd_bufaddr = 0;
  1227. bdp++;
  1228. }
  1229. /* Set the last buffer to wrap */
  1230. bdp--;
  1231. bdp->cbd_sc |= BD_SC_WRAP;
  1232. fec_restart(ndev, 0);
  1233. return 0;
  1234. }
  1235. #ifdef CONFIG_OF
  1236. static int __devinit fec_get_phy_mode_dt(struct platform_device *pdev)
  1237. {
  1238. struct device_node *np = pdev->dev.of_node;
  1239. if (np)
  1240. return of_get_phy_mode(np);
  1241. return -ENODEV;
  1242. }
  1243. static void __devinit fec_reset_phy(struct platform_device *pdev)
  1244. {
  1245. int err, phy_reset;
  1246. struct device_node *np = pdev->dev.of_node;
  1247. if (!np)
  1248. return;
  1249. phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
  1250. err = gpio_request_one(phy_reset, GPIOF_OUT_INIT_LOW, "phy-reset");
  1251. if (err) {
  1252. pr_debug("FEC: failed to get gpio phy-reset: %d\n", err);
  1253. return;
  1254. }
  1255. msleep(1);
  1256. gpio_set_value(phy_reset, 1);
  1257. }
  1258. #else /* CONFIG_OF */
  1259. static inline int fec_get_phy_mode_dt(struct platform_device *pdev)
  1260. {
  1261. return -ENODEV;
  1262. }
  1263. static inline void fec_reset_phy(struct platform_device *pdev)
  1264. {
  1265. /*
  1266. * In case of platform probe, the reset has been done
  1267. * by machine code.
  1268. */
  1269. }
  1270. #endif /* CONFIG_OF */
  1271. static int __devinit
  1272. fec_probe(struct platform_device *pdev)
  1273. {
  1274. struct fec_enet_private *fep;
  1275. struct fec_platform_data *pdata;
  1276. struct net_device *ndev;
  1277. int i, irq, ret = 0;
  1278. struct resource *r;
  1279. const struct of_device_id *of_id;
  1280. of_id = of_match_device(fec_dt_ids, &pdev->dev);
  1281. if (of_id)
  1282. pdev->id_entry = of_id->data;
  1283. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1284. if (!r)
  1285. return -ENXIO;
  1286. r = request_mem_region(r->start, resource_size(r), pdev->name);
  1287. if (!r)
  1288. return -EBUSY;
  1289. /* Init network device */
  1290. ndev = alloc_etherdev(sizeof(struct fec_enet_private));
  1291. if (!ndev) {
  1292. ret = -ENOMEM;
  1293. goto failed_alloc_etherdev;
  1294. }
  1295. SET_NETDEV_DEV(ndev, &pdev->dev);
  1296. /* setup board info structure */
  1297. fep = netdev_priv(ndev);
  1298. fep->hwp = ioremap(r->start, resource_size(r));
  1299. fep->pdev = pdev;
  1300. if (!fep->hwp) {
  1301. ret = -ENOMEM;
  1302. goto failed_ioremap;
  1303. }
  1304. platform_set_drvdata(pdev, ndev);
  1305. ret = fec_get_phy_mode_dt(pdev);
  1306. if (ret < 0) {
  1307. pdata = pdev->dev.platform_data;
  1308. if (pdata)
  1309. fep->phy_interface = pdata->phy;
  1310. else
  1311. fep->phy_interface = PHY_INTERFACE_MODE_MII;
  1312. } else {
  1313. fep->phy_interface = ret;
  1314. }
  1315. fec_reset_phy(pdev);
  1316. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1317. irq = platform_get_irq(pdev, i);
  1318. if (i && irq < 0)
  1319. break;
  1320. ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
  1321. if (ret) {
  1322. while (--i >= 0) {
  1323. irq = platform_get_irq(pdev, i);
  1324. free_irq(irq, ndev);
  1325. }
  1326. goto failed_irq;
  1327. }
  1328. }
  1329. fep->clk = clk_get(&pdev->dev, "fec_clk");
  1330. if (IS_ERR(fep->clk)) {
  1331. ret = PTR_ERR(fep->clk);
  1332. goto failed_clk;
  1333. }
  1334. clk_enable(fep->clk);
  1335. ret = fec_enet_init(ndev);
  1336. if (ret)
  1337. goto failed_init;
  1338. ret = fec_enet_mii_init(pdev);
  1339. if (ret)
  1340. goto failed_mii_init;
  1341. /* Carrier starts down, phylib will bring it up */
  1342. netif_carrier_off(ndev);
  1343. ret = register_netdev(ndev);
  1344. if (ret)
  1345. goto failed_register;
  1346. return 0;
  1347. failed_register:
  1348. fec_enet_mii_remove(fep);
  1349. failed_mii_init:
  1350. failed_init:
  1351. clk_disable(fep->clk);
  1352. clk_put(fep->clk);
  1353. failed_clk:
  1354. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1355. irq = platform_get_irq(pdev, i);
  1356. if (irq > 0)
  1357. free_irq(irq, ndev);
  1358. }
  1359. failed_irq:
  1360. iounmap(fep->hwp);
  1361. failed_ioremap:
  1362. free_netdev(ndev);
  1363. failed_alloc_etherdev:
  1364. release_mem_region(r->start, resource_size(r));
  1365. return ret;
  1366. }
  1367. static int __devexit
  1368. fec_drv_remove(struct platform_device *pdev)
  1369. {
  1370. struct net_device *ndev = platform_get_drvdata(pdev);
  1371. struct fec_enet_private *fep = netdev_priv(ndev);
  1372. struct resource *r;
  1373. fec_stop(ndev);
  1374. fec_enet_mii_remove(fep);
  1375. clk_disable(fep->clk);
  1376. clk_put(fep->clk);
  1377. iounmap(fep->hwp);
  1378. unregister_netdev(ndev);
  1379. free_netdev(ndev);
  1380. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1381. BUG_ON(!r);
  1382. release_mem_region(r->start, resource_size(r));
  1383. platform_set_drvdata(pdev, NULL);
  1384. return 0;
  1385. }
  1386. #ifdef CONFIG_PM
  1387. static int
  1388. fec_suspend(struct device *dev)
  1389. {
  1390. struct net_device *ndev = dev_get_drvdata(dev);
  1391. struct fec_enet_private *fep = netdev_priv(ndev);
  1392. if (netif_running(ndev)) {
  1393. fec_stop(ndev);
  1394. netif_device_detach(ndev);
  1395. }
  1396. clk_disable(fep->clk);
  1397. return 0;
  1398. }
  1399. static int
  1400. fec_resume(struct device *dev)
  1401. {
  1402. struct net_device *ndev = dev_get_drvdata(dev);
  1403. struct fec_enet_private *fep = netdev_priv(ndev);
  1404. clk_enable(fep->clk);
  1405. if (netif_running(ndev)) {
  1406. fec_restart(ndev, fep->full_duplex);
  1407. netif_device_attach(ndev);
  1408. }
  1409. return 0;
  1410. }
  1411. static const struct dev_pm_ops fec_pm_ops = {
  1412. .suspend = fec_suspend,
  1413. .resume = fec_resume,
  1414. .freeze = fec_suspend,
  1415. .thaw = fec_resume,
  1416. .poweroff = fec_suspend,
  1417. .restore = fec_resume,
  1418. };
  1419. #endif
  1420. static struct platform_driver fec_driver = {
  1421. .driver = {
  1422. .name = DRIVER_NAME,
  1423. .owner = THIS_MODULE,
  1424. #ifdef CONFIG_PM
  1425. .pm = &fec_pm_ops,
  1426. #endif
  1427. .of_match_table = fec_dt_ids,
  1428. },
  1429. .id_table = fec_devtype,
  1430. .probe = fec_probe,
  1431. .remove = __devexit_p(fec_drv_remove),
  1432. };
  1433. static int __init
  1434. fec_enet_module_init(void)
  1435. {
  1436. printk(KERN_INFO "FEC Ethernet Driver\n");
  1437. return platform_driver_register(&fec_driver);
  1438. }
  1439. static void __exit
  1440. fec_enet_cleanup(void)
  1441. {
  1442. platform_driver_unregister(&fec_driver);
  1443. }
  1444. module_exit(fec_enet_cleanup);
  1445. module_init(fec_enet_module_init);
  1446. MODULE_LICENSE("GPL");