fec_main.c 57 KB

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