Add support for I2C in BCM2835 boards
Subsystems (3)
v4 cover Add support for I2C in BCM2835 boards
This patch series implements support for the Broadcom Serial Controller used by BCM2835 based boards for I2C.
[Changes in v4]
- Added IRQ or-gate for common BSC IRQ.
- Added valid sizes to MemoryRegionOps.
- Use version tag instead of master
[Changes in v3]
- Add SPDX license identifiers.
- Fix a few minor whitespace issues.
[Changes in v2]
- Fixed and simplified writing to status register
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/481
Rayhan Faizel (3):
hw/i2c: Implement Broadcom Serial Controller (BSC) hw/arm: Connect BSC to BCM2835 board as I2C0, I2C1 and I2C2 tests/qtest: Add testcase for BCM2835 BSC
docs/system/arm/raspi.rst | 1 + hw/arm/Kconfig | 1 + hw/arm/bcm2835_peripherals.c | 45 ++++- hw/i2c/Kconfig | 4 + hw/i2c/bcm2835_i2c.c | 282 +++++++++++++++++++++++++++ hw/i2c/meson.build | 1 + include/hw/arm/bcm2835_peripherals.h | 4 +- include/hw/i2c/bcm2835_i2c.h | 80 ++++++++ tests/qtest/bcm2835-i2c-test.c | 115 +++++++++++ tests/qtest/meson.build | 2 +- 10 files changed, 530 insertions(+), 5 deletions(-) create mode 100644 hw/i2c/bcm2835_i2c.c create mode 100644 include/hw/i2c/bcm2835_i2c.h create mode 100644 tests/qtest/bcm2835-i2c-test.c
Signature · 1 line
2.34.1
PATCH v4 1/3 hw/i2c: Implement Broadcom Serial Controller (BSC)
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.
--- docs/system/arm/raspi.rst | 1 + hw/i2c/Kconfig | 4 + hw/i2c/bcm2835_i2c.c | 282 +++++++++++++++++++++++++++++++++++ hw/i2c/meson.build | 1 + include/hw/i2c/bcm2835_i2c.h | 80 ++++++++++ 5 files changed, 368 insertions(+) create mode 100644 hw/i2c/bcm2835_i2c.c create mode 100644 include/hw/i2c/bcm2835_i2c.h
docs/system/arm/raspi.rst +1 -0
@@ -34,6 +34,7 @@ Implemented devices * MailBox controller (MBOX) * VideoCore firmware (property) * Peripheral SPI controller (SPI) + * Broadcom Serial Controller (I2C) Missing devices
hw/i2c/Kconfig +4 -0
@@ -45,3 +45,7 @@ config PCA954X config PMBUS bool select SMBUS + +config BCM2835_I2C + bool + select I2C
hw/i2c/bcm2835_i2c.c new file +282 -0
282 lines not on this page · raw message
hw/i2c/meson.build +1 -0
@@ -17,4 +17,5 @@ i2c_ss.add(when: 'CONFIG_OMAP', if_true: files('omap_i2c.c')) i2c_ss.add(when: 'CONFIG_PPC4XX', if_true: files('ppc4xx_i2c.c')) i2c_ss.add(when: 'CONFIG_PCA954X', if_true: files('i2c_mux_pca954x.c')) i2c_ss.add(when: 'CONFIG_PMBUS', if_true: files('pmbus_device.c')) +i2c_ss.add(when: 'CONFIG_BCM2835_I2C', if_true: files('bcm2835_i2c.c')) system_ss.add_all(when: 'CONFIG_I2C', if_true: i2c_ss)
include/hw/i2c/bcm2835_i2c.h new file +80 -0
80 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 2/3 hw/arm: Connect BSC to BCM2835 board as I2C0, I2C1 and I2C2
In reply to Rayhan FaizelBCM2835 has three I2C controllers. All of them share the same interrupt line.
--- hw/arm/Kconfig | 1 + hw/arm/bcm2835_peripherals.c | 45 ++++++++++++++++++++++++++-- include/hw/arm/bcm2835_peripherals.h | 4 ++- 3 files changed, 46 insertions(+), 4 deletions(-)
hw/arm/Kconfig +1 -0
@@ -430,6 +430,7 @@ config RASPI select SDHCI select USB_DWC2 select BCM2835_SPI + select BCM2835_I2C config STM32F100_SOC bool
hw/arm/bcm2835_peripherals.c +42 -3
68 lines not on this page · raw message
include/hw/arm/bcm2835_peripherals.h +3 -1
@@ -32,6 +32,7 @@#include "hw/timer/bcm2835_systmr.h" #include "hw/usb/hcd-dwc2.h" #include "hw/ssi/bcm2835_spi.h" +#include "hw/i2c/bcm2835_i2c.h" #include "hw/misc/unimp.h" #include "qom/object.h"
@@ -68,7 +69,8 @@ struct BCM2835PeripheralState {Bcm2835ThermalState thermal;UnimplementedDeviceState i2s;BCM2835SPIState spi[1 ]; -UnimplementedDeviceState i2c[3 ]; +BCM2835I2CState i2c[3 ]; +OrIRQState orgated_i2c_irq;UnimplementedDeviceState otp;UnimplementedDeviceState dbus;UnimplementedDeviceState ave0;
Signature · 1 line
2.34.1
PATCH v4 3/3 tests/qtest: Add testcase for BCM2835 BSC
In reply to Rayhan FaizelSimple testcase for validating proper operation of read and write for all three BSC controllers.
--- tests/qtest/bcm2835-i2c-test.c | 115 +++++++++++++++++++++++++++++++++ tests/qtest/meson.build | 2 +- 2 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/bcm2835-i2c-test.c
tests/qtest/bcm2835-i2c-test.c new file +115 -0
@@ -0,0 +1,115 @@ +/* +* QTest testcase for Broadcom Serial Controller (BSC) +* +* Copyright (c) 2024 Rayhan Faizel <[email protected]> +* +* 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 "libqtest-single.h" + +#include "hw/i2c/bcm2835_i2c.h" +#include "hw/sensor/tmp105_regs.h" + +static const uint32_t bsc_base_addrs[] = { +0x3f205000 ,/* I2C0 */ +0x3f804000 ,/* I2C1 */ +0x3f805000 ,/* I2C2 */ +}; + +static void bcm2835_i2c_init_transfer (uint32_t base_addr,bool read) +{ +/* read flag is bit 0 so we can write it directly */ +int interrupt = read ?BCM2835_I2C_C_INTR :BCM2835_I2C_C_INTT ; + +writel (base_addr +BCM2835_I2C_C , +BCM2835_I2C_C_I2CEN |BCM2835_I2C_C_INTD | +BCM2835_I2C_C_ST |BCM2835_I2C_C_CLEAR | interrupt | read); +} + +static void test_i2c_read_write (gconstpointer data) +{ +uint32_t i2cdata; +intptr_t index = (intptr_t ) data; +uint32_t base_addr = bsc_base_addrs[index]; + +/* Write to TMP105 register */ +writel (base_addr +BCM2835_I2C_A ,0x50 ); +writel (base_addr +BCM2835_I2C_DLEN ,3 ); + +bcm2835_i2c_init_transfer (base_addr,0 ); + +writel (base_addr +BCM2835_I2C_FIFO ,TMP105_REG_T_HIGH ); +writel (base_addr +BCM2835_I2C_FIFO ,0xde ); +writel (base_addr +BCM2835_I2C_FIFO ,0xad ); + +/* Clear flags */ +writel (base_addr +BCM2835_I2C_S ,BCM2835_I2C_S_DONE |BCM2835_I2C_S_ERR | +BCM2835_I2C_S_CLKT ); + +/* Read from TMP105 register */ +writel (base_addr +BCM2835_I2C_A ,0x50 ); +writel (base_addr +BCM2835_I2C_DLEN ,1 ); + +bcm2835_i2c_init_transfer (base_addr,0 ); + +writel (base_addr +BCM2835_I2C_FIFO ,TMP105_REG_T_HIGH ); + +writel (base_addr +BCM2835_I2C_DLEN ,2 ); +bcm2835_i2c_init_transfer (base_addr,1 ); + + i2cdata =readl (base_addr +BCM2835_I2C_FIFO ); + g_assert_cmpint(i2cdata, ==, 0xde); + + i2cdata =readl (base_addr +BCM2835_I2C_FIFO ); + g_assert_cmpint(i2cdata, ==, 0xad); + +/* Clear flags */ +writel (base_addr +BCM2835_I2C_S ,BCM2835_I2C_S_DONE |BCM2835_I2C_S_ERR | +BCM2835_I2C_S_CLKT ); + +} + +int main (int argc,char **argv) +{ +int ret; +int i; + +g_test_init (&argc, &argv,NULL ); + +for (i =0 ; i <3 ; i++) { + g_autofree char *test_name = +g_strdup_printf ("/bcm2835/bcm2835-i2c%d/read_write" , i); +qtest_add_data_func (test_name, (void *)(intptr_t ) i, + test_i2c_read_write); + } + +/* Run I2C tests with TMP105 slaves on all three buses */ +qtest_start ("-M raspi3b " +"-device tmp105,address=0x50,bus=i2c-bus.0 " +"-device tmp105,address=0x50,bus=i2c-bus.1 " +"-device tmp105,address=0x50,bus=i2c-bus.2" ); + ret =g_test_run (); +qtest_end (); + +return ret; +}
tests/qtest/meson.build +1 -1
@@ -228,7 +228,7 @@ qtests_aarch64 = \ ['tpm-tis-device-test', 'tpm-tis-device-swtpm-test'] : []) + \ (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test', 'fuzz-xlnx-dp-test'] : []) + \ (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test', 'xlnx-versal-trng-test'] : []) + \ - (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test'] : []) + \ + (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test', 'bcm2835-i2c-test'] : []) + \ (config_all_accel.has_key('CONFIG_TCG') and \ config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test'] : []) + \ (config_all_devices.has_key('CONFIG_NPCM7XX') ? qtests_npcm7xx : []) + \
Signature · 1 line
2.34.1