Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Faraday FTGMAC100 Gigabit Ethernet |
| 3 | * |
| 4 | * (C) Copyright 2009-2011 Faraday Technology |
| 5 | * Po-Yu Chuang <ratbert@faraday-tech.com> |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
| 24 | #include <linux/dma-mapping.h> |
| 25 | #include <linux/etherdevice.h> |
| 26 | #include <linux/ethtool.h> |
Thomas Faber | 17f1bbc | 2012-01-18 13:45:44 +0000 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 28 | #include <linux/io.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/netdevice.h> |
Mark Brown | 3af887c | 2017-03-30 17:00:12 +0100 | [diff] [blame] | 31 | #include <linux/of.h> |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 32 | #include <linux/phy.h> |
| 33 | #include <linux/platform_device.h> |
Mark Brown | 3af887c | 2017-03-30 17:00:12 +0100 | [diff] [blame] | 34 | #include <linux/property.h> |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 35 | #include <net/ip.h> |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 36 | #include <net/ncsi.h> |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 37 | |
| 38 | #include "ftgmac100.h" |
| 39 | |
| 40 | #define DRV_NAME "ftgmac100" |
| 41 | #define DRV_VERSION "0.7" |
| 42 | |
| 43 | #define RX_QUEUE_ENTRIES 256 /* must be power of 2 */ |
| 44 | #define TX_QUEUE_ENTRIES 512 /* must be power of 2 */ |
| 45 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 46 | #define MAX_PKT_SIZE 1536 |
| 47 | #define RX_BUF_SIZE MAX_PKT_SIZE /* must be smaller than 0x3fff */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 48 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 49 | struct ftgmac100_descs { |
| 50 | struct ftgmac100_rxdes rxdes[RX_QUEUE_ENTRIES]; |
| 51 | struct ftgmac100_txdes txdes[TX_QUEUE_ENTRIES]; |
| 52 | }; |
| 53 | |
| 54 | struct ftgmac100 { |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 55 | /* Registers */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 56 | struct resource *res; |
| 57 | void __iomem *base; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 58 | |
| 59 | struct ftgmac100_descs *descs; |
| 60 | dma_addr_t descs_dma_addr; |
| 61 | |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 62 | /* Rx ring */ |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 63 | struct sk_buff *rx_skbs[RX_QUEUE_ENTRIES]; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 64 | unsigned int rx_pointer; |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 65 | u32 rxdes0_edorr_mask; |
| 66 | |
| 67 | /* Tx ring */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 68 | unsigned int tx_clean_pointer; |
| 69 | unsigned int tx_pointer; |
| 70 | unsigned int tx_pending; |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 71 | u32 txdes0_edotr_mask; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 72 | spinlock_t tx_lock; |
| 73 | |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 74 | /* Scratch page to use when rx skb alloc fails */ |
| 75 | void *rx_scratch; |
| 76 | dma_addr_t rx_scratch_dma; |
| 77 | |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 78 | /* Component structures */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 79 | struct net_device *netdev; |
| 80 | struct device *dev; |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 81 | struct ncsi_dev *ndev; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 82 | struct napi_struct napi; |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 83 | struct work_struct reset_task; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 84 | struct mii_bus *mii_bus; |
Andrew Jeffery | 7906a4d | 2016-09-22 08:34:59 +0930 | [diff] [blame] | 85 | |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 86 | /* Link management */ |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 87 | int cur_speed; |
| 88 | int cur_duplex; |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 89 | bool use_ncsi; |
| 90 | |
| 91 | /* Misc */ |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 92 | bool need_mac_restart; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 95 | static void ftgmac100_set_rx_ring_base(struct ftgmac100 *priv, dma_addr_t addr) |
| 96 | { |
| 97 | iowrite32(addr, priv->base + FTGMAC100_OFFSET_RXR_BADR); |
| 98 | } |
| 99 | |
| 100 | static void ftgmac100_set_rx_buffer_size(struct ftgmac100 *priv, |
| 101 | unsigned int size) |
| 102 | { |
| 103 | size = FTGMAC100_RBSR_SIZE(size); |
| 104 | iowrite32(size, priv->base + FTGMAC100_OFFSET_RBSR); |
| 105 | } |
| 106 | |
| 107 | static void ftgmac100_set_normal_prio_tx_ring_base(struct ftgmac100 *priv, |
| 108 | dma_addr_t addr) |
| 109 | { |
| 110 | iowrite32(addr, priv->base + FTGMAC100_OFFSET_NPTXR_BADR); |
| 111 | } |
| 112 | |
| 113 | static void ftgmac100_txdma_normal_prio_start_polling(struct ftgmac100 *priv) |
| 114 | { |
| 115 | iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD); |
| 116 | } |
| 117 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 118 | static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 119 | { |
| 120 | struct net_device *netdev = priv->netdev; |
| 121 | int i; |
| 122 | |
| 123 | /* NOTE: reset clears all registers */ |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 124 | iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR); |
| 125 | iowrite32(maccr | FTGMAC100_MACCR_SW_RST, |
| 126 | priv->base + FTGMAC100_OFFSET_MACCR); |
| 127 | for (i = 0; i < 50; i++) { |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 128 | unsigned int maccr; |
| 129 | |
| 130 | maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR); |
| 131 | if (!(maccr & FTGMAC100_MACCR_SW_RST)) |
| 132 | return 0; |
| 133 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 134 | udelay(1); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 137 | netdev_err(netdev, "Hardware reset failed\n"); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 138 | return -EIO; |
| 139 | } |
| 140 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 141 | static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv) |
| 142 | { |
| 143 | u32 maccr = 0; |
| 144 | |
| 145 | switch (priv->cur_speed) { |
| 146 | case SPEED_10: |
| 147 | case 0: /* no link */ |
| 148 | break; |
| 149 | |
| 150 | case SPEED_100: |
| 151 | maccr |= FTGMAC100_MACCR_FAST_MODE; |
| 152 | break; |
| 153 | |
| 154 | case SPEED_1000: |
| 155 | maccr |= FTGMAC100_MACCR_GIGA_MODE; |
| 156 | break; |
| 157 | default: |
| 158 | netdev_err(priv->netdev, "Unknown speed %d !\n", |
| 159 | priv->cur_speed); |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | /* (Re)initialize the queue pointers */ |
| 164 | priv->rx_pointer = 0; |
| 165 | priv->tx_clean_pointer = 0; |
| 166 | priv->tx_pointer = 0; |
| 167 | priv->tx_pending = 0; |
| 168 | |
| 169 | /* The doc says reset twice with 10us interval */ |
| 170 | if (ftgmac100_reset_mac(priv, maccr)) |
| 171 | return -EIO; |
| 172 | usleep_range(10, 1000); |
| 173 | return ftgmac100_reset_mac(priv, maccr); |
| 174 | } |
| 175 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 176 | static void ftgmac100_set_mac(struct ftgmac100 *priv, const unsigned char *mac) |
| 177 | { |
| 178 | unsigned int maddr = mac[0] << 8 | mac[1]; |
| 179 | unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5]; |
| 180 | |
| 181 | iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR); |
| 182 | iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR); |
| 183 | } |
| 184 | |
Gavin Shan | 113ce10 | 2016-07-19 11:54:22 +1000 | [diff] [blame] | 185 | static void ftgmac100_setup_mac(struct ftgmac100 *priv) |
| 186 | { |
| 187 | u8 mac[ETH_ALEN]; |
| 188 | unsigned int m; |
| 189 | unsigned int l; |
| 190 | void *addr; |
| 191 | |
| 192 | addr = device_get_mac_address(priv->dev, mac, ETH_ALEN); |
| 193 | if (addr) { |
| 194 | ether_addr_copy(priv->netdev->dev_addr, mac); |
| 195 | dev_info(priv->dev, "Read MAC address %pM from device tree\n", |
| 196 | mac); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR); |
| 201 | l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR); |
| 202 | |
| 203 | mac[0] = (m >> 8) & 0xff; |
| 204 | mac[1] = m & 0xff; |
| 205 | mac[2] = (l >> 24) & 0xff; |
| 206 | mac[3] = (l >> 16) & 0xff; |
| 207 | mac[4] = (l >> 8) & 0xff; |
| 208 | mac[5] = l & 0xff; |
| 209 | |
Gavin Shan | 113ce10 | 2016-07-19 11:54:22 +1000 | [diff] [blame] | 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); |
| 213 | } else { |
| 214 | eth_hw_addr_random(priv->netdev); |
| 215 | dev_info(priv->dev, "Generated random MAC address %pM\n", |
| 216 | priv->netdev->dev_addr); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static int ftgmac100_set_mac_addr(struct net_device *dev, void *p) |
| 221 | { |
| 222 | int ret; |
| 223 | |
| 224 | ret = eth_prepare_mac_addr_change(dev, p); |
| 225 | if (ret < 0) |
| 226 | return ret; |
| 227 | |
| 228 | eth_commit_mac_addr_change(dev, p); |
| 229 | ftgmac100_set_mac(netdev_priv(dev), dev->dev_addr); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 234 | static void ftgmac100_init_hw(struct ftgmac100 *priv) |
| 235 | { |
| 236 | /* setup ring buffer base registers */ |
| 237 | ftgmac100_set_rx_ring_base(priv, |
| 238 | priv->descs_dma_addr + |
| 239 | offsetof(struct ftgmac100_descs, rxdes)); |
| 240 | ftgmac100_set_normal_prio_tx_ring_base(priv, |
| 241 | priv->descs_dma_addr + |
| 242 | offsetof(struct ftgmac100_descs, txdes)); |
| 243 | |
| 244 | ftgmac100_set_rx_buffer_size(priv, RX_BUF_SIZE); |
| 245 | |
| 246 | iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1), priv->base + FTGMAC100_OFFSET_APTC); |
| 247 | |
| 248 | ftgmac100_set_mac(priv, priv->netdev->dev_addr); |
| 249 | } |
| 250 | |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 251 | static void ftgmac100_start_hw(struct ftgmac100 *priv) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 252 | { |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 253 | u32 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 254 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 255 | /* Keep the original GMAC and FAST bits */ |
| 256 | maccr &= (FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_GIGA_MODE); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 257 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 258 | /* Add all the main enable bits */ |
| 259 | maccr |= FTGMAC100_MACCR_TXDMA_EN | |
| 260 | FTGMAC100_MACCR_RXDMA_EN | |
| 261 | FTGMAC100_MACCR_TXMAC_EN | |
| 262 | FTGMAC100_MACCR_RXMAC_EN | |
| 263 | FTGMAC100_MACCR_CRC_APD | |
| 264 | FTGMAC100_MACCR_PHY_LINK_LEVEL | |
| 265 | FTGMAC100_MACCR_RX_RUNT | |
| 266 | FTGMAC100_MACCR_RX_BROADPKT; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 267 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 268 | /* Add other bits as needed */ |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 269 | if (priv->cur_duplex == DUPLEX_FULL) |
| 270 | maccr |= FTGMAC100_MACCR_FULLDUP; |
| 271 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 272 | /* Hit the HW */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 273 | iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR); |
| 274 | } |
| 275 | |
| 276 | static void ftgmac100_stop_hw(struct ftgmac100 *priv) |
| 277 | { |
| 278 | iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR); |
| 279 | } |
| 280 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 281 | static int ftgmac100_alloc_rx_buf(struct ftgmac100 *priv, unsigned int entry, |
| 282 | struct ftgmac100_rxdes *rxdes, gfp_t gfp) |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 283 | { |
| 284 | struct net_device *netdev = priv->netdev; |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 285 | struct sk_buff *skb; |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 286 | dma_addr_t map; |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 287 | int err; |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 288 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 289 | skb = netdev_alloc_skb_ip_align(netdev, RX_BUF_SIZE); |
| 290 | if (unlikely(!skb)) { |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 291 | if (net_ratelimit()) |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 292 | netdev_warn(netdev, "failed to allocate rx skb\n"); |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 293 | err = -ENOMEM; |
| 294 | map = priv->rx_scratch_dma; |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 295 | } else { |
| 296 | map = dma_map_single(priv->dev, skb->data, RX_BUF_SIZE, |
| 297 | DMA_FROM_DEVICE); |
| 298 | if (unlikely(dma_mapping_error(priv->dev, map))) { |
| 299 | if (net_ratelimit()) |
| 300 | netdev_err(netdev, "failed to map rx page\n"); |
| 301 | dev_kfree_skb_any(skb); |
| 302 | map = priv->rx_scratch_dma; |
| 303 | skb = NULL; |
| 304 | err = -ENOMEM; |
| 305 | } |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 306 | } |
| 307 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 308 | /* Store skb */ |
| 309 | priv->rx_skbs[entry] = skb; |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 310 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 311 | /* Store DMA address into RX desc */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 312 | rxdes->rxdes3 = cpu_to_le32(map); |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 313 | |
| 314 | /* Ensure the above is ordered vs clearing the OWN bit */ |
| 315 | dma_wmb(); |
| 316 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 317 | /* Clean status (which resets own bit) */ |
| 318 | if (entry == (RX_QUEUE_ENTRIES - 1)) |
| 319 | rxdes->rxdes0 = cpu_to_le32(priv->rxdes0_edorr_mask); |
| 320 | else |
| 321 | rxdes->rxdes0 = 0; |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 322 | |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 326 | static int ftgmac100_next_rx_pointer(int pointer) |
| 327 | { |
| 328 | return (pointer + 1) & (RX_QUEUE_ENTRIES - 1); |
| 329 | } |
| 330 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 331 | static void ftgmac100_rx_packet_error(struct ftgmac100 *priv, u32 status) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 332 | { |
| 333 | struct net_device *netdev = priv->netdev; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 334 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 335 | if (status & FTGMAC100_RXDES0_RX_ERR) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 336 | netdev->stats.rx_errors++; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 337 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 338 | if (status & FTGMAC100_RXDES0_CRC_ERR) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 339 | netdev->stats.rx_crc_errors++; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 340 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 341 | if (status & (FTGMAC100_RXDES0_FTL | |
| 342 | FTGMAC100_RXDES0_RUNT | |
| 343 | FTGMAC100_RXDES0_RX_ODD_NB)) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 344 | netdev->stats.rx_length_errors++; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 347 | static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed) |
| 348 | { |
| 349 | struct net_device *netdev = priv->netdev; |
| 350 | struct ftgmac100_rxdes *rxdes; |
| 351 | struct sk_buff *skb; |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 352 | unsigned int pointer, size; |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 353 | u32 status, csum_vlan; |
Benjamin Herrenschmidt | b1977bf | 2017-04-06 11:02:44 +1000 | [diff] [blame] | 354 | dma_addr_t map; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 355 | |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 356 | /* Grab next RX descriptor */ |
| 357 | pointer = priv->rx_pointer; |
| 358 | rxdes = &priv->descs->rxdes[pointer]; |
| 359 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 360 | /* Grab descriptor status */ |
| 361 | status = le32_to_cpu(rxdes->rxdes0); |
| 362 | |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 363 | /* Do we have a packet ? */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 364 | if (!(status & FTGMAC100_RXDES0_RXPKT_RDY)) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 365 | return false; |
| 366 | |
Benjamin Herrenschmidt | 027f426 | 2017-04-06 11:02:50 +1000 | [diff] [blame] | 367 | /* Order subsequent reads with the test for the ready bit */ |
| 368 | dma_rmb(); |
| 369 | |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 370 | /* We don't cope with fragmented RX packets */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 371 | if (unlikely(!(status & FTGMAC100_RXDES0_FRS) || |
| 372 | !(status & FTGMAC100_RXDES0_LRS))) |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 373 | goto drop; |
| 374 | |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 375 | /* Grab received size and csum vlan field in the descriptor */ |
| 376 | size = status & FTGMAC100_RXDES0_VDBC; |
| 377 | csum_vlan = le32_to_cpu(rxdes->rxdes1); |
| 378 | |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 379 | /* Any error (other than csum offload) flagged ? */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 380 | if (unlikely(status & RXDES0_ANY_ERROR)) { |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 381 | /* Correct for incorrect flagging of runt packets |
| 382 | * with vlan tags... Just accept a runt packet that |
| 383 | * has been flagged as vlan and whose size is at |
| 384 | * least 60 bytes. |
| 385 | */ |
| 386 | if ((status & FTGMAC100_RXDES0_RUNT) && |
| 387 | (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL) && |
| 388 | (size >= 60)) |
| 389 | status &= ~FTGMAC100_RXDES0_RUNT; |
| 390 | |
| 391 | /* Any error still in there ? */ |
| 392 | if (status & RXDES0_ANY_ERROR) { |
| 393 | ftgmac100_rx_packet_error(priv, status); |
| 394 | goto drop; |
| 395 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 398 | /* If the packet had no skb (failed to allocate earlier) |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 399 | * then try to allocate one and skip |
| 400 | */ |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 401 | skb = priv->rx_skbs[pointer]; |
| 402 | if (!unlikely(skb)) { |
| 403 | ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC); |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 404 | goto drop; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 407 | if (unlikely(status & FTGMAC100_RXDES0_MULTICAST)) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 408 | netdev->stats.multicast++; |
| 409 | |
Benjamin Herrenschmidt | 6720219 | 2017-04-06 11:02:46 +1000 | [diff] [blame] | 410 | /* If the HW found checksum errors, bounce it to software. |
| 411 | * |
| 412 | * If we didn't, we need to see if the packet was recognized |
| 413 | * by HW as one of the supported checksummed protocols before |
| 414 | * we accept the HW test results. |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 415 | */ |
Benjamin Herrenschmidt | 6720219 | 2017-04-06 11:02:46 +1000 | [diff] [blame] | 416 | if (netdev->features & NETIF_F_RXCSUM) { |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 417 | u32 err_bits = FTGMAC100_RXDES1_TCP_CHKSUM_ERR | |
| 418 | FTGMAC100_RXDES1_UDP_CHKSUM_ERR | |
| 419 | FTGMAC100_RXDES1_IP_CHKSUM_ERR; |
Benjamin Herrenschmidt | 6720219 | 2017-04-06 11:02:46 +1000 | [diff] [blame] | 420 | if ((csum_vlan & err_bits) || |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 421 | !(csum_vlan & FTGMAC100_RXDES1_PROT_MASK)) |
Benjamin Herrenschmidt | 6720219 | 2017-04-06 11:02:46 +1000 | [diff] [blame] | 422 | skb->ip_summed = CHECKSUM_NONE; |
| 423 | else |
| 424 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 425 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 426 | |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 427 | /* Transfer received size to skb */ |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 428 | skb_put(skb, size); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 429 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 430 | /* Tear down DMA mapping, do necessary cache management */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 431 | map = le32_to_cpu(rxdes->rxdes3); |
| 432 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 433 | #if defined(CONFIG_ARM) && !defined(CONFIG_ARM_DMA_USE_IOMMU) |
| 434 | /* When we don't have an iommu, we can save cycles by not |
| 435 | * invalidating the cache for the part of the packet that |
| 436 | * wasn't received. |
| 437 | */ |
| 438 | dma_unmap_single(priv->dev, map, size, DMA_FROM_DEVICE); |
| 439 | #else |
| 440 | dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE); |
| 441 | #endif |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 442 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 443 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 444 | /* Resplenish rx ring */ |
| 445 | ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC); |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 446 | priv->rx_pointer = ftgmac100_next_rx_pointer(pointer); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 447 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 448 | skb->protocol = eth_type_trans(skb, netdev); |
| 449 | |
| 450 | netdev->stats.rx_packets++; |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 451 | netdev->stats.rx_bytes += size; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 452 | |
| 453 | /* push packet to protocol stack */ |
Benjamin Herrenschmidt | 6720219 | 2017-04-06 11:02:46 +1000 | [diff] [blame] | 454 | if (skb->ip_summed == CHECKSUM_NONE) |
| 455 | netif_receive_skb(skb); |
| 456 | else |
| 457 | napi_gro_receive(&priv->napi, skb); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 458 | |
| 459 | (*processed)++; |
| 460 | return true; |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 461 | |
| 462 | drop: |
| 463 | /* Clean rxdes0 (which resets own bit) */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 464 | rxdes->rxdes0 = cpu_to_le32(status & priv->rxdes0_edorr_mask); |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 465 | priv->rx_pointer = ftgmac100_next_rx_pointer(pointer); |
| 466 | netdev->stats.rx_dropped++; |
| 467 | return true; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Andrew Jeffery | 7906a4d | 2016-09-22 08:34:59 +0930 | [diff] [blame] | 470 | static void ftgmac100_txdes_reset(const struct ftgmac100 *priv, |
| 471 | struct ftgmac100_txdes *txdes) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 472 | { |
| 473 | /* clear all except end of ring bit */ |
Andrew Jeffery | 7906a4d | 2016-09-22 08:34:59 +0930 | [diff] [blame] | 474 | txdes->txdes0 &= cpu_to_le32(priv->txdes0_edotr_mask); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 475 | txdes->txdes1 = 0; |
| 476 | txdes->txdes2 = 0; |
| 477 | txdes->txdes3 = 0; |
| 478 | } |
| 479 | |
| 480 | static bool ftgmac100_txdes_owned_by_dma(struct ftgmac100_txdes *txdes) |
| 481 | { |
| 482 | return txdes->txdes0 & cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN); |
| 483 | } |
| 484 | |
| 485 | static void ftgmac100_txdes_set_dma_own(struct ftgmac100_txdes *txdes) |
| 486 | { |
| 487 | /* |
| 488 | * Make sure dma own bit will not be set before any other |
| 489 | * descriptor fields. |
| 490 | */ |
| 491 | wmb(); |
| 492 | txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN); |
| 493 | } |
| 494 | |
Andrew Jeffery | 7906a4d | 2016-09-22 08:34:59 +0930 | [diff] [blame] | 495 | static void ftgmac100_txdes_set_end_of_ring(const struct ftgmac100 *priv, |
| 496 | struct ftgmac100_txdes *txdes) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 497 | { |
Andrew Jeffery | 7906a4d | 2016-09-22 08:34:59 +0930 | [diff] [blame] | 498 | txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static void ftgmac100_txdes_set_first_segment(struct ftgmac100_txdes *txdes) |
| 502 | { |
| 503 | txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_FTS); |
| 504 | } |
| 505 | |
| 506 | static void ftgmac100_txdes_set_last_segment(struct ftgmac100_txdes *txdes) |
| 507 | { |
| 508 | txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_LTS); |
| 509 | } |
| 510 | |
| 511 | static void ftgmac100_txdes_set_buffer_size(struct ftgmac100_txdes *txdes, |
| 512 | unsigned int len) |
| 513 | { |
| 514 | txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXBUF_SIZE(len)); |
| 515 | } |
| 516 | |
| 517 | static void ftgmac100_txdes_set_txint(struct ftgmac100_txdes *txdes) |
| 518 | { |
| 519 | txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TXIC); |
| 520 | } |
| 521 | |
| 522 | static void ftgmac100_txdes_set_tcpcs(struct ftgmac100_txdes *txdes) |
| 523 | { |
| 524 | txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TCP_CHKSUM); |
| 525 | } |
| 526 | |
| 527 | static void ftgmac100_txdes_set_udpcs(struct ftgmac100_txdes *txdes) |
| 528 | { |
| 529 | txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_UDP_CHKSUM); |
| 530 | } |
| 531 | |
| 532 | static void ftgmac100_txdes_set_ipcs(struct ftgmac100_txdes *txdes) |
| 533 | { |
| 534 | txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_IP_CHKSUM); |
| 535 | } |
| 536 | |
| 537 | static void ftgmac100_txdes_set_dma_addr(struct ftgmac100_txdes *txdes, |
| 538 | dma_addr_t addr) |
| 539 | { |
| 540 | txdes->txdes3 = cpu_to_le32(addr); |
| 541 | } |
| 542 | |
| 543 | static dma_addr_t ftgmac100_txdes_get_dma_addr(struct ftgmac100_txdes *txdes) |
| 544 | { |
| 545 | return le32_to_cpu(txdes->txdes3); |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * txdes2 is not used by hardware. We use it to keep track of socket buffer. |
| 550 | * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu(). |
| 551 | */ |
| 552 | static void ftgmac100_txdes_set_skb(struct ftgmac100_txdes *txdes, |
| 553 | struct sk_buff *skb) |
| 554 | { |
| 555 | txdes->txdes2 = (unsigned int)skb; |
| 556 | } |
| 557 | |
| 558 | static struct sk_buff *ftgmac100_txdes_get_skb(struct ftgmac100_txdes *txdes) |
| 559 | { |
| 560 | return (struct sk_buff *)txdes->txdes2; |
| 561 | } |
| 562 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 563 | static int ftgmac100_next_tx_pointer(int pointer) |
| 564 | { |
| 565 | return (pointer + 1) & (TX_QUEUE_ENTRIES - 1); |
| 566 | } |
| 567 | |
| 568 | static void ftgmac100_tx_pointer_advance(struct ftgmac100 *priv) |
| 569 | { |
| 570 | priv->tx_pointer = ftgmac100_next_tx_pointer(priv->tx_pointer); |
| 571 | } |
| 572 | |
| 573 | static void ftgmac100_tx_clean_pointer_advance(struct ftgmac100 *priv) |
| 574 | { |
| 575 | priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv->tx_clean_pointer); |
| 576 | } |
| 577 | |
| 578 | static struct ftgmac100_txdes *ftgmac100_current_txdes(struct ftgmac100 *priv) |
| 579 | { |
| 580 | return &priv->descs->txdes[priv->tx_pointer]; |
| 581 | } |
| 582 | |
| 583 | static struct ftgmac100_txdes * |
| 584 | ftgmac100_current_clean_txdes(struct ftgmac100 *priv) |
| 585 | { |
| 586 | return &priv->descs->txdes[priv->tx_clean_pointer]; |
| 587 | } |
| 588 | |
| 589 | static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv) |
| 590 | { |
| 591 | struct net_device *netdev = priv->netdev; |
| 592 | struct ftgmac100_txdes *txdes; |
| 593 | struct sk_buff *skb; |
| 594 | dma_addr_t map; |
| 595 | |
| 596 | if (priv->tx_pending == 0) |
| 597 | return false; |
| 598 | |
| 599 | txdes = ftgmac100_current_clean_txdes(priv); |
| 600 | |
| 601 | if (ftgmac100_txdes_owned_by_dma(txdes)) |
| 602 | return false; |
| 603 | |
| 604 | skb = ftgmac100_txdes_get_skb(txdes); |
| 605 | map = ftgmac100_txdes_get_dma_addr(txdes); |
| 606 | |
| 607 | netdev->stats.tx_packets++; |
| 608 | netdev->stats.tx_bytes += skb->len; |
| 609 | |
| 610 | dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE); |
| 611 | |
| 612 | dev_kfree_skb(skb); |
| 613 | |
Andrew Jeffery | 7906a4d | 2016-09-22 08:34:59 +0930 | [diff] [blame] | 614 | ftgmac100_txdes_reset(priv, txdes); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 615 | |
| 616 | ftgmac100_tx_clean_pointer_advance(priv); |
| 617 | |
| 618 | spin_lock(&priv->tx_lock); |
| 619 | priv->tx_pending--; |
| 620 | spin_unlock(&priv->tx_lock); |
| 621 | netif_wake_queue(netdev); |
| 622 | |
| 623 | return true; |
| 624 | } |
| 625 | |
| 626 | static void ftgmac100_tx_complete(struct ftgmac100 *priv) |
| 627 | { |
| 628 | while (ftgmac100_tx_complete_packet(priv)) |
| 629 | ; |
| 630 | } |
| 631 | |
Benjamin Herrenschmidt | 43b25ee | 2017-04-10 11:15:17 +1000 | [diff] [blame^] | 632 | static int ftgmac100_hard_start_xmit(struct sk_buff *skb, |
| 633 | struct net_device *netdev) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 634 | { |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 635 | unsigned int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len; |
Benjamin Herrenschmidt | 43b25ee | 2017-04-10 11:15:17 +1000 | [diff] [blame^] | 636 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 637 | struct ftgmac100_txdes *txdes; |
| 638 | dma_addr_t map; |
| 639 | |
| 640 | if (unlikely(skb->len > MAX_PKT_SIZE)) { |
| 641 | if (net_ratelimit()) |
| 642 | netdev_dbg(netdev, "tx packet too big\n"); |
| 643 | |
| 644 | netdev->stats.tx_dropped++; |
| 645 | kfree_skb(skb); |
| 646 | return NETDEV_TX_OK; |
| 647 | } |
| 648 | |
| 649 | map = dma_map_single(priv->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE); |
| 650 | if (unlikely(dma_mapping_error(priv->dev, map))) { |
| 651 | /* drop packet */ |
| 652 | if (net_ratelimit()) |
| 653 | netdev_err(netdev, "map socket buffer failed\n"); |
| 654 | |
| 655 | netdev->stats.tx_dropped++; |
| 656 | kfree_skb(skb); |
| 657 | return NETDEV_TX_OK; |
| 658 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 659 | |
| 660 | txdes = ftgmac100_current_txdes(priv); |
| 661 | ftgmac100_tx_pointer_advance(priv); |
| 662 | |
| 663 | /* setup TX descriptor */ |
| 664 | ftgmac100_txdes_set_skb(txdes, skb); |
| 665 | ftgmac100_txdes_set_dma_addr(txdes, map); |
| 666 | ftgmac100_txdes_set_buffer_size(txdes, len); |
| 667 | |
| 668 | ftgmac100_txdes_set_first_segment(txdes); |
| 669 | ftgmac100_txdes_set_last_segment(txdes); |
| 670 | ftgmac100_txdes_set_txint(txdes); |
| 671 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 672 | __be16 protocol = skb->protocol; |
| 673 | |
| 674 | if (protocol == cpu_to_be16(ETH_P_IP)) { |
| 675 | u8 ip_proto = ip_hdr(skb)->protocol; |
| 676 | |
| 677 | ftgmac100_txdes_set_ipcs(txdes); |
| 678 | if (ip_proto == IPPROTO_TCP) |
| 679 | ftgmac100_txdes_set_tcpcs(txdes); |
| 680 | else if (ip_proto == IPPROTO_UDP) |
| 681 | ftgmac100_txdes_set_udpcs(txdes); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | spin_lock(&priv->tx_lock); |
| 686 | priv->tx_pending++; |
| 687 | if (priv->tx_pending == TX_QUEUE_ENTRIES) |
| 688 | netif_stop_queue(netdev); |
| 689 | |
| 690 | /* start transmit */ |
| 691 | ftgmac100_txdes_set_dma_own(txdes); |
| 692 | spin_unlock(&priv->tx_lock); |
| 693 | |
| 694 | ftgmac100_txdma_normal_prio_start_polling(priv); |
| 695 | |
| 696 | return NETDEV_TX_OK; |
| 697 | } |
| 698 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 699 | static void ftgmac100_free_buffers(struct ftgmac100 *priv) |
| 700 | { |
| 701 | int i; |
| 702 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 703 | /* Free all RX buffers */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 704 | for (i = 0; i < RX_QUEUE_ENTRIES; i++) { |
| 705 | struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i]; |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 706 | struct sk_buff *skb = priv->rx_skbs[i]; |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 707 | dma_addr_t map = le32_to_cpu(rxdes->rxdes3); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 708 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 709 | if (!skb) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 710 | continue; |
| 711 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 712 | priv->rx_skbs[i] = NULL; |
| 713 | dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE); |
| 714 | dev_kfree_skb_any(skb); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 717 | /* Free all TX buffers */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 718 | for (i = 0; i < TX_QUEUE_ENTRIES; i++) { |
| 719 | struct ftgmac100_txdes *txdes = &priv->descs->txdes[i]; |
| 720 | struct sk_buff *skb = ftgmac100_txdes_get_skb(txdes); |
| 721 | dma_addr_t map = ftgmac100_txdes_get_dma_addr(txdes); |
| 722 | |
| 723 | if (!skb) |
| 724 | continue; |
| 725 | |
| 726 | dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE); |
Eric Dumazet | 0113e34 | 2014-01-16 23:38:24 -0800 | [diff] [blame] | 727 | kfree_skb(skb); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 728 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 731 | static void ftgmac100_free_rings(struct ftgmac100 *priv) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 732 | { |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 733 | /* Free descriptors */ |
| 734 | if (priv->descs) |
| 735 | dma_free_coherent(priv->dev, sizeof(struct ftgmac100_descs), |
| 736 | priv->descs, priv->descs_dma_addr); |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 737 | |
| 738 | /* Free scratch packet buffer */ |
| 739 | if (priv->rx_scratch) |
| 740 | dma_free_coherent(priv->dev, RX_BUF_SIZE, |
| 741 | priv->rx_scratch, priv->rx_scratch_dma); |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 742 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 743 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 744 | static int ftgmac100_alloc_rings(struct ftgmac100 *priv) |
| 745 | { |
| 746 | /* Allocate descriptors */ |
Joe Perches | ede23fa8 | 2013-08-26 22:45:23 -0700 | [diff] [blame] | 747 | priv->descs = dma_zalloc_coherent(priv->dev, |
| 748 | sizeof(struct ftgmac100_descs), |
| 749 | &priv->descs_dma_addr, GFP_KERNEL); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 750 | if (!priv->descs) |
| 751 | return -ENOMEM; |
| 752 | |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 753 | /* Allocate scratch packet buffer */ |
| 754 | priv->rx_scratch = dma_alloc_coherent(priv->dev, |
| 755 | RX_BUF_SIZE, |
| 756 | &priv->rx_scratch_dma, |
| 757 | GFP_KERNEL); |
| 758 | if (!priv->rx_scratch) |
| 759 | return -ENOMEM; |
| 760 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | static void ftgmac100_init_rings(struct ftgmac100 *priv) |
| 765 | { |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 766 | struct ftgmac100_rxdes *rxdes; |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 767 | int i; |
| 768 | |
| 769 | /* Initialize RX ring */ |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 770 | for (i = 0; i < RX_QUEUE_ENTRIES; i++) { |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 771 | rxdes = &priv->descs->rxdes[i]; |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 772 | rxdes->rxdes0 = 0; |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 773 | rxdes->rxdes3 = cpu_to_le32(priv->rx_scratch_dma); |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 774 | } |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 775 | /* Mark the end of the ring */ |
| 776 | rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask); |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 777 | |
| 778 | /* Initialize TX ring */ |
| 779 | for (i = 0; i < TX_QUEUE_ENTRIES; i++) |
| 780 | priv->descs->txdes[i].txdes0 = 0; |
| 781 | ftgmac100_txdes_set_end_of_ring(priv, &priv->descs->txdes[i -1]); |
| 782 | } |
| 783 | |
| 784 | static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv) |
| 785 | { |
| 786 | int i; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 787 | |
| 788 | for (i = 0; i < RX_QUEUE_ENTRIES; i++) { |
| 789 | struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i]; |
| 790 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 791 | if (ftgmac100_alloc_rx_buf(priv, i, rxdes, GFP_KERNEL)) |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 792 | return -ENOMEM; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 793 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 794 | return 0; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 797 | static void ftgmac100_adjust_link(struct net_device *netdev) |
| 798 | { |
| 799 | struct ftgmac100 *priv = netdev_priv(netdev); |
Philippe Reynes | b3c40ad | 2016-05-16 01:35:13 +0200 | [diff] [blame] | 800 | struct phy_device *phydev = netdev->phydev; |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 801 | int new_speed; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 802 | |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 803 | /* We store "no link" as speed 0 */ |
| 804 | if (!phydev->link) |
| 805 | new_speed = 0; |
| 806 | else |
| 807 | new_speed = phydev->speed; |
| 808 | |
| 809 | if (phydev->speed == priv->cur_speed && |
| 810 | phydev->duplex == priv->cur_duplex) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 811 | return; |
| 812 | |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 813 | /* Print status if we have a link or we had one and just lost it, |
| 814 | * don't print otherwise. |
| 815 | */ |
| 816 | if (new_speed || priv->cur_speed) |
| 817 | phy_print_status(phydev); |
| 818 | |
| 819 | priv->cur_speed = new_speed; |
| 820 | priv->cur_duplex = phydev->duplex; |
| 821 | |
| 822 | /* Link is down, do nothing else */ |
| 823 | if (!new_speed) |
| 824 | return; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 825 | |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 826 | /* Disable all interrupts */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 827 | iowrite32(0, priv->base + FTGMAC100_OFFSET_IER); |
| 828 | |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 829 | /* Reset the adapter asynchronously */ |
| 830 | schedule_work(&priv->reset_task); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | static int ftgmac100_mii_probe(struct ftgmac100 *priv) |
| 834 | { |
| 835 | struct net_device *netdev = priv->netdev; |
Guenter Roeck | e574f39 | 2016-01-10 12:04:32 -0800 | [diff] [blame] | 836 | struct phy_device *phydev; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 837 | |
Guenter Roeck | e574f39 | 2016-01-10 12:04:32 -0800 | [diff] [blame] | 838 | phydev = phy_find_first(priv->mii_bus); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 839 | if (!phydev) { |
| 840 | netdev_info(netdev, "%s: no PHY found\n", netdev->name); |
| 841 | return -ENODEV; |
| 842 | } |
| 843 | |
Andrew Lunn | 84eff6d | 2016-01-06 20:11:10 +0100 | [diff] [blame] | 844 | phydev = phy_connect(netdev, phydev_name(phydev), |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 845 | &ftgmac100_adjust_link, PHY_INTERFACE_MODE_GMII); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 846 | |
| 847 | if (IS_ERR(phydev)) { |
| 848 | netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name); |
| 849 | return PTR_ERR(phydev); |
| 850 | } |
| 851 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 852 | return 0; |
| 853 | } |
| 854 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 855 | static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum) |
| 856 | { |
| 857 | struct net_device *netdev = bus->priv; |
| 858 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 859 | unsigned int phycr; |
| 860 | int i; |
| 861 | |
| 862 | phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR); |
| 863 | |
| 864 | /* preserve MDC cycle threshold */ |
| 865 | phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK; |
| 866 | |
| 867 | phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) | |
| 868 | FTGMAC100_PHYCR_REGAD(regnum) | |
| 869 | FTGMAC100_PHYCR_MIIRD; |
| 870 | |
| 871 | iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR); |
| 872 | |
| 873 | for (i = 0; i < 10; i++) { |
| 874 | phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR); |
| 875 | |
| 876 | if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) { |
| 877 | int data; |
| 878 | |
| 879 | data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA); |
| 880 | return FTGMAC100_PHYDATA_MIIRDATA(data); |
| 881 | } |
| 882 | |
| 883 | udelay(100); |
| 884 | } |
| 885 | |
| 886 | netdev_err(netdev, "mdio read timed out\n"); |
| 887 | return -EIO; |
| 888 | } |
| 889 | |
| 890 | static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr, |
| 891 | int regnum, u16 value) |
| 892 | { |
| 893 | struct net_device *netdev = bus->priv; |
| 894 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 895 | unsigned int phycr; |
| 896 | int data; |
| 897 | int i; |
| 898 | |
| 899 | phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR); |
| 900 | |
| 901 | /* preserve MDC cycle threshold */ |
| 902 | phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK; |
| 903 | |
| 904 | phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) | |
| 905 | FTGMAC100_PHYCR_REGAD(regnum) | |
| 906 | FTGMAC100_PHYCR_MIIWR; |
| 907 | |
| 908 | data = FTGMAC100_PHYDATA_MIIWDATA(value); |
| 909 | |
| 910 | iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA); |
| 911 | iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR); |
| 912 | |
| 913 | for (i = 0; i < 10; i++) { |
| 914 | phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR); |
| 915 | |
| 916 | if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0) |
| 917 | return 0; |
| 918 | |
| 919 | udelay(100); |
| 920 | } |
| 921 | |
| 922 | netdev_err(netdev, "mdio write timed out\n"); |
| 923 | return -EIO; |
| 924 | } |
| 925 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 926 | static void ftgmac100_get_drvinfo(struct net_device *netdev, |
| 927 | struct ethtool_drvinfo *info) |
| 928 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 929 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 930 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); |
| 931 | strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info)); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 934 | static const struct ethtool_ops ftgmac100_ethtool_ops = { |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 935 | .get_drvinfo = ftgmac100_get_drvinfo, |
| 936 | .get_link = ethtool_op_get_link, |
Philippe Reynes | fd24d72 | 2016-05-16 01:35:14 +0200 | [diff] [blame] | 937 | .get_link_ksettings = phy_ethtool_get_link_ksettings, |
| 938 | .set_link_ksettings = phy_ethtool_set_link_ksettings, |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 939 | }; |
| 940 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 941 | static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id) |
| 942 | { |
| 943 | struct net_device *netdev = dev_id; |
| 944 | struct ftgmac100 *priv = netdev_priv(netdev); |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 945 | unsigned int status, new_mask = FTGMAC100_INT_BAD; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 946 | |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 947 | /* Fetch and clear interrupt bits, process abnormal ones */ |
| 948 | status = ioread32(priv->base + FTGMAC100_OFFSET_ISR); |
| 949 | iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR); |
| 950 | if (unlikely(status & FTGMAC100_INT_BAD)) { |
| 951 | |
| 952 | /* RX buffer unavailable */ |
| 953 | if (status & FTGMAC100_INT_NO_RXBUF) |
| 954 | netdev->stats.rx_over_errors++; |
| 955 | |
| 956 | /* received packet lost due to RX FIFO full */ |
| 957 | if (status & FTGMAC100_INT_RPKT_LOST) |
| 958 | netdev->stats.rx_fifo_errors++; |
| 959 | |
| 960 | /* sent packet lost due to excessive TX collision */ |
| 961 | if (status & FTGMAC100_INT_XPKT_LOST) |
| 962 | netdev->stats.tx_fifo_errors++; |
| 963 | |
| 964 | /* AHB error -> Reset the chip */ |
| 965 | if (status & FTGMAC100_INT_AHB_ERR) { |
| 966 | if (net_ratelimit()) |
| 967 | netdev_warn(netdev, |
| 968 | "AHB bus error ! Resetting chip.\n"); |
| 969 | iowrite32(0, priv->base + FTGMAC100_OFFSET_IER); |
| 970 | schedule_work(&priv->reset_task); |
| 971 | return IRQ_HANDLED; |
| 972 | } |
| 973 | |
| 974 | /* We may need to restart the MAC after such errors, delay |
| 975 | * this until after we have freed some Rx buffers though |
| 976 | */ |
| 977 | priv->need_mac_restart = true; |
| 978 | |
| 979 | /* Disable those errors until we restart */ |
| 980 | new_mask &= ~status; |
| 981 | } |
| 982 | |
| 983 | /* Only enable "bad" interrupts while NAPI is on */ |
| 984 | iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER); |
| 985 | |
| 986 | /* Schedule NAPI bh */ |
| 987 | napi_schedule_irqoff(&priv->napi); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 988 | |
| 989 | return IRQ_HANDLED; |
| 990 | } |
| 991 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 992 | static bool ftgmac100_check_rx(struct ftgmac100 *priv) |
| 993 | { |
| 994 | struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[priv->rx_pointer]; |
| 995 | |
| 996 | /* Do we have a packet ? */ |
| 997 | return !!(rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY)); |
| 998 | } |
| 999 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1000 | static int ftgmac100_poll(struct napi_struct *napi, int budget) |
| 1001 | { |
| 1002 | struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi); |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1003 | bool more, completed = true; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1004 | int rx = 0; |
| 1005 | |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1006 | ftgmac100_tx_complete(priv); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1007 | |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1008 | do { |
| 1009 | more = ftgmac100_rx_packet(priv, &rx); |
| 1010 | } while (more && rx < budget); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1011 | |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1012 | if (more && rx == budget) |
| 1013 | completed = false; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1014 | |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1015 | |
| 1016 | /* The interrupt is telling us to kick the MAC back to life |
| 1017 | * after an RX overflow |
| 1018 | */ |
| 1019 | if (unlikely(priv->need_mac_restart)) { |
| 1020 | ftgmac100_start_hw(priv); |
| 1021 | |
| 1022 | /* Re-enable "bad" interrupts */ |
| 1023 | iowrite32(FTGMAC100_INT_BAD, |
| 1024 | priv->base + FTGMAC100_OFFSET_IER); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1027 | /* Keep NAPI going if we have still packets to reclaim */ |
| 1028 | if (priv->tx_pending) |
| 1029 | return budget; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1030 | |
| 1031 | if (completed) { |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1032 | /* We are about to re-enable all interrupts. However |
| 1033 | * the HW has been latching RX/TX packet interrupts while |
| 1034 | * they were masked. So we clear them first, then we need |
| 1035 | * to re-check if there's something to process |
| 1036 | */ |
| 1037 | iowrite32(FTGMAC100_INT_RXTX, |
| 1038 | priv->base + FTGMAC100_OFFSET_ISR); |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 1039 | if (ftgmac100_check_rx(priv) || priv->tx_pending) |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1040 | return budget; |
| 1041 | |
| 1042 | /* deschedule NAPI */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1043 | napi_complete(napi); |
| 1044 | |
| 1045 | /* enable all interrupts */ |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1046 | iowrite32(FTGMAC100_INT_ALL, |
Gavin Shan | fc6061c | 2016-07-19 11:54:25 +1000 | [diff] [blame] | 1047 | priv->base + FTGMAC100_OFFSET_IER); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | return rx; |
| 1051 | } |
| 1052 | |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1053 | static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err) |
| 1054 | { |
| 1055 | int err = 0; |
| 1056 | |
| 1057 | /* Re-init descriptors (adjust queue sizes) */ |
| 1058 | ftgmac100_init_rings(priv); |
| 1059 | |
| 1060 | /* Realloc rx descriptors */ |
| 1061 | err = ftgmac100_alloc_rx_buffers(priv); |
| 1062 | if (err && !ignore_alloc_err) |
| 1063 | return err; |
| 1064 | |
| 1065 | /* Reinit and restart HW */ |
| 1066 | ftgmac100_init_hw(priv); |
| 1067 | ftgmac100_start_hw(priv); |
| 1068 | |
| 1069 | /* Re-enable the device */ |
| 1070 | napi_enable(&priv->napi); |
| 1071 | netif_start_queue(priv->netdev); |
| 1072 | |
| 1073 | /* Enable all interrupts */ |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1074 | iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER); |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1075 | |
| 1076 | return err; |
| 1077 | } |
| 1078 | |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1079 | static void ftgmac100_reset_task(struct work_struct *work) |
| 1080 | { |
| 1081 | struct ftgmac100 *priv = container_of(work, struct ftgmac100, |
| 1082 | reset_task); |
| 1083 | struct net_device *netdev = priv->netdev; |
| 1084 | int err; |
| 1085 | |
| 1086 | netdev_dbg(netdev, "Resetting NIC...\n"); |
| 1087 | |
| 1088 | /* Lock the world */ |
| 1089 | rtnl_lock(); |
| 1090 | if (netdev->phydev) |
| 1091 | mutex_lock(&netdev->phydev->lock); |
| 1092 | if (priv->mii_bus) |
| 1093 | mutex_lock(&priv->mii_bus->mdio_lock); |
| 1094 | |
| 1095 | |
| 1096 | /* Check if the interface is still up */ |
| 1097 | if (!netif_running(netdev)) |
| 1098 | goto bail; |
| 1099 | |
| 1100 | /* Stop the network stack */ |
| 1101 | netif_trans_update(netdev); |
| 1102 | napi_disable(&priv->napi); |
| 1103 | netif_tx_disable(netdev); |
| 1104 | |
| 1105 | /* Stop and reset the MAC */ |
| 1106 | ftgmac100_stop_hw(priv); |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 1107 | err = ftgmac100_reset_and_config_mac(priv); |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1108 | if (err) { |
| 1109 | /* Not much we can do ... it might come back... */ |
| 1110 | netdev_err(netdev, "attempting to continue...\n"); |
| 1111 | } |
| 1112 | |
| 1113 | /* Free all rx and tx buffers */ |
| 1114 | ftgmac100_free_buffers(priv); |
| 1115 | |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1116 | /* Setup everything again and restart chip */ |
| 1117 | ftgmac100_init_all(priv, true); |
| 1118 | |
| 1119 | netdev_dbg(netdev, "Reset done !\n"); |
| 1120 | bail: |
| 1121 | if (priv->mii_bus) |
| 1122 | mutex_unlock(&priv->mii_bus->mdio_lock); |
| 1123 | if (netdev->phydev) |
| 1124 | mutex_unlock(&netdev->phydev->lock); |
| 1125 | rtnl_unlock(); |
| 1126 | } |
| 1127 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1128 | static int ftgmac100_open(struct net_device *netdev) |
| 1129 | { |
| 1130 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 1131 | int err; |
| 1132 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 1133 | /* Allocate ring buffers */ |
| 1134 | err = ftgmac100_alloc_rings(priv); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1135 | if (err) { |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 1136 | netdev_err(netdev, "Failed to allocate descriptors\n"); |
| 1137 | return err; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 1140 | /* When using NC-SI we force the speed to 100Mbit/s full duplex, |
| 1141 | * |
| 1142 | * Otherwise we leave it set to 0 (no link), the link |
| 1143 | * message from the PHY layer will handle setting it up to |
| 1144 | * something else if needed. |
| 1145 | */ |
| 1146 | if (priv->use_ncsi) { |
| 1147 | priv->cur_duplex = DUPLEX_FULL; |
| 1148 | priv->cur_speed = SPEED_100; |
| 1149 | } else { |
| 1150 | priv->cur_duplex = 0; |
| 1151 | priv->cur_speed = 0; |
| 1152 | } |
| 1153 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 1154 | /* Reset the hardware */ |
| 1155 | err = ftgmac100_reset_and_config_mac(priv); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1156 | if (err) |
| 1157 | goto err_hw; |
| 1158 | |
Benjamin Herrenschmidt | b8dbecf | 2017-04-05 12:28:47 +1000 | [diff] [blame] | 1159 | /* Initialize NAPI */ |
| 1160 | netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64); |
| 1161 | |
Benjamin Herrenschmidt | 81f1eca | 2017-04-05 12:28:48 +1000 | [diff] [blame] | 1162 | /* Grab our interrupt */ |
| 1163 | err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev); |
| 1164 | if (err) { |
| 1165 | netdev_err(netdev, "failed to request irq %d\n", netdev->irq); |
| 1166 | goto err_irq; |
| 1167 | } |
| 1168 | |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1169 | /* Start things up */ |
| 1170 | err = ftgmac100_init_all(priv, false); |
| 1171 | if (err) { |
| 1172 | netdev_err(netdev, "Failed to allocate packet buffers\n"); |
| 1173 | goto err_alloc; |
| 1174 | } |
Gavin Shan | 08c9c12 | 2016-09-22 08:35:01 +0930 | [diff] [blame] | 1175 | |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1176 | if (netdev->phydev) { |
| 1177 | /* If we have a PHY, start polling */ |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1178 | phy_start(netdev->phydev); |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1179 | } else if (priv->use_ncsi) { |
| 1180 | /* If using NC-SI, set our carrier on and start the stack */ |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1181 | netif_carrier_on(netdev); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1182 | |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1183 | /* Start the NCSI device */ |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1184 | err = ncsi_start_dev(priv->ndev); |
| 1185 | if (err) |
| 1186 | goto err_ncsi; |
| 1187 | } |
| 1188 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1189 | return 0; |
| 1190 | |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1191 | err_ncsi: |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1192 | napi_disable(&priv->napi); |
| 1193 | netif_stop_queue(netdev); |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1194 | err_alloc: |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 1195 | ftgmac100_free_buffers(priv); |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1196 | free_irq(netdev->irq, netdev); |
| 1197 | err_irq: |
| 1198 | netif_napi_del(&priv->napi); |
| 1199 | err_hw: |
| 1200 | iowrite32(0, priv->base + FTGMAC100_OFFSET_IER); |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 1201 | ftgmac100_free_rings(priv); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1202 | return err; |
| 1203 | } |
| 1204 | |
| 1205 | static int ftgmac100_stop(struct net_device *netdev) |
| 1206 | { |
| 1207 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 1208 | |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1209 | /* Note about the reset task: We are called with the rtnl lock |
| 1210 | * held, so we are synchronized against the core of the reset |
| 1211 | * task. We must not try to synchronously cancel it otherwise |
| 1212 | * we can deadlock. But since it will test for netif_running() |
| 1213 | * which has already been cleared by the net core, we don't |
| 1214 | * anything special to do. |
| 1215 | */ |
| 1216 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1217 | /* disable all interrupts */ |
| 1218 | iowrite32(0, priv->base + FTGMAC100_OFFSET_IER); |
| 1219 | |
| 1220 | netif_stop_queue(netdev); |
| 1221 | napi_disable(&priv->napi); |
Benjamin Herrenschmidt | b8dbecf | 2017-04-05 12:28:47 +1000 | [diff] [blame] | 1222 | netif_napi_del(&priv->napi); |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1223 | if (netdev->phydev) |
| 1224 | phy_stop(netdev->phydev); |
Gavin Shan | 2c15f25 | 2016-10-04 11:25:54 +1100 | [diff] [blame] | 1225 | else if (priv->use_ncsi) |
| 1226 | ncsi_stop_dev(priv->ndev); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1227 | |
| 1228 | ftgmac100_stop_hw(priv); |
Benjamin Herrenschmidt | 60b28a1 | 2017-04-05 12:28:41 +1000 | [diff] [blame] | 1229 | free_irq(netdev->irq, netdev); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1230 | ftgmac100_free_buffers(priv); |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 1231 | ftgmac100_free_rings(priv); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1232 | |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1236 | /* optional */ |
| 1237 | static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) |
| 1238 | { |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1239 | if (!netdev->phydev) |
| 1240 | return -ENXIO; |
| 1241 | |
Philippe Reynes | b3c40ad | 2016-05-16 01:35:13 +0200 | [diff] [blame] | 1242 | return phy_mii_ioctl(netdev->phydev, ifr, cmd); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Benjamin Herrenschmidt | d3ca8fb | 2017-04-10 11:15:15 +1000 | [diff] [blame] | 1245 | static void ftgmac100_tx_timeout(struct net_device *netdev) |
| 1246 | { |
| 1247 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 1248 | |
| 1249 | /* Disable all interrupts */ |
| 1250 | iowrite32(0, priv->base + FTGMAC100_OFFSET_IER); |
| 1251 | |
| 1252 | /* Do the reset outside of interrupt context */ |
| 1253 | schedule_work(&priv->reset_task); |
| 1254 | } |
| 1255 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1256 | static const struct net_device_ops ftgmac100_netdev_ops = { |
| 1257 | .ndo_open = ftgmac100_open, |
| 1258 | .ndo_stop = ftgmac100_stop, |
| 1259 | .ndo_start_xmit = ftgmac100_hard_start_xmit, |
Gavin Shan | 113ce10 | 2016-07-19 11:54:22 +1000 | [diff] [blame] | 1260 | .ndo_set_mac_address = ftgmac100_set_mac_addr, |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1261 | .ndo_validate_addr = eth_validate_addr, |
| 1262 | .ndo_do_ioctl = ftgmac100_do_ioctl, |
Benjamin Herrenschmidt | d3ca8fb | 2017-04-10 11:15:15 +1000 | [diff] [blame] | 1263 | .ndo_tx_timeout = ftgmac100_tx_timeout, |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1264 | }; |
| 1265 | |
Gavin Shan | eb41818 | 2016-07-19 11:54:21 +1000 | [diff] [blame] | 1266 | static int ftgmac100_setup_mdio(struct net_device *netdev) |
| 1267 | { |
| 1268 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 1269 | struct platform_device *pdev = to_platform_device(priv->dev); |
| 1270 | int i, err = 0; |
Joel Stanley | e07dc63 | 2016-09-22 08:35:02 +0930 | [diff] [blame] | 1271 | u32 reg; |
Gavin Shan | eb41818 | 2016-07-19 11:54:21 +1000 | [diff] [blame] | 1272 | |
| 1273 | /* initialize mdio bus */ |
| 1274 | priv->mii_bus = mdiobus_alloc(); |
| 1275 | if (!priv->mii_bus) |
| 1276 | return -EIO; |
| 1277 | |
Joel Stanley | e07dc63 | 2016-09-22 08:35:02 +0930 | [diff] [blame] | 1278 | if (of_machine_is_compatible("aspeed,ast2400") || |
| 1279 | of_machine_is_compatible("aspeed,ast2500")) { |
| 1280 | /* This driver supports the old MDIO interface */ |
| 1281 | reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR); |
| 1282 | reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE; |
| 1283 | iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR); |
| 1284 | }; |
| 1285 | |
Gavin Shan | eb41818 | 2016-07-19 11:54:21 +1000 | [diff] [blame] | 1286 | priv->mii_bus->name = "ftgmac100_mdio"; |
| 1287 | snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d", |
| 1288 | pdev->name, pdev->id); |
| 1289 | priv->mii_bus->priv = priv->netdev; |
| 1290 | priv->mii_bus->read = ftgmac100_mdiobus_read; |
| 1291 | priv->mii_bus->write = ftgmac100_mdiobus_write; |
| 1292 | |
| 1293 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 1294 | priv->mii_bus->irq[i] = PHY_POLL; |
| 1295 | |
| 1296 | err = mdiobus_register(priv->mii_bus); |
| 1297 | if (err) { |
| 1298 | dev_err(priv->dev, "Cannot register MDIO bus!\n"); |
| 1299 | goto err_register_mdiobus; |
| 1300 | } |
| 1301 | |
| 1302 | err = ftgmac100_mii_probe(priv); |
| 1303 | if (err) { |
| 1304 | dev_err(priv->dev, "MII Probe failed!\n"); |
| 1305 | goto err_mii_probe; |
| 1306 | } |
| 1307 | |
| 1308 | return 0; |
| 1309 | |
| 1310 | err_mii_probe: |
| 1311 | mdiobus_unregister(priv->mii_bus); |
| 1312 | err_register_mdiobus: |
| 1313 | mdiobus_free(priv->mii_bus); |
| 1314 | return err; |
| 1315 | } |
| 1316 | |
| 1317 | static void ftgmac100_destroy_mdio(struct net_device *netdev) |
| 1318 | { |
| 1319 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 1320 | |
| 1321 | if (!netdev->phydev) |
| 1322 | return; |
| 1323 | |
| 1324 | phy_disconnect(netdev->phydev); |
| 1325 | mdiobus_unregister(priv->mii_bus); |
| 1326 | mdiobus_free(priv->mii_bus); |
| 1327 | } |
| 1328 | |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1329 | static void ftgmac100_ncsi_handler(struct ncsi_dev *nd) |
| 1330 | { |
| 1331 | if (unlikely(nd->state != ncsi_dev_state_functional)) |
| 1332 | return; |
| 1333 | |
| 1334 | netdev_info(nd->dev, "NCSI interface %s\n", |
| 1335 | nd->link_up ? "up" : "down"); |
| 1336 | } |
| 1337 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1338 | static int ftgmac100_probe(struct platform_device *pdev) |
| 1339 | { |
| 1340 | struct resource *res; |
| 1341 | int irq; |
| 1342 | struct net_device *netdev; |
| 1343 | struct ftgmac100 *priv; |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1344 | int err = 0; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1345 | |
| 1346 | if (!pdev) |
| 1347 | return -ENODEV; |
| 1348 | |
| 1349 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1350 | if (!res) |
| 1351 | return -ENXIO; |
| 1352 | |
| 1353 | irq = platform_get_irq(pdev, 0); |
| 1354 | if (irq < 0) |
| 1355 | return irq; |
| 1356 | |
| 1357 | /* setup net_device */ |
| 1358 | netdev = alloc_etherdev(sizeof(*priv)); |
| 1359 | if (!netdev) { |
| 1360 | err = -ENOMEM; |
| 1361 | goto err_alloc_etherdev; |
| 1362 | } |
| 1363 | |
| 1364 | SET_NETDEV_DEV(netdev, &pdev->dev); |
| 1365 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1366 | netdev->ethtool_ops = &ftgmac100_ethtool_ops; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1367 | netdev->netdev_ops = &ftgmac100_netdev_ops; |
Benjamin Herrenschmidt | d3ca8fb | 2017-04-10 11:15:15 +1000 | [diff] [blame] | 1368 | netdev->watchdog_timeo = 5 * HZ; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1369 | |
| 1370 | platform_set_drvdata(pdev, netdev); |
| 1371 | |
| 1372 | /* setup private data */ |
| 1373 | priv = netdev_priv(netdev); |
| 1374 | priv->netdev = netdev; |
| 1375 | priv->dev = &pdev->dev; |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1376 | INIT_WORK(&priv->reset_task, ftgmac100_reset_task); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1377 | |
| 1378 | spin_lock_init(&priv->tx_lock); |
| 1379 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1380 | /* map io memory */ |
| 1381 | priv->res = request_mem_region(res->start, resource_size(res), |
| 1382 | dev_name(&pdev->dev)); |
| 1383 | if (!priv->res) { |
| 1384 | dev_err(&pdev->dev, "Could not reserve memory region\n"); |
| 1385 | err = -ENOMEM; |
| 1386 | goto err_req_mem; |
| 1387 | } |
| 1388 | |
| 1389 | priv->base = ioremap(res->start, resource_size(res)); |
| 1390 | if (!priv->base) { |
| 1391 | dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n"); |
| 1392 | err = -EIO; |
| 1393 | goto err_ioremap; |
| 1394 | } |
| 1395 | |
Benjamin Herrenschmidt | 60b28a1 | 2017-04-05 12:28:41 +1000 | [diff] [blame] | 1396 | netdev->irq = irq; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1397 | |
Gavin Shan | 113ce10 | 2016-07-19 11:54:22 +1000 | [diff] [blame] | 1398 | /* MAC address from chip or random one */ |
| 1399 | ftgmac100_setup_mac(priv); |
| 1400 | |
Joel Stanley | 2a0ab8eb | 2016-09-22 08:35:00 +0930 | [diff] [blame] | 1401 | if (of_machine_is_compatible("aspeed,ast2400") || |
| 1402 | of_machine_is_compatible("aspeed,ast2500")) { |
| 1403 | priv->rxdes0_edorr_mask = BIT(30); |
| 1404 | priv->txdes0_edotr_mask = BIT(30); |
| 1405 | } else { |
| 1406 | priv->rxdes0_edorr_mask = BIT(15); |
| 1407 | priv->txdes0_edotr_mask = BIT(15); |
| 1408 | } |
| 1409 | |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1410 | if (pdev->dev.of_node && |
| 1411 | of_get_property(pdev->dev.of_node, "use-ncsi", NULL)) { |
| 1412 | if (!IS_ENABLED(CONFIG_NET_NCSI)) { |
| 1413 | dev_err(&pdev->dev, "NCSI stack not enabled\n"); |
| 1414 | goto err_ncsi_dev; |
| 1415 | } |
| 1416 | |
| 1417 | dev_info(&pdev->dev, "Using NCSI interface\n"); |
| 1418 | priv->use_ncsi = true; |
| 1419 | priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler); |
| 1420 | if (!priv->ndev) |
| 1421 | goto err_ncsi_dev; |
| 1422 | } else { |
| 1423 | priv->use_ncsi = false; |
| 1424 | err = ftgmac100_setup_mdio(netdev); |
| 1425 | if (err) |
| 1426 | goto err_setup_mdio; |
| 1427 | } |
| 1428 | |
| 1429 | /* We have to disable on-chip IP checksum functionality |
| 1430 | * when NCSI is enabled on the interface. It doesn't work |
| 1431 | * in that case. |
| 1432 | */ |
Benjamin Herrenschmidt | 6720219 | 2017-04-06 11:02:46 +1000 | [diff] [blame] | 1433 | netdev->features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_GRO; |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1434 | if (priv->use_ncsi && |
| 1435 | of_get_property(pdev->dev.of_node, "no-hw-checksum", NULL)) |
| 1436 | netdev->features &= ~NETIF_F_IP_CSUM; |
| 1437 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1438 | |
| 1439 | /* register network device */ |
| 1440 | err = register_netdev(netdev); |
| 1441 | if (err) { |
| 1442 | dev_err(&pdev->dev, "Failed to register netdev\n"); |
| 1443 | goto err_register_netdev; |
| 1444 | } |
| 1445 | |
Benjamin Herrenschmidt | 60b28a1 | 2017-04-05 12:28:41 +1000 | [diff] [blame] | 1446 | netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1447 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1448 | return 0; |
| 1449 | |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1450 | err_ncsi_dev: |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1451 | err_register_netdev: |
Gavin Shan | eb41818 | 2016-07-19 11:54:21 +1000 | [diff] [blame] | 1452 | ftgmac100_destroy_mdio(netdev); |
| 1453 | err_setup_mdio: |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1454 | iounmap(priv->base); |
| 1455 | err_ioremap: |
| 1456 | release_resource(priv->res); |
| 1457 | err_req_mem: |
| 1458 | netif_napi_del(&priv->napi); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1459 | free_netdev(netdev); |
| 1460 | err_alloc_etherdev: |
| 1461 | return err; |
| 1462 | } |
| 1463 | |
Dmitry Torokhov | be12502 | 2017-03-01 17:24:47 -0800 | [diff] [blame] | 1464 | static int ftgmac100_remove(struct platform_device *pdev) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1465 | { |
| 1466 | struct net_device *netdev; |
| 1467 | struct ftgmac100 *priv; |
| 1468 | |
| 1469 | netdev = platform_get_drvdata(pdev); |
| 1470 | priv = netdev_priv(netdev); |
| 1471 | |
| 1472 | unregister_netdev(netdev); |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1473 | |
| 1474 | /* There's a small chance the reset task will have been re-queued, |
| 1475 | * during stop, make sure it's gone before we free the structure. |
| 1476 | */ |
| 1477 | cancel_work_sync(&priv->reset_task); |
| 1478 | |
Gavin Shan | eb41818 | 2016-07-19 11:54:21 +1000 | [diff] [blame] | 1479 | ftgmac100_destroy_mdio(netdev); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1480 | |
| 1481 | iounmap(priv->base); |
| 1482 | release_resource(priv->res); |
| 1483 | |
| 1484 | netif_napi_del(&priv->napi); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1485 | free_netdev(netdev); |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
Gavin Shan | bb168e2 | 2016-07-19 11:54:24 +1000 | [diff] [blame] | 1489 | static const struct of_device_id ftgmac100_of_match[] = { |
| 1490 | { .compatible = "faraday,ftgmac100" }, |
| 1491 | { } |
| 1492 | }; |
| 1493 | MODULE_DEVICE_TABLE(of, ftgmac100_of_match); |
| 1494 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1495 | static struct platform_driver ftgmac100_driver = { |
Gavin Shan | bb168e2 | 2016-07-19 11:54:24 +1000 | [diff] [blame] | 1496 | .probe = ftgmac100_probe, |
Dmitry Torokhov | be12502 | 2017-03-01 17:24:47 -0800 | [diff] [blame] | 1497 | .remove = ftgmac100_remove, |
Gavin Shan | bb168e2 | 2016-07-19 11:54:24 +1000 | [diff] [blame] | 1498 | .driver = { |
| 1499 | .name = DRV_NAME, |
| 1500 | .of_match_table = ftgmac100_of_match, |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1501 | }, |
| 1502 | }; |
Sachin Kamat | 14f645d | 2013-03-18 01:50:48 +0000 | [diff] [blame] | 1503 | module_platform_driver(ftgmac100_driver); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1504 | |
| 1505 | MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>"); |
| 1506 | MODULE_DESCRIPTION("FTGMAC100 driver"); |
| 1507 | MODULE_LICENSE("GPL"); |