blob: 34066e0649f5c673b6d623c2d2056340a5c39801 [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];
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900249 rx_desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900250 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900251 PKT_BUF_SZ,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300252 DMA_FROM_DEVICE);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300253 /* We just set the data size to 0 for a failed mapping which
254 * should prevent DMA from happening...
255 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900256 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300257 rx_desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300258 rx_desc->dptr = cpu_to_le32(dma_addr);
259 rx_desc->die_dt = DT_FEMPTY;
260 }
261 rx_desc = &priv->rx_ring[q][i];
262 rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
263 rx_desc->die_dt = DT_LINKFIX; /* type */
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300264
265 memset(priv->tx_ring[q], 0, tx_ring_size);
266 /* Build TX ring buffer */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300267 for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
268 i++, tx_desc++) {
269 tx_desc->die_dt = DT_EEMPTY;
270 tx_desc++;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300271 tx_desc->die_dt = DT_EEMPTY;
272 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300273 tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
274 tx_desc->die_dt = DT_LINKFIX; /* type */
275
276 /* RX descriptor base address for best effort */
277 desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
278 desc->die_dt = DT_LINKFIX; /* type */
279 desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
280
281 /* TX descriptor base address for best effort */
282 desc = &priv->desc_bat[q];
283 desc->die_dt = DT_LINKFIX; /* type */
284 desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
285}
286
287/* Init skb and descriptor buffer for Ethernet AVB */
288static int ravb_ring_init(struct net_device *ndev, int q)
289{
290 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300291 struct sk_buff *skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300292 int ring_size;
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300293 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300294
295 /* Allocate RX and TX skb rings */
296 priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
297 sizeof(*priv->rx_skb[q]), GFP_KERNEL);
298 priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
299 sizeof(*priv->tx_skb[q]), GFP_KERNEL);
300 if (!priv->rx_skb[q] || !priv->tx_skb[q])
301 goto error;
302
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300303 for (i = 0; i < priv->num_rx_ring[q]; i++) {
304 skb = netdev_alloc_skb(ndev, PKT_BUF_SZ + RAVB_ALIGN - 1);
305 if (!skb)
306 goto error;
307 ravb_set_buffer_align(skb);
308 priv->rx_skb[q][i] = skb;
309 }
310
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300311 /* Allocate rings for the aligned buffers */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300312 priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
313 DPTR_ALIGN - 1, GFP_KERNEL);
314 if (!priv->tx_align[q])
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300315 goto error;
316
317 /* Allocate all RX descriptors. */
318 ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900319 priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300320 &priv->rx_desc_dma[q],
321 GFP_KERNEL);
322 if (!priv->rx_ring[q])
323 goto error;
324
325 priv->dirty_rx[q] = 0;
326
327 /* Allocate all TX descriptors. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300328 ring_size = sizeof(struct ravb_tx_desc) *
329 (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900330 priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300331 &priv->tx_desc_dma[q],
332 GFP_KERNEL);
333 if (!priv->tx_ring[q])
334 goto error;
335
336 return 0;
337
338error:
339 ravb_ring_free(ndev, q);
340
341 return -ENOMEM;
342}
343
344/* E-MAC init function */
345static void ravb_emac_init(struct net_device *ndev)
346{
347 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300348
349 /* Receive frame limit set register */
350 ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
351
352 /* PAUSE prohibition */
Sergei Shtylyov1c1fa822016-01-11 00:27:38 +0300353 ravb_write(ndev, ECMR_ZPF | (priv->duplex ? ECMR_DM : 0) |
354 ECMR_TE | ECMR_RE, ECMR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300355
356 ravb_set_rate(ndev);
357
358 /* Set MAC address */
359 ravb_write(ndev,
360 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
361 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
362 ravb_write(ndev,
363 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
364
365 ravb_write(ndev, 1, MPR);
366
367 /* E-MAC status register clear */
368 ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
369
370 /* E-MAC interrupt enable register */
371 ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
372}
373
374/* Device init function for Ethernet AVB */
375static int ravb_dmac_init(struct net_device *ndev)
376{
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900377 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300378 int error;
379
380 /* Set CONFIG mode */
381 error = ravb_config(ndev);
382 if (error)
383 return error;
384
385 error = ravb_ring_init(ndev, RAVB_BE);
386 if (error)
387 return error;
388 error = ravb_ring_init(ndev, RAVB_NC);
389 if (error) {
390 ravb_ring_free(ndev, RAVB_BE);
391 return error;
392 }
393
394 /* Descriptor format */
395 ravb_ring_format(ndev, RAVB_BE);
396 ravb_ring_format(ndev, RAVB_NC);
397
398#if defined(__LITTLE_ENDIAN)
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300399 ravb_modify(ndev, CCC, CCC_BOC, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300400#else
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300401 ravb_modify(ndev, CCC, CCC_BOC, CCC_BOC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300402#endif
403
404 /* Set AVB RX */
405 ravb_write(ndev, RCR_EFFS | RCR_ENCF | RCR_ETS0 | 0x18000000, RCR);
406
407 /* Set FIFO size */
408 ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00222200, TGC);
409
410 /* Timestamp enable */
411 ravb_write(ndev, TCCR_TFEN, TCCR);
412
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900413 /* Interrupt init: */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900414 if (priv->chip_id == RCAR_GEN3) {
415 /* Clear DIL.DPLx */
416 ravb_write(ndev, 0, DIL);
417 /* Set queue specific interrupt */
418 ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE);
419 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300420 /* Frame receive */
421 ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
Kazuya Mizuguchi6474de52015-12-15 01:24:58 +0900422 /* Disable FIFO full warning */
423 ravb_write(ndev, 0, RIC1);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300424 /* Receive FIFO full error, descriptor empty */
425 ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
426 /* Frame transmitted, timestamp FIFO updated */
427 ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
428
429 /* Setting the control will start the AVB-DMAC process. */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300430 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300431
432 return 0;
433}
434
435/* Free TX skb function for AVB-IP */
436static int ravb_tx_free(struct net_device *ndev, int q)
437{
438 struct ravb_private *priv = netdev_priv(ndev);
439 struct net_device_stats *stats = &priv->stats[q];
440 struct ravb_tx_desc *desc;
441 int free_num = 0;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300442 int entry;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300443 u32 size;
444
445 for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300446 entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
447 NUM_TX_DESC);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300448 desc = &priv->tx_ring[q][entry];
449 if (desc->die_dt != DT_FEMPTY)
450 break;
451 /* Descriptor type must be checked before all other reads */
452 dma_rmb();
453 size = le16_to_cpu(desc->ds_tagl) & TX_DS;
454 /* Free the original skb. */
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300455 if (priv->tx_skb[q][entry / NUM_TX_DESC]) {
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900456 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300457 size, DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +0300458 /* Last packet descriptor? */
459 if (entry % NUM_TX_DESC == NUM_TX_DESC - 1) {
460 entry /= NUM_TX_DESC;
461 dev_kfree_skb_any(priv->tx_skb[q][entry]);
462 priv->tx_skb[q][entry] = NULL;
463 stats->tx_packets++;
464 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300465 free_num++;
466 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300467 stats->tx_bytes += size;
468 desc->die_dt = DT_EEMPTY;
469 }
470 return free_num;
471}
472
473static void ravb_get_tx_tstamp(struct net_device *ndev)
474{
475 struct ravb_private *priv = netdev_priv(ndev);
476 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
477 struct skb_shared_hwtstamps shhwtstamps;
478 struct sk_buff *skb;
479 struct timespec64 ts;
480 u16 tag, tfa_tag;
481 int count;
482 u32 tfa2;
483
484 count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
485 while (count--) {
486 tfa2 = ravb_read(ndev, TFA2);
487 tfa_tag = (tfa2 & TFA2_TST) >> 16;
488 ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
489 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
490 ravb_read(ndev, TFA1);
491 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
492 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
493 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
494 list) {
495 skb = ts_skb->skb;
496 tag = ts_skb->tag;
497 list_del(&ts_skb->list);
498 kfree(ts_skb);
499 if (tag == tfa_tag) {
500 skb_tstamp_tx(skb, &shhwtstamps);
501 break;
502 }
503 }
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300504 ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300505 }
506}
507
508/* Packet receive function for Ethernet AVB */
509static bool ravb_rx(struct net_device *ndev, int *quota, int q)
510{
511 struct ravb_private *priv = netdev_priv(ndev);
512 int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
513 int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
514 priv->cur_rx[q];
515 struct net_device_stats *stats = &priv->stats[q];
516 struct ravb_ex_rx_desc *desc;
517 struct sk_buff *skb;
518 dma_addr_t dma_addr;
519 struct timespec64 ts;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300520 u8 desc_status;
Sergei Shtylyovaad0d512015-07-10 21:10:10 +0300521 u16 pkt_len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300522 int limit;
523
524 boguscnt = min(boguscnt, *quota);
525 limit = boguscnt;
526 desc = &priv->rx_ring[q][entry];
527 while (desc->die_dt != DT_FEMPTY) {
528 /* Descriptor type must be checked before all other reads */
529 dma_rmb();
530 desc_status = desc->msc;
531 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
532
533 if (--boguscnt < 0)
534 break;
535
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300536 /* We use 0-byte descriptors to mark the DMA mapping errors */
537 if (!pkt_len)
538 continue;
539
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300540 if (desc_status & MSC_MC)
541 stats->multicast++;
542
543 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
544 MSC_CEEF)) {
545 stats->rx_errors++;
546 if (desc_status & MSC_CRC)
547 stats->rx_crc_errors++;
548 if (desc_status & MSC_RFE)
549 stats->rx_frame_errors++;
550 if (desc_status & (MSC_RTLF | MSC_RTSF))
551 stats->rx_length_errors++;
552 if (desc_status & MSC_CEEF)
553 stats->rx_missed_errors++;
554 } else {
555 u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
556
557 skb = priv->rx_skb[q][entry];
558 priv->rx_skb[q][entry] = NULL;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900559 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900560 PKT_BUF_SZ,
Sergei Shtylyove2370f02015-07-15 00:56:52 +0300561 DMA_FROM_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300562 get_ts &= (q == RAVB_NC) ?
563 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
564 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
565 if (get_ts) {
566 struct skb_shared_hwtstamps *shhwtstamps;
567
568 shhwtstamps = skb_hwtstamps(skb);
569 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
570 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
571 32) | le32_to_cpu(desc->ts_sl);
572 ts.tv_nsec = le32_to_cpu(desc->ts_n);
573 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
574 }
575 skb_put(skb, pkt_len);
576 skb->protocol = eth_type_trans(skb, ndev);
577 napi_gro_receive(&priv->napi[q], skb);
578 stats->rx_packets++;
579 stats->rx_bytes += pkt_len;
580 }
581
582 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
583 desc = &priv->rx_ring[q][entry];
584 }
585
586 /* Refill the RX ring buffers. */
587 for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
588 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
589 desc = &priv->rx_ring[q][entry];
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900590 desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300591
592 if (!priv->rx_skb[q][entry]) {
593 skb = netdev_alloc_skb(ndev,
594 PKT_BUF_SZ + RAVB_ALIGN - 1);
595 if (!skb)
596 break; /* Better luck next round. */
597 ravb_set_buffer_align(skb);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900598 dma_addr = dma_map_single(ndev->dev.parent, skb->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300599 le16_to_cpu(desc->ds_cc),
600 DMA_FROM_DEVICE);
601 skb_checksum_none_assert(skb);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300602 /* We just set the data size to 0 for a failed mapping
603 * which should prevent DMA from happening...
604 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900605 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300606 desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300607 desc->dptr = cpu_to_le32(dma_addr);
608 priv->rx_skb[q][entry] = skb;
609 }
610 /* Descriptor type must be set after all the above writes */
611 dma_wmb();
612 desc->die_dt = DT_FEMPTY;
613 }
614
615 *quota -= limit - (++boguscnt);
616
617 return boguscnt <= 0;
618}
619
620static void ravb_rcv_snd_disable(struct net_device *ndev)
621{
622 /* Disable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300623 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300624}
625
626static void ravb_rcv_snd_enable(struct net_device *ndev)
627{
628 /* Enable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300629 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300630}
631
632/* function for waiting dma process finished */
633static int ravb_stop_dma(struct net_device *ndev)
634{
635 int error;
636
637 /* Wait for stopping the hardware TX process */
638 error = ravb_wait(ndev, TCCR,
639 TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
640 if (error)
641 return error;
642
643 error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
644 0);
645 if (error)
646 return error;
647
648 /* Stop the E-MAC's RX/TX processes. */
649 ravb_rcv_snd_disable(ndev);
650
651 /* Wait for stopping the RX DMA process */
652 error = ravb_wait(ndev, CSR, CSR_RPO, 0);
653 if (error)
654 return error;
655
656 /* Stop AVB-DMAC process */
657 return ravb_config(ndev);
658}
659
660/* E-MAC interrupt handler */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900661static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300662{
663 struct ravb_private *priv = netdev_priv(ndev);
664 u32 ecsr, psr;
665
666 ecsr = ravb_read(ndev, ECSR);
667 ravb_write(ndev, ecsr, ECSR); /* clear interrupt */
668 if (ecsr & ECSR_ICD)
669 ndev->stats.tx_carrier_errors++;
670 if (ecsr & ECSR_LCHNG) {
671 /* Link changed */
672 if (priv->no_avb_link)
673 return;
674 psr = ravb_read(ndev, PSR);
675 if (priv->avb_link_active_low)
676 psr ^= PSR_LMON;
677 if (!(psr & PSR_LMON)) {
678 /* DIsable RX and TX */
679 ravb_rcv_snd_disable(ndev);
680 } else {
681 /* Enable RX and TX */
682 ravb_rcv_snd_enable(ndev);
683 }
684 }
685}
686
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900687static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
688{
689 struct net_device *ndev = dev_id;
690 struct ravb_private *priv = netdev_priv(ndev);
691
692 spin_lock(&priv->lock);
693 ravb_emac_interrupt_unlocked(ndev);
694 mmiowb();
695 spin_unlock(&priv->lock);
696 return IRQ_HANDLED;
697}
698
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300699/* Error interrupt handler */
700static void ravb_error_interrupt(struct net_device *ndev)
701{
702 struct ravb_private *priv = netdev_priv(ndev);
703 u32 eis, ris2;
704
705 eis = ravb_read(ndev, EIS);
706 ravb_write(ndev, ~EIS_QFS, EIS);
707 if (eis & EIS_QFS) {
708 ris2 = ravb_read(ndev, RIS2);
709 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF), RIS2);
710
711 /* Receive Descriptor Empty int */
712 if (ris2 & RIS2_QFF0)
713 priv->stats[RAVB_BE].rx_over_errors++;
714
715 /* Receive Descriptor Empty int */
716 if (ris2 & RIS2_QFF1)
717 priv->stats[RAVB_NC].rx_over_errors++;
718
719 /* Receive FIFO Overflow int */
720 if (ris2 & RIS2_RFFF)
721 priv->rx_fifo_errors++;
722 }
723}
724
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900725static bool ravb_queue_interrupt(struct net_device *ndev, int q)
726{
727 struct ravb_private *priv = netdev_priv(ndev);
728 u32 ris0 = ravb_read(ndev, RIS0);
729 u32 ric0 = ravb_read(ndev, RIC0);
730 u32 tis = ravb_read(ndev, TIS);
731 u32 tic = ravb_read(ndev, TIC);
732
733 if (((ris0 & ric0) & BIT(q)) || ((tis & tic) & BIT(q))) {
734 if (napi_schedule_prep(&priv->napi[q])) {
735 /* Mask RX and TX interrupts */
736 if (priv->chip_id == RCAR_GEN2) {
737 ravb_write(ndev, ric0 & ~BIT(q), RIC0);
738 ravb_write(ndev, tic & ~BIT(q), TIC);
739 } else {
740 ravb_write(ndev, BIT(q), RID0);
741 ravb_write(ndev, BIT(q), TID);
742 }
743 __napi_schedule(&priv->napi[q]);
744 } else {
745 netdev_warn(ndev,
746 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
747 ris0, ric0);
748 netdev_warn(ndev,
749 " tx status 0x%08x, tx mask 0x%08x.\n",
750 tis, tic);
751 }
752 return true;
753 }
754 return false;
755}
756
757static bool ravb_timestamp_interrupt(struct net_device *ndev)
758{
759 u32 tis = ravb_read(ndev, TIS);
760
761 if (tis & TIS_TFUF) {
762 ravb_write(ndev, ~TIS_TFUF, TIS);
763 ravb_get_tx_tstamp(ndev);
764 return true;
765 }
766 return false;
767}
768
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300769static irqreturn_t ravb_interrupt(int irq, void *dev_id)
770{
771 struct net_device *ndev = dev_id;
772 struct ravb_private *priv = netdev_priv(ndev);
773 irqreturn_t result = IRQ_NONE;
774 u32 iss;
775
776 spin_lock(&priv->lock);
777 /* Get interrupt status */
778 iss = ravb_read(ndev, ISS);
779
780 /* Received and transmitted interrupts */
781 if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300782 int q;
783
784 /* Timestamp updated */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900785 if (ravb_timestamp_interrupt(ndev))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300786 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300787
788 /* Network control and best effort queue RX/TX */
789 for (q = RAVB_NC; q >= RAVB_BE; q--) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900790 if (ravb_queue_interrupt(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300791 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300792 }
793 }
794
795 /* E-MAC status summary */
796 if (iss & ISS_MS) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900797 ravb_emac_interrupt_unlocked(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300798 result = IRQ_HANDLED;
799 }
800
801 /* Error status summary */
802 if (iss & ISS_ES) {
803 ravb_error_interrupt(ndev);
804 result = IRQ_HANDLED;
805 }
806
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900807 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300808 if (iss & ISS_CGIS) {
809 ravb_ptp_interrupt(ndev);
Yoshihiro Kaneko38c848c2016-03-16 00:52:16 +0900810 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300811 }
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 */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300841 if (iss & ISS_CGIS) {
842 ravb_ptp_interrupt(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900843 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300844 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900845
846 mmiowb();
847 spin_unlock(&priv->lock);
848 return result;
849}
850
851static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
852{
853 struct net_device *ndev = dev_id;
854 struct ravb_private *priv = netdev_priv(ndev);
855 irqreturn_t result = IRQ_NONE;
856
857 spin_lock(&priv->lock);
858
859 /* Network control/Best effort queue RX/TX */
860 if (ravb_queue_interrupt(ndev, q))
861 result = IRQ_HANDLED;
862
863 mmiowb();
864 spin_unlock(&priv->lock);
865 return result;
866}
867
868static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
869{
870 return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
871}
872
873static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
874{
875 return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
876}
877
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300878static int ravb_poll(struct napi_struct *napi, int budget)
879{
880 struct net_device *ndev = napi->dev;
881 struct ravb_private *priv = netdev_priv(ndev);
882 unsigned long flags;
883 int q = napi - priv->napi;
884 int mask = BIT(q);
885 int quota = budget;
886 u32 ris0, tis;
887
888 for (;;) {
889 tis = ravb_read(ndev, TIS);
890 ris0 = ravb_read(ndev, RIS0);
891 if (!((ris0 & mask) || (tis & mask)))
892 break;
893
894 /* Processing RX Descriptor Ring */
895 if (ris0 & mask) {
896 /* Clear RX interrupt */
897 ravb_write(ndev, ~mask, RIS0);
898 if (ravb_rx(ndev, &quota, q))
899 goto out;
900 }
901 /* Processing TX Descriptor Ring */
902 if (tis & mask) {
903 spin_lock_irqsave(&priv->lock, flags);
904 /* Clear TX interrupt */
905 ravb_write(ndev, ~mask, TIS);
906 ravb_tx_free(ndev, q);
907 netif_wake_subqueue(ndev, q);
908 mmiowb();
909 spin_unlock_irqrestore(&priv->lock, flags);
910 }
911 }
912
913 napi_complete(napi);
914
915 /* Re-enable RX/TX interrupts */
916 spin_lock_irqsave(&priv->lock, flags);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900917 if (priv->chip_id == RCAR_GEN2) {
918 ravb_modify(ndev, RIC0, mask, mask);
919 ravb_modify(ndev, TIC, mask, mask);
920 } else {
921 ravb_write(ndev, mask, RIE0);
922 ravb_write(ndev, mask, TIE);
923 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300924 mmiowb();
925 spin_unlock_irqrestore(&priv->lock, flags);
926
927 /* Receive error message handling */
928 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
929 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
930 if (priv->rx_over_errors != ndev->stats.rx_over_errors) {
931 ndev->stats.rx_over_errors = priv->rx_over_errors;
932 netif_err(priv, rx_err, ndev, "Receive Descriptor Empty\n");
933 }
934 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors) {
935 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
936 netif_err(priv, rx_err, ndev, "Receive FIFO Overflow\n");
937 }
938out:
939 return budget - quota;
940}
941
942/* PHY state control function */
943static void ravb_adjust_link(struct net_device *ndev)
944{
945 struct ravb_private *priv = netdev_priv(ndev);
946 struct phy_device *phydev = priv->phydev;
947 bool new_state = false;
948
949 if (phydev->link) {
950 if (phydev->duplex != priv->duplex) {
951 new_state = true;
952 priv->duplex = phydev->duplex;
953 ravb_set_duplex(ndev);
954 }
955
956 if (phydev->speed != priv->speed) {
957 new_state = true;
958 priv->speed = phydev->speed;
959 ravb_set_rate(ndev);
960 }
961 if (!priv->link) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300962 ravb_modify(ndev, ECMR, ECMR_TXF, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300963 new_state = true;
964 priv->link = phydev->link;
965 if (priv->no_avb_link)
966 ravb_rcv_snd_enable(ndev);
967 }
968 } else if (priv->link) {
969 new_state = true;
970 priv->link = 0;
971 priv->speed = 0;
972 priv->duplex = -1;
973 if (priv->no_avb_link)
974 ravb_rcv_snd_disable(ndev);
975 }
976
977 if (new_state && netif_msg_link(priv))
978 phy_print_status(phydev);
979}
980
981/* PHY init function */
982static int ravb_phy_init(struct net_device *ndev)
983{
984 struct device_node *np = ndev->dev.parent->of_node;
985 struct ravb_private *priv = netdev_priv(ndev);
986 struct phy_device *phydev;
987 struct device_node *pn;
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900988 int err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300989
990 priv->link = 0;
991 priv->speed = 0;
992 priv->duplex = -1;
993
994 /* Try connecting to PHY */
995 pn = of_parse_phandle(np, "phy-handle", 0);
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900996 if (!pn) {
997 /* In the case of a fixed PHY, the DT node associated
998 * to the PHY is the Ethernet MAC DT node.
999 */
1000 if (of_phy_is_fixed_link(np)) {
1001 err = of_phy_register_fixed_link(np);
1002 if (err)
1003 return err;
1004 }
1005 pn = of_node_get(np);
1006 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001007 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
1008 priv->phy_interface);
1009 if (!phydev) {
1010 netdev_err(ndev, "failed to connect PHY\n");
1011 return -ENOENT;
1012 }
1013
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001014 /* This driver only support 10/100Mbit speeds on Gen3
1015 * at this time.
1016 */
1017 if (priv->chip_id == RCAR_GEN3) {
1018 int err;
1019
1020 err = phy_set_max_speed(phydev, SPEED_100);
1021 if (err) {
1022 netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
1023 phy_disconnect(phydev);
1024 return err;
1025 }
1026
1027 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
1028 }
1029
Kazuya Mizuguchi54499962015-12-14 00:15:58 +09001030 /* 10BASE is not supported */
1031 phydev->supported &= ~PHY_10BT_FEATURES;
1032
Andrew Lunn22209432016-01-06 20:11:13 +01001033 phy_attached_info(phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001034
1035 priv->phydev = phydev;
1036
1037 return 0;
1038}
1039
1040/* PHY control start function */
1041static int ravb_phy_start(struct net_device *ndev)
1042{
1043 struct ravb_private *priv = netdev_priv(ndev);
1044 int error;
1045
1046 error = ravb_phy_init(ndev);
1047 if (error)
1048 return error;
1049
1050 phy_start(priv->phydev);
1051
1052 return 0;
1053}
1054
1055static int ravb_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1056{
1057 struct ravb_private *priv = netdev_priv(ndev);
1058 int error = -ENODEV;
1059 unsigned long flags;
1060
1061 if (priv->phydev) {
1062 spin_lock_irqsave(&priv->lock, flags);
1063 error = phy_ethtool_gset(priv->phydev, ecmd);
1064 spin_unlock_irqrestore(&priv->lock, flags);
1065 }
1066
1067 return error;
1068}
1069
1070static int ravb_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1071{
1072 struct ravb_private *priv = netdev_priv(ndev);
1073 unsigned long flags;
1074 int error;
1075
1076 if (!priv->phydev)
1077 return -ENODEV;
1078
1079 spin_lock_irqsave(&priv->lock, flags);
1080
1081 /* Disable TX and RX */
1082 ravb_rcv_snd_disable(ndev);
1083
1084 error = phy_ethtool_sset(priv->phydev, ecmd);
1085 if (error)
1086 goto error_exit;
1087
1088 if (ecmd->duplex == DUPLEX_FULL)
1089 priv->duplex = 1;
1090 else
1091 priv->duplex = 0;
1092
1093 ravb_set_duplex(ndev);
1094
1095error_exit:
1096 mdelay(1);
1097
1098 /* Enable TX and RX */
1099 ravb_rcv_snd_enable(ndev);
1100
1101 mmiowb();
1102 spin_unlock_irqrestore(&priv->lock, flags);
1103
1104 return error;
1105}
1106
1107static int ravb_nway_reset(struct net_device *ndev)
1108{
1109 struct ravb_private *priv = netdev_priv(ndev);
1110 int error = -ENODEV;
1111 unsigned long flags;
1112
1113 if (priv->phydev) {
1114 spin_lock_irqsave(&priv->lock, flags);
1115 error = phy_start_aneg(priv->phydev);
1116 spin_unlock_irqrestore(&priv->lock, flags);
1117 }
1118
1119 return error;
1120}
1121
1122static u32 ravb_get_msglevel(struct net_device *ndev)
1123{
1124 struct ravb_private *priv = netdev_priv(ndev);
1125
1126 return priv->msg_enable;
1127}
1128
1129static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1130{
1131 struct ravb_private *priv = netdev_priv(ndev);
1132
1133 priv->msg_enable = value;
1134}
1135
1136static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1137 "rx_queue_0_current",
1138 "tx_queue_0_current",
1139 "rx_queue_0_dirty",
1140 "tx_queue_0_dirty",
1141 "rx_queue_0_packets",
1142 "tx_queue_0_packets",
1143 "rx_queue_0_bytes",
1144 "tx_queue_0_bytes",
1145 "rx_queue_0_mcast_packets",
1146 "rx_queue_0_errors",
1147 "rx_queue_0_crc_errors",
1148 "rx_queue_0_frame_errors",
1149 "rx_queue_0_length_errors",
1150 "rx_queue_0_missed_errors",
1151 "rx_queue_0_over_errors",
1152
1153 "rx_queue_1_current",
1154 "tx_queue_1_current",
1155 "rx_queue_1_dirty",
1156 "tx_queue_1_dirty",
1157 "rx_queue_1_packets",
1158 "tx_queue_1_packets",
1159 "rx_queue_1_bytes",
1160 "tx_queue_1_bytes",
1161 "rx_queue_1_mcast_packets",
1162 "rx_queue_1_errors",
1163 "rx_queue_1_crc_errors",
Sergei Shtylyovb17c1d92015-12-04 01:51:10 +03001164 "rx_queue_1_frame_errors",
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001165 "rx_queue_1_length_errors",
1166 "rx_queue_1_missed_errors",
1167 "rx_queue_1_over_errors",
1168};
1169
1170#define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1171
1172static int ravb_get_sset_count(struct net_device *netdev, int sset)
1173{
1174 switch (sset) {
1175 case ETH_SS_STATS:
1176 return RAVB_STATS_LEN;
1177 default:
1178 return -EOPNOTSUPP;
1179 }
1180}
1181
1182static void ravb_get_ethtool_stats(struct net_device *ndev,
1183 struct ethtool_stats *stats, u64 *data)
1184{
1185 struct ravb_private *priv = netdev_priv(ndev);
1186 int i = 0;
1187 int q;
1188
1189 /* Device-specific stats */
1190 for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1191 struct net_device_stats *stats = &priv->stats[q];
1192
1193 data[i++] = priv->cur_rx[q];
1194 data[i++] = priv->cur_tx[q];
1195 data[i++] = priv->dirty_rx[q];
1196 data[i++] = priv->dirty_tx[q];
1197 data[i++] = stats->rx_packets;
1198 data[i++] = stats->tx_packets;
1199 data[i++] = stats->rx_bytes;
1200 data[i++] = stats->tx_bytes;
1201 data[i++] = stats->multicast;
1202 data[i++] = stats->rx_errors;
1203 data[i++] = stats->rx_crc_errors;
1204 data[i++] = stats->rx_frame_errors;
1205 data[i++] = stats->rx_length_errors;
1206 data[i++] = stats->rx_missed_errors;
1207 data[i++] = stats->rx_over_errors;
1208 }
1209}
1210
1211static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1212{
1213 switch (stringset) {
1214 case ETH_SS_STATS:
1215 memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
1216 break;
1217 }
1218}
1219
1220static void ravb_get_ringparam(struct net_device *ndev,
1221 struct ethtool_ringparam *ring)
1222{
1223 struct ravb_private *priv = netdev_priv(ndev);
1224
1225 ring->rx_max_pending = BE_RX_RING_MAX;
1226 ring->tx_max_pending = BE_TX_RING_MAX;
1227 ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1228 ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1229}
1230
1231static int ravb_set_ringparam(struct net_device *ndev,
1232 struct ethtool_ringparam *ring)
1233{
1234 struct ravb_private *priv = netdev_priv(ndev);
1235 int error;
1236
1237 if (ring->tx_pending > BE_TX_RING_MAX ||
1238 ring->rx_pending > BE_RX_RING_MAX ||
1239 ring->tx_pending < BE_TX_RING_MIN ||
1240 ring->rx_pending < BE_RX_RING_MIN)
1241 return -EINVAL;
1242 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1243 return -EINVAL;
1244
1245 if (netif_running(ndev)) {
1246 netif_device_detach(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001247 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001248 if (priv->chip_id == RCAR_GEN2)
1249 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001250 /* Wait for DMA stopping */
1251 error = ravb_stop_dma(ndev);
1252 if (error) {
1253 netdev_err(ndev,
1254 "cannot set ringparam! Any AVB processes are still running?\n");
1255 return error;
1256 }
1257 synchronize_irq(ndev->irq);
1258
1259 /* Free all the skb's in the RX queue and the DMA buffers. */
1260 ravb_ring_free(ndev, RAVB_BE);
1261 ravb_ring_free(ndev, RAVB_NC);
1262 }
1263
1264 /* Set new parameters */
1265 priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1266 priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1267
1268 if (netif_running(ndev)) {
1269 error = ravb_dmac_init(ndev);
1270 if (error) {
1271 netdev_err(ndev,
1272 "%s: ravb_dmac_init() failed, error %d\n",
1273 __func__, error);
1274 return error;
1275 }
1276
1277 ravb_emac_init(ndev);
1278
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001279 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001280 if (priv->chip_id == RCAR_GEN2)
1281 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001282
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001283 netif_device_attach(ndev);
1284 }
1285
1286 return 0;
1287}
1288
1289static int ravb_get_ts_info(struct net_device *ndev,
1290 struct ethtool_ts_info *info)
1291{
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001292 struct ravb_private *priv = netdev_priv(ndev);
1293
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001294 info->so_timestamping =
1295 SOF_TIMESTAMPING_TX_SOFTWARE |
1296 SOF_TIMESTAMPING_RX_SOFTWARE |
1297 SOF_TIMESTAMPING_SOFTWARE |
1298 SOF_TIMESTAMPING_TX_HARDWARE |
1299 SOF_TIMESTAMPING_RX_HARDWARE |
1300 SOF_TIMESTAMPING_RAW_HARDWARE;
1301 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1302 info->rx_filters =
1303 (1 << HWTSTAMP_FILTER_NONE) |
1304 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1305 (1 << HWTSTAMP_FILTER_ALL);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001306 info->phc_index = ptp_clock_index(priv->ptp.clock);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001307
1308 return 0;
1309}
1310
1311static const struct ethtool_ops ravb_ethtool_ops = {
1312 .get_settings = ravb_get_settings,
1313 .set_settings = ravb_set_settings,
1314 .nway_reset = ravb_nway_reset,
1315 .get_msglevel = ravb_get_msglevel,
1316 .set_msglevel = ravb_set_msglevel,
1317 .get_link = ethtool_op_get_link,
1318 .get_strings = ravb_get_strings,
1319 .get_ethtool_stats = ravb_get_ethtool_stats,
1320 .get_sset_count = ravb_get_sset_count,
1321 .get_ringparam = ravb_get_ringparam,
1322 .set_ringparam = ravb_set_ringparam,
1323 .get_ts_info = ravb_get_ts_info,
1324};
1325
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001326static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1327 struct net_device *ndev, struct device *dev,
1328 const char *ch)
1329{
1330 char *name;
1331 int error;
1332
1333 name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1334 if (!name)
1335 return -ENOMEM;
1336 error = request_irq(irq, handler, 0, name, ndev);
1337 if (error)
1338 netdev_err(ndev, "cannot request IRQ %s\n", name);
1339
1340 return error;
1341}
1342
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001343/* Network device open function for Ethernet AVB */
1344static int ravb_open(struct net_device *ndev)
1345{
1346 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001347 struct platform_device *pdev = priv->pdev;
1348 struct device *dev = &pdev->dev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001349 int error;
1350
1351 napi_enable(&priv->napi[RAVB_BE]);
1352 napi_enable(&priv->napi[RAVB_NC]);
1353
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001354 if (priv->chip_id == RCAR_GEN2) {
1355 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1356 ndev->name, ndev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001357 if (error) {
1358 netdev_err(ndev, "cannot request IRQ\n");
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001359 goto out_napi_off;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001360 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001361 } else {
1362 error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1363 dev, "ch22:multi");
1364 if (error)
1365 goto out_napi_off;
1366 error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1367 dev, "ch24:emac");
1368 if (error)
1369 goto out_free_irq;
1370 error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1371 ndev, dev, "ch0:rx_be");
1372 if (error)
1373 goto out_free_irq_emac;
1374 error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1375 ndev, dev, "ch18:tx_be");
1376 if (error)
1377 goto out_free_irq_be_rx;
1378 error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1379 ndev, dev, "ch1:rx_nc");
1380 if (error)
1381 goto out_free_irq_be_tx;
1382 error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1383 ndev, dev, "ch19:tx_nc");
1384 if (error)
1385 goto out_free_irq_nc_rx;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001386 }
1387
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001388 /* Device init */
1389 error = ravb_dmac_init(ndev);
1390 if (error)
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001391 goto out_free_irq_nc_tx;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001392 ravb_emac_init(ndev);
1393
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001394 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001395 if (priv->chip_id == RCAR_GEN2)
1396 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001397
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001398 netif_tx_start_all_queues(ndev);
1399
1400 /* PHY control start */
1401 error = ravb_phy_start(ndev);
1402 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001403 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001404
1405 return 0;
1406
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001407out_ptp_stop:
1408 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001409 if (priv->chip_id == RCAR_GEN2)
1410 ravb_ptp_stop(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001411out_free_irq_nc_tx:
1412 if (priv->chip_id == RCAR_GEN2)
1413 goto out_free_irq;
1414 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1415out_free_irq_nc_rx:
1416 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1417out_free_irq_be_tx:
1418 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1419out_free_irq_be_rx:
1420 free_irq(priv->rx_irqs[RAVB_BE], ndev);
1421out_free_irq_emac:
1422 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001423out_free_irq:
1424 free_irq(ndev->irq, ndev);
1425out_napi_off:
1426 napi_disable(&priv->napi[RAVB_NC]);
1427 napi_disable(&priv->napi[RAVB_BE]);
1428 return error;
1429}
1430
1431/* Timeout function for Ethernet AVB */
1432static void ravb_tx_timeout(struct net_device *ndev)
1433{
1434 struct ravb_private *priv = netdev_priv(ndev);
1435
1436 netif_err(priv, tx_err, ndev,
1437 "transmit timed out, status %08x, resetting...\n",
1438 ravb_read(ndev, ISS));
1439
1440 /* tx_errors count up */
1441 ndev->stats.tx_errors++;
1442
1443 schedule_work(&priv->work);
1444}
1445
1446static void ravb_tx_timeout_work(struct work_struct *work)
1447{
1448 struct ravb_private *priv = container_of(work, struct ravb_private,
1449 work);
1450 struct net_device *ndev = priv->ndev;
1451
1452 netif_tx_stop_all_queues(ndev);
1453
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001454 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001455 if (priv->chip_id == RCAR_GEN2)
1456 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001457
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001458 /* Wait for DMA stopping */
1459 ravb_stop_dma(ndev);
1460
1461 ravb_ring_free(ndev, RAVB_BE);
1462 ravb_ring_free(ndev, RAVB_NC);
1463
1464 /* Device init */
1465 ravb_dmac_init(ndev);
1466 ravb_emac_init(ndev);
1467
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001468 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001469 if (priv->chip_id == RCAR_GEN2)
1470 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001471
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001472 netif_tx_start_all_queues(ndev);
1473}
1474
1475/* Packet transmit function for Ethernet AVB */
1476static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1477{
1478 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001479 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001480 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001481 struct ravb_tx_desc *desc;
1482 unsigned long flags;
1483 u32 dma_addr;
1484 void *buffer;
1485 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001486 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001487
1488 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001489 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1490 NUM_TX_DESC) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001491 netif_err(priv, tx_queued, ndev,
1492 "still transmitting with the full ring!\n");
1493 netif_stop_subqueue(ndev, q);
1494 spin_unlock_irqrestore(&priv->lock, flags);
1495 return NETDEV_TX_BUSY;
1496 }
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001497 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1498 priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001499
1500 if (skb_put_padto(skb, ETH_ZLEN))
1501 goto drop;
1502
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001503 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1504 entry / NUM_TX_DESC * DPTR_ALIGN;
1505 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
1506 memcpy(buffer, skb->data, len);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001507 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1508 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001509 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001510
1511 desc = &priv->tx_ring[q][entry];
1512 desc->ds_tagl = cpu_to_le16(len);
1513 desc->dptr = cpu_to_le32(dma_addr);
1514
1515 buffer = skb->data + len;
1516 len = skb->len - len;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001517 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1518 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001519 goto unmap;
1520
1521 desc++;
1522 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001523 desc->dptr = cpu_to_le32(dma_addr);
1524
1525 /* TX timestamp required */
1526 if (q == RAVB_NC) {
1527 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1528 if (!ts_skb) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001529 desc--;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001530 dma_unmap_single(ndev->dev.parent, dma_addr, len,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001531 DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001532 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001533 }
1534 ts_skb->skb = skb;
1535 ts_skb->tag = priv->ts_skb_tag++;
1536 priv->ts_skb_tag &= 0x3ff;
1537 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1538
1539 /* TAG and timestamp required flag */
1540 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001541 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1542 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1543 }
1544
Lino Sanfilippod7be81a2016-03-27 12:22:02 +02001545 skb_tx_timestamp(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001546 /* Descriptor type must be set after all the above writes */
1547 dma_wmb();
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001548 desc->die_dt = DT_FEND;
1549 desc--;
1550 desc->die_dt = DT_FSTART;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001551
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001552 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001553
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001554 priv->cur_tx[q] += NUM_TX_DESC;
1555 if (priv->cur_tx[q] - priv->dirty_tx[q] >
1556 (priv->num_tx_ring[q] - 1) * NUM_TX_DESC && !ravb_tx_free(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001557 netif_stop_subqueue(ndev, q);
1558
1559exit:
1560 mmiowb();
1561 spin_unlock_irqrestore(&priv->lock, flags);
1562 return NETDEV_TX_OK;
1563
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001564unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001565 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001566 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001567drop:
1568 dev_kfree_skb_any(skb);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001569 priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001570 goto exit;
1571}
1572
1573static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1574 void *accel_priv, select_queue_fallback_t fallback)
1575{
1576 /* If skb needs TX timestamp, it is handled in network control queue */
1577 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1578 RAVB_BE;
1579
1580}
1581
1582static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1583{
1584 struct ravb_private *priv = netdev_priv(ndev);
1585 struct net_device_stats *nstats, *stats0, *stats1;
1586
1587 nstats = &ndev->stats;
1588 stats0 = &priv->stats[RAVB_BE];
1589 stats1 = &priv->stats[RAVB_NC];
1590
1591 nstats->tx_dropped += ravb_read(ndev, TROCR);
1592 ravb_write(ndev, 0, TROCR); /* (write clear) */
1593 nstats->collisions += ravb_read(ndev, CDCR);
1594 ravb_write(ndev, 0, CDCR); /* (write clear) */
1595 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1596 ravb_write(ndev, 0, LCCR); /* (write clear) */
1597
1598 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1599 ravb_write(ndev, 0, CERCR); /* (write clear) */
1600 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1601 ravb_write(ndev, 0, CEECR); /* (write clear) */
1602
1603 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1604 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1605 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1606 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1607 nstats->multicast = stats0->multicast + stats1->multicast;
1608 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1609 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1610 nstats->rx_frame_errors =
1611 stats0->rx_frame_errors + stats1->rx_frame_errors;
1612 nstats->rx_length_errors =
1613 stats0->rx_length_errors + stats1->rx_length_errors;
1614 nstats->rx_missed_errors =
1615 stats0->rx_missed_errors + stats1->rx_missed_errors;
1616 nstats->rx_over_errors =
1617 stats0->rx_over_errors + stats1->rx_over_errors;
1618
1619 return nstats;
1620}
1621
1622/* Update promiscuous bit */
1623static void ravb_set_rx_mode(struct net_device *ndev)
1624{
1625 struct ravb_private *priv = netdev_priv(ndev);
1626 unsigned long flags;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001627
1628 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001629 ravb_modify(ndev, ECMR, ECMR_PRM,
1630 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001631 mmiowb();
1632 spin_unlock_irqrestore(&priv->lock, flags);
1633}
1634
1635/* Device close function for Ethernet AVB */
1636static int ravb_close(struct net_device *ndev)
1637{
1638 struct ravb_private *priv = netdev_priv(ndev);
1639 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1640
1641 netif_tx_stop_all_queues(ndev);
1642
1643 /* Disable interrupts by clearing the interrupt masks. */
1644 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001645 ravb_write(ndev, 0, RIC2);
1646 ravb_write(ndev, 0, TIC);
1647
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001648 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001649 if (priv->chip_id == RCAR_GEN2)
1650 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001651
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001652 /* Set the config mode to stop the AVB-DMAC's processes */
1653 if (ravb_stop_dma(ndev) < 0)
1654 netdev_err(ndev,
1655 "device will be stopped after h/w processes are done.\n");
1656
1657 /* Clear the timestamp list */
1658 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1659 list_del(&ts_skb->list);
1660 kfree(ts_skb);
1661 }
1662
1663 /* PHY disconnect */
1664 if (priv->phydev) {
1665 phy_stop(priv->phydev);
1666 phy_disconnect(priv->phydev);
1667 priv->phydev = NULL;
1668 }
1669
1670 free_irq(ndev->irq, ndev);
1671
1672 napi_disable(&priv->napi[RAVB_NC]);
1673 napi_disable(&priv->napi[RAVB_BE]);
1674
1675 /* Free all the skb's in the RX queue and the DMA buffers. */
1676 ravb_ring_free(ndev, RAVB_BE);
1677 ravb_ring_free(ndev, RAVB_NC);
1678
1679 return 0;
1680}
1681
1682static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1683{
1684 struct ravb_private *priv = netdev_priv(ndev);
1685 struct hwtstamp_config config;
1686
1687 config.flags = 0;
1688 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1689 HWTSTAMP_TX_OFF;
1690 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1691 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1692 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1693 config.rx_filter = HWTSTAMP_FILTER_ALL;
1694 else
1695 config.rx_filter = HWTSTAMP_FILTER_NONE;
1696
1697 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1698 -EFAULT : 0;
1699}
1700
1701/* Control hardware time stamping */
1702static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1703{
1704 struct ravb_private *priv = netdev_priv(ndev);
1705 struct hwtstamp_config config;
1706 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1707 u32 tstamp_tx_ctrl;
1708
1709 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1710 return -EFAULT;
1711
1712 /* Reserved for future extensions */
1713 if (config.flags)
1714 return -EINVAL;
1715
1716 switch (config.tx_type) {
1717 case HWTSTAMP_TX_OFF:
1718 tstamp_tx_ctrl = 0;
1719 break;
1720 case HWTSTAMP_TX_ON:
1721 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1722 break;
1723 default:
1724 return -ERANGE;
1725 }
1726
1727 switch (config.rx_filter) {
1728 case HWTSTAMP_FILTER_NONE:
1729 tstamp_rx_ctrl = 0;
1730 break;
1731 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1732 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1733 break;
1734 default:
1735 config.rx_filter = HWTSTAMP_FILTER_ALL;
1736 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1737 }
1738
1739 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1740 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1741
1742 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1743 -EFAULT : 0;
1744}
1745
1746/* ioctl to device function */
1747static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1748{
1749 struct ravb_private *priv = netdev_priv(ndev);
1750 struct phy_device *phydev = priv->phydev;
1751
1752 if (!netif_running(ndev))
1753 return -EINVAL;
1754
1755 if (!phydev)
1756 return -ENODEV;
1757
1758 switch (cmd) {
1759 case SIOCGHWTSTAMP:
1760 return ravb_hwtstamp_get(ndev, req);
1761 case SIOCSHWTSTAMP:
1762 return ravb_hwtstamp_set(ndev, req);
1763 }
1764
1765 return phy_mii_ioctl(phydev, req, cmd);
1766}
1767
1768static const struct net_device_ops ravb_netdev_ops = {
1769 .ndo_open = ravb_open,
1770 .ndo_stop = ravb_close,
1771 .ndo_start_xmit = ravb_start_xmit,
1772 .ndo_select_queue = ravb_select_queue,
1773 .ndo_get_stats = ravb_get_stats,
1774 .ndo_set_rx_mode = ravb_set_rx_mode,
1775 .ndo_tx_timeout = ravb_tx_timeout,
1776 .ndo_do_ioctl = ravb_do_ioctl,
1777 .ndo_validate_addr = eth_validate_addr,
1778 .ndo_set_mac_address = eth_mac_addr,
1779 .ndo_change_mtu = eth_change_mtu,
1780};
1781
1782/* MDIO bus init function */
1783static int ravb_mdio_init(struct ravb_private *priv)
1784{
1785 struct platform_device *pdev = priv->pdev;
1786 struct device *dev = &pdev->dev;
1787 int error;
1788
1789 /* Bitbang init */
1790 priv->mdiobb.ops = &bb_ops;
1791
1792 /* MII controller setting */
1793 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1794 if (!priv->mii_bus)
1795 return -ENOMEM;
1796
1797 /* Hook up MII support for ethtool */
1798 priv->mii_bus->name = "ravb_mii";
1799 priv->mii_bus->parent = dev;
1800 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1801 pdev->name, pdev->id);
1802
1803 /* Register MDIO bus */
1804 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1805 if (error)
1806 goto out_free_bus;
1807
1808 return 0;
1809
1810out_free_bus:
1811 free_mdio_bitbang(priv->mii_bus);
1812 return error;
1813}
1814
1815/* MDIO bus release function */
1816static int ravb_mdio_release(struct ravb_private *priv)
1817{
1818 /* Unregister mdio bus */
1819 mdiobus_unregister(priv->mii_bus);
1820
1821 /* Free bitbang info */
1822 free_mdio_bitbang(priv->mii_bus);
1823
1824 return 0;
1825}
1826
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001827static const struct of_device_id ravb_match_table[] = {
1828 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1829 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001830 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001831 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001832 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001833 { }
1834};
1835MODULE_DEVICE_TABLE(of, ravb_match_table);
1836
Simon Hormanb3d39a82015-11-20 11:29:39 -08001837static int ravb_set_gti(struct net_device *ndev)
1838{
1839
1840 struct device *dev = ndev->dev.parent;
1841 struct device_node *np = dev->of_node;
1842 unsigned long rate;
1843 struct clk *clk;
1844 uint64_t inc;
1845
1846 clk = of_clk_get(np, 0);
1847 if (IS_ERR(clk)) {
1848 dev_err(dev, "could not get clock\n");
1849 return PTR_ERR(clk);
1850 }
1851
1852 rate = clk_get_rate(clk);
1853 clk_put(clk);
1854
Wolfram Sanga6d37132016-04-08 13:28:42 +02001855 if (!rate)
1856 return -EINVAL;
1857
Simon Hormanb3d39a82015-11-20 11:29:39 -08001858 inc = 1000000000ULL << 20;
1859 do_div(inc, rate);
1860
1861 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1862 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1863 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1864 return -EINVAL;
1865 }
1866
1867 ravb_write(ndev, inc, GTI);
1868
1869 return 0;
1870}
1871
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001872static int ravb_probe(struct platform_device *pdev)
1873{
1874 struct device_node *np = pdev->dev.of_node;
1875 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001876 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001877 struct net_device *ndev;
1878 int error, irq, q;
1879 struct resource *res;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001880 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001881
1882 if (!np) {
1883 dev_err(&pdev->dev,
1884 "this driver is required to be instantiated from device tree\n");
1885 return -EINVAL;
1886 }
1887
1888 /* Get base address */
1889 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1890 if (!res) {
1891 dev_err(&pdev->dev, "invalid resource\n");
1892 return -EINVAL;
1893 }
1894
1895 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
1896 NUM_TX_QUEUE, NUM_RX_QUEUE);
1897 if (!ndev)
1898 return -ENOMEM;
1899
1900 pm_runtime_enable(&pdev->dev);
1901 pm_runtime_get_sync(&pdev->dev);
1902
1903 /* The Ether-specific entries in the device structure. */
1904 ndev->base_addr = res->start;
1905 ndev->dma = -1;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001906
Wolfram Sange8668632016-03-01 17:37:58 +01001907 chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001908
1909 if (chip_id == RCAR_GEN3)
1910 irq = platform_get_irq_byname(pdev, "ch22");
1911 else
1912 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001913 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03001914 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001915 goto out_release;
1916 }
1917 ndev->irq = irq;
1918
1919 SET_NETDEV_DEV(ndev, &pdev->dev);
1920
1921 priv = netdev_priv(ndev);
1922 priv->ndev = ndev;
1923 priv->pdev = pdev;
1924 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
1925 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
1926 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
1927 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
1928 priv->addr = devm_ioremap_resource(&pdev->dev, res);
1929 if (IS_ERR(priv->addr)) {
1930 error = PTR_ERR(priv->addr);
1931 goto out_release;
1932 }
1933
1934 spin_lock_init(&priv->lock);
1935 INIT_WORK(&priv->work, ravb_tx_timeout_work);
1936
1937 priv->phy_interface = of_get_phy_mode(np);
1938
1939 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
1940 priv->avb_link_active_low =
1941 of_property_read_bool(np, "renesas,ether-link-active-low");
1942
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001943 if (chip_id == RCAR_GEN3) {
1944 irq = platform_get_irq_byname(pdev, "ch24");
1945 if (irq < 0) {
1946 error = irq;
1947 goto out_release;
1948 }
1949 priv->emac_irq = irq;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001950 for (i = 0; i < NUM_RX_QUEUE; i++) {
1951 irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
1952 if (irq < 0) {
1953 error = irq;
1954 goto out_release;
1955 }
1956 priv->rx_irqs[i] = irq;
1957 }
1958 for (i = 0; i < NUM_TX_QUEUE; i++) {
1959 irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
1960 if (irq < 0) {
1961 error = irq;
1962 goto out_release;
1963 }
1964 priv->tx_irqs[i] = irq;
1965 }
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001966 }
1967
1968 priv->chip_id = chip_id;
1969
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001970 /* Set function */
1971 ndev->netdev_ops = &ravb_netdev_ops;
1972 ndev->ethtool_ops = &ravb_ethtool_ops;
1973
1974 /* Set AVB config mode */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001975 if (chip_id == RCAR_GEN2) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001976 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001977 /* Set CSEL value */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001978 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001979 } else {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001980 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
1981 CCC_GAC | CCC_CSEL_HPB);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001982 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001983
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001984 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08001985 error = ravb_set_gti(ndev);
1986 if (error)
1987 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001988
1989 /* Request GTI loading */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001990 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001991
1992 /* Allocate descriptor base address table */
1993 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001994 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001995 &priv->desc_bat_dma, GFP_KERNEL);
1996 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09001997 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001998 "Cannot allocate desc base address table (size %d bytes)\n",
1999 priv->desc_bat_size);
2000 error = -ENOMEM;
2001 goto out_release;
2002 }
2003 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2004 priv->desc_bat[q].die_dt = DT_EOS;
2005 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2006
2007 /* Initialise HW timestamp list */
2008 INIT_LIST_HEAD(&priv->ts_skb_list);
2009
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002010 /* Initialise PTP Clock driver */
2011 if (chip_id != RCAR_GEN2)
2012 ravb_ptp_init(ndev, pdev);
2013
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002014 /* Debug message level */
2015 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2016
2017 /* Read and set MAC address */
2018 ravb_read_mac_address(ndev, of_get_mac_address(np));
2019 if (!is_valid_ether_addr(ndev->dev_addr)) {
2020 dev_warn(&pdev->dev,
2021 "no valid MAC address supplied, using a random one\n");
2022 eth_hw_addr_random(ndev);
2023 }
2024
2025 /* MDIO bus init */
2026 error = ravb_mdio_init(priv);
2027 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09002028 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002029 goto out_dma_free;
2030 }
2031
2032 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
2033 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
2034
2035 /* Network device register */
2036 error = register_netdev(ndev);
2037 if (error)
2038 goto out_napi_del;
2039
2040 /* Print device information */
2041 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2042 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2043
2044 platform_set_drvdata(pdev, ndev);
2045
2046 return 0;
2047
2048out_napi_del:
2049 netif_napi_del(&priv->napi[RAVB_NC]);
2050 netif_napi_del(&priv->napi[RAVB_BE]);
2051 ravb_mdio_release(priv);
2052out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002053 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002054 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002055
2056 /* Stop PTP Clock driver */
2057 if (chip_id != RCAR_GEN2)
2058 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002059out_release:
2060 if (ndev)
2061 free_netdev(ndev);
2062
2063 pm_runtime_put(&pdev->dev);
2064 pm_runtime_disable(&pdev->dev);
2065 return error;
2066}
2067
2068static int ravb_remove(struct platform_device *pdev)
2069{
2070 struct net_device *ndev = platform_get_drvdata(pdev);
2071 struct ravb_private *priv = netdev_priv(ndev);
2072
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002073 /* Stop PTP Clock driver */
2074 if (priv->chip_id != RCAR_GEN2)
2075 ravb_ptp_stop(ndev);
2076
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002077 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002078 priv->desc_bat_dma);
2079 /* Set reset mode */
2080 ravb_write(ndev, CCC_OPC_RESET, CCC);
2081 pm_runtime_put_sync(&pdev->dev);
2082 unregister_netdev(ndev);
2083 netif_napi_del(&priv->napi[RAVB_NC]);
2084 netif_napi_del(&priv->napi[RAVB_BE]);
2085 ravb_mdio_release(priv);
2086 pm_runtime_disable(&pdev->dev);
2087 free_netdev(ndev);
2088 platform_set_drvdata(pdev, NULL);
2089
2090 return 0;
2091}
2092
2093#ifdef CONFIG_PM
2094static int ravb_runtime_nop(struct device *dev)
2095{
2096 /* Runtime PM callback shared between ->runtime_suspend()
2097 * and ->runtime_resume(). Simply returns success.
2098 *
2099 * This driver re-initializes all registers after
2100 * pm_runtime_get_sync() anyway so there is no need
2101 * to save and restore registers here.
2102 */
2103 return 0;
2104}
2105
2106static const struct dev_pm_ops ravb_dev_pm_ops = {
2107 .runtime_suspend = ravb_runtime_nop,
2108 .runtime_resume = ravb_runtime_nop,
2109};
2110
2111#define RAVB_PM_OPS (&ravb_dev_pm_ops)
2112#else
2113#define RAVB_PM_OPS NULL
2114#endif
2115
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002116static struct platform_driver ravb_driver = {
2117 .probe = ravb_probe,
2118 .remove = ravb_remove,
2119 .driver = {
2120 .name = "ravb",
2121 .pm = RAVB_PM_OPS,
2122 .of_match_table = ravb_match_table,
2123 },
2124};
2125
2126module_platform_driver(ravb_driver);
2127
2128MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2129MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2130MODULE_LICENSE("GPL v2");