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