fec.c 49 KB

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