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>
34 #include <linux/sys_soc.h>
36 #include <asm/div64.h>
40 #define RAVB_DEF_MSG_ENABLE \
46 static const char *ravb_rx_irqs
[NUM_RX_QUEUE
] = {
51 static const char *ravb_tx_irqs
[NUM_TX_QUEUE
] = {
56 void ravb_modify(struct net_device
*ndev
, enum ravb_reg reg
, u32 clear
,
59 ravb_write(ndev
, (ravb_read(ndev
, reg
) & ~clear
) | set
, reg
);
62 int ravb_wait(struct net_device
*ndev
, enum ravb_reg reg
, u32 mask
, u32 value
)
66 for (i
= 0; i
< 10000; i
++) {
67 if ((ravb_read(ndev
, reg
) & mask
) == value
)
74 static int ravb_config(struct net_device
*ndev
)
79 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_CONFIG
);
80 /* Check if the operating mode is changed to the config mode */
81 error
= ravb_wait(ndev
, CSR
, CSR_OPS
, CSR_OPS_CONFIG
);
83 netdev_err(ndev
, "failed to switch device to config mode\n");
88 static void ravb_set_duplex(struct net_device
*ndev
)
90 struct ravb_private
*priv
= netdev_priv(ndev
);
92 ravb_modify(ndev
, ECMR
, ECMR_DM
, priv
->duplex
? ECMR_DM
: 0);
95 static void ravb_set_rate(struct net_device
*ndev
)
97 struct ravb_private
*priv
= netdev_priv(ndev
);
99 switch (priv
->speed
) {
100 case 100: /* 100BASE */
101 ravb_write(ndev
, GECMR_SPEED_100
, GECMR
);
103 case 1000: /* 1000BASE */
104 ravb_write(ndev
, GECMR_SPEED_1000
, GECMR
);
109 static void ravb_set_buffer_align(struct sk_buff
*skb
)
111 u32 reserve
= (unsigned long)skb
->data
& (RAVB_ALIGN
- 1);
114 skb_reserve(skb
, RAVB_ALIGN
- reserve
);
117 /* Get MAC address from the MAC address registers
119 * Ethernet AVB device doesn't have ROM for MAC address.
120 * This function gets the MAC address that was used by a bootloader.
122 static void ravb_read_mac_address(struct net_device
*ndev
, const u8
*mac
)
125 ether_addr_copy(ndev
->dev_addr
, mac
);
127 u32 mahr
= ravb_read(ndev
, MAHR
);
128 u32 malr
= ravb_read(ndev
, MALR
);
130 ndev
->dev_addr
[0] = (mahr
>> 24) & 0xFF;
131 ndev
->dev_addr
[1] = (mahr
>> 16) & 0xFF;
132 ndev
->dev_addr
[2] = (mahr
>> 8) & 0xFF;
133 ndev
->dev_addr
[3] = (mahr
>> 0) & 0xFF;
134 ndev
->dev_addr
[4] = (malr
>> 8) & 0xFF;
135 ndev
->dev_addr
[5] = (malr
>> 0) & 0xFF;
139 static void ravb_mdio_ctrl(struct mdiobb_ctrl
*ctrl
, u32 mask
, int set
)
141 struct ravb_private
*priv
= container_of(ctrl
, struct ravb_private
,
144 ravb_modify(priv
->ndev
, PIR
, mask
, set
? mask
: 0);
147 /* MDC pin control */
148 static void ravb_set_mdc(struct mdiobb_ctrl
*ctrl
, int level
)
150 ravb_mdio_ctrl(ctrl
, PIR_MDC
, level
);
153 /* Data I/O pin control */
154 static void ravb_set_mdio_dir(struct mdiobb_ctrl
*ctrl
, int output
)
156 ravb_mdio_ctrl(ctrl
, PIR_MMD
, output
);
160 static void ravb_set_mdio_data(struct mdiobb_ctrl
*ctrl
, int value
)
162 ravb_mdio_ctrl(ctrl
, PIR_MDO
, value
);
166 static int ravb_get_mdio_data(struct mdiobb_ctrl
*ctrl
)
168 struct ravb_private
*priv
= container_of(ctrl
, struct ravb_private
,
171 return (ravb_read(priv
->ndev
, PIR
) & PIR_MDI
) != 0;
174 /* MDIO bus control struct */
175 static struct mdiobb_ops bb_ops
= {
176 .owner
= THIS_MODULE
,
177 .set_mdc
= ravb_set_mdc
,
178 .set_mdio_dir
= ravb_set_mdio_dir
,
179 .set_mdio_data
= ravb_set_mdio_data
,
180 .get_mdio_data
= ravb_get_mdio_data
,
183 /* Free TX skb function for AVB-IP */
184 static int ravb_tx_free(struct net_device
*ndev
, int q
, bool free_txed_only
)
186 struct ravb_private
*priv
= netdev_priv(ndev
);
187 struct net_device_stats
*stats
= &priv
->stats
[q
];
188 struct ravb_tx_desc
*desc
;
193 for (; priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] > 0; priv
->dirty_tx
[q
]++) {
196 entry
= priv
->dirty_tx
[q
] % (priv
->num_tx_ring
[q
] *
198 desc
= &priv
->tx_ring
[q
][entry
];
199 txed
= desc
->die_dt
== DT_FEMPTY
;
200 if (free_txed_only
&& !txed
)
202 /* Descriptor type must be checked before all other reads */
204 size
= le16_to_cpu(desc
->ds_tagl
) & TX_DS
;
205 /* Free the original skb. */
206 if (priv
->tx_skb
[q
][entry
/ NUM_TX_DESC
]) {
207 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
208 size
, DMA_TO_DEVICE
);
209 /* Last packet descriptor? */
210 if (entry
% NUM_TX_DESC
== NUM_TX_DESC
- 1) {
211 entry
/= NUM_TX_DESC
;
212 dev_kfree_skb_any(priv
->tx_skb
[q
][entry
]);
213 priv
->tx_skb
[q
][entry
] = NULL
;
220 stats
->tx_bytes
+= size
;
221 desc
->die_dt
= DT_EEMPTY
;
226 /* Free skb's and DMA buffers for Ethernet AVB */
227 static void ravb_ring_free(struct net_device
*ndev
, int q
)
229 struct ravb_private
*priv
= netdev_priv(ndev
);
233 if (priv
->rx_ring
[q
]) {
234 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++) {
235 struct ravb_ex_rx_desc
*desc
= &priv
->rx_ring
[q
][i
];
237 if (!dma_mapping_error(ndev
->dev
.parent
,
238 le32_to_cpu(desc
->dptr
)))
239 dma_unmap_single(ndev
->dev
.parent
,
240 le32_to_cpu(desc
->dptr
),
244 ring_size
= sizeof(struct ravb_ex_rx_desc
) *
245 (priv
->num_rx_ring
[q
] + 1);
246 dma_free_coherent(ndev
->dev
.parent
, ring_size
, priv
->rx_ring
[q
],
247 priv
->rx_desc_dma
[q
]);
248 priv
->rx_ring
[q
] = NULL
;
251 if (priv
->tx_ring
[q
]) {
252 ravb_tx_free(ndev
, q
, false);
254 ring_size
= sizeof(struct ravb_tx_desc
) *
255 (priv
->num_tx_ring
[q
] * NUM_TX_DESC
+ 1);
256 dma_free_coherent(ndev
->dev
.parent
, ring_size
, priv
->tx_ring
[q
],
257 priv
->tx_desc_dma
[q
]);
258 priv
->tx_ring
[q
] = NULL
;
261 /* Free RX skb ringbuffer */
262 if (priv
->rx_skb
[q
]) {
263 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++)
264 dev_kfree_skb(priv
->rx_skb
[q
][i
]);
266 kfree(priv
->rx_skb
[q
]);
267 priv
->rx_skb
[q
] = NULL
;
269 /* Free aligned TX buffers */
270 kfree(priv
->tx_align
[q
]);
271 priv
->tx_align
[q
] = NULL
;
273 /* Free TX skb ringbuffer.
274 * SKBs are freed by ravb_tx_free() call above.
276 kfree(priv
->tx_skb
[q
]);
277 priv
->tx_skb
[q
] = NULL
;
280 /* Format skb and descriptor buffer for Ethernet AVB */
281 static void ravb_ring_format(struct net_device
*ndev
, int q
)
283 struct ravb_private
*priv
= netdev_priv(ndev
);
284 struct ravb_ex_rx_desc
*rx_desc
;
285 struct ravb_tx_desc
*tx_desc
;
286 struct ravb_desc
*desc
;
287 int rx_ring_size
= sizeof(*rx_desc
) * priv
->num_rx_ring
[q
];
288 int tx_ring_size
= sizeof(*tx_desc
) * priv
->num_tx_ring
[q
] *
295 priv
->dirty_rx
[q
] = 0;
296 priv
->dirty_tx
[q
] = 0;
298 memset(priv
->rx_ring
[q
], 0, rx_ring_size
);
299 /* Build RX ring buffer */
300 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++) {
302 rx_desc
= &priv
->rx_ring
[q
][i
];
303 rx_desc
->ds_cc
= cpu_to_le16(PKT_BUF_SZ
);
304 dma_addr
= dma_map_single(ndev
->dev
.parent
, priv
->rx_skb
[q
][i
]->data
,
307 /* We just set the data size to 0 for a failed mapping which
308 * should prevent DMA from happening...
310 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
311 rx_desc
->ds_cc
= cpu_to_le16(0);
312 rx_desc
->dptr
= cpu_to_le32(dma_addr
);
313 rx_desc
->die_dt
= DT_FEMPTY
;
315 rx_desc
= &priv
->rx_ring
[q
][i
];
316 rx_desc
->dptr
= cpu_to_le32((u32
)priv
->rx_desc_dma
[q
]);
317 rx_desc
->die_dt
= DT_LINKFIX
; /* type */
319 memset(priv
->tx_ring
[q
], 0, tx_ring_size
);
320 /* Build TX ring buffer */
321 for (i
= 0, tx_desc
= priv
->tx_ring
[q
]; i
< priv
->num_tx_ring
[q
];
323 tx_desc
->die_dt
= DT_EEMPTY
;
325 tx_desc
->die_dt
= DT_EEMPTY
;
327 tx_desc
->dptr
= cpu_to_le32((u32
)priv
->tx_desc_dma
[q
]);
328 tx_desc
->die_dt
= DT_LINKFIX
; /* type */
330 /* RX descriptor base address for best effort */
331 desc
= &priv
->desc_bat
[RX_QUEUE_OFFSET
+ q
];
332 desc
->die_dt
= DT_LINKFIX
; /* type */
333 desc
->dptr
= cpu_to_le32((u32
)priv
->rx_desc_dma
[q
]);
335 /* TX descriptor base address for best effort */
336 desc
= &priv
->desc_bat
[q
];
337 desc
->die_dt
= DT_LINKFIX
; /* type */
338 desc
->dptr
= cpu_to_le32((u32
)priv
->tx_desc_dma
[q
]);
341 /* Init skb and descriptor buffer for Ethernet AVB */
342 static int ravb_ring_init(struct net_device
*ndev
, int q
)
344 struct ravb_private
*priv
= netdev_priv(ndev
);
349 /* Allocate RX and TX skb rings */
350 priv
->rx_skb
[q
] = kcalloc(priv
->num_rx_ring
[q
],
351 sizeof(*priv
->rx_skb
[q
]), GFP_KERNEL
);
352 priv
->tx_skb
[q
] = kcalloc(priv
->num_tx_ring
[q
],
353 sizeof(*priv
->tx_skb
[q
]), GFP_KERNEL
);
354 if (!priv
->rx_skb
[q
] || !priv
->tx_skb
[q
])
357 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++) {
358 skb
= netdev_alloc_skb(ndev
, PKT_BUF_SZ
+ RAVB_ALIGN
- 1);
361 ravb_set_buffer_align(skb
);
362 priv
->rx_skb
[q
][i
] = skb
;
365 /* Allocate rings for the aligned buffers */
366 priv
->tx_align
[q
] = kmalloc(DPTR_ALIGN
* priv
->num_tx_ring
[q
] +
367 DPTR_ALIGN
- 1, GFP_KERNEL
);
368 if (!priv
->tx_align
[q
])
371 /* Allocate all RX descriptors. */
372 ring_size
= sizeof(struct ravb_ex_rx_desc
) * (priv
->num_rx_ring
[q
] + 1);
373 priv
->rx_ring
[q
] = dma_alloc_coherent(ndev
->dev
.parent
, ring_size
,
374 &priv
->rx_desc_dma
[q
],
376 if (!priv
->rx_ring
[q
])
379 priv
->dirty_rx
[q
] = 0;
381 /* Allocate all TX descriptors. */
382 ring_size
= sizeof(struct ravb_tx_desc
) *
383 (priv
->num_tx_ring
[q
] * NUM_TX_DESC
+ 1);
384 priv
->tx_ring
[q
] = dma_alloc_coherent(ndev
->dev
.parent
, ring_size
,
385 &priv
->tx_desc_dma
[q
],
387 if (!priv
->tx_ring
[q
])
393 ravb_ring_free(ndev
, q
);
398 /* E-MAC init function */
399 static void ravb_emac_init(struct net_device
*ndev
)
401 struct ravb_private
*priv
= netdev_priv(ndev
);
403 /* Receive frame limit set register */
404 ravb_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
, RFLR
);
406 /* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */
407 ravb_write(ndev
, ECMR_ZPF
| (priv
->duplex
? ECMR_DM
: 0) |
408 (ndev
->features
& NETIF_F_RXCSUM
? ECMR_RCSC
: 0) |
409 ECMR_TE
| ECMR_RE
, ECMR
);
413 /* Set MAC address */
415 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
416 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
418 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
420 /* E-MAC status register clear */
421 ravb_write(ndev
, ECSR_ICD
| ECSR_MPD
, ECSR
);
423 /* E-MAC interrupt enable register */
424 ravb_write(ndev
, ECSIPR_ICDIP
| ECSIPR_MPDIP
| ECSIPR_LCHNGIP
, ECSIPR
);
427 /* Device init function for Ethernet AVB */
428 static int ravb_dmac_init(struct net_device
*ndev
)
430 struct ravb_private
*priv
= netdev_priv(ndev
);
433 /* Set CONFIG mode */
434 error
= ravb_config(ndev
);
438 error
= ravb_ring_init(ndev
, RAVB_BE
);
441 error
= ravb_ring_init(ndev
, RAVB_NC
);
443 ravb_ring_free(ndev
, RAVB_BE
);
447 /* Descriptor format */
448 ravb_ring_format(ndev
, RAVB_BE
);
449 ravb_ring_format(ndev
, RAVB_NC
);
451 #if defined(__LITTLE_ENDIAN)
452 ravb_modify(ndev
, CCC
, CCC_BOC
, 0);
454 ravb_modify(ndev
, CCC
, CCC_BOC
, CCC_BOC
);
459 RCR_EFFS
| RCR_ENCF
| RCR_ETS0
| RCR_ESF
| 0x18000000, RCR
);
462 ravb_write(ndev
, TGC_TQP_AVBMODE1
| 0x00222200, TGC
);
464 /* Timestamp enable */
465 ravb_write(ndev
, TCCR_TFEN
, TCCR
);
467 /* Interrupt init: */
468 if (priv
->chip_id
== RCAR_GEN3
) {
470 ravb_write(ndev
, 0, DIL
);
471 /* Set queue specific interrupt */
472 ravb_write(ndev
, CIE_CRIE
| CIE_CTIE
| CIE_CL0M
, CIE
);
475 ravb_write(ndev
, RIC0_FRE0
| RIC0_FRE1
, RIC0
);
476 /* Disable FIFO full warning */
477 ravb_write(ndev
, 0, RIC1
);
478 /* Receive FIFO full error, descriptor empty */
479 ravb_write(ndev
, RIC2_QFE0
| RIC2_QFE1
| RIC2_RFFE
, RIC2
);
480 /* Frame transmitted, timestamp FIFO updated */
481 ravb_write(ndev
, TIC_FTE0
| TIC_FTE1
| TIC_TFUE
, TIC
);
483 /* Setting the control will start the AVB-DMAC process. */
484 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_OPERATION
);
489 static void ravb_get_tx_tstamp(struct net_device
*ndev
)
491 struct ravb_private
*priv
= netdev_priv(ndev
);
492 struct ravb_tstamp_skb
*ts_skb
, *ts_skb2
;
493 struct skb_shared_hwtstamps shhwtstamps
;
495 struct timespec64 ts
;
500 count
= (ravb_read(ndev
, TSR
) & TSR_TFFL
) >> 8;
502 tfa2
= ravb_read(ndev
, TFA2
);
503 tfa_tag
= (tfa2
& TFA2_TST
) >> 16;
504 ts
.tv_nsec
= (u64
)ravb_read(ndev
, TFA0
);
505 ts
.tv_sec
= ((u64
)(tfa2
& TFA2_TSV
) << 32) |
506 ravb_read(ndev
, TFA1
);
507 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
508 shhwtstamps
.hwtstamp
= timespec64_to_ktime(ts
);
509 list_for_each_entry_safe(ts_skb
, ts_skb2
, &priv
->ts_skb_list
,
513 list_del(&ts_skb
->list
);
515 if (tag
== tfa_tag
) {
516 skb_tstamp_tx(skb
, &shhwtstamps
);
520 ravb_modify(ndev
, TCCR
, TCCR_TFR
, TCCR_TFR
);
524 static void ravb_rx_csum(struct sk_buff
*skb
)
528 /* The hardware checksum is 2 bytes appended to packet data */
529 if (unlikely(skb
->len
< 2))
531 hw_csum
= skb_tail_pointer(skb
) - 2;
532 skb
->csum
= csum_unfold((__force __sum16
)get_unaligned_le16(hw_csum
));
533 skb
->ip_summed
= CHECKSUM_COMPLETE
;
534 skb_trim(skb
, skb
->len
- 2);
537 /* Packet receive function for Ethernet AVB */
538 static bool ravb_rx(struct net_device
*ndev
, int *quota
, int q
)
540 struct ravb_private
*priv
= netdev_priv(ndev
);
541 int entry
= priv
->cur_rx
[q
] % priv
->num_rx_ring
[q
];
542 int boguscnt
= (priv
->dirty_rx
[q
] + priv
->num_rx_ring
[q
]) -
544 struct net_device_stats
*stats
= &priv
->stats
[q
];
545 struct ravb_ex_rx_desc
*desc
;
548 struct timespec64 ts
;
553 boguscnt
= min(boguscnt
, *quota
);
555 desc
= &priv
->rx_ring
[q
][entry
];
556 while (desc
->die_dt
!= DT_FEMPTY
) {
557 /* Descriptor type must be checked before all other reads */
559 desc_status
= desc
->msc
;
560 pkt_len
= le16_to_cpu(desc
->ds_cc
) & RX_DS
;
565 /* We use 0-byte descriptors to mark the DMA mapping errors */
569 if (desc_status
& MSC_MC
)
572 if (desc_status
& (MSC_CRC
| MSC_RFE
| MSC_RTSF
| MSC_RTLF
|
575 if (desc_status
& MSC_CRC
)
576 stats
->rx_crc_errors
++;
577 if (desc_status
& MSC_RFE
)
578 stats
->rx_frame_errors
++;
579 if (desc_status
& (MSC_RTLF
| MSC_RTSF
))
580 stats
->rx_length_errors
++;
581 if (desc_status
& MSC_CEEF
)
582 stats
->rx_missed_errors
++;
584 u32 get_ts
= priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE
;
586 skb
= priv
->rx_skb
[q
][entry
];
587 priv
->rx_skb
[q
][entry
] = NULL
;
588 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
591 get_ts
&= (q
== RAVB_NC
) ?
592 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
:
593 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
;
595 struct skb_shared_hwtstamps
*shhwtstamps
;
597 shhwtstamps
= skb_hwtstamps(skb
);
598 memset(shhwtstamps
, 0, sizeof(*shhwtstamps
));
599 ts
.tv_sec
= ((u64
) le16_to_cpu(desc
->ts_sh
) <<
600 32) | le32_to_cpu(desc
->ts_sl
);
601 ts
.tv_nsec
= le32_to_cpu(desc
->ts_n
);
602 shhwtstamps
->hwtstamp
= timespec64_to_ktime(ts
);
605 skb_put(skb
, pkt_len
);
606 skb
->protocol
= eth_type_trans(skb
, ndev
);
607 if (ndev
->features
& NETIF_F_RXCSUM
)
609 napi_gro_receive(&priv
->napi
[q
], skb
);
611 stats
->rx_bytes
+= pkt_len
;
614 entry
= (++priv
->cur_rx
[q
]) % priv
->num_rx_ring
[q
];
615 desc
= &priv
->rx_ring
[q
][entry
];
618 /* Refill the RX ring buffers. */
619 for (; priv
->cur_rx
[q
] - priv
->dirty_rx
[q
] > 0; priv
->dirty_rx
[q
]++) {
620 entry
= priv
->dirty_rx
[q
] % priv
->num_rx_ring
[q
];
621 desc
= &priv
->rx_ring
[q
][entry
];
622 desc
->ds_cc
= cpu_to_le16(PKT_BUF_SZ
);
624 if (!priv
->rx_skb
[q
][entry
]) {
625 skb
= netdev_alloc_skb(ndev
,
626 PKT_BUF_SZ
+ RAVB_ALIGN
- 1);
628 break; /* Better luck next round. */
629 ravb_set_buffer_align(skb
);
630 dma_addr
= dma_map_single(ndev
->dev
.parent
, skb
->data
,
631 le16_to_cpu(desc
->ds_cc
),
633 skb_checksum_none_assert(skb
);
634 /* We just set the data size to 0 for a failed mapping
635 * which should prevent DMA from happening...
637 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
638 desc
->ds_cc
= cpu_to_le16(0);
639 desc
->dptr
= cpu_to_le32(dma_addr
);
640 priv
->rx_skb
[q
][entry
] = skb
;
642 /* Descriptor type must be set after all the above writes */
644 desc
->die_dt
= DT_FEMPTY
;
647 *quota
-= limit
- (++boguscnt
);
649 return boguscnt
<= 0;
652 static void ravb_rcv_snd_disable(struct net_device
*ndev
)
654 /* Disable TX and RX */
655 ravb_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, 0);
658 static void ravb_rcv_snd_enable(struct net_device
*ndev
)
660 /* Enable TX and RX */
661 ravb_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, ECMR_RE
| ECMR_TE
);
664 /* function for waiting dma process finished */
665 static int ravb_stop_dma(struct net_device
*ndev
)
669 /* Wait for stopping the hardware TX process */
670 error
= ravb_wait(ndev
, TCCR
,
671 TCCR_TSRQ0
| TCCR_TSRQ1
| TCCR_TSRQ2
| TCCR_TSRQ3
, 0);
675 error
= ravb_wait(ndev
, CSR
, CSR_TPO0
| CSR_TPO1
| CSR_TPO2
| CSR_TPO3
,
680 /* Stop the E-MAC's RX/TX processes. */
681 ravb_rcv_snd_disable(ndev
);
683 /* Wait for stopping the RX DMA process */
684 error
= ravb_wait(ndev
, CSR
, CSR_RPO
, 0);
688 /* Stop AVB-DMAC process */
689 return ravb_config(ndev
);
692 /* E-MAC interrupt handler */
693 static void ravb_emac_interrupt_unlocked(struct net_device
*ndev
)
695 struct ravb_private
*priv
= netdev_priv(ndev
);
698 ecsr
= ravb_read(ndev
, ECSR
);
699 ravb_write(ndev
, ecsr
, ECSR
); /* clear interrupt */
702 pm_wakeup_event(&priv
->pdev
->dev
, 0);
704 ndev
->stats
.tx_carrier_errors
++;
705 if (ecsr
& ECSR_LCHNG
) {
707 if (priv
->no_avb_link
)
709 psr
= ravb_read(ndev
, PSR
);
710 if (priv
->avb_link_active_low
)
712 if (!(psr
& PSR_LMON
)) {
713 /* DIsable RX and TX */
714 ravb_rcv_snd_disable(ndev
);
716 /* Enable RX and TX */
717 ravb_rcv_snd_enable(ndev
);
722 static irqreturn_t
ravb_emac_interrupt(int irq
, void *dev_id
)
724 struct net_device
*ndev
= dev_id
;
725 struct ravb_private
*priv
= netdev_priv(ndev
);
727 spin_lock(&priv
->lock
);
728 ravb_emac_interrupt_unlocked(ndev
);
730 spin_unlock(&priv
->lock
);
734 /* Error interrupt handler */
735 static void ravb_error_interrupt(struct net_device
*ndev
)
737 struct ravb_private
*priv
= netdev_priv(ndev
);
740 eis
= ravb_read(ndev
, EIS
);
741 ravb_write(ndev
, ~EIS_QFS
, EIS
);
743 ris2
= ravb_read(ndev
, RIS2
);
744 ravb_write(ndev
, ~(RIS2_QFF0
| RIS2_RFFF
), RIS2
);
746 /* Receive Descriptor Empty int */
747 if (ris2
& RIS2_QFF0
)
748 priv
->stats
[RAVB_BE
].rx_over_errors
++;
750 /* Receive Descriptor Empty int */
751 if (ris2
& RIS2_QFF1
)
752 priv
->stats
[RAVB_NC
].rx_over_errors
++;
754 /* Receive FIFO Overflow int */
755 if (ris2
& RIS2_RFFF
)
756 priv
->rx_fifo_errors
++;
760 static bool ravb_queue_interrupt(struct net_device
*ndev
, int q
)
762 struct ravb_private
*priv
= netdev_priv(ndev
);
763 u32 ris0
= ravb_read(ndev
, RIS0
);
764 u32 ric0
= ravb_read(ndev
, RIC0
);
765 u32 tis
= ravb_read(ndev
, TIS
);
766 u32 tic
= ravb_read(ndev
, TIC
);
768 if (((ris0
& ric0
) & BIT(q
)) || ((tis
& tic
) & BIT(q
))) {
769 if (napi_schedule_prep(&priv
->napi
[q
])) {
770 /* Mask RX and TX interrupts */
771 if (priv
->chip_id
== RCAR_GEN2
) {
772 ravb_write(ndev
, ric0
& ~BIT(q
), RIC0
);
773 ravb_write(ndev
, tic
& ~BIT(q
), TIC
);
775 ravb_write(ndev
, BIT(q
), RID0
);
776 ravb_write(ndev
, BIT(q
), TID
);
778 __napi_schedule(&priv
->napi
[q
]);
781 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
784 " tx status 0x%08x, tx mask 0x%08x.\n",
792 static bool ravb_timestamp_interrupt(struct net_device
*ndev
)
794 u32 tis
= ravb_read(ndev
, TIS
);
796 if (tis
& TIS_TFUF
) {
797 ravb_write(ndev
, ~TIS_TFUF
, TIS
);
798 ravb_get_tx_tstamp(ndev
);
804 static irqreturn_t
ravb_interrupt(int irq
, void *dev_id
)
806 struct net_device
*ndev
= dev_id
;
807 struct ravb_private
*priv
= netdev_priv(ndev
);
808 irqreturn_t result
= IRQ_NONE
;
811 spin_lock(&priv
->lock
);
812 /* Get interrupt status */
813 iss
= ravb_read(ndev
, ISS
);
815 /* Received and transmitted interrupts */
816 if (iss
& (ISS_FRS
| ISS_FTS
| ISS_TFUS
)) {
819 /* Timestamp updated */
820 if (ravb_timestamp_interrupt(ndev
))
821 result
= IRQ_HANDLED
;
823 /* Network control and best effort queue RX/TX */
824 for (q
= RAVB_NC
; q
>= RAVB_BE
; q
--) {
825 if (ravb_queue_interrupt(ndev
, q
))
826 result
= IRQ_HANDLED
;
830 /* E-MAC status summary */
832 ravb_emac_interrupt_unlocked(ndev
);
833 result
= IRQ_HANDLED
;
836 /* Error status summary */
838 ravb_error_interrupt(ndev
);
839 result
= IRQ_HANDLED
;
842 /* gPTP interrupt status summary */
843 if (iss
& ISS_CGIS
) {
844 ravb_ptp_interrupt(ndev
);
845 result
= IRQ_HANDLED
;
849 spin_unlock(&priv
->lock
);
853 /* Timestamp/Error/gPTP interrupt handler */
854 static irqreturn_t
ravb_multi_interrupt(int irq
, void *dev_id
)
856 struct net_device
*ndev
= dev_id
;
857 struct ravb_private
*priv
= netdev_priv(ndev
);
858 irqreturn_t result
= IRQ_NONE
;
861 spin_lock(&priv
->lock
);
862 /* Get interrupt status */
863 iss
= ravb_read(ndev
, ISS
);
865 /* Timestamp updated */
866 if ((iss
& ISS_TFUS
) && ravb_timestamp_interrupt(ndev
))
867 result
= IRQ_HANDLED
;
869 /* Error status summary */
871 ravb_error_interrupt(ndev
);
872 result
= IRQ_HANDLED
;
875 /* gPTP interrupt status summary */
876 if (iss
& ISS_CGIS
) {
877 ravb_ptp_interrupt(ndev
);
878 result
= IRQ_HANDLED
;
882 spin_unlock(&priv
->lock
);
886 static irqreturn_t
ravb_dma_interrupt(int irq
, void *dev_id
, int q
)
888 struct net_device
*ndev
= dev_id
;
889 struct ravb_private
*priv
= netdev_priv(ndev
);
890 irqreturn_t result
= IRQ_NONE
;
892 spin_lock(&priv
->lock
);
894 /* Network control/Best effort queue RX/TX */
895 if (ravb_queue_interrupt(ndev
, q
))
896 result
= IRQ_HANDLED
;
899 spin_unlock(&priv
->lock
);
903 static irqreturn_t
ravb_be_interrupt(int irq
, void *dev_id
)
905 return ravb_dma_interrupt(irq
, dev_id
, RAVB_BE
);
908 static irqreturn_t
ravb_nc_interrupt(int irq
, void *dev_id
)
910 return ravb_dma_interrupt(irq
, dev_id
, RAVB_NC
);
913 static int ravb_poll(struct napi_struct
*napi
, int budget
)
915 struct net_device
*ndev
= napi
->dev
;
916 struct ravb_private
*priv
= netdev_priv(ndev
);
918 int q
= napi
- priv
->napi
;
924 tis
= ravb_read(ndev
, TIS
);
925 ris0
= ravb_read(ndev
, RIS0
);
926 if (!((ris0
& mask
) || (tis
& mask
)))
929 /* Processing RX Descriptor Ring */
931 /* Clear RX interrupt */
932 ravb_write(ndev
, ~mask
, RIS0
);
933 if (ravb_rx(ndev
, "a
, q
))
936 /* Processing TX Descriptor Ring */
938 spin_lock_irqsave(&priv
->lock
, flags
);
939 /* Clear TX interrupt */
940 ravb_write(ndev
, ~mask
, TIS
);
941 ravb_tx_free(ndev
, q
, true);
942 netif_wake_subqueue(ndev
, q
);
944 spin_unlock_irqrestore(&priv
->lock
, flags
);
950 /* Re-enable RX/TX interrupts */
951 spin_lock_irqsave(&priv
->lock
, flags
);
952 if (priv
->chip_id
== RCAR_GEN2
) {
953 ravb_modify(ndev
, RIC0
, mask
, mask
);
954 ravb_modify(ndev
, TIC
, mask
, mask
);
956 ravb_write(ndev
, mask
, RIE0
);
957 ravb_write(ndev
, mask
, TIE
);
960 spin_unlock_irqrestore(&priv
->lock
, flags
);
962 /* Receive error message handling */
963 priv
->rx_over_errors
= priv
->stats
[RAVB_BE
].rx_over_errors
;
964 priv
->rx_over_errors
+= priv
->stats
[RAVB_NC
].rx_over_errors
;
965 if (priv
->rx_over_errors
!= ndev
->stats
.rx_over_errors
)
966 ndev
->stats
.rx_over_errors
= priv
->rx_over_errors
;
967 if (priv
->rx_fifo_errors
!= ndev
->stats
.rx_fifo_errors
)
968 ndev
->stats
.rx_fifo_errors
= priv
->rx_fifo_errors
;
970 return budget
- quota
;
973 /* PHY state control function */
974 static void ravb_adjust_link(struct net_device
*ndev
)
976 struct ravb_private
*priv
= netdev_priv(ndev
);
977 struct phy_device
*phydev
= ndev
->phydev
;
978 bool new_state
= false;
981 if (phydev
->duplex
!= priv
->duplex
) {
983 priv
->duplex
= phydev
->duplex
;
984 ravb_set_duplex(ndev
);
987 if (phydev
->speed
!= priv
->speed
) {
989 priv
->speed
= phydev
->speed
;
993 ravb_modify(ndev
, ECMR
, ECMR_TXF
, 0);
995 priv
->link
= phydev
->link
;
996 if (priv
->no_avb_link
)
997 ravb_rcv_snd_enable(ndev
);
999 } else if (priv
->link
) {
1004 if (priv
->no_avb_link
)
1005 ravb_rcv_snd_disable(ndev
);
1008 if (new_state
&& netif_msg_link(priv
))
1009 phy_print_status(phydev
);
1012 static const struct soc_device_attribute r8a7795es10
[] = {
1013 { .soc_id
= "r8a7795", .revision
= "ES1.0", },
1017 /* PHY init function */
1018 static int ravb_phy_init(struct net_device
*ndev
)
1020 struct device_node
*np
= ndev
->dev
.parent
->of_node
;
1021 struct ravb_private
*priv
= netdev_priv(ndev
);
1022 struct phy_device
*phydev
;
1023 struct device_node
*pn
;
1030 /* Try connecting to PHY */
1031 pn
= of_parse_phandle(np
, "phy-handle", 0);
1033 /* In the case of a fixed PHY, the DT node associated
1034 * to the PHY is the Ethernet MAC DT node.
1036 if (of_phy_is_fixed_link(np
)) {
1037 err
= of_phy_register_fixed_link(np
);
1041 pn
= of_node_get(np
);
1043 phydev
= of_phy_connect(ndev
, pn
, ravb_adjust_link
, 0,
1044 priv
->phy_interface
);
1047 netdev_err(ndev
, "failed to connect PHY\n");
1049 goto err_deregister_fixed_link
;
1052 /* This driver only support 10/100Mbit speeds on R-Car H3 ES1.0
1055 if (soc_device_match(r8a7795es10
)) {
1056 err
= phy_set_max_speed(phydev
, SPEED_100
);
1058 netdev_err(ndev
, "failed to limit PHY to 100Mbit/s\n");
1059 goto err_phy_disconnect
;
1062 netdev_info(ndev
, "limited PHY to 100Mbit/s\n");
1065 /* 10BASE is not supported */
1066 phydev
->supported
&= ~PHY_10BT_FEATURES
;
1068 phy_attached_info(phydev
);
1073 phy_disconnect(phydev
);
1074 err_deregister_fixed_link
:
1075 if (of_phy_is_fixed_link(np
))
1076 of_phy_deregister_fixed_link(np
);
1081 /* PHY control start function */
1082 static int ravb_phy_start(struct net_device
*ndev
)
1086 error
= ravb_phy_init(ndev
);
1090 phy_start(ndev
->phydev
);
1095 static int ravb_get_link_ksettings(struct net_device
*ndev
,
1096 struct ethtool_link_ksettings
*cmd
)
1098 struct ravb_private
*priv
= netdev_priv(ndev
);
1099 unsigned long flags
;
1104 spin_lock_irqsave(&priv
->lock
, flags
);
1105 phy_ethtool_ksettings_get(ndev
->phydev
, cmd
);
1106 spin_unlock_irqrestore(&priv
->lock
, flags
);
1111 static int ravb_set_link_ksettings(struct net_device
*ndev
,
1112 const struct ethtool_link_ksettings
*cmd
)
1114 struct ravb_private
*priv
= netdev_priv(ndev
);
1115 unsigned long flags
;
1121 spin_lock_irqsave(&priv
->lock
, flags
);
1123 /* Disable TX and RX */
1124 ravb_rcv_snd_disable(ndev
);
1126 error
= phy_ethtool_ksettings_set(ndev
->phydev
, cmd
);
1130 if (cmd
->base
.duplex
== DUPLEX_FULL
)
1135 ravb_set_duplex(ndev
);
1140 /* Enable TX and RX */
1141 ravb_rcv_snd_enable(ndev
);
1144 spin_unlock_irqrestore(&priv
->lock
, flags
);
1149 static int ravb_nway_reset(struct net_device
*ndev
)
1151 struct ravb_private
*priv
= netdev_priv(ndev
);
1152 int error
= -ENODEV
;
1153 unsigned long flags
;
1156 spin_lock_irqsave(&priv
->lock
, flags
);
1157 error
= phy_start_aneg(ndev
->phydev
);
1158 spin_unlock_irqrestore(&priv
->lock
, flags
);
1164 static u32
ravb_get_msglevel(struct net_device
*ndev
)
1166 struct ravb_private
*priv
= netdev_priv(ndev
);
1168 return priv
->msg_enable
;
1171 static void ravb_set_msglevel(struct net_device
*ndev
, u32 value
)
1173 struct ravb_private
*priv
= netdev_priv(ndev
);
1175 priv
->msg_enable
= value
;
1178 static const char ravb_gstrings_stats
[][ETH_GSTRING_LEN
] = {
1179 "rx_queue_0_current",
1180 "tx_queue_0_current",
1183 "rx_queue_0_packets",
1184 "tx_queue_0_packets",
1187 "rx_queue_0_mcast_packets",
1188 "rx_queue_0_errors",
1189 "rx_queue_0_crc_errors",
1190 "rx_queue_0_frame_errors",
1191 "rx_queue_0_length_errors",
1192 "rx_queue_0_missed_errors",
1193 "rx_queue_0_over_errors",
1195 "rx_queue_1_current",
1196 "tx_queue_1_current",
1199 "rx_queue_1_packets",
1200 "tx_queue_1_packets",
1203 "rx_queue_1_mcast_packets",
1204 "rx_queue_1_errors",
1205 "rx_queue_1_crc_errors",
1206 "rx_queue_1_frame_errors",
1207 "rx_queue_1_length_errors",
1208 "rx_queue_1_missed_errors",
1209 "rx_queue_1_over_errors",
1212 #define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1214 static int ravb_get_sset_count(struct net_device
*netdev
, int sset
)
1218 return RAVB_STATS_LEN
;
1224 static void ravb_get_ethtool_stats(struct net_device
*ndev
,
1225 struct ethtool_stats
*stats
, u64
*data
)
1227 struct ravb_private
*priv
= netdev_priv(ndev
);
1231 /* Device-specific stats */
1232 for (q
= RAVB_BE
; q
< NUM_RX_QUEUE
; q
++) {
1233 struct net_device_stats
*stats
= &priv
->stats
[q
];
1235 data
[i
++] = priv
->cur_rx
[q
];
1236 data
[i
++] = priv
->cur_tx
[q
];
1237 data
[i
++] = priv
->dirty_rx
[q
];
1238 data
[i
++] = priv
->dirty_tx
[q
];
1239 data
[i
++] = stats
->rx_packets
;
1240 data
[i
++] = stats
->tx_packets
;
1241 data
[i
++] = stats
->rx_bytes
;
1242 data
[i
++] = stats
->tx_bytes
;
1243 data
[i
++] = stats
->multicast
;
1244 data
[i
++] = stats
->rx_errors
;
1245 data
[i
++] = stats
->rx_crc_errors
;
1246 data
[i
++] = stats
->rx_frame_errors
;
1247 data
[i
++] = stats
->rx_length_errors
;
1248 data
[i
++] = stats
->rx_missed_errors
;
1249 data
[i
++] = stats
->rx_over_errors
;
1253 static void ravb_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
1255 switch (stringset
) {
1257 memcpy(data
, *ravb_gstrings_stats
, sizeof(ravb_gstrings_stats
));
1262 static void ravb_get_ringparam(struct net_device
*ndev
,
1263 struct ethtool_ringparam
*ring
)
1265 struct ravb_private
*priv
= netdev_priv(ndev
);
1267 ring
->rx_max_pending
= BE_RX_RING_MAX
;
1268 ring
->tx_max_pending
= BE_TX_RING_MAX
;
1269 ring
->rx_pending
= priv
->num_rx_ring
[RAVB_BE
];
1270 ring
->tx_pending
= priv
->num_tx_ring
[RAVB_BE
];
1273 static int ravb_set_ringparam(struct net_device
*ndev
,
1274 struct ethtool_ringparam
*ring
)
1276 struct ravb_private
*priv
= netdev_priv(ndev
);
1279 if (ring
->tx_pending
> BE_TX_RING_MAX
||
1280 ring
->rx_pending
> BE_RX_RING_MAX
||
1281 ring
->tx_pending
< BE_TX_RING_MIN
||
1282 ring
->rx_pending
< BE_RX_RING_MIN
)
1284 if (ring
->rx_mini_pending
|| ring
->rx_jumbo_pending
)
1287 if (netif_running(ndev
)) {
1288 netif_device_detach(ndev
);
1289 /* Stop PTP Clock driver */
1290 if (priv
->chip_id
== RCAR_GEN2
)
1291 ravb_ptp_stop(ndev
);
1292 /* Wait for DMA stopping */
1293 error
= ravb_stop_dma(ndev
);
1296 "cannot set ringparam! Any AVB processes are still running?\n");
1299 synchronize_irq(ndev
->irq
);
1301 /* Free all the skb's in the RX queue and the DMA buffers. */
1302 ravb_ring_free(ndev
, RAVB_BE
);
1303 ravb_ring_free(ndev
, RAVB_NC
);
1306 /* Set new parameters */
1307 priv
->num_rx_ring
[RAVB_BE
] = ring
->rx_pending
;
1308 priv
->num_tx_ring
[RAVB_BE
] = ring
->tx_pending
;
1310 if (netif_running(ndev
)) {
1311 error
= ravb_dmac_init(ndev
);
1314 "%s: ravb_dmac_init() failed, error %d\n",
1319 ravb_emac_init(ndev
);
1321 /* Initialise PTP Clock driver */
1322 if (priv
->chip_id
== RCAR_GEN2
)
1323 ravb_ptp_init(ndev
, priv
->pdev
);
1325 netif_device_attach(ndev
);
1331 static int ravb_get_ts_info(struct net_device
*ndev
,
1332 struct ethtool_ts_info
*info
)
1334 struct ravb_private
*priv
= netdev_priv(ndev
);
1336 info
->so_timestamping
=
1337 SOF_TIMESTAMPING_TX_SOFTWARE
|
1338 SOF_TIMESTAMPING_RX_SOFTWARE
|
1339 SOF_TIMESTAMPING_SOFTWARE
|
1340 SOF_TIMESTAMPING_TX_HARDWARE
|
1341 SOF_TIMESTAMPING_RX_HARDWARE
|
1342 SOF_TIMESTAMPING_RAW_HARDWARE
;
1343 info
->tx_types
= (1 << HWTSTAMP_TX_OFF
) | (1 << HWTSTAMP_TX_ON
);
1345 (1 << HWTSTAMP_FILTER_NONE
) |
1346 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT
) |
1347 (1 << HWTSTAMP_FILTER_ALL
);
1348 info
->phc_index
= ptp_clock_index(priv
->ptp
.clock
);
1353 static void ravb_get_wol(struct net_device
*ndev
, struct ethtool_wolinfo
*wol
)
1355 struct ravb_private
*priv
= netdev_priv(ndev
);
1357 wol
->supported
= WAKE_MAGIC
;
1358 wol
->wolopts
= priv
->wol_enabled
? WAKE_MAGIC
: 0;
1361 static int ravb_set_wol(struct net_device
*ndev
, struct ethtool_wolinfo
*wol
)
1363 struct ravb_private
*priv
= netdev_priv(ndev
);
1365 if (wol
->wolopts
& ~WAKE_MAGIC
)
1368 priv
->wol_enabled
= !!(wol
->wolopts
& WAKE_MAGIC
);
1370 device_set_wakeup_enable(&priv
->pdev
->dev
, priv
->wol_enabled
);
1375 static const struct ethtool_ops ravb_ethtool_ops
= {
1376 .nway_reset
= ravb_nway_reset
,
1377 .get_msglevel
= ravb_get_msglevel
,
1378 .set_msglevel
= ravb_set_msglevel
,
1379 .get_link
= ethtool_op_get_link
,
1380 .get_strings
= ravb_get_strings
,
1381 .get_ethtool_stats
= ravb_get_ethtool_stats
,
1382 .get_sset_count
= ravb_get_sset_count
,
1383 .get_ringparam
= ravb_get_ringparam
,
1384 .set_ringparam
= ravb_set_ringparam
,
1385 .get_ts_info
= ravb_get_ts_info
,
1386 .get_link_ksettings
= ravb_get_link_ksettings
,
1387 .set_link_ksettings
= ravb_set_link_ksettings
,
1388 .get_wol
= ravb_get_wol
,
1389 .set_wol
= ravb_set_wol
,
1392 static inline int ravb_hook_irq(unsigned int irq
, irq_handler_t handler
,
1393 struct net_device
*ndev
, struct device
*dev
,
1399 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s:%s", ndev
->name
, ch
);
1402 error
= request_irq(irq
, handler
, 0, name
, ndev
);
1404 netdev_err(ndev
, "cannot request IRQ %s\n", name
);
1409 /* Network device open function for Ethernet AVB */
1410 static int ravb_open(struct net_device
*ndev
)
1412 struct ravb_private
*priv
= netdev_priv(ndev
);
1413 struct platform_device
*pdev
= priv
->pdev
;
1414 struct device
*dev
= &pdev
->dev
;
1417 napi_enable(&priv
->napi
[RAVB_BE
]);
1418 napi_enable(&priv
->napi
[RAVB_NC
]);
1420 if (priv
->chip_id
== RCAR_GEN2
) {
1421 error
= request_irq(ndev
->irq
, ravb_interrupt
, IRQF_SHARED
,
1424 netdev_err(ndev
, "cannot request IRQ\n");
1428 error
= ravb_hook_irq(ndev
->irq
, ravb_multi_interrupt
, ndev
,
1432 error
= ravb_hook_irq(priv
->emac_irq
, ravb_emac_interrupt
, ndev
,
1436 error
= ravb_hook_irq(priv
->rx_irqs
[RAVB_BE
], ravb_be_interrupt
,
1437 ndev
, dev
, "ch0:rx_be");
1439 goto out_free_irq_emac
;
1440 error
= ravb_hook_irq(priv
->tx_irqs
[RAVB_BE
], ravb_be_interrupt
,
1441 ndev
, dev
, "ch18:tx_be");
1443 goto out_free_irq_be_rx
;
1444 error
= ravb_hook_irq(priv
->rx_irqs
[RAVB_NC
], ravb_nc_interrupt
,
1445 ndev
, dev
, "ch1:rx_nc");
1447 goto out_free_irq_be_tx
;
1448 error
= ravb_hook_irq(priv
->tx_irqs
[RAVB_NC
], ravb_nc_interrupt
,
1449 ndev
, dev
, "ch19:tx_nc");
1451 goto out_free_irq_nc_rx
;
1455 error
= ravb_dmac_init(ndev
);
1457 goto out_free_irq_nc_tx
;
1458 ravb_emac_init(ndev
);
1460 /* Initialise PTP Clock driver */
1461 if (priv
->chip_id
== RCAR_GEN2
)
1462 ravb_ptp_init(ndev
, priv
->pdev
);
1464 netif_tx_start_all_queues(ndev
);
1466 /* PHY control start */
1467 error
= ravb_phy_start(ndev
);
1474 /* Stop PTP Clock driver */
1475 if (priv
->chip_id
== RCAR_GEN2
)
1476 ravb_ptp_stop(ndev
);
1478 if (priv
->chip_id
== RCAR_GEN2
)
1480 free_irq(priv
->tx_irqs
[RAVB_NC
], ndev
);
1482 free_irq(priv
->rx_irqs
[RAVB_NC
], ndev
);
1484 free_irq(priv
->tx_irqs
[RAVB_BE
], ndev
);
1486 free_irq(priv
->rx_irqs
[RAVB_BE
], ndev
);
1488 free_irq(priv
->emac_irq
, ndev
);
1490 free_irq(ndev
->irq
, ndev
);
1492 napi_disable(&priv
->napi
[RAVB_NC
]);
1493 napi_disable(&priv
->napi
[RAVB_BE
]);
1497 /* Timeout function for Ethernet AVB */
1498 static void ravb_tx_timeout(struct net_device
*ndev
)
1500 struct ravb_private
*priv
= netdev_priv(ndev
);
1502 netif_err(priv
, tx_err
, ndev
,
1503 "transmit timed out, status %08x, resetting...\n",
1504 ravb_read(ndev
, ISS
));
1506 /* tx_errors count up */
1507 ndev
->stats
.tx_errors
++;
1509 schedule_work(&priv
->work
);
1512 static void ravb_tx_timeout_work(struct work_struct
*work
)
1514 struct ravb_private
*priv
= container_of(work
, struct ravb_private
,
1516 struct net_device
*ndev
= priv
->ndev
;
1518 netif_tx_stop_all_queues(ndev
);
1520 /* Stop PTP Clock driver */
1521 if (priv
->chip_id
== RCAR_GEN2
)
1522 ravb_ptp_stop(ndev
);
1524 /* Wait for DMA stopping */
1525 ravb_stop_dma(ndev
);
1527 ravb_ring_free(ndev
, RAVB_BE
);
1528 ravb_ring_free(ndev
, RAVB_NC
);
1531 ravb_dmac_init(ndev
);
1532 ravb_emac_init(ndev
);
1534 /* Initialise PTP Clock driver */
1535 if (priv
->chip_id
== RCAR_GEN2
)
1536 ravb_ptp_init(ndev
, priv
->pdev
);
1538 netif_tx_start_all_queues(ndev
);
1541 /* Packet transmit function for Ethernet AVB */
1542 static netdev_tx_t
ravb_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1544 struct ravb_private
*priv
= netdev_priv(ndev
);
1545 u16 q
= skb_get_queue_mapping(skb
);
1546 struct ravb_tstamp_skb
*ts_skb
;
1547 struct ravb_tx_desc
*desc
;
1548 unsigned long flags
;
1554 spin_lock_irqsave(&priv
->lock
, flags
);
1555 if (priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] > (priv
->num_tx_ring
[q
] - 1) *
1557 netif_err(priv
, tx_queued
, ndev
,
1558 "still transmitting with the full ring!\n");
1559 netif_stop_subqueue(ndev
, q
);
1560 spin_unlock_irqrestore(&priv
->lock
, flags
);
1561 return NETDEV_TX_BUSY
;
1564 if (skb_put_padto(skb
, ETH_ZLEN
))
1567 entry
= priv
->cur_tx
[q
] % (priv
->num_tx_ring
[q
] * NUM_TX_DESC
);
1568 priv
->tx_skb
[q
][entry
/ NUM_TX_DESC
] = skb
;
1570 buffer
= PTR_ALIGN(priv
->tx_align
[q
], DPTR_ALIGN
) +
1571 entry
/ NUM_TX_DESC
* DPTR_ALIGN
;
1572 len
= PTR_ALIGN(skb
->data
, DPTR_ALIGN
) - skb
->data
;
1573 /* Zero length DMA descriptors are problematic as they seem to
1574 * terminate DMA transfers. Avoid them by simply using a length of
1575 * DPTR_ALIGN (4) when skb data is aligned to DPTR_ALIGN.
1577 * As skb is guaranteed to have at least ETH_ZLEN (60) bytes of
1578 * data by the call to skb_put_padto() above this is safe with
1579 * respect to both the length of the first DMA descriptor (len)
1580 * overflowing the available data and the length of the second DMA
1581 * descriptor (skb->len - len) being negative.
1586 memcpy(buffer
, skb
->data
, len
);
1587 dma_addr
= dma_map_single(ndev
->dev
.parent
, buffer
, len
, DMA_TO_DEVICE
);
1588 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
1591 desc
= &priv
->tx_ring
[q
][entry
];
1592 desc
->ds_tagl
= cpu_to_le16(len
);
1593 desc
->dptr
= cpu_to_le32(dma_addr
);
1595 buffer
= skb
->data
+ len
;
1596 len
= skb
->len
- len
;
1597 dma_addr
= dma_map_single(ndev
->dev
.parent
, buffer
, len
, DMA_TO_DEVICE
);
1598 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
1602 desc
->ds_tagl
= cpu_to_le16(len
);
1603 desc
->dptr
= cpu_to_le32(dma_addr
);
1605 /* TX timestamp required */
1607 ts_skb
= kmalloc(sizeof(*ts_skb
), GFP_ATOMIC
);
1610 dma_unmap_single(ndev
->dev
.parent
, dma_addr
, len
,
1615 ts_skb
->tag
= priv
->ts_skb_tag
++;
1616 priv
->ts_skb_tag
&= 0x3ff;
1617 list_add_tail(&ts_skb
->list
, &priv
->ts_skb_list
);
1619 /* TAG and timestamp required flag */
1620 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
1621 desc
->tagh_tsr
= (ts_skb
->tag
>> 4) | TX_TSR
;
1622 desc
->ds_tagl
|= le16_to_cpu(ts_skb
->tag
<< 12);
1625 skb_tx_timestamp(skb
);
1626 /* Descriptor type must be set after all the above writes */
1628 desc
->die_dt
= DT_FEND
;
1630 desc
->die_dt
= DT_FSTART
;
1632 ravb_modify(ndev
, TCCR
, TCCR_TSRQ0
<< q
, TCCR_TSRQ0
<< q
);
1634 priv
->cur_tx
[q
] += NUM_TX_DESC
;
1635 if (priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] >
1636 (priv
->num_tx_ring
[q
] - 1) * NUM_TX_DESC
&&
1637 !ravb_tx_free(ndev
, q
, true))
1638 netif_stop_subqueue(ndev
, q
);
1642 spin_unlock_irqrestore(&priv
->lock
, flags
);
1643 return NETDEV_TX_OK
;
1646 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
1647 le16_to_cpu(desc
->ds_tagl
), DMA_TO_DEVICE
);
1649 dev_kfree_skb_any(skb
);
1650 priv
->tx_skb
[q
][entry
/ NUM_TX_DESC
] = NULL
;
1654 static u16
ravb_select_queue(struct net_device
*ndev
, struct sk_buff
*skb
,
1655 void *accel_priv
, select_queue_fallback_t fallback
)
1657 /* If skb needs TX timestamp, it is handled in network control queue */
1658 return (skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
) ? RAVB_NC
:
1663 static struct net_device_stats
*ravb_get_stats(struct net_device
*ndev
)
1665 struct ravb_private
*priv
= netdev_priv(ndev
);
1666 struct net_device_stats
*nstats
, *stats0
, *stats1
;
1668 nstats
= &ndev
->stats
;
1669 stats0
= &priv
->stats
[RAVB_BE
];
1670 stats1
= &priv
->stats
[RAVB_NC
];
1672 nstats
->tx_dropped
+= ravb_read(ndev
, TROCR
);
1673 ravb_write(ndev
, 0, TROCR
); /* (write clear) */
1674 nstats
->collisions
+= ravb_read(ndev
, CDCR
);
1675 ravb_write(ndev
, 0, CDCR
); /* (write clear) */
1676 nstats
->tx_carrier_errors
+= ravb_read(ndev
, LCCR
);
1677 ravb_write(ndev
, 0, LCCR
); /* (write clear) */
1679 nstats
->tx_carrier_errors
+= ravb_read(ndev
, CERCR
);
1680 ravb_write(ndev
, 0, CERCR
); /* (write clear) */
1681 nstats
->tx_carrier_errors
+= ravb_read(ndev
, CEECR
);
1682 ravb_write(ndev
, 0, CEECR
); /* (write clear) */
1684 nstats
->rx_packets
= stats0
->rx_packets
+ stats1
->rx_packets
;
1685 nstats
->tx_packets
= stats0
->tx_packets
+ stats1
->tx_packets
;
1686 nstats
->rx_bytes
= stats0
->rx_bytes
+ stats1
->rx_bytes
;
1687 nstats
->tx_bytes
= stats0
->tx_bytes
+ stats1
->tx_bytes
;
1688 nstats
->multicast
= stats0
->multicast
+ stats1
->multicast
;
1689 nstats
->rx_errors
= stats0
->rx_errors
+ stats1
->rx_errors
;
1690 nstats
->rx_crc_errors
= stats0
->rx_crc_errors
+ stats1
->rx_crc_errors
;
1691 nstats
->rx_frame_errors
=
1692 stats0
->rx_frame_errors
+ stats1
->rx_frame_errors
;
1693 nstats
->rx_length_errors
=
1694 stats0
->rx_length_errors
+ stats1
->rx_length_errors
;
1695 nstats
->rx_missed_errors
=
1696 stats0
->rx_missed_errors
+ stats1
->rx_missed_errors
;
1697 nstats
->rx_over_errors
=
1698 stats0
->rx_over_errors
+ stats1
->rx_over_errors
;
1703 /* Update promiscuous bit */
1704 static void ravb_set_rx_mode(struct net_device
*ndev
)
1706 struct ravb_private
*priv
= netdev_priv(ndev
);
1707 unsigned long flags
;
1709 spin_lock_irqsave(&priv
->lock
, flags
);
1710 ravb_modify(ndev
, ECMR
, ECMR_PRM
,
1711 ndev
->flags
& IFF_PROMISC
? ECMR_PRM
: 0);
1713 spin_unlock_irqrestore(&priv
->lock
, flags
);
1716 /* Device close function for Ethernet AVB */
1717 static int ravb_close(struct net_device
*ndev
)
1719 struct device_node
*np
= ndev
->dev
.parent
->of_node
;
1720 struct ravb_private
*priv
= netdev_priv(ndev
);
1721 struct ravb_tstamp_skb
*ts_skb
, *ts_skb2
;
1723 netif_tx_stop_all_queues(ndev
);
1725 /* Disable interrupts by clearing the interrupt masks. */
1726 ravb_write(ndev
, 0, RIC0
);
1727 ravb_write(ndev
, 0, RIC2
);
1728 ravb_write(ndev
, 0, TIC
);
1730 /* Stop PTP Clock driver */
1731 if (priv
->chip_id
== RCAR_GEN2
)
1732 ravb_ptp_stop(ndev
);
1734 /* Set the config mode to stop the AVB-DMAC's processes */
1735 if (ravb_stop_dma(ndev
) < 0)
1737 "device will be stopped after h/w processes are done.\n");
1739 /* Clear the timestamp list */
1740 list_for_each_entry_safe(ts_skb
, ts_skb2
, &priv
->ts_skb_list
, list
) {
1741 list_del(&ts_skb
->list
);
1745 /* PHY disconnect */
1747 phy_stop(ndev
->phydev
);
1748 phy_disconnect(ndev
->phydev
);
1749 if (of_phy_is_fixed_link(np
))
1750 of_phy_deregister_fixed_link(np
);
1753 if (priv
->chip_id
!= RCAR_GEN2
) {
1754 free_irq(priv
->tx_irqs
[RAVB_NC
], ndev
);
1755 free_irq(priv
->rx_irqs
[RAVB_NC
], ndev
);
1756 free_irq(priv
->tx_irqs
[RAVB_BE
], ndev
);
1757 free_irq(priv
->rx_irqs
[RAVB_BE
], ndev
);
1758 free_irq(priv
->emac_irq
, ndev
);
1760 free_irq(ndev
->irq
, ndev
);
1762 napi_disable(&priv
->napi
[RAVB_NC
]);
1763 napi_disable(&priv
->napi
[RAVB_BE
]);
1765 /* Free all the skb's in the RX queue and the DMA buffers. */
1766 ravb_ring_free(ndev
, RAVB_BE
);
1767 ravb_ring_free(ndev
, RAVB_NC
);
1772 static int ravb_hwtstamp_get(struct net_device
*ndev
, struct ifreq
*req
)
1774 struct ravb_private
*priv
= netdev_priv(ndev
);
1775 struct hwtstamp_config config
;
1778 config
.tx_type
= priv
->tstamp_tx_ctrl
? HWTSTAMP_TX_ON
:
1780 if (priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
)
1781 config
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_L2_EVENT
;
1782 else if (priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE_ALL
)
1783 config
.rx_filter
= HWTSTAMP_FILTER_ALL
;
1785 config
.rx_filter
= HWTSTAMP_FILTER_NONE
;
1787 return copy_to_user(req
->ifr_data
, &config
, sizeof(config
)) ?
1791 /* Control hardware time stamping */
1792 static int ravb_hwtstamp_set(struct net_device
*ndev
, struct ifreq
*req
)
1794 struct ravb_private
*priv
= netdev_priv(ndev
);
1795 struct hwtstamp_config config
;
1796 u32 tstamp_rx_ctrl
= RAVB_RXTSTAMP_ENABLED
;
1799 if (copy_from_user(&config
, req
->ifr_data
, sizeof(config
)))
1802 /* Reserved for future extensions */
1806 switch (config
.tx_type
) {
1807 case HWTSTAMP_TX_OFF
:
1810 case HWTSTAMP_TX_ON
:
1811 tstamp_tx_ctrl
= RAVB_TXTSTAMP_ENABLED
;
1817 switch (config
.rx_filter
) {
1818 case HWTSTAMP_FILTER_NONE
:
1821 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
1822 tstamp_rx_ctrl
|= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
;
1825 config
.rx_filter
= HWTSTAMP_FILTER_ALL
;
1826 tstamp_rx_ctrl
|= RAVB_RXTSTAMP_TYPE_ALL
;
1829 priv
->tstamp_tx_ctrl
= tstamp_tx_ctrl
;
1830 priv
->tstamp_rx_ctrl
= tstamp_rx_ctrl
;
1832 return copy_to_user(req
->ifr_data
, &config
, sizeof(config
)) ?
1836 /* ioctl to device function */
1837 static int ravb_do_ioctl(struct net_device
*ndev
, struct ifreq
*req
, int cmd
)
1839 struct phy_device
*phydev
= ndev
->phydev
;
1841 if (!netif_running(ndev
))
1849 return ravb_hwtstamp_get(ndev
, req
);
1851 return ravb_hwtstamp_set(ndev
, req
);
1854 return phy_mii_ioctl(phydev
, req
, cmd
);
1857 static void ravb_set_rx_csum(struct net_device
*ndev
, bool enable
)
1859 struct ravb_private
*priv
= netdev_priv(ndev
);
1860 unsigned long flags
;
1862 spin_lock_irqsave(&priv
->lock
, flags
);
1864 /* Disable TX and RX */
1865 ravb_rcv_snd_disable(ndev
);
1867 /* Modify RX Checksum setting */
1868 ravb_modify(ndev
, ECMR
, ECMR_RCSC
, enable
? ECMR_RCSC
: 0);
1870 /* Enable TX and RX */
1871 ravb_rcv_snd_enable(ndev
);
1873 spin_unlock_irqrestore(&priv
->lock
, flags
);
1876 static int ravb_set_features(struct net_device
*ndev
,
1877 netdev_features_t features
)
1879 netdev_features_t changed
= ndev
->features
^ features
;
1881 if (changed
& NETIF_F_RXCSUM
)
1882 ravb_set_rx_csum(ndev
, features
& NETIF_F_RXCSUM
);
1884 ndev
->features
= features
;
1889 static const struct net_device_ops ravb_netdev_ops
= {
1890 .ndo_open
= ravb_open
,
1891 .ndo_stop
= ravb_close
,
1892 .ndo_start_xmit
= ravb_start_xmit
,
1893 .ndo_select_queue
= ravb_select_queue
,
1894 .ndo_get_stats
= ravb_get_stats
,
1895 .ndo_set_rx_mode
= ravb_set_rx_mode
,
1896 .ndo_tx_timeout
= ravb_tx_timeout
,
1897 .ndo_do_ioctl
= ravb_do_ioctl
,
1898 .ndo_validate_addr
= eth_validate_addr
,
1899 .ndo_set_mac_address
= eth_mac_addr
,
1900 .ndo_set_features
= ravb_set_features
,
1903 /* MDIO bus init function */
1904 static int ravb_mdio_init(struct ravb_private
*priv
)
1906 struct platform_device
*pdev
= priv
->pdev
;
1907 struct device
*dev
= &pdev
->dev
;
1911 priv
->mdiobb
.ops
= &bb_ops
;
1913 /* MII controller setting */
1914 priv
->mii_bus
= alloc_mdio_bitbang(&priv
->mdiobb
);
1918 /* Hook up MII support for ethtool */
1919 priv
->mii_bus
->name
= "ravb_mii";
1920 priv
->mii_bus
->parent
= dev
;
1921 snprintf(priv
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
1922 pdev
->name
, pdev
->id
);
1924 /* Register MDIO bus */
1925 error
= of_mdiobus_register(priv
->mii_bus
, dev
->of_node
);
1932 free_mdio_bitbang(priv
->mii_bus
);
1936 /* MDIO bus release function */
1937 static int ravb_mdio_release(struct ravb_private
*priv
)
1939 /* Unregister mdio bus */
1940 mdiobus_unregister(priv
->mii_bus
);
1942 /* Free bitbang info */
1943 free_mdio_bitbang(priv
->mii_bus
);
1948 static const struct of_device_id ravb_match_table
[] = {
1949 { .compatible
= "renesas,etheravb-r8a7790", .data
= (void *)RCAR_GEN2
},
1950 { .compatible
= "renesas,etheravb-r8a7794", .data
= (void *)RCAR_GEN2
},
1951 { .compatible
= "renesas,etheravb-rcar-gen2", .data
= (void *)RCAR_GEN2
},
1952 { .compatible
= "renesas,etheravb-r8a7795", .data
= (void *)RCAR_GEN3
},
1953 { .compatible
= "renesas,etheravb-rcar-gen3", .data
= (void *)RCAR_GEN3
},
1956 MODULE_DEVICE_TABLE(of
, ravb_match_table
);
1958 static int ravb_set_gti(struct net_device
*ndev
)
1960 struct ravb_private
*priv
= netdev_priv(ndev
);
1961 struct device
*dev
= ndev
->dev
.parent
;
1965 rate
= clk_get_rate(priv
->clk
);
1969 inc
= 1000000000ULL << 20;
1972 if (inc
< GTI_TIV_MIN
|| inc
> GTI_TIV_MAX
) {
1973 dev_err(dev
, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1974 inc
, GTI_TIV_MIN
, GTI_TIV_MAX
);
1978 ravb_write(ndev
, inc
, GTI
);
1983 static void ravb_set_config_mode(struct net_device
*ndev
)
1985 struct ravb_private
*priv
= netdev_priv(ndev
);
1987 if (priv
->chip_id
== RCAR_GEN2
) {
1988 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_CONFIG
);
1989 /* Set CSEL value */
1990 ravb_modify(ndev
, CCC
, CCC_CSEL
, CCC_CSEL_HPB
);
1992 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_CONFIG
|
1993 CCC_GAC
| CCC_CSEL_HPB
);
1997 /* Set tx and rx clock internal delay modes */
1998 static void ravb_set_delay_mode(struct net_device
*ndev
)
2000 struct ravb_private
*priv
= netdev_priv(ndev
);
2003 if (priv
->phy_interface
== PHY_INTERFACE_MODE_RGMII_ID
||
2004 priv
->phy_interface
== PHY_INTERFACE_MODE_RGMII_RXID
)
2007 if (priv
->phy_interface
== PHY_INTERFACE_MODE_RGMII_ID
||
2008 priv
->phy_interface
== PHY_INTERFACE_MODE_RGMII_TXID
)
2011 ravb_modify(ndev
, APSR
, APSR_DM
, set
);
2014 static int ravb_probe(struct platform_device
*pdev
)
2016 struct device_node
*np
= pdev
->dev
.of_node
;
2017 struct ravb_private
*priv
;
2018 enum ravb_chip_id chip_id
;
2019 struct net_device
*ndev
;
2021 struct resource
*res
;
2026 "this driver is required to be instantiated from device tree\n");
2030 /* Get base address */
2031 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2033 dev_err(&pdev
->dev
, "invalid resource\n");
2037 ndev
= alloc_etherdev_mqs(sizeof(struct ravb_private
),
2038 NUM_TX_QUEUE
, NUM_RX_QUEUE
);
2042 ndev
->features
= NETIF_F_RXCSUM
;
2043 ndev
->hw_features
= NETIF_F_RXCSUM
;
2045 pm_runtime_enable(&pdev
->dev
);
2046 pm_runtime_get_sync(&pdev
->dev
);
2048 /* The Ether-specific entries in the device structure. */
2049 ndev
->base_addr
= res
->start
;
2051 chip_id
= (enum ravb_chip_id
)of_device_get_match_data(&pdev
->dev
);
2053 if (chip_id
== RCAR_GEN3
)
2054 irq
= platform_get_irq_byname(pdev
, "ch22");
2056 irq
= platform_get_irq(pdev
, 0);
2063 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
2065 priv
= netdev_priv(ndev
);
2068 priv
->num_tx_ring
[RAVB_BE
] = BE_TX_RING_SIZE
;
2069 priv
->num_rx_ring
[RAVB_BE
] = BE_RX_RING_SIZE
;
2070 priv
->num_tx_ring
[RAVB_NC
] = NC_TX_RING_SIZE
;
2071 priv
->num_rx_ring
[RAVB_NC
] = NC_RX_RING_SIZE
;
2072 priv
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
2073 if (IS_ERR(priv
->addr
)) {
2074 error
= PTR_ERR(priv
->addr
);
2078 spin_lock_init(&priv
->lock
);
2079 INIT_WORK(&priv
->work
, ravb_tx_timeout_work
);
2081 priv
->phy_interface
= of_get_phy_mode(np
);
2083 priv
->no_avb_link
= of_property_read_bool(np
, "renesas,no-ether-link");
2084 priv
->avb_link_active_low
=
2085 of_property_read_bool(np
, "renesas,ether-link-active-low");
2087 if (chip_id
== RCAR_GEN3
) {
2088 irq
= platform_get_irq_byname(pdev
, "ch24");
2093 priv
->emac_irq
= irq
;
2094 for (i
= 0; i
< NUM_RX_QUEUE
; i
++) {
2095 irq
= platform_get_irq_byname(pdev
, ravb_rx_irqs
[i
]);
2100 priv
->rx_irqs
[i
] = irq
;
2102 for (i
= 0; i
< NUM_TX_QUEUE
; i
++) {
2103 irq
= platform_get_irq_byname(pdev
, ravb_tx_irqs
[i
]);
2108 priv
->tx_irqs
[i
] = irq
;
2112 priv
->chip_id
= chip_id
;
2114 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
2115 if (IS_ERR(priv
->clk
)) {
2116 error
= PTR_ERR(priv
->clk
);
2121 ndev
->netdev_ops
= &ravb_netdev_ops
;
2122 ndev
->ethtool_ops
= &ravb_ethtool_ops
;
2124 /* Set AVB config mode */
2125 ravb_set_config_mode(ndev
);
2128 error
= ravb_set_gti(ndev
);
2132 /* Request GTI loading */
2133 ravb_modify(ndev
, GCCR
, GCCR_LTI
, GCCR_LTI
);
2135 if (priv
->chip_id
!= RCAR_GEN2
)
2136 ravb_set_delay_mode(ndev
);
2138 /* Allocate descriptor base address table */
2139 priv
->desc_bat_size
= sizeof(struct ravb_desc
) * DBAT_ENTRY_NUM
;
2140 priv
->desc_bat
= dma_alloc_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
,
2141 &priv
->desc_bat_dma
, GFP_KERNEL
);
2142 if (!priv
->desc_bat
) {
2144 "Cannot allocate desc base address table (size %d bytes)\n",
2145 priv
->desc_bat_size
);
2149 for (q
= RAVB_BE
; q
< DBAT_ENTRY_NUM
; q
++)
2150 priv
->desc_bat
[q
].die_dt
= DT_EOS
;
2151 ravb_write(ndev
, priv
->desc_bat_dma
, DBAT
);
2153 /* Initialise HW timestamp list */
2154 INIT_LIST_HEAD(&priv
->ts_skb_list
);
2156 /* Initialise PTP Clock driver */
2157 if (chip_id
!= RCAR_GEN2
)
2158 ravb_ptp_init(ndev
, pdev
);
2160 /* Debug message level */
2161 priv
->msg_enable
= RAVB_DEF_MSG_ENABLE
;
2163 /* Read and set MAC address */
2164 ravb_read_mac_address(ndev
, of_get_mac_address(np
));
2165 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
2166 dev_warn(&pdev
->dev
,
2167 "no valid MAC address supplied, using a random one\n");
2168 eth_hw_addr_random(ndev
);
2172 error
= ravb_mdio_init(priv
);
2174 dev_err(&pdev
->dev
, "failed to initialize MDIO\n");
2178 netif_napi_add(ndev
, &priv
->napi
[RAVB_BE
], ravb_poll
, 64);
2179 netif_napi_add(ndev
, &priv
->napi
[RAVB_NC
], ravb_poll
, 64);
2181 /* Network device register */
2182 error
= register_netdev(ndev
);
2186 device_set_wakeup_capable(&pdev
->dev
, 1);
2188 /* Print device information */
2189 netdev_info(ndev
, "Base address at %#x, %pM, IRQ %d.\n",
2190 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
2192 platform_set_drvdata(pdev
, ndev
);
2197 netif_napi_del(&priv
->napi
[RAVB_NC
]);
2198 netif_napi_del(&priv
->napi
[RAVB_BE
]);
2199 ravb_mdio_release(priv
);
2201 dma_free_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
, priv
->desc_bat
,
2202 priv
->desc_bat_dma
);
2204 /* Stop PTP Clock driver */
2205 if (chip_id
!= RCAR_GEN2
)
2206 ravb_ptp_stop(ndev
);
2210 pm_runtime_put(&pdev
->dev
);
2211 pm_runtime_disable(&pdev
->dev
);
2215 static int ravb_remove(struct platform_device
*pdev
)
2217 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2218 struct ravb_private
*priv
= netdev_priv(ndev
);
2220 /* Stop PTP Clock driver */
2221 if (priv
->chip_id
!= RCAR_GEN2
)
2222 ravb_ptp_stop(ndev
);
2224 dma_free_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
, priv
->desc_bat
,
2225 priv
->desc_bat_dma
);
2226 /* Set reset mode */
2227 ravb_write(ndev
, CCC_OPC_RESET
, CCC
);
2228 pm_runtime_put_sync(&pdev
->dev
);
2229 unregister_netdev(ndev
);
2230 netif_napi_del(&priv
->napi
[RAVB_NC
]);
2231 netif_napi_del(&priv
->napi
[RAVB_BE
]);
2232 ravb_mdio_release(priv
);
2233 pm_runtime_disable(&pdev
->dev
);
2235 platform_set_drvdata(pdev
, NULL
);
2240 static int ravb_wol_setup(struct net_device
*ndev
)
2242 struct ravb_private
*priv
= netdev_priv(ndev
);
2244 /* Disable interrupts by clearing the interrupt masks. */
2245 ravb_write(ndev
, 0, RIC0
);
2246 ravb_write(ndev
, 0, RIC2
);
2247 ravb_write(ndev
, 0, TIC
);
2249 /* Only allow ECI interrupts */
2250 synchronize_irq(priv
->emac_irq
);
2251 napi_disable(&priv
->napi
[RAVB_NC
]);
2252 napi_disable(&priv
->napi
[RAVB_BE
]);
2253 ravb_write(ndev
, ECSIPR_MPDIP
, ECSIPR
);
2255 /* Enable MagicPacket */
2256 ravb_modify(ndev
, ECMR
, ECMR_MPDE
, ECMR_MPDE
);
2258 return enable_irq_wake(priv
->emac_irq
);
2261 static int ravb_wol_restore(struct net_device
*ndev
)
2263 struct ravb_private
*priv
= netdev_priv(ndev
);
2266 napi_enable(&priv
->napi
[RAVB_NC
]);
2267 napi_enable(&priv
->napi
[RAVB_BE
]);
2269 /* Disable MagicPacket */
2270 ravb_modify(ndev
, ECMR
, ECMR_MPDE
, 0);
2272 ret
= ravb_close(ndev
);
2276 return disable_irq_wake(priv
->emac_irq
);
2279 static int __maybe_unused
ravb_suspend(struct device
*dev
)
2281 struct net_device
*ndev
= dev_get_drvdata(dev
);
2282 struct ravb_private
*priv
= netdev_priv(ndev
);
2285 if (!netif_running(ndev
))
2288 netif_device_detach(ndev
);
2290 if (priv
->wol_enabled
)
2291 ret
= ravb_wol_setup(ndev
);
2293 ret
= ravb_close(ndev
);
2298 static int __maybe_unused
ravb_resume(struct device
*dev
)
2300 struct net_device
*ndev
= dev_get_drvdata(dev
);
2301 struct ravb_private
*priv
= netdev_priv(ndev
);
2304 /* If WoL is enabled set reset mode to rearm the WoL logic */
2305 if (priv
->wol_enabled
)
2306 ravb_write(ndev
, CCC_OPC_RESET
, CCC
);
2308 /* All register have been reset to default values.
2309 * Restore all registers which where setup at probe time and
2310 * reopen device if it was running before system suspended.
2313 /* Set AVB config mode */
2314 ravb_set_config_mode(ndev
);
2317 ret
= ravb_set_gti(ndev
);
2321 /* Request GTI loading */
2322 ravb_modify(ndev
, GCCR
, GCCR_LTI
, GCCR_LTI
);
2324 if (priv
->chip_id
!= RCAR_GEN2
)
2325 ravb_set_delay_mode(ndev
);
2327 /* Restore descriptor base address table */
2328 ravb_write(ndev
, priv
->desc_bat_dma
, DBAT
);
2330 if (netif_running(ndev
)) {
2331 if (priv
->wol_enabled
) {
2332 ret
= ravb_wol_restore(ndev
);
2336 ret
= ravb_open(ndev
);
2339 netif_device_attach(ndev
);
2345 static int __maybe_unused
ravb_runtime_nop(struct device
*dev
)
2347 /* Runtime PM callback shared between ->runtime_suspend()
2348 * and ->runtime_resume(). Simply returns success.
2350 * This driver re-initializes all registers after
2351 * pm_runtime_get_sync() anyway so there is no need
2352 * to save and restore registers here.
2357 static const struct dev_pm_ops ravb_dev_pm_ops
= {
2358 SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend
, ravb_resume
)
2359 SET_RUNTIME_PM_OPS(ravb_runtime_nop
, ravb_runtime_nop
, NULL
)
2362 static struct platform_driver ravb_driver
= {
2363 .probe
= ravb_probe
,
2364 .remove
= ravb_remove
,
2367 .pm
= &ravb_dev_pm_ops
,
2368 .of_match_table
= ravb_match_table
,
2372 module_platform_driver(ravb_driver
);
2374 MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2375 MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2376 MODULE_LICENSE("GPL v2");