fec.c 39 KB

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