1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Faraday FTMAC100 10/100 Ethernet
5 * (C) Copyright 2009-2011 Faraday Technology
6 * Po-Yu Chuang <ratbert@faraday-tech.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/dma-mapping.h>
12 #include <linux/etherdevice.h>
13 #include <linux/ethtool.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
17 #include <linux/mii.h>
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/netdevice.h>
21 #include <linux/platform_device.h>
25 #define DRV_NAME "ftmac100"
26 #define DRV_VERSION "0.2"
28 #define RX_QUEUE_ENTRIES 128 /* must be power of 2 */
29 #define TX_QUEUE_ENTRIES 16 /* must be power of 2 */
31 #define MAX_PKT_SIZE 1518
32 #define RX_BUF_SIZE 2044 /* must be smaller than 0x7ff */
34 #if MAX_PKT_SIZE > 0x7ff
35 #error invalid MAX_PKT_SIZE
38 #if RX_BUF_SIZE > 0x7ff || RX_BUF_SIZE > PAGE_SIZE
39 #error invalid RX_BUF_SIZE
42 /******************************************************************************
44 *****************************************************************************/
45 struct ftmac100_descs
{
46 struct ftmac100_rxdes rxdes
[RX_QUEUE_ENTRIES
];
47 struct ftmac100_txdes txdes
[TX_QUEUE_ENTRIES
];
55 struct ftmac100_descs
*descs
;
56 dma_addr_t descs_dma_addr
;
58 unsigned int rx_pointer
;
59 unsigned int tx_clean_pointer
;
60 unsigned int tx_pointer
;
61 unsigned int tx_pending
;
65 struct net_device
*netdev
;
67 struct napi_struct napi
;
69 struct mii_if_info mii
;
72 static int ftmac100_alloc_rx_page(struct ftmac100
*priv
,
73 struct ftmac100_rxdes
*rxdes
, gfp_t gfp
);
75 /******************************************************************************
76 * internal functions (hardware register access)
77 *****************************************************************************/
78 #define INT_MASK_ALL_ENABLED (FTMAC100_INT_RPKT_FINISH | \
79 FTMAC100_INT_NORXBUF | \
80 FTMAC100_INT_XPKT_OK | \
81 FTMAC100_INT_XPKT_LOST | \
82 FTMAC100_INT_RPKT_LOST | \
83 FTMAC100_INT_AHB_ERR | \
84 FTMAC100_INT_PHYSTS_CHG)
86 #define INT_MASK_ALL_DISABLED 0
88 static void ftmac100_enable_all_int(struct ftmac100
*priv
)
90 iowrite32(INT_MASK_ALL_ENABLED
, priv
->base
+ FTMAC100_OFFSET_IMR
);
93 static void ftmac100_disable_all_int(struct ftmac100
*priv
)
95 iowrite32(INT_MASK_ALL_DISABLED
, priv
->base
+ FTMAC100_OFFSET_IMR
);
98 static void ftmac100_set_rx_ring_base(struct ftmac100
*priv
, dma_addr_t addr
)
100 iowrite32(addr
, priv
->base
+ FTMAC100_OFFSET_RXR_BADR
);
103 static void ftmac100_set_tx_ring_base(struct ftmac100
*priv
, dma_addr_t addr
)
105 iowrite32(addr
, priv
->base
+ FTMAC100_OFFSET_TXR_BADR
);
108 static void ftmac100_txdma_start_polling(struct ftmac100
*priv
)
110 iowrite32(1, priv
->base
+ FTMAC100_OFFSET_TXPD
);
113 static int ftmac100_reset(struct ftmac100
*priv
)
115 struct net_device
*netdev
= priv
->netdev
;
118 /* NOTE: reset clears all registers */
119 iowrite32(FTMAC100_MACCR_SW_RST
, priv
->base
+ FTMAC100_OFFSET_MACCR
);
121 for (i
= 0; i
< 5; i
++) {
124 maccr
= ioread32(priv
->base
+ FTMAC100_OFFSET_MACCR
);
125 if (!(maccr
& FTMAC100_MACCR_SW_RST
)) {
127 * FTMAC100_MACCR_SW_RST cleared does not indicate
128 * that hardware reset completed (what the f*ck).
129 * We still need to wait for a while.
138 netdev_err(netdev
, "software reset failed\n");
142 static void ftmac100_set_mac(struct ftmac100
*priv
, const unsigned char *mac
)
144 unsigned int maddr
= mac
[0] << 8 | mac
[1];
145 unsigned int laddr
= mac
[2] << 24 | mac
[3] << 16 | mac
[4] << 8 | mac
[5];
147 iowrite32(maddr
, priv
->base
+ FTMAC100_OFFSET_MAC_MADR
);
148 iowrite32(laddr
, priv
->base
+ FTMAC100_OFFSET_MAC_LADR
);
151 #define MACCR_ENABLE_ALL (FTMAC100_MACCR_XMT_EN | \
152 FTMAC100_MACCR_RCV_EN | \
153 FTMAC100_MACCR_XDMA_EN | \
154 FTMAC100_MACCR_RDMA_EN | \
155 FTMAC100_MACCR_CRC_APD | \
156 FTMAC100_MACCR_FULLDUP | \
157 FTMAC100_MACCR_RX_RUNT | \
158 FTMAC100_MACCR_RX_BROADPKT)
160 static int ftmac100_start_hw(struct ftmac100
*priv
)
162 struct net_device
*netdev
= priv
->netdev
;
164 if (ftmac100_reset(priv
))
167 /* setup ring buffer base registers */
168 ftmac100_set_rx_ring_base(priv
,
169 priv
->descs_dma_addr
+
170 offsetof(struct ftmac100_descs
, rxdes
));
171 ftmac100_set_tx_ring_base(priv
,
172 priv
->descs_dma_addr
+
173 offsetof(struct ftmac100_descs
, txdes
));
175 iowrite32(FTMAC100_APTC_RXPOLL_CNT(1), priv
->base
+ FTMAC100_OFFSET_APTC
);
177 ftmac100_set_mac(priv
, netdev
->dev_addr
);
179 iowrite32(MACCR_ENABLE_ALL
, priv
->base
+ FTMAC100_OFFSET_MACCR
);
183 static void ftmac100_stop_hw(struct ftmac100
*priv
)
185 iowrite32(0, priv
->base
+ FTMAC100_OFFSET_MACCR
);
188 /******************************************************************************
189 * internal functions (receive descriptor)
190 *****************************************************************************/
191 static bool ftmac100_rxdes_first_segment(struct ftmac100_rxdes
*rxdes
)
193 return rxdes
->rxdes0
& cpu_to_le32(FTMAC100_RXDES0_FRS
);
196 static bool ftmac100_rxdes_last_segment(struct ftmac100_rxdes
*rxdes
)
198 return rxdes
->rxdes0
& cpu_to_le32(FTMAC100_RXDES0_LRS
);
201 static bool ftmac100_rxdes_owned_by_dma(struct ftmac100_rxdes
*rxdes
)
203 return rxdes
->rxdes0
& cpu_to_le32(FTMAC100_RXDES0_RXDMA_OWN
);
206 static void ftmac100_rxdes_set_dma_own(struct ftmac100_rxdes
*rxdes
)
208 /* clear status bits */
209 rxdes
->rxdes0
= cpu_to_le32(FTMAC100_RXDES0_RXDMA_OWN
);
212 static bool ftmac100_rxdes_rx_error(struct ftmac100_rxdes
*rxdes
)
214 return rxdes
->rxdes0
& cpu_to_le32(FTMAC100_RXDES0_RX_ERR
);
217 static bool ftmac100_rxdes_crc_error(struct ftmac100_rxdes
*rxdes
)
219 return rxdes
->rxdes0
& cpu_to_le32(FTMAC100_RXDES0_CRC_ERR
);
222 static bool ftmac100_rxdes_frame_too_long(struct ftmac100_rxdes
*rxdes
)
224 return rxdes
->rxdes0
& cpu_to_le32(FTMAC100_RXDES0_FTL
);
227 static bool ftmac100_rxdes_runt(struct ftmac100_rxdes
*rxdes
)
229 return rxdes
->rxdes0
& cpu_to_le32(FTMAC100_RXDES0_RUNT
);
232 static bool ftmac100_rxdes_odd_nibble(struct ftmac100_rxdes
*rxdes
)
234 return rxdes
->rxdes0
& cpu_to_le32(FTMAC100_RXDES0_RX_ODD_NB
);
237 static unsigned int ftmac100_rxdes_frame_length(struct ftmac100_rxdes
*rxdes
)
239 return le32_to_cpu(rxdes
->rxdes0
) & FTMAC100_RXDES0_RFL
;
242 static bool ftmac100_rxdes_multicast(struct ftmac100_rxdes
*rxdes
)
244 return rxdes
->rxdes0
& cpu_to_le32(FTMAC100_RXDES0_MULTICAST
);
247 static void ftmac100_rxdes_set_buffer_size(struct ftmac100_rxdes
*rxdes
,
250 rxdes
->rxdes1
&= cpu_to_le32(FTMAC100_RXDES1_EDORR
);
251 rxdes
->rxdes1
|= cpu_to_le32(FTMAC100_RXDES1_RXBUF_SIZE(size
));
254 static void ftmac100_rxdes_set_end_of_ring(struct ftmac100_rxdes
*rxdes
)
256 rxdes
->rxdes1
|= cpu_to_le32(FTMAC100_RXDES1_EDORR
);
259 static void ftmac100_rxdes_set_dma_addr(struct ftmac100_rxdes
*rxdes
,
262 rxdes
->rxdes2
= cpu_to_le32(addr
);
265 static dma_addr_t
ftmac100_rxdes_get_dma_addr(struct ftmac100_rxdes
*rxdes
)
267 return le32_to_cpu(rxdes
->rxdes2
);
271 * rxdes3 is not used by hardware. We use it to keep track of page.
272 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
274 static void ftmac100_rxdes_set_page(struct ftmac100_rxdes
*rxdes
, struct page
*page
)
276 rxdes
->rxdes3
= (unsigned int)page
;
279 static struct page
*ftmac100_rxdes_get_page(struct ftmac100_rxdes
*rxdes
)
281 return (struct page
*)rxdes
->rxdes3
;
284 /******************************************************************************
285 * internal functions (receive)
286 *****************************************************************************/
287 static int ftmac100_next_rx_pointer(int pointer
)
289 return (pointer
+ 1) & (RX_QUEUE_ENTRIES
- 1);
292 static void ftmac100_rx_pointer_advance(struct ftmac100
*priv
)
294 priv
->rx_pointer
= ftmac100_next_rx_pointer(priv
->rx_pointer
);
297 static struct ftmac100_rxdes
*ftmac100_current_rxdes(struct ftmac100
*priv
)
299 return &priv
->descs
->rxdes
[priv
->rx_pointer
];
302 static struct ftmac100_rxdes
*
303 ftmac100_rx_locate_first_segment(struct ftmac100
*priv
)
305 struct ftmac100_rxdes
*rxdes
= ftmac100_current_rxdes(priv
);
307 while (!ftmac100_rxdes_owned_by_dma(rxdes
)) {
308 if (ftmac100_rxdes_first_segment(rxdes
))
311 ftmac100_rxdes_set_dma_own(rxdes
);
312 ftmac100_rx_pointer_advance(priv
);
313 rxdes
= ftmac100_current_rxdes(priv
);
319 static bool ftmac100_rx_packet_error(struct ftmac100
*priv
,
320 struct ftmac100_rxdes
*rxdes
)
322 struct net_device
*netdev
= priv
->netdev
;
325 if (unlikely(ftmac100_rxdes_rx_error(rxdes
))) {
327 netdev_info(netdev
, "rx err\n");
329 netdev
->stats
.rx_errors
++;
333 if (unlikely(ftmac100_rxdes_crc_error(rxdes
))) {
335 netdev_info(netdev
, "rx crc err\n");
337 netdev
->stats
.rx_crc_errors
++;
341 if (unlikely(ftmac100_rxdes_frame_too_long(rxdes
))) {
343 netdev_info(netdev
, "rx frame too long\n");
345 netdev
->stats
.rx_length_errors
++;
347 } else if (unlikely(ftmac100_rxdes_runt(rxdes
))) {
349 netdev_info(netdev
, "rx runt\n");
351 netdev
->stats
.rx_length_errors
++;
353 } else if (unlikely(ftmac100_rxdes_odd_nibble(rxdes
))) {
355 netdev_info(netdev
, "rx odd nibble\n");
357 netdev
->stats
.rx_length_errors
++;
364 static void ftmac100_rx_drop_packet(struct ftmac100
*priv
)
366 struct net_device
*netdev
= priv
->netdev
;
367 struct ftmac100_rxdes
*rxdes
= ftmac100_current_rxdes(priv
);
371 netdev_dbg(netdev
, "drop packet %p\n", rxdes
);
374 if (ftmac100_rxdes_last_segment(rxdes
))
377 ftmac100_rxdes_set_dma_own(rxdes
);
378 ftmac100_rx_pointer_advance(priv
);
379 rxdes
= ftmac100_current_rxdes(priv
);
380 } while (!done
&& !ftmac100_rxdes_owned_by_dma(rxdes
));
382 netdev
->stats
.rx_dropped
++;
385 static bool ftmac100_rx_packet(struct ftmac100
*priv
, int *processed
)
387 struct net_device
*netdev
= priv
->netdev
;
388 struct ftmac100_rxdes
*rxdes
;
395 rxdes
= ftmac100_rx_locate_first_segment(priv
);
399 if (unlikely(ftmac100_rx_packet_error(priv
, rxdes
))) {
400 ftmac100_rx_drop_packet(priv
);
405 * It is impossible to get multi-segment packets
406 * because we always provide big enough receive buffers.
408 ret
= ftmac100_rxdes_last_segment(rxdes
);
411 /* start processing */
412 skb
= netdev_alloc_skb_ip_align(netdev
, 128);
413 if (unlikely(!skb
)) {
415 netdev_err(netdev
, "rx skb alloc failed\n");
417 ftmac100_rx_drop_packet(priv
);
421 if (unlikely(ftmac100_rxdes_multicast(rxdes
)))
422 netdev
->stats
.multicast
++;
424 map
= ftmac100_rxdes_get_dma_addr(rxdes
);
425 dma_unmap_page(priv
->dev
, map
, RX_BUF_SIZE
, DMA_FROM_DEVICE
);
427 length
= ftmac100_rxdes_frame_length(rxdes
);
428 page
= ftmac100_rxdes_get_page(rxdes
);
429 skb_fill_page_desc(skb
, 0, page
, 0, length
);
431 skb
->data_len
+= length
;
434 skb
->truesize
+= PAGE_SIZE
;
435 /* We pull the minimum amount into linear part */
436 __pskb_pull_tail(skb
, ETH_HLEN
);
438 /* Small frames are copied into linear part to free one page */
439 __pskb_pull_tail(skb
, length
);
441 ftmac100_alloc_rx_page(priv
, rxdes
, GFP_ATOMIC
);
443 ftmac100_rx_pointer_advance(priv
);
445 skb
->protocol
= eth_type_trans(skb
, netdev
);
447 netdev
->stats
.rx_packets
++;
448 netdev
->stats
.rx_bytes
+= skb
->len
;
450 /* push packet to protocol stack */
451 netif_receive_skb(skb
);
457 /******************************************************************************
458 * internal functions (transmit descriptor)
459 *****************************************************************************/
460 static void ftmac100_txdes_reset(struct ftmac100_txdes
*txdes
)
462 /* clear all except end of ring bit */
464 txdes
->txdes1
&= cpu_to_le32(FTMAC100_TXDES1_EDOTR
);
469 static bool ftmac100_txdes_owned_by_dma(struct ftmac100_txdes
*txdes
)
471 return txdes
->txdes0
& cpu_to_le32(FTMAC100_TXDES0_TXDMA_OWN
);
474 static void ftmac100_txdes_set_dma_own(struct ftmac100_txdes
*txdes
)
477 * Make sure dma own bit will not be set before any other
481 txdes
->txdes0
|= cpu_to_le32(FTMAC100_TXDES0_TXDMA_OWN
);
484 static bool ftmac100_txdes_excessive_collision(struct ftmac100_txdes
*txdes
)
486 return txdes
->txdes0
& cpu_to_le32(FTMAC100_TXDES0_TXPKT_EXSCOL
);
489 static bool ftmac100_txdes_late_collision(struct ftmac100_txdes
*txdes
)
491 return txdes
->txdes0
& cpu_to_le32(FTMAC100_TXDES0_TXPKT_LATECOL
);
494 static void ftmac100_txdes_set_end_of_ring(struct ftmac100_txdes
*txdes
)
496 txdes
->txdes1
|= cpu_to_le32(FTMAC100_TXDES1_EDOTR
);
499 static void ftmac100_txdes_set_first_segment(struct ftmac100_txdes
*txdes
)
501 txdes
->txdes1
|= cpu_to_le32(FTMAC100_TXDES1_FTS
);
504 static void ftmac100_txdes_set_last_segment(struct ftmac100_txdes
*txdes
)
506 txdes
->txdes1
|= cpu_to_le32(FTMAC100_TXDES1_LTS
);
509 static void ftmac100_txdes_set_txint(struct ftmac100_txdes
*txdes
)
511 txdes
->txdes1
|= cpu_to_le32(FTMAC100_TXDES1_TXIC
);
514 static void ftmac100_txdes_set_buffer_size(struct ftmac100_txdes
*txdes
,
517 txdes
->txdes1
|= cpu_to_le32(FTMAC100_TXDES1_TXBUF_SIZE(len
));
520 static void ftmac100_txdes_set_dma_addr(struct ftmac100_txdes
*txdes
,
523 txdes
->txdes2
= cpu_to_le32(addr
);
526 static dma_addr_t
ftmac100_txdes_get_dma_addr(struct ftmac100_txdes
*txdes
)
528 return le32_to_cpu(txdes
->txdes2
);
532 * txdes3 is not used by hardware. We use it to keep track of socket buffer.
533 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
535 static void ftmac100_txdes_set_skb(struct ftmac100_txdes
*txdes
, struct sk_buff
*skb
)
537 txdes
->txdes3
= (unsigned int)skb
;
540 static struct sk_buff
*ftmac100_txdes_get_skb(struct ftmac100_txdes
*txdes
)
542 return (struct sk_buff
*)txdes
->txdes3
;
545 /******************************************************************************
546 * internal functions (transmit)
547 *****************************************************************************/
548 static int ftmac100_next_tx_pointer(int pointer
)
550 return (pointer
+ 1) & (TX_QUEUE_ENTRIES
- 1);
553 static void ftmac100_tx_pointer_advance(struct ftmac100
*priv
)
555 priv
->tx_pointer
= ftmac100_next_tx_pointer(priv
->tx_pointer
);
558 static void ftmac100_tx_clean_pointer_advance(struct ftmac100
*priv
)
560 priv
->tx_clean_pointer
= ftmac100_next_tx_pointer(priv
->tx_clean_pointer
);
563 static struct ftmac100_txdes
*ftmac100_current_txdes(struct ftmac100
*priv
)
565 return &priv
->descs
->txdes
[priv
->tx_pointer
];
568 static struct ftmac100_txdes
*ftmac100_current_clean_txdes(struct ftmac100
*priv
)
570 return &priv
->descs
->txdes
[priv
->tx_clean_pointer
];
573 static bool ftmac100_tx_complete_packet(struct ftmac100
*priv
)
575 struct net_device
*netdev
= priv
->netdev
;
576 struct ftmac100_txdes
*txdes
;
580 if (priv
->tx_pending
== 0)
583 txdes
= ftmac100_current_clean_txdes(priv
);
585 if (ftmac100_txdes_owned_by_dma(txdes
))
588 skb
= ftmac100_txdes_get_skb(txdes
);
589 map
= ftmac100_txdes_get_dma_addr(txdes
);
591 if (unlikely(ftmac100_txdes_excessive_collision(txdes
) ||
592 ftmac100_txdes_late_collision(txdes
))) {
594 * packet transmitted to ethernet lost due to late collision
595 * or excessive collision
597 netdev
->stats
.tx_aborted_errors
++;
599 netdev
->stats
.tx_packets
++;
600 netdev
->stats
.tx_bytes
+= skb
->len
;
603 dma_unmap_single(priv
->dev
, map
, skb_headlen(skb
), DMA_TO_DEVICE
);
606 ftmac100_txdes_reset(txdes
);
608 ftmac100_tx_clean_pointer_advance(priv
);
610 spin_lock(&priv
->tx_lock
);
612 spin_unlock(&priv
->tx_lock
);
613 netif_wake_queue(netdev
);
618 static void ftmac100_tx_complete(struct ftmac100
*priv
)
620 while (ftmac100_tx_complete_packet(priv
))
624 static netdev_tx_t
ftmac100_xmit(struct ftmac100
*priv
, struct sk_buff
*skb
,
627 struct net_device
*netdev
= priv
->netdev
;
628 struct ftmac100_txdes
*txdes
;
629 unsigned int len
= (skb
->len
< ETH_ZLEN
) ? ETH_ZLEN
: skb
->len
;
631 txdes
= ftmac100_current_txdes(priv
);
632 ftmac100_tx_pointer_advance(priv
);
634 /* setup TX descriptor */
635 ftmac100_txdes_set_skb(txdes
, skb
);
636 ftmac100_txdes_set_dma_addr(txdes
, map
);
638 ftmac100_txdes_set_first_segment(txdes
);
639 ftmac100_txdes_set_last_segment(txdes
);
640 ftmac100_txdes_set_txint(txdes
);
641 ftmac100_txdes_set_buffer_size(txdes
, len
);
643 spin_lock(&priv
->tx_lock
);
645 if (priv
->tx_pending
== TX_QUEUE_ENTRIES
)
646 netif_stop_queue(netdev
);
649 ftmac100_txdes_set_dma_own(txdes
);
650 spin_unlock(&priv
->tx_lock
);
652 ftmac100_txdma_start_polling(priv
);
656 /******************************************************************************
657 * internal functions (buffer)
658 *****************************************************************************/
659 static int ftmac100_alloc_rx_page(struct ftmac100
*priv
,
660 struct ftmac100_rxdes
*rxdes
, gfp_t gfp
)
662 struct net_device
*netdev
= priv
->netdev
;
666 page
= alloc_page(gfp
);
669 netdev_err(netdev
, "failed to allocate rx page\n");
673 map
= dma_map_page(priv
->dev
, page
, 0, RX_BUF_SIZE
, DMA_FROM_DEVICE
);
674 if (unlikely(dma_mapping_error(priv
->dev
, map
))) {
676 netdev_err(netdev
, "failed to map rx page\n");
681 ftmac100_rxdes_set_page(rxdes
, page
);
682 ftmac100_rxdes_set_dma_addr(rxdes
, map
);
683 ftmac100_rxdes_set_buffer_size(rxdes
, RX_BUF_SIZE
);
684 ftmac100_rxdes_set_dma_own(rxdes
);
688 static void ftmac100_free_buffers(struct ftmac100
*priv
)
692 for (i
= 0; i
< RX_QUEUE_ENTRIES
; i
++) {
693 struct ftmac100_rxdes
*rxdes
= &priv
->descs
->rxdes
[i
];
694 struct page
*page
= ftmac100_rxdes_get_page(rxdes
);
695 dma_addr_t map
= ftmac100_rxdes_get_dma_addr(rxdes
);
700 dma_unmap_page(priv
->dev
, map
, RX_BUF_SIZE
, DMA_FROM_DEVICE
);
704 for (i
= 0; i
< TX_QUEUE_ENTRIES
; i
++) {
705 struct ftmac100_txdes
*txdes
= &priv
->descs
->txdes
[i
];
706 struct sk_buff
*skb
= ftmac100_txdes_get_skb(txdes
);
707 dma_addr_t map
= ftmac100_txdes_get_dma_addr(txdes
);
712 dma_unmap_single(priv
->dev
, map
, skb_headlen(skb
), DMA_TO_DEVICE
);
716 dma_free_coherent(priv
->dev
, sizeof(struct ftmac100_descs
),
717 priv
->descs
, priv
->descs_dma_addr
);
720 static int ftmac100_alloc_buffers(struct ftmac100
*priv
)
724 priv
->descs
= dma_alloc_coherent(priv
->dev
,
725 sizeof(struct ftmac100_descs
),
726 &priv
->descs_dma_addr
, GFP_KERNEL
);
730 /* initialize RX ring */
731 ftmac100_rxdes_set_end_of_ring(&priv
->descs
->rxdes
[RX_QUEUE_ENTRIES
- 1]);
733 for (i
= 0; i
< RX_QUEUE_ENTRIES
; i
++) {
734 struct ftmac100_rxdes
*rxdes
= &priv
->descs
->rxdes
[i
];
736 if (ftmac100_alloc_rx_page(priv
, rxdes
, GFP_KERNEL
))
740 /* initialize TX ring */
741 ftmac100_txdes_set_end_of_ring(&priv
->descs
->txdes
[TX_QUEUE_ENTRIES
- 1]);
745 ftmac100_free_buffers(priv
);
749 /******************************************************************************
750 * struct mii_if_info functions
751 *****************************************************************************/
752 static int ftmac100_mdio_read(struct net_device
*netdev
, int phy_id
, int reg
)
754 struct ftmac100
*priv
= netdev_priv(netdev
);
758 phycr
= FTMAC100_PHYCR_PHYAD(phy_id
) |
759 FTMAC100_PHYCR_REGAD(reg
) |
760 FTMAC100_PHYCR_MIIRD
;
762 iowrite32(phycr
, priv
->base
+ FTMAC100_OFFSET_PHYCR
);
764 for (i
= 0; i
< 10; i
++) {
765 phycr
= ioread32(priv
->base
+ FTMAC100_OFFSET_PHYCR
);
767 if ((phycr
& FTMAC100_PHYCR_MIIRD
) == 0)
768 return phycr
& FTMAC100_PHYCR_MIIRDATA
;
773 netdev_err(netdev
, "mdio read timed out\n");
777 static void ftmac100_mdio_write(struct net_device
*netdev
, int phy_id
, int reg
,
780 struct ftmac100
*priv
= netdev_priv(netdev
);
784 phycr
= FTMAC100_PHYCR_PHYAD(phy_id
) |
785 FTMAC100_PHYCR_REGAD(reg
) |
786 FTMAC100_PHYCR_MIIWR
;
788 data
= FTMAC100_PHYWDATA_MIIWDATA(data
);
790 iowrite32(data
, priv
->base
+ FTMAC100_OFFSET_PHYWDATA
);
791 iowrite32(phycr
, priv
->base
+ FTMAC100_OFFSET_PHYCR
);
793 for (i
= 0; i
< 10; i
++) {
794 phycr
= ioread32(priv
->base
+ FTMAC100_OFFSET_PHYCR
);
796 if ((phycr
& FTMAC100_PHYCR_MIIWR
) == 0)
802 netdev_err(netdev
, "mdio write timed out\n");
805 /******************************************************************************
806 * struct ethtool_ops functions
807 *****************************************************************************/
808 static void ftmac100_get_drvinfo(struct net_device
*netdev
,
809 struct ethtool_drvinfo
*info
)
811 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
812 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
813 strlcpy(info
->bus_info
, dev_name(&netdev
->dev
), sizeof(info
->bus_info
));
816 static int ftmac100_get_link_ksettings(struct net_device
*netdev
,
817 struct ethtool_link_ksettings
*cmd
)
819 struct ftmac100
*priv
= netdev_priv(netdev
);
821 mii_ethtool_get_link_ksettings(&priv
->mii
, cmd
);
826 static int ftmac100_set_link_ksettings(struct net_device
*netdev
,
827 const struct ethtool_link_ksettings
*cmd
)
829 struct ftmac100
*priv
= netdev_priv(netdev
);
830 return mii_ethtool_set_link_ksettings(&priv
->mii
, cmd
);
833 static int ftmac100_nway_reset(struct net_device
*netdev
)
835 struct ftmac100
*priv
= netdev_priv(netdev
);
836 return mii_nway_restart(&priv
->mii
);
839 static u32
ftmac100_get_link(struct net_device
*netdev
)
841 struct ftmac100
*priv
= netdev_priv(netdev
);
842 return mii_link_ok(&priv
->mii
);
845 static const struct ethtool_ops ftmac100_ethtool_ops
= {
846 .get_drvinfo
= ftmac100_get_drvinfo
,
847 .nway_reset
= ftmac100_nway_reset
,
848 .get_link
= ftmac100_get_link
,
849 .get_link_ksettings
= ftmac100_get_link_ksettings
,
850 .set_link_ksettings
= ftmac100_set_link_ksettings
,
853 /******************************************************************************
855 *****************************************************************************/
856 static irqreturn_t
ftmac100_interrupt(int irq
, void *dev_id
)
858 struct net_device
*netdev
= dev_id
;
859 struct ftmac100
*priv
= netdev_priv(netdev
);
861 /* Disable interrupts for polling */
862 ftmac100_disable_all_int(priv
);
863 if (likely(netif_running(netdev
)))
864 napi_schedule(&priv
->napi
);
869 /******************************************************************************
870 * struct napi_struct functions
871 *****************************************************************************/
872 static int ftmac100_poll(struct napi_struct
*napi
, int budget
)
874 struct ftmac100
*priv
= container_of(napi
, struct ftmac100
, napi
);
875 struct net_device
*netdev
= priv
->netdev
;
877 bool completed
= true;
880 status
= ioread32(priv
->base
+ FTMAC100_OFFSET_ISR
);
882 if (status
& (FTMAC100_INT_RPKT_FINISH
| FTMAC100_INT_NORXBUF
)) {
884 * FTMAC100_INT_RPKT_FINISH:
885 * RX DMA has received packets into RX buffer successfully
887 * FTMAC100_INT_NORXBUF:
888 * RX buffer unavailable
893 retry
= ftmac100_rx_packet(priv
, &rx
);
894 } while (retry
&& rx
< budget
);
896 if (retry
&& rx
== budget
)
900 if (status
& (FTMAC100_INT_XPKT_OK
| FTMAC100_INT_XPKT_LOST
)) {
902 * FTMAC100_INT_XPKT_OK:
903 * packet transmitted to ethernet successfully
905 * FTMAC100_INT_XPKT_LOST:
906 * packet transmitted to ethernet lost due to late
907 * collision or excessive collision
909 ftmac100_tx_complete(priv
);
912 if (status
& (FTMAC100_INT_NORXBUF
| FTMAC100_INT_RPKT_LOST
|
913 FTMAC100_INT_AHB_ERR
| FTMAC100_INT_PHYSTS_CHG
)) {
915 netdev_info(netdev
, "[ISR] = 0x%x: %s%s%s%s\n", status
,
916 status
& FTMAC100_INT_NORXBUF
? "NORXBUF " : "",
917 status
& FTMAC100_INT_RPKT_LOST
? "RPKT_LOST " : "",
918 status
& FTMAC100_INT_AHB_ERR
? "AHB_ERR " : "",
919 status
& FTMAC100_INT_PHYSTS_CHG
? "PHYSTS_CHG" : "");
921 if (status
& FTMAC100_INT_NORXBUF
) {
922 /* RX buffer unavailable */
923 netdev
->stats
.rx_over_errors
++;
926 if (status
& FTMAC100_INT_RPKT_LOST
) {
927 /* received packet lost due to RX FIFO full */
928 netdev
->stats
.rx_fifo_errors
++;
931 if (status
& FTMAC100_INT_PHYSTS_CHG
) {
932 /* PHY link status change */
933 mii_check_link(&priv
->mii
);
940 ftmac100_enable_all_int(priv
);
946 /******************************************************************************
947 * struct net_device_ops functions
948 *****************************************************************************/
949 static int ftmac100_open(struct net_device
*netdev
)
951 struct ftmac100
*priv
= netdev_priv(netdev
);
954 err
= ftmac100_alloc_buffers(priv
);
956 netdev_err(netdev
, "failed to allocate buffers\n");
960 err
= request_irq(priv
->irq
, ftmac100_interrupt
, 0, netdev
->name
, netdev
);
962 netdev_err(netdev
, "failed to request irq %d\n", priv
->irq
);
966 priv
->rx_pointer
= 0;
967 priv
->tx_clean_pointer
= 0;
968 priv
->tx_pointer
= 0;
969 priv
->tx_pending
= 0;
971 err
= ftmac100_start_hw(priv
);
975 napi_enable(&priv
->napi
);
976 netif_start_queue(netdev
);
978 ftmac100_enable_all_int(priv
);
983 free_irq(priv
->irq
, netdev
);
985 ftmac100_free_buffers(priv
);
990 static int ftmac100_stop(struct net_device
*netdev
)
992 struct ftmac100
*priv
= netdev_priv(netdev
);
994 ftmac100_disable_all_int(priv
);
995 netif_stop_queue(netdev
);
996 napi_disable(&priv
->napi
);
997 ftmac100_stop_hw(priv
);
998 free_irq(priv
->irq
, netdev
);
999 ftmac100_free_buffers(priv
);
1005 ftmac100_hard_start_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
1007 struct ftmac100
*priv
= netdev_priv(netdev
);
1010 if (unlikely(skb
->len
> MAX_PKT_SIZE
)) {
1011 if (net_ratelimit())
1012 netdev_dbg(netdev
, "tx packet too big\n");
1014 netdev
->stats
.tx_dropped
++;
1016 return NETDEV_TX_OK
;
1019 map
= dma_map_single(priv
->dev
, skb
->data
, skb_headlen(skb
), DMA_TO_DEVICE
);
1020 if (unlikely(dma_mapping_error(priv
->dev
, map
))) {
1022 if (net_ratelimit())
1023 netdev_err(netdev
, "map socket buffer failed\n");
1025 netdev
->stats
.tx_dropped
++;
1027 return NETDEV_TX_OK
;
1030 return ftmac100_xmit(priv
, skb
, map
);
1034 static int ftmac100_do_ioctl(struct net_device
*netdev
, struct ifreq
*ifr
, int cmd
)
1036 struct ftmac100
*priv
= netdev_priv(netdev
);
1037 struct mii_ioctl_data
*data
= if_mii(ifr
);
1039 return generic_mii_ioctl(&priv
->mii
, data
, cmd
, NULL
);
1042 static const struct net_device_ops ftmac100_netdev_ops
= {
1043 .ndo_open
= ftmac100_open
,
1044 .ndo_stop
= ftmac100_stop
,
1045 .ndo_start_xmit
= ftmac100_hard_start_xmit
,
1046 .ndo_set_mac_address
= eth_mac_addr
,
1047 .ndo_validate_addr
= eth_validate_addr
,
1048 .ndo_do_ioctl
= ftmac100_do_ioctl
,
1051 /******************************************************************************
1052 * struct platform_driver functions
1053 *****************************************************************************/
1054 static int ftmac100_probe(struct platform_device
*pdev
)
1056 struct resource
*res
;
1058 struct net_device
*netdev
;
1059 struct ftmac100
*priv
;
1065 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1069 irq
= platform_get_irq(pdev
, 0);
1073 /* setup net_device */
1074 netdev
= alloc_etherdev(sizeof(*priv
));
1077 goto err_alloc_etherdev
;
1080 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
1081 netdev
->ethtool_ops
= &ftmac100_ethtool_ops
;
1082 netdev
->netdev_ops
= &ftmac100_netdev_ops
;
1084 platform_set_drvdata(pdev
, netdev
);
1086 /* setup private data */
1087 priv
= netdev_priv(netdev
);
1088 priv
->netdev
= netdev
;
1089 priv
->dev
= &pdev
->dev
;
1091 spin_lock_init(&priv
->tx_lock
);
1093 /* initialize NAPI */
1094 netif_napi_add(netdev
, &priv
->napi
, ftmac100_poll
, 64);
1097 priv
->res
= request_mem_region(res
->start
, resource_size(res
),
1098 dev_name(&pdev
->dev
));
1100 dev_err(&pdev
->dev
, "Could not reserve memory region\n");
1105 priv
->base
= ioremap(res
->start
, resource_size(res
));
1107 dev_err(&pdev
->dev
, "Failed to ioremap ethernet registers\n");
1114 /* initialize struct mii_if_info */
1115 priv
->mii
.phy_id
= 0;
1116 priv
->mii
.phy_id_mask
= 0x1f;
1117 priv
->mii
.reg_num_mask
= 0x1f;
1118 priv
->mii
.dev
= netdev
;
1119 priv
->mii
.mdio_read
= ftmac100_mdio_read
;
1120 priv
->mii
.mdio_write
= ftmac100_mdio_write
;
1122 /* register network device */
1123 err
= register_netdev(netdev
);
1125 dev_err(&pdev
->dev
, "Failed to register netdev\n");
1126 goto err_register_netdev
;
1129 netdev_info(netdev
, "irq %d, mapped at %p\n", priv
->irq
, priv
->base
);
1131 if (!is_valid_ether_addr(netdev
->dev_addr
)) {
1132 eth_hw_addr_random(netdev
);
1133 netdev_info(netdev
, "generated random MAC address %pM\n",
1139 err_register_netdev
:
1140 iounmap(priv
->base
);
1142 release_resource(priv
->res
);
1144 netif_napi_del(&priv
->napi
);
1145 free_netdev(netdev
);
1150 static int ftmac100_remove(struct platform_device
*pdev
)
1152 struct net_device
*netdev
;
1153 struct ftmac100
*priv
;
1155 netdev
= platform_get_drvdata(pdev
);
1156 priv
= netdev_priv(netdev
);
1158 unregister_netdev(netdev
);
1160 iounmap(priv
->base
);
1161 release_resource(priv
->res
);
1163 netif_napi_del(&priv
->napi
);
1164 free_netdev(netdev
);
1168 static const struct of_device_id ftmac100_of_ids
[] = {
1169 { .compatible
= "andestech,atmac100" },
1173 static struct platform_driver ftmac100_driver
= {
1174 .probe
= ftmac100_probe
,
1175 .remove
= ftmac100_remove
,
1178 .of_match_table
= ftmac100_of_ids
1182 /******************************************************************************
1183 * initialization / finalization
1184 *****************************************************************************/
1185 static int __init
ftmac100_init(void)
1187 pr_info("Loading version " DRV_VERSION
" ...\n");
1188 return platform_driver_register(&ftmac100_driver
);
1191 static void __exit
ftmac100_exit(void)
1193 platform_driver_unregister(&ftmac100_driver
);
1196 module_init(ftmac100_init
);
1197 module_exit(ftmac100_exit
);
1199 MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1200 MODULE_DESCRIPTION("FTMAC100 driver");
1201 MODULE_LICENSE("GPL");
1202 MODULE_DEVICE_TABLE(of
, ftmac100_of_ids
);