fec_main.c 53 KB

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