fec_main.c 61 KB

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