fec_main.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  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. memcpy(ndev->dev_addr, iap, ETH_ALEN);
  881. /* Adjust MAC if using macaddr */
  882. if (iap == macaddr)
  883. ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
  884. }
  885. /* ------------------------------------------------------------------------- */
  886. /*
  887. * Phy section
  888. */
  889. static void fec_enet_adjust_link(struct net_device *ndev)
  890. {
  891. struct fec_enet_private *fep = netdev_priv(ndev);
  892. struct phy_device *phy_dev = fep->phy_dev;
  893. int status_change = 0;
  894. /* Prevent a state halted on mii error */
  895. if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
  896. phy_dev->state = PHY_RESUMING;
  897. return;
  898. }
  899. if (phy_dev->link) {
  900. if (!fep->link) {
  901. fep->link = phy_dev->link;
  902. status_change = 1;
  903. }
  904. if (fep->full_duplex != phy_dev->duplex)
  905. status_change = 1;
  906. if (phy_dev->speed != fep->speed) {
  907. fep->speed = phy_dev->speed;
  908. status_change = 1;
  909. }
  910. /* if any of the above changed restart the FEC */
  911. if (status_change)
  912. fec_restart(ndev, phy_dev->duplex);
  913. } else {
  914. if (fep->link) {
  915. fec_stop(ndev);
  916. fep->link = phy_dev->link;
  917. status_change = 1;
  918. }
  919. }
  920. if (status_change)
  921. phy_print_status(phy_dev);
  922. }
  923. static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  924. {
  925. struct fec_enet_private *fep = bus->priv;
  926. unsigned long time_left;
  927. fep->mii_timeout = 0;
  928. init_completion(&fep->mdio_done);
  929. /* start a read op */
  930. writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
  931. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  932. FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
  933. /* wait for end of transfer */
  934. time_left = wait_for_completion_timeout(&fep->mdio_done,
  935. usecs_to_jiffies(FEC_MII_TIMEOUT));
  936. if (time_left == 0) {
  937. fep->mii_timeout = 1;
  938. netdev_err(fep->netdev, "MDIO read timeout\n");
  939. return -ETIMEDOUT;
  940. }
  941. /* return value */
  942. return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
  943. }
  944. static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  945. u16 value)
  946. {
  947. struct fec_enet_private *fep = bus->priv;
  948. unsigned long time_left;
  949. fep->mii_timeout = 0;
  950. init_completion(&fep->mdio_done);
  951. /* start a write op */
  952. writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
  953. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  954. FEC_MMFR_TA | FEC_MMFR_DATA(value),
  955. fep->hwp + FEC_MII_DATA);
  956. /* wait for end of transfer */
  957. time_left = wait_for_completion_timeout(&fep->mdio_done,
  958. usecs_to_jiffies(FEC_MII_TIMEOUT));
  959. if (time_left == 0) {
  960. fep->mii_timeout = 1;
  961. netdev_err(fep->netdev, "MDIO write timeout\n");
  962. return -ETIMEDOUT;
  963. }
  964. return 0;
  965. }
  966. static int fec_enet_mdio_reset(struct mii_bus *bus)
  967. {
  968. return 0;
  969. }
  970. static int fec_enet_mii_probe(struct net_device *ndev)
  971. {
  972. struct fec_enet_private *fep = netdev_priv(ndev);
  973. const struct platform_device_id *id_entry =
  974. platform_get_device_id(fep->pdev);
  975. struct phy_device *phy_dev = NULL;
  976. char mdio_bus_id[MII_BUS_ID_SIZE];
  977. char phy_name[MII_BUS_ID_SIZE + 3];
  978. int phy_id;
  979. int dev_id = fep->dev_id;
  980. fep->phy_dev = NULL;
  981. /* check for attached phy */
  982. for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
  983. if ((fep->mii_bus->phy_mask & (1 << phy_id)))
  984. continue;
  985. if (fep->mii_bus->phy_map[phy_id] == NULL)
  986. continue;
  987. if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
  988. continue;
  989. if (dev_id--)
  990. continue;
  991. strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
  992. break;
  993. }
  994. if (phy_id >= PHY_MAX_ADDR) {
  995. netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
  996. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
  997. phy_id = 0;
  998. }
  999. snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
  1000. phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
  1001. fep->phy_interface);
  1002. if (IS_ERR(phy_dev)) {
  1003. netdev_err(ndev, "could not attach to PHY\n");
  1004. return PTR_ERR(phy_dev);
  1005. }
  1006. /* mask with MAC supported features */
  1007. if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
  1008. phy_dev->supported &= PHY_GBIT_FEATURES;
  1009. phy_dev->supported |= SUPPORTED_Pause;
  1010. }
  1011. else
  1012. phy_dev->supported &= PHY_BASIC_FEATURES;
  1013. phy_dev->advertising = phy_dev->supported;
  1014. fep->phy_dev = phy_dev;
  1015. fep->link = 0;
  1016. fep->full_duplex = 0;
  1017. netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  1018. fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
  1019. fep->phy_dev->irq);
  1020. return 0;
  1021. }
  1022. static int fec_enet_mii_init(struct platform_device *pdev)
  1023. {
  1024. static struct mii_bus *fec0_mii_bus;
  1025. struct net_device *ndev = platform_get_drvdata(pdev);
  1026. struct fec_enet_private *fep = netdev_priv(ndev);
  1027. const struct platform_device_id *id_entry =
  1028. platform_get_device_id(fep->pdev);
  1029. int err = -ENXIO, i;
  1030. /*
  1031. * The dual fec interfaces are not equivalent with enet-mac.
  1032. * Here are the differences:
  1033. *
  1034. * - fec0 supports MII & RMII modes while fec1 only supports RMII
  1035. * - fec0 acts as the 1588 time master while fec1 is slave
  1036. * - external phys can only be configured by fec0
  1037. *
  1038. * That is to say fec1 can not work independently. It only works
  1039. * when fec0 is working. The reason behind this design is that the
  1040. * second interface is added primarily for Switch mode.
  1041. *
  1042. * Because of the last point above, both phys are attached on fec0
  1043. * mdio interface in board design, and need to be configured by
  1044. * fec0 mii_bus.
  1045. */
  1046. if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
  1047. /* fec1 uses fec0 mii_bus */
  1048. if (mii_cnt && fec0_mii_bus) {
  1049. fep->mii_bus = fec0_mii_bus;
  1050. mii_cnt++;
  1051. return 0;
  1052. }
  1053. return -ENOENT;
  1054. }
  1055. fep->mii_timeout = 0;
  1056. /*
  1057. * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
  1058. *
  1059. * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
  1060. * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
  1061. * Reference Manual has an error on this, and gets fixed on i.MX6Q
  1062. * document.
  1063. */
  1064. fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
  1065. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
  1066. fep->phy_speed--;
  1067. fep->phy_speed <<= 1;
  1068. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1069. fep->mii_bus = mdiobus_alloc();
  1070. if (fep->mii_bus == NULL) {
  1071. err = -ENOMEM;
  1072. goto err_out;
  1073. }
  1074. fep->mii_bus->name = "fec_enet_mii_bus";
  1075. fep->mii_bus->read = fec_enet_mdio_read;
  1076. fep->mii_bus->write = fec_enet_mdio_write;
  1077. fep->mii_bus->reset = fec_enet_mdio_reset;
  1078. snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  1079. pdev->name, fep->dev_id + 1);
  1080. fep->mii_bus->priv = fep;
  1081. fep->mii_bus->parent = &pdev->dev;
  1082. fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  1083. if (!fep->mii_bus->irq) {
  1084. err = -ENOMEM;
  1085. goto err_out_free_mdiobus;
  1086. }
  1087. for (i = 0; i < PHY_MAX_ADDR; i++)
  1088. fep->mii_bus->irq[i] = PHY_POLL;
  1089. if (mdiobus_register(fep->mii_bus))
  1090. goto err_out_free_mdio_irq;
  1091. mii_cnt++;
  1092. /* save fec0 mii_bus */
  1093. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
  1094. fec0_mii_bus = fep->mii_bus;
  1095. return 0;
  1096. err_out_free_mdio_irq:
  1097. kfree(fep->mii_bus->irq);
  1098. err_out_free_mdiobus:
  1099. mdiobus_free(fep->mii_bus);
  1100. err_out:
  1101. return err;
  1102. }
  1103. static void fec_enet_mii_remove(struct fec_enet_private *fep)
  1104. {
  1105. if (--mii_cnt == 0) {
  1106. mdiobus_unregister(fep->mii_bus);
  1107. kfree(fep->mii_bus->irq);
  1108. mdiobus_free(fep->mii_bus);
  1109. }
  1110. }
  1111. static int fec_enet_get_settings(struct net_device *ndev,
  1112. struct ethtool_cmd *cmd)
  1113. {
  1114. struct fec_enet_private *fep = netdev_priv(ndev);
  1115. struct phy_device *phydev = fep->phy_dev;
  1116. if (!phydev)
  1117. return -ENODEV;
  1118. return phy_ethtool_gset(phydev, cmd);
  1119. }
  1120. static int fec_enet_set_settings(struct net_device *ndev,
  1121. struct ethtool_cmd *cmd)
  1122. {
  1123. struct fec_enet_private *fep = netdev_priv(ndev);
  1124. struct phy_device *phydev = fep->phy_dev;
  1125. if (!phydev)
  1126. return -ENODEV;
  1127. return phy_ethtool_sset(phydev, cmd);
  1128. }
  1129. static void fec_enet_get_drvinfo(struct net_device *ndev,
  1130. struct ethtool_drvinfo *info)
  1131. {
  1132. struct fec_enet_private *fep = netdev_priv(ndev);
  1133. strlcpy(info->driver, fep->pdev->dev.driver->name,
  1134. sizeof(info->driver));
  1135. strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
  1136. strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
  1137. }
  1138. static int fec_enet_get_ts_info(struct net_device *ndev,
  1139. struct ethtool_ts_info *info)
  1140. {
  1141. struct fec_enet_private *fep = netdev_priv(ndev);
  1142. if (fep->bufdesc_ex) {
  1143. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  1144. SOF_TIMESTAMPING_RX_SOFTWARE |
  1145. SOF_TIMESTAMPING_SOFTWARE |
  1146. SOF_TIMESTAMPING_TX_HARDWARE |
  1147. SOF_TIMESTAMPING_RX_HARDWARE |
  1148. SOF_TIMESTAMPING_RAW_HARDWARE;
  1149. if (fep->ptp_clock)
  1150. info->phc_index = ptp_clock_index(fep->ptp_clock);
  1151. else
  1152. info->phc_index = -1;
  1153. info->tx_types = (1 << HWTSTAMP_TX_OFF) |
  1154. (1 << HWTSTAMP_TX_ON);
  1155. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  1156. (1 << HWTSTAMP_FILTER_ALL);
  1157. return 0;
  1158. } else {
  1159. return ethtool_op_get_ts_info(ndev, info);
  1160. }
  1161. }
  1162. static void fec_enet_get_pauseparam(struct net_device *ndev,
  1163. struct ethtool_pauseparam *pause)
  1164. {
  1165. struct fec_enet_private *fep = netdev_priv(ndev);
  1166. pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
  1167. pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
  1168. pause->rx_pause = pause->tx_pause;
  1169. }
  1170. static int fec_enet_set_pauseparam(struct net_device *ndev,
  1171. struct ethtool_pauseparam *pause)
  1172. {
  1173. struct fec_enet_private *fep = netdev_priv(ndev);
  1174. if (pause->tx_pause != pause->rx_pause) {
  1175. netdev_info(ndev,
  1176. "hardware only support enable/disable both tx and rx");
  1177. return -EINVAL;
  1178. }
  1179. fep->pause_flag = 0;
  1180. /* tx pause must be same as rx pause */
  1181. fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
  1182. fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
  1183. if (pause->rx_pause || pause->autoneg) {
  1184. fep->phy_dev->supported |= ADVERTISED_Pause;
  1185. fep->phy_dev->advertising |= ADVERTISED_Pause;
  1186. } else {
  1187. fep->phy_dev->supported &= ~ADVERTISED_Pause;
  1188. fep->phy_dev->advertising &= ~ADVERTISED_Pause;
  1189. }
  1190. if (pause->autoneg) {
  1191. if (netif_running(ndev))
  1192. fec_stop(ndev);
  1193. phy_start_aneg(fep->phy_dev);
  1194. }
  1195. if (netif_running(ndev))
  1196. fec_restart(ndev, 0);
  1197. return 0;
  1198. }
  1199. static const struct ethtool_ops fec_enet_ethtool_ops = {
  1200. .get_pauseparam = fec_enet_get_pauseparam,
  1201. .set_pauseparam = fec_enet_set_pauseparam,
  1202. .get_settings = fec_enet_get_settings,
  1203. .set_settings = fec_enet_set_settings,
  1204. .get_drvinfo = fec_enet_get_drvinfo,
  1205. .get_link = ethtool_op_get_link,
  1206. .get_ts_info = fec_enet_get_ts_info,
  1207. };
  1208. static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  1209. {
  1210. struct fec_enet_private *fep = netdev_priv(ndev);
  1211. struct phy_device *phydev = fep->phy_dev;
  1212. if (!netif_running(ndev))
  1213. return -EINVAL;
  1214. if (!phydev)
  1215. return -ENODEV;
  1216. if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex)
  1217. return fec_ptp_ioctl(ndev, rq, cmd);
  1218. return phy_mii_ioctl(phydev, rq, cmd);
  1219. }
  1220. static void fec_enet_free_buffers(struct net_device *ndev)
  1221. {
  1222. struct fec_enet_private *fep = netdev_priv(ndev);
  1223. unsigned int i;
  1224. struct sk_buff *skb;
  1225. struct bufdesc *bdp;
  1226. bdp = fep->rx_bd_base;
  1227. for (i = 0; i < RX_RING_SIZE; i++) {
  1228. skb = fep->rx_skbuff[i];
  1229. if (bdp->cbd_bufaddr)
  1230. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  1231. FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
  1232. if (skb)
  1233. dev_kfree_skb(skb);
  1234. bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
  1235. }
  1236. bdp = fep->tx_bd_base;
  1237. for (i = 0; i < TX_RING_SIZE; i++)
  1238. kfree(fep->tx_bounce[i]);
  1239. }
  1240. static int fec_enet_alloc_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 = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
  1249. if (!skb) {
  1250. fec_enet_free_buffers(ndev);
  1251. return -ENOMEM;
  1252. }
  1253. fep->rx_skbuff[i] = skb;
  1254. bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
  1255. FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
  1256. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  1257. if (fep->bufdesc_ex) {
  1258. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1259. ebdp->cbd_esc = BD_ENET_RX_INT;
  1260. }
  1261. bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
  1262. }
  1263. /* Set the last buffer to wrap. */
  1264. bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
  1265. bdp->cbd_sc |= BD_SC_WRAP;
  1266. bdp = fep->tx_bd_base;
  1267. for (i = 0; i < TX_RING_SIZE; i++) {
  1268. fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
  1269. bdp->cbd_sc = 0;
  1270. bdp->cbd_bufaddr = 0;
  1271. if (fep->bufdesc_ex) {
  1272. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1273. ebdp->cbd_esc = BD_ENET_TX_INT;
  1274. }
  1275. bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
  1276. }
  1277. /* Set the last buffer to wrap. */
  1278. bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
  1279. bdp->cbd_sc |= BD_SC_WRAP;
  1280. return 0;
  1281. }
  1282. static int
  1283. fec_enet_open(struct net_device *ndev)
  1284. {
  1285. struct fec_enet_private *fep = netdev_priv(ndev);
  1286. int ret;
  1287. napi_enable(&fep->napi);
  1288. /* I should reset the ring buffers here, but I don't yet know
  1289. * a simple way to do that.
  1290. */
  1291. ret = fec_enet_alloc_buffers(ndev);
  1292. if (ret)
  1293. return ret;
  1294. /* Probe and connect to PHY when open the interface */
  1295. ret = fec_enet_mii_probe(ndev);
  1296. if (ret) {
  1297. fec_enet_free_buffers(ndev);
  1298. return ret;
  1299. }
  1300. phy_start(fep->phy_dev);
  1301. netif_start_queue(ndev);
  1302. fep->opened = 1;
  1303. return 0;
  1304. }
  1305. static int
  1306. fec_enet_close(struct net_device *ndev)
  1307. {
  1308. struct fec_enet_private *fep = netdev_priv(ndev);
  1309. /* Don't know what to do yet. */
  1310. napi_disable(&fep->napi);
  1311. fep->opened = 0;
  1312. netif_stop_queue(ndev);
  1313. fec_stop(ndev);
  1314. if (fep->phy_dev) {
  1315. phy_stop(fep->phy_dev);
  1316. phy_disconnect(fep->phy_dev);
  1317. }
  1318. fec_enet_free_buffers(ndev);
  1319. return 0;
  1320. }
  1321. /* Set or clear the multicast filter for this adaptor.
  1322. * Skeleton taken from sunlance driver.
  1323. * The CPM Ethernet implementation allows Multicast as well as individual
  1324. * MAC address filtering. Some of the drivers check to make sure it is
  1325. * a group multicast address, and discard those that are not. I guess I
  1326. * will do the same for now, but just remove the test if you want
  1327. * individual filtering as well (do the upper net layers want or support
  1328. * this kind of feature?).
  1329. */
  1330. #define HASH_BITS 6 /* #bits in hash */
  1331. #define CRC32_POLY 0xEDB88320
  1332. static void set_multicast_list(struct net_device *ndev)
  1333. {
  1334. struct fec_enet_private *fep = netdev_priv(ndev);
  1335. struct netdev_hw_addr *ha;
  1336. unsigned int i, bit, data, crc, tmp;
  1337. unsigned char hash;
  1338. if (ndev->flags & IFF_PROMISC) {
  1339. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1340. tmp |= 0x8;
  1341. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1342. return;
  1343. }
  1344. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1345. tmp &= ~0x8;
  1346. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1347. if (ndev->flags & IFF_ALLMULTI) {
  1348. /* Catch all multicast addresses, so set the
  1349. * filter to all 1's
  1350. */
  1351. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1352. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1353. return;
  1354. }
  1355. /* Clear filter and add the addresses in hash register
  1356. */
  1357. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1358. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1359. netdev_for_each_mc_addr(ha, ndev) {
  1360. /* calculate crc32 value of mac address */
  1361. crc = 0xffffffff;
  1362. for (i = 0; i < ndev->addr_len; i++) {
  1363. data = ha->addr[i];
  1364. for (bit = 0; bit < 8; bit++, data >>= 1) {
  1365. crc = (crc >> 1) ^
  1366. (((crc ^ data) & 1) ? CRC32_POLY : 0);
  1367. }
  1368. }
  1369. /* only upper 6 bits (HASH_BITS) are used
  1370. * which point to specific bit in he hash registers
  1371. */
  1372. hash = (crc >> (32 - HASH_BITS)) & 0x3f;
  1373. if (hash > 31) {
  1374. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1375. tmp |= 1 << (hash - 32);
  1376. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1377. } else {
  1378. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1379. tmp |= 1 << hash;
  1380. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1381. }
  1382. }
  1383. }
  1384. /* Set a MAC change in hardware. */
  1385. static int
  1386. fec_set_mac_address(struct net_device *ndev, void *p)
  1387. {
  1388. struct fec_enet_private *fep = netdev_priv(ndev);
  1389. struct sockaddr *addr = p;
  1390. if (!is_valid_ether_addr(addr->sa_data))
  1391. return -EADDRNOTAVAIL;
  1392. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  1393. writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
  1394. (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
  1395. fep->hwp + FEC_ADDR_LOW);
  1396. writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
  1397. fep->hwp + FEC_ADDR_HIGH);
  1398. return 0;
  1399. }
  1400. #ifdef CONFIG_NET_POLL_CONTROLLER
  1401. /**
  1402. * fec_poll_controller - FEC Poll controller function
  1403. * @dev: The FEC network adapter
  1404. *
  1405. * Polled functionality used by netconsole and others in non interrupt mode
  1406. *
  1407. */
  1408. static void fec_poll_controller(struct net_device *dev)
  1409. {
  1410. int i;
  1411. struct fec_enet_private *fep = netdev_priv(dev);
  1412. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1413. if (fep->irq[i] > 0) {
  1414. disable_irq(fep->irq[i]);
  1415. fec_enet_interrupt(fep->irq[i], dev);
  1416. enable_irq(fep->irq[i]);
  1417. }
  1418. }
  1419. }
  1420. #endif
  1421. static int fec_set_features(struct net_device *netdev,
  1422. netdev_features_t features)
  1423. {
  1424. struct fec_enet_private *fep = netdev_priv(netdev);
  1425. netdev_features_t changed = features ^ netdev->features;
  1426. netdev->features = features;
  1427. /* Receive checksum has been changed */
  1428. if (changed & NETIF_F_RXCSUM) {
  1429. if (features & NETIF_F_RXCSUM)
  1430. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  1431. else
  1432. fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
  1433. if (netif_running(netdev)) {
  1434. fec_stop(netdev);
  1435. fec_restart(netdev, fep->phy_dev->duplex);
  1436. netif_wake_queue(netdev);
  1437. } else {
  1438. fec_restart(netdev, fep->phy_dev->duplex);
  1439. }
  1440. }
  1441. return 0;
  1442. }
  1443. static const struct net_device_ops fec_netdev_ops = {
  1444. .ndo_open = fec_enet_open,
  1445. .ndo_stop = fec_enet_close,
  1446. .ndo_start_xmit = fec_enet_start_xmit,
  1447. .ndo_set_rx_mode = set_multicast_list,
  1448. .ndo_change_mtu = eth_change_mtu,
  1449. .ndo_validate_addr = eth_validate_addr,
  1450. .ndo_tx_timeout = fec_timeout,
  1451. .ndo_set_mac_address = fec_set_mac_address,
  1452. .ndo_do_ioctl = fec_enet_ioctl,
  1453. #ifdef CONFIG_NET_POLL_CONTROLLER
  1454. .ndo_poll_controller = fec_poll_controller,
  1455. #endif
  1456. .ndo_set_features = fec_set_features,
  1457. };
  1458. /*
  1459. * XXX: We need to clean up on failure exits here.
  1460. *
  1461. */
  1462. static int fec_enet_init(struct net_device *ndev)
  1463. {
  1464. struct fec_enet_private *fep = netdev_priv(ndev);
  1465. const struct platform_device_id *id_entry =
  1466. platform_get_device_id(fep->pdev);
  1467. struct bufdesc *cbd_base;
  1468. /* Allocate memory for buffer descriptors. */
  1469. cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
  1470. GFP_KERNEL);
  1471. if (!cbd_base)
  1472. return -ENOMEM;
  1473. memset(cbd_base, 0, PAGE_SIZE);
  1474. fep->netdev = ndev;
  1475. /* Get the Ethernet address */
  1476. fec_get_mac(ndev);
  1477. /* Set receive and transmit descriptor base. */
  1478. fep->rx_bd_base = cbd_base;
  1479. if (fep->bufdesc_ex)
  1480. fep->tx_bd_base = (struct bufdesc *)
  1481. (((struct bufdesc_ex *)cbd_base) + RX_RING_SIZE);
  1482. else
  1483. fep->tx_bd_base = cbd_base + RX_RING_SIZE;
  1484. /* The FEC Ethernet specific entries in the device structure */
  1485. ndev->watchdog_timeo = TX_TIMEOUT;
  1486. ndev->netdev_ops = &fec_netdev_ops;
  1487. ndev->ethtool_ops = &fec_enet_ethtool_ops;
  1488. writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
  1489. netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, FEC_NAPI_WEIGHT);
  1490. if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
  1491. /* enable hw accelerator */
  1492. ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
  1493. | NETIF_F_RXCSUM);
  1494. ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
  1495. | NETIF_F_RXCSUM);
  1496. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  1497. }
  1498. fec_restart(ndev, 0);
  1499. return 0;
  1500. }
  1501. #ifdef CONFIG_OF
  1502. static void fec_reset_phy(struct platform_device *pdev)
  1503. {
  1504. int err, phy_reset;
  1505. int msec = 1;
  1506. struct device_node *np = pdev->dev.of_node;
  1507. if (!np)
  1508. return;
  1509. of_property_read_u32(np, "phy-reset-duration", &msec);
  1510. /* A sane reset duration should not be longer than 1s */
  1511. if (msec > 1000)
  1512. msec = 1;
  1513. phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
  1514. if (!gpio_is_valid(phy_reset))
  1515. return;
  1516. err = devm_gpio_request_one(&pdev->dev, phy_reset,
  1517. GPIOF_OUT_INIT_LOW, "phy-reset");
  1518. if (err) {
  1519. dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
  1520. return;
  1521. }
  1522. msleep(msec);
  1523. gpio_set_value(phy_reset, 1);
  1524. }
  1525. #else /* CONFIG_OF */
  1526. static void fec_reset_phy(struct platform_device *pdev)
  1527. {
  1528. /*
  1529. * In case of platform probe, the reset has been done
  1530. * by machine code.
  1531. */
  1532. }
  1533. #endif /* CONFIG_OF */
  1534. static int
  1535. fec_probe(struct platform_device *pdev)
  1536. {
  1537. struct fec_enet_private *fep;
  1538. struct fec_platform_data *pdata;
  1539. struct net_device *ndev;
  1540. int i, irq, ret = 0;
  1541. struct resource *r;
  1542. const struct of_device_id *of_id;
  1543. static int dev_id;
  1544. of_id = of_match_device(fec_dt_ids, &pdev->dev);
  1545. if (of_id)
  1546. pdev->id_entry = of_id->data;
  1547. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1548. if (!r)
  1549. return -ENXIO;
  1550. /* Init network device */
  1551. ndev = alloc_etherdev(sizeof(struct fec_enet_private));
  1552. if (!ndev)
  1553. return -ENOMEM;
  1554. SET_NETDEV_DEV(ndev, &pdev->dev);
  1555. /* setup board info structure */
  1556. fep = netdev_priv(ndev);
  1557. /* default enable pause frame auto negotiation */
  1558. if (pdev->id_entry &&
  1559. (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
  1560. fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
  1561. fep->hwp = devm_request_and_ioremap(&pdev->dev, r);
  1562. fep->pdev = pdev;
  1563. fep->dev_id = dev_id++;
  1564. fep->bufdesc_ex = 0;
  1565. if (!fep->hwp) {
  1566. ret = -ENOMEM;
  1567. goto failed_ioremap;
  1568. }
  1569. platform_set_drvdata(pdev, ndev);
  1570. ret = of_get_phy_mode(pdev->dev.of_node);
  1571. if (ret < 0) {
  1572. pdata = pdev->dev.platform_data;
  1573. if (pdata)
  1574. fep->phy_interface = pdata->phy;
  1575. else
  1576. fep->phy_interface = PHY_INTERFACE_MODE_MII;
  1577. } else {
  1578. fep->phy_interface = ret;
  1579. }
  1580. fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1581. if (IS_ERR(fep->clk_ipg)) {
  1582. ret = PTR_ERR(fep->clk_ipg);
  1583. goto failed_clk;
  1584. }
  1585. fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1586. if (IS_ERR(fep->clk_ahb)) {
  1587. ret = PTR_ERR(fep->clk_ahb);
  1588. goto failed_clk;
  1589. }
  1590. /* enet_out is optional, depends on board */
  1591. fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
  1592. if (IS_ERR(fep->clk_enet_out))
  1593. fep->clk_enet_out = NULL;
  1594. fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
  1595. fep->bufdesc_ex =
  1596. pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
  1597. if (IS_ERR(fep->clk_ptp)) {
  1598. fep->clk_ptp = NULL;
  1599. fep->bufdesc_ex = 0;
  1600. }
  1601. clk_prepare_enable(fep->clk_ahb);
  1602. clk_prepare_enable(fep->clk_ipg);
  1603. clk_prepare_enable(fep->clk_enet_out);
  1604. clk_prepare_enable(fep->clk_ptp);
  1605. fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
  1606. if (!IS_ERR(fep->reg_phy)) {
  1607. ret = regulator_enable(fep->reg_phy);
  1608. if (ret) {
  1609. dev_err(&pdev->dev,
  1610. "Failed to enable phy regulator: %d\n", ret);
  1611. goto failed_regulator;
  1612. }
  1613. }
  1614. fec_reset_phy(pdev);
  1615. if (fep->bufdesc_ex)
  1616. fec_ptp_init(ndev, pdev);
  1617. ret = fec_enet_init(ndev);
  1618. if (ret)
  1619. goto failed_init;
  1620. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1621. irq = platform_get_irq(pdev, i);
  1622. if (irq < 0) {
  1623. if (i)
  1624. break;
  1625. ret = irq;
  1626. goto failed_irq;
  1627. }
  1628. ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
  1629. if (ret) {
  1630. while (--i >= 0) {
  1631. irq = platform_get_irq(pdev, i);
  1632. free_irq(irq, ndev);
  1633. }
  1634. goto failed_irq;
  1635. }
  1636. }
  1637. ret = fec_enet_mii_init(pdev);
  1638. if (ret)
  1639. goto failed_mii_init;
  1640. /* Carrier starts down, phylib will bring it up */
  1641. netif_carrier_off(ndev);
  1642. ret = register_netdev(ndev);
  1643. if (ret)
  1644. goto failed_register;
  1645. if (fep->bufdesc_ex && fep->ptp_clock)
  1646. netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
  1647. INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
  1648. return 0;
  1649. failed_register:
  1650. fec_enet_mii_remove(fep);
  1651. failed_mii_init:
  1652. failed_init:
  1653. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1654. irq = platform_get_irq(pdev, i);
  1655. if (irq > 0)
  1656. free_irq(irq, ndev);
  1657. }
  1658. failed_irq:
  1659. failed_regulator:
  1660. clk_disable_unprepare(fep->clk_ahb);
  1661. clk_disable_unprepare(fep->clk_ipg);
  1662. clk_disable_unprepare(fep->clk_enet_out);
  1663. clk_disable_unprepare(fep->clk_ptp);
  1664. failed_clk:
  1665. failed_ioremap:
  1666. free_netdev(ndev);
  1667. return ret;
  1668. }
  1669. static int
  1670. fec_drv_remove(struct platform_device *pdev)
  1671. {
  1672. struct net_device *ndev = platform_get_drvdata(pdev);
  1673. struct fec_enet_private *fep = netdev_priv(ndev);
  1674. int i;
  1675. cancel_delayed_work_sync(&(fep->delay_work.delay_work));
  1676. unregister_netdev(ndev);
  1677. fec_enet_mii_remove(fep);
  1678. del_timer_sync(&fep->time_keep);
  1679. clk_disable_unprepare(fep->clk_ptp);
  1680. if (fep->ptp_clock)
  1681. ptp_clock_unregister(fep->ptp_clock);
  1682. clk_disable_unprepare(fep->clk_enet_out);
  1683. clk_disable_unprepare(fep->clk_ahb);
  1684. clk_disable_unprepare(fep->clk_ipg);
  1685. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1686. int irq = platform_get_irq(pdev, i);
  1687. if (irq > 0)
  1688. free_irq(irq, ndev);
  1689. }
  1690. free_netdev(ndev);
  1691. platform_set_drvdata(pdev, NULL);
  1692. return 0;
  1693. }
  1694. #ifdef CONFIG_PM_SLEEP
  1695. static int
  1696. fec_suspend(struct device *dev)
  1697. {
  1698. struct net_device *ndev = dev_get_drvdata(dev);
  1699. struct fec_enet_private *fep = netdev_priv(ndev);
  1700. if (netif_running(ndev)) {
  1701. fec_stop(ndev);
  1702. netif_device_detach(ndev);
  1703. }
  1704. clk_disable_unprepare(fep->clk_enet_out);
  1705. clk_disable_unprepare(fep->clk_ahb);
  1706. clk_disable_unprepare(fep->clk_ipg);
  1707. return 0;
  1708. }
  1709. static int
  1710. fec_resume(struct device *dev)
  1711. {
  1712. struct net_device *ndev = dev_get_drvdata(dev);
  1713. struct fec_enet_private *fep = netdev_priv(ndev);
  1714. clk_prepare_enable(fep->clk_enet_out);
  1715. clk_prepare_enable(fep->clk_ahb);
  1716. clk_prepare_enable(fep->clk_ipg);
  1717. if (netif_running(ndev)) {
  1718. fec_restart(ndev, fep->full_duplex);
  1719. netif_device_attach(ndev);
  1720. }
  1721. return 0;
  1722. }
  1723. #endif /* CONFIG_PM_SLEEP */
  1724. static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
  1725. static struct platform_driver fec_driver = {
  1726. .driver = {
  1727. .name = DRIVER_NAME,
  1728. .owner = THIS_MODULE,
  1729. .pm = &fec_pm_ops,
  1730. .of_match_table = fec_dt_ids,
  1731. },
  1732. .id_table = fec_devtype,
  1733. .probe = fec_probe,
  1734. .remove = fec_drv_remove,
  1735. };
  1736. module_platform_driver(fec_driver);
  1737. MODULE_LICENSE("GPL");