fec_main.c 49 KB

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