fec_main.c 52 KB

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