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 skb's and DMA buffers for Ethernet AVB */
183 static void ravb_ring_free(struct net_device
*ndev
, int q
)
185 struct ravb_private
*priv
= netdev_priv(ndev
);
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
]);
194 kfree(priv
->rx_skb
[q
]);
195 priv
->rx_skb
[q
] = NULL
;
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
]);
202 kfree(priv
->tx_skb
[q
]);
203 priv
->tx_skb
[q
] = NULL
;
205 /* Free aligned TX buffers */
206 kfree(priv
->tx_align
[q
]);
207 priv
->tx_align
[q
] = NULL
;
209 if (priv
->rx_ring
[q
]) {
210 ring_size
= sizeof(struct ravb_ex_rx_desc
) *
211 (priv
->num_rx_ring
[q
] + 1);
212 dma_free_coherent(ndev
->dev
.parent
, ring_size
, priv
->rx_ring
[q
],
213 priv
->rx_desc_dma
[q
]);
214 priv
->rx_ring
[q
] = NULL
;
217 if (priv
->tx_ring
[q
]) {
218 ring_size
= sizeof(struct ravb_tx_desc
) *
219 (priv
->num_tx_ring
[q
] * NUM_TX_DESC
+ 1);
220 dma_free_coherent(ndev
->dev
.parent
, ring_size
, priv
->tx_ring
[q
],
221 priv
->tx_desc_dma
[q
]);
222 priv
->tx_ring
[q
] = NULL
;
226 /* Format skb and descriptor buffer for Ethernet AVB */
227 static void ravb_ring_format(struct net_device
*ndev
, int q
)
229 struct ravb_private
*priv
= netdev_priv(ndev
);
230 struct ravb_ex_rx_desc
*rx_desc
;
231 struct ravb_tx_desc
*tx_desc
;
232 struct ravb_desc
*desc
;
233 int rx_ring_size
= sizeof(*rx_desc
) * priv
->num_rx_ring
[q
];
234 int tx_ring_size
= sizeof(*tx_desc
) * priv
->num_tx_ring
[q
] *
241 priv
->dirty_rx
[q
] = 0;
242 priv
->dirty_tx
[q
] = 0;
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
++) {
248 rx_desc
= &priv
->rx_ring
[q
][i
];
249 rx_desc
->ds_cc
= cpu_to_le16(PKT_BUF_SZ
);
250 dma_addr
= dma_map_single(ndev
->dev
.parent
, priv
->rx_skb
[q
][i
]->data
,
253 /* We just set the data size to 0 for a failed mapping which
254 * should prevent DMA from happening...
256 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
257 rx_desc
->ds_cc
= cpu_to_le16(0);
258 rx_desc
->dptr
= cpu_to_le32(dma_addr
);
259 rx_desc
->die_dt
= DT_FEMPTY
;
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 */
265 memset(priv
->tx_ring
[q
], 0, tx_ring_size
);
266 /* Build TX ring buffer */
267 for (i
= 0, tx_desc
= priv
->tx_ring
[q
]; i
< priv
->num_tx_ring
[q
];
269 tx_desc
->die_dt
= DT_EEMPTY
;
271 tx_desc
->die_dt
= DT_EEMPTY
;
273 tx_desc
->dptr
= cpu_to_le32((u32
)priv
->tx_desc_dma
[q
]);
274 tx_desc
->die_dt
= DT_LINKFIX
; /* type */
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
]);
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
]);
287 /* Init skb and descriptor buffer for Ethernet AVB */
288 static int ravb_ring_init(struct net_device
*ndev
, int q
)
290 struct ravb_private
*priv
= netdev_priv(ndev
);
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
])
303 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++) {
304 skb
= netdev_alloc_skb(ndev
, PKT_BUF_SZ
+ RAVB_ALIGN
- 1);
307 ravb_set_buffer_align(skb
);
308 priv
->rx_skb
[q
][i
] = skb
;
311 /* Allocate rings for the aligned buffers */
312 priv
->tx_align
[q
] = kmalloc(DPTR_ALIGN
* priv
->num_tx_ring
[q
] +
313 DPTR_ALIGN
- 1, GFP_KERNEL
);
314 if (!priv
->tx_align
[q
])
317 /* Allocate all RX descriptors. */
318 ring_size
= sizeof(struct ravb_ex_rx_desc
) * (priv
->num_rx_ring
[q
] + 1);
319 priv
->rx_ring
[q
] = dma_alloc_coherent(ndev
->dev
.parent
, ring_size
,
320 &priv
->rx_desc_dma
[q
],
322 if (!priv
->rx_ring
[q
])
325 priv
->dirty_rx
[q
] = 0;
327 /* Allocate all TX descriptors. */
328 ring_size
= sizeof(struct ravb_tx_desc
) *
329 (priv
->num_tx_ring
[q
] * NUM_TX_DESC
+ 1);
330 priv
->tx_ring
[q
] = dma_alloc_coherent(ndev
->dev
.parent
, ring_size
,
331 &priv
->tx_desc_dma
[q
],
333 if (!priv
->tx_ring
[q
])
339 ravb_ring_free(ndev
, q
);
344 /* E-MAC init function */
345 static void ravb_emac_init(struct net_device
*ndev
)
347 struct ravb_private
*priv
= netdev_priv(ndev
);
349 /* Receive frame limit set register */
350 ravb_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
, RFLR
);
352 /* PAUSE prohibition */
353 ravb_write(ndev
, ECMR_ZPF
| (priv
->duplex
? ECMR_DM
: 0) |
354 ECMR_TE
| ECMR_RE
, ECMR
);
358 /* Set MAC address */
360 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
361 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
363 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
365 /* E-MAC status register clear */
366 ravb_write(ndev
, ECSR_ICD
| ECSR_MPD
, ECSR
);
368 /* E-MAC interrupt enable register */
369 ravb_write(ndev
, ECSIPR_ICDIP
| ECSIPR_MPDIP
| ECSIPR_LCHNGIP
, ECSIPR
);
372 /* Device init function for Ethernet AVB */
373 static int ravb_dmac_init(struct net_device
*ndev
)
375 struct ravb_private
*priv
= netdev_priv(ndev
);
378 /* Set CONFIG mode */
379 error
= ravb_config(ndev
);
383 error
= ravb_ring_init(ndev
, RAVB_BE
);
386 error
= ravb_ring_init(ndev
, RAVB_NC
);
388 ravb_ring_free(ndev
, RAVB_BE
);
392 /* Descriptor format */
393 ravb_ring_format(ndev
, RAVB_BE
);
394 ravb_ring_format(ndev
, RAVB_NC
);
396 #if defined(__LITTLE_ENDIAN)
397 ravb_modify(ndev
, CCC
, CCC_BOC
, 0);
399 ravb_modify(ndev
, CCC
, CCC_BOC
, CCC_BOC
);
404 RCR_EFFS
| RCR_ENCF
| RCR_ETS0
| RCR_ESF
| 0x18000000, RCR
);
407 ravb_write(ndev
, TGC_TQP_AVBMODE1
| 0x00222200, TGC
);
409 /* Timestamp enable */
410 ravb_write(ndev
, TCCR_TFEN
, TCCR
);
412 /* Interrupt init: */
413 if (priv
->chip_id
== RCAR_GEN3
) {
415 ravb_write(ndev
, 0, DIL
);
416 /* Set queue specific interrupt */
417 ravb_write(ndev
, CIE_CRIE
| CIE_CTIE
| CIE_CL0M
, CIE
);
420 ravb_write(ndev
, RIC0_FRE0
| RIC0_FRE1
, RIC0
);
421 /* Disable FIFO full warning */
422 ravb_write(ndev
, 0, RIC1
);
423 /* Receive FIFO full error, descriptor empty */
424 ravb_write(ndev
, RIC2_QFE0
| RIC2_QFE1
| RIC2_RFFE
, RIC2
);
425 /* Frame transmitted, timestamp FIFO updated */
426 ravb_write(ndev
, TIC_FTE0
| TIC_FTE1
| TIC_TFUE
, TIC
);
428 /* Setting the control will start the AVB-DMAC process. */
429 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_OPERATION
);
434 /* Free TX skb function for AVB-IP */
435 static int ravb_tx_free(struct net_device
*ndev
, int q
)
437 struct ravb_private
*priv
= netdev_priv(ndev
);
438 struct net_device_stats
*stats
= &priv
->stats
[q
];
439 struct ravb_tx_desc
*desc
;
444 for (; priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] > 0; priv
->dirty_tx
[q
]++) {
445 entry
= priv
->dirty_tx
[q
] % (priv
->num_tx_ring
[q
] *
447 desc
= &priv
->tx_ring
[q
][entry
];
448 if (desc
->die_dt
!= DT_FEMPTY
)
450 /* Descriptor type must be checked before all other reads */
452 size
= le16_to_cpu(desc
->ds_tagl
) & TX_DS
;
453 /* Free the original skb. */
454 if (priv
->tx_skb
[q
][entry
/ NUM_TX_DESC
]) {
455 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
456 size
, DMA_TO_DEVICE
);
457 /* Last packet descriptor? */
458 if (entry
% NUM_TX_DESC
== NUM_TX_DESC
- 1) {
459 entry
/= NUM_TX_DESC
;
460 dev_kfree_skb_any(priv
->tx_skb
[q
][entry
]);
461 priv
->tx_skb
[q
][entry
] = NULL
;
466 stats
->tx_bytes
+= size
;
467 desc
->die_dt
= DT_EEMPTY
;
472 static void ravb_get_tx_tstamp(struct net_device
*ndev
)
474 struct ravb_private
*priv
= netdev_priv(ndev
);
475 struct ravb_tstamp_skb
*ts_skb
, *ts_skb2
;
476 struct skb_shared_hwtstamps shhwtstamps
;
478 struct timespec64 ts
;
483 count
= (ravb_read(ndev
, TSR
) & TSR_TFFL
) >> 8;
485 tfa2
= ravb_read(ndev
, TFA2
);
486 tfa_tag
= (tfa2
& TFA2_TST
) >> 16;
487 ts
.tv_nsec
= (u64
)ravb_read(ndev
, TFA0
);
488 ts
.tv_sec
= ((u64
)(tfa2
& TFA2_TSV
) << 32) |
489 ravb_read(ndev
, TFA1
);
490 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
491 shhwtstamps
.hwtstamp
= timespec64_to_ktime(ts
);
492 list_for_each_entry_safe(ts_skb
, ts_skb2
, &priv
->ts_skb_list
,
496 list_del(&ts_skb
->list
);
498 if (tag
== tfa_tag
) {
499 skb_tstamp_tx(skb
, &shhwtstamps
);
503 ravb_modify(ndev
, TCCR
, TCCR_TFR
, TCCR_TFR
);
507 /* Packet receive function for Ethernet AVB */
508 static bool ravb_rx(struct net_device
*ndev
, int *quota
, int q
)
510 struct ravb_private
*priv
= netdev_priv(ndev
);
511 int entry
= priv
->cur_rx
[q
] % priv
->num_rx_ring
[q
];
512 int boguscnt
= (priv
->dirty_rx
[q
] + priv
->num_rx_ring
[q
]) -
514 struct net_device_stats
*stats
= &priv
->stats
[q
];
515 struct ravb_ex_rx_desc
*desc
;
518 struct timespec64 ts
;
523 boguscnt
= min(boguscnt
, *quota
);
525 desc
= &priv
->rx_ring
[q
][entry
];
526 while (desc
->die_dt
!= DT_FEMPTY
) {
527 /* Descriptor type must be checked before all other reads */
529 desc_status
= desc
->msc
;
530 pkt_len
= le16_to_cpu(desc
->ds_cc
) & RX_DS
;
535 /* We use 0-byte descriptors to mark the DMA mapping errors */
539 if (desc_status
& MSC_MC
)
542 if (desc_status
& (MSC_CRC
| MSC_RFE
| MSC_RTSF
| MSC_RTLF
|
545 if (desc_status
& MSC_CRC
)
546 stats
->rx_crc_errors
++;
547 if (desc_status
& MSC_RFE
)
548 stats
->rx_frame_errors
++;
549 if (desc_status
& (MSC_RTLF
| MSC_RTSF
))
550 stats
->rx_length_errors
++;
551 if (desc_status
& MSC_CEEF
)
552 stats
->rx_missed_errors
++;
554 u32 get_ts
= priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE
;
556 skb
= priv
->rx_skb
[q
][entry
];
557 priv
->rx_skb
[q
][entry
] = NULL
;
558 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
561 get_ts
&= (q
== RAVB_NC
) ?
562 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
:
563 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
;
565 struct skb_shared_hwtstamps
*shhwtstamps
;
567 shhwtstamps
= skb_hwtstamps(skb
);
568 memset(shhwtstamps
, 0, sizeof(*shhwtstamps
));
569 ts
.tv_sec
= ((u64
) le16_to_cpu(desc
->ts_sh
) <<
570 32) | le32_to_cpu(desc
->ts_sl
);
571 ts
.tv_nsec
= le32_to_cpu(desc
->ts_n
);
572 shhwtstamps
->hwtstamp
= timespec64_to_ktime(ts
);
574 skb_put(skb
, pkt_len
);
575 skb
->protocol
= eth_type_trans(skb
, ndev
);
576 napi_gro_receive(&priv
->napi
[q
], skb
);
578 stats
->rx_bytes
+= pkt_len
;
581 entry
= (++priv
->cur_rx
[q
]) % priv
->num_rx_ring
[q
];
582 desc
= &priv
->rx_ring
[q
][entry
];
585 /* Refill the RX ring buffers. */
586 for (; priv
->cur_rx
[q
] - priv
->dirty_rx
[q
] > 0; priv
->dirty_rx
[q
]++) {
587 entry
= priv
->dirty_rx
[q
] % priv
->num_rx_ring
[q
];
588 desc
= &priv
->rx_ring
[q
][entry
];
589 desc
->ds_cc
= cpu_to_le16(PKT_BUF_SZ
);
591 if (!priv
->rx_skb
[q
][entry
]) {
592 skb
= netdev_alloc_skb(ndev
,
593 PKT_BUF_SZ
+ RAVB_ALIGN
- 1);
595 break; /* Better luck next round. */
596 ravb_set_buffer_align(skb
);
597 dma_addr
= dma_map_single(ndev
->dev
.parent
, skb
->data
,
598 le16_to_cpu(desc
->ds_cc
),
600 skb_checksum_none_assert(skb
);
601 /* We just set the data size to 0 for a failed mapping
602 * which should prevent DMA from happening...
604 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
605 desc
->ds_cc
= cpu_to_le16(0);
606 desc
->dptr
= cpu_to_le32(dma_addr
);
607 priv
->rx_skb
[q
][entry
] = skb
;
609 /* Descriptor type must be set after all the above writes */
611 desc
->die_dt
= DT_FEMPTY
;
614 *quota
-= limit
- (++boguscnt
);
616 return boguscnt
<= 0;
619 static void ravb_rcv_snd_disable(struct net_device
*ndev
)
621 /* Disable TX and RX */
622 ravb_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, 0);
625 static void ravb_rcv_snd_enable(struct net_device
*ndev
)
627 /* Enable TX and RX */
628 ravb_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, ECMR_RE
| ECMR_TE
);
631 /* function for waiting dma process finished */
632 static int ravb_stop_dma(struct net_device
*ndev
)
636 /* Wait for stopping the hardware TX process */
637 error
= ravb_wait(ndev
, TCCR
,
638 TCCR_TSRQ0
| TCCR_TSRQ1
| TCCR_TSRQ2
| TCCR_TSRQ3
, 0);
642 error
= ravb_wait(ndev
, CSR
, CSR_TPO0
| CSR_TPO1
| CSR_TPO2
| CSR_TPO3
,
647 /* Stop the E-MAC's RX/TX processes. */
648 ravb_rcv_snd_disable(ndev
);
650 /* Wait for stopping the RX DMA process */
651 error
= ravb_wait(ndev
, CSR
, CSR_RPO
, 0);
655 /* Stop AVB-DMAC process */
656 return ravb_config(ndev
);
659 /* E-MAC interrupt handler */
660 static void ravb_emac_interrupt_unlocked(struct net_device
*ndev
)
662 struct ravb_private
*priv
= netdev_priv(ndev
);
665 ecsr
= ravb_read(ndev
, ECSR
);
666 ravb_write(ndev
, ecsr
, ECSR
); /* clear interrupt */
668 ndev
->stats
.tx_carrier_errors
++;
669 if (ecsr
& ECSR_LCHNG
) {
671 if (priv
->no_avb_link
)
673 psr
= ravb_read(ndev
, PSR
);
674 if (priv
->avb_link_active_low
)
676 if (!(psr
& PSR_LMON
)) {
677 /* DIsable RX and TX */
678 ravb_rcv_snd_disable(ndev
);
680 /* Enable RX and TX */
681 ravb_rcv_snd_enable(ndev
);
686 static irqreturn_t
ravb_emac_interrupt(int irq
, void *dev_id
)
688 struct net_device
*ndev
= dev_id
;
689 struct ravb_private
*priv
= netdev_priv(ndev
);
691 spin_lock(&priv
->lock
);
692 ravb_emac_interrupt_unlocked(ndev
);
694 spin_unlock(&priv
->lock
);
698 /* Error interrupt handler */
699 static void ravb_error_interrupt(struct net_device
*ndev
)
701 struct ravb_private
*priv
= netdev_priv(ndev
);
704 eis
= ravb_read(ndev
, EIS
);
705 ravb_write(ndev
, ~EIS_QFS
, EIS
);
707 ris2
= ravb_read(ndev
, RIS2
);
708 ravb_write(ndev
, ~(RIS2_QFF0
| RIS2_RFFF
), RIS2
);
710 /* Receive Descriptor Empty int */
711 if (ris2
& RIS2_QFF0
)
712 priv
->stats
[RAVB_BE
].rx_over_errors
++;
714 /* Receive Descriptor Empty int */
715 if (ris2
& RIS2_QFF1
)
716 priv
->stats
[RAVB_NC
].rx_over_errors
++;
718 /* Receive FIFO Overflow int */
719 if (ris2
& RIS2_RFFF
)
720 priv
->rx_fifo_errors
++;
724 static bool ravb_queue_interrupt(struct net_device
*ndev
, int q
)
726 struct ravb_private
*priv
= netdev_priv(ndev
);
727 u32 ris0
= ravb_read(ndev
, RIS0
);
728 u32 ric0
= ravb_read(ndev
, RIC0
);
729 u32 tis
= ravb_read(ndev
, TIS
);
730 u32 tic
= ravb_read(ndev
, TIC
);
732 if (((ris0
& ric0
) & BIT(q
)) || ((tis
& tic
) & BIT(q
))) {
733 if (napi_schedule_prep(&priv
->napi
[q
])) {
734 /* Mask RX and TX interrupts */
735 if (priv
->chip_id
== RCAR_GEN2
) {
736 ravb_write(ndev
, ric0
& ~BIT(q
), RIC0
);
737 ravb_write(ndev
, tic
& ~BIT(q
), TIC
);
739 ravb_write(ndev
, BIT(q
), RID0
);
740 ravb_write(ndev
, BIT(q
), TID
);
742 __napi_schedule(&priv
->napi
[q
]);
745 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
748 " tx status 0x%08x, tx mask 0x%08x.\n",
756 static bool ravb_timestamp_interrupt(struct net_device
*ndev
)
758 u32 tis
= ravb_read(ndev
, TIS
);
760 if (tis
& TIS_TFUF
) {
761 ravb_write(ndev
, ~TIS_TFUF
, TIS
);
762 ravb_get_tx_tstamp(ndev
);
768 static irqreturn_t
ravb_interrupt(int irq
, void *dev_id
)
770 struct net_device
*ndev
= dev_id
;
771 struct ravb_private
*priv
= netdev_priv(ndev
);
772 irqreturn_t result
= IRQ_NONE
;
775 spin_lock(&priv
->lock
);
776 /* Get interrupt status */
777 iss
= ravb_read(ndev
, ISS
);
779 /* Received and transmitted interrupts */
780 if (iss
& (ISS_FRS
| ISS_FTS
| ISS_TFUS
)) {
783 /* Timestamp updated */
784 if (ravb_timestamp_interrupt(ndev
))
785 result
= IRQ_HANDLED
;
787 /* Network control and best effort queue RX/TX */
788 for (q
= RAVB_NC
; q
>= RAVB_BE
; q
--) {
789 if (ravb_queue_interrupt(ndev
, q
))
790 result
= IRQ_HANDLED
;
794 /* E-MAC status summary */
796 ravb_emac_interrupt_unlocked(ndev
);
797 result
= IRQ_HANDLED
;
800 /* Error status summary */
802 ravb_error_interrupt(ndev
);
803 result
= IRQ_HANDLED
;
806 /* gPTP interrupt status summary */
807 if (iss
& ISS_CGIS
) {
808 ravb_ptp_interrupt(ndev
);
809 result
= IRQ_HANDLED
;
813 spin_unlock(&priv
->lock
);
817 /* Timestamp/Error/gPTP interrupt handler */
818 static irqreturn_t
ravb_multi_interrupt(int irq
, void *dev_id
)
820 struct net_device
*ndev
= dev_id
;
821 struct ravb_private
*priv
= netdev_priv(ndev
);
822 irqreturn_t result
= IRQ_NONE
;
825 spin_lock(&priv
->lock
);
826 /* Get interrupt status */
827 iss
= ravb_read(ndev
, ISS
);
829 /* Timestamp updated */
830 if ((iss
& ISS_TFUS
) && ravb_timestamp_interrupt(ndev
))
831 result
= IRQ_HANDLED
;
833 /* Error status summary */
835 ravb_error_interrupt(ndev
);
836 result
= IRQ_HANDLED
;
839 /* gPTP interrupt status summary */
840 if (iss
& ISS_CGIS
) {
841 ravb_ptp_interrupt(ndev
);
842 result
= IRQ_HANDLED
;
846 spin_unlock(&priv
->lock
);
850 static irqreturn_t
ravb_dma_interrupt(int irq
, void *dev_id
, int q
)
852 struct net_device
*ndev
= dev_id
;
853 struct ravb_private
*priv
= netdev_priv(ndev
);
854 irqreturn_t result
= IRQ_NONE
;
856 spin_lock(&priv
->lock
);
858 /* Network control/Best effort queue RX/TX */
859 if (ravb_queue_interrupt(ndev
, q
))
860 result
= IRQ_HANDLED
;
863 spin_unlock(&priv
->lock
);
867 static irqreturn_t
ravb_be_interrupt(int irq
, void *dev_id
)
869 return ravb_dma_interrupt(irq
, dev_id
, RAVB_BE
);
872 static irqreturn_t
ravb_nc_interrupt(int irq
, void *dev_id
)
874 return ravb_dma_interrupt(irq
, dev_id
, RAVB_NC
);
877 static int ravb_poll(struct napi_struct
*napi
, int budget
)
879 struct net_device
*ndev
= napi
->dev
;
880 struct ravb_private
*priv
= netdev_priv(ndev
);
882 int q
= napi
- priv
->napi
;
888 tis
= ravb_read(ndev
, TIS
);
889 ris0
= ravb_read(ndev
, RIS0
);
890 if (!((ris0
& mask
) || (tis
& mask
)))
893 /* Processing RX Descriptor Ring */
895 /* Clear RX interrupt */
896 ravb_write(ndev
, ~mask
, RIS0
);
897 if (ravb_rx(ndev
, "a
, q
))
900 /* Processing TX Descriptor Ring */
902 spin_lock_irqsave(&priv
->lock
, flags
);
903 /* Clear TX interrupt */
904 ravb_write(ndev
, ~mask
, TIS
);
905 ravb_tx_free(ndev
, q
);
906 netif_wake_subqueue(ndev
, q
);
908 spin_unlock_irqrestore(&priv
->lock
, flags
);
914 /* Re-enable RX/TX interrupts */
915 spin_lock_irqsave(&priv
->lock
, flags
);
916 if (priv
->chip_id
== RCAR_GEN2
) {
917 ravb_modify(ndev
, RIC0
, mask
, mask
);
918 ravb_modify(ndev
, TIC
, mask
, mask
);
920 ravb_write(ndev
, mask
, RIE0
);
921 ravb_write(ndev
, mask
, TIE
);
924 spin_unlock_irqrestore(&priv
->lock
, flags
);
926 /* Receive error message handling */
927 priv
->rx_over_errors
= priv
->stats
[RAVB_BE
].rx_over_errors
;
928 priv
->rx_over_errors
+= priv
->stats
[RAVB_NC
].rx_over_errors
;
929 if (priv
->rx_over_errors
!= ndev
->stats
.rx_over_errors
) {
930 ndev
->stats
.rx_over_errors
= priv
->rx_over_errors
;
931 netif_err(priv
, rx_err
, ndev
, "Receive Descriptor Empty\n");
933 if (priv
->rx_fifo_errors
!= ndev
->stats
.rx_fifo_errors
) {
934 ndev
->stats
.rx_fifo_errors
= priv
->rx_fifo_errors
;
935 netif_err(priv
, rx_err
, ndev
, "Receive FIFO Overflow\n");
938 return budget
- quota
;
941 /* PHY state control function */
942 static void ravb_adjust_link(struct net_device
*ndev
)
944 struct ravb_private
*priv
= netdev_priv(ndev
);
945 struct phy_device
*phydev
= ndev
->phydev
;
946 bool new_state
= false;
949 if (phydev
->duplex
!= priv
->duplex
) {
951 priv
->duplex
= phydev
->duplex
;
952 ravb_set_duplex(ndev
);
955 if (phydev
->speed
!= priv
->speed
) {
957 priv
->speed
= phydev
->speed
;
961 ravb_modify(ndev
, ECMR
, ECMR_TXF
, 0);
963 priv
->link
= phydev
->link
;
964 if (priv
->no_avb_link
)
965 ravb_rcv_snd_enable(ndev
);
967 } else if (priv
->link
) {
972 if (priv
->no_avb_link
)
973 ravb_rcv_snd_disable(ndev
);
976 if (new_state
&& netif_msg_link(priv
))
977 phy_print_status(phydev
);
980 /* PHY init function */
981 static int ravb_phy_init(struct net_device
*ndev
)
983 struct device_node
*np
= ndev
->dev
.parent
->of_node
;
984 struct ravb_private
*priv
= netdev_priv(ndev
);
985 struct phy_device
*phydev
;
986 struct device_node
*pn
;
993 /* Try connecting to PHY */
994 pn
= of_parse_phandle(np
, "phy-handle", 0);
996 /* In the case of a fixed PHY, the DT node associated
997 * to the PHY is the Ethernet MAC DT node.
999 if (of_phy_is_fixed_link(np
)) {
1000 err
= of_phy_register_fixed_link(np
);
1004 pn
= of_node_get(np
);
1006 phydev
= of_phy_connect(ndev
, pn
, ravb_adjust_link
, 0,
1007 priv
->phy_interface
);
1010 netdev_err(ndev
, "failed to connect PHY\n");
1014 /* This driver only support 10/100Mbit speeds on Gen3
1017 if (priv
->chip_id
== RCAR_GEN3
) {
1020 err
= phy_set_max_speed(phydev
, SPEED_100
);
1022 netdev_err(ndev
, "failed to limit PHY to 100Mbit/s\n");
1023 phy_disconnect(phydev
);
1027 netdev_info(ndev
, "limited PHY to 100Mbit/s\n");
1030 /* 10BASE is not supported */
1031 phydev
->supported
&= ~PHY_10BT_FEATURES
;
1033 phy_attached_info(phydev
);
1038 /* PHY control start function */
1039 static int ravb_phy_start(struct net_device
*ndev
)
1043 error
= ravb_phy_init(ndev
);
1047 phy_start(ndev
->phydev
);
1052 static int ravb_get_link_ksettings(struct net_device
*ndev
,
1053 struct ethtool_link_ksettings
*cmd
)
1055 struct ravb_private
*priv
= netdev_priv(ndev
);
1056 int error
= -ENODEV
;
1057 unsigned long flags
;
1060 spin_lock_irqsave(&priv
->lock
, flags
);
1061 error
= phy_ethtool_ksettings_get(ndev
->phydev
, cmd
);
1062 spin_unlock_irqrestore(&priv
->lock
, flags
);
1068 static int ravb_set_link_ksettings(struct net_device
*ndev
,
1069 const struct ethtool_link_ksettings
*cmd
)
1071 struct ravb_private
*priv
= netdev_priv(ndev
);
1072 unsigned long flags
;
1078 spin_lock_irqsave(&priv
->lock
, flags
);
1080 /* Disable TX and RX */
1081 ravb_rcv_snd_disable(ndev
);
1083 error
= phy_ethtool_ksettings_set(ndev
->phydev
, cmd
);
1087 if (cmd
->base
.duplex
== DUPLEX_FULL
)
1092 ravb_set_duplex(ndev
);
1097 /* Enable TX and RX */
1098 ravb_rcv_snd_enable(ndev
);
1101 spin_unlock_irqrestore(&priv
->lock
, flags
);
1106 static int ravb_nway_reset(struct net_device
*ndev
)
1108 struct ravb_private
*priv
= netdev_priv(ndev
);
1109 int error
= -ENODEV
;
1110 unsigned long flags
;
1113 spin_lock_irqsave(&priv
->lock
, flags
);
1114 error
= phy_start_aneg(ndev
->phydev
);
1115 spin_unlock_irqrestore(&priv
->lock
, flags
);
1121 static u32
ravb_get_msglevel(struct net_device
*ndev
)
1123 struct ravb_private
*priv
= netdev_priv(ndev
);
1125 return priv
->msg_enable
;
1128 static void ravb_set_msglevel(struct net_device
*ndev
, u32 value
)
1130 struct ravb_private
*priv
= netdev_priv(ndev
);
1132 priv
->msg_enable
= value
;
1135 static const char ravb_gstrings_stats
[][ETH_GSTRING_LEN
] = {
1136 "rx_queue_0_current",
1137 "tx_queue_0_current",
1140 "rx_queue_0_packets",
1141 "tx_queue_0_packets",
1144 "rx_queue_0_mcast_packets",
1145 "rx_queue_0_errors",
1146 "rx_queue_0_crc_errors",
1147 "rx_queue_0_frame_errors",
1148 "rx_queue_0_length_errors",
1149 "rx_queue_0_missed_errors",
1150 "rx_queue_0_over_errors",
1152 "rx_queue_1_current",
1153 "tx_queue_1_current",
1156 "rx_queue_1_packets",
1157 "tx_queue_1_packets",
1160 "rx_queue_1_mcast_packets",
1161 "rx_queue_1_errors",
1162 "rx_queue_1_crc_errors",
1163 "rx_queue_1_frame_errors",
1164 "rx_queue_1_length_errors",
1165 "rx_queue_1_missed_errors",
1166 "rx_queue_1_over_errors",
1169 #define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1171 static int ravb_get_sset_count(struct net_device
*netdev
, int sset
)
1175 return RAVB_STATS_LEN
;
1181 static void ravb_get_ethtool_stats(struct net_device
*ndev
,
1182 struct ethtool_stats
*stats
, u64
*data
)
1184 struct ravb_private
*priv
= netdev_priv(ndev
);
1188 /* Device-specific stats */
1189 for (q
= RAVB_BE
; q
< NUM_RX_QUEUE
; q
++) {
1190 struct net_device_stats
*stats
= &priv
->stats
[q
];
1192 data
[i
++] = priv
->cur_rx
[q
];
1193 data
[i
++] = priv
->cur_tx
[q
];
1194 data
[i
++] = priv
->dirty_rx
[q
];
1195 data
[i
++] = priv
->dirty_tx
[q
];
1196 data
[i
++] = stats
->rx_packets
;
1197 data
[i
++] = stats
->tx_packets
;
1198 data
[i
++] = stats
->rx_bytes
;
1199 data
[i
++] = stats
->tx_bytes
;
1200 data
[i
++] = stats
->multicast
;
1201 data
[i
++] = stats
->rx_errors
;
1202 data
[i
++] = stats
->rx_crc_errors
;
1203 data
[i
++] = stats
->rx_frame_errors
;
1204 data
[i
++] = stats
->rx_length_errors
;
1205 data
[i
++] = stats
->rx_missed_errors
;
1206 data
[i
++] = stats
->rx_over_errors
;
1210 static void ravb_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
1212 switch (stringset
) {
1214 memcpy(data
, *ravb_gstrings_stats
, sizeof(ravb_gstrings_stats
));
1219 static void ravb_get_ringparam(struct net_device
*ndev
,
1220 struct ethtool_ringparam
*ring
)
1222 struct ravb_private
*priv
= netdev_priv(ndev
);
1224 ring
->rx_max_pending
= BE_RX_RING_MAX
;
1225 ring
->tx_max_pending
= BE_TX_RING_MAX
;
1226 ring
->rx_pending
= priv
->num_rx_ring
[RAVB_BE
];
1227 ring
->tx_pending
= priv
->num_tx_ring
[RAVB_BE
];
1230 static int ravb_set_ringparam(struct net_device
*ndev
,
1231 struct ethtool_ringparam
*ring
)
1233 struct ravb_private
*priv
= netdev_priv(ndev
);
1236 if (ring
->tx_pending
> BE_TX_RING_MAX
||
1237 ring
->rx_pending
> BE_RX_RING_MAX
||
1238 ring
->tx_pending
< BE_TX_RING_MIN
||
1239 ring
->rx_pending
< BE_RX_RING_MIN
)
1241 if (ring
->rx_mini_pending
|| ring
->rx_jumbo_pending
)
1244 if (netif_running(ndev
)) {
1245 netif_device_detach(ndev
);
1246 /* Stop PTP Clock driver */
1247 if (priv
->chip_id
== RCAR_GEN2
)
1248 ravb_ptp_stop(ndev
);
1249 /* Wait for DMA stopping */
1250 error
= ravb_stop_dma(ndev
);
1253 "cannot set ringparam! Any AVB processes are still running?\n");
1256 synchronize_irq(ndev
->irq
);
1258 /* Free all the skb's in the RX queue and the DMA buffers. */
1259 ravb_ring_free(ndev
, RAVB_BE
);
1260 ravb_ring_free(ndev
, RAVB_NC
);
1263 /* Set new parameters */
1264 priv
->num_rx_ring
[RAVB_BE
] = ring
->rx_pending
;
1265 priv
->num_tx_ring
[RAVB_BE
] = ring
->tx_pending
;
1267 if (netif_running(ndev
)) {
1268 error
= ravb_dmac_init(ndev
);
1271 "%s: ravb_dmac_init() failed, error %d\n",
1276 ravb_emac_init(ndev
);
1278 /* Initialise PTP Clock driver */
1279 if (priv
->chip_id
== RCAR_GEN2
)
1280 ravb_ptp_init(ndev
, priv
->pdev
);
1282 netif_device_attach(ndev
);
1288 static int ravb_get_ts_info(struct net_device
*ndev
,
1289 struct ethtool_ts_info
*info
)
1291 struct ravb_private
*priv
= netdev_priv(ndev
);
1293 info
->so_timestamping
=
1294 SOF_TIMESTAMPING_TX_SOFTWARE
|
1295 SOF_TIMESTAMPING_RX_SOFTWARE
|
1296 SOF_TIMESTAMPING_SOFTWARE
|
1297 SOF_TIMESTAMPING_TX_HARDWARE
|
1298 SOF_TIMESTAMPING_RX_HARDWARE
|
1299 SOF_TIMESTAMPING_RAW_HARDWARE
;
1300 info
->tx_types
= (1 << HWTSTAMP_TX_OFF
) | (1 << HWTSTAMP_TX_ON
);
1302 (1 << HWTSTAMP_FILTER_NONE
) |
1303 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT
) |
1304 (1 << HWTSTAMP_FILTER_ALL
);
1305 info
->phc_index
= ptp_clock_index(priv
->ptp
.clock
);
1310 static const struct ethtool_ops ravb_ethtool_ops
= {
1311 .nway_reset
= ravb_nway_reset
,
1312 .get_msglevel
= ravb_get_msglevel
,
1313 .set_msglevel
= ravb_set_msglevel
,
1314 .get_link
= ethtool_op_get_link
,
1315 .get_strings
= ravb_get_strings
,
1316 .get_ethtool_stats
= ravb_get_ethtool_stats
,
1317 .get_sset_count
= ravb_get_sset_count
,
1318 .get_ringparam
= ravb_get_ringparam
,
1319 .set_ringparam
= ravb_set_ringparam
,
1320 .get_ts_info
= ravb_get_ts_info
,
1321 .get_link_ksettings
= ravb_get_link_ksettings
,
1322 .set_link_ksettings
= ravb_set_link_ksettings
,
1325 static inline int ravb_hook_irq(unsigned int irq
, irq_handler_t handler
,
1326 struct net_device
*ndev
, struct device
*dev
,
1332 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s:%s", ndev
->name
, ch
);
1335 error
= request_irq(irq
, handler
, 0, name
, ndev
);
1337 netdev_err(ndev
, "cannot request IRQ %s\n", name
);
1342 /* Network device open function for Ethernet AVB */
1343 static int ravb_open(struct net_device
*ndev
)
1345 struct ravb_private
*priv
= netdev_priv(ndev
);
1346 struct platform_device
*pdev
= priv
->pdev
;
1347 struct device
*dev
= &pdev
->dev
;
1350 napi_enable(&priv
->napi
[RAVB_BE
]);
1351 napi_enable(&priv
->napi
[RAVB_NC
]);
1353 if (priv
->chip_id
== RCAR_GEN2
) {
1354 error
= request_irq(ndev
->irq
, ravb_interrupt
, IRQF_SHARED
,
1357 netdev_err(ndev
, "cannot request IRQ\n");
1361 error
= ravb_hook_irq(ndev
->irq
, ravb_multi_interrupt
, ndev
,
1365 error
= ravb_hook_irq(priv
->emac_irq
, ravb_emac_interrupt
, ndev
,
1369 error
= ravb_hook_irq(priv
->rx_irqs
[RAVB_BE
], ravb_be_interrupt
,
1370 ndev
, dev
, "ch0:rx_be");
1372 goto out_free_irq_emac
;
1373 error
= ravb_hook_irq(priv
->tx_irqs
[RAVB_BE
], ravb_be_interrupt
,
1374 ndev
, dev
, "ch18:tx_be");
1376 goto out_free_irq_be_rx
;
1377 error
= ravb_hook_irq(priv
->rx_irqs
[RAVB_NC
], ravb_nc_interrupt
,
1378 ndev
, dev
, "ch1:rx_nc");
1380 goto out_free_irq_be_tx
;
1381 error
= ravb_hook_irq(priv
->tx_irqs
[RAVB_NC
], ravb_nc_interrupt
,
1382 ndev
, dev
, "ch19:tx_nc");
1384 goto out_free_irq_nc_rx
;
1388 error
= ravb_dmac_init(ndev
);
1390 goto out_free_irq_nc_tx
;
1391 ravb_emac_init(ndev
);
1393 /* Initialise PTP Clock driver */
1394 if (priv
->chip_id
== RCAR_GEN2
)
1395 ravb_ptp_init(ndev
, priv
->pdev
);
1397 netif_tx_start_all_queues(ndev
);
1399 /* PHY control start */
1400 error
= ravb_phy_start(ndev
);
1407 /* Stop PTP Clock driver */
1408 if (priv
->chip_id
== RCAR_GEN2
)
1409 ravb_ptp_stop(ndev
);
1411 if (priv
->chip_id
== RCAR_GEN2
)
1413 free_irq(priv
->tx_irqs
[RAVB_NC
], ndev
);
1415 free_irq(priv
->rx_irqs
[RAVB_NC
], ndev
);
1417 free_irq(priv
->tx_irqs
[RAVB_BE
], ndev
);
1419 free_irq(priv
->rx_irqs
[RAVB_BE
], ndev
);
1421 free_irq(priv
->emac_irq
, ndev
);
1423 free_irq(ndev
->irq
, ndev
);
1425 napi_disable(&priv
->napi
[RAVB_NC
]);
1426 napi_disable(&priv
->napi
[RAVB_BE
]);
1430 /* Timeout function for Ethernet AVB */
1431 static void ravb_tx_timeout(struct net_device
*ndev
)
1433 struct ravb_private
*priv
= netdev_priv(ndev
);
1435 netif_err(priv
, tx_err
, ndev
,
1436 "transmit timed out, status %08x, resetting...\n",
1437 ravb_read(ndev
, ISS
));
1439 /* tx_errors count up */
1440 ndev
->stats
.tx_errors
++;
1442 schedule_work(&priv
->work
);
1445 static void ravb_tx_timeout_work(struct work_struct
*work
)
1447 struct ravb_private
*priv
= container_of(work
, struct ravb_private
,
1449 struct net_device
*ndev
= priv
->ndev
;
1451 netif_tx_stop_all_queues(ndev
);
1453 /* Stop PTP Clock driver */
1454 if (priv
->chip_id
== RCAR_GEN2
)
1455 ravb_ptp_stop(ndev
);
1457 /* Wait for DMA stopping */
1458 ravb_stop_dma(ndev
);
1460 ravb_ring_free(ndev
, RAVB_BE
);
1461 ravb_ring_free(ndev
, RAVB_NC
);
1464 ravb_dmac_init(ndev
);
1465 ravb_emac_init(ndev
);
1467 /* Initialise PTP Clock driver */
1468 if (priv
->chip_id
== RCAR_GEN2
)
1469 ravb_ptp_init(ndev
, priv
->pdev
);
1471 netif_tx_start_all_queues(ndev
);
1474 /* Packet transmit function for Ethernet AVB */
1475 static netdev_tx_t
ravb_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1477 struct ravb_private
*priv
= netdev_priv(ndev
);
1478 u16 q
= skb_get_queue_mapping(skb
);
1479 struct ravb_tstamp_skb
*ts_skb
;
1480 struct ravb_tx_desc
*desc
;
1481 unsigned long flags
;
1487 spin_lock_irqsave(&priv
->lock
, flags
);
1488 if (priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] > (priv
->num_tx_ring
[q
] - 1) *
1490 netif_err(priv
, tx_queued
, ndev
,
1491 "still transmitting with the full ring!\n");
1492 netif_stop_subqueue(ndev
, q
);
1493 spin_unlock_irqrestore(&priv
->lock
, flags
);
1494 return NETDEV_TX_BUSY
;
1496 entry
= priv
->cur_tx
[q
] % (priv
->num_tx_ring
[q
] * NUM_TX_DESC
);
1497 priv
->tx_skb
[q
][entry
/ NUM_TX_DESC
] = skb
;
1499 if (skb_put_padto(skb
, ETH_ZLEN
))
1502 buffer
= PTR_ALIGN(priv
->tx_align
[q
], DPTR_ALIGN
) +
1503 entry
/ NUM_TX_DESC
* DPTR_ALIGN
;
1504 len
= PTR_ALIGN(skb
->data
, DPTR_ALIGN
) - skb
->data
;
1505 memcpy(buffer
, skb
->data
, len
);
1506 dma_addr
= dma_map_single(ndev
->dev
.parent
, buffer
, len
, DMA_TO_DEVICE
);
1507 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
1510 desc
= &priv
->tx_ring
[q
][entry
];
1511 desc
->ds_tagl
= cpu_to_le16(len
);
1512 desc
->dptr
= cpu_to_le32(dma_addr
);
1514 buffer
= skb
->data
+ len
;
1515 len
= skb
->len
- len
;
1516 dma_addr
= dma_map_single(ndev
->dev
.parent
, buffer
, len
, DMA_TO_DEVICE
);
1517 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
1521 desc
->ds_tagl
= cpu_to_le16(len
);
1522 desc
->dptr
= cpu_to_le32(dma_addr
);
1524 /* TX timestamp required */
1526 ts_skb
= kmalloc(sizeof(*ts_skb
), GFP_ATOMIC
);
1529 dma_unmap_single(ndev
->dev
.parent
, dma_addr
, len
,
1534 ts_skb
->tag
= priv
->ts_skb_tag
++;
1535 priv
->ts_skb_tag
&= 0x3ff;
1536 list_add_tail(&ts_skb
->list
, &priv
->ts_skb_list
);
1538 /* TAG and timestamp required flag */
1539 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
1540 desc
->tagh_tsr
= (ts_skb
->tag
>> 4) | TX_TSR
;
1541 desc
->ds_tagl
|= le16_to_cpu(ts_skb
->tag
<< 12);
1544 skb_tx_timestamp(skb
);
1545 /* Descriptor type must be set after all the above writes */
1547 desc
->die_dt
= DT_FEND
;
1549 desc
->die_dt
= DT_FSTART
;
1551 ravb_modify(ndev
, TCCR
, TCCR_TSRQ0
<< q
, TCCR_TSRQ0
<< q
);
1553 priv
->cur_tx
[q
] += NUM_TX_DESC
;
1554 if (priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] >
1555 (priv
->num_tx_ring
[q
] - 1) * NUM_TX_DESC
&& !ravb_tx_free(ndev
, q
))
1556 netif_stop_subqueue(ndev
, q
);
1560 spin_unlock_irqrestore(&priv
->lock
, flags
);
1561 return NETDEV_TX_OK
;
1564 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
1565 le16_to_cpu(desc
->ds_tagl
), DMA_TO_DEVICE
);
1567 dev_kfree_skb_any(skb
);
1568 priv
->tx_skb
[q
][entry
/ NUM_TX_DESC
] = NULL
;
1572 static u16
ravb_select_queue(struct net_device
*ndev
, struct sk_buff
*skb
,
1573 void *accel_priv
, select_queue_fallback_t fallback
)
1575 /* If skb needs TX timestamp, it is handled in network control queue */
1576 return (skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
) ? RAVB_NC
:
1581 static struct net_device_stats
*ravb_get_stats(struct net_device
*ndev
)
1583 struct ravb_private
*priv
= netdev_priv(ndev
);
1584 struct net_device_stats
*nstats
, *stats0
, *stats1
;
1586 nstats
= &ndev
->stats
;
1587 stats0
= &priv
->stats
[RAVB_BE
];
1588 stats1
= &priv
->stats
[RAVB_NC
];
1590 nstats
->tx_dropped
+= ravb_read(ndev
, TROCR
);
1591 ravb_write(ndev
, 0, TROCR
); /* (write clear) */
1592 nstats
->collisions
+= ravb_read(ndev
, CDCR
);
1593 ravb_write(ndev
, 0, CDCR
); /* (write clear) */
1594 nstats
->tx_carrier_errors
+= ravb_read(ndev
, LCCR
);
1595 ravb_write(ndev
, 0, LCCR
); /* (write clear) */
1597 nstats
->tx_carrier_errors
+= ravb_read(ndev
, CERCR
);
1598 ravb_write(ndev
, 0, CERCR
); /* (write clear) */
1599 nstats
->tx_carrier_errors
+= ravb_read(ndev
, CEECR
);
1600 ravb_write(ndev
, 0, CEECR
); /* (write clear) */
1602 nstats
->rx_packets
= stats0
->rx_packets
+ stats1
->rx_packets
;
1603 nstats
->tx_packets
= stats0
->tx_packets
+ stats1
->tx_packets
;
1604 nstats
->rx_bytes
= stats0
->rx_bytes
+ stats1
->rx_bytes
;
1605 nstats
->tx_bytes
= stats0
->tx_bytes
+ stats1
->tx_bytes
;
1606 nstats
->multicast
= stats0
->multicast
+ stats1
->multicast
;
1607 nstats
->rx_errors
= stats0
->rx_errors
+ stats1
->rx_errors
;
1608 nstats
->rx_crc_errors
= stats0
->rx_crc_errors
+ stats1
->rx_crc_errors
;
1609 nstats
->rx_frame_errors
=
1610 stats0
->rx_frame_errors
+ stats1
->rx_frame_errors
;
1611 nstats
->rx_length_errors
=
1612 stats0
->rx_length_errors
+ stats1
->rx_length_errors
;
1613 nstats
->rx_missed_errors
=
1614 stats0
->rx_missed_errors
+ stats1
->rx_missed_errors
;
1615 nstats
->rx_over_errors
=
1616 stats0
->rx_over_errors
+ stats1
->rx_over_errors
;
1621 /* Update promiscuous bit */
1622 static void ravb_set_rx_mode(struct net_device
*ndev
)
1624 struct ravb_private
*priv
= netdev_priv(ndev
);
1625 unsigned long flags
;
1627 spin_lock_irqsave(&priv
->lock
, flags
);
1628 ravb_modify(ndev
, ECMR
, ECMR_PRM
,
1629 ndev
->flags
& IFF_PROMISC
? ECMR_PRM
: 0);
1631 spin_unlock_irqrestore(&priv
->lock
, flags
);
1634 /* Device close function for Ethernet AVB */
1635 static int ravb_close(struct net_device
*ndev
)
1637 struct ravb_private
*priv
= netdev_priv(ndev
);
1638 struct ravb_tstamp_skb
*ts_skb
, *ts_skb2
;
1640 netif_tx_stop_all_queues(ndev
);
1642 /* Disable interrupts by clearing the interrupt masks. */
1643 ravb_write(ndev
, 0, RIC0
);
1644 ravb_write(ndev
, 0, RIC2
);
1645 ravb_write(ndev
, 0, TIC
);
1647 /* Stop PTP Clock driver */
1648 if (priv
->chip_id
== RCAR_GEN2
)
1649 ravb_ptp_stop(ndev
);
1651 /* Set the config mode to stop the AVB-DMAC's processes */
1652 if (ravb_stop_dma(ndev
) < 0)
1654 "device will be stopped after h/w processes are done.\n");
1656 /* Clear the timestamp list */
1657 list_for_each_entry_safe(ts_skb
, ts_skb2
, &priv
->ts_skb_list
, list
) {
1658 list_del(&ts_skb
->list
);
1662 /* PHY disconnect */
1664 phy_stop(ndev
->phydev
);
1665 phy_disconnect(ndev
->phydev
);
1668 if (priv
->chip_id
!= RCAR_GEN2
) {
1669 free_irq(priv
->tx_irqs
[RAVB_NC
], ndev
);
1670 free_irq(priv
->rx_irqs
[RAVB_NC
], ndev
);
1671 free_irq(priv
->tx_irqs
[RAVB_BE
], ndev
);
1672 free_irq(priv
->rx_irqs
[RAVB_BE
], ndev
);
1673 free_irq(priv
->emac_irq
, ndev
);
1675 free_irq(ndev
->irq
, ndev
);
1677 napi_disable(&priv
->napi
[RAVB_NC
]);
1678 napi_disable(&priv
->napi
[RAVB_BE
]);
1680 /* Free all the skb's in the RX queue and the DMA buffers. */
1681 ravb_ring_free(ndev
, RAVB_BE
);
1682 ravb_ring_free(ndev
, RAVB_NC
);
1687 static int ravb_hwtstamp_get(struct net_device
*ndev
, struct ifreq
*req
)
1689 struct ravb_private
*priv
= netdev_priv(ndev
);
1690 struct hwtstamp_config config
;
1693 config
.tx_type
= priv
->tstamp_tx_ctrl
? HWTSTAMP_TX_ON
:
1695 if (priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
)
1696 config
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_L2_EVENT
;
1697 else if (priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE_ALL
)
1698 config
.rx_filter
= HWTSTAMP_FILTER_ALL
;
1700 config
.rx_filter
= HWTSTAMP_FILTER_NONE
;
1702 return copy_to_user(req
->ifr_data
, &config
, sizeof(config
)) ?
1706 /* Control hardware time stamping */
1707 static int ravb_hwtstamp_set(struct net_device
*ndev
, struct ifreq
*req
)
1709 struct ravb_private
*priv
= netdev_priv(ndev
);
1710 struct hwtstamp_config config
;
1711 u32 tstamp_rx_ctrl
= RAVB_RXTSTAMP_ENABLED
;
1714 if (copy_from_user(&config
, req
->ifr_data
, sizeof(config
)))
1717 /* Reserved for future extensions */
1721 switch (config
.tx_type
) {
1722 case HWTSTAMP_TX_OFF
:
1725 case HWTSTAMP_TX_ON
:
1726 tstamp_tx_ctrl
= RAVB_TXTSTAMP_ENABLED
;
1732 switch (config
.rx_filter
) {
1733 case HWTSTAMP_FILTER_NONE
:
1736 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
1737 tstamp_rx_ctrl
|= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
;
1740 config
.rx_filter
= HWTSTAMP_FILTER_ALL
;
1741 tstamp_rx_ctrl
|= RAVB_RXTSTAMP_TYPE_ALL
;
1744 priv
->tstamp_tx_ctrl
= tstamp_tx_ctrl
;
1745 priv
->tstamp_rx_ctrl
= tstamp_rx_ctrl
;
1747 return copy_to_user(req
->ifr_data
, &config
, sizeof(config
)) ?
1751 /* ioctl to device function */
1752 static int ravb_do_ioctl(struct net_device
*ndev
, struct ifreq
*req
, int cmd
)
1754 struct phy_device
*phydev
= ndev
->phydev
;
1756 if (!netif_running(ndev
))
1764 return ravb_hwtstamp_get(ndev
, req
);
1766 return ravb_hwtstamp_set(ndev
, req
);
1769 return phy_mii_ioctl(phydev
, req
, cmd
);
1772 static const struct net_device_ops ravb_netdev_ops
= {
1773 .ndo_open
= ravb_open
,
1774 .ndo_stop
= ravb_close
,
1775 .ndo_start_xmit
= ravb_start_xmit
,
1776 .ndo_select_queue
= ravb_select_queue
,
1777 .ndo_get_stats
= ravb_get_stats
,
1778 .ndo_set_rx_mode
= ravb_set_rx_mode
,
1779 .ndo_tx_timeout
= ravb_tx_timeout
,
1780 .ndo_do_ioctl
= ravb_do_ioctl
,
1781 .ndo_validate_addr
= eth_validate_addr
,
1782 .ndo_set_mac_address
= eth_mac_addr
,
1783 .ndo_change_mtu
= eth_change_mtu
,
1786 /* MDIO bus init function */
1787 static int ravb_mdio_init(struct ravb_private
*priv
)
1789 struct platform_device
*pdev
= priv
->pdev
;
1790 struct device
*dev
= &pdev
->dev
;
1794 priv
->mdiobb
.ops
= &bb_ops
;
1796 /* MII controller setting */
1797 priv
->mii_bus
= alloc_mdio_bitbang(&priv
->mdiobb
);
1801 /* Hook up MII support for ethtool */
1802 priv
->mii_bus
->name
= "ravb_mii";
1803 priv
->mii_bus
->parent
= dev
;
1804 snprintf(priv
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
1805 pdev
->name
, pdev
->id
);
1807 /* Register MDIO bus */
1808 error
= of_mdiobus_register(priv
->mii_bus
, dev
->of_node
);
1815 free_mdio_bitbang(priv
->mii_bus
);
1819 /* MDIO bus release function */
1820 static int ravb_mdio_release(struct ravb_private
*priv
)
1822 /* Unregister mdio bus */
1823 mdiobus_unregister(priv
->mii_bus
);
1825 /* Free bitbang info */
1826 free_mdio_bitbang(priv
->mii_bus
);
1831 static const struct of_device_id ravb_match_table
[] = {
1832 { .compatible
= "renesas,etheravb-r8a7790", .data
= (void *)RCAR_GEN2
},
1833 { .compatible
= "renesas,etheravb-r8a7794", .data
= (void *)RCAR_GEN2
},
1834 { .compatible
= "renesas,etheravb-rcar-gen2", .data
= (void *)RCAR_GEN2
},
1835 { .compatible
= "renesas,etheravb-r8a7795", .data
= (void *)RCAR_GEN3
},
1836 { .compatible
= "renesas,etheravb-rcar-gen3", .data
= (void *)RCAR_GEN3
},
1839 MODULE_DEVICE_TABLE(of
, ravb_match_table
);
1841 static int ravb_set_gti(struct net_device
*ndev
)
1844 struct device
*dev
= ndev
->dev
.parent
;
1845 struct device_node
*np
= dev
->of_node
;
1850 clk
= of_clk_get(np
, 0);
1852 dev_err(dev
, "could not get clock\n");
1853 return PTR_ERR(clk
);
1856 rate
= clk_get_rate(clk
);
1862 inc
= 1000000000ULL << 20;
1865 if (inc
< GTI_TIV_MIN
|| inc
> GTI_TIV_MAX
) {
1866 dev_err(dev
, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1867 inc
, GTI_TIV_MIN
, GTI_TIV_MAX
);
1871 ravb_write(ndev
, inc
, GTI
);
1876 static void ravb_set_config_mode(struct net_device
*ndev
)
1878 struct ravb_private
*priv
= netdev_priv(ndev
);
1880 if (priv
->chip_id
== RCAR_GEN2
) {
1881 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_CONFIG
);
1882 /* Set CSEL value */
1883 ravb_modify(ndev
, CCC
, CCC_CSEL
, CCC_CSEL_HPB
);
1885 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_CONFIG
|
1886 CCC_GAC
| CCC_CSEL_HPB
);
1890 static int ravb_probe(struct platform_device
*pdev
)
1892 struct device_node
*np
= pdev
->dev
.of_node
;
1893 struct ravb_private
*priv
;
1894 enum ravb_chip_id chip_id
;
1895 struct net_device
*ndev
;
1897 struct resource
*res
;
1902 "this driver is required to be instantiated from device tree\n");
1906 /* Get base address */
1907 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1909 dev_err(&pdev
->dev
, "invalid resource\n");
1913 ndev
= alloc_etherdev_mqs(sizeof(struct ravb_private
),
1914 NUM_TX_QUEUE
, NUM_RX_QUEUE
);
1918 pm_runtime_enable(&pdev
->dev
);
1919 pm_runtime_get_sync(&pdev
->dev
);
1921 /* The Ether-specific entries in the device structure. */
1922 ndev
->base_addr
= res
->start
;
1924 chip_id
= (enum ravb_chip_id
)of_device_get_match_data(&pdev
->dev
);
1926 if (chip_id
== RCAR_GEN3
)
1927 irq
= platform_get_irq_byname(pdev
, "ch22");
1929 irq
= platform_get_irq(pdev
, 0);
1936 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1938 priv
= netdev_priv(ndev
);
1941 priv
->num_tx_ring
[RAVB_BE
] = BE_TX_RING_SIZE
;
1942 priv
->num_rx_ring
[RAVB_BE
] = BE_RX_RING_SIZE
;
1943 priv
->num_tx_ring
[RAVB_NC
] = NC_TX_RING_SIZE
;
1944 priv
->num_rx_ring
[RAVB_NC
] = NC_RX_RING_SIZE
;
1945 priv
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
1946 if (IS_ERR(priv
->addr
)) {
1947 error
= PTR_ERR(priv
->addr
);
1951 spin_lock_init(&priv
->lock
);
1952 INIT_WORK(&priv
->work
, ravb_tx_timeout_work
);
1954 priv
->phy_interface
= of_get_phy_mode(np
);
1956 priv
->no_avb_link
= of_property_read_bool(np
, "renesas,no-ether-link");
1957 priv
->avb_link_active_low
=
1958 of_property_read_bool(np
, "renesas,ether-link-active-low");
1960 if (chip_id
== RCAR_GEN3
) {
1961 irq
= platform_get_irq_byname(pdev
, "ch24");
1966 priv
->emac_irq
= irq
;
1967 for (i
= 0; i
< NUM_RX_QUEUE
; i
++) {
1968 irq
= platform_get_irq_byname(pdev
, ravb_rx_irqs
[i
]);
1973 priv
->rx_irqs
[i
] = irq
;
1975 for (i
= 0; i
< NUM_TX_QUEUE
; i
++) {
1976 irq
= platform_get_irq_byname(pdev
, ravb_tx_irqs
[i
]);
1981 priv
->tx_irqs
[i
] = irq
;
1985 priv
->chip_id
= chip_id
;
1988 ndev
->netdev_ops
= &ravb_netdev_ops
;
1989 ndev
->ethtool_ops
= &ravb_ethtool_ops
;
1991 /* Set AVB config mode */
1992 ravb_set_config_mode(ndev
);
1995 error
= ravb_set_gti(ndev
);
1999 /* Request GTI loading */
2000 ravb_modify(ndev
, GCCR
, GCCR_LTI
, GCCR_LTI
);
2002 /* Allocate descriptor base address table */
2003 priv
->desc_bat_size
= sizeof(struct ravb_desc
) * DBAT_ENTRY_NUM
;
2004 priv
->desc_bat
= dma_alloc_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
,
2005 &priv
->desc_bat_dma
, GFP_KERNEL
);
2006 if (!priv
->desc_bat
) {
2008 "Cannot allocate desc base address table (size %d bytes)\n",
2009 priv
->desc_bat_size
);
2013 for (q
= RAVB_BE
; q
< DBAT_ENTRY_NUM
; q
++)
2014 priv
->desc_bat
[q
].die_dt
= DT_EOS
;
2015 ravb_write(ndev
, priv
->desc_bat_dma
, DBAT
);
2017 /* Initialise HW timestamp list */
2018 INIT_LIST_HEAD(&priv
->ts_skb_list
);
2020 /* Initialise PTP Clock driver */
2021 if (chip_id
!= RCAR_GEN2
)
2022 ravb_ptp_init(ndev
, pdev
);
2024 /* Debug message level */
2025 priv
->msg_enable
= RAVB_DEF_MSG_ENABLE
;
2027 /* Read and set MAC address */
2028 ravb_read_mac_address(ndev
, of_get_mac_address(np
));
2029 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
2030 dev_warn(&pdev
->dev
,
2031 "no valid MAC address supplied, using a random one\n");
2032 eth_hw_addr_random(ndev
);
2036 error
= ravb_mdio_init(priv
);
2038 dev_err(&pdev
->dev
, "failed to initialize MDIO\n");
2042 netif_napi_add(ndev
, &priv
->napi
[RAVB_BE
], ravb_poll
, 64);
2043 netif_napi_add(ndev
, &priv
->napi
[RAVB_NC
], ravb_poll
, 64);
2045 /* Network device register */
2046 error
= register_netdev(ndev
);
2050 /* Print device information */
2051 netdev_info(ndev
, "Base address at %#x, %pM, IRQ %d.\n",
2052 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
2054 platform_set_drvdata(pdev
, ndev
);
2059 netif_napi_del(&priv
->napi
[RAVB_NC
]);
2060 netif_napi_del(&priv
->napi
[RAVB_BE
]);
2061 ravb_mdio_release(priv
);
2063 dma_free_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
, priv
->desc_bat
,
2064 priv
->desc_bat_dma
);
2066 /* Stop PTP Clock driver */
2067 if (chip_id
!= RCAR_GEN2
)
2068 ravb_ptp_stop(ndev
);
2073 pm_runtime_put(&pdev
->dev
);
2074 pm_runtime_disable(&pdev
->dev
);
2078 static int ravb_remove(struct platform_device
*pdev
)
2080 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2081 struct ravb_private
*priv
= netdev_priv(ndev
);
2083 /* Stop PTP Clock driver */
2084 if (priv
->chip_id
!= RCAR_GEN2
)
2085 ravb_ptp_stop(ndev
);
2087 dma_free_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
, priv
->desc_bat
,
2088 priv
->desc_bat_dma
);
2089 /* Set reset mode */
2090 ravb_write(ndev
, CCC_OPC_RESET
, CCC
);
2091 pm_runtime_put_sync(&pdev
->dev
);
2092 unregister_netdev(ndev
);
2093 netif_napi_del(&priv
->napi
[RAVB_NC
]);
2094 netif_napi_del(&priv
->napi
[RAVB_BE
]);
2095 ravb_mdio_release(priv
);
2096 pm_runtime_disable(&pdev
->dev
);
2098 platform_set_drvdata(pdev
, NULL
);
2103 static int __maybe_unused
ravb_suspend(struct device
*dev
)
2105 struct net_device
*ndev
= dev_get_drvdata(dev
);
2108 if (netif_running(ndev
)) {
2109 netif_device_detach(ndev
);
2110 ret
= ravb_close(ndev
);
2116 static int __maybe_unused
ravb_resume(struct device
*dev
)
2118 struct net_device
*ndev
= dev_get_drvdata(dev
);
2119 struct ravb_private
*priv
= netdev_priv(ndev
);
2122 /* All register have been reset to default values.
2123 * Restore all registers which where setup at probe time and
2124 * reopen device if it was running before system suspended.
2127 /* Set AVB config mode */
2128 ravb_set_config_mode(ndev
);
2131 ret
= ravb_set_gti(ndev
);
2135 /* Request GTI loading */
2136 ravb_modify(ndev
, GCCR
, GCCR_LTI
, GCCR_LTI
);
2138 /* Restore descriptor base address table */
2139 ravb_write(ndev
, priv
->desc_bat_dma
, DBAT
);
2141 if (netif_running(ndev
)) {
2142 ret
= ravb_open(ndev
);
2145 netif_device_attach(ndev
);
2151 static int __maybe_unused
ravb_runtime_nop(struct device
*dev
)
2153 /* Runtime PM callback shared between ->runtime_suspend()
2154 * and ->runtime_resume(). Simply returns success.
2156 * This driver re-initializes all registers after
2157 * pm_runtime_get_sync() anyway so there is no need
2158 * to save and restore registers here.
2163 static const struct dev_pm_ops ravb_dev_pm_ops
= {
2164 SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend
, ravb_resume
)
2165 SET_RUNTIME_PM_OPS(ravb_runtime_nop
, ravb_runtime_nop
, NULL
)
2168 static struct platform_driver ravb_driver
= {
2169 .probe
= ravb_probe
,
2170 .remove
= ravb_remove
,
2173 .pm
= &ravb_dev_pm_ops
,
2174 .of_match_table
= ravb_match_table
,
2178 module_platform_driver(ravb_driver
);
2180 MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2181 MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2182 MODULE_LICENSE("GPL v2");