gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / ethernet / faraday / ftmac100.c
blob32cf54f0e35bb005d1f99a9d9e406951ec4b5b5a
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Faraday FTMAC100 10/100 Ethernet
5 * (C) Copyright 2009-2011 Faraday Technology
6 * Po-Yu Chuang <ratbert@faraday-tech.com>
7 */
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>
16 #include <linux/io.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>
23 #include "ftmac100.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
35 #endif
37 #if RX_BUF_SIZE > 0x7ff || RX_BUF_SIZE > PAGE_SIZE
38 #error invalid RX_BUF_SIZE
39 #endif
41 /******************************************************************************
42 * private data
43 *****************************************************************************/
44 struct ftmac100_descs {
45 struct ftmac100_rxdes rxdes[RX_QUEUE_ENTRIES];
46 struct ftmac100_txdes txdes[TX_QUEUE_ENTRIES];
49 struct ftmac100 {
50 struct resource *res;
51 void __iomem *base;
52 int irq;
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;
62 spinlock_t tx_lock;
64 struct net_device *netdev;
65 struct device *dev;
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;
115 int i;
117 /* NOTE: reset clears all registers */
118 iowrite32(FTMAC100_MACCR_SW_RST, priv->base + FTMAC100_OFFSET_MACCR);
120 for (i = 0; i < 5; i++) {
121 unsigned int maccr;
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.
130 udelay(500);
131 return 0;
134 udelay(1000);
137 netdev_err(netdev, "software reset failed\n");
138 return -EIO;
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))
164 return -EIO;
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);
179 return 0;
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,
247 unsigned int size)
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,
259 dma_addr_t addr)
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))
308 return rxdes;
310 ftmac100_rxdes_set_dma_own(rxdes);
311 ftmac100_rx_pointer_advance(priv);
312 rxdes = ftmac100_current_rxdes(priv);
315 return NULL;
318 static bool ftmac100_rx_packet_error(struct ftmac100 *priv,
319 struct ftmac100_rxdes *rxdes)
321 struct net_device *netdev = priv->netdev;
322 bool error = false;
324 if (unlikely(ftmac100_rxdes_rx_error(rxdes))) {
325 if (net_ratelimit())
326 netdev_info(netdev, "rx err\n");
328 netdev->stats.rx_errors++;
329 error = true;
332 if (unlikely(ftmac100_rxdes_crc_error(rxdes))) {
333 if (net_ratelimit())
334 netdev_info(netdev, "rx crc err\n");
336 netdev->stats.rx_crc_errors++;
337 error = true;
340 if (unlikely(ftmac100_rxdes_frame_too_long(rxdes))) {
341 if (net_ratelimit())
342 netdev_info(netdev, "rx frame too long\n");
344 netdev->stats.rx_length_errors++;
345 error = true;
346 } else if (unlikely(ftmac100_rxdes_runt(rxdes))) {
347 if (net_ratelimit())
348 netdev_info(netdev, "rx runt\n");
350 netdev->stats.rx_length_errors++;
351 error = true;
352 } else if (unlikely(ftmac100_rxdes_odd_nibble(rxdes))) {
353 if (net_ratelimit())
354 netdev_info(netdev, "rx odd nibble\n");
356 netdev->stats.rx_length_errors++;
357 error = true;
360 return error;
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);
367 bool done = false;
369 if (net_ratelimit())
370 netdev_dbg(netdev, "drop packet %p\n", rxdes);
372 do {
373 if (ftmac100_rxdes_last_segment(rxdes))
374 done = true;
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;
388 struct sk_buff *skb;
389 struct page *page;
390 dma_addr_t map;
391 int length;
392 bool ret;
394 rxdes = ftmac100_rx_locate_first_segment(priv);
395 if (!rxdes)
396 return false;
398 if (unlikely(ftmac100_rx_packet_error(priv, rxdes))) {
399 ftmac100_rx_drop_packet(priv);
400 return true;
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);
408 BUG_ON(!ret);
410 /* start processing */
411 skb = netdev_alloc_skb_ip_align(netdev, 128);
412 if (unlikely(!skb)) {
413 if (net_ratelimit())
414 netdev_err(netdev, "rx skb alloc failed\n");
416 ftmac100_rx_drop_packet(priv);
417 return true;
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);
429 skb->len += length;
430 skb->data_len += length;
432 if (length > 128) {
433 skb->truesize += PAGE_SIZE;
434 /* We pull the minimum amount into linear part */
435 __pskb_pull_tail(skb, ETH_HLEN);
436 } else {
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);
452 (*processed)++;
453 return true;
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 */
462 txdes->txdes0 = 0;
463 txdes->txdes1 &= cpu_to_le32(FTMAC100_TXDES1_EDOTR);
464 txdes->txdes2 = 0;
465 txdes->txdes3 = 0;
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
477 * descriptor fields.
479 wmb();
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,
514 unsigned int len)
516 txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_TXBUF_SIZE(len));
519 static void ftmac100_txdes_set_dma_addr(struct ftmac100_txdes *txdes,
520 dma_addr_t addr)
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;
576 struct sk_buff *skb;
577 dma_addr_t map;
579 if (priv->tx_pending == 0)
580 return false;
582 txdes = ftmac100_current_clean_txdes(priv);
584 if (ftmac100_txdes_owned_by_dma(txdes))
585 return false;
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++;
597 } else {
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);
603 dev_kfree_skb(skb);
605 ftmac100_txdes_reset(txdes);
607 ftmac100_tx_clean_pointer_advance(priv);
609 spin_lock(&priv->tx_lock);
610 priv->tx_pending--;
611 spin_unlock(&priv->tx_lock);
612 netif_wake_queue(netdev);
614 return true;
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,
624 dma_addr_t map)
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);
643 priv->tx_pending++;
644 if (priv->tx_pending == TX_QUEUE_ENTRIES)
645 netif_stop_queue(netdev);
647 /* start transmit */
648 ftmac100_txdes_set_dma_own(txdes);
649 spin_unlock(&priv->tx_lock);
651 ftmac100_txdma_start_polling(priv);
652 return NETDEV_TX_OK;
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;
662 struct page *page;
663 dma_addr_t map;
665 page = alloc_page(gfp);
666 if (!page) {
667 if (net_ratelimit())
668 netdev_err(netdev, "failed to allocate rx page\n");
669 return -ENOMEM;
672 map = dma_map_page(priv->dev, page, 0, RX_BUF_SIZE, DMA_FROM_DEVICE);
673 if (unlikely(dma_mapping_error(priv->dev, map))) {
674 if (net_ratelimit())
675 netdev_err(netdev, "failed to map rx page\n");
676 __free_page(page);
677 return -ENOMEM;
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);
684 return 0;
687 static void ftmac100_free_buffers(struct ftmac100 *priv)
689 int i;
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);
696 if (!page)
697 continue;
699 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
700 __free_page(page);
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);
708 if (!skb)
709 continue;
711 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
712 dev_kfree_skb(skb);
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)
721 int i;
723 priv->descs = dma_alloc_coherent(priv->dev,
724 sizeof(struct ftmac100_descs),
725 &priv->descs_dma_addr, GFP_KERNEL);
726 if (!priv->descs)
727 return -ENOMEM;
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))
736 goto err;
739 /* initialize TX ring */
740 ftmac100_txdes_set_end_of_ring(&priv->descs->txdes[TX_QUEUE_ENTRIES - 1]);
741 return 0;
743 err:
744 ftmac100_free_buffers(priv);
745 return -ENOMEM;
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);
754 unsigned int phycr;
755 int i;
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;
769 udelay(100);
772 netdev_err(netdev, "mdio read timed out\n");
773 return 0;
776 static void ftmac100_mdio_write(struct net_device *netdev, int phy_id, int reg,
777 int data)
779 struct ftmac100 *priv = netdev_priv(netdev);
780 unsigned int phycr;
781 int i;
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)
796 return;
798 udelay(100);
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);
821 return 0;
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 /******************************************************************************
852 * interrupt handler
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);
864 return IRQ_HANDLED;
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;
874 unsigned int status;
875 bool completed = true;
876 int rx = 0;
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
888 bool retry;
890 do {
891 retry = ftmac100_rx_packet(priv, &rx);
892 } while (retry && rx < budget);
894 if (retry && rx == budget)
895 completed = false;
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)) {
912 if (net_ratelimit())
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);
935 if (completed) {
936 /* stop polling */
937 napi_complete(napi);
938 ftmac100_enable_all_int(priv);
941 return rx;
944 /******************************************************************************
945 * struct net_device_ops functions
946 *****************************************************************************/
947 static int ftmac100_open(struct net_device *netdev)
949 struct ftmac100 *priv = netdev_priv(netdev);
950 int err;
952 err = ftmac100_alloc_buffers(priv);
953 if (err) {
954 netdev_err(netdev, "failed to allocate buffers\n");
955 goto err_alloc;
958 err = request_irq(priv->irq, ftmac100_interrupt, 0, netdev->name, netdev);
959 if (err) {
960 netdev_err(netdev, "failed to request irq %d\n", priv->irq);
961 goto err_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);
970 if (err)
971 goto err_hw;
973 napi_enable(&priv->napi);
974 netif_start_queue(netdev);
976 ftmac100_enable_all_int(priv);
978 return 0;
980 err_hw:
981 free_irq(priv->irq, netdev);
982 err_irq:
983 ftmac100_free_buffers(priv);
984 err_alloc:
985 return err;
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);
999 return 0;
1002 static netdev_tx_t
1003 ftmac100_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
1005 struct ftmac100 *priv = netdev_priv(netdev);
1006 dma_addr_t map;
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++;
1013 dev_kfree_skb(skb);
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))) {
1019 /* drop packet */
1020 if (net_ratelimit())
1021 netdev_err(netdev, "map socket buffer failed\n");
1023 netdev->stats.tx_dropped++;
1024 dev_kfree_skb(skb);
1025 return NETDEV_TX_OK;
1028 return ftmac100_xmit(priv, skb, map);
1031 /* optional */
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_do_ioctl = ftmac100_do_ioctl,
1049 /******************************************************************************
1050 * struct platform_driver functions
1051 *****************************************************************************/
1052 static int ftmac100_probe(struct platform_device *pdev)
1054 struct resource *res;
1055 int irq;
1056 struct net_device *netdev;
1057 struct ftmac100 *priv;
1058 int err;
1060 if (!pdev)
1061 return -ENODEV;
1063 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1064 if (!res)
1065 return -ENXIO;
1067 irq = platform_get_irq(pdev, 0);
1068 if (irq < 0)
1069 return irq;
1071 /* setup net_device */
1072 netdev = alloc_etherdev(sizeof(*priv));
1073 if (!netdev) {
1074 err = -ENOMEM;
1075 goto err_alloc_etherdev;
1078 SET_NETDEV_DEV(netdev, &pdev->dev);
1079 netdev->ethtool_ops = &ftmac100_ethtool_ops;
1080 netdev->netdev_ops = &ftmac100_netdev_ops;
1082 platform_set_drvdata(pdev, netdev);
1084 /* setup private data */
1085 priv = netdev_priv(netdev);
1086 priv->netdev = netdev;
1087 priv->dev = &pdev->dev;
1089 spin_lock_init(&priv->tx_lock);
1091 /* initialize NAPI */
1092 netif_napi_add(netdev, &priv->napi, ftmac100_poll, 64);
1094 /* map io memory */
1095 priv->res = request_mem_region(res->start, resource_size(res),
1096 dev_name(&pdev->dev));
1097 if (!priv->res) {
1098 dev_err(&pdev->dev, "Could not reserve memory region\n");
1099 err = -ENOMEM;
1100 goto err_req_mem;
1103 priv->base = ioremap(res->start, resource_size(res));
1104 if (!priv->base) {
1105 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1106 err = -EIO;
1107 goto err_ioremap;
1110 priv->irq = irq;
1112 /* initialize struct mii_if_info */
1113 priv->mii.phy_id = 0;
1114 priv->mii.phy_id_mask = 0x1f;
1115 priv->mii.reg_num_mask = 0x1f;
1116 priv->mii.dev = netdev;
1117 priv->mii.mdio_read = ftmac100_mdio_read;
1118 priv->mii.mdio_write = ftmac100_mdio_write;
1120 /* register network device */
1121 err = register_netdev(netdev);
1122 if (err) {
1123 dev_err(&pdev->dev, "Failed to register netdev\n");
1124 goto err_register_netdev;
1127 netdev_info(netdev, "irq %d, mapped at %p\n", priv->irq, priv->base);
1129 if (!is_valid_ether_addr(netdev->dev_addr)) {
1130 eth_hw_addr_random(netdev);
1131 netdev_info(netdev, "generated random MAC address %pM\n",
1132 netdev->dev_addr);
1135 return 0;
1137 err_register_netdev:
1138 iounmap(priv->base);
1139 err_ioremap:
1140 release_resource(priv->res);
1141 err_req_mem:
1142 netif_napi_del(&priv->napi);
1143 free_netdev(netdev);
1144 err_alloc_etherdev:
1145 return err;
1148 static int ftmac100_remove(struct platform_device *pdev)
1150 struct net_device *netdev;
1151 struct ftmac100 *priv;
1153 netdev = platform_get_drvdata(pdev);
1154 priv = netdev_priv(netdev);
1156 unregister_netdev(netdev);
1158 iounmap(priv->base);
1159 release_resource(priv->res);
1161 netif_napi_del(&priv->napi);
1162 free_netdev(netdev);
1163 return 0;
1166 static const struct of_device_id ftmac100_of_ids[] = {
1167 { .compatible = "andestech,atmac100" },
1171 static struct platform_driver ftmac100_driver = {
1172 .probe = ftmac100_probe,
1173 .remove = ftmac100_remove,
1174 .driver = {
1175 .name = DRV_NAME,
1176 .of_match_table = ftmac100_of_ids
1180 /******************************************************************************
1181 * initialization / finalization
1182 *****************************************************************************/
1183 static int __init ftmac100_init(void)
1185 return platform_driver_register(&ftmac100_driver);
1188 static void __exit ftmac100_exit(void)
1190 platform_driver_unregister(&ftmac100_driver);
1193 module_init(ftmac100_init);
1194 module_exit(ftmac100_exit);
1196 MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1197 MODULE_DESCRIPTION("FTMAC100 driver");
1198 MODULE_LICENSE("GPL");
1199 MODULE_DEVICE_TABLE(of, ftmac100_of_ids);