fec.c 39 KB

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