2 * Faraday FTMAC100 10/100 Ethernet
4 * (C) Copyright 2009-2011 Faraday Technology
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/dma-mapping.h>
25 #include <linux/etherdevice.h>
26 #include <linux/ethtool.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
30 #include <linux/mii.h>
31 #include <linux/module.h>
32 #include <linux/mod_devicetable.h>
33 #include <linux/netdevice.h>
34 #include <linux/platform_device.h>
38 #define DRV_NAME "ftmac100"
39 #define DRV_VERSION "0.2"
41 #define RX_QUEUE_ENTRIES 128 /* must be power of 2 */
42 #define TX_QUEUE_ENTRIES 16 /* must be power of 2 */
44 #define MAX_PKT_SIZE 1518
45 #define RX_BUF_SIZE 2044 /* must be smaller than 0x7ff */
47 #if MAX_PKT_SIZE > 0x7ff
48 #error invalid MAX_PKT_SIZE
51 #if RX_BUF_SIZE > 0x7ff || RX_BUF_SIZE > PAGE_SIZE
52 #error invalid RX_BUF_SIZE
55 /******************************************************************************
57 *****************************************************************************/
58 struct ftmac100_descs {
59 struct ftmac100_rxdes rxdes[RX_QUEUE_ENTRIES];
60 struct ftmac100_txdes txdes[TX_QUEUE_ENTRIES];
68 struct ftmac100_descs *descs;
69 dma_addr_t descs_dma_addr;
71 unsigned int rx_pointer;
72 unsigned int tx_clean_pointer;
73 unsigned int tx_pointer;
74 unsigned int tx_pending;
78 struct net_device *netdev;
80 struct napi_struct napi;
82 struct mii_if_info mii;
85 static int ftmac100_alloc_rx_page(struct ftmac100 *priv,
86 struct ftmac100_rxdes *rxdes, gfp_t gfp);
88 /******************************************************************************
89 * internal functions (hardware register access)
90 *****************************************************************************/
91 #define INT_MASK_ALL_ENABLED (FTMAC100_INT_RPKT_FINISH | \
92 FTMAC100_INT_NORXBUF | \
93 FTMAC100_INT_XPKT_OK | \
94 FTMAC100_INT_XPKT_LOST | \
95 FTMAC100_INT_RPKT_LOST | \
96 FTMAC100_INT_AHB_ERR | \
97 FTMAC100_INT_PHYSTS_CHG)
99 #define INT_MASK_ALL_DISABLED 0
101 static void ftmac100_enable_all_int(struct ftmac100 *priv)
103 iowrite32(INT_MASK_ALL_ENABLED, priv->base + FTMAC100_OFFSET_IMR);
106 static void ftmac100_disable_all_int(struct ftmac100 *priv)
108 iowrite32(INT_MASK_ALL_DISABLED, priv->base + FTMAC100_OFFSET_IMR);
111 static void ftmac100_set_rx_ring_base(struct ftmac100 *priv, dma_addr_t addr)
113 iowrite32(addr, priv->base + FTMAC100_OFFSET_RXR_BADR);
116 static void ftmac100_set_tx_ring_base(struct ftmac100 *priv, dma_addr_t addr)
118 iowrite32(addr, priv->base + FTMAC100_OFFSET_TXR_BADR);
121 static void ftmac100_txdma_start_polling(struct ftmac100 *priv)
123 iowrite32(1, priv->base + FTMAC100_OFFSET_TXPD);
126 static int ftmac100_reset(struct ftmac100 *priv)
128 struct net_device *netdev = priv->netdev;
131 /* NOTE: reset clears all registers */
132 iowrite32(FTMAC100_MACCR_SW_RST, priv->base + FTMAC100_OFFSET_MACCR);
134 for (i = 0; i < 5; i++) {
137 maccr = ioread32(priv->base + FTMAC100_OFFSET_MACCR);
138 if (!(maccr & FTMAC100_MACCR_SW_RST)) {
140 * FTMAC100_MACCR_SW_RST cleared does not indicate
141 * that hardware reset completed (what the f*ck).
142 * We still need to wait for a while.
151 netdev_err(netdev, "software reset failed\n");
155 static void ftmac100_set_mac(struct ftmac100 *priv, const unsigned char *mac)
157 unsigned int maddr = mac[0] << 8 | mac[1];
158 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
160 iowrite32(maddr, priv->base + FTMAC100_OFFSET_MAC_MADR);
161 iowrite32(laddr, priv->base + FTMAC100_OFFSET_MAC_LADR);
164 #define MACCR_ENABLE_ALL (FTMAC100_MACCR_XMT_EN | \
165 FTMAC100_MACCR_RCV_EN | \
166 FTMAC100_MACCR_XDMA_EN | \
167 FTMAC100_MACCR_RDMA_EN | \
168 FTMAC100_MACCR_CRC_APD | \
169 FTMAC100_MACCR_FULLDUP | \
170 FTMAC100_MACCR_RX_RUNT | \
171 FTMAC100_MACCR_RX_BROADPKT)
173 static int ftmac100_start_hw(struct ftmac100 *priv)
175 struct net_device *netdev = priv->netdev;
177 if (ftmac100_reset(priv))
180 /* setup ring buffer base registers */
181 ftmac100_set_rx_ring_base(priv,
182 priv->descs_dma_addr +
183 offsetof(struct ftmac100_descs, rxdes));
184 ftmac100_set_tx_ring_base(priv,
185 priv->descs_dma_addr +
186 offsetof(struct ftmac100_descs, txdes));
188 iowrite32(FTMAC100_APTC_RXPOLL_CNT(1), priv->base + FTMAC100_OFFSET_APTC);
190 ftmac100_set_mac(priv, netdev->dev_addr);
192 iowrite32(MACCR_ENABLE_ALL, priv->base + FTMAC100_OFFSET_MACCR);
196 static void ftmac100_stop_hw(struct ftmac100 *priv)
198 iowrite32(0, priv->base + FTMAC100_OFFSET_MACCR);
201 /******************************************************************************
202 * internal functions (receive descriptor)
203 *****************************************************************************/
204 static bool ftmac100_rxdes_first_segment(struct ftmac100_rxdes *rxdes)
206 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_FRS);
209 static bool ftmac100_rxdes_last_segment(struct ftmac100_rxdes *rxdes)
211 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_LRS);
214 static bool ftmac100_rxdes_owned_by_dma(struct ftmac100_rxdes *rxdes)
216 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RXDMA_OWN);
219 static void ftmac100_rxdes_set_dma_own(struct ftmac100_rxdes *rxdes)
221 /* clear status bits */
222 rxdes->rxdes0 = cpu_to_le32(FTMAC100_RXDES0_RXDMA_OWN);
225 static bool ftmac100_rxdes_rx_error(struct ftmac100_rxdes *rxdes)
227 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RX_ERR);
230 static bool ftmac100_rxdes_crc_error(struct ftmac100_rxdes *rxdes)
232 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_CRC_ERR);
235 static bool ftmac100_rxdes_frame_too_long(struct ftmac100_rxdes *rxdes)
237 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_FTL);
240 static bool ftmac100_rxdes_runt(struct ftmac100_rxdes *rxdes)
242 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RUNT);
245 static bool ftmac100_rxdes_odd_nibble(struct ftmac100_rxdes *rxdes)
247 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RX_ODD_NB);
250 static unsigned int ftmac100_rxdes_frame_length(struct ftmac100_rxdes *rxdes)
252 return le32_to_cpu(rxdes->rxdes0) & FTMAC100_RXDES0_RFL;
255 static bool ftmac100_rxdes_multicast(struct ftmac100_rxdes *rxdes)
257 return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_MULTICAST);
260 static void ftmac100_rxdes_set_buffer_size(struct ftmac100_rxdes *rxdes,
263 rxdes->rxdes1 &= cpu_to_le32(FTMAC100_RXDES1_EDORR);
264 rxdes->rxdes1 |= cpu_to_le32(FTMAC100_RXDES1_RXBUF_SIZE(size));
267 static void ftmac100_rxdes_set_end_of_ring(struct ftmac100_rxdes *rxdes)
269 rxdes->rxdes1 |= cpu_to_le32(FTMAC100_RXDES1_EDORR);
272 static void ftmac100_rxdes_set_dma_addr(struct ftmac100_rxdes *rxdes,
275 rxdes->rxdes2 = cpu_to_le32(addr);
278 static dma_addr_t ftmac100_rxdes_get_dma_addr(struct ftmac100_rxdes *rxdes)
280 return le32_to_cpu(rxdes->rxdes2);
284 * rxdes3 is not used by hardware. We use it to keep track of page.
285 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
287 static void ftmac100_rxdes_set_page(struct ftmac100_rxdes *rxdes, struct page *page)
289 rxdes->rxdes3 = (unsigned int)page;
292 static struct page *ftmac100_rxdes_get_page(struct ftmac100_rxdes *rxdes)
294 return (struct page *)rxdes->rxdes3;
297 /******************************************************************************
298 * internal functions (receive)
299 *****************************************************************************/
300 static int ftmac100_next_rx_pointer(int pointer)
302 return (pointer + 1) & (RX_QUEUE_ENTRIES - 1);
305 static void ftmac100_rx_pointer_advance(struct ftmac100 *priv)
307 priv->rx_pointer = ftmac100_next_rx_pointer(priv->rx_pointer);
310 static struct ftmac100_rxdes *ftmac100_current_rxdes(struct ftmac100 *priv)
312 return &priv->descs->rxdes[priv->rx_pointer];
315 static struct ftmac100_rxdes *
316 ftmac100_rx_locate_first_segment(struct ftmac100 *priv)
318 struct ftmac100_rxdes *rxdes = ftmac100_current_rxdes(priv);
320 while (!ftmac100_rxdes_owned_by_dma(rxdes)) {
321 if (ftmac100_rxdes_first_segment(rxdes))
324 ftmac100_rxdes_set_dma_own(rxdes);
325 ftmac100_rx_pointer_advance(priv);
326 rxdes = ftmac100_current_rxdes(priv);
332 static bool ftmac100_rx_packet_error(struct ftmac100 *priv,
333 struct ftmac100_rxdes *rxdes)
335 struct net_device *netdev = priv->netdev;
338 if (unlikely(ftmac100_rxdes_rx_error(rxdes))) {
340 netdev_info(netdev, "rx err\n");
342 netdev->stats.rx_errors++;
346 if (unlikely(ftmac100_rxdes_crc_error(rxdes))) {
348 netdev_info(netdev, "rx crc err\n");
350 netdev->stats.rx_crc_errors++;
354 if (unlikely(ftmac100_rxdes_frame_too_long(rxdes))) {
356 netdev_info(netdev, "rx frame too long\n");
358 netdev->stats.rx_length_errors++;
360 } else if (unlikely(ftmac100_rxdes_runt(rxdes))) {
362 netdev_info(netdev, "rx runt\n");
364 netdev->stats.rx_length_errors++;
366 } else if (unlikely(ftmac100_rxdes_odd_nibble(rxdes))) {
368 netdev_info(netdev, "rx odd nibble\n");
370 netdev->stats.rx_length_errors++;
377 static void ftmac100_rx_drop_packet(struct ftmac100 *priv)
379 struct net_device *netdev = priv->netdev;
380 struct ftmac100_rxdes *rxdes = ftmac100_current_rxdes(priv);
384 netdev_dbg(netdev, "drop packet %p\n", rxdes);
387 if (ftmac100_rxdes_last_segment(rxdes))
390 ftmac100_rxdes_set_dma_own(rxdes);
391 ftmac100_rx_pointer_advance(priv);
392 rxdes = ftmac100_current_rxdes(priv);
393 } while (!done && !ftmac100_rxdes_owned_by_dma(rxdes));
395 netdev->stats.rx_dropped++;
398 static bool ftmac100_rx_packet(struct ftmac100 *priv, int *processed)
400 struct net_device *netdev = priv->netdev;
401 struct ftmac100_rxdes *rxdes;
408 rxdes = ftmac100_rx_locate_first_segment(priv);
412 if (unlikely(ftmac100_rx_packet_error(priv, rxdes))) {
413 ftmac100_rx_drop_packet(priv);
418 * It is impossible to get multi-segment packets
419 * because we always provide big enough receive buffers.
421 ret = ftmac100_rxdes_last_segment(rxdes);
424 /* start processing */
425 skb = netdev_alloc_skb_ip_align(netdev, 128);
426 if (unlikely(!skb)) {
428 netdev_err(netdev, "rx skb alloc failed\n");
430 ftmac100_rx_drop_packet(priv);
434 if (unlikely(ftmac100_rxdes_multicast(rxdes)))
435 netdev->stats.multicast++;
437 map = ftmac100_rxdes_get_dma_addr(rxdes);
438 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
440 length = ftmac100_rxdes_frame_length(rxdes);
441 page = ftmac100_rxdes_get_page(rxdes);
442 skb_fill_page_desc(skb, 0, page, 0, length);
444 skb->data_len += length;
447 skb->truesize += PAGE_SIZE;
448 /* We pull the minimum amount into linear part */
449 __pskb_pull_tail(skb, ETH_HLEN);
451 /* Small frames are copied into linear part to free one page */
452 __pskb_pull_tail(skb, length);
454 ftmac100_alloc_rx_page(priv, rxdes, GFP_ATOMIC);
456 ftmac100_rx_pointer_advance(priv);
458 skb->protocol = eth_type_trans(skb, netdev);
460 netdev->stats.rx_packets++;
461 netdev->stats.rx_bytes += skb->len;
463 /* push packet to protocol stack */
464 netif_receive_skb(skb);
470 /******************************************************************************
471 * internal functions (transmit descriptor)
472 *****************************************************************************/
473 static void ftmac100_txdes_reset(struct ftmac100_txdes *txdes)
475 /* clear all except end of ring bit */
477 txdes->txdes1 &= cpu_to_le32(FTMAC100_TXDES1_EDOTR);
482 static bool ftmac100_txdes_owned_by_dma(struct ftmac100_txdes *txdes)
484 return txdes->txdes0 & cpu_to_le32(FTMAC100_TXDES0_TXDMA_OWN);
487 static void ftmac100_txdes_set_dma_own(struct ftmac100_txdes *txdes)
490 * Make sure dma own bit will not be set before any other
494 txdes->txdes0 |= cpu_to_le32(FTMAC100_TXDES0_TXDMA_OWN);
497 static bool ftmac100_txdes_excessive_collision(struct ftmac100_txdes *txdes)
499 return txdes->txdes0 & cpu_to_le32(FTMAC100_TXDES0_TXPKT_EXSCOL);
502 static bool ftmac100_txdes_late_collision(struct ftmac100_txdes *txdes)
504 return txdes->txdes0 & cpu_to_le32(FTMAC100_TXDES0_TXPKT_LATECOL);
507 static void ftmac100_txdes_set_end_of_ring(struct ftmac100_txdes *txdes)
509 txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_EDOTR);
512 static void ftmac100_txdes_set_first_segment(struct ftmac100_txdes *txdes)
514 txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_FTS);
517 static void ftmac100_txdes_set_last_segment(struct ftmac100_txdes *txdes)
519 txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_LTS);
522 static void ftmac100_txdes_set_txint(struct ftmac100_txdes *txdes)
524 txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_TXIC);
527 static void ftmac100_txdes_set_buffer_size(struct ftmac100_txdes *txdes,
530 txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_TXBUF_SIZE(len));
533 static void ftmac100_txdes_set_dma_addr(struct ftmac100_txdes *txdes,
536 txdes->txdes2 = cpu_to_le32(addr);
539 static dma_addr_t ftmac100_txdes_get_dma_addr(struct ftmac100_txdes *txdes)
541 return le32_to_cpu(txdes->txdes2);
545 * txdes3 is not used by hardware. We use it to keep track of socket buffer.
546 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
548 static void ftmac100_txdes_set_skb(struct ftmac100_txdes *txdes, struct sk_buff *skb)
550 txdes->txdes3 = (unsigned int)skb;
553 static struct sk_buff *ftmac100_txdes_get_skb(struct ftmac100_txdes *txdes)
555 return (struct sk_buff *)txdes->txdes3;
558 /******************************************************************************
559 * internal functions (transmit)
560 *****************************************************************************/
561 static int ftmac100_next_tx_pointer(int pointer)
563 return (pointer + 1) & (TX_QUEUE_ENTRIES - 1);
566 static void ftmac100_tx_pointer_advance(struct ftmac100 *priv)
568 priv->tx_pointer = ftmac100_next_tx_pointer(priv->tx_pointer);
571 static void ftmac100_tx_clean_pointer_advance(struct ftmac100 *priv)
573 priv->tx_clean_pointer = ftmac100_next_tx_pointer(priv->tx_clean_pointer);
576 static struct ftmac100_txdes *ftmac100_current_txdes(struct ftmac100 *priv)
578 return &priv->descs->txdes[priv->tx_pointer];
581 static struct ftmac100_txdes *ftmac100_current_clean_txdes(struct ftmac100 *priv)
583 return &priv->descs->txdes[priv->tx_clean_pointer];
586 static bool ftmac100_tx_complete_packet(struct ftmac100 *priv)
588 struct net_device *netdev = priv->netdev;
589 struct ftmac100_txdes *txdes;
593 if (priv->tx_pending == 0)
596 txdes = ftmac100_current_clean_txdes(priv);
598 if (ftmac100_txdes_owned_by_dma(txdes))
601 skb = ftmac100_txdes_get_skb(txdes);
602 map = ftmac100_txdes_get_dma_addr(txdes);
604 if (unlikely(ftmac100_txdes_excessive_collision(txdes) ||
605 ftmac100_txdes_late_collision(txdes))) {
607 * packet transmitted to ethernet lost due to late collision
608 * or excessive collision
610 netdev->stats.tx_aborted_errors++;
612 netdev->stats.tx_packets++;
613 netdev->stats.tx_bytes += skb->len;
616 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
619 ftmac100_txdes_reset(txdes);
621 ftmac100_tx_clean_pointer_advance(priv);
623 spin_lock(&priv->tx_lock);
625 spin_unlock(&priv->tx_lock);
626 netif_wake_queue(netdev);
631 static void ftmac100_tx_complete(struct ftmac100 *priv)
633 while (ftmac100_tx_complete_packet(priv))
637 static netdev_tx_t ftmac100_xmit(struct ftmac100 *priv, struct sk_buff *skb,
640 struct net_device *netdev = priv->netdev;
641 struct ftmac100_txdes *txdes;
642 unsigned int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
644 txdes = ftmac100_current_txdes(priv);
645 ftmac100_tx_pointer_advance(priv);
647 /* setup TX descriptor */
648 ftmac100_txdes_set_skb(txdes, skb);
649 ftmac100_txdes_set_dma_addr(txdes, map);
651 ftmac100_txdes_set_first_segment(txdes);
652 ftmac100_txdes_set_last_segment(txdes);
653 ftmac100_txdes_set_txint(txdes);
654 ftmac100_txdes_set_buffer_size(txdes, len);
656 spin_lock(&priv->tx_lock);
658 if (priv->tx_pending == TX_QUEUE_ENTRIES)
659 netif_stop_queue(netdev);
662 ftmac100_txdes_set_dma_own(txdes);
663 spin_unlock(&priv->tx_lock);
665 ftmac100_txdma_start_polling(priv);
669 /******************************************************************************
670 * internal functions (buffer)
671 *****************************************************************************/
672 static int ftmac100_alloc_rx_page(struct ftmac100 *priv,
673 struct ftmac100_rxdes *rxdes, gfp_t gfp)
675 struct net_device *netdev = priv->netdev;
679 page = alloc_page(gfp);
682 netdev_err(netdev, "failed to allocate rx page\n");
686 map = dma_map_page(priv->dev, page, 0, RX_BUF_SIZE, DMA_FROM_DEVICE);
687 if (unlikely(dma_mapping_error(priv->dev, map))) {
689 netdev_err(netdev, "failed to map rx page\n");
694 ftmac100_rxdes_set_page(rxdes, page);
695 ftmac100_rxdes_set_dma_addr(rxdes, map);
696 ftmac100_rxdes_set_buffer_size(rxdes, RX_BUF_SIZE);
697 ftmac100_rxdes_set_dma_own(rxdes);
701 static void ftmac100_free_buffers(struct ftmac100 *priv)
705 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
706 struct ftmac100_rxdes *rxdes = &priv->descs->rxdes[i];
707 struct page *page = ftmac100_rxdes_get_page(rxdes);
708 dma_addr_t map = ftmac100_rxdes_get_dma_addr(rxdes);
713 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
717 for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
718 struct ftmac100_txdes *txdes = &priv->descs->txdes[i];
719 struct sk_buff *skb = ftmac100_txdes_get_skb(txdes);
720 dma_addr_t map = ftmac100_txdes_get_dma_addr(txdes);
725 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
729 dma_free_coherent(priv->dev, sizeof(struct ftmac100_descs),
730 priv->descs, priv->descs_dma_addr);
733 static int ftmac100_alloc_buffers(struct ftmac100 *priv)
737 priv->descs = dma_zalloc_coherent(priv->dev,
738 sizeof(struct ftmac100_descs),
739 &priv->descs_dma_addr,
744 /* initialize RX ring */
745 ftmac100_rxdes_set_end_of_ring(&priv->descs->rxdes[RX_QUEUE_ENTRIES - 1]);
747 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
748 struct ftmac100_rxdes *rxdes = &priv->descs->rxdes[i];
750 if (ftmac100_alloc_rx_page(priv, rxdes, GFP_KERNEL))
754 /* initialize TX ring */
755 ftmac100_txdes_set_end_of_ring(&priv->descs->txdes[TX_QUEUE_ENTRIES - 1]);
759 ftmac100_free_buffers(priv);
763 /******************************************************************************
764 * struct mii_if_info functions
765 *****************************************************************************/
766 static int ftmac100_mdio_read(struct net_device *netdev, int phy_id, int reg)
768 struct ftmac100 *priv = netdev_priv(netdev);
772 phycr = FTMAC100_PHYCR_PHYAD(phy_id) |
773 FTMAC100_PHYCR_REGAD(reg) |
774 FTMAC100_PHYCR_MIIRD;
776 iowrite32(phycr, priv->base + FTMAC100_OFFSET_PHYCR);
778 for (i = 0; i < 10; i++) {
779 phycr = ioread32(priv->base + FTMAC100_OFFSET_PHYCR);
781 if ((phycr & FTMAC100_PHYCR_MIIRD) == 0)
782 return phycr & FTMAC100_PHYCR_MIIRDATA;
787 netdev_err(netdev, "mdio read timed out\n");
791 static void ftmac100_mdio_write(struct net_device *netdev, int phy_id, int reg,
794 struct ftmac100 *priv = netdev_priv(netdev);
798 phycr = FTMAC100_PHYCR_PHYAD(phy_id) |
799 FTMAC100_PHYCR_REGAD(reg) |
800 FTMAC100_PHYCR_MIIWR;
802 data = FTMAC100_PHYWDATA_MIIWDATA(data);
804 iowrite32(data, priv->base + FTMAC100_OFFSET_PHYWDATA);
805 iowrite32(phycr, priv->base + FTMAC100_OFFSET_PHYCR);
807 for (i = 0; i < 10; i++) {
808 phycr = ioread32(priv->base + FTMAC100_OFFSET_PHYCR);
810 if ((phycr & FTMAC100_PHYCR_MIIWR) == 0)
816 netdev_err(netdev, "mdio write timed out\n");
819 /******************************************************************************
820 * struct ethtool_ops functions
821 *****************************************************************************/
822 static void ftmac100_get_drvinfo(struct net_device *netdev,
823 struct ethtool_drvinfo *info)
825 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
826 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
827 strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
830 static int ftmac100_get_link_ksettings(struct net_device *netdev,
831 struct ethtool_link_ksettings *cmd)
833 struct ftmac100 *priv = netdev_priv(netdev);
835 mii_ethtool_get_link_ksettings(&priv->mii, cmd);
840 static int ftmac100_set_link_ksettings(struct net_device *netdev,
841 const struct ethtool_link_ksettings *cmd)
843 struct ftmac100 *priv = netdev_priv(netdev);
844 return mii_ethtool_set_link_ksettings(&priv->mii, cmd);
847 static int ftmac100_nway_reset(struct net_device *netdev)
849 struct ftmac100 *priv = netdev_priv(netdev);
850 return mii_nway_restart(&priv->mii);
853 static u32 ftmac100_get_link(struct net_device *netdev)
855 struct ftmac100 *priv = netdev_priv(netdev);
856 return mii_link_ok(&priv->mii);
859 static const struct ethtool_ops ftmac100_ethtool_ops = {
860 .get_drvinfo = ftmac100_get_drvinfo,
861 .nway_reset = ftmac100_nway_reset,
862 .get_link = ftmac100_get_link,
863 .get_link_ksettings = ftmac100_get_link_ksettings,
864 .set_link_ksettings = ftmac100_set_link_ksettings,
867 /******************************************************************************
869 *****************************************************************************/
870 static irqreturn_t ftmac100_interrupt(int irq, void *dev_id)
872 struct net_device *netdev = dev_id;
873 struct ftmac100 *priv = netdev_priv(netdev);
875 if (likely(netif_running(netdev))) {
876 /* Disable interrupts for polling */
877 ftmac100_disable_all_int(priv);
878 napi_schedule(&priv->napi);
884 /******************************************************************************
885 * struct napi_struct functions
886 *****************************************************************************/
887 static int ftmac100_poll(struct napi_struct *napi, int budget)
889 struct ftmac100 *priv = container_of(napi, struct ftmac100, napi);
890 struct net_device *netdev = priv->netdev;
892 bool completed = true;
895 status = ioread32(priv->base + FTMAC100_OFFSET_ISR);
897 if (status & (FTMAC100_INT_RPKT_FINISH | FTMAC100_INT_NORXBUF)) {
899 * FTMAC100_INT_RPKT_FINISH:
900 * RX DMA has received packets into RX buffer successfully
902 * FTMAC100_INT_NORXBUF:
903 * RX buffer unavailable
908 retry = ftmac100_rx_packet(priv, &rx);
909 } while (retry && rx < budget);
911 if (retry && rx == budget)
915 if (status & (FTMAC100_INT_XPKT_OK | FTMAC100_INT_XPKT_LOST)) {
917 * FTMAC100_INT_XPKT_OK:
918 * packet transmitted to ethernet successfully
920 * FTMAC100_INT_XPKT_LOST:
921 * packet transmitted to ethernet lost due to late
922 * collision or excessive collision
924 ftmac100_tx_complete(priv);
927 if (status & (FTMAC100_INT_NORXBUF | FTMAC100_INT_RPKT_LOST |
928 FTMAC100_INT_AHB_ERR | FTMAC100_INT_PHYSTS_CHG)) {
930 netdev_info(netdev, "[ISR] = 0x%x: %s%s%s%s\n", status,
931 status & FTMAC100_INT_NORXBUF ? "NORXBUF " : "",
932 status & FTMAC100_INT_RPKT_LOST ? "RPKT_LOST " : "",
933 status & FTMAC100_INT_AHB_ERR ? "AHB_ERR " : "",
934 status & FTMAC100_INT_PHYSTS_CHG ? "PHYSTS_CHG" : "");
936 if (status & FTMAC100_INT_NORXBUF) {
937 /* RX buffer unavailable */
938 netdev->stats.rx_over_errors++;
941 if (status & FTMAC100_INT_RPKT_LOST) {
942 /* received packet lost due to RX FIFO full */
943 netdev->stats.rx_fifo_errors++;
946 if (status & FTMAC100_INT_PHYSTS_CHG) {
947 /* PHY link status change */
948 mii_check_link(&priv->mii);
955 ftmac100_enable_all_int(priv);
961 /******************************************************************************
962 * struct net_device_ops functions
963 *****************************************************************************/
964 static int ftmac100_open(struct net_device *netdev)
966 struct ftmac100 *priv = netdev_priv(netdev);
969 err = ftmac100_alloc_buffers(priv);
971 netdev_err(netdev, "failed to allocate buffers\n");
975 err = request_irq(priv->irq, ftmac100_interrupt, 0, netdev->name, netdev);
977 netdev_err(netdev, "failed to request irq %d\n", priv->irq);
981 priv->rx_pointer = 0;
982 priv->tx_clean_pointer = 0;
983 priv->tx_pointer = 0;
984 priv->tx_pending = 0;
986 err = ftmac100_start_hw(priv);
990 napi_enable(&priv->napi);
991 netif_start_queue(netdev);
993 ftmac100_enable_all_int(priv);
998 free_irq(priv->irq, netdev);
1000 ftmac100_free_buffers(priv);
1005 static int ftmac100_stop(struct net_device *netdev)
1007 struct ftmac100 *priv = netdev_priv(netdev);
1009 ftmac100_disable_all_int(priv);
1010 netif_stop_queue(netdev);
1011 napi_disable(&priv->napi);
1012 ftmac100_stop_hw(priv);
1013 free_irq(priv->irq, netdev);
1014 ftmac100_free_buffers(priv);
1020 ftmac100_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
1022 struct ftmac100 *priv = netdev_priv(netdev);
1025 if (unlikely(skb->len > MAX_PKT_SIZE)) {
1026 if (net_ratelimit())
1027 netdev_dbg(netdev, "tx packet too big\n");
1029 netdev->stats.tx_dropped++;
1031 return NETDEV_TX_OK;
1034 map = dma_map_single(priv->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
1035 if (unlikely(dma_mapping_error(priv->dev, map))) {
1037 if (net_ratelimit())
1038 netdev_err(netdev, "map socket buffer failed\n");
1040 netdev->stats.tx_dropped++;
1042 return NETDEV_TX_OK;
1045 return ftmac100_xmit(priv, skb, map);
1049 static int ftmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1051 struct ftmac100 *priv = netdev_priv(netdev);
1052 struct mii_ioctl_data *data = if_mii(ifr);
1054 return generic_mii_ioctl(&priv->mii, data, cmd, NULL);
1057 static const struct net_device_ops ftmac100_netdev_ops = {
1058 .ndo_open = ftmac100_open,
1059 .ndo_stop = ftmac100_stop,
1060 .ndo_start_xmit = ftmac100_hard_start_xmit,
1061 .ndo_set_mac_address = eth_mac_addr,
1062 .ndo_validate_addr = eth_validate_addr,
1063 .ndo_do_ioctl = ftmac100_do_ioctl,
1066 /******************************************************************************
1067 * struct platform_driver functions
1068 *****************************************************************************/
1069 static int ftmac100_probe(struct platform_device *pdev)
1071 struct resource *res;
1073 struct net_device *netdev;
1074 struct ftmac100 *priv;
1080 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1084 irq = platform_get_irq(pdev, 0);
1088 /* setup net_device */
1089 netdev = alloc_etherdev(sizeof(*priv));
1092 goto err_alloc_etherdev;
1095 SET_NETDEV_DEV(netdev, &pdev->dev);
1096 netdev->ethtool_ops = &ftmac100_ethtool_ops;
1097 netdev->netdev_ops = &ftmac100_netdev_ops;
1099 platform_set_drvdata(pdev, netdev);
1101 /* setup private data */
1102 priv = netdev_priv(netdev);
1103 priv->netdev = netdev;
1104 priv->dev = &pdev->dev;
1106 spin_lock_init(&priv->tx_lock);
1108 /* initialize NAPI */
1109 netif_napi_add(netdev, &priv->napi, ftmac100_poll, 64);
1112 priv->res = request_mem_region(res->start, resource_size(res),
1113 dev_name(&pdev->dev));
1115 dev_err(&pdev->dev, "Could not reserve memory region\n");
1120 priv->base = ioremap(res->start, resource_size(res));
1122 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1129 /* initialize struct mii_if_info */
1130 priv->mii.phy_id = 0;
1131 priv->mii.phy_id_mask = 0x1f;
1132 priv->mii.reg_num_mask = 0x1f;
1133 priv->mii.dev = netdev;
1134 priv->mii.mdio_read = ftmac100_mdio_read;
1135 priv->mii.mdio_write = ftmac100_mdio_write;
1137 /* register network device */
1138 err = register_netdev(netdev);
1140 dev_err(&pdev->dev, "Failed to register netdev\n");
1141 goto err_register_netdev;
1144 netdev_info(netdev, "irq %d, mapped at %p\n", priv->irq, priv->base);
1146 if (!is_valid_ether_addr(netdev->dev_addr)) {
1147 eth_hw_addr_random(netdev);
1148 netdev_info(netdev, "generated random MAC address %pM\n",
1154 err_register_netdev:
1155 iounmap(priv->base);
1157 release_resource(priv->res);
1159 netif_napi_del(&priv->napi);
1160 free_netdev(netdev);
1165 static int ftmac100_remove(struct platform_device *pdev)
1167 struct net_device *netdev;
1168 struct ftmac100 *priv;
1170 netdev = platform_get_drvdata(pdev);
1171 priv = netdev_priv(netdev);
1173 unregister_netdev(netdev);
1175 iounmap(priv->base);
1176 release_resource(priv->res);
1178 netif_napi_del(&priv->napi);
1179 free_netdev(netdev);
1183 static const struct of_device_id ftmac100_of_ids[] = {
1184 { .compatible = "andestech,atmac100" },
1188 static struct platform_driver ftmac100_driver = {
1189 .probe = ftmac100_probe,
1190 .remove = ftmac100_remove,
1193 .of_match_table = ftmac100_of_ids
1197 /******************************************************************************
1198 * initialization / finalization
1199 *****************************************************************************/
1200 static int __init ftmac100_init(void)
1202 pr_info("Loading version " DRV_VERSION " ...\n");
1203 return platform_driver_register(&ftmac100_driver);
1206 static void __exit ftmac100_exit(void)
1208 platform_driver_unregister(&ftmac100_driver);
1211 module_init(ftmac100_init);
1212 module_exit(ftmac100_exit);
1215 MODULE_DESCRIPTION("FTMAC100 driver");
1216 MODULE_LICENSE("GPL");
1217 MODULE_DEVICE_TABLE(of, ftmac100_of_ids);