fec_mpc52xx.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Driver for the MPC5200 Fast Ethernet Controller
  3. *
  4. * Originally written by Dale Farnsworth <dfarnsworth@mvista.com> and
  5. * now maintained by Sylvain Munaut <tnt@246tNt.com>
  6. *
  7. * Copyright (C) 2007 Domen Puncer, Telargo, Inc.
  8. * Copyright (C) 2007 Sylvain Munaut <tnt@246tNt.com>
  9. * Copyright (C) 2003-2004 MontaVista, Software, Inc.
  10. *
  11. * This file is licensed under the terms of the GNU General Public License
  12. * version 2. This program is licensed "as is" without any warranty of any
  13. * kind, whether express or implied.
  14. *
  15. */
  16. #include <linux/dma-mapping.h>
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/slab.h>
  22. #include <linux/errno.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/crc32.h>
  26. #include <linux/hardirq.h>
  27. #include <linux/delay.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of_mdio.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/skbuff.h>
  35. #include <asm/io.h>
  36. #include <asm/delay.h>
  37. #include <asm/mpc52xx.h>
  38. #include <sysdev/bestcomm/bestcomm.h>
  39. #include <sysdev/bestcomm/fec.h>
  40. #include "fec_mpc52xx.h"
  41. #define DRIVER_NAME "mpc52xx-fec"
  42. /* Private driver data structure */
  43. struct mpc52xx_fec_priv {
  44. struct net_device *ndev;
  45. int duplex;
  46. int speed;
  47. int r_irq;
  48. int t_irq;
  49. struct mpc52xx_fec __iomem *fec;
  50. struct bcom_task *rx_dmatsk;
  51. struct bcom_task *tx_dmatsk;
  52. spinlock_t lock;
  53. int msg_enable;
  54. /* MDIO link details */
  55. unsigned int mdio_speed;
  56. struct device_node *phy_node;
  57. struct phy_device *phydev;
  58. enum phy_state link;
  59. int seven_wire_mode;
  60. };
  61. static irqreturn_t mpc52xx_fec_interrupt(int, void *);
  62. static irqreturn_t mpc52xx_fec_rx_interrupt(int, void *);
  63. static irqreturn_t mpc52xx_fec_tx_interrupt(int, void *);
  64. static void mpc52xx_fec_stop(struct net_device *dev);
  65. static void mpc52xx_fec_start(struct net_device *dev);
  66. static void mpc52xx_fec_reset(struct net_device *dev);
  67. static u8 mpc52xx_fec_mac_addr[6];
  68. module_param_array_named(mac, mpc52xx_fec_mac_addr, byte, NULL, 0);
  69. MODULE_PARM_DESC(mac, "six hex digits, ie. 0x1,0x2,0xc0,0x01,0xba,0xbe");
  70. #define MPC52xx_MESSAGES_DEFAULT ( NETIF_MSG_DRV | NETIF_MSG_PROBE | \
  71. NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
  72. static int debug = -1; /* the above default */
  73. module_param(debug, int, 0);
  74. MODULE_PARM_DESC(debug, "debugging messages level");
  75. static void mpc52xx_fec_tx_timeout(struct net_device *dev)
  76. {
  77. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  78. unsigned long flags;
  79. dev_warn(&dev->dev, "transmit timed out\n");
  80. spin_lock_irqsave(&priv->lock, flags);
  81. mpc52xx_fec_reset(dev);
  82. dev->stats.tx_errors++;
  83. spin_unlock_irqrestore(&priv->lock, flags);
  84. netif_wake_queue(dev);
  85. }
  86. static void mpc52xx_fec_set_paddr(struct net_device *dev, u8 *mac)
  87. {
  88. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  89. struct mpc52xx_fec __iomem *fec = priv->fec;
  90. out_be32(&fec->paddr1, *(u32 *)(&mac[0]));
  91. out_be32(&fec->paddr2, (*(u16 *)(&mac[4]) << 16) | FEC_PADDR2_TYPE);
  92. }
  93. static void mpc52xx_fec_get_paddr(struct net_device *dev, u8 *mac)
  94. {
  95. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  96. struct mpc52xx_fec __iomem *fec = priv->fec;
  97. *(u32 *)(&mac[0]) = in_be32(&fec->paddr1);
  98. *(u16 *)(&mac[4]) = in_be32(&fec->paddr2) >> 16;
  99. }
  100. static int mpc52xx_fec_set_mac_address(struct net_device *dev, void *addr)
  101. {
  102. struct sockaddr *sock = addr;
  103. memcpy(dev->dev_addr, sock->sa_data, dev->addr_len);
  104. mpc52xx_fec_set_paddr(dev, sock->sa_data);
  105. return 0;
  106. }
  107. static void mpc52xx_fec_free_rx_buffers(struct net_device *dev, struct bcom_task *s)
  108. {
  109. while (!bcom_queue_empty(s)) {
  110. struct bcom_fec_bd *bd;
  111. struct sk_buff *skb;
  112. skb = bcom_retrieve_buffer(s, NULL, (struct bcom_bd **)&bd);
  113. dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
  114. DMA_FROM_DEVICE);
  115. kfree_skb(skb);
  116. }
  117. }
  118. static void
  119. mpc52xx_fec_rx_submit(struct net_device *dev, struct sk_buff *rskb)
  120. {
  121. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  122. struct bcom_fec_bd *bd;
  123. bd = (struct bcom_fec_bd *) bcom_prepare_next_buffer(priv->rx_dmatsk);
  124. bd->status = FEC_RX_BUFFER_SIZE;
  125. bd->skb_pa = dma_map_single(dev->dev.parent, rskb->data,
  126. FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
  127. bcom_submit_next_buffer(priv->rx_dmatsk, rskb);
  128. }
  129. static int mpc52xx_fec_alloc_rx_buffers(struct net_device *dev, struct bcom_task *rxtsk)
  130. {
  131. struct sk_buff *skb;
  132. while (!bcom_queue_full(rxtsk)) {
  133. skb = netdev_alloc_skb(dev, FEC_RX_BUFFER_SIZE);
  134. if (!skb)
  135. return -EAGAIN;
  136. /* zero out the initial receive buffers to aid debugging */
  137. memset(skb->data, 0, FEC_RX_BUFFER_SIZE);
  138. mpc52xx_fec_rx_submit(dev, skb);
  139. }
  140. return 0;
  141. }
  142. /* based on generic_adjust_link from fs_enet-main.c */
  143. static void mpc52xx_fec_adjust_link(struct net_device *dev)
  144. {
  145. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  146. struct phy_device *phydev = priv->phydev;
  147. int new_state = 0;
  148. if (phydev->link != PHY_DOWN) {
  149. if (phydev->duplex != priv->duplex) {
  150. struct mpc52xx_fec __iomem *fec = priv->fec;
  151. u32 rcntrl;
  152. u32 tcntrl;
  153. new_state = 1;
  154. priv->duplex = phydev->duplex;
  155. rcntrl = in_be32(&fec->r_cntrl);
  156. tcntrl = in_be32(&fec->x_cntrl);
  157. rcntrl &= ~FEC_RCNTRL_DRT;
  158. tcntrl &= ~FEC_TCNTRL_FDEN;
  159. if (phydev->duplex == DUPLEX_FULL)
  160. tcntrl |= FEC_TCNTRL_FDEN; /* FD enable */
  161. else
  162. rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
  163. out_be32(&fec->r_cntrl, rcntrl);
  164. out_be32(&fec->x_cntrl, tcntrl);
  165. }
  166. if (phydev->speed != priv->speed) {
  167. new_state = 1;
  168. priv->speed = phydev->speed;
  169. }
  170. if (priv->link == PHY_DOWN) {
  171. new_state = 1;
  172. priv->link = phydev->link;
  173. }
  174. } else if (priv->link) {
  175. new_state = 1;
  176. priv->link = PHY_DOWN;
  177. priv->speed = 0;
  178. priv->duplex = -1;
  179. }
  180. if (new_state && netif_msg_link(priv))
  181. phy_print_status(phydev);
  182. }
  183. static int mpc52xx_fec_open(struct net_device *dev)
  184. {
  185. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  186. int err = -EBUSY;
  187. if (priv->phy_node) {
  188. priv->phydev = of_phy_connect(priv->ndev, priv->phy_node,
  189. mpc52xx_fec_adjust_link, 0, 0);
  190. if (!priv->phydev) {
  191. dev_err(&dev->dev, "of_phy_connect failed\n");
  192. return -ENODEV;
  193. }
  194. phy_start(priv->phydev);
  195. }
  196. if (request_irq(dev->irq, mpc52xx_fec_interrupt, IRQF_SHARED,
  197. DRIVER_NAME "_ctrl", dev)) {
  198. dev_err(&dev->dev, "ctrl interrupt request failed\n");
  199. goto free_phy;
  200. }
  201. if (request_irq(priv->r_irq, mpc52xx_fec_rx_interrupt, 0,
  202. DRIVER_NAME "_rx", dev)) {
  203. dev_err(&dev->dev, "rx interrupt request failed\n");
  204. goto free_ctrl_irq;
  205. }
  206. if (request_irq(priv->t_irq, mpc52xx_fec_tx_interrupt, 0,
  207. DRIVER_NAME "_tx", dev)) {
  208. dev_err(&dev->dev, "tx interrupt request failed\n");
  209. goto free_2irqs;
  210. }
  211. bcom_fec_rx_reset(priv->rx_dmatsk);
  212. bcom_fec_tx_reset(priv->tx_dmatsk);
  213. err = mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
  214. if (err) {
  215. dev_err(&dev->dev, "mpc52xx_fec_alloc_rx_buffers failed\n");
  216. goto free_irqs;
  217. }
  218. bcom_enable(priv->rx_dmatsk);
  219. bcom_enable(priv->tx_dmatsk);
  220. mpc52xx_fec_start(dev);
  221. netif_start_queue(dev);
  222. return 0;
  223. free_irqs:
  224. free_irq(priv->t_irq, dev);
  225. free_2irqs:
  226. free_irq(priv->r_irq, dev);
  227. free_ctrl_irq:
  228. free_irq(dev->irq, dev);
  229. free_phy:
  230. if (priv->phydev) {
  231. phy_stop(priv->phydev);
  232. phy_disconnect(priv->phydev);
  233. priv->phydev = NULL;
  234. }
  235. return err;
  236. }
  237. static int mpc52xx_fec_close(struct net_device *dev)
  238. {
  239. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  240. netif_stop_queue(dev);
  241. mpc52xx_fec_stop(dev);
  242. mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
  243. free_irq(dev->irq, dev);
  244. free_irq(priv->r_irq, dev);
  245. free_irq(priv->t_irq, dev);
  246. if (priv->phydev) {
  247. /* power down phy */
  248. phy_stop(priv->phydev);
  249. phy_disconnect(priv->phydev);
  250. priv->phydev = NULL;
  251. }
  252. return 0;
  253. }
  254. /* This will only be invoked if your driver is _not_ in XOFF state.
  255. * What this means is that you need not check it, and that this
  256. * invariant will hold if you make sure that the netif_*_queue()
  257. * calls are done at the proper times.
  258. */
  259. static int mpc52xx_fec_start_xmit(struct sk_buff *skb, struct net_device *dev)
  260. {
  261. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  262. struct bcom_fec_bd *bd;
  263. unsigned long flags;
  264. if (bcom_queue_full(priv->tx_dmatsk)) {
  265. if (net_ratelimit())
  266. dev_err(&dev->dev, "transmit queue overrun\n");
  267. return NETDEV_TX_BUSY;
  268. }
  269. spin_lock_irqsave(&priv->lock, flags);
  270. bd = (struct bcom_fec_bd *)
  271. bcom_prepare_next_buffer(priv->tx_dmatsk);
  272. bd->status = skb->len | BCOM_FEC_TX_BD_TFD | BCOM_FEC_TX_BD_TC;
  273. bd->skb_pa = dma_map_single(dev->dev.parent, skb->data, skb->len,
  274. DMA_TO_DEVICE);
  275. skb_tx_timestamp(skb);
  276. bcom_submit_next_buffer(priv->tx_dmatsk, skb);
  277. spin_unlock_irqrestore(&priv->lock, flags);
  278. if (bcom_queue_full(priv->tx_dmatsk)) {
  279. netif_stop_queue(dev);
  280. }
  281. return NETDEV_TX_OK;
  282. }
  283. #ifdef CONFIG_NET_POLL_CONTROLLER
  284. static void mpc52xx_fec_poll_controller(struct net_device *dev)
  285. {
  286. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  287. disable_irq(priv->t_irq);
  288. mpc52xx_fec_tx_interrupt(priv->t_irq, dev);
  289. enable_irq(priv->t_irq);
  290. disable_irq(priv->r_irq);
  291. mpc52xx_fec_rx_interrupt(priv->r_irq, dev);
  292. enable_irq(priv->r_irq);
  293. }
  294. #endif
  295. /* This handles BestComm transmit task interrupts
  296. */
  297. static irqreturn_t mpc52xx_fec_tx_interrupt(int irq, void *dev_id)
  298. {
  299. struct net_device *dev = dev_id;
  300. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  301. spin_lock(&priv->lock);
  302. while (bcom_buffer_done(priv->tx_dmatsk)) {
  303. struct sk_buff *skb;
  304. struct bcom_fec_bd *bd;
  305. skb = bcom_retrieve_buffer(priv->tx_dmatsk, NULL,
  306. (struct bcom_bd **)&bd);
  307. dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
  308. DMA_TO_DEVICE);
  309. dev_kfree_skb_irq(skb);
  310. }
  311. spin_unlock(&priv->lock);
  312. netif_wake_queue(dev);
  313. return IRQ_HANDLED;
  314. }
  315. static irqreturn_t mpc52xx_fec_rx_interrupt(int irq, void *dev_id)
  316. {
  317. struct net_device *dev = dev_id;
  318. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  319. struct sk_buff *rskb; /* received sk_buff */
  320. struct sk_buff *skb; /* new sk_buff to enqueue in its place */
  321. struct bcom_fec_bd *bd;
  322. u32 status, physaddr;
  323. int length;
  324. spin_lock(&priv->lock);
  325. while (bcom_buffer_done(priv->rx_dmatsk)) {
  326. rskb = bcom_retrieve_buffer(priv->rx_dmatsk, &status,
  327. (struct bcom_bd **)&bd);
  328. physaddr = bd->skb_pa;
  329. /* Test for errors in received frame */
  330. if (status & BCOM_FEC_RX_BD_ERRORS) {
  331. /* Drop packet and reuse the buffer */
  332. mpc52xx_fec_rx_submit(dev, rskb);
  333. dev->stats.rx_dropped++;
  334. continue;
  335. }
  336. /* skbs are allocated on open, so now we allocate a new one,
  337. * and remove the old (with the packet) */
  338. skb = netdev_alloc_skb(dev, FEC_RX_BUFFER_SIZE);
  339. if (!skb) {
  340. /* Can't get a new one : reuse the same & drop pkt */
  341. dev_notice(&dev->dev, "Low memory - dropped packet.\n");
  342. mpc52xx_fec_rx_submit(dev, rskb);
  343. dev->stats.rx_dropped++;
  344. continue;
  345. }
  346. /* Enqueue the new sk_buff back on the hardware */
  347. mpc52xx_fec_rx_submit(dev, skb);
  348. /* Process the received skb - Drop the spin lock while
  349. * calling into the network stack */
  350. spin_unlock(&priv->lock);
  351. dma_unmap_single(dev->dev.parent, physaddr, rskb->len,
  352. DMA_FROM_DEVICE);
  353. length = status & BCOM_FEC_RX_BD_LEN_MASK;
  354. skb_put(rskb, length - 4); /* length without CRC32 */
  355. rskb->protocol = eth_type_trans(rskb, dev);
  356. if (!skb_defer_rx_timestamp(rskb))
  357. netif_rx(rskb);
  358. spin_lock(&priv->lock);
  359. }
  360. spin_unlock(&priv->lock);
  361. return IRQ_HANDLED;
  362. }
  363. static irqreturn_t mpc52xx_fec_interrupt(int irq, void *dev_id)
  364. {
  365. struct net_device *dev = dev_id;
  366. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  367. struct mpc52xx_fec __iomem *fec = priv->fec;
  368. u32 ievent;
  369. ievent = in_be32(&fec->ievent);
  370. ievent &= ~FEC_IEVENT_MII; /* mii is handled separately */
  371. if (!ievent)
  372. return IRQ_NONE;
  373. out_be32(&fec->ievent, ievent); /* clear pending events */
  374. /* on fifo error, soft-reset fec */
  375. if (ievent & (FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {
  376. if (net_ratelimit() && (ievent & FEC_IEVENT_RFIFO_ERROR))
  377. dev_warn(&dev->dev, "FEC_IEVENT_RFIFO_ERROR\n");
  378. if (net_ratelimit() && (ievent & FEC_IEVENT_XFIFO_ERROR))
  379. dev_warn(&dev->dev, "FEC_IEVENT_XFIFO_ERROR\n");
  380. spin_lock(&priv->lock);
  381. mpc52xx_fec_reset(dev);
  382. spin_unlock(&priv->lock);
  383. return IRQ_HANDLED;
  384. }
  385. if (ievent & ~FEC_IEVENT_TFINT)
  386. dev_dbg(&dev->dev, "ievent: %08x\n", ievent);
  387. return IRQ_HANDLED;
  388. }
  389. /*
  390. * Get the current statistics.
  391. * This may be called with the card open or closed.
  392. */
  393. static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev)
  394. {
  395. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  396. struct net_device_stats *stats = &dev->stats;
  397. struct mpc52xx_fec __iomem *fec = priv->fec;
  398. stats->rx_bytes = in_be32(&fec->rmon_r_octets);
  399. stats->rx_packets = in_be32(&fec->rmon_r_packets);
  400. stats->rx_errors = in_be32(&fec->rmon_r_crc_align) +
  401. in_be32(&fec->rmon_r_undersize) +
  402. in_be32(&fec->rmon_r_oversize) +
  403. in_be32(&fec->rmon_r_frag) +
  404. in_be32(&fec->rmon_r_jab);
  405. stats->tx_bytes = in_be32(&fec->rmon_t_octets);
  406. stats->tx_packets = in_be32(&fec->rmon_t_packets);
  407. stats->tx_errors = in_be32(&fec->rmon_t_crc_align) +
  408. in_be32(&fec->rmon_t_undersize) +
  409. in_be32(&fec->rmon_t_oversize) +
  410. in_be32(&fec->rmon_t_frag) +
  411. in_be32(&fec->rmon_t_jab);
  412. stats->multicast = in_be32(&fec->rmon_r_mc_pkt);
  413. stats->collisions = in_be32(&fec->rmon_t_col);
  414. /* detailed rx_errors: */
  415. stats->rx_length_errors = in_be32(&fec->rmon_r_undersize)
  416. + in_be32(&fec->rmon_r_oversize)
  417. + in_be32(&fec->rmon_r_frag)
  418. + in_be32(&fec->rmon_r_jab);
  419. stats->rx_over_errors = in_be32(&fec->r_macerr);
  420. stats->rx_crc_errors = in_be32(&fec->ieee_r_crc);
  421. stats->rx_frame_errors = in_be32(&fec->ieee_r_align);
  422. stats->rx_fifo_errors = in_be32(&fec->rmon_r_drop);
  423. stats->rx_missed_errors = in_be32(&fec->rmon_r_drop);
  424. /* detailed tx_errors: */
  425. stats->tx_aborted_errors = 0;
  426. stats->tx_carrier_errors = in_be32(&fec->ieee_t_cserr);
  427. stats->tx_fifo_errors = in_be32(&fec->rmon_t_drop);
  428. stats->tx_heartbeat_errors = in_be32(&fec->ieee_t_sqe);
  429. stats->tx_window_errors = in_be32(&fec->ieee_t_lcol);
  430. return stats;
  431. }
  432. /*
  433. * Read MIB counters in order to reset them,
  434. * then zero all the stats fields in memory
  435. */
  436. static void mpc52xx_fec_reset_stats(struct net_device *dev)
  437. {
  438. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  439. struct mpc52xx_fec __iomem *fec = priv->fec;
  440. out_be32(&fec->mib_control, FEC_MIB_DISABLE);
  441. memset_io(&fec->rmon_t_drop, 0,
  442. offsetof(struct mpc52xx_fec, reserved10) -
  443. offsetof(struct mpc52xx_fec, rmon_t_drop));
  444. out_be32(&fec->mib_control, 0);
  445. memset(&dev->stats, 0, sizeof(dev->stats));
  446. }
  447. /*
  448. * Set or clear the multicast filter for this adaptor.
  449. */
  450. static void mpc52xx_fec_set_multicast_list(struct net_device *dev)
  451. {
  452. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  453. struct mpc52xx_fec __iomem *fec = priv->fec;
  454. u32 rx_control;
  455. rx_control = in_be32(&fec->r_cntrl);
  456. if (dev->flags & IFF_PROMISC) {
  457. rx_control |= FEC_RCNTRL_PROM;
  458. out_be32(&fec->r_cntrl, rx_control);
  459. } else {
  460. rx_control &= ~FEC_RCNTRL_PROM;
  461. out_be32(&fec->r_cntrl, rx_control);
  462. if (dev->flags & IFF_ALLMULTI) {
  463. out_be32(&fec->gaddr1, 0xffffffff);
  464. out_be32(&fec->gaddr2, 0xffffffff);
  465. } else {
  466. u32 crc;
  467. struct netdev_hw_addr *ha;
  468. u32 gaddr1 = 0x00000000;
  469. u32 gaddr2 = 0x00000000;
  470. netdev_for_each_mc_addr(ha, dev) {
  471. crc = ether_crc_le(6, ha->addr) >> 26;
  472. if (crc >= 32)
  473. gaddr1 |= 1 << (crc-32);
  474. else
  475. gaddr2 |= 1 << crc;
  476. }
  477. out_be32(&fec->gaddr1, gaddr1);
  478. out_be32(&fec->gaddr2, gaddr2);
  479. }
  480. }
  481. }
  482. /**
  483. * mpc52xx_fec_hw_init
  484. * @dev: network device
  485. *
  486. * Setup various hardware setting, only needed once on start
  487. */
  488. static void mpc52xx_fec_hw_init(struct net_device *dev)
  489. {
  490. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  491. struct mpc52xx_fec __iomem *fec = priv->fec;
  492. int i;
  493. /* Whack a reset. We should wait for this. */
  494. out_be32(&fec->ecntrl, FEC_ECNTRL_RESET);
  495. for (i = 0; i < FEC_RESET_DELAY; ++i) {
  496. if ((in_be32(&fec->ecntrl) & FEC_ECNTRL_RESET) == 0)
  497. break;
  498. udelay(1);
  499. }
  500. if (i == FEC_RESET_DELAY)
  501. dev_err(&dev->dev, "FEC Reset timeout!\n");
  502. /* set pause to 0x20 frames */
  503. out_be32(&fec->op_pause, FEC_OP_PAUSE_OPCODE | 0x20);
  504. /* high service request will be deasserted when there's < 7 bytes in fifo
  505. * low service request will be deasserted when there's < 4*7 bytes in fifo
  506. */
  507. out_be32(&fec->rfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
  508. out_be32(&fec->tfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
  509. /* alarm when <= x bytes in FIFO */
  510. out_be32(&fec->rfifo_alarm, 0x0000030c);
  511. out_be32(&fec->tfifo_alarm, 0x00000100);
  512. /* begin transmittion when 256 bytes are in FIFO (or EOF or FIFO full) */
  513. out_be32(&fec->x_wmrk, FEC_FIFO_WMRK_256B);
  514. /* enable crc generation */
  515. out_be32(&fec->xmit_fsm, FEC_XMIT_FSM_APPEND_CRC | FEC_XMIT_FSM_ENABLE_CRC);
  516. out_be32(&fec->iaddr1, 0x00000000); /* No individual filter */
  517. out_be32(&fec->iaddr2, 0x00000000); /* No individual filter */
  518. /* set phy speed.
  519. * this can't be done in phy driver, since it needs to be called
  520. * before fec stuff (even on resume) */
  521. out_be32(&fec->mii_speed, priv->mdio_speed);
  522. }
  523. /**
  524. * mpc52xx_fec_start
  525. * @dev: network device
  526. *
  527. * This function is called to start or restart the FEC during a link
  528. * change. This happens on fifo errors or when switching between half
  529. * and full duplex.
  530. */
  531. static void mpc52xx_fec_start(struct net_device *dev)
  532. {
  533. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  534. struct mpc52xx_fec __iomem *fec = priv->fec;
  535. u32 rcntrl;
  536. u32 tcntrl;
  537. u32 tmp;
  538. /* clear sticky error bits */
  539. tmp = FEC_FIFO_STATUS_ERR | FEC_FIFO_STATUS_UF | FEC_FIFO_STATUS_OF;
  540. out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status) & tmp);
  541. out_be32(&fec->tfifo_status, in_be32(&fec->tfifo_status) & tmp);
  542. /* FIFOs will reset on mpc52xx_fec_enable */
  543. out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_ENABLE_IS_RESET);
  544. /* Set station address. */
  545. mpc52xx_fec_set_paddr(dev, dev->dev_addr);
  546. mpc52xx_fec_set_multicast_list(dev);
  547. /* set max frame len, enable flow control, select mii mode */
  548. rcntrl = FEC_RX_BUFFER_SIZE << 16; /* max frame length */
  549. rcntrl |= FEC_RCNTRL_FCE;
  550. if (!priv->seven_wire_mode)
  551. rcntrl |= FEC_RCNTRL_MII_MODE;
  552. if (priv->duplex == DUPLEX_FULL)
  553. tcntrl = FEC_TCNTRL_FDEN; /* FD enable */
  554. else {
  555. rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
  556. tcntrl = 0;
  557. }
  558. out_be32(&fec->r_cntrl, rcntrl);
  559. out_be32(&fec->x_cntrl, tcntrl);
  560. /* Clear any outstanding interrupt. */
  561. out_be32(&fec->ievent, 0xffffffff);
  562. /* Enable interrupts we wish to service. */
  563. out_be32(&fec->imask, FEC_IMASK_ENABLE);
  564. /* And last, enable the transmit and receive processing. */
  565. out_be32(&fec->ecntrl, FEC_ECNTRL_ETHER_EN);
  566. out_be32(&fec->r_des_active, 0x01000000);
  567. }
  568. /**
  569. * mpc52xx_fec_stop
  570. * @dev: network device
  571. *
  572. * stop all activity on fec and empty dma buffers
  573. */
  574. static void mpc52xx_fec_stop(struct net_device *dev)
  575. {
  576. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  577. struct mpc52xx_fec __iomem *fec = priv->fec;
  578. unsigned long timeout;
  579. /* disable all interrupts */
  580. out_be32(&fec->imask, 0);
  581. /* Disable the rx task. */
  582. bcom_disable(priv->rx_dmatsk);
  583. /* Wait for tx queue to drain, but only if we're in process context */
  584. if (!in_interrupt()) {
  585. timeout = jiffies + msecs_to_jiffies(2000);
  586. while (time_before(jiffies, timeout) &&
  587. !bcom_queue_empty(priv->tx_dmatsk))
  588. msleep(100);
  589. if (time_after_eq(jiffies, timeout))
  590. dev_err(&dev->dev, "queues didn't drain\n");
  591. #if 1
  592. if (time_after_eq(jiffies, timeout)) {
  593. dev_err(&dev->dev, " tx: index: %i, outdex: %i\n",
  594. priv->tx_dmatsk->index,
  595. priv->tx_dmatsk->outdex);
  596. dev_err(&dev->dev, " rx: index: %i, outdex: %i\n",
  597. priv->rx_dmatsk->index,
  598. priv->rx_dmatsk->outdex);
  599. }
  600. #endif
  601. }
  602. bcom_disable(priv->tx_dmatsk);
  603. /* Stop FEC */
  604. out_be32(&fec->ecntrl, in_be32(&fec->ecntrl) & ~FEC_ECNTRL_ETHER_EN);
  605. }
  606. /* reset fec and bestcomm tasks */
  607. static void mpc52xx_fec_reset(struct net_device *dev)
  608. {
  609. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  610. struct mpc52xx_fec __iomem *fec = priv->fec;
  611. mpc52xx_fec_stop(dev);
  612. out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status));
  613. out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_RESET_FIFO);
  614. mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
  615. mpc52xx_fec_hw_init(dev);
  616. bcom_fec_rx_reset(priv->rx_dmatsk);
  617. bcom_fec_tx_reset(priv->tx_dmatsk);
  618. mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
  619. bcom_enable(priv->rx_dmatsk);
  620. bcom_enable(priv->tx_dmatsk);
  621. mpc52xx_fec_start(dev);
  622. netif_wake_queue(dev);
  623. }
  624. /* ethtool interface */
  625. static int mpc52xx_fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  626. {
  627. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  628. if (!priv->phydev)
  629. return -ENODEV;
  630. return phy_ethtool_gset(priv->phydev, cmd);
  631. }
  632. static int mpc52xx_fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  633. {
  634. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  635. if (!priv->phydev)
  636. return -ENODEV;
  637. return phy_ethtool_sset(priv->phydev, cmd);
  638. }
  639. static u32 mpc52xx_fec_get_msglevel(struct net_device *dev)
  640. {
  641. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  642. return priv->msg_enable;
  643. }
  644. static void mpc52xx_fec_set_msglevel(struct net_device *dev, u32 level)
  645. {
  646. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  647. priv->msg_enable = level;
  648. }
  649. static const struct ethtool_ops mpc52xx_fec_ethtool_ops = {
  650. .get_settings = mpc52xx_fec_get_settings,
  651. .set_settings = mpc52xx_fec_set_settings,
  652. .get_link = ethtool_op_get_link,
  653. .get_msglevel = mpc52xx_fec_get_msglevel,
  654. .set_msglevel = mpc52xx_fec_set_msglevel,
  655. };
  656. static int mpc52xx_fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  657. {
  658. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  659. if (!priv->phydev)
  660. return -ENOTSUPP;
  661. return phy_mii_ioctl(priv->phydev, rq, cmd);
  662. }
  663. static const struct net_device_ops mpc52xx_fec_netdev_ops = {
  664. .ndo_open = mpc52xx_fec_open,
  665. .ndo_stop = mpc52xx_fec_close,
  666. .ndo_start_xmit = mpc52xx_fec_start_xmit,
  667. .ndo_set_rx_mode = mpc52xx_fec_set_multicast_list,
  668. .ndo_set_mac_address = mpc52xx_fec_set_mac_address,
  669. .ndo_validate_addr = eth_validate_addr,
  670. .ndo_do_ioctl = mpc52xx_fec_ioctl,
  671. .ndo_change_mtu = eth_change_mtu,
  672. .ndo_tx_timeout = mpc52xx_fec_tx_timeout,
  673. .ndo_get_stats = mpc52xx_fec_get_stats,
  674. #ifdef CONFIG_NET_POLL_CONTROLLER
  675. .ndo_poll_controller = mpc52xx_fec_poll_controller,
  676. #endif
  677. };
  678. /* ======================================================================== */
  679. /* OF Driver */
  680. /* ======================================================================== */
  681. static int __devinit mpc52xx_fec_probe(struct platform_device *op)
  682. {
  683. int rv;
  684. struct net_device *ndev;
  685. struct mpc52xx_fec_priv *priv = NULL;
  686. struct resource mem;
  687. const u32 *prop;
  688. int prop_size;
  689. phys_addr_t rx_fifo;
  690. phys_addr_t tx_fifo;
  691. /* Get the ether ndev & it's private zone */
  692. ndev = alloc_etherdev(sizeof(struct mpc52xx_fec_priv));
  693. if (!ndev)
  694. return -ENOMEM;
  695. priv = netdev_priv(ndev);
  696. priv->ndev = ndev;
  697. /* Reserve FEC control zone */
  698. rv = of_address_to_resource(op->dev.of_node, 0, &mem);
  699. if (rv) {
  700. printk(KERN_ERR DRIVER_NAME ": "
  701. "Error while parsing device node resource\n" );
  702. goto err_netdev;
  703. }
  704. if (resource_size(&mem) < sizeof(struct mpc52xx_fec)) {
  705. printk(KERN_ERR DRIVER_NAME
  706. " - invalid resource size (%lx < %x), check mpc52xx_devices.c\n",
  707. (unsigned long)resource_size(&mem),
  708. sizeof(struct mpc52xx_fec));
  709. rv = -EINVAL;
  710. goto err_netdev;
  711. }
  712. if (!request_mem_region(mem.start, sizeof(struct mpc52xx_fec),
  713. DRIVER_NAME)) {
  714. rv = -EBUSY;
  715. goto err_netdev;
  716. }
  717. /* Init ether ndev with what we have */
  718. ndev->netdev_ops = &mpc52xx_fec_netdev_ops;
  719. ndev->ethtool_ops = &mpc52xx_fec_ethtool_ops;
  720. ndev->watchdog_timeo = FEC_WATCHDOG_TIMEOUT;
  721. ndev->base_addr = mem.start;
  722. SET_NETDEV_DEV(ndev, &op->dev);
  723. spin_lock_init(&priv->lock);
  724. /* ioremap the zones */
  725. priv->fec = ioremap(mem.start, sizeof(struct mpc52xx_fec));
  726. if (!priv->fec) {
  727. rv = -ENOMEM;
  728. goto err_mem_region;
  729. }
  730. /* Bestcomm init */
  731. rx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, rfifo_data);
  732. tx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, tfifo_data);
  733. priv->rx_dmatsk = bcom_fec_rx_init(FEC_RX_NUM_BD, rx_fifo, FEC_RX_BUFFER_SIZE);
  734. priv->tx_dmatsk = bcom_fec_tx_init(FEC_TX_NUM_BD, tx_fifo);
  735. if (!priv->rx_dmatsk || !priv->tx_dmatsk) {
  736. printk(KERN_ERR DRIVER_NAME ": Can not init SDMA tasks\n" );
  737. rv = -ENOMEM;
  738. goto err_rx_tx_dmatsk;
  739. }
  740. /* Get the IRQ we need one by one */
  741. /* Control */
  742. ndev->irq = irq_of_parse_and_map(op->dev.of_node, 0);
  743. /* RX */
  744. priv->r_irq = bcom_get_task_irq(priv->rx_dmatsk);
  745. /* TX */
  746. priv->t_irq = bcom_get_task_irq(priv->tx_dmatsk);
  747. /* MAC address init */
  748. if (!is_zero_ether_addr(mpc52xx_fec_mac_addr))
  749. memcpy(ndev->dev_addr, mpc52xx_fec_mac_addr, 6);
  750. else
  751. mpc52xx_fec_get_paddr(ndev, ndev->dev_addr);
  752. priv->msg_enable = netif_msg_init(debug, MPC52xx_MESSAGES_DEFAULT);
  753. /*
  754. * Link mode configuration
  755. */
  756. /* Start with safe defaults for link connection */
  757. priv->speed = 100;
  758. priv->duplex = DUPLEX_HALF;
  759. priv->mdio_speed = ((mpc5xxx_get_bus_frequency(op->dev.of_node) >> 20) / 5) << 1;
  760. /* The current speed preconfigures the speed of the MII link */
  761. prop = of_get_property(op->dev.of_node, "current-speed", &prop_size);
  762. if (prop && (prop_size >= sizeof(u32) * 2)) {
  763. priv->speed = prop[0];
  764. priv->duplex = prop[1] ? DUPLEX_FULL : DUPLEX_HALF;
  765. }
  766. /* If there is a phy handle, then get the PHY node */
  767. priv->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
  768. /* the 7-wire property means don't use MII mode */
  769. if (of_find_property(op->dev.of_node, "fsl,7-wire-mode", NULL)) {
  770. priv->seven_wire_mode = 1;
  771. dev_info(&ndev->dev, "using 7-wire PHY mode\n");
  772. }
  773. /* Hardware init */
  774. mpc52xx_fec_hw_init(ndev);
  775. mpc52xx_fec_reset_stats(ndev);
  776. rv = register_netdev(ndev);
  777. if (rv < 0)
  778. goto err_node;
  779. /* We're done ! */
  780. dev_set_drvdata(&op->dev, ndev);
  781. return 0;
  782. err_node:
  783. of_node_put(priv->phy_node);
  784. irq_dispose_mapping(ndev->irq);
  785. err_rx_tx_dmatsk:
  786. if (priv->rx_dmatsk)
  787. bcom_fec_rx_release(priv->rx_dmatsk);
  788. if (priv->tx_dmatsk)
  789. bcom_fec_tx_release(priv->tx_dmatsk);
  790. iounmap(priv->fec);
  791. err_mem_region:
  792. release_mem_region(mem.start, sizeof(struct mpc52xx_fec));
  793. err_netdev:
  794. free_netdev(ndev);
  795. return rv;
  796. }
  797. static int
  798. mpc52xx_fec_remove(struct platform_device *op)
  799. {
  800. struct net_device *ndev;
  801. struct mpc52xx_fec_priv *priv;
  802. ndev = dev_get_drvdata(&op->dev);
  803. priv = netdev_priv(ndev);
  804. unregister_netdev(ndev);
  805. if (priv->phy_node)
  806. of_node_put(priv->phy_node);
  807. priv->phy_node = NULL;
  808. irq_dispose_mapping(ndev->irq);
  809. bcom_fec_rx_release(priv->rx_dmatsk);
  810. bcom_fec_tx_release(priv->tx_dmatsk);
  811. iounmap(priv->fec);
  812. release_mem_region(ndev->base_addr, sizeof(struct mpc52xx_fec));
  813. free_netdev(ndev);
  814. dev_set_drvdata(&op->dev, NULL);
  815. return 0;
  816. }
  817. #ifdef CONFIG_PM
  818. static int mpc52xx_fec_of_suspend(struct platform_device *op, pm_message_t state)
  819. {
  820. struct net_device *dev = dev_get_drvdata(&op->dev);
  821. if (netif_running(dev))
  822. mpc52xx_fec_close(dev);
  823. return 0;
  824. }
  825. static int mpc52xx_fec_of_resume(struct platform_device *op)
  826. {
  827. struct net_device *dev = dev_get_drvdata(&op->dev);
  828. mpc52xx_fec_hw_init(dev);
  829. mpc52xx_fec_reset_stats(dev);
  830. if (netif_running(dev))
  831. mpc52xx_fec_open(dev);
  832. return 0;
  833. }
  834. #endif
  835. static struct of_device_id mpc52xx_fec_match[] = {
  836. { .compatible = "fsl,mpc5200b-fec", },
  837. { .compatible = "fsl,mpc5200-fec", },
  838. { .compatible = "mpc5200-fec", },
  839. { }
  840. };
  841. MODULE_DEVICE_TABLE(of, mpc52xx_fec_match);
  842. static struct platform_driver mpc52xx_fec_driver = {
  843. .driver = {
  844. .name = DRIVER_NAME,
  845. .owner = THIS_MODULE,
  846. .of_match_table = mpc52xx_fec_match,
  847. },
  848. .probe = mpc52xx_fec_probe,
  849. .remove = mpc52xx_fec_remove,
  850. #ifdef CONFIG_PM
  851. .suspend = mpc52xx_fec_of_suspend,
  852. .resume = mpc52xx_fec_of_resume,
  853. #endif
  854. };
  855. /* ======================================================================== */
  856. /* Module */
  857. /* ======================================================================== */
  858. static int __init
  859. mpc52xx_fec_init(void)
  860. {
  861. #ifdef CONFIG_FEC_MPC52xx_MDIO
  862. int ret;
  863. ret = platform_driver_register(&mpc52xx_fec_mdio_driver);
  864. if (ret) {
  865. printk(KERN_ERR DRIVER_NAME ": failed to register mdio driver\n");
  866. return ret;
  867. }
  868. #endif
  869. return platform_driver_register(&mpc52xx_fec_driver);
  870. }
  871. static void __exit
  872. mpc52xx_fec_exit(void)
  873. {
  874. platform_driver_unregister(&mpc52xx_fec_driver);
  875. #ifdef CONFIG_FEC_MPC52xx_MDIO
  876. platform_driver_unregister(&mpc52xx_fec_mdio_driver);
  877. #endif
  878. }
  879. module_init(mpc52xx_fec_init);
  880. module_exit(mpc52xx_fec_exit);
  881. MODULE_LICENSE("GPL");
  882. MODULE_AUTHOR("Dale Farnsworth");
  883. MODULE_DESCRIPTION("Ethernet driver for the Freescale MPC52xx FEC");