fec_main.c 52 KB

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