blob: 6d10db1b51468031b66320cae457811564a902b6 [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
406 /* PAUSE prohibition */
Sergei Shtylyov1c1fa822016-01-11 00:27:38 +0300407 ravb_write(ndev, ECMR_ZPF | (priv->duplex ? ECMR_DM : 0) |
408 ECMR_TE | ECMR_RE, ECMR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300409
410 ravb_set_rate(ndev);
411
412 /* Set MAC address */
413 ravb_write(ndev,
414 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
415 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
416 ravb_write(ndev,
417 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
418
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300419 /* E-MAC status register clear */
420 ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
421
422 /* E-MAC interrupt enable register */
423 ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
424}
425
426/* Device init function for Ethernet AVB */
427static int ravb_dmac_init(struct net_device *ndev)
428{
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900429 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300430 int error;
431
432 /* Set CONFIG mode */
433 error = ravb_config(ndev);
434 if (error)
435 return error;
436
437 error = ravb_ring_init(ndev, RAVB_BE);
438 if (error)
439 return error;
440 error = ravb_ring_init(ndev, RAVB_NC);
441 if (error) {
442 ravb_ring_free(ndev, RAVB_BE);
443 return error;
444 }
445
446 /* Descriptor format */
447 ravb_ring_format(ndev, RAVB_BE);
448 ravb_ring_format(ndev, RAVB_NC);
449
450#if defined(__LITTLE_ENDIAN)
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300451 ravb_modify(ndev, CCC, CCC_BOC, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300452#else
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300453 ravb_modify(ndev, CCC, CCC_BOC, CCC_BOC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300454#endif
455
456 /* Set AVB RX */
Masaru Nagai8d9c4182016-06-01 03:01:28 +0900457 ravb_write(ndev,
458 RCR_EFFS | RCR_ENCF | RCR_ETS0 | RCR_ESF | 0x18000000, RCR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300459
460 /* Set FIFO size */
461 ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00222200, TGC);
462
463 /* Timestamp enable */
464 ravb_write(ndev, TCCR_TFEN, TCCR);
465
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900466 /* Interrupt init: */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900467 if (priv->chip_id == RCAR_GEN3) {
468 /* Clear DIL.DPLx */
469 ravb_write(ndev, 0, DIL);
470 /* Set queue specific interrupt */
471 ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE);
472 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300473 /* Frame receive */
474 ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900475 /* Disable FIFO full warning */
476 ravb_write(ndev, 0, RIC1);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300477 /* Receive FIFO full error, descriptor empty */
478 ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
479 /* Frame transmitted, timestamp FIFO updated */
480 ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
481
482 /* Setting the control will start the AVB-DMAC process. */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300483 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300484
485 return 0;
486}
487
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300488static void ravb_get_tx_tstamp(struct net_device *ndev)
489{
490 struct ravb_private *priv = netdev_priv(ndev);
491 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
492 struct skb_shared_hwtstamps shhwtstamps;
493 struct sk_buff *skb;
494 struct timespec64 ts;
495 u16 tag, tfa_tag;
496 int count;
497 u32 tfa2;
498
499 count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
500 while (count--) {
501 tfa2 = ravb_read(ndev, TFA2);
502 tfa_tag = (tfa2 & TFA2_TST) >> 16;
503 ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
504 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
505 ravb_read(ndev, TFA1);
506 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
507 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
508 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
509 list) {
510 skb = ts_skb->skb;
511 tag = ts_skb->tag;
512 list_del(&ts_skb->list);
513 kfree(ts_skb);
514 if (tag == tfa_tag) {
515 skb_tstamp_tx(skb, &shhwtstamps);
516 break;
517 }
518 }
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300519 ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300520 }
521}
522
523/* Packet receive function for Ethernet AVB */
524static bool ravb_rx(struct net_device *ndev, int *quota, int q)
525{
526 struct ravb_private *priv = netdev_priv(ndev);
527 int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
528 int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
529 priv->cur_rx[q];
530 struct net_device_stats *stats = &priv->stats[q];
531 struct ravb_ex_rx_desc *desc;
532 struct sk_buff *skb;
533 dma_addr_t dma_addr;
534 struct timespec64 ts;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300535 u8 desc_status;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300536 u16 pkt_len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300537 int limit;
538
539 boguscnt = min(boguscnt, *quota);
540 limit = boguscnt;
541 desc = &priv->rx_ring[q][entry];
542 while (desc->die_dt != DT_FEMPTY) {
543 /* Descriptor type must be checked before all other reads */
544 dma_rmb();
545 desc_status = desc->msc;
546 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
547
548 if (--boguscnt < 0)
549 break;
550
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300551 /* We use 0-byte descriptors to mark the DMA mapping errors */
552 if (!pkt_len)
553 continue;
554
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300555 if (desc_status & MSC_MC)
556 stats->multicast++;
557
558 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
559 MSC_CEEF)) {
560 stats->rx_errors++;
561 if (desc_status & MSC_CRC)
562 stats->rx_crc_errors++;
563 if (desc_status & MSC_RFE)
564 stats->rx_frame_errors++;
565 if (desc_status & (MSC_RTLF | MSC_RTSF))
566 stats->rx_length_errors++;
567 if (desc_status & MSC_CEEF)
568 stats->rx_missed_errors++;
569 } else {
570 u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
571
572 skb = priv->rx_skb[q][entry];
573 priv->rx_skb[q][entry] = NULL;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900574 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900575 PKT_BUF_SZ,
Sergei Shtylyove2370f02015-07-15 00:56:52 +0300576 DMA_FROM_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300577 get_ts &= (q == RAVB_NC) ?
578 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
579 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
580 if (get_ts) {
581 struct skb_shared_hwtstamps *shhwtstamps;
582
583 shhwtstamps = skb_hwtstamps(skb);
584 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
585 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
586 32) | le32_to_cpu(desc->ts_sl);
587 ts.tv_nsec = le32_to_cpu(desc->ts_n);
588 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
589 }
590 skb_put(skb, pkt_len);
591 skb->protocol = eth_type_trans(skb, ndev);
592 napi_gro_receive(&priv->napi[q], skb);
593 stats->rx_packets++;
594 stats->rx_bytes += pkt_len;
595 }
596
597 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
598 desc = &priv->rx_ring[q][entry];
599 }
600
601 /* Refill the RX ring buffers. */
602 for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
603 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
604 desc = &priv->rx_ring[q][entry];
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900605 desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300606
607 if (!priv->rx_skb[q][entry]) {
608 skb = netdev_alloc_skb(ndev,
609 PKT_BUF_SZ + RAVB_ALIGN - 1);
610 if (!skb)
611 break; /* Better luck next round. */
612 ravb_set_buffer_align(skb);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900613 dma_addr = dma_map_single(ndev->dev.parent, skb->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300614 le16_to_cpu(desc->ds_cc),
615 DMA_FROM_DEVICE);
616 skb_checksum_none_assert(skb);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300617 /* We just set the data size to 0 for a failed mapping
618 * which should prevent DMA from happening...
619 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900620 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300621 desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300622 desc->dptr = cpu_to_le32(dma_addr);
623 priv->rx_skb[q][entry] = skb;
624 }
625 /* Descriptor type must be set after all the above writes */
626 dma_wmb();
627 desc->die_dt = DT_FEMPTY;
628 }
629
630 *quota -= limit - (++boguscnt);
631
632 return boguscnt <= 0;
633}
634
635static void ravb_rcv_snd_disable(struct net_device *ndev)
636{
637 /* Disable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300638 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300639}
640
641static void ravb_rcv_snd_enable(struct net_device *ndev)
642{
643 /* Enable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300644 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300645}
646
647/* function for waiting dma process finished */
648static int ravb_stop_dma(struct net_device *ndev)
649{
650 int error;
651
652 /* Wait for stopping the hardware TX process */
653 error = ravb_wait(ndev, TCCR,
654 TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
655 if (error)
656 return error;
657
658 error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
659 0);
660 if (error)
661 return error;
662
663 /* Stop the E-MAC's RX/TX processes. */
664 ravb_rcv_snd_disable(ndev);
665
666 /* Wait for stopping the RX DMA process */
667 error = ravb_wait(ndev, CSR, CSR_RPO, 0);
668 if (error)
669 return error;
670
671 /* Stop AVB-DMAC process */
672 return ravb_config(ndev);
673}
674
675/* E-MAC interrupt handler */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900676static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300677{
678 struct ravb_private *priv = netdev_priv(ndev);
679 u32 ecsr, psr;
680
681 ecsr = ravb_read(ndev, ECSR);
682 ravb_write(ndev, ecsr, ECSR); /* clear interrupt */
Niklas Söderlund3e3d6472017-08-01 12:14:36 +0200683
684 if (ecsr & ECSR_MPD)
685 pm_wakeup_event(&priv->pdev->dev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300686 if (ecsr & ECSR_ICD)
687 ndev->stats.tx_carrier_errors++;
688 if (ecsr & ECSR_LCHNG) {
689 /* Link changed */
690 if (priv->no_avb_link)
691 return;
692 psr = ravb_read(ndev, PSR);
693 if (priv->avb_link_active_low)
694 psr ^= PSR_LMON;
695 if (!(psr & PSR_LMON)) {
696 /* DIsable RX and TX */
697 ravb_rcv_snd_disable(ndev);
698 } else {
699 /* Enable RX and TX */
700 ravb_rcv_snd_enable(ndev);
701 }
702 }
703}
704
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900705static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
706{
707 struct net_device *ndev = dev_id;
708 struct ravb_private *priv = netdev_priv(ndev);
709
710 spin_lock(&priv->lock);
711 ravb_emac_interrupt_unlocked(ndev);
712 mmiowb();
713 spin_unlock(&priv->lock);
714 return IRQ_HANDLED;
715}
716
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300717/* Error interrupt handler */
718static void ravb_error_interrupt(struct net_device *ndev)
719{
720 struct ravb_private *priv = netdev_priv(ndev);
721 u32 eis, ris2;
722
723 eis = ravb_read(ndev, EIS);
724 ravb_write(ndev, ~EIS_QFS, EIS);
725 if (eis & EIS_QFS) {
726 ris2 = ravb_read(ndev, RIS2);
727 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF), RIS2);
728
729 /* Receive Descriptor Empty int */
730 if (ris2 & RIS2_QFF0)
731 priv->stats[RAVB_BE].rx_over_errors++;
732
733 /* Receive Descriptor Empty int */
734 if (ris2 & RIS2_QFF1)
735 priv->stats[RAVB_NC].rx_over_errors++;
736
737 /* Receive FIFO Overflow int */
738 if (ris2 & RIS2_RFFF)
739 priv->rx_fifo_errors++;
740 }
741}
742
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900743static bool ravb_queue_interrupt(struct net_device *ndev, int q)
744{
745 struct ravb_private *priv = netdev_priv(ndev);
746 u32 ris0 = ravb_read(ndev, RIS0);
747 u32 ric0 = ravb_read(ndev, RIC0);
748 u32 tis = ravb_read(ndev, TIS);
749 u32 tic = ravb_read(ndev, TIC);
750
751 if (((ris0 & ric0) & BIT(q)) || ((tis & tic) & BIT(q))) {
752 if (napi_schedule_prep(&priv->napi[q])) {
753 /* Mask RX and TX interrupts */
754 if (priv->chip_id == RCAR_GEN2) {
755 ravb_write(ndev, ric0 & ~BIT(q), RIC0);
756 ravb_write(ndev, tic & ~BIT(q), TIC);
757 } else {
758 ravb_write(ndev, BIT(q), RID0);
759 ravb_write(ndev, BIT(q), TID);
760 }
761 __napi_schedule(&priv->napi[q]);
762 } else {
763 netdev_warn(ndev,
764 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
765 ris0, ric0);
766 netdev_warn(ndev,
767 " tx status 0x%08x, tx mask 0x%08x.\n",
768 tis, tic);
769 }
770 return true;
771 }
772 return false;
773}
774
775static bool ravb_timestamp_interrupt(struct net_device *ndev)
776{
777 u32 tis = ravb_read(ndev, TIS);
778
779 if (tis & TIS_TFUF) {
780 ravb_write(ndev, ~TIS_TFUF, TIS);
781 ravb_get_tx_tstamp(ndev);
782 return true;
783 }
784 return false;
785}
786
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300787static irqreturn_t ravb_interrupt(int irq, void *dev_id)
788{
789 struct net_device *ndev = dev_id;
790 struct ravb_private *priv = netdev_priv(ndev);
791 irqreturn_t result = IRQ_NONE;
792 u32 iss;
793
794 spin_lock(&priv->lock);
795 /* Get interrupt status */
796 iss = ravb_read(ndev, ISS);
797
798 /* Received and transmitted interrupts */
799 if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300800 int q;
801
802 /* Timestamp updated */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900803 if (ravb_timestamp_interrupt(ndev))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300804 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300805
806 /* Network control and best effort queue RX/TX */
807 for (q = RAVB_NC; q >= RAVB_BE; q--) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900808 if (ravb_queue_interrupt(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300809 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300810 }
811 }
812
813 /* E-MAC status summary */
814 if (iss & ISS_MS) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900815 ravb_emac_interrupt_unlocked(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300816 result = IRQ_HANDLED;
817 }
818
819 /* Error status summary */
820 if (iss & ISS_ES) {
821 ravb_error_interrupt(ndev);
822 result = IRQ_HANDLED;
823 }
824
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900825 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300826 if (iss & ISS_CGIS) {
827 ravb_ptp_interrupt(ndev);
Yoshihiro Kaneko38c848c2016-03-16 00:52:16 +0900828 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300829 }
Sergei Shtylyova0d2f202015-06-11 01:02:30 +0300830
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300831 mmiowb();
832 spin_unlock(&priv->lock);
833 return result;
834}
835
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900836/* Timestamp/Error/gPTP interrupt handler */
837static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
838{
839 struct net_device *ndev = dev_id;
840 struct ravb_private *priv = netdev_priv(ndev);
841 irqreturn_t result = IRQ_NONE;
842 u32 iss;
843
844 spin_lock(&priv->lock);
845 /* Get interrupt status */
846 iss = ravb_read(ndev, ISS);
847
848 /* Timestamp updated */
849 if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
850 result = IRQ_HANDLED;
851
852 /* Error status summary */
853 if (iss & ISS_ES) {
854 ravb_error_interrupt(ndev);
855 result = IRQ_HANDLED;
856 }
857
858 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300859 if (iss & ISS_CGIS) {
860 ravb_ptp_interrupt(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900861 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300862 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900863
864 mmiowb();
865 spin_unlock(&priv->lock);
866 return result;
867}
868
869static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
870{
871 struct net_device *ndev = dev_id;
872 struct ravb_private *priv = netdev_priv(ndev);
873 irqreturn_t result = IRQ_NONE;
874
875 spin_lock(&priv->lock);
876
877 /* Network control/Best effort queue RX/TX */
878 if (ravb_queue_interrupt(ndev, q))
879 result = IRQ_HANDLED;
880
881 mmiowb();
882 spin_unlock(&priv->lock);
883 return result;
884}
885
886static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
887{
888 return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
889}
890
891static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
892{
893 return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
894}
895
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300896static int ravb_poll(struct napi_struct *napi, int budget)
897{
898 struct net_device *ndev = napi->dev;
899 struct ravb_private *priv = netdev_priv(ndev);
900 unsigned long flags;
901 int q = napi - priv->napi;
902 int mask = BIT(q);
903 int quota = budget;
904 u32 ris0, tis;
905
906 for (;;) {
907 tis = ravb_read(ndev, TIS);
908 ris0 = ravb_read(ndev, RIS0);
909 if (!((ris0 & mask) || (tis & mask)))
910 break;
911
912 /* Processing RX Descriptor Ring */
913 if (ris0 & mask) {
914 /* Clear RX interrupt */
915 ravb_write(ndev, ~mask, RIS0);
916 if (ravb_rx(ndev, &quota, q))
917 goto out;
918 }
919 /* Processing TX Descriptor Ring */
920 if (tis & mask) {
921 spin_lock_irqsave(&priv->lock, flags);
922 /* Clear TX interrupt */
923 ravb_write(ndev, ~mask, TIS);
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100924 ravb_tx_free(ndev, q, true);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300925 netif_wake_subqueue(ndev, q);
926 mmiowb();
927 spin_unlock_irqrestore(&priv->lock, flags);
928 }
929 }
930
931 napi_complete(napi);
932
933 /* Re-enable RX/TX interrupts */
934 spin_lock_irqsave(&priv->lock, flags);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900935 if (priv->chip_id == RCAR_GEN2) {
936 ravb_modify(ndev, RIC0, mask, mask);
937 ravb_modify(ndev, TIC, mask, mask);
938 } else {
939 ravb_write(ndev, mask, RIE0);
940 ravb_write(ndev, mask, TIE);
941 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300942 mmiowb();
943 spin_unlock_irqrestore(&priv->lock, flags);
944
945 /* Receive error message handling */
946 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
947 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
Kazuya Mizuguchi18a3ed52017-01-12 13:21:06 +0100948 if (priv->rx_over_errors != ndev->stats.rx_over_errors)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300949 ndev->stats.rx_over_errors = priv->rx_over_errors;
Kazuya Mizuguchi18a3ed52017-01-12 13:21:06 +0100950 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300951 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300952out:
953 return budget - quota;
954}
955
956/* PHY state control function */
957static void ravb_adjust_link(struct net_device *ndev)
958{
959 struct ravb_private *priv = netdev_priv(ndev);
Philippe Reynes0f635172016-08-20 00:52:18 +0200960 struct phy_device *phydev = ndev->phydev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300961 bool new_state = false;
962
963 if (phydev->link) {
964 if (phydev->duplex != priv->duplex) {
965 new_state = true;
966 priv->duplex = phydev->duplex;
967 ravb_set_duplex(ndev);
968 }
969
970 if (phydev->speed != priv->speed) {
971 new_state = true;
972 priv->speed = phydev->speed;
973 ravb_set_rate(ndev);
974 }
975 if (!priv->link) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300976 ravb_modify(ndev, ECMR, ECMR_TXF, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300977 new_state = true;
978 priv->link = phydev->link;
979 if (priv->no_avb_link)
980 ravb_rcv_snd_enable(ndev);
981 }
982 } else if (priv->link) {
983 new_state = true;
984 priv->link = 0;
985 priv->speed = 0;
986 priv->duplex = -1;
987 if (priv->no_avb_link)
988 ravb_rcv_snd_disable(ndev);
989 }
990
991 if (new_state && netif_msg_link(priv))
992 phy_print_status(phydev);
993}
994
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +0100995static const struct soc_device_attribute r8a7795es10[] = {
996 { .soc_id = "r8a7795", .revision = "ES1.0", },
997 { /* sentinel */ }
998};
999
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001000/* PHY init function */
1001static int ravb_phy_init(struct net_device *ndev)
1002{
1003 struct device_node *np = ndev->dev.parent->of_node;
1004 struct ravb_private *priv = netdev_priv(ndev);
1005 struct phy_device *phydev;
1006 struct device_node *pn;
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +09001007 int err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001008
1009 priv->link = 0;
1010 priv->speed = 0;
1011 priv->duplex = -1;
1012
1013 /* Try connecting to PHY */
1014 pn = of_parse_phandle(np, "phy-handle", 0);
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +09001015 if (!pn) {
1016 /* In the case of a fixed PHY, the DT node associated
1017 * to the PHY is the Ethernet MAC DT node.
1018 */
1019 if (of_phy_is_fixed_link(np)) {
1020 err = of_phy_register_fixed_link(np);
1021 if (err)
1022 return err;
1023 }
1024 pn = of_node_get(np);
1025 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001026 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
1027 priv->phy_interface);
Peter Chenc9b1eb82016-08-01 15:02:39 +08001028 of_node_put(pn);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001029 if (!phydev) {
1030 netdev_err(ndev, "failed to connect PHY\n");
Johan Hovold9f70eb32016-11-28 19:25:06 +01001031 err = -ENOENT;
1032 goto err_deregister_fixed_link;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001033 }
1034
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +01001035 /* This driver only support 10/100Mbit speeds on R-Car H3 ES1.0
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001036 * at this time.
1037 */
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +01001038 if (soc_device_match(r8a7795es10)) {
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001039 err = phy_set_max_speed(phydev, SPEED_100);
1040 if (err) {
1041 netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
Johan Hovold9f70eb32016-11-28 19:25:06 +01001042 goto err_phy_disconnect;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001043 }
1044
1045 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
1046 }
1047
Kazuya Mizuguchi54499962015-12-14 00:15:58 +09001048 /* 10BASE is not supported */
1049 phydev->supported &= ~PHY_10BT_FEATURES;
1050
Andrew Lunn22209432016-01-06 20:11:13 +01001051 phy_attached_info(phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001052
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001053 return 0;
Johan Hovold9f70eb32016-11-28 19:25:06 +01001054
1055err_phy_disconnect:
1056 phy_disconnect(phydev);
1057err_deregister_fixed_link:
1058 if (of_phy_is_fixed_link(np))
1059 of_phy_deregister_fixed_link(np);
1060
1061 return err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001062}
1063
1064/* PHY control start function */
1065static int ravb_phy_start(struct net_device *ndev)
1066{
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001067 int error;
1068
1069 error = ravb_phy_init(ndev);
1070 if (error)
1071 return error;
1072
Philippe Reynes0f635172016-08-20 00:52:18 +02001073 phy_start(ndev->phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001074
1075 return 0;
1076}
1077
Philippe Reynes04462f22016-08-20 00:52:19 +02001078static int ravb_get_link_ksettings(struct net_device *ndev,
1079 struct ethtool_link_ksettings *cmd)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001080{
1081 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001082 unsigned long flags;
1083
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03001084 if (!ndev->phydev)
1085 return -ENODEV;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001086
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03001087 spin_lock_irqsave(&priv->lock, flags);
1088 phy_ethtool_ksettings_get(ndev->phydev, cmd);
1089 spin_unlock_irqrestore(&priv->lock, flags);
1090
1091 return 0;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001092}
1093
Philippe Reynes04462f22016-08-20 00:52:19 +02001094static int ravb_set_link_ksettings(struct net_device *ndev,
1095 const struct ethtool_link_ksettings *cmd)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001096{
1097 struct ravb_private *priv = netdev_priv(ndev);
1098 unsigned long flags;
1099 int error;
1100
Philippe Reynes0f635172016-08-20 00:52:18 +02001101 if (!ndev->phydev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001102 return -ENODEV;
1103
1104 spin_lock_irqsave(&priv->lock, flags);
1105
1106 /* Disable TX and RX */
1107 ravb_rcv_snd_disable(ndev);
1108
Philippe Reynes04462f22016-08-20 00:52:19 +02001109 error = phy_ethtool_ksettings_set(ndev->phydev, cmd);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001110 if (error)
1111 goto error_exit;
1112
Philippe Reynes04462f22016-08-20 00:52:19 +02001113 if (cmd->base.duplex == DUPLEX_FULL)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001114 priv->duplex = 1;
1115 else
1116 priv->duplex = 0;
1117
1118 ravb_set_duplex(ndev);
1119
1120error_exit:
1121 mdelay(1);
1122
1123 /* Enable TX and RX */
1124 ravb_rcv_snd_enable(ndev);
1125
1126 mmiowb();
1127 spin_unlock_irqrestore(&priv->lock, flags);
1128
1129 return error;
1130}
1131
1132static int ravb_nway_reset(struct net_device *ndev)
1133{
1134 struct ravb_private *priv = netdev_priv(ndev);
1135 int error = -ENODEV;
1136 unsigned long flags;
1137
Philippe Reynes0f635172016-08-20 00:52:18 +02001138 if (ndev->phydev) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001139 spin_lock_irqsave(&priv->lock, flags);
Philippe Reynes0f635172016-08-20 00:52:18 +02001140 error = phy_start_aneg(ndev->phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001141 spin_unlock_irqrestore(&priv->lock, flags);
1142 }
1143
1144 return error;
1145}
1146
1147static u32 ravb_get_msglevel(struct net_device *ndev)
1148{
1149 struct ravb_private *priv = netdev_priv(ndev);
1150
1151 return priv->msg_enable;
1152}
1153
1154static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1155{
1156 struct ravb_private *priv = netdev_priv(ndev);
1157
1158 priv->msg_enable = value;
1159}
1160
1161static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1162 "rx_queue_0_current",
1163 "tx_queue_0_current",
1164 "rx_queue_0_dirty",
1165 "tx_queue_0_dirty",
1166 "rx_queue_0_packets",
1167 "tx_queue_0_packets",
1168 "rx_queue_0_bytes",
1169 "tx_queue_0_bytes",
1170 "rx_queue_0_mcast_packets",
1171 "rx_queue_0_errors",
1172 "rx_queue_0_crc_errors",
1173 "rx_queue_0_frame_errors",
1174 "rx_queue_0_length_errors",
1175 "rx_queue_0_missed_errors",
1176 "rx_queue_0_over_errors",
1177
1178 "rx_queue_1_current",
1179 "tx_queue_1_current",
1180 "rx_queue_1_dirty",
1181 "tx_queue_1_dirty",
1182 "rx_queue_1_packets",
1183 "tx_queue_1_packets",
1184 "rx_queue_1_bytes",
1185 "tx_queue_1_bytes",
1186 "rx_queue_1_mcast_packets",
1187 "rx_queue_1_errors",
1188 "rx_queue_1_crc_errors",
Sergei Shtylyovb17c1d92015-12-04 01:51:10 +03001189 "rx_queue_1_frame_errors",
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001190 "rx_queue_1_length_errors",
1191 "rx_queue_1_missed_errors",
1192 "rx_queue_1_over_errors",
1193};
1194
1195#define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1196
1197static int ravb_get_sset_count(struct net_device *netdev, int sset)
1198{
1199 switch (sset) {
1200 case ETH_SS_STATS:
1201 return RAVB_STATS_LEN;
1202 default:
1203 return -EOPNOTSUPP;
1204 }
1205}
1206
1207static void ravb_get_ethtool_stats(struct net_device *ndev,
1208 struct ethtool_stats *stats, u64 *data)
1209{
1210 struct ravb_private *priv = netdev_priv(ndev);
1211 int i = 0;
1212 int q;
1213
1214 /* Device-specific stats */
1215 for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1216 struct net_device_stats *stats = &priv->stats[q];
1217
1218 data[i++] = priv->cur_rx[q];
1219 data[i++] = priv->cur_tx[q];
1220 data[i++] = priv->dirty_rx[q];
1221 data[i++] = priv->dirty_tx[q];
1222 data[i++] = stats->rx_packets;
1223 data[i++] = stats->tx_packets;
1224 data[i++] = stats->rx_bytes;
1225 data[i++] = stats->tx_bytes;
1226 data[i++] = stats->multicast;
1227 data[i++] = stats->rx_errors;
1228 data[i++] = stats->rx_crc_errors;
1229 data[i++] = stats->rx_frame_errors;
1230 data[i++] = stats->rx_length_errors;
1231 data[i++] = stats->rx_missed_errors;
1232 data[i++] = stats->rx_over_errors;
1233 }
1234}
1235
1236static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1237{
1238 switch (stringset) {
1239 case ETH_SS_STATS:
1240 memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
1241 break;
1242 }
1243}
1244
1245static void ravb_get_ringparam(struct net_device *ndev,
1246 struct ethtool_ringparam *ring)
1247{
1248 struct ravb_private *priv = netdev_priv(ndev);
1249
1250 ring->rx_max_pending = BE_RX_RING_MAX;
1251 ring->tx_max_pending = BE_TX_RING_MAX;
1252 ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1253 ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1254}
1255
1256static int ravb_set_ringparam(struct net_device *ndev,
1257 struct ethtool_ringparam *ring)
1258{
1259 struct ravb_private *priv = netdev_priv(ndev);
1260 int error;
1261
1262 if (ring->tx_pending > BE_TX_RING_MAX ||
1263 ring->rx_pending > BE_RX_RING_MAX ||
1264 ring->tx_pending < BE_TX_RING_MIN ||
1265 ring->rx_pending < BE_RX_RING_MIN)
1266 return -EINVAL;
1267 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1268 return -EINVAL;
1269
1270 if (netif_running(ndev)) {
1271 netif_device_detach(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001272 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001273 if (priv->chip_id == RCAR_GEN2)
1274 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001275 /* Wait for DMA stopping */
1276 error = ravb_stop_dma(ndev);
1277 if (error) {
1278 netdev_err(ndev,
1279 "cannot set ringparam! Any AVB processes are still running?\n");
1280 return error;
1281 }
1282 synchronize_irq(ndev->irq);
1283
1284 /* Free all the skb's in the RX queue and the DMA buffers. */
1285 ravb_ring_free(ndev, RAVB_BE);
1286 ravb_ring_free(ndev, RAVB_NC);
1287 }
1288
1289 /* Set new parameters */
1290 priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1291 priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1292
1293 if (netif_running(ndev)) {
1294 error = ravb_dmac_init(ndev);
1295 if (error) {
1296 netdev_err(ndev,
1297 "%s: ravb_dmac_init() failed, error %d\n",
1298 __func__, error);
1299 return error;
1300 }
1301
1302 ravb_emac_init(ndev);
1303
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001304 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001305 if (priv->chip_id == RCAR_GEN2)
1306 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001307
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001308 netif_device_attach(ndev);
1309 }
1310
1311 return 0;
1312}
1313
1314static int ravb_get_ts_info(struct net_device *ndev,
1315 struct ethtool_ts_info *info)
1316{
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001317 struct ravb_private *priv = netdev_priv(ndev);
1318
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001319 info->so_timestamping =
1320 SOF_TIMESTAMPING_TX_SOFTWARE |
1321 SOF_TIMESTAMPING_RX_SOFTWARE |
1322 SOF_TIMESTAMPING_SOFTWARE |
1323 SOF_TIMESTAMPING_TX_HARDWARE |
1324 SOF_TIMESTAMPING_RX_HARDWARE |
1325 SOF_TIMESTAMPING_RAW_HARDWARE;
1326 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1327 info->rx_filters =
1328 (1 << HWTSTAMP_FILTER_NONE) |
1329 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1330 (1 << HWTSTAMP_FILTER_ALL);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001331 info->phc_index = ptp_clock_index(priv->ptp.clock);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001332
1333 return 0;
1334}
1335
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02001336static void ravb_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1337{
1338 struct ravb_private *priv = netdev_priv(ndev);
1339
1340 wol->supported = 0;
1341 wol->wolopts = 0;
1342
1343 if (priv->clk) {
1344 wol->supported = WAKE_MAGIC;
1345 wol->wolopts = priv->wol_enabled ? WAKE_MAGIC : 0;
1346 }
1347}
1348
1349static int ravb_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1350{
1351 struct ravb_private *priv = netdev_priv(ndev);
1352
1353 if (!priv->clk || wol->wolopts & ~WAKE_MAGIC)
1354 return -EOPNOTSUPP;
1355
1356 priv->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
1357
1358 device_set_wakeup_enable(&priv->pdev->dev, priv->wol_enabled);
1359
1360 return 0;
1361}
1362
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001363static const struct ethtool_ops ravb_ethtool_ops = {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001364 .nway_reset = ravb_nway_reset,
1365 .get_msglevel = ravb_get_msglevel,
1366 .set_msglevel = ravb_set_msglevel,
1367 .get_link = ethtool_op_get_link,
1368 .get_strings = ravb_get_strings,
1369 .get_ethtool_stats = ravb_get_ethtool_stats,
1370 .get_sset_count = ravb_get_sset_count,
1371 .get_ringparam = ravb_get_ringparam,
1372 .set_ringparam = ravb_set_ringparam,
1373 .get_ts_info = ravb_get_ts_info,
Philippe Reynes04462f22016-08-20 00:52:19 +02001374 .get_link_ksettings = ravb_get_link_ksettings,
1375 .set_link_ksettings = ravb_set_link_ksettings,
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02001376 .get_wol = ravb_get_wol,
1377 .set_wol = ravb_set_wol,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001378};
1379
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001380static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1381 struct net_device *ndev, struct device *dev,
1382 const char *ch)
1383{
1384 char *name;
1385 int error;
1386
1387 name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1388 if (!name)
1389 return -ENOMEM;
1390 error = request_irq(irq, handler, 0, name, ndev);
1391 if (error)
1392 netdev_err(ndev, "cannot request IRQ %s\n", name);
1393
1394 return error;
1395}
1396
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001397/* Network device open function for Ethernet AVB */
1398static int ravb_open(struct net_device *ndev)
1399{
1400 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001401 struct platform_device *pdev = priv->pdev;
1402 struct device *dev = &pdev->dev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001403 int error;
1404
1405 napi_enable(&priv->napi[RAVB_BE]);
1406 napi_enable(&priv->napi[RAVB_NC]);
1407
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001408 if (priv->chip_id == RCAR_GEN2) {
1409 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1410 ndev->name, ndev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001411 if (error) {
1412 netdev_err(ndev, "cannot request IRQ\n");
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001413 goto out_napi_off;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001414 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001415 } else {
1416 error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1417 dev, "ch22:multi");
1418 if (error)
1419 goto out_napi_off;
1420 error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1421 dev, "ch24:emac");
1422 if (error)
1423 goto out_free_irq;
1424 error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1425 ndev, dev, "ch0:rx_be");
1426 if (error)
1427 goto out_free_irq_emac;
1428 error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1429 ndev, dev, "ch18:tx_be");
1430 if (error)
1431 goto out_free_irq_be_rx;
1432 error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1433 ndev, dev, "ch1:rx_nc");
1434 if (error)
1435 goto out_free_irq_be_tx;
1436 error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1437 ndev, dev, "ch19:tx_nc");
1438 if (error)
1439 goto out_free_irq_nc_rx;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001440 }
1441
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001442 /* Device init */
1443 error = ravb_dmac_init(ndev);
1444 if (error)
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001445 goto out_free_irq_nc_tx;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001446 ravb_emac_init(ndev);
1447
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001448 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001449 if (priv->chip_id == RCAR_GEN2)
1450 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001451
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001452 netif_tx_start_all_queues(ndev);
1453
1454 /* PHY control start */
1455 error = ravb_phy_start(ndev);
1456 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001457 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001458
1459 return 0;
1460
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001461out_ptp_stop:
1462 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001463 if (priv->chip_id == RCAR_GEN2)
1464 ravb_ptp_stop(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001465out_free_irq_nc_tx:
1466 if (priv->chip_id == RCAR_GEN2)
1467 goto out_free_irq;
1468 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1469out_free_irq_nc_rx:
1470 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1471out_free_irq_be_tx:
1472 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1473out_free_irq_be_rx:
1474 free_irq(priv->rx_irqs[RAVB_BE], ndev);
1475out_free_irq_emac:
1476 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001477out_free_irq:
1478 free_irq(ndev->irq, ndev);
1479out_napi_off:
1480 napi_disable(&priv->napi[RAVB_NC]);
1481 napi_disable(&priv->napi[RAVB_BE]);
1482 return error;
1483}
1484
1485/* Timeout function for Ethernet AVB */
1486static void ravb_tx_timeout(struct net_device *ndev)
1487{
1488 struct ravb_private *priv = netdev_priv(ndev);
1489
1490 netif_err(priv, tx_err, ndev,
1491 "transmit timed out, status %08x, resetting...\n",
1492 ravb_read(ndev, ISS));
1493
1494 /* tx_errors count up */
1495 ndev->stats.tx_errors++;
1496
1497 schedule_work(&priv->work);
1498}
1499
1500static void ravb_tx_timeout_work(struct work_struct *work)
1501{
1502 struct ravb_private *priv = container_of(work, struct ravb_private,
1503 work);
1504 struct net_device *ndev = priv->ndev;
1505
1506 netif_tx_stop_all_queues(ndev);
1507
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001508 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001509 if (priv->chip_id == RCAR_GEN2)
1510 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001511
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001512 /* Wait for DMA stopping */
1513 ravb_stop_dma(ndev);
1514
1515 ravb_ring_free(ndev, RAVB_BE);
1516 ravb_ring_free(ndev, RAVB_NC);
1517
1518 /* Device init */
1519 ravb_dmac_init(ndev);
1520 ravb_emac_init(ndev);
1521
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001522 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001523 if (priv->chip_id == RCAR_GEN2)
1524 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001525
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001526 netif_tx_start_all_queues(ndev);
1527}
1528
1529/* Packet transmit function for Ethernet AVB */
1530static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1531{
1532 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001533 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001534 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001535 struct ravb_tx_desc *desc;
1536 unsigned long flags;
1537 u32 dma_addr;
1538 void *buffer;
1539 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001540 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001541
1542 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001543 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1544 NUM_TX_DESC) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001545 netif_err(priv, tx_queued, ndev,
1546 "still transmitting with the full ring!\n");
1547 netif_stop_subqueue(ndev, q);
1548 spin_unlock_irqrestore(&priv->lock, flags);
1549 return NETDEV_TX_BUSY;
1550 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001551
1552 if (skb_put_padto(skb, ETH_ZLEN))
Dan Carpenter9199cb72017-04-22 13:46:56 +03001553 goto exit;
1554
1555 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1556 priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001557
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001558 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1559 entry / NUM_TX_DESC * DPTR_ALIGN;
1560 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
Masaru Nagai8ec3e8a2017-01-16 11:45:21 +01001561 /* Zero length DMA descriptors are problematic as they seem to
1562 * terminate DMA transfers. Avoid them by simply using a length of
1563 * DPTR_ALIGN (4) when skb data is aligned to DPTR_ALIGN.
1564 *
1565 * As skb is guaranteed to have at least ETH_ZLEN (60) bytes of
1566 * data by the call to skb_put_padto() above this is safe with
1567 * respect to both the length of the first DMA descriptor (len)
1568 * overflowing the available data and the length of the second DMA
1569 * descriptor (skb->len - len) being negative.
1570 */
1571 if (len == 0)
1572 len = DPTR_ALIGN;
1573
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001574 memcpy(buffer, skb->data, len);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001575 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1576 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001577 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001578
1579 desc = &priv->tx_ring[q][entry];
1580 desc->ds_tagl = cpu_to_le16(len);
1581 desc->dptr = cpu_to_le32(dma_addr);
1582
1583 buffer = skb->data + len;
1584 len = skb->len - len;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001585 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1586 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001587 goto unmap;
1588
1589 desc++;
1590 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001591 desc->dptr = cpu_to_le32(dma_addr);
1592
1593 /* TX timestamp required */
1594 if (q == RAVB_NC) {
1595 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1596 if (!ts_skb) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001597 desc--;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001598 dma_unmap_single(ndev->dev.parent, dma_addr, len,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001599 DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001600 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001601 }
1602 ts_skb->skb = skb;
1603 ts_skb->tag = priv->ts_skb_tag++;
1604 priv->ts_skb_tag &= 0x3ff;
1605 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1606
1607 /* TAG and timestamp required flag */
1608 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001609 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1610 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1611 }
1612
Lino Sanfilippod7be81a2016-03-27 12:22:02 +02001613 skb_tx_timestamp(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001614 /* Descriptor type must be set after all the above writes */
1615 dma_wmb();
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001616 desc->die_dt = DT_FEND;
1617 desc--;
1618 desc->die_dt = DT_FSTART;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001619
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001620 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001621
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001622 priv->cur_tx[q] += NUM_TX_DESC;
1623 if (priv->cur_tx[q] - priv->dirty_tx[q] >
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +01001624 (priv->num_tx_ring[q] - 1) * NUM_TX_DESC &&
1625 !ravb_tx_free(ndev, q, true))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001626 netif_stop_subqueue(ndev, q);
1627
1628exit:
1629 mmiowb();
1630 spin_unlock_irqrestore(&priv->lock, flags);
1631 return NETDEV_TX_OK;
1632
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001633unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001634 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001635 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001636drop:
1637 dev_kfree_skb_any(skb);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001638 priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001639 goto exit;
1640}
1641
1642static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1643 void *accel_priv, select_queue_fallback_t fallback)
1644{
1645 /* If skb needs TX timestamp, it is handled in network control queue */
1646 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1647 RAVB_BE;
1648
1649}
1650
1651static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1652{
1653 struct ravb_private *priv = netdev_priv(ndev);
1654 struct net_device_stats *nstats, *stats0, *stats1;
1655
1656 nstats = &ndev->stats;
1657 stats0 = &priv->stats[RAVB_BE];
1658 stats1 = &priv->stats[RAVB_NC];
1659
1660 nstats->tx_dropped += ravb_read(ndev, TROCR);
1661 ravb_write(ndev, 0, TROCR); /* (write clear) */
1662 nstats->collisions += ravb_read(ndev, CDCR);
1663 ravb_write(ndev, 0, CDCR); /* (write clear) */
1664 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1665 ravb_write(ndev, 0, LCCR); /* (write clear) */
1666
1667 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1668 ravb_write(ndev, 0, CERCR); /* (write clear) */
1669 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1670 ravb_write(ndev, 0, CEECR); /* (write clear) */
1671
1672 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1673 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1674 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1675 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1676 nstats->multicast = stats0->multicast + stats1->multicast;
1677 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1678 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1679 nstats->rx_frame_errors =
1680 stats0->rx_frame_errors + stats1->rx_frame_errors;
1681 nstats->rx_length_errors =
1682 stats0->rx_length_errors + stats1->rx_length_errors;
1683 nstats->rx_missed_errors =
1684 stats0->rx_missed_errors + stats1->rx_missed_errors;
1685 nstats->rx_over_errors =
1686 stats0->rx_over_errors + stats1->rx_over_errors;
1687
1688 return nstats;
1689}
1690
1691/* Update promiscuous bit */
1692static void ravb_set_rx_mode(struct net_device *ndev)
1693{
1694 struct ravb_private *priv = netdev_priv(ndev);
1695 unsigned long flags;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001696
1697 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001698 ravb_modify(ndev, ECMR, ECMR_PRM,
1699 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001700 mmiowb();
1701 spin_unlock_irqrestore(&priv->lock, flags);
1702}
1703
1704/* Device close function for Ethernet AVB */
1705static int ravb_close(struct net_device *ndev)
1706{
Johan Hovold9f70eb32016-11-28 19:25:06 +01001707 struct device_node *np = ndev->dev.parent->of_node;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001708 struct ravb_private *priv = netdev_priv(ndev);
1709 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1710
1711 netif_tx_stop_all_queues(ndev);
1712
1713 /* Disable interrupts by clearing the interrupt masks. */
1714 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001715 ravb_write(ndev, 0, RIC2);
1716 ravb_write(ndev, 0, TIC);
1717
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001718 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001719 if (priv->chip_id == RCAR_GEN2)
1720 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001721
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001722 /* Set the config mode to stop the AVB-DMAC's processes */
1723 if (ravb_stop_dma(ndev) < 0)
1724 netdev_err(ndev,
1725 "device will be stopped after h/w processes are done.\n");
1726
1727 /* Clear the timestamp list */
1728 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1729 list_del(&ts_skb->list);
1730 kfree(ts_skb);
1731 }
1732
1733 /* PHY disconnect */
Philippe Reynes0f635172016-08-20 00:52:18 +02001734 if (ndev->phydev) {
1735 phy_stop(ndev->phydev);
1736 phy_disconnect(ndev->phydev);
Johan Hovold9f70eb32016-11-28 19:25:06 +01001737 if (of_phy_is_fixed_link(np))
1738 of_phy_deregister_fixed_link(np);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001739 }
1740
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001741 if (priv->chip_id != RCAR_GEN2) {
1742 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1743 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1744 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1745 free_irq(priv->rx_irqs[RAVB_BE], ndev);
Geert Uytterhoeven7fa816b2016-05-07 13:17:11 +02001746 free_irq(priv->emac_irq, ndev);
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001747 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001748 free_irq(ndev->irq, ndev);
1749
1750 napi_disable(&priv->napi[RAVB_NC]);
1751 napi_disable(&priv->napi[RAVB_BE]);
1752
1753 /* Free all the skb's in the RX queue and the DMA buffers. */
1754 ravb_ring_free(ndev, RAVB_BE);
1755 ravb_ring_free(ndev, RAVB_NC);
1756
1757 return 0;
1758}
1759
1760static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1761{
1762 struct ravb_private *priv = netdev_priv(ndev);
1763 struct hwtstamp_config config;
1764
1765 config.flags = 0;
1766 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1767 HWTSTAMP_TX_OFF;
1768 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1769 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1770 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1771 config.rx_filter = HWTSTAMP_FILTER_ALL;
1772 else
1773 config.rx_filter = HWTSTAMP_FILTER_NONE;
1774
1775 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1776 -EFAULT : 0;
1777}
1778
1779/* Control hardware time stamping */
1780static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1781{
1782 struct ravb_private *priv = netdev_priv(ndev);
1783 struct hwtstamp_config config;
1784 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1785 u32 tstamp_tx_ctrl;
1786
1787 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1788 return -EFAULT;
1789
1790 /* Reserved for future extensions */
1791 if (config.flags)
1792 return -EINVAL;
1793
1794 switch (config.tx_type) {
1795 case HWTSTAMP_TX_OFF:
1796 tstamp_tx_ctrl = 0;
1797 break;
1798 case HWTSTAMP_TX_ON:
1799 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1800 break;
1801 default:
1802 return -ERANGE;
1803 }
1804
1805 switch (config.rx_filter) {
1806 case HWTSTAMP_FILTER_NONE:
1807 tstamp_rx_ctrl = 0;
1808 break;
1809 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1810 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1811 break;
1812 default:
1813 config.rx_filter = HWTSTAMP_FILTER_ALL;
1814 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1815 }
1816
1817 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1818 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1819
1820 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1821 -EFAULT : 0;
1822}
1823
1824/* ioctl to device function */
1825static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1826{
Philippe Reynes0f635172016-08-20 00:52:18 +02001827 struct phy_device *phydev = ndev->phydev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001828
1829 if (!netif_running(ndev))
1830 return -EINVAL;
1831
1832 if (!phydev)
1833 return -ENODEV;
1834
1835 switch (cmd) {
1836 case SIOCGHWTSTAMP:
1837 return ravb_hwtstamp_get(ndev, req);
1838 case SIOCSHWTSTAMP:
1839 return ravb_hwtstamp_set(ndev, req);
1840 }
1841
1842 return phy_mii_ioctl(phydev, req, cmd);
1843}
1844
1845static const struct net_device_ops ravb_netdev_ops = {
1846 .ndo_open = ravb_open,
1847 .ndo_stop = ravb_close,
1848 .ndo_start_xmit = ravb_start_xmit,
1849 .ndo_select_queue = ravb_select_queue,
1850 .ndo_get_stats = ravb_get_stats,
1851 .ndo_set_rx_mode = ravb_set_rx_mode,
1852 .ndo_tx_timeout = ravb_tx_timeout,
1853 .ndo_do_ioctl = ravb_do_ioctl,
1854 .ndo_validate_addr = eth_validate_addr,
1855 .ndo_set_mac_address = eth_mac_addr,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001856};
1857
1858/* MDIO bus init function */
1859static int ravb_mdio_init(struct ravb_private *priv)
1860{
1861 struct platform_device *pdev = priv->pdev;
1862 struct device *dev = &pdev->dev;
1863 int error;
1864
1865 /* Bitbang init */
1866 priv->mdiobb.ops = &bb_ops;
1867
1868 /* MII controller setting */
1869 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1870 if (!priv->mii_bus)
1871 return -ENOMEM;
1872
1873 /* Hook up MII support for ethtool */
1874 priv->mii_bus->name = "ravb_mii";
1875 priv->mii_bus->parent = dev;
1876 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1877 pdev->name, pdev->id);
1878
1879 /* Register MDIO bus */
1880 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1881 if (error)
1882 goto out_free_bus;
1883
1884 return 0;
1885
1886out_free_bus:
1887 free_mdio_bitbang(priv->mii_bus);
1888 return error;
1889}
1890
1891/* MDIO bus release function */
1892static int ravb_mdio_release(struct ravb_private *priv)
1893{
1894 /* Unregister mdio bus */
1895 mdiobus_unregister(priv->mii_bus);
1896
1897 /* Free bitbang info */
1898 free_mdio_bitbang(priv->mii_bus);
1899
1900 return 0;
1901}
1902
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001903static const struct of_device_id ravb_match_table[] = {
1904 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1905 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001906 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001907 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001908 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001909 { }
1910};
1911MODULE_DEVICE_TABLE(of, ravb_match_table);
1912
Simon Hormanb3d39a82015-11-20 11:29:39 -08001913static int ravb_set_gti(struct net_device *ndev)
1914{
1915
1916 struct device *dev = ndev->dev.parent;
1917 struct device_node *np = dev->of_node;
1918 unsigned long rate;
1919 struct clk *clk;
1920 uint64_t inc;
1921
1922 clk = of_clk_get(np, 0);
1923 if (IS_ERR(clk)) {
1924 dev_err(dev, "could not get clock\n");
1925 return PTR_ERR(clk);
1926 }
1927
1928 rate = clk_get_rate(clk);
1929 clk_put(clk);
1930
Wolfram Sanga6d37132016-04-08 13:28:42 +02001931 if (!rate)
1932 return -EINVAL;
1933
Simon Hormanb3d39a82015-11-20 11:29:39 -08001934 inc = 1000000000ULL << 20;
1935 do_div(inc, rate);
1936
1937 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1938 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1939 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1940 return -EINVAL;
1941 }
1942
1943 ravb_write(ndev, inc, GTI);
1944
1945 return 0;
1946}
1947
Niklas Söderlund01841652016-08-03 15:56:47 +02001948static void ravb_set_config_mode(struct net_device *ndev)
1949{
1950 struct ravb_private *priv = netdev_priv(ndev);
1951
1952 if (priv->chip_id == RCAR_GEN2) {
1953 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
1954 /* Set CSEL value */
1955 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
1956 } else {
1957 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
1958 CCC_GAC | CCC_CSEL_HPB);
1959 }
1960}
1961
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01001962/* Set tx and rx clock internal delay modes */
1963static void ravb_set_delay_mode(struct net_device *ndev)
1964{
1965 struct ravb_private *priv = netdev_priv(ndev);
1966 int set = 0;
1967
1968 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1969 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID)
1970 set |= APSR_DM_RDM;
1971
1972 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1973 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1974 set |= APSR_DM_TDM;
1975
1976 ravb_modify(ndev, APSR, APSR_DM, set);
1977}
1978
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001979static int ravb_probe(struct platform_device *pdev)
1980{
1981 struct device_node *np = pdev->dev.of_node;
1982 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001983 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001984 struct net_device *ndev;
1985 int error, irq, q;
1986 struct resource *res;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001987 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001988
1989 if (!np) {
1990 dev_err(&pdev->dev,
1991 "this driver is required to be instantiated from device tree\n");
1992 return -EINVAL;
1993 }
1994
1995 /* Get base address */
1996 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1997 if (!res) {
1998 dev_err(&pdev->dev, "invalid resource\n");
1999 return -EINVAL;
2000 }
2001
2002 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
2003 NUM_TX_QUEUE, NUM_RX_QUEUE);
2004 if (!ndev)
2005 return -ENOMEM;
2006
2007 pm_runtime_enable(&pdev->dev);
2008 pm_runtime_get_sync(&pdev->dev);
2009
2010 /* The Ether-specific entries in the device structure. */
2011 ndev->base_addr = res->start;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002012
Wolfram Sange8668632016-03-01 17:37:58 +01002013 chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002014
2015 if (chip_id == RCAR_GEN3)
2016 irq = platform_get_irq_byname(pdev, "ch22");
2017 else
2018 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002019 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03002020 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002021 goto out_release;
2022 }
2023 ndev->irq = irq;
2024
2025 SET_NETDEV_DEV(ndev, &pdev->dev);
2026
2027 priv = netdev_priv(ndev);
2028 priv->ndev = ndev;
2029 priv->pdev = pdev;
2030 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
2031 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
2032 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
2033 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
2034 priv->addr = devm_ioremap_resource(&pdev->dev, res);
2035 if (IS_ERR(priv->addr)) {
2036 error = PTR_ERR(priv->addr);
2037 goto out_release;
2038 }
2039
2040 spin_lock_init(&priv->lock);
2041 INIT_WORK(&priv->work, ravb_tx_timeout_work);
2042
2043 priv->phy_interface = of_get_phy_mode(np);
2044
2045 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
2046 priv->avb_link_active_low =
2047 of_property_read_bool(np, "renesas,ether-link-active-low");
2048
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002049 if (chip_id == RCAR_GEN3) {
2050 irq = platform_get_irq_byname(pdev, "ch24");
2051 if (irq < 0) {
2052 error = irq;
2053 goto out_release;
2054 }
2055 priv->emac_irq = irq;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09002056 for (i = 0; i < NUM_RX_QUEUE; i++) {
2057 irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
2058 if (irq < 0) {
2059 error = irq;
2060 goto out_release;
2061 }
2062 priv->rx_irqs[i] = irq;
2063 }
2064 for (i = 0; i < NUM_TX_QUEUE; i++) {
2065 irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
2066 if (irq < 0) {
2067 error = irq;
2068 goto out_release;
2069 }
2070 priv->tx_irqs[i] = irq;
2071 }
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002072 }
2073
2074 priv->chip_id = chip_id;
2075
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002076 /* Get clock, if not found that's OK but Wake-On-Lan is unavailable */
2077 priv->clk = devm_clk_get(&pdev->dev, NULL);
2078 if (IS_ERR(priv->clk))
2079 priv->clk = NULL;
2080
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002081 /* Set function */
2082 ndev->netdev_ops = &ravb_netdev_ops;
2083 ndev->ethtool_ops = &ravb_ethtool_ops;
2084
2085 /* Set AVB config mode */
Niklas Söderlund01841652016-08-03 15:56:47 +02002086 ravb_set_config_mode(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002087
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002088 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08002089 error = ravb_set_gti(ndev);
2090 if (error)
2091 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002092
2093 /* Request GTI loading */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03002094 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002095
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002096 if (priv->chip_id != RCAR_GEN2)
2097 ravb_set_delay_mode(ndev);
2098
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002099 /* Allocate descriptor base address table */
2100 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002101 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002102 &priv->desc_bat_dma, GFP_KERNEL);
2103 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09002104 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002105 "Cannot allocate desc base address table (size %d bytes)\n",
2106 priv->desc_bat_size);
2107 error = -ENOMEM;
2108 goto out_release;
2109 }
2110 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2111 priv->desc_bat[q].die_dt = DT_EOS;
2112 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2113
2114 /* Initialise HW timestamp list */
2115 INIT_LIST_HEAD(&priv->ts_skb_list);
2116
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002117 /* Initialise PTP Clock driver */
2118 if (chip_id != RCAR_GEN2)
2119 ravb_ptp_init(ndev, pdev);
2120
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002121 /* Debug message level */
2122 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2123
2124 /* Read and set MAC address */
2125 ravb_read_mac_address(ndev, of_get_mac_address(np));
2126 if (!is_valid_ether_addr(ndev->dev_addr)) {
2127 dev_warn(&pdev->dev,
2128 "no valid MAC address supplied, using a random one\n");
2129 eth_hw_addr_random(ndev);
2130 }
2131
2132 /* MDIO bus init */
2133 error = ravb_mdio_init(priv);
2134 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09002135 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002136 goto out_dma_free;
2137 }
2138
2139 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
2140 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
2141
2142 /* Network device register */
2143 error = register_netdev(ndev);
2144 if (error)
2145 goto out_napi_del;
2146
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002147 if (priv->clk)
2148 device_set_wakeup_capable(&pdev->dev, 1);
2149
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002150 /* Print device information */
2151 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2152 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2153
2154 platform_set_drvdata(pdev, ndev);
2155
2156 return 0;
2157
2158out_napi_del:
2159 netif_napi_del(&priv->napi[RAVB_NC]);
2160 netif_napi_del(&priv->napi[RAVB_BE]);
2161 ravb_mdio_release(priv);
2162out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002163 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002164 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002165
2166 /* Stop PTP Clock driver */
2167 if (chip_id != RCAR_GEN2)
2168 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002169out_release:
2170 if (ndev)
2171 free_netdev(ndev);
2172
2173 pm_runtime_put(&pdev->dev);
2174 pm_runtime_disable(&pdev->dev);
2175 return error;
2176}
2177
2178static int ravb_remove(struct platform_device *pdev)
2179{
2180 struct net_device *ndev = platform_get_drvdata(pdev);
2181 struct ravb_private *priv = netdev_priv(ndev);
2182
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002183 /* Stop PTP Clock driver */
2184 if (priv->chip_id != RCAR_GEN2)
2185 ravb_ptp_stop(ndev);
2186
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002187 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002188 priv->desc_bat_dma);
2189 /* Set reset mode */
2190 ravb_write(ndev, CCC_OPC_RESET, CCC);
2191 pm_runtime_put_sync(&pdev->dev);
2192 unregister_netdev(ndev);
2193 netif_napi_del(&priv->napi[RAVB_NC]);
2194 netif_napi_del(&priv->napi[RAVB_BE]);
2195 ravb_mdio_release(priv);
2196 pm_runtime_disable(&pdev->dev);
2197 free_netdev(ndev);
2198 platform_set_drvdata(pdev, NULL);
2199
2200 return 0;
2201}
2202
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002203static int ravb_wol_setup(struct net_device *ndev)
2204{
2205 struct ravb_private *priv = netdev_priv(ndev);
2206
2207 /* Disable interrupts by clearing the interrupt masks. */
2208 ravb_write(ndev, 0, RIC0);
2209 ravb_write(ndev, 0, RIC2);
2210 ravb_write(ndev, 0, TIC);
2211
2212 /* Only allow ECI interrupts */
2213 synchronize_irq(priv->emac_irq);
2214 napi_disable(&priv->napi[RAVB_NC]);
2215 napi_disable(&priv->napi[RAVB_BE]);
2216 ravb_write(ndev, ECSIPR_MPDIP, ECSIPR);
2217
2218 /* Enable MagicPacket */
2219 ravb_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
2220
2221 /* Increased clock usage so device won't be suspended */
2222 clk_enable(priv->clk);
2223
2224 return enable_irq_wake(priv->emac_irq);
2225}
2226
2227static int ravb_wol_restore(struct net_device *ndev)
2228{
2229 struct ravb_private *priv = netdev_priv(ndev);
2230 int ret;
2231
2232 napi_enable(&priv->napi[RAVB_NC]);
2233 napi_enable(&priv->napi[RAVB_BE]);
2234
2235 /* Disable MagicPacket */
2236 ravb_modify(ndev, ECMR, ECMR_MPDE, 0);
2237
2238 ret = ravb_close(ndev);
2239 if (ret < 0)
2240 return ret;
2241
2242 /* Restore clock usage count */
2243 clk_disable(priv->clk);
2244
2245 return disable_irq_wake(priv->emac_irq);
2246}
2247
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002248static int __maybe_unused ravb_suspend(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002249{
2250 struct net_device *ndev = dev_get_drvdata(dev);
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002251 struct ravb_private *priv = netdev_priv(ndev);
2252 int ret;
Niklas Söderlund01841652016-08-03 15:56:47 +02002253
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002254 if (!netif_running(ndev))
2255 return 0;
2256
2257 netif_device_detach(ndev);
2258
2259 if (priv->wol_enabled)
2260 ret = ravb_wol_setup(ndev);
2261 else
Niklas Söderlund01841652016-08-03 15:56:47 +02002262 ret = ravb_close(ndev);
Niklas Söderlund01841652016-08-03 15:56:47 +02002263
2264 return ret;
2265}
2266
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002267static int __maybe_unused ravb_resume(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002268{
2269 struct net_device *ndev = dev_get_drvdata(dev);
2270 struct ravb_private *priv = netdev_priv(ndev);
2271 int ret = 0;
2272
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002273 /* If WoL is enabled set reset mode to rearm the WoL logic */
2274 if (priv->wol_enabled)
2275 ravb_write(ndev, CCC_OPC_RESET, CCC);
2276
Niklas Söderlund01841652016-08-03 15:56:47 +02002277 /* All register have been reset to default values.
2278 * Restore all registers which where setup at probe time and
2279 * reopen device if it was running before system suspended.
2280 */
2281
2282 /* Set AVB config mode */
2283 ravb_set_config_mode(ndev);
2284
2285 /* Set GTI value */
2286 ret = ravb_set_gti(ndev);
2287 if (ret)
2288 return ret;
2289
2290 /* Request GTI loading */
2291 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
2292
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002293 if (priv->chip_id != RCAR_GEN2)
2294 ravb_set_delay_mode(ndev);
2295
Niklas Söderlund01841652016-08-03 15:56:47 +02002296 /* Restore descriptor base address table */
2297 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2298
2299 if (netif_running(ndev)) {
Niklas Söderlund3e3d6472017-08-01 12:14:36 +02002300 if (priv->wol_enabled) {
2301 ret = ravb_wol_restore(ndev);
2302 if (ret)
2303 return ret;
2304 }
Niklas Söderlund01841652016-08-03 15:56:47 +02002305 ret = ravb_open(ndev);
2306 if (ret < 0)
2307 return ret;
2308 netif_device_attach(ndev);
2309 }
2310
2311 return ret;
2312}
2313
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002314static int __maybe_unused ravb_runtime_nop(struct device *dev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002315{
2316 /* Runtime PM callback shared between ->runtime_suspend()
2317 * and ->runtime_resume(). Simply returns success.
2318 *
2319 * This driver re-initializes all registers after
2320 * pm_runtime_get_sync() anyway so there is no need
2321 * to save and restore registers here.
2322 */
2323 return 0;
2324}
2325
2326static const struct dev_pm_ops ravb_dev_pm_ops = {
Niklas Söderlundb89b8152016-08-10 13:09:49 +02002327 SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume)
Kazuya Mizuguchi524c6f62016-05-30 05:25:43 +09002328 SET_RUNTIME_PM_OPS(ravb_runtime_nop, ravb_runtime_nop, NULL)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002329};
2330
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002331static struct platform_driver ravb_driver = {
2332 .probe = ravb_probe,
2333 .remove = ravb_remove,
2334 .driver = {
2335 .name = "ravb",
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002336 .pm = &ravb_dev_pm_ops,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002337 .of_match_table = ravb_match_table,
2338 },
2339};
2340
2341module_platform_driver(ravb_driver);
2342
2343MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2344MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2345MODULE_LICENSE("GPL v2");