1 // SPDX-License-Identifier: GPL-2.0
2 /* Renesas Ethernet Switch device driver
4 * Copyright (C) 2022 Renesas Electronics Corporation
8 #include <linux/dma-mapping.h>
10 #include <linux/etherdevice.h>
11 #include <linux/iopoll.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/net_tstamp.h>
16 #include <linux/of_mdio.h>
17 #include <linux/of_net.h>
18 #include <linux/phy/phy.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/sys_soc.h>
28 static int rswitch_reg_wait(void __iomem *addr, u32 offs, u32 mask, u32 expected)
32 return readl_poll_timeout_atomic(addr + offs, val, (val & mask) == expected,
33 1, RSWITCH_TIMEOUT_US);
36 static void rswitch_modify(void __iomem *addr, enum rswitch_reg reg, u32 clear, u32 set)
38 iowrite32((ioread32(addr + reg) & ~clear) | set, addr + reg);
41 /* Common Agent block (COMA) */
42 static void rswitch_reset(struct rswitch_private *priv)
44 iowrite32(RRC_RR, priv->addr + RRC);
45 iowrite32(RRC_RR_CLR, priv->addr + RRC);
48 static void rswitch_clock_enable(struct rswitch_private *priv)
50 iowrite32(RCEC_ACE_DEFAULT | RCEC_RCE, priv->addr + RCEC);
53 static void rswitch_clock_disable(struct rswitch_private *priv)
55 iowrite32(RCDC_RCD, priv->addr + RCDC);
58 static bool rswitch_agent_clock_is_enabled(void __iomem *coma_addr, int port)
60 u32 val = ioread32(coma_addr + RCEC);
63 return (val & BIT(port)) ? true : false;
68 static void rswitch_agent_clock_ctrl(void __iomem *coma_addr, int port, int enable)
73 val = ioread32(coma_addr + RCEC);
74 iowrite32(val | RCEC_RCE | BIT(port), coma_addr + RCEC);
76 val = ioread32(coma_addr + RCDC);
77 iowrite32(val | BIT(port), coma_addr + RCDC);
81 static int rswitch_bpool_config(struct rswitch_private *priv)
85 val = ioread32(priv->addr + CABPIRM);
86 if (val & CABPIRM_BPR)
89 iowrite32(CABPIRM_BPIOG, priv->addr + CABPIRM);
91 return rswitch_reg_wait(priv->addr, CABPIRM, CABPIRM_BPR, CABPIRM_BPR);
94 static void rswitch_coma_init(struct rswitch_private *priv)
96 iowrite32(CABPPFLC_INIT_VALUE, priv->addr + CABPPFLC0);
99 /* R-Switch-2 block (TOP) */
100 static void rswitch_top_init(struct rswitch_private *priv)
104 for (i = 0; i < RSWITCH_MAX_NUM_QUEUES; i++)
105 iowrite32((i / 16) << (GWCA_INDEX * 8), priv->addr + TPEMIMC7(i));
108 /* Forwarding engine block (MFWD) */
109 static void rswitch_fwd_init(struct rswitch_private *priv)
114 for (i = 0; i < RSWITCH_NUM_PORTS; i++) {
115 iowrite32(FWPC0_DEFAULT, priv->addr + FWPC0(i));
116 iowrite32(0, priv->addr + FWPBFC(i));
119 for (i = 0; i < RSWITCH_NUM_PORTS; i++) {
120 iowrite32(priv->rdev[i]->rx_queue->index,
121 priv->addr + FWPBFCSDC(GWCA_INDEX, i));
122 iowrite32(BIT(priv->gwca.index), priv->addr + FWPBFC(i));
126 iowrite32(FWPC0_DEFAULT, priv->addr + FWPC0(priv->gwca.index));
127 iowrite32(FWPC1_DDE, priv->addr + FWPC1(priv->gwca.index));
128 iowrite32(0, priv->addr + FWPBFC(priv->gwca.index));
129 iowrite32(GENMASK(RSWITCH_NUM_PORTS - 1, 0), priv->addr + FWPBFC(priv->gwca.index));
132 /* Gateway CPU agent block (GWCA) */
133 static int rswitch_gwca_change_mode(struct rswitch_private *priv,
134 enum rswitch_gwca_mode mode)
138 if (!rswitch_agent_clock_is_enabled(priv->addr, priv->gwca.index))
139 rswitch_agent_clock_ctrl(priv->addr, priv->gwca.index, 1);
141 iowrite32(mode, priv->addr + GWMC);
143 ret = rswitch_reg_wait(priv->addr, GWMS, GWMS_OPS_MASK, mode);
145 if (mode == GWMC_OPC_DISABLE)
146 rswitch_agent_clock_ctrl(priv->addr, priv->gwca.index, 0);
151 static int rswitch_gwca_mcast_table_reset(struct rswitch_private *priv)
153 iowrite32(GWMTIRM_MTIOG, priv->addr + GWMTIRM);
155 return rswitch_reg_wait(priv->addr, GWMTIRM, GWMTIRM_MTR, GWMTIRM_MTR);
158 static int rswitch_gwca_axi_ram_reset(struct rswitch_private *priv)
160 iowrite32(GWARIRM_ARIOG, priv->addr + GWARIRM);
162 return rswitch_reg_wait(priv->addr, GWARIRM, GWARIRM_ARR, GWARIRM_ARR);
165 static bool rswitch_is_any_data_irq(struct rswitch_private *priv, u32 *dis, bool tx)
167 u32 *mask = tx ? priv->gwca.tx_irq_bits : priv->gwca.rx_irq_bits;
170 for (i = 0; i < RSWITCH_NUM_IRQ_REGS; i++) {
171 if (dis[i] & mask[i])
178 static void rswitch_get_data_irq_status(struct rswitch_private *priv, u32 *dis)
182 for (i = 0; i < RSWITCH_NUM_IRQ_REGS; i++) {
183 dis[i] = ioread32(priv->addr + GWDIS(i));
184 dis[i] &= ioread32(priv->addr + GWDIE(i));
188 static void rswitch_enadis_data_irq(struct rswitch_private *priv, int index, bool enable)
190 u32 offs = enable ? GWDIE(index / 32) : GWDID(index / 32);
192 iowrite32(BIT(index % 32), priv->addr + offs);
195 static void rswitch_ack_data_irq(struct rswitch_private *priv, int index)
197 u32 offs = GWDIS(index / 32);
199 iowrite32(BIT(index % 32), priv->addr + offs);
202 static int rswitch_next_queue_index(struct rswitch_gwca_queue *gq, bool cur, int num)
204 int index = cur ? gq->cur : gq->dirty;
206 if (index + num >= gq->ring_size)
207 index = (index + num) % gq->ring_size;
214 static int rswitch_get_num_cur_queues(struct rswitch_gwca_queue *gq)
216 if (gq->cur >= gq->dirty)
217 return gq->cur - gq->dirty;
219 return gq->ring_size - gq->dirty + gq->cur;
222 static bool rswitch_is_queue_rxed(struct rswitch_gwca_queue *gq)
224 struct rswitch_ext_ts_desc *desc = &gq->rx_ring[gq->dirty];
226 if ((desc->desc.die_dt & DT_MASK) != DT_FEMPTY)
232 static int rswitch_gwca_queue_alloc_skb(struct rswitch_gwca_queue *gq,
233 int start_index, int num)
237 for (i = 0; i < num; i++) {
238 index = (i + start_index) % gq->ring_size;
241 gq->skbs[index] = netdev_alloc_skb_ip_align(gq->ndev,
242 PKT_BUF_SZ + RSWITCH_ALIGN - 1);
243 if (!gq->skbs[index])
250 for (i--; i >= 0; i--) {
251 index = (i + start_index) % gq->ring_size;
252 dev_kfree_skb(gq->skbs[index]);
253 gq->skbs[index] = NULL;
259 static void rswitch_gwca_queue_free(struct net_device *ndev,
260 struct rswitch_gwca_queue *gq)
265 dma_free_coherent(ndev->dev.parent,
266 sizeof(struct rswitch_ext_ts_desc) *
267 (gq->ring_size + 1), gq->rx_ring, gq->ring_dma);
270 for (i = 0; i < gq->ring_size; i++)
271 dev_kfree_skb(gq->skbs[i]);
273 dma_free_coherent(ndev->dev.parent,
274 sizeof(struct rswitch_ext_desc) *
275 (gq->ring_size + 1), gq->tx_ring, gq->ring_dma);
283 static void rswitch_gwca_ts_queue_free(struct rswitch_private *priv)
285 struct rswitch_gwca_queue *gq = &priv->gwca.ts_queue;
287 dma_free_coherent(&priv->pdev->dev,
288 sizeof(struct rswitch_ts_desc) * (gq->ring_size + 1),
289 gq->ts_ring, gq->ring_dma);
293 static int rswitch_gwca_queue_alloc(struct net_device *ndev,
294 struct rswitch_private *priv,
295 struct rswitch_gwca_queue *gq,
296 bool dir_tx, int ring_size)
301 gq->ring_size = ring_size;
304 gq->skbs = kcalloc(gq->ring_size, sizeof(*gq->skbs), GFP_KERNEL);
309 rswitch_gwca_queue_alloc_skb(gq, 0, gq->ring_size);
311 gq->rx_ring = dma_alloc_coherent(ndev->dev.parent,
312 sizeof(struct rswitch_ext_ts_desc) *
313 (gq->ring_size + 1), &gq->ring_dma, GFP_KERNEL);
315 gq->tx_ring = dma_alloc_coherent(ndev->dev.parent,
316 sizeof(struct rswitch_ext_desc) *
317 (gq->ring_size + 1), &gq->ring_dma, GFP_KERNEL);
320 if (!gq->rx_ring && !gq->tx_ring)
324 bit = BIT(gq->index % 32);
326 priv->gwca.tx_irq_bits[i] |= bit;
328 priv->gwca.rx_irq_bits[i] |= bit;
333 rswitch_gwca_queue_free(ndev, gq);
338 static void rswitch_desc_set_dptr(struct rswitch_desc *desc, dma_addr_t addr)
340 desc->dptrl = cpu_to_le32(lower_32_bits(addr));
341 desc->dptrh = upper_32_bits(addr) & 0xff;
344 static dma_addr_t rswitch_desc_get_dptr(const struct rswitch_desc *desc)
346 return __le32_to_cpu(desc->dptrl) | (u64)(desc->dptrh) << 32;
349 static int rswitch_gwca_queue_format(struct net_device *ndev,
350 struct rswitch_private *priv,
351 struct rswitch_gwca_queue *gq)
353 int ring_size = sizeof(struct rswitch_ext_desc) * gq->ring_size;
354 struct rswitch_ext_desc *desc;
355 struct rswitch_desc *linkfix;
359 memset(gq->tx_ring, 0, ring_size);
360 for (i = 0, desc = gq->tx_ring; i < gq->ring_size; i++, desc++) {
362 dma_addr = dma_map_single(ndev->dev.parent,
363 gq->skbs[i]->data, PKT_BUF_SZ,
365 if (dma_mapping_error(ndev->dev.parent, dma_addr))
368 desc->desc.info_ds = cpu_to_le16(PKT_BUF_SZ);
369 rswitch_desc_set_dptr(&desc->desc, dma_addr);
370 desc->desc.die_dt = DT_FEMPTY | DIE;
372 desc->desc.die_dt = DT_EEMPTY | DIE;
375 rswitch_desc_set_dptr(&desc->desc, gq->ring_dma);
376 desc->desc.die_dt = DT_LINKFIX;
378 linkfix = &priv->gwca.linkfix_table[gq->index];
379 linkfix->die_dt = DT_LINKFIX;
380 rswitch_desc_set_dptr(linkfix, gq->ring_dma);
382 iowrite32(GWDCC_BALR | (gq->dir_tx ? GWDCC_DCP(GWCA_IPV_NUM) | GWDCC_DQT : 0) | GWDCC_EDE,
383 priv->addr + GWDCC_OFFS(gq->index));
389 for (i--, desc = gq->tx_ring; i >= 0; i--, desc++) {
390 dma_addr = rswitch_desc_get_dptr(&desc->desc);
391 dma_unmap_single(ndev->dev.parent, dma_addr, PKT_BUF_SZ,
399 static void rswitch_gwca_ts_queue_fill(struct rswitch_private *priv,
400 int start_index, int num)
402 struct rswitch_gwca_queue *gq = &priv->gwca.ts_queue;
403 struct rswitch_ts_desc *desc;
406 for (i = 0; i < num; i++) {
407 index = (i + start_index) % gq->ring_size;
408 desc = &gq->ts_ring[index];
409 desc->desc.die_dt = DT_FEMPTY_ND | DIE;
413 static int rswitch_gwca_queue_ext_ts_fill(struct net_device *ndev,
414 struct rswitch_gwca_queue *gq,
415 int start_index, int num)
417 struct rswitch_device *rdev = netdev_priv(ndev);
418 struct rswitch_ext_ts_desc *desc;
422 for (i = 0; i < num; i++) {
423 index = (i + start_index) % gq->ring_size;
424 desc = &gq->rx_ring[index];
426 dma_addr = dma_map_single(ndev->dev.parent,
427 gq->skbs[index]->data, PKT_BUF_SZ,
429 if (dma_mapping_error(ndev->dev.parent, dma_addr))
432 desc->desc.info_ds = cpu_to_le16(PKT_BUF_SZ);
433 rswitch_desc_set_dptr(&desc->desc, dma_addr);
435 desc->desc.die_dt = DT_FEMPTY | DIE;
436 desc->info1 = cpu_to_le64(INFO1_SPN(rdev->etha->index));
438 desc->desc.die_dt = DT_EEMPTY | DIE;
446 for (i--; i >= 0; i--) {
447 index = (i + start_index) % gq->ring_size;
448 desc = &gq->rx_ring[index];
449 dma_addr = rswitch_desc_get_dptr(&desc->desc);
450 dma_unmap_single(ndev->dev.parent, dma_addr, PKT_BUF_SZ,
458 static int rswitch_gwca_queue_ext_ts_format(struct net_device *ndev,
459 struct rswitch_private *priv,
460 struct rswitch_gwca_queue *gq)
462 int ring_size = sizeof(struct rswitch_ext_ts_desc) * gq->ring_size;
463 struct rswitch_ext_ts_desc *desc;
464 struct rswitch_desc *linkfix;
467 memset(gq->rx_ring, 0, ring_size);
468 err = rswitch_gwca_queue_ext_ts_fill(ndev, gq, 0, gq->ring_size);
472 desc = &gq->rx_ring[gq->ring_size]; /* Last */
473 rswitch_desc_set_dptr(&desc->desc, gq->ring_dma);
474 desc->desc.die_dt = DT_LINKFIX;
476 linkfix = &priv->gwca.linkfix_table[gq->index];
477 linkfix->die_dt = DT_LINKFIX;
478 rswitch_desc_set_dptr(linkfix, gq->ring_dma);
480 iowrite32(GWDCC_BALR | (gq->dir_tx ? GWDCC_DCP(GWCA_IPV_NUM) | GWDCC_DQT : 0) |
481 GWDCC_ETS | GWDCC_EDE,
482 priv->addr + GWDCC_OFFS(gq->index));
487 static int rswitch_gwca_linkfix_alloc(struct rswitch_private *priv)
489 int i, num_queues = priv->gwca.num_queues;
490 struct rswitch_gwca *gwca = &priv->gwca;
491 struct device *dev = &priv->pdev->dev;
493 gwca->linkfix_table_size = sizeof(struct rswitch_desc) * num_queues;
494 gwca->linkfix_table = dma_alloc_coherent(dev, gwca->linkfix_table_size,
495 &gwca->linkfix_table_dma, GFP_KERNEL);
496 if (!gwca->linkfix_table)
498 for (i = 0; i < num_queues; i++)
499 gwca->linkfix_table[i].die_dt = DT_EOS;
504 static void rswitch_gwca_linkfix_free(struct rswitch_private *priv)
506 struct rswitch_gwca *gwca = &priv->gwca;
508 if (gwca->linkfix_table)
509 dma_free_coherent(&priv->pdev->dev, gwca->linkfix_table_size,
510 gwca->linkfix_table, gwca->linkfix_table_dma);
511 gwca->linkfix_table = NULL;
514 static int rswitch_gwca_ts_queue_alloc(struct rswitch_private *priv)
516 struct rswitch_gwca_queue *gq = &priv->gwca.ts_queue;
517 struct rswitch_ts_desc *desc;
519 gq->ring_size = TS_RING_SIZE;
520 gq->ts_ring = dma_alloc_coherent(&priv->pdev->dev,
521 sizeof(struct rswitch_ts_desc) *
522 (gq->ring_size + 1), &gq->ring_dma, GFP_KERNEL);
527 rswitch_gwca_ts_queue_fill(priv, 0, TS_RING_SIZE);
528 desc = &gq->ts_ring[gq->ring_size];
529 desc->desc.die_dt = DT_LINKFIX;
530 rswitch_desc_set_dptr(&desc->desc, gq->ring_dma);
531 INIT_LIST_HEAD(&priv->gwca.ts_info_list);
536 static struct rswitch_gwca_queue *rswitch_gwca_get(struct rswitch_private *priv)
538 struct rswitch_gwca_queue *gq;
541 index = find_first_zero_bit(priv->gwca.used, priv->gwca.num_queues);
542 if (index >= priv->gwca.num_queues)
544 set_bit(index, priv->gwca.used);
545 gq = &priv->gwca.queues[index];
546 memset(gq, 0, sizeof(*gq));
552 static void rswitch_gwca_put(struct rswitch_private *priv,
553 struct rswitch_gwca_queue *gq)
555 clear_bit(gq->index, priv->gwca.used);
558 static int rswitch_txdmac_alloc(struct net_device *ndev)
560 struct rswitch_device *rdev = netdev_priv(ndev);
561 struct rswitch_private *priv = rdev->priv;
564 rdev->tx_queue = rswitch_gwca_get(priv);
568 err = rswitch_gwca_queue_alloc(ndev, priv, rdev->tx_queue, true, TX_RING_SIZE);
570 rswitch_gwca_put(priv, rdev->tx_queue);
577 static void rswitch_txdmac_free(struct net_device *ndev)
579 struct rswitch_device *rdev = netdev_priv(ndev);
581 rswitch_gwca_queue_free(ndev, rdev->tx_queue);
582 rswitch_gwca_put(rdev->priv, rdev->tx_queue);
585 static int rswitch_txdmac_init(struct rswitch_private *priv, int index)
587 struct rswitch_device *rdev = priv->rdev[index];
589 return rswitch_gwca_queue_format(rdev->ndev, priv, rdev->tx_queue);
592 static int rswitch_rxdmac_alloc(struct net_device *ndev)
594 struct rswitch_device *rdev = netdev_priv(ndev);
595 struct rswitch_private *priv = rdev->priv;
598 rdev->rx_queue = rswitch_gwca_get(priv);
602 err = rswitch_gwca_queue_alloc(ndev, priv, rdev->rx_queue, false, RX_RING_SIZE);
604 rswitch_gwca_put(priv, rdev->rx_queue);
611 static void rswitch_rxdmac_free(struct net_device *ndev)
613 struct rswitch_device *rdev = netdev_priv(ndev);
615 rswitch_gwca_queue_free(ndev, rdev->rx_queue);
616 rswitch_gwca_put(rdev->priv, rdev->rx_queue);
619 static int rswitch_rxdmac_init(struct rswitch_private *priv, int index)
621 struct rswitch_device *rdev = priv->rdev[index];
622 struct net_device *ndev = rdev->ndev;
624 return rswitch_gwca_queue_ext_ts_format(ndev, priv, rdev->rx_queue);
627 static int rswitch_gwca_hw_init(struct rswitch_private *priv)
631 err = rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE);
634 err = rswitch_gwca_change_mode(priv, GWMC_OPC_CONFIG);
638 err = rswitch_gwca_mcast_table_reset(priv);
641 err = rswitch_gwca_axi_ram_reset(priv);
645 iowrite32(GWVCC_VEM_SC_TAG, priv->addr + GWVCC);
646 iowrite32(0, priv->addr + GWTTFC);
647 iowrite32(lower_32_bits(priv->gwca.linkfix_table_dma), priv->addr + GWDCBAC1);
648 iowrite32(upper_32_bits(priv->gwca.linkfix_table_dma), priv->addr + GWDCBAC0);
649 iowrite32(lower_32_bits(priv->gwca.ts_queue.ring_dma), priv->addr + GWTDCAC10);
650 iowrite32(upper_32_bits(priv->gwca.ts_queue.ring_dma), priv->addr + GWTDCAC00);
651 iowrite32(GWCA_TS_IRQ_BIT, priv->addr + GWTSDCC0);
653 iowrite32(GWTPC_PPPL(GWCA_IPV_NUM), priv->addr + GWTPC0);
655 for (i = 0; i < RSWITCH_NUM_PORTS; i++) {
656 err = rswitch_rxdmac_init(priv, i);
659 err = rswitch_txdmac_init(priv, i);
664 err = rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE);
667 return rswitch_gwca_change_mode(priv, GWMC_OPC_OPERATION);
670 static int rswitch_gwca_hw_deinit(struct rswitch_private *priv)
674 err = rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE);
677 err = rswitch_gwca_change_mode(priv, GWMC_OPC_RESET);
681 return rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE);
684 static int rswitch_gwca_halt(struct rswitch_private *priv)
688 priv->gwca_halt = true;
689 err = rswitch_gwca_hw_deinit(priv);
690 dev_err(&priv->pdev->dev, "halted (%d)\n", err);
695 static bool rswitch_rx(struct net_device *ndev, int *quota)
697 struct rswitch_device *rdev = netdev_priv(ndev);
698 struct rswitch_gwca_queue *gq = rdev->rx_queue;
699 struct rswitch_ext_ts_desc *desc;
700 int limit, boguscnt, num, ret;
709 boguscnt = min_t(int, gq->ring_size, *quota);
712 desc = &gq->rx_ring[gq->cur];
713 while ((desc->desc.die_dt & DT_MASK) != DT_FEMPTY) {
715 pkt_len = le16_to_cpu(desc->desc.info_ds) & RX_DS;
716 skb = gq->skbs[gq->cur];
717 gq->skbs[gq->cur] = NULL;
718 dma_addr = rswitch_desc_get_dptr(&desc->desc);
719 dma_unmap_single(ndev->dev.parent, dma_addr, PKT_BUF_SZ, DMA_FROM_DEVICE);
720 get_ts = rdev->priv->ptp_priv->tstamp_rx_ctrl & RCAR_GEN4_RXTSTAMP_TYPE_V2_L2_EVENT;
722 struct skb_shared_hwtstamps *shhwtstamps;
723 struct timespec64 ts;
725 shhwtstamps = skb_hwtstamps(skb);
726 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
727 ts.tv_sec = __le32_to_cpu(desc->ts_sec);
728 ts.tv_nsec = __le32_to_cpu(desc->ts_nsec & cpu_to_le32(0x3fffffff));
729 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
731 skb_put(skb, pkt_len);
732 skb->protocol = eth_type_trans(skb, ndev);
733 napi_gro_receive(&rdev->napi, skb);
734 rdev->ndev->stats.rx_packets++;
735 rdev->ndev->stats.rx_bytes += pkt_len;
737 gq->cur = rswitch_next_queue_index(gq, true, 1);
738 desc = &gq->rx_ring[gq->cur];
744 num = rswitch_get_num_cur_queues(gq);
745 ret = rswitch_gwca_queue_alloc_skb(gq, gq->dirty, num);
748 ret = rswitch_gwca_queue_ext_ts_fill(ndev, gq, gq->dirty, num);
751 gq->dirty = rswitch_next_queue_index(gq, false, num);
753 *quota -= limit - boguscnt;
755 return boguscnt <= 0;
758 rswitch_gwca_halt(rdev->priv);
763 static int rswitch_tx_free(struct net_device *ndev, bool free_txed_only)
765 struct rswitch_device *rdev = netdev_priv(ndev);
766 struct rswitch_gwca_queue *gq = rdev->tx_queue;
767 struct rswitch_ext_desc *desc;
773 for (; rswitch_get_num_cur_queues(gq) > 0;
774 gq->dirty = rswitch_next_queue_index(gq, false, 1)) {
775 desc = &gq->tx_ring[gq->dirty];
776 if (free_txed_only && (desc->desc.die_dt & DT_MASK) != DT_FEMPTY)
780 size = le16_to_cpu(desc->desc.info_ds) & TX_DS;
781 skb = gq->skbs[gq->dirty];
783 dma_addr = rswitch_desc_get_dptr(&desc->desc);
784 dma_unmap_single(ndev->dev.parent, dma_addr,
785 size, DMA_TO_DEVICE);
786 dev_kfree_skb_any(gq->skbs[gq->dirty]);
787 gq->skbs[gq->dirty] = NULL;
790 desc->desc.die_dt = DT_EEMPTY;
791 rdev->ndev->stats.tx_packets++;
792 rdev->ndev->stats.tx_bytes += size;
798 static int rswitch_poll(struct napi_struct *napi, int budget)
800 struct net_device *ndev = napi->dev;
801 struct rswitch_private *priv;
802 struct rswitch_device *rdev;
806 rdev = netdev_priv(ndev);
810 rswitch_tx_free(ndev, true);
812 if (rswitch_rx(ndev, "a))
814 else if (rdev->priv->gwca_halt)
816 else if (rswitch_is_queue_rxed(rdev->rx_queue))
819 netif_wake_subqueue(ndev, 0);
821 if (napi_complete_done(napi, budget - quota)) {
822 spin_lock_irqsave(&priv->lock, flags);
823 rswitch_enadis_data_irq(priv, rdev->tx_queue->index, true);
824 rswitch_enadis_data_irq(priv, rdev->rx_queue->index, true);
825 spin_unlock_irqrestore(&priv->lock, flags);
829 return budget - quota;
837 static void rswitch_queue_interrupt(struct net_device *ndev)
839 struct rswitch_device *rdev = netdev_priv(ndev);
841 if (napi_schedule_prep(&rdev->napi)) {
842 spin_lock(&rdev->priv->lock);
843 rswitch_enadis_data_irq(rdev->priv, rdev->tx_queue->index, false);
844 rswitch_enadis_data_irq(rdev->priv, rdev->rx_queue->index, false);
845 spin_unlock(&rdev->priv->lock);
846 __napi_schedule(&rdev->napi);
850 static irqreturn_t rswitch_data_irq(struct rswitch_private *priv, u32 *dis)
852 struct rswitch_gwca_queue *gq;
855 for (i = 0; i < priv->gwca.num_queues; i++) {
856 gq = &priv->gwca.queues[i];
857 index = gq->index / 32;
858 bit = BIT(gq->index % 32);
859 if (!(dis[index] & bit))
862 rswitch_ack_data_irq(priv, gq->index);
863 rswitch_queue_interrupt(gq->ndev);
869 static irqreturn_t rswitch_gwca_irq(int irq, void *dev_id)
871 struct rswitch_private *priv = dev_id;
872 u32 dis[RSWITCH_NUM_IRQ_REGS];
873 irqreturn_t ret = IRQ_NONE;
875 rswitch_get_data_irq_status(priv, dis);
877 if (rswitch_is_any_data_irq(priv, dis, true) ||
878 rswitch_is_any_data_irq(priv, dis, false))
879 ret = rswitch_data_irq(priv, dis);
884 static int rswitch_gwca_request_irqs(struct rswitch_private *priv)
886 char *resource_name, *irq_name;
889 for (i = 0; i < GWCA_NUM_IRQS; i++) {
890 resource_name = kasprintf(GFP_KERNEL, GWCA_IRQ_RESOURCE_NAME, i);
894 irq = platform_get_irq_byname(priv->pdev, resource_name);
895 kfree(resource_name);
899 irq_name = devm_kasprintf(&priv->pdev->dev, GFP_KERNEL,
904 ret = devm_request_irq(&priv->pdev->dev, irq, rswitch_gwca_irq,
913 static void rswitch_ts(struct rswitch_private *priv)
915 struct rswitch_gwca_queue *gq = &priv->gwca.ts_queue;
916 struct rswitch_gwca_ts_info *ts_info, *ts_info2;
917 struct skb_shared_hwtstamps shhwtstamps;
918 struct rswitch_ts_desc *desc;
919 struct timespec64 ts;
923 desc = &gq->ts_ring[gq->cur];
924 while ((desc->desc.die_dt & DT_MASK) != DT_FEMPTY_ND) {
927 port = TS_DESC_DPN(__le32_to_cpu(desc->desc.dptrl));
928 tag = TS_DESC_TSUN(__le32_to_cpu(desc->desc.dptrl));
930 list_for_each_entry_safe(ts_info, ts_info2, &priv->gwca.ts_info_list, list) {
931 if (!(ts_info->port == port && ts_info->tag == tag))
934 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
935 ts.tv_sec = __le32_to_cpu(desc->ts_sec);
936 ts.tv_nsec = __le32_to_cpu(desc->ts_nsec & cpu_to_le32(0x3fffffff));
937 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
938 skb_tstamp_tx(ts_info->skb, &shhwtstamps);
939 dev_consume_skb_irq(ts_info->skb);
940 list_del(&ts_info->list);
945 gq->cur = rswitch_next_queue_index(gq, true, 1);
946 desc = &gq->ts_ring[gq->cur];
949 num = rswitch_get_num_cur_queues(gq);
950 rswitch_gwca_ts_queue_fill(priv, gq->dirty, num);
951 gq->dirty = rswitch_next_queue_index(gq, false, num);
954 static irqreturn_t rswitch_gwca_ts_irq(int irq, void *dev_id)
956 struct rswitch_private *priv = dev_id;
958 if (ioread32(priv->addr + GWTSDIS) & GWCA_TS_IRQ_BIT) {
959 iowrite32(GWCA_TS_IRQ_BIT, priv->addr + GWTSDIS);
968 static int rswitch_gwca_ts_request_irqs(struct rswitch_private *priv)
972 irq = platform_get_irq_byname(priv->pdev, GWCA_TS_IRQ_RESOURCE_NAME);
976 return devm_request_irq(&priv->pdev->dev, irq, rswitch_gwca_ts_irq,
977 0, GWCA_TS_IRQ_NAME, priv);
980 /* Ethernet TSN Agent block (ETHA) and Ethernet MAC IP block (RMAC) */
981 static int rswitch_etha_change_mode(struct rswitch_etha *etha,
982 enum rswitch_etha_mode mode)
986 if (!rswitch_agent_clock_is_enabled(etha->coma_addr, etha->index))
987 rswitch_agent_clock_ctrl(etha->coma_addr, etha->index, 1);
989 iowrite32(mode, etha->addr + EAMC);
991 ret = rswitch_reg_wait(etha->addr, EAMS, EAMS_OPS_MASK, mode);
993 if (mode == EAMC_OPC_DISABLE)
994 rswitch_agent_clock_ctrl(etha->coma_addr, etha->index, 0);
999 static void rswitch_etha_read_mac_address(struct rswitch_etha *etha)
1001 u32 mrmac0 = ioread32(etha->addr + MRMAC0);
1002 u32 mrmac1 = ioread32(etha->addr + MRMAC1);
1003 u8 *mac = ða->mac_addr[0];
1005 mac[0] = (mrmac0 >> 8) & 0xFF;
1006 mac[1] = (mrmac0 >> 0) & 0xFF;
1007 mac[2] = (mrmac1 >> 24) & 0xFF;
1008 mac[3] = (mrmac1 >> 16) & 0xFF;
1009 mac[4] = (mrmac1 >> 8) & 0xFF;
1010 mac[5] = (mrmac1 >> 0) & 0xFF;
1013 static void rswitch_etha_write_mac_address(struct rswitch_etha *etha, const u8 *mac)
1015 iowrite32((mac[0] << 8) | mac[1], etha->addr + MRMAC0);
1016 iowrite32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
1017 etha->addr + MRMAC1);
1020 static int rswitch_etha_wait_link_verification(struct rswitch_etha *etha)
1022 iowrite32(MLVC_PLV, etha->addr + MLVC);
1024 return rswitch_reg_wait(etha->addr, MLVC, MLVC_PLV, 0);
1027 static void rswitch_rmac_setting(struct rswitch_etha *etha, const u8 *mac)
1031 rswitch_etha_write_mac_address(etha, mac);
1033 switch (etha->speed) {
1035 val = MPIC_LSC_100M;
1041 val = MPIC_LSC_2_5G;
1047 iowrite32(MPIC_PIS_GMII | val, etha->addr + MPIC);
1050 static void rswitch_etha_enable_mii(struct rswitch_etha *etha)
1052 rswitch_modify(etha->addr, MPIC, MPIC_PSMCS_MASK | MPIC_PSMHT_MASK,
1053 MPIC_PSMCS(etha->psmcs) | MPIC_PSMHT(0x06));
1054 rswitch_modify(etha->addr, MPSM, 0, MPSM_MFF_C45);
1057 static int rswitch_etha_hw_init(struct rswitch_etha *etha, const u8 *mac)
1061 err = rswitch_etha_change_mode(etha, EAMC_OPC_DISABLE);
1064 err = rswitch_etha_change_mode(etha, EAMC_OPC_CONFIG);
1068 iowrite32(EAVCC_VEM_SC_TAG, etha->addr + EAVCC);
1069 rswitch_rmac_setting(etha, mac);
1070 rswitch_etha_enable_mii(etha);
1072 err = rswitch_etha_wait_link_verification(etha);
1076 err = rswitch_etha_change_mode(etha, EAMC_OPC_DISABLE);
1080 return rswitch_etha_change_mode(etha, EAMC_OPC_OPERATION);
1083 static int rswitch_etha_set_access(struct rswitch_etha *etha, bool read,
1084 int phyad, int devad, int regad, int data)
1086 int pop = read ? MDIO_READ_C45 : MDIO_WRITE_C45;
1090 if (devad == 0xffffffff)
1093 writel(MMIS1_CLEAR_FLAGS, etha->addr + MMIS1);
1095 val = MPSM_PSME | MPSM_MFF_C45;
1096 iowrite32((regad << 16) | (devad << 8) | (phyad << 3) | val, etha->addr + MPSM);
1098 ret = rswitch_reg_wait(etha->addr, MMIS1, MMIS1_PAACS, MMIS1_PAACS);
1102 rswitch_modify(etha->addr, MMIS1, MMIS1_PAACS, MMIS1_PAACS);
1105 writel((pop << 13) | (devad << 8) | (phyad << 3) | val, etha->addr + MPSM);
1107 ret = rswitch_reg_wait(etha->addr, MMIS1, MMIS1_PRACS, MMIS1_PRACS);
1111 ret = (ioread32(etha->addr + MPSM) & MPSM_PRD_MASK) >> 16;
1113 rswitch_modify(etha->addr, MMIS1, MMIS1_PRACS, MMIS1_PRACS);
1115 iowrite32((data << 16) | (pop << 13) | (devad << 8) | (phyad << 3) | val,
1118 ret = rswitch_reg_wait(etha->addr, MMIS1, MMIS1_PWACS, MMIS1_PWACS);
1124 static int rswitch_etha_mii_read_c45(struct mii_bus *bus, int addr, int devad,
1127 struct rswitch_etha *etha = bus->priv;
1129 return rswitch_etha_set_access(etha, true, addr, devad, regad, 0);
1132 static int rswitch_etha_mii_write_c45(struct mii_bus *bus, int addr, int devad,
1135 struct rswitch_etha *etha = bus->priv;
1137 return rswitch_etha_set_access(etha, false, addr, devad, regad, val);
1140 /* Call of_node_put(port) after done */
1141 static struct device_node *rswitch_get_port_node(struct rswitch_device *rdev)
1143 struct device_node *ports, *port;
1147 ports = of_get_child_by_name(rdev->ndev->dev.parent->of_node,
1152 for_each_child_of_node(ports, port) {
1153 err = of_property_read_u32(port, "reg", &index);
1158 if (index == rdev->etha->index) {
1159 if (!of_device_is_available(port))
1171 static int rswitch_etha_get_params(struct rswitch_device *rdev)
1177 return 0; /* ignored */
1179 err = of_get_phy_mode(rdev->np_port, &rdev->etha->phy_interface);
1183 err = of_property_read_u32(rdev->np_port, "max-speed", &max_speed);
1185 rdev->etha->speed = max_speed;
1189 /* if no "max-speed" property, let's use default speed */
1190 switch (rdev->etha->phy_interface) {
1191 case PHY_INTERFACE_MODE_MII:
1192 rdev->etha->speed = SPEED_100;
1194 case PHY_INTERFACE_MODE_SGMII:
1195 rdev->etha->speed = SPEED_1000;
1197 case PHY_INTERFACE_MODE_USXGMII:
1198 rdev->etha->speed = SPEED_2500;
1207 static int rswitch_mii_register(struct rswitch_device *rdev)
1209 struct device_node *mdio_np;
1210 struct mii_bus *mii_bus;
1213 mii_bus = mdiobus_alloc();
1217 mii_bus->name = "rswitch_mii";
1218 sprintf(mii_bus->id, "etha%d", rdev->etha->index);
1219 mii_bus->priv = rdev->etha;
1220 mii_bus->read_c45 = rswitch_etha_mii_read_c45;
1221 mii_bus->write_c45 = rswitch_etha_mii_write_c45;
1222 mii_bus->parent = &rdev->priv->pdev->dev;
1224 mdio_np = of_get_child_by_name(rdev->np_port, "mdio");
1225 err = of_mdiobus_register(mii_bus, mdio_np);
1227 mdiobus_free(mii_bus);
1231 rdev->etha->mii = mii_bus;
1234 of_node_put(mdio_np);
1239 static void rswitch_mii_unregister(struct rswitch_device *rdev)
1241 if (rdev->etha->mii) {
1242 mdiobus_unregister(rdev->etha->mii);
1243 mdiobus_free(rdev->etha->mii);
1244 rdev->etha->mii = NULL;
1248 static void rswitch_adjust_link(struct net_device *ndev)
1250 struct rswitch_device *rdev = netdev_priv(ndev);
1251 struct phy_device *phydev = ndev->phydev;
1253 if (phydev->link != rdev->etha->link) {
1254 phy_print_status(phydev);
1256 phy_power_on(rdev->serdes);
1257 else if (rdev->serdes->power_count)
1258 phy_power_off(rdev->serdes);
1260 rdev->etha->link = phydev->link;
1262 if (!rdev->priv->etha_no_runtime_change &&
1263 phydev->speed != rdev->etha->speed) {
1264 rdev->etha->speed = phydev->speed;
1266 rswitch_etha_hw_init(rdev->etha, rdev->ndev->dev_addr);
1267 phy_set_speed(rdev->serdes, rdev->etha->speed);
1272 static void rswitch_phy_remove_link_mode(struct rswitch_device *rdev,
1273 struct phy_device *phydev)
1275 if (!rdev->priv->etha_no_runtime_change)
1278 switch (rdev->etha->speed) {
1280 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Full_BIT);
1281 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Full_BIT);
1284 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_2500baseX_Full_BIT);
1285 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Full_BIT);
1288 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_2500baseX_Full_BIT);
1289 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Full_BIT);
1295 phy_set_max_speed(phydev, rdev->etha->speed);
1298 static int rswitch_phy_device_init(struct rswitch_device *rdev)
1300 struct phy_device *phydev;
1301 struct device_node *phy;
1307 phy = of_parse_phandle(rdev->np_port, "phy-handle", 0);
1311 /* Set phydev->host_interfaces before calling of_phy_connect() to
1312 * configure the PHY with the information of host_interfaces.
1314 phydev = of_phy_find_device(phy);
1317 __set_bit(rdev->etha->phy_interface, phydev->host_interfaces);
1319 phydev = of_phy_connect(rdev->ndev, phy, rswitch_adjust_link, 0,
1320 rdev->etha->phy_interface);
1324 phy_set_max_speed(phydev, SPEED_2500);
1325 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Half_BIT);
1326 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Full_BIT);
1327 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Half_BIT);
1328 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
1329 rswitch_phy_remove_link_mode(rdev, phydev);
1331 phy_attached_info(phydev);
1340 static void rswitch_phy_device_deinit(struct rswitch_device *rdev)
1342 if (rdev->ndev->phydev)
1343 phy_disconnect(rdev->ndev->phydev);
1346 static int rswitch_serdes_set_params(struct rswitch_device *rdev)
1350 err = phy_set_mode_ext(rdev->serdes, PHY_MODE_ETHERNET,
1351 rdev->etha->phy_interface);
1355 return phy_set_speed(rdev->serdes, rdev->etha->speed);
1358 static int rswitch_ether_port_init_one(struct rswitch_device *rdev)
1362 if (!rdev->etha->operated) {
1363 err = rswitch_etha_hw_init(rdev->etha, rdev->ndev->dev_addr);
1366 if (rdev->priv->etha_no_runtime_change)
1367 rdev->etha->operated = true;
1370 err = rswitch_mii_register(rdev);
1374 err = rswitch_phy_device_init(rdev);
1376 goto err_phy_device_init;
1378 rdev->serdes = devm_of_phy_get(&rdev->priv->pdev->dev, rdev->np_port, NULL);
1379 if (IS_ERR(rdev->serdes)) {
1380 err = PTR_ERR(rdev->serdes);
1381 goto err_serdes_phy_get;
1384 err = rswitch_serdes_set_params(rdev);
1386 goto err_serdes_set_params;
1390 err_serdes_set_params:
1392 rswitch_phy_device_deinit(rdev);
1394 err_phy_device_init:
1395 rswitch_mii_unregister(rdev);
1400 static void rswitch_ether_port_deinit_one(struct rswitch_device *rdev)
1402 rswitch_phy_device_deinit(rdev);
1403 rswitch_mii_unregister(rdev);
1406 static int rswitch_ether_port_init_all(struct rswitch_private *priv)
1410 rswitch_for_each_enabled_port(priv, i) {
1411 err = rswitch_ether_port_init_one(priv->rdev[i]);
1416 rswitch_for_each_enabled_port(priv, i) {
1417 err = phy_init(priv->rdev[i]->serdes);
1425 rswitch_for_each_enabled_port_continue_reverse(priv, i)
1426 phy_exit(priv->rdev[i]->serdes);
1427 i = RSWITCH_NUM_PORTS;
1430 rswitch_for_each_enabled_port_continue_reverse(priv, i)
1431 rswitch_ether_port_deinit_one(priv->rdev[i]);
1436 static void rswitch_ether_port_deinit_all(struct rswitch_private *priv)
1440 for (i = 0; i < RSWITCH_NUM_PORTS; i++) {
1441 phy_exit(priv->rdev[i]->serdes);
1442 rswitch_ether_port_deinit_one(priv->rdev[i]);
1446 static int rswitch_open(struct net_device *ndev)
1448 struct rswitch_device *rdev = netdev_priv(ndev);
1449 unsigned long flags;
1451 phy_start(ndev->phydev);
1453 napi_enable(&rdev->napi);
1454 netif_start_queue(ndev);
1456 spin_lock_irqsave(&rdev->priv->lock, flags);
1457 rswitch_enadis_data_irq(rdev->priv, rdev->tx_queue->index, true);
1458 rswitch_enadis_data_irq(rdev->priv, rdev->rx_queue->index, true);
1459 spin_unlock_irqrestore(&rdev->priv->lock, flags);
1461 if (bitmap_empty(rdev->priv->opened_ports, RSWITCH_NUM_PORTS))
1462 iowrite32(GWCA_TS_IRQ_BIT, rdev->priv->addr + GWTSDIE);
1464 bitmap_set(rdev->priv->opened_ports, rdev->port, 1);
1469 static int rswitch_stop(struct net_device *ndev)
1471 struct rswitch_device *rdev = netdev_priv(ndev);
1472 struct rswitch_gwca_ts_info *ts_info, *ts_info2;
1473 unsigned long flags;
1475 netif_tx_stop_all_queues(ndev);
1476 bitmap_clear(rdev->priv->opened_ports, rdev->port, 1);
1478 if (bitmap_empty(rdev->priv->opened_ports, RSWITCH_NUM_PORTS))
1479 iowrite32(GWCA_TS_IRQ_BIT, rdev->priv->addr + GWTSDID);
1481 list_for_each_entry_safe(ts_info, ts_info2, &rdev->priv->gwca.ts_info_list, list) {
1482 if (ts_info->port != rdev->port)
1484 dev_kfree_skb_irq(ts_info->skb);
1485 list_del(&ts_info->list);
1489 spin_lock_irqsave(&rdev->priv->lock, flags);
1490 rswitch_enadis_data_irq(rdev->priv, rdev->tx_queue->index, false);
1491 rswitch_enadis_data_irq(rdev->priv, rdev->rx_queue->index, false);
1492 spin_unlock_irqrestore(&rdev->priv->lock, flags);
1494 phy_stop(ndev->phydev);
1495 napi_disable(&rdev->napi);
1500 static netdev_tx_t rswitch_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1502 struct rswitch_device *rdev = netdev_priv(ndev);
1503 struct rswitch_gwca_queue *gq = rdev->tx_queue;
1504 struct rswitch_ext_desc *desc;
1505 int ret = NETDEV_TX_OK;
1506 dma_addr_t dma_addr;
1508 if (rswitch_get_num_cur_queues(gq) >= gq->ring_size - 1) {
1509 netif_stop_subqueue(ndev, 0);
1510 return NETDEV_TX_BUSY;
1513 if (skb_put_padto(skb, ETH_ZLEN))
1516 dma_addr = dma_map_single(ndev->dev.parent, skb->data, skb->len, DMA_TO_DEVICE);
1517 if (dma_mapping_error(ndev->dev.parent, dma_addr)) {
1518 dev_kfree_skb_any(skb);
1522 gq->skbs[gq->cur] = skb;
1523 desc = &gq->tx_ring[gq->cur];
1524 rswitch_desc_set_dptr(&desc->desc, dma_addr);
1525 desc->desc.info_ds = cpu_to_le16(skb->len);
1527 desc->info1 = cpu_to_le64(INFO1_DV(BIT(rdev->etha->index)) |
1528 INFO1_IPV(GWCA_IPV_NUM) | INFO1_FMT);
1529 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
1530 struct rswitch_gwca_ts_info *ts_info;
1532 ts_info = kzalloc(sizeof(*ts_info), GFP_ATOMIC);
1534 dma_unmap_single(ndev->dev.parent, dma_addr, skb->len, DMA_TO_DEVICE);
1538 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1540 desc->info1 |= cpu_to_le64(INFO1_TSUN(rdev->ts_tag) | INFO1_TXC);
1542 ts_info->skb = skb_get(skb);
1543 ts_info->port = rdev->port;
1544 ts_info->tag = rdev->ts_tag;
1545 list_add_tail(&ts_info->list, &rdev->priv->gwca.ts_info_list);
1547 skb_tx_timestamp(skb);
1552 desc->desc.die_dt = DT_FSINGLE | DIE;
1553 wmb(); /* gq->cur must be incremented after die_dt was set */
1555 gq->cur = rswitch_next_queue_index(gq, true, 1);
1556 rswitch_modify(rdev->addr, GWTRC(gq->index), 0, BIT(gq->index % 32));
1561 static struct net_device_stats *rswitch_get_stats(struct net_device *ndev)
1563 return &ndev->stats;
1566 static int rswitch_hwstamp_get(struct net_device *ndev, struct ifreq *req)
1568 struct rswitch_device *rdev = netdev_priv(ndev);
1569 struct rcar_gen4_ptp_private *ptp_priv;
1570 struct hwtstamp_config config;
1572 ptp_priv = rdev->priv->ptp_priv;
1575 config.tx_type = ptp_priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1577 switch (ptp_priv->tstamp_rx_ctrl & RCAR_GEN4_RXTSTAMP_TYPE) {
1578 case RCAR_GEN4_RXTSTAMP_TYPE_V2_L2_EVENT:
1579 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1581 case RCAR_GEN4_RXTSTAMP_TYPE_ALL:
1582 config.rx_filter = HWTSTAMP_FILTER_ALL;
1585 config.rx_filter = HWTSTAMP_FILTER_NONE;
1589 return copy_to_user(req->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
1592 static int rswitch_hwstamp_set(struct net_device *ndev, struct ifreq *req)
1594 struct rswitch_device *rdev = netdev_priv(ndev);
1595 u32 tstamp_rx_ctrl = RCAR_GEN4_RXTSTAMP_ENABLED;
1596 struct hwtstamp_config config;
1599 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1605 switch (config.tx_type) {
1606 case HWTSTAMP_TX_OFF:
1609 case HWTSTAMP_TX_ON:
1610 tstamp_tx_ctrl = RCAR_GEN4_TXTSTAMP_ENABLED;
1616 switch (config.rx_filter) {
1617 case HWTSTAMP_FILTER_NONE:
1620 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1621 tstamp_rx_ctrl |= RCAR_GEN4_RXTSTAMP_TYPE_V2_L2_EVENT;
1624 config.rx_filter = HWTSTAMP_FILTER_ALL;
1625 tstamp_rx_ctrl |= RCAR_GEN4_RXTSTAMP_TYPE_ALL;
1629 rdev->priv->ptp_priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1630 rdev->priv->ptp_priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1632 return copy_to_user(req->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
1635 static int rswitch_eth_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1637 if (!netif_running(ndev))
1642 return rswitch_hwstamp_get(ndev, req);
1644 return rswitch_hwstamp_set(ndev, req);
1646 return phy_mii_ioctl(ndev->phydev, req, cmd);
1650 static const struct net_device_ops rswitch_netdev_ops = {
1651 .ndo_open = rswitch_open,
1652 .ndo_stop = rswitch_stop,
1653 .ndo_start_xmit = rswitch_start_xmit,
1654 .ndo_get_stats = rswitch_get_stats,
1655 .ndo_eth_ioctl = rswitch_eth_ioctl,
1656 .ndo_validate_addr = eth_validate_addr,
1657 .ndo_set_mac_address = eth_mac_addr,
1660 static int rswitch_get_ts_info(struct net_device *ndev, struct ethtool_ts_info *info)
1662 struct rswitch_device *rdev = netdev_priv(ndev);
1664 info->phc_index = ptp_clock_index(rdev->priv->ptp_priv->clock);
1665 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1666 SOF_TIMESTAMPING_RX_SOFTWARE |
1667 SOF_TIMESTAMPING_SOFTWARE |
1668 SOF_TIMESTAMPING_TX_HARDWARE |
1669 SOF_TIMESTAMPING_RX_HARDWARE |
1670 SOF_TIMESTAMPING_RAW_HARDWARE;
1671 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
1672 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
1677 static const struct ethtool_ops rswitch_ethtool_ops = {
1678 .get_ts_info = rswitch_get_ts_info,
1679 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1680 .set_link_ksettings = phy_ethtool_set_link_ksettings,
1683 static const struct of_device_id renesas_eth_sw_of_table[] = {
1684 { .compatible = "renesas,r8a779f0-ether-switch", },
1687 MODULE_DEVICE_TABLE(of, renesas_eth_sw_of_table);
1689 static void rswitch_etha_init(struct rswitch_private *priv, int index)
1691 struct rswitch_etha *etha = &priv->etha[index];
1693 memset(etha, 0, sizeof(*etha));
1694 etha->index = index;
1695 etha->addr = priv->addr + RSWITCH_ETHA_OFFSET + index * RSWITCH_ETHA_SIZE;
1696 etha->coma_addr = priv->addr;
1698 /* MPIC.PSMCS = (clk [MHz] / (MDC frequency [MHz] * 2) - 1.
1699 * Calculating PSMCS value as MDC frequency = 2.5MHz. So, multiply
1700 * both the numerator and the denominator by 10.
1702 etha->psmcs = clk_get_rate(priv->clk) / 100000 / (25 * 2) - 1;
1705 static int rswitch_device_alloc(struct rswitch_private *priv, int index)
1707 struct platform_device *pdev = priv->pdev;
1708 struct rswitch_device *rdev;
1709 struct net_device *ndev;
1712 if (index >= RSWITCH_NUM_PORTS)
1715 ndev = alloc_etherdev_mqs(sizeof(struct rswitch_device), 1, 1);
1719 SET_NETDEV_DEV(ndev, &pdev->dev);
1722 rdev = netdev_priv(ndev);
1725 priv->rdev[index] = rdev;
1727 rdev->etha = &priv->etha[index];
1728 rdev->addr = priv->addr;
1730 ndev->base_addr = (unsigned long)rdev->addr;
1731 snprintf(ndev->name, IFNAMSIZ, "tsn%d", index);
1732 ndev->netdev_ops = &rswitch_netdev_ops;
1733 ndev->ethtool_ops = &rswitch_ethtool_ops;
1735 netif_napi_add(ndev, &rdev->napi, rswitch_poll);
1737 rdev->np_port = rswitch_get_port_node(rdev);
1738 rdev->disabled = !rdev->np_port;
1739 err = of_get_ethdev_address(rdev->np_port, ndev);
1740 of_node_put(rdev->np_port);
1742 if (is_valid_ether_addr(rdev->etha->mac_addr))
1743 eth_hw_addr_set(ndev, rdev->etha->mac_addr);
1745 eth_hw_addr_random(ndev);
1748 err = rswitch_etha_get_params(rdev);
1750 goto out_get_params;
1752 if (rdev->priv->gwca.speed < rdev->etha->speed)
1753 rdev->priv->gwca.speed = rdev->etha->speed;
1755 err = rswitch_rxdmac_alloc(ndev);
1759 err = rswitch_txdmac_alloc(ndev);
1766 rswitch_rxdmac_free(ndev);
1770 netif_napi_del(&rdev->napi);
1776 static void rswitch_device_free(struct rswitch_private *priv, int index)
1778 struct rswitch_device *rdev = priv->rdev[index];
1779 struct net_device *ndev = rdev->ndev;
1781 rswitch_txdmac_free(ndev);
1782 rswitch_rxdmac_free(ndev);
1783 netif_napi_del(&rdev->napi);
1787 static int rswitch_init(struct rswitch_private *priv)
1791 for (i = 0; i < RSWITCH_NUM_PORTS; i++)
1792 rswitch_etha_init(priv, i);
1794 rswitch_clock_enable(priv);
1795 for (i = 0; i < RSWITCH_NUM_PORTS; i++)
1796 rswitch_etha_read_mac_address(&priv->etha[i]);
1798 rswitch_reset(priv);
1800 rswitch_clock_enable(priv);
1801 rswitch_top_init(priv);
1802 err = rswitch_bpool_config(priv);
1806 rswitch_coma_init(priv);
1808 err = rswitch_gwca_linkfix_alloc(priv);
1812 err = rswitch_gwca_ts_queue_alloc(priv);
1814 goto err_ts_queue_alloc;
1816 for (i = 0; i < RSWITCH_NUM_PORTS; i++) {
1817 err = rswitch_device_alloc(priv, i);
1819 for (i--; i >= 0; i--)
1820 rswitch_device_free(priv, i);
1821 goto err_device_alloc;
1825 rswitch_fwd_init(priv);
1827 err = rcar_gen4_ptp_register(priv->ptp_priv, RCAR_GEN4_PTP_REG_LAYOUT_S4,
1828 RCAR_GEN4_PTP_CLOCK_S4);
1830 goto err_ptp_register;
1832 err = rswitch_gwca_request_irqs(priv);
1834 goto err_gwca_request_irq;
1836 err = rswitch_gwca_ts_request_irqs(priv);
1838 goto err_gwca_ts_request_irq;
1840 err = rswitch_gwca_hw_init(priv);
1842 goto err_gwca_hw_init;
1844 err = rswitch_ether_port_init_all(priv);
1846 goto err_ether_port_init_all;
1848 rswitch_for_each_enabled_port(priv, i) {
1849 err = register_netdev(priv->rdev[i]->ndev);
1851 rswitch_for_each_enabled_port_continue_reverse(priv, i)
1852 unregister_netdev(priv->rdev[i]->ndev);
1853 goto err_register_netdev;
1857 rswitch_for_each_enabled_port(priv, i)
1858 netdev_info(priv->rdev[i]->ndev, "MAC address %pM\n",
1859 priv->rdev[i]->ndev->dev_addr);
1863 err_register_netdev:
1864 rswitch_ether_port_deinit_all(priv);
1866 err_ether_port_init_all:
1867 rswitch_gwca_hw_deinit(priv);
1870 err_gwca_ts_request_irq:
1871 err_gwca_request_irq:
1872 rcar_gen4_ptp_unregister(priv->ptp_priv);
1875 for (i = 0; i < RSWITCH_NUM_PORTS; i++)
1876 rswitch_device_free(priv, i);
1879 rswitch_gwca_ts_queue_free(priv);
1882 rswitch_gwca_linkfix_free(priv);
1887 static const struct soc_device_attribute rswitch_soc_no_speed_change[] = {
1888 { .soc_id = "r8a779f0", .revision = "ES1.0" },
1892 static int renesas_eth_sw_probe(struct platform_device *pdev)
1894 const struct soc_device_attribute *attr;
1895 struct rswitch_private *priv;
1896 struct resource *res;
1899 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "secure_base");
1901 dev_err(&pdev->dev, "invalid resource\n");
1905 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1908 spin_lock_init(&priv->lock);
1910 priv->clk = devm_clk_get(&pdev->dev, NULL);
1911 if (IS_ERR(priv->clk))
1912 return PTR_ERR(priv->clk);
1914 attr = soc_device_match(rswitch_soc_no_speed_change);
1916 priv->etha_no_runtime_change = true;
1918 priv->ptp_priv = rcar_gen4_ptp_alloc(pdev);
1919 if (!priv->ptp_priv)
1922 platform_set_drvdata(pdev, priv);
1924 priv->addr = devm_ioremap_resource(&pdev->dev, res);
1925 if (IS_ERR(priv->addr))
1926 return PTR_ERR(priv->addr);
1928 priv->ptp_priv->addr = priv->addr + RCAR_GEN4_GPTP_OFFSET_S4;
1930 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
1932 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1937 priv->gwca.index = AGENT_INDEX_GWCA;
1938 priv->gwca.num_queues = min(RSWITCH_NUM_PORTS * NUM_QUEUES_PER_NDEV,
1939 RSWITCH_MAX_NUM_QUEUES);
1940 priv->gwca.queues = devm_kcalloc(&pdev->dev, priv->gwca.num_queues,
1941 sizeof(*priv->gwca.queues), GFP_KERNEL);
1942 if (!priv->gwca.queues)
1945 pm_runtime_enable(&pdev->dev);
1946 pm_runtime_get_sync(&pdev->dev);
1948 ret = rswitch_init(priv);
1950 pm_runtime_put(&pdev->dev);
1951 pm_runtime_disable(&pdev->dev);
1955 device_set_wakeup_capable(&pdev->dev, 1);
1960 static void rswitch_deinit(struct rswitch_private *priv)
1964 rswitch_gwca_hw_deinit(priv);
1965 rcar_gen4_ptp_unregister(priv->ptp_priv);
1967 rswitch_for_each_enabled_port(priv, i) {
1968 struct rswitch_device *rdev = priv->rdev[i];
1970 unregister_netdev(rdev->ndev);
1971 rswitch_ether_port_deinit_one(rdev);
1972 phy_exit(priv->rdev[i]->serdes);
1975 for (i = 0; i < RSWITCH_NUM_PORTS; i++)
1976 rswitch_device_free(priv, i);
1978 rswitch_gwca_ts_queue_free(priv);
1979 rswitch_gwca_linkfix_free(priv);
1981 rswitch_clock_disable(priv);
1984 static int renesas_eth_sw_remove(struct platform_device *pdev)
1986 struct rswitch_private *priv = platform_get_drvdata(pdev);
1988 rswitch_deinit(priv);
1990 pm_runtime_put(&pdev->dev);
1991 pm_runtime_disable(&pdev->dev);
1993 platform_set_drvdata(pdev, NULL);
1998 static struct platform_driver renesas_eth_sw_driver_platform = {
1999 .probe = renesas_eth_sw_probe,
2000 .remove = renesas_eth_sw_remove,
2002 .name = "renesas_eth_sw",
2003 .of_match_table = renesas_eth_sw_of_table,
2006 module_platform_driver(renesas_eth_sw_driver_platform);
2007 MODULE_AUTHOR("Yoshihiro Shimoda");
2008 MODULE_DESCRIPTION("Renesas Ethernet Switch device driver");
2009 MODULE_LICENSE("GPL");