fec_mpc52xx.c 28 KB

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