fec_main.c 49 KB

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