cpmac.c 33.9 KB
Newer Older
M
Matteo Croce 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2006, 2007 Eugene Konev
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <linux/module.h>
20
#include <linux/interrupt.h>
M
Matteo Croce 已提交
21 22 23 24 25 26 27 28 29 30
#include <linux/moduleparam.h>

#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/delay.h>

#include <linux/netdevice.h>
31
#include <linux/if_vlan.h>
M
Matteo Croce 已提交
32 33 34 35 36
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/skbuff.h>
#include <linux/mii.h>
#include <linux/phy.h>
37
#include <linux/phy_fixed.h>
M
Matteo Croce 已提交
38 39
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
40
#include <linux/clk.h>
41
#include <linux/gpio.h>
A
Arun Sharma 已提交
42
#include <linux/atomic.h>
M
Matteo Croce 已提交
43

44 45
#include <asm/mach-ar7/ar7.h>

M
Matteo Croce 已提交
46 47 48
MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
MODULE_LICENSE("GPL");
49
MODULE_ALIAS("platform:cpmac");
M
Matteo Croce 已提交
50 51 52 53 54 55 56 57 58 59 60

static int debug_level = 8;
static int dumb_switch;

/* Next 2 are only used in cpmac_probe, so it's pointless to change them */
module_param(debug_level, int, 0444);
module_param(dumb_switch, int, 0444);

MODULE_PARM_DESC(debug_level, "Number of NETIF_MSG bits to enable");
MODULE_PARM_DESC(dumb_switch, "Assume switch is not connected to MDIO bus");

F
Florian Fainelli 已提交
61
#define CPMAC_VERSION "0.5.2"
62 63
/* frame size + 802.1q tag + FCS size */
#define CPMAC_SKB_SIZE		(ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)
M
Matteo Croce 已提交
64 65 66 67 68 69 70 71
#define CPMAC_QUEUES	8

/* Ethernet registers */
#define CPMAC_TX_CONTROL		0x0004
#define CPMAC_TX_TEARDOWN		0x0008
#define CPMAC_RX_CONTROL		0x0014
#define CPMAC_RX_TEARDOWN		0x0018
#define CPMAC_MBP			0x0100
72 73 74 75 76 77 78 79 80 81 82 83
#define MBP_RXPASSCRC			0x40000000
#define MBP_RXQOS			0x20000000
#define MBP_RXNOCHAIN			0x10000000
#define MBP_RXCMF			0x01000000
#define MBP_RXSHORT			0x00800000
#define MBP_RXCEF			0x00400000
#define MBP_RXPROMISC			0x00200000
#define MBP_PROMISCCHAN(channel)	(((channel) & 0x7) << 16)
#define MBP_RXBCAST			0x00002000
#define MBP_BCASTCHAN(channel)		(((channel) & 0x7) << 8)
#define MBP_RXMCAST			0x00000020
#define MBP_MCASTCHAN(channel)		((channel) & 0x7)
M
Matteo Croce 已提交
84 85 86 87 88
#define CPMAC_UNICAST_ENABLE		0x0104
#define CPMAC_UNICAST_CLEAR		0x0108
#define CPMAC_MAX_LENGTH		0x010c
#define CPMAC_BUFFER_OFFSET		0x0110
#define CPMAC_MAC_CONTROL		0x0160
89 90 91 92 93 94 95 96
#define MAC_TXPTYPE			0x00000200
#define MAC_TXPACE			0x00000040
#define MAC_MII				0x00000020
#define MAC_TXFLOW			0x00000010
#define MAC_RXFLOW			0x00000008
#define MAC_MTEST			0x00000004
#define MAC_LOOPBACK			0x00000002
#define MAC_FDX				0x00000001
M
Matteo Croce 已提交
97
#define CPMAC_MAC_STATUS		0x0164
98 99 100
#define MAC_STATUS_QOS			0x00000004
#define MAC_STATUS_RXFLOW		0x00000002
#define MAC_STATUS_TXFLOW		0x00000001
M
Matteo Croce 已提交
101 102 103
#define CPMAC_TX_INT_ENABLE		0x0178
#define CPMAC_TX_INT_CLEAR		0x017c
#define CPMAC_MAC_INT_VECTOR		0x0180
104 105 106 107
#define MAC_INT_STATUS			0x00080000
#define MAC_INT_HOST			0x00040000
#define MAC_INT_RX			0x00020000
#define MAC_INT_TX			0x00010000
M
Matteo Croce 已提交
108 109 110 111 112
#define CPMAC_MAC_EOI_VECTOR		0x0184
#define CPMAC_RX_INT_ENABLE		0x0198
#define CPMAC_RX_INT_CLEAR		0x019c
#define CPMAC_MAC_INT_ENABLE		0x01a8
#define CPMAC_MAC_INT_CLEAR		0x01ac
113
#define CPMAC_MAC_ADDR_LO(channel)	(0x01b0 + (channel) * 4)
M
Matteo Croce 已提交
114 115 116 117 118 119 120 121 122
#define CPMAC_MAC_ADDR_MID		0x01d0
#define CPMAC_MAC_ADDR_HI		0x01d4
#define CPMAC_MAC_HASH_LO		0x01d8
#define CPMAC_MAC_HASH_HI		0x01dc
#define CPMAC_TX_PTR(channel)		(0x0600 + (channel) * 4)
#define CPMAC_RX_PTR(channel)		(0x0620 + (channel) * 4)
#define CPMAC_TX_ACK(channel)		(0x0640 + (channel) * 4)
#define CPMAC_RX_ACK(channel)		(0x0660 + (channel) * 4)
#define CPMAC_REG_END			0x0680
V
Varka Bhadram 已提交
123 124

/* Rx/Tx statistics
M
Matteo Croce 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
 * TODO: use some of them to fill stats in cpmac_stats()
 */
#define CPMAC_STATS_RX_GOOD		0x0200
#define CPMAC_STATS_RX_BCAST		0x0204
#define CPMAC_STATS_RX_MCAST		0x0208
#define CPMAC_STATS_RX_PAUSE		0x020c
#define CPMAC_STATS_RX_CRC		0x0210
#define CPMAC_STATS_RX_ALIGN		0x0214
#define CPMAC_STATS_RX_OVER		0x0218
#define CPMAC_STATS_RX_JABBER		0x021c
#define CPMAC_STATS_RX_UNDER		0x0220
#define CPMAC_STATS_RX_FRAG		0x0224
#define CPMAC_STATS_RX_FILTER		0x0228
#define CPMAC_STATS_RX_QOSFILTER	0x022c
#define CPMAC_STATS_RX_OCTETS		0x0230

#define CPMAC_STATS_TX_GOOD		0x0234
#define CPMAC_STATS_TX_BCAST		0x0238
#define CPMAC_STATS_TX_MCAST		0x023c
#define CPMAC_STATS_TX_PAUSE		0x0240
#define CPMAC_STATS_TX_DEFER		0x0244
#define CPMAC_STATS_TX_COLLISION	0x0248
#define CPMAC_STATS_TX_SINGLECOLL	0x024c
#define CPMAC_STATS_TX_MULTICOLL	0x0250
#define CPMAC_STATS_TX_EXCESSCOLL	0x0254
#define CPMAC_STATS_TX_LATECOLL		0x0258
#define CPMAC_STATS_TX_UNDERRUN		0x025c
#define CPMAC_STATS_TX_CARRIERSENSE	0x0260
#define CPMAC_STATS_TX_OCTETS		0x0264

#define cpmac_read(base, reg)		(readl((void __iomem *)(base) + (reg)))
#define cpmac_write(base, reg, val)	(writel(val, (void __iomem *)(base) + \
						(reg)))

/* MDIO bus */
#define CPMAC_MDIO_VERSION		0x0000
#define CPMAC_MDIO_CONTROL		0x0004
162 163 164 165 166 167 168
#define MDIOC_IDLE			0x80000000
#define MDIOC_ENABLE			0x40000000
#define MDIOC_PREAMBLE			0x00100000
#define MDIOC_FAULT			0x00080000
#define MDIOC_FAULTDETECT		0x00040000
#define MDIOC_INTTEST			0x00020000
#define MDIOC_CLKDIV(div)		((div) & 0xff)
M
Matteo Croce 已提交
169 170 171
#define CPMAC_MDIO_ALIVE		0x0008
#define CPMAC_MDIO_LINK			0x000c
#define CPMAC_MDIO_ACCESS(channel)	(0x0080 + (channel) * 8)
172 173 174 175 176
#define MDIO_BUSY			0x80000000
#define MDIO_WRITE			0x40000000
#define MDIO_REG(reg)			(((reg) & 0x1f) << 21)
#define MDIO_PHY(phy)			(((phy) & 0x1f) << 16)
#define MDIO_DATA(data)			((data) & 0xffff)
M
Matteo Croce 已提交
177
#define CPMAC_MDIO_PHYSEL(channel)	(0x0084 + (channel) * 8)
178 179
#define PHYSEL_LINKSEL			0x00000040
#define PHYSEL_LINKINT			0x00000020
M
Matteo Croce 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193

struct cpmac_desc {
	u32 hw_next;
	u32 hw_data;
	u16 buflen;
	u16 bufflags;
	u16 datalen;
	u16 dataflags;
#define CPMAC_SOP			0x8000
#define CPMAC_EOP			0x4000
#define CPMAC_OWN			0x2000
#define CPMAC_EOQ			0x1000
	struct sk_buff *skb;
	struct cpmac_desc *next;
M
Matteo Croce 已提交
194
	struct cpmac_desc *prev;
M
Matteo Croce 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208
	dma_addr_t mapping;
	dma_addr_t data_mapping;
};

struct cpmac_priv {
	spinlock_t lock;
	spinlock_t rx_lock;
	struct cpmac_desc *rx_head;
	int ring_size;
	struct cpmac_desc *desc_ring;
	dma_addr_t dma_ring;
	void __iomem *regs;
	struct mii_bus *mii_bus;
	struct phy_device *phy;
209
	char phy_name[MII_BUS_ID_SIZE + 3];
M
Matteo Croce 已提交
210 211 212 213 214
	int oldlink, oldspeed, oldduplex;
	u32 msg_enable;
	struct net_device *dev;
	struct work_struct reset_work;
	struct platform_device *pdev;
215
	struct napi_struct napi;
M
Matteo Croce 已提交
216
	atomic_t reset_pending;
M
Matteo Croce 已提交
217 218 219 220 221 222 223 224 225 226 227 228
};

static irqreturn_t cpmac_irq(int, void *);
static void cpmac_hw_start(struct net_device *dev);
static void cpmac_hw_stop(struct net_device *dev);
static int cpmac_stop(struct net_device *dev);
static int cpmac_open(struct net_device *dev);

static void cpmac_dump_regs(struct net_device *dev)
{
	int i;
	struct cpmac_priv *priv = netdev_priv(dev);
229

M
Matteo Croce 已提交
230 231 232
	for (i = 0; i < CPMAC_REG_END; i += 4) {
		if (i % 16 == 0) {
			if (i)
233 234
				printk("\n");
			printk("%s: reg[%p]:", dev->name, priv->regs + i);
M
Matteo Croce 已提交
235
		}
236
		printk(" %08x", cpmac_read(priv->regs, i));
M
Matteo Croce 已提交
237
	}
238
	printk("\n");
M
Matteo Croce 已提交
239 240 241 242 243
}

static void cpmac_dump_desc(struct net_device *dev, struct cpmac_desc *desc)
{
	int i;
244

245
	printk("%s: desc[%p]:", dev->name, desc);
M
Matteo Croce 已提交
246
	for (i = 0; i < sizeof(*desc) / 4; i++)
247 248
		printk(" %08x", ((u32 *)desc)[i]);
	printk("\n");
M
Matteo Croce 已提交
249 250
}

M
Matteo Croce 已提交
251 252 253 254
static void cpmac_dump_all_desc(struct net_device *dev)
{
	struct cpmac_priv *priv = netdev_priv(dev);
	struct cpmac_desc *dump = priv->rx_head;
255

M
Matteo Croce 已提交
256 257 258 259 260 261
	do {
		cpmac_dump_desc(dev, dump);
		dump = dump->next;
	} while (dump != priv->rx_head);
}

M
Matteo Croce 已提交
262 263 264
static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
{
	int i;
265

266
	printk("%s: skb 0x%p, len=%d\n", dev->name, skb, skb->len);
M
Matteo Croce 已提交
267 268 269
	for (i = 0; i < skb->len; i++) {
		if (i % 16 == 0) {
			if (i)
270 271
				printk("\n");
			printk("%s: data[%p]:", dev->name, skb->data + i);
M
Matteo Croce 已提交
272
		}
273
		printk(" %02x", ((u8 *)skb->data)[i]);
M
Matteo Croce 已提交
274
	}
275
	printk("\n");
M
Matteo Croce 已提交
276 277 278 279 280 281 282 283 284 285 286 287
}

static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
{
	u32 val;

	while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
		cpu_relax();
	cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
		    MDIO_PHY(phy_id));
	while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
		cpu_relax();
288

M
Matteo Croce 已提交
289 290 291 292 293 294 295 296 297 298
	return MDIO_DATA(val);
}

static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
			    int reg, u16 val)
{
	while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
		cpu_relax();
	cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
		    MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
299

M
Matteo Croce 已提交
300 301 302 303 304
	return 0;
}

static int cpmac_mdio_reset(struct mii_bus *bus)
{
305 306 307 308
	struct clk *cpmac_clk;

	cpmac_clk = clk_get(&bus->dev, "cpmac");
	if (IS_ERR(cpmac_clk)) {
V
Varka Bhadram 已提交
309
		pr_err("unable to get cpmac clock\n");
310 311
		return -1;
	}
M
Matteo Croce 已提交
312 313
	ar7_device_reset(AR7_RESET_BIT_MDIO);
	cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
314
		    MDIOC_CLKDIV(clk_get_rate(cpmac_clk) / 2200000 - 1));
315

M
Matteo Croce 已提交
316 317 318 319 320
	return 0;
}

static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };

321
static struct mii_bus *cpmac_mii;
M
Matteo Croce 已提交
322 323 324

static void cpmac_set_multicast_list(struct net_device *dev)
{
325
	struct netdev_hw_addr *ha;
M
Matteo Croce 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	u8 tmp;
	u32 mbp, bit, hash[2] = { 0, };
	struct cpmac_priv *priv = netdev_priv(dev);

	mbp = cpmac_read(priv->regs, CPMAC_MBP);
	if (dev->flags & IFF_PROMISC) {
		cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
			    MBP_RXPROMISC);
	} else {
		cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
		if (dev->flags & IFF_ALLMULTI) {
			/* enable all multicast mode */
			cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
			cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
		} else {
V
Varka Bhadram 已提交
341
			/* cpmac uses some strange mac address hashing
M
Matteo Croce 已提交
342 343
			 * (not crc32)
			 */
344
			netdev_for_each_mc_addr(ha, dev) {
M
Matteo Croce 已提交
345
				bit = 0;
346
				tmp = ha->addr[0];
M
Matteo Croce 已提交
347
				bit  ^= (tmp >> 2) ^ (tmp << 4);
348
				tmp = ha->addr[1];
M
Matteo Croce 已提交
349
				bit  ^= (tmp >> 4) ^ (tmp << 2);
350
				tmp = ha->addr[2];
M
Matteo Croce 已提交
351
				bit  ^= (tmp >> 6) ^ tmp;
352
				tmp = ha->addr[3];
M
Matteo Croce 已提交
353
				bit  ^= (tmp >> 2) ^ (tmp << 4);
354
				tmp = ha->addr[4];
M
Matteo Croce 已提交
355
				bit  ^= (tmp >> 4) ^ (tmp << 2);
356
				tmp = ha->addr[5];
M
Matteo Croce 已提交
357 358 359 360 361 362 363 364 365 366 367
				bit  ^= (tmp >> 6) ^ tmp;
				bit &= 0x3f;
				hash[bit / 32] |= 1 << (bit % 32);
			}

			cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
			cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
		}
	}
}

368
static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
M
Matteo Croce 已提交
369 370 371 372 373
				    struct cpmac_desc *desc)
{
	struct sk_buff *skb, *result = NULL;

	if (unlikely(netif_msg_hw(priv)))
374
		cpmac_dump_desc(priv->dev, desc);
M
Matteo Croce 已提交
375 376 377
	cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
	if (unlikely(!desc->datalen)) {
		if (netif_msg_rx_err(priv) && net_ratelimit())
V
Varka Bhadram 已提交
378 379
			netdev_warn(priv->dev, "rx: spurious interrupt\n");

M
Matteo Croce 已提交
380 381 382
		return NULL;
	}

383
	skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
M
Matteo Croce 已提交
384 385
	if (likely(skb)) {
		skb_put(desc->skb, desc->datalen);
386
		desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
387
		skb_checksum_none_assert(desc->skb);
388 389
		priv->dev->stats.rx_packets++;
		priv->dev->stats.rx_bytes += desc->datalen;
M
Matteo Croce 已提交
390
		result = desc->skb;
391 392
		dma_unmap_single(&priv->dev->dev, desc->data_mapping,
				 CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
M
Matteo Croce 已提交
393
		desc->skb = skb;
394
		desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
M
Matteo Croce 已提交
395 396 397 398
						    CPMAC_SKB_SIZE,
						    DMA_FROM_DEVICE);
		desc->hw_data = (u32)desc->data_mapping;
		if (unlikely(netif_msg_pktdata(priv))) {
V
Varka Bhadram 已提交
399
			netdev_dbg(priv->dev, "received packet:\n");
400
			cpmac_dump_skb(priv->dev, result);
M
Matteo Croce 已提交
401 402 403
		}
	} else {
		if (netif_msg_rx_err(priv) && net_ratelimit())
V
Varka Bhadram 已提交
404 405 406
			netdev_warn(priv->dev,
				    "low on skbs, dropping packet\n");

407
		priv->dev->stats.rx_dropped++;
M
Matteo Croce 已提交
408 409 410 411 412 413 414 415
	}

	desc->buflen = CPMAC_SKB_SIZE;
	desc->dataflags = CPMAC_OWN;

	return result;
}

416
static int cpmac_poll(struct napi_struct *napi, int budget)
M
Matteo Croce 已提交
417 418
{
	struct sk_buff *skb;
M
Matteo Croce 已提交
419
	struct cpmac_desc *desc, *restart;
420
	struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
M
Matteo Croce 已提交
421
	int received = 0, processed = 0;
M
Matteo Croce 已提交
422 423 424 425

	spin_lock(&priv->rx_lock);
	if (unlikely(!priv->rx_head)) {
		if (netif_msg_rx_err(priv) && net_ratelimit())
V
Varka Bhadram 已提交
426 427
			netdev_warn(priv->dev, "rx: polling, but no queue\n");

M
Matteo Croce 已提交
428
		spin_unlock(&priv->rx_lock);
429
		napi_complete(napi);
M
Matteo Croce 已提交
430 431 432 433
		return 0;
	}

	desc = priv->rx_head;
M
Matteo Croce 已提交
434
	restart = NULL;
435
	while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
M
Matteo Croce 已提交
436 437 438 439
		processed++;

		if ((desc->dataflags & CPMAC_EOQ) != 0) {
			/* The last update to eoq->hw_next didn't happen
V
Varka Bhadram 已提交
440 441 442 443
			 * soon enough, and the receiver stopped here.
			 * Remember this descriptor so we can restart
			 * the receiver after freeing some space.
			 */
M
Matteo Croce 已提交
444 445
			if (unlikely(restart)) {
				if (netif_msg_rx_err(priv))
V
Varka Bhadram 已提交
446 447 448
					netdev_err(priv->dev, "poll found a"
						   " duplicate EOQ: %p and %p\n",
						   restart, desc);
M
Matteo Croce 已提交
449 450 451 452 453 454
				goto fatal_error;
			}

			restart = desc->next;
		}

455
		skb = cpmac_rx_one(priv, desc);
M
Matteo Croce 已提交
456 457 458 459 460 461 462
		if (likely(skb)) {
			netif_receive_skb(skb);
			received++;
		}
		desc = desc->next;
	}

M
Matteo Croce 已提交
463 464
	if (desc != priv->rx_head) {
		/* We freed some buffers, but not the whole ring,
V
Varka Bhadram 已提交
465 466
		 * add what we did free to the rx list
		 */
M
Matteo Croce 已提交
467 468 469 470 471 472
		desc->prev->hw_next = (u32)0;
		priv->rx_head->prev->hw_next = priv->rx_head->mapping;
	}

	/* Optimization: If we did not actually process an EOQ (perhaps because
	 * of quota limits), check to see if the tail of the queue has EOQ set.
V
Varka Bhadram 已提交
473 474 475 476 477
	 * We should immediately restart in that case so that the receiver can
	 * restart and run in parallel with more packet processing.
	 * This lets us handle slightly larger bursts before running
	 * out of ring space (assuming dev->weight < ring_size)
	 */
M
Matteo Croce 已提交
478 479 480 481 482 483

	if (!restart &&
	     (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
		    == CPMAC_EOQ &&
	     (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
		/* reset EOQ so the poll loop (above) doesn't try to
V
Varka Bhadram 已提交
484 485
		 * restart this when it eventually gets to this descriptor.
		 */
M
Matteo Croce 已提交
486 487 488 489 490 491 492 493
		priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
		restart = priv->rx_head;
	}

	if (restart) {
		priv->dev->stats.rx_errors++;
		priv->dev->stats.rx_fifo_errors++;
		if (netif_msg_rx_err(priv) && net_ratelimit())
V
Varka Bhadram 已提交
494
			netdev_warn(priv->dev, "rx dma ring overrun\n");
M
Matteo Croce 已提交
495 496 497

		if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
			if (netif_msg_drv(priv))
V
Varka Bhadram 已提交
498 499 500
				netdev_err(priv->dev, "cpmac_poll is trying "
					"to restart rx from a descriptor "
					"that's not free: %p\n", restart);
501
			goto fatal_error;
M
Matteo Croce 已提交
502 503 504 505 506
		}

		cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
	}

M
Matteo Croce 已提交
507 508 509
	priv->rx_head = desc;
	spin_unlock(&priv->rx_lock);
	if (unlikely(netif_msg_rx_status(priv)))
V
Varka Bhadram 已提交
510 511
		netdev_dbg(priv->dev, "poll processed %d packets\n", received);

M
Matteo Croce 已提交
512 513
	if (processed == 0) {
		/* we ran out of packets to read,
V
Varka Bhadram 已提交
514 515
		 * revert to interrupt-driven mode
		 */
516
		napi_complete(napi);
M
Matteo Croce 已提交
517 518 519 520 521
		cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
		return 0;
	}

	return 1;
M
Matteo Croce 已提交
522 523 524

fatal_error:
	/* Something went horribly wrong.
V
Varka Bhadram 已提交
525 526
	 * Reset hardware to try to recover rather than wedging.
	 */
M
Matteo Croce 已提交
527
	if (netif_msg_drv(priv)) {
V
Varka Bhadram 已提交
528 529
		netdev_err(priv->dev, "cpmac_poll is confused. "
			   "Resetting hardware\n");
M
Matteo Croce 已提交
530
		cpmac_dump_all_desc(priv->dev);
V
Varka Bhadram 已提交
531 532 533
		netdev_dbg(priv->dev, "RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
			   cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
			   cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
M
Matteo Croce 已提交
534 535 536
	}

	spin_unlock(&priv->rx_lock);
537
	napi_complete(napi);
538
	netif_tx_stop_all_queues(priv->dev);
M
Matteo Croce 已提交
539 540 541 542 543 544
	napi_disable(&priv->napi);

	atomic_inc(&priv->reset_pending);
	cpmac_hw_stop(priv->dev);
	if (!schedule_work(&priv->reset_work))
		atomic_dec(&priv->reset_pending);
545

M
Matteo Croce 已提交
546 547
	return 0;

M
Matteo Croce 已提交
548 549 550 551 552 553 554 555
}

static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	int queue, len;
	struct cpmac_desc *desc;
	struct cpmac_priv *priv = netdev_priv(dev);

M
Matteo Croce 已提交
556 557 558
	if (unlikely(atomic_read(&priv->reset_pending)))
		return NETDEV_TX_BUSY;

559 560
	if (unlikely(skb_padto(skb, ETH_ZLEN)))
		return NETDEV_TX_OK;
M
Matteo Croce 已提交
561 562

	len = max(skb->len, ETH_ZLEN);
M
Matteo Croce 已提交
563
	queue = skb_get_queue_mapping(skb);
M
Matteo Croce 已提交
564 565 566 567 568
	netif_stop_subqueue(dev, queue);

	desc = &priv->desc_ring[queue];
	if (unlikely(desc->dataflags & CPMAC_OWN)) {
		if (netif_msg_tx_err(priv) && net_ratelimit())
V
Varka Bhadram 已提交
569 570
			netdev_warn(dev, "tx dma ring full\n");

571
		return NETDEV_TX_BUSY;
M
Matteo Croce 已提交
572 573 574 575 576 577 578 579 580 581 582 583
	}

	spin_lock(&priv->lock);
	spin_unlock(&priv->lock);
	desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
	desc->skb = skb;
	desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
					    DMA_TO_DEVICE);
	desc->hw_data = (u32)desc->data_mapping;
	desc->datalen = len;
	desc->buflen = len;
	if (unlikely(netif_msg_tx_queued(priv)))
V
Varka Bhadram 已提交
584
		netdev_dbg(dev, "sending 0x%p, len=%d\n", skb, skb->len);
M
Matteo Croce 已提交
585 586 587 588 589 590
	if (unlikely(netif_msg_hw(priv)))
		cpmac_dump_desc(dev, desc);
	if (unlikely(netif_msg_pktdata(priv)))
		cpmac_dump_skb(dev, skb);
	cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);

591
	return NETDEV_TX_OK;
M
Matteo Croce 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
}

static void cpmac_end_xmit(struct net_device *dev, int queue)
{
	struct cpmac_desc *desc;
	struct cpmac_priv *priv = netdev_priv(dev);

	desc = &priv->desc_ring[queue];
	cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
	if (likely(desc->skb)) {
		spin_lock(&priv->lock);
		dev->stats.tx_packets++;
		dev->stats.tx_bytes += desc->skb->len;
		spin_unlock(&priv->lock);
		dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
				 DMA_TO_DEVICE);

		if (unlikely(netif_msg_tx_done(priv)))
V
Varka Bhadram 已提交
610 611
			netdev_dbg(dev, "sent 0x%p, len=%d\n",
				   desc->skb, desc->skb->len);
M
Matteo Croce 已提交
612 613 614

		dev_kfree_skb_irq(desc->skb);
		desc->skb = NULL;
615
		if (__netif_subqueue_stopped(dev, queue))
M
Matteo Croce 已提交
616 617 618
			netif_wake_subqueue(dev, queue);
	} else {
		if (netif_msg_tx_err(priv) && net_ratelimit())
V
Varka Bhadram 已提交
619
			netdev_warn(dev, "end_xmit: spurious interrupt\n");
620
		if (__netif_subqueue_stopped(dev, queue))
M
Matteo Croce 已提交
621 622 623 624 625 626 627 628
			netif_wake_subqueue(dev, queue);
	}
}

static void cpmac_hw_stop(struct net_device *dev)
{
	int i;
	struct cpmac_priv *priv = netdev_priv(dev);
J
Jingoo Han 已提交
629
	struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
M
Matteo Croce 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651

	ar7_device_reset(pdata->reset_bit);
	cpmac_write(priv->regs, CPMAC_RX_CONTROL,
		    cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
	cpmac_write(priv->regs, CPMAC_TX_CONTROL,
		    cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
	for (i = 0; i < 8; i++) {
		cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
		cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
	}
	cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
	cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
	cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
	cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
	cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
		    cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
}

static void cpmac_hw_start(struct net_device *dev)
{
	int i;
	struct cpmac_priv *priv = netdev_priv(dev);
J
Jingoo Han 已提交
652
	struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
M
Matteo Croce 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

	ar7_device_reset(pdata->reset_bit);
	for (i = 0; i < 8; i++) {
		cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
		cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
	}
	cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);

	cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
		    MBP_RXMCAST);
	cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
	for (i = 0; i < 8; i++)
		cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
	cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
	cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
		    (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
		    (dev->dev_addr[3] << 24));
	cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
	cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
	cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
	cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
	cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
	cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
	cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
	cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
	cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);

	cpmac_write(priv->regs, CPMAC_RX_CONTROL,
		    cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
	cpmac_write(priv->regs, CPMAC_TX_CONTROL,
		    cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
	cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
		    cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
		    MAC_FDX);
}

static void cpmac_clear_rx(struct net_device *dev)
{
	struct cpmac_priv *priv = netdev_priv(dev);
	struct cpmac_desc *desc;
	int i;
694

M
Matteo Croce 已提交
695 696 697 698 699 700
	if (unlikely(!priv->rx_head))
		return;
	desc = priv->rx_head;
	for (i = 0; i < priv->ring_size; i++) {
		if ((desc->dataflags & CPMAC_OWN) == 0) {
			if (netif_msg_rx_err(priv) && net_ratelimit())
V
Varka Bhadram 已提交
701
				netdev_warn(dev, "packet dropped\n");
M
Matteo Croce 已提交
702 703 704 705 706
			if (unlikely(netif_msg_hw(priv)))
				cpmac_dump_desc(dev, desc);
			desc->dataflags = CPMAC_OWN;
			dev->stats.rx_dropped++;
		}
M
Matteo Croce 已提交
707
		desc->hw_next = desc->next->mapping;
M
Matteo Croce 已提交
708 709
		desc = desc->next;
	}
M
Matteo Croce 已提交
710
	priv->rx_head->prev->hw_next = 0;
M
Matteo Croce 已提交
711 712 713 714 715 716
}

static void cpmac_clear_tx(struct net_device *dev)
{
	struct cpmac_priv *priv = netdev_priv(dev);
	int i;
717

M
Matteo Croce 已提交
718 719
	if (unlikely(!priv->desc_ring))
		return;
720 721
	for (i = 0; i < CPMAC_QUEUES; i++) {
		priv->desc_ring[i].dataflags = 0;
M
Matteo Croce 已提交
722 723
		if (priv->desc_ring[i].skb) {
			dev_kfree_skb_any(priv->desc_ring[i].skb);
M
Matteo Croce 已提交
724
			priv->desc_ring[i].skb = NULL;
M
Matteo Croce 已提交
725
		}
726
	}
M
Matteo Croce 已提交
727 728 729 730 731 732 733 734 735 736 737 738
}

static void cpmac_hw_error(struct work_struct *work)
{
	struct cpmac_priv *priv =
		container_of(work, struct cpmac_priv, reset_work);

	spin_lock(&priv->rx_lock);
	cpmac_clear_rx(priv->dev);
	spin_unlock(&priv->rx_lock);
	cpmac_clear_tx(priv->dev);
	cpmac_hw_start(priv->dev);
M
Matteo Croce 已提交
739 740 741
	barrier();
	atomic_dec(&priv->reset_pending);

742
	netif_tx_wake_all_queues(priv->dev);
M
Matteo Croce 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
}

static void cpmac_check_status(struct net_device *dev)
{
	struct cpmac_priv *priv = netdev_priv(dev);

	u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
	int rx_channel = (macstatus >> 8) & 7;
	int rx_code = (macstatus >> 12) & 15;
	int tx_channel = (macstatus >> 16) & 7;
	int tx_code = (macstatus >> 20) & 15;

	if (rx_code || tx_code) {
		if (netif_msg_drv(priv) && net_ratelimit()) {
			/* Can't find any documentation on what these
V
Varka Bhadram 已提交
759
			 * error codes actually are. So just log them and hope..
M
Matteo Croce 已提交
760 761
			 */
			if (rx_code)
V
Varka Bhadram 已提交
762 763 764
				netdev_warn(dev, "host error %d on rx "
					"channel %d (macstatus %08x), resetting\n",
					rx_code, rx_channel, macstatus);
M
Matteo Croce 已提交
765
			if (tx_code)
V
Varka Bhadram 已提交
766 767 768
				netdev_warn(dev, "host error %d on tx "
					"channel %d (macstatus %08x), resetting\n",
					tx_code, tx_channel, macstatus);
M
Matteo Croce 已提交
769 770
		}

771
		netif_tx_stop_all_queues(dev);
M
Matteo Croce 已提交
772 773 774 775 776 777 778
		cpmac_hw_stop(dev);
		if (schedule_work(&priv->reset_work))
			atomic_inc(&priv->reset_pending);
		if (unlikely(netif_msg_hw(priv)))
			cpmac_dump_regs(dev);
	}
	cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
M
Matteo Croce 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792
}

static irqreturn_t cpmac_irq(int irq, void *dev_id)
{
	struct net_device *dev = dev_id;
	struct cpmac_priv *priv;
	int queue;
	u32 status;

	priv = netdev_priv(dev);

	status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);

	if (unlikely(netif_msg_intr(priv)))
V
Varka Bhadram 已提交
793
		netdev_dbg(dev, "interrupt status: 0x%08x\n", status);
M
Matteo Croce 已提交
794 795 796 797 798 799

	if (status & MAC_INT_TX)
		cpmac_end_xmit(dev, (status & 7));

	if (status & MAC_INT_RX) {
		queue = (status >> 8) & 7;
800
		if (napi_schedule_prep(&priv->napi)) {
801
			cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
802
			__napi_schedule(&priv->napi);
803
		}
M
Matteo Croce 已提交
804 805 806 807
	}

	cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);

M
Matteo Croce 已提交
808 809
	if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
		cpmac_check_status(dev);
M
Matteo Croce 已提交
810 811 812 813 814 815

	return IRQ_HANDLED;
}

static void cpmac_tx_timeout(struct net_device *dev)
{
M
Matteo Croce 已提交
816
	struct cpmac_priv *priv = netdev_priv(dev);
M
Matteo Croce 已提交
817 818 819 820 821

	spin_lock(&priv->lock);
	dev->stats.tx_errors++;
	spin_unlock(&priv->lock);
	if (netif_msg_tx_err(priv) && net_ratelimit())
V
Varka Bhadram 已提交
822
		netdev_warn(dev, "transmit timeout\n");
M
Matteo Croce 已提交
823 824 825 826 827 828 829

	atomic_inc(&priv->reset_pending);
	barrier();
	cpmac_clear_tx(dev);
	barrier();
	atomic_dec(&priv->reset_pending);

830
	netif_tx_wake_all_queues(priv->dev);
M
Matteo Croce 已提交
831 832 833 834 835
}

static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
	struct cpmac_priv *priv = netdev_priv(dev);
836

M
Matteo Croce 已提交
837 838 839 840 841
	if (!(netif_running(dev)))
		return -EINVAL;
	if (!priv->phy)
		return -EINVAL;

842
	return phy_mii_ioctl(priv->phy, ifr, cmd);
M
Matteo Croce 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
}

static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct cpmac_priv *priv = netdev_priv(dev);

	if (priv->phy)
		return phy_ethtool_gset(priv->phy, cmd);

	return -EINVAL;
}

static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct cpmac_priv *priv = netdev_priv(dev);

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

	if (priv->phy)
		return phy_ethtool_sset(priv->phy, cmd);

	return -EINVAL;
}

868 869
static void cpmac_get_ringparam(struct net_device *dev,
						struct ethtool_ringparam *ring)
M
Matteo Croce 已提交
870 871 872 873 874 875 876 877 878 879 880 881 882 883
{
	struct cpmac_priv *priv = netdev_priv(dev);

	ring->rx_max_pending = 1024;
	ring->rx_mini_max_pending = 1;
	ring->rx_jumbo_max_pending = 1;
	ring->tx_max_pending = 1;

	ring->rx_pending = priv->ring_size;
	ring->rx_mini_pending = 1;
	ring->rx_jumbo_pending = 1;
	ring->tx_pending = 1;
}

884 885
static int cpmac_set_ringparam(struct net_device *dev,
						struct ethtool_ringparam *ring)
M
Matteo Croce 已提交
886 887 888
{
	struct cpmac_priv *priv = netdev_priv(dev);

889
	if (netif_running(dev))
M
Matteo Croce 已提交
890 891
		return -EBUSY;
	priv->ring_size = ring->rx_pending;
892

M
Matteo Croce 已提交
893 894 895 896 897 898
	return 0;
}

static void cpmac_get_drvinfo(struct net_device *dev,
			      struct ethtool_drvinfo *info)
{
899 900 901
	strlcpy(info->driver, "cpmac", sizeof(info->driver));
	strlcpy(info->version, CPMAC_VERSION, sizeof(info->version));
	snprintf(info->bus_info, sizeof(info->bus_info), "%s", "cpmac");
M
Matteo Croce 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
}

static const struct ethtool_ops cpmac_ethtool_ops = {
	.get_settings = cpmac_get_settings,
	.set_settings = cpmac_set_settings,
	.get_drvinfo = cpmac_get_drvinfo,
	.get_link = ethtool_op_get_link,
	.get_ringparam = cpmac_get_ringparam,
	.set_ringparam = cpmac_set_ringparam,
};

static void cpmac_adjust_link(struct net_device *dev)
{
	struct cpmac_priv *priv = netdev_priv(dev);
	int new_state = 0;

	spin_lock(&priv->lock);
	if (priv->phy->link) {
920
		netif_tx_start_all_queues(dev);
M
Matteo Croce 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
		if (priv->phy->duplex != priv->oldduplex) {
			new_state = 1;
			priv->oldduplex = priv->phy->duplex;
		}

		if (priv->phy->speed != priv->oldspeed) {
			new_state = 1;
			priv->oldspeed = priv->phy->speed;
		}

		if (!priv->oldlink) {
			new_state = 1;
			priv->oldlink = 1;
		}
	} else if (priv->oldlink) {
		new_state = 1;
		priv->oldlink = 0;
		priv->oldspeed = 0;
		priv->oldduplex = -1;
	}

	if (new_state && netif_msg_link(priv) && net_ratelimit())
		phy_print_status(priv->phy);

	spin_unlock(&priv->lock);
}

static int cpmac_open(struct net_device *dev)
{
	int i, size, res;
	struct cpmac_priv *priv = netdev_priv(dev);
	struct resource *mem;
	struct cpmac_desc *desc;
	struct sk_buff *skb;

	mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
D
Dan Carpenter 已提交
957
	if (!request_mem_region(mem->start, resource_size(mem), dev->name)) {
M
Matteo Croce 已提交
958
		if (netif_msg_drv(priv))
V
Varka Bhadram 已提交
959 960
			netdev_err(dev, "failed to request registers\n");

M
Matteo Croce 已提交
961 962 963 964
		res = -ENXIO;
		goto fail_reserve;
	}

D
Dan Carpenter 已提交
965
	priv->regs = ioremap(mem->start, resource_size(mem));
M
Matteo Croce 已提交
966 967
	if (!priv->regs) {
		if (netif_msg_drv(priv))
V
Varka Bhadram 已提交
968 969
			netdev_err(dev, "failed to remap registers\n");

M
Matteo Croce 已提交
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
		res = -ENXIO;
		goto fail_remap;
	}

	size = priv->ring_size + CPMAC_QUEUES;
	priv->desc_ring = dma_alloc_coherent(&dev->dev,
					     sizeof(struct cpmac_desc) * size,
					     &priv->dma_ring,
					     GFP_KERNEL);
	if (!priv->desc_ring) {
		res = -ENOMEM;
		goto fail_alloc;
	}

	for (i = 0; i < size; i++)
		priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;

	priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
	for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
989
		skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
M
Matteo Croce 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001
		if (unlikely(!skb)) {
			res = -ENOMEM;
			goto fail_desc;
		}
		desc->skb = skb;
		desc->data_mapping = dma_map_single(&dev->dev, skb->data,
						    CPMAC_SKB_SIZE,
						    DMA_FROM_DEVICE);
		desc->hw_data = (u32)desc->data_mapping;
		desc->buflen = CPMAC_SKB_SIZE;
		desc->dataflags = CPMAC_OWN;
		desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
M
Matteo Croce 已提交
1002
		desc->next->prev = desc;
M
Matteo Croce 已提交
1003 1004 1005
		desc->hw_next = (u32)desc->next->mapping;
	}

M
Matteo Croce 已提交
1006 1007
	priv->rx_head->prev->hw_next = (u32)0;

1008 1009
	res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED, dev->name, dev);
	if (res) {
M
Matteo Croce 已提交
1010
		if (netif_msg_drv(priv))
V
Varka Bhadram 已提交
1011 1012
			netdev_err(dev, "failed to obtain irq\n");

M
Matteo Croce 已提交
1013 1014 1015
		goto fail_irq;
	}

M
Matteo Croce 已提交
1016
	atomic_set(&priv->reset_pending, 0);
M
Matteo Croce 已提交
1017 1018 1019
	INIT_WORK(&priv->reset_work, cpmac_hw_error);
	cpmac_hw_start(dev);

1020
	napi_enable(&priv->napi);
M
Matteo Croce 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
	priv->phy->state = PHY_CHANGELINK;
	phy_start(priv->phy);

	return 0;

fail_irq:
fail_desc:
	for (i = 0; i < priv->ring_size; i++) {
		if (priv->rx_head[i].skb) {
			dma_unmap_single(&dev->dev,
					 priv->rx_head[i].data_mapping,
					 CPMAC_SKB_SIZE,
					 DMA_FROM_DEVICE);
			kfree_skb(priv->rx_head[i].skb);
		}
	}
fail_alloc:
	kfree(priv->desc_ring);
	iounmap(priv->regs);

fail_remap:
D
Dan Carpenter 已提交
1042
	release_mem_region(mem->start, resource_size(mem));
M
Matteo Croce 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

fail_reserve:
	return res;
}

static int cpmac_stop(struct net_device *dev)
{
	int i;
	struct cpmac_priv *priv = netdev_priv(dev);
	struct resource *mem;

1054
	netif_tx_stop_all_queues(dev);
M
Matteo Croce 已提交
1055 1056

	cancel_work_sync(&priv->reset_work);
1057
	napi_disable(&priv->napi);
M
Matteo Croce 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
	phy_stop(priv->phy);

	cpmac_hw_stop(dev);

	for (i = 0; i < 8; i++)
		cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
	cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
	cpmac_write(priv->regs, CPMAC_MBP, 0);

	free_irq(dev->irq, dev);
	iounmap(priv->regs);
	mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
D
Dan Carpenter 已提交
1070
	release_mem_region(mem->start, resource_size(mem));
M
Matteo Croce 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
	priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
	for (i = 0; i < priv->ring_size; i++) {
		if (priv->rx_head[i].skb) {
			dma_unmap_single(&dev->dev,
					 priv->rx_head[i].data_mapping,
					 CPMAC_SKB_SIZE,
					 DMA_FROM_DEVICE);
			kfree_skb(priv->rx_head[i].skb);
		}
	}

	dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
			  (CPMAC_QUEUES + priv->ring_size),
			  priv->desc_ring, priv->dma_ring);
1085

M
Matteo Croce 已提交
1086 1087 1088
	return 0;
}

1089 1090 1091 1092 1093
static const struct net_device_ops cpmac_netdev_ops = {
	.ndo_open		= cpmac_open,
	.ndo_stop		= cpmac_stop,
	.ndo_start_xmit		= cpmac_start_xmit,
	.ndo_tx_timeout		= cpmac_tx_timeout,
1094
	.ndo_set_rx_mode	= cpmac_set_multicast_list,
1095
	.ndo_do_ioctl		= cpmac_ioctl,
1096 1097 1098 1099 1100
	.ndo_change_mtu		= eth_change_mtu,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_set_mac_address	= eth_mac_addr,
};

M
Matteo Croce 已提交
1101 1102
static int external_switch;

B
Bill Pemberton 已提交
1103
static int cpmac_probe(struct platform_device *pdev)
M
Matteo Croce 已提交
1104
{
1105
	int rc, phy_id;
1106
	char mdio_bus_id[MII_BUS_ID_SIZE];
M
Matteo Croce 已提交
1107 1108 1109 1110 1111
	struct resource *mem;
	struct cpmac_priv *priv;
	struct net_device *dev;
	struct plat_cpmac_data *pdata;

J
Jingoo Han 已提交
1112
	pdata = dev_get_platdata(&pdev->dev);
M
Matteo Croce 已提交
1113

1114
	if (external_switch || dumb_switch) {
1115
		strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
1116 1117 1118 1119 1120 1121 1122
		phy_id = pdev->id;
	} else {
		for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
			if (!(pdata->phy_mask & (1 << phy_id)))
				continue;
			if (!cpmac_mii->phy_map[phy_id])
				continue;
1123
			strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
1124 1125
			break;
		}
M
Matteo Croce 已提交
1126 1127 1128
	}

	if (phy_id == PHY_MAX_ADDR) {
1129
		dev_err(&pdev->dev, "no PHY present, falling back "
V
Varka Bhadram 已提交
1130
			"to switch on MDIO bus 0\n");
1131
		strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
1132
		phy_id = pdev->id;
M
Matteo Croce 已提交
1133
	}
1134
	mdio_bus_id[sizeof(mdio_bus_id) - 1] = '\0';
M
Matteo Croce 已提交
1135 1136

	dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
1137
	if (!dev)
M
Matteo Croce 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146
		return -ENOMEM;

	platform_set_drvdata(pdev, dev);
	priv = netdev_priv(dev);

	priv->pdev = pdev;
	mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
	if (!mem) {
		rc = -ENODEV;
1147
		goto out;
M
Matteo Croce 已提交
1148 1149 1150 1151
	}

	dev->irq = platform_get_irq_byname(pdev, "irq");

1152 1153
	dev->netdev_ops = &cpmac_netdev_ops;
	dev->ethtool_ops = &cpmac_ethtool_ops;
M
Matteo Croce 已提交
1154

1155 1156
	netif_napi_add(dev, &priv->napi, cpmac_poll, 64);

M
Matteo Croce 已提交
1157 1158 1159 1160 1161
	spin_lock_init(&priv->lock);
	spin_lock_init(&priv->rx_lock);
	priv->dev = dev;
	priv->ring_size = 64;
	priv->msg_enable = netif_msg_init(debug_level, 0xff);
1162
	memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
1163

1164 1165
	snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
						mdio_bus_id, phy_id);
1166

1167 1168
	priv->phy = phy_connect(dev, priv->phy_name, cpmac_adjust_link,
				PHY_INTERFACE_MODE_MII);
1169

1170 1171
	if (IS_ERR(priv->phy)) {
		if (netif_msg_drv(priv))
V
Varka Bhadram 已提交
1172 1173
			dev_err(&pdev->dev, "Could not attach to PHY\n");

1174
		rc = PTR_ERR(priv->phy);
1175
		goto out;
1176
	}
M
Matteo Croce 已提交
1177

1178 1179
	rc = register_netdev(dev);
	if (rc) {
V
Varka Bhadram 已提交
1180
		dev_err(&pdev->dev, "Could not register net device\n");
M
Matteo Croce 已提交
1181 1182 1183 1184
		goto fail;
	}

	if (netif_msg_probe(priv)) {
V
Varka Bhadram 已提交
1185 1186 1187
		dev_info(&pdev->dev, "regs: %p, irq: %d, phy: %s, "
			 "mac: %pM\n", (void *)mem->start, dev->irq,
			 priv->phy_name, dev->dev_addr);
M
Matteo Croce 已提交
1188
	}
1189

M
Matteo Croce 已提交
1190 1191 1192 1193
	return 0;

fail:
	free_netdev(dev);
1194
out:
M
Matteo Croce 已提交
1195 1196 1197
	return rc;
}

B
Bill Pemberton 已提交
1198
static int cpmac_remove(struct platform_device *pdev)
M
Matteo Croce 已提交
1199 1200
{
	struct net_device *dev = platform_get_drvdata(pdev);
1201

M
Matteo Croce 已提交
1202 1203
	unregister_netdev(dev);
	free_netdev(dev);
1204

M
Matteo Croce 已提交
1205 1206 1207 1208
	return 0;
}

static struct platform_driver cpmac_driver = {
1209 1210 1211 1212
	.driver = {
		.name 	= "cpmac",
	},
	.probe 	= cpmac_probe,
B
Bill Pemberton 已提交
1213
	.remove = cpmac_remove,
M
Matteo Croce 已提交
1214 1215
};

B
Bill Pemberton 已提交
1216
int cpmac_init(void)
M
Matteo Croce 已提交
1217 1218 1219 1220
{
	u32 mask;
	int i, res;

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	cpmac_mii = mdiobus_alloc();
	if (cpmac_mii == NULL)
		return -ENOMEM;

	cpmac_mii->name = "cpmac-mii";
	cpmac_mii->read = cpmac_mdio_read;
	cpmac_mii->write = cpmac_mdio_write;
	cpmac_mii->reset = cpmac_mdio_reset;
	cpmac_mii->irq = mii_irqs;

	cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
M
Matteo Croce 已提交
1232

1233
	if (!cpmac_mii->priv) {
V
Varka Bhadram 已提交
1234
		pr_err("Can't ioremap mdio registers\n");
1235 1236
		res = -ENXIO;
		goto fail_alloc;
M
Matteo Croce 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245
	}

#warning FIXME: unhardcode gpio&reset bits
	ar7_gpio_disable(26);
	ar7_gpio_disable(27);
	ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
	ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
	ar7_device_reset(AR7_RESET_BIT_EPHY);

1246
	cpmac_mii->reset(cpmac_mii);
M
Matteo Croce 已提交
1247

1248 1249 1250
	for (i = 0; i < 300; i++) {
		mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE);
		if (mask)
M
Matteo Croce 已提交
1251 1252
			break;
		else
1253
			msleep(10);
1254
	}
M
Matteo Croce 已提交
1255 1256 1257 1258 1259 1260 1261

	mask &= 0x7fffffff;
	if (mask & (mask - 1)) {
		external_switch = 1;
		mask = 0;
	}

1262
	cpmac_mii->phy_mask = ~(mask | 0x80000000);
1263
	snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "cpmac-1");
M
Matteo Croce 已提交
1264

1265
	res = mdiobus_register(cpmac_mii);
M
Matteo Croce 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
	if (res)
		goto fail_mii;

	res = platform_driver_register(&cpmac_driver);
	if (res)
		goto fail_cpmac;

	return 0;

fail_cpmac:
1276
	mdiobus_unregister(cpmac_mii);
M
Matteo Croce 已提交
1277 1278

fail_mii:
1279 1280 1281 1282
	iounmap(cpmac_mii->priv);

fail_alloc:
	mdiobus_free(cpmac_mii);
M
Matteo Croce 已提交
1283 1284 1285 1286

	return res;
}

B
Bill Pemberton 已提交
1287
void cpmac_exit(void)
M
Matteo Croce 已提交
1288 1289
{
	platform_driver_unregister(&cpmac_driver);
1290 1291
	mdiobus_unregister(cpmac_mii);
	iounmap(cpmac_mii->priv);
D
Dan Carpenter 已提交
1292
	mdiobus_free(cpmac_mii);
M
Matteo Croce 已提交
1293 1294 1295 1296
}

module_init(cpmac_init);
module_exit(cpmac_exit);