1 // SPDX-License-Identifier: GPL-2.0
2 /* Renesas Ethernet AVB device driver
4 * Copyright (C) 2014-2015 Renesas Electronics Corporation
5 * Copyright (C) 2015 Renesas Solutions Corp.
6 * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
8 * Based on the SuperH Ethernet driver
11 #include <linux/cache.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/err.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/if_vlan.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/net_tstamp.h>
24 #include <linux/of_device.h>
25 #include <linux/of_irq.h>
26 #include <linux/of_mdio.h>
27 #include <linux/of_net.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/slab.h>
30 #include <linux/spinlock.h>
31 #include <linux/sys_soc.h>
33 #include <asm/div64.h>
37 #define RAVB_DEF_MSG_ENABLE \
43 static const char *ravb_rx_irqs
[NUM_RX_QUEUE
] = {
48 static const char *ravb_tx_irqs
[NUM_TX_QUEUE
] = {
53 void ravb_modify(struct net_device
*ndev
, enum ravb_reg reg
, u32 clear
,
56 ravb_write(ndev
, (ravb_read(ndev
, reg
) & ~clear
) | set
, reg
);
59 int ravb_wait(struct net_device
*ndev
, enum ravb_reg reg
, u32 mask
, u32 value
)
63 for (i
= 0; i
< 10000; i
++) {
64 if ((ravb_read(ndev
, reg
) & mask
) == value
)
71 static int ravb_config(struct net_device
*ndev
)
76 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_CONFIG
);
77 /* Check if the operating mode is changed to the config mode */
78 error
= ravb_wait(ndev
, CSR
, CSR_OPS
, CSR_OPS_CONFIG
);
80 netdev_err(ndev
, "failed to switch device to config mode\n");
85 static void ravb_set_duplex(struct net_device
*ndev
)
87 struct ravb_private
*priv
= netdev_priv(ndev
);
89 ravb_modify(ndev
, ECMR
, ECMR_DM
, priv
->duplex
? ECMR_DM
: 0);
92 static void ravb_set_rate(struct net_device
*ndev
)
94 struct ravb_private
*priv
= netdev_priv(ndev
);
96 switch (priv
->speed
) {
97 case 100: /* 100BASE */
98 ravb_write(ndev
, GECMR_SPEED_100
, GECMR
);
100 case 1000: /* 1000BASE */
101 ravb_write(ndev
, GECMR_SPEED_1000
, GECMR
);
106 static void ravb_set_buffer_align(struct sk_buff
*skb
)
108 u32 reserve
= (unsigned long)skb
->data
& (RAVB_ALIGN
- 1);
111 skb_reserve(skb
, RAVB_ALIGN
- reserve
);
114 /* Get MAC address from the MAC address registers
116 * Ethernet AVB device doesn't have ROM for MAC address.
117 * This function gets the MAC address that was used by a bootloader.
119 static void ravb_read_mac_address(struct net_device
*ndev
, const u8
*mac
)
122 ether_addr_copy(ndev
->dev_addr
, mac
);
124 u32 mahr
= ravb_read(ndev
, MAHR
);
125 u32 malr
= ravb_read(ndev
, MALR
);
127 ndev
->dev_addr
[0] = (mahr
>> 24) & 0xFF;
128 ndev
->dev_addr
[1] = (mahr
>> 16) & 0xFF;
129 ndev
->dev_addr
[2] = (mahr
>> 8) & 0xFF;
130 ndev
->dev_addr
[3] = (mahr
>> 0) & 0xFF;
131 ndev
->dev_addr
[4] = (malr
>> 8) & 0xFF;
132 ndev
->dev_addr
[5] = (malr
>> 0) & 0xFF;
136 static void ravb_mdio_ctrl(struct mdiobb_ctrl
*ctrl
, u32 mask
, int set
)
138 struct ravb_private
*priv
= container_of(ctrl
, struct ravb_private
,
141 ravb_modify(priv
->ndev
, PIR
, mask
, set
? mask
: 0);
144 /* MDC pin control */
145 static void ravb_set_mdc(struct mdiobb_ctrl
*ctrl
, int level
)
147 ravb_mdio_ctrl(ctrl
, PIR_MDC
, level
);
150 /* Data I/O pin control */
151 static void ravb_set_mdio_dir(struct mdiobb_ctrl
*ctrl
, int output
)
153 ravb_mdio_ctrl(ctrl
, PIR_MMD
, output
);
157 static void ravb_set_mdio_data(struct mdiobb_ctrl
*ctrl
, int value
)
159 ravb_mdio_ctrl(ctrl
, PIR_MDO
, value
);
163 static int ravb_get_mdio_data(struct mdiobb_ctrl
*ctrl
)
165 struct ravb_private
*priv
= container_of(ctrl
, struct ravb_private
,
168 return (ravb_read(priv
->ndev
, PIR
) & PIR_MDI
) != 0;
171 /* MDIO bus control struct */
172 static struct mdiobb_ops bb_ops
= {
173 .owner
= THIS_MODULE
,
174 .set_mdc
= ravb_set_mdc
,
175 .set_mdio_dir
= ravb_set_mdio_dir
,
176 .set_mdio_data
= ravb_set_mdio_data
,
177 .get_mdio_data
= ravb_get_mdio_data
,
180 /* Free TX skb function for AVB-IP */
181 static int ravb_tx_free(struct net_device
*ndev
, int q
, bool free_txed_only
)
183 struct ravb_private
*priv
= netdev_priv(ndev
);
184 struct net_device_stats
*stats
= &priv
->stats
[q
];
185 int num_tx_desc
= priv
->num_tx_desc
;
186 struct ravb_tx_desc
*desc
;
191 for (; priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] > 0; priv
->dirty_tx
[q
]++) {
194 entry
= priv
->dirty_tx
[q
] % (priv
->num_tx_ring
[q
] *
196 desc
= &priv
->tx_ring
[q
][entry
];
197 txed
= desc
->die_dt
== DT_FEMPTY
;
198 if (free_txed_only
&& !txed
)
200 /* Descriptor type must be checked before all other reads */
202 size
= le16_to_cpu(desc
->ds_tagl
) & TX_DS
;
203 /* Free the original skb. */
204 if (priv
->tx_skb
[q
][entry
/ num_tx_desc
]) {
205 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
206 size
, DMA_TO_DEVICE
);
207 /* Last packet descriptor? */
208 if (entry
% num_tx_desc
== num_tx_desc
- 1) {
209 entry
/= num_tx_desc
;
210 dev_kfree_skb_any(priv
->tx_skb
[q
][entry
]);
211 priv
->tx_skb
[q
][entry
] = NULL
;
218 stats
->tx_bytes
+= size
;
219 desc
->die_dt
= DT_EEMPTY
;
224 /* Free skb's and DMA buffers for Ethernet AVB */
225 static void ravb_ring_free(struct net_device
*ndev
, int q
)
227 struct ravb_private
*priv
= netdev_priv(ndev
);
228 int num_tx_desc
= priv
->num_tx_desc
;
232 if (priv
->rx_ring
[q
]) {
233 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++) {
234 struct ravb_ex_rx_desc
*desc
= &priv
->rx_ring
[q
][i
];
236 if (!dma_mapping_error(ndev
->dev
.parent
,
237 le32_to_cpu(desc
->dptr
)))
238 dma_unmap_single(ndev
->dev
.parent
,
239 le32_to_cpu(desc
->dptr
),
243 ring_size
= sizeof(struct ravb_ex_rx_desc
) *
244 (priv
->num_rx_ring
[q
] + 1);
245 dma_free_coherent(ndev
->dev
.parent
, ring_size
, priv
->rx_ring
[q
],
246 priv
->rx_desc_dma
[q
]);
247 priv
->rx_ring
[q
] = NULL
;
250 if (priv
->tx_ring
[q
]) {
251 ravb_tx_free(ndev
, q
, false);
253 ring_size
= sizeof(struct ravb_tx_desc
) *
254 (priv
->num_tx_ring
[q
] * num_tx_desc
+ 1);
255 dma_free_coherent(ndev
->dev
.parent
, ring_size
, priv
->tx_ring
[q
],
256 priv
->tx_desc_dma
[q
]);
257 priv
->tx_ring
[q
] = NULL
;
260 /* Free RX skb ringbuffer */
261 if (priv
->rx_skb
[q
]) {
262 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++)
263 dev_kfree_skb(priv
->rx_skb
[q
][i
]);
265 kfree(priv
->rx_skb
[q
]);
266 priv
->rx_skb
[q
] = NULL
;
268 /* Free aligned TX buffers */
269 kfree(priv
->tx_align
[q
]);
270 priv
->tx_align
[q
] = NULL
;
272 /* Free TX skb ringbuffer.
273 * SKBs are freed by ravb_tx_free() call above.
275 kfree(priv
->tx_skb
[q
]);
276 priv
->tx_skb
[q
] = NULL
;
279 /* Format skb and descriptor buffer for Ethernet AVB */
280 static void ravb_ring_format(struct net_device
*ndev
, int q
)
282 struct ravb_private
*priv
= netdev_priv(ndev
);
283 int num_tx_desc
= priv
->num_tx_desc
;
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(priv
->rx_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
;
324 if (num_tx_desc
> 1) {
326 tx_desc
->die_dt
= DT_EEMPTY
;
329 tx_desc
->dptr
= cpu_to_le32((u32
)priv
->tx_desc_dma
[q
]);
330 tx_desc
->die_dt
= DT_LINKFIX
; /* type */
332 /* RX descriptor base address for best effort */
333 desc
= &priv
->desc_bat
[RX_QUEUE_OFFSET
+ q
];
334 desc
->die_dt
= DT_LINKFIX
; /* type */
335 desc
->dptr
= cpu_to_le32((u32
)priv
->rx_desc_dma
[q
]);
337 /* TX descriptor base address for best effort */
338 desc
= &priv
->desc_bat
[q
];
339 desc
->die_dt
= DT_LINKFIX
; /* type */
340 desc
->dptr
= cpu_to_le32((u32
)priv
->tx_desc_dma
[q
]);
343 /* Init skb and descriptor buffer for Ethernet AVB */
344 static int ravb_ring_init(struct net_device
*ndev
, int q
)
346 struct ravb_private
*priv
= netdev_priv(ndev
);
347 int num_tx_desc
= priv
->num_tx_desc
;
352 priv
->rx_buf_sz
= (ndev
->mtu
<= 1492 ? PKT_BUF_SZ
: ndev
->mtu
) +
353 ETH_HLEN
+ VLAN_HLEN
;
355 /* Allocate RX and TX skb rings */
356 priv
->rx_skb
[q
] = kcalloc(priv
->num_rx_ring
[q
],
357 sizeof(*priv
->rx_skb
[q
]), GFP_KERNEL
);
358 priv
->tx_skb
[q
] = kcalloc(priv
->num_tx_ring
[q
],
359 sizeof(*priv
->tx_skb
[q
]), GFP_KERNEL
);
360 if (!priv
->rx_skb
[q
] || !priv
->tx_skb
[q
])
363 for (i
= 0; i
< priv
->num_rx_ring
[q
]; i
++) {
364 skb
= netdev_alloc_skb(ndev
, priv
->rx_buf_sz
+ RAVB_ALIGN
- 1);
367 ravb_set_buffer_align(skb
);
368 priv
->rx_skb
[q
][i
] = skb
;
371 if (num_tx_desc
> 1) {
372 /* Allocate rings for the aligned buffers */
373 priv
->tx_align
[q
] = kmalloc(DPTR_ALIGN
* priv
->num_tx_ring
[q
] +
374 DPTR_ALIGN
- 1, GFP_KERNEL
);
375 if (!priv
->tx_align
[q
])
379 /* Allocate all RX descriptors. */
380 ring_size
= sizeof(struct ravb_ex_rx_desc
) * (priv
->num_rx_ring
[q
] + 1);
381 priv
->rx_ring
[q
] = dma_alloc_coherent(ndev
->dev
.parent
, ring_size
,
382 &priv
->rx_desc_dma
[q
],
384 if (!priv
->rx_ring
[q
])
387 priv
->dirty_rx
[q
] = 0;
389 /* Allocate all TX descriptors. */
390 ring_size
= sizeof(struct ravb_tx_desc
) *
391 (priv
->num_tx_ring
[q
] * num_tx_desc
+ 1);
392 priv
->tx_ring
[q
] = dma_alloc_coherent(ndev
->dev
.parent
, ring_size
,
393 &priv
->tx_desc_dma
[q
],
395 if (!priv
->tx_ring
[q
])
401 ravb_ring_free(ndev
, q
);
406 /* E-MAC init function */
407 static void ravb_emac_init(struct net_device
*ndev
)
409 struct ravb_private
*priv
= netdev_priv(ndev
);
411 /* Receive frame limit set register */
412 ravb_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
, RFLR
);
414 /* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */
415 ravb_write(ndev
, ECMR_ZPF
| (priv
->duplex
? ECMR_DM
: 0) |
416 (ndev
->features
& NETIF_F_RXCSUM
? ECMR_RCSC
: 0) |
417 ECMR_TE
| ECMR_RE
, ECMR
);
421 /* Set MAC address */
423 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
424 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
426 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
428 /* E-MAC status register clear */
429 ravb_write(ndev
, ECSR_ICD
| ECSR_MPD
, ECSR
);
431 /* E-MAC interrupt enable register */
432 ravb_write(ndev
, ECSIPR_ICDIP
| ECSIPR_MPDIP
| ECSIPR_LCHNGIP
, ECSIPR
);
435 /* Device init function for Ethernet AVB */
436 static int ravb_dmac_init(struct net_device
*ndev
)
438 struct ravb_private
*priv
= netdev_priv(ndev
);
441 /* Set CONFIG mode */
442 error
= ravb_config(ndev
);
446 error
= ravb_ring_init(ndev
, RAVB_BE
);
449 error
= ravb_ring_init(ndev
, RAVB_NC
);
451 ravb_ring_free(ndev
, RAVB_BE
);
455 /* Descriptor format */
456 ravb_ring_format(ndev
, RAVB_BE
);
457 ravb_ring_format(ndev
, RAVB_NC
);
459 #if defined(__LITTLE_ENDIAN)
460 ravb_modify(ndev
, CCC
, CCC_BOC
, 0);
462 ravb_modify(ndev
, CCC
, CCC_BOC
, CCC_BOC
);
467 RCR_EFFS
| RCR_ENCF
| RCR_ETS0
| RCR_ESF
| 0x18000000, RCR
);
470 ravb_write(ndev
, TGC_TQP_AVBMODE1
| 0x00222200, TGC
);
472 /* Timestamp enable */
473 ravb_write(ndev
, TCCR_TFEN
, TCCR
);
475 /* Interrupt init: */
476 if (priv
->chip_id
== RCAR_GEN3
) {
478 ravb_write(ndev
, 0, DIL
);
479 /* Set queue specific interrupt */
480 ravb_write(ndev
, CIE_CRIE
| CIE_CTIE
| CIE_CL0M
, CIE
);
483 ravb_write(ndev
, RIC0_FRE0
| RIC0_FRE1
, RIC0
);
484 /* Disable FIFO full warning */
485 ravb_write(ndev
, 0, RIC1
);
486 /* Receive FIFO full error, descriptor empty */
487 ravb_write(ndev
, RIC2_QFE0
| RIC2_QFE1
| RIC2_RFFE
, RIC2
);
488 /* Frame transmitted, timestamp FIFO updated */
489 ravb_write(ndev
, TIC_FTE0
| TIC_FTE1
| TIC_TFUE
, TIC
);
491 /* Setting the control will start the AVB-DMAC process. */
492 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_OPERATION
);
497 static void ravb_get_tx_tstamp(struct net_device
*ndev
)
499 struct ravb_private
*priv
= netdev_priv(ndev
);
500 struct ravb_tstamp_skb
*ts_skb
, *ts_skb2
;
501 struct skb_shared_hwtstamps shhwtstamps
;
503 struct timespec64 ts
;
508 count
= (ravb_read(ndev
, TSR
) & TSR_TFFL
) >> 8;
510 tfa2
= ravb_read(ndev
, TFA2
);
511 tfa_tag
= (tfa2
& TFA2_TST
) >> 16;
512 ts
.tv_nsec
= (u64
)ravb_read(ndev
, TFA0
);
513 ts
.tv_sec
= ((u64
)(tfa2
& TFA2_TSV
) << 32) |
514 ravb_read(ndev
, TFA1
);
515 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
516 shhwtstamps
.hwtstamp
= timespec64_to_ktime(ts
);
517 list_for_each_entry_safe(ts_skb
, ts_skb2
, &priv
->ts_skb_list
,
521 list_del(&ts_skb
->list
);
523 if (tag
== tfa_tag
) {
524 skb_tstamp_tx(skb
, &shhwtstamps
);
528 ravb_modify(ndev
, TCCR
, TCCR_TFR
, TCCR_TFR
);
532 static void ravb_rx_csum(struct sk_buff
*skb
)
536 /* The hardware checksum is 2 bytes appended to packet data */
537 if (unlikely(skb
->len
< 2))
539 hw_csum
= skb_tail_pointer(skb
) - 2;
540 skb
->csum
= csum_unfold((__force __sum16
)get_unaligned_le16(hw_csum
));
541 skb
->ip_summed
= CHECKSUM_COMPLETE
;
542 skb_trim(skb
, skb
->len
- 2);
545 /* Packet receive function for Ethernet AVB */
546 static bool ravb_rx(struct net_device
*ndev
, int *quota
, int q
)
548 struct ravb_private
*priv
= netdev_priv(ndev
);
549 int entry
= priv
->cur_rx
[q
] % priv
->num_rx_ring
[q
];
550 int boguscnt
= (priv
->dirty_rx
[q
] + priv
->num_rx_ring
[q
]) -
552 struct net_device_stats
*stats
= &priv
->stats
[q
];
553 struct ravb_ex_rx_desc
*desc
;
556 struct timespec64 ts
;
561 boguscnt
= min(boguscnt
, *quota
);
563 desc
= &priv
->rx_ring
[q
][entry
];
564 while (desc
->die_dt
!= DT_FEMPTY
) {
565 /* Descriptor type must be checked before all other reads */
567 desc_status
= desc
->msc
;
568 pkt_len
= le16_to_cpu(desc
->ds_cc
) & RX_DS
;
573 /* We use 0-byte descriptors to mark the DMA mapping errors */
577 if (desc_status
& MSC_MC
)
580 if (desc_status
& (MSC_CRC
| MSC_RFE
| MSC_RTSF
| MSC_RTLF
|
583 if (desc_status
& MSC_CRC
)
584 stats
->rx_crc_errors
++;
585 if (desc_status
& MSC_RFE
)
586 stats
->rx_frame_errors
++;
587 if (desc_status
& (MSC_RTLF
| MSC_RTSF
))
588 stats
->rx_length_errors
++;
589 if (desc_status
& MSC_CEEF
)
590 stats
->rx_missed_errors
++;
592 u32 get_ts
= priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE
;
594 skb
= priv
->rx_skb
[q
][entry
];
595 priv
->rx_skb
[q
][entry
] = NULL
;
596 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
599 get_ts
&= (q
== RAVB_NC
) ?
600 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
:
601 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
;
603 struct skb_shared_hwtstamps
*shhwtstamps
;
605 shhwtstamps
= skb_hwtstamps(skb
);
606 memset(shhwtstamps
, 0, sizeof(*shhwtstamps
));
607 ts
.tv_sec
= ((u64
) le16_to_cpu(desc
->ts_sh
) <<
608 32) | le32_to_cpu(desc
->ts_sl
);
609 ts
.tv_nsec
= le32_to_cpu(desc
->ts_n
);
610 shhwtstamps
->hwtstamp
= timespec64_to_ktime(ts
);
613 skb_put(skb
, pkt_len
);
614 skb
->protocol
= eth_type_trans(skb
, ndev
);
615 if (ndev
->features
& NETIF_F_RXCSUM
)
617 napi_gro_receive(&priv
->napi
[q
], skb
);
619 stats
->rx_bytes
+= pkt_len
;
622 entry
= (++priv
->cur_rx
[q
]) % priv
->num_rx_ring
[q
];
623 desc
= &priv
->rx_ring
[q
][entry
];
626 /* Refill the RX ring buffers. */
627 for (; priv
->cur_rx
[q
] - priv
->dirty_rx
[q
] > 0; priv
->dirty_rx
[q
]++) {
628 entry
= priv
->dirty_rx
[q
] % priv
->num_rx_ring
[q
];
629 desc
= &priv
->rx_ring
[q
][entry
];
630 desc
->ds_cc
= cpu_to_le16(priv
->rx_buf_sz
);
632 if (!priv
->rx_skb
[q
][entry
]) {
633 skb
= netdev_alloc_skb(ndev
,
637 break; /* Better luck next round. */
638 ravb_set_buffer_align(skb
);
639 dma_addr
= dma_map_single(ndev
->dev
.parent
, skb
->data
,
640 le16_to_cpu(desc
->ds_cc
),
642 skb_checksum_none_assert(skb
);
643 /* We just set the data size to 0 for a failed mapping
644 * which should prevent DMA from happening...
646 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
647 desc
->ds_cc
= cpu_to_le16(0);
648 desc
->dptr
= cpu_to_le32(dma_addr
);
649 priv
->rx_skb
[q
][entry
] = skb
;
651 /* Descriptor type must be set after all the above writes */
653 desc
->die_dt
= DT_FEMPTY
;
656 *quota
-= limit
- (++boguscnt
);
658 return boguscnt
<= 0;
661 static void ravb_rcv_snd_disable(struct net_device
*ndev
)
663 /* Disable TX and RX */
664 ravb_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, 0);
667 static void ravb_rcv_snd_enable(struct net_device
*ndev
)
669 /* Enable TX and RX */
670 ravb_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, ECMR_RE
| ECMR_TE
);
673 /* function for waiting dma process finished */
674 static int ravb_stop_dma(struct net_device
*ndev
)
678 /* Wait for stopping the hardware TX process */
679 error
= ravb_wait(ndev
, TCCR
,
680 TCCR_TSRQ0
| TCCR_TSRQ1
| TCCR_TSRQ2
| TCCR_TSRQ3
, 0);
684 error
= ravb_wait(ndev
, CSR
, CSR_TPO0
| CSR_TPO1
| CSR_TPO2
| CSR_TPO3
,
689 /* Stop the E-MAC's RX/TX processes. */
690 ravb_rcv_snd_disable(ndev
);
692 /* Wait for stopping the RX DMA process */
693 error
= ravb_wait(ndev
, CSR
, CSR_RPO
, 0);
697 /* Stop AVB-DMAC process */
698 return ravb_config(ndev
);
701 /* E-MAC interrupt handler */
702 static void ravb_emac_interrupt_unlocked(struct net_device
*ndev
)
704 struct ravb_private
*priv
= netdev_priv(ndev
);
707 ecsr
= ravb_read(ndev
, ECSR
);
708 ravb_write(ndev
, ecsr
, ECSR
); /* clear interrupt */
711 pm_wakeup_event(&priv
->pdev
->dev
, 0);
713 ndev
->stats
.tx_carrier_errors
++;
714 if (ecsr
& ECSR_LCHNG
) {
716 if (priv
->no_avb_link
)
718 psr
= ravb_read(ndev
, PSR
);
719 if (priv
->avb_link_active_low
)
721 if (!(psr
& PSR_LMON
)) {
722 /* DIsable RX and TX */
723 ravb_rcv_snd_disable(ndev
);
725 /* Enable RX and TX */
726 ravb_rcv_snd_enable(ndev
);
731 static irqreturn_t
ravb_emac_interrupt(int irq
, void *dev_id
)
733 struct net_device
*ndev
= dev_id
;
734 struct ravb_private
*priv
= netdev_priv(ndev
);
736 spin_lock(&priv
->lock
);
737 ravb_emac_interrupt_unlocked(ndev
);
739 spin_unlock(&priv
->lock
);
743 /* Error interrupt handler */
744 static void ravb_error_interrupt(struct net_device
*ndev
)
746 struct ravb_private
*priv
= netdev_priv(ndev
);
749 eis
= ravb_read(ndev
, EIS
);
750 ravb_write(ndev
, ~(EIS_QFS
| EIS_RESERVED
), EIS
);
752 ris2
= ravb_read(ndev
, RIS2
);
753 ravb_write(ndev
, ~(RIS2_QFF0
| RIS2_RFFF
| RIS2_RESERVED
),
756 /* Receive Descriptor Empty int */
757 if (ris2
& RIS2_QFF0
)
758 priv
->stats
[RAVB_BE
].rx_over_errors
++;
760 /* Receive Descriptor Empty int */
761 if (ris2
& RIS2_QFF1
)
762 priv
->stats
[RAVB_NC
].rx_over_errors
++;
764 /* Receive FIFO Overflow int */
765 if (ris2
& RIS2_RFFF
)
766 priv
->rx_fifo_errors
++;
770 static bool ravb_queue_interrupt(struct net_device
*ndev
, int q
)
772 struct ravb_private
*priv
= netdev_priv(ndev
);
773 u32 ris0
= ravb_read(ndev
, RIS0
);
774 u32 ric0
= ravb_read(ndev
, RIC0
);
775 u32 tis
= ravb_read(ndev
, TIS
);
776 u32 tic
= ravb_read(ndev
, TIC
);
778 if (((ris0
& ric0
) & BIT(q
)) || ((tis
& tic
) & BIT(q
))) {
779 if (napi_schedule_prep(&priv
->napi
[q
])) {
780 /* Mask RX and TX interrupts */
781 if (priv
->chip_id
== RCAR_GEN2
) {
782 ravb_write(ndev
, ric0
& ~BIT(q
), RIC0
);
783 ravb_write(ndev
, tic
& ~BIT(q
), TIC
);
785 ravb_write(ndev
, BIT(q
), RID0
);
786 ravb_write(ndev
, BIT(q
), TID
);
788 __napi_schedule(&priv
->napi
[q
]);
791 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
794 " tx status 0x%08x, tx mask 0x%08x.\n",
802 static bool ravb_timestamp_interrupt(struct net_device
*ndev
)
804 u32 tis
= ravb_read(ndev
, TIS
);
806 if (tis
& TIS_TFUF
) {
807 ravb_write(ndev
, ~(TIS_TFUF
| TIS_RESERVED
), TIS
);
808 ravb_get_tx_tstamp(ndev
);
814 static irqreturn_t
ravb_interrupt(int irq
, void *dev_id
)
816 struct net_device
*ndev
= dev_id
;
817 struct ravb_private
*priv
= netdev_priv(ndev
);
818 irqreturn_t result
= IRQ_NONE
;
821 spin_lock(&priv
->lock
);
822 /* Get interrupt status */
823 iss
= ravb_read(ndev
, ISS
);
825 /* Received and transmitted interrupts */
826 if (iss
& (ISS_FRS
| ISS_FTS
| ISS_TFUS
)) {
829 /* Timestamp updated */
830 if (ravb_timestamp_interrupt(ndev
))
831 result
= IRQ_HANDLED
;
833 /* Network control and best effort queue RX/TX */
834 for (q
= RAVB_NC
; q
>= RAVB_BE
; q
--) {
835 if (ravb_queue_interrupt(ndev
, q
))
836 result
= IRQ_HANDLED
;
840 /* E-MAC status summary */
842 ravb_emac_interrupt_unlocked(ndev
);
843 result
= IRQ_HANDLED
;
846 /* Error status summary */
848 ravb_error_interrupt(ndev
);
849 result
= IRQ_HANDLED
;
852 /* gPTP interrupt status summary */
853 if (iss
& ISS_CGIS
) {
854 ravb_ptp_interrupt(ndev
);
855 result
= IRQ_HANDLED
;
859 spin_unlock(&priv
->lock
);
863 /* Timestamp/Error/gPTP interrupt handler */
864 static irqreturn_t
ravb_multi_interrupt(int irq
, void *dev_id
)
866 struct net_device
*ndev
= dev_id
;
867 struct ravb_private
*priv
= netdev_priv(ndev
);
868 irqreturn_t result
= IRQ_NONE
;
871 spin_lock(&priv
->lock
);
872 /* Get interrupt status */
873 iss
= ravb_read(ndev
, ISS
);
875 /* Timestamp updated */
876 if ((iss
& ISS_TFUS
) && ravb_timestamp_interrupt(ndev
))
877 result
= IRQ_HANDLED
;
879 /* Error status summary */
881 ravb_error_interrupt(ndev
);
882 result
= IRQ_HANDLED
;
885 /* gPTP interrupt status summary */
886 if (iss
& ISS_CGIS
) {
887 ravb_ptp_interrupt(ndev
);
888 result
= IRQ_HANDLED
;
892 spin_unlock(&priv
->lock
);
896 static irqreturn_t
ravb_dma_interrupt(int irq
, void *dev_id
, int q
)
898 struct net_device
*ndev
= dev_id
;
899 struct ravb_private
*priv
= netdev_priv(ndev
);
900 irqreturn_t result
= IRQ_NONE
;
902 spin_lock(&priv
->lock
);
904 /* Network control/Best effort queue RX/TX */
905 if (ravb_queue_interrupt(ndev
, q
))
906 result
= IRQ_HANDLED
;
909 spin_unlock(&priv
->lock
);
913 static irqreturn_t
ravb_be_interrupt(int irq
, void *dev_id
)
915 return ravb_dma_interrupt(irq
, dev_id
, RAVB_BE
);
918 static irqreturn_t
ravb_nc_interrupt(int irq
, void *dev_id
)
920 return ravb_dma_interrupt(irq
, dev_id
, RAVB_NC
);
923 static int ravb_poll(struct napi_struct
*napi
, int budget
)
925 struct net_device
*ndev
= napi
->dev
;
926 struct ravb_private
*priv
= netdev_priv(ndev
);
928 int q
= napi
- priv
->napi
;
934 tis
= ravb_read(ndev
, TIS
);
935 ris0
= ravb_read(ndev
, RIS0
);
936 if (!((ris0
& mask
) || (tis
& mask
)))
939 /* Processing RX Descriptor Ring */
941 /* Clear RX interrupt */
942 ravb_write(ndev
, ~(mask
| RIS0_RESERVED
), RIS0
);
943 if (ravb_rx(ndev
, "a
, q
))
946 /* Processing TX Descriptor Ring */
948 spin_lock_irqsave(&priv
->lock
, flags
);
949 /* Clear TX interrupt */
950 ravb_write(ndev
, ~(mask
| TIS_RESERVED
), TIS
);
951 ravb_tx_free(ndev
, q
, true);
952 netif_wake_subqueue(ndev
, q
);
954 spin_unlock_irqrestore(&priv
->lock
, flags
);
960 /* Re-enable RX/TX interrupts */
961 spin_lock_irqsave(&priv
->lock
, flags
);
962 if (priv
->chip_id
== RCAR_GEN2
) {
963 ravb_modify(ndev
, RIC0
, mask
, mask
);
964 ravb_modify(ndev
, TIC
, mask
, mask
);
966 ravb_write(ndev
, mask
, RIE0
);
967 ravb_write(ndev
, mask
, TIE
);
970 spin_unlock_irqrestore(&priv
->lock
, flags
);
972 /* Receive error message handling */
973 priv
->rx_over_errors
= priv
->stats
[RAVB_BE
].rx_over_errors
;
974 priv
->rx_over_errors
+= priv
->stats
[RAVB_NC
].rx_over_errors
;
975 if (priv
->rx_over_errors
!= ndev
->stats
.rx_over_errors
)
976 ndev
->stats
.rx_over_errors
= priv
->rx_over_errors
;
977 if (priv
->rx_fifo_errors
!= ndev
->stats
.rx_fifo_errors
)
978 ndev
->stats
.rx_fifo_errors
= priv
->rx_fifo_errors
;
980 return budget
- quota
;
983 /* PHY state control function */
984 static void ravb_adjust_link(struct net_device
*ndev
)
986 struct ravb_private
*priv
= netdev_priv(ndev
);
987 struct phy_device
*phydev
= ndev
->phydev
;
988 bool new_state
= false;
991 spin_lock_irqsave(&priv
->lock
, flags
);
993 /* Disable TX and RX right over here, if E-MAC change is ignored */
994 if (priv
->no_avb_link
)
995 ravb_rcv_snd_disable(ndev
);
998 if (phydev
->duplex
!= priv
->duplex
) {
1000 priv
->duplex
= phydev
->duplex
;
1001 ravb_set_duplex(ndev
);
1004 if (phydev
->speed
!= priv
->speed
) {
1006 priv
->speed
= phydev
->speed
;
1007 ravb_set_rate(ndev
);
1010 ravb_modify(ndev
, ECMR
, ECMR_TXF
, 0);
1012 priv
->link
= phydev
->link
;
1014 } else if (priv
->link
) {
1021 /* Enable TX and RX right over here, if E-MAC change is ignored */
1022 if (priv
->no_avb_link
&& phydev
->link
)
1023 ravb_rcv_snd_enable(ndev
);
1026 spin_unlock_irqrestore(&priv
->lock
, flags
);
1028 if (new_state
&& netif_msg_link(priv
))
1029 phy_print_status(phydev
);
1032 static const struct soc_device_attribute r8a7795es10
[] = {
1033 { .soc_id
= "r8a7795", .revision
= "ES1.0", },
1037 /* PHY init function */
1038 static int ravb_phy_init(struct net_device
*ndev
)
1040 struct device_node
*np
= ndev
->dev
.parent
->of_node
;
1041 struct ravb_private
*priv
= netdev_priv(ndev
);
1042 struct phy_device
*phydev
;
1043 struct device_node
*pn
;
1050 /* Try connecting to PHY */
1051 pn
= of_parse_phandle(np
, "phy-handle", 0);
1053 /* In the case of a fixed PHY, the DT node associated
1054 * to the PHY is the Ethernet MAC DT node.
1056 if (of_phy_is_fixed_link(np
)) {
1057 err
= of_phy_register_fixed_link(np
);
1061 pn
= of_node_get(np
);
1063 phydev
= of_phy_connect(ndev
, pn
, ravb_adjust_link
, 0,
1064 priv
->phy_interface
);
1067 netdev_err(ndev
, "failed to connect PHY\n");
1069 goto err_deregister_fixed_link
;
1072 /* This driver only support 10/100Mbit speeds on R-Car H3 ES1.0
1075 if (soc_device_match(r8a7795es10
)) {
1076 err
= phy_set_max_speed(phydev
, SPEED_100
);
1078 netdev_err(ndev
, "failed to limit PHY to 100Mbit/s\n");
1079 goto err_phy_disconnect
;
1082 netdev_info(ndev
, "limited PHY to 100Mbit/s\n");
1085 /* 10BASE, Pause and Asym Pause is not supported */
1086 phy_remove_link_mode(phydev
, ETHTOOL_LINK_MODE_10baseT_Half_BIT
);
1087 phy_remove_link_mode(phydev
, ETHTOOL_LINK_MODE_10baseT_Full_BIT
);
1088 phy_remove_link_mode(phydev
, ETHTOOL_LINK_MODE_Pause_BIT
);
1089 phy_remove_link_mode(phydev
, ETHTOOL_LINK_MODE_Asym_Pause_BIT
);
1091 phy_attached_info(phydev
);
1096 phy_disconnect(phydev
);
1097 err_deregister_fixed_link
:
1098 if (of_phy_is_fixed_link(np
))
1099 of_phy_deregister_fixed_link(np
);
1104 /* PHY control start function */
1105 static int ravb_phy_start(struct net_device
*ndev
)
1109 error
= ravb_phy_init(ndev
);
1113 phy_start(ndev
->phydev
);
1118 static u32
ravb_get_msglevel(struct net_device
*ndev
)
1120 struct ravb_private
*priv
= netdev_priv(ndev
);
1122 return priv
->msg_enable
;
1125 static void ravb_set_msglevel(struct net_device
*ndev
, u32 value
)
1127 struct ravb_private
*priv
= netdev_priv(ndev
);
1129 priv
->msg_enable
= value
;
1132 static const char ravb_gstrings_stats
[][ETH_GSTRING_LEN
] = {
1133 "rx_queue_0_current",
1134 "tx_queue_0_current",
1137 "rx_queue_0_packets",
1138 "tx_queue_0_packets",
1141 "rx_queue_0_mcast_packets",
1142 "rx_queue_0_errors",
1143 "rx_queue_0_crc_errors",
1144 "rx_queue_0_frame_errors",
1145 "rx_queue_0_length_errors",
1146 "rx_queue_0_missed_errors",
1147 "rx_queue_0_over_errors",
1149 "rx_queue_1_current",
1150 "tx_queue_1_current",
1153 "rx_queue_1_packets",
1154 "tx_queue_1_packets",
1157 "rx_queue_1_mcast_packets",
1158 "rx_queue_1_errors",
1159 "rx_queue_1_crc_errors",
1160 "rx_queue_1_frame_errors",
1161 "rx_queue_1_length_errors",
1162 "rx_queue_1_missed_errors",
1163 "rx_queue_1_over_errors",
1166 #define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
1168 static int ravb_get_sset_count(struct net_device
*netdev
, int sset
)
1172 return RAVB_STATS_LEN
;
1178 static void ravb_get_ethtool_stats(struct net_device
*ndev
,
1179 struct ethtool_stats
*estats
, u64
*data
)
1181 struct ravb_private
*priv
= netdev_priv(ndev
);
1185 /* Device-specific stats */
1186 for (q
= RAVB_BE
; q
< NUM_RX_QUEUE
; q
++) {
1187 struct net_device_stats
*stats
= &priv
->stats
[q
];
1189 data
[i
++] = priv
->cur_rx
[q
];
1190 data
[i
++] = priv
->cur_tx
[q
];
1191 data
[i
++] = priv
->dirty_rx
[q
];
1192 data
[i
++] = priv
->dirty_tx
[q
];
1193 data
[i
++] = stats
->rx_packets
;
1194 data
[i
++] = stats
->tx_packets
;
1195 data
[i
++] = stats
->rx_bytes
;
1196 data
[i
++] = stats
->tx_bytes
;
1197 data
[i
++] = stats
->multicast
;
1198 data
[i
++] = stats
->rx_errors
;
1199 data
[i
++] = stats
->rx_crc_errors
;
1200 data
[i
++] = stats
->rx_frame_errors
;
1201 data
[i
++] = stats
->rx_length_errors
;
1202 data
[i
++] = stats
->rx_missed_errors
;
1203 data
[i
++] = stats
->rx_over_errors
;
1207 static void ravb_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
1209 switch (stringset
) {
1211 memcpy(data
, ravb_gstrings_stats
, sizeof(ravb_gstrings_stats
));
1216 static void ravb_get_ringparam(struct net_device
*ndev
,
1217 struct ethtool_ringparam
*ring
)
1219 struct ravb_private
*priv
= netdev_priv(ndev
);
1221 ring
->rx_max_pending
= BE_RX_RING_MAX
;
1222 ring
->tx_max_pending
= BE_TX_RING_MAX
;
1223 ring
->rx_pending
= priv
->num_rx_ring
[RAVB_BE
];
1224 ring
->tx_pending
= priv
->num_tx_ring
[RAVB_BE
];
1227 static int ravb_set_ringparam(struct net_device
*ndev
,
1228 struct ethtool_ringparam
*ring
)
1230 struct ravb_private
*priv
= netdev_priv(ndev
);
1233 if (ring
->tx_pending
> BE_TX_RING_MAX
||
1234 ring
->rx_pending
> BE_RX_RING_MAX
||
1235 ring
->tx_pending
< BE_TX_RING_MIN
||
1236 ring
->rx_pending
< BE_RX_RING_MIN
)
1238 if (ring
->rx_mini_pending
|| ring
->rx_jumbo_pending
)
1241 if (netif_running(ndev
)) {
1242 netif_device_detach(ndev
);
1243 /* Stop PTP Clock driver */
1244 if (priv
->chip_id
== RCAR_GEN2
)
1245 ravb_ptp_stop(ndev
);
1246 /* Wait for DMA stopping */
1247 error
= ravb_stop_dma(ndev
);
1250 "cannot set ringparam! Any AVB processes are still running?\n");
1253 synchronize_irq(ndev
->irq
);
1255 /* Free all the skb's in the RX queue and the DMA buffers. */
1256 ravb_ring_free(ndev
, RAVB_BE
);
1257 ravb_ring_free(ndev
, RAVB_NC
);
1260 /* Set new parameters */
1261 priv
->num_rx_ring
[RAVB_BE
] = ring
->rx_pending
;
1262 priv
->num_tx_ring
[RAVB_BE
] = ring
->tx_pending
;
1264 if (netif_running(ndev
)) {
1265 error
= ravb_dmac_init(ndev
);
1268 "%s: ravb_dmac_init() failed, error %d\n",
1273 ravb_emac_init(ndev
);
1275 /* Initialise PTP Clock driver */
1276 if (priv
->chip_id
== RCAR_GEN2
)
1277 ravb_ptp_init(ndev
, priv
->pdev
);
1279 netif_device_attach(ndev
);
1285 static int ravb_get_ts_info(struct net_device
*ndev
,
1286 struct ethtool_ts_info
*info
)
1288 struct ravb_private
*priv
= netdev_priv(ndev
);
1290 info
->so_timestamping
=
1291 SOF_TIMESTAMPING_TX_SOFTWARE
|
1292 SOF_TIMESTAMPING_RX_SOFTWARE
|
1293 SOF_TIMESTAMPING_SOFTWARE
|
1294 SOF_TIMESTAMPING_TX_HARDWARE
|
1295 SOF_TIMESTAMPING_RX_HARDWARE
|
1296 SOF_TIMESTAMPING_RAW_HARDWARE
;
1297 info
->tx_types
= (1 << HWTSTAMP_TX_OFF
) | (1 << HWTSTAMP_TX_ON
);
1299 (1 << HWTSTAMP_FILTER_NONE
) |
1300 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT
) |
1301 (1 << HWTSTAMP_FILTER_ALL
);
1302 info
->phc_index
= ptp_clock_index(priv
->ptp
.clock
);
1307 static void ravb_get_wol(struct net_device
*ndev
, struct ethtool_wolinfo
*wol
)
1309 struct ravb_private
*priv
= netdev_priv(ndev
);
1311 wol
->supported
= WAKE_MAGIC
;
1312 wol
->wolopts
= priv
->wol_enabled
? WAKE_MAGIC
: 0;
1315 static int ravb_set_wol(struct net_device
*ndev
, struct ethtool_wolinfo
*wol
)
1317 struct ravb_private
*priv
= netdev_priv(ndev
);
1319 if (wol
->wolopts
& ~WAKE_MAGIC
)
1322 priv
->wol_enabled
= !!(wol
->wolopts
& WAKE_MAGIC
);
1324 device_set_wakeup_enable(&priv
->pdev
->dev
, priv
->wol_enabled
);
1329 static const struct ethtool_ops ravb_ethtool_ops
= {
1330 .nway_reset
= phy_ethtool_nway_reset
,
1331 .get_msglevel
= ravb_get_msglevel
,
1332 .set_msglevel
= ravb_set_msglevel
,
1333 .get_link
= ethtool_op_get_link
,
1334 .get_strings
= ravb_get_strings
,
1335 .get_ethtool_stats
= ravb_get_ethtool_stats
,
1336 .get_sset_count
= ravb_get_sset_count
,
1337 .get_ringparam
= ravb_get_ringparam
,
1338 .set_ringparam
= ravb_set_ringparam
,
1339 .get_ts_info
= ravb_get_ts_info
,
1340 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
1341 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
1342 .get_wol
= ravb_get_wol
,
1343 .set_wol
= ravb_set_wol
,
1346 static inline int ravb_hook_irq(unsigned int irq
, irq_handler_t handler
,
1347 struct net_device
*ndev
, struct device
*dev
,
1353 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s:%s", ndev
->name
, ch
);
1356 error
= request_irq(irq
, handler
, 0, name
, ndev
);
1358 netdev_err(ndev
, "cannot request IRQ %s\n", name
);
1363 /* Network device open function for Ethernet AVB */
1364 static int ravb_open(struct net_device
*ndev
)
1366 struct ravb_private
*priv
= netdev_priv(ndev
);
1367 struct platform_device
*pdev
= priv
->pdev
;
1368 struct device
*dev
= &pdev
->dev
;
1371 napi_enable(&priv
->napi
[RAVB_BE
]);
1372 napi_enable(&priv
->napi
[RAVB_NC
]);
1374 if (priv
->chip_id
== RCAR_GEN2
) {
1375 error
= request_irq(ndev
->irq
, ravb_interrupt
, IRQF_SHARED
,
1378 netdev_err(ndev
, "cannot request IRQ\n");
1382 error
= ravb_hook_irq(ndev
->irq
, ravb_multi_interrupt
, ndev
,
1386 error
= ravb_hook_irq(priv
->emac_irq
, ravb_emac_interrupt
, ndev
,
1390 error
= ravb_hook_irq(priv
->rx_irqs
[RAVB_BE
], ravb_be_interrupt
,
1391 ndev
, dev
, "ch0:rx_be");
1393 goto out_free_irq_emac
;
1394 error
= ravb_hook_irq(priv
->tx_irqs
[RAVB_BE
], ravb_be_interrupt
,
1395 ndev
, dev
, "ch18:tx_be");
1397 goto out_free_irq_be_rx
;
1398 error
= ravb_hook_irq(priv
->rx_irqs
[RAVB_NC
], ravb_nc_interrupt
,
1399 ndev
, dev
, "ch1:rx_nc");
1401 goto out_free_irq_be_tx
;
1402 error
= ravb_hook_irq(priv
->tx_irqs
[RAVB_NC
], ravb_nc_interrupt
,
1403 ndev
, dev
, "ch19:tx_nc");
1405 goto out_free_irq_nc_rx
;
1409 error
= ravb_dmac_init(ndev
);
1411 goto out_free_irq_nc_tx
;
1412 ravb_emac_init(ndev
);
1414 /* Initialise PTP Clock driver */
1415 if (priv
->chip_id
== RCAR_GEN2
)
1416 ravb_ptp_init(ndev
, priv
->pdev
);
1418 netif_tx_start_all_queues(ndev
);
1420 /* PHY control start */
1421 error
= ravb_phy_start(ndev
);
1428 /* Stop PTP Clock driver */
1429 if (priv
->chip_id
== RCAR_GEN2
)
1430 ravb_ptp_stop(ndev
);
1432 if (priv
->chip_id
== RCAR_GEN2
)
1434 free_irq(priv
->tx_irqs
[RAVB_NC
], ndev
);
1436 free_irq(priv
->rx_irqs
[RAVB_NC
], ndev
);
1438 free_irq(priv
->tx_irqs
[RAVB_BE
], ndev
);
1440 free_irq(priv
->rx_irqs
[RAVB_BE
], ndev
);
1442 free_irq(priv
->emac_irq
, ndev
);
1444 free_irq(ndev
->irq
, ndev
);
1446 napi_disable(&priv
->napi
[RAVB_NC
]);
1447 napi_disable(&priv
->napi
[RAVB_BE
]);
1451 /* Timeout function for Ethernet AVB */
1452 static void ravb_tx_timeout(struct net_device
*ndev
)
1454 struct ravb_private
*priv
= netdev_priv(ndev
);
1456 netif_err(priv
, tx_err
, ndev
,
1457 "transmit timed out, status %08x, resetting...\n",
1458 ravb_read(ndev
, ISS
));
1460 /* tx_errors count up */
1461 ndev
->stats
.tx_errors
++;
1463 schedule_work(&priv
->work
);
1466 static void ravb_tx_timeout_work(struct work_struct
*work
)
1468 struct ravb_private
*priv
= container_of(work
, struct ravb_private
,
1470 struct net_device
*ndev
= priv
->ndev
;
1472 netif_tx_stop_all_queues(ndev
);
1474 /* Stop PTP Clock driver */
1475 if (priv
->chip_id
== RCAR_GEN2
)
1476 ravb_ptp_stop(ndev
);
1478 /* Wait for DMA stopping */
1479 ravb_stop_dma(ndev
);
1481 ravb_ring_free(ndev
, RAVB_BE
);
1482 ravb_ring_free(ndev
, RAVB_NC
);
1485 ravb_dmac_init(ndev
);
1486 ravb_emac_init(ndev
);
1488 /* Initialise PTP Clock driver */
1489 if (priv
->chip_id
== RCAR_GEN2
)
1490 ravb_ptp_init(ndev
, priv
->pdev
);
1492 netif_tx_start_all_queues(ndev
);
1495 /* Packet transmit function for Ethernet AVB */
1496 static netdev_tx_t
ravb_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1498 struct ravb_private
*priv
= netdev_priv(ndev
);
1499 int num_tx_desc
= priv
->num_tx_desc
;
1500 u16 q
= skb_get_queue_mapping(skb
);
1501 struct ravb_tstamp_skb
*ts_skb
;
1502 struct ravb_tx_desc
*desc
;
1503 unsigned long flags
;
1509 spin_lock_irqsave(&priv
->lock
, flags
);
1510 if (priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] > (priv
->num_tx_ring
[q
] - 1) *
1512 netif_err(priv
, tx_queued
, ndev
,
1513 "still transmitting with the full ring!\n");
1514 netif_stop_subqueue(ndev
, q
);
1515 spin_unlock_irqrestore(&priv
->lock
, flags
);
1516 return NETDEV_TX_BUSY
;
1519 if (skb_put_padto(skb
, ETH_ZLEN
))
1522 entry
= priv
->cur_tx
[q
] % (priv
->num_tx_ring
[q
] * num_tx_desc
);
1523 priv
->tx_skb
[q
][entry
/ num_tx_desc
] = skb
;
1525 if (num_tx_desc
> 1) {
1526 buffer
= PTR_ALIGN(priv
->tx_align
[q
], DPTR_ALIGN
) +
1527 entry
/ num_tx_desc
* DPTR_ALIGN
;
1528 len
= PTR_ALIGN(skb
->data
, DPTR_ALIGN
) - skb
->data
;
1530 /* Zero length DMA descriptors are problematic as they seem
1531 * to terminate DMA transfers. Avoid them by simply using a
1532 * length of DPTR_ALIGN (4) when skb data is aligned to
1535 * As skb is guaranteed to have at least ETH_ZLEN (60)
1536 * bytes of data by the call to skb_put_padto() above this
1537 * is safe with respect to both the length of the first DMA
1538 * descriptor (len) overflowing the available data and the
1539 * length of the second DMA descriptor (skb->len - len)
1545 memcpy(buffer
, skb
->data
, len
);
1546 dma_addr
= dma_map_single(ndev
->dev
.parent
, buffer
, len
,
1548 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
1551 desc
= &priv
->tx_ring
[q
][entry
];
1552 desc
->ds_tagl
= cpu_to_le16(len
);
1553 desc
->dptr
= cpu_to_le32(dma_addr
);
1555 buffer
= skb
->data
+ len
;
1556 len
= skb
->len
- len
;
1557 dma_addr
= dma_map_single(ndev
->dev
.parent
, buffer
, len
,
1559 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
1564 desc
= &priv
->tx_ring
[q
][entry
];
1566 dma_addr
= dma_map_single(ndev
->dev
.parent
, skb
->data
, skb
->len
,
1568 if (dma_mapping_error(ndev
->dev
.parent
, dma_addr
))
1571 desc
->ds_tagl
= cpu_to_le16(len
);
1572 desc
->dptr
= cpu_to_le32(dma_addr
);
1574 /* TX timestamp required */
1576 ts_skb
= kmalloc(sizeof(*ts_skb
), GFP_ATOMIC
);
1578 if (num_tx_desc
> 1) {
1580 dma_unmap_single(ndev
->dev
.parent
, dma_addr
,
1581 len
, DMA_TO_DEVICE
);
1586 ts_skb
->tag
= priv
->ts_skb_tag
++;
1587 priv
->ts_skb_tag
&= 0x3ff;
1588 list_add_tail(&ts_skb
->list
, &priv
->ts_skb_list
);
1590 /* TAG and timestamp required flag */
1591 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
1592 desc
->tagh_tsr
= (ts_skb
->tag
>> 4) | TX_TSR
;
1593 desc
->ds_tagl
|= cpu_to_le16(ts_skb
->tag
<< 12);
1596 skb_tx_timestamp(skb
);
1597 /* Descriptor type must be set after all the above writes */
1599 if (num_tx_desc
> 1) {
1600 desc
->die_dt
= DT_FEND
;
1602 desc
->die_dt
= DT_FSTART
;
1604 desc
->die_dt
= DT_FSINGLE
;
1606 ravb_modify(ndev
, TCCR
, TCCR_TSRQ0
<< q
, TCCR_TSRQ0
<< q
);
1608 priv
->cur_tx
[q
] += num_tx_desc
;
1609 if (priv
->cur_tx
[q
] - priv
->dirty_tx
[q
] >
1610 (priv
->num_tx_ring
[q
] - 1) * num_tx_desc
&&
1611 !ravb_tx_free(ndev
, q
, true))
1612 netif_stop_subqueue(ndev
, q
);
1616 spin_unlock_irqrestore(&priv
->lock
, flags
);
1617 return NETDEV_TX_OK
;
1620 dma_unmap_single(ndev
->dev
.parent
, le32_to_cpu(desc
->dptr
),
1621 le16_to_cpu(desc
->ds_tagl
), DMA_TO_DEVICE
);
1623 dev_kfree_skb_any(skb
);
1624 priv
->tx_skb
[q
][entry
/ num_tx_desc
] = NULL
;
1628 static u16
ravb_select_queue(struct net_device
*ndev
, struct sk_buff
*skb
,
1629 struct net_device
*sb_dev
,
1630 select_queue_fallback_t fallback
)
1632 /* If skb needs TX timestamp, it is handled in network control queue */
1633 return (skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
) ? RAVB_NC
:
1638 static struct net_device_stats
*ravb_get_stats(struct net_device
*ndev
)
1640 struct ravb_private
*priv
= netdev_priv(ndev
);
1641 struct net_device_stats
*nstats
, *stats0
, *stats1
;
1643 nstats
= &ndev
->stats
;
1644 stats0
= &priv
->stats
[RAVB_BE
];
1645 stats1
= &priv
->stats
[RAVB_NC
];
1647 nstats
->tx_dropped
+= ravb_read(ndev
, TROCR
);
1648 ravb_write(ndev
, 0, TROCR
); /* (write clear) */
1649 nstats
->collisions
+= ravb_read(ndev
, CDCR
);
1650 ravb_write(ndev
, 0, CDCR
); /* (write clear) */
1651 nstats
->tx_carrier_errors
+= ravb_read(ndev
, LCCR
);
1652 ravb_write(ndev
, 0, LCCR
); /* (write clear) */
1654 nstats
->tx_carrier_errors
+= ravb_read(ndev
, CERCR
);
1655 ravb_write(ndev
, 0, CERCR
); /* (write clear) */
1656 nstats
->tx_carrier_errors
+= ravb_read(ndev
, CEECR
);
1657 ravb_write(ndev
, 0, CEECR
); /* (write clear) */
1659 nstats
->rx_packets
= stats0
->rx_packets
+ stats1
->rx_packets
;
1660 nstats
->tx_packets
= stats0
->tx_packets
+ stats1
->tx_packets
;
1661 nstats
->rx_bytes
= stats0
->rx_bytes
+ stats1
->rx_bytes
;
1662 nstats
->tx_bytes
= stats0
->tx_bytes
+ stats1
->tx_bytes
;
1663 nstats
->multicast
= stats0
->multicast
+ stats1
->multicast
;
1664 nstats
->rx_errors
= stats0
->rx_errors
+ stats1
->rx_errors
;
1665 nstats
->rx_crc_errors
= stats0
->rx_crc_errors
+ stats1
->rx_crc_errors
;
1666 nstats
->rx_frame_errors
=
1667 stats0
->rx_frame_errors
+ stats1
->rx_frame_errors
;
1668 nstats
->rx_length_errors
=
1669 stats0
->rx_length_errors
+ stats1
->rx_length_errors
;
1670 nstats
->rx_missed_errors
=
1671 stats0
->rx_missed_errors
+ stats1
->rx_missed_errors
;
1672 nstats
->rx_over_errors
=
1673 stats0
->rx_over_errors
+ stats1
->rx_over_errors
;
1678 /* Update promiscuous bit */
1679 static void ravb_set_rx_mode(struct net_device
*ndev
)
1681 struct ravb_private
*priv
= netdev_priv(ndev
);
1682 unsigned long flags
;
1684 spin_lock_irqsave(&priv
->lock
, flags
);
1685 ravb_modify(ndev
, ECMR
, ECMR_PRM
,
1686 ndev
->flags
& IFF_PROMISC
? ECMR_PRM
: 0);
1688 spin_unlock_irqrestore(&priv
->lock
, flags
);
1691 /* Device close function for Ethernet AVB */
1692 static int ravb_close(struct net_device
*ndev
)
1694 struct device_node
*np
= ndev
->dev
.parent
->of_node
;
1695 struct ravb_private
*priv
= netdev_priv(ndev
);
1696 struct ravb_tstamp_skb
*ts_skb
, *ts_skb2
;
1698 netif_tx_stop_all_queues(ndev
);
1700 /* Disable interrupts by clearing the interrupt masks. */
1701 ravb_write(ndev
, 0, RIC0
);
1702 ravb_write(ndev
, 0, RIC2
);
1703 ravb_write(ndev
, 0, TIC
);
1705 /* Stop PTP Clock driver */
1706 if (priv
->chip_id
== RCAR_GEN2
)
1707 ravb_ptp_stop(ndev
);
1709 /* Set the config mode to stop the AVB-DMAC's processes */
1710 if (ravb_stop_dma(ndev
) < 0)
1712 "device will be stopped after h/w processes are done.\n");
1714 /* Clear the timestamp list */
1715 list_for_each_entry_safe(ts_skb
, ts_skb2
, &priv
->ts_skb_list
, list
) {
1716 list_del(&ts_skb
->list
);
1720 /* PHY disconnect */
1722 phy_stop(ndev
->phydev
);
1723 phy_disconnect(ndev
->phydev
);
1724 if (of_phy_is_fixed_link(np
))
1725 of_phy_deregister_fixed_link(np
);
1728 if (priv
->chip_id
!= RCAR_GEN2
) {
1729 free_irq(priv
->tx_irqs
[RAVB_NC
], ndev
);
1730 free_irq(priv
->rx_irqs
[RAVB_NC
], ndev
);
1731 free_irq(priv
->tx_irqs
[RAVB_BE
], ndev
);
1732 free_irq(priv
->rx_irqs
[RAVB_BE
], ndev
);
1733 free_irq(priv
->emac_irq
, ndev
);
1735 free_irq(ndev
->irq
, ndev
);
1737 napi_disable(&priv
->napi
[RAVB_NC
]);
1738 napi_disable(&priv
->napi
[RAVB_BE
]);
1740 /* Free all the skb's in the RX queue and the DMA buffers. */
1741 ravb_ring_free(ndev
, RAVB_BE
);
1742 ravb_ring_free(ndev
, RAVB_NC
);
1747 static int ravb_hwtstamp_get(struct net_device
*ndev
, struct ifreq
*req
)
1749 struct ravb_private
*priv
= netdev_priv(ndev
);
1750 struct hwtstamp_config config
;
1753 config
.tx_type
= priv
->tstamp_tx_ctrl
? HWTSTAMP_TX_ON
:
1755 if (priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
)
1756 config
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_L2_EVENT
;
1757 else if (priv
->tstamp_rx_ctrl
& RAVB_RXTSTAMP_TYPE_ALL
)
1758 config
.rx_filter
= HWTSTAMP_FILTER_ALL
;
1760 config
.rx_filter
= HWTSTAMP_FILTER_NONE
;
1762 return copy_to_user(req
->ifr_data
, &config
, sizeof(config
)) ?
1766 /* Control hardware time stamping */
1767 static int ravb_hwtstamp_set(struct net_device
*ndev
, struct ifreq
*req
)
1769 struct ravb_private
*priv
= netdev_priv(ndev
);
1770 struct hwtstamp_config config
;
1771 u32 tstamp_rx_ctrl
= RAVB_RXTSTAMP_ENABLED
;
1774 if (copy_from_user(&config
, req
->ifr_data
, sizeof(config
)))
1777 /* Reserved for future extensions */
1781 switch (config
.tx_type
) {
1782 case HWTSTAMP_TX_OFF
:
1785 case HWTSTAMP_TX_ON
:
1786 tstamp_tx_ctrl
= RAVB_TXTSTAMP_ENABLED
;
1792 switch (config
.rx_filter
) {
1793 case HWTSTAMP_FILTER_NONE
:
1796 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
1797 tstamp_rx_ctrl
|= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT
;
1800 config
.rx_filter
= HWTSTAMP_FILTER_ALL
;
1801 tstamp_rx_ctrl
|= RAVB_RXTSTAMP_TYPE_ALL
;
1804 priv
->tstamp_tx_ctrl
= tstamp_tx_ctrl
;
1805 priv
->tstamp_rx_ctrl
= tstamp_rx_ctrl
;
1807 return copy_to_user(req
->ifr_data
, &config
, sizeof(config
)) ?
1811 /* ioctl to device function */
1812 static int ravb_do_ioctl(struct net_device
*ndev
, struct ifreq
*req
, int cmd
)
1814 struct phy_device
*phydev
= ndev
->phydev
;
1816 if (!netif_running(ndev
))
1824 return ravb_hwtstamp_get(ndev
, req
);
1826 return ravb_hwtstamp_set(ndev
, req
);
1829 return phy_mii_ioctl(phydev
, req
, cmd
);
1832 static int ravb_change_mtu(struct net_device
*ndev
, int new_mtu
)
1834 if (netif_running(ndev
))
1837 ndev
->mtu
= new_mtu
;
1838 netdev_update_features(ndev
);
1843 static void ravb_set_rx_csum(struct net_device
*ndev
, bool enable
)
1845 struct ravb_private
*priv
= netdev_priv(ndev
);
1846 unsigned long flags
;
1848 spin_lock_irqsave(&priv
->lock
, flags
);
1850 /* Disable TX and RX */
1851 ravb_rcv_snd_disable(ndev
);
1853 /* Modify RX Checksum setting */
1854 ravb_modify(ndev
, ECMR
, ECMR_RCSC
, enable
? ECMR_RCSC
: 0);
1856 /* Enable TX and RX */
1857 ravb_rcv_snd_enable(ndev
);
1859 spin_unlock_irqrestore(&priv
->lock
, flags
);
1862 static int ravb_set_features(struct net_device
*ndev
,
1863 netdev_features_t features
)
1865 netdev_features_t changed
= ndev
->features
^ features
;
1867 if (changed
& NETIF_F_RXCSUM
)
1868 ravb_set_rx_csum(ndev
, features
& NETIF_F_RXCSUM
);
1870 ndev
->features
= features
;
1875 static const struct net_device_ops ravb_netdev_ops
= {
1876 .ndo_open
= ravb_open
,
1877 .ndo_stop
= ravb_close
,
1878 .ndo_start_xmit
= ravb_start_xmit
,
1879 .ndo_select_queue
= ravb_select_queue
,
1880 .ndo_get_stats
= ravb_get_stats
,
1881 .ndo_set_rx_mode
= ravb_set_rx_mode
,
1882 .ndo_tx_timeout
= ravb_tx_timeout
,
1883 .ndo_do_ioctl
= ravb_do_ioctl
,
1884 .ndo_change_mtu
= ravb_change_mtu
,
1885 .ndo_validate_addr
= eth_validate_addr
,
1886 .ndo_set_mac_address
= eth_mac_addr
,
1887 .ndo_set_features
= ravb_set_features
,
1890 /* MDIO bus init function */
1891 static int ravb_mdio_init(struct ravb_private
*priv
)
1893 struct platform_device
*pdev
= priv
->pdev
;
1894 struct device
*dev
= &pdev
->dev
;
1898 priv
->mdiobb
.ops
= &bb_ops
;
1900 /* MII controller setting */
1901 priv
->mii_bus
= alloc_mdio_bitbang(&priv
->mdiobb
);
1905 /* Hook up MII support for ethtool */
1906 priv
->mii_bus
->name
= "ravb_mii";
1907 priv
->mii_bus
->parent
= dev
;
1908 snprintf(priv
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
1909 pdev
->name
, pdev
->id
);
1911 /* Register MDIO bus */
1912 error
= of_mdiobus_register(priv
->mii_bus
, dev
->of_node
);
1919 free_mdio_bitbang(priv
->mii_bus
);
1923 /* MDIO bus release function */
1924 static int ravb_mdio_release(struct ravb_private
*priv
)
1926 /* Unregister mdio bus */
1927 mdiobus_unregister(priv
->mii_bus
);
1929 /* Free bitbang info */
1930 free_mdio_bitbang(priv
->mii_bus
);
1935 static const struct of_device_id ravb_match_table
[] = {
1936 { .compatible
= "renesas,etheravb-r8a7790", .data
= (void *)RCAR_GEN2
},
1937 { .compatible
= "renesas,etheravb-r8a7794", .data
= (void *)RCAR_GEN2
},
1938 { .compatible
= "renesas,etheravb-rcar-gen2", .data
= (void *)RCAR_GEN2
},
1939 { .compatible
= "renesas,etheravb-r8a7795", .data
= (void *)RCAR_GEN3
},
1940 { .compatible
= "renesas,etheravb-rcar-gen3", .data
= (void *)RCAR_GEN3
},
1943 MODULE_DEVICE_TABLE(of
, ravb_match_table
);
1945 static int ravb_set_gti(struct net_device
*ndev
)
1947 struct ravb_private
*priv
= netdev_priv(ndev
);
1948 struct device
*dev
= ndev
->dev
.parent
;
1952 rate
= clk_get_rate(priv
->clk
);
1956 inc
= 1000000000ULL << 20;
1959 if (inc
< GTI_TIV_MIN
|| inc
> GTI_TIV_MAX
) {
1960 dev_err(dev
, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1961 inc
, GTI_TIV_MIN
, GTI_TIV_MAX
);
1965 ravb_write(ndev
, inc
, GTI
);
1970 static void ravb_set_config_mode(struct net_device
*ndev
)
1972 struct ravb_private
*priv
= netdev_priv(ndev
);
1974 if (priv
->chip_id
== RCAR_GEN2
) {
1975 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_CONFIG
);
1976 /* Set CSEL value */
1977 ravb_modify(ndev
, CCC
, CCC_CSEL
, CCC_CSEL_HPB
);
1979 ravb_modify(ndev
, CCC
, CCC_OPC
, CCC_OPC_CONFIG
|
1980 CCC_GAC
| CCC_CSEL_HPB
);
1984 /* Set tx and rx clock internal delay modes */
1985 static void ravb_set_delay_mode(struct net_device
*ndev
)
1987 struct ravb_private
*priv
= netdev_priv(ndev
);
1990 if (priv
->phy_interface
== PHY_INTERFACE_MODE_RGMII_ID
||
1991 priv
->phy_interface
== PHY_INTERFACE_MODE_RGMII_RXID
)
1994 if (priv
->phy_interface
== PHY_INTERFACE_MODE_RGMII_ID
||
1995 priv
->phy_interface
== PHY_INTERFACE_MODE_RGMII_TXID
)
1998 ravb_modify(ndev
, APSR
, APSR_DM
, set
);
2001 static int ravb_probe(struct platform_device
*pdev
)
2003 struct device_node
*np
= pdev
->dev
.of_node
;
2004 struct ravb_private
*priv
;
2005 enum ravb_chip_id chip_id
;
2006 struct net_device
*ndev
;
2008 struct resource
*res
;
2013 "this driver is required to be instantiated from device tree\n");
2017 /* Get base address */
2018 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2020 dev_err(&pdev
->dev
, "invalid resource\n");
2024 ndev
= alloc_etherdev_mqs(sizeof(struct ravb_private
),
2025 NUM_TX_QUEUE
, NUM_RX_QUEUE
);
2029 ndev
->features
= NETIF_F_RXCSUM
;
2030 ndev
->hw_features
= NETIF_F_RXCSUM
;
2032 pm_runtime_enable(&pdev
->dev
);
2033 pm_runtime_get_sync(&pdev
->dev
);
2035 /* The Ether-specific entries in the device structure. */
2036 ndev
->base_addr
= res
->start
;
2038 chip_id
= (enum ravb_chip_id
)of_device_get_match_data(&pdev
->dev
);
2040 if (chip_id
== RCAR_GEN3
)
2041 irq
= platform_get_irq_byname(pdev
, "ch22");
2043 irq
= platform_get_irq(pdev
, 0);
2050 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
2052 priv
= netdev_priv(ndev
);
2055 priv
->num_tx_ring
[RAVB_BE
] = BE_TX_RING_SIZE
;
2056 priv
->num_rx_ring
[RAVB_BE
] = BE_RX_RING_SIZE
;
2057 priv
->num_tx_ring
[RAVB_NC
] = NC_TX_RING_SIZE
;
2058 priv
->num_rx_ring
[RAVB_NC
] = NC_RX_RING_SIZE
;
2059 priv
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
2060 if (IS_ERR(priv
->addr
)) {
2061 error
= PTR_ERR(priv
->addr
);
2065 spin_lock_init(&priv
->lock
);
2066 INIT_WORK(&priv
->work
, ravb_tx_timeout_work
);
2068 priv
->phy_interface
= of_get_phy_mode(np
);
2070 priv
->no_avb_link
= of_property_read_bool(np
, "renesas,no-ether-link");
2071 priv
->avb_link_active_low
=
2072 of_property_read_bool(np
, "renesas,ether-link-active-low");
2074 if (chip_id
== RCAR_GEN3
) {
2075 irq
= platform_get_irq_byname(pdev
, "ch24");
2080 priv
->emac_irq
= irq
;
2081 for (i
= 0; i
< NUM_RX_QUEUE
; i
++) {
2082 irq
= platform_get_irq_byname(pdev
, ravb_rx_irqs
[i
]);
2087 priv
->rx_irqs
[i
] = irq
;
2089 for (i
= 0; i
< NUM_TX_QUEUE
; i
++) {
2090 irq
= platform_get_irq_byname(pdev
, ravb_tx_irqs
[i
]);
2095 priv
->tx_irqs
[i
] = irq
;
2099 priv
->chip_id
= chip_id
;
2101 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
2102 if (IS_ERR(priv
->clk
)) {
2103 error
= PTR_ERR(priv
->clk
);
2107 ndev
->max_mtu
= 2048 - (ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
);
2108 ndev
->min_mtu
= ETH_MIN_MTU
;
2110 priv
->num_tx_desc
= chip_id
== RCAR_GEN2
?
2111 NUM_TX_DESC_GEN2
: NUM_TX_DESC_GEN3
;
2114 ndev
->netdev_ops
= &ravb_netdev_ops
;
2115 ndev
->ethtool_ops
= &ravb_ethtool_ops
;
2117 /* Set AVB config mode */
2118 ravb_set_config_mode(ndev
);
2121 error
= ravb_set_gti(ndev
);
2125 /* Request GTI loading */
2126 ravb_modify(ndev
, GCCR
, GCCR_LTI
, GCCR_LTI
);
2128 if (priv
->chip_id
!= RCAR_GEN2
)
2129 ravb_set_delay_mode(ndev
);
2131 /* Allocate descriptor base address table */
2132 priv
->desc_bat_size
= sizeof(struct ravb_desc
) * DBAT_ENTRY_NUM
;
2133 priv
->desc_bat
= dma_alloc_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
,
2134 &priv
->desc_bat_dma
, GFP_KERNEL
);
2135 if (!priv
->desc_bat
) {
2137 "Cannot allocate desc base address table (size %d bytes)\n",
2138 priv
->desc_bat_size
);
2142 for (q
= RAVB_BE
; q
< DBAT_ENTRY_NUM
; q
++)
2143 priv
->desc_bat
[q
].die_dt
= DT_EOS
;
2144 ravb_write(ndev
, priv
->desc_bat_dma
, DBAT
);
2146 /* Initialise HW timestamp list */
2147 INIT_LIST_HEAD(&priv
->ts_skb_list
);
2149 /* Initialise PTP Clock driver */
2150 if (chip_id
!= RCAR_GEN2
)
2151 ravb_ptp_init(ndev
, pdev
);
2153 /* Debug message level */
2154 priv
->msg_enable
= RAVB_DEF_MSG_ENABLE
;
2156 /* Read and set MAC address */
2157 ravb_read_mac_address(ndev
, of_get_mac_address(np
));
2158 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
2159 dev_warn(&pdev
->dev
,
2160 "no valid MAC address supplied, using a random one\n");
2161 eth_hw_addr_random(ndev
);
2165 error
= ravb_mdio_init(priv
);
2167 dev_err(&pdev
->dev
, "failed to initialize MDIO\n");
2171 netif_napi_add(ndev
, &priv
->napi
[RAVB_BE
], ravb_poll
, 64);
2172 netif_napi_add(ndev
, &priv
->napi
[RAVB_NC
], ravb_poll
, 64);
2174 /* Network device register */
2175 error
= register_netdev(ndev
);
2179 device_set_wakeup_capable(&pdev
->dev
, 1);
2181 /* Print device information */
2182 netdev_info(ndev
, "Base address at %#x, %pM, IRQ %d.\n",
2183 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
2185 platform_set_drvdata(pdev
, ndev
);
2190 netif_napi_del(&priv
->napi
[RAVB_NC
]);
2191 netif_napi_del(&priv
->napi
[RAVB_BE
]);
2192 ravb_mdio_release(priv
);
2194 dma_free_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
, priv
->desc_bat
,
2195 priv
->desc_bat_dma
);
2197 /* Stop PTP Clock driver */
2198 if (chip_id
!= RCAR_GEN2
)
2199 ravb_ptp_stop(ndev
);
2203 pm_runtime_put(&pdev
->dev
);
2204 pm_runtime_disable(&pdev
->dev
);
2208 static int ravb_remove(struct platform_device
*pdev
)
2210 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2211 struct ravb_private
*priv
= netdev_priv(ndev
);
2213 /* Stop PTP Clock driver */
2214 if (priv
->chip_id
!= RCAR_GEN2
)
2215 ravb_ptp_stop(ndev
);
2217 dma_free_coherent(ndev
->dev
.parent
, priv
->desc_bat_size
, priv
->desc_bat
,
2218 priv
->desc_bat_dma
);
2219 /* Set reset mode */
2220 ravb_write(ndev
, CCC_OPC_RESET
, CCC
);
2221 pm_runtime_put_sync(&pdev
->dev
);
2222 unregister_netdev(ndev
);
2223 netif_napi_del(&priv
->napi
[RAVB_NC
]);
2224 netif_napi_del(&priv
->napi
[RAVB_BE
]);
2225 ravb_mdio_release(priv
);
2226 pm_runtime_disable(&pdev
->dev
);
2228 platform_set_drvdata(pdev
, NULL
);
2233 static int ravb_wol_setup(struct net_device
*ndev
)
2235 struct ravb_private
*priv
= netdev_priv(ndev
);
2237 /* Disable interrupts by clearing the interrupt masks. */
2238 ravb_write(ndev
, 0, RIC0
);
2239 ravb_write(ndev
, 0, RIC2
);
2240 ravb_write(ndev
, 0, TIC
);
2242 /* Only allow ECI interrupts */
2243 synchronize_irq(priv
->emac_irq
);
2244 napi_disable(&priv
->napi
[RAVB_NC
]);
2245 napi_disable(&priv
->napi
[RAVB_BE
]);
2246 ravb_write(ndev
, ECSIPR_MPDIP
, ECSIPR
);
2248 /* Enable MagicPacket */
2249 ravb_modify(ndev
, ECMR
, ECMR_MPDE
, ECMR_MPDE
);
2251 return enable_irq_wake(priv
->emac_irq
);
2254 static int ravb_wol_restore(struct net_device
*ndev
)
2256 struct ravb_private
*priv
= netdev_priv(ndev
);
2259 napi_enable(&priv
->napi
[RAVB_NC
]);
2260 napi_enable(&priv
->napi
[RAVB_BE
]);
2262 /* Disable MagicPacket */
2263 ravb_modify(ndev
, ECMR
, ECMR_MPDE
, 0);
2265 ret
= ravb_close(ndev
);
2269 return disable_irq_wake(priv
->emac_irq
);
2272 static int __maybe_unused
ravb_suspend(struct device
*dev
)
2274 struct net_device
*ndev
= dev_get_drvdata(dev
);
2275 struct ravb_private
*priv
= netdev_priv(ndev
);
2278 if (!netif_running(ndev
))
2281 netif_device_detach(ndev
);
2283 if (priv
->wol_enabled
)
2284 ret
= ravb_wol_setup(ndev
);
2286 ret
= ravb_close(ndev
);
2291 static int __maybe_unused
ravb_resume(struct device
*dev
)
2293 struct net_device
*ndev
= dev_get_drvdata(dev
);
2294 struct ravb_private
*priv
= netdev_priv(ndev
);
2297 /* If WoL is enabled set reset mode to rearm the WoL logic */
2298 if (priv
->wol_enabled
)
2299 ravb_write(ndev
, CCC_OPC_RESET
, CCC
);
2301 /* All register have been reset to default values.
2302 * Restore all registers which where setup at probe time and
2303 * reopen device if it was running before system suspended.
2306 /* Set AVB config mode */
2307 ravb_set_config_mode(ndev
);
2310 ret
= ravb_set_gti(ndev
);
2314 /* Request GTI loading */
2315 ravb_modify(ndev
, GCCR
, GCCR_LTI
, GCCR_LTI
);
2317 if (priv
->chip_id
!= RCAR_GEN2
)
2318 ravb_set_delay_mode(ndev
);
2320 /* Restore descriptor base address table */
2321 ravb_write(ndev
, priv
->desc_bat_dma
, DBAT
);
2323 if (netif_running(ndev
)) {
2324 if (priv
->wol_enabled
) {
2325 ret
= ravb_wol_restore(ndev
);
2329 ret
= ravb_open(ndev
);
2332 netif_device_attach(ndev
);
2338 static int __maybe_unused
ravb_runtime_nop(struct device
*dev
)
2340 /* Runtime PM callback shared between ->runtime_suspend()
2341 * and ->runtime_resume(). Simply returns success.
2343 * This driver re-initializes all registers after
2344 * pm_runtime_get_sync() anyway so there is no need
2345 * to save and restore registers here.
2350 static const struct dev_pm_ops ravb_dev_pm_ops
= {
2351 SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend
, ravb_resume
)
2352 SET_RUNTIME_PM_OPS(ravb_runtime_nop
, ravb_runtime_nop
, NULL
)
2355 static struct platform_driver ravb_driver
= {
2356 .probe
= ravb_probe
,
2357 .remove
= ravb_remove
,
2360 .pm
= &ravb_dev_pm_ops
,
2361 .of_match_table
= ravb_match_table
,
2365 module_platform_driver(ravb_driver
);
2367 MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2368 MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2369 MODULE_LICENSE("GPL v2");