Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Alchemy Au1x00 ethernet driver |
| 4 | * |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 5 | * Copyright 2001-2003, 2006 MontaVista Software Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Copyright 2002 TimeSys Corp. |
| 7 | * Added ethtool/mii-tool support, |
| 8 | * Copyright 2004 Matt Porter <mporter@kernel.crashing.org> |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 9 | * Update: 2004 Bjoern Riemer, riemer@fokus.fraunhofer.de |
| 10 | * or riemer@riemer-nt.de: fixed the link beat detection with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * ioctls (SIOCGMIIPHY) |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 12 | * Copyright 2006 Herbert Valerio Riedel <hvr@gnu.org> |
| 13 | * converted to use linux-2.6.x's PHY framework |
| 14 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Author: MontaVista Software, Inc. |
| 16 | * ppopov@mvista.com or source@mvista.com |
| 17 | * |
| 18 | * ######################################################################## |
| 19 | * |
| 20 | * This program is free software; you can distribute it and/or modify it |
| 21 | * under the terms of the GNU General Public License (Version 2) as |
| 22 | * published by the Free Software Foundation. |
| 23 | * |
| 24 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 25 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 26 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 27 | * for more details. |
| 28 | * |
| 29 | * You should have received a copy of the GNU General Public License along |
| 30 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 31 | * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 32 | * |
| 33 | * ######################################################################## |
| 34 | * |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 35 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/kernel.h> |
| 40 | #include <linux/sched.h> |
| 41 | #include <linux/string.h> |
| 42 | #include <linux/timer.h> |
| 43 | #include <linux/errno.h> |
| 44 | #include <linux/in.h> |
| 45 | #include <linux/ioport.h> |
| 46 | #include <linux/bitops.h> |
| 47 | #include <linux/slab.h> |
| 48 | #include <linux/interrupt.h> |
| 49 | #include <linux/pci.h> |
| 50 | #include <linux/init.h> |
| 51 | #include <linux/netdevice.h> |
| 52 | #include <linux/etherdevice.h> |
| 53 | #include <linux/ethtool.h> |
| 54 | #include <linux/mii.h> |
| 55 | #include <linux/skbuff.h> |
| 56 | #include <linux/delay.h> |
Herbert Valerio Riedel | 8cd35da | 2006-05-01 15:37:09 +0200 | [diff] [blame] | 57 | #include <linux/crc32.h> |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 58 | #include <linux/phy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <asm/mipsregs.h> |
| 60 | #include <asm/irq.h> |
| 61 | #include <asm/io.h> |
| 62 | #include <asm/processor.h> |
| 63 | |
| 64 | #include <asm/mach-au1x00/au1000.h> |
| 65 | #include <asm/cpu.h> |
| 66 | #include "au1000_eth.h" |
| 67 | |
| 68 | #ifdef AU1000_ETH_DEBUG |
| 69 | static int au1000_debug = 5; |
| 70 | #else |
| 71 | static int au1000_debug = 3; |
| 72 | #endif |
| 73 | |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 74 | #define DRV_NAME "au1000_eth" |
Andy Gospodarek | d5b2069 | 2006-09-11 17:39:18 -0400 | [diff] [blame] | 75 | #define DRV_VERSION "1.6" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #define DRV_AUTHOR "Pete Popov <ppopov@embeddedalley.com>" |
| 77 | #define DRV_DESC "Au1xxx on-chip Ethernet driver" |
| 78 | |
| 79 | MODULE_AUTHOR(DRV_AUTHOR); |
| 80 | MODULE_DESCRIPTION(DRV_DESC); |
| 81 | MODULE_LICENSE("GPL"); |
| 82 | |
| 83 | // prototypes |
| 84 | static void hard_stop(struct net_device *); |
| 85 | static void enable_rx_tx(struct net_device *dev); |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 86 | static struct net_device * au1000_probe(int port_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | static int au1000_init(struct net_device *); |
| 88 | static int au1000_open(struct net_device *); |
| 89 | static int au1000_close(struct net_device *); |
| 90 | static int au1000_tx(struct sk_buff *, struct net_device *); |
| 91 | static int au1000_rx(struct net_device *); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 92 | static irqreturn_t au1000_interrupt(int, void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | static void au1000_tx_timeout(struct net_device *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | static void set_rx_mode(struct net_device *); |
| 95 | static struct net_device_stats *au1000_get_stats(struct net_device *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | static int au1000_ioctl(struct net_device *, struct ifreq *, int); |
| 97 | static int mdio_read(struct net_device *, int, int); |
| 98 | static void mdio_write(struct net_device *, int, int, u16); |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 99 | static void au1000_adjust_link(struct net_device *); |
| 100 | static void enable_mac(struct net_device *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | // externs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | extern int get_ethernet_addr(char *ethernet_addr); |
| 104 | extern void str2eaddr(unsigned char *ea, unsigned char *str); |
Ralf Baechle | c21e6d6 | 2006-10-31 13:41:59 +0000 | [diff] [blame] | 105 | extern char * prom_getcmdline(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * Theory of operation |
| 109 | * |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 110 | * The Au1000 MACs use a simple rx and tx descriptor ring scheme. |
| 111 | * There are four receive and four transmit descriptors. These |
| 112 | * descriptors are not in memory; rather, they are just a set of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * hardware registers. |
| 114 | * |
| 115 | * Since the Au1000 has a coherent data cache, the receive and |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 116 | * transmit buffers are allocated from the KSEG0 segment. The |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * hardware registers, however, are still mapped at KSEG1 to |
| 118 | * make sure there's no out-of-order writes, and that all writes |
| 119 | * complete immediately. |
| 120 | */ |
| 121 | |
| 122 | /* These addresses are only used if yamon doesn't tell us what |
| 123 | * the mac address is, and the mac address is not passed on the |
| 124 | * command line. |
| 125 | */ |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 126 | static unsigned char au1000_mac_addr[6] __devinitdata = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | 0x00, 0x50, 0xc2, 0x0c, 0x30, 0x00 |
| 128 | }; |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | struct au1000_private *au_macs[NUM_ETH_INTERFACES]; |
| 131 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 132 | /* |
| 133 | * board-specific configurations |
| 134 | * |
| 135 | * PHY detection algorithm |
| 136 | * |
| 137 | * If AU1XXX_PHY_STATIC_CONFIG is undefined, the PHY setup is |
| 138 | * autodetected: |
| 139 | * |
| 140 | * mii_probe() first searches the current MAC's MII bus for a PHY, |
| 141 | * selecting the first (or last, if AU1XXX_PHY_SEARCH_HIGHEST_ADDR is |
| 142 | * defined) PHY address not already claimed by another netdev. |
| 143 | * |
| 144 | * If nothing was found that way when searching for the 2nd ethernet |
| 145 | * controller's PHY and AU1XXX_PHY1_SEARCH_ON_MAC0 is defined, then |
| 146 | * the first MII bus is searched as well for an unclaimed PHY; this is |
| 147 | * needed in case of a dual-PHY accessible only through the MAC0's MII |
| 148 | * bus. |
| 149 | * |
| 150 | * Finally, if no PHY is found, then the corresponding ethernet |
| 151 | * controller is not registered to the network subsystem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | */ |
| 153 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 154 | /* autodetection defaults */ |
| 155 | #undef AU1XXX_PHY_SEARCH_HIGHEST_ADDR |
| 156 | #define AU1XXX_PHY1_SEARCH_ON_MAC0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 158 | /* static PHY setup |
| 159 | * |
| 160 | * most boards PHY setup should be detectable properly with the |
| 161 | * autodetection algorithm in mii_probe(), but in some cases (e.g. if |
| 162 | * you have a switch attached, or want to use the PHY's interrupt |
| 163 | * notification capabilities) you can provide a static PHY |
| 164 | * configuration here |
| 165 | * |
| 166 | * IRQs may only be set, if a PHY address was configured |
| 167 | * If a PHY address is given, also a bus id is required to be set |
| 168 | * |
| 169 | * ps: make sure the used irqs are configured properly in the board |
| 170 | * specific irq-map |
| 171 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 173 | #if defined(CONFIG_MIPS_BOSPORUS) |
| 174 | /* |
| 175 | * Micrel/Kendin 5 port switch attached to MAC0, |
| 176 | * MAC0 is associated with PHY address 5 (== WAN port) |
| 177 | * MAC1 is not associated with any PHY, since it's connected directly |
| 178 | * to the switch. |
| 179 | * no interrupts are used |
| 180 | */ |
| 181 | # define AU1XXX_PHY_STATIC_CONFIG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 183 | # define AU1XXX_PHY0_ADDR 5 |
| 184 | # define AU1XXX_PHY0_BUSID 0 |
| 185 | # undef AU1XXX_PHY0_IRQ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 187 | # undef AU1XXX_PHY1_ADDR |
| 188 | # undef AU1XXX_PHY1_BUSID |
| 189 | # undef AU1XXX_PHY1_IRQ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | #endif |
| 191 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 192 | #if defined(AU1XXX_PHY0_BUSID) && (AU1XXX_PHY0_BUSID > 0) |
| 193 | # error MAC0-associated PHY attached 2nd MACs MII bus not supported yet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | #endif |
| 195 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 196 | /* |
| 197 | * MII operations |
| 198 | */ |
| 199 | static int mdio_read(struct net_device *dev, int phy_addr, int reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
| 201 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 202 | volatile u32 *const mii_control_reg = &aup->mac->mii_control; |
| 203 | volatile u32 *const mii_data_reg = &aup->mac->mii_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | u32 timedout = 20; |
| 205 | u32 mii_control; |
| 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | while (*mii_control_reg & MAC_MII_BUSY) { |
| 208 | mdelay(1); |
| 209 | if (--timedout == 0) { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 210 | printk(KERN_ERR "%s: read_MII busy timeout!!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | dev->name); |
| 212 | return -1; |
| 213 | } |
| 214 | } |
| 215 | |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 216 | mii_control = MAC_SET_MII_SELECT_REG(reg) | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 217 | MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | *mii_control_reg = mii_control; |
| 220 | |
| 221 | timedout = 20; |
| 222 | while (*mii_control_reg & MAC_MII_BUSY) { |
| 223 | mdelay(1); |
| 224 | if (--timedout == 0) { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 225 | printk(KERN_ERR "%s: mdio_read busy timeout!!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | dev->name); |
| 227 | return -1; |
| 228 | } |
| 229 | } |
| 230 | return (int)*mii_data_reg; |
| 231 | } |
| 232 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 233 | static void mdio_write(struct net_device *dev, int phy_addr, int reg, u16 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
| 235 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 236 | volatile u32 *const mii_control_reg = &aup->mac->mii_control; |
| 237 | volatile u32 *const mii_data_reg = &aup->mac->mii_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | u32 timedout = 20; |
| 239 | u32 mii_control; |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | while (*mii_control_reg & MAC_MII_BUSY) { |
| 242 | mdelay(1); |
| 243 | if (--timedout == 0) { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 244 | printk(KERN_ERR "%s: mdio_write busy timeout!!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | dev->name); |
| 246 | return; |
| 247 | } |
| 248 | } |
| 249 | |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 250 | mii_control = MAC_SET_MII_SELECT_REG(reg) | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 251 | MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | *mii_data_reg = value; |
| 254 | *mii_control_reg = mii_control; |
| 255 | } |
| 256 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 257 | static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 259 | /* WARNING: bus->phy_map[phy_addr].attached_dev == dev does |
| 260 | * _NOT_ hold (e.g. when PHY is accessed through other MAC's MII bus) */ |
| 261 | struct net_device *const dev = bus->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 263 | enable_mac(dev, 0); /* make sure the MAC associated with this |
| 264 | * mii_bus is enabled */ |
| 265 | return mdio_read(dev, phy_addr, regnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 268 | static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum, |
| 269 | u16 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 271 | struct net_device *const dev = bus->priv; |
| 272 | |
| 273 | enable_mac(dev, 0); /* make sure the MAC associated with this |
| 274 | * mii_bus is enabled */ |
| 275 | mdio_write(dev, phy_addr, regnum, value); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int mdiobus_reset(struct mii_bus *bus) |
| 280 | { |
| 281 | struct net_device *const dev = bus->priv; |
| 282 | |
| 283 | enable_mac(dev, 0); /* make sure the MAC associated with this |
| 284 | * mii_bus is enabled */ |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int mii_probe (struct net_device *dev) |
| 289 | { |
| 290 | struct au1000_private *const aup = (struct au1000_private *) dev->priv; |
| 291 | struct phy_device *phydev = NULL; |
| 292 | |
| 293 | #if defined(AU1XXX_PHY_STATIC_CONFIG) |
| 294 | BUG_ON(aup->mac_id < 0 || aup->mac_id > 1); |
| 295 | |
| 296 | if(aup->mac_id == 0) { /* get PHY0 */ |
| 297 | # if defined(AU1XXX_PHY0_ADDR) |
| 298 | phydev = au_macs[AU1XXX_PHY0_BUSID]->mii_bus.phy_map[AU1XXX_PHY0_ADDR]; |
| 299 | # else |
| 300 | printk (KERN_INFO DRV_NAME ":%s: using PHY-less setup\n", |
| 301 | dev->name); |
| 302 | return 0; |
| 303 | # endif /* defined(AU1XXX_PHY0_ADDR) */ |
| 304 | } else if (aup->mac_id == 1) { /* get PHY1 */ |
| 305 | # if defined(AU1XXX_PHY1_ADDR) |
| 306 | phydev = au_macs[AU1XXX_PHY1_BUSID]->mii_bus.phy_map[AU1XXX_PHY1_ADDR]; |
| 307 | # else |
| 308 | printk (KERN_INFO DRV_NAME ":%s: using PHY-less setup\n", |
| 309 | dev->name); |
| 310 | return 0; |
| 311 | # endif /* defined(AU1XXX_PHY1_ADDR) */ |
| 312 | } |
| 313 | |
| 314 | #else /* defined(AU1XXX_PHY_STATIC_CONFIG) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | int phy_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 317 | /* find the first (lowest address) PHY on the current MAC's MII bus */ |
| 318 | for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) |
| 319 | if (aup->mii_bus.phy_map[phy_addr]) { |
| 320 | phydev = aup->mii_bus.phy_map[phy_addr]; |
| 321 | # if !defined(AU1XXX_PHY_SEARCH_HIGHEST_ADDR) |
| 322 | break; /* break out with first one found */ |
| 323 | # endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 326 | # if defined(AU1XXX_PHY1_SEARCH_ON_MAC0) |
| 327 | /* try harder to find a PHY */ |
| 328 | if (!phydev && (aup->mac_id == 1)) { |
| 329 | /* no PHY found, maybe we have a dual PHY? */ |
| 330 | printk (KERN_INFO DRV_NAME ": no PHY found on MAC1, " |
| 331 | "let's see if it's attached to MAC0...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 333 | BUG_ON(!au_macs[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 335 | /* find the first (lowest address) non-attached PHY on |
| 336 | * the MAC0 MII bus */ |
| 337 | for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) { |
| 338 | struct phy_device *const tmp_phydev = |
| 339 | au_macs[0]->mii_bus.phy_map[phy_addr]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 341 | if (!tmp_phydev) |
| 342 | continue; /* no PHY here... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 344 | if (tmp_phydev->attached_dev) |
| 345 | continue; /* already claimed by MAC0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 347 | phydev = tmp_phydev; |
| 348 | break; /* found it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 351 | # endif /* defined(AU1XXX_PHY1_SEARCH_OTHER_BUS) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 353 | #endif /* defined(AU1XXX_PHY_STATIC_CONFIG) */ |
| 354 | if (!phydev) { |
| 355 | printk (KERN_ERR DRV_NAME ":%s: no PHY found\n", dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return -1; |
| 357 | } |
| 358 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 359 | /* now we are supposed to have a proper phydev, to attach to... */ |
| 360 | BUG_ON(!phydev); |
| 361 | BUG_ON(phydev->attached_dev); |
| 362 | |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame^] | 363 | phydev = phy_connect(dev, phydev->dev.bus_id, &au1000_adjust_link, 0, |
| 364 | PHY_INTERFACE_MODE_MII); |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 365 | |
| 366 | if (IS_ERR(phydev)) { |
| 367 | printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name); |
| 368 | return PTR_ERR(phydev); |
| 369 | } |
| 370 | |
| 371 | /* mask with MAC supported features */ |
| 372 | phydev->supported &= (SUPPORTED_10baseT_Half |
| 373 | | SUPPORTED_10baseT_Full |
| 374 | | SUPPORTED_100baseT_Half |
| 375 | | SUPPORTED_100baseT_Full |
| 376 | | SUPPORTED_Autoneg |
| 377 | /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */ |
| 378 | | SUPPORTED_MII |
| 379 | | SUPPORTED_TP); |
| 380 | |
| 381 | phydev->advertising = phydev->supported; |
| 382 | |
| 383 | aup->old_link = 0; |
| 384 | aup->old_speed = 0; |
| 385 | aup->old_duplex = -1; |
| 386 | aup->phy_dev = phydev; |
| 387 | |
| 388 | printk(KERN_INFO "%s: attached PHY driver [%s] " |
| 389 | "(mii_bus:phy_addr=%s, irq=%d)\n", |
| 390 | dev->name, phydev->drv->name, phydev->dev.bus_id, phydev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /* |
| 397 | * Buffer allocation/deallocation routines. The buffer descriptor returned |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 398 | * has the virtual and dma address of a buffer suitable for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | * both, receive and transmit operations. |
| 400 | */ |
| 401 | static db_dest_t *GetFreeDB(struct au1000_private *aup) |
| 402 | { |
| 403 | db_dest_t *pDB; |
| 404 | pDB = aup->pDBfree; |
| 405 | |
| 406 | if (pDB) { |
| 407 | aup->pDBfree = pDB->pnext; |
| 408 | } |
| 409 | return pDB; |
| 410 | } |
| 411 | |
| 412 | void ReleaseDB(struct au1000_private *aup, db_dest_t *pDB) |
| 413 | { |
| 414 | db_dest_t *pDBfree = aup->pDBfree; |
| 415 | if (pDBfree) |
| 416 | pDBfree->pnext = pDB; |
| 417 | aup->pDBfree = pDB; |
| 418 | } |
| 419 | |
| 420 | static void enable_rx_tx(struct net_device *dev) |
| 421 | { |
| 422 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
| 423 | |
| 424 | if (au1000_debug > 4) |
| 425 | printk(KERN_INFO "%s: enable_rx_tx\n", dev->name); |
| 426 | |
| 427 | aup->mac->control |= (MAC_RX_ENABLE | MAC_TX_ENABLE); |
| 428 | au_sync_delay(10); |
| 429 | } |
| 430 | |
| 431 | static void hard_stop(struct net_device *dev) |
| 432 | { |
| 433 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
| 434 | |
| 435 | if (au1000_debug > 4) |
| 436 | printk(KERN_INFO "%s: hard stop\n", dev->name); |
| 437 | |
| 438 | aup->mac->control &= ~(MAC_RX_ENABLE | MAC_TX_ENABLE); |
| 439 | au_sync_delay(10); |
| 440 | } |
| 441 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 442 | static void enable_mac(struct net_device *dev, int force_reset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 444 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | spin_lock_irqsave(&aup->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 449 | if(force_reset || (!aup->mac_enabled)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | *aup->enable = MAC_EN_CLOCK_ENABLE; |
| 451 | au_sync_delay(2); |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 452 | *aup->enable = (MAC_EN_RESET0 | MAC_EN_RESET1 | MAC_EN_RESET2 |
| 453 | | MAC_EN_CLOCK_ENABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | au_sync_delay(2); |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 455 | |
| 456 | aup->mac_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 458 | |
| 459 | spin_unlock_irqrestore(&aup->lock, flags); |
| 460 | } |
| 461 | |
| 462 | static void reset_mac_unlocked(struct net_device *dev) |
| 463 | { |
| 464 | struct au1000_private *const aup = (struct au1000_private *) dev->priv; |
| 465 | int i; |
| 466 | |
| 467 | hard_stop(dev); |
| 468 | |
| 469 | *aup->enable = MAC_EN_CLOCK_ENABLE; |
| 470 | au_sync_delay(2); |
| 471 | *aup->enable = 0; |
| 472 | au_sync_delay(2); |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | aup->tx_full = 0; |
| 475 | for (i = 0; i < NUM_RX_DMA; i++) { |
| 476 | /* reset control bits */ |
| 477 | aup->rx_dma_ring[i]->buff_stat &= ~0xf; |
| 478 | } |
| 479 | for (i = 0; i < NUM_TX_DMA; i++) { |
| 480 | /* reset control bits */ |
| 481 | aup->tx_dma_ring[i]->buff_stat &= ~0xf; |
| 482 | } |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 483 | |
| 484 | aup->mac_enabled = 0; |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 488 | static void reset_mac(struct net_device *dev) |
| 489 | { |
| 490 | struct au1000_private *const aup = (struct au1000_private *) dev->priv; |
| 491 | unsigned long flags; |
| 492 | |
| 493 | if (au1000_debug > 4) |
| 494 | printk(KERN_INFO "%s: reset mac, aup %x\n", |
| 495 | dev->name, (unsigned)aup); |
| 496 | |
| 497 | spin_lock_irqsave(&aup->lock, flags); |
| 498 | |
| 499 | reset_mac_unlocked (dev); |
| 500 | |
| 501 | spin_unlock_irqrestore(&aup->lock, flags); |
| 502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 504 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | * Setup the receive and transmit "rings". These pointers are the addresses |
| 506 | * of the rx and tx MAC DMA registers so they are fixed by the hardware -- |
| 507 | * these are not descriptors sitting in memory. |
| 508 | */ |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 509 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | setup_hw_rings(struct au1000_private *aup, u32 rx_base, u32 tx_base) |
| 511 | { |
| 512 | int i; |
| 513 | |
| 514 | for (i = 0; i < NUM_RX_DMA; i++) { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 515 | aup->rx_dma_ring[i] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | (volatile rx_dma_t *) (rx_base + sizeof(rx_dma_t)*i); |
| 517 | } |
| 518 | for (i = 0; i < NUM_TX_DMA; i++) { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 519 | aup->tx_dma_ring[i] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | (volatile tx_dma_t *) (tx_base + sizeof(tx_dma_t)*i); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | static struct { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | u32 base_addr; |
| 526 | u32 macen_addr; |
| 527 | int irq; |
| 528 | struct net_device *dev; |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 529 | } iflist[2] = { |
| 530 | #ifdef CONFIG_SOC_AU1000 |
| 531 | {AU1000_ETH0_BASE, AU1000_MAC0_ENABLE, AU1000_MAC0_DMA_INT}, |
| 532 | {AU1000_ETH1_BASE, AU1000_MAC1_ENABLE, AU1000_MAC1_DMA_INT} |
| 533 | #endif |
| 534 | #ifdef CONFIG_SOC_AU1100 |
| 535 | {AU1100_ETH0_BASE, AU1100_MAC0_ENABLE, AU1100_MAC0_DMA_INT} |
| 536 | #endif |
| 537 | #ifdef CONFIG_SOC_AU1500 |
| 538 | {AU1500_ETH0_BASE, AU1500_MAC0_ENABLE, AU1500_MAC0_DMA_INT}, |
| 539 | {AU1500_ETH1_BASE, AU1500_MAC1_ENABLE, AU1500_MAC1_DMA_INT} |
| 540 | #endif |
| 541 | #ifdef CONFIG_SOC_AU1550 |
| 542 | {AU1550_ETH0_BASE, AU1550_MAC0_ENABLE, AU1550_MAC0_DMA_INT}, |
| 543 | {AU1550_ETH1_BASE, AU1550_MAC1_ENABLE, AU1550_MAC1_DMA_INT} |
| 544 | #endif |
| 545 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | static int num_ifs; |
| 548 | |
| 549 | /* |
| 550 | * Setup the base address and interupt of the Au1xxx ethernet macs |
| 551 | * based on cpu type and whether the interface is enabled in sys_pinfunc |
| 552 | * register. The last interface is enabled if SYS_PF_NI2 (bit 4) is 0. |
| 553 | */ |
| 554 | static int __init au1000_init_module(void) |
| 555 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | int ni = (int)((au_readl(SYS_PINFUNC) & (u32)(SYS_PF_NI2)) >> 4); |
| 557 | struct net_device *dev; |
| 558 | int i, found_one = 0; |
| 559 | |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 560 | num_ifs = NUM_ETH_INTERFACES - ni; |
| 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | for(i = 0; i < num_ifs; i++) { |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 563 | dev = au1000_probe(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | iflist[i].dev = dev; |
| 565 | if (dev) |
| 566 | found_one++; |
| 567 | } |
| 568 | if (!found_one) |
| 569 | return -ENODEV; |
| 570 | return 0; |
| 571 | } |
| 572 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 573 | /* |
| 574 | * ethtool operations |
| 575 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | static int au1000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 578 | { |
| 579 | struct au1000_private *aup = (struct au1000_private *)dev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 581 | if (aup->phy_dev) |
| 582 | return phy_ethtool_gset(aup->phy_dev, cmd); |
| 583 | |
| 584 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static int au1000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 588 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | struct au1000_private *aup = (struct au1000_private *)dev->priv; |
| 590 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 591 | if (!capable(CAP_NET_ADMIN)) |
| 592 | return -EPERM; |
| 593 | |
| 594 | if (aup->phy_dev) |
| 595 | return phy_ethtool_sset(aup->phy_dev, cmd); |
| 596 | |
| 597 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static void |
| 601 | au1000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 602 | { |
| 603 | struct au1000_private *aup = (struct au1000_private *)dev->priv; |
| 604 | |
| 605 | strcpy(info->driver, DRV_NAME); |
| 606 | strcpy(info->version, DRV_VERSION); |
| 607 | info->fw_version[0] = '\0'; |
| 608 | sprintf(info->bus_info, "%s %d", DRV_NAME, aup->mac_id); |
| 609 | info->regdump_len = 0; |
| 610 | } |
| 611 | |
Jeff Garzik | 7282d49 | 2006-09-13 14:30:00 -0400 | [diff] [blame] | 612 | static const struct ethtool_ops au1000_ethtool_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | .get_settings = au1000_get_settings, |
| 614 | .set_settings = au1000_set_settings, |
| 615 | .get_drvinfo = au1000_get_drvinfo, |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 616 | .get_link = ethtool_op_get_link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | }; |
| 618 | |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 619 | static struct net_device * au1000_probe(int port_num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | { |
| 621 | static unsigned version_printed = 0; |
| 622 | struct au1000_private *aup = NULL; |
| 623 | struct net_device *dev = NULL; |
| 624 | db_dest_t *pDB, *pDBfree; |
| 625 | char *pmac, *argptr; |
| 626 | char ethaddr[6]; |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 627 | int irq, i, err; |
| 628 | u32 base, macen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 630 | if (port_num >= NUM_ETH_INTERFACES) |
| 631 | return NULL; |
| 632 | |
| 633 | base = CPHYSADDR(iflist[port_num].base_addr ); |
| 634 | macen = CPHYSADDR(iflist[port_num].macen_addr); |
| 635 | irq = iflist[port_num].irq; |
| 636 | |
| 637 | if (!request_mem_region( base, MAC_IOSIZE, "Au1x00 ENET") || |
| 638 | !request_mem_region(macen, 4, "Au1x00 ENET")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | return NULL; |
| 640 | |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 641 | if (version_printed++ == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | printk("%s version %s %s\n", DRV_NAME, DRV_VERSION, DRV_AUTHOR); |
| 643 | |
| 644 | dev = alloc_etherdev(sizeof(struct au1000_private)); |
| 645 | if (!dev) { |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 646 | printk(KERN_ERR "%s: alloc_etherdev failed\n", DRV_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | return NULL; |
| 648 | } |
| 649 | |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 650 | if ((err = register_netdev(dev)) != 0) { |
| 651 | printk(KERN_ERR "%s: Cannot register net device, error %d\n", |
| 652 | DRV_NAME, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | free_netdev(dev); |
| 654 | return NULL; |
| 655 | } |
| 656 | |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 657 | printk("%s: Au1xx0 Ethernet found at 0x%x, irq %d\n", |
| 658 | dev->name, base, irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | aup = dev->priv; |
| 661 | |
| 662 | /* Allocate the data buffers */ |
| 663 | /* Snooping works fine with eth on all au1xxx */ |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 664 | aup->vaddr = (u32)dma_alloc_noncoherent(NULL, MAX_BUF_SIZE * |
| 665 | (NUM_TX_BUFFS + NUM_RX_BUFFS), |
| 666 | &aup->dma_addr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | if (!aup->vaddr) { |
| 668 | free_netdev(dev); |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 669 | release_mem_region( base, MAC_IOSIZE); |
| 670 | release_mem_region(macen, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | return NULL; |
| 672 | } |
| 673 | |
| 674 | /* aup->mac is the base address of the MAC's registers */ |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 675 | aup->mac = (volatile mac_reg_t *)iflist[port_num].base_addr; |
| 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | /* Setup some variables for quick register address access */ |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 678 | aup->enable = (volatile u32 *)iflist[port_num].macen_addr; |
| 679 | aup->mac_id = port_num; |
| 680 | au_macs[port_num] = aup; |
| 681 | |
| 682 | if (port_num == 0) { |
| 683 | /* Check the environment variables first */ |
| 684 | if (get_ethernet_addr(ethaddr) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | memcpy(au1000_mac_addr, ethaddr, sizeof(au1000_mac_addr)); |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 686 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | /* Check command line */ |
| 688 | argptr = prom_getcmdline(); |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 689 | if ((pmac = strstr(argptr, "ethaddr=")) == NULL) |
| 690 | printk(KERN_INFO "%s: No MAC address found\n", |
| 691 | dev->name); |
| 692 | /* Use the hard coded MAC addresses */ |
| 693 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | str2eaddr(ethaddr, pmac + strlen("ethaddr=")); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 695 | memcpy(au1000_mac_addr, ethaddr, |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 696 | sizeof(au1000_mac_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
| 698 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 700 | setup_hw_rings(aup, MAC0_RX_DMA_ADDR, MAC0_TX_DMA_ADDR); |
| 701 | } else if (port_num == 1) |
| 702 | setup_hw_rings(aup, MAC1_RX_DMA_ADDR, MAC1_TX_DMA_ADDR); |
| 703 | |
| 704 | /* |
| 705 | * Assign to the Ethernet ports two consecutive MAC addresses |
| 706 | * to match those that are printed on their stickers |
| 707 | */ |
| 708 | memcpy(dev->dev_addr, au1000_mac_addr, sizeof(au1000_mac_addr)); |
| 709 | dev->dev_addr[5] += port_num; |
| 710 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 711 | *aup->enable = 0; |
| 712 | aup->mac_enabled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 714 | aup->mii_bus.priv = dev; |
| 715 | aup->mii_bus.read = mdiobus_read; |
| 716 | aup->mii_bus.write = mdiobus_write; |
| 717 | aup->mii_bus.reset = mdiobus_reset; |
| 718 | aup->mii_bus.name = "au1000_eth_mii"; |
| 719 | aup->mii_bus.id = aup->mac_id; |
| 720 | aup->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL); |
| 721 | for(i = 0; i < PHY_MAX_ADDR; ++i) |
| 722 | aup->mii_bus.irq[i] = PHY_POLL; |
| 723 | |
| 724 | /* if known, set corresponding PHY IRQs */ |
| 725 | #if defined(AU1XXX_PHY_STATIC_CONFIG) |
| 726 | # if defined(AU1XXX_PHY0_IRQ) |
| 727 | if (AU1XXX_PHY0_BUSID == aup->mii_bus.id) |
| 728 | aup->mii_bus.irq[AU1XXX_PHY0_ADDR] = AU1XXX_PHY0_IRQ; |
| 729 | # endif |
| 730 | # if defined(AU1XXX_PHY1_IRQ) |
| 731 | if (AU1XXX_PHY1_BUSID == aup->mii_bus.id) |
| 732 | aup->mii_bus.irq[AU1XXX_PHY1_ADDR] = AU1XXX_PHY1_IRQ; |
| 733 | # endif |
| 734 | #endif |
| 735 | mdiobus_register(&aup->mii_bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
| 737 | if (mii_probe(dev) != 0) { |
| 738 | goto err_out; |
| 739 | } |
| 740 | |
| 741 | pDBfree = NULL; |
| 742 | /* setup the data buffer descriptors and attach a buffer to each one */ |
| 743 | pDB = aup->db; |
| 744 | for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) { |
| 745 | pDB->pnext = pDBfree; |
| 746 | pDBfree = pDB; |
| 747 | pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i); |
| 748 | pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr); |
| 749 | pDB++; |
| 750 | } |
| 751 | aup->pDBfree = pDBfree; |
| 752 | |
| 753 | for (i = 0; i < NUM_RX_DMA; i++) { |
| 754 | pDB = GetFreeDB(aup); |
| 755 | if (!pDB) { |
| 756 | goto err_out; |
| 757 | } |
| 758 | aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr; |
| 759 | aup->rx_db_inuse[i] = pDB; |
| 760 | } |
| 761 | for (i = 0; i < NUM_TX_DMA; i++) { |
| 762 | pDB = GetFreeDB(aup); |
| 763 | if (!pDB) { |
| 764 | goto err_out; |
| 765 | } |
| 766 | aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr; |
| 767 | aup->tx_dma_ring[i]->len = 0; |
| 768 | aup->tx_db_inuse[i] = pDB; |
| 769 | } |
| 770 | |
| 771 | spin_lock_init(&aup->lock); |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 772 | dev->base_addr = base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | dev->irq = irq; |
| 774 | dev->open = au1000_open; |
| 775 | dev->hard_start_xmit = au1000_tx; |
| 776 | dev->stop = au1000_close; |
| 777 | dev->get_stats = au1000_get_stats; |
| 778 | dev->set_multicast_list = &set_rx_mode; |
| 779 | dev->do_ioctl = &au1000_ioctl; |
| 780 | SET_ETHTOOL_OPS(dev, &au1000_ethtool_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | dev->tx_timeout = au1000_tx_timeout; |
| 782 | dev->watchdog_timeo = ETH_TX_TIMEOUT; |
| 783 | |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 784 | /* |
| 785 | * The boot code uses the ethernet controller, so reset it to start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | * fresh. au1000_init() expects that the device is in reset state. |
| 787 | */ |
| 788 | reset_mac(dev); |
| 789 | |
| 790 | return dev; |
| 791 | |
| 792 | err_out: |
| 793 | /* here we should have a valid dev plus aup-> register addresses |
| 794 | * so we can reset the mac properly.*/ |
| 795 | reset_mac(dev); |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | for (i = 0; i < NUM_RX_DMA; i++) { |
| 798 | if (aup->rx_db_inuse[i]) |
| 799 | ReleaseDB(aup, aup->rx_db_inuse[i]); |
| 800 | } |
| 801 | for (i = 0; i < NUM_TX_DMA; i++) { |
| 802 | if (aup->tx_db_inuse[i]) |
| 803 | ReleaseDB(aup, aup->tx_db_inuse[i]); |
| 804 | } |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 805 | dma_free_noncoherent(NULL, MAX_BUF_SIZE * (NUM_TX_BUFFS + NUM_RX_BUFFS), |
| 806 | (void *)aup->vaddr, aup->dma_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | unregister_netdev(dev); |
| 808 | free_netdev(dev); |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 809 | release_mem_region( base, MAC_IOSIZE); |
| 810 | release_mem_region(macen, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | return NULL; |
| 812 | } |
| 813 | |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 814 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | * Initialize the interface. |
| 816 | * |
| 817 | * When the device powers up, the clocks are disabled and the |
| 818 | * mac is in reset state. When the interface is closed, we |
| 819 | * do the same -- reset the device and disable the clocks to |
| 820 | * conserve power. Thus, whenever au1000_init() is called, |
| 821 | * the device should already be in reset state. |
| 822 | */ |
| 823 | static int au1000_init(struct net_device *dev) |
| 824 | { |
| 825 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
| 826 | u32 flags; |
| 827 | int i; |
| 828 | u32 control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 830 | if (au1000_debug > 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | printk("%s: au1000_init\n", dev->name); |
| 832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | /* bring the device out of reset */ |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 834 | enable_mac(dev, 1); |
| 835 | |
| 836 | spin_lock_irqsave(&aup->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
| 838 | aup->mac->control = 0; |
| 839 | aup->tx_head = (aup->tx_dma_ring[0]->buff_stat & 0xC) >> 2; |
| 840 | aup->tx_tail = aup->tx_head; |
| 841 | aup->rx_head = (aup->rx_dma_ring[0]->buff_stat & 0xC) >> 2; |
| 842 | |
| 843 | aup->mac->mac_addr_high = dev->dev_addr[5]<<8 | dev->dev_addr[4]; |
| 844 | aup->mac->mac_addr_low = dev->dev_addr[3]<<24 | dev->dev_addr[2]<<16 | |
| 845 | dev->dev_addr[1]<<8 | dev->dev_addr[0]; |
| 846 | |
| 847 | for (i = 0; i < NUM_RX_DMA; i++) { |
| 848 | aup->rx_dma_ring[i]->buff_stat |= RX_DMA_ENABLE; |
| 849 | } |
| 850 | au_sync(); |
| 851 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 852 | control = MAC_RX_ENABLE | MAC_TX_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | #ifndef CONFIG_CPU_LITTLE_ENDIAN |
| 854 | control |= MAC_BIG_ENDIAN; |
| 855 | #endif |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 856 | if (aup->phy_dev) { |
| 857 | if (aup->phy_dev->link && (DUPLEX_FULL == aup->phy_dev->duplex)) |
| 858 | control |= MAC_FULL_DUPLEX; |
| 859 | else |
| 860 | control |= MAC_DISABLE_RX_OWN; |
| 861 | } else { /* PHY-less op, assume full-duplex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | control |= MAC_FULL_DUPLEX; |
| 863 | } |
| 864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | aup->mac->control = control; |
| 866 | aup->mac->vlan1_tag = 0x8100; /* activate vlan support */ |
| 867 | au_sync(); |
| 868 | |
| 869 | spin_unlock_irqrestore(&aup->lock, flags); |
| 870 | return 0; |
| 871 | } |
| 872 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 873 | static void |
| 874 | au1000_adjust_link(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 877 | struct phy_device *phydev = aup->phy_dev; |
| 878 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 880 | int status_change = 0; |
| 881 | |
| 882 | BUG_ON(!aup->phy_dev); |
| 883 | |
| 884 | spin_lock_irqsave(&aup->lock, flags); |
| 885 | |
| 886 | if (phydev->link && (aup->old_speed != phydev->speed)) { |
| 887 | // speed changed |
| 888 | |
| 889 | switch(phydev->speed) { |
| 890 | case SPEED_10: |
| 891 | case SPEED_100: |
| 892 | break; |
| 893 | default: |
| 894 | printk(KERN_WARNING |
| 895 | "%s: Speed (%d) is not 10/100 ???\n", |
| 896 | dev->name, phydev->speed); |
| 897 | break; |
| 898 | } |
| 899 | |
| 900 | aup->old_speed = phydev->speed; |
| 901 | |
| 902 | status_change = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 905 | if (phydev->link && (aup->old_duplex != phydev->duplex)) { |
| 906 | // duplex mode changed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 908 | /* switching duplex mode requires to disable rx and tx! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | hard_stop(dev); |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 910 | |
| 911 | if (DUPLEX_FULL == phydev->duplex) |
| 912 | aup->mac->control = ((aup->mac->control |
| 913 | | MAC_FULL_DUPLEX) |
| 914 | & ~MAC_DISABLE_RX_OWN); |
| 915 | else |
| 916 | aup->mac->control = ((aup->mac->control |
| 917 | & ~MAC_FULL_DUPLEX) |
| 918 | | MAC_DISABLE_RX_OWN); |
| 919 | au_sync_delay(1); |
| 920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | enable_rx_tx(dev); |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 922 | aup->old_duplex = phydev->duplex; |
| 923 | |
| 924 | status_change = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 927 | if(phydev->link != aup->old_link) { |
| 928 | // link state changed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 930 | if (phydev->link) // link went up |
| 931 | netif_schedule(dev); |
| 932 | else { // link went down |
| 933 | aup->old_speed = 0; |
| 934 | aup->old_duplex = -1; |
| 935 | } |
| 936 | |
| 937 | aup->old_link = phydev->link; |
| 938 | status_change = 1; |
| 939 | } |
| 940 | |
| 941 | spin_unlock_irqrestore(&aup->lock, flags); |
| 942 | |
| 943 | if (status_change) { |
| 944 | if (phydev->link) |
| 945 | printk(KERN_INFO "%s: link up (%d/%s)\n", |
| 946 | dev->name, phydev->speed, |
| 947 | DUPLEX_FULL == phydev->duplex ? "Full" : "Half"); |
| 948 | else |
| 949 | printk(KERN_INFO "%s: link down\n", dev->name); |
| 950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | static int au1000_open(struct net_device *dev) |
| 954 | { |
| 955 | int retval; |
| 956 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
| 957 | |
| 958 | if (au1000_debug > 4) |
| 959 | printk("%s: open: dev=%p\n", dev->name, dev); |
| 960 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 961 | if ((retval = request_irq(dev->irq, &au1000_interrupt, 0, |
| 962 | dev->name, dev))) { |
| 963 | printk(KERN_ERR "%s: unable to get IRQ %d\n", |
| 964 | dev->name, dev->irq); |
| 965 | return retval; |
| 966 | } |
| 967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | if ((retval = au1000_init(dev))) { |
| 969 | printk(KERN_ERR "%s: error in au1000_init\n", dev->name); |
| 970 | free_irq(dev->irq, dev); |
| 971 | return retval; |
| 972 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 974 | if (aup->phy_dev) { |
| 975 | /* cause the PHY state machine to schedule a link state check */ |
| 976 | aup->phy_dev->state = PHY_CHANGELINK; |
| 977 | phy_start(aup->phy_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 980 | netif_start_queue(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
| 982 | if (au1000_debug > 4) |
| 983 | printk("%s: open: Initialization done.\n", dev->name); |
| 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static int au1000_close(struct net_device *dev) |
| 989 | { |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 990 | unsigned long flags; |
| 991 | struct au1000_private *const aup = (struct au1000_private *) dev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | |
| 993 | if (au1000_debug > 4) |
| 994 | printk("%s: close: dev=%p\n", dev->name, dev); |
| 995 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 996 | if (aup->phy_dev) |
| 997 | phy_stop(aup->phy_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
| 999 | spin_lock_irqsave(&aup->lock, flags); |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 1000 | |
| 1001 | reset_mac_unlocked (dev); |
| 1002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | /* stop the device */ |
| 1004 | netif_stop_queue(dev); |
| 1005 | |
| 1006 | /* disable the interrupt */ |
| 1007 | free_irq(dev->irq, dev); |
| 1008 | spin_unlock_irqrestore(&aup->lock, flags); |
| 1009 | |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
| 1013 | static void __exit au1000_cleanup_module(void) |
| 1014 | { |
| 1015 | int i, j; |
| 1016 | struct net_device *dev; |
| 1017 | struct au1000_private *aup; |
| 1018 | |
| 1019 | for (i = 0; i < num_ifs; i++) { |
| 1020 | dev = iflist[i].dev; |
| 1021 | if (dev) { |
| 1022 | aup = (struct au1000_private *) dev->priv; |
| 1023 | unregister_netdev(dev); |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 1024 | for (j = 0; j < NUM_RX_DMA; j++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | if (aup->rx_db_inuse[j]) |
| 1026 | ReleaseDB(aup, aup->rx_db_inuse[j]); |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 1027 | for (j = 0; j < NUM_TX_DMA; j++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | if (aup->tx_db_inuse[j]) |
| 1029 | ReleaseDB(aup, aup->tx_db_inuse[j]); |
Sergei Shtylyov | 89be050 | 2006-04-19 22:46:21 +0400 | [diff] [blame] | 1030 | dma_free_noncoherent(NULL, MAX_BUF_SIZE * |
| 1031 | (NUM_TX_BUFFS + NUM_RX_BUFFS), |
| 1032 | (void *)aup->vaddr, aup->dma_addr); |
| 1033 | release_mem_region(dev->base_addr, MAC_IOSIZE); |
| 1034 | release_mem_region(CPHYSADDR(iflist[i].macen_addr), 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | free_netdev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
Sergei Shtylylov | c2d3d4b | 2006-03-21 22:53:52 -0800 | [diff] [blame] | 1040 | static void update_tx_stats(struct net_device *dev, u32 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | { |
| 1042 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
| 1043 | struct net_device_stats *ps = &aup->stats; |
| 1044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | if (status & TX_FRAME_ABORTED) { |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 1046 | if (!aup->phy_dev || (DUPLEX_FULL == aup->phy_dev->duplex)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) { |
| 1048 | /* any other tx errors are only valid |
| 1049 | * in half duplex mode */ |
| 1050 | ps->tx_errors++; |
| 1051 | ps->tx_aborted_errors++; |
| 1052 | } |
| 1053 | } |
| 1054 | else { |
| 1055 | ps->tx_errors++; |
| 1056 | ps->tx_aborted_errors++; |
| 1057 | if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER)) |
| 1058 | ps->tx_carrier_errors++; |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | |
| 1064 | /* |
| 1065 | * Called from the interrupt service routine to acknowledge |
| 1066 | * the TX DONE bits. This is a must if the irq is setup as |
| 1067 | * edge triggered. |
| 1068 | */ |
| 1069 | static void au1000_tx_ack(struct net_device *dev) |
| 1070 | { |
| 1071 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
| 1072 | volatile tx_dma_t *ptxd; |
| 1073 | |
| 1074 | ptxd = aup->tx_dma_ring[aup->tx_tail]; |
| 1075 | |
| 1076 | while (ptxd->buff_stat & TX_T_DONE) { |
Sergei Shtylylov | c2d3d4b | 2006-03-21 22:53:52 -0800 | [diff] [blame] | 1077 | update_tx_stats(dev, ptxd->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | ptxd->buff_stat &= ~TX_T_DONE; |
| 1079 | ptxd->len = 0; |
| 1080 | au_sync(); |
| 1081 | |
| 1082 | aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1); |
| 1083 | ptxd = aup->tx_dma_ring[aup->tx_tail]; |
| 1084 | |
| 1085 | if (aup->tx_full) { |
| 1086 | aup->tx_full = 0; |
| 1087 | netif_wake_queue(dev); |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | /* |
| 1094 | * Au1000 transmit routine. |
| 1095 | */ |
| 1096 | static int au1000_tx(struct sk_buff *skb, struct net_device *dev) |
| 1097 | { |
| 1098 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
Sergei Shtylylov | c2d3d4b | 2006-03-21 22:53:52 -0800 | [diff] [blame] | 1099 | struct net_device_stats *ps = &aup->stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | volatile tx_dma_t *ptxd; |
| 1101 | u32 buff_stat; |
| 1102 | db_dest_t *pDB; |
| 1103 | int i; |
| 1104 | |
| 1105 | if (au1000_debug > 5) |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1106 | printk("%s: tx: aup %x len=%d, data=%p, head %d\n", |
| 1107 | dev->name, (unsigned)aup, skb->len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | skb->data, aup->tx_head); |
| 1109 | |
| 1110 | ptxd = aup->tx_dma_ring[aup->tx_head]; |
| 1111 | buff_stat = ptxd->buff_stat; |
| 1112 | if (buff_stat & TX_DMA_ENABLE) { |
| 1113 | /* We've wrapped around and the transmitter is still busy */ |
| 1114 | netif_stop_queue(dev); |
| 1115 | aup->tx_full = 1; |
| 1116 | return 1; |
| 1117 | } |
| 1118 | else if (buff_stat & TX_T_DONE) { |
Sergei Shtylylov | c2d3d4b | 2006-03-21 22:53:52 -0800 | [diff] [blame] | 1119 | update_tx_stats(dev, ptxd->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | ptxd->len = 0; |
| 1121 | } |
| 1122 | |
| 1123 | if (aup->tx_full) { |
| 1124 | aup->tx_full = 0; |
| 1125 | netif_wake_queue(dev); |
| 1126 | } |
| 1127 | |
| 1128 | pDB = aup->tx_db_inuse[aup->tx_head]; |
| 1129 | memcpy((void *)pDB->vaddr, skb->data, skb->len); |
| 1130 | if (skb->len < ETH_ZLEN) { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1131 | for (i=skb->len; i<ETH_ZLEN; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | ((char *)pDB->vaddr)[i] = 0; |
| 1133 | } |
| 1134 | ptxd->len = ETH_ZLEN; |
| 1135 | } |
| 1136 | else |
| 1137 | ptxd->len = skb->len; |
| 1138 | |
Sergei Shtylylov | c2d3d4b | 2006-03-21 22:53:52 -0800 | [diff] [blame] | 1139 | ps->tx_packets++; |
| 1140 | ps->tx_bytes += ptxd->len; |
| 1141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE; |
| 1143 | au_sync(); |
| 1144 | dev_kfree_skb(skb); |
| 1145 | aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1); |
| 1146 | dev->trans_start = jiffies; |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | static inline void update_rx_stats(struct net_device *dev, u32 status) |
| 1151 | { |
| 1152 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
| 1153 | struct net_device_stats *ps = &aup->stats; |
| 1154 | |
| 1155 | ps->rx_packets++; |
| 1156 | if (status & RX_MCAST_FRAME) |
| 1157 | ps->multicast++; |
| 1158 | |
| 1159 | if (status & RX_ERROR) { |
| 1160 | ps->rx_errors++; |
| 1161 | if (status & RX_MISSED_FRAME) |
| 1162 | ps->rx_missed_errors++; |
| 1163 | if (status & (RX_OVERLEN | RX_OVERLEN | RX_LEN_ERROR)) |
| 1164 | ps->rx_length_errors++; |
| 1165 | if (status & RX_CRC_ERROR) |
| 1166 | ps->rx_crc_errors++; |
| 1167 | if (status & RX_COLL) |
| 1168 | ps->collisions++; |
| 1169 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1170 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | ps->rx_bytes += status & RX_FRAME_LEN_MASK; |
| 1172 | |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * Au1000 receive routine. |
| 1177 | */ |
| 1178 | static int au1000_rx(struct net_device *dev) |
| 1179 | { |
| 1180 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
| 1181 | struct sk_buff *skb; |
| 1182 | volatile rx_dma_t *prxd; |
| 1183 | u32 buff_stat, status; |
| 1184 | db_dest_t *pDB; |
| 1185 | u32 frmlen; |
| 1186 | |
| 1187 | if (au1000_debug > 5) |
| 1188 | printk("%s: au1000_rx head %d\n", dev->name, aup->rx_head); |
| 1189 | |
| 1190 | prxd = aup->rx_dma_ring[aup->rx_head]; |
| 1191 | buff_stat = prxd->buff_stat; |
| 1192 | while (buff_stat & RX_T_DONE) { |
| 1193 | status = prxd->status; |
| 1194 | pDB = aup->rx_db_inuse[aup->rx_head]; |
| 1195 | update_rx_stats(dev, status); |
| 1196 | if (!(status & RX_ERROR)) { |
| 1197 | |
| 1198 | /* good frame */ |
| 1199 | frmlen = (status & RX_FRAME_LEN_MASK); |
| 1200 | frmlen -= 4; /* Remove FCS */ |
| 1201 | skb = dev_alloc_skb(frmlen + 2); |
| 1202 | if (skb == NULL) { |
| 1203 | printk(KERN_ERR |
| 1204 | "%s: Memory squeeze, dropping packet.\n", |
| 1205 | dev->name); |
| 1206 | aup->stats.rx_dropped++; |
| 1207 | continue; |
| 1208 | } |
| 1209 | skb->dev = dev; |
| 1210 | skb_reserve(skb, 2); /* 16 byte IP header align */ |
| 1211 | eth_copy_and_sum(skb, |
| 1212 | (unsigned char *)pDB->vaddr, frmlen, 0); |
| 1213 | skb_put(skb, frmlen); |
| 1214 | skb->protocol = eth_type_trans(skb, dev); |
| 1215 | netif_rx(skb); /* pass the packet to upper layers */ |
| 1216 | } |
| 1217 | else { |
| 1218 | if (au1000_debug > 4) { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1219 | if (status & RX_MISSED_FRAME) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | printk("rx miss\n"); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1221 | if (status & RX_WDOG_TIMER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | printk("rx wdog\n"); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1223 | if (status & RX_RUNT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | printk("rx runt\n"); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1225 | if (status & RX_OVERLEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | printk("rx overlen\n"); |
| 1227 | if (status & RX_COLL) |
| 1228 | printk("rx coll\n"); |
| 1229 | if (status & RX_MII_ERROR) |
| 1230 | printk("rx mii error\n"); |
| 1231 | if (status & RX_CRC_ERROR) |
| 1232 | printk("rx crc error\n"); |
| 1233 | if (status & RX_LEN_ERROR) |
| 1234 | printk("rx len error\n"); |
| 1235 | if (status & RX_U_CNTRL_FRAME) |
| 1236 | printk("rx u control frame\n"); |
| 1237 | if (status & RX_MISSED_FRAME) |
| 1238 | printk("rx miss\n"); |
| 1239 | } |
| 1240 | } |
| 1241 | prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE); |
| 1242 | aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1); |
| 1243 | au_sync(); |
| 1244 | |
| 1245 | /* next descriptor */ |
| 1246 | prxd = aup->rx_dma_ring[aup->rx_head]; |
| 1247 | buff_stat = prxd->buff_stat; |
| 1248 | dev->last_rx = jiffies; |
| 1249 | } |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | |
| 1254 | /* |
| 1255 | * Au1000 interrupt service routine. |
| 1256 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1257 | static irqreturn_t au1000_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | { |
| 1259 | struct net_device *dev = (struct net_device *) dev_id; |
| 1260 | |
| 1261 | if (dev == NULL) { |
| 1262 | printk(KERN_ERR "%s: isr: null dev ptr\n", dev->name); |
| 1263 | return IRQ_RETVAL(1); |
| 1264 | } |
| 1265 | |
| 1266 | /* Handle RX interrupts first to minimize chance of overrun */ |
| 1267 | |
| 1268 | au1000_rx(dev); |
| 1269 | au1000_tx_ack(dev); |
| 1270 | return IRQ_RETVAL(1); |
| 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | /* |
| 1275 | * The Tx ring has been full longer than the watchdog timeout |
| 1276 | * value. The transmitter must be hung? |
| 1277 | */ |
| 1278 | static void au1000_tx_timeout(struct net_device *dev) |
| 1279 | { |
| 1280 | printk(KERN_ERR "%s: au1000_tx_timeout: dev=%p\n", dev->name, dev); |
| 1281 | reset_mac(dev); |
| 1282 | au1000_init(dev); |
| 1283 | dev->trans_start = jiffies; |
| 1284 | netif_wake_queue(dev); |
| 1285 | } |
| 1286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | static void set_rx_mode(struct net_device *dev) |
| 1288 | { |
| 1289 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
| 1290 | |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1291 | if (au1000_debug > 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | printk("%s: set_rx_mode: flags=%x\n", dev->name, dev->flags); |
| 1293 | |
| 1294 | if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */ |
| 1295 | aup->mac->control |= MAC_PROMISCUOUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | } else if ((dev->flags & IFF_ALLMULTI) || |
| 1297 | dev->mc_count > MULTICAST_FILTER_LIMIT) { |
| 1298 | aup->mac->control |= MAC_PASS_ALL_MULTI; |
| 1299 | aup->mac->control &= ~MAC_PROMISCUOUS; |
| 1300 | printk(KERN_INFO "%s: Pass all multicast\n", dev->name); |
| 1301 | } else { |
| 1302 | int i; |
| 1303 | struct dev_mc_list *mclist; |
| 1304 | u32 mc_filter[2]; /* Multicast hash filter */ |
| 1305 | |
| 1306 | mc_filter[1] = mc_filter[0] = 0; |
| 1307 | for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; |
| 1308 | i++, mclist = mclist->next) { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1309 | set_bit(ether_crc(ETH_ALEN, mclist->dmi_addr)>>26, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | (long *)mc_filter); |
| 1311 | } |
| 1312 | aup->mac->multi_hash_high = mc_filter[1]; |
| 1313 | aup->mac->multi_hash_low = mc_filter[0]; |
| 1314 | aup->mac->control &= ~MAC_PROMISCUOUS; |
| 1315 | aup->mac->control |= MAC_HASH_MODE; |
| 1316 | } |
| 1317 | } |
| 1318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
| 1320 | { |
| 1321 | struct au1000_private *aup = (struct au1000_private *)dev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 1323 | if (!netif_running(dev)) return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 1325 | if (!aup->phy_dev) return -EINVAL; // PHY not controllable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | |
Herbert Valerio Riedel | 0638dec | 2006-06-01 09:41:04 +0200 | [diff] [blame] | 1327 | return phy_mii_ioctl(aup->phy_dev, if_mii(rq), cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | static struct net_device_stats *au1000_get_stats(struct net_device *dev) |
| 1331 | { |
| 1332 | struct au1000_private *aup = (struct au1000_private *) dev->priv; |
| 1333 | |
| 1334 | if (au1000_debug > 4) |
| 1335 | printk("%s: au1000_get_stats: dev=%p\n", dev->name, dev); |
| 1336 | |
| 1337 | if (netif_device_present(dev)) { |
| 1338 | return &aup->stats; |
| 1339 | } |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | module_init(au1000_init_module); |
| 1344 | module_exit(au1000_cleanup_module); |