blob: 1ceadbfe85727930d9286249b8145b2317108d44 [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 */
Masaru Nagai8d9c4182016-06-01 03:01:28 +0900405 ravb_write(ndev,
406 RCR_EFFS | RCR_ENCF | RCR_ETS0 | RCR_ESF | 0x18000000, RCR);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300407
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),
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900561 PKT_BUF_SZ,
Sergei Shtylyove2370f02015-07-15 00:56:52 +0300562 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];
Kazuya Mizuguchi094e43d2016-05-02 00:19:51 +0900591 desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300592
593 if (!priv->rx_skb[q][entry]) {
594 skb = netdev_alloc_skb(ndev,
595 PKT_BUF_SZ + RAVB_ALIGN - 1);
596 if (!skb)
597 break; /* Better luck next round. */
598 ravb_set_buffer_align(skb);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900599 dma_addr = dma_map_single(ndev->dev.parent, skb->data,
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300600 le16_to_cpu(desc->ds_cc),
601 DMA_FROM_DEVICE);
602 skb_checksum_none_assert(skb);
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300603 /* We just set the data size to 0 for a failed mapping
604 * which should prevent DMA from happening...
605 */
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +0900606 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovd8b48912015-07-22 01:31:59 +0300607 desc->ds_cc = cpu_to_le16(0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300608 desc->dptr = cpu_to_le32(dma_addr);
609 priv->rx_skb[q][entry] = skb;
610 }
611 /* Descriptor type must be set after all the above writes */
612 dma_wmb();
613 desc->die_dt = DT_FEMPTY;
614 }
615
616 *quota -= limit - (++boguscnt);
617
618 return boguscnt <= 0;
619}
620
621static void ravb_rcv_snd_disable(struct net_device *ndev)
622{
623 /* Disable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300624 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300625}
626
627static void ravb_rcv_snd_enable(struct net_device *ndev)
628{
629 /* Enable TX and RX */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300630 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300631}
632
633/* function for waiting dma process finished */
634static int ravb_stop_dma(struct net_device *ndev)
635{
636 int error;
637
638 /* Wait for stopping the hardware TX process */
639 error = ravb_wait(ndev, TCCR,
640 TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
641 if (error)
642 return error;
643
644 error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
645 0);
646 if (error)
647 return error;
648
649 /* Stop the E-MAC's RX/TX processes. */
650 ravb_rcv_snd_disable(ndev);
651
652 /* Wait for stopping the RX DMA process */
653 error = ravb_wait(ndev, CSR, CSR_RPO, 0);
654 if (error)
655 return error;
656
657 /* Stop AVB-DMAC process */
658 return ravb_config(ndev);
659}
660
661/* E-MAC interrupt handler */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900662static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300663{
664 struct ravb_private *priv = netdev_priv(ndev);
665 u32 ecsr, psr;
666
667 ecsr = ravb_read(ndev, ECSR);
668 ravb_write(ndev, ecsr, ECSR); /* clear interrupt */
669 if (ecsr & ECSR_ICD)
670 ndev->stats.tx_carrier_errors++;
671 if (ecsr & ECSR_LCHNG) {
672 /* Link changed */
673 if (priv->no_avb_link)
674 return;
675 psr = ravb_read(ndev, PSR);
676 if (priv->avb_link_active_low)
677 psr ^= PSR_LMON;
678 if (!(psr & PSR_LMON)) {
679 /* DIsable RX and TX */
680 ravb_rcv_snd_disable(ndev);
681 } else {
682 /* Enable RX and TX */
683 ravb_rcv_snd_enable(ndev);
684 }
685 }
686}
687
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900688static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
689{
690 struct net_device *ndev = dev_id;
691 struct ravb_private *priv = netdev_priv(ndev);
692
693 spin_lock(&priv->lock);
694 ravb_emac_interrupt_unlocked(ndev);
695 mmiowb();
696 spin_unlock(&priv->lock);
697 return IRQ_HANDLED;
698}
699
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300700/* Error interrupt handler */
701static void ravb_error_interrupt(struct net_device *ndev)
702{
703 struct ravb_private *priv = netdev_priv(ndev);
704 u32 eis, ris2;
705
706 eis = ravb_read(ndev, EIS);
707 ravb_write(ndev, ~EIS_QFS, EIS);
708 if (eis & EIS_QFS) {
709 ris2 = ravb_read(ndev, RIS2);
710 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF), RIS2);
711
712 /* Receive Descriptor Empty int */
713 if (ris2 & RIS2_QFF0)
714 priv->stats[RAVB_BE].rx_over_errors++;
715
716 /* Receive Descriptor Empty int */
717 if (ris2 & RIS2_QFF1)
718 priv->stats[RAVB_NC].rx_over_errors++;
719
720 /* Receive FIFO Overflow int */
721 if (ris2 & RIS2_RFFF)
722 priv->rx_fifo_errors++;
723 }
724}
725
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900726static bool ravb_queue_interrupt(struct net_device *ndev, int q)
727{
728 struct ravb_private *priv = netdev_priv(ndev);
729 u32 ris0 = ravb_read(ndev, RIS0);
730 u32 ric0 = ravb_read(ndev, RIC0);
731 u32 tis = ravb_read(ndev, TIS);
732 u32 tic = ravb_read(ndev, TIC);
733
734 if (((ris0 & ric0) & BIT(q)) || ((tis & tic) & BIT(q))) {
735 if (napi_schedule_prep(&priv->napi[q])) {
736 /* Mask RX and TX interrupts */
737 if (priv->chip_id == RCAR_GEN2) {
738 ravb_write(ndev, ric0 & ~BIT(q), RIC0);
739 ravb_write(ndev, tic & ~BIT(q), TIC);
740 } else {
741 ravb_write(ndev, BIT(q), RID0);
742 ravb_write(ndev, BIT(q), TID);
743 }
744 __napi_schedule(&priv->napi[q]);
745 } else {
746 netdev_warn(ndev,
747 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
748 ris0, ric0);
749 netdev_warn(ndev,
750 " tx status 0x%08x, tx mask 0x%08x.\n",
751 tis, tic);
752 }
753 return true;
754 }
755 return false;
756}
757
758static bool ravb_timestamp_interrupt(struct net_device *ndev)
759{
760 u32 tis = ravb_read(ndev, TIS);
761
762 if (tis & TIS_TFUF) {
763 ravb_write(ndev, ~TIS_TFUF, TIS);
764 ravb_get_tx_tstamp(ndev);
765 return true;
766 }
767 return false;
768}
769
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300770static irqreturn_t ravb_interrupt(int irq, void *dev_id)
771{
772 struct net_device *ndev = dev_id;
773 struct ravb_private *priv = netdev_priv(ndev);
774 irqreturn_t result = IRQ_NONE;
775 u32 iss;
776
777 spin_lock(&priv->lock);
778 /* Get interrupt status */
779 iss = ravb_read(ndev, ISS);
780
781 /* Received and transmitted interrupts */
782 if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300783 int q;
784
785 /* Timestamp updated */
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900786 if (ravb_timestamp_interrupt(ndev))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300787 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300788
789 /* Network control and best effort queue RX/TX */
790 for (q = RAVB_NC; q >= RAVB_BE; q--) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900791 if (ravb_queue_interrupt(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300792 result = IRQ_HANDLED;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300793 }
794 }
795
796 /* E-MAC status summary */
797 if (iss & ISS_MS) {
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900798 ravb_emac_interrupt_unlocked(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300799 result = IRQ_HANDLED;
800 }
801
802 /* Error status summary */
803 if (iss & ISS_ES) {
804 ravb_error_interrupt(ndev);
805 result = IRQ_HANDLED;
806 }
807
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900808 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300809 if (iss & ISS_CGIS) {
810 ravb_ptp_interrupt(ndev);
Yoshihiro Kaneko38c848c2016-03-16 00:52:16 +0900811 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300812 }
Sergei Shtylyova0d2f202015-06-11 01:02:30 +0300813
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300814 mmiowb();
815 spin_unlock(&priv->lock);
816 return result;
817}
818
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900819/* Timestamp/Error/gPTP interrupt handler */
820static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
821{
822 struct net_device *ndev = dev_id;
823 struct ravb_private *priv = netdev_priv(ndev);
824 irqreturn_t result = IRQ_NONE;
825 u32 iss;
826
827 spin_lock(&priv->lock);
828 /* Get interrupt status */
829 iss = ravb_read(ndev, ISS);
830
831 /* Timestamp updated */
832 if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
833 result = IRQ_HANDLED;
834
835 /* Error status summary */
836 if (iss & ISS_ES) {
837 ravb_error_interrupt(ndev);
838 result = IRQ_HANDLED;
839 }
840
841 /* gPTP interrupt status summary */
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300842 if (iss & ISS_CGIS) {
843 ravb_ptp_interrupt(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900844 result = IRQ_HANDLED;
Sergei Shtylyovd0988a52016-04-10 23:55:15 +0300845 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900846
847 mmiowb();
848 spin_unlock(&priv->lock);
849 return result;
850}
851
852static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
853{
854 struct net_device *ndev = dev_id;
855 struct ravb_private *priv = netdev_priv(ndev);
856 irqreturn_t result = IRQ_NONE;
857
858 spin_lock(&priv->lock);
859
860 /* Network control/Best effort queue RX/TX */
861 if (ravb_queue_interrupt(ndev, q))
862 result = IRQ_HANDLED;
863
864 mmiowb();
865 spin_unlock(&priv->lock);
866 return result;
867}
868
869static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
870{
871 return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
872}
873
874static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
875{
876 return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
877}
878
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300879static int ravb_poll(struct napi_struct *napi, int budget)
880{
881 struct net_device *ndev = napi->dev;
882 struct ravb_private *priv = netdev_priv(ndev);
883 unsigned long flags;
884 int q = napi - priv->napi;
885 int mask = BIT(q);
886 int quota = budget;
887 u32 ris0, tis;
888
889 for (;;) {
890 tis = ravb_read(ndev, TIS);
891 ris0 = ravb_read(ndev, RIS0);
892 if (!((ris0 & mask) || (tis & mask)))
893 break;
894
895 /* Processing RX Descriptor Ring */
896 if (ris0 & mask) {
897 /* Clear RX interrupt */
898 ravb_write(ndev, ~mask, RIS0);
899 if (ravb_rx(ndev, &quota, q))
900 goto out;
901 }
902 /* Processing TX Descriptor Ring */
903 if (tis & mask) {
904 spin_lock_irqsave(&priv->lock, flags);
905 /* Clear TX interrupt */
906 ravb_write(ndev, ~mask, TIS);
907 ravb_tx_free(ndev, q);
908 netif_wake_subqueue(ndev, q);
909 mmiowb();
910 spin_unlock_irqrestore(&priv->lock, flags);
911 }
912 }
913
914 napi_complete(napi);
915
916 /* Re-enable RX/TX interrupts */
917 spin_lock_irqsave(&priv->lock, flags);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +0900918 if (priv->chip_id == RCAR_GEN2) {
919 ravb_modify(ndev, RIC0, mask, mask);
920 ravb_modify(ndev, TIC, mask, mask);
921 } else {
922 ravb_write(ndev, mask, RIE0);
923 ravb_write(ndev, mask, TIE);
924 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300925 mmiowb();
926 spin_unlock_irqrestore(&priv->lock, flags);
927
928 /* Receive error message handling */
929 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
930 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
931 if (priv->rx_over_errors != ndev->stats.rx_over_errors) {
932 ndev->stats.rx_over_errors = priv->rx_over_errors;
933 netif_err(priv, rx_err, ndev, "Receive Descriptor Empty\n");
934 }
935 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors) {
936 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
937 netif_err(priv, rx_err, ndev, "Receive FIFO Overflow\n");
938 }
939out:
940 return budget - quota;
941}
942
943/* PHY state control function */
944static void ravb_adjust_link(struct net_device *ndev)
945{
946 struct ravb_private *priv = netdev_priv(ndev);
947 struct phy_device *phydev = priv->phydev;
948 bool new_state = false;
949
950 if (phydev->link) {
951 if (phydev->duplex != priv->duplex) {
952 new_state = true;
953 priv->duplex = phydev->duplex;
954 ravb_set_duplex(ndev);
955 }
956
957 if (phydev->speed != priv->speed) {
958 new_state = true;
959 priv->speed = phydev->speed;
960 ravb_set_rate(ndev);
961 }
962 if (!priv->link) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +0300963 ravb_modify(ndev, ECMR, ECMR_TXF, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300964 new_state = true;
965 priv->link = phydev->link;
966 if (priv->no_avb_link)
967 ravb_rcv_snd_enable(ndev);
968 }
969 } else if (priv->link) {
970 new_state = true;
971 priv->link = 0;
972 priv->speed = 0;
973 priv->duplex = -1;
974 if (priv->no_avb_link)
975 ravb_rcv_snd_disable(ndev);
976 }
977
978 if (new_state && netif_msg_link(priv))
979 phy_print_status(phydev);
980}
981
982/* PHY init function */
983static int ravb_phy_init(struct net_device *ndev)
984{
985 struct device_node *np = ndev->dev.parent->of_node;
986 struct ravb_private *priv = netdev_priv(ndev);
987 struct phy_device *phydev;
988 struct device_node *pn;
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900989 int err;
Sergei Shtylyovc1566332015-06-11 01:01:43 +0300990
991 priv->link = 0;
992 priv->speed = 0;
993 priv->duplex = -1;
994
995 /* Try connecting to PHY */
996 pn = of_parse_phandle(np, "phy-handle", 0);
Kazuya Mizuguchib4bc88a2015-12-15 19:44:13 +0900997 if (!pn) {
998 /* In the case of a fixed PHY, the DT node associated
999 * to the PHY is the Ethernet MAC DT node.
1000 */
1001 if (of_phy_is_fixed_link(np)) {
1002 err = of_phy_register_fixed_link(np);
1003 if (err)
1004 return err;
1005 }
1006 pn = of_node_get(np);
1007 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001008 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
1009 priv->phy_interface);
1010 if (!phydev) {
1011 netdev_err(ndev, "failed to connect PHY\n");
1012 return -ENOENT;
1013 }
1014
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001015 /* This driver only support 10/100Mbit speeds on Gen3
1016 * at this time.
1017 */
1018 if (priv->chip_id == RCAR_GEN3) {
1019 int err;
1020
1021 err = phy_set_max_speed(phydev, SPEED_100);
1022 if (err) {
1023 netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
1024 phy_disconnect(phydev);
1025 return err;
1026 }
1027
1028 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
1029 }
1030
Kazuya Mizuguchi54499962015-12-14 00:15:58 +09001031 /* 10BASE is not supported */
1032 phydev->supported &= ~PHY_10BT_FEATURES;
1033
Andrew Lunn22209432016-01-06 20:11:13 +01001034 phy_attached_info(phydev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001035
1036 priv->phydev = phydev;
1037
1038 return 0;
1039}
1040
1041/* PHY control start function */
1042static int ravb_phy_start(struct net_device *ndev)
1043{
1044 struct ravb_private *priv = netdev_priv(ndev);
1045 int error;
1046
1047 error = ravb_phy_init(ndev);
1048 if (error)
1049 return error;
1050
1051 phy_start(priv->phydev);
1052
1053 return 0;
1054}
1055
1056static int ravb_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1057{
1058 struct ravb_private *priv = netdev_priv(ndev);
1059 int error = -ENODEV;
1060 unsigned long flags;
1061
1062 if (priv->phydev) {
1063 spin_lock_irqsave(&priv->lock, flags);
1064 error = phy_ethtool_gset(priv->phydev, ecmd);
1065 spin_unlock_irqrestore(&priv->lock, flags);
1066 }
1067
1068 return error;
1069}
1070
1071static int ravb_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1072{
1073 struct ravb_private *priv = netdev_priv(ndev);
1074 unsigned long flags;
1075 int error;
1076
1077 if (!priv->phydev)
1078 return -ENODEV;
1079
1080 spin_lock_irqsave(&priv->lock, flags);
1081
1082 /* Disable TX and RX */
1083 ravb_rcv_snd_disable(ndev);
1084
1085 error = phy_ethtool_sset(priv->phydev, ecmd);
1086 if (error)
1087 goto error_exit;
1088
1089 if (ecmd->duplex == DUPLEX_FULL)
1090 priv->duplex = 1;
1091 else
1092 priv->duplex = 0;
1093
1094 ravb_set_duplex(ndev);
1095
1096error_exit:
1097 mdelay(1);
1098
1099 /* Enable TX and RX */
1100 ravb_rcv_snd_enable(ndev);
1101
1102 mmiowb();
1103 spin_unlock_irqrestore(&priv->lock, flags);
1104
1105 return error;
1106}
1107
1108static int ravb_nway_reset(struct net_device *ndev)
1109{
1110 struct ravb_private *priv = netdev_priv(ndev);
1111 int error = -ENODEV;
1112 unsigned long flags;
1113
1114 if (priv->phydev) {
1115 spin_lock_irqsave(&priv->lock, flags);
1116 error = phy_start_aneg(priv->phydev);
1117 spin_unlock_irqrestore(&priv->lock, flags);
1118 }
1119
1120 return error;
1121}
1122
1123static u32 ravb_get_msglevel(struct net_device *ndev)
1124{
1125 struct ravb_private *priv = netdev_priv(ndev);
1126
1127 return priv->msg_enable;
1128}
1129
1130static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1131{
1132 struct ravb_private *priv = netdev_priv(ndev);
1133
1134 priv->msg_enable = value;
1135}
1136
1137static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1138 "rx_queue_0_current",
1139 "tx_queue_0_current",
1140 "rx_queue_0_dirty",
1141 "tx_queue_0_dirty",
1142 "rx_queue_0_packets",
1143 "tx_queue_0_packets",
1144 "rx_queue_0_bytes",
1145 "tx_queue_0_bytes",
1146 "rx_queue_0_mcast_packets",
1147 "rx_queue_0_errors",
1148 "rx_queue_0_crc_errors",
1149 "rx_queue_0_frame_errors",
1150 "rx_queue_0_length_errors",
1151 "rx_queue_0_missed_errors",
1152 "rx_queue_0_over_errors",
1153
1154 "rx_queue_1_current",
1155 "tx_queue_1_current",
1156 "rx_queue_1_dirty",
1157 "tx_queue_1_dirty",
1158 "rx_queue_1_packets",
1159 "tx_queue_1_packets",
1160 "rx_queue_1_bytes",
1161 "tx_queue_1_bytes",
1162 "rx_queue_1_mcast_packets",
1163 "rx_queue_1_errors",
1164 "rx_queue_1_crc_errors",
Sergei Shtylyovb17c1d92015-12-04 01:51:10 +03001165 "rx_queue_1_frame_errors",
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001166 "rx_queue_1_length_errors",
1167 "rx_queue_1_missed_errors",
1168 "rx_queue_1_over_errors",
1169};
1170
1171#define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1172
1173static int ravb_get_sset_count(struct net_device *netdev, int sset)
1174{
1175 switch (sset) {
1176 case ETH_SS_STATS:
1177 return RAVB_STATS_LEN;
1178 default:
1179 return -EOPNOTSUPP;
1180 }
1181}
1182
1183static void ravb_get_ethtool_stats(struct net_device *ndev,
1184 struct ethtool_stats *stats, u64 *data)
1185{
1186 struct ravb_private *priv = netdev_priv(ndev);
1187 int i = 0;
1188 int q;
1189
1190 /* Device-specific stats */
1191 for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1192 struct net_device_stats *stats = &priv->stats[q];
1193
1194 data[i++] = priv->cur_rx[q];
1195 data[i++] = priv->cur_tx[q];
1196 data[i++] = priv->dirty_rx[q];
1197 data[i++] = priv->dirty_tx[q];
1198 data[i++] = stats->rx_packets;
1199 data[i++] = stats->tx_packets;
1200 data[i++] = stats->rx_bytes;
1201 data[i++] = stats->tx_bytes;
1202 data[i++] = stats->multicast;
1203 data[i++] = stats->rx_errors;
1204 data[i++] = stats->rx_crc_errors;
1205 data[i++] = stats->rx_frame_errors;
1206 data[i++] = stats->rx_length_errors;
1207 data[i++] = stats->rx_missed_errors;
1208 data[i++] = stats->rx_over_errors;
1209 }
1210}
1211
1212static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1213{
1214 switch (stringset) {
1215 case ETH_SS_STATS:
1216 memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
1217 break;
1218 }
1219}
1220
1221static void ravb_get_ringparam(struct net_device *ndev,
1222 struct ethtool_ringparam *ring)
1223{
1224 struct ravb_private *priv = netdev_priv(ndev);
1225
1226 ring->rx_max_pending = BE_RX_RING_MAX;
1227 ring->tx_max_pending = BE_TX_RING_MAX;
1228 ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1229 ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1230}
1231
1232static int ravb_set_ringparam(struct net_device *ndev,
1233 struct ethtool_ringparam *ring)
1234{
1235 struct ravb_private *priv = netdev_priv(ndev);
1236 int error;
1237
1238 if (ring->tx_pending > BE_TX_RING_MAX ||
1239 ring->rx_pending > BE_RX_RING_MAX ||
1240 ring->tx_pending < BE_TX_RING_MIN ||
1241 ring->rx_pending < BE_RX_RING_MIN)
1242 return -EINVAL;
1243 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1244 return -EINVAL;
1245
1246 if (netif_running(ndev)) {
1247 netif_device_detach(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001248 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001249 if (priv->chip_id == RCAR_GEN2)
1250 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001251 /* Wait for DMA stopping */
1252 error = ravb_stop_dma(ndev);
1253 if (error) {
1254 netdev_err(ndev,
1255 "cannot set ringparam! Any AVB processes are still running?\n");
1256 return error;
1257 }
1258 synchronize_irq(ndev->irq);
1259
1260 /* Free all the skb's in the RX queue and the DMA buffers. */
1261 ravb_ring_free(ndev, RAVB_BE);
1262 ravb_ring_free(ndev, RAVB_NC);
1263 }
1264
1265 /* Set new parameters */
1266 priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1267 priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1268
1269 if (netif_running(ndev)) {
1270 error = ravb_dmac_init(ndev);
1271 if (error) {
1272 netdev_err(ndev,
1273 "%s: ravb_dmac_init() failed, error %d\n",
1274 __func__, error);
1275 return error;
1276 }
1277
1278 ravb_emac_init(ndev);
1279
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001280 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001281 if (priv->chip_id == RCAR_GEN2)
1282 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001283
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001284 netif_device_attach(ndev);
1285 }
1286
1287 return 0;
1288}
1289
1290static int ravb_get_ts_info(struct net_device *ndev,
1291 struct ethtool_ts_info *info)
1292{
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001293 struct ravb_private *priv = netdev_priv(ndev);
1294
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001295 info->so_timestamping =
1296 SOF_TIMESTAMPING_TX_SOFTWARE |
1297 SOF_TIMESTAMPING_RX_SOFTWARE |
1298 SOF_TIMESTAMPING_SOFTWARE |
1299 SOF_TIMESTAMPING_TX_HARDWARE |
1300 SOF_TIMESTAMPING_RX_HARDWARE |
1301 SOF_TIMESTAMPING_RAW_HARDWARE;
1302 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1303 info->rx_filters =
1304 (1 << HWTSTAMP_FILTER_NONE) |
1305 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1306 (1 << HWTSTAMP_FILTER_ALL);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001307 info->phc_index = ptp_clock_index(priv->ptp.clock);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001308
1309 return 0;
1310}
1311
1312static const struct ethtool_ops ravb_ethtool_ops = {
1313 .get_settings = ravb_get_settings,
1314 .set_settings = ravb_set_settings,
1315 .nway_reset = ravb_nway_reset,
1316 .get_msglevel = ravb_get_msglevel,
1317 .set_msglevel = ravb_set_msglevel,
1318 .get_link = ethtool_op_get_link,
1319 .get_strings = ravb_get_strings,
1320 .get_ethtool_stats = ravb_get_ethtool_stats,
1321 .get_sset_count = ravb_get_sset_count,
1322 .get_ringparam = ravb_get_ringparam,
1323 .set_ringparam = ravb_set_ringparam,
1324 .get_ts_info = ravb_get_ts_info,
1325};
1326
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001327static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1328 struct net_device *ndev, struct device *dev,
1329 const char *ch)
1330{
1331 char *name;
1332 int error;
1333
1334 name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1335 if (!name)
1336 return -ENOMEM;
1337 error = request_irq(irq, handler, 0, name, ndev);
1338 if (error)
1339 netdev_err(ndev, "cannot request IRQ %s\n", name);
1340
1341 return error;
1342}
1343
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001344/* Network device open function for Ethernet AVB */
1345static int ravb_open(struct net_device *ndev)
1346{
1347 struct ravb_private *priv = netdev_priv(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001348 struct platform_device *pdev = priv->pdev;
1349 struct device *dev = &pdev->dev;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001350 int error;
1351
1352 napi_enable(&priv->napi[RAVB_BE]);
1353 napi_enable(&priv->napi[RAVB_NC]);
1354
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001355 if (priv->chip_id == RCAR_GEN2) {
1356 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1357 ndev->name, ndev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001358 if (error) {
1359 netdev_err(ndev, "cannot request IRQ\n");
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001360 goto out_napi_off;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001361 }
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001362 } else {
1363 error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1364 dev, "ch22:multi");
1365 if (error)
1366 goto out_napi_off;
1367 error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1368 dev, "ch24:emac");
1369 if (error)
1370 goto out_free_irq;
1371 error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1372 ndev, dev, "ch0:rx_be");
1373 if (error)
1374 goto out_free_irq_emac;
1375 error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1376 ndev, dev, "ch18:tx_be");
1377 if (error)
1378 goto out_free_irq_be_rx;
1379 error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1380 ndev, dev, "ch1:rx_nc");
1381 if (error)
1382 goto out_free_irq_be_tx;
1383 error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1384 ndev, dev, "ch19:tx_nc");
1385 if (error)
1386 goto out_free_irq_nc_rx;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001387 }
1388
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001389 /* Device init */
1390 error = ravb_dmac_init(ndev);
1391 if (error)
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001392 goto out_free_irq_nc_tx;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001393 ravb_emac_init(ndev);
1394
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001395 /* Initialise PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001396 if (priv->chip_id == RCAR_GEN2)
1397 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001398
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001399 netif_tx_start_all_queues(ndev);
1400
1401 /* PHY control start */
1402 error = ravb_phy_start(ndev);
1403 if (error)
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001404 goto out_ptp_stop;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001405
1406 return 0;
1407
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001408out_ptp_stop:
1409 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001410 if (priv->chip_id == RCAR_GEN2)
1411 ravb_ptp_stop(ndev);
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001412out_free_irq_nc_tx:
1413 if (priv->chip_id == RCAR_GEN2)
1414 goto out_free_irq;
1415 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1416out_free_irq_nc_rx:
1417 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1418out_free_irq_be_tx:
1419 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1420out_free_irq_be_rx:
1421 free_irq(priv->rx_irqs[RAVB_BE], ndev);
1422out_free_irq_emac:
1423 free_irq(priv->emac_irq, ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001424out_free_irq:
1425 free_irq(ndev->irq, ndev);
1426out_napi_off:
1427 napi_disable(&priv->napi[RAVB_NC]);
1428 napi_disable(&priv->napi[RAVB_BE]);
1429 return error;
1430}
1431
1432/* Timeout function for Ethernet AVB */
1433static void ravb_tx_timeout(struct net_device *ndev)
1434{
1435 struct ravb_private *priv = netdev_priv(ndev);
1436
1437 netif_err(priv, tx_err, ndev,
1438 "transmit timed out, status %08x, resetting...\n",
1439 ravb_read(ndev, ISS));
1440
1441 /* tx_errors count up */
1442 ndev->stats.tx_errors++;
1443
1444 schedule_work(&priv->work);
1445}
1446
1447static void ravb_tx_timeout_work(struct work_struct *work)
1448{
1449 struct ravb_private *priv = container_of(work, struct ravb_private,
1450 work);
1451 struct net_device *ndev = priv->ndev;
1452
1453 netif_tx_stop_all_queues(ndev);
1454
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001455 /* Stop PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001456 if (priv->chip_id == RCAR_GEN2)
1457 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001458
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001459 /* Wait for DMA stopping */
1460 ravb_stop_dma(ndev);
1461
1462 ravb_ring_free(ndev, RAVB_BE);
1463 ravb_ring_free(ndev, RAVB_NC);
1464
1465 /* Device init */
1466 ravb_dmac_init(ndev);
1467 ravb_emac_init(ndev);
1468
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001469 /* Initialise PTP Clock driver */
Sergei Shtylyov50bfd832016-02-06 17:47:22 +03001470 if (priv->chip_id == RCAR_GEN2)
1471 ravb_ptp_init(ndev, priv->pdev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001472
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001473 netif_tx_start_all_queues(ndev);
1474}
1475
1476/* Packet transmit function for Ethernet AVB */
1477static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1478{
1479 struct ravb_private *priv = netdev_priv(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001480 u16 q = skb_get_queue_mapping(skb);
Sergei Shtylyovaad0d512015-07-10 21:10:10 +03001481 struct ravb_tstamp_skb *ts_skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001482 struct ravb_tx_desc *desc;
1483 unsigned long flags;
1484 u32 dma_addr;
1485 void *buffer;
1486 u32 entry;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001487 u32 len;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001488
1489 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001490 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1491 NUM_TX_DESC) {
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001492 netif_err(priv, tx_queued, ndev,
1493 "still transmitting with the full ring!\n");
1494 netif_stop_subqueue(ndev, q);
1495 spin_unlock_irqrestore(&priv->lock, flags);
1496 return NETDEV_TX_BUSY;
1497 }
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001498 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1499 priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001500
1501 if (skb_put_padto(skb, ETH_ZLEN))
1502 goto drop;
1503
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001504 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1505 entry / NUM_TX_DESC * DPTR_ALIGN;
1506 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
1507 memcpy(buffer, skb->data, len);
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001508 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1509 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001510 goto drop;
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001511
1512 desc = &priv->tx_ring[q][entry];
1513 desc->ds_tagl = cpu_to_le16(len);
1514 desc->dptr = cpu_to_le32(dma_addr);
1515
1516 buffer = skb->data + len;
1517 len = skb->len - len;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001518 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1519 if (dma_mapping_error(ndev->dev.parent, dma_addr))
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001520 goto unmap;
1521
1522 desc++;
1523 desc->ds_tagl = cpu_to_le16(len);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001524 desc->dptr = cpu_to_le32(dma_addr);
1525
1526 /* TX timestamp required */
1527 if (q == RAVB_NC) {
1528 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1529 if (!ts_skb) {
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001530 desc--;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001531 dma_unmap_single(ndev->dev.parent, dma_addr, len,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001532 DMA_TO_DEVICE);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001533 goto unmap;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001534 }
1535 ts_skb->skb = skb;
1536 ts_skb->tag = priv->ts_skb_tag++;
1537 priv->ts_skb_tag &= 0x3ff;
1538 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1539
1540 /* TAG and timestamp required flag */
1541 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001542 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1543 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1544 }
1545
Lino Sanfilippod7be81a2016-03-27 12:22:02 +02001546 skb_tx_timestamp(skb);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001547 /* Descriptor type must be set after all the above writes */
1548 dma_wmb();
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001549 desc->die_dt = DT_FEND;
1550 desc--;
1551 desc->die_dt = DT_FSTART;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001552
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001553 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001554
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001555 priv->cur_tx[q] += NUM_TX_DESC;
1556 if (priv->cur_tx[q] - priv->dirty_tx[q] >
1557 (priv->num_tx_ring[q] - 1) * NUM_TX_DESC && !ravb_tx_free(ndev, q))
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001558 netif_stop_subqueue(ndev, q);
1559
1560exit:
1561 mmiowb();
1562 spin_unlock_irqrestore(&priv->lock, flags);
1563 return NETDEV_TX_OK;
1564
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001565unmap:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09001566 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001567 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001568drop:
1569 dev_kfree_skb_any(skb);
Sergei Shtylyov2f45d192015-07-25 23:42:01 +03001570 priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001571 goto exit;
1572}
1573
1574static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1575 void *accel_priv, select_queue_fallback_t fallback)
1576{
1577 /* If skb needs TX timestamp, it is handled in network control queue */
1578 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1579 RAVB_BE;
1580
1581}
1582
1583static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1584{
1585 struct ravb_private *priv = netdev_priv(ndev);
1586 struct net_device_stats *nstats, *stats0, *stats1;
1587
1588 nstats = &ndev->stats;
1589 stats0 = &priv->stats[RAVB_BE];
1590 stats1 = &priv->stats[RAVB_NC];
1591
1592 nstats->tx_dropped += ravb_read(ndev, TROCR);
1593 ravb_write(ndev, 0, TROCR); /* (write clear) */
1594 nstats->collisions += ravb_read(ndev, CDCR);
1595 ravb_write(ndev, 0, CDCR); /* (write clear) */
1596 nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1597 ravb_write(ndev, 0, LCCR); /* (write clear) */
1598
1599 nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1600 ravb_write(ndev, 0, CERCR); /* (write clear) */
1601 nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1602 ravb_write(ndev, 0, CEECR); /* (write clear) */
1603
1604 nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1605 nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1606 nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1607 nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1608 nstats->multicast = stats0->multicast + stats1->multicast;
1609 nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1610 nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1611 nstats->rx_frame_errors =
1612 stats0->rx_frame_errors + stats1->rx_frame_errors;
1613 nstats->rx_length_errors =
1614 stats0->rx_length_errors + stats1->rx_length_errors;
1615 nstats->rx_missed_errors =
1616 stats0->rx_missed_errors + stats1->rx_missed_errors;
1617 nstats->rx_over_errors =
1618 stats0->rx_over_errors + stats1->rx_over_errors;
1619
1620 return nstats;
1621}
1622
1623/* Update promiscuous bit */
1624static void ravb_set_rx_mode(struct net_device *ndev)
1625{
1626 struct ravb_private *priv = netdev_priv(ndev);
1627 unsigned long flags;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001628
1629 spin_lock_irqsave(&priv->lock, flags);
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001630 ravb_modify(ndev, ECMR, ECMR_PRM,
1631 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001632 mmiowb();
1633 spin_unlock_irqrestore(&priv->lock, flags);
1634}
1635
1636/* Device close function for Ethernet AVB */
1637static int ravb_close(struct net_device *ndev)
1638{
1639 struct ravb_private *priv = netdev_priv(ndev);
1640 struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1641
1642 netif_tx_stop_all_queues(ndev);
1643
1644 /* Disable interrupts by clearing the interrupt masks. */
1645 ravb_write(ndev, 0, RIC0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001646 ravb_write(ndev, 0, RIC2);
1647 ravb_write(ndev, 0, TIC);
1648
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001649 /* Stop PTP Clock driver */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001650 if (priv->chip_id == RCAR_GEN2)
1651 ravb_ptp_stop(ndev);
Sergei Shtylyova0d2f202015-06-11 01:02:30 +03001652
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001653 /* Set the config mode to stop the AVB-DMAC's processes */
1654 if (ravb_stop_dma(ndev) < 0)
1655 netdev_err(ndev,
1656 "device will be stopped after h/w processes are done.\n");
1657
1658 /* Clear the timestamp list */
1659 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1660 list_del(&ts_skb->list);
1661 kfree(ts_skb);
1662 }
1663
1664 /* PHY disconnect */
1665 if (priv->phydev) {
1666 phy_stop(priv->phydev);
1667 phy_disconnect(priv->phydev);
1668 priv->phydev = NULL;
1669 }
1670
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001671 if (priv->chip_id != RCAR_GEN2) {
1672 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1673 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1674 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1675 free_irq(priv->rx_irqs[RAVB_BE], ndev);
Geert Uytterhoeven7fa816b2016-05-07 13:17:11 +02001676 free_irq(priv->emac_irq, ndev);
Geert Uytterhoevenccf92822016-05-17 11:05:34 +02001677 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001678 free_irq(ndev->irq, ndev);
1679
1680 napi_disable(&priv->napi[RAVB_NC]);
1681 napi_disable(&priv->napi[RAVB_BE]);
1682
1683 /* Free all the skb's in the RX queue and the DMA buffers. */
1684 ravb_ring_free(ndev, RAVB_BE);
1685 ravb_ring_free(ndev, RAVB_NC);
1686
1687 return 0;
1688}
1689
1690static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1691{
1692 struct ravb_private *priv = netdev_priv(ndev);
1693 struct hwtstamp_config config;
1694
1695 config.flags = 0;
1696 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1697 HWTSTAMP_TX_OFF;
1698 if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
1699 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1700 else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
1701 config.rx_filter = HWTSTAMP_FILTER_ALL;
1702 else
1703 config.rx_filter = HWTSTAMP_FILTER_NONE;
1704
1705 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1706 -EFAULT : 0;
1707}
1708
1709/* Control hardware time stamping */
1710static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1711{
1712 struct ravb_private *priv = netdev_priv(ndev);
1713 struct hwtstamp_config config;
1714 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1715 u32 tstamp_tx_ctrl;
1716
1717 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1718 return -EFAULT;
1719
1720 /* Reserved for future extensions */
1721 if (config.flags)
1722 return -EINVAL;
1723
1724 switch (config.tx_type) {
1725 case HWTSTAMP_TX_OFF:
1726 tstamp_tx_ctrl = 0;
1727 break;
1728 case HWTSTAMP_TX_ON:
1729 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1730 break;
1731 default:
1732 return -ERANGE;
1733 }
1734
1735 switch (config.rx_filter) {
1736 case HWTSTAMP_FILTER_NONE:
1737 tstamp_rx_ctrl = 0;
1738 break;
1739 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1740 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1741 break;
1742 default:
1743 config.rx_filter = HWTSTAMP_FILTER_ALL;
1744 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1745 }
1746
1747 priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1748 priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1749
1750 return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1751 -EFAULT : 0;
1752}
1753
1754/* ioctl to device function */
1755static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1756{
1757 struct ravb_private *priv = netdev_priv(ndev);
1758 struct phy_device *phydev = priv->phydev;
1759
1760 if (!netif_running(ndev))
1761 return -EINVAL;
1762
1763 if (!phydev)
1764 return -ENODEV;
1765
1766 switch (cmd) {
1767 case SIOCGHWTSTAMP:
1768 return ravb_hwtstamp_get(ndev, req);
1769 case SIOCSHWTSTAMP:
1770 return ravb_hwtstamp_set(ndev, req);
1771 }
1772
1773 return phy_mii_ioctl(phydev, req, cmd);
1774}
1775
1776static const struct net_device_ops ravb_netdev_ops = {
1777 .ndo_open = ravb_open,
1778 .ndo_stop = ravb_close,
1779 .ndo_start_xmit = ravb_start_xmit,
1780 .ndo_select_queue = ravb_select_queue,
1781 .ndo_get_stats = ravb_get_stats,
1782 .ndo_set_rx_mode = ravb_set_rx_mode,
1783 .ndo_tx_timeout = ravb_tx_timeout,
1784 .ndo_do_ioctl = ravb_do_ioctl,
1785 .ndo_validate_addr = eth_validate_addr,
1786 .ndo_set_mac_address = eth_mac_addr,
1787 .ndo_change_mtu = eth_change_mtu,
1788};
1789
1790/* MDIO bus init function */
1791static int ravb_mdio_init(struct ravb_private *priv)
1792{
1793 struct platform_device *pdev = priv->pdev;
1794 struct device *dev = &pdev->dev;
1795 int error;
1796
1797 /* Bitbang init */
1798 priv->mdiobb.ops = &bb_ops;
1799
1800 /* MII controller setting */
1801 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1802 if (!priv->mii_bus)
1803 return -ENOMEM;
1804
1805 /* Hook up MII support for ethtool */
1806 priv->mii_bus->name = "ravb_mii";
1807 priv->mii_bus->parent = dev;
1808 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1809 pdev->name, pdev->id);
1810
1811 /* Register MDIO bus */
1812 error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1813 if (error)
1814 goto out_free_bus;
1815
1816 return 0;
1817
1818out_free_bus:
1819 free_mdio_bitbang(priv->mii_bus);
1820 return error;
1821}
1822
1823/* MDIO bus release function */
1824static int ravb_mdio_release(struct ravb_private *priv)
1825{
1826 /* Unregister mdio bus */
1827 mdiobus_unregister(priv->mii_bus);
1828
1829 /* Free bitbang info */
1830 free_mdio_bitbang(priv->mii_bus);
1831
1832 return 0;
1833}
1834
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001835static const struct of_device_id ravb_match_table[] = {
1836 { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1837 { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
Simon Horman0e874362015-12-02 14:58:32 +09001838 { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001839 { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
Simon Horman0e874362015-12-02 14:58:32 +09001840 { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001841 { }
1842};
1843MODULE_DEVICE_TABLE(of, ravb_match_table);
1844
Simon Hormanb3d39a82015-11-20 11:29:39 -08001845static int ravb_set_gti(struct net_device *ndev)
1846{
1847
1848 struct device *dev = ndev->dev.parent;
1849 struct device_node *np = dev->of_node;
1850 unsigned long rate;
1851 struct clk *clk;
1852 uint64_t inc;
1853
1854 clk = of_clk_get(np, 0);
1855 if (IS_ERR(clk)) {
1856 dev_err(dev, "could not get clock\n");
1857 return PTR_ERR(clk);
1858 }
1859
1860 rate = clk_get_rate(clk);
1861 clk_put(clk);
1862
Wolfram Sanga6d37132016-04-08 13:28:42 +02001863 if (!rate)
1864 return -EINVAL;
1865
Simon Hormanb3d39a82015-11-20 11:29:39 -08001866 inc = 1000000000ULL << 20;
1867 do_div(inc, rate);
1868
1869 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1870 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1871 inc, GTI_TIV_MIN, GTI_TIV_MAX);
1872 return -EINVAL;
1873 }
1874
1875 ravb_write(ndev, inc, GTI);
1876
1877 return 0;
1878}
1879
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001880static int ravb_probe(struct platform_device *pdev)
1881{
1882 struct device_node *np = pdev->dev.of_node;
1883 struct ravb_private *priv;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001884 enum ravb_chip_id chip_id;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001885 struct net_device *ndev;
1886 int error, irq, q;
1887 struct resource *res;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001888 int i;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001889
1890 if (!np) {
1891 dev_err(&pdev->dev,
1892 "this driver is required to be instantiated from device tree\n");
1893 return -EINVAL;
1894 }
1895
1896 /* Get base address */
1897 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1898 if (!res) {
1899 dev_err(&pdev->dev, "invalid resource\n");
1900 return -EINVAL;
1901 }
1902
1903 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
1904 NUM_TX_QUEUE, NUM_RX_QUEUE);
1905 if (!ndev)
1906 return -ENOMEM;
1907
1908 pm_runtime_enable(&pdev->dev);
1909 pm_runtime_get_sync(&pdev->dev);
1910
1911 /* The Ether-specific entries in the device structure. */
1912 ndev->base_addr = res->start;
1913 ndev->dma = -1;
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001914
Wolfram Sange8668632016-03-01 17:37:58 +01001915 chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001916
1917 if (chip_id == RCAR_GEN3)
1918 irq = platform_get_irq_byname(pdev, "ch22");
1919 else
1920 irq = platform_get_irq(pdev, 0);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001921 if (irq < 0) {
Sergei Shtylyovf3753392015-08-28 16:55:10 +03001922 error = irq;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001923 goto out_release;
1924 }
1925 ndev->irq = irq;
1926
1927 SET_NETDEV_DEV(ndev, &pdev->dev);
1928
1929 priv = netdev_priv(ndev);
1930 priv->ndev = ndev;
1931 priv->pdev = pdev;
1932 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
1933 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
1934 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
1935 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
1936 priv->addr = devm_ioremap_resource(&pdev->dev, res);
1937 if (IS_ERR(priv->addr)) {
1938 error = PTR_ERR(priv->addr);
1939 goto out_release;
1940 }
1941
1942 spin_lock_init(&priv->lock);
1943 INIT_WORK(&priv->work, ravb_tx_timeout_work);
1944
1945 priv->phy_interface = of_get_phy_mode(np);
1946
1947 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
1948 priv->avb_link_active_low =
1949 of_property_read_bool(np, "renesas,ether-link-active-low");
1950
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001951 if (chip_id == RCAR_GEN3) {
1952 irq = platform_get_irq_byname(pdev, "ch24");
1953 if (irq < 0) {
1954 error = irq;
1955 goto out_release;
1956 }
1957 priv->emac_irq = irq;
Kazuya Mizuguchif51bdc22016-04-03 23:54:38 +09001958 for (i = 0; i < NUM_RX_QUEUE; i++) {
1959 irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
1960 if (irq < 0) {
1961 error = irq;
1962 goto out_release;
1963 }
1964 priv->rx_irqs[i] = irq;
1965 }
1966 for (i = 0; i < NUM_TX_QUEUE; i++) {
1967 irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
1968 if (irq < 0) {
1969 error = irq;
1970 goto out_release;
1971 }
1972 priv->tx_irqs[i] = irq;
1973 }
Kazuya Mizuguchi22d4df82015-09-30 15:15:55 +09001974 }
1975
1976 priv->chip_id = chip_id;
1977
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001978 /* Set function */
1979 ndev->netdev_ops = &ravb_netdev_ops;
1980 ndev->ethtool_ops = &ravb_ethtool_ops;
1981
1982 /* Set AVB config mode */
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001983 if (chip_id == RCAR_GEN2) {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001984 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001985 /* Set CSEL value */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001986 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001987 } else {
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001988 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
1989 CCC_GAC | CCC_CSEL_HPB);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09001990 }
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001991
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001992 /* Set GTI value */
Simon Hormanb3d39a82015-11-20 11:29:39 -08001993 error = ravb_set_gti(ndev);
1994 if (error)
1995 goto out_release;
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001996
1997 /* Request GTI loading */
Sergei Shtylyov568b3ce2016-02-10 01:37:44 +03001998 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03001999
2000 /* Allocate descriptor base address table */
2001 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002002 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002003 &priv->desc_bat_dma, GFP_KERNEL);
2004 if (!priv->desc_bat) {
Simon Hormanc4511132015-11-02 10:40:17 +09002005 dev_err(&pdev->dev,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002006 "Cannot allocate desc base address table (size %d bytes)\n",
2007 priv->desc_bat_size);
2008 error = -ENOMEM;
2009 goto out_release;
2010 }
2011 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2012 priv->desc_bat[q].die_dt = DT_EOS;
2013 ravb_write(ndev, priv->desc_bat_dma, DBAT);
2014
2015 /* Initialise HW timestamp list */
2016 INIT_LIST_HEAD(&priv->ts_skb_list);
2017
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002018 /* Initialise PTP Clock driver */
2019 if (chip_id != RCAR_GEN2)
2020 ravb_ptp_init(ndev, pdev);
2021
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002022 /* Debug message level */
2023 priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2024
2025 /* Read and set MAC address */
2026 ravb_read_mac_address(ndev, of_get_mac_address(np));
2027 if (!is_valid_ether_addr(ndev->dev_addr)) {
2028 dev_warn(&pdev->dev,
2029 "no valid MAC address supplied, using a random one\n");
2030 eth_hw_addr_random(ndev);
2031 }
2032
2033 /* MDIO bus init */
2034 error = ravb_mdio_init(priv);
2035 if (error) {
Simon Hormanc4511132015-11-02 10:40:17 +09002036 dev_err(&pdev->dev, "failed to initialize MDIO\n");
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002037 goto out_dma_free;
2038 }
2039
2040 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
2041 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
2042
2043 /* Network device register */
2044 error = register_netdev(ndev);
2045 if (error)
2046 goto out_napi_del;
2047
2048 /* Print device information */
2049 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2050 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2051
2052 platform_set_drvdata(pdev, ndev);
2053
2054 return 0;
2055
2056out_napi_del:
2057 netif_napi_del(&priv->napi[RAVB_NC]);
2058 netif_napi_del(&priv->napi[RAVB_BE]);
2059 ravb_mdio_release(priv);
2060out_dma_free:
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002061 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002062 priv->desc_bat_dma);
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002063
2064 /* Stop PTP Clock driver */
2065 if (chip_id != RCAR_GEN2)
2066 ravb_ptp_stop(ndev);
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002067out_release:
2068 if (ndev)
2069 free_netdev(ndev);
2070
2071 pm_runtime_put(&pdev->dev);
2072 pm_runtime_disable(&pdev->dev);
2073 return error;
2074}
2075
2076static int ravb_remove(struct platform_device *pdev)
2077{
2078 struct net_device *ndev = platform_get_drvdata(pdev);
2079 struct ravb_private *priv = netdev_priv(ndev);
2080
Kazuya Mizuguchif5d78372015-12-02 02:04:39 +09002081 /* Stop PTP Clock driver */
2082 if (priv->chip_id != RCAR_GEN2)
2083 ravb_ptp_stop(ndev);
2084
Kazuya Mizuguchie2dbb332015-09-30 15:15:53 +09002085 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002086 priv->desc_bat_dma);
2087 /* Set reset mode */
2088 ravb_write(ndev, CCC_OPC_RESET, CCC);
2089 pm_runtime_put_sync(&pdev->dev);
2090 unregister_netdev(ndev);
2091 netif_napi_del(&priv->napi[RAVB_NC]);
2092 netif_napi_del(&priv->napi[RAVB_BE]);
2093 ravb_mdio_release(priv);
2094 pm_runtime_disable(&pdev->dev);
2095 free_netdev(ndev);
2096 platform_set_drvdata(pdev, NULL);
2097
2098 return 0;
2099}
2100
2101#ifdef CONFIG_PM
2102static int ravb_runtime_nop(struct device *dev)
2103{
2104 /* Runtime PM callback shared between ->runtime_suspend()
2105 * and ->runtime_resume(). Simply returns success.
2106 *
2107 * This driver re-initializes all registers after
2108 * pm_runtime_get_sync() anyway so there is no need
2109 * to save and restore registers here.
2110 */
2111 return 0;
2112}
2113
2114static const struct dev_pm_ops ravb_dev_pm_ops = {
2115 .runtime_suspend = ravb_runtime_nop,
2116 .runtime_resume = ravb_runtime_nop,
2117};
2118
2119#define RAVB_PM_OPS (&ravb_dev_pm_ops)
2120#else
2121#define RAVB_PM_OPS NULL
2122#endif
2123
Sergei Shtylyovc1566332015-06-11 01:01:43 +03002124static struct platform_driver ravb_driver = {
2125 .probe = ravb_probe,
2126 .remove = ravb_remove,
2127 .driver = {
2128 .name = "ravb",
2129 .pm = RAVB_PM_OPS,
2130 .of_match_table = ravb_match_table,
2131 },
2132};
2133
2134module_platform_driver(ravb_driver);
2135
2136MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2137MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2138MODULE_LICENSE("GPL v2");