Skip to content

Commit

Permalink
net: ethernet: renesas: rcar_gen4_ptp: Get clock increment from clock…
Browse files Browse the repository at this point in the history
… rate

Instead of using hard coded clock increment values for each SoC derive
the clock increment from the module clock. This is done in preparation
to support a second platform, R-Car V4H that uses a 200Mhz clock
compared with the 320Mhz clock used on R-Car S4.

Tested on both SoCs,

S4 reports a clock of 320000000Hz which gives a value of 0x19000000.
Documentation says a 320Mhz clock is used and the correct increment for
that clock is 0x19000000.

V4H reports a clock of 199999992Hz which gives a value of 0x2800001a.
Documentation says a 200Mhz clock is used and the correct increment for
that clock is 0x28000000.

Suggested-by: Geert Uytterhoeven <geert@linux-m68k.org>
Signed-off-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
Reviewed-by: Simon Horman <horms@kernel.org>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
  • Loading branch information
Niklas Söderlund authored and Paolo Abeni committed Nov 23, 2023
1 parent 46c361a commit be5f81d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
14 changes: 12 additions & 2 deletions drivers/net/ethernet/renesas/rcar_gen4_ptp.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,18 @@ static int rcar_gen4_ptp_set_offs(struct rcar_gen4_ptp_private *ptp_priv,
return 0;
}

static s64 rcar_gen4_ptp_rate_to_increment(u32 rate)
{
/* Timer increment in ns.
* bit[31:27] - integer
* bit[26:0] - decimal
* increment[ns] = perid[ns] * 2^27 => (1ns * 2^27) / rate[hz]
*/
return div_s64(1000000000LL << 27, rate);
}

int rcar_gen4_ptp_register(struct rcar_gen4_ptp_private *ptp_priv,
enum rcar_gen4_ptp_reg_layout layout, u32 clock)
enum rcar_gen4_ptp_reg_layout layout, u32 rate)
{
int ret;

Expand All @@ -155,7 +165,7 @@ int rcar_gen4_ptp_register(struct rcar_gen4_ptp_private *ptp_priv,
if (ret)
return ret;

ptp_priv->default_addend = clock;
ptp_priv->default_addend = rcar_gen4_ptp_rate_to_increment(rate);
iowrite32(ptp_priv->default_addend, ptp_priv->addr + ptp_priv->offs->increment);
ptp_priv->clock = ptp_clock_register(&ptp_priv->info, NULL);
if (IS_ERR(ptp_priv->clock))
Expand Down
4 changes: 1 addition & 3 deletions drivers/net/ethernet/renesas/rcar_gen4_ptp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include <linux/ptp_clock_kernel.h>

#define PTPTIVC_INIT 0x19000000 /* 320MHz */
#define RCAR_GEN4_PTP_CLOCK_S4 PTPTIVC_INIT
#define RCAR_GEN4_GPTP_OFFSET_S4 0x00018000

enum rcar_gen4_ptp_reg_layout {
Expand Down Expand Up @@ -64,7 +62,7 @@ struct rcar_gen4_ptp_private {
};

int rcar_gen4_ptp_register(struct rcar_gen4_ptp_private *ptp_priv,
enum rcar_gen4_ptp_reg_layout layout, u32 clock);
enum rcar_gen4_ptp_reg_layout layout, u32 rate);
int rcar_gen4_ptp_unregister(struct rcar_gen4_ptp_private *ptp_priv);
struct rcar_gen4_ptp_private *rcar_gen4_ptp_alloc(struct platform_device *pdev);

Expand Down
2 changes: 1 addition & 1 deletion drivers/net/ethernet/renesas/rswitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,7 @@ static int rswitch_init(struct rswitch_private *priv)
rswitch_fwd_init(priv);

err = rcar_gen4_ptp_register(priv->ptp_priv, RCAR_GEN4_PTP_REG_LAYOUT,
RCAR_GEN4_PTP_CLOCK_S4);
clk_get_rate(priv->clk));
if (err < 0)
goto err_ptp_register;

Expand Down

0 comments on commit be5f81d

Please sign in to comment.