blob: 009780df664b4a09ea3e88855b0e707ff0b24b10 [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
Geert Uytterhoevenab104612017-10-12 10:24:53 +02001357 wol->supported = WAKE_MAGIC;
1358 wol->wolopts = priv->wol_enabled ? WAKE_MAGIC : 0;
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02001359}
1360
1361static int ravb_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1362{
1363 struct ravb_private *priv = netdev_priv(ndev);
1364
Geert Uytterhoevenab104612017-10-12 10:24:53 +02001365 if (wol->wolopts & ~WAKE_MAGIC)
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02001366 return -EOPNOTSUPP;
1367
1368 priv->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
1369
1370 device_set_wakeup_enable(&priv->pdev->dev, priv->wol_enabled);
1371
1372 return 0;
1373}
1374
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001375static const struct ethtool_ops ravb_ethtool_ops = {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001376 .nway_reset = ravb_nway_reset,
1377 .get_msglevel = ravb_get_msglevel,
1378 .set_msglevel = ravb_set_msglevel,
1379 .get_link = ethtool_op_get_link,
1380 .get_strings = ravb_get_strings,
1381 .get_ethtool_stats = ravb_get_ethtool_stats,
1382 .get_sset_count = ravb_get_sset_count,
1383 .get_ringparam = ravb_get_ringparam,
1384 .set_ringparam = ravb_set_ringparam,
1385 .get_ts_info = ravb_get_ts_info,
Philippe Reynes04462f22016-08-20 00:52:19 +02001386 .get_link_ksettings = ravb_get_link_ksettings,
1387 .set_link_ksettings = ravb_set_link_ksettings,
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02001388 .get_wol = ravb_get_wol,
1389 .set_wol = ravb_set_wol,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001390};
1391
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001392static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1393 struct net_device *ndev, struct device *dev,
1394 const char *ch)
1395{
1396 char *name;
1397 int error;
1398
1399 name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1400 if (!name)
1401 return -ENOMEM;
1402 error = request_irq(irq, handler, 0, name, ndev);
1403 if (error)
1404 netdev_err(ndev, "cannot request IRQ %s\n", name);
1405
1406 return error;
1407}
1408
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001409/* Network device open function for Ethernet AVB */
1410static int ravb_open(struct net_device *ndev)
1411{
1412 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001413 struct platform_device *pdev = priv->pdev;
1414 struct device *dev = &pdev->dev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001415 int error;
1416
1417 napi_enable(&priv->napi[RAVB_BE]);
1418 napi_enable(&priv->napi[RAVB_NC]);
1419
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001420 if (priv->chip_id == RCAR_GEN2) {
1421 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1422 ndev->name, ndev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001423 if (error) {
1424 netdev_err(ndev, "cannot request IRQ\n");
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001425 goto out_napi_off;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001426 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001427 } else {
1428 error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1429 dev, "ch22:multi");
1430 if (error)
1431 goto out_napi_off;
1432 error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1433 dev, "ch24:emac");
1434 if (error)
1435 goto out_free_irq;
1436 error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1437 ndev, dev, "ch0:rx_be");
1438 if (error)
1439 goto out_free_irq_emac;
1440 error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1441 ndev, dev, "ch18:tx_be");
1442 if (error)
1443 goto out_free_irq_be_rx;
1444 error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1445 ndev, dev, "ch1:rx_nc");
1446 if (error)
1447 goto out_free_irq_be_tx;
1448 error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1449 ndev, dev, "ch19:tx_nc");
1450 if (error)
1451 goto out_free_irq_nc_rx;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001452 }
1453
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001454 /* Device init */
1455 error = ravb_dmac_init(ndev);
1456 if (error)
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001457 goto out_free_irq_nc_tx;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001458 ravb_emac_init(ndev);
1459
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001460 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001461 if (priv->chip_id == RCAR_GEN2)
1462 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001463
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001464 netif_tx_start_all_queues(ndev);
1465
1466 /* PHY control start */
1467 error = ravb_phy_start(ndev);
1468 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001469 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001470
1471 return 0;
1472
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001473out_ptp_stop:
1474 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001475 if (priv->chip_id == RCAR_GEN2)
1476 ravb_ptp_stop(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001477out_free_irq_nc_tx:
1478 if (priv->chip_id == RCAR_GEN2)
1479 goto out_free_irq;
1480 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1481out_free_irq_nc_rx:
1482 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1483out_free_irq_be_tx:
1484 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1485out_free_irq_be_rx:
1486 free_irq(priv->rx_irqs[RAVB_BE], ndev);
1487out_free_irq_emac:
1488 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001489out_free_irq:
1490 free_irq(ndev->irq, ndev);
1491out_napi_off:
1492 napi_disable(&priv->napi[RAVB_NC]);
1493 napi_disable(&priv->napi[RAVB_BE]);
1494 return error;
1495}
1496
1497/* Timeout function for Ethernet AVB */
1498static void ravb_tx_timeout(struct net_device *ndev)
1499{
1500 struct ravb_private *priv = netdev_priv(ndev);
1501
1502 netif_err(priv, tx_err, ndev,
1503 "transmit timed out, status %08x, resetting...\n",
1504 ravb_read(ndev, ISS));
1505
1506 /* tx_errors count up */
1507 ndev->stats.tx_errors++;
1508
1509 schedule_work(&priv->work);
1510}
1511
1512static void ravb_tx_timeout_work(struct work_struct *work)
1513{
1514 struct ravb_private *priv = container_of(work, struct ravb_private,
1515 work);
1516 struct net_device *ndev = priv->ndev;
1517
1518 netif_tx_stop_all_queues(ndev);
1519
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001520 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001521 if (priv->chip_id == RCAR_GEN2)
1522 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001523
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001524 /* Wait for DMA stopping */
1525 ravb_stop_dma(ndev);
1526
1527 ravb_ring_free(ndev, RAVB_BE);
1528 ravb_ring_free(ndev, RAVB_NC);
1529
1530 /* Device init */
1531 ravb_dmac_init(ndev);
1532 ravb_emac_init(ndev);
1533
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001534 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001535 if (priv->chip_id == RCAR_GEN2)
1536 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001537
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001538 netif_tx_start_all_queues(ndev);
1539}
1540
1541/* Packet transmit function for Ethernet AVB */
1542static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1543{
1544 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001545 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001546 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001547 struct ravb_tx_desc *desc;
1548 unsigned long flags;
1549 u32 dma_addr;
1550 void *buffer;
1551 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001552 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001553
1554 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001555 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1556 NUM_TX_DESC) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001557 netif_err(priv, tx_queued, ndev,
1558 "still transmitting with the full ring!\n");
1559 netif_stop_subqueue(ndev, q);
1560 spin_unlock_irqrestore(&priv->lock, flags);
1561 return NETDEV_TX_BUSY;
1562 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001563
1564 if (skb_put_padto(skb, ETH_ZLEN))
Dan Carpenter9199cb72017-04-22 13:46:56 +03001565 goto exit;
1566
1567 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1568 priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001569
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001570 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1571 entry / NUM_TX_DESC * DPTR_ALIGN;
1572 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
Masaru Nagai8ec3e8a2017-01-16 11:45:21 +01001573 /* Zero length DMA descriptors are problematic as they seem to
1574 * terminate DMA transfers. Avoid them by simply using a length of
1575 * DPTR_ALIGN (4) when skb data is aligned to DPTR_ALIGN.
1576 *
1577 * As skb is guaranteed to have at least ETH_ZLEN (60) bytes of
1578 * data by the call to skb_put_padto() above this is safe with
1579 * respect to both the length of the first DMA descriptor (len)
1580 * overflowing the available data and the length of the second DMA
1581 * descriptor (skb->len - len) being negative.
1582 */
1583 if (len == 0)
1584 len = DPTR_ALIGN;
1585
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001586 memcpy(buffer, skb->data, len);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001587 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1588 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001589 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001590
1591 desc = &priv->tx_ring[q][entry];
1592 desc->ds_tagl = cpu_to_le16(len);
1593 desc->dptr = cpu_to_le32(dma_addr);
1594
1595 buffer = skb->data + len;
1596 len = skb->len - len;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001597 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1598 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001599 goto unmap;
1600
1601 desc++;
1602 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001603 desc->dptr = cpu_to_le32(dma_addr);
1604
1605 /* TX timestamp required */
1606 if (q == RAVB_NC) {
1607 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1608 if (!ts_skb) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001609 desc--;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001610 dma_unmap_single(ndev->dev.parent, dma_addr, len,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001611 DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001612 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001613 }
1614 ts_skb->skb = skb;
1615 ts_skb->tag = priv->ts_skb_tag++;
1616 priv->ts_skb_tag &= 0x3ff;
1617 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1618
1619 /* TAG and timestamp required flag */
1620 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001621 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1622 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1623 }
1624
Lino Sanfilippod7be81a2016-03-27 12:22:02 +02001625 skb_tx_timestamp(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001626 /* Descriptor type must be set after all the above writes */
1627 dma_wmb();
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001628 desc->die_dt = DT_FEND;
1629 desc--;
1630 desc->die_dt = DT_FSTART;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001631
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001632 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001633
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001634 priv->cur_tx[q] += NUM_TX_DESC;
1635 if (priv->cur_tx[q] - priv->dirty_tx[q] >
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +01001636 (priv->num_tx_ring[q] - 1) * NUM_TX_DESC &&
1637 !ravb_tx_free(ndev, q, true))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001638 netif_stop_subqueue(ndev, q);
1639
1640exit:
1641 mmiowb();
1642 spin_unlock_irqrestore(&priv->lock, flags);
1643 return NETDEV_TX_OK;
1644
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001645unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001646 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001647 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001648drop:
1649 dev_kfree_skb_any(skb);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001650 priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001651 goto exit;
1652}
1653
1654static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1655 void *accel_priv, select_queue_fallback_t fallback)
1656{
1657 /* If skb needs TX timestamp, it is handled in network control queue */
1658 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1659 RAVB_BE;
1660
1661}
1662
1663static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1664{
1665 struct ravb_private *priv = netdev_priv(ndev);
1666 struct net_device_stats *nstats, *stats0, *stats1;
1667
1668 nstats = &ndev->stats;
1669 stats0 = &priv->stats[RAVB_BE];
1670 stats1 = &priv->stats[RAVB_NC];
1671
1672 nstats->tx_dropped += ravb_read(ndev, TROCR);
1673 ravb_write(ndev, 0, TROCR); /* (write clear) */
1674 nstats->collisions += ravb_read(ndev, CDCR);
1675 ravb_write(ndev, 0, CDCR); /* (write clear) */
1676 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1677 ravb_write(ndev, 0, LCCR); /* (write clear) */
1678
1679 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1680 ravb_write(ndev, 0, CERCR); /* (write clear) */
1681 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1682 ravb_write(ndev, 0, CEECR); /* (write clear) */
1683
1684 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1685 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1686 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1687 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1688 nstats->multicast = stats0->multicast + stats1->multicast;
1689 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1690 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1691 nstats->rx_frame_errors =
1692 stats0->rx_frame_errors + stats1->rx_frame_errors;
1693 nstats->rx_length_errors =
1694 stats0->rx_length_errors + stats1->rx_length_errors;
1695 nstats->rx_missed_errors =
1696 stats0->rx_missed_errors + stats1->rx_missed_errors;
1697 nstats->rx_over_errors =
1698 stats0->rx_over_errors + stats1->rx_over_errors;
1699
1700 return nstats;
1701}
1702
1703/* Update promiscuous bit */
1704static void ravb_set_rx_mode(struct net_device *ndev)
1705{
1706 struct ravb_private *priv = netdev_priv(ndev);
1707 unsigned long flags;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001708
1709 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001710 ravb_modify(ndev, ECMR, ECMR_PRM,
1711 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001712 mmiowb();
1713 spin_unlock_irqrestore(&priv->lock, flags);
1714}
1715
1716/* Device close function for Ethernet AVB */
1717static int ravb_close(struct net_device *ndev)
1718{
Johan Hovold9f70eb32016-11-28 19:25:06 +01001719 struct device_node *np = ndev->dev.parent->of_node;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001720 struct ravb_private *priv = netdev_priv(ndev);
1721 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1722
1723 netif_tx_stop_all_queues(ndev);
1724
1725 /* Disable interrupts by clearing the interrupt masks. */
1726 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001727 ravb_write(ndev, 0, RIC2);
1728 ravb_write(ndev, 0, TIC);
1729
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001730 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001731 if (priv->chip_id == RCAR_GEN2)
1732 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001733
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001734 /* Set the config mode to stop the AVB-DMAC's processes */
1735 if (ravb_stop_dma(ndev) < 0)
1736 netdev_err(ndev,
1737 "device will be stopped after h/w processes are done.\n");
1738
1739 /* Clear the timestamp list */
1740 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1741 list_del(&ts_skb->list);
1742 kfree(ts_skb);
1743 }
1744
1745 /* PHY disconnect */
Philippe Reynes0f635172016-08-20 00:52:18 +02001746 if (ndev->phydev) {
1747 phy_stop(ndev->phydev);
1748 phy_disconnect(ndev->phydev);
Johan Hovold9f70eb32016-11-28 19:25:06 +01001749 if (of_phy_is_fixed_link(np))
1750 of_phy_deregister_fixed_link(np);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001751 }
1752
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001753 if (priv->chip_id != RCAR_GEN2) {
1754 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1755 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1756 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1757 free_irq(priv->rx_irqs[RAVB_BE], ndev);
Geert Uytterhoeven7fa816b2016-05-07 13:17:11 +02001758 free_irq(priv->emac_irq, ndev);
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001759 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001760 free_irq(ndev->irq, ndev);
1761
1762 napi_disable(&priv->napi[RAVB_NC]);
1763 napi_disable(&priv->napi[RAVB_BE]);
1764
1765 /* Free all the skb's in the RX queue and the DMA buffers. */
1766 ravb_ring_free(ndev, RAVB_BE);
1767 ravb_ring_free(ndev, RAVB_NC);
1768
1769 return 0;
1770}
1771
1772static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1773{
1774 struct ravb_private *priv = netdev_priv(ndev);
1775 struct hwtstamp_config config;
1776
1777 config.flags = 0;
1778 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1779 HWTSTAMP_TX_OFF;
1780 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1781 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1782 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1783 config.rx_filter = HWTSTAMP_FILTER_ALL;
1784 else
1785 config.rx_filter = HWTSTAMP_FILTER_NONE;
1786
1787 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1788 -EFAULT : 0;
1789}
1790
1791/* Control hardware time stamping */
1792static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1793{
1794 struct ravb_private *priv = netdev_priv(ndev);
1795 struct hwtstamp_config config;
1796 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1797 u32 tstamp_tx_ctrl;
1798
1799 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1800 return -EFAULT;
1801
1802 /* Reserved for future extensions */
1803 if (config.flags)
1804 return -EINVAL;
1805
1806 switch (config.tx_type) {
1807 case HWTSTAMP_TX_OFF:
1808 tstamp_tx_ctrl = 0;
1809 break;
1810 case HWTSTAMP_TX_ON:
1811 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1812 break;
1813 default:
1814 return -ERANGE;
1815 }
1816
1817 switch (config.rx_filter) {
1818 case HWTSTAMP_FILTER_NONE:
1819 tstamp_rx_ctrl = 0;
1820 break;
1821 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1822 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1823 break;
1824 default:
1825 config.rx_filter = HWTSTAMP_FILTER_ALL;
1826 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1827 }
1828
1829 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1830 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1831
1832 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1833 -EFAULT : 0;
1834}
1835
1836/* ioctl to device function */
1837static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1838{
Philippe Reynes0f635172016-08-20 00:52:18 +02001839 struct phy_device *phydev = ndev->phydev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001840
1841 if (!netif_running(ndev))
1842 return -EINVAL;
1843
1844 if (!phydev)
1845 return -ENODEV;
1846
1847 switch (cmd) {
1848 case SIOCGHWTSTAMP:
1849 return ravb_hwtstamp_get(ndev, req);
1850 case SIOCSHWTSTAMP:
1851 return ravb_hwtstamp_set(ndev, req);
1852 }
1853
1854 return phy_mii_ioctl(phydev, req, cmd);
1855}
1856
Simon Horman4d86d382017-10-04 09:54:27 +02001857static void ravb_set_rx_csum(struct net_device *ndev, bool enable)
1858{
1859 struct ravb_private *priv = netdev_priv(ndev);
1860 unsigned long flags;
1861
1862 spin_lock_irqsave(&priv->lock, flags);
1863
1864 /* Disable TX and RX */
1865 ravb_rcv_snd_disable(ndev);
1866
1867 /* Modify RX Checksum setting */
1868 ravb_modify(ndev, ECMR, ECMR_RCSC, enable ? ECMR_RCSC : 0);
1869
1870 /* Enable TX and RX */
1871 ravb_rcv_snd_enable(ndev);
1872
1873 spin_unlock_irqrestore(&priv->lock, flags);
1874}
1875
1876static int ravb_set_features(struct net_device *ndev,
1877 netdev_features_t features)
1878{
1879 netdev_features_t changed = ndev->features ^ features;
1880
1881 if (changed & NETIF_F_RXCSUM)
1882 ravb_set_rx_csum(ndev, features & NETIF_F_RXCSUM);
1883
1884 ndev->features = features;
1885
1886 return 0;
1887}
1888
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001889static const struct net_device_ops ravb_netdev_ops = {
1890 .ndo_open = ravb_open,
1891 .ndo_stop = ravb_close,
1892 .ndo_start_xmit = ravb_start_xmit,
1893 .ndo_select_queue = ravb_select_queue,
1894 .ndo_get_stats = ravb_get_stats,
1895 .ndo_set_rx_mode = ravb_set_rx_mode,
1896 .ndo_tx_timeout = ravb_tx_timeout,
1897 .ndo_do_ioctl = ravb_do_ioctl,
1898 .ndo_validate_addr = eth_validate_addr,
1899 .ndo_set_mac_address = eth_mac_addr,
Simon Horman4d86d382017-10-04 09:54:27 +02001900 .ndo_set_features = ravb_set_features,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001901};
1902
1903/* MDIO bus init function */
1904static int ravb_mdio_init(struct ravb_private *priv)
1905{
1906 struct platform_device *pdev = priv->pdev;
1907 struct device *dev = &pdev->dev;
1908 int error;
1909
1910 /* Bitbang init */
1911 priv->mdiobb.ops = &bb_ops;
1912
1913 /* MII controller setting */
1914 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1915 if (!priv->mii_bus)
1916 return -ENOMEM;
1917
1918 /* Hook up MII support for ethtool */
1919 priv->mii_bus->name = "ravb_mii";
1920 priv->mii_bus->parent = dev;
1921 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1922 pdev->name, pdev->id);
1923
1924 /* Register MDIO bus */
1925 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1926 if (error)
1927 goto out_free_bus;
1928
1929 return 0;
1930
1931out_free_bus:
1932 free_mdio_bitbang(priv->mii_bus);
1933 return error;
1934}
1935
1936/* MDIO bus release function */
1937static int ravb_mdio_release(struct ravb_private *priv)
1938{
1939 /* Unregister mdio bus */
1940 mdiobus_unregister(priv->mii_bus);
1941
1942 /* Free bitbang info */
1943 free_mdio_bitbang(priv->mii_bus);
1944
1945 return 0;
1946}
1947
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001948static const struct of_device_id ravb_match_table[] = {
1949 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1950 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001951 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001952 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001953 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001954 { }
1955};
1956MODULE_DEVICE_TABLE(of, ravb_match_table);
1957
Simon Hormanb3d39a82015-11-20 11:29:39 -08001958static int ravb_set_gti(struct net_device *ndev)
1959{
Geert Uytterhoevenab104612017-10-12 10:24:53 +02001960 struct ravb_private *priv = netdev_priv(ndev);
Simon Hormanb3d39a82015-11-20 11:29:39 -08001961 struct device *dev = ndev->dev.parent;
Simon Hormanb3d39a82015-11-20 11:29:39 -08001962 unsigned long rate;
Simon Hormanb3d39a82015-11-20 11:29:39 -08001963 uint64_t inc;
1964
Geert Uytterhoevenab104612017-10-12 10:24:53 +02001965 rate = clk_get_rate(priv->clk);
Wolfram Sanga6d37132016-04-08 13:28:42 +02001966 if (!rate)
1967 return -EINVAL;
1968
Simon Hormanb3d39a82015-11-20 11:29:39 -08001969 inc = 1000000000ULL << 20;
1970 do_div(inc, rate);
1971
1972 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1973 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1974 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1975 return -EINVAL;
1976 }
1977
1978 ravb_write(ndev, inc, GTI);
1979
1980 return 0;
1981}
1982
Niklas Söderlund01841652016-08-03 15:56:47 +02001983static void ravb_set_config_mode(struct net_device *ndev)
1984{
1985 struct ravb_private *priv = netdev_priv(ndev);
1986
1987 if (priv->chip_id == RCAR_GEN2) {
1988 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
1989 /* Set CSEL value */
1990 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
1991 } else {
1992 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
1993 CCC_GAC | CCC_CSEL_HPB);
1994 }
1995}
1996
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01001997/* Set tx and rx clock internal delay modes */
1998static void ravb_set_delay_mode(struct net_device *ndev)
1999{
2000 struct ravb_private *priv = netdev_priv(ndev);
2001 int set = 0;
2002
2003 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
2004 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID)
2005 set |= APSR_DM_RDM;
2006
2007 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
2008 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
2009 set |= APSR_DM_TDM;
2010
2011 ravb_modify(ndev, APSR, APSR_DM, set);
2012}
2013
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002014static int ravb_probe(struct platform_device *pdev)
2015{
2016 struct device_node *np = pdev->dev.of_node;
2017 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002018 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002019 struct net_device *ndev;
2020 int error, irq, q;
2021 struct resource *res;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09002022 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002023
2024 if (!np) {
2025 dev_err(&pdev->dev,
2026 "this driver is required to be instantiated from device tree\n");
2027 return -EINVAL;
2028 }
2029
2030 /* Get base address */
2031 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2032 if (!res) {
2033 dev_err(&pdev->dev, "invalid resource\n");
2034 return -EINVAL;
2035 }
2036
2037 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
2038 NUM_TX_QUEUE, NUM_RX_QUEUE);
2039 if (!ndev)
2040 return -ENOMEM;
2041
Simon Horman4d86d382017-10-04 09:54:27 +02002042 ndev->features = NETIF_F_RXCSUM;
2043 ndev->hw_features = NETIF_F_RXCSUM;
2044
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002045 pm_runtime_enable(&pdev->dev);
2046 pm_runtime_get_sync(&pdev->dev);
2047
2048 /* The Ether-specific entries in the device structure. */
2049 ndev->base_addr = res->start;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002050
Wolfram Sange8668632016-03-01 17:37:58 +01002051 chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002052
2053 if (chip_id == RCAR_GEN3)
2054 irq = platform_get_irq_byname(pdev, "ch22");
2055 else
2056 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002057 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03002058 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002059 goto out_release;
2060 }
2061 ndev->irq = irq;
2062
2063 SET_NETDEV_DEV(ndev, &pdev->dev);
2064
2065 priv = netdev_priv(ndev);
2066 priv->ndev = ndev;
2067 priv->pdev = pdev;
2068 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
2069 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
2070 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
2071 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
2072 priv->addr = devm_ioremap_resource(&pdev->dev, res);
2073 if (IS_ERR(priv->addr)) {
2074 error = PTR_ERR(priv->addr);
2075 goto out_release;
2076 }
2077
2078 spin_lock_init(&priv->lock);
2079 INIT_WORK(&priv->work, ravb_tx_timeout_work);
2080
2081 priv->phy_interface = of_get_phy_mode(np);
2082
2083 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
2084 priv->avb_link_active_low =
2085 of_property_read_bool(np, "renesas,ether-link-active-low");
2086
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002087 if (chip_id == RCAR_GEN3) {
2088 irq = platform_get_irq_byname(pdev, "ch24");
2089 if (irq < 0) {
2090 error = irq;
2091 goto out_release;
2092 }
2093 priv->emac_irq = irq;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09002094 for (i = 0; i < NUM_RX_QUEUE; i++) {
2095 irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
2096 if (irq < 0) {
2097 error = irq;
2098 goto out_release;
2099 }
2100 priv->rx_irqs[i] = irq;
2101 }
2102 for (i = 0; i < NUM_TX_QUEUE; i++) {
2103 irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
2104 if (irq < 0) {
2105 error = irq;
2106 goto out_release;
2107 }
2108 priv->tx_irqs[i] = irq;
2109 }
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002110 }
2111
2112 priv->chip_id = chip_id;
2113
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002114 priv->clk = devm_clk_get(&pdev->dev, NULL);
Geert Uytterhoevenab104612017-10-12 10:24:53 +02002115 if (IS_ERR(priv->clk)) {
2116 error = PTR_ERR(priv->clk);
2117 goto out_release;
2118 }
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002119
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002120 /* Set function */
2121 ndev->netdev_ops = &ravb_netdev_ops;
2122 ndev->ethtool_ops = &ravb_ethtool_ops;
2123
2124 /* Set AVB config mode */
Niklas Söderlund01841652016-08-03 15:56:47 +02002125 ravb_set_config_mode(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002126
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002127 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08002128 error = ravb_set_gti(ndev);
2129 if (error)
2130 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002131
2132 /* Request GTI loading */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03002133 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002134
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002135 if (priv->chip_id != RCAR_GEN2)
2136 ravb_set_delay_mode(ndev);
2137
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002138 /* Allocate descriptor base address table */
2139 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002140 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002141 &priv->desc_bat_dma, GFP_KERNEL);
2142 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09002143 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002144 "Cannot allocate desc base address table (size %d bytes)\n",
2145 priv->desc_bat_size);
2146 error = -ENOMEM;
2147 goto out_release;
2148 }
2149 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2150 priv->desc_bat[q].die_dt = DT_EOS;
2151 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2152
2153 /* Initialise HW timestamp list */
2154 INIT_LIST_HEAD(&priv->ts_skb_list);
2155
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002156 /* Initialise PTP Clock driver */
2157 if (chip_id != RCAR_GEN2)
2158 ravb_ptp_init(ndev, pdev);
2159
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002160 /* Debug message level */
2161 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2162
2163 /* Read and set MAC address */
2164 ravb_read_mac_address(ndev, of_get_mac_address(np));
2165 if (!is_valid_ether_addr(ndev->dev_addr)) {
2166 dev_warn(&pdev->dev,
2167 "no valid MAC address supplied, using a random one\n");
2168 eth_hw_addr_random(ndev);
2169 }
2170
2171 /* MDIO bus init */
2172 error = ravb_mdio_init(priv);
2173 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09002174 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002175 goto out_dma_free;
2176 }
2177
2178 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
2179 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
2180
2181 /* Network device register */
2182 error = register_netdev(ndev);
2183 if (error)
2184 goto out_napi_del;
2185
Geert Uytterhoevenab104612017-10-12 10:24:53 +02002186 device_set_wakeup_capable(&pdev->dev, 1);
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002187
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002188 /* Print device information */
2189 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2190 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2191
2192 platform_set_drvdata(pdev, ndev);
2193
2194 return 0;
2195
2196out_napi_del:
2197 netif_napi_del(&priv->napi[RAVB_NC]);
2198 netif_napi_del(&priv->napi[RAVB_BE]);
2199 ravb_mdio_release(priv);
2200out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002201 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002202 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002203
2204 /* Stop PTP Clock driver */
2205 if (chip_id != RCAR_GEN2)
2206 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002207out_release:
2208 if (ndev)
2209 free_netdev(ndev);
2210
2211 pm_runtime_put(&pdev->dev);
2212 pm_runtime_disable(&pdev->dev);
2213 return error;
2214}
2215
2216static int ravb_remove(struct platform_device *pdev)
2217{
2218 struct net_device *ndev = platform_get_drvdata(pdev);
2219 struct ravb_private *priv = netdev_priv(ndev);
2220
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002221 /* Stop PTP Clock driver */
2222 if (priv->chip_id != RCAR_GEN2)
2223 ravb_ptp_stop(ndev);
2224
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002225 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002226 priv->desc_bat_dma);
2227 /* Set reset mode */
2228 ravb_write(ndev, CCC_OPC_RESET, CCC);
2229 pm_runtime_put_sync(&pdev->dev);
2230 unregister_netdev(ndev);
2231 netif_napi_del(&priv->napi[RAVB_NC]);
2232 netif_napi_del(&priv->napi[RAVB_BE]);
2233 ravb_mdio_release(priv);
2234 pm_runtime_disable(&pdev->dev);
2235 free_netdev(ndev);
2236 platform_set_drvdata(pdev, NULL);
2237
2238 return 0;
2239}
2240
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002241static int ravb_wol_setup(struct net_device *ndev)
2242{
2243 struct ravb_private *priv = netdev_priv(ndev);
2244
2245 /* Disable interrupts by clearing the interrupt masks. */
2246 ravb_write(ndev, 0, RIC0);
2247 ravb_write(ndev, 0, RIC2);
2248 ravb_write(ndev, 0, TIC);
2249
2250 /* Only allow ECI interrupts */
2251 synchronize_irq(priv->emac_irq);
2252 napi_disable(&priv->napi[RAVB_NC]);
2253 napi_disable(&priv->napi[RAVB_BE]);
2254 ravb_write(ndev, ECSIPR_MPDIP, ECSIPR);
2255
2256 /* Enable MagicPacket */
2257 ravb_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
2258
2259 /* Increased clock usage so device won't be suspended */
2260 clk_enable(priv->clk);
2261
2262 return enable_irq_wake(priv->emac_irq);
2263}
2264
2265static int ravb_wol_restore(struct net_device *ndev)
2266{
2267 struct ravb_private *priv = netdev_priv(ndev);
2268 int ret;
2269
2270 napi_enable(&priv->napi[RAVB_NC]);
2271 napi_enable(&priv->napi[RAVB_BE]);
2272
2273 /* Disable MagicPacket */
2274 ravb_modify(ndev, ECMR, ECMR_MPDE, 0);
2275
2276 ret = ravb_close(ndev);
2277 if (ret < 0)
2278 return ret;
2279
2280 /* Restore clock usage count */
2281 clk_disable(priv->clk);
2282
2283 return disable_irq_wake(priv->emac_irq);
2284}
2285
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002286static int __maybe_unused ravb_suspend(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002287{
2288 struct net_device *ndev = dev_get_drvdata(dev);
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002289 struct ravb_private *priv = netdev_priv(ndev);
2290 int ret;
Niklas Söderlund01841652016-08-03 15:56:47 +02002291
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002292 if (!netif_running(ndev))
2293 return 0;
2294
2295 netif_device_detach(ndev);
2296
2297 if (priv->wol_enabled)
2298 ret = ravb_wol_setup(ndev);
2299 else
Niklas Söderlund01841652016-08-03 15:56:47 +02002300 ret = ravb_close(ndev);
Niklas Söderlund01841652016-08-03 15:56:47 +02002301
2302 return ret;
2303}
2304
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002305static int __maybe_unused ravb_resume(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002306{
2307 struct net_device *ndev = dev_get_drvdata(dev);
2308 struct ravb_private *priv = netdev_priv(ndev);
2309 int ret = 0;
2310
Geert Uytterhoeven6b782f42017-12-11 09:54:09 +01002311 /* If WoL is enabled set reset mode to rearm the WoL logic */
2312 if (priv->wol_enabled)
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002313 ravb_write(ndev, CCC_OPC_RESET, CCC);
2314
Niklas Söderlund01841652016-08-03 15:56:47 +02002315 /* All register have been reset to default values.
2316 * Restore all registers which where setup at probe time and
2317 * reopen device if it was running before system suspended.
2318 */
2319
2320 /* Set AVB config mode */
2321 ravb_set_config_mode(ndev);
2322
2323 /* Set GTI value */
2324 ret = ravb_set_gti(ndev);
2325 if (ret)
2326 return ret;
2327
2328 /* Request GTI loading */
2329 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
2330
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002331 if (priv->chip_id != RCAR_GEN2)
2332 ravb_set_delay_mode(ndev);
2333
Niklas Söderlund01841652016-08-03 15:56:47 +02002334 /* Restore descriptor base address table */
2335 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2336
2337 if (netif_running(ndev)) {
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002338 if (priv->wol_enabled) {
2339 ret = ravb_wol_restore(ndev);
2340 if (ret)
2341 return ret;
2342 }
Niklas Söderlund01841652016-08-03 15:56:47 +02002343 ret = ravb_open(ndev);
2344 if (ret < 0)
2345 return ret;
2346 netif_device_attach(ndev);
2347 }
2348
2349 return ret;
2350}
2351
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002352static int __maybe_unused ravb_runtime_nop(struct device *dev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002353{
2354 /* Runtime PM callback shared between ->runtime_suspend()
2355 * and ->runtime_resume(). Simply returns success.
2356 *
2357 * This driver re-initializes all registers after
2358 * pm_runtime_get_sync() anyway so there is no need
2359 * to save and restore registers here.
2360 */
2361 return 0;
2362}
2363
2364static const struct dev_pm_ops ravb_dev_pm_ops = {
Niklas Söderlundb89b8152016-08-10 13:09:49 +02002365 SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume)
Kazuya Mizuguchi524c6f62016-05-30 05:25:43 +09002366 SET_RUNTIME_PM_OPS(ravb_runtime_nop, ravb_runtime_nop, NULL)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002367};
2368
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002369static struct platform_driver ravb_driver = {
2370 .probe = ravb_probe,
2371 .remove = ravb_remove,
2372 .driver = {
2373 .name = "ravb",
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002374 .pm = &ravb_dev_pm_ops,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002375 .of_match_table = ravb_match_table,
2376 },
2377};
2378
2379module_platform_driver(ravb_driver);
2380
2381MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2382MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2383MODULE_LICENSE("GPL v2");