fec_main.c 56 KB

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