blob: c936682aae68df0808eb68c63c25b42b0e3b4dca [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
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030045void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear,
46 u32 set)
47{
48 ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg);
49}
50
Sergei Shtylyova0d2f202015-06-11 01:02:30 +030051int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value)
Sergei Shtylyovc1566332015-06-11 01:01:43 +030052{
53 int i;
54
55 for (i = 0; i < 10000; i++) {
56 if ((ravb_read(ndev, reg) & mask) == value)
57 return 0;
58 udelay(10);
59 }
60 return -ETIMEDOUT;
61}
62
63static int ravb_config(struct net_device *ndev)
64{
65 int error;
66
67 /* Set config mode */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030068 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030069 /* Check if the operating mode is changed to the config mode */
70 error = ravb_wait(ndev, CSR, CSR_OPS, CSR_OPS_CONFIG);
71 if (error)
72 netdev_err(ndev, "failed to switch device to config mode\n");
73
74 return error;
75}
76
77static void ravb_set_duplex(struct net_device *ndev)
78{
79 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030080
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030081 ravb_modify(ndev, ECMR, ECMR_DM, priv->duplex ? ECMR_DM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030082}
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);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300134
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300135 ravb_modify(priv->ndev, PIR, mask, set ? mask : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300136}
137
138/* MDC pin control */
139static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level)
140{
141 ravb_mdio_ctrl(ctrl, PIR_MDC, level);
142}
143
144/* Data I/O pin control */
145static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
146{
147 ravb_mdio_ctrl(ctrl, PIR_MMD, output);
148}
149
150/* Set data bit */
151static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
152{
153 ravb_mdio_ctrl(ctrl, PIR_MDO, value);
154}
155
156/* Get data bit */
157static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl)
158{
159 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
160 mdiobb);
161
162 return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0;
163}
164
165/* MDIO bus control struct */
166static struct mdiobb_ops bb_ops = {
167 .owner = THIS_MODULE,
168 .set_mdc = ravb_set_mdc,
169 .set_mdio_dir = ravb_set_mdio_dir,
170 .set_mdio_data = ravb_set_mdio_data,
171 .get_mdio_data = ravb_get_mdio_data,
172};
173
174/* Free skb's and DMA buffers for Ethernet AVB */
175static void ravb_ring_free(struct net_device *ndev, int q)
176{
177 struct ravb_private *priv = netdev_priv(ndev);
178 int ring_size;
179 int i;
180
181 /* Free RX skb ringbuffer */
182 if (priv->rx_skb[q]) {
183 for (i = 0; i < priv->num_rx_ring[q]; i++)
184 dev_kfree_skb(priv->rx_skb[q][i]);
185 }
186 kfree(priv->rx_skb[q]);
187 priv->rx_skb[q] = NULL;
188
189 /* Free TX skb ringbuffer */
190 if (priv->tx_skb[q]) {
191 for (i = 0; i < priv->num_tx_ring[q]; i++)
192 dev_kfree_skb(priv->tx_skb[q][i]);
193 }
194 kfree(priv->tx_skb[q]);
195 priv->tx_skb[q] = NULL;
196
197 /* Free aligned TX buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300198 kfree(priv->tx_align[q]);
199 priv->tx_align[q] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300200
201 if (priv->rx_ring[q]) {
202 ring_size = sizeof(struct ravb_ex_rx_desc) *
203 (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900204 dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300205 priv->rx_desc_dma[q]);
206 priv->rx_ring[q] = NULL;
207 }
208
209 if (priv->tx_ring[q]) {
210 ring_size = sizeof(struct ravb_tx_desc) *
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300211 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900212 dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300213 priv->tx_desc_dma[q]);
214 priv->tx_ring[q] = NULL;
215 }
216}
217
218/* Format skb and descriptor buffer for Ethernet AVB */
219static void ravb_ring_format(struct net_device *ndev, int q)
220{
221 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300222 struct ravb_ex_rx_desc *rx_desc;
223 struct ravb_tx_desc *tx_desc;
224 struct ravb_desc *desc;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300225 int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q];
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300226 int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] *
227 NUM_TX_DESC;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300228 dma_addr_t dma_addr;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300229 int i;
230
231 priv->cur_rx[q] = 0;
232 priv->cur_tx[q] = 0;
233 priv->dirty_rx[q] = 0;
234 priv->dirty_tx[q] = 0;
235
236 memset(priv->rx_ring[q], 0, rx_ring_size);
237 /* Build RX ring buffer */
238 for (i = 0; i < priv->num_rx_ring[q]; i++) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300239 /* RX descriptor */
240 rx_desc = &priv->rx_ring[q][i];
241 /* The size of the buffer should be on 16-byte boundary. */
242 rx_desc->ds_cc = cpu_to_le16(ALIGN(PKT_BUF_SZ, 16));
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900243 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300244 ALIGN(PKT_BUF_SZ, 16),
245 DMA_FROM_DEVICE);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300246 /* We just set the data size to 0 for a failed mapping which
247 * should prevent DMA from happening...
248 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900249 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300250 rx_desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300251 rx_desc->dptr = cpu_to_le32(dma_addr);
252 rx_desc->die_dt = DT_FEMPTY;
253 }
254 rx_desc = &priv->rx_ring[q][i];
255 rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
256 rx_desc->die_dt = DT_LINKFIX; /* type */
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300257
258 memset(priv->tx_ring[q], 0, tx_ring_size);
259 /* Build TX ring buffer */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300260 for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
261 i++, tx_desc++) {
262 tx_desc->die_dt = DT_EEMPTY;
263 tx_desc++;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300264 tx_desc->die_dt = DT_EEMPTY;
265 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300266 tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
267 tx_desc->die_dt = DT_LINKFIX; /* type */
268
269 /* RX descriptor base address for best effort */
270 desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
271 desc->die_dt = DT_LINKFIX; /* type */
272 desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
273
274 /* TX descriptor base address for best effort */
275 desc = &priv->desc_bat[q];
276 desc->die_dt = DT_LINKFIX; /* type */
277 desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
278}
279
280/* Init skb and descriptor buffer for Ethernet AVB */
281static int ravb_ring_init(struct net_device *ndev, int q)
282{
283 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300284 struct sk_buff *skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300285 int ring_size;
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300286 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300287
288 /* Allocate RX and TX skb rings */
289 priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
290 sizeof(*priv->rx_skb[q]), GFP_KERNEL);
291 priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
292 sizeof(*priv->tx_skb[q]), GFP_KERNEL);
293 if (!priv->rx_skb[q] || !priv->tx_skb[q])
294 goto error;
295
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300296 for (i = 0; i < priv->num_rx_ring[q]; i++) {
297 skb = netdev_alloc_skb(ndev, PKT_BUF_SZ + RAVB_ALIGN - 1);
298 if (!skb)
299 goto error;
300 ravb_set_buffer_align(skb);
301 priv->rx_skb[q][i] = skb;
302 }
303
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300304 /* Allocate rings for the aligned buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300305 priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
306 DPTR_ALIGN - 1, GFP_KERNEL);
307 if (!priv->tx_align[q])
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300308 goto error;
309
310 /* Allocate all RX descriptors. */
311 ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900312 priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300313 &priv->rx_desc_dma[q],
314 GFP_KERNEL);
315 if (!priv->rx_ring[q])
316 goto error;
317
318 priv->dirty_rx[q] = 0;
319
320 /* Allocate all TX descriptors. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300321 ring_size = sizeof(struct ravb_tx_desc) *
322 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900323 priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300324 &priv->tx_desc_dma[q],
325 GFP_KERNEL);
326 if (!priv->tx_ring[q])
327 goto error;
328
329 return 0;
330
331error:
332 ravb_ring_free(ndev, q);
333
334 return -ENOMEM;
335}
336
337/* E-MAC init function */
338static void ravb_emac_init(struct net_device *ndev)
339{
340 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300341
342 /* Receive frame limit set register */
343 ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
344
345 /* PAUSE prohibition */
Sergei Shtylyov1c1fa822016-01-11 00:27:38 +0300346 ravb_write(ndev, ECMR_ZPF | (priv->duplex ? ECMR_DM : 0) |
347 ECMR_TE | ECMR_RE, ECMR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300348
349 ravb_set_rate(ndev);
350
351 /* Set MAC address */
352 ravb_write(ndev,
353 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
354 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
355 ravb_write(ndev,
356 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
357
358 ravb_write(ndev, 1, MPR);
359
360 /* E-MAC status register clear */
361 ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
362
363 /* E-MAC interrupt enable register */
364 ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
365}
366
367/* Device init function for Ethernet AVB */
368static int ravb_dmac_init(struct net_device *ndev)
369{
370 int error;
371
372 /* Set CONFIG mode */
373 error = ravb_config(ndev);
374 if (error)
375 return error;
376
377 error = ravb_ring_init(ndev, RAVB_BE);
378 if (error)
379 return error;
380 error = ravb_ring_init(ndev, RAVB_NC);
381 if (error) {
382 ravb_ring_free(ndev, RAVB_BE);
383 return error;
384 }
385
386 /* Descriptor format */
387 ravb_ring_format(ndev, RAVB_BE);
388 ravb_ring_format(ndev, RAVB_NC);
389
390#if defined(__LITTLE_ENDIAN)
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300391 ravb_modify(ndev, CCC, CCC_BOC, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300392#else
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300393 ravb_modify(ndev, CCC, CCC_BOC, CCC_BOC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300394#endif
395
396 /* Set AVB RX */
397 ravb_write(ndev, RCR_EFFS | RCR_ENCF | RCR_ETS0 | 0x18000000, RCR);
398
399 /* Set FIFO size */
400 ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00222200, TGC);
401
402 /* Timestamp enable */
403 ravb_write(ndev, TCCR_TFEN, TCCR);
404
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900405 /* Interrupt init: */
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300406 /* Frame receive */
407 ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900408 /* Disable FIFO full warning */
409 ravb_write(ndev, 0, RIC1);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300410 /* Receive FIFO full error, descriptor empty */
411 ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
412 /* Frame transmitted, timestamp FIFO updated */
413 ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
414
415 /* Setting the control will start the AVB-DMAC process. */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300416 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300417
418 return 0;
419}
420
421/* Free TX skb function for AVB-IP */
422static int ravb_tx_free(struct net_device *ndev, int q)
423{
424 struct ravb_private *priv = netdev_priv(ndev);
425 struct net_device_stats *stats = &priv->stats[q];
426 struct ravb_tx_desc *desc;
427 int free_num = 0;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300428 int entry;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300429 u32 size;
430
431 for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300432 entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
433 NUM_TX_DESC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300434 desc = &priv->tx_ring[q][entry];
435 if (desc->die_dt != DT_FEMPTY)
436 break;
437 /* Descriptor type must be checked before all other reads */
438 dma_rmb();
439 size = le16_to_cpu(desc->ds_tagl) & TX_DS;
440 /* Free the original skb. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300441 if (priv->tx_skb[q][entry / NUM_TX_DESC]) {
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900442 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300443 size, DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300444 /* Last packet descriptor? */
445 if (entry % NUM_TX_DESC == NUM_TX_DESC - 1) {
446 entry /= NUM_TX_DESC;
447 dev_kfree_skb_any(priv->tx_skb[q][entry]);
448 priv->tx_skb[q][entry] = NULL;
449 stats->tx_packets++;
450 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300451 free_num++;
452 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300453 stats->tx_bytes += size;
454 desc->die_dt = DT_EEMPTY;
455 }
456 return free_num;
457}
458
459static void ravb_get_tx_tstamp(struct net_device *ndev)
460{
461 struct ravb_private *priv = netdev_priv(ndev);
462 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
463 struct skb_shared_hwtstamps shhwtstamps;
464 struct sk_buff *skb;
465 struct timespec64 ts;
466 u16 tag, tfa_tag;
467 int count;
468 u32 tfa2;
469
470 count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
471 while (count--) {
472 tfa2 = ravb_read(ndev, TFA2);
473 tfa_tag = (tfa2 & TFA2_TST) >> 16;
474 ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
475 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
476 ravb_read(ndev, TFA1);
477 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
478 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
479 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
480 list) {
481 skb = ts_skb->skb;
482 tag = ts_skb->tag;
483 list_del(&ts_skb->list);
484 kfree(ts_skb);
485 if (tag == tfa_tag) {
486 skb_tstamp_tx(skb, &shhwtstamps);
487 break;
488 }
489 }
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300490 ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300491 }
492}
493
494/* Packet receive function for Ethernet AVB */
495static bool ravb_rx(struct net_device *ndev, int *quota, int q)
496{
497 struct ravb_private *priv = netdev_priv(ndev);
498 int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
499 int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
500 priv->cur_rx[q];
501 struct net_device_stats *stats = &priv->stats[q];
502 struct ravb_ex_rx_desc *desc;
503 struct sk_buff *skb;
504 dma_addr_t dma_addr;
505 struct timespec64 ts;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300506 u8 desc_status;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300507 u16 pkt_len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300508 int limit;
509
510 boguscnt = min(boguscnt, *quota);
511 limit = boguscnt;
512 desc = &priv->rx_ring[q][entry];
513 while (desc->die_dt != DT_FEMPTY) {
514 /* Descriptor type must be checked before all other reads */
515 dma_rmb();
516 desc_status = desc->msc;
517 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
518
519 if (--boguscnt < 0)
520 break;
521
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300522 /* We use 0-byte descriptors to mark the DMA mapping errors */
523 if (!pkt_len)
524 continue;
525
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300526 if (desc_status & MSC_MC)
527 stats->multicast++;
528
529 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
530 MSC_CEEF)) {
531 stats->rx_errors++;
532 if (desc_status & MSC_CRC)
533 stats->rx_crc_errors++;
534 if (desc_status & MSC_RFE)
535 stats->rx_frame_errors++;
536 if (desc_status & (MSC_RTLF | MSC_RTSF))
537 stats->rx_length_errors++;
538 if (desc_status & MSC_CEEF)
539 stats->rx_missed_errors++;
540 } else {
541 u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
542
543 skb = priv->rx_skb[q][entry];
544 priv->rx_skb[q][entry] = NULL;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900545 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyove2370f02015-07-15 00:56:52 +0300546 ALIGN(PKT_BUF_SZ, 16),
547 DMA_FROM_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300548 get_ts &= (q == RAVB_NC) ?
549 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
550 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
551 if (get_ts) {
552 struct skb_shared_hwtstamps *shhwtstamps;
553
554 shhwtstamps = skb_hwtstamps(skb);
555 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
556 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
557 32) | le32_to_cpu(desc->ts_sl);
558 ts.tv_nsec = le32_to_cpu(desc->ts_n);
559 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
560 }
561 skb_put(skb, pkt_len);
562 skb->protocol = eth_type_trans(skb, ndev);
563 napi_gro_receive(&priv->napi[q], skb);
564 stats->rx_packets++;
565 stats->rx_bytes += pkt_len;
566 }
567
568 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
569 desc = &priv->rx_ring[q][entry];
570 }
571
572 /* Refill the RX ring buffers. */
573 for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
574 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
575 desc = &priv->rx_ring[q][entry];
576 /* The size of the buffer should be on 16-byte boundary. */
577 desc->ds_cc = cpu_to_le16(ALIGN(PKT_BUF_SZ, 16));
578
579 if (!priv->rx_skb[q][entry]) {
580 skb = netdev_alloc_skb(ndev,
581 PKT_BUF_SZ + RAVB_ALIGN - 1);
582 if (!skb)
583 break; /* Better luck next round. */
584 ravb_set_buffer_align(skb);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900585 dma_addr = dma_map_single(ndev->dev.parent, skb->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300586 le16_to_cpu(desc->ds_cc),
587 DMA_FROM_DEVICE);
588 skb_checksum_none_assert(skb);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300589 /* We just set the data size to 0 for a failed mapping
590 * which should prevent DMA from happening...
591 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900592 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300593 desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300594 desc->dptr = cpu_to_le32(dma_addr);
595 priv->rx_skb[q][entry] = skb;
596 }
597 /* Descriptor type must be set after all the above writes */
598 dma_wmb();
599 desc->die_dt = DT_FEMPTY;
600 }
601
602 *quota -= limit - (++boguscnt);
603
604 return boguscnt <= 0;
605}
606
607static void ravb_rcv_snd_disable(struct net_device *ndev)
608{
609 /* Disable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300610 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300611}
612
613static void ravb_rcv_snd_enable(struct net_device *ndev)
614{
615 /* Enable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300616 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300617}
618
619/* function for waiting dma process finished */
620static int ravb_stop_dma(struct net_device *ndev)
621{
622 int error;
623
624 /* Wait for stopping the hardware TX process */
625 error = ravb_wait(ndev, TCCR,
626 TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
627 if (error)
628 return error;
629
630 error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
631 0);
632 if (error)
633 return error;
634
635 /* Stop the E-MAC's RX/TX processes. */
636 ravb_rcv_snd_disable(ndev);
637
638 /* Wait for stopping the RX DMA process */
639 error = ravb_wait(ndev, CSR, CSR_RPO, 0);
640 if (error)
641 return error;
642
643 /* Stop AVB-DMAC process */
644 return ravb_config(ndev);
645}
646
647/* E-MAC interrupt handler */
648static void ravb_emac_interrupt(struct net_device *ndev)
649{
650 struct ravb_private *priv = netdev_priv(ndev);
651 u32 ecsr, psr;
652
653 ecsr = ravb_read(ndev, ECSR);
654 ravb_write(ndev, ecsr, ECSR); /* clear interrupt */
655 if (ecsr & ECSR_ICD)
656 ndev->stats.tx_carrier_errors++;
657 if (ecsr & ECSR_LCHNG) {
658 /* Link changed */
659 if (priv->no_avb_link)
660 return;
661 psr = ravb_read(ndev, PSR);
662 if (priv->avb_link_active_low)
663 psr ^= PSR_LMON;
664 if (!(psr & PSR_LMON)) {
665 /* DIsable RX and TX */
666 ravb_rcv_snd_disable(ndev);
667 } else {
668 /* Enable RX and TX */
669 ravb_rcv_snd_enable(ndev);
670 }
671 }
672}
673
674/* Error interrupt handler */
675static void ravb_error_interrupt(struct net_device *ndev)
676{
677 struct ravb_private *priv = netdev_priv(ndev);
678 u32 eis, ris2;
679
680 eis = ravb_read(ndev, EIS);
681 ravb_write(ndev, ~EIS_QFS, EIS);
682 if (eis & EIS_QFS) {
683 ris2 = ravb_read(ndev, RIS2);
684 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF), RIS2);
685
686 /* Receive Descriptor Empty int */
687 if (ris2 & RIS2_QFF0)
688 priv->stats[RAVB_BE].rx_over_errors++;
689
690 /* Receive Descriptor Empty int */
691 if (ris2 & RIS2_QFF1)
692 priv->stats[RAVB_NC].rx_over_errors++;
693
694 /* Receive FIFO Overflow int */
695 if (ris2 & RIS2_RFFF)
696 priv->rx_fifo_errors++;
697 }
698}
699
700static irqreturn_t ravb_interrupt(int irq, void *dev_id)
701{
702 struct net_device *ndev = dev_id;
703 struct ravb_private *priv = netdev_priv(ndev);
704 irqreturn_t result = IRQ_NONE;
705 u32 iss;
706
707 spin_lock(&priv->lock);
708 /* Get interrupt status */
709 iss = ravb_read(ndev, ISS);
710
711 /* Received and transmitted interrupts */
712 if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
713 u32 ris0 = ravb_read(ndev, RIS0);
714 u32 ric0 = ravb_read(ndev, RIC0);
715 u32 tis = ravb_read(ndev, TIS);
716 u32 tic = ravb_read(ndev, TIC);
717 int q;
718
719 /* Timestamp updated */
720 if (tis & TIS_TFUF) {
721 ravb_write(ndev, ~TIS_TFUF, TIS);
722 ravb_get_tx_tstamp(ndev);
723 result = IRQ_HANDLED;
724 }
725
726 /* Network control and best effort queue RX/TX */
727 for (q = RAVB_NC; q >= RAVB_BE; q--) {
728 if (((ris0 & ric0) & BIT(q)) ||
729 ((tis & tic) & BIT(q))) {
730 if (napi_schedule_prep(&priv->napi[q])) {
731 /* Mask RX and TX interrupts */
Masaru Nagai2452cb02015-11-13 19:24:49 +0900732 ric0 &= ~BIT(q);
733 tic &= ~BIT(q);
734 ravb_write(ndev, ric0, RIC0);
735 ravb_write(ndev, tic, TIC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300736 __napi_schedule(&priv->napi[q]);
737 } else {
738 netdev_warn(ndev,
739 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
740 ris0, ric0);
741 netdev_warn(ndev,
742 " tx status 0x%08x, tx mask 0x%08x.\n",
743 tis, tic);
744 }
745 result = IRQ_HANDLED;
746 }
747 }
748 }
749
750 /* E-MAC status summary */
751 if (iss & ISS_MS) {
752 ravb_emac_interrupt(ndev);
753 result = IRQ_HANDLED;
754 }
755
756 /* Error status summary */
757 if (iss & ISS_ES) {
758 ravb_error_interrupt(ndev);
759 result = IRQ_HANDLED;
760 }
761
Sergei Shtylyova0d2f202015-06-11 01:02:30 +0300762 if (iss & ISS_CGIS)
763 result = ravb_ptp_interrupt(ndev);
764
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300765 mmiowb();
766 spin_unlock(&priv->lock);
767 return result;
768}
769
770static int ravb_poll(struct napi_struct *napi, int budget)
771{
772 struct net_device *ndev = napi->dev;
773 struct ravb_private *priv = netdev_priv(ndev);
774 unsigned long flags;
775 int q = napi - priv->napi;
776 int mask = BIT(q);
777 int quota = budget;
778 u32 ris0, tis;
779
780 for (;;) {
781 tis = ravb_read(ndev, TIS);
782 ris0 = ravb_read(ndev, RIS0);
783 if (!((ris0 & mask) || (tis & mask)))
784 break;
785
786 /* Processing RX Descriptor Ring */
787 if (ris0 & mask) {
788 /* Clear RX interrupt */
789 ravb_write(ndev, ~mask, RIS0);
790 if (ravb_rx(ndev, &quota, q))
791 goto out;
792 }
793 /* Processing TX Descriptor Ring */
794 if (tis & mask) {
795 spin_lock_irqsave(&priv->lock, flags);
796 /* Clear TX interrupt */
797 ravb_write(ndev, ~mask, TIS);
798 ravb_tx_free(ndev, q);
799 netif_wake_subqueue(ndev, q);
800 mmiowb();
801 spin_unlock_irqrestore(&priv->lock, flags);
802 }
803 }
804
805 napi_complete(napi);
806
807 /* Re-enable RX/TX interrupts */
808 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300809 ravb_modify(ndev, RIC0, mask, mask);
810 ravb_modify(ndev, TIC, mask, mask);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300811 mmiowb();
812 spin_unlock_irqrestore(&priv->lock, flags);
813
814 /* Receive error message handling */
815 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
816 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
817 if (priv->rx_over_errors != ndev->stats.rx_over_errors) {
818 ndev->stats.rx_over_errors = priv->rx_over_errors;
819 netif_err(priv, rx_err, ndev, "Receive Descriptor Empty\n");
820 }
821 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors) {
822 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
823 netif_err(priv, rx_err, ndev, "Receive FIFO Overflow\n");
824 }
825out:
826 return budget - quota;
827}
828
829/* PHY state control function */
830static void ravb_adjust_link(struct net_device *ndev)
831{
832 struct ravb_private *priv = netdev_priv(ndev);
833 struct phy_device *phydev = priv->phydev;
834 bool new_state = false;
835
836 if (phydev->link) {
837 if (phydev->duplex != priv->duplex) {
838 new_state = true;
839 priv->duplex = phydev->duplex;
840 ravb_set_duplex(ndev);
841 }
842
843 if (phydev->speed != priv->speed) {
844 new_state = true;
845 priv->speed = phydev->speed;
846 ravb_set_rate(ndev);
847 }
848 if (!priv->link) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300849 ravb_modify(ndev, ECMR, ECMR_TXF, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300850 new_state = true;
851 priv->link = phydev->link;
852 if (priv->no_avb_link)
853 ravb_rcv_snd_enable(ndev);
854 }
855 } else if (priv->link) {
856 new_state = true;
857 priv->link = 0;
858 priv->speed = 0;
859 priv->duplex = -1;
860 if (priv->no_avb_link)
861 ravb_rcv_snd_disable(ndev);
862 }
863
864 if (new_state && netif_msg_link(priv))
865 phy_print_status(phydev);
866}
867
868/* PHY init function */
869static int ravb_phy_init(struct net_device *ndev)
870{
871 struct device_node *np = ndev->dev.parent->of_node;
872 struct ravb_private *priv = netdev_priv(ndev);
873 struct phy_device *phydev;
874 struct device_node *pn;
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900875 int err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300876
877 priv->link = 0;
878 priv->speed = 0;
879 priv->duplex = -1;
880
881 /* Try connecting to PHY */
882 pn = of_parse_phandle(np, "phy-handle", 0);
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900883 if (!pn) {
884 /* In the case of a fixed PHY, the DT node associated
885 * to the PHY is the Ethernet MAC DT node.
886 */
887 if (of_phy_is_fixed_link(np)) {
888 err = of_phy_register_fixed_link(np);
889 if (err)
890 return err;
891 }
892 pn = of_node_get(np);
893 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300894 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
895 priv->phy_interface);
896 if (!phydev) {
897 netdev_err(ndev, "failed to connect PHY\n");
898 return -ENOENT;
899 }
900
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +0900901 /* This driver only support 10/100Mbit speeds on Gen3
902 * at this time.
903 */
904 if (priv->chip_id == RCAR_GEN3) {
905 int err;
906
907 err = phy_set_max_speed(phydev, SPEED_100);
908 if (err) {
909 netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
910 phy_disconnect(phydev);
911 return err;
912 }
913
914 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
915 }
916
Kazuya Mizuguchi54499962015-12-14 00:15:58 +0900917 /* 10BASE is not supported */
918 phydev->supported &= ~PHY_10BT_FEATURES;
919
Andrew Lunn22209432016-01-06 20:11:13 +0100920 phy_attached_info(phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300921
922 priv->phydev = phydev;
923
924 return 0;
925}
926
927/* PHY control start function */
928static int ravb_phy_start(struct net_device *ndev)
929{
930 struct ravb_private *priv = netdev_priv(ndev);
931 int error;
932
933 error = ravb_phy_init(ndev);
934 if (error)
935 return error;
936
937 phy_start(priv->phydev);
938
939 return 0;
940}
941
942static int ravb_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
943{
944 struct ravb_private *priv = netdev_priv(ndev);
945 int error = -ENODEV;
946 unsigned long flags;
947
948 if (priv->phydev) {
949 spin_lock_irqsave(&priv->lock, flags);
950 error = phy_ethtool_gset(priv->phydev, ecmd);
951 spin_unlock_irqrestore(&priv->lock, flags);
952 }
953
954 return error;
955}
956
957static int ravb_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
958{
959 struct ravb_private *priv = netdev_priv(ndev);
960 unsigned long flags;
961 int error;
962
963 if (!priv->phydev)
964 return -ENODEV;
965
966 spin_lock_irqsave(&priv->lock, flags);
967
968 /* Disable TX and RX */
969 ravb_rcv_snd_disable(ndev);
970
971 error = phy_ethtool_sset(priv->phydev, ecmd);
972 if (error)
973 goto error_exit;
974
975 if (ecmd->duplex == DUPLEX_FULL)
976 priv->duplex = 1;
977 else
978 priv->duplex = 0;
979
980 ravb_set_duplex(ndev);
981
982error_exit:
983 mdelay(1);
984
985 /* Enable TX and RX */
986 ravb_rcv_snd_enable(ndev);
987
988 mmiowb();
989 spin_unlock_irqrestore(&priv->lock, flags);
990
991 return error;
992}
993
994static int ravb_nway_reset(struct net_device *ndev)
995{
996 struct ravb_private *priv = netdev_priv(ndev);
997 int error = -ENODEV;
998 unsigned long flags;
999
1000 if (priv->phydev) {
1001 spin_lock_irqsave(&priv->lock, flags);
1002 error = phy_start_aneg(priv->phydev);
1003 spin_unlock_irqrestore(&priv->lock, flags);
1004 }
1005
1006 return error;
1007}
1008
1009static u32 ravb_get_msglevel(struct net_device *ndev)
1010{
1011 struct ravb_private *priv = netdev_priv(ndev);
1012
1013 return priv->msg_enable;
1014}
1015
1016static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1017{
1018 struct ravb_private *priv = netdev_priv(ndev);
1019
1020 priv->msg_enable = value;
1021}
1022
1023static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1024 "rx_queue_0_current",
1025 "tx_queue_0_current",
1026 "rx_queue_0_dirty",
1027 "tx_queue_0_dirty",
1028 "rx_queue_0_packets",
1029 "tx_queue_0_packets",
1030 "rx_queue_0_bytes",
1031 "tx_queue_0_bytes",
1032 "rx_queue_0_mcast_packets",
1033 "rx_queue_0_errors",
1034 "rx_queue_0_crc_errors",
1035 "rx_queue_0_frame_errors",
1036 "rx_queue_0_length_errors",
1037 "rx_queue_0_missed_errors",
1038 "rx_queue_0_over_errors",
1039
1040 "rx_queue_1_current",
1041 "tx_queue_1_current",
1042 "rx_queue_1_dirty",
1043 "tx_queue_1_dirty",
1044 "rx_queue_1_packets",
1045 "tx_queue_1_packets",
1046 "rx_queue_1_bytes",
1047 "tx_queue_1_bytes",
1048 "rx_queue_1_mcast_packets",
1049 "rx_queue_1_errors",
1050 "rx_queue_1_crc_errors",
Sergei Shtylyovb17c1d92015-12-04 01:51:10 +03001051 "rx_queue_1_frame_errors",
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001052 "rx_queue_1_length_errors",
1053 "rx_queue_1_missed_errors",
1054 "rx_queue_1_over_errors",
1055};
1056
1057#define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1058
1059static int ravb_get_sset_count(struct net_device *netdev, int sset)
1060{
1061 switch (sset) {
1062 case ETH_SS_STATS:
1063 return RAVB_STATS_LEN;
1064 default:
1065 return -EOPNOTSUPP;
1066 }
1067}
1068
1069static void ravb_get_ethtool_stats(struct net_device *ndev,
1070 struct ethtool_stats *stats, u64 *data)
1071{
1072 struct ravb_private *priv = netdev_priv(ndev);
1073 int i = 0;
1074 int q;
1075
1076 /* Device-specific stats */
1077 for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1078 struct net_device_stats *stats = &priv->stats[q];
1079
1080 data[i++] = priv->cur_rx[q];
1081 data[i++] = priv->cur_tx[q];
1082 data[i++] = priv->dirty_rx[q];
1083 data[i++] = priv->dirty_tx[q];
1084 data[i++] = stats->rx_packets;
1085 data[i++] = stats->tx_packets;
1086 data[i++] = stats->rx_bytes;
1087 data[i++] = stats->tx_bytes;
1088 data[i++] = stats->multicast;
1089 data[i++] = stats->rx_errors;
1090 data[i++] = stats->rx_crc_errors;
1091 data[i++] = stats->rx_frame_errors;
1092 data[i++] = stats->rx_length_errors;
1093 data[i++] = stats->rx_missed_errors;
1094 data[i++] = stats->rx_over_errors;
1095 }
1096}
1097
1098static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1099{
1100 switch (stringset) {
1101 case ETH_SS_STATS:
1102 memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
1103 break;
1104 }
1105}
1106
1107static void ravb_get_ringparam(struct net_device *ndev,
1108 struct ethtool_ringparam *ring)
1109{
1110 struct ravb_private *priv = netdev_priv(ndev);
1111
1112 ring->rx_max_pending = BE_RX_RING_MAX;
1113 ring->tx_max_pending = BE_TX_RING_MAX;
1114 ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1115 ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1116}
1117
1118static int ravb_set_ringparam(struct net_device *ndev,
1119 struct ethtool_ringparam *ring)
1120{
1121 struct ravb_private *priv = netdev_priv(ndev);
1122 int error;
1123
1124 if (ring->tx_pending > BE_TX_RING_MAX ||
1125 ring->rx_pending > BE_RX_RING_MAX ||
1126 ring->tx_pending < BE_TX_RING_MIN ||
1127 ring->rx_pending < BE_RX_RING_MIN)
1128 return -EINVAL;
1129 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1130 return -EINVAL;
1131
1132 if (netif_running(ndev)) {
1133 netif_device_detach(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001134 /* Stop PTP Clock driver */
1135 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001136 /* Wait for DMA stopping */
1137 error = ravb_stop_dma(ndev);
1138 if (error) {
1139 netdev_err(ndev,
1140 "cannot set ringparam! Any AVB processes are still running?\n");
1141 return error;
1142 }
1143 synchronize_irq(ndev->irq);
1144
1145 /* Free all the skb's in the RX queue and the DMA buffers. */
1146 ravb_ring_free(ndev, RAVB_BE);
1147 ravb_ring_free(ndev, RAVB_NC);
1148 }
1149
1150 /* Set new parameters */
1151 priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1152 priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1153
1154 if (netif_running(ndev)) {
1155 error = ravb_dmac_init(ndev);
1156 if (error) {
1157 netdev_err(ndev,
1158 "%s: ravb_dmac_init() failed, error %d\n",
1159 __func__, error);
1160 return error;
1161 }
1162
1163 ravb_emac_init(ndev);
1164
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001165 /* Initialise PTP Clock driver */
1166 ravb_ptp_init(ndev, priv->pdev);
1167
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001168 netif_device_attach(ndev);
1169 }
1170
1171 return 0;
1172}
1173
1174static int ravb_get_ts_info(struct net_device *ndev,
1175 struct ethtool_ts_info *info)
1176{
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001177 struct ravb_private *priv = netdev_priv(ndev);
1178
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001179 info->so_timestamping =
1180 SOF_TIMESTAMPING_TX_SOFTWARE |
1181 SOF_TIMESTAMPING_RX_SOFTWARE |
1182 SOF_TIMESTAMPING_SOFTWARE |
1183 SOF_TIMESTAMPING_TX_HARDWARE |
1184 SOF_TIMESTAMPING_RX_HARDWARE |
1185 SOF_TIMESTAMPING_RAW_HARDWARE;
1186 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1187 info->rx_filters =
1188 (1 << HWTSTAMP_FILTER_NONE) |
1189 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1190 (1 << HWTSTAMP_FILTER_ALL);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001191 info->phc_index = ptp_clock_index(priv->ptp.clock);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001192
1193 return 0;
1194}
1195
1196static const struct ethtool_ops ravb_ethtool_ops = {
1197 .get_settings = ravb_get_settings,
1198 .set_settings = ravb_set_settings,
1199 .nway_reset = ravb_nway_reset,
1200 .get_msglevel = ravb_get_msglevel,
1201 .set_msglevel = ravb_set_msglevel,
1202 .get_link = ethtool_op_get_link,
1203 .get_strings = ravb_get_strings,
1204 .get_ethtool_stats = ravb_get_ethtool_stats,
1205 .get_sset_count = ravb_get_sset_count,
1206 .get_ringparam = ravb_get_ringparam,
1207 .set_ringparam = ravb_set_ringparam,
1208 .get_ts_info = ravb_get_ts_info,
1209};
1210
1211/* Network device open function for Ethernet AVB */
1212static int ravb_open(struct net_device *ndev)
1213{
1214 struct ravb_private *priv = netdev_priv(ndev);
1215 int error;
1216
1217 napi_enable(&priv->napi[RAVB_BE]);
1218 napi_enable(&priv->napi[RAVB_NC]);
1219
1220 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED, ndev->name,
1221 ndev);
1222 if (error) {
1223 netdev_err(ndev, "cannot request IRQ\n");
1224 goto out_napi_off;
1225 }
1226
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001227 if (priv->chip_id == RCAR_GEN3) {
1228 error = request_irq(priv->emac_irq, ravb_interrupt,
1229 IRQF_SHARED, ndev->name, ndev);
1230 if (error) {
1231 netdev_err(ndev, "cannot request IRQ\n");
1232 goto out_free_irq;
1233 }
1234 }
1235
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001236 /* Device init */
1237 error = ravb_dmac_init(ndev);
1238 if (error)
Sergei Shtylyov508dc062015-11-19 01:39:51 +03001239 goto out_free_irq2;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001240 ravb_emac_init(ndev);
1241
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001242 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001243 if (priv->chip_id == RCAR_GEN2)
1244 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001245
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001246 netif_tx_start_all_queues(ndev);
1247
1248 /* PHY control start */
1249 error = ravb_phy_start(ndev);
1250 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001251 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001252
1253 return 0;
1254
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001255out_ptp_stop:
1256 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001257 if (priv->chip_id == RCAR_GEN2)
1258 ravb_ptp_stop(ndev);
Sergei Shtylyov508dc062015-11-19 01:39:51 +03001259out_free_irq2:
1260 if (priv->chip_id == RCAR_GEN3)
1261 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001262out_free_irq:
1263 free_irq(ndev->irq, ndev);
1264out_napi_off:
1265 napi_disable(&priv->napi[RAVB_NC]);
1266 napi_disable(&priv->napi[RAVB_BE]);
1267 return error;
1268}
1269
1270/* Timeout function for Ethernet AVB */
1271static void ravb_tx_timeout(struct net_device *ndev)
1272{
1273 struct ravb_private *priv = netdev_priv(ndev);
1274
1275 netif_err(priv, tx_err, ndev,
1276 "transmit timed out, status %08x, resetting...\n",
1277 ravb_read(ndev, ISS));
1278
1279 /* tx_errors count up */
1280 ndev->stats.tx_errors++;
1281
1282 schedule_work(&priv->work);
1283}
1284
1285static void ravb_tx_timeout_work(struct work_struct *work)
1286{
1287 struct ravb_private *priv = container_of(work, struct ravb_private,
1288 work);
1289 struct net_device *ndev = priv->ndev;
1290
1291 netif_tx_stop_all_queues(ndev);
1292
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001293 /* Stop PTP Clock driver */
1294 ravb_ptp_stop(ndev);
1295
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001296 /* Wait for DMA stopping */
1297 ravb_stop_dma(ndev);
1298
1299 ravb_ring_free(ndev, RAVB_BE);
1300 ravb_ring_free(ndev, RAVB_NC);
1301
1302 /* Device init */
1303 ravb_dmac_init(ndev);
1304 ravb_emac_init(ndev);
1305
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001306 /* Initialise PTP Clock driver */
1307 ravb_ptp_init(ndev, priv->pdev);
1308
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001309 netif_tx_start_all_queues(ndev);
1310}
1311
1312/* Packet transmit function for Ethernet AVB */
1313static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1314{
1315 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001316 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001317 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001318 struct ravb_tx_desc *desc;
1319 unsigned long flags;
1320 u32 dma_addr;
1321 void *buffer;
1322 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001323 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001324
1325 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001326 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1327 NUM_TX_DESC) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001328 netif_err(priv, tx_queued, ndev,
1329 "still transmitting with the full ring!\n");
1330 netif_stop_subqueue(ndev, q);
1331 spin_unlock_irqrestore(&priv->lock, flags);
1332 return NETDEV_TX_BUSY;
1333 }
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001334 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1335 priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001336
1337 if (skb_put_padto(skb, ETH_ZLEN))
1338 goto drop;
1339
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001340 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1341 entry / NUM_TX_DESC * DPTR_ALIGN;
1342 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
1343 memcpy(buffer, skb->data, len);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001344 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1345 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001346 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001347
1348 desc = &priv->tx_ring[q][entry];
1349 desc->ds_tagl = cpu_to_le16(len);
1350 desc->dptr = cpu_to_le32(dma_addr);
1351
1352 buffer = skb->data + len;
1353 len = skb->len - 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 Shtylyov2f45d192015-07-25 23:42:01 +03001356 goto unmap;
1357
1358 desc++;
1359 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001360 desc->dptr = cpu_to_le32(dma_addr);
1361
1362 /* TX timestamp required */
1363 if (q == RAVB_NC) {
1364 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1365 if (!ts_skb) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001366 desc--;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001367 dma_unmap_single(ndev->dev.parent, dma_addr, len,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001368 DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001369 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001370 }
1371 ts_skb->skb = skb;
1372 ts_skb->tag = priv->ts_skb_tag++;
1373 priv->ts_skb_tag &= 0x3ff;
1374 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1375
1376 /* TAG and timestamp required flag */
1377 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1378 skb_tx_timestamp(skb);
1379 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1380 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1381 }
1382
1383 /* Descriptor type must be set after all the above writes */
1384 dma_wmb();
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001385 desc->die_dt = DT_FEND;
1386 desc--;
1387 desc->die_dt = DT_FSTART;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001388
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001389 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001390
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001391 priv->cur_tx[q] += NUM_TX_DESC;
1392 if (priv->cur_tx[q] - priv->dirty_tx[q] >
1393 (priv->num_tx_ring[q] - 1) * NUM_TX_DESC && !ravb_tx_free(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001394 netif_stop_subqueue(ndev, q);
1395
1396exit:
1397 mmiowb();
1398 spin_unlock_irqrestore(&priv->lock, flags);
1399 return NETDEV_TX_OK;
1400
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001401unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001402 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001403 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001404drop:
1405 dev_kfree_skb_any(skb);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001406 priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001407 goto exit;
1408}
1409
1410static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1411 void *accel_priv, select_queue_fallback_t fallback)
1412{
1413 /* If skb needs TX timestamp, it is handled in network control queue */
1414 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1415 RAVB_BE;
1416
1417}
1418
1419static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1420{
1421 struct ravb_private *priv = netdev_priv(ndev);
1422 struct net_device_stats *nstats, *stats0, *stats1;
1423
1424 nstats = &ndev->stats;
1425 stats0 = &priv->stats[RAVB_BE];
1426 stats1 = &priv->stats[RAVB_NC];
1427
1428 nstats->tx_dropped += ravb_read(ndev, TROCR);
1429 ravb_write(ndev, 0, TROCR); /* (write clear) */
1430 nstats->collisions += ravb_read(ndev, CDCR);
1431 ravb_write(ndev, 0, CDCR); /* (write clear) */
1432 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1433 ravb_write(ndev, 0, LCCR); /* (write clear) */
1434
1435 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1436 ravb_write(ndev, 0, CERCR); /* (write clear) */
1437 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1438 ravb_write(ndev, 0, CEECR); /* (write clear) */
1439
1440 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1441 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1442 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1443 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1444 nstats->multicast = stats0->multicast + stats1->multicast;
1445 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1446 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1447 nstats->rx_frame_errors =
1448 stats0->rx_frame_errors + stats1->rx_frame_errors;
1449 nstats->rx_length_errors =
1450 stats0->rx_length_errors + stats1->rx_length_errors;
1451 nstats->rx_missed_errors =
1452 stats0->rx_missed_errors + stats1->rx_missed_errors;
1453 nstats->rx_over_errors =
1454 stats0->rx_over_errors + stats1->rx_over_errors;
1455
1456 return nstats;
1457}
1458
1459/* Update promiscuous bit */
1460static void ravb_set_rx_mode(struct net_device *ndev)
1461{
1462 struct ravb_private *priv = netdev_priv(ndev);
1463 unsigned long flags;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001464
1465 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001466 ravb_modify(ndev, ECMR, ECMR_PRM,
1467 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001468 mmiowb();
1469 spin_unlock_irqrestore(&priv->lock, flags);
1470}
1471
1472/* Device close function for Ethernet AVB */
1473static int ravb_close(struct net_device *ndev)
1474{
1475 struct ravb_private *priv = netdev_priv(ndev);
1476 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1477
1478 netif_tx_stop_all_queues(ndev);
1479
1480 /* Disable interrupts by clearing the interrupt masks. */
1481 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001482 ravb_write(ndev, 0, RIC2);
1483 ravb_write(ndev, 0, TIC);
1484
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001485 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001486 if (priv->chip_id == RCAR_GEN2)
1487 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001488
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001489 /* Set the config mode to stop the AVB-DMAC's processes */
1490 if (ravb_stop_dma(ndev) < 0)
1491 netdev_err(ndev,
1492 "device will be stopped after h/w processes are done.\n");
1493
1494 /* Clear the timestamp list */
1495 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1496 list_del(&ts_skb->list);
1497 kfree(ts_skb);
1498 }
1499
1500 /* PHY disconnect */
1501 if (priv->phydev) {
1502 phy_stop(priv->phydev);
1503 phy_disconnect(priv->phydev);
1504 priv->phydev = NULL;
1505 }
1506
1507 free_irq(ndev->irq, ndev);
1508
1509 napi_disable(&priv->napi[RAVB_NC]);
1510 napi_disable(&priv->napi[RAVB_BE]);
1511
1512 /* Free all the skb's in the RX queue and the DMA buffers. */
1513 ravb_ring_free(ndev, RAVB_BE);
1514 ravb_ring_free(ndev, RAVB_NC);
1515
1516 return 0;
1517}
1518
1519static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1520{
1521 struct ravb_private *priv = netdev_priv(ndev);
1522 struct hwtstamp_config config;
1523
1524 config.flags = 0;
1525 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1526 HWTSTAMP_TX_OFF;
1527 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1528 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1529 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1530 config.rx_filter = HWTSTAMP_FILTER_ALL;
1531 else
1532 config.rx_filter = HWTSTAMP_FILTER_NONE;
1533
1534 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1535 -EFAULT : 0;
1536}
1537
1538/* Control hardware time stamping */
1539static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1540{
1541 struct ravb_private *priv = netdev_priv(ndev);
1542 struct hwtstamp_config config;
1543 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1544 u32 tstamp_tx_ctrl;
1545
1546 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1547 return -EFAULT;
1548
1549 /* Reserved for future extensions */
1550 if (config.flags)
1551 return -EINVAL;
1552
1553 switch (config.tx_type) {
1554 case HWTSTAMP_TX_OFF:
1555 tstamp_tx_ctrl = 0;
1556 break;
1557 case HWTSTAMP_TX_ON:
1558 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1559 break;
1560 default:
1561 return -ERANGE;
1562 }
1563
1564 switch (config.rx_filter) {
1565 case HWTSTAMP_FILTER_NONE:
1566 tstamp_rx_ctrl = 0;
1567 break;
1568 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1569 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1570 break;
1571 default:
1572 config.rx_filter = HWTSTAMP_FILTER_ALL;
1573 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1574 }
1575
1576 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1577 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1578
1579 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1580 -EFAULT : 0;
1581}
1582
1583/* ioctl to device function */
1584static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1585{
1586 struct ravb_private *priv = netdev_priv(ndev);
1587 struct phy_device *phydev = priv->phydev;
1588
1589 if (!netif_running(ndev))
1590 return -EINVAL;
1591
1592 if (!phydev)
1593 return -ENODEV;
1594
1595 switch (cmd) {
1596 case SIOCGHWTSTAMP:
1597 return ravb_hwtstamp_get(ndev, req);
1598 case SIOCSHWTSTAMP:
1599 return ravb_hwtstamp_set(ndev, req);
1600 }
1601
1602 return phy_mii_ioctl(phydev, req, cmd);
1603}
1604
1605static const struct net_device_ops ravb_netdev_ops = {
1606 .ndo_open = ravb_open,
1607 .ndo_stop = ravb_close,
1608 .ndo_start_xmit = ravb_start_xmit,
1609 .ndo_select_queue = ravb_select_queue,
1610 .ndo_get_stats = ravb_get_stats,
1611 .ndo_set_rx_mode = ravb_set_rx_mode,
1612 .ndo_tx_timeout = ravb_tx_timeout,
1613 .ndo_do_ioctl = ravb_do_ioctl,
1614 .ndo_validate_addr = eth_validate_addr,
1615 .ndo_set_mac_address = eth_mac_addr,
1616 .ndo_change_mtu = eth_change_mtu,
1617};
1618
1619/* MDIO bus init function */
1620static int ravb_mdio_init(struct ravb_private *priv)
1621{
1622 struct platform_device *pdev = priv->pdev;
1623 struct device *dev = &pdev->dev;
1624 int error;
1625
1626 /* Bitbang init */
1627 priv->mdiobb.ops = &bb_ops;
1628
1629 /* MII controller setting */
1630 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1631 if (!priv->mii_bus)
1632 return -ENOMEM;
1633
1634 /* Hook up MII support for ethtool */
1635 priv->mii_bus->name = "ravb_mii";
1636 priv->mii_bus->parent = dev;
1637 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1638 pdev->name, pdev->id);
1639
1640 /* Register MDIO bus */
1641 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1642 if (error)
1643 goto out_free_bus;
1644
1645 return 0;
1646
1647out_free_bus:
1648 free_mdio_bitbang(priv->mii_bus);
1649 return error;
1650}
1651
1652/* MDIO bus release function */
1653static int ravb_mdio_release(struct ravb_private *priv)
1654{
1655 /* Unregister mdio bus */
1656 mdiobus_unregister(priv->mii_bus);
1657
1658 /* Free bitbang info */
1659 free_mdio_bitbang(priv->mii_bus);
1660
1661 return 0;
1662}
1663
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001664static const struct of_device_id ravb_match_table[] = {
1665 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1666 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001667 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001668 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001669 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001670 { }
1671};
1672MODULE_DEVICE_TABLE(of, ravb_match_table);
1673
Simon Hormanb3d39a82015-11-20 11:29:39 -08001674static int ravb_set_gti(struct net_device *ndev)
1675{
1676
1677 struct device *dev = ndev->dev.parent;
1678 struct device_node *np = dev->of_node;
1679 unsigned long rate;
1680 struct clk *clk;
1681 uint64_t inc;
1682
1683 clk = of_clk_get(np, 0);
1684 if (IS_ERR(clk)) {
1685 dev_err(dev, "could not get clock\n");
1686 return PTR_ERR(clk);
1687 }
1688
1689 rate = clk_get_rate(clk);
1690 clk_put(clk);
1691
1692 inc = 1000000000ULL << 20;
1693 do_div(inc, rate);
1694
1695 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1696 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1697 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1698 return -EINVAL;
1699 }
1700
1701 ravb_write(ndev, inc, GTI);
1702
1703 return 0;
1704}
1705
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001706static int ravb_probe(struct platform_device *pdev)
1707{
1708 struct device_node *np = pdev->dev.of_node;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001709 const struct of_device_id *match;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001710 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001711 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001712 struct net_device *ndev;
1713 int error, irq, q;
1714 struct resource *res;
1715
1716 if (!np) {
1717 dev_err(&pdev->dev,
1718 "this driver is required to be instantiated from device tree\n");
1719 return -EINVAL;
1720 }
1721
1722 /* Get base address */
1723 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1724 if (!res) {
1725 dev_err(&pdev->dev, "invalid resource\n");
1726 return -EINVAL;
1727 }
1728
1729 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
1730 NUM_TX_QUEUE, NUM_RX_QUEUE);
1731 if (!ndev)
1732 return -ENOMEM;
1733
1734 pm_runtime_enable(&pdev->dev);
1735 pm_runtime_get_sync(&pdev->dev);
1736
1737 /* The Ether-specific entries in the device structure. */
1738 ndev->base_addr = res->start;
1739 ndev->dma = -1;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001740
1741 match = of_match_device(of_match_ptr(ravb_match_table), &pdev->dev);
1742 chip_id = (enum ravb_chip_id)match->data;
1743
1744 if (chip_id == RCAR_GEN3)
1745 irq = platform_get_irq_byname(pdev, "ch22");
1746 else
1747 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001748 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03001749 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001750 goto out_release;
1751 }
1752 ndev->irq = irq;
1753
1754 SET_NETDEV_DEV(ndev, &pdev->dev);
1755
1756 priv = netdev_priv(ndev);
1757 priv->ndev = ndev;
1758 priv->pdev = pdev;
1759 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
1760 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
1761 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
1762 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
1763 priv->addr = devm_ioremap_resource(&pdev->dev, res);
1764 if (IS_ERR(priv->addr)) {
1765 error = PTR_ERR(priv->addr);
1766 goto out_release;
1767 }
1768
1769 spin_lock_init(&priv->lock);
1770 INIT_WORK(&priv->work, ravb_tx_timeout_work);
1771
1772 priv->phy_interface = of_get_phy_mode(np);
1773
1774 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
1775 priv->avb_link_active_low =
1776 of_property_read_bool(np, "renesas,ether-link-active-low");
1777
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001778 if (chip_id == RCAR_GEN3) {
1779 irq = platform_get_irq_byname(pdev, "ch24");
1780 if (irq < 0) {
1781 error = irq;
1782 goto out_release;
1783 }
1784 priv->emac_irq = irq;
1785 }
1786
1787 priv->chip_id = chip_id;
1788
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001789 /* Set function */
1790 ndev->netdev_ops = &ravb_netdev_ops;
1791 ndev->ethtool_ops = &ravb_ethtool_ops;
1792
1793 /* Set AVB config mode */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001794 if (chip_id == RCAR_GEN2) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001795 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001796 /* Set CSEL value */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001797 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001798 } else {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001799 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
1800 CCC_GAC | CCC_CSEL_HPB);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001801 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001802
1803 /* Set CSEL value */
1804 ravb_write(ndev, (ravb_read(ndev, CCC) & ~CCC_CSEL) | CCC_CSEL_HPB,
1805 CCC);
1806
1807 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08001808 error = ravb_set_gti(ndev);
1809 if (error)
1810 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001811
1812 /* Request GTI loading */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001813 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001814
1815 /* Allocate descriptor base address table */
1816 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001817 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001818 &priv->desc_bat_dma, GFP_KERNEL);
1819 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09001820 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001821 "Cannot allocate desc base address table (size %d bytes)\n",
1822 priv->desc_bat_size);
1823 error = -ENOMEM;
1824 goto out_release;
1825 }
1826 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
1827 priv->desc_bat[q].die_dt = DT_EOS;
1828 ravb_write(ndev, priv->desc_bat_dma, DBAT);
1829
1830 /* Initialise HW timestamp list */
1831 INIT_LIST_HEAD(&priv->ts_skb_list);
1832
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001833 /* Initialise PTP Clock driver */
1834 if (chip_id != RCAR_GEN2)
1835 ravb_ptp_init(ndev, pdev);
1836
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001837 /* Debug message level */
1838 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
1839
1840 /* Read and set MAC address */
1841 ravb_read_mac_address(ndev, of_get_mac_address(np));
1842 if (!is_valid_ether_addr(ndev->dev_addr)) {
1843 dev_warn(&pdev->dev,
1844 "no valid MAC address supplied, using a random one\n");
1845 eth_hw_addr_random(ndev);
1846 }
1847
1848 /* MDIO bus init */
1849 error = ravb_mdio_init(priv);
1850 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09001851 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001852 goto out_dma_free;
1853 }
1854
1855 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
1856 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
1857
1858 /* Network device register */
1859 error = register_netdev(ndev);
1860 if (error)
1861 goto out_napi_del;
1862
1863 /* Print device information */
1864 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
1865 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
1866
1867 platform_set_drvdata(pdev, ndev);
1868
1869 return 0;
1870
1871out_napi_del:
1872 netif_napi_del(&priv->napi[RAVB_NC]);
1873 netif_napi_del(&priv->napi[RAVB_BE]);
1874 ravb_mdio_release(priv);
1875out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001876 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001877 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001878
1879 /* Stop PTP Clock driver */
1880 if (chip_id != RCAR_GEN2)
1881 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001882out_release:
1883 if (ndev)
1884 free_netdev(ndev);
1885
1886 pm_runtime_put(&pdev->dev);
1887 pm_runtime_disable(&pdev->dev);
1888 return error;
1889}
1890
1891static int ravb_remove(struct platform_device *pdev)
1892{
1893 struct net_device *ndev = platform_get_drvdata(pdev);
1894 struct ravb_private *priv = netdev_priv(ndev);
1895
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001896 /* Stop PTP Clock driver */
1897 if (priv->chip_id != RCAR_GEN2)
1898 ravb_ptp_stop(ndev);
1899
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001900 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001901 priv->desc_bat_dma);
1902 /* Set reset mode */
1903 ravb_write(ndev, CCC_OPC_RESET, CCC);
1904 pm_runtime_put_sync(&pdev->dev);
1905 unregister_netdev(ndev);
1906 netif_napi_del(&priv->napi[RAVB_NC]);
1907 netif_napi_del(&priv->napi[RAVB_BE]);
1908 ravb_mdio_release(priv);
1909 pm_runtime_disable(&pdev->dev);
1910 free_netdev(ndev);
1911 platform_set_drvdata(pdev, NULL);
1912
1913 return 0;
1914}
1915
1916#ifdef CONFIG_PM
1917static int ravb_runtime_nop(struct device *dev)
1918{
1919 /* Runtime PM callback shared between ->runtime_suspend()
1920 * and ->runtime_resume(). Simply returns success.
1921 *
1922 * This driver re-initializes all registers after
1923 * pm_runtime_get_sync() anyway so there is no need
1924 * to save and restore registers here.
1925 */
1926 return 0;
1927}
1928
1929static const struct dev_pm_ops ravb_dev_pm_ops = {
1930 .runtime_suspend = ravb_runtime_nop,
1931 .runtime_resume = ravb_runtime_nop,
1932};
1933
1934#define RAVB_PM_OPS (&ravb_dev_pm_ops)
1935#else
1936#define RAVB_PM_OPS NULL
1937#endif
1938
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001939static struct platform_driver ravb_driver = {
1940 .probe = ravb_probe,
1941 .remove = ravb_remove,
1942 .driver = {
1943 .name = "ravb",
1944 .pm = RAVB_PM_OPS,
1945 .of_match_table = ravb_match_table,
1946 },
1947};
1948
1949module_platform_driver(ravb_driver);
1950
1951MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
1952MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
1953MODULE_LICENSE("GPL v2");