fec_mpc52xx.c 30 KB

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