1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Faraday FTMAC100 10/100 Ethernet
5 * (C) Copyright 2009-2011 Faraday Technology
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"
27 #define RX_QUEUE_ENTRIES 128 /* must be power of 2 */
28 #define TX_QUEUE_ENTRIES 16 /* must be power of 2 */
30 #define MAX_PKT_SIZE 1518
31 #define RX_BUF_SIZE 2044 /* must be smaller than 0x7ff */
33 #if MAX_PKT_SIZE > 0x7ff
34 #error invalid MAX_PKT_SIZE
37 #if RX_BUF_SIZE > 0x7ff || RX_BUF_SIZE > PAGE_SIZE
38 #error invalid RX_BUF_SIZE
41 /******************************************************************************
43 *****************************************************************************/
44 struct ftmac100_descs {
45 struct ftmac100_rxdes rxdes[RX_QUEUE_ENTRIES];
46 struct ftmac100_txdes txdes[TX_QUEUE_ENTRIES];
54 struct ftmac100_descs *descs;
55 dma_addr_t descs_dma_addr;
57 unsigned int rx_pointer;
58 unsigned int tx_clean_pointer;
59 unsigned int tx_pointer;
60 unsigned int tx_pending;
64 struct net_device *netdev;
66 struct napi_struct napi;
68 struct mii_if_info mii;
71 static int ftmac100_alloc_rx_page(struct ftmac100 *priv,
72 struct ftmac100_rxdes *rxdes, gfp_t gfp);
74 /******************************************************************************
75 * internal functions (hardware register access)
76 *****************************************************************************/
77 #define INT_MASK_ALL_ENABLED (FTMAC100_INT_RPKT_FINISH | \
78 FTMAC100_INT_NORXBUF | \
79 FTMAC100_INT_XPKT_OK | \
80 FTMAC100_INT_XPKT_LOST | \
81 FTMAC100_INT_RPKT_LOST | \
82 FTMAC100_INT_AHB_ERR | \
83 FTMAC100_INT_PHYSTS_CHG)
85 #define INT_MASK_ALL_DISABLED 0
87 static void ftmac100_enable_all_int(struct ftmac100 *priv)
89 iowrite32(INT_MASK_ALL_ENABLED, priv->base + FTMAC100_OFFSET_IMR);
92 static void ftmac100_disable_all_int(struct ftmac100 *priv)
94 iowrite32(INT_MASK_ALL_DISABLED, priv->base + FTMAC100_OFFSET_IMR);
97 static void ftmac100_set_rx_ring_base(struct ftmac100 *priv, dma_addr_t addr)
99 iowrite32(addr, priv->base + FTMAC100_OFFSET_RXR_BADR);
102 static void ftmac100_set_tx_ring_base(struct ftmac100 *priv, dma_addr_t addr)
104 iowrite32(addr, priv->base + FTMAC100_OFFSET_TXR_BADR);
107 static void ftmac100_txdma_start_polling(struct ftmac100 *priv)
109 iowrite32(1, priv->base + FTMAC100_OFFSET_TXPD);
112 static int ftmac100_reset(struct ftmac100 *priv)
114 struct net_device *netdev = priv->netdev;
117 /* NOTE: reset clears all registers */
118 iowrite32(FTMAC100_MACCR_SW_RST, priv->base + FTMAC100_OFFSET_MACCR);
120 for (i = 0; i < 5; i++) {
123 maccr = ioread32(priv->base + FTMAC100_OFFSET_MACCR);
124 if (!(maccr & FTMAC100_MACCR_SW_RST)) {
126 * FTMAC100_MACCR_SW_RST cleared does not indicate
127 * that hardware reset completed (what the f*ck).
128 * We still need to wait for a while.
137 netdev_err(netdev, "software reset failed\n");
141 static void ftmac100_set_mac(struct ftmac100 *priv, const unsigned char *mac)
143 unsigned int maddr = mac[0] << 8 | mac[1];
144 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
146 iowrite32(maddr, priv->base + FTMAC100_OFFSET_MAC_MADR);
147 iowrite32(laddr, priv->base + FTMAC100_OFFSET_MAC_LADR);
150 #define MACCR_ENABLE_ALL (FTMAC100_MACCR_XMT_EN | \
151 FTMAC100_MACCR_RCV_EN | \
152 FTMAC100_MACCR_XDMA_EN | \
153 FTMAC100_MACCR_RDMA_EN | \
154 FTMAC100_MACCR_CRC_APD | \
155 FTMAC100_MACCR_FULLDUP | \
156 FTMAC100_MACCR_RX_RUNT | \
157 FTMAC100_MACCR_RX_BROADPKT)
159 static int ftmac100_start_hw(struct ftmac100 *priv)
161 struct net_device *netdev = priv->netdev;
163 if (ftmac100_reset(priv))
166 /* setup ring buffer base registers */
167 ftmac100_set_rx_ring_base(priv,
168 priv->descs_dma_addr +
169 offsetof(struct ftmac100_descs, rxdes));
170 ftmac100_set_tx_ring_base(priv,
171 priv->descs_dma_addr +
172 offsetof(struct ftmac100_descs, txdes));
174 iowrite32(FTMAC100_APTC_RXPOLL_CNT(1), priv->base + FTMAC100_OFFSET_APTC);
176 ftmac100_set_mac(priv, netdev->dev_addr);
178 iowrite32(MACCR_ENABLE_ALL, priv->base + FTMAC100_OFFSET_MACCR);
182 static void ftmac100_stop_hw(struct ftmac100 *priv)
184 iowrite32(0, priv->base + FTMAC100_OFFSET_MACCR);
187 /******************************************************************************
188 * internal functions (receive descriptor)
189 *****************************************************************************/
190 static bool ftmac100_rxdes_first_segment(struct ftmac100_rxdes *rxdes)
192 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_FRS);
195 static bool ftmac100_rxdes_last_segment(struct ftmac100_rxdes *rxdes)
197 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_LRS);
200 static bool ftmac100_rxdes_owned_by_dma(struct ftmac100_rxdes *rxdes)
202 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RXDMA_OWN);
205 static void ftmac100_rxdes_set_dma_own(struct ftmac100_rxdes *rxdes)
207 /* clear status bits */
208 rxdes->rxdes0 = cpu_to_le32(FTMAC100_RXDES0_RXDMA_OWN);
211 static bool ftmac100_rxdes_rx_error(struct ftmac100_rxdes *rxdes)
213 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RX_ERR);
216 static bool ftmac100_rxdes_crc_error(struct ftmac100_rxdes *rxdes)
218 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_CRC_ERR);
221 static bool ftmac100_rxdes_frame_too_long(struct ftmac100_rxdes *rxdes)
223 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_FTL);
226 static bool ftmac100_rxdes_runt(struct ftmac100_rxdes *rxdes)
228 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RUNT);
231 static bool ftmac100_rxdes_odd_nibble(struct ftmac100_rxdes *rxdes)
233 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RX_ODD_NB);
236 static unsigned int ftmac100_rxdes_frame_length(struct ftmac100_rxdes *rxdes)
238 return le32_to_cpu(rxdes->rxdes0) & FTMAC100_RXDES0_RFL;
241 static bool ftmac100_rxdes_multicast(struct ftmac100_rxdes *rxdes)
243 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_MULTICAST);
246 static void ftmac100_rxdes_set_buffer_size(struct ftmac100_rxdes *rxdes,
249 rxdes->rxdes1 &= cpu_to_le32(FTMAC100_RXDES1_EDORR);
250 rxdes->rxdes1 |= cpu_to_le32(FTMAC100_RXDES1_RXBUF_SIZE(size));
253 static void ftmac100_rxdes_set_end_of_ring(struct ftmac100_rxdes *rxdes)
255 rxdes->rxdes1 |= cpu_to_le32(FTMAC100_RXDES1_EDORR);
258 static void ftmac100_rxdes_set_dma_addr(struct ftmac100_rxdes *rxdes,
261 rxdes->rxdes2 = cpu_to_le32(addr);
264 static dma_addr_t ftmac100_rxdes_get_dma_addr(struct ftmac100_rxdes *rxdes)
266 return le32_to_cpu(rxdes->rxdes2);
270 * rxdes3 is not used by hardware. We use it to keep track of page.
271 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
273 static void ftmac100_rxdes_set_page(struct ftmac100_rxdes *rxdes, struct page *page)
275 rxdes->rxdes3 = (unsigned int)page;
278 static struct page *ftmac100_rxdes_get_page(struct ftmac100_rxdes *rxdes)
280 return (struct page *)rxdes->rxdes3;
283 /******************************************************************************
284 * internal functions (receive)
285 *****************************************************************************/
286 static int ftmac100_next_rx_pointer(int pointer)
288 return (pointer + 1) & (RX_QUEUE_ENTRIES - 1);
291 static void ftmac100_rx_pointer_advance(struct ftmac100 *priv)
293 priv->rx_pointer = ftmac100_next_rx_pointer(priv->rx_pointer);
296 static struct ftmac100_rxdes *ftmac100_current_rxdes(struct ftmac100 *priv)
298 return &priv->descs->rxdes[priv->rx_pointer];
301 static struct ftmac100_rxdes *
302 ftmac100_rx_locate_first_segment(struct ftmac100 *priv)
304 struct ftmac100_rxdes *rxdes = ftmac100_current_rxdes(priv);
306 while (!ftmac100_rxdes_owned_by_dma(rxdes)) {
307 if (ftmac100_rxdes_first_segment(rxdes))
310 ftmac100_rxdes_set_dma_own(rxdes);
311 ftmac100_rx_pointer_advance(priv);
312 rxdes = ftmac100_current_rxdes(priv);
318 static bool ftmac100_rx_packet_error(struct ftmac100 *priv,
319 struct ftmac100_rxdes *rxdes)
321 struct net_device *netdev = priv->netdev;
324 if (unlikely(ftmac100_rxdes_rx_error(rxdes))) {
326 netdev_info(netdev, "rx err\n");
328 netdev->stats.rx_errors++;
332 if (unlikely(ftmac100_rxdes_crc_error(rxdes))) {
334 netdev_info(netdev, "rx crc err\n");
336 netdev->stats.rx_crc_errors++;
340 if (unlikely(ftmac100_rxdes_frame_too_long(rxdes))) {
342 netdev_info(netdev, "rx frame too long\n");
344 netdev->stats.rx_length_errors++;
346 } else if (unlikely(ftmac100_rxdes_runt(rxdes))) {
348 netdev_info(netdev, "rx runt\n");
350 netdev->stats.rx_length_errors++;
352 } else if (unlikely(ftmac100_rxdes_odd_nibble(rxdes))) {
354 netdev_info(netdev, "rx odd nibble\n");
356 netdev->stats.rx_length_errors++;
363 static void ftmac100_rx_drop_packet(struct ftmac100 *priv)
365 struct net_device *netdev = priv->netdev;
366 struct ftmac100_rxdes *rxdes = ftmac100_current_rxdes(priv);
370 netdev_dbg(netdev, "drop packet %p\n", rxdes);
373 if (ftmac100_rxdes_last_segment(rxdes))
376 ftmac100_rxdes_set_dma_own(rxdes);
377 ftmac100_rx_pointer_advance(priv);
378 rxdes = ftmac100_current_rxdes(priv);
379 } while (!done && !ftmac100_rxdes_owned_by_dma(rxdes));
381 netdev->stats.rx_dropped++;
384 static bool ftmac100_rx_packet(struct ftmac100 *priv, int *processed)
386 struct net_device *netdev = priv->netdev;
387 struct ftmac100_rxdes *rxdes;
394 rxdes = ftmac100_rx_locate_first_segment(priv);
398 if (unlikely(ftmac100_rx_packet_error(priv, rxdes))) {
399 ftmac100_rx_drop_packet(priv);
404 * It is impossible to get multi-segment packets
405 * because we always provide big enough receive buffers.
407 ret = ftmac100_rxdes_last_segment(rxdes);
410 /* start processing */
411 skb = netdev_alloc_skb_ip_align(netdev, 128);
412 if (unlikely(!skb)) {
414 netdev_err(netdev, "rx skb alloc failed\n");
416 ftmac100_rx_drop_packet(priv);
420 if (unlikely(ftmac100_rxdes_multicast(rxdes)))
421 netdev->stats.multicast++;
423 map = ftmac100_rxdes_get_dma_addr(rxdes);
424 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
426 length = ftmac100_rxdes_frame_length(rxdes);
427 page = ftmac100_rxdes_get_page(rxdes);
428 skb_fill_page_desc(skb, 0, page, 0, length);
430 skb->data_len += length;
433 skb->truesize += PAGE_SIZE;
434 /* We pull the minimum amount into linear part */
435 __pskb_pull_tail(skb, ETH_HLEN);
437 /* Small frames are copied into linear part to free one page */
438 __pskb_pull_tail(skb, length);
440 ftmac100_alloc_rx_page(priv, rxdes, GFP_ATOMIC);
442 ftmac100_rx_pointer_advance(priv);
444 skb->protocol = eth_type_trans(skb, netdev);
446 netdev->stats.rx_packets++;
447 netdev->stats.rx_bytes += skb->len;
449 /* push packet to protocol stack */
450 netif_receive_skb(skb);
456 /******************************************************************************
457 * internal functions (transmit descriptor)
458 *****************************************************************************/
459 static void ftmac100_txdes_reset(struct ftmac100_txdes *txdes)
461 /* clear all except end of ring bit */
463 txdes->txdes1 &= cpu_to_le32(FTMAC100_TXDES1_EDOTR);
468 static bool ftmac100_txdes_owned_by_dma(struct ftmac100_txdes *txdes)
470 return txdes->txdes0 & cpu_to_le32(FTMAC100_TXDES0_TXDMA_OWN);
473 static void ftmac100_txdes_set_dma_own(struct ftmac100_txdes *txdes)
476 * Make sure dma own bit will not be set before any other
480 txdes->txdes0 |= cpu_to_le32(FTMAC100_TXDES0_TXDMA_OWN);
483 static bool ftmac100_txdes_excessive_collision(struct ftmac100_txdes *txdes)
485 return txdes->txdes0 & cpu_to_le32(FTMAC100_TXDES0_TXPKT_EXSCOL);
488 static bool ftmac100_txdes_late_collision(struct ftmac100_txdes *txdes)
490 return txdes->txdes0 & cpu_to_le32(FTMAC100_TXDES0_TXPKT_LATECOL);
493 static void ftmac100_txdes_set_end_of_ring(struct ftmac100_txdes *txdes)
495 txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_EDOTR);
498 static void ftmac100_txdes_set_first_segment(struct ftmac100_txdes *txdes)
500 txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_FTS);
503 static void ftmac100_txdes_set_last_segment(struct ftmac100_txdes *txdes)
505 txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_LTS);
508 static void ftmac100_txdes_set_txint(struct ftmac100_txdes *txdes)
510 txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_TXIC);
513 static void ftmac100_txdes_set_buffer_size(struct ftmac100_txdes *txdes,
516 txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_TXBUF_SIZE(len));
519 static void ftmac100_txdes_set_dma_addr(struct ftmac100_txdes *txdes,
522 txdes->txdes2 = cpu_to_le32(addr);
525 static dma_addr_t ftmac100_txdes_get_dma_addr(struct ftmac100_txdes *txdes)
527 return le32_to_cpu(txdes->txdes2);
531 * txdes3 is not used by hardware. We use it to keep track of socket buffer.
532 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
534 static void ftmac100_txdes_set_skb(struct ftmac100_txdes *txdes, struct sk_buff *skb)
536 txdes->txdes3 = (unsigned int)skb;
539 static struct sk_buff *ftmac100_txdes_get_skb(struct ftmac100_txdes *txdes)
541 return (struct sk_buff *)txdes->txdes3;
544 /******************************************************************************
545 * internal functions (transmit)
546 *****************************************************************************/
547 static int ftmac100_next_tx_pointer(int pointer)
549 return (pointer + 1) & (TX_QUEUE_ENTRIES - 1);
552 static void ftmac100_tx_pointer_advance(struct ftmac100 *priv)
554 priv->tx_pointer = ftmac100_next_tx_pointer(priv->tx_pointer);
557 static void ftmac100_tx_clean_pointer_advance(struct ftmac100 *priv)
559 priv->tx_clean_pointer = ftmac100_next_tx_pointer(priv->tx_clean_pointer);
562 static struct ftmac100_txdes *ftmac100_current_txdes(struct ftmac100 *priv)
564 return &priv->descs->txdes[priv->tx_pointer];
567 static struct ftmac100_txdes *ftmac100_current_clean_txdes(struct ftmac100 *priv)
569 return &priv->descs->txdes[priv->tx_clean_pointer];
572 static bool ftmac100_tx_complete_packet(struct ftmac100 *priv)
574 struct net_device *netdev = priv->netdev;
575 struct ftmac100_txdes *txdes;
579 if (priv->tx_pending == 0)
582 txdes = ftmac100_current_clean_txdes(priv);
584 if (ftmac100_txdes_owned_by_dma(txdes))
587 skb = ftmac100_txdes_get_skb(txdes);
588 map = ftmac100_txdes_get_dma_addr(txdes);
590 if (unlikely(ftmac100_txdes_excessive_collision(txdes) ||
591 ftmac100_txdes_late_collision(txdes))) {
593 * packet transmitted to ethernet lost due to late collision
594 * or excessive collision
596 netdev->stats.tx_aborted_errors++;
598 netdev->stats.tx_packets++;
599 netdev->stats.tx_bytes += skb->len;
602 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
605 ftmac100_txdes_reset(txdes);
607 ftmac100_tx_clean_pointer_advance(priv);
609 spin_lock(&priv->tx_lock);
611 spin_unlock(&priv->tx_lock);
612 netif_wake_queue(netdev);
617 static void ftmac100_tx_complete(struct ftmac100 *priv)
619 while (ftmac100_tx_complete_packet(priv))
623 static netdev_tx_t ftmac100_xmit(struct ftmac100 *priv, struct sk_buff *skb,
626 struct net_device *netdev = priv->netdev;
627 struct ftmac100_txdes *txdes;
628 unsigned int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
630 txdes = ftmac100_current_txdes(priv);
631 ftmac100_tx_pointer_advance(priv);
633 /* setup TX descriptor */
634 ftmac100_txdes_set_skb(txdes, skb);
635 ftmac100_txdes_set_dma_addr(txdes, map);
637 ftmac100_txdes_set_first_segment(txdes);
638 ftmac100_txdes_set_last_segment(txdes);
639 ftmac100_txdes_set_txint(txdes);
640 ftmac100_txdes_set_buffer_size(txdes, len);
642 spin_lock(&priv->tx_lock);
644 if (priv->tx_pending == TX_QUEUE_ENTRIES)
645 netif_stop_queue(netdev);
648 ftmac100_txdes_set_dma_own(txdes);
649 spin_unlock(&priv->tx_lock);
651 ftmac100_txdma_start_polling(priv);
655 /******************************************************************************
656 * internal functions (buffer)
657 *****************************************************************************/
658 static int ftmac100_alloc_rx_page(struct ftmac100 *priv,
659 struct ftmac100_rxdes *rxdes, gfp_t gfp)
661 struct net_device *netdev = priv->netdev;
665 page = alloc_page(gfp);
668 netdev_err(netdev, "failed to allocate rx page\n");
672 map = dma_map_page(priv->dev, page, 0, RX_BUF_SIZE, DMA_FROM_DEVICE);
673 if (unlikely(dma_mapping_error(priv->dev, map))) {
675 netdev_err(netdev, "failed to map rx page\n");
680 ftmac100_rxdes_set_page(rxdes, page);
681 ftmac100_rxdes_set_dma_addr(rxdes, map);
682 ftmac100_rxdes_set_buffer_size(rxdes, RX_BUF_SIZE);
683 ftmac100_rxdes_set_dma_own(rxdes);
687 static void ftmac100_free_buffers(struct ftmac100 *priv)
691 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
692 struct ftmac100_rxdes *rxdes = &priv->descs->rxdes[i];
693 struct page *page = ftmac100_rxdes_get_page(rxdes);
694 dma_addr_t map = ftmac100_rxdes_get_dma_addr(rxdes);
699 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
703 for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
704 struct ftmac100_txdes *txdes = &priv->descs->txdes[i];
705 struct sk_buff *skb = ftmac100_txdes_get_skb(txdes);
706 dma_addr_t map = ftmac100_txdes_get_dma_addr(txdes);
711 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
715 dma_free_coherent(priv->dev, sizeof(struct ftmac100_descs),
716 priv->descs, priv->descs_dma_addr);
719 static int ftmac100_alloc_buffers(struct ftmac100 *priv)
723 priv->descs = dma_alloc_coherent(priv->dev,
724 sizeof(struct ftmac100_descs),
725 &priv->descs_dma_addr, GFP_KERNEL);
729 /* initialize RX ring */
730 ftmac100_rxdes_set_end_of_ring(&priv->descs->rxdes[RX_QUEUE_ENTRIES - 1]);
732 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
733 struct ftmac100_rxdes *rxdes = &priv->descs->rxdes[i];
735 if (ftmac100_alloc_rx_page(priv, rxdes, GFP_KERNEL))
739 /* initialize TX ring */
740 ftmac100_txdes_set_end_of_ring(&priv->descs->txdes[TX_QUEUE_ENTRIES - 1]);
744 ftmac100_free_buffers(priv);
748 /******************************************************************************
749 * struct mii_if_info functions
750 *****************************************************************************/
751 static int ftmac100_mdio_read(struct net_device *netdev, int phy_id, int reg)
753 struct ftmac100 *priv = netdev_priv(netdev);
757 phycr = FTMAC100_PHYCR_PHYAD(phy_id) |
758 FTMAC100_PHYCR_REGAD(reg) |
759 FTMAC100_PHYCR_MIIRD;
761 iowrite32(phycr, priv->base + FTMAC100_OFFSET_PHYCR);
763 for (i = 0; i < 10; i++) {
764 phycr = ioread32(priv->base + FTMAC100_OFFSET_PHYCR);
766 if ((phycr & FTMAC100_PHYCR_MIIRD) == 0)
767 return phycr & FTMAC100_PHYCR_MIIRDATA;
772 netdev_err(netdev, "mdio read timed out\n");
776 static void ftmac100_mdio_write(struct net_device *netdev, int phy_id, int reg,
779 struct ftmac100 *priv = netdev_priv(netdev);
783 phycr = FTMAC100_PHYCR_PHYAD(phy_id) |
784 FTMAC100_PHYCR_REGAD(reg) |
785 FTMAC100_PHYCR_MIIWR;
787 data = FTMAC100_PHYWDATA_MIIWDATA(data);
789 iowrite32(data, priv->base + FTMAC100_OFFSET_PHYWDATA);
790 iowrite32(phycr, priv->base + FTMAC100_OFFSET_PHYCR);
792 for (i = 0; i < 10; i++) {
793 phycr = ioread32(priv->base + FTMAC100_OFFSET_PHYCR);
795 if ((phycr & FTMAC100_PHYCR_MIIWR) == 0)
801 netdev_err(netdev, "mdio write timed out\n");
804 /******************************************************************************
805 * struct ethtool_ops functions
806 *****************************************************************************/
807 static void ftmac100_get_drvinfo(struct net_device *netdev,
808 struct ethtool_drvinfo *info)
810 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
811 strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
814 static int ftmac100_get_link_ksettings(struct net_device *netdev,
815 struct ethtool_link_ksettings *cmd)
817 struct ftmac100 *priv = netdev_priv(netdev);
819 mii_ethtool_get_link_ksettings(&priv->mii, cmd);
824 static int ftmac100_set_link_ksettings(struct net_device *netdev,
825 const struct ethtool_link_ksettings *cmd)
827 struct ftmac100 *priv = netdev_priv(netdev);
828 return mii_ethtool_set_link_ksettings(&priv->mii, cmd);
831 static int ftmac100_nway_reset(struct net_device *netdev)
833 struct ftmac100 *priv = netdev_priv(netdev);
834 return mii_nway_restart(&priv->mii);
837 static u32 ftmac100_get_link(struct net_device *netdev)
839 struct ftmac100 *priv = netdev_priv(netdev);
840 return mii_link_ok(&priv->mii);
843 static const struct ethtool_ops ftmac100_ethtool_ops = {
844 .get_drvinfo = ftmac100_get_drvinfo,
845 .nway_reset = ftmac100_nway_reset,
846 .get_link = ftmac100_get_link,
847 .get_link_ksettings = ftmac100_get_link_ksettings,
848 .set_link_ksettings = ftmac100_set_link_ksettings,
851 /******************************************************************************
853 *****************************************************************************/
854 static irqreturn_t ftmac100_interrupt(int irq, void *dev_id)
856 struct net_device *netdev = dev_id;
857 struct ftmac100 *priv = netdev_priv(netdev);
859 /* Disable interrupts for polling */
860 ftmac100_disable_all_int(priv);
861 if (likely(netif_running(netdev)))
862 napi_schedule(&priv->napi);
867 /******************************************************************************
868 * struct napi_struct functions
869 *****************************************************************************/
870 static int ftmac100_poll(struct napi_struct *napi, int budget)
872 struct ftmac100 *priv = container_of(napi, struct ftmac100, napi);
873 struct net_device *netdev = priv->netdev;
875 bool completed = true;
878 status = ioread32(priv->base + FTMAC100_OFFSET_ISR);
880 if (status & (FTMAC100_INT_RPKT_FINISH | FTMAC100_INT_NORXBUF)) {
882 * FTMAC100_INT_RPKT_FINISH:
883 * RX DMA has received packets into RX buffer successfully
885 * FTMAC100_INT_NORXBUF:
886 * RX buffer unavailable
891 retry = ftmac100_rx_packet(priv, &rx);
892 } while (retry && rx < budget);
894 if (retry && rx == budget)
898 if (status & (FTMAC100_INT_XPKT_OK | FTMAC100_INT_XPKT_LOST)) {
900 * FTMAC100_INT_XPKT_OK:
901 * packet transmitted to ethernet successfully
903 * FTMAC100_INT_XPKT_LOST:
904 * packet transmitted to ethernet lost due to late
905 * collision or excessive collision
907 ftmac100_tx_complete(priv);
910 if (status & (FTMAC100_INT_NORXBUF | FTMAC100_INT_RPKT_LOST |
911 FTMAC100_INT_AHB_ERR | FTMAC100_INT_PHYSTS_CHG)) {
913 netdev_info(netdev, "[ISR] = 0x%x: %s%s%s%s\n", status,
914 status & FTMAC100_INT_NORXBUF ? "NORXBUF " : "",
915 status & FTMAC100_INT_RPKT_LOST ? "RPKT_LOST " : "",
916 status & FTMAC100_INT_AHB_ERR ? "AHB_ERR " : "",
917 status & FTMAC100_INT_PHYSTS_CHG ? "PHYSTS_CHG" : "");
919 if (status & FTMAC100_INT_NORXBUF) {
920 /* RX buffer unavailable */
921 netdev->stats.rx_over_errors++;
924 if (status & FTMAC100_INT_RPKT_LOST) {
925 /* received packet lost due to RX FIFO full */
926 netdev->stats.rx_fifo_errors++;
929 if (status & FTMAC100_INT_PHYSTS_CHG) {
930 /* PHY link status change */
931 mii_check_link(&priv->mii);
938 ftmac100_enable_all_int(priv);
944 /******************************************************************************
945 * struct net_device_ops functions
946 *****************************************************************************/
947 static int ftmac100_open(struct net_device *netdev)
949 struct ftmac100 *priv = netdev_priv(netdev);
952 err = ftmac100_alloc_buffers(priv);
954 netdev_err(netdev, "failed to allocate buffers\n");
958 err = request_irq(priv->irq, ftmac100_interrupt, 0, netdev->name, netdev);
960 netdev_err(netdev, "failed to request irq %d\n", priv->irq);
964 priv->rx_pointer = 0;
965 priv->tx_clean_pointer = 0;
966 priv->tx_pointer = 0;
967 priv->tx_pending = 0;
969 err = ftmac100_start_hw(priv);
973 napi_enable(&priv->napi);
974 netif_start_queue(netdev);
976 ftmac100_enable_all_int(priv);
981 free_irq(priv->irq, netdev);
983 ftmac100_free_buffers(priv);
988 static int ftmac100_stop(struct net_device *netdev)
990 struct ftmac100 *priv = netdev_priv(netdev);
992 ftmac100_disable_all_int(priv);
993 netif_stop_queue(netdev);
994 napi_disable(&priv->napi);
995 ftmac100_stop_hw(priv);
996 free_irq(priv->irq, netdev);
997 ftmac100_free_buffers(priv);
1003 ftmac100_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
1005 struct ftmac100 *priv = netdev_priv(netdev);
1008 if (unlikely(skb->len > MAX_PKT_SIZE)) {
1009 if (net_ratelimit())
1010 netdev_dbg(netdev, "tx packet too big\n");
1012 netdev->stats.tx_dropped++;
1014 return NETDEV_TX_OK;
1017 map = dma_map_single(priv->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
1018 if (unlikely(dma_mapping_error(priv->dev, map))) {
1020 if (net_ratelimit())
1021 netdev_err(netdev, "map socket buffer failed\n");
1023 netdev->stats.tx_dropped++;
1025 return NETDEV_TX_OK;
1028 return ftmac100_xmit(priv, skb, map);
1032 static int ftmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1034 struct ftmac100 *priv = netdev_priv(netdev);
1035 struct mii_ioctl_data *data = if_mii(ifr);
1037 return generic_mii_ioctl(&priv->mii, data, cmd, NULL);
1040 static const struct net_device_ops ftmac100_netdev_ops = {
1041 .ndo_open = ftmac100_open,
1042 .ndo_stop = ftmac100_stop,
1043 .ndo_start_xmit = ftmac100_hard_start_xmit,
1044 .ndo_set_mac_address = eth_mac_addr,
1045 .ndo_validate_addr = eth_validate_addr,
1046 .ndo_eth_ioctl = ftmac100_do_ioctl,
1049 /******************************************************************************
1050 * struct platform_driver functions
1051 *****************************************************************************/
1052 static int ftmac100_probe(struct platform_device *pdev)
1054 struct resource *res;
1056 struct net_device *netdev;
1057 struct ftmac100 *priv;
1060 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1064 irq = platform_get_irq(pdev, 0);
1068 /* setup net_device */
1069 netdev = alloc_etherdev(sizeof(*priv));
1072 goto err_alloc_etherdev;
1075 SET_NETDEV_DEV(netdev, &pdev->dev);
1076 netdev->ethtool_ops = &ftmac100_ethtool_ops;
1077 netdev->netdev_ops = &ftmac100_netdev_ops;
1079 platform_set_drvdata(pdev, netdev);
1081 /* setup private data */
1082 priv = netdev_priv(netdev);
1083 priv->netdev = netdev;
1084 priv->dev = &pdev->dev;
1086 spin_lock_init(&priv->tx_lock);
1088 /* initialize NAPI */
1089 netif_napi_add(netdev, &priv->napi, ftmac100_poll, 64);
1092 priv->res = request_mem_region(res->start, resource_size(res),
1093 dev_name(&pdev->dev));
1095 dev_err(&pdev->dev, "Could not reserve memory region\n");
1100 priv->base = ioremap(res->start, resource_size(res));
1102 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1109 /* initialize struct mii_if_info */
1110 priv->mii.phy_id = 0;
1111 priv->mii.phy_id_mask = 0x1f;
1112 priv->mii.reg_num_mask = 0x1f;
1113 priv->mii.dev = netdev;
1114 priv->mii.mdio_read = ftmac100_mdio_read;
1115 priv->mii.mdio_write = ftmac100_mdio_write;
1117 /* register network device */
1118 err = register_netdev(netdev);
1120 dev_err(&pdev->dev, "Failed to register netdev\n");
1121 goto err_register_netdev;
1124 netdev_info(netdev, "irq %d, mapped at %p\n", priv->irq, priv->base);
1126 if (!is_valid_ether_addr(netdev->dev_addr)) {
1127 eth_hw_addr_random(netdev);
1128 netdev_info(netdev, "generated random MAC address %pM\n",
1134 err_register_netdev:
1135 iounmap(priv->base);
1137 release_resource(priv->res);
1139 netif_napi_del(&priv->napi);
1140 free_netdev(netdev);
1145 static int ftmac100_remove(struct platform_device *pdev)
1147 struct net_device *netdev;
1148 struct ftmac100 *priv;
1150 netdev = platform_get_drvdata(pdev);
1151 priv = netdev_priv(netdev);
1153 unregister_netdev(netdev);
1155 iounmap(priv->base);
1156 release_resource(priv->res);
1158 netif_napi_del(&priv->napi);
1159 free_netdev(netdev);
1163 static const struct of_device_id ftmac100_of_ids[] = {
1164 { .compatible = "andestech,atmac100" },
1168 static struct platform_driver ftmac100_driver = {
1169 .probe = ftmac100_probe,
1170 .remove = ftmac100_remove,
1173 .of_match_table = ftmac100_of_ids
1177 /******************************************************************************
1178 * initialization / finalization
1179 *****************************************************************************/
1180 module_platform_driver(ftmac100_driver);
1183 MODULE_DESCRIPTION("FTMAC100 driver");
1184 MODULE_LICENSE("GPL");
1185 MODULE_DEVICE_TABLE(of, ftmac100_of_ids);