blob: 7d40177de399f32877892e2e6f52991695b9f1d4 [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
Sergei Shtylyov89be0502006-04-19 22:46:21 +040078#define DRV_NAME "au1000_eth"
Andy Gospodarekd5b20692006-09-11 17:39:18 -040079#define DRV_VERSION "1.6"
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define DRV_AUTHOR "Pete Popov <ppopov@embeddedalley.com>"
81#define DRV_DESC "Au1xxx on-chip Ethernet driver"
82
83MODULE_AUTHOR(DRV_AUTHOR);
84MODULE_DESCRIPTION(DRV_DESC);
85MODULE_LICENSE("GPL");
Florian Fainelli13130c72010-04-06 22:08:57 +000086MODULE_VERSION(DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * Theory of operation
90 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -040091 * The Au1000 MACs use a simple rx and tx descriptor ring scheme.
92 * There are four receive and four transmit descriptors. These
93 * descriptors are not in memory; rather, they are just a set of
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * hardware registers.
95 *
96 * Since the Au1000 has a coherent data cache, the receive and
Jeff Garzik6aa20a22006-09-13 13:24:59 -040097 * transmit buffers are allocated from the KSEG0 segment. The
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * hardware registers, however, are still mapped at KSEG1 to
99 * make sure there's no out-of-order writes, and that all writes
100 * complete immediately.
101 */
102
103/* These addresses are only used if yamon doesn't tell us what
104 * the mac address is, and the mac address is not passed on the
105 * command line.
106 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400107static unsigned char au1000_mac_addr[6] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 0x00, 0x50, 0xc2, 0x0c, 0x30, 0x00
109};
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111struct au1000_private *au_macs[NUM_ETH_INTERFACES];
112
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200113/*
114 * board-specific configurations
115 *
116 * PHY detection algorithm
117 *
Florian Fainellibd2302c2009-11-10 01:13:38 +0100118 * If phy_static_config is undefined, the PHY setup is
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200119 * autodetected:
120 *
121 * mii_probe() first searches the current MAC's MII bus for a PHY,
Florian Fainellibd2302c2009-11-10 01:13:38 +0100122 * selecting the first (or last, if phy_search_highest_addr is
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200123 * defined) PHY address not already claimed by another netdev.
124 *
125 * If nothing was found that way when searching for the 2nd ethernet
Florian Fainellibd2302c2009-11-10 01:13:38 +0100126 * controller's PHY and phy1_search_mac0 is defined, then
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200127 * the first MII bus is searched as well for an unclaimed PHY; this is
128 * needed in case of a dual-PHY accessible only through the MAC0's MII
129 * bus.
130 *
131 * Finally, if no PHY is found, then the corresponding ethernet
132 * controller is not registered to the network subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134
Florian Fainellibd2302c2009-11-10 01:13:38 +0100135/* autodetection defaults: phy1_search_mac0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200137/* static PHY setup
138 *
139 * most boards PHY setup should be detectable properly with the
140 * autodetection algorithm in mii_probe(), but in some cases (e.g. if
141 * you have a switch attached, or want to use the PHY's interrupt
142 * notification capabilities) you can provide a static PHY
143 * configuration here
144 *
145 * IRQs may only be set, if a PHY address was configured
146 * If a PHY address is given, also a bus id is required to be set
147 *
148 * ps: make sure the used irqs are configured properly in the board
149 * specific irq-map
150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Florian Fainellieb049632010-04-06 22:09:01 +0000152static void au1000_enable_mac(struct net_device *dev, int force_reset)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800153{
154 unsigned long flags;
155 struct au1000_private *aup = netdev_priv(dev);
156
157 spin_lock_irqsave(&aup->lock, flags);
158
159 if(force_reset || (!aup->mac_enabled)) {
160 *aup->enable = MAC_EN_CLOCK_ENABLE;
161 au_sync_delay(2);
162 *aup->enable = (MAC_EN_RESET0 | MAC_EN_RESET1 | MAC_EN_RESET2
163 | MAC_EN_CLOCK_ENABLE);
164 au_sync_delay(2);
165
166 aup->mac_enabled = 1;
167 }
168
169 spin_unlock_irqrestore(&aup->lock, flags);
170}
171
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200172/*
173 * MII operations
174 */
Adrian Bunk1210dde2008-10-12 21:02:19 -0700175static int au1000_mdio_read(struct net_device *dev, int phy_addr, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Wang Chen454d7c92008-11-12 23:37:49 -0800177 struct au1000_private *aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200178 volatile u32 *const mii_control_reg = &aup->mac->mii_control;
179 volatile u32 *const mii_data_reg = &aup->mac->mii_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 u32 timedout = 20;
181 u32 mii_control;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 while (*mii_control_reg & MAC_MII_BUSY) {
184 mdelay(1);
185 if (--timedout == 0) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400186 printk(KERN_ERR "%s: read_MII busy timeout!!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 dev->name);
188 return -1;
189 }
190 }
191
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400192 mii_control = MAC_SET_MII_SELECT_REG(reg) |
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200193 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 *mii_control_reg = mii_control;
196
197 timedout = 20;
198 while (*mii_control_reg & MAC_MII_BUSY) {
199 mdelay(1);
200 if (--timedout == 0) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400201 printk(KERN_ERR "%s: mdio_read busy timeout!!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 dev->name);
203 return -1;
204 }
205 }
206 return (int)*mii_data_reg;
207}
208
Adrian Bunk1210dde2008-10-12 21:02:19 -0700209static void au1000_mdio_write(struct net_device *dev, int phy_addr,
210 int reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Wang Chen454d7c92008-11-12 23:37:49 -0800212 struct au1000_private *aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200213 volatile u32 *const mii_control_reg = &aup->mac->mii_control;
214 volatile u32 *const mii_data_reg = &aup->mac->mii_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 u32 timedout = 20;
216 u32 mii_control;
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 while (*mii_control_reg & MAC_MII_BUSY) {
219 mdelay(1);
220 if (--timedout == 0) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400221 printk(KERN_ERR "%s: mdio_write busy timeout!!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 dev->name);
223 return;
224 }
225 }
226
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400227 mii_control = MAC_SET_MII_SELECT_REG(reg) |
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200228 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 *mii_data_reg = value;
231 *mii_control_reg = mii_control;
232}
233
Adrian Bunk1210dde2008-10-12 21:02:19 -0700234static int au1000_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200236 /* WARNING: bus->phy_map[phy_addr].attached_dev == dev does
237 * _NOT_ hold (e.g. when PHY is accessed through other MAC's MII bus) */
238 struct net_device *const dev = bus->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Florian Fainellieb049632010-04-06 22:09:01 +0000240 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200241 * mii_bus is enabled */
Adrian Bunk1210dde2008-10-12 21:02:19 -0700242 return au1000_mdio_read(dev, phy_addr, regnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Adrian Bunk1210dde2008-10-12 21:02:19 -0700245static int au1000_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
246 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200248 struct net_device *const dev = bus->priv;
249
Florian Fainellieb049632010-04-06 22:09:01 +0000250 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200251 * mii_bus is enabled */
Adrian Bunk1210dde2008-10-12 21:02:19 -0700252 au1000_mdio_write(dev, phy_addr, regnum, value);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200253 return 0;
254}
255
Adrian Bunk1210dde2008-10-12 21:02:19 -0700256static int au1000_mdiobus_reset(struct mii_bus *bus)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200257{
258 struct net_device *const dev = bus->priv;
259
Florian Fainellieb049632010-04-06 22:09:01 +0000260 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200261 * mii_bus is enabled */
262 return 0;
263}
264
Florian Fainellieb049632010-04-06 22:09:01 +0000265static void au1000_hard_stop(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800266{
267 struct au1000_private *aup = netdev_priv(dev);
268
269 if (au1000_debug > 4)
270 printk(KERN_INFO "%s: hard stop\n", dev->name);
271
272 aup->mac->control &= ~(MAC_RX_ENABLE | MAC_TX_ENABLE);
273 au_sync_delay(10);
274}
275
Florian Fainellieb049632010-04-06 22:09:01 +0000276static void au1000_enable_rx_tx(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800277{
278 struct au1000_private *aup = netdev_priv(dev);
279
280 if (au1000_debug > 4)
281 printk(KERN_INFO "%s: enable_rx_tx\n", dev->name);
282
283 aup->mac->control |= (MAC_RX_ENABLE | MAC_TX_ENABLE);
284 au_sync_delay(10);
285}
286
287static void
288au1000_adjust_link(struct net_device *dev)
289{
290 struct au1000_private *aup = netdev_priv(dev);
291 struct phy_device *phydev = aup->phy_dev;
292 unsigned long flags;
293
294 int status_change = 0;
295
296 BUG_ON(!aup->phy_dev);
297
298 spin_lock_irqsave(&aup->lock, flags);
299
300 if (phydev->link && (aup->old_speed != phydev->speed)) {
301 // speed changed
302
303 switch(phydev->speed) {
304 case SPEED_10:
305 case SPEED_100:
306 break;
307 default:
308 printk(KERN_WARNING
309 "%s: Speed (%d) is not 10/100 ???\n",
310 dev->name, phydev->speed);
311 break;
312 }
313
314 aup->old_speed = phydev->speed;
315
316 status_change = 1;
317 }
318
319 if (phydev->link && (aup->old_duplex != phydev->duplex)) {
320 // duplex mode changed
321
322 /* switching duplex mode requires to disable rx and tx! */
Florian Fainellieb049632010-04-06 22:09:01 +0000323 au1000_hard_stop(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800324
325 if (DUPLEX_FULL == phydev->duplex)
326 aup->mac->control = ((aup->mac->control
327 | MAC_FULL_DUPLEX)
328 & ~MAC_DISABLE_RX_OWN);
329 else
330 aup->mac->control = ((aup->mac->control
331 & ~MAC_FULL_DUPLEX)
332 | MAC_DISABLE_RX_OWN);
333 au_sync_delay(1);
334
Florian Fainellieb049632010-04-06 22:09:01 +0000335 au1000_enable_rx_tx(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800336 aup->old_duplex = phydev->duplex;
337
338 status_change = 1;
339 }
340
341 if(phydev->link != aup->old_link) {
342 // link state changed
343
344 if (!phydev->link) {
345 /* link went down */
346 aup->old_speed = 0;
347 aup->old_duplex = -1;
348 }
349
350 aup->old_link = phydev->link;
351 status_change = 1;
352 }
353
354 spin_unlock_irqrestore(&aup->lock, flags);
355
356 if (status_change) {
357 if (phydev->link)
358 printk(KERN_INFO "%s: link up (%d/%s)\n",
359 dev->name, phydev->speed,
360 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
361 else
362 printk(KERN_INFO "%s: link down\n", dev->name);
363 }
364}
365
Florian Fainellieb049632010-04-06 22:09:01 +0000366static int au1000_mii_probe (struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200367{
Wang Chen454d7c92008-11-12 23:37:49 -0800368 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200369 struct phy_device *phydev = NULL;
370
Florian Fainellibd2302c2009-11-10 01:13:38 +0100371 if (aup->phy_static_config) {
372 BUG_ON(aup->mac_id < 0 || aup->mac_id > 1);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200373
Florian Fainellibd2302c2009-11-10 01:13:38 +0100374 if (aup->phy_addr)
375 phydev = aup->mii_bus->phy_map[aup->phy_addr];
376 else
377 printk (KERN_INFO DRV_NAME ":%s: using PHY-less setup\n",
378 dev->name);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200379 return 0;
Florian Fainellibd2302c2009-11-10 01:13:38 +0100380 } else {
381 int phy_addr;
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200382
Florian Fainellibd2302c2009-11-10 01:13:38 +0100383 /* find the first (lowest address) PHY on the current MAC's MII bus */
384 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
385 if (aup->mii_bus->phy_map[phy_addr]) {
386 phydev = aup->mii_bus->phy_map[phy_addr];
387 if (!aup->phy_search_highest_addr)
388 break; /* break out with first one found */
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Florian Fainellibd2302c2009-11-10 01:13:38 +0100391 if (aup->phy1_search_mac0) {
392 /* try harder to find a PHY */
393 if (!phydev && (aup->mac_id == 1)) {
394 /* no PHY found, maybe we have a dual PHY? */
395 printk (KERN_INFO DRV_NAME ": no PHY found on MAC1, "
396 "let's see if it's attached to MAC0...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Florian Fainellibd2302c2009-11-10 01:13:38 +0100398 /* find the first (lowest address) non-attached PHY on
399 * the MAC0 MII bus */
400 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
401 struct phy_device *const tmp_phydev =
402 aup->mii_bus->phy_map[phy_addr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Florian Fainellibd2302c2009-11-10 01:13:38 +0100404 if (aup->mac_id == 1)
405 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Florian Fainellibd2302c2009-11-10 01:13:38 +0100407 if (!tmp_phydev)
408 continue; /* no PHY here... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Florian Fainellibd2302c2009-11-10 01:13:38 +0100410 if (tmp_phydev->attached_dev)
411 continue; /* already claimed by MAC0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Florian Fainellibd2302c2009-11-10 01:13:38 +0100413 phydev = tmp_phydev;
414 break; /* found it */
415 }
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200420 if (!phydev) {
421 printk (KERN_ERR DRV_NAME ":%s: no PHY found\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return -1;
423 }
424
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200425 /* now we are supposed to have a proper phydev, to attach to... */
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200426 BUG_ON(phydev->attached_dev);
427
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800428 phydev = phy_connect(dev, dev_name(&phydev->dev), &au1000_adjust_link,
429 0, PHY_INTERFACE_MODE_MII);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200430
431 if (IS_ERR(phydev)) {
432 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
433 return PTR_ERR(phydev);
434 }
435
436 /* mask with MAC supported features */
437 phydev->supported &= (SUPPORTED_10baseT_Half
438 | SUPPORTED_10baseT_Full
439 | SUPPORTED_100baseT_Half
440 | SUPPORTED_100baseT_Full
441 | SUPPORTED_Autoneg
442 /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
443 | SUPPORTED_MII
444 | SUPPORTED_TP);
445
446 phydev->advertising = phydev->supported;
447
448 aup->old_link = 0;
449 aup->old_speed = 0;
450 aup->old_duplex = -1;
451 aup->phy_dev = phydev;
452
453 printk(KERN_INFO "%s: attached PHY driver [%s] "
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800454 "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
455 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 return 0;
458}
459
460
461/*
462 * Buffer allocation/deallocation routines. The buffer descriptor returned
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400463 * has the virtual and dma address of a buffer suitable for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * both, receive and transmit operations.
465 */
Florian Fainellieb049632010-04-06 22:09:01 +0000466static db_dest_t *au1000_GetFreeDB(struct au1000_private *aup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 db_dest_t *pDB;
469 pDB = aup->pDBfree;
470
471 if (pDB) {
472 aup->pDBfree = pDB->pnext;
473 }
474 return pDB;
475}
476
Florian Fainellieb049632010-04-06 22:09:01 +0000477void au1000_ReleaseDB(struct au1000_private *aup, db_dest_t *pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 db_dest_t *pDBfree = aup->pDBfree;
480 if (pDBfree)
481 pDBfree->pnext = pDB;
482 aup->pDBfree = pDB;
483}
484
Florian Fainellieb049632010-04-06 22:09:01 +0000485static void au1000_reset_mac_unlocked(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200486{
Wang Chen454d7c92008-11-12 23:37:49 -0800487 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200488 int i;
489
Florian Fainellieb049632010-04-06 22:09:01 +0000490 au1000_hard_stop(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200491
492 *aup->enable = MAC_EN_CLOCK_ENABLE;
493 au_sync_delay(2);
494 *aup->enable = 0;
495 au_sync_delay(2);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 aup->tx_full = 0;
498 for (i = 0; i < NUM_RX_DMA; i++) {
499 /* reset control bits */
500 aup->rx_dma_ring[i]->buff_stat &= ~0xf;
501 }
502 for (i = 0; i < NUM_TX_DMA; i++) {
503 /* reset control bits */
504 aup->tx_dma_ring[i]->buff_stat &= ~0xf;
505 }
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200506
507 aup->mac_enabled = 0;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Florian Fainellieb049632010-04-06 22:09:01 +0000511static void au1000_reset_mac(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200512{
Wang Chen454d7c92008-11-12 23:37:49 -0800513 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200514 unsigned long flags;
515
516 if (au1000_debug > 4)
517 printk(KERN_INFO "%s: reset mac, aup %x\n",
518 dev->name, (unsigned)aup);
519
520 spin_lock_irqsave(&aup->lock, flags);
521
Florian Fainellieb049632010-04-06 22:09:01 +0000522 au1000_reset_mac_unlocked (dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200523
524 spin_unlock_irqrestore(&aup->lock, flags);
525}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400527/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * Setup the receive and transmit "rings". These pointers are the addresses
529 * of the rx and tx MAC DMA registers so they are fixed by the hardware --
530 * these are not descriptors sitting in memory.
531 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400532static void
Florian Fainellieb049632010-04-06 22:09:01 +0000533au1000_setup_hw_rings(struct au1000_private *aup, u32 rx_base, u32 tx_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 int i;
536
537 for (i = 0; i < NUM_RX_DMA; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400538 aup->rx_dma_ring[i] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 (volatile rx_dma_t *) (rx_base + sizeof(rx_dma_t)*i);
540 }
541 for (i = 0; i < NUM_TX_DMA; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400542 aup->tx_dma_ring[i] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 (volatile tx_dma_t *) (tx_base + sizeof(tx_dma_t)*i);
544 }
545}
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/*
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200548 * ethtool operations
549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551static int au1000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
552{
Wang Chen454d7c92008-11-12 23:37:49 -0800553 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200555 if (aup->phy_dev)
556 return phy_ethtool_gset(aup->phy_dev, cmd);
557
558 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561static int au1000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
562{
Wang Chen454d7c92008-11-12 23:37:49 -0800563 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200565 if (!capable(CAP_NET_ADMIN))
566 return -EPERM;
567
568 if (aup->phy_dev)
569 return phy_ethtool_sset(aup->phy_dev, cmd);
570
571 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574static void
575au1000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
576{
Wang Chen454d7c92008-11-12 23:37:49 -0800577 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 strcpy(info->driver, DRV_NAME);
580 strcpy(info->version, DRV_VERSION);
581 info->fw_version[0] = '\0';
582 sprintf(info->bus_info, "%s %d", DRV_NAME, aup->mac_id);
583 info->regdump_len = 0;
584}
585
Jeff Garzik7282d492006-09-13 14:30:00 -0400586static const struct ethtool_ops au1000_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 .get_settings = au1000_get_settings,
588 .set_settings = au1000_set_settings,
589 .get_drvinfo = au1000_get_drvinfo,
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200590 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591};
592
Florian Fainelli5ef30412009-01-22 14:06:25 -0800593
594/*
595 * Initialize the interface.
596 *
597 * When the device powers up, the clocks are disabled and the
598 * mac is in reset state. When the interface is closed, we
599 * do the same -- reset the device and disable the clocks to
600 * conserve power. Thus, whenever au1000_init() is called,
601 * the device should already be in reset state.
602 */
603static int au1000_init(struct net_device *dev)
604{
605 struct au1000_private *aup = netdev_priv(dev);
606 unsigned long flags;
607 int i;
608 u32 control;
609
610 if (au1000_debug > 4)
611 printk("%s: au1000_init\n", dev->name);
612
613 /* bring the device out of reset */
Florian Fainellieb049632010-04-06 22:09:01 +0000614 au1000_enable_mac(dev, 1);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800615
616 spin_lock_irqsave(&aup->lock, flags);
617
618 aup->mac->control = 0;
619 aup->tx_head = (aup->tx_dma_ring[0]->buff_stat & 0xC) >> 2;
620 aup->tx_tail = aup->tx_head;
621 aup->rx_head = (aup->rx_dma_ring[0]->buff_stat & 0xC) >> 2;
622
623 aup->mac->mac_addr_high = dev->dev_addr[5]<<8 | dev->dev_addr[4];
624 aup->mac->mac_addr_low = dev->dev_addr[3]<<24 | dev->dev_addr[2]<<16 |
625 dev->dev_addr[1]<<8 | dev->dev_addr[0];
626
627 for (i = 0; i < NUM_RX_DMA; i++) {
628 aup->rx_dma_ring[i]->buff_stat |= RX_DMA_ENABLE;
629 }
630 au_sync();
631
632 control = MAC_RX_ENABLE | MAC_TX_ENABLE;
633#ifndef CONFIG_CPU_LITTLE_ENDIAN
634 control |= MAC_BIG_ENDIAN;
635#endif
636 if (aup->phy_dev) {
637 if (aup->phy_dev->link && (DUPLEX_FULL == aup->phy_dev->duplex))
638 control |= MAC_FULL_DUPLEX;
639 else
640 control |= MAC_DISABLE_RX_OWN;
641 } else { /* PHY-less op, assume full-duplex */
642 control |= MAC_FULL_DUPLEX;
643 }
644
645 aup->mac->control = control;
646 aup->mac->vlan1_tag = 0x8100; /* activate vlan support */
647 au_sync();
648
649 spin_unlock_irqrestore(&aup->lock, flags);
650 return 0;
651}
652
Florian Fainellieb049632010-04-06 22:09:01 +0000653static inline void au1000_update_rx_stats(struct net_device *dev, u32 status)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800654{
Florian Fainelli5ef30412009-01-22 14:06:25 -0800655 struct net_device_stats *ps = &dev->stats;
656
657 ps->rx_packets++;
658 if (status & RX_MCAST_FRAME)
659 ps->multicast++;
660
661 if (status & RX_ERROR) {
662 ps->rx_errors++;
663 if (status & RX_MISSED_FRAME)
664 ps->rx_missed_errors++;
roel kluin4989ccb2009-10-06 09:54:18 +0000665 if (status & (RX_OVERLEN | RX_RUNT | RX_LEN_ERROR))
Florian Fainelli5ef30412009-01-22 14:06:25 -0800666 ps->rx_length_errors++;
667 if (status & RX_CRC_ERROR)
668 ps->rx_crc_errors++;
669 if (status & RX_COLL)
670 ps->collisions++;
671 }
672 else
673 ps->rx_bytes += status & RX_FRAME_LEN_MASK;
674
675}
676
677/*
678 * Au1000 receive routine.
679 */
680static int au1000_rx(struct net_device *dev)
681{
682 struct au1000_private *aup = netdev_priv(dev);
683 struct sk_buff *skb;
684 volatile rx_dma_t *prxd;
685 u32 buff_stat, status;
686 db_dest_t *pDB;
687 u32 frmlen;
688
689 if (au1000_debug > 5)
690 printk("%s: au1000_rx head %d\n", dev->name, aup->rx_head);
691
692 prxd = aup->rx_dma_ring[aup->rx_head];
693 buff_stat = prxd->buff_stat;
694 while (buff_stat & RX_T_DONE) {
695 status = prxd->status;
696 pDB = aup->rx_db_inuse[aup->rx_head];
Florian Fainellieb049632010-04-06 22:09:01 +0000697 au1000_update_rx_stats(dev, status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800698 if (!(status & RX_ERROR)) {
699
700 /* good frame */
701 frmlen = (status & RX_FRAME_LEN_MASK);
702 frmlen -= 4; /* Remove FCS */
703 skb = dev_alloc_skb(frmlen + 2);
704 if (skb == NULL) {
705 printk(KERN_ERR
706 "%s: Memory squeeze, dropping packet.\n",
707 dev->name);
708 dev->stats.rx_dropped++;
709 continue;
710 }
711 skb_reserve(skb, 2); /* 16 byte IP header align */
712 skb_copy_to_linear_data(skb,
713 (unsigned char *)pDB->vaddr, frmlen);
714 skb_put(skb, frmlen);
715 skb->protocol = eth_type_trans(skb, dev);
716 netif_rx(skb); /* pass the packet to upper layers */
717 }
718 else {
719 if (au1000_debug > 4) {
720 if (status & RX_MISSED_FRAME)
721 printk("rx miss\n");
722 if (status & RX_WDOG_TIMER)
723 printk("rx wdog\n");
724 if (status & RX_RUNT)
725 printk("rx runt\n");
726 if (status & RX_OVERLEN)
727 printk("rx overlen\n");
728 if (status & RX_COLL)
729 printk("rx coll\n");
730 if (status & RX_MII_ERROR)
731 printk("rx mii error\n");
732 if (status & RX_CRC_ERROR)
733 printk("rx crc error\n");
734 if (status & RX_LEN_ERROR)
735 printk("rx len error\n");
736 if (status & RX_U_CNTRL_FRAME)
737 printk("rx u control frame\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800738 }
739 }
740 prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE);
741 aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1);
742 au_sync();
743
744 /* next descriptor */
745 prxd = aup->rx_dma_ring[aup->rx_head];
746 buff_stat = prxd->buff_stat;
747 }
748 return 0;
749}
750
Florian Fainellieb049632010-04-06 22:09:01 +0000751static void au1000_update_tx_stats(struct net_device *dev, u32 status)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800752{
753 struct au1000_private *aup = netdev_priv(dev);
754 struct net_device_stats *ps = &dev->stats;
755
756 if (status & TX_FRAME_ABORTED) {
757 if (!aup->phy_dev || (DUPLEX_FULL == aup->phy_dev->duplex)) {
758 if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) {
759 /* any other tx errors are only valid
760 * in half duplex mode */
761 ps->tx_errors++;
762 ps->tx_aborted_errors++;
763 }
764 }
765 else {
766 ps->tx_errors++;
767 ps->tx_aborted_errors++;
768 if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER))
769 ps->tx_carrier_errors++;
770 }
771 }
772}
773
774/*
775 * Called from the interrupt service routine to acknowledge
776 * the TX DONE bits. This is a must if the irq is setup as
777 * edge triggered.
778 */
779static void au1000_tx_ack(struct net_device *dev)
780{
781 struct au1000_private *aup = netdev_priv(dev);
782 volatile tx_dma_t *ptxd;
783
784 ptxd = aup->tx_dma_ring[aup->tx_tail];
785
786 while (ptxd->buff_stat & TX_T_DONE) {
Florian Fainellieb049632010-04-06 22:09:01 +0000787 au1000_update_tx_stats(dev, ptxd->status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800788 ptxd->buff_stat &= ~TX_T_DONE;
789 ptxd->len = 0;
790 au_sync();
791
792 aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1);
793 ptxd = aup->tx_dma_ring[aup->tx_tail];
794
795 if (aup->tx_full) {
796 aup->tx_full = 0;
797 netif_wake_queue(dev);
798 }
799 }
800}
801
802/*
803 * Au1000 interrupt service routine.
804 */
805static irqreturn_t au1000_interrupt(int irq, void *dev_id)
806{
807 struct net_device *dev = dev_id;
808
809 /* Handle RX interrupts first to minimize chance of overrun */
810
811 au1000_rx(dev);
812 au1000_tx_ack(dev);
813 return IRQ_RETVAL(1);
814}
815
816static int au1000_open(struct net_device *dev)
817{
818 int retval;
819 struct au1000_private *aup = netdev_priv(dev);
820
821 if (au1000_debug > 4)
822 printk("%s: open: dev=%p\n", dev->name, dev);
823
Joe Perchesa0607fd2009-11-18 23:29:17 -0800824 if ((retval = request_irq(dev->irq, au1000_interrupt, 0,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800825 dev->name, dev))) {
826 printk(KERN_ERR "%s: unable to get IRQ %d\n",
827 dev->name, dev->irq);
828 return retval;
829 }
830
831 if ((retval = au1000_init(dev))) {
832 printk(KERN_ERR "%s: error in au1000_init\n", dev->name);
833 free_irq(dev->irq, dev);
834 return retval;
835 }
836
837 if (aup->phy_dev) {
838 /* cause the PHY state machine to schedule a link state check */
839 aup->phy_dev->state = PHY_CHANGELINK;
840 phy_start(aup->phy_dev);
841 }
842
843 netif_start_queue(dev);
844
845 if (au1000_debug > 4)
846 printk("%s: open: Initialization done.\n", dev->name);
847
848 return 0;
849}
850
851static int au1000_close(struct net_device *dev)
852{
853 unsigned long flags;
854 struct au1000_private *const aup = netdev_priv(dev);
855
856 if (au1000_debug > 4)
857 printk("%s: close: dev=%p\n", dev->name, dev);
858
859 if (aup->phy_dev)
860 phy_stop(aup->phy_dev);
861
862 spin_lock_irqsave(&aup->lock, flags);
863
Florian Fainellieb049632010-04-06 22:09:01 +0000864 au1000_reset_mac_unlocked (dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800865
866 /* stop the device */
867 netif_stop_queue(dev);
868
869 /* disable the interrupt */
870 free_irq(dev->irq, dev);
871 spin_unlock_irqrestore(&aup->lock, flags);
872
873 return 0;
874}
875
876/*
877 * Au1000 transmit routine.
878 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000879static netdev_tx_t au1000_tx(struct sk_buff *skb, struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800880{
881 struct au1000_private *aup = netdev_priv(dev);
882 struct net_device_stats *ps = &dev->stats;
883 volatile tx_dma_t *ptxd;
884 u32 buff_stat;
885 db_dest_t *pDB;
886 int i;
887
888 if (au1000_debug > 5)
889 printk("%s: tx: aup %x len=%d, data=%p, head %d\n",
890 dev->name, (unsigned)aup, skb->len,
891 skb->data, aup->tx_head);
892
893 ptxd = aup->tx_dma_ring[aup->tx_head];
894 buff_stat = ptxd->buff_stat;
895 if (buff_stat & TX_DMA_ENABLE) {
896 /* We've wrapped around and the transmitter is still busy */
897 netif_stop_queue(dev);
898 aup->tx_full = 1;
Patrick McHardy5b548142009-06-12 06:22:29 +0000899 return NETDEV_TX_BUSY;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800900 }
901 else if (buff_stat & TX_T_DONE) {
Florian Fainellieb049632010-04-06 22:09:01 +0000902 au1000_update_tx_stats(dev, ptxd->status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800903 ptxd->len = 0;
904 }
905
906 if (aup->tx_full) {
907 aup->tx_full = 0;
908 netif_wake_queue(dev);
909 }
910
911 pDB = aup->tx_db_inuse[aup->tx_head];
Florian Fainellibd2302c2009-11-10 01:13:38 +0100912 skb_copy_from_linear_data(skb, (void *)pDB->vaddr, skb->len);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800913 if (skb->len < ETH_ZLEN) {
914 for (i=skb->len; i<ETH_ZLEN; i++) {
915 ((char *)pDB->vaddr)[i] = 0;
916 }
917 ptxd->len = ETH_ZLEN;
918 }
919 else
920 ptxd->len = skb->len;
921
922 ps->tx_packets++;
923 ps->tx_bytes += ptxd->len;
924
925 ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE;
926 au_sync();
927 dev_kfree_skb(skb);
928 aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1);
929 dev->trans_start = jiffies;
Patrick McHardy6ed10652009-06-23 06:03:08 +0000930 return NETDEV_TX_OK;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800931}
932
933/*
934 * The Tx ring has been full longer than the watchdog timeout
935 * value. The transmitter must be hung?
936 */
937static void au1000_tx_timeout(struct net_device *dev)
938{
939 printk(KERN_ERR "%s: au1000_tx_timeout: dev=%p\n", dev->name, dev);
Florian Fainellieb049632010-04-06 22:09:01 +0000940 au1000_reset_mac(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800941 au1000_init(dev);
942 dev->trans_start = jiffies;
943 netif_wake_queue(dev);
944}
945
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +0000946static void au1000_multicast_list(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800947{
948 struct au1000_private *aup = netdev_priv(dev);
949
950 if (au1000_debug > 4)
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +0000951 printk("%s: au1000_multicast_list: flags=%x\n", dev->name, dev->flags);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800952
953 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
954 aup->mac->control |= MAC_PROMISCUOUS;
955 } else if ((dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000956 netdev_mc_count(dev) > MULTICAST_FILTER_LIMIT) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800957 aup->mac->control |= MAC_PASS_ALL_MULTI;
958 aup->mac->control &= ~MAC_PROMISCUOUS;
959 printk(KERN_INFO "%s: Pass all multicast\n", dev->name);
960 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000961 struct netdev_hw_addr *ha;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800962 u32 mc_filter[2]; /* Multicast hash filter */
963
964 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000965 netdev_for_each_mc_addr(ha, dev)
966 set_bit(ether_crc(ETH_ALEN, ha->addr)>>26,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800967 (long *)mc_filter);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800968 aup->mac->multi_hash_high = mc_filter[1];
969 aup->mac->multi_hash_low = mc_filter[0];
970 aup->mac->control &= ~MAC_PROMISCUOUS;
971 aup->mac->control |= MAC_HASH_MODE;
972 }
973}
974
975static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
976{
977 struct au1000_private *aup = netdev_priv(dev);
978
979 if (!netif_running(dev)) return -EINVAL;
980
981 if (!aup->phy_dev) return -EINVAL; // PHY not controllable
982
983 return phy_mii_ioctl(aup->phy_dev, if_mii(rq), cmd);
984}
985
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +0000986static const struct net_device_ops au1000_netdev_ops = {
987 .ndo_open = au1000_open,
988 .ndo_stop = au1000_close,
989 .ndo_start_xmit = au1000_tx,
990 .ndo_set_multicast_list = au1000_multicast_list,
991 .ndo_do_ioctl = au1000_ioctl,
992 .ndo_tx_timeout = au1000_tx_timeout,
993 .ndo_set_mac_address = eth_mac_addr,
994 .ndo_validate_addr = eth_validate_addr,
995 .ndo_change_mtu = eth_change_mtu,
996};
997
Florian Fainellibd2302c2009-11-10 01:13:38 +0100998static int __devinit au1000_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 static unsigned version_printed = 0;
1001 struct au1000_private *aup = NULL;
Florian Fainellibd2302c2009-11-10 01:13:38 +01001002 struct au1000_eth_platform_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 struct net_device *dev = NULL;
1004 db_dest_t *pDB, *pDBfree;
Florian Fainellibd2302c2009-11-10 01:13:38 +01001005 int irq, i, err = 0;
1006 struct resource *base, *macen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 char ethaddr[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Florian Fainellibd2302c2009-11-10 01:13:38 +01001009 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1010 if (!base) {
1011 printk(KERN_ERR DRV_NAME ": failed to retrieve base register\n");
1012 err = -ENODEV;
1013 goto out;
1014 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001015
Florian Fainellibd2302c2009-11-10 01:13:38 +01001016 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1017 if (!macen) {
1018 printk(KERN_ERR DRV_NAME ": failed to retrieve MAC Enable register\n");
1019 err = -ENODEV;
1020 goto out;
1021 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001022
Florian Fainellibd2302c2009-11-10 01:13:38 +01001023 irq = platform_get_irq(pdev, 0);
1024 if (irq < 0) {
1025 printk(KERN_ERR DRV_NAME ": failed to retrieve IRQ\n");
1026 err = -ENODEV;
1027 goto out;
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Florian Fainellibd2302c2009-11-10 01:13:38 +01001030 if (!request_mem_region(base->start, resource_size(base), pdev->name)) {
1031 printk(KERN_ERR DRV_NAME ": failed to request memory region for base registers\n");
1032 err = -ENXIO;
1033 goto out;
1034 }
1035
1036 if (!request_mem_region(macen->start, resource_size(macen), pdev->name)) {
1037 printk(KERN_ERR DRV_NAME ": failed to request memory region for MAC enable register\n");
1038 err = -ENXIO;
1039 goto err_request;
1040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 dev = alloc_etherdev(sizeof(struct au1000_private));
1043 if (!dev) {
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001044 printk(KERN_ERR "%s: alloc_etherdev failed\n", DRV_NAME);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001045 err = -ENOMEM;
1046 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
Florian Fainellibd2302c2009-11-10 01:13:38 +01001049 SET_NETDEV_DEV(dev, &pdev->dev);
1050 platform_set_drvdata(pdev, dev);
Wang Chen454d7c92008-11-12 23:37:49 -08001051 aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Martin Gebert533763d2008-07-23 09:40:09 +02001053 spin_lock_init(&aup->lock);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* Allocate the data buffers */
1056 /* Snooping works fine with eth on all au1xxx */
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001057 aup->vaddr = (u32)dma_alloc_noncoherent(NULL, MAX_BUF_SIZE *
1058 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1059 &aup->dma_addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (!aup->vaddr) {
Florian Fainellibd2302c2009-11-10 01:13:38 +01001061 printk(KERN_ERR DRV_NAME ": failed to allocate data buffers\n");
1062 err = -ENOMEM;
1063 goto err_vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 /* aup->mac is the base address of the MAC's registers */
Florian Fainellibd2302c2009-11-10 01:13:38 +01001067 aup->mac = (volatile mac_reg_t *)ioremap_nocache(base->start, resource_size(base));
1068 if (!aup->mac) {
1069 printk(KERN_ERR DRV_NAME ": failed to ioremap MAC registers\n");
1070 err = -ENXIO;
1071 goto err_remap1;
1072 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001073
Florian Fainellibd2302c2009-11-10 01:13:38 +01001074 /* Setup some variables for quick register address access */
1075 aup->enable = (volatile u32 *)ioremap_nocache(macen->start, resource_size(macen));
1076 if (!aup->enable) {
1077 printk(KERN_ERR DRV_NAME ": failed to ioremap MAC enable register\n");
1078 err = -ENXIO;
1079 goto err_remap2;
1080 }
1081 aup->mac_id = pdev->id;
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001082
Florian Fainellibd2302c2009-11-10 01:13:38 +01001083 if (pdev->id == 0) {
Yoichi Yuasa2de88922007-10-15 19:06:20 +09001084 if (prom_get_ethernet_addr(ethaddr) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 memcpy(au1000_mac_addr, ethaddr, sizeof(au1000_mac_addr));
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001086 else {
Yoichi Yuasa2de88922007-10-15 19:06:20 +09001087 printk(KERN_INFO "%s: No MAC address found\n",
1088 dev->name);
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001089 /* Use the hard coded MAC addresses */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Florian Fainellieb049632010-04-06 22:09:01 +00001092 au1000_setup_hw_rings(aup, MAC0_RX_DMA_ADDR, MAC0_TX_DMA_ADDR);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001093 } else if (pdev->id == 1)
Florian Fainellieb049632010-04-06 22:09:01 +00001094 au1000_setup_hw_rings(aup, MAC1_RX_DMA_ADDR, MAC1_TX_DMA_ADDR);
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001095
1096 /*
1097 * Assign to the Ethernet ports two consecutive MAC addresses
1098 * to match those that are printed on their stickers
1099 */
1100 memcpy(dev->dev_addr, au1000_mac_addr, sizeof(au1000_mac_addr));
Florian Fainellibd2302c2009-11-10 01:13:38 +01001101 dev->dev_addr[5] += pdev->id;
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001102
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001103 *aup->enable = 0;
1104 aup->mac_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Florian Fainellibd2302c2009-11-10 01:13:38 +01001106 pd = pdev->dev.platform_data;
1107 if (!pd) {
1108 printk(KERN_INFO DRV_NAME ": no platform_data passed, PHY search on MAC0\n");
1109 aup->phy1_search_mac0 = 1;
1110 } else {
1111 aup->phy_static_config = pd->phy_static_config;
1112 aup->phy_search_highest_addr = pd->phy_search_highest_addr;
1113 aup->phy1_search_mac0 = pd->phy1_search_mac0;
1114 aup->phy_addr = pd->phy_addr;
1115 aup->phy_busid = pd->phy_busid;
1116 aup->phy_irq = pd->phy_irq;
1117 }
1118
1119 if (aup->phy_busid && aup->phy_busid > 0) {
1120 printk(KERN_ERR DRV_NAME ": MAC0-associated PHY attached 2nd MACs MII"
1121 "bus not supported yet\n");
1122 err = -ENODEV;
1123 goto err_mdiobus_alloc;
1124 }
1125
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001126 aup->mii_bus = mdiobus_alloc();
Florian Fainellibd2302c2009-11-10 01:13:38 +01001127 if (aup->mii_bus == NULL) {
1128 printk(KERN_ERR DRV_NAME ": failed to allocate mdiobus structure\n");
1129 err = -ENOMEM;
1130 goto err_mdiobus_alloc;
1131 }
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001132
1133 aup->mii_bus->priv = dev;
Adrian Bunk1210dde2008-10-12 21:02:19 -07001134 aup->mii_bus->read = au1000_mdiobus_read;
1135 aup->mii_bus->write = au1000_mdiobus_write;
1136 aup->mii_bus->reset = au1000_mdiobus_reset;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001137 aup->mii_bus->name = "au1000_eth_mii";
1138 snprintf(aup->mii_bus->id, MII_BUS_ID_SIZE, "%x", aup->mac_id);
1139 aup->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
roel kluindcbfef82009-08-30 22:40:15 +00001140 if (aup->mii_bus->irq == NULL)
1141 goto err_out;
1142
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001143 for(i = 0; i < PHY_MAX_ADDR; ++i)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001144 aup->mii_bus->irq[i] = PHY_POLL;
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001145 /* if known, set corresponding PHY IRQs */
Florian Fainellibd2302c2009-11-10 01:13:38 +01001146 if (aup->phy_static_config)
1147 if (aup->phy_irq && aup->phy_busid == aup->mac_id)
1148 aup->mii_bus->irq[aup->phy_addr] = aup->phy_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Florian Fainellibd2302c2009-11-10 01:13:38 +01001150 err = mdiobus_register(aup->mii_bus);
1151 if (err) {
1152 printk(KERN_ERR DRV_NAME " failed to register MDIO bus\n");
1153 goto err_mdiobus_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155
Florian Fainellieb049632010-04-06 22:09:01 +00001156 if (au1000_mii_probe(dev) != 0)
Florian Fainellibd2302c2009-11-10 01:13:38 +01001157 goto err_out;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 pDBfree = NULL;
1160 /* setup the data buffer descriptors and attach a buffer to each one */
1161 pDB = aup->db;
1162 for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) {
1163 pDB->pnext = pDBfree;
1164 pDBfree = pDB;
1165 pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i);
1166 pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
1167 pDB++;
1168 }
1169 aup->pDBfree = pDBfree;
1170
1171 for (i = 0; i < NUM_RX_DMA; i++) {
Florian Fainellieb049632010-04-06 22:09:01 +00001172 pDB = au1000_GetFreeDB(aup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (!pDB) {
1174 goto err_out;
1175 }
1176 aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1177 aup->rx_db_inuse[i] = pDB;
1178 }
1179 for (i = 0; i < NUM_TX_DMA; i++) {
Florian Fainellieb049632010-04-06 22:09:01 +00001180 pDB = au1000_GetFreeDB(aup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (!pDB) {
1182 goto err_out;
1183 }
1184 aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1185 aup->tx_dma_ring[i]->len = 0;
1186 aup->tx_db_inuse[i] = pDB;
1187 }
1188
Florian Fainellibd2302c2009-11-10 01:13:38 +01001189 dev->base_addr = base->start;
1190 dev->irq = irq;
1191 dev->netdev_ops = &au1000_netdev_ops;
1192 SET_ETHTOOL_OPS(dev, &au1000_ethtool_ops);
1193 dev->watchdog_timeo = ETH_TX_TIMEOUT;
1194
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001195 /*
1196 * The boot code uses the ethernet controller, so reset it to start
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 * fresh. au1000_init() expects that the device is in reset state.
1198 */
Florian Fainellieb049632010-04-06 22:09:01 +00001199 au1000_reset_mac(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Florian Fainellibd2302c2009-11-10 01:13:38 +01001201 err = register_netdev(dev);
1202 if (err) {
1203 printk(KERN_ERR DRV_NAME "%s: Cannot register net device, aborting.\n",
1204 dev->name);
1205 goto err_out;
1206 }
1207
1208 printk("%s: Au1xx0 Ethernet found at 0x%lx, irq %d\n",
1209 dev->name, (unsigned long)base->start, irq);
1210 if (version_printed++ == 0)
1211 printk("%s version %s %s\n", DRV_NAME, DRV_VERSION, DRV_AUTHOR);
1212
1213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215err_out:
Florian Fainellibd2302c2009-11-10 01:13:38 +01001216 if (aup->mii_bus != NULL)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001217 mdiobus_unregister(aup->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /* here we should have a valid dev plus aup-> register addresses
1220 * so we can reset the mac properly.*/
Florian Fainellieb049632010-04-06 22:09:01 +00001221 au1000_reset_mac(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 for (i = 0; i < NUM_RX_DMA; i++) {
1224 if (aup->rx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001225 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227 for (i = 0; i < NUM_TX_DMA; i++) {
1228 if (aup->tx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001229 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
Florian Fainellibd2302c2009-11-10 01:13:38 +01001231err_mdiobus_reg:
1232 mdiobus_free(aup->mii_bus);
1233err_mdiobus_alloc:
1234 iounmap(aup->enable);
1235err_remap2:
1236 iounmap(aup->mac);
1237err_remap1:
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001238 dma_free_noncoherent(NULL, MAX_BUF_SIZE * (NUM_TX_BUFFS + NUM_RX_BUFFS),
1239 (void *)aup->vaddr, aup->dma_addr);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001240err_vaddr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 free_netdev(dev);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001242err_alloc:
1243 release_mem_region(macen->start, resource_size(macen));
1244err_request:
1245 release_mem_region(base->start, resource_size(base));
1246out:
1247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Florian Fainellibd2302c2009-11-10 01:13:38 +01001250static int __devexit au1000_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Florian Fainellibd2302c2009-11-10 01:13:38 +01001252 struct net_device *dev = platform_get_drvdata(pdev);
1253 struct au1000_private *aup = netdev_priv(dev);
1254 int i;
1255 struct resource *base, *macen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Florian Fainellibd2302c2009-11-10 01:13:38 +01001257 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Florian Fainellibd2302c2009-11-10 01:13:38 +01001259 unregister_netdev(dev);
1260 mdiobus_unregister(aup->mii_bus);
1261 mdiobus_free(aup->mii_bus);
1262
1263 for (i = 0; i < NUM_RX_DMA; i++)
1264 if (aup->rx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001265 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001266
1267 for (i = 0; i < NUM_TX_DMA; i++)
1268 if (aup->tx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001269 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001270
1271 dma_free_noncoherent(NULL, MAX_BUF_SIZE *
1272 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1273 (void *)aup->vaddr, aup->dma_addr);
1274
1275 iounmap(aup->mac);
1276 iounmap(aup->enable);
1277
1278 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1279 release_mem_region(base->start, resource_size(base));
1280
1281 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1282 release_mem_region(macen->start, resource_size(macen));
1283
1284 free_netdev(dev);
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return 0;
1287}
1288
Florian Fainellibd2302c2009-11-10 01:13:38 +01001289static struct platform_driver au1000_eth_driver = {
1290 .probe = au1000_probe,
1291 .remove = __devexit_p(au1000_remove),
1292 .driver = {
1293 .name = "au1000-eth",
1294 .owner = THIS_MODULE,
1295 },
1296};
1297MODULE_ALIAS("platform:au1000-eth");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Florian Fainellibd2302c2009-11-10 01:13:38 +01001299
1300static int __init au1000_init_module(void)
1301{
1302 return platform_driver_register(&au1000_eth_driver);
1303}
1304
1305static void __exit au1000_exit_module(void)
1306{
1307 platform_driver_unregister(&au1000_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310module_init(au1000_init_module);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001311module_exit(au1000_exit_module);