fec_main.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  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. /* And last, enable the transmit and receive processing */
  519. writel(ecntl, fep->hwp + FEC_ECNTRL);
  520. writel(0, fep->hwp + FEC_R_DES_ACTIVE);
  521. if (fep->bufdesc_ex)
  522. fec_ptp_start_cyclecounter(ndev);
  523. /* Enable interrupts we wish to service */
  524. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  525. if (netif_running(ndev)) {
  526. netif_tx_unlock_bh(ndev);
  527. netif_wake_queue(ndev);
  528. napi_enable(&fep->napi);
  529. netif_device_attach(ndev);
  530. }
  531. }
  532. static void
  533. fec_stop(struct net_device *ndev)
  534. {
  535. struct fec_enet_private *fep = netdev_priv(ndev);
  536. const struct platform_device_id *id_entry =
  537. platform_get_device_id(fep->pdev);
  538. u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
  539. /* We cannot expect a graceful transmit stop without link !!! */
  540. if (fep->link) {
  541. writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
  542. udelay(10);
  543. if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
  544. netdev_err(ndev, "Graceful transmit stop did not complete!\n");
  545. }
  546. /* Whack a reset. We should wait for this. */
  547. writel(1, fep->hwp + FEC_ECNTRL);
  548. udelay(10);
  549. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  550. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  551. /* We have to keep ENET enabled to have MII interrupt stay working */
  552. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
  553. writel(2, fep->hwp + FEC_ECNTRL);
  554. writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
  555. }
  556. }
  557. static void
  558. fec_timeout(struct net_device *ndev)
  559. {
  560. struct fec_enet_private *fep = netdev_priv(ndev);
  561. ndev->stats.tx_errors++;
  562. fep->delay_work.timeout = true;
  563. schedule_delayed_work(&(fep->delay_work.delay_work), 0);
  564. }
  565. static void fec_enet_work(struct work_struct *work)
  566. {
  567. struct fec_enet_private *fep =
  568. container_of(work,
  569. struct fec_enet_private,
  570. delay_work.delay_work.work);
  571. if (fep->delay_work.timeout) {
  572. fep->delay_work.timeout = false;
  573. fec_restart(fep->netdev, fep->full_duplex);
  574. netif_wake_queue(fep->netdev);
  575. }
  576. }
  577. static void
  578. fec_enet_tx(struct net_device *ndev)
  579. {
  580. struct fec_enet_private *fep;
  581. struct bufdesc *bdp;
  582. unsigned short status;
  583. struct sk_buff *skb;
  584. int index = 0;
  585. fep = netdev_priv(ndev);
  586. bdp = fep->dirty_tx;
  587. /* get next bdp of dirty_tx */
  588. if (bdp->cbd_sc & BD_ENET_TX_WRAP)
  589. bdp = fep->tx_bd_base;
  590. else
  591. bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
  592. while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
  593. /* current queue is empty */
  594. if (bdp == fep->cur_tx)
  595. break;
  596. if (fep->bufdesc_ex)
  597. index = (struct bufdesc_ex *)bdp -
  598. (struct bufdesc_ex *)fep->tx_bd_base;
  599. else
  600. index = bdp - fep->tx_bd_base;
  601. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  602. FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
  603. bdp->cbd_bufaddr = 0;
  604. skb = fep->tx_skbuff[index];
  605. /* Check for errors. */
  606. if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  607. BD_ENET_TX_RL | BD_ENET_TX_UN |
  608. BD_ENET_TX_CSL)) {
  609. ndev->stats.tx_errors++;
  610. if (status & BD_ENET_TX_HB) /* No heartbeat */
  611. ndev->stats.tx_heartbeat_errors++;
  612. if (status & BD_ENET_TX_LC) /* Late collision */
  613. ndev->stats.tx_window_errors++;
  614. if (status & BD_ENET_TX_RL) /* Retrans limit */
  615. ndev->stats.tx_aborted_errors++;
  616. if (status & BD_ENET_TX_UN) /* Underrun */
  617. ndev->stats.tx_fifo_errors++;
  618. if (status & BD_ENET_TX_CSL) /* Carrier lost */
  619. ndev->stats.tx_carrier_errors++;
  620. } else {
  621. ndev->stats.tx_packets++;
  622. }
  623. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
  624. fep->bufdesc_ex) {
  625. struct skb_shared_hwtstamps shhwtstamps;
  626. unsigned long flags;
  627. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  628. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  629. spin_lock_irqsave(&fep->tmreg_lock, flags);
  630. shhwtstamps.hwtstamp = ns_to_ktime(
  631. timecounter_cyc2time(&fep->tc, ebdp->ts));
  632. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  633. skb_tstamp_tx(skb, &shhwtstamps);
  634. }
  635. if (status & BD_ENET_TX_READY)
  636. netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
  637. /* Deferred means some collisions occurred during transmit,
  638. * but we eventually sent the packet OK.
  639. */
  640. if (status & BD_ENET_TX_DEF)
  641. ndev->stats.collisions++;
  642. /* Free the sk buffer associated with this last transmit */
  643. dev_kfree_skb_any(skb);
  644. fep->tx_skbuff[index] = NULL;
  645. fep->dirty_tx = bdp;
  646. /* Update pointer to next buffer descriptor to be transmitted */
  647. if (status & BD_ENET_TX_WRAP)
  648. bdp = fep->tx_bd_base;
  649. else
  650. bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
  651. /* Since we have freed up a buffer, the ring is no longer full
  652. */
  653. if (fep->dirty_tx != fep->cur_tx) {
  654. if (netif_queue_stopped(ndev))
  655. netif_wake_queue(ndev);
  656. }
  657. }
  658. return;
  659. }
  660. /* During a receive, the cur_rx points to the current incoming buffer.
  661. * When we update through the ring, if the next incoming buffer has
  662. * not been given to the system, we just set the empty indicator,
  663. * effectively tossing the packet.
  664. */
  665. static int
  666. fec_enet_rx(struct net_device *ndev, int budget)
  667. {
  668. struct fec_enet_private *fep = netdev_priv(ndev);
  669. const struct platform_device_id *id_entry =
  670. platform_get_device_id(fep->pdev);
  671. struct bufdesc *bdp;
  672. unsigned short status;
  673. struct sk_buff *skb;
  674. ushort pkt_len;
  675. __u8 *data;
  676. int pkt_received = 0;
  677. #ifdef CONFIG_M532x
  678. flush_cache_all();
  679. #endif
  680. /* First, grab all of the stats for the incoming packet.
  681. * These get messed up if we get called due to a busy condition.
  682. */
  683. bdp = fep->cur_rx;
  684. while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
  685. if (pkt_received >= budget)
  686. break;
  687. pkt_received++;
  688. /* Since we have allocated space to hold a complete frame,
  689. * the last indicator should be set.
  690. */
  691. if ((status & BD_ENET_RX_LAST) == 0)
  692. netdev_err(ndev, "rcv is not +last\n");
  693. if (!fep->opened)
  694. goto rx_processing_done;
  695. /* Check for errors. */
  696. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
  697. BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  698. ndev->stats.rx_errors++;
  699. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
  700. /* Frame too long or too short. */
  701. ndev->stats.rx_length_errors++;
  702. }
  703. if (status & BD_ENET_RX_NO) /* Frame alignment */
  704. ndev->stats.rx_frame_errors++;
  705. if (status & BD_ENET_RX_CR) /* CRC Error */
  706. ndev->stats.rx_crc_errors++;
  707. if (status & BD_ENET_RX_OV) /* FIFO overrun */
  708. ndev->stats.rx_fifo_errors++;
  709. }
  710. /* Report late collisions as a frame error.
  711. * On this error, the BD is closed, but we don't know what we
  712. * have in the buffer. So, just drop this frame on the floor.
  713. */
  714. if (status & BD_ENET_RX_CL) {
  715. ndev->stats.rx_errors++;
  716. ndev->stats.rx_frame_errors++;
  717. goto rx_processing_done;
  718. }
  719. /* Process the incoming frame. */
  720. ndev->stats.rx_packets++;
  721. pkt_len = bdp->cbd_datlen;
  722. ndev->stats.rx_bytes += pkt_len;
  723. data = (__u8*)__va(bdp->cbd_bufaddr);
  724. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  725. FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
  726. if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
  727. swap_buffer(data, pkt_len);
  728. /* This does 16 byte alignment, exactly what we need.
  729. * The packet length includes FCS, but we don't want to
  730. * include that when passing upstream as it messes up
  731. * bridging applications.
  732. */
  733. skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
  734. if (unlikely(!skb)) {
  735. ndev->stats.rx_dropped++;
  736. } else {
  737. skb_reserve(skb, NET_IP_ALIGN);
  738. skb_put(skb, pkt_len - 4); /* Make room */
  739. skb_copy_to_linear_data(skb, data, pkt_len - 4);
  740. skb->protocol = eth_type_trans(skb, ndev);
  741. /* Get receive timestamp from the skb */
  742. if (fep->hwts_rx_en && fep->bufdesc_ex) {
  743. struct skb_shared_hwtstamps *shhwtstamps =
  744. skb_hwtstamps(skb);
  745. unsigned long flags;
  746. struct bufdesc_ex *ebdp =
  747. (struct bufdesc_ex *)bdp;
  748. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  749. spin_lock_irqsave(&fep->tmreg_lock, flags);
  750. shhwtstamps->hwtstamp = ns_to_ktime(
  751. timecounter_cyc2time(&fep->tc, ebdp->ts));
  752. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  753. }
  754. if (fep->bufdesc_ex &&
  755. (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
  756. struct bufdesc_ex *ebdp =
  757. (struct bufdesc_ex *)bdp;
  758. if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
  759. /* don't check it */
  760. skb->ip_summed = CHECKSUM_UNNECESSARY;
  761. } else {
  762. skb_checksum_none_assert(skb);
  763. }
  764. }
  765. if (!skb_defer_rx_timestamp(skb))
  766. napi_gro_receive(&fep->napi, skb);
  767. }
  768. bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
  769. FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
  770. rx_processing_done:
  771. /* Clear the status flags for this buffer */
  772. status &= ~BD_ENET_RX_STATS;
  773. /* Mark the buffer empty */
  774. status |= BD_ENET_RX_EMPTY;
  775. bdp->cbd_sc = status;
  776. if (fep->bufdesc_ex) {
  777. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  778. ebdp->cbd_esc = BD_ENET_RX_INT;
  779. ebdp->cbd_prot = 0;
  780. ebdp->cbd_bdu = 0;
  781. }
  782. /* Update BD pointer to next entry */
  783. if (status & BD_ENET_RX_WRAP)
  784. bdp = fep->rx_bd_base;
  785. else
  786. bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
  787. /* Doing this here will keep the FEC running while we process
  788. * incoming frames. On a heavily loaded network, we should be
  789. * able to keep up at the expense of system resources.
  790. */
  791. writel(0, fep->hwp + FEC_R_DES_ACTIVE);
  792. }
  793. fep->cur_rx = bdp;
  794. return pkt_received;
  795. }
  796. static irqreturn_t
  797. fec_enet_interrupt(int irq, void *dev_id)
  798. {
  799. struct net_device *ndev = dev_id;
  800. struct fec_enet_private *fep = netdev_priv(ndev);
  801. uint int_events;
  802. irqreturn_t ret = IRQ_NONE;
  803. do {
  804. int_events = readl(fep->hwp + FEC_IEVENT);
  805. writel(int_events, fep->hwp + FEC_IEVENT);
  806. if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) {
  807. ret = IRQ_HANDLED;
  808. /* Disable the RX interrupt */
  809. if (napi_schedule_prep(&fep->napi)) {
  810. writel(FEC_RX_DISABLED_IMASK,
  811. fep->hwp + FEC_IMASK);
  812. __napi_schedule(&fep->napi);
  813. }
  814. }
  815. if (int_events & FEC_ENET_MII) {
  816. ret = IRQ_HANDLED;
  817. complete(&fep->mdio_done);
  818. }
  819. } while (int_events);
  820. return ret;
  821. }
  822. static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
  823. {
  824. struct net_device *ndev = napi->dev;
  825. int pkts = fec_enet_rx(ndev, budget);
  826. struct fec_enet_private *fep = netdev_priv(ndev);
  827. fec_enet_tx(ndev);
  828. if (pkts < budget) {
  829. napi_complete(napi);
  830. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  831. }
  832. return pkts;
  833. }
  834. /* ------------------------------------------------------------------------- */
  835. static void fec_get_mac(struct net_device *ndev)
  836. {
  837. struct fec_enet_private *fep = netdev_priv(ndev);
  838. struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
  839. unsigned char *iap, tmpaddr[ETH_ALEN];
  840. /*
  841. * try to get mac address in following order:
  842. *
  843. * 1) module parameter via kernel command line in form
  844. * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
  845. */
  846. iap = macaddr;
  847. /*
  848. * 2) from device tree data
  849. */
  850. if (!is_valid_ether_addr(iap)) {
  851. struct device_node *np = fep->pdev->dev.of_node;
  852. if (np) {
  853. const char *mac = of_get_mac_address(np);
  854. if (mac)
  855. iap = (unsigned char *) mac;
  856. }
  857. }
  858. /*
  859. * 3) from flash or fuse (via platform data)
  860. */
  861. if (!is_valid_ether_addr(iap)) {
  862. #ifdef CONFIG_M5272
  863. if (FEC_FLASHMAC)
  864. iap = (unsigned char *)FEC_FLASHMAC;
  865. #else
  866. if (pdata)
  867. iap = (unsigned char *)&pdata->mac;
  868. #endif
  869. }
  870. /*
  871. * 4) FEC mac registers set by bootloader
  872. */
  873. if (!is_valid_ether_addr(iap)) {
  874. *((unsigned long *) &tmpaddr[0]) =
  875. be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
  876. *((unsigned short *) &tmpaddr[4]) =
  877. be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
  878. iap = &tmpaddr[0];
  879. }
  880. /*
  881. * 5) random mac address
  882. */
  883. if (!is_valid_ether_addr(iap)) {
  884. /* Report it and use a random ethernet address instead */
  885. netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
  886. eth_hw_addr_random(ndev);
  887. netdev_info(ndev, "Using random MAC address: %pM\n",
  888. ndev->dev_addr);
  889. return;
  890. }
  891. memcpy(ndev->dev_addr, iap, ETH_ALEN);
  892. /* Adjust MAC if using macaddr */
  893. if (iap == macaddr)
  894. ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
  895. }
  896. /* ------------------------------------------------------------------------- */
  897. /*
  898. * Phy section
  899. */
  900. static void fec_enet_adjust_link(struct net_device *ndev)
  901. {
  902. struct fec_enet_private *fep = netdev_priv(ndev);
  903. struct phy_device *phy_dev = fep->phy_dev;
  904. int status_change = 0;
  905. /* Prevent a state halted on mii error */
  906. if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
  907. phy_dev->state = PHY_RESUMING;
  908. return;
  909. }
  910. if (phy_dev->link) {
  911. if (!fep->link) {
  912. fep->link = phy_dev->link;
  913. status_change = 1;
  914. }
  915. if (fep->full_duplex != phy_dev->duplex)
  916. status_change = 1;
  917. if (phy_dev->speed != fep->speed) {
  918. fep->speed = phy_dev->speed;
  919. status_change = 1;
  920. }
  921. /* if any of the above changed restart the FEC */
  922. if (status_change)
  923. fec_restart(ndev, phy_dev->duplex);
  924. } else {
  925. if (fep->link) {
  926. fec_stop(ndev);
  927. fep->link = phy_dev->link;
  928. status_change = 1;
  929. }
  930. }
  931. if (status_change)
  932. phy_print_status(phy_dev);
  933. }
  934. static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  935. {
  936. struct fec_enet_private *fep = bus->priv;
  937. unsigned long time_left;
  938. fep->mii_timeout = 0;
  939. init_completion(&fep->mdio_done);
  940. /* start a read op */
  941. writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
  942. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  943. FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
  944. /* wait for end of transfer */
  945. time_left = wait_for_completion_timeout(&fep->mdio_done,
  946. usecs_to_jiffies(FEC_MII_TIMEOUT));
  947. if (time_left == 0) {
  948. fep->mii_timeout = 1;
  949. netdev_err(fep->netdev, "MDIO read timeout\n");
  950. return -ETIMEDOUT;
  951. }
  952. /* return value */
  953. return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
  954. }
  955. static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  956. u16 value)
  957. {
  958. struct fec_enet_private *fep = bus->priv;
  959. unsigned long time_left;
  960. fep->mii_timeout = 0;
  961. init_completion(&fep->mdio_done);
  962. /* start a write op */
  963. writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
  964. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  965. FEC_MMFR_TA | FEC_MMFR_DATA(value),
  966. fep->hwp + FEC_MII_DATA);
  967. /* wait for end of transfer */
  968. time_left = wait_for_completion_timeout(&fep->mdio_done,
  969. usecs_to_jiffies(FEC_MII_TIMEOUT));
  970. if (time_left == 0) {
  971. fep->mii_timeout = 1;
  972. netdev_err(fep->netdev, "MDIO write timeout\n");
  973. return -ETIMEDOUT;
  974. }
  975. return 0;
  976. }
  977. static int fec_enet_mdio_reset(struct mii_bus *bus)
  978. {
  979. return 0;
  980. }
  981. static int fec_enet_mii_probe(struct net_device *ndev)
  982. {
  983. struct fec_enet_private *fep = netdev_priv(ndev);
  984. const struct platform_device_id *id_entry =
  985. platform_get_device_id(fep->pdev);
  986. struct phy_device *phy_dev = NULL;
  987. char mdio_bus_id[MII_BUS_ID_SIZE];
  988. char phy_name[MII_BUS_ID_SIZE + 3];
  989. int phy_id;
  990. int dev_id = fep->dev_id;
  991. fep->phy_dev = NULL;
  992. /* check for attached phy */
  993. for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
  994. if ((fep->mii_bus->phy_mask & (1 << phy_id)))
  995. continue;
  996. if (fep->mii_bus->phy_map[phy_id] == NULL)
  997. continue;
  998. if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
  999. continue;
  1000. if (dev_id--)
  1001. continue;
  1002. strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
  1003. break;
  1004. }
  1005. if (phy_id >= PHY_MAX_ADDR) {
  1006. netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
  1007. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
  1008. phy_id = 0;
  1009. }
  1010. snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
  1011. phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
  1012. fep->phy_interface);
  1013. if (IS_ERR(phy_dev)) {
  1014. netdev_err(ndev, "could not attach to PHY\n");
  1015. return PTR_ERR(phy_dev);
  1016. }
  1017. /* mask with MAC supported features */
  1018. if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
  1019. phy_dev->supported &= PHY_GBIT_FEATURES;
  1020. phy_dev->supported |= SUPPORTED_Pause;
  1021. }
  1022. else
  1023. phy_dev->supported &= PHY_BASIC_FEATURES;
  1024. phy_dev->advertising = phy_dev->supported;
  1025. fep->phy_dev = phy_dev;
  1026. fep->link = 0;
  1027. fep->full_duplex = 0;
  1028. netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  1029. fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
  1030. fep->phy_dev->irq);
  1031. return 0;
  1032. }
  1033. static int fec_enet_mii_init(struct platform_device *pdev)
  1034. {
  1035. static struct mii_bus *fec0_mii_bus;
  1036. struct net_device *ndev = platform_get_drvdata(pdev);
  1037. struct fec_enet_private *fep = netdev_priv(ndev);
  1038. const struct platform_device_id *id_entry =
  1039. platform_get_device_id(fep->pdev);
  1040. int err = -ENXIO, i;
  1041. /*
  1042. * The dual fec interfaces are not equivalent with enet-mac.
  1043. * Here are the differences:
  1044. *
  1045. * - fec0 supports MII & RMII modes while fec1 only supports RMII
  1046. * - fec0 acts as the 1588 time master while fec1 is slave
  1047. * - external phys can only be configured by fec0
  1048. *
  1049. * That is to say fec1 can not work independently. It only works
  1050. * when fec0 is working. The reason behind this design is that the
  1051. * second interface is added primarily for Switch mode.
  1052. *
  1053. * Because of the last point above, both phys are attached on fec0
  1054. * mdio interface in board design, and need to be configured by
  1055. * fec0 mii_bus.
  1056. */
  1057. if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
  1058. /* fec1 uses fec0 mii_bus */
  1059. if (mii_cnt && fec0_mii_bus) {
  1060. fep->mii_bus = fec0_mii_bus;
  1061. mii_cnt++;
  1062. return 0;
  1063. }
  1064. return -ENOENT;
  1065. }
  1066. fep->mii_timeout = 0;
  1067. /*
  1068. * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
  1069. *
  1070. * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
  1071. * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
  1072. * Reference Manual has an error on this, and gets fixed on i.MX6Q
  1073. * document.
  1074. */
  1075. fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
  1076. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
  1077. fep->phy_speed--;
  1078. fep->phy_speed <<= 1;
  1079. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1080. fep->mii_bus = mdiobus_alloc();
  1081. if (fep->mii_bus == NULL) {
  1082. err = -ENOMEM;
  1083. goto err_out;
  1084. }
  1085. fep->mii_bus->name = "fec_enet_mii_bus";
  1086. fep->mii_bus->read = fec_enet_mdio_read;
  1087. fep->mii_bus->write = fec_enet_mdio_write;
  1088. fep->mii_bus->reset = fec_enet_mdio_reset;
  1089. snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  1090. pdev->name, fep->dev_id + 1);
  1091. fep->mii_bus->priv = fep;
  1092. fep->mii_bus->parent = &pdev->dev;
  1093. fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  1094. if (!fep->mii_bus->irq) {
  1095. err = -ENOMEM;
  1096. goto err_out_free_mdiobus;
  1097. }
  1098. for (i = 0; i < PHY_MAX_ADDR; i++)
  1099. fep->mii_bus->irq[i] = PHY_POLL;
  1100. if (mdiobus_register(fep->mii_bus))
  1101. goto err_out_free_mdio_irq;
  1102. mii_cnt++;
  1103. /* save fec0 mii_bus */
  1104. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
  1105. fec0_mii_bus = fep->mii_bus;
  1106. return 0;
  1107. err_out_free_mdio_irq:
  1108. kfree(fep->mii_bus->irq);
  1109. err_out_free_mdiobus:
  1110. mdiobus_free(fep->mii_bus);
  1111. err_out:
  1112. return err;
  1113. }
  1114. static void fec_enet_mii_remove(struct fec_enet_private *fep)
  1115. {
  1116. if (--mii_cnt == 0) {
  1117. mdiobus_unregister(fep->mii_bus);
  1118. kfree(fep->mii_bus->irq);
  1119. mdiobus_free(fep->mii_bus);
  1120. }
  1121. }
  1122. static int fec_enet_get_settings(struct net_device *ndev,
  1123. struct ethtool_cmd *cmd)
  1124. {
  1125. struct fec_enet_private *fep = netdev_priv(ndev);
  1126. struct phy_device *phydev = fep->phy_dev;
  1127. if (!phydev)
  1128. return -ENODEV;
  1129. return phy_ethtool_gset(phydev, cmd);
  1130. }
  1131. static int fec_enet_set_settings(struct net_device *ndev,
  1132. struct ethtool_cmd *cmd)
  1133. {
  1134. struct fec_enet_private *fep = netdev_priv(ndev);
  1135. struct phy_device *phydev = fep->phy_dev;
  1136. if (!phydev)
  1137. return -ENODEV;
  1138. return phy_ethtool_sset(phydev, cmd);
  1139. }
  1140. static void fec_enet_get_drvinfo(struct net_device *ndev,
  1141. struct ethtool_drvinfo *info)
  1142. {
  1143. struct fec_enet_private *fep = netdev_priv(ndev);
  1144. strlcpy(info->driver, fep->pdev->dev.driver->name,
  1145. sizeof(info->driver));
  1146. strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
  1147. strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
  1148. }
  1149. static int fec_enet_get_ts_info(struct net_device *ndev,
  1150. struct ethtool_ts_info *info)
  1151. {
  1152. struct fec_enet_private *fep = netdev_priv(ndev);
  1153. if (fep->bufdesc_ex) {
  1154. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  1155. SOF_TIMESTAMPING_RX_SOFTWARE |
  1156. SOF_TIMESTAMPING_SOFTWARE |
  1157. SOF_TIMESTAMPING_TX_HARDWARE |
  1158. SOF_TIMESTAMPING_RX_HARDWARE |
  1159. SOF_TIMESTAMPING_RAW_HARDWARE;
  1160. if (fep->ptp_clock)
  1161. info->phc_index = ptp_clock_index(fep->ptp_clock);
  1162. else
  1163. info->phc_index = -1;
  1164. info->tx_types = (1 << HWTSTAMP_TX_OFF) |
  1165. (1 << HWTSTAMP_TX_ON);
  1166. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  1167. (1 << HWTSTAMP_FILTER_ALL);
  1168. return 0;
  1169. } else {
  1170. return ethtool_op_get_ts_info(ndev, info);
  1171. }
  1172. }
  1173. static void fec_enet_get_pauseparam(struct net_device *ndev,
  1174. struct ethtool_pauseparam *pause)
  1175. {
  1176. struct fec_enet_private *fep = netdev_priv(ndev);
  1177. pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
  1178. pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
  1179. pause->rx_pause = pause->tx_pause;
  1180. }
  1181. static int fec_enet_set_pauseparam(struct net_device *ndev,
  1182. struct ethtool_pauseparam *pause)
  1183. {
  1184. struct fec_enet_private *fep = netdev_priv(ndev);
  1185. if (pause->tx_pause != pause->rx_pause) {
  1186. netdev_info(ndev,
  1187. "hardware only support enable/disable both tx and rx");
  1188. return -EINVAL;
  1189. }
  1190. fep->pause_flag = 0;
  1191. /* tx pause must be same as rx pause */
  1192. fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
  1193. fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
  1194. if (pause->rx_pause || pause->autoneg) {
  1195. fep->phy_dev->supported |= ADVERTISED_Pause;
  1196. fep->phy_dev->advertising |= ADVERTISED_Pause;
  1197. } else {
  1198. fep->phy_dev->supported &= ~ADVERTISED_Pause;
  1199. fep->phy_dev->advertising &= ~ADVERTISED_Pause;
  1200. }
  1201. if (pause->autoneg) {
  1202. if (netif_running(ndev))
  1203. fec_stop(ndev);
  1204. phy_start_aneg(fep->phy_dev);
  1205. }
  1206. if (netif_running(ndev))
  1207. fec_restart(ndev, 0);
  1208. return 0;
  1209. }
  1210. static int fec_enet_nway_reset(struct net_device *dev)
  1211. {
  1212. struct fec_enet_private *fep = netdev_priv(dev);
  1213. struct phy_device *phydev = fep->phy_dev;
  1214. if (!phydev)
  1215. return -ENODEV;
  1216. return genphy_restart_aneg(phydev);
  1217. }
  1218. static const struct ethtool_ops fec_enet_ethtool_ops = {
  1219. .get_pauseparam = fec_enet_get_pauseparam,
  1220. .set_pauseparam = fec_enet_set_pauseparam,
  1221. .get_settings = fec_enet_get_settings,
  1222. .set_settings = fec_enet_set_settings,
  1223. .get_drvinfo = fec_enet_get_drvinfo,
  1224. .get_link = ethtool_op_get_link,
  1225. .get_ts_info = fec_enet_get_ts_info,
  1226. .nway_reset = fec_enet_nway_reset,
  1227. };
  1228. static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  1229. {
  1230. struct fec_enet_private *fep = netdev_priv(ndev);
  1231. struct phy_device *phydev = fep->phy_dev;
  1232. if (!netif_running(ndev))
  1233. return -EINVAL;
  1234. if (!phydev)
  1235. return -ENODEV;
  1236. if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex)
  1237. return fec_ptp_ioctl(ndev, rq, cmd);
  1238. return phy_mii_ioctl(phydev, rq, cmd);
  1239. }
  1240. static void fec_enet_free_buffers(struct net_device *ndev)
  1241. {
  1242. struct fec_enet_private *fep = netdev_priv(ndev);
  1243. unsigned int i;
  1244. struct sk_buff *skb;
  1245. struct bufdesc *bdp;
  1246. bdp = fep->rx_bd_base;
  1247. for (i = 0; i < RX_RING_SIZE; i++) {
  1248. skb = fep->rx_skbuff[i];
  1249. if (bdp->cbd_bufaddr)
  1250. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  1251. FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
  1252. if (skb)
  1253. dev_kfree_skb(skb);
  1254. bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
  1255. }
  1256. bdp = fep->tx_bd_base;
  1257. for (i = 0; i < TX_RING_SIZE; i++)
  1258. kfree(fep->tx_bounce[i]);
  1259. }
  1260. static int fec_enet_alloc_buffers(struct net_device *ndev)
  1261. {
  1262. struct fec_enet_private *fep = netdev_priv(ndev);
  1263. unsigned int i;
  1264. struct sk_buff *skb;
  1265. struct bufdesc *bdp;
  1266. bdp = fep->rx_bd_base;
  1267. for (i = 0; i < RX_RING_SIZE; i++) {
  1268. skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
  1269. if (!skb) {
  1270. fec_enet_free_buffers(ndev);
  1271. return -ENOMEM;
  1272. }
  1273. fep->rx_skbuff[i] = skb;
  1274. bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
  1275. FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
  1276. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  1277. if (fep->bufdesc_ex) {
  1278. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1279. ebdp->cbd_esc = BD_ENET_RX_INT;
  1280. }
  1281. bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
  1282. }
  1283. /* Set the last buffer to wrap. */
  1284. bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
  1285. bdp->cbd_sc |= BD_SC_WRAP;
  1286. bdp = fep->tx_bd_base;
  1287. for (i = 0; i < TX_RING_SIZE; i++) {
  1288. fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
  1289. bdp->cbd_sc = 0;
  1290. bdp->cbd_bufaddr = 0;
  1291. if (fep->bufdesc_ex) {
  1292. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1293. ebdp->cbd_esc = BD_ENET_TX_INT;
  1294. }
  1295. bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
  1296. }
  1297. /* Set the last buffer to wrap. */
  1298. bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
  1299. bdp->cbd_sc |= BD_SC_WRAP;
  1300. return 0;
  1301. }
  1302. static int
  1303. fec_enet_open(struct net_device *ndev)
  1304. {
  1305. struct fec_enet_private *fep = netdev_priv(ndev);
  1306. int ret;
  1307. napi_enable(&fep->napi);
  1308. /* I should reset the ring buffers here, but I don't yet know
  1309. * a simple way to do that.
  1310. */
  1311. ret = fec_enet_alloc_buffers(ndev);
  1312. if (ret)
  1313. return ret;
  1314. /* Probe and connect to PHY when open the interface */
  1315. ret = fec_enet_mii_probe(ndev);
  1316. if (ret) {
  1317. fec_enet_free_buffers(ndev);
  1318. return ret;
  1319. }
  1320. phy_start(fep->phy_dev);
  1321. netif_start_queue(ndev);
  1322. fep->opened = 1;
  1323. return 0;
  1324. }
  1325. static int
  1326. fec_enet_close(struct net_device *ndev)
  1327. {
  1328. struct fec_enet_private *fep = netdev_priv(ndev);
  1329. /* Don't know what to do yet. */
  1330. napi_disable(&fep->napi);
  1331. fep->opened = 0;
  1332. netif_stop_queue(ndev);
  1333. fec_stop(ndev);
  1334. if (fep->phy_dev) {
  1335. phy_stop(fep->phy_dev);
  1336. phy_disconnect(fep->phy_dev);
  1337. }
  1338. fec_enet_free_buffers(ndev);
  1339. return 0;
  1340. }
  1341. /* Set or clear the multicast filter for this adaptor.
  1342. * Skeleton taken from sunlance driver.
  1343. * The CPM Ethernet implementation allows Multicast as well as individual
  1344. * MAC address filtering. Some of the drivers check to make sure it is
  1345. * a group multicast address, and discard those that are not. I guess I
  1346. * will do the same for now, but just remove the test if you want
  1347. * individual filtering as well (do the upper net layers want or support
  1348. * this kind of feature?).
  1349. */
  1350. #define HASH_BITS 6 /* #bits in hash */
  1351. #define CRC32_POLY 0xEDB88320
  1352. static void set_multicast_list(struct net_device *ndev)
  1353. {
  1354. struct fec_enet_private *fep = netdev_priv(ndev);
  1355. struct netdev_hw_addr *ha;
  1356. unsigned int i, bit, data, crc, tmp;
  1357. unsigned char hash;
  1358. if (ndev->flags & IFF_PROMISC) {
  1359. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1360. tmp |= 0x8;
  1361. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1362. return;
  1363. }
  1364. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1365. tmp &= ~0x8;
  1366. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1367. if (ndev->flags & IFF_ALLMULTI) {
  1368. /* Catch all multicast addresses, so set the
  1369. * filter to all 1's
  1370. */
  1371. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1372. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1373. return;
  1374. }
  1375. /* Clear filter and add the addresses in hash register
  1376. */
  1377. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1378. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1379. netdev_for_each_mc_addr(ha, ndev) {
  1380. /* calculate crc32 value of mac address */
  1381. crc = 0xffffffff;
  1382. for (i = 0; i < ndev->addr_len; i++) {
  1383. data = ha->addr[i];
  1384. for (bit = 0; bit < 8; bit++, data >>= 1) {
  1385. crc = (crc >> 1) ^
  1386. (((crc ^ data) & 1) ? CRC32_POLY : 0);
  1387. }
  1388. }
  1389. /* only upper 6 bits (HASH_BITS) are used
  1390. * which point to specific bit in he hash registers
  1391. */
  1392. hash = (crc >> (32 - HASH_BITS)) & 0x3f;
  1393. if (hash > 31) {
  1394. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1395. tmp |= 1 << (hash - 32);
  1396. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1397. } else {
  1398. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1399. tmp |= 1 << hash;
  1400. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1401. }
  1402. }
  1403. }
  1404. /* Set a MAC change in hardware. */
  1405. static int
  1406. fec_set_mac_address(struct net_device *ndev, void *p)
  1407. {
  1408. struct fec_enet_private *fep = netdev_priv(ndev);
  1409. struct sockaddr *addr = p;
  1410. if (!is_valid_ether_addr(addr->sa_data))
  1411. return -EADDRNOTAVAIL;
  1412. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  1413. writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
  1414. (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
  1415. fep->hwp + FEC_ADDR_LOW);
  1416. writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
  1417. fep->hwp + FEC_ADDR_HIGH);
  1418. return 0;
  1419. }
  1420. #ifdef CONFIG_NET_POLL_CONTROLLER
  1421. /**
  1422. * fec_poll_controller - FEC Poll controller function
  1423. * @dev: The FEC network adapter
  1424. *
  1425. * Polled functionality used by netconsole and others in non interrupt mode
  1426. *
  1427. */
  1428. static void fec_poll_controller(struct net_device *dev)
  1429. {
  1430. int i;
  1431. struct fec_enet_private *fep = netdev_priv(dev);
  1432. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1433. if (fep->irq[i] > 0) {
  1434. disable_irq(fep->irq[i]);
  1435. fec_enet_interrupt(fep->irq[i], dev);
  1436. enable_irq(fep->irq[i]);
  1437. }
  1438. }
  1439. }
  1440. #endif
  1441. static int fec_set_features(struct net_device *netdev,
  1442. netdev_features_t features)
  1443. {
  1444. struct fec_enet_private *fep = netdev_priv(netdev);
  1445. netdev_features_t changed = features ^ netdev->features;
  1446. netdev->features = features;
  1447. /* Receive checksum has been changed */
  1448. if (changed & NETIF_F_RXCSUM) {
  1449. if (features & NETIF_F_RXCSUM)
  1450. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  1451. else
  1452. fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
  1453. if (netif_running(netdev)) {
  1454. fec_stop(netdev);
  1455. fec_restart(netdev, fep->phy_dev->duplex);
  1456. netif_wake_queue(netdev);
  1457. } else {
  1458. fec_restart(netdev, fep->phy_dev->duplex);
  1459. }
  1460. }
  1461. return 0;
  1462. }
  1463. static const struct net_device_ops fec_netdev_ops = {
  1464. .ndo_open = fec_enet_open,
  1465. .ndo_stop = fec_enet_close,
  1466. .ndo_start_xmit = fec_enet_start_xmit,
  1467. .ndo_set_rx_mode = set_multicast_list,
  1468. .ndo_change_mtu = eth_change_mtu,
  1469. .ndo_validate_addr = eth_validate_addr,
  1470. .ndo_tx_timeout = fec_timeout,
  1471. .ndo_set_mac_address = fec_set_mac_address,
  1472. .ndo_do_ioctl = fec_enet_ioctl,
  1473. #ifdef CONFIG_NET_POLL_CONTROLLER
  1474. .ndo_poll_controller = fec_poll_controller,
  1475. #endif
  1476. .ndo_set_features = fec_set_features,
  1477. };
  1478. /*
  1479. * XXX: We need to clean up on failure exits here.
  1480. *
  1481. */
  1482. static int fec_enet_init(struct net_device *ndev)
  1483. {
  1484. struct fec_enet_private *fep = netdev_priv(ndev);
  1485. const struct platform_device_id *id_entry =
  1486. platform_get_device_id(fep->pdev);
  1487. struct bufdesc *cbd_base;
  1488. /* Allocate memory for buffer descriptors. */
  1489. cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
  1490. GFP_KERNEL);
  1491. if (!cbd_base)
  1492. return -ENOMEM;
  1493. memset(cbd_base, 0, PAGE_SIZE);
  1494. fep->netdev = ndev;
  1495. /* Get the Ethernet address */
  1496. fec_get_mac(ndev);
  1497. /* Set receive and transmit descriptor base. */
  1498. fep->rx_bd_base = cbd_base;
  1499. if (fep->bufdesc_ex)
  1500. fep->tx_bd_base = (struct bufdesc *)
  1501. (((struct bufdesc_ex *)cbd_base) + RX_RING_SIZE);
  1502. else
  1503. fep->tx_bd_base = cbd_base + RX_RING_SIZE;
  1504. /* The FEC Ethernet specific entries in the device structure */
  1505. ndev->watchdog_timeo = TX_TIMEOUT;
  1506. ndev->netdev_ops = &fec_netdev_ops;
  1507. ndev->ethtool_ops = &fec_enet_ethtool_ops;
  1508. writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
  1509. netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, FEC_NAPI_WEIGHT);
  1510. if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
  1511. /* enable hw accelerator */
  1512. ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
  1513. | NETIF_F_RXCSUM);
  1514. ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
  1515. | NETIF_F_RXCSUM);
  1516. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  1517. }
  1518. fec_restart(ndev, 0);
  1519. return 0;
  1520. }
  1521. #ifdef CONFIG_OF
  1522. static void fec_reset_phy(struct platform_device *pdev)
  1523. {
  1524. int err, phy_reset;
  1525. int msec = 1;
  1526. struct device_node *np = pdev->dev.of_node;
  1527. if (!np)
  1528. return;
  1529. of_property_read_u32(np, "phy-reset-duration", &msec);
  1530. /* A sane reset duration should not be longer than 1s */
  1531. if (msec > 1000)
  1532. msec = 1;
  1533. phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
  1534. if (!gpio_is_valid(phy_reset))
  1535. return;
  1536. err = devm_gpio_request_one(&pdev->dev, phy_reset,
  1537. GPIOF_OUT_INIT_LOW, "phy-reset");
  1538. if (err) {
  1539. dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
  1540. return;
  1541. }
  1542. msleep(msec);
  1543. gpio_set_value(phy_reset, 1);
  1544. }
  1545. #else /* CONFIG_OF */
  1546. static void fec_reset_phy(struct platform_device *pdev)
  1547. {
  1548. /*
  1549. * In case of platform probe, the reset has been done
  1550. * by machine code.
  1551. */
  1552. }
  1553. #endif /* CONFIG_OF */
  1554. static int
  1555. fec_probe(struct platform_device *pdev)
  1556. {
  1557. struct fec_enet_private *fep;
  1558. struct fec_platform_data *pdata;
  1559. struct net_device *ndev;
  1560. int i, irq, ret = 0;
  1561. struct resource *r;
  1562. const struct of_device_id *of_id;
  1563. static int dev_id;
  1564. of_id = of_match_device(fec_dt_ids, &pdev->dev);
  1565. if (of_id)
  1566. pdev->id_entry = of_id->data;
  1567. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1568. if (!r)
  1569. return -ENXIO;
  1570. /* Init network device */
  1571. ndev = alloc_etherdev(sizeof(struct fec_enet_private));
  1572. if (!ndev)
  1573. return -ENOMEM;
  1574. SET_NETDEV_DEV(ndev, &pdev->dev);
  1575. /* setup board info structure */
  1576. fep = netdev_priv(ndev);
  1577. /* default enable pause frame auto negotiation */
  1578. if (pdev->id_entry &&
  1579. (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
  1580. fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
  1581. fep->hwp = devm_ioremap_resource(&pdev->dev, r);
  1582. if (IS_ERR(fep->hwp)) {
  1583. ret = PTR_ERR(fep->hwp);
  1584. goto failed_ioremap;
  1585. }
  1586. fep->pdev = pdev;
  1587. fep->dev_id = dev_id++;
  1588. fep->bufdesc_ex = 0;
  1589. platform_set_drvdata(pdev, ndev);
  1590. ret = of_get_phy_mode(pdev->dev.of_node);
  1591. if (ret < 0) {
  1592. pdata = pdev->dev.platform_data;
  1593. if (pdata)
  1594. fep->phy_interface = pdata->phy;
  1595. else
  1596. fep->phy_interface = PHY_INTERFACE_MODE_MII;
  1597. } else {
  1598. fep->phy_interface = ret;
  1599. }
  1600. fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1601. if (IS_ERR(fep->clk_ipg)) {
  1602. ret = PTR_ERR(fep->clk_ipg);
  1603. goto failed_clk;
  1604. }
  1605. fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1606. if (IS_ERR(fep->clk_ahb)) {
  1607. ret = PTR_ERR(fep->clk_ahb);
  1608. goto failed_clk;
  1609. }
  1610. /* enet_out is optional, depends on board */
  1611. fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
  1612. if (IS_ERR(fep->clk_enet_out))
  1613. fep->clk_enet_out = NULL;
  1614. fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
  1615. fep->bufdesc_ex =
  1616. pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
  1617. if (IS_ERR(fep->clk_ptp)) {
  1618. fep->clk_ptp = NULL;
  1619. fep->bufdesc_ex = 0;
  1620. }
  1621. clk_prepare_enable(fep->clk_ahb);
  1622. clk_prepare_enable(fep->clk_ipg);
  1623. clk_prepare_enable(fep->clk_enet_out);
  1624. clk_prepare_enable(fep->clk_ptp);
  1625. fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
  1626. if (!IS_ERR(fep->reg_phy)) {
  1627. ret = regulator_enable(fep->reg_phy);
  1628. if (ret) {
  1629. dev_err(&pdev->dev,
  1630. "Failed to enable phy regulator: %d\n", ret);
  1631. goto failed_regulator;
  1632. }
  1633. } else {
  1634. fep->reg_phy = NULL;
  1635. }
  1636. fec_reset_phy(pdev);
  1637. if (fep->bufdesc_ex)
  1638. fec_ptp_init(pdev);
  1639. ret = fec_enet_init(ndev);
  1640. if (ret)
  1641. goto failed_init;
  1642. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1643. irq = platform_get_irq(pdev, i);
  1644. if (irq < 0) {
  1645. if (i)
  1646. break;
  1647. ret = irq;
  1648. goto failed_irq;
  1649. }
  1650. ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
  1651. if (ret) {
  1652. while (--i >= 0) {
  1653. irq = platform_get_irq(pdev, i);
  1654. free_irq(irq, ndev);
  1655. }
  1656. goto failed_irq;
  1657. }
  1658. }
  1659. ret = fec_enet_mii_init(pdev);
  1660. if (ret)
  1661. goto failed_mii_init;
  1662. /* Carrier starts down, phylib will bring it up */
  1663. netif_carrier_off(ndev);
  1664. ret = register_netdev(ndev);
  1665. if (ret)
  1666. goto failed_register;
  1667. if (fep->bufdesc_ex && fep->ptp_clock)
  1668. netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
  1669. INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
  1670. return 0;
  1671. failed_register:
  1672. fec_enet_mii_remove(fep);
  1673. failed_mii_init:
  1674. failed_irq:
  1675. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1676. irq = platform_get_irq(pdev, i);
  1677. if (irq > 0)
  1678. free_irq(irq, ndev);
  1679. }
  1680. failed_init:
  1681. if (fep->reg_phy)
  1682. regulator_disable(fep->reg_phy);
  1683. failed_regulator:
  1684. clk_disable_unprepare(fep->clk_ahb);
  1685. clk_disable_unprepare(fep->clk_ipg);
  1686. clk_disable_unprepare(fep->clk_enet_out);
  1687. clk_disable_unprepare(fep->clk_ptp);
  1688. failed_clk:
  1689. failed_ioremap:
  1690. free_netdev(ndev);
  1691. return ret;
  1692. }
  1693. static int
  1694. fec_drv_remove(struct platform_device *pdev)
  1695. {
  1696. struct net_device *ndev = platform_get_drvdata(pdev);
  1697. struct fec_enet_private *fep = netdev_priv(ndev);
  1698. int i;
  1699. cancel_delayed_work_sync(&(fep->delay_work.delay_work));
  1700. unregister_netdev(ndev);
  1701. fec_enet_mii_remove(fep);
  1702. del_timer_sync(&fep->time_keep);
  1703. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1704. int irq = platform_get_irq(pdev, i);
  1705. if (irq > 0)
  1706. free_irq(irq, ndev);
  1707. }
  1708. if (fep->reg_phy)
  1709. regulator_disable(fep->reg_phy);
  1710. clk_disable_unprepare(fep->clk_ptp);
  1711. if (fep->ptp_clock)
  1712. ptp_clock_unregister(fep->ptp_clock);
  1713. clk_disable_unprepare(fep->clk_enet_out);
  1714. clk_disable_unprepare(fep->clk_ahb);
  1715. clk_disable_unprepare(fep->clk_ipg);
  1716. free_netdev(ndev);
  1717. return 0;
  1718. }
  1719. #ifdef CONFIG_PM_SLEEP
  1720. static int
  1721. fec_suspend(struct device *dev)
  1722. {
  1723. struct net_device *ndev = dev_get_drvdata(dev);
  1724. struct fec_enet_private *fep = netdev_priv(ndev);
  1725. if (netif_running(ndev)) {
  1726. fec_stop(ndev);
  1727. netif_device_detach(ndev);
  1728. }
  1729. clk_disable_unprepare(fep->clk_enet_out);
  1730. clk_disable_unprepare(fep->clk_ahb);
  1731. clk_disable_unprepare(fep->clk_ipg);
  1732. if (fep->reg_phy)
  1733. regulator_disable(fep->reg_phy);
  1734. return 0;
  1735. }
  1736. static int
  1737. fec_resume(struct device *dev)
  1738. {
  1739. struct net_device *ndev = dev_get_drvdata(dev);
  1740. struct fec_enet_private *fep = netdev_priv(ndev);
  1741. int ret;
  1742. if (fep->reg_phy) {
  1743. ret = regulator_enable(fep->reg_phy);
  1744. if (ret)
  1745. return ret;
  1746. }
  1747. clk_prepare_enable(fep->clk_enet_out);
  1748. clk_prepare_enable(fep->clk_ahb);
  1749. clk_prepare_enable(fep->clk_ipg);
  1750. if (netif_running(ndev)) {
  1751. fec_restart(ndev, fep->full_duplex);
  1752. netif_device_attach(ndev);
  1753. }
  1754. return 0;
  1755. }
  1756. #endif /* CONFIG_PM_SLEEP */
  1757. static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
  1758. static struct platform_driver fec_driver = {
  1759. .driver = {
  1760. .name = DRIVER_NAME,
  1761. .owner = THIS_MODULE,
  1762. .pm = &fec_pm_ops,
  1763. .of_match_table = fec_dt_ids,
  1764. },
  1765. .id_table = fec_devtype,
  1766. .probe = fec_probe,
  1767. .remove = fec_drv_remove,
  1768. };
  1769. module_platform_driver(fec_driver);
  1770. MODULE_LICENSE("GPL");