fec_mpc52xx.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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, PHY_ID_FMT,
  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. if (ievent & ~(FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {
  379. if (ievent & ~FEC_IEVENT_TFINT)
  380. dev_dbg(&dev->dev, "ievent: %08x\n", ievent);
  381. return IRQ_HANDLED;
  382. }
  383. if (net_ratelimit() && (ievent & FEC_IEVENT_RFIFO_ERROR))
  384. dev_warn(&dev->dev, "FEC_IEVENT_RFIFO_ERROR\n");
  385. if (net_ratelimit() && (ievent & FEC_IEVENT_XFIFO_ERROR))
  386. dev_warn(&dev->dev, "FEC_IEVENT_XFIFO_ERROR\n");
  387. mpc52xx_fec_reset(dev);
  388. netif_wake_queue(dev);
  389. return IRQ_HANDLED;
  390. }
  391. /*
  392. * Get the current statistics.
  393. * This may be called with the card open or closed.
  394. */
  395. static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev)
  396. {
  397. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  398. struct net_device_stats *stats = &dev->stats;
  399. struct mpc52xx_fec __iomem *fec = priv->fec;
  400. stats->rx_bytes = in_be32(&fec->rmon_r_octets);
  401. stats->rx_packets = in_be32(&fec->rmon_r_packets);
  402. stats->rx_errors = in_be32(&fec->rmon_r_crc_align) +
  403. in_be32(&fec->rmon_r_undersize) +
  404. in_be32(&fec->rmon_r_oversize) +
  405. in_be32(&fec->rmon_r_frag) +
  406. in_be32(&fec->rmon_r_jab);
  407. stats->tx_bytes = in_be32(&fec->rmon_t_octets);
  408. stats->tx_packets = in_be32(&fec->rmon_t_packets);
  409. stats->tx_errors = in_be32(&fec->rmon_t_crc_align) +
  410. in_be32(&fec->rmon_t_undersize) +
  411. in_be32(&fec->rmon_t_oversize) +
  412. in_be32(&fec->rmon_t_frag) +
  413. in_be32(&fec->rmon_t_jab);
  414. stats->multicast = in_be32(&fec->rmon_r_mc_pkt);
  415. stats->collisions = in_be32(&fec->rmon_t_col);
  416. /* detailed rx_errors: */
  417. stats->rx_length_errors = in_be32(&fec->rmon_r_undersize)
  418. + in_be32(&fec->rmon_r_oversize)
  419. + in_be32(&fec->rmon_r_frag)
  420. + in_be32(&fec->rmon_r_jab);
  421. stats->rx_over_errors = in_be32(&fec->r_macerr);
  422. stats->rx_crc_errors = in_be32(&fec->ieee_r_crc);
  423. stats->rx_frame_errors = in_be32(&fec->ieee_r_align);
  424. stats->rx_fifo_errors = in_be32(&fec->rmon_r_drop);
  425. stats->rx_missed_errors = in_be32(&fec->rmon_r_drop);
  426. /* detailed tx_errors: */
  427. stats->tx_aborted_errors = 0;
  428. stats->tx_carrier_errors = in_be32(&fec->ieee_t_cserr);
  429. stats->tx_fifo_errors = in_be32(&fec->rmon_t_drop);
  430. stats->tx_heartbeat_errors = in_be32(&fec->ieee_t_sqe);
  431. stats->tx_window_errors = in_be32(&fec->ieee_t_lcol);
  432. return stats;
  433. }
  434. /*
  435. * Read MIB counters in order to reset them,
  436. * then zero all the stats fields in memory
  437. */
  438. static void mpc52xx_fec_reset_stats(struct net_device *dev)
  439. {
  440. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  441. struct mpc52xx_fec __iomem *fec = priv->fec;
  442. out_be32(&fec->mib_control, FEC_MIB_DISABLE);
  443. memset_io(&fec->rmon_t_drop, 0,
  444. offsetof(struct mpc52xx_fec, reserved10) -
  445. offsetof(struct mpc52xx_fec, rmon_t_drop));
  446. out_be32(&fec->mib_control, 0);
  447. memset(&dev->stats, 0, sizeof(dev->stats));
  448. }
  449. /*
  450. * Set or clear the multicast filter for this adaptor.
  451. */
  452. static void mpc52xx_fec_set_multicast_list(struct net_device *dev)
  453. {
  454. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  455. struct mpc52xx_fec __iomem *fec = priv->fec;
  456. u32 rx_control;
  457. rx_control = in_be32(&fec->r_cntrl);
  458. if (dev->flags & IFF_PROMISC) {
  459. rx_control |= FEC_RCNTRL_PROM;
  460. out_be32(&fec->r_cntrl, rx_control);
  461. } else {
  462. rx_control &= ~FEC_RCNTRL_PROM;
  463. out_be32(&fec->r_cntrl, rx_control);
  464. if (dev->flags & IFF_ALLMULTI) {
  465. out_be32(&fec->gaddr1, 0xffffffff);
  466. out_be32(&fec->gaddr2, 0xffffffff);
  467. } else {
  468. u32 crc;
  469. int i;
  470. struct dev_mc_list *dmi;
  471. u32 gaddr1 = 0x00000000;
  472. u32 gaddr2 = 0x00000000;
  473. dmi = dev->mc_list;
  474. for (i=0; i<dev->mc_count; i++) {
  475. crc = ether_crc_le(6, dmi->dmi_addr) >> 26;
  476. if (crc >= 32)
  477. gaddr1 |= 1 << (crc-32);
  478. else
  479. gaddr2 |= 1 << crc;
  480. dmi = dmi->next;
  481. }
  482. out_be32(&fec->gaddr1, gaddr1);
  483. out_be32(&fec->gaddr2, gaddr2);
  484. }
  485. }
  486. }
  487. /**
  488. * mpc52xx_fec_hw_init
  489. * @dev: network device
  490. *
  491. * Setup various hardware setting, only needed once on start
  492. */
  493. static void mpc52xx_fec_hw_init(struct net_device *dev)
  494. {
  495. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  496. struct mpc52xx_fec __iomem *fec = priv->fec;
  497. int i;
  498. /* Whack a reset. We should wait for this. */
  499. out_be32(&fec->ecntrl, FEC_ECNTRL_RESET);
  500. for (i = 0; i < FEC_RESET_DELAY; ++i) {
  501. if ((in_be32(&fec->ecntrl) & FEC_ECNTRL_RESET) == 0)
  502. break;
  503. udelay(1);
  504. }
  505. if (i == FEC_RESET_DELAY)
  506. dev_err(&dev->dev, "FEC Reset timeout!\n");
  507. /* set pause to 0x20 frames */
  508. out_be32(&fec->op_pause, FEC_OP_PAUSE_OPCODE | 0x20);
  509. /* high service request will be deasserted when there's < 7 bytes in fifo
  510. * low service request will be deasserted when there's < 4*7 bytes in fifo
  511. */
  512. out_be32(&fec->rfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
  513. out_be32(&fec->tfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
  514. /* alarm when <= x bytes in FIFO */
  515. out_be32(&fec->rfifo_alarm, 0x0000030c);
  516. out_be32(&fec->tfifo_alarm, 0x00000100);
  517. /* begin transmittion when 256 bytes are in FIFO (or EOF or FIFO full) */
  518. out_be32(&fec->x_wmrk, FEC_FIFO_WMRK_256B);
  519. /* enable crc generation */
  520. out_be32(&fec->xmit_fsm, FEC_XMIT_FSM_APPEND_CRC | FEC_XMIT_FSM_ENABLE_CRC);
  521. out_be32(&fec->iaddr1, 0x00000000); /* No individual filter */
  522. out_be32(&fec->iaddr2, 0x00000000); /* No individual filter */
  523. /* set phy speed.
  524. * this can't be done in phy driver, since it needs to be called
  525. * before fec stuff (even on resume) */
  526. mpc52xx_fec_phy_hw_init(priv);
  527. }
  528. /**
  529. * mpc52xx_fec_start
  530. * @dev: network device
  531. *
  532. * This function is called to start or restart the FEC during a link
  533. * change. This happens on fifo errors or when switching between half
  534. * and full duplex.
  535. */
  536. static void mpc52xx_fec_start(struct net_device *dev)
  537. {
  538. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  539. struct mpc52xx_fec __iomem *fec = priv->fec;
  540. u32 rcntrl;
  541. u32 tcntrl;
  542. u32 tmp;
  543. /* clear sticky error bits */
  544. tmp = FEC_FIFO_STATUS_ERR | FEC_FIFO_STATUS_UF | FEC_FIFO_STATUS_OF;
  545. out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status) & tmp);
  546. out_be32(&fec->tfifo_status, in_be32(&fec->tfifo_status) & tmp);
  547. /* FIFOs will reset on mpc52xx_fec_enable */
  548. out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_ENABLE_IS_RESET);
  549. /* Set station address. */
  550. mpc52xx_fec_set_paddr(dev, dev->dev_addr);
  551. mpc52xx_fec_set_multicast_list(dev);
  552. /* set max frame len, enable flow control, select mii mode */
  553. rcntrl = FEC_RX_BUFFER_SIZE << 16; /* max frame length */
  554. rcntrl |= FEC_RCNTRL_FCE;
  555. if (priv->has_phy)
  556. rcntrl |= FEC_RCNTRL_MII_MODE;
  557. if (priv->duplex == DUPLEX_FULL)
  558. tcntrl = FEC_TCNTRL_FDEN; /* FD enable */
  559. else {
  560. rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
  561. tcntrl = 0;
  562. }
  563. out_be32(&fec->r_cntrl, rcntrl);
  564. out_be32(&fec->x_cntrl, tcntrl);
  565. /* Clear any outstanding interrupt. */
  566. out_be32(&fec->ievent, 0xffffffff);
  567. /* Enable interrupts we wish to service. */
  568. out_be32(&fec->imask, FEC_IMASK_ENABLE);
  569. /* And last, enable the transmit and receive processing. */
  570. out_be32(&fec->ecntrl, FEC_ECNTRL_ETHER_EN);
  571. out_be32(&fec->r_des_active, 0x01000000);
  572. }
  573. /**
  574. * mpc52xx_fec_stop
  575. * @dev: network device
  576. *
  577. * stop all activity on fec and empty dma buffers
  578. */
  579. static void mpc52xx_fec_stop(struct net_device *dev)
  580. {
  581. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  582. struct mpc52xx_fec __iomem *fec = priv->fec;
  583. unsigned long timeout;
  584. /* disable all interrupts */
  585. out_be32(&fec->imask, 0);
  586. /* Disable the rx task. */
  587. bcom_disable(priv->rx_dmatsk);
  588. /* Wait for tx queue to drain, but only if we're in process context */
  589. if (!in_interrupt()) {
  590. timeout = jiffies + msecs_to_jiffies(2000);
  591. while (time_before(jiffies, timeout) &&
  592. !bcom_queue_empty(priv->tx_dmatsk))
  593. msleep(100);
  594. if (time_after_eq(jiffies, timeout))
  595. dev_err(&dev->dev, "queues didn't drain\n");
  596. #if 1
  597. if (time_after_eq(jiffies, timeout)) {
  598. dev_err(&dev->dev, " tx: index: %i, outdex: %i\n",
  599. priv->tx_dmatsk->index,
  600. priv->tx_dmatsk->outdex);
  601. dev_err(&dev->dev, " rx: index: %i, outdex: %i\n",
  602. priv->rx_dmatsk->index,
  603. priv->rx_dmatsk->outdex);
  604. }
  605. #endif
  606. }
  607. bcom_disable(priv->tx_dmatsk);
  608. /* Stop FEC */
  609. out_be32(&fec->ecntrl, in_be32(&fec->ecntrl) & ~FEC_ECNTRL_ETHER_EN);
  610. return;
  611. }
  612. /* reset fec and bestcomm tasks */
  613. static void mpc52xx_fec_reset(struct net_device *dev)
  614. {
  615. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  616. struct mpc52xx_fec __iomem *fec = priv->fec;
  617. mpc52xx_fec_stop(dev);
  618. out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status));
  619. out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_RESET_FIFO);
  620. mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
  621. mpc52xx_fec_hw_init(dev);
  622. phy_stop(priv->phydev);
  623. phy_write(priv->phydev, MII_BMCR, BMCR_RESET);
  624. phy_start(priv->phydev);
  625. bcom_fec_rx_reset(priv->rx_dmatsk);
  626. bcom_fec_tx_reset(priv->tx_dmatsk);
  627. mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
  628. bcom_enable(priv->rx_dmatsk);
  629. bcom_enable(priv->tx_dmatsk);
  630. mpc52xx_fec_start(dev);
  631. }
  632. /* ethtool interface */
  633. static void mpc52xx_fec_get_drvinfo(struct net_device *dev,
  634. struct ethtool_drvinfo *info)
  635. {
  636. strcpy(info->driver, DRIVER_NAME);
  637. }
  638. static int mpc52xx_fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  639. {
  640. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  641. return phy_ethtool_gset(priv->phydev, cmd);
  642. }
  643. static int mpc52xx_fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  644. {
  645. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  646. return phy_ethtool_sset(priv->phydev, cmd);
  647. }
  648. static u32 mpc52xx_fec_get_msglevel(struct net_device *dev)
  649. {
  650. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  651. return priv->msg_enable;
  652. }
  653. static void mpc52xx_fec_set_msglevel(struct net_device *dev, u32 level)
  654. {
  655. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  656. priv->msg_enable = level;
  657. }
  658. static const struct ethtool_ops mpc52xx_fec_ethtool_ops = {
  659. .get_drvinfo = mpc52xx_fec_get_drvinfo,
  660. .get_settings = mpc52xx_fec_get_settings,
  661. .set_settings = mpc52xx_fec_set_settings,
  662. .get_link = ethtool_op_get_link,
  663. .get_msglevel = mpc52xx_fec_get_msglevel,
  664. .set_msglevel = mpc52xx_fec_set_msglevel,
  665. };
  666. static int mpc52xx_fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  667. {
  668. struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  669. return mpc52xx_fec_phy_mii_ioctl(priv, if_mii(rq), cmd);
  670. }
  671. /* ======================================================================== */
  672. /* OF Driver */
  673. /* ======================================================================== */
  674. static int __devinit
  675. mpc52xx_fec_probe(struct of_device *op, const struct of_device_id *match)
  676. {
  677. int rv;
  678. struct net_device *ndev;
  679. struct mpc52xx_fec_priv *priv = NULL;
  680. struct resource mem;
  681. const phandle *ph;
  682. phys_addr_t rx_fifo;
  683. phys_addr_t tx_fifo;
  684. /* Get the ether ndev & it's private zone */
  685. ndev = alloc_etherdev(sizeof(struct mpc52xx_fec_priv));
  686. if (!ndev)
  687. return -ENOMEM;
  688. priv = netdev_priv(ndev);
  689. /* Reserve FEC control zone */
  690. rv = of_address_to_resource(op->node, 0, &mem);
  691. if (rv) {
  692. printk(KERN_ERR DRIVER_NAME ": "
  693. "Error while parsing device node resource\n" );
  694. return rv;
  695. }
  696. if ((mem.end - mem.start + 1) < sizeof(struct mpc52xx_fec)) {
  697. printk(KERN_ERR DRIVER_NAME
  698. " - invalid resource size (%lx < %x), check mpc52xx_devices.c\n",
  699. (unsigned long)(mem.end - mem.start + 1), sizeof(struct mpc52xx_fec));
  700. return -EINVAL;
  701. }
  702. if (!request_mem_region(mem.start, sizeof(struct mpc52xx_fec), DRIVER_NAME))
  703. return -EBUSY;
  704. /* Init ether ndev with what we have */
  705. ndev->open = mpc52xx_fec_open;
  706. ndev->stop = mpc52xx_fec_close;
  707. ndev->hard_start_xmit = mpc52xx_fec_hard_start_xmit;
  708. ndev->do_ioctl = mpc52xx_fec_ioctl;
  709. ndev->ethtool_ops = &mpc52xx_fec_ethtool_ops;
  710. ndev->get_stats = mpc52xx_fec_get_stats;
  711. ndev->set_mac_address = mpc52xx_fec_set_mac_address;
  712. ndev->set_multicast_list = mpc52xx_fec_set_multicast_list;
  713. ndev->tx_timeout = mpc52xx_fec_tx_timeout;
  714. ndev->watchdog_timeo = FEC_WATCHDOG_TIMEOUT;
  715. ndev->base_addr = mem.start;
  716. priv->t_irq = priv->r_irq = ndev->irq = NO_IRQ; /* IRQ are free for now */
  717. spin_lock_init(&priv->lock);
  718. /* ioremap the zones */
  719. priv->fec = ioremap(mem.start, sizeof(struct mpc52xx_fec));
  720. if (!priv->fec) {
  721. rv = -ENOMEM;
  722. goto probe_error;
  723. }
  724. /* Bestcomm init */
  725. rx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, rfifo_data);
  726. tx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, tfifo_data);
  727. priv->rx_dmatsk = bcom_fec_rx_init(FEC_RX_NUM_BD, rx_fifo, FEC_RX_BUFFER_SIZE);
  728. priv->tx_dmatsk = bcom_fec_tx_init(FEC_TX_NUM_BD, tx_fifo);
  729. if (!priv->rx_dmatsk || !priv->tx_dmatsk) {
  730. printk(KERN_ERR DRIVER_NAME ": Can not init SDMA tasks\n" );
  731. rv = -ENOMEM;
  732. goto probe_error;
  733. }
  734. /* Get the IRQ we need one by one */
  735. /* Control */
  736. ndev->irq = irq_of_parse_and_map(op->node, 0);
  737. /* RX */
  738. priv->r_irq = bcom_get_task_irq(priv->rx_dmatsk);
  739. /* TX */
  740. priv->t_irq = bcom_get_task_irq(priv->tx_dmatsk);
  741. /* MAC address init */
  742. if (!is_zero_ether_addr(mpc52xx_fec_mac_addr))
  743. memcpy(ndev->dev_addr, mpc52xx_fec_mac_addr, 6);
  744. else
  745. mpc52xx_fec_get_paddr(ndev, ndev->dev_addr);
  746. priv->msg_enable = netif_msg_init(debug, MPC52xx_MESSAGES_DEFAULT);
  747. priv->duplex = DUPLEX_FULL;
  748. /* is the phy present in device tree? */
  749. ph = of_get_property(op->node, "phy-handle", NULL);
  750. if (ph) {
  751. const unsigned int *prop;
  752. struct device_node *phy_dn;
  753. priv->has_phy = 1;
  754. phy_dn = of_find_node_by_phandle(*ph);
  755. prop = of_get_property(phy_dn, "reg", NULL);
  756. priv->phy_addr = *prop;
  757. of_node_put(phy_dn);
  758. /* Phy speed */
  759. priv->phy_speed = ((mpc52xx_find_ipb_freq(op->node) >> 20) / 5) << 1;
  760. } else {
  761. dev_info(&ndev->dev, "can't find \"phy-handle\" in device"
  762. " tree, using 7-wire mode\n");
  763. }
  764. /* Hardware init */
  765. mpc52xx_fec_hw_init(ndev);
  766. mpc52xx_fec_reset_stats(ndev);
  767. SET_NETDEV_DEV(ndev, &op->dev);
  768. /* Register the new network device */
  769. rv = register_netdev(ndev);
  770. if (rv < 0)
  771. goto probe_error;
  772. /* We're done ! */
  773. dev_set_drvdata(&op->dev, ndev);
  774. return 0;
  775. /* Error handling - free everything that might be allocated */
  776. probe_error:
  777. irq_dispose_mapping(ndev->irq);
  778. if (priv->rx_dmatsk)
  779. bcom_fec_rx_release(priv->rx_dmatsk);
  780. if (priv->tx_dmatsk)
  781. bcom_fec_tx_release(priv->tx_dmatsk);
  782. if (priv->fec)
  783. iounmap(priv->fec);
  784. release_mem_region(mem.start, sizeof(struct mpc52xx_fec));
  785. free_netdev(ndev);
  786. return rv;
  787. }
  788. static int
  789. mpc52xx_fec_remove(struct of_device *op)
  790. {
  791. struct net_device *ndev;
  792. struct mpc52xx_fec_priv *priv;
  793. ndev = dev_get_drvdata(&op->dev);
  794. priv = netdev_priv(ndev);
  795. unregister_netdev(ndev);
  796. irq_dispose_mapping(ndev->irq);
  797. bcom_fec_rx_release(priv->rx_dmatsk);
  798. bcom_fec_tx_release(priv->tx_dmatsk);
  799. iounmap(priv->fec);
  800. release_mem_region(ndev->base_addr, sizeof(struct mpc52xx_fec));
  801. free_netdev(ndev);
  802. dev_set_drvdata(&op->dev, NULL);
  803. return 0;
  804. }
  805. #ifdef CONFIG_PM
  806. static int mpc52xx_fec_of_suspend(struct of_device *op, pm_message_t state)
  807. {
  808. struct net_device *dev = dev_get_drvdata(&op->dev);
  809. if (netif_running(dev))
  810. mpc52xx_fec_close(dev);
  811. return 0;
  812. }
  813. static int mpc52xx_fec_of_resume(struct of_device *op)
  814. {
  815. struct net_device *dev = dev_get_drvdata(&op->dev);
  816. mpc52xx_fec_hw_init(dev);
  817. mpc52xx_fec_reset_stats(dev);
  818. if (netif_running(dev))
  819. mpc52xx_fec_open(dev);
  820. return 0;
  821. }
  822. #endif
  823. static struct of_device_id mpc52xx_fec_match[] = {
  824. {
  825. .type = "network",
  826. .compatible = "mpc5200-fec",
  827. },
  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");