22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/ethtool.h>
30 #include <linux/mii.h>
31 #include <linux/module.h>
32 #include <linux/netdevice.h>
37 #define DRV_NAME "ftmac100"
38 #define DRV_VERSION "0.2"
40 #define RX_QUEUE_ENTRIES 128
41 #define TX_QUEUE_ENTRIES 16
43 #define MAX_PKT_SIZE 1518
44 #define RX_BUF_SIZE 2044
46 #if MAX_PKT_SIZE > 0x7ff
47 #error invalid MAX_PKT_SIZE
50 #if RX_BUF_SIZE > 0x7ff || RX_BUF_SIZE > PAGE_SIZE
51 #error invalid RX_BUF_SIZE
90 #define INT_MASK_ALL_ENABLED (FTMAC100_INT_RPKT_FINISH | \
91 FTMAC100_INT_NORXBUF | \
92 FTMAC100_INT_XPKT_OK | \
93 FTMAC100_INT_XPKT_LOST | \
94 FTMAC100_INT_RPKT_LOST | \
95 FTMAC100_INT_AHB_ERR | \
96 FTMAC100_INT_PHYSTS_CHG)
98 #define INT_MASK_ALL_DISABLED 0
100 static void ftmac100_enable_all_int(
struct ftmac100 *
priv)
105 static void ftmac100_disable_all_int(
struct ftmac100 *
priv)
120 static void ftmac100_txdma_start_polling(
struct ftmac100 *
priv)
133 for (i = 0; i < 5; i++) {
150 netdev_err(netdev,
"software reset failed\n");
154 static void ftmac100_set_mac(
struct ftmac100 *priv,
const unsigned char *
mac)
156 unsigned int maddr = mac[0] << 8 | mac[1];
157 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
163 #define MACCR_ENABLE_ALL (FTMAC100_MACCR_XMT_EN | \
164 FTMAC100_MACCR_RCV_EN | \
165 FTMAC100_MACCR_XDMA_EN | \
166 FTMAC100_MACCR_RDMA_EN | \
167 FTMAC100_MACCR_CRC_APD | \
168 FTMAC100_MACCR_FULLDUP | \
169 FTMAC100_MACCR_RX_RUNT | \
170 FTMAC100_MACCR_RX_BROADPKT)
172 static int ftmac100_start_hw(
struct ftmac100 *priv)
176 if (ftmac100_reset(priv))
180 ftmac100_set_rx_ring_base(priv,
183 ftmac100_set_tx_ring_base(priv,
189 ftmac100_set_mac(priv, netdev->
dev_addr);
195 static void ftmac100_stop_hw(
struct ftmac100 *priv)
203 static bool ftmac100_rxdes_first_segment(
struct ftmac100_rxdes *rxdes)
208 static bool ftmac100_rxdes_last_segment(
struct ftmac100_rxdes *rxdes)
213 static bool ftmac100_rxdes_owned_by_dma(
struct ftmac100_rxdes *rxdes)
218 static void ftmac100_rxdes_set_dma_own(
struct ftmac100_rxdes *rxdes)
229 static bool ftmac100_rxdes_crc_error(
struct ftmac100_rxdes *rxdes)
234 static bool ftmac100_rxdes_frame_too_long(
struct ftmac100_rxdes *rxdes)
244 static bool ftmac100_rxdes_odd_nibble(
struct ftmac100_rxdes *rxdes)
249 static unsigned int ftmac100_rxdes_frame_length(
struct ftmac100_rxdes *rxdes)
254 static bool ftmac100_rxdes_multicast(
struct ftmac100_rxdes *rxdes)
259 static void ftmac100_rxdes_set_buffer_size(
struct ftmac100_rxdes *rxdes,
266 static void ftmac100_rxdes_set_end_of_ring(
struct ftmac100_rxdes *rxdes)
271 static void ftmac100_rxdes_set_dma_addr(
struct ftmac100_rxdes *rxdes,
299 static int ftmac100_next_rx_pointer(
int pointer)
304 static void ftmac100_rx_pointer_advance(
struct ftmac100 *priv)
315 ftmac100_rx_locate_first_segment(
struct ftmac100 *priv)
319 while (!ftmac100_rxdes_owned_by_dma(rxdes)) {
320 if (ftmac100_rxdes_first_segment(rxdes))
323 ftmac100_rxdes_set_dma_own(rxdes);
324 ftmac100_rx_pointer_advance(priv);
325 rxdes = ftmac100_current_rxdes(priv);
331 static bool ftmac100_rx_packet_error(
struct ftmac100 *priv,
337 if (
unlikely(ftmac100_rxdes_rx_error(rxdes))) {
339 netdev_info(netdev,
"rx err\n");
341 netdev->
stats.rx_errors++;
345 if (
unlikely(ftmac100_rxdes_crc_error(rxdes))) {
347 netdev_info(netdev,
"rx crc err\n");
349 netdev->
stats.rx_crc_errors++;
353 if (
unlikely(ftmac100_rxdes_frame_too_long(rxdes))) {
355 netdev_info(netdev,
"rx frame too long\n");
357 netdev->
stats.rx_length_errors++;
359 }
else if (
unlikely(ftmac100_rxdes_runt(rxdes))) {
361 netdev_info(netdev,
"rx runt\n");
363 netdev->
stats.rx_length_errors++;
365 }
else if (
unlikely(ftmac100_rxdes_odd_nibble(rxdes))) {
367 netdev_info(netdev,
"rx odd nibble\n");
369 netdev->
stats.rx_length_errors++;
376 static void ftmac100_rx_drop_packet(
struct ftmac100 *priv)
383 netdev_dbg(netdev,
"drop packet %p\n", rxdes);
386 if (ftmac100_rxdes_last_segment(rxdes))
389 ftmac100_rxdes_set_dma_own(rxdes);
390 ftmac100_rx_pointer_advance(priv);
391 rxdes = ftmac100_current_rxdes(priv);
392 }
while (!done && !ftmac100_rxdes_owned_by_dma(rxdes));
394 netdev->
stats.rx_dropped++;
397 static bool ftmac100_rx_packet(
struct ftmac100 *priv,
int *processed)
406 rxdes = ftmac100_rx_locate_first_segment(priv);
410 if (
unlikely(ftmac100_rx_packet_error(priv, rxdes))) {
411 ftmac100_rx_drop_packet(priv);
419 if (
unlikely(!ftmac100_rxdes_last_segment(rxdes)))
423 skb = netdev_alloc_skb_ip_align(netdev, 128);
426 netdev_err(netdev,
"rx skb alloc failed\n");
428 ftmac100_rx_drop_packet(priv);
432 if (
unlikely(ftmac100_rxdes_multicast(rxdes)))
433 netdev->
stats.multicast++;
435 map = ftmac100_rxdes_get_dma_addr(rxdes);
438 length = ftmac100_rxdes_frame_length(rxdes);
439 page = ftmac100_rxdes_get_page(rxdes);
440 skb_fill_page_desc(skb, 0, page, 0, length);
452 ftmac100_alloc_rx_page(priv, rxdes,
GFP_ATOMIC);
454 ftmac100_rx_pointer_advance(priv);
458 netdev->
stats.rx_packets++;
480 static bool ftmac100_txdes_owned_by_dma(
struct ftmac100_txdes *txdes)
485 static void ftmac100_txdes_set_dma_own(
struct ftmac100_txdes *txdes)
495 static bool ftmac100_txdes_excessive_collision(
struct ftmac100_txdes *txdes)
500 static bool ftmac100_txdes_late_collision(
struct ftmac100_txdes *txdes)
505 static void ftmac100_txdes_set_end_of_ring(
struct ftmac100_txdes *txdes)
510 static void ftmac100_txdes_set_first_segment(
struct ftmac100_txdes *txdes)
515 static void ftmac100_txdes_set_last_segment(
struct ftmac100_txdes *txdes)
520 static void ftmac100_txdes_set_txint(
struct ftmac100_txdes *txdes)
525 static void ftmac100_txdes_set_buffer_size(
struct ftmac100_txdes *txdes,
531 static void ftmac100_txdes_set_dma_addr(
struct ftmac100_txdes *txdes,
559 static int ftmac100_next_tx_pointer(
int pointer)
564 static void ftmac100_tx_pointer_advance(
struct ftmac100 *priv)
569 static void ftmac100_tx_clean_pointer_advance(
struct ftmac100 *priv)
584 static bool ftmac100_tx_complete_packet(
struct ftmac100 *priv)
594 txdes = ftmac100_current_clean_txdes(priv);
596 if (ftmac100_txdes_owned_by_dma(txdes))
599 skb = ftmac100_txdes_get_skb(txdes);
600 map = ftmac100_txdes_get_dma_addr(txdes);
602 if (
unlikely(ftmac100_txdes_excessive_collision(txdes) ||
603 ftmac100_txdes_late_collision(txdes))) {
608 netdev->
stats.tx_aborted_errors++;
610 netdev->
stats.tx_packets++;
617 ftmac100_txdes_reset(txdes);
619 ftmac100_tx_clean_pointer_advance(priv);
624 netif_wake_queue(netdev);
629 static void ftmac100_tx_complete(
struct ftmac100 *priv)
631 while (ftmac100_tx_complete_packet(priv))
642 txdes = ftmac100_current_txdes(priv);
643 ftmac100_tx_pointer_advance(priv);
646 ftmac100_txdes_set_skb(txdes, skb);
647 ftmac100_txdes_set_dma_addr(txdes, map);
649 ftmac100_txdes_set_first_segment(txdes);
650 ftmac100_txdes_set_last_segment(txdes);
651 ftmac100_txdes_set_txint(txdes);
652 ftmac100_txdes_set_buffer_size(txdes, len);
657 netif_stop_queue(netdev);
660 ftmac100_txdes_set_dma_own(txdes);
663 ftmac100_txdma_start_polling(priv);
670 static int ftmac100_alloc_rx_page(
struct ftmac100 *priv,
680 netdev_err(netdev,
"failed to allocate rx page\n");
687 netdev_err(netdev,
"failed to map rx page\n");
692 ftmac100_rxdes_set_page(rxdes, page);
693 ftmac100_rxdes_set_dma_addr(rxdes, map);
694 ftmac100_rxdes_set_buffer_size(rxdes,
RX_BUF_SIZE);
695 ftmac100_rxdes_set_dma_own(rxdes);
699 static void ftmac100_free_buffers(
struct ftmac100 *priv)
705 struct page *page = ftmac100_rxdes_get_page(rxdes);
706 dma_addr_t map = ftmac100_rxdes_get_dma_addr(rxdes);
717 struct sk_buff *skb = ftmac100_txdes_get_skb(txdes);
718 dma_addr_t map = ftmac100_txdes_get_dma_addr(txdes);
731 static int ftmac100_alloc_buffers(
struct ftmac100 *priv)
743 ftmac100_rxdes_set_end_of_ring(&priv->
descs->rxdes[RX_QUEUE_ENTRIES - 1]);
748 if (ftmac100_alloc_rx_page(priv, rxdes,
GFP_KERNEL))
753 ftmac100_txdes_set_end_of_ring(&priv->
descs->txdes[TX_QUEUE_ENTRIES - 1]);
757 ftmac100_free_buffers(priv);
766 struct ftmac100 *priv = netdev_priv(netdev);
776 for (i = 0; i < 10; i++) {
785 netdev_err(netdev,
"mdio read timed out\n");
789 static void ftmac100_mdio_write(
struct net_device *netdev,
int phy_id,
int reg,
792 struct ftmac100 *priv = netdev_priv(netdev);
805 for (i = 0; i < 10; i++) {
814 netdev_err(netdev,
"mdio write timed out\n");
820 static void ftmac100_get_drvinfo(
struct net_device *netdev,
830 struct ftmac100 *priv = netdev_priv(netdev);
836 struct ftmac100 *priv = netdev_priv(netdev);
840 static int ftmac100_nway_reset(
struct net_device *netdev)
842 struct ftmac100 *priv = netdev_priv(netdev);
848 struct ftmac100 *priv = netdev_priv(netdev);
852 static const struct ethtool_ops ftmac100_ethtool_ops = {
853 .set_settings = ftmac100_set_settings,
854 .get_settings = ftmac100_get_settings,
855 .get_drvinfo = ftmac100_get_drvinfo,
856 .nway_reset = ftmac100_nway_reset,
857 .get_link = ftmac100_get_link,
866 struct ftmac100 *priv = netdev_priv(netdev);
868 if (
likely(netif_running(netdev))) {
870 ftmac100_disable_all_int(priv);
871 napi_schedule(&priv->
napi);
901 retry = ftmac100_rx_packet(priv, &rx);
902 }
while (retry && rx < budget);
904 if (retry && rx == budget)
917 ftmac100_tx_complete(priv);
923 netdev_info(netdev,
"[ISR] = 0x%x: %s%s%s%s\n", status,
931 netdev->
stats.rx_over_errors++;
936 netdev->
stats.rx_fifo_errors++;
948 ftmac100_enable_all_int(priv);
957 static int ftmac100_open(
struct net_device *netdev)
959 struct ftmac100 *priv = netdev_priv(netdev);
962 err = ftmac100_alloc_buffers(priv);
964 netdev_err(netdev,
"failed to allocate buffers\n");
970 netdev_err(netdev,
"failed to request irq %d\n", priv->
irq);
979 err = ftmac100_start_hw(priv);
983 napi_enable(&priv->
napi);
984 netif_start_queue(netdev);
986 ftmac100_enable_all_int(priv);
993 ftmac100_free_buffers(priv);
998 static int ftmac100_stop(
struct net_device *netdev)
1000 struct ftmac100 *priv = netdev_priv(netdev);
1002 ftmac100_disable_all_int(priv);
1003 netif_stop_queue(netdev);
1004 napi_disable(&priv->
napi);
1005 ftmac100_stop_hw(priv);
1007 ftmac100_free_buffers(priv);
1012 static int ftmac100_hard_start_xmit(
struct sk_buff *skb,
struct net_device *netdev)
1014 struct ftmac100 *priv = netdev_priv(netdev);
1021 netdev->
stats.tx_dropped++;
1030 netdev_err(netdev,
"map socket buffer failed\n");
1032 netdev->
stats.tx_dropped++;
1037 return ftmac100_xmit(priv, skb, map);
1043 struct ftmac100 *priv = netdev_priv(netdev);
1050 .ndo_open = ftmac100_open,
1051 .ndo_stop = ftmac100_stop,
1052 .ndo_start_xmit = ftmac100_hard_start_xmit,
1055 .ndo_do_ioctl = ftmac100_do_ioctl,
1081 netdev = alloc_etherdev(
sizeof(*priv));
1084 goto err_alloc_etherdev;
1091 platform_set_drvdata(pdev, netdev);
1094 priv = netdev_priv(netdev);
1105 dev_name(&pdev->
dev));
1107 dev_err(&pdev->
dev,
"Could not reserve memory region\n");
1114 dev_err(&pdev->
dev,
"Failed to ioremap ethernet registers\n");
1122 priv->
mii.phy_id = 0;
1123 priv->
mii.phy_id_mask = 0x1f;
1124 priv->
mii.reg_num_mask = 0x1f;
1126 priv->
mii.mdio_read = ftmac100_mdio_read;
1127 priv->
mii.mdio_write = ftmac100_mdio_write;
1132 dev_err(&pdev->
dev,
"Failed to register netdev\n");
1133 goto err_register_netdev;
1136 netdev_info(netdev,
"irq %d, mapped at %p\n", priv->
irq, priv->
base);
1138 if (!is_valid_ether_addr(netdev->
dev_addr)) {
1139 eth_hw_addr_random(netdev);
1140 netdev_info(netdev,
"generated random MAC address %pM\n",
1146 err_register_netdev:
1152 platform_set_drvdata(pdev,
NULL);
1163 netdev = platform_get_drvdata(pdev);
1164 priv = netdev_priv(netdev);
1172 platform_set_drvdata(pdev,
NULL);
1178 .probe = ftmac100_probe,
1179 .remove =
__exit_p(ftmac100_remove),
1189 static int __init ftmac100_init(
void)
1195 static void __exit ftmac100_exit(
void)