fec.c 44 KB

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