Commit ccb68993 authored by David S. Miller's avatar David S. Miller

Merge branch 'PTP-clock-source-for-SJA1105-tc-taprio-offload'

Vladimir Oltean says:

====================
PTP clock source for SJA1105 tc-taprio offload

This series makes the IEEE 802.1Qbv egress scheduler of the sja1105
switch use a time reference that is synchronized to the network. This
enables quite a few real Time Sensitive Networking use cases, since in
this mode the switch can offer its clients a TDMA sort of access to the
network, and guaranteed latency for frames that are properly scheduled
based on the common PTP time.

The driver needs to do a 2-part activity:
- Program the gate control list into the static config and upload it
  over SPI to the switch (already supported)
- Write the activation time of the scheduler (base-time) into the
  PTPSCHTM register, and set the PTPSTRTSCH bit.
- Monitor the activation of the scheduler at the planned time and its
  health.

Ok, 3 parts.

The time-aware scheduler cannot be programmed to activate at a time in
the past, and there is some logic to avoid that.

PTPCLKCORP is one of those "black magic" registers that just need to be
written to the length of the cycle. There is a 40-line long comment in
the second patch which explains why.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 72c99609 86db36a3
...@@ -28,6 +28,7 @@ config NET_DSA_SJA1105_TAS ...@@ -28,6 +28,7 @@ config NET_DSA_SJA1105_TAS
bool "Support for the Time-Aware Scheduler on NXP SJA1105" bool "Support for the Time-Aware Scheduler on NXP SJA1105"
depends on NET_DSA_SJA1105 && NET_SCH_TAPRIO depends on NET_DSA_SJA1105 && NET_SCH_TAPRIO
depends on NET_SCH_TAPRIO=y || NET_DSA_SJA1105=m depends on NET_SCH_TAPRIO=y || NET_DSA_SJA1105=m
depends on NET_DSA_SJA1105_PTP
help help
This enables support for the TTEthernet-based egress scheduling This enables support for the TTEthernet-based egress scheduling
engine in the SJA1105 DSA driver, which is controlled using a engine in the SJA1105 DSA driver, which is controlled using a
......
...@@ -20,6 +20,11 @@ ...@@ -20,6 +20,11 @@
*/ */
#define SJA1105_AGEING_TIME_MS(ms) ((ms) / 10) #define SJA1105_AGEING_TIME_MS(ms) ((ms) / 10)
typedef enum {
SPI_READ = 0,
SPI_WRITE = 1,
} sja1105_spi_rw_mode_t;
#include "sja1105_tas.h" #include "sja1105_tas.h"
#include "sja1105_ptp.h" #include "sja1105_ptp.h"
...@@ -35,6 +40,8 @@ struct sja1105_regs { ...@@ -35,6 +40,8 @@ struct sja1105_regs {
u64 ptp_control; u64 ptp_control;
u64 ptpclkval; u64 ptpclkval;
u64 ptpclkrate; u64 ptpclkrate;
u64 ptpclkcorp;
u64 ptpschtm;
u64 ptpegr_ts[SJA1105_NUM_PORTS]; u64 ptpegr_ts[SJA1105_NUM_PORTS];
u64 pad_mii_tx[SJA1105_NUM_PORTS]; u64 pad_mii_tx[SJA1105_NUM_PORTS];
u64 pad_mii_id[SJA1105_NUM_PORTS]; u64 pad_mii_id[SJA1105_NUM_PORTS];
...@@ -71,8 +78,6 @@ struct sja1105_info { ...@@ -71,8 +78,6 @@ struct sja1105_info {
const struct sja1105_dynamic_table_ops *dyn_ops; const struct sja1105_dynamic_table_ops *dyn_ops;
const struct sja1105_table_ops *static_ops; const struct sja1105_table_ops *static_ops;
const struct sja1105_regs *regs; const struct sja1105_regs *regs;
int (*ptp_cmd)(const struct dsa_switch *ds,
const struct sja1105_ptp_cmd *cmd);
int (*reset_cmd)(const void *ctx, const void *data); int (*reset_cmd)(const void *ctx, const void *data);
int (*setup_rgmii_delay)(const void *ctx, int port); int (*setup_rgmii_delay)(const void *ctx, int port);
/* Prototypes from include/net/dsa.h */ /* Prototypes from include/net/dsa.h */
...@@ -80,6 +85,8 @@ struct sja1105_info { ...@@ -80,6 +85,8 @@ struct sja1105_info {
const unsigned char *addr, u16 vid); const unsigned char *addr, u16 vid);
int (*fdb_del_cmd)(struct dsa_switch *ds, int port, int (*fdb_del_cmd)(struct dsa_switch *ds, int port,
const unsigned char *addr, u16 vid); const unsigned char *addr, u16 vid);
void (*ptp_cmd_packing)(u8 *buf, struct sja1105_ptp_cmd *cmd,
enum packing_op op);
const char *name; const char *name;
}; };
...@@ -109,11 +116,6 @@ struct sja1105_spi_message { ...@@ -109,11 +116,6 @@ struct sja1105_spi_message {
u64 address; u64 address;
}; };
typedef enum {
SPI_READ = 0,
SPI_WRITE = 1,
} sja1105_spi_rw_mode_t;
/* From sja1105_main.c */ /* From sja1105_main.c */
enum sja1105_reset_reason { enum sja1105_reset_reason {
SJA1105_VLAN_FILTERING = 0, SJA1105_VLAN_FILTERING = 0,
......
...@@ -193,42 +193,54 @@ int sja1105_get_ts_info(struct dsa_switch *ds, int port, ...@@ -193,42 +193,54 @@ int sja1105_get_ts_info(struct dsa_switch *ds, int port,
return 0; return 0;
} }
int sja1105et_ptp_cmd(const struct dsa_switch *ds, void sja1105et_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
const struct sja1105_ptp_cmd *cmd) enum packing_op op)
{ {
const struct sja1105_private *priv = ds->priv;
const struct sja1105_regs *regs = priv->info->regs;
const int size = SJA1105_SIZE_PTP_CMD; const int size = SJA1105_SIZE_PTP_CMD;
u8 buf[SJA1105_SIZE_PTP_CMD] = {0};
/* No need to keep this as part of the structure */ /* No need to keep this as part of the structure */
u64 valid = 1; u64 valid = 1;
sja1105_pack(buf, &valid, 31, 31, size); sja1105_packing(buf, &valid, 31, 31, size, op);
sja1105_pack(buf, &cmd->resptp, 2, 2, size); sja1105_packing(buf, &cmd->ptpstrtsch, 30, 30, size, op);
sja1105_pack(buf, &cmd->corrclk4ts, 1, 1, size); sja1105_packing(buf, &cmd->ptpstopsch, 29, 29, size, op);
sja1105_pack(buf, &cmd->ptpclkadd, 0, 0, size); sja1105_packing(buf, &cmd->resptp, 2, 2, size, op);
sja1105_packing(buf, &cmd->corrclk4ts, 1, 1, size, op);
sja1105_packing(buf, &cmd->ptpclkadd, 0, 0, size, op);
}
return sja1105_xfer_buf(priv, SPI_WRITE, regs->ptp_control, buf, void sja1105pqrs_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
SJA1105_SIZE_PTP_CMD); enum packing_op op)
{
const int size = SJA1105_SIZE_PTP_CMD;
/* No need to keep this as part of the structure */
u64 valid = 1;
sja1105_packing(buf, &valid, 31, 31, size, op);
sja1105_packing(buf, &cmd->ptpstrtsch, 30, 30, size, op);
sja1105_packing(buf, &cmd->ptpstopsch, 29, 29, size, op);
sja1105_packing(buf, &cmd->resptp, 3, 3, size, op);
sja1105_packing(buf, &cmd->corrclk4ts, 2, 2, size, op);
sja1105_packing(buf, &cmd->ptpclkadd, 0, 0, size, op);
} }
int sja1105pqrs_ptp_cmd(const struct dsa_switch *ds, int sja1105_ptp_commit(struct dsa_switch *ds, struct sja1105_ptp_cmd *cmd,
const struct sja1105_ptp_cmd *cmd) sja1105_spi_rw_mode_t rw)
{ {
const struct sja1105_private *priv = ds->priv; const struct sja1105_private *priv = ds->priv;
const struct sja1105_regs *regs = priv->info->regs; const struct sja1105_regs *regs = priv->info->regs;
const int size = SJA1105_SIZE_PTP_CMD;
u8 buf[SJA1105_SIZE_PTP_CMD] = {0}; u8 buf[SJA1105_SIZE_PTP_CMD] = {0};
/* No need to keep this as part of the structure */ int rc;
u64 valid = 1;
sja1105_pack(buf, &valid, 31, 31, size); if (rw == SPI_WRITE)
sja1105_pack(buf, &cmd->resptp, 3, 3, size); priv->info->ptp_cmd_packing(buf, cmd, PACK);
sja1105_pack(buf, &cmd->corrclk4ts, 2, 2, size);
sja1105_pack(buf, &cmd->ptpclkadd, 0, 0, size);
return sja1105_xfer_buf(priv, SPI_WRITE, regs->ptp_control, buf, rc = sja1105_xfer_buf(priv, SPI_WRITE, regs->ptp_control, buf,
SJA1105_SIZE_PTP_CMD); SJA1105_SIZE_PTP_CMD);
if (rw == SPI_READ)
priv->info->ptp_cmd_packing(buf, cmd, UNPACK);
return rc;
} }
/* The switch returns partial timestamps (24 bits for SJA1105 E/T, which wrap /* The switch returns partial timestamps (24 bits for SJA1105 E/T, which wrap
...@@ -438,8 +450,11 @@ static int sja1105_ptp_reset(struct dsa_switch *ds) ...@@ -438,8 +450,11 @@ static int sja1105_ptp_reset(struct dsa_switch *ds)
mutex_lock(&ptp_data->lock); mutex_lock(&ptp_data->lock);
cmd.resptp = 1; cmd.resptp = 1;
dev_dbg(ds->dev, "Resetting PTP clock\n"); dev_dbg(ds->dev, "Resetting PTP clock\n");
rc = priv->info->ptp_cmd(ds, &cmd); rc = sja1105_ptp_commit(ds, &cmd, SPI_WRITE);
sja1105_tas_clockstep(priv->ds);
mutex_unlock(&ptp_data->lock); mutex_unlock(&ptp_data->lock);
...@@ -495,7 +510,7 @@ static int sja1105_ptp_mode_set(struct sja1105_private *priv, ...@@ -495,7 +510,7 @@ static int sja1105_ptp_mode_set(struct sja1105_private *priv,
ptp_data->cmd.ptpclkadd = mode; ptp_data->cmd.ptpclkadd = mode;
return priv->info->ptp_cmd(priv->ds, &ptp_data->cmd); return sja1105_ptp_commit(priv->ds, &ptp_data->cmd, SPI_WRITE);
} }
/* Write to PTPCLKVAL while PTPCLKADD is 0 */ /* Write to PTPCLKVAL while PTPCLKADD is 0 */
...@@ -512,7 +527,11 @@ int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns, ...@@ -512,7 +527,11 @@ int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
return rc; return rc;
} }
return sja1105_ptpclkval_write(priv, ticks, ptp_sts); rc = sja1105_ptpclkval_write(priv, ticks, ptp_sts);
sja1105_tas_clockstep(priv->ds);
return rc;
} }
static int sja1105_ptp_settime(struct ptp_clock_info *ptp, static int sja1105_ptp_settime(struct ptp_clock_info *ptp,
...@@ -554,6 +573,8 @@ static int sja1105_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) ...@@ -554,6 +573,8 @@ static int sja1105_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->ptpclkrate, &clkrate32, rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->ptpclkrate, &clkrate32,
NULL); NULL);
sja1105_tas_adjfreq(priv->ds);
mutex_unlock(&ptp_data->lock); mutex_unlock(&ptp_data->lock);
return rc; return rc;
...@@ -572,7 +593,11 @@ int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta) ...@@ -572,7 +593,11 @@ int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta)
return rc; return rc;
} }
return sja1105_ptpclkval_write(priv, ticks, NULL); rc = sja1105_ptpclkval_write(priv, ticks, NULL);
sja1105_tas_clockstep(priv->ds);
return rc;
} }
static int sja1105_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) static int sja1105_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
......
...@@ -22,6 +22,8 @@ static inline s64 sja1105_ticks_to_ns(s64 ticks) ...@@ -22,6 +22,8 @@ static inline s64 sja1105_ticks_to_ns(s64 ticks)
} }
struct sja1105_ptp_cmd { struct sja1105_ptp_cmd {
u64 ptpstrtsch; /* start schedule */
u64 ptpstopsch; /* stop schedule */
u64 resptp; /* reset */ u64 resptp; /* reset */
u64 corrclk4ts; /* use the corrected clock for timestamps */ u64 corrclk4ts; /* use the corrected clock for timestamps */
u64 ptpclkadd; /* enum sja1105_ptp_clk_mode */ u64 ptpclkadd; /* enum sja1105_ptp_clk_mode */
...@@ -39,11 +41,11 @@ int sja1105_ptp_clock_register(struct dsa_switch *ds); ...@@ -39,11 +41,11 @@ int sja1105_ptp_clock_register(struct dsa_switch *ds);
void sja1105_ptp_clock_unregister(struct dsa_switch *ds); void sja1105_ptp_clock_unregister(struct dsa_switch *ds);
int sja1105et_ptp_cmd(const struct dsa_switch *ds, void sja1105et_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
const struct sja1105_ptp_cmd *cmd); enum packing_op op);
int sja1105pqrs_ptp_cmd(const struct dsa_switch *ds, void sja1105pqrs_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
const struct sja1105_ptp_cmd *cmd); enum packing_op op);
int sja1105_get_ts_info(struct dsa_switch *ds, int port, int sja1105_get_ts_info(struct dsa_switch *ds, int port,
struct ethtool_ts_info *ts); struct ethtool_ts_info *ts);
...@@ -69,6 +71,9 @@ int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns, ...@@ -69,6 +71,9 @@ int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta); int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta);
int sja1105_ptp_commit(struct dsa_switch *ds, struct sja1105_ptp_cmd *cmd,
sja1105_spi_rw_mode_t rw);
#else #else
struct sja1105_ptp_cmd; struct sja1105_ptp_cmd;
...@@ -110,9 +115,16 @@ static inline int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta) ...@@ -110,9 +115,16 @@ static inline int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta)
return 0; return 0;
} }
#define sja1105et_ptp_cmd NULL static inline int sja1105_ptp_commit(struct dsa_switch *ds,
struct sja1105_ptp_cmd *cmd,
sja1105_spi_rw_mode_t rw)
{
return 0;
}
#define sja1105et_ptp_cmd_packing NULL
#define sja1105pqrs_ptp_cmd NULL #define sja1105pqrs_ptp_cmd_packing NULL
#define sja1105_get_ts_info NULL #define sja1105_get_ts_info NULL
......
...@@ -539,9 +539,11 @@ static struct sja1105_regs sja1105et_regs = { ...@@ -539,9 +539,11 @@ static struct sja1105_regs sja1105et_regs = {
.rmii_ref_clk = {0x100015, 0x10001C, 0x100023, 0x10002A, 0x100031}, .rmii_ref_clk = {0x100015, 0x10001C, 0x100023, 0x10002A, 0x100031},
.rmii_ext_tx_clk = {0x100018, 0x10001F, 0x100026, 0x10002D, 0x100034}, .rmii_ext_tx_clk = {0x100018, 0x10001F, 0x100026, 0x10002D, 0x100034},
.ptpegr_ts = {0xC0, 0xC2, 0xC4, 0xC6, 0xC8}, .ptpegr_ts = {0xC0, 0xC2, 0xC4, 0xC6, 0xC8},
.ptpschtm = 0x12, /* Spans 0x12 to 0x13 */
.ptp_control = 0x17, .ptp_control = 0x17,
.ptpclkval = 0x18, /* Spans 0x18 to 0x19 */ .ptpclkval = 0x18, /* Spans 0x18 to 0x19 */
.ptpclkrate = 0x1A, .ptpclkrate = 0x1A,
.ptpclkcorp = 0x1D,
}; };
static struct sja1105_regs sja1105pqrs_regs = { static struct sja1105_regs sja1105pqrs_regs = {
...@@ -569,9 +571,11 @@ static struct sja1105_regs sja1105pqrs_regs = { ...@@ -569,9 +571,11 @@ static struct sja1105_regs sja1105pqrs_regs = {
.rmii_ext_tx_clk = {0x100017, 0x10001D, 0x100023, 0x100029, 0x10002F}, .rmii_ext_tx_clk = {0x100017, 0x10001D, 0x100023, 0x100029, 0x10002F},
.qlevel = {0x604, 0x614, 0x624, 0x634, 0x644}, .qlevel = {0x604, 0x614, 0x624, 0x634, 0x644},
.ptpegr_ts = {0xC0, 0xC4, 0xC8, 0xCC, 0xD0}, .ptpegr_ts = {0xC0, 0xC4, 0xC8, 0xCC, 0xD0},
.ptpschtm = 0x13, /* Spans 0x13 to 0x14 */
.ptp_control = 0x18, .ptp_control = 0x18,
.ptpclkval = 0x19, .ptpclkval = 0x19,
.ptpclkrate = 0x1B, .ptpclkrate = 0x1B,
.ptpclkcorp = 0x1E,
}; };
struct sja1105_info sja1105e_info = { struct sja1105_info sja1105e_info = {
...@@ -584,7 +588,7 @@ struct sja1105_info sja1105e_info = { ...@@ -584,7 +588,7 @@ struct sja1105_info sja1105e_info = {
.reset_cmd = sja1105et_reset_cmd, .reset_cmd = sja1105et_reset_cmd,
.fdb_add_cmd = sja1105et_fdb_add, .fdb_add_cmd = sja1105et_fdb_add,
.fdb_del_cmd = sja1105et_fdb_del, .fdb_del_cmd = sja1105et_fdb_del,
.ptp_cmd = sja1105et_ptp_cmd, .ptp_cmd_packing = sja1105et_ptp_cmd_packing,
.regs = &sja1105et_regs, .regs = &sja1105et_regs,
.name = "SJA1105E", .name = "SJA1105E",
}; };
...@@ -598,7 +602,7 @@ struct sja1105_info sja1105t_info = { ...@@ -598,7 +602,7 @@ struct sja1105_info sja1105t_info = {
.reset_cmd = sja1105et_reset_cmd, .reset_cmd = sja1105et_reset_cmd,
.fdb_add_cmd = sja1105et_fdb_add, .fdb_add_cmd = sja1105et_fdb_add,
.fdb_del_cmd = sja1105et_fdb_del, .fdb_del_cmd = sja1105et_fdb_del,
.ptp_cmd = sja1105et_ptp_cmd, .ptp_cmd_packing = sja1105et_ptp_cmd_packing,
.regs = &sja1105et_regs, .regs = &sja1105et_regs,
.name = "SJA1105T", .name = "SJA1105T",
}; };
...@@ -613,7 +617,7 @@ struct sja1105_info sja1105p_info = { ...@@ -613,7 +617,7 @@ struct sja1105_info sja1105p_info = {
.reset_cmd = sja1105pqrs_reset_cmd, .reset_cmd = sja1105pqrs_reset_cmd,
.fdb_add_cmd = sja1105pqrs_fdb_add, .fdb_add_cmd = sja1105pqrs_fdb_add,
.fdb_del_cmd = sja1105pqrs_fdb_del, .fdb_del_cmd = sja1105pqrs_fdb_del,
.ptp_cmd = sja1105pqrs_ptp_cmd, .ptp_cmd_packing = sja1105pqrs_ptp_cmd_packing,
.regs = &sja1105pqrs_regs, .regs = &sja1105pqrs_regs,
.name = "SJA1105P", .name = "SJA1105P",
}; };
...@@ -628,7 +632,7 @@ struct sja1105_info sja1105q_info = { ...@@ -628,7 +632,7 @@ struct sja1105_info sja1105q_info = {
.reset_cmd = sja1105pqrs_reset_cmd, .reset_cmd = sja1105pqrs_reset_cmd,
.fdb_add_cmd = sja1105pqrs_fdb_add, .fdb_add_cmd = sja1105pqrs_fdb_add,
.fdb_del_cmd = sja1105pqrs_fdb_del, .fdb_del_cmd = sja1105pqrs_fdb_del,
.ptp_cmd = sja1105pqrs_ptp_cmd, .ptp_cmd_packing = sja1105pqrs_ptp_cmd_packing,
.regs = &sja1105pqrs_regs, .regs = &sja1105pqrs_regs,
.name = "SJA1105Q", .name = "SJA1105Q",
}; };
...@@ -643,7 +647,7 @@ struct sja1105_info sja1105r_info = { ...@@ -643,7 +647,7 @@ struct sja1105_info sja1105r_info = {
.reset_cmd = sja1105pqrs_reset_cmd, .reset_cmd = sja1105pqrs_reset_cmd,
.fdb_add_cmd = sja1105pqrs_fdb_add, .fdb_add_cmd = sja1105pqrs_fdb_add,
.fdb_del_cmd = sja1105pqrs_fdb_del, .fdb_del_cmd = sja1105pqrs_fdb_del,
.ptp_cmd = sja1105pqrs_ptp_cmd, .ptp_cmd_packing = sja1105pqrs_ptp_cmd_packing,
.regs = &sja1105pqrs_regs, .regs = &sja1105pqrs_regs,
.name = "SJA1105R", .name = "SJA1105R",
}; };
...@@ -659,6 +663,6 @@ struct sja1105_info sja1105s_info = { ...@@ -659,6 +663,6 @@ struct sja1105_info sja1105s_info = {
.reset_cmd = sja1105pqrs_reset_cmd, .reset_cmd = sja1105pqrs_reset_cmd,
.fdb_add_cmd = sja1105pqrs_fdb_add, .fdb_add_cmd = sja1105pqrs_fdb_add,
.fdb_del_cmd = sja1105pqrs_fdb_del, .fdb_del_cmd = sja1105pqrs_fdb_del,
.ptp_cmd = sja1105pqrs_ptp_cmd, .ptp_cmd_packing = sja1105pqrs_ptp_cmd_packing,
.name = "SJA1105S", .name = "SJA1105S",
}; };
...@@ -10,6 +10,11 @@ ...@@ -10,6 +10,11 @@
#define SJA1105_TAS_MAX_DELTA BIT(19) #define SJA1105_TAS_MAX_DELTA BIT(19)
#define SJA1105_GATE_MASK GENMASK_ULL(SJA1105_NUM_TC - 1, 0) #define SJA1105_GATE_MASK GENMASK_ULL(SJA1105_NUM_TC - 1, 0)
#define work_to_sja1105_tas(d) \
container_of((d), struct sja1105_tas_data, tas_work)
#define tas_to_sja1105(d) \
container_of((d), struct sja1105_private, tas_data)
/* This is not a preprocessor macro because the "ns" argument may or may not be /* This is not a preprocessor macro because the "ns" argument may or may not be
* s64 at caller side. This ensures it is properly type-cast before div_s64. * s64 at caller side. This ensures it is properly type-cast before div_s64.
*/ */
...@@ -18,6 +23,100 @@ static s64 ns_to_sja1105_delta(s64 ns) ...@@ -18,6 +23,100 @@ static s64 ns_to_sja1105_delta(s64 ns)
return div_s64(ns, 200); return div_s64(ns, 200);
} }
static s64 sja1105_delta_to_ns(s64 delta)
{
return delta * 200;
}
/* Calculate the first base_time in the future that satisfies this
* relationship:
*
* future_base_time = base_time + N x cycle_time >= now, or
*
* now - base_time
* N >= ---------------
* cycle_time
*
* Because N is an integer, the ceiling value of the above "a / b" ratio
* is in fact precisely the floor value of "(a + b - 1) / b", which is
* easier to calculate only having integer division tools.
*/
static s64 future_base_time(s64 base_time, s64 cycle_time, s64 now)
{
s64 a, b, n;
if (base_time >= now)
return base_time;
a = now - base_time;
b = cycle_time;
n = div_s64(a + b - 1, b);
return base_time + n * cycle_time;
}
static int sja1105_tas_set_runtime_params(struct sja1105_private *priv)
{
struct sja1105_tas_data *tas_data = &priv->tas_data;
struct dsa_switch *ds = priv->ds;
s64 earliest_base_time = S64_MAX;
s64 latest_base_time = 0;
s64 its_cycle_time = 0;
s64 max_cycle_time = 0;
int port;
tas_data->enabled = false;
for (port = 0; port < SJA1105_NUM_PORTS; port++) {
const struct tc_taprio_qopt_offload *offload;
offload = tas_data->offload[port];
if (!offload)
continue;
tas_data->enabled = true;
if (max_cycle_time < offload->cycle_time)
max_cycle_time = offload->cycle_time;
if (latest_base_time < offload->base_time)
latest_base_time = offload->base_time;
if (earliest_base_time > offload->base_time) {
earliest_base_time = offload->base_time;
its_cycle_time = offload->cycle_time;
}
}
if (!tas_data->enabled)
return 0;
/* Roll the earliest base time over until it is in a comparable
* time base with the latest, then compare their deltas.
* We want to enforce that all ports' base times are within
* SJA1105_TAS_MAX_DELTA 200ns cycles of one another.
*/
earliest_base_time = future_base_time(earliest_base_time,
its_cycle_time,
latest_base_time);
while (earliest_base_time > latest_base_time)
earliest_base_time -= its_cycle_time;
if (latest_base_time - earliest_base_time >
sja1105_delta_to_ns(SJA1105_TAS_MAX_DELTA)) {
dev_err(ds->dev,
"Base times too far apart: min %llu max %llu\n",
earliest_base_time, latest_base_time);
return -ERANGE;
}
tas_data->earliest_base_time = earliest_base_time;
tas_data->max_cycle_time = max_cycle_time;
dev_dbg(ds->dev, "earliest base time %lld ns\n", earliest_base_time);
dev_dbg(ds->dev, "latest base time %lld ns\n", latest_base_time);
dev_dbg(ds->dev, "longest cycle time %lld ns\n", max_cycle_time);
return 0;
}
/* Lo and behold: the egress scheduler from hell. /* Lo and behold: the egress scheduler from hell.
* *
* At the hardware level, the Time-Aware Shaper holds a global linear arrray of * At the hardware level, the Time-Aware Shaper holds a global linear arrray of
...@@ -99,7 +198,11 @@ static int sja1105_init_scheduling(struct sja1105_private *priv) ...@@ -99,7 +198,11 @@ static int sja1105_init_scheduling(struct sja1105_private *priv)
int num_cycles = 0; int num_cycles = 0;
int cycle = 0; int cycle = 0;
int i, k = 0; int i, k = 0;
int port; int port, rc;
rc = sja1105_tas_set_runtime_params(priv);
if (rc < 0)
return rc;
/* Discard previous Schedule Table */ /* Discard previous Schedule Table */
table = &priv->static_config.tables[BLK_IDX_SCHEDULE]; table = &priv->static_config.tables[BLK_IDX_SCHEDULE];
...@@ -184,11 +287,13 @@ static int sja1105_init_scheduling(struct sja1105_private *priv) ...@@ -184,11 +287,13 @@ static int sja1105_init_scheduling(struct sja1105_private *priv)
schedule_entry_points = table->entries; schedule_entry_points = table->entries;
/* Finally start populating the static config tables */ /* Finally start populating the static config tables */
schedule_entry_points_params->clksrc = SJA1105_TAS_CLKSRC_STANDALONE; schedule_entry_points_params->clksrc = SJA1105_TAS_CLKSRC_PTP;
schedule_entry_points_params->actsubsch = num_cycles - 1; schedule_entry_points_params->actsubsch = num_cycles - 1;
for (port = 0; port < SJA1105_NUM_PORTS; port++) { for (port = 0; port < SJA1105_NUM_PORTS; port++) {
const struct tc_taprio_qopt_offload *offload; const struct tc_taprio_qopt_offload *offload;
/* Relative base time */
s64 rbt;
offload = tas_data->offload[port]; offload = tas_data->offload[port];
if (!offload) if (!offload)
...@@ -196,15 +301,21 @@ static int sja1105_init_scheduling(struct sja1105_private *priv) ...@@ -196,15 +301,21 @@ static int sja1105_init_scheduling(struct sja1105_private *priv)
schedule_start_idx = k; schedule_start_idx = k;
schedule_end_idx = k + offload->num_entries - 1; schedule_end_idx = k + offload->num_entries - 1;
/* TODO this is the base time for the port's subschedule, /* This is the base time expressed as a number of TAS ticks
* relative to PTPSCHTM. But as we're using the standalone * relative to PTPSCHTM, which we'll (perhaps improperly) call
* clock source and not PTP clock as time reference, there's * the operational base time.
* little point in even trying to put more logic into this, */
* like preserving the phases between the subschedules of rbt = future_base_time(offload->base_time,
* different ports. We'll get all of that when switching to the offload->cycle_time,
* PTP clock source. tas_data->earliest_base_time);
rbt -= tas_data->earliest_base_time;
/* UM10944.pdf 4.2.2. Schedule Entry Points table says that
* delta cannot be zero, which is shitty. Advance all relative
* base times by 1 TAS delta, so that even the earliest base
* time becomes 1 in relative terms. Then start the operational
* base time (PTPSCHTM) one TAS delta earlier than planned.
*/ */
entry_point_delta = 1; entry_point_delta = ns_to_sja1105_delta(rbt) + 1;
schedule_entry_points[cycle].subschindx = cycle; schedule_entry_points[cycle].subschindx = cycle;
schedule_entry_points[cycle].delta = entry_point_delta; schedule_entry_points[cycle].delta = entry_point_delta;
...@@ -403,8 +514,303 @@ int sja1105_setup_tc_taprio(struct dsa_switch *ds, int port, ...@@ -403,8 +514,303 @@ int sja1105_setup_tc_taprio(struct dsa_switch *ds, int port,
return sja1105_static_config_reload(priv, SJA1105_SCHEDULING); return sja1105_static_config_reload(priv, SJA1105_SCHEDULING);
} }
static int sja1105_tas_check_running(struct sja1105_private *priv)
{
struct sja1105_tas_data *tas_data = &priv->tas_data;
struct dsa_switch *ds = priv->ds;
struct sja1105_ptp_cmd cmd = {0};
int rc;
rc = sja1105_ptp_commit(ds, &cmd, SPI_READ);
if (rc < 0)
return rc;
if (cmd.ptpstrtsch == 1)
/* Schedule successfully started */
tas_data->state = SJA1105_TAS_STATE_RUNNING;
else if (cmd.ptpstopsch == 1)
/* Schedule is stopped */
tas_data->state = SJA1105_TAS_STATE_DISABLED;
else
/* Schedule is probably not configured with PTP clock source */
rc = -EINVAL;
return rc;
}
/* Write to PTPCLKCORP */
static int sja1105_tas_adjust_drift(struct sja1105_private *priv,
u64 correction)
{
const struct sja1105_regs *regs = priv->info->regs;
u32 ptpclkcorp = ns_to_sja1105_ticks(correction);
return sja1105_xfer_u32(priv, SPI_WRITE, regs->ptpclkcorp,
&ptpclkcorp, NULL);
}
/* Write to PTPSCHTM */
static int sja1105_tas_set_base_time(struct sja1105_private *priv,
u64 base_time)
{
const struct sja1105_regs *regs = priv->info->regs;
u64 ptpschtm = ns_to_sja1105_ticks(base_time);
return sja1105_xfer_u64(priv, SPI_WRITE, regs->ptpschtm,
&ptpschtm, NULL);
}
static int sja1105_tas_start(struct sja1105_private *priv)
{
struct sja1105_tas_data *tas_data = &priv->tas_data;
struct sja1105_ptp_cmd *cmd = &priv->ptp_data.cmd;
struct dsa_switch *ds = priv->ds;
int rc;
dev_dbg(ds->dev, "Starting the TAS\n");
if (tas_data->state == SJA1105_TAS_STATE_ENABLED_NOT_RUNNING ||
tas_data->state == SJA1105_TAS_STATE_RUNNING) {
dev_err(ds->dev, "TAS already started\n");
return -EINVAL;
}
cmd->ptpstrtsch = 1;
cmd->ptpstopsch = 0;
rc = sja1105_ptp_commit(ds, cmd, SPI_WRITE);
if (rc < 0)
return rc;
tas_data->state = SJA1105_TAS_STATE_ENABLED_NOT_RUNNING;
return 0;
}
static int sja1105_tas_stop(struct sja1105_private *priv)
{
struct sja1105_tas_data *tas_data = &priv->tas_data;
struct sja1105_ptp_cmd *cmd = &priv->ptp_data.cmd;
struct dsa_switch *ds = priv->ds;
int rc;
dev_dbg(ds->dev, "Stopping the TAS\n");
if (tas_data->state == SJA1105_TAS_STATE_DISABLED) {
dev_err(ds->dev, "TAS already disabled\n");
return -EINVAL;
}
cmd->ptpstopsch = 1;
cmd->ptpstrtsch = 0;
rc = sja1105_ptp_commit(ds, cmd, SPI_WRITE);
if (rc < 0)
return rc;
tas_data->state = SJA1105_TAS_STATE_DISABLED;
return 0;
}
/* The schedule engine and the PTP clock are driven by the same oscillator, and
* they run in parallel. But whilst the PTP clock can keep an absolute
* time-of-day, the schedule engine is only running in 'ticks' (25 ticks make
* up a delta, which is 200ns), and wrapping around at the end of each cycle.
* The schedule engine is started when the PTP clock reaches the PTPSCHTM time
* (in PTP domain).
* Because the PTP clock can be rate-corrected (accelerated or slowed down) by
* a software servo, and the schedule engine clock runs in parallel to the PTP
* clock, there is logic internal to the switch that periodically keeps the
* schedule engine from drifting away. The frequency with which this internal
* syntonization happens is the PTP clock correction period (PTPCLKCORP). It is
* a value also in the PTP clock domain, and is also rate-corrected.
* To be precise, during a correction period, there is logic to determine by
* how many scheduler clock ticks has the PTP clock drifted. At the end of each
* correction period/beginning of new one, the length of a delta is shrunk or
* expanded with an integer number of ticks, compared with the typical 25.
* So a delta lasts for 200ns (or 25 ticks) only on average.
* Sometimes it is longer, sometimes it is shorter. The internal syntonization
* logic can adjust for at most 5 ticks each 20 ticks.
*
* The first implication is that you should choose your schedule correction
* period to be an integer multiple of the schedule length. Preferably one.
* In case there are schedules of multiple ports active, then the correction
* period needs to be a multiple of them all. Given the restriction that the
* cycle times have to be multiples of one another anyway, this means the
* correction period can simply be the largest cycle time, hence the current
* choice. This way, the updates are always synchronous to the transmission
* cycle, and therefore predictable.
*
* The second implication is that at the beginning of a correction period, the
* first few deltas will be modulated in time, until the schedule engine is
* properly phase-aligned with the PTP clock. For this reason, you should place
* your best-effort traffic at the beginning of a cycle, and your
* time-triggered traffic afterwards.
*
* The third implication is that once the schedule engine is started, it can
* only adjust for so much drift within a correction period. In the servo you
* can only change the PTPCLKRATE, but not step the clock (PTPCLKADD). If you
* want to do the latter, you need to stop and restart the schedule engine,
* which is what the state machine handles.
*/
static void sja1105_tas_state_machine(struct work_struct *work)
{
struct sja1105_tas_data *tas_data = work_to_sja1105_tas(work);
struct sja1105_private *priv = tas_to_sja1105(tas_data);
struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
struct timespec64 base_time_ts, now_ts;
struct dsa_switch *ds = priv->ds;
struct timespec64 diff;
s64 base_time, now;
int rc = 0;
mutex_lock(&ptp_data->lock);
switch (tas_data->state) {
case SJA1105_TAS_STATE_DISABLED:
/* Can't do anything at all if clock is still being stepped */
if (tas_data->last_op != SJA1105_PTP_ADJUSTFREQ)
break;
rc = sja1105_tas_adjust_drift(priv, tas_data->max_cycle_time);
if (rc < 0)
break;
rc = __sja1105_ptp_gettimex(ds, &now, NULL);
if (rc < 0)
break;
/* Plan to start the earliest schedule first. The others
* will be started in hardware, by way of their respective
* entry points delta.
* Try our best to avoid fringe cases (race condition between
* ptpschtm and ptpstrtsch) by pushing the oper_base_time at
* least one second in the future from now. This is not ideal,
* but this only needs to buy us time until the
* sja1105_tas_start command below gets executed.
*/
base_time = future_base_time(tas_data->earliest_base_time,
tas_data->max_cycle_time,
now + 1ull * NSEC_PER_SEC);
base_time -= sja1105_delta_to_ns(1);
rc = sja1105_tas_set_base_time(priv, base_time);
if (rc < 0)
break;
tas_data->oper_base_time = base_time;
rc = sja1105_tas_start(priv);
if (rc < 0)
break;
base_time_ts = ns_to_timespec64(base_time);
now_ts = ns_to_timespec64(now);
dev_dbg(ds->dev, "OPER base time %lld.%09ld (now %lld.%09ld)\n",
base_time_ts.tv_sec, base_time_ts.tv_nsec,
now_ts.tv_sec, now_ts.tv_nsec);
break;
case SJA1105_TAS_STATE_ENABLED_NOT_RUNNING:
if (tas_data->last_op != SJA1105_PTP_ADJUSTFREQ) {
/* Clock was stepped.. bad news for TAS */
sja1105_tas_stop(priv);
break;
}
/* Check if TAS has actually started, by comparing the
* scheduled start time with the SJA1105 PTP clock
*/
rc = __sja1105_ptp_gettimex(ds, &now, NULL);
if (rc < 0)
break;
if (now < tas_data->oper_base_time) {
/* TAS has not started yet */
diff = ns_to_timespec64(tas_data->oper_base_time - now);
dev_dbg(ds->dev, "time to start: [%lld.%09ld]",
diff.tv_sec, diff.tv_nsec);
break;
}
/* Time elapsed, what happened? */
rc = sja1105_tas_check_running(priv);
if (rc < 0)
break;
if (tas_data->state != SJA1105_TAS_STATE_RUNNING)
/* TAS has started */
dev_err(ds->dev,
"TAS not started despite time elapsed\n");
break;
case SJA1105_TAS_STATE_RUNNING:
/* Clock was stepped.. bad news for TAS */
if (tas_data->last_op != SJA1105_PTP_ADJUSTFREQ) {
sja1105_tas_stop(priv);
break;
}
rc = sja1105_tas_check_running(priv);
if (rc < 0)
break;
if (tas_data->state != SJA1105_TAS_STATE_RUNNING)
dev_err(ds->dev, "TAS surprisingly stopped\n");
break;
default:
if (net_ratelimit())
dev_err(ds->dev, "TAS in an invalid state (incorrect use of API)!\n");
}
if (rc && net_ratelimit())
dev_err(ds->dev, "An operation returned %d\n", rc);
mutex_unlock(&ptp_data->lock);
}
void sja1105_tas_clockstep(struct dsa_switch *ds)
{
struct sja1105_private *priv = ds->priv;
struct sja1105_tas_data *tas_data = &priv->tas_data;
if (!tas_data->enabled)
return;
tas_data->last_op = SJA1105_PTP_CLOCKSTEP;
schedule_work(&tas_data->tas_work);
}
void sja1105_tas_adjfreq(struct dsa_switch *ds)
{
struct sja1105_private *priv = ds->priv;
struct sja1105_tas_data *tas_data = &priv->tas_data;
if (!tas_data->enabled)
return;
/* No reason to schedule the workqueue, nothing changed */
if (tas_data->state == SJA1105_TAS_STATE_RUNNING)
return;
tas_data->last_op = SJA1105_PTP_ADJUSTFREQ;
schedule_work(&tas_data->tas_work);
}
void sja1105_tas_setup(struct dsa_switch *ds) void sja1105_tas_setup(struct dsa_switch *ds)
{ {
struct sja1105_private *priv = ds->priv;
struct sja1105_tas_data *tas_data = &priv->tas_data;
INIT_WORK(&tas_data->tas_work, sja1105_tas_state_machine);
tas_data->state = SJA1105_TAS_STATE_DISABLED;
tas_data->last_op = SJA1105_PTP_NONE;
} }
void sja1105_tas_teardown(struct dsa_switch *ds) void sja1105_tas_teardown(struct dsa_switch *ds)
...@@ -413,6 +819,8 @@ void sja1105_tas_teardown(struct dsa_switch *ds) ...@@ -413,6 +819,8 @@ void sja1105_tas_teardown(struct dsa_switch *ds)
struct tc_taprio_qopt_offload *offload; struct tc_taprio_qopt_offload *offload;
int port; int port;
cancel_work_sync(&priv->tas_data.tas_work);
for (port = 0; port < SJA1105_NUM_PORTS; port++) { for (port = 0; port < SJA1105_NUM_PORTS; port++) {
offload = priv->tas_data.offload[port]; offload = priv->tas_data.offload[port];
if (!offload) if (!offload)
......
...@@ -8,8 +8,27 @@ ...@@ -8,8 +8,27 @@
#if IS_ENABLED(CONFIG_NET_DSA_SJA1105_TAS) #if IS_ENABLED(CONFIG_NET_DSA_SJA1105_TAS)
enum sja1105_tas_state {
SJA1105_TAS_STATE_DISABLED,
SJA1105_TAS_STATE_ENABLED_NOT_RUNNING,
SJA1105_TAS_STATE_RUNNING,
};
enum sja1105_ptp_op {
SJA1105_PTP_NONE,
SJA1105_PTP_CLOCKSTEP,
SJA1105_PTP_ADJUSTFREQ,
};
struct sja1105_tas_data { struct sja1105_tas_data {
struct tc_taprio_qopt_offload *offload[SJA1105_NUM_PORTS]; struct tc_taprio_qopt_offload *offload[SJA1105_NUM_PORTS];
enum sja1105_tas_state state;
enum sja1105_ptp_op last_op;
struct work_struct tas_work;
s64 earliest_base_time;
s64 oper_base_time;
u64 max_cycle_time;
bool enabled;
}; };
int sja1105_setup_tc_taprio(struct dsa_switch *ds, int port, int sja1105_setup_tc_taprio(struct dsa_switch *ds, int port,
...@@ -19,6 +38,10 @@ void sja1105_tas_setup(struct dsa_switch *ds); ...@@ -19,6 +38,10 @@ void sja1105_tas_setup(struct dsa_switch *ds);
void sja1105_tas_teardown(struct dsa_switch *ds); void sja1105_tas_teardown(struct dsa_switch *ds);
void sja1105_tas_clockstep(struct dsa_switch *ds);
void sja1105_tas_adjfreq(struct dsa_switch *ds);
#else #else
/* C doesn't allow empty structures, bah! */ /* C doesn't allow empty structures, bah! */
...@@ -36,6 +59,10 @@ static inline void sja1105_tas_setup(struct dsa_switch *ds) { } ...@@ -36,6 +59,10 @@ static inline void sja1105_tas_setup(struct dsa_switch *ds) { }
static inline void sja1105_tas_teardown(struct dsa_switch *ds) { } static inline void sja1105_tas_teardown(struct dsa_switch *ds) { }
static inline void sja1105_tas_clockstep(struct dsa_switch *ds) { }
static inline void sja1105_tas_adjfreq(struct dsa_switch *ds) { }
#endif /* IS_ENABLED(CONFIG_NET_DSA_SJA1105_TAS) */ #endif /* IS_ENABLED(CONFIG_NET_DSA_SJA1105_TAS) */
#endif /* _SJA1105_TAS_H */ #endif /* _SJA1105_TAS_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment