Skip to content

Commit 7b42961

Browse files
Rafal Ozieblodavem330
Rafal Ozieblo
authored andcommitted
net: macb: Add support for PTP timestamps in DMA descriptors
This patch adds support for PTP timestamps in DMA buffer descriptors. It checks capability at runtime and uses appropriate buffer descriptor. Signed-off-by: Rafal Ozieblo <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b079115 commit 7b42961

File tree

3 files changed

+122
-37
lines changed

3 files changed

+122
-37
lines changed

drivers/net/ethernet/cadence/Kconfig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ config MACB
2929
support for the MACB/GEM chip.
3030

3131
To compile this driver as a module, choose M here: the module
32-
will be called macb.
32+
will be macb.
33+
34+
config MACB_USE_HWSTAMP
35+
bool "Use IEEE 1588 hwstamp"
36+
depends on MACB
37+
default y
38+
imply PTP_1588_CLOCK
39+
---help---
40+
Enable IEEE 1588 Precision Time Protocol (PTP) support for MACB.
3341

3442
config MACB_PCI
3543
tristate "Cadence PCI MACB/GEM support"

drivers/net/ethernet/cadence/macb.c

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,33 +79,84 @@
7979
#define MACB_HALT_TIMEOUT 1230
8080

8181
/* DMA buffer descriptor might be different size
82-
* depends on hardware configuration.
82+
* depends on hardware configuration:
83+
*
84+
* 1. dma address width 32 bits:
85+
* word 1: 32 bit address of Data Buffer
86+
* word 2: control
87+
*
88+
* 2. dma address width 64 bits:
89+
* word 1: 32 bit address of Data Buffer
90+
* word 2: control
91+
* word 3: upper 32 bit address of Data Buffer
92+
* word 4: unused
93+
*
94+
* 3. dma address width 32 bits with hardware timestamping:
95+
* word 1: 32 bit address of Data Buffer
96+
* word 2: control
97+
* word 3: timestamp word 1
98+
* word 4: timestamp word 2
99+
*
100+
* 4. dma address width 64 bits with hardware timestamping:
101+
* word 1: 32 bit address of Data Buffer
102+
* word 2: control
103+
* word 3: upper 32 bit address of Data Buffer
104+
* word 4: unused
105+
* word 5: timestamp word 1
106+
* word 6: timestamp word 2
83107
*/
84108
static unsigned int macb_dma_desc_get_size(struct macb *bp)
85109
{
86-
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
87-
if (bp->hw_dma_cap == HW_DMA_CAP_64B)
88-
return sizeof(struct macb_dma_desc) + sizeof(struct macb_dma_desc_64);
110+
#ifdef MACB_EXT_DESC
111+
unsigned int desc_size;
112+
113+
switch (bp->hw_dma_cap) {
114+
case HW_DMA_CAP_64B:
115+
desc_size = sizeof(struct macb_dma_desc)
116+
+ sizeof(struct macb_dma_desc_64);
117+
break;
118+
case HW_DMA_CAP_PTP:
119+
desc_size = sizeof(struct macb_dma_desc)
120+
+ sizeof(struct macb_dma_desc_ptp);
121+
break;
122+
case HW_DMA_CAP_64B_PTP:
123+
desc_size = sizeof(struct macb_dma_desc)
124+
+ sizeof(struct macb_dma_desc_64)
125+
+ sizeof(struct macb_dma_desc_ptp);
126+
break;
127+
default:
128+
desc_size = sizeof(struct macb_dma_desc);
129+
}
130+
return desc_size;
89131
#endif
90132
return sizeof(struct macb_dma_desc);
91133
}
92134

93-
static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int idx)
135+
static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
94136
{
95-
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
96-
/* Dma buffer descriptor is 4 words length (instead of 2 words)
97-
* for 64b GEM.
98-
*/
99-
if (bp->hw_dma_cap == HW_DMA_CAP_64B)
100-
idx <<= 1;
137+
#ifdef MACB_EXT_DESC
138+
switch (bp->hw_dma_cap) {
139+
case HW_DMA_CAP_64B:
140+
case HW_DMA_CAP_PTP:
141+
desc_idx <<= 1;
142+
break;
143+
case HW_DMA_CAP_64B_PTP:
144+
desc_idx *= 3;
145+
break;
146+
default:
147+
break;
148+
}
149+
return desc_idx;
101150
#endif
102-
return idx;
151+
return desc_idx;
103152
}
104153

105154
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
106155
static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
107156
{
108-
return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
157+
if (bp->hw_dma_cap & HW_DMA_CAP_64B)
158+
return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
159+
return NULL;
109160
}
110161
#endif
111162

@@ -621,7 +672,7 @@ static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_
621672
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
622673
struct macb_dma_desc_64 *desc_64;
623674

624-
if (bp->hw_dma_cap == HW_DMA_CAP_64B) {
675+
if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
625676
desc_64 = macb_64b_desc(bp, desc);
626677
desc_64->addrh = upper_32_bits(addr);
627678
}
@@ -635,7 +686,7 @@ static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
635686
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
636687
struct macb_dma_desc_64 *desc_64;
637688

638-
if (bp->hw_dma_cap == HW_DMA_CAP_64B) {
689+
if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
639690
desc_64 = macb_64b_desc(bp, desc);
640691
addr = ((u64)(desc_64->addrh) << 32);
641692
}
@@ -734,7 +785,7 @@ static void macb_tx_error_task(struct work_struct *work)
734785
/* Reinitialize the TX desc queue */
735786
queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
736787
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
737-
if (bp->hw_dma_cap == HW_DMA_CAP_64B)
788+
if (bp->hw_dma_cap & HW_DMA_CAP_64B)
738789
queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
739790
#endif
740791
/* Make TX ring reflect state of hardware */
@@ -1942,8 +1993,12 @@ static void macb_configure_dma(struct macb *bp)
19421993
dmacfg &= ~GEM_BIT(TXCOEN);
19431994

19441995
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1945-
if (bp->hw_dma_cap == HW_DMA_CAP_64B)
1996+
if (bp->hw_dma_cap & HW_DMA_CAP_64B)
19461997
dmacfg |= GEM_BIT(ADDR64);
1998+
#endif
1999+
#ifdef CONFIG_MACB_USE_HWSTAMP
2000+
if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2001+
dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
19472002
#endif
19482003
netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
19492004
dmacfg);
@@ -1992,13 +2047,13 @@ static void macb_init_hw(struct macb *bp)
19922047
/* Initialize TX and RX buffers */
19932048
macb_writel(bp, RBQP, lower_32_bits(bp->rx_ring_dma));
19942049
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1995-
if (bp->hw_dma_cap == HW_DMA_CAP_64B)
2050+
if (bp->hw_dma_cap & HW_DMA_CAP_64B)
19962051
macb_writel(bp, RBQPH, upper_32_bits(bp->rx_ring_dma));
19972052
#endif
19982053
for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
19992054
queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
20002055
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2001-
if (bp->hw_dma_cap == HW_DMA_CAP_64B)
2056+
if (bp->hw_dma_cap & HW_DMA_CAP_64B)
20022057
queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
20032058
#endif
20042059

@@ -2600,6 +2655,12 @@ static void macb_configure_caps(struct macb *bp,
26002655
dcfg = gem_readl(bp, DCFG2);
26012656
if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
26022657
bp->caps |= MACB_CAPS_FIFO_MODE;
2658+
if (IS_ENABLED(CONFIG_MACB_USE_HWSTAMP) && gem_has_ptp(bp)) {
2659+
if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
2660+
pr_err("GEM doesn't support hardware ptp.\n");
2661+
else
2662+
bp->hw_dma_cap |= HW_DMA_CAP_PTP;
2663+
}
26032664
}
26042665

26052666
dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
@@ -2737,7 +2798,7 @@ static int macb_init(struct platform_device *pdev)
27372798
queue->IMR = GEM_IMR(hw_q - 1);
27382799
queue->TBQP = GEM_TBQP(hw_q - 1);
27392800
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2740-
if (bp->hw_dma_cap == HW_DMA_CAP_64B)
2801+
if (bp->hw_dma_cap & HW_DMA_CAP_64B)
27412802
queue->TBQPH = GEM_TBQPH(hw_q - 1);
27422803
#endif
27432804
} else {
@@ -2748,7 +2809,7 @@ static int macb_init(struct platform_device *pdev)
27482809
queue->IMR = MACB_IMR;
27492810
queue->TBQP = MACB_TBQP;
27502811
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2751-
if (bp->hw_dma_cap == HW_DMA_CAP_64B)
2812+
if (bp->hw_dma_cap & HW_DMA_CAP_64B)
27522813
queue->TBQPH = MACB_TBQPH;
27532814
#endif
27542815
}
@@ -3328,19 +3389,17 @@ static int macb_probe(struct platform_device *pdev)
33283389
bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
33293390
device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
33303391

3331-
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3332-
if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
3333-
dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3334-
bp->hw_dma_cap = HW_DMA_CAP_64B;
3335-
} else
3336-
bp->hw_dma_cap = HW_DMA_CAP_32B;
3337-
#endif
3338-
33393392
spin_lock_init(&bp->lock);
33403393

33413394
/* setup capabilities */
33423395
macb_configure_caps(bp, macb_config);
33433396

3397+
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3398+
if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
3399+
dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3400+
bp->hw_dma_cap |= HW_DMA_CAP_64B;
3401+
}
3402+
#endif
33443403
platform_set_drvdata(pdev, dev);
33453404

33463405
dev->irq = platform_get_irq(pdev, 0);

drivers/net/ethernet/cadence/macb.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
#include <linux/phy.h>
1414

15+
#if defined(CONFIG_ARCH_DMA_ADDR_T_64BIT) || defined(CONFIG_MACB_USE_HWSTAMP)
16+
#define MACB_EXT_DESC
17+
#endif
18+
1519
#define MACB_GREGS_NBR 16
1620
#define MACB_GREGS_VERSION 2
1721
#define MACB_MAX_QUEUES 8
@@ -269,6 +273,10 @@
269273
#define GEM_RXBS_SIZE 8
270274
#define GEM_DDRP_OFFSET 24 /* disc_when_no_ahb */
271275
#define GEM_DDRP_SIZE 1
276+
#define GEM_RXEXT_OFFSET 28 /* RX extended Buffer Descriptor mode */
277+
#define GEM_RXEXT_SIZE 1
278+
#define GEM_TXEXT_OFFSET 29 /* TX extended Buffer Descriptor mode */
279+
#define GEM_TXEXT_SIZE 1
272280
#define GEM_ADDR64_OFFSET 30 /* Address bus width - 64b or 32b */
273281
#define GEM_ADDR64_SIZE 1
274282

@@ -425,6 +433,11 @@
425433
#define GEM_TX_PKT_BUFF_OFFSET 21
426434
#define GEM_TX_PKT_BUFF_SIZE 1
427435

436+
437+
/* Bitfields in DCFG5. */
438+
#define GEM_TSU_OFFSET 8
439+
#define GEM_TSU_SIZE 1
440+
428441
/* Bitfields in DCFG6. */
429442
#define GEM_PBUF_LSO_OFFSET 27
430443
#define GEM_PBUF_LSO_SIZE 1
@@ -546,16 +559,21 @@ struct macb_dma_desc {
546559
u32 ctrl;
547560
};
548561

549-
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
550-
enum macb_hw_dma_cap {
551-
HW_DMA_CAP_32B,
552-
HW_DMA_CAP_64B,
553-
};
562+
#ifdef MACB_EXT_DESC
563+
#define HW_DMA_CAP_32B 0
564+
#define HW_DMA_CAP_64B (1 << 0)
565+
#define HW_DMA_CAP_PTP (1 << 1)
566+
#define HW_DMA_CAP_64B_PTP (HW_DMA_CAP_64B | HW_DMA_CAP_PTP)
554567

555568
struct macb_dma_desc_64 {
556569
u32 addrh;
557570
u32 resvd;
558571
};
572+
573+
struct macb_dma_desc_ptp {
574+
u32 ts_1;
575+
u32 ts_2;
576+
};
559577
#endif
560578

561579
/* DMA descriptor bitfields */
@@ -955,8 +973,8 @@ struct macb {
955973
u32 wol;
956974

957975
struct macb_ptp_info *ptp_info; /* macb-ptp interface */
958-
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
959-
enum macb_hw_dma_cap hw_dma_cap;
976+
#ifdef MACB_EXT_DESC
977+
uint8_t hw_dma_cap;
960978
#endif
961979
};
962980

0 commit comments

Comments
 (0)