blob: 4b71951e185d8ad73e145d95693be097c46e33d0 [file] [log] [blame]
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001/* Renesas Ethernet AVB device driver
2 *
3 * Copyright (C) 2014-2015 Renesas Electronics Corporation
4 * Copyright (C) 2015 Renesas Solutions Corp.
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03005 * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
Sergei Shtylyovc1566332015-06-11 01:01:43 +03006 *
7 * Based on the SuperH Ethernet driver
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
12 */
13
14#include <linux/cache.h>
15#include <linux/clk.h>
16#include <linux/delay.h>
17#include <linux/dma-mapping.h>
18#include <linux/err.h>
19#include <linux/etherdevice.h>
20#include <linux/ethtool.h>
21#include <linux/if_vlan.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
24#include <linux/module.h>
25#include <linux/net_tstamp.h>
26#include <linux/of.h>
27#include <linux/of_device.h>
28#include <linux/of_irq.h>
29#include <linux/of_mdio.h>
30#include <linux/of_net.h>
Sergei Shtylyovc1566332015-06-11 01:01:43 +030031#include <linux/pm_runtime.h>
32#include <linux/slab.h>
33#include <linux/spinlock.h>
34
Simon Hormanb3d39a82015-11-20 11:29:39 -080035#include <asm/div64.h>
36
Sergei Shtylyovc1566332015-06-11 01:01:43 +030037#include "ravb.h"
38
39#define RAVB_DEF_MSG_ENABLE \
40 (NETIF_MSG_LINK | \
41 NETIF_MSG_TIMER | \
42 NETIF_MSG_RX_ERR | \
43 NETIF_MSG_TX_ERR)
44
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +090045static const char *ravb_rx_irqs[NUM_RX_QUEUE] = {
46 "ch0", /* RAVB_BE */
47 "ch1", /* RAVB_NC */
48};
49
50static const char *ravb_tx_irqs[NUM_TX_QUEUE] = {
51 "ch18", /* RAVB_BE */
52 "ch19", /* RAVB_NC */
53};
54
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030055void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear,
56 u32 set)
57{
58 ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg);
59}
60
Sergei Shtylyova0d2f202015-06-11 01:02:30 +030061int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value)
Sergei Shtylyovc1566332015-06-11 01:01:43 +030062{
63 int i;
64
65 for (i = 0; i < 10000; i++) {
66 if ((ravb_read(ndev, reg) & mask) == value)
67 return 0;
68 udelay(10);
69 }
70 return -ETIMEDOUT;
71}
72
73static int ravb_config(struct net_device *ndev)
74{
75 int error;
76
77 /* Set config mode */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030078 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030079 /* Check if the operating mode is changed to the config mode */
80 error = ravb_wait(ndev, CSR, CSR_OPS, CSR_OPS_CONFIG);
81 if (error)
82 netdev_err(ndev, "failed to switch device to config mode\n");
83
84 return error;
85}
86
87static void ravb_set_duplex(struct net_device *ndev)
88{
89 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030090
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +030091 ravb_modify(ndev, ECMR, ECMR_DM, priv->duplex ? ECMR_DM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +030092}
93
94static void ravb_set_rate(struct net_device *ndev)
95{
96 struct ravb_private *priv = netdev_priv(ndev);
97
98 switch (priv->speed) {
99 case 100: /* 100BASE */
100 ravb_write(ndev, GECMR_SPEED_100, GECMR);
101 break;
102 case 1000: /* 1000BASE */
103 ravb_write(ndev, GECMR_SPEED_1000, GECMR);
104 break;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300105 }
106}
107
108static void ravb_set_buffer_align(struct sk_buff *skb)
109{
110 u32 reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1);
111
112 if (reserve)
113 skb_reserve(skb, RAVB_ALIGN - reserve);
114}
115
116/* Get MAC address from the MAC address registers
117 *
118 * Ethernet AVB device doesn't have ROM for MAC address.
119 * This function gets the MAC address that was used by a bootloader.
120 */
121static void ravb_read_mac_address(struct net_device *ndev, const u8 *mac)
122{
123 if (mac) {
124 ether_addr_copy(ndev->dev_addr, mac);
125 } else {
Sergei Shtylyovd9660632015-12-05 00:58:07 +0300126 u32 mahr = ravb_read(ndev, MAHR);
127 u32 malr = ravb_read(ndev, MALR);
128
129 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
130 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
131 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
132 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
133 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
134 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300135 }
136}
137
138static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
139{
140 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
141 mdiobb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300142
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300143 ravb_modify(priv->ndev, PIR, mask, set ? mask : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300144}
145
146/* MDC pin control */
147static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level)
148{
149 ravb_mdio_ctrl(ctrl, PIR_MDC, level);
150}
151
152/* Data I/O pin control */
153static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
154{
155 ravb_mdio_ctrl(ctrl, PIR_MMD, output);
156}
157
158/* Set data bit */
159static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
160{
161 ravb_mdio_ctrl(ctrl, PIR_MDO, value);
162}
163
164/* Get data bit */
165static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl)
166{
167 struct ravb_private *priv = container_of(ctrl, struct ravb_private,
168 mdiobb);
169
170 return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0;
171}
172
173/* MDIO bus control struct */
174static struct mdiobb_ops bb_ops = {
175 .owner = THIS_MODULE,
176 .set_mdc = ravb_set_mdc,
177 .set_mdio_dir = ravb_set_mdio_dir,
178 .set_mdio_data = ravb_set_mdio_data,
179 .get_mdio_data = ravb_get_mdio_data,
180};
181
182/* Free skb's and DMA buffers for Ethernet AVB */
183static void ravb_ring_free(struct net_device *ndev, int q)
184{
185 struct ravb_private *priv = netdev_priv(ndev);
186 int ring_size;
187 int i;
188
189 /* Free RX skb ringbuffer */
190 if (priv->rx_skb[q]) {
191 for (i = 0; i < priv->num_rx_ring[q]; i++)
192 dev_kfree_skb(priv->rx_skb[q][i]);
193 }
194 kfree(priv->rx_skb[q]);
195 priv->rx_skb[q] = NULL;
196
197 /* Free TX skb ringbuffer */
198 if (priv->tx_skb[q]) {
199 for (i = 0; i < priv->num_tx_ring[q]; i++)
200 dev_kfree_skb(priv->tx_skb[q][i]);
201 }
202 kfree(priv->tx_skb[q]);
203 priv->tx_skb[q] = NULL;
204
205 /* Free aligned TX buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300206 kfree(priv->tx_align[q]);
207 priv->tx_align[q] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300208
209 if (priv->rx_ring[q]) {
210 ring_size = sizeof(struct ravb_ex_rx_desc) *
211 (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900212 dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300213 priv->rx_desc_dma[q]);
214 priv->rx_ring[q] = NULL;
215 }
216
217 if (priv->tx_ring[q]) {
218 ring_size = sizeof(struct ravb_tx_desc) *
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300219 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900220 dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q],
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300221 priv->tx_desc_dma[q]);
222 priv->tx_ring[q] = NULL;
223 }
224}
225
226/* Format skb and descriptor buffer for Ethernet AVB */
227static void ravb_ring_format(struct net_device *ndev, int q)
228{
229 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300230 struct ravb_ex_rx_desc *rx_desc;
231 struct ravb_tx_desc *tx_desc;
232 struct ravb_desc *desc;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300233 int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q];
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300234 int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] *
235 NUM_TX_DESC;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300236 dma_addr_t dma_addr;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300237 int i;
238
239 priv->cur_rx[q] = 0;
240 priv->cur_tx[q] = 0;
241 priv->dirty_rx[q] = 0;
242 priv->dirty_tx[q] = 0;
243
244 memset(priv->rx_ring[q], 0, rx_ring_size);
245 /* Build RX ring buffer */
246 for (i = 0; i < priv->num_rx_ring[q]; i++) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300247 /* RX descriptor */
248 rx_desc = &priv->rx_ring[q][i];
249 /* The size of the buffer should be on 16-byte boundary. */
250 rx_desc->ds_cc = cpu_to_le16(ALIGN(PKT_BUF_SZ, 16));
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900251 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300252 ALIGN(PKT_BUF_SZ, 16),
253 DMA_FROM_DEVICE);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300254 /* We just set the data size to 0 for a failed mapping which
255 * should prevent DMA from happening...
256 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900257 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300258 rx_desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300259 rx_desc->dptr = cpu_to_le32(dma_addr);
260 rx_desc->die_dt = DT_FEMPTY;
261 }
262 rx_desc = &priv->rx_ring[q][i];
263 rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
264 rx_desc->die_dt = DT_LINKFIX; /* type */
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300265
266 memset(priv->tx_ring[q], 0, tx_ring_size);
267 /* Build TX ring buffer */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300268 for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
269 i++, tx_desc++) {
270 tx_desc->die_dt = DT_EEMPTY;
271 tx_desc++;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300272 tx_desc->die_dt = DT_EEMPTY;
273 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300274 tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
275 tx_desc->die_dt = DT_LINKFIX; /* type */
276
277 /* RX descriptor base address for best effort */
278 desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
279 desc->die_dt = DT_LINKFIX; /* type */
280 desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
281
282 /* TX descriptor base address for best effort */
283 desc = &priv->desc_bat[q];
284 desc->die_dt = DT_LINKFIX; /* type */
285 desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
286}
287
288/* Init skb and descriptor buffer for Ethernet AVB */
289static int ravb_ring_init(struct net_device *ndev, int q)
290{
291 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300292 struct sk_buff *skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300293 int ring_size;
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300294 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300295
296 /* Allocate RX and TX skb rings */
297 priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
298 sizeof(*priv->rx_skb[q]), GFP_KERNEL);
299 priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
300 sizeof(*priv->tx_skb[q]), GFP_KERNEL);
301 if (!priv->rx_skb[q] || !priv->tx_skb[q])
302 goto error;
303
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300304 for (i = 0; i < priv->num_rx_ring[q]; i++) {
305 skb = netdev_alloc_skb(ndev, PKT_BUF_SZ + RAVB_ALIGN - 1);
306 if (!skb)
307 goto error;
308 ravb_set_buffer_align(skb);
309 priv->rx_skb[q][i] = skb;
310 }
311
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300312 /* Allocate rings for the aligned buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300313 priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
314 DPTR_ALIGN - 1, GFP_KERNEL);
315 if (!priv->tx_align[q])
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300316 goto error;
317
318 /* Allocate all RX descriptors. */
319 ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900320 priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300321 &priv->rx_desc_dma[q],
322 GFP_KERNEL);
323 if (!priv->rx_ring[q])
324 goto error;
325
326 priv->dirty_rx[q] = 0;
327
328 /* Allocate all TX descriptors. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300329 ring_size = sizeof(struct ravb_tx_desc) *
330 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900331 priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300332 &priv->tx_desc_dma[q],
333 GFP_KERNEL);
334 if (!priv->tx_ring[q])
335 goto error;
336
337 return 0;
338
339error:
340 ravb_ring_free(ndev, q);
341
342 return -ENOMEM;
343}
344
345/* E-MAC init function */
346static void ravb_emac_init(struct net_device *ndev)
347{
348 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300349
350 /* Receive frame limit set register */
351 ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
352
353 /* PAUSE prohibition */
Sergei Shtylyov1c1fa822016-01-11 00:27:38 +0300354 ravb_write(ndev, ECMR_ZPF | (priv->duplex ? ECMR_DM : 0) |
355 ECMR_TE | ECMR_RE, ECMR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300356
357 ravb_set_rate(ndev);
358
359 /* Set MAC address */
360 ravb_write(ndev,
361 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
362 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
363 ravb_write(ndev,
364 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
365
366 ravb_write(ndev, 1, MPR);
367
368 /* E-MAC status register clear */
369 ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
370
371 /* E-MAC interrupt enable register */
372 ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
373}
374
375/* Device init function for Ethernet AVB */
376static int ravb_dmac_init(struct net_device *ndev)
377{
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900378 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300379 int error;
380
381 /* Set CONFIG mode */
382 error = ravb_config(ndev);
383 if (error)
384 return error;
385
386 error = ravb_ring_init(ndev, RAVB_BE);
387 if (error)
388 return error;
389 error = ravb_ring_init(ndev, RAVB_NC);
390 if (error) {
391 ravb_ring_free(ndev, RAVB_BE);
392 return error;
393 }
394
395 /* Descriptor format */
396 ravb_ring_format(ndev, RAVB_BE);
397 ravb_ring_format(ndev, RAVB_NC);
398
399#if defined(__LITTLE_ENDIAN)
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300400 ravb_modify(ndev, CCC, CCC_BOC, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300401#else
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300402 ravb_modify(ndev, CCC, CCC_BOC, CCC_BOC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300403#endif
404
405 /* Set AVB RX */
406 ravb_write(ndev, RCR_EFFS | RCR_ENCF | RCR_ETS0 | 0x18000000, RCR);
407
408 /* Set FIFO size */
409 ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00222200, TGC);
410
411 /* Timestamp enable */
412 ravb_write(ndev, TCCR_TFEN, TCCR);
413
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900414 /* Interrupt init: */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900415 if (priv->chip_id == RCAR_GEN3) {
416 /* Clear DIL.DPLx */
417 ravb_write(ndev, 0, DIL);
418 /* Set queue specific interrupt */
419 ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE);
420 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300421 /* Frame receive */
422 ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900423 /* Disable FIFO full warning */
424 ravb_write(ndev, 0, RIC1);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300425 /* Receive FIFO full error, descriptor empty */
426 ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
427 /* Frame transmitted, timestamp FIFO updated */
428 ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
429
430 /* Setting the control will start the AVB-DMAC process. */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300431 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300432
433 return 0;
434}
435
436/* Free TX skb function for AVB-IP */
437static int ravb_tx_free(struct net_device *ndev, int q)
438{
439 struct ravb_private *priv = netdev_priv(ndev);
440 struct net_device_stats *stats = &priv->stats[q];
441 struct ravb_tx_desc *desc;
442 int free_num = 0;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300443 int entry;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300444 u32 size;
445
446 for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300447 entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
448 NUM_TX_DESC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300449 desc = &priv->tx_ring[q][entry];
450 if (desc->die_dt != DT_FEMPTY)
451 break;
452 /* Descriptor type must be checked before all other reads */
453 dma_rmb();
454 size = le16_to_cpu(desc->ds_tagl) & TX_DS;
455 /* Free the original skb. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300456 if (priv->tx_skb[q][entry / NUM_TX_DESC]) {
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900457 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300458 size, DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300459 /* Last packet descriptor? */
460 if (entry % NUM_TX_DESC == NUM_TX_DESC - 1) {
461 entry /= NUM_TX_DESC;
462 dev_kfree_skb_any(priv->tx_skb[q][entry]);
463 priv->tx_skb[q][entry] = NULL;
464 stats->tx_packets++;
465 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300466 free_num++;
467 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300468 stats->tx_bytes += size;
469 desc->die_dt = DT_EEMPTY;
470 }
471 return free_num;
472}
473
474static void ravb_get_tx_tstamp(struct net_device *ndev)
475{
476 struct ravb_private *priv = netdev_priv(ndev);
477 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
478 struct skb_shared_hwtstamps shhwtstamps;
479 struct sk_buff *skb;
480 struct timespec64 ts;
481 u16 tag, tfa_tag;
482 int count;
483 u32 tfa2;
484
485 count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
486 while (count--) {
487 tfa2 = ravb_read(ndev, TFA2);
488 tfa_tag = (tfa2 & TFA2_TST) >> 16;
489 ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
490 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
491 ravb_read(ndev, TFA1);
492 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
493 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
494 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
495 list) {
496 skb = ts_skb->skb;
497 tag = ts_skb->tag;
498 list_del(&ts_skb->list);
499 kfree(ts_skb);
500 if (tag == tfa_tag) {
501 skb_tstamp_tx(skb, &shhwtstamps);
502 break;
503 }
504 }
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300505 ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300506 }
507}
508
509/* Packet receive function for Ethernet AVB */
510static bool ravb_rx(struct net_device *ndev, int *quota, int q)
511{
512 struct ravb_private *priv = netdev_priv(ndev);
513 int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
514 int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
515 priv->cur_rx[q];
516 struct net_device_stats *stats = &priv->stats[q];
517 struct ravb_ex_rx_desc *desc;
518 struct sk_buff *skb;
519 dma_addr_t dma_addr;
520 struct timespec64 ts;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300521 u8 desc_status;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300522 u16 pkt_len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300523 int limit;
524
525 boguscnt = min(boguscnt, *quota);
526 limit = boguscnt;
527 desc = &priv->rx_ring[q][entry];
528 while (desc->die_dt != DT_FEMPTY) {
529 /* Descriptor type must be checked before all other reads */
530 dma_rmb();
531 desc_status = desc->msc;
532 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
533
534 if (--boguscnt < 0)
535 break;
536
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300537 /* We use 0-byte descriptors to mark the DMA mapping errors */
538 if (!pkt_len)
539 continue;
540
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300541 if (desc_status & MSC_MC)
542 stats->multicast++;
543
544 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
545 MSC_CEEF)) {
546 stats->rx_errors++;
547 if (desc_status & MSC_CRC)
548 stats->rx_crc_errors++;
549 if (desc_status & MSC_RFE)
550 stats->rx_frame_errors++;
551 if (desc_status & (MSC_RTLF | MSC_RTSF))
552 stats->rx_length_errors++;
553 if (desc_status & MSC_CEEF)
554 stats->rx_missed_errors++;
555 } else {
556 u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
557
558 skb = priv->rx_skb[q][entry];
559 priv->rx_skb[q][entry] = NULL;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900560 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyove2370f02015-07-15 00:56:52 +0300561 ALIGN(PKT_BUF_SZ, 16),
562 DMA_FROM_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300563 get_ts &= (q == RAVB_NC) ?
564 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
565 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
566 if (get_ts) {
567 struct skb_shared_hwtstamps *shhwtstamps;
568
569 shhwtstamps = skb_hwtstamps(skb);
570 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
571 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
572 32) | le32_to_cpu(desc->ts_sl);
573 ts.tv_nsec = le32_to_cpu(desc->ts_n);
574 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
575 }
576 skb_put(skb, pkt_len);
577 skb->protocol = eth_type_trans(skb, ndev);
578 napi_gro_receive(&priv->napi[q], skb);
579 stats->rx_packets++;
580 stats->rx_bytes += pkt_len;
581 }
582
583 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
584 desc = &priv->rx_ring[q][entry];
585 }
586
587 /* Refill the RX ring buffers. */
588 for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
589 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
590 desc = &priv->rx_ring[q][entry];
591 /* The size of the buffer should be on 16-byte boundary. */
592 desc->ds_cc = cpu_to_le16(ALIGN(PKT_BUF_SZ, 16));
593
594 if (!priv->rx_skb[q][entry]) {
595 skb = netdev_alloc_skb(ndev,
596 PKT_BUF_SZ + RAVB_ALIGN - 1);
597 if (!skb)
598 break; /* Better luck next round. */
599 ravb_set_buffer_align(skb);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900600 dma_addr = dma_map_single(ndev->dev.parent, skb->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300601 le16_to_cpu(desc->ds_cc),
602 DMA_FROM_DEVICE);
603 skb_checksum_none_assert(skb);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300604 /* We just set the data size to 0 for a failed mapping
605 * which should prevent DMA from happening...
606 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900607 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300608 desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300609 desc->dptr = cpu_to_le32(dma_addr);
610 priv->rx_skb[q][entry] = skb;
611 }
612 /* Descriptor type must be set after all the above writes */
613 dma_wmb();
614 desc->die_dt = DT_FEMPTY;
615 }
616
617 *quota -= limit - (++boguscnt);
618
619 return boguscnt <= 0;
620}
621
622static void ravb_rcv_snd_disable(struct net_device *ndev)
623{
624 /* Disable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300625 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300626}
627
628static void ravb_rcv_snd_enable(struct net_device *ndev)
629{
630 /* Enable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300631 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300632}
633
634/* function for waiting dma process finished */
635static int ravb_stop_dma(struct net_device *ndev)
636{
637 int error;
638
639 /* Wait for stopping the hardware TX process */
640 error = ravb_wait(ndev, TCCR,
641 TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
642 if (error)
643 return error;
644
645 error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
646 0);
647 if (error)
648 return error;
649
650 /* Stop the E-MAC's RX/TX processes. */
651 ravb_rcv_snd_disable(ndev);
652
653 /* Wait for stopping the RX DMA process */
654 error = ravb_wait(ndev, CSR, CSR_RPO, 0);
655 if (error)
656 return error;
657
658 /* Stop AVB-DMAC process */
659 return ravb_config(ndev);
660}
661
662/* E-MAC interrupt handler */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900663static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300664{
665 struct ravb_private *priv = netdev_priv(ndev);
666 u32 ecsr, psr;
667
668 ecsr = ravb_read(ndev, ECSR);
669 ravb_write(ndev, ecsr, ECSR); /* clear interrupt */
670 if (ecsr & ECSR_ICD)
671 ndev->stats.tx_carrier_errors++;
672 if (ecsr & ECSR_LCHNG) {
673 /* Link changed */
674 if (priv->no_avb_link)
675 return;
676 psr = ravb_read(ndev, PSR);
677 if (priv->avb_link_active_low)
678 psr ^= PSR_LMON;
679 if (!(psr & PSR_LMON)) {
680 /* DIsable RX and TX */
681 ravb_rcv_snd_disable(ndev);
682 } else {
683 /* Enable RX and TX */
684 ravb_rcv_snd_enable(ndev);
685 }
686 }
687}
688
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900689static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
690{
691 struct net_device *ndev = dev_id;
692 struct ravb_private *priv = netdev_priv(ndev);
693
694 spin_lock(&priv->lock);
695 ravb_emac_interrupt_unlocked(ndev);
696 mmiowb();
697 spin_unlock(&priv->lock);
698 return IRQ_HANDLED;
699}
700
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300701/* Error interrupt handler */
702static void ravb_error_interrupt(struct net_device *ndev)
703{
704 struct ravb_private *priv = netdev_priv(ndev);
705 u32 eis, ris2;
706
707 eis = ravb_read(ndev, EIS);
708 ravb_write(ndev, ~EIS_QFS, EIS);
709 if (eis & EIS_QFS) {
710 ris2 = ravb_read(ndev, RIS2);
711 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF), RIS2);
712
713 /* Receive Descriptor Empty int */
714 if (ris2 & RIS2_QFF0)
715 priv->stats[RAVB_BE].rx_over_errors++;
716
717 /* Receive Descriptor Empty int */
718 if (ris2 & RIS2_QFF1)
719 priv->stats[RAVB_NC].rx_over_errors++;
720
721 /* Receive FIFO Overflow int */
722 if (ris2 & RIS2_RFFF)
723 priv->rx_fifo_errors++;
724 }
725}
726
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900727static bool ravb_queue_interrupt(struct net_device *ndev, int q)
728{
729 struct ravb_private *priv = netdev_priv(ndev);
730 u32 ris0 = ravb_read(ndev, RIS0);
731 u32 ric0 = ravb_read(ndev, RIC0);
732 u32 tis = ravb_read(ndev, TIS);
733 u32 tic = ravb_read(ndev, TIC);
734
735 if (((ris0 & ric0) & BIT(q)) || ((tis & tic) & BIT(q))) {
736 if (napi_schedule_prep(&priv->napi[q])) {
737 /* Mask RX and TX interrupts */
738 if (priv->chip_id == RCAR_GEN2) {
739 ravb_write(ndev, ric0 & ~BIT(q), RIC0);
740 ravb_write(ndev, tic & ~BIT(q), TIC);
741 } else {
742 ravb_write(ndev, BIT(q), RID0);
743 ravb_write(ndev, BIT(q), TID);
744 }
745 __napi_schedule(&priv->napi[q]);
746 } else {
747 netdev_warn(ndev,
748 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
749 ris0, ric0);
750 netdev_warn(ndev,
751 " tx status 0x%08x, tx mask 0x%08x.\n",
752 tis, tic);
753 }
754 return true;
755 }
756 return false;
757}
758
759static bool ravb_timestamp_interrupt(struct net_device *ndev)
760{
761 u32 tis = ravb_read(ndev, TIS);
762
763 if (tis & TIS_TFUF) {
764 ravb_write(ndev, ~TIS_TFUF, TIS);
765 ravb_get_tx_tstamp(ndev);
766 return true;
767 }
768 return false;
769}
770
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300771static irqreturn_t ravb_interrupt(int irq, void *dev_id)
772{
773 struct net_device *ndev = dev_id;
774 struct ravb_private *priv = netdev_priv(ndev);
775 irqreturn_t result = IRQ_NONE;
776 u32 iss;
777
778 spin_lock(&priv->lock);
779 /* Get interrupt status */
780 iss = ravb_read(ndev, ISS);
781
782 /* Received and transmitted interrupts */
783 if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300784 int q;
785
786 /* Timestamp updated */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900787 if (ravb_timestamp_interrupt(ndev))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300788 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300789
790 /* Network control and best effort queue RX/TX */
791 for (q = RAVB_NC; q >= RAVB_BE; q--) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900792 if (ravb_queue_interrupt(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300793 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300794 }
795 }
796
797 /* E-MAC status summary */
798 if (iss & ISS_MS) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900799 ravb_emac_interrupt_unlocked(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300800 result = IRQ_HANDLED;
801 }
802
803 /* Error status summary */
804 if (iss & ISS_ES) {
805 ravb_error_interrupt(ndev);
806 result = IRQ_HANDLED;
807 }
808
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900809 /* gPTP interrupt status summary */
Yoshihiro Kaneko38c848c2016-03-16 00:52:16 +0900810 if ((iss & ISS_CGIS) && ravb_ptp_interrupt(ndev) == IRQ_HANDLED)
811 result = IRQ_HANDLED;
Sergei Shtylyova0d2f202015-06-11 01:02:30 +0300812
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300813 mmiowb();
814 spin_unlock(&priv->lock);
815 return result;
816}
817
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900818/* Timestamp/Error/gPTP interrupt handler */
819static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
820{
821 struct net_device *ndev = dev_id;
822 struct ravb_private *priv = netdev_priv(ndev);
823 irqreturn_t result = IRQ_NONE;
824 u32 iss;
825
826 spin_lock(&priv->lock);
827 /* Get interrupt status */
828 iss = ravb_read(ndev, ISS);
829
830 /* Timestamp updated */
831 if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
832 result = IRQ_HANDLED;
833
834 /* Error status summary */
835 if (iss & ISS_ES) {
836 ravb_error_interrupt(ndev);
837 result = IRQ_HANDLED;
838 }
839
840 /* gPTP interrupt status summary */
841 if ((iss & ISS_CGIS) && ravb_ptp_interrupt(ndev) == IRQ_HANDLED)
842 result = IRQ_HANDLED;
843
844 mmiowb();
845 spin_unlock(&priv->lock);
846 return result;
847}
848
849static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
850{
851 struct net_device *ndev = dev_id;
852 struct ravb_private *priv = netdev_priv(ndev);
853 irqreturn_t result = IRQ_NONE;
854
855 spin_lock(&priv->lock);
856
857 /* Network control/Best effort queue RX/TX */
858 if (ravb_queue_interrupt(ndev, q))
859 result = IRQ_HANDLED;
860
861 mmiowb();
862 spin_unlock(&priv->lock);
863 return result;
864}
865
866static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
867{
868 return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
869}
870
871static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
872{
873 return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
874}
875
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300876static int ravb_poll(struct napi_struct *napi, int budget)
877{
878 struct net_device *ndev = napi->dev;
879 struct ravb_private *priv = netdev_priv(ndev);
880 unsigned long flags;
881 int q = napi - priv->napi;
882 int mask = BIT(q);
883 int quota = budget;
884 u32 ris0, tis;
885
886 for (;;) {
887 tis = ravb_read(ndev, TIS);
888 ris0 = ravb_read(ndev, RIS0);
889 if (!((ris0 & mask) || (tis & mask)))
890 break;
891
892 /* Processing RX Descriptor Ring */
893 if (ris0 & mask) {
894 /* Clear RX interrupt */
895 ravb_write(ndev, ~mask, RIS0);
896 if (ravb_rx(ndev, &quota, q))
897 goto out;
898 }
899 /* Processing TX Descriptor Ring */
900 if (tis & mask) {
901 spin_lock_irqsave(&priv->lock, flags);
902 /* Clear TX interrupt */
903 ravb_write(ndev, ~mask, TIS);
904 ravb_tx_free(ndev, q);
905 netif_wake_subqueue(ndev, q);
906 mmiowb();
907 spin_unlock_irqrestore(&priv->lock, flags);
908 }
909 }
910
911 napi_complete(napi);
912
913 /* Re-enable RX/TX interrupts */
914 spin_lock_irqsave(&priv->lock, flags);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900915 if (priv->chip_id == RCAR_GEN2) {
916 ravb_modify(ndev, RIC0, mask, mask);
917 ravb_modify(ndev, TIC, mask, mask);
918 } else {
919 ravb_write(ndev, mask, RIE0);
920 ravb_write(ndev, mask, TIE);
921 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300922 mmiowb();
923 spin_unlock_irqrestore(&priv->lock, flags);
924
925 /* Receive error message handling */
926 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
927 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
928 if (priv->rx_over_errors != ndev->stats.rx_over_errors) {
929 ndev->stats.rx_over_errors = priv->rx_over_errors;
930 netif_err(priv, rx_err, ndev, "Receive Descriptor Empty\n");
931 }
932 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors) {
933 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
934 netif_err(priv, rx_err, ndev, "Receive FIFO Overflow\n");
935 }
936out:
937 return budget - quota;
938}
939
940/* PHY state control function */
941static void ravb_adjust_link(struct net_device *ndev)
942{
943 struct ravb_private *priv = netdev_priv(ndev);
944 struct phy_device *phydev = priv->phydev;
945 bool new_state = false;
946
947 if (phydev->link) {
948 if (phydev->duplex != priv->duplex) {
949 new_state = true;
950 priv->duplex = phydev->duplex;
951 ravb_set_duplex(ndev);
952 }
953
954 if (phydev->speed != priv->speed) {
955 new_state = true;
956 priv->speed = phydev->speed;
957 ravb_set_rate(ndev);
958 }
959 if (!priv->link) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300960 ravb_modify(ndev, ECMR, ECMR_TXF, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300961 new_state = true;
962 priv->link = phydev->link;
963 if (priv->no_avb_link)
964 ravb_rcv_snd_enable(ndev);
965 }
966 } else if (priv->link) {
967 new_state = true;
968 priv->link = 0;
969 priv->speed = 0;
970 priv->duplex = -1;
971 if (priv->no_avb_link)
972 ravb_rcv_snd_disable(ndev);
973 }
974
975 if (new_state && netif_msg_link(priv))
976 phy_print_status(phydev);
977}
978
979/* PHY init function */
980static int ravb_phy_init(struct net_device *ndev)
981{
982 struct device_node *np = ndev->dev.parent->of_node;
983 struct ravb_private *priv = netdev_priv(ndev);
984 struct phy_device *phydev;
985 struct device_node *pn;
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900986 int err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300987
988 priv->link = 0;
989 priv->speed = 0;
990 priv->duplex = -1;
991
992 /* Try connecting to PHY */
993 pn = of_parse_phandle(np, "phy-handle", 0);
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900994 if (!pn) {
995 /* In the case of a fixed PHY, the DT node associated
996 * to the PHY is the Ethernet MAC DT node.
997 */
998 if (of_phy_is_fixed_link(np)) {
999 err = of_phy_register_fixed_link(np);
1000 if (err)
1001 return err;
1002 }
1003 pn = of_node_get(np);
1004 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001005 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
1006 priv->phy_interface);
1007 if (!phydev) {
1008 netdev_err(ndev, "failed to connect PHY\n");
1009 return -ENOENT;
1010 }
1011
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001012 /* This driver only support 10/100Mbit speeds on Gen3
1013 * at this time.
1014 */
1015 if (priv->chip_id == RCAR_GEN3) {
1016 int err;
1017
1018 err = phy_set_max_speed(phydev, SPEED_100);
1019 if (err) {
1020 netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
1021 phy_disconnect(phydev);
1022 return err;
1023 }
1024
1025 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
1026 }
1027
Kazuya Mizuguchi54499962015-12-14 00:15:58 +09001028 /* 10BASE is not supported */
1029 phydev->supported &= ~PHY_10BT_FEATURES;
1030
Andrew Lunn22209432016-01-06 20:11:13 +01001031 phy_attached_info(phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001032
1033 priv->phydev = phydev;
1034
1035 return 0;
1036}
1037
1038/* PHY control start function */
1039static int ravb_phy_start(struct net_device *ndev)
1040{
1041 struct ravb_private *priv = netdev_priv(ndev);
1042 int error;
1043
1044 error = ravb_phy_init(ndev);
1045 if (error)
1046 return error;
1047
1048 phy_start(priv->phydev);
1049
1050 return 0;
1051}
1052
1053static int ravb_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1054{
1055 struct ravb_private *priv = netdev_priv(ndev);
1056 int error = -ENODEV;
1057 unsigned long flags;
1058
1059 if (priv->phydev) {
1060 spin_lock_irqsave(&priv->lock, flags);
1061 error = phy_ethtool_gset(priv->phydev, ecmd);
1062 spin_unlock_irqrestore(&priv->lock, flags);
1063 }
1064
1065 return error;
1066}
1067
1068static int ravb_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1069{
1070 struct ravb_private *priv = netdev_priv(ndev);
1071 unsigned long flags;
1072 int error;
1073
1074 if (!priv->phydev)
1075 return -ENODEV;
1076
1077 spin_lock_irqsave(&priv->lock, flags);
1078
1079 /* Disable TX and RX */
1080 ravb_rcv_snd_disable(ndev);
1081
1082 error = phy_ethtool_sset(priv->phydev, ecmd);
1083 if (error)
1084 goto error_exit;
1085
1086 if (ecmd->duplex == DUPLEX_FULL)
1087 priv->duplex = 1;
1088 else
1089 priv->duplex = 0;
1090
1091 ravb_set_duplex(ndev);
1092
1093error_exit:
1094 mdelay(1);
1095
1096 /* Enable TX and RX */
1097 ravb_rcv_snd_enable(ndev);
1098
1099 mmiowb();
1100 spin_unlock_irqrestore(&priv->lock, flags);
1101
1102 return error;
1103}
1104
1105static int ravb_nway_reset(struct net_device *ndev)
1106{
1107 struct ravb_private *priv = netdev_priv(ndev);
1108 int error = -ENODEV;
1109 unsigned long flags;
1110
1111 if (priv->phydev) {
1112 spin_lock_irqsave(&priv->lock, flags);
1113 error = phy_start_aneg(priv->phydev);
1114 spin_unlock_irqrestore(&priv->lock, flags);
1115 }
1116
1117 return error;
1118}
1119
1120static u32 ravb_get_msglevel(struct net_device *ndev)
1121{
1122 struct ravb_private *priv = netdev_priv(ndev);
1123
1124 return priv->msg_enable;
1125}
1126
1127static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1128{
1129 struct ravb_private *priv = netdev_priv(ndev);
1130
1131 priv->msg_enable = value;
1132}
1133
1134static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1135 "rx_queue_0_current",
1136 "tx_queue_0_current",
1137 "rx_queue_0_dirty",
1138 "tx_queue_0_dirty",
1139 "rx_queue_0_packets",
1140 "tx_queue_0_packets",
1141 "rx_queue_0_bytes",
1142 "tx_queue_0_bytes",
1143 "rx_queue_0_mcast_packets",
1144 "rx_queue_0_errors",
1145 "rx_queue_0_crc_errors",
1146 "rx_queue_0_frame_errors",
1147 "rx_queue_0_length_errors",
1148 "rx_queue_0_missed_errors",
1149 "rx_queue_0_over_errors",
1150
1151 "rx_queue_1_current",
1152 "tx_queue_1_current",
1153 "rx_queue_1_dirty",
1154 "tx_queue_1_dirty",
1155 "rx_queue_1_packets",
1156 "tx_queue_1_packets",
1157 "rx_queue_1_bytes",
1158 "tx_queue_1_bytes",
1159 "rx_queue_1_mcast_packets",
1160 "rx_queue_1_errors",
1161 "rx_queue_1_crc_errors",
Sergei Shtylyovb17c1d92015-12-04 01:51:10 +03001162 "rx_queue_1_frame_errors",
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001163 "rx_queue_1_length_errors",
1164 "rx_queue_1_missed_errors",
1165 "rx_queue_1_over_errors",
1166};
1167
1168#define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1169
1170static int ravb_get_sset_count(struct net_device *netdev, int sset)
1171{
1172 switch (sset) {
1173 case ETH_SS_STATS:
1174 return RAVB_STATS_LEN;
1175 default:
1176 return -EOPNOTSUPP;
1177 }
1178}
1179
1180static void ravb_get_ethtool_stats(struct net_device *ndev,
1181 struct ethtool_stats *stats, u64 *data)
1182{
1183 struct ravb_private *priv = netdev_priv(ndev);
1184 int i = 0;
1185 int q;
1186
1187 /* Device-specific stats */
1188 for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1189 struct net_device_stats *stats = &priv->stats[q];
1190
1191 data[i++] = priv->cur_rx[q];
1192 data[i++] = priv->cur_tx[q];
1193 data[i++] = priv->dirty_rx[q];
1194 data[i++] = priv->dirty_tx[q];
1195 data[i++] = stats->rx_packets;
1196 data[i++] = stats->tx_packets;
1197 data[i++] = stats->rx_bytes;
1198 data[i++] = stats->tx_bytes;
1199 data[i++] = stats->multicast;
1200 data[i++] = stats->rx_errors;
1201 data[i++] = stats->rx_crc_errors;
1202 data[i++] = stats->rx_frame_errors;
1203 data[i++] = stats->rx_length_errors;
1204 data[i++] = stats->rx_missed_errors;
1205 data[i++] = stats->rx_over_errors;
1206 }
1207}
1208
1209static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1210{
1211 switch (stringset) {
1212 case ETH_SS_STATS:
1213 memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
1214 break;
1215 }
1216}
1217
1218static void ravb_get_ringparam(struct net_device *ndev,
1219 struct ethtool_ringparam *ring)
1220{
1221 struct ravb_private *priv = netdev_priv(ndev);
1222
1223 ring->rx_max_pending = BE_RX_RING_MAX;
1224 ring->tx_max_pending = BE_TX_RING_MAX;
1225 ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1226 ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1227}
1228
1229static int ravb_set_ringparam(struct net_device *ndev,
1230 struct ethtool_ringparam *ring)
1231{
1232 struct ravb_private *priv = netdev_priv(ndev);
1233 int error;
1234
1235 if (ring->tx_pending > BE_TX_RING_MAX ||
1236 ring->rx_pending > BE_RX_RING_MAX ||
1237 ring->tx_pending < BE_TX_RING_MIN ||
1238 ring->rx_pending < BE_RX_RING_MIN)
1239 return -EINVAL;
1240 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1241 return -EINVAL;
1242
1243 if (netif_running(ndev)) {
1244 netif_device_detach(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001245 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001246 if (priv->chip_id == RCAR_GEN2)
1247 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001248 /* Wait for DMA stopping */
1249 error = ravb_stop_dma(ndev);
1250 if (error) {
1251 netdev_err(ndev,
1252 "cannot set ringparam! Any AVB processes are still running?\n");
1253 return error;
1254 }
1255 synchronize_irq(ndev->irq);
1256
1257 /* Free all the skb's in the RX queue and the DMA buffers. */
1258 ravb_ring_free(ndev, RAVB_BE);
1259 ravb_ring_free(ndev, RAVB_NC);
1260 }
1261
1262 /* Set new parameters */
1263 priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1264 priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1265
1266 if (netif_running(ndev)) {
1267 error = ravb_dmac_init(ndev);
1268 if (error) {
1269 netdev_err(ndev,
1270 "%s: ravb_dmac_init() failed, error %d\n",
1271 __func__, error);
1272 return error;
1273 }
1274
1275 ravb_emac_init(ndev);
1276
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001277 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001278 if (priv->chip_id == RCAR_GEN2)
1279 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001280
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001281 netif_device_attach(ndev);
1282 }
1283
1284 return 0;
1285}
1286
1287static int ravb_get_ts_info(struct net_device *ndev,
1288 struct ethtool_ts_info *info)
1289{
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001290 struct ravb_private *priv = netdev_priv(ndev);
1291
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001292 info->so_timestamping =
1293 SOF_TIMESTAMPING_TX_SOFTWARE |
1294 SOF_TIMESTAMPING_RX_SOFTWARE |
1295 SOF_TIMESTAMPING_SOFTWARE |
1296 SOF_TIMESTAMPING_TX_HARDWARE |
1297 SOF_TIMESTAMPING_RX_HARDWARE |
1298 SOF_TIMESTAMPING_RAW_HARDWARE;
1299 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1300 info->rx_filters =
1301 (1 << HWTSTAMP_FILTER_NONE) |
1302 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1303 (1 << HWTSTAMP_FILTER_ALL);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001304 info->phc_index = ptp_clock_index(priv->ptp.clock);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001305
1306 return 0;
1307}
1308
1309static const struct ethtool_ops ravb_ethtool_ops = {
1310 .get_settings = ravb_get_settings,
1311 .set_settings = ravb_set_settings,
1312 .nway_reset = ravb_nway_reset,
1313 .get_msglevel = ravb_get_msglevel,
1314 .set_msglevel = ravb_set_msglevel,
1315 .get_link = ethtool_op_get_link,
1316 .get_strings = ravb_get_strings,
1317 .get_ethtool_stats = ravb_get_ethtool_stats,
1318 .get_sset_count = ravb_get_sset_count,
1319 .get_ringparam = ravb_get_ringparam,
1320 .set_ringparam = ravb_set_ringparam,
1321 .get_ts_info = ravb_get_ts_info,
1322};
1323
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001324static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1325 struct net_device *ndev, struct device *dev,
1326 const char *ch)
1327{
1328 char *name;
1329 int error;
1330
1331 name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1332 if (!name)
1333 return -ENOMEM;
1334 error = request_irq(irq, handler, 0, name, ndev);
1335 if (error)
1336 netdev_err(ndev, "cannot request IRQ %s\n", name);
1337
1338 return error;
1339}
1340
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001341/* Network device open function for Ethernet AVB */
1342static int ravb_open(struct net_device *ndev)
1343{
1344 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001345 struct platform_device *pdev = priv->pdev;
1346 struct device *dev = &pdev->dev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001347 int error;
1348
1349 napi_enable(&priv->napi[RAVB_BE]);
1350 napi_enable(&priv->napi[RAVB_NC]);
1351
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001352 if (priv->chip_id == RCAR_GEN2) {
1353 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1354 ndev->name, ndev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001355 if (error) {
1356 netdev_err(ndev, "cannot request IRQ\n");
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001357 goto out_napi_off;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001358 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001359 } else {
1360 error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1361 dev, "ch22:multi");
1362 if (error)
1363 goto out_napi_off;
1364 error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1365 dev, "ch24:emac");
1366 if (error)
1367 goto out_free_irq;
1368 error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1369 ndev, dev, "ch0:rx_be");
1370 if (error)
1371 goto out_free_irq_emac;
1372 error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1373 ndev, dev, "ch18:tx_be");
1374 if (error)
1375 goto out_free_irq_be_rx;
1376 error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1377 ndev, dev, "ch1:rx_nc");
1378 if (error)
1379 goto out_free_irq_be_tx;
1380 error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1381 ndev, dev, "ch19:tx_nc");
1382 if (error)
1383 goto out_free_irq_nc_rx;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001384 }
1385
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001386 /* Device init */
1387 error = ravb_dmac_init(ndev);
1388 if (error)
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001389 goto out_free_irq_nc_tx;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001390 ravb_emac_init(ndev);
1391
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001392 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001393 if (priv->chip_id == RCAR_GEN2)
1394 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001395
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001396 netif_tx_start_all_queues(ndev);
1397
1398 /* PHY control start */
1399 error = ravb_phy_start(ndev);
1400 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001401 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001402
1403 return 0;
1404
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001405out_ptp_stop:
1406 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001407 if (priv->chip_id == RCAR_GEN2)
1408 ravb_ptp_stop(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001409out_free_irq_nc_tx:
1410 if (priv->chip_id == RCAR_GEN2)
1411 goto out_free_irq;
1412 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1413out_free_irq_nc_rx:
1414 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1415out_free_irq_be_tx:
1416 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1417out_free_irq_be_rx:
1418 free_irq(priv->rx_irqs[RAVB_BE], ndev);
1419out_free_irq_emac:
1420 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001421out_free_irq:
1422 free_irq(ndev->irq, ndev);
1423out_napi_off:
1424 napi_disable(&priv->napi[RAVB_NC]);
1425 napi_disable(&priv->napi[RAVB_BE]);
1426 return error;
1427}
1428
1429/* Timeout function for Ethernet AVB */
1430static void ravb_tx_timeout(struct net_device *ndev)
1431{
1432 struct ravb_private *priv = netdev_priv(ndev);
1433
1434 netif_err(priv, tx_err, ndev,
1435 "transmit timed out, status %08x, resetting...\n",
1436 ravb_read(ndev, ISS));
1437
1438 /* tx_errors count up */
1439 ndev->stats.tx_errors++;
1440
1441 schedule_work(&priv->work);
1442}
1443
1444static void ravb_tx_timeout_work(struct work_struct *work)
1445{
1446 struct ravb_private *priv = container_of(work, struct ravb_private,
1447 work);
1448 struct net_device *ndev = priv->ndev;
1449
1450 netif_tx_stop_all_queues(ndev);
1451
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001452 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001453 if (priv->chip_id == RCAR_GEN2)
1454 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001455
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001456 /* Wait for DMA stopping */
1457 ravb_stop_dma(ndev);
1458
1459 ravb_ring_free(ndev, RAVB_BE);
1460 ravb_ring_free(ndev, RAVB_NC);
1461
1462 /* Device init */
1463 ravb_dmac_init(ndev);
1464 ravb_emac_init(ndev);
1465
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001466 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001467 if (priv->chip_id == RCAR_GEN2)
1468 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001469
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001470 netif_tx_start_all_queues(ndev);
1471}
1472
1473/* Packet transmit function for Ethernet AVB */
1474static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1475{
1476 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001477 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001478 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001479 struct ravb_tx_desc *desc;
1480 unsigned long flags;
1481 u32 dma_addr;
1482 void *buffer;
1483 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001484 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001485
1486 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001487 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1488 NUM_TX_DESC) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001489 netif_err(priv, tx_queued, ndev,
1490 "still transmitting with the full ring!\n");
1491 netif_stop_subqueue(ndev, q);
1492 spin_unlock_irqrestore(&priv->lock, flags);
1493 return NETDEV_TX_BUSY;
1494 }
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001495 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1496 priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001497
1498 if (skb_put_padto(skb, ETH_ZLEN))
1499 goto drop;
1500
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001501 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1502 entry / NUM_TX_DESC * DPTR_ALIGN;
1503 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
1504 memcpy(buffer, skb->data, len);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001505 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1506 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001507 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001508
1509 desc = &priv->tx_ring[q][entry];
1510 desc->ds_tagl = cpu_to_le16(len);
1511 desc->dptr = cpu_to_le32(dma_addr);
1512
1513 buffer = skb->data + len;
1514 len = skb->len - len;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001515 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1516 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001517 goto unmap;
1518
1519 desc++;
1520 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001521 desc->dptr = cpu_to_le32(dma_addr);
1522
1523 /* TX timestamp required */
1524 if (q == RAVB_NC) {
1525 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1526 if (!ts_skb) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001527 desc--;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001528 dma_unmap_single(ndev->dev.parent, dma_addr, len,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001529 DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001530 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001531 }
1532 ts_skb->skb = skb;
1533 ts_skb->tag = priv->ts_skb_tag++;
1534 priv->ts_skb_tag &= 0x3ff;
1535 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1536
1537 /* TAG and timestamp required flag */
1538 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001539 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1540 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1541 }
1542
Lino Sanfilippod7be81a2016-03-27 12:22:02 +02001543 skb_tx_timestamp(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001544 /* Descriptor type must be set after all the above writes */
1545 dma_wmb();
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001546 desc->die_dt = DT_FEND;
1547 desc--;
1548 desc->die_dt = DT_FSTART;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001549
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001550 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001551
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001552 priv->cur_tx[q] += NUM_TX_DESC;
1553 if (priv->cur_tx[q] - priv->dirty_tx[q] >
1554 (priv->num_tx_ring[q] - 1) * NUM_TX_DESC && !ravb_tx_free(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001555 netif_stop_subqueue(ndev, q);
1556
1557exit:
1558 mmiowb();
1559 spin_unlock_irqrestore(&priv->lock, flags);
1560 return NETDEV_TX_OK;
1561
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001562unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001563 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001564 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001565drop:
1566 dev_kfree_skb_any(skb);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001567 priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001568 goto exit;
1569}
1570
1571static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1572 void *accel_priv, select_queue_fallback_t fallback)
1573{
1574 /* If skb needs TX timestamp, it is handled in network control queue */
1575 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1576 RAVB_BE;
1577
1578}
1579
1580static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1581{
1582 struct ravb_private *priv = netdev_priv(ndev);
1583 struct net_device_stats *nstats, *stats0, *stats1;
1584
1585 nstats = &ndev->stats;
1586 stats0 = &priv->stats[RAVB_BE];
1587 stats1 = &priv->stats[RAVB_NC];
1588
1589 nstats->tx_dropped += ravb_read(ndev, TROCR);
1590 ravb_write(ndev, 0, TROCR); /* (write clear) */
1591 nstats->collisions += ravb_read(ndev, CDCR);
1592 ravb_write(ndev, 0, CDCR); /* (write clear) */
1593 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1594 ravb_write(ndev, 0, LCCR); /* (write clear) */
1595
1596 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1597 ravb_write(ndev, 0, CERCR); /* (write clear) */
1598 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1599 ravb_write(ndev, 0, CEECR); /* (write clear) */
1600
1601 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1602 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1603 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1604 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1605 nstats->multicast = stats0->multicast + stats1->multicast;
1606 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1607 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1608 nstats->rx_frame_errors =
1609 stats0->rx_frame_errors + stats1->rx_frame_errors;
1610 nstats->rx_length_errors =
1611 stats0->rx_length_errors + stats1->rx_length_errors;
1612 nstats->rx_missed_errors =
1613 stats0->rx_missed_errors + stats1->rx_missed_errors;
1614 nstats->rx_over_errors =
1615 stats0->rx_over_errors + stats1->rx_over_errors;
1616
1617 return nstats;
1618}
1619
1620/* Update promiscuous bit */
1621static void ravb_set_rx_mode(struct net_device *ndev)
1622{
1623 struct ravb_private *priv = netdev_priv(ndev);
1624 unsigned long flags;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001625
1626 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001627 ravb_modify(ndev, ECMR, ECMR_PRM,
1628 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001629 mmiowb();
1630 spin_unlock_irqrestore(&priv->lock, flags);
1631}
1632
1633/* Device close function for Ethernet AVB */
1634static int ravb_close(struct net_device *ndev)
1635{
1636 struct ravb_private *priv = netdev_priv(ndev);
1637 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1638
1639 netif_tx_stop_all_queues(ndev);
1640
1641 /* Disable interrupts by clearing the interrupt masks. */
1642 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001643 ravb_write(ndev, 0, RIC2);
1644 ravb_write(ndev, 0, TIC);
1645
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001646 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001647 if (priv->chip_id == RCAR_GEN2)
1648 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001649
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001650 /* Set the config mode to stop the AVB-DMAC's processes */
1651 if (ravb_stop_dma(ndev) < 0)
1652 netdev_err(ndev,
1653 "device will be stopped after h/w processes are done.\n");
1654
1655 /* Clear the timestamp list */
1656 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1657 list_del(&ts_skb->list);
1658 kfree(ts_skb);
1659 }
1660
1661 /* PHY disconnect */
1662 if (priv->phydev) {
1663 phy_stop(priv->phydev);
1664 phy_disconnect(priv->phydev);
1665 priv->phydev = NULL;
1666 }
1667
1668 free_irq(ndev->irq, ndev);
1669
1670 napi_disable(&priv->napi[RAVB_NC]);
1671 napi_disable(&priv->napi[RAVB_BE]);
1672
1673 /* Free all the skb's in the RX queue and the DMA buffers. */
1674 ravb_ring_free(ndev, RAVB_BE);
1675 ravb_ring_free(ndev, RAVB_NC);
1676
1677 return 0;
1678}
1679
1680static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1681{
1682 struct ravb_private *priv = netdev_priv(ndev);
1683 struct hwtstamp_config config;
1684
1685 config.flags = 0;
1686 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1687 HWTSTAMP_TX_OFF;
1688 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1689 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1690 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1691 config.rx_filter = HWTSTAMP_FILTER_ALL;
1692 else
1693 config.rx_filter = HWTSTAMP_FILTER_NONE;
1694
1695 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1696 -EFAULT : 0;
1697}
1698
1699/* Control hardware time stamping */
1700static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1701{
1702 struct ravb_private *priv = netdev_priv(ndev);
1703 struct hwtstamp_config config;
1704 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1705 u32 tstamp_tx_ctrl;
1706
1707 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1708 return -EFAULT;
1709
1710 /* Reserved for future extensions */
1711 if (config.flags)
1712 return -EINVAL;
1713
1714 switch (config.tx_type) {
1715 case HWTSTAMP_TX_OFF:
1716 tstamp_tx_ctrl = 0;
1717 break;
1718 case HWTSTAMP_TX_ON:
1719 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1720 break;
1721 default:
1722 return -ERANGE;
1723 }
1724
1725 switch (config.rx_filter) {
1726 case HWTSTAMP_FILTER_NONE:
1727 tstamp_rx_ctrl = 0;
1728 break;
1729 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1730 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1731 break;
1732 default:
1733 config.rx_filter = HWTSTAMP_FILTER_ALL;
1734 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1735 }
1736
1737 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1738 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1739
1740 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1741 -EFAULT : 0;
1742}
1743
1744/* ioctl to device function */
1745static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1746{
1747 struct ravb_private *priv = netdev_priv(ndev);
1748 struct phy_device *phydev = priv->phydev;
1749
1750 if (!netif_running(ndev))
1751 return -EINVAL;
1752
1753 if (!phydev)
1754 return -ENODEV;
1755
1756 switch (cmd) {
1757 case SIOCGHWTSTAMP:
1758 return ravb_hwtstamp_get(ndev, req);
1759 case SIOCSHWTSTAMP:
1760 return ravb_hwtstamp_set(ndev, req);
1761 }
1762
1763 return phy_mii_ioctl(phydev, req, cmd);
1764}
1765
1766static const struct net_device_ops ravb_netdev_ops = {
1767 .ndo_open = ravb_open,
1768 .ndo_stop = ravb_close,
1769 .ndo_start_xmit = ravb_start_xmit,
1770 .ndo_select_queue = ravb_select_queue,
1771 .ndo_get_stats = ravb_get_stats,
1772 .ndo_set_rx_mode = ravb_set_rx_mode,
1773 .ndo_tx_timeout = ravb_tx_timeout,
1774 .ndo_do_ioctl = ravb_do_ioctl,
1775 .ndo_validate_addr = eth_validate_addr,
1776 .ndo_set_mac_address = eth_mac_addr,
1777 .ndo_change_mtu = eth_change_mtu,
1778};
1779
1780/* MDIO bus init function */
1781static int ravb_mdio_init(struct ravb_private *priv)
1782{
1783 struct platform_device *pdev = priv->pdev;
1784 struct device *dev = &pdev->dev;
1785 int error;
1786
1787 /* Bitbang init */
1788 priv->mdiobb.ops = &bb_ops;
1789
1790 /* MII controller setting */
1791 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1792 if (!priv->mii_bus)
1793 return -ENOMEM;
1794
1795 /* Hook up MII support for ethtool */
1796 priv->mii_bus->name = "ravb_mii";
1797 priv->mii_bus->parent = dev;
1798 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1799 pdev->name, pdev->id);
1800
1801 /* Register MDIO bus */
1802 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1803 if (error)
1804 goto out_free_bus;
1805
1806 return 0;
1807
1808out_free_bus:
1809 free_mdio_bitbang(priv->mii_bus);
1810 return error;
1811}
1812
1813/* MDIO bus release function */
1814static int ravb_mdio_release(struct ravb_private *priv)
1815{
1816 /* Unregister mdio bus */
1817 mdiobus_unregister(priv->mii_bus);
1818
1819 /* Free bitbang info */
1820 free_mdio_bitbang(priv->mii_bus);
1821
1822 return 0;
1823}
1824
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001825static const struct of_device_id ravb_match_table[] = {
1826 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1827 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001828 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001829 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001830 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001831 { }
1832};
1833MODULE_DEVICE_TABLE(of, ravb_match_table);
1834
Simon Hormanb3d39a82015-11-20 11:29:39 -08001835static int ravb_set_gti(struct net_device *ndev)
1836{
1837
1838 struct device *dev = ndev->dev.parent;
1839 struct device_node *np = dev->of_node;
1840 unsigned long rate;
1841 struct clk *clk;
1842 uint64_t inc;
1843
1844 clk = of_clk_get(np, 0);
1845 if (IS_ERR(clk)) {
1846 dev_err(dev, "could not get clock\n");
1847 return PTR_ERR(clk);
1848 }
1849
1850 rate = clk_get_rate(clk);
1851 clk_put(clk);
1852
1853 inc = 1000000000ULL << 20;
1854 do_div(inc, rate);
1855
1856 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1857 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1858 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1859 return -EINVAL;
1860 }
1861
1862 ravb_write(ndev, inc, GTI);
1863
1864 return 0;
1865}
1866
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001867static int ravb_probe(struct platform_device *pdev)
1868{
1869 struct device_node *np = pdev->dev.of_node;
1870 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001871 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001872 struct net_device *ndev;
1873 int error, irq, q;
1874 struct resource *res;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001875 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001876
1877 if (!np) {
1878 dev_err(&pdev->dev,
1879 "this driver is required to be instantiated from device tree\n");
1880 return -EINVAL;
1881 }
1882
1883 /* Get base address */
1884 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1885 if (!res) {
1886 dev_err(&pdev->dev, "invalid resource\n");
1887 return -EINVAL;
1888 }
1889
1890 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
1891 NUM_TX_QUEUE, NUM_RX_QUEUE);
1892 if (!ndev)
1893 return -ENOMEM;
1894
1895 pm_runtime_enable(&pdev->dev);
1896 pm_runtime_get_sync(&pdev->dev);
1897
1898 /* The Ether-specific entries in the device structure. */
1899 ndev->base_addr = res->start;
1900 ndev->dma = -1;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001901
Wolfram Sange8668632016-03-01 17:37:58 +01001902 chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001903
1904 if (chip_id == RCAR_GEN3)
1905 irq = platform_get_irq_byname(pdev, "ch22");
1906 else
1907 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001908 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03001909 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001910 goto out_release;
1911 }
1912 ndev->irq = irq;
1913
1914 SET_NETDEV_DEV(ndev, &pdev->dev);
1915
1916 priv = netdev_priv(ndev);
1917 priv->ndev = ndev;
1918 priv->pdev = pdev;
1919 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
1920 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
1921 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
1922 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
1923 priv->addr = devm_ioremap_resource(&pdev->dev, res);
1924 if (IS_ERR(priv->addr)) {
1925 error = PTR_ERR(priv->addr);
1926 goto out_release;
1927 }
1928
1929 spin_lock_init(&priv->lock);
1930 INIT_WORK(&priv->work, ravb_tx_timeout_work);
1931
1932 priv->phy_interface = of_get_phy_mode(np);
1933
1934 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
1935 priv->avb_link_active_low =
1936 of_property_read_bool(np, "renesas,ether-link-active-low");
1937
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001938 if (chip_id == RCAR_GEN3) {
1939 irq = platform_get_irq_byname(pdev, "ch24");
1940 if (irq < 0) {
1941 error = irq;
1942 goto out_release;
1943 }
1944 priv->emac_irq = irq;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001945 for (i = 0; i < NUM_RX_QUEUE; i++) {
1946 irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
1947 if (irq < 0) {
1948 error = irq;
1949 goto out_release;
1950 }
1951 priv->rx_irqs[i] = irq;
1952 }
1953 for (i = 0; i < NUM_TX_QUEUE; i++) {
1954 irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
1955 if (irq < 0) {
1956 error = irq;
1957 goto out_release;
1958 }
1959 priv->tx_irqs[i] = irq;
1960 }
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001961 }
1962
1963 priv->chip_id = chip_id;
1964
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001965 /* Set function */
1966 ndev->netdev_ops = &ravb_netdev_ops;
1967 ndev->ethtool_ops = &ravb_ethtool_ops;
1968
1969 /* Set AVB config mode */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001970 if (chip_id == RCAR_GEN2) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001971 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001972 /* Set CSEL value */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001973 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001974 } else {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001975 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
1976 CCC_GAC | CCC_CSEL_HPB);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001977 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001978
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001979 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08001980 error = ravb_set_gti(ndev);
1981 if (error)
1982 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001983
1984 /* Request GTI loading */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001985 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001986
1987 /* Allocate descriptor base address table */
1988 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001989 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001990 &priv->desc_bat_dma, GFP_KERNEL);
1991 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09001992 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001993 "Cannot allocate desc base address table (size %d bytes)\n",
1994 priv->desc_bat_size);
1995 error = -ENOMEM;
1996 goto out_release;
1997 }
1998 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
1999 priv->desc_bat[q].die_dt = DT_EOS;
2000 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2001
2002 /* Initialise HW timestamp list */
2003 INIT_LIST_HEAD(&priv->ts_skb_list);
2004
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002005 /* Initialise PTP Clock driver */
2006 if (chip_id != RCAR_GEN2)
2007 ravb_ptp_init(ndev, pdev);
2008
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002009 /* Debug message level */
2010 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2011
2012 /* Read and set MAC address */
2013 ravb_read_mac_address(ndev, of_get_mac_address(np));
2014 if (!is_valid_ether_addr(ndev->dev_addr)) {
2015 dev_warn(&pdev->dev,
2016 "no valid MAC address supplied, using a random one\n");
2017 eth_hw_addr_random(ndev);
2018 }
2019
2020 /* MDIO bus init */
2021 error = ravb_mdio_init(priv);
2022 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09002023 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002024 goto out_dma_free;
2025 }
2026
2027 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
2028 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
2029
2030 /* Network device register */
2031 error = register_netdev(ndev);
2032 if (error)
2033 goto out_napi_del;
2034
2035 /* Print device information */
2036 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2037 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2038
2039 platform_set_drvdata(pdev, ndev);
2040
2041 return 0;
2042
2043out_napi_del:
2044 netif_napi_del(&priv->napi[RAVB_NC]);
2045 netif_napi_del(&priv->napi[RAVB_BE]);
2046 ravb_mdio_release(priv);
2047out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002048 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002049 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002050
2051 /* Stop PTP Clock driver */
2052 if (chip_id != RCAR_GEN2)
2053 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002054out_release:
2055 if (ndev)
2056 free_netdev(ndev);
2057
2058 pm_runtime_put(&pdev->dev);
2059 pm_runtime_disable(&pdev->dev);
2060 return error;
2061}
2062
2063static int ravb_remove(struct platform_device *pdev)
2064{
2065 struct net_device *ndev = platform_get_drvdata(pdev);
2066 struct ravb_private *priv = netdev_priv(ndev);
2067
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002068 /* Stop PTP Clock driver */
2069 if (priv->chip_id != RCAR_GEN2)
2070 ravb_ptp_stop(ndev);
2071
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002072 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002073 priv->desc_bat_dma);
2074 /* Set reset mode */
2075 ravb_write(ndev, CCC_OPC_RESET, CCC);
2076 pm_runtime_put_sync(&pdev->dev);
2077 unregister_netdev(ndev);
2078 netif_napi_del(&priv->napi[RAVB_NC]);
2079 netif_napi_del(&priv->napi[RAVB_BE]);
2080 ravb_mdio_release(priv);
2081 pm_runtime_disable(&pdev->dev);
2082 free_netdev(ndev);
2083 platform_set_drvdata(pdev, NULL);
2084
2085 return 0;
2086}
2087
2088#ifdef CONFIG_PM
2089static int ravb_runtime_nop(struct device *dev)
2090{
2091 /* Runtime PM callback shared between ->runtime_suspend()
2092 * and ->runtime_resume(). Simply returns success.
2093 *
2094 * This driver re-initializes all registers after
2095 * pm_runtime_get_sync() anyway so there is no need
2096 * to save and restore registers here.
2097 */
2098 return 0;
2099}
2100
2101static const struct dev_pm_ops ravb_dev_pm_ops = {
2102 .runtime_suspend = ravb_runtime_nop,
2103 .runtime_resume = ravb_runtime_nop,
2104};
2105
2106#define RAVB_PM_OPS (&ravb_dev_pm_ops)
2107#else
2108#define RAVB_PM_OPS NULL
2109#endif
2110
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002111static struct platform_driver ravb_driver = {
2112 .probe = ravb_probe,
2113 .remove = ravb_remove,
2114 .driver = {
2115 .name = "ravb",
2116 .pm = RAVB_PM_OPS,
2117 .of_match_table = ravb_match_table,
2118 },
2119};
2120
2121module_platform_driver(ravb_driver);
2122
2123MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2124MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2125MODULE_LICENSE("GPL v2");