fec_main.c 53 KB

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