fec.c 46 KB

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