fec_main.c 61 KB

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