saa9730.c 30.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
R
Ralf Baechle 已提交
2 3 4 5
 * Copyright (C) 2000, 2005  MIPS Technologies, Inc.  All rights reserved.
 *	Authors: Carsten Langgaard <carstenl@mips.com>
 *		 Maciej W. Rozycki <macro@mips.com>
 * Copyright (C) 2004 Ralf Baechle <ralf@linux-mips.org>
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  This program is free software; you can distribute it and/or modify it
 *  under the terms of the GNU General Public License (Version 2) as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope 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.,
 *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
 *
 * SAA9730 ethernet driver.
 *
 * Changes:
R
Ralf Baechle 已提交
23 24 25 26
 * Angelo Dell'Aera <buffer@antifork.org> :	Conversion to the new PCI API
 *						(pci_driver).
 *						Conversion to spinlocks.
 *						Error handling fixes.
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34 35 36
 */

#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/delay.h>
#include <linux/etherdevice.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/pci.h>
#include <linux/spinlock.h>
R
Ralf Baechle 已提交
37
#include <linux/types.h>
L
Linus Torvalds 已提交
38 39

#include <asm/addrspace.h>
R
Ralf Baechle 已提交
40 41
#include <asm/io.h>

L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54
#include <asm/mips-boards/prom.h>

#include "saa9730.h"

#ifdef LAN_SAA9730_DEBUG
int lan_saa9730_debug = LAN_SAA9730_DEBUG;
#else
int lan_saa9730_debug;
#endif

#define DRV_MODULE_NAME "saa9730"

static struct pci_device_id saa9730_pci_tbl[] = {
R
Ralf Baechle 已提交
55
	{ PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA9730,
56
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66
	{ 0, }
};

MODULE_DEVICE_TABLE(pci, saa9730_pci_tbl);

/* Non-zero only if the current card is a PCI with BIOS-set IRQ. */
static unsigned int pci_irq_line;

static void evm_saa9730_enable_lan_int(struct lan_saa9730_private *lp)
{
67 68 69 70 71 72
	writel(readl(&lp->evm_saa9730_regs->InterruptBlock1) | EVM_LAN_INT,
	       &lp->evm_saa9730_regs->InterruptBlock1);
	writel(readl(&lp->evm_saa9730_regs->InterruptStatus1) | EVM_LAN_INT,
	       &lp->evm_saa9730_regs->InterruptStatus1);
	writel(readl(&lp->evm_saa9730_regs->InterruptEnable1) | EVM_LAN_INT |
	       EVM_MASTER_EN, &lp->evm_saa9730_regs->InterruptEnable1);
L
Linus Torvalds 已提交
73
}
R
Ralf Baechle 已提交
74

L
Linus Torvalds 已提交
75 76
static void evm_saa9730_disable_lan_int(struct lan_saa9730_private *lp)
{
77 78 79 80
	writel(readl(&lp->evm_saa9730_regs->InterruptBlock1) & ~EVM_LAN_INT,
	       &lp->evm_saa9730_regs->InterruptBlock1);
	writel(readl(&lp->evm_saa9730_regs->InterruptEnable1) & ~EVM_LAN_INT,
	       &lp->evm_saa9730_regs->InterruptEnable1);
L
Linus Torvalds 已提交
81 82 83 84
}

static void evm_saa9730_clear_lan_int(struct lan_saa9730_private *lp)
{
85
	writel(EVM_LAN_INT, &lp->evm_saa9730_regs->InterruptStatus1);
L
Linus Torvalds 已提交
86 87 88 89
}

static void evm_saa9730_block_lan_int(struct lan_saa9730_private *lp)
{
90 91
	writel(readl(&lp->evm_saa9730_regs->InterruptBlock1) & ~EVM_LAN_INT,
	       &lp->evm_saa9730_regs->InterruptBlock1);
L
Linus Torvalds 已提交
92 93 94 95
}

static void evm_saa9730_unblock_lan_int(struct lan_saa9730_private *lp)
{
96 97
	writel(readl(&lp->evm_saa9730_regs->InterruptBlock1) | EVM_LAN_INT,
	       &lp->evm_saa9730_regs->InterruptBlock1);
L
Linus Torvalds 已提交
98 99
}

R
Ralf Baechle 已提交
100
static void __used show_saa9730_regs(struct net_device *dev)
L
Linus Torvalds 已提交
101
{
R
Ralf Baechle 已提交
102
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
103
	int i, j;
R
Ralf Baechle 已提交
104

R
Ralf Baechle 已提交
105 106 107 108
	printk("TxmBufferA = %p\n", lp->TxmBuffer[0][0]);
	printk("TxmBufferB = %p\n", lp->TxmBuffer[1][0]);
	printk("RcvBufferA = %p\n", lp->RcvBuffer[0][0]);
	printk("RcvBufferB = %p\n", lp->RcvBuffer[1][0]);
R
Ralf Baechle 已提交
109

L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
			printk("TxmBuffer[%d][%d] = %x\n", i, j,
			       le32_to_cpu(*(unsigned int *)
					   lp->TxmBuffer[i][j]));
		}
	}
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
			printk("RcvBuffer[%d][%d] = %x\n", i, j,
			       le32_to_cpu(*(unsigned int *)
					   lp->RcvBuffer[i][j]));
		}
	}
	printk("lp->evm_saa9730_regs->InterruptBlock1 = %x\n",
R
Ralf Baechle 已提交
125
	       readl(&lp->evm_saa9730_regs->InterruptBlock1));
L
Linus Torvalds 已提交
126
	printk("lp->evm_saa9730_regs->InterruptStatus1 = %x\n",
R
Ralf Baechle 已提交
127
	       readl(&lp->evm_saa9730_regs->InterruptStatus1));
L
Linus Torvalds 已提交
128
	printk("lp->evm_saa9730_regs->InterruptEnable1 = %x\n",
R
Ralf Baechle 已提交
129
	       readl(&lp->evm_saa9730_regs->InterruptEnable1));
L
Linus Torvalds 已提交
130
	printk("lp->lan_saa9730_regs->Ok2Use = %x\n",
R
Ralf Baechle 已提交
131
	       readl(&lp->lan_saa9730_regs->Ok2Use));
L
Linus Torvalds 已提交
132 133 134 135 136 137 138
	printk("lp->NextTxmBufferIndex = %x\n", lp->NextTxmBufferIndex);
	printk("lp->NextTxmPacketIndex = %x\n", lp->NextTxmPacketIndex);
	printk("lp->PendingTxmBufferIndex = %x\n",
	       lp->PendingTxmBufferIndex);
	printk("lp->PendingTxmPacketIndex = %x\n",
	       lp->PendingTxmPacketIndex);
	printk("lp->lan_saa9730_regs->LanDmaCtl = %x\n",
R
Ralf Baechle 已提交
139
	       readl(&lp->lan_saa9730_regs->LanDmaCtl));
L
Linus Torvalds 已提交
140
	printk("lp->lan_saa9730_regs->DmaStatus = %x\n",
R
Ralf Baechle 已提交
141
	       readl(&lp->lan_saa9730_regs->DmaStatus));
L
Linus Torvalds 已提交
142
	printk("lp->lan_saa9730_regs->CamCtl = %x\n",
R
Ralf Baechle 已提交
143
	       readl(&lp->lan_saa9730_regs->CamCtl));
L
Linus Torvalds 已提交
144
	printk("lp->lan_saa9730_regs->TxCtl = %x\n",
R
Ralf Baechle 已提交
145
	       readl(&lp->lan_saa9730_regs->TxCtl));
L
Linus Torvalds 已提交
146
	printk("lp->lan_saa9730_regs->TxStatus = %x\n",
R
Ralf Baechle 已提交
147
	       readl(&lp->lan_saa9730_regs->TxStatus));
L
Linus Torvalds 已提交
148
	printk("lp->lan_saa9730_regs->RxCtl = %x\n",
R
Ralf Baechle 已提交
149
	       readl(&lp->lan_saa9730_regs->RxCtl));
L
Linus Torvalds 已提交
150
	printk("lp->lan_saa9730_regs->RxStatus = %x\n",
R
Ralf Baechle 已提交
151
	       readl(&lp->lan_saa9730_regs->RxStatus));
R
Ralf Baechle 已提交
152

L
Linus Torvalds 已提交
153
	for (i = 0; i < LAN_SAA9730_CAM_DWORDS; i++) {
154
		writel(i, &lp->lan_saa9730_regs->CamAddress);
L
Linus Torvalds 已提交
155
		printk("lp->lan_saa9730_regs->CamData = %x\n",
R
Ralf Baechle 已提交
156
		       readl(&lp->lan_saa9730_regs->CamData));
L
Linus Torvalds 已提交
157
	}
R
Ralf Baechle 已提交
158

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	printk("dev->stats.tx_packets = %lx\n", dev->stats.tx_packets);
	printk("dev->stats.tx_errors = %lx\n", dev->stats.tx_errors);
	printk("dev->stats.tx_aborted_errors = %lx\n",
	       dev->stats.tx_aborted_errors);
	printk("dev->stats.tx_window_errors = %lx\n",
	       dev->stats.tx_window_errors);
	printk("dev->stats.tx_carrier_errors = %lx\n",
	       dev->stats.tx_carrier_errors);
	printk("dev->stats.tx_fifo_errors = %lx\n",
	       dev->stats.tx_fifo_errors);
	printk("dev->stats.tx_heartbeat_errors = %lx\n",
	       dev->stats.tx_heartbeat_errors);
	printk("dev->stats.collisions = %lx\n", dev->stats.collisions);

	printk("dev->stats.rx_packets = %lx\n", dev->stats.rx_packets);
	printk("dev->stats.rx_errors = %lx\n", dev->stats.rx_errors);
	printk("dev->stats.rx_dropped = %lx\n", dev->stats.rx_dropped);
	printk("dev->stats.rx_crc_errors = %lx\n", dev->stats.rx_crc_errors);
	printk("dev->stats.rx_frame_errors = %lx\n",
	       dev->stats.rx_frame_errors);
	printk("dev->stats.rx_fifo_errors = %lx\n",
	       dev->stats.rx_fifo_errors);
	printk("dev->stats.rx_length_errors = %lx\n",
	       dev->stats.rx_length_errors);
L
Linus Torvalds 已提交
183 184

	printk("lp->lan_saa9730_regs->DebugPCIMasterAddr = %x\n",
R
Ralf Baechle 已提交
185
	       readl(&lp->lan_saa9730_regs->DebugPCIMasterAddr));
L
Linus Torvalds 已提交
186
	printk("lp->lan_saa9730_regs->DebugLanTxStateMachine = %x\n",
R
Ralf Baechle 已提交
187
	       readl(&lp->lan_saa9730_regs->DebugLanTxStateMachine));
L
Linus Torvalds 已提交
188
	printk("lp->lan_saa9730_regs->DebugLanRxStateMachine = %x\n",
R
Ralf Baechle 已提交
189
	       readl(&lp->lan_saa9730_regs->DebugLanRxStateMachine));
L
Linus Torvalds 已提交
190
	printk("lp->lan_saa9730_regs->DebugLanTxFifoPointers = %x\n",
R
Ralf Baechle 已提交
191
	       readl(&lp->lan_saa9730_regs->DebugLanTxFifoPointers));
L
Linus Torvalds 已提交
192
	printk("lp->lan_saa9730_regs->DebugLanRxFifoPointers = %x\n",
R
Ralf Baechle 已提交
193
	       readl(&lp->lan_saa9730_regs->DebugLanRxFifoPointers));
L
Linus Torvalds 已提交
194
	printk("lp->lan_saa9730_regs->DebugLanCtlStateMachine = %x\n",
R
Ralf Baechle 已提交
195
	       readl(&lp->lan_saa9730_regs->DebugLanCtlStateMachine));
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
}

static void lan_saa9730_buffer_init(struct lan_saa9730_private *lp)
{
	int i, j;

	/* Init RX buffers */
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
			*(unsigned int *) lp->RcvBuffer[i][j] =
			    cpu_to_le32(RXSF_READY <<
					RX_STAT_CTL_OWNER_SHF);
		}
	}

	/* Init TX buffers */
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
			*(unsigned int *) lp->TxmBuffer[i][j] =
			    cpu_to_le32(TXSF_EMPTY <<
					TX_STAT_CTL_OWNER_SHF);
		}
	}
}

R
Ralf Baechle 已提交
221 222 223 224 225 226 227 228 229
static void lan_saa9730_free_buffers(struct pci_dev *pdev,
				     struct lan_saa9730_private *lp)
{
	pci_free_consistent(pdev, lp->buffer_size, lp->buffer_start,
			    lp->dma_addr);
}

static int lan_saa9730_allocate_buffers(struct pci_dev *pdev,
					struct lan_saa9730_private *lp)
L
Linus Torvalds 已提交
230 231
{
	void *Pa;
R
Ralf Baechle 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	unsigned int i, j, rxoffset, txoffset;
	int ret;

	/* Initialize buffer space */
	lp->DmaRcvPackets = LAN_SAA9730_RCV_Q_SIZE;
	lp->DmaTxmPackets = LAN_SAA9730_TXM_Q_SIZE;

	/* Initialize Rx Buffer Index */
	lp->NextRcvPacketIndex = 0;
	lp->NextRcvBufferIndex = 0;

	/* Set current buffer index & next available packet index */
	lp->NextTxmPacketIndex = 0;
	lp->NextTxmBufferIndex = 0;
	lp->PendingTxmPacketIndex = 0;
	lp->PendingTxmBufferIndex = 0;
L
Linus Torvalds 已提交
248

249 250
	/*
	 * Allocate all RX and TX packets in one chunk.
L
Linus Torvalds 已提交
251 252
	 * The Rx and Tx packets must be PACKET_SIZE aligned.
	 */
R
Ralf Baechle 已提交
253 254 255 256 257 258 259 260 261
	lp->buffer_size = ((LAN_SAA9730_RCV_Q_SIZE + LAN_SAA9730_TXM_Q_SIZE) *
			   LAN_SAA9730_PACKET_SIZE * LAN_SAA9730_BUFFERS) +
			  LAN_SAA9730_PACKET_SIZE;
	lp->buffer_start = pci_alloc_consistent(pdev, lp->buffer_size,
						&lp->dma_addr);
	if (!lp->buffer_start) {
		ret = -ENOMEM;
		goto out;
	}
L
Linus Torvalds 已提交
262

R
Ralf Baechle 已提交
263 264
	Pa = (void *)ALIGN((unsigned long)lp->buffer_start,
			   LAN_SAA9730_PACKET_SIZE);
L
Linus Torvalds 已提交
265

R
Ralf Baechle 已提交
266
	rxoffset = Pa - lp->buffer_start;
L
Linus Torvalds 已提交
267 268 269 270 271 272 273

	/* Init RX buffers */
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
			*(unsigned int *) Pa =
			    cpu_to_le32(RXSF_READY <<
					RX_STAT_CTL_OWNER_SHF);
R
Ralf Baechle 已提交
274 275
			lp->RcvBuffer[i][j] = Pa;
			Pa += LAN_SAA9730_PACKET_SIZE;
L
Linus Torvalds 已提交
276 277 278
		}
	}

R
Ralf Baechle 已提交
279 280
	txoffset = Pa - lp->buffer_start;

L
Linus Torvalds 已提交
281 282 283 284 285 286
	/* Init TX buffers */
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
			*(unsigned int *) Pa =
			    cpu_to_le32(TXSF_EMPTY <<
					TX_STAT_CTL_OWNER_SHF);
R
Ralf Baechle 已提交
287 288
			lp->TxmBuffer[i][j] = Pa;
			Pa += LAN_SAA9730_PACKET_SIZE;
L
Linus Torvalds 已提交
289 290 291
		}
	}

292 293
	/*
	 * Set rx buffer A and rx buffer B to point to the first two buffer
L
Linus Torvalds 已提交
294 295
	 * spaces.
	 */
296 297 298 299
	writel(lp->dma_addr + rxoffset, &lp->lan_saa9730_regs->RxBuffA);
	writel(lp->dma_addr + rxoffset +
	       LAN_SAA9730_PACKET_SIZE * LAN_SAA9730_RCV_Q_SIZE,
	       &lp->lan_saa9730_regs->RxBuffB);
L
Linus Torvalds 已提交
300

301
	/*
L
Linus Torvalds 已提交
302
	 * Set txm_buf_a and txm_buf_b to point to the first two buffer
303
	 * space
L
Linus Torvalds 已提交
304
	 */
305 306 307 308 309
	writel(lp->dma_addr + txoffset,
	       &lp->lan_saa9730_regs->TxBuffA);
	writel(lp->dma_addr + txoffset +
	       LAN_SAA9730_PACKET_SIZE * LAN_SAA9730_TXM_Q_SIZE,
	       &lp->lan_saa9730_regs->TxBuffB);
L
Linus Torvalds 已提交
310 311

	/* Set packet number */
312 313 314 315 316
	writel((lp->DmaRcvPackets << PK_COUNT_RX_A_SHF) |
	       (lp->DmaRcvPackets << PK_COUNT_RX_B_SHF) |
	       (lp->DmaTxmPackets << PK_COUNT_TX_A_SHF) |
	       (lp->DmaTxmPackets << PK_COUNT_TX_B_SHF),
	       &lp->lan_saa9730_regs->PacketCount);
L
Linus Torvalds 已提交
317 318

	return 0;
R
Ralf Baechle 已提交
319 320 321

out:
	return ret;
L
Linus Torvalds 已提交
322 323 324 325 326 327 328 329 330 331 332
}

static int lan_saa9730_cam_load(struct lan_saa9730_private *lp)
{
	unsigned int i;
	unsigned char *NetworkAddress;

	NetworkAddress = (unsigned char *) &lp->PhysicalAddress[0][0];

	for (i = 0; i < LAN_SAA9730_CAM_DWORDS; i++) {
		/* First set address to where data is written */
333 334 335 336
		writel(i, &lp->lan_saa9730_regs->CamAddress);
		writel((NetworkAddress[0] << 24) | (NetworkAddress[1] << 16) |
		       (NetworkAddress[2] << 8) | NetworkAddress[3],
		       &lp->lan_saa9730_regs->CamData);
L
Linus Torvalds 已提交
337 338 339 340 341 342 343
		NetworkAddress += 4;
	}
	return 0;
}

static int lan_saa9730_cam_init(struct net_device *dev)
{
R
Ralf Baechle 已提交
344
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
	unsigned int i;

	/* Copy MAC-address into all entries. */
	for (i = 0; i < LAN_SAA9730_CAM_ENTRIES; i++) {
		memcpy((unsigned char *) lp->PhysicalAddress[i],
		       (unsigned char *) dev->dev_addr, 6);
	}

	return 0;
}

static int lan_saa9730_mii_init(struct lan_saa9730_private *lp)
{
	int i, l;

	/* Check link status, spin here till station is not busy. */
	i = 0;
R
Ralf Baechle 已提交
362
	while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) & MD_CA_BUSY) {
L
Linus Torvalds 已提交
363 364 365 366 367 368 369 370 371
		i++;
		if (i > 100) {
			printk("Error: lan_saa9730_mii_init: timeout\n");
			return -1;
		}
		mdelay(1);	/* wait 1 ms. */
	}

	/* Now set the control and address register. */
372 373
	writel(MD_CA_BUSY | PHY_STATUS | PHY_ADDRESS << MD_CA_PHY_SHF,
	       &lp->lan_saa9730_regs->StationMgmtCtl);
L
Linus Torvalds 已提交
374 375 376

	/* check link status, spin here till station is not busy */
	i = 0;
R
Ralf Baechle 已提交
377
	while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) & MD_CA_BUSY) {
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385 386 387 388 389
		i++;
		if (i > 100) {
			printk("Error: lan_saa9730_mii_init: timeout\n");
			return -1;
		}
		mdelay(1);	/* wait 1 ms. */
	}

	/* Wait for 1 ms. */
	mdelay(1);

	/* Check the link status. */
R
Ralf Baechle 已提交
390
	if (readl(&lp->lan_saa9730_regs->StationMgmtData) &
L
Linus Torvalds 已提交
391 392 393 394 395 396 397
	    PHY_STATUS_LINK_UP) {
		/* Link is up. */
		return 0;
	} else {
		/* Link is down, reset the PHY first. */

		/* set PHY address = 'CONTROL' */
398 399
		writel(PHY_ADDRESS << MD_CA_PHY_SHF | MD_CA_WR | PHY_CONTROL,
		       &lp->lan_saa9730_regs->StationMgmtCtl);
L
Linus Torvalds 已提交
400 401 402 403 404

		/* Wait for 1 ms. */
		mdelay(1);

		/* set 'CONTROL' = force reset and renegotiate */
405 406 407
		writel(PHY_CONTROL_RESET | PHY_CONTROL_AUTO_NEG |
		       PHY_CONTROL_RESTART_AUTO_NEG,
		       &lp->lan_saa9730_regs->StationMgmtData);
L
Linus Torvalds 已提交
408 409 410 411 412

		/* Wait for 50 ms. */
		mdelay(50);

		/* set 'BUSY' to start operation */
413 414
		writel(MD_CA_BUSY | PHY_ADDRESS << MD_CA_PHY_SHF | MD_CA_WR |
		       PHY_CONTROL, &lp->lan_saa9730_regs->StationMgmtCtl);
L
Linus Torvalds 已提交
415 416 417

		/* await completion */
		i = 0;
R
Ralf Baechle 已提交
418
		while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) &
L
Linus Torvalds 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
		       MD_CA_BUSY) {
			i++;
			if (i > 100) {
				printk
				    ("Error: lan_saa9730_mii_init: timeout\n");
				return -1;
			}
			mdelay(1);	/* wait 1 ms. */
		}

		/* Wait for 1 ms. */
		mdelay(1);

		for (l = 0; l < 2; l++) {
			/* set PHY address = 'STATUS' */
434 435 436
			writel(MD_CA_BUSY | PHY_ADDRESS << MD_CA_PHY_SHF |
			       PHY_STATUS,
			       &lp->lan_saa9730_regs->StationMgmtCtl);
L
Linus Torvalds 已提交
437 438 439

			/* await completion */
			i = 0;
R
Ralf Baechle 已提交
440
			while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) &
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454
			       MD_CA_BUSY) {
				i++;
				if (i > 100) {
					printk
					    ("Error: lan_saa9730_mii_init: timeout\n");
					return -1;
				}
				mdelay(1);	/* wait 1 ms. */
			}

			/* wait for 3 sec. */
			mdelay(3000);

			/* check the link status */
R
Ralf Baechle 已提交
455
			if (readl(&lp->lan_saa9730_regs->StationMgmtData) &
L
Linus Torvalds 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468
			    PHY_STATUS_LINK_UP) {
				/* link is up */
				break;
			}
		}
	}

	return 0;
}

static int lan_saa9730_control_init(struct lan_saa9730_private *lp)
{
	/* Initialize DMA control register. */
469 470 471 472 473 474
	writel((LANMB_ANY << DMA_CTL_MAX_XFER_SHF) |
	       (LANEND_LITTLE << DMA_CTL_ENDIAN_SHF) |
	       (LAN_SAA9730_RCV_Q_INT_THRESHOLD << DMA_CTL_RX_INT_COUNT_SHF)
	       | DMA_CTL_RX_INT_TO_EN | DMA_CTL_RX_INT_EN |
	       DMA_CTL_MAC_RX_INT_EN | DMA_CTL_MAC_TX_INT_EN,
	       &lp->lan_saa9730_regs->LanDmaCtl);
L
Linus Torvalds 已提交
475 476

	/* Initial MAC control register. */
477 478
	writel((MACCM_MII << MAC_CONTROL_CONN_SHF) | MAC_CONTROL_FULL_DUP,
	       &lp->lan_saa9730_regs->MacCtl);
L
Linus Torvalds 已提交
479 480

	/* Initialize CAM control register. */
481 482
	writel(CAM_CONTROL_COMP_EN | CAM_CONTROL_BROAD_ACC,
	       &lp->lan_saa9730_regs->CamCtl);
L
Linus Torvalds 已提交
483

484
	/*
L
Linus Torvalds 已提交
485
	 * Initialize CAM enable register, only turn on first entry, should
486
	 * contain own addr.
L
Linus Torvalds 已提交
487
	 */
488
	writel(0x0001, &lp->lan_saa9730_regs->CamEnable);
L
Linus Torvalds 已提交
489 490

	/* Initialize Tx control register */
491
	writel(TX_CTL_EN_COMP, &lp->lan_saa9730_regs->TxCtl);
L
Linus Torvalds 已提交
492 493

	/* Initialize Rcv control register */
494
	writel(RX_CTL_STRIP_CRC, &lp->lan_saa9730_regs->RxCtl);
L
Linus Torvalds 已提交
495 496

	/* Reset DMA engine */
497
	writel(DMA_TEST_SW_RESET, &lp->lan_saa9730_regs->DmaTest);
L
Linus Torvalds 已提交
498 499 500 501 502 503 504 505 506

	return 0;
}

static int lan_saa9730_stop(struct lan_saa9730_private *lp)
{
	int i;

	/* Stop DMA first */
507 508 509
	writel(readl(&lp->lan_saa9730_regs->LanDmaCtl) &
	       ~(DMA_CTL_EN_TX_DMA | DMA_CTL_EN_RX_DMA),
	       &lp->lan_saa9730_regs->LanDmaCtl);
L
Linus Torvalds 已提交
510 511

	/* Set the SW Reset bits in DMA and MAC control registers */
512 513 514
	writel(DMA_TEST_SW_RESET, &lp->lan_saa9730_regs->DmaTest);
	writel(readl(&lp->lan_saa9730_regs->MacCtl) | MAC_CONTROL_RESET,
	       &lp->lan_saa9730_regs->MacCtl);
L
Linus Torvalds 已提交
515

516
	/*
L
Linus Torvalds 已提交
517 518 519 520
	 * Wait for MAC reset to have finished. The reset bit is auto cleared
	 * when the reset is done.
	 */
	i = 0;
R
Ralf Baechle 已提交
521
	while (readl(&lp->lan_saa9730_regs->MacCtl) & MAC_CONTROL_RESET) {
L
Linus Torvalds 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
		i++;
		if (i > 100) {
			printk
			    ("Error: lan_sa9730_stop: MAC reset timeout\n");
			return -1;
		}
		mdelay(1);	/* wait 1 ms. */
	}

	return 0;
}

static int lan_saa9730_dma_init(struct lan_saa9730_private *lp)
{
	/* Stop lan controller. */
	lan_saa9730_stop(lp);

539 540
	writel(LAN_SAA9730_DEFAULT_TIME_OUT_CNT,
	       &lp->lan_saa9730_regs->Timeout);
L
Linus Torvalds 已提交
541 542 543 544 545 546 547 548 549 550

	return 0;
}

static int lan_saa9730_start(struct lan_saa9730_private *lp)
{
	lan_saa9730_buffer_init(lp);

	/* Initialize Rx Buffer Index */
	lp->NextRcvPacketIndex = 0;
R
Ralf Baechle 已提交
551
	lp->NextRcvBufferIndex = 0;
L
Linus Torvalds 已提交
552

R
Ralf Baechle 已提交
553
	/* Set current buffer index & next available packet index */
L
Linus Torvalds 已提交
554 555 556 557 558
	lp->NextTxmPacketIndex = 0;
	lp->NextTxmBufferIndex = 0;
	lp->PendingTxmPacketIndex = 0;
	lp->PendingTxmBufferIndex = 0;

559 560
	writel(readl(&lp->lan_saa9730_regs->LanDmaCtl) | DMA_CTL_EN_TX_DMA |
	       DMA_CTL_EN_RX_DMA, &lp->lan_saa9730_regs->LanDmaCtl);
L
Linus Torvalds 已提交
561 562

	/* For Tx, turn on MAC then DMA */
563 564
	writel(readl(&lp->lan_saa9730_regs->TxCtl) | TX_CTL_TX_EN,
	       &lp->lan_saa9730_regs->TxCtl);
L
Linus Torvalds 已提交
565 566

	/* For Rx, turn on DMA then MAC */
567 568
	writel(readl(&lp->lan_saa9730_regs->RxCtl) | RX_CTL_RX_EN,
	       &lp->lan_saa9730_regs->RxCtl);
L
Linus Torvalds 已提交
569

R
Ralf Baechle 已提交
570
	/* Set Ok2Use to let hardware own the buffers.	*/
571
	writel(OK2USE_RX_A | OK2USE_RX_B, &lp->lan_saa9730_regs->Ok2Use);
L
Linus Torvalds 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585

	return 0;
}

static int lan_saa9730_restart(struct lan_saa9730_private *lp)
{
	lan_saa9730_stop(lp);
	lan_saa9730_start(lp);

	return 0;
}

static int lan_saa9730_tx(struct net_device *dev)
{
R
Ralf Baechle 已提交
586
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
587 588 589 590 591 592 593
	unsigned int *pPacket;
	unsigned int tx_status;

	if (lan_saa9730_debug > 5)
		printk("lan_saa9730_tx interrupt\n");

	/* Clear interrupt. */
594
	writel(DMA_STATUS_MAC_TX_INT, &lp->lan_saa9730_regs->DmaStatus);
L
Linus Torvalds 已提交
595 596

	while (1) {
R
Ralf Baechle 已提交
597 598
		pPacket = lp->TxmBuffer[lp->PendingTxmBufferIndex]
				       [lp->PendingTxmPacketIndex];
L
Linus Torvalds 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612

		/* Get status of first packet transmitted. */
		tx_status = le32_to_cpu(*pPacket);

		/* Check ownership. */
		if ((tx_status & TX_STAT_CTL_OWNER_MSK) !=
		    (TXSF_HWDONE << TX_STAT_CTL_OWNER_SHF)) break;

		/* Check for error. */
		if (tx_status & TX_STAT_CTL_ERROR_MSK) {
			if (lan_saa9730_debug > 1)
				printk("lan_saa9730_tx: tx error = %x\n",
				       tx_status);

613
			dev->stats.tx_errors++;
L
Linus Torvalds 已提交
614 615
			if (tx_status &
			    (TX_STATUS_EX_COLL << TX_STAT_CTL_STATUS_SHF))
616
				dev->stats.tx_aborted_errors++;
L
Linus Torvalds 已提交
617
			if (tx_status &
R
Ralf Baechle 已提交
618
			    (TX_STATUS_LATE_COLL << TX_STAT_CTL_STATUS_SHF))
619
				dev->stats.tx_window_errors++;
L
Linus Torvalds 已提交
620 621
			if (tx_status &
			    (TX_STATUS_L_CARR << TX_STAT_CTL_STATUS_SHF))
622
				dev->stats.tx_carrier_errors++;
L
Linus Torvalds 已提交
623 624
			if (tx_status &
			    (TX_STATUS_UNDER << TX_STAT_CTL_STATUS_SHF))
625
				dev->stats.tx_fifo_errors++;
L
Linus Torvalds 已提交
626 627
			if (tx_status &
			    (TX_STATUS_SQ_ERR << TX_STAT_CTL_STATUS_SHF))
628
				dev->stats.tx_heartbeat_errors++;
L
Linus Torvalds 已提交
629

630
			dev->stats.collisions +=
R
Ralf Baechle 已提交
631
				tx_status & TX_STATUS_TX_COLL_MSK;
L
Linus Torvalds 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645
		}

		/* Free buffer. */
		*pPacket =
		    cpu_to_le32(TXSF_EMPTY << TX_STAT_CTL_OWNER_SHF);

		/* Update pending index pointer. */
		lp->PendingTxmPacketIndex++;
		if (lp->PendingTxmPacketIndex >= LAN_SAA9730_TXM_Q_SIZE) {
			lp->PendingTxmPacketIndex = 0;
			lp->PendingTxmBufferIndex ^= 1;
		}
	}

R
Ralf Baechle 已提交
646 647
	/* The tx buffer is no longer full. */
	netif_wake_queue(dev);
L
Linus Torvalds 已提交
648 649 650 651 652 653

	return 0;
}

static int lan_saa9730_rx(struct net_device *dev)
{
R
Ralf Baechle 已提交
654
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
655 656 657 658 659 660 661 662 663 664 665 666
	int len = 0;
	struct sk_buff *skb = 0;
	unsigned int rx_status;
	int BufferIndex;
	int PacketIndex;
	unsigned int *pPacket;
	unsigned char *pData;

	if (lan_saa9730_debug > 5)
		printk("lan_saa9730_rx interrupt\n");

	/* Clear receive interrupts. */
667 668
	writel(DMA_STATUS_MAC_RX_INT | DMA_STATUS_RX_INT |
	       DMA_STATUS_RX_TO_INT, &lp->lan_saa9730_regs->DmaStatus);
L
Linus Torvalds 已提交
669 670

	/* Address next packet */
R
Ralf Baechle 已提交
671
	BufferIndex = lp->NextRcvBufferIndex;
L
Linus Torvalds 已提交
672
	PacketIndex = lp->NextRcvPacketIndex;
R
Ralf Baechle 已提交
673
	pPacket = lp->RcvBuffer[BufferIndex][PacketIndex];
L
Linus Torvalds 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
	rx_status = le32_to_cpu(*pPacket);

	/* Process each packet. */
	while ((rx_status & RX_STAT_CTL_OWNER_MSK) ==
	       (RXSF_HWDONE << RX_STAT_CTL_OWNER_SHF)) {
		/* Check the rx status. */
		if (rx_status & (RX_STATUS_GOOD << RX_STAT_CTL_STATUS_SHF)) {
			/* Received packet is good. */
			len = (rx_status & RX_STAT_CTL_LENGTH_MSK) >>
			    RX_STAT_CTL_LENGTH_SHF;

			pData = (unsigned char *) pPacket;
			pData += 4;
			skb = dev_alloc_skb(len + 2);
			if (skb == 0) {
				printk
				    ("%s: Memory squeeze, deferring packet.\n",
				     dev->name);
692
				dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
693
			} else {
694 695
				dev->stats.rx_bytes += len;
				dev->stats.rx_packets++;
L
Linus Torvalds 已提交
696 697
				skb_reserve(skb, 2);	/* 16 byte align */
				skb_put(skb, len);	/* make room */
698
				skb_copy_to_linear_data(skb,
L
Linus Torvalds 已提交
699
						 (unsigned char *) pData,
700
						 len);
L
Linus Torvalds 已提交
701 702 703 704 705 706 707 708 709 710 711
				skb->protocol = eth_type_trans(skb, dev);
				netif_rx(skb);
				dev->last_rx = jiffies;
			}
		} else {
			/* We got an error packet. */
			if (lan_saa9730_debug > 2)
				printk
				    ("lan_saa9730_rx: We got an error packet = %x\n",
				     rx_status);

712
			dev->stats.rx_errors++;
L
Linus Torvalds 已提交
713 714
			if (rx_status &
			    (RX_STATUS_CRC_ERR << RX_STAT_CTL_STATUS_SHF))
715
				dev->stats.rx_crc_errors++;
L
Linus Torvalds 已提交
716
			if (rx_status &
R
Ralf Baechle 已提交
717
			    (RX_STATUS_ALIGN_ERR << RX_STAT_CTL_STATUS_SHF))
718
				dev->stats.rx_frame_errors++;
L
Linus Torvalds 已提交
719 720
			if (rx_status &
			    (RX_STATUS_OVERFLOW << RX_STAT_CTL_STATUS_SHF))
721
				dev->stats.rx_fifo_errors++;
L
Linus Torvalds 已提交
722 723
			if (rx_status &
			    (RX_STATUS_LONG_ERR << RX_STAT_CTL_STATUS_SHF))
724
				dev->stats.rx_length_errors++;
L
Linus Torvalds 已提交
725 726 727
		}

		/* Indicate we have processed the buffer. */
R
Ralf Baechle 已提交
728 729 730
		*pPacket = cpu_to_le32(RXSF_READY << RX_STAT_CTL_OWNER_SHF);

		/* Make sure A or B is available to hardware as appropriate. */
731 732
		writel(BufferIndex ? OK2USE_RX_B : OK2USE_RX_A,
		       &lp->lan_saa9730_regs->Ok2Use);
L
Linus Torvalds 已提交
733 734 735 736 737

		/* Go to next packet in sequence. */
		lp->NextRcvPacketIndex++;
		if (lp->NextRcvPacketIndex >= LAN_SAA9730_RCV_Q_SIZE) {
			lp->NextRcvPacketIndex = 0;
R
Ralf Baechle 已提交
738
			lp->NextRcvBufferIndex ^= 1;
L
Linus Torvalds 已提交
739 740 741
		}

		/* Address next packet */
R
Ralf Baechle 已提交
742
		BufferIndex = lp->NextRcvBufferIndex;
L
Linus Torvalds 已提交
743
		PacketIndex = lp->NextRcvPacketIndex;
R
Ralf Baechle 已提交
744
		pPacket = lp->RcvBuffer[BufferIndex][PacketIndex];
L
Linus Torvalds 已提交
745 746 747 748 749 750
		rx_status = le32_to_cpu(*pPacket);
	}

	return 0;
}

751
static irqreturn_t lan_saa9730_interrupt(const int irq, void *dev_id)
L
Linus Torvalds 已提交
752
{
753
	struct net_device *dev = dev_id;
R
Ralf Baechle 已提交
754
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
755 756 757 758 759 760 761 762 763 764 765

	if (lan_saa9730_debug > 5)
		printk("lan_saa9730_interrupt\n");

	/* Disable the EVM LAN interrupt. */
	evm_saa9730_block_lan_int(lp);

	/* Clear the EVM LAN interrupt. */
	evm_saa9730_clear_lan_int(lp);

	/* Service pending transmit interrupts. */
R
Ralf Baechle 已提交
766
	if (readl(&lp->lan_saa9730_regs->DmaStatus) & DMA_STATUS_MAC_TX_INT)
L
Linus Torvalds 已提交
767 768 769
		lan_saa9730_tx(dev);

	/* Service pending receive interrupts. */
R
Ralf Baechle 已提交
770
	if (readl(&lp->lan_saa9730_regs->DmaStatus) &
L
Linus Torvalds 已提交
771 772 773 774 775 776 777 778 779 780 781
	    (DMA_STATUS_MAC_RX_INT | DMA_STATUS_RX_INT |
	     DMA_STATUS_RX_TO_INT)) lan_saa9730_rx(dev);

	/* Enable the EVM LAN interrupt. */
	evm_saa9730_unblock_lan_int(lp);

	return IRQ_HANDLED;
}

static int lan_saa9730_open(struct net_device *dev)
{
R
Ralf Baechle 已提交
782
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

	/* Associate IRQ with lan_saa9730_interrupt */
	if (request_irq(dev->irq, &lan_saa9730_interrupt, 0, "SAA9730 Eth",
			dev)) {
		printk("lan_saa9730_open: Can't get irq %d\n", dev->irq);
		return -EAGAIN;
	}

	/* Enable the Lan interrupt in the event manager. */
	evm_saa9730_enable_lan_int(lp);

	/* Start the LAN controller */
	if (lan_saa9730_start(lp))
		return -1;

	netif_start_queue(dev);

	return 0;
}

static int lan_saa9730_write(struct lan_saa9730_private *lp,
			     struct sk_buff *skb, int skblen)
{
	unsigned char *pbData = skb->data;
	unsigned int len = skblen;
	unsigned char *pbPacketData;
	unsigned int tx_status;
	int BufferIndex;
	int PacketIndex;

	if (lan_saa9730_debug > 5)
R
Ralf Baechle 已提交
814
		printk("lan_saa9730_write: skb=%p\n", skb);
L
Linus Torvalds 已提交
815 816 817 818

	BufferIndex = lp->NextTxmBufferIndex;
	PacketIndex = lp->NextTxmPacketIndex;

R
Ralf Baechle 已提交
819 820
	tx_status = le32_to_cpu(*(unsigned int *)lp->TxmBuffer[BufferIndex]
							      [PacketIndex]);
L
Linus Torvalds 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
	if ((tx_status & TX_STAT_CTL_OWNER_MSK) !=
	    (TXSF_EMPTY << TX_STAT_CTL_OWNER_SHF)) {
		if (lan_saa9730_debug > 4)
			printk
			    ("lan_saa9730_write: Tx buffer not available: tx_status = %x\n",
			     tx_status);
		return -1;
	}

	lp->NextTxmPacketIndex++;
	if (lp->NextTxmPacketIndex >= LAN_SAA9730_TXM_Q_SIZE) {
		lp->NextTxmPacketIndex = 0;
		lp->NextTxmBufferIndex ^= 1;
	}

R
Ralf Baechle 已提交
836
	pbPacketData = lp->TxmBuffer[BufferIndex][PacketIndex];
L
Linus Torvalds 已提交
837 838 839 840 841 842
	pbPacketData += 4;

	/* copy the bits */
	memcpy(pbPacketData, pbData, len);

	/* Set transmit status for hardware */
R
Ralf Baechle 已提交
843 844 845 846 847 848 849
	*(unsigned int *)lp->TxmBuffer[BufferIndex][PacketIndex] =
		cpu_to_le32((TXSF_READY << TX_STAT_CTL_OWNER_SHF) |
			    (TX_STAT_CTL_INT_AFTER_TX <<
			     TX_STAT_CTL_FRAME_SHF) |
			    (len << TX_STAT_CTL_LENGTH_SHF));

	/* Make sure A or B is available to hardware as appropriate. */
850 851
	writel(BufferIndex ? OK2USE_TX_B : OK2USE_TX_A,
	       &lp->lan_saa9730_regs->Ok2Use);
L
Linus Torvalds 已提交
852 853 854 855 856 857

	return 0;
}

static void lan_saa9730_tx_timeout(struct net_device *dev)
{
R
Ralf Baechle 已提交
858
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
859 860

	/* Transmitter timeout, serious problems */
861
	dev->stats.tx_errors++;
L
Linus Torvalds 已提交
862
	printk("%s: transmit timed out, reset\n", dev->name);
R
Ralf Baechle 已提交
863
	/*show_saa9730_regs(dev); */
L
Linus Torvalds 已提交
864 865 866
	lan_saa9730_restart(lp);

	dev->trans_start = jiffies;
R
Ralf Baechle 已提交
867
	netif_wake_queue(dev);
L
Linus Torvalds 已提交
868 869 870 871 872
}

static int lan_saa9730_start_xmit(struct sk_buff *skb,
				  struct net_device *dev)
{
R
Ralf Baechle 已提交
873
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
874 875 876 877 878
	unsigned long flags;
	int skblen;
	int len;

	if (lan_saa9730_debug > 4)
R
Ralf Baechle 已提交
879
		printk("Send packet: skb=%p\n", skb);
L
Linus Torvalds 已提交
880 881 882 883 884 885 886 887 888

	skblen = skb->len;

	spin_lock_irqsave(&lp->lock, flags);

	len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;

	if (lan_saa9730_write(lp, skb, skblen)) {
		spin_unlock_irqrestore(&lp->lock, flags);
R
Ralf Baechle 已提交
889
		printk("Error when writing packet to controller: skb=%p\n", skb);
L
Linus Torvalds 已提交
890 891 892 893
		netif_stop_queue(dev);
		return -1;
	}

894 895
	dev->stats.tx_bytes += len;
	dev->stats.tx_packets++;
L
Linus Torvalds 已提交
896 897

	dev->trans_start = jiffies;
R
Ralf Baechle 已提交
898
	netif_wake_queue(dev);
L
Linus Torvalds 已提交
899 900 901 902 903 904 905 906 907
	dev_kfree_skb(skb);

	spin_unlock_irqrestore(&lp->lock, flags);

	return 0;
}

static int lan_saa9730_close(struct net_device *dev)
{
R
Ralf Baechle 已提交
908
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928

	if (lan_saa9730_debug > 1)
		printk("lan_saa9730_close:\n");

	netif_stop_queue(dev);

	/* Disable the Lan interrupt in the event manager. */
	evm_saa9730_disable_lan_int(lp);

	/* Stop the controller */
	if (lan_saa9730_stop(lp))
		return -1;

	free_irq(dev->irq, (void *) dev);

	return 0;
}

static void lan_saa9730_set_multicast(struct net_device *dev)
{
R
Ralf Baechle 已提交
929
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
930 931 932 933 934 935

	/* Stop the controller */
	lan_saa9730_stop(lp);

	if (dev->flags & IFF_PROMISC) {
		/* accept all packets */
936 937 938
		writel(CAM_CONTROL_COMP_EN | CAM_CONTROL_STATION_ACC |
		       CAM_CONTROL_GROUP_ACC | CAM_CONTROL_BROAD_ACC,
		       &lp->lan_saa9730_regs->CamCtl);
L
Linus Torvalds 已提交
939
	} else {
940
		if (dev->flags & IFF_ALLMULTI || dev->mc_count) {
L
Linus Torvalds 已提交
941
			/* accept all multicast packets */
942
			/*
L
Linus Torvalds 已提交
943 944
			 * Will handle the multicast stuff later. -carstenl
			 */
945 946 947
			writel(CAM_CONTROL_COMP_EN | CAM_CONTROL_GROUP_ACC |
			       CAM_CONTROL_BROAD_ACC,
			       &lp->lan_saa9730_regs->CamCtl);
L
Linus Torvalds 已提交
948 949 950 951 952 953 954 955 956
		}
	}

	lan_saa9730_restart(lp);
}


static void __devexit saa9730_remove_one(struct pci_dev *pdev)
{
957
	struct net_device *dev = pci_get_drvdata(pdev);
R
Ralf Baechle 已提交
958
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
959

960 961
	if (dev) {
		unregister_netdev(dev);
R
Ralf Baechle 已提交
962 963 964
		lan_saa9730_free_buffers(pdev, lp);
		iounmap(lp->lan_saa9730_regs);
		iounmap(lp->evm_saa9730_regs);
965 966 967 968 969
		free_netdev(dev);
		pci_release_regions(pdev);
		pci_disable_device(pdev);
		pci_set_drvdata(pdev, NULL);
	}
L
Linus Torvalds 已提交
970 971 972
}


R
Ralf Baechle 已提交
973 974
static int lan_saa9730_init(struct net_device *dev, struct pci_dev *pdev,
	unsigned long ioaddr, int irq)
L
Linus Torvalds 已提交
975
{
R
Ralf Baechle 已提交
976
	struct lan_saa9730_private *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
977
	unsigned char ethernet_addr[6];
R
Ralf Baechle 已提交
978
	int ret;
L
Linus Torvalds 已提交
979

R
Ralf Baechle 已提交
980 981 982 983
	if (get_ethernet_addr(ethernet_addr)) {
		ret = -ENODEV;
		goto out;
	}
984

L
Linus Torvalds 已提交
985 986 987
	memcpy(dev->dev_addr, ethernet_addr, 6);
	dev->base_addr = ioaddr;
	dev->irq = irq;
988

R
Ralf Baechle 已提交
989
	lp->pci_dev = pdev;
L
Linus Torvalds 已提交
990 991

	/* Set SAA9730 LAN base address. */
R
Ralf Baechle 已提交
992 993 994 995 996 997
	lp->lan_saa9730_regs = ioremap(ioaddr + SAA9730_LAN_REGS_ADDR,
				       SAA9730_LAN_REGS_SIZE);
	if (!lp->lan_saa9730_regs) {
		ret = -ENOMEM;
		goto out;
	}
L
Linus Torvalds 已提交
998 999

	/* Set SAA9730 EVM base address. */
R
Ralf Baechle 已提交
1000 1001 1002 1003 1004 1005
	lp->evm_saa9730_regs = ioremap(ioaddr + SAA9730_EVM_REGS_ADDR,
				       SAA9730_EVM_REGS_SIZE);
	if (!lp->evm_saa9730_regs) {
		ret = -ENOMEM;
		goto out_iounmap_lan;
	}
L
Linus Torvalds 已提交
1006 1007

	/* Allocate LAN RX/TX frame buffer space. */
R
Ralf Baechle 已提交
1008 1009
	if ((ret = lan_saa9730_allocate_buffers(pdev, lp)))
		goto out_iounmap;
L
Linus Torvalds 已提交
1010 1011

	/* Stop LAN controller. */
1012
	if ((ret = lan_saa9730_stop(lp)))
R
Ralf Baechle 已提交
1013
		goto out_free_consistent;
1014

L
Linus Torvalds 已提交
1015 1016
	/* Initialize CAM registers. */
	if ((ret = lan_saa9730_cam_init(dev)))
R
Ralf Baechle 已提交
1017
		goto out_free_consistent;
L
Linus Torvalds 已提交
1018 1019 1020

	/* Initialize MII registers. */
	if ((ret = lan_saa9730_mii_init(lp)))
R
Ralf Baechle 已提交
1021
		goto out_free_consistent;
L
Linus Torvalds 已提交
1022 1023

	/* Initialize control registers. */
1024
	if ((ret = lan_saa9730_control_init(lp)))
R
Ralf Baechle 已提交
1025
		goto out_free_consistent;
1026

L
Linus Torvalds 已提交
1027
	/* Load CAM registers. */
1028
	if ((ret = lan_saa9730_cam_load(lp)))
R
Ralf Baechle 已提交
1029
		goto out_free_consistent;
1030

L
Linus Torvalds 已提交
1031 1032
	/* Initialize DMA context registers. */
	if ((ret = lan_saa9730_dma_init(lp)))
R
Ralf Baechle 已提交
1033
		goto out_free_consistent;
1034

L
Linus Torvalds 已提交
1035
	spin_lock_init(&lp->lock);
1036

L
Linus Torvalds 已提交
1037 1038 1039 1040 1041 1042 1043
	dev->open = lan_saa9730_open;
	dev->hard_start_xmit = lan_saa9730_start_xmit;
	dev->stop = lan_saa9730_close;
	dev->set_multicast_list = lan_saa9730_set_multicast;
	dev->tx_timeout = lan_saa9730_tx_timeout;
	dev->watchdog_timeo = (HZ >> 1);
	dev->dma = 0;
1044

R
Ralf Baechle 已提交
1045
	ret = register_netdev (dev);
L
Linus Torvalds 已提交
1046
	if (ret)
R
Ralf Baechle 已提交
1047 1048
		goto out_free_consistent;

L
Linus Torvalds 已提交
1049 1050
	return 0;

R
Ralf Baechle 已提交
1051 1052 1053 1054 1055 1056 1057
out_free_consistent:
	lan_saa9730_free_buffers(pdev, lp);
out_iounmap:
	iounmap(lp->evm_saa9730_regs);
out_iounmap_lan:
	iounmap(lp->lan_saa9730_regs);
out:
L
Linus Torvalds 已提交
1058 1059 1060 1061 1062 1063
	return ret;
}


static int __devinit saa9730_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
{
R
Ralf Baechle 已提交
1064 1065
	struct net_device *dev = NULL;
	unsigned long pci_ioaddr;
L
Linus Torvalds 已提交
1066 1067 1068 1069 1070 1071
	int err;

	if (lan_saa9730_debug > 1)
		printk("saa9730.c: PCI bios is present, checking for devices...\n");

	err = pci_enable_device(pdev);
1072 1073
	if (err) {
		printk(KERN_ERR "Cannot enable PCI device, aborting.\n");
R
Ralf Baechle 已提交
1074
		goto out;
1075
	}
L
Linus Torvalds 已提交
1076 1077 1078 1079

	err = pci_request_regions(pdev, DRV_MODULE_NAME);
	if (err) {
		printk(KERN_ERR "Cannot obtain PCI resources, aborting.\n");
R
Ralf Baechle 已提交
1080
		goto out_disable_pdev;
L
Linus Torvalds 已提交
1081 1082 1083 1084 1085 1086 1087 1088
	}

	pci_irq_line = pdev->irq;
	/* LAN base address in located at BAR 1. */

	pci_ioaddr = pci_resource_start(pdev, 1);
	pci_set_master(pdev);

R
Ralf Baechle 已提交
1089
	printk("Found SAA9730 (PCI) at %lx, irq %d.\n",
L
Linus Torvalds 已提交
1090 1091
	       pci_ioaddr, pci_irq_line);

R
Ralf Baechle 已提交
1092 1093 1094 1095 1096
	dev = alloc_etherdev(sizeof(struct lan_saa9730_private));
	if (!dev)
		goto out_disable_pdev;

	err = lan_saa9730_init(dev, pdev, pci_ioaddr, pci_irq_line);
L
Linus Torvalds 已提交
1097
	if (err) {
R
Ralf Baechle 已提交
1098 1099
		printk("LAN init failed");
		goto out_free_netdev;
L
Linus Torvalds 已提交
1100 1101 1102 1103 1104
	}

	pci_set_drvdata(pdev, dev);
	SET_NETDEV_DEV(dev, &pdev->dev);
	return 0;
1105

R
Ralf Baechle 已提交
1106
out_free_netdev:
L
Linus Torvalds 已提交
1107
	free_netdev(dev);
R
Ralf Baechle 已提交
1108 1109
out_disable_pdev:
	pci_disable_device(pdev);
L
Linus Torvalds 已提交
1110
out:
R
Ralf Baechle 已提交
1111
	pci_set_drvdata(pdev, NULL);
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116
	return err;
}


static struct pci_driver saa9730_driver = {
1117 1118 1119 1120
	.name		= DRV_MODULE_NAME,
	.id_table	= saa9730_pci_tbl,
	.probe		= saa9730_init_one,
	.remove		= __devexit_p(saa9730_remove_one),
L
Linus Torvalds 已提交
1121 1122 1123 1124 1125
};


static int __init saa9730_init(void)
{
1126
	return pci_register_driver(&saa9730_driver);
L
Linus Torvalds 已提交
1127 1128 1129 1130
}

static void __exit saa9730_cleanup(void)
{
1131
	pci_unregister_driver(&saa9730_driver);
L
Linus Torvalds 已提交
1132 1133 1134 1135 1136
}

module_init(saa9730_init);
module_exit(saa9730_cleanup);

R
Ralf Baechle 已提交
1137 1138
MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
MODULE_DESCRIPTION("Philips SAA9730 ethernet driver");
L
Linus Torvalds 已提交
1139
MODULE_LICENSE("GPL");