fec.c 44 KB

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