fec_main.c 60 KB

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