fec_main.c 53 KB

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