blob: ef8f08931fe8b9d3cb75daee8a09c18aaa858f5f [file] [log] [blame]
Wolfram Sang00e1cae2018-08-22 00:02:19 +02001// SPDX-License-Identifier: GPL-2.0
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002/* Renesas Ethernet AVB device driver
3 *
4 * Copyright (C) 2014-2015 Renesas Electronics Corporation
5 * Copyright (C) 2015 Renesas Solutions Corp.
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03006 * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
Sergei Shtylyovc1566332015-06-11 01:01:43 +03007 *
8 * Based on the SuperH Ethernet driver
Sergei Shtylyovc1566332015-06-11 01:01:43 +03009 */
10
11#include <linux/cache.h>
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/dma-mapping.h>
15#include <linux/err.h>
16#include <linux/etherdevice.h>
17#include <linux/ethtool.h>
18#include <linux/if_vlan.h>
19#include <linux/kernel.h>
20#include <linux/list.h>
21#include <linux/module.h>
22#include <linux/net_tstamp.h>
23#include <linux/of.h>
24#include <linux/of_device.h>
25#include <linux/of_irq.h>
26#include <linux/of_mdio.h>
27#include <linux/of_net.h>
Sergei Shtylyovc1566332015-06-11 01:01:43 +030028#include <linux/pm_runtime.h>
29#include <linux/slab.h>
30#include <linux/spinlock.h>
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +010031#include <linux/sys_soc.h>
Sergei Shtylyovc1566332015-06-11 01:01:43 +030032
Simon Hormanb3d39a82015-11-20 11:29:39 -080033#include <asm/div64.h>
34
Sergei Shtylyovc1566332015-06-11 01:01:43 +030035#include "ravb.h"
36
37#define RAVB_DEF_MSG_ENABLE \
38 (NETIF_MSG_LINK | \
39 NETIF_MSG_TIMER | \
40 NETIF_MSG_RX_ERR | \
41 NETIF_MSG_TX_ERR)
42
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +090043static const char *ravb_rx_irqs[NUM_RX_QUEUE] = {
44 "ch0", /* RAVB_BE */
45 "ch1", /* RAVB_NC */
46};
47
48static const char *ravb_tx_irqs[NUM_TX_QUEUE] = {
49 "ch18", /* RAVB_BE */
50 "ch19", /* RAVB_NC */
51};
52
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030053void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear,
54 u32 set)
55{
56 ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg);
57}
58
Sergei Shtylyova0d2f202015-06-11 01:02:30 +030059int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value)
Sergei Shtylyovc1566332015-06-11 01:01:43 +030060{
61 int i;
62
63 for (i = 0; i < 10000; i++) {
64 if ((ravb_read(ndev, reg) & mask) == value)
65 return 0;
66 udelay(10);
67 }
68 return -ETIMEDOUT;
69}
70
71static int ravb_config(struct net_device *ndev)
72{
73 int error;
74
75 /* Set config mode */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030076 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030077 /* Check if the operating mode is changed to the config mode */
78 error = ravb_wait(ndev, CSR, CSR_OPS, CSR_OPS_CONFIG);
79 if (error)
80 netdev_err(ndev, "failed to switch device to config mode\n");
81
82 return error;
83}
84
Sergei Shtylyovc1566332015-06-11 01:01:43 +030085static void ravb_set_rate(struct net_device *ndev)
86{
87 struct ravb_private *priv = netdev_priv(ndev);
88
89 switch (priv->speed) {
90 case 100: /* 100BASE */
91 ravb_write(ndev, GECMR_SPEED_100, GECMR);
92 break;
93 case 1000: /* 1000BASE */
94 ravb_write(ndev, GECMR_SPEED_1000, GECMR);
95 break;
Sergei Shtylyovc1566332015-06-11 01:01:43 +030096 }
97}
98
99static void ravb_set_buffer_align(struct sk_buff *skb)
100{
101 u32 reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1);
102
103 if (reserve)
104 skb_reserve(skb, RAVB_ALIGN - reserve);
105}
106
107/* Get MAC address from the MAC address registers
108 *
109 * Ethernet AVB device doesn't have ROM for MAC address.
110 * This function gets the MAC address that was used by a bootloader.
111 */
112static void ravb_read_mac_address(struct net_device *ndev, const u8 *mac)
113{
Petr Štetiara51645f2019-05-06 23:27:04 +0200114 if (!IS_ERR(mac)) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300115 ether_addr_copy(ndev->dev_addr, mac);
116 } else {
Sergei Shtylyovd9660632015-12-05 00:58:07 +0300117 u32 mahr = ravb_read(ndev, MAHR);
118 u32 malr = ravb_read(ndev, MALR);
119
120 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
121 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
122 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
123 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
124 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
125 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300126 }
127}
128
129static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
130{
131 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
132 mdiobb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300133
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300134 ravb_modify(priv->ndev, PIR, mask, set ? mask : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300135}
136
137/* MDC pin control */
138static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level)
139{
140 ravb_mdio_ctrl(ctrl, PIR_MDC, level);
141}
142
143/* Data I/O pin control */
144static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
145{
146 ravb_mdio_ctrl(ctrl, PIR_MMD, output);
147}
148
149/* Set data bit */
150static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
151{
152 ravb_mdio_ctrl(ctrl, PIR_MDO, value);
153}
154
155/* Get data bit */
156static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl)
157{
158 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
159 mdiobb);
160
161 return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0;
162}
163
164/* MDIO bus control struct */
165static struct mdiobb_ops bb_ops = {
166 .owner = THIS_MODULE,
167 .set_mdc = ravb_set_mdc,
168 .set_mdio_dir = ravb_set_mdio_dir,
169 .set_mdio_data = ravb_set_mdio_data,
170 .get_mdio_data = ravb_get_mdio_data,
171};
172
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100173/* Free TX skb function for AVB-IP */
174static int ravb_tx_free(struct net_device *ndev, int q, bool free_txed_only)
175{
176 struct ravb_private *priv = netdev_priv(ndev);
177 struct net_device_stats *stats = &priv->stats[q];
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200178 int num_tx_desc = priv->num_tx_desc;
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100179 struct ravb_tx_desc *desc;
180 int free_num = 0;
181 int entry;
182 u32 size;
183
184 for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
185 bool txed;
186
187 entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200188 num_tx_desc);
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100189 desc = &priv->tx_ring[q][entry];
190 txed = desc->die_dt == DT_FEMPTY;
191 if (free_txed_only && !txed)
192 break;
193 /* Descriptor type must be checked before all other reads */
194 dma_rmb();
195 size = le16_to_cpu(desc->ds_tagl) & TX_DS;
196 /* Free the original skb. */
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200197 if (priv->tx_skb[q][entry / num_tx_desc]) {
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100198 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
199 size, DMA_TO_DEVICE);
200 /* Last packet descriptor? */
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200201 if (entry % num_tx_desc == num_tx_desc - 1) {
202 entry /= num_tx_desc;
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100203 dev_kfree_skb_any(priv->tx_skb[q][entry]);
204 priv->tx_skb[q][entry] = NULL;
205 if (txed)
206 stats->tx_packets++;
207 }
208 free_num++;
209 }
210 if (txed)
211 stats->tx_bytes += size;
212 desc->die_dt = DT_EEMPTY;
213 }
214 return free_num;
215}
216
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300217/* Free skb's and DMA buffers for Ethernet AVB */
218static void ravb_ring_free(struct net_device *ndev, int q)
219{
220 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200221 int num_tx_desc = priv->num_tx_desc;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300222 int ring_size;
223 int i;
224
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300225 if (priv->rx_ring[q]) {
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100226 for (i = 0; i < priv->num_rx_ring[q]; i++) {
227 struct ravb_ex_rx_desc *desc = &priv->rx_ring[q][i];
228
229 if (!dma_mapping_error(ndev->dev.parent,
230 le32_to_cpu(desc->dptr)))
231 dma_unmap_single(ndev->dev.parent,
232 le32_to_cpu(desc->dptr),
Niklas Söderlund75efa062018-02-16 17:10:08 +0100233 priv->rx_buf_sz,
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100234 DMA_FROM_DEVICE);
235 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300236 ring_size = sizeof(struct ravb_ex_rx_desc) *
237 (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900238 dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300239 priv->rx_desc_dma[q]);
240 priv->rx_ring[q] = NULL;
241 }
242
243 if (priv->tx_ring[q]) {
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100244 ravb_tx_free(ndev, q, false);
245
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300246 ring_size = sizeof(struct ravb_tx_desc) *
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200247 (priv->num_tx_ring[q] * num_tx_desc + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900248 dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300249 priv->tx_desc_dma[q]);
250 priv->tx_ring[q] = NULL;
251 }
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100252
Eugeniu Rosca79514ef2017-06-06 00:08:10 +0200253 /* Free RX skb ringbuffer */
254 if (priv->rx_skb[q]) {
255 for (i = 0; i < priv->num_rx_ring[q]; i++)
256 dev_kfree_skb(priv->rx_skb[q][i]);
257 }
258 kfree(priv->rx_skb[q]);
259 priv->rx_skb[q] = NULL;
260
261 /* Free aligned TX buffers */
262 kfree(priv->tx_align[q]);
263 priv->tx_align[q] = NULL;
264
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100265 /* Free TX skb ringbuffer.
266 * SKBs are freed by ravb_tx_free() call above.
267 */
268 kfree(priv->tx_skb[q]);
269 priv->tx_skb[q] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300270}
271
272/* Format skb and descriptor buffer for Ethernet AVB */
273static void ravb_ring_format(struct net_device *ndev, int q)
274{
275 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200276 int num_tx_desc = priv->num_tx_desc;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300277 struct ravb_ex_rx_desc *rx_desc;
278 struct ravb_tx_desc *tx_desc;
279 struct ravb_desc *desc;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300280 int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q];
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300281 int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] *
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200282 num_tx_desc;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300283 dma_addr_t dma_addr;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300284 int i;
285
286 priv->cur_rx[q] = 0;
287 priv->cur_tx[q] = 0;
288 priv->dirty_rx[q] = 0;
289 priv->dirty_tx[q] = 0;
290
291 memset(priv->rx_ring[q], 0, rx_ring_size);
292 /* Build RX ring buffer */
293 for (i = 0; i < priv->num_rx_ring[q]; i++) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300294 /* RX descriptor */
295 rx_desc = &priv->rx_ring[q][i];
Niklas Söderlund75efa062018-02-16 17:10:08 +0100296 rx_desc->ds_cc = cpu_to_le16(priv->rx_buf_sz);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900297 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
Niklas Söderlund75efa062018-02-16 17:10:08 +0100298 priv->rx_buf_sz,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300299 DMA_FROM_DEVICE);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300300 /* We just set the data size to 0 for a failed mapping which
301 * should prevent DMA from happening...
302 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900303 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300304 rx_desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300305 rx_desc->dptr = cpu_to_le32(dma_addr);
306 rx_desc->die_dt = DT_FEMPTY;
307 }
308 rx_desc = &priv->rx_ring[q][i];
309 rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
310 rx_desc->die_dt = DT_LINKFIX; /* type */
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300311
312 memset(priv->tx_ring[q], 0, tx_ring_size);
313 /* Build TX ring buffer */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300314 for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
315 i++, tx_desc++) {
316 tx_desc->die_dt = DT_EEMPTY;
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200317 if (num_tx_desc > 1) {
318 tx_desc++;
319 tx_desc->die_dt = DT_EEMPTY;
320 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300321 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300322 tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
323 tx_desc->die_dt = DT_LINKFIX; /* type */
324
325 /* RX descriptor base address for best effort */
326 desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
327 desc->die_dt = DT_LINKFIX; /* type */
328 desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
329
330 /* TX descriptor base address for best effort */
331 desc = &priv->desc_bat[q];
332 desc->die_dt = DT_LINKFIX; /* type */
333 desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
334}
335
336/* Init skb and descriptor buffer for Ethernet AVB */
337static int ravb_ring_init(struct net_device *ndev, int q)
338{
339 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200340 int num_tx_desc = priv->num_tx_desc;
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300341 struct sk_buff *skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300342 int ring_size;
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300343 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300344
Niklas Söderlund75efa062018-02-16 17:10:08 +0100345 priv->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ : ndev->mtu) +
Simon Horman12da6432019-01-23 12:14:52 +0100346 ETH_HLEN + VLAN_HLEN + sizeof(__sum16);
Niklas Söderlund75efa062018-02-16 17:10:08 +0100347
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300348 /* Allocate RX and TX skb rings */
349 priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
350 sizeof(*priv->rx_skb[q]), GFP_KERNEL);
351 priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
352 sizeof(*priv->tx_skb[q]), GFP_KERNEL);
353 if (!priv->rx_skb[q] || !priv->tx_skb[q])
354 goto error;
355
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300356 for (i = 0; i < priv->num_rx_ring[q]; i++) {
Niklas Söderlund75efa062018-02-16 17:10:08 +0100357 skb = netdev_alloc_skb(ndev, priv->rx_buf_sz + RAVB_ALIGN - 1);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300358 if (!skb)
359 goto error;
360 ravb_set_buffer_align(skb);
361 priv->rx_skb[q][i] = skb;
362 }
363
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200364 if (num_tx_desc > 1) {
365 /* Allocate rings for the aligned buffers */
366 priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
367 DPTR_ALIGN - 1, GFP_KERNEL);
368 if (!priv->tx_align[q])
369 goto error;
370 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300371
372 /* Allocate all RX descriptors. */
373 ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900374 priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300375 &priv->rx_desc_dma[q],
376 GFP_KERNEL);
377 if (!priv->rx_ring[q])
378 goto error;
379
380 priv->dirty_rx[q] = 0;
381
382 /* Allocate all TX descriptors. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300383 ring_size = sizeof(struct ravb_tx_desc) *
Kazuya Mizuguchif5433052018-09-19 10:06:21 +0200384 (priv->num_tx_ring[q] * num_tx_desc + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900385 priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300386 &priv->tx_desc_dma[q],
387 GFP_KERNEL);
388 if (!priv->tx_ring[q])
389 goto error;
390
391 return 0;
392
393error:
394 ravb_ring_free(ndev, q);
395
396 return -ENOMEM;
397}
398
399/* E-MAC init function */
400static void ravb_emac_init(struct net_device *ndev)
401{
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300402 /* Receive frame limit set register */
403 ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
404
Simon Horman4d86d382017-10-04 09:54:27 +0200405 /* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */
Magnus Damm08b43852018-11-21 20:21:26 +0900406 ravb_write(ndev, ECMR_ZPF | ECMR_DM |
Simon Horman4d86d382017-10-04 09:54:27 +0200407 (ndev->features & NETIF_F_RXCSUM ? ECMR_RCSC : 0) |
Sergei Shtylyov1c1fa822016-01-11 00:27:38 +0300408 ECMR_TE | ECMR_RE, ECMR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300409
410 ravb_set_rate(ndev);
411
412 /* Set MAC address */
413 ravb_write(ndev,
414 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
415 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
416 ravb_write(ndev,
417 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
418
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300419 /* E-MAC status register clear */
420 ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
421
422 /* E-MAC interrupt enable register */
423 ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
424}
425
426/* Device init function for Ethernet AVB */
427static int ravb_dmac_init(struct net_device *ndev)
428{
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900429 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300430 int error;
431
432 /* Set CONFIG mode */
433 error = ravb_config(ndev);
434 if (error)
435 return error;
436
437 error = ravb_ring_init(ndev, RAVB_BE);
438 if (error)
439 return error;
440 error = ravb_ring_init(ndev, RAVB_NC);
441 if (error) {
442 ravb_ring_free(ndev, RAVB_BE);
443 return error;
444 }
445
446 /* Descriptor format */
447 ravb_ring_format(ndev, RAVB_BE);
448 ravb_ring_format(ndev, RAVB_NC);
449
450#if defined(__LITTLE_ENDIAN)
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300451 ravb_modify(ndev, CCC, CCC_BOC, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300452#else
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300453 ravb_modify(ndev, CCC, CCC_BOC, CCC_BOC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300454#endif
455
456 /* Set AVB RX */
Masaru Nagai8d9c4182016-06-01 03:01:28 +0900457 ravb_write(ndev,
458 RCR_EFFS | RCR_ENCF | RCR_ETS0 | RCR_ESF | 0x18000000, RCR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300459
460 /* Set FIFO size */
Masaru Nagaiae9819e2019-03-07 11:24:47 +0100461 ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00112200, TGC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300462
463 /* Timestamp enable */
464 ravb_write(ndev, TCCR_TFEN, TCCR);
465
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900466 /* Interrupt init: */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900467 if (priv->chip_id == RCAR_GEN3) {
468 /* Clear DIL.DPLx */
469 ravb_write(ndev, 0, DIL);
470 /* Set queue specific interrupt */
471 ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE);
472 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300473 /* Frame receive */
474 ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900475 /* Disable FIFO full warning */
476 ravb_write(ndev, 0, RIC1);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300477 /* Receive FIFO full error, descriptor empty */
478 ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
479 /* Frame transmitted, timestamp FIFO updated */
480 ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
481
482 /* Setting the control will start the AVB-DMAC process. */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300483 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300484
485 return 0;
486}
487
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300488static void ravb_get_tx_tstamp(struct net_device *ndev)
489{
490 struct ravb_private *priv = netdev_priv(ndev);
491 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
492 struct skb_shared_hwtstamps shhwtstamps;
493 struct sk_buff *skb;
494 struct timespec64 ts;
495 u16 tag, tfa_tag;
496 int count;
497 u32 tfa2;
498
499 count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
500 while (count--) {
501 tfa2 = ravb_read(ndev, TFA2);
502 tfa_tag = (tfa2 & TFA2_TST) >> 16;
503 ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
504 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
505 ravb_read(ndev, TFA1);
506 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
507 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
508 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
509 list) {
510 skb = ts_skb->skb;
511 tag = ts_skb->tag;
512 list_del(&ts_skb->list);
513 kfree(ts_skb);
514 if (tag == tfa_tag) {
515 skb_tstamp_tx(skb, &shhwtstamps);
516 break;
517 }
518 }
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300519 ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300520 }
521}
522
Simon Horman4d86d382017-10-04 09:54:27 +0200523static void ravb_rx_csum(struct sk_buff *skb)
524{
525 u8 *hw_csum;
526
Simon Horman12da6432019-01-23 12:14:52 +0100527 /* The hardware checksum is contained in sizeof(__sum16) (2) bytes
528 * appended to packet data
529 */
530 if (unlikely(skb->len < sizeof(__sum16)))
Simon Horman4d86d382017-10-04 09:54:27 +0200531 return;
Simon Horman12da6432019-01-23 12:14:52 +0100532 hw_csum = skb_tail_pointer(skb) - sizeof(__sum16);
Simon Horman4d86d382017-10-04 09:54:27 +0200533 skb->csum = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum));
534 skb->ip_summed = CHECKSUM_COMPLETE;
Simon Horman12da6432019-01-23 12:14:52 +0100535 skb_trim(skb, skb->len - sizeof(__sum16));
Simon Horman4d86d382017-10-04 09:54:27 +0200536}
537
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300538/* Packet receive function for Ethernet AVB */
539static bool ravb_rx(struct net_device *ndev, int *quota, int q)
540{
541 struct ravb_private *priv = netdev_priv(ndev);
542 int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
543 int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
544 priv->cur_rx[q];
545 struct net_device_stats *stats = &priv->stats[q];
546 struct ravb_ex_rx_desc *desc;
547 struct sk_buff *skb;
548 dma_addr_t dma_addr;
549 struct timespec64 ts;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300550 u8 desc_status;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300551 u16 pkt_len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300552 int limit;
553
554 boguscnt = min(boguscnt, *quota);
555 limit = boguscnt;
556 desc = &priv->rx_ring[q][entry];
557 while (desc->die_dt != DT_FEMPTY) {
558 /* Descriptor type must be checked before all other reads */
559 dma_rmb();
560 desc_status = desc->msc;
561 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
562
563 if (--boguscnt < 0)
564 break;
565
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300566 /* We use 0-byte descriptors to mark the DMA mapping errors */
567 if (!pkt_len)
568 continue;
569
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300570 if (desc_status & MSC_MC)
571 stats->multicast++;
572
573 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
574 MSC_CEEF)) {
575 stats->rx_errors++;
576 if (desc_status & MSC_CRC)
577 stats->rx_crc_errors++;
578 if (desc_status & MSC_RFE)
579 stats->rx_frame_errors++;
580 if (desc_status & (MSC_RTLF | MSC_RTSF))
581 stats->rx_length_errors++;
582 if (desc_status & MSC_CEEF)
583 stats->rx_missed_errors++;
584 } else {
585 u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
586
587 skb = priv->rx_skb[q][entry];
588 priv->rx_skb[q][entry] = NULL;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900589 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Niklas Söderlund75efa062018-02-16 17:10:08 +0100590 priv->rx_buf_sz,
Sergei Shtylyove2370f02015-07-15 00:56:52 +0300591 DMA_FROM_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300592 get_ts &= (q == RAVB_NC) ?
593 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
594 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
595 if (get_ts) {
596 struct skb_shared_hwtstamps *shhwtstamps;
597
598 shhwtstamps = skb_hwtstamps(skb);
599 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
600 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
601 32) | le32_to_cpu(desc->ts_sl);
602 ts.tv_nsec = le32_to_cpu(desc->ts_n);
603 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
604 }
Simon Horman4d86d382017-10-04 09:54:27 +0200605
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300606 skb_put(skb, pkt_len);
607 skb->protocol = eth_type_trans(skb, ndev);
Simon Horman4d86d382017-10-04 09:54:27 +0200608 if (ndev->features & NETIF_F_RXCSUM)
609 ravb_rx_csum(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300610 napi_gro_receive(&priv->napi[q], skb);
611 stats->rx_packets++;
612 stats->rx_bytes += pkt_len;
613 }
614
615 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
616 desc = &priv->rx_ring[q][entry];
617 }
618
619 /* Refill the RX ring buffers. */
620 for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
621 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
622 desc = &priv->rx_ring[q][entry];
Niklas Söderlund75efa062018-02-16 17:10:08 +0100623 desc->ds_cc = cpu_to_le16(priv->rx_buf_sz);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300624
625 if (!priv->rx_skb[q][entry]) {
626 skb = netdev_alloc_skb(ndev,
Niklas Söderlund75efa062018-02-16 17:10:08 +0100627 priv->rx_buf_sz +
628 RAVB_ALIGN - 1);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300629 if (!skb)
630 break; /* Better luck next round. */
631 ravb_set_buffer_align(skb);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900632 dma_addr = dma_map_single(ndev->dev.parent, skb->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300633 le16_to_cpu(desc->ds_cc),
634 DMA_FROM_DEVICE);
635 skb_checksum_none_assert(skb);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300636 /* We just set the data size to 0 for a failed mapping
637 * which should prevent DMA from happening...
638 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900639 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300640 desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300641 desc->dptr = cpu_to_le32(dma_addr);
642 priv->rx_skb[q][entry] = skb;
643 }
644 /* Descriptor type must be set after all the above writes */
645 dma_wmb();
646 desc->die_dt = DT_FEMPTY;
647 }
648
649 *quota -= limit - (++boguscnt);
650
651 return boguscnt <= 0;
652}
653
654static void ravb_rcv_snd_disable(struct net_device *ndev)
655{
656 /* Disable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300657 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300658}
659
660static void ravb_rcv_snd_enable(struct net_device *ndev)
661{
662 /* Enable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300663 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300664}
665
666/* function for waiting dma process finished */
667static int ravb_stop_dma(struct net_device *ndev)
668{
669 int error;
670
671 /* Wait for stopping the hardware TX process */
672 error = ravb_wait(ndev, TCCR,
673 TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
674 if (error)
675 return error;
676
677 error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
678 0);
679 if (error)
680 return error;
681
682 /* Stop the E-MAC's RX/TX processes. */
683 ravb_rcv_snd_disable(ndev);
684
685 /* Wait for stopping the RX DMA process */
686 error = ravb_wait(ndev, CSR, CSR_RPO, 0);
687 if (error)
688 return error;
689
690 /* Stop AVB-DMAC process */
691 return ravb_config(ndev);
692}
693
694/* E-MAC interrupt handler */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900695static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300696{
697 struct ravb_private *priv = netdev_priv(ndev);
698 u32 ecsr, psr;
699
700 ecsr = ravb_read(ndev, ECSR);
701 ravb_write(ndev, ecsr, ECSR); /* clear interrupt */
Niklas Söderlund3e3d6472017-08-01 12:14:36 +0200702
703 if (ecsr & ECSR_MPD)
704 pm_wakeup_event(&priv->pdev->dev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300705 if (ecsr & ECSR_ICD)
706 ndev->stats.tx_carrier_errors++;
707 if (ecsr & ECSR_LCHNG) {
708 /* Link changed */
709 if (priv->no_avb_link)
710 return;
711 psr = ravb_read(ndev, PSR);
712 if (priv->avb_link_active_low)
713 psr ^= PSR_LMON;
714 if (!(psr & PSR_LMON)) {
715 /* DIsable RX and TX */
716 ravb_rcv_snd_disable(ndev);
717 } else {
718 /* Enable RX and TX */
719 ravb_rcv_snd_enable(ndev);
720 }
721 }
722}
723
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900724static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
725{
726 struct net_device *ndev = dev_id;
727 struct ravb_private *priv = netdev_priv(ndev);
728
729 spin_lock(&priv->lock);
730 ravb_emac_interrupt_unlocked(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900731 spin_unlock(&priv->lock);
732 return IRQ_HANDLED;
733}
734
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300735/* Error interrupt handler */
736static void ravb_error_interrupt(struct net_device *ndev)
737{
738 struct ravb_private *priv = netdev_priv(ndev);
739 u32 eis, ris2;
740
741 eis = ravb_read(ndev, EIS);
Kazuya Mizuguchi2fe397a2018-09-18 12:22:26 +0200742 ravb_write(ndev, ~(EIS_QFS | EIS_RESERVED), EIS);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300743 if (eis & EIS_QFS) {
744 ris2 = ravb_read(ndev, RIS2);
Kazuya Mizuguchi2fe397a2018-09-18 12:22:26 +0200745 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF | RIS2_RESERVED),
746 RIS2);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300747
748 /* Receive Descriptor Empty int */
749 if (ris2 & RIS2_QFF0)
750 priv->stats[RAVB_BE].rx_over_errors++;
751
752 /* Receive Descriptor Empty int */
753 if (ris2 & RIS2_QFF1)
754 priv->stats[RAVB_NC].rx_over_errors++;
755
756 /* Receive FIFO Overflow int */
757 if (ris2 & RIS2_RFFF)
758 priv->rx_fifo_errors++;
759 }
760}
761
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900762static bool ravb_queue_interrupt(struct net_device *ndev, int q)
763{
764 struct ravb_private *priv = netdev_priv(ndev);
765 u32 ris0 = ravb_read(ndev, RIS0);
766 u32 ric0 = ravb_read(ndev, RIC0);
767 u32 tis = ravb_read(ndev, TIS);
768 u32 tic = ravb_read(ndev, TIC);
769
770 if (((ris0 & ric0) & BIT(q)) || ((tis & tic) & BIT(q))) {
771 if (napi_schedule_prep(&priv->napi[q])) {
772 /* Mask RX and TX interrupts */
773 if (priv->chip_id == RCAR_GEN2) {
774 ravb_write(ndev, ric0 & ~BIT(q), RIC0);
775 ravb_write(ndev, tic & ~BIT(q), TIC);
776 } else {
777 ravb_write(ndev, BIT(q), RID0);
778 ravb_write(ndev, BIT(q), TID);
779 }
780 __napi_schedule(&priv->napi[q]);
781 } else {
782 netdev_warn(ndev,
783 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
784 ris0, ric0);
785 netdev_warn(ndev,
786 " tx status 0x%08x, tx mask 0x%08x.\n",
787 tis, tic);
788 }
789 return true;
790 }
791 return false;
792}
793
794static bool ravb_timestamp_interrupt(struct net_device *ndev)
795{
796 u32 tis = ravb_read(ndev, TIS);
797
798 if (tis & TIS_TFUF) {
Kazuya Mizuguchi2fe397a2018-09-18 12:22:26 +0200799 ravb_write(ndev, ~(TIS_TFUF | TIS_RESERVED), TIS);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900800 ravb_get_tx_tstamp(ndev);
801 return true;
802 }
803 return false;
804}
805
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300806static irqreturn_t ravb_interrupt(int irq, void *dev_id)
807{
808 struct net_device *ndev = dev_id;
809 struct ravb_private *priv = netdev_priv(ndev);
810 irqreturn_t result = IRQ_NONE;
811 u32 iss;
812
813 spin_lock(&priv->lock);
814 /* Get interrupt status */
815 iss = ravb_read(ndev, ISS);
816
817 /* Received and transmitted interrupts */
818 if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300819 int q;
820
821 /* Timestamp updated */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900822 if (ravb_timestamp_interrupt(ndev))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300823 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300824
825 /* Network control and best effort queue RX/TX */
826 for (q = RAVB_NC; q >= RAVB_BE; q--) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900827 if (ravb_queue_interrupt(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300828 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300829 }
830 }
831
832 /* E-MAC status summary */
833 if (iss & ISS_MS) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900834 ravb_emac_interrupt_unlocked(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300835 result = IRQ_HANDLED;
836 }
837
838 /* Error status summary */
839 if (iss & ISS_ES) {
840 ravb_error_interrupt(ndev);
841 result = IRQ_HANDLED;
842 }
843
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900844 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300845 if (iss & ISS_CGIS) {
846 ravb_ptp_interrupt(ndev);
Yoshihiro Kaneko38c848c2016-03-16 00:52:16 +0900847 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300848 }
Sergei Shtylyova0d2f202015-06-11 01:02:30 +0300849
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300850 spin_unlock(&priv->lock);
851 return result;
852}
853
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900854/* Timestamp/Error/gPTP interrupt handler */
855static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
856{
857 struct net_device *ndev = dev_id;
858 struct ravb_private *priv = netdev_priv(ndev);
859 irqreturn_t result = IRQ_NONE;
860 u32 iss;
861
862 spin_lock(&priv->lock);
863 /* Get interrupt status */
864 iss = ravb_read(ndev, ISS);
865
866 /* Timestamp updated */
867 if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
868 result = IRQ_HANDLED;
869
870 /* Error status summary */
871 if (iss & ISS_ES) {
872 ravb_error_interrupt(ndev);
873 result = IRQ_HANDLED;
874 }
875
876 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300877 if (iss & ISS_CGIS) {
878 ravb_ptp_interrupt(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900879 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300880 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900881
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900882 spin_unlock(&priv->lock);
883 return result;
884}
885
886static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
887{
888 struct net_device *ndev = dev_id;
889 struct ravb_private *priv = netdev_priv(ndev);
890 irqreturn_t result = IRQ_NONE;
891
892 spin_lock(&priv->lock);
893
894 /* Network control/Best effort queue RX/TX */
895 if (ravb_queue_interrupt(ndev, q))
896 result = IRQ_HANDLED;
897
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900898 spin_unlock(&priv->lock);
899 return result;
900}
901
902static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
903{
904 return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
905}
906
907static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
908{
909 return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
910}
911
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300912static int ravb_poll(struct napi_struct *napi, int budget)
913{
914 struct net_device *ndev = napi->dev;
915 struct ravb_private *priv = netdev_priv(ndev);
916 unsigned long flags;
917 int q = napi - priv->napi;
918 int mask = BIT(q);
919 int quota = budget;
920 u32 ris0, tis;
921
922 for (;;) {
923 tis = ravb_read(ndev, TIS);
924 ris0 = ravb_read(ndev, RIS0);
925 if (!((ris0 & mask) || (tis & mask)))
926 break;
927
928 /* Processing RX Descriptor Ring */
929 if (ris0 & mask) {
930 /* Clear RX interrupt */
Kazuya Mizuguchi2fe397a2018-09-18 12:22:26 +0200931 ravb_write(ndev, ~(mask | RIS0_RESERVED), RIS0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300932 if (ravb_rx(ndev, &quota, q))
933 goto out;
934 }
935 /* Processing TX Descriptor Ring */
936 if (tis & mask) {
937 spin_lock_irqsave(&priv->lock, flags);
938 /* Clear TX interrupt */
Kazuya Mizuguchi2fe397a2018-09-18 12:22:26 +0200939 ravb_write(ndev, ~(mask | TIS_RESERVED), TIS);
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100940 ravb_tx_free(ndev, q, true);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300941 netif_wake_subqueue(ndev, q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300942 spin_unlock_irqrestore(&priv->lock, flags);
943 }
944 }
945
946 napi_complete(napi);
947
948 /* Re-enable RX/TX interrupts */
949 spin_lock_irqsave(&priv->lock, flags);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900950 if (priv->chip_id == RCAR_GEN2) {
951 ravb_modify(ndev, RIC0, mask, mask);
952 ravb_modify(ndev, TIC, mask, mask);
953 } else {
954 ravb_write(ndev, mask, RIE0);
955 ravb_write(ndev, mask, TIE);
956 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300957 spin_unlock_irqrestore(&priv->lock, flags);
958
959 /* Receive error message handling */
960 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
961 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
Kazuya Mizuguchi18a3ed52017-01-12 13:21:06 +0100962 if (priv->rx_over_errors != ndev->stats.rx_over_errors)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300963 ndev->stats.rx_over_errors = priv->rx_over_errors;
Kazuya Mizuguchi18a3ed52017-01-12 13:21:06 +0100964 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300965 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300966out:
967 return budget - quota;
968}
969
970/* PHY state control function */
971static void ravb_adjust_link(struct net_device *ndev)
972{
973 struct ravb_private *priv = netdev_priv(ndev);
Philippe Reynes0f635172016-08-20 00:52:18 +0200974 struct phy_device *phydev = ndev->phydev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300975 bool new_state = false;
Vladimir Zapolskiy05925e52018-07-04 11:14:51 +0300976 unsigned long flags;
977
978 spin_lock_irqsave(&priv->lock, flags);
979
980 /* Disable TX and RX right over here, if E-MAC change is ignored */
981 if (priv->no_avb_link)
982 ravb_rcv_snd_disable(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300983
984 if (phydev->link) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300985 if (phydev->speed != priv->speed) {
986 new_state = true;
987 priv->speed = phydev->speed;
988 ravb_set_rate(ndev);
989 }
990 if (!priv->link) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300991 ravb_modify(ndev, ECMR, ECMR_TXF, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300992 new_state = true;
993 priv->link = phydev->link;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300994 }
995 } else if (priv->link) {
996 new_state = true;
997 priv->link = 0;
998 priv->speed = 0;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300999 }
1000
Vladimir Zapolskiy05925e52018-07-04 11:14:51 +03001001 /* Enable TX and RX right over here, if E-MAC change is ignored */
1002 if (priv->no_avb_link && phydev->link)
1003 ravb_rcv_snd_enable(ndev);
1004
Vladimir Zapolskiy05925e52018-07-04 11:14:51 +03001005 spin_unlock_irqrestore(&priv->lock, flags);
1006
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001007 if (new_state && netif_msg_link(priv))
1008 phy_print_status(phydev);
1009}
1010
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +01001011static const struct soc_device_attribute r8a7795es10[] = {
1012 { .soc_id = "r8a7795", .revision = "ES1.0", },
1013 { /* sentinel */ }
1014};
1015
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001016/* PHY init function */
1017static int ravb_phy_init(struct net_device *ndev)
1018{
1019 struct device_node *np = ndev->dev.parent->of_node;
1020 struct ravb_private *priv = netdev_priv(ndev);
1021 struct phy_device *phydev;
1022 struct device_node *pn;
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +09001023 int err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001024
1025 priv->link = 0;
1026 priv->speed = 0;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001027
1028 /* Try connecting to PHY */
1029 pn = of_parse_phandle(np, "phy-handle", 0);
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +09001030 if (!pn) {
1031 /* In the case of a fixed PHY, the DT node associated
1032 * to the PHY is the Ethernet MAC DT node.
1033 */
1034 if (of_phy_is_fixed_link(np)) {
1035 err = of_phy_register_fixed_link(np);
1036 if (err)
1037 return err;
1038 }
1039 pn = of_node_get(np);
1040 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001041 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
1042 priv->phy_interface);
Peter Chenc9b1eb82016-08-01 15:02:39 +08001043 of_node_put(pn);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001044 if (!phydev) {
1045 netdev_err(ndev, "failed to connect PHY\n");
Johan Hovold9f70eb32016-11-28 19:25:06 +01001046 err = -ENOENT;
1047 goto err_deregister_fixed_link;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001048 }
1049
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +01001050 /* This driver only support 10/100Mbit speeds on R-Car H3 ES1.0
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001051 * at this time.
1052 */
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +01001053 if (soc_device_match(r8a7795es10)) {
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001054 err = phy_set_max_speed(phydev, SPEED_100);
1055 if (err) {
1056 netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
Johan Hovold9f70eb32016-11-28 19:25:06 +01001057 goto err_phy_disconnect;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001058 }
1059
1060 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
1061 }
1062
Andrew Lunn65c58772018-09-21 15:52:26 +02001063 /* 10BASE, Pause and Asym Pause is not supported */
Andrew Lunn41124fa2018-09-12 01:53:14 +02001064 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Half_BIT);
1065 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Full_BIT);
Andrew Lunn65c58772018-09-21 15:52:26 +02001066 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_Pause_BIT);
1067 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_Asym_Pause_BIT);
Kazuya Mizuguchi54499962015-12-14 00:15:58 +09001068
Magnus Dammebc227f2018-11-21 20:21:17 +09001069 /* Half Duplex is not supported */
1070 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
1071 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Half_BIT);
1072
Andrew Lunn22209432016-01-06 20:11:13 +01001073 phy_attached_info(phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001074
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001075 return 0;
Johan Hovold9f70eb32016-11-28 19:25:06 +01001076
1077err_phy_disconnect:
1078 phy_disconnect(phydev);
1079err_deregister_fixed_link:
1080 if (of_phy_is_fixed_link(np))
1081 of_phy_deregister_fixed_link(np);
1082
1083 return err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001084}
1085
1086/* PHY control start function */
1087static int ravb_phy_start(struct net_device *ndev)
1088{
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001089 int error;
1090
1091 error = ravb_phy_init(ndev);
1092 if (error)
1093 return error;
1094
Philippe Reynes0f635172016-08-20 00:52:18 +02001095 phy_start(ndev->phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001096
1097 return 0;
1098}
1099
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001100static u32 ravb_get_msglevel(struct net_device *ndev)
1101{
1102 struct ravb_private *priv = netdev_priv(ndev);
1103
1104 return priv->msg_enable;
1105}
1106
1107static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1108{
1109 struct ravb_private *priv = netdev_priv(ndev);
1110
1111 priv->msg_enable = value;
1112}
1113
1114static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1115 "rx_queue_0_current",
1116 "tx_queue_0_current",
1117 "rx_queue_0_dirty",
1118 "tx_queue_0_dirty",
1119 "rx_queue_0_packets",
1120 "tx_queue_0_packets",
1121 "rx_queue_0_bytes",
1122 "tx_queue_0_bytes",
1123 "rx_queue_0_mcast_packets",
1124 "rx_queue_0_errors",
1125 "rx_queue_0_crc_errors",
1126 "rx_queue_0_frame_errors",
1127 "rx_queue_0_length_errors",
1128 "rx_queue_0_missed_errors",
1129 "rx_queue_0_over_errors",
1130
1131 "rx_queue_1_current",
1132 "tx_queue_1_current",
1133 "rx_queue_1_dirty",
1134 "tx_queue_1_dirty",
1135 "rx_queue_1_packets",
1136 "tx_queue_1_packets",
1137 "rx_queue_1_bytes",
1138 "tx_queue_1_bytes",
1139 "rx_queue_1_mcast_packets",
1140 "rx_queue_1_errors",
1141 "rx_queue_1_crc_errors",
Sergei Shtylyovb17c1d92015-12-04 01:51:10 +03001142 "rx_queue_1_frame_errors",
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001143 "rx_queue_1_length_errors",
1144 "rx_queue_1_missed_errors",
1145 "rx_queue_1_over_errors",
1146};
1147
1148#define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1149
1150static int ravb_get_sset_count(struct net_device *netdev, int sset)
1151{
1152 switch (sset) {
1153 case ETH_SS_STATS:
1154 return RAVB_STATS_LEN;
1155 default:
1156 return -EOPNOTSUPP;
1157 }
1158}
1159
1160static void ravb_get_ethtool_stats(struct net_device *ndev,
Niklas Söderlundc94f2fc2018-07-16 14:19:25 +02001161 struct ethtool_stats *estats, u64 *data)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001162{
1163 struct ravb_private *priv = netdev_priv(ndev);
1164 int i = 0;
1165 int q;
1166
1167 /* Device-specific stats */
1168 for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1169 struct net_device_stats *stats = &priv->stats[q];
1170
1171 data[i++] = priv->cur_rx[q];
1172 data[i++] = priv->cur_tx[q];
1173 data[i++] = priv->dirty_rx[q];
1174 data[i++] = priv->dirty_tx[q];
1175 data[i++] = stats->rx_packets;
1176 data[i++] = stats->tx_packets;
1177 data[i++] = stats->rx_bytes;
1178 data[i++] = stats->tx_bytes;
1179 data[i++] = stats->multicast;
1180 data[i++] = stats->rx_errors;
1181 data[i++] = stats->rx_crc_errors;
1182 data[i++] = stats->rx_frame_errors;
1183 data[i++] = stats->rx_length_errors;
1184 data[i++] = stats->rx_missed_errors;
1185 data[i++] = stats->rx_over_errors;
1186 }
1187}
1188
1189static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1190{
1191 switch (stringset) {
1192 case ETH_SS_STATS:
Niklas Söderlund49f33032018-07-16 14:19:26 +02001193 memcpy(data, ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001194 break;
1195 }
1196}
1197
1198static void ravb_get_ringparam(struct net_device *ndev,
1199 struct ethtool_ringparam *ring)
1200{
1201 struct ravb_private *priv = netdev_priv(ndev);
1202
1203 ring->rx_max_pending = BE_RX_RING_MAX;
1204 ring->tx_max_pending = BE_TX_RING_MAX;
1205 ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1206 ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1207}
1208
1209static int ravb_set_ringparam(struct net_device *ndev,
1210 struct ethtool_ringparam *ring)
1211{
1212 struct ravb_private *priv = netdev_priv(ndev);
1213 int error;
1214
1215 if (ring->tx_pending > BE_TX_RING_MAX ||
1216 ring->rx_pending > BE_RX_RING_MAX ||
1217 ring->tx_pending < BE_TX_RING_MIN ||
1218 ring->rx_pending < BE_RX_RING_MIN)
1219 return -EINVAL;
1220 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1221 return -EINVAL;
1222
1223 if (netif_running(ndev)) {
1224 netif_device_detach(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001225 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001226 if (priv->chip_id == RCAR_GEN2)
1227 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001228 /* Wait for DMA stopping */
1229 error = ravb_stop_dma(ndev);
1230 if (error) {
1231 netdev_err(ndev,
1232 "cannot set ringparam! Any AVB processes are still running?\n");
1233 return error;
1234 }
1235 synchronize_irq(ndev->irq);
1236
1237 /* Free all the skb's in the RX queue and the DMA buffers. */
1238 ravb_ring_free(ndev, RAVB_BE);
1239 ravb_ring_free(ndev, RAVB_NC);
1240 }
1241
1242 /* Set new parameters */
1243 priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1244 priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1245
1246 if (netif_running(ndev)) {
1247 error = ravb_dmac_init(ndev);
1248 if (error) {
1249 netdev_err(ndev,
1250 "%s: ravb_dmac_init() failed, error %d\n",
1251 __func__, error);
1252 return error;
1253 }
1254
1255 ravb_emac_init(ndev);
1256
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001257 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001258 if (priv->chip_id == RCAR_GEN2)
1259 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001260
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001261 netif_device_attach(ndev);
1262 }
1263
1264 return 0;
1265}
1266
1267static int ravb_get_ts_info(struct net_device *ndev,
1268 struct ethtool_ts_info *info)
1269{
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001270 struct ravb_private *priv = netdev_priv(ndev);
1271
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001272 info->so_timestamping =
1273 SOF_TIMESTAMPING_TX_SOFTWARE |
1274 SOF_TIMESTAMPING_RX_SOFTWARE |
1275 SOF_TIMESTAMPING_SOFTWARE |
1276 SOF_TIMESTAMPING_TX_HARDWARE |
1277 SOF_TIMESTAMPING_RX_HARDWARE |
1278 SOF_TIMESTAMPING_RAW_HARDWARE;
1279 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1280 info->rx_filters =
1281 (1 << HWTSTAMP_FILTER_NONE) |
1282 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1283 (1 << HWTSTAMP_FILTER_ALL);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001284 info->phc_index = ptp_clock_index(priv->ptp.clock);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001285
1286 return 0;
1287}
1288
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02001289static void ravb_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1290{
1291 struct ravb_private *priv = netdev_priv(ndev);
1292
Geert Uytterhoevenab104612017-10-12 10:24:53 +02001293 wol->supported = WAKE_MAGIC;
1294 wol->wolopts = priv->wol_enabled ? WAKE_MAGIC : 0;
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02001295}
1296
1297static int ravb_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1298{
1299 struct ravb_private *priv = netdev_priv(ndev);
1300
Geert Uytterhoevenab104612017-10-12 10:24:53 +02001301 if (wol->wolopts & ~WAKE_MAGIC)
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02001302 return -EOPNOTSUPP;
1303
1304 priv->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
1305
1306 device_set_wakeup_enable(&priv->pdev->dev, priv->wol_enabled);
1307
1308 return 0;
1309}
1310
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001311static const struct ethtool_ops ravb_ethtool_ops = {
Vladimir Zapolskiyeeb07282018-07-04 11:16:09 +03001312 .nway_reset = phy_ethtool_nway_reset,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001313 .get_msglevel = ravb_get_msglevel,
1314 .set_msglevel = ravb_set_msglevel,
1315 .get_link = ethtool_op_get_link,
1316 .get_strings = ravb_get_strings,
1317 .get_ethtool_stats = ravb_get_ethtool_stats,
1318 .get_sset_count = ravb_get_sset_count,
1319 .get_ringparam = ravb_get_ringparam,
1320 .set_ringparam = ravb_set_ringparam,
1321 .get_ts_info = ravb_get_ts_info,
Vladimir Zapolskiy468e40b2018-07-04 11:16:11 +03001322 .get_link_ksettings = phy_ethtool_get_link_ksettings,
Vladimir Zapolskiy44f3d552018-07-04 11:16:12 +03001323 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02001324 .get_wol = ravb_get_wol,
1325 .set_wol = ravb_set_wol,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001326};
1327
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001328static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1329 struct net_device *ndev, struct device *dev,
1330 const char *ch)
1331{
1332 char *name;
1333 int error;
1334
1335 name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1336 if (!name)
1337 return -ENOMEM;
1338 error = request_irq(irq, handler, 0, name, ndev);
1339 if (error)
1340 netdev_err(ndev, "cannot request IRQ %s\n", name);
1341
1342 return error;
1343}
1344
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001345/* Network device open function for Ethernet AVB */
1346static int ravb_open(struct net_device *ndev)
1347{
1348 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001349 struct platform_device *pdev = priv->pdev;
1350 struct device *dev = &pdev->dev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001351 int error;
1352
1353 napi_enable(&priv->napi[RAVB_BE]);
1354 napi_enable(&priv->napi[RAVB_NC]);
1355
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001356 if (priv->chip_id == RCAR_GEN2) {
1357 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1358 ndev->name, ndev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001359 if (error) {
1360 netdev_err(ndev, "cannot request IRQ\n");
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001361 goto out_napi_off;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001362 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001363 } else {
1364 error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1365 dev, "ch22:multi");
1366 if (error)
1367 goto out_napi_off;
1368 error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1369 dev, "ch24:emac");
1370 if (error)
1371 goto out_free_irq;
1372 error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1373 ndev, dev, "ch0:rx_be");
1374 if (error)
1375 goto out_free_irq_emac;
1376 error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1377 ndev, dev, "ch18:tx_be");
1378 if (error)
1379 goto out_free_irq_be_rx;
1380 error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1381 ndev, dev, "ch1:rx_nc");
1382 if (error)
1383 goto out_free_irq_be_tx;
1384 error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1385 ndev, dev, "ch19:tx_nc");
1386 if (error)
1387 goto out_free_irq_nc_rx;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001388 }
1389
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001390 /* Device init */
1391 error = ravb_dmac_init(ndev);
1392 if (error)
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001393 goto out_free_irq_nc_tx;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001394 ravb_emac_init(ndev);
1395
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001396 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001397 if (priv->chip_id == RCAR_GEN2)
1398 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001399
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001400 netif_tx_start_all_queues(ndev);
1401
1402 /* PHY control start */
1403 error = ravb_phy_start(ndev);
1404 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001405 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001406
1407 return 0;
1408
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001409out_ptp_stop:
1410 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001411 if (priv->chip_id == RCAR_GEN2)
1412 ravb_ptp_stop(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001413out_free_irq_nc_tx:
1414 if (priv->chip_id == RCAR_GEN2)
1415 goto out_free_irq;
1416 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1417out_free_irq_nc_rx:
1418 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1419out_free_irq_be_tx:
1420 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1421out_free_irq_be_rx:
1422 free_irq(priv->rx_irqs[RAVB_BE], ndev);
1423out_free_irq_emac:
1424 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001425out_free_irq:
1426 free_irq(ndev->irq, ndev);
1427out_napi_off:
1428 napi_disable(&priv->napi[RAVB_NC]);
1429 napi_disable(&priv->napi[RAVB_BE]);
1430 return error;
1431}
1432
1433/* Timeout function for Ethernet AVB */
1434static void ravb_tx_timeout(struct net_device *ndev)
1435{
1436 struct ravb_private *priv = netdev_priv(ndev);
1437
1438 netif_err(priv, tx_err, ndev,
1439 "transmit timed out, status %08x, resetting...\n",
1440 ravb_read(ndev, ISS));
1441
1442 /* tx_errors count up */
1443 ndev->stats.tx_errors++;
1444
1445 schedule_work(&priv->work);
1446}
1447
1448static void ravb_tx_timeout_work(struct work_struct *work)
1449{
1450 struct ravb_private *priv = container_of(work, struct ravb_private,
1451 work);
1452 struct net_device *ndev = priv->ndev;
1453
1454 netif_tx_stop_all_queues(ndev);
1455
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001456 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001457 if (priv->chip_id == RCAR_GEN2)
1458 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001459
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001460 /* Wait for DMA stopping */
1461 ravb_stop_dma(ndev);
1462
1463 ravb_ring_free(ndev, RAVB_BE);
1464 ravb_ring_free(ndev, RAVB_NC);
1465
1466 /* Device init */
1467 ravb_dmac_init(ndev);
1468 ravb_emac_init(ndev);
1469
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001470 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001471 if (priv->chip_id == RCAR_GEN2)
1472 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001473
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001474 netif_tx_start_all_queues(ndev);
1475}
1476
1477/* Packet transmit function for Ethernet AVB */
1478static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1479{
1480 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001481 int num_tx_desc = priv->num_tx_desc;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001482 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001483 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001484 struct ravb_tx_desc *desc;
1485 unsigned long flags;
1486 u32 dma_addr;
1487 void *buffer;
1488 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001489 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001490
1491 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001492 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001493 num_tx_desc) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001494 netif_err(priv, tx_queued, ndev,
1495 "still transmitting with the full ring!\n");
1496 netif_stop_subqueue(ndev, q);
1497 spin_unlock_irqrestore(&priv->lock, flags);
1498 return NETDEV_TX_BUSY;
1499 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001500
1501 if (skb_put_padto(skb, ETH_ZLEN))
Dan Carpenter9199cb72017-04-22 13:46:56 +03001502 goto exit;
1503
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001504 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * num_tx_desc);
1505 priv->tx_skb[q][entry / num_tx_desc] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001506
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001507 if (num_tx_desc > 1) {
1508 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1509 entry / num_tx_desc * DPTR_ALIGN;
1510 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
Masaru Nagai8ec3e8a2017-01-16 11:45:21 +01001511
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001512 /* Zero length DMA descriptors are problematic as they seem
1513 * to terminate DMA transfers. Avoid them by simply using a
1514 * length of DPTR_ALIGN (4) when skb data is aligned to
1515 * DPTR_ALIGN.
1516 *
1517 * As skb is guaranteed to have at least ETH_ZLEN (60)
1518 * bytes of data by the call to skb_put_padto() above this
1519 * is safe with respect to both the length of the first DMA
1520 * descriptor (len) overflowing the available data and the
1521 * length of the second DMA descriptor (skb->len - len)
1522 * being negative.
1523 */
1524 if (len == 0)
1525 len = DPTR_ALIGN;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001526
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001527 memcpy(buffer, skb->data, len);
1528 dma_addr = dma_map_single(ndev->dev.parent, buffer, len,
1529 DMA_TO_DEVICE);
1530 if (dma_mapping_error(ndev->dev.parent, dma_addr))
1531 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001532
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001533 desc = &priv->tx_ring[q][entry];
1534 desc->ds_tagl = cpu_to_le16(len);
1535 desc->dptr = cpu_to_le32(dma_addr);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001536
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001537 buffer = skb->data + len;
1538 len = skb->len - len;
1539 dma_addr = dma_map_single(ndev->dev.parent, buffer, len,
1540 DMA_TO_DEVICE);
1541 if (dma_mapping_error(ndev->dev.parent, dma_addr))
1542 goto unmap;
1543
1544 desc++;
1545 } else {
1546 desc = &priv->tx_ring[q][entry];
1547 len = skb->len;
1548 dma_addr = dma_map_single(ndev->dev.parent, skb->data, skb->len,
1549 DMA_TO_DEVICE);
1550 if (dma_mapping_error(ndev->dev.parent, dma_addr))
1551 goto drop;
1552 }
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001553 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001554 desc->dptr = cpu_to_le32(dma_addr);
1555
1556 /* TX timestamp required */
1557 if (q == RAVB_NC) {
1558 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1559 if (!ts_skb) {
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001560 if (num_tx_desc > 1) {
1561 desc--;
1562 dma_unmap_single(ndev->dev.parent, dma_addr,
1563 len, DMA_TO_DEVICE);
1564 }
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001565 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001566 }
1567 ts_skb->skb = skb;
1568 ts_skb->tag = priv->ts_skb_tag++;
1569 priv->ts_skb_tag &= 0x3ff;
1570 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1571
1572 /* TAG and timestamp required flag */
1573 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001574 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
Niklas Söderlunde49b42f2018-07-16 14:19:27 +02001575 desc->ds_tagl |= cpu_to_le16(ts_skb->tag << 12);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001576 }
1577
Lino Sanfilippod7be81a2016-03-27 12:22:02 +02001578 skb_tx_timestamp(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001579 /* Descriptor type must be set after all the above writes */
1580 dma_wmb();
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001581 if (num_tx_desc > 1) {
1582 desc->die_dt = DT_FEND;
1583 desc--;
1584 desc->die_dt = DT_FSTART;
1585 } else {
1586 desc->die_dt = DT_FSINGLE;
1587 }
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001588 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001589
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001590 priv->cur_tx[q] += num_tx_desc;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001591 if (priv->cur_tx[q] - priv->dirty_tx[q] >
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001592 (priv->num_tx_ring[q] - 1) * num_tx_desc &&
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +01001593 !ravb_tx_free(ndev, q, true))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001594 netif_stop_subqueue(ndev, q);
1595
1596exit:
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001597 spin_unlock_irqrestore(&priv->lock, flags);
1598 return NETDEV_TX_OK;
1599
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001600unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001601 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001602 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001603drop:
1604 dev_kfree_skb_any(skb);
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02001605 priv->tx_skb[q][entry / num_tx_desc] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001606 goto exit;
1607}
1608
1609static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01001610 struct net_device *sb_dev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001611{
1612 /* If skb needs TX timestamp, it is handled in network control queue */
1613 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1614 RAVB_BE;
1615
1616}
1617
1618static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1619{
1620 struct ravb_private *priv = netdev_priv(ndev);
1621 struct net_device_stats *nstats, *stats0, *stats1;
1622
1623 nstats = &ndev->stats;
1624 stats0 = &priv->stats[RAVB_BE];
1625 stats1 = &priv->stats[RAVB_NC];
1626
1627 nstats->tx_dropped += ravb_read(ndev, TROCR);
1628 ravb_write(ndev, 0, TROCR); /* (write clear) */
1629 nstats->collisions += ravb_read(ndev, CDCR);
1630 ravb_write(ndev, 0, CDCR); /* (write clear) */
1631 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1632 ravb_write(ndev, 0, LCCR); /* (write clear) */
1633
1634 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1635 ravb_write(ndev, 0, CERCR); /* (write clear) */
1636 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1637 ravb_write(ndev, 0, CEECR); /* (write clear) */
1638
1639 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1640 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1641 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1642 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1643 nstats->multicast = stats0->multicast + stats1->multicast;
1644 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1645 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1646 nstats->rx_frame_errors =
1647 stats0->rx_frame_errors + stats1->rx_frame_errors;
1648 nstats->rx_length_errors =
1649 stats0->rx_length_errors + stats1->rx_length_errors;
1650 nstats->rx_missed_errors =
1651 stats0->rx_missed_errors + stats1->rx_missed_errors;
1652 nstats->rx_over_errors =
1653 stats0->rx_over_errors + stats1->rx_over_errors;
1654
1655 return nstats;
1656}
1657
1658/* Update promiscuous bit */
1659static void ravb_set_rx_mode(struct net_device *ndev)
1660{
1661 struct ravb_private *priv = netdev_priv(ndev);
1662 unsigned long flags;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001663
1664 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001665 ravb_modify(ndev, ECMR, ECMR_PRM,
1666 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001667 spin_unlock_irqrestore(&priv->lock, flags);
1668}
1669
1670/* Device close function for Ethernet AVB */
1671static int ravb_close(struct net_device *ndev)
1672{
Johan Hovold9f70eb32016-11-28 19:25:06 +01001673 struct device_node *np = ndev->dev.parent->of_node;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001674 struct ravb_private *priv = netdev_priv(ndev);
1675 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1676
1677 netif_tx_stop_all_queues(ndev);
1678
1679 /* Disable interrupts by clearing the interrupt masks. */
1680 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001681 ravb_write(ndev, 0, RIC2);
1682 ravb_write(ndev, 0, TIC);
1683
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001684 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001685 if (priv->chip_id == RCAR_GEN2)
1686 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001687
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001688 /* Set the config mode to stop the AVB-DMAC's processes */
1689 if (ravb_stop_dma(ndev) < 0)
1690 netdev_err(ndev,
1691 "device will be stopped after h/w processes are done.\n");
1692
1693 /* Clear the timestamp list */
1694 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1695 list_del(&ts_skb->list);
1696 kfree(ts_skb);
1697 }
1698
1699 /* PHY disconnect */
Philippe Reynes0f635172016-08-20 00:52:18 +02001700 if (ndev->phydev) {
1701 phy_stop(ndev->phydev);
1702 phy_disconnect(ndev->phydev);
Johan Hovold9f70eb32016-11-28 19:25:06 +01001703 if (of_phy_is_fixed_link(np))
1704 of_phy_deregister_fixed_link(np);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001705 }
1706
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001707 if (priv->chip_id != RCAR_GEN2) {
1708 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1709 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1710 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1711 free_irq(priv->rx_irqs[RAVB_BE], ndev);
Geert Uytterhoeven7fa816b2016-05-07 13:17:11 +02001712 free_irq(priv->emac_irq, ndev);
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001713 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001714 free_irq(ndev->irq, ndev);
1715
1716 napi_disable(&priv->napi[RAVB_NC]);
1717 napi_disable(&priv->napi[RAVB_BE]);
1718
1719 /* Free all the skb's in the RX queue and the DMA buffers. */
1720 ravb_ring_free(ndev, RAVB_BE);
1721 ravb_ring_free(ndev, RAVB_NC);
1722
1723 return 0;
1724}
1725
1726static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1727{
1728 struct ravb_private *priv = netdev_priv(ndev);
1729 struct hwtstamp_config config;
1730
1731 config.flags = 0;
1732 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1733 HWTSTAMP_TX_OFF;
1734 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1735 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1736 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1737 config.rx_filter = HWTSTAMP_FILTER_ALL;
1738 else
1739 config.rx_filter = HWTSTAMP_FILTER_NONE;
1740
1741 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1742 -EFAULT : 0;
1743}
1744
1745/* Control hardware time stamping */
1746static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1747{
1748 struct ravb_private *priv = netdev_priv(ndev);
1749 struct hwtstamp_config config;
1750 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1751 u32 tstamp_tx_ctrl;
1752
1753 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1754 return -EFAULT;
1755
1756 /* Reserved for future extensions */
1757 if (config.flags)
1758 return -EINVAL;
1759
1760 switch (config.tx_type) {
1761 case HWTSTAMP_TX_OFF:
1762 tstamp_tx_ctrl = 0;
1763 break;
1764 case HWTSTAMP_TX_ON:
1765 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1766 break;
1767 default:
1768 return -ERANGE;
1769 }
1770
1771 switch (config.rx_filter) {
1772 case HWTSTAMP_FILTER_NONE:
1773 tstamp_rx_ctrl = 0;
1774 break;
1775 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1776 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1777 break;
1778 default:
1779 config.rx_filter = HWTSTAMP_FILTER_ALL;
1780 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1781 }
1782
1783 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1784 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1785
1786 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1787 -EFAULT : 0;
1788}
1789
1790/* ioctl to device function */
1791static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1792{
Philippe Reynes0f635172016-08-20 00:52:18 +02001793 struct phy_device *phydev = ndev->phydev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001794
1795 if (!netif_running(ndev))
1796 return -EINVAL;
1797
1798 if (!phydev)
1799 return -ENODEV;
1800
1801 switch (cmd) {
1802 case SIOCGHWTSTAMP:
1803 return ravb_hwtstamp_get(ndev, req);
1804 case SIOCSHWTSTAMP:
1805 return ravb_hwtstamp_set(ndev, req);
1806 }
1807
1808 return phy_mii_ioctl(phydev, req, cmd);
1809}
1810
Niklas Söderlund75efa062018-02-16 17:10:08 +01001811static int ravb_change_mtu(struct net_device *ndev, int new_mtu)
1812{
1813 if (netif_running(ndev))
1814 return -EBUSY;
1815
1816 ndev->mtu = new_mtu;
1817 netdev_update_features(ndev);
1818
1819 return 0;
1820}
1821
Simon Horman4d86d382017-10-04 09:54:27 +02001822static void ravb_set_rx_csum(struct net_device *ndev, bool enable)
1823{
1824 struct ravb_private *priv = netdev_priv(ndev);
1825 unsigned long flags;
1826
1827 spin_lock_irqsave(&priv->lock, flags);
1828
1829 /* Disable TX and RX */
1830 ravb_rcv_snd_disable(ndev);
1831
1832 /* Modify RX Checksum setting */
1833 ravb_modify(ndev, ECMR, ECMR_RCSC, enable ? ECMR_RCSC : 0);
1834
1835 /* Enable TX and RX */
1836 ravb_rcv_snd_enable(ndev);
1837
1838 spin_unlock_irqrestore(&priv->lock, flags);
1839}
1840
1841static int ravb_set_features(struct net_device *ndev,
1842 netdev_features_t features)
1843{
1844 netdev_features_t changed = ndev->features ^ features;
1845
1846 if (changed & NETIF_F_RXCSUM)
1847 ravb_set_rx_csum(ndev, features & NETIF_F_RXCSUM);
1848
1849 ndev->features = features;
1850
1851 return 0;
1852}
1853
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001854static const struct net_device_ops ravb_netdev_ops = {
1855 .ndo_open = ravb_open,
1856 .ndo_stop = ravb_close,
1857 .ndo_start_xmit = ravb_start_xmit,
1858 .ndo_select_queue = ravb_select_queue,
1859 .ndo_get_stats = ravb_get_stats,
1860 .ndo_set_rx_mode = ravb_set_rx_mode,
1861 .ndo_tx_timeout = ravb_tx_timeout,
1862 .ndo_do_ioctl = ravb_do_ioctl,
Niklas Söderlund75efa062018-02-16 17:10:08 +01001863 .ndo_change_mtu = ravb_change_mtu,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001864 .ndo_validate_addr = eth_validate_addr,
1865 .ndo_set_mac_address = eth_mac_addr,
Simon Horman4d86d382017-10-04 09:54:27 +02001866 .ndo_set_features = ravb_set_features,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001867};
1868
1869/* MDIO bus init function */
1870static int ravb_mdio_init(struct ravb_private *priv)
1871{
1872 struct platform_device *pdev = priv->pdev;
1873 struct device *dev = &pdev->dev;
1874 int error;
1875
1876 /* Bitbang init */
1877 priv->mdiobb.ops = &bb_ops;
1878
1879 /* MII controller setting */
1880 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1881 if (!priv->mii_bus)
1882 return -ENOMEM;
1883
1884 /* Hook up MII support for ethtool */
1885 priv->mii_bus->name = "ravb_mii";
1886 priv->mii_bus->parent = dev;
1887 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1888 pdev->name, pdev->id);
1889
1890 /* Register MDIO bus */
1891 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1892 if (error)
1893 goto out_free_bus;
1894
1895 return 0;
1896
1897out_free_bus:
1898 free_mdio_bitbang(priv->mii_bus);
1899 return error;
1900}
1901
1902/* MDIO bus release function */
1903static int ravb_mdio_release(struct ravb_private *priv)
1904{
1905 /* Unregister mdio bus */
1906 mdiobus_unregister(priv->mii_bus);
1907
1908 /* Free bitbang info */
1909 free_mdio_bitbang(priv->mii_bus);
1910
1911 return 0;
1912}
1913
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001914static const struct of_device_id ravb_match_table[] = {
1915 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1916 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001917 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001918 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001919 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001920 { }
1921};
1922MODULE_DEVICE_TABLE(of, ravb_match_table);
1923
Simon Hormanb3d39a82015-11-20 11:29:39 -08001924static int ravb_set_gti(struct net_device *ndev)
1925{
Geert Uytterhoevenab104612017-10-12 10:24:53 +02001926 struct ravb_private *priv = netdev_priv(ndev);
Simon Hormanb3d39a82015-11-20 11:29:39 -08001927 struct device *dev = ndev->dev.parent;
Simon Hormanb3d39a82015-11-20 11:29:39 -08001928 unsigned long rate;
Simon Hormanb3d39a82015-11-20 11:29:39 -08001929 uint64_t inc;
1930
Geert Uytterhoevenab104612017-10-12 10:24:53 +02001931 rate = clk_get_rate(priv->clk);
Wolfram Sanga6d37132016-04-08 13:28:42 +02001932 if (!rate)
1933 return -EINVAL;
1934
Simon Hormanb3d39a82015-11-20 11:29:39 -08001935 inc = 1000000000ULL << 20;
1936 do_div(inc, rate);
1937
1938 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1939 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1940 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1941 return -EINVAL;
1942 }
1943
1944 ravb_write(ndev, inc, GTI);
1945
1946 return 0;
1947}
1948
Niklas Söderlund01841652016-08-03 15:56:47 +02001949static void ravb_set_config_mode(struct net_device *ndev)
1950{
1951 struct ravb_private *priv = netdev_priv(ndev);
1952
1953 if (priv->chip_id == RCAR_GEN2) {
1954 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
1955 /* Set CSEL value */
1956 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
1957 } else {
1958 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
1959 CCC_GAC | CCC_CSEL_HPB);
1960 }
1961}
1962
Simon Horman0a5d3292019-04-23 15:01:53 +02001963static const struct soc_device_attribute ravb_delay_mode_quirk_match[] = {
1964 { .soc_id = "r8a774c0" },
1965 { .soc_id = "r8a77990" },
1966 { .soc_id = "r8a77995" },
1967 { /* sentinel */ }
1968};
1969
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01001970/* Set tx and rx clock internal delay modes */
1971static void ravb_set_delay_mode(struct net_device *ndev)
1972{
1973 struct ravb_private *priv = netdev_priv(ndev);
1974 int set = 0;
1975
1976 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1977 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID)
1978 set |= APSR_DM_RDM;
1979
1980 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
Simon Horman0a5d3292019-04-23 15:01:53 +02001981 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) {
1982 if (!WARN(soc_device_match(ravb_delay_mode_quirk_match),
1983 "phy-mode %s requires TX clock internal delay mode which is not supported by this hardware revision. Please update device tree",
1984 phy_modes(priv->phy_interface)))
1985 set |= APSR_DM_TDM;
1986 }
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01001987
1988 ravb_modify(ndev, APSR, APSR_DM, set);
1989}
1990
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001991static int ravb_probe(struct platform_device *pdev)
1992{
1993 struct device_node *np = pdev->dev.of_node;
1994 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001995 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001996 struct net_device *ndev;
1997 int error, irq, q;
1998 struct resource *res;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001999 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002000
2001 if (!np) {
2002 dev_err(&pdev->dev,
2003 "this driver is required to be instantiated from device tree\n");
2004 return -EINVAL;
2005 }
2006
2007 /* Get base address */
2008 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2009 if (!res) {
2010 dev_err(&pdev->dev, "invalid resource\n");
2011 return -EINVAL;
2012 }
2013
2014 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
2015 NUM_TX_QUEUE, NUM_RX_QUEUE);
2016 if (!ndev)
2017 return -ENOMEM;
2018
Simon Horman4d86d382017-10-04 09:54:27 +02002019 ndev->features = NETIF_F_RXCSUM;
2020 ndev->hw_features = NETIF_F_RXCSUM;
2021
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002022 pm_runtime_enable(&pdev->dev);
2023 pm_runtime_get_sync(&pdev->dev);
2024
2025 /* The Ether-specific entries in the device structure. */
2026 ndev->base_addr = res->start;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002027
Wolfram Sange8668632016-03-01 17:37:58 +01002028 chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002029
2030 if (chip_id == RCAR_GEN3)
2031 irq = platform_get_irq_byname(pdev, "ch22");
2032 else
2033 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002034 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03002035 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002036 goto out_release;
2037 }
2038 ndev->irq = irq;
2039
2040 SET_NETDEV_DEV(ndev, &pdev->dev);
2041
2042 priv = netdev_priv(ndev);
2043 priv->ndev = ndev;
2044 priv->pdev = pdev;
2045 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
2046 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
2047 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
2048 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
2049 priv->addr = devm_ioremap_resource(&pdev->dev, res);
2050 if (IS_ERR(priv->addr)) {
2051 error = PTR_ERR(priv->addr);
2052 goto out_release;
2053 }
2054
2055 spin_lock_init(&priv->lock);
2056 INIT_WORK(&priv->work, ravb_tx_timeout_work);
2057
2058 priv->phy_interface = of_get_phy_mode(np);
2059
2060 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
2061 priv->avb_link_active_low =
2062 of_property_read_bool(np, "renesas,ether-link-active-low");
2063
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002064 if (chip_id == RCAR_GEN3) {
2065 irq = platform_get_irq_byname(pdev, "ch24");
2066 if (irq < 0) {
2067 error = irq;
2068 goto out_release;
2069 }
2070 priv->emac_irq = irq;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09002071 for (i = 0; i < NUM_RX_QUEUE; i++) {
2072 irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
2073 if (irq < 0) {
2074 error = irq;
2075 goto out_release;
2076 }
2077 priv->rx_irqs[i] = irq;
2078 }
2079 for (i = 0; i < NUM_TX_QUEUE; i++) {
2080 irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
2081 if (irq < 0) {
2082 error = irq;
2083 goto out_release;
2084 }
2085 priv->tx_irqs[i] = irq;
2086 }
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002087 }
2088
2089 priv->chip_id = chip_id;
2090
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002091 priv->clk = devm_clk_get(&pdev->dev, NULL);
Geert Uytterhoevenab104612017-10-12 10:24:53 +02002092 if (IS_ERR(priv->clk)) {
2093 error = PTR_ERR(priv->clk);
2094 goto out_release;
2095 }
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002096
Niklas Söderlund75efa062018-02-16 17:10:08 +01002097 ndev->max_mtu = 2048 - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
2098 ndev->min_mtu = ETH_MIN_MTU;
2099
Kazuya Mizuguchif5433052018-09-19 10:06:21 +02002100 priv->num_tx_desc = chip_id == RCAR_GEN2 ?
2101 NUM_TX_DESC_GEN2 : NUM_TX_DESC_GEN3;
2102
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002103 /* Set function */
2104 ndev->netdev_ops = &ravb_netdev_ops;
2105 ndev->ethtool_ops = &ravb_ethtool_ops;
2106
2107 /* Set AVB config mode */
Niklas Söderlund01841652016-08-03 15:56:47 +02002108 ravb_set_config_mode(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002109
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002110 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08002111 error = ravb_set_gti(ndev);
2112 if (error)
2113 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002114
2115 /* Request GTI loading */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03002116 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002117
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002118 if (priv->chip_id != RCAR_GEN2)
2119 ravb_set_delay_mode(ndev);
2120
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002121 /* Allocate descriptor base address table */
2122 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002123 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002124 &priv->desc_bat_dma, GFP_KERNEL);
2125 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09002126 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002127 "Cannot allocate desc base address table (size %d bytes)\n",
2128 priv->desc_bat_size);
2129 error = -ENOMEM;
2130 goto out_release;
2131 }
2132 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2133 priv->desc_bat[q].die_dt = DT_EOS;
2134 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2135
2136 /* Initialise HW timestamp list */
2137 INIT_LIST_HEAD(&priv->ts_skb_list);
2138
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002139 /* Initialise PTP Clock driver */
2140 if (chip_id != RCAR_GEN2)
2141 ravb_ptp_init(ndev, pdev);
2142
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002143 /* Debug message level */
2144 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2145
2146 /* Read and set MAC address */
2147 ravb_read_mac_address(ndev, of_get_mac_address(np));
2148 if (!is_valid_ether_addr(ndev->dev_addr)) {
2149 dev_warn(&pdev->dev,
2150 "no valid MAC address supplied, using a random one\n");
2151 eth_hw_addr_random(ndev);
2152 }
2153
2154 /* MDIO bus init */
2155 error = ravb_mdio_init(priv);
2156 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09002157 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002158 goto out_dma_free;
2159 }
2160
2161 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
2162 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
2163
2164 /* Network device register */
2165 error = register_netdev(ndev);
2166 if (error)
2167 goto out_napi_del;
2168
Geert Uytterhoevenab104612017-10-12 10:24:53 +02002169 device_set_wakeup_capable(&pdev->dev, 1);
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002170
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002171 /* Print device information */
2172 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2173 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2174
2175 platform_set_drvdata(pdev, ndev);
2176
2177 return 0;
2178
2179out_napi_del:
2180 netif_napi_del(&priv->napi[RAVB_NC]);
2181 netif_napi_del(&priv->napi[RAVB_BE]);
2182 ravb_mdio_release(priv);
2183out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002184 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002185 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002186
2187 /* Stop PTP Clock driver */
2188 if (chip_id != RCAR_GEN2)
2189 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002190out_release:
Sergei Shtylyov5d0c1002017-12-31 21:41:35 +03002191 free_netdev(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002192
2193 pm_runtime_put(&pdev->dev);
2194 pm_runtime_disable(&pdev->dev);
2195 return error;
2196}
2197
2198static int ravb_remove(struct platform_device *pdev)
2199{
2200 struct net_device *ndev = platform_get_drvdata(pdev);
2201 struct ravb_private *priv = netdev_priv(ndev);
2202
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002203 /* Stop PTP Clock driver */
2204 if (priv->chip_id != RCAR_GEN2)
2205 ravb_ptp_stop(ndev);
2206
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002207 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002208 priv->desc_bat_dma);
2209 /* Set reset mode */
2210 ravb_write(ndev, CCC_OPC_RESET, CCC);
2211 pm_runtime_put_sync(&pdev->dev);
2212 unregister_netdev(ndev);
2213 netif_napi_del(&priv->napi[RAVB_NC]);
2214 netif_napi_del(&priv->napi[RAVB_BE]);
2215 ravb_mdio_release(priv);
2216 pm_runtime_disable(&pdev->dev);
2217 free_netdev(ndev);
2218 platform_set_drvdata(pdev, NULL);
2219
2220 return 0;
2221}
2222
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002223static int ravb_wol_setup(struct net_device *ndev)
2224{
2225 struct ravb_private *priv = netdev_priv(ndev);
2226
2227 /* Disable interrupts by clearing the interrupt masks. */
2228 ravb_write(ndev, 0, RIC0);
2229 ravb_write(ndev, 0, RIC2);
2230 ravb_write(ndev, 0, TIC);
2231
2232 /* Only allow ECI interrupts */
2233 synchronize_irq(priv->emac_irq);
2234 napi_disable(&priv->napi[RAVB_NC]);
2235 napi_disable(&priv->napi[RAVB_BE]);
2236 ravb_write(ndev, ECSIPR_MPDIP, ECSIPR);
2237
2238 /* Enable MagicPacket */
2239 ravb_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
2240
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002241 return enable_irq_wake(priv->emac_irq);
2242}
2243
2244static int ravb_wol_restore(struct net_device *ndev)
2245{
2246 struct ravb_private *priv = netdev_priv(ndev);
2247 int ret;
2248
2249 napi_enable(&priv->napi[RAVB_NC]);
2250 napi_enable(&priv->napi[RAVB_BE]);
2251
2252 /* Disable MagicPacket */
2253 ravb_modify(ndev, ECMR, ECMR_MPDE, 0);
2254
2255 ret = ravb_close(ndev);
2256 if (ret < 0)
2257 return ret;
2258
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002259 return disable_irq_wake(priv->emac_irq);
2260}
2261
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002262static int __maybe_unused ravb_suspend(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002263{
2264 struct net_device *ndev = dev_get_drvdata(dev);
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002265 struct ravb_private *priv = netdev_priv(ndev);
2266 int ret;
Niklas Söderlund01841652016-08-03 15:56:47 +02002267
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002268 if (!netif_running(ndev))
2269 return 0;
2270
2271 netif_device_detach(ndev);
2272
2273 if (priv->wol_enabled)
2274 ret = ravb_wol_setup(ndev);
2275 else
Niklas Söderlund01841652016-08-03 15:56:47 +02002276 ret = ravb_close(ndev);
Niklas Söderlund01841652016-08-03 15:56:47 +02002277
2278 return ret;
2279}
2280
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002281static int __maybe_unused ravb_resume(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002282{
2283 struct net_device *ndev = dev_get_drvdata(dev);
2284 struct ravb_private *priv = netdev_priv(ndev);
2285 int ret = 0;
2286
Geert Uytterhoeven6b782f42017-12-11 09:54:09 +01002287 /* If WoL is enabled set reset mode to rearm the WoL logic */
2288 if (priv->wol_enabled)
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002289 ravb_write(ndev, CCC_OPC_RESET, CCC);
2290
Niklas Söderlund01841652016-08-03 15:56:47 +02002291 /* All register have been reset to default values.
2292 * Restore all registers which where setup at probe time and
2293 * reopen device if it was running before system suspended.
2294 */
2295
2296 /* Set AVB config mode */
2297 ravb_set_config_mode(ndev);
2298
2299 /* Set GTI value */
2300 ret = ravb_set_gti(ndev);
2301 if (ret)
2302 return ret;
2303
2304 /* Request GTI loading */
2305 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
2306
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002307 if (priv->chip_id != RCAR_GEN2)
2308 ravb_set_delay_mode(ndev);
2309
Niklas Söderlund01841652016-08-03 15:56:47 +02002310 /* Restore descriptor base address table */
2311 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2312
2313 if (netif_running(ndev)) {
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002314 if (priv->wol_enabled) {
2315 ret = ravb_wol_restore(ndev);
2316 if (ret)
2317 return ret;
2318 }
Niklas Söderlund01841652016-08-03 15:56:47 +02002319 ret = ravb_open(ndev);
2320 if (ret < 0)
2321 return ret;
2322 netif_device_attach(ndev);
2323 }
2324
2325 return ret;
2326}
2327
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002328static int __maybe_unused ravb_runtime_nop(struct device *dev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002329{
2330 /* Runtime PM callback shared between ->runtime_suspend()
2331 * and ->runtime_resume(). Simply returns success.
2332 *
2333 * This driver re-initializes all registers after
2334 * pm_runtime_get_sync() anyway so there is no need
2335 * to save and restore registers here.
2336 */
2337 return 0;
2338}
2339
2340static const struct dev_pm_ops ravb_dev_pm_ops = {
Niklas Söderlundb89b8152016-08-10 13:09:49 +02002341 SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume)
Kazuya Mizuguchi524c6f62016-05-30 05:25:43 +09002342 SET_RUNTIME_PM_OPS(ravb_runtime_nop, ravb_runtime_nop, NULL)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002343};
2344
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002345static struct platform_driver ravb_driver = {
2346 .probe = ravb_probe,
2347 .remove = ravb_remove,
2348 .driver = {
2349 .name = "ravb",
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002350 .pm = &ravb_dev_pm_ops,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002351 .of_match_table = ravb_match_table,
2352 },
2353};
2354
2355module_platform_driver(ravb_driver);
2356
2357MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2358MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2359MODULE_LICENSE("GPL v2");