blob: 0c59d4c5bc013b704b942514219c7ff82244d933 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 *
3 * Alchemy Au1x00 ethernet driver
4 *
Sergei Shtylyov89be0502006-04-19 22:46:21 +04005 * Copyright 2001-2003, 2006 MontaVista Software Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright 2002 TimeSys Corp.
7 * Added ethtool/mii-tool support,
8 * Copyright 2004 Matt Porter <mporter@kernel.crashing.org>
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009 * Update: 2004 Bjoern Riemer, riemer@fokus.fraunhofer.de
10 * or riemer@riemer-nt.de: fixed the link beat detection with
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * ioctls (SIOCGMIIPHY)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +020012 * Copyright 2006 Herbert Valerio Riedel <hvr@gnu.org>
13 * converted to use linux-2.6.x's PHY framework
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * 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 Garzik6aa20a22006-09-13 13:24:59 -040035 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
Manuel Laussbc36b422009-10-17 02:00:07 +000037#include <linux/capability.h>
Ralf Baechled791c2b2007-06-24 15:59:54 +020038#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/module.h>
40#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/init.h>
50#include <linux/netdevice.h>
51#include <linux/etherdevice.h>
52#include <linux/ethtool.h>
53#include <linux/mii.h>
54#include <linux/skbuff.h>
55#include <linux/delay.h>
Herbert Valerio Riedel8cd35da2006-05-01 15:37:09 +020056#include <linux/crc32.h>
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +020057#include <linux/phy.h>
Florian Fainellibd2302c2009-11-10 01:13:38 +010058#include <linux/platform_device.h>
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090059
60#include <asm/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/mipsregs.h>
62#include <asm/irq.h>
63#include <asm/io.h>
64#include <asm/processor.h>
65
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090066#include <au1000.h>
Florian Fainellibd2302c2009-11-10 01:13:38 +010067#include <au1xxx_eth.h>
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090068#include <prom.h>
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include "au1000_eth.h"
71
72#ifdef AU1000_ETH_DEBUG
73static int au1000_debug = 5;
74#else
75static int au1000_debug = 3;
76#endif
77
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +000078#define AU1000_DEF_MSG_ENABLE (NETIF_MSG_DRV | \
79 NETIF_MSG_PROBE | \
80 NETIF_MSG_LINK)
81
Sergei Shtylyov89be0502006-04-19 22:46:21 +040082#define DRV_NAME "au1000_eth"
Andy Gospodarekd5b20692006-09-11 17:39:18 -040083#define DRV_VERSION "1.6"
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define DRV_AUTHOR "Pete Popov <ppopov@embeddedalley.com>"
85#define DRV_DESC "Au1xxx on-chip Ethernet driver"
86
87MODULE_AUTHOR(DRV_AUTHOR);
88MODULE_DESCRIPTION(DRV_DESC);
89MODULE_LICENSE("GPL");
Florian Fainelli13130c72010-04-06 22:08:57 +000090MODULE_VERSION(DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * Theory of operation
94 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -040095 * The Au1000 MACs use a simple rx and tx descriptor ring scheme.
96 * There are four receive and four transmit descriptors. These
97 * descriptors are not in memory; rather, they are just a set of
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * hardware registers.
99 *
100 * Since the Au1000 has a coherent data cache, the receive and
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400101 * transmit buffers are allocated from the KSEG0 segment. The
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * hardware registers, however, are still mapped at KSEG1 to
103 * make sure there's no out-of-order writes, and that all writes
104 * complete immediately.
105 */
106
107/* These addresses are only used if yamon doesn't tell us what
108 * the mac address is, and the mac address is not passed on the
109 * command line.
110 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400111static unsigned char au1000_mac_addr[6] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 0x00, 0x50, 0xc2, 0x0c, 0x30, 0x00
113};
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115struct au1000_private *au_macs[NUM_ETH_INTERFACES];
116
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200117/*
118 * board-specific configurations
119 *
120 * PHY detection algorithm
121 *
Florian Fainellibd2302c2009-11-10 01:13:38 +0100122 * If phy_static_config is undefined, the PHY setup is
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200123 * autodetected:
124 *
125 * mii_probe() first searches the current MAC's MII bus for a PHY,
Florian Fainellibd2302c2009-11-10 01:13:38 +0100126 * selecting the first (or last, if phy_search_highest_addr is
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200127 * defined) PHY address not already claimed by another netdev.
128 *
129 * If nothing was found that way when searching for the 2nd ethernet
Florian Fainellibd2302c2009-11-10 01:13:38 +0100130 * controller's PHY and phy1_search_mac0 is defined, then
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200131 * the first MII bus is searched as well for an unclaimed PHY; this is
132 * needed in case of a dual-PHY accessible only through the MAC0's MII
133 * bus.
134 *
135 * Finally, if no PHY is found, then the corresponding ethernet
136 * controller is not registered to the network subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
138
Florian Fainellibd2302c2009-11-10 01:13:38 +0100139/* autodetection defaults: phy1_search_mac0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200141/* static PHY setup
142 *
143 * most boards PHY setup should be detectable properly with the
144 * autodetection algorithm in mii_probe(), but in some cases (e.g. if
145 * you have a switch attached, or want to use the PHY's interrupt
146 * notification capabilities) you can provide a static PHY
147 * configuration here
148 *
149 * IRQs may only be set, if a PHY address was configured
150 * If a PHY address is given, also a bus id is required to be set
151 *
152 * ps: make sure the used irqs are configured properly in the board
153 * specific irq-map
154 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Florian Fainellieb049632010-04-06 22:09:01 +0000156static void au1000_enable_mac(struct net_device *dev, int force_reset)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800157{
158 unsigned long flags;
159 struct au1000_private *aup = netdev_priv(dev);
160
161 spin_lock_irqsave(&aup->lock, flags);
162
163 if(force_reset || (!aup->mac_enabled)) {
164 *aup->enable = MAC_EN_CLOCK_ENABLE;
165 au_sync_delay(2);
166 *aup->enable = (MAC_EN_RESET0 | MAC_EN_RESET1 | MAC_EN_RESET2
167 | MAC_EN_CLOCK_ENABLE);
168 au_sync_delay(2);
169
170 aup->mac_enabled = 1;
171 }
172
173 spin_unlock_irqrestore(&aup->lock, flags);
174}
175
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200176/*
177 * MII operations
178 */
Adrian Bunk1210dde2008-10-12 21:02:19 -0700179static int au1000_mdio_read(struct net_device *dev, int phy_addr, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Wang Chen454d7c92008-11-12 23:37:49 -0800181 struct au1000_private *aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200182 volatile u32 *const mii_control_reg = &aup->mac->mii_control;
183 volatile u32 *const mii_data_reg = &aup->mac->mii_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u32 timedout = 20;
185 u32 mii_control;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 while (*mii_control_reg & MAC_MII_BUSY) {
188 mdelay(1);
189 if (--timedout == 0) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400190 printk(KERN_ERR "%s: read_MII busy timeout!!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 dev->name);
192 return -1;
193 }
194 }
195
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400196 mii_control = MAC_SET_MII_SELECT_REG(reg) |
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200197 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 *mii_control_reg = mii_control;
200
201 timedout = 20;
202 while (*mii_control_reg & MAC_MII_BUSY) {
203 mdelay(1);
204 if (--timedout == 0) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400205 printk(KERN_ERR "%s: mdio_read busy timeout!!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 dev->name);
207 return -1;
208 }
209 }
210 return (int)*mii_data_reg;
211}
212
Adrian Bunk1210dde2008-10-12 21:02:19 -0700213static void au1000_mdio_write(struct net_device *dev, int phy_addr,
214 int reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Wang Chen454d7c92008-11-12 23:37:49 -0800216 struct au1000_private *aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200217 volatile u32 *const mii_control_reg = &aup->mac->mii_control;
218 volatile u32 *const mii_data_reg = &aup->mac->mii_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 u32 timedout = 20;
220 u32 mii_control;
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 while (*mii_control_reg & MAC_MII_BUSY) {
223 mdelay(1);
224 if (--timedout == 0) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400225 printk(KERN_ERR "%s: mdio_write busy timeout!!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 dev->name);
227 return;
228 }
229 }
230
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400231 mii_control = MAC_SET_MII_SELECT_REG(reg) |
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200232 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 *mii_data_reg = value;
235 *mii_control_reg = mii_control;
236}
237
Adrian Bunk1210dde2008-10-12 21:02:19 -0700238static int au1000_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200240 /* WARNING: bus->phy_map[phy_addr].attached_dev == dev does
241 * _NOT_ hold (e.g. when PHY is accessed through other MAC's MII bus) */
242 struct net_device *const dev = bus->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Florian Fainellieb049632010-04-06 22:09:01 +0000244 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200245 * mii_bus is enabled */
Adrian Bunk1210dde2008-10-12 21:02:19 -0700246 return au1000_mdio_read(dev, phy_addr, regnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Adrian Bunk1210dde2008-10-12 21:02:19 -0700249static int au1000_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
250 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200252 struct net_device *const dev = bus->priv;
253
Florian Fainellieb049632010-04-06 22:09:01 +0000254 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200255 * mii_bus is enabled */
Adrian Bunk1210dde2008-10-12 21:02:19 -0700256 au1000_mdio_write(dev, phy_addr, regnum, value);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200257 return 0;
258}
259
Adrian Bunk1210dde2008-10-12 21:02:19 -0700260static int au1000_mdiobus_reset(struct mii_bus *bus)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200261{
262 struct net_device *const dev = bus->priv;
263
Florian Fainellieb049632010-04-06 22:09:01 +0000264 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200265 * mii_bus is enabled */
266 return 0;
267}
268
Florian Fainellieb049632010-04-06 22:09:01 +0000269static void au1000_hard_stop(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800270{
271 struct au1000_private *aup = netdev_priv(dev);
272
273 if (au1000_debug > 4)
274 printk(KERN_INFO "%s: hard stop\n", dev->name);
275
276 aup->mac->control &= ~(MAC_RX_ENABLE | MAC_TX_ENABLE);
277 au_sync_delay(10);
278}
279
Florian Fainellieb049632010-04-06 22:09:01 +0000280static void au1000_enable_rx_tx(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800281{
282 struct au1000_private *aup = netdev_priv(dev);
283
284 if (au1000_debug > 4)
285 printk(KERN_INFO "%s: enable_rx_tx\n", dev->name);
286
287 aup->mac->control |= (MAC_RX_ENABLE | MAC_TX_ENABLE);
288 au_sync_delay(10);
289}
290
291static void
292au1000_adjust_link(struct net_device *dev)
293{
294 struct au1000_private *aup = netdev_priv(dev);
295 struct phy_device *phydev = aup->phy_dev;
296 unsigned long flags;
297
298 int status_change = 0;
299
300 BUG_ON(!aup->phy_dev);
301
302 spin_lock_irqsave(&aup->lock, flags);
303
304 if (phydev->link && (aup->old_speed != phydev->speed)) {
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000305 /* speed changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800306
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000307 switch (phydev->speed) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800308 case SPEED_10:
309 case SPEED_100:
310 break;
311 default:
312 printk(KERN_WARNING
313 "%s: Speed (%d) is not 10/100 ???\n",
314 dev->name, phydev->speed);
315 break;
316 }
317
318 aup->old_speed = phydev->speed;
319
320 status_change = 1;
321 }
322
323 if (phydev->link && (aup->old_duplex != phydev->duplex)) {
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000324 /* duplex mode changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800325
326 /* switching duplex mode requires to disable rx and tx! */
Florian Fainellieb049632010-04-06 22:09:01 +0000327 au1000_hard_stop(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800328
329 if (DUPLEX_FULL == phydev->duplex)
330 aup->mac->control = ((aup->mac->control
331 | MAC_FULL_DUPLEX)
332 & ~MAC_DISABLE_RX_OWN);
333 else
334 aup->mac->control = ((aup->mac->control
335 & ~MAC_FULL_DUPLEX)
336 | MAC_DISABLE_RX_OWN);
337 au_sync_delay(1);
338
Florian Fainellieb049632010-04-06 22:09:01 +0000339 au1000_enable_rx_tx(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800340 aup->old_duplex = phydev->duplex;
341
342 status_change = 1;
343 }
344
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000345 if (phydev->link != aup->old_link) {
346 /* link state changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800347
348 if (!phydev->link) {
349 /* link went down */
350 aup->old_speed = 0;
351 aup->old_duplex = -1;
352 }
353
354 aup->old_link = phydev->link;
355 status_change = 1;
356 }
357
358 spin_unlock_irqrestore(&aup->lock, flags);
359
360 if (status_change) {
361 if (phydev->link)
362 printk(KERN_INFO "%s: link up (%d/%s)\n",
363 dev->name, phydev->speed,
364 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
365 else
366 printk(KERN_INFO "%s: link down\n", dev->name);
367 }
368}
369
Florian Fainellieb049632010-04-06 22:09:01 +0000370static int au1000_mii_probe (struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200371{
Wang Chen454d7c92008-11-12 23:37:49 -0800372 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200373 struct phy_device *phydev = NULL;
374
Florian Fainellibd2302c2009-11-10 01:13:38 +0100375 if (aup->phy_static_config) {
376 BUG_ON(aup->mac_id < 0 || aup->mac_id > 1);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200377
Florian Fainellibd2302c2009-11-10 01:13:38 +0100378 if (aup->phy_addr)
379 phydev = aup->mii_bus->phy_map[aup->phy_addr];
380 else
381 printk (KERN_INFO DRV_NAME ":%s: using PHY-less setup\n",
382 dev->name);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200383 return 0;
Florian Fainellibd2302c2009-11-10 01:13:38 +0100384 } else {
385 int phy_addr;
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200386
Florian Fainellibd2302c2009-11-10 01:13:38 +0100387 /* find the first (lowest address) PHY on the current MAC's MII bus */
388 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
389 if (aup->mii_bus->phy_map[phy_addr]) {
390 phydev = aup->mii_bus->phy_map[phy_addr];
391 if (!aup->phy_search_highest_addr)
392 break; /* break out with first one found */
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Florian Fainellibd2302c2009-11-10 01:13:38 +0100395 if (aup->phy1_search_mac0) {
396 /* try harder to find a PHY */
397 if (!phydev && (aup->mac_id == 1)) {
398 /* no PHY found, maybe we have a dual PHY? */
399 printk (KERN_INFO DRV_NAME ": no PHY found on MAC1, "
400 "let's see if it's attached to MAC0...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Florian Fainellibd2302c2009-11-10 01:13:38 +0100402 /* find the first (lowest address) non-attached PHY on
403 * the MAC0 MII bus */
404 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
405 struct phy_device *const tmp_phydev =
406 aup->mii_bus->phy_map[phy_addr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Florian Fainellibd2302c2009-11-10 01:13:38 +0100408 if (aup->mac_id == 1)
409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Florian Fainellibd2302c2009-11-10 01:13:38 +0100411 if (!tmp_phydev)
412 continue; /* no PHY here... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Florian Fainellibd2302c2009-11-10 01:13:38 +0100414 if (tmp_phydev->attached_dev)
415 continue; /* already claimed by MAC0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Florian Fainellibd2302c2009-11-10 01:13:38 +0100417 phydev = tmp_phydev;
418 break; /* found it */
419 }
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200424 if (!phydev) {
425 printk (KERN_ERR DRV_NAME ":%s: no PHY found\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return -1;
427 }
428
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200429 /* now we are supposed to have a proper phydev, to attach to... */
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200430 BUG_ON(phydev->attached_dev);
431
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800432 phydev = phy_connect(dev, dev_name(&phydev->dev), &au1000_adjust_link,
433 0, PHY_INTERFACE_MODE_MII);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200434
435 if (IS_ERR(phydev)) {
436 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
437 return PTR_ERR(phydev);
438 }
439
440 /* mask with MAC supported features */
441 phydev->supported &= (SUPPORTED_10baseT_Half
442 | SUPPORTED_10baseT_Full
443 | SUPPORTED_100baseT_Half
444 | SUPPORTED_100baseT_Full
445 | SUPPORTED_Autoneg
446 /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
447 | SUPPORTED_MII
448 | SUPPORTED_TP);
449
450 phydev->advertising = phydev->supported;
451
452 aup->old_link = 0;
453 aup->old_speed = 0;
454 aup->old_duplex = -1;
455 aup->phy_dev = phydev;
456
457 printk(KERN_INFO "%s: attached PHY driver [%s] "
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800458 "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
459 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 return 0;
462}
463
464
465/*
466 * Buffer allocation/deallocation routines. The buffer descriptor returned
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400467 * has the virtual and dma address of a buffer suitable for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * both, receive and transmit operations.
469 */
Florian Fainellieb049632010-04-06 22:09:01 +0000470static db_dest_t *au1000_GetFreeDB(struct au1000_private *aup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 db_dest_t *pDB;
473 pDB = aup->pDBfree;
474
475 if (pDB) {
476 aup->pDBfree = pDB->pnext;
477 }
478 return pDB;
479}
480
Florian Fainellieb049632010-04-06 22:09:01 +0000481void au1000_ReleaseDB(struct au1000_private *aup, db_dest_t *pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 db_dest_t *pDBfree = aup->pDBfree;
484 if (pDBfree)
485 pDBfree->pnext = pDB;
486 aup->pDBfree = pDB;
487}
488
Florian Fainellieb049632010-04-06 22:09:01 +0000489static void au1000_reset_mac_unlocked(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200490{
Wang Chen454d7c92008-11-12 23:37:49 -0800491 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200492 int i;
493
Florian Fainellieb049632010-04-06 22:09:01 +0000494 au1000_hard_stop(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200495
496 *aup->enable = MAC_EN_CLOCK_ENABLE;
497 au_sync_delay(2);
498 *aup->enable = 0;
499 au_sync_delay(2);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 aup->tx_full = 0;
502 for (i = 0; i < NUM_RX_DMA; i++) {
503 /* reset control bits */
504 aup->rx_dma_ring[i]->buff_stat &= ~0xf;
505 }
506 for (i = 0; i < NUM_TX_DMA; i++) {
507 /* reset control bits */
508 aup->tx_dma_ring[i]->buff_stat &= ~0xf;
509 }
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200510
511 aup->mac_enabled = 0;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Florian Fainellieb049632010-04-06 22:09:01 +0000515static void au1000_reset_mac(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200516{
Wang Chen454d7c92008-11-12 23:37:49 -0800517 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200518 unsigned long flags;
519
520 if (au1000_debug > 4)
521 printk(KERN_INFO "%s: reset mac, aup %x\n",
522 dev->name, (unsigned)aup);
523
524 spin_lock_irqsave(&aup->lock, flags);
525
Florian Fainellieb049632010-04-06 22:09:01 +0000526 au1000_reset_mac_unlocked (dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200527
528 spin_unlock_irqrestore(&aup->lock, flags);
529}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400531/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * Setup the receive and transmit "rings". These pointers are the addresses
533 * of the rx and tx MAC DMA registers so they are fixed by the hardware --
534 * these are not descriptors sitting in memory.
535 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400536static void
Florian Fainellieb049632010-04-06 22:09:01 +0000537au1000_setup_hw_rings(struct au1000_private *aup, u32 rx_base, u32 tx_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 int i;
540
541 for (i = 0; i < NUM_RX_DMA; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400542 aup->rx_dma_ring[i] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 (volatile rx_dma_t *) (rx_base + sizeof(rx_dma_t)*i);
544 }
545 for (i = 0; i < NUM_TX_DMA; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400546 aup->tx_dma_ring[i] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 (volatile tx_dma_t *) (tx_base + sizeof(tx_dma_t)*i);
548 }
549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/*
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200552 * ethtool operations
553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555static int au1000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
556{
Wang Chen454d7c92008-11-12 23:37:49 -0800557 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200559 if (aup->phy_dev)
560 return phy_ethtool_gset(aup->phy_dev, cmd);
561
562 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565static int au1000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
566{
Wang Chen454d7c92008-11-12 23:37:49 -0800567 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200569 if (!capable(CAP_NET_ADMIN))
570 return -EPERM;
571
572 if (aup->phy_dev)
573 return phy_ethtool_sset(aup->phy_dev, cmd);
574
575 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578static void
579au1000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
580{
Wang Chen454d7c92008-11-12 23:37:49 -0800581 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 strcpy(info->driver, DRV_NAME);
584 strcpy(info->version, DRV_VERSION);
585 info->fw_version[0] = '\0';
586 sprintf(info->bus_info, "%s %d", DRV_NAME, aup->mac_id);
587 info->regdump_len = 0;
588}
589
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +0000590static void au1000_set_msglevel(struct net_device *dev, u32 value)
591{
592 struct au1000_private *aup = netdev_priv(dev);
593 aup->msg_enable = value;
594}
595
596static u32 au1000_get_msglevel(struct net_device *dev)
597{
598 struct au1000_private *aup = netdev_priv(dev);
599 return aup->msg_enable;
600}
601
Jeff Garzik7282d492006-09-13 14:30:00 -0400602static const struct ethtool_ops au1000_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 .get_settings = au1000_get_settings,
604 .set_settings = au1000_set_settings,
605 .get_drvinfo = au1000_get_drvinfo,
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200606 .get_link = ethtool_op_get_link,
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +0000607 .get_msglevel = au1000_get_msglevel,
608 .set_msglevel = au1000_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609};
610
Florian Fainelli5ef30412009-01-22 14:06:25 -0800611
612/*
613 * Initialize the interface.
614 *
615 * When the device powers up, the clocks are disabled and the
616 * mac is in reset state. When the interface is closed, we
617 * do the same -- reset the device and disable the clocks to
618 * conserve power. Thus, whenever au1000_init() is called,
619 * the device should already be in reset state.
620 */
621static int au1000_init(struct net_device *dev)
622{
623 struct au1000_private *aup = netdev_priv(dev);
624 unsigned long flags;
625 int i;
626 u32 control;
627
628 if (au1000_debug > 4)
629 printk("%s: au1000_init\n", dev->name);
630
631 /* bring the device out of reset */
Florian Fainellieb049632010-04-06 22:09:01 +0000632 au1000_enable_mac(dev, 1);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800633
634 spin_lock_irqsave(&aup->lock, flags);
635
636 aup->mac->control = 0;
637 aup->tx_head = (aup->tx_dma_ring[0]->buff_stat & 0xC) >> 2;
638 aup->tx_tail = aup->tx_head;
639 aup->rx_head = (aup->rx_dma_ring[0]->buff_stat & 0xC) >> 2;
640
641 aup->mac->mac_addr_high = dev->dev_addr[5]<<8 | dev->dev_addr[4];
642 aup->mac->mac_addr_low = dev->dev_addr[3]<<24 | dev->dev_addr[2]<<16 |
643 dev->dev_addr[1]<<8 | dev->dev_addr[0];
644
645 for (i = 0; i < NUM_RX_DMA; i++) {
646 aup->rx_dma_ring[i]->buff_stat |= RX_DMA_ENABLE;
647 }
648 au_sync();
649
650 control = MAC_RX_ENABLE | MAC_TX_ENABLE;
651#ifndef CONFIG_CPU_LITTLE_ENDIAN
652 control |= MAC_BIG_ENDIAN;
653#endif
654 if (aup->phy_dev) {
655 if (aup->phy_dev->link && (DUPLEX_FULL == aup->phy_dev->duplex))
656 control |= MAC_FULL_DUPLEX;
657 else
658 control |= MAC_DISABLE_RX_OWN;
659 } else { /* PHY-less op, assume full-duplex */
660 control |= MAC_FULL_DUPLEX;
661 }
662
663 aup->mac->control = control;
664 aup->mac->vlan1_tag = 0x8100; /* activate vlan support */
665 au_sync();
666
667 spin_unlock_irqrestore(&aup->lock, flags);
668 return 0;
669}
670
Florian Fainellieb049632010-04-06 22:09:01 +0000671static inline void au1000_update_rx_stats(struct net_device *dev, u32 status)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800672{
Florian Fainelli5ef30412009-01-22 14:06:25 -0800673 struct net_device_stats *ps = &dev->stats;
674
675 ps->rx_packets++;
676 if (status & RX_MCAST_FRAME)
677 ps->multicast++;
678
679 if (status & RX_ERROR) {
680 ps->rx_errors++;
681 if (status & RX_MISSED_FRAME)
682 ps->rx_missed_errors++;
roel kluin4989ccb2009-10-06 09:54:18 +0000683 if (status & (RX_OVERLEN | RX_RUNT | RX_LEN_ERROR))
Florian Fainelli5ef30412009-01-22 14:06:25 -0800684 ps->rx_length_errors++;
685 if (status & RX_CRC_ERROR)
686 ps->rx_crc_errors++;
687 if (status & RX_COLL)
688 ps->collisions++;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000689 } else
Florian Fainelli5ef30412009-01-22 14:06:25 -0800690 ps->rx_bytes += status & RX_FRAME_LEN_MASK;
691
692}
693
694/*
695 * Au1000 receive routine.
696 */
697static int au1000_rx(struct net_device *dev)
698{
699 struct au1000_private *aup = netdev_priv(dev);
700 struct sk_buff *skb;
701 volatile rx_dma_t *prxd;
702 u32 buff_stat, status;
703 db_dest_t *pDB;
704 u32 frmlen;
705
706 if (au1000_debug > 5)
707 printk("%s: au1000_rx head %d\n", dev->name, aup->rx_head);
708
709 prxd = aup->rx_dma_ring[aup->rx_head];
710 buff_stat = prxd->buff_stat;
711 while (buff_stat & RX_T_DONE) {
712 status = prxd->status;
713 pDB = aup->rx_db_inuse[aup->rx_head];
Florian Fainellieb049632010-04-06 22:09:01 +0000714 au1000_update_rx_stats(dev, status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800715 if (!(status & RX_ERROR)) {
716
717 /* good frame */
718 frmlen = (status & RX_FRAME_LEN_MASK);
719 frmlen -= 4; /* Remove FCS */
720 skb = dev_alloc_skb(frmlen + 2);
721 if (skb == NULL) {
722 printk(KERN_ERR
723 "%s: Memory squeeze, dropping packet.\n",
724 dev->name);
725 dev->stats.rx_dropped++;
726 continue;
727 }
728 skb_reserve(skb, 2); /* 16 byte IP header align */
729 skb_copy_to_linear_data(skb,
730 (unsigned char *)pDB->vaddr, frmlen);
731 skb_put(skb, frmlen);
732 skb->protocol = eth_type_trans(skb, dev);
733 netif_rx(skb); /* pass the packet to upper layers */
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000734 } else {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800735 if (au1000_debug > 4) {
736 if (status & RX_MISSED_FRAME)
737 printk("rx miss\n");
738 if (status & RX_WDOG_TIMER)
739 printk("rx wdog\n");
740 if (status & RX_RUNT)
741 printk("rx runt\n");
742 if (status & RX_OVERLEN)
743 printk("rx overlen\n");
744 if (status & RX_COLL)
745 printk("rx coll\n");
746 if (status & RX_MII_ERROR)
747 printk("rx mii error\n");
748 if (status & RX_CRC_ERROR)
749 printk("rx crc error\n");
750 if (status & RX_LEN_ERROR)
751 printk("rx len error\n");
752 if (status & RX_U_CNTRL_FRAME)
753 printk("rx u control frame\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800754 }
755 }
756 prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE);
757 aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1);
758 au_sync();
759
760 /* next descriptor */
761 prxd = aup->rx_dma_ring[aup->rx_head];
762 buff_stat = prxd->buff_stat;
763 }
764 return 0;
765}
766
Florian Fainellieb049632010-04-06 22:09:01 +0000767static void au1000_update_tx_stats(struct net_device *dev, u32 status)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800768{
769 struct au1000_private *aup = netdev_priv(dev);
770 struct net_device_stats *ps = &dev->stats;
771
772 if (status & TX_FRAME_ABORTED) {
773 if (!aup->phy_dev || (DUPLEX_FULL == aup->phy_dev->duplex)) {
774 if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) {
775 /* any other tx errors are only valid
776 * in half duplex mode */
777 ps->tx_errors++;
778 ps->tx_aborted_errors++;
779 }
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000780 } else {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800781 ps->tx_errors++;
782 ps->tx_aborted_errors++;
783 if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER))
784 ps->tx_carrier_errors++;
785 }
786 }
787}
788
789/*
790 * Called from the interrupt service routine to acknowledge
791 * the TX DONE bits. This is a must if the irq is setup as
792 * edge triggered.
793 */
794static void au1000_tx_ack(struct net_device *dev)
795{
796 struct au1000_private *aup = netdev_priv(dev);
797 volatile tx_dma_t *ptxd;
798
799 ptxd = aup->tx_dma_ring[aup->tx_tail];
800
801 while (ptxd->buff_stat & TX_T_DONE) {
Florian Fainellieb049632010-04-06 22:09:01 +0000802 au1000_update_tx_stats(dev, ptxd->status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800803 ptxd->buff_stat &= ~TX_T_DONE;
804 ptxd->len = 0;
805 au_sync();
806
807 aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1);
808 ptxd = aup->tx_dma_ring[aup->tx_tail];
809
810 if (aup->tx_full) {
811 aup->tx_full = 0;
812 netif_wake_queue(dev);
813 }
814 }
815}
816
817/*
818 * Au1000 interrupt service routine.
819 */
820static irqreturn_t au1000_interrupt(int irq, void *dev_id)
821{
822 struct net_device *dev = dev_id;
823
824 /* Handle RX interrupts first to minimize chance of overrun */
825
826 au1000_rx(dev);
827 au1000_tx_ack(dev);
828 return IRQ_RETVAL(1);
829}
830
831static int au1000_open(struct net_device *dev)
832{
833 int retval;
834 struct au1000_private *aup = netdev_priv(dev);
835
836 if (au1000_debug > 4)
837 printk("%s: open: dev=%p\n", dev->name, dev);
838
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000839 retval = request_irq(dev->irq, au1000_interrupt, 0,
840 dev->name, dev);
841 if (retval) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800842 printk(KERN_ERR "%s: unable to get IRQ %d\n",
843 dev->name, dev->irq);
844 return retval;
845 }
846
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000847 retval = au1000_init(dev);
848 if (retval) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800849 printk(KERN_ERR "%s: error in au1000_init\n", dev->name);
850 free_irq(dev->irq, dev);
851 return retval;
852 }
853
854 if (aup->phy_dev) {
855 /* cause the PHY state machine to schedule a link state check */
856 aup->phy_dev->state = PHY_CHANGELINK;
857 phy_start(aup->phy_dev);
858 }
859
860 netif_start_queue(dev);
861
862 if (au1000_debug > 4)
863 printk("%s: open: Initialization done.\n", dev->name);
864
865 return 0;
866}
867
868static int au1000_close(struct net_device *dev)
869{
870 unsigned long flags;
871 struct au1000_private *const aup = netdev_priv(dev);
872
873 if (au1000_debug > 4)
874 printk("%s: close: dev=%p\n", dev->name, dev);
875
876 if (aup->phy_dev)
877 phy_stop(aup->phy_dev);
878
879 spin_lock_irqsave(&aup->lock, flags);
880
Florian Fainellieb049632010-04-06 22:09:01 +0000881 au1000_reset_mac_unlocked (dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800882
883 /* stop the device */
884 netif_stop_queue(dev);
885
886 /* disable the interrupt */
887 free_irq(dev->irq, dev);
888 spin_unlock_irqrestore(&aup->lock, flags);
889
890 return 0;
891}
892
893/*
894 * Au1000 transmit routine.
895 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000896static netdev_tx_t au1000_tx(struct sk_buff *skb, struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800897{
898 struct au1000_private *aup = netdev_priv(dev);
899 struct net_device_stats *ps = &dev->stats;
900 volatile tx_dma_t *ptxd;
901 u32 buff_stat;
902 db_dest_t *pDB;
903 int i;
904
905 if (au1000_debug > 5)
906 printk("%s: tx: aup %x len=%d, data=%p, head %d\n",
907 dev->name, (unsigned)aup, skb->len,
908 skb->data, aup->tx_head);
909
910 ptxd = aup->tx_dma_ring[aup->tx_head];
911 buff_stat = ptxd->buff_stat;
912 if (buff_stat & TX_DMA_ENABLE) {
913 /* We've wrapped around and the transmitter is still busy */
914 netif_stop_queue(dev);
915 aup->tx_full = 1;
Patrick McHardy5b548142009-06-12 06:22:29 +0000916 return NETDEV_TX_BUSY;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000917 } else if (buff_stat & TX_T_DONE) {
Florian Fainellieb049632010-04-06 22:09:01 +0000918 au1000_update_tx_stats(dev, ptxd->status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800919 ptxd->len = 0;
920 }
921
922 if (aup->tx_full) {
923 aup->tx_full = 0;
924 netif_wake_queue(dev);
925 }
926
927 pDB = aup->tx_db_inuse[aup->tx_head];
Florian Fainellibd2302c2009-11-10 01:13:38 +0100928 skb_copy_from_linear_data(skb, (void *)pDB->vaddr, skb->len);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800929 if (skb->len < ETH_ZLEN) {
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000930 for (i = skb->len; i < ETH_ZLEN; i++) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800931 ((char *)pDB->vaddr)[i] = 0;
932 }
933 ptxd->len = ETH_ZLEN;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000934 } else
Florian Fainelli5ef30412009-01-22 14:06:25 -0800935 ptxd->len = skb->len;
936
937 ps->tx_packets++;
938 ps->tx_bytes += ptxd->len;
939
940 ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE;
941 au_sync();
942 dev_kfree_skb(skb);
943 aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1);
944 dev->trans_start = jiffies;
Patrick McHardy6ed10652009-06-23 06:03:08 +0000945 return NETDEV_TX_OK;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800946}
947
948/*
949 * The Tx ring has been full longer than the watchdog timeout
950 * value. The transmitter must be hung?
951 */
952static void au1000_tx_timeout(struct net_device *dev)
953{
954 printk(KERN_ERR "%s: au1000_tx_timeout: dev=%p\n", dev->name, dev);
Florian Fainellieb049632010-04-06 22:09:01 +0000955 au1000_reset_mac(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800956 au1000_init(dev);
957 dev->trans_start = jiffies;
958 netif_wake_queue(dev);
959}
960
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +0000961static void au1000_multicast_list(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800962{
963 struct au1000_private *aup = netdev_priv(dev);
964
965 if (au1000_debug > 4)
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +0000966 printk("%s: au1000_multicast_list: flags=%x\n", dev->name, dev->flags);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800967
968 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
969 aup->mac->control |= MAC_PROMISCUOUS;
970 } else if ((dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000971 netdev_mc_count(dev) > MULTICAST_FILTER_LIMIT) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800972 aup->mac->control |= MAC_PASS_ALL_MULTI;
973 aup->mac->control &= ~MAC_PROMISCUOUS;
974 printk(KERN_INFO "%s: Pass all multicast\n", dev->name);
975 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000976 struct netdev_hw_addr *ha;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800977 u32 mc_filter[2]; /* Multicast hash filter */
978
979 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000980 netdev_for_each_mc_addr(ha, dev)
981 set_bit(ether_crc(ETH_ALEN, ha->addr)>>26,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800982 (long *)mc_filter);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800983 aup->mac->multi_hash_high = mc_filter[1];
984 aup->mac->multi_hash_low = mc_filter[0];
985 aup->mac->control &= ~MAC_PROMISCUOUS;
986 aup->mac->control |= MAC_HASH_MODE;
987 }
988}
989
990static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
991{
992 struct au1000_private *aup = netdev_priv(dev);
993
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000994 if (!netif_running(dev))
995 return -EINVAL;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800996
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000997 if (!aup->phy_dev)
998 return -EINVAL; /* PHY not controllable */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800999
1000 return phy_mii_ioctl(aup->phy_dev, if_mii(rq), cmd);
1001}
1002
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +00001003static const struct net_device_ops au1000_netdev_ops = {
1004 .ndo_open = au1000_open,
1005 .ndo_stop = au1000_close,
1006 .ndo_start_xmit = au1000_tx,
1007 .ndo_set_multicast_list = au1000_multicast_list,
1008 .ndo_do_ioctl = au1000_ioctl,
1009 .ndo_tx_timeout = au1000_tx_timeout,
1010 .ndo_set_mac_address = eth_mac_addr,
1011 .ndo_validate_addr = eth_validate_addr,
1012 .ndo_change_mtu = eth_change_mtu,
1013};
1014
Florian Fainellibd2302c2009-11-10 01:13:38 +01001015static int __devinit au1000_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +00001017 static unsigned version_printed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 struct au1000_private *aup = NULL;
Florian Fainellibd2302c2009-11-10 01:13:38 +01001019 struct au1000_eth_platform_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct net_device *dev = NULL;
1021 db_dest_t *pDB, *pDBfree;
Florian Fainellibd2302c2009-11-10 01:13:38 +01001022 int irq, i, err = 0;
1023 struct resource *base, *macen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 char ethaddr[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Florian Fainellibd2302c2009-11-10 01:13:38 +01001026 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1027 if (!base) {
1028 printk(KERN_ERR DRV_NAME ": failed to retrieve base register\n");
1029 err = -ENODEV;
1030 goto out;
1031 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001032
Florian Fainellibd2302c2009-11-10 01:13:38 +01001033 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1034 if (!macen) {
1035 printk(KERN_ERR DRV_NAME ": failed to retrieve MAC Enable register\n");
1036 err = -ENODEV;
1037 goto out;
1038 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001039
Florian Fainellibd2302c2009-11-10 01:13:38 +01001040 irq = platform_get_irq(pdev, 0);
1041 if (irq < 0) {
1042 printk(KERN_ERR DRV_NAME ": failed to retrieve IRQ\n");
1043 err = -ENODEV;
1044 goto out;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Florian Fainellibd2302c2009-11-10 01:13:38 +01001047 if (!request_mem_region(base->start, resource_size(base), pdev->name)) {
1048 printk(KERN_ERR DRV_NAME ": failed to request memory region for base registers\n");
1049 err = -ENXIO;
1050 goto out;
1051 }
1052
1053 if (!request_mem_region(macen->start, resource_size(macen), pdev->name)) {
1054 printk(KERN_ERR DRV_NAME ": failed to request memory region for MAC enable register\n");
1055 err = -ENXIO;
1056 goto err_request;
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 dev = alloc_etherdev(sizeof(struct au1000_private));
1060 if (!dev) {
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001061 printk(KERN_ERR "%s: alloc_etherdev failed\n", DRV_NAME);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001062 err = -ENOMEM;
1063 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
Florian Fainellibd2302c2009-11-10 01:13:38 +01001066 SET_NETDEV_DEV(dev, &pdev->dev);
1067 platform_set_drvdata(pdev, dev);
Wang Chen454d7c92008-11-12 23:37:49 -08001068 aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Martin Gebert533763d2008-07-23 09:40:09 +02001070 spin_lock_init(&aup->lock);
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +00001071 aup->msg_enable = (au1000_debug < 4 ? AU1000_DEF_MSG_ENABLE : au1000_debug);
Martin Gebert533763d2008-07-23 09:40:09 +02001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* Allocate the data buffers */
1074 /* Snooping works fine with eth on all au1xxx */
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001075 aup->vaddr = (u32)dma_alloc_noncoherent(NULL, MAX_BUF_SIZE *
1076 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1077 &aup->dma_addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!aup->vaddr) {
Florian Fainellibd2302c2009-11-10 01:13:38 +01001079 printk(KERN_ERR DRV_NAME ": failed to allocate data buffers\n");
1080 err = -ENOMEM;
1081 goto err_vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
1084 /* aup->mac is the base address of the MAC's registers */
Florian Fainellibd2302c2009-11-10 01:13:38 +01001085 aup->mac = (volatile mac_reg_t *)ioremap_nocache(base->start, resource_size(base));
1086 if (!aup->mac) {
1087 printk(KERN_ERR DRV_NAME ": failed to ioremap MAC registers\n");
1088 err = -ENXIO;
1089 goto err_remap1;
1090 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001091
Florian Fainellibd2302c2009-11-10 01:13:38 +01001092 /* Setup some variables for quick register address access */
1093 aup->enable = (volatile u32 *)ioremap_nocache(macen->start, resource_size(macen));
1094 if (!aup->enable) {
1095 printk(KERN_ERR DRV_NAME ": failed to ioremap MAC enable register\n");
1096 err = -ENXIO;
1097 goto err_remap2;
1098 }
1099 aup->mac_id = pdev->id;
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001100
Florian Fainellibd2302c2009-11-10 01:13:38 +01001101 if (pdev->id == 0) {
Yoichi Yuasa2de88922007-10-15 19:06:20 +09001102 if (prom_get_ethernet_addr(ethaddr) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 memcpy(au1000_mac_addr, ethaddr, sizeof(au1000_mac_addr));
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001104 else {
Yoichi Yuasa2de88922007-10-15 19:06:20 +09001105 printk(KERN_INFO "%s: No MAC address found\n",
1106 dev->name);
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001107 /* Use the hard coded MAC addresses */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Florian Fainellieb049632010-04-06 22:09:01 +00001110 au1000_setup_hw_rings(aup, MAC0_RX_DMA_ADDR, MAC0_TX_DMA_ADDR);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001111 } else if (pdev->id == 1)
Florian Fainellieb049632010-04-06 22:09:01 +00001112 au1000_setup_hw_rings(aup, MAC1_RX_DMA_ADDR, MAC1_TX_DMA_ADDR);
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001113
1114 /*
1115 * Assign to the Ethernet ports two consecutive MAC addresses
1116 * to match those that are printed on their stickers
1117 */
1118 memcpy(dev->dev_addr, au1000_mac_addr, sizeof(au1000_mac_addr));
Florian Fainellibd2302c2009-11-10 01:13:38 +01001119 dev->dev_addr[5] += pdev->id;
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001120
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001121 *aup->enable = 0;
1122 aup->mac_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Florian Fainellibd2302c2009-11-10 01:13:38 +01001124 pd = pdev->dev.platform_data;
1125 if (!pd) {
1126 printk(KERN_INFO DRV_NAME ": no platform_data passed, PHY search on MAC0\n");
1127 aup->phy1_search_mac0 = 1;
1128 } else {
1129 aup->phy_static_config = pd->phy_static_config;
1130 aup->phy_search_highest_addr = pd->phy_search_highest_addr;
1131 aup->phy1_search_mac0 = pd->phy1_search_mac0;
1132 aup->phy_addr = pd->phy_addr;
1133 aup->phy_busid = pd->phy_busid;
1134 aup->phy_irq = pd->phy_irq;
1135 }
1136
1137 if (aup->phy_busid && aup->phy_busid > 0) {
1138 printk(KERN_ERR DRV_NAME ": MAC0-associated PHY attached 2nd MACs MII"
1139 "bus not supported yet\n");
1140 err = -ENODEV;
1141 goto err_mdiobus_alloc;
1142 }
1143
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001144 aup->mii_bus = mdiobus_alloc();
Florian Fainellibd2302c2009-11-10 01:13:38 +01001145 if (aup->mii_bus == NULL) {
1146 printk(KERN_ERR DRV_NAME ": failed to allocate mdiobus structure\n");
1147 err = -ENOMEM;
1148 goto err_mdiobus_alloc;
1149 }
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001150
1151 aup->mii_bus->priv = dev;
Adrian Bunk1210dde2008-10-12 21:02:19 -07001152 aup->mii_bus->read = au1000_mdiobus_read;
1153 aup->mii_bus->write = au1000_mdiobus_write;
1154 aup->mii_bus->reset = au1000_mdiobus_reset;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001155 aup->mii_bus->name = "au1000_eth_mii";
1156 snprintf(aup->mii_bus->id, MII_BUS_ID_SIZE, "%x", aup->mac_id);
1157 aup->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
roel kluindcbfef82009-08-30 22:40:15 +00001158 if (aup->mii_bus->irq == NULL)
1159 goto err_out;
1160
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +00001161 for (i = 0; i < PHY_MAX_ADDR; ++i)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001162 aup->mii_bus->irq[i] = PHY_POLL;
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001163 /* if known, set corresponding PHY IRQs */
Florian Fainellibd2302c2009-11-10 01:13:38 +01001164 if (aup->phy_static_config)
1165 if (aup->phy_irq && aup->phy_busid == aup->mac_id)
1166 aup->mii_bus->irq[aup->phy_addr] = aup->phy_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Florian Fainellibd2302c2009-11-10 01:13:38 +01001168 err = mdiobus_register(aup->mii_bus);
1169 if (err) {
1170 printk(KERN_ERR DRV_NAME " failed to register MDIO bus\n");
1171 goto err_mdiobus_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173
Florian Fainellieb049632010-04-06 22:09:01 +00001174 if (au1000_mii_probe(dev) != 0)
Florian Fainellibd2302c2009-11-10 01:13:38 +01001175 goto err_out;
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 pDBfree = NULL;
1178 /* setup the data buffer descriptors and attach a buffer to each one */
1179 pDB = aup->db;
1180 for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) {
1181 pDB->pnext = pDBfree;
1182 pDBfree = pDB;
1183 pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i);
1184 pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
1185 pDB++;
1186 }
1187 aup->pDBfree = pDBfree;
1188
1189 for (i = 0; i < NUM_RX_DMA; i++) {
Florian Fainellieb049632010-04-06 22:09:01 +00001190 pDB = au1000_GetFreeDB(aup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (!pDB) {
1192 goto err_out;
1193 }
1194 aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1195 aup->rx_db_inuse[i] = pDB;
1196 }
1197 for (i = 0; i < NUM_TX_DMA; i++) {
Florian Fainellieb049632010-04-06 22:09:01 +00001198 pDB = au1000_GetFreeDB(aup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (!pDB) {
1200 goto err_out;
1201 }
1202 aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1203 aup->tx_dma_ring[i]->len = 0;
1204 aup->tx_db_inuse[i] = pDB;
1205 }
1206
Florian Fainellibd2302c2009-11-10 01:13:38 +01001207 dev->base_addr = base->start;
1208 dev->irq = irq;
1209 dev->netdev_ops = &au1000_netdev_ops;
1210 SET_ETHTOOL_OPS(dev, &au1000_ethtool_ops);
1211 dev->watchdog_timeo = ETH_TX_TIMEOUT;
1212
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001213 /*
1214 * The boot code uses the ethernet controller, so reset it to start
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 * fresh. au1000_init() expects that the device is in reset state.
1216 */
Florian Fainellieb049632010-04-06 22:09:01 +00001217 au1000_reset_mac(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Florian Fainellibd2302c2009-11-10 01:13:38 +01001219 err = register_netdev(dev);
1220 if (err) {
1221 printk(KERN_ERR DRV_NAME "%s: Cannot register net device, aborting.\n",
1222 dev->name);
1223 goto err_out;
1224 }
1225
1226 printk("%s: Au1xx0 Ethernet found at 0x%lx, irq %d\n",
1227 dev->name, (unsigned long)base->start, irq);
1228 if (version_printed++ == 0)
1229 printk("%s version %s %s\n", DRV_NAME, DRV_VERSION, DRV_AUTHOR);
1230
1231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233err_out:
Florian Fainellibd2302c2009-11-10 01:13:38 +01001234 if (aup->mii_bus != NULL)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001235 mdiobus_unregister(aup->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /* here we should have a valid dev plus aup-> register addresses
1238 * so we can reset the mac properly.*/
Florian Fainellieb049632010-04-06 22:09:01 +00001239 au1000_reset_mac(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 for (i = 0; i < NUM_RX_DMA; i++) {
1242 if (aup->rx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001243 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245 for (i = 0; i < NUM_TX_DMA; i++) {
1246 if (aup->tx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001247 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
Florian Fainellibd2302c2009-11-10 01:13:38 +01001249err_mdiobus_reg:
1250 mdiobus_free(aup->mii_bus);
1251err_mdiobus_alloc:
1252 iounmap(aup->enable);
1253err_remap2:
1254 iounmap(aup->mac);
1255err_remap1:
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001256 dma_free_noncoherent(NULL, MAX_BUF_SIZE * (NUM_TX_BUFFS + NUM_RX_BUFFS),
1257 (void *)aup->vaddr, aup->dma_addr);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001258err_vaddr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 free_netdev(dev);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001260err_alloc:
1261 release_mem_region(macen->start, resource_size(macen));
1262err_request:
1263 release_mem_region(base->start, resource_size(base));
1264out:
1265 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Florian Fainellibd2302c2009-11-10 01:13:38 +01001268static int __devexit au1000_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Florian Fainellibd2302c2009-11-10 01:13:38 +01001270 struct net_device *dev = platform_get_drvdata(pdev);
1271 struct au1000_private *aup = netdev_priv(dev);
1272 int i;
1273 struct resource *base, *macen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Florian Fainellibd2302c2009-11-10 01:13:38 +01001275 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Florian Fainellibd2302c2009-11-10 01:13:38 +01001277 unregister_netdev(dev);
1278 mdiobus_unregister(aup->mii_bus);
1279 mdiobus_free(aup->mii_bus);
1280
1281 for (i = 0; i < NUM_RX_DMA; i++)
1282 if (aup->rx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001283 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001284
1285 for (i = 0; i < NUM_TX_DMA; i++)
1286 if (aup->tx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001287 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001288
1289 dma_free_noncoherent(NULL, MAX_BUF_SIZE *
1290 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1291 (void *)aup->vaddr, aup->dma_addr);
1292
1293 iounmap(aup->mac);
1294 iounmap(aup->enable);
1295
1296 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1297 release_mem_region(base->start, resource_size(base));
1298
1299 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1300 release_mem_region(macen->start, resource_size(macen));
1301
1302 free_netdev(dev);
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return 0;
1305}
1306
Florian Fainellibd2302c2009-11-10 01:13:38 +01001307static struct platform_driver au1000_eth_driver = {
1308 .probe = au1000_probe,
1309 .remove = __devexit_p(au1000_remove),
1310 .driver = {
1311 .name = "au1000-eth",
1312 .owner = THIS_MODULE,
1313 },
1314};
1315MODULE_ALIAS("platform:au1000-eth");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Florian Fainellibd2302c2009-11-10 01:13:38 +01001317
1318static int __init au1000_init_module(void)
1319{
1320 return platform_driver_register(&au1000_eth_driver);
1321}
1322
1323static void __exit au1000_exit_module(void)
1324{
1325 platform_driver_unregister(&au1000_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328module_init(au1000_init_module);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001329module_exit(au1000_exit_module);