fec_main.c 53 KB

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