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