fec_main.c 59 KB

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