fec.c 43 KB

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