fec.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  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/pci.h>
  32. #include <linux/init.h>
  33. #include <linux/delay.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/workqueue.h>
  39. #include <linux/bitops.h>
  40. #include <linux/io.h>
  41. #include <linux/irq.h>
  42. #include <linux/clk.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/phy.h>
  45. #include <linux/fec.h>
  46. #include <linux/of.h>
  47. #include <linux/of_device.h>
  48. #include <linux/of_gpio.h>
  49. #include <linux/of_net.h>
  50. #include <linux/pinctrl/consumer.h>
  51. #include <linux/regulator/consumer.h>
  52. #include <asm/cacheflush.h>
  53. #ifndef CONFIG_ARM
  54. #include <asm/coldfire.h>
  55. #include <asm/mcfsim.h>
  56. #endif
  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. /* Controller is ENET-MAC */
  65. #define FEC_QUIRK_ENET_MAC (1 << 0)
  66. /* Controller needs driver to swap frame */
  67. #define FEC_QUIRK_SWAP_FRAME (1 << 1)
  68. /* Controller uses gasket */
  69. #define FEC_QUIRK_USE_GASKET (1 << 2)
  70. /* Controller has GBIT support */
  71. #define FEC_QUIRK_HAS_GBIT (1 << 3)
  72. static struct platform_device_id fec_devtype[] = {
  73. {
  74. /* keep it for coldfire */
  75. .name = DRIVER_NAME,
  76. .driver_data = 0,
  77. }, {
  78. .name = "imx25-fec",
  79. .driver_data = FEC_QUIRK_USE_GASKET,
  80. }, {
  81. .name = "imx27-fec",
  82. .driver_data = 0,
  83. }, {
  84. .name = "imx28-fec",
  85. .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
  86. }, {
  87. .name = "imx6q-fec",
  88. .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT,
  89. }, {
  90. /* sentinel */
  91. }
  92. };
  93. MODULE_DEVICE_TABLE(platform, fec_devtype);
  94. enum imx_fec_type {
  95. IMX25_FEC = 1, /* runs on i.mx25/50/53 */
  96. IMX27_FEC, /* runs on i.mx27/35/51 */
  97. IMX28_FEC,
  98. IMX6Q_FEC,
  99. };
  100. static const struct of_device_id fec_dt_ids[] = {
  101. { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
  102. { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
  103. { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
  104. { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
  105. { /* sentinel */ }
  106. };
  107. MODULE_DEVICE_TABLE(of, fec_dt_ids);
  108. static unsigned char macaddr[ETH_ALEN];
  109. module_param_array(macaddr, byte, NULL, 0);
  110. MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
  111. #if defined(CONFIG_M5272)
  112. /*
  113. * Some hardware gets it MAC address out of local flash memory.
  114. * if this is non-zero then assume it is the address to get MAC from.
  115. */
  116. #if defined(CONFIG_NETtel)
  117. #define FEC_FLASHMAC 0xf0006006
  118. #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
  119. #define FEC_FLASHMAC 0xf0006000
  120. #elif defined(CONFIG_CANCam)
  121. #define FEC_FLASHMAC 0xf0020000
  122. #elif defined (CONFIG_M5272C3)
  123. #define FEC_FLASHMAC (0xffe04000 + 4)
  124. #elif defined(CONFIG_MOD5272)
  125. #define FEC_FLASHMAC 0xffc0406b
  126. #else
  127. #define FEC_FLASHMAC 0
  128. #endif
  129. #endif /* CONFIG_M5272 */
  130. #if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
  131. #error "FEC: descriptor ring size constants too large"
  132. #endif
  133. /* Interrupt events/masks. */
  134. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  135. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  136. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  137. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  138. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  139. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  140. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  141. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  142. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  143. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  144. #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
  145. /* The FEC stores dest/src/type, data, and checksum for receive packets.
  146. */
  147. #define PKT_MAXBUF_SIZE 1518
  148. #define PKT_MINBUF_SIZE 64
  149. #define PKT_MAXBLR_SIZE 1520
  150. /*
  151. * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
  152. * size bits. Other FEC hardware does not, so we need to take that into
  153. * account when setting it.
  154. */
  155. #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  156. defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
  157. #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
  158. #else
  159. #define OPT_FRAME_SIZE 0
  160. #endif
  161. /* FEC MII MMFR bits definition */
  162. #define FEC_MMFR_ST (1 << 30)
  163. #define FEC_MMFR_OP_READ (2 << 28)
  164. #define FEC_MMFR_OP_WRITE (1 << 28)
  165. #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
  166. #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
  167. #define FEC_MMFR_TA (2 << 16)
  168. #define FEC_MMFR_DATA(v) (v & 0xffff)
  169. #define FEC_MII_TIMEOUT 30000 /* us */
  170. /* Transmitter timeout */
  171. #define TX_TIMEOUT (2 * HZ)
  172. static int mii_cnt;
  173. static void *swap_buffer(void *bufaddr, int len)
  174. {
  175. int i;
  176. unsigned int *buf = bufaddr;
  177. for (i = 0; i < (len + 3) / 4; i++, buf++)
  178. *buf = cpu_to_be32(*buf);
  179. return bufaddr;
  180. }
  181. static netdev_tx_t
  182. fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  183. {
  184. struct fec_enet_private *fep = netdev_priv(ndev);
  185. const struct platform_device_id *id_entry =
  186. platform_get_device_id(fep->pdev);
  187. struct bufdesc *bdp;
  188. void *bufaddr;
  189. unsigned short status;
  190. unsigned long flags;
  191. if (!fep->link) {
  192. /* Link is down or autonegotiation is in progress. */
  193. return NETDEV_TX_BUSY;
  194. }
  195. spin_lock_irqsave(&fep->hw_lock, flags);
  196. /* Fill in a Tx ring entry */
  197. bdp = fep->cur_tx;
  198. status = bdp->cbd_sc;
  199. if (status & BD_ENET_TX_READY) {
  200. /* Ooops. All transmit buffers are full. Bail out.
  201. * This should not happen, since ndev->tbusy should be set.
  202. */
  203. printk("%s: tx queue full!.\n", ndev->name);
  204. spin_unlock_irqrestore(&fep->hw_lock, flags);
  205. return NETDEV_TX_BUSY;
  206. }
  207. /* Clear all of the status flags */
  208. status &= ~BD_ENET_TX_STATS;
  209. /* Set buffer length and buffer pointer */
  210. bufaddr = skb->data;
  211. bdp->cbd_datlen = skb->len;
  212. /*
  213. * On some FEC implementations data must be aligned on
  214. * 4-byte boundaries. Use bounce buffers to copy data
  215. * and get it aligned. Ugh.
  216. */
  217. if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
  218. unsigned int index;
  219. index = bdp - fep->tx_bd_base;
  220. memcpy(fep->tx_bounce[index], skb->data, skb->len);
  221. bufaddr = fep->tx_bounce[index];
  222. }
  223. /*
  224. * Some design made an incorrect assumption on endian mode of
  225. * the system that it's running on. As the result, driver has to
  226. * swap every frame going to and coming from the controller.
  227. */
  228. if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
  229. swap_buffer(bufaddr, skb->len);
  230. /* Save skb pointer */
  231. fep->tx_skbuff[fep->skb_cur] = skb;
  232. ndev->stats.tx_bytes += skb->len;
  233. fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
  234. /* Push the data cache so the CPM does not get stale memory
  235. * data.
  236. */
  237. bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
  238. FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
  239. /* Send it on its way. Tell FEC it's ready, interrupt when done,
  240. * it's the last BD of the frame, and to put the CRC on the end.
  241. */
  242. status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
  243. | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  244. bdp->cbd_sc = status;
  245. /* Trigger transmission start */
  246. writel(0, fep->hwp + FEC_X_DES_ACTIVE);
  247. /* If this was the last BD in the ring, start at the beginning again. */
  248. if (status & BD_ENET_TX_WRAP)
  249. bdp = fep->tx_bd_base;
  250. else
  251. bdp++;
  252. if (bdp == fep->dirty_tx) {
  253. fep->tx_full = 1;
  254. netif_stop_queue(ndev);
  255. }
  256. fep->cur_tx = bdp;
  257. skb_tx_timestamp(skb);
  258. spin_unlock_irqrestore(&fep->hw_lock, flags);
  259. return NETDEV_TX_OK;
  260. }
  261. /* This function is called to start or restart the FEC during a link
  262. * change. This only happens when switching between half and full
  263. * duplex.
  264. */
  265. static void
  266. fec_restart(struct net_device *ndev, int duplex)
  267. {
  268. struct fec_enet_private *fep = netdev_priv(ndev);
  269. const struct platform_device_id *id_entry =
  270. platform_get_device_id(fep->pdev);
  271. int i;
  272. u32 temp_mac[2];
  273. u32 rcntl = OPT_FRAME_SIZE | 0x04;
  274. u32 ecntl = 0x2; /* ETHEREN */
  275. /* Whack a reset. We should wait for this. */
  276. writel(1, fep->hwp + FEC_ECNTRL);
  277. udelay(10);
  278. /*
  279. * enet-mac reset will reset mac address registers too,
  280. * so need to reconfigure it.
  281. */
  282. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
  283. memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
  284. writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
  285. writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
  286. }
  287. /* Clear any outstanding interrupt. */
  288. writel(0xffc00000, fep->hwp + FEC_IEVENT);
  289. /* Reset all multicast. */
  290. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  291. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  292. #ifndef CONFIG_M5272
  293. writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
  294. writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
  295. #endif
  296. /* Set maximum receive buffer size. */
  297. writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
  298. /* Set receive and transmit descriptor base. */
  299. writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
  300. writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
  301. fep->hwp + FEC_X_DES_START);
  302. fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
  303. fep->cur_rx = fep->rx_bd_base;
  304. /* Reset SKB transmit buffers. */
  305. fep->skb_cur = fep->skb_dirty = 0;
  306. for (i = 0; i <= TX_RING_MOD_MASK; i++) {
  307. if (fep->tx_skbuff[i]) {
  308. dev_kfree_skb_any(fep->tx_skbuff[i]);
  309. fep->tx_skbuff[i] = NULL;
  310. }
  311. }
  312. /* Enable MII mode */
  313. if (duplex) {
  314. /* FD enable */
  315. writel(0x04, fep->hwp + FEC_X_CNTRL);
  316. } else {
  317. /* No Rcv on Xmit */
  318. rcntl |= 0x02;
  319. writel(0x0, fep->hwp + FEC_X_CNTRL);
  320. }
  321. fep->full_duplex = duplex;
  322. /* Set MII speed */
  323. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  324. /*
  325. * The phy interface and speed need to get configured
  326. * differently on enet-mac.
  327. */
  328. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
  329. /* Enable flow control and length check */
  330. rcntl |= 0x40000000 | 0x00000020;
  331. /* RGMII, RMII or MII */
  332. if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
  333. rcntl |= (1 << 6);
  334. else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
  335. rcntl |= (1 << 8);
  336. else
  337. rcntl &= ~(1 << 8);
  338. /* 1G, 100M or 10M */
  339. if (fep->phy_dev) {
  340. if (fep->phy_dev->speed == SPEED_1000)
  341. ecntl |= (1 << 5);
  342. else if (fep->phy_dev->speed == SPEED_100)
  343. rcntl &= ~(1 << 9);
  344. else
  345. rcntl |= (1 << 9);
  346. }
  347. } else {
  348. #ifdef FEC_MIIGSK_ENR
  349. if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
  350. u32 cfgr;
  351. /* disable the gasket and wait */
  352. writel(0, fep->hwp + FEC_MIIGSK_ENR);
  353. while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
  354. udelay(1);
  355. /*
  356. * configure the gasket:
  357. * RMII, 50 MHz, no loopback, no echo
  358. * MII, 25 MHz, no loopback, no echo
  359. */
  360. cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
  361. ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
  362. if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
  363. cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
  364. writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
  365. /* re-enable the gasket */
  366. writel(2, fep->hwp + FEC_MIIGSK_ENR);
  367. }
  368. #endif
  369. }
  370. writel(rcntl, fep->hwp + FEC_R_CNTRL);
  371. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
  372. /* enable ENET endian swap */
  373. ecntl |= (1 << 8);
  374. /* enable ENET store and forward mode */
  375. writel(1 << 8, fep->hwp + FEC_X_WMRK);
  376. }
  377. /* And last, enable the transmit and receive processing */
  378. writel(ecntl, fep->hwp + FEC_ECNTRL);
  379. writel(0, fep->hwp + FEC_R_DES_ACTIVE);
  380. /* Enable interrupts we wish to service */
  381. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  382. }
  383. static void
  384. fec_stop(struct net_device *ndev)
  385. {
  386. struct fec_enet_private *fep = netdev_priv(ndev);
  387. const struct platform_device_id *id_entry =
  388. platform_get_device_id(fep->pdev);
  389. u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
  390. /* We cannot expect a graceful transmit stop without link !!! */
  391. if (fep->link) {
  392. writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
  393. udelay(10);
  394. if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
  395. printk("fec_stop : Graceful transmit stop did not complete !\n");
  396. }
  397. /* Whack a reset. We should wait for this. */
  398. writel(1, fep->hwp + FEC_ECNTRL);
  399. udelay(10);
  400. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  401. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  402. /* We have to keep ENET enabled to have MII interrupt stay working */
  403. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
  404. writel(2, fep->hwp + FEC_ECNTRL);
  405. writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
  406. }
  407. }
  408. static void
  409. fec_timeout(struct net_device *ndev)
  410. {
  411. struct fec_enet_private *fep = netdev_priv(ndev);
  412. ndev->stats.tx_errors++;
  413. fec_restart(ndev, fep->full_duplex);
  414. netif_wake_queue(ndev);
  415. }
  416. static void
  417. fec_enet_tx(struct net_device *ndev)
  418. {
  419. struct fec_enet_private *fep;
  420. struct bufdesc *bdp;
  421. unsigned short status;
  422. struct sk_buff *skb;
  423. fep = netdev_priv(ndev);
  424. spin_lock(&fep->hw_lock);
  425. bdp = fep->dirty_tx;
  426. while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
  427. if (bdp == fep->cur_tx && fep->tx_full == 0)
  428. break;
  429. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  430. FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
  431. bdp->cbd_bufaddr = 0;
  432. skb = fep->tx_skbuff[fep->skb_dirty];
  433. /* Check for errors. */
  434. if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  435. BD_ENET_TX_RL | BD_ENET_TX_UN |
  436. BD_ENET_TX_CSL)) {
  437. ndev->stats.tx_errors++;
  438. if (status & BD_ENET_TX_HB) /* No heartbeat */
  439. ndev->stats.tx_heartbeat_errors++;
  440. if (status & BD_ENET_TX_LC) /* Late collision */
  441. ndev->stats.tx_window_errors++;
  442. if (status & BD_ENET_TX_RL) /* Retrans limit */
  443. ndev->stats.tx_aborted_errors++;
  444. if (status & BD_ENET_TX_UN) /* Underrun */
  445. ndev->stats.tx_fifo_errors++;
  446. if (status & BD_ENET_TX_CSL) /* Carrier lost */
  447. ndev->stats.tx_carrier_errors++;
  448. } else {
  449. ndev->stats.tx_packets++;
  450. }
  451. if (status & BD_ENET_TX_READY)
  452. printk("HEY! Enet xmit interrupt and TX_READY.\n");
  453. /* Deferred means some collisions occurred during transmit,
  454. * but we eventually sent the packet OK.
  455. */
  456. if (status & BD_ENET_TX_DEF)
  457. ndev->stats.collisions++;
  458. /* Free the sk buffer associated with this last transmit */
  459. dev_kfree_skb_any(skb);
  460. fep->tx_skbuff[fep->skb_dirty] = NULL;
  461. fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
  462. /* Update pointer to next buffer descriptor to be transmitted */
  463. if (status & BD_ENET_TX_WRAP)
  464. bdp = fep->tx_bd_base;
  465. else
  466. bdp++;
  467. /* Since we have freed up a buffer, the ring is no longer full
  468. */
  469. if (fep->tx_full) {
  470. fep->tx_full = 0;
  471. if (netif_queue_stopped(ndev))
  472. netif_wake_queue(ndev);
  473. }
  474. }
  475. fep->dirty_tx = bdp;
  476. spin_unlock(&fep->hw_lock);
  477. }
  478. /* During a receive, the cur_rx points to the current incoming buffer.
  479. * When we update through the ring, if the next incoming buffer has
  480. * not been given to the system, we just set the empty indicator,
  481. * effectively tossing the packet.
  482. */
  483. static void
  484. fec_enet_rx(struct net_device *ndev)
  485. {
  486. struct fec_enet_private *fep = netdev_priv(ndev);
  487. const struct platform_device_id *id_entry =
  488. platform_get_device_id(fep->pdev);
  489. struct bufdesc *bdp;
  490. unsigned short status;
  491. struct sk_buff *skb;
  492. ushort pkt_len;
  493. __u8 *data;
  494. #ifdef CONFIG_M532x
  495. flush_cache_all();
  496. #endif
  497. spin_lock(&fep->hw_lock);
  498. /* First, grab all of the stats for the incoming packet.
  499. * These get messed up if we get called due to a busy condition.
  500. */
  501. bdp = fep->cur_rx;
  502. while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
  503. /* Since we have allocated space to hold a complete frame,
  504. * the last indicator should be set.
  505. */
  506. if ((status & BD_ENET_RX_LAST) == 0)
  507. printk("FEC ENET: rcv is not +last\n");
  508. if (!fep->opened)
  509. goto rx_processing_done;
  510. /* Check for errors. */
  511. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
  512. BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  513. ndev->stats.rx_errors++;
  514. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
  515. /* Frame too long or too short. */
  516. ndev->stats.rx_length_errors++;
  517. }
  518. if (status & BD_ENET_RX_NO) /* Frame alignment */
  519. ndev->stats.rx_frame_errors++;
  520. if (status & BD_ENET_RX_CR) /* CRC Error */
  521. ndev->stats.rx_crc_errors++;
  522. if (status & BD_ENET_RX_OV) /* FIFO overrun */
  523. ndev->stats.rx_fifo_errors++;
  524. }
  525. /* Report late collisions as a frame error.
  526. * On this error, the BD is closed, but we don't know what we
  527. * have in the buffer. So, just drop this frame on the floor.
  528. */
  529. if (status & BD_ENET_RX_CL) {
  530. ndev->stats.rx_errors++;
  531. ndev->stats.rx_frame_errors++;
  532. goto rx_processing_done;
  533. }
  534. /* Process the incoming frame. */
  535. ndev->stats.rx_packets++;
  536. pkt_len = bdp->cbd_datlen;
  537. ndev->stats.rx_bytes += pkt_len;
  538. data = (__u8*)__va(bdp->cbd_bufaddr);
  539. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  540. FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
  541. if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
  542. swap_buffer(data, pkt_len);
  543. /* This does 16 byte alignment, exactly what we need.
  544. * The packet length includes FCS, but we don't want to
  545. * include that when passing upstream as it messes up
  546. * bridging applications.
  547. */
  548. skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
  549. if (unlikely(!skb)) {
  550. printk("%s: Memory squeeze, dropping packet.\n",
  551. ndev->name);
  552. ndev->stats.rx_dropped++;
  553. } else {
  554. skb_reserve(skb, NET_IP_ALIGN);
  555. skb_put(skb, pkt_len - 4); /* Make room */
  556. skb_copy_to_linear_data(skb, data, pkt_len - 4);
  557. skb->protocol = eth_type_trans(skb, ndev);
  558. if (!skb_defer_rx_timestamp(skb))
  559. netif_rx(skb);
  560. }
  561. bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
  562. FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
  563. rx_processing_done:
  564. /* Clear the status flags for this buffer */
  565. status &= ~BD_ENET_RX_STATS;
  566. /* Mark the buffer empty */
  567. status |= BD_ENET_RX_EMPTY;
  568. bdp->cbd_sc = status;
  569. /* Update BD pointer to next entry */
  570. if (status & BD_ENET_RX_WRAP)
  571. bdp = fep->rx_bd_base;
  572. else
  573. bdp++;
  574. /* Doing this here will keep the FEC running while we process
  575. * incoming frames. On a heavily loaded network, we should be
  576. * able to keep up at the expense of system resources.
  577. */
  578. writel(0, fep->hwp + FEC_R_DES_ACTIVE);
  579. }
  580. fep->cur_rx = bdp;
  581. spin_unlock(&fep->hw_lock);
  582. }
  583. static irqreturn_t
  584. fec_enet_interrupt(int irq, void *dev_id)
  585. {
  586. struct net_device *ndev = dev_id;
  587. struct fec_enet_private *fep = netdev_priv(ndev);
  588. uint int_events;
  589. irqreturn_t ret = IRQ_NONE;
  590. do {
  591. int_events = readl(fep->hwp + FEC_IEVENT);
  592. writel(int_events, fep->hwp + FEC_IEVENT);
  593. if (int_events & FEC_ENET_RXF) {
  594. ret = IRQ_HANDLED;
  595. fec_enet_rx(ndev);
  596. }
  597. /* Transmit OK, or non-fatal error. Update the buffer
  598. * descriptors. FEC handles all errors, we just discover
  599. * them as part of the transmit process.
  600. */
  601. if (int_events & FEC_ENET_TXF) {
  602. ret = IRQ_HANDLED;
  603. fec_enet_tx(ndev);
  604. }
  605. if (int_events & FEC_ENET_MII) {
  606. ret = IRQ_HANDLED;
  607. complete(&fep->mdio_done);
  608. }
  609. } while (int_events);
  610. return ret;
  611. }
  612. /* ------------------------------------------------------------------------- */
  613. static void __inline__ fec_get_mac(struct net_device *ndev)
  614. {
  615. struct fec_enet_private *fep = netdev_priv(ndev);
  616. struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
  617. unsigned char *iap, tmpaddr[ETH_ALEN];
  618. /*
  619. * try to get mac address in following order:
  620. *
  621. * 1) module parameter via kernel command line in form
  622. * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
  623. */
  624. iap = macaddr;
  625. #ifdef CONFIG_OF
  626. /*
  627. * 2) from device tree data
  628. */
  629. if (!is_valid_ether_addr(iap)) {
  630. struct device_node *np = fep->pdev->dev.of_node;
  631. if (np) {
  632. const char *mac = of_get_mac_address(np);
  633. if (mac)
  634. iap = (unsigned char *) mac;
  635. }
  636. }
  637. #endif
  638. /*
  639. * 3) from flash or fuse (via platform data)
  640. */
  641. if (!is_valid_ether_addr(iap)) {
  642. #ifdef CONFIG_M5272
  643. if (FEC_FLASHMAC)
  644. iap = (unsigned char *)FEC_FLASHMAC;
  645. #else
  646. if (pdata)
  647. iap = (unsigned char *)&pdata->mac;
  648. #endif
  649. }
  650. /*
  651. * 4) FEC mac registers set by bootloader
  652. */
  653. if (!is_valid_ether_addr(iap)) {
  654. *((unsigned long *) &tmpaddr[0]) =
  655. be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
  656. *((unsigned short *) &tmpaddr[4]) =
  657. be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
  658. iap = &tmpaddr[0];
  659. }
  660. memcpy(ndev->dev_addr, iap, ETH_ALEN);
  661. /* Adjust MAC if using macaddr */
  662. if (iap == macaddr)
  663. ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
  664. }
  665. /* ------------------------------------------------------------------------- */
  666. /*
  667. * Phy section
  668. */
  669. static void fec_enet_adjust_link(struct net_device *ndev)
  670. {
  671. struct fec_enet_private *fep = netdev_priv(ndev);
  672. struct phy_device *phy_dev = fep->phy_dev;
  673. unsigned long flags;
  674. int status_change = 0;
  675. spin_lock_irqsave(&fep->hw_lock, flags);
  676. /* Prevent a state halted on mii error */
  677. if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
  678. phy_dev->state = PHY_RESUMING;
  679. goto spin_unlock;
  680. }
  681. /* Duplex link change */
  682. if (phy_dev->link) {
  683. if (fep->full_duplex != phy_dev->duplex) {
  684. fec_restart(ndev, phy_dev->duplex);
  685. /* prevent unnecessary second fec_restart() below */
  686. fep->link = phy_dev->link;
  687. status_change = 1;
  688. }
  689. }
  690. /* Link on or off change */
  691. if (phy_dev->link != fep->link) {
  692. fep->link = phy_dev->link;
  693. if (phy_dev->link)
  694. fec_restart(ndev, phy_dev->duplex);
  695. else
  696. fec_stop(ndev);
  697. status_change = 1;
  698. }
  699. spin_unlock:
  700. spin_unlock_irqrestore(&fep->hw_lock, flags);
  701. if (status_change)
  702. phy_print_status(phy_dev);
  703. }
  704. static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  705. {
  706. struct fec_enet_private *fep = bus->priv;
  707. unsigned long time_left;
  708. fep->mii_timeout = 0;
  709. init_completion(&fep->mdio_done);
  710. /* start a read op */
  711. writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
  712. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  713. FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
  714. /* wait for end of transfer */
  715. time_left = wait_for_completion_timeout(&fep->mdio_done,
  716. usecs_to_jiffies(FEC_MII_TIMEOUT));
  717. if (time_left == 0) {
  718. fep->mii_timeout = 1;
  719. printk(KERN_ERR "FEC: MDIO read timeout\n");
  720. return -ETIMEDOUT;
  721. }
  722. /* return value */
  723. return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
  724. }
  725. static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  726. u16 value)
  727. {
  728. struct fec_enet_private *fep = bus->priv;
  729. unsigned long time_left;
  730. fep->mii_timeout = 0;
  731. init_completion(&fep->mdio_done);
  732. /* start a write op */
  733. writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
  734. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  735. FEC_MMFR_TA | FEC_MMFR_DATA(value),
  736. fep->hwp + FEC_MII_DATA);
  737. /* wait for end of transfer */
  738. time_left = wait_for_completion_timeout(&fep->mdio_done,
  739. usecs_to_jiffies(FEC_MII_TIMEOUT));
  740. if (time_left == 0) {
  741. fep->mii_timeout = 1;
  742. printk(KERN_ERR "FEC: MDIO write timeout\n");
  743. return -ETIMEDOUT;
  744. }
  745. return 0;
  746. }
  747. static int fec_enet_mdio_reset(struct mii_bus *bus)
  748. {
  749. return 0;
  750. }
  751. static int fec_enet_mii_probe(struct net_device *ndev)
  752. {
  753. struct fec_enet_private *fep = netdev_priv(ndev);
  754. const struct platform_device_id *id_entry =
  755. platform_get_device_id(fep->pdev);
  756. struct phy_device *phy_dev = NULL;
  757. char mdio_bus_id[MII_BUS_ID_SIZE];
  758. char phy_name[MII_BUS_ID_SIZE + 3];
  759. int phy_id;
  760. int dev_id = fep->dev_id;
  761. fep->phy_dev = NULL;
  762. /* check for attached phy */
  763. for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
  764. if ((fep->mii_bus->phy_mask & (1 << phy_id)))
  765. continue;
  766. if (fep->mii_bus->phy_map[phy_id] == NULL)
  767. continue;
  768. if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
  769. continue;
  770. if (dev_id--)
  771. continue;
  772. strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
  773. break;
  774. }
  775. if (phy_id >= PHY_MAX_ADDR) {
  776. printk(KERN_INFO
  777. "%s: no PHY, assuming direct connection to switch\n",
  778. ndev->name);
  779. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
  780. phy_id = 0;
  781. }
  782. snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
  783. phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, 0,
  784. fep->phy_interface);
  785. if (IS_ERR(phy_dev)) {
  786. printk(KERN_ERR "%s: could not attach to PHY\n", ndev->name);
  787. return PTR_ERR(phy_dev);
  788. }
  789. /* mask with MAC supported features */
  790. if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT)
  791. phy_dev->supported &= PHY_GBIT_FEATURES;
  792. else
  793. phy_dev->supported &= PHY_BASIC_FEATURES;
  794. phy_dev->advertising = phy_dev->supported;
  795. fep->phy_dev = phy_dev;
  796. fep->link = 0;
  797. fep->full_duplex = 0;
  798. printk(KERN_INFO
  799. "%s: Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  800. ndev->name,
  801. fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
  802. fep->phy_dev->irq);
  803. return 0;
  804. }
  805. static int fec_enet_mii_init(struct platform_device *pdev)
  806. {
  807. static struct mii_bus *fec0_mii_bus;
  808. struct net_device *ndev = platform_get_drvdata(pdev);
  809. struct fec_enet_private *fep = netdev_priv(ndev);
  810. const struct platform_device_id *id_entry =
  811. platform_get_device_id(fep->pdev);
  812. int err = -ENXIO, i;
  813. /*
  814. * The dual fec interfaces are not equivalent with enet-mac.
  815. * Here are the differences:
  816. *
  817. * - fec0 supports MII & RMII modes while fec1 only supports RMII
  818. * - fec0 acts as the 1588 time master while fec1 is slave
  819. * - external phys can only be configured by fec0
  820. *
  821. * That is to say fec1 can not work independently. It only works
  822. * when fec0 is working. The reason behind this design is that the
  823. * second interface is added primarily for Switch mode.
  824. *
  825. * Because of the last point above, both phys are attached on fec0
  826. * mdio interface in board design, and need to be configured by
  827. * fec0 mii_bus.
  828. */
  829. if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
  830. /* fec1 uses fec0 mii_bus */
  831. if (mii_cnt && fec0_mii_bus) {
  832. fep->mii_bus = fec0_mii_bus;
  833. mii_cnt++;
  834. return 0;
  835. }
  836. return -ENOENT;
  837. }
  838. fep->mii_timeout = 0;
  839. /*
  840. * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
  841. *
  842. * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
  843. * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
  844. * Reference Manual has an error on this, and gets fixed on i.MX6Q
  845. * document.
  846. */
  847. fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
  848. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
  849. fep->phy_speed--;
  850. fep->phy_speed <<= 1;
  851. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  852. fep->mii_bus = mdiobus_alloc();
  853. if (fep->mii_bus == NULL) {
  854. err = -ENOMEM;
  855. goto err_out;
  856. }
  857. fep->mii_bus->name = "fec_enet_mii_bus";
  858. fep->mii_bus->read = fec_enet_mdio_read;
  859. fep->mii_bus->write = fec_enet_mdio_write;
  860. fep->mii_bus->reset = fec_enet_mdio_reset;
  861. snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  862. pdev->name, fep->dev_id + 1);
  863. fep->mii_bus->priv = fep;
  864. fep->mii_bus->parent = &pdev->dev;
  865. fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  866. if (!fep->mii_bus->irq) {
  867. err = -ENOMEM;
  868. goto err_out_free_mdiobus;
  869. }
  870. for (i = 0; i < PHY_MAX_ADDR; i++)
  871. fep->mii_bus->irq[i] = PHY_POLL;
  872. if (mdiobus_register(fep->mii_bus))
  873. goto err_out_free_mdio_irq;
  874. mii_cnt++;
  875. /* save fec0 mii_bus */
  876. if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
  877. fec0_mii_bus = fep->mii_bus;
  878. return 0;
  879. err_out_free_mdio_irq:
  880. kfree(fep->mii_bus->irq);
  881. err_out_free_mdiobus:
  882. mdiobus_free(fep->mii_bus);
  883. err_out:
  884. return err;
  885. }
  886. static void fec_enet_mii_remove(struct fec_enet_private *fep)
  887. {
  888. if (--mii_cnt == 0) {
  889. mdiobus_unregister(fep->mii_bus);
  890. kfree(fep->mii_bus->irq);
  891. mdiobus_free(fep->mii_bus);
  892. }
  893. }
  894. static int fec_enet_get_settings(struct net_device *ndev,
  895. struct ethtool_cmd *cmd)
  896. {
  897. struct fec_enet_private *fep = netdev_priv(ndev);
  898. struct phy_device *phydev = fep->phy_dev;
  899. if (!phydev)
  900. return -ENODEV;
  901. return phy_ethtool_gset(phydev, cmd);
  902. }
  903. static int fec_enet_set_settings(struct net_device *ndev,
  904. struct ethtool_cmd *cmd)
  905. {
  906. struct fec_enet_private *fep = netdev_priv(ndev);
  907. struct phy_device *phydev = fep->phy_dev;
  908. if (!phydev)
  909. return -ENODEV;
  910. return phy_ethtool_sset(phydev, cmd);
  911. }
  912. static void fec_enet_get_drvinfo(struct net_device *ndev,
  913. struct ethtool_drvinfo *info)
  914. {
  915. struct fec_enet_private *fep = netdev_priv(ndev);
  916. strcpy(info->driver, fep->pdev->dev.driver->name);
  917. strcpy(info->version, "Revision: 1.0");
  918. strcpy(info->bus_info, dev_name(&ndev->dev));
  919. }
  920. static const struct ethtool_ops fec_enet_ethtool_ops = {
  921. .get_settings = fec_enet_get_settings,
  922. .set_settings = fec_enet_set_settings,
  923. .get_drvinfo = fec_enet_get_drvinfo,
  924. .get_link = ethtool_op_get_link,
  925. .get_ts_info = ethtool_op_get_ts_info,
  926. };
  927. static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  928. {
  929. struct fec_enet_private *fep = netdev_priv(ndev);
  930. struct phy_device *phydev = fep->phy_dev;
  931. if (!netif_running(ndev))
  932. return -EINVAL;
  933. if (!phydev)
  934. return -ENODEV;
  935. return phy_mii_ioctl(phydev, rq, cmd);
  936. }
  937. static void fec_enet_free_buffers(struct net_device *ndev)
  938. {
  939. struct fec_enet_private *fep = netdev_priv(ndev);
  940. int i;
  941. struct sk_buff *skb;
  942. struct bufdesc *bdp;
  943. bdp = fep->rx_bd_base;
  944. for (i = 0; i < RX_RING_SIZE; i++) {
  945. skb = fep->rx_skbuff[i];
  946. if (bdp->cbd_bufaddr)
  947. dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
  948. FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
  949. if (skb)
  950. dev_kfree_skb(skb);
  951. bdp++;
  952. }
  953. bdp = fep->tx_bd_base;
  954. for (i = 0; i < TX_RING_SIZE; i++)
  955. kfree(fep->tx_bounce[i]);
  956. }
  957. static int fec_enet_alloc_buffers(struct net_device *ndev)
  958. {
  959. struct fec_enet_private *fep = netdev_priv(ndev);
  960. int i;
  961. struct sk_buff *skb;
  962. struct bufdesc *bdp;
  963. bdp = fep->rx_bd_base;
  964. for (i = 0; i < RX_RING_SIZE; i++) {
  965. skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
  966. if (!skb) {
  967. fec_enet_free_buffers(ndev);
  968. return -ENOMEM;
  969. }
  970. fep->rx_skbuff[i] = skb;
  971. bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
  972. FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
  973. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  974. bdp++;
  975. }
  976. /* Set the last buffer to wrap. */
  977. bdp--;
  978. bdp->cbd_sc |= BD_SC_WRAP;
  979. bdp = fep->tx_bd_base;
  980. for (i = 0; i < TX_RING_SIZE; i++) {
  981. fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
  982. bdp->cbd_sc = 0;
  983. bdp->cbd_bufaddr = 0;
  984. bdp++;
  985. }
  986. /* Set the last buffer to wrap. */
  987. bdp--;
  988. bdp->cbd_sc |= BD_SC_WRAP;
  989. return 0;
  990. }
  991. static int
  992. fec_enet_open(struct net_device *ndev)
  993. {
  994. struct fec_enet_private *fep = netdev_priv(ndev);
  995. int ret;
  996. /* I should reset the ring buffers here, but I don't yet know
  997. * a simple way to do that.
  998. */
  999. ret = fec_enet_alloc_buffers(ndev);
  1000. if (ret)
  1001. return ret;
  1002. /* Probe and connect to PHY when open the interface */
  1003. ret = fec_enet_mii_probe(ndev);
  1004. if (ret) {
  1005. fec_enet_free_buffers(ndev);
  1006. return ret;
  1007. }
  1008. phy_start(fep->phy_dev);
  1009. netif_start_queue(ndev);
  1010. fep->opened = 1;
  1011. return 0;
  1012. }
  1013. static int
  1014. fec_enet_close(struct net_device *ndev)
  1015. {
  1016. struct fec_enet_private *fep = netdev_priv(ndev);
  1017. /* Don't know what to do yet. */
  1018. fep->opened = 0;
  1019. netif_stop_queue(ndev);
  1020. fec_stop(ndev);
  1021. if (fep->phy_dev) {
  1022. phy_stop(fep->phy_dev);
  1023. phy_disconnect(fep->phy_dev);
  1024. }
  1025. fec_enet_free_buffers(ndev);
  1026. return 0;
  1027. }
  1028. /* Set or clear the multicast filter for this adaptor.
  1029. * Skeleton taken from sunlance driver.
  1030. * The CPM Ethernet implementation allows Multicast as well as individual
  1031. * MAC address filtering. Some of the drivers check to make sure it is
  1032. * a group multicast address, and discard those that are not. I guess I
  1033. * will do the same for now, but just remove the test if you want
  1034. * individual filtering as well (do the upper net layers want or support
  1035. * this kind of feature?).
  1036. */
  1037. #define HASH_BITS 6 /* #bits in hash */
  1038. #define CRC32_POLY 0xEDB88320
  1039. static void set_multicast_list(struct net_device *ndev)
  1040. {
  1041. struct fec_enet_private *fep = netdev_priv(ndev);
  1042. struct netdev_hw_addr *ha;
  1043. unsigned int i, bit, data, crc, tmp;
  1044. unsigned char hash;
  1045. if (ndev->flags & IFF_PROMISC) {
  1046. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1047. tmp |= 0x8;
  1048. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1049. return;
  1050. }
  1051. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1052. tmp &= ~0x8;
  1053. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1054. if (ndev->flags & IFF_ALLMULTI) {
  1055. /* Catch all multicast addresses, so set the
  1056. * filter to all 1's
  1057. */
  1058. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1059. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1060. return;
  1061. }
  1062. /* Clear filter and add the addresses in hash register
  1063. */
  1064. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1065. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1066. netdev_for_each_mc_addr(ha, ndev) {
  1067. /* calculate crc32 value of mac address */
  1068. crc = 0xffffffff;
  1069. for (i = 0; i < ndev->addr_len; i++) {
  1070. data = ha->addr[i];
  1071. for (bit = 0; bit < 8; bit++, data >>= 1) {
  1072. crc = (crc >> 1) ^
  1073. (((crc ^ data) & 1) ? CRC32_POLY : 0);
  1074. }
  1075. }
  1076. /* only upper 6 bits (HASH_BITS) are used
  1077. * which point to specific bit in he hash registers
  1078. */
  1079. hash = (crc >> (32 - HASH_BITS)) & 0x3f;
  1080. if (hash > 31) {
  1081. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1082. tmp |= 1 << (hash - 32);
  1083. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1084. } else {
  1085. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1086. tmp |= 1 << hash;
  1087. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1088. }
  1089. }
  1090. }
  1091. /* Set a MAC change in hardware. */
  1092. static int
  1093. fec_set_mac_address(struct net_device *ndev, void *p)
  1094. {
  1095. struct fec_enet_private *fep = netdev_priv(ndev);
  1096. struct sockaddr *addr = p;
  1097. if (!is_valid_ether_addr(addr->sa_data))
  1098. return -EADDRNOTAVAIL;
  1099. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  1100. writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
  1101. (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
  1102. fep->hwp + FEC_ADDR_LOW);
  1103. writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
  1104. fep->hwp + FEC_ADDR_HIGH);
  1105. return 0;
  1106. }
  1107. #ifdef CONFIG_NET_POLL_CONTROLLER
  1108. /**
  1109. * fec_poll_controller - FEC Poll controller function
  1110. * @dev: The FEC network adapter
  1111. *
  1112. * Polled functionality used by netconsole and others in non interrupt mode
  1113. *
  1114. */
  1115. void fec_poll_controller(struct net_device *dev)
  1116. {
  1117. int i;
  1118. struct fec_enet_private *fep = netdev_priv(dev);
  1119. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1120. if (fep->irq[i] > 0) {
  1121. disable_irq(fep->irq[i]);
  1122. fec_enet_interrupt(fep->irq[i], dev);
  1123. enable_irq(fep->irq[i]);
  1124. }
  1125. }
  1126. }
  1127. #endif
  1128. static const struct net_device_ops fec_netdev_ops = {
  1129. .ndo_open = fec_enet_open,
  1130. .ndo_stop = fec_enet_close,
  1131. .ndo_start_xmit = fec_enet_start_xmit,
  1132. .ndo_set_rx_mode = set_multicast_list,
  1133. .ndo_change_mtu = eth_change_mtu,
  1134. .ndo_validate_addr = eth_validate_addr,
  1135. .ndo_tx_timeout = fec_timeout,
  1136. .ndo_set_mac_address = fec_set_mac_address,
  1137. .ndo_do_ioctl = fec_enet_ioctl,
  1138. #ifdef CONFIG_NET_POLL_CONTROLLER
  1139. .ndo_poll_controller = fec_poll_controller,
  1140. #endif
  1141. };
  1142. /*
  1143. * XXX: We need to clean up on failure exits here.
  1144. *
  1145. */
  1146. static int fec_enet_init(struct net_device *ndev)
  1147. {
  1148. struct fec_enet_private *fep = netdev_priv(ndev);
  1149. struct bufdesc *cbd_base;
  1150. struct bufdesc *bdp;
  1151. int i;
  1152. /* Allocate memory for buffer descriptors. */
  1153. cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
  1154. GFP_KERNEL);
  1155. if (!cbd_base) {
  1156. printk("FEC: allocate descriptor memory failed?\n");
  1157. return -ENOMEM;
  1158. }
  1159. spin_lock_init(&fep->hw_lock);
  1160. fep->netdev = ndev;
  1161. /* Get the Ethernet address */
  1162. fec_get_mac(ndev);
  1163. /* Set receive and transmit descriptor base. */
  1164. fep->rx_bd_base = cbd_base;
  1165. fep->tx_bd_base = cbd_base + RX_RING_SIZE;
  1166. /* The FEC Ethernet specific entries in the device structure */
  1167. ndev->watchdog_timeo = TX_TIMEOUT;
  1168. ndev->netdev_ops = &fec_netdev_ops;
  1169. ndev->ethtool_ops = &fec_enet_ethtool_ops;
  1170. /* Initialize the receive buffer descriptors. */
  1171. bdp = fep->rx_bd_base;
  1172. for (i = 0; i < RX_RING_SIZE; i++) {
  1173. /* Initialize the BD for every fragment in the page. */
  1174. bdp->cbd_sc = 0;
  1175. bdp++;
  1176. }
  1177. /* Set the last buffer to wrap */
  1178. bdp--;
  1179. bdp->cbd_sc |= BD_SC_WRAP;
  1180. /* ...and the same for transmit */
  1181. bdp = fep->tx_bd_base;
  1182. for (i = 0; i < TX_RING_SIZE; i++) {
  1183. /* Initialize the BD for every fragment in the page. */
  1184. bdp->cbd_sc = 0;
  1185. bdp->cbd_bufaddr = 0;
  1186. bdp++;
  1187. }
  1188. /* Set the last buffer to wrap */
  1189. bdp--;
  1190. bdp->cbd_sc |= BD_SC_WRAP;
  1191. fec_restart(ndev, 0);
  1192. return 0;
  1193. }
  1194. #ifdef CONFIG_OF
  1195. static int __devinit fec_get_phy_mode_dt(struct platform_device *pdev)
  1196. {
  1197. struct device_node *np = pdev->dev.of_node;
  1198. if (np)
  1199. return of_get_phy_mode(np);
  1200. return -ENODEV;
  1201. }
  1202. static void __devinit fec_reset_phy(struct platform_device *pdev)
  1203. {
  1204. int err, phy_reset;
  1205. int msec = 1;
  1206. struct device_node *np = pdev->dev.of_node;
  1207. if (!np)
  1208. return;
  1209. of_property_read_u32(np, "phy-reset-duration", &msec);
  1210. /* A sane reset duration should not be longer than 1s */
  1211. if (msec > 1000)
  1212. msec = 1;
  1213. phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
  1214. err = devm_gpio_request_one(&pdev->dev, phy_reset,
  1215. GPIOF_OUT_INIT_LOW, "phy-reset");
  1216. if (err) {
  1217. pr_debug("FEC: failed to get gpio phy-reset: %d\n", err);
  1218. return;
  1219. }
  1220. msleep(msec);
  1221. gpio_set_value(phy_reset, 1);
  1222. }
  1223. #else /* CONFIG_OF */
  1224. static inline int fec_get_phy_mode_dt(struct platform_device *pdev)
  1225. {
  1226. return -ENODEV;
  1227. }
  1228. static inline void fec_reset_phy(struct platform_device *pdev)
  1229. {
  1230. /*
  1231. * In case of platform probe, the reset has been done
  1232. * by machine code.
  1233. */
  1234. }
  1235. #endif /* CONFIG_OF */
  1236. static int __devinit
  1237. fec_probe(struct platform_device *pdev)
  1238. {
  1239. struct fec_enet_private *fep;
  1240. struct fec_platform_data *pdata;
  1241. struct net_device *ndev;
  1242. int i, irq, ret = 0;
  1243. struct resource *r;
  1244. const struct of_device_id *of_id;
  1245. static int dev_id;
  1246. struct pinctrl *pinctrl;
  1247. struct regulator *reg_phy;
  1248. of_id = of_match_device(fec_dt_ids, &pdev->dev);
  1249. if (of_id)
  1250. pdev->id_entry = of_id->data;
  1251. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1252. if (!r)
  1253. return -ENXIO;
  1254. r = request_mem_region(r->start, resource_size(r), pdev->name);
  1255. if (!r)
  1256. return -EBUSY;
  1257. /* Init network device */
  1258. ndev = alloc_etherdev(sizeof(struct fec_enet_private));
  1259. if (!ndev) {
  1260. ret = -ENOMEM;
  1261. goto failed_alloc_etherdev;
  1262. }
  1263. SET_NETDEV_DEV(ndev, &pdev->dev);
  1264. /* setup board info structure */
  1265. fep = netdev_priv(ndev);
  1266. fep->hwp = ioremap(r->start, resource_size(r));
  1267. fep->pdev = pdev;
  1268. fep->dev_id = dev_id++;
  1269. if (!fep->hwp) {
  1270. ret = -ENOMEM;
  1271. goto failed_ioremap;
  1272. }
  1273. platform_set_drvdata(pdev, ndev);
  1274. ret = fec_get_phy_mode_dt(pdev);
  1275. if (ret < 0) {
  1276. pdata = pdev->dev.platform_data;
  1277. if (pdata)
  1278. fep->phy_interface = pdata->phy;
  1279. else
  1280. fep->phy_interface = PHY_INTERFACE_MODE_MII;
  1281. } else {
  1282. fep->phy_interface = ret;
  1283. }
  1284. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1285. irq = platform_get_irq(pdev, i);
  1286. if (irq < 0) {
  1287. if (i)
  1288. break;
  1289. ret = irq;
  1290. goto failed_irq;
  1291. }
  1292. ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
  1293. if (ret) {
  1294. while (--i >= 0) {
  1295. irq = platform_get_irq(pdev, i);
  1296. free_irq(irq, ndev);
  1297. }
  1298. goto failed_irq;
  1299. }
  1300. }
  1301. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  1302. if (IS_ERR(pinctrl)) {
  1303. ret = PTR_ERR(pinctrl);
  1304. goto failed_pin;
  1305. }
  1306. fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1307. if (IS_ERR(fep->clk_ipg)) {
  1308. ret = PTR_ERR(fep->clk_ipg);
  1309. goto failed_clk;
  1310. }
  1311. fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1312. if (IS_ERR(fep->clk_ahb)) {
  1313. ret = PTR_ERR(fep->clk_ahb);
  1314. goto failed_clk;
  1315. }
  1316. clk_prepare_enable(fep->clk_ahb);
  1317. clk_prepare_enable(fep->clk_ipg);
  1318. reg_phy = devm_regulator_get(&pdev->dev, "phy");
  1319. if (!IS_ERR(reg_phy)) {
  1320. ret = regulator_enable(reg_phy);
  1321. if (ret) {
  1322. dev_err(&pdev->dev,
  1323. "Failed to enable phy regulator: %d\n", ret);
  1324. goto failed_regulator;
  1325. }
  1326. }
  1327. fec_reset_phy(pdev);
  1328. ret = fec_enet_init(ndev);
  1329. if (ret)
  1330. goto failed_init;
  1331. ret = fec_enet_mii_init(pdev);
  1332. if (ret)
  1333. goto failed_mii_init;
  1334. /* Carrier starts down, phylib will bring it up */
  1335. netif_carrier_off(ndev);
  1336. ret = register_netdev(ndev);
  1337. if (ret)
  1338. goto failed_register;
  1339. return 0;
  1340. failed_register:
  1341. fec_enet_mii_remove(fep);
  1342. failed_mii_init:
  1343. failed_init:
  1344. failed_regulator:
  1345. clk_disable_unprepare(fep->clk_ahb);
  1346. clk_disable_unprepare(fep->clk_ipg);
  1347. failed_pin:
  1348. failed_clk:
  1349. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1350. irq = platform_get_irq(pdev, i);
  1351. if (irq > 0)
  1352. free_irq(irq, ndev);
  1353. }
  1354. failed_irq:
  1355. iounmap(fep->hwp);
  1356. failed_ioremap:
  1357. free_netdev(ndev);
  1358. failed_alloc_etherdev:
  1359. release_mem_region(r->start, resource_size(r));
  1360. return ret;
  1361. }
  1362. static int __devexit
  1363. fec_drv_remove(struct platform_device *pdev)
  1364. {
  1365. struct net_device *ndev = platform_get_drvdata(pdev);
  1366. struct fec_enet_private *fep = netdev_priv(ndev);
  1367. struct resource *r;
  1368. int i;
  1369. unregister_netdev(ndev);
  1370. fec_enet_mii_remove(fep);
  1371. for (i = 0; i < FEC_IRQ_NUM; i++) {
  1372. int irq = platform_get_irq(pdev, i);
  1373. if (irq > 0)
  1374. free_irq(irq, ndev);
  1375. }
  1376. clk_disable_unprepare(fep->clk_ahb);
  1377. clk_disable_unprepare(fep->clk_ipg);
  1378. iounmap(fep->hwp);
  1379. free_netdev(ndev);
  1380. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1381. BUG_ON(!r);
  1382. release_mem_region(r->start, resource_size(r));
  1383. platform_set_drvdata(pdev, NULL);
  1384. return 0;
  1385. }
  1386. #ifdef CONFIG_PM
  1387. static int
  1388. fec_suspend(struct device *dev)
  1389. {
  1390. struct net_device *ndev = dev_get_drvdata(dev);
  1391. struct fec_enet_private *fep = netdev_priv(ndev);
  1392. if (netif_running(ndev)) {
  1393. fec_stop(ndev);
  1394. netif_device_detach(ndev);
  1395. }
  1396. clk_disable_unprepare(fep->clk_ahb);
  1397. clk_disable_unprepare(fep->clk_ipg);
  1398. return 0;
  1399. }
  1400. static int
  1401. fec_resume(struct device *dev)
  1402. {
  1403. struct net_device *ndev = dev_get_drvdata(dev);
  1404. struct fec_enet_private *fep = netdev_priv(ndev);
  1405. clk_prepare_enable(fep->clk_ahb);
  1406. clk_prepare_enable(fep->clk_ipg);
  1407. if (netif_running(ndev)) {
  1408. fec_restart(ndev, fep->full_duplex);
  1409. netif_device_attach(ndev);
  1410. }
  1411. return 0;
  1412. }
  1413. static const struct dev_pm_ops fec_pm_ops = {
  1414. .suspend = fec_suspend,
  1415. .resume = fec_resume,
  1416. .freeze = fec_suspend,
  1417. .thaw = fec_resume,
  1418. .poweroff = fec_suspend,
  1419. .restore = fec_resume,
  1420. };
  1421. #endif
  1422. static struct platform_driver fec_driver = {
  1423. .driver = {
  1424. .name = DRIVER_NAME,
  1425. .owner = THIS_MODULE,
  1426. #ifdef CONFIG_PM
  1427. .pm = &fec_pm_ops,
  1428. #endif
  1429. .of_match_table = fec_dt_ids,
  1430. },
  1431. .id_table = fec_devtype,
  1432. .probe = fec_probe,
  1433. .remove = __devexit_p(fec_drv_remove),
  1434. };
  1435. module_platform_driver(fec_driver);
  1436. MODULE_LICENSE("GPL");