fec_main.c 60 KB

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