Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / tulip / xircom_cb.c
blob5a73752be2ca2cdf455968855f82ae5b3bfa8f9e
1 /*
2 * xircom_cb: A driver for the (tulip-like) Xircom Cardbus ethernet cards
4 * This software is (C) by the respective authors, and licensed under the GPL
5 * License.
7 * Written by Arjan van de Ven for Red Hat, Inc.
8 * Based on work by Jeff Garzik, Doug Ledford and Donald Becker
10 * This software may be used and distributed according to the terms
11 * of the GNU General Public License, incorporated herein by reference.
14 * $Id: xircom_cb.c,v 1.33 2001/03/19 14:02:07 arjanv Exp $
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/ioport.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/pci.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/delay.h>
31 #include <linux/init.h>
32 #include <linux/bitops.h>
34 #include <asm/uaccess.h>
35 #include <asm/io.h>
36 #ifdef CONFIG_NET_POLL_CONTROLLER
37 #include <asm/irq.h>
38 #endif
40 #ifdef DEBUG
41 #define enter(x) printk("Enter: %s, %s line %i\n",x,__FILE__,__LINE__)
42 #define leave(x) printk("Leave: %s, %s line %i\n",x,__FILE__,__LINE__)
43 #else
44 #define enter(x) do {} while (0)
45 #define leave(x) do {} while (0)
46 #endif
49 MODULE_DESCRIPTION("Xircom Cardbus ethernet driver");
50 MODULE_AUTHOR("Arjan van de Ven <arjanv@redhat.com>");
51 MODULE_LICENSE("GPL");
55 /* IO registers on the card, offsets */
56 #define CSR0 0x00
57 #define CSR1 0x08
58 #define CSR2 0x10
59 #define CSR3 0x18
60 #define CSR4 0x20
61 #define CSR5 0x28
62 #define CSR6 0x30
63 #define CSR7 0x38
64 #define CSR8 0x40
65 #define CSR9 0x48
66 #define CSR10 0x50
67 #define CSR11 0x58
68 #define CSR12 0x60
69 #define CSR13 0x68
70 #define CSR14 0x70
71 #define CSR15 0x78
72 #define CSR16 0x80
74 /* PCI registers */
75 #define PCI_POWERMGMT 0x40
77 /* Offsets of the buffers within the descriptor pages, in bytes */
79 #define NUMDESCRIPTORS 4
81 static int bufferoffsets[NUMDESCRIPTORS] = {128,2048,4096,6144};
84 struct xircom_private {
85 /* Send and receive buffers, kernel-addressable and dma addressable forms */
87 __le32 *rx_buffer;
88 __le32 *tx_buffer;
90 dma_addr_t rx_dma_handle;
91 dma_addr_t tx_dma_handle;
93 struct sk_buff *tx_skb[4];
95 unsigned long io_port;
96 int open;
98 /* transmit_used is the rotating counter that indicates which transmit
99 descriptor has to be used next */
100 int transmit_used;
102 /* Spinlock to serialize register operations.
103 It must be helt while manipulating the following registers:
104 CSR0, CSR6, CSR7, CSR9, CSR10, CSR15
106 spinlock_t lock;
108 struct pci_dev *pdev;
109 struct net_device *dev;
113 /* Function prototypes */
114 static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id);
115 static void xircom_remove(struct pci_dev *pdev);
116 static irqreturn_t xircom_interrupt(int irq, void *dev_instance);
117 static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
118 struct net_device *dev);
119 static int xircom_open(struct net_device *dev);
120 static int xircom_close(struct net_device *dev);
121 static void xircom_up(struct xircom_private *card);
122 #ifdef CONFIG_NET_POLL_CONTROLLER
123 static void xircom_poll_controller(struct net_device *dev);
124 #endif
126 static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset);
127 static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset);
128 static void read_mac_address(struct xircom_private *card);
129 static void transceiver_voodoo(struct xircom_private *card);
130 static void initialize_card(struct xircom_private *card);
131 static void trigger_transmit(struct xircom_private *card);
132 static void trigger_receive(struct xircom_private *card);
133 static void setup_descriptors(struct xircom_private *card);
134 static void remove_descriptors(struct xircom_private *card);
135 static int link_status_changed(struct xircom_private *card);
136 static void activate_receiver(struct xircom_private *card);
137 static void deactivate_receiver(struct xircom_private *card);
138 static void activate_transmitter(struct xircom_private *card);
139 static void deactivate_transmitter(struct xircom_private *card);
140 static void enable_transmit_interrupt(struct xircom_private *card);
141 static void enable_receive_interrupt(struct xircom_private *card);
142 static void enable_link_interrupt(struct xircom_private *card);
143 static void disable_all_interrupts(struct xircom_private *card);
144 static int link_status(struct xircom_private *card);
148 static DEFINE_PCI_DEVICE_TABLE(xircom_pci_table) = {
149 {0x115D, 0x0003, PCI_ANY_ID, PCI_ANY_ID,},
150 {0,},
152 MODULE_DEVICE_TABLE(pci, xircom_pci_table);
154 static struct pci_driver xircom_ops = {
155 .name = "xircom_cb",
156 .id_table = xircom_pci_table,
157 .probe = xircom_probe,
158 .remove = xircom_remove,
159 .suspend =NULL,
160 .resume =NULL
164 #ifdef DEBUG
165 static void print_binary(unsigned int number)
167 int i,i2;
168 char buffer[64];
169 memset(buffer,0,64);
170 i2=0;
171 for (i=31;i>=0;i--) {
172 if (number & (1<<i))
173 buffer[i2++]='1';
174 else
175 buffer[i2++]='0';
176 if ((i&3)==0)
177 buffer[i2++]=' ';
179 printk("%s\n",buffer);
181 #endif
183 static const struct net_device_ops netdev_ops = {
184 .ndo_open = xircom_open,
185 .ndo_stop = xircom_close,
186 .ndo_start_xmit = xircom_start_xmit,
187 .ndo_change_mtu = eth_change_mtu,
188 .ndo_set_mac_address = eth_mac_addr,
189 .ndo_validate_addr = eth_validate_addr,
190 #ifdef CONFIG_NET_POLL_CONTROLLER
191 .ndo_poll_controller = xircom_poll_controller,
192 #endif
195 /* xircom_probe is the code that gets called on device insertion.
196 it sets up the hardware and registers the device to the networklayer.
198 TODO: Send 1 or 2 "dummy" packets here as the card seems to discard the
199 first two packets that get send, and pump hates that.
202 static int __devinit xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id)
204 struct net_device *dev = NULL;
205 struct xircom_private *private;
206 unsigned long flags;
207 unsigned short tmp16;
208 enter("xircom_probe");
210 /* First do the PCI initialisation */
212 if (pci_enable_device(pdev))
213 return -ENODEV;
215 /* disable all powermanagement */
216 pci_write_config_dword(pdev, PCI_POWERMGMT, 0x0000);
218 pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
220 /* clear PCI status, if any */
221 pci_read_config_word (pdev,PCI_STATUS, &tmp16);
222 pci_write_config_word (pdev, PCI_STATUS,tmp16);
224 if (!request_region(pci_resource_start(pdev, 0), 128, "xircom_cb")) {
225 pr_err("%s: failed to allocate io-region\n", __func__);
226 return -ENODEV;
230 Before changing the hardware, allocate the memory.
231 This way, we can fail gracefully if not enough memory
232 is available.
234 dev = alloc_etherdev(sizeof(struct xircom_private));
235 if (!dev) {
236 pr_err("%s: failed to allocate etherdev\n", __func__);
237 goto device_fail;
239 private = netdev_priv(dev);
241 /* Allocate the send/receive buffers */
242 private->rx_buffer = pci_alloc_consistent(pdev,8192,&private->rx_dma_handle);
243 if (private->rx_buffer == NULL) {
244 pr_err("%s: no memory for rx buffer\n", __func__);
245 goto rx_buf_fail;
247 private->tx_buffer = pci_alloc_consistent(pdev,8192,&private->tx_dma_handle);
248 if (private->tx_buffer == NULL) {
249 pr_err("%s: no memory for tx buffer\n", __func__);
250 goto tx_buf_fail;
253 SET_NETDEV_DEV(dev, &pdev->dev);
256 private->dev = dev;
257 private->pdev = pdev;
258 private->io_port = pci_resource_start(pdev, 0);
259 spin_lock_init(&private->lock);
260 dev->irq = pdev->irq;
261 dev->base_addr = private->io_port;
263 initialize_card(private);
264 read_mac_address(private);
265 setup_descriptors(private);
267 dev->netdev_ops = &netdev_ops;
268 pci_set_drvdata(pdev, dev);
270 if (register_netdev(dev)) {
271 pr_err("%s: netdevice registration failed\n", __func__);
272 goto reg_fail;
275 dev_info(&dev->dev, "Xircom cardbus revision %i at irq %i\n",
276 pdev->revision, pdev->irq);
277 /* start the transmitter to get a heartbeat */
278 /* TODO: send 2 dummy packets here */
279 transceiver_voodoo(private);
281 spin_lock_irqsave(&private->lock,flags);
282 activate_transmitter(private);
283 activate_receiver(private);
284 spin_unlock_irqrestore(&private->lock,flags);
286 trigger_receive(private);
288 leave("xircom_probe");
289 return 0;
291 reg_fail:
292 kfree(private->tx_buffer);
293 tx_buf_fail:
294 kfree(private->rx_buffer);
295 rx_buf_fail:
296 free_netdev(dev);
297 device_fail:
298 return -ENODEV;
303 xircom_remove is called on module-unload or on device-eject.
304 it unregisters the irq, io-region and network device.
305 Interrupts and such are already stopped in the "ifconfig ethX down"
306 code.
308 static void __devexit xircom_remove(struct pci_dev *pdev)
310 struct net_device *dev = pci_get_drvdata(pdev);
311 struct xircom_private *card = netdev_priv(dev);
313 enter("xircom_remove");
314 pci_free_consistent(pdev,8192,card->rx_buffer,card->rx_dma_handle);
315 pci_free_consistent(pdev,8192,card->tx_buffer,card->tx_dma_handle);
317 release_region(dev->base_addr, 128);
318 unregister_netdev(dev);
319 free_netdev(dev);
320 pci_set_drvdata(pdev, NULL);
321 leave("xircom_remove");
324 static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
326 struct net_device *dev = (struct net_device *) dev_instance;
327 struct xircom_private *card = netdev_priv(dev);
328 unsigned int status;
329 int i;
331 enter("xircom_interrupt\n");
333 spin_lock(&card->lock);
334 status = inl(card->io_port+CSR5);
336 #ifdef DEBUG
337 print_binary(status);
338 printk("tx status 0x%08x 0x%08x\n",
339 card->tx_buffer[0], card->tx_buffer[4]);
340 printk("rx status 0x%08x 0x%08x\n",
341 card->rx_buffer[0], card->rx_buffer[4]);
342 #endif
343 /* Handle shared irq and hotplug */
344 if (status == 0 || status == 0xffffffff) {
345 spin_unlock(&card->lock);
346 return IRQ_NONE;
349 if (link_status_changed(card)) {
350 int newlink;
351 printk(KERN_DEBUG "xircom_cb: Link status has changed\n");
352 newlink = link_status(card);
353 dev_info(&dev->dev, "Link is %i mbit\n", newlink);
354 if (newlink)
355 netif_carrier_on(dev);
356 else
357 netif_carrier_off(dev);
361 /* Clear all remaining interrupts */
362 status |= 0xffffffff; /* FIXME: make this clear only the
363 real existing bits */
364 outl(status,card->io_port+CSR5);
367 for (i=0;i<NUMDESCRIPTORS;i++)
368 investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
369 for (i=0;i<NUMDESCRIPTORS;i++)
370 investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
373 spin_unlock(&card->lock);
374 leave("xircom_interrupt");
375 return IRQ_HANDLED;
378 static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
379 struct net_device *dev)
381 struct xircom_private *card;
382 unsigned long flags;
383 int nextdescriptor;
384 int desc;
385 enter("xircom_start_xmit");
387 card = netdev_priv(dev);
388 spin_lock_irqsave(&card->lock,flags);
390 /* First see if we can free some descriptors */
391 for (desc=0;desc<NUMDESCRIPTORS;desc++)
392 investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
395 nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
396 desc = card->transmit_used;
398 /* only send the packet if the descriptor is free */
399 if (card->tx_buffer[4*desc]==0) {
400 /* Copy the packet data; zero the memory first as the card
401 sometimes sends more than you ask it to. */
403 memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
404 skb_copy_from_linear_data(skb,
405 &(card->tx_buffer[bufferoffsets[desc] / 4]),
406 skb->len);
407 /* FIXME: The specification tells us that the length we send HAS to be a multiple of
408 4 bytes. */
410 card->tx_buffer[4*desc+1] = cpu_to_le32(skb->len);
411 if (desc == NUMDESCRIPTORS - 1) /* bit 25: last descriptor of the ring */
412 card->tx_buffer[4*desc+1] |= cpu_to_le32(1<<25);
414 card->tx_buffer[4*desc+1] |= cpu_to_le32(0xF0000000);
415 /* 0xF0... means want interrupts*/
416 card->tx_skb[desc] = skb;
418 wmb();
419 /* This gives the descriptor to the card */
420 card->tx_buffer[4*desc] = cpu_to_le32(0x80000000);
421 trigger_transmit(card);
422 if (card->tx_buffer[nextdescriptor*4] & cpu_to_le32(0x8000000)) {
423 /* next descriptor is occupied... */
424 netif_stop_queue(dev);
426 card->transmit_used = nextdescriptor;
427 leave("xircom-start_xmit - sent");
428 spin_unlock_irqrestore(&card->lock,flags);
429 return NETDEV_TX_OK;
434 /* Uh oh... no free descriptor... drop the packet */
435 netif_stop_queue(dev);
436 spin_unlock_irqrestore(&card->lock,flags);
437 trigger_transmit(card);
439 return NETDEV_TX_BUSY;
445 static int xircom_open(struct net_device *dev)
447 struct xircom_private *xp = netdev_priv(dev);
448 int retval;
449 enter("xircom_open");
450 pr_info("xircom cardbus adaptor found, registering as %s, using irq %i\n",
451 dev->name, dev->irq);
452 retval = request_irq(dev->irq, xircom_interrupt, IRQF_SHARED, dev->name, dev);
453 if (retval) {
454 leave("xircom_open - No IRQ");
455 return retval;
458 xircom_up(xp);
459 xp->open = 1;
460 leave("xircom_open");
461 return 0;
464 static int xircom_close(struct net_device *dev)
466 struct xircom_private *card;
467 unsigned long flags;
469 enter("xircom_close");
470 card = netdev_priv(dev);
471 netif_stop_queue(dev); /* we don't want new packets */
474 spin_lock_irqsave(&card->lock,flags);
476 disable_all_interrupts(card);
477 #if 0
478 /* We can enable this again once we send dummy packets on ifconfig ethX up */
479 deactivate_receiver(card);
480 deactivate_transmitter(card);
481 #endif
482 remove_descriptors(card);
484 spin_unlock_irqrestore(&card->lock,flags);
486 card->open = 0;
487 free_irq(dev->irq,dev);
489 leave("xircom_close");
491 return 0;
496 #ifdef CONFIG_NET_POLL_CONTROLLER
497 static void xircom_poll_controller(struct net_device *dev)
499 disable_irq(dev->irq);
500 xircom_interrupt(dev->irq, dev);
501 enable_irq(dev->irq);
503 #endif
506 static void initialize_card(struct xircom_private *card)
508 unsigned int val;
509 unsigned long flags;
510 enter("initialize_card");
513 spin_lock_irqsave(&card->lock, flags);
515 /* First: reset the card */
516 val = inl(card->io_port + CSR0);
517 val |= 0x01; /* Software reset */
518 outl(val, card->io_port + CSR0);
520 udelay(100); /* give the card some time to reset */
522 val = inl(card->io_port + CSR0);
523 val &= ~0x01; /* disable Software reset */
524 outl(val, card->io_port + CSR0);
527 val = 0; /* Value 0x00 is a safe and conservative value
528 for the PCI configuration settings */
529 outl(val, card->io_port + CSR0);
532 disable_all_interrupts(card);
533 deactivate_receiver(card);
534 deactivate_transmitter(card);
536 spin_unlock_irqrestore(&card->lock, flags);
538 leave("initialize_card");
542 trigger_transmit causes the card to check for frames to be transmitted.
543 This is accomplished by writing to the CSR1 port. The documentation
544 claims that the act of writing is sufficient and that the value is
545 ignored; I chose zero.
547 static void trigger_transmit(struct xircom_private *card)
549 unsigned int val;
550 enter("trigger_transmit");
552 val = 0;
553 outl(val, card->io_port + CSR1);
555 leave("trigger_transmit");
559 trigger_receive causes the card to check for empty frames in the
560 descriptor list in which packets can be received.
561 This is accomplished by writing to the CSR2 port. The documentation
562 claims that the act of writing is sufficient and that the value is
563 ignored; I chose zero.
565 static void trigger_receive(struct xircom_private *card)
567 unsigned int val;
568 enter("trigger_receive");
570 val = 0;
571 outl(val, card->io_port + CSR2);
573 leave("trigger_receive");
577 setup_descriptors initializes the send and receive buffers to be valid
578 descriptors and programs the addresses into the card.
580 static void setup_descriptors(struct xircom_private *card)
582 u32 address;
583 int i;
584 enter("setup_descriptors");
587 BUG_ON(card->rx_buffer == NULL);
588 BUG_ON(card->tx_buffer == NULL);
590 /* Receive descriptors */
591 memset(card->rx_buffer, 0, 128); /* clear the descriptors */
592 for (i=0;i<NUMDESCRIPTORS;i++ ) {
594 /* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
595 card->rx_buffer[i*4 + 0] = cpu_to_le32(0x80000000);
596 /* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
597 card->rx_buffer[i*4 + 1] = cpu_to_le32(1536);
598 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
599 card->rx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
601 /* Rx Descr2: address of the buffer
602 we store the buffer at the 2nd half of the page */
604 address = card->rx_dma_handle;
605 card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
606 /* Rx Desc3: address of 2nd buffer -> 0 */
607 card->rx_buffer[i*4 + 3] = 0;
610 wmb();
611 /* Write the receive descriptor ring address to the card */
612 address = card->rx_dma_handle;
613 outl(address, card->io_port + CSR3); /* Receive descr list address */
616 /* transmit descriptors */
617 memset(card->tx_buffer, 0, 128); /* clear the descriptors */
619 for (i=0;i<NUMDESCRIPTORS;i++ ) {
620 /* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
621 card->tx_buffer[i*4 + 0] = 0x00000000;
622 /* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
623 card->tx_buffer[i*4 + 1] = cpu_to_le32(1536);
624 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
625 card->tx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
627 /* Tx Descr2: address of the buffer
628 we store the buffer at the 2nd half of the page */
629 address = card->tx_dma_handle;
630 card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
631 /* Tx Desc3: address of 2nd buffer -> 0 */
632 card->tx_buffer[i*4 + 3] = 0;
635 wmb();
636 /* wite the transmit descriptor ring to the card */
637 address = card->tx_dma_handle;
638 outl(address, card->io_port + CSR4); /* xmit descr list address */
640 leave("setup_descriptors");
644 remove_descriptors informs the card the descriptors are no longer
645 valid by setting the address in the card to 0x00.
647 static void remove_descriptors(struct xircom_private *card)
649 unsigned int val;
650 enter("remove_descriptors");
652 val = 0;
653 outl(val, card->io_port + CSR3); /* Receive descriptor address */
654 outl(val, card->io_port + CSR4); /* Send descriptor address */
656 leave("remove_descriptors");
660 link_status_changed returns 1 if the card has indicated that
661 the link status has changed. The new link status has to be read from CSR12.
663 This function also clears the status-bit.
665 static int link_status_changed(struct xircom_private *card)
667 unsigned int val;
668 enter("link_status_changed");
670 val = inl(card->io_port + CSR5); /* Status register */
672 if ((val & (1 << 27)) == 0) { /* no change */
673 leave("link_status_changed - nochange");
674 return 0;
677 /* clear the event by writing a 1 to the bit in the
678 status register. */
679 val = (1 << 27);
680 outl(val, card->io_port + CSR5);
682 leave("link_status_changed - changed");
683 return 1;
688 transmit_active returns 1 if the transmitter on the card is
689 in a non-stopped state.
691 static int transmit_active(struct xircom_private *card)
693 unsigned int val;
694 enter("transmit_active");
696 val = inl(card->io_port + CSR5); /* Status register */
698 if ((val & (7 << 20)) == 0) { /* transmitter disabled */
699 leave("transmit_active - inactive");
700 return 0;
703 leave("transmit_active - active");
704 return 1;
708 receive_active returns 1 if the receiver on the card is
709 in a non-stopped state.
711 static int receive_active(struct xircom_private *card)
713 unsigned int val;
714 enter("receive_active");
717 val = inl(card->io_port + CSR5); /* Status register */
719 if ((val & (7 << 17)) == 0) { /* receiver disabled */
720 leave("receive_active - inactive");
721 return 0;
724 leave("receive_active - active");
725 return 1;
729 activate_receiver enables the receiver on the card.
730 Before being allowed to active the receiver, the receiver
731 must be completely de-activated. To achieve this,
732 this code actually disables the receiver first; then it waits for the
733 receiver to become inactive, then it activates the receiver and then
734 it waits for the receiver to be active.
736 must be called with the lock held and interrupts disabled.
738 static void activate_receiver(struct xircom_private *card)
740 unsigned int val;
741 int counter;
742 enter("activate_receiver");
745 val = inl(card->io_port + CSR6); /* Operation mode */
747 /* If the "active" bit is set and the receiver is already
748 active, no need to do the expensive thing */
749 if ((val&2) && (receive_active(card)))
750 return;
753 val = val & ~2; /* disable the receiver */
754 outl(val, card->io_port + CSR6);
756 counter = 10;
757 while (counter > 0) {
758 if (!receive_active(card))
759 break;
760 /* wait a while */
761 udelay(50);
762 counter--;
763 if (counter <= 0)
764 pr_err("Receiver failed to deactivate\n");
767 /* enable the receiver */
768 val = inl(card->io_port + CSR6); /* Operation mode */
769 val = val | 2; /* enable the receiver */
770 outl(val, card->io_port + CSR6);
772 /* now wait for the card to activate again */
773 counter = 10;
774 while (counter > 0) {
775 if (receive_active(card))
776 break;
777 /* wait a while */
778 udelay(50);
779 counter--;
780 if (counter <= 0)
781 pr_err("Receiver failed to re-activate\n");
784 leave("activate_receiver");
788 deactivate_receiver disables the receiver on the card.
789 To achieve this this code disables the receiver first;
790 then it waits for the receiver to become inactive.
792 must be called with the lock held and interrupts disabled.
794 static void deactivate_receiver(struct xircom_private *card)
796 unsigned int val;
797 int counter;
798 enter("deactivate_receiver");
800 val = inl(card->io_port + CSR6); /* Operation mode */
801 val = val & ~2; /* disable the receiver */
802 outl(val, card->io_port + CSR6);
804 counter = 10;
805 while (counter > 0) {
806 if (!receive_active(card))
807 break;
808 /* wait a while */
809 udelay(50);
810 counter--;
811 if (counter <= 0)
812 pr_err("Receiver failed to deactivate\n");
816 leave("deactivate_receiver");
821 activate_transmitter enables the transmitter on the card.
822 Before being allowed to active the transmitter, the transmitter
823 must be completely de-activated. To achieve this,
824 this code actually disables the transmitter first; then it waits for the
825 transmitter to become inactive, then it activates the transmitter and then
826 it waits for the transmitter to be active again.
828 must be called with the lock held and interrupts disabled.
830 static void activate_transmitter(struct xircom_private *card)
832 unsigned int val;
833 int counter;
834 enter("activate_transmitter");
837 val = inl(card->io_port + CSR6); /* Operation mode */
839 /* If the "active" bit is set and the receiver is already
840 active, no need to do the expensive thing */
841 if ((val&(1<<13)) && (transmit_active(card)))
842 return;
844 val = val & ~(1 << 13); /* disable the transmitter */
845 outl(val, card->io_port + CSR6);
847 counter = 10;
848 while (counter > 0) {
849 if (!transmit_active(card))
850 break;
851 /* wait a while */
852 udelay(50);
853 counter--;
854 if (counter <= 0)
855 pr_err("Transmitter failed to deactivate\n");
858 /* enable the transmitter */
859 val = inl(card->io_port + CSR6); /* Operation mode */
860 val = val | (1 << 13); /* enable the transmitter */
861 outl(val, card->io_port + CSR6);
863 /* now wait for the card to activate again */
864 counter = 10;
865 while (counter > 0) {
866 if (transmit_active(card))
867 break;
868 /* wait a while */
869 udelay(50);
870 counter--;
871 if (counter <= 0)
872 pr_err("Transmitter failed to re-activate\n");
875 leave("activate_transmitter");
879 deactivate_transmitter disables the transmitter on the card.
880 To achieve this this code disables the transmitter first;
881 then it waits for the transmitter to become inactive.
883 must be called with the lock held and interrupts disabled.
885 static void deactivate_transmitter(struct xircom_private *card)
887 unsigned int val;
888 int counter;
889 enter("deactivate_transmitter");
891 val = inl(card->io_port + CSR6); /* Operation mode */
892 val = val & ~2; /* disable the transmitter */
893 outl(val, card->io_port + CSR6);
895 counter = 20;
896 while (counter > 0) {
897 if (!transmit_active(card))
898 break;
899 /* wait a while */
900 udelay(50);
901 counter--;
902 if (counter <= 0)
903 pr_err("Transmitter failed to deactivate\n");
907 leave("deactivate_transmitter");
912 enable_transmit_interrupt enables the transmit interrupt
914 must be called with the lock held and interrupts disabled.
916 static void enable_transmit_interrupt(struct xircom_private *card)
918 unsigned int val;
919 enter("enable_transmit_interrupt");
921 val = inl(card->io_port + CSR7); /* Interrupt enable register */
922 val |= 1; /* enable the transmit interrupt */
923 outl(val, card->io_port + CSR7);
925 leave("enable_transmit_interrupt");
930 enable_receive_interrupt enables the receive interrupt
932 must be called with the lock held and interrupts disabled.
934 static void enable_receive_interrupt(struct xircom_private *card)
936 unsigned int val;
937 enter("enable_receive_interrupt");
939 val = inl(card->io_port + CSR7); /* Interrupt enable register */
940 val = val | (1 << 6); /* enable the receive interrupt */
941 outl(val, card->io_port + CSR7);
943 leave("enable_receive_interrupt");
947 enable_link_interrupt enables the link status change interrupt
949 must be called with the lock held and interrupts disabled.
951 static void enable_link_interrupt(struct xircom_private *card)
953 unsigned int val;
954 enter("enable_link_interrupt");
956 val = inl(card->io_port + CSR7); /* Interrupt enable register */
957 val = val | (1 << 27); /* enable the link status chage interrupt */
958 outl(val, card->io_port + CSR7);
960 leave("enable_link_interrupt");
966 disable_all_interrupts disables all interrupts
968 must be called with the lock held and interrupts disabled.
970 static void disable_all_interrupts(struct xircom_private *card)
972 unsigned int val;
973 enter("enable_all_interrupts");
975 val = 0; /* disable all interrupts */
976 outl(val, card->io_port + CSR7);
978 leave("disable_all_interrupts");
982 enable_common_interrupts enables several weird interrupts
984 must be called with the lock held and interrupts disabled.
986 static void enable_common_interrupts(struct xircom_private *card)
988 unsigned int val;
989 enter("enable_link_interrupt");
991 val = inl(card->io_port + CSR7); /* Interrupt enable register */
992 val |= (1<<16); /* Normal Interrupt Summary */
993 val |= (1<<15); /* Abnormal Interrupt Summary */
994 val |= (1<<13); /* Fatal bus error */
995 val |= (1<<8); /* Receive Process Stopped */
996 val |= (1<<7); /* Receive Buffer Unavailable */
997 val |= (1<<5); /* Transmit Underflow */
998 val |= (1<<2); /* Transmit Buffer Unavailable */
999 val |= (1<<1); /* Transmit Process Stopped */
1000 outl(val, card->io_port + CSR7);
1002 leave("enable_link_interrupt");
1006 enable_promisc starts promisc mode
1008 must be called with the lock held and interrupts disabled.
1010 static int enable_promisc(struct xircom_private *card)
1012 unsigned int val;
1013 enter("enable_promisc");
1015 val = inl(card->io_port + CSR6);
1016 val = val | (1 << 6);
1017 outl(val, card->io_port + CSR6);
1019 leave("enable_promisc");
1020 return 1;
1027 link_status() checks the links status and will return 0 for no link, 10 for 10mbit link and 100 for.. guess what.
1029 Must be called in locked state with interrupts disabled
1031 static int link_status(struct xircom_private *card)
1033 unsigned int val;
1034 enter("link_status");
1036 val = inb(card->io_port + CSR12);
1038 if (!(val&(1<<2))) /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
1039 return 10;
1040 if (!(val&(1<<1))) /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
1041 return 100;
1043 /* If we get here -> no link at all */
1045 leave("link_status");
1046 return 0;
1054 read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
1056 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
1058 static void read_mac_address(struct xircom_private *card)
1060 unsigned char j, tuple, link, data_id, data_count;
1061 unsigned long flags;
1062 int i;
1064 enter("read_mac_address");
1066 spin_lock_irqsave(&card->lock, flags);
1068 outl(1 << 12, card->io_port + CSR9); /* enable boot rom access */
1069 for (i = 0x100; i < 0x1f7; i += link + 2) {
1070 outl(i, card->io_port + CSR10);
1071 tuple = inl(card->io_port + CSR9) & 0xff;
1072 outl(i + 1, card->io_port + CSR10);
1073 link = inl(card->io_port + CSR9) & 0xff;
1074 outl(i + 2, card->io_port + CSR10);
1075 data_id = inl(card->io_port + CSR9) & 0xff;
1076 outl(i + 3, card->io_port + CSR10);
1077 data_count = inl(card->io_port + CSR9) & 0xff;
1078 if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
1080 * This is it. We have the data we want.
1082 for (j = 0; j < 6; j++) {
1083 outl(i + j + 4, card->io_port + CSR10);
1084 card->dev->dev_addr[j] = inl(card->io_port + CSR9) & 0xff;
1086 break;
1087 } else if (link == 0) {
1088 break;
1091 spin_unlock_irqrestore(&card->lock, flags);
1092 pr_debug(" %pM\n", card->dev->dev_addr);
1093 leave("read_mac_address");
1098 transceiver_voodoo() enables the external UTP plug thingy.
1099 it's called voodoo as I stole this code and cannot cross-reference
1100 it with the specification.
1102 static void transceiver_voodoo(struct xircom_private *card)
1104 unsigned long flags;
1106 enter("transceiver_voodoo");
1108 /* disable all powermanagement */
1109 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1111 setup_descriptors(card);
1113 spin_lock_irqsave(&card->lock, flags);
1115 outl(0x0008, card->io_port + CSR15);
1116 udelay(25);
1117 outl(0xa8050000, card->io_port + CSR15);
1118 udelay(25);
1119 outl(0xa00f0000, card->io_port + CSR15);
1120 udelay(25);
1122 spin_unlock_irqrestore(&card->lock, flags);
1124 netif_start_queue(card->dev);
1125 leave("transceiver_voodoo");
1129 static void xircom_up(struct xircom_private *card)
1131 unsigned long flags;
1132 int i;
1134 enter("xircom_up");
1136 /* disable all powermanagement */
1137 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1139 setup_descriptors(card);
1141 spin_lock_irqsave(&card->lock, flags);
1144 enable_link_interrupt(card);
1145 enable_transmit_interrupt(card);
1146 enable_receive_interrupt(card);
1147 enable_common_interrupts(card);
1148 enable_promisc(card);
1150 /* The card can have received packets already, read them away now */
1151 for (i=0;i<NUMDESCRIPTORS;i++)
1152 investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1155 spin_unlock_irqrestore(&card->lock, flags);
1156 trigger_receive(card);
1157 trigger_transmit(card);
1158 netif_start_queue(card->dev);
1159 leave("xircom_up");
1162 /* Bufferoffset is in BYTES */
1163 static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset)
1165 int status;
1167 enter("investigate_read_descriptor");
1168 status = le32_to_cpu(card->rx_buffer[4*descnr]);
1170 if ((status > 0)) { /* packet received */
1172 /* TODO: discard error packets */
1174 short pkt_len = ((status >> 16) & 0x7ff) - 4; /* minus 4, we don't want the CRC */
1175 struct sk_buff *skb;
1177 if (pkt_len > 1518) {
1178 pr_err("Packet length %i is bogus\n", pkt_len);
1179 pkt_len = 1518;
1182 skb = dev_alloc_skb(pkt_len + 2);
1183 if (skb == NULL) {
1184 dev->stats.rx_dropped++;
1185 goto out;
1187 skb_reserve(skb, 2);
1188 skb_copy_to_linear_data(skb, (unsigned char*)&card->rx_buffer[bufferoffset / 4], pkt_len);
1189 skb_put(skb, pkt_len);
1190 skb->protocol = eth_type_trans(skb, dev);
1191 netif_rx(skb);
1192 dev->stats.rx_packets++;
1193 dev->stats.rx_bytes += pkt_len;
1195 out:
1196 /* give the buffer back to the card */
1197 card->rx_buffer[4*descnr] = cpu_to_le32(0x80000000);
1198 trigger_receive(card);
1201 leave("investigate_read_descriptor");
1206 /* Bufferoffset is in BYTES */
1207 static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset)
1209 int status;
1211 enter("investigate_write_descriptor");
1213 status = le32_to_cpu(card->tx_buffer[4*descnr]);
1214 #if 0
1215 if (status & 0x8000) { /* Major error */
1216 pr_err("Major transmit error status %x\n", status);
1217 card->tx_buffer[4*descnr] = 0;
1218 netif_wake_queue (dev);
1220 #endif
1221 if (status > 0) { /* bit 31 is 0 when done */
1222 if (card->tx_skb[descnr]!=NULL) {
1223 dev->stats.tx_bytes += card->tx_skb[descnr]->len;
1224 dev_kfree_skb_irq(card->tx_skb[descnr]);
1226 card->tx_skb[descnr] = NULL;
1227 /* Bit 8 in the status field is 1 if there was a collision */
1228 if (status&(1<<8))
1229 dev->stats.collisions++;
1230 card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1231 netif_wake_queue (dev);
1232 dev->stats.tx_packets++;
1235 leave("investigate_write_descriptor");
1240 static int __init xircom_init(void)
1242 return pci_register_driver(&xircom_ops);
1245 static void __exit xircom_exit(void)
1247 pci_unregister_driver(&xircom_ops);
1250 module_init(xircom_init)
1251 module_exit(xircom_exit)