2 This program is free software; you can redistribute it and/or
3 modify it under the terms of the GNU General Public License
4 as published by the Free Software Foundation; either version 2
5 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
15 #define DRV_NAME "uli526x"
16 #define DRV_VERSION "0.9.3"
17 #define DRV_RELDATE "2005-7-29"
19 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/timer.h>
24 #include <linux/errno.h>
25 #include <linux/ioport.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/pci.h>
29 #include <linux/init.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/ethtool.h>
33 #include <linux/skbuff.h>
34 #include <linux/delay.h>
35 #include <linux/spinlock.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/bitops.h>
39 #include <asm/processor.h>
42 #include <asm/uaccess.h>
45 /* Board/System/Debug information/definition ---------------- */
46 #define PCI_ULI5261_ID 0x526110B9 /* ULi M5261 ID*/
47 #define PCI_ULI5263_ID 0x526310B9 /* ULi M5263 ID*/
49 #define ULI526X_IO_SIZE 0x100
50 #define TX_DESC_CNT 0x20 /* Allocated Tx descriptors */
51 #define RX_DESC_CNT 0x30 /* Allocated Rx descriptors */
52 #define TX_FREE_DESC_CNT (TX_DESC_CNT - 2) /* Max TX packet count */
53 #define TX_WAKE_DESC_CNT (TX_DESC_CNT - 3) /* TX wakeup count */
54 #define DESC_ALL_CNT (TX_DESC_CNT + RX_DESC_CNT)
55 #define TX_BUF_ALLOC 0x600
56 #define RX_ALLOC_SIZE 0x620
57 #define ULI526X_RESET 1
59 #define CR6_DEFAULT 0x22200000
60 #define CR7_DEFAULT 0x180c1
61 #define CR15_DEFAULT 0x06 /* TxJabber RxWatchdog */
62 #define TDES0_ERR_MASK 0x4302 /* TXJT, LC, EC, FUE */
63 #define MAX_PACKET_SIZE 1514
64 #define ULI5261_MAX_MULTICAST 14
65 #define RX_COPY_SIZE 100
66 #define MAX_CHECK_PACKET 0x8000
68 #define ULI526X_10MHF 0
69 #define ULI526X_100MHF 1
70 #define ULI526X_10MFD 4
71 #define ULI526X_100MFD 5
72 #define ULI526X_AUTO 8
74 #define ULI526X_TXTH_72 0x400000 /* TX TH 72 byte */
75 #define ULI526X_TXTH_96 0x404000 /* TX TH 96 byte */
76 #define ULI526X_TXTH_128 0x0000 /* TX TH 128 byte */
77 #define ULI526X_TXTH_256 0x4000 /* TX TH 256 byte */
78 #define ULI526X_TXTH_512 0x8000 /* TX TH 512 byte */
79 #define ULI526X_TXTH_1K 0xC000 /* TX TH 1K byte */
81 #define ULI526X_TIMER_WUT (jiffies + HZ * 1)/* timer wakeup time : 1 second */
82 #define ULI526X_TX_TIMEOUT ((16*HZ)/2) /* tx packet time-out time 8 s" */
83 #define ULI526X_TX_KICK (4*HZ/2) /* tx packet Kick-out time 2 s" */
85 #define ULI526X_DBUG(dbug_now, msg, value) if (uli526x_debug || (dbug_now)) printk(KERN_ERR DRV_NAME ": %s %lx\n", (msg), (long) (value))
87 #define SHOW_MEDIA_TYPE(mode) printk(KERN_ERR DRV_NAME ": Change Speed to %sMhz %s duplex\n",mode & 1 ?"100":"10", mode & 4 ? "full":"half");
90 /* CR9 definition: SROM/MII */
91 #define CR9_SROM_READ 0x4800
94 #define CR9_CRDOUT 0x8
95 #define SROM_DATA_0 0x0
96 #define SROM_DATA_1 0x4
97 #define PHY_DATA_1 0x20000
98 #define PHY_DATA_0 0x00000
99 #define MDCLKH 0x10000
101 #define PHY_POWER_DOWN 0x800
103 #define SROM_V41_CODE 0x14
105 #define SROM_CLK_WRITE(data, ioaddr) \
106 outl(data|CR9_SROM_READ|CR9_SRCS,ioaddr); \
108 outl(data|CR9_SROM_READ|CR9_SRCS|CR9_SRCLK,ioaddr); \
110 outl(data|CR9_SROM_READ|CR9_SRCS,ioaddr); \
113 /* Structure/enum declaration ------------------------------- */
115 __le32 tdes0, tdes1, tdes2, tdes3; /* Data for the card */
116 char *tx_buf_ptr; /* Data for us */
117 struct tx_desc *next_tx_desc;
118 } __attribute__(( aligned(32) ));
121 __le32 rdes0, rdes1, rdes2, rdes3; /* Data for the card */
122 struct sk_buff *rx_skb_ptr; /* Data for us */
123 struct rx_desc *next_rx_desc;
124 } __attribute__(( aligned(32) ));
126 struct uli526x_board_info {
127 u32 chip_id; /* Chip vendor/Device ID */
128 struct net_device *next_dev; /* next device */
129 struct pci_dev *pdev; /* PCI device */
132 long ioaddr; /* I/O base address */
139 /* pointer for memory physical address */
140 dma_addr_t buf_pool_dma_ptr; /* Tx buffer pool memory */
141 dma_addr_t buf_pool_dma_start; /* Tx buffer pool align dword */
142 dma_addr_t desc_pool_dma_ptr; /* descriptor pool memory */
143 dma_addr_t first_tx_desc_dma;
144 dma_addr_t first_rx_desc_dma;
146 /* descriptor pointer */
147 unsigned char *buf_pool_ptr; /* Tx buffer pool memory */
148 unsigned char *buf_pool_start; /* Tx buffer pool align dword */
149 unsigned char *desc_pool_ptr; /* descriptor pool memory */
150 struct tx_desc *first_tx_desc;
151 struct tx_desc *tx_insert_ptr;
152 struct tx_desc *tx_remove_ptr;
153 struct rx_desc *first_rx_desc;
154 struct rx_desc *rx_insert_ptr;
155 struct rx_desc *rx_ready_ptr; /* packet come pointer */
156 unsigned long tx_packet_cnt; /* transmitted packet count */
157 unsigned long rx_avail_cnt; /* available rx descriptor count */
158 unsigned long interval_rx_cnt; /* rx packet count a callback time */
161 u16 NIC_capability; /* NIC media capability */
162 u16 PHY_reg4; /* Saved Phyxcer register 4 value */
164 u8 media_mode; /* user specify media mode */
165 u8 op_mode; /* real work media mode */
167 u8 link_failed; /* Ever link failed */
168 u8 wait_reset; /* Hardware failed, need to reset */
169 struct timer_list timer;
171 /* Driver defined statistic counter */
172 unsigned long tx_fifo_underrun;
173 unsigned long tx_loss_carrier;
174 unsigned long tx_no_carrier;
175 unsigned long tx_late_collision;
176 unsigned long tx_excessive_collision;
177 unsigned long tx_jabber_timeout;
178 unsigned long reset_count;
179 unsigned long reset_cr8;
180 unsigned long reset_fatal;
181 unsigned long reset_TXtimeout;
184 unsigned char srom[128];
188 enum uli526x_offsets {
189 DCR0 = 0x00, DCR1 = 0x08, DCR2 = 0x10, DCR3 = 0x18, DCR4 = 0x20,
190 DCR5 = 0x28, DCR6 = 0x30, DCR7 = 0x38, DCR8 = 0x40, DCR9 = 0x48,
191 DCR10 = 0x50, DCR11 = 0x58, DCR12 = 0x60, DCR13 = 0x68, DCR14 = 0x70,
195 enum uli526x_CR6_bits {
196 CR6_RXSC = 0x2, CR6_PBF = 0x8, CR6_PM = 0x40, CR6_PAM = 0x80,
197 CR6_FDM = 0x200, CR6_TXSC = 0x2000, CR6_STI = 0x100000,
198 CR6_SFT = 0x200000, CR6_RXA = 0x40000000, CR6_NO_PURGE = 0x20000000
201 /* Global variable declaration ----------------------------- */
202 static int __devinitdata printed_version;
203 static char version[] __devinitdata =
204 KERN_INFO DRV_NAME ": ULi M5261/M5263 net driver, version "
205 DRV_VERSION " (" DRV_RELDATE ")\n";
207 static int uli526x_debug;
208 static unsigned char uli526x_media_mode = ULI526X_AUTO;
209 static u32 uli526x_cr6_user_set;
211 /* For module input parameter */
216 /* function declaration ------------------------------------- */
217 static int uli526x_open(struct net_device *);
218 static int uli526x_start_xmit(struct sk_buff *, struct net_device *);
219 static int uli526x_stop(struct net_device *);
220 static void uli526x_set_filter_mode(struct net_device *);
221 static const struct ethtool_ops netdev_ethtool_ops;
222 static u16 read_srom_word(long, int);
223 static irqreturn_t uli526x_interrupt(int, void *);
224 #ifdef CONFIG_NET_POLL_CONTROLLER
225 static void uli526x_poll(struct net_device *dev);
227 static void uli526x_descriptor_init(struct uli526x_board_info *, unsigned long);
228 static void allocate_rx_buffer(struct uli526x_board_info *);
229 static void update_cr6(u32, unsigned long);
230 static void send_filter_frame(struct net_device *, int);
231 static u16 phy_read(unsigned long, u8, u8, u32);
232 static u16 phy_readby_cr10(unsigned long, u8, u8);
233 static void phy_write(unsigned long, u8, u8, u16, u32);
234 static void phy_writeby_cr10(unsigned long, u8, u8, u16);
235 static void phy_write_1bit(unsigned long, u32, u32);
236 static u16 phy_read_1bit(unsigned long, u32);
237 static u8 uli526x_sense_speed(struct uli526x_board_info *);
238 static void uli526x_process_mode(struct uli526x_board_info *);
239 static void uli526x_timer(unsigned long);
240 static void uli526x_rx_packet(struct net_device *, struct uli526x_board_info *);
241 static void uli526x_free_tx_pkt(struct net_device *, struct uli526x_board_info *);
242 static void uli526x_reuse_skb(struct uli526x_board_info *, struct sk_buff *);
243 static void uli526x_dynamic_reset(struct net_device *);
244 static void uli526x_free_rxbuffer(struct uli526x_board_info *);
245 static void uli526x_init(struct net_device *);
246 static void uli526x_set_phyxcer(struct uli526x_board_info *);
248 /* ULI526X network board routine ---------------------------- */
250 static const struct net_device_ops netdev_ops = {
251 .ndo_open = uli526x_open,
252 .ndo_stop = uli526x_stop,
253 .ndo_start_xmit = uli526x_start_xmit,
254 .ndo_set_multicast_list = uli526x_set_filter_mode,
255 .ndo_change_mtu = eth_change_mtu,
256 .ndo_set_mac_address = eth_mac_addr,
257 .ndo_validate_addr = eth_validate_addr,
258 #ifdef CONFIG_NET_POLL_CONTROLLER
259 .ndo_poll_controller = uli526x_poll,
264 * Search ULI526X board, allocate space and register it
267 static int __devinit uli526x_init_one (struct pci_dev *pdev,
268 const struct pci_device_id *ent)
270 struct uli526x_board_info *db; /* board information structure */
271 struct net_device *dev;
274 ULI526X_DBUG(0, "uli526x_init_one()", 0);
276 if (!printed_version++)
279 /* Init network device */
280 dev = alloc_etherdev(sizeof(*db));
283 SET_NETDEV_DEV(dev, &pdev->dev);
285 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
286 printk(KERN_WARNING DRV_NAME ": 32-bit PCI DMA not available.\n");
291 /* Enable Master/IO access, Disable memory access */
292 err = pci_enable_device(pdev);
296 if (!pci_resource_start(pdev, 0)) {
297 printk(KERN_ERR DRV_NAME ": I/O base is zero\n");
299 goto err_out_disable;
302 if (pci_resource_len(pdev, 0) < (ULI526X_IO_SIZE) ) {
303 printk(KERN_ERR DRV_NAME ": Allocated I/O size too small\n");
305 goto err_out_disable;
308 if (pci_request_regions(pdev, DRV_NAME)) {
309 printk(KERN_ERR DRV_NAME ": Failed to request PCI regions\n");
311 goto err_out_disable;
314 /* Init system & device */
315 db = netdev_priv(dev);
317 /* Allocate Tx/Rx descriptor memory */
318 db->desc_pool_ptr = pci_alloc_consistent(pdev, sizeof(struct tx_desc) * DESC_ALL_CNT + 0x20, &db->desc_pool_dma_ptr);
319 if(db->desc_pool_ptr == NULL)
324 db->buf_pool_ptr = pci_alloc_consistent(pdev, TX_BUF_ALLOC * TX_DESC_CNT + 4, &db->buf_pool_dma_ptr);
325 if(db->buf_pool_ptr == NULL)
331 db->first_tx_desc = (struct tx_desc *) db->desc_pool_ptr;
332 db->first_tx_desc_dma = db->desc_pool_dma_ptr;
333 db->buf_pool_start = db->buf_pool_ptr;
334 db->buf_pool_dma_start = db->buf_pool_dma_ptr;
336 db->chip_id = ent->driver_data;
337 db->ioaddr = pci_resource_start(pdev, 0);
342 dev->base_addr = db->ioaddr;
343 dev->irq = pdev->irq;
344 pci_set_drvdata(pdev, dev);
346 /* Register some necessary functions */
347 dev->netdev_ops = &netdev_ops;
348 dev->ethtool_ops = &netdev_ethtool_ops;
350 spin_lock_init(&db->lock);
353 /* read 64 word srom data */
354 for (i = 0; i < 64; i++)
355 ((__le16 *) db->srom)[i] = cpu_to_le16(read_srom_word(db->ioaddr, i));
357 /* Set Node address */
358 if(((u16 *) db->srom)[0] == 0xffff || ((u16 *) db->srom)[0] == 0) /* SROM absent, so read MAC address from ID Table */
360 outl(0x10000, db->ioaddr + DCR0); //Diagnosis mode
361 outl(0x1c0, db->ioaddr + DCR13); //Reset dianostic pointer port
362 outl(0, db->ioaddr + DCR14); //Clear reset port
363 outl(0x10, db->ioaddr + DCR14); //Reset ID Table pointer
364 outl(0, db->ioaddr + DCR14); //Clear reset port
365 outl(0, db->ioaddr + DCR13); //Clear CR13
366 outl(0x1b0, db->ioaddr + DCR13); //Select ID Table access port
367 //Read MAC address from CR14
368 for (i = 0; i < 6; i++)
369 dev->dev_addr[i] = inl(db->ioaddr + DCR14);
371 outl(0, db->ioaddr + DCR13); //Clear CR13
372 outl(0, db->ioaddr + DCR0); //Clear CR0
377 for (i = 0; i < 6; i++)
378 dev->dev_addr[i] = db->srom[20 + i];
380 err = register_netdev (dev);
384 printk(KERN_INFO "%s: ULi M%04lx at pci%s, %pM, irq %d.\n",
385 dev->name,ent->driver_data >> 16,pci_name(pdev),
386 dev->dev_addr, dev->irq);
388 pci_set_master(pdev);
393 pci_release_regions(pdev);
395 if(db->desc_pool_ptr)
396 pci_free_consistent(pdev, sizeof(struct tx_desc) * DESC_ALL_CNT + 0x20,
397 db->desc_pool_ptr, db->desc_pool_dma_ptr);
399 if(db->buf_pool_ptr != NULL)
400 pci_free_consistent(pdev, TX_BUF_ALLOC * TX_DESC_CNT + 4,
401 db->buf_pool_ptr, db->buf_pool_dma_ptr);
403 pci_disable_device(pdev);
405 pci_set_drvdata(pdev, NULL);
412 static void __devexit uli526x_remove_one (struct pci_dev *pdev)
414 struct net_device *dev = pci_get_drvdata(pdev);
415 struct uli526x_board_info *db = netdev_priv(dev);
417 ULI526X_DBUG(0, "uli526x_remove_one()", 0);
419 pci_free_consistent(db->pdev, sizeof(struct tx_desc) *
420 DESC_ALL_CNT + 0x20, db->desc_pool_ptr,
421 db->desc_pool_dma_ptr);
422 pci_free_consistent(db->pdev, TX_BUF_ALLOC * TX_DESC_CNT + 4,
423 db->buf_pool_ptr, db->buf_pool_dma_ptr);
424 unregister_netdev(dev);
425 pci_release_regions(pdev);
426 free_netdev(dev); /* free board information */
427 pci_set_drvdata(pdev, NULL);
428 pci_disable_device(pdev);
429 ULI526X_DBUG(0, "uli526x_remove_one() exit", 0);
434 * Open the interface.
435 * The interface is opened whenever "ifconfig" activates it.
438 static int uli526x_open(struct net_device *dev)
441 struct uli526x_board_info *db = netdev_priv(dev);
443 ULI526X_DBUG(0, "uli526x_open", 0);
445 /* system variable init */
446 db->cr6_data = CR6_DEFAULT | uli526x_cr6_user_set;
447 db->tx_packet_cnt = 0;
448 db->rx_avail_cnt = 0;
450 netif_carrier_off(dev);
453 db->NIC_capability = 0xf; /* All capability*/
454 db->PHY_reg4 = 0x1e0;
456 /* CR6 operation mode decision */
457 db->cr6_data |= ULI526X_TXTH_256;
458 db->cr0_data = CR0_DEFAULT;
460 /* Initialize ULI526X board */
463 ret = request_irq(dev->irq, &uli526x_interrupt, IRQF_SHARED, dev->name, dev);
467 /* Active System Interface */
468 netif_wake_queue(dev);
470 /* set and active a timer process */
471 init_timer(&db->timer);
472 db->timer.expires = ULI526X_TIMER_WUT + HZ * 2;
473 db->timer.data = (unsigned long)dev;
474 db->timer.function = &uli526x_timer;
475 add_timer(&db->timer);
481 /* Initialize ULI526X board
482 * Reset ULI526X board
483 * Initialize TX/Rx descriptor chain structure
484 * Send the set-up frame
485 * Enable Tx/Rx machine
488 static void uli526x_init(struct net_device *dev)
490 struct uli526x_board_info *db = netdev_priv(dev);
491 unsigned long ioaddr = db->ioaddr;
498 ULI526X_DBUG(0, "uli526x_init()", 0);
500 /* Reset M526x MAC controller */
501 outl(ULI526X_RESET, ioaddr + DCR0); /* RESET MAC */
503 outl(db->cr0_data, ioaddr + DCR0);
506 /* Phy addr : In some boards,M5261/M5263 phy address != 1 */
508 for(phy_tmp=0;phy_tmp<32;phy_tmp++)
510 phy_value=phy_read(db->ioaddr,phy_tmp,3,db->chip_id);//peer add
511 if(phy_value != 0xffff&&phy_value!=0)
513 db->phy_addr = phy_tmp;
518 printk(KERN_WARNING "Can not find the phy address!!!");
519 /* Parser SROM and media mode */
520 db->media_mode = uli526x_media_mode;
522 /* phyxcer capability setting */
523 phy_reg_reset = phy_read(db->ioaddr, db->phy_addr, 0, db->chip_id);
524 phy_reg_reset = (phy_reg_reset | 0x8000);
525 phy_write(db->ioaddr, db->phy_addr, 0, phy_reg_reset, db->chip_id);
527 /* See IEEE 802.3-2002.pdf (Section 2, Chapter "22.2.4 Management
528 * functions") or phy data sheet for details on phy reset
533 phy_read(db->ioaddr, db->phy_addr, 0, db->chip_id) & 0x8000)
536 /* Process Phyxcer Media Mode */
537 uli526x_set_phyxcer(db);
539 /* Media Mode Process */
540 if ( !(db->media_mode & ULI526X_AUTO) )
541 db->op_mode = db->media_mode; /* Force Mode */
543 /* Initialize Transmit/Receive decriptor and CR3/4 */
544 uli526x_descriptor_init(db, ioaddr);
546 /* Init CR6 to program M526X operation */
547 update_cr6(db->cr6_data, ioaddr);
549 /* Send setup frame */
550 send_filter_frame(dev, dev->mc_count); /* M5261/M5263 */
552 /* Init CR7, interrupt active bit */
553 db->cr7_data = CR7_DEFAULT;
554 outl(db->cr7_data, ioaddr + DCR7);
556 /* Init CR15, Tx jabber and Rx watchdog timer */
557 outl(db->cr15_data, ioaddr + DCR15);
559 /* Enable ULI526X Tx/Rx function */
560 db->cr6_data |= CR6_RXSC | CR6_TXSC;
561 update_cr6(db->cr6_data, ioaddr);
566 * Hardware start transmission.
567 * Send a packet to media from the upper layer.
570 static int uli526x_start_xmit(struct sk_buff *skb, struct net_device *dev)
572 struct uli526x_board_info *db = netdev_priv(dev);
573 struct tx_desc *txptr;
576 ULI526X_DBUG(0, "uli526x_start_xmit", 0);
578 /* Resource flag check */
579 netif_stop_queue(dev);
581 /* Too large packet check */
582 if (skb->len > MAX_PACKET_SIZE) {
583 printk(KERN_ERR DRV_NAME ": big packet = %d\n", (u16)skb->len);
588 spin_lock_irqsave(&db->lock, flags);
590 /* No Tx resource check, it never happen nromally */
591 if (db->tx_packet_cnt >= TX_FREE_DESC_CNT) {
592 spin_unlock_irqrestore(&db->lock, flags);
593 printk(KERN_ERR DRV_NAME ": No Tx resource %ld\n", db->tx_packet_cnt);
597 /* Disable NIC interrupt */
598 outl(0, dev->base_addr + DCR7);
600 /* transmit this packet */
601 txptr = db->tx_insert_ptr;
602 skb_copy_from_linear_data(skb, txptr->tx_buf_ptr, skb->len);
603 txptr->tdes1 = cpu_to_le32(0xe1000000 | skb->len);
605 /* Point to next transmit free descriptor */
606 db->tx_insert_ptr = txptr->next_tx_desc;
608 /* Transmit Packet Process */
609 if ( (db->tx_packet_cnt < TX_DESC_CNT) ) {
610 txptr->tdes0 = cpu_to_le32(0x80000000); /* Set owner bit */
611 db->tx_packet_cnt++; /* Ready to send */
612 outl(0x1, dev->base_addr + DCR1); /* Issue Tx polling */
613 dev->trans_start = jiffies; /* saved time stamp */
616 /* Tx resource check */
617 if ( db->tx_packet_cnt < TX_FREE_DESC_CNT )
618 netif_wake_queue(dev);
620 /* Restore CR7 to enable interrupt */
621 spin_unlock_irqrestore(&db->lock, flags);
622 outl(db->cr7_data, dev->base_addr + DCR7);
632 * Stop the interface.
633 * The interface is stopped when it is brought.
636 static int uli526x_stop(struct net_device *dev)
638 struct uli526x_board_info *db = netdev_priv(dev);
639 unsigned long ioaddr = dev->base_addr;
641 ULI526X_DBUG(0, "uli526x_stop", 0);
644 netif_stop_queue(dev);
647 del_timer_sync(&db->timer);
649 /* Reset & stop ULI526X board */
650 outl(ULI526X_RESET, ioaddr + DCR0);
652 phy_write(db->ioaddr, db->phy_addr, 0, 0x8000, db->chip_id);
655 free_irq(dev->irq, dev);
657 /* free allocated rx buffer */
658 uli526x_free_rxbuffer(db);
661 /* show statistic counter */
662 printk(DRV_NAME ": FU:%lx EC:%lx LC:%lx NC:%lx LOC:%lx TXJT:%lx RESET:%lx RCR8:%lx FAL:%lx TT:%lx\n",
663 db->tx_fifo_underrun, db->tx_excessive_collision,
664 db->tx_late_collision, db->tx_no_carrier, db->tx_loss_carrier,
665 db->tx_jabber_timeout, db->reset_count, db->reset_cr8,
666 db->reset_fatal, db->reset_TXtimeout);
674 * M5261/M5263 insterrupt handler
675 * receive the packet to upper layer, free the transmitted packet
678 static irqreturn_t uli526x_interrupt(int irq, void *dev_id)
680 struct net_device *dev = dev_id;
681 struct uli526x_board_info *db = netdev_priv(dev);
682 unsigned long ioaddr = dev->base_addr;
685 spin_lock_irqsave(&db->lock, flags);
686 outl(0, ioaddr + DCR7);
688 /* Got ULI526X status */
689 db->cr5_data = inl(ioaddr + DCR5);
690 outl(db->cr5_data, ioaddr + DCR5);
691 if ( !(db->cr5_data & 0x180c1) ) {
692 /* Restore CR7 to enable interrupt mask */
693 outl(db->cr7_data, ioaddr + DCR7);
694 spin_unlock_irqrestore(&db->lock, flags);
698 /* Check system status */
699 if (db->cr5_data & 0x2000) {
700 /* system bus error happen */
701 ULI526X_DBUG(1, "System bus error happen. CR5=", db->cr5_data);
703 db->wait_reset = 1; /* Need to RESET */
704 spin_unlock_irqrestore(&db->lock, flags);
708 /* Received the coming packet */
709 if ( (db->cr5_data & 0x40) && db->rx_avail_cnt )
710 uli526x_rx_packet(dev, db);
712 /* reallocate rx descriptor buffer */
713 if (db->rx_avail_cnt<RX_DESC_CNT)
714 allocate_rx_buffer(db);
716 /* Free the transmitted descriptor */
717 if ( db->cr5_data & 0x01)
718 uli526x_free_tx_pkt(dev, db);
720 /* Restore CR7 to enable interrupt mask */
721 outl(db->cr7_data, ioaddr + DCR7);
723 spin_unlock_irqrestore(&db->lock, flags);
727 #ifdef CONFIG_NET_POLL_CONTROLLER
728 static void uli526x_poll(struct net_device *dev)
730 /* ISR grabs the irqsave lock, so this should be safe */
731 uli526x_interrupt(dev->irq, dev);
736 * Free TX resource after TX complete
739 static void uli526x_free_tx_pkt(struct net_device *dev,
740 struct uli526x_board_info * db)
742 struct tx_desc *txptr;
745 txptr = db->tx_remove_ptr;
746 while(db->tx_packet_cnt) {
747 tdes0 = le32_to_cpu(txptr->tdes0);
748 /* printk(DRV_NAME ": tdes0=%x\n", tdes0); */
749 if (tdes0 & 0x80000000)
752 /* A packet sent completed */
754 dev->stats.tx_packets++;
756 /* Transmit statistic counter */
757 if ( tdes0 != 0x7fffffff ) {
758 /* printk(DRV_NAME ": tdes0=%x\n", tdes0); */
759 dev->stats.collisions += (tdes0 >> 3) & 0xf;
760 dev->stats.tx_bytes += le32_to_cpu(txptr->tdes1) & 0x7ff;
761 if (tdes0 & TDES0_ERR_MASK) {
762 dev->stats.tx_errors++;
763 if (tdes0 & 0x0002) { /* UnderRun */
764 db->tx_fifo_underrun++;
765 if ( !(db->cr6_data & CR6_SFT) ) {
766 db->cr6_data = db->cr6_data | CR6_SFT;
767 update_cr6(db->cr6_data, db->ioaddr);
771 db->tx_excessive_collision++;
773 db->tx_late_collision++;
777 db->tx_loss_carrier++;
779 db->tx_jabber_timeout++;
783 txptr = txptr->next_tx_desc;
786 /* Update TX remove pointer to next */
787 db->tx_remove_ptr = txptr;
789 /* Resource available check */
790 if ( db->tx_packet_cnt < TX_WAKE_DESC_CNT )
791 netif_wake_queue(dev); /* Active upper layer, send again */
796 * Receive the come packet and pass to upper layer
799 static void uli526x_rx_packet(struct net_device *dev, struct uli526x_board_info * db)
801 struct rx_desc *rxptr;
806 rxptr = db->rx_ready_ptr;
808 while(db->rx_avail_cnt) {
809 rdes0 = le32_to_cpu(rxptr->rdes0);
810 if (rdes0 & 0x80000000) /* packet owner check */
816 db->interval_rx_cnt++;
818 pci_unmap_single(db->pdev, le32_to_cpu(rxptr->rdes2), RX_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
819 if ( (rdes0 & 0x300) != 0x300) {
820 /* A packet without First/Last flag */
822 ULI526X_DBUG(0, "Reuse SK buffer, rdes0", rdes0);
823 uli526x_reuse_skb(db, rxptr->rx_skb_ptr);
825 /* A packet with First/Last flag */
826 rxlen = ( (rdes0 >> 16) & 0x3fff) - 4;
828 /* error summary bit check */
829 if (rdes0 & 0x8000) {
830 /* This is a error packet */
831 //printk(DRV_NAME ": rdes0: %lx\n", rdes0);
832 dev->stats.rx_errors++;
834 dev->stats.rx_fifo_errors++;
836 dev->stats.rx_crc_errors++;
838 dev->stats.rx_length_errors++;
841 if ( !(rdes0 & 0x8000) ||
842 ((db->cr6_data & CR6_PM) && (rxlen>6)) ) {
843 skb = rxptr->rx_skb_ptr;
845 /* Good packet, send to upper layer */
846 /* Shorst packet used new SKB */
847 if ( (rxlen < RX_COPY_SIZE) &&
848 ( (skb = dev_alloc_skb(rxlen + 2) )
850 /* size less than COPY_SIZE, allocate a rxlen SKB */
851 skb_reserve(skb, 2); /* 16byte align */
852 memcpy(skb_put(skb, rxlen),
853 skb_tail_pointer(rxptr->rx_skb_ptr),
855 uli526x_reuse_skb(db, rxptr->rx_skb_ptr);
859 skb->protocol = eth_type_trans(skb, dev);
861 dev->stats.rx_packets++;
862 dev->stats.rx_bytes += rxlen;
865 /* Reuse SKB buffer when the packet is error */
866 ULI526X_DBUG(0, "Reuse SK buffer, rdes0", rdes0);
867 uli526x_reuse_skb(db, rxptr->rx_skb_ptr);
871 rxptr = rxptr->next_rx_desc;
874 db->rx_ready_ptr = rxptr;
879 * Set ULI526X multicast address
882 static void uli526x_set_filter_mode(struct net_device * dev)
884 struct uli526x_board_info *db = netdev_priv(dev);
887 ULI526X_DBUG(0, "uli526x_set_filter_mode()", 0);
888 spin_lock_irqsave(&db->lock, flags);
890 if (dev->flags & IFF_PROMISC) {
891 ULI526X_DBUG(0, "Enable PROM Mode", 0);
892 db->cr6_data |= CR6_PM | CR6_PBF;
893 update_cr6(db->cr6_data, db->ioaddr);
894 spin_unlock_irqrestore(&db->lock, flags);
898 if (dev->flags & IFF_ALLMULTI || dev->mc_count > ULI5261_MAX_MULTICAST) {
899 ULI526X_DBUG(0, "Pass all multicast address", dev->mc_count);
900 db->cr6_data &= ~(CR6_PM | CR6_PBF);
901 db->cr6_data |= CR6_PAM;
902 spin_unlock_irqrestore(&db->lock, flags);
906 ULI526X_DBUG(0, "Set multicast address", dev->mc_count);
907 send_filter_frame(dev, dev->mc_count); /* M5261/M5263 */
908 spin_unlock_irqrestore(&db->lock, flags);
912 ULi_ethtool_gset(struct uli526x_board_info *db, struct ethtool_cmd *ecmd)
914 ecmd->supported = (SUPPORTED_10baseT_Half |
915 SUPPORTED_10baseT_Full |
916 SUPPORTED_100baseT_Half |
917 SUPPORTED_100baseT_Full |
921 ecmd->advertising = (ADVERTISED_10baseT_Half |
922 ADVERTISED_10baseT_Full |
923 ADVERTISED_100baseT_Half |
924 ADVERTISED_100baseT_Full |
929 ecmd->port = PORT_MII;
930 ecmd->phy_address = db->phy_addr;
932 ecmd->transceiver = XCVR_EXTERNAL;
935 ecmd->duplex = DUPLEX_HALF;
937 if(db->op_mode==ULI526X_100MHF || db->op_mode==ULI526X_100MFD)
941 if(db->op_mode==ULI526X_10MFD || db->op_mode==ULI526X_100MFD)
943 ecmd->duplex = DUPLEX_FULL;
951 if (db->media_mode & ULI526X_AUTO)
953 ecmd->autoneg = AUTONEG_ENABLE;
957 static void netdev_get_drvinfo(struct net_device *dev,
958 struct ethtool_drvinfo *info)
960 struct uli526x_board_info *np = netdev_priv(dev);
962 strcpy(info->driver, DRV_NAME);
963 strcpy(info->version, DRV_VERSION);
965 strcpy(info->bus_info, pci_name(np->pdev));
967 sprintf(info->bus_info, "EISA 0x%lx %d",
968 dev->base_addr, dev->irq);
971 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
972 struct uli526x_board_info *np = netdev_priv(dev);
974 ULi_ethtool_gset(np, cmd);
979 static u32 netdev_get_link(struct net_device *dev) {
980 struct uli526x_board_info *np = netdev_priv(dev);
988 static void uli526x_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
990 wol->supported = WAKE_PHY | WAKE_MAGIC;
994 static const struct ethtool_ops netdev_ethtool_ops = {
995 .get_drvinfo = netdev_get_drvinfo,
996 .get_settings = netdev_get_settings,
997 .get_link = netdev_get_link,
998 .get_wol = uli526x_get_wol,
1002 * A periodic timer routine
1003 * Dynamic media sense, allocate Rx buffer...
1006 static void uli526x_timer(unsigned long data)
1009 unsigned char tmp_cr12=0;
1010 struct net_device *dev = (struct net_device *) data;
1011 struct uli526x_board_info *db = netdev_priv(dev);
1012 unsigned long flags;
1015 //ULI526X_DBUG(0, "uli526x_timer()", 0);
1016 spin_lock_irqsave(&db->lock, flags);
1019 /* Dynamic reset ULI526X : system error or transmit time-out */
1020 tmp_cr8 = inl(db->ioaddr + DCR8);
1021 if ( (db->interval_rx_cnt==0) && (tmp_cr8) ) {
1025 db->interval_rx_cnt = 0;
1027 /* TX polling kick monitor */
1028 if ( db->tx_packet_cnt &&
1029 time_after(jiffies, dev->trans_start + ULI526X_TX_KICK) ) {
1030 outl(0x1, dev->base_addr + DCR1); // Tx polling again
1033 if ( time_after(jiffies, dev->trans_start + ULI526X_TX_TIMEOUT) ) {
1034 db->reset_TXtimeout++;
1036 printk( "%s: Tx timeout - resetting\n",
1041 if (db->wait_reset) {
1042 ULI526X_DBUG(0, "Dynamic Reset device", db->tx_packet_cnt);
1044 uli526x_dynamic_reset(dev);
1045 db->timer.expires = ULI526X_TIMER_WUT;
1046 add_timer(&db->timer);
1047 spin_unlock_irqrestore(&db->lock, flags);
1051 /* Link status check, Dynamic media type change */
1052 if((phy_read(db->ioaddr, db->phy_addr, 5, db->chip_id) & 0x01e0)!=0)
1055 if ( !(tmp_cr12 & 0x3) && !db->link_failed ) {
1057 ULI526X_DBUG(0, "Link Failed", tmp_cr12);
1058 netif_carrier_off(dev);
1059 printk(KERN_INFO "uli526x: %s NIC Link is Down\n",dev->name);
1060 db->link_failed = 1;
1062 /* For Force 10/100M Half/Full mode: Enable Auto-Nego mode */
1063 /* AUTO don't need */
1064 if ( !(db->media_mode & 0x8) )
1065 phy_write(db->ioaddr, db->phy_addr, 0, 0x1000, db->chip_id);
1067 /* AUTO mode, if INT phyxcer link failed, select EXT device */
1068 if (db->media_mode & ULI526X_AUTO) {
1069 db->cr6_data&=~0x00000200; /* bit9=0, HD mode */
1070 update_cr6(db->cr6_data, db->ioaddr);
1073 if ((tmp_cr12 & 0x3) && db->link_failed) {
1074 ULI526X_DBUG(0, "Link link OK", tmp_cr12);
1075 db->link_failed = 0;
1077 /* Auto Sense Speed */
1078 if ( (db->media_mode & ULI526X_AUTO) &&
1079 uli526x_sense_speed(db) )
1080 db->link_failed = 1;
1081 uli526x_process_mode(db);
1083 if(db->link_failed==0)
1085 if(db->op_mode==ULI526X_100MHF || db->op_mode==ULI526X_100MFD)
1089 if(db->op_mode==ULI526X_10MFD || db->op_mode==ULI526X_100MFD)
1091 printk(KERN_INFO "uli526x: %s NIC Link is Up %d Mbps Full duplex\n",dev->name,TmpSpeed);
1095 printk(KERN_INFO "uli526x: %s NIC Link is Up %d Mbps Half duplex\n",dev->name,TmpSpeed);
1097 netif_carrier_on(dev);
1099 /* SHOW_MEDIA_TYPE(db->op_mode); */
1101 else if(!(tmp_cr12 & 0x3) && db->link_failed)
1105 printk(KERN_INFO "uli526x: %s NIC Link is Down\n",dev->name);
1106 netif_carrier_off(dev);
1111 /* Timer active again */
1112 db->timer.expires = ULI526X_TIMER_WUT;
1113 add_timer(&db->timer);
1114 spin_unlock_irqrestore(&db->lock, flags);
1119 * Stop ULI526X board
1120 * Free Tx/Rx allocated memory
1121 * Init system variable
1124 static void uli526x_reset_prepare(struct net_device *dev)
1126 struct uli526x_board_info *db = netdev_priv(dev);
1128 /* Sopt MAC controller */
1129 db->cr6_data &= ~(CR6_RXSC | CR6_TXSC); /* Disable Tx/Rx */
1130 update_cr6(db->cr6_data, dev->base_addr);
1131 outl(0, dev->base_addr + DCR7); /* Disable Interrupt */
1132 outl(inl(dev->base_addr + DCR5), dev->base_addr + DCR5);
1134 /* Disable upper layer interface */
1135 netif_stop_queue(dev);
1137 /* Free Rx Allocate buffer */
1138 uli526x_free_rxbuffer(db);
1140 /* system variable init */
1141 db->tx_packet_cnt = 0;
1142 db->rx_avail_cnt = 0;
1143 db->link_failed = 1;
1150 * Dynamic reset the ULI526X board
1151 * Stop ULI526X board
1152 * Free Tx/Rx allocated memory
1153 * Reset ULI526X board
1154 * Re-initialize ULI526X board
1157 static void uli526x_dynamic_reset(struct net_device *dev)
1159 ULI526X_DBUG(0, "uli526x_dynamic_reset()", 0);
1161 uli526x_reset_prepare(dev);
1163 /* Re-initialize ULI526X board */
1166 /* Restart upper layer interface */
1167 netif_wake_queue(dev);
1174 * Suspend the interface.
1177 static int uli526x_suspend(struct pci_dev *pdev, pm_message_t state)
1179 struct net_device *dev = pci_get_drvdata(pdev);
1180 pci_power_t power_state;
1183 ULI526X_DBUG(0, "uli526x_suspend", 0);
1185 if (!netdev_priv(dev))
1188 pci_save_state(pdev);
1190 if (!netif_running(dev))
1193 netif_device_detach(dev);
1194 uli526x_reset_prepare(dev);
1196 power_state = pci_choose_state(pdev, state);
1197 pci_enable_wake(pdev, power_state, 0);
1198 err = pci_set_power_state(pdev, power_state);
1200 netif_device_attach(dev);
1201 /* Re-initialize ULI526X board */
1203 /* Restart upper layer interface */
1204 netif_wake_queue(dev);
1211 * Resume the interface.
1214 static int uli526x_resume(struct pci_dev *pdev)
1216 struct net_device *dev = pci_get_drvdata(pdev);
1219 ULI526X_DBUG(0, "uli526x_resume", 0);
1221 if (!netdev_priv(dev))
1224 pci_restore_state(pdev);
1226 if (!netif_running(dev))
1229 err = pci_set_power_state(pdev, PCI_D0);
1231 printk(KERN_WARNING "%s: Could not put device into D0\n",
1236 netif_device_attach(dev);
1237 /* Re-initialize ULI526X board */
1239 /* Restart upper layer interface */
1240 netif_wake_queue(dev);
1245 #else /* !CONFIG_PM */
1247 #define uli526x_suspend NULL
1248 #define uli526x_resume NULL
1250 #endif /* !CONFIG_PM */
1254 * free all allocated rx buffer
1257 static void uli526x_free_rxbuffer(struct uli526x_board_info * db)
1259 ULI526X_DBUG(0, "uli526x_free_rxbuffer()", 0);
1261 /* free allocated rx buffer */
1262 while (db->rx_avail_cnt) {
1263 dev_kfree_skb(db->rx_ready_ptr->rx_skb_ptr);
1264 db->rx_ready_ptr = db->rx_ready_ptr->next_rx_desc;
1271 * Reuse the SK buffer
1274 static void uli526x_reuse_skb(struct uli526x_board_info *db, struct sk_buff * skb)
1276 struct rx_desc *rxptr = db->rx_insert_ptr;
1278 if (!(rxptr->rdes0 & cpu_to_le32(0x80000000))) {
1279 rxptr->rx_skb_ptr = skb;
1280 rxptr->rdes2 = cpu_to_le32(pci_map_single(db->pdev,
1281 skb_tail_pointer(skb),
1283 PCI_DMA_FROMDEVICE));
1285 rxptr->rdes0 = cpu_to_le32(0x80000000);
1287 db->rx_insert_ptr = rxptr->next_rx_desc;
1289 ULI526X_DBUG(0, "SK Buffer reuse method error", db->rx_avail_cnt);
1294 * Initialize transmit/Receive descriptor
1295 * Using Chain structure, and allocate Tx/Rx buffer
1298 static void uli526x_descriptor_init(struct uli526x_board_info *db, unsigned long ioaddr)
1300 struct tx_desc *tmp_tx;
1301 struct rx_desc *tmp_rx;
1302 unsigned char *tmp_buf;
1303 dma_addr_t tmp_tx_dma, tmp_rx_dma;
1304 dma_addr_t tmp_buf_dma;
1307 ULI526X_DBUG(0, "uli526x_descriptor_init()", 0);
1309 /* tx descriptor start pointer */
1310 db->tx_insert_ptr = db->first_tx_desc;
1311 db->tx_remove_ptr = db->first_tx_desc;
1312 outl(db->first_tx_desc_dma, ioaddr + DCR4); /* TX DESC address */
1314 /* rx descriptor start pointer */
1315 db->first_rx_desc = (void *)db->first_tx_desc + sizeof(struct tx_desc) * TX_DESC_CNT;
1316 db->first_rx_desc_dma = db->first_tx_desc_dma + sizeof(struct tx_desc) * TX_DESC_CNT;
1317 db->rx_insert_ptr = db->first_rx_desc;
1318 db->rx_ready_ptr = db->first_rx_desc;
1319 outl(db->first_rx_desc_dma, ioaddr + DCR3); /* RX DESC address */
1321 /* Init Transmit chain */
1322 tmp_buf = db->buf_pool_start;
1323 tmp_buf_dma = db->buf_pool_dma_start;
1324 tmp_tx_dma = db->first_tx_desc_dma;
1325 for (tmp_tx = db->first_tx_desc, i = 0; i < TX_DESC_CNT; i++, tmp_tx++) {
1326 tmp_tx->tx_buf_ptr = tmp_buf;
1327 tmp_tx->tdes0 = cpu_to_le32(0);
1328 tmp_tx->tdes1 = cpu_to_le32(0x81000000); /* IC, chain */
1329 tmp_tx->tdes2 = cpu_to_le32(tmp_buf_dma);
1330 tmp_tx_dma += sizeof(struct tx_desc);
1331 tmp_tx->tdes3 = cpu_to_le32(tmp_tx_dma);
1332 tmp_tx->next_tx_desc = tmp_tx + 1;
1333 tmp_buf = tmp_buf + TX_BUF_ALLOC;
1334 tmp_buf_dma = tmp_buf_dma + TX_BUF_ALLOC;
1336 (--tmp_tx)->tdes3 = cpu_to_le32(db->first_tx_desc_dma);
1337 tmp_tx->next_tx_desc = db->first_tx_desc;
1339 /* Init Receive descriptor chain */
1340 tmp_rx_dma=db->first_rx_desc_dma;
1341 for (tmp_rx = db->first_rx_desc, i = 0; i < RX_DESC_CNT; i++, tmp_rx++) {
1342 tmp_rx->rdes0 = cpu_to_le32(0);
1343 tmp_rx->rdes1 = cpu_to_le32(0x01000600);
1344 tmp_rx_dma += sizeof(struct rx_desc);
1345 tmp_rx->rdes3 = cpu_to_le32(tmp_rx_dma);
1346 tmp_rx->next_rx_desc = tmp_rx + 1;
1348 (--tmp_rx)->rdes3 = cpu_to_le32(db->first_rx_desc_dma);
1349 tmp_rx->next_rx_desc = db->first_rx_desc;
1351 /* pre-allocate Rx buffer */
1352 allocate_rx_buffer(db);
1358 * Firstly stop ULI526X, then written value and start
1361 static void update_cr6(u32 cr6_data, unsigned long ioaddr)
1364 outl(cr6_data, ioaddr + DCR6);
1370 * Send a setup frame for M5261/M5263
1371 * This setup frame initialize ULI526X address filter mode
1375 #define FLT_SHIFT 16
1380 static void send_filter_frame(struct net_device *dev, int mc_cnt)
1382 struct uli526x_board_info *db = netdev_priv(dev);
1383 struct dev_mc_list *mcptr;
1384 struct tx_desc *txptr;
1389 ULI526X_DBUG(0, "send_filter_frame()", 0);
1391 txptr = db->tx_insert_ptr;
1392 suptr = (u32 *) txptr->tx_buf_ptr;
1395 addrptr = (u16 *) dev->dev_addr;
1396 *suptr++ = addrptr[0] << FLT_SHIFT;
1397 *suptr++ = addrptr[1] << FLT_SHIFT;
1398 *suptr++ = addrptr[2] << FLT_SHIFT;
1400 /* broadcast address */
1401 *suptr++ = 0xffff << FLT_SHIFT;
1402 *suptr++ = 0xffff << FLT_SHIFT;
1403 *suptr++ = 0xffff << FLT_SHIFT;
1405 /* fit the multicast address */
1406 for (mcptr = dev->mc_list, i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
1407 addrptr = (u16 *) mcptr->dmi_addr;
1408 *suptr++ = addrptr[0] << FLT_SHIFT;
1409 *suptr++ = addrptr[1] << FLT_SHIFT;
1410 *suptr++ = addrptr[2] << FLT_SHIFT;
1414 *suptr++ = 0xffff << FLT_SHIFT;
1415 *suptr++ = 0xffff << FLT_SHIFT;
1416 *suptr++ = 0xffff << FLT_SHIFT;
1419 /* prepare the setup frame */
1420 db->tx_insert_ptr = txptr->next_tx_desc;
1421 txptr->tdes1 = cpu_to_le32(0x890000c0);
1423 /* Resource Check and Send the setup packet */
1424 if (db->tx_packet_cnt < TX_DESC_CNT) {
1425 /* Resource Empty */
1426 db->tx_packet_cnt++;
1427 txptr->tdes0 = cpu_to_le32(0x80000000);
1428 update_cr6(db->cr6_data | 0x2000, dev->base_addr);
1429 outl(0x1, dev->base_addr + DCR1); /* Issue Tx polling */
1430 update_cr6(db->cr6_data, dev->base_addr);
1431 dev->trans_start = jiffies;
1433 printk(KERN_ERR DRV_NAME ": No Tx resource - Send_filter_frame!\n");
1438 * Allocate rx buffer,
1439 * As possible as allocate maxiumn Rx buffer
1442 static void allocate_rx_buffer(struct uli526x_board_info *db)
1444 struct rx_desc *rxptr;
1445 struct sk_buff *skb;
1447 rxptr = db->rx_insert_ptr;
1449 while(db->rx_avail_cnt < RX_DESC_CNT) {
1450 if ( ( skb = dev_alloc_skb(RX_ALLOC_SIZE) ) == NULL )
1452 rxptr->rx_skb_ptr = skb; /* FIXME (?) */
1453 rxptr->rdes2 = cpu_to_le32(pci_map_single(db->pdev,
1454 skb_tail_pointer(skb),
1456 PCI_DMA_FROMDEVICE));
1458 rxptr->rdes0 = cpu_to_le32(0x80000000);
1459 rxptr = rxptr->next_rx_desc;
1463 db->rx_insert_ptr = rxptr;
1468 * Read one word data from the serial ROM
1471 static u16 read_srom_word(long ioaddr, int offset)
1475 long cr9_ioaddr = ioaddr + DCR9;
1477 outl(CR9_SROM_READ, cr9_ioaddr);
1478 outl(CR9_SROM_READ | CR9_SRCS, cr9_ioaddr);
1480 /* Send the Read Command 110b */
1481 SROM_CLK_WRITE(SROM_DATA_1, cr9_ioaddr);
1482 SROM_CLK_WRITE(SROM_DATA_1, cr9_ioaddr);
1483 SROM_CLK_WRITE(SROM_DATA_0, cr9_ioaddr);
1485 /* Send the offset */
1486 for (i = 5; i >= 0; i--) {
1487 srom_data = (offset & (1 << i)) ? SROM_DATA_1 : SROM_DATA_0;
1488 SROM_CLK_WRITE(srom_data, cr9_ioaddr);
1491 outl(CR9_SROM_READ | CR9_SRCS, cr9_ioaddr);
1493 for (i = 16; i > 0; i--) {
1494 outl(CR9_SROM_READ | CR9_SRCS | CR9_SRCLK, cr9_ioaddr);
1496 srom_data = (srom_data << 1) | ((inl(cr9_ioaddr) & CR9_CRDOUT) ? 1 : 0);
1497 outl(CR9_SROM_READ | CR9_SRCS, cr9_ioaddr);
1501 outl(CR9_SROM_READ, cr9_ioaddr);
1507 * Auto sense the media mode
1510 static u8 uli526x_sense_speed(struct uli526x_board_info * db)
1515 phy_mode = phy_read(db->ioaddr, db->phy_addr, 1, db->chip_id);
1516 phy_mode = phy_read(db->ioaddr, db->phy_addr, 1, db->chip_id);
1518 if ( (phy_mode & 0x24) == 0x24 ) {
1520 phy_mode = ((phy_read(db->ioaddr, db->phy_addr, 5, db->chip_id) & 0x01e0)<<7);
1523 else if(phy_mode&0x4000)
1525 else if(phy_mode&0x2000)
1530 /* printk(DRV_NAME ": Phy_mode %x ",phy_mode); */
1532 case 0x1000: db->op_mode = ULI526X_10MHF; break;
1533 case 0x2000: db->op_mode = ULI526X_10MFD; break;
1534 case 0x4000: db->op_mode = ULI526X_100MHF; break;
1535 case 0x8000: db->op_mode = ULI526X_100MFD; break;
1536 default: db->op_mode = ULI526X_10MHF; ErrFlag = 1; break;
1539 db->op_mode = ULI526X_10MHF;
1540 ULI526X_DBUG(0, "Link Failed :", phy_mode);
1549 * Set 10/100 phyxcer capability
1550 * AUTO mode : phyxcer register4 is NIC capability
1551 * Force mode: phyxcer register4 is the force media
1554 static void uli526x_set_phyxcer(struct uli526x_board_info *db)
1558 /* Phyxcer capability setting */
1559 phy_reg = phy_read(db->ioaddr, db->phy_addr, 4, db->chip_id) & ~0x01e0;
1561 if (db->media_mode & ULI526X_AUTO) {
1563 phy_reg |= db->PHY_reg4;
1566 switch(db->media_mode) {
1567 case ULI526X_10MHF: phy_reg |= 0x20; break;
1568 case ULI526X_10MFD: phy_reg |= 0x40; break;
1569 case ULI526X_100MHF: phy_reg |= 0x80; break;
1570 case ULI526X_100MFD: phy_reg |= 0x100; break;
1575 /* Write new capability to Phyxcer Reg4 */
1576 if ( !(phy_reg & 0x01e0)) {
1577 phy_reg|=db->PHY_reg4;
1578 db->media_mode|=ULI526X_AUTO;
1580 phy_write(db->ioaddr, db->phy_addr, 4, phy_reg, db->chip_id);
1582 /* Restart Auto-Negotiation */
1583 phy_write(db->ioaddr, db->phy_addr, 0, 0x1200, db->chip_id);
1590 AUTO mode : PHY controller in Auto-negotiation Mode
1591 * Force mode: PHY controller in force mode with HUB
1592 * N-way force capability with SWITCH
1595 static void uli526x_process_mode(struct uli526x_board_info *db)
1599 /* Full Duplex Mode Check */
1600 if (db->op_mode & 0x4)
1601 db->cr6_data |= CR6_FDM; /* Set Full Duplex Bit */
1603 db->cr6_data &= ~CR6_FDM; /* Clear Full Duplex Bit */
1605 update_cr6(db->cr6_data, db->ioaddr);
1607 /* 10/100M phyxcer force mode need */
1608 if ( !(db->media_mode & 0x8)) {
1610 phy_reg = phy_read(db->ioaddr, db->phy_addr, 6, db->chip_id);
1611 if ( !(phy_reg & 0x1) ) {
1612 /* parter without N-Way capability */
1614 switch(db->op_mode) {
1615 case ULI526X_10MHF: phy_reg = 0x0; break;
1616 case ULI526X_10MFD: phy_reg = 0x100; break;
1617 case ULI526X_100MHF: phy_reg = 0x2000; break;
1618 case ULI526X_100MFD: phy_reg = 0x2100; break;
1620 phy_write(db->ioaddr, db->phy_addr, 0, phy_reg, db->chip_id);
1627 * Write a word to Phy register
1630 static void phy_write(unsigned long iobase, u8 phy_addr, u8 offset, u16 phy_data, u32 chip_id)
1633 unsigned long ioaddr;
1635 if(chip_id == PCI_ULI5263_ID)
1637 phy_writeby_cr10(iobase, phy_addr, offset, phy_data);
1640 /* M5261/M5263 Chip */
1641 ioaddr = iobase + DCR9;
1643 /* Send 33 synchronization clock to Phy controller */
1644 for (i = 0; i < 35; i++)
1645 phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);
1647 /* Send start command(01) to Phy */
1648 phy_write_1bit(ioaddr, PHY_DATA_0, chip_id);
1649 phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);
1651 /* Send write command(01) to Phy */
1652 phy_write_1bit(ioaddr, PHY_DATA_0, chip_id);
1653 phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);
1655 /* Send Phy address */
1656 for (i = 0x10; i > 0; i = i >> 1)
1657 phy_write_1bit(ioaddr, phy_addr & i ? PHY_DATA_1 : PHY_DATA_0, chip_id);
1659 /* Send register address */
1660 for (i = 0x10; i > 0; i = i >> 1)
1661 phy_write_1bit(ioaddr, offset & i ? PHY_DATA_1 : PHY_DATA_0, chip_id);
1663 /* written trasnition */
1664 phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);
1665 phy_write_1bit(ioaddr, PHY_DATA_0, chip_id);
1667 /* Write a word data to PHY controller */
1668 for ( i = 0x8000; i > 0; i >>= 1)
1669 phy_write_1bit(ioaddr, phy_data & i ? PHY_DATA_1 : PHY_DATA_0, chip_id);
1675 * Read a word data from phy register
1678 static u16 phy_read(unsigned long iobase, u8 phy_addr, u8 offset, u32 chip_id)
1682 unsigned long ioaddr;
1684 if(chip_id == PCI_ULI5263_ID)
1685 return phy_readby_cr10(iobase, phy_addr, offset);
1686 /* M5261/M5263 Chip */
1687 ioaddr = iobase + DCR9;
1689 /* Send 33 synchronization clock to Phy controller */
1690 for (i = 0; i < 35; i++)
1691 phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);
1693 /* Send start command(01) to Phy */
1694 phy_write_1bit(ioaddr, PHY_DATA_0, chip_id);
1695 phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);
1697 /* Send read command(10) to Phy */
1698 phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);
1699 phy_write_1bit(ioaddr, PHY_DATA_0, chip_id);
1701 /* Send Phy address */
1702 for (i = 0x10; i > 0; i = i >> 1)
1703 phy_write_1bit(ioaddr, phy_addr & i ? PHY_DATA_1 : PHY_DATA_0, chip_id);
1705 /* Send register address */
1706 for (i = 0x10; i > 0; i = i >> 1)
1707 phy_write_1bit(ioaddr, offset & i ? PHY_DATA_1 : PHY_DATA_0, chip_id);
1709 /* Skip transition state */
1710 phy_read_1bit(ioaddr, chip_id);
1712 /* read 16bit data */
1713 for (phy_data = 0, i = 0; i < 16; i++) {
1715 phy_data |= phy_read_1bit(ioaddr, chip_id);
1721 static u16 phy_readby_cr10(unsigned long iobase, u8 phy_addr, u8 offset)
1723 unsigned long ioaddr,cr10_value;
1725 ioaddr = iobase + DCR10;
1726 cr10_value = phy_addr;
1727 cr10_value = (cr10_value<<5) + offset;
1728 cr10_value = (cr10_value<<16) + 0x08000000;
1729 outl(cr10_value,ioaddr);
1733 cr10_value = inl(ioaddr);
1734 if(cr10_value&0x10000000)
1737 return (cr10_value&0x0ffff);
1740 static void phy_writeby_cr10(unsigned long iobase, u8 phy_addr, u8 offset, u16 phy_data)
1742 unsigned long ioaddr,cr10_value;
1744 ioaddr = iobase + DCR10;
1745 cr10_value = phy_addr;
1746 cr10_value = (cr10_value<<5) + offset;
1747 cr10_value = (cr10_value<<16) + 0x04000000 + phy_data;
1748 outl(cr10_value,ioaddr);
1752 * Write one bit data to Phy Controller
1755 static void phy_write_1bit(unsigned long ioaddr, u32 phy_data, u32 chip_id)
1757 outl(phy_data , ioaddr); /* MII Clock Low */
1759 outl(phy_data | MDCLKH, ioaddr); /* MII Clock High */
1761 outl(phy_data , ioaddr); /* MII Clock Low */
1767 * Read one bit phy data from PHY controller
1770 static u16 phy_read_1bit(unsigned long ioaddr, u32 chip_id)
1774 outl(0x50000 , ioaddr);
1776 phy_data = ( inl(ioaddr) >> 19 ) & 0x1;
1777 outl(0x40000 , ioaddr);
1784 static struct pci_device_id uli526x_pci_tbl[] = {
1785 { 0x10B9, 0x5261, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PCI_ULI5261_ID },
1786 { 0x10B9, 0x5263, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PCI_ULI5263_ID },
1789 MODULE_DEVICE_TABLE(pci, uli526x_pci_tbl);
1792 static struct pci_driver uli526x_driver = {
1794 .id_table = uli526x_pci_tbl,
1795 .probe = uli526x_init_one,
1796 .remove = __devexit_p(uli526x_remove_one),
1797 .suspend = uli526x_suspend,
1798 .resume = uli526x_resume,
1801 MODULE_AUTHOR("Peer Chen, peer.chen@uli.com.tw");
1802 MODULE_DESCRIPTION("ULi M5261/M5263 fast ethernet driver");
1803 MODULE_LICENSE("GPL");
1805 module_param(debug, int, 0644);
1806 module_param(mode, int, 0);
1807 module_param(cr6set, int, 0);
1808 MODULE_PARM_DESC(debug, "ULi M5261/M5263 enable debugging (0-1)");
1809 MODULE_PARM_DESC(mode, "ULi M5261/M5263: Bit 0: 10/100Mbps, bit 2: duplex, bit 8: HomePNA");
1812 * when user used insmod to add module, system invoked init_module()
1813 * to register the services.
1816 static int __init uli526x_init_module(void)
1820 printed_version = 1;
1822 ULI526X_DBUG(0, "init_module() ", debug);
1825 uli526x_debug = debug; /* set debug flag */
1827 uli526x_cr6_user_set = cr6set;
1831 case ULI526X_100MHF:
1833 case ULI526X_100MFD:
1834 uli526x_media_mode = mode;
1837 uli526x_media_mode = ULI526X_AUTO;
1841 return pci_register_driver(&uli526x_driver);
1847 * when user used rmmod to delete module, system invoked clean_module()
1848 * to un-register all registered services.
1851 static void __exit uli526x_cleanup_module(void)
1853 ULI526X_DBUG(0, "uli526x_clean_module() ", debug);
1854 pci_unregister_driver(&uli526x_driver);
1857 module_init(uli526x_init_module);
1858 module_exit(uli526x_cleanup_module);