24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/kernel.h>
28 #include <linux/module.h>
30 #include <linux/netdevice.h>
32 #include <linux/ethtool.h>
38 #define DRV_NAME "ks8842"
41 #define REG_TIMB_RST 0x1c
42 #define REG_TIMB_FIFO 0x20
43 #define REG_TIMB_ISR 0x24
44 #define REG_TIMB_IER 0x28
45 #define REG_TIMB_IAR 0x2C
46 #define REQ_TIMB_DMA_RESUME 0x30
50 #define REG_SELECT_BANK 0x0e
53 #define REG_QRFCR 0x04
67 #define REG_TXMIR 0x08
68 #define REG_RXMIR 0x0A
71 #define REG_TXQCR 0x00
72 #define REG_RXQCR 0x02
73 #define REG_TXFDPR 0x04
74 #define REG_RXFDPR 0x06
75 #define REG_QMU_DATA_LO 0x08
76 #define REG_QMU_DATA_HI 0x0A
80 #define IRQ_LINK_CHANGE 0x8000
83 #define IRQ_RX_OVERRUN 0x0800
84 #define IRQ_TX_STOPPED 0x0200
85 #define IRQ_RX_STOPPED 0x0100
86 #define IRQ_RX_ERROR 0x0080
87 #define ENABLED_IRQS (IRQ_LINK_CHANGE | IRQ_TX | IRQ_RX | IRQ_RX_STOPPED | \
88 IRQ_TX_STOPPED | IRQ_RX_OVERRUN | IRQ_RX_ERROR)
95 #define ENABLED_IRQS_DMA_IP (IRQ_LINK_CHANGE | IRQ_RX_STOPPED | \
96 IRQ_TX_STOPPED | IRQ_RX_OVERRUN | IRQ_RX_ERROR)
97 #define ENABLED_IRQS_DMA (ENABLED_IRQS_DMA_IP | IRQ_RX)
100 #define RXSR_VALID 0x8000
101 #define RXSR_BROADCAST 0x80
102 #define RXSR_MULTICAST 0x40
103 #define RXSR_UNICAST 0x20
104 #define RXSR_FRAMETYPE 0x08
105 #define RXSR_TOO_LONG 0x04
106 #define RXSR_RUNT 0x02
107 #define RXSR_CRC_ERROR 0x01
108 #define RXSR_ERROR (RXSR_TOO_LONG | RXSR_RUNT | RXSR_CRC_ERROR)
111 #define REG_SW_ID_AND_ENABLE 0x00
112 #define REG_SGCR1 0x02
113 #define REG_SGCR2 0x04
114 #define REG_SGCR3 0x06
117 #define REG_MACAR1 0x00
118 #define REG_MACAR2 0x02
119 #define REG_MACAR3 0x04
122 #define REG_P1MBCR 0x00
123 #define REG_P1MBSR 0x02
126 #define REG_P2MBCR 0x00
127 #define REG_P2MBSR 0x02
130 #define REG_P1CR2 0x02
133 #define REG_P1CR4 0x02
134 #define REG_P1SR 0x04
137 #define MICREL_KS884X 0x01
138 #define KS884X_16BIT 0x02
140 #define DMA_BUFFER_SIZE 2048
159 #define KS8842_USE_DMA(adapter) (((adapter)->dma_tx.channel != -1) && \
160 ((adapter)->dma_rx.channel != -1))
175 static void ks8842_dma_rx_cb(
void *
data);
176 static void ks8842_dma_tx_cb(
void *
data);
191 ks8842_select_bank(adapter, bank);
198 ks8842_select_bank(adapter, bank);
206 ks8842_select_bank(adapter, bank);
216 ks8842_select_bank(adapter, bank);
225 ks8842_select_bank(adapter, bank);
232 ks8842_select_bank(adapter, bank);
239 ks8842_select_bank(adapter, bank);
246 ks8842_select_bank(adapter, bank);
253 ks8842_write16(adapter, 3, 1,
REG_GRR);
269 static void ks8842_update_link_status(
struct net_device *netdev,
273 if (ks8842_read16(adapter, 45,
REG_P1MBSR) & 0x4) {
275 netif_wake_queue(netdev);
277 netif_stop_queue(netdev);
284 ks8842_enable_bits(adapter, 16, 0x01,
REG_TXCR);
289 ks8842_clear_bits(adapter, 16, 0x01,
REG_TXCR);
294 ks8842_enable_bits(adapter, 16, 0x01,
REG_RXCR);
299 ks8842_clear_bits(adapter, 16, 0x01,
REG_RXCR);
305 ks8842_reset(adapter);
308 ks8842_write16(adapter, 16, 0x000E,
REG_TXCR);
312 ks8842_write16(adapter, 16, 0x8 | 0x20 | 0x40 | 0x80 | 0x400,
316 ks8842_write16(adapter, 17, 0x4000,
REG_TXFDPR);
319 ks8842_write16(adapter, 17, 0x4000,
REG_RXFDPR);
322 ks8842_write16(adapter, 0, 0x1000,
REG_QRFCR);
325 ks8842_enable_bits(adapter, 32, 1 << 8,
REG_SGCR1);
328 ks8842_enable_bits(adapter, 32, 1 << 3,
REG_SGCR2);
331 ks8842_write16(adapter, 48, 0x1E07,
REG_P1CR2);
334 ks8842_enable_bits(adapter, 49, 1 << 13,
REG_P1CR4);
337 ks8842_enable_tx(adapter);
340 ks8842_enable_rx(adapter);
343 ks8842_write16(adapter, 18, 0xffff,
REG_ISR);
369 dest[ETH_ALEN - i - 1] = ks8842_read8(adapter, 2,
REG_MARL + i);
377 mac = ks8842_read16(adapter, 2,
REG_MARL);
379 mac = ks8842_read16(adapter, 2,
REG_MARM);
381 mac = ks8842_read16(adapter, 2,
REG_MARH);
386 mac = ks8842_read16(adapter, 2,
REG_MARL);
388 mac = ks8842_read16(adapter, 2,
REG_MARM);
390 mac = ks8842_read16(adapter, 2,
REG_MARH);
402 ks8842_write8(adapter, 2, mac[ETH_ALEN - i - 1],
REG_MARL + i);
404 ks8842_write8(adapter, 39, mac[ETH_ALEN - i - 1],
416 mac = ks8842_read16(adapter, 2,
REG_MARL);
418 mac = ks8842_read16(adapter, 2,
REG_MARM);
420 mac = ks8842_read16(adapter, 2,
REG_MARH);
423 spin_unlock_irqrestore(&adapter->
lock, flags);
428 return ks8842_read16(adapter, 16,
REG_TXMIR) & 0x1fff;
438 netdev_dbg(netdev,
"%s: TX ongoing\n", __func__);
449 *buf++ = skb->
len & 0xff;
450 *buf++ = (skb->
len >> 8) & 0xff;
451 skb_copy_from_linear_data(skb, buf, skb->
len);
461 ctl->
adesc = dmaengine_prep_slave_sg(ctl->
chan,
467 ctl->
adesc->callback_param = netdev;
468 ctl->
adesc->callback = ks8842_dma_tx_cb;
483 netdev_dbg(netdev,
"%s: len %u head %p data %p tail %p end %p\n",
485 skb_tail_pointer(skb), skb_end_pointer(skb));
488 if (ks8842_tx_fifo_space(adapter) < len + 8)
495 netdev->
stats.tx_bytes += len;
508 ctrl = 0x8000 | 0x100 | (len << 16);
511 netdev->
stats.tx_bytes += len;
522 ks8842_write16(adapter, 17, 1,
REG_TXQCR);
531 netdev_dbg(netdev,
"RX error, status: %x\n", status);
533 netdev->
stats.rx_errors++;
535 netdev->
stats.rx_length_errors++;
537 netdev->
stats.rx_crc_errors++;
539 netdev->
stats.rx_frame_errors++;
542 static void ks8842_update_rx_counters(
struct net_device *netdev,
u32 status,
545 netdev_dbg(netdev,
"RX packet, len: %d\n", len);
547 netdev->
stats.rx_packets++;
548 netdev->
stats.rx_bytes += len;
550 netdev->
stats.multicast++;
553 static int __ks8842_start_new_rx_dma(
struct net_device *netdev)
573 ctl->
adesc = dmaengine_prep_slave_sg(ctl->
chan,
580 ctl->
adesc->callback_param = netdev;
581 ctl->
adesc->callback = ks8842_dma_rx_cb;
596 dev_kfree_skb(ctl->
skb);
604 static void ks8842_rx_frame_dma_tasklet(
unsigned long arg)
616 __ks8842_start_new_rx_dma(netdev);
623 netdev_dbg(netdev,
"%s - rx_data: status: %x\n",
624 __func__, status & 0xffff);
628 int len = (status >> 16) & 0x7ff;
630 ks8842_update_rx_counters(netdev, status, len);
639 ks8842_update_rx_err_counters(netdev, status);
644 static void ks8842_rx_frame(
struct net_device *netdev,
653 netdev_dbg(netdev,
"%s - rx_data: status: %x\n",
657 len = (status >> 16) & 0x7ff;
659 netdev_dbg(netdev,
"%s - rx_data: status: %x\n",
664 if ((status & RXSR_VALID) && !(status &
RXSR_ERROR)) {
665 struct sk_buff *skb = netdev_alloc_skb_ip_align(netdev, len + 3);
669 ks8842_update_rx_counters(netdev, status, len);
673 ks8842_select_bank(adapter, 17);
684 ks8842_select_bank(adapter, 17);
694 netdev->
stats.rx_dropped++;
696 ks8842_update_rx_err_counters(netdev, status);
699 ks8842_clear_bits(adapter, 0, 1 << 12,
REG_QRFCR);
702 ks8842_write16(adapter, 17, 0x01,
REG_RXQCR);
705 ks8842_enable_bits(adapter, 0, 1 << 12,
REG_QRFCR);
711 netdev_dbg(netdev,
"%s Entry - rx_data: %d\n", __func__, rx_data);
713 ks8842_rx_frame(netdev, adapter);
714 rx_data = ks8842_read16(adapter, 16,
REG_RXMIR) & 0x1fff;
721 netdev_dbg(netdev,
"%s - entry, sr: %x\n", __func__, sr);
722 netdev->
stats.tx_packets++;
723 if (netif_queue_stopped(netdev))
724 netif_wake_queue(netdev);
731 netdev->
stats.rx_errors++;
732 netdev->
stats.rx_fifo_errors++;
746 spin_unlock_irqrestore(&adapter->
lock, flags);
748 isr = ks8842_read16(adapter, 18,
REG_ISR);
749 netdev_dbg(netdev,
"%s - ISR: 0x%x\n", __func__, isr);
758 ks8842_write16(adapter, 18, isr,
REG_ISR);
764 if (!netif_running(netdev))
768 ks8842_update_link_status(netdev, adapter);
782 ks8842_disable_tx(adapter);
783 ks8842_enable_tx(adapter);
787 ks8842_disable_rx(adapter);
788 ks8842_enable_rx(adapter);
802 ks8842_resume_dma(adapter);
804 spin_unlock_irqrestore(&adapter->
lock, flags);
815 isr = ks8842_read16(adapter, 18,
REG_ISR);
816 netdev_dbg(netdev,
"%s - ISR: 0x%x\n", __func__, isr);
824 ks8842_write16(adapter, 18, 0x00,
REG_IER);
827 tasklet_schedule(&adapter->
tasklet);
837 ks8842_resume_dma(adapter);
842 static void ks8842_dma_rx_cb(
void *
data)
849 if (adapter->
dma_rx.adesc)
850 tasklet_schedule(&adapter->
dma_rx.tasklet);
853 static void ks8842_dma_tx_cb(
void *data)
864 netdev->
stats.tx_packets++;
867 if (netif_queue_stopped(netdev))
868 netif_wake_queue(netdev);
878 tx_ctl->
chan->device->device_control(tx_ctl->
chan,
883 rx_ctl->
chan->device->device_control(rx_ctl->
chan,
891 dev_kfree_skb(rx_ctl->
skb);
895 static void ks8842_dealloc_dma_bufs(
struct ks8842_adapter *adapter)
900 ks8842_stop_dma(adapter);
921 static bool ks8842_dma_filter_fn(
struct dma_chan *
chan,
void *filter_param)
926 static int ks8842_alloc_dma_bufs(
struct net_device *netdev)
942 (
void *)(
long)tx_ctl->
channel);
965 (
void *)(
long)rx_ctl->
channel);
972 (
unsigned long)netdev);
976 ks8842_dealloc_dma_bufs(adapter);
982 static int ks8842_open(
struct net_device *netdev)
990 err = ks8842_alloc_dma_bufs(netdev);
994 err = __ks8842_start_new_rx_dma(netdev);
996 ks8842_dealloc_dma_bufs(adapter);
1001 ": Failed to initiate DMA, running PIO\n");
1002 ks8842_dealloc_dma_bufs(adapter);
1003 adapter->
dma_rx.channel = -1;
1004 adapter->
dma_tx.channel = -1;
1009 ks8842_reset_hw(adapter);
1011 ks8842_write_mac_addr(adapter, netdev->
dev_addr);
1013 ks8842_update_link_status(netdev, adapter);
1018 pr_err(
"Failed to request IRQ: %d: %d\n", adapter->
irq, err);
1025 static int ks8842_close(
struct net_device *netdev)
1029 netdev_dbg(netdev,
"%s - entry\n", __func__);
1034 ks8842_dealloc_dma_bufs(adapter);
1054 unsigned long flags;
1055 ret = ks8842_tx_frame_dma(skb, netdev);
1058 if (adapter->
dma_tx.adesc)
1059 netif_stop_queue(netdev);
1060 spin_unlock_irqrestore(&adapter->
lock, flags);
1064 ret = ks8842_tx_frame(skb, netdev);
1066 if (ks8842_tx_fifo_space(adapter) < netdev->
mtu + 8)
1067 netif_stop_queue(netdev);
1072 static int ks8842_set_mac(
struct net_device *netdev,
void *
p)
1080 if (!is_valid_ether_addr(addr->
sa_data))
1086 ks8842_write_mac_addr(adapter, mac);
1095 unsigned long flags;
1102 ks8842_stop_dma(adapter);
1105 ks8842_write16(adapter, 18, 0,
REG_IER);
1106 ks8842_write16(adapter, 18, 0xFFFF,
REG_ISR);
1108 netif_stop_queue(netdev);
1110 spin_unlock_irqrestore(&adapter->
lock, flags);
1112 ks8842_reset_hw(adapter);
1114 ks8842_write_mac_addr(adapter, netdev->
dev_addr);
1116 ks8842_update_link_status(netdev, adapter);
1119 __ks8842_start_new_rx_dma(netdev);
1122 static void ks8842_tx_timeout(
struct net_device *netdev)
1132 .ndo_open = ks8842_open,
1133 .ndo_stop = ks8842_close,
1134 .ndo_start_xmit = ks8842_xmit_frame,
1135 .ndo_set_mac_address = ks8842_set_mac,
1136 .ndo_tx_timeout = ks8842_tx_timeout,
1140 static const struct ethtool_ops ks8842_ethtool_ops = {
1156 goto err_mem_region;
1160 goto err_alloc_etherdev;
1164 adapter = netdev_priv(netdev);
1165 adapter->
netdev = netdev;
1174 if (adapter->
irq < 0) {
1179 adapter->
dev = (pdev->
dev.parent) ? pdev->
dev.parent : &pdev->
dev;
1188 adapter->
dma_rx.channel = -1;
1189 adapter->
dma_tx.channel = -1;
1201 for (i = 0; i < netdev->
addr_len; i++)
1205 if (i < netdev->addr_len)
1212 ks8842_read_mac_addr(adapter, netdev->
dev_addr);
1214 if (!is_valid_ether_addr(netdev->
dev_addr))
1215 eth_hw_addr_random(netdev);
1225 platform_set_drvdata(pdev, netdev);
1227 pr_info(
"Found chip, family: 0x%x, id: 0x%x, rev: 0x%x\n",
1228 (
id >> 8) & 0xff, (
id >> 4) & 0xf, (
id >> 1) & 0x7);
1245 struct net_device *netdev = platform_get_drvdata(pdev);
1254 platform_set_drvdata(pdev,
NULL);
1264 .probe = ks8842_probe,