fec_main.c 61 KB

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