fec.c 36 KB

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