fec.c 50 KB

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