fec_main.c 61 KB

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