fec_main.c 70 KB

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