blob: 8cfc4a54f2dc69240ae1fc42195ef854e0b1c2c9 [file] [log] [blame]
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001/* Renesas Ethernet AVB device driver
2 *
3 * Copyright (C) 2014-2015 Renesas Electronics Corporation
4 * Copyright (C) 2015 Renesas Solutions Corp.
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03005 * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
Sergei Shtylyovc1566332015-06-11 01:01:43 +03006 *
7 * Based on the SuperH Ethernet driver
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
12 */
13
14#include <linux/cache.h>
15#include <linux/clk.h>
16#include <linux/delay.h>
17#include <linux/dma-mapping.h>
18#include <linux/err.h>
19#include <linux/etherdevice.h>
20#include <linux/ethtool.h>
21#include <linux/if_vlan.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
24#include <linux/module.h>
25#include <linux/net_tstamp.h>
26#include <linux/of.h>
27#include <linux/of_device.h>
28#include <linux/of_irq.h>
29#include <linux/of_mdio.h>
30#include <linux/of_net.h>
Sergei Shtylyovc1566332015-06-11 01:01:43 +030031#include <linux/pm_runtime.h>
32#include <linux/slab.h>
33#include <linux/spinlock.h>
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +010034#include <linux/sys_soc.h>
Sergei Shtylyovc1566332015-06-11 01:01:43 +030035
Simon Hormanb3d39a82015-11-20 11:29:39 -080036#include <asm/div64.h>
37
Sergei Shtylyovc1566332015-06-11 01:01:43 +030038#include "ravb.h"
39
40#define RAVB_DEF_MSG_ENABLE \
41 (NETIF_MSG_LINK | \
42 NETIF_MSG_TIMER | \
43 NETIF_MSG_RX_ERR | \
44 NETIF_MSG_TX_ERR)
45
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +090046static const char *ravb_rx_irqs[NUM_RX_QUEUE] = {
47 "ch0", /* RAVB_BE */
48 "ch1", /* RAVB_NC */
49};
50
51static const char *ravb_tx_irqs[NUM_TX_QUEUE] = {
52 "ch18", /* RAVB_BE */
53 "ch19", /* RAVB_NC */
54};
55
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030056void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear,
57 u32 set)
58{
59 ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg);
60}
61
Sergei Shtylyova0d2f202015-06-11 01:02:30 +030062int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value)
Sergei Shtylyovc1566332015-06-11 01:01:43 +030063{
64 int i;
65
66 for (i = 0; i < 10000; i++) {
67 if ((ravb_read(ndev, reg) & mask) == value)
68 return 0;
69 udelay(10);
70 }
71 return -ETIMEDOUT;
72}
73
74static int ravb_config(struct net_device *ndev)
75{
76 int error;
77
78 /* Set config mode */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030079 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030080 /* Check if the operating mode is changed to the config mode */
81 error = ravb_wait(ndev, CSR, CSR_OPS, CSR_OPS_CONFIG);
82 if (error)
83 netdev_err(ndev, "failed to switch device to config mode\n");
84
85 return error;
86}
87
88static void ravb_set_duplex(struct net_device *ndev)
89{
90 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030091
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030092 ravb_modify(ndev, ECMR, ECMR_DM, priv->duplex ? ECMR_DM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030093}
94
95static void ravb_set_rate(struct net_device *ndev)
96{
97 struct ravb_private *priv = netdev_priv(ndev);
98
99 switch (priv->speed) {
100 case 100: /* 100BASE */
101 ravb_write(ndev, GECMR_SPEED_100, GECMR);
102 break;
103 case 1000: /* 1000BASE */
104 ravb_write(ndev, GECMR_SPEED_1000, GECMR);
105 break;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300106 }
107}
108
109static void ravb_set_buffer_align(struct sk_buff *skb)
110{
111 u32 reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1);
112
113 if (reserve)
114 skb_reserve(skb, RAVB_ALIGN - reserve);
115}
116
117/* Get MAC address from the MAC address registers
118 *
119 * Ethernet AVB device doesn't have ROM for MAC address.
120 * This function gets the MAC address that was used by a bootloader.
121 */
122static void ravb_read_mac_address(struct net_device *ndev, const u8 *mac)
123{
124 if (mac) {
125 ether_addr_copy(ndev->dev_addr, mac);
126 } else {
Sergei Shtylyovd9660632015-12-05 00:58:07 +0300127 u32 mahr = ravb_read(ndev, MAHR);
128 u32 malr = ravb_read(ndev, MALR);
129
130 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
131 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
132 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
133 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
134 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
135 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300136 }
137}
138
139static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
140{
141 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
142 mdiobb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300143
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300144 ravb_modify(priv->ndev, PIR, mask, set ? mask : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300145}
146
147/* MDC pin control */
148static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level)
149{
150 ravb_mdio_ctrl(ctrl, PIR_MDC, level);
151}
152
153/* Data I/O pin control */
154static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
155{
156 ravb_mdio_ctrl(ctrl, PIR_MMD, output);
157}
158
159/* Set data bit */
160static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
161{
162 ravb_mdio_ctrl(ctrl, PIR_MDO, value);
163}
164
165/* Get data bit */
166static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl)
167{
168 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
169 mdiobb);
170
171 return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0;
172}
173
174/* MDIO bus control struct */
175static struct mdiobb_ops bb_ops = {
176 .owner = THIS_MODULE,
177 .set_mdc = ravb_set_mdc,
178 .set_mdio_dir = ravb_set_mdio_dir,
179 .set_mdio_data = ravb_set_mdio_data,
180 .get_mdio_data = ravb_get_mdio_data,
181};
182
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100183/* Free TX skb function for AVB-IP */
184static int ravb_tx_free(struct net_device *ndev, int q, bool free_txed_only)
185{
186 struct ravb_private *priv = netdev_priv(ndev);
187 struct net_device_stats *stats = &priv->stats[q];
188 struct ravb_tx_desc *desc;
189 int free_num = 0;
190 int entry;
191 u32 size;
192
193 for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
194 bool txed;
195
196 entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
197 NUM_TX_DESC);
198 desc = &priv->tx_ring[q][entry];
199 txed = desc->die_dt == DT_FEMPTY;
200 if (free_txed_only && !txed)
201 break;
202 /* Descriptor type must be checked before all other reads */
203 dma_rmb();
204 size = le16_to_cpu(desc->ds_tagl) & TX_DS;
205 /* Free the original skb. */
206 if (priv->tx_skb[q][entry / NUM_TX_DESC]) {
207 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
208 size, DMA_TO_DEVICE);
209 /* Last packet descriptor? */
210 if (entry % NUM_TX_DESC == NUM_TX_DESC - 1) {
211 entry /= NUM_TX_DESC;
212 dev_kfree_skb_any(priv->tx_skb[q][entry]);
213 priv->tx_skb[q][entry] = NULL;
214 if (txed)
215 stats->tx_packets++;
216 }
217 free_num++;
218 }
219 if (txed)
220 stats->tx_bytes += size;
221 desc->die_dt = DT_EEMPTY;
222 }
223 return free_num;
224}
225
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300226/* Free skb's and DMA buffers for Ethernet AVB */
227static void ravb_ring_free(struct net_device *ndev, int q)
228{
229 struct ravb_private *priv = netdev_priv(ndev);
230 int ring_size;
231 int i;
232
233 /* Free RX skb ringbuffer */
234 if (priv->rx_skb[q]) {
235 for (i = 0; i < priv->num_rx_ring[q]; i++)
236 dev_kfree_skb(priv->rx_skb[q][i]);
237 }
238 kfree(priv->rx_skb[q]);
239 priv->rx_skb[q] = NULL;
240
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300241 /* Free aligned TX buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300242 kfree(priv->tx_align[q]);
243 priv->tx_align[q] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300244
245 if (priv->rx_ring[q]) {
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100246 for (i = 0; i < priv->num_rx_ring[q]; i++) {
247 struct ravb_ex_rx_desc *desc = &priv->rx_ring[q][i];
248
249 if (!dma_mapping_error(ndev->dev.parent,
250 le32_to_cpu(desc->dptr)))
251 dma_unmap_single(ndev->dev.parent,
252 le32_to_cpu(desc->dptr),
253 PKT_BUF_SZ,
254 DMA_FROM_DEVICE);
255 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300256 ring_size = sizeof(struct ravb_ex_rx_desc) *
257 (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900258 dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300259 priv->rx_desc_dma[q]);
260 priv->rx_ring[q] = NULL;
261 }
262
263 if (priv->tx_ring[q]) {
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100264 ravb_tx_free(ndev, q, false);
265
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300266 ring_size = sizeof(struct ravb_tx_desc) *
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300267 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900268 dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300269 priv->tx_desc_dma[q]);
270 priv->tx_ring[q] = NULL;
271 }
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100272
273 /* Free TX skb ringbuffer.
274 * SKBs are freed by ravb_tx_free() call above.
275 */
276 kfree(priv->tx_skb[q]);
277 priv->tx_skb[q] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300278}
279
280/* Format skb and descriptor buffer for Ethernet AVB */
281static void ravb_ring_format(struct net_device *ndev, int q)
282{
283 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300284 struct ravb_ex_rx_desc *rx_desc;
285 struct ravb_tx_desc *tx_desc;
286 struct ravb_desc *desc;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300287 int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q];
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300288 int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] *
289 NUM_TX_DESC;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300290 dma_addr_t dma_addr;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300291 int i;
292
293 priv->cur_rx[q] = 0;
294 priv->cur_tx[q] = 0;
295 priv->dirty_rx[q] = 0;
296 priv->dirty_tx[q] = 0;
297
298 memset(priv->rx_ring[q], 0, rx_ring_size);
299 /* Build RX ring buffer */
300 for (i = 0; i < priv->num_rx_ring[q]; i++) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300301 /* RX descriptor */
302 rx_desc = &priv->rx_ring[q][i];
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900303 rx_desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900304 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900305 PKT_BUF_SZ,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300306 DMA_FROM_DEVICE);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300307 /* We just set the data size to 0 for a failed mapping which
308 * should prevent DMA from happening...
309 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900310 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300311 rx_desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300312 rx_desc->dptr = cpu_to_le32(dma_addr);
313 rx_desc->die_dt = DT_FEMPTY;
314 }
315 rx_desc = &priv->rx_ring[q][i];
316 rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
317 rx_desc->die_dt = DT_LINKFIX; /* type */
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300318
319 memset(priv->tx_ring[q], 0, tx_ring_size);
320 /* Build TX ring buffer */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300321 for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
322 i++, tx_desc++) {
323 tx_desc->die_dt = DT_EEMPTY;
324 tx_desc++;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300325 tx_desc->die_dt = DT_EEMPTY;
326 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300327 tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
328 tx_desc->die_dt = DT_LINKFIX; /* type */
329
330 /* RX descriptor base address for best effort */
331 desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
332 desc->die_dt = DT_LINKFIX; /* type */
333 desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
334
335 /* TX descriptor base address for best effort */
336 desc = &priv->desc_bat[q];
337 desc->die_dt = DT_LINKFIX; /* type */
338 desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
339}
340
341/* Init skb and descriptor buffer for Ethernet AVB */
342static int ravb_ring_init(struct net_device *ndev, int q)
343{
344 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300345 struct sk_buff *skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300346 int ring_size;
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300347 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300348
349 /* Allocate RX and TX skb rings */
350 priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
351 sizeof(*priv->rx_skb[q]), GFP_KERNEL);
352 priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
353 sizeof(*priv->tx_skb[q]), GFP_KERNEL);
354 if (!priv->rx_skb[q] || !priv->tx_skb[q])
355 goto error;
356
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300357 for (i = 0; i < priv->num_rx_ring[q]; i++) {
358 skb = netdev_alloc_skb(ndev, PKT_BUF_SZ + RAVB_ALIGN - 1);
359 if (!skb)
360 goto error;
361 ravb_set_buffer_align(skb);
362 priv->rx_skb[q][i] = skb;
363 }
364
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300365 /* Allocate rings for the aligned buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300366 priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
367 DPTR_ALIGN - 1, GFP_KERNEL);
368 if (!priv->tx_align[q])
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300369 goto error;
370
371 /* Allocate all RX descriptors. */
372 ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900373 priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300374 &priv->rx_desc_dma[q],
375 GFP_KERNEL);
376 if (!priv->rx_ring[q])
377 goto error;
378
379 priv->dirty_rx[q] = 0;
380
381 /* Allocate all TX descriptors. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300382 ring_size = sizeof(struct ravb_tx_desc) *
383 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900384 priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300385 &priv->tx_desc_dma[q],
386 GFP_KERNEL);
387 if (!priv->tx_ring[q])
388 goto error;
389
390 return 0;
391
392error:
393 ravb_ring_free(ndev, q);
394
395 return -ENOMEM;
396}
397
398/* E-MAC init function */
399static void ravb_emac_init(struct net_device *ndev)
400{
401 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300402
403 /* Receive frame limit set register */
404 ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
405
406 /* PAUSE prohibition */
Sergei Shtylyov1c1fa822016-01-11 00:27:38 +0300407 ravb_write(ndev, ECMR_ZPF | (priv->duplex ? ECMR_DM : 0) |
408 ECMR_TE | ECMR_RE, ECMR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300409
410 ravb_set_rate(ndev);
411
412 /* Set MAC address */
413 ravb_write(ndev,
414 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
415 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
416 ravb_write(ndev,
417 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
418
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300419 /* E-MAC status register clear */
420 ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
421
422 /* E-MAC interrupt enable register */
423 ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
424}
425
426/* Device init function for Ethernet AVB */
427static int ravb_dmac_init(struct net_device *ndev)
428{
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900429 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300430 int error;
431
432 /* Set CONFIG mode */
433 error = ravb_config(ndev);
434 if (error)
435 return error;
436
437 error = ravb_ring_init(ndev, RAVB_BE);
438 if (error)
439 return error;
440 error = ravb_ring_init(ndev, RAVB_NC);
441 if (error) {
442 ravb_ring_free(ndev, RAVB_BE);
443 return error;
444 }
445
446 /* Descriptor format */
447 ravb_ring_format(ndev, RAVB_BE);
448 ravb_ring_format(ndev, RAVB_NC);
449
450#if defined(__LITTLE_ENDIAN)
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300451 ravb_modify(ndev, CCC, CCC_BOC, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300452#else
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300453 ravb_modify(ndev, CCC, CCC_BOC, CCC_BOC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300454#endif
455
456 /* Set AVB RX */
Masaru Nagai8d9c4182016-06-01 03:01:28 +0900457 ravb_write(ndev,
458 RCR_EFFS | RCR_ENCF | RCR_ETS0 | RCR_ESF | 0x18000000, RCR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300459
460 /* Set FIFO size */
461 ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00222200, TGC);
462
463 /* Timestamp enable */
464 ravb_write(ndev, TCCR_TFEN, TCCR);
465
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900466 /* Interrupt init: */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900467 if (priv->chip_id == RCAR_GEN3) {
468 /* Clear DIL.DPLx */
469 ravb_write(ndev, 0, DIL);
470 /* Set queue specific interrupt */
471 ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE);
472 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300473 /* Frame receive */
474 ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900475 /* Disable FIFO full warning */
476 ravb_write(ndev, 0, RIC1);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300477 /* Receive FIFO full error, descriptor empty */
478 ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
479 /* Frame transmitted, timestamp FIFO updated */
480 ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
481
482 /* Setting the control will start the AVB-DMAC process. */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300483 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300484
485 return 0;
486}
487
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300488static void ravb_get_tx_tstamp(struct net_device *ndev)
489{
490 struct ravb_private *priv = netdev_priv(ndev);
491 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
492 struct skb_shared_hwtstamps shhwtstamps;
493 struct sk_buff *skb;
494 struct timespec64 ts;
495 u16 tag, tfa_tag;
496 int count;
497 u32 tfa2;
498
499 count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
500 while (count--) {
501 tfa2 = ravb_read(ndev, TFA2);
502 tfa_tag = (tfa2 & TFA2_TST) >> 16;
503 ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
504 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
505 ravb_read(ndev, TFA1);
506 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
507 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
508 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
509 list) {
510 skb = ts_skb->skb;
511 tag = ts_skb->tag;
512 list_del(&ts_skb->list);
513 kfree(ts_skb);
514 if (tag == tfa_tag) {
515 skb_tstamp_tx(skb, &shhwtstamps);
516 break;
517 }
518 }
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300519 ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300520 }
521}
522
523/* Packet receive function for Ethernet AVB */
524static bool ravb_rx(struct net_device *ndev, int *quota, int q)
525{
526 struct ravb_private *priv = netdev_priv(ndev);
527 int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
528 int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
529 priv->cur_rx[q];
530 struct net_device_stats *stats = &priv->stats[q];
531 struct ravb_ex_rx_desc *desc;
532 struct sk_buff *skb;
533 dma_addr_t dma_addr;
534 struct timespec64 ts;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300535 u8 desc_status;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300536 u16 pkt_len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300537 int limit;
538
539 boguscnt = min(boguscnt, *quota);
540 limit = boguscnt;
541 desc = &priv->rx_ring[q][entry];
542 while (desc->die_dt != DT_FEMPTY) {
543 /* Descriptor type must be checked before all other reads */
544 dma_rmb();
545 desc_status = desc->msc;
546 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
547
548 if (--boguscnt < 0)
549 break;
550
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300551 /* We use 0-byte descriptors to mark the DMA mapping errors */
552 if (!pkt_len)
553 continue;
554
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300555 if (desc_status & MSC_MC)
556 stats->multicast++;
557
558 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
559 MSC_CEEF)) {
560 stats->rx_errors++;
561 if (desc_status & MSC_CRC)
562 stats->rx_crc_errors++;
563 if (desc_status & MSC_RFE)
564 stats->rx_frame_errors++;
565 if (desc_status & (MSC_RTLF | MSC_RTSF))
566 stats->rx_length_errors++;
567 if (desc_status & MSC_CEEF)
568 stats->rx_missed_errors++;
569 } else {
570 u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
571
572 skb = priv->rx_skb[q][entry];
573 priv->rx_skb[q][entry] = NULL;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900574 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900575 PKT_BUF_SZ,
Sergei Shtylyove2370f02015-07-15 00:56:52 +0300576 DMA_FROM_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300577 get_ts &= (q == RAVB_NC) ?
578 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
579 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
580 if (get_ts) {
581 struct skb_shared_hwtstamps *shhwtstamps;
582
583 shhwtstamps = skb_hwtstamps(skb);
584 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
585 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
586 32) | le32_to_cpu(desc->ts_sl);
587 ts.tv_nsec = le32_to_cpu(desc->ts_n);
588 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
589 }
590 skb_put(skb, pkt_len);
591 skb->protocol = eth_type_trans(skb, ndev);
592 napi_gro_receive(&priv->napi[q], skb);
593 stats->rx_packets++;
594 stats->rx_bytes += pkt_len;
595 }
596
597 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
598 desc = &priv->rx_ring[q][entry];
599 }
600
601 /* Refill the RX ring buffers. */
602 for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
603 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
604 desc = &priv->rx_ring[q][entry];
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900605 desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300606
607 if (!priv->rx_skb[q][entry]) {
608 skb = netdev_alloc_skb(ndev,
609 PKT_BUF_SZ + RAVB_ALIGN - 1);
610 if (!skb)
611 break; /* Better luck next round. */
612 ravb_set_buffer_align(skb);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900613 dma_addr = dma_map_single(ndev->dev.parent, skb->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300614 le16_to_cpu(desc->ds_cc),
615 DMA_FROM_DEVICE);
616 skb_checksum_none_assert(skb);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300617 /* We just set the data size to 0 for a failed mapping
618 * which should prevent DMA from happening...
619 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900620 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300621 desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300622 desc->dptr = cpu_to_le32(dma_addr);
623 priv->rx_skb[q][entry] = skb;
624 }
625 /* Descriptor type must be set after all the above writes */
626 dma_wmb();
627 desc->die_dt = DT_FEMPTY;
628 }
629
630 *quota -= limit - (++boguscnt);
631
632 return boguscnt <= 0;
633}
634
635static void ravb_rcv_snd_disable(struct net_device *ndev)
636{
637 /* Disable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300638 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300639}
640
641static void ravb_rcv_snd_enable(struct net_device *ndev)
642{
643 /* Enable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300644 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300645}
646
647/* function for waiting dma process finished */
648static int ravb_stop_dma(struct net_device *ndev)
649{
650 int error;
651
652 /* Wait for stopping the hardware TX process */
653 error = ravb_wait(ndev, TCCR,
654 TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
655 if (error)
656 return error;
657
658 error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
659 0);
660 if (error)
661 return error;
662
663 /* Stop the E-MAC's RX/TX processes. */
664 ravb_rcv_snd_disable(ndev);
665
666 /* Wait for stopping the RX DMA process */
667 error = ravb_wait(ndev, CSR, CSR_RPO, 0);
668 if (error)
669 return error;
670
671 /* Stop AVB-DMAC process */
672 return ravb_config(ndev);
673}
674
675/* E-MAC interrupt handler */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900676static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300677{
678 struct ravb_private *priv = netdev_priv(ndev);
679 u32 ecsr, psr;
680
681 ecsr = ravb_read(ndev, ECSR);
682 ravb_write(ndev, ecsr, ECSR); /* clear interrupt */
683 if (ecsr & ECSR_ICD)
684 ndev->stats.tx_carrier_errors++;
685 if (ecsr & ECSR_LCHNG) {
686 /* Link changed */
687 if (priv->no_avb_link)
688 return;
689 psr = ravb_read(ndev, PSR);
690 if (priv->avb_link_active_low)
691 psr ^= PSR_LMON;
692 if (!(psr & PSR_LMON)) {
693 /* DIsable RX and TX */
694 ravb_rcv_snd_disable(ndev);
695 } else {
696 /* Enable RX and TX */
697 ravb_rcv_snd_enable(ndev);
698 }
699 }
700}
701
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900702static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
703{
704 struct net_device *ndev = dev_id;
705 struct ravb_private *priv = netdev_priv(ndev);
706
707 spin_lock(&priv->lock);
708 ravb_emac_interrupt_unlocked(ndev);
709 mmiowb();
710 spin_unlock(&priv->lock);
711 return IRQ_HANDLED;
712}
713
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300714/* Error interrupt handler */
715static void ravb_error_interrupt(struct net_device *ndev)
716{
717 struct ravb_private *priv = netdev_priv(ndev);
718 u32 eis, ris2;
719
720 eis = ravb_read(ndev, EIS);
721 ravb_write(ndev, ~EIS_QFS, EIS);
722 if (eis & EIS_QFS) {
723 ris2 = ravb_read(ndev, RIS2);
724 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF), RIS2);
725
726 /* Receive Descriptor Empty int */
727 if (ris2 & RIS2_QFF0)
728 priv->stats[RAVB_BE].rx_over_errors++;
729
730 /* Receive Descriptor Empty int */
731 if (ris2 & RIS2_QFF1)
732 priv->stats[RAVB_NC].rx_over_errors++;
733
734 /* Receive FIFO Overflow int */
735 if (ris2 & RIS2_RFFF)
736 priv->rx_fifo_errors++;
737 }
738}
739
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900740static bool ravb_queue_interrupt(struct net_device *ndev, int q)
741{
742 struct ravb_private *priv = netdev_priv(ndev);
743 u32 ris0 = ravb_read(ndev, RIS0);
744 u32 ric0 = ravb_read(ndev, RIC0);
745 u32 tis = ravb_read(ndev, TIS);
746 u32 tic = ravb_read(ndev, TIC);
747
748 if (((ris0 & ric0) & BIT(q)) || ((tis & tic) & BIT(q))) {
749 if (napi_schedule_prep(&priv->napi[q])) {
750 /* Mask RX and TX interrupts */
751 if (priv->chip_id == RCAR_GEN2) {
752 ravb_write(ndev, ric0 & ~BIT(q), RIC0);
753 ravb_write(ndev, tic & ~BIT(q), TIC);
754 } else {
755 ravb_write(ndev, BIT(q), RID0);
756 ravb_write(ndev, BIT(q), TID);
757 }
758 __napi_schedule(&priv->napi[q]);
759 } else {
760 netdev_warn(ndev,
761 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
762 ris0, ric0);
763 netdev_warn(ndev,
764 " tx status 0x%08x, tx mask 0x%08x.\n",
765 tis, tic);
766 }
767 return true;
768 }
769 return false;
770}
771
772static bool ravb_timestamp_interrupt(struct net_device *ndev)
773{
774 u32 tis = ravb_read(ndev, TIS);
775
776 if (tis & TIS_TFUF) {
777 ravb_write(ndev, ~TIS_TFUF, TIS);
778 ravb_get_tx_tstamp(ndev);
779 return true;
780 }
781 return false;
782}
783
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300784static irqreturn_t ravb_interrupt(int irq, void *dev_id)
785{
786 struct net_device *ndev = dev_id;
787 struct ravb_private *priv = netdev_priv(ndev);
788 irqreturn_t result = IRQ_NONE;
789 u32 iss;
790
791 spin_lock(&priv->lock);
792 /* Get interrupt status */
793 iss = ravb_read(ndev, ISS);
794
795 /* Received and transmitted interrupts */
796 if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300797 int q;
798
799 /* Timestamp updated */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900800 if (ravb_timestamp_interrupt(ndev))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300801 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300802
803 /* Network control and best effort queue RX/TX */
804 for (q = RAVB_NC; q >= RAVB_BE; q--) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900805 if (ravb_queue_interrupt(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300806 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300807 }
808 }
809
810 /* E-MAC status summary */
811 if (iss & ISS_MS) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900812 ravb_emac_interrupt_unlocked(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300813 result = IRQ_HANDLED;
814 }
815
816 /* Error status summary */
817 if (iss & ISS_ES) {
818 ravb_error_interrupt(ndev);
819 result = IRQ_HANDLED;
820 }
821
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900822 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300823 if (iss & ISS_CGIS) {
824 ravb_ptp_interrupt(ndev);
Yoshihiro Kaneko38c848c2016-03-16 00:52:16 +0900825 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300826 }
Sergei Shtylyova0d2f202015-06-11 01:02:30 +0300827
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300828 mmiowb();
829 spin_unlock(&priv->lock);
830 return result;
831}
832
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900833/* Timestamp/Error/gPTP interrupt handler */
834static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
835{
836 struct net_device *ndev = dev_id;
837 struct ravb_private *priv = netdev_priv(ndev);
838 irqreturn_t result = IRQ_NONE;
839 u32 iss;
840
841 spin_lock(&priv->lock);
842 /* Get interrupt status */
843 iss = ravb_read(ndev, ISS);
844
845 /* Timestamp updated */
846 if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
847 result = IRQ_HANDLED;
848
849 /* Error status summary */
850 if (iss & ISS_ES) {
851 ravb_error_interrupt(ndev);
852 result = IRQ_HANDLED;
853 }
854
855 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300856 if (iss & ISS_CGIS) {
857 ravb_ptp_interrupt(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900858 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300859 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900860
861 mmiowb();
862 spin_unlock(&priv->lock);
863 return result;
864}
865
866static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
867{
868 struct net_device *ndev = dev_id;
869 struct ravb_private *priv = netdev_priv(ndev);
870 irqreturn_t result = IRQ_NONE;
871
872 spin_lock(&priv->lock);
873
874 /* Network control/Best effort queue RX/TX */
875 if (ravb_queue_interrupt(ndev, q))
876 result = IRQ_HANDLED;
877
878 mmiowb();
879 spin_unlock(&priv->lock);
880 return result;
881}
882
883static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
884{
885 return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
886}
887
888static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
889{
890 return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
891}
892
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300893static int ravb_poll(struct napi_struct *napi, int budget)
894{
895 struct net_device *ndev = napi->dev;
896 struct ravb_private *priv = netdev_priv(ndev);
897 unsigned long flags;
898 int q = napi - priv->napi;
899 int mask = BIT(q);
900 int quota = budget;
901 u32 ris0, tis;
902
903 for (;;) {
904 tis = ravb_read(ndev, TIS);
905 ris0 = ravb_read(ndev, RIS0);
906 if (!((ris0 & mask) || (tis & mask)))
907 break;
908
909 /* Processing RX Descriptor Ring */
910 if (ris0 & mask) {
911 /* Clear RX interrupt */
912 ravb_write(ndev, ~mask, RIS0);
913 if (ravb_rx(ndev, &quota, q))
914 goto out;
915 }
916 /* Processing TX Descriptor Ring */
917 if (tis & mask) {
918 spin_lock_irqsave(&priv->lock, flags);
919 /* Clear TX interrupt */
920 ravb_write(ndev, ~mask, TIS);
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +0100921 ravb_tx_free(ndev, q, true);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300922 netif_wake_subqueue(ndev, q);
923 mmiowb();
924 spin_unlock_irqrestore(&priv->lock, flags);
925 }
926 }
927
928 napi_complete(napi);
929
930 /* Re-enable RX/TX interrupts */
931 spin_lock_irqsave(&priv->lock, flags);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900932 if (priv->chip_id == RCAR_GEN2) {
933 ravb_modify(ndev, RIC0, mask, mask);
934 ravb_modify(ndev, TIC, mask, mask);
935 } else {
936 ravb_write(ndev, mask, RIE0);
937 ravb_write(ndev, mask, TIE);
938 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300939 mmiowb();
940 spin_unlock_irqrestore(&priv->lock, flags);
941
942 /* Receive error message handling */
943 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
944 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
Kazuya Mizuguchi18a3ed52017-01-12 13:21:06 +0100945 if (priv->rx_over_errors != ndev->stats.rx_over_errors)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300946 ndev->stats.rx_over_errors = priv->rx_over_errors;
Kazuya Mizuguchi18a3ed52017-01-12 13:21:06 +0100947 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300948 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300949out:
950 return budget - quota;
951}
952
953/* PHY state control function */
954static void ravb_adjust_link(struct net_device *ndev)
955{
956 struct ravb_private *priv = netdev_priv(ndev);
Philippe Reynes0f635172016-08-20 00:52:18 +0200957 struct phy_device *phydev = ndev->phydev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300958 bool new_state = false;
959
960 if (phydev->link) {
961 if (phydev->duplex != priv->duplex) {
962 new_state = true;
963 priv->duplex = phydev->duplex;
964 ravb_set_duplex(ndev);
965 }
966
967 if (phydev->speed != priv->speed) {
968 new_state = true;
969 priv->speed = phydev->speed;
970 ravb_set_rate(ndev);
971 }
972 if (!priv->link) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300973 ravb_modify(ndev, ECMR, ECMR_TXF, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300974 new_state = true;
975 priv->link = phydev->link;
976 if (priv->no_avb_link)
977 ravb_rcv_snd_enable(ndev);
978 }
979 } else if (priv->link) {
980 new_state = true;
981 priv->link = 0;
982 priv->speed = 0;
983 priv->duplex = -1;
984 if (priv->no_avb_link)
985 ravb_rcv_snd_disable(ndev);
986 }
987
988 if (new_state && netif_msg_link(priv))
989 phy_print_status(phydev);
990}
991
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +0100992static const struct soc_device_attribute r8a7795es10[] = {
993 { .soc_id = "r8a7795", .revision = "ES1.0", },
994 { /* sentinel */ }
995};
996
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300997/* PHY init function */
998static int ravb_phy_init(struct net_device *ndev)
999{
1000 struct device_node *np = ndev->dev.parent->of_node;
1001 struct ravb_private *priv = netdev_priv(ndev);
1002 struct phy_device *phydev;
1003 struct device_node *pn;
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +09001004 int err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001005
1006 priv->link = 0;
1007 priv->speed = 0;
1008 priv->duplex = -1;
1009
1010 /* Try connecting to PHY */
1011 pn = of_parse_phandle(np, "phy-handle", 0);
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +09001012 if (!pn) {
1013 /* In the case of a fixed PHY, the DT node associated
1014 * to the PHY is the Ethernet MAC DT node.
1015 */
1016 if (of_phy_is_fixed_link(np)) {
1017 err = of_phy_register_fixed_link(np);
1018 if (err)
1019 return err;
1020 }
1021 pn = of_node_get(np);
1022 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001023 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
1024 priv->phy_interface);
Peter Chenc9b1eb82016-08-01 15:02:39 +08001025 of_node_put(pn);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001026 if (!phydev) {
1027 netdev_err(ndev, "failed to connect PHY\n");
Johan Hovold9f70eb32016-11-28 19:25:06 +01001028 err = -ENOENT;
1029 goto err_deregister_fixed_link;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001030 }
1031
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +01001032 /* This driver only support 10/100Mbit speeds on R-Car H3 ES1.0
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001033 * at this time.
1034 */
Geert Uytterhoeven0e98f9d2017-01-27 20:46:27 +01001035 if (soc_device_match(r8a7795es10)) {
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001036 err = phy_set_max_speed(phydev, SPEED_100);
1037 if (err) {
1038 netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
Johan Hovold9f70eb32016-11-28 19:25:06 +01001039 goto err_phy_disconnect;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001040 }
1041
1042 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
1043 }
1044
Kazuya Mizuguchi54499962015-12-14 00:15:58 +09001045 /* 10BASE is not supported */
1046 phydev->supported &= ~PHY_10BT_FEATURES;
1047
Andrew Lunn22209432016-01-06 20:11:13 +01001048 phy_attached_info(phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001049
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001050 return 0;
Johan Hovold9f70eb32016-11-28 19:25:06 +01001051
1052err_phy_disconnect:
1053 phy_disconnect(phydev);
1054err_deregister_fixed_link:
1055 if (of_phy_is_fixed_link(np))
1056 of_phy_deregister_fixed_link(np);
1057
1058 return err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001059}
1060
1061/* PHY control start function */
1062static int ravb_phy_start(struct net_device *ndev)
1063{
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001064 int error;
1065
1066 error = ravb_phy_init(ndev);
1067 if (error)
1068 return error;
1069
Philippe Reynes0f635172016-08-20 00:52:18 +02001070 phy_start(ndev->phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001071
1072 return 0;
1073}
1074
Philippe Reynes04462f22016-08-20 00:52:19 +02001075static int ravb_get_link_ksettings(struct net_device *ndev,
1076 struct ethtool_link_ksettings *cmd)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001077{
1078 struct ravb_private *priv = netdev_priv(ndev);
1079 int error = -ENODEV;
1080 unsigned long flags;
1081
Philippe Reynes0f635172016-08-20 00:52:18 +02001082 if (ndev->phydev) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001083 spin_lock_irqsave(&priv->lock, flags);
Philippe Reynes04462f22016-08-20 00:52:19 +02001084 error = phy_ethtool_ksettings_get(ndev->phydev, cmd);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001085 spin_unlock_irqrestore(&priv->lock, flags);
1086 }
1087
1088 return error;
1089}
1090
Philippe Reynes04462f22016-08-20 00:52:19 +02001091static int ravb_set_link_ksettings(struct net_device *ndev,
1092 const struct ethtool_link_ksettings *cmd)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001093{
1094 struct ravb_private *priv = netdev_priv(ndev);
1095 unsigned long flags;
1096 int error;
1097
Philippe Reynes0f635172016-08-20 00:52:18 +02001098 if (!ndev->phydev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001099 return -ENODEV;
1100
1101 spin_lock_irqsave(&priv->lock, flags);
1102
1103 /* Disable TX and RX */
1104 ravb_rcv_snd_disable(ndev);
1105
Philippe Reynes04462f22016-08-20 00:52:19 +02001106 error = phy_ethtool_ksettings_set(ndev->phydev, cmd);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001107 if (error)
1108 goto error_exit;
1109
Philippe Reynes04462f22016-08-20 00:52:19 +02001110 if (cmd->base.duplex == DUPLEX_FULL)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001111 priv->duplex = 1;
1112 else
1113 priv->duplex = 0;
1114
1115 ravb_set_duplex(ndev);
1116
1117error_exit:
1118 mdelay(1);
1119
1120 /* Enable TX and RX */
1121 ravb_rcv_snd_enable(ndev);
1122
1123 mmiowb();
1124 spin_unlock_irqrestore(&priv->lock, flags);
1125
1126 return error;
1127}
1128
1129static int ravb_nway_reset(struct net_device *ndev)
1130{
1131 struct ravb_private *priv = netdev_priv(ndev);
1132 int error = -ENODEV;
1133 unsigned long flags;
1134
Philippe Reynes0f635172016-08-20 00:52:18 +02001135 if (ndev->phydev) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001136 spin_lock_irqsave(&priv->lock, flags);
Philippe Reynes0f635172016-08-20 00:52:18 +02001137 error = phy_start_aneg(ndev->phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001138 spin_unlock_irqrestore(&priv->lock, flags);
1139 }
1140
1141 return error;
1142}
1143
1144static u32 ravb_get_msglevel(struct net_device *ndev)
1145{
1146 struct ravb_private *priv = netdev_priv(ndev);
1147
1148 return priv->msg_enable;
1149}
1150
1151static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1152{
1153 struct ravb_private *priv = netdev_priv(ndev);
1154
1155 priv->msg_enable = value;
1156}
1157
1158static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1159 "rx_queue_0_current",
1160 "tx_queue_0_current",
1161 "rx_queue_0_dirty",
1162 "tx_queue_0_dirty",
1163 "rx_queue_0_packets",
1164 "tx_queue_0_packets",
1165 "rx_queue_0_bytes",
1166 "tx_queue_0_bytes",
1167 "rx_queue_0_mcast_packets",
1168 "rx_queue_0_errors",
1169 "rx_queue_0_crc_errors",
1170 "rx_queue_0_frame_errors",
1171 "rx_queue_0_length_errors",
1172 "rx_queue_0_missed_errors",
1173 "rx_queue_0_over_errors",
1174
1175 "rx_queue_1_current",
1176 "tx_queue_1_current",
1177 "rx_queue_1_dirty",
1178 "tx_queue_1_dirty",
1179 "rx_queue_1_packets",
1180 "tx_queue_1_packets",
1181 "rx_queue_1_bytes",
1182 "tx_queue_1_bytes",
1183 "rx_queue_1_mcast_packets",
1184 "rx_queue_1_errors",
1185 "rx_queue_1_crc_errors",
Sergei Shtylyovb17c1d92015-12-04 01:51:10 +03001186 "rx_queue_1_frame_errors",
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001187 "rx_queue_1_length_errors",
1188 "rx_queue_1_missed_errors",
1189 "rx_queue_1_over_errors",
1190};
1191
1192#define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1193
1194static int ravb_get_sset_count(struct net_device *netdev, int sset)
1195{
1196 switch (sset) {
1197 case ETH_SS_STATS:
1198 return RAVB_STATS_LEN;
1199 default:
1200 return -EOPNOTSUPP;
1201 }
1202}
1203
1204static void ravb_get_ethtool_stats(struct net_device *ndev,
1205 struct ethtool_stats *stats, u64 *data)
1206{
1207 struct ravb_private *priv = netdev_priv(ndev);
1208 int i = 0;
1209 int q;
1210
1211 /* Device-specific stats */
1212 for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1213 struct net_device_stats *stats = &priv->stats[q];
1214
1215 data[i++] = priv->cur_rx[q];
1216 data[i++] = priv->cur_tx[q];
1217 data[i++] = priv->dirty_rx[q];
1218 data[i++] = priv->dirty_tx[q];
1219 data[i++] = stats->rx_packets;
1220 data[i++] = stats->tx_packets;
1221 data[i++] = stats->rx_bytes;
1222 data[i++] = stats->tx_bytes;
1223 data[i++] = stats->multicast;
1224 data[i++] = stats->rx_errors;
1225 data[i++] = stats->rx_crc_errors;
1226 data[i++] = stats->rx_frame_errors;
1227 data[i++] = stats->rx_length_errors;
1228 data[i++] = stats->rx_missed_errors;
1229 data[i++] = stats->rx_over_errors;
1230 }
1231}
1232
1233static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1234{
1235 switch (stringset) {
1236 case ETH_SS_STATS:
1237 memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
1238 break;
1239 }
1240}
1241
1242static void ravb_get_ringparam(struct net_device *ndev,
1243 struct ethtool_ringparam *ring)
1244{
1245 struct ravb_private *priv = netdev_priv(ndev);
1246
1247 ring->rx_max_pending = BE_RX_RING_MAX;
1248 ring->tx_max_pending = BE_TX_RING_MAX;
1249 ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1250 ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1251}
1252
1253static int ravb_set_ringparam(struct net_device *ndev,
1254 struct ethtool_ringparam *ring)
1255{
1256 struct ravb_private *priv = netdev_priv(ndev);
1257 int error;
1258
1259 if (ring->tx_pending > BE_TX_RING_MAX ||
1260 ring->rx_pending > BE_RX_RING_MAX ||
1261 ring->tx_pending < BE_TX_RING_MIN ||
1262 ring->rx_pending < BE_RX_RING_MIN)
1263 return -EINVAL;
1264 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1265 return -EINVAL;
1266
1267 if (netif_running(ndev)) {
1268 netif_device_detach(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001269 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001270 if (priv->chip_id == RCAR_GEN2)
1271 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001272 /* Wait for DMA stopping */
1273 error = ravb_stop_dma(ndev);
1274 if (error) {
1275 netdev_err(ndev,
1276 "cannot set ringparam! Any AVB processes are still running?\n");
1277 return error;
1278 }
1279 synchronize_irq(ndev->irq);
1280
1281 /* Free all the skb's in the RX queue and the DMA buffers. */
1282 ravb_ring_free(ndev, RAVB_BE);
1283 ravb_ring_free(ndev, RAVB_NC);
1284 }
1285
1286 /* Set new parameters */
1287 priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1288 priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1289
1290 if (netif_running(ndev)) {
1291 error = ravb_dmac_init(ndev);
1292 if (error) {
1293 netdev_err(ndev,
1294 "%s: ravb_dmac_init() failed, error %d\n",
1295 __func__, error);
1296 return error;
1297 }
1298
1299 ravb_emac_init(ndev);
1300
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001301 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001302 if (priv->chip_id == RCAR_GEN2)
1303 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001304
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001305 netif_device_attach(ndev);
1306 }
1307
1308 return 0;
1309}
1310
1311static int ravb_get_ts_info(struct net_device *ndev,
1312 struct ethtool_ts_info *info)
1313{
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001314 struct ravb_private *priv = netdev_priv(ndev);
1315
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001316 info->so_timestamping =
1317 SOF_TIMESTAMPING_TX_SOFTWARE |
1318 SOF_TIMESTAMPING_RX_SOFTWARE |
1319 SOF_TIMESTAMPING_SOFTWARE |
1320 SOF_TIMESTAMPING_TX_HARDWARE |
1321 SOF_TIMESTAMPING_RX_HARDWARE |
1322 SOF_TIMESTAMPING_RAW_HARDWARE;
1323 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1324 info->rx_filters =
1325 (1 << HWTSTAMP_FILTER_NONE) |
1326 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1327 (1 << HWTSTAMP_FILTER_ALL);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001328 info->phc_index = ptp_clock_index(priv->ptp.clock);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001329
1330 return 0;
1331}
1332
1333static const struct ethtool_ops ravb_ethtool_ops = {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001334 .nway_reset = ravb_nway_reset,
1335 .get_msglevel = ravb_get_msglevel,
1336 .set_msglevel = ravb_set_msglevel,
1337 .get_link = ethtool_op_get_link,
1338 .get_strings = ravb_get_strings,
1339 .get_ethtool_stats = ravb_get_ethtool_stats,
1340 .get_sset_count = ravb_get_sset_count,
1341 .get_ringparam = ravb_get_ringparam,
1342 .set_ringparam = ravb_set_ringparam,
1343 .get_ts_info = ravb_get_ts_info,
Philippe Reynes04462f22016-08-20 00:52:19 +02001344 .get_link_ksettings = ravb_get_link_ksettings,
1345 .set_link_ksettings = ravb_set_link_ksettings,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001346};
1347
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001348static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1349 struct net_device *ndev, struct device *dev,
1350 const char *ch)
1351{
1352 char *name;
1353 int error;
1354
1355 name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1356 if (!name)
1357 return -ENOMEM;
1358 error = request_irq(irq, handler, 0, name, ndev);
1359 if (error)
1360 netdev_err(ndev, "cannot request IRQ %s\n", name);
1361
1362 return error;
1363}
1364
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001365/* Network device open function for Ethernet AVB */
1366static int ravb_open(struct net_device *ndev)
1367{
1368 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001369 struct platform_device *pdev = priv->pdev;
1370 struct device *dev = &pdev->dev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001371 int error;
1372
1373 napi_enable(&priv->napi[RAVB_BE]);
1374 napi_enable(&priv->napi[RAVB_NC]);
1375
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001376 if (priv->chip_id == RCAR_GEN2) {
1377 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1378 ndev->name, ndev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001379 if (error) {
1380 netdev_err(ndev, "cannot request IRQ\n");
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001381 goto out_napi_off;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001382 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001383 } else {
1384 error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1385 dev, "ch22:multi");
1386 if (error)
1387 goto out_napi_off;
1388 error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1389 dev, "ch24:emac");
1390 if (error)
1391 goto out_free_irq;
1392 error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1393 ndev, dev, "ch0:rx_be");
1394 if (error)
1395 goto out_free_irq_emac;
1396 error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1397 ndev, dev, "ch18:tx_be");
1398 if (error)
1399 goto out_free_irq_be_rx;
1400 error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1401 ndev, dev, "ch1:rx_nc");
1402 if (error)
1403 goto out_free_irq_be_tx;
1404 error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1405 ndev, dev, "ch19:tx_nc");
1406 if (error)
1407 goto out_free_irq_nc_rx;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001408 }
1409
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001410 /* Device init */
1411 error = ravb_dmac_init(ndev);
1412 if (error)
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001413 goto out_free_irq_nc_tx;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001414 ravb_emac_init(ndev);
1415
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001416 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001417 if (priv->chip_id == RCAR_GEN2)
1418 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001419
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001420 netif_tx_start_all_queues(ndev);
1421
1422 /* PHY control start */
1423 error = ravb_phy_start(ndev);
1424 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001425 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001426
1427 return 0;
1428
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001429out_ptp_stop:
1430 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001431 if (priv->chip_id == RCAR_GEN2)
1432 ravb_ptp_stop(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001433out_free_irq_nc_tx:
1434 if (priv->chip_id == RCAR_GEN2)
1435 goto out_free_irq;
1436 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1437out_free_irq_nc_rx:
1438 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1439out_free_irq_be_tx:
1440 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1441out_free_irq_be_rx:
1442 free_irq(priv->rx_irqs[RAVB_BE], ndev);
1443out_free_irq_emac:
1444 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001445out_free_irq:
1446 free_irq(ndev->irq, ndev);
1447out_napi_off:
1448 napi_disable(&priv->napi[RAVB_NC]);
1449 napi_disable(&priv->napi[RAVB_BE]);
1450 return error;
1451}
1452
1453/* Timeout function for Ethernet AVB */
1454static void ravb_tx_timeout(struct net_device *ndev)
1455{
1456 struct ravb_private *priv = netdev_priv(ndev);
1457
1458 netif_err(priv, tx_err, ndev,
1459 "transmit timed out, status %08x, resetting...\n",
1460 ravb_read(ndev, ISS));
1461
1462 /* tx_errors count up */
1463 ndev->stats.tx_errors++;
1464
1465 schedule_work(&priv->work);
1466}
1467
1468static void ravb_tx_timeout_work(struct work_struct *work)
1469{
1470 struct ravb_private *priv = container_of(work, struct ravb_private,
1471 work);
1472 struct net_device *ndev = priv->ndev;
1473
1474 netif_tx_stop_all_queues(ndev);
1475
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001476 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001477 if (priv->chip_id == RCAR_GEN2)
1478 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001479
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001480 /* Wait for DMA stopping */
1481 ravb_stop_dma(ndev);
1482
1483 ravb_ring_free(ndev, RAVB_BE);
1484 ravb_ring_free(ndev, RAVB_NC);
1485
1486 /* Device init */
1487 ravb_dmac_init(ndev);
1488 ravb_emac_init(ndev);
1489
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001490 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001491 if (priv->chip_id == RCAR_GEN2)
1492 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001493
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001494 netif_tx_start_all_queues(ndev);
1495}
1496
1497/* Packet transmit function for Ethernet AVB */
1498static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1499{
1500 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001501 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001502 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001503 struct ravb_tx_desc *desc;
1504 unsigned long flags;
1505 u32 dma_addr;
1506 void *buffer;
1507 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001508 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001509
1510 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001511 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1512 NUM_TX_DESC) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001513 netif_err(priv, tx_queued, ndev,
1514 "still transmitting with the full ring!\n");
1515 netif_stop_subqueue(ndev, q);
1516 spin_unlock_irqrestore(&priv->lock, flags);
1517 return NETDEV_TX_BUSY;
1518 }
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001519 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1520 priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001521
1522 if (skb_put_padto(skb, ETH_ZLEN))
1523 goto drop;
1524
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001525 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1526 entry / NUM_TX_DESC * DPTR_ALIGN;
1527 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
Masaru Nagai8ec3e8a2017-01-16 11:45:21 +01001528 /* Zero length DMA descriptors are problematic as they seem to
1529 * terminate DMA transfers. Avoid them by simply using a length of
1530 * DPTR_ALIGN (4) when skb data is aligned to DPTR_ALIGN.
1531 *
1532 * As skb is guaranteed to have at least ETH_ZLEN (60) bytes of
1533 * data by the call to skb_put_padto() above this is safe with
1534 * respect to both the length of the first DMA descriptor (len)
1535 * overflowing the available data and the length of the second DMA
1536 * descriptor (skb->len - len) being negative.
1537 */
1538 if (len == 0)
1539 len = DPTR_ALIGN;
1540
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001541 memcpy(buffer, skb->data, len);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001542 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1543 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001544 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001545
1546 desc = &priv->tx_ring[q][entry];
1547 desc->ds_tagl = cpu_to_le16(len);
1548 desc->dptr = cpu_to_le32(dma_addr);
1549
1550 buffer = skb->data + len;
1551 len = skb->len - len;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001552 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1553 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001554 goto unmap;
1555
1556 desc++;
1557 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001558 desc->dptr = cpu_to_le32(dma_addr);
1559
1560 /* TX timestamp required */
1561 if (q == RAVB_NC) {
1562 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1563 if (!ts_skb) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001564 desc--;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001565 dma_unmap_single(ndev->dev.parent, dma_addr, len,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001566 DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001567 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001568 }
1569 ts_skb->skb = skb;
1570 ts_skb->tag = priv->ts_skb_tag++;
1571 priv->ts_skb_tag &= 0x3ff;
1572 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1573
1574 /* TAG and timestamp required flag */
1575 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001576 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1577 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1578 }
1579
Lino Sanfilippod7be81a2016-03-27 12:22:02 +02001580 skb_tx_timestamp(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001581 /* Descriptor type must be set after all the above writes */
1582 dma_wmb();
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001583 desc->die_dt = DT_FEND;
1584 desc--;
1585 desc->die_dt = DT_FSTART;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001586
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001587 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001588
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001589 priv->cur_tx[q] += NUM_TX_DESC;
1590 if (priv->cur_tx[q] - priv->dirty_tx[q] >
Kazuya Mizuguchia47b70e2017-01-26 14:29:27 +01001591 (priv->num_tx_ring[q] - 1) * NUM_TX_DESC &&
1592 !ravb_tx_free(ndev, q, true))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001593 netif_stop_subqueue(ndev, q);
1594
1595exit:
1596 mmiowb();
1597 spin_unlock_irqrestore(&priv->lock, flags);
1598 return NETDEV_TX_OK;
1599
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001600unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001601 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001602 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001603drop:
1604 dev_kfree_skb_any(skb);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001605 priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001606 goto exit;
1607}
1608
1609static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1610 void *accel_priv, select_queue_fallback_t fallback)
1611{
1612 /* If skb needs TX timestamp, it is handled in network control queue */
1613 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1614 RAVB_BE;
1615
1616}
1617
1618static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1619{
1620 struct ravb_private *priv = netdev_priv(ndev);
1621 struct net_device_stats *nstats, *stats0, *stats1;
1622
1623 nstats = &ndev->stats;
1624 stats0 = &priv->stats[RAVB_BE];
1625 stats1 = &priv->stats[RAVB_NC];
1626
1627 nstats->tx_dropped += ravb_read(ndev, TROCR);
1628 ravb_write(ndev, 0, TROCR); /* (write clear) */
1629 nstats->collisions += ravb_read(ndev, CDCR);
1630 ravb_write(ndev, 0, CDCR); /* (write clear) */
1631 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1632 ravb_write(ndev, 0, LCCR); /* (write clear) */
1633
1634 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1635 ravb_write(ndev, 0, CERCR); /* (write clear) */
1636 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1637 ravb_write(ndev, 0, CEECR); /* (write clear) */
1638
1639 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1640 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1641 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1642 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1643 nstats->multicast = stats0->multicast + stats1->multicast;
1644 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1645 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1646 nstats->rx_frame_errors =
1647 stats0->rx_frame_errors + stats1->rx_frame_errors;
1648 nstats->rx_length_errors =
1649 stats0->rx_length_errors + stats1->rx_length_errors;
1650 nstats->rx_missed_errors =
1651 stats0->rx_missed_errors + stats1->rx_missed_errors;
1652 nstats->rx_over_errors =
1653 stats0->rx_over_errors + stats1->rx_over_errors;
1654
1655 return nstats;
1656}
1657
1658/* Update promiscuous bit */
1659static void ravb_set_rx_mode(struct net_device *ndev)
1660{
1661 struct ravb_private *priv = netdev_priv(ndev);
1662 unsigned long flags;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001663
1664 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001665 ravb_modify(ndev, ECMR, ECMR_PRM,
1666 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001667 mmiowb();
1668 spin_unlock_irqrestore(&priv->lock, flags);
1669}
1670
1671/* Device close function for Ethernet AVB */
1672static int ravb_close(struct net_device *ndev)
1673{
Johan Hovold9f70eb32016-11-28 19:25:06 +01001674 struct device_node *np = ndev->dev.parent->of_node;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001675 struct ravb_private *priv = netdev_priv(ndev);
1676 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1677
1678 netif_tx_stop_all_queues(ndev);
1679
1680 /* Disable interrupts by clearing the interrupt masks. */
1681 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001682 ravb_write(ndev, 0, RIC2);
1683 ravb_write(ndev, 0, TIC);
1684
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001685 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001686 if (priv->chip_id == RCAR_GEN2)
1687 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001688
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001689 /* Set the config mode to stop the AVB-DMAC's processes */
1690 if (ravb_stop_dma(ndev) < 0)
1691 netdev_err(ndev,
1692 "device will be stopped after h/w processes are done.\n");
1693
1694 /* Clear the timestamp list */
1695 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1696 list_del(&ts_skb->list);
1697 kfree(ts_skb);
1698 }
1699
1700 /* PHY disconnect */
Philippe Reynes0f635172016-08-20 00:52:18 +02001701 if (ndev->phydev) {
1702 phy_stop(ndev->phydev);
1703 phy_disconnect(ndev->phydev);
Johan Hovold9f70eb32016-11-28 19:25:06 +01001704 if (of_phy_is_fixed_link(np))
1705 of_phy_deregister_fixed_link(np);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001706 }
1707
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001708 if (priv->chip_id != RCAR_GEN2) {
1709 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1710 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1711 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1712 free_irq(priv->rx_irqs[RAVB_BE], ndev);
Geert Uytterhoeven7fa816b2016-05-07 13:17:11 +02001713 free_irq(priv->emac_irq, ndev);
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001714 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001715 free_irq(ndev->irq, ndev);
1716
1717 napi_disable(&priv->napi[RAVB_NC]);
1718 napi_disable(&priv->napi[RAVB_BE]);
1719
1720 /* Free all the skb's in the RX queue and the DMA buffers. */
1721 ravb_ring_free(ndev, RAVB_BE);
1722 ravb_ring_free(ndev, RAVB_NC);
1723
1724 return 0;
1725}
1726
1727static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1728{
1729 struct ravb_private *priv = netdev_priv(ndev);
1730 struct hwtstamp_config config;
1731
1732 config.flags = 0;
1733 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1734 HWTSTAMP_TX_OFF;
1735 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1736 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1737 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1738 config.rx_filter = HWTSTAMP_FILTER_ALL;
1739 else
1740 config.rx_filter = HWTSTAMP_FILTER_NONE;
1741
1742 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1743 -EFAULT : 0;
1744}
1745
1746/* Control hardware time stamping */
1747static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1748{
1749 struct ravb_private *priv = netdev_priv(ndev);
1750 struct hwtstamp_config config;
1751 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1752 u32 tstamp_tx_ctrl;
1753
1754 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1755 return -EFAULT;
1756
1757 /* Reserved for future extensions */
1758 if (config.flags)
1759 return -EINVAL;
1760
1761 switch (config.tx_type) {
1762 case HWTSTAMP_TX_OFF:
1763 tstamp_tx_ctrl = 0;
1764 break;
1765 case HWTSTAMP_TX_ON:
1766 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1767 break;
1768 default:
1769 return -ERANGE;
1770 }
1771
1772 switch (config.rx_filter) {
1773 case HWTSTAMP_FILTER_NONE:
1774 tstamp_rx_ctrl = 0;
1775 break;
1776 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1777 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1778 break;
1779 default:
1780 config.rx_filter = HWTSTAMP_FILTER_ALL;
1781 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1782 }
1783
1784 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1785 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1786
1787 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1788 -EFAULT : 0;
1789}
1790
1791/* ioctl to device function */
1792static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1793{
Philippe Reynes0f635172016-08-20 00:52:18 +02001794 struct phy_device *phydev = ndev->phydev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001795
1796 if (!netif_running(ndev))
1797 return -EINVAL;
1798
1799 if (!phydev)
1800 return -ENODEV;
1801
1802 switch (cmd) {
1803 case SIOCGHWTSTAMP:
1804 return ravb_hwtstamp_get(ndev, req);
1805 case SIOCSHWTSTAMP:
1806 return ravb_hwtstamp_set(ndev, req);
1807 }
1808
1809 return phy_mii_ioctl(phydev, req, cmd);
1810}
1811
1812static const struct net_device_ops ravb_netdev_ops = {
1813 .ndo_open = ravb_open,
1814 .ndo_stop = ravb_close,
1815 .ndo_start_xmit = ravb_start_xmit,
1816 .ndo_select_queue = ravb_select_queue,
1817 .ndo_get_stats = ravb_get_stats,
1818 .ndo_set_rx_mode = ravb_set_rx_mode,
1819 .ndo_tx_timeout = ravb_tx_timeout,
1820 .ndo_do_ioctl = ravb_do_ioctl,
1821 .ndo_validate_addr = eth_validate_addr,
1822 .ndo_set_mac_address = eth_mac_addr,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001823};
1824
1825/* MDIO bus init function */
1826static int ravb_mdio_init(struct ravb_private *priv)
1827{
1828 struct platform_device *pdev = priv->pdev;
1829 struct device *dev = &pdev->dev;
1830 int error;
1831
1832 /* Bitbang init */
1833 priv->mdiobb.ops = &bb_ops;
1834
1835 /* MII controller setting */
1836 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1837 if (!priv->mii_bus)
1838 return -ENOMEM;
1839
1840 /* Hook up MII support for ethtool */
1841 priv->mii_bus->name = "ravb_mii";
1842 priv->mii_bus->parent = dev;
1843 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1844 pdev->name, pdev->id);
1845
1846 /* Register MDIO bus */
1847 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1848 if (error)
1849 goto out_free_bus;
1850
1851 return 0;
1852
1853out_free_bus:
1854 free_mdio_bitbang(priv->mii_bus);
1855 return error;
1856}
1857
1858/* MDIO bus release function */
1859static int ravb_mdio_release(struct ravb_private *priv)
1860{
1861 /* Unregister mdio bus */
1862 mdiobus_unregister(priv->mii_bus);
1863
1864 /* Free bitbang info */
1865 free_mdio_bitbang(priv->mii_bus);
1866
1867 return 0;
1868}
1869
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001870static const struct of_device_id ravb_match_table[] = {
1871 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1872 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001873 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001874 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001875 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001876 { }
1877};
1878MODULE_DEVICE_TABLE(of, ravb_match_table);
1879
Simon Hormanb3d39a82015-11-20 11:29:39 -08001880static int ravb_set_gti(struct net_device *ndev)
1881{
1882
1883 struct device *dev = ndev->dev.parent;
1884 struct device_node *np = dev->of_node;
1885 unsigned long rate;
1886 struct clk *clk;
1887 uint64_t inc;
1888
1889 clk = of_clk_get(np, 0);
1890 if (IS_ERR(clk)) {
1891 dev_err(dev, "could not get clock\n");
1892 return PTR_ERR(clk);
1893 }
1894
1895 rate = clk_get_rate(clk);
1896 clk_put(clk);
1897
Wolfram Sanga6d37132016-04-08 13:28:42 +02001898 if (!rate)
1899 return -EINVAL;
1900
Simon Hormanb3d39a82015-11-20 11:29:39 -08001901 inc = 1000000000ULL << 20;
1902 do_div(inc, rate);
1903
1904 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1905 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1906 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1907 return -EINVAL;
1908 }
1909
1910 ravb_write(ndev, inc, GTI);
1911
1912 return 0;
1913}
1914
Niklas Söderlund01841652016-08-03 15:56:47 +02001915static void ravb_set_config_mode(struct net_device *ndev)
1916{
1917 struct ravb_private *priv = netdev_priv(ndev);
1918
1919 if (priv->chip_id == RCAR_GEN2) {
1920 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
1921 /* Set CSEL value */
1922 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
1923 } else {
1924 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
1925 CCC_GAC | CCC_CSEL_HPB);
1926 }
1927}
1928
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01001929/* Set tx and rx clock internal delay modes */
1930static void ravb_set_delay_mode(struct net_device *ndev)
1931{
1932 struct ravb_private *priv = netdev_priv(ndev);
1933 int set = 0;
1934
1935 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1936 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID)
1937 set |= APSR_DM_RDM;
1938
1939 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1940 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1941 set |= APSR_DM_TDM;
1942
1943 ravb_modify(ndev, APSR, APSR_DM, set);
1944}
1945
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001946static int ravb_probe(struct platform_device *pdev)
1947{
1948 struct device_node *np = pdev->dev.of_node;
1949 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001950 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001951 struct net_device *ndev;
1952 int error, irq, q;
1953 struct resource *res;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001954 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001955
1956 if (!np) {
1957 dev_err(&pdev->dev,
1958 "this driver is required to be instantiated from device tree\n");
1959 return -EINVAL;
1960 }
1961
1962 /* Get base address */
1963 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1964 if (!res) {
1965 dev_err(&pdev->dev, "invalid resource\n");
1966 return -EINVAL;
1967 }
1968
1969 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
1970 NUM_TX_QUEUE, NUM_RX_QUEUE);
1971 if (!ndev)
1972 return -ENOMEM;
1973
1974 pm_runtime_enable(&pdev->dev);
1975 pm_runtime_get_sync(&pdev->dev);
1976
1977 /* The Ether-specific entries in the device structure. */
1978 ndev->base_addr = res->start;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001979
Wolfram Sange8668632016-03-01 17:37:58 +01001980 chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001981
1982 if (chip_id == RCAR_GEN3)
1983 irq = platform_get_irq_byname(pdev, "ch22");
1984 else
1985 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001986 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03001987 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001988 goto out_release;
1989 }
1990 ndev->irq = irq;
1991
1992 SET_NETDEV_DEV(ndev, &pdev->dev);
1993
1994 priv = netdev_priv(ndev);
1995 priv->ndev = ndev;
1996 priv->pdev = pdev;
1997 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
1998 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
1999 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
2000 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
2001 priv->addr = devm_ioremap_resource(&pdev->dev, res);
2002 if (IS_ERR(priv->addr)) {
2003 error = PTR_ERR(priv->addr);
2004 goto out_release;
2005 }
2006
2007 spin_lock_init(&priv->lock);
2008 INIT_WORK(&priv->work, ravb_tx_timeout_work);
2009
2010 priv->phy_interface = of_get_phy_mode(np);
2011
2012 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
2013 priv->avb_link_active_low =
2014 of_property_read_bool(np, "renesas,ether-link-active-low");
2015
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002016 if (chip_id == RCAR_GEN3) {
2017 irq = platform_get_irq_byname(pdev, "ch24");
2018 if (irq < 0) {
2019 error = irq;
2020 goto out_release;
2021 }
2022 priv->emac_irq = irq;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09002023 for (i = 0; i < NUM_RX_QUEUE; i++) {
2024 irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
2025 if (irq < 0) {
2026 error = irq;
2027 goto out_release;
2028 }
2029 priv->rx_irqs[i] = irq;
2030 }
2031 for (i = 0; i < NUM_TX_QUEUE; i++) {
2032 irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
2033 if (irq < 0) {
2034 error = irq;
2035 goto out_release;
2036 }
2037 priv->tx_irqs[i] = irq;
2038 }
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09002039 }
2040
2041 priv->chip_id = chip_id;
2042
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002043 /* Set function */
2044 ndev->netdev_ops = &ravb_netdev_ops;
2045 ndev->ethtool_ops = &ravb_ethtool_ops;
2046
2047 /* Set AVB config mode */
Niklas Söderlund01841652016-08-03 15:56:47 +02002048 ravb_set_config_mode(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002049
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002050 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08002051 error = ravb_set_gti(ndev);
2052 if (error)
2053 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002054
2055 /* Request GTI loading */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03002056 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002057
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002058 if (priv->chip_id != RCAR_GEN2)
2059 ravb_set_delay_mode(ndev);
2060
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002061 /* Allocate descriptor base address table */
2062 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002063 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002064 &priv->desc_bat_dma, GFP_KERNEL);
2065 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09002066 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002067 "Cannot allocate desc base address table (size %d bytes)\n",
2068 priv->desc_bat_size);
2069 error = -ENOMEM;
2070 goto out_release;
2071 }
2072 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2073 priv->desc_bat[q].die_dt = DT_EOS;
2074 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2075
2076 /* Initialise HW timestamp list */
2077 INIT_LIST_HEAD(&priv->ts_skb_list);
2078
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002079 /* Initialise PTP Clock driver */
2080 if (chip_id != RCAR_GEN2)
2081 ravb_ptp_init(ndev, pdev);
2082
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002083 /* Debug message level */
2084 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2085
2086 /* Read and set MAC address */
2087 ravb_read_mac_address(ndev, of_get_mac_address(np));
2088 if (!is_valid_ether_addr(ndev->dev_addr)) {
2089 dev_warn(&pdev->dev,
2090 "no valid MAC address supplied, using a random one\n");
2091 eth_hw_addr_random(ndev);
2092 }
2093
2094 /* MDIO bus init */
2095 error = ravb_mdio_init(priv);
2096 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09002097 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002098 goto out_dma_free;
2099 }
2100
2101 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
2102 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
2103
2104 /* Network device register */
2105 error = register_netdev(ndev);
2106 if (error)
2107 goto out_napi_del;
2108
2109 /* Print device information */
2110 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2111 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2112
2113 platform_set_drvdata(pdev, ndev);
2114
2115 return 0;
2116
2117out_napi_del:
2118 netif_napi_del(&priv->napi[RAVB_NC]);
2119 netif_napi_del(&priv->napi[RAVB_BE]);
2120 ravb_mdio_release(priv);
2121out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002122 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002123 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002124
2125 /* Stop PTP Clock driver */
2126 if (chip_id != RCAR_GEN2)
2127 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002128out_release:
2129 if (ndev)
2130 free_netdev(ndev);
2131
2132 pm_runtime_put(&pdev->dev);
2133 pm_runtime_disable(&pdev->dev);
2134 return error;
2135}
2136
2137static int ravb_remove(struct platform_device *pdev)
2138{
2139 struct net_device *ndev = platform_get_drvdata(pdev);
2140 struct ravb_private *priv = netdev_priv(ndev);
2141
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002142 /* Stop PTP Clock driver */
2143 if (priv->chip_id != RCAR_GEN2)
2144 ravb_ptp_stop(ndev);
2145
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002146 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002147 priv->desc_bat_dma);
2148 /* Set reset mode */
2149 ravb_write(ndev, CCC_OPC_RESET, CCC);
2150 pm_runtime_put_sync(&pdev->dev);
2151 unregister_netdev(ndev);
2152 netif_napi_del(&priv->napi[RAVB_NC]);
2153 netif_napi_del(&priv->napi[RAVB_BE]);
2154 ravb_mdio_release(priv);
2155 pm_runtime_disable(&pdev->dev);
2156 free_netdev(ndev);
2157 platform_set_drvdata(pdev, NULL);
2158
2159 return 0;
2160}
2161
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002162static int __maybe_unused ravb_suspend(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002163{
2164 struct net_device *ndev = dev_get_drvdata(dev);
2165 int ret = 0;
2166
2167 if (netif_running(ndev)) {
2168 netif_device_detach(ndev);
2169 ret = ravb_close(ndev);
2170 }
2171
2172 return ret;
2173}
2174
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002175static int __maybe_unused ravb_resume(struct device *dev)
Niklas Söderlund01841652016-08-03 15:56:47 +02002176{
2177 struct net_device *ndev = dev_get_drvdata(dev);
2178 struct ravb_private *priv = netdev_priv(ndev);
2179 int ret = 0;
2180
2181 /* All register have been reset to default values.
2182 * Restore all registers which where setup at probe time and
2183 * reopen device if it was running before system suspended.
2184 */
2185
2186 /* Set AVB config mode */
2187 ravb_set_config_mode(ndev);
2188
2189 /* Set GTI value */
2190 ret = ravb_set_gti(ndev);
2191 if (ret)
2192 return ret;
2193
2194 /* Request GTI loading */
2195 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
2196
Kazuya Mizuguchi61fccb22017-01-27 20:46:26 +01002197 if (priv->chip_id != RCAR_GEN2)
2198 ravb_set_delay_mode(ndev);
2199
Niklas Söderlund01841652016-08-03 15:56:47 +02002200 /* Restore descriptor base address table */
2201 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2202
2203 if (netif_running(ndev)) {
2204 ret = ravb_open(ndev);
2205 if (ret < 0)
2206 return ret;
2207 netif_device_attach(ndev);
2208 }
2209
2210 return ret;
2211}
2212
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002213static int __maybe_unused ravb_runtime_nop(struct device *dev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002214{
2215 /* Runtime PM callback shared between ->runtime_suspend()
2216 * and ->runtime_resume(). Simply returns success.
2217 *
2218 * This driver re-initializes all registers after
2219 * pm_runtime_get_sync() anyway so there is no need
2220 * to save and restore registers here.
2221 */
2222 return 0;
2223}
2224
2225static const struct dev_pm_ops ravb_dev_pm_ops = {
Niklas Söderlundb89b8152016-08-10 13:09:49 +02002226 SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume)
Kazuya Mizuguchi524c6f62016-05-30 05:25:43 +09002227 SET_RUNTIME_PM_OPS(ravb_runtime_nop, ravb_runtime_nop, NULL)
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002228};
2229
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002230static struct platform_driver ravb_driver = {
2231 .probe = ravb_probe,
2232 .remove = ravb_remove,
2233 .driver = {
2234 .name = "ravb",
Arnd Bergmann1ddcf412016-08-26 17:30:29 +02002235 .pm = &ravb_dev_pm_ops,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002236 .of_match_table = ravb_match_table,
2237 },
2238};
2239
2240module_platform_driver(ravb_driver);
2241
2242MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2243MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2244MODULE_LICENSE("GPL v2");