fec_main.c 61 KB

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