blob: 9e20f37a3b6fd576c38b5c2e93b3723fd0de0d73 [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.
5 * Copyright (C) 2015 Cogent Embedded, Inc. <source@cogentembedded.com>
6 *
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
Sergei Shtylyova0d2f202015-06-11 01:02:30 +030045int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value)
Sergei Shtylyovc1566332015-06-11 01:01:43 +030046{
47 int i;
48
49 for (i = 0; i < 10000; i++) {
50 if ((ravb_read(ndev, reg) & mask) == value)
51 return 0;
52 udelay(10);
53 }
54 return -ETIMEDOUT;
55}
56
57static int ravb_config(struct net_device *ndev)
58{
59 int error;
60
61 /* Set config mode */
62 ravb_write(ndev, (ravb_read(ndev, CCC) & ~CCC_OPC) | CCC_OPC_CONFIG,
63 CCC);
64 /* Check if the operating mode is changed to the config mode */
65 error = ravb_wait(ndev, CSR, CSR_OPS, CSR_OPS_CONFIG);
66 if (error)
67 netdev_err(ndev, "failed to switch device to config mode\n");
68
69 return error;
70}
71
72static void ravb_set_duplex(struct net_device *ndev)
73{
74 struct ravb_private *priv = netdev_priv(ndev);
75 u32 ecmr = ravb_read(ndev, ECMR);
76
77 if (priv->duplex) /* Full */
78 ecmr |= ECMR_DM;
79 else /* Half */
80 ecmr &= ~ECMR_DM;
81 ravb_write(ndev, ecmr, ECMR);
82}
83
84static void ravb_set_rate(struct net_device *ndev)
85{
86 struct ravb_private *priv = netdev_priv(ndev);
87
88 switch (priv->speed) {
89 case 100: /* 100BASE */
90 ravb_write(ndev, GECMR_SPEED_100, GECMR);
91 break;
92 case 1000: /* 1000BASE */
93 ravb_write(ndev, GECMR_SPEED_1000, GECMR);
94 break;
95 default:
96 break;
97 }
98}
99
100static void ravb_set_buffer_align(struct sk_buff *skb)
101{
102 u32 reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1);
103
104 if (reserve)
105 skb_reserve(skb, RAVB_ALIGN - reserve);
106}
107
108/* Get MAC address from the MAC address registers
109 *
110 * Ethernet AVB device doesn't have ROM for MAC address.
111 * This function gets the MAC address that was used by a bootloader.
112 */
113static void ravb_read_mac_address(struct net_device *ndev, const u8 *mac)
114{
115 if (mac) {
116 ether_addr_copy(ndev->dev_addr, mac);
117 } else {
Sergei Shtylyovd9660632015-12-05 00:58:07 +0300118 u32 mahr = ravb_read(ndev, MAHR);
119 u32 malr = ravb_read(ndev, MALR);
120
121 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
122 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
123 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
124 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
125 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
126 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300127 }
128}
129
130static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
131{
132 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
133 mdiobb);
134 u32 pir = ravb_read(priv->ndev, PIR);
135
136 if (set)
137 pir |= mask;
138 else
139 pir &= ~mask;
140 ravb_write(priv->ndev, pir, PIR);
141}
142
143/* MDC pin control */
144static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level)
145{
146 ravb_mdio_ctrl(ctrl, PIR_MDC, level);
147}
148
149/* Data I/O pin control */
150static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
151{
152 ravb_mdio_ctrl(ctrl, PIR_MMD, output);
153}
154
155/* Set data bit */
156static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
157{
158 ravb_mdio_ctrl(ctrl, PIR_MDO, value);
159}
160
161/* Get data bit */
162static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl)
163{
164 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
165 mdiobb);
166
167 return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0;
168}
169
170/* MDIO bus control struct */
171static struct mdiobb_ops bb_ops = {
172 .owner = THIS_MODULE,
173 .set_mdc = ravb_set_mdc,
174 .set_mdio_dir = ravb_set_mdio_dir,
175 .set_mdio_data = ravb_set_mdio_data,
176 .get_mdio_data = ravb_get_mdio_data,
177};
178
179/* Free skb's and DMA buffers for Ethernet AVB */
180static void ravb_ring_free(struct net_device *ndev, int q)
181{
182 struct ravb_private *priv = netdev_priv(ndev);
183 int ring_size;
184 int i;
185
186 /* Free RX skb ringbuffer */
187 if (priv->rx_skb[q]) {
188 for (i = 0; i < priv->num_rx_ring[q]; i++)
189 dev_kfree_skb(priv->rx_skb[q][i]);
190 }
191 kfree(priv->rx_skb[q]);
192 priv->rx_skb[q] = NULL;
193
194 /* Free TX skb ringbuffer */
195 if (priv->tx_skb[q]) {
196 for (i = 0; i < priv->num_tx_ring[q]; i++)
197 dev_kfree_skb(priv->tx_skb[q][i]);
198 }
199 kfree(priv->tx_skb[q]);
200 priv->tx_skb[q] = NULL;
201
202 /* Free aligned TX buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300203 kfree(priv->tx_align[q]);
204 priv->tx_align[q] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300205
206 if (priv->rx_ring[q]) {
207 ring_size = sizeof(struct ravb_ex_rx_desc) *
208 (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900209 dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300210 priv->rx_desc_dma[q]);
211 priv->rx_ring[q] = NULL;
212 }
213
214 if (priv->tx_ring[q]) {
215 ring_size = sizeof(struct ravb_tx_desc) *
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300216 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900217 dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300218 priv->tx_desc_dma[q]);
219 priv->tx_ring[q] = NULL;
220 }
221}
222
223/* Format skb and descriptor buffer for Ethernet AVB */
224static void ravb_ring_format(struct net_device *ndev, int q)
225{
226 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300227 struct ravb_ex_rx_desc *rx_desc;
228 struct ravb_tx_desc *tx_desc;
229 struct ravb_desc *desc;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300230 int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q];
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300231 int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] *
232 NUM_TX_DESC;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300233 dma_addr_t dma_addr;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300234 int i;
235
236 priv->cur_rx[q] = 0;
237 priv->cur_tx[q] = 0;
238 priv->dirty_rx[q] = 0;
239 priv->dirty_tx[q] = 0;
240
241 memset(priv->rx_ring[q], 0, rx_ring_size);
242 /* Build RX ring buffer */
243 for (i = 0; i < priv->num_rx_ring[q]; i++) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300244 /* RX descriptor */
245 rx_desc = &priv->rx_ring[q][i];
246 /* The size of the buffer should be on 16-byte boundary. */
247 rx_desc->ds_cc = cpu_to_le16(ALIGN(PKT_BUF_SZ, 16));
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900248 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300249 ALIGN(PKT_BUF_SZ, 16),
250 DMA_FROM_DEVICE);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300251 /* We just set the data size to 0 for a failed mapping which
252 * should prevent DMA from happening...
253 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900254 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300255 rx_desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300256 rx_desc->dptr = cpu_to_le32(dma_addr);
257 rx_desc->die_dt = DT_FEMPTY;
258 }
259 rx_desc = &priv->rx_ring[q][i];
260 rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
261 rx_desc->die_dt = DT_LINKFIX; /* type */
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300262
263 memset(priv->tx_ring[q], 0, tx_ring_size);
264 /* Build TX ring buffer */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300265 for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
266 i++, tx_desc++) {
267 tx_desc->die_dt = DT_EEMPTY;
268 tx_desc++;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300269 tx_desc->die_dt = DT_EEMPTY;
270 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300271 tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
272 tx_desc->die_dt = DT_LINKFIX; /* type */
273
274 /* RX descriptor base address for best effort */
275 desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
276 desc->die_dt = DT_LINKFIX; /* type */
277 desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
278
279 /* TX descriptor base address for best effort */
280 desc = &priv->desc_bat[q];
281 desc->die_dt = DT_LINKFIX; /* type */
282 desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
283}
284
285/* Init skb and descriptor buffer for Ethernet AVB */
286static int ravb_ring_init(struct net_device *ndev, int q)
287{
288 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300289 struct sk_buff *skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300290 int ring_size;
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300291 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300292
293 /* Allocate RX and TX skb rings */
294 priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
295 sizeof(*priv->rx_skb[q]), GFP_KERNEL);
296 priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
297 sizeof(*priv->tx_skb[q]), GFP_KERNEL);
298 if (!priv->rx_skb[q] || !priv->tx_skb[q])
299 goto error;
300
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300301 for (i = 0; i < priv->num_rx_ring[q]; i++) {
302 skb = netdev_alloc_skb(ndev, PKT_BUF_SZ + RAVB_ALIGN - 1);
303 if (!skb)
304 goto error;
305 ravb_set_buffer_align(skb);
306 priv->rx_skb[q][i] = skb;
307 }
308
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300309 /* Allocate rings for the aligned buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300310 priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
311 DPTR_ALIGN - 1, GFP_KERNEL);
312 if (!priv->tx_align[q])
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300313 goto error;
314
315 /* Allocate all RX descriptors. */
316 ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900317 priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300318 &priv->rx_desc_dma[q],
319 GFP_KERNEL);
320 if (!priv->rx_ring[q])
321 goto error;
322
323 priv->dirty_rx[q] = 0;
324
325 /* Allocate all TX descriptors. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300326 ring_size = sizeof(struct ravb_tx_desc) *
327 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900328 priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300329 &priv->tx_desc_dma[q],
330 GFP_KERNEL);
331 if (!priv->tx_ring[q])
332 goto error;
333
334 return 0;
335
336error:
337 ravb_ring_free(ndev, q);
338
339 return -ENOMEM;
340}
341
342/* E-MAC init function */
343static void ravb_emac_init(struct net_device *ndev)
344{
345 struct ravb_private *priv = netdev_priv(ndev);
346 u32 ecmr;
347
348 /* Receive frame limit set register */
349 ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
350
351 /* PAUSE prohibition */
352 ecmr = ravb_read(ndev, ECMR);
353 ecmr &= ECMR_DM;
354 ecmr |= ECMR_ZPF | (priv->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
355 ravb_write(ndev, ecmr, ECMR);
356
357 ravb_set_rate(ndev);
358
359 /* Set MAC address */
360 ravb_write(ndev,
361 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
362 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
363 ravb_write(ndev,
364 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
365
366 ravb_write(ndev, 1, MPR);
367
368 /* E-MAC status register clear */
369 ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
370
371 /* E-MAC interrupt enable register */
372 ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
373}
374
375/* Device init function for Ethernet AVB */
376static int ravb_dmac_init(struct net_device *ndev)
377{
378 int error;
379
380 /* Set CONFIG mode */
381 error = ravb_config(ndev);
382 if (error)
383 return error;
384
385 error = ravb_ring_init(ndev, RAVB_BE);
386 if (error)
387 return error;
388 error = ravb_ring_init(ndev, RAVB_NC);
389 if (error) {
390 ravb_ring_free(ndev, RAVB_BE);
391 return error;
392 }
393
394 /* Descriptor format */
395 ravb_ring_format(ndev, RAVB_BE);
396 ravb_ring_format(ndev, RAVB_NC);
397
398#if defined(__LITTLE_ENDIAN)
399 ravb_write(ndev, ravb_read(ndev, CCC) & ~CCC_BOC, CCC);
400#else
401 ravb_write(ndev, ravb_read(ndev, CCC) | CCC_BOC, CCC);
402#endif
403
404 /* Set AVB RX */
405 ravb_write(ndev, RCR_EFFS | RCR_ENCF | RCR_ETS0 | 0x18000000, RCR);
406
407 /* Set FIFO size */
408 ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00222200, TGC);
409
410 /* Timestamp enable */
411 ravb_write(ndev, TCCR_TFEN, TCCR);
412
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900413 /* Interrupt init: */
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300414 /* Frame receive */
415 ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900416 /* Disable FIFO full warning */
417 ravb_write(ndev, 0, RIC1);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300418 /* Receive FIFO full error, descriptor empty */
419 ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
420 /* Frame transmitted, timestamp FIFO updated */
421 ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
422
423 /* Setting the control will start the AVB-DMAC process. */
424 ravb_write(ndev, (ravb_read(ndev, CCC) & ~CCC_OPC) | CCC_OPC_OPERATION,
425 CCC);
426
427 return 0;
428}
429
430/* Free TX skb function for AVB-IP */
431static int ravb_tx_free(struct net_device *ndev, int q)
432{
433 struct ravb_private *priv = netdev_priv(ndev);
434 struct net_device_stats *stats = &priv->stats[q];
435 struct ravb_tx_desc *desc;
436 int free_num = 0;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300437 int entry;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300438 u32 size;
439
440 for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300441 entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
442 NUM_TX_DESC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300443 desc = &priv->tx_ring[q][entry];
444 if (desc->die_dt != DT_FEMPTY)
445 break;
446 /* Descriptor type must be checked before all other reads */
447 dma_rmb();
448 size = le16_to_cpu(desc->ds_tagl) & TX_DS;
449 /* Free the original skb. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300450 if (priv->tx_skb[q][entry / NUM_TX_DESC]) {
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900451 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300452 size, DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300453 /* Last packet descriptor? */
454 if (entry % NUM_TX_DESC == NUM_TX_DESC - 1) {
455 entry /= NUM_TX_DESC;
456 dev_kfree_skb_any(priv->tx_skb[q][entry]);
457 priv->tx_skb[q][entry] = NULL;
458 stats->tx_packets++;
459 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300460 free_num++;
461 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300462 stats->tx_bytes += size;
463 desc->die_dt = DT_EEMPTY;
464 }
465 return free_num;
466}
467
468static void ravb_get_tx_tstamp(struct net_device *ndev)
469{
470 struct ravb_private *priv = netdev_priv(ndev);
471 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
472 struct skb_shared_hwtstamps shhwtstamps;
473 struct sk_buff *skb;
474 struct timespec64 ts;
475 u16 tag, tfa_tag;
476 int count;
477 u32 tfa2;
478
479 count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
480 while (count--) {
481 tfa2 = ravb_read(ndev, TFA2);
482 tfa_tag = (tfa2 & TFA2_TST) >> 16;
483 ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
484 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
485 ravb_read(ndev, TFA1);
486 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
487 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
488 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
489 list) {
490 skb = ts_skb->skb;
491 tag = ts_skb->tag;
492 list_del(&ts_skb->list);
493 kfree(ts_skb);
494 if (tag == tfa_tag) {
495 skb_tstamp_tx(skb, &shhwtstamps);
496 break;
497 }
498 }
499 ravb_write(ndev, ravb_read(ndev, TCCR) | TCCR_TFR, TCCR);
500 }
501}
502
503/* Packet receive function for Ethernet AVB */
504static bool ravb_rx(struct net_device *ndev, int *quota, int q)
505{
506 struct ravb_private *priv = netdev_priv(ndev);
507 int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
508 int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
509 priv->cur_rx[q];
510 struct net_device_stats *stats = &priv->stats[q];
511 struct ravb_ex_rx_desc *desc;
512 struct sk_buff *skb;
513 dma_addr_t dma_addr;
514 struct timespec64 ts;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300515 u8 desc_status;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300516 u16 pkt_len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300517 int limit;
518
519 boguscnt = min(boguscnt, *quota);
520 limit = boguscnt;
521 desc = &priv->rx_ring[q][entry];
522 while (desc->die_dt != DT_FEMPTY) {
523 /* Descriptor type must be checked before all other reads */
524 dma_rmb();
525 desc_status = desc->msc;
526 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
527
528 if (--boguscnt < 0)
529 break;
530
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300531 /* We use 0-byte descriptors to mark the DMA mapping errors */
532 if (!pkt_len)
533 continue;
534
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300535 if (desc_status & MSC_MC)
536 stats->multicast++;
537
538 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
539 MSC_CEEF)) {
540 stats->rx_errors++;
541 if (desc_status & MSC_CRC)
542 stats->rx_crc_errors++;
543 if (desc_status & MSC_RFE)
544 stats->rx_frame_errors++;
545 if (desc_status & (MSC_RTLF | MSC_RTSF))
546 stats->rx_length_errors++;
547 if (desc_status & MSC_CEEF)
548 stats->rx_missed_errors++;
549 } else {
550 u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
551
552 skb = priv->rx_skb[q][entry];
553 priv->rx_skb[q][entry] = NULL;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900554 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyove2370f02015-07-15 00:56:52 +0300555 ALIGN(PKT_BUF_SZ, 16),
556 DMA_FROM_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300557 get_ts &= (q == RAVB_NC) ?
558 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
559 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
560 if (get_ts) {
561 struct skb_shared_hwtstamps *shhwtstamps;
562
563 shhwtstamps = skb_hwtstamps(skb);
564 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
565 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
566 32) | le32_to_cpu(desc->ts_sl);
567 ts.tv_nsec = le32_to_cpu(desc->ts_n);
568 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
569 }
570 skb_put(skb, pkt_len);
571 skb->protocol = eth_type_trans(skb, ndev);
572 napi_gro_receive(&priv->napi[q], skb);
573 stats->rx_packets++;
574 stats->rx_bytes += pkt_len;
575 }
576
577 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
578 desc = &priv->rx_ring[q][entry];
579 }
580
581 /* Refill the RX ring buffers. */
582 for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
583 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
584 desc = &priv->rx_ring[q][entry];
585 /* The size of the buffer should be on 16-byte boundary. */
586 desc->ds_cc = cpu_to_le16(ALIGN(PKT_BUF_SZ, 16));
587
588 if (!priv->rx_skb[q][entry]) {
589 skb = netdev_alloc_skb(ndev,
590 PKT_BUF_SZ + RAVB_ALIGN - 1);
591 if (!skb)
592 break; /* Better luck next round. */
593 ravb_set_buffer_align(skb);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900594 dma_addr = dma_map_single(ndev->dev.parent, skb->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300595 le16_to_cpu(desc->ds_cc),
596 DMA_FROM_DEVICE);
597 skb_checksum_none_assert(skb);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300598 /* We just set the data size to 0 for a failed mapping
599 * which should prevent DMA from happening...
600 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900601 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300602 desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300603 desc->dptr = cpu_to_le32(dma_addr);
604 priv->rx_skb[q][entry] = skb;
605 }
606 /* Descriptor type must be set after all the above writes */
607 dma_wmb();
608 desc->die_dt = DT_FEMPTY;
609 }
610
611 *quota -= limit - (++boguscnt);
612
613 return boguscnt <= 0;
614}
615
616static void ravb_rcv_snd_disable(struct net_device *ndev)
617{
618 /* Disable TX and RX */
619 ravb_write(ndev, ravb_read(ndev, ECMR) & ~(ECMR_RE | ECMR_TE), ECMR);
620}
621
622static void ravb_rcv_snd_enable(struct net_device *ndev)
623{
624 /* Enable TX and RX */
625 ravb_write(ndev, ravb_read(ndev, ECMR) | ECMR_RE | ECMR_TE, ECMR);
626}
627
628/* function for waiting dma process finished */
629static int ravb_stop_dma(struct net_device *ndev)
630{
631 int error;
632
633 /* Wait for stopping the hardware TX process */
634 error = ravb_wait(ndev, TCCR,
635 TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
636 if (error)
637 return error;
638
639 error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
640 0);
641 if (error)
642 return error;
643
644 /* Stop the E-MAC's RX/TX processes. */
645 ravb_rcv_snd_disable(ndev);
646
647 /* Wait for stopping the RX DMA process */
648 error = ravb_wait(ndev, CSR, CSR_RPO, 0);
649 if (error)
650 return error;
651
652 /* Stop AVB-DMAC process */
653 return ravb_config(ndev);
654}
655
656/* E-MAC interrupt handler */
657static void ravb_emac_interrupt(struct net_device *ndev)
658{
659 struct ravb_private *priv = netdev_priv(ndev);
660 u32 ecsr, psr;
661
662 ecsr = ravb_read(ndev, ECSR);
663 ravb_write(ndev, ecsr, ECSR); /* clear interrupt */
664 if (ecsr & ECSR_ICD)
665 ndev->stats.tx_carrier_errors++;
666 if (ecsr & ECSR_LCHNG) {
667 /* Link changed */
668 if (priv->no_avb_link)
669 return;
670 psr = ravb_read(ndev, PSR);
671 if (priv->avb_link_active_low)
672 psr ^= PSR_LMON;
673 if (!(psr & PSR_LMON)) {
674 /* DIsable RX and TX */
675 ravb_rcv_snd_disable(ndev);
676 } else {
677 /* Enable RX and TX */
678 ravb_rcv_snd_enable(ndev);
679 }
680 }
681}
682
683/* Error interrupt handler */
684static void ravb_error_interrupt(struct net_device *ndev)
685{
686 struct ravb_private *priv = netdev_priv(ndev);
687 u32 eis, ris2;
688
689 eis = ravb_read(ndev, EIS);
690 ravb_write(ndev, ~EIS_QFS, EIS);
691 if (eis & EIS_QFS) {
692 ris2 = ravb_read(ndev, RIS2);
693 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF), RIS2);
694
695 /* Receive Descriptor Empty int */
696 if (ris2 & RIS2_QFF0)
697 priv->stats[RAVB_BE].rx_over_errors++;
698
699 /* Receive Descriptor Empty int */
700 if (ris2 & RIS2_QFF1)
701 priv->stats[RAVB_NC].rx_over_errors++;
702
703 /* Receive FIFO Overflow int */
704 if (ris2 & RIS2_RFFF)
705 priv->rx_fifo_errors++;
706 }
707}
708
709static irqreturn_t ravb_interrupt(int irq, void *dev_id)
710{
711 struct net_device *ndev = dev_id;
712 struct ravb_private *priv = netdev_priv(ndev);
713 irqreturn_t result = IRQ_NONE;
714 u32 iss;
715
716 spin_lock(&priv->lock);
717 /* Get interrupt status */
718 iss = ravb_read(ndev, ISS);
719
720 /* Received and transmitted interrupts */
721 if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
722 u32 ris0 = ravb_read(ndev, RIS0);
723 u32 ric0 = ravb_read(ndev, RIC0);
724 u32 tis = ravb_read(ndev, TIS);
725 u32 tic = ravb_read(ndev, TIC);
726 int q;
727
728 /* Timestamp updated */
729 if (tis & TIS_TFUF) {
730 ravb_write(ndev, ~TIS_TFUF, TIS);
731 ravb_get_tx_tstamp(ndev);
732 result = IRQ_HANDLED;
733 }
734
735 /* Network control and best effort queue RX/TX */
736 for (q = RAVB_NC; q >= RAVB_BE; q--) {
737 if (((ris0 & ric0) & BIT(q)) ||
738 ((tis & tic) & BIT(q))) {
739 if (napi_schedule_prep(&priv->napi[q])) {
740 /* Mask RX and TX interrupts */
Masaru Nagai2452cb02015-11-13 19:24:49 +0900741 ric0 &= ~BIT(q);
742 tic &= ~BIT(q);
743 ravb_write(ndev, ric0, RIC0);
744 ravb_write(ndev, tic, TIC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300745 __napi_schedule(&priv->napi[q]);
746 } else {
747 netdev_warn(ndev,
748 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
749 ris0, ric0);
750 netdev_warn(ndev,
751 " tx status 0x%08x, tx mask 0x%08x.\n",
752 tis, tic);
753 }
754 result = IRQ_HANDLED;
755 }
756 }
757 }
758
759 /* E-MAC status summary */
760 if (iss & ISS_MS) {
761 ravb_emac_interrupt(ndev);
762 result = IRQ_HANDLED;
763 }
764
765 /* Error status summary */
766 if (iss & ISS_ES) {
767 ravb_error_interrupt(ndev);
768 result = IRQ_HANDLED;
769 }
770
Sergei Shtylyova0d2f202015-06-11 01:02:30 +0300771 if (iss & ISS_CGIS)
772 result = ravb_ptp_interrupt(ndev);
773
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300774 mmiowb();
775 spin_unlock(&priv->lock);
776 return result;
777}
778
779static int ravb_poll(struct napi_struct *napi, int budget)
780{
781 struct net_device *ndev = napi->dev;
782 struct ravb_private *priv = netdev_priv(ndev);
783 unsigned long flags;
784 int q = napi - priv->napi;
785 int mask = BIT(q);
786 int quota = budget;
787 u32 ris0, tis;
788
789 for (;;) {
790 tis = ravb_read(ndev, TIS);
791 ris0 = ravb_read(ndev, RIS0);
792 if (!((ris0 & mask) || (tis & mask)))
793 break;
794
795 /* Processing RX Descriptor Ring */
796 if (ris0 & mask) {
797 /* Clear RX interrupt */
798 ravb_write(ndev, ~mask, RIS0);
799 if (ravb_rx(ndev, &quota, q))
800 goto out;
801 }
802 /* Processing TX Descriptor Ring */
803 if (tis & mask) {
804 spin_lock_irqsave(&priv->lock, flags);
805 /* Clear TX interrupt */
806 ravb_write(ndev, ~mask, TIS);
807 ravb_tx_free(ndev, q);
808 netif_wake_subqueue(ndev, q);
809 mmiowb();
810 spin_unlock_irqrestore(&priv->lock, flags);
811 }
812 }
813
814 napi_complete(napi);
815
816 /* Re-enable RX/TX interrupts */
817 spin_lock_irqsave(&priv->lock, flags);
818 ravb_write(ndev, ravb_read(ndev, RIC0) | mask, RIC0);
819 ravb_write(ndev, ravb_read(ndev, TIC) | mask, TIC);
820 mmiowb();
821 spin_unlock_irqrestore(&priv->lock, flags);
822
823 /* Receive error message handling */
824 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
825 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
826 if (priv->rx_over_errors != ndev->stats.rx_over_errors) {
827 ndev->stats.rx_over_errors = priv->rx_over_errors;
828 netif_err(priv, rx_err, ndev, "Receive Descriptor Empty\n");
829 }
830 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors) {
831 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
832 netif_err(priv, rx_err, ndev, "Receive FIFO Overflow\n");
833 }
834out:
835 return budget - quota;
836}
837
838/* PHY state control function */
839static void ravb_adjust_link(struct net_device *ndev)
840{
841 struct ravb_private *priv = netdev_priv(ndev);
842 struct phy_device *phydev = priv->phydev;
843 bool new_state = false;
844
845 if (phydev->link) {
846 if (phydev->duplex != priv->duplex) {
847 new_state = true;
848 priv->duplex = phydev->duplex;
849 ravb_set_duplex(ndev);
850 }
851
852 if (phydev->speed != priv->speed) {
853 new_state = true;
854 priv->speed = phydev->speed;
855 ravb_set_rate(ndev);
856 }
857 if (!priv->link) {
858 ravb_write(ndev, ravb_read(ndev, ECMR) & ~ECMR_TXF,
859 ECMR);
860 new_state = true;
861 priv->link = phydev->link;
862 if (priv->no_avb_link)
863 ravb_rcv_snd_enable(ndev);
864 }
865 } else if (priv->link) {
866 new_state = true;
867 priv->link = 0;
868 priv->speed = 0;
869 priv->duplex = -1;
870 if (priv->no_avb_link)
871 ravb_rcv_snd_disable(ndev);
872 }
873
874 if (new_state && netif_msg_link(priv))
875 phy_print_status(phydev);
876}
877
878/* PHY init function */
879static int ravb_phy_init(struct net_device *ndev)
880{
881 struct device_node *np = ndev->dev.parent->of_node;
882 struct ravb_private *priv = netdev_priv(ndev);
883 struct phy_device *phydev;
884 struct device_node *pn;
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900885 int err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300886
887 priv->link = 0;
888 priv->speed = 0;
889 priv->duplex = -1;
890
891 /* Try connecting to PHY */
892 pn = of_parse_phandle(np, "phy-handle", 0);
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900893 if (!pn) {
894 /* In the case of a fixed PHY, the DT node associated
895 * to the PHY is the Ethernet MAC DT node.
896 */
897 if (of_phy_is_fixed_link(np)) {
898 err = of_phy_register_fixed_link(np);
899 if (err)
900 return err;
901 }
902 pn = of_node_get(np);
903 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300904 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
905 priv->phy_interface);
906 if (!phydev) {
907 netdev_err(ndev, "failed to connect PHY\n");
908 return -ENOENT;
909 }
910
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +0900911 /* This driver only support 10/100Mbit speeds on Gen3
912 * at this time.
913 */
914 if (priv->chip_id == RCAR_GEN3) {
915 int err;
916
917 err = phy_set_max_speed(phydev, SPEED_100);
918 if (err) {
919 netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
920 phy_disconnect(phydev);
921 return err;
922 }
923
924 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
925 }
926
Kazuya Mizuguchi54499962015-12-14 00:15:58 +0900927 /* 10BASE is not supported */
928 phydev->supported &= ~PHY_10BT_FEATURES;
929
Andrew Lunn22209432016-01-06 20:11:13 +0100930 phy_attached_info(phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300931
932 priv->phydev = phydev;
933
934 return 0;
935}
936
937/* PHY control start function */
938static int ravb_phy_start(struct net_device *ndev)
939{
940 struct ravb_private *priv = netdev_priv(ndev);
941 int error;
942
943 error = ravb_phy_init(ndev);
944 if (error)
945 return error;
946
947 phy_start(priv->phydev);
948
949 return 0;
950}
951
952static int ravb_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
953{
954 struct ravb_private *priv = netdev_priv(ndev);
955 int error = -ENODEV;
956 unsigned long flags;
957
958 if (priv->phydev) {
959 spin_lock_irqsave(&priv->lock, flags);
960 error = phy_ethtool_gset(priv->phydev, ecmd);
961 spin_unlock_irqrestore(&priv->lock, flags);
962 }
963
964 return error;
965}
966
967static int ravb_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
968{
969 struct ravb_private *priv = netdev_priv(ndev);
970 unsigned long flags;
971 int error;
972
973 if (!priv->phydev)
974 return -ENODEV;
975
976 spin_lock_irqsave(&priv->lock, flags);
977
978 /* Disable TX and RX */
979 ravb_rcv_snd_disable(ndev);
980
981 error = phy_ethtool_sset(priv->phydev, ecmd);
982 if (error)
983 goto error_exit;
984
985 if (ecmd->duplex == DUPLEX_FULL)
986 priv->duplex = 1;
987 else
988 priv->duplex = 0;
989
990 ravb_set_duplex(ndev);
991
992error_exit:
993 mdelay(1);
994
995 /* Enable TX and RX */
996 ravb_rcv_snd_enable(ndev);
997
998 mmiowb();
999 spin_unlock_irqrestore(&priv->lock, flags);
1000
1001 return error;
1002}
1003
1004static int ravb_nway_reset(struct net_device *ndev)
1005{
1006 struct ravb_private *priv = netdev_priv(ndev);
1007 int error = -ENODEV;
1008 unsigned long flags;
1009
1010 if (priv->phydev) {
1011 spin_lock_irqsave(&priv->lock, flags);
1012 error = phy_start_aneg(priv->phydev);
1013 spin_unlock_irqrestore(&priv->lock, flags);
1014 }
1015
1016 return error;
1017}
1018
1019static u32 ravb_get_msglevel(struct net_device *ndev)
1020{
1021 struct ravb_private *priv = netdev_priv(ndev);
1022
1023 return priv->msg_enable;
1024}
1025
1026static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1027{
1028 struct ravb_private *priv = netdev_priv(ndev);
1029
1030 priv->msg_enable = value;
1031}
1032
1033static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1034 "rx_queue_0_current",
1035 "tx_queue_0_current",
1036 "rx_queue_0_dirty",
1037 "tx_queue_0_dirty",
1038 "rx_queue_0_packets",
1039 "tx_queue_0_packets",
1040 "rx_queue_0_bytes",
1041 "tx_queue_0_bytes",
1042 "rx_queue_0_mcast_packets",
1043 "rx_queue_0_errors",
1044 "rx_queue_0_crc_errors",
1045 "rx_queue_0_frame_errors",
1046 "rx_queue_0_length_errors",
1047 "rx_queue_0_missed_errors",
1048 "rx_queue_0_over_errors",
1049
1050 "rx_queue_1_current",
1051 "tx_queue_1_current",
1052 "rx_queue_1_dirty",
1053 "tx_queue_1_dirty",
1054 "rx_queue_1_packets",
1055 "tx_queue_1_packets",
1056 "rx_queue_1_bytes",
1057 "tx_queue_1_bytes",
1058 "rx_queue_1_mcast_packets",
1059 "rx_queue_1_errors",
1060 "rx_queue_1_crc_errors",
Sergei Shtylyovb17c1d92015-12-04 01:51:10 +03001061 "rx_queue_1_frame_errors",
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001062 "rx_queue_1_length_errors",
1063 "rx_queue_1_missed_errors",
1064 "rx_queue_1_over_errors",
1065};
1066
1067#define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1068
1069static int ravb_get_sset_count(struct net_device *netdev, int sset)
1070{
1071 switch (sset) {
1072 case ETH_SS_STATS:
1073 return RAVB_STATS_LEN;
1074 default:
1075 return -EOPNOTSUPP;
1076 }
1077}
1078
1079static void ravb_get_ethtool_stats(struct net_device *ndev,
1080 struct ethtool_stats *stats, u64 *data)
1081{
1082 struct ravb_private *priv = netdev_priv(ndev);
1083 int i = 0;
1084 int q;
1085
1086 /* Device-specific stats */
1087 for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1088 struct net_device_stats *stats = &priv->stats[q];
1089
1090 data[i++] = priv->cur_rx[q];
1091 data[i++] = priv->cur_tx[q];
1092 data[i++] = priv->dirty_rx[q];
1093 data[i++] = priv->dirty_tx[q];
1094 data[i++] = stats->rx_packets;
1095 data[i++] = stats->tx_packets;
1096 data[i++] = stats->rx_bytes;
1097 data[i++] = stats->tx_bytes;
1098 data[i++] = stats->multicast;
1099 data[i++] = stats->rx_errors;
1100 data[i++] = stats->rx_crc_errors;
1101 data[i++] = stats->rx_frame_errors;
1102 data[i++] = stats->rx_length_errors;
1103 data[i++] = stats->rx_missed_errors;
1104 data[i++] = stats->rx_over_errors;
1105 }
1106}
1107
1108static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1109{
1110 switch (stringset) {
1111 case ETH_SS_STATS:
1112 memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
1113 break;
1114 }
1115}
1116
1117static void ravb_get_ringparam(struct net_device *ndev,
1118 struct ethtool_ringparam *ring)
1119{
1120 struct ravb_private *priv = netdev_priv(ndev);
1121
1122 ring->rx_max_pending = BE_RX_RING_MAX;
1123 ring->tx_max_pending = BE_TX_RING_MAX;
1124 ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1125 ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1126}
1127
1128static int ravb_set_ringparam(struct net_device *ndev,
1129 struct ethtool_ringparam *ring)
1130{
1131 struct ravb_private *priv = netdev_priv(ndev);
1132 int error;
1133
1134 if (ring->tx_pending > BE_TX_RING_MAX ||
1135 ring->rx_pending > BE_RX_RING_MAX ||
1136 ring->tx_pending < BE_TX_RING_MIN ||
1137 ring->rx_pending < BE_RX_RING_MIN)
1138 return -EINVAL;
1139 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1140 return -EINVAL;
1141
1142 if (netif_running(ndev)) {
1143 netif_device_detach(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001144 /* Stop PTP Clock driver */
1145 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001146 /* Wait for DMA stopping */
1147 error = ravb_stop_dma(ndev);
1148 if (error) {
1149 netdev_err(ndev,
1150 "cannot set ringparam! Any AVB processes are still running?\n");
1151 return error;
1152 }
1153 synchronize_irq(ndev->irq);
1154
1155 /* Free all the skb's in the RX queue and the DMA buffers. */
1156 ravb_ring_free(ndev, RAVB_BE);
1157 ravb_ring_free(ndev, RAVB_NC);
1158 }
1159
1160 /* Set new parameters */
1161 priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1162 priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1163
1164 if (netif_running(ndev)) {
1165 error = ravb_dmac_init(ndev);
1166 if (error) {
1167 netdev_err(ndev,
1168 "%s: ravb_dmac_init() failed, error %d\n",
1169 __func__, error);
1170 return error;
1171 }
1172
1173 ravb_emac_init(ndev);
1174
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001175 /* Initialise PTP Clock driver */
1176 ravb_ptp_init(ndev, priv->pdev);
1177
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001178 netif_device_attach(ndev);
1179 }
1180
1181 return 0;
1182}
1183
1184static int ravb_get_ts_info(struct net_device *ndev,
1185 struct ethtool_ts_info *info)
1186{
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001187 struct ravb_private *priv = netdev_priv(ndev);
1188
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001189 info->so_timestamping =
1190 SOF_TIMESTAMPING_TX_SOFTWARE |
1191 SOF_TIMESTAMPING_RX_SOFTWARE |
1192 SOF_TIMESTAMPING_SOFTWARE |
1193 SOF_TIMESTAMPING_TX_HARDWARE |
1194 SOF_TIMESTAMPING_RX_HARDWARE |
1195 SOF_TIMESTAMPING_RAW_HARDWARE;
1196 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1197 info->rx_filters =
1198 (1 << HWTSTAMP_FILTER_NONE) |
1199 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1200 (1 << HWTSTAMP_FILTER_ALL);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001201 info->phc_index = ptp_clock_index(priv->ptp.clock);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001202
1203 return 0;
1204}
1205
1206static const struct ethtool_ops ravb_ethtool_ops = {
1207 .get_settings = ravb_get_settings,
1208 .set_settings = ravb_set_settings,
1209 .nway_reset = ravb_nway_reset,
1210 .get_msglevel = ravb_get_msglevel,
1211 .set_msglevel = ravb_set_msglevel,
1212 .get_link = ethtool_op_get_link,
1213 .get_strings = ravb_get_strings,
1214 .get_ethtool_stats = ravb_get_ethtool_stats,
1215 .get_sset_count = ravb_get_sset_count,
1216 .get_ringparam = ravb_get_ringparam,
1217 .set_ringparam = ravb_set_ringparam,
1218 .get_ts_info = ravb_get_ts_info,
1219};
1220
1221/* Network device open function for Ethernet AVB */
1222static int ravb_open(struct net_device *ndev)
1223{
1224 struct ravb_private *priv = netdev_priv(ndev);
1225 int error;
1226
1227 napi_enable(&priv->napi[RAVB_BE]);
1228 napi_enable(&priv->napi[RAVB_NC]);
1229
1230 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED, ndev->name,
1231 ndev);
1232 if (error) {
1233 netdev_err(ndev, "cannot request IRQ\n");
1234 goto out_napi_off;
1235 }
1236
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001237 if (priv->chip_id == RCAR_GEN3) {
1238 error = request_irq(priv->emac_irq, ravb_interrupt,
1239 IRQF_SHARED, ndev->name, ndev);
1240 if (error) {
1241 netdev_err(ndev, "cannot request IRQ\n");
1242 goto out_free_irq;
1243 }
1244 }
1245
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001246 /* Device init */
1247 error = ravb_dmac_init(ndev);
1248 if (error)
Sergei Shtylyov508dc062015-11-19 01:39:51 +03001249 goto out_free_irq2;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001250 ravb_emac_init(ndev);
1251
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001252 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001253 if (priv->chip_id == RCAR_GEN2)
1254 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001255
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001256 netif_tx_start_all_queues(ndev);
1257
1258 /* PHY control start */
1259 error = ravb_phy_start(ndev);
1260 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001261 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001262
1263 return 0;
1264
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001265out_ptp_stop:
1266 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001267 if (priv->chip_id == RCAR_GEN2)
1268 ravb_ptp_stop(ndev);
Sergei Shtylyov508dc062015-11-19 01:39:51 +03001269out_free_irq2:
1270 if (priv->chip_id == RCAR_GEN3)
1271 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001272out_free_irq:
1273 free_irq(ndev->irq, ndev);
1274out_napi_off:
1275 napi_disable(&priv->napi[RAVB_NC]);
1276 napi_disable(&priv->napi[RAVB_BE]);
1277 return error;
1278}
1279
1280/* Timeout function for Ethernet AVB */
1281static void ravb_tx_timeout(struct net_device *ndev)
1282{
1283 struct ravb_private *priv = netdev_priv(ndev);
1284
1285 netif_err(priv, tx_err, ndev,
1286 "transmit timed out, status %08x, resetting...\n",
1287 ravb_read(ndev, ISS));
1288
1289 /* tx_errors count up */
1290 ndev->stats.tx_errors++;
1291
1292 schedule_work(&priv->work);
1293}
1294
1295static void ravb_tx_timeout_work(struct work_struct *work)
1296{
1297 struct ravb_private *priv = container_of(work, struct ravb_private,
1298 work);
1299 struct net_device *ndev = priv->ndev;
1300
1301 netif_tx_stop_all_queues(ndev);
1302
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001303 /* Stop PTP Clock driver */
1304 ravb_ptp_stop(ndev);
1305
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001306 /* Wait for DMA stopping */
1307 ravb_stop_dma(ndev);
1308
1309 ravb_ring_free(ndev, RAVB_BE);
1310 ravb_ring_free(ndev, RAVB_NC);
1311
1312 /* Device init */
1313 ravb_dmac_init(ndev);
1314 ravb_emac_init(ndev);
1315
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001316 /* Initialise PTP Clock driver */
1317 ravb_ptp_init(ndev, priv->pdev);
1318
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001319 netif_tx_start_all_queues(ndev);
1320}
1321
1322/* Packet transmit function for Ethernet AVB */
1323static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1324{
1325 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001326 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001327 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001328 struct ravb_tx_desc *desc;
1329 unsigned long flags;
1330 u32 dma_addr;
1331 void *buffer;
1332 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001333 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001334
1335 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001336 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1337 NUM_TX_DESC) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001338 netif_err(priv, tx_queued, ndev,
1339 "still transmitting with the full ring!\n");
1340 netif_stop_subqueue(ndev, q);
1341 spin_unlock_irqrestore(&priv->lock, flags);
1342 return NETDEV_TX_BUSY;
1343 }
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001344 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1345 priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001346
1347 if (skb_put_padto(skb, ETH_ZLEN))
1348 goto drop;
1349
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001350 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1351 entry / NUM_TX_DESC * DPTR_ALIGN;
1352 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
1353 memcpy(buffer, skb->data, len);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001354 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1355 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001356 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001357
1358 desc = &priv->tx_ring[q][entry];
1359 desc->ds_tagl = cpu_to_le16(len);
1360 desc->dptr = cpu_to_le32(dma_addr);
1361
1362 buffer = skb->data + len;
1363 len = skb->len - len;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001364 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1365 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001366 goto unmap;
1367
1368 desc++;
1369 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001370 desc->dptr = cpu_to_le32(dma_addr);
1371
1372 /* TX timestamp required */
1373 if (q == RAVB_NC) {
1374 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1375 if (!ts_skb) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001376 desc--;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001377 dma_unmap_single(ndev->dev.parent, dma_addr, len,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001378 DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001379 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001380 }
1381 ts_skb->skb = skb;
1382 ts_skb->tag = priv->ts_skb_tag++;
1383 priv->ts_skb_tag &= 0x3ff;
1384 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1385
1386 /* TAG and timestamp required flag */
1387 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1388 skb_tx_timestamp(skb);
1389 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1390 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1391 }
1392
1393 /* Descriptor type must be set after all the above writes */
1394 dma_wmb();
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001395 desc->die_dt = DT_FEND;
1396 desc--;
1397 desc->die_dt = DT_FSTART;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001398
Sergei Shtylyov06613e32015-07-17 00:28:38 +03001399 ravb_write(ndev, ravb_read(ndev, TCCR) | (TCCR_TSRQ0 << q), TCCR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001400
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001401 priv->cur_tx[q] += NUM_TX_DESC;
1402 if (priv->cur_tx[q] - priv->dirty_tx[q] >
1403 (priv->num_tx_ring[q] - 1) * NUM_TX_DESC && !ravb_tx_free(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001404 netif_stop_subqueue(ndev, q);
1405
1406exit:
1407 mmiowb();
1408 spin_unlock_irqrestore(&priv->lock, flags);
1409 return NETDEV_TX_OK;
1410
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001411unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001412 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001413 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001414drop:
1415 dev_kfree_skb_any(skb);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001416 priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001417 goto exit;
1418}
1419
1420static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1421 void *accel_priv, select_queue_fallback_t fallback)
1422{
1423 /* If skb needs TX timestamp, it is handled in network control queue */
1424 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1425 RAVB_BE;
1426
1427}
1428
1429static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1430{
1431 struct ravb_private *priv = netdev_priv(ndev);
1432 struct net_device_stats *nstats, *stats0, *stats1;
1433
1434 nstats = &ndev->stats;
1435 stats0 = &priv->stats[RAVB_BE];
1436 stats1 = &priv->stats[RAVB_NC];
1437
1438 nstats->tx_dropped += ravb_read(ndev, TROCR);
1439 ravb_write(ndev, 0, TROCR); /* (write clear) */
1440 nstats->collisions += ravb_read(ndev, CDCR);
1441 ravb_write(ndev, 0, CDCR); /* (write clear) */
1442 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1443 ravb_write(ndev, 0, LCCR); /* (write clear) */
1444
1445 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1446 ravb_write(ndev, 0, CERCR); /* (write clear) */
1447 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1448 ravb_write(ndev, 0, CEECR); /* (write clear) */
1449
1450 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1451 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1452 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1453 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1454 nstats->multicast = stats0->multicast + stats1->multicast;
1455 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1456 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1457 nstats->rx_frame_errors =
1458 stats0->rx_frame_errors + stats1->rx_frame_errors;
1459 nstats->rx_length_errors =
1460 stats0->rx_length_errors + stats1->rx_length_errors;
1461 nstats->rx_missed_errors =
1462 stats0->rx_missed_errors + stats1->rx_missed_errors;
1463 nstats->rx_over_errors =
1464 stats0->rx_over_errors + stats1->rx_over_errors;
1465
1466 return nstats;
1467}
1468
1469/* Update promiscuous bit */
1470static void ravb_set_rx_mode(struct net_device *ndev)
1471{
1472 struct ravb_private *priv = netdev_priv(ndev);
1473 unsigned long flags;
1474 u32 ecmr;
1475
1476 spin_lock_irqsave(&priv->lock, flags);
1477 ecmr = ravb_read(ndev, ECMR);
1478 if (ndev->flags & IFF_PROMISC)
1479 ecmr |= ECMR_PRM;
1480 else
1481 ecmr &= ~ECMR_PRM;
1482 ravb_write(ndev, ecmr, ECMR);
1483 mmiowb();
1484 spin_unlock_irqrestore(&priv->lock, flags);
1485}
1486
1487/* Device close function for Ethernet AVB */
1488static int ravb_close(struct net_device *ndev)
1489{
1490 struct ravb_private *priv = netdev_priv(ndev);
1491 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1492
1493 netif_tx_stop_all_queues(ndev);
1494
1495 /* Disable interrupts by clearing the interrupt masks. */
1496 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001497 ravb_write(ndev, 0, RIC2);
1498 ravb_write(ndev, 0, TIC);
1499
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001500 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001501 if (priv->chip_id == RCAR_GEN2)
1502 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001503
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001504 /* Set the config mode to stop the AVB-DMAC's processes */
1505 if (ravb_stop_dma(ndev) < 0)
1506 netdev_err(ndev,
1507 "device will be stopped after h/w processes are done.\n");
1508
1509 /* Clear the timestamp list */
1510 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1511 list_del(&ts_skb->list);
1512 kfree(ts_skb);
1513 }
1514
1515 /* PHY disconnect */
1516 if (priv->phydev) {
1517 phy_stop(priv->phydev);
1518 phy_disconnect(priv->phydev);
1519 priv->phydev = NULL;
1520 }
1521
1522 free_irq(ndev->irq, ndev);
1523
1524 napi_disable(&priv->napi[RAVB_NC]);
1525 napi_disable(&priv->napi[RAVB_BE]);
1526
1527 /* Free all the skb's in the RX queue and the DMA buffers. */
1528 ravb_ring_free(ndev, RAVB_BE);
1529 ravb_ring_free(ndev, RAVB_NC);
1530
1531 return 0;
1532}
1533
1534static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1535{
1536 struct ravb_private *priv = netdev_priv(ndev);
1537 struct hwtstamp_config config;
1538
1539 config.flags = 0;
1540 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1541 HWTSTAMP_TX_OFF;
1542 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1543 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1544 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1545 config.rx_filter = HWTSTAMP_FILTER_ALL;
1546 else
1547 config.rx_filter = HWTSTAMP_FILTER_NONE;
1548
1549 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1550 -EFAULT : 0;
1551}
1552
1553/* Control hardware time stamping */
1554static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1555{
1556 struct ravb_private *priv = netdev_priv(ndev);
1557 struct hwtstamp_config config;
1558 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1559 u32 tstamp_tx_ctrl;
1560
1561 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1562 return -EFAULT;
1563
1564 /* Reserved for future extensions */
1565 if (config.flags)
1566 return -EINVAL;
1567
1568 switch (config.tx_type) {
1569 case HWTSTAMP_TX_OFF:
1570 tstamp_tx_ctrl = 0;
1571 break;
1572 case HWTSTAMP_TX_ON:
1573 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1574 break;
1575 default:
1576 return -ERANGE;
1577 }
1578
1579 switch (config.rx_filter) {
1580 case HWTSTAMP_FILTER_NONE:
1581 tstamp_rx_ctrl = 0;
1582 break;
1583 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1584 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1585 break;
1586 default:
1587 config.rx_filter = HWTSTAMP_FILTER_ALL;
1588 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1589 }
1590
1591 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1592 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1593
1594 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1595 -EFAULT : 0;
1596}
1597
1598/* ioctl to device function */
1599static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1600{
1601 struct ravb_private *priv = netdev_priv(ndev);
1602 struct phy_device *phydev = priv->phydev;
1603
1604 if (!netif_running(ndev))
1605 return -EINVAL;
1606
1607 if (!phydev)
1608 return -ENODEV;
1609
1610 switch (cmd) {
1611 case SIOCGHWTSTAMP:
1612 return ravb_hwtstamp_get(ndev, req);
1613 case SIOCSHWTSTAMP:
1614 return ravb_hwtstamp_set(ndev, req);
1615 }
1616
1617 return phy_mii_ioctl(phydev, req, cmd);
1618}
1619
1620static const struct net_device_ops ravb_netdev_ops = {
1621 .ndo_open = ravb_open,
1622 .ndo_stop = ravb_close,
1623 .ndo_start_xmit = ravb_start_xmit,
1624 .ndo_select_queue = ravb_select_queue,
1625 .ndo_get_stats = ravb_get_stats,
1626 .ndo_set_rx_mode = ravb_set_rx_mode,
1627 .ndo_tx_timeout = ravb_tx_timeout,
1628 .ndo_do_ioctl = ravb_do_ioctl,
1629 .ndo_validate_addr = eth_validate_addr,
1630 .ndo_set_mac_address = eth_mac_addr,
1631 .ndo_change_mtu = eth_change_mtu,
1632};
1633
1634/* MDIO bus init function */
1635static int ravb_mdio_init(struct ravb_private *priv)
1636{
1637 struct platform_device *pdev = priv->pdev;
1638 struct device *dev = &pdev->dev;
1639 int error;
1640
1641 /* Bitbang init */
1642 priv->mdiobb.ops = &bb_ops;
1643
1644 /* MII controller setting */
1645 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1646 if (!priv->mii_bus)
1647 return -ENOMEM;
1648
1649 /* Hook up MII support for ethtool */
1650 priv->mii_bus->name = "ravb_mii";
1651 priv->mii_bus->parent = dev;
1652 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1653 pdev->name, pdev->id);
1654
1655 /* Register MDIO bus */
1656 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1657 if (error)
1658 goto out_free_bus;
1659
1660 return 0;
1661
1662out_free_bus:
1663 free_mdio_bitbang(priv->mii_bus);
1664 return error;
1665}
1666
1667/* MDIO bus release function */
1668static int ravb_mdio_release(struct ravb_private *priv)
1669{
1670 /* Unregister mdio bus */
1671 mdiobus_unregister(priv->mii_bus);
1672
1673 /* Free bitbang info */
1674 free_mdio_bitbang(priv->mii_bus);
1675
1676 return 0;
1677}
1678
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001679static const struct of_device_id ravb_match_table[] = {
1680 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1681 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001682 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001683 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001684 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001685 { }
1686};
1687MODULE_DEVICE_TABLE(of, ravb_match_table);
1688
Simon Hormanb3d39a82015-11-20 11:29:39 -08001689static int ravb_set_gti(struct net_device *ndev)
1690{
1691
1692 struct device *dev = ndev->dev.parent;
1693 struct device_node *np = dev->of_node;
1694 unsigned long rate;
1695 struct clk *clk;
1696 uint64_t inc;
1697
1698 clk = of_clk_get(np, 0);
1699 if (IS_ERR(clk)) {
1700 dev_err(dev, "could not get clock\n");
1701 return PTR_ERR(clk);
1702 }
1703
1704 rate = clk_get_rate(clk);
1705 clk_put(clk);
1706
1707 inc = 1000000000ULL << 20;
1708 do_div(inc, rate);
1709
1710 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1711 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1712 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1713 return -EINVAL;
1714 }
1715
1716 ravb_write(ndev, inc, GTI);
1717
1718 return 0;
1719}
1720
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001721static int ravb_probe(struct platform_device *pdev)
1722{
1723 struct device_node *np = pdev->dev.of_node;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001724 const struct of_device_id *match;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001725 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001726 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001727 struct net_device *ndev;
1728 int error, irq, q;
1729 struct resource *res;
1730
1731 if (!np) {
1732 dev_err(&pdev->dev,
1733 "this driver is required to be instantiated from device tree\n");
1734 return -EINVAL;
1735 }
1736
1737 /* Get base address */
1738 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1739 if (!res) {
1740 dev_err(&pdev->dev, "invalid resource\n");
1741 return -EINVAL;
1742 }
1743
1744 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
1745 NUM_TX_QUEUE, NUM_RX_QUEUE);
1746 if (!ndev)
1747 return -ENOMEM;
1748
1749 pm_runtime_enable(&pdev->dev);
1750 pm_runtime_get_sync(&pdev->dev);
1751
1752 /* The Ether-specific entries in the device structure. */
1753 ndev->base_addr = res->start;
1754 ndev->dma = -1;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001755
1756 match = of_match_device(of_match_ptr(ravb_match_table), &pdev->dev);
1757 chip_id = (enum ravb_chip_id)match->data;
1758
1759 if (chip_id == RCAR_GEN3)
1760 irq = platform_get_irq_byname(pdev, "ch22");
1761 else
1762 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001763 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03001764 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001765 goto out_release;
1766 }
1767 ndev->irq = irq;
1768
1769 SET_NETDEV_DEV(ndev, &pdev->dev);
1770
1771 priv = netdev_priv(ndev);
1772 priv->ndev = ndev;
1773 priv->pdev = pdev;
1774 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
1775 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
1776 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
1777 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
1778 priv->addr = devm_ioremap_resource(&pdev->dev, res);
1779 if (IS_ERR(priv->addr)) {
1780 error = PTR_ERR(priv->addr);
1781 goto out_release;
1782 }
1783
1784 spin_lock_init(&priv->lock);
1785 INIT_WORK(&priv->work, ravb_tx_timeout_work);
1786
1787 priv->phy_interface = of_get_phy_mode(np);
1788
1789 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
1790 priv->avb_link_active_low =
1791 of_property_read_bool(np, "renesas,ether-link-active-low");
1792
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001793 if (chip_id == RCAR_GEN3) {
1794 irq = platform_get_irq_byname(pdev, "ch24");
1795 if (irq < 0) {
1796 error = irq;
1797 goto out_release;
1798 }
1799 priv->emac_irq = irq;
1800 }
1801
1802 priv->chip_id = chip_id;
1803
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001804 /* Set function */
1805 ndev->netdev_ops = &ravb_netdev_ops;
1806 ndev->ethtool_ops = &ravb_ethtool_ops;
1807
1808 /* Set AVB config mode */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001809 if (chip_id == RCAR_GEN2) {
1810 ravb_write(ndev, (ravb_read(ndev, CCC) & ~CCC_OPC) |
1811 CCC_OPC_CONFIG, CCC);
1812 /* Set CSEL value */
1813 ravb_write(ndev, (ravb_read(ndev, CCC) & ~CCC_CSEL) |
1814 CCC_CSEL_HPB, CCC);
1815 } else {
1816 ravb_write(ndev, (ravb_read(ndev, CCC) & ~CCC_OPC) |
1817 CCC_OPC_CONFIG | CCC_GAC | CCC_CSEL_HPB, CCC);
1818 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001819
1820 /* Set CSEL value */
1821 ravb_write(ndev, (ravb_read(ndev, CCC) & ~CCC_CSEL) | CCC_CSEL_HPB,
1822 CCC);
1823
1824 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08001825 error = ravb_set_gti(ndev);
1826 if (error)
1827 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001828
1829 /* Request GTI loading */
1830 ravb_write(ndev, ravb_read(ndev, GCCR) | GCCR_LTI, GCCR);
1831
1832 /* Allocate descriptor base address table */
1833 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001834 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001835 &priv->desc_bat_dma, GFP_KERNEL);
1836 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09001837 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001838 "Cannot allocate desc base address table (size %d bytes)\n",
1839 priv->desc_bat_size);
1840 error = -ENOMEM;
1841 goto out_release;
1842 }
1843 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
1844 priv->desc_bat[q].die_dt = DT_EOS;
1845 ravb_write(ndev, priv->desc_bat_dma, DBAT);
1846
1847 /* Initialise HW timestamp list */
1848 INIT_LIST_HEAD(&priv->ts_skb_list);
1849
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001850 /* Initialise PTP Clock driver */
1851 if (chip_id != RCAR_GEN2)
1852 ravb_ptp_init(ndev, pdev);
1853
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001854 /* Debug message level */
1855 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
1856
1857 /* Read and set MAC address */
1858 ravb_read_mac_address(ndev, of_get_mac_address(np));
1859 if (!is_valid_ether_addr(ndev->dev_addr)) {
1860 dev_warn(&pdev->dev,
1861 "no valid MAC address supplied, using a random one\n");
1862 eth_hw_addr_random(ndev);
1863 }
1864
1865 /* MDIO bus init */
1866 error = ravb_mdio_init(priv);
1867 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09001868 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001869 goto out_dma_free;
1870 }
1871
1872 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
1873 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
1874
1875 /* Network device register */
1876 error = register_netdev(ndev);
1877 if (error)
1878 goto out_napi_del;
1879
1880 /* Print device information */
1881 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
1882 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
1883
1884 platform_set_drvdata(pdev, ndev);
1885
1886 return 0;
1887
1888out_napi_del:
1889 netif_napi_del(&priv->napi[RAVB_NC]);
1890 netif_napi_del(&priv->napi[RAVB_BE]);
1891 ravb_mdio_release(priv);
1892out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001893 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001894 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001895
1896 /* Stop PTP Clock driver */
1897 if (chip_id != RCAR_GEN2)
1898 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001899out_release:
1900 if (ndev)
1901 free_netdev(ndev);
1902
1903 pm_runtime_put(&pdev->dev);
1904 pm_runtime_disable(&pdev->dev);
1905 return error;
1906}
1907
1908static int ravb_remove(struct platform_device *pdev)
1909{
1910 struct net_device *ndev = platform_get_drvdata(pdev);
1911 struct ravb_private *priv = netdev_priv(ndev);
1912
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001913 /* Stop PTP Clock driver */
1914 if (priv->chip_id != RCAR_GEN2)
1915 ravb_ptp_stop(ndev);
1916
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001917 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001918 priv->desc_bat_dma);
1919 /* Set reset mode */
1920 ravb_write(ndev, CCC_OPC_RESET, CCC);
1921 pm_runtime_put_sync(&pdev->dev);
1922 unregister_netdev(ndev);
1923 netif_napi_del(&priv->napi[RAVB_NC]);
1924 netif_napi_del(&priv->napi[RAVB_BE]);
1925 ravb_mdio_release(priv);
1926 pm_runtime_disable(&pdev->dev);
1927 free_netdev(ndev);
1928 platform_set_drvdata(pdev, NULL);
1929
1930 return 0;
1931}
1932
1933#ifdef CONFIG_PM
1934static int ravb_runtime_nop(struct device *dev)
1935{
1936 /* Runtime PM callback shared between ->runtime_suspend()
1937 * and ->runtime_resume(). Simply returns success.
1938 *
1939 * This driver re-initializes all registers after
1940 * pm_runtime_get_sync() anyway so there is no need
1941 * to save and restore registers here.
1942 */
1943 return 0;
1944}
1945
1946static const struct dev_pm_ops ravb_dev_pm_ops = {
1947 .runtime_suspend = ravb_runtime_nop,
1948 .runtime_resume = ravb_runtime_nop,
1949};
1950
1951#define RAVB_PM_OPS (&ravb_dev_pm_ops)
1952#else
1953#define RAVB_PM_OPS NULL
1954#endif
1955
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001956static struct platform_driver ravb_driver = {
1957 .probe = ravb_probe,
1958 .remove = ravb_remove,
1959 .driver = {
1960 .name = "ravb",
1961 .pm = RAVB_PM_OPS,
1962 .of_match_table = ravb_match_table,
1963 },
1964};
1965
1966module_platform_driver(ravb_driver);
1967
1968MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
1969MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
1970MODULE_LICENSE("GPL v2");