fec.c 41 KB

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