fec_main.c 59 KB

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