fec.c 35 KB

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