fec.c 39 KB

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