fec_main.c 69 KB

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