fec.c 43 KB

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