blob: 9d8320edea536e7ee87f1a069cdfa8d90b7182f4 [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>
34
Simon Hormanb3d39a82015-11-20 11:29:39 -080035#include <asm/div64.h>
36
Sergei Shtylyovc1566332015-06-11 01:01:43 +030037#include "ravb.h"
38
39#define RAVB_DEF_MSG_ENABLE \
40 (NETIF_MSG_LINK | \
41 NETIF_MSG_TIMER | \
42 NETIF_MSG_RX_ERR | \
43 NETIF_MSG_TX_ERR)
44
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +090045static const char *ravb_rx_irqs[NUM_RX_QUEUE] = {
46 "ch0", /* RAVB_BE */
47 "ch1", /* RAVB_NC */
48};
49
50static const char *ravb_tx_irqs[NUM_TX_QUEUE] = {
51 "ch18", /* RAVB_BE */
52 "ch19", /* RAVB_NC */
53};
54
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030055void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear,
56 u32 set)
57{
58 ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg);
59}
60
Sergei Shtylyova0d2f202015-06-11 01:02:30 +030061int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value)
Sergei Shtylyovc1566332015-06-11 01:01:43 +030062{
63 int i;
64
65 for (i = 0; i < 10000; i++) {
66 if ((ravb_read(ndev, reg) & mask) == value)
67 return 0;
68 udelay(10);
69 }
70 return -ETIMEDOUT;
71}
72
73static int ravb_config(struct net_device *ndev)
74{
75 int error;
76
77 /* Set config mode */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030078 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030079 /* Check if the operating mode is changed to the config mode */
80 error = ravb_wait(ndev, CSR, CSR_OPS, CSR_OPS_CONFIG);
81 if (error)
82 netdev_err(ndev, "failed to switch device to config mode\n");
83
84 return error;
85}
86
87static void ravb_set_duplex(struct net_device *ndev)
88{
89 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030090
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030091 ravb_modify(ndev, ECMR, ECMR_DM, priv->duplex ? ECMR_DM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030092}
93
94static void ravb_set_rate(struct net_device *ndev)
95{
96 struct ravb_private *priv = netdev_priv(ndev);
97
98 switch (priv->speed) {
99 case 100: /* 100BASE */
100 ravb_write(ndev, GECMR_SPEED_100, GECMR);
101 break;
102 case 1000: /* 1000BASE */
103 ravb_write(ndev, GECMR_SPEED_1000, GECMR);
104 break;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300105 }
106}
107
108static void ravb_set_buffer_align(struct sk_buff *skb)
109{
110 u32 reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1);
111
112 if (reserve)
113 skb_reserve(skb, RAVB_ALIGN - reserve);
114}
115
116/* Get MAC address from the MAC address registers
117 *
118 * Ethernet AVB device doesn't have ROM for MAC address.
119 * This function gets the MAC address that was used by a bootloader.
120 */
121static void ravb_read_mac_address(struct net_device *ndev, const u8 *mac)
122{
123 if (mac) {
124 ether_addr_copy(ndev->dev_addr, mac);
125 } else {
Sergei Shtylyovd9660632015-12-05 00:58:07 +0300126 u32 mahr = ravb_read(ndev, MAHR);
127 u32 malr = ravb_read(ndev, MALR);
128
129 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
130 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
131 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
132 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
133 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
134 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300135 }
136}
137
138static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
139{
140 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
141 mdiobb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300142
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300143 ravb_modify(priv->ndev, PIR, mask, set ? mask : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300144}
145
146/* MDC pin control */
147static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level)
148{
149 ravb_mdio_ctrl(ctrl, PIR_MDC, level);
150}
151
152/* Data I/O pin control */
153static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
154{
155 ravb_mdio_ctrl(ctrl, PIR_MMD, output);
156}
157
158/* Set data bit */
159static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
160{
161 ravb_mdio_ctrl(ctrl, PIR_MDO, value);
162}
163
164/* Get data bit */
165static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl)
166{
167 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
168 mdiobb);
169
170 return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0;
171}
172
173/* MDIO bus control struct */
174static struct mdiobb_ops bb_ops = {
175 .owner = THIS_MODULE,
176 .set_mdc = ravb_set_mdc,
177 .set_mdio_dir = ravb_set_mdio_dir,
178 .set_mdio_data = ravb_set_mdio_data,
179 .get_mdio_data = ravb_get_mdio_data,
180};
181
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100182/* Free TX skb function for AVB-IP */
183static int ravb_tx_free(struct net_device *ndev, int q, bool free_txed_only)
184{
185 struct ravb_private *priv = netdev_priv(ndev);
186 struct net_device_stats *stats = &priv->stats[q];
187 struct ravb_tx_desc *desc;
188 int free_num = 0;
189 int entry;
190 u32 size;
191
192 for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
193 bool txed;
194
195 entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
196 NUM_TX_DESC);
197 desc = &priv->tx_ring[q][entry];
198 txed = desc->die_dt == DT_FEMPTY;
199 if (free_txed_only && !txed)
200 break;
201 /* Descriptor type must be checked before all other reads */
202 dma_rmb();
203 size = le16_to_cpu(desc->ds_tagl) & TX_DS;
204 /* Free the original skb. */
205 if (priv->tx_skb[q][entry / NUM_TX_DESC]) {
206 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
207 size, DMA_TO_DEVICE);
208 /* Last packet descriptor? */
209 if (entry % NUM_TX_DESC == NUM_TX_DESC - 1) {
210 entry /= NUM_TX_DESC;
211 dev_kfree_skb_any(priv->tx_skb[q][entry]);
212 priv->tx_skb[q][entry] = NULL;
213 if (txed)
214 stats->tx_packets++;
215 }
216 free_num++;
217 }
218 if (txed)
219 stats->tx_bytes += size;
220 desc->die_dt = DT_EEMPTY;
221 }
222 return free_num;
223}
224
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300225/* Free skb's and DMA buffers for Ethernet AVB */
226static void ravb_ring_free(struct net_device *ndev, int q)
227{
228 struct ravb_private *priv = netdev_priv(ndev);
229 int ring_size;
230 int i;
231
232 /* Free RX skb ringbuffer */
233 if (priv->rx_skb[q]) {
234 for (i = 0; i < priv->num_rx_ring[q]; i++)
235 dev_kfree_skb(priv->rx_skb[q][i]);
236 }
237 kfree(priv->rx_skb[q]);
238 priv->rx_skb[q] = NULL;
239
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300240 /* Free aligned TX buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300241 kfree(priv->tx_align[q]);
242 priv->tx_align[q] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300243
244 if (priv->rx_ring[q]) {
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100245 for (i = 0; i < priv->num_rx_ring[q]; i++) {
246 struct ravb_ex_rx_desc *desc = &priv->rx_ring[q][i];
247
248 if (!dma_mapping_error(ndev->dev.parent,
249 le32_to_cpu(desc->dptr)))
250 dma_unmap_single(ndev->dev.parent,
251 le32_to_cpu(desc->dptr),
252 PKT_BUF_SZ,
253 DMA_FROM_DEVICE);
254 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300255 ring_size = sizeof(struct ravb_ex_rx_desc) *
256 (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900257 dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300258 priv->rx_desc_dma[q]);
259 priv->rx_ring[q] = NULL;
260 }
261
262 if (priv->tx_ring[q]) {
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100263 ravb_tx_free(ndev, q, false);
264
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300265 ring_size = sizeof(struct ravb_tx_desc) *
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300266 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900267 dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300268 priv->tx_desc_dma[q]);
269 priv->tx_ring[q] = NULL;
270 }
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100271
272 /* Free TX skb ringbuffer.
273 * SKBs are freed by ravb_tx_free() call above.
274 */
275 kfree(priv->tx_skb[q]);
276 priv->tx_skb[q] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300277}
278
279/* Format skb and descriptor buffer for Ethernet AVB */
280static void ravb_ring_format(struct net_device *ndev, int q)
281{
282 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300283 struct ravb_ex_rx_desc *rx_desc;
284 struct ravb_tx_desc *tx_desc;
285 struct ravb_desc *desc;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300286 int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q];
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300287 int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] *
288 NUM_TX_DESC;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300289 dma_addr_t dma_addr;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300290 int i;
291
292 priv->cur_rx[q] = 0;
293 priv->cur_tx[q] = 0;
294 priv->dirty_rx[q] = 0;
295 priv->dirty_tx[q] = 0;
296
297 memset(priv->rx_ring[q], 0, rx_ring_size);
298 /* Build RX ring buffer */
299 for (i = 0; i < priv->num_rx_ring[q]; i++) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300300 /* RX descriptor */
301 rx_desc = &priv->rx_ring[q][i];
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900302 rx_desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900303 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900304 PKT_BUF_SZ,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300305 DMA_FROM_DEVICE);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300306 /* We just set the data size to 0 for a failed mapping which
307 * should prevent DMA from happening...
308 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900309 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300310 rx_desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300311 rx_desc->dptr = cpu_to_le32(dma_addr);
312 rx_desc->die_dt = DT_FEMPTY;
313 }
314 rx_desc = &priv->rx_ring[q][i];
315 rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
316 rx_desc->die_dt = DT_LINKFIX; /* type */
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300317
318 memset(priv->tx_ring[q], 0, tx_ring_size);
319 /* Build TX ring buffer */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300320 for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
321 i++, tx_desc++) {
322 tx_desc->die_dt = DT_EEMPTY;
323 tx_desc++;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300324 tx_desc->die_dt = DT_EEMPTY;
325 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300326 tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
327 tx_desc->die_dt = DT_LINKFIX; /* type */
328
329 /* RX descriptor base address for best effort */
330 desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
331 desc->die_dt = DT_LINKFIX; /* type */
332 desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
333
334 /* TX descriptor base address for best effort */
335 desc = &priv->desc_bat[q];
336 desc->die_dt = DT_LINKFIX; /* type */
337 desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
338}
339
340/* Init skb and descriptor buffer for Ethernet AVB */
341static int ravb_ring_init(struct net_device *ndev, int q)
342{
343 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300344 struct sk_buff *skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300345 int ring_size;
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300346 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300347
348 /* Allocate RX and TX skb rings */
349 priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
350 sizeof(*priv->rx_skb[q]), GFP_KERNEL);
351 priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
352 sizeof(*priv->tx_skb[q]), GFP_KERNEL);
353 if (!priv->rx_skb[q] || !priv->tx_skb[q])
354 goto error;
355
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300356 for (i = 0; i < priv->num_rx_ring[q]; i++) {
357 skb = netdev_alloc_skb(ndev, PKT_BUF_SZ + RAVB_ALIGN - 1);
358 if (!skb)
359 goto error;
360 ravb_set_buffer_align(skb);
361 priv->rx_skb[q][i] = skb;
362 }
363
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300364 /* Allocate rings for the aligned buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300365 priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
366 DPTR_ALIGN - 1, GFP_KERNEL);
367 if (!priv->tx_align[q])
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300368 goto error;
369
370 /* Allocate all RX descriptors. */
371 ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900372 priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300373 &priv->rx_desc_dma[q],
374 GFP_KERNEL);
375 if (!priv->rx_ring[q])
376 goto error;
377
378 priv->dirty_rx[q] = 0;
379
380 /* Allocate all TX descriptors. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300381 ring_size = sizeof(struct ravb_tx_desc) *
382 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900383 priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300384 &priv->tx_desc_dma[q],
385 GFP_KERNEL);
386 if (!priv->tx_ring[q])
387 goto error;
388
389 return 0;
390
391error:
392 ravb_ring_free(ndev, q);
393
394 return -ENOMEM;
395}
396
397/* E-MAC init function */
398static void ravb_emac_init(struct net_device *ndev)
399{
400 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300401
402 /* Receive frame limit set register */
403 ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
404
405 /* PAUSE prohibition */
Sergei Shtylyov1c1fa822016-01-11 00:27:38 +0300406 ravb_write(ndev, ECMR_ZPF | (priv->duplex ? ECMR_DM : 0) |
407 ECMR_TE | ECMR_RE, ECMR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300408
409 ravb_set_rate(ndev);
410
411 /* Set MAC address */
412 ravb_write(ndev,
413 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
414 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
415 ravb_write(ndev,
416 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
417
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300418 /* E-MAC status register clear */
419 ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
420
421 /* E-MAC interrupt enable register */
422 ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
423}
424
425/* Device init function for Ethernet AVB */
426static int ravb_dmac_init(struct net_device *ndev)
427{
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900428 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300429 int error;
430
431 /* Set CONFIG mode */
432 error = ravb_config(ndev);
433 if (error)
434 return error;
435
436 error = ravb_ring_init(ndev, RAVB_BE);
437 if (error)
438 return error;
439 error = ravb_ring_init(ndev, RAVB_NC);
440 if (error) {
441 ravb_ring_free(ndev, RAVB_BE);
442 return error;
443 }
444
445 /* Descriptor format */
446 ravb_ring_format(ndev, RAVB_BE);
447 ravb_ring_format(ndev, RAVB_NC);
448
449#if defined(__LITTLE_ENDIAN)
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300450 ravb_modify(ndev, CCC, CCC_BOC, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300451#else
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300452 ravb_modify(ndev, CCC, CCC_BOC, CCC_BOC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300453#endif
454
455 /* Set AVB RX */
Masaru Nagai8d9c4182016-06-01 03:01:28 +0900456 ravb_write(ndev,
457 RCR_EFFS | RCR_ENCF | RCR_ETS0 | RCR_ESF | 0x18000000, RCR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300458
459 /* Set FIFO size */
460 ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00222200, TGC);
461
462 /* Timestamp enable */
463 ravb_write(ndev, TCCR_TFEN, TCCR);
464
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900465 /* Interrupt init: */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900466 if (priv->chip_id == RCAR_GEN3) {
467 /* Clear DIL.DPLx */
468 ravb_write(ndev, 0, DIL);
469 /* Set queue specific interrupt */
470 ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE);
471 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300472 /* Frame receive */
473 ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900474 /* Disable FIFO full warning */
475 ravb_write(ndev, 0, RIC1);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300476 /* Receive FIFO full error, descriptor empty */
477 ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
478 /* Frame transmitted, timestamp FIFO updated */
479 ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
480
481 /* Setting the control will start the AVB-DMAC process. */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300482 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300483
484 return 0;
485}
486
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300487static void ravb_get_tx_tstamp(struct net_device *ndev)
488{
489 struct ravb_private *priv = netdev_priv(ndev);
490 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
491 struct skb_shared_hwtstamps shhwtstamps;
492 struct sk_buff *skb;
493 struct timespec64 ts;
494 u16 tag, tfa_tag;
495 int count;
496 u32 tfa2;
497
498 count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
499 while (count--) {
500 tfa2 = ravb_read(ndev, TFA2);
501 tfa_tag = (tfa2 & TFA2_TST) >> 16;
502 ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
503 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
504 ravb_read(ndev, TFA1);
505 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
506 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
507 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
508 list) {
509 skb = ts_skb->skb;
510 tag = ts_skb->tag;
511 list_del(&ts_skb->list);
512 kfree(ts_skb);
513 if (tag == tfa_tag) {
514 skb_tstamp_tx(skb, &shhwtstamps);
515 break;
516 }
517 }
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300518 ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300519 }
520}
521
522/* Packet receive function for Ethernet AVB */
523static bool ravb_rx(struct net_device *ndev, int *quota, int q)
524{
525 struct ravb_private *priv = netdev_priv(ndev);
526 int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
527 int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
528 priv->cur_rx[q];
529 struct net_device_stats *stats = &priv->stats[q];
530 struct ravb_ex_rx_desc *desc;
531 struct sk_buff *skb;
532 dma_addr_t dma_addr;
533 struct timespec64 ts;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300534 u8 desc_status;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300535 u16 pkt_len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300536 int limit;
537
538 boguscnt = min(boguscnt, *quota);
539 limit = boguscnt;
540 desc = &priv->rx_ring[q][entry];
541 while (desc->die_dt != DT_FEMPTY) {
542 /* Descriptor type must be checked before all other reads */
543 dma_rmb();
544 desc_status = desc->msc;
545 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
546
547 if (--boguscnt < 0)
548 break;
549
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300550 /* We use 0-byte descriptors to mark the DMA mapping errors */
551 if (!pkt_len)
552 continue;
553
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300554 if (desc_status & MSC_MC)
555 stats->multicast++;
556
557 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
558 MSC_CEEF)) {
559 stats->rx_errors++;
560 if (desc_status & MSC_CRC)
561 stats->rx_crc_errors++;
562 if (desc_status & MSC_RFE)
563 stats->rx_frame_errors++;
564 if (desc_status & (MSC_RTLF | MSC_RTSF))
565 stats->rx_length_errors++;
566 if (desc_status & MSC_CEEF)
567 stats->rx_missed_errors++;
568 } else {
569 u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
570
571 skb = priv->rx_skb[q][entry];
572 priv->rx_skb[q][entry] = NULL;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900573 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900574 PKT_BUF_SZ,
Sergei Shtylyove2370f02015-07-15 00:56:52 +0300575 DMA_FROM_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300576 get_ts &= (q == RAVB_NC) ?
577 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
578 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
579 if (get_ts) {
580 struct skb_shared_hwtstamps *shhwtstamps;
581
582 shhwtstamps = skb_hwtstamps(skb);
583 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
584 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
585 32) | le32_to_cpu(desc->ts_sl);
586 ts.tv_nsec = le32_to_cpu(desc->ts_n);
587 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
588 }
589 skb_put(skb, pkt_len);
590 skb->protocol = eth_type_trans(skb, ndev);
591 napi_gro_receive(&priv->napi[q], skb);
592 stats->rx_packets++;
593 stats->rx_bytes += pkt_len;
594 }
595
596 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
597 desc = &priv->rx_ring[q][entry];
598 }
599
600 /* Refill the RX ring buffers. */
601 for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
602 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
603 desc = &priv->rx_ring[q][entry];
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900604 desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300605
606 if (!priv->rx_skb[q][entry]) {
607 skb = netdev_alloc_skb(ndev,
608 PKT_BUF_SZ + RAVB_ALIGN - 1);
609 if (!skb)
610 break; /* Better luck next round. */
611 ravb_set_buffer_align(skb);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900612 dma_addr = dma_map_single(ndev->dev.parent, skb->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300613 le16_to_cpu(desc->ds_cc),
614 DMA_FROM_DEVICE);
615 skb_checksum_none_assert(skb);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300616 /* We just set the data size to 0 for a failed mapping
617 * which should prevent DMA from happening...
618 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900619 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300620 desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300621 desc->dptr = cpu_to_le32(dma_addr);
622 priv->rx_skb[q][entry] = skb;
623 }
624 /* Descriptor type must be set after all the above writes */
625 dma_wmb();
626 desc->die_dt = DT_FEMPTY;
627 }
628
629 *quota -= limit - (++boguscnt);
630
631 return boguscnt <= 0;
632}
633
634static void ravb_rcv_snd_disable(struct net_device *ndev)
635{
636 /* Disable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300637 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300638}
639
640static void ravb_rcv_snd_enable(struct net_device *ndev)
641{
642 /* Enable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300643 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300644}
645
646/* function for waiting dma process finished */
647static int ravb_stop_dma(struct net_device *ndev)
648{
649 int error;
650
651 /* Wait for stopping the hardware TX process */
652 error = ravb_wait(ndev, TCCR,
653 TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
654 if (error)
655 return error;
656
657 error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
658 0);
659 if (error)
660 return error;
661
662 /* Stop the E-MAC's RX/TX processes. */
663 ravb_rcv_snd_disable(ndev);
664
665 /* Wait for stopping the RX DMA process */
666 error = ravb_wait(ndev, CSR, CSR_RPO, 0);
667 if (error)
668 return error;
669
670 /* Stop AVB-DMAC process */
671 return ravb_config(ndev);
672}
673
674/* E-MAC interrupt handler */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900675static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300676{
677 struct ravb_private *priv = netdev_priv(ndev);
678 u32 ecsr, psr;
679
680 ecsr = ravb_read(ndev, ECSR);
681 ravb_write(ndev, ecsr, ECSR); /* clear interrupt */
682 if (ecsr & ECSR_ICD)
683 ndev->stats.tx_carrier_errors++;
684 if (ecsr & ECSR_LCHNG) {
685 /* Link changed */
686 if (priv->no_avb_link)
687 return;
688 psr = ravb_read(ndev, PSR);
689 if (priv->avb_link_active_low)
690 psr ^= PSR_LMON;
691 if (!(psr & PSR_LMON)) {
692 /* DIsable RX and TX */
693 ravb_rcv_snd_disable(ndev);
694 } else {
695 /* Enable RX and TX */
696 ravb_rcv_snd_enable(ndev);
697 }
698 }
699}
700
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900701static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
702{
703 struct net_device *ndev = dev_id;
704 struct ravb_private *priv = netdev_priv(ndev);
705
706 spin_lock(&priv->lock);
707 ravb_emac_interrupt_unlocked(ndev);
708 mmiowb();
709 spin_unlock(&priv->lock);
710 return IRQ_HANDLED;
711}
712
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300713/* Error interrupt handler */
714static void ravb_error_interrupt(struct net_device *ndev)
715{
716 struct ravb_private *priv = netdev_priv(ndev);
717 u32 eis, ris2;
718
719 eis = ravb_read(ndev, EIS);
720 ravb_write(ndev, ~EIS_QFS, EIS);
721 if (eis & EIS_QFS) {
722 ris2 = ravb_read(ndev, RIS2);
723 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF), RIS2);
724
725 /* Receive Descriptor Empty int */
726 if (ris2 & RIS2_QFF0)
727 priv->stats[RAVB_BE].rx_over_errors++;
728
729 /* Receive Descriptor Empty int */
730 if (ris2 & RIS2_QFF1)
731 priv->stats[RAVB_NC].rx_over_errors++;
732
733 /* Receive FIFO Overflow int */
734 if (ris2 & RIS2_RFFF)
735 priv->rx_fifo_errors++;
736 }
737}
738
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900739static bool ravb_queue_interrupt(struct net_device *ndev, int q)
740{
741 struct ravb_private *priv = netdev_priv(ndev);
742 u32 ris0 = ravb_read(ndev, RIS0);
743 u32 ric0 = ravb_read(ndev, RIC0);
744 u32 tis = ravb_read(ndev, TIS);
745 u32 tic = ravb_read(ndev, TIC);
746
747 if (((ris0 & ric0) & BIT(q)) || ((tis & tic) & BIT(q))) {
748 if (napi_schedule_prep(&priv->napi[q])) {
749 /* Mask RX and TX interrupts */
750 if (priv->chip_id == RCAR_GEN2) {
751 ravb_write(ndev, ric0 & ~BIT(q), RIC0);
752 ravb_write(ndev, tic & ~BIT(q), TIC);
753 } else {
754 ravb_write(ndev, BIT(q), RID0);
755 ravb_write(ndev, BIT(q), TID);
756 }
757 __napi_schedule(&priv->napi[q]);
758 } else {
759 netdev_warn(ndev,
760 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
761 ris0, ric0);
762 netdev_warn(ndev,
763 " tx status 0x%08x, tx mask 0x%08x.\n",
764 tis, tic);
765 }
766 return true;
767 }
768 return false;
769}
770
771static bool ravb_timestamp_interrupt(struct net_device *ndev)
772{
773 u32 tis = ravb_read(ndev, TIS);
774
775 if (tis & TIS_TFUF) {
776 ravb_write(ndev, ~TIS_TFUF, TIS);
777 ravb_get_tx_tstamp(ndev);
778 return true;
779 }
780 return false;
781}
782
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300783static irqreturn_t ravb_interrupt(int irq, void *dev_id)
784{
785 struct net_device *ndev = dev_id;
786 struct ravb_private *priv = netdev_priv(ndev);
787 irqreturn_t result = IRQ_NONE;
788 u32 iss;
789
790 spin_lock(&priv->lock);
791 /* Get interrupt status */
792 iss = ravb_read(ndev, ISS);
793
794 /* Received and transmitted interrupts */
795 if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300796 int q;
797
798 /* Timestamp updated */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900799 if (ravb_timestamp_interrupt(ndev))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300800 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300801
802 /* Network control and best effort queue RX/TX */
803 for (q = RAVB_NC; q >= RAVB_BE; q--) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900804 if (ravb_queue_interrupt(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300805 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300806 }
807 }
808
809 /* E-MAC status summary */
810 if (iss & ISS_MS) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900811 ravb_emac_interrupt_unlocked(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300812 result = IRQ_HANDLED;
813 }
814
815 /* Error status summary */
816 if (iss & ISS_ES) {
817 ravb_error_interrupt(ndev);
818 result = IRQ_HANDLED;
819 }
820
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900821 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300822 if (iss & ISS_CGIS) {
823 ravb_ptp_interrupt(ndev);
Yoshihiro Kaneko38c848c2016-03-16 00:52:16 +0900824 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300825 }
Sergei Shtylyova0d2f202015-06-11 01:02:30 +0300826
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300827 mmiowb();
828 spin_unlock(&priv->lock);
829 return result;
830}
831
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900832/* Timestamp/Error/gPTP interrupt handler */
833static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
834{
835 struct net_device *ndev = dev_id;
836 struct ravb_private *priv = netdev_priv(ndev);
837 irqreturn_t result = IRQ_NONE;
838 u32 iss;
839
840 spin_lock(&priv->lock);
841 /* Get interrupt status */
842 iss = ravb_read(ndev, ISS);
843
844 /* Timestamp updated */
845 if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
846 result = IRQ_HANDLED;
847
848 /* Error status summary */
849 if (iss & ISS_ES) {
850 ravb_error_interrupt(ndev);
851 result = IRQ_HANDLED;
852 }
853
854 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300855 if (iss & ISS_CGIS) {
856 ravb_ptp_interrupt(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900857 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300858 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900859
860 mmiowb();
861 spin_unlock(&priv->lock);
862 return result;
863}
864
865static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
866{
867 struct net_device *ndev = dev_id;
868 struct ravb_private *priv = netdev_priv(ndev);
869 irqreturn_t result = IRQ_NONE;
870
871 spin_lock(&priv->lock);
872
873 /* Network control/Best effort queue RX/TX */
874 if (ravb_queue_interrupt(ndev, q))
875 result = IRQ_HANDLED;
876
877 mmiowb();
878 spin_unlock(&priv->lock);
879 return result;
880}
881
882static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
883{
884 return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
885}
886
887static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
888{
889 return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
890}
891
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300892static int ravb_poll(struct napi_struct *napi, int budget)
893{
894 struct net_device *ndev = napi->dev;
895 struct ravb_private *priv = netdev_priv(ndev);
896 unsigned long flags;
897 int q = napi - priv->napi;
898 int mask = BIT(q);
899 int quota = budget;
900 u32 ris0, tis;
901
902 for (;;) {
903 tis = ravb_read(ndev, TIS);
904 ris0 = ravb_read(ndev, RIS0);
905 if (!((ris0 & mask) || (tis & mask)))
906 break;
907
908 /* Processing RX Descriptor Ring */
909 if (ris0 & mask) {
910 /* Clear RX interrupt */
911 ravb_write(ndev, ~mask, RIS0);
912 if (ravb_rx(ndev, &quota, q))
913 goto out;
914 }
915 /* Processing TX Descriptor Ring */
916 if (tis & mask) {
917 spin_lock_irqsave(&priv->lock, flags);
918 /* Clear TX interrupt */
919 ravb_write(ndev, ~mask, TIS);
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100920 ravb_tx_free(ndev, q, true);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300921 netif_wake_subqueue(ndev, q);
922 mmiowb();
923 spin_unlock_irqrestore(&priv->lock, flags);
924 }
925 }
926
927 napi_complete(napi);
928
929 /* Re-enable RX/TX interrupts */
930 spin_lock_irqsave(&priv->lock, flags);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900931 if (priv->chip_id == RCAR_GEN2) {
932 ravb_modify(ndev, RIC0, mask, mask);
933 ravb_modify(ndev, TIC, mask, mask);
934 } else {
935 ravb_write(ndev, mask, RIE0);
936 ravb_write(ndev, mask, TIE);
937 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300938 mmiowb();
939 spin_unlock_irqrestore(&priv->lock, flags);
940
941 /* Receive error message handling */
942 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
943 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
Kazuya Mizuguchi18a3ed52017-01-12 13:21:06 +0100944 if (priv->rx_over_errors != ndev->stats.rx_over_errors)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300945 ndev->stats.rx_over_errors = priv->rx_over_errors;
Kazuya Mizuguchi18a3ed52017-01-12 13:21:06 +0100946 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300947 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300948out:
949 return budget - quota;
950}
951
952/* PHY state control function */
953static void ravb_adjust_link(struct net_device *ndev)
954{
955 struct ravb_private *priv = netdev_priv(ndev);
Philippe Reynes0f635172016-08-20 00:52:18 +0200956 struct phy_device *phydev = ndev->phydev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300957 bool new_state = false;
958
959 if (phydev->link) {
960 if (phydev->duplex != priv->duplex) {
961 new_state = true;
962 priv->duplex = phydev->duplex;
963 ravb_set_duplex(ndev);
964 }
965
966 if (phydev->speed != priv->speed) {
967 new_state = true;
968 priv->speed = phydev->speed;
969 ravb_set_rate(ndev);
970 }
971 if (!priv->link) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300972 ravb_modify(ndev, ECMR, ECMR_TXF, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300973 new_state = true;
974 priv->link = phydev->link;
975 if (priv->no_avb_link)
976 ravb_rcv_snd_enable(ndev);
977 }
978 } else if (priv->link) {
979 new_state = true;
980 priv->link = 0;
981 priv->speed = 0;
982 priv->duplex = -1;
983 if (priv->no_avb_link)
984 ravb_rcv_snd_disable(ndev);
985 }
986
987 if (new_state && netif_msg_link(priv))
988 phy_print_status(phydev);
989}
990
991/* PHY init function */
992static int ravb_phy_init(struct net_device *ndev)
993{
994 struct device_node *np = ndev->dev.parent->of_node;
995 struct ravb_private *priv = netdev_priv(ndev);
996 struct phy_device *phydev;
997 struct device_node *pn;
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900998 int err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300999
1000 priv->link = 0;
1001 priv->speed = 0;
1002 priv->duplex = -1;
1003
1004 /* Try connecting to PHY */
1005 pn = of_parse_phandle(np, "phy-handle", 0);
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +09001006 if (!pn) {
1007 /* In the case of a fixed PHY, the DT node associated
1008 * to the PHY is the Ethernet MAC DT node.
1009 */
1010 if (of_phy_is_fixed_link(np)) {
1011 err = of_phy_register_fixed_link(np);
1012 if (err)
1013 return err;
1014 }
1015 pn = of_node_get(np);
1016 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001017 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
1018 priv->phy_interface);
Peter Chenc9b1eb82016-08-01 15:02:39 +08001019 of_node_put(pn);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001020 if (!phydev) {
1021 netdev_err(ndev, "failed to connect PHY\n");
Johan Hovold9f70eb32016-11-28 19:25:06 +01001022 err = -ENOENT;
1023 goto err_deregister_fixed_link;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001024 }
1025
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001026 /* This driver only support 10/100Mbit speeds on Gen3
1027 * at this time.
1028 */
1029 if (priv->chip_id == RCAR_GEN3) {
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001030 err = phy_set_max_speed(phydev, SPEED_100);
1031 if (err) {
1032 netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
Johan Hovold9f70eb32016-11-28 19:25:06 +01001033 goto err_phy_disconnect;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001034 }
1035
1036 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
1037 }
1038
Kazuya Mizuguchi54499962015-12-14 00:15:58 +09001039 /* 10BASE is not supported */
1040 phydev->supported &= ~PHY_10BT_FEATURES;
1041
Andrew Lunn22209432016-01-06 20:11:13 +01001042 phy_attached_info(phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001043
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001044 return 0;
Johan Hovold9f70eb32016-11-28 19:25:06 +01001045
1046err_phy_disconnect:
1047 phy_disconnect(phydev);
1048err_deregister_fixed_link:
1049 if (of_phy_is_fixed_link(np))
1050 of_phy_deregister_fixed_link(np);
1051
1052 return err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001053}
1054
1055/* PHY control start function */
1056static int ravb_phy_start(struct net_device *ndev)
1057{
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001058 int error;
1059
1060 error = ravb_phy_init(ndev);
1061 if (error)
1062 return error;
1063
Philippe Reynes0f635172016-08-20 00:52:18 +02001064 phy_start(ndev->phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001065
1066 return 0;
1067}
1068
Philippe Reynes04462f22016-08-20 00:52:19 +02001069static int ravb_get_link_ksettings(struct net_device *ndev,
1070 struct ethtool_link_ksettings *cmd)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001071{
1072 struct ravb_private *priv = netdev_priv(ndev);
1073 int error = -ENODEV;
1074 unsigned long flags;
1075
Philippe Reynes0f635172016-08-20 00:52:18 +02001076 if (ndev->phydev) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001077 spin_lock_irqsave(&priv->lock, flags);
Philippe Reynes04462f22016-08-20 00:52:19 +02001078 error = phy_ethtool_ksettings_get(ndev->phydev, cmd);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001079 spin_unlock_irqrestore(&priv->lock, flags);
1080 }
1081
1082 return error;
1083}
1084
Philippe Reynes04462f22016-08-20 00:52:19 +02001085static int ravb_set_link_ksettings(struct net_device *ndev,
1086 const struct ethtool_link_ksettings *cmd)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001087{
1088 struct ravb_private *priv = netdev_priv(ndev);
1089 unsigned long flags;
1090 int error;
1091
Philippe Reynes0f635172016-08-20 00:52:18 +02001092 if (!ndev->phydev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001093 return -ENODEV;
1094
1095 spin_lock_irqsave(&priv->lock, flags);
1096
1097 /* Disable TX and RX */
1098 ravb_rcv_snd_disable(ndev);
1099
Philippe Reynes04462f22016-08-20 00:52:19 +02001100 error = phy_ethtool_ksettings_set(ndev->phydev, cmd);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001101 if (error)
1102 goto error_exit;
1103
Philippe Reynes04462f22016-08-20 00:52:19 +02001104 if (cmd->base.duplex == DUPLEX_FULL)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001105 priv->duplex = 1;
1106 else
1107 priv->duplex = 0;
1108
1109 ravb_set_duplex(ndev);
1110
1111error_exit:
1112 mdelay(1);
1113
1114 /* Enable TX and RX */
1115 ravb_rcv_snd_enable(ndev);
1116
1117 mmiowb();
1118 spin_unlock_irqrestore(&priv->lock, flags);
1119
1120 return error;
1121}
1122
1123static int ravb_nway_reset(struct net_device *ndev)
1124{
1125 struct ravb_private *priv = netdev_priv(ndev);
1126 int error = -ENODEV;
1127 unsigned long flags;
1128
Philippe Reynes0f635172016-08-20 00:52:18 +02001129 if (ndev->phydev) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001130 spin_lock_irqsave(&priv->lock, flags);
Philippe Reynes0f635172016-08-20 00:52:18 +02001131 error = phy_start_aneg(ndev->phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001132 spin_unlock_irqrestore(&priv->lock, flags);
1133 }
1134
1135 return error;
1136}
1137
1138static u32 ravb_get_msglevel(struct net_device *ndev)
1139{
1140 struct ravb_private *priv = netdev_priv(ndev);
1141
1142 return priv->msg_enable;
1143}
1144
1145static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1146{
1147 struct ravb_private *priv = netdev_priv(ndev);
1148
1149 priv->msg_enable = value;
1150}
1151
1152static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1153 "rx_queue_0_current",
1154 "tx_queue_0_current",
1155 "rx_queue_0_dirty",
1156 "tx_queue_0_dirty",
1157 "rx_queue_0_packets",
1158 "tx_queue_0_packets",
1159 "rx_queue_0_bytes",
1160 "tx_queue_0_bytes",
1161 "rx_queue_0_mcast_packets",
1162 "rx_queue_0_errors",
1163 "rx_queue_0_crc_errors",
1164 "rx_queue_0_frame_errors",
1165 "rx_queue_0_length_errors",
1166 "rx_queue_0_missed_errors",
1167 "rx_queue_0_over_errors",
1168
1169 "rx_queue_1_current",
1170 "tx_queue_1_current",
1171 "rx_queue_1_dirty",
1172 "tx_queue_1_dirty",
1173 "rx_queue_1_packets",
1174 "tx_queue_1_packets",
1175 "rx_queue_1_bytes",
1176 "tx_queue_1_bytes",
1177 "rx_queue_1_mcast_packets",
1178 "rx_queue_1_errors",
1179 "rx_queue_1_crc_errors",
Sergei Shtylyovb17c1d92015-12-04 01:51:10 +03001180 "rx_queue_1_frame_errors",
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001181 "rx_queue_1_length_errors",
1182 "rx_queue_1_missed_errors",
1183 "rx_queue_1_over_errors",
1184};
1185
1186#define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1187
1188static int ravb_get_sset_count(struct net_device *netdev, int sset)
1189{
1190 switch (sset) {
1191 case ETH_SS_STATS:
1192 return RAVB_STATS_LEN;
1193 default:
1194 return -EOPNOTSUPP;
1195 }
1196}
1197
1198static void ravb_get_ethtool_stats(struct net_device *ndev,
1199 struct ethtool_stats *stats, u64 *data)
1200{
1201 struct ravb_private *priv = netdev_priv(ndev);
1202 int i = 0;
1203 int q;
1204
1205 /* Device-specific stats */
1206 for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1207 struct net_device_stats *stats = &priv->stats[q];
1208
1209 data[i++] = priv->cur_rx[q];
1210 data[i++] = priv->cur_tx[q];
1211 data[i++] = priv->dirty_rx[q];
1212 data[i++] = priv->dirty_tx[q];
1213 data[i++] = stats->rx_packets;
1214 data[i++] = stats->tx_packets;
1215 data[i++] = stats->rx_bytes;
1216 data[i++] = stats->tx_bytes;
1217 data[i++] = stats->multicast;
1218 data[i++] = stats->rx_errors;
1219 data[i++] = stats->rx_crc_errors;
1220 data[i++] = stats->rx_frame_errors;
1221 data[i++] = stats->rx_length_errors;
1222 data[i++] = stats->rx_missed_errors;
1223 data[i++] = stats->rx_over_errors;
1224 }
1225}
1226
1227static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1228{
1229 switch (stringset) {
1230 case ETH_SS_STATS:
1231 memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
1232 break;
1233 }
1234}
1235
1236static void ravb_get_ringparam(struct net_device *ndev,
1237 struct ethtool_ringparam *ring)
1238{
1239 struct ravb_private *priv = netdev_priv(ndev);
1240
1241 ring->rx_max_pending = BE_RX_RING_MAX;
1242 ring->tx_max_pending = BE_TX_RING_MAX;
1243 ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1244 ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1245}
1246
1247static int ravb_set_ringparam(struct net_device *ndev,
1248 struct ethtool_ringparam *ring)
1249{
1250 struct ravb_private *priv = netdev_priv(ndev);
1251 int error;
1252
1253 if (ring->tx_pending > BE_TX_RING_MAX ||
1254 ring->rx_pending > BE_RX_RING_MAX ||
1255 ring->tx_pending < BE_TX_RING_MIN ||
1256 ring->rx_pending < BE_RX_RING_MIN)
1257 return -EINVAL;
1258 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1259 return -EINVAL;
1260
1261 if (netif_running(ndev)) {
1262 netif_device_detach(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001263 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001264 if (priv->chip_id == RCAR_GEN2)
1265 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001266 /* Wait for DMA stopping */
1267 error = ravb_stop_dma(ndev);
1268 if (error) {
1269 netdev_err(ndev,
1270 "cannot set ringparam! Any AVB processes are still running?\n");
1271 return error;
1272 }
1273 synchronize_irq(ndev->irq);
1274
1275 /* Free all the skb's in the RX queue and the DMA buffers. */
1276 ravb_ring_free(ndev, RAVB_BE);
1277 ravb_ring_free(ndev, RAVB_NC);
1278 }
1279
1280 /* Set new parameters */
1281 priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1282 priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1283
1284 if (netif_running(ndev)) {
1285 error = ravb_dmac_init(ndev);
1286 if (error) {
1287 netdev_err(ndev,
1288 "%s: ravb_dmac_init() failed, error %d\n",
1289 __func__, error);
1290 return error;
1291 }
1292
1293 ravb_emac_init(ndev);
1294
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001295 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001296 if (priv->chip_id == RCAR_GEN2)
1297 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001298
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001299 netif_device_attach(ndev);
1300 }
1301
1302 return 0;
1303}
1304
1305static int ravb_get_ts_info(struct net_device *ndev,
1306 struct ethtool_ts_info *info)
1307{
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001308 struct ravb_private *priv = netdev_priv(ndev);
1309
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001310 info->so_timestamping =
1311 SOF_TIMESTAMPING_TX_SOFTWARE |
1312 SOF_TIMESTAMPING_RX_SOFTWARE |
1313 SOF_TIMESTAMPING_SOFTWARE |
1314 SOF_TIMESTAMPING_TX_HARDWARE |
1315 SOF_TIMESTAMPING_RX_HARDWARE |
1316 SOF_TIMESTAMPING_RAW_HARDWARE;
1317 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1318 info->rx_filters =
1319 (1 << HWTSTAMP_FILTER_NONE) |
1320 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1321 (1 << HWTSTAMP_FILTER_ALL);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001322 info->phc_index = ptp_clock_index(priv->ptp.clock);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001323
1324 return 0;
1325}
1326
1327static const struct ethtool_ops ravb_ethtool_ops = {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001328 .nway_reset = ravb_nway_reset,
1329 .get_msglevel = ravb_get_msglevel,
1330 .set_msglevel = ravb_set_msglevel,
1331 .get_link = ethtool_op_get_link,
1332 .get_strings = ravb_get_strings,
1333 .get_ethtool_stats = ravb_get_ethtool_stats,
1334 .get_sset_count = ravb_get_sset_count,
1335 .get_ringparam = ravb_get_ringparam,
1336 .set_ringparam = ravb_set_ringparam,
1337 .get_ts_info = ravb_get_ts_info,
Philippe Reynes04462f22016-08-20 00:52:19 +02001338 .get_link_ksettings = ravb_get_link_ksettings,
1339 .set_link_ksettings = ravb_set_link_ksettings,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001340};
1341
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001342static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1343 struct net_device *ndev, struct device *dev,
1344 const char *ch)
1345{
1346 char *name;
1347 int error;
1348
1349 name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1350 if (!name)
1351 return -ENOMEM;
1352 error = request_irq(irq, handler, 0, name, ndev);
1353 if (error)
1354 netdev_err(ndev, "cannot request IRQ %s\n", name);
1355
1356 return error;
1357}
1358
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001359/* Network device open function for Ethernet AVB */
1360static int ravb_open(struct net_device *ndev)
1361{
1362 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001363 struct platform_device *pdev = priv->pdev;
1364 struct device *dev = &pdev->dev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001365 int error;
1366
1367 napi_enable(&priv->napi[RAVB_BE]);
1368 napi_enable(&priv->napi[RAVB_NC]);
1369
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001370 if (priv->chip_id == RCAR_GEN2) {
1371 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1372 ndev->name, ndev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001373 if (error) {
1374 netdev_err(ndev, "cannot request IRQ\n");
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001375 goto out_napi_off;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001376 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001377 } else {
1378 error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1379 dev, "ch22:multi");
1380 if (error)
1381 goto out_napi_off;
1382 error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1383 dev, "ch24:emac");
1384 if (error)
1385 goto out_free_irq;
1386 error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1387 ndev, dev, "ch0:rx_be");
1388 if (error)
1389 goto out_free_irq_emac;
1390 error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1391 ndev, dev, "ch18:tx_be");
1392 if (error)
1393 goto out_free_irq_be_rx;
1394 error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1395 ndev, dev, "ch1:rx_nc");
1396 if (error)
1397 goto out_free_irq_be_tx;
1398 error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1399 ndev, dev, "ch19:tx_nc");
1400 if (error)
1401 goto out_free_irq_nc_rx;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001402 }
1403
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001404 /* Device init */
1405 error = ravb_dmac_init(ndev);
1406 if (error)
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001407 goto out_free_irq_nc_tx;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001408 ravb_emac_init(ndev);
1409
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001410 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001411 if (priv->chip_id == RCAR_GEN2)
1412 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001413
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001414 netif_tx_start_all_queues(ndev);
1415
1416 /* PHY control start */
1417 error = ravb_phy_start(ndev);
1418 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001419 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001420
1421 return 0;
1422
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001423out_ptp_stop:
1424 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001425 if (priv->chip_id == RCAR_GEN2)
1426 ravb_ptp_stop(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001427out_free_irq_nc_tx:
1428 if (priv->chip_id == RCAR_GEN2)
1429 goto out_free_irq;
1430 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1431out_free_irq_nc_rx:
1432 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1433out_free_irq_be_tx:
1434 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1435out_free_irq_be_rx:
1436 free_irq(priv->rx_irqs[RAVB_BE], ndev);
1437out_free_irq_emac:
1438 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001439out_free_irq:
1440 free_irq(ndev->irq, ndev);
1441out_napi_off:
1442 napi_disable(&priv->napi[RAVB_NC]);
1443 napi_disable(&priv->napi[RAVB_BE]);
1444 return error;
1445}
1446
1447/* Timeout function for Ethernet AVB */
1448static void ravb_tx_timeout(struct net_device *ndev)
1449{
1450 struct ravb_private *priv = netdev_priv(ndev);
1451
1452 netif_err(priv, tx_err, ndev,
1453 "transmit timed out, status %08x, resetting...\n",
1454 ravb_read(ndev, ISS));
1455
1456 /* tx_errors count up */
1457 ndev->stats.tx_errors++;
1458
1459 schedule_work(&priv->work);
1460}
1461
1462static void ravb_tx_timeout_work(struct work_struct *work)
1463{
1464 struct ravb_private *priv = container_of(work, struct ravb_private,
1465 work);
1466 struct net_device *ndev = priv->ndev;
1467
1468 netif_tx_stop_all_queues(ndev);
1469
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001470 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001471 if (priv->chip_id == RCAR_GEN2)
1472 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001473
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001474 /* Wait for DMA stopping */
1475 ravb_stop_dma(ndev);
1476
1477 ravb_ring_free(ndev, RAVB_BE);
1478 ravb_ring_free(ndev, RAVB_NC);
1479
1480 /* Device init */
1481 ravb_dmac_init(ndev);
1482 ravb_emac_init(ndev);
1483
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001484 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001485 if (priv->chip_id == RCAR_GEN2)
1486 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001487
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001488 netif_tx_start_all_queues(ndev);
1489}
1490
1491/* Packet transmit function for Ethernet AVB */
1492static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1493{
1494 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001495 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001496 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001497 struct ravb_tx_desc *desc;
1498 unsigned long flags;
1499 u32 dma_addr;
1500 void *buffer;
1501 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001502 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001503
1504 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001505 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1506 NUM_TX_DESC) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001507 netif_err(priv, tx_queued, ndev,
1508 "still transmitting with the full ring!\n");
1509 netif_stop_subqueue(ndev, q);
1510 spin_unlock_irqrestore(&priv->lock, flags);
1511 return NETDEV_TX_BUSY;
1512 }
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001513 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1514 priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001515
1516 if (skb_put_padto(skb, ETH_ZLEN))
1517 goto drop;
1518
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001519 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1520 entry / NUM_TX_DESC * DPTR_ALIGN;
1521 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
Masaru Nagai8ec3e8a2017-01-16 11:45:21 +01001522 /* Zero length DMA descriptors are problematic as they seem to
1523 * terminate DMA transfers. Avoid them by simply using a length of
1524 * DPTR_ALIGN (4) when skb data is aligned to DPTR_ALIGN.
1525 *
1526 * As skb is guaranteed to have at least ETH_ZLEN (60) bytes of
1527 * data by the call to skb_put_padto() above this is safe with
1528 * respect to both the length of the first DMA descriptor (len)
1529 * overflowing the available data and the length of the second DMA
1530 * descriptor (skb->len - len) being negative.
1531 */
1532 if (len == 0)
1533 len = DPTR_ALIGN;
1534
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001535 memcpy(buffer, skb->data, len);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001536 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1537 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001538 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001539
1540 desc = &priv->tx_ring[q][entry];
1541 desc->ds_tagl = cpu_to_le16(len);
1542 desc->dptr = cpu_to_le32(dma_addr);
1543
1544 buffer = skb->data + len;
1545 len = skb->len - len;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001546 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1547 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001548 goto unmap;
1549
1550 desc++;
1551 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001552 desc->dptr = cpu_to_le32(dma_addr);
1553
1554 /* TX timestamp required */
1555 if (q == RAVB_NC) {
1556 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1557 if (!ts_skb) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001558 desc--;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001559 dma_unmap_single(ndev->dev.parent, dma_addr, len,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001560 DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001561 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001562 }
1563 ts_skb->skb = skb;
1564 ts_skb->tag = priv->ts_skb_tag++;
1565 priv->ts_skb_tag &= 0x3ff;
1566 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1567
1568 /* TAG and timestamp required flag */
1569 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001570 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1571 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1572 }
1573
Lino Sanfilippod7be81a2016-03-27 12:22:02 +02001574 skb_tx_timestamp(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001575 /* Descriptor type must be set after all the above writes */
1576 dma_wmb();
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001577 desc->die_dt = DT_FEND;
1578 desc--;
1579 desc->die_dt = DT_FSTART;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001580
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001581 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001582
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001583 priv->cur_tx[q] += NUM_TX_DESC;
1584 if (priv->cur_tx[q] - priv->dirty_tx[q] >
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +01001585 (priv->num_tx_ring[q] - 1) * NUM_TX_DESC &&
1586 !ravb_tx_free(ndev, q, true))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001587 netif_stop_subqueue(ndev, q);
1588
1589exit:
1590 mmiowb();
1591 spin_unlock_irqrestore(&priv->lock, flags);
1592 return NETDEV_TX_OK;
1593
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001594unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001595 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001596 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001597drop:
1598 dev_kfree_skb_any(skb);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001599 priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001600 goto exit;
1601}
1602
1603static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1604 void *accel_priv, select_queue_fallback_t fallback)
1605{
1606 /* If skb needs TX timestamp, it is handled in network control queue */
1607 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1608 RAVB_BE;
1609
1610}
1611
1612static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1613{
1614 struct ravb_private *priv = netdev_priv(ndev);
1615 struct net_device_stats *nstats, *stats0, *stats1;
1616
1617 nstats = &ndev->stats;
1618 stats0 = &priv->stats[RAVB_BE];
1619 stats1 = &priv->stats[RAVB_NC];
1620
1621 nstats->tx_dropped += ravb_read(ndev, TROCR);
1622 ravb_write(ndev, 0, TROCR); /* (write clear) */
1623 nstats->collisions += ravb_read(ndev, CDCR);
1624 ravb_write(ndev, 0, CDCR); /* (write clear) */
1625 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1626 ravb_write(ndev, 0, LCCR); /* (write clear) */
1627
1628 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1629 ravb_write(ndev, 0, CERCR); /* (write clear) */
1630 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1631 ravb_write(ndev, 0, CEECR); /* (write clear) */
1632
1633 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1634 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1635 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1636 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1637 nstats->multicast = stats0->multicast + stats1->multicast;
1638 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1639 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1640 nstats->rx_frame_errors =
1641 stats0->rx_frame_errors + stats1->rx_frame_errors;
1642 nstats->rx_length_errors =
1643 stats0->rx_length_errors + stats1->rx_length_errors;
1644 nstats->rx_missed_errors =
1645 stats0->rx_missed_errors + stats1->rx_missed_errors;
1646 nstats->rx_over_errors =
1647 stats0->rx_over_errors + stats1->rx_over_errors;
1648
1649 return nstats;
1650}
1651
1652/* Update promiscuous bit */
1653static void ravb_set_rx_mode(struct net_device *ndev)
1654{
1655 struct ravb_private *priv = netdev_priv(ndev);
1656 unsigned long flags;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001657
1658 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001659 ravb_modify(ndev, ECMR, ECMR_PRM,
1660 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001661 mmiowb();
1662 spin_unlock_irqrestore(&priv->lock, flags);
1663}
1664
1665/* Device close function for Ethernet AVB */
1666static int ravb_close(struct net_device *ndev)
1667{
Johan Hovold9f70eb32016-11-28 19:25:06 +01001668 struct device_node *np = ndev->dev.parent->of_node;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001669 struct ravb_private *priv = netdev_priv(ndev);
1670 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1671
1672 netif_tx_stop_all_queues(ndev);
1673
1674 /* Disable interrupts by clearing the interrupt masks. */
1675 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001676 ravb_write(ndev, 0, RIC2);
1677 ravb_write(ndev, 0, TIC);
1678
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001679 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001680 if (priv->chip_id == RCAR_GEN2)
1681 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001682
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001683 /* Set the config mode to stop the AVB-DMAC's processes */
1684 if (ravb_stop_dma(ndev) < 0)
1685 netdev_err(ndev,
1686 "device will be stopped after h/w processes are done.\n");
1687
1688 /* Clear the timestamp list */
1689 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1690 list_del(&ts_skb->list);
1691 kfree(ts_skb);
1692 }
1693
1694 /* PHY disconnect */
Philippe Reynes0f635172016-08-20 00:52:18 +02001695 if (ndev->phydev) {
1696 phy_stop(ndev->phydev);
1697 phy_disconnect(ndev->phydev);
Johan Hovold9f70eb32016-11-28 19:25:06 +01001698 if (of_phy_is_fixed_link(np))
1699 of_phy_deregister_fixed_link(np);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001700 }
1701
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001702 if (priv->chip_id != RCAR_GEN2) {
1703 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1704 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1705 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1706 free_irq(priv->rx_irqs[RAVB_BE], ndev);
Geert Uytterhoeven7fa816b2016-05-07 13:17:11 +02001707 free_irq(priv->emac_irq, ndev);
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001708 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001709 free_irq(ndev->irq, ndev);
1710
1711 napi_disable(&priv->napi[RAVB_NC]);
1712 napi_disable(&priv->napi[RAVB_BE]);
1713
1714 /* Free all the skb's in the RX queue and the DMA buffers. */
1715 ravb_ring_free(ndev, RAVB_BE);
1716 ravb_ring_free(ndev, RAVB_NC);
1717
1718 return 0;
1719}
1720
1721static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1722{
1723 struct ravb_private *priv = netdev_priv(ndev);
1724 struct hwtstamp_config config;
1725
1726 config.flags = 0;
1727 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1728 HWTSTAMP_TX_OFF;
1729 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1730 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1731 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1732 config.rx_filter = HWTSTAMP_FILTER_ALL;
1733 else
1734 config.rx_filter = HWTSTAMP_FILTER_NONE;
1735
1736 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1737 -EFAULT : 0;
1738}
1739
1740/* Control hardware time stamping */
1741static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1742{
1743 struct ravb_private *priv = netdev_priv(ndev);
1744 struct hwtstamp_config config;
1745 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1746 u32 tstamp_tx_ctrl;
1747
1748 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1749 return -EFAULT;
1750
1751 /* Reserved for future extensions */
1752 if (config.flags)
1753 return -EINVAL;
1754
1755 switch (config.tx_type) {
1756 case HWTSTAMP_TX_OFF:
1757 tstamp_tx_ctrl = 0;
1758 break;
1759 case HWTSTAMP_TX_ON:
1760 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1761 break;
1762 default:
1763 return -ERANGE;
1764 }
1765
1766 switch (config.rx_filter) {
1767 case HWTSTAMP_FILTER_NONE:
1768 tstamp_rx_ctrl = 0;
1769 break;
1770 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1771 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1772 break;
1773 default:
1774 config.rx_filter = HWTSTAMP_FILTER_ALL;
1775 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1776 }
1777
1778 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1779 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1780
1781 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1782 -EFAULT : 0;
1783}
1784
1785/* ioctl to device function */
1786static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1787{
Philippe Reynes0f635172016-08-20 00:52:18 +02001788 struct phy_device *phydev = ndev->phydev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001789
1790 if (!netif_running(ndev))
1791 return -EINVAL;
1792
1793 if (!phydev)
1794 return -ENODEV;
1795
1796 switch (cmd) {
1797 case SIOCGHWTSTAMP:
1798 return ravb_hwtstamp_get(ndev, req);
1799 case SIOCSHWTSTAMP:
1800 return ravb_hwtstamp_set(ndev, req);
1801 }
1802
1803 return phy_mii_ioctl(phydev, req, cmd);
1804}
1805
1806static const struct net_device_ops ravb_netdev_ops = {
1807 .ndo_open = ravb_open,
1808 .ndo_stop = ravb_close,
1809 .ndo_start_xmit = ravb_start_xmit,
1810 .ndo_select_queue = ravb_select_queue,
1811 .ndo_get_stats = ravb_get_stats,
1812 .ndo_set_rx_mode = ravb_set_rx_mode,
1813 .ndo_tx_timeout = ravb_tx_timeout,
1814 .ndo_do_ioctl = ravb_do_ioctl,
1815 .ndo_validate_addr = eth_validate_addr,
1816 .ndo_set_mac_address = eth_mac_addr,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001817};
1818
1819/* MDIO bus init function */
1820static int ravb_mdio_init(struct ravb_private *priv)
1821{
1822 struct platform_device *pdev = priv->pdev;
1823 struct device *dev = &pdev->dev;
1824 int error;
1825
1826 /* Bitbang init */
1827 priv->mdiobb.ops = &bb_ops;
1828
1829 /* MII controller setting */
1830 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1831 if (!priv->mii_bus)
1832 return -ENOMEM;
1833
1834 /* Hook up MII support for ethtool */
1835 priv->mii_bus->name = "ravb_mii";
1836 priv->mii_bus->parent = dev;
1837 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1838 pdev->name, pdev->id);
1839
1840 /* Register MDIO bus */
1841 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1842 if (error)
1843 goto out_free_bus;
1844
1845 return 0;
1846
1847out_free_bus:
1848 free_mdio_bitbang(priv->mii_bus);
1849 return error;
1850}
1851
1852/* MDIO bus release function */
1853static int ravb_mdio_release(struct ravb_private *priv)
1854{
1855 /* Unregister mdio bus */
1856 mdiobus_unregister(priv->mii_bus);
1857
1858 /* Free bitbang info */
1859 free_mdio_bitbang(priv->mii_bus);
1860
1861 return 0;
1862}
1863
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001864static const struct of_device_id ravb_match_table[] = {
1865 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1866 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001867 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001868 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001869 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001870 { }
1871};
1872MODULE_DEVICE_TABLE(of, ravb_match_table);
1873
Simon Hormanb3d39a82015-11-20 11:29:39 -08001874static int ravb_set_gti(struct net_device *ndev)
1875{
1876
1877 struct device *dev = ndev->dev.parent;
1878 struct device_node *np = dev->of_node;
1879 unsigned long rate;
1880 struct clk *clk;
1881 uint64_t inc;
1882
1883 clk = of_clk_get(np, 0);
1884 if (IS_ERR(clk)) {
1885 dev_err(dev, "could not get clock\n");
1886 return PTR_ERR(clk);
1887 }
1888
1889 rate = clk_get_rate(clk);
1890 clk_put(clk);
1891
Wolfram Sanga6d37132016-04-08 13:28:42 +02001892 if (!rate)
1893 return -EINVAL;
1894
Simon Hormanb3d39a82015-11-20 11:29:39 -08001895 inc = 1000000000ULL << 20;
1896 do_div(inc, rate);
1897
1898 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1899 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1900 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1901 return -EINVAL;
1902 }
1903
1904 ravb_write(ndev, inc, GTI);
1905
1906 return 0;
1907}
1908
Niklas Söderlund01841652016-08-03 15:56:47 +02001909static void ravb_set_config_mode(struct net_device *ndev)
1910{
1911 struct ravb_private *priv = netdev_priv(ndev);
1912
1913 if (priv->chip_id == RCAR_GEN2) {
1914 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
1915 /* Set CSEL value */
1916 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
1917 } else {
1918 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
1919 CCC_GAC | CCC_CSEL_HPB);
1920 }
1921}
1922
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01001923/* Set tx and rx clock internal delay modes */
1924static void ravb_set_delay_mode(struct net_device *ndev)
1925{
1926 struct ravb_private *priv = netdev_priv(ndev);
1927 int set = 0;
1928
1929 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1930 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID)
1931 set |= APSR_DM_RDM;
1932
1933 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1934 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1935 set |= APSR_DM_TDM;
1936
1937 ravb_modify(ndev, APSR, APSR_DM, set);
1938}
1939
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001940static int ravb_probe(struct platform_device *pdev)
1941{
1942 struct device_node *np = pdev->dev.of_node;
1943 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001944 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001945 struct net_device *ndev;
1946 int error, irq, q;
1947 struct resource *res;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001948 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001949
1950 if (!np) {
1951 dev_err(&pdev->dev,
1952 "this driver is required to be instantiated from device tree\n");
1953 return -EINVAL;
1954 }
1955
1956 /* Get base address */
1957 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1958 if (!res) {
1959 dev_err(&pdev->dev, "invalid resource\n");
1960 return -EINVAL;
1961 }
1962
1963 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
1964 NUM_TX_QUEUE, NUM_RX_QUEUE);
1965 if (!ndev)
1966 return -ENOMEM;
1967
1968 pm_runtime_enable(&pdev->dev);
1969 pm_runtime_get_sync(&pdev->dev);
1970
1971 /* The Ether-specific entries in the device structure. */
1972 ndev->base_addr = res->start;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001973
Wolfram Sange8668632016-03-01 17:37:58 +01001974 chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001975
1976 if (chip_id == RCAR_GEN3)
1977 irq = platform_get_irq_byname(pdev, "ch22");
1978 else
1979 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001980 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03001981 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001982 goto out_release;
1983 }
1984 ndev->irq = irq;
1985
1986 SET_NETDEV_DEV(ndev, &pdev->dev);
1987
1988 priv = netdev_priv(ndev);
1989 priv->ndev = ndev;
1990 priv->pdev = pdev;
1991 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
1992 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
1993 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
1994 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
1995 priv->addr = devm_ioremap_resource(&pdev->dev, res);
1996 if (IS_ERR(priv->addr)) {
1997 error = PTR_ERR(priv->addr);
1998 goto out_release;
1999 }
2000
2001 spin_lock_init(&priv->lock);
2002 INIT_WORK(&priv->work, ravb_tx_timeout_work);
2003
2004 priv->phy_interface = of_get_phy_mode(np);
2005
2006 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
2007 priv->avb_link_active_low =
2008 of_property_read_bool(np, "renesas,ether-link-active-low");
2009
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002010 if (chip_id == RCAR_GEN3) {
2011 irq = platform_get_irq_byname(pdev, "ch24");
2012 if (irq < 0) {
2013 error = irq;
2014 goto out_release;
2015 }
2016 priv->emac_irq = irq;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09002017 for (i = 0; i < NUM_RX_QUEUE; i++) {
2018 irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
2019 if (irq < 0) {
2020 error = irq;
2021 goto out_release;
2022 }
2023 priv->rx_irqs[i] = irq;
2024 }
2025 for (i = 0; i < NUM_TX_QUEUE; i++) {
2026 irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
2027 if (irq < 0) {
2028 error = irq;
2029 goto out_release;
2030 }
2031 priv->tx_irqs[i] = irq;
2032 }
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002033 }
2034
2035 priv->chip_id = chip_id;
2036
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002037 /* Set function */
2038 ndev->netdev_ops = &ravb_netdev_ops;
2039 ndev->ethtool_ops = &ravb_ethtool_ops;
2040
2041 /* Set AVB config mode */
Niklas Söderlund01841652016-08-03 15:56:47 +02002042 ravb_set_config_mode(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002043
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002044 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08002045 error = ravb_set_gti(ndev);
2046 if (error)
2047 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002048
2049 /* Request GTI loading */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03002050 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002051
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002052 if (priv->chip_id != RCAR_GEN2)
2053 ravb_set_delay_mode(ndev);
2054
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002055 /* Allocate descriptor base address table */
2056 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002057 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002058 &priv->desc_bat_dma, GFP_KERNEL);
2059 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09002060 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002061 "Cannot allocate desc base address table (size %d bytes)\n",
2062 priv->desc_bat_size);
2063 error = -ENOMEM;
2064 goto out_release;
2065 }
2066 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2067 priv->desc_bat[q].die_dt = DT_EOS;
2068 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2069
2070 /* Initialise HW timestamp list */
2071 INIT_LIST_HEAD(&priv->ts_skb_list);
2072
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002073 /* Initialise PTP Clock driver */
2074 if (chip_id != RCAR_GEN2)
2075 ravb_ptp_init(ndev, pdev);
2076
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002077 /* Debug message level */
2078 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2079
2080 /* Read and set MAC address */
2081 ravb_read_mac_address(ndev, of_get_mac_address(np));
2082 if (!is_valid_ether_addr(ndev->dev_addr)) {
2083 dev_warn(&pdev->dev,
2084 "no valid MAC address supplied, using a random one\n");
2085 eth_hw_addr_random(ndev);
2086 }
2087
2088 /* MDIO bus init */
2089 error = ravb_mdio_init(priv);
2090 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09002091 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002092 goto out_dma_free;
2093 }
2094
2095 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
2096 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
2097
2098 /* Network device register */
2099 error = register_netdev(ndev);
2100 if (error)
2101 goto out_napi_del;
2102
2103 /* Print device information */
2104 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2105 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2106
2107 platform_set_drvdata(pdev, ndev);
2108
2109 return 0;
2110
2111out_napi_del:
2112 netif_napi_del(&priv->napi[RAVB_NC]);
2113 netif_napi_del(&priv->napi[RAVB_BE]);
2114 ravb_mdio_release(priv);
2115out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002116 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002117 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002118
2119 /* Stop PTP Clock driver */
2120 if (chip_id != RCAR_GEN2)
2121 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002122out_release:
2123 if (ndev)
2124 free_netdev(ndev);
2125
2126 pm_runtime_put(&pdev->dev);
2127 pm_runtime_disable(&pdev->dev);
2128 return error;
2129}
2130
2131static int ravb_remove(struct platform_device *pdev)
2132{
2133 struct net_device *ndev = platform_get_drvdata(pdev);
2134 struct ravb_private *priv = netdev_priv(ndev);
2135
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002136 /* Stop PTP Clock driver */
2137 if (priv->chip_id != RCAR_GEN2)
2138 ravb_ptp_stop(ndev);
2139
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002140 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002141 priv->desc_bat_dma);
2142 /* Set reset mode */
2143 ravb_write(ndev, CCC_OPC_RESET, CCC);
2144 pm_runtime_put_sync(&pdev->dev);
2145 unregister_netdev(ndev);
2146 netif_napi_del(&priv->napi[RAVB_NC]);
2147 netif_napi_del(&priv->napi[RAVB_BE]);
2148 ravb_mdio_release(priv);
2149 pm_runtime_disable(&pdev->dev);
2150 free_netdev(ndev);
2151 platform_set_drvdata(pdev, NULL);
2152
2153 return 0;
2154}
2155
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002156static int __maybe_unused ravb_suspend(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002157{
2158 struct net_device *ndev = dev_get_drvdata(dev);
2159 int ret = 0;
2160
2161 if (netif_running(ndev)) {
2162 netif_device_detach(ndev);
2163 ret = ravb_close(ndev);
2164 }
2165
2166 return ret;
2167}
2168
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002169static int __maybe_unused ravb_resume(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002170{
2171 struct net_device *ndev = dev_get_drvdata(dev);
2172 struct ravb_private *priv = netdev_priv(ndev);
2173 int ret = 0;
2174
2175 /* All register have been reset to default values.
2176 * Restore all registers which where setup at probe time and
2177 * reopen device if it was running before system suspended.
2178 */
2179
2180 /* Set AVB config mode */
2181 ravb_set_config_mode(ndev);
2182
2183 /* Set GTI value */
2184 ret = ravb_set_gti(ndev);
2185 if (ret)
2186 return ret;
2187
2188 /* Request GTI loading */
2189 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
2190
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002191 if (priv->chip_id != RCAR_GEN2)
2192 ravb_set_delay_mode(ndev);
2193
Niklas Söderlund01841652016-08-03 15:56:47 +02002194 /* Restore descriptor base address table */
2195 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2196
2197 if (netif_running(ndev)) {
2198 ret = ravb_open(ndev);
2199 if (ret < 0)
2200 return ret;
2201 netif_device_attach(ndev);
2202 }
2203
2204 return ret;
2205}
2206
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002207static int __maybe_unused ravb_runtime_nop(struct device *dev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002208{
2209 /* Runtime PM callback shared between ->runtime_suspend()
2210 * and ->runtime_resume(). Simply returns success.
2211 *
2212 * This driver re-initializes all registers after
2213 * pm_runtime_get_sync() anyway so there is no need
2214 * to save and restore registers here.
2215 */
2216 return 0;
2217}
2218
2219static const struct dev_pm_ops ravb_dev_pm_ops = {
Niklas Söderlundb89b8152016-08-10 13:09:49 +02002220 SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume)
Kazuya Mizuguchi524c6f62016-05-30 05:25:43 +09002221 SET_RUNTIME_PM_OPS(ravb_runtime_nop, ravb_runtime_nop, NULL)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002222};
2223
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002224static struct platform_driver ravb_driver = {
2225 .probe = ravb_probe,
2226 .remove = ravb_remove,
2227 .driver = {
2228 .name = "ravb",
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002229 .pm = &ravb_dev_pm_ops,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002230 .of_match_table = ravb_match_table,
2231 },
2232};
2233
2234module_platform_driver(ravb_driver);
2235
2236MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2237MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2238MODULE_LICENSE("GPL v2");