blob: a8822a756e08324396cfe9c9812f40e9db14dec9 [file] [log] [blame]
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001/* Renesas Ethernet AVB device driver
2 *
3 * Copyright (C) 2014-2015 Renesas Electronics Corporation
4 * Copyright (C) 2015 Renesas Solutions Corp.
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03005 * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
Sergei Shtylyovc1566332015-06-11 01:01:43 +03006 *
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 Shtylyovc1566332015-06-11 01:01:43 +030031#include <linux/pm_runtime.h>
32#include <linux/slab.h>
33#include <linux/spinlock.h>
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +010034#include <linux/sys_soc.h>
Sergei Shtylyovc1566332015-06-11 01:01:43 +030035
Simon Hormanb3d39a82015-11-20 11:29:39 -080036#include <asm/div64.h>
37
Sergei Shtylyovc1566332015-06-11 01:01:43 +030038#include "ravb.h"
39
40#define RAVB_DEF_MSG_ENABLE \
41 (NETIF_MSG_LINK | \
42 NETIF_MSG_TIMER | \
43 NETIF_MSG_RX_ERR | \
44 NETIF_MSG_TX_ERR)
45
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +090046static const char *ravb_rx_irqs[NUM_RX_QUEUE] = {
47 "ch0", /* RAVB_BE */
48 "ch1", /* RAVB_NC */
49};
50
51static const char *ravb_tx_irqs[NUM_TX_QUEUE] = {
52 "ch18", /* RAVB_BE */
53 "ch19", /* RAVB_NC */
54};
55
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030056void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear,
57 u32 set)
58{
59 ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg);
60}
61
Sergei Shtylyova0d2f202015-06-11 01:02:30 +030062int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value)
Sergei Shtylyovc1566332015-06-11 01:01:43 +030063{
64 int i;
65
66 for (i = 0; i < 10000; i++) {
67 if ((ravb_read(ndev, reg) & mask) == value)
68 return 0;
69 udelay(10);
70 }
71 return -ETIMEDOUT;
72}
73
74static int ravb_config(struct net_device *ndev)
75{
76 int error;
77
78 /* Set config mode */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030079 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030080 /* Check if the operating mode is changed to the config mode */
81 error = ravb_wait(ndev, CSR, CSR_OPS, CSR_OPS_CONFIG);
82 if (error)
83 netdev_err(ndev, "failed to switch device to config mode\n");
84
85 return error;
86}
87
88static void ravb_set_duplex(struct net_device *ndev)
89{
90 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030091
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030092 ravb_modify(ndev, ECMR, ECMR_DM, priv->duplex ? ECMR_DM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030093}
94
95static void ravb_set_rate(struct net_device *ndev)
96{
97 struct ravb_private *priv = netdev_priv(ndev);
98
99 switch (priv->speed) {
100 case 100: /* 100BASE */
101 ravb_write(ndev, GECMR_SPEED_100, GECMR);
102 break;
103 case 1000: /* 1000BASE */
104 ravb_write(ndev, GECMR_SPEED_1000, GECMR);
105 break;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300106 }
107}
108
109static void ravb_set_buffer_align(struct sk_buff *skb)
110{
111 u32 reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1);
112
113 if (reserve)
114 skb_reserve(skb, RAVB_ALIGN - reserve);
115}
116
117/* Get MAC address from the MAC address registers
118 *
119 * Ethernet AVB device doesn't have ROM for MAC address.
120 * This function gets the MAC address that was used by a bootloader.
121 */
122static void ravb_read_mac_address(struct net_device *ndev, const u8 *mac)
123{
124 if (mac) {
125 ether_addr_copy(ndev->dev_addr, mac);
126 } else {
Sergei Shtylyovd9660632015-12-05 00:58:07 +0300127 u32 mahr = ravb_read(ndev, MAHR);
128 u32 malr = ravb_read(ndev, MALR);
129
130 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
131 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
132 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
133 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
134 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
135 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300136 }
137}
138
139static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
140{
141 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
142 mdiobb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300143
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300144 ravb_modify(priv->ndev, PIR, mask, set ? mask : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300145}
146
147/* MDC pin control */
148static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level)
149{
150 ravb_mdio_ctrl(ctrl, PIR_MDC, level);
151}
152
153/* Data I/O pin control */
154static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
155{
156 ravb_mdio_ctrl(ctrl, PIR_MMD, output);
157}
158
159/* Set data bit */
160static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
161{
162 ravb_mdio_ctrl(ctrl, PIR_MDO, value);
163}
164
165/* Get data bit */
166static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl)
167{
168 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
169 mdiobb);
170
171 return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0;
172}
173
174/* MDIO bus control struct */
175static struct mdiobb_ops bb_ops = {
176 .owner = THIS_MODULE,
177 .set_mdc = ravb_set_mdc,
178 .set_mdio_dir = ravb_set_mdio_dir,
179 .set_mdio_data = ravb_set_mdio_data,
180 .get_mdio_data = ravb_get_mdio_data,
181};
182
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100183/* Free TX skb function for AVB-IP */
184static int ravb_tx_free(struct net_device *ndev, int q, bool free_txed_only)
185{
186 struct ravb_private *priv = netdev_priv(ndev);
187 struct net_device_stats *stats = &priv->stats[q];
188 struct ravb_tx_desc *desc;
189 int free_num = 0;
190 int entry;
191 u32 size;
192
193 for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
194 bool txed;
195
196 entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
197 NUM_TX_DESC);
198 desc = &priv->tx_ring[q][entry];
199 txed = desc->die_dt == DT_FEMPTY;
200 if (free_txed_only && !txed)
201 break;
202 /* Descriptor type must be checked before all other reads */
203 dma_rmb();
204 size = le16_to_cpu(desc->ds_tagl) & TX_DS;
205 /* Free the original skb. */
206 if (priv->tx_skb[q][entry / NUM_TX_DESC]) {
207 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
208 size, DMA_TO_DEVICE);
209 /* Last packet descriptor? */
210 if (entry % NUM_TX_DESC == NUM_TX_DESC - 1) {
211 entry /= NUM_TX_DESC;
212 dev_kfree_skb_any(priv->tx_skb[q][entry]);
213 priv->tx_skb[q][entry] = NULL;
214 if (txed)
215 stats->tx_packets++;
216 }
217 free_num++;
218 }
219 if (txed)
220 stats->tx_bytes += size;
221 desc->die_dt = DT_EEMPTY;
222 }
223 return free_num;
224}
225
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300226/* Free skb's and DMA buffers for Ethernet AVB */
227static void ravb_ring_free(struct net_device *ndev, int q)
228{
229 struct ravb_private *priv = netdev_priv(ndev);
230 int ring_size;
231 int i;
232
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300233 if (priv->rx_ring[q]) {
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100234 for (i = 0; i < priv->num_rx_ring[q]; i++) {
235 struct ravb_ex_rx_desc *desc = &priv->rx_ring[q][i];
236
237 if (!dma_mapping_error(ndev->dev.parent,
238 le32_to_cpu(desc->dptr)))
239 dma_unmap_single(ndev->dev.parent,
240 le32_to_cpu(desc->dptr),
241 PKT_BUF_SZ,
242 DMA_FROM_DEVICE);
243 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300244 ring_size = sizeof(struct ravb_ex_rx_desc) *
245 (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900246 dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300247 priv->rx_desc_dma[q]);
248 priv->rx_ring[q] = NULL;
249 }
250
251 if (priv->tx_ring[q]) {
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100252 ravb_tx_free(ndev, q, false);
253
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300254 ring_size = sizeof(struct ravb_tx_desc) *
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300255 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900256 dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300257 priv->tx_desc_dma[q]);
258 priv->tx_ring[q] = NULL;
259 }
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100260
Eugeniu Rosca79514ef2017-06-06 00:08:10 +0200261 /* Free RX skb ringbuffer */
262 if (priv->rx_skb[q]) {
263 for (i = 0; i < priv->num_rx_ring[q]; i++)
264 dev_kfree_skb(priv->rx_skb[q][i]);
265 }
266 kfree(priv->rx_skb[q]);
267 priv->rx_skb[q] = NULL;
268
269 /* Free aligned TX buffers */
270 kfree(priv->tx_align[q]);
271 priv->tx_align[q] = NULL;
272
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100273 /* Free TX skb ringbuffer.
274 * SKBs are freed by ravb_tx_free() call above.
275 */
276 kfree(priv->tx_skb[q]);
277 priv->tx_skb[q] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300278}
279
280/* Format skb and descriptor buffer for Ethernet AVB */
281static void ravb_ring_format(struct net_device *ndev, int q)
282{
283 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300284 struct ravb_ex_rx_desc *rx_desc;
285 struct ravb_tx_desc *tx_desc;
286 struct ravb_desc *desc;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300287 int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q];
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300288 int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] *
289 NUM_TX_DESC;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300290 dma_addr_t dma_addr;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300291 int i;
292
293 priv->cur_rx[q] = 0;
294 priv->cur_tx[q] = 0;
295 priv->dirty_rx[q] = 0;
296 priv->dirty_tx[q] = 0;
297
298 memset(priv->rx_ring[q], 0, rx_ring_size);
299 /* Build RX ring buffer */
300 for (i = 0; i < priv->num_rx_ring[q]; i++) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300301 /* RX descriptor */
302 rx_desc = &priv->rx_ring[q][i];
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900303 rx_desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900304 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900305 PKT_BUF_SZ,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300306 DMA_FROM_DEVICE);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300307 /* We just set the data size to 0 for a failed mapping which
308 * should prevent DMA from happening...
309 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900310 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300311 rx_desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300312 rx_desc->dptr = cpu_to_le32(dma_addr);
313 rx_desc->die_dt = DT_FEMPTY;
314 }
315 rx_desc = &priv->rx_ring[q][i];
316 rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
317 rx_desc->die_dt = DT_LINKFIX; /* type */
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300318
319 memset(priv->tx_ring[q], 0, tx_ring_size);
320 /* Build TX ring buffer */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300321 for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
322 i++, tx_desc++) {
323 tx_desc->die_dt = DT_EEMPTY;
324 tx_desc++;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300325 tx_desc->die_dt = DT_EEMPTY;
326 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300327 tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
328 tx_desc->die_dt = DT_LINKFIX; /* type */
329
330 /* RX descriptor base address for best effort */
331 desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
332 desc->die_dt = DT_LINKFIX; /* type */
333 desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
334
335 /* TX descriptor base address for best effort */
336 desc = &priv->desc_bat[q];
337 desc->die_dt = DT_LINKFIX; /* type */
338 desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
339}
340
341/* Init skb and descriptor buffer for Ethernet AVB */
342static int ravb_ring_init(struct net_device *ndev, int q)
343{
344 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300345 struct sk_buff *skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300346 int ring_size;
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300347 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300348
349 /* Allocate RX and TX skb rings */
350 priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
351 sizeof(*priv->rx_skb[q]), GFP_KERNEL);
352 priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
353 sizeof(*priv->tx_skb[q]), GFP_KERNEL);
354 if (!priv->rx_skb[q] || !priv->tx_skb[q])
355 goto error;
356
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300357 for (i = 0; i < priv->num_rx_ring[q]; i++) {
358 skb = netdev_alloc_skb(ndev, PKT_BUF_SZ + RAVB_ALIGN - 1);
359 if (!skb)
360 goto error;
361 ravb_set_buffer_align(skb);
362 priv->rx_skb[q][i] = skb;
363 }
364
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300365 /* Allocate rings for the aligned buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300366 priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
367 DPTR_ALIGN - 1, GFP_KERNEL);
368 if (!priv->tx_align[q])
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300369 goto error;
370
371 /* Allocate all RX descriptors. */
372 ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900373 priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300374 &priv->rx_desc_dma[q],
375 GFP_KERNEL);
376 if (!priv->rx_ring[q])
377 goto error;
378
379 priv->dirty_rx[q] = 0;
380
381 /* Allocate all TX descriptors. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300382 ring_size = sizeof(struct ravb_tx_desc) *
383 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900384 priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300385 &priv->tx_desc_dma[q],
386 GFP_KERNEL);
387 if (!priv->tx_ring[q])
388 goto error;
389
390 return 0;
391
392error:
393 ravb_ring_free(ndev, q);
394
395 return -ENOMEM;
396}
397
398/* E-MAC init function */
399static void ravb_emac_init(struct net_device *ndev)
400{
401 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300402
403 /* Receive frame limit set register */
404 ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
405
Simon Horman4d86d382017-10-04 09:54:27 +0200406 /* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */
Sergei Shtylyov1c1fa822016-01-11 00:27:38 +0300407 ravb_write(ndev, ECMR_ZPF | (priv->duplex ? ECMR_DM : 0) |
Simon Horman4d86d382017-10-04 09:54:27 +0200408 (ndev->features & NETIF_F_RXCSUM ? ECMR_RCSC : 0) |
Sergei Shtylyov1c1fa822016-01-11 00:27:38 +0300409 ECMR_TE | ECMR_RE, ECMR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300410
411 ravb_set_rate(ndev);
412
413 /* Set MAC address */
414 ravb_write(ndev,
415 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
416 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
417 ravb_write(ndev,
418 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
419
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300420 /* E-MAC status register clear */
421 ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
422
423 /* E-MAC interrupt enable register */
424 ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
425}
426
427/* Device init function for Ethernet AVB */
428static int ravb_dmac_init(struct net_device *ndev)
429{
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900430 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300431 int error;
432
433 /* Set CONFIG mode */
434 error = ravb_config(ndev);
435 if (error)
436 return error;
437
438 error = ravb_ring_init(ndev, RAVB_BE);
439 if (error)
440 return error;
441 error = ravb_ring_init(ndev, RAVB_NC);
442 if (error) {
443 ravb_ring_free(ndev, RAVB_BE);
444 return error;
445 }
446
447 /* Descriptor format */
448 ravb_ring_format(ndev, RAVB_BE);
449 ravb_ring_format(ndev, RAVB_NC);
450
451#if defined(__LITTLE_ENDIAN)
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300452 ravb_modify(ndev, CCC, CCC_BOC, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300453#else
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300454 ravb_modify(ndev, CCC, CCC_BOC, CCC_BOC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300455#endif
456
457 /* Set AVB RX */
Masaru Nagai8d9c4182016-06-01 03:01:28 +0900458 ravb_write(ndev,
459 RCR_EFFS | RCR_ENCF | RCR_ETS0 | RCR_ESF | 0x18000000, RCR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300460
461 /* Set FIFO size */
462 ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00222200, TGC);
463
464 /* Timestamp enable */
465 ravb_write(ndev, TCCR_TFEN, TCCR);
466
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900467 /* Interrupt init: */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900468 if (priv->chip_id == RCAR_GEN3) {
469 /* Clear DIL.DPLx */
470 ravb_write(ndev, 0, DIL);
471 /* Set queue specific interrupt */
472 ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE);
473 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300474 /* Frame receive */
475 ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900476 /* Disable FIFO full warning */
477 ravb_write(ndev, 0, RIC1);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300478 /* Receive FIFO full error, descriptor empty */
479 ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
480 /* Frame transmitted, timestamp FIFO updated */
481 ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
482
483 /* Setting the control will start the AVB-DMAC process. */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300484 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300485
486 return 0;
487}
488
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300489static void ravb_get_tx_tstamp(struct net_device *ndev)
490{
491 struct ravb_private *priv = netdev_priv(ndev);
492 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
493 struct skb_shared_hwtstamps shhwtstamps;
494 struct sk_buff *skb;
495 struct timespec64 ts;
496 u16 tag, tfa_tag;
497 int count;
498 u32 tfa2;
499
500 count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
501 while (count--) {
502 tfa2 = ravb_read(ndev, TFA2);
503 tfa_tag = (tfa2 & TFA2_TST) >> 16;
504 ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
505 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
506 ravb_read(ndev, TFA1);
507 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
508 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
509 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
510 list) {
511 skb = ts_skb->skb;
512 tag = ts_skb->tag;
513 list_del(&ts_skb->list);
514 kfree(ts_skb);
515 if (tag == tfa_tag) {
516 skb_tstamp_tx(skb, &shhwtstamps);
517 break;
518 }
519 }
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300520 ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300521 }
522}
523
Simon Horman4d86d382017-10-04 09:54:27 +0200524static void ravb_rx_csum(struct sk_buff *skb)
525{
526 u8 *hw_csum;
527
528 /* The hardware checksum is 2 bytes appended to packet data */
529 if (unlikely(skb->len < 2))
530 return;
531 hw_csum = skb_tail_pointer(skb) - 2;
532 skb->csum = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum));
533 skb->ip_summed = CHECKSUM_COMPLETE;
534 skb_trim(skb, skb->len - 2);
535}
536
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300537/* Packet receive function for Ethernet AVB */
538static bool ravb_rx(struct net_device *ndev, int *quota, int q)
539{
540 struct ravb_private *priv = netdev_priv(ndev);
541 int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
542 int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
543 priv->cur_rx[q];
544 struct net_device_stats *stats = &priv->stats[q];
545 struct ravb_ex_rx_desc *desc;
546 struct sk_buff *skb;
547 dma_addr_t dma_addr;
548 struct timespec64 ts;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300549 u8 desc_status;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300550 u16 pkt_len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300551 int limit;
552
553 boguscnt = min(boguscnt, *quota);
554 limit = boguscnt;
555 desc = &priv->rx_ring[q][entry];
556 while (desc->die_dt != DT_FEMPTY) {
557 /* Descriptor type must be checked before all other reads */
558 dma_rmb();
559 desc_status = desc->msc;
560 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
561
562 if (--boguscnt < 0)
563 break;
564
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300565 /* We use 0-byte descriptors to mark the DMA mapping errors */
566 if (!pkt_len)
567 continue;
568
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300569 if (desc_status & MSC_MC)
570 stats->multicast++;
571
572 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
573 MSC_CEEF)) {
574 stats->rx_errors++;
575 if (desc_status & MSC_CRC)
576 stats->rx_crc_errors++;
577 if (desc_status & MSC_RFE)
578 stats->rx_frame_errors++;
579 if (desc_status & (MSC_RTLF | MSC_RTSF))
580 stats->rx_length_errors++;
581 if (desc_status & MSC_CEEF)
582 stats->rx_missed_errors++;
583 } else {
584 u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
585
586 skb = priv->rx_skb[q][entry];
587 priv->rx_skb[q][entry] = NULL;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900588 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900589 PKT_BUF_SZ,
Sergei Shtylyove2370f02015-07-15 00:56:52 +0300590 DMA_FROM_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300591 get_ts &= (q == RAVB_NC) ?
592 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
593 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
594 if (get_ts) {
595 struct skb_shared_hwtstamps *shhwtstamps;
596
597 shhwtstamps = skb_hwtstamps(skb);
598 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
599 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
600 32) | le32_to_cpu(desc->ts_sl);
601 ts.tv_nsec = le32_to_cpu(desc->ts_n);
602 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
603 }
Simon Horman4d86d382017-10-04 09:54:27 +0200604
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300605 skb_put(skb, pkt_len);
606 skb->protocol = eth_type_trans(skb, ndev);
Simon Horman4d86d382017-10-04 09:54:27 +0200607 if (ndev->features & NETIF_F_RXCSUM)
608 ravb_rx_csum(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300609 napi_gro_receive(&priv->napi[q], skb);
610 stats->rx_packets++;
611 stats->rx_bytes += pkt_len;
612 }
613
614 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
615 desc = &priv->rx_ring[q][entry];
616 }
617
618 /* Refill the RX ring buffers. */
619 for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
620 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
621 desc = &priv->rx_ring[q][entry];
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900622 desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300623
624 if (!priv->rx_skb[q][entry]) {
625 skb = netdev_alloc_skb(ndev,
626 PKT_BUF_SZ + RAVB_ALIGN - 1);
627 if (!skb)
628 break; /* Better luck next round. */
629 ravb_set_buffer_align(skb);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900630 dma_addr = dma_map_single(ndev->dev.parent, skb->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300631 le16_to_cpu(desc->ds_cc),
632 DMA_FROM_DEVICE);
633 skb_checksum_none_assert(skb);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300634 /* We just set the data size to 0 for a failed mapping
635 * which should prevent DMA from happening...
636 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900637 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300638 desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300639 desc->dptr = cpu_to_le32(dma_addr);
640 priv->rx_skb[q][entry] = skb;
641 }
642 /* Descriptor type must be set after all the above writes */
643 dma_wmb();
644 desc->die_dt = DT_FEMPTY;
645 }
646
647 *quota -= limit - (++boguscnt);
648
649 return boguscnt <= 0;
650}
651
652static void ravb_rcv_snd_disable(struct net_device *ndev)
653{
654 /* Disable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300655 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300656}
657
658static void ravb_rcv_snd_enable(struct net_device *ndev)
659{
660 /* Enable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300661 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300662}
663
664/* function for waiting dma process finished */
665static int ravb_stop_dma(struct net_device *ndev)
666{
667 int error;
668
669 /* Wait for stopping the hardware TX process */
670 error = ravb_wait(ndev, TCCR,
671 TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
672 if (error)
673 return error;
674
675 error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
676 0);
677 if (error)
678 return error;
679
680 /* Stop the E-MAC's RX/TX processes. */
681 ravb_rcv_snd_disable(ndev);
682
683 /* Wait for stopping the RX DMA process */
684 error = ravb_wait(ndev, CSR, CSR_RPO, 0);
685 if (error)
686 return error;
687
688 /* Stop AVB-DMAC process */
689 return ravb_config(ndev);
690}
691
692/* E-MAC interrupt handler */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900693static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300694{
695 struct ravb_private *priv = netdev_priv(ndev);
696 u32 ecsr, psr;
697
698 ecsr = ravb_read(ndev, ECSR);
699 ravb_write(ndev, ecsr, ECSR); /* clear interrupt */
Niklas Söderlund3e3d6472017-08-01 12:14:36 +0200700
701 if (ecsr & ECSR_MPD)
702 pm_wakeup_event(&priv->pdev->dev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300703 if (ecsr & ECSR_ICD)
704 ndev->stats.tx_carrier_errors++;
705 if (ecsr & ECSR_LCHNG) {
706 /* Link changed */
707 if (priv->no_avb_link)
708 return;
709 psr = ravb_read(ndev, PSR);
710 if (priv->avb_link_active_low)
711 psr ^= PSR_LMON;
712 if (!(psr & PSR_LMON)) {
713 /* DIsable RX and TX */
714 ravb_rcv_snd_disable(ndev);
715 } else {
716 /* Enable RX and TX */
717 ravb_rcv_snd_enable(ndev);
718 }
719 }
720}
721
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900722static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
723{
724 struct net_device *ndev = dev_id;
725 struct ravb_private *priv = netdev_priv(ndev);
726
727 spin_lock(&priv->lock);
728 ravb_emac_interrupt_unlocked(ndev);
729 mmiowb();
730 spin_unlock(&priv->lock);
731 return IRQ_HANDLED;
732}
733
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300734/* Error interrupt handler */
735static void ravb_error_interrupt(struct net_device *ndev)
736{
737 struct ravb_private *priv = netdev_priv(ndev);
738 u32 eis, ris2;
739
740 eis = ravb_read(ndev, EIS);
741 ravb_write(ndev, ~EIS_QFS, EIS);
742 if (eis & EIS_QFS) {
743 ris2 = ravb_read(ndev, RIS2);
744 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF), RIS2);
745
746 /* Receive Descriptor Empty int */
747 if (ris2 & RIS2_QFF0)
748 priv->stats[RAVB_BE].rx_over_errors++;
749
750 /* Receive Descriptor Empty int */
751 if (ris2 & RIS2_QFF1)
752 priv->stats[RAVB_NC].rx_over_errors++;
753
754 /* Receive FIFO Overflow int */
755 if (ris2 & RIS2_RFFF)
756 priv->rx_fifo_errors++;
757 }
758}
759
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900760static bool ravb_queue_interrupt(struct net_device *ndev, int q)
761{
762 struct ravb_private *priv = netdev_priv(ndev);
763 u32 ris0 = ravb_read(ndev, RIS0);
764 u32 ric0 = ravb_read(ndev, RIC0);
765 u32 tis = ravb_read(ndev, TIS);
766 u32 tic = ravb_read(ndev, TIC);
767
768 if (((ris0 & ric0) & BIT(q)) || ((tis & tic) & BIT(q))) {
769 if (napi_schedule_prep(&priv->napi[q])) {
770 /* Mask RX and TX interrupts */
771 if (priv->chip_id == RCAR_GEN2) {
772 ravb_write(ndev, ric0 & ~BIT(q), RIC0);
773 ravb_write(ndev, tic & ~BIT(q), TIC);
774 } else {
775 ravb_write(ndev, BIT(q), RID0);
776 ravb_write(ndev, BIT(q), TID);
777 }
778 __napi_schedule(&priv->napi[q]);
779 } else {
780 netdev_warn(ndev,
781 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
782 ris0, ric0);
783 netdev_warn(ndev,
784 " tx status 0x%08x, tx mask 0x%08x.\n",
785 tis, tic);
786 }
787 return true;
788 }
789 return false;
790}
791
792static bool ravb_timestamp_interrupt(struct net_device *ndev)
793{
794 u32 tis = ravb_read(ndev, TIS);
795
796 if (tis & TIS_TFUF) {
797 ravb_write(ndev, ~TIS_TFUF, TIS);
798 ravb_get_tx_tstamp(ndev);
799 return true;
800 }
801 return false;
802}
803
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300804static irqreturn_t ravb_interrupt(int irq, void *dev_id)
805{
806 struct net_device *ndev = dev_id;
807 struct ravb_private *priv = netdev_priv(ndev);
808 irqreturn_t result = IRQ_NONE;
809 u32 iss;
810
811 spin_lock(&priv->lock);
812 /* Get interrupt status */
813 iss = ravb_read(ndev, ISS);
814
815 /* Received and transmitted interrupts */
816 if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300817 int q;
818
819 /* Timestamp updated */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900820 if (ravb_timestamp_interrupt(ndev))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300821 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300822
823 /* Network control and best effort queue RX/TX */
824 for (q = RAVB_NC; q >= RAVB_BE; q--) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900825 if (ravb_queue_interrupt(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300826 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300827 }
828 }
829
830 /* E-MAC status summary */
831 if (iss & ISS_MS) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900832 ravb_emac_interrupt_unlocked(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300833 result = IRQ_HANDLED;
834 }
835
836 /* Error status summary */
837 if (iss & ISS_ES) {
838 ravb_error_interrupt(ndev);
839 result = IRQ_HANDLED;
840 }
841
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900842 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300843 if (iss & ISS_CGIS) {
844 ravb_ptp_interrupt(ndev);
Yoshihiro Kaneko38c848c2016-03-16 00:52:16 +0900845 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300846 }
Sergei Shtylyova0d2f202015-06-11 01:02:30 +0300847
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300848 mmiowb();
849 spin_unlock(&priv->lock);
850 return result;
851}
852
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900853/* Timestamp/Error/gPTP interrupt handler */
854static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
855{
856 struct net_device *ndev = dev_id;
857 struct ravb_private *priv = netdev_priv(ndev);
858 irqreturn_t result = IRQ_NONE;
859 u32 iss;
860
861 spin_lock(&priv->lock);
862 /* Get interrupt status */
863 iss = ravb_read(ndev, ISS);
864
865 /* Timestamp updated */
866 if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
867 result = IRQ_HANDLED;
868
869 /* Error status summary */
870 if (iss & ISS_ES) {
871 ravb_error_interrupt(ndev);
872 result = IRQ_HANDLED;
873 }
874
875 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300876 if (iss & ISS_CGIS) {
877 ravb_ptp_interrupt(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900878 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300879 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900880
881 mmiowb();
882 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
898 mmiowb();
899 spin_unlock(&priv->lock);
900 return result;
901}
902
903static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
904{
905 return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
906}
907
908static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
909{
910 return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
911}
912
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300913static int ravb_poll(struct napi_struct *napi, int budget)
914{
915 struct net_device *ndev = napi->dev;
916 struct ravb_private *priv = netdev_priv(ndev);
917 unsigned long flags;
918 int q = napi - priv->napi;
919 int mask = BIT(q);
920 int quota = budget;
921 u32 ris0, tis;
922
923 for (;;) {
924 tis = ravb_read(ndev, TIS);
925 ris0 = ravb_read(ndev, RIS0);
926 if (!((ris0 & mask) || (tis & mask)))
927 break;
928
929 /* Processing RX Descriptor Ring */
930 if (ris0 & mask) {
931 /* Clear RX interrupt */
932 ravb_write(ndev, ~mask, RIS0);
933 if (ravb_rx(ndev, &quota, q))
934 goto out;
935 }
936 /* Processing TX Descriptor Ring */
937 if (tis & mask) {
938 spin_lock_irqsave(&priv->lock, flags);
939 /* Clear TX interrupt */
940 ravb_write(ndev, ~mask, TIS);
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100941 ravb_tx_free(ndev, q, true);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300942 netif_wake_subqueue(ndev, q);
943 mmiowb();
944 spin_unlock_irqrestore(&priv->lock, flags);
945 }
946 }
947
948 napi_complete(napi);
949
950 /* Re-enable RX/TX interrupts */
951 spin_lock_irqsave(&priv->lock, flags);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900952 if (priv->chip_id == RCAR_GEN2) {
953 ravb_modify(ndev, RIC0, mask, mask);
954 ravb_modify(ndev, TIC, mask, mask);
955 } else {
956 ravb_write(ndev, mask, RIE0);
957 ravb_write(ndev, mask, TIE);
958 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300959 mmiowb();
960 spin_unlock_irqrestore(&priv->lock, flags);
961
962 /* Receive error message handling */
963 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
964 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
Kazuya Mizuguchi18a3ed52017-01-12 13:21:06 +0100965 if (priv->rx_over_errors != ndev->stats.rx_over_errors)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300966 ndev->stats.rx_over_errors = priv->rx_over_errors;
Kazuya Mizuguchi18a3ed52017-01-12 13:21:06 +0100967 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300968 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300969out:
970 return budget - quota;
971}
972
973/* PHY state control function */
974static void ravb_adjust_link(struct net_device *ndev)
975{
976 struct ravb_private *priv = netdev_priv(ndev);
Philippe Reynes0f635172016-08-20 00:52:18 +0200977 struct phy_device *phydev = ndev->phydev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300978 bool new_state = false;
979
980 if (phydev->link) {
981 if (phydev->duplex != priv->duplex) {
982 new_state = true;
983 priv->duplex = phydev->duplex;
984 ravb_set_duplex(ndev);
985 }
986
987 if (phydev->speed != priv->speed) {
988 new_state = true;
989 priv->speed = phydev->speed;
990 ravb_set_rate(ndev);
991 }
992 if (!priv->link) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300993 ravb_modify(ndev, ECMR, ECMR_TXF, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300994 new_state = true;
995 priv->link = phydev->link;
996 if (priv->no_avb_link)
997 ravb_rcv_snd_enable(ndev);
998 }
999 } else if (priv->link) {
1000 new_state = true;
1001 priv->link = 0;
1002 priv->speed = 0;
1003 priv->duplex = -1;
1004 if (priv->no_avb_link)
1005 ravb_rcv_snd_disable(ndev);
1006 }
1007
1008 if (new_state && netif_msg_link(priv))
1009 phy_print_status(phydev);
1010}
1011
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +01001012static const struct soc_device_attribute r8a7795es10[] = {
1013 { .soc_id = "r8a7795", .revision = "ES1.0", },
1014 { /* sentinel */ }
1015};
1016
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001017/* PHY init function */
1018static int ravb_phy_init(struct net_device *ndev)
1019{
1020 struct device_node *np = ndev->dev.parent->of_node;
1021 struct ravb_private *priv = netdev_priv(ndev);
1022 struct phy_device *phydev;
1023 struct device_node *pn;
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +09001024 int err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001025
1026 priv->link = 0;
1027 priv->speed = 0;
1028 priv->duplex = -1;
1029
1030 /* Try connecting to PHY */
1031 pn = of_parse_phandle(np, "phy-handle", 0);
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +09001032 if (!pn) {
1033 /* In the case of a fixed PHY, the DT node associated
1034 * to the PHY is the Ethernet MAC DT node.
1035 */
1036 if (of_phy_is_fixed_link(np)) {
1037 err = of_phy_register_fixed_link(np);
1038 if (err)
1039 return err;
1040 }
1041 pn = of_node_get(np);
1042 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001043 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
1044 priv->phy_interface);
Peter Chenc9b1eb82016-08-01 15:02:39 +08001045 of_node_put(pn);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001046 if (!phydev) {
1047 netdev_err(ndev, "failed to connect PHY\n");
Johan Hovold9f70eb32016-11-28 19:25:06 +01001048 err = -ENOENT;
1049 goto err_deregister_fixed_link;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001050 }
1051
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +01001052 /* This driver only support 10/100Mbit speeds on R-Car H3 ES1.0
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001053 * at this time.
1054 */
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +01001055 if (soc_device_match(r8a7795es10)) {
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001056 err = phy_set_max_speed(phydev, SPEED_100);
1057 if (err) {
1058 netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
Johan Hovold9f70eb32016-11-28 19:25:06 +01001059 goto err_phy_disconnect;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001060 }
1061
1062 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
1063 }
1064
Kazuya Mizuguchi54499962015-12-14 00:15:58 +09001065 /* 10BASE is not supported */
1066 phydev->supported &= ~PHY_10BT_FEATURES;
1067
Andrew Lunn22209432016-01-06 20:11:13 +01001068 phy_attached_info(phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001069
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001070 return 0;
Johan Hovold9f70eb32016-11-28 19:25:06 +01001071
1072err_phy_disconnect:
1073 phy_disconnect(phydev);
1074err_deregister_fixed_link:
1075 if (of_phy_is_fixed_link(np))
1076 of_phy_deregister_fixed_link(np);
1077
1078 return err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001079}
1080
1081/* PHY control start function */
1082static int ravb_phy_start(struct net_device *ndev)
1083{
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001084 int error;
1085
1086 error = ravb_phy_init(ndev);
1087 if (error)
1088 return error;
1089
Philippe Reynes0f635172016-08-20 00:52:18 +02001090 phy_start(ndev->phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001091
1092 return 0;
1093}
1094
Philippe Reynes04462f22016-08-20 00:52:19 +02001095static int ravb_get_link_ksettings(struct net_device *ndev,
1096 struct ethtool_link_ksettings *cmd)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001097{
1098 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001099 unsigned long flags;
1100
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03001101 if (!ndev->phydev)
1102 return -ENODEV;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001103
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03001104 spin_lock_irqsave(&priv->lock, flags);
1105 phy_ethtool_ksettings_get(ndev->phydev, cmd);
1106 spin_unlock_irqrestore(&priv->lock, flags);
1107
1108 return 0;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001109}
1110
Philippe Reynes04462f22016-08-20 00:52:19 +02001111static int ravb_set_link_ksettings(struct net_device *ndev,
1112 const struct ethtool_link_ksettings *cmd)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001113{
1114 struct ravb_private *priv = netdev_priv(ndev);
1115 unsigned long flags;
1116 int error;
1117
Philippe Reynes0f635172016-08-20 00:52:18 +02001118 if (!ndev->phydev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001119 return -ENODEV;
1120
1121 spin_lock_irqsave(&priv->lock, flags);
1122
1123 /* Disable TX and RX */
1124 ravb_rcv_snd_disable(ndev);
1125
Philippe Reynes04462f22016-08-20 00:52:19 +02001126 error = phy_ethtool_ksettings_set(ndev->phydev, cmd);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001127 if (error)
1128 goto error_exit;
1129
Philippe Reynes04462f22016-08-20 00:52:19 +02001130 if (cmd->base.duplex == DUPLEX_FULL)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001131 priv->duplex = 1;
1132 else
1133 priv->duplex = 0;
1134
1135 ravb_set_duplex(ndev);
1136
1137error_exit:
1138 mdelay(1);
1139
1140 /* Enable TX and RX */
1141 ravb_rcv_snd_enable(ndev);
1142
1143 mmiowb();
1144 spin_unlock_irqrestore(&priv->lock, flags);
1145
1146 return error;
1147}
1148
1149static int ravb_nway_reset(struct net_device *ndev)
1150{
1151 struct ravb_private *priv = netdev_priv(ndev);
1152 int error = -ENODEV;
1153 unsigned long flags;
1154
Philippe Reynes0f635172016-08-20 00:52:18 +02001155 if (ndev->phydev) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001156 spin_lock_irqsave(&priv->lock, flags);
Philippe Reynes0f635172016-08-20 00:52:18 +02001157 error = phy_start_aneg(ndev->phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001158 spin_unlock_irqrestore(&priv->lock, flags);
1159 }
1160
1161 return error;
1162}
1163
1164static u32 ravb_get_msglevel(struct net_device *ndev)
1165{
1166 struct ravb_private *priv = netdev_priv(ndev);
1167
1168 return priv->msg_enable;
1169}
1170
1171static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1172{
1173 struct ravb_private *priv = netdev_priv(ndev);
1174
1175 priv->msg_enable = value;
1176}
1177
1178static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1179 "rx_queue_0_current",
1180 "tx_queue_0_current",
1181 "rx_queue_0_dirty",
1182 "tx_queue_0_dirty",
1183 "rx_queue_0_packets",
1184 "tx_queue_0_packets",
1185 "rx_queue_0_bytes",
1186 "tx_queue_0_bytes",
1187 "rx_queue_0_mcast_packets",
1188 "rx_queue_0_errors",
1189 "rx_queue_0_crc_errors",
1190 "rx_queue_0_frame_errors",
1191 "rx_queue_0_length_errors",
1192 "rx_queue_0_missed_errors",
1193 "rx_queue_0_over_errors",
1194
1195 "rx_queue_1_current",
1196 "tx_queue_1_current",
1197 "rx_queue_1_dirty",
1198 "tx_queue_1_dirty",
1199 "rx_queue_1_packets",
1200 "tx_queue_1_packets",
1201 "rx_queue_1_bytes",
1202 "tx_queue_1_bytes",
1203 "rx_queue_1_mcast_packets",
1204 "rx_queue_1_errors",
1205 "rx_queue_1_crc_errors",
Sergei Shtylyovb17c1d92015-12-04 01:51:10 +03001206 "rx_queue_1_frame_errors",
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001207 "rx_queue_1_length_errors",
1208 "rx_queue_1_missed_errors",
1209 "rx_queue_1_over_errors",
1210};
1211
1212#define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1213
1214static int ravb_get_sset_count(struct net_device *netdev, int sset)
1215{
1216 switch (sset) {
1217 case ETH_SS_STATS:
1218 return RAVB_STATS_LEN;
1219 default:
1220 return -EOPNOTSUPP;
1221 }
1222}
1223
1224static void ravb_get_ethtool_stats(struct net_device *ndev,
1225 struct ethtool_stats *stats, u64 *data)
1226{
1227 struct ravb_private *priv = netdev_priv(ndev);
1228 int i = 0;
1229 int q;
1230
1231 /* Device-specific stats */
1232 for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1233 struct net_device_stats *stats = &priv->stats[q];
1234
1235 data[i++] = priv->cur_rx[q];
1236 data[i++] = priv->cur_tx[q];
1237 data[i++] = priv->dirty_rx[q];
1238 data[i++] = priv->dirty_tx[q];
1239 data[i++] = stats->rx_packets;
1240 data[i++] = stats->tx_packets;
1241 data[i++] = stats->rx_bytes;
1242 data[i++] = stats->tx_bytes;
1243 data[i++] = stats->multicast;
1244 data[i++] = stats->rx_errors;
1245 data[i++] = stats->rx_crc_errors;
1246 data[i++] = stats->rx_frame_errors;
1247 data[i++] = stats->rx_length_errors;
1248 data[i++] = stats->rx_missed_errors;
1249 data[i++] = stats->rx_over_errors;
1250 }
1251}
1252
1253static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1254{
1255 switch (stringset) {
1256 case ETH_SS_STATS:
1257 memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
1258 break;
1259 }
1260}
1261
1262static void ravb_get_ringparam(struct net_device *ndev,
1263 struct ethtool_ringparam *ring)
1264{
1265 struct ravb_private *priv = netdev_priv(ndev);
1266
1267 ring->rx_max_pending = BE_RX_RING_MAX;
1268 ring->tx_max_pending = BE_TX_RING_MAX;
1269 ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1270 ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1271}
1272
1273static int ravb_set_ringparam(struct net_device *ndev,
1274 struct ethtool_ringparam *ring)
1275{
1276 struct ravb_private *priv = netdev_priv(ndev);
1277 int error;
1278
1279 if (ring->tx_pending > BE_TX_RING_MAX ||
1280 ring->rx_pending > BE_RX_RING_MAX ||
1281 ring->tx_pending < BE_TX_RING_MIN ||
1282 ring->rx_pending < BE_RX_RING_MIN)
1283 return -EINVAL;
1284 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1285 return -EINVAL;
1286
1287 if (netif_running(ndev)) {
1288 netif_device_detach(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001289 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001290 if (priv->chip_id == RCAR_GEN2)
1291 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001292 /* Wait for DMA stopping */
1293 error = ravb_stop_dma(ndev);
1294 if (error) {
1295 netdev_err(ndev,
1296 "cannot set ringparam! Any AVB processes are still running?\n");
1297 return error;
1298 }
1299 synchronize_irq(ndev->irq);
1300
1301 /* Free all the skb's in the RX queue and the DMA buffers. */
1302 ravb_ring_free(ndev, RAVB_BE);
1303 ravb_ring_free(ndev, RAVB_NC);
1304 }
1305
1306 /* Set new parameters */
1307 priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1308 priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1309
1310 if (netif_running(ndev)) {
1311 error = ravb_dmac_init(ndev);
1312 if (error) {
1313 netdev_err(ndev,
1314 "%s: ravb_dmac_init() failed, error %d\n",
1315 __func__, error);
1316 return error;
1317 }
1318
1319 ravb_emac_init(ndev);
1320
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001321 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001322 if (priv->chip_id == RCAR_GEN2)
1323 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001324
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001325 netif_device_attach(ndev);
1326 }
1327
1328 return 0;
1329}
1330
1331static int ravb_get_ts_info(struct net_device *ndev,
1332 struct ethtool_ts_info *info)
1333{
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001334 struct ravb_private *priv = netdev_priv(ndev);
1335
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001336 info->so_timestamping =
1337 SOF_TIMESTAMPING_TX_SOFTWARE |
1338 SOF_TIMESTAMPING_RX_SOFTWARE |
1339 SOF_TIMESTAMPING_SOFTWARE |
1340 SOF_TIMESTAMPING_TX_HARDWARE |
1341 SOF_TIMESTAMPING_RX_HARDWARE |
1342 SOF_TIMESTAMPING_RAW_HARDWARE;
1343 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1344 info->rx_filters =
1345 (1 << HWTSTAMP_FILTER_NONE) |
1346 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1347 (1 << HWTSTAMP_FILTER_ALL);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001348 info->phc_index = ptp_clock_index(priv->ptp.clock);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001349
1350 return 0;
1351}
1352
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02001353static void ravb_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1354{
1355 struct ravb_private *priv = netdev_priv(ndev);
1356
1357 wol->supported = 0;
1358 wol->wolopts = 0;
1359
1360 if (priv->clk) {
1361 wol->supported = WAKE_MAGIC;
1362 wol->wolopts = priv->wol_enabled ? WAKE_MAGIC : 0;
1363 }
1364}
1365
1366static int ravb_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1367{
1368 struct ravb_private *priv = netdev_priv(ndev);
1369
1370 if (!priv->clk || wol->wolopts & ~WAKE_MAGIC)
1371 return -EOPNOTSUPP;
1372
1373 priv->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
1374
1375 device_set_wakeup_enable(&priv->pdev->dev, priv->wol_enabled);
1376
1377 return 0;
1378}
1379
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001380static const struct ethtool_ops ravb_ethtool_ops = {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001381 .nway_reset = ravb_nway_reset,
1382 .get_msglevel = ravb_get_msglevel,
1383 .set_msglevel = ravb_set_msglevel,
1384 .get_link = ethtool_op_get_link,
1385 .get_strings = ravb_get_strings,
1386 .get_ethtool_stats = ravb_get_ethtool_stats,
1387 .get_sset_count = ravb_get_sset_count,
1388 .get_ringparam = ravb_get_ringparam,
1389 .set_ringparam = ravb_set_ringparam,
1390 .get_ts_info = ravb_get_ts_info,
Philippe Reynes04462f22016-08-20 00:52:19 +02001391 .get_link_ksettings = ravb_get_link_ksettings,
1392 .set_link_ksettings = ravb_set_link_ksettings,
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02001393 .get_wol = ravb_get_wol,
1394 .set_wol = ravb_set_wol,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001395};
1396
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001397static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1398 struct net_device *ndev, struct device *dev,
1399 const char *ch)
1400{
1401 char *name;
1402 int error;
1403
1404 name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1405 if (!name)
1406 return -ENOMEM;
1407 error = request_irq(irq, handler, 0, name, ndev);
1408 if (error)
1409 netdev_err(ndev, "cannot request IRQ %s\n", name);
1410
1411 return error;
1412}
1413
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001414/* Network device open function for Ethernet AVB */
1415static int ravb_open(struct net_device *ndev)
1416{
1417 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001418 struct platform_device *pdev = priv->pdev;
1419 struct device *dev = &pdev->dev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001420 int error;
1421
1422 napi_enable(&priv->napi[RAVB_BE]);
1423 napi_enable(&priv->napi[RAVB_NC]);
1424
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001425 if (priv->chip_id == RCAR_GEN2) {
1426 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1427 ndev->name, ndev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001428 if (error) {
1429 netdev_err(ndev, "cannot request IRQ\n");
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001430 goto out_napi_off;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001431 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001432 } else {
1433 error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1434 dev, "ch22:multi");
1435 if (error)
1436 goto out_napi_off;
1437 error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1438 dev, "ch24:emac");
1439 if (error)
1440 goto out_free_irq;
1441 error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1442 ndev, dev, "ch0:rx_be");
1443 if (error)
1444 goto out_free_irq_emac;
1445 error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1446 ndev, dev, "ch18:tx_be");
1447 if (error)
1448 goto out_free_irq_be_rx;
1449 error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1450 ndev, dev, "ch1:rx_nc");
1451 if (error)
1452 goto out_free_irq_be_tx;
1453 error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1454 ndev, dev, "ch19:tx_nc");
1455 if (error)
1456 goto out_free_irq_nc_rx;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001457 }
1458
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001459 /* Device init */
1460 error = ravb_dmac_init(ndev);
1461 if (error)
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001462 goto out_free_irq_nc_tx;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001463 ravb_emac_init(ndev);
1464
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001465 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001466 if (priv->chip_id == RCAR_GEN2)
1467 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001468
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001469 netif_tx_start_all_queues(ndev);
1470
1471 /* PHY control start */
1472 error = ravb_phy_start(ndev);
1473 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001474 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001475
1476 return 0;
1477
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001478out_ptp_stop:
1479 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001480 if (priv->chip_id == RCAR_GEN2)
1481 ravb_ptp_stop(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001482out_free_irq_nc_tx:
1483 if (priv->chip_id == RCAR_GEN2)
1484 goto out_free_irq;
1485 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1486out_free_irq_nc_rx:
1487 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1488out_free_irq_be_tx:
1489 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1490out_free_irq_be_rx:
1491 free_irq(priv->rx_irqs[RAVB_BE], ndev);
1492out_free_irq_emac:
1493 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001494out_free_irq:
1495 free_irq(ndev->irq, ndev);
1496out_napi_off:
1497 napi_disable(&priv->napi[RAVB_NC]);
1498 napi_disable(&priv->napi[RAVB_BE]);
1499 return error;
1500}
1501
1502/* Timeout function for Ethernet AVB */
1503static void ravb_tx_timeout(struct net_device *ndev)
1504{
1505 struct ravb_private *priv = netdev_priv(ndev);
1506
1507 netif_err(priv, tx_err, ndev,
1508 "transmit timed out, status %08x, resetting...\n",
1509 ravb_read(ndev, ISS));
1510
1511 /* tx_errors count up */
1512 ndev->stats.tx_errors++;
1513
1514 schedule_work(&priv->work);
1515}
1516
1517static void ravb_tx_timeout_work(struct work_struct *work)
1518{
1519 struct ravb_private *priv = container_of(work, struct ravb_private,
1520 work);
1521 struct net_device *ndev = priv->ndev;
1522
1523 netif_tx_stop_all_queues(ndev);
1524
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001525 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001526 if (priv->chip_id == RCAR_GEN2)
1527 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001528
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001529 /* Wait for DMA stopping */
1530 ravb_stop_dma(ndev);
1531
1532 ravb_ring_free(ndev, RAVB_BE);
1533 ravb_ring_free(ndev, RAVB_NC);
1534
1535 /* Device init */
1536 ravb_dmac_init(ndev);
1537 ravb_emac_init(ndev);
1538
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001539 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001540 if (priv->chip_id == RCAR_GEN2)
1541 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001542
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001543 netif_tx_start_all_queues(ndev);
1544}
1545
1546/* Packet transmit function for Ethernet AVB */
1547static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1548{
1549 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001550 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001551 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001552 struct ravb_tx_desc *desc;
1553 unsigned long flags;
1554 u32 dma_addr;
1555 void *buffer;
1556 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001557 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001558
1559 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001560 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1561 NUM_TX_DESC) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001562 netif_err(priv, tx_queued, ndev,
1563 "still transmitting with the full ring!\n");
1564 netif_stop_subqueue(ndev, q);
1565 spin_unlock_irqrestore(&priv->lock, flags);
1566 return NETDEV_TX_BUSY;
1567 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001568
1569 if (skb_put_padto(skb, ETH_ZLEN))
Dan Carpenter9199cb72017-04-22 13:46:56 +03001570 goto exit;
1571
1572 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1573 priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001574
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001575 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1576 entry / NUM_TX_DESC * DPTR_ALIGN;
1577 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
Masaru Nagai8ec3e8a2017-01-16 11:45:21 +01001578 /* Zero length DMA descriptors are problematic as they seem to
1579 * terminate DMA transfers. Avoid them by simply using a length of
1580 * DPTR_ALIGN (4) when skb data is aligned to DPTR_ALIGN.
1581 *
1582 * As skb is guaranteed to have at least ETH_ZLEN (60) bytes of
1583 * data by the call to skb_put_padto() above this is safe with
1584 * respect to both the length of the first DMA descriptor (len)
1585 * overflowing the available data and the length of the second DMA
1586 * descriptor (skb->len - len) being negative.
1587 */
1588 if (len == 0)
1589 len = DPTR_ALIGN;
1590
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001591 memcpy(buffer, skb->data, len);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001592 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1593 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001594 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001595
1596 desc = &priv->tx_ring[q][entry];
1597 desc->ds_tagl = cpu_to_le16(len);
1598 desc->dptr = cpu_to_le32(dma_addr);
1599
1600 buffer = skb->data + len;
1601 len = skb->len - len;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001602 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1603 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001604 goto unmap;
1605
1606 desc++;
1607 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001608 desc->dptr = cpu_to_le32(dma_addr);
1609
1610 /* TX timestamp required */
1611 if (q == RAVB_NC) {
1612 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1613 if (!ts_skb) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001614 desc--;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001615 dma_unmap_single(ndev->dev.parent, dma_addr, len,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001616 DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001617 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001618 }
1619 ts_skb->skb = skb;
1620 ts_skb->tag = priv->ts_skb_tag++;
1621 priv->ts_skb_tag &= 0x3ff;
1622 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1623
1624 /* TAG and timestamp required flag */
1625 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001626 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1627 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1628 }
1629
Lino Sanfilippod7be81a2016-03-27 12:22:02 +02001630 skb_tx_timestamp(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001631 /* Descriptor type must be set after all the above writes */
1632 dma_wmb();
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001633 desc->die_dt = DT_FEND;
1634 desc--;
1635 desc->die_dt = DT_FSTART;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001636
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001637 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001638
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001639 priv->cur_tx[q] += NUM_TX_DESC;
1640 if (priv->cur_tx[q] - priv->dirty_tx[q] >
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +01001641 (priv->num_tx_ring[q] - 1) * NUM_TX_DESC &&
1642 !ravb_tx_free(ndev, q, true))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001643 netif_stop_subqueue(ndev, q);
1644
1645exit:
1646 mmiowb();
1647 spin_unlock_irqrestore(&priv->lock, flags);
1648 return NETDEV_TX_OK;
1649
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001650unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001651 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001652 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001653drop:
1654 dev_kfree_skb_any(skb);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001655 priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001656 goto exit;
1657}
1658
1659static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1660 void *accel_priv, select_queue_fallback_t fallback)
1661{
1662 /* If skb needs TX timestamp, it is handled in network control queue */
1663 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1664 RAVB_BE;
1665
1666}
1667
1668static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1669{
1670 struct ravb_private *priv = netdev_priv(ndev);
1671 struct net_device_stats *nstats, *stats0, *stats1;
1672
1673 nstats = &ndev->stats;
1674 stats0 = &priv->stats[RAVB_BE];
1675 stats1 = &priv->stats[RAVB_NC];
1676
1677 nstats->tx_dropped += ravb_read(ndev, TROCR);
1678 ravb_write(ndev, 0, TROCR); /* (write clear) */
1679 nstats->collisions += ravb_read(ndev, CDCR);
1680 ravb_write(ndev, 0, CDCR); /* (write clear) */
1681 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1682 ravb_write(ndev, 0, LCCR); /* (write clear) */
1683
1684 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1685 ravb_write(ndev, 0, CERCR); /* (write clear) */
1686 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1687 ravb_write(ndev, 0, CEECR); /* (write clear) */
1688
1689 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1690 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1691 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1692 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1693 nstats->multicast = stats0->multicast + stats1->multicast;
1694 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1695 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1696 nstats->rx_frame_errors =
1697 stats0->rx_frame_errors + stats1->rx_frame_errors;
1698 nstats->rx_length_errors =
1699 stats0->rx_length_errors + stats1->rx_length_errors;
1700 nstats->rx_missed_errors =
1701 stats0->rx_missed_errors + stats1->rx_missed_errors;
1702 nstats->rx_over_errors =
1703 stats0->rx_over_errors + stats1->rx_over_errors;
1704
1705 return nstats;
1706}
1707
1708/* Update promiscuous bit */
1709static void ravb_set_rx_mode(struct net_device *ndev)
1710{
1711 struct ravb_private *priv = netdev_priv(ndev);
1712 unsigned long flags;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001713
1714 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001715 ravb_modify(ndev, ECMR, ECMR_PRM,
1716 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001717 mmiowb();
1718 spin_unlock_irqrestore(&priv->lock, flags);
1719}
1720
1721/* Device close function for Ethernet AVB */
1722static int ravb_close(struct net_device *ndev)
1723{
Johan Hovold9f70eb32016-11-28 19:25:06 +01001724 struct device_node *np = ndev->dev.parent->of_node;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001725 struct ravb_private *priv = netdev_priv(ndev);
1726 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1727
1728 netif_tx_stop_all_queues(ndev);
1729
1730 /* Disable interrupts by clearing the interrupt masks. */
1731 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001732 ravb_write(ndev, 0, RIC2);
1733 ravb_write(ndev, 0, TIC);
1734
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001735 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001736 if (priv->chip_id == RCAR_GEN2)
1737 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001738
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001739 /* Set the config mode to stop the AVB-DMAC's processes */
1740 if (ravb_stop_dma(ndev) < 0)
1741 netdev_err(ndev,
1742 "device will be stopped after h/w processes are done.\n");
1743
1744 /* Clear the timestamp list */
1745 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1746 list_del(&ts_skb->list);
1747 kfree(ts_skb);
1748 }
1749
1750 /* PHY disconnect */
Philippe Reynes0f635172016-08-20 00:52:18 +02001751 if (ndev->phydev) {
1752 phy_stop(ndev->phydev);
1753 phy_disconnect(ndev->phydev);
Johan Hovold9f70eb32016-11-28 19:25:06 +01001754 if (of_phy_is_fixed_link(np))
1755 of_phy_deregister_fixed_link(np);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001756 }
1757
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001758 if (priv->chip_id != RCAR_GEN2) {
1759 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1760 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1761 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1762 free_irq(priv->rx_irqs[RAVB_BE], ndev);
Geert Uytterhoeven7fa816b2016-05-07 13:17:11 +02001763 free_irq(priv->emac_irq, ndev);
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001764 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001765 free_irq(ndev->irq, ndev);
1766
1767 napi_disable(&priv->napi[RAVB_NC]);
1768 napi_disable(&priv->napi[RAVB_BE]);
1769
1770 /* Free all the skb's in the RX queue and the DMA buffers. */
1771 ravb_ring_free(ndev, RAVB_BE);
1772 ravb_ring_free(ndev, RAVB_NC);
1773
1774 return 0;
1775}
1776
1777static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1778{
1779 struct ravb_private *priv = netdev_priv(ndev);
1780 struct hwtstamp_config config;
1781
1782 config.flags = 0;
1783 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1784 HWTSTAMP_TX_OFF;
1785 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1786 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1787 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1788 config.rx_filter = HWTSTAMP_FILTER_ALL;
1789 else
1790 config.rx_filter = HWTSTAMP_FILTER_NONE;
1791
1792 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1793 -EFAULT : 0;
1794}
1795
1796/* Control hardware time stamping */
1797static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1798{
1799 struct ravb_private *priv = netdev_priv(ndev);
1800 struct hwtstamp_config config;
1801 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1802 u32 tstamp_tx_ctrl;
1803
1804 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1805 return -EFAULT;
1806
1807 /* Reserved for future extensions */
1808 if (config.flags)
1809 return -EINVAL;
1810
1811 switch (config.tx_type) {
1812 case HWTSTAMP_TX_OFF:
1813 tstamp_tx_ctrl = 0;
1814 break;
1815 case HWTSTAMP_TX_ON:
1816 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1817 break;
1818 default:
1819 return -ERANGE;
1820 }
1821
1822 switch (config.rx_filter) {
1823 case HWTSTAMP_FILTER_NONE:
1824 tstamp_rx_ctrl = 0;
1825 break;
1826 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1827 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1828 break;
1829 default:
1830 config.rx_filter = HWTSTAMP_FILTER_ALL;
1831 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1832 }
1833
1834 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1835 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1836
1837 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1838 -EFAULT : 0;
1839}
1840
1841/* ioctl to device function */
1842static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1843{
Philippe Reynes0f635172016-08-20 00:52:18 +02001844 struct phy_device *phydev = ndev->phydev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001845
1846 if (!netif_running(ndev))
1847 return -EINVAL;
1848
1849 if (!phydev)
1850 return -ENODEV;
1851
1852 switch (cmd) {
1853 case SIOCGHWTSTAMP:
1854 return ravb_hwtstamp_get(ndev, req);
1855 case SIOCSHWTSTAMP:
1856 return ravb_hwtstamp_set(ndev, req);
1857 }
1858
1859 return phy_mii_ioctl(phydev, req, cmd);
1860}
1861
Simon Horman4d86d382017-10-04 09:54:27 +02001862static void ravb_set_rx_csum(struct net_device *ndev, bool enable)
1863{
1864 struct ravb_private *priv = netdev_priv(ndev);
1865 unsigned long flags;
1866
1867 spin_lock_irqsave(&priv->lock, flags);
1868
1869 /* Disable TX and RX */
1870 ravb_rcv_snd_disable(ndev);
1871
1872 /* Modify RX Checksum setting */
1873 ravb_modify(ndev, ECMR, ECMR_RCSC, enable ? ECMR_RCSC : 0);
1874
1875 /* Enable TX and RX */
1876 ravb_rcv_snd_enable(ndev);
1877
1878 spin_unlock_irqrestore(&priv->lock, flags);
1879}
1880
1881static int ravb_set_features(struct net_device *ndev,
1882 netdev_features_t features)
1883{
1884 netdev_features_t changed = ndev->features ^ features;
1885
1886 if (changed & NETIF_F_RXCSUM)
1887 ravb_set_rx_csum(ndev, features & NETIF_F_RXCSUM);
1888
1889 ndev->features = features;
1890
1891 return 0;
1892}
1893
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001894static const struct net_device_ops ravb_netdev_ops = {
1895 .ndo_open = ravb_open,
1896 .ndo_stop = ravb_close,
1897 .ndo_start_xmit = ravb_start_xmit,
1898 .ndo_select_queue = ravb_select_queue,
1899 .ndo_get_stats = ravb_get_stats,
1900 .ndo_set_rx_mode = ravb_set_rx_mode,
1901 .ndo_tx_timeout = ravb_tx_timeout,
1902 .ndo_do_ioctl = ravb_do_ioctl,
1903 .ndo_validate_addr = eth_validate_addr,
1904 .ndo_set_mac_address = eth_mac_addr,
Simon Horman4d86d382017-10-04 09:54:27 +02001905 .ndo_set_features = ravb_set_features,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001906};
1907
1908/* MDIO bus init function */
1909static int ravb_mdio_init(struct ravb_private *priv)
1910{
1911 struct platform_device *pdev = priv->pdev;
1912 struct device *dev = &pdev->dev;
1913 int error;
1914
1915 /* Bitbang init */
1916 priv->mdiobb.ops = &bb_ops;
1917
1918 /* MII controller setting */
1919 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1920 if (!priv->mii_bus)
1921 return -ENOMEM;
1922
1923 /* Hook up MII support for ethtool */
1924 priv->mii_bus->name = "ravb_mii";
1925 priv->mii_bus->parent = dev;
1926 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1927 pdev->name, pdev->id);
1928
1929 /* Register MDIO bus */
1930 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1931 if (error)
1932 goto out_free_bus;
1933
1934 return 0;
1935
1936out_free_bus:
1937 free_mdio_bitbang(priv->mii_bus);
1938 return error;
1939}
1940
1941/* MDIO bus release function */
1942static int ravb_mdio_release(struct ravb_private *priv)
1943{
1944 /* Unregister mdio bus */
1945 mdiobus_unregister(priv->mii_bus);
1946
1947 /* Free bitbang info */
1948 free_mdio_bitbang(priv->mii_bus);
1949
1950 return 0;
1951}
1952
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001953static const struct of_device_id ravb_match_table[] = {
1954 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1955 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001956 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001957 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001958 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001959 { }
1960};
1961MODULE_DEVICE_TABLE(of, ravb_match_table);
1962
Simon Hormanb3d39a82015-11-20 11:29:39 -08001963static int ravb_set_gti(struct net_device *ndev)
1964{
1965
1966 struct device *dev = ndev->dev.parent;
1967 struct device_node *np = dev->of_node;
1968 unsigned long rate;
1969 struct clk *clk;
1970 uint64_t inc;
1971
1972 clk = of_clk_get(np, 0);
1973 if (IS_ERR(clk)) {
1974 dev_err(dev, "could not get clock\n");
1975 return PTR_ERR(clk);
1976 }
1977
1978 rate = clk_get_rate(clk);
1979 clk_put(clk);
1980
Wolfram Sanga6d37132016-04-08 13:28:42 +02001981 if (!rate)
1982 return -EINVAL;
1983
Simon Hormanb3d39a82015-11-20 11:29:39 -08001984 inc = 1000000000ULL << 20;
1985 do_div(inc, rate);
1986
1987 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1988 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1989 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1990 return -EINVAL;
1991 }
1992
1993 ravb_write(ndev, inc, GTI);
1994
1995 return 0;
1996}
1997
Niklas Söderlund01841652016-08-03 15:56:47 +02001998static void ravb_set_config_mode(struct net_device *ndev)
1999{
2000 struct ravb_private *priv = netdev_priv(ndev);
2001
2002 if (priv->chip_id == RCAR_GEN2) {
2003 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
2004 /* Set CSEL value */
2005 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
2006 } else {
2007 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
2008 CCC_GAC | CCC_CSEL_HPB);
2009 }
2010}
2011
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002012/* Set tx and rx clock internal delay modes */
2013static void ravb_set_delay_mode(struct net_device *ndev)
2014{
2015 struct ravb_private *priv = netdev_priv(ndev);
2016 int set = 0;
2017
2018 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
2019 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID)
2020 set |= APSR_DM_RDM;
2021
2022 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
2023 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
2024 set |= APSR_DM_TDM;
2025
2026 ravb_modify(ndev, APSR, APSR_DM, set);
2027}
2028
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002029static int ravb_probe(struct platform_device *pdev)
2030{
2031 struct device_node *np = pdev->dev.of_node;
2032 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002033 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002034 struct net_device *ndev;
2035 int error, irq, q;
2036 struct resource *res;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09002037 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002038
2039 if (!np) {
2040 dev_err(&pdev->dev,
2041 "this driver is required to be instantiated from device tree\n");
2042 return -EINVAL;
2043 }
2044
2045 /* Get base address */
2046 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2047 if (!res) {
2048 dev_err(&pdev->dev, "invalid resource\n");
2049 return -EINVAL;
2050 }
2051
2052 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
2053 NUM_TX_QUEUE, NUM_RX_QUEUE);
2054 if (!ndev)
2055 return -ENOMEM;
2056
Simon Horman4d86d382017-10-04 09:54:27 +02002057 ndev->features = NETIF_F_RXCSUM;
2058 ndev->hw_features = NETIF_F_RXCSUM;
2059
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002060 pm_runtime_enable(&pdev->dev);
2061 pm_runtime_get_sync(&pdev->dev);
2062
2063 /* The Ether-specific entries in the device structure. */
2064 ndev->base_addr = res->start;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002065
Wolfram Sange8668632016-03-01 17:37:58 +01002066 chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002067
2068 if (chip_id == RCAR_GEN3)
2069 irq = platform_get_irq_byname(pdev, "ch22");
2070 else
2071 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002072 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03002073 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002074 goto out_release;
2075 }
2076 ndev->irq = irq;
2077
2078 SET_NETDEV_DEV(ndev, &pdev->dev);
2079
2080 priv = netdev_priv(ndev);
2081 priv->ndev = ndev;
2082 priv->pdev = pdev;
2083 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
2084 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
2085 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
2086 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
2087 priv->addr = devm_ioremap_resource(&pdev->dev, res);
2088 if (IS_ERR(priv->addr)) {
2089 error = PTR_ERR(priv->addr);
2090 goto out_release;
2091 }
2092
2093 spin_lock_init(&priv->lock);
2094 INIT_WORK(&priv->work, ravb_tx_timeout_work);
2095
2096 priv->phy_interface = of_get_phy_mode(np);
2097
2098 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
2099 priv->avb_link_active_low =
2100 of_property_read_bool(np, "renesas,ether-link-active-low");
2101
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002102 if (chip_id == RCAR_GEN3) {
2103 irq = platform_get_irq_byname(pdev, "ch24");
2104 if (irq < 0) {
2105 error = irq;
2106 goto out_release;
2107 }
2108 priv->emac_irq = irq;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09002109 for (i = 0; i < NUM_RX_QUEUE; i++) {
2110 irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
2111 if (irq < 0) {
2112 error = irq;
2113 goto out_release;
2114 }
2115 priv->rx_irqs[i] = irq;
2116 }
2117 for (i = 0; i < NUM_TX_QUEUE; i++) {
2118 irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
2119 if (irq < 0) {
2120 error = irq;
2121 goto out_release;
2122 }
2123 priv->tx_irqs[i] = irq;
2124 }
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002125 }
2126
2127 priv->chip_id = chip_id;
2128
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002129 /* Get clock, if not found that's OK but Wake-On-Lan is unavailable */
2130 priv->clk = devm_clk_get(&pdev->dev, NULL);
2131 if (IS_ERR(priv->clk))
2132 priv->clk = NULL;
2133
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002134 /* Set function */
2135 ndev->netdev_ops = &ravb_netdev_ops;
2136 ndev->ethtool_ops = &ravb_ethtool_ops;
2137
2138 /* Set AVB config mode */
Niklas Söderlund01841652016-08-03 15:56:47 +02002139 ravb_set_config_mode(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002140
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002141 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08002142 error = ravb_set_gti(ndev);
2143 if (error)
2144 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002145
2146 /* Request GTI loading */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03002147 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002148
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002149 if (priv->chip_id != RCAR_GEN2)
2150 ravb_set_delay_mode(ndev);
2151
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002152 /* Allocate descriptor base address table */
2153 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002154 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002155 &priv->desc_bat_dma, GFP_KERNEL);
2156 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09002157 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002158 "Cannot allocate desc base address table (size %d bytes)\n",
2159 priv->desc_bat_size);
2160 error = -ENOMEM;
2161 goto out_release;
2162 }
2163 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2164 priv->desc_bat[q].die_dt = DT_EOS;
2165 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2166
2167 /* Initialise HW timestamp list */
2168 INIT_LIST_HEAD(&priv->ts_skb_list);
2169
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002170 /* Initialise PTP Clock driver */
2171 if (chip_id != RCAR_GEN2)
2172 ravb_ptp_init(ndev, pdev);
2173
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002174 /* Debug message level */
2175 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2176
2177 /* Read and set MAC address */
2178 ravb_read_mac_address(ndev, of_get_mac_address(np));
2179 if (!is_valid_ether_addr(ndev->dev_addr)) {
2180 dev_warn(&pdev->dev,
2181 "no valid MAC address supplied, using a random one\n");
2182 eth_hw_addr_random(ndev);
2183 }
2184
2185 /* MDIO bus init */
2186 error = ravb_mdio_init(priv);
2187 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09002188 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002189 goto out_dma_free;
2190 }
2191
2192 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
2193 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
2194
2195 /* Network device register */
2196 error = register_netdev(ndev);
2197 if (error)
2198 goto out_napi_del;
2199
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002200 if (priv->clk)
2201 device_set_wakeup_capable(&pdev->dev, 1);
2202
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002203 /* Print device information */
2204 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2205 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2206
2207 platform_set_drvdata(pdev, ndev);
2208
2209 return 0;
2210
2211out_napi_del:
2212 netif_napi_del(&priv->napi[RAVB_NC]);
2213 netif_napi_del(&priv->napi[RAVB_BE]);
2214 ravb_mdio_release(priv);
2215out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002216 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002217 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002218
2219 /* Stop PTP Clock driver */
2220 if (chip_id != RCAR_GEN2)
2221 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002222out_release:
2223 if (ndev)
2224 free_netdev(ndev);
2225
2226 pm_runtime_put(&pdev->dev);
2227 pm_runtime_disable(&pdev->dev);
2228 return error;
2229}
2230
2231static int ravb_remove(struct platform_device *pdev)
2232{
2233 struct net_device *ndev = platform_get_drvdata(pdev);
2234 struct ravb_private *priv = netdev_priv(ndev);
2235
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002236 /* Stop PTP Clock driver */
2237 if (priv->chip_id != RCAR_GEN2)
2238 ravb_ptp_stop(ndev);
2239
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002240 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002241 priv->desc_bat_dma);
2242 /* Set reset mode */
2243 ravb_write(ndev, CCC_OPC_RESET, CCC);
2244 pm_runtime_put_sync(&pdev->dev);
2245 unregister_netdev(ndev);
2246 netif_napi_del(&priv->napi[RAVB_NC]);
2247 netif_napi_del(&priv->napi[RAVB_BE]);
2248 ravb_mdio_release(priv);
2249 pm_runtime_disable(&pdev->dev);
2250 free_netdev(ndev);
2251 platform_set_drvdata(pdev, NULL);
2252
2253 return 0;
2254}
2255
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002256static int ravb_wol_setup(struct net_device *ndev)
2257{
2258 struct ravb_private *priv = netdev_priv(ndev);
2259
2260 /* Disable interrupts by clearing the interrupt masks. */
2261 ravb_write(ndev, 0, RIC0);
2262 ravb_write(ndev, 0, RIC2);
2263 ravb_write(ndev, 0, TIC);
2264
2265 /* Only allow ECI interrupts */
2266 synchronize_irq(priv->emac_irq);
2267 napi_disable(&priv->napi[RAVB_NC]);
2268 napi_disable(&priv->napi[RAVB_BE]);
2269 ravb_write(ndev, ECSIPR_MPDIP, ECSIPR);
2270
2271 /* Enable MagicPacket */
2272 ravb_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
2273
2274 /* Increased clock usage so device won't be suspended */
2275 clk_enable(priv->clk);
2276
2277 return enable_irq_wake(priv->emac_irq);
2278}
2279
2280static int ravb_wol_restore(struct net_device *ndev)
2281{
2282 struct ravb_private *priv = netdev_priv(ndev);
2283 int ret;
2284
2285 napi_enable(&priv->napi[RAVB_NC]);
2286 napi_enable(&priv->napi[RAVB_BE]);
2287
2288 /* Disable MagicPacket */
2289 ravb_modify(ndev, ECMR, ECMR_MPDE, 0);
2290
2291 ret = ravb_close(ndev);
2292 if (ret < 0)
2293 return ret;
2294
2295 /* Restore clock usage count */
2296 clk_disable(priv->clk);
2297
2298 return disable_irq_wake(priv->emac_irq);
2299}
2300
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002301static int __maybe_unused ravb_suspend(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002302{
2303 struct net_device *ndev = dev_get_drvdata(dev);
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002304 struct ravb_private *priv = netdev_priv(ndev);
2305 int ret;
Niklas Söderlund01841652016-08-03 15:56:47 +02002306
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002307 if (!netif_running(ndev))
2308 return 0;
2309
2310 netif_device_detach(ndev);
2311
2312 if (priv->wol_enabled)
2313 ret = ravb_wol_setup(ndev);
2314 else
Niklas Söderlund01841652016-08-03 15:56:47 +02002315 ret = ravb_close(ndev);
Niklas Söderlund01841652016-08-03 15:56:47 +02002316
2317 return ret;
2318}
2319
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002320static int __maybe_unused ravb_resume(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002321{
2322 struct net_device *ndev = dev_get_drvdata(dev);
2323 struct ravb_private *priv = netdev_priv(ndev);
2324 int ret = 0;
2325
Niklas Söderlundfbf3d032017-08-01 12:14:37 +02002326 if (priv->wol_enabled) {
2327 /* Reduce the usecount of the clock to zero and then
2328 * restore it to its original value. This is done to force
2329 * the clock to be re-enabled which is a workaround
2330 * for renesas-cpg-mssr driver which do not enable clocks
2331 * when resuming from PSCI suspend/resume.
2332 *
2333 * Without this workaround the driver fails to communicate
2334 * with the hardware if WoL was enabled when the system
2335 * entered PSCI suspend. This is due to that if WoL is enabled
2336 * we explicitly keep the clock from being turned off when
2337 * suspending, but in PSCI sleep power is cut so the clock
2338 * is disabled anyhow, the clock driver is not aware of this
2339 * so the clock is not turned back on when resuming.
2340 *
2341 * TODO: once the renesas-cpg-mssr suspend/resume is working
2342 * this clock dance should be removed.
2343 */
2344 clk_disable(priv->clk);
2345 clk_disable(priv->clk);
2346 clk_enable(priv->clk);
2347 clk_enable(priv->clk);
2348
2349 /* Set reset mode to rearm the WoL logic */
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002350 ravb_write(ndev, CCC_OPC_RESET, CCC);
Niklas Söderlundfbf3d032017-08-01 12:14:37 +02002351 }
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002352
Niklas Söderlund01841652016-08-03 15:56:47 +02002353 /* All register have been reset to default values.
2354 * Restore all registers which where setup at probe time and
2355 * reopen device if it was running before system suspended.
2356 */
2357
2358 /* Set AVB config mode */
2359 ravb_set_config_mode(ndev);
2360
2361 /* Set GTI value */
2362 ret = ravb_set_gti(ndev);
2363 if (ret)
2364 return ret;
2365
2366 /* Request GTI loading */
2367 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
2368
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002369 if (priv->chip_id != RCAR_GEN2)
2370 ravb_set_delay_mode(ndev);
2371
Niklas Söderlund01841652016-08-03 15:56:47 +02002372 /* Restore descriptor base address table */
2373 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2374
2375 if (netif_running(ndev)) {
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002376 if (priv->wol_enabled) {
2377 ret = ravb_wol_restore(ndev);
2378 if (ret)
2379 return ret;
2380 }
Niklas Söderlund01841652016-08-03 15:56:47 +02002381 ret = ravb_open(ndev);
2382 if (ret < 0)
2383 return ret;
2384 netif_device_attach(ndev);
2385 }
2386
2387 return ret;
2388}
2389
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002390static int __maybe_unused ravb_runtime_nop(struct device *dev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002391{
2392 /* Runtime PM callback shared between ->runtime_suspend()
2393 * and ->runtime_resume(). Simply returns success.
2394 *
2395 * This driver re-initializes all registers after
2396 * pm_runtime_get_sync() anyway so there is no need
2397 * to save and restore registers here.
2398 */
2399 return 0;
2400}
2401
2402static const struct dev_pm_ops ravb_dev_pm_ops = {
Niklas Söderlundb89b8152016-08-10 13:09:49 +02002403 SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume)
Kazuya Mizuguchi524c6f62016-05-30 05:25:43 +09002404 SET_RUNTIME_PM_OPS(ravb_runtime_nop, ravb_runtime_nop, NULL)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002405};
2406
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002407static struct platform_driver ravb_driver = {
2408 .probe = ravb_probe,
2409 .remove = ravb_remove,
2410 .driver = {
2411 .name = "ravb",
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002412 .pm = &ravb_dev_pm_ops,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002413 .of_match_table = ravb_match_table,
2414 },
2415};
2416
2417module_platform_driver(ravb_driver);
2418
2419MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2420MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2421MODULE_LICENSE("GPL v2");