1 // SPDX-License-Identifier: GPL-2.0-or-later
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #define DRV_NAME "uli526x"
11 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/interrupt.h>
19 #include <linux/pci.h>
20 #include <linux/init.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/ethtool.h>
24 #include <linux/skbuff.h>
25 #include <linux/delay.h>
26 #include <linux/spinlock.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/bitops.h>
30 #include <asm/processor.h>
33 #include <linux/uaccess.h>
35 #define uw32(reg, val) iowrite32(val, ioaddr + (reg))
36 #define ur32(reg) ioread32(ioaddr + (reg))
38 /* Board/System/Debug information/definition ---------------- */
39 #define PCI_ULI5261_ID 0x526110B9 /* ULi M5261 ID*/
40 #define PCI_ULI5263_ID 0x526310B9 /* ULi M5263 ID*/
42 #define ULI526X_IO_SIZE 0x100
43 #define TX_DESC_CNT 0x20 /* Allocated Tx descriptors */
44 #define RX_DESC_CNT 0x30 /* Allocated Rx descriptors */
45 #define TX_FREE_DESC_CNT (TX_DESC_CNT - 2) /* Max TX packet count */
46 #define TX_WAKE_DESC_CNT (TX_DESC_CNT - 3) /* TX wakeup count */
47 #define DESC_ALL_CNT (TX_DESC_CNT + RX_DESC_CNT)
48 #define TX_BUF_ALLOC 0x600
49 #define RX_ALLOC_SIZE 0x620
50 #define ULI526X_RESET 1
52 #define CR6_DEFAULT 0x22200000
53 #define CR7_DEFAULT 0x180c1
54 #define CR15_DEFAULT 0x06 /* TxJabber RxWatchdog */
55 #define TDES0_ERR_MASK 0x4302 /* TXJT, LC, EC, FUE */
56 #define MAX_PACKET_SIZE 1514
57 #define ULI5261_MAX_MULTICAST 14
58 #define RX_COPY_SIZE 100
59 #define MAX_CHECK_PACKET 0x8000
61 #define ULI526X_10MHF 0
62 #define ULI526X_100MHF 1
63 #define ULI526X_10MFD 4
64 #define ULI526X_100MFD 5
65 #define ULI526X_AUTO 8
67 #define ULI526X_TXTH_72 0x400000 /* TX TH 72 byte */
68 #define ULI526X_TXTH_96 0x404000 /* TX TH 96 byte */
69 #define ULI526X_TXTH_128 0x0000 /* TX TH 128 byte */
70 #define ULI526X_TXTH_256 0x4000 /* TX TH 256 byte */
71 #define ULI526X_TXTH_512 0x8000 /* TX TH 512 byte */
72 #define ULI526X_TXTH_1K 0xC000 /* TX TH 1K byte */
74 #define ULI526X_TIMER_WUT (jiffies + HZ * 1)/* timer wakeup time : 1 second */
75 #define ULI526X_TX_TIMEOUT ((16*HZ)/2) /* tx packet time-out time 8 s" */
76 #define ULI526X_TX_KICK (4*HZ/2) /* tx packet Kick-out time 2 s" */
78 #define ULI526X_DBUG(dbug_now, msg, value) \
80 if (uli526x_debug || (dbug_now)) \
81 pr_err("%s %lx\n", (msg), (long) (value)); \
84 #define SHOW_MEDIA_TYPE(mode) \
85 pr_err("Change Speed to %sMhz %s duplex\n", \
86 mode & 1 ? "100" : "10", \
87 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 /* Structure/enum declaration ------------------------------- */
107 __le32 tdes0
, tdes1
, tdes2
, tdes3
; /* Data for the card */
108 char *tx_buf_ptr
; /* Data for us */
109 struct tx_desc
*next_tx_desc
;
110 } __attribute__(( aligned(32) ));
113 __le32 rdes0
, rdes1
, rdes2
, rdes3
; /* Data for the card */
114 struct sk_buff
*rx_skb_ptr
; /* Data for us */
115 struct rx_desc
*next_rx_desc
;
116 } __attribute__(( aligned(32) ));
118 struct uli526x_board_info
{
120 void (*write
)(struct uli526x_board_info
*, u8
, u8
, u16
);
121 u16 (*read
)(struct uli526x_board_info
*, u8
, u8
);
123 struct net_device
*next_dev
; /* next device */
124 struct pci_dev
*pdev
; /* PCI device */
127 void __iomem
*ioaddr
; /* I/O base address */
134 /* pointer for memory physical address */
135 dma_addr_t buf_pool_dma_ptr
; /* Tx buffer pool memory */
136 dma_addr_t buf_pool_dma_start
; /* Tx buffer pool align dword */
137 dma_addr_t desc_pool_dma_ptr
; /* descriptor pool memory */
138 dma_addr_t first_tx_desc_dma
;
139 dma_addr_t first_rx_desc_dma
;
141 /* descriptor pointer */
142 unsigned char *buf_pool_ptr
; /* Tx buffer pool memory */
143 unsigned char *buf_pool_start
; /* Tx buffer pool align dword */
144 unsigned char *desc_pool_ptr
; /* descriptor pool memory */
145 struct tx_desc
*first_tx_desc
;
146 struct tx_desc
*tx_insert_ptr
;
147 struct tx_desc
*tx_remove_ptr
;
148 struct rx_desc
*first_rx_desc
;
149 struct rx_desc
*rx_insert_ptr
;
150 struct rx_desc
*rx_ready_ptr
; /* packet come pointer */
151 unsigned long tx_packet_cnt
; /* transmitted packet count */
152 unsigned long rx_avail_cnt
; /* available rx descriptor count */
153 unsigned long interval_rx_cnt
; /* rx packet count a callback time */
156 u16 NIC_capability
; /* NIC media capability */
157 u16 PHY_reg4
; /* Saved Phyxcer register 4 value */
159 u8 media_mode
; /* user specify media mode */
160 u8 op_mode
; /* real work media mode */
162 u8 link_failed
; /* Ever link failed */
163 u8 wait_reset
; /* Hardware failed, need to reset */
164 struct timer_list timer
;
166 /* Driver defined statistic counter */
167 unsigned long tx_fifo_underrun
;
168 unsigned long tx_loss_carrier
;
169 unsigned long tx_no_carrier
;
170 unsigned long tx_late_collision
;
171 unsigned long tx_excessive_collision
;
172 unsigned long tx_jabber_timeout
;
173 unsigned long reset_count
;
174 unsigned long reset_cr8
;
175 unsigned long reset_fatal
;
176 unsigned long reset_TXtimeout
;
179 unsigned char srom
[128];
183 enum uli526x_offsets
{
184 DCR0
= 0x00, DCR1
= 0x08, DCR2
= 0x10, DCR3
= 0x18, DCR4
= 0x20,
185 DCR5
= 0x28, DCR6
= 0x30, DCR7
= 0x38, DCR8
= 0x40, DCR9
= 0x48,
186 DCR10
= 0x50, DCR11
= 0x58, DCR12
= 0x60, DCR13
= 0x68, DCR14
= 0x70,
190 enum uli526x_CR6_bits
{
191 CR6_RXSC
= 0x2, CR6_PBF
= 0x8, CR6_PM
= 0x40, CR6_PAM
= 0x80,
192 CR6_FDM
= 0x200, CR6_TXSC
= 0x2000, CR6_STI
= 0x100000,
193 CR6_SFT
= 0x200000, CR6_RXA
= 0x40000000, CR6_NO_PURGE
= 0x20000000
196 /* Global variable declaration ----------------------------- */
197 static int uli526x_debug
;
198 static unsigned char uli526x_media_mode
= ULI526X_AUTO
;
199 static u32 uli526x_cr6_user_set
;
201 /* For module input parameter */
206 /* function declaration ------------------------------------- */
207 static int uli526x_open(struct net_device
*);
208 static netdev_tx_t
uli526x_start_xmit(struct sk_buff
*,
209 struct net_device
*);
210 static int uli526x_stop(struct net_device
*);
211 static void uli526x_set_filter_mode(struct net_device
*);
212 static const struct ethtool_ops netdev_ethtool_ops
;
213 static u16
read_srom_word(struct uli526x_board_info
*, int);
214 static irqreturn_t
uli526x_interrupt(int, void *);
215 #ifdef CONFIG_NET_POLL_CONTROLLER
216 static void uli526x_poll(struct net_device
*dev
);
218 static void uli526x_descriptor_init(struct net_device
*, void __iomem
*);
219 static void allocate_rx_buffer(struct net_device
*);
220 static void update_cr6(u32
, void __iomem
*);
221 static void send_filter_frame(struct net_device
*, int);
222 static u16
phy_readby_cr9(struct uli526x_board_info
*, u8
, u8
);
223 static u16
phy_readby_cr10(struct uli526x_board_info
*, u8
, u8
);
224 static void phy_writeby_cr9(struct uli526x_board_info
*, u8
, u8
, u16
);
225 static void phy_writeby_cr10(struct uli526x_board_info
*, u8
, u8
, u16
);
226 static void phy_write_1bit(struct uli526x_board_info
*db
, u32
);
227 static u16
phy_read_1bit(struct uli526x_board_info
*db
);
228 static u8
uli526x_sense_speed(struct uli526x_board_info
*);
229 static void uli526x_process_mode(struct uli526x_board_info
*);
230 static void uli526x_timer(struct timer_list
*t
);
231 static void uli526x_rx_packet(struct net_device
*, struct uli526x_board_info
*);
232 static void uli526x_free_tx_pkt(struct net_device
*, struct uli526x_board_info
*);
233 static void uli526x_reuse_skb(struct uli526x_board_info
*, struct sk_buff
*);
234 static void uli526x_dynamic_reset(struct net_device
*);
235 static void uli526x_free_rxbuffer(struct uli526x_board_info
*);
236 static void uli526x_init(struct net_device
*);
237 static void uli526x_set_phyxcer(struct uli526x_board_info
*);
239 static void srom_clk_write(struct uli526x_board_info
*db
, u32 data
)
241 void __iomem
*ioaddr
= db
->ioaddr
;
243 uw32(DCR9
, data
| CR9_SROM_READ
| CR9_SRCS
);
245 uw32(DCR9
, data
| CR9_SROM_READ
| CR9_SRCS
| CR9_SRCLK
);
247 uw32(DCR9
, data
| CR9_SROM_READ
| CR9_SRCS
);
251 /* ULI526X network board routine ---------------------------- */
253 static const struct net_device_ops netdev_ops
= {
254 .ndo_open
= uli526x_open
,
255 .ndo_stop
= uli526x_stop
,
256 .ndo_start_xmit
= uli526x_start_xmit
,
257 .ndo_set_rx_mode
= uli526x_set_filter_mode
,
258 .ndo_set_mac_address
= eth_mac_addr
,
259 .ndo_validate_addr
= eth_validate_addr
,
260 #ifdef CONFIG_NET_POLL_CONTROLLER
261 .ndo_poll_controller
= uli526x_poll
,
266 * Search ULI526X board, allocate space and register it
269 static int uli526x_init_one(struct pci_dev
*pdev
,
270 const struct pci_device_id
*ent
)
272 struct uli526x_board_info
*db
; /* board information structure */
273 struct net_device
*dev
;
274 void __iomem
*ioaddr
;
277 ULI526X_DBUG(0, "uli526x_init_one()", 0);
279 /* Init network device */
280 dev
= alloc_etherdev(sizeof(*db
));
283 SET_NETDEV_DEV(dev
, &pdev
->dev
);
285 if (dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(32))) {
286 pr_warn("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 pr_err("I/O base is zero\n");
299 goto err_out_disable
;
302 if (pci_resource_len(pdev
, 0) < (ULI526X_IO_SIZE
) ) {
303 pr_err("Allocated I/O size too small\n");
305 goto err_out_disable
;
308 err
= pci_request_regions(pdev
, DRV_NAME
);
310 pr_err("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 */
320 db
->desc_pool_ptr
= dma_alloc_coherent(&pdev
->dev
,
321 sizeof(struct tx_desc
) * DESC_ALL_CNT
+ 0x20,
322 &db
->desc_pool_dma_ptr
, GFP_KERNEL
);
323 if (!db
->desc_pool_ptr
)
324 goto err_out_release
;
326 db
->buf_pool_ptr
= dma_alloc_coherent(&pdev
->dev
,
327 TX_BUF_ALLOC
* TX_DESC_CNT
+ 4,
328 &db
->buf_pool_dma_ptr
, GFP_KERNEL
);
329 if (!db
->buf_pool_ptr
)
330 goto err_out_free_tx_desc
;
332 db
->first_tx_desc
= (struct tx_desc
*) db
->desc_pool_ptr
;
333 db
->first_tx_desc_dma
= db
->desc_pool_dma_ptr
;
334 db
->buf_pool_start
= db
->buf_pool_ptr
;
335 db
->buf_pool_dma_start
= db
->buf_pool_dma_ptr
;
337 switch (ent
->driver_data
) {
339 db
->phy
.write
= phy_writeby_cr10
;
340 db
->phy
.read
= phy_readby_cr10
;
343 db
->phy
.write
= phy_writeby_cr9
;
344 db
->phy
.read
= phy_readby_cr9
;
349 ioaddr
= pci_iomap(pdev
, 0, 0);
351 goto err_out_free_tx_buf
;
357 pci_set_drvdata(pdev
, dev
);
359 /* Register some necessary functions */
360 dev
->netdev_ops
= &netdev_ops
;
361 dev
->ethtool_ops
= &netdev_ethtool_ops
;
363 spin_lock_init(&db
->lock
);
366 /* read 64 word srom data */
367 for (i
= 0; i
< 64; i
++)
368 ((__le16
*) db
->srom
)[i
] = cpu_to_le16(read_srom_word(db
, i
));
370 /* Set Node address */
371 if(((u16
*) db
->srom
)[0] == 0xffff || ((u16
*) db
->srom
)[0] == 0) /* SROM absent, so read MAC address from ID Table */
373 uw32(DCR0
, 0x10000); //Diagnosis mode
374 uw32(DCR13
, 0x1c0); //Reset dianostic pointer port
375 uw32(DCR14
, 0); //Clear reset port
376 uw32(DCR14
, 0x10); //Reset ID Table pointer
377 uw32(DCR14
, 0); //Clear reset port
378 uw32(DCR13
, 0); //Clear CR13
379 uw32(DCR13
, 0x1b0); //Select ID Table access port
380 //Read MAC address from CR14
381 for (i
= 0; i
< 6; i
++)
382 dev
->dev_addr
[i
] = ur32(DCR14
);
384 uw32(DCR13
, 0); //Clear CR13
385 uw32(DCR0
, 0); //Clear CR0
390 for (i
= 0; i
< 6; i
++)
391 dev
->dev_addr
[i
] = db
->srom
[20 + i
];
393 err
= register_netdev (dev
);
397 netdev_info(dev
, "ULi M%04lx at pci%s, %pM, irq %d\n",
398 ent
->driver_data
>> 16, pci_name(pdev
),
399 dev
->dev_addr
, pdev
->irq
);
401 pci_set_master(pdev
);
406 pci_iounmap(pdev
, db
->ioaddr
);
408 dma_free_coherent(&pdev
->dev
, TX_BUF_ALLOC
* TX_DESC_CNT
+ 4,
409 db
->buf_pool_ptr
, db
->buf_pool_dma_ptr
);
410 err_out_free_tx_desc
:
411 dma_free_coherent(&pdev
->dev
,
412 sizeof(struct tx_desc
) * DESC_ALL_CNT
+ 0x20,
413 db
->desc_pool_ptr
, db
->desc_pool_dma_ptr
);
415 pci_release_regions(pdev
);
417 pci_disable_device(pdev
);
425 static void uli526x_remove_one(struct pci_dev
*pdev
)
427 struct net_device
*dev
= pci_get_drvdata(pdev
);
428 struct uli526x_board_info
*db
= netdev_priv(dev
);
430 unregister_netdev(dev
);
431 pci_iounmap(pdev
, db
->ioaddr
);
432 dma_free_coherent(&db
->pdev
->dev
,
433 sizeof(struct tx_desc
) * DESC_ALL_CNT
+ 0x20,
434 db
->desc_pool_ptr
, db
->desc_pool_dma_ptr
);
435 dma_free_coherent(&db
->pdev
->dev
, TX_BUF_ALLOC
* TX_DESC_CNT
+ 4,
436 db
->buf_pool_ptr
, db
->buf_pool_dma_ptr
);
437 pci_release_regions(pdev
);
438 pci_disable_device(pdev
);
444 * Open the interface.
445 * The interface is opened whenever "ifconfig" activates it.
448 static int uli526x_open(struct net_device
*dev
)
451 struct uli526x_board_info
*db
= netdev_priv(dev
);
453 ULI526X_DBUG(0, "uli526x_open", 0);
455 /* system variable init */
456 db
->cr6_data
= CR6_DEFAULT
| uli526x_cr6_user_set
;
457 db
->tx_packet_cnt
= 0;
458 db
->rx_avail_cnt
= 0;
460 netif_carrier_off(dev
);
463 db
->NIC_capability
= 0xf; /* All capability*/
464 db
->PHY_reg4
= 0x1e0;
466 /* CR6 operation mode decision */
467 db
->cr6_data
|= ULI526X_TXTH_256
;
468 db
->cr0_data
= CR0_DEFAULT
;
470 /* Initialize ULI526X board */
473 ret
= request_irq(db
->pdev
->irq
, uli526x_interrupt
, IRQF_SHARED
,
478 /* Active System Interface */
479 netif_wake_queue(dev
);
481 /* set and active a timer process */
482 timer_setup(&db
->timer
, uli526x_timer
, 0);
483 db
->timer
.expires
= ULI526X_TIMER_WUT
+ HZ
* 2;
484 add_timer(&db
->timer
);
490 /* Initialize ULI526X board
491 * Reset ULI526X board
492 * Initialize TX/Rx descriptor chain structure
493 * Send the set-up frame
494 * Enable Tx/Rx machine
497 static void uli526x_init(struct net_device
*dev
)
499 struct uli526x_board_info
*db
= netdev_priv(dev
);
500 struct uli_phy_ops
*phy
= &db
->phy
;
501 void __iomem
*ioaddr
= db
->ioaddr
;
507 ULI526X_DBUG(0, "uli526x_init()", 0);
509 /* Reset M526x MAC controller */
510 uw32(DCR0
, ULI526X_RESET
); /* RESET MAC */
512 uw32(DCR0
, db
->cr0_data
);
515 /* Phy addr : In some boards,M5261/M5263 phy address != 1 */
517 for (phy_tmp
= 0; phy_tmp
< 32; phy_tmp
++) {
520 phy_value
= phy
->read(db
, phy_tmp
, 3); //peer add
521 if (phy_value
!= 0xffff && phy_value
!= 0) {
522 db
->phy_addr
= phy_tmp
;
528 pr_warn("Can not find the phy address!!!\n");
529 /* Parser SROM and media mode */
530 db
->media_mode
= uli526x_media_mode
;
532 /* phyxcer capability setting */
533 phy_reg_reset
= phy
->read(db
, db
->phy_addr
, 0);
534 phy_reg_reset
= (phy_reg_reset
| 0x8000);
535 phy
->write(db
, db
->phy_addr
, 0, phy_reg_reset
);
537 /* See IEEE 802.3-2002.pdf (Section 2, Chapter "22.2.4 Management
538 * functions") or phy data sheet for details on phy reset
542 while (timeout
-- && phy
->read(db
, db
->phy_addr
, 0) & 0x8000)
545 /* Process Phyxcer Media Mode */
546 uli526x_set_phyxcer(db
);
548 /* Media Mode Process */
549 if ( !(db
->media_mode
& ULI526X_AUTO
) )
550 db
->op_mode
= db
->media_mode
; /* Force Mode */
552 /* Initialize Transmit/Receive descriptor and CR3/4 */
553 uli526x_descriptor_init(dev
, ioaddr
);
555 /* Init CR6 to program M526X operation */
556 update_cr6(db
->cr6_data
, ioaddr
);
558 /* Send setup frame */
559 send_filter_frame(dev
, netdev_mc_count(dev
)); /* M5261/M5263 */
561 /* Init CR7, interrupt active bit */
562 db
->cr7_data
= CR7_DEFAULT
;
563 uw32(DCR7
, db
->cr7_data
);
565 /* Init CR15, Tx jabber and Rx watchdog timer */
566 uw32(DCR15
, db
->cr15_data
);
568 /* Enable ULI526X Tx/Rx function */
569 db
->cr6_data
|= CR6_RXSC
| CR6_TXSC
;
570 update_cr6(db
->cr6_data
, ioaddr
);
575 * Hardware start transmission.
576 * Send a packet to media from the upper layer.
579 static netdev_tx_t
uli526x_start_xmit(struct sk_buff
*skb
,
580 struct net_device
*dev
)
582 struct uli526x_board_info
*db
= netdev_priv(dev
);
583 void __iomem
*ioaddr
= db
->ioaddr
;
584 struct tx_desc
*txptr
;
587 ULI526X_DBUG(0, "uli526x_start_xmit", 0);
589 /* Resource flag check */
590 netif_stop_queue(dev
);
592 /* Too large packet check */
593 if (skb
->len
> MAX_PACKET_SIZE
) {
594 netdev_err(dev
, "big packet = %d\n", (u16
)skb
->len
);
595 dev_kfree_skb_any(skb
);
599 spin_lock_irqsave(&db
->lock
, flags
);
601 /* No Tx resource check, it never happen nromally */
602 if (db
->tx_packet_cnt
>= TX_FREE_DESC_CNT
) {
603 spin_unlock_irqrestore(&db
->lock
, flags
);
604 netdev_err(dev
, "No Tx resource %ld\n", db
->tx_packet_cnt
);
605 return NETDEV_TX_BUSY
;
608 /* Disable NIC interrupt */
611 /* transmit this packet */
612 txptr
= db
->tx_insert_ptr
;
613 skb_copy_from_linear_data(skb
, txptr
->tx_buf_ptr
, skb
->len
);
614 txptr
->tdes1
= cpu_to_le32(0xe1000000 | skb
->len
);
616 /* Point to next transmit free descriptor */
617 db
->tx_insert_ptr
= txptr
->next_tx_desc
;
619 /* Transmit Packet Process */
620 if (db
->tx_packet_cnt
< TX_DESC_CNT
) {
621 txptr
->tdes0
= cpu_to_le32(0x80000000); /* Set owner bit */
622 db
->tx_packet_cnt
++; /* Ready to send */
623 uw32(DCR1
, 0x1); /* Issue Tx polling */
624 netif_trans_update(dev
); /* saved time stamp */
627 /* Tx resource check */
628 if ( db
->tx_packet_cnt
< TX_FREE_DESC_CNT
)
629 netif_wake_queue(dev
);
631 /* Restore CR7 to enable interrupt */
632 spin_unlock_irqrestore(&db
->lock
, flags
);
633 uw32(DCR7
, db
->cr7_data
);
636 dev_consume_skb_any(skb
);
643 * Stop the interface.
644 * The interface is stopped when it is brought.
647 static int uli526x_stop(struct net_device
*dev
)
649 struct uli526x_board_info
*db
= netdev_priv(dev
);
650 void __iomem
*ioaddr
= db
->ioaddr
;
653 netif_stop_queue(dev
);
656 del_timer_sync(&db
->timer
);
658 /* Reset & stop ULI526X board */
659 uw32(DCR0
, ULI526X_RESET
);
661 db
->phy
.write(db
, db
->phy_addr
, 0, 0x8000);
664 free_irq(db
->pdev
->irq
, dev
);
666 /* free allocated rx buffer */
667 uli526x_free_rxbuffer(db
);
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 void __iomem
*ioaddr
= db
->ioaddr
;
685 spin_lock_irqsave(&db
->lock
, flags
);
688 /* Got ULI526X status */
689 db
->cr5_data
= ur32(DCR5
);
690 uw32(DCR5
, db
->cr5_data
);
691 if ( !(db
->cr5_data
& 0x180c1) ) {
692 /* Restore CR7 to enable interrupt mask */
693 uw32(DCR7
, db
->cr7_data
);
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(dev
);
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 uw32(DCR7
, db
->cr7_data
);
723 spin_unlock_irqrestore(&db
->lock
, flags
);
727 #ifdef CONFIG_NET_POLL_CONTROLLER
728 static void uli526x_poll(struct net_device
*dev
)
730 struct uli526x_board_info
*db
= netdev_priv(dev
);
732 /* ISR grabs the irqsave lock, so this should be safe */
733 uli526x_interrupt(db
->pdev
->irq
, dev
);
738 * Free TX resource after TX complete
741 static void uli526x_free_tx_pkt(struct net_device
*dev
,
742 struct uli526x_board_info
* db
)
744 struct tx_desc
*txptr
;
747 txptr
= db
->tx_remove_ptr
;
748 while(db
->tx_packet_cnt
) {
749 tdes0
= le32_to_cpu(txptr
->tdes0
);
750 if (tdes0
& 0x80000000)
753 /* A packet sent completed */
755 dev
->stats
.tx_packets
++;
757 /* Transmit statistic counter */
758 if ( tdes0
!= 0x7fffffff ) {
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 dma_unmap_single(&db
->pdev
->dev
, le32_to_cpu(rxptr
->rdes2
),
819 RX_ALLOC_SIZE
, DMA_FROM_DEVICE
);
820 if ( (rdes0
& 0x300) != 0x300) {
821 /* A packet without First/Last flag */
823 ULI526X_DBUG(0, "Reuse SK buffer, rdes0", rdes0
);
824 uli526x_reuse_skb(db
, rxptr
->rx_skb_ptr
);
826 /* A packet with First/Last flag */
827 rxlen
= ( (rdes0
>> 16) & 0x3fff) - 4;
829 /* error summary bit check */
830 if (rdes0
& 0x8000) {
831 /* This is a error packet */
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 struct sk_buff
*new_skb
= NULL
;
845 skb
= rxptr
->rx_skb_ptr
;
847 /* Good packet, send to upper layer */
848 /* Shorst packet used new SKB */
849 if ((rxlen
< RX_COPY_SIZE
) &&
850 (((new_skb
= netdev_alloc_skb(dev
, rxlen
+ 2)) != NULL
))) {
852 /* size less than COPY_SIZE, allocate a rxlen SKB */
853 skb_reserve(skb
, 2); /* 16byte align */
855 skb_tail_pointer(rxptr
->rx_skb_ptr
),
857 uli526x_reuse_skb(db
, rxptr
->rx_skb_ptr
);
861 skb
->protocol
= eth_type_trans(skb
, dev
);
863 dev
->stats
.rx_packets
++;
864 dev
->stats
.rx_bytes
+= rxlen
;
867 /* Reuse SKB buffer when the packet is error */
868 ULI526X_DBUG(0, "Reuse SK buffer, rdes0", rdes0
);
869 uli526x_reuse_skb(db
, rxptr
->rx_skb_ptr
);
873 rxptr
= rxptr
->next_rx_desc
;
876 db
->rx_ready_ptr
= rxptr
;
881 * Set ULI526X multicast address
884 static void uli526x_set_filter_mode(struct net_device
* dev
)
886 struct uli526x_board_info
*db
= netdev_priv(dev
);
889 ULI526X_DBUG(0, "uli526x_set_filter_mode()", 0);
890 spin_lock_irqsave(&db
->lock
, flags
);
892 if (dev
->flags
& IFF_PROMISC
) {
893 ULI526X_DBUG(0, "Enable PROM Mode", 0);
894 db
->cr6_data
|= CR6_PM
| CR6_PBF
;
895 update_cr6(db
->cr6_data
, db
->ioaddr
);
896 spin_unlock_irqrestore(&db
->lock
, flags
);
900 if (dev
->flags
& IFF_ALLMULTI
||
901 netdev_mc_count(dev
) > ULI5261_MAX_MULTICAST
) {
902 ULI526X_DBUG(0, "Pass all multicast address",
903 netdev_mc_count(dev
));
904 db
->cr6_data
&= ~(CR6_PM
| CR6_PBF
);
905 db
->cr6_data
|= CR6_PAM
;
906 spin_unlock_irqrestore(&db
->lock
, flags
);
910 ULI526X_DBUG(0, "Set multicast address", netdev_mc_count(dev
));
911 send_filter_frame(dev
, netdev_mc_count(dev
)); /* M5261/M5263 */
912 spin_unlock_irqrestore(&db
->lock
, flags
);
916 ULi_ethtool_get_link_ksettings(struct uli526x_board_info
*db
,
917 struct ethtool_link_ksettings
*cmd
)
919 u32 supported
, advertising
;
921 supported
= (SUPPORTED_10baseT_Half
|
922 SUPPORTED_10baseT_Full
|
923 SUPPORTED_100baseT_Half
|
924 SUPPORTED_100baseT_Full
|
928 advertising
= (ADVERTISED_10baseT_Half
|
929 ADVERTISED_10baseT_Full
|
930 ADVERTISED_100baseT_Half
|
931 ADVERTISED_100baseT_Full
|
935 ethtool_convert_legacy_u32_to_link_mode(cmd
->link_modes
.supported
,
937 ethtool_convert_legacy_u32_to_link_mode(cmd
->link_modes
.advertising
,
940 cmd
->base
.port
= PORT_MII
;
941 cmd
->base
.phy_address
= db
->phy_addr
;
943 cmd
->base
.speed
= SPEED_10
;
944 cmd
->base
.duplex
= DUPLEX_HALF
;
946 if(db
->op_mode
==ULI526X_100MHF
|| db
->op_mode
==ULI526X_100MFD
)
948 cmd
->base
.speed
= SPEED_100
;
950 if(db
->op_mode
==ULI526X_10MFD
|| db
->op_mode
==ULI526X_100MFD
)
952 cmd
->base
.duplex
= DUPLEX_FULL
;
956 cmd
->base
.speed
= SPEED_UNKNOWN
;
957 cmd
->base
.duplex
= DUPLEX_UNKNOWN
;
960 if (db
->media_mode
& ULI526X_AUTO
)
962 cmd
->base
.autoneg
= AUTONEG_ENABLE
;
966 static void netdev_get_drvinfo(struct net_device
*dev
,
967 struct ethtool_drvinfo
*info
)
969 struct uli526x_board_info
*np
= netdev_priv(dev
);
971 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
972 strlcpy(info
->bus_info
, pci_name(np
->pdev
), sizeof(info
->bus_info
));
975 static int netdev_get_link_ksettings(struct net_device
*dev
,
976 struct ethtool_link_ksettings
*cmd
)
978 struct uli526x_board_info
*np
= netdev_priv(dev
);
980 ULi_ethtool_get_link_ksettings(np
, cmd
);
985 static u32
netdev_get_link(struct net_device
*dev
) {
986 struct uli526x_board_info
*np
= netdev_priv(dev
);
994 static void uli526x_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
996 wol
->supported
= WAKE_PHY
| WAKE_MAGIC
;
1000 static const struct ethtool_ops netdev_ethtool_ops
= {
1001 .get_drvinfo
= netdev_get_drvinfo
,
1002 .get_link
= netdev_get_link
,
1003 .get_wol
= uli526x_get_wol
,
1004 .get_link_ksettings
= netdev_get_link_ksettings
,
1008 * A periodic timer routine
1009 * Dynamic media sense, allocate Rx buffer...
1012 static void uli526x_timer(struct timer_list
*t
)
1014 struct uli526x_board_info
*db
= from_timer(db
, t
, timer
);
1015 struct net_device
*dev
= pci_get_drvdata(db
->pdev
);
1016 struct uli_phy_ops
*phy
= &db
->phy
;
1017 void __iomem
*ioaddr
= db
->ioaddr
;
1018 unsigned long flags
;
1022 //ULI526X_DBUG(0, "uli526x_timer()", 0);
1023 spin_lock_irqsave(&db
->lock
, flags
);
1026 /* Dynamic reset ULI526X : system error or transmit time-out */
1027 tmp_cr8
= ur32(DCR8
);
1028 if ( (db
->interval_rx_cnt
==0) && (tmp_cr8
) ) {
1032 db
->interval_rx_cnt
= 0;
1034 /* TX polling kick monitor */
1035 if ( db
->tx_packet_cnt
&&
1036 time_after(jiffies
, dev_trans_start(dev
) + ULI526X_TX_KICK
) ) {
1037 uw32(DCR1
, 0x1); // Tx polling again
1040 if ( time_after(jiffies
, dev_trans_start(dev
) + ULI526X_TX_TIMEOUT
) ) {
1041 db
->reset_TXtimeout
++;
1043 netdev_err(dev
, " Tx timeout - resetting\n");
1047 if (db
->wait_reset
) {
1048 ULI526X_DBUG(0, "Dynamic Reset device", db
->tx_packet_cnt
);
1050 uli526x_dynamic_reset(dev
);
1051 db
->timer
.expires
= ULI526X_TIMER_WUT
;
1052 add_timer(&db
->timer
);
1053 spin_unlock_irqrestore(&db
->lock
, flags
);
1057 /* Link status check, Dynamic media type change */
1058 if ((phy
->read(db
, db
->phy_addr
, 5) & 0x01e0)!=0)
1061 if ( !(tmp_cr12
& 0x3) && !db
->link_failed
) {
1063 ULI526X_DBUG(0, "Link Failed", tmp_cr12
);
1064 netif_carrier_off(dev
);
1065 netdev_info(dev
, "NIC Link is Down\n");
1066 db
->link_failed
= 1;
1068 /* For Force 10/100M Half/Full mode: Enable Auto-Nego mode */
1069 /* AUTO don't need */
1070 if ( !(db
->media_mode
& 0x8) )
1071 phy
->write(db
, db
->phy_addr
, 0, 0x1000);
1073 /* AUTO mode, if INT phyxcer link failed, select EXT device */
1074 if (db
->media_mode
& ULI526X_AUTO
) {
1075 db
->cr6_data
&=~0x00000200; /* bit9=0, HD mode */
1076 update_cr6(db
->cr6_data
, db
->ioaddr
);
1079 if ((tmp_cr12
& 0x3) && db
->link_failed
) {
1080 ULI526X_DBUG(0, "Link link OK", tmp_cr12
);
1081 db
->link_failed
= 0;
1083 /* Auto Sense Speed */
1084 if ( (db
->media_mode
& ULI526X_AUTO
) &&
1085 uli526x_sense_speed(db
) )
1086 db
->link_failed
= 1;
1087 uli526x_process_mode(db
);
1089 if(db
->link_failed
==0)
1091 netdev_info(dev
, "NIC Link is Up %d Mbps %s duplex\n",
1092 (db
->op_mode
== ULI526X_100MHF
||
1093 db
->op_mode
== ULI526X_100MFD
)
1095 (db
->op_mode
== ULI526X_10MFD
||
1096 db
->op_mode
== ULI526X_100MFD
)
1098 netif_carrier_on(dev
);
1100 /* SHOW_MEDIA_TYPE(db->op_mode); */
1102 else if(!(tmp_cr12
& 0x3) && db
->link_failed
)
1106 netdev_info(dev
, "NIC Link is Down\n");
1107 netif_carrier_off(dev
);
1112 /* Timer active again */
1113 db
->timer
.expires
= ULI526X_TIMER_WUT
;
1114 add_timer(&db
->timer
);
1115 spin_unlock_irqrestore(&db
->lock
, flags
);
1120 * Stop ULI526X board
1121 * Free Tx/Rx allocated memory
1122 * Init system variable
1125 static void uli526x_reset_prepare(struct net_device
*dev
)
1127 struct uli526x_board_info
*db
= netdev_priv(dev
);
1128 void __iomem
*ioaddr
= db
->ioaddr
;
1130 /* Sopt MAC controller */
1131 db
->cr6_data
&= ~(CR6_RXSC
| CR6_TXSC
); /* Disable Tx/Rx */
1132 update_cr6(db
->cr6_data
, ioaddr
);
1133 uw32(DCR7
, 0); /* Disable Interrupt */
1134 uw32(DCR5
, ur32(DCR5
));
1136 /* Disable upper layer interface */
1137 netif_stop_queue(dev
);
1139 /* Free Rx Allocate buffer */
1140 uli526x_free_rxbuffer(db
);
1142 /* system variable init */
1143 db
->tx_packet_cnt
= 0;
1144 db
->rx_avail_cnt
= 0;
1145 db
->link_failed
= 1;
1152 * Dynamic reset the ULI526X board
1153 * Stop ULI526X board
1154 * Free Tx/Rx allocated memory
1155 * Reset ULI526X board
1156 * Re-initialize ULI526X board
1159 static void uli526x_dynamic_reset(struct net_device
*dev
)
1161 ULI526X_DBUG(0, "uli526x_dynamic_reset()", 0);
1163 uli526x_reset_prepare(dev
);
1165 /* Re-initialize ULI526X board */
1168 /* Restart upper layer interface */
1169 netif_wake_queue(dev
);
1173 * Suspend the interface.
1176 static int __maybe_unused
uli526x_suspend(struct device
*dev_d
)
1178 struct net_device
*dev
= dev_get_drvdata(dev_d
);
1180 ULI526X_DBUG(0, "uli526x_suspend", 0);
1182 if (!netif_running(dev
))
1185 netif_device_detach(dev
);
1186 uli526x_reset_prepare(dev
);
1188 device_set_wakeup_enable(dev_d
, 0);
1194 * Resume the interface.
1197 static int __maybe_unused
uli526x_resume(struct device
*dev_d
)
1199 struct net_device
*dev
= dev_get_drvdata(dev_d
);
1201 ULI526X_DBUG(0, "uli526x_resume", 0);
1204 if (!netif_running(dev
))
1207 netif_device_attach(dev
);
1208 /* Re-initialize ULI526X board */
1210 /* Restart upper layer interface */
1211 netif_wake_queue(dev
);
1217 * free all allocated rx buffer
1220 static void uli526x_free_rxbuffer(struct uli526x_board_info
* db
)
1222 ULI526X_DBUG(0, "uli526x_free_rxbuffer()", 0);
1224 /* free allocated rx buffer */
1225 while (db
->rx_avail_cnt
) {
1226 dev_kfree_skb(db
->rx_ready_ptr
->rx_skb_ptr
);
1227 db
->rx_ready_ptr
= db
->rx_ready_ptr
->next_rx_desc
;
1234 * Reuse the SK buffer
1237 static void uli526x_reuse_skb(struct uli526x_board_info
*db
, struct sk_buff
* skb
)
1239 struct rx_desc
*rxptr
= db
->rx_insert_ptr
;
1241 if (!(rxptr
->rdes0
& cpu_to_le32(0x80000000))) {
1242 rxptr
->rx_skb_ptr
= skb
;
1243 rxptr
->rdes2
= cpu_to_le32(dma_map_single(&db
->pdev
->dev
, skb_tail_pointer(skb
),
1244 RX_ALLOC_SIZE
, DMA_FROM_DEVICE
));
1246 rxptr
->rdes0
= cpu_to_le32(0x80000000);
1248 db
->rx_insert_ptr
= rxptr
->next_rx_desc
;
1250 ULI526X_DBUG(0, "SK Buffer reuse method error", db
->rx_avail_cnt
);
1255 * Initialize transmit/Receive descriptor
1256 * Using Chain structure, and allocate Tx/Rx buffer
1259 static void uli526x_descriptor_init(struct net_device
*dev
, void __iomem
*ioaddr
)
1261 struct uli526x_board_info
*db
= netdev_priv(dev
);
1262 struct tx_desc
*tmp_tx
;
1263 struct rx_desc
*tmp_rx
;
1264 unsigned char *tmp_buf
;
1265 dma_addr_t tmp_tx_dma
, tmp_rx_dma
;
1266 dma_addr_t tmp_buf_dma
;
1269 ULI526X_DBUG(0, "uli526x_descriptor_init()", 0);
1271 /* tx descriptor start pointer */
1272 db
->tx_insert_ptr
= db
->first_tx_desc
;
1273 db
->tx_remove_ptr
= db
->first_tx_desc
;
1274 uw32(DCR4
, db
->first_tx_desc_dma
); /* TX DESC address */
1276 /* rx descriptor start pointer */
1277 db
->first_rx_desc
= (void *)db
->first_tx_desc
+ sizeof(struct tx_desc
) * TX_DESC_CNT
;
1278 db
->first_rx_desc_dma
= db
->first_tx_desc_dma
+ sizeof(struct tx_desc
) * TX_DESC_CNT
;
1279 db
->rx_insert_ptr
= db
->first_rx_desc
;
1280 db
->rx_ready_ptr
= db
->first_rx_desc
;
1281 uw32(DCR3
, db
->first_rx_desc_dma
); /* RX DESC address */
1283 /* Init Transmit chain */
1284 tmp_buf
= db
->buf_pool_start
;
1285 tmp_buf_dma
= db
->buf_pool_dma_start
;
1286 tmp_tx_dma
= db
->first_tx_desc_dma
;
1287 for (tmp_tx
= db
->first_tx_desc
, i
= 0; i
< TX_DESC_CNT
; i
++, tmp_tx
++) {
1288 tmp_tx
->tx_buf_ptr
= tmp_buf
;
1289 tmp_tx
->tdes0
= cpu_to_le32(0);
1290 tmp_tx
->tdes1
= cpu_to_le32(0x81000000); /* IC, chain */
1291 tmp_tx
->tdes2
= cpu_to_le32(tmp_buf_dma
);
1292 tmp_tx_dma
+= sizeof(struct tx_desc
);
1293 tmp_tx
->tdes3
= cpu_to_le32(tmp_tx_dma
);
1294 tmp_tx
->next_tx_desc
= tmp_tx
+ 1;
1295 tmp_buf
= tmp_buf
+ TX_BUF_ALLOC
;
1296 tmp_buf_dma
= tmp_buf_dma
+ TX_BUF_ALLOC
;
1298 (--tmp_tx
)->tdes3
= cpu_to_le32(db
->first_tx_desc_dma
);
1299 tmp_tx
->next_tx_desc
= db
->first_tx_desc
;
1301 /* Init Receive descriptor chain */
1302 tmp_rx_dma
=db
->first_rx_desc_dma
;
1303 for (tmp_rx
= db
->first_rx_desc
, i
= 0; i
< RX_DESC_CNT
; i
++, tmp_rx
++) {
1304 tmp_rx
->rdes0
= cpu_to_le32(0);
1305 tmp_rx
->rdes1
= cpu_to_le32(0x01000600);
1306 tmp_rx_dma
+= sizeof(struct rx_desc
);
1307 tmp_rx
->rdes3
= cpu_to_le32(tmp_rx_dma
);
1308 tmp_rx
->next_rx_desc
= tmp_rx
+ 1;
1310 (--tmp_rx
)->rdes3
= cpu_to_le32(db
->first_rx_desc_dma
);
1311 tmp_rx
->next_rx_desc
= db
->first_rx_desc
;
1313 /* pre-allocate Rx buffer */
1314 allocate_rx_buffer(dev
);
1320 * Firstly stop ULI526X, then written value and start
1322 static void update_cr6(u32 cr6_data
, void __iomem
*ioaddr
)
1324 uw32(DCR6
, cr6_data
);
1330 * Send a setup frame for M5261/M5263
1331 * This setup frame initialize ULI526X address filter mode
1335 #define FLT_SHIFT 16
1340 static void send_filter_frame(struct net_device
*dev
, int mc_cnt
)
1342 struct uli526x_board_info
*db
= netdev_priv(dev
);
1343 void __iomem
*ioaddr
= db
->ioaddr
;
1344 struct netdev_hw_addr
*ha
;
1345 struct tx_desc
*txptr
;
1350 ULI526X_DBUG(0, "send_filter_frame()", 0);
1352 txptr
= db
->tx_insert_ptr
;
1353 suptr
= (u32
*) txptr
->tx_buf_ptr
;
1356 addrptr
= (u16
*) dev
->dev_addr
;
1357 *suptr
++ = addrptr
[0] << FLT_SHIFT
;
1358 *suptr
++ = addrptr
[1] << FLT_SHIFT
;
1359 *suptr
++ = addrptr
[2] << FLT_SHIFT
;
1361 /* broadcast address */
1362 *suptr
++ = 0xffff << FLT_SHIFT
;
1363 *suptr
++ = 0xffff << FLT_SHIFT
;
1364 *suptr
++ = 0xffff << FLT_SHIFT
;
1366 /* fit the multicast address */
1367 netdev_for_each_mc_addr(ha
, dev
) {
1368 addrptr
= (u16
*) ha
->addr
;
1369 *suptr
++ = addrptr
[0] << FLT_SHIFT
;
1370 *suptr
++ = addrptr
[1] << FLT_SHIFT
;
1371 *suptr
++ = addrptr
[2] << FLT_SHIFT
;
1374 for (i
= netdev_mc_count(dev
); i
< 14; i
++) {
1375 *suptr
++ = 0xffff << FLT_SHIFT
;
1376 *suptr
++ = 0xffff << FLT_SHIFT
;
1377 *suptr
++ = 0xffff << FLT_SHIFT
;
1380 /* prepare the setup frame */
1381 db
->tx_insert_ptr
= txptr
->next_tx_desc
;
1382 txptr
->tdes1
= cpu_to_le32(0x890000c0);
1384 /* Resource Check and Send the setup packet */
1385 if (db
->tx_packet_cnt
< TX_DESC_CNT
) {
1386 /* Resource Empty */
1387 db
->tx_packet_cnt
++;
1388 txptr
->tdes0
= cpu_to_le32(0x80000000);
1389 update_cr6(db
->cr6_data
| 0x2000, ioaddr
);
1390 uw32(DCR1
, 0x1); /* Issue Tx polling */
1391 update_cr6(db
->cr6_data
, ioaddr
);
1392 netif_trans_update(dev
);
1394 netdev_err(dev
, "No Tx resource - Send_filter_frame!\n");
1399 * Allocate rx buffer,
1400 * As possible as allocate maxiumn Rx buffer
1403 static void allocate_rx_buffer(struct net_device
*dev
)
1405 struct uli526x_board_info
*db
= netdev_priv(dev
);
1406 struct rx_desc
*rxptr
;
1407 struct sk_buff
*skb
;
1409 rxptr
= db
->rx_insert_ptr
;
1411 while(db
->rx_avail_cnt
< RX_DESC_CNT
) {
1412 skb
= netdev_alloc_skb(dev
, RX_ALLOC_SIZE
);
1415 rxptr
->rx_skb_ptr
= skb
; /* FIXME (?) */
1416 rxptr
->rdes2
= cpu_to_le32(dma_map_single(&db
->pdev
->dev
, skb_tail_pointer(skb
),
1417 RX_ALLOC_SIZE
, DMA_FROM_DEVICE
));
1419 rxptr
->rdes0
= cpu_to_le32(0x80000000);
1420 rxptr
= rxptr
->next_rx_desc
;
1424 db
->rx_insert_ptr
= rxptr
;
1429 * Read one word data from the serial ROM
1432 static u16
read_srom_word(struct uli526x_board_info
*db
, int offset
)
1434 void __iomem
*ioaddr
= db
->ioaddr
;
1438 uw32(DCR9
, CR9_SROM_READ
);
1439 uw32(DCR9
, CR9_SROM_READ
| CR9_SRCS
);
1441 /* Send the Read Command 110b */
1442 srom_clk_write(db
, SROM_DATA_1
);
1443 srom_clk_write(db
, SROM_DATA_1
);
1444 srom_clk_write(db
, SROM_DATA_0
);
1446 /* Send the offset */
1447 for (i
= 5; i
>= 0; i
--) {
1448 srom_data
= (offset
& (1 << i
)) ? SROM_DATA_1
: SROM_DATA_0
;
1449 srom_clk_write(db
, srom_data
);
1452 uw32(DCR9
, CR9_SROM_READ
| CR9_SRCS
);
1454 for (i
= 16; i
> 0; i
--) {
1455 uw32(DCR9
, CR9_SROM_READ
| CR9_SRCS
| CR9_SRCLK
);
1457 srom_data
= (srom_data
<< 1) |
1458 ((ur32(DCR9
) & CR9_CRDOUT
) ? 1 : 0);
1459 uw32(DCR9
, CR9_SROM_READ
| CR9_SRCS
);
1463 uw32(DCR9
, CR9_SROM_READ
);
1469 * Auto sense the media mode
1472 static u8
uli526x_sense_speed(struct uli526x_board_info
* db
)
1474 struct uli_phy_ops
*phy
= &db
->phy
;
1478 phy_mode
= phy
->read(db
, db
->phy_addr
, 1);
1479 phy_mode
= phy
->read(db
, db
->phy_addr
, 1);
1481 if ( (phy_mode
& 0x24) == 0x24 ) {
1483 phy_mode
= ((phy
->read(db
, db
->phy_addr
, 5) & 0x01e0)<<7);
1486 else if(phy_mode
&0x4000)
1488 else if(phy_mode
&0x2000)
1494 case 0x1000: db
->op_mode
= ULI526X_10MHF
; break;
1495 case 0x2000: db
->op_mode
= ULI526X_10MFD
; break;
1496 case 0x4000: db
->op_mode
= ULI526X_100MHF
; break;
1497 case 0x8000: db
->op_mode
= ULI526X_100MFD
; break;
1498 default: db
->op_mode
= ULI526X_10MHF
; ErrFlag
= 1; break;
1501 db
->op_mode
= ULI526X_10MHF
;
1502 ULI526X_DBUG(0, "Link Failed :", phy_mode
);
1511 * Set 10/100 phyxcer capability
1512 * AUTO mode : phyxcer register4 is NIC capability
1513 * Force mode: phyxcer register4 is the force media
1516 static void uli526x_set_phyxcer(struct uli526x_board_info
*db
)
1518 struct uli_phy_ops
*phy
= &db
->phy
;
1521 /* Phyxcer capability setting */
1522 phy_reg
= phy
->read(db
, db
->phy_addr
, 4) & ~0x01e0;
1524 if (db
->media_mode
& ULI526X_AUTO
) {
1526 phy_reg
|= db
->PHY_reg4
;
1529 switch(db
->media_mode
) {
1530 case ULI526X_10MHF
: phy_reg
|= 0x20; break;
1531 case ULI526X_10MFD
: phy_reg
|= 0x40; break;
1532 case ULI526X_100MHF
: phy_reg
|= 0x80; break;
1533 case ULI526X_100MFD
: phy_reg
|= 0x100; break;
1538 /* Write new capability to Phyxcer Reg4 */
1539 if ( !(phy_reg
& 0x01e0)) {
1540 phy_reg
|=db
->PHY_reg4
;
1541 db
->media_mode
|=ULI526X_AUTO
;
1543 phy
->write(db
, db
->phy_addr
, 4, phy_reg
);
1545 /* Restart Auto-Negotiation */
1546 phy
->write(db
, db
->phy_addr
, 0, 0x1200);
1553 AUTO mode : PHY controller in Auto-negotiation Mode
1554 * Force mode: PHY controller in force mode with HUB
1555 * N-way force capability with SWITCH
1558 static void uli526x_process_mode(struct uli526x_board_info
*db
)
1560 struct uli_phy_ops
*phy
= &db
->phy
;
1563 /* Full Duplex Mode Check */
1564 if (db
->op_mode
& 0x4)
1565 db
->cr6_data
|= CR6_FDM
; /* Set Full Duplex Bit */
1567 db
->cr6_data
&= ~CR6_FDM
; /* Clear Full Duplex Bit */
1569 update_cr6(db
->cr6_data
, db
->ioaddr
);
1571 /* 10/100M phyxcer force mode need */
1572 if (!(db
->media_mode
& 0x8)) {
1574 phy_reg
= phy
->read(db
, db
->phy_addr
, 6);
1575 if (!(phy_reg
& 0x1)) {
1576 /* parter without N-Way capability */
1578 switch(db
->op_mode
) {
1579 case ULI526X_10MHF
: phy_reg
= 0x0; break;
1580 case ULI526X_10MFD
: phy_reg
= 0x100; break;
1581 case ULI526X_100MHF
: phy_reg
= 0x2000; break;
1582 case ULI526X_100MFD
: phy_reg
= 0x2100; break;
1584 phy
->write(db
, db
->phy_addr
, 0, phy_reg
);
1590 /* M5261/M5263 Chip */
1591 static void phy_writeby_cr9(struct uli526x_board_info
*db
, u8 phy_addr
,
1592 u8 offset
, u16 phy_data
)
1596 /* Send 33 synchronization clock to Phy controller */
1597 for (i
= 0; i
< 35; i
++)
1598 phy_write_1bit(db
, PHY_DATA_1
);
1600 /* Send start command(01) to Phy */
1601 phy_write_1bit(db
, PHY_DATA_0
);
1602 phy_write_1bit(db
, PHY_DATA_1
);
1604 /* Send write command(01) to Phy */
1605 phy_write_1bit(db
, PHY_DATA_0
);
1606 phy_write_1bit(db
, PHY_DATA_1
);
1608 /* Send Phy address */
1609 for (i
= 0x10; i
> 0; i
= i
>> 1)
1610 phy_write_1bit(db
, phy_addr
& i
? PHY_DATA_1
: PHY_DATA_0
);
1612 /* Send register address */
1613 for (i
= 0x10; i
> 0; i
= i
>> 1)
1614 phy_write_1bit(db
, offset
& i
? PHY_DATA_1
: PHY_DATA_0
);
1616 /* written trasnition */
1617 phy_write_1bit(db
, PHY_DATA_1
);
1618 phy_write_1bit(db
, PHY_DATA_0
);
1620 /* Write a word data to PHY controller */
1621 for (i
= 0x8000; i
> 0; i
>>= 1)
1622 phy_write_1bit(db
, phy_data
& i
? PHY_DATA_1
: PHY_DATA_0
);
1625 static u16
phy_readby_cr9(struct uli526x_board_info
*db
, u8 phy_addr
, u8 offset
)
1630 /* Send 33 synchronization clock to Phy controller */
1631 for (i
= 0; i
< 35; i
++)
1632 phy_write_1bit(db
, PHY_DATA_1
);
1634 /* Send start command(01) to Phy */
1635 phy_write_1bit(db
, PHY_DATA_0
);
1636 phy_write_1bit(db
, PHY_DATA_1
);
1638 /* Send read command(10) to Phy */
1639 phy_write_1bit(db
, PHY_DATA_1
);
1640 phy_write_1bit(db
, PHY_DATA_0
);
1642 /* Send Phy address */
1643 for (i
= 0x10; i
> 0; i
= i
>> 1)
1644 phy_write_1bit(db
, phy_addr
& i
? PHY_DATA_1
: PHY_DATA_0
);
1646 /* Send register address */
1647 for (i
= 0x10; i
> 0; i
= i
>> 1)
1648 phy_write_1bit(db
, offset
& i
? PHY_DATA_1
: PHY_DATA_0
);
1650 /* Skip transition state */
1653 /* read 16bit data */
1654 for (phy_data
= 0, i
= 0; i
< 16; i
++) {
1656 phy_data
|= phy_read_1bit(db
);
1662 static u16
phy_readby_cr10(struct uli526x_board_info
*db
, u8 phy_addr
,
1665 void __iomem
*ioaddr
= db
->ioaddr
;
1666 u32 cr10_value
= phy_addr
;
1668 cr10_value
= (cr10_value
<< 5) + offset
;
1669 cr10_value
= (cr10_value
<< 16) + 0x08000000;
1670 uw32(DCR10
, cr10_value
);
1673 cr10_value
= ur32(DCR10
);
1674 if (cr10_value
& 0x10000000)
1677 return cr10_value
& 0x0ffff;
1680 static void phy_writeby_cr10(struct uli526x_board_info
*db
, u8 phy_addr
,
1681 u8 offset
, u16 phy_data
)
1683 void __iomem
*ioaddr
= db
->ioaddr
;
1684 u32 cr10_value
= phy_addr
;
1686 cr10_value
= (cr10_value
<< 5) + offset
;
1687 cr10_value
= (cr10_value
<< 16) + 0x04000000 + phy_data
;
1688 uw32(DCR10
, cr10_value
);
1692 * Write one bit data to Phy Controller
1695 static void phy_write_1bit(struct uli526x_board_info
*db
, u32 data
)
1697 void __iomem
*ioaddr
= db
->ioaddr
;
1699 uw32(DCR9
, data
); /* MII Clock Low */
1701 uw32(DCR9
, data
| MDCLKH
); /* MII Clock High */
1703 uw32(DCR9
, data
); /* MII Clock Low */
1709 * Read one bit phy data from PHY controller
1712 static u16
phy_read_1bit(struct uli526x_board_info
*db
)
1714 void __iomem
*ioaddr
= db
->ioaddr
;
1717 uw32(DCR9
, 0x50000);
1719 phy_data
= (ur32(DCR9
) >> 19) & 0x1;
1720 uw32(DCR9
, 0x40000);
1727 static const struct pci_device_id uli526x_pci_tbl
[] = {
1728 { 0x10B9, 0x5261, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, PCI_ULI5261_ID
},
1729 { 0x10B9, 0x5263, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, PCI_ULI5263_ID
},
1732 MODULE_DEVICE_TABLE(pci
, uli526x_pci_tbl
);
1734 static SIMPLE_DEV_PM_OPS(uli526x_pm_ops
, uli526x_suspend
, uli526x_resume
);
1736 static struct pci_driver uli526x_driver
= {
1738 .id_table
= uli526x_pci_tbl
,
1739 .probe
= uli526x_init_one
,
1740 .remove
= uli526x_remove_one
,
1741 .driver
.pm
= &uli526x_pm_ops
,
1744 MODULE_AUTHOR("Peer Chen, peer.chen@uli.com.tw");
1745 MODULE_DESCRIPTION("ULi M5261/M5263 fast ethernet driver");
1746 MODULE_LICENSE("GPL");
1748 module_param(debug
, int, 0644);
1749 module_param(mode
, int, 0);
1750 module_param(cr6set
, int, 0);
1751 MODULE_PARM_DESC(debug
, "ULi M5261/M5263 enable debugging (0-1)");
1752 MODULE_PARM_DESC(mode
, "ULi M5261/M5263: Bit 0: 10/100Mbps, bit 2: duplex, bit 8: HomePNA");
1755 * when user used insmod to add module, system invoked init_module()
1756 * to register the services.
1759 static int __init
uli526x_init_module(void)
1762 ULI526X_DBUG(0, "init_module() ", debug
);
1765 uli526x_debug
= debug
; /* set debug flag */
1767 uli526x_cr6_user_set
= cr6set
;
1771 case ULI526X_100MHF
:
1773 case ULI526X_100MFD
:
1774 uli526x_media_mode
= mode
;
1777 uli526x_media_mode
= ULI526X_AUTO
;
1781 return pci_register_driver(&uli526x_driver
);
1787 * when user used rmmod to delete module, system invoked clean_module()
1788 * to un-register all registered services.
1791 static void __exit
uli526x_cleanup_module(void)
1793 ULI526X_DBUG(0, "uli526x_cleanup_module() ", debug
);
1794 pci_unregister_driver(&uli526x_driver
);
1797 module_init(uli526x_init_module
);
1798 module_exit(uli526x_cleanup_module
);