blob: a78e4c13695980e295ead2c8d85fd6f6a351f5c8 [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/netdevice.h>
51#include <linux/etherdevice.h>
52#include <linux/ethtool.h>
53#include <linux/mii.h>
54#include <linux/skbuff.h>
55#include <linux/delay.h>
Herbert Valerio Riedel8cd35da2006-05-01 15:37:09 +020056#include <linux/crc32.h>
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +020057#include <linux/phy.h>
Florian Fainellibd2302c2009-11-10 01:13:38 +010058#include <linux/platform_device.h>
Florian Fainelli49a42c02010-09-08 11:11:49 +000059#include <linux/cpu.h>
60#include <linux/io.h>
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/mipsregs.h>
63#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <asm/processor.h>
65
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090066#include <au1000.h>
Florian Fainellibd2302c2009-11-10 01:13:38 +010067#include <au1xxx_eth.h>
Yoichi Yuasa25b31cb2007-10-15 19:11:24 +090068#include <prom.h>
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include "au1000_eth.h"
71
72#ifdef AU1000_ETH_DEBUG
73static int au1000_debug = 5;
74#else
75static int au1000_debug = 3;
76#endif
77
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +000078#define AU1000_DEF_MSG_ENABLE (NETIF_MSG_DRV | \
79 NETIF_MSG_PROBE | \
80 NETIF_MSG_LINK)
81
Sergei Shtylyov89be0502006-04-19 22:46:21 +040082#define DRV_NAME "au1000_eth"
Florian Fainelli8020eb82010-04-06 22:09:20 +000083#define DRV_VERSION "1.7"
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define DRV_AUTHOR "Pete Popov <ppopov@embeddedalley.com>"
85#define DRV_DESC "Au1xxx on-chip Ethernet driver"
86
87MODULE_AUTHOR(DRV_AUTHOR);
88MODULE_DESCRIPTION(DRV_DESC);
89MODULE_LICENSE("GPL");
Florian Fainelli13130c72010-04-06 22:08:57 +000090MODULE_VERSION(DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * Theory of operation
94 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -040095 * The Au1000 MACs use a simple rx and tx descriptor ring scheme.
96 * There are four receive and four transmit descriptors. These
97 * descriptors are not in memory; rather, they are just a set of
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * hardware registers.
99 *
100 * Since the Au1000 has a coherent data cache, the receive and
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400101 * transmit buffers are allocated from the KSEG0 segment. The
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * hardware registers, however, are still mapped at KSEG1 to
103 * make sure there's no out-of-order writes, and that all writes
104 * complete immediately.
105 */
106
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200107/*
108 * board-specific configurations
109 *
110 * PHY detection algorithm
111 *
Florian Fainellibd2302c2009-11-10 01:13:38 +0100112 * If phy_static_config is undefined, the PHY setup is
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200113 * autodetected:
114 *
115 * mii_probe() first searches the current MAC's MII bus for a PHY,
Florian Fainellibd2302c2009-11-10 01:13:38 +0100116 * selecting the first (or last, if phy_search_highest_addr is
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200117 * defined) PHY address not already claimed by another netdev.
118 *
119 * If nothing was found that way when searching for the 2nd ethernet
Florian Fainellibd2302c2009-11-10 01:13:38 +0100120 * controller's PHY and phy1_search_mac0 is defined, then
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200121 * the first MII bus is searched as well for an unclaimed PHY; this is
122 * needed in case of a dual-PHY accessible only through the MAC0's MII
123 * bus.
124 *
125 * Finally, if no PHY is found, then the corresponding ethernet
126 * controller is not registered to the network subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 */
128
Florian Fainellibd2302c2009-11-10 01:13:38 +0100129/* autodetection defaults: phy1_search_mac0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200131/* static PHY setup
132 *
133 * most boards PHY setup should be detectable properly with the
134 * autodetection algorithm in mii_probe(), but in some cases (e.g. if
135 * you have a switch attached, or want to use the PHY's interrupt
136 * notification capabilities) you can provide a static PHY
137 * configuration here
138 *
139 * IRQs may only be set, if a PHY address was configured
140 * If a PHY address is given, also a bus id is required to be set
141 *
142 * ps: make sure the used irqs are configured properly in the board
143 * specific irq-map
144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Florian Fainellieb049632010-04-06 22:09:01 +0000146static void au1000_enable_mac(struct net_device *dev, int force_reset)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800147{
148 unsigned long flags;
149 struct au1000_private *aup = netdev_priv(dev);
150
151 spin_lock_irqsave(&aup->lock, flags);
152
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000153 if (force_reset || (!aup->mac_enabled)) {
Wolfgang Grandegger462ca992010-11-23 06:40:25 +0000154 writel(MAC_EN_CLOCK_ENABLE, aup->enable);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800155 au_sync_delay(2);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000156 writel((MAC_EN_RESET0 | MAC_EN_RESET1 | MAC_EN_RESET2
Wolfgang Grandegger462ca992010-11-23 06:40:25 +0000157 | MAC_EN_CLOCK_ENABLE), aup->enable);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800158 au_sync_delay(2);
159
160 aup->mac_enabled = 1;
161 }
162
163 spin_unlock_irqrestore(&aup->lock, flags);
164}
165
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200166/*
167 * MII operations
168 */
Adrian Bunk1210dde2008-10-12 21:02:19 -0700169static int au1000_mdio_read(struct net_device *dev, int phy_addr, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Wang Chen454d7c92008-11-12 23:37:49 -0800171 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000172 u32 *const mii_control_reg = &aup->mac->mii_control;
173 u32 *const mii_data_reg = &aup->mac->mii_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 u32 timedout = 20;
175 u32 mii_control;
176
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000177 while (readl(mii_control_reg) & MAC_MII_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 mdelay(1);
179 if (--timedout == 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000180 netdev_err(dev, "read_MII busy timeout!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return -1;
182 }
183 }
184
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400185 mii_control = MAC_SET_MII_SELECT_REG(reg) |
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200186 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000188 writel(mii_control, mii_control_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 timedout = 20;
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000191 while (readl(mii_control_reg) & MAC_MII_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 mdelay(1);
193 if (--timedout == 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000194 netdev_err(dev, "mdio_read busy timeout!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -1;
196 }
197 }
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000198 return readl(mii_data_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Adrian Bunk1210dde2008-10-12 21:02:19 -0700201static void au1000_mdio_write(struct net_device *dev, int phy_addr,
202 int reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Wang Chen454d7c92008-11-12 23:37:49 -0800204 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000205 u32 *const mii_control_reg = &aup->mac->mii_control;
206 u32 *const mii_data_reg = &aup->mac->mii_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 u32 timedout = 20;
208 u32 mii_control;
209
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000210 while (readl(mii_control_reg) & MAC_MII_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 mdelay(1);
212 if (--timedout == 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000213 netdev_err(dev, "mdio_write busy timeout!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return;
215 }
216 }
217
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400218 mii_control = MAC_SET_MII_SELECT_REG(reg) |
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200219 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000221 writel(value, mii_data_reg);
222 writel(mii_control, mii_control_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Adrian Bunk1210dde2008-10-12 21:02:19 -0700225static int au1000_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200227 /* WARNING: bus->phy_map[phy_addr].attached_dev == dev does
Florian Fainellidc998392010-09-08 11:11:59 +0000228 * _NOT_ hold (e.g. when PHY is accessed through other MAC's MII bus)
229 */
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200230 struct net_device *const dev = bus->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Florian Fainellidc998392010-09-08 11:11:59 +0000232 /* make sure the MAC associated with this
233 * mii_bus is enabled
234 */
235 au1000_enable_mac(dev, 0);
236
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 Fainellidc998392010-09-08 11:11:59 +0000245 /* make sure the MAC associated with this
246 * mii_bus is enabled
247 */
248 au1000_enable_mac(dev, 0);
249
Adrian Bunk1210dde2008-10-12 21:02:19 -0700250 au1000_mdio_write(dev, phy_addr, regnum, value);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200251 return 0;
252}
253
Adrian Bunk1210dde2008-10-12 21:02:19 -0700254static int au1000_mdiobus_reset(struct mii_bus *bus)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200255{
256 struct net_device *const dev = bus->priv;
257
Florian Fainellidc998392010-09-08 11:11:59 +0000258 /* make sure the MAC associated with this
259 * mii_bus is enabled
260 */
261 au1000_enable_mac(dev, 0);
262
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200263 return 0;
264}
265
Florian Fainellieb049632010-04-06 22:09:01 +0000266static void au1000_hard_stop(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800267{
268 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000269 u32 reg;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800270
Florian Fainelli5368c722010-04-06 22:09:17 +0000271 netif_dbg(aup, drv, dev, "hard stop\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800272
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000273 reg = readl(&aup->mac->control);
274 reg &= ~(MAC_RX_ENABLE | MAC_TX_ENABLE);
275 writel(reg, &aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800276 au_sync_delay(10);
277}
278
Florian Fainellieb049632010-04-06 22:09:01 +0000279static void au1000_enable_rx_tx(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800280{
281 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000282 u32 reg;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800283
Florian Fainelli5368c722010-04-06 22:09:17 +0000284 netif_dbg(aup, hw, dev, "enable_rx_tx\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800285
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000286 reg = readl(&aup->mac->control);
287 reg |= (MAC_RX_ENABLE | MAC_TX_ENABLE);
288 writel(reg, &aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800289 au_sync_delay(10);
290}
291
292static void
293au1000_adjust_link(struct net_device *dev)
294{
295 struct au1000_private *aup = netdev_priv(dev);
296 struct phy_device *phydev = aup->phy_dev;
297 unsigned long flags;
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000298 u32 reg;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800299
300 int status_change = 0;
301
302 BUG_ON(!aup->phy_dev);
303
304 spin_lock_irqsave(&aup->lock, flags);
305
306 if (phydev->link && (aup->old_speed != phydev->speed)) {
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000307 /* speed changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800308
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000309 switch (phydev->speed) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800310 case SPEED_10:
311 case SPEED_100:
312 break;
313 default:
Florian Fainelli5368c722010-04-06 22:09:17 +0000314 netdev_warn(dev, "Speed (%d) is not 10/100 ???\n",
315 phydev->speed);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800316 break;
317 }
318
319 aup->old_speed = phydev->speed;
320
321 status_change = 1;
322 }
323
324 if (phydev->link && (aup->old_duplex != phydev->duplex)) {
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000325 /* duplex mode changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800326
327 /* switching duplex mode requires to disable rx and tx! */
Florian Fainellieb049632010-04-06 22:09:01 +0000328 au1000_hard_stop(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800329
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000330 reg = readl(&aup->mac->control);
331 if (DUPLEX_FULL == phydev->duplex) {
332 reg |= MAC_FULL_DUPLEX;
333 reg &= ~MAC_DISABLE_RX_OWN;
334 } else {
335 reg &= ~MAC_FULL_DUPLEX;
336 reg |= MAC_DISABLE_RX_OWN;
337 }
338 writel(reg, &aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800339 au_sync_delay(1);
340
Florian Fainellieb049632010-04-06 22:09:01 +0000341 au1000_enable_rx_tx(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800342 aup->old_duplex = phydev->duplex;
343
344 status_change = 1;
345 }
346
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000347 if (phydev->link != aup->old_link) {
348 /* link state changed */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800349
350 if (!phydev->link) {
351 /* link went down */
352 aup->old_speed = 0;
353 aup->old_duplex = -1;
354 }
355
356 aup->old_link = phydev->link;
357 status_change = 1;
358 }
359
360 spin_unlock_irqrestore(&aup->lock, flags);
361
362 if (status_change) {
363 if (phydev->link)
Florian Fainelli5368c722010-04-06 22:09:17 +0000364 netdev_info(dev, "link up (%d/%s)\n",
365 phydev->speed,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800366 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
367 else
Florian Fainelli5368c722010-04-06 22:09:17 +0000368 netdev_info(dev, "link down\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800369 }
370}
371
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000372static int au1000_mii_probe(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200373{
Wang Chen454d7c92008-11-12 23:37:49 -0800374 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200375 struct phy_device *phydev = NULL;
Florian Fainelli18b8e152010-09-08 11:11:40 +0000376 int phy_addr;
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200377
Florian Fainellibd2302c2009-11-10 01:13:38 +0100378 if (aup->phy_static_config) {
379 BUG_ON(aup->mac_id < 0 || aup->mac_id > 1);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200380
Florian Fainellibd2302c2009-11-10 01:13:38 +0100381 if (aup->phy_addr)
382 phydev = aup->mii_bus->phy_map[aup->phy_addr];
383 else
Florian Fainelli5368c722010-04-06 22:09:17 +0000384 netdev_info(dev, "using PHY-less setup\n");
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200385 return 0;
Florian Fainelli18b8e152010-09-08 11:11:40 +0000386 }
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200387
Florian Fainelli18b8e152010-09-08 11:11:40 +0000388 /* find the first (lowest address) PHY
Florian Fainellidc998392010-09-08 11:11:59 +0000389 * on the current MAC's MII bus
390 */
Florian Fainelli18b8e152010-09-08 11:11:40 +0000391 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
392 if (aup->mii_bus->phy_map[phy_addr]) {
393 phydev = aup->mii_bus->phy_map[phy_addr];
394 if (!aup->phy_search_highest_addr)
395 /* break out with first one found */
396 break;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Florian Fainelli18b8e152010-09-08 11:11:40 +0000399 if (aup->phy1_search_mac0) {
400 /* try harder to find a PHY */
401 if (!phydev && (aup->mac_id == 1)) {
402 /* no PHY found, maybe we have a dual PHY? */
403 dev_info(&dev->dev, ": no PHY found on MAC1, "
404 "let's see if it's attached to MAC0...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Florian Fainelli18b8e152010-09-08 11:11:40 +0000406 /* find the first (lowest address) non-attached
407 * PHY on the MAC0 MII bus
408 */
409 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
410 struct phy_device *const tmp_phydev =
411 aup->mii_bus->phy_map[phy_addr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Florian Fainelli18b8e152010-09-08 11:11:40 +0000413 if (aup->mac_id == 1)
414 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Florian Fainelli18b8e152010-09-08 11:11:40 +0000416 /* no PHY here... */
417 if (!tmp_phydev)
418 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Florian Fainelli18b8e152010-09-08 11:11:40 +0000420 /* already claimed by MAC0 */
421 if (tmp_phydev->attached_dev)
422 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Florian Fainelli18b8e152010-09-08 11:11:40 +0000424 phydev = tmp_phydev;
425 break; /* found it */
Florian Fainellibd2302c2009-11-10 01:13:38 +0100426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200430 if (!phydev) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000431 netdev_err(dev, "no PHY found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return -1;
433 }
434
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200435 /* now we are supposed to have a proper phydev, to attach to... */
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200436 BUG_ON(phydev->attached_dev);
437
Florian Fainellif9a8f832013-01-14 00:52:52 +0000438 phydev = phy_connect(dev, dev_name(&phydev->dev),
439 &au1000_adjust_link, PHY_INTERFACE_MODE_MII);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200440
441 if (IS_ERR(phydev)) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000442 netdev_err(dev, "Could not attach to PHY\n");
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200443 return PTR_ERR(phydev);
444 }
445
446 /* mask with MAC supported features */
447 phydev->supported &= (SUPPORTED_10baseT_Half
448 | SUPPORTED_10baseT_Full
449 | SUPPORTED_100baseT_Half
450 | SUPPORTED_100baseT_Full
451 | SUPPORTED_Autoneg
452 /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
453 | SUPPORTED_MII
454 | SUPPORTED_TP);
455
456 phydev->advertising = phydev->supported;
457
458 aup->old_link = 0;
459 aup->old_speed = 0;
460 aup->old_duplex = -1;
461 aup->phy_dev = phydev;
462
Florian Fainelli5368c722010-04-06 22:09:17 +0000463 netdev_info(dev, "attached PHY driver [%s] "
464 "(mii_bus:phy_addr=%s, irq=%d)\n",
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800465 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 return 0;
468}
469
470
471/*
472 * Buffer allocation/deallocation routines. The buffer descriptor returned
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400473 * has the virtual and dma address of a buffer suitable for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 * both, receive and transmit operations.
475 */
Florian Fainelli34415922010-09-08 11:11:25 +0000476static struct db_dest *au1000_GetFreeDB(struct au1000_private *aup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Florian Fainelli34415922010-09-08 11:11:25 +0000478 struct db_dest *pDB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 pDB = aup->pDBfree;
480
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000481 if (pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 aup->pDBfree = pDB->pnext;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return pDB;
485}
486
Florian Fainelli34415922010-09-08 11:11:25 +0000487void au1000_ReleaseDB(struct au1000_private *aup, struct db_dest *pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Florian Fainelli34415922010-09-08 11:11:25 +0000489 struct db_dest *pDBfree = aup->pDBfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (pDBfree)
491 pDBfree->pnext = pDB;
492 aup->pDBfree = pDB;
493}
494
Florian Fainellieb049632010-04-06 22:09:01 +0000495static void au1000_reset_mac_unlocked(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200496{
Wang Chen454d7c92008-11-12 23:37:49 -0800497 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200498 int i;
499
Florian Fainellieb049632010-04-06 22:09:01 +0000500 au1000_hard_stop(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200501
Wolfgang Grandegger462ca992010-11-23 06:40:25 +0000502 writel(MAC_EN_CLOCK_ENABLE, aup->enable);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200503 au_sync_delay(2);
Wolfgang Grandegger462ca992010-11-23 06:40:25 +0000504 writel(0, aup->enable);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200505 au_sync_delay(2);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 aup->tx_full = 0;
508 for (i = 0; i < NUM_RX_DMA; i++) {
509 /* reset control bits */
510 aup->rx_dma_ring[i]->buff_stat &= ~0xf;
511 }
512 for (i = 0; i < NUM_TX_DMA; i++) {
513 /* reset control bits */
514 aup->tx_dma_ring[i]->buff_stat &= ~0xf;
515 }
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200516
517 aup->mac_enabled = 0;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Florian Fainellieb049632010-04-06 22:09:01 +0000521static void au1000_reset_mac(struct net_device *dev)
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200522{
Wang Chen454d7c92008-11-12 23:37:49 -0800523 struct au1000_private *const aup = netdev_priv(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200524 unsigned long flags;
525
Florian Fainelli5368c722010-04-06 22:09:17 +0000526 netif_dbg(aup, hw, dev, "reset mac, aup %x\n",
527 (unsigned)aup);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200528
529 spin_lock_irqsave(&aup->lock, flags);
530
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000531 au1000_reset_mac_unlocked(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200532
533 spin_unlock_irqrestore(&aup->lock, flags);
534}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400536/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * Setup the receive and transmit "rings". These pointers are the addresses
538 * of the rx and tx MAC DMA registers so they are fixed by the hardware --
539 * these are not descriptors sitting in memory.
540 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400541static void
Linus Torvaldsd6748062011-11-03 13:28:14 -0700542au1000_setup_hw_rings(struct au1000_private *aup, void __iomem *tx_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 int i;
545
546 for (i = 0; i < NUM_RX_DMA; i++) {
Linus Torvaldsd6748062011-11-03 13:28:14 -0700547 aup->rx_dma_ring[i] = (struct rx_dma *)
548 (tx_base + 0x100 + sizeof(struct rx_dma) * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 for (i = 0; i < NUM_TX_DMA; i++) {
Linus Torvaldsd6748062011-11-03 13:28:14 -0700551 aup->tx_dma_ring[i] = (struct tx_dma *)
552 (tx_base + sizeof(struct tx_dma) * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/*
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200557 * ethtool operations
558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560static int au1000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
561{
Wang Chen454d7c92008-11-12 23:37:49 -0800562 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200564 if (aup->phy_dev)
565 return phy_ethtool_gset(aup->phy_dev, cmd);
566
567 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570static int au1000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
571{
Wang Chen454d7c92008-11-12 23:37:49 -0800572 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200574 if (!capable(CAP_NET_ADMIN))
575 return -EPERM;
576
577 if (aup->phy_dev)
578 return phy_ethtool_sset(aup->phy_dev, cmd);
579
580 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583static void
584au1000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
585{
Wang Chen454d7c92008-11-12 23:37:49 -0800586 struct au1000_private *aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Jiri Pirko7826d432013-01-06 00:44:26 +0000588 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
589 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
590 snprintf(info->bus_info, sizeof(info->bus_info), "%s %d", DRV_NAME,
591 aup->mac_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 info->regdump_len = 0;
593}
594
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +0000595static void au1000_set_msglevel(struct net_device *dev, u32 value)
596{
597 struct au1000_private *aup = netdev_priv(dev);
598 aup->msg_enable = value;
599}
600
601static u32 au1000_get_msglevel(struct net_device *dev)
602{
603 struct au1000_private *aup = netdev_priv(dev);
604 return aup->msg_enable;
605}
606
Jeff Garzik7282d492006-09-13 14:30:00 -0400607static const struct ethtool_ops au1000_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 .get_settings = au1000_get_settings,
609 .set_settings = au1000_set_settings,
610 .get_drvinfo = au1000_get_drvinfo,
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +0200611 .get_link = ethtool_op_get_link,
Florian Fainelli7cd2e6e2010-04-06 22:09:09 +0000612 .get_msglevel = au1000_get_msglevel,
613 .set_msglevel = au1000_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614};
615
Florian Fainelli5ef30412009-01-22 14:06:25 -0800616
617/*
618 * Initialize the interface.
619 *
620 * When the device powers up, the clocks are disabled and the
621 * mac is in reset state. When the interface is closed, we
622 * do the same -- reset the device and disable the clocks to
623 * conserve power. Thus, whenever au1000_init() is called,
624 * the device should already be in reset state.
625 */
626static int au1000_init(struct net_device *dev)
627{
628 struct au1000_private *aup = netdev_priv(dev);
629 unsigned long flags;
630 int i;
631 u32 control;
632
Florian Fainelli5368c722010-04-06 22:09:17 +0000633 netif_dbg(aup, hw, dev, "au1000_init\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800634
635 /* bring the device out of reset */
Florian Fainellieb049632010-04-06 22:09:01 +0000636 au1000_enable_mac(dev, 1);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800637
638 spin_lock_irqsave(&aup->lock, flags);
639
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000640 writel(0, &aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800641 aup->tx_head = (aup->tx_dma_ring[0]->buff_stat & 0xC) >> 2;
642 aup->tx_tail = aup->tx_head;
643 aup->rx_head = (aup->rx_dma_ring[0]->buff_stat & 0xC) >> 2;
644
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000645 writel(dev->dev_addr[5]<<8 | dev->dev_addr[4],
646 &aup->mac->mac_addr_high);
647 writel(dev->dev_addr[3]<<24 | dev->dev_addr[2]<<16 |
648 dev->dev_addr[1]<<8 | dev->dev_addr[0],
649 &aup->mac->mac_addr_low);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800650
Florian Fainelli18b8e152010-09-08 11:11:40 +0000651
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000652 for (i = 0; i < NUM_RX_DMA; i++)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800653 aup->rx_dma_ring[i]->buff_stat |= RX_DMA_ENABLE;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000654
Florian Fainelli5ef30412009-01-22 14:06:25 -0800655 au_sync();
656
657 control = MAC_RX_ENABLE | MAC_TX_ENABLE;
658#ifndef CONFIG_CPU_LITTLE_ENDIAN
659 control |= MAC_BIG_ENDIAN;
660#endif
661 if (aup->phy_dev) {
662 if (aup->phy_dev->link && (DUPLEX_FULL == aup->phy_dev->duplex))
663 control |= MAC_FULL_DUPLEX;
664 else
665 control |= MAC_DISABLE_RX_OWN;
666 } else { /* PHY-less op, assume full-duplex */
667 control |= MAC_FULL_DUPLEX;
668 }
669
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000670 writel(control, &aup->mac->control);
671 writel(0x8100, &aup->mac->vlan1_tag); /* activate vlan support */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800672 au_sync();
673
674 spin_unlock_irqrestore(&aup->lock, flags);
675 return 0;
676}
677
Florian Fainellieb049632010-04-06 22:09:01 +0000678static inline void au1000_update_rx_stats(struct net_device *dev, u32 status)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800679{
Florian Fainelli5ef30412009-01-22 14:06:25 -0800680 struct net_device_stats *ps = &dev->stats;
681
682 ps->rx_packets++;
683 if (status & RX_MCAST_FRAME)
684 ps->multicast++;
685
686 if (status & RX_ERROR) {
687 ps->rx_errors++;
688 if (status & RX_MISSED_FRAME)
689 ps->rx_missed_errors++;
roel kluin4989ccb2009-10-06 09:54:18 +0000690 if (status & (RX_OVERLEN | RX_RUNT | RX_LEN_ERROR))
Florian Fainelli5ef30412009-01-22 14:06:25 -0800691 ps->rx_length_errors++;
692 if (status & RX_CRC_ERROR)
693 ps->rx_crc_errors++;
694 if (status & RX_COLL)
695 ps->collisions++;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000696 } else
Florian Fainelli5ef30412009-01-22 14:06:25 -0800697 ps->rx_bytes += status & RX_FRAME_LEN_MASK;
698
699}
700
701/*
702 * Au1000 receive routine.
703 */
704static int au1000_rx(struct net_device *dev)
705{
706 struct au1000_private *aup = netdev_priv(dev);
707 struct sk_buff *skb;
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000708 struct rx_dma *prxd;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800709 u32 buff_stat, status;
Florian Fainelli34415922010-09-08 11:11:25 +0000710 struct db_dest *pDB;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800711 u32 frmlen;
712
Florian Fainelli5368c722010-04-06 22:09:17 +0000713 netif_dbg(aup, rx_status, dev, "au1000_rx head %d\n", aup->rx_head);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800714
715 prxd = aup->rx_dma_ring[aup->rx_head];
716 buff_stat = prxd->buff_stat;
717 while (buff_stat & RX_T_DONE) {
718 status = prxd->status;
719 pDB = aup->rx_db_inuse[aup->rx_head];
Florian Fainellieb049632010-04-06 22:09:01 +0000720 au1000_update_rx_stats(dev, status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800721 if (!(status & RX_ERROR)) {
722
723 /* good frame */
724 frmlen = (status & RX_FRAME_LEN_MASK);
725 frmlen -= 4; /* Remove FCS */
Pradeep A Dalvi1d266432012-02-05 02:49:09 +0000726 skb = netdev_alloc_skb(dev, frmlen + 2);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800727 if (skb == NULL) {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800728 dev->stats.rx_dropped++;
729 continue;
730 }
731 skb_reserve(skb, 2); /* 16 byte IP header align */
732 skb_copy_to_linear_data(skb,
733 (unsigned char *)pDB->vaddr, frmlen);
734 skb_put(skb, frmlen);
735 skb->protocol = eth_type_trans(skb, dev);
736 netif_rx(skb); /* pass the packet to upper layers */
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000737 } else {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800738 if (au1000_debug > 4) {
Florian Fainelli215e17b2010-09-08 11:11:45 +0000739 pr_err("rx_error(s):");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800740 if (status & RX_MISSED_FRAME)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000741 pr_cont(" miss");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800742 if (status & RX_WDOG_TIMER)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000743 pr_cont(" wdog");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800744 if (status & RX_RUNT)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000745 pr_cont(" runt");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800746 if (status & RX_OVERLEN)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000747 pr_cont(" overlen");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800748 if (status & RX_COLL)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000749 pr_cont(" coll");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800750 if (status & RX_MII_ERROR)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000751 pr_cont(" mii error");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800752 if (status & RX_CRC_ERROR)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000753 pr_cont(" crc error");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800754 if (status & RX_LEN_ERROR)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000755 pr_cont(" len error");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800756 if (status & RX_U_CNTRL_FRAME)
Florian Fainelli215e17b2010-09-08 11:11:45 +0000757 pr_cont(" u control frame");
758 pr_cont("\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800759 }
760 }
761 prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE);
762 aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1);
763 au_sync();
764
765 /* next descriptor */
766 prxd = aup->rx_dma_ring[aup->rx_head];
767 buff_stat = prxd->buff_stat;
768 }
769 return 0;
770}
771
Florian Fainellieb049632010-04-06 22:09:01 +0000772static void au1000_update_tx_stats(struct net_device *dev, u32 status)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800773{
774 struct au1000_private *aup = netdev_priv(dev);
775 struct net_device_stats *ps = &dev->stats;
776
777 if (status & TX_FRAME_ABORTED) {
778 if (!aup->phy_dev || (DUPLEX_FULL == aup->phy_dev->duplex)) {
779 if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) {
780 /* any other tx errors are only valid
Florian Fainellidc998392010-09-08 11:11:59 +0000781 * in half duplex mode
782 */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800783 ps->tx_errors++;
784 ps->tx_aborted_errors++;
785 }
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000786 } else {
Florian Fainelli5ef30412009-01-22 14:06:25 -0800787 ps->tx_errors++;
788 ps->tx_aborted_errors++;
789 if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER))
790 ps->tx_carrier_errors++;
791 }
792 }
793}
794
795/*
796 * Called from the interrupt service routine to acknowledge
797 * the TX DONE bits. This is a must if the irq is setup as
798 * edge triggered.
799 */
800static void au1000_tx_ack(struct net_device *dev)
801{
802 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000803 struct tx_dma *ptxd;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800804
805 ptxd = aup->tx_dma_ring[aup->tx_tail];
806
807 while (ptxd->buff_stat & TX_T_DONE) {
Florian Fainellieb049632010-04-06 22:09:01 +0000808 au1000_update_tx_stats(dev, ptxd->status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800809 ptxd->buff_stat &= ~TX_T_DONE;
810 ptxd->len = 0;
811 au_sync();
812
813 aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1);
814 ptxd = aup->tx_dma_ring[aup->tx_tail];
815
816 if (aup->tx_full) {
817 aup->tx_full = 0;
818 netif_wake_queue(dev);
819 }
820 }
821}
822
823/*
824 * Au1000 interrupt service routine.
825 */
826static irqreturn_t au1000_interrupt(int irq, void *dev_id)
827{
828 struct net_device *dev = dev_id;
829
830 /* Handle RX interrupts first to minimize chance of overrun */
831
832 au1000_rx(dev);
833 au1000_tx_ack(dev);
834 return IRQ_RETVAL(1);
835}
836
837static int au1000_open(struct net_device *dev)
838{
839 int retval;
840 struct au1000_private *aup = netdev_priv(dev);
841
Florian Fainelli5368c722010-04-06 22:09:17 +0000842 netif_dbg(aup, drv, dev, "open: dev=%p\n", dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800843
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000844 retval = request_irq(dev->irq, au1000_interrupt, 0,
845 dev->name, dev);
846 if (retval) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000847 netdev_err(dev, "unable to get IRQ %d\n", dev->irq);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800848 return retval;
849 }
850
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000851 retval = au1000_init(dev);
852 if (retval) {
Florian Fainelli5368c722010-04-06 22:09:17 +0000853 netdev_err(dev, "error in au1000_init\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800854 free_irq(dev->irq, dev);
855 return retval;
856 }
857
858 if (aup->phy_dev) {
859 /* cause the PHY state machine to schedule a link state check */
860 aup->phy_dev->state = PHY_CHANGELINK;
861 phy_start(aup->phy_dev);
862 }
863
864 netif_start_queue(dev);
865
Florian Fainelli5368c722010-04-06 22:09:17 +0000866 netif_dbg(aup, drv, dev, "open: Initialization done.\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800867
868 return 0;
869}
870
871static int au1000_close(struct net_device *dev)
872{
873 unsigned long flags;
874 struct au1000_private *const aup = netdev_priv(dev);
875
Florian Fainelli5368c722010-04-06 22:09:17 +0000876 netif_dbg(aup, drv, dev, "close: dev=%p\n", dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800877
878 if (aup->phy_dev)
879 phy_stop(aup->phy_dev);
880
881 spin_lock_irqsave(&aup->lock, flags);
882
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000883 au1000_reset_mac_unlocked(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800884
885 /* stop the device */
886 netif_stop_queue(dev);
887
888 /* disable the interrupt */
889 free_irq(dev->irq, dev);
890 spin_unlock_irqrestore(&aup->lock, flags);
891
892 return 0;
893}
894
895/*
896 * Au1000 transmit routine.
897 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000898static netdev_tx_t au1000_tx(struct sk_buff *skb, struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800899{
900 struct au1000_private *aup = netdev_priv(dev);
901 struct net_device_stats *ps = &dev->stats;
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000902 struct tx_dma *ptxd;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800903 u32 buff_stat;
Florian Fainelli34415922010-09-08 11:11:25 +0000904 struct db_dest *pDB;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800905 int i;
906
Florian Fainelli5368c722010-04-06 22:09:17 +0000907 netif_dbg(aup, tx_queued, dev, "tx: aup %x len=%d, data=%p, head %d\n",
908 (unsigned)aup, skb->len,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800909 skb->data, aup->tx_head);
910
911 ptxd = aup->tx_dma_ring[aup->tx_head];
912 buff_stat = ptxd->buff_stat;
913 if (buff_stat & TX_DMA_ENABLE) {
914 /* We've wrapped around and the transmitter is still busy */
915 netif_stop_queue(dev);
916 aup->tx_full = 1;
Patrick McHardy5b548142009-06-12 06:22:29 +0000917 return NETDEV_TX_BUSY;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000918 } else if (buff_stat & TX_T_DONE) {
Florian Fainellieb049632010-04-06 22:09:01 +0000919 au1000_update_tx_stats(dev, ptxd->status);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800920 ptxd->len = 0;
921 }
922
923 if (aup->tx_full) {
924 aup->tx_full = 0;
925 netif_wake_queue(dev);
926 }
927
928 pDB = aup->tx_db_inuse[aup->tx_head];
Florian Fainellibd2302c2009-11-10 01:13:38 +0100929 skb_copy_from_linear_data(skb, (void *)pDB->vaddr, skb->len);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800930 if (skb->len < ETH_ZLEN) {
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000931 for (i = skb->len; i < ETH_ZLEN; i++)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800932 ((char *)pDB->vaddr)[i] = 0;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +0000933
Florian Fainelli5ef30412009-01-22 14:06:25 -0800934 ptxd->len = ETH_ZLEN;
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000935 } else
Florian Fainelli5ef30412009-01-22 14:06:25 -0800936 ptxd->len = skb->len;
937
938 ps->tx_packets++;
939 ps->tx_bytes += ptxd->len;
940
941 ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE;
942 au_sync();
943 dev_kfree_skb(skb);
944 aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000945 return NETDEV_TX_OK;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800946}
947
948/*
949 * The Tx ring has been full longer than the watchdog timeout
950 * value. The transmitter must be hung?
951 */
952static void au1000_tx_timeout(struct net_device *dev)
953{
Florian Fainelli5368c722010-04-06 22:09:17 +0000954 netdev_err(dev, "au1000_tx_timeout: dev=%p\n", dev);
Florian Fainellieb049632010-04-06 22:09:01 +0000955 au1000_reset_mac(dev);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800956 au1000_init(dev);
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700957 dev->trans_start = jiffies; /* prevent tx timeout */
Florian Fainelli5ef30412009-01-22 14:06:25 -0800958 netif_wake_queue(dev);
959}
960
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +0000961static void au1000_multicast_list(struct net_device *dev)
Florian Fainelli5ef30412009-01-22 14:06:25 -0800962{
963 struct au1000_private *aup = netdev_priv(dev);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000964 u32 reg;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800965
Florian Fainelli18b8e152010-09-08 11:11:40 +0000966 netif_dbg(aup, drv, dev, "%s: flags=%x\n", __func__, dev->flags);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000967 reg = readl(&aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800968 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000969 reg |= MAC_PROMISCUOUS;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800970 } else if ((dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000971 netdev_mc_count(dev) > MULTICAST_FILTER_LIMIT) {
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000972 reg |= MAC_PASS_ALL_MULTI;
973 reg &= ~MAC_PROMISCUOUS;
Florian Fainelli5368c722010-04-06 22:09:17 +0000974 netdev_info(dev, "Pass all multicast\n");
Florian Fainelli5ef30412009-01-22 14:06:25 -0800975 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000976 struct netdev_hw_addr *ha;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800977 u32 mc_filter[2]; /* Multicast hash filter */
978
979 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000980 netdev_for_each_mc_addr(ha, dev)
981 set_bit(ether_crc(ETH_ALEN, ha->addr)>>26,
Florian Fainelli5ef30412009-01-22 14:06:25 -0800982 (long *)mc_filter);
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000983 writel(mc_filter[1], &aup->mac->multi_hash_high);
984 writel(mc_filter[0], &aup->mac->multi_hash_low);
985 reg &= ~MAC_PROMISCUOUS;
986 reg |= MAC_HASH_MODE;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800987 }
Florian Fainellid0e7cb52010-09-08 11:15:13 +0000988 writel(reg, &aup->mac->control);
Florian Fainelli5ef30412009-01-22 14:06:25 -0800989}
990
991static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
992{
993 struct au1000_private *aup = netdev_priv(dev);
994
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000995 if (!netif_running(dev))
996 return -EINVAL;
Florian Fainelli5ef30412009-01-22 14:06:25 -0800997
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +0000998 if (!aup->phy_dev)
999 return -EINVAL; /* PHY not controllable */
Florian Fainelli5ef30412009-01-22 14:06:25 -08001000
Richard Cochran28b04112010-07-17 08:48:55 +00001001 return phy_mii_ioctl(aup->phy_dev, rq, cmd);
Florian Fainelli5ef30412009-01-22 14:06:25 -08001002}
1003
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +00001004static const struct net_device_ops au1000_netdev_ops = {
1005 .ndo_open = au1000_open,
1006 .ndo_stop = au1000_close,
1007 .ndo_start_xmit = au1000_tx,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001008 .ndo_set_rx_mode = au1000_multicast_list,
Alexander Beregalovd9a92ce2009-04-14 18:30:23 +00001009 .ndo_do_ioctl = au1000_ioctl,
1010 .ndo_tx_timeout = au1000_tx_timeout,
1011 .ndo_set_mac_address = eth_mac_addr,
1012 .ndo_validate_addr = eth_validate_addr,
1013 .ndo_change_mtu = eth_change_mtu,
1014};
1015
Bill Pemberton0cb05682012-12-03 09:23:54 -05001016static int au1000_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +00001018 static unsigned version_printed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 struct au1000_private *aup = NULL;
Florian Fainellibd2302c2009-11-10 01:13:38 +01001020 struct au1000_eth_platform_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct net_device *dev = NULL;
Florian Fainelli34415922010-09-08 11:11:25 +00001022 struct db_dest *pDB, *pDBfree;
Florian Fainellibd2302c2009-11-10 01:13:38 +01001023 int irq, i, err = 0;
Linus Torvaldsd6748062011-11-03 13:28:14 -07001024 struct resource *base, *macen, *macdma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Florian Fainellibd2302c2009-11-10 01:13:38 +01001026 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1027 if (!base) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001028 dev_err(&pdev->dev, "failed to retrieve base register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001029 err = -ENODEV;
1030 goto out;
1031 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001032
Florian Fainellibd2302c2009-11-10 01:13:38 +01001033 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1034 if (!macen) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001035 dev_err(&pdev->dev, "failed to retrieve MAC Enable register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001036 err = -ENODEV;
1037 goto out;
1038 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001039
Florian Fainellibd2302c2009-11-10 01:13:38 +01001040 irq = platform_get_irq(pdev, 0);
1041 if (irq < 0) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001042 dev_err(&pdev->dev, "failed to retrieve IRQ\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001043 err = -ENODEV;
1044 goto out;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Linus Torvaldsd6748062011-11-03 13:28:14 -07001047 macdma = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1048 if (!macdma) {
1049 dev_err(&pdev->dev, "failed to retrieve MACDMA registers\n");
1050 err = -ENODEV;
1051 goto out;
1052 }
1053
Florian Fainelli18b8e152010-09-08 11:11:40 +00001054 if (!request_mem_region(base->start, resource_size(base),
1055 pdev->name)) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001056 dev_err(&pdev->dev, "failed to request memory region for base registers\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001057 err = -ENXIO;
1058 goto out;
1059 }
1060
Florian Fainelli18b8e152010-09-08 11:11:40 +00001061 if (!request_mem_region(macen->start, resource_size(macen),
1062 pdev->name)) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001063 dev_err(&pdev->dev, "failed to request memory region for MAC enable register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001064 err = -ENXIO;
1065 goto err_request;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Linus Torvaldsd6748062011-11-03 13:28:14 -07001068 if (!request_mem_region(macdma->start, resource_size(macdma),
1069 pdev->name)) {
1070 dev_err(&pdev->dev, "failed to request MACDMA memory region\n");
1071 err = -ENXIO;
1072 goto err_macdma;
1073 }
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 dev = alloc_etherdev(sizeof(struct au1000_private));
1076 if (!dev) {
Florian Fainellibd2302c2009-11-10 01:13:38 +01001077 err = -ENOMEM;
1078 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
Florian Fainellibd2302c2009-11-10 01:13:38 +01001081 SET_NETDEV_DEV(dev, &pdev->dev);
1082 platform_set_drvdata(pdev, dev);
Wang Chen454d7c92008-11-12 23:37:49 -08001083 aup = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Martin Gebert533763d2008-07-23 09:40:09 +02001085 spin_lock_init(&aup->lock);
Florian Fainelli18b8e152010-09-08 11:11:40 +00001086 aup->msg_enable = (au1000_debug < 4 ?
1087 AU1000_DEF_MSG_ENABLE : au1000_debug);
Martin Gebert533763d2008-07-23 09:40:09 +02001088
Florian Fainellidc998392010-09-08 11:11:59 +00001089 /* Allocate the data buffers
1090 * Snooping works fine with eth on all au1xxx
1091 */
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001092 aup->vaddr = (u32)dma_alloc_noncoherent(NULL, MAX_BUF_SIZE *
1093 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1094 &aup->dma_addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!aup->vaddr) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001096 dev_err(&pdev->dev, "failed to allocate data buffers\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001097 err = -ENOMEM;
1098 goto err_vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
1101 /* aup->mac is the base address of the MAC's registers */
Florian Fainellid0e7cb52010-09-08 11:15:13 +00001102 aup->mac = (struct mac_reg *)
Florian Fainelli18b8e152010-09-08 11:11:40 +00001103 ioremap_nocache(base->start, resource_size(base));
Florian Fainellibd2302c2009-11-10 01:13:38 +01001104 if (!aup->mac) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001105 dev_err(&pdev->dev, "failed to ioremap MAC registers\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001106 err = -ENXIO;
1107 goto err_remap1;
1108 }
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001109
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001110 /* Setup some variables for quick register address access */
Florian Fainellid0e7cb52010-09-08 11:15:13 +00001111 aup->enable = (u32 *)ioremap_nocache(macen->start,
Florian Fainelli18b8e152010-09-08 11:11:40 +00001112 resource_size(macen));
Florian Fainellibd2302c2009-11-10 01:13:38 +01001113 if (!aup->enable) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001114 dev_err(&pdev->dev, "failed to ioremap MAC enable register\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001115 err = -ENXIO;
1116 goto err_remap2;
1117 }
1118 aup->mac_id = pdev->id;
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001119
Linus Torvaldsd6748062011-11-03 13:28:14 -07001120 aup->macdma = ioremap_nocache(macdma->start, resource_size(macdma));
1121 if (!aup->macdma) {
1122 dev_err(&pdev->dev, "failed to ioremap MACDMA registers\n");
1123 err = -ENXIO;
1124 goto err_remap3;
1125 }
1126
1127 au1000_setup_hw_rings(aup, aup->macdma);
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001128
Wolfgang Grandegger462ca992010-11-23 06:40:25 +00001129 writel(0, aup->enable);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001130 aup->mac_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Jingoo Han1fc2c462013-08-30 13:51:45 +09001132 pd = dev_get_platdata(&pdev->dev);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001133 if (!pd) {
Florian Fainelli18b8e152010-09-08 11:11:40 +00001134 dev_info(&pdev->dev, "no platform_data passed,"
1135 " PHY search on MAC0\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001136 aup->phy1_search_mac0 = 1;
1137 } else {
Danny Kukawka7718f2c2012-02-17 05:43:22 +00001138 if (is_valid_ether_addr(pd->mac)) {
Joe Perchesd458cdf2013-10-01 19:04:40 -07001139 memcpy(dev->dev_addr, pd->mac, ETH_ALEN);
Danny Kukawka7718f2c2012-02-17 05:43:22 +00001140 } else {
1141 /* Set a random MAC since no valid provided by platform_data. */
1142 eth_hw_addr_random(dev);
1143 }
Manuel Laussf6673652010-07-21 14:30:50 +02001144
Florian Fainellibd2302c2009-11-10 01:13:38 +01001145 aup->phy_static_config = pd->phy_static_config;
1146 aup->phy_search_highest_addr = pd->phy_search_highest_addr;
1147 aup->phy1_search_mac0 = pd->phy1_search_mac0;
1148 aup->phy_addr = pd->phy_addr;
1149 aup->phy_busid = pd->phy_busid;
1150 aup->phy_irq = pd->phy_irq;
1151 }
1152
1153 if (aup->phy_busid && aup->phy_busid > 0) {
Florian Fainelli18b8e152010-09-08 11:11:40 +00001154 dev_err(&pdev->dev, "MAC0-associated PHY attached 2nd MACs MII bus not supported yet\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001155 err = -ENODEV;
1156 goto err_mdiobus_alloc;
1157 }
1158
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001159 aup->mii_bus = mdiobus_alloc();
Florian Fainellibd2302c2009-11-10 01:13:38 +01001160 if (aup->mii_bus == NULL) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001161 dev_err(&pdev->dev, "failed to allocate mdiobus structure\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001162 err = -ENOMEM;
1163 goto err_mdiobus_alloc;
1164 }
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001165
1166 aup->mii_bus->priv = dev;
Adrian Bunk1210dde2008-10-12 21:02:19 -07001167 aup->mii_bus->read = au1000_mdiobus_read;
1168 aup->mii_bus->write = au1000_mdiobus_write;
1169 aup->mii_bus->reset = au1000_mdiobus_reset;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001170 aup->mii_bus->name = "au1000_eth_mii";
Florian Fainellif74299b2012-01-09 23:59:09 +00001171 snprintf(aup->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1172 pdev->name, aup->mac_id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001173 aup->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
Peter Senna Tschudin69129922012-10-05 12:10:52 +00001174 if (aup->mii_bus->irq == NULL) {
1175 err = -ENOMEM;
roel kluindcbfef82009-08-30 22:40:15 +00001176 goto err_out;
Peter Senna Tschudin69129922012-10-05 12:10:52 +00001177 }
roel kluindcbfef82009-08-30 22:40:15 +00001178
Florian Fainelli2cc3c6b2010-04-06 22:09:06 +00001179 for (i = 0; i < PHY_MAX_ADDR; ++i)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001180 aup->mii_bus->irq[i] = PHY_POLL;
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001181 /* if known, set corresponding PHY IRQs */
Florian Fainellibd2302c2009-11-10 01:13:38 +01001182 if (aup->phy_static_config)
1183 if (aup->phy_irq && aup->phy_busid == aup->mac_id)
1184 aup->mii_bus->irq[aup->phy_addr] = aup->phy_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Florian Fainellibd2302c2009-11-10 01:13:38 +01001186 err = mdiobus_register(aup->mii_bus);
1187 if (err) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001188 dev_err(&pdev->dev, "failed to register MDIO bus\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001189 goto err_mdiobus_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191
Peter Senna Tschudin69129922012-10-05 12:10:52 +00001192 err = au1000_mii_probe(dev);
1193 if (err != 0)
Florian Fainellibd2302c2009-11-10 01:13:38 +01001194 goto err_out;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 pDBfree = NULL;
1197 /* setup the data buffer descriptors and attach a buffer to each one */
1198 pDB = aup->db;
1199 for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) {
1200 pDB->pnext = pDBfree;
1201 pDBfree = pDB;
1202 pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i);
1203 pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
1204 pDB++;
1205 }
1206 aup->pDBfree = pDBfree;
1207
Peter Senna Tschudin69129922012-10-05 12:10:52 +00001208 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 for (i = 0; i < NUM_RX_DMA; i++) {
Florian Fainellieb049632010-04-06 22:09:01 +00001210 pDB = au1000_GetFreeDB(aup);
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001211 if (!pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto err_out;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1215 aup->rx_db_inuse[i] = pDB;
1216 }
Peter Senna Tschudin69129922012-10-05 12:10:52 +00001217
1218 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 for (i = 0; i < NUM_TX_DMA; i++) {
Florian Fainellieb049632010-04-06 22:09:01 +00001220 pDB = au1000_GetFreeDB(aup);
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001221 if (!pDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 goto err_out;
Florian Fainelliec7eabdd2010-09-08 11:11:31 +00001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1225 aup->tx_dma_ring[i]->len = 0;
1226 aup->tx_db_inuse[i] = pDB;
1227 }
1228
Florian Fainellibd2302c2009-11-10 01:13:38 +01001229 dev->base_addr = base->start;
1230 dev->irq = irq;
1231 dev->netdev_ops = &au1000_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001232 dev->ethtool_ops = &au1000_ethtool_ops;
Florian Fainellibd2302c2009-11-10 01:13:38 +01001233 dev->watchdog_timeo = ETH_TX_TIMEOUT;
1234
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001235 /*
1236 * The boot code uses the ethernet controller, so reset it to start
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 * fresh. au1000_init() expects that the device is in reset state.
1238 */
Florian Fainellieb049632010-04-06 22:09:01 +00001239 au1000_reset_mac(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Florian Fainellibd2302c2009-11-10 01:13:38 +01001241 err = register_netdev(dev);
1242 if (err) {
Florian Fainelli5368c722010-04-06 22:09:17 +00001243 netdev_err(dev, "Cannot register net device, aborting.\n");
Florian Fainellibd2302c2009-11-10 01:13:38 +01001244 goto err_out;
1245 }
1246
Florian Fainelli5368c722010-04-06 22:09:17 +00001247 netdev_info(dev, "Au1xx0 Ethernet found at 0x%lx, irq %d\n",
1248 (unsigned long)base->start, irq);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001249 if (version_printed++ == 0)
Florian Fainelli215e17b2010-09-08 11:11:45 +00001250 pr_info("%s version %s %s\n",
1251 DRV_NAME, DRV_VERSION, DRV_AUTHOR);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001252
1253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255err_out:
Florian Fainellibd2302c2009-11-10 01:13:38 +01001256 if (aup->mii_bus != NULL)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001257 mdiobus_unregister(aup->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /* here we should have a valid dev plus aup-> register addresses
Florian Fainellidc998392010-09-08 11:11:59 +00001260 * so we can reset the mac properly.
1261 */
Florian Fainellieb049632010-04-06 22:09:01 +00001262 au1000_reset_mac(dev);
Herbert Valerio Riedel0638dec2006-06-01 09:41:04 +02001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 for (i = 0; i < NUM_RX_DMA; i++) {
1265 if (aup->rx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001266 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 for (i = 0; i < NUM_TX_DMA; i++) {
1269 if (aup->tx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001270 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Florian Fainellibd2302c2009-11-10 01:13:38 +01001272err_mdiobus_reg:
1273 mdiobus_free(aup->mii_bus);
1274err_mdiobus_alloc:
Linus Torvaldsd6748062011-11-03 13:28:14 -07001275 iounmap(aup->macdma);
1276err_remap3:
Florian Fainellibd2302c2009-11-10 01:13:38 +01001277 iounmap(aup->enable);
1278err_remap2:
1279 iounmap(aup->mac);
1280err_remap1:
Sergei Shtylyov89be0502006-04-19 22:46:21 +04001281 dma_free_noncoherent(NULL, MAX_BUF_SIZE * (NUM_TX_BUFFS + NUM_RX_BUFFS),
1282 (void *)aup->vaddr, aup->dma_addr);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001283err_vaddr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 free_netdev(dev);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001285err_alloc:
Linus Torvaldsd6748062011-11-03 13:28:14 -07001286 release_mem_region(macdma->start, resource_size(macdma));
1287err_macdma:
Florian Fainellibd2302c2009-11-10 01:13:38 +01001288 release_mem_region(macen->start, resource_size(macen));
1289err_request:
1290 release_mem_region(base->start, resource_size(base));
1291out:
1292 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Bill Pemberton0cb05682012-12-03 09:23:54 -05001295static int au1000_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Florian Fainellibd2302c2009-11-10 01:13:38 +01001297 struct net_device *dev = platform_get_drvdata(pdev);
1298 struct au1000_private *aup = netdev_priv(dev);
1299 int i;
1300 struct resource *base, *macen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Florian Fainellibd2302c2009-11-10 01:13:38 +01001302 unregister_netdev(dev);
1303 mdiobus_unregister(aup->mii_bus);
1304 mdiobus_free(aup->mii_bus);
1305
1306 for (i = 0; i < NUM_RX_DMA; i++)
1307 if (aup->rx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001308 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001309
1310 for (i = 0; i < NUM_TX_DMA; i++)
1311 if (aup->tx_db_inuse[i])
Florian Fainellieb049632010-04-06 22:09:01 +00001312 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001313
1314 dma_free_noncoherent(NULL, MAX_BUF_SIZE *
1315 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1316 (void *)aup->vaddr, aup->dma_addr);
1317
Linus Torvaldsd6748062011-11-03 13:28:14 -07001318 iounmap(aup->macdma);
Florian Fainellibd2302c2009-11-10 01:13:38 +01001319 iounmap(aup->mac);
1320 iounmap(aup->enable);
1321
Linus Torvaldsd6748062011-11-03 13:28:14 -07001322 base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1323 release_mem_region(base->start, resource_size(base));
1324
Florian Fainellibd2302c2009-11-10 01:13:38 +01001325 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1326 release_mem_region(base->start, resource_size(base));
1327
1328 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1329 release_mem_region(macen->start, resource_size(macen));
1330
1331 free_netdev(dev);
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return 0;
1334}
1335
Florian Fainellibd2302c2009-11-10 01:13:38 +01001336static struct platform_driver au1000_eth_driver = {
1337 .probe = au1000_probe,
Bill Pemberton0cb05682012-12-03 09:23:54 -05001338 .remove = au1000_remove,
Florian Fainellibd2302c2009-11-10 01:13:38 +01001339 .driver = {
1340 .name = "au1000-eth",
1341 .owner = THIS_MODULE,
1342 },
1343};
Axel Lindb62f682011-11-27 16:44:17 +00001344
1345module_platform_driver(au1000_eth_driver);
1346
Florian Fainellibd2302c2009-11-10 01:13:38 +01001347MODULE_ALIAS("platform:au1000-eth");