fec.c 35 KB

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