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 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
);
75 static void evm_saa9730_disable_lan_int(struct lan_saa9730_private
*lp
)
77 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
);
83 static void evm_saa9730_clear_lan_int(struct lan_saa9730_private
*lp
)
85 writel(EVM_LAN_INT
, &lp
->evm_saa9730_regs
->InterruptStatus1
);
88 static void evm_saa9730_block_lan_int(struct lan_saa9730_private
*lp
)
90 writel(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 writel(readl(&lp
->evm_saa9730_regs
->InterruptBlock1
) | EVM_LAN_INT
,
97 &lp
->evm_saa9730_regs
->InterruptBlock1
);
100 static void __used
show_saa9730_regs(struct net_device
*dev
)
102 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
105 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]);
110 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
]));
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
]));
124 printk("lp->evm_saa9730_regs->InterruptBlock1 = %x\n",
125 readl(&lp
->evm_saa9730_regs
->InterruptBlock1
));
126 printk("lp->evm_saa9730_regs->InterruptStatus1 = %x\n",
127 readl(&lp
->evm_saa9730_regs
->InterruptStatus1
));
128 printk("lp->evm_saa9730_regs->InterruptEnable1 = %x\n",
129 readl(&lp
->evm_saa9730_regs
->InterruptEnable1
));
130 printk("lp->lan_saa9730_regs->Ok2Use = %x\n",
131 readl(&lp
->lan_saa9730_regs
->Ok2Use
));
132 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",
139 readl(&lp
->lan_saa9730_regs
->LanDmaCtl
));
140 printk("lp->lan_saa9730_regs->DmaStatus = %x\n",
141 readl(&lp
->lan_saa9730_regs
->DmaStatus
));
142 printk("lp->lan_saa9730_regs->CamCtl = %x\n",
143 readl(&lp
->lan_saa9730_regs
->CamCtl
));
144 printk("lp->lan_saa9730_regs->TxCtl = %x\n",
145 readl(&lp
->lan_saa9730_regs
->TxCtl
));
146 printk("lp->lan_saa9730_regs->TxStatus = %x\n",
147 readl(&lp
->lan_saa9730_regs
->TxStatus
));
148 printk("lp->lan_saa9730_regs->RxCtl = %x\n",
149 readl(&lp
->lan_saa9730_regs
->RxCtl
));
150 printk("lp->lan_saa9730_regs->RxStatus = %x\n",
151 readl(&lp
->lan_saa9730_regs
->RxStatus
));
153 for (i
= 0; i
< LAN_SAA9730_CAM_DWORDS
; i
++) {
154 writel(i
, &lp
->lan_saa9730_regs
->CamAddress
);
155 printk("lp->lan_saa9730_regs->CamData = %x\n",
156 readl(&lp
->lan_saa9730_regs
->CamData
));
159 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
);
173 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
);
184 printk("lp->lan_saa9730_regs->DebugPCIMasterAddr = %x\n",
185 readl(&lp
->lan_saa9730_regs
->DebugPCIMasterAddr
));
186 printk("lp->lan_saa9730_regs->DebugLanTxStateMachine = %x\n",
187 readl(&lp
->lan_saa9730_regs
->DebugLanTxStateMachine
));
188 printk("lp->lan_saa9730_regs->DebugLanRxStateMachine = %x\n",
189 readl(&lp
->lan_saa9730_regs
->DebugLanRxStateMachine
));
190 printk("lp->lan_saa9730_regs->DebugLanTxFifoPointers = %x\n",
191 readl(&lp
->lan_saa9730_regs
->DebugLanTxFifoPointers
));
192 printk("lp->lan_saa9730_regs->DebugLanRxFifoPointers = %x\n",
193 readl(&lp
->lan_saa9730_regs
->DebugLanRxFifoPointers
));
194 printk("lp->lan_saa9730_regs->DebugLanCtlStateMachine = %x\n",
195 readl(&lp
->lan_saa9730_regs
->DebugLanCtlStateMachine
));
198 static void lan_saa9730_buffer_init(struct lan_saa9730_private
*lp
)
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
);
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
);
221 static void lan_saa9730_free_buffers(struct pci_dev
*pdev
,
222 struct lan_saa9730_private
*lp
)
224 pci_free_consistent(pdev
, lp
->buffer_size
, lp
->buffer_start
,
228 static int lan_saa9730_allocate_buffers(struct pci_dev
*pdev
,
229 struct lan_saa9730_private
*lp
)
232 unsigned int i
, j
, rxoffset
, txoffset
;
235 /* Initialize buffer space */
236 lp
->DmaRcvPackets
= LAN_SAA9730_RCV_Q_SIZE
;
237 lp
->DmaTxmPackets
= LAN_SAA9730_TXM_Q_SIZE
;
239 /* Initialize Rx Buffer Index */
240 lp
->NextRcvPacketIndex
= 0;
241 lp
->NextRcvBufferIndex
= 0;
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;
250 * Allocate all RX and TX packets in one chunk.
251 * The Rx and Tx packets must be PACKET_SIZE aligned.
253 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
,
258 if (!lp
->buffer_start
) {
263 Pa
= (void *)ALIGN((unsigned long)lp
->buffer_start
,
264 LAN_SAA9730_PACKET_SIZE
);
266 rxoffset
= Pa
- lp
->buffer_start
;
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
);
274 lp
->RcvBuffer
[i
][j
] = Pa
;
275 Pa
+= LAN_SAA9730_PACKET_SIZE
;
279 txoffset
= Pa
- lp
->buffer_start
;
281 /* 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
);
287 lp
->TxmBuffer
[i
][j
] = Pa
;
288 Pa
+= LAN_SAA9730_PACKET_SIZE
;
293 * Set rx buffer A and rx buffer B to point to the first two buffer
296 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
);
302 * Set txm_buf_a and txm_buf_b to point to the first two buffer
305 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
);
311 /* Set packet number */
312 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
);
324 static int lan_saa9730_cam_load(struct lan_saa9730_private
*lp
)
327 unsigned char *NetworkAddress
;
329 NetworkAddress
= (unsigned char *) &lp
->PhysicalAddress
[0][0];
331 for (i
= 0; i
< LAN_SAA9730_CAM_DWORDS
; i
++) {
332 /* First set address to where data is written */
333 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
);
342 static int lan_saa9730_cam_init(struct net_device
*dev
)
344 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
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);
356 static int lan_saa9730_mii_init(struct lan_saa9730_private
*lp
)
360 /* Check link status, spin here till station is not busy. */
362 while (readl(&lp
->lan_saa9730_regs
->StationMgmtCtl
) & MD_CA_BUSY
) {
365 printk("Error: lan_saa9730_mii_init: timeout\n");
368 mdelay(1); /* wait 1 ms. */
371 /* Now set the control and address register. */
372 writel(MD_CA_BUSY
| PHY_STATUS
| PHY_ADDRESS
<< MD_CA_PHY_SHF
,
373 &lp
->lan_saa9730_regs
->StationMgmtCtl
);
375 /* check link status, spin here till station is not busy */
377 while (readl(&lp
->lan_saa9730_regs
->StationMgmtCtl
) & MD_CA_BUSY
) {
380 printk("Error: lan_saa9730_mii_init: timeout\n");
383 mdelay(1); /* wait 1 ms. */
389 /* Check the link status. */
390 if (readl(&lp
->lan_saa9730_regs
->StationMgmtData
) &
391 PHY_STATUS_LINK_UP
) {
395 /* Link is down, reset the PHY first. */
397 /* set PHY address = 'CONTROL' */
398 writel(PHY_ADDRESS
<< MD_CA_PHY_SHF
| MD_CA_WR
| PHY_CONTROL
,
399 &lp
->lan_saa9730_regs
->StationMgmtCtl
);
404 /* set 'CONTROL' = force reset and renegotiate */
405 writel(PHY_CONTROL_RESET
| PHY_CONTROL_AUTO_NEG
|
406 PHY_CONTROL_RESTART_AUTO_NEG
,
407 &lp
->lan_saa9730_regs
->StationMgmtData
);
409 /* Wait for 50 ms. */
412 /* set 'BUSY' to start operation */
413 writel(MD_CA_BUSY
| PHY_ADDRESS
<< MD_CA_PHY_SHF
| MD_CA_WR
|
414 PHY_CONTROL
, &lp
->lan_saa9730_regs
->StationMgmtCtl
);
416 /* await completion */
418 while (readl(&lp
->lan_saa9730_regs
->StationMgmtCtl
) &
423 ("Error: lan_saa9730_mii_init: timeout\n");
426 mdelay(1); /* wait 1 ms. */
432 for (l
= 0; l
< 2; l
++) {
433 /* set PHY address = 'STATUS' */
434 writel(MD_CA_BUSY
| PHY_ADDRESS
<< MD_CA_PHY_SHF
|
436 &lp
->lan_saa9730_regs
->StationMgmtCtl
);
438 /* await completion */
440 while (readl(&lp
->lan_saa9730_regs
->StationMgmtCtl
) &
445 ("Error: lan_saa9730_mii_init: timeout\n");
448 mdelay(1); /* wait 1 ms. */
451 /* wait for 3 sec. */
454 /* check the link status */
455 if (readl(&lp
->lan_saa9730_regs
->StationMgmtData
) &
456 PHY_STATUS_LINK_UP
) {
466 static int lan_saa9730_control_init(struct lan_saa9730_private
*lp
)
468 /* Initialize DMA control register. */
469 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
);
476 /* Initial MAC control register. */
477 writel((MACCM_MII
<< MAC_CONTROL_CONN_SHF
) | MAC_CONTROL_FULL_DUP
,
478 &lp
->lan_saa9730_regs
->MacCtl
);
480 /* Initialize CAM control register. */
481 writel(CAM_CONTROL_COMP_EN
| CAM_CONTROL_BROAD_ACC
,
482 &lp
->lan_saa9730_regs
->CamCtl
);
485 * Initialize CAM enable register, only turn on first entry, should
488 writel(0x0001, &lp
->lan_saa9730_regs
->CamEnable
);
490 /* Initialize Tx control register */
491 writel(TX_CTL_EN_COMP
, &lp
->lan_saa9730_regs
->TxCtl
);
493 /* Initialize Rcv control register */
494 writel(RX_CTL_STRIP_CRC
, &lp
->lan_saa9730_regs
->RxCtl
);
496 /* Reset DMA engine */
497 writel(DMA_TEST_SW_RESET
, &lp
->lan_saa9730_regs
->DmaTest
);
502 static int lan_saa9730_stop(struct lan_saa9730_private
*lp
)
507 writel(readl(&lp
->lan_saa9730_regs
->LanDmaCtl
) &
508 ~(DMA_CTL_EN_TX_DMA
| DMA_CTL_EN_RX_DMA
),
509 &lp
->lan_saa9730_regs
->LanDmaCtl
);
511 /* Set the SW Reset bits in DMA and MAC control registers */
512 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
);
517 * Wait for MAC reset to have finished. The reset bit is auto cleared
518 * when the reset is done.
521 while (readl(&lp
->lan_saa9730_regs
->MacCtl
) & MAC_CONTROL_RESET
) {
525 ("Error: lan_sa9730_stop: MAC reset timeout\n");
528 mdelay(1); /* wait 1 ms. */
534 static int lan_saa9730_dma_init(struct lan_saa9730_private
*lp
)
536 /* Stop lan controller. */
537 lan_saa9730_stop(lp
);
539 writel(LAN_SAA9730_DEFAULT_TIME_OUT_CNT
,
540 &lp
->lan_saa9730_regs
->Timeout
);
545 static int lan_saa9730_start(struct lan_saa9730_private
*lp
)
547 lan_saa9730_buffer_init(lp
);
549 /* Initialize Rx Buffer Index */
550 lp
->NextRcvPacketIndex
= 0;
551 lp
->NextRcvBufferIndex
= 0;
553 /* Set current buffer index & next available packet index */
554 lp
->NextTxmPacketIndex
= 0;
555 lp
->NextTxmBufferIndex
= 0;
556 lp
->PendingTxmPacketIndex
= 0;
557 lp
->PendingTxmBufferIndex
= 0;
559 writel(readl(&lp
->lan_saa9730_regs
->LanDmaCtl
) | DMA_CTL_EN_TX_DMA
|
560 DMA_CTL_EN_RX_DMA
, &lp
->lan_saa9730_regs
->LanDmaCtl
);
562 /* For Tx, turn on MAC then DMA */
563 writel(readl(&lp
->lan_saa9730_regs
->TxCtl
) | TX_CTL_TX_EN
,
564 &lp
->lan_saa9730_regs
->TxCtl
);
566 /* For Rx, turn on DMA then MAC */
567 writel(readl(&lp
->lan_saa9730_regs
->RxCtl
) | RX_CTL_RX_EN
,
568 &lp
->lan_saa9730_regs
->RxCtl
);
570 /* Set Ok2Use to let hardware own the buffers. */
571 writel(OK2USE_RX_A
| OK2USE_RX_B
, &lp
->lan_saa9730_regs
->Ok2Use
);
576 static int lan_saa9730_restart(struct lan_saa9730_private
*lp
)
578 lan_saa9730_stop(lp
);
579 lan_saa9730_start(lp
);
584 static int lan_saa9730_tx(struct net_device
*dev
)
586 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
587 unsigned int *pPacket
;
588 unsigned int tx_status
;
590 if (lan_saa9730_debug
> 5)
591 printk("lan_saa9730_tx interrupt\n");
593 /* Clear interrupt. */
594 writel(DMA_STATUS_MAC_TX_INT
, &lp
->lan_saa9730_regs
->DmaStatus
);
597 pPacket
= lp
->TxmBuffer
[lp
->PendingTxmBufferIndex
]
598 [lp
->PendingTxmPacketIndex
];
600 /* Get status of first packet transmitted. */
601 tx_status
= le32_to_cpu(*pPacket
);
603 /* Check ownership. */
604 if ((tx_status
& TX_STAT_CTL_OWNER_MSK
) !=
605 (TXSF_HWDONE
<< TX_STAT_CTL_OWNER_SHF
)) break;
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",
613 dev
->stats
.tx_errors
++;
615 (TX_STATUS_EX_COLL
<< TX_STAT_CTL_STATUS_SHF
))
616 dev
->stats
.tx_aborted_errors
++;
618 (TX_STATUS_LATE_COLL
<< TX_STAT_CTL_STATUS_SHF
))
619 dev
->stats
.tx_window_errors
++;
621 (TX_STATUS_L_CARR
<< TX_STAT_CTL_STATUS_SHF
))
622 dev
->stats
.tx_carrier_errors
++;
624 (TX_STATUS_UNDER
<< TX_STAT_CTL_STATUS_SHF
))
625 dev
->stats
.tx_fifo_errors
++;
627 (TX_STATUS_SQ_ERR
<< TX_STAT_CTL_STATUS_SHF
))
628 dev
->stats
.tx_heartbeat_errors
++;
630 dev
->stats
.collisions
+=
631 tx_status
& TX_STATUS_TX_COLL_MSK
;
636 cpu_to_le32(TXSF_EMPTY
<< TX_STAT_CTL_OWNER_SHF
);
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;
646 /* The tx buffer is no longer full. */
647 netif_wake_queue(dev
);
652 static int lan_saa9730_rx(struct net_device
*dev
)
654 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
656 struct sk_buff
*skb
= 0;
657 unsigned int rx_status
;
660 unsigned int *pPacket
;
661 unsigned char *pData
;
663 if (lan_saa9730_debug
> 5)
664 printk("lan_saa9730_rx interrupt\n");
666 /* Clear receive interrupts. */
667 writel(DMA_STATUS_MAC_RX_INT
| DMA_STATUS_RX_INT
|
668 DMA_STATUS_RX_TO_INT
, &lp
->lan_saa9730_regs
->DmaStatus
);
670 /* Address next packet */
671 BufferIndex
= lp
->NextRcvBufferIndex
;
672 PacketIndex
= lp
->NextRcvPacketIndex
;
673 pPacket
= lp
->RcvBuffer
[BufferIndex
][PacketIndex
];
674 rx_status
= le32_to_cpu(*pPacket
);
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
;
685 pData
= (unsigned char *) pPacket
;
687 skb
= dev_alloc_skb(len
+ 2);
690 ("%s: Memory squeeze, deferring packet.\n",
692 dev
->stats
.rx_dropped
++;
694 dev
->stats
.rx_bytes
+= len
;
695 dev
->stats
.rx_packets
++;
696 skb_reserve(skb
, 2); /* 16 byte align */
697 skb_put(skb
, len
); /* make room */
698 skb_copy_to_linear_data(skb
,
699 (unsigned char *) pData
,
701 skb
->protocol
= eth_type_trans(skb
, dev
);
703 dev
->last_rx
= jiffies
;
706 /* We got an error packet. */
707 if (lan_saa9730_debug
> 2)
709 ("lan_saa9730_rx: We got an error packet = %x\n",
712 dev
->stats
.rx_errors
++;
714 (RX_STATUS_CRC_ERR
<< RX_STAT_CTL_STATUS_SHF
))
715 dev
->stats
.rx_crc_errors
++;
717 (RX_STATUS_ALIGN_ERR
<< RX_STAT_CTL_STATUS_SHF
))
718 dev
->stats
.rx_frame_errors
++;
720 (RX_STATUS_OVERFLOW
<< RX_STAT_CTL_STATUS_SHF
))
721 dev
->stats
.rx_fifo_errors
++;
723 (RX_STATUS_LONG_ERR
<< RX_STAT_CTL_STATUS_SHF
))
724 dev
->stats
.rx_length_errors
++;
727 /* Indicate we have processed the buffer. */
728 *pPacket
= cpu_to_le32(RXSF_READY
<< RX_STAT_CTL_OWNER_SHF
);
730 /* Make sure A or B is available to hardware as appropriate. */
731 writel(BufferIndex
? OK2USE_RX_B
: OK2USE_RX_A
,
732 &lp
->lan_saa9730_regs
->Ok2Use
);
734 /* Go to next packet in sequence. */
735 lp
->NextRcvPacketIndex
++;
736 if (lp
->NextRcvPacketIndex
>= LAN_SAA9730_RCV_Q_SIZE
) {
737 lp
->NextRcvPacketIndex
= 0;
738 lp
->NextRcvBufferIndex
^= 1;
741 /* Address next packet */
742 BufferIndex
= lp
->NextRcvBufferIndex
;
743 PacketIndex
= lp
->NextRcvPacketIndex
;
744 pPacket
= lp
->RcvBuffer
[BufferIndex
][PacketIndex
];
745 rx_status
= le32_to_cpu(*pPacket
);
751 static irqreturn_t
lan_saa9730_interrupt(const int irq
, void *dev_id
)
753 struct net_device
*dev
= dev_id
;
754 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
756 if (lan_saa9730_debug
> 5)
757 printk("lan_saa9730_interrupt\n");
759 /* Disable the EVM LAN interrupt. */
760 evm_saa9730_block_lan_int(lp
);
762 /* Clear the EVM LAN interrupt. */
763 evm_saa9730_clear_lan_int(lp
);
765 /* Service pending transmit interrupts. */
766 if (readl(&lp
->lan_saa9730_regs
->DmaStatus
) & DMA_STATUS_MAC_TX_INT
)
769 /* Service pending receive interrupts. */
770 if (readl(&lp
->lan_saa9730_regs
->DmaStatus
) &
771 (DMA_STATUS_MAC_RX_INT
| DMA_STATUS_RX_INT
|
772 DMA_STATUS_RX_TO_INT
)) lan_saa9730_rx(dev
);
774 /* Enable the EVM LAN interrupt. */
775 evm_saa9730_unblock_lan_int(lp
);
780 static int lan_saa9730_open(struct net_device
*dev
)
782 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
784 /* Associate IRQ with lan_saa9730_interrupt */
785 if (request_irq(dev
->irq
, &lan_saa9730_interrupt
, 0, "SAA9730 Eth",
787 printk("lan_saa9730_open: Can't get irq %d\n", dev
->irq
);
791 /* Enable the Lan interrupt in the event manager. */
792 evm_saa9730_enable_lan_int(lp
);
794 /* Start the LAN controller */
795 if (lan_saa9730_start(lp
))
798 netif_start_queue(dev
);
803 static int lan_saa9730_write(struct lan_saa9730_private
*lp
,
804 struct sk_buff
*skb
, int skblen
)
806 unsigned char *pbData
= skb
->data
;
807 unsigned int len
= skblen
;
808 unsigned char *pbPacketData
;
809 unsigned int tx_status
;
813 if (lan_saa9730_debug
> 5)
814 printk("lan_saa9730_write: skb=%p\n", skb
);
816 BufferIndex
= lp
->NextTxmBufferIndex
;
817 PacketIndex
= lp
->NextTxmPacketIndex
;
819 tx_status
= le32_to_cpu(*(unsigned int *)lp
->TxmBuffer
[BufferIndex
]
821 if ((tx_status
& TX_STAT_CTL_OWNER_MSK
) !=
822 (TXSF_EMPTY
<< TX_STAT_CTL_OWNER_SHF
)) {
823 if (lan_saa9730_debug
> 4)
825 ("lan_saa9730_write: Tx buffer not available: tx_status = %x\n",
830 lp
->NextTxmPacketIndex
++;
831 if (lp
->NextTxmPacketIndex
>= LAN_SAA9730_TXM_Q_SIZE
) {
832 lp
->NextTxmPacketIndex
= 0;
833 lp
->NextTxmBufferIndex
^= 1;
836 pbPacketData
= lp
->TxmBuffer
[BufferIndex
][PacketIndex
];
840 memcpy(pbPacketData
, pbData
, len
);
842 /* Set transmit status for hardware */
843 *(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
));
849 /* Make sure A or B is available to hardware as appropriate. */
850 writel(BufferIndex
? OK2USE_TX_B
: OK2USE_TX_A
,
851 &lp
->lan_saa9730_regs
->Ok2Use
);
856 static void lan_saa9730_tx_timeout(struct net_device
*dev
)
858 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
860 /* Transmitter timeout, serious problems */
861 dev
->stats
.tx_errors
++;
862 printk("%s: transmit timed out, reset\n", dev
->name
);
863 /*show_saa9730_regs(dev); */
864 lan_saa9730_restart(lp
);
866 dev
->trans_start
= jiffies
;
867 netif_wake_queue(dev
);
870 static int lan_saa9730_start_xmit(struct sk_buff
*skb
,
871 struct net_device
*dev
)
873 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
878 if (lan_saa9730_debug
> 4)
879 printk("Send packet: skb=%p\n", skb
);
883 spin_lock_irqsave(&lp
->lock
, flags
);
885 len
= (skblen
<= ETH_ZLEN
) ? ETH_ZLEN
: skblen
;
887 if (lan_saa9730_write(lp
, skb
, skblen
)) {
888 spin_unlock_irqrestore(&lp
->lock
, flags
);
889 printk("Error when writing packet to controller: skb=%p\n", skb
);
890 netif_stop_queue(dev
);
894 dev
->stats
.tx_bytes
+= len
;
895 dev
->stats
.tx_packets
++;
897 dev
->trans_start
= jiffies
;
898 netif_wake_queue(dev
);
901 spin_unlock_irqrestore(&lp
->lock
, flags
);
906 static int lan_saa9730_close(struct net_device
*dev
)
908 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
910 if (lan_saa9730_debug
> 1)
911 printk("lan_saa9730_close:\n");
913 netif_stop_queue(dev
);
915 /* Disable the Lan interrupt in the event manager. */
916 evm_saa9730_disable_lan_int(lp
);
918 /* Stop the controller */
919 if (lan_saa9730_stop(lp
))
922 free_irq(dev
->irq
, (void *) dev
);
927 static void lan_saa9730_set_multicast(struct net_device
*dev
)
929 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
931 /* Stop the controller */
932 lan_saa9730_stop(lp
);
934 if (dev
->flags
& IFF_PROMISC
) {
935 /* accept all packets */
936 writel(CAM_CONTROL_COMP_EN
| CAM_CONTROL_STATION_ACC
|
937 CAM_CONTROL_GROUP_ACC
| CAM_CONTROL_BROAD_ACC
,
938 &lp
->lan_saa9730_regs
->CamCtl
);
940 if (dev
->flags
& IFF_ALLMULTI
|| dev
->mc_count
) {
941 /* accept all multicast packets */
943 * Will handle the multicast stuff later. -carstenl
945 writel(CAM_CONTROL_COMP_EN
| CAM_CONTROL_GROUP_ACC
|
946 CAM_CONTROL_BROAD_ACC
,
947 &lp
->lan_saa9730_regs
->CamCtl
);
951 lan_saa9730_restart(lp
);
955 static void __devexit
saa9730_remove_one(struct pci_dev
*pdev
)
957 struct net_device
*dev
= pci_get_drvdata(pdev
);
958 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
961 unregister_netdev(dev
);
962 lan_saa9730_free_buffers(pdev
, lp
);
963 iounmap(lp
->lan_saa9730_regs
);
964 iounmap(lp
->evm_saa9730_regs
);
966 pci_release_regions(pdev
);
967 pci_disable_device(pdev
);
968 pci_set_drvdata(pdev
, NULL
);
973 static int lan_saa9730_init(struct net_device
*dev
, struct pci_dev
*pdev
,
974 unsigned long ioaddr
, int irq
)
976 struct lan_saa9730_private
*lp
= netdev_priv(dev
);
977 unsigned char ethernet_addr
[6];
980 if (get_ethernet_addr(ethernet_addr
)) {
985 memcpy(dev
->dev_addr
, ethernet_addr
, 6);
986 dev
->base_addr
= ioaddr
;
991 /* Set SAA9730 LAN base address. */
992 lp
->lan_saa9730_regs
= ioremap(ioaddr
+ SAA9730_LAN_REGS_ADDR
,
993 SAA9730_LAN_REGS_SIZE
);
994 if (!lp
->lan_saa9730_regs
) {
999 /* Set SAA9730 EVM base address. */
1000 lp
->evm_saa9730_regs
= ioremap(ioaddr
+ SAA9730_EVM_REGS_ADDR
,
1001 SAA9730_EVM_REGS_SIZE
);
1002 if (!lp
->evm_saa9730_regs
) {
1004 goto out_iounmap_lan
;
1007 /* Allocate LAN RX/TX frame buffer space. */
1008 if ((ret
= lan_saa9730_allocate_buffers(pdev
, lp
)))
1011 /* Stop LAN controller. */
1012 if ((ret
= lan_saa9730_stop(lp
)))
1013 goto out_free_consistent
;
1015 /* Initialize CAM registers. */
1016 if ((ret
= lan_saa9730_cam_init(dev
)))
1017 goto out_free_consistent
;
1019 /* Initialize MII registers. */
1020 if ((ret
= lan_saa9730_mii_init(lp
)))
1021 goto out_free_consistent
;
1023 /* Initialize control registers. */
1024 if ((ret
= lan_saa9730_control_init(lp
)))
1025 goto out_free_consistent
;
1027 /* Load CAM registers. */
1028 if ((ret
= lan_saa9730_cam_load(lp
)))
1029 goto out_free_consistent
;
1031 /* Initialize DMA context registers. */
1032 if ((ret
= lan_saa9730_dma_init(lp
)))
1033 goto out_free_consistent
;
1035 spin_lock_init(&lp
->lock
);
1037 dev
->open
= lan_saa9730_open
;
1038 dev
->hard_start_xmit
= lan_saa9730_start_xmit
;
1039 dev
->stop
= lan_saa9730_close
;
1040 dev
->set_multicast_list
= lan_saa9730_set_multicast
;
1041 dev
->tx_timeout
= lan_saa9730_tx_timeout
;
1042 dev
->watchdog_timeo
= (HZ
>> 1);
1045 ret
= register_netdev (dev
);
1047 goto out_free_consistent
;
1051 out_free_consistent
:
1052 lan_saa9730_free_buffers(pdev
, lp
);
1054 iounmap(lp
->evm_saa9730_regs
);
1056 iounmap(lp
->lan_saa9730_regs
);
1062 static int __devinit
saa9730_init_one(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
1064 struct net_device
*dev
= NULL
;
1065 unsigned long pci_ioaddr
;
1068 if (lan_saa9730_debug
> 1)
1069 printk("saa9730.c: PCI bios is present, checking for devices...\n");
1071 err
= pci_enable_device(pdev
);
1073 printk(KERN_ERR
"Cannot enable PCI device, aborting.\n");
1077 err
= pci_request_regions(pdev
, DRV_MODULE_NAME
);
1079 printk(KERN_ERR
"Cannot obtain PCI resources, aborting.\n");
1080 goto out_disable_pdev
;
1083 pci_irq_line
= pdev
->irq
;
1084 /* LAN base address in located at BAR 1. */
1086 pci_ioaddr
= pci_resource_start(pdev
, 1);
1087 pci_set_master(pdev
);
1089 printk("Found SAA9730 (PCI) at %lx, irq %d.\n",
1090 pci_ioaddr
, pci_irq_line
);
1092 dev
= alloc_etherdev(sizeof(struct lan_saa9730_private
));
1094 goto out_disable_pdev
;
1096 err
= lan_saa9730_init(dev
, pdev
, pci_ioaddr
, pci_irq_line
);
1098 printk("LAN init failed");
1099 goto out_free_netdev
;
1102 pci_set_drvdata(pdev
, dev
);
1103 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1109 pci_disable_device(pdev
);
1111 pci_set_drvdata(pdev
, NULL
);
1116 static struct pci_driver saa9730_driver
= {
1117 .name
= DRV_MODULE_NAME
,
1118 .id_table
= saa9730_pci_tbl
,
1119 .probe
= saa9730_init_one
,
1120 .remove
= __devexit_p(saa9730_remove_one
),
1124 static int __init
saa9730_init(void)
1126 return pci_register_driver(&saa9730_driver
);
1129 static void __exit
saa9730_cleanup(void)
1131 pci_unregister_driver(&saa9730_driver
);
1134 module_init(saa9730_init
);
1135 module_exit(saa9730_cleanup
);
1137 MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
1138 MODULE_DESCRIPTION("Philips SAA9730 ethernet driver");
1139 MODULE_LICENSE("GPL");