Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1 | /* Renesas Ethernet AVB device driver |
| 2 | * |
| 3 | * Copyright (C) 2014-2015 Renesas Electronics Corporation |
| 4 | * Copyright (C) 2015 Renesas Solutions Corp. |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 5 | * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com> |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 6 | * |
| 7 | * Based on the SuperH Ethernet driver |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms and conditions of the GNU General Public License version 2, |
| 11 | * as published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/cache.h> |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/etherdevice.h> |
| 20 | #include <linux/ethtool.h> |
| 21 | #include <linux/if_vlan.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/net_tstamp.h> |
| 26 | #include <linux/of.h> |
| 27 | #include <linux/of_device.h> |
| 28 | #include <linux/of_irq.h> |
| 29 | #include <linux/of_mdio.h> |
| 30 | #include <linux/of_net.h> |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 31 | #include <linux/pm_runtime.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/spinlock.h> |
| 34 | |
Simon Horman | b3d39a8 | 2015-11-20 11:29:39 -0800 | [diff] [blame] | 35 | #include <asm/div64.h> |
| 36 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 37 | #include "ravb.h" |
| 38 | |
| 39 | #define RAVB_DEF_MSG_ENABLE \ |
| 40 | (NETIF_MSG_LINK | \ |
| 41 | NETIF_MSG_TIMER | \ |
| 42 | NETIF_MSG_RX_ERR | \ |
| 43 | NETIF_MSG_TX_ERR) |
| 44 | |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 45 | static const char *ravb_rx_irqs[NUM_RX_QUEUE] = { |
| 46 | "ch0", /* RAVB_BE */ |
| 47 | "ch1", /* RAVB_NC */ |
| 48 | }; |
| 49 | |
| 50 | static const char *ravb_tx_irqs[NUM_TX_QUEUE] = { |
| 51 | "ch18", /* RAVB_BE */ |
| 52 | "ch19", /* RAVB_NC */ |
| 53 | }; |
| 54 | |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 55 | void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear, |
| 56 | u32 set) |
| 57 | { |
| 58 | ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg); |
| 59 | } |
| 60 | |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 61 | int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value) |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 62 | { |
| 63 | int i; |
| 64 | |
| 65 | for (i = 0; i < 10000; i++) { |
| 66 | if ((ravb_read(ndev, reg) & mask) == value) |
| 67 | return 0; |
| 68 | udelay(10); |
| 69 | } |
| 70 | return -ETIMEDOUT; |
| 71 | } |
| 72 | |
| 73 | static int ravb_config(struct net_device *ndev) |
| 74 | { |
| 75 | int error; |
| 76 | |
| 77 | /* Set config mode */ |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 78 | ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 79 | /* Check if the operating mode is changed to the config mode */ |
| 80 | error = ravb_wait(ndev, CSR, CSR_OPS, CSR_OPS_CONFIG); |
| 81 | if (error) |
| 82 | netdev_err(ndev, "failed to switch device to config mode\n"); |
| 83 | |
| 84 | return error; |
| 85 | } |
| 86 | |
| 87 | static void ravb_set_duplex(struct net_device *ndev) |
| 88 | { |
| 89 | struct ravb_private *priv = netdev_priv(ndev); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 90 | |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 91 | ravb_modify(ndev, ECMR, ECMR_DM, priv->duplex ? ECMR_DM : 0); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void ravb_set_rate(struct net_device *ndev) |
| 95 | { |
| 96 | struct ravb_private *priv = netdev_priv(ndev); |
| 97 | |
| 98 | switch (priv->speed) { |
| 99 | case 100: /* 100BASE */ |
| 100 | ravb_write(ndev, GECMR_SPEED_100, GECMR); |
| 101 | break; |
| 102 | case 1000: /* 1000BASE */ |
| 103 | ravb_write(ndev, GECMR_SPEED_1000, GECMR); |
| 104 | break; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | static void ravb_set_buffer_align(struct sk_buff *skb) |
| 109 | { |
| 110 | u32 reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1); |
| 111 | |
| 112 | if (reserve) |
| 113 | skb_reserve(skb, RAVB_ALIGN - reserve); |
| 114 | } |
| 115 | |
| 116 | /* Get MAC address from the MAC address registers |
| 117 | * |
| 118 | * Ethernet AVB device doesn't have ROM for MAC address. |
| 119 | * This function gets the MAC address that was used by a bootloader. |
| 120 | */ |
| 121 | static void ravb_read_mac_address(struct net_device *ndev, const u8 *mac) |
| 122 | { |
| 123 | if (mac) { |
| 124 | ether_addr_copy(ndev->dev_addr, mac); |
| 125 | } else { |
Sergei Shtylyov | d966063 | 2015-12-05 00:58:07 +0300 | [diff] [blame] | 126 | u32 mahr = ravb_read(ndev, MAHR); |
| 127 | u32 malr = ravb_read(ndev, MALR); |
| 128 | |
| 129 | ndev->dev_addr[0] = (mahr >> 24) & 0xFF; |
| 130 | ndev->dev_addr[1] = (mahr >> 16) & 0xFF; |
| 131 | ndev->dev_addr[2] = (mahr >> 8) & 0xFF; |
| 132 | ndev->dev_addr[3] = (mahr >> 0) & 0xFF; |
| 133 | ndev->dev_addr[4] = (malr >> 8) & 0xFF; |
| 134 | ndev->dev_addr[5] = (malr >> 0) & 0xFF; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set) |
| 139 | { |
| 140 | struct ravb_private *priv = container_of(ctrl, struct ravb_private, |
| 141 | mdiobb); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 142 | |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 143 | ravb_modify(priv->ndev, PIR, mask, set ? mask : 0); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* MDC pin control */ |
| 147 | static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level) |
| 148 | { |
| 149 | ravb_mdio_ctrl(ctrl, PIR_MDC, level); |
| 150 | } |
| 151 | |
| 152 | /* Data I/O pin control */ |
| 153 | static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output) |
| 154 | { |
| 155 | ravb_mdio_ctrl(ctrl, PIR_MMD, output); |
| 156 | } |
| 157 | |
| 158 | /* Set data bit */ |
| 159 | static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value) |
| 160 | { |
| 161 | ravb_mdio_ctrl(ctrl, PIR_MDO, value); |
| 162 | } |
| 163 | |
| 164 | /* Get data bit */ |
| 165 | static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl) |
| 166 | { |
| 167 | struct ravb_private *priv = container_of(ctrl, struct ravb_private, |
| 168 | mdiobb); |
| 169 | |
| 170 | return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0; |
| 171 | } |
| 172 | |
| 173 | /* MDIO bus control struct */ |
| 174 | static struct mdiobb_ops bb_ops = { |
| 175 | .owner = THIS_MODULE, |
| 176 | .set_mdc = ravb_set_mdc, |
| 177 | .set_mdio_dir = ravb_set_mdio_dir, |
| 178 | .set_mdio_data = ravb_set_mdio_data, |
| 179 | .get_mdio_data = ravb_get_mdio_data, |
| 180 | }; |
| 181 | |
| 182 | /* Free skb's and DMA buffers for Ethernet AVB */ |
| 183 | static void ravb_ring_free(struct net_device *ndev, int q) |
| 184 | { |
| 185 | struct ravb_private *priv = netdev_priv(ndev); |
| 186 | int ring_size; |
| 187 | int i; |
| 188 | |
| 189 | /* Free RX skb ringbuffer */ |
| 190 | if (priv->rx_skb[q]) { |
| 191 | for (i = 0; i < priv->num_rx_ring[q]; i++) |
| 192 | dev_kfree_skb(priv->rx_skb[q][i]); |
| 193 | } |
| 194 | kfree(priv->rx_skb[q]); |
| 195 | priv->rx_skb[q] = NULL; |
| 196 | |
| 197 | /* Free TX skb ringbuffer */ |
| 198 | if (priv->tx_skb[q]) { |
| 199 | for (i = 0; i < priv->num_tx_ring[q]; i++) |
| 200 | dev_kfree_skb(priv->tx_skb[q][i]); |
| 201 | } |
| 202 | kfree(priv->tx_skb[q]); |
| 203 | priv->tx_skb[q] = NULL; |
| 204 | |
| 205 | /* Free aligned TX buffers */ |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 206 | kfree(priv->tx_align[q]); |
| 207 | priv->tx_align[q] = NULL; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 208 | |
| 209 | if (priv->rx_ring[q]) { |
| 210 | ring_size = sizeof(struct ravb_ex_rx_desc) * |
| 211 | (priv->num_rx_ring[q] + 1); |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 212 | dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q], |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 213 | priv->rx_desc_dma[q]); |
| 214 | priv->rx_ring[q] = NULL; |
| 215 | } |
| 216 | |
| 217 | if (priv->tx_ring[q]) { |
| 218 | ring_size = sizeof(struct ravb_tx_desc) * |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 219 | (priv->num_tx_ring[q] * NUM_TX_DESC + 1); |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 220 | dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q], |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 221 | priv->tx_desc_dma[q]); |
| 222 | priv->tx_ring[q] = NULL; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* Format skb and descriptor buffer for Ethernet AVB */ |
| 227 | static void ravb_ring_format(struct net_device *ndev, int q) |
| 228 | { |
| 229 | struct ravb_private *priv = netdev_priv(ndev); |
Sergei Shtylyov | aad0d51 | 2015-07-10 21:10:10 +0300 | [diff] [blame] | 230 | struct ravb_ex_rx_desc *rx_desc; |
| 231 | struct ravb_tx_desc *tx_desc; |
| 232 | struct ravb_desc *desc; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 233 | int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q]; |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 234 | int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] * |
| 235 | NUM_TX_DESC; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 236 | dma_addr_t dma_addr; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 237 | int i; |
| 238 | |
| 239 | priv->cur_rx[q] = 0; |
| 240 | priv->cur_tx[q] = 0; |
| 241 | priv->dirty_rx[q] = 0; |
| 242 | priv->dirty_tx[q] = 0; |
| 243 | |
| 244 | memset(priv->rx_ring[q], 0, rx_ring_size); |
| 245 | /* Build RX ring buffer */ |
| 246 | for (i = 0; i < priv->num_rx_ring[q]; i++) { |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 247 | /* RX descriptor */ |
| 248 | rx_desc = &priv->rx_ring[q][i]; |
| 249 | /* The size of the buffer should be on 16-byte boundary. */ |
| 250 | rx_desc->ds_cc = cpu_to_le16(ALIGN(PKT_BUF_SZ, 16)); |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 251 | dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data, |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 252 | ALIGN(PKT_BUF_SZ, 16), |
| 253 | DMA_FROM_DEVICE); |
Sergei Shtylyov | d8b4891 | 2015-07-22 01:31:59 +0300 | [diff] [blame] | 254 | /* We just set the data size to 0 for a failed mapping which |
| 255 | * should prevent DMA from happening... |
| 256 | */ |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 257 | if (dma_mapping_error(ndev->dev.parent, dma_addr)) |
Sergei Shtylyov | d8b4891 | 2015-07-22 01:31:59 +0300 | [diff] [blame] | 258 | rx_desc->ds_cc = cpu_to_le16(0); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 259 | rx_desc->dptr = cpu_to_le32(dma_addr); |
| 260 | rx_desc->die_dt = DT_FEMPTY; |
| 261 | } |
| 262 | rx_desc = &priv->rx_ring[q][i]; |
| 263 | rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]); |
| 264 | rx_desc->die_dt = DT_LINKFIX; /* type */ |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 265 | |
| 266 | memset(priv->tx_ring[q], 0, tx_ring_size); |
| 267 | /* Build TX ring buffer */ |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 268 | for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q]; |
| 269 | i++, tx_desc++) { |
| 270 | tx_desc->die_dt = DT_EEMPTY; |
| 271 | tx_desc++; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 272 | tx_desc->die_dt = DT_EEMPTY; |
| 273 | } |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 274 | tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]); |
| 275 | tx_desc->die_dt = DT_LINKFIX; /* type */ |
| 276 | |
| 277 | /* RX descriptor base address for best effort */ |
| 278 | desc = &priv->desc_bat[RX_QUEUE_OFFSET + q]; |
| 279 | desc->die_dt = DT_LINKFIX; /* type */ |
| 280 | desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]); |
| 281 | |
| 282 | /* TX descriptor base address for best effort */ |
| 283 | desc = &priv->desc_bat[q]; |
| 284 | desc->die_dt = DT_LINKFIX; /* type */ |
| 285 | desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]); |
| 286 | } |
| 287 | |
| 288 | /* Init skb and descriptor buffer for Ethernet AVB */ |
| 289 | static int ravb_ring_init(struct net_device *ndev, int q) |
| 290 | { |
| 291 | struct ravb_private *priv = netdev_priv(ndev); |
Sergei Shtylyov | d8b4891 | 2015-07-22 01:31:59 +0300 | [diff] [blame] | 292 | struct sk_buff *skb; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 293 | int ring_size; |
Sergei Shtylyov | d8b4891 | 2015-07-22 01:31:59 +0300 | [diff] [blame] | 294 | int i; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 295 | |
| 296 | /* Allocate RX and TX skb rings */ |
| 297 | priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q], |
| 298 | sizeof(*priv->rx_skb[q]), GFP_KERNEL); |
| 299 | priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q], |
| 300 | sizeof(*priv->tx_skb[q]), GFP_KERNEL); |
| 301 | if (!priv->rx_skb[q] || !priv->tx_skb[q]) |
| 302 | goto error; |
| 303 | |
Sergei Shtylyov | d8b4891 | 2015-07-22 01:31:59 +0300 | [diff] [blame] | 304 | for (i = 0; i < priv->num_rx_ring[q]; i++) { |
| 305 | skb = netdev_alloc_skb(ndev, PKT_BUF_SZ + RAVB_ALIGN - 1); |
| 306 | if (!skb) |
| 307 | goto error; |
| 308 | ravb_set_buffer_align(skb); |
| 309 | priv->rx_skb[q][i] = skb; |
| 310 | } |
| 311 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 312 | /* Allocate rings for the aligned buffers */ |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 313 | priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] + |
| 314 | DPTR_ALIGN - 1, GFP_KERNEL); |
| 315 | if (!priv->tx_align[q]) |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 316 | goto error; |
| 317 | |
| 318 | /* Allocate all RX descriptors. */ |
| 319 | ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1); |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 320 | priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size, |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 321 | &priv->rx_desc_dma[q], |
| 322 | GFP_KERNEL); |
| 323 | if (!priv->rx_ring[q]) |
| 324 | goto error; |
| 325 | |
| 326 | priv->dirty_rx[q] = 0; |
| 327 | |
| 328 | /* Allocate all TX descriptors. */ |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 329 | ring_size = sizeof(struct ravb_tx_desc) * |
| 330 | (priv->num_tx_ring[q] * NUM_TX_DESC + 1); |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 331 | priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size, |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 332 | &priv->tx_desc_dma[q], |
| 333 | GFP_KERNEL); |
| 334 | if (!priv->tx_ring[q]) |
| 335 | goto error; |
| 336 | |
| 337 | return 0; |
| 338 | |
| 339 | error: |
| 340 | ravb_ring_free(ndev, q); |
| 341 | |
| 342 | return -ENOMEM; |
| 343 | } |
| 344 | |
| 345 | /* E-MAC init function */ |
| 346 | static void ravb_emac_init(struct net_device *ndev) |
| 347 | { |
| 348 | struct ravb_private *priv = netdev_priv(ndev); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 349 | |
| 350 | /* Receive frame limit set register */ |
| 351 | ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR); |
| 352 | |
| 353 | /* PAUSE prohibition */ |
Sergei Shtylyov | 1c1fa82 | 2016-01-11 00:27:38 +0300 | [diff] [blame] | 354 | ravb_write(ndev, ECMR_ZPF | (priv->duplex ? ECMR_DM : 0) | |
| 355 | ECMR_TE | ECMR_RE, ECMR); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 356 | |
| 357 | ravb_set_rate(ndev); |
| 358 | |
| 359 | /* Set MAC address */ |
| 360 | ravb_write(ndev, |
| 361 | (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) | |
| 362 | (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR); |
| 363 | ravb_write(ndev, |
| 364 | (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR); |
| 365 | |
| 366 | ravb_write(ndev, 1, MPR); |
| 367 | |
| 368 | /* E-MAC status register clear */ |
| 369 | ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR); |
| 370 | |
| 371 | /* E-MAC interrupt enable register */ |
| 372 | ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR); |
| 373 | } |
| 374 | |
| 375 | /* Device init function for Ethernet AVB */ |
| 376 | static int ravb_dmac_init(struct net_device *ndev) |
| 377 | { |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 378 | struct ravb_private *priv = netdev_priv(ndev); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 379 | int error; |
| 380 | |
| 381 | /* Set CONFIG mode */ |
| 382 | error = ravb_config(ndev); |
| 383 | if (error) |
| 384 | return error; |
| 385 | |
| 386 | error = ravb_ring_init(ndev, RAVB_BE); |
| 387 | if (error) |
| 388 | return error; |
| 389 | error = ravb_ring_init(ndev, RAVB_NC); |
| 390 | if (error) { |
| 391 | ravb_ring_free(ndev, RAVB_BE); |
| 392 | return error; |
| 393 | } |
| 394 | |
| 395 | /* Descriptor format */ |
| 396 | ravb_ring_format(ndev, RAVB_BE); |
| 397 | ravb_ring_format(ndev, RAVB_NC); |
| 398 | |
| 399 | #if defined(__LITTLE_ENDIAN) |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 400 | ravb_modify(ndev, CCC, CCC_BOC, 0); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 401 | #else |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 402 | ravb_modify(ndev, CCC, CCC_BOC, CCC_BOC); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 403 | #endif |
| 404 | |
| 405 | /* Set AVB RX */ |
| 406 | ravb_write(ndev, RCR_EFFS | RCR_ENCF | RCR_ETS0 | 0x18000000, RCR); |
| 407 | |
| 408 | /* Set FIFO size */ |
| 409 | ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00222200, TGC); |
| 410 | |
| 411 | /* Timestamp enable */ |
| 412 | ravb_write(ndev, TCCR_TFEN, TCCR); |
| 413 | |
Kazuya Mizuguchi | 6474de5 | 2015-12-15 01:24:58 +0900 | [diff] [blame] | 414 | /* Interrupt init: */ |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 415 | if (priv->chip_id == RCAR_GEN3) { |
| 416 | /* Clear DIL.DPLx */ |
| 417 | ravb_write(ndev, 0, DIL); |
| 418 | /* Set queue specific interrupt */ |
| 419 | ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE); |
| 420 | } |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 421 | /* Frame receive */ |
| 422 | ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0); |
Kazuya Mizuguchi | 6474de5 | 2015-12-15 01:24:58 +0900 | [diff] [blame] | 423 | /* Disable FIFO full warning */ |
| 424 | ravb_write(ndev, 0, RIC1); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 425 | /* Receive FIFO full error, descriptor empty */ |
| 426 | ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2); |
| 427 | /* Frame transmitted, timestamp FIFO updated */ |
| 428 | ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC); |
| 429 | |
| 430 | /* Setting the control will start the AVB-DMAC process. */ |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 431 | ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* Free TX skb function for AVB-IP */ |
| 437 | static int ravb_tx_free(struct net_device *ndev, int q) |
| 438 | { |
| 439 | struct ravb_private *priv = netdev_priv(ndev); |
| 440 | struct net_device_stats *stats = &priv->stats[q]; |
| 441 | struct ravb_tx_desc *desc; |
| 442 | int free_num = 0; |
Sergei Shtylyov | aad0d51 | 2015-07-10 21:10:10 +0300 | [diff] [blame] | 443 | int entry; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 444 | u32 size; |
| 445 | |
| 446 | for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) { |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 447 | entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] * |
| 448 | NUM_TX_DESC); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 449 | desc = &priv->tx_ring[q][entry]; |
| 450 | if (desc->die_dt != DT_FEMPTY) |
| 451 | break; |
| 452 | /* Descriptor type must be checked before all other reads */ |
| 453 | dma_rmb(); |
| 454 | size = le16_to_cpu(desc->ds_tagl) & TX_DS; |
| 455 | /* Free the original skb. */ |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 456 | if (priv->tx_skb[q][entry / NUM_TX_DESC]) { |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 457 | dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr), |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 458 | size, DMA_TO_DEVICE); |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 459 | /* Last packet descriptor? */ |
| 460 | if (entry % NUM_TX_DESC == NUM_TX_DESC - 1) { |
| 461 | entry /= NUM_TX_DESC; |
| 462 | dev_kfree_skb_any(priv->tx_skb[q][entry]); |
| 463 | priv->tx_skb[q][entry] = NULL; |
| 464 | stats->tx_packets++; |
| 465 | } |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 466 | free_num++; |
| 467 | } |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 468 | stats->tx_bytes += size; |
| 469 | desc->die_dt = DT_EEMPTY; |
| 470 | } |
| 471 | return free_num; |
| 472 | } |
| 473 | |
| 474 | static void ravb_get_tx_tstamp(struct net_device *ndev) |
| 475 | { |
| 476 | struct ravb_private *priv = netdev_priv(ndev); |
| 477 | struct ravb_tstamp_skb *ts_skb, *ts_skb2; |
| 478 | struct skb_shared_hwtstamps shhwtstamps; |
| 479 | struct sk_buff *skb; |
| 480 | struct timespec64 ts; |
| 481 | u16 tag, tfa_tag; |
| 482 | int count; |
| 483 | u32 tfa2; |
| 484 | |
| 485 | count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8; |
| 486 | while (count--) { |
| 487 | tfa2 = ravb_read(ndev, TFA2); |
| 488 | tfa_tag = (tfa2 & TFA2_TST) >> 16; |
| 489 | ts.tv_nsec = (u64)ravb_read(ndev, TFA0); |
| 490 | ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) | |
| 491 | ravb_read(ndev, TFA1); |
| 492 | memset(&shhwtstamps, 0, sizeof(shhwtstamps)); |
| 493 | shhwtstamps.hwtstamp = timespec64_to_ktime(ts); |
| 494 | list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, |
| 495 | list) { |
| 496 | skb = ts_skb->skb; |
| 497 | tag = ts_skb->tag; |
| 498 | list_del(&ts_skb->list); |
| 499 | kfree(ts_skb); |
| 500 | if (tag == tfa_tag) { |
| 501 | skb_tstamp_tx(skb, &shhwtstamps); |
| 502 | break; |
| 503 | } |
| 504 | } |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 505 | ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | /* Packet receive function for Ethernet AVB */ |
| 510 | static bool ravb_rx(struct net_device *ndev, int *quota, int q) |
| 511 | { |
| 512 | struct ravb_private *priv = netdev_priv(ndev); |
| 513 | int entry = priv->cur_rx[q] % priv->num_rx_ring[q]; |
| 514 | int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) - |
| 515 | priv->cur_rx[q]; |
| 516 | struct net_device_stats *stats = &priv->stats[q]; |
| 517 | struct ravb_ex_rx_desc *desc; |
| 518 | struct sk_buff *skb; |
| 519 | dma_addr_t dma_addr; |
| 520 | struct timespec64 ts; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 521 | u8 desc_status; |
Sergei Shtylyov | aad0d51 | 2015-07-10 21:10:10 +0300 | [diff] [blame] | 522 | u16 pkt_len; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 523 | int limit; |
| 524 | |
| 525 | boguscnt = min(boguscnt, *quota); |
| 526 | limit = boguscnt; |
| 527 | desc = &priv->rx_ring[q][entry]; |
| 528 | while (desc->die_dt != DT_FEMPTY) { |
| 529 | /* Descriptor type must be checked before all other reads */ |
| 530 | dma_rmb(); |
| 531 | desc_status = desc->msc; |
| 532 | pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS; |
| 533 | |
| 534 | if (--boguscnt < 0) |
| 535 | break; |
| 536 | |
Sergei Shtylyov | d8b4891 | 2015-07-22 01:31:59 +0300 | [diff] [blame] | 537 | /* We use 0-byte descriptors to mark the DMA mapping errors */ |
| 538 | if (!pkt_len) |
| 539 | continue; |
| 540 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 541 | if (desc_status & MSC_MC) |
| 542 | stats->multicast++; |
| 543 | |
| 544 | if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF | |
| 545 | MSC_CEEF)) { |
| 546 | stats->rx_errors++; |
| 547 | if (desc_status & MSC_CRC) |
| 548 | stats->rx_crc_errors++; |
| 549 | if (desc_status & MSC_RFE) |
| 550 | stats->rx_frame_errors++; |
| 551 | if (desc_status & (MSC_RTLF | MSC_RTSF)) |
| 552 | stats->rx_length_errors++; |
| 553 | if (desc_status & MSC_CEEF) |
| 554 | stats->rx_missed_errors++; |
| 555 | } else { |
| 556 | u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE; |
| 557 | |
| 558 | skb = priv->rx_skb[q][entry]; |
| 559 | priv->rx_skb[q][entry] = NULL; |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 560 | dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr), |
Sergei Shtylyov | e2370f0 | 2015-07-15 00:56:52 +0300 | [diff] [blame] | 561 | ALIGN(PKT_BUF_SZ, 16), |
| 562 | DMA_FROM_DEVICE); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 563 | get_ts &= (q == RAVB_NC) ? |
| 564 | RAVB_RXTSTAMP_TYPE_V2_L2_EVENT : |
| 565 | ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT; |
| 566 | if (get_ts) { |
| 567 | struct skb_shared_hwtstamps *shhwtstamps; |
| 568 | |
| 569 | shhwtstamps = skb_hwtstamps(skb); |
| 570 | memset(shhwtstamps, 0, sizeof(*shhwtstamps)); |
| 571 | ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) << |
| 572 | 32) | le32_to_cpu(desc->ts_sl); |
| 573 | ts.tv_nsec = le32_to_cpu(desc->ts_n); |
| 574 | shhwtstamps->hwtstamp = timespec64_to_ktime(ts); |
| 575 | } |
| 576 | skb_put(skb, pkt_len); |
| 577 | skb->protocol = eth_type_trans(skb, ndev); |
| 578 | napi_gro_receive(&priv->napi[q], skb); |
| 579 | stats->rx_packets++; |
| 580 | stats->rx_bytes += pkt_len; |
| 581 | } |
| 582 | |
| 583 | entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q]; |
| 584 | desc = &priv->rx_ring[q][entry]; |
| 585 | } |
| 586 | |
| 587 | /* Refill the RX ring buffers. */ |
| 588 | for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) { |
| 589 | entry = priv->dirty_rx[q] % priv->num_rx_ring[q]; |
| 590 | desc = &priv->rx_ring[q][entry]; |
| 591 | /* The size of the buffer should be on 16-byte boundary. */ |
| 592 | desc->ds_cc = cpu_to_le16(ALIGN(PKT_BUF_SZ, 16)); |
| 593 | |
| 594 | if (!priv->rx_skb[q][entry]) { |
| 595 | skb = netdev_alloc_skb(ndev, |
| 596 | PKT_BUF_SZ + RAVB_ALIGN - 1); |
| 597 | if (!skb) |
| 598 | break; /* Better luck next round. */ |
| 599 | ravb_set_buffer_align(skb); |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 600 | dma_addr = dma_map_single(ndev->dev.parent, skb->data, |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 601 | le16_to_cpu(desc->ds_cc), |
| 602 | DMA_FROM_DEVICE); |
| 603 | skb_checksum_none_assert(skb); |
Sergei Shtylyov | d8b4891 | 2015-07-22 01:31:59 +0300 | [diff] [blame] | 604 | /* We just set the data size to 0 for a failed mapping |
| 605 | * which should prevent DMA from happening... |
| 606 | */ |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 607 | if (dma_mapping_error(ndev->dev.parent, dma_addr)) |
Sergei Shtylyov | d8b4891 | 2015-07-22 01:31:59 +0300 | [diff] [blame] | 608 | desc->ds_cc = cpu_to_le16(0); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 609 | desc->dptr = cpu_to_le32(dma_addr); |
| 610 | priv->rx_skb[q][entry] = skb; |
| 611 | } |
| 612 | /* Descriptor type must be set after all the above writes */ |
| 613 | dma_wmb(); |
| 614 | desc->die_dt = DT_FEMPTY; |
| 615 | } |
| 616 | |
| 617 | *quota -= limit - (++boguscnt); |
| 618 | |
| 619 | return boguscnt <= 0; |
| 620 | } |
| 621 | |
| 622 | static void ravb_rcv_snd_disable(struct net_device *ndev) |
| 623 | { |
| 624 | /* Disable TX and RX */ |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 625 | ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | static void ravb_rcv_snd_enable(struct net_device *ndev) |
| 629 | { |
| 630 | /* Enable TX and RX */ |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 631 | ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /* function for waiting dma process finished */ |
| 635 | static int ravb_stop_dma(struct net_device *ndev) |
| 636 | { |
| 637 | int error; |
| 638 | |
| 639 | /* Wait for stopping the hardware TX process */ |
| 640 | error = ravb_wait(ndev, TCCR, |
| 641 | TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0); |
| 642 | if (error) |
| 643 | return error; |
| 644 | |
| 645 | error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3, |
| 646 | 0); |
| 647 | if (error) |
| 648 | return error; |
| 649 | |
| 650 | /* Stop the E-MAC's RX/TX processes. */ |
| 651 | ravb_rcv_snd_disable(ndev); |
| 652 | |
| 653 | /* Wait for stopping the RX DMA process */ |
| 654 | error = ravb_wait(ndev, CSR, CSR_RPO, 0); |
| 655 | if (error) |
| 656 | return error; |
| 657 | |
| 658 | /* Stop AVB-DMAC process */ |
| 659 | return ravb_config(ndev); |
| 660 | } |
| 661 | |
| 662 | /* E-MAC interrupt handler */ |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 663 | static void ravb_emac_interrupt_unlocked(struct net_device *ndev) |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 664 | { |
| 665 | struct ravb_private *priv = netdev_priv(ndev); |
| 666 | u32 ecsr, psr; |
| 667 | |
| 668 | ecsr = ravb_read(ndev, ECSR); |
| 669 | ravb_write(ndev, ecsr, ECSR); /* clear interrupt */ |
| 670 | if (ecsr & ECSR_ICD) |
| 671 | ndev->stats.tx_carrier_errors++; |
| 672 | if (ecsr & ECSR_LCHNG) { |
| 673 | /* Link changed */ |
| 674 | if (priv->no_avb_link) |
| 675 | return; |
| 676 | psr = ravb_read(ndev, PSR); |
| 677 | if (priv->avb_link_active_low) |
| 678 | psr ^= PSR_LMON; |
| 679 | if (!(psr & PSR_LMON)) { |
| 680 | /* DIsable RX and TX */ |
| 681 | ravb_rcv_snd_disable(ndev); |
| 682 | } else { |
| 683 | /* Enable RX and TX */ |
| 684 | ravb_rcv_snd_enable(ndev); |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 689 | static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id) |
| 690 | { |
| 691 | struct net_device *ndev = dev_id; |
| 692 | struct ravb_private *priv = netdev_priv(ndev); |
| 693 | |
| 694 | spin_lock(&priv->lock); |
| 695 | ravb_emac_interrupt_unlocked(ndev); |
| 696 | mmiowb(); |
| 697 | spin_unlock(&priv->lock); |
| 698 | return IRQ_HANDLED; |
| 699 | } |
| 700 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 701 | /* Error interrupt handler */ |
| 702 | static void ravb_error_interrupt(struct net_device *ndev) |
| 703 | { |
| 704 | struct ravb_private *priv = netdev_priv(ndev); |
| 705 | u32 eis, ris2; |
| 706 | |
| 707 | eis = ravb_read(ndev, EIS); |
| 708 | ravb_write(ndev, ~EIS_QFS, EIS); |
| 709 | if (eis & EIS_QFS) { |
| 710 | ris2 = ravb_read(ndev, RIS2); |
| 711 | ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF), RIS2); |
| 712 | |
| 713 | /* Receive Descriptor Empty int */ |
| 714 | if (ris2 & RIS2_QFF0) |
| 715 | priv->stats[RAVB_BE].rx_over_errors++; |
| 716 | |
| 717 | /* Receive Descriptor Empty int */ |
| 718 | if (ris2 & RIS2_QFF1) |
| 719 | priv->stats[RAVB_NC].rx_over_errors++; |
| 720 | |
| 721 | /* Receive FIFO Overflow int */ |
| 722 | if (ris2 & RIS2_RFFF) |
| 723 | priv->rx_fifo_errors++; |
| 724 | } |
| 725 | } |
| 726 | |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 727 | static bool ravb_queue_interrupt(struct net_device *ndev, int q) |
| 728 | { |
| 729 | struct ravb_private *priv = netdev_priv(ndev); |
| 730 | u32 ris0 = ravb_read(ndev, RIS0); |
| 731 | u32 ric0 = ravb_read(ndev, RIC0); |
| 732 | u32 tis = ravb_read(ndev, TIS); |
| 733 | u32 tic = ravb_read(ndev, TIC); |
| 734 | |
| 735 | if (((ris0 & ric0) & BIT(q)) || ((tis & tic) & BIT(q))) { |
| 736 | if (napi_schedule_prep(&priv->napi[q])) { |
| 737 | /* Mask RX and TX interrupts */ |
| 738 | if (priv->chip_id == RCAR_GEN2) { |
| 739 | ravb_write(ndev, ric0 & ~BIT(q), RIC0); |
| 740 | ravb_write(ndev, tic & ~BIT(q), TIC); |
| 741 | } else { |
| 742 | ravb_write(ndev, BIT(q), RID0); |
| 743 | ravb_write(ndev, BIT(q), TID); |
| 744 | } |
| 745 | __napi_schedule(&priv->napi[q]); |
| 746 | } else { |
| 747 | netdev_warn(ndev, |
| 748 | "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n", |
| 749 | ris0, ric0); |
| 750 | netdev_warn(ndev, |
| 751 | " tx status 0x%08x, tx mask 0x%08x.\n", |
| 752 | tis, tic); |
| 753 | } |
| 754 | return true; |
| 755 | } |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | static bool ravb_timestamp_interrupt(struct net_device *ndev) |
| 760 | { |
| 761 | u32 tis = ravb_read(ndev, TIS); |
| 762 | |
| 763 | if (tis & TIS_TFUF) { |
| 764 | ravb_write(ndev, ~TIS_TFUF, TIS); |
| 765 | ravb_get_tx_tstamp(ndev); |
| 766 | return true; |
| 767 | } |
| 768 | return false; |
| 769 | } |
| 770 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 771 | static irqreturn_t ravb_interrupt(int irq, void *dev_id) |
| 772 | { |
| 773 | struct net_device *ndev = dev_id; |
| 774 | struct ravb_private *priv = netdev_priv(ndev); |
| 775 | irqreturn_t result = IRQ_NONE; |
| 776 | u32 iss; |
| 777 | |
| 778 | spin_lock(&priv->lock); |
| 779 | /* Get interrupt status */ |
| 780 | iss = ravb_read(ndev, ISS); |
| 781 | |
| 782 | /* Received and transmitted interrupts */ |
| 783 | if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) { |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 784 | int q; |
| 785 | |
| 786 | /* Timestamp updated */ |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 787 | if (ravb_timestamp_interrupt(ndev)) |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 788 | result = IRQ_HANDLED; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 789 | |
| 790 | /* Network control and best effort queue RX/TX */ |
| 791 | for (q = RAVB_NC; q >= RAVB_BE; q--) { |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 792 | if (ravb_queue_interrupt(ndev, q)) |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 793 | result = IRQ_HANDLED; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | |
| 797 | /* E-MAC status summary */ |
| 798 | if (iss & ISS_MS) { |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 799 | ravb_emac_interrupt_unlocked(ndev); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 800 | result = IRQ_HANDLED; |
| 801 | } |
| 802 | |
| 803 | /* Error status summary */ |
| 804 | if (iss & ISS_ES) { |
| 805 | ravb_error_interrupt(ndev); |
| 806 | result = IRQ_HANDLED; |
| 807 | } |
| 808 | |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 809 | /* gPTP interrupt status summary */ |
Yoshihiro Kaneko | 38c848c | 2016-03-16 00:52:16 +0900 | [diff] [blame] | 810 | if ((iss & ISS_CGIS) && ravb_ptp_interrupt(ndev) == IRQ_HANDLED) |
| 811 | result = IRQ_HANDLED; |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 812 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 813 | mmiowb(); |
| 814 | spin_unlock(&priv->lock); |
| 815 | return result; |
| 816 | } |
| 817 | |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 818 | /* Timestamp/Error/gPTP interrupt handler */ |
| 819 | static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id) |
| 820 | { |
| 821 | struct net_device *ndev = dev_id; |
| 822 | struct ravb_private *priv = netdev_priv(ndev); |
| 823 | irqreturn_t result = IRQ_NONE; |
| 824 | u32 iss; |
| 825 | |
| 826 | spin_lock(&priv->lock); |
| 827 | /* Get interrupt status */ |
| 828 | iss = ravb_read(ndev, ISS); |
| 829 | |
| 830 | /* Timestamp updated */ |
| 831 | if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev)) |
| 832 | result = IRQ_HANDLED; |
| 833 | |
| 834 | /* Error status summary */ |
| 835 | if (iss & ISS_ES) { |
| 836 | ravb_error_interrupt(ndev); |
| 837 | result = IRQ_HANDLED; |
| 838 | } |
| 839 | |
| 840 | /* gPTP interrupt status summary */ |
| 841 | if ((iss & ISS_CGIS) && ravb_ptp_interrupt(ndev) == IRQ_HANDLED) |
| 842 | result = IRQ_HANDLED; |
| 843 | |
| 844 | mmiowb(); |
| 845 | spin_unlock(&priv->lock); |
| 846 | return result; |
| 847 | } |
| 848 | |
| 849 | static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q) |
| 850 | { |
| 851 | struct net_device *ndev = dev_id; |
| 852 | struct ravb_private *priv = netdev_priv(ndev); |
| 853 | irqreturn_t result = IRQ_NONE; |
| 854 | |
| 855 | spin_lock(&priv->lock); |
| 856 | |
| 857 | /* Network control/Best effort queue RX/TX */ |
| 858 | if (ravb_queue_interrupt(ndev, q)) |
| 859 | result = IRQ_HANDLED; |
| 860 | |
| 861 | mmiowb(); |
| 862 | spin_unlock(&priv->lock); |
| 863 | return result; |
| 864 | } |
| 865 | |
| 866 | static irqreturn_t ravb_be_interrupt(int irq, void *dev_id) |
| 867 | { |
| 868 | return ravb_dma_interrupt(irq, dev_id, RAVB_BE); |
| 869 | } |
| 870 | |
| 871 | static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id) |
| 872 | { |
| 873 | return ravb_dma_interrupt(irq, dev_id, RAVB_NC); |
| 874 | } |
| 875 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 876 | static int ravb_poll(struct napi_struct *napi, int budget) |
| 877 | { |
| 878 | struct net_device *ndev = napi->dev; |
| 879 | struct ravb_private *priv = netdev_priv(ndev); |
| 880 | unsigned long flags; |
| 881 | int q = napi - priv->napi; |
| 882 | int mask = BIT(q); |
| 883 | int quota = budget; |
| 884 | u32 ris0, tis; |
| 885 | |
| 886 | for (;;) { |
| 887 | tis = ravb_read(ndev, TIS); |
| 888 | ris0 = ravb_read(ndev, RIS0); |
| 889 | if (!((ris0 & mask) || (tis & mask))) |
| 890 | break; |
| 891 | |
| 892 | /* Processing RX Descriptor Ring */ |
| 893 | if (ris0 & mask) { |
| 894 | /* Clear RX interrupt */ |
| 895 | ravb_write(ndev, ~mask, RIS0); |
| 896 | if (ravb_rx(ndev, "a, q)) |
| 897 | goto out; |
| 898 | } |
| 899 | /* Processing TX Descriptor Ring */ |
| 900 | if (tis & mask) { |
| 901 | spin_lock_irqsave(&priv->lock, flags); |
| 902 | /* Clear TX interrupt */ |
| 903 | ravb_write(ndev, ~mask, TIS); |
| 904 | ravb_tx_free(ndev, q); |
| 905 | netif_wake_subqueue(ndev, q); |
| 906 | mmiowb(); |
| 907 | spin_unlock_irqrestore(&priv->lock, flags); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | napi_complete(napi); |
| 912 | |
| 913 | /* Re-enable RX/TX interrupts */ |
| 914 | spin_lock_irqsave(&priv->lock, flags); |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 915 | if (priv->chip_id == RCAR_GEN2) { |
| 916 | ravb_modify(ndev, RIC0, mask, mask); |
| 917 | ravb_modify(ndev, TIC, mask, mask); |
| 918 | } else { |
| 919 | ravb_write(ndev, mask, RIE0); |
| 920 | ravb_write(ndev, mask, TIE); |
| 921 | } |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 922 | mmiowb(); |
| 923 | spin_unlock_irqrestore(&priv->lock, flags); |
| 924 | |
| 925 | /* Receive error message handling */ |
| 926 | priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors; |
| 927 | priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors; |
| 928 | if (priv->rx_over_errors != ndev->stats.rx_over_errors) { |
| 929 | ndev->stats.rx_over_errors = priv->rx_over_errors; |
| 930 | netif_err(priv, rx_err, ndev, "Receive Descriptor Empty\n"); |
| 931 | } |
| 932 | if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors) { |
| 933 | ndev->stats.rx_fifo_errors = priv->rx_fifo_errors; |
| 934 | netif_err(priv, rx_err, ndev, "Receive FIFO Overflow\n"); |
| 935 | } |
| 936 | out: |
| 937 | return budget - quota; |
| 938 | } |
| 939 | |
| 940 | /* PHY state control function */ |
| 941 | static void ravb_adjust_link(struct net_device *ndev) |
| 942 | { |
| 943 | struct ravb_private *priv = netdev_priv(ndev); |
| 944 | struct phy_device *phydev = priv->phydev; |
| 945 | bool new_state = false; |
| 946 | |
| 947 | if (phydev->link) { |
| 948 | if (phydev->duplex != priv->duplex) { |
| 949 | new_state = true; |
| 950 | priv->duplex = phydev->duplex; |
| 951 | ravb_set_duplex(ndev); |
| 952 | } |
| 953 | |
| 954 | if (phydev->speed != priv->speed) { |
| 955 | new_state = true; |
| 956 | priv->speed = phydev->speed; |
| 957 | ravb_set_rate(ndev); |
| 958 | } |
| 959 | if (!priv->link) { |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 960 | ravb_modify(ndev, ECMR, ECMR_TXF, 0); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 961 | new_state = true; |
| 962 | priv->link = phydev->link; |
| 963 | if (priv->no_avb_link) |
| 964 | ravb_rcv_snd_enable(ndev); |
| 965 | } |
| 966 | } else if (priv->link) { |
| 967 | new_state = true; |
| 968 | priv->link = 0; |
| 969 | priv->speed = 0; |
| 970 | priv->duplex = -1; |
| 971 | if (priv->no_avb_link) |
| 972 | ravb_rcv_snd_disable(ndev); |
| 973 | } |
| 974 | |
| 975 | if (new_state && netif_msg_link(priv)) |
| 976 | phy_print_status(phydev); |
| 977 | } |
| 978 | |
| 979 | /* PHY init function */ |
| 980 | static int ravb_phy_init(struct net_device *ndev) |
| 981 | { |
| 982 | struct device_node *np = ndev->dev.parent->of_node; |
| 983 | struct ravb_private *priv = netdev_priv(ndev); |
| 984 | struct phy_device *phydev; |
| 985 | struct device_node *pn; |
Kazuya Mizuguchi | b4bc88a | 2015-12-15 19:44:13 +0900 | [diff] [blame] | 986 | int err; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 987 | |
| 988 | priv->link = 0; |
| 989 | priv->speed = 0; |
| 990 | priv->duplex = -1; |
| 991 | |
| 992 | /* Try connecting to PHY */ |
| 993 | pn = of_parse_phandle(np, "phy-handle", 0); |
Kazuya Mizuguchi | b4bc88a | 2015-12-15 19:44:13 +0900 | [diff] [blame] | 994 | if (!pn) { |
| 995 | /* In the case of a fixed PHY, the DT node associated |
| 996 | * to the PHY is the Ethernet MAC DT node. |
| 997 | */ |
| 998 | if (of_phy_is_fixed_link(np)) { |
| 999 | err = of_phy_register_fixed_link(np); |
| 1000 | if (err) |
| 1001 | return err; |
| 1002 | } |
| 1003 | pn = of_node_get(np); |
| 1004 | } |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1005 | phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0, |
| 1006 | priv->phy_interface); |
| 1007 | if (!phydev) { |
| 1008 | netdev_err(ndev, "failed to connect PHY\n"); |
| 1009 | return -ENOENT; |
| 1010 | } |
| 1011 | |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1012 | /* This driver only support 10/100Mbit speeds on Gen3 |
| 1013 | * at this time. |
| 1014 | */ |
| 1015 | if (priv->chip_id == RCAR_GEN3) { |
| 1016 | int err; |
| 1017 | |
| 1018 | err = phy_set_max_speed(phydev, SPEED_100); |
| 1019 | if (err) { |
| 1020 | netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n"); |
| 1021 | phy_disconnect(phydev); |
| 1022 | return err; |
| 1023 | } |
| 1024 | |
| 1025 | netdev_info(ndev, "limited PHY to 100Mbit/s\n"); |
| 1026 | } |
| 1027 | |
Kazuya Mizuguchi | 5449996 | 2015-12-14 00:15:58 +0900 | [diff] [blame] | 1028 | /* 10BASE is not supported */ |
| 1029 | phydev->supported &= ~PHY_10BT_FEATURES; |
| 1030 | |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1031 | phy_attached_info(phydev); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1032 | |
| 1033 | priv->phydev = phydev; |
| 1034 | |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | /* PHY control start function */ |
| 1039 | static int ravb_phy_start(struct net_device *ndev) |
| 1040 | { |
| 1041 | struct ravb_private *priv = netdev_priv(ndev); |
| 1042 | int error; |
| 1043 | |
| 1044 | error = ravb_phy_init(ndev); |
| 1045 | if (error) |
| 1046 | return error; |
| 1047 | |
| 1048 | phy_start(priv->phydev); |
| 1049 | |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | static int ravb_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd) |
| 1054 | { |
| 1055 | struct ravb_private *priv = netdev_priv(ndev); |
| 1056 | int error = -ENODEV; |
| 1057 | unsigned long flags; |
| 1058 | |
| 1059 | if (priv->phydev) { |
| 1060 | spin_lock_irqsave(&priv->lock, flags); |
| 1061 | error = phy_ethtool_gset(priv->phydev, ecmd); |
| 1062 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1063 | } |
| 1064 | |
| 1065 | return error; |
| 1066 | } |
| 1067 | |
| 1068 | static int ravb_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd) |
| 1069 | { |
| 1070 | struct ravb_private *priv = netdev_priv(ndev); |
| 1071 | unsigned long flags; |
| 1072 | int error; |
| 1073 | |
| 1074 | if (!priv->phydev) |
| 1075 | return -ENODEV; |
| 1076 | |
| 1077 | spin_lock_irqsave(&priv->lock, flags); |
| 1078 | |
| 1079 | /* Disable TX and RX */ |
| 1080 | ravb_rcv_snd_disable(ndev); |
| 1081 | |
| 1082 | error = phy_ethtool_sset(priv->phydev, ecmd); |
| 1083 | if (error) |
| 1084 | goto error_exit; |
| 1085 | |
| 1086 | if (ecmd->duplex == DUPLEX_FULL) |
| 1087 | priv->duplex = 1; |
| 1088 | else |
| 1089 | priv->duplex = 0; |
| 1090 | |
| 1091 | ravb_set_duplex(ndev); |
| 1092 | |
| 1093 | error_exit: |
| 1094 | mdelay(1); |
| 1095 | |
| 1096 | /* Enable TX and RX */ |
| 1097 | ravb_rcv_snd_enable(ndev); |
| 1098 | |
| 1099 | mmiowb(); |
| 1100 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1101 | |
| 1102 | return error; |
| 1103 | } |
| 1104 | |
| 1105 | static int ravb_nway_reset(struct net_device *ndev) |
| 1106 | { |
| 1107 | struct ravb_private *priv = netdev_priv(ndev); |
| 1108 | int error = -ENODEV; |
| 1109 | unsigned long flags; |
| 1110 | |
| 1111 | if (priv->phydev) { |
| 1112 | spin_lock_irqsave(&priv->lock, flags); |
| 1113 | error = phy_start_aneg(priv->phydev); |
| 1114 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1115 | } |
| 1116 | |
| 1117 | return error; |
| 1118 | } |
| 1119 | |
| 1120 | static u32 ravb_get_msglevel(struct net_device *ndev) |
| 1121 | { |
| 1122 | struct ravb_private *priv = netdev_priv(ndev); |
| 1123 | |
| 1124 | return priv->msg_enable; |
| 1125 | } |
| 1126 | |
| 1127 | static void ravb_set_msglevel(struct net_device *ndev, u32 value) |
| 1128 | { |
| 1129 | struct ravb_private *priv = netdev_priv(ndev); |
| 1130 | |
| 1131 | priv->msg_enable = value; |
| 1132 | } |
| 1133 | |
| 1134 | static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = { |
| 1135 | "rx_queue_0_current", |
| 1136 | "tx_queue_0_current", |
| 1137 | "rx_queue_0_dirty", |
| 1138 | "tx_queue_0_dirty", |
| 1139 | "rx_queue_0_packets", |
| 1140 | "tx_queue_0_packets", |
| 1141 | "rx_queue_0_bytes", |
| 1142 | "tx_queue_0_bytes", |
| 1143 | "rx_queue_0_mcast_packets", |
| 1144 | "rx_queue_0_errors", |
| 1145 | "rx_queue_0_crc_errors", |
| 1146 | "rx_queue_0_frame_errors", |
| 1147 | "rx_queue_0_length_errors", |
| 1148 | "rx_queue_0_missed_errors", |
| 1149 | "rx_queue_0_over_errors", |
| 1150 | |
| 1151 | "rx_queue_1_current", |
| 1152 | "tx_queue_1_current", |
| 1153 | "rx_queue_1_dirty", |
| 1154 | "tx_queue_1_dirty", |
| 1155 | "rx_queue_1_packets", |
| 1156 | "tx_queue_1_packets", |
| 1157 | "rx_queue_1_bytes", |
| 1158 | "tx_queue_1_bytes", |
| 1159 | "rx_queue_1_mcast_packets", |
| 1160 | "rx_queue_1_errors", |
| 1161 | "rx_queue_1_crc_errors", |
Sergei Shtylyov | b17c1d9 | 2015-12-04 01:51:10 +0300 | [diff] [blame] | 1162 | "rx_queue_1_frame_errors", |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1163 | "rx_queue_1_length_errors", |
| 1164 | "rx_queue_1_missed_errors", |
| 1165 | "rx_queue_1_over_errors", |
| 1166 | }; |
| 1167 | |
| 1168 | #define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats) |
| 1169 | |
| 1170 | static int ravb_get_sset_count(struct net_device *netdev, int sset) |
| 1171 | { |
| 1172 | switch (sset) { |
| 1173 | case ETH_SS_STATS: |
| 1174 | return RAVB_STATS_LEN; |
| 1175 | default: |
| 1176 | return -EOPNOTSUPP; |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | static void ravb_get_ethtool_stats(struct net_device *ndev, |
| 1181 | struct ethtool_stats *stats, u64 *data) |
| 1182 | { |
| 1183 | struct ravb_private *priv = netdev_priv(ndev); |
| 1184 | int i = 0; |
| 1185 | int q; |
| 1186 | |
| 1187 | /* Device-specific stats */ |
| 1188 | for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) { |
| 1189 | struct net_device_stats *stats = &priv->stats[q]; |
| 1190 | |
| 1191 | data[i++] = priv->cur_rx[q]; |
| 1192 | data[i++] = priv->cur_tx[q]; |
| 1193 | data[i++] = priv->dirty_rx[q]; |
| 1194 | data[i++] = priv->dirty_tx[q]; |
| 1195 | data[i++] = stats->rx_packets; |
| 1196 | data[i++] = stats->tx_packets; |
| 1197 | data[i++] = stats->rx_bytes; |
| 1198 | data[i++] = stats->tx_bytes; |
| 1199 | data[i++] = stats->multicast; |
| 1200 | data[i++] = stats->rx_errors; |
| 1201 | data[i++] = stats->rx_crc_errors; |
| 1202 | data[i++] = stats->rx_frame_errors; |
| 1203 | data[i++] = stats->rx_length_errors; |
| 1204 | data[i++] = stats->rx_missed_errors; |
| 1205 | data[i++] = stats->rx_over_errors; |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data) |
| 1210 | { |
| 1211 | switch (stringset) { |
| 1212 | case ETH_SS_STATS: |
| 1213 | memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats)); |
| 1214 | break; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | static void ravb_get_ringparam(struct net_device *ndev, |
| 1219 | struct ethtool_ringparam *ring) |
| 1220 | { |
| 1221 | struct ravb_private *priv = netdev_priv(ndev); |
| 1222 | |
| 1223 | ring->rx_max_pending = BE_RX_RING_MAX; |
| 1224 | ring->tx_max_pending = BE_TX_RING_MAX; |
| 1225 | ring->rx_pending = priv->num_rx_ring[RAVB_BE]; |
| 1226 | ring->tx_pending = priv->num_tx_ring[RAVB_BE]; |
| 1227 | } |
| 1228 | |
| 1229 | static int ravb_set_ringparam(struct net_device *ndev, |
| 1230 | struct ethtool_ringparam *ring) |
| 1231 | { |
| 1232 | struct ravb_private *priv = netdev_priv(ndev); |
| 1233 | int error; |
| 1234 | |
| 1235 | if (ring->tx_pending > BE_TX_RING_MAX || |
| 1236 | ring->rx_pending > BE_RX_RING_MAX || |
| 1237 | ring->tx_pending < BE_TX_RING_MIN || |
| 1238 | ring->rx_pending < BE_RX_RING_MIN) |
| 1239 | return -EINVAL; |
| 1240 | if (ring->rx_mini_pending || ring->rx_jumbo_pending) |
| 1241 | return -EINVAL; |
| 1242 | |
| 1243 | if (netif_running(ndev)) { |
| 1244 | netif_device_detach(ndev); |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1245 | /* Stop PTP Clock driver */ |
Sergei Shtylyov | 50bfd83 | 2016-02-06 17:47:22 +0300 | [diff] [blame] | 1246 | if (priv->chip_id == RCAR_GEN2) |
| 1247 | ravb_ptp_stop(ndev); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1248 | /* Wait for DMA stopping */ |
| 1249 | error = ravb_stop_dma(ndev); |
| 1250 | if (error) { |
| 1251 | netdev_err(ndev, |
| 1252 | "cannot set ringparam! Any AVB processes are still running?\n"); |
| 1253 | return error; |
| 1254 | } |
| 1255 | synchronize_irq(ndev->irq); |
| 1256 | |
| 1257 | /* Free all the skb's in the RX queue and the DMA buffers. */ |
| 1258 | ravb_ring_free(ndev, RAVB_BE); |
| 1259 | ravb_ring_free(ndev, RAVB_NC); |
| 1260 | } |
| 1261 | |
| 1262 | /* Set new parameters */ |
| 1263 | priv->num_rx_ring[RAVB_BE] = ring->rx_pending; |
| 1264 | priv->num_tx_ring[RAVB_BE] = ring->tx_pending; |
| 1265 | |
| 1266 | if (netif_running(ndev)) { |
| 1267 | error = ravb_dmac_init(ndev); |
| 1268 | if (error) { |
| 1269 | netdev_err(ndev, |
| 1270 | "%s: ravb_dmac_init() failed, error %d\n", |
| 1271 | __func__, error); |
| 1272 | return error; |
| 1273 | } |
| 1274 | |
| 1275 | ravb_emac_init(ndev); |
| 1276 | |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1277 | /* Initialise PTP Clock driver */ |
Sergei Shtylyov | 50bfd83 | 2016-02-06 17:47:22 +0300 | [diff] [blame] | 1278 | if (priv->chip_id == RCAR_GEN2) |
| 1279 | ravb_ptp_init(ndev, priv->pdev); |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1280 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1281 | netif_device_attach(ndev); |
| 1282 | } |
| 1283 | |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | static int ravb_get_ts_info(struct net_device *ndev, |
| 1288 | struct ethtool_ts_info *info) |
| 1289 | { |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1290 | struct ravb_private *priv = netdev_priv(ndev); |
| 1291 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1292 | info->so_timestamping = |
| 1293 | SOF_TIMESTAMPING_TX_SOFTWARE | |
| 1294 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 1295 | SOF_TIMESTAMPING_SOFTWARE | |
| 1296 | SOF_TIMESTAMPING_TX_HARDWARE | |
| 1297 | SOF_TIMESTAMPING_RX_HARDWARE | |
| 1298 | SOF_TIMESTAMPING_RAW_HARDWARE; |
| 1299 | info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON); |
| 1300 | info->rx_filters = |
| 1301 | (1 << HWTSTAMP_FILTER_NONE) | |
| 1302 | (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | |
| 1303 | (1 << HWTSTAMP_FILTER_ALL); |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1304 | info->phc_index = ptp_clock_index(priv->ptp.clock); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1305 | |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | static const struct ethtool_ops ravb_ethtool_ops = { |
| 1310 | .get_settings = ravb_get_settings, |
| 1311 | .set_settings = ravb_set_settings, |
| 1312 | .nway_reset = ravb_nway_reset, |
| 1313 | .get_msglevel = ravb_get_msglevel, |
| 1314 | .set_msglevel = ravb_set_msglevel, |
| 1315 | .get_link = ethtool_op_get_link, |
| 1316 | .get_strings = ravb_get_strings, |
| 1317 | .get_ethtool_stats = ravb_get_ethtool_stats, |
| 1318 | .get_sset_count = ravb_get_sset_count, |
| 1319 | .get_ringparam = ravb_get_ringparam, |
| 1320 | .set_ringparam = ravb_set_ringparam, |
| 1321 | .get_ts_info = ravb_get_ts_info, |
| 1322 | }; |
| 1323 | |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 1324 | static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler, |
| 1325 | struct net_device *ndev, struct device *dev, |
| 1326 | const char *ch) |
| 1327 | { |
| 1328 | char *name; |
| 1329 | int error; |
| 1330 | |
| 1331 | name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch); |
| 1332 | if (!name) |
| 1333 | return -ENOMEM; |
| 1334 | error = request_irq(irq, handler, 0, name, ndev); |
| 1335 | if (error) |
| 1336 | netdev_err(ndev, "cannot request IRQ %s\n", name); |
| 1337 | |
| 1338 | return error; |
| 1339 | } |
| 1340 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1341 | /* Network device open function for Ethernet AVB */ |
| 1342 | static int ravb_open(struct net_device *ndev) |
| 1343 | { |
| 1344 | struct ravb_private *priv = netdev_priv(ndev); |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 1345 | struct platform_device *pdev = priv->pdev; |
| 1346 | struct device *dev = &pdev->dev; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1347 | int error; |
| 1348 | |
| 1349 | napi_enable(&priv->napi[RAVB_BE]); |
| 1350 | napi_enable(&priv->napi[RAVB_NC]); |
| 1351 | |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 1352 | if (priv->chip_id == RCAR_GEN2) { |
| 1353 | error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED, |
| 1354 | ndev->name, ndev); |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1355 | if (error) { |
| 1356 | netdev_err(ndev, "cannot request IRQ\n"); |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 1357 | goto out_napi_off; |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1358 | } |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 1359 | } else { |
| 1360 | error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev, |
| 1361 | dev, "ch22:multi"); |
| 1362 | if (error) |
| 1363 | goto out_napi_off; |
| 1364 | error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev, |
| 1365 | dev, "ch24:emac"); |
| 1366 | if (error) |
| 1367 | goto out_free_irq; |
| 1368 | error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt, |
| 1369 | ndev, dev, "ch0:rx_be"); |
| 1370 | if (error) |
| 1371 | goto out_free_irq_emac; |
| 1372 | error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt, |
| 1373 | ndev, dev, "ch18:tx_be"); |
| 1374 | if (error) |
| 1375 | goto out_free_irq_be_rx; |
| 1376 | error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt, |
| 1377 | ndev, dev, "ch1:rx_nc"); |
| 1378 | if (error) |
| 1379 | goto out_free_irq_be_tx; |
| 1380 | error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt, |
| 1381 | ndev, dev, "ch19:tx_nc"); |
| 1382 | if (error) |
| 1383 | goto out_free_irq_nc_rx; |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1384 | } |
| 1385 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1386 | /* Device init */ |
| 1387 | error = ravb_dmac_init(ndev); |
| 1388 | if (error) |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 1389 | goto out_free_irq_nc_tx; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1390 | ravb_emac_init(ndev); |
| 1391 | |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1392 | /* Initialise PTP Clock driver */ |
Kazuya Mizuguchi | f5d7837 | 2015-12-02 02:04:39 +0900 | [diff] [blame] | 1393 | if (priv->chip_id == RCAR_GEN2) |
| 1394 | ravb_ptp_init(ndev, priv->pdev); |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1395 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1396 | netif_tx_start_all_queues(ndev); |
| 1397 | |
| 1398 | /* PHY control start */ |
| 1399 | error = ravb_phy_start(ndev); |
| 1400 | if (error) |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1401 | goto out_ptp_stop; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1402 | |
| 1403 | return 0; |
| 1404 | |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1405 | out_ptp_stop: |
| 1406 | /* Stop PTP Clock driver */ |
Kazuya Mizuguchi | f5d7837 | 2015-12-02 02:04:39 +0900 | [diff] [blame] | 1407 | if (priv->chip_id == RCAR_GEN2) |
| 1408 | ravb_ptp_stop(ndev); |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 1409 | out_free_irq_nc_tx: |
| 1410 | if (priv->chip_id == RCAR_GEN2) |
| 1411 | goto out_free_irq; |
| 1412 | free_irq(priv->tx_irqs[RAVB_NC], ndev); |
| 1413 | out_free_irq_nc_rx: |
| 1414 | free_irq(priv->rx_irqs[RAVB_NC], ndev); |
| 1415 | out_free_irq_be_tx: |
| 1416 | free_irq(priv->tx_irqs[RAVB_BE], ndev); |
| 1417 | out_free_irq_be_rx: |
| 1418 | free_irq(priv->rx_irqs[RAVB_BE], ndev); |
| 1419 | out_free_irq_emac: |
| 1420 | free_irq(priv->emac_irq, ndev); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1421 | out_free_irq: |
| 1422 | free_irq(ndev->irq, ndev); |
| 1423 | out_napi_off: |
| 1424 | napi_disable(&priv->napi[RAVB_NC]); |
| 1425 | napi_disable(&priv->napi[RAVB_BE]); |
| 1426 | return error; |
| 1427 | } |
| 1428 | |
| 1429 | /* Timeout function for Ethernet AVB */ |
| 1430 | static void ravb_tx_timeout(struct net_device *ndev) |
| 1431 | { |
| 1432 | struct ravb_private *priv = netdev_priv(ndev); |
| 1433 | |
| 1434 | netif_err(priv, tx_err, ndev, |
| 1435 | "transmit timed out, status %08x, resetting...\n", |
| 1436 | ravb_read(ndev, ISS)); |
| 1437 | |
| 1438 | /* tx_errors count up */ |
| 1439 | ndev->stats.tx_errors++; |
| 1440 | |
| 1441 | schedule_work(&priv->work); |
| 1442 | } |
| 1443 | |
| 1444 | static void ravb_tx_timeout_work(struct work_struct *work) |
| 1445 | { |
| 1446 | struct ravb_private *priv = container_of(work, struct ravb_private, |
| 1447 | work); |
| 1448 | struct net_device *ndev = priv->ndev; |
| 1449 | |
| 1450 | netif_tx_stop_all_queues(ndev); |
| 1451 | |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1452 | /* Stop PTP Clock driver */ |
Sergei Shtylyov | 50bfd83 | 2016-02-06 17:47:22 +0300 | [diff] [blame] | 1453 | if (priv->chip_id == RCAR_GEN2) |
| 1454 | ravb_ptp_stop(ndev); |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1455 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1456 | /* Wait for DMA stopping */ |
| 1457 | ravb_stop_dma(ndev); |
| 1458 | |
| 1459 | ravb_ring_free(ndev, RAVB_BE); |
| 1460 | ravb_ring_free(ndev, RAVB_NC); |
| 1461 | |
| 1462 | /* Device init */ |
| 1463 | ravb_dmac_init(ndev); |
| 1464 | ravb_emac_init(ndev); |
| 1465 | |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1466 | /* Initialise PTP Clock driver */ |
Sergei Shtylyov | 50bfd83 | 2016-02-06 17:47:22 +0300 | [diff] [blame] | 1467 | if (priv->chip_id == RCAR_GEN2) |
| 1468 | ravb_ptp_init(ndev, priv->pdev); |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1469 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1470 | netif_tx_start_all_queues(ndev); |
| 1471 | } |
| 1472 | |
| 1473 | /* Packet transmit function for Ethernet AVB */ |
| 1474 | static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 1475 | { |
| 1476 | struct ravb_private *priv = netdev_priv(ndev); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1477 | u16 q = skb_get_queue_mapping(skb); |
Sergei Shtylyov | aad0d51 | 2015-07-10 21:10:10 +0300 | [diff] [blame] | 1478 | struct ravb_tstamp_skb *ts_skb; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1479 | struct ravb_tx_desc *desc; |
| 1480 | unsigned long flags; |
| 1481 | u32 dma_addr; |
| 1482 | void *buffer; |
| 1483 | u32 entry; |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1484 | u32 len; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1485 | |
| 1486 | spin_lock_irqsave(&priv->lock, flags); |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1487 | if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) * |
| 1488 | NUM_TX_DESC) { |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1489 | netif_err(priv, tx_queued, ndev, |
| 1490 | "still transmitting with the full ring!\n"); |
| 1491 | netif_stop_subqueue(ndev, q); |
| 1492 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1493 | return NETDEV_TX_BUSY; |
| 1494 | } |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1495 | entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC); |
| 1496 | priv->tx_skb[q][entry / NUM_TX_DESC] = skb; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1497 | |
| 1498 | if (skb_put_padto(skb, ETH_ZLEN)) |
| 1499 | goto drop; |
| 1500 | |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1501 | buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) + |
| 1502 | entry / NUM_TX_DESC * DPTR_ALIGN; |
| 1503 | len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data; |
| 1504 | memcpy(buffer, skb->data, len); |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 1505 | dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE); |
| 1506 | if (dma_mapping_error(ndev->dev.parent, dma_addr)) |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1507 | goto drop; |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1508 | |
| 1509 | desc = &priv->tx_ring[q][entry]; |
| 1510 | desc->ds_tagl = cpu_to_le16(len); |
| 1511 | desc->dptr = cpu_to_le32(dma_addr); |
| 1512 | |
| 1513 | buffer = skb->data + len; |
| 1514 | len = skb->len - len; |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 1515 | dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE); |
| 1516 | if (dma_mapping_error(ndev->dev.parent, dma_addr)) |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1517 | goto unmap; |
| 1518 | |
| 1519 | desc++; |
| 1520 | desc->ds_tagl = cpu_to_le16(len); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1521 | desc->dptr = cpu_to_le32(dma_addr); |
| 1522 | |
| 1523 | /* TX timestamp required */ |
| 1524 | if (q == RAVB_NC) { |
| 1525 | ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC); |
| 1526 | if (!ts_skb) { |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1527 | desc--; |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 1528 | dma_unmap_single(ndev->dev.parent, dma_addr, len, |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1529 | DMA_TO_DEVICE); |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1530 | goto unmap; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1531 | } |
| 1532 | ts_skb->skb = skb; |
| 1533 | ts_skb->tag = priv->ts_skb_tag++; |
| 1534 | priv->ts_skb_tag &= 0x3ff; |
| 1535 | list_add_tail(&ts_skb->list, &priv->ts_skb_list); |
| 1536 | |
| 1537 | /* TAG and timestamp required flag */ |
| 1538 | skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1539 | desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR; |
| 1540 | desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12); |
| 1541 | } |
| 1542 | |
Lino Sanfilippo | d7be81a | 2016-03-27 12:22:02 +0200 | [diff] [blame] | 1543 | skb_tx_timestamp(skb); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1544 | /* Descriptor type must be set after all the above writes */ |
| 1545 | dma_wmb(); |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1546 | desc->die_dt = DT_FEND; |
| 1547 | desc--; |
| 1548 | desc->die_dt = DT_FSTART; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1549 | |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 1550 | ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1551 | |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1552 | priv->cur_tx[q] += NUM_TX_DESC; |
| 1553 | if (priv->cur_tx[q] - priv->dirty_tx[q] > |
| 1554 | (priv->num_tx_ring[q] - 1) * NUM_TX_DESC && !ravb_tx_free(ndev, q)) |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1555 | netif_stop_subqueue(ndev, q); |
| 1556 | |
| 1557 | exit: |
| 1558 | mmiowb(); |
| 1559 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1560 | return NETDEV_TX_OK; |
| 1561 | |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1562 | unmap: |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 1563 | dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr), |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1564 | le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1565 | drop: |
| 1566 | dev_kfree_skb_any(skb); |
Sergei Shtylyov | 2f45d19 | 2015-07-25 23:42:01 +0300 | [diff] [blame] | 1567 | priv->tx_skb[q][entry / NUM_TX_DESC] = NULL; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1568 | goto exit; |
| 1569 | } |
| 1570 | |
| 1571 | static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb, |
| 1572 | void *accel_priv, select_queue_fallback_t fallback) |
| 1573 | { |
| 1574 | /* If skb needs TX timestamp, it is handled in network control queue */ |
| 1575 | return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC : |
| 1576 | RAVB_BE; |
| 1577 | |
| 1578 | } |
| 1579 | |
| 1580 | static struct net_device_stats *ravb_get_stats(struct net_device *ndev) |
| 1581 | { |
| 1582 | struct ravb_private *priv = netdev_priv(ndev); |
| 1583 | struct net_device_stats *nstats, *stats0, *stats1; |
| 1584 | |
| 1585 | nstats = &ndev->stats; |
| 1586 | stats0 = &priv->stats[RAVB_BE]; |
| 1587 | stats1 = &priv->stats[RAVB_NC]; |
| 1588 | |
| 1589 | nstats->tx_dropped += ravb_read(ndev, TROCR); |
| 1590 | ravb_write(ndev, 0, TROCR); /* (write clear) */ |
| 1591 | nstats->collisions += ravb_read(ndev, CDCR); |
| 1592 | ravb_write(ndev, 0, CDCR); /* (write clear) */ |
| 1593 | nstats->tx_carrier_errors += ravb_read(ndev, LCCR); |
| 1594 | ravb_write(ndev, 0, LCCR); /* (write clear) */ |
| 1595 | |
| 1596 | nstats->tx_carrier_errors += ravb_read(ndev, CERCR); |
| 1597 | ravb_write(ndev, 0, CERCR); /* (write clear) */ |
| 1598 | nstats->tx_carrier_errors += ravb_read(ndev, CEECR); |
| 1599 | ravb_write(ndev, 0, CEECR); /* (write clear) */ |
| 1600 | |
| 1601 | nstats->rx_packets = stats0->rx_packets + stats1->rx_packets; |
| 1602 | nstats->tx_packets = stats0->tx_packets + stats1->tx_packets; |
| 1603 | nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes; |
| 1604 | nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes; |
| 1605 | nstats->multicast = stats0->multicast + stats1->multicast; |
| 1606 | nstats->rx_errors = stats0->rx_errors + stats1->rx_errors; |
| 1607 | nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors; |
| 1608 | nstats->rx_frame_errors = |
| 1609 | stats0->rx_frame_errors + stats1->rx_frame_errors; |
| 1610 | nstats->rx_length_errors = |
| 1611 | stats0->rx_length_errors + stats1->rx_length_errors; |
| 1612 | nstats->rx_missed_errors = |
| 1613 | stats0->rx_missed_errors + stats1->rx_missed_errors; |
| 1614 | nstats->rx_over_errors = |
| 1615 | stats0->rx_over_errors + stats1->rx_over_errors; |
| 1616 | |
| 1617 | return nstats; |
| 1618 | } |
| 1619 | |
| 1620 | /* Update promiscuous bit */ |
| 1621 | static void ravb_set_rx_mode(struct net_device *ndev) |
| 1622 | { |
| 1623 | struct ravb_private *priv = netdev_priv(ndev); |
| 1624 | unsigned long flags; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1625 | |
| 1626 | spin_lock_irqsave(&priv->lock, flags); |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 1627 | ravb_modify(ndev, ECMR, ECMR_PRM, |
| 1628 | ndev->flags & IFF_PROMISC ? ECMR_PRM : 0); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1629 | mmiowb(); |
| 1630 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1631 | } |
| 1632 | |
| 1633 | /* Device close function for Ethernet AVB */ |
| 1634 | static int ravb_close(struct net_device *ndev) |
| 1635 | { |
| 1636 | struct ravb_private *priv = netdev_priv(ndev); |
| 1637 | struct ravb_tstamp_skb *ts_skb, *ts_skb2; |
| 1638 | |
| 1639 | netif_tx_stop_all_queues(ndev); |
| 1640 | |
| 1641 | /* Disable interrupts by clearing the interrupt masks. */ |
| 1642 | ravb_write(ndev, 0, RIC0); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1643 | ravb_write(ndev, 0, RIC2); |
| 1644 | ravb_write(ndev, 0, TIC); |
| 1645 | |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1646 | /* Stop PTP Clock driver */ |
Kazuya Mizuguchi | f5d7837 | 2015-12-02 02:04:39 +0900 | [diff] [blame] | 1647 | if (priv->chip_id == RCAR_GEN2) |
| 1648 | ravb_ptp_stop(ndev); |
Sergei Shtylyov | a0d2f20 | 2015-06-11 01:02:30 +0300 | [diff] [blame] | 1649 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1650 | /* Set the config mode to stop the AVB-DMAC's processes */ |
| 1651 | if (ravb_stop_dma(ndev) < 0) |
| 1652 | netdev_err(ndev, |
| 1653 | "device will be stopped after h/w processes are done.\n"); |
| 1654 | |
| 1655 | /* Clear the timestamp list */ |
| 1656 | list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) { |
| 1657 | list_del(&ts_skb->list); |
| 1658 | kfree(ts_skb); |
| 1659 | } |
| 1660 | |
| 1661 | /* PHY disconnect */ |
| 1662 | if (priv->phydev) { |
| 1663 | phy_stop(priv->phydev); |
| 1664 | phy_disconnect(priv->phydev); |
| 1665 | priv->phydev = NULL; |
| 1666 | } |
| 1667 | |
| 1668 | free_irq(ndev->irq, ndev); |
| 1669 | |
| 1670 | napi_disable(&priv->napi[RAVB_NC]); |
| 1671 | napi_disable(&priv->napi[RAVB_BE]); |
| 1672 | |
| 1673 | /* Free all the skb's in the RX queue and the DMA buffers. */ |
| 1674 | ravb_ring_free(ndev, RAVB_BE); |
| 1675 | ravb_ring_free(ndev, RAVB_NC); |
| 1676 | |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req) |
| 1681 | { |
| 1682 | struct ravb_private *priv = netdev_priv(ndev); |
| 1683 | struct hwtstamp_config config; |
| 1684 | |
| 1685 | config.flags = 0; |
| 1686 | config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON : |
| 1687 | HWTSTAMP_TX_OFF; |
| 1688 | if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT) |
| 1689 | config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT; |
| 1690 | else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL) |
| 1691 | config.rx_filter = HWTSTAMP_FILTER_ALL; |
| 1692 | else |
| 1693 | config.rx_filter = HWTSTAMP_FILTER_NONE; |
| 1694 | |
| 1695 | return copy_to_user(req->ifr_data, &config, sizeof(config)) ? |
| 1696 | -EFAULT : 0; |
| 1697 | } |
| 1698 | |
| 1699 | /* Control hardware time stamping */ |
| 1700 | static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req) |
| 1701 | { |
| 1702 | struct ravb_private *priv = netdev_priv(ndev); |
| 1703 | struct hwtstamp_config config; |
| 1704 | u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED; |
| 1705 | u32 tstamp_tx_ctrl; |
| 1706 | |
| 1707 | if (copy_from_user(&config, req->ifr_data, sizeof(config))) |
| 1708 | return -EFAULT; |
| 1709 | |
| 1710 | /* Reserved for future extensions */ |
| 1711 | if (config.flags) |
| 1712 | return -EINVAL; |
| 1713 | |
| 1714 | switch (config.tx_type) { |
| 1715 | case HWTSTAMP_TX_OFF: |
| 1716 | tstamp_tx_ctrl = 0; |
| 1717 | break; |
| 1718 | case HWTSTAMP_TX_ON: |
| 1719 | tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED; |
| 1720 | break; |
| 1721 | default: |
| 1722 | return -ERANGE; |
| 1723 | } |
| 1724 | |
| 1725 | switch (config.rx_filter) { |
| 1726 | case HWTSTAMP_FILTER_NONE: |
| 1727 | tstamp_rx_ctrl = 0; |
| 1728 | break; |
| 1729 | case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: |
| 1730 | tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT; |
| 1731 | break; |
| 1732 | default: |
| 1733 | config.rx_filter = HWTSTAMP_FILTER_ALL; |
| 1734 | tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL; |
| 1735 | } |
| 1736 | |
| 1737 | priv->tstamp_tx_ctrl = tstamp_tx_ctrl; |
| 1738 | priv->tstamp_rx_ctrl = tstamp_rx_ctrl; |
| 1739 | |
| 1740 | return copy_to_user(req->ifr_data, &config, sizeof(config)) ? |
| 1741 | -EFAULT : 0; |
| 1742 | } |
| 1743 | |
| 1744 | /* ioctl to device function */ |
| 1745 | static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd) |
| 1746 | { |
| 1747 | struct ravb_private *priv = netdev_priv(ndev); |
| 1748 | struct phy_device *phydev = priv->phydev; |
| 1749 | |
| 1750 | if (!netif_running(ndev)) |
| 1751 | return -EINVAL; |
| 1752 | |
| 1753 | if (!phydev) |
| 1754 | return -ENODEV; |
| 1755 | |
| 1756 | switch (cmd) { |
| 1757 | case SIOCGHWTSTAMP: |
| 1758 | return ravb_hwtstamp_get(ndev, req); |
| 1759 | case SIOCSHWTSTAMP: |
| 1760 | return ravb_hwtstamp_set(ndev, req); |
| 1761 | } |
| 1762 | |
| 1763 | return phy_mii_ioctl(phydev, req, cmd); |
| 1764 | } |
| 1765 | |
| 1766 | static const struct net_device_ops ravb_netdev_ops = { |
| 1767 | .ndo_open = ravb_open, |
| 1768 | .ndo_stop = ravb_close, |
| 1769 | .ndo_start_xmit = ravb_start_xmit, |
| 1770 | .ndo_select_queue = ravb_select_queue, |
| 1771 | .ndo_get_stats = ravb_get_stats, |
| 1772 | .ndo_set_rx_mode = ravb_set_rx_mode, |
| 1773 | .ndo_tx_timeout = ravb_tx_timeout, |
| 1774 | .ndo_do_ioctl = ravb_do_ioctl, |
| 1775 | .ndo_validate_addr = eth_validate_addr, |
| 1776 | .ndo_set_mac_address = eth_mac_addr, |
| 1777 | .ndo_change_mtu = eth_change_mtu, |
| 1778 | }; |
| 1779 | |
| 1780 | /* MDIO bus init function */ |
| 1781 | static int ravb_mdio_init(struct ravb_private *priv) |
| 1782 | { |
| 1783 | struct platform_device *pdev = priv->pdev; |
| 1784 | struct device *dev = &pdev->dev; |
| 1785 | int error; |
| 1786 | |
| 1787 | /* Bitbang init */ |
| 1788 | priv->mdiobb.ops = &bb_ops; |
| 1789 | |
| 1790 | /* MII controller setting */ |
| 1791 | priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb); |
| 1792 | if (!priv->mii_bus) |
| 1793 | return -ENOMEM; |
| 1794 | |
| 1795 | /* Hook up MII support for ethtool */ |
| 1796 | priv->mii_bus->name = "ravb_mii"; |
| 1797 | priv->mii_bus->parent = dev; |
| 1798 | snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", |
| 1799 | pdev->name, pdev->id); |
| 1800 | |
| 1801 | /* Register MDIO bus */ |
| 1802 | error = of_mdiobus_register(priv->mii_bus, dev->of_node); |
| 1803 | if (error) |
| 1804 | goto out_free_bus; |
| 1805 | |
| 1806 | return 0; |
| 1807 | |
| 1808 | out_free_bus: |
| 1809 | free_mdio_bitbang(priv->mii_bus); |
| 1810 | return error; |
| 1811 | } |
| 1812 | |
| 1813 | /* MDIO bus release function */ |
| 1814 | static int ravb_mdio_release(struct ravb_private *priv) |
| 1815 | { |
| 1816 | /* Unregister mdio bus */ |
| 1817 | mdiobus_unregister(priv->mii_bus); |
| 1818 | |
| 1819 | /* Free bitbang info */ |
| 1820 | free_mdio_bitbang(priv->mii_bus); |
| 1821 | |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1825 | static const struct of_device_id ravb_match_table[] = { |
| 1826 | { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 }, |
| 1827 | { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 }, |
Simon Horman | 0e87436 | 2015-12-02 14:58:32 +0900 | [diff] [blame] | 1828 | { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 }, |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1829 | { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 }, |
Simon Horman | 0e87436 | 2015-12-02 14:58:32 +0900 | [diff] [blame] | 1830 | { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 }, |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1831 | { } |
| 1832 | }; |
| 1833 | MODULE_DEVICE_TABLE(of, ravb_match_table); |
| 1834 | |
Simon Horman | b3d39a8 | 2015-11-20 11:29:39 -0800 | [diff] [blame] | 1835 | static int ravb_set_gti(struct net_device *ndev) |
| 1836 | { |
| 1837 | |
| 1838 | struct device *dev = ndev->dev.parent; |
| 1839 | struct device_node *np = dev->of_node; |
| 1840 | unsigned long rate; |
| 1841 | struct clk *clk; |
| 1842 | uint64_t inc; |
| 1843 | |
| 1844 | clk = of_clk_get(np, 0); |
| 1845 | if (IS_ERR(clk)) { |
| 1846 | dev_err(dev, "could not get clock\n"); |
| 1847 | return PTR_ERR(clk); |
| 1848 | } |
| 1849 | |
| 1850 | rate = clk_get_rate(clk); |
| 1851 | clk_put(clk); |
| 1852 | |
| 1853 | inc = 1000000000ULL << 20; |
| 1854 | do_div(inc, rate); |
| 1855 | |
| 1856 | if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) { |
| 1857 | dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n", |
| 1858 | inc, GTI_TIV_MIN, GTI_TIV_MAX); |
| 1859 | return -EINVAL; |
| 1860 | } |
| 1861 | |
| 1862 | ravb_write(ndev, inc, GTI); |
| 1863 | |
| 1864 | return 0; |
| 1865 | } |
| 1866 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1867 | static int ravb_probe(struct platform_device *pdev) |
| 1868 | { |
| 1869 | struct device_node *np = pdev->dev.of_node; |
| 1870 | struct ravb_private *priv; |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1871 | enum ravb_chip_id chip_id; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1872 | struct net_device *ndev; |
| 1873 | int error, irq, q; |
| 1874 | struct resource *res; |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 1875 | int i; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1876 | |
| 1877 | if (!np) { |
| 1878 | dev_err(&pdev->dev, |
| 1879 | "this driver is required to be instantiated from device tree\n"); |
| 1880 | return -EINVAL; |
| 1881 | } |
| 1882 | |
| 1883 | /* Get base address */ |
| 1884 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1885 | if (!res) { |
| 1886 | dev_err(&pdev->dev, "invalid resource\n"); |
| 1887 | return -EINVAL; |
| 1888 | } |
| 1889 | |
| 1890 | ndev = alloc_etherdev_mqs(sizeof(struct ravb_private), |
| 1891 | NUM_TX_QUEUE, NUM_RX_QUEUE); |
| 1892 | if (!ndev) |
| 1893 | return -ENOMEM; |
| 1894 | |
| 1895 | pm_runtime_enable(&pdev->dev); |
| 1896 | pm_runtime_get_sync(&pdev->dev); |
| 1897 | |
| 1898 | /* The Ether-specific entries in the device structure. */ |
| 1899 | ndev->base_addr = res->start; |
| 1900 | ndev->dma = -1; |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1901 | |
Wolfram Sang | e866863 | 2016-03-01 17:37:58 +0100 | [diff] [blame] | 1902 | chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev); |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1903 | |
| 1904 | if (chip_id == RCAR_GEN3) |
| 1905 | irq = platform_get_irq_byname(pdev, "ch22"); |
| 1906 | else |
| 1907 | irq = platform_get_irq(pdev, 0); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1908 | if (irq < 0) { |
Sergei Shtylyov | f375339 | 2015-08-28 16:55:10 +0300 | [diff] [blame] | 1909 | error = irq; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1910 | goto out_release; |
| 1911 | } |
| 1912 | ndev->irq = irq; |
| 1913 | |
| 1914 | SET_NETDEV_DEV(ndev, &pdev->dev); |
| 1915 | |
| 1916 | priv = netdev_priv(ndev); |
| 1917 | priv->ndev = ndev; |
| 1918 | priv->pdev = pdev; |
| 1919 | priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE; |
| 1920 | priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE; |
| 1921 | priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE; |
| 1922 | priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE; |
| 1923 | priv->addr = devm_ioremap_resource(&pdev->dev, res); |
| 1924 | if (IS_ERR(priv->addr)) { |
| 1925 | error = PTR_ERR(priv->addr); |
| 1926 | goto out_release; |
| 1927 | } |
| 1928 | |
| 1929 | spin_lock_init(&priv->lock); |
| 1930 | INIT_WORK(&priv->work, ravb_tx_timeout_work); |
| 1931 | |
| 1932 | priv->phy_interface = of_get_phy_mode(np); |
| 1933 | |
| 1934 | priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link"); |
| 1935 | priv->avb_link_active_low = |
| 1936 | of_property_read_bool(np, "renesas,ether-link-active-low"); |
| 1937 | |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1938 | if (chip_id == RCAR_GEN3) { |
| 1939 | irq = platform_get_irq_byname(pdev, "ch24"); |
| 1940 | if (irq < 0) { |
| 1941 | error = irq; |
| 1942 | goto out_release; |
| 1943 | } |
| 1944 | priv->emac_irq = irq; |
Kazuya Mizuguchi | f51bdc2 | 2016-04-03 23:54:38 +0900 | [diff] [blame^] | 1945 | for (i = 0; i < NUM_RX_QUEUE; i++) { |
| 1946 | irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]); |
| 1947 | if (irq < 0) { |
| 1948 | error = irq; |
| 1949 | goto out_release; |
| 1950 | } |
| 1951 | priv->rx_irqs[i] = irq; |
| 1952 | } |
| 1953 | for (i = 0; i < NUM_TX_QUEUE; i++) { |
| 1954 | irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]); |
| 1955 | if (irq < 0) { |
| 1956 | error = irq; |
| 1957 | goto out_release; |
| 1958 | } |
| 1959 | priv->tx_irqs[i] = irq; |
| 1960 | } |
Kazuya Mizuguchi | 22d4df8 | 2015-09-30 15:15:55 +0900 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | priv->chip_id = chip_id; |
| 1964 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1965 | /* Set function */ |
| 1966 | ndev->netdev_ops = &ravb_netdev_ops; |
| 1967 | ndev->ethtool_ops = &ravb_ethtool_ops; |
| 1968 | |
| 1969 | /* Set AVB config mode */ |
Kazuya Mizuguchi | f5d7837 | 2015-12-02 02:04:39 +0900 | [diff] [blame] | 1970 | if (chip_id == RCAR_GEN2) { |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 1971 | ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG); |
Kazuya Mizuguchi | f5d7837 | 2015-12-02 02:04:39 +0900 | [diff] [blame] | 1972 | /* Set CSEL value */ |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 1973 | ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB); |
Kazuya Mizuguchi | f5d7837 | 2015-12-02 02:04:39 +0900 | [diff] [blame] | 1974 | } else { |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 1975 | ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG | |
| 1976 | CCC_GAC | CCC_CSEL_HPB); |
Kazuya Mizuguchi | f5d7837 | 2015-12-02 02:04:39 +0900 | [diff] [blame] | 1977 | } |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1978 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1979 | /* Set GTI value */ |
Simon Horman | b3d39a8 | 2015-11-20 11:29:39 -0800 | [diff] [blame] | 1980 | error = ravb_set_gti(ndev); |
| 1981 | if (error) |
| 1982 | goto out_release; |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1983 | |
| 1984 | /* Request GTI loading */ |
Sergei Shtylyov | 568b3ce | 2016-02-10 01:37:44 +0300 | [diff] [blame] | 1985 | ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1986 | |
| 1987 | /* Allocate descriptor base address table */ |
| 1988 | priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM; |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 1989 | priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size, |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1990 | &priv->desc_bat_dma, GFP_KERNEL); |
| 1991 | if (!priv->desc_bat) { |
Simon Horman | c451113 | 2015-11-02 10:40:17 +0900 | [diff] [blame] | 1992 | dev_err(&pdev->dev, |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 1993 | "Cannot allocate desc base address table (size %d bytes)\n", |
| 1994 | priv->desc_bat_size); |
| 1995 | error = -ENOMEM; |
| 1996 | goto out_release; |
| 1997 | } |
| 1998 | for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++) |
| 1999 | priv->desc_bat[q].die_dt = DT_EOS; |
| 2000 | ravb_write(ndev, priv->desc_bat_dma, DBAT); |
| 2001 | |
| 2002 | /* Initialise HW timestamp list */ |
| 2003 | INIT_LIST_HEAD(&priv->ts_skb_list); |
| 2004 | |
Kazuya Mizuguchi | f5d7837 | 2015-12-02 02:04:39 +0900 | [diff] [blame] | 2005 | /* Initialise PTP Clock driver */ |
| 2006 | if (chip_id != RCAR_GEN2) |
| 2007 | ravb_ptp_init(ndev, pdev); |
| 2008 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 2009 | /* Debug message level */ |
| 2010 | priv->msg_enable = RAVB_DEF_MSG_ENABLE; |
| 2011 | |
| 2012 | /* Read and set MAC address */ |
| 2013 | ravb_read_mac_address(ndev, of_get_mac_address(np)); |
| 2014 | if (!is_valid_ether_addr(ndev->dev_addr)) { |
| 2015 | dev_warn(&pdev->dev, |
| 2016 | "no valid MAC address supplied, using a random one\n"); |
| 2017 | eth_hw_addr_random(ndev); |
| 2018 | } |
| 2019 | |
| 2020 | /* MDIO bus init */ |
| 2021 | error = ravb_mdio_init(priv); |
| 2022 | if (error) { |
Simon Horman | c451113 | 2015-11-02 10:40:17 +0900 | [diff] [blame] | 2023 | dev_err(&pdev->dev, "failed to initialize MDIO\n"); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 2024 | goto out_dma_free; |
| 2025 | } |
| 2026 | |
| 2027 | netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64); |
| 2028 | netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64); |
| 2029 | |
| 2030 | /* Network device register */ |
| 2031 | error = register_netdev(ndev); |
| 2032 | if (error) |
| 2033 | goto out_napi_del; |
| 2034 | |
| 2035 | /* Print device information */ |
| 2036 | netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n", |
| 2037 | (u32)ndev->base_addr, ndev->dev_addr, ndev->irq); |
| 2038 | |
| 2039 | platform_set_drvdata(pdev, ndev); |
| 2040 | |
| 2041 | return 0; |
| 2042 | |
| 2043 | out_napi_del: |
| 2044 | netif_napi_del(&priv->napi[RAVB_NC]); |
| 2045 | netif_napi_del(&priv->napi[RAVB_BE]); |
| 2046 | ravb_mdio_release(priv); |
| 2047 | out_dma_free: |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 2048 | dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat, |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 2049 | priv->desc_bat_dma); |
Kazuya Mizuguchi | f5d7837 | 2015-12-02 02:04:39 +0900 | [diff] [blame] | 2050 | |
| 2051 | /* Stop PTP Clock driver */ |
| 2052 | if (chip_id != RCAR_GEN2) |
| 2053 | ravb_ptp_stop(ndev); |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 2054 | out_release: |
| 2055 | if (ndev) |
| 2056 | free_netdev(ndev); |
| 2057 | |
| 2058 | pm_runtime_put(&pdev->dev); |
| 2059 | pm_runtime_disable(&pdev->dev); |
| 2060 | return error; |
| 2061 | } |
| 2062 | |
| 2063 | static int ravb_remove(struct platform_device *pdev) |
| 2064 | { |
| 2065 | struct net_device *ndev = platform_get_drvdata(pdev); |
| 2066 | struct ravb_private *priv = netdev_priv(ndev); |
| 2067 | |
Kazuya Mizuguchi | f5d7837 | 2015-12-02 02:04:39 +0900 | [diff] [blame] | 2068 | /* Stop PTP Clock driver */ |
| 2069 | if (priv->chip_id != RCAR_GEN2) |
| 2070 | ravb_ptp_stop(ndev); |
| 2071 | |
Kazuya Mizuguchi | e2dbb33 | 2015-09-30 15:15:53 +0900 | [diff] [blame] | 2072 | dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat, |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 2073 | priv->desc_bat_dma); |
| 2074 | /* Set reset mode */ |
| 2075 | ravb_write(ndev, CCC_OPC_RESET, CCC); |
| 2076 | pm_runtime_put_sync(&pdev->dev); |
| 2077 | unregister_netdev(ndev); |
| 2078 | netif_napi_del(&priv->napi[RAVB_NC]); |
| 2079 | netif_napi_del(&priv->napi[RAVB_BE]); |
| 2080 | ravb_mdio_release(priv); |
| 2081 | pm_runtime_disable(&pdev->dev); |
| 2082 | free_netdev(ndev); |
| 2083 | platform_set_drvdata(pdev, NULL); |
| 2084 | |
| 2085 | return 0; |
| 2086 | } |
| 2087 | |
| 2088 | #ifdef CONFIG_PM |
| 2089 | static int ravb_runtime_nop(struct device *dev) |
| 2090 | { |
| 2091 | /* Runtime PM callback shared between ->runtime_suspend() |
| 2092 | * and ->runtime_resume(). Simply returns success. |
| 2093 | * |
| 2094 | * This driver re-initializes all registers after |
| 2095 | * pm_runtime_get_sync() anyway so there is no need |
| 2096 | * to save and restore registers here. |
| 2097 | */ |
| 2098 | return 0; |
| 2099 | } |
| 2100 | |
| 2101 | static const struct dev_pm_ops ravb_dev_pm_ops = { |
| 2102 | .runtime_suspend = ravb_runtime_nop, |
| 2103 | .runtime_resume = ravb_runtime_nop, |
| 2104 | }; |
| 2105 | |
| 2106 | #define RAVB_PM_OPS (&ravb_dev_pm_ops) |
| 2107 | #else |
| 2108 | #define RAVB_PM_OPS NULL |
| 2109 | #endif |
| 2110 | |
Sergei Shtylyov | c156633 | 2015-06-11 01:01:43 +0300 | [diff] [blame] | 2111 | static struct platform_driver ravb_driver = { |
| 2112 | .probe = ravb_probe, |
| 2113 | .remove = ravb_remove, |
| 2114 | .driver = { |
| 2115 | .name = "ravb", |
| 2116 | .pm = RAVB_PM_OPS, |
| 2117 | .of_match_table = ravb_match_table, |
| 2118 | }, |
| 2119 | }; |
| 2120 | |
| 2121 | module_platform_driver(ravb_driver); |
| 2122 | |
| 2123 | MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai"); |
| 2124 | MODULE_DESCRIPTION("Renesas Ethernet AVB driver"); |
| 2125 | MODULE_LICENSE("GPL v2"); |