at91_ether.c

来自「LINUX 2.6.17.4的源码」· C语言 代码 · 共 1,111 行 · 第 1/3 页

C
1,111
字号
/* * Ethernet driver for the Atmel AT91RM9200 (Thunder) * *  Copyright (C) 2003 SAN People (Pty) Ltd * * Based on an earlier Atmel EMAC macrocell driver by Atmel and Lineo Inc. * Initial version by Rick Bronson 01/11/2003 * * Intel LXT971A PHY support by Christopher Bahns & David Knickerbocker *   (Polaroid Corporation) * * Realtek RTL8201(B)L PHY support by Roman Avramenko <roman@imsystems.ru> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */#include <linux/module.h>#include <linux/init.h>#include <linux/config.h>#include <linux/mii.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <linux/dma-mapping.h>#include <linux/ethtool.h>#include <linux/platform_device.h>#include <linux/clk.h>#include <asm/io.h>#include <asm/uaccess.h>#include <asm/mach-types.h>#include <asm/arch/at91rm9200_emac.h>#include <asm/arch/gpio.h>#include <asm/arch/board.h>#include "at91_ether.h"#define DRV_NAME	"at91_ether"#define DRV_VERSION	"1.0"static struct net_device *at91_dev;static struct clk *ether_clk;/* ..................................................................... *//* * Read from a EMAC register. */static inline unsigned long at91_emac_read(unsigned int reg){	void __iomem *emac_base = (void __iomem *)AT91_VA_BASE_EMAC;	return __raw_readl(emac_base + reg);}/* * Write to a EMAC register. */static inline void at91_emac_write(unsigned int reg, unsigned long value){	void __iomem *emac_base = (void __iomem *)AT91_VA_BASE_EMAC;	__raw_writel(value, emac_base + reg);}/* ........................... PHY INTERFACE ........................... *//* * Enable the MDIO bit in MAC control register * When not called from an interrupt-handler, access to the PHY must be *  protected by a spinlock. */static void enable_mdi(void){	unsigned long ctl;	ctl = at91_emac_read(AT91_EMAC_CTL);	at91_emac_write(AT91_EMAC_CTL, ctl | AT91_EMAC_MPE);	/* enable management port */}/* * Disable the MDIO bit in the MAC control register */static void disable_mdi(void){	unsigned long ctl;	ctl = at91_emac_read(AT91_EMAC_CTL);	at91_emac_write(AT91_EMAC_CTL, ctl & ~AT91_EMAC_MPE);	/* disable management port */}/* * Wait until the PHY operation is complete. */static inline void at91_phy_wait(void) {	unsigned long timeout = jiffies + 2;	while (!(at91_emac_read(AT91_EMAC_SR) & AT91_EMAC_SR_IDLE)) {		if (time_after(jiffies, timeout)) {			printk("at91_ether: MIO timeout\n");			break;		}		cpu_relax();	}}/* * Write value to the a PHY register * Note: MDI interface is assumed to already have been enabled. */static void write_phy(unsigned char phy_addr, unsigned char address, unsigned int value){	at91_emac_write(AT91_EMAC_MAN, AT91_EMAC_MAN_802_3 | AT91_EMAC_RW_W		| ((phy_addr & 0x1f) << 23) | (address << 18) | (value & AT91_EMAC_DATA));	/* Wait until IDLE bit in Network Status register is cleared */	at91_phy_wait();}/* * Read value stored in a PHY register. * Note: MDI interface is assumed to already have been enabled. */static void read_phy(unsigned char phy_addr, unsigned char address, unsigned int *value){	at91_emac_write(AT91_EMAC_MAN, AT91_EMAC_MAN_802_3 | AT91_EMAC_RW_R		| ((phy_addr & 0x1f) << 23) | (address << 18));	/* Wait until IDLE bit in Network Status register is cleared */	at91_phy_wait();	*value = at91_emac_read(AT91_EMAC_MAN) & AT91_EMAC_DATA;}/* ........................... PHY MANAGEMENT .......................... *//* * Access the PHY to determine the current link speed and mode, and update the * MAC accordingly. * If no link or auto-negotiation is busy, then no changes are made. */static void update_linkspeed(struct net_device *dev){	struct at91_private *lp = (struct at91_private *) dev->priv;	unsigned int bmsr, bmcr, lpa, mac_cfg;	unsigned int speed, duplex;	if (!mii_link_ok(&lp->mii)) {		/* no link */		netif_carrier_off(dev);		printk(KERN_INFO "%s: Link down.\n", dev->name);		return;	}	/* Link up, or auto-negotiation still in progress */	read_phy(lp->phy_address, MII_BMSR, &bmsr);	read_phy(lp->phy_address, MII_BMCR, &bmcr);	if (bmcr & BMCR_ANENABLE) {				/* AutoNegotiation is enabled */		if (!(bmsr & BMSR_ANEGCOMPLETE))			return;			/* Do nothing - another interrupt generated when negotiation complete */		read_phy(lp->phy_address, MII_LPA, &lpa);		if ((lpa & LPA_100FULL) || (lpa & LPA_100HALF)) speed = SPEED_100;		else speed = SPEED_10;		if ((lpa & LPA_100FULL) || (lpa & LPA_10FULL)) duplex = DUPLEX_FULL;		else duplex = DUPLEX_HALF;	} else {		speed = (bmcr & BMCR_SPEED100) ? SPEED_100 : SPEED_10;		duplex = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;	}	/* Update the MAC */	mac_cfg = at91_emac_read(AT91_EMAC_CFG) & ~(AT91_EMAC_SPD | AT91_EMAC_FD);	if (speed == SPEED_100) {		if (duplex == DUPLEX_FULL)		/* 100 Full Duplex */			mac_cfg |= AT91_EMAC_SPD | AT91_EMAC_FD;		else					/* 100 Half Duplex */			mac_cfg |= AT91_EMAC_SPD;	} else {		if (duplex == DUPLEX_FULL)		/* 10 Full Duplex */			mac_cfg |= AT91_EMAC_FD;		else {}					/* 10 Half Duplex */	}	at91_emac_write(AT91_EMAC_CFG, mac_cfg);	printk(KERN_INFO "%s: Link now %i-%s\n", dev->name, speed, (duplex == DUPLEX_FULL) ? "FullDuplex" : "HalfDuplex");	netif_carrier_on(dev);}/* * Handle interrupts from the PHY */static irqreturn_t at91ether_phy_interrupt(int irq, void *dev_id, struct pt_regs *regs){	struct net_device *dev = (struct net_device *) dev_id;	struct at91_private *lp = (struct at91_private *) dev->priv;	unsigned int phy;	/*	 * This hander is triggered on both edges, but the PHY chips expect	 * level-triggering.  We therefore have to check if the PHY actually has	 * an IRQ pending.	 */	enable_mdi();	if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {		read_phy(lp->phy_address, MII_DSINTR_REG, &phy);	/* ack interrupt in Davicom PHY */		if (!(phy & (1 << 0)))			goto done;	}	else if (lp->phy_type == MII_LXT971A_ID) {		read_phy(lp->phy_address, MII_ISINTS_REG, &phy);	/* ack interrupt in Intel PHY */		if (!(phy & (1 << 2)))			goto done;	}	else if (lp->phy_type == MII_BCM5221_ID) {		read_phy(lp->phy_address, MII_BCMINTR_REG, &phy);	/* ack interrupt in Broadcom PHY */		if (!(phy & (1 << 0)))			goto done;	}	else if (lp->phy_type == MII_KS8721_ID) {		read_phy(lp->phy_address, MII_TPISTATUS, &phy);		/* ack interrupt in Micrel PHY */		if (!(phy & ((1 << 2) | 1)))			goto done;	}	update_linkspeed(dev);done:	disable_mdi();	return IRQ_HANDLED;}/* * Initialize and enable the PHY interrupt for link-state changes */static void enable_phyirq(struct net_device *dev){	struct at91_private *lp = (struct at91_private *) dev->priv;	unsigned int dsintr, irq_number;	int status;	if (lp->phy_type == MII_RTL8201_ID)	/* RTL8201 does not have an interrupt */		return;	if (lp->phy_type == MII_DP83847_ID)	/* DP83847 does not have an interrupt */		return;	if (lp->phy_type == MII_AC101L_ID)	/* AC101L interrupt not supported yet */		return;	irq_number = lp->board_data.phy_irq_pin;	status = request_irq(irq_number, at91ether_phy_interrupt, 0, dev->name, dev);	if (status) {		printk(KERN_ERR "at91_ether: PHY IRQ %d request failed - status %d!\n", irq_number, status);		return;	}	spin_lock_irq(&lp->lock);	enable_mdi();	if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {	/* for Davicom PHY */		read_phy(lp->phy_address, MII_DSINTR_REG, &dsintr);		dsintr = dsintr & ~0xf00;		/* clear bits 8..11 */		write_phy(lp->phy_address, MII_DSINTR_REG, dsintr);	}	else if (lp->phy_type == MII_LXT971A_ID) {	/* for Intel PHY */		read_phy(lp->phy_address, MII_ISINTE_REG, &dsintr);		dsintr = dsintr | 0xf2;			/* set bits 1, 4..7 */		write_phy(lp->phy_address, MII_ISINTE_REG, dsintr);	}	else if (lp->phy_type == MII_BCM5221_ID) {	/* for Broadcom PHY */		dsintr = (1 << 15) | ( 1 << 14);		write_phy(lp->phy_address, MII_BCMINTR_REG, dsintr);	}	else if (lp->phy_type == MII_KS8721_ID) {	/* for Micrel PHY */		dsintr = (1 << 10) | ( 1 << 8);		write_phy(lp->phy_address, MII_TPISTATUS, dsintr);	}	disable_mdi();	spin_unlock_irq(&lp->lock);}/* * Disable the PHY interrupt */static void disable_phyirq(struct net_device *dev){	struct at91_private *lp = (struct at91_private *) dev->priv;	unsigned int dsintr;	unsigned int irq_number;	if (lp->phy_type == MII_RTL8201_ID) 	/* RTL8201 does not have an interrupt */		return;	if (lp->phy_type == MII_DP83847_ID)	/* DP83847 does not have an interrupt */		return;	if (lp->phy_type == MII_AC101L_ID)	/* AC101L interrupt not supported yet */		return;	spin_lock_irq(&lp->lock);	enable_mdi();	if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {	/* for Davicom PHY */		read_phy(lp->phy_address, MII_DSINTR_REG, &dsintr);		dsintr = dsintr | 0xf00;			/* set bits 8..11 */		write_phy(lp->phy_address, MII_DSINTR_REG, dsintr);	}	else if (lp->phy_type == MII_LXT971A_ID) {	/* for Intel PHY */		read_phy(lp->phy_address, MII_ISINTE_REG, &dsintr);		dsintr = dsintr & ~0xf2;			/* clear bits 1, 4..7 */		write_phy(lp->phy_address, MII_ISINTE_REG, dsintr);	}	else if (lp->phy_type == MII_BCM5221_ID) {	/* for Broadcom PHY */		read_phy(lp->phy_address, MII_BCMINTR_REG, &dsintr);		dsintr = ~(1 << 14);		write_phy(lp->phy_address, MII_BCMINTR_REG, dsintr);	}	else if (lp->phy_type == MII_KS8721_ID) {	/* for Micrel PHY */		read_phy(lp->phy_address, MII_TPISTATUS, &dsintr);		dsintr = ~((1 << 10) | (1 << 8));		write_phy(lp->phy_address, MII_TPISTATUS, dsintr);	}	disable_mdi();	spin_unlock_irq(&lp->lock);	irq_number = lp->board_data.phy_irq_pin;	free_irq(irq_number, dev);			/* Free interrupt handler */}/* * Perform a software reset of the PHY. */#if 0static void reset_phy(struct net_device *dev){	struct at91_private *lp = (struct at91_private *) dev->priv;	unsigned int bmcr;	spin_lock_irq(&lp->lock);	enable_mdi();	/* Perform PHY reset */	write_phy(lp->phy_address, MII_BMCR, BMCR_RESET);	/* Wait until PHY reset is complete */	do {		read_phy(lp->phy_address, MII_BMCR, &bmcr);	} while (!(bmcr && BMCR_RESET));	disable_mdi();	spin_unlock_irq(&lp->lock);}#endif/* ......................... ADDRESS MANAGEMENT ........................ *//* * NOTE: Your bootloader must always set the MAC address correctly before * booting into Linux. * * - It must always set the MAC address after reset, even if it doesn't *   happen to access the Ethernet while it's booting.  Some versions of *   U-Boot on the AT91RM9200-DK do not do this. * * - Likewise it must store the addresses in the correct byte order. *   MicroMonitor (uMon) on the CSB337 does this incorrectly (and *   continues to do so, for bug-compatibility). */

⌨️ 快捷键说明

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