1 /* Renesas Ethernet AVB device driver
3 * Copyright (C) 2014-2015 Renesas Electronics Corporation
4 * Copyright (C) 2015 Renesas Solutions Corp.
5 * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
7 * Based on the SuperH Ethernet driver
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.
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>
27 #include <linux/of_device.h>
28 #include <linux/of_irq.h>
29 #include <linux/of_mdio.h>
30 #include <linux/of_net.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
35 #include <asm/div64.h>
39 #define RAVB_DEF_MSG_ENABLE \
45 static const char *ravb_rx_irqs
[NUM_RX_QUEUE
] = {
50 static const char *ravb_tx_irqs
[NUM_TX_QUEUE
] = {
55 void ravb_modify(struct net_device
*ndev
, enum ravb_reg reg
, u32 clear
,
58 ravb_write(ndev
, (ravb_read(ndev
, reg
) & ~clear
) | set
, reg
);
61 int ravb_wait(struct net_device
*ndev
, enum ravb_reg reg
, u32 mask
, u32 value
)
65 for (i
= 0; i
< 10000; i
++) {
66 if ((ravb_read(ndev
, reg
) & mask
) == value
)
73 static int ravb_config(struct net_device
*ndev
)
78 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_CONFIG
);
79 /* Check if the operating mode is changed to the config mode */
80 error
= ravb_wait(ndev
, CSR
, CSR_OPS
, CSR_OPS_CONFIG
);
82 netdev_err(ndev
, "failed to switch device to config mode\n");
87 static void ravb_set_duplex(struct net_device
*ndev
)
89 struct ravb_private
*priv
= netdev_priv(ndev
);
91 ravb_modify(ndev
, ECMR
, ECMR_DM
, priv
->duplex
? ECMR_DM
: 0);
94 static void ravb_set_rate(struct net_device
*ndev
)
96 struct ravb_private
*priv
= netdev_priv(ndev
);
98 switch (priv
->speed
) {
99 case 100: /* 100BASE */
100 ravb_write(ndev
, GECMR_SPEED_100
, GECMR
);
102 case 1000: /* 1000BASE */
103 ravb_write(ndev
, GECMR_SPEED_1000
, GECMR
);
108 static void ravb_set_buffer_align(struct sk_buff
*skb
)
110 u32 reserve
= (unsigned long)skb
->data
& (RAVB_ALIGN
- 1);
113 skb_reserve(skb
, RAVB_ALIGN
- reserve
);
116 /* Get MAC address from the MAC address registers
118 * Ethernet AVB device doesn't have ROM for MAC address.
119 * This function gets the MAC address that was used by a bootloader.
121 static void ravb_read_mac_address(struct net_device
*ndev
, const u8
*mac
)
124 ether_addr_copy(ndev
->dev_addr
, mac
);
126 u32 mahr
= ravb_read(ndev
, MAHR
);
127 u32 malr
= ravb_read(ndev
, MALR
);
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;
138 static void ravb_mdio_ctrl(struct mdiobb_ctrl
*ctrl
, u32 mask
, int set
)
140 struct ravb_private
*priv
= container_of(ctrl
, struct ravb_private
,
143 ravb_modify(priv
->ndev
, PIR
, mask
, set
? mask
: 0);
146 /* MDC pin control */
147 static void ravb_set_mdc(struct mdiobb_ctrl
*ctrl
, int level
)
149 ravb_mdio_ctrl(ctrl
, PIR_MDC
, level
);
152 /* Data I/O pin control */
153 static void ravb_set_mdio_dir(struct mdiobb_ctrl
*ctrl
, int output
)
155 ravb_mdio_ctrl(ctrl
, PIR_MMD
, output
);
159 static void ravb_set_mdio_data(struct mdiobb_ctrl
*ctrl
, int value
)
161 ravb_mdio_ctrl(ctrl
, PIR_MDO
, value
);
165 static int ravb_get_mdio_data(struct mdiobb_ctrl
*ctrl
)
167 struct ravb_private
*priv
= container_of(ctrl
, struct ravb_private
,
170 return (ravb_read(priv
->ndev
, PIR
) & PIR_MDI
) != 0;
173 /* MDIO bus control struct */
174 static 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
,
182 /* Free TX skb function for AVB-IP */
183 static int ravb_tx_free(struct net_device
*ndev
, int q
, bool free_txed_only
)
185 struct ravb_private
*priv
= netdev_priv(ndev
);
186 struct net_device_stats
*stats
= &priv
->stats
[q
];
187 struct ravb_tx_desc
*desc
;
192 for (; priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] > 0; priv
->dirty_tx
[q
]++) {
195 entry
= priv
->dirty_tx
[q
] % (priv
->num_tx_ring
[q
] *
197 desc
= &priv
->tx_ring
[q
][entry
];
198 txed
= desc
->die_dt
== DT_FEMPTY
;
199 if (free_txed_only
&& !txed
)
201 /* Descriptor type must be checked before all other reads */
203 size
= le16_to_cpu(desc
->ds_tagl
) & TX_DS
;
204 /* Free the original skb. */
205 if (priv
->tx_skb
[q
][entry
/ NUM_TX_DESC
]) {
206 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
207 size
, DMA_TO_DEVICE
);
208 /* Last packet descriptor? */
209 if (entry
% NUM_TX_DESC
== NUM_TX_DESC
- 1) {
210 entry
/= NUM_TX_DESC
;
211 dev_kfree_skb_any(priv
->tx_skb
[q
][entry
]);
212 priv
->tx_skb
[q
][entry
] = NULL
;
219 stats
->tx_bytes
+= size
;
220 desc
->die_dt
= DT_EEMPTY
;
225 /* Free skb's and DMA buffers for Ethernet AVB */
226 static void ravb_ring_free(struct net_device
*ndev
, int q
)
228 struct ravb_private
*priv
= netdev_priv(ndev
);
232 /* Free RX skb ringbuffer */
233 if (priv
->rx_skb
[q
]) {
234 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++)
235 dev_kfree_skb(priv
->rx_skb
[q
][i
]);
237 kfree(priv
->rx_skb
[q
]);
238 priv
->rx_skb
[q
] = NULL
;
240 /* Free aligned TX buffers */
241 kfree(priv
->tx_align
[q
]);
242 priv
->tx_align
[q
] = NULL
;
244 if (priv
->rx_ring
[q
]) {
245 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++) {
246 struct ravb_ex_rx_desc
*desc
= &priv
->rx_ring
[q
][i
];
248 if (!dma_mapping_error(ndev
->dev
.parent
,
249 le32_to_cpu(desc
->dptr
)))
250 dma_unmap_single(ndev
->dev
.parent
,
251 le32_to_cpu(desc
->dptr
),
255 ring_size
= sizeof(struct ravb_ex_rx_desc
) *
256 (priv
->num_rx_ring
[q
] + 1);
257 dma_free_coherent(ndev
->dev
.parent
, ring_size
, priv
->rx_ring
[q
],
258 priv
->rx_desc_dma
[q
]);
259 priv
->rx_ring
[q
] = NULL
;
262 if (priv
->tx_ring
[q
]) {
263 ravb_tx_free(ndev
, q
, false);
265 ring_size
= sizeof(struct ravb_tx_desc
) *
266 (priv
->num_tx_ring
[q
] * NUM_TX_DESC
+ 1);
267 dma_free_coherent(ndev
->dev
.parent
, ring_size
, priv
->tx_ring
[q
],
268 priv
->tx_desc_dma
[q
]);
269 priv
->tx_ring
[q
] = NULL
;
272 /* Free TX skb ringbuffer.
273 * SKBs are freed by ravb_tx_free() call above.
275 kfree(priv
->tx_skb
[q
]);
276 priv
->tx_skb
[q
] = NULL
;
279 /* Format skb and descriptor buffer for Ethernet AVB */
280 static void ravb_ring_format(struct net_device
*ndev
, int q
)
282 struct ravb_private
*priv
= netdev_priv(ndev
);
283 struct ravb_ex_rx_desc
*rx_desc
;
284 struct ravb_tx_desc
*tx_desc
;
285 struct ravb_desc
*desc
;
286 int rx_ring_size
= sizeof(*rx_desc
) * priv
->num_rx_ring
[q
];
287 int tx_ring_size
= sizeof(*tx_desc
) * priv
->num_tx_ring
[q
] *
294 priv
->dirty_rx
[q
] = 0;
295 priv
->dirty_tx
[q
] = 0;
297 memset(priv
->rx_ring
[q
], 0, rx_ring_size
);
298 /* Build RX ring buffer */
299 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++) {
301 rx_desc
= &priv
->rx_ring
[q
][i
];
302 rx_desc
->ds_cc
= cpu_to_le16(PKT_BUF_SZ
);
303 dma_addr
= dma_map_single(ndev
->dev
.parent
, priv
->rx_skb
[q
][i
]->data
,
306 /* We just set the data size to 0 for a failed mapping which
307 * should prevent DMA from happening...
309 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
310 rx_desc
->ds_cc
= cpu_to_le16(0);
311 rx_desc
->dptr
= cpu_to_le32(dma_addr
);
312 rx_desc
->die_dt
= DT_FEMPTY
;
314 rx_desc
= &priv
->rx_ring
[q
][i
];
315 rx_desc
->dptr
= cpu_to_le32((u32
)priv
->rx_desc_dma
[q
]);
316 rx_desc
->die_dt
= DT_LINKFIX
; /* type */
318 memset(priv
->tx_ring
[q
], 0, tx_ring_size
);
319 /* Build TX ring buffer */
320 for (i
= 0, tx_desc
= priv
->tx_ring
[q
]; i
< priv
->num_tx_ring
[q
];
322 tx_desc
->die_dt
= DT_EEMPTY
;
324 tx_desc
->die_dt
= DT_EEMPTY
;
326 tx_desc
->dptr
= cpu_to_le32((u32
)priv
->tx_desc_dma
[q
]);
327 tx_desc
->die_dt
= DT_LINKFIX
; /* type */
329 /* RX descriptor base address for best effort */
330 desc
= &priv
->desc_bat
[RX_QUEUE_OFFSET
+ q
];
331 desc
->die_dt
= DT_LINKFIX
; /* type */
332 desc
->dptr
= cpu_to_le32((u32
)priv
->rx_desc_dma
[q
]);
334 /* TX descriptor base address for best effort */
335 desc
= &priv
->desc_bat
[q
];
336 desc
->die_dt
= DT_LINKFIX
; /* type */
337 desc
->dptr
= cpu_to_le32((u32
)priv
->tx_desc_dma
[q
]);
340 /* Init skb and descriptor buffer for Ethernet AVB */
341 static int ravb_ring_init(struct net_device
*ndev
, int q
)
343 struct ravb_private
*priv
= netdev_priv(ndev
);
348 /* Allocate RX and TX skb rings */
349 priv
->rx_skb
[q
] = kcalloc(priv
->num_rx_ring
[q
],
350 sizeof(*priv
->rx_skb
[q
]), GFP_KERNEL
);
351 priv
->tx_skb
[q
] = kcalloc(priv
->num_tx_ring
[q
],
352 sizeof(*priv
->tx_skb
[q
]), GFP_KERNEL
);
353 if (!priv
->rx_skb
[q
] || !priv
->tx_skb
[q
])
356 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++) {
357 skb
= netdev_alloc_skb(ndev
, PKT_BUF_SZ
+ RAVB_ALIGN
- 1);
360 ravb_set_buffer_align(skb
);
361 priv
->rx_skb
[q
][i
] = skb
;
364 /* Allocate rings for the aligned buffers */
365 priv
->tx_align
[q
] = kmalloc(DPTR_ALIGN
* priv
->num_tx_ring
[q
] +
366 DPTR_ALIGN
- 1, GFP_KERNEL
);
367 if (!priv
->tx_align
[q
])
370 /* Allocate all RX descriptors. */
371 ring_size
= sizeof(struct ravb_ex_rx_desc
) * (priv
->num_rx_ring
[q
] + 1);
372 priv
->rx_ring
[q
] = dma_alloc_coherent(ndev
->dev
.parent
, ring_size
,
373 &priv
->rx_desc_dma
[q
],
375 if (!priv
->rx_ring
[q
])
378 priv
->dirty_rx
[q
] = 0;
380 /* Allocate all TX descriptors. */
381 ring_size
= sizeof(struct ravb_tx_desc
) *
382 (priv
->num_tx_ring
[q
] * NUM_TX_DESC
+ 1);
383 priv
->tx_ring
[q
] = dma_alloc_coherent(ndev
->dev
.parent
, ring_size
,
384 &priv
->tx_desc_dma
[q
],
386 if (!priv
->tx_ring
[q
])
392 ravb_ring_free(ndev
, q
);
397 /* E-MAC init function */
398 static void ravb_emac_init(struct net_device
*ndev
)
400 struct ravb_private
*priv
= netdev_priv(ndev
);
402 /* Receive frame limit set register */
403 ravb_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
, RFLR
);
405 /* PAUSE prohibition */
406 ravb_write(ndev
, ECMR_ZPF
| (priv
->duplex
? ECMR_DM
: 0) |
407 ECMR_TE
| ECMR_RE
, ECMR
);
411 /* Set MAC address */
413 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
414 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
416 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
418 /* E-MAC status register clear */
419 ravb_write(ndev
, ECSR_ICD
| ECSR_MPD
, ECSR
);
421 /* E-MAC interrupt enable register */
422 ravb_write(ndev
, ECSIPR_ICDIP
| ECSIPR_MPDIP
| ECSIPR_LCHNGIP
, ECSIPR
);
425 /* Device init function for Ethernet AVB */
426 static int ravb_dmac_init(struct net_device
*ndev
)
428 struct ravb_private
*priv
= netdev_priv(ndev
);
431 /* Set CONFIG mode */
432 error
= ravb_config(ndev
);
436 error
= ravb_ring_init(ndev
, RAVB_BE
);
439 error
= ravb_ring_init(ndev
, RAVB_NC
);
441 ravb_ring_free(ndev
, RAVB_BE
);
445 /* Descriptor format */
446 ravb_ring_format(ndev
, RAVB_BE
);
447 ravb_ring_format(ndev
, RAVB_NC
);
449 #if defined(__LITTLE_ENDIAN)
450 ravb_modify(ndev
, CCC
, CCC_BOC
, 0);
452 ravb_modify(ndev
, CCC
, CCC_BOC
, CCC_BOC
);
457 RCR_EFFS
| RCR_ENCF
| RCR_ETS0
| RCR_ESF
| 0x18000000, RCR
);
460 ravb_write(ndev
, TGC_TQP_AVBMODE1
| 0x00222200, TGC
);
462 /* Timestamp enable */
463 ravb_write(ndev
, TCCR_TFEN
, TCCR
);
465 /* Interrupt init: */
466 if (priv
->chip_id
== RCAR_GEN3
) {
468 ravb_write(ndev
, 0, DIL
);
469 /* Set queue specific interrupt */
470 ravb_write(ndev
, CIE_CRIE
| CIE_CTIE
| CIE_CL0M
, CIE
);
473 ravb_write(ndev
, RIC0_FRE0
| RIC0_FRE1
, RIC0
);
474 /* Disable FIFO full warning */
475 ravb_write(ndev
, 0, RIC1
);
476 /* Receive FIFO full error, descriptor empty */
477 ravb_write(ndev
, RIC2_QFE0
| RIC2_QFE1
| RIC2_RFFE
, RIC2
);
478 /* Frame transmitted, timestamp FIFO updated */
479 ravb_write(ndev
, TIC_FTE0
| TIC_FTE1
| TIC_TFUE
, TIC
);
481 /* Setting the control will start the AVB-DMAC process. */
482 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_OPERATION
);
487 static void ravb_get_tx_tstamp(struct net_device
*ndev
)
489 struct ravb_private
*priv
= netdev_priv(ndev
);
490 struct ravb_tstamp_skb
*ts_skb
, *ts_skb2
;
491 struct skb_shared_hwtstamps shhwtstamps
;
493 struct timespec64 ts
;
498 count
= (ravb_read(ndev
, TSR
) & TSR_TFFL
) >> 8;
500 tfa2
= ravb_read(ndev
, TFA2
);
501 tfa_tag
= (tfa2
& TFA2_TST
) >> 16;
502 ts
.tv_nsec
= (u64
)ravb_read(ndev
, TFA0
);
503 ts
.tv_sec
= ((u64
)(tfa2
& TFA2_TSV
) << 32) |
504 ravb_read(ndev
, TFA1
);
505 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
506 shhwtstamps
.hwtstamp
= timespec64_to_ktime(ts
);
507 list_for_each_entry_safe(ts_skb
, ts_skb2
, &priv
->ts_skb_list
,
511 list_del(&ts_skb
->list
);
513 if (tag
== tfa_tag
) {
514 skb_tstamp_tx(skb
, &shhwtstamps
);
518 ravb_modify(ndev
, TCCR
, TCCR_TFR
, TCCR_TFR
);
522 /* Packet receive function for Ethernet AVB */
523 static bool ravb_rx(struct net_device
*ndev
, int *quota
, int q
)
525 struct ravb_private
*priv
= netdev_priv(ndev
);
526 int entry
= priv
->cur_rx
[q
] % priv
->num_rx_ring
[q
];
527 int boguscnt
= (priv
->dirty_rx
[q
] + priv
->num_rx_ring
[q
]) -
529 struct net_device_stats
*stats
= &priv
->stats
[q
];
530 struct ravb_ex_rx_desc
*desc
;
533 struct timespec64 ts
;
538 boguscnt
= min(boguscnt
, *quota
);
540 desc
= &priv
->rx_ring
[q
][entry
];
541 while (desc
->die_dt
!= DT_FEMPTY
) {
542 /* Descriptor type must be checked before all other reads */
544 desc_status
= desc
->msc
;
545 pkt_len
= le16_to_cpu(desc
->ds_cc
) & RX_DS
;
550 /* We use 0-byte descriptors to mark the DMA mapping errors */
554 if (desc_status
& MSC_MC
)
557 if (desc_status
& (MSC_CRC
| MSC_RFE
| MSC_RTSF
| MSC_RTLF
|
560 if (desc_status
& MSC_CRC
)
561 stats
->rx_crc_errors
++;
562 if (desc_status
& MSC_RFE
)
563 stats
->rx_frame_errors
++;
564 if (desc_status
& (MSC_RTLF
| MSC_RTSF
))
565 stats
->rx_length_errors
++;
566 if (desc_status
& MSC_CEEF
)
567 stats
->rx_missed_errors
++;
569 u32 get_ts
= priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE
;
571 skb
= priv
->rx_skb
[q
][entry
];
572 priv
->rx_skb
[q
][entry
] = NULL
;
573 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
576 get_ts
&= (q
== RAVB_NC
) ?
577 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
:
578 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
;
580 struct skb_shared_hwtstamps
*shhwtstamps
;
582 shhwtstamps
= skb_hwtstamps(skb
);
583 memset(shhwtstamps
, 0, sizeof(*shhwtstamps
));
584 ts
.tv_sec
= ((u64
) le16_to_cpu(desc
->ts_sh
) <<
585 32) | le32_to_cpu(desc
->ts_sl
);
586 ts
.tv_nsec
= le32_to_cpu(desc
->ts_n
);
587 shhwtstamps
->hwtstamp
= timespec64_to_ktime(ts
);
589 skb_put(skb
, pkt_len
);
590 skb
->protocol
= eth_type_trans(skb
, ndev
);
591 napi_gro_receive(&priv
->napi
[q
], skb
);
593 stats
->rx_bytes
+= pkt_len
;
596 entry
= (++priv
->cur_rx
[q
]) % priv
->num_rx_ring
[q
];
597 desc
= &priv
->rx_ring
[q
][entry
];
600 /* Refill the RX ring buffers. */
601 for (; priv
->cur_rx
[q
] - priv
->dirty_rx
[q
] > 0; priv
->dirty_rx
[q
]++) {
602 entry
= priv
->dirty_rx
[q
] % priv
->num_rx_ring
[q
];
603 desc
= &priv
->rx_ring
[q
][entry
];
604 desc
->ds_cc
= cpu_to_le16(PKT_BUF_SZ
);
606 if (!priv
->rx_skb
[q
][entry
]) {
607 skb
= netdev_alloc_skb(ndev
,
608 PKT_BUF_SZ
+ RAVB_ALIGN
- 1);
610 break; /* Better luck next round. */
611 ravb_set_buffer_align(skb
);
612 dma_addr
= dma_map_single(ndev
->dev
.parent
, skb
->data
,
613 le16_to_cpu(desc
->ds_cc
),
615 skb_checksum_none_assert(skb
);
616 /* We just set the data size to 0 for a failed mapping
617 * which should prevent DMA from happening...
619 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
620 desc
->ds_cc
= cpu_to_le16(0);
621 desc
->dptr
= cpu_to_le32(dma_addr
);
622 priv
->rx_skb
[q
][entry
] = skb
;
624 /* Descriptor type must be set after all the above writes */
626 desc
->die_dt
= DT_FEMPTY
;
629 *quota
-= limit
- (++boguscnt
);
631 return boguscnt
<= 0;
634 static void ravb_rcv_snd_disable(struct net_device
*ndev
)
636 /* Disable TX and RX */
637 ravb_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, 0);
640 static void ravb_rcv_snd_enable(struct net_device
*ndev
)
642 /* Enable TX and RX */
643 ravb_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, ECMR_RE
| ECMR_TE
);
646 /* function for waiting dma process finished */
647 static int ravb_stop_dma(struct net_device
*ndev
)
651 /* Wait for stopping the hardware TX process */
652 error
= ravb_wait(ndev
, TCCR
,
653 TCCR_TSRQ0
| TCCR_TSRQ1
| TCCR_TSRQ2
| TCCR_TSRQ3
, 0);
657 error
= ravb_wait(ndev
, CSR
, CSR_TPO0
| CSR_TPO1
| CSR_TPO2
| CSR_TPO3
,
662 /* Stop the E-MAC's RX/TX processes. */
663 ravb_rcv_snd_disable(ndev
);
665 /* Wait for stopping the RX DMA process */
666 error
= ravb_wait(ndev
, CSR
, CSR_RPO
, 0);
670 /* Stop AVB-DMAC process */
671 return ravb_config(ndev
);
674 /* E-MAC interrupt handler */
675 static void ravb_emac_interrupt_unlocked(struct net_device
*ndev
)
677 struct ravb_private
*priv
= netdev_priv(ndev
);
680 ecsr
= ravb_read(ndev
, ECSR
);
681 ravb_write(ndev
, ecsr
, ECSR
); /* clear interrupt */
683 ndev
->stats
.tx_carrier_errors
++;
684 if (ecsr
& ECSR_LCHNG
) {
686 if (priv
->no_avb_link
)
688 psr
= ravb_read(ndev
, PSR
);
689 if (priv
->avb_link_active_low
)
691 if (!(psr
& PSR_LMON
)) {
692 /* DIsable RX and TX */
693 ravb_rcv_snd_disable(ndev
);
695 /* Enable RX and TX */
696 ravb_rcv_snd_enable(ndev
);
701 static irqreturn_t
ravb_emac_interrupt(int irq
, void *dev_id
)
703 struct net_device
*ndev
= dev_id
;
704 struct ravb_private
*priv
= netdev_priv(ndev
);
706 spin_lock(&priv
->lock
);
707 ravb_emac_interrupt_unlocked(ndev
);
709 spin_unlock(&priv
->lock
);
713 /* Error interrupt handler */
714 static void ravb_error_interrupt(struct net_device
*ndev
)
716 struct ravb_private
*priv
= netdev_priv(ndev
);
719 eis
= ravb_read(ndev
, EIS
);
720 ravb_write(ndev
, ~EIS_QFS
, EIS
);
722 ris2
= ravb_read(ndev
, RIS2
);
723 ravb_write(ndev
, ~(RIS2_QFF0
| RIS2_RFFF
), RIS2
);
725 /* Receive Descriptor Empty int */
726 if (ris2
& RIS2_QFF0
)
727 priv
->stats
[RAVB_BE
].rx_over_errors
++;
729 /* Receive Descriptor Empty int */
730 if (ris2
& RIS2_QFF1
)
731 priv
->stats
[RAVB_NC
].rx_over_errors
++;
733 /* Receive FIFO Overflow int */
734 if (ris2
& RIS2_RFFF
)
735 priv
->rx_fifo_errors
++;
739 static bool ravb_queue_interrupt(struct net_device
*ndev
, int q
)
741 struct ravb_private
*priv
= netdev_priv(ndev
);
742 u32 ris0
= ravb_read(ndev
, RIS0
);
743 u32 ric0
= ravb_read(ndev
, RIC0
);
744 u32 tis
= ravb_read(ndev
, TIS
);
745 u32 tic
= ravb_read(ndev
, TIC
);
747 if (((ris0
& ric0
) & BIT(q
)) || ((tis
& tic
) & BIT(q
))) {
748 if (napi_schedule_prep(&priv
->napi
[q
])) {
749 /* Mask RX and TX interrupts */
750 if (priv
->chip_id
== RCAR_GEN2
) {
751 ravb_write(ndev
, ric0
& ~BIT(q
), RIC0
);
752 ravb_write(ndev
, tic
& ~BIT(q
), TIC
);
754 ravb_write(ndev
, BIT(q
), RID0
);
755 ravb_write(ndev
, BIT(q
), TID
);
757 __napi_schedule(&priv
->napi
[q
]);
760 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
763 " tx status 0x%08x, tx mask 0x%08x.\n",
771 static bool ravb_timestamp_interrupt(struct net_device
*ndev
)
773 u32 tis
= ravb_read(ndev
, TIS
);
775 if (tis
& TIS_TFUF
) {
776 ravb_write(ndev
, ~TIS_TFUF
, TIS
);
777 ravb_get_tx_tstamp(ndev
);
783 static irqreturn_t
ravb_interrupt(int irq
, void *dev_id
)
785 struct net_device
*ndev
= dev_id
;
786 struct ravb_private
*priv
= netdev_priv(ndev
);
787 irqreturn_t result
= IRQ_NONE
;
790 spin_lock(&priv
->lock
);
791 /* Get interrupt status */
792 iss
= ravb_read(ndev
, ISS
);
794 /* Received and transmitted interrupts */
795 if (iss
& (ISS_FRS
| ISS_FTS
| ISS_TFUS
)) {
798 /* Timestamp updated */
799 if (ravb_timestamp_interrupt(ndev
))
800 result
= IRQ_HANDLED
;
802 /* Network control and best effort queue RX/TX */
803 for (q
= RAVB_NC
; q
>= RAVB_BE
; q
--) {
804 if (ravb_queue_interrupt(ndev
, q
))
805 result
= IRQ_HANDLED
;
809 /* E-MAC status summary */
811 ravb_emac_interrupt_unlocked(ndev
);
812 result
= IRQ_HANDLED
;
815 /* Error status summary */
817 ravb_error_interrupt(ndev
);
818 result
= IRQ_HANDLED
;
821 /* gPTP interrupt status summary */
822 if (iss
& ISS_CGIS
) {
823 ravb_ptp_interrupt(ndev
);
824 result
= IRQ_HANDLED
;
828 spin_unlock(&priv
->lock
);
832 /* Timestamp/Error/gPTP interrupt handler */
833 static irqreturn_t
ravb_multi_interrupt(int irq
, void *dev_id
)
835 struct net_device
*ndev
= dev_id
;
836 struct ravb_private
*priv
= netdev_priv(ndev
);
837 irqreturn_t result
= IRQ_NONE
;
840 spin_lock(&priv
->lock
);
841 /* Get interrupt status */
842 iss
= ravb_read(ndev
, ISS
);
844 /* Timestamp updated */
845 if ((iss
& ISS_TFUS
) && ravb_timestamp_interrupt(ndev
))
846 result
= IRQ_HANDLED
;
848 /* Error status summary */
850 ravb_error_interrupt(ndev
);
851 result
= IRQ_HANDLED
;
854 /* gPTP interrupt status summary */
855 if (iss
& ISS_CGIS
) {
856 ravb_ptp_interrupt(ndev
);
857 result
= IRQ_HANDLED
;
861 spin_unlock(&priv
->lock
);
865 static irqreturn_t
ravb_dma_interrupt(int irq
, void *dev_id
, int q
)
867 struct net_device
*ndev
= dev_id
;
868 struct ravb_private
*priv
= netdev_priv(ndev
);
869 irqreturn_t result
= IRQ_NONE
;
871 spin_lock(&priv
->lock
);
873 /* Network control/Best effort queue RX/TX */
874 if (ravb_queue_interrupt(ndev
, q
))
875 result
= IRQ_HANDLED
;
878 spin_unlock(&priv
->lock
);
882 static irqreturn_t
ravb_be_interrupt(int irq
, void *dev_id
)
884 return ravb_dma_interrupt(irq
, dev_id
, RAVB_BE
);
887 static irqreturn_t
ravb_nc_interrupt(int irq
, void *dev_id
)
889 return ravb_dma_interrupt(irq
, dev_id
, RAVB_NC
);
892 static int ravb_poll(struct napi_struct
*napi
, int budget
)
894 struct net_device
*ndev
= napi
->dev
;
895 struct ravb_private
*priv
= netdev_priv(ndev
);
897 int q
= napi
- priv
->napi
;
903 tis
= ravb_read(ndev
, TIS
);
904 ris0
= ravb_read(ndev
, RIS0
);
905 if (!((ris0
& mask
) || (tis
& mask
)))
908 /* Processing RX Descriptor Ring */
910 /* Clear RX interrupt */
911 ravb_write(ndev
, ~mask
, RIS0
);
912 if (ravb_rx(ndev
, "a
, q
))
915 /* Processing TX Descriptor Ring */
917 spin_lock_irqsave(&priv
->lock
, flags
);
918 /* Clear TX interrupt */
919 ravb_write(ndev
, ~mask
, TIS
);
920 ravb_tx_free(ndev
, q
, true);
921 netif_wake_subqueue(ndev
, q
);
923 spin_unlock_irqrestore(&priv
->lock
, flags
);
929 /* Re-enable RX/TX interrupts */
930 spin_lock_irqsave(&priv
->lock
, flags
);
931 if (priv
->chip_id
== RCAR_GEN2
) {
932 ravb_modify(ndev
, RIC0
, mask
, mask
);
933 ravb_modify(ndev
, TIC
, mask
, mask
);
935 ravb_write(ndev
, mask
, RIE0
);
936 ravb_write(ndev
, mask
, TIE
);
939 spin_unlock_irqrestore(&priv
->lock
, flags
);
941 /* Receive error message handling */
942 priv
->rx_over_errors
= priv
->stats
[RAVB_BE
].rx_over_errors
;
943 priv
->rx_over_errors
+= priv
->stats
[RAVB_NC
].rx_over_errors
;
944 if (priv
->rx_over_errors
!= ndev
->stats
.rx_over_errors
) {
945 ndev
->stats
.rx_over_errors
= priv
->rx_over_errors
;
946 netif_err(priv
, rx_err
, ndev
, "Receive Descriptor Empty\n");
948 if (priv
->rx_fifo_errors
!= ndev
->stats
.rx_fifo_errors
) {
949 ndev
->stats
.rx_fifo_errors
= priv
->rx_fifo_errors
;
950 netif_err(priv
, rx_err
, ndev
, "Receive FIFO Overflow\n");
953 return budget
- quota
;
956 /* PHY state control function */
957 static void ravb_adjust_link(struct net_device
*ndev
)
959 struct ravb_private
*priv
= netdev_priv(ndev
);
960 struct phy_device
*phydev
= ndev
->phydev
;
961 bool new_state
= false;
964 if (phydev
->duplex
!= priv
->duplex
) {
966 priv
->duplex
= phydev
->duplex
;
967 ravb_set_duplex(ndev
);
970 if (phydev
->speed
!= priv
->speed
) {
972 priv
->speed
= phydev
->speed
;
976 ravb_modify(ndev
, ECMR
, ECMR_TXF
, 0);
978 priv
->link
= phydev
->link
;
979 if (priv
->no_avb_link
)
980 ravb_rcv_snd_enable(ndev
);
982 } else if (priv
->link
) {
987 if (priv
->no_avb_link
)
988 ravb_rcv_snd_disable(ndev
);
991 if (new_state
&& netif_msg_link(priv
))
992 phy_print_status(phydev
);
995 /* PHY init function */
996 static int ravb_phy_init(struct net_device
*ndev
)
998 struct device_node
*np
= ndev
->dev
.parent
->of_node
;
999 struct ravb_private
*priv
= netdev_priv(ndev
);
1000 struct phy_device
*phydev
;
1001 struct device_node
*pn
;
1008 /* Try connecting to PHY */
1009 pn
= of_parse_phandle(np
, "phy-handle", 0);
1011 /* In the case of a fixed PHY, the DT node associated
1012 * to the PHY is the Ethernet MAC DT node.
1014 if (of_phy_is_fixed_link(np
)) {
1015 err
= of_phy_register_fixed_link(np
);
1019 pn
= of_node_get(np
);
1021 phydev
= of_phy_connect(ndev
, pn
, ravb_adjust_link
, 0,
1022 priv
->phy_interface
);
1025 netdev_err(ndev
, "failed to connect PHY\n");
1027 goto err_deregister_fixed_link
;
1030 /* This driver only support 10/100Mbit speeds on Gen3
1033 if (priv
->chip_id
== RCAR_GEN3
) {
1034 err
= phy_set_max_speed(phydev
, SPEED_100
);
1036 netdev_err(ndev
, "failed to limit PHY to 100Mbit/s\n");
1037 goto err_phy_disconnect
;
1040 netdev_info(ndev
, "limited PHY to 100Mbit/s\n");
1043 /* 10BASE is not supported */
1044 phydev
->supported
&= ~PHY_10BT_FEATURES
;
1046 phy_attached_info(phydev
);
1051 phy_disconnect(phydev
);
1052 err_deregister_fixed_link
:
1053 if (of_phy_is_fixed_link(np
))
1054 of_phy_deregister_fixed_link(np
);
1059 /* PHY control start function */
1060 static int ravb_phy_start(struct net_device
*ndev
)
1064 error
= ravb_phy_init(ndev
);
1068 phy_start(ndev
->phydev
);
1073 static int ravb_get_link_ksettings(struct net_device
*ndev
,
1074 struct ethtool_link_ksettings
*cmd
)
1076 struct ravb_private
*priv
= netdev_priv(ndev
);
1077 int error
= -ENODEV
;
1078 unsigned long flags
;
1081 spin_lock_irqsave(&priv
->lock
, flags
);
1082 error
= phy_ethtool_ksettings_get(ndev
->phydev
, cmd
);
1083 spin_unlock_irqrestore(&priv
->lock
, flags
);
1089 static int ravb_set_link_ksettings(struct net_device
*ndev
,
1090 const struct ethtool_link_ksettings
*cmd
)
1092 struct ravb_private
*priv
= netdev_priv(ndev
);
1093 unsigned long flags
;
1099 spin_lock_irqsave(&priv
->lock
, flags
);
1101 /* Disable TX and RX */
1102 ravb_rcv_snd_disable(ndev
);
1104 error
= phy_ethtool_ksettings_set(ndev
->phydev
, cmd
);
1108 if (cmd
->base
.duplex
== DUPLEX_FULL
)
1113 ravb_set_duplex(ndev
);
1118 /* Enable TX and RX */
1119 ravb_rcv_snd_enable(ndev
);
1122 spin_unlock_irqrestore(&priv
->lock
, flags
);
1127 static int ravb_nway_reset(struct net_device
*ndev
)
1129 struct ravb_private
*priv
= netdev_priv(ndev
);
1130 int error
= -ENODEV
;
1131 unsigned long flags
;
1134 spin_lock_irqsave(&priv
->lock
, flags
);
1135 error
= phy_start_aneg(ndev
->phydev
);
1136 spin_unlock_irqrestore(&priv
->lock
, flags
);
1142 static u32
ravb_get_msglevel(struct net_device
*ndev
)
1144 struct ravb_private
*priv
= netdev_priv(ndev
);
1146 return priv
->msg_enable
;
1149 static void ravb_set_msglevel(struct net_device
*ndev
, u32 value
)
1151 struct ravb_private
*priv
= netdev_priv(ndev
);
1153 priv
->msg_enable
= value
;
1156 static const char ravb_gstrings_stats
[][ETH_GSTRING_LEN
] = {
1157 "rx_queue_0_current",
1158 "tx_queue_0_current",
1161 "rx_queue_0_packets",
1162 "tx_queue_0_packets",
1165 "rx_queue_0_mcast_packets",
1166 "rx_queue_0_errors",
1167 "rx_queue_0_crc_errors",
1168 "rx_queue_0_frame_errors",
1169 "rx_queue_0_length_errors",
1170 "rx_queue_0_missed_errors",
1171 "rx_queue_0_over_errors",
1173 "rx_queue_1_current",
1174 "tx_queue_1_current",
1177 "rx_queue_1_packets",
1178 "tx_queue_1_packets",
1181 "rx_queue_1_mcast_packets",
1182 "rx_queue_1_errors",
1183 "rx_queue_1_crc_errors",
1184 "rx_queue_1_frame_errors",
1185 "rx_queue_1_length_errors",
1186 "rx_queue_1_missed_errors",
1187 "rx_queue_1_over_errors",
1190 #define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1192 static int ravb_get_sset_count(struct net_device
*netdev
, int sset
)
1196 return RAVB_STATS_LEN
;
1202 static void ravb_get_ethtool_stats(struct net_device
*ndev
,
1203 struct ethtool_stats
*stats
, u64
*data
)
1205 struct ravb_private
*priv
= netdev_priv(ndev
);
1209 /* Device-specific stats */
1210 for (q
= RAVB_BE
; q
< NUM_RX_QUEUE
; q
++) {
1211 struct net_device_stats
*stats
= &priv
->stats
[q
];
1213 data
[i
++] = priv
->cur_rx
[q
];
1214 data
[i
++] = priv
->cur_tx
[q
];
1215 data
[i
++] = priv
->dirty_rx
[q
];
1216 data
[i
++] = priv
->dirty_tx
[q
];
1217 data
[i
++] = stats
->rx_packets
;
1218 data
[i
++] = stats
->tx_packets
;
1219 data
[i
++] = stats
->rx_bytes
;
1220 data
[i
++] = stats
->tx_bytes
;
1221 data
[i
++] = stats
->multicast
;
1222 data
[i
++] = stats
->rx_errors
;
1223 data
[i
++] = stats
->rx_crc_errors
;
1224 data
[i
++] = stats
->rx_frame_errors
;
1225 data
[i
++] = stats
->rx_length_errors
;
1226 data
[i
++] = stats
->rx_missed_errors
;
1227 data
[i
++] = stats
->rx_over_errors
;
1231 static void ravb_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
1233 switch (stringset
) {
1235 memcpy(data
, *ravb_gstrings_stats
, sizeof(ravb_gstrings_stats
));
1240 static void ravb_get_ringparam(struct net_device
*ndev
,
1241 struct ethtool_ringparam
*ring
)
1243 struct ravb_private
*priv
= netdev_priv(ndev
);
1245 ring
->rx_max_pending
= BE_RX_RING_MAX
;
1246 ring
->tx_max_pending
= BE_TX_RING_MAX
;
1247 ring
->rx_pending
= priv
->num_rx_ring
[RAVB_BE
];
1248 ring
->tx_pending
= priv
->num_tx_ring
[RAVB_BE
];
1251 static int ravb_set_ringparam(struct net_device
*ndev
,
1252 struct ethtool_ringparam
*ring
)
1254 struct ravb_private
*priv
= netdev_priv(ndev
);
1257 if (ring
->tx_pending
> BE_TX_RING_MAX
||
1258 ring
->rx_pending
> BE_RX_RING_MAX
||
1259 ring
->tx_pending
< BE_TX_RING_MIN
||
1260 ring
->rx_pending
< BE_RX_RING_MIN
)
1262 if (ring
->rx_mini_pending
|| ring
->rx_jumbo_pending
)
1265 if (netif_running(ndev
)) {
1266 netif_device_detach(ndev
);
1267 /* Stop PTP Clock driver */
1268 if (priv
->chip_id
== RCAR_GEN2
)
1269 ravb_ptp_stop(ndev
);
1270 /* Wait for DMA stopping */
1271 error
= ravb_stop_dma(ndev
);
1274 "cannot set ringparam! Any AVB processes are still running?\n");
1277 synchronize_irq(ndev
->irq
);
1279 /* Free all the skb's in the RX queue and the DMA buffers. */
1280 ravb_ring_free(ndev
, RAVB_BE
);
1281 ravb_ring_free(ndev
, RAVB_NC
);
1284 /* Set new parameters */
1285 priv
->num_rx_ring
[RAVB_BE
] = ring
->rx_pending
;
1286 priv
->num_tx_ring
[RAVB_BE
] = ring
->tx_pending
;
1288 if (netif_running(ndev
)) {
1289 error
= ravb_dmac_init(ndev
);
1292 "%s: ravb_dmac_init() failed, error %d\n",
1297 ravb_emac_init(ndev
);
1299 /* Initialise PTP Clock driver */
1300 if (priv
->chip_id
== RCAR_GEN2
)
1301 ravb_ptp_init(ndev
, priv
->pdev
);
1303 netif_device_attach(ndev
);
1309 static int ravb_get_ts_info(struct net_device
*ndev
,
1310 struct ethtool_ts_info
*info
)
1312 struct ravb_private
*priv
= netdev_priv(ndev
);
1314 info
->so_timestamping
=
1315 SOF_TIMESTAMPING_TX_SOFTWARE
|
1316 SOF_TIMESTAMPING_RX_SOFTWARE
|
1317 SOF_TIMESTAMPING_SOFTWARE
|
1318 SOF_TIMESTAMPING_TX_HARDWARE
|
1319 SOF_TIMESTAMPING_RX_HARDWARE
|
1320 SOF_TIMESTAMPING_RAW_HARDWARE
;
1321 info
->tx_types
= (1 << HWTSTAMP_TX_OFF
) | (1 << HWTSTAMP_TX_ON
);
1323 (1 << HWTSTAMP_FILTER_NONE
) |
1324 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT
) |
1325 (1 << HWTSTAMP_FILTER_ALL
);
1326 info
->phc_index
= ptp_clock_index(priv
->ptp
.clock
);
1331 static const struct ethtool_ops ravb_ethtool_ops
= {
1332 .nway_reset
= ravb_nway_reset
,
1333 .get_msglevel
= ravb_get_msglevel
,
1334 .set_msglevel
= ravb_set_msglevel
,
1335 .get_link
= ethtool_op_get_link
,
1336 .get_strings
= ravb_get_strings
,
1337 .get_ethtool_stats
= ravb_get_ethtool_stats
,
1338 .get_sset_count
= ravb_get_sset_count
,
1339 .get_ringparam
= ravb_get_ringparam
,
1340 .set_ringparam
= ravb_set_ringparam
,
1341 .get_ts_info
= ravb_get_ts_info
,
1342 .get_link_ksettings
= ravb_get_link_ksettings
,
1343 .set_link_ksettings
= ravb_set_link_ksettings
,
1346 static inline int ravb_hook_irq(unsigned int irq
, irq_handler_t handler
,
1347 struct net_device
*ndev
, struct device
*dev
,
1353 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s:%s", ndev
->name
, ch
);
1356 error
= request_irq(irq
, handler
, 0, name
, ndev
);
1358 netdev_err(ndev
, "cannot request IRQ %s\n", name
);
1363 /* Network device open function for Ethernet AVB */
1364 static int ravb_open(struct net_device
*ndev
)
1366 struct ravb_private
*priv
= netdev_priv(ndev
);
1367 struct platform_device
*pdev
= priv
->pdev
;
1368 struct device
*dev
= &pdev
->dev
;
1371 napi_enable(&priv
->napi
[RAVB_BE
]);
1372 napi_enable(&priv
->napi
[RAVB_NC
]);
1374 if (priv
->chip_id
== RCAR_GEN2
) {
1375 error
= request_irq(ndev
->irq
, ravb_interrupt
, IRQF_SHARED
,
1378 netdev_err(ndev
, "cannot request IRQ\n");
1382 error
= ravb_hook_irq(ndev
->irq
, ravb_multi_interrupt
, ndev
,
1386 error
= ravb_hook_irq(priv
->emac_irq
, ravb_emac_interrupt
, ndev
,
1390 error
= ravb_hook_irq(priv
->rx_irqs
[RAVB_BE
], ravb_be_interrupt
,
1391 ndev
, dev
, "ch0:rx_be");
1393 goto out_free_irq_emac
;
1394 error
= ravb_hook_irq(priv
->tx_irqs
[RAVB_BE
], ravb_be_interrupt
,
1395 ndev
, dev
, "ch18:tx_be");
1397 goto out_free_irq_be_rx
;
1398 error
= ravb_hook_irq(priv
->rx_irqs
[RAVB_NC
], ravb_nc_interrupt
,
1399 ndev
, dev
, "ch1:rx_nc");
1401 goto out_free_irq_be_tx
;
1402 error
= ravb_hook_irq(priv
->tx_irqs
[RAVB_NC
], ravb_nc_interrupt
,
1403 ndev
, dev
, "ch19:tx_nc");
1405 goto out_free_irq_nc_rx
;
1409 error
= ravb_dmac_init(ndev
);
1411 goto out_free_irq_nc_tx
;
1412 ravb_emac_init(ndev
);
1414 /* Initialise PTP Clock driver */
1415 if (priv
->chip_id
== RCAR_GEN2
)
1416 ravb_ptp_init(ndev
, priv
->pdev
);
1418 netif_tx_start_all_queues(ndev
);
1420 /* PHY control start */
1421 error
= ravb_phy_start(ndev
);
1428 /* Stop PTP Clock driver */
1429 if (priv
->chip_id
== RCAR_GEN2
)
1430 ravb_ptp_stop(ndev
);
1432 if (priv
->chip_id
== RCAR_GEN2
)
1434 free_irq(priv
->tx_irqs
[RAVB_NC
], ndev
);
1436 free_irq(priv
->rx_irqs
[RAVB_NC
], ndev
);
1438 free_irq(priv
->tx_irqs
[RAVB_BE
], ndev
);
1440 free_irq(priv
->rx_irqs
[RAVB_BE
], ndev
);
1442 free_irq(priv
->emac_irq
, ndev
);
1444 free_irq(ndev
->irq
, ndev
);
1446 napi_disable(&priv
->napi
[RAVB_NC
]);
1447 napi_disable(&priv
->napi
[RAVB_BE
]);
1451 /* Timeout function for Ethernet AVB */
1452 static void ravb_tx_timeout(struct net_device
*ndev
)
1454 struct ravb_private
*priv
= netdev_priv(ndev
);
1456 netif_err(priv
, tx_err
, ndev
,
1457 "transmit timed out, status %08x, resetting...\n",
1458 ravb_read(ndev
, ISS
));
1460 /* tx_errors count up */
1461 ndev
->stats
.tx_errors
++;
1463 schedule_work(&priv
->work
);
1466 static void ravb_tx_timeout_work(struct work_struct
*work
)
1468 struct ravb_private
*priv
= container_of(work
, struct ravb_private
,
1470 struct net_device
*ndev
= priv
->ndev
;
1472 netif_tx_stop_all_queues(ndev
);
1474 /* Stop PTP Clock driver */
1475 if (priv
->chip_id
== RCAR_GEN2
)
1476 ravb_ptp_stop(ndev
);
1478 /* Wait for DMA stopping */
1479 ravb_stop_dma(ndev
);
1481 ravb_ring_free(ndev
, RAVB_BE
);
1482 ravb_ring_free(ndev
, RAVB_NC
);
1485 ravb_dmac_init(ndev
);
1486 ravb_emac_init(ndev
);
1488 /* Initialise PTP Clock driver */
1489 if (priv
->chip_id
== RCAR_GEN2
)
1490 ravb_ptp_init(ndev
, priv
->pdev
);
1492 netif_tx_start_all_queues(ndev
);
1495 /* Packet transmit function for Ethernet AVB */
1496 static netdev_tx_t
ravb_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1498 struct ravb_private
*priv
= netdev_priv(ndev
);
1499 u16 q
= skb_get_queue_mapping(skb
);
1500 struct ravb_tstamp_skb
*ts_skb
;
1501 struct ravb_tx_desc
*desc
;
1502 unsigned long flags
;
1508 spin_lock_irqsave(&priv
->lock
, flags
);
1509 if (priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] > (priv
->num_tx_ring
[q
] - 1) *
1511 netif_err(priv
, tx_queued
, ndev
,
1512 "still transmitting with the full ring!\n");
1513 netif_stop_subqueue(ndev
, q
);
1514 spin_unlock_irqrestore(&priv
->lock
, flags
);
1515 return NETDEV_TX_BUSY
;
1517 entry
= priv
->cur_tx
[q
] % (priv
->num_tx_ring
[q
] * NUM_TX_DESC
);
1518 priv
->tx_skb
[q
][entry
/ NUM_TX_DESC
] = skb
;
1520 if (skb_put_padto(skb
, ETH_ZLEN
))
1523 buffer
= PTR_ALIGN(priv
->tx_align
[q
], DPTR_ALIGN
) +
1524 entry
/ NUM_TX_DESC
* DPTR_ALIGN
;
1525 len
= PTR_ALIGN(skb
->data
, DPTR_ALIGN
) - skb
->data
;
1526 /* Zero length DMA descriptors are problematic as they seem to
1527 * terminate DMA transfers. Avoid them by simply using a length of
1528 * DPTR_ALIGN (4) when skb data is aligned to DPTR_ALIGN.
1530 * As skb is guaranteed to have at least ETH_ZLEN (60) bytes of
1531 * data by the call to skb_put_padto() above this is safe with
1532 * respect to both the length of the first DMA descriptor (len)
1533 * overflowing the available data and the length of the second DMA
1534 * descriptor (skb->len - len) being negative.
1539 memcpy(buffer
, skb
->data
, len
);
1540 dma_addr
= dma_map_single(ndev
->dev
.parent
, buffer
, len
, DMA_TO_DEVICE
);
1541 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
1544 desc
= &priv
->tx_ring
[q
][entry
];
1545 desc
->ds_tagl
= cpu_to_le16(len
);
1546 desc
->dptr
= cpu_to_le32(dma_addr
);
1548 buffer
= skb
->data
+ len
;
1549 len
= skb
->len
- len
;
1550 dma_addr
= dma_map_single(ndev
->dev
.parent
, buffer
, len
, DMA_TO_DEVICE
);
1551 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
1555 desc
->ds_tagl
= cpu_to_le16(len
);
1556 desc
->dptr
= cpu_to_le32(dma_addr
);
1558 /* TX timestamp required */
1560 ts_skb
= kmalloc(sizeof(*ts_skb
), GFP_ATOMIC
);
1563 dma_unmap_single(ndev
->dev
.parent
, dma_addr
, len
,
1568 ts_skb
->tag
= priv
->ts_skb_tag
++;
1569 priv
->ts_skb_tag
&= 0x3ff;
1570 list_add_tail(&ts_skb
->list
, &priv
->ts_skb_list
);
1572 /* TAG and timestamp required flag */
1573 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
1574 desc
->tagh_tsr
= (ts_skb
->tag
>> 4) | TX_TSR
;
1575 desc
->ds_tagl
|= le16_to_cpu(ts_skb
->tag
<< 12);
1578 skb_tx_timestamp(skb
);
1579 /* Descriptor type must be set after all the above writes */
1581 desc
->die_dt
= DT_FEND
;
1583 desc
->die_dt
= DT_FSTART
;
1585 ravb_modify(ndev
, TCCR
, TCCR_TSRQ0
<< q
, TCCR_TSRQ0
<< q
);
1587 priv
->cur_tx
[q
] += NUM_TX_DESC
;
1588 if (priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] >
1589 (priv
->num_tx_ring
[q
] - 1) * NUM_TX_DESC
&&
1590 !ravb_tx_free(ndev
, q
, true))
1591 netif_stop_subqueue(ndev
, q
);
1595 spin_unlock_irqrestore(&priv
->lock
, flags
);
1596 return NETDEV_TX_OK
;
1599 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
1600 le16_to_cpu(desc
->ds_tagl
), DMA_TO_DEVICE
);
1602 dev_kfree_skb_any(skb
);
1603 priv
->tx_skb
[q
][entry
/ NUM_TX_DESC
] = NULL
;
1607 static u16
ravb_select_queue(struct net_device
*ndev
, struct sk_buff
*skb
,
1608 void *accel_priv
, select_queue_fallback_t fallback
)
1610 /* If skb needs TX timestamp, it is handled in network control queue */
1611 return (skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
) ? RAVB_NC
:
1616 static struct net_device_stats
*ravb_get_stats(struct net_device
*ndev
)
1618 struct ravb_private
*priv
= netdev_priv(ndev
);
1619 struct net_device_stats
*nstats
, *stats0
, *stats1
;
1621 nstats
= &ndev
->stats
;
1622 stats0
= &priv
->stats
[RAVB_BE
];
1623 stats1
= &priv
->stats
[RAVB_NC
];
1625 nstats
->tx_dropped
+= ravb_read(ndev
, TROCR
);
1626 ravb_write(ndev
, 0, TROCR
); /* (write clear) */
1627 nstats
->collisions
+= ravb_read(ndev
, CDCR
);
1628 ravb_write(ndev
, 0, CDCR
); /* (write clear) */
1629 nstats
->tx_carrier_errors
+= ravb_read(ndev
, LCCR
);
1630 ravb_write(ndev
, 0, LCCR
); /* (write clear) */
1632 nstats
->tx_carrier_errors
+= ravb_read(ndev
, CERCR
);
1633 ravb_write(ndev
, 0, CERCR
); /* (write clear) */
1634 nstats
->tx_carrier_errors
+= ravb_read(ndev
, CEECR
);
1635 ravb_write(ndev
, 0, CEECR
); /* (write clear) */
1637 nstats
->rx_packets
= stats0
->rx_packets
+ stats1
->rx_packets
;
1638 nstats
->tx_packets
= stats0
->tx_packets
+ stats1
->tx_packets
;
1639 nstats
->rx_bytes
= stats0
->rx_bytes
+ stats1
->rx_bytes
;
1640 nstats
->tx_bytes
= stats0
->tx_bytes
+ stats1
->tx_bytes
;
1641 nstats
->multicast
= stats0
->multicast
+ stats1
->multicast
;
1642 nstats
->rx_errors
= stats0
->rx_errors
+ stats1
->rx_errors
;
1643 nstats
->rx_crc_errors
= stats0
->rx_crc_errors
+ stats1
->rx_crc_errors
;
1644 nstats
->rx_frame_errors
=
1645 stats0
->rx_frame_errors
+ stats1
->rx_frame_errors
;
1646 nstats
->rx_length_errors
=
1647 stats0
->rx_length_errors
+ stats1
->rx_length_errors
;
1648 nstats
->rx_missed_errors
=
1649 stats0
->rx_missed_errors
+ stats1
->rx_missed_errors
;
1650 nstats
->rx_over_errors
=
1651 stats0
->rx_over_errors
+ stats1
->rx_over_errors
;
1656 /* Update promiscuous bit */
1657 static void ravb_set_rx_mode(struct net_device
*ndev
)
1659 struct ravb_private
*priv
= netdev_priv(ndev
);
1660 unsigned long flags
;
1662 spin_lock_irqsave(&priv
->lock
, flags
);
1663 ravb_modify(ndev
, ECMR
, ECMR_PRM
,
1664 ndev
->flags
& IFF_PROMISC
? ECMR_PRM
: 0);
1666 spin_unlock_irqrestore(&priv
->lock
, flags
);
1669 /* Device close function for Ethernet AVB */
1670 static int ravb_close(struct net_device
*ndev
)
1672 struct device_node
*np
= ndev
->dev
.parent
->of_node
;
1673 struct ravb_private
*priv
= netdev_priv(ndev
);
1674 struct ravb_tstamp_skb
*ts_skb
, *ts_skb2
;
1676 netif_tx_stop_all_queues(ndev
);
1678 /* Disable interrupts by clearing the interrupt masks. */
1679 ravb_write(ndev
, 0, RIC0
);
1680 ravb_write(ndev
, 0, RIC2
);
1681 ravb_write(ndev
, 0, TIC
);
1683 /* Stop PTP Clock driver */
1684 if (priv
->chip_id
== RCAR_GEN2
)
1685 ravb_ptp_stop(ndev
);
1687 /* Set the config mode to stop the AVB-DMAC's processes */
1688 if (ravb_stop_dma(ndev
) < 0)
1690 "device will be stopped after h/w processes are done.\n");
1692 /* Clear the timestamp list */
1693 list_for_each_entry_safe(ts_skb
, ts_skb2
, &priv
->ts_skb_list
, list
) {
1694 list_del(&ts_skb
->list
);
1698 /* PHY disconnect */
1700 phy_stop(ndev
->phydev
);
1701 phy_disconnect(ndev
->phydev
);
1702 if (of_phy_is_fixed_link(np
))
1703 of_phy_deregister_fixed_link(np
);
1706 if (priv
->chip_id
!= RCAR_GEN2
) {
1707 free_irq(priv
->tx_irqs
[RAVB_NC
], ndev
);
1708 free_irq(priv
->rx_irqs
[RAVB_NC
], ndev
);
1709 free_irq(priv
->tx_irqs
[RAVB_BE
], ndev
);
1710 free_irq(priv
->rx_irqs
[RAVB_BE
], ndev
);
1711 free_irq(priv
->emac_irq
, ndev
);
1713 free_irq(ndev
->irq
, ndev
);
1715 napi_disable(&priv
->napi
[RAVB_NC
]);
1716 napi_disable(&priv
->napi
[RAVB_BE
]);
1718 /* Free all the skb's in the RX queue and the DMA buffers. */
1719 ravb_ring_free(ndev
, RAVB_BE
);
1720 ravb_ring_free(ndev
, RAVB_NC
);
1725 static int ravb_hwtstamp_get(struct net_device
*ndev
, struct ifreq
*req
)
1727 struct ravb_private
*priv
= netdev_priv(ndev
);
1728 struct hwtstamp_config config
;
1731 config
.tx_type
= priv
->tstamp_tx_ctrl
? HWTSTAMP_TX_ON
:
1733 if (priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
)
1734 config
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_L2_EVENT
;
1735 else if (priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE_ALL
)
1736 config
.rx_filter
= HWTSTAMP_FILTER_ALL
;
1738 config
.rx_filter
= HWTSTAMP_FILTER_NONE
;
1740 return copy_to_user(req
->ifr_data
, &config
, sizeof(config
)) ?
1744 /* Control hardware time stamping */
1745 static int ravb_hwtstamp_set(struct net_device
*ndev
, struct ifreq
*req
)
1747 struct ravb_private
*priv
= netdev_priv(ndev
);
1748 struct hwtstamp_config config
;
1749 u32 tstamp_rx_ctrl
= RAVB_RXTSTAMP_ENABLED
;
1752 if (copy_from_user(&config
, req
->ifr_data
, sizeof(config
)))
1755 /* Reserved for future extensions */
1759 switch (config
.tx_type
) {
1760 case HWTSTAMP_TX_OFF
:
1763 case HWTSTAMP_TX_ON
:
1764 tstamp_tx_ctrl
= RAVB_TXTSTAMP_ENABLED
;
1770 switch (config
.rx_filter
) {
1771 case HWTSTAMP_FILTER_NONE
:
1774 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
1775 tstamp_rx_ctrl
|= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
;
1778 config
.rx_filter
= HWTSTAMP_FILTER_ALL
;
1779 tstamp_rx_ctrl
|= RAVB_RXTSTAMP_TYPE_ALL
;
1782 priv
->tstamp_tx_ctrl
= tstamp_tx_ctrl
;
1783 priv
->tstamp_rx_ctrl
= tstamp_rx_ctrl
;
1785 return copy_to_user(req
->ifr_data
, &config
, sizeof(config
)) ?
1789 /* ioctl to device function */
1790 static int ravb_do_ioctl(struct net_device
*ndev
, struct ifreq
*req
, int cmd
)
1792 struct phy_device
*phydev
= ndev
->phydev
;
1794 if (!netif_running(ndev
))
1802 return ravb_hwtstamp_get(ndev
, req
);
1804 return ravb_hwtstamp_set(ndev
, req
);
1807 return phy_mii_ioctl(phydev
, req
, cmd
);
1810 static const struct net_device_ops ravb_netdev_ops
= {
1811 .ndo_open
= ravb_open
,
1812 .ndo_stop
= ravb_close
,
1813 .ndo_start_xmit
= ravb_start_xmit
,
1814 .ndo_select_queue
= ravb_select_queue
,
1815 .ndo_get_stats
= ravb_get_stats
,
1816 .ndo_set_rx_mode
= ravb_set_rx_mode
,
1817 .ndo_tx_timeout
= ravb_tx_timeout
,
1818 .ndo_do_ioctl
= ravb_do_ioctl
,
1819 .ndo_validate_addr
= eth_validate_addr
,
1820 .ndo_set_mac_address
= eth_mac_addr
,
1821 .ndo_change_mtu
= eth_change_mtu
,
1824 /* MDIO bus init function */
1825 static int ravb_mdio_init(struct ravb_private
*priv
)
1827 struct platform_device
*pdev
= priv
->pdev
;
1828 struct device
*dev
= &pdev
->dev
;
1832 priv
->mdiobb
.ops
= &bb_ops
;
1834 /* MII controller setting */
1835 priv
->mii_bus
= alloc_mdio_bitbang(&priv
->mdiobb
);
1839 /* Hook up MII support for ethtool */
1840 priv
->mii_bus
->name
= "ravb_mii";
1841 priv
->mii_bus
->parent
= dev
;
1842 snprintf(priv
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
1843 pdev
->name
, pdev
->id
);
1845 /* Register MDIO bus */
1846 error
= of_mdiobus_register(priv
->mii_bus
, dev
->of_node
);
1853 free_mdio_bitbang(priv
->mii_bus
);
1857 /* MDIO bus release function */
1858 static int ravb_mdio_release(struct ravb_private
*priv
)
1860 /* Unregister mdio bus */
1861 mdiobus_unregister(priv
->mii_bus
);
1863 /* Free bitbang info */
1864 free_mdio_bitbang(priv
->mii_bus
);
1869 static const struct of_device_id ravb_match_table
[] = {
1870 { .compatible
= "renesas,etheravb-r8a7790", .data
= (void *)RCAR_GEN2
},
1871 { .compatible
= "renesas,etheravb-r8a7794", .data
= (void *)RCAR_GEN2
},
1872 { .compatible
= "renesas,etheravb-rcar-gen2", .data
= (void *)RCAR_GEN2
},
1873 { .compatible
= "renesas,etheravb-r8a7795", .data
= (void *)RCAR_GEN3
},
1874 { .compatible
= "renesas,etheravb-rcar-gen3", .data
= (void *)RCAR_GEN3
},
1877 MODULE_DEVICE_TABLE(of
, ravb_match_table
);
1879 static int ravb_set_gti(struct net_device
*ndev
)
1882 struct device
*dev
= ndev
->dev
.parent
;
1883 struct device_node
*np
= dev
->of_node
;
1888 clk
= of_clk_get(np
, 0);
1890 dev_err(dev
, "could not get clock\n");
1891 return PTR_ERR(clk
);
1894 rate
= clk_get_rate(clk
);
1900 inc
= 1000000000ULL << 20;
1903 if (inc
< GTI_TIV_MIN
|| inc
> GTI_TIV_MAX
) {
1904 dev_err(dev
, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1905 inc
, GTI_TIV_MIN
, GTI_TIV_MAX
);
1909 ravb_write(ndev
, inc
, GTI
);
1914 static void ravb_set_config_mode(struct net_device
*ndev
)
1916 struct ravb_private
*priv
= netdev_priv(ndev
);
1918 if (priv
->chip_id
== RCAR_GEN2
) {
1919 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_CONFIG
);
1920 /* Set CSEL value */
1921 ravb_modify(ndev
, CCC
, CCC_CSEL
, CCC_CSEL_HPB
);
1923 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_CONFIG
|
1924 CCC_GAC
| CCC_CSEL_HPB
);
1928 static int ravb_probe(struct platform_device
*pdev
)
1930 struct device_node
*np
= pdev
->dev
.of_node
;
1931 struct ravb_private
*priv
;
1932 enum ravb_chip_id chip_id
;
1933 struct net_device
*ndev
;
1935 struct resource
*res
;
1940 "this driver is required to be instantiated from device tree\n");
1944 /* Get base address */
1945 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1947 dev_err(&pdev
->dev
, "invalid resource\n");
1951 ndev
= alloc_etherdev_mqs(sizeof(struct ravb_private
),
1952 NUM_TX_QUEUE
, NUM_RX_QUEUE
);
1956 pm_runtime_enable(&pdev
->dev
);
1957 pm_runtime_get_sync(&pdev
->dev
);
1959 /* The Ether-specific entries in the device structure. */
1960 ndev
->base_addr
= res
->start
;
1962 chip_id
= (enum ravb_chip_id
)of_device_get_match_data(&pdev
->dev
);
1964 if (chip_id
== RCAR_GEN3
)
1965 irq
= platform_get_irq_byname(pdev
, "ch22");
1967 irq
= platform_get_irq(pdev
, 0);
1974 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1976 priv
= netdev_priv(ndev
);
1979 priv
->num_tx_ring
[RAVB_BE
] = BE_TX_RING_SIZE
;
1980 priv
->num_rx_ring
[RAVB_BE
] = BE_RX_RING_SIZE
;
1981 priv
->num_tx_ring
[RAVB_NC
] = NC_TX_RING_SIZE
;
1982 priv
->num_rx_ring
[RAVB_NC
] = NC_RX_RING_SIZE
;
1983 priv
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
1984 if (IS_ERR(priv
->addr
)) {
1985 error
= PTR_ERR(priv
->addr
);
1989 spin_lock_init(&priv
->lock
);
1990 INIT_WORK(&priv
->work
, ravb_tx_timeout_work
);
1992 priv
->phy_interface
= of_get_phy_mode(np
);
1994 priv
->no_avb_link
= of_property_read_bool(np
, "renesas,no-ether-link");
1995 priv
->avb_link_active_low
=
1996 of_property_read_bool(np
, "renesas,ether-link-active-low");
1998 if (chip_id
== RCAR_GEN3
) {
1999 irq
= platform_get_irq_byname(pdev
, "ch24");
2004 priv
->emac_irq
= irq
;
2005 for (i
= 0; i
< NUM_RX_QUEUE
; i
++) {
2006 irq
= platform_get_irq_byname(pdev
, ravb_rx_irqs
[i
]);
2011 priv
->rx_irqs
[i
] = irq
;
2013 for (i
= 0; i
< NUM_TX_QUEUE
; i
++) {
2014 irq
= platform_get_irq_byname(pdev
, ravb_tx_irqs
[i
]);
2019 priv
->tx_irqs
[i
] = irq
;
2023 priv
->chip_id
= chip_id
;
2026 ndev
->netdev_ops
= &ravb_netdev_ops
;
2027 ndev
->ethtool_ops
= &ravb_ethtool_ops
;
2029 /* Set AVB config mode */
2030 ravb_set_config_mode(ndev
);
2033 error
= ravb_set_gti(ndev
);
2037 /* Request GTI loading */
2038 ravb_modify(ndev
, GCCR
, GCCR_LTI
, GCCR_LTI
);
2040 /* Allocate descriptor base address table */
2041 priv
->desc_bat_size
= sizeof(struct ravb_desc
) * DBAT_ENTRY_NUM
;
2042 priv
->desc_bat
= dma_alloc_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
,
2043 &priv
->desc_bat_dma
, GFP_KERNEL
);
2044 if (!priv
->desc_bat
) {
2046 "Cannot allocate desc base address table (size %d bytes)\n",
2047 priv
->desc_bat_size
);
2051 for (q
= RAVB_BE
; q
< DBAT_ENTRY_NUM
; q
++)
2052 priv
->desc_bat
[q
].die_dt
= DT_EOS
;
2053 ravb_write(ndev
, priv
->desc_bat_dma
, DBAT
);
2055 /* Initialise HW timestamp list */
2056 INIT_LIST_HEAD(&priv
->ts_skb_list
);
2058 /* Initialise PTP Clock driver */
2059 if (chip_id
!= RCAR_GEN2
)
2060 ravb_ptp_init(ndev
, pdev
);
2062 /* Debug message level */
2063 priv
->msg_enable
= RAVB_DEF_MSG_ENABLE
;
2065 /* Read and set MAC address */
2066 ravb_read_mac_address(ndev
, of_get_mac_address(np
));
2067 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
2068 dev_warn(&pdev
->dev
,
2069 "no valid MAC address supplied, using a random one\n");
2070 eth_hw_addr_random(ndev
);
2074 error
= ravb_mdio_init(priv
);
2076 dev_err(&pdev
->dev
, "failed to initialize MDIO\n");
2080 netif_napi_add(ndev
, &priv
->napi
[RAVB_BE
], ravb_poll
, 64);
2081 netif_napi_add(ndev
, &priv
->napi
[RAVB_NC
], ravb_poll
, 64);
2083 /* Network device register */
2084 error
= register_netdev(ndev
);
2088 /* Print device information */
2089 netdev_info(ndev
, "Base address at %#x, %pM, IRQ %d.\n",
2090 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
2092 platform_set_drvdata(pdev
, ndev
);
2097 netif_napi_del(&priv
->napi
[RAVB_NC
]);
2098 netif_napi_del(&priv
->napi
[RAVB_BE
]);
2099 ravb_mdio_release(priv
);
2101 dma_free_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
, priv
->desc_bat
,
2102 priv
->desc_bat_dma
);
2104 /* Stop PTP Clock driver */
2105 if (chip_id
!= RCAR_GEN2
)
2106 ravb_ptp_stop(ndev
);
2111 pm_runtime_put(&pdev
->dev
);
2112 pm_runtime_disable(&pdev
->dev
);
2116 static int ravb_remove(struct platform_device
*pdev
)
2118 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2119 struct ravb_private
*priv
= netdev_priv(ndev
);
2121 /* Stop PTP Clock driver */
2122 if (priv
->chip_id
!= RCAR_GEN2
)
2123 ravb_ptp_stop(ndev
);
2125 dma_free_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
, priv
->desc_bat
,
2126 priv
->desc_bat_dma
);
2127 /* Set reset mode */
2128 ravb_write(ndev
, CCC_OPC_RESET
, CCC
);
2129 pm_runtime_put_sync(&pdev
->dev
);
2130 unregister_netdev(ndev
);
2131 netif_napi_del(&priv
->napi
[RAVB_NC
]);
2132 netif_napi_del(&priv
->napi
[RAVB_BE
]);
2133 ravb_mdio_release(priv
);
2134 pm_runtime_disable(&pdev
->dev
);
2136 platform_set_drvdata(pdev
, NULL
);
2141 static int __maybe_unused
ravb_suspend(struct device
*dev
)
2143 struct net_device
*ndev
= dev_get_drvdata(dev
);
2146 if (netif_running(ndev
)) {
2147 netif_device_detach(ndev
);
2148 ret
= ravb_close(ndev
);
2154 static int __maybe_unused
ravb_resume(struct device
*dev
)
2156 struct net_device
*ndev
= dev_get_drvdata(dev
);
2157 struct ravb_private
*priv
= netdev_priv(ndev
);
2160 /* All register have been reset to default values.
2161 * Restore all registers which where setup at probe time and
2162 * reopen device if it was running before system suspended.
2165 /* Set AVB config mode */
2166 ravb_set_config_mode(ndev
);
2169 ret
= ravb_set_gti(ndev
);
2173 /* Request GTI loading */
2174 ravb_modify(ndev
, GCCR
, GCCR_LTI
, GCCR_LTI
);
2176 /* Restore descriptor base address table */
2177 ravb_write(ndev
, priv
->desc_bat_dma
, DBAT
);
2179 if (netif_running(ndev
)) {
2180 ret
= ravb_open(ndev
);
2183 netif_device_attach(ndev
);
2189 static int __maybe_unused
ravb_runtime_nop(struct device
*dev
)
2191 /* Runtime PM callback shared between ->runtime_suspend()
2192 * and ->runtime_resume(). Simply returns success.
2194 * This driver re-initializes all registers after
2195 * pm_runtime_get_sync() anyway so there is no need
2196 * to save and restore registers here.
2201 static const struct dev_pm_ops ravb_dev_pm_ops
= {
2202 SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend
, ravb_resume
)
2203 SET_RUNTIME_PM_OPS(ravb_runtime_nop
, ravb_runtime_nop
, NULL
)
2206 static struct platform_driver ravb_driver
= {
2207 .probe
= ravb_probe
,
2208 .remove
= ravb_remove
,
2211 .pm
= &ravb_dev_pm_ops
,
2212 .of_match_table
= ravb_match_table
,
2216 module_platform_driver(ravb_driver
);
2218 MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2219 MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2220 MODULE_LICENSE("GPL v2");