2 * 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>
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.
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
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.
20 * SAA9730 ethernet driver.
23 * Angelo Dell'Aera <buffer@antifork.org> : Conversion to the new PCI API
25 * Conversion to spinlocks.
26 * Error handling fixes.
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>
37 #include <linux/types.h>
39 #include <asm/addrspace.h>
42 #include <asm/mips-boards/prom.h>
46 #ifdef LAN_SAA9730_DEBUG
47 int lan_saa9730_debug
= LAN_SAA9730_DEBUG
;
49 int lan_saa9730_debug
;
52 #define DRV_MODULE_NAME "saa9730"
54 static struct pci_device_id saa9730_pci_tbl
[] = {
55 { PCI_VENDOR_ID_PHILIPS
, PCI_DEVICE_ID_PHILIPS_SAA9730
,
56 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
60 MODULE_DEVICE_TABLE(pci
, saa9730_pci_tbl
);
62 /* Non-zero only if the current card is a PCI with BIOS-set IRQ. */
63 static unsigned int pci_irq_line
;
65 static void evm_saa9730_enable_lan_int(struct lan_saa9730_private
*lp
)
67 outl(readl(&lp
->evm_saa9730_regs
->InterruptBlock1
) | EVM_LAN_INT
,
68 &lp
->evm_saa9730_regs
->InterruptBlock1
);
69 outl(readl(&lp
->evm_saa9730_regs
->InterruptStatus1
) | EVM_LAN_INT
,
70 &lp
->evm_saa9730_regs
->InterruptStatus1
);
71 outl(readl(&lp
->evm_saa9730_regs
->InterruptEnable1
) | EVM_LAN_INT
|
72 EVM_MASTER_EN
, &lp
->evm_saa9730_regs
->InterruptEnable1
);
75 static void evm_saa9730_disable_lan_int(struct lan_saa9730_private
*lp
)
77 outl(readl(&lp
->evm_saa9730_regs
->InterruptBlock1
) & ~EVM_LAN_INT
,
78 &lp
->evm_saa9730_regs
->InterruptBlock1
);
79 outl(readl(&lp
->evm_saa9730_regs
->InterruptEnable1
) & ~EVM_LAN_INT
,
80 &lp
->evm_saa9730_regs
->InterruptEnable1
);
83 static void evm_saa9730_clear_lan_int(struct lan_saa9730_private
*lp
)
85 outl(EVM_LAN_INT
, &lp
->evm_saa9730_regs
->InterruptStatus1
);
88 static void evm_saa9730_block_lan_int(struct lan_saa9730_private
*lp
)
90 outl(readl(&lp
->evm_saa9730_regs
->InterruptBlock1
) & ~EVM_LAN_INT
,
91 &lp
->evm_saa9730_regs
->InterruptBlock1
);
94 static void evm_saa9730_unblock_lan_int(struct lan_saa9730_private
*lp
)
96 outl(readl(&lp
->evm_saa9730_regs
->InterruptBlock1
) | EVM_LAN_INT
,
97 &lp
->evm_saa9730_regs
->InterruptBlock1
);
100 static void __attribute_used__
show_saa9730_regs(struct lan_saa9730_private
*lp
)
103 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]);
107 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
]));
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
]));
121 printk("lp->evm_saa9730_regs->InterruptBlock1 = %x\n",
122 readl(&lp
->evm_saa9730_regs
->InterruptBlock1
));
123 printk("lp->evm_saa9730_regs->InterruptStatus1 = %x\n",
124 readl(&lp
->evm_saa9730_regs
->InterruptStatus1
));
125 printk("lp->evm_saa9730_regs->InterruptEnable1 = %x\n",
126 readl(&lp
->evm_saa9730_regs
->InterruptEnable1
));
127 printk("lp->lan_saa9730_regs->Ok2Use = %x\n",
128 readl(&lp
->lan_saa9730_regs
->Ok2Use
));
129 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",
136 readl(&lp
->lan_saa9730_regs
->LanDmaCtl
));
137 printk("lp->lan_saa9730_regs->DmaStatus = %x\n",
138 readl(&lp
->lan_saa9730_regs
->DmaStatus
));
139 printk("lp->lan_saa9730_regs->CamCtl = %x\n",
140 readl(&lp
->lan_saa9730_regs
->CamCtl
));
141 printk("lp->lan_saa9730_regs->TxCtl = %x\n",
142 readl(&lp
->lan_saa9730_regs
->TxCtl
));
143 printk("lp->lan_saa9730_regs->TxStatus = %x\n",
144 readl(&lp
->lan_saa9730_regs
->TxStatus
));
145 printk("lp->lan_saa9730_regs->RxCtl = %x\n",
146 readl(&lp
->lan_saa9730_regs
->RxCtl
));
147 printk("lp->lan_saa9730_regs->RxStatus = %x\n",
148 readl(&lp
->lan_saa9730_regs
->RxStatus
));
149 for (i
= 0; i
< LAN_SAA9730_CAM_DWORDS
; i
++) {
150 outl(i
, &lp
->lan_saa9730_regs
->CamAddress
);
151 printk("lp->lan_saa9730_regs->CamData = %x\n",
152 readl(&lp
->lan_saa9730_regs
->CamData
));
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
);
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
);
179 printk("lp->lan_saa9730_regs->DebugPCIMasterAddr = %x\n",
180 readl(&lp
->lan_saa9730_regs
->DebugPCIMasterAddr
));
181 printk("lp->lan_saa9730_regs->DebugLanTxStateMachine = %x\n",
182 readl(&lp
->lan_saa9730_regs
->DebugLanTxStateMachine
));
183 printk("lp->lan_saa9730_regs->DebugLanRxStateMachine = %x\n",
184 readl(&lp
->lan_saa9730_regs
->DebugLanRxStateMachine
));
185 printk("lp->lan_saa9730_regs->DebugLanTxFifoPointers = %x\n",
186 readl(&lp
->lan_saa9730_regs
->DebugLanTxFifoPointers
));
187 printk("lp->lan_saa9730_regs->DebugLanRxFifoPointers = %x\n",
188 readl(&lp
->lan_saa9730_regs
->DebugLanRxFifoPointers
));
189 printk("lp->lan_saa9730_regs->DebugLanCtlStateMachine = %x\n",
190 readl(&lp
->lan_saa9730_regs
->DebugLanCtlStateMachine
));
193 static void lan_saa9730_buffer_init(struct lan_saa9730_private
*lp
)
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
);
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
);
216 static void lan_saa9730_free_buffers(struct pci_dev
*pdev
,
217 struct lan_saa9730_private
*lp
)
219 pci_free_consistent(pdev
, lp
->buffer_size
, lp
->buffer_start
,
223 static int lan_saa9730_allocate_buffers(struct pci_dev
*pdev
,
224 struct lan_saa9730_private
*lp
)
227 unsigned int i
, j
, rxoffset
, txoffset
;
230 /* Initialize buffer space */
231 lp
->DmaRcvPackets
= LAN_SAA9730_RCV_Q_SIZE
;
232 lp
->DmaTxmPackets
= LAN_SAA9730_TXM_Q_SIZE
;
234 /* Initialize Rx Buffer Index */
235 lp
->NextRcvPacketIndex
= 0;
236 lp
->NextRcvBufferIndex
= 0;
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;
245 * Allocate all RX and TX packets in one chunk.
246 * The Rx and Tx packets must be PACKET_SIZE aligned.
248 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
,
253 if (!lp
->buffer_start
) {
258 Pa
= (void *)ALIGN((unsigned long)lp
->buffer_start
,
259 LAN_SAA9730_PACKET_SIZE
);
261 rxoffset
= Pa
- lp
->buffer_start
;
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
);
269 lp
->RcvBuffer
[i
][j
] = Pa
;
270 Pa
+= LAN_SAA9730_PACKET_SIZE
;
274 txoffset
= Pa
- lp
->buffer_start
;
276 /* 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
);
282 lp
->TxmBuffer
[i
][j
] = Pa
;
283 Pa
+= LAN_SAA9730_PACKET_SIZE
;
288 * Set rx buffer A and rx buffer B to point to the first two buffer
291 outl(lp
->dma_addr
+ rxoffset
,
292 &lp
->lan_saa9730_regs
->RxBuffA
);
293 outl(lp
->dma_addr
+ rxoffset
+
294 LAN_SAA9730_PACKET_SIZE
* LAN_SAA9730_RCV_Q_SIZE
,
295 &lp
->lan_saa9730_regs
->RxBuffB
);
298 * Set txm_buf_a and txm_buf_b to point to the first two buffer
301 outl(lp
->dma_addr
+ txoffset
,
302 &lp
->lan_saa9730_regs
->TxBuffA
);
303 outl(lp
->dma_addr
+ txoffset
+
304 LAN_SAA9730_PACKET_SIZE
* LAN_SAA9730_TXM_Q_SIZE
,
305 &lp
->lan_saa9730_regs
->TxBuffB
);
307 /* Set packet number */
308 outl((lp
->DmaRcvPackets
<< PK_COUNT_RX_A_SHF
) |
309 (lp
->DmaRcvPackets
<< PK_COUNT_RX_B_SHF
) |
310 (lp
->DmaTxmPackets
<< PK_COUNT_TX_A_SHF
) |
311 (lp
->DmaTxmPackets
<< PK_COUNT_TX_B_SHF
),
312 &lp
->lan_saa9730_regs
->PacketCount
);
320 static int lan_saa9730_cam_load(struct lan_saa9730_private
*lp
)
323 unsigned char *NetworkAddress
;
325 NetworkAddress
= (unsigned char *) &lp
->PhysicalAddress
[0][0];
327 for (i
= 0; i
< LAN_SAA9730_CAM_DWORDS
; i
++) {
328 /* First set address to where data is written */
329 outl(i
, &lp
->lan_saa9730_regs
->CamAddress
);
330 outl((NetworkAddress
[0] << 24) | (NetworkAddress
[1] << 16)
331 | (NetworkAddress
[2] << 8) | NetworkAddress
[3],
332 &lp
->lan_saa9730_regs
->CamData
);
338 static int lan_saa9730_cam_init(struct net_device
*dev
)
340 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
343 /* Copy MAC-address into all entries. */
344 for (i
= 0; i
< LAN_SAA9730_CAM_ENTRIES
; i
++) {
345 memcpy((unsigned char *) lp
->PhysicalAddress
[i
],
346 (unsigned char *) dev
->dev_addr
, 6);
352 static int lan_saa9730_mii_init(struct lan_saa9730_private
*lp
)
356 /* Check link status, spin here till station is not busy. */
358 while (readl(&lp
->lan_saa9730_regs
->StationMgmtCtl
) & MD_CA_BUSY
) {
361 printk("Error: lan_saa9730_mii_init: timeout\n");
364 mdelay(1); /* wait 1 ms. */
367 /* Now set the control and address register. */
368 outl(MD_CA_BUSY
| PHY_STATUS
| PHY_ADDRESS
<< MD_CA_PHY_SHF
,
369 &lp
->lan_saa9730_regs
->StationMgmtCtl
);
371 /* check link status, spin here till station is not busy */
373 while (readl(&lp
->lan_saa9730_regs
->StationMgmtCtl
) & MD_CA_BUSY
) {
376 printk("Error: lan_saa9730_mii_init: timeout\n");
379 mdelay(1); /* wait 1 ms. */
385 /* Check the link status. */
386 if (readl(&lp
->lan_saa9730_regs
->StationMgmtData
) &
387 PHY_STATUS_LINK_UP
) {
391 /* Link is down, reset the PHY first. */
393 /* set PHY address = 'CONTROL' */
394 outl(PHY_ADDRESS
<< MD_CA_PHY_SHF
| MD_CA_WR
| PHY_CONTROL
,
395 &lp
->lan_saa9730_regs
->StationMgmtCtl
);
400 /* set 'CONTROL' = force reset and renegotiate */
401 outl(PHY_CONTROL_RESET
| PHY_CONTROL_AUTO_NEG
|
402 PHY_CONTROL_RESTART_AUTO_NEG
,
403 &lp
->lan_saa9730_regs
->StationMgmtData
);
405 /* Wait for 50 ms. */
408 /* set 'BUSY' to start operation */
409 outl(MD_CA_BUSY
| PHY_ADDRESS
<< MD_CA_PHY_SHF
| MD_CA_WR
|
410 PHY_CONTROL
, &lp
->lan_saa9730_regs
->StationMgmtCtl
);
412 /* await completion */
414 while (readl(&lp
->lan_saa9730_regs
->StationMgmtCtl
) &
419 ("Error: lan_saa9730_mii_init: timeout\n");
422 mdelay(1); /* wait 1 ms. */
428 for (l
= 0; l
< 2; l
++) {
429 /* set PHY address = 'STATUS' */
430 outl(MD_CA_BUSY
| PHY_ADDRESS
<< MD_CA_PHY_SHF
|
432 &lp
->lan_saa9730_regs
->StationMgmtCtl
);
434 /* await completion */
436 while (readl(&lp
->lan_saa9730_regs
->StationMgmtCtl
) &
441 ("Error: lan_saa9730_mii_init: timeout\n");
444 mdelay(1); /* wait 1 ms. */
447 /* wait for 3 sec. */
450 /* check the link status */
451 if (readl(&lp
->lan_saa9730_regs
->StationMgmtData
) &
452 PHY_STATUS_LINK_UP
) {
462 static int lan_saa9730_control_init(struct lan_saa9730_private
*lp
)
464 /* Initialize DMA control register. */
465 outl((LANMB_ANY
<< DMA_CTL_MAX_XFER_SHF
) |
466 (LANEND_LITTLE
<< DMA_CTL_ENDIAN_SHF
) |
467 (LAN_SAA9730_RCV_Q_INT_THRESHOLD
<< DMA_CTL_RX_INT_COUNT_SHF
)
468 | DMA_CTL_RX_INT_TO_EN
| DMA_CTL_RX_INT_EN
|
469 DMA_CTL_MAC_RX_INT_EN
| DMA_CTL_MAC_TX_INT_EN
,
470 &lp
->lan_saa9730_regs
->LanDmaCtl
);
472 /* Initial MAC control register. */
473 outl((MACCM_MII
<< MAC_CONTROL_CONN_SHF
) | MAC_CONTROL_FULL_DUP
,
474 &lp
->lan_saa9730_regs
->MacCtl
);
476 /* Initialize CAM control register. */
477 outl(CAM_CONTROL_COMP_EN
| CAM_CONTROL_BROAD_ACC
,
478 &lp
->lan_saa9730_regs
->CamCtl
);
481 * Initialize CAM enable register, only turn on first entry, should
484 outl(0x0001, &lp
->lan_saa9730_regs
->CamEnable
);
486 /* Initialize Tx control register */
487 outl(TX_CTL_EN_COMP
, &lp
->lan_saa9730_regs
->TxCtl
);
489 /* Initialize Rcv control register */
490 outl(RX_CTL_STRIP_CRC
, &lp
->lan_saa9730_regs
->RxCtl
);
492 /* Reset DMA engine */
493 outl(DMA_TEST_SW_RESET
, &lp
->lan_saa9730_regs
->DmaTest
);
498 static int lan_saa9730_stop(struct lan_saa9730_private
*lp
)
503 outl(readl(&lp
->lan_saa9730_regs
->LanDmaCtl
) &
504 ~(DMA_CTL_EN_TX_DMA
| DMA_CTL_EN_RX_DMA
),
505 &lp
->lan_saa9730_regs
->LanDmaCtl
);
507 /* Set the SW Reset bits in DMA and MAC control registers */
508 outl(DMA_TEST_SW_RESET
, &lp
->lan_saa9730_regs
->DmaTest
);
509 outl(readl(&lp
->lan_saa9730_regs
->MacCtl
) | MAC_CONTROL_RESET
,
510 &lp
->lan_saa9730_regs
->MacCtl
);
513 * Wait for MAC reset to have finished. The reset bit is auto cleared
514 * when the reset is done.
517 while (readl(&lp
->lan_saa9730_regs
->MacCtl
) & MAC_CONTROL_RESET
) {
521 ("Error: lan_sa9730_stop: MAC reset timeout\n");
524 mdelay(1); /* wait 1 ms. */
530 static int lan_saa9730_dma_init(struct lan_saa9730_private
*lp
)
532 /* Stop lan controller. */
533 lan_saa9730_stop(lp
);
535 outl(LAN_SAA9730_DEFAULT_TIME_OUT_CNT
,
536 &lp
->lan_saa9730_regs
->Timeout
);
541 static int lan_saa9730_start(struct lan_saa9730_private
*lp
)
543 lan_saa9730_buffer_init(lp
);
545 /* Initialize Rx Buffer Index */
546 lp
->NextRcvPacketIndex
= 0;
547 lp
->NextRcvBufferIndex
= 0;
549 /* Set current buffer index & next available packet index */
550 lp
->NextTxmPacketIndex
= 0;
551 lp
->NextTxmBufferIndex
= 0;
552 lp
->PendingTxmPacketIndex
= 0;
553 lp
->PendingTxmBufferIndex
= 0;
555 outl(readl(&lp
->lan_saa9730_regs
->LanDmaCtl
) | DMA_CTL_EN_TX_DMA
|
556 DMA_CTL_EN_RX_DMA
, &lp
->lan_saa9730_regs
->LanDmaCtl
);
558 /* For Tx, turn on MAC then DMA */
559 outl(readl(&lp
->lan_saa9730_regs
->TxCtl
) | TX_CTL_TX_EN
,
560 &lp
->lan_saa9730_regs
->TxCtl
);
562 /* For Rx, turn on DMA then MAC */
563 outl(readl(&lp
->lan_saa9730_regs
->RxCtl
) | RX_CTL_RX_EN
,
564 &lp
->lan_saa9730_regs
->RxCtl
);
566 /* Set Ok2Use to let hardware own the buffers. */
567 outl(OK2USE_RX_A
| OK2USE_RX_B
, &lp
->lan_saa9730_regs
->Ok2Use
);
572 static int lan_saa9730_restart(struct lan_saa9730_private
*lp
)
574 lan_saa9730_stop(lp
);
575 lan_saa9730_start(lp
);
580 static int lan_saa9730_tx(struct net_device
*dev
)
582 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
583 unsigned int *pPacket
;
584 unsigned int tx_status
;
586 if (lan_saa9730_debug
> 5)
587 printk("lan_saa9730_tx interrupt\n");
589 /* Clear interrupt. */
590 outl(DMA_STATUS_MAC_TX_INT
, &lp
->lan_saa9730_regs
->DmaStatus
);
593 pPacket
= lp
->TxmBuffer
[lp
->PendingTxmBufferIndex
]
594 [lp
->PendingTxmPacketIndex
];
596 /* Get status of first packet transmitted. */
597 tx_status
= le32_to_cpu(*pPacket
);
599 /* Check ownership. */
600 if ((tx_status
& TX_STAT_CTL_OWNER_MSK
) !=
601 (TXSF_HWDONE
<< TX_STAT_CTL_OWNER_SHF
)) break;
603 /* Check for error. */
604 if (tx_status
& TX_STAT_CTL_ERROR_MSK
) {
605 if (lan_saa9730_debug
> 1)
606 printk("lan_saa9730_tx: tx error = %x\n",
609 lp
->stats
.tx_errors
++;
611 (TX_STATUS_EX_COLL
<< TX_STAT_CTL_STATUS_SHF
))
612 lp
->stats
.tx_aborted_errors
++;
614 (TX_STATUS_LATE_COLL
<< TX_STAT_CTL_STATUS_SHF
))
615 lp
->stats
.tx_window_errors
++;
617 (TX_STATUS_L_CARR
<< TX_STAT_CTL_STATUS_SHF
))
618 lp
->stats
.tx_carrier_errors
++;
620 (TX_STATUS_UNDER
<< TX_STAT_CTL_STATUS_SHF
))
621 lp
->stats
.tx_fifo_errors
++;
623 (TX_STATUS_SQ_ERR
<< TX_STAT_CTL_STATUS_SHF
))
624 lp
->stats
.tx_heartbeat_errors
++;
626 lp
->stats
.collisions
+=
627 tx_status
& TX_STATUS_TX_COLL_MSK
;
632 cpu_to_le32(TXSF_EMPTY
<< TX_STAT_CTL_OWNER_SHF
);
634 /* Update pending index pointer. */
635 lp
->PendingTxmPacketIndex
++;
636 if (lp
->PendingTxmPacketIndex
>= LAN_SAA9730_TXM_Q_SIZE
) {
637 lp
->PendingTxmPacketIndex
= 0;
638 lp
->PendingTxmBufferIndex
^= 1;
642 /* The tx buffer is no longer full. */
643 netif_wake_queue(dev
);
648 static int lan_saa9730_rx(struct net_device
*dev
)
650 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
652 struct sk_buff
*skb
= 0;
653 unsigned int rx_status
;
656 unsigned int *pPacket
;
657 unsigned char *pData
;
659 if (lan_saa9730_debug
> 5)
660 printk("lan_saa9730_rx interrupt\n");
662 /* Clear receive interrupts. */
663 outl(DMA_STATUS_MAC_RX_INT
| DMA_STATUS_RX_INT
|
664 DMA_STATUS_RX_TO_INT
, &lp
->lan_saa9730_regs
->DmaStatus
);
666 /* Address next packet */
667 BufferIndex
= lp
->NextRcvBufferIndex
;
668 PacketIndex
= lp
->NextRcvPacketIndex
;
669 pPacket
= lp
->RcvBuffer
[BufferIndex
][PacketIndex
];
670 rx_status
= le32_to_cpu(*pPacket
);
672 /* Process each packet. */
673 while ((rx_status
& RX_STAT_CTL_OWNER_MSK
) ==
674 (RXSF_HWDONE
<< RX_STAT_CTL_OWNER_SHF
)) {
675 /* Check the rx status. */
676 if (rx_status
& (RX_STATUS_GOOD
<< RX_STAT_CTL_STATUS_SHF
)) {
677 /* Received packet is good. */
678 len
= (rx_status
& RX_STAT_CTL_LENGTH_MSK
) >>
679 RX_STAT_CTL_LENGTH_SHF
;
681 pData
= (unsigned char *) pPacket
;
683 skb
= dev_alloc_skb(len
+ 2);
686 ("%s: Memory squeeze, deferring packet.\n",
688 lp
->stats
.rx_dropped
++;
690 lp
->stats
.rx_bytes
+= len
;
691 lp
->stats
.rx_packets
++;
693 skb_reserve(skb
, 2); /* 16 byte align */
694 skb_put(skb
, len
); /* make room */
695 eth_copy_and_sum(skb
,
696 (unsigned char *) pData
,
698 skb
->protocol
= eth_type_trans(skb
, dev
);
700 dev
->last_rx
= jiffies
;
703 /* We got an error packet. */
704 if (lan_saa9730_debug
> 2)
706 ("lan_saa9730_rx: We got an error packet = %x\n",
709 lp
->stats
.rx_errors
++;
711 (RX_STATUS_CRC_ERR
<< RX_STAT_CTL_STATUS_SHF
))
712 lp
->stats
.rx_crc_errors
++;
714 (RX_STATUS_ALIGN_ERR
<< RX_STAT_CTL_STATUS_SHF
))
715 lp
->stats
.rx_frame_errors
++;
717 (RX_STATUS_OVERFLOW
<< RX_STAT_CTL_STATUS_SHF
))
718 lp
->stats
.rx_fifo_errors
++;
720 (RX_STATUS_LONG_ERR
<< RX_STAT_CTL_STATUS_SHF
))
721 lp
->stats
.rx_length_errors
++;
724 /* Indicate we have processed the buffer. */
725 *pPacket
= cpu_to_le32(RXSF_READY
<< RX_STAT_CTL_OWNER_SHF
);
727 /* Make sure A or B is available to hardware as appropriate. */
728 outl(BufferIndex
? OK2USE_RX_B
: OK2USE_RX_A
,
729 &lp
->lan_saa9730_regs
->Ok2Use
);
731 /* Go to next packet in sequence. */
732 lp
->NextRcvPacketIndex
++;
733 if (lp
->NextRcvPacketIndex
>= LAN_SAA9730_RCV_Q_SIZE
) {
734 lp
->NextRcvPacketIndex
= 0;
735 lp
->NextRcvBufferIndex
^= 1;
738 /* Address next packet */
739 BufferIndex
= lp
->NextRcvBufferIndex
;
740 PacketIndex
= lp
->NextRcvPacketIndex
;
741 pPacket
= lp
->RcvBuffer
[BufferIndex
][PacketIndex
];
742 rx_status
= le32_to_cpu(*pPacket
);
748 static irqreturn_t
lan_saa9730_interrupt(const int irq
, void *dev_id
,
749 struct pt_regs
*regs
)
751 struct net_device
*dev
= (struct net_device
*) dev_id
;
752 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
754 if (lan_saa9730_debug
> 5)
755 printk("lan_saa9730_interrupt\n");
757 /* Disable the EVM LAN interrupt. */
758 evm_saa9730_block_lan_int(lp
);
760 /* Clear the EVM LAN interrupt. */
761 evm_saa9730_clear_lan_int(lp
);
763 /* Service pending transmit interrupts. */
764 if (readl(&lp
->lan_saa9730_regs
->DmaStatus
) & DMA_STATUS_MAC_TX_INT
)
767 /* Service pending receive interrupts. */
768 if (readl(&lp
->lan_saa9730_regs
->DmaStatus
) &
769 (DMA_STATUS_MAC_RX_INT
| DMA_STATUS_RX_INT
|
770 DMA_STATUS_RX_TO_INT
)) lan_saa9730_rx(dev
);
772 /* Enable the EVM LAN interrupt. */
773 evm_saa9730_unblock_lan_int(lp
);
778 static int lan_saa9730_open(struct net_device
*dev
)
780 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
782 /* Associate IRQ with lan_saa9730_interrupt */
783 if (request_irq(dev
->irq
, &lan_saa9730_interrupt
, 0, "SAA9730 Eth",
785 printk("lan_saa9730_open: Can't get irq %d\n", dev
->irq
);
789 /* Enable the Lan interrupt in the event manager. */
790 evm_saa9730_enable_lan_int(lp
);
792 /* Start the LAN controller */
793 if (lan_saa9730_start(lp
))
796 netif_start_queue(dev
);
801 static int lan_saa9730_write(struct lan_saa9730_private
*lp
,
802 struct sk_buff
*skb
, int skblen
)
804 unsigned char *pbData
= skb
->data
;
805 unsigned int len
= skblen
;
806 unsigned char *pbPacketData
;
807 unsigned int tx_status
;
811 if (lan_saa9730_debug
> 5)
812 printk("lan_saa9730_write: skb=%p\n", skb
);
814 BufferIndex
= lp
->NextTxmBufferIndex
;
815 PacketIndex
= lp
->NextTxmPacketIndex
;
817 tx_status
= le32_to_cpu(*(unsigned int *)lp
->TxmBuffer
[BufferIndex
]
819 if ((tx_status
& TX_STAT_CTL_OWNER_MSK
) !=
820 (TXSF_EMPTY
<< TX_STAT_CTL_OWNER_SHF
)) {
821 if (lan_saa9730_debug
> 4)
823 ("lan_saa9730_write: Tx buffer not available: tx_status = %x\n",
828 lp
->NextTxmPacketIndex
++;
829 if (lp
->NextTxmPacketIndex
>= LAN_SAA9730_TXM_Q_SIZE
) {
830 lp
->NextTxmPacketIndex
= 0;
831 lp
->NextTxmBufferIndex
^= 1;
834 pbPacketData
= lp
->TxmBuffer
[BufferIndex
][PacketIndex
];
838 memcpy(pbPacketData
, pbData
, len
);
840 /* Set transmit status for hardware */
841 *(unsigned int *)lp
->TxmBuffer
[BufferIndex
][PacketIndex
] =
842 cpu_to_le32((TXSF_READY
<< TX_STAT_CTL_OWNER_SHF
) |
843 (TX_STAT_CTL_INT_AFTER_TX
<<
844 TX_STAT_CTL_FRAME_SHF
) |
845 (len
<< TX_STAT_CTL_LENGTH_SHF
));
847 /* Make sure A or B is available to hardware as appropriate. */
848 outl(BufferIndex
? OK2USE_TX_B
: OK2USE_TX_A
,
849 &lp
->lan_saa9730_regs
->Ok2Use
);
854 static void lan_saa9730_tx_timeout(struct net_device
*dev
)
856 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
858 /* Transmitter timeout, serious problems */
859 lp
->stats
.tx_errors
++;
860 printk("%s: transmit timed out, reset\n", dev
->name
);
861 /*show_saa9730_regs(lp); */
862 lan_saa9730_restart(lp
);
864 dev
->trans_start
= jiffies
;
865 netif_wake_queue(dev
);
868 static int lan_saa9730_start_xmit(struct sk_buff
*skb
,
869 struct net_device
*dev
)
871 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
876 if (lan_saa9730_debug
> 4)
877 printk("Send packet: skb=%p\n", skb
);
881 spin_lock_irqsave(&lp
->lock
, flags
);
883 len
= (skblen
<= ETH_ZLEN
) ? ETH_ZLEN
: skblen
;
885 if (lan_saa9730_write(lp
, skb
, skblen
)) {
886 spin_unlock_irqrestore(&lp
->lock
, flags
);
887 printk("Error when writing packet to controller: skb=%p\n", skb
);
888 netif_stop_queue(dev
);
892 lp
->stats
.tx_bytes
+= len
;
893 lp
->stats
.tx_packets
++;
895 dev
->trans_start
= jiffies
;
896 netif_wake_queue(dev
);
899 spin_unlock_irqrestore(&lp
->lock
, flags
);
904 static int lan_saa9730_close(struct net_device
*dev
)
906 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
908 if (lan_saa9730_debug
> 1)
909 printk("lan_saa9730_close:\n");
911 netif_stop_queue(dev
);
913 /* Disable the Lan interrupt in the event manager. */
914 evm_saa9730_disable_lan_int(lp
);
916 /* Stop the controller */
917 if (lan_saa9730_stop(lp
))
920 free_irq(dev
->irq
, (void *) dev
);
925 static struct net_device_stats
*lan_saa9730_get_stats(struct net_device
928 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
933 static void lan_saa9730_set_multicast(struct net_device
*dev
)
935 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
937 /* Stop the controller */
938 lan_saa9730_stop(lp
);
940 if (dev
->flags
& IFF_PROMISC
) {
941 /* accept all packets */
942 outl(CAM_CONTROL_COMP_EN
| CAM_CONTROL_STATION_ACC
|
943 CAM_CONTROL_GROUP_ACC
| CAM_CONTROL_BROAD_ACC
,
944 &lp
->lan_saa9730_regs
->CamCtl
);
946 if (dev
->flags
& IFF_ALLMULTI
) {
947 /* accept all multicast packets */
948 outl(CAM_CONTROL_COMP_EN
| CAM_CONTROL_GROUP_ACC
|
949 CAM_CONTROL_BROAD_ACC
,
950 &lp
->lan_saa9730_regs
->CamCtl
);
953 * Will handle the multicast stuff later. -carstenl
958 lan_saa9730_restart(lp
);
962 static void __devexit
saa9730_remove_one(struct pci_dev
*pdev
)
964 struct net_device
*dev
= pci_get_drvdata(pdev
);
965 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
968 unregister_netdev(dev
);
969 lan_saa9730_free_buffers(pdev
, lp
);
970 iounmap(lp
->lan_saa9730_regs
);
971 iounmap(lp
->evm_saa9730_regs
);
973 pci_release_regions(pdev
);
974 pci_disable_device(pdev
);
975 pci_set_drvdata(pdev
, NULL
);
980 static int lan_saa9730_init(struct net_device
*dev
, struct pci_dev
*pdev
,
981 unsigned long ioaddr
, int irq
)
983 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
984 unsigned char ethernet_addr
[6];
987 if (get_ethernet_addr(ethernet_addr
)) {
992 memcpy(dev
->dev_addr
, ethernet_addr
, 6);
993 dev
->base_addr
= ioaddr
;
998 /* Set SAA9730 LAN base address. */
999 lp
->lan_saa9730_regs
= ioremap(ioaddr
+ SAA9730_LAN_REGS_ADDR
,
1000 SAA9730_LAN_REGS_SIZE
);
1001 if (!lp
->lan_saa9730_regs
) {
1006 /* Set SAA9730 EVM base address. */
1007 lp
->evm_saa9730_regs
= ioremap(ioaddr
+ SAA9730_EVM_REGS_ADDR
,
1008 SAA9730_EVM_REGS_SIZE
);
1009 if (!lp
->evm_saa9730_regs
) {
1011 goto out_iounmap_lan
;
1014 /* Allocate LAN RX/TX frame buffer space. */
1015 if ((ret
= lan_saa9730_allocate_buffers(pdev
, lp
)))
1018 /* Stop LAN controller. */
1019 if ((ret
= lan_saa9730_stop(lp
)))
1020 goto out_free_consistent
;
1022 /* Initialize CAM registers. */
1023 if ((ret
= lan_saa9730_cam_init(dev
)))
1024 goto out_free_consistent
;
1026 /* Initialize MII registers. */
1027 if ((ret
= lan_saa9730_mii_init(lp
)))
1028 goto out_free_consistent
;
1030 /* Initialize control registers. */
1031 if ((ret
= lan_saa9730_control_init(lp
)))
1032 goto out_free_consistent
;
1034 /* Load CAM registers. */
1035 if ((ret
= lan_saa9730_cam_load(lp
)))
1036 goto out_free_consistent
;
1038 /* Initialize DMA context registers. */
1039 if ((ret
= lan_saa9730_dma_init(lp
)))
1040 goto out_free_consistent
;
1042 spin_lock_init(&lp
->lock
);
1044 dev
->open
= lan_saa9730_open
;
1045 dev
->hard_start_xmit
= lan_saa9730_start_xmit
;
1046 dev
->stop
= lan_saa9730_close
;
1047 dev
->get_stats
= lan_saa9730_get_stats
;
1048 dev
->set_multicast_list
= lan_saa9730_set_multicast
;
1049 dev
->tx_timeout
= lan_saa9730_tx_timeout
;
1050 dev
->watchdog_timeo
= (HZ
>> 1);
1053 ret
= register_netdev (dev
);
1055 goto out_free_consistent
;
1059 out_free_consistent
:
1060 lan_saa9730_free_buffers(pdev
, lp
);
1062 iounmap(lp
->evm_saa9730_regs
);
1064 iounmap(lp
->lan_saa9730_regs
);
1070 static int __devinit
saa9730_init_one(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
1072 struct net_device
*dev
= NULL
;
1073 unsigned long pci_ioaddr
;
1076 if (lan_saa9730_debug
> 1)
1077 printk("saa9730.c: PCI bios is present, checking for devices...\n");
1079 err
= pci_enable_device(pdev
);
1081 printk(KERN_ERR
"Cannot enable PCI device, aborting.\n");
1085 err
= pci_request_regions(pdev
, DRV_MODULE_NAME
);
1087 printk(KERN_ERR
"Cannot obtain PCI resources, aborting.\n");
1088 goto out_disable_pdev
;
1091 pci_irq_line
= pdev
->irq
;
1092 /* LAN base address in located at BAR 1. */
1094 pci_ioaddr
= pci_resource_start(pdev
, 1);
1095 pci_set_master(pdev
);
1097 printk("Found SAA9730 (PCI) at %lx, irq %d.\n",
1098 pci_ioaddr
, pci_irq_line
);
1100 dev
= alloc_etherdev(sizeof(struct lan_saa9730_private
));
1102 goto out_disable_pdev
;
1104 err
= lan_saa9730_init(dev
, pdev
, pci_ioaddr
, pci_irq_line
);
1106 printk("LAN init failed");
1107 goto out_free_netdev
;
1110 pci_set_drvdata(pdev
, dev
);
1111 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1117 pci_disable_device(pdev
);
1119 pci_set_drvdata(pdev
, NULL
);
1124 static struct pci_driver saa9730_driver
= {
1125 .name
= DRV_MODULE_NAME
,
1126 .id_table
= saa9730_pci_tbl
,
1127 .probe
= saa9730_init_one
,
1128 .remove
= __devexit_p(saa9730_remove_one
),
1132 static int __init
saa9730_init(void)
1134 return pci_module_init(&saa9730_driver
);
1137 static void __exit
saa9730_cleanup(void)
1139 pci_unregister_driver(&saa9730_driver
);
1142 module_init(saa9730_init
);
1143 module_exit(saa9730_cleanup
);
1145 MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
1146 MODULE_DESCRIPTION("Philips SAA9730 ethernet driver");
1147 MODULE_LICENSE("GPL");