Re: [PATCH v3 1/3] hw/i2c: Implement Broadcom Serial Controller (BSC)

2024-02-23 Thread Peter Maydell
On Tue, 20 Feb 2024 at 13:42, Rayhan Faizel  wrote:
>
> A few deficiencies in the current device model need to be noted.
>
> 1. FIFOs are not used. All sends and receives are done directly.
> 2. Repeated starts are not emulated. Repeated starts can be triggered in real
> hardware by sending a new read transfer request in the window time between
> transfer active set of write transfer request and done bit set of the same.
>
> Signed-off-by: Rayhan Faizel 

Other than the minor things Philippe has noted,

Reviewed-by: Peter Maydell 

thanks
-- PMM



Re: [PATCH v3 1/3] hw/i2c: Implement Broadcom Serial Controller (BSC)

2024-02-22 Thread Philippe Mathieu-Daudé

On 20/2/24 14:41, Rayhan Faizel wrote:

A few deficiencies in the current device model need to be noted.

1. FIFOs are not used. All sends and receives are done directly.
2. Repeated starts are not emulated. Repeated starts can be triggered in real
hardware by sending a new read transfer request in the window time between
transfer active set of write transfer request and done bit set of the same.

Signed-off-by: Rayhan Faizel 
---
  docs/system/arm/raspi.rst|   1 +
  hw/i2c/Kconfig   |   4 +
  hw/i2c/bcm2835_i2c.c | 278 +++
  hw/i2c/meson.build   |   1 +
  include/hw/i2c/bcm2835_i2c.h |  80 ++
  5 files changed, 364 insertions(+)
  create mode 100644 hw/i2c/bcm2835_i2c.c
  create mode 100644 include/hw/i2c/bcm2835_i2c.h




+static const MemoryRegionOps bcm2835_i2c_ops = {
+.read = bcm2835_i2c_read,
+.write = bcm2835_i2c_write,
+.endianness = DEVICE_NATIVE_ENDIAN,


Watch out, your implementation is 32-bit, so this misses:

  .impl = {
  .min_access_size = 4,
  .max_access_size = 4,
  },


+};




diff --git a/include/hw/i2c/bcm2835_i2c.h b/include/hw/i2c/bcm2835_i2c.h
new file mode 100644
index 00..0a56df4720
--- /dev/null
+++ b/include/hw/i2c/bcm2835_i2c.h




+#define BCM2835_I2C_C   0x0   /* Control */
+#define BCM2835_I2C_S   0x4   /* Status */
+#define BCM2835_I2C_DLEN0x8   /* Data Length */
+#define BCM2835_I2C_A   0xc   /* Slave Address */
+#define BCM2835_I2C_FIFO0x10  /* FIFO */
+#define BCM2835_I2C_DIV 0x14  /* Clock Divider */
+#define BCM2835_I2C_DEL 0x18  /* Data Delay */
+#define BCM2835_I2C_CLKT0x20  /* Clock Stretch Timeout */




Re: [PATCH v3 1/3] hw/i2c: Implement Broadcom Serial Controller (BSC)

2024-02-22 Thread Philippe Mathieu-Daudé

Hi Rayhan,

On 20/2/24 14:41, Rayhan Faizel wrote:

A few deficiencies in the current device model need to be noted.

1. FIFOs are not used. All sends and receives are done directly.
2. Repeated starts are not emulated. Repeated starts can be triggered in real
hardware by sending a new read transfer request in the window time between
transfer active set of write transfer request and done bit set of the same.

Signed-off-by: Rayhan Faizel 
---
  docs/system/arm/raspi.rst|   1 +
  hw/i2c/Kconfig   |   4 +
  hw/i2c/bcm2835_i2c.c | 278 +++
  hw/i2c/meson.build   |   1 +
  include/hw/i2c/bcm2835_i2c.h |  80 ++
  5 files changed, 364 insertions(+)
  create mode 100644 hw/i2c/bcm2835_i2c.c
  create mode 100644 include/hw/i2c/bcm2835_i2c.h




new file mode 100644
index 00..d6b9bf887a
--- /dev/null
+++ b/hw/i2c/bcm2835_i2c.c
@@ -0,0 +1,278 @@
+/*
+ * Broadcom Serial Controller (BSC)
+ *
+ * Copyright (c) 2024 Rayhan Faizel 
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "hw/i2c/bcm2835_i2c.h"
+#include "hw/irq.h"
+#include "migration/vmstate.h"
+
+static void bcm2835_i2c_update_interrupt(BCM2835I2CState *s)
+{
+int do_interrupt = 0;
+/* Interrupt on RXR (Needs reading) */
+if (s->c & BCM2835_I2C_C_INTR && s->s & BCM2835_I2C_S_RXR) {
+do_interrupt = 1;
+}
+
+/* Interrupt on TXW (Needs writing) */
+if (s->c & BCM2835_I2C_C_INTT && s->s & BCM2835_I2C_S_TXW) {
+do_interrupt = 1;
+}
+
+/* Interrupt on DONE (Transfer complete) */
+if (s->c & BCM2835_I2C_C_INTD && s->s & BCM2835_I2C_S_DONE) {
+do_interrupt = 1;
+}
+qemu_set_irq(s->irq, do_interrupt);
+}
+
+static void bcm2835_i2c_begin_transfer(BCM2835I2CState *s)
+{
+int direction = s->c & BCM2835_I2C_C_READ;
+if (i2c_start_transfer(s->bus, s->a, direction)) {
+s->s |= BCM2835_I2C_S_ERR;
+}
+s->s |= BCM2835_I2C_S_TA;
+
+if (direction) {
+s->s |= BCM2835_I2C_S_RXR | BCM2835_I2C_S_RXD;
+} else {
+s->s |= BCM2835_I2C_S_TXW;
+}
+}
+
+static void bcm2835_i2c_finish_transfer(BCM2835I2CState *s)
+{
+/*
+ * STOP is sent when DLEN counts down to zero.
+ *
+ * 
https://github.com/torvalds/linux/blob/master/drivers/i2c/busses/i2c-bcm2835.c#L223-L261


Sorry for not reviewing your patches earlier.

Since this documentation will stay for long and the Linux master branch
will change, better use a tag:
https://github.com/torvalds/linux/blob//v6.7/drivers/i2c/busses/i2c-bcm2835.c#L223-L261

Do you mind posting a patch to correct this?

Thanks,

Phil.



[PATCH v3 1/3] hw/i2c: Implement Broadcom Serial Controller (BSC)

2024-02-20 Thread Rayhan Faizel
A few deficiencies in the current device model need to be noted.

1. FIFOs are not used. All sends and receives are done directly.
2. Repeated starts are not emulated. Repeated starts can be triggered in real
hardware by sending a new read transfer request in the window time between
transfer active set of write transfer request and done bit set of the same.

Signed-off-by: Rayhan Faizel 
---
 docs/system/arm/raspi.rst|   1 +
 hw/i2c/Kconfig   |   4 +
 hw/i2c/bcm2835_i2c.c | 278 +++
 hw/i2c/meson.build   |   1 +
 include/hw/i2c/bcm2835_i2c.h |  80 ++
 5 files changed, 364 insertions(+)
 create mode 100644 hw/i2c/bcm2835_i2c.c
 create mode 100644 include/hw/i2c/bcm2835_i2c.h

diff --git a/docs/system/arm/raspi.rst b/docs/system/arm/raspi.rst
index d0a6f08b2b..f2c0d6d6b8 100644
--- a/docs/system/arm/raspi.rst
+++ b/docs/system/arm/raspi.rst
@@ -34,6 +34,7 @@ Implemented devices
  * MailBox controller (MBOX)
  * VideoCore firmware (property)
  * Peripheral SPI controller (SPI)
+ * Broadcom Serial Controller (I2C)
 
 
 Missing devices
diff --git a/hw/i2c/Kconfig b/hw/i2c/Kconfig
index 14886b35da..596a7a3165 100644
--- a/hw/i2c/Kconfig
+++ b/hw/i2c/Kconfig
@@ -45,3 +45,7 @@ config PCA954X
 config PMBUS
 bool
 select SMBUS
+
+config BCM2835_I2C
+bool
+select I2C
diff --git a/hw/i2c/bcm2835_i2c.c b/hw/i2c/bcm2835_i2c.c
new file mode 100644
index 00..d6b9bf887a
--- /dev/null
+++ b/hw/i2c/bcm2835_i2c.c
@@ -0,0 +1,278 @@
+/*
+ * Broadcom Serial Controller (BSC)
+ *
+ * Copyright (c) 2024 Rayhan Faizel 
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "hw/i2c/bcm2835_i2c.h"
+#include "hw/irq.h"
+#include "migration/vmstate.h"
+
+static void bcm2835_i2c_update_interrupt(BCM2835I2CState *s)
+{
+int do_interrupt = 0;
+/* Interrupt on RXR (Needs reading) */
+if (s->c & BCM2835_I2C_C_INTR && s->s & BCM2835_I2C_S_RXR) {
+do_interrupt = 1;
+}
+
+/* Interrupt on TXW (Needs writing) */
+if (s->c & BCM2835_I2C_C_INTT && s->s & BCM2835_I2C_S_TXW) {
+do_interrupt = 1;
+}
+
+/* Interrupt on DONE (Transfer complete) */
+if (s->c & BCM2835_I2C_C_INTD && s->s & BCM2835_I2C_S_DONE) {
+do_interrupt = 1;
+}
+qemu_set_irq(s->irq, do_interrupt);
+}
+
+static void bcm2835_i2c_begin_transfer(BCM2835I2CState *s)
+{
+int direction = s->c & BCM2835_I2C_C_READ;
+if (i2c_start_transfer(s->bus, s->a, direction)) {
+s->s |= BCM2835_I2C_S_ERR;
+}
+s->s |= BCM2835_I2C_S_TA;
+
+if (direction) {
+s->s |= BCM2835_I2C_S_RXR | BCM2835_I2C_S_RXD;
+} else {
+s->s |= BCM2835_I2C_S_TXW;
+}
+}
+
+static void bcm2835_i2c_finish_transfer(BCM2835I2CState *s)
+{
+/*
+ * STOP is sent when DLEN counts down to zero.
+ *
+ * 
https://github.com/torvalds/linux/blob/master/drivers/i2c/busses/i2c-bcm2835.c#L223-L261
+ * It is possible to initiate repeated starts on real hardware.
+ * However, this requires sending another ST request before the bytes in
+ * TX FIFO are shifted out.
+ *
+ * This is not emulated currently.
+ */
+i2c_end_transfer(s->bus);
+s->s |= BCM2835_I2C_S_DONE;
+
+/* Ensure RXD is cleared, otherwise the driver registers an error */
+s->s &= ~(BCM2835_I2C_S_TA | BCM2835_I2C_S_RXR |
+  BCM2835_I2C_S_TXW | BCM2835_I2C_S_RXD);
+}
+
+static uint64_t bcm2835_i2c_read(void *opaque, hwaddr addr, unsigned size)
+{
+BCM2835I2CState *s = opaque;
+uint32_t readval = 0;
+
+switch (addr) {
+case BCM2835_I2C_C:
+readval = s->c;
+break;
+case BCM2835_I2C_S:
+readval = s->s;
+break;
+case BCM2835_I2C_DLEN:
+readval = s->dlen;
+break;
+case BCM2835_I2C_A:
+readval = s->a;
+b