blob: 2eee5764805d71732d1af1da940f8da58202ae68 [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
Jeff Kirsher0ab75ae2013-12-06 06:28:43 -080030 * with this program; if not, see <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
32 * ########################################################################
33 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -040034 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
Florian Fainelli215e17b2010-09-08 11:11:45 +000036#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Manuel Laussbc36b422009-10-17 02:00:07 +000038#include <linux/capability.h>
Ralf Baechled791c2b2007-06-24 15:59:54 +020039#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/module.h>
41#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
43#include <linux/timer.h>
44#include <linux/errno.h>
45#include <linux/in.h>
46#include <linux/ioport.h>
47#include <linux/bitops.h>
48#include <linux/slab.h>
49#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/init.h>
51#include <linux/netdevice.h>
52#include <linux/etherdevice.h>
53#include <linux/ethtool.h>
54#include <linux/mii.h>
55#include <linux/skbuff.h>
56#include <linux/delay.h>
Herbert Valerio Riedel8cd35da2006-05-01 15:37:09 +020057#include <linux/crc32.h>
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +020058#include <linux/phy.h>
Florian Fainellibd2302c2009-11-10 01:13:38 +010059#include <linux/platform_device.h>
Florian Fainelli49a42c02010-09-08 11:11:49 +000060#include <linux/cpu.h>
61#include <linux/io.h>
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <asm/mipsregs.h>
64#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <asm/processor.h>
66
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090067#include <au1000.h>
Florian Fainellibd2302c2009-11-10 01:13:38 +010068#include <au1xxx_eth.h>
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090069#include <prom.h>
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include "au1000_eth.h"
72
73#ifdef AU1000_ETH_DEBUG
74static int au1000_debug = 5;
75#else
76static int au1000_debug = 3;
77#endif
78
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +000079#define AU1000_DEF_MSG_ENABLE (NETIF_MSG_DRV | \
80 NETIF_MSG_PROBE | \
81 NETIF_MSG_LINK)
82
Sergei Shtylyov89be0502006-04-19 22:46:21 +040083#define DRV_NAME "au1000_eth"
Florian Fainelli8020eb82010-04-06 22:09:20 +000084#define DRV_VERSION "1.7"
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define DRV_AUTHOR "Pete Popov <ppopov@embeddedalley.com>"
86#define DRV_DESC "Au1xxx on-chip Ethernet driver"
87
88MODULE_AUTHOR(DRV_AUTHOR);
89MODULE_DESCRIPTION(DRV_DESC);
90MODULE_LICENSE("GPL");
Florian Fainelli13130c72010-04-06 22:08:57 +000091MODULE_VERSION(DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
94 * Theory of operation
95 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -040096 * The Au1000 MACs use a simple rx and tx descriptor ring scheme.
97 * There are four receive and four transmit descriptors. These
98 * descriptors are not in memory; rather, they are just a set of
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * hardware registers.
100 *
101 * Since the Au1000 has a coherent data cache, the receive and
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400102 * transmit buffers are allocated from the KSEG0 segment. The
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * hardware registers, however, are still mapped at KSEG1 to
104 * make sure there's no out-of-order writes, and that all writes
105 * complete immediately.
106 */
107
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200108/*
109 * board-specific configurations
110 *
111 * PHY detection algorithm
112 *
Florian Fainellibd2302c2009-11-10 01:13:38 +0100113 * If phy_static_config is undefined, the PHY setup is
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200114 * autodetected:
115 *
116 * mii_probe() first searches the current MAC's MII bus for a PHY,
Florian Fainellibd2302c2009-11-10 01:13:38 +0100117 * selecting the first (or last, if phy_search_highest_addr is
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200118 * defined) PHY address not already claimed by another netdev.
119 *
120 * If nothing was found that way when searching for the 2nd ethernet
Florian Fainellibd2302c2009-11-10 01:13:38 +0100121 * controller's PHY and phy1_search_mac0 is defined, then
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200122 * the first MII bus is searched as well for an unclaimed PHY; this is
123 * needed in case of a dual-PHY accessible only through the MAC0's MII
124 * bus.
125 *
126 * Finally, if no PHY is found, then the corresponding ethernet
127 * controller is not registered to the network subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
129
Florian Fainellibd2302c2009-11-10 01:13:38 +0100130/* autodetection defaults: phy1_search_mac0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200132/* static PHY setup
133 *
134 * most boards PHY setup should be detectable properly with the
135 * autodetection algorithm in mii_probe(), but in some cases (e.g. if
136 * you have a switch attached, or want to use the PHY's interrupt
137 * notification capabilities) you can provide a static PHY
138 * configuration here
139 *
140 * IRQs may only be set, if a PHY address was configured
141 * If a PHY address is given, also a bus id is required to be set
142 *
143 * ps: make sure the used irqs are configured properly in the board
144 * specific irq-map
145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Florian Fainellieb049632010-04-06 22:09:01 +0000147static void au1000_enable_mac(struct net_device *dev, int force_reset)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800148{
149 unsigned long flags;
150 struct au1000_private *aup = netdev_priv(dev);
151
152 spin_lock_irqsave(&aup->lock, flags);
153
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000154 if (force_reset || (!aup->mac_enabled)) {
Wolfgang Grandegger462ca992010-11-23 06:40:25 +0000155 writel(MAC_EN_CLOCK_ENABLE, aup->enable);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800156 au_sync_delay(2);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000157 writel((MAC_EN_RESET0 | MAC_EN_RESET1 | MAC_EN_RESET2
Wolfgang Grandegger462ca992010-11-23 06:40:25 +0000158 | MAC_EN_CLOCK_ENABLE), aup->enable);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800159 au_sync_delay(2);
160
161 aup->mac_enabled = 1;
162 }
163
164 spin_unlock_irqrestore(&aup->lock, flags);
165}
166
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200167/*
168 * MII operations
169 */
Adrian Bunk1210dde2008-10-12 21:02:19 -0700170static int au1000_mdio_read(struct net_device *dev, int phy_addr, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Wang Chen454d7c92008-11-12 23:37:49 -0800172 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000173 u32 *const mii_control_reg = &aup->mac->mii_control;
174 u32 *const mii_data_reg = &aup->mac->mii_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 u32 timedout = 20;
176 u32 mii_control;
177
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000178 while (readl(mii_control_reg) & MAC_MII_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 mdelay(1);
180 if (--timedout == 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000181 netdev_err(dev, "read_MII busy timeout!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return -1;
183 }
184 }
185
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400186 mii_control = MAC_SET_MII_SELECT_REG(reg) |
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200187 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000189 writel(mii_control, mii_control_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 timedout = 20;
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000192 while (readl(mii_control_reg) & MAC_MII_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 mdelay(1);
194 if (--timedout == 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000195 netdev_err(dev, "mdio_read busy timeout!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -1;
197 }
198 }
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000199 return readl(mii_data_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Adrian Bunk1210dde2008-10-12 21:02:19 -0700202static void au1000_mdio_write(struct net_device *dev, int phy_addr,
203 int reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Wang Chen454d7c92008-11-12 23:37:49 -0800205 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000206 u32 *const mii_control_reg = &aup->mac->mii_control;
207 u32 *const mii_data_reg = &aup->mac->mii_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 u32 timedout = 20;
209 u32 mii_control;
210
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000211 while (readl(mii_control_reg) & MAC_MII_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 mdelay(1);
213 if (--timedout == 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000214 netdev_err(dev, "mdio_write busy timeout!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return;
216 }
217 }
218
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400219 mii_control = MAC_SET_MII_SELECT_REG(reg) |
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200220 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000222 writel(value, mii_data_reg);
223 writel(mii_control, mii_control_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Adrian Bunk1210dde2008-10-12 21:02:19 -0700226static int au1000_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200228 /* WARNING: bus->phy_map[phy_addr].attached_dev == dev does
Florian Fainellidc998392010-09-08 11:11:59 +0000229 * _NOT_ hold (e.g. when PHY is accessed through other MAC's MII bus)
230 */
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200231 struct net_device *const dev = bus->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Florian Fainellidc998392010-09-08 11:11:59 +0000233 /* make sure the MAC associated with this
234 * mii_bus is enabled
235 */
236 au1000_enable_mac(dev, 0);
237
Adrian Bunk1210dde2008-10-12 21:02:19 -0700238 return au1000_mdio_read(dev, phy_addr, regnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Adrian Bunk1210dde2008-10-12 21:02:19 -0700241static int au1000_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
242 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200244 struct net_device *const dev = bus->priv;
245
Florian Fainellidc998392010-09-08 11:11:59 +0000246 /* make sure the MAC associated with this
247 * mii_bus is enabled
248 */
249 au1000_enable_mac(dev, 0);
250
Adrian Bunk1210dde2008-10-12 21:02:19 -0700251 au1000_mdio_write(dev, phy_addr, regnum, value);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200252 return 0;
253}
254
Adrian Bunk1210dde2008-10-12 21:02:19 -0700255static int au1000_mdiobus_reset(struct mii_bus *bus)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200256{
257 struct net_device *const dev = bus->priv;
258
Florian Fainellidc998392010-09-08 11:11:59 +0000259 /* make sure the MAC associated with this
260 * mii_bus is enabled
261 */
262 au1000_enable_mac(dev, 0);
263
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200264 return 0;
265}
266
Florian Fainellieb049632010-04-06 22:09:01 +0000267static void au1000_hard_stop(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800268{
269 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000270 u32 reg;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800271
Florian Fainelli5368c722010-04-06 22:09:17 +0000272 netif_dbg(aup, drv, dev, "hard stop\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800273
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000274 reg = readl(&aup->mac->control);
275 reg &= ~(MAC_RX_ENABLE | MAC_TX_ENABLE);
276 writel(reg, &aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800277 au_sync_delay(10);
278}
279
Florian Fainellieb049632010-04-06 22:09:01 +0000280static void au1000_enable_rx_tx(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800281{
282 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000283 u32 reg;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800284
Florian Fainelli5368c722010-04-06 22:09:17 +0000285 netif_dbg(aup, hw, dev, "enable_rx_tx\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800286
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000287 reg = readl(&aup->mac->control);
288 reg |= (MAC_RX_ENABLE | MAC_TX_ENABLE);
289 writel(reg, &aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800290 au_sync_delay(10);
291}
292
293static void
294au1000_adjust_link(struct net_device *dev)
295{
296 struct au1000_private *aup = netdev_priv(dev);
297 struct phy_device *phydev = aup->phy_dev;
298 unsigned long flags;
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000299 u32 reg;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800300
301 int status_change = 0;
302
303 BUG_ON(!aup->phy_dev);
304
305 spin_lock_irqsave(&aup->lock, flags);
306
307 if (phydev->link && (aup->old_speed != phydev->speed)) {
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000308 /* speed changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800309
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000310 switch (phydev->speed) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800311 case SPEED_10:
312 case SPEED_100:
313 break;
314 default:
Florian Fainelli5368c722010-04-06 22:09:17 +0000315 netdev_warn(dev, "Speed (%d) is not 10/100 ???\n",
316 phydev->speed);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800317 break;
318 }
319
320 aup->old_speed = phydev->speed;
321
322 status_change = 1;
323 }
324
325 if (phydev->link && (aup->old_duplex != phydev->duplex)) {
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000326 /* duplex mode changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800327
328 /* switching duplex mode requires to disable rx and tx! */
Florian Fainellieb049632010-04-06 22:09:01 +0000329 au1000_hard_stop(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800330
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000331 reg = readl(&aup->mac->control);
332 if (DUPLEX_FULL == phydev->duplex) {
333 reg |= MAC_FULL_DUPLEX;
334 reg &= ~MAC_DISABLE_RX_OWN;
335 } else {
336 reg &= ~MAC_FULL_DUPLEX;
337 reg |= MAC_DISABLE_RX_OWN;
338 }
339 writel(reg, &aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800340 au_sync_delay(1);
341
Florian Fainellieb049632010-04-06 22:09:01 +0000342 au1000_enable_rx_tx(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800343 aup->old_duplex = phydev->duplex;
344
345 status_change = 1;
346 }
347
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000348 if (phydev->link != aup->old_link) {
349 /* link state changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800350
351 if (!phydev->link) {
352 /* link went down */
353 aup->old_speed = 0;
354 aup->old_duplex = -1;
355 }
356
357 aup->old_link = phydev->link;
358 status_change = 1;
359 }
360
361 spin_unlock_irqrestore(&aup->lock, flags);
362
363 if (status_change) {
364 if (phydev->link)
Florian Fainelli5368c722010-04-06 22:09:17 +0000365 netdev_info(dev, "link up (%d/%s)\n",
366 phydev->speed,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800367 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
368 else
Florian Fainelli5368c722010-04-06 22:09:17 +0000369 netdev_info(dev, "link down\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800370 }
371}
372
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000373static int au1000_mii_probe(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200374{
Wang Chen454d7c92008-11-12 23:37:49 -0800375 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200376 struct phy_device *phydev = NULL;
Florian Fainelli18b8e152010-09-08 11:11:40 +0000377 int phy_addr;
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200378
Florian Fainellibd2302c2009-11-10 01:13:38 +0100379 if (aup->phy_static_config) {
380 BUG_ON(aup->mac_id < 0 || aup->mac_id > 1);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200381
Florian Fainellibd2302c2009-11-10 01:13:38 +0100382 if (aup->phy_addr)
383 phydev = aup->mii_bus->phy_map[aup->phy_addr];
384 else
Florian Fainelli5368c722010-04-06 22:09:17 +0000385 netdev_info(dev, "using PHY-less setup\n");
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200386 return 0;
Florian Fainelli18b8e152010-09-08 11:11:40 +0000387 }
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200388
Florian Fainelli18b8e152010-09-08 11:11:40 +0000389 /* find the first (lowest address) PHY
Florian Fainellidc998392010-09-08 11:11:59 +0000390 * on the current MAC's MII bus
391 */
Florian Fainelli18b8e152010-09-08 11:11:40 +0000392 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
393 if (aup->mii_bus->phy_map[phy_addr]) {
394 phydev = aup->mii_bus->phy_map[phy_addr];
395 if (!aup->phy_search_highest_addr)
396 /* break out with first one found */
397 break;
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Florian Fainelli18b8e152010-09-08 11:11:40 +0000400 if (aup->phy1_search_mac0) {
401 /* try harder to find a PHY */
402 if (!phydev && (aup->mac_id == 1)) {
403 /* no PHY found, maybe we have a dual PHY? */
404 dev_info(&dev->dev, ": no PHY found on MAC1, "
405 "let's see if it's attached to MAC0...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Florian Fainelli18b8e152010-09-08 11:11:40 +0000407 /* find the first (lowest address) non-attached
408 * PHY on the MAC0 MII bus
409 */
410 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
411 struct phy_device *const tmp_phydev =
412 aup->mii_bus->phy_map[phy_addr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Florian Fainelli18b8e152010-09-08 11:11:40 +0000414 if (aup->mac_id == 1)
415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Florian Fainelli18b8e152010-09-08 11:11:40 +0000417 /* no PHY here... */
418 if (!tmp_phydev)
419 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Florian Fainelli18b8e152010-09-08 11:11:40 +0000421 /* already claimed by MAC0 */
422 if (tmp_phydev->attached_dev)
423 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Florian Fainelli18b8e152010-09-08 11:11:40 +0000425 phydev = tmp_phydev;
426 break; /* found it */
Florian Fainellibd2302c2009-11-10 01:13:38 +0100427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200431 if (!phydev) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000432 netdev_err(dev, "no PHY found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -1;
434 }
435
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200436 /* now we are supposed to have a proper phydev, to attach to... */
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200437 BUG_ON(phydev->attached_dev);
438
Florian Fainellif9a8f832013-01-14 00:52:52 +0000439 phydev = phy_connect(dev, dev_name(&phydev->dev),
440 &au1000_adjust_link, PHY_INTERFACE_MODE_MII);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200441
442 if (IS_ERR(phydev)) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000443 netdev_err(dev, "Could not attach to PHY\n");
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200444 return PTR_ERR(phydev);
445 }
446
447 /* mask with MAC supported features */
448 phydev->supported &= (SUPPORTED_10baseT_Half
449 | SUPPORTED_10baseT_Full
450 | SUPPORTED_100baseT_Half
451 | SUPPORTED_100baseT_Full
452 | SUPPORTED_Autoneg
453 /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
454 | SUPPORTED_MII
455 | SUPPORTED_TP);
456
457 phydev->advertising = phydev->supported;
458
459 aup->old_link = 0;
460 aup->old_speed = 0;
461 aup->old_duplex = -1;
462 aup->phy_dev = phydev;
463
Florian Fainelli5368c722010-04-06 22:09:17 +0000464 netdev_info(dev, "attached PHY driver [%s] "
465 "(mii_bus:phy_addr=%s, irq=%d)\n",
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800466 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 return 0;
469}
470
471
472/*
473 * Buffer allocation/deallocation routines. The buffer descriptor returned
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400474 * has the virtual and dma address of a buffer suitable for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * both, receive and transmit operations.
476 */
Florian Fainelli34415922010-09-08 11:11:25 +0000477static struct db_dest *au1000_GetFreeDB(struct au1000_private *aup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Florian Fainelli34415922010-09-08 11:11:25 +0000479 struct db_dest *pDB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 pDB = aup->pDBfree;
481
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000482 if (pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 aup->pDBfree = pDB->pnext;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return pDB;
486}
487
Florian Fainelli34415922010-09-08 11:11:25 +0000488void au1000_ReleaseDB(struct au1000_private *aup, struct db_dest *pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Florian Fainelli34415922010-09-08 11:11:25 +0000490 struct db_dest *pDBfree = aup->pDBfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (pDBfree)
492 pDBfree->pnext = pDB;
493 aup->pDBfree = pDB;
494}
495
Florian Fainellieb049632010-04-06 22:09:01 +0000496static void au1000_reset_mac_unlocked(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200497{
Wang Chen454d7c92008-11-12 23:37:49 -0800498 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200499 int i;
500
Florian Fainellieb049632010-04-06 22:09:01 +0000501 au1000_hard_stop(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200502
Wolfgang Grandegger462ca992010-11-23 06:40:25 +0000503 writel(MAC_EN_CLOCK_ENABLE, aup->enable);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200504 au_sync_delay(2);
Wolfgang Grandegger462ca992010-11-23 06:40:25 +0000505 writel(0, aup->enable);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200506 au_sync_delay(2);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 aup->tx_full = 0;
509 for (i = 0; i < NUM_RX_DMA; i++) {
510 /* reset control bits */
511 aup->rx_dma_ring[i]->buff_stat &= ~0xf;
512 }
513 for (i = 0; i < NUM_TX_DMA; i++) {
514 /* reset control bits */
515 aup->tx_dma_ring[i]->buff_stat &= ~0xf;
516 }
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200517
518 aup->mac_enabled = 0;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Florian Fainellieb049632010-04-06 22:09:01 +0000522static void au1000_reset_mac(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200523{
Wang Chen454d7c92008-11-12 23:37:49 -0800524 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200525 unsigned long flags;
526
Florian Fainelli5368c722010-04-06 22:09:17 +0000527 netif_dbg(aup, hw, dev, "reset mac, aup %x\n",
528 (unsigned)aup);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200529
530 spin_lock_irqsave(&aup->lock, flags);
531
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000532 au1000_reset_mac_unlocked(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200533
534 spin_unlock_irqrestore(&aup->lock, flags);
535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400537/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * Setup the receive and transmit "rings". These pointers are the addresses
539 * of the rx and tx MAC DMA registers so they are fixed by the hardware --
540 * these are not descriptors sitting in memory.
541 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400542static void
Linus Torvaldsd6748062011-11-03 13:28:14 -0700543au1000_setup_hw_rings(struct au1000_private *aup, void __iomem *tx_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 int i;
546
547 for (i = 0; i < NUM_RX_DMA; i++) {
Linus Torvaldsd6748062011-11-03 13:28:14 -0700548 aup->rx_dma_ring[i] = (struct rx_dma *)
549 (tx_base + 0x100 + sizeof(struct rx_dma) * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 for (i = 0; i < NUM_TX_DMA; i++) {
Linus Torvaldsd6748062011-11-03 13:28:14 -0700552 aup->tx_dma_ring[i] = (struct tx_dma *)
553 (tx_base + sizeof(struct tx_dma) * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/*
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200558 * ethtool operations
559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561static int au1000_get_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 (aup->phy_dev)
566 return phy_ethtool_gset(aup->phy_dev, cmd);
567
568 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571static int au1000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
572{
Wang Chen454d7c92008-11-12 23:37:49 -0800573 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200575 if (!capable(CAP_NET_ADMIN))
576 return -EPERM;
577
578 if (aup->phy_dev)
579 return phy_ethtool_sset(aup->phy_dev, cmd);
580
581 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584static void
585au1000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
586{
Wang Chen454d7c92008-11-12 23:37:49 -0800587 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Jiri Pirko7826d432013-01-06 00:44:26 +0000589 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
590 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
591 snprintf(info->bus_info, sizeof(info->bus_info), "%s %d", DRV_NAME,
592 aup->mac_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 info->regdump_len = 0;
594}
595
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +0000596static void au1000_set_msglevel(struct net_device *dev, u32 value)
597{
598 struct au1000_private *aup = netdev_priv(dev);
599 aup->msg_enable = value;
600}
601
602static u32 au1000_get_msglevel(struct net_device *dev)
603{
604 struct au1000_private *aup = netdev_priv(dev);
605 return aup->msg_enable;
606}
607
Jeff Garzik7282d492006-09-13 14:30:00 -0400608static const struct ethtool_ops au1000_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 .get_settings = au1000_get_settings,
610 .set_settings = au1000_set_settings,
611 .get_drvinfo = au1000_get_drvinfo,
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200612 .get_link = ethtool_op_get_link,
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +0000613 .get_msglevel = au1000_get_msglevel,
614 .set_msglevel = au1000_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615};
616
Florian Fainelli5ef30412009-01-22 14:06:25 -0800617
618/*
619 * Initialize the interface.
620 *
621 * When the device powers up, the clocks are disabled and the
622 * mac is in reset state. When the interface is closed, we
623 * do the same -- reset the device and disable the clocks to
624 * conserve power. Thus, whenever au1000_init() is called,
625 * the device should already be in reset state.
626 */
627static int au1000_init(struct net_device *dev)
628{
629 struct au1000_private *aup = netdev_priv(dev);
630 unsigned long flags;
631 int i;
632 u32 control;
633
Florian Fainelli5368c722010-04-06 22:09:17 +0000634 netif_dbg(aup, hw, dev, "au1000_init\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800635
636 /* bring the device out of reset */
Florian Fainellieb049632010-04-06 22:09:01 +0000637 au1000_enable_mac(dev, 1);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800638
639 spin_lock_irqsave(&aup->lock, flags);
640
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000641 writel(0, &aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800642 aup->tx_head = (aup->tx_dma_ring[0]->buff_stat & 0xC) >> 2;
643 aup->tx_tail = aup->tx_head;
644 aup->rx_head = (aup->rx_dma_ring[0]->buff_stat & 0xC) >> 2;
645
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000646 writel(dev->dev_addr[5]<<8 | dev->dev_addr[4],
647 &aup->mac->mac_addr_high);
648 writel(dev->dev_addr[3]<<24 | dev->dev_addr[2]<<16 |
649 dev->dev_addr[1]<<8 | dev->dev_addr[0],
650 &aup->mac->mac_addr_low);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800651
Florian Fainelli18b8e152010-09-08 11:11:40 +0000652
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000653 for (i = 0; i < NUM_RX_DMA; i++)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800654 aup->rx_dma_ring[i]->buff_stat |= RX_DMA_ENABLE;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000655
Florian Fainelli5ef30412009-01-22 14:06:25 -0800656 au_sync();
657
658 control = MAC_RX_ENABLE | MAC_TX_ENABLE;
659#ifndef CONFIG_CPU_LITTLE_ENDIAN
660 control |= MAC_BIG_ENDIAN;
661#endif
662 if (aup->phy_dev) {
663 if (aup->phy_dev->link && (DUPLEX_FULL == aup->phy_dev->duplex))
664 control |= MAC_FULL_DUPLEX;
665 else
666 control |= MAC_DISABLE_RX_OWN;
667 } else { /* PHY-less op, assume full-duplex */
668 control |= MAC_FULL_DUPLEX;
669 }
670
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000671 writel(control, &aup->mac->control);
672 writel(0x8100, &aup->mac->vlan1_tag); /* activate vlan support */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800673 au_sync();
674
675 spin_unlock_irqrestore(&aup->lock, flags);
676 return 0;
677}
678
Florian Fainellieb049632010-04-06 22:09:01 +0000679static inline void au1000_update_rx_stats(struct net_device *dev, u32 status)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800680{
Florian Fainelli5ef30412009-01-22 14:06:25 -0800681 struct net_device_stats *ps = &dev->stats;
682
683 ps->rx_packets++;
684 if (status & RX_MCAST_FRAME)
685 ps->multicast++;
686
687 if (status & RX_ERROR) {
688 ps->rx_errors++;
689 if (status & RX_MISSED_FRAME)
690 ps->rx_missed_errors++;
roel kluin4989ccb2009-10-06 09:54:18 +0000691 if (status & (RX_OVERLEN | RX_RUNT | RX_LEN_ERROR))
Florian Fainelli5ef30412009-01-22 14:06:25 -0800692 ps->rx_length_errors++;
693 if (status & RX_CRC_ERROR)
694 ps->rx_crc_errors++;
695 if (status & RX_COLL)
696 ps->collisions++;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000697 } else
Florian Fainelli5ef30412009-01-22 14:06:25 -0800698 ps->rx_bytes += status & RX_FRAME_LEN_MASK;
699
700}
701
702/*
703 * Au1000 receive routine.
704 */
705static int au1000_rx(struct net_device *dev)
706{
707 struct au1000_private *aup = netdev_priv(dev);
708 struct sk_buff *skb;
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000709 struct rx_dma *prxd;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800710 u32 buff_stat, status;
Florian Fainelli34415922010-09-08 11:11:25 +0000711 struct db_dest *pDB;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800712 u32 frmlen;
713
Florian Fainelli5368c722010-04-06 22:09:17 +0000714 netif_dbg(aup, rx_status, dev, "au1000_rx head %d\n", aup->rx_head);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800715
716 prxd = aup->rx_dma_ring[aup->rx_head];
717 buff_stat = prxd->buff_stat;
718 while (buff_stat & RX_T_DONE) {
719 status = prxd->status;
720 pDB = aup->rx_db_inuse[aup->rx_head];
Florian Fainellieb049632010-04-06 22:09:01 +0000721 au1000_update_rx_stats(dev, status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800722 if (!(status & RX_ERROR)) {
723
724 /* good frame */
725 frmlen = (status & RX_FRAME_LEN_MASK);
726 frmlen -= 4; /* Remove FCS */
Pradeep A Dalvi1d266432012-02-05 02:49:09 +0000727 skb = netdev_alloc_skb(dev, frmlen + 2);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800728 if (skb == NULL) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800729 dev->stats.rx_dropped++;
730 continue;
731 }
732 skb_reserve(skb, 2); /* 16 byte IP header align */
733 skb_copy_to_linear_data(skb,
734 (unsigned char *)pDB->vaddr, frmlen);
735 skb_put(skb, frmlen);
736 skb->protocol = eth_type_trans(skb, dev);
737 netif_rx(skb); /* pass the packet to upper layers */
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000738 } else {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800739 if (au1000_debug > 4) {
Florian Fainelli215e17b2010-09-08 11:11:45 +0000740 pr_err("rx_error(s):");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800741 if (status & RX_MISSED_FRAME)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000742 pr_cont(" miss");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800743 if (status & RX_WDOG_TIMER)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000744 pr_cont(" wdog");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800745 if (status & RX_RUNT)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000746 pr_cont(" runt");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800747 if (status & RX_OVERLEN)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000748 pr_cont(" overlen");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800749 if (status & RX_COLL)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000750 pr_cont(" coll");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800751 if (status & RX_MII_ERROR)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000752 pr_cont(" mii error");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800753 if (status & RX_CRC_ERROR)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000754 pr_cont(" crc error");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800755 if (status & RX_LEN_ERROR)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000756 pr_cont(" len error");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800757 if (status & RX_U_CNTRL_FRAME)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000758 pr_cont(" u control frame");
759 pr_cont("\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800760 }
761 }
762 prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE);
763 aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1);
764 au_sync();
765
766 /* next descriptor */
767 prxd = aup->rx_dma_ring[aup->rx_head];
768 buff_stat = prxd->buff_stat;
769 }
770 return 0;
771}
772
Florian Fainellieb049632010-04-06 22:09:01 +0000773static void au1000_update_tx_stats(struct net_device *dev, u32 status)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800774{
775 struct au1000_private *aup = netdev_priv(dev);
776 struct net_device_stats *ps = &dev->stats;
777
778 if (status & TX_FRAME_ABORTED) {
779 if (!aup->phy_dev || (DUPLEX_FULL == aup->phy_dev->duplex)) {
780 if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) {
781 /* any other tx errors are only valid
Florian Fainellidc998392010-09-08 11:11:59 +0000782 * in half duplex mode
783 */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800784 ps->tx_errors++;
785 ps->tx_aborted_errors++;
786 }
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000787 } else {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800788 ps->tx_errors++;
789 ps->tx_aborted_errors++;
790 if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER))
791 ps->tx_carrier_errors++;
792 }
793 }
794}
795
796/*
797 * Called from the interrupt service routine to acknowledge
798 * the TX DONE bits. This is a must if the irq is setup as
799 * edge triggered.
800 */
801static void au1000_tx_ack(struct net_device *dev)
802{
803 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000804 struct tx_dma *ptxd;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800805
806 ptxd = aup->tx_dma_ring[aup->tx_tail];
807
808 while (ptxd->buff_stat & TX_T_DONE) {
Florian Fainellieb049632010-04-06 22:09:01 +0000809 au1000_update_tx_stats(dev, ptxd->status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800810 ptxd->buff_stat &= ~TX_T_DONE;
811 ptxd->len = 0;
812 au_sync();
813
814 aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1);
815 ptxd = aup->tx_dma_ring[aup->tx_tail];
816
817 if (aup->tx_full) {
818 aup->tx_full = 0;
819 netif_wake_queue(dev);
820 }
821 }
822}
823
824/*
825 * Au1000 interrupt service routine.
826 */
827static irqreturn_t au1000_interrupt(int irq, void *dev_id)
828{
829 struct net_device *dev = dev_id;
830
831 /* Handle RX interrupts first to minimize chance of overrun */
832
833 au1000_rx(dev);
834 au1000_tx_ack(dev);
835 return IRQ_RETVAL(1);
836}
837
838static int au1000_open(struct net_device *dev)
839{
840 int retval;
841 struct au1000_private *aup = netdev_priv(dev);
842
Florian Fainelli5368c722010-04-06 22:09:17 +0000843 netif_dbg(aup, drv, dev, "open: dev=%p\n", dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800844
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000845 retval = request_irq(dev->irq, au1000_interrupt, 0,
846 dev->name, dev);
847 if (retval) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000848 netdev_err(dev, "unable to get IRQ %d\n", dev->irq);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800849 return retval;
850 }
851
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000852 retval = au1000_init(dev);
853 if (retval) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000854 netdev_err(dev, "error in au1000_init\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800855 free_irq(dev->irq, dev);
856 return retval;
857 }
858
859 if (aup->phy_dev) {
860 /* cause the PHY state machine to schedule a link state check */
861 aup->phy_dev->state = PHY_CHANGELINK;
862 phy_start(aup->phy_dev);
863 }
864
865 netif_start_queue(dev);
866
Florian Fainelli5368c722010-04-06 22:09:17 +0000867 netif_dbg(aup, drv, dev, "open: Initialization done.\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800868
869 return 0;
870}
871
872static int au1000_close(struct net_device *dev)
873{
874 unsigned long flags;
875 struct au1000_private *const aup = netdev_priv(dev);
876
Florian Fainelli5368c722010-04-06 22:09:17 +0000877 netif_dbg(aup, drv, dev, "close: dev=%p\n", dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800878
879 if (aup->phy_dev)
880 phy_stop(aup->phy_dev);
881
882 spin_lock_irqsave(&aup->lock, flags);
883
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000884 au1000_reset_mac_unlocked(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800885
886 /* stop the device */
887 netif_stop_queue(dev);
888
889 /* disable the interrupt */
890 free_irq(dev->irq, dev);
891 spin_unlock_irqrestore(&aup->lock, flags);
892
893 return 0;
894}
895
896/*
897 * Au1000 transmit routine.
898 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000899static netdev_tx_t au1000_tx(struct sk_buff *skb, struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800900{
901 struct au1000_private *aup = netdev_priv(dev);
902 struct net_device_stats *ps = &dev->stats;
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000903 struct tx_dma *ptxd;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800904 u32 buff_stat;
Florian Fainelli34415922010-09-08 11:11:25 +0000905 struct db_dest *pDB;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800906 int i;
907
Florian Fainelli5368c722010-04-06 22:09:17 +0000908 netif_dbg(aup, tx_queued, dev, "tx: aup %x len=%d, data=%p, head %d\n",
909 (unsigned)aup, skb->len,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800910 skb->data, aup->tx_head);
911
912 ptxd = aup->tx_dma_ring[aup->tx_head];
913 buff_stat = ptxd->buff_stat;
914 if (buff_stat & TX_DMA_ENABLE) {
915 /* We've wrapped around and the transmitter is still busy */
916 netif_stop_queue(dev);
917 aup->tx_full = 1;
Patrick McHardy5b548142009-06-12 06:22:29 +0000918 return NETDEV_TX_BUSY;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000919 } else if (buff_stat & TX_T_DONE) {
Florian Fainellieb049632010-04-06 22:09:01 +0000920 au1000_update_tx_stats(dev, ptxd->status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800921 ptxd->len = 0;
922 }
923
924 if (aup->tx_full) {
925 aup->tx_full = 0;
926 netif_wake_queue(dev);
927 }
928
929 pDB = aup->tx_db_inuse[aup->tx_head];
Florian Fainellibd2302c2009-11-10 01:13:38 +0100930 skb_copy_from_linear_data(skb, (void *)pDB->vaddr, skb->len);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800931 if (skb->len < ETH_ZLEN) {
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000932 for (i = skb->len; i < ETH_ZLEN; i++)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800933 ((char *)pDB->vaddr)[i] = 0;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000934
Florian Fainelli5ef30412009-01-22 14:06:25 -0800935 ptxd->len = ETH_ZLEN;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000936 } else
Florian Fainelli5ef30412009-01-22 14:06:25 -0800937 ptxd->len = skb->len;
938
939 ps->tx_packets++;
940 ps->tx_bytes += ptxd->len;
941
942 ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE;
943 au_sync();
944 dev_kfree_skb(skb);
945 aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000946 return NETDEV_TX_OK;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800947}
948
949/*
950 * The Tx ring has been full longer than the watchdog timeout
951 * value. The transmitter must be hung?
952 */
953static void au1000_tx_timeout(struct net_device *dev)
954{
Florian Fainelli5368c722010-04-06 22:09:17 +0000955 netdev_err(dev, "au1000_tx_timeout: dev=%p\n", dev);
Florian Fainellieb049632010-04-06 22:09:01 +0000956 au1000_reset_mac(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800957 au1000_init(dev);
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700958 dev->trans_start = jiffies; /* prevent tx timeout */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800959 netif_wake_queue(dev);
960}
961
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +0000962static void au1000_multicast_list(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800963{
964 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000965 u32 reg;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800966
Florian Fainelli18b8e152010-09-08 11:11:40 +0000967 netif_dbg(aup, drv, dev, "%s: flags=%x\n", __func__, dev->flags);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000968 reg = readl(&aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800969 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000970 reg |= MAC_PROMISCUOUS;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800971 } else if ((dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000972 netdev_mc_count(dev) > MULTICAST_FILTER_LIMIT) {
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000973 reg |= MAC_PASS_ALL_MULTI;
974 reg &= ~MAC_PROMISCUOUS;
Florian Fainelli5368c722010-04-06 22:09:17 +0000975 netdev_info(dev, "Pass all multicast\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800976 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000977 struct netdev_hw_addr *ha;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800978 u32 mc_filter[2]; /* Multicast hash filter */
979
980 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000981 netdev_for_each_mc_addr(ha, dev)
982 set_bit(ether_crc(ETH_ALEN, ha->addr)>>26,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800983 (long *)mc_filter);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000984 writel(mc_filter[1], &aup->mac->multi_hash_high);
985 writel(mc_filter[0], &aup->mac->multi_hash_low);
986 reg &= ~MAC_PROMISCUOUS;
987 reg |= MAC_HASH_MODE;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800988 }
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000989 writel(reg, &aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800990}
991
992static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
993{
994 struct au1000_private *aup = netdev_priv(dev);
995
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000996 if (!netif_running(dev))
997 return -EINVAL;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800998
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000999 if (!aup->phy_dev)
1000 return -EINVAL; /* PHY not controllable */
Florian Fainelli5ef30412009-01-22 14:06:25 -08001001
Richard Cochran28b04112010-07-17 08:48:55 +00001002 return phy_mii_ioctl(aup->phy_dev, rq, cmd);
Florian Fainelli5ef30412009-01-22 14:06:25 -08001003}
1004
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +00001005static const struct net_device_ops au1000_netdev_ops = {
1006 .ndo_open = au1000_open,
1007 .ndo_stop = au1000_close,
1008 .ndo_start_xmit = au1000_tx,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001009 .ndo_set_rx_mode = au1000_multicast_list,
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +00001010 .ndo_do_ioctl = au1000_ioctl,
1011 .ndo_tx_timeout = au1000_tx_timeout,
1012 .ndo_set_mac_address = eth_mac_addr,
1013 .ndo_validate_addr = eth_validate_addr,
1014 .ndo_change_mtu = eth_change_mtu,
1015};
1016
Bill Pemberton0cb05682012-12-03 09:23:54 -05001017static int au1000_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +00001019 static unsigned version_printed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct au1000_private *aup = NULL;
Florian Fainellibd2302c2009-11-10 01:13:38 +01001021 struct au1000_eth_platform_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct net_device *dev = NULL;
Florian Fainelli34415922010-09-08 11:11:25 +00001023 struct db_dest *pDB, *pDBfree;
Florian Fainellibd2302c2009-11-10 01:13:38 +01001024 int irq, i, err = 0;
Linus Torvaldsd6748062011-11-03 13:28:14 -07001025 struct resource *base, *macen, *macdma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Florian Fainellibd2302c2009-11-10 01:13:38 +01001027 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1028 if (!base) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001029 dev_err(&pdev->dev, "failed to retrieve base register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001030 err = -ENODEV;
1031 goto out;
1032 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001033
Florian Fainellibd2302c2009-11-10 01:13:38 +01001034 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1035 if (!macen) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001036 dev_err(&pdev->dev, "failed to retrieve MAC Enable register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001037 err = -ENODEV;
1038 goto out;
1039 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001040
Florian Fainellibd2302c2009-11-10 01:13:38 +01001041 irq = platform_get_irq(pdev, 0);
1042 if (irq < 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001043 dev_err(&pdev->dev, "failed to retrieve IRQ\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001044 err = -ENODEV;
1045 goto out;
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Linus Torvaldsd6748062011-11-03 13:28:14 -07001048 macdma = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1049 if (!macdma) {
1050 dev_err(&pdev->dev, "failed to retrieve MACDMA registers\n");
1051 err = -ENODEV;
1052 goto out;
1053 }
1054
Florian Fainelli18b8e152010-09-08 11:11:40 +00001055 if (!request_mem_region(base->start, resource_size(base),
1056 pdev->name)) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001057 dev_err(&pdev->dev, "failed to request memory region for base registers\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001058 err = -ENXIO;
1059 goto out;
1060 }
1061
Florian Fainelli18b8e152010-09-08 11:11:40 +00001062 if (!request_mem_region(macen->start, resource_size(macen),
1063 pdev->name)) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001064 dev_err(&pdev->dev, "failed to request memory region for MAC enable register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001065 err = -ENXIO;
1066 goto err_request;
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Linus Torvaldsd6748062011-11-03 13:28:14 -07001069 if (!request_mem_region(macdma->start, resource_size(macdma),
1070 pdev->name)) {
1071 dev_err(&pdev->dev, "failed to request MACDMA memory region\n");
1072 err = -ENXIO;
1073 goto err_macdma;
1074 }
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 dev = alloc_etherdev(sizeof(struct au1000_private));
1077 if (!dev) {
Florian Fainellibd2302c2009-11-10 01:13:38 +01001078 err = -ENOMEM;
1079 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
Florian Fainellibd2302c2009-11-10 01:13:38 +01001082 SET_NETDEV_DEV(dev, &pdev->dev);
1083 platform_set_drvdata(pdev, dev);
Wang Chen454d7c92008-11-12 23:37:49 -08001084 aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Martin Gebert533763d2008-07-23 09:40:09 +02001086 spin_lock_init(&aup->lock);
Florian Fainelli18b8e152010-09-08 11:11:40 +00001087 aup->msg_enable = (au1000_debug < 4 ?
1088 AU1000_DEF_MSG_ENABLE : au1000_debug);
Martin Gebert533763d2008-07-23 09:40:09 +02001089
Florian Fainellidc998392010-09-08 11:11:59 +00001090 /* Allocate the data buffers
1091 * Snooping works fine with eth on all au1xxx
1092 */
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001093 aup->vaddr = (u32)dma_alloc_noncoherent(NULL, MAX_BUF_SIZE *
1094 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1095 &aup->dma_addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (!aup->vaddr) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001097 dev_err(&pdev->dev, "failed to allocate data buffers\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001098 err = -ENOMEM;
1099 goto err_vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101
1102 /* aup->mac is the base address of the MAC's registers */
Florian Fainellid0e7cb52010-09-08 11:15:13 +00001103 aup->mac = (struct mac_reg *)
Florian Fainelli18b8e152010-09-08 11:11:40 +00001104 ioremap_nocache(base->start, resource_size(base));
Florian Fainellibd2302c2009-11-10 01:13:38 +01001105 if (!aup->mac) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001106 dev_err(&pdev->dev, "failed to ioremap MAC registers\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001107 err = -ENXIO;
1108 goto err_remap1;
1109 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001110
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001111 /* Setup some variables for quick register address access */
Florian Fainellid0e7cb52010-09-08 11:15:13 +00001112 aup->enable = (u32 *)ioremap_nocache(macen->start,
Florian Fainelli18b8e152010-09-08 11:11:40 +00001113 resource_size(macen));
Florian Fainellibd2302c2009-11-10 01:13:38 +01001114 if (!aup->enable) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001115 dev_err(&pdev->dev, "failed to ioremap MAC enable register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001116 err = -ENXIO;
1117 goto err_remap2;
1118 }
1119 aup->mac_id = pdev->id;
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001120
Linus Torvaldsd6748062011-11-03 13:28:14 -07001121 aup->macdma = ioremap_nocache(macdma->start, resource_size(macdma));
1122 if (!aup->macdma) {
1123 dev_err(&pdev->dev, "failed to ioremap MACDMA registers\n");
1124 err = -ENXIO;
1125 goto err_remap3;
1126 }
1127
1128 au1000_setup_hw_rings(aup, aup->macdma);
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001129
Wolfgang Grandegger462ca992010-11-23 06:40:25 +00001130 writel(0, aup->enable);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001131 aup->mac_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Jingoo Han1fc2c462013-08-30 13:51:45 +09001133 pd = dev_get_platdata(&pdev->dev);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001134 if (!pd) {
Florian Fainelli18b8e152010-09-08 11:11:40 +00001135 dev_info(&pdev->dev, "no platform_data passed,"
1136 " PHY search on MAC0\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001137 aup->phy1_search_mac0 = 1;
1138 } else {
Danny Kukawka7718f2c2012-02-17 05:43:22 +00001139 if (is_valid_ether_addr(pd->mac)) {
Joe Perchesd458cdf2013-10-01 19:04:40 -07001140 memcpy(dev->dev_addr, pd->mac, ETH_ALEN);
Danny Kukawka7718f2c2012-02-17 05:43:22 +00001141 } else {
1142 /* Set a random MAC since no valid provided by platform_data. */
1143 eth_hw_addr_random(dev);
1144 }
Manuel Laussf6673652010-07-21 14:30:50 +02001145
Florian Fainellibd2302c2009-11-10 01:13:38 +01001146 aup->phy_static_config = pd->phy_static_config;
1147 aup->phy_search_highest_addr = pd->phy_search_highest_addr;
1148 aup->phy1_search_mac0 = pd->phy1_search_mac0;
1149 aup->phy_addr = pd->phy_addr;
1150 aup->phy_busid = pd->phy_busid;
1151 aup->phy_irq = pd->phy_irq;
1152 }
1153
1154 if (aup->phy_busid && aup->phy_busid > 0) {
Florian Fainelli18b8e152010-09-08 11:11:40 +00001155 dev_err(&pdev->dev, "MAC0-associated PHY attached 2nd MACs MII bus not supported yet\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001156 err = -ENODEV;
1157 goto err_mdiobus_alloc;
1158 }
1159
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001160 aup->mii_bus = mdiobus_alloc();
Florian Fainellibd2302c2009-11-10 01:13:38 +01001161 if (aup->mii_bus == NULL) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001162 dev_err(&pdev->dev, "failed to allocate mdiobus structure\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001163 err = -ENOMEM;
1164 goto err_mdiobus_alloc;
1165 }
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001166
1167 aup->mii_bus->priv = dev;
Adrian Bunk1210dde2008-10-12 21:02:19 -07001168 aup->mii_bus->read = au1000_mdiobus_read;
1169 aup->mii_bus->write = au1000_mdiobus_write;
1170 aup->mii_bus->reset = au1000_mdiobus_reset;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001171 aup->mii_bus->name = "au1000_eth_mii";
Florian Fainellif74299b2012-01-09 23:59:09 +00001172 snprintf(aup->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1173 pdev->name, aup->mac_id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001174 aup->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
Peter Senna Tschudin69129922012-10-05 12:10:52 +00001175 if (aup->mii_bus->irq == NULL) {
1176 err = -ENOMEM;
roel kluindcbfef82009-08-30 22:40:15 +00001177 goto err_out;
Peter Senna Tschudin69129922012-10-05 12:10:52 +00001178 }
roel kluindcbfef82009-08-30 22:40:15 +00001179
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +00001180 for (i = 0; i < PHY_MAX_ADDR; ++i)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001181 aup->mii_bus->irq[i] = PHY_POLL;
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001182 /* if known, set corresponding PHY IRQs */
Florian Fainellibd2302c2009-11-10 01:13:38 +01001183 if (aup->phy_static_config)
1184 if (aup->phy_irq && aup->phy_busid == aup->mac_id)
1185 aup->mii_bus->irq[aup->phy_addr] = aup->phy_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Florian Fainellibd2302c2009-11-10 01:13:38 +01001187 err = mdiobus_register(aup->mii_bus);
1188 if (err) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001189 dev_err(&pdev->dev, "failed to register MDIO bus\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001190 goto err_mdiobus_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192
Peter Senna Tschudin69129922012-10-05 12:10:52 +00001193 err = au1000_mii_probe(dev);
1194 if (err != 0)
Florian Fainellibd2302c2009-11-10 01:13:38 +01001195 goto err_out;
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 pDBfree = NULL;
1198 /* setup the data buffer descriptors and attach a buffer to each one */
1199 pDB = aup->db;
1200 for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) {
1201 pDB->pnext = pDBfree;
1202 pDBfree = pDB;
1203 pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i);
1204 pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
1205 pDB++;
1206 }
1207 aup->pDBfree = pDBfree;
1208
Peter Senna Tschudin69129922012-10-05 12:10:52 +00001209 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 for (i = 0; i < NUM_RX_DMA; i++) {
Florian Fainellieb049632010-04-06 22:09:01 +00001211 pDB = au1000_GetFreeDB(aup);
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001212 if (!pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 goto err_out;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1216 aup->rx_db_inuse[i] = pDB;
1217 }
Peter Senna Tschudin69129922012-10-05 12:10:52 +00001218
1219 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 for (i = 0; i < NUM_TX_DMA; i++) {
Florian Fainellieb049632010-04-06 22:09:01 +00001221 pDB = au1000_GetFreeDB(aup);
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001222 if (!pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 goto err_out;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1226 aup->tx_dma_ring[i]->len = 0;
1227 aup->tx_db_inuse[i] = pDB;
1228 }
1229
Florian Fainellibd2302c2009-11-10 01:13:38 +01001230 dev->base_addr = base->start;
1231 dev->irq = irq;
1232 dev->netdev_ops = &au1000_netdev_ops;
1233 SET_ETHTOOL_OPS(dev, &au1000_ethtool_ops);
1234 dev->watchdog_timeo = ETH_TX_TIMEOUT;
1235
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001236 /*
1237 * The boot code uses the ethernet controller, so reset it to start
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 * fresh. au1000_init() expects that the device is in reset state.
1239 */
Florian Fainellieb049632010-04-06 22:09:01 +00001240 au1000_reset_mac(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Florian Fainellibd2302c2009-11-10 01:13:38 +01001242 err = register_netdev(dev);
1243 if (err) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001244 netdev_err(dev, "Cannot register net device, aborting.\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001245 goto err_out;
1246 }
1247
Florian Fainelli5368c722010-04-06 22:09:17 +00001248 netdev_info(dev, "Au1xx0 Ethernet found at 0x%lx, irq %d\n",
1249 (unsigned long)base->start, irq);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001250 if (version_printed++ == 0)
Florian Fainelli215e17b2010-09-08 11:11:45 +00001251 pr_info("%s version %s %s\n",
1252 DRV_NAME, DRV_VERSION, DRV_AUTHOR);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001253
1254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256err_out:
Florian Fainellibd2302c2009-11-10 01:13:38 +01001257 if (aup->mii_bus != NULL)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001258 mdiobus_unregister(aup->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /* here we should have a valid dev plus aup-> register addresses
Florian Fainellidc998392010-09-08 11:11:59 +00001261 * so we can reset the mac properly.
1262 */
Florian Fainellieb049632010-04-06 22:09:01 +00001263 au1000_reset_mac(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 for (i = 0; i < NUM_RX_DMA; i++) {
1266 if (aup->rx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001267 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 for (i = 0; i < NUM_TX_DMA; i++) {
1270 if (aup->tx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001271 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
Florian Fainellibd2302c2009-11-10 01:13:38 +01001273err_mdiobus_reg:
1274 mdiobus_free(aup->mii_bus);
1275err_mdiobus_alloc:
Linus Torvaldsd6748062011-11-03 13:28:14 -07001276 iounmap(aup->macdma);
1277err_remap3:
Florian Fainellibd2302c2009-11-10 01:13:38 +01001278 iounmap(aup->enable);
1279err_remap2:
1280 iounmap(aup->mac);
1281err_remap1:
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001282 dma_free_noncoherent(NULL, MAX_BUF_SIZE * (NUM_TX_BUFFS + NUM_RX_BUFFS),
1283 (void *)aup->vaddr, aup->dma_addr);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001284err_vaddr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 free_netdev(dev);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001286err_alloc:
Linus Torvaldsd6748062011-11-03 13:28:14 -07001287 release_mem_region(macdma->start, resource_size(macdma));
1288err_macdma:
Florian Fainellibd2302c2009-11-10 01:13:38 +01001289 release_mem_region(macen->start, resource_size(macen));
1290err_request:
1291 release_mem_region(base->start, resource_size(base));
1292out:
1293 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Bill Pemberton0cb05682012-12-03 09:23:54 -05001296static int au1000_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Florian Fainellibd2302c2009-11-10 01:13:38 +01001298 struct net_device *dev = platform_get_drvdata(pdev);
1299 struct au1000_private *aup = netdev_priv(dev);
1300 int i;
1301 struct resource *base, *macen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Florian Fainellibd2302c2009-11-10 01:13:38 +01001303 unregister_netdev(dev);
1304 mdiobus_unregister(aup->mii_bus);
1305 mdiobus_free(aup->mii_bus);
1306
1307 for (i = 0; i < NUM_RX_DMA; i++)
1308 if (aup->rx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001309 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001310
1311 for (i = 0; i < NUM_TX_DMA; i++)
1312 if (aup->tx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001313 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001314
1315 dma_free_noncoherent(NULL, MAX_BUF_SIZE *
1316 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1317 (void *)aup->vaddr, aup->dma_addr);
1318
Linus Torvaldsd6748062011-11-03 13:28:14 -07001319 iounmap(aup->macdma);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001320 iounmap(aup->mac);
1321 iounmap(aup->enable);
1322
Linus Torvaldsd6748062011-11-03 13:28:14 -07001323 base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1324 release_mem_region(base->start, resource_size(base));
1325
Florian Fainellibd2302c2009-11-10 01:13:38 +01001326 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1327 release_mem_region(base->start, resource_size(base));
1328
1329 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1330 release_mem_region(macen->start, resource_size(macen));
1331
1332 free_netdev(dev);
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return 0;
1335}
1336
Florian Fainellibd2302c2009-11-10 01:13:38 +01001337static struct platform_driver au1000_eth_driver = {
1338 .probe = au1000_probe,
Bill Pemberton0cb05682012-12-03 09:23:54 -05001339 .remove = au1000_remove,
Florian Fainellibd2302c2009-11-10 01:13:38 +01001340 .driver = {
1341 .name = "au1000-eth",
1342 .owner = THIS_MODULE,
1343 },
1344};
Axel Lindb62f682011-11-27 16:44:17 +00001345
1346module_platform_driver(au1000_eth_driver);
1347
Florian Fainellibd2302c2009-11-10 01:13:38 +01001348MODULE_ALIAS("platform:au1000-eth");