fec.c 44 KB

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