⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fec_mpc52xx.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Driver for the MPC5200 Fast Ethernet Controller * * Originally written by Dale Farnsworth <dfarnsworth@mvista.com> and * now maintained by Sylvain Munaut <tnt@246tNt.com> * * Copyright (C) 2007  Domen Puncer, Telargo, Inc. * Copyright (C) 2007  Sylvain Munaut <tnt@246tNt.com> * Copyright (C) 2003-2004  MontaVista, Software, Inc. * * This file is licensed under the terms of the GNU General Public License * version 2. This program is licensed "as is" without any warranty of any * kind, whether express or implied. * */#include <linux/module.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/spinlock.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/crc32.h>#include <linux/hardirq.h>#include <linux/delay.h>#include <linux/of_device.h>#include <linux/of_platform.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/ethtool.h>#include <linux/skbuff.h>#include <asm/io.h>#include <asm/delay.h>#include <asm/mpc52xx.h>#include <sysdev/bestcomm/bestcomm.h>#include <sysdev/bestcomm/fec.h>#include "fec_mpc52xx.h"#define DRIVER_NAME "mpc52xx-fec"static irqreturn_t mpc52xx_fec_interrupt(int, void *);static irqreturn_t mpc52xx_fec_rx_interrupt(int, void *);static irqreturn_t mpc52xx_fec_tx_interrupt(int, void *);static void mpc52xx_fec_stop(struct net_device *dev);static void mpc52xx_fec_start(struct net_device *dev);static void mpc52xx_fec_reset(struct net_device *dev);static u8 mpc52xx_fec_mac_addr[6];module_param_array_named(mac, mpc52xx_fec_mac_addr, byte, NULL, 0);MODULE_PARM_DESC(mac, "six hex digits, ie. 0x1,0x2,0xc0,0x01,0xba,0xbe");#define MPC52xx_MESSAGES_DEFAULT ( NETIF_MSG_DRV | NETIF_MSG_PROBE | \		NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFDOWN )static int debug = -1;	/* the above default */module_param(debug, int, 0);MODULE_PARM_DESC(debug, "debugging messages level");static void mpc52xx_fec_tx_timeout(struct net_device *dev){	dev_warn(&dev->dev, "transmit timed out\n");	mpc52xx_fec_reset(dev);	dev->stats.tx_errors++;	netif_wake_queue(dev);}static void mpc52xx_fec_set_paddr(struct net_device *dev, u8 *mac){	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	struct mpc52xx_fec __iomem *fec = priv->fec;	out_be32(&fec->paddr1, *(u32 *)(&mac[0]));	out_be32(&fec->paddr2, (*(u16 *)(&mac[4]) << 16) | FEC_PADDR2_TYPE);}static void mpc52xx_fec_get_paddr(struct net_device *dev, u8 *mac){	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	struct mpc52xx_fec __iomem *fec = priv->fec;	*(u32 *)(&mac[0]) = in_be32(&fec->paddr1);	*(u16 *)(&mac[4]) = in_be32(&fec->paddr2) >> 16;}static int mpc52xx_fec_set_mac_address(struct net_device *dev, void *addr){	struct sockaddr *sock = addr;	memcpy(dev->dev_addr, sock->sa_data, dev->addr_len);	mpc52xx_fec_set_paddr(dev, sock->sa_data);	return 0;}static void mpc52xx_fec_free_rx_buffers(struct net_device *dev, struct bcom_task *s){	while (!bcom_queue_empty(s)) {		struct bcom_fec_bd *bd;		struct sk_buff *skb;		skb = bcom_retrieve_buffer(s, NULL, (struct bcom_bd **)&bd);		dma_unmap_single(&dev->dev, bd->skb_pa, skb->len, DMA_FROM_DEVICE);		kfree_skb(skb);	}}static int mpc52xx_fec_alloc_rx_buffers(struct net_device *dev, struct bcom_task *rxtsk){	while (!bcom_queue_full(rxtsk)) {		struct sk_buff *skb;		struct bcom_fec_bd *bd;		skb = dev_alloc_skb(FEC_RX_BUFFER_SIZE);		if (skb == NULL)			return -EAGAIN;		/* zero out the initial receive buffers to aid debugging */		memset(skb->data, 0, FEC_RX_BUFFER_SIZE);		bd = (struct bcom_fec_bd *)bcom_prepare_next_buffer(rxtsk);		bd->status = FEC_RX_BUFFER_SIZE;		bd->skb_pa = dma_map_single(&dev->dev, skb->data,				FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);		bcom_submit_next_buffer(rxtsk, skb);	}	return 0;}/* based on generic_adjust_link from fs_enet-main.c */static void mpc52xx_fec_adjust_link(struct net_device *dev){	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	struct phy_device *phydev = priv->phydev;	int new_state = 0;	if (phydev->link != PHY_DOWN) {		if (phydev->duplex != priv->duplex) {			struct mpc52xx_fec __iomem *fec = priv->fec;			u32 rcntrl;			u32 tcntrl;			new_state = 1;			priv->duplex = phydev->duplex;			rcntrl = in_be32(&fec->r_cntrl);			tcntrl = in_be32(&fec->x_cntrl);			rcntrl &= ~FEC_RCNTRL_DRT;			tcntrl &= ~FEC_TCNTRL_FDEN;			if (phydev->duplex == DUPLEX_FULL)				tcntrl |= FEC_TCNTRL_FDEN;	/* FD enable */			else				rcntrl |= FEC_RCNTRL_DRT;	/* disable Rx on Tx (HD) */			out_be32(&fec->r_cntrl, rcntrl);			out_be32(&fec->x_cntrl, tcntrl);		}		if (phydev->speed != priv->speed) {			new_state = 1;			priv->speed = phydev->speed;		}		if (priv->link == PHY_DOWN) {			new_state = 1;			priv->link = phydev->link;			netif_schedule(dev);			netif_carrier_on(dev);			netif_start_queue(dev);		}	} else if (priv->link) {		new_state = 1;		priv->link = PHY_DOWN;		priv->speed = 0;		priv->duplex = -1;		netif_stop_queue(dev);		netif_carrier_off(dev);	}	if (new_state && netif_msg_link(priv))		phy_print_status(phydev);}static int mpc52xx_fec_init_phy(struct net_device *dev){	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	struct phy_device *phydev;	char phy_id[BUS_ID_SIZE];	snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT,			(unsigned int)dev->base_addr, priv->phy_addr);	priv->link = PHY_DOWN;	priv->speed = 0;	priv->duplex = -1;	phydev = phy_connect(dev, phy_id, &mpc52xx_fec_adjust_link, 0, PHY_INTERFACE_MODE_MII);	if (IS_ERR(phydev)) {		dev_err(&dev->dev, "phy_connect failed\n");		return PTR_ERR(phydev);	}	dev_info(&dev->dev, "attached phy %i to driver %s\n",			phydev->addr, phydev->drv->name);	priv->phydev = phydev;	return 0;}static int mpc52xx_fec_phy_start(struct net_device *dev){	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	int err;	if (!priv->has_phy)		return 0;	err = mpc52xx_fec_init_phy(dev);	if (err) {		dev_err(&dev->dev, "mpc52xx_fec_init_phy failed\n");		return err;	}	/* reset phy - this also wakes it from PDOWN */	phy_write(priv->phydev, MII_BMCR, BMCR_RESET);	phy_start(priv->phydev);	return 0;}static void mpc52xx_fec_phy_stop(struct net_device *dev){	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	if (!priv->has_phy)		return;	phy_disconnect(priv->phydev);	/* power down phy */	phy_stop(priv->phydev);	phy_write(priv->phydev, MII_BMCR, BMCR_PDOWN);}static int mpc52xx_fec_phy_mii_ioctl(struct mpc52xx_fec_priv *priv,		struct mii_ioctl_data *mii_data, int cmd){	if (!priv->has_phy)		return -ENOTSUPP;	return phy_mii_ioctl(priv->phydev, mii_data, cmd);}static void mpc52xx_fec_phy_hw_init(struct mpc52xx_fec_priv *priv){	struct mpc52xx_fec __iomem *fec = priv->fec;	if (!priv->has_phy)		return;	out_be32(&fec->mii_speed, priv->phy_speed);}static int mpc52xx_fec_open(struct net_device *dev){	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	int err = -EBUSY;	if (request_irq(dev->irq, &mpc52xx_fec_interrupt, IRQF_SHARED,	                DRIVER_NAME "_ctrl", dev)) {		dev_err(&dev->dev, "ctrl interrupt request failed\n");		goto out;	}	if (request_irq(priv->r_irq, &mpc52xx_fec_rx_interrupt, 0,	                DRIVER_NAME "_rx", dev)) {		dev_err(&dev->dev, "rx interrupt request failed\n");		goto free_ctrl_irq;	}	if (request_irq(priv->t_irq, &mpc52xx_fec_tx_interrupt, 0,	                DRIVER_NAME "_tx", dev)) {		dev_err(&dev->dev, "tx interrupt request failed\n");		goto free_2irqs;	}	bcom_fec_rx_reset(priv->rx_dmatsk);	bcom_fec_tx_reset(priv->tx_dmatsk);	err = mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);	if (err) {		dev_err(&dev->dev, "mpc52xx_fec_alloc_rx_buffers failed\n");		goto free_irqs;	}	err = mpc52xx_fec_phy_start(dev);	if (err)		goto free_skbs;	bcom_enable(priv->rx_dmatsk);	bcom_enable(priv->tx_dmatsk);	mpc52xx_fec_start(dev);	netif_start_queue(dev);	return 0; free_skbs:	mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk); free_irqs:	free_irq(priv->t_irq, dev); free_2irqs:	free_irq(priv->r_irq, dev); free_ctrl_irq:	free_irq(dev->irq, dev); out:	return err;}static int mpc52xx_fec_close(struct net_device *dev){	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	netif_stop_queue(dev);	mpc52xx_fec_stop(dev);	mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);	free_irq(dev->irq, dev);	free_irq(priv->r_irq, dev);	free_irq(priv->t_irq, dev);	mpc52xx_fec_phy_stop(dev);	return 0;}/* This will only be invoked if your driver is _not_ in XOFF state. * What this means is that you need not check it, and that this * invariant will hold if you make sure that the netif_*_queue() * calls are done at the proper times. */static int mpc52xx_fec_hard_start_xmit(struct sk_buff *skb, struct net_device *dev){	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	struct bcom_fec_bd *bd;	if (bcom_queue_full(priv->tx_dmatsk)) {		if (net_ratelimit())			dev_err(&dev->dev, "transmit queue overrun\n");		return 1;	}	spin_lock_irq(&priv->lock);	dev->trans_start = jiffies;	bd = (struct bcom_fec_bd *)		bcom_prepare_next_buffer(priv->tx_dmatsk);	bd->status = skb->len | BCOM_FEC_TX_BD_TFD | BCOM_FEC_TX_BD_TC;	bd->skb_pa = dma_map_single(&dev->dev, skb->data, skb->len, DMA_TO_DEVICE);	bcom_submit_next_buffer(priv->tx_dmatsk, skb);	if (bcom_queue_full(priv->tx_dmatsk)) {		netif_stop_queue(dev);	}	spin_unlock_irq(&priv->lock);	return 0;}/* This handles BestComm transmit task interrupts */static irqreturn_t mpc52xx_fec_tx_interrupt(int irq, void *dev_id){	struct net_device *dev = dev_id;	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	spin_lock(&priv->lock);	while (bcom_buffer_done(priv->tx_dmatsk)) {		struct sk_buff *skb;		struct bcom_fec_bd *bd;		skb = bcom_retrieve_buffer(priv->tx_dmatsk, NULL,				(struct bcom_bd **)&bd);		dma_unmap_single(&dev->dev, bd->skb_pa, skb->len, DMA_TO_DEVICE);		dev_kfree_skb_irq(skb);	}	netif_wake_queue(dev);	spin_unlock(&priv->lock);	return IRQ_HANDLED;}static irqreturn_t mpc52xx_fec_rx_interrupt(int irq, void *dev_id){	struct net_device *dev = dev_id;	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	while (bcom_buffer_done(priv->rx_dmatsk)) {		struct sk_buff *skb;		struct sk_buff *rskb;		struct bcom_fec_bd *bd;		u32 status;		rskb = bcom_retrieve_buffer(priv->rx_dmatsk, &status,				(struct bcom_bd **)&bd);		dma_unmap_single(&dev->dev, bd->skb_pa, rskb->len, DMA_FROM_DEVICE);		/* Test for errors in received frame */		if (status & BCOM_FEC_RX_BD_ERRORS) {			/* Drop packet and reuse the buffer */			bd = (struct bcom_fec_bd *)				bcom_prepare_next_buffer(priv->rx_dmatsk);			bd->status = FEC_RX_BUFFER_SIZE;			bd->skb_pa = dma_map_single(&dev->dev, rskb->data,					FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);			bcom_submit_next_buffer(priv->rx_dmatsk, rskb);			dev->stats.rx_dropped++;			continue;		}		/* skbs are allocated on open, so now we allocate a new one,		 * and remove the old (with the packet) */		skb = dev_alloc_skb(FEC_RX_BUFFER_SIZE);		if (skb) {			/* Process the received skb */			int length = status & BCOM_FEC_RX_BD_LEN_MASK;			skb_put(rskb, length - 4);	/* length without CRC32 */			rskb->dev = dev;			rskb->protocol = eth_type_trans(rskb, dev);			netif_rx(rskb);			dev->last_rx = jiffies;		} else {			/* Can't get a new one : reuse the same & drop pkt */			dev_notice(&dev->dev, "Memory squeeze, dropping packet.\n");			dev->stats.rx_dropped++;			skb = rskb;		}		bd = (struct bcom_fec_bd *)			bcom_prepare_next_buffer(priv->rx_dmatsk);		bd->status = FEC_RX_BUFFER_SIZE;		bd->skb_pa = dma_map_single(&dev->dev, skb->data,				FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);		bcom_submit_next_buffer(priv->rx_dmatsk, skb);	}	return IRQ_HANDLED;}static irqreturn_t mpc52xx_fec_interrupt(int irq, void *dev_id){	struct net_device *dev = dev_id;	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	struct mpc52xx_fec __iomem *fec = priv->fec;	u32 ievent;	ievent = in_be32(&fec->ievent);	ievent &= ~FEC_IEVENT_MII;	/* mii is handled separately */	if (!ievent)		return IRQ_NONE;	out_be32(&fec->ievent, ievent);		/* clear pending events */	if (ievent & ~(FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {		if (ievent & ~FEC_IEVENT_TFINT)			dev_dbg(&dev->dev, "ievent: %08x\n", ievent);		return IRQ_HANDLED;	}	if (net_ratelimit() && (ievent & FEC_IEVENT_RFIFO_ERROR))		dev_warn(&dev->dev, "FEC_IEVENT_RFIFO_ERROR\n");	if (net_ratelimit() && (ievent & FEC_IEVENT_XFIFO_ERROR))		dev_warn(&dev->dev, "FEC_IEVENT_XFIFO_ERROR\n");	mpc52xx_fec_reset(dev);	netif_wake_queue(dev);	return IRQ_HANDLED;}/* * Get the current statistics. * This may be called with the card open or closed. */static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev){	struct mpc52xx_fec_priv *priv = netdev_priv(dev);	struct net_device_stats *stats = &dev->stats;	struct mpc52xx_fec __iomem *fec = priv->fec;	stats->rx_bytes = in_be32(&fec->rmon_r_octets);	stats->rx_packets = in_be32(&fec->rmon_r_packets);	stats->rx_errors = in_be32(&fec->rmon_r_crc_align) +		in_be32(&fec->rmon_r_undersize) +		in_be32(&fec->rmon_r_oversize) +		in_be32(&fec->rmon_r_frag) +		in_be32(&fec->rmon_r_jab);	stats->tx_bytes = in_be32(&fec->rmon_t_octets);	stats->tx_packets = in_be32(&fec->rmon_t_packets);	stats->tx_errors = in_be32(&fec->rmon_t_crc_align) +		in_be32(&fec->rmon_t_undersize) +		in_be32(&fec->rmon_t_oversize) +		in_be32(&fec->rmon_t_frag) +		in_be32(&fec->rmon_t_jab);	stats->multicast = in_be32(&fec->rmon_r_mc_pkt);	stats->collisions = in_be32(&fec->rmon_t_col);	/* detailed rx_errors: */	stats->rx_length_errors = in_be32(&fec->rmon_r_undersize)					+ in_be32(&fec->rmon_r_oversize)					+ in_be32(&fec->rmon_r_frag)					+ in_be32(&fec->rmon_r_jab);	stats->rx_over_errors = in_be32(&fec->r_macerr);	stats->rx_crc_errors = in_be32(&fec->ieee_r_crc);	stats->rx_frame_errors = in_be32(&fec->ieee_r_align);	stats->rx_fifo_errors = in_be32(&fec->rmon_r_drop);	stats->rx_missed_errors = in_be32(&fec->rmon_r_drop);	/* detailed tx_errors: */	stats->tx_aborted_errors = 0;	stats->tx_carrier_errors = in_be32(&fec->ieee_t_cserr);	stats->tx_fifo_errors = in_be32(&fec->rmon_t_drop);	stats->tx_heartbeat_errors = in_be32(&fec->ieee_t_sqe);	stats->tx_window_errors = in_be32(&fec->ieee_t_lcol);	return stats;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -