On 13 Oct 2026, at 20:46, [email protected] wrote:

> From: Alexander Hansen <[email protected]>
>
> Who to explain it better than the manufacturer:
>
> ```
> The PCA9544A is a 1-of-4 bidirectional translating multiplexer,
> controlled via the I2C-bus. The SCL/SDA upstream pair fans out to four
> SCx/SDx downstream pairs, or channels. Only one SCx/SDx channel is
> selected at a time, determined by the contents of the programmable
> control register.
> ```

Might be worth to add a note on how this device differs from the previously 
supported ones, to spot the channel selection behavior?

> The device updates the selected channel after STOP condition, not on
> the last byte written [1].

See below: I think this is the case for all supported devices, and that 
existing emulation is not fully valid on this point.

> For this reason the parent event handler has to be hooked to prevent
> write-read sequence from updating channel selection before STOP
> condition.
>
> Reset also had to be modified since a write would not apply the channel
> selection by itself.
>
> This emulation is done to support yosemite4 machine emulation.

Maybe part of the cover letter rather than this specific commit?

>
> Interrupt forwarding is currently UNSUPPORTED.
>
> References:
> [1] https://www.nxp.com/docs/en/data-sheet/PCA9544A.pdf
>
> Cc: Patrick Leis <[email protected]> (maintainer:pca954x)
> Cc: Emmanuel Blot <[email protected]>
> Cc: Titus Rwantare <[email protected]>
> Cc: "Cédric Le Goater" <[email protected]> (maintainer:ASPEED BMCs)
> Cc: [email protected] (open list:ASPEED BMCs)
> Cc: [email protected] (open list:All patches CC here)
>
> Signed-off-by: Alexander Hansen <[email protected]>
> ---
>  hw/i2c/i2c_mux_pca954x.c         | 88 ++++++++++++++++++++++++++++----
>  include/hw/i2c/i2c_mux_pca954x.h |  1 +
>  2 files changed, 78 insertions(+), 11 deletions(-)
>
> diff --git a/hw/i2c/i2c_mux_pca954x.c b/hw/i2c/i2c_mux_pca954x.c
> index c603446028..19212dfa34 100644
> --- a/hw/i2c/i2c_mux_pca954x.c
> +++ b/hw/i2c/i2c_mux_pca954x.c
> @@ -30,6 +30,10 @@
>  #define PCA9548_CHANNEL_COUNT 8
>  #define PCA9546_CHANNEL_COUNT 4
> +#define PCA9544_CHANNEL_COUNT 4
> +
> +/* enable bit */
> +#define PCA9544_ENABLE 0x4

It seems PCA9544_ENABLE is used both as a flag and to compute the channel mask.
It might be easier to read using a boolean for the flag, and 
(PCA9544_CHANNEL_COUNT - 1) or (mc->nchans - 1) for the mask?

>  /*
>   * struct Pca954xState - The pca954x state object.
> @@ -56,6 +60,11 @@ typedef struct Pca954xClass {
>      SMBusDeviceClass parent;
>
>      uint8_t nchans;
> +
> +    /* channel enable bit for mux devices, zero for switches */
> +    uint8_t mux_enable;

Exiting document style (@mux_enable …) in struct Pca954xClass comment block 
should be preferred.

> +
> +    int (*parent_event)(I2CSlave *s, enum i2c_event event);
>  } Pca954xClass;
>
>  #define TYPE_PCA954X "pca954x"
> @@ -99,28 +108,43 @@ static bool pca954x_match(I2CSlave *candidate, uint8_t 
> address,
>      return broadcast;
>  }
>
> -static void pca954x_enable_channel(Pca954xState *s, uint8_t enable_mask)
> +static void pca954x_enable_channel(Pca954xState *s, uint8_t control)
>  {
>      Pca954xClass *mc = PCA954X_GET_CLASS(s);
>      int i;
>
> +    if (mc->mux_enable) {
> +        uint8_t selected = control & (mc->mux_enable - 1);
> +        bool enabled = control & mc->mux_enable;
> +
> +        for (i = 0; i < mc->nchans; i++) {
> +            s->enabled[i] = enabled && i == selected;
> +        }
> +        return;
> +    }
> +
>      /*
> -     * For each channel, check if their bit is set in enable_mask and if yes,
> +     * For each channel, check if their bit is set in control and if yes,
>       * enable it, otherwise disable, hide it.
>       */
>      for (i = 0; i < mc->nchans; i++) {
> -        if (enable_mask & (1 << i)) {
> -            s->enabled[i] = true;
> -        } else {
> -            s->enabled[i] = false;
> -        }
> +        s->enabled[i] = control & (1 << i);
>      }
>  }
>
>  static void pca954x_write(Pca954xState *s, uint8_t data)
>  {
> -    s->control = data;
> -    pca954x_enable_channel(s, data);
> +    Pca954xClass *mc = PCA954X_GET_CLASS(s);
> +    uint8_t data_writable = data;
> +
> +    if (mc->mux_enable) {
> +        data_writable = data & 0x0f;

Maybe use a named constant here

> +    }
> +
> +    s->control = data_writable;
> +    if (!mc->mux_enable) {
> +        pca954x_enable_channel(s, data_writable);
> +    }

Maybe simplify this whole function:
  - channel enablement occurs on STOP condition for all devices, not only 
PCA9544; channel enablement should be deferred to this event.
  - I think pca954x_enable_channel always ignore non-channel bits, whatever the 
device

Could be as simple as:
s->control = mc->mux_enable ? data & 0x0f : data;

>
>      trace_pca954x_write_bytes(data);
>  }
> @@ -128,6 +152,7 @@ static void pca954x_write(Pca954xState *s, uint8_t data)
>  static int pca954x_write_data(SMBusDevice *d, uint8_t *buf, uint8_t len)
>  {
>      Pca954xState *s = PCA954X(d);
> +    Pca954xClass *mc = PCA954X_GET_CLASS(s);
>
>      if (len == 0) {
>          qemu_log_mask(LOG_GUEST_ERROR, "%s: writing empty data\n", __func__);
> @@ -136,15 +161,17 @@ static int pca954x_write_data(SMBusDevice *d, uint8_t 
> *buf, uint8_t len)
>
>      /*
>       * len should be 1, because they write one byte to enable/disable 
> channels.
> +     * In case of PCA9544A, multiple bytes can be written but only the last 
> one
> +     * will be used.
>       */

I think for all devices, last byte is always considered, there is no special 
case for PCA9544.
The error log call / return error could then be removed.

> -    if (len > 1) {
> +    if (len > 1 && !mc->mux_enable) {
>          qemu_log_mask(LOG_GUEST_ERROR,
>              "%s: extra data after channel selection mask\n",
>              __func__);
>          return -1;
>      }
>
> -    pca954x_write(s, buf[0]);
> +    pca954x_write(s, buf[len - 1]);
>      return 0;
>  }
>
> @@ -156,11 +183,33 @@ static uint8_t pca954x_read_byte(SMBusDevice *d)
>      return data;
>  }
>
> +static int pca954x_event(I2CSlave *s, enum i2c_event event)
> +{
> +    Pca954xState *state = PCA954X(s);
> +    Pca954xClass *mc = PCA954X_GET_CLASS(state);
> +
> +    int res = mc->parent_event(s, event);
> +
> +    /*
> +     * When a channel is selected, it will become active after
> +     * a STOP condition has been placed on the I2C-bus.
> +     */
> +    if (mc->mux_enable && event == I2C_FINISH) {
> +        pca954x_enable_channel(state, state->control);
> +    }
> +    return res;
> +}
> +
>  static void pca954x_enter_reset(Object *obj, ResetType type)
>  {
>      Pca954xState *s = PCA954X(obj);
> +    Pca954xClass *mc = PCA954X_GET_CLASS(s);
>      /* Reset will disable all channels. */
>      pca954x_write(s, 0);

I would favor a call to pca954x_enable_channel rather than pca954x_write here 
whatever the device kind, it would be cleaner as pca954x_write does masking 
then call pca954x_enable_channel and it’s not a write call per se.

> +
> +    if (mc->mux_enable) {
> +        pca954x_enable_channel(s, s->control);
> +    }
>  }
>
>  I2CBus *pca954x_i2c_get_bus(I2CSlave *mux, uint8_t channel)
> @@ -172,6 +221,13 @@ I2CBus *pca954x_i2c_get_bus(I2CSlave *mux, uint8_t 
> channel)
>      return pca954x->bus[channel];
>  }
>
> +static void pca9544_class_init(ObjectClass *klass, const void *data)
> +{
> +    Pca954xClass *s = PCA954X_CLASS(klass);

Prefer c or k, as s is usually used with *State structures.

> +    s->nchans = PCA9544_CHANNEL_COUNT;
> +    s->mux_enable = PCA9544_ENABLE;
> +}
> +
>  static void pca9546_class_init(ObjectClass *klass, const void *data)
>  {
>      Pca954xClass *s = PCA954X_CLASS(klass);
> @@ -221,8 +277,13 @@ static void pca954x_class_init(ObjectClass *klass, const 
> void *data)
>      ResettableClass *rc = RESETTABLE_CLASS(klass);
>      DeviceClass *dc = DEVICE_CLASS(klass);
>      SMBusDeviceClass *k = SMBUS_DEVICE_CLASS(klass);
> +    Pca954xClass *s = PCA954X_CLASS(klass);
> +
> +    /* hook our parent event handler */
> +    s->parent_event = sc->event;
>
>      sc->match_and_add = pca954x_match;
> +    sc->event = pca954x_event;
>
>      rc->phases.enter = pca954x_enter_reset;
>
> @@ -245,6 +306,11 @@ static const TypeInfo pca954x_info[] = {
>          .class_init    = pca954x_class_init,
>          .abstract      = true,
>      },
> +    {
> +        .name          = TYPE_PCA9544,
> +        .parent        = TYPE_PCA954X,
> +        .class_init    = pca9544_class_init,
> +    },
>      {
>          .name          = TYPE_PCA9546,
>          .parent        = TYPE_PCA954X,
> diff --git a/include/hw/i2c/i2c_mux_pca954x.h 
> b/include/hw/i2c/i2c_mux_pca954x.h
> index 3dd25ec983..6449a6dfae 100644
> --- a/include/hw/i2c/i2c_mux_pca954x.h
> +++ b/include/hw/i2c/i2c_mux_pca954x.h
> @@ -3,6 +3,7 @@
>
>  #include "hw/i2c/i2c.h"
>
> +#define TYPE_PCA9544 "pca9544"
>  #define TYPE_PCA9546 "pca9546"
>  #define TYPE_PCA9548 "pca9548"
>
> -- 
> 2.56.0

Note: it might be worth splitting this commit into:
1. existing PCA954x fixes:
  - update channel enablement on STOP bit
  - last-byte-wins
  - enable_channel loop simplification
  - reset calls …enable_channel rather than …write
  - header inclusion removal (sys bus.h at least)
  - …
2. adding support for PCA9544

Emmanuel.


Reply via email to