fec_main.c 56 KB

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