fec.c 36 KB

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