On Wed, 31 May 2023 13:47:42 +0200 Klaus Jensen <i...@irrelevant.dk> wrote:
> From: Klaus Jensen <k.jen...@samsung.com> > > Add i2c_smbus_pec() to calculate the SMBus Packet Error Code for a > message. > > Signed-off-by: Klaus Jensen <k.jen...@samsung.com> LGTM Reviewed-by: Jonathan Cameron <jonathan.came...@huawei.com> > --- > hw/i2c/smbus_master.c | 28 ++++++++++++++++++++++++++++ > include/hw/i2c/smbus_master.h | 2 ++ > 2 files changed, 30 insertions(+) > > diff --git a/hw/i2c/smbus_master.c b/hw/i2c/smbus_master.c > index 6a53c34e70b7..47f9eb24e033 100644 > --- a/hw/i2c/smbus_master.c > +++ b/hw/i2c/smbus_master.c > @@ -15,6 +15,34 @@ > #include "hw/i2c/i2c.h" > #include "hw/i2c/smbus_master.h" > > +static uint8_t crc8(uint16_t data) > +{ > +#define POLY (0x1070U << 3) > + int i; > + > + for (i = 0; i < 8; i++) { > + if (data & 0x8000) { > + data = data ^ POLY; > + } > + > + data = data << 1; > + } > + > + return (uint8_t)(data >> 8); > +#undef POLY > +} > + > +uint8_t i2c_smbus_pec(uint8_t crc, uint8_t *buf, size_t len) > +{ > + int i; > + > + for (i = 0; i < len; i++) { > + crc = crc8((crc ^ buf[i]) << 8); > + } > + > + return crc; > +} > + > /* Master device commands. */ > int smbus_quick_command(I2CBus *bus, uint8_t addr, int read) > { > diff --git a/include/hw/i2c/smbus_master.h b/include/hw/i2c/smbus_master.h > index bb13bc423c22..d90f81767d86 100644 > --- a/include/hw/i2c/smbus_master.h > +++ b/include/hw/i2c/smbus_master.h > @@ -27,6 +27,8 @@ > > #include "hw/i2c/i2c.h" > > +uint8_t i2c_smbus_pec(uint8_t crc, uint8_t *buf, size_t len); > + > /* Master device commands. */ > int smbus_quick_command(I2CBus *bus, uint8_t addr, int read); > int smbus_receive_byte(I2CBus *bus, uint8_t addr);