blob: 4bbc537a78708c587d1a494d5beb453d79deb67c [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.
Florian Fainelliec7eabdd2010-09-08 11:11:31 +000016 * ppopov@mvista.com or source@mvista.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
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 */
Florian Fainelli215e17b2010-09-08 11:11:45 +000037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Manuel Laussbc36b422009-10-17 02:00:07 +000039#include <linux/capability.h>
Ralf Baechled791c2b2007-06-24 15:59:54 +020040#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/string.h>
44#include <linux/timer.h>
45#include <linux/errno.h>
46#include <linux/in.h>
47#include <linux/ioport.h>
48#include <linux/bitops.h>
49#include <linux/slab.h>
50#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/init.h>
52#include <linux/netdevice.h>
53#include <linux/etherdevice.h>
54#include <linux/ethtool.h>
55#include <linux/mii.h>
56#include <linux/skbuff.h>
57#include <linux/delay.h>
Herbert Valerio Riedel8cd35da2006-05-01 15:37:09 +020058#include <linux/crc32.h>
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +020059#include <linux/phy.h>
Florian Fainellibd2302c2009-11-10 01:13:38 +010060#include <linux/platform_device.h>
Florian Fainelli49a42c02010-09-08 11:11:49 +000061#include <linux/cpu.h>
62#include <linux/io.h>
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <asm/mipsregs.h>
65#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <asm/processor.h>
67
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090068#include <au1000.h>
Florian Fainellibd2302c2009-11-10 01:13:38 +010069#include <au1xxx_eth.h>
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090070#include <prom.h>
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include "au1000_eth.h"
73
74#ifdef AU1000_ETH_DEBUG
75static int au1000_debug = 5;
76#else
77static int au1000_debug = 3;
78#endif
79
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +000080#define AU1000_DEF_MSG_ENABLE (NETIF_MSG_DRV | \
81 NETIF_MSG_PROBE | \
82 NETIF_MSG_LINK)
83
Sergei Shtylyov89be0502006-04-19 22:46:21 +040084#define DRV_NAME "au1000_eth"
Florian Fainelli8020eb82010-04-06 22:09:20 +000085#define DRV_VERSION "1.7"
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define DRV_AUTHOR "Pete Popov <ppopov@embeddedalley.com>"
87#define DRV_DESC "Au1xxx on-chip Ethernet driver"
88
89MODULE_AUTHOR(DRV_AUTHOR);
90MODULE_DESCRIPTION(DRV_DESC);
91MODULE_LICENSE("GPL");
Florian Fainelli13130c72010-04-06 22:08:57 +000092MODULE_VERSION(DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
95 * Theory of operation
96 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -040097 * The Au1000 MACs use a simple rx and tx descriptor ring scheme.
98 * There are four receive and four transmit descriptors. These
99 * descriptors are not in memory; rather, they are just a set of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * hardware registers.
101 *
102 * Since the Au1000 has a coherent data cache, the receive and
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400103 * transmit buffers are allocated from the KSEG0 segment. The
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * hardware registers, however, are still mapped at KSEG1 to
105 * make sure there's no out-of-order writes, and that all writes
106 * complete immediately.
107 */
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109struct au1000_private *au_macs[NUM_ETH_INTERFACES];
110
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200111/*
112 * board-specific configurations
113 *
114 * PHY detection algorithm
115 *
Florian Fainellibd2302c2009-11-10 01:13:38 +0100116 * If phy_static_config is undefined, the PHY setup is
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200117 * autodetected:
118 *
119 * mii_probe() first searches the current MAC's MII bus for a PHY,
Florian Fainellibd2302c2009-11-10 01:13:38 +0100120 * selecting the first (or last, if phy_search_highest_addr is
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200121 * defined) PHY address not already claimed by another netdev.
122 *
123 * If nothing was found that way when searching for the 2nd ethernet
Florian Fainellibd2302c2009-11-10 01:13:38 +0100124 * controller's PHY and phy1_search_mac0 is defined, then
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200125 * the first MII bus is searched as well for an unclaimed PHY; this is
126 * needed in case of a dual-PHY accessible only through the MAC0's MII
127 * bus.
128 *
129 * Finally, if no PHY is found, then the corresponding ethernet
130 * controller is not registered to the network subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
132
Florian Fainellibd2302c2009-11-10 01:13:38 +0100133/* autodetection defaults: phy1_search_mac0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200135/* static PHY setup
136 *
137 * most boards PHY setup should be detectable properly with the
138 * autodetection algorithm in mii_probe(), but in some cases (e.g. if
139 * you have a switch attached, or want to use the PHY's interrupt
140 * notification capabilities) you can provide a static PHY
141 * configuration here
142 *
143 * IRQs may only be set, if a PHY address was configured
144 * If a PHY address is given, also a bus id is required to be set
145 *
146 * ps: make sure the used irqs are configured properly in the board
147 * specific irq-map
148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Florian Fainellieb049632010-04-06 22:09:01 +0000150static void au1000_enable_mac(struct net_device *dev, int force_reset)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800151{
152 unsigned long flags;
153 struct au1000_private *aup = netdev_priv(dev);
154
155 spin_lock_irqsave(&aup->lock, flags);
156
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000157 if (force_reset || (!aup->mac_enabled)) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800158 *aup->enable = MAC_EN_CLOCK_ENABLE;
159 au_sync_delay(2);
160 *aup->enable = (MAC_EN_RESET0 | MAC_EN_RESET1 | MAC_EN_RESET2
161 | MAC_EN_CLOCK_ENABLE);
162 au_sync_delay(2);
163
164 aup->mac_enabled = 1;
165 }
166
167 spin_unlock_irqrestore(&aup->lock, flags);
168}
169
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200170/*
171 * MII operations
172 */
Adrian Bunk1210dde2008-10-12 21:02:19 -0700173static int au1000_mdio_read(struct net_device *dev, int phy_addr, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Wang Chen454d7c92008-11-12 23:37:49 -0800175 struct au1000_private *aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200176 volatile u32 *const mii_control_reg = &aup->mac->mii_control;
177 volatile u32 *const mii_data_reg = &aup->mac->mii_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 u32 timedout = 20;
179 u32 mii_control;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 while (*mii_control_reg & MAC_MII_BUSY) {
182 mdelay(1);
183 if (--timedout == 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000184 netdev_err(dev, "read_MII busy timeout!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return -1;
186 }
187 }
188
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400189 mii_control = MAC_SET_MII_SELECT_REG(reg) |
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200190 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 *mii_control_reg = mii_control;
193
194 timedout = 20;
195 while (*mii_control_reg & MAC_MII_BUSY) {
196 mdelay(1);
197 if (--timedout == 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000198 netdev_err(dev, "mdio_read busy timeout!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -1;
200 }
201 }
202 return (int)*mii_data_reg;
203}
204
Adrian Bunk1210dde2008-10-12 21:02:19 -0700205static void au1000_mdio_write(struct net_device *dev, int phy_addr,
206 int reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Wang Chen454d7c92008-11-12 23:37:49 -0800208 struct au1000_private *aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200209 volatile u32 *const mii_control_reg = &aup->mac->mii_control;
210 volatile u32 *const mii_data_reg = &aup->mac->mii_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 u32 timedout = 20;
212 u32 mii_control;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 while (*mii_control_reg & MAC_MII_BUSY) {
215 mdelay(1);
216 if (--timedout == 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000217 netdev_err(dev, "mdio_write busy timeout!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return;
219 }
220 }
221
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400222 mii_control = MAC_SET_MII_SELECT_REG(reg) |
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200223 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 *mii_data_reg = value;
226 *mii_control_reg = mii_control;
227}
228
Adrian Bunk1210dde2008-10-12 21:02:19 -0700229static int au1000_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200231 /* WARNING: bus->phy_map[phy_addr].attached_dev == dev does
232 * _NOT_ hold (e.g. when PHY is accessed through other MAC's MII bus) */
233 struct net_device *const dev = bus->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Florian Fainellieb049632010-04-06 22:09:01 +0000235 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200236 * mii_bus is enabled */
Adrian Bunk1210dde2008-10-12 21:02:19 -0700237 return au1000_mdio_read(dev, phy_addr, regnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Adrian Bunk1210dde2008-10-12 21:02:19 -0700240static int au1000_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
241 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200243 struct net_device *const dev = bus->priv;
244
Florian Fainellieb049632010-04-06 22:09:01 +0000245 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200246 * mii_bus is enabled */
Adrian Bunk1210dde2008-10-12 21:02:19 -0700247 au1000_mdio_write(dev, phy_addr, regnum, value);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200248 return 0;
249}
250
Adrian Bunk1210dde2008-10-12 21:02:19 -0700251static int au1000_mdiobus_reset(struct mii_bus *bus)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200252{
253 struct net_device *const dev = bus->priv;
254
Florian Fainellieb049632010-04-06 22:09:01 +0000255 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200256 * mii_bus is enabled */
257 return 0;
258}
259
Florian Fainellieb049632010-04-06 22:09:01 +0000260static void au1000_hard_stop(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800261{
262 struct au1000_private *aup = netdev_priv(dev);
263
Florian Fainelli5368c722010-04-06 22:09:17 +0000264 netif_dbg(aup, drv, dev, "hard stop\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800265
266 aup->mac->control &= ~(MAC_RX_ENABLE | MAC_TX_ENABLE);
267 au_sync_delay(10);
268}
269
Florian Fainellieb049632010-04-06 22:09:01 +0000270static void au1000_enable_rx_tx(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800271{
272 struct au1000_private *aup = netdev_priv(dev);
273
Florian Fainelli5368c722010-04-06 22:09:17 +0000274 netif_dbg(aup, hw, dev, "enable_rx_tx\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800275
276 aup->mac->control |= (MAC_RX_ENABLE | MAC_TX_ENABLE);
277 au_sync_delay(10);
278}
279
280static void
281au1000_adjust_link(struct net_device *dev)
282{
283 struct au1000_private *aup = netdev_priv(dev);
284 struct phy_device *phydev = aup->phy_dev;
285 unsigned long flags;
286
287 int status_change = 0;
288
289 BUG_ON(!aup->phy_dev);
290
291 spin_lock_irqsave(&aup->lock, flags);
292
293 if (phydev->link && (aup->old_speed != phydev->speed)) {
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000294 /* speed changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800295
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000296 switch (phydev->speed) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800297 case SPEED_10:
298 case SPEED_100:
299 break;
300 default:
Florian Fainelli5368c722010-04-06 22:09:17 +0000301 netdev_warn(dev, "Speed (%d) is not 10/100 ???\n",
302 phydev->speed);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800303 break;
304 }
305
306 aup->old_speed = phydev->speed;
307
308 status_change = 1;
309 }
310
311 if (phydev->link && (aup->old_duplex != phydev->duplex)) {
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000312 /* duplex mode changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800313
314 /* switching duplex mode requires to disable rx and tx! */
Florian Fainellieb049632010-04-06 22:09:01 +0000315 au1000_hard_stop(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800316
317 if (DUPLEX_FULL == phydev->duplex)
318 aup->mac->control = ((aup->mac->control
319 | MAC_FULL_DUPLEX)
320 & ~MAC_DISABLE_RX_OWN);
321 else
322 aup->mac->control = ((aup->mac->control
323 & ~MAC_FULL_DUPLEX)
324 | MAC_DISABLE_RX_OWN);
325 au_sync_delay(1);
326
Florian Fainellieb049632010-04-06 22:09:01 +0000327 au1000_enable_rx_tx(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800328 aup->old_duplex = phydev->duplex;
329
330 status_change = 1;
331 }
332
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000333 if (phydev->link != aup->old_link) {
334 /* link state changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800335
336 if (!phydev->link) {
337 /* link went down */
338 aup->old_speed = 0;
339 aup->old_duplex = -1;
340 }
341
342 aup->old_link = phydev->link;
343 status_change = 1;
344 }
345
346 spin_unlock_irqrestore(&aup->lock, flags);
347
348 if (status_change) {
349 if (phydev->link)
Florian Fainelli5368c722010-04-06 22:09:17 +0000350 netdev_info(dev, "link up (%d/%s)\n",
351 phydev->speed,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800352 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
353 else
Florian Fainelli5368c722010-04-06 22:09:17 +0000354 netdev_info(dev, "link down\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800355 }
356}
357
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000358static int au1000_mii_probe(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200359{
Wang Chen454d7c92008-11-12 23:37:49 -0800360 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200361 struct phy_device *phydev = NULL;
Florian Fainelli18b8e152010-09-08 11:11:40 +0000362 int phy_addr;
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200363
Florian Fainellibd2302c2009-11-10 01:13:38 +0100364 if (aup->phy_static_config) {
365 BUG_ON(aup->mac_id < 0 || aup->mac_id > 1);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200366
Florian Fainellibd2302c2009-11-10 01:13:38 +0100367 if (aup->phy_addr)
368 phydev = aup->mii_bus->phy_map[aup->phy_addr];
369 else
Florian Fainelli5368c722010-04-06 22:09:17 +0000370 netdev_info(dev, "using PHY-less setup\n");
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200371 return 0;
Florian Fainelli18b8e152010-09-08 11:11:40 +0000372 }
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200373
Florian Fainelli18b8e152010-09-08 11:11:40 +0000374 /* find the first (lowest address) PHY
375 * on the current MAC's MII bus */
376 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
377 if (aup->mii_bus->phy_map[phy_addr]) {
378 phydev = aup->mii_bus->phy_map[phy_addr];
379 if (!aup->phy_search_highest_addr)
380 /* break out with first one found */
381 break;
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Florian Fainelli18b8e152010-09-08 11:11:40 +0000384 if (aup->phy1_search_mac0) {
385 /* try harder to find a PHY */
386 if (!phydev && (aup->mac_id == 1)) {
387 /* no PHY found, maybe we have a dual PHY? */
388 dev_info(&dev->dev, ": no PHY found on MAC1, "
389 "let's see if it's attached to MAC0...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Florian Fainelli18b8e152010-09-08 11:11:40 +0000391 /* find the first (lowest address) non-attached
392 * PHY on the MAC0 MII bus
393 */
394 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
395 struct phy_device *const tmp_phydev =
396 aup->mii_bus->phy_map[phy_addr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Florian Fainelli18b8e152010-09-08 11:11:40 +0000398 if (aup->mac_id == 1)
399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Florian Fainelli18b8e152010-09-08 11:11:40 +0000401 /* no PHY here... */
402 if (!tmp_phydev)
403 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Florian Fainelli18b8e152010-09-08 11:11:40 +0000405 /* already claimed by MAC0 */
406 if (tmp_phydev->attached_dev)
407 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Florian Fainelli18b8e152010-09-08 11:11:40 +0000409 phydev = tmp_phydev;
410 break; /* found it */
Florian Fainellibd2302c2009-11-10 01:13:38 +0100411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200415 if (!phydev) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000416 netdev_err(dev, "no PHY found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -1;
418 }
419
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200420 /* now we are supposed to have a proper phydev, to attach to... */
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200421 BUG_ON(phydev->attached_dev);
422
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800423 phydev = phy_connect(dev, dev_name(&phydev->dev), &au1000_adjust_link,
424 0, PHY_INTERFACE_MODE_MII);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200425
426 if (IS_ERR(phydev)) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000427 netdev_err(dev, "Could not attach to PHY\n");
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200428 return PTR_ERR(phydev);
429 }
430
431 /* mask with MAC supported features */
432 phydev->supported &= (SUPPORTED_10baseT_Half
433 | SUPPORTED_10baseT_Full
434 | SUPPORTED_100baseT_Half
435 | SUPPORTED_100baseT_Full
436 | SUPPORTED_Autoneg
437 /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
438 | SUPPORTED_MII
439 | SUPPORTED_TP);
440
441 phydev->advertising = phydev->supported;
442
443 aup->old_link = 0;
444 aup->old_speed = 0;
445 aup->old_duplex = -1;
446 aup->phy_dev = phydev;
447
Florian Fainelli5368c722010-04-06 22:09:17 +0000448 netdev_info(dev, "attached PHY driver [%s] "
449 "(mii_bus:phy_addr=%s, irq=%d)\n",
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800450 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 return 0;
453}
454
455
456/*
457 * Buffer allocation/deallocation routines. The buffer descriptor returned
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400458 * has the virtual and dma address of a buffer suitable for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * both, receive and transmit operations.
460 */
Florian Fainelli34415922010-09-08 11:11:25 +0000461static struct db_dest *au1000_GetFreeDB(struct au1000_private *aup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Florian Fainelli34415922010-09-08 11:11:25 +0000463 struct db_dest *pDB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 pDB = aup->pDBfree;
465
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000466 if (pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 aup->pDBfree = pDB->pnext;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return pDB;
470}
471
Florian Fainelli34415922010-09-08 11:11:25 +0000472void au1000_ReleaseDB(struct au1000_private *aup, struct db_dest *pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Florian Fainelli34415922010-09-08 11:11:25 +0000474 struct db_dest *pDBfree = aup->pDBfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (pDBfree)
476 pDBfree->pnext = pDB;
477 aup->pDBfree = pDB;
478}
479
Florian Fainellieb049632010-04-06 22:09:01 +0000480static void au1000_reset_mac_unlocked(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200481{
Wang Chen454d7c92008-11-12 23:37:49 -0800482 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200483 int i;
484
Florian Fainellieb049632010-04-06 22:09:01 +0000485 au1000_hard_stop(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200486
487 *aup->enable = MAC_EN_CLOCK_ENABLE;
488 au_sync_delay(2);
489 *aup->enable = 0;
490 au_sync_delay(2);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 aup->tx_full = 0;
493 for (i = 0; i < NUM_RX_DMA; i++) {
494 /* reset control bits */
495 aup->rx_dma_ring[i]->buff_stat &= ~0xf;
496 }
497 for (i = 0; i < NUM_TX_DMA; i++) {
498 /* reset control bits */
499 aup->tx_dma_ring[i]->buff_stat &= ~0xf;
500 }
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200501
502 aup->mac_enabled = 0;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Florian Fainellieb049632010-04-06 22:09:01 +0000506static void au1000_reset_mac(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200507{
Wang Chen454d7c92008-11-12 23:37:49 -0800508 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200509 unsigned long flags;
510
Florian Fainelli5368c722010-04-06 22:09:17 +0000511 netif_dbg(aup, hw, dev, "reset mac, aup %x\n",
512 (unsigned)aup);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200513
514 spin_lock_irqsave(&aup->lock, flags);
515
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000516 au1000_reset_mac_unlocked(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200517
518 spin_unlock_irqrestore(&aup->lock, flags);
519}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400521/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * Setup the receive and transmit "rings". These pointers are the addresses
523 * of the rx and tx MAC DMA registers so they are fixed by the hardware --
524 * these are not descriptors sitting in memory.
525 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400526static void
Florian Fainellieb049632010-04-06 22:09:01 +0000527au1000_setup_hw_rings(struct au1000_private *aup, u32 rx_base, u32 tx_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 int i;
530
531 for (i = 0; i < NUM_RX_DMA; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400532 aup->rx_dma_ring[i] =
Florian Fainelli18b8e152010-09-08 11:11:40 +0000533 (volatile struct rx_dma *)
534 (rx_base + sizeof(struct rx_dma)*i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536 for (i = 0; i < NUM_TX_DMA; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400537 aup->tx_dma_ring[i] =
Florian Fainelli18b8e152010-09-08 11:11:40 +0000538 (volatile struct tx_dma *)
539 (tx_base + sizeof(struct tx_dma)*i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/*
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200544 * ethtool operations
545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547static int au1000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
548{
Wang Chen454d7c92008-11-12 23:37:49 -0800549 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200551 if (aup->phy_dev)
552 return phy_ethtool_gset(aup->phy_dev, cmd);
553
554 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557static int au1000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
558{
Wang Chen454d7c92008-11-12 23:37:49 -0800559 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200561 if (!capable(CAP_NET_ADMIN))
562 return -EPERM;
563
564 if (aup->phy_dev)
565 return phy_ethtool_sset(aup->phy_dev, cmd);
566
567 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570static void
571au1000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
572{
Wang Chen454d7c92008-11-12 23:37:49 -0800573 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 strcpy(info->driver, DRV_NAME);
576 strcpy(info->version, DRV_VERSION);
577 info->fw_version[0] = '\0';
578 sprintf(info->bus_info, "%s %d", DRV_NAME, aup->mac_id);
579 info->regdump_len = 0;
580}
581
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +0000582static void au1000_set_msglevel(struct net_device *dev, u32 value)
583{
584 struct au1000_private *aup = netdev_priv(dev);
585 aup->msg_enable = value;
586}
587
588static u32 au1000_get_msglevel(struct net_device *dev)
589{
590 struct au1000_private *aup = netdev_priv(dev);
591 return aup->msg_enable;
592}
593
Jeff Garzik7282d492006-09-13 14:30:00 -0400594static const struct ethtool_ops au1000_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 .get_settings = au1000_get_settings,
596 .set_settings = au1000_set_settings,
597 .get_drvinfo = au1000_get_drvinfo,
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200598 .get_link = ethtool_op_get_link,
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +0000599 .get_msglevel = au1000_get_msglevel,
600 .set_msglevel = au1000_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601};
602
Florian Fainelli5ef30412009-01-22 14:06:25 -0800603
604/*
605 * Initialize the interface.
606 *
607 * When the device powers up, the clocks are disabled and the
608 * mac is in reset state. When the interface is closed, we
609 * do the same -- reset the device and disable the clocks to
610 * conserve power. Thus, whenever au1000_init() is called,
611 * the device should already be in reset state.
612 */
613static int au1000_init(struct net_device *dev)
614{
615 struct au1000_private *aup = netdev_priv(dev);
616 unsigned long flags;
617 int i;
618 u32 control;
619
Florian Fainelli5368c722010-04-06 22:09:17 +0000620 netif_dbg(aup, hw, dev, "au1000_init\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800621
622 /* bring the device out of reset */
Florian Fainellieb049632010-04-06 22:09:01 +0000623 au1000_enable_mac(dev, 1);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800624
625 spin_lock_irqsave(&aup->lock, flags);
626
627 aup->mac->control = 0;
628 aup->tx_head = (aup->tx_dma_ring[0]->buff_stat & 0xC) >> 2;
629 aup->tx_tail = aup->tx_head;
630 aup->rx_head = (aup->rx_dma_ring[0]->buff_stat & 0xC) >> 2;
631
632 aup->mac->mac_addr_high = dev->dev_addr[5]<<8 | dev->dev_addr[4];
633 aup->mac->mac_addr_low = dev->dev_addr[3]<<24 | dev->dev_addr[2]<<16 |
634 dev->dev_addr[1]<<8 | dev->dev_addr[0];
635
Florian Fainelli18b8e152010-09-08 11:11:40 +0000636
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000637 for (i = 0; i < NUM_RX_DMA; i++)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800638 aup->rx_dma_ring[i]->buff_stat |= RX_DMA_ENABLE;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000639
Florian Fainelli5ef30412009-01-22 14:06:25 -0800640 au_sync();
641
642 control = MAC_RX_ENABLE | MAC_TX_ENABLE;
643#ifndef CONFIG_CPU_LITTLE_ENDIAN
644 control |= MAC_BIG_ENDIAN;
645#endif
646 if (aup->phy_dev) {
647 if (aup->phy_dev->link && (DUPLEX_FULL == aup->phy_dev->duplex))
648 control |= MAC_FULL_DUPLEX;
649 else
650 control |= MAC_DISABLE_RX_OWN;
651 } else { /* PHY-less op, assume full-duplex */
652 control |= MAC_FULL_DUPLEX;
653 }
654
655 aup->mac->control = control;
656 aup->mac->vlan1_tag = 0x8100; /* activate vlan support */
657 au_sync();
658
659 spin_unlock_irqrestore(&aup->lock, flags);
660 return 0;
661}
662
Florian Fainellieb049632010-04-06 22:09:01 +0000663static inline void au1000_update_rx_stats(struct net_device *dev, u32 status)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800664{
Florian Fainelli5ef30412009-01-22 14:06:25 -0800665 struct net_device_stats *ps = &dev->stats;
666
667 ps->rx_packets++;
668 if (status & RX_MCAST_FRAME)
669 ps->multicast++;
670
671 if (status & RX_ERROR) {
672 ps->rx_errors++;
673 if (status & RX_MISSED_FRAME)
674 ps->rx_missed_errors++;
roel kluin4989ccb2009-10-06 09:54:18 +0000675 if (status & (RX_OVERLEN | RX_RUNT | RX_LEN_ERROR))
Florian Fainelli5ef30412009-01-22 14:06:25 -0800676 ps->rx_length_errors++;
677 if (status & RX_CRC_ERROR)
678 ps->rx_crc_errors++;
679 if (status & RX_COLL)
680 ps->collisions++;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000681 } else
Florian Fainelli5ef30412009-01-22 14:06:25 -0800682 ps->rx_bytes += status & RX_FRAME_LEN_MASK;
683
684}
685
686/*
687 * Au1000 receive routine.
688 */
689static int au1000_rx(struct net_device *dev)
690{
691 struct au1000_private *aup = netdev_priv(dev);
692 struct sk_buff *skb;
Florian Fainelli34415922010-09-08 11:11:25 +0000693 volatile struct rx_dma *prxd;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800694 u32 buff_stat, status;
Florian Fainelli34415922010-09-08 11:11:25 +0000695 struct db_dest *pDB;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800696 u32 frmlen;
697
Florian Fainelli5368c722010-04-06 22:09:17 +0000698 netif_dbg(aup, rx_status, dev, "au1000_rx head %d\n", aup->rx_head);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800699
700 prxd = aup->rx_dma_ring[aup->rx_head];
701 buff_stat = prxd->buff_stat;
702 while (buff_stat & RX_T_DONE) {
703 status = prxd->status;
704 pDB = aup->rx_db_inuse[aup->rx_head];
Florian Fainellieb049632010-04-06 22:09:01 +0000705 au1000_update_rx_stats(dev, status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800706 if (!(status & RX_ERROR)) {
707
708 /* good frame */
709 frmlen = (status & RX_FRAME_LEN_MASK);
710 frmlen -= 4; /* Remove FCS */
711 skb = dev_alloc_skb(frmlen + 2);
712 if (skb == NULL) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000713 netdev_err(dev, "Memory squeeze, dropping packet.\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800714 dev->stats.rx_dropped++;
715 continue;
716 }
717 skb_reserve(skb, 2); /* 16 byte IP header align */
718 skb_copy_to_linear_data(skb,
719 (unsigned char *)pDB->vaddr, frmlen);
720 skb_put(skb, frmlen);
721 skb->protocol = eth_type_trans(skb, dev);
722 netif_rx(skb); /* pass the packet to upper layers */
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000723 } else {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800724 if (au1000_debug > 4) {
Florian Fainelli215e17b2010-09-08 11:11:45 +0000725 pr_err("rx_error(s):");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800726 if (status & RX_MISSED_FRAME)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000727 pr_cont(" miss");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800728 if (status & RX_WDOG_TIMER)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000729 pr_cont(" wdog");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800730 if (status & RX_RUNT)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000731 pr_cont(" runt");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800732 if (status & RX_OVERLEN)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000733 pr_cont(" overlen");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800734 if (status & RX_COLL)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000735 pr_cont(" coll");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800736 if (status & RX_MII_ERROR)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000737 pr_cont(" mii error");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800738 if (status & RX_CRC_ERROR)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000739 pr_cont(" crc error");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800740 if (status & RX_LEN_ERROR)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000741 pr_cont(" len error");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800742 if (status & RX_U_CNTRL_FRAME)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000743 pr_cont(" u control frame");
744 pr_cont("\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800745 }
746 }
747 prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE);
748 aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1);
749 au_sync();
750
751 /* next descriptor */
752 prxd = aup->rx_dma_ring[aup->rx_head];
753 buff_stat = prxd->buff_stat;
754 }
755 return 0;
756}
757
Florian Fainellieb049632010-04-06 22:09:01 +0000758static void au1000_update_tx_stats(struct net_device *dev, u32 status)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800759{
760 struct au1000_private *aup = netdev_priv(dev);
761 struct net_device_stats *ps = &dev->stats;
762
763 if (status & TX_FRAME_ABORTED) {
764 if (!aup->phy_dev || (DUPLEX_FULL == aup->phy_dev->duplex)) {
765 if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) {
766 /* any other tx errors are only valid
767 * in half duplex mode */
768 ps->tx_errors++;
769 ps->tx_aborted_errors++;
770 }
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000771 } else {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800772 ps->tx_errors++;
773 ps->tx_aborted_errors++;
774 if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER))
775 ps->tx_carrier_errors++;
776 }
777 }
778}
779
780/*
781 * Called from the interrupt service routine to acknowledge
782 * the TX DONE bits. This is a must if the irq is setup as
783 * edge triggered.
784 */
785static void au1000_tx_ack(struct net_device *dev)
786{
787 struct au1000_private *aup = netdev_priv(dev);
Florian Fainelli34415922010-09-08 11:11:25 +0000788 volatile struct tx_dma *ptxd;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800789
790 ptxd = aup->tx_dma_ring[aup->tx_tail];
791
792 while (ptxd->buff_stat & TX_T_DONE) {
Florian Fainellieb049632010-04-06 22:09:01 +0000793 au1000_update_tx_stats(dev, ptxd->status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800794 ptxd->buff_stat &= ~TX_T_DONE;
795 ptxd->len = 0;
796 au_sync();
797
798 aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1);
799 ptxd = aup->tx_dma_ring[aup->tx_tail];
800
801 if (aup->tx_full) {
802 aup->tx_full = 0;
803 netif_wake_queue(dev);
804 }
805 }
806}
807
808/*
809 * Au1000 interrupt service routine.
810 */
811static irqreturn_t au1000_interrupt(int irq, void *dev_id)
812{
813 struct net_device *dev = dev_id;
814
815 /* Handle RX interrupts first to minimize chance of overrun */
816
817 au1000_rx(dev);
818 au1000_tx_ack(dev);
819 return IRQ_RETVAL(1);
820}
821
822static int au1000_open(struct net_device *dev)
823{
824 int retval;
825 struct au1000_private *aup = netdev_priv(dev);
826
Florian Fainelli5368c722010-04-06 22:09:17 +0000827 netif_dbg(aup, drv, dev, "open: dev=%p\n", dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800828
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000829 retval = request_irq(dev->irq, au1000_interrupt, 0,
830 dev->name, dev);
831 if (retval) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000832 netdev_err(dev, "unable to get IRQ %d\n", dev->irq);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800833 return retval;
834 }
835
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000836 retval = au1000_init(dev);
837 if (retval) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000838 netdev_err(dev, "error in au1000_init\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800839 free_irq(dev->irq, dev);
840 return retval;
841 }
842
843 if (aup->phy_dev) {
844 /* cause the PHY state machine to schedule a link state check */
845 aup->phy_dev->state = PHY_CHANGELINK;
846 phy_start(aup->phy_dev);
847 }
848
849 netif_start_queue(dev);
850
Florian Fainelli5368c722010-04-06 22:09:17 +0000851 netif_dbg(aup, drv, dev, "open: Initialization done.\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800852
853 return 0;
854}
855
856static int au1000_close(struct net_device *dev)
857{
858 unsigned long flags;
859 struct au1000_private *const aup = netdev_priv(dev);
860
Florian Fainelli5368c722010-04-06 22:09:17 +0000861 netif_dbg(aup, drv, dev, "close: dev=%p\n", dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800862
863 if (aup->phy_dev)
864 phy_stop(aup->phy_dev);
865
866 spin_lock_irqsave(&aup->lock, flags);
867
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000868 au1000_reset_mac_unlocked(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800869
870 /* stop the device */
871 netif_stop_queue(dev);
872
873 /* disable the interrupt */
874 free_irq(dev->irq, dev);
875 spin_unlock_irqrestore(&aup->lock, flags);
876
877 return 0;
878}
879
880/*
881 * Au1000 transmit routine.
882 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000883static netdev_tx_t au1000_tx(struct sk_buff *skb, struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800884{
885 struct au1000_private *aup = netdev_priv(dev);
886 struct net_device_stats *ps = &dev->stats;
Florian Fainelli34415922010-09-08 11:11:25 +0000887 volatile struct tx_dma *ptxd;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800888 u32 buff_stat;
Florian Fainelli34415922010-09-08 11:11:25 +0000889 struct db_dest *pDB;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800890 int i;
891
Florian Fainelli5368c722010-04-06 22:09:17 +0000892 netif_dbg(aup, tx_queued, dev, "tx: aup %x len=%d, data=%p, head %d\n",
893 (unsigned)aup, skb->len,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800894 skb->data, aup->tx_head);
895
896 ptxd = aup->tx_dma_ring[aup->tx_head];
897 buff_stat = ptxd->buff_stat;
898 if (buff_stat & TX_DMA_ENABLE) {
899 /* We've wrapped around and the transmitter is still busy */
900 netif_stop_queue(dev);
901 aup->tx_full = 1;
Patrick McHardy5b548142009-06-12 06:22:29 +0000902 return NETDEV_TX_BUSY;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000903 } else if (buff_stat & TX_T_DONE) {
Florian Fainellieb049632010-04-06 22:09:01 +0000904 au1000_update_tx_stats(dev, ptxd->status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800905 ptxd->len = 0;
906 }
907
908 if (aup->tx_full) {
909 aup->tx_full = 0;
910 netif_wake_queue(dev);
911 }
912
913 pDB = aup->tx_db_inuse[aup->tx_head];
Florian Fainellibd2302c2009-11-10 01:13:38 +0100914 skb_copy_from_linear_data(skb, (void *)pDB->vaddr, skb->len);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800915 if (skb->len < ETH_ZLEN) {
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000916 for (i = skb->len; i < ETH_ZLEN; i++)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800917 ((char *)pDB->vaddr)[i] = 0;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000918
Florian Fainelli5ef30412009-01-22 14:06:25 -0800919 ptxd->len = ETH_ZLEN;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000920 } else
Florian Fainelli5ef30412009-01-22 14:06:25 -0800921 ptxd->len = skb->len;
922
923 ps->tx_packets++;
924 ps->tx_bytes += ptxd->len;
925
926 ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE;
927 au_sync();
928 dev_kfree_skb(skb);
929 aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1);
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{
Florian Fainelli5368c722010-04-06 22:09:17 +0000939 netdev_err(dev, "au1000_tx_timeout: dev=%p\n", dev);
Florian Fainellieb049632010-04-06 22:09:01 +0000940 au1000_reset_mac(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800941 au1000_init(dev);
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700942 dev->trans_start = jiffies; /* prevent tx timeout */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800943 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
Florian Fainelli18b8e152010-09-08 11:11:40 +0000950 netif_dbg(aup, drv, dev, "%s: flags=%x\n", __func__, dev->flags);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800951 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
952 aup->mac->control |= MAC_PROMISCUOUS;
953 } else if ((dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000954 netdev_mc_count(dev) > MULTICAST_FILTER_LIMIT) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800955 aup->mac->control |= MAC_PASS_ALL_MULTI;
956 aup->mac->control &= ~MAC_PROMISCUOUS;
Florian Fainelli5368c722010-04-06 22:09:17 +0000957 netdev_info(dev, "Pass all multicast\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800958 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000959 struct netdev_hw_addr *ha;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800960 u32 mc_filter[2]; /* Multicast hash filter */
961
962 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000963 netdev_for_each_mc_addr(ha, dev)
964 set_bit(ether_crc(ETH_ALEN, ha->addr)>>26,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800965 (long *)mc_filter);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800966 aup->mac->multi_hash_high = mc_filter[1];
967 aup->mac->multi_hash_low = mc_filter[0];
968 aup->mac->control &= ~MAC_PROMISCUOUS;
969 aup->mac->control |= MAC_HASH_MODE;
970 }
971}
972
973static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
974{
975 struct au1000_private *aup = netdev_priv(dev);
976
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000977 if (!netif_running(dev))
978 return -EINVAL;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800979
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000980 if (!aup->phy_dev)
981 return -EINVAL; /* PHY not controllable */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800982
Richard Cochran28b04112010-07-17 08:48:55 +0000983 return phy_mii_ioctl(aup->phy_dev, rq, cmd);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800984}
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{
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +00001000 static unsigned version_printed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 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;
Florian Fainelli34415922010-09-08 11:11:25 +00001004 struct db_dest *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
Florian Fainellibd2302c2009-11-10 01:13:38 +01001008 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1009 if (!base) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001010 dev_err(&pdev->dev, "failed to retrieve base register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001011 err = -ENODEV;
1012 goto out;
1013 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001014
Florian Fainellibd2302c2009-11-10 01:13:38 +01001015 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1016 if (!macen) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001017 dev_err(&pdev->dev, "failed to retrieve MAC Enable register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001018 err = -ENODEV;
1019 goto out;
1020 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001021
Florian Fainellibd2302c2009-11-10 01:13:38 +01001022 irq = platform_get_irq(pdev, 0);
1023 if (irq < 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001024 dev_err(&pdev->dev, "failed to retrieve IRQ\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001025 err = -ENODEV;
1026 goto out;
1027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Florian Fainelli18b8e152010-09-08 11:11:40 +00001029 if (!request_mem_region(base->start, resource_size(base),
1030 pdev->name)) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001031 dev_err(&pdev->dev, "failed to request memory region for base registers\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001032 err = -ENXIO;
1033 goto out;
1034 }
1035
Florian Fainelli18b8e152010-09-08 11:11:40 +00001036 if (!request_mem_region(macen->start, resource_size(macen),
1037 pdev->name)) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001038 dev_err(&pdev->dev, "failed to request memory region for MAC enable register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001039 err = -ENXIO;
1040 goto err_request;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 dev = alloc_etherdev(sizeof(struct au1000_private));
1044 if (!dev) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001045 dev_err(&pdev->dev, "alloc_etherdev failed\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001046 err = -ENOMEM;
1047 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049
Florian Fainellibd2302c2009-11-10 01:13:38 +01001050 SET_NETDEV_DEV(dev, &pdev->dev);
1051 platform_set_drvdata(pdev, dev);
Wang Chen454d7c92008-11-12 23:37:49 -08001052 aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Martin Gebert533763d2008-07-23 09:40:09 +02001054 spin_lock_init(&aup->lock);
Florian Fainelli18b8e152010-09-08 11:11:40 +00001055 aup->msg_enable = (au1000_debug < 4 ?
1056 AU1000_DEF_MSG_ENABLE : au1000_debug);
Martin Gebert533763d2008-07-23 09:40:09 +02001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* Allocate the data buffers */
1059 /* Snooping works fine with eth on all au1xxx */
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001060 aup->vaddr = (u32)dma_alloc_noncoherent(NULL, MAX_BUF_SIZE *
1061 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1062 &aup->dma_addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (!aup->vaddr) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001064 dev_err(&pdev->dev, "failed to allocate data buffers\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001065 err = -ENOMEM;
1066 goto err_vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
1069 /* aup->mac is the base address of the MAC's registers */
Florian Fainelli18b8e152010-09-08 11:11:40 +00001070 aup->mac = (volatile struct mac_reg *)
1071 ioremap_nocache(base->start, resource_size(base));
Florian Fainellibd2302c2009-11-10 01:13:38 +01001072 if (!aup->mac) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001073 dev_err(&pdev->dev, "failed to ioremap MAC registers\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001074 err = -ENXIO;
1075 goto err_remap1;
1076 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001077
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001078 /* Setup some variables for quick register address access */
Florian Fainelli18b8e152010-09-08 11:11:40 +00001079 aup->enable = (volatile u32 *)ioremap_nocache(macen->start,
1080 resource_size(macen));
Florian Fainellibd2302c2009-11-10 01:13:38 +01001081 if (!aup->enable) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001082 dev_err(&pdev->dev, "failed to ioremap MAC enable register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001083 err = -ENXIO;
1084 goto err_remap2;
1085 }
1086 aup->mac_id = pdev->id;
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001087
Manuel Laussf6673652010-07-21 14:30:50 +02001088 if (pdev->id == 0)
Florian Fainellieb049632010-04-06 22:09:01 +00001089 au1000_setup_hw_rings(aup, MAC0_RX_DMA_ADDR, MAC0_TX_DMA_ADDR);
Manuel Laussf6673652010-07-21 14:30:50 +02001090 else if (pdev->id == 1)
Florian Fainellieb049632010-04-06 22:09:01 +00001091 au1000_setup_hw_rings(aup, MAC1_RX_DMA_ADDR, MAC1_TX_DMA_ADDR);
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001092
Manuel Laussf6673652010-07-21 14:30:50 +02001093 /* set a random MAC now in case platform_data doesn't provide one */
1094 random_ether_addr(dev->dev_addr);
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001095
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001096 *aup->enable = 0;
1097 aup->mac_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Florian Fainellibd2302c2009-11-10 01:13:38 +01001099 pd = pdev->dev.platform_data;
1100 if (!pd) {
Florian Fainelli18b8e152010-09-08 11:11:40 +00001101 dev_info(&pdev->dev, "no platform_data passed,"
1102 " PHY search on MAC0\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001103 aup->phy1_search_mac0 = 1;
1104 } else {
Manuel Laussf6673652010-07-21 14:30:50 +02001105 if (is_valid_ether_addr(pd->mac))
1106 memcpy(dev->dev_addr, pd->mac, 6);
1107
Florian Fainellibd2302c2009-11-10 01:13:38 +01001108 aup->phy_static_config = pd->phy_static_config;
1109 aup->phy_search_highest_addr = pd->phy_search_highest_addr;
1110 aup->phy1_search_mac0 = pd->phy1_search_mac0;
1111 aup->phy_addr = pd->phy_addr;
1112 aup->phy_busid = pd->phy_busid;
1113 aup->phy_irq = pd->phy_irq;
1114 }
1115
1116 if (aup->phy_busid && aup->phy_busid > 0) {
Florian Fainelli18b8e152010-09-08 11:11:40 +00001117 dev_err(&pdev->dev, "MAC0-associated PHY attached 2nd MACs MII bus not supported yet\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001118 err = -ENODEV;
1119 goto err_mdiobus_alloc;
1120 }
1121
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001122 aup->mii_bus = mdiobus_alloc();
Florian Fainellibd2302c2009-11-10 01:13:38 +01001123 if (aup->mii_bus == NULL) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001124 dev_err(&pdev->dev, "failed to allocate mdiobus structure\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001125 err = -ENOMEM;
1126 goto err_mdiobus_alloc;
1127 }
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001128
1129 aup->mii_bus->priv = dev;
Adrian Bunk1210dde2008-10-12 21:02:19 -07001130 aup->mii_bus->read = au1000_mdiobus_read;
1131 aup->mii_bus->write = au1000_mdiobus_write;
1132 aup->mii_bus->reset = au1000_mdiobus_reset;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001133 aup->mii_bus->name = "au1000_eth_mii";
1134 snprintf(aup->mii_bus->id, MII_BUS_ID_SIZE, "%x", aup->mac_id);
1135 aup->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
roel kluindcbfef82009-08-30 22:40:15 +00001136 if (aup->mii_bus->irq == NULL)
1137 goto err_out;
1138
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +00001139 for (i = 0; i < PHY_MAX_ADDR; ++i)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001140 aup->mii_bus->irq[i] = PHY_POLL;
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001141 /* if known, set corresponding PHY IRQs */
Florian Fainellibd2302c2009-11-10 01:13:38 +01001142 if (aup->phy_static_config)
1143 if (aup->phy_irq && aup->phy_busid == aup->mac_id)
1144 aup->mii_bus->irq[aup->phy_addr] = aup->phy_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Florian Fainellibd2302c2009-11-10 01:13:38 +01001146 err = mdiobus_register(aup->mii_bus);
1147 if (err) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001148 dev_err(&pdev->dev, "failed to register MDIO bus\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001149 goto err_mdiobus_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
Florian Fainellieb049632010-04-06 22:09:01 +00001152 if (au1000_mii_probe(dev) != 0)
Florian Fainellibd2302c2009-11-10 01:13:38 +01001153 goto err_out;
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 pDBfree = NULL;
1156 /* setup the data buffer descriptors and attach a buffer to each one */
1157 pDB = aup->db;
1158 for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) {
1159 pDB->pnext = pDBfree;
1160 pDBfree = pDB;
1161 pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i);
1162 pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
1163 pDB++;
1164 }
1165 aup->pDBfree = pDBfree;
1166
1167 for (i = 0; i < NUM_RX_DMA; i++) {
Florian Fainellieb049632010-04-06 22:09:01 +00001168 pDB = au1000_GetFreeDB(aup);
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001169 if (!pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 goto err_out;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1173 aup->rx_db_inuse[i] = pDB;
1174 }
1175 for (i = 0; i < NUM_TX_DMA; i++) {
Florian Fainellieb049632010-04-06 22:09:01 +00001176 pDB = au1000_GetFreeDB(aup);
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001177 if (!pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto err_out;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1181 aup->tx_dma_ring[i]->len = 0;
1182 aup->tx_db_inuse[i] = pDB;
1183 }
1184
Florian Fainellibd2302c2009-11-10 01:13:38 +01001185 dev->base_addr = base->start;
1186 dev->irq = irq;
1187 dev->netdev_ops = &au1000_netdev_ops;
1188 SET_ETHTOOL_OPS(dev, &au1000_ethtool_ops);
1189 dev->watchdog_timeo = ETH_TX_TIMEOUT;
1190
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001191 /*
1192 * The boot code uses the ethernet controller, so reset it to start
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * fresh. au1000_init() expects that the device is in reset state.
1194 */
Florian Fainellieb049632010-04-06 22:09:01 +00001195 au1000_reset_mac(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Florian Fainellibd2302c2009-11-10 01:13:38 +01001197 err = register_netdev(dev);
1198 if (err) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001199 netdev_err(dev, "Cannot register net device, aborting.\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001200 goto err_out;
1201 }
1202
Florian Fainelli5368c722010-04-06 22:09:17 +00001203 netdev_info(dev, "Au1xx0 Ethernet found at 0x%lx, irq %d\n",
1204 (unsigned long)base->start, irq);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001205 if (version_printed++ == 0)
Florian Fainelli215e17b2010-09-08 11:11:45 +00001206 pr_info("%s version %s %s\n",
1207 DRV_NAME, DRV_VERSION, DRV_AUTHOR);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001208
1209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211err_out:
Florian Fainellibd2302c2009-11-10 01:13:38 +01001212 if (aup->mii_bus != NULL)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001213 mdiobus_unregister(aup->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /* here we should have a valid dev plus aup-> register addresses
1216 * so we can reset the mac properly.*/
Florian Fainellieb049632010-04-06 22:09:01 +00001217 au1000_reset_mac(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 for (i = 0; i < NUM_RX_DMA; i++) {
1220 if (aup->rx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001221 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223 for (i = 0; i < NUM_TX_DMA; i++) {
1224 if (aup->tx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001225 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Florian Fainellibd2302c2009-11-10 01:13:38 +01001227err_mdiobus_reg:
1228 mdiobus_free(aup->mii_bus);
1229err_mdiobus_alloc:
1230 iounmap(aup->enable);
1231err_remap2:
1232 iounmap(aup->mac);
1233err_remap1:
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001234 dma_free_noncoherent(NULL, MAX_BUF_SIZE * (NUM_TX_BUFFS + NUM_RX_BUFFS),
1235 (void *)aup->vaddr, aup->dma_addr);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001236err_vaddr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 free_netdev(dev);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001238err_alloc:
1239 release_mem_region(macen->start, resource_size(macen));
1240err_request:
1241 release_mem_region(base->start, resource_size(base));
1242out:
1243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Florian Fainellibd2302c2009-11-10 01:13:38 +01001246static int __devexit au1000_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Florian Fainellibd2302c2009-11-10 01:13:38 +01001248 struct net_device *dev = platform_get_drvdata(pdev);
1249 struct au1000_private *aup = netdev_priv(dev);
1250 int i;
1251 struct resource *base, *macen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Florian Fainellibd2302c2009-11-10 01:13:38 +01001253 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Florian Fainellibd2302c2009-11-10 01:13:38 +01001255 unregister_netdev(dev);
1256 mdiobus_unregister(aup->mii_bus);
1257 mdiobus_free(aup->mii_bus);
1258
1259 for (i = 0; i < NUM_RX_DMA; i++)
1260 if (aup->rx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001261 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001262
1263 for (i = 0; i < NUM_TX_DMA; i++)
1264 if (aup->tx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001265 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001266
1267 dma_free_noncoherent(NULL, MAX_BUF_SIZE *
1268 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1269 (void *)aup->vaddr, aup->dma_addr);
1270
1271 iounmap(aup->mac);
1272 iounmap(aup->enable);
1273
1274 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1275 release_mem_region(base->start, resource_size(base));
1276
1277 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1278 release_mem_region(macen->start, resource_size(macen));
1279
1280 free_netdev(dev);
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return 0;
1283}
1284
Florian Fainellibd2302c2009-11-10 01:13:38 +01001285static struct platform_driver au1000_eth_driver = {
1286 .probe = au1000_probe,
1287 .remove = __devexit_p(au1000_remove),
1288 .driver = {
1289 .name = "au1000-eth",
1290 .owner = THIS_MODULE,
1291 },
1292};
1293MODULE_ALIAS("platform:au1000-eth");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Florian Fainellibd2302c2009-11-10 01:13:38 +01001295
1296static int __init au1000_init_module(void)
1297{
1298 return platform_driver_register(&au1000_eth_driver);
1299}
1300
1301static void __exit au1000_exit_module(void)
1302{
1303 platform_driver_unregister(&au1000_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306module_init(au1000_init_module);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001307module_exit(au1000_exit_module);