blob: c65199df8a7f54aac9b4268c0aa640771267d06c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ralf Baechle05d9c842005-11-09 17:10:05 +00002 * Copyright (C) 2000, 2005 MIPS Technologies, Inc. All rights reserved.
3 * Authors: Carsten Langgaard <carstenl@mips.com>
4 * Maciej W. Rozycki <macro@mips.com>
5 * Copyright (C) 2004 Ralf Baechle <ralf@linux-mips.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can distribute it and/or modify it
8 * under the terms of the GNU General Public License (Version 2) as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
19 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * SAA9730 ethernet driver.
21 *
22 * Changes:
Ralf Baechle05d9c842005-11-09 17:10:05 +000023 * Angelo Dell'Aera <buffer@antifork.org> : Conversion to the new PCI API
24 * (pci_driver).
25 * Conversion to spinlocks.
26 * Error handling fixes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/init.h>
30#include <linux/netdevice.h>
31#include <linux/delay.h>
32#include <linux/etherdevice.h>
33#include <linux/module.h>
34#include <linux/skbuff.h>
35#include <linux/pci.h>
36#include <linux/spinlock.h>
Ralf Baechle05d9c842005-11-09 17:10:05 +000037#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/addrspace.h>
Ralf Baechle05d9c842005-11-09 17:10:05 +000040#include <asm/io.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/mips-boards/prom.h>
43
44#include "saa9730.h"
45
46#ifdef LAN_SAA9730_DEBUG
47int lan_saa9730_debug = LAN_SAA9730_DEBUG;
48#else
49int lan_saa9730_debug;
50#endif
51
52#define DRV_MODULE_NAME "saa9730"
53
54static struct pci_device_id saa9730_pci_tbl[] = {
Ralf Baechle05d9c842005-11-09 17:10:05 +000055 { PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA9730,
Ralf Baechle62ff0d02005-11-09 16:44:02 +000056 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 { 0, }
58};
59
60MODULE_DEVICE_TABLE(pci, saa9730_pci_tbl);
61
62/* Non-zero only if the current card is a PCI with BIOS-set IRQ. */
63static unsigned int pci_irq_line;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void evm_saa9730_enable_lan_int(struct lan_saa9730_private *lp)
66{
Ralf Baechle69a43ac2007-03-20 12:40:09 +000067 writel(readl(&lp->evm_saa9730_regs->InterruptBlock1) | EVM_LAN_INT,
68 &lp->evm_saa9730_regs->InterruptBlock1);
69 writel(readl(&lp->evm_saa9730_regs->InterruptStatus1) | EVM_LAN_INT,
70 &lp->evm_saa9730_regs->InterruptStatus1);
71 writel(readl(&lp->evm_saa9730_regs->InterruptEnable1) | EVM_LAN_INT |
72 EVM_MASTER_EN, &lp->evm_saa9730_regs->InterruptEnable1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
Ralf Baechle05d9c842005-11-09 17:10:05 +000074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static void evm_saa9730_disable_lan_int(struct lan_saa9730_private *lp)
76{
Ralf Baechle69a43ac2007-03-20 12:40:09 +000077 writel(readl(&lp->evm_saa9730_regs->InterruptBlock1) & ~EVM_LAN_INT,
78 &lp->evm_saa9730_regs->InterruptBlock1);
79 writel(readl(&lp->evm_saa9730_regs->InterruptEnable1) & ~EVM_LAN_INT,
80 &lp->evm_saa9730_regs->InterruptEnable1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83static void evm_saa9730_clear_lan_int(struct lan_saa9730_private *lp)
84{
Ralf Baechle69a43ac2007-03-20 12:40:09 +000085 writel(EVM_LAN_INT, &lp->evm_saa9730_regs->InterruptStatus1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88static void evm_saa9730_block_lan_int(struct lan_saa9730_private *lp)
89{
Ralf Baechle69a43ac2007-03-20 12:40:09 +000090 writel(readl(&lp->evm_saa9730_regs->InterruptBlock1) & ~EVM_LAN_INT,
91 &lp->evm_saa9730_regs->InterruptBlock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94static void evm_saa9730_unblock_lan_int(struct lan_saa9730_private *lp)
95{
Ralf Baechle69a43ac2007-03-20 12:40:09 +000096 writel(readl(&lp->evm_saa9730_regs->InterruptBlock1) | EVM_LAN_INT,
97 &lp->evm_saa9730_regs->InterruptBlock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Ralf Baechleb77eb352007-10-14 14:13:58 +0100100static void __used show_saa9730_regs(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Ralf Baechleb77eb352007-10-14 14:13:58 +0100102 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int i, j;
Ralf Baechleb77eb352007-10-14 14:13:58 +0100104
Ralf Baechle05d9c842005-11-09 17:10:05 +0000105 printk("TxmBufferA = %p\n", lp->TxmBuffer[0][0]);
106 printk("TxmBufferB = %p\n", lp->TxmBuffer[1][0]);
107 printk("RcvBufferA = %p\n", lp->RcvBuffer[0][0]);
108 printk("RcvBufferB = %p\n", lp->RcvBuffer[1][0]);
Ralf Baechleb77eb352007-10-14 14:13:58 +0100109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
111 for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
112 printk("TxmBuffer[%d][%d] = %x\n", i, j,
113 le32_to_cpu(*(unsigned int *)
114 lp->TxmBuffer[i][j]));
115 }
116 }
117 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
118 for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
119 printk("RcvBuffer[%d][%d] = %x\n", i, j,
120 le32_to_cpu(*(unsigned int *)
121 lp->RcvBuffer[i][j]));
122 }
123 }
124 printk("lp->evm_saa9730_regs->InterruptBlock1 = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000125 readl(&lp->evm_saa9730_regs->InterruptBlock1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 printk("lp->evm_saa9730_regs->InterruptStatus1 = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000127 readl(&lp->evm_saa9730_regs->InterruptStatus1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 printk("lp->evm_saa9730_regs->InterruptEnable1 = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000129 readl(&lp->evm_saa9730_regs->InterruptEnable1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 printk("lp->lan_saa9730_regs->Ok2Use = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000131 readl(&lp->lan_saa9730_regs->Ok2Use));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 printk("lp->NextTxmBufferIndex = %x\n", lp->NextTxmBufferIndex);
133 printk("lp->NextTxmPacketIndex = %x\n", lp->NextTxmPacketIndex);
134 printk("lp->PendingTxmBufferIndex = %x\n",
135 lp->PendingTxmBufferIndex);
136 printk("lp->PendingTxmPacketIndex = %x\n",
137 lp->PendingTxmPacketIndex);
138 printk("lp->lan_saa9730_regs->LanDmaCtl = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000139 readl(&lp->lan_saa9730_regs->LanDmaCtl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 printk("lp->lan_saa9730_regs->DmaStatus = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000141 readl(&lp->lan_saa9730_regs->DmaStatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 printk("lp->lan_saa9730_regs->CamCtl = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000143 readl(&lp->lan_saa9730_regs->CamCtl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 printk("lp->lan_saa9730_regs->TxCtl = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000145 readl(&lp->lan_saa9730_regs->TxCtl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 printk("lp->lan_saa9730_regs->TxStatus = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000147 readl(&lp->lan_saa9730_regs->TxStatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 printk("lp->lan_saa9730_regs->RxCtl = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000149 readl(&lp->lan_saa9730_regs->RxCtl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 printk("lp->lan_saa9730_regs->RxStatus = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000151 readl(&lp->lan_saa9730_regs->RxStatus));
Ralf Baechleb77eb352007-10-14 14:13:58 +0100152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 for (i = 0; i < LAN_SAA9730_CAM_DWORDS; i++) {
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000154 writel(i, &lp->lan_saa9730_regs->CamAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 printk("lp->lan_saa9730_regs->CamData = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000156 readl(&lp->lan_saa9730_regs->CamData));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
Ralf Baechleb77eb352007-10-14 14:13:58 +0100158
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700159 printk("dev->stats.tx_packets = %lx\n", dev->stats.tx_packets);
160 printk("dev->stats.tx_errors = %lx\n", dev->stats.tx_errors);
161 printk("dev->stats.tx_aborted_errors = %lx\n",
162 dev->stats.tx_aborted_errors);
163 printk("dev->stats.tx_window_errors = %lx\n",
164 dev->stats.tx_window_errors);
165 printk("dev->stats.tx_carrier_errors = %lx\n",
166 dev->stats.tx_carrier_errors);
167 printk("dev->stats.tx_fifo_errors = %lx\n",
168 dev->stats.tx_fifo_errors);
169 printk("dev->stats.tx_heartbeat_errors = %lx\n",
170 dev->stats.tx_heartbeat_errors);
171 printk("dev->stats.collisions = %lx\n", dev->stats.collisions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700173 printk("dev->stats.rx_packets = %lx\n", dev->stats.rx_packets);
174 printk("dev->stats.rx_errors = %lx\n", dev->stats.rx_errors);
175 printk("dev->stats.rx_dropped = %lx\n", dev->stats.rx_dropped);
176 printk("dev->stats.rx_crc_errors = %lx\n", dev->stats.rx_crc_errors);
177 printk("dev->stats.rx_frame_errors = %lx\n",
178 dev->stats.rx_frame_errors);
179 printk("dev->stats.rx_fifo_errors = %lx\n",
180 dev->stats.rx_fifo_errors);
181 printk("dev->stats.rx_length_errors = %lx\n",
182 dev->stats.rx_length_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 printk("lp->lan_saa9730_regs->DebugPCIMasterAddr = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000185 readl(&lp->lan_saa9730_regs->DebugPCIMasterAddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 printk("lp->lan_saa9730_regs->DebugLanTxStateMachine = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000187 readl(&lp->lan_saa9730_regs->DebugLanTxStateMachine));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 printk("lp->lan_saa9730_regs->DebugLanRxStateMachine = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000189 readl(&lp->lan_saa9730_regs->DebugLanRxStateMachine));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 printk("lp->lan_saa9730_regs->DebugLanTxFifoPointers = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000191 readl(&lp->lan_saa9730_regs->DebugLanTxFifoPointers));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 printk("lp->lan_saa9730_regs->DebugLanRxFifoPointers = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000193 readl(&lp->lan_saa9730_regs->DebugLanRxFifoPointers));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printk("lp->lan_saa9730_regs->DebugLanCtlStateMachine = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000195 readl(&lp->lan_saa9730_regs->DebugLanCtlStateMachine));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198static void lan_saa9730_buffer_init(struct lan_saa9730_private *lp)
199{
200 int i, j;
201
202 /* Init RX buffers */
203 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
204 for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
205 *(unsigned int *) lp->RcvBuffer[i][j] =
206 cpu_to_le32(RXSF_READY <<
207 RX_STAT_CTL_OWNER_SHF);
208 }
209 }
210
211 /* Init TX buffers */
212 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
213 for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
214 *(unsigned int *) lp->TxmBuffer[i][j] =
215 cpu_to_le32(TXSF_EMPTY <<
216 TX_STAT_CTL_OWNER_SHF);
217 }
218 }
219}
220
Ralf Baechle05d9c842005-11-09 17:10:05 +0000221static void lan_saa9730_free_buffers(struct pci_dev *pdev,
222 struct lan_saa9730_private *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000224 pci_free_consistent(pdev, lp->buffer_size, lp->buffer_start,
225 lp->dma_addr);
226}
227
228static int lan_saa9730_allocate_buffers(struct pci_dev *pdev,
229 struct lan_saa9730_private *lp)
230{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 void *Pa;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000232 unsigned int i, j, rxoffset, txoffset;
233 int ret;
234
235 /* Initialize buffer space */
236 lp->DmaRcvPackets = LAN_SAA9730_RCV_Q_SIZE;
237 lp->DmaTxmPackets = LAN_SAA9730_TXM_Q_SIZE;
238
239 /* Initialize Rx Buffer Index */
240 lp->NextRcvPacketIndex = 0;
241 lp->NextRcvBufferIndex = 0;
242
243 /* Set current buffer index & next available packet index */
244 lp->NextTxmPacketIndex = 0;
245 lp->NextTxmBufferIndex = 0;
246 lp->PendingTxmPacketIndex = 0;
247 lp->PendingTxmBufferIndex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000249 /*
250 * Allocate all RX and TX packets in one chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * The Rx and Tx packets must be PACKET_SIZE aligned.
252 */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000253 lp->buffer_size = ((LAN_SAA9730_RCV_Q_SIZE + LAN_SAA9730_TXM_Q_SIZE) *
254 LAN_SAA9730_PACKET_SIZE * LAN_SAA9730_BUFFERS) +
255 LAN_SAA9730_PACKET_SIZE;
256 lp->buffer_start = pci_alloc_consistent(pdev, lp->buffer_size,
257 &lp->dma_addr);
258 if (!lp->buffer_start) {
259 ret = -ENOMEM;
260 goto out;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Ralf Baechle05d9c842005-11-09 17:10:05 +0000263 Pa = (void *)ALIGN((unsigned long)lp->buffer_start,
264 LAN_SAA9730_PACKET_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Ralf Baechle05d9c842005-11-09 17:10:05 +0000266 rxoffset = Pa - lp->buffer_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /* Init RX buffers */
269 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
270 for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
271 *(unsigned int *) Pa =
272 cpu_to_le32(RXSF_READY <<
273 RX_STAT_CTL_OWNER_SHF);
Ralf Baechle05d9c842005-11-09 17:10:05 +0000274 lp->RcvBuffer[i][j] = Pa;
275 Pa += LAN_SAA9730_PACKET_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 }
278
Ralf Baechle05d9c842005-11-09 17:10:05 +0000279 txoffset = Pa - lp->buffer_start;
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* Init TX buffers */
282 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
283 for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
284 *(unsigned int *) Pa =
285 cpu_to_le32(TXSF_EMPTY <<
286 TX_STAT_CTL_OWNER_SHF);
Ralf Baechle05d9c842005-11-09 17:10:05 +0000287 lp->TxmBuffer[i][j] = Pa;
288 Pa += LAN_SAA9730_PACKET_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 }
291
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000292 /*
293 * Set rx buffer A and rx buffer B to point to the first two buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * spaces.
295 */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000296 writel(lp->dma_addr + rxoffset, &lp->lan_saa9730_regs->RxBuffA);
297 writel(lp->dma_addr + rxoffset +
298 LAN_SAA9730_PACKET_SIZE * LAN_SAA9730_RCV_Q_SIZE,
299 &lp->lan_saa9730_regs->RxBuffB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000301 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * Set txm_buf_a and txm_buf_b to point to the first two buffer
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000303 * space
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000305 writel(lp->dma_addr + txoffset,
306 &lp->lan_saa9730_regs->TxBuffA);
307 writel(lp->dma_addr + txoffset +
308 LAN_SAA9730_PACKET_SIZE * LAN_SAA9730_TXM_Q_SIZE,
309 &lp->lan_saa9730_regs->TxBuffB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* Set packet number */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000312 writel((lp->DmaRcvPackets << PK_COUNT_RX_A_SHF) |
313 (lp->DmaRcvPackets << PK_COUNT_RX_B_SHF) |
314 (lp->DmaTxmPackets << PK_COUNT_TX_A_SHF) |
315 (lp->DmaTxmPackets << PK_COUNT_TX_B_SHF),
316 &lp->lan_saa9730_regs->PacketCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 return 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000319
320out:
321 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324static int lan_saa9730_cam_load(struct lan_saa9730_private *lp)
325{
326 unsigned int i;
327 unsigned char *NetworkAddress;
328
329 NetworkAddress = (unsigned char *) &lp->PhysicalAddress[0][0];
330
331 for (i = 0; i < LAN_SAA9730_CAM_DWORDS; i++) {
332 /* First set address to where data is written */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000333 writel(i, &lp->lan_saa9730_regs->CamAddress);
334 writel((NetworkAddress[0] << 24) | (NetworkAddress[1] << 16) |
335 (NetworkAddress[2] << 8) | NetworkAddress[3],
336 &lp->lan_saa9730_regs->CamData);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 NetworkAddress += 4;
338 }
339 return 0;
340}
341
342static int lan_saa9730_cam_init(struct net_device *dev)
343{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000344 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 unsigned int i;
346
347 /* Copy MAC-address into all entries. */
348 for (i = 0; i < LAN_SAA9730_CAM_ENTRIES; i++) {
349 memcpy((unsigned char *) lp->PhysicalAddress[i],
350 (unsigned char *) dev->dev_addr, 6);
351 }
352
353 return 0;
354}
355
356static int lan_saa9730_mii_init(struct lan_saa9730_private *lp)
357{
358 int i, l;
359
360 /* Check link status, spin here till station is not busy. */
361 i = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000362 while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) & MD_CA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 i++;
364 if (i > 100) {
365 printk("Error: lan_saa9730_mii_init: timeout\n");
366 return -1;
367 }
368 mdelay(1); /* wait 1 ms. */
369 }
370
371 /* Now set the control and address register. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000372 writel(MD_CA_BUSY | PHY_STATUS | PHY_ADDRESS << MD_CA_PHY_SHF,
373 &lp->lan_saa9730_regs->StationMgmtCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* check link status, spin here till station is not busy */
376 i = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000377 while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) & MD_CA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 i++;
379 if (i > 100) {
380 printk("Error: lan_saa9730_mii_init: timeout\n");
381 return -1;
382 }
383 mdelay(1); /* wait 1 ms. */
384 }
385
386 /* Wait for 1 ms. */
387 mdelay(1);
388
389 /* Check the link status. */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000390 if (readl(&lp->lan_saa9730_regs->StationMgmtData) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 PHY_STATUS_LINK_UP) {
392 /* Link is up. */
393 return 0;
394 } else {
395 /* Link is down, reset the PHY first. */
396
397 /* set PHY address = 'CONTROL' */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000398 writel(PHY_ADDRESS << MD_CA_PHY_SHF | MD_CA_WR | PHY_CONTROL,
399 &lp->lan_saa9730_regs->StationMgmtCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* Wait for 1 ms. */
402 mdelay(1);
403
404 /* set 'CONTROL' = force reset and renegotiate */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000405 writel(PHY_CONTROL_RESET | PHY_CONTROL_AUTO_NEG |
406 PHY_CONTROL_RESTART_AUTO_NEG,
407 &lp->lan_saa9730_regs->StationMgmtData);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* Wait for 50 ms. */
410 mdelay(50);
411
412 /* set 'BUSY' to start operation */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000413 writel(MD_CA_BUSY | PHY_ADDRESS << MD_CA_PHY_SHF | MD_CA_WR |
414 PHY_CONTROL, &lp->lan_saa9730_regs->StationMgmtCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* await completion */
417 i = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000418 while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 MD_CA_BUSY) {
420 i++;
421 if (i > 100) {
422 printk
423 ("Error: lan_saa9730_mii_init: timeout\n");
424 return -1;
425 }
426 mdelay(1); /* wait 1 ms. */
427 }
428
429 /* Wait for 1 ms. */
430 mdelay(1);
431
432 for (l = 0; l < 2; l++) {
433 /* set PHY address = 'STATUS' */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000434 writel(MD_CA_BUSY | PHY_ADDRESS << MD_CA_PHY_SHF |
435 PHY_STATUS,
436 &lp->lan_saa9730_regs->StationMgmtCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* await completion */
439 i = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000440 while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 MD_CA_BUSY) {
442 i++;
443 if (i > 100) {
444 printk
445 ("Error: lan_saa9730_mii_init: timeout\n");
446 return -1;
447 }
448 mdelay(1); /* wait 1 ms. */
449 }
450
451 /* wait for 3 sec. */
452 mdelay(3000);
453
454 /* check the link status */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000455 if (readl(&lp->lan_saa9730_regs->StationMgmtData) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 PHY_STATUS_LINK_UP) {
457 /* link is up */
458 break;
459 }
460 }
461 }
462
463 return 0;
464}
465
466static int lan_saa9730_control_init(struct lan_saa9730_private *lp)
467{
468 /* Initialize DMA control register. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000469 writel((LANMB_ANY << DMA_CTL_MAX_XFER_SHF) |
470 (LANEND_LITTLE << DMA_CTL_ENDIAN_SHF) |
471 (LAN_SAA9730_RCV_Q_INT_THRESHOLD << DMA_CTL_RX_INT_COUNT_SHF)
472 | DMA_CTL_RX_INT_TO_EN | DMA_CTL_RX_INT_EN |
473 DMA_CTL_MAC_RX_INT_EN | DMA_CTL_MAC_TX_INT_EN,
474 &lp->lan_saa9730_regs->LanDmaCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /* Initial MAC control register. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000477 writel((MACCM_MII << MAC_CONTROL_CONN_SHF) | MAC_CONTROL_FULL_DUP,
478 &lp->lan_saa9730_regs->MacCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 /* Initialize CAM control register. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000481 writel(CAM_CONTROL_COMP_EN | CAM_CONTROL_BROAD_ACC,
482 &lp->lan_saa9730_regs->CamCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000484 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * Initialize CAM enable register, only turn on first entry, should
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000486 * contain own addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000488 writel(0x0001, &lp->lan_saa9730_regs->CamEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* Initialize Tx control register */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000491 writel(TX_CTL_EN_COMP, &lp->lan_saa9730_regs->TxCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* Initialize Rcv control register */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000494 writel(RX_CTL_STRIP_CRC, &lp->lan_saa9730_regs->RxCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /* Reset DMA engine */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000497 writel(DMA_TEST_SW_RESET, &lp->lan_saa9730_regs->DmaTest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 return 0;
500}
501
502static int lan_saa9730_stop(struct lan_saa9730_private *lp)
503{
504 int i;
505
506 /* Stop DMA first */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000507 writel(readl(&lp->lan_saa9730_regs->LanDmaCtl) &
508 ~(DMA_CTL_EN_TX_DMA | DMA_CTL_EN_RX_DMA),
509 &lp->lan_saa9730_regs->LanDmaCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* Set the SW Reset bits in DMA and MAC control registers */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000512 writel(DMA_TEST_SW_RESET, &lp->lan_saa9730_regs->DmaTest);
513 writel(readl(&lp->lan_saa9730_regs->MacCtl) | MAC_CONTROL_RESET,
514 &lp->lan_saa9730_regs->MacCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000516 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * Wait for MAC reset to have finished. The reset bit is auto cleared
518 * when the reset is done.
519 */
520 i = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000521 while (readl(&lp->lan_saa9730_regs->MacCtl) & MAC_CONTROL_RESET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 i++;
523 if (i > 100) {
524 printk
525 ("Error: lan_sa9730_stop: MAC reset timeout\n");
526 return -1;
527 }
528 mdelay(1); /* wait 1 ms. */
529 }
530
531 return 0;
532}
533
534static int lan_saa9730_dma_init(struct lan_saa9730_private *lp)
535{
536 /* Stop lan controller. */
537 lan_saa9730_stop(lp);
538
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000539 writel(LAN_SAA9730_DEFAULT_TIME_OUT_CNT,
540 &lp->lan_saa9730_regs->Timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 return 0;
543}
544
545static int lan_saa9730_start(struct lan_saa9730_private *lp)
546{
547 lan_saa9730_buffer_init(lp);
548
549 /* Initialize Rx Buffer Index */
550 lp->NextRcvPacketIndex = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000551 lp->NextRcvBufferIndex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Ralf Baechle05d9c842005-11-09 17:10:05 +0000553 /* Set current buffer index & next available packet index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 lp->NextTxmPacketIndex = 0;
555 lp->NextTxmBufferIndex = 0;
556 lp->PendingTxmPacketIndex = 0;
557 lp->PendingTxmBufferIndex = 0;
558
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000559 writel(readl(&lp->lan_saa9730_regs->LanDmaCtl) | DMA_CTL_EN_TX_DMA |
560 DMA_CTL_EN_RX_DMA, &lp->lan_saa9730_regs->LanDmaCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 /* For Tx, turn on MAC then DMA */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000563 writel(readl(&lp->lan_saa9730_regs->TxCtl) | TX_CTL_TX_EN,
564 &lp->lan_saa9730_regs->TxCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* For Rx, turn on DMA then MAC */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000567 writel(readl(&lp->lan_saa9730_regs->RxCtl) | RX_CTL_RX_EN,
568 &lp->lan_saa9730_regs->RxCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Ralf Baechle05d9c842005-11-09 17:10:05 +0000570 /* Set Ok2Use to let hardware own the buffers. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000571 writel(OK2USE_RX_A | OK2USE_RX_B, &lp->lan_saa9730_regs->Ok2Use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 return 0;
574}
575
576static int lan_saa9730_restart(struct lan_saa9730_private *lp)
577{
578 lan_saa9730_stop(lp);
579 lan_saa9730_start(lp);
580
581 return 0;
582}
583
584static int lan_saa9730_tx(struct net_device *dev)
585{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000586 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 unsigned int *pPacket;
588 unsigned int tx_status;
589
590 if (lan_saa9730_debug > 5)
591 printk("lan_saa9730_tx interrupt\n");
592
593 /* Clear interrupt. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000594 writel(DMA_STATUS_MAC_TX_INT, &lp->lan_saa9730_regs->DmaStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 while (1) {
Ralf Baechle05d9c842005-11-09 17:10:05 +0000597 pPacket = lp->TxmBuffer[lp->PendingTxmBufferIndex]
598 [lp->PendingTxmPacketIndex];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* Get status of first packet transmitted. */
601 tx_status = le32_to_cpu(*pPacket);
602
603 /* Check ownership. */
604 if ((tx_status & TX_STAT_CTL_OWNER_MSK) !=
605 (TXSF_HWDONE << TX_STAT_CTL_OWNER_SHF)) break;
606
607 /* Check for error. */
608 if (tx_status & TX_STAT_CTL_ERROR_MSK) {
609 if (lan_saa9730_debug > 1)
610 printk("lan_saa9730_tx: tx error = %x\n",
611 tx_status);
612
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700613 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (tx_status &
615 (TX_STATUS_EX_COLL << TX_STAT_CTL_STATUS_SHF))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700616 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (tx_status &
Ralf Baechle05d9c842005-11-09 17:10:05 +0000618 (TX_STATUS_LATE_COLL << TX_STAT_CTL_STATUS_SHF))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700619 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (tx_status &
621 (TX_STATUS_L_CARR << TX_STAT_CTL_STATUS_SHF))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700622 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (tx_status &
624 (TX_STATUS_UNDER << TX_STAT_CTL_STATUS_SHF))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700625 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (tx_status &
627 (TX_STATUS_SQ_ERR << TX_STAT_CTL_STATUS_SHF))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700628 dev->stats.tx_heartbeat_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700630 dev->stats.collisions +=
Ralf Baechle05d9c842005-11-09 17:10:05 +0000631 tx_status & TX_STATUS_TX_COLL_MSK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 /* Free buffer. */
635 *pPacket =
636 cpu_to_le32(TXSF_EMPTY << TX_STAT_CTL_OWNER_SHF);
637
638 /* Update pending index pointer. */
639 lp->PendingTxmPacketIndex++;
640 if (lp->PendingTxmPacketIndex >= LAN_SAA9730_TXM_Q_SIZE) {
641 lp->PendingTxmPacketIndex = 0;
642 lp->PendingTxmBufferIndex ^= 1;
643 }
644 }
645
Ralf Baechle05d9c842005-11-09 17:10:05 +0000646 /* The tx buffer is no longer full. */
647 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 return 0;
650}
651
652static int lan_saa9730_rx(struct net_device *dev)
653{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000654 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 int len = 0;
656 struct sk_buff *skb = 0;
657 unsigned int rx_status;
658 int BufferIndex;
659 int PacketIndex;
660 unsigned int *pPacket;
661 unsigned char *pData;
662
663 if (lan_saa9730_debug > 5)
664 printk("lan_saa9730_rx interrupt\n");
665
666 /* Clear receive interrupts. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000667 writel(DMA_STATUS_MAC_RX_INT | DMA_STATUS_RX_INT |
668 DMA_STATUS_RX_TO_INT, &lp->lan_saa9730_regs->DmaStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /* Address next packet */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000671 BufferIndex = lp->NextRcvBufferIndex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 PacketIndex = lp->NextRcvPacketIndex;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000673 pPacket = lp->RcvBuffer[BufferIndex][PacketIndex];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 rx_status = le32_to_cpu(*pPacket);
675
676 /* Process each packet. */
677 while ((rx_status & RX_STAT_CTL_OWNER_MSK) ==
678 (RXSF_HWDONE << RX_STAT_CTL_OWNER_SHF)) {
679 /* Check the rx status. */
680 if (rx_status & (RX_STATUS_GOOD << RX_STAT_CTL_STATUS_SHF)) {
681 /* Received packet is good. */
682 len = (rx_status & RX_STAT_CTL_LENGTH_MSK) >>
683 RX_STAT_CTL_LENGTH_SHF;
684
685 pData = (unsigned char *) pPacket;
686 pData += 4;
687 skb = dev_alloc_skb(len + 2);
688 if (skb == 0) {
689 printk
690 ("%s: Memory squeeze, deferring packet.\n",
691 dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700692 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700694 dev->stats.rx_bytes += len;
695 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 skb_reserve(skb, 2); /* 16 byte align */
697 skb_put(skb, len); /* make room */
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700698 skb_copy_to_linear_data(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 (unsigned char *) pData,
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700700 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 skb->protocol = eth_type_trans(skb, dev);
702 netif_rx(skb);
703 dev->last_rx = jiffies;
704 }
705 } else {
706 /* We got an error packet. */
707 if (lan_saa9730_debug > 2)
708 printk
709 ("lan_saa9730_rx: We got an error packet = %x\n",
710 rx_status);
711
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700712 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (rx_status &
714 (RX_STATUS_CRC_ERR << RX_STAT_CTL_STATUS_SHF))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700715 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (rx_status &
Ralf Baechle05d9c842005-11-09 17:10:05 +0000717 (RX_STATUS_ALIGN_ERR << RX_STAT_CTL_STATUS_SHF))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700718 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (rx_status &
720 (RX_STATUS_OVERFLOW << RX_STAT_CTL_STATUS_SHF))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700721 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (rx_status &
723 (RX_STATUS_LONG_ERR << RX_STAT_CTL_STATUS_SHF))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700724 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
727 /* Indicate we have processed the buffer. */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000728 *pPacket = cpu_to_le32(RXSF_READY << RX_STAT_CTL_OWNER_SHF);
729
730 /* Make sure A or B is available to hardware as appropriate. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000731 writel(BufferIndex ? OK2USE_RX_B : OK2USE_RX_A,
732 &lp->lan_saa9730_regs->Ok2Use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /* Go to next packet in sequence. */
735 lp->NextRcvPacketIndex++;
736 if (lp->NextRcvPacketIndex >= LAN_SAA9730_RCV_Q_SIZE) {
737 lp->NextRcvPacketIndex = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000738 lp->NextRcvBufferIndex ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /* Address next packet */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000742 BufferIndex = lp->NextRcvBufferIndex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 PacketIndex = lp->NextRcvPacketIndex;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000744 pPacket = lp->RcvBuffer[BufferIndex][PacketIndex];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 rx_status = le32_to_cpu(*pPacket);
746 }
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return 0;
749}
750
David Howells7d12e782006-10-05 14:55:46 +0100751static irqreturn_t lan_saa9730_interrupt(const int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Jeff Garzikc31f28e2006-10-06 14:56:04 -0400753 struct net_device *dev = dev_id;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000754 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (lan_saa9730_debug > 5)
757 printk("lan_saa9730_interrupt\n");
758
759 /* Disable the EVM LAN interrupt. */
760 evm_saa9730_block_lan_int(lp);
761
762 /* Clear the EVM LAN interrupt. */
763 evm_saa9730_clear_lan_int(lp);
764
765 /* Service pending transmit interrupts. */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000766 if (readl(&lp->lan_saa9730_regs->DmaStatus) & DMA_STATUS_MAC_TX_INT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 lan_saa9730_tx(dev);
768
769 /* Service pending receive interrupts. */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000770 if (readl(&lp->lan_saa9730_regs->DmaStatus) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 (DMA_STATUS_MAC_RX_INT | DMA_STATUS_RX_INT |
772 DMA_STATUS_RX_TO_INT)) lan_saa9730_rx(dev);
773
774 /* Enable the EVM LAN interrupt. */
775 evm_saa9730_unblock_lan_int(lp);
776
777 return IRQ_HANDLED;
778}
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780static int lan_saa9730_open(struct net_device *dev)
781{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000782 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* Associate IRQ with lan_saa9730_interrupt */
785 if (request_irq(dev->irq, &lan_saa9730_interrupt, 0, "SAA9730 Eth",
786 dev)) {
787 printk("lan_saa9730_open: Can't get irq %d\n", dev->irq);
788 return -EAGAIN;
789 }
790
791 /* Enable the Lan interrupt in the event manager. */
792 evm_saa9730_enable_lan_int(lp);
793
794 /* Start the LAN controller */
795 if (lan_saa9730_start(lp))
796 return -1;
797
798 netif_start_queue(dev);
799
800 return 0;
801}
802
803static int lan_saa9730_write(struct lan_saa9730_private *lp,
804 struct sk_buff *skb, int skblen)
805{
806 unsigned char *pbData = skb->data;
807 unsigned int len = skblen;
808 unsigned char *pbPacketData;
809 unsigned int tx_status;
810 int BufferIndex;
811 int PacketIndex;
812
813 if (lan_saa9730_debug > 5)
Ralf Baechle05d9c842005-11-09 17:10:05 +0000814 printk("lan_saa9730_write: skb=%p\n", skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 BufferIndex = lp->NextTxmBufferIndex;
817 PacketIndex = lp->NextTxmPacketIndex;
818
Ralf Baechle05d9c842005-11-09 17:10:05 +0000819 tx_status = le32_to_cpu(*(unsigned int *)lp->TxmBuffer[BufferIndex]
820 [PacketIndex]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if ((tx_status & TX_STAT_CTL_OWNER_MSK) !=
822 (TXSF_EMPTY << TX_STAT_CTL_OWNER_SHF)) {
823 if (lan_saa9730_debug > 4)
824 printk
825 ("lan_saa9730_write: Tx buffer not available: tx_status = %x\n",
826 tx_status);
827 return -1;
828 }
829
830 lp->NextTxmPacketIndex++;
831 if (lp->NextTxmPacketIndex >= LAN_SAA9730_TXM_Q_SIZE) {
832 lp->NextTxmPacketIndex = 0;
833 lp->NextTxmBufferIndex ^= 1;
834 }
835
Ralf Baechle05d9c842005-11-09 17:10:05 +0000836 pbPacketData = lp->TxmBuffer[BufferIndex][PacketIndex];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 pbPacketData += 4;
838
839 /* copy the bits */
840 memcpy(pbPacketData, pbData, len);
841
842 /* Set transmit status for hardware */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000843 *(unsigned int *)lp->TxmBuffer[BufferIndex][PacketIndex] =
844 cpu_to_le32((TXSF_READY << TX_STAT_CTL_OWNER_SHF) |
845 (TX_STAT_CTL_INT_AFTER_TX <<
846 TX_STAT_CTL_FRAME_SHF) |
847 (len << TX_STAT_CTL_LENGTH_SHF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Ralf Baechle05d9c842005-11-09 17:10:05 +0000849 /* Make sure A or B is available to hardware as appropriate. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000850 writel(BufferIndex ? OK2USE_TX_B : OK2USE_TX_A,
851 &lp->lan_saa9730_regs->Ok2Use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 return 0;
854}
855
856static void lan_saa9730_tx_timeout(struct net_device *dev)
857{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000858 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /* Transmitter timeout, serious problems */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700861 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 printk("%s: transmit timed out, reset\n", dev->name);
Ralf Baechleb77eb352007-10-14 14:13:58 +0100863 /*show_saa9730_regs(dev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 lan_saa9730_restart(lp);
865
866 dev->trans_start = jiffies;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000867 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870static int lan_saa9730_start_xmit(struct sk_buff *skb,
871 struct net_device *dev)
872{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000873 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 unsigned long flags;
875 int skblen;
876 int len;
877
878 if (lan_saa9730_debug > 4)
Ralf Baechle05d9c842005-11-09 17:10:05 +0000879 printk("Send packet: skb=%p\n", skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 skblen = skb->len;
882
883 spin_lock_irqsave(&lp->lock, flags);
884
885 len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
886
887 if (lan_saa9730_write(lp, skb, skblen)) {
888 spin_unlock_irqrestore(&lp->lock, flags);
Ralf Baechle05d9c842005-11-09 17:10:05 +0000889 printk("Error when writing packet to controller: skb=%p\n", skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 netif_stop_queue(dev);
891 return -1;
892 }
893
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700894 dev->stats.tx_bytes += len;
895 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 dev->trans_start = jiffies;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000898 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 dev_kfree_skb(skb);
900
901 spin_unlock_irqrestore(&lp->lock, flags);
902
903 return 0;
904}
905
906static int lan_saa9730_close(struct net_device *dev)
907{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000908 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 if (lan_saa9730_debug > 1)
911 printk("lan_saa9730_close:\n");
912
913 netif_stop_queue(dev);
914
915 /* Disable the Lan interrupt in the event manager. */
916 evm_saa9730_disable_lan_int(lp);
917
918 /* Stop the controller */
919 if (lan_saa9730_stop(lp))
920 return -1;
921
922 free_irq(dev->irq, (void *) dev);
923
924 return 0;
925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927static void lan_saa9730_set_multicast(struct net_device *dev)
928{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000929 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 /* Stop the controller */
932 lan_saa9730_stop(lp);
933
934 if (dev->flags & IFF_PROMISC) {
935 /* accept all packets */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000936 writel(CAM_CONTROL_COMP_EN | CAM_CONTROL_STATION_ACC |
937 CAM_CONTROL_GROUP_ACC | CAM_CONTROL_BROAD_ACC,
938 &lp->lan_saa9730_regs->CamCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 } else {
YOSHIFUJI Hideaki / 吉藤英明82a02442007-07-17 13:46:00 +0900940 if (dev->flags & IFF_ALLMULTI || dev->mc_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* accept all multicast packets */
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000942 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * Will handle the multicast stuff later. -carstenl
944 */
YOSHIFUJI Hideaki / 吉藤英明82a02442007-07-17 13:46:00 +0900945 writel(CAM_CONTROL_COMP_EN | CAM_CONTROL_GROUP_ACC |
946 CAM_CONTROL_BROAD_ACC,
947 &lp->lan_saa9730_regs->CamCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949 }
950
951 lan_saa9730_restart(lp);
952}
953
954
955static void __devexit saa9730_remove_one(struct pci_dev *pdev)
956{
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000957 struct net_device *dev = pci_get_drvdata(pdev);
Ralf Baechle05d9c842005-11-09 17:10:05 +0000958 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000960 if (dev) {
961 unregister_netdev(dev);
Ralf Baechle05d9c842005-11-09 17:10:05 +0000962 lan_saa9730_free_buffers(pdev, lp);
963 iounmap(lp->lan_saa9730_regs);
964 iounmap(lp->evm_saa9730_regs);
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000965 free_netdev(dev);
966 pci_release_regions(pdev);
967 pci_disable_device(pdev);
968 pci_set_drvdata(pdev, NULL);
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972
Ralf Baechle05d9c842005-11-09 17:10:05 +0000973static int lan_saa9730_init(struct net_device *dev, struct pci_dev *pdev,
974 unsigned long ioaddr, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000976 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 unsigned char ethernet_addr[6];
Ralf Baechle05d9c842005-11-09 17:10:05 +0000978 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Ralf Baechle05d9c842005-11-09 17:10:05 +0000980 if (get_ethernet_addr(ethernet_addr)) {
981 ret = -ENODEV;
982 goto out;
983 }
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 memcpy(dev->dev_addr, ethernet_addr, 6);
986 dev->base_addr = ioaddr;
987 dev->irq = irq;
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000988
Ralf Baechle05d9c842005-11-09 17:10:05 +0000989 lp->pci_dev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /* Set SAA9730 LAN base address. */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000992 lp->lan_saa9730_regs = ioremap(ioaddr + SAA9730_LAN_REGS_ADDR,
993 SAA9730_LAN_REGS_SIZE);
994 if (!lp->lan_saa9730_regs) {
995 ret = -ENOMEM;
996 goto out;
997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /* Set SAA9730 EVM base address. */
Ralf Baechle05d9c842005-11-09 17:10:05 +00001000 lp->evm_saa9730_regs = ioremap(ioaddr + SAA9730_EVM_REGS_ADDR,
1001 SAA9730_EVM_REGS_SIZE);
1002 if (!lp->evm_saa9730_regs) {
1003 ret = -ENOMEM;
1004 goto out_iounmap_lan;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /* Allocate LAN RX/TX frame buffer space. */
Ralf Baechle05d9c842005-11-09 17:10:05 +00001008 if ((ret = lan_saa9730_allocate_buffers(pdev, lp)))
1009 goto out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 /* Stop LAN controller. */
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001012 if ((ret = lan_saa9730_stop(lp)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001013 goto out_free_consistent;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /* Initialize CAM registers. */
1016 if ((ret = lan_saa9730_cam_init(dev)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001017 goto out_free_consistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 /* Initialize MII registers. */
1020 if ((ret = lan_saa9730_mii_init(lp)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001021 goto out_free_consistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /* Initialize control registers. */
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001024 if ((ret = lan_saa9730_control_init(lp)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001025 goto out_free_consistent;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /* Load CAM registers. */
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001028 if ((ret = lan_saa9730_cam_load(lp)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001029 goto out_free_consistent;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /* Initialize DMA context registers. */
1032 if ((ret = lan_saa9730_dma_init(lp)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001033 goto out_free_consistent;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 spin_lock_init(&lp->lock);
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 dev->open = lan_saa9730_open;
1038 dev->hard_start_xmit = lan_saa9730_start_xmit;
1039 dev->stop = lan_saa9730_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 dev->set_multicast_list = lan_saa9730_set_multicast;
1041 dev->tx_timeout = lan_saa9730_tx_timeout;
1042 dev->watchdog_timeo = (HZ >> 1);
1043 dev->dma = 0;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001044
Ralf Baechle05d9c842005-11-09 17:10:05 +00001045 ret = register_netdev (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (ret)
Ralf Baechle05d9c842005-11-09 17:10:05 +00001047 goto out_free_consistent;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
1050
Ralf Baechle05d9c842005-11-09 17:10:05 +00001051out_free_consistent:
1052 lan_saa9730_free_buffers(pdev, lp);
1053out_iounmap:
1054 iounmap(lp->evm_saa9730_regs);
1055out_iounmap_lan:
1056 iounmap(lp->lan_saa9730_regs);
1057out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return ret;
1059}
1060
1061
1062static int __devinit saa9730_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1063{
Ralf Baechle05d9c842005-11-09 17:10:05 +00001064 struct net_device *dev = NULL;
1065 unsigned long pci_ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 int err;
1067
1068 if (lan_saa9730_debug > 1)
1069 printk("saa9730.c: PCI bios is present, checking for devices...\n");
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 err = pci_enable_device(pdev);
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001072 if (err) {
1073 printk(KERN_ERR "Cannot enable PCI device, aborting.\n");
Ralf Baechle05d9c842005-11-09 17:10:05 +00001074 goto out;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 err = pci_request_regions(pdev, DRV_MODULE_NAME);
1078 if (err) {
1079 printk(KERN_ERR "Cannot obtain PCI resources, aborting.\n");
Ralf Baechle05d9c842005-11-09 17:10:05 +00001080 goto out_disable_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082
1083 pci_irq_line = pdev->irq;
1084 /* LAN base address in located at BAR 1. */
1085
1086 pci_ioaddr = pci_resource_start(pdev, 1);
1087 pci_set_master(pdev);
1088
Ralf Baechle05d9c842005-11-09 17:10:05 +00001089 printk("Found SAA9730 (PCI) at %lx, irq %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 pci_ioaddr, pci_irq_line);
1091
Ralf Baechle05d9c842005-11-09 17:10:05 +00001092 dev = alloc_etherdev(sizeof(struct lan_saa9730_private));
1093 if (!dev)
1094 goto out_disable_pdev;
1095
1096 err = lan_saa9730_init(dev, pdev, pci_ioaddr, pci_irq_line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (err) {
Ralf Baechle05d9c842005-11-09 17:10:05 +00001098 printk("LAN init failed");
1099 goto out_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101
1102 pci_set_drvdata(pdev, dev);
1103 SET_NETDEV_DEV(dev, &pdev->dev);
1104 return 0;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001105
Ralf Baechle05d9c842005-11-09 17:10:05 +00001106out_free_netdev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 free_netdev(dev);
Ralf Baechle05d9c842005-11-09 17:10:05 +00001108out_disable_pdev:
1109 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110out:
Ralf Baechle05d9c842005-11-09 17:10:05 +00001111 pci_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return err;
1113}
1114
1115
1116static struct pci_driver saa9730_driver = {
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001117 .name = DRV_MODULE_NAME,
1118 .id_table = saa9730_pci_tbl,
1119 .probe = saa9730_init_one,
1120 .remove = __devexit_p(saa9730_remove_one),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121};
1122
1123
1124static int __init saa9730_init(void)
1125{
Jeff Garzik29917622006-08-19 17:48:59 -04001126 return pci_register_driver(&saa9730_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129static void __exit saa9730_cleanup(void)
1130{
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001131 pci_unregister_driver(&saa9730_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
1134module_init(saa9730_init);
1135module_exit(saa9730_cleanup);
1136
Ralf Baechle05d9c842005-11-09 17:10:05 +00001137MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
1138MODULE_DESCRIPTION("Philips SAA9730 ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139MODULE_LICENSE("GPL");