fec.c 50 KB

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