blob: 143958f1ef0a07a54d7814575ac3240c3c2db922 [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 Baechle05d9c842005-11-09 17:10:05 +0000100static void __attribute_used__ show_saa9730_regs(struct lan_saa9730_private *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 int i, j;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000103 printk("TxmBufferA = %p\n", lp->TxmBuffer[0][0]);
104 printk("TxmBufferB = %p\n", lp->TxmBuffer[1][0]);
105 printk("RcvBufferA = %p\n", lp->RcvBuffer[0][0]);
106 printk("RcvBufferB = %p\n", lp->RcvBuffer[1][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
108 for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
109 printk("TxmBuffer[%d][%d] = %x\n", i, j,
110 le32_to_cpu(*(unsigned int *)
111 lp->TxmBuffer[i][j]));
112 }
113 }
114 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
115 for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
116 printk("RcvBuffer[%d][%d] = %x\n", i, j,
117 le32_to_cpu(*(unsigned int *)
118 lp->RcvBuffer[i][j]));
119 }
120 }
121 printk("lp->evm_saa9730_regs->InterruptBlock1 = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000122 readl(&lp->evm_saa9730_regs->InterruptBlock1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 printk("lp->evm_saa9730_regs->InterruptStatus1 = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000124 readl(&lp->evm_saa9730_regs->InterruptStatus1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 printk("lp->evm_saa9730_regs->InterruptEnable1 = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000126 readl(&lp->evm_saa9730_regs->InterruptEnable1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 printk("lp->lan_saa9730_regs->Ok2Use = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000128 readl(&lp->lan_saa9730_regs->Ok2Use));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 printk("lp->NextTxmBufferIndex = %x\n", lp->NextTxmBufferIndex);
130 printk("lp->NextTxmPacketIndex = %x\n", lp->NextTxmPacketIndex);
131 printk("lp->PendingTxmBufferIndex = %x\n",
132 lp->PendingTxmBufferIndex);
133 printk("lp->PendingTxmPacketIndex = %x\n",
134 lp->PendingTxmPacketIndex);
135 printk("lp->lan_saa9730_regs->LanDmaCtl = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000136 readl(&lp->lan_saa9730_regs->LanDmaCtl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 printk("lp->lan_saa9730_regs->DmaStatus = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000138 readl(&lp->lan_saa9730_regs->DmaStatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 printk("lp->lan_saa9730_regs->CamCtl = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000140 readl(&lp->lan_saa9730_regs->CamCtl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 printk("lp->lan_saa9730_regs->TxCtl = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000142 readl(&lp->lan_saa9730_regs->TxCtl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 printk("lp->lan_saa9730_regs->TxStatus = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000144 readl(&lp->lan_saa9730_regs->TxStatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 printk("lp->lan_saa9730_regs->RxCtl = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000146 readl(&lp->lan_saa9730_regs->RxCtl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 printk("lp->lan_saa9730_regs->RxStatus = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000148 readl(&lp->lan_saa9730_regs->RxStatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 for (i = 0; i < LAN_SAA9730_CAM_DWORDS; i++) {
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000150 writel(i, &lp->lan_saa9730_regs->CamAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 printk("lp->lan_saa9730_regs->CamData = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000152 readl(&lp->lan_saa9730_regs->CamData));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154 printk("lp->stats.tx_packets = %lx\n", lp->stats.tx_packets);
155 printk("lp->stats.tx_errors = %lx\n", lp->stats.tx_errors);
156 printk("lp->stats.tx_aborted_errors = %lx\n",
157 lp->stats.tx_aborted_errors);
158 printk("lp->stats.tx_window_errors = %lx\n",
159 lp->stats.tx_window_errors);
160 printk("lp->stats.tx_carrier_errors = %lx\n",
161 lp->stats.tx_carrier_errors);
162 printk("lp->stats.tx_fifo_errors = %lx\n",
163 lp->stats.tx_fifo_errors);
164 printk("lp->stats.tx_heartbeat_errors = %lx\n",
165 lp->stats.tx_heartbeat_errors);
166 printk("lp->stats.collisions = %lx\n", lp->stats.collisions);
167
168 printk("lp->stats.rx_packets = %lx\n", lp->stats.rx_packets);
169 printk("lp->stats.rx_errors = %lx\n", lp->stats.rx_errors);
170 printk("lp->stats.rx_dropped = %lx\n", lp->stats.rx_dropped);
171 printk("lp->stats.rx_crc_errors = %lx\n", lp->stats.rx_crc_errors);
172 printk("lp->stats.rx_frame_errors = %lx\n",
173 lp->stats.rx_frame_errors);
174 printk("lp->stats.rx_fifo_errors = %lx\n",
175 lp->stats.rx_fifo_errors);
176 printk("lp->stats.rx_length_errors = %lx\n",
177 lp->stats.rx_length_errors);
178
179 printk("lp->lan_saa9730_regs->DebugPCIMasterAddr = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000180 readl(&lp->lan_saa9730_regs->DebugPCIMasterAddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 printk("lp->lan_saa9730_regs->DebugLanTxStateMachine = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000182 readl(&lp->lan_saa9730_regs->DebugLanTxStateMachine));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 printk("lp->lan_saa9730_regs->DebugLanRxStateMachine = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000184 readl(&lp->lan_saa9730_regs->DebugLanRxStateMachine));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 printk("lp->lan_saa9730_regs->DebugLanTxFifoPointers = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000186 readl(&lp->lan_saa9730_regs->DebugLanTxFifoPointers));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 printk("lp->lan_saa9730_regs->DebugLanRxFifoPointers = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000188 readl(&lp->lan_saa9730_regs->DebugLanRxFifoPointers));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 printk("lp->lan_saa9730_regs->DebugLanCtlStateMachine = %x\n",
Ralf Baechle05d9c842005-11-09 17:10:05 +0000190 readl(&lp->lan_saa9730_regs->DebugLanCtlStateMachine));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193static void lan_saa9730_buffer_init(struct lan_saa9730_private *lp)
194{
195 int i, j;
196
197 /* Init RX buffers */
198 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
199 for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
200 *(unsigned int *) lp->RcvBuffer[i][j] =
201 cpu_to_le32(RXSF_READY <<
202 RX_STAT_CTL_OWNER_SHF);
203 }
204 }
205
206 /* Init TX buffers */
207 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
208 for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
209 *(unsigned int *) lp->TxmBuffer[i][j] =
210 cpu_to_le32(TXSF_EMPTY <<
211 TX_STAT_CTL_OWNER_SHF);
212 }
213 }
214}
215
Ralf Baechle05d9c842005-11-09 17:10:05 +0000216static void lan_saa9730_free_buffers(struct pci_dev *pdev,
217 struct lan_saa9730_private *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000219 pci_free_consistent(pdev, lp->buffer_size, lp->buffer_start,
220 lp->dma_addr);
221}
222
223static int lan_saa9730_allocate_buffers(struct pci_dev *pdev,
224 struct lan_saa9730_private *lp)
225{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 void *Pa;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000227 unsigned int i, j, rxoffset, txoffset;
228 int ret;
229
230 /* Initialize buffer space */
231 lp->DmaRcvPackets = LAN_SAA9730_RCV_Q_SIZE;
232 lp->DmaTxmPackets = LAN_SAA9730_TXM_Q_SIZE;
233
234 /* Initialize Rx Buffer Index */
235 lp->NextRcvPacketIndex = 0;
236 lp->NextRcvBufferIndex = 0;
237
238 /* Set current buffer index & next available packet index */
239 lp->NextTxmPacketIndex = 0;
240 lp->NextTxmBufferIndex = 0;
241 lp->PendingTxmPacketIndex = 0;
242 lp->PendingTxmBufferIndex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000244 /*
245 * Allocate all RX and TX packets in one chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * The Rx and Tx packets must be PACKET_SIZE aligned.
247 */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000248 lp->buffer_size = ((LAN_SAA9730_RCV_Q_SIZE + LAN_SAA9730_TXM_Q_SIZE) *
249 LAN_SAA9730_PACKET_SIZE * LAN_SAA9730_BUFFERS) +
250 LAN_SAA9730_PACKET_SIZE;
251 lp->buffer_start = pci_alloc_consistent(pdev, lp->buffer_size,
252 &lp->dma_addr);
253 if (!lp->buffer_start) {
254 ret = -ENOMEM;
255 goto out;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Ralf Baechle05d9c842005-11-09 17:10:05 +0000258 Pa = (void *)ALIGN((unsigned long)lp->buffer_start,
259 LAN_SAA9730_PACKET_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Ralf Baechle05d9c842005-11-09 17:10:05 +0000261 rxoffset = Pa - lp->buffer_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* Init RX buffers */
264 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
265 for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
266 *(unsigned int *) Pa =
267 cpu_to_le32(RXSF_READY <<
268 RX_STAT_CTL_OWNER_SHF);
Ralf Baechle05d9c842005-11-09 17:10:05 +0000269 lp->RcvBuffer[i][j] = Pa;
270 Pa += LAN_SAA9730_PACKET_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 }
273
Ralf Baechle05d9c842005-11-09 17:10:05 +0000274 txoffset = Pa - lp->buffer_start;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* Init TX buffers */
277 for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
278 for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
279 *(unsigned int *) Pa =
280 cpu_to_le32(TXSF_EMPTY <<
281 TX_STAT_CTL_OWNER_SHF);
Ralf Baechle05d9c842005-11-09 17:10:05 +0000282 lp->TxmBuffer[i][j] = Pa;
283 Pa += LAN_SAA9730_PACKET_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285 }
286
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000287 /*
288 * Set rx buffer A and rx buffer B to point to the first two buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * spaces.
290 */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000291 writel(lp->dma_addr + rxoffset, &lp->lan_saa9730_regs->RxBuffA);
292 writel(lp->dma_addr + rxoffset +
293 LAN_SAA9730_PACKET_SIZE * LAN_SAA9730_RCV_Q_SIZE,
294 &lp->lan_saa9730_regs->RxBuffB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000296 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Set txm_buf_a and txm_buf_b to point to the first two buffer
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000298 * space
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000300 writel(lp->dma_addr + txoffset,
301 &lp->lan_saa9730_regs->TxBuffA);
302 writel(lp->dma_addr + txoffset +
303 LAN_SAA9730_PACKET_SIZE * LAN_SAA9730_TXM_Q_SIZE,
304 &lp->lan_saa9730_regs->TxBuffB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* Set packet number */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000307 writel((lp->DmaRcvPackets << PK_COUNT_RX_A_SHF) |
308 (lp->DmaRcvPackets << PK_COUNT_RX_B_SHF) |
309 (lp->DmaTxmPackets << PK_COUNT_TX_A_SHF) |
310 (lp->DmaTxmPackets << PK_COUNT_TX_B_SHF),
311 &lp->lan_saa9730_regs->PacketCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 return 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000314
315out:
316 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319static int lan_saa9730_cam_load(struct lan_saa9730_private *lp)
320{
321 unsigned int i;
322 unsigned char *NetworkAddress;
323
324 NetworkAddress = (unsigned char *) &lp->PhysicalAddress[0][0];
325
326 for (i = 0; i < LAN_SAA9730_CAM_DWORDS; i++) {
327 /* First set address to where data is written */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000328 writel(i, &lp->lan_saa9730_regs->CamAddress);
329 writel((NetworkAddress[0] << 24) | (NetworkAddress[1] << 16) |
330 (NetworkAddress[2] << 8) | NetworkAddress[3],
331 &lp->lan_saa9730_regs->CamData);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 NetworkAddress += 4;
333 }
334 return 0;
335}
336
337static int lan_saa9730_cam_init(struct net_device *dev)
338{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000339 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned int i;
341
342 /* Copy MAC-address into all entries. */
343 for (i = 0; i < LAN_SAA9730_CAM_ENTRIES; i++) {
344 memcpy((unsigned char *) lp->PhysicalAddress[i],
345 (unsigned char *) dev->dev_addr, 6);
346 }
347
348 return 0;
349}
350
351static int lan_saa9730_mii_init(struct lan_saa9730_private *lp)
352{
353 int i, l;
354
355 /* Check link status, spin here till station is not busy. */
356 i = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000357 while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) & MD_CA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 i++;
359 if (i > 100) {
360 printk("Error: lan_saa9730_mii_init: timeout\n");
361 return -1;
362 }
363 mdelay(1); /* wait 1 ms. */
364 }
365
366 /* Now set the control and address register. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000367 writel(MD_CA_BUSY | PHY_STATUS | PHY_ADDRESS << MD_CA_PHY_SHF,
368 &lp->lan_saa9730_regs->StationMgmtCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* check link status, spin here till station is not busy */
371 i = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000372 while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) & MD_CA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 i++;
374 if (i > 100) {
375 printk("Error: lan_saa9730_mii_init: timeout\n");
376 return -1;
377 }
378 mdelay(1); /* wait 1 ms. */
379 }
380
381 /* Wait for 1 ms. */
382 mdelay(1);
383
384 /* Check the link status. */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000385 if (readl(&lp->lan_saa9730_regs->StationMgmtData) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 PHY_STATUS_LINK_UP) {
387 /* Link is up. */
388 return 0;
389 } else {
390 /* Link is down, reset the PHY first. */
391
392 /* set PHY address = 'CONTROL' */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000393 writel(PHY_ADDRESS << MD_CA_PHY_SHF | MD_CA_WR | PHY_CONTROL,
394 &lp->lan_saa9730_regs->StationMgmtCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* Wait for 1 ms. */
397 mdelay(1);
398
399 /* set 'CONTROL' = force reset and renegotiate */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000400 writel(PHY_CONTROL_RESET | PHY_CONTROL_AUTO_NEG |
401 PHY_CONTROL_RESTART_AUTO_NEG,
402 &lp->lan_saa9730_regs->StationMgmtData);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* Wait for 50 ms. */
405 mdelay(50);
406
407 /* set 'BUSY' to start operation */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000408 writel(MD_CA_BUSY | PHY_ADDRESS << MD_CA_PHY_SHF | MD_CA_WR |
409 PHY_CONTROL, &lp->lan_saa9730_regs->StationMgmtCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* await completion */
412 i = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000413 while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 MD_CA_BUSY) {
415 i++;
416 if (i > 100) {
417 printk
418 ("Error: lan_saa9730_mii_init: timeout\n");
419 return -1;
420 }
421 mdelay(1); /* wait 1 ms. */
422 }
423
424 /* Wait for 1 ms. */
425 mdelay(1);
426
427 for (l = 0; l < 2; l++) {
428 /* set PHY address = 'STATUS' */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000429 writel(MD_CA_BUSY | PHY_ADDRESS << MD_CA_PHY_SHF |
430 PHY_STATUS,
431 &lp->lan_saa9730_regs->StationMgmtCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /* await completion */
434 i = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000435 while (readl(&lp->lan_saa9730_regs->StationMgmtCtl) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 MD_CA_BUSY) {
437 i++;
438 if (i > 100) {
439 printk
440 ("Error: lan_saa9730_mii_init: timeout\n");
441 return -1;
442 }
443 mdelay(1); /* wait 1 ms. */
444 }
445
446 /* wait for 3 sec. */
447 mdelay(3000);
448
449 /* check the link status */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000450 if (readl(&lp->lan_saa9730_regs->StationMgmtData) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 PHY_STATUS_LINK_UP) {
452 /* link is up */
453 break;
454 }
455 }
456 }
457
458 return 0;
459}
460
461static int lan_saa9730_control_init(struct lan_saa9730_private *lp)
462{
463 /* Initialize DMA control register. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000464 writel((LANMB_ANY << DMA_CTL_MAX_XFER_SHF) |
465 (LANEND_LITTLE << DMA_CTL_ENDIAN_SHF) |
466 (LAN_SAA9730_RCV_Q_INT_THRESHOLD << DMA_CTL_RX_INT_COUNT_SHF)
467 | DMA_CTL_RX_INT_TO_EN | DMA_CTL_RX_INT_EN |
468 DMA_CTL_MAC_RX_INT_EN | DMA_CTL_MAC_TX_INT_EN,
469 &lp->lan_saa9730_regs->LanDmaCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* Initial MAC control register. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000472 writel((MACCM_MII << MAC_CONTROL_CONN_SHF) | MAC_CONTROL_FULL_DUP,
473 &lp->lan_saa9730_regs->MacCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* Initialize CAM control register. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000476 writel(CAM_CONTROL_COMP_EN | CAM_CONTROL_BROAD_ACC,
477 &lp->lan_saa9730_regs->CamCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000479 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * Initialize CAM enable register, only turn on first entry, should
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000481 * contain own addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000483 writel(0x0001, &lp->lan_saa9730_regs->CamEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* Initialize Tx control register */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000486 writel(TX_CTL_EN_COMP, &lp->lan_saa9730_regs->TxCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* Initialize Rcv control register */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000489 writel(RX_CTL_STRIP_CRC, &lp->lan_saa9730_regs->RxCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /* Reset DMA engine */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000492 writel(DMA_TEST_SW_RESET, &lp->lan_saa9730_regs->DmaTest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 return 0;
495}
496
497static int lan_saa9730_stop(struct lan_saa9730_private *lp)
498{
499 int i;
500
501 /* Stop DMA first */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000502 writel(readl(&lp->lan_saa9730_regs->LanDmaCtl) &
503 ~(DMA_CTL_EN_TX_DMA | DMA_CTL_EN_RX_DMA),
504 &lp->lan_saa9730_regs->LanDmaCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* Set the SW Reset bits in DMA and MAC control registers */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000507 writel(DMA_TEST_SW_RESET, &lp->lan_saa9730_regs->DmaTest);
508 writel(readl(&lp->lan_saa9730_regs->MacCtl) | MAC_CONTROL_RESET,
509 &lp->lan_saa9730_regs->MacCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000511 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * Wait for MAC reset to have finished. The reset bit is auto cleared
513 * when the reset is done.
514 */
515 i = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000516 while (readl(&lp->lan_saa9730_regs->MacCtl) & MAC_CONTROL_RESET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 i++;
518 if (i > 100) {
519 printk
520 ("Error: lan_sa9730_stop: MAC reset timeout\n");
521 return -1;
522 }
523 mdelay(1); /* wait 1 ms. */
524 }
525
526 return 0;
527}
528
529static int lan_saa9730_dma_init(struct lan_saa9730_private *lp)
530{
531 /* Stop lan controller. */
532 lan_saa9730_stop(lp);
533
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000534 writel(LAN_SAA9730_DEFAULT_TIME_OUT_CNT,
535 &lp->lan_saa9730_regs->Timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 return 0;
538}
539
540static int lan_saa9730_start(struct lan_saa9730_private *lp)
541{
542 lan_saa9730_buffer_init(lp);
543
544 /* Initialize Rx Buffer Index */
545 lp->NextRcvPacketIndex = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000546 lp->NextRcvBufferIndex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Ralf Baechle05d9c842005-11-09 17:10:05 +0000548 /* Set current buffer index & next available packet index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 lp->NextTxmPacketIndex = 0;
550 lp->NextTxmBufferIndex = 0;
551 lp->PendingTxmPacketIndex = 0;
552 lp->PendingTxmBufferIndex = 0;
553
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000554 writel(readl(&lp->lan_saa9730_regs->LanDmaCtl) | DMA_CTL_EN_TX_DMA |
555 DMA_CTL_EN_RX_DMA, &lp->lan_saa9730_regs->LanDmaCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* For Tx, turn on MAC then DMA */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000558 writel(readl(&lp->lan_saa9730_regs->TxCtl) | TX_CTL_TX_EN,
559 &lp->lan_saa9730_regs->TxCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* For Rx, turn on DMA then MAC */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000562 writel(readl(&lp->lan_saa9730_regs->RxCtl) | RX_CTL_RX_EN,
563 &lp->lan_saa9730_regs->RxCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Ralf Baechle05d9c842005-11-09 17:10:05 +0000565 /* Set Ok2Use to let hardware own the buffers. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000566 writel(OK2USE_RX_A | OK2USE_RX_B, &lp->lan_saa9730_regs->Ok2Use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 return 0;
569}
570
571static int lan_saa9730_restart(struct lan_saa9730_private *lp)
572{
573 lan_saa9730_stop(lp);
574 lan_saa9730_start(lp);
575
576 return 0;
577}
578
579static int lan_saa9730_tx(struct net_device *dev)
580{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000581 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 unsigned int *pPacket;
583 unsigned int tx_status;
584
585 if (lan_saa9730_debug > 5)
586 printk("lan_saa9730_tx interrupt\n");
587
588 /* Clear interrupt. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000589 writel(DMA_STATUS_MAC_TX_INT, &lp->lan_saa9730_regs->DmaStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 while (1) {
Ralf Baechle05d9c842005-11-09 17:10:05 +0000592 pPacket = lp->TxmBuffer[lp->PendingTxmBufferIndex]
593 [lp->PendingTxmPacketIndex];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /* Get status of first packet transmitted. */
596 tx_status = le32_to_cpu(*pPacket);
597
598 /* Check ownership. */
599 if ((tx_status & TX_STAT_CTL_OWNER_MSK) !=
600 (TXSF_HWDONE << TX_STAT_CTL_OWNER_SHF)) break;
601
602 /* Check for error. */
603 if (tx_status & TX_STAT_CTL_ERROR_MSK) {
604 if (lan_saa9730_debug > 1)
605 printk("lan_saa9730_tx: tx error = %x\n",
606 tx_status);
607
608 lp->stats.tx_errors++;
609 if (tx_status &
610 (TX_STATUS_EX_COLL << TX_STAT_CTL_STATUS_SHF))
Ralf Baechle05d9c842005-11-09 17:10:05 +0000611 lp->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (tx_status &
Ralf Baechle05d9c842005-11-09 17:10:05 +0000613 (TX_STATUS_LATE_COLL << TX_STAT_CTL_STATUS_SHF))
614 lp->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (tx_status &
616 (TX_STATUS_L_CARR << TX_STAT_CTL_STATUS_SHF))
Ralf Baechle05d9c842005-11-09 17:10:05 +0000617 lp->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (tx_status &
619 (TX_STATUS_UNDER << TX_STAT_CTL_STATUS_SHF))
Ralf Baechle05d9c842005-11-09 17:10:05 +0000620 lp->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (tx_status &
622 (TX_STATUS_SQ_ERR << TX_STAT_CTL_STATUS_SHF))
Ralf Baechle05d9c842005-11-09 17:10:05 +0000623 lp->stats.tx_heartbeat_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 lp->stats.collisions +=
Ralf Baechle05d9c842005-11-09 17:10:05 +0000626 tx_status & TX_STATUS_TX_COLL_MSK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
629 /* Free buffer. */
630 *pPacket =
631 cpu_to_le32(TXSF_EMPTY << TX_STAT_CTL_OWNER_SHF);
632
633 /* Update pending index pointer. */
634 lp->PendingTxmPacketIndex++;
635 if (lp->PendingTxmPacketIndex >= LAN_SAA9730_TXM_Q_SIZE) {
636 lp->PendingTxmPacketIndex = 0;
637 lp->PendingTxmBufferIndex ^= 1;
638 }
639 }
640
Ralf Baechle05d9c842005-11-09 17:10:05 +0000641 /* The tx buffer is no longer full. */
642 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 return 0;
645}
646
647static int lan_saa9730_rx(struct net_device *dev)
648{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000649 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 int len = 0;
651 struct sk_buff *skb = 0;
652 unsigned int rx_status;
653 int BufferIndex;
654 int PacketIndex;
655 unsigned int *pPacket;
656 unsigned char *pData;
657
658 if (lan_saa9730_debug > 5)
659 printk("lan_saa9730_rx interrupt\n");
660
661 /* Clear receive interrupts. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000662 writel(DMA_STATUS_MAC_RX_INT | DMA_STATUS_RX_INT |
663 DMA_STATUS_RX_TO_INT, &lp->lan_saa9730_regs->DmaStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /* Address next packet */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000666 BufferIndex = lp->NextRcvBufferIndex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 PacketIndex = lp->NextRcvPacketIndex;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000668 pPacket = lp->RcvBuffer[BufferIndex][PacketIndex];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 rx_status = le32_to_cpu(*pPacket);
670
671 /* Process each packet. */
672 while ((rx_status & RX_STAT_CTL_OWNER_MSK) ==
673 (RXSF_HWDONE << RX_STAT_CTL_OWNER_SHF)) {
674 /* Check the rx status. */
675 if (rx_status & (RX_STATUS_GOOD << RX_STAT_CTL_STATUS_SHF)) {
676 /* Received packet is good. */
677 len = (rx_status & RX_STAT_CTL_LENGTH_MSK) >>
678 RX_STAT_CTL_LENGTH_SHF;
679
680 pData = (unsigned char *) pPacket;
681 pData += 4;
682 skb = dev_alloc_skb(len + 2);
683 if (skb == 0) {
684 printk
685 ("%s: Memory squeeze, deferring packet.\n",
686 dev->name);
687 lp->stats.rx_dropped++;
688 } else {
689 lp->stats.rx_bytes += len;
690 lp->stats.rx_packets++;
691 skb->dev = dev;
692 skb_reserve(skb, 2); /* 16 byte align */
693 skb_put(skb, len); /* make room */
694 eth_copy_and_sum(skb,
695 (unsigned char *) pData,
696 len, 0);
697 skb->protocol = eth_type_trans(skb, dev);
698 netif_rx(skb);
699 dev->last_rx = jiffies;
700 }
701 } else {
702 /* We got an error packet. */
703 if (lan_saa9730_debug > 2)
704 printk
705 ("lan_saa9730_rx: We got an error packet = %x\n",
706 rx_status);
707
708 lp->stats.rx_errors++;
709 if (rx_status &
710 (RX_STATUS_CRC_ERR << RX_STAT_CTL_STATUS_SHF))
Ralf Baechle05d9c842005-11-09 17:10:05 +0000711 lp->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (rx_status &
Ralf Baechle05d9c842005-11-09 17:10:05 +0000713 (RX_STATUS_ALIGN_ERR << RX_STAT_CTL_STATUS_SHF))
714 lp->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (rx_status &
716 (RX_STATUS_OVERFLOW << RX_STAT_CTL_STATUS_SHF))
Ralf Baechle05d9c842005-11-09 17:10:05 +0000717 lp->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (rx_status &
719 (RX_STATUS_LONG_ERR << RX_STAT_CTL_STATUS_SHF))
Ralf Baechle05d9c842005-11-09 17:10:05 +0000720 lp->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
723 /* Indicate we have processed the buffer. */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000724 *pPacket = cpu_to_le32(RXSF_READY << RX_STAT_CTL_OWNER_SHF);
725
726 /* Make sure A or B is available to hardware as appropriate. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000727 writel(BufferIndex ? OK2USE_RX_B : OK2USE_RX_A,
728 &lp->lan_saa9730_regs->Ok2Use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /* Go to next packet in sequence. */
731 lp->NextRcvPacketIndex++;
732 if (lp->NextRcvPacketIndex >= LAN_SAA9730_RCV_Q_SIZE) {
733 lp->NextRcvPacketIndex = 0;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000734 lp->NextRcvBufferIndex ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /* Address next packet */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000738 BufferIndex = lp->NextRcvBufferIndex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 PacketIndex = lp->NextRcvPacketIndex;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000740 pPacket = lp->RcvBuffer[BufferIndex][PacketIndex];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 rx_status = le32_to_cpu(*pPacket);
742 }
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return 0;
745}
746
David Howells7d12e782006-10-05 14:55:46 +0100747static irqreturn_t lan_saa9730_interrupt(const int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Jeff Garzikc31f28e2006-10-06 14:56:04 -0400749 struct net_device *dev = dev_id;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000750 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 if (lan_saa9730_debug > 5)
753 printk("lan_saa9730_interrupt\n");
754
755 /* Disable the EVM LAN interrupt. */
756 evm_saa9730_block_lan_int(lp);
757
758 /* Clear the EVM LAN interrupt. */
759 evm_saa9730_clear_lan_int(lp);
760
761 /* Service pending transmit interrupts. */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000762 if (readl(&lp->lan_saa9730_regs->DmaStatus) & DMA_STATUS_MAC_TX_INT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 lan_saa9730_tx(dev);
764
765 /* Service pending receive interrupts. */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000766 if (readl(&lp->lan_saa9730_regs->DmaStatus) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 (DMA_STATUS_MAC_RX_INT | DMA_STATUS_RX_INT |
768 DMA_STATUS_RX_TO_INT)) lan_saa9730_rx(dev);
769
770 /* Enable the EVM LAN interrupt. */
771 evm_saa9730_unblock_lan_int(lp);
772
773 return IRQ_HANDLED;
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776static int lan_saa9730_open(struct net_device *dev)
777{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000778 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* Associate IRQ with lan_saa9730_interrupt */
781 if (request_irq(dev->irq, &lan_saa9730_interrupt, 0, "SAA9730 Eth",
782 dev)) {
783 printk("lan_saa9730_open: Can't get irq %d\n", dev->irq);
784 return -EAGAIN;
785 }
786
787 /* Enable the Lan interrupt in the event manager. */
788 evm_saa9730_enable_lan_int(lp);
789
790 /* Start the LAN controller */
791 if (lan_saa9730_start(lp))
792 return -1;
793
794 netif_start_queue(dev);
795
796 return 0;
797}
798
799static int lan_saa9730_write(struct lan_saa9730_private *lp,
800 struct sk_buff *skb, int skblen)
801{
802 unsigned char *pbData = skb->data;
803 unsigned int len = skblen;
804 unsigned char *pbPacketData;
805 unsigned int tx_status;
806 int BufferIndex;
807 int PacketIndex;
808
809 if (lan_saa9730_debug > 5)
Ralf Baechle05d9c842005-11-09 17:10:05 +0000810 printk("lan_saa9730_write: skb=%p\n", skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 BufferIndex = lp->NextTxmBufferIndex;
813 PacketIndex = lp->NextTxmPacketIndex;
814
Ralf Baechle05d9c842005-11-09 17:10:05 +0000815 tx_status = le32_to_cpu(*(unsigned int *)lp->TxmBuffer[BufferIndex]
816 [PacketIndex]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if ((tx_status & TX_STAT_CTL_OWNER_MSK) !=
818 (TXSF_EMPTY << TX_STAT_CTL_OWNER_SHF)) {
819 if (lan_saa9730_debug > 4)
820 printk
821 ("lan_saa9730_write: Tx buffer not available: tx_status = %x\n",
822 tx_status);
823 return -1;
824 }
825
826 lp->NextTxmPacketIndex++;
827 if (lp->NextTxmPacketIndex >= LAN_SAA9730_TXM_Q_SIZE) {
828 lp->NextTxmPacketIndex = 0;
829 lp->NextTxmBufferIndex ^= 1;
830 }
831
Ralf Baechle05d9c842005-11-09 17:10:05 +0000832 pbPacketData = lp->TxmBuffer[BufferIndex][PacketIndex];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 pbPacketData += 4;
834
835 /* copy the bits */
836 memcpy(pbPacketData, pbData, len);
837
838 /* Set transmit status for hardware */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000839 *(unsigned int *)lp->TxmBuffer[BufferIndex][PacketIndex] =
840 cpu_to_le32((TXSF_READY << TX_STAT_CTL_OWNER_SHF) |
841 (TX_STAT_CTL_INT_AFTER_TX <<
842 TX_STAT_CTL_FRAME_SHF) |
843 (len << TX_STAT_CTL_LENGTH_SHF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Ralf Baechle05d9c842005-11-09 17:10:05 +0000845 /* Make sure A or B is available to hardware as appropriate. */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000846 writel(BufferIndex ? OK2USE_TX_B : OK2USE_TX_A,
847 &lp->lan_saa9730_regs->Ok2Use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 return 0;
850}
851
852static void lan_saa9730_tx_timeout(struct net_device *dev)
853{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000854 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /* Transmitter timeout, serious problems */
857 lp->stats.tx_errors++;
858 printk("%s: transmit timed out, reset\n", dev->name);
859 /*show_saa9730_regs(lp); */
860 lan_saa9730_restart(lp);
861
862 dev->trans_start = jiffies;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000863 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866static int lan_saa9730_start_xmit(struct sk_buff *skb,
867 struct net_device *dev)
868{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000869 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 unsigned long flags;
871 int skblen;
872 int len;
873
874 if (lan_saa9730_debug > 4)
Ralf Baechle05d9c842005-11-09 17:10:05 +0000875 printk("Send packet: skb=%p\n", skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 skblen = skb->len;
878
879 spin_lock_irqsave(&lp->lock, flags);
880
881 len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
882
883 if (lan_saa9730_write(lp, skb, skblen)) {
884 spin_unlock_irqrestore(&lp->lock, flags);
Ralf Baechle05d9c842005-11-09 17:10:05 +0000885 printk("Error when writing packet to controller: skb=%p\n", skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 netif_stop_queue(dev);
887 return -1;
888 }
889
890 lp->stats.tx_bytes += len;
891 lp->stats.tx_packets++;
892
893 dev->trans_start = jiffies;
Ralf Baechle05d9c842005-11-09 17:10:05 +0000894 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 dev_kfree_skb(skb);
896
897 spin_unlock_irqrestore(&lp->lock, flags);
898
899 return 0;
900}
901
902static int lan_saa9730_close(struct net_device *dev)
903{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000904 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 if (lan_saa9730_debug > 1)
907 printk("lan_saa9730_close:\n");
908
909 netif_stop_queue(dev);
910
911 /* Disable the Lan interrupt in the event manager. */
912 evm_saa9730_disable_lan_int(lp);
913
914 /* Stop the controller */
915 if (lan_saa9730_stop(lp))
916 return -1;
917
918 free_irq(dev->irq, (void *) dev);
919
920 return 0;
921}
922
923static struct net_device_stats *lan_saa9730_get_stats(struct net_device
924 *dev)
925{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000926 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 return &lp->stats;
929}
930
931static void lan_saa9730_set_multicast(struct net_device *dev)
932{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000933 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* Stop the controller */
936 lan_saa9730_stop(lp);
937
938 if (dev->flags & IFF_PROMISC) {
939 /* accept all packets */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000940 writel(CAM_CONTROL_COMP_EN | CAM_CONTROL_STATION_ACC |
941 CAM_CONTROL_GROUP_ACC | CAM_CONTROL_BROAD_ACC,
942 &lp->lan_saa9730_regs->CamCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 } else {
944 if (dev->flags & IFF_ALLMULTI) {
945 /* accept all multicast packets */
Ralf Baechle69a43ac2007-03-20 12:40:09 +0000946 writel(CAM_CONTROL_COMP_EN | CAM_CONTROL_GROUP_ACC |
947 CAM_CONTROL_BROAD_ACC,
948 &lp->lan_saa9730_regs->CamCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 } else {
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000950 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * Will handle the multicast stuff later. -carstenl
952 */
953 }
954 }
955
956 lan_saa9730_restart(lp);
957}
958
959
960static void __devexit saa9730_remove_one(struct pci_dev *pdev)
961{
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000962 struct net_device *dev = pci_get_drvdata(pdev);
Ralf Baechle05d9c842005-11-09 17:10:05 +0000963 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000965 if (dev) {
966 unregister_netdev(dev);
Ralf Baechle05d9c842005-11-09 17:10:05 +0000967 lan_saa9730_free_buffers(pdev, lp);
968 iounmap(lp->lan_saa9730_regs);
969 iounmap(lp->evm_saa9730_regs);
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000970 free_netdev(dev);
971 pci_release_regions(pdev);
972 pci_disable_device(pdev);
973 pci_set_drvdata(pdev, NULL);
974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977
Ralf Baechle05d9c842005-11-09 17:10:05 +0000978static int lan_saa9730_init(struct net_device *dev, struct pci_dev *pdev,
979 unsigned long ioaddr, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Ralf Baechle05d9c842005-11-09 17:10:05 +0000981 struct lan_saa9730_private *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 unsigned char ethernet_addr[6];
Ralf Baechle05d9c842005-11-09 17:10:05 +0000983 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Ralf Baechle05d9c842005-11-09 17:10:05 +0000985 if (get_ethernet_addr(ethernet_addr)) {
986 ret = -ENODEV;
987 goto out;
988 }
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 memcpy(dev->dev_addr, ethernet_addr, 6);
991 dev->base_addr = ioaddr;
992 dev->irq = irq;
Ralf Baechle62ff0d02005-11-09 16:44:02 +0000993
Ralf Baechle05d9c842005-11-09 17:10:05 +0000994 lp->pci_dev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /* Set SAA9730 LAN base address. */
Ralf Baechle05d9c842005-11-09 17:10:05 +0000997 lp->lan_saa9730_regs = ioremap(ioaddr + SAA9730_LAN_REGS_ADDR,
998 SAA9730_LAN_REGS_SIZE);
999 if (!lp->lan_saa9730_regs) {
1000 ret = -ENOMEM;
1001 goto out;
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 /* Set SAA9730 EVM base address. */
Ralf Baechle05d9c842005-11-09 17:10:05 +00001005 lp->evm_saa9730_regs = ioremap(ioaddr + SAA9730_EVM_REGS_ADDR,
1006 SAA9730_EVM_REGS_SIZE);
1007 if (!lp->evm_saa9730_regs) {
1008 ret = -ENOMEM;
1009 goto out_iounmap_lan;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 /* Allocate LAN RX/TX frame buffer space. */
Ralf Baechle05d9c842005-11-09 17:10:05 +00001013 if ((ret = lan_saa9730_allocate_buffers(pdev, lp)))
1014 goto out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /* Stop LAN controller. */
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001017 if ((ret = lan_saa9730_stop(lp)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001018 goto out_free_consistent;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /* Initialize CAM registers. */
1021 if ((ret = lan_saa9730_cam_init(dev)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001022 goto out_free_consistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /* Initialize MII registers. */
1025 if ((ret = lan_saa9730_mii_init(lp)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001026 goto out_free_consistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 /* Initialize control registers. */
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001029 if ((ret = lan_saa9730_control_init(lp)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001030 goto out_free_consistent;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /* Load CAM registers. */
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001033 if ((ret = lan_saa9730_cam_load(lp)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001034 goto out_free_consistent;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 /* Initialize DMA context registers. */
1037 if ((ret = lan_saa9730_dma_init(lp)))
Ralf Baechle05d9c842005-11-09 17:10:05 +00001038 goto out_free_consistent;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 spin_lock_init(&lp->lock);
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 dev->open = lan_saa9730_open;
1043 dev->hard_start_xmit = lan_saa9730_start_xmit;
1044 dev->stop = lan_saa9730_close;
1045 dev->get_stats = lan_saa9730_get_stats;
1046 dev->set_multicast_list = lan_saa9730_set_multicast;
1047 dev->tx_timeout = lan_saa9730_tx_timeout;
1048 dev->watchdog_timeo = (HZ >> 1);
1049 dev->dma = 0;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001050
Ralf Baechle05d9c842005-11-09 17:10:05 +00001051 ret = register_netdev (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (ret)
Ralf Baechle05d9c842005-11-09 17:10:05 +00001053 goto out_free_consistent;
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return 0;
1056
Ralf Baechle05d9c842005-11-09 17:10:05 +00001057out_free_consistent:
1058 lan_saa9730_free_buffers(pdev, lp);
1059out_iounmap:
1060 iounmap(lp->evm_saa9730_regs);
1061out_iounmap_lan:
1062 iounmap(lp->lan_saa9730_regs);
1063out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return ret;
1065}
1066
1067
1068static int __devinit saa9730_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1069{
Ralf Baechle05d9c842005-11-09 17:10:05 +00001070 struct net_device *dev = NULL;
1071 unsigned long pci_ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 int err;
1073
1074 if (lan_saa9730_debug > 1)
1075 printk("saa9730.c: PCI bios is present, checking for devices...\n");
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 err = pci_enable_device(pdev);
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001078 if (err) {
1079 printk(KERN_ERR "Cannot enable PCI device, aborting.\n");
Ralf Baechle05d9c842005-11-09 17:10:05 +00001080 goto out;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 err = pci_request_regions(pdev, DRV_MODULE_NAME);
1084 if (err) {
1085 printk(KERN_ERR "Cannot obtain PCI resources, aborting.\n");
Ralf Baechle05d9c842005-11-09 17:10:05 +00001086 goto out_disable_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
1089 pci_irq_line = pdev->irq;
1090 /* LAN base address in located at BAR 1. */
1091
1092 pci_ioaddr = pci_resource_start(pdev, 1);
1093 pci_set_master(pdev);
1094
Ralf Baechle05d9c842005-11-09 17:10:05 +00001095 printk("Found SAA9730 (PCI) at %lx, irq %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 pci_ioaddr, pci_irq_line);
1097
Ralf Baechle05d9c842005-11-09 17:10:05 +00001098 dev = alloc_etherdev(sizeof(struct lan_saa9730_private));
1099 if (!dev)
1100 goto out_disable_pdev;
1101
1102 err = lan_saa9730_init(dev, pdev, pci_ioaddr, pci_irq_line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (err) {
Ralf Baechle05d9c842005-11-09 17:10:05 +00001104 printk("LAN init failed");
1105 goto out_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
1108 pci_set_drvdata(pdev, dev);
1109 SET_NETDEV_DEV(dev, &pdev->dev);
1110 return 0;
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001111
Ralf Baechle05d9c842005-11-09 17:10:05 +00001112out_free_netdev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 free_netdev(dev);
Ralf Baechle05d9c842005-11-09 17:10:05 +00001114out_disable_pdev:
1115 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116out:
Ralf Baechle05d9c842005-11-09 17:10:05 +00001117 pci_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return err;
1119}
1120
1121
1122static struct pci_driver saa9730_driver = {
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001123 .name = DRV_MODULE_NAME,
1124 .id_table = saa9730_pci_tbl,
1125 .probe = saa9730_init_one,
1126 .remove = __devexit_p(saa9730_remove_one),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127};
1128
1129
1130static int __init saa9730_init(void)
1131{
Jeff Garzik29917622006-08-19 17:48:59 -04001132 return pci_register_driver(&saa9730_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135static void __exit saa9730_cleanup(void)
1136{
Ralf Baechle62ff0d02005-11-09 16:44:02 +00001137 pci_unregister_driver(&saa9730_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
1140module_init(saa9730_init);
1141module_exit(saa9730_cleanup);
1142
Ralf Baechle05d9c842005-11-09 17:10:05 +00001143MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
1144MODULE_DESCRIPTION("Philips SAA9730 ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145MODULE_LICENSE("GPL");