2 * Faraday FTGMAC100 Gigabit Ethernet
4 * (C) Copyright 2009-2011 Faraday Technology
5 * Po-Yu Chuang <ratbert@faraday-tech.com>
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/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/netdevice.h>
32 #include <linux/phy.h>
33 #include <linux/platform_device.h>
34 #include <linux/property.h>
35 #include <linux/crc32.h>
36 #include <linux/if_vlan.h>
37 #include <linux/of_net.h>
41 #include "ftgmac100.h"
43 #define DRV_NAME "ftgmac100"
44 #define DRV_VERSION "0.7"
46 /* Arbitrary values, I am not sure the HW has limits */
47 #define MAX_RX_QUEUE_ENTRIES 1024
48 #define MAX_TX_QUEUE_ENTRIES 1024
49 #define MIN_RX_QUEUE_ENTRIES 32
50 #define MIN_TX_QUEUE_ENTRIES 32
53 #define DEF_RX_QUEUE_ENTRIES 128
54 #define DEF_TX_QUEUE_ENTRIES 128
56 #define MAX_PKT_SIZE 1536
57 #define RX_BUF_SIZE MAX_PKT_SIZE /* must be smaller than 0x3fff */
59 /* Min number of tx ring entries before stopping queue */
60 #define TX_THRESHOLD (MAX_SKB_FRAGS + 1)
68 unsigned int rx_q_entries;
69 struct ftgmac100_rxdes *rxdes;
71 struct sk_buff **rx_skbs;
72 unsigned int rx_pointer;
73 u32 rxdes0_edorr_mask;
76 unsigned int tx_q_entries;
77 struct ftgmac100_txdes *txdes;
79 struct sk_buff **tx_skbs;
80 unsigned int tx_clean_pointer;
81 unsigned int tx_pointer;
82 u32 txdes0_edotr_mask;
84 /* Used to signal the reset task of ring change request */
85 unsigned int new_rx_q_entries;
86 unsigned int new_tx_q_entries;
88 /* Scratch page to use when rx skb alloc fails */
90 dma_addr_t rx_scratch_dma;
92 /* Component structures */
93 struct net_device *netdev;
95 struct ncsi_dev *ndev;
96 struct napi_struct napi;
97 struct work_struct reset_task;
98 struct mii_bus *mii_bus;
100 /* Link management */
105 /* Multicast filter settings */
109 /* Flow control settings */
115 bool need_mac_restart;
119 static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr)
121 struct net_device *netdev = priv->netdev;
124 /* NOTE: reset clears all registers */
125 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
126 iowrite32(maccr | FTGMAC100_MACCR_SW_RST,
127 priv->base + FTGMAC100_OFFSET_MACCR);
128 for (i = 0; i < 200; i++) {
131 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
132 if (!(maccr & FTGMAC100_MACCR_SW_RST))
138 netdev_err(netdev, "Hardware reset failed\n");
142 static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv)
146 switch (priv->cur_speed) {
148 case 0: /* no link */
152 maccr |= FTGMAC100_MACCR_FAST_MODE;
156 maccr |= FTGMAC100_MACCR_GIGA_MODE;
159 netdev_err(priv->netdev, "Unknown speed %d !\n",
164 /* (Re)initialize the queue pointers */
165 priv->rx_pointer = 0;
166 priv->tx_clean_pointer = 0;
167 priv->tx_pointer = 0;
169 /* The doc says reset twice with 10us interval */
170 if (ftgmac100_reset_mac(priv, maccr))
172 usleep_range(10, 1000);
173 return ftgmac100_reset_mac(priv, maccr);
176 static void ftgmac100_write_mac_addr(struct ftgmac100 *priv, const u8 *mac)
178 unsigned int maddr = mac[0] << 8 | mac[1];
179 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
181 iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR);
182 iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
185 static void ftgmac100_initial_mac(struct ftgmac100 *priv)
192 addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
194 ether_addr_copy(priv->netdev->dev_addr, mac);
195 dev_info(priv->dev, "Read MAC address %pM from device tree\n",
200 m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
201 l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
203 mac[0] = (m >> 8) & 0xff;
205 mac[2] = (l >> 24) & 0xff;
206 mac[3] = (l >> 16) & 0xff;
207 mac[4] = (l >> 8) & 0xff;
210 if (is_valid_ether_addr(mac)) {
211 ether_addr_copy(priv->netdev->dev_addr, mac);
212 dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
214 eth_hw_addr_random(priv->netdev);
215 dev_info(priv->dev, "Generated random MAC address %pM\n",
216 priv->netdev->dev_addr);
220 static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
224 ret = eth_prepare_mac_addr_change(dev, p);
228 eth_commit_mac_addr_change(dev, p);
229 ftgmac100_write_mac_addr(netdev_priv(dev), dev->dev_addr);
234 static void ftgmac100_config_pause(struct ftgmac100 *priv)
236 u32 fcr = FTGMAC100_FCR_PAUSE_TIME(16);
238 /* Throttle tx queue when receiving pause frames */
240 fcr |= FTGMAC100_FCR_FC_EN;
242 /* Enables sending pause frames when the RX queue is past a
246 fcr |= FTGMAC100_FCR_FCTHR_EN;
248 iowrite32(fcr, priv->base + FTGMAC100_OFFSET_FCR);
251 static void ftgmac100_init_hw(struct ftgmac100 *priv)
253 u32 reg, rfifo_sz, tfifo_sz;
255 /* Clear stale interrupts */
256 reg = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
257 iowrite32(reg, priv->base + FTGMAC100_OFFSET_ISR);
259 /* Setup RX ring buffer base */
260 iowrite32(priv->rxdes_dma, priv->base + FTGMAC100_OFFSET_RXR_BADR);
262 /* Setup TX ring buffer base */
263 iowrite32(priv->txdes_dma, priv->base + FTGMAC100_OFFSET_NPTXR_BADR);
265 /* Configure RX buffer size */
266 iowrite32(FTGMAC100_RBSR_SIZE(RX_BUF_SIZE),
267 priv->base + FTGMAC100_OFFSET_RBSR);
269 /* Set RX descriptor autopoll */
270 iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1),
271 priv->base + FTGMAC100_OFFSET_APTC);
273 /* Write MAC address */
274 ftgmac100_write_mac_addr(priv, priv->netdev->dev_addr);
276 /* Write multicast filter */
277 iowrite32(priv->maht0, priv->base + FTGMAC100_OFFSET_MAHT0);
278 iowrite32(priv->maht1, priv->base + FTGMAC100_OFFSET_MAHT1);
280 /* Configure descriptor sizes and increase burst sizes according
281 * to values in Aspeed SDK. The FIFO arbitration is enabled and
282 * the thresholds set based on the recommended values in the
283 * AST2400 specification.
285 iowrite32(FTGMAC100_DBLAC_RXDES_SIZE(2) | /* 2*8 bytes RX descs */
286 FTGMAC100_DBLAC_TXDES_SIZE(2) | /* 2*8 bytes TX descs */
287 FTGMAC100_DBLAC_RXBURST_SIZE(3) | /* 512 bytes max RX bursts */
288 FTGMAC100_DBLAC_TXBURST_SIZE(3) | /* 512 bytes max TX bursts */
289 FTGMAC100_DBLAC_RX_THR_EN | /* Enable fifo threshold arb */
290 FTGMAC100_DBLAC_RXFIFO_HTHR(6) | /* 6/8 of FIFO high threshold */
291 FTGMAC100_DBLAC_RXFIFO_LTHR(2), /* 2/8 of FIFO low threshold */
292 priv->base + FTGMAC100_OFFSET_DBLAC);
294 /* Interrupt mitigation configured for 1 interrupt/packet. HW interrupt
295 * mitigation doesn't seem to provide any benefit with NAPI so leave
298 iowrite32(FTGMAC100_ITC_RXINT_THR(1) |
299 FTGMAC100_ITC_TXINT_THR(1),
300 priv->base + FTGMAC100_OFFSET_ITC);
302 /* Configure FIFO sizes in the TPAFCR register */
303 reg = ioread32(priv->base + FTGMAC100_OFFSET_FEAR);
304 rfifo_sz = reg & 0x00000007;
305 tfifo_sz = (reg >> 3) & 0x00000007;
306 reg = ioread32(priv->base + FTGMAC100_OFFSET_TPAFCR);
308 reg |= (tfifo_sz << 27);
309 reg |= (rfifo_sz << 24);
310 iowrite32(reg, priv->base + FTGMAC100_OFFSET_TPAFCR);
313 static void ftgmac100_start_hw(struct ftgmac100 *priv)
315 u32 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
317 /* Keep the original GMAC and FAST bits */
318 maccr &= (FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_GIGA_MODE);
320 /* Add all the main enable bits */
321 maccr |= FTGMAC100_MACCR_TXDMA_EN |
322 FTGMAC100_MACCR_RXDMA_EN |
323 FTGMAC100_MACCR_TXMAC_EN |
324 FTGMAC100_MACCR_RXMAC_EN |
325 FTGMAC100_MACCR_CRC_APD |
326 FTGMAC100_MACCR_PHY_LINK_LEVEL |
327 FTGMAC100_MACCR_RX_RUNT |
328 FTGMAC100_MACCR_RX_BROADPKT;
330 /* Add other bits as needed */
331 if (priv->cur_duplex == DUPLEX_FULL)
332 maccr |= FTGMAC100_MACCR_FULLDUP;
333 if (priv->netdev->flags & IFF_PROMISC)
334 maccr |= FTGMAC100_MACCR_RX_ALL;
335 if (priv->netdev->flags & IFF_ALLMULTI)
336 maccr |= FTGMAC100_MACCR_RX_MULTIPKT;
337 else if (netdev_mc_count(priv->netdev))
338 maccr |= FTGMAC100_MACCR_HT_MULTI_EN;
340 /* Vlan filtering enabled */
341 if (priv->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
342 maccr |= FTGMAC100_MACCR_RM_VLAN;
345 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
348 static void ftgmac100_stop_hw(struct ftgmac100 *priv)
350 iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR);
353 static void ftgmac100_calc_mc_hash(struct ftgmac100 *priv)
355 struct netdev_hw_addr *ha;
359 netdev_for_each_mc_addr(ha, priv->netdev) {
360 u32 crc_val = ether_crc_le(ETH_ALEN, ha->addr);
362 crc_val = (~(crc_val >> 2)) & 0x3f;
364 priv->maht1 |= 1ul << (crc_val - 32);
366 priv->maht0 |= 1ul << (crc_val);
370 static void ftgmac100_set_rx_mode(struct net_device *netdev)
372 struct ftgmac100 *priv = netdev_priv(netdev);
374 /* Setup the hash filter */
375 ftgmac100_calc_mc_hash(priv);
377 /* Interface down ? that's all there is to do */
378 if (!netif_running(netdev))
382 iowrite32(priv->maht0, priv->base + FTGMAC100_OFFSET_MAHT0);
383 iowrite32(priv->maht1, priv->base + FTGMAC100_OFFSET_MAHT1);
385 /* Reconfigure MACCR */
386 ftgmac100_start_hw(priv);
389 static int ftgmac100_alloc_rx_buf(struct ftgmac100 *priv, unsigned int entry,
390 struct ftgmac100_rxdes *rxdes, gfp_t gfp)
392 struct net_device *netdev = priv->netdev;
397 skb = netdev_alloc_skb_ip_align(netdev, RX_BUF_SIZE);
398 if (unlikely(!skb)) {
400 netdev_warn(netdev, "failed to allocate rx skb\n");
402 map = priv->rx_scratch_dma;
404 map = dma_map_single(priv->dev, skb->data, RX_BUF_SIZE,
406 if (unlikely(dma_mapping_error(priv->dev, map))) {
408 netdev_err(netdev, "failed to map rx page\n");
409 dev_kfree_skb_any(skb);
410 map = priv->rx_scratch_dma;
417 priv->rx_skbs[entry] = skb;
419 /* Store DMA address into RX desc */
420 rxdes->rxdes3 = cpu_to_le32(map);
422 /* Ensure the above is ordered vs clearing the OWN bit */
425 /* Clean status (which resets own bit) */
426 if (entry == (priv->rx_q_entries - 1))
427 rxdes->rxdes0 = cpu_to_le32(priv->rxdes0_edorr_mask);
434 static unsigned int ftgmac100_next_rx_pointer(struct ftgmac100 *priv,
435 unsigned int pointer)
437 return (pointer + 1) & (priv->rx_q_entries - 1);
440 static void ftgmac100_rx_packet_error(struct ftgmac100 *priv, u32 status)
442 struct net_device *netdev = priv->netdev;
444 if (status & FTGMAC100_RXDES0_RX_ERR)
445 netdev->stats.rx_errors++;
447 if (status & FTGMAC100_RXDES0_CRC_ERR)
448 netdev->stats.rx_crc_errors++;
450 if (status & (FTGMAC100_RXDES0_FTL |
451 FTGMAC100_RXDES0_RUNT |
452 FTGMAC100_RXDES0_RX_ODD_NB))
453 netdev->stats.rx_length_errors++;
456 static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed)
458 struct net_device *netdev = priv->netdev;
459 struct ftgmac100_rxdes *rxdes;
461 unsigned int pointer, size;
462 u32 status, csum_vlan;
465 /* Grab next RX descriptor */
466 pointer = priv->rx_pointer;
467 rxdes = &priv->rxdes[pointer];
469 /* Grab descriptor status */
470 status = le32_to_cpu(rxdes->rxdes0);
472 /* Do we have a packet ? */
473 if (!(status & FTGMAC100_RXDES0_RXPKT_RDY))
476 /* Order subsequent reads with the test for the ready bit */
479 /* We don't cope with fragmented RX packets */
480 if (unlikely(!(status & FTGMAC100_RXDES0_FRS) ||
481 !(status & FTGMAC100_RXDES0_LRS)))
484 /* Grab received size and csum vlan field in the descriptor */
485 size = status & FTGMAC100_RXDES0_VDBC;
486 csum_vlan = le32_to_cpu(rxdes->rxdes1);
488 /* Any error (other than csum offload) flagged ? */
489 if (unlikely(status & RXDES0_ANY_ERROR)) {
490 /* Correct for incorrect flagging of runt packets
491 * with vlan tags... Just accept a runt packet that
492 * has been flagged as vlan and whose size is at
495 if ((status & FTGMAC100_RXDES0_RUNT) &&
496 (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL) &&
498 status &= ~FTGMAC100_RXDES0_RUNT;
500 /* Any error still in there ? */
501 if (status & RXDES0_ANY_ERROR) {
502 ftgmac100_rx_packet_error(priv, status);
507 /* If the packet had no skb (failed to allocate earlier)
508 * then try to allocate one and skip
510 skb = priv->rx_skbs[pointer];
511 if (!unlikely(skb)) {
512 ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
516 if (unlikely(status & FTGMAC100_RXDES0_MULTICAST))
517 netdev->stats.multicast++;
519 /* If the HW found checksum errors, bounce it to software.
521 * If we didn't, we need to see if the packet was recognized
522 * by HW as one of the supported checksummed protocols before
523 * we accept the HW test results.
525 if (netdev->features & NETIF_F_RXCSUM) {
526 u32 err_bits = FTGMAC100_RXDES1_TCP_CHKSUM_ERR |
527 FTGMAC100_RXDES1_UDP_CHKSUM_ERR |
528 FTGMAC100_RXDES1_IP_CHKSUM_ERR;
529 if ((csum_vlan & err_bits) ||
530 !(csum_vlan & FTGMAC100_RXDES1_PROT_MASK))
531 skb->ip_summed = CHECKSUM_NONE;
533 skb->ip_summed = CHECKSUM_UNNECESSARY;
536 /* Transfer received size to skb */
539 /* Extract vlan tag */
540 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
541 (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL))
542 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
545 /* Tear down DMA mapping, do necessary cache management */
546 map = le32_to_cpu(rxdes->rxdes3);
548 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM_DMA_USE_IOMMU)
549 /* When we don't have an iommu, we can save cycles by not
550 * invalidating the cache for the part of the packet that
553 dma_unmap_single(priv->dev, map, size, DMA_FROM_DEVICE);
555 dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
559 /* Resplenish rx ring */
560 ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
561 priv->rx_pointer = ftgmac100_next_rx_pointer(priv, pointer);
563 skb->protocol = eth_type_trans(skb, netdev);
565 netdev->stats.rx_packets++;
566 netdev->stats.rx_bytes += size;
568 /* push packet to protocol stack */
569 if (skb->ip_summed == CHECKSUM_NONE)
570 netif_receive_skb(skb);
572 napi_gro_receive(&priv->napi, skb);
578 /* Clean rxdes0 (which resets own bit) */
579 rxdes->rxdes0 = cpu_to_le32(status & priv->rxdes0_edorr_mask);
580 priv->rx_pointer = ftgmac100_next_rx_pointer(priv, pointer);
581 netdev->stats.rx_dropped++;
585 static u32 ftgmac100_base_tx_ctlstat(struct ftgmac100 *priv,
588 if (index == (priv->tx_q_entries - 1))
589 return priv->txdes0_edotr_mask;
594 static unsigned int ftgmac100_next_tx_pointer(struct ftgmac100 *priv,
595 unsigned int pointer)
597 return (pointer + 1) & (priv->tx_q_entries - 1);
600 static u32 ftgmac100_tx_buf_avail(struct ftgmac100 *priv)
602 /* Returns the number of available slots in the TX queue
604 * This always leaves one free slot so we don't have to
605 * worry about empty vs. full, and this simplifies the
606 * test for ftgmac100_tx_buf_cleanable() below
608 return (priv->tx_clean_pointer - priv->tx_pointer - 1) &
609 (priv->tx_q_entries - 1);
612 static bool ftgmac100_tx_buf_cleanable(struct ftgmac100 *priv)
614 return priv->tx_pointer != priv->tx_clean_pointer;
617 static void ftgmac100_free_tx_packet(struct ftgmac100 *priv,
618 unsigned int pointer,
620 struct ftgmac100_txdes *txdes,
623 dma_addr_t map = le32_to_cpu(txdes->txdes3);
626 if (ctl_stat & FTGMAC100_TXDES0_FTS) {
627 len = skb_headlen(skb);
628 dma_unmap_single(priv->dev, map, len, DMA_TO_DEVICE);
630 len = FTGMAC100_TXDES0_TXBUF_SIZE(ctl_stat);
631 dma_unmap_page(priv->dev, map, len, DMA_TO_DEVICE);
634 /* Free SKB on last segment */
635 if (ctl_stat & FTGMAC100_TXDES0_LTS)
637 priv->tx_skbs[pointer] = NULL;
640 static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv)
642 struct net_device *netdev = priv->netdev;
643 struct ftgmac100_txdes *txdes;
645 unsigned int pointer;
648 pointer = priv->tx_clean_pointer;
649 txdes = &priv->txdes[pointer];
651 ctl_stat = le32_to_cpu(txdes->txdes0);
652 if (ctl_stat & FTGMAC100_TXDES0_TXDMA_OWN)
655 skb = priv->tx_skbs[pointer];
656 netdev->stats.tx_packets++;
657 netdev->stats.tx_bytes += skb->len;
658 ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat);
659 txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask);
661 priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv, pointer);
666 static void ftgmac100_tx_complete(struct ftgmac100 *priv)
668 struct net_device *netdev = priv->netdev;
670 /* Process all completed packets */
671 while (ftgmac100_tx_buf_cleanable(priv) &&
672 ftgmac100_tx_complete_packet(priv))
675 /* Restart queue if needed */
677 if (unlikely(netif_queue_stopped(netdev) &&
678 ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)) {
679 struct netdev_queue *txq;
681 txq = netdev_get_tx_queue(netdev, 0);
682 __netif_tx_lock(txq, smp_processor_id());
683 if (netif_queue_stopped(netdev) &&
684 ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
685 netif_wake_queue(netdev);
686 __netif_tx_unlock(txq);
690 static bool ftgmac100_prep_tx_csum(struct sk_buff *skb, u32 *csum_vlan)
692 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
693 u8 ip_proto = ip_hdr(skb)->protocol;
695 *csum_vlan |= FTGMAC100_TXDES1_IP_CHKSUM;
698 *csum_vlan |= FTGMAC100_TXDES1_TCP_CHKSUM;
701 *csum_vlan |= FTGMAC100_TXDES1_UDP_CHKSUM;
707 return skb_checksum_help(skb) == 0;
710 static netdev_tx_t ftgmac100_hard_start_xmit(struct sk_buff *skb,
711 struct net_device *netdev)
713 struct ftgmac100 *priv = netdev_priv(netdev);
714 struct ftgmac100_txdes *txdes, *first;
715 unsigned int pointer, nfrags, len, i, j;
716 u32 f_ctl_stat, ctl_stat, csum_vlan;
719 /* The HW doesn't pad small frames */
720 if (eth_skb_pad(skb)) {
721 netdev->stats.tx_dropped++;
725 /* Reject oversize packets */
726 if (unlikely(skb->len > MAX_PKT_SIZE)) {
728 netdev_dbg(netdev, "tx packet too big\n");
732 /* Do we have a limit on #fragments ? I yet have to get a reply
733 * from Aspeed. If there's one I haven't hit it.
735 nfrags = skb_shinfo(skb)->nr_frags;
737 /* Setup HW checksumming */
739 if (skb->ip_summed == CHECKSUM_PARTIAL &&
740 !ftgmac100_prep_tx_csum(skb, &csum_vlan))
744 if (skb_vlan_tag_present(skb)) {
745 csum_vlan |= FTGMAC100_TXDES1_INS_VLANTAG;
746 csum_vlan |= skb_vlan_tag_get(skb) & 0xffff;
750 len = skb_headlen(skb);
752 /* Map the packet head */
753 map = dma_map_single(priv->dev, skb->data, len, DMA_TO_DEVICE);
754 if (dma_mapping_error(priv->dev, map)) {
756 netdev_err(netdev, "map tx packet head failed\n");
760 /* Grab the next free tx descriptor */
761 pointer = priv->tx_pointer;
762 txdes = first = &priv->txdes[pointer];
764 /* Setup it up with the packet head. Don't write the head to the
767 priv->tx_skbs[pointer] = skb;
768 f_ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
769 f_ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
770 f_ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
771 f_ctl_stat |= FTGMAC100_TXDES0_FTS;
773 f_ctl_stat |= FTGMAC100_TXDES0_LTS;
774 txdes->txdes3 = cpu_to_le32(map);
775 txdes->txdes1 = cpu_to_le32(csum_vlan);
777 /* Next descriptor */
778 pointer = ftgmac100_next_tx_pointer(priv, pointer);
780 /* Add the fragments */
781 for (i = 0; i < nfrags; i++) {
782 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
787 map = skb_frag_dma_map(priv->dev, frag, 0, len,
789 if (dma_mapping_error(priv->dev, map))
792 /* Setup descriptor */
793 priv->tx_skbs[pointer] = skb;
794 txdes = &priv->txdes[pointer];
795 ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
796 ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
797 ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
798 if (i == (nfrags - 1))
799 ctl_stat |= FTGMAC100_TXDES0_LTS;
800 txdes->txdes0 = cpu_to_le32(ctl_stat);
802 txdes->txdes3 = cpu_to_le32(map);
805 pointer = ftgmac100_next_tx_pointer(priv, pointer);
808 /* Order the previous packet and descriptor udpates
809 * before setting the OWN bit on the first descriptor.
812 first->txdes0 = cpu_to_le32(f_ctl_stat);
814 /* Update next TX pointer */
815 priv->tx_pointer = pointer;
817 /* If there isn't enough room for all the fragments of a new packet
818 * in the TX ring, stop the queue. The sequence below is race free
819 * vs. a concurrent restart in ftgmac100_poll()
821 if (unlikely(ftgmac100_tx_buf_avail(priv) < TX_THRESHOLD)) {
822 netif_stop_queue(netdev);
823 /* Order the queue stop with the test below */
825 if (ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
826 netif_wake_queue(netdev);
829 /* Poke transmitter to read the updated TX descriptors */
830 iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD);
836 netdev_err(netdev, "map tx fragment failed\n");
839 pointer = priv->tx_pointer;
840 ftgmac100_free_tx_packet(priv, pointer, skb, first, f_ctl_stat);
841 first->txdes0 = cpu_to_le32(f_ctl_stat & priv->txdes0_edotr_mask);
843 /* Then all fragments */
844 for (j = 0; j < i; j++) {
845 pointer = ftgmac100_next_tx_pointer(priv, pointer);
846 txdes = &priv->txdes[pointer];
847 ctl_stat = le32_to_cpu(txdes->txdes0);
848 ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat);
849 txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask);
852 /* This cannot be reached if we successfully mapped the
853 * last fragment, so we know ftgmac100_free_tx_packet()
854 * hasn't freed the skb yet.
857 /* Drop the packet */
858 dev_kfree_skb_any(skb);
859 netdev->stats.tx_dropped++;
864 static void ftgmac100_free_buffers(struct ftgmac100 *priv)
868 /* Free all RX buffers */
869 for (i = 0; i < priv->rx_q_entries; i++) {
870 struct ftgmac100_rxdes *rxdes = &priv->rxdes[i];
871 struct sk_buff *skb = priv->rx_skbs[i];
872 dma_addr_t map = le32_to_cpu(rxdes->rxdes3);
877 priv->rx_skbs[i] = NULL;
878 dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
879 dev_kfree_skb_any(skb);
882 /* Free all TX buffers */
883 for (i = 0; i < priv->tx_q_entries; i++) {
884 struct ftgmac100_txdes *txdes = &priv->txdes[i];
885 struct sk_buff *skb = priv->tx_skbs[i];
889 ftgmac100_free_tx_packet(priv, i, skb, txdes,
890 le32_to_cpu(txdes->txdes0));
894 static void ftgmac100_free_rings(struct ftgmac100 *priv)
896 /* Free skb arrays */
897 kfree(priv->rx_skbs);
898 kfree(priv->tx_skbs);
900 /* Free descriptors */
902 dma_free_coherent(priv->dev, MAX_RX_QUEUE_ENTRIES *
903 sizeof(struct ftgmac100_rxdes),
904 priv->rxdes, priv->rxdes_dma);
908 dma_free_coherent(priv->dev, MAX_TX_QUEUE_ENTRIES *
909 sizeof(struct ftgmac100_txdes),
910 priv->txdes, priv->txdes_dma);
913 /* Free scratch packet buffer */
914 if (priv->rx_scratch)
915 dma_free_coherent(priv->dev, RX_BUF_SIZE,
916 priv->rx_scratch, priv->rx_scratch_dma);
919 static int ftgmac100_alloc_rings(struct ftgmac100 *priv)
921 /* Allocate skb arrays */
922 priv->rx_skbs = kcalloc(MAX_RX_QUEUE_ENTRIES, sizeof(void *),
926 priv->tx_skbs = kcalloc(MAX_TX_QUEUE_ENTRIES, sizeof(void *),
931 /* Allocate descriptors */
932 priv->rxdes = dma_zalloc_coherent(priv->dev,
933 MAX_RX_QUEUE_ENTRIES *
934 sizeof(struct ftgmac100_rxdes),
935 &priv->rxdes_dma, GFP_KERNEL);
938 priv->txdes = dma_zalloc_coherent(priv->dev,
939 MAX_TX_QUEUE_ENTRIES *
940 sizeof(struct ftgmac100_txdes),
941 &priv->txdes_dma, GFP_KERNEL);
945 /* Allocate scratch packet buffer */
946 priv->rx_scratch = dma_alloc_coherent(priv->dev,
948 &priv->rx_scratch_dma,
950 if (!priv->rx_scratch)
956 static void ftgmac100_init_rings(struct ftgmac100 *priv)
958 struct ftgmac100_rxdes *rxdes = NULL;
959 struct ftgmac100_txdes *txdes = NULL;
962 /* Update entries counts */
963 priv->rx_q_entries = priv->new_rx_q_entries;
964 priv->tx_q_entries = priv->new_tx_q_entries;
966 if (WARN_ON(priv->rx_q_entries < MIN_RX_QUEUE_ENTRIES))
969 /* Initialize RX ring */
970 for (i = 0; i < priv->rx_q_entries; i++) {
971 rxdes = &priv->rxdes[i];
973 rxdes->rxdes3 = cpu_to_le32(priv->rx_scratch_dma);
975 /* Mark the end of the ring */
976 rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask);
978 if (WARN_ON(priv->tx_q_entries < MIN_RX_QUEUE_ENTRIES))
981 /* Initialize TX ring */
982 for (i = 0; i < priv->tx_q_entries; i++) {
983 txdes = &priv->txdes[i];
986 txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask);
989 static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv)
993 for (i = 0; i < priv->rx_q_entries; i++) {
994 struct ftgmac100_rxdes *rxdes = &priv->rxdes[i];
996 if (ftgmac100_alloc_rx_buf(priv, i, rxdes, GFP_KERNEL))
1002 static void ftgmac100_adjust_link(struct net_device *netdev)
1004 struct ftgmac100 *priv = netdev_priv(netdev);
1005 struct phy_device *phydev = netdev->phydev;
1006 bool tx_pause, rx_pause;
1009 /* We store "no link" as speed 0 */
1013 new_speed = phydev->speed;
1015 /* Grab pause settings from PHY if configured to do so */
1016 if (priv->aneg_pause) {
1017 rx_pause = tx_pause = phydev->pause;
1018 if (phydev->asym_pause)
1019 tx_pause = !rx_pause;
1021 rx_pause = priv->rx_pause;
1022 tx_pause = priv->tx_pause;
1025 /* Link hasn't changed, do nothing */
1026 if (phydev->speed == priv->cur_speed &&
1027 phydev->duplex == priv->cur_duplex &&
1028 rx_pause == priv->rx_pause &&
1029 tx_pause == priv->tx_pause)
1032 /* Print status if we have a link or we had one and just lost it,
1033 * don't print otherwise.
1035 if (new_speed || priv->cur_speed)
1036 phy_print_status(phydev);
1038 priv->cur_speed = new_speed;
1039 priv->cur_duplex = phydev->duplex;
1040 priv->rx_pause = rx_pause;
1041 priv->tx_pause = tx_pause;
1043 /* Link is down, do nothing else */
1047 /* Disable all interrupts */
1048 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1050 /* Reset the adapter asynchronously */
1051 schedule_work(&priv->reset_task);
1054 static int ftgmac100_mii_probe(struct ftgmac100 *priv, phy_interface_t intf)
1056 struct net_device *netdev = priv->netdev;
1057 struct phy_device *phydev;
1059 phydev = phy_find_first(priv->mii_bus);
1061 netdev_info(netdev, "%s: no PHY found\n", netdev->name);
1065 phydev = phy_connect(netdev, phydev_name(phydev),
1066 &ftgmac100_adjust_link, intf);
1068 if (IS_ERR(phydev)) {
1069 netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
1070 return PTR_ERR(phydev);
1073 /* Indicate that we support PAUSE frames (see comment in
1074 * Documentation/networking/phy.txt)
1076 phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1077 phydev->advertising = phydev->supported;
1079 /* Display what we found */
1080 phy_attached_info(phydev);
1085 static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
1087 struct net_device *netdev = bus->priv;
1088 struct ftgmac100 *priv = netdev_priv(netdev);
1092 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1094 /* preserve MDC cycle threshold */
1095 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
1097 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
1098 FTGMAC100_PHYCR_REGAD(regnum) |
1099 FTGMAC100_PHYCR_MIIRD;
1101 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
1103 for (i = 0; i < 10; i++) {
1104 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1106 if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
1109 data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
1110 return FTGMAC100_PHYDATA_MIIRDATA(data);
1116 netdev_err(netdev, "mdio read timed out\n");
1120 static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
1121 int regnum, u16 value)
1123 struct net_device *netdev = bus->priv;
1124 struct ftgmac100 *priv = netdev_priv(netdev);
1129 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1131 /* preserve MDC cycle threshold */
1132 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
1134 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
1135 FTGMAC100_PHYCR_REGAD(regnum) |
1136 FTGMAC100_PHYCR_MIIWR;
1138 data = FTGMAC100_PHYDATA_MIIWDATA(value);
1140 iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
1141 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
1143 for (i = 0; i < 10; i++) {
1144 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1146 if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
1152 netdev_err(netdev, "mdio write timed out\n");
1156 static void ftgmac100_get_drvinfo(struct net_device *netdev,
1157 struct ethtool_drvinfo *info)
1159 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1160 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1161 strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
1164 static void ftgmac100_get_ringparam(struct net_device *netdev,
1165 struct ethtool_ringparam *ering)
1167 struct ftgmac100 *priv = netdev_priv(netdev);
1169 memset(ering, 0, sizeof(*ering));
1170 ering->rx_max_pending = MAX_RX_QUEUE_ENTRIES;
1171 ering->tx_max_pending = MAX_TX_QUEUE_ENTRIES;
1172 ering->rx_pending = priv->rx_q_entries;
1173 ering->tx_pending = priv->tx_q_entries;
1176 static int ftgmac100_set_ringparam(struct net_device *netdev,
1177 struct ethtool_ringparam *ering)
1179 struct ftgmac100 *priv = netdev_priv(netdev);
1181 if (ering->rx_pending > MAX_RX_QUEUE_ENTRIES ||
1182 ering->tx_pending > MAX_TX_QUEUE_ENTRIES ||
1183 ering->rx_pending < MIN_RX_QUEUE_ENTRIES ||
1184 ering->tx_pending < MIN_TX_QUEUE_ENTRIES ||
1185 !is_power_of_2(ering->rx_pending) ||
1186 !is_power_of_2(ering->tx_pending))
1189 priv->new_rx_q_entries = ering->rx_pending;
1190 priv->new_tx_q_entries = ering->tx_pending;
1191 if (netif_running(netdev))
1192 schedule_work(&priv->reset_task);
1197 static void ftgmac100_get_pauseparam(struct net_device *netdev,
1198 struct ethtool_pauseparam *pause)
1200 struct ftgmac100 *priv = netdev_priv(netdev);
1202 pause->autoneg = priv->aneg_pause;
1203 pause->tx_pause = priv->tx_pause;
1204 pause->rx_pause = priv->rx_pause;
1207 static int ftgmac100_set_pauseparam(struct net_device *netdev,
1208 struct ethtool_pauseparam *pause)
1210 struct ftgmac100 *priv = netdev_priv(netdev);
1211 struct phy_device *phydev = netdev->phydev;
1213 priv->aneg_pause = pause->autoneg;
1214 priv->tx_pause = pause->tx_pause;
1215 priv->rx_pause = pause->rx_pause;
1218 phydev->advertising &= ~ADVERTISED_Pause;
1219 phydev->advertising &= ~ADVERTISED_Asym_Pause;
1221 if (pause->rx_pause) {
1222 phydev->advertising |= ADVERTISED_Pause;
1223 phydev->advertising |= ADVERTISED_Asym_Pause;
1226 if (pause->tx_pause)
1227 phydev->advertising ^= ADVERTISED_Asym_Pause;
1229 if (netif_running(netdev)) {
1230 if (phydev && priv->aneg_pause)
1231 phy_start_aneg(phydev);
1233 ftgmac100_config_pause(priv);
1239 static const struct ethtool_ops ftgmac100_ethtool_ops = {
1240 .get_drvinfo = ftgmac100_get_drvinfo,
1241 .get_link = ethtool_op_get_link,
1242 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1243 .set_link_ksettings = phy_ethtool_set_link_ksettings,
1244 .nway_reset = phy_ethtool_nway_reset,
1245 .get_ringparam = ftgmac100_get_ringparam,
1246 .set_ringparam = ftgmac100_set_ringparam,
1247 .get_pauseparam = ftgmac100_get_pauseparam,
1248 .set_pauseparam = ftgmac100_set_pauseparam,
1251 static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
1253 struct net_device *netdev = dev_id;
1254 struct ftgmac100 *priv = netdev_priv(netdev);
1255 unsigned int status, new_mask = FTGMAC100_INT_BAD;
1257 /* Fetch and clear interrupt bits, process abnormal ones */
1258 status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
1259 iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
1260 if (unlikely(status & FTGMAC100_INT_BAD)) {
1262 /* RX buffer unavailable */
1263 if (status & FTGMAC100_INT_NO_RXBUF)
1264 netdev->stats.rx_over_errors++;
1266 /* received packet lost due to RX FIFO full */
1267 if (status & FTGMAC100_INT_RPKT_LOST)
1268 netdev->stats.rx_fifo_errors++;
1270 /* sent packet lost due to excessive TX collision */
1271 if (status & FTGMAC100_INT_XPKT_LOST)
1272 netdev->stats.tx_fifo_errors++;
1274 /* AHB error -> Reset the chip */
1275 if (status & FTGMAC100_INT_AHB_ERR) {
1276 if (net_ratelimit())
1278 "AHB bus error ! Resetting chip.\n");
1279 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1280 schedule_work(&priv->reset_task);
1284 /* We may need to restart the MAC after such errors, delay
1285 * this until after we have freed some Rx buffers though
1287 priv->need_mac_restart = true;
1289 /* Disable those errors until we restart */
1290 new_mask &= ~status;
1293 /* Only enable "bad" interrupts while NAPI is on */
1294 iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER);
1296 /* Schedule NAPI bh */
1297 napi_schedule_irqoff(&priv->napi);
1302 static bool ftgmac100_check_rx(struct ftgmac100 *priv)
1304 struct ftgmac100_rxdes *rxdes = &priv->rxdes[priv->rx_pointer];
1306 /* Do we have a packet ? */
1307 return !!(rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY));
1310 static int ftgmac100_poll(struct napi_struct *napi, int budget)
1312 struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
1316 /* Handle TX completions */
1317 if (ftgmac100_tx_buf_cleanable(priv))
1318 ftgmac100_tx_complete(priv);
1320 /* Handle RX packets */
1322 more = ftgmac100_rx_packet(priv, &work_done);
1323 } while (more && work_done < budget);
1326 /* The interrupt is telling us to kick the MAC back to life
1327 * after an RX overflow
1329 if (unlikely(priv->need_mac_restart)) {
1330 ftgmac100_start_hw(priv);
1331 priv->need_mac_restart = false;
1333 /* Re-enable "bad" interrupts */
1334 iowrite32(FTGMAC100_INT_BAD,
1335 priv->base + FTGMAC100_OFFSET_IER);
1338 /* As long as we are waiting for transmit packets to be
1339 * completed we keep NAPI going
1341 if (ftgmac100_tx_buf_cleanable(priv))
1344 if (work_done < budget) {
1345 /* We are about to re-enable all interrupts. However
1346 * the HW has been latching RX/TX packet interrupts while
1347 * they were masked. So we clear them first, then we need
1348 * to re-check if there's something to process
1350 iowrite32(FTGMAC100_INT_RXTX,
1351 priv->base + FTGMAC100_OFFSET_ISR);
1353 /* Push the above (and provides a barrier vs. subsequent
1354 * reads of the descriptor).
1356 ioread32(priv->base + FTGMAC100_OFFSET_ISR);
1358 /* Check RX and TX descriptors for more work to do */
1359 if (ftgmac100_check_rx(priv) ||
1360 ftgmac100_tx_buf_cleanable(priv))
1363 /* deschedule NAPI */
1364 napi_complete(napi);
1366 /* enable all interrupts */
1367 iowrite32(FTGMAC100_INT_ALL,
1368 priv->base + FTGMAC100_OFFSET_IER);
1374 static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
1378 /* Re-init descriptors (adjust queue sizes) */
1379 ftgmac100_init_rings(priv);
1381 /* Realloc rx descriptors */
1382 err = ftgmac100_alloc_rx_buffers(priv);
1383 if (err && !ignore_alloc_err)
1386 /* Reinit and restart HW */
1387 ftgmac100_init_hw(priv);
1388 ftgmac100_config_pause(priv);
1389 ftgmac100_start_hw(priv);
1391 /* Re-enable the device */
1392 napi_enable(&priv->napi);
1393 netif_start_queue(priv->netdev);
1395 /* Enable all interrupts */
1396 iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
1401 static void ftgmac100_reset_task(struct work_struct *work)
1403 struct ftgmac100 *priv = container_of(work, struct ftgmac100,
1405 struct net_device *netdev = priv->netdev;
1408 netdev_dbg(netdev, "Resetting NIC...\n");
1410 /* Lock the world */
1413 mutex_lock(&netdev->phydev->lock);
1415 mutex_lock(&priv->mii_bus->mdio_lock);
1418 /* Check if the interface is still up */
1419 if (!netif_running(netdev))
1422 /* Stop the network stack */
1423 netif_trans_update(netdev);
1424 napi_disable(&priv->napi);
1425 netif_tx_disable(netdev);
1427 /* Stop and reset the MAC */
1428 ftgmac100_stop_hw(priv);
1429 err = ftgmac100_reset_and_config_mac(priv);
1431 /* Not much we can do ... it might come back... */
1432 netdev_err(netdev, "attempting to continue...\n");
1435 /* Free all rx and tx buffers */
1436 ftgmac100_free_buffers(priv);
1438 /* Setup everything again and restart chip */
1439 ftgmac100_init_all(priv, true);
1441 netdev_dbg(netdev, "Reset done !\n");
1444 mutex_unlock(&priv->mii_bus->mdio_lock);
1446 mutex_unlock(&netdev->phydev->lock);
1450 static int ftgmac100_open(struct net_device *netdev)
1452 struct ftgmac100 *priv = netdev_priv(netdev);
1455 /* Allocate ring buffers */
1456 err = ftgmac100_alloc_rings(priv);
1458 netdev_err(netdev, "Failed to allocate descriptors\n");
1462 /* When using NC-SI we force the speed to 100Mbit/s full duplex,
1464 * Otherwise we leave it set to 0 (no link), the link
1465 * message from the PHY layer will handle setting it up to
1466 * something else if needed.
1468 if (priv->use_ncsi) {
1469 priv->cur_duplex = DUPLEX_FULL;
1470 priv->cur_speed = SPEED_100;
1472 priv->cur_duplex = 0;
1473 priv->cur_speed = 0;
1476 /* Reset the hardware */
1477 err = ftgmac100_reset_and_config_mac(priv);
1481 /* Initialize NAPI */
1482 netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
1484 /* Grab our interrupt */
1485 err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
1487 netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
1491 /* Start things up */
1492 err = ftgmac100_init_all(priv, false);
1494 netdev_err(netdev, "Failed to allocate packet buffers\n");
1498 if (netdev->phydev) {
1499 /* If we have a PHY, start polling */
1500 phy_start(netdev->phydev);
1501 } else if (priv->use_ncsi) {
1502 /* If using NC-SI, set our carrier on and start the stack */
1503 netif_carrier_on(netdev);
1505 /* Start the NCSI device */
1506 err = ncsi_start_dev(priv->ndev);
1514 napi_disable(&priv->napi);
1515 netif_stop_queue(netdev);
1517 ftgmac100_free_buffers(priv);
1518 free_irq(netdev->irq, netdev);
1520 netif_napi_del(&priv->napi);
1522 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1523 ftgmac100_free_rings(priv);
1527 static int ftgmac100_stop(struct net_device *netdev)
1529 struct ftgmac100 *priv = netdev_priv(netdev);
1531 /* Note about the reset task: We are called with the rtnl lock
1532 * held, so we are synchronized against the core of the reset
1533 * task. We must not try to synchronously cancel it otherwise
1534 * we can deadlock. But since it will test for netif_running()
1535 * which has already been cleared by the net core, we don't
1536 * anything special to do.
1539 /* disable all interrupts */
1540 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1542 netif_stop_queue(netdev);
1543 napi_disable(&priv->napi);
1544 netif_napi_del(&priv->napi);
1546 phy_stop(netdev->phydev);
1547 else if (priv->use_ncsi)
1548 ncsi_stop_dev(priv->ndev);
1550 ftgmac100_stop_hw(priv);
1551 free_irq(netdev->irq, netdev);
1552 ftgmac100_free_buffers(priv);
1553 ftgmac100_free_rings(priv);
1559 static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1561 if (!netdev->phydev)
1564 return phy_mii_ioctl(netdev->phydev, ifr, cmd);
1567 static void ftgmac100_tx_timeout(struct net_device *netdev)
1569 struct ftgmac100 *priv = netdev_priv(netdev);
1571 /* Disable all interrupts */
1572 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1574 /* Do the reset outside of interrupt context */
1575 schedule_work(&priv->reset_task);
1578 static int ftgmac100_set_features(struct net_device *netdev,
1579 netdev_features_t features)
1581 struct ftgmac100 *priv = netdev_priv(netdev);
1582 netdev_features_t changed = netdev->features ^ features;
1584 if (!netif_running(netdev))
1587 /* Update the vlan filtering bit */
1588 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1591 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
1592 if (priv->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
1593 maccr |= FTGMAC100_MACCR_RM_VLAN;
1595 maccr &= ~FTGMAC100_MACCR_RM_VLAN;
1596 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
1602 #ifdef CONFIG_NET_POLL_CONTROLLER
1603 static void ftgmac100_poll_controller(struct net_device *netdev)
1605 unsigned long flags;
1607 local_irq_save(flags);
1608 ftgmac100_interrupt(netdev->irq, netdev);
1609 local_irq_restore(flags);
1613 static const struct net_device_ops ftgmac100_netdev_ops = {
1614 .ndo_open = ftgmac100_open,
1615 .ndo_stop = ftgmac100_stop,
1616 .ndo_start_xmit = ftgmac100_hard_start_xmit,
1617 .ndo_set_mac_address = ftgmac100_set_mac_addr,
1618 .ndo_validate_addr = eth_validate_addr,
1619 .ndo_do_ioctl = ftgmac100_do_ioctl,
1620 .ndo_tx_timeout = ftgmac100_tx_timeout,
1621 .ndo_set_rx_mode = ftgmac100_set_rx_mode,
1622 .ndo_set_features = ftgmac100_set_features,
1623 #ifdef CONFIG_NET_POLL_CONTROLLER
1624 .ndo_poll_controller = ftgmac100_poll_controller,
1626 .ndo_vlan_rx_add_vid = ncsi_vlan_rx_add_vid,
1627 .ndo_vlan_rx_kill_vid = ncsi_vlan_rx_kill_vid,
1630 static int ftgmac100_setup_mdio(struct net_device *netdev)
1632 struct ftgmac100 *priv = netdev_priv(netdev);
1633 struct platform_device *pdev = to_platform_device(priv->dev);
1634 int phy_intf = PHY_INTERFACE_MODE_RGMII;
1635 struct device_node *np = pdev->dev.of_node;
1639 /* initialize mdio bus */
1640 priv->mii_bus = mdiobus_alloc();
1644 if (priv->is_aspeed) {
1645 /* This driver supports the old MDIO interface */
1646 reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
1647 reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
1648 iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
1651 /* Get PHY mode from device-tree */
1653 /* Default to RGMII. It's a gigabit part after all */
1654 phy_intf = of_get_phy_mode(np);
1656 phy_intf = PHY_INTERFACE_MODE_RGMII;
1658 /* Aspeed only supports these. I don't know about other IP
1659 * block vendors so I'm going to just let them through for
1660 * now. Note that this is only a warning if for some obscure
1661 * reason the DT really means to lie about it or it's a newer
1662 * part we don't know about.
1664 * On the Aspeed SoC there are additionally straps and SCU
1665 * control bits that could tell us what the interface is
1666 * (or allow us to configure it while the IP block is held
1667 * in reset). For now I chose to keep this driver away from
1668 * those SoC specific bits and assume the device-tree is
1669 * right and the SCU has been configured properly by pinmux
1672 if (priv->is_aspeed &&
1673 phy_intf != PHY_INTERFACE_MODE_RMII &&
1674 phy_intf != PHY_INTERFACE_MODE_RGMII &&
1675 phy_intf != PHY_INTERFACE_MODE_RGMII_ID &&
1676 phy_intf != PHY_INTERFACE_MODE_RGMII_RXID &&
1677 phy_intf != PHY_INTERFACE_MODE_RGMII_TXID) {
1679 "Unsupported PHY mode %s !\n",
1680 phy_modes(phy_intf));
1684 priv->mii_bus->name = "ftgmac100_mdio";
1685 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1686 pdev->name, pdev->id);
1687 priv->mii_bus->parent = priv->dev;
1688 priv->mii_bus->priv = priv->netdev;
1689 priv->mii_bus->read = ftgmac100_mdiobus_read;
1690 priv->mii_bus->write = ftgmac100_mdiobus_write;
1692 for (i = 0; i < PHY_MAX_ADDR; i++)
1693 priv->mii_bus->irq[i] = PHY_POLL;
1695 err = mdiobus_register(priv->mii_bus);
1697 dev_err(priv->dev, "Cannot register MDIO bus!\n");
1698 goto err_register_mdiobus;
1701 err = ftgmac100_mii_probe(priv, phy_intf);
1703 dev_err(priv->dev, "MII Probe failed!\n");
1710 mdiobus_unregister(priv->mii_bus);
1711 err_register_mdiobus:
1712 mdiobus_free(priv->mii_bus);
1716 static void ftgmac100_destroy_mdio(struct net_device *netdev)
1718 struct ftgmac100 *priv = netdev_priv(netdev);
1720 if (!netdev->phydev)
1723 phy_disconnect(netdev->phydev);
1724 mdiobus_unregister(priv->mii_bus);
1725 mdiobus_free(priv->mii_bus);
1728 static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
1730 if (unlikely(nd->state != ncsi_dev_state_functional))
1733 netdev_info(nd->dev, "NCSI interface %s\n",
1734 nd->link_up ? "up" : "down");
1737 static int ftgmac100_probe(struct platform_device *pdev)
1739 struct resource *res;
1741 struct net_device *netdev;
1742 struct ftgmac100 *priv;
1743 struct device_node *np;
1749 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1753 irq = platform_get_irq(pdev, 0);
1757 /* setup net_device */
1758 netdev = alloc_etherdev(sizeof(*priv));
1761 goto err_alloc_etherdev;
1764 SET_NETDEV_DEV(netdev, &pdev->dev);
1766 netdev->ethtool_ops = &ftgmac100_ethtool_ops;
1767 netdev->netdev_ops = &ftgmac100_netdev_ops;
1768 netdev->watchdog_timeo = 5 * HZ;
1770 platform_set_drvdata(pdev, netdev);
1772 /* setup private data */
1773 priv = netdev_priv(netdev);
1774 priv->netdev = netdev;
1775 priv->dev = &pdev->dev;
1776 INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
1779 priv->res = request_mem_region(res->start, resource_size(res),
1780 dev_name(&pdev->dev));
1782 dev_err(&pdev->dev, "Could not reserve memory region\n");
1787 priv->base = ioremap(res->start, resource_size(res));
1789 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1797 priv->tx_pause = true;
1798 priv->rx_pause = true;
1799 priv->aneg_pause = true;
1801 /* MAC address from chip or random one */
1802 ftgmac100_initial_mac(priv);
1804 np = pdev->dev.of_node;
1805 if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac") ||
1806 of_device_is_compatible(np, "aspeed,ast2500-mac"))) {
1807 priv->rxdes0_edorr_mask = BIT(30);
1808 priv->txdes0_edotr_mask = BIT(30);
1809 priv->is_aspeed = true;
1811 priv->rxdes0_edorr_mask = BIT(15);
1812 priv->txdes0_edotr_mask = BIT(15);
1815 if (np && of_get_property(np, "use-ncsi", NULL)) {
1816 if (!IS_ENABLED(CONFIG_NET_NCSI)) {
1817 dev_err(&pdev->dev, "NCSI stack not enabled\n");
1821 dev_info(&pdev->dev, "Using NCSI interface\n");
1822 priv->use_ncsi = true;
1823 priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
1827 priv->use_ncsi = false;
1828 err = ftgmac100_setup_mdio(netdev);
1830 goto err_setup_mdio;
1833 /* Default ring sizes */
1834 priv->rx_q_entries = priv->new_rx_q_entries = DEF_RX_QUEUE_ENTRIES;
1835 priv->tx_q_entries = priv->new_tx_q_entries = DEF_TX_QUEUE_ENTRIES;
1837 /* Base feature set */
1838 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM |
1839 NETIF_F_GRO | NETIF_F_SG | NETIF_F_HW_VLAN_CTAG_RX |
1840 NETIF_F_HW_VLAN_CTAG_TX;
1843 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1845 /* AST2400 doesn't have working HW checksum generation */
1846 if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac")))
1847 netdev->hw_features &= ~NETIF_F_HW_CSUM;
1849 /* AST2600 tx checksum with NCSI is broken */
1850 if (priv->use_ncsi && of_device_is_compatible(np, "aspeed,ast2600-mac"))
1851 netdev->hw_features &= ~NETIF_F_HW_CSUM;
1853 if (np && of_get_property(np, "no-hw-checksum", NULL))
1854 netdev->hw_features &= ~(NETIF_F_HW_CSUM | NETIF_F_RXCSUM);
1855 netdev->features |= netdev->hw_features;
1857 /* register network device */
1858 err = register_netdev(netdev);
1860 dev_err(&pdev->dev, "Failed to register netdev\n");
1861 goto err_register_netdev;
1864 netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
1870 ncsi_unregister_dev(priv->ndev);
1871 err_register_netdev:
1872 ftgmac100_destroy_mdio(netdev);
1874 iounmap(priv->base);
1876 release_resource(priv->res);
1878 free_netdev(netdev);
1883 static int ftgmac100_remove(struct platform_device *pdev)
1885 struct net_device *netdev;
1886 struct ftgmac100 *priv;
1888 netdev = platform_get_drvdata(pdev);
1889 priv = netdev_priv(netdev);
1892 ncsi_unregister_dev(priv->ndev);
1893 unregister_netdev(netdev);
1895 /* There's a small chance the reset task will have been re-queued,
1896 * during stop, make sure it's gone before we free the structure.
1898 cancel_work_sync(&priv->reset_task);
1900 ftgmac100_destroy_mdio(netdev);
1902 iounmap(priv->base);
1903 release_resource(priv->res);
1905 netif_napi_del(&priv->napi);
1906 free_netdev(netdev);
1910 static const struct of_device_id ftgmac100_of_match[] = {
1911 { .compatible = "faraday,ftgmac100" },
1914 MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
1916 static struct platform_driver ftgmac100_driver = {
1917 .probe = ftgmac100_probe,
1918 .remove = ftgmac100_remove,
1921 .of_match_table = ftgmac100_of_match,
1924 module_platform_driver(ftgmac100_driver);
1926 MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1927 MODULE_DESCRIPTION("FTGMAC100 driver");
1928 MODULE_LICENSE("GPL");