blob: 0f1b314f4d64ec509a053ad637a280261db26e1f [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 */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300810 if (iss & ISS_CGIS) {
811 ravb_ptp_interrupt(ndev);
Yoshihiro Kaneko38c848c2016-03-16 00:52:16 +0900812 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300813 }
Sergei Shtylyova0d2f202015-06-11 01:02:30 +0300814
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300815 mmiowb();
816 spin_unlock(&priv->lock);
817 return result;
818}
819
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900820/* Timestamp/Error/gPTP interrupt handler */
821static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
822{
823 struct net_device *ndev = dev_id;
824 struct ravb_private *priv = netdev_priv(ndev);
825 irqreturn_t result = IRQ_NONE;
826 u32 iss;
827
828 spin_lock(&priv->lock);
829 /* Get interrupt status */
830 iss = ravb_read(ndev, ISS);
831
832 /* Timestamp updated */
833 if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
834 result = IRQ_HANDLED;
835
836 /* Error status summary */
837 if (iss & ISS_ES) {
838 ravb_error_interrupt(ndev);
839 result = IRQ_HANDLED;
840 }
841
842 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300843 if (iss & ISS_CGIS) {
844 ravb_ptp_interrupt(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900845 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300846 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900847
848 mmiowb();
849 spin_unlock(&priv->lock);
850 return result;
851}
852
853static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
854{
855 struct net_device *ndev = dev_id;
856 struct ravb_private *priv = netdev_priv(ndev);
857 irqreturn_t result = IRQ_NONE;
858
859 spin_lock(&priv->lock);
860
861 /* Network control/Best effort queue RX/TX */
862 if (ravb_queue_interrupt(ndev, q))
863 result = IRQ_HANDLED;
864
865 mmiowb();
866 spin_unlock(&priv->lock);
867 return result;
868}
869
870static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
871{
872 return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
873}
874
875static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
876{
877 return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
878}
879
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300880static int ravb_poll(struct napi_struct *napi, int budget)
881{
882 struct net_device *ndev = napi->dev;
883 struct ravb_private *priv = netdev_priv(ndev);
884 unsigned long flags;
885 int q = napi - priv->napi;
886 int mask = BIT(q);
887 int quota = budget;
888 u32 ris0, tis;
889
890 for (;;) {
891 tis = ravb_read(ndev, TIS);
892 ris0 = ravb_read(ndev, RIS0);
893 if (!((ris0 & mask) || (tis & mask)))
894 break;
895
896 /* Processing RX Descriptor Ring */
897 if (ris0 & mask) {
898 /* Clear RX interrupt */
899 ravb_write(ndev, ~mask, RIS0);
900 if (ravb_rx(ndev, &quota, q))
901 goto out;
902 }
903 /* Processing TX Descriptor Ring */
904 if (tis & mask) {
905 spin_lock_irqsave(&priv->lock, flags);
906 /* Clear TX interrupt */
907 ravb_write(ndev, ~mask, TIS);
908 ravb_tx_free(ndev, q);
909 netif_wake_subqueue(ndev, q);
910 mmiowb();
911 spin_unlock_irqrestore(&priv->lock, flags);
912 }
913 }
914
915 napi_complete(napi);
916
917 /* Re-enable RX/TX interrupts */
918 spin_lock_irqsave(&priv->lock, flags);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900919 if (priv->chip_id == RCAR_GEN2) {
920 ravb_modify(ndev, RIC0, mask, mask);
921 ravb_modify(ndev, TIC, mask, mask);
922 } else {
923 ravb_write(ndev, mask, RIE0);
924 ravb_write(ndev, mask, TIE);
925 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300926 mmiowb();
927 spin_unlock_irqrestore(&priv->lock, flags);
928
929 /* Receive error message handling */
930 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
931 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
932 if (priv->rx_over_errors != ndev->stats.rx_over_errors) {
933 ndev->stats.rx_over_errors = priv->rx_over_errors;
934 netif_err(priv, rx_err, ndev, "Receive Descriptor Empty\n");
935 }
936 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors) {
937 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
938 netif_err(priv, rx_err, ndev, "Receive FIFO Overflow\n");
939 }
940out:
941 return budget - quota;
942}
943
944/* PHY state control function */
945static void ravb_adjust_link(struct net_device *ndev)
946{
947 struct ravb_private *priv = netdev_priv(ndev);
948 struct phy_device *phydev = priv->phydev;
949 bool new_state = false;
950
951 if (phydev->link) {
952 if (phydev->duplex != priv->duplex) {
953 new_state = true;
954 priv->duplex = phydev->duplex;
955 ravb_set_duplex(ndev);
956 }
957
958 if (phydev->speed != priv->speed) {
959 new_state = true;
960 priv->speed = phydev->speed;
961 ravb_set_rate(ndev);
962 }
963 if (!priv->link) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300964 ravb_modify(ndev, ECMR, ECMR_TXF, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300965 new_state = true;
966 priv->link = phydev->link;
967 if (priv->no_avb_link)
968 ravb_rcv_snd_enable(ndev);
969 }
970 } else if (priv->link) {
971 new_state = true;
972 priv->link = 0;
973 priv->speed = 0;
974 priv->duplex = -1;
975 if (priv->no_avb_link)
976 ravb_rcv_snd_disable(ndev);
977 }
978
979 if (new_state && netif_msg_link(priv))
980 phy_print_status(phydev);
981}
982
983/* PHY init function */
984static int ravb_phy_init(struct net_device *ndev)
985{
986 struct device_node *np = ndev->dev.parent->of_node;
987 struct ravb_private *priv = netdev_priv(ndev);
988 struct phy_device *phydev;
989 struct device_node *pn;
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900990 int err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300991
992 priv->link = 0;
993 priv->speed = 0;
994 priv->duplex = -1;
995
996 /* Try connecting to PHY */
997 pn = of_parse_phandle(np, "phy-handle", 0);
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900998 if (!pn) {
999 /* In the case of a fixed PHY, the DT node associated
1000 * to the PHY is the Ethernet MAC DT node.
1001 */
1002 if (of_phy_is_fixed_link(np)) {
1003 err = of_phy_register_fixed_link(np);
1004 if (err)
1005 return err;
1006 }
1007 pn = of_node_get(np);
1008 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001009 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
1010 priv->phy_interface);
1011 if (!phydev) {
1012 netdev_err(ndev, "failed to connect PHY\n");
1013 return -ENOENT;
1014 }
1015
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001016 /* This driver only support 10/100Mbit speeds on Gen3
1017 * at this time.
1018 */
1019 if (priv->chip_id == RCAR_GEN3) {
1020 int err;
1021
1022 err = phy_set_max_speed(phydev, SPEED_100);
1023 if (err) {
1024 netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
1025 phy_disconnect(phydev);
1026 return err;
1027 }
1028
1029 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
1030 }
1031
Kazuya Mizuguchi54499962015-12-14 00:15:58 +09001032 /* 10BASE is not supported */
1033 phydev->supported &= ~PHY_10BT_FEATURES;
1034
Andrew Lunn22209432016-01-06 20:11:13 +01001035 phy_attached_info(phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001036
1037 priv->phydev = phydev;
1038
1039 return 0;
1040}
1041
1042/* PHY control start function */
1043static int ravb_phy_start(struct net_device *ndev)
1044{
1045 struct ravb_private *priv = netdev_priv(ndev);
1046 int error;
1047
1048 error = ravb_phy_init(ndev);
1049 if (error)
1050 return error;
1051
1052 phy_start(priv->phydev);
1053
1054 return 0;
1055}
1056
1057static int ravb_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1058{
1059 struct ravb_private *priv = netdev_priv(ndev);
1060 int error = -ENODEV;
1061 unsigned long flags;
1062
1063 if (priv->phydev) {
1064 spin_lock_irqsave(&priv->lock, flags);
1065 error = phy_ethtool_gset(priv->phydev, ecmd);
1066 spin_unlock_irqrestore(&priv->lock, flags);
1067 }
1068
1069 return error;
1070}
1071
1072static int ravb_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1073{
1074 struct ravb_private *priv = netdev_priv(ndev);
1075 unsigned long flags;
1076 int error;
1077
1078 if (!priv->phydev)
1079 return -ENODEV;
1080
1081 spin_lock_irqsave(&priv->lock, flags);
1082
1083 /* Disable TX and RX */
1084 ravb_rcv_snd_disable(ndev);
1085
1086 error = phy_ethtool_sset(priv->phydev, ecmd);
1087 if (error)
1088 goto error_exit;
1089
1090 if (ecmd->duplex == DUPLEX_FULL)
1091 priv->duplex = 1;
1092 else
1093 priv->duplex = 0;
1094
1095 ravb_set_duplex(ndev);
1096
1097error_exit:
1098 mdelay(1);
1099
1100 /* Enable TX and RX */
1101 ravb_rcv_snd_enable(ndev);
1102
1103 mmiowb();
1104 spin_unlock_irqrestore(&priv->lock, flags);
1105
1106 return error;
1107}
1108
1109static int ravb_nway_reset(struct net_device *ndev)
1110{
1111 struct ravb_private *priv = netdev_priv(ndev);
1112 int error = -ENODEV;
1113 unsigned long flags;
1114
1115 if (priv->phydev) {
1116 spin_lock_irqsave(&priv->lock, flags);
1117 error = phy_start_aneg(priv->phydev);
1118 spin_unlock_irqrestore(&priv->lock, flags);
1119 }
1120
1121 return error;
1122}
1123
1124static u32 ravb_get_msglevel(struct net_device *ndev)
1125{
1126 struct ravb_private *priv = netdev_priv(ndev);
1127
1128 return priv->msg_enable;
1129}
1130
1131static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1132{
1133 struct ravb_private *priv = netdev_priv(ndev);
1134
1135 priv->msg_enable = value;
1136}
1137
1138static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1139 "rx_queue_0_current",
1140 "tx_queue_0_current",
1141 "rx_queue_0_dirty",
1142 "tx_queue_0_dirty",
1143 "rx_queue_0_packets",
1144 "tx_queue_0_packets",
1145 "rx_queue_0_bytes",
1146 "tx_queue_0_bytes",
1147 "rx_queue_0_mcast_packets",
1148 "rx_queue_0_errors",
1149 "rx_queue_0_crc_errors",
1150 "rx_queue_0_frame_errors",
1151 "rx_queue_0_length_errors",
1152 "rx_queue_0_missed_errors",
1153 "rx_queue_0_over_errors",
1154
1155 "rx_queue_1_current",
1156 "tx_queue_1_current",
1157 "rx_queue_1_dirty",
1158 "tx_queue_1_dirty",
1159 "rx_queue_1_packets",
1160 "tx_queue_1_packets",
1161 "rx_queue_1_bytes",
1162 "tx_queue_1_bytes",
1163 "rx_queue_1_mcast_packets",
1164 "rx_queue_1_errors",
1165 "rx_queue_1_crc_errors",
Sergei Shtylyovb17c1d92015-12-04 01:51:10 +03001166 "rx_queue_1_frame_errors",
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001167 "rx_queue_1_length_errors",
1168 "rx_queue_1_missed_errors",
1169 "rx_queue_1_over_errors",
1170};
1171
1172#define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1173
1174static int ravb_get_sset_count(struct net_device *netdev, int sset)
1175{
1176 switch (sset) {
1177 case ETH_SS_STATS:
1178 return RAVB_STATS_LEN;
1179 default:
1180 return -EOPNOTSUPP;
1181 }
1182}
1183
1184static void ravb_get_ethtool_stats(struct net_device *ndev,
1185 struct ethtool_stats *stats, u64 *data)
1186{
1187 struct ravb_private *priv = netdev_priv(ndev);
1188 int i = 0;
1189 int q;
1190
1191 /* Device-specific stats */
1192 for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1193 struct net_device_stats *stats = &priv->stats[q];
1194
1195 data[i++] = priv->cur_rx[q];
1196 data[i++] = priv->cur_tx[q];
1197 data[i++] = priv->dirty_rx[q];
1198 data[i++] = priv->dirty_tx[q];
1199 data[i++] = stats->rx_packets;
1200 data[i++] = stats->tx_packets;
1201 data[i++] = stats->rx_bytes;
1202 data[i++] = stats->tx_bytes;
1203 data[i++] = stats->multicast;
1204 data[i++] = stats->rx_errors;
1205 data[i++] = stats->rx_crc_errors;
1206 data[i++] = stats->rx_frame_errors;
1207 data[i++] = stats->rx_length_errors;
1208 data[i++] = stats->rx_missed_errors;
1209 data[i++] = stats->rx_over_errors;
1210 }
1211}
1212
1213static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1214{
1215 switch (stringset) {
1216 case ETH_SS_STATS:
1217 memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
1218 break;
1219 }
1220}
1221
1222static void ravb_get_ringparam(struct net_device *ndev,
1223 struct ethtool_ringparam *ring)
1224{
1225 struct ravb_private *priv = netdev_priv(ndev);
1226
1227 ring->rx_max_pending = BE_RX_RING_MAX;
1228 ring->tx_max_pending = BE_TX_RING_MAX;
1229 ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1230 ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1231}
1232
1233static int ravb_set_ringparam(struct net_device *ndev,
1234 struct ethtool_ringparam *ring)
1235{
1236 struct ravb_private *priv = netdev_priv(ndev);
1237 int error;
1238
1239 if (ring->tx_pending > BE_TX_RING_MAX ||
1240 ring->rx_pending > BE_RX_RING_MAX ||
1241 ring->tx_pending < BE_TX_RING_MIN ||
1242 ring->rx_pending < BE_RX_RING_MIN)
1243 return -EINVAL;
1244 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1245 return -EINVAL;
1246
1247 if (netif_running(ndev)) {
1248 netif_device_detach(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001249 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001250 if (priv->chip_id == RCAR_GEN2)
1251 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001252 /* Wait for DMA stopping */
1253 error = ravb_stop_dma(ndev);
1254 if (error) {
1255 netdev_err(ndev,
1256 "cannot set ringparam! Any AVB processes are still running?\n");
1257 return error;
1258 }
1259 synchronize_irq(ndev->irq);
1260
1261 /* Free all the skb's in the RX queue and the DMA buffers. */
1262 ravb_ring_free(ndev, RAVB_BE);
1263 ravb_ring_free(ndev, RAVB_NC);
1264 }
1265
1266 /* Set new parameters */
1267 priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1268 priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1269
1270 if (netif_running(ndev)) {
1271 error = ravb_dmac_init(ndev);
1272 if (error) {
1273 netdev_err(ndev,
1274 "%s: ravb_dmac_init() failed, error %d\n",
1275 __func__, error);
1276 return error;
1277 }
1278
1279 ravb_emac_init(ndev);
1280
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001281 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001282 if (priv->chip_id == RCAR_GEN2)
1283 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001284
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001285 netif_device_attach(ndev);
1286 }
1287
1288 return 0;
1289}
1290
1291static int ravb_get_ts_info(struct net_device *ndev,
1292 struct ethtool_ts_info *info)
1293{
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001294 struct ravb_private *priv = netdev_priv(ndev);
1295
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001296 info->so_timestamping =
1297 SOF_TIMESTAMPING_TX_SOFTWARE |
1298 SOF_TIMESTAMPING_RX_SOFTWARE |
1299 SOF_TIMESTAMPING_SOFTWARE |
1300 SOF_TIMESTAMPING_TX_HARDWARE |
1301 SOF_TIMESTAMPING_RX_HARDWARE |
1302 SOF_TIMESTAMPING_RAW_HARDWARE;
1303 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1304 info->rx_filters =
1305 (1 << HWTSTAMP_FILTER_NONE) |
1306 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1307 (1 << HWTSTAMP_FILTER_ALL);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001308 info->phc_index = ptp_clock_index(priv->ptp.clock);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001309
1310 return 0;
1311}
1312
1313static const struct ethtool_ops ravb_ethtool_ops = {
1314 .get_settings = ravb_get_settings,
1315 .set_settings = ravb_set_settings,
1316 .nway_reset = ravb_nway_reset,
1317 .get_msglevel = ravb_get_msglevel,
1318 .set_msglevel = ravb_set_msglevel,
1319 .get_link = ethtool_op_get_link,
1320 .get_strings = ravb_get_strings,
1321 .get_ethtool_stats = ravb_get_ethtool_stats,
1322 .get_sset_count = ravb_get_sset_count,
1323 .get_ringparam = ravb_get_ringparam,
1324 .set_ringparam = ravb_set_ringparam,
1325 .get_ts_info = ravb_get_ts_info,
1326};
1327
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001328static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1329 struct net_device *ndev, struct device *dev,
1330 const char *ch)
1331{
1332 char *name;
1333 int error;
1334
1335 name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1336 if (!name)
1337 return -ENOMEM;
1338 error = request_irq(irq, handler, 0, name, ndev);
1339 if (error)
1340 netdev_err(ndev, "cannot request IRQ %s\n", name);
1341
1342 return error;
1343}
1344
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001345/* Network device open function for Ethernet AVB */
1346static int ravb_open(struct net_device *ndev)
1347{
1348 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001349 struct platform_device *pdev = priv->pdev;
1350 struct device *dev = &pdev->dev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001351 int error;
1352
1353 napi_enable(&priv->napi[RAVB_BE]);
1354 napi_enable(&priv->napi[RAVB_NC]);
1355
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001356 if (priv->chip_id == RCAR_GEN2) {
1357 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1358 ndev->name, ndev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001359 if (error) {
1360 netdev_err(ndev, "cannot request IRQ\n");
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001361 goto out_napi_off;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001362 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001363 } else {
1364 error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1365 dev, "ch22:multi");
1366 if (error)
1367 goto out_napi_off;
1368 error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1369 dev, "ch24:emac");
1370 if (error)
1371 goto out_free_irq;
1372 error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1373 ndev, dev, "ch0:rx_be");
1374 if (error)
1375 goto out_free_irq_emac;
1376 error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1377 ndev, dev, "ch18:tx_be");
1378 if (error)
1379 goto out_free_irq_be_rx;
1380 error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1381 ndev, dev, "ch1:rx_nc");
1382 if (error)
1383 goto out_free_irq_be_tx;
1384 error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1385 ndev, dev, "ch19:tx_nc");
1386 if (error)
1387 goto out_free_irq_nc_rx;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001388 }
1389
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001390 /* Device init */
1391 error = ravb_dmac_init(ndev);
1392 if (error)
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001393 goto out_free_irq_nc_tx;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001394 ravb_emac_init(ndev);
1395
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001396 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001397 if (priv->chip_id == RCAR_GEN2)
1398 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001399
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001400 netif_tx_start_all_queues(ndev);
1401
1402 /* PHY control start */
1403 error = ravb_phy_start(ndev);
1404 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001405 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001406
1407 return 0;
1408
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001409out_ptp_stop:
1410 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001411 if (priv->chip_id == RCAR_GEN2)
1412 ravb_ptp_stop(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001413out_free_irq_nc_tx:
1414 if (priv->chip_id == RCAR_GEN2)
1415 goto out_free_irq;
1416 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1417out_free_irq_nc_rx:
1418 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1419out_free_irq_be_tx:
1420 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1421out_free_irq_be_rx:
1422 free_irq(priv->rx_irqs[RAVB_BE], ndev);
1423out_free_irq_emac:
1424 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001425out_free_irq:
1426 free_irq(ndev->irq, ndev);
1427out_napi_off:
1428 napi_disable(&priv->napi[RAVB_NC]);
1429 napi_disable(&priv->napi[RAVB_BE]);
1430 return error;
1431}
1432
1433/* Timeout function for Ethernet AVB */
1434static void ravb_tx_timeout(struct net_device *ndev)
1435{
1436 struct ravb_private *priv = netdev_priv(ndev);
1437
1438 netif_err(priv, tx_err, ndev,
1439 "transmit timed out, status %08x, resetting...\n",
1440 ravb_read(ndev, ISS));
1441
1442 /* tx_errors count up */
1443 ndev->stats.tx_errors++;
1444
1445 schedule_work(&priv->work);
1446}
1447
1448static void ravb_tx_timeout_work(struct work_struct *work)
1449{
1450 struct ravb_private *priv = container_of(work, struct ravb_private,
1451 work);
1452 struct net_device *ndev = priv->ndev;
1453
1454 netif_tx_stop_all_queues(ndev);
1455
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001456 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001457 if (priv->chip_id == RCAR_GEN2)
1458 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001459
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001460 /* Wait for DMA stopping */
1461 ravb_stop_dma(ndev);
1462
1463 ravb_ring_free(ndev, RAVB_BE);
1464 ravb_ring_free(ndev, RAVB_NC);
1465
1466 /* Device init */
1467 ravb_dmac_init(ndev);
1468 ravb_emac_init(ndev);
1469
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001470 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001471 if (priv->chip_id == RCAR_GEN2)
1472 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001473
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001474 netif_tx_start_all_queues(ndev);
1475}
1476
1477/* Packet transmit function for Ethernet AVB */
1478static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1479{
1480 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001481 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001482 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001483 struct ravb_tx_desc *desc;
1484 unsigned long flags;
1485 u32 dma_addr;
1486 void *buffer;
1487 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001488 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001489
1490 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001491 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1492 NUM_TX_DESC) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001493 netif_err(priv, tx_queued, ndev,
1494 "still transmitting with the full ring!\n");
1495 netif_stop_subqueue(ndev, q);
1496 spin_unlock_irqrestore(&priv->lock, flags);
1497 return NETDEV_TX_BUSY;
1498 }
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001499 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1500 priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001501
1502 if (skb_put_padto(skb, ETH_ZLEN))
1503 goto drop;
1504
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001505 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1506 entry / NUM_TX_DESC * DPTR_ALIGN;
1507 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
1508 memcpy(buffer, skb->data, len);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001509 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1510 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001511 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001512
1513 desc = &priv->tx_ring[q][entry];
1514 desc->ds_tagl = cpu_to_le16(len);
1515 desc->dptr = cpu_to_le32(dma_addr);
1516
1517 buffer = skb->data + len;
1518 len = skb->len - len;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001519 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1520 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001521 goto unmap;
1522
1523 desc++;
1524 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001525 desc->dptr = cpu_to_le32(dma_addr);
1526
1527 /* TX timestamp required */
1528 if (q == RAVB_NC) {
1529 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1530 if (!ts_skb) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001531 desc--;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001532 dma_unmap_single(ndev->dev.parent, dma_addr, len,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001533 DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001534 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001535 }
1536 ts_skb->skb = skb;
1537 ts_skb->tag = priv->ts_skb_tag++;
1538 priv->ts_skb_tag &= 0x3ff;
1539 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1540
1541 /* TAG and timestamp required flag */
1542 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001543 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1544 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1545 }
1546
Lino Sanfilippod7be81a2016-03-27 12:22:02 +02001547 skb_tx_timestamp(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001548 /* Descriptor type must be set after all the above writes */
1549 dma_wmb();
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001550 desc->die_dt = DT_FEND;
1551 desc--;
1552 desc->die_dt = DT_FSTART;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001553
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001554 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001555
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001556 priv->cur_tx[q] += NUM_TX_DESC;
1557 if (priv->cur_tx[q] - priv->dirty_tx[q] >
1558 (priv->num_tx_ring[q] - 1) * NUM_TX_DESC && !ravb_tx_free(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001559 netif_stop_subqueue(ndev, q);
1560
1561exit:
1562 mmiowb();
1563 spin_unlock_irqrestore(&priv->lock, flags);
1564 return NETDEV_TX_OK;
1565
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001566unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001567 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001568 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001569drop:
1570 dev_kfree_skb_any(skb);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001571 priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001572 goto exit;
1573}
1574
1575static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1576 void *accel_priv, select_queue_fallback_t fallback)
1577{
1578 /* If skb needs TX timestamp, it is handled in network control queue */
1579 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1580 RAVB_BE;
1581
1582}
1583
1584static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1585{
1586 struct ravb_private *priv = netdev_priv(ndev);
1587 struct net_device_stats *nstats, *stats0, *stats1;
1588
1589 nstats = &ndev->stats;
1590 stats0 = &priv->stats[RAVB_BE];
1591 stats1 = &priv->stats[RAVB_NC];
1592
1593 nstats->tx_dropped += ravb_read(ndev, TROCR);
1594 ravb_write(ndev, 0, TROCR); /* (write clear) */
1595 nstats->collisions += ravb_read(ndev, CDCR);
1596 ravb_write(ndev, 0, CDCR); /* (write clear) */
1597 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1598 ravb_write(ndev, 0, LCCR); /* (write clear) */
1599
1600 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1601 ravb_write(ndev, 0, CERCR); /* (write clear) */
1602 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1603 ravb_write(ndev, 0, CEECR); /* (write clear) */
1604
1605 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1606 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1607 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1608 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1609 nstats->multicast = stats0->multicast + stats1->multicast;
1610 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1611 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1612 nstats->rx_frame_errors =
1613 stats0->rx_frame_errors + stats1->rx_frame_errors;
1614 nstats->rx_length_errors =
1615 stats0->rx_length_errors + stats1->rx_length_errors;
1616 nstats->rx_missed_errors =
1617 stats0->rx_missed_errors + stats1->rx_missed_errors;
1618 nstats->rx_over_errors =
1619 stats0->rx_over_errors + stats1->rx_over_errors;
1620
1621 return nstats;
1622}
1623
1624/* Update promiscuous bit */
1625static void ravb_set_rx_mode(struct net_device *ndev)
1626{
1627 struct ravb_private *priv = netdev_priv(ndev);
1628 unsigned long flags;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001629
1630 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001631 ravb_modify(ndev, ECMR, ECMR_PRM,
1632 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001633 mmiowb();
1634 spin_unlock_irqrestore(&priv->lock, flags);
1635}
1636
1637/* Device close function for Ethernet AVB */
1638static int ravb_close(struct net_device *ndev)
1639{
1640 struct ravb_private *priv = netdev_priv(ndev);
1641 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1642
1643 netif_tx_stop_all_queues(ndev);
1644
1645 /* Disable interrupts by clearing the interrupt masks. */
1646 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001647 ravb_write(ndev, 0, RIC2);
1648 ravb_write(ndev, 0, TIC);
1649
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001650 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001651 if (priv->chip_id == RCAR_GEN2)
1652 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001653
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001654 /* Set the config mode to stop the AVB-DMAC's processes */
1655 if (ravb_stop_dma(ndev) < 0)
1656 netdev_err(ndev,
1657 "device will be stopped after h/w processes are done.\n");
1658
1659 /* Clear the timestamp list */
1660 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1661 list_del(&ts_skb->list);
1662 kfree(ts_skb);
1663 }
1664
1665 /* PHY disconnect */
1666 if (priv->phydev) {
1667 phy_stop(priv->phydev);
1668 phy_disconnect(priv->phydev);
1669 priv->phydev = NULL;
1670 }
1671
1672 free_irq(ndev->irq, ndev);
1673
1674 napi_disable(&priv->napi[RAVB_NC]);
1675 napi_disable(&priv->napi[RAVB_BE]);
1676
1677 /* Free all the skb's in the RX queue and the DMA buffers. */
1678 ravb_ring_free(ndev, RAVB_BE);
1679 ravb_ring_free(ndev, RAVB_NC);
1680
1681 return 0;
1682}
1683
1684static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1685{
1686 struct ravb_private *priv = netdev_priv(ndev);
1687 struct hwtstamp_config config;
1688
1689 config.flags = 0;
1690 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1691 HWTSTAMP_TX_OFF;
1692 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1693 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1694 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1695 config.rx_filter = HWTSTAMP_FILTER_ALL;
1696 else
1697 config.rx_filter = HWTSTAMP_FILTER_NONE;
1698
1699 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1700 -EFAULT : 0;
1701}
1702
1703/* Control hardware time stamping */
1704static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1705{
1706 struct ravb_private *priv = netdev_priv(ndev);
1707 struct hwtstamp_config config;
1708 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1709 u32 tstamp_tx_ctrl;
1710
1711 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1712 return -EFAULT;
1713
1714 /* Reserved for future extensions */
1715 if (config.flags)
1716 return -EINVAL;
1717
1718 switch (config.tx_type) {
1719 case HWTSTAMP_TX_OFF:
1720 tstamp_tx_ctrl = 0;
1721 break;
1722 case HWTSTAMP_TX_ON:
1723 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1724 break;
1725 default:
1726 return -ERANGE;
1727 }
1728
1729 switch (config.rx_filter) {
1730 case HWTSTAMP_FILTER_NONE:
1731 tstamp_rx_ctrl = 0;
1732 break;
1733 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1734 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1735 break;
1736 default:
1737 config.rx_filter = HWTSTAMP_FILTER_ALL;
1738 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1739 }
1740
1741 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1742 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1743
1744 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1745 -EFAULT : 0;
1746}
1747
1748/* ioctl to device function */
1749static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1750{
1751 struct ravb_private *priv = netdev_priv(ndev);
1752 struct phy_device *phydev = priv->phydev;
1753
1754 if (!netif_running(ndev))
1755 return -EINVAL;
1756
1757 if (!phydev)
1758 return -ENODEV;
1759
1760 switch (cmd) {
1761 case SIOCGHWTSTAMP:
1762 return ravb_hwtstamp_get(ndev, req);
1763 case SIOCSHWTSTAMP:
1764 return ravb_hwtstamp_set(ndev, req);
1765 }
1766
1767 return phy_mii_ioctl(phydev, req, cmd);
1768}
1769
1770static const struct net_device_ops ravb_netdev_ops = {
1771 .ndo_open = ravb_open,
1772 .ndo_stop = ravb_close,
1773 .ndo_start_xmit = ravb_start_xmit,
1774 .ndo_select_queue = ravb_select_queue,
1775 .ndo_get_stats = ravb_get_stats,
1776 .ndo_set_rx_mode = ravb_set_rx_mode,
1777 .ndo_tx_timeout = ravb_tx_timeout,
1778 .ndo_do_ioctl = ravb_do_ioctl,
1779 .ndo_validate_addr = eth_validate_addr,
1780 .ndo_set_mac_address = eth_mac_addr,
1781 .ndo_change_mtu = eth_change_mtu,
1782};
1783
1784/* MDIO bus init function */
1785static int ravb_mdio_init(struct ravb_private *priv)
1786{
1787 struct platform_device *pdev = priv->pdev;
1788 struct device *dev = &pdev->dev;
1789 int error;
1790
1791 /* Bitbang init */
1792 priv->mdiobb.ops = &bb_ops;
1793
1794 /* MII controller setting */
1795 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1796 if (!priv->mii_bus)
1797 return -ENOMEM;
1798
1799 /* Hook up MII support for ethtool */
1800 priv->mii_bus->name = "ravb_mii";
1801 priv->mii_bus->parent = dev;
1802 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1803 pdev->name, pdev->id);
1804
1805 /* Register MDIO bus */
1806 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1807 if (error)
1808 goto out_free_bus;
1809
1810 return 0;
1811
1812out_free_bus:
1813 free_mdio_bitbang(priv->mii_bus);
1814 return error;
1815}
1816
1817/* MDIO bus release function */
1818static int ravb_mdio_release(struct ravb_private *priv)
1819{
1820 /* Unregister mdio bus */
1821 mdiobus_unregister(priv->mii_bus);
1822
1823 /* Free bitbang info */
1824 free_mdio_bitbang(priv->mii_bus);
1825
1826 return 0;
1827}
1828
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001829static const struct of_device_id ravb_match_table[] = {
1830 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1831 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001832 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001833 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001834 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001835 { }
1836};
1837MODULE_DEVICE_TABLE(of, ravb_match_table);
1838
Simon Hormanb3d39a82015-11-20 11:29:39 -08001839static int ravb_set_gti(struct net_device *ndev)
1840{
1841
1842 struct device *dev = ndev->dev.parent;
1843 struct device_node *np = dev->of_node;
1844 unsigned long rate;
1845 struct clk *clk;
1846 uint64_t inc;
1847
1848 clk = of_clk_get(np, 0);
1849 if (IS_ERR(clk)) {
1850 dev_err(dev, "could not get clock\n");
1851 return PTR_ERR(clk);
1852 }
1853
1854 rate = clk_get_rate(clk);
1855 clk_put(clk);
1856
1857 inc = 1000000000ULL << 20;
1858 do_div(inc, rate);
1859
1860 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1861 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1862 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1863 return -EINVAL;
1864 }
1865
1866 ravb_write(ndev, inc, GTI);
1867
1868 return 0;
1869}
1870
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001871static int ravb_probe(struct platform_device *pdev)
1872{
1873 struct device_node *np = pdev->dev.of_node;
1874 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001875 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001876 struct net_device *ndev;
1877 int error, irq, q;
1878 struct resource *res;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001879 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001880
1881 if (!np) {
1882 dev_err(&pdev->dev,
1883 "this driver is required to be instantiated from device tree\n");
1884 return -EINVAL;
1885 }
1886
1887 /* Get base address */
1888 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1889 if (!res) {
1890 dev_err(&pdev->dev, "invalid resource\n");
1891 return -EINVAL;
1892 }
1893
1894 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
1895 NUM_TX_QUEUE, NUM_RX_QUEUE);
1896 if (!ndev)
1897 return -ENOMEM;
1898
1899 pm_runtime_enable(&pdev->dev);
1900 pm_runtime_get_sync(&pdev->dev);
1901
1902 /* The Ether-specific entries in the device structure. */
1903 ndev->base_addr = res->start;
1904 ndev->dma = -1;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001905
Wolfram Sange8668632016-03-01 17:37:58 +01001906 chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001907
1908 if (chip_id == RCAR_GEN3)
1909 irq = platform_get_irq_byname(pdev, "ch22");
1910 else
1911 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001912 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03001913 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001914 goto out_release;
1915 }
1916 ndev->irq = irq;
1917
1918 SET_NETDEV_DEV(ndev, &pdev->dev);
1919
1920 priv = netdev_priv(ndev);
1921 priv->ndev = ndev;
1922 priv->pdev = pdev;
1923 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
1924 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
1925 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
1926 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
1927 priv->addr = devm_ioremap_resource(&pdev->dev, res);
1928 if (IS_ERR(priv->addr)) {
1929 error = PTR_ERR(priv->addr);
1930 goto out_release;
1931 }
1932
1933 spin_lock_init(&priv->lock);
1934 INIT_WORK(&priv->work, ravb_tx_timeout_work);
1935
1936 priv->phy_interface = of_get_phy_mode(np);
1937
1938 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
1939 priv->avb_link_active_low =
1940 of_property_read_bool(np, "renesas,ether-link-active-low");
1941
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001942 if (chip_id == RCAR_GEN3) {
1943 irq = platform_get_irq_byname(pdev, "ch24");
1944 if (irq < 0) {
1945 error = irq;
1946 goto out_release;
1947 }
1948 priv->emac_irq = irq;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001949 for (i = 0; i < NUM_RX_QUEUE; i++) {
1950 irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
1951 if (irq < 0) {
1952 error = irq;
1953 goto out_release;
1954 }
1955 priv->rx_irqs[i] = irq;
1956 }
1957 for (i = 0; i < NUM_TX_QUEUE; i++) {
1958 irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
1959 if (irq < 0) {
1960 error = irq;
1961 goto out_release;
1962 }
1963 priv->tx_irqs[i] = irq;
1964 }
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001965 }
1966
1967 priv->chip_id = chip_id;
1968
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001969 /* Set function */
1970 ndev->netdev_ops = &ravb_netdev_ops;
1971 ndev->ethtool_ops = &ravb_ethtool_ops;
1972
1973 /* Set AVB config mode */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001974 if (chip_id == RCAR_GEN2) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001975 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001976 /* Set CSEL value */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001977 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001978 } else {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001979 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
1980 CCC_GAC | CCC_CSEL_HPB);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001981 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001982
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001983 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08001984 error = ravb_set_gti(ndev);
1985 if (error)
1986 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001987
1988 /* Request GTI loading */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001989 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001990
1991 /* Allocate descriptor base address table */
1992 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001993 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001994 &priv->desc_bat_dma, GFP_KERNEL);
1995 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09001996 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001997 "Cannot allocate desc base address table (size %d bytes)\n",
1998 priv->desc_bat_size);
1999 error = -ENOMEM;
2000 goto out_release;
2001 }
2002 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2003 priv->desc_bat[q].die_dt = DT_EOS;
2004 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2005
2006 /* Initialise HW timestamp list */
2007 INIT_LIST_HEAD(&priv->ts_skb_list);
2008
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002009 /* Initialise PTP Clock driver */
2010 if (chip_id != RCAR_GEN2)
2011 ravb_ptp_init(ndev, pdev);
2012
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002013 /* Debug message level */
2014 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2015
2016 /* Read and set MAC address */
2017 ravb_read_mac_address(ndev, of_get_mac_address(np));
2018 if (!is_valid_ether_addr(ndev->dev_addr)) {
2019 dev_warn(&pdev->dev,
2020 "no valid MAC address supplied, using a random one\n");
2021 eth_hw_addr_random(ndev);
2022 }
2023
2024 /* MDIO bus init */
2025 error = ravb_mdio_init(priv);
2026 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09002027 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002028 goto out_dma_free;
2029 }
2030
2031 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
2032 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
2033
2034 /* Network device register */
2035 error = register_netdev(ndev);
2036 if (error)
2037 goto out_napi_del;
2038
2039 /* Print device information */
2040 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2041 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2042
2043 platform_set_drvdata(pdev, ndev);
2044
2045 return 0;
2046
2047out_napi_del:
2048 netif_napi_del(&priv->napi[RAVB_NC]);
2049 netif_napi_del(&priv->napi[RAVB_BE]);
2050 ravb_mdio_release(priv);
2051out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002052 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002053 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002054
2055 /* Stop PTP Clock driver */
2056 if (chip_id != RCAR_GEN2)
2057 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002058out_release:
2059 if (ndev)
2060 free_netdev(ndev);
2061
2062 pm_runtime_put(&pdev->dev);
2063 pm_runtime_disable(&pdev->dev);
2064 return error;
2065}
2066
2067static int ravb_remove(struct platform_device *pdev)
2068{
2069 struct net_device *ndev = platform_get_drvdata(pdev);
2070 struct ravb_private *priv = netdev_priv(ndev);
2071
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002072 /* Stop PTP Clock driver */
2073 if (priv->chip_id != RCAR_GEN2)
2074 ravb_ptp_stop(ndev);
2075
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002076 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002077 priv->desc_bat_dma);
2078 /* Set reset mode */
2079 ravb_write(ndev, CCC_OPC_RESET, CCC);
2080 pm_runtime_put_sync(&pdev->dev);
2081 unregister_netdev(ndev);
2082 netif_napi_del(&priv->napi[RAVB_NC]);
2083 netif_napi_del(&priv->napi[RAVB_BE]);
2084 ravb_mdio_release(priv);
2085 pm_runtime_disable(&pdev->dev);
2086 free_netdev(ndev);
2087 platform_set_drvdata(pdev, NULL);
2088
2089 return 0;
2090}
2091
2092#ifdef CONFIG_PM
2093static int ravb_runtime_nop(struct device *dev)
2094{
2095 /* Runtime PM callback shared between ->runtime_suspend()
2096 * and ->runtime_resume(). Simply returns success.
2097 *
2098 * This driver re-initializes all registers after
2099 * pm_runtime_get_sync() anyway so there is no need
2100 * to save and restore registers here.
2101 */
2102 return 0;
2103}
2104
2105static const struct dev_pm_ops ravb_dev_pm_ops = {
2106 .runtime_suspend = ravb_runtime_nop,
2107 .runtime_resume = ravb_runtime_nop,
2108};
2109
2110#define RAVB_PM_OPS (&ravb_dev_pm_ops)
2111#else
2112#define RAVB_PM_OPS NULL
2113#endif
2114
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002115static struct platform_driver ravb_driver = {
2116 .probe = ravb_probe,
2117 .remove = ravb_remove,
2118 .driver = {
2119 .name = "ravb",
2120 .pm = RAVB_PM_OPS,
2121 .of_match_table = ravb_match_table,
2122 },
2123};
2124
2125module_platform_driver(ravb_driver);
2126
2127MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2128MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2129MODULE_LICENSE("GPL v2");