fec.c 36 KB

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