aspeed: add a witherspoon-bmc machine
v6 cover aspeed: add a witherspoon-bmc machine
Hello,
This series adds a new Aspeed machine to emulate the BMC of a Witherspoon system. It also extends the other Aspeed machines with I2C devices and adds a simple model for the pca9552 LED blinker present on the witherspoon board.
Thanks,
C.
Changes since v5:
- created the ROM memory region even if there is no file backend specified by the user
Changes since v4:
- use a ROM memory region
Changes since v3:
- introduce a dummy ROM device to catch invalid writes
- removed 'ignore_memory_transaction_failures' on all Aspeed machines
- reworked PCA9552 device :
. simpler I2C handles
. auto-increment support
. unit test
Changes since v2:
- removed comments on the I2C buffer size pf the PCA9552 device.
- removed 'ignore_memory_transaction_failures' flag on the witherspoon machine.
Changes since v1:
- introduced smbus_eeprom_init_one()
Cédric Le Goater (8):
aspeed: use a ROM memory region to catch invalid writes
aspeed: remove ignore_memory_transaction_failures on all boards
aspeed: add support for the witherspoon-bmc board
aspeed: add an I2C RTC device to all machines
smbus: add a smbus_eeprom_init_one() routine
aspeed: Add EEPROM I2C devices
misc: add pca9552 LED blinker model
aspeed: add the pc9552 chips to the witherspoon machine
default-configs/arm-softmmu.mak | 1 + hw/arm/aspeed.c | 139 ++++++++++++++++++--- hw/i2c/smbus_eeprom.c | 16 ++- hw/misc/Makefile.objs | 1 + hw/misc/pca9552.c | 259 ++++++++++++++++++++++++++++++++++++++++ include/hw/i2c/smbus.h | 1 + include/hw/misc/pca9552.h | 33 +++++ tests/Makefile.include | 2 + tests/pca9552-test.c | 131 ++++++++++++++++++++ 9 files changed, 560 insertions(+), 23 deletions(-) create mode 100644 hw/misc/pca9552.c create mode 100644 include/hw/misc/pca9552.h create mode 100644 tests/pca9552-test.c
Signature · 1 line
2.13.6
PATCH v6 1/8 aspeed: use a ROM memory region to catch invalid writes
Some legacy firmwares access unimplemented addresses on the Aspeed SoC (old U-Boot code using variables in the bss when it shouldn't do). Let's use a ROM memory region to catch the invalid writes and support new boards without using the 'ignore_memory_transaction_failures' flag.
This change is breaking migration compatibility for the Aspeed machines.
---
Changes since v5 :
- added a comment on migration compatibility
- created the ROM memory region even if there is no file backend specified by the user
Changes since v4 :
- use a ROM memory region
hw/arm/aspeed.c | 51 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-)
hw/arm/aspeed.c +36 -15
@@ -166,12 +166,31 @@ static void aspeed_board_init_flashes(AspeedSMCState *s, const char *flashtype, } } +/* +* This is to track invalid writes done in the ROM by some legacy +* firmwares +*/ +static void boot_rom_write (void *opaque,hwaddr offset,uint64_t value, +unsigned size) +{ +qemu_log_mask (LOG_GUEST_ERROR , +"%s: 0x%" HWADDR_PRIx" <- 0x%" PRIx64" [%u]\n" , + __func__, offset, value, size); +} + +static const MemoryRegionOps boot_rom_ops = { + .write = boot_rom_write, + .endianness =DEVICE_NATIVE_ENDIAN , +}; +static void aspeed_board_init (MachineState *machine,const AspeedBoardConfig *cfg) {AspeedBoardState *bmc;AspeedSoCClass *sc;DriveInfo *drive0 =drive_get (IF_MTD ,0 ,0 ); +MemoryRegion *boot_rom; +size_t boot_rom_size; bmc =g_new0 (AspeedBoardState,1 );object_initialize (&bmc->soc, (sizeof (bmc->soc)), cfg->soc_name);
@@ -205,22 +224,24 @@ static void aspeed_board_init(MachineState *machine,aspeed_board_init_flashes (&bmc->soc.fmc, cfg->fmc_model, &error_abort);aspeed_board_init_flashes (&bmc->soc.spi[0 ], cfg->spi_model, &error_abort); -/* Install first FMC flash content as a boot rom. */ +/* +* create a ROM region using the default mapping window size of +* the FMC CS0 flash module. The window size is 64MB for the +* AST2400 SoC and 128MB for the AST2500 SoC, which is twice as +* big as needed by the flash modules of the Aspeed machines. +*/ + boot_rom =g_new (MemoryRegion,1 ); + boot_rom_size = bmc->soc.fmc.flashes[0 ].size; + +memory_region_init_rom_device (boot_rom,OBJECT (bmc), &boot_rom_ops, +NULL ,"aspeed.boot_rom" , boot_rom_size, + &error_abort); +memory_region_add_subregion (get_system_memory (),FIRMWARE_ADDR , + boot_rom); + +/* Install first FMC flash content (if any) as a boot rom. */ if (drive0) { -AspeedSMCFlash *fl = &bmc->soc.fmc.flashes[0 ]; -MemoryRegion *boot_rom =g_new (MemoryRegion,1 ); - -/* -* create a ROM region using the default mapping window size of -* the flash module. The window size is 64MB for the AST2400 -* SoC and 128MB for the AST2500 SoC, which is twice as big as -* needed by the flash modules of the Aspeed machines. -*/ -memory_region_init_rom_nomigrate (boot_rom,OBJECT (bmc),"aspeed.boot_rom" , - fl->size, &error_abort); -memory_region_add_subregion (get_system_memory (),FIRMWARE_ADDR , - boot_rom); -write_boot_rom (drive0,FIRMWARE_ADDR , fl->size, &error_abort); +write_boot_rom (drive0,FIRMWARE_ADDR , boot_rom_size, &error_abort); } aspeed_board_binfo.kernel_filename = machine->kernel_filename;
Signature · 1 line
2.13.6
PATCH v6 2/8 aspeed: remove ignore_memory_transaction_failures on all boards
In reply to Cédric Le GoaterNow that we have a ROM memory region catching the invalid writes, we can remove this flag and work on fixing the current firwmares still accessing unimplemented addresses.
--- hw/arm/aspeed.c | 3 --- 1 file changed, 3 deletions(-)
hw/arm/aspeed.c +0 -3
@@ -291,7 +291,6 @@ static void palmetto_bmc_class_init(ObjectClass *oc, void *data) mc->no_floppy =1 ; mc->no_cdrom =1 ; mc->no_parallel =1 ; - mc->ignore_memory_transaction_failures = true; }static const TypeInfo palmetto_bmc_type = {
@@ -324,7 +323,6 @@ static void ast2500_evb_class_init(ObjectClass *oc, void *data) mc->no_floppy =1 ; mc->no_cdrom =1 ; mc->no_parallel =1 ; - mc->ignore_memory_transaction_failures = true; }static const TypeInfo ast2500_evb_type = {
@@ -349,7 +347,6 @@ static void romulus_bmc_class_init(ObjectClass *oc, void *data) mc->no_floppy =1 ; mc->no_cdrom =1 ; mc->no_parallel =1 ; - mc->ignore_memory_transaction_failures = true; }static const TypeInfo romulus_bmc_type = {
Signature · 1 line
2.13.6
PATCH v6 3/8 aspeed: add support for the witherspoon-bmc board
In reply to Cédric Le GoaterThe Witherspoon boards are OpenPOWER system hosting POWER9 Processors. Let's add support for their BMC including a couple of I2C devices as found on real HW.
---
Changes since v2:
- removed 'ignore_memory_transaction_failures' flag on the machine.
- Added a comment on the I2C device used.
hw/arm/aspeed.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
hw/arm/aspeed.c +49 -0
@@ -46,6 +46,7 @@ enum {PALMETTO_BMC ,AST2500_EVB ,ROMULUS_BMC , + WITHERSPOON_BMC, };/* Palmetto hardware value: 0x120CE416 */
@@ -83,8 +84,12 @@ enum { SCU_AST2500_HW_STRAP_ACPI_ENABLE | \ SCU_HW_STRAP_SPI_MODE(SCU_HW_STRAP_SPI_MASTER)) +/* Witherspoon hardware value: 0xF10AD216 (but use romulus definition) */ +#define WITHERSPOON_BMC_HW_STRAP1 ROMULUS_BMC_HW_STRAP1 +static void palmetto_bmc_i2c_init (AspeedBoardState *bmc);static void ast2500_evb_i2c_init (AspeedBoardState *bmc); +static void witherspoon_bmc_i2c_init (AspeedBoardState *bmc);static const AspeedBoardConfig aspeed_boards[] = { [PALMETTO_BMC ] = {
@@ -110,6 +115,14 @@ static const AspeedBoardConfig aspeed_boards[] = { .spi_model = "mx66l1g45g", .num_cs = 2, }, + [WITHERSPOON_BMC] = { + .soc_name = "ast2500-a1", + .hw_strap1 = WITHERSPOON_BMC_HW_STRAP1, + .fmc_model = "mx25l25635e", + .spi_model = "mx66l1g45g", + .num_cs = 2, + .i2c_init = witherspoon_bmc_i2c_init, + }, };#define FIRMWARE_ADDR 0x0
@@ -355,11 +368,47 @@ static const TypeInfo romulus_bmc_type = { .class_init = romulus_bmc_class_init, }; +static void witherspoon_bmc_i2c_init (AspeedBoardState *bmc) +{ +AspeedSoCState *soc = &bmc->soc; + +i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),4 ),"tmp423" ,0x4c ); +i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),5 ),"tmp423" ,0x4c ); + +/* The Witherspoon expects a TMP275 but a TMP105 is compatible */ +i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),9 ),"tmp105" ,0x4a ); +} + +static void witherspoon_bmc_init (MachineState *machine) +{ +aspeed_board_init (machine, &aspeed_boards[WITHERSPOON_BMC ]); +} + +static void witherspoon_bmc_class_init (ObjectClass *oc,void *data) +{ +MachineClass *mc =MACHINE_CLASS (oc); + + mc->desc ="OpenPOWER Witherspoon BMC (ARM1176)" ; + mc->init = witherspoon_bmc_init; + mc->max_cpus =1 ; + mc->no_sdcard =1 ; + mc->no_floppy =1 ; + mc->no_cdrom =1 ; + mc->no_parallel =1 ; +} + +static const TypeInfo witherspoon_bmc_type = { + .name =MACHINE_TYPE_NAME ("witherspoon-bmc" ), + .parent =TYPE_MACHINE , + .class_init = witherspoon_bmc_class_init, +}; +static void aspeed_machine_init (void ) {type_register_static (&palmetto_bmc_type);type_register_static (&ast2500_evb_type);type_register_static (&romulus_bmc_type); +type_register_static (&witherspoon_bmc_type); }type_init (aspeed_machine_init)
Signature · 1 line
2.13.6
PATCH v6 4/8 aspeed: add an I2C RTC device to all machines
In reply to Cédric Le GoaterThe AST2500 EVB does not have an RTC but we can pretend that one is plugged on the I2C bus header.
The romulus and witherspoon boards expects an Epson RX8900 I2C RTC but a ds1338 is good enough for the basic features we need.
--- hw/arm/aspeed.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
hw/arm/aspeed.c +19 -0
@@ -89,6 +89,7 @@ enum {static void palmetto_bmc_i2c_init (AspeedBoardState *bmc);static void ast2500_evb_i2c_init (AspeedBoardState *bmc); +static void romulus_bmc_i2c_init (AspeedBoardState *bmc);static void witherspoon_bmc_i2c_init (AspeedBoardState *bmc);static const AspeedBoardConfig aspeed_boards[] = {
@@ -114,6 +115,7 @@ static const AspeedBoardConfig aspeed_boards[] = { .fmc_model = "n25q256a", .spi_model = "mx66l1g45g", .num_cs = 2, + .i2c_init = romulus_bmc_i2c_init, }, [WITHERSPOON_BMC] = { .soc_name = "ast2500-a1",
@@ -318,6 +320,10 @@ static void ast2500_evb_i2c_init(AspeedBoardState *bmc)/* The AST2500 EVB expects a LM75 but a TMP105 is compatible */ i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),7 ),"tmp105" ,0x4d ); + +/* The AST2500 EVB does not have an RTC. Let's pretend that one is +* plugged on the I2C bus header */ +i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),11 ),"ds1338" ,0x32 ); }static void ast2500_evb_init (MachineState *machine)
@@ -344,6 +350,15 @@ static const TypeInfo ast2500_evb_type = { .class_init = ast2500_evb_class_init, }; +static void romulus_bmc_i2c_init (AspeedBoardState *bmc) +{ +AspeedSoCState *soc = &bmc->soc; + +/* The romulus board expects Epson RX8900 I2C RTC but a ds1338 is +* good enough */ +i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),11 ),"ds1338" ,0x32 ); +} +static void romulus_bmc_init (MachineState *machine) {aspeed_board_init (machine, &aspeed_boards[ROMULUS_BMC ]);
@@ -377,6 +392,10 @@ static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc)/* The Witherspoon expects a TMP275 but a TMP105 is compatible */ i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),9 ),"tmp105" ,0x4a ); + +/* The witherspoon board expects Epson RX8900 I2C RTC but a ds1338 is +* good enough */ +i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),11 ),"ds1338" ,0x32 ); }static void witherspoon_bmc_init (MachineState *machine)
Signature · 1 line
2.13.6
PATCH v6 5/8 smbus: add a smbus_eeprom_init_one() routine
In reply to Cédric Le GoaterThis is an helper routine to add a single EEPROM on an I2C bus. It can be directly used by smbus_eeprom_init() which adds a certain number of EEPROMs on mips and x86 machines.
--- hw/i2c/smbus_eeprom.c | 16 +++++++++++----- include/hw/i2c/smbus.h | 1 + 2 files changed, 12 insertions(+), 5 deletions(-)
hw/i2c/smbus_eeprom.c +11 -5
@@ -140,6 +140,16 @@ static void smbus_eeprom_register_types(void)type_init (smbus_eeprom_register_types) +void smbus_eeprom_init_one(I2CBus *smbus, uint8_t address, uint8_t *eeprom_buf) +{ +DeviceState *dev; + + dev =qdev_create ((BusState *) smbus,"smbus-eeprom" ); +qdev_prop_set_uint8 (dev,"address" , address); +qdev_prop_set_ptr (dev,"data" , eeprom_buf); +qdev_init_nofail (dev); +} +void smbus_eeprom_init (I2CBus *smbus,int nb_eeprom,const uint8_t *eeprom_spd,int eeprom_spd_size) {
@@ -150,10 +160,6 @@ void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom, }for (i =0 ; i < nb_eeprom; i++) { -DeviceState *eeprom; - eeprom =qdev_create ((BusState *)smbus,"smbus-eeprom" ); -qdev_prop_set_uint8 (eeprom,"address" ,0x50 + i); -qdev_prop_set_ptr (eeprom,"data" , eeprom_buf + (i *256 )); -qdev_init_nofail (eeprom); +smbus_eeprom_init_one (smbus,0x50 + i, eeprom_buf + (i *256 )); } }
include/hw/i2c/smbus.h +1 -0
@@ -77,6 +77,7 @@ int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data);int smbus_write_block (I2CBus *bus,uint8_t addr,uint8_t command,uint8_t *data,int len); +void smbus_eeprom_init_one (I2CBus *smbus,uint8_t address,uint8_t *eeprom_buf);void smbus_eeprom_init (I2CBus *smbus,int nb_eeprom,const uint8_t *eeprom_spd,int size);
Signature · 1 line
2.13.6
PATCH v6 6/8 aspeed: Add EEPROM I2C devices
In reply to Cédric Le GoaterThe Aspeed boards have at least one EEPROM to hold the Vital Product Data (VPD).
--- hw/arm/aspeed.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
hw/arm/aspeed.c +13 -0
@@ -17,6 +17,7 @@#include "hw/arm/arm.h" #include "hw/arm/aspeed_soc.h" #include "hw/boards.h" +#include "hw/i2c/smbus.h" #include "qemu/log.h" #include "sysemu/block-backend.h" #include "sysemu/blockdev.h"
@@ -276,11 +277,15 @@ static void palmetto_bmc_i2c_init(AspeedBoardState *bmc) {AspeedSoCState *soc = &bmc->soc;DeviceState *dev; +uint8_t *eeprom_buf =g_malloc0 (32 *1024 );/* The palmetto platform expects a ds3231 RTC but a ds1338 is * enough to provide basic RTC features. Alarms will be missing */ i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),0 ),"ds1338" ,0x68 ); +smbus_eeprom_init_one (aspeed_i2c_get_bus (DEVICE (&soc->i2c),0 ),0x50 , + eeprom_buf); +/* add a TMP423 temperature sensor */ dev =i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),2 ),"tmp423" ,0x4c );
@@ -317,6 +322,10 @@ static const TypeInfo palmetto_bmc_type = {static void ast2500_evb_i2c_init (AspeedBoardState *bmc) {AspeedSoCState *soc = &bmc->soc; +uint8_t *eeprom_buf =g_malloc0 (8 *1024 ); + +smbus_eeprom_init_one (aspeed_i2c_get_bus (DEVICE (&soc->i2c),3 ),0x50 , + eeprom_buf);/* The AST2500 EVB expects a LM75 but a TMP105 is compatible */ i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),7 ),"tmp105" ,0x4d );
@@ -386,6 +395,7 @@ static const TypeInfo romulus_bmc_type = {static void witherspoon_bmc_i2c_init (AspeedBoardState *bmc) {AspeedSoCState *soc = &bmc->soc; +uint8_t *eeprom_buf =g_malloc0 (8 *1024 );i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),4 ),"tmp423" ,0x4c );i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),5 ),"tmp423" ,0x4c );
@@ -396,6 +406,9 @@ static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc)/* The witherspoon board expects Epson RX8900 I2C RTC but a ds1338 is * good enough */ i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),11 ),"ds1338" ,0x32 ); + +smbus_eeprom_init_one (aspeed_i2c_get_bus (DEVICE (&soc->i2c),11 ),0x51 , + eeprom_buf); }static void witherspoon_bmc_init (MachineState *machine)
Signature · 1 line
2.13.6
PATCH v6 7/8 misc: add pca9552 LED blinker model
In reply to Cédric Le GoaterSpecs are available here :
https://www.nxp.com/docs/en/application-note/AN264.pdf
This is a simple model supporting the basic registers for led and GPIO mode. The device also supports two blinking rates but not the model yet.
---
Changes since v3:
- introduced auto-increment support
- removed the buffer collecting bytes on the bus
- improved reset
- used extract32
- added a unit test
Changes since v2:
- removed comments on the I2C buffer size, but kept the array. I did not want to rewrite the buffer handling
default-configs/arm-softmmu.mak | 1 + hw/misc/Makefile.objs | 1 + hw/misc/pca9552.c | 259 ++++++++++++++++++++++++++++++++++++++++ include/hw/misc/pca9552.h | 33 +++++ tests/Makefile.include | 2 + tests/pca9552-test.c | 131 ++++++++++++++++++++ 6 files changed, 427 insertions(+) create mode 100644 hw/misc/pca9552.c create mode 100644 include/hw/misc/pca9552.h create mode 100644 tests/pca9552-test.c
default-configs/arm-softmmu.mak +1 -0
@@ -16,6 +16,7 @@ CONFIG_TSC2005=y CONFIG_LM832X=y CONFIG_TMP105=y CONFIG_TMP421=y +CONFIG_PCA9552=y CONFIG_STELLARIS=y CONFIG_STELLARIS_INPUT=y CONFIG_STELLARIS_ENET=y
hw/misc/Makefile.objs +1 -0
@@ -7,6 +7,7 @@ common-obj-$(CONFIG_SGA) += sga.o common-obj-$(CONFIG_ISA_TESTDEV) += pc-testdev.o common-obj-$(CONFIG_PCI_TESTDEV) += pci-testdev.o common-obj-$(CONFIG_EDU) += edu.o +common-obj-$(CONFIG_PCA9552) += pca9552.o common-obj-y += unimp.o
hw/misc/pca9552.c new file +259 -0
259 lines not on this page · raw message
include/hw/misc/pca9552.h new file +33 -0
@@ -0,0 +1,33 @@ +/* +* PCA9552 I2C LED blinker +* +* Copyright (c) 2017, IBM Corporation. +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ +#ifndef PCA9552_H +#define PCA9552_H + +#include "hw/i2c/i2c.h" + +#define TYPE_PCA9552 "pca9552" +#define PCA9552 (obj) OBJECT_CHECK(PCA9552State, (obj), TYPE_PCA9552) + + +#define PCA9552_NR_REGS 10 + +typedef struct PCA9552State { +/*< private >*/ +I2CSlave i2c; +/*< public >*/ + +uint8_t len; +uint8_t pointer; + +uint8_t regs[PCA9552_NR_REGS ]; +uint8_t max_reg; +uint8_t nr_leds; +}PCA9552State ; + +#endif
tests/Makefile.include +2 -0
@@ -347,6 +347,7 @@ check-qtest-sparc64-y = tests/endianness-test$(EXESUF) check-qtest-sparc64-y += tests/prom-env-test$(EXESUF) check-qtest-arm-y = tests/tmp105-test$(EXESUF) +check-qtest-arm-y += tests/pca9552-test$(EXESUF) check-qtest-arm-y += tests/ds1338-test$(EXESUF) check-qtest-arm-y += tests/m25p80-test$(EXESUF) gcov-files-arm-y += hw/misc/tmp105.c
@@ -739,6 +740,7 @@ tests/bios-tables-test$(EXESUF): tests/bios-tables-test.o \ tests/boot-sector.o tests/acpi-utils.o $(libqos-obj-y) tests/pxe-test$(EXESUF): tests/pxe-test.o tests/boot-sector.o $(libqos-obj-y) tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y) +tests/pca9552-test$(EXESUF): tests/pca9552-test.o $(libqos-omap-obj-y) tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y) tests/m25p80-test$(EXESUF): tests/m25p80-test.o tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y)
tests/pca9552-test.c new file +131 -0
131 lines not on this page · raw message
Signature · 1 line
2.13.6
PATCH v6 8/8 aspeed: add the pc9552 chips to the witherspoon machine
In reply to Cédric Le GoaterThe pca9552 LED blinkers on the Witherspoon machine are used for leds but also as GPIOs to control fans and GPUs.
--- hw/arm/aspeed.c | 4 ++++ 1 file changed, 4 insertions(+)
hw/arm/aspeed.c +4 -0
@@ -397,6 +397,8 @@ static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc)AspeedSoCState *soc = &bmc->soc;uint8_t *eeprom_buf =g_malloc0 (8 *1024 ); +i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),3 ),"pca9552" ,0x60 ); +i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),4 ),"tmp423" ,0x4c );i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),5 ),"tmp423" ,0x4c );
@@ -409,6 +411,8 @@ static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc)smbus_eeprom_init_one (aspeed_i2c_get_bus (DEVICE (&soc->i2c),11 ),0x51 , eeprom_buf); +i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),11 ),"pca9552" , +0x60 ); }static void witherspoon_bmc_init (MachineState *machine)
Signature · 1 line
2.13.6
Re: [Qemu-arm] [PATCH v6 7/8] misc: add pca9552 LED blinker model
In reply to Cédric Le GoaterHi Cédric,
Quoted 22 lines · Cédric Le Goater
Specs are available here :
https://www.nxp.com/docs/en/application-note/AN264.pdfThis is a simple model supporting the basic registers for led and GPIO mode. The device also supports two blinking rates but not the model yet.
Signed-off-by Cédric Le Goater <[email protected]>Reviewed-by Peter Maydell <[email protected]>---
Changes since v3:
- introduced auto-increment support
- removed the buffer collecting bytes on the bus
- improved reset
- used extract32
- added a unit testChanges since v2:
Quoted 7 lines
- removed comments on the I2C buffer size, but kept the array. I did not want to rewrite the buffer handling
default-configs/arm-softmmu.mak | 1 + hw/misc/Makefile.objs | 1 + hw/misc/pca9552.c | 259 ++++++++++++++++++++++++++++++++++++++++ include/hw/misc/pca9552.h | 33 +++++
If you mind using scripts/git.orderfile the review'd get easier :)
Quoted 56 lines
tests/Makefile.include | 2 + tests/pca9552-test.c | 131 ++++++++++++++++++++ 6 files changed, 427 insertions(+) create mode 100644 hw/misc/pca9552.c create mode 100644 include/hw/misc/pca9552.h create mode 100644 tests/pca9552-test.cdefault-configs/arm-softmmu.mak +1
-0@@ -16,6 +16,7 @@ CONFIG_TSC2005=y CONFIG_LM832X=y CONFIG_TMP105=y CONFIG_TMP421=y +CONFIG_PCA9552=y CONFIG_STELLARIS=y CONFIG_STELLARIS_INPUT=y CONFIG_STELLARIS_ENET=yhw/misc/Makefile.objs +1
-0@@ -7,6 +7,7 @@ common-obj-$(CONFIG_SGA) += sga.o common-obj-$(CONFIG_ISA_TESTDEV) += pc-testdev.o common-obj-$(CONFIG_PCI_TESTDEV) += pci-testdev.o common-obj-$(CONFIG_EDU) += edu.o +common-obj-$(CONFIG_PCA9552) += pca9552.o common-obj-y += unimp.ohw/misc/pca9552.c new file +19
-0@@ -0,0 +1,259 @@ +/* +* PCA9552 I2C LED blinker +* +* https://www.nxp.com/docs/en/application-note/AN264.pdf +* +* Copyright (c) 2017, IBM Corporation. +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "hw/hw.h" +#include "hw/misc/pca9552.h" + +/* +* Bits [0:3] are used to address a specific register. +*/
hw/misc/pca9552.c line 29
Quoted 10 lines
hw/misc/pca9552.c new file +10
-0+#define PCA9552_INPUT0 0/* read only input register 0 */ +#define PCA9552_INPUT1 1/* read only input register 1 */ +#define PCA9552_PSC0 2/* read/write frequency prescaler 0 */ +#define PCA9552_PWM0 3/* read/write PWM register 0 */ +#define PCA9552_PSC1 4/* read/write frequency prescaler 1 */ +#define PCA9552_PWM1 5/* read/write PWM register 1 */ +#define PCA9552_LS0 6/* read/write LED0 to LED3 selector */ +#define PCA9552_LS1 7/* read/write LED4 to LED7 selector */ +#define PCA9552_LS2 8/* read/write LED8 to LED11 selector */ +#define PCA9552_LS3 9/* read/write LED12 to LED15 selector */
Since you use those in your test, can you move them to "hw/misc/pca9552.h"?
hw/misc/pca9552.c line 35
Quoted 6 lines
hw/misc/pca9552.c new file +6
-0+ +/* +* Bit [4] is used to activate the Auto-Increment option of the +* register address +*/ +#define PCA9552_AUTOINC (1 << 4)
ditto
Quoted 185 lines
hw/misc/pca9552.c new file +185
-0+ +#define PCA9552_LED_ON 0x0 +#define PCA9552_LED_OFF 0x1 +#define PCA9552_LED_PWM0 0x2 +#define PCA9552_LED_PWM1 0x3 + +static uint8_t pca9552_pin_get_config (PCA9552State *s,int pin) +{ +uint8_t reg =PCA9552_LS0 + (pin /4 ); +uint8_t shift = (pin %4 ) <<1 ; + +return extract32 (s->regs[reg], shift,2 ); +} + +static void pca9552_update_pin_input (PCA9552State *s) +{ +int i; + +for (i =0 ; i < s->nr_leds; i++) { +uint8_t input_reg =PCA9552_INPUT0 + (i /8 ); +uint8_t input_shift = (i %8 ); +uint8_t config =pca9552_pin_get_config (s, i); + +switch (config) { +case PCA9552_LED_ON : + s->regs[input_reg] |=1 << input_shift; +break ; +case PCA9552_LED_OFF : + s->regs[input_reg] &= ~(1 << input_shift); +break ; +case PCA9552_LED_PWM0 : +case PCA9552_LED_PWM1 : +/* TODO */ +default : +break ; + } + } +} + +static uint8_t pca9552_read (PCA9552State *s,uint8_t reg) +{ +switch (reg) { +case PCA9552_INPUT0 : +case PCA9552_INPUT1 : +case PCA9552_PSC0 : +case PCA9552_PWM0 : +case PCA9552_PSC1 : +case PCA9552_PWM1 : +case PCA9552_LS0 : +case PCA9552_LS1 : +case PCA9552_LS2 : +case PCA9552_LS3 : +return s->regs[reg]; +default : +qemu_log_mask (LOG_GUEST_ERROR ,"%s: unexpected read to register %d\n" , + __func__, reg); +return 0xFF ; + } +} + +static void pca9552_write (PCA9552State *s,uint8_t reg,uint8_t data) +{ +switch (reg) { +case PCA9552_PSC0 : +case PCA9552_PWM0 : +case PCA9552_PSC1 : +case PCA9552_PWM1 : + s->regs[reg] = data; +break ; + +case PCA9552_LS0 : +case PCA9552_LS1 : +case PCA9552_LS2 : +case PCA9552_LS3 : + s->regs[reg] = data; +pca9552_update_pin_input (s); +break ; + +case PCA9552_INPUT0 : +case PCA9552_INPUT1 : +default : +qemu_log_mask (LOG_GUEST_ERROR ,"%s: unexpected write to register %d\n" , + __func__, reg); + } +} + +/* +* When Auto-Increment is on, the register address is incremented +* after each byte is sent to or received by the device. The index +* rollovers to 0 when the maximum register address is reached. +*/ +static void pca9552_autoinc (PCA9552State *s) +{ +if (s->pointer !=0xFF && s->pointer &PCA9552_AUTOINC ) { +uint8_t reg = s->pointer &0xf ; + + reg = (reg +1 ) % (s->max_reg +1 ); + s->pointer = reg |PCA9552_AUTOINC ; + } +} + +static int pca9552_recv (I2CSlave *i2c) +{ +PCA9552State *s =PCA9552 (i2c); +uint8_t ret; + + ret =pca9552_read (s, s->pointer &0xf ); + +/* +* From the Specs: +* +* Important Note: When a Read sequence is initiated and the +* AI bit is set to Logic Level 1, the Read Sequence MUST +* start by a register different from 0. +* +* I don't know what should be done in this case, so throw an +* error. +*/ +if (s->pointer ==PCA9552_AUTOINC ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Autoincrement read starting with register 0\n" , + __func__); + } + +pca9552_autoinc (s); + +return ret; +} + +static int pca9552_send (I2CSlave *i2c,uint8_t data) +{ +PCA9552State *s =PCA9552 (i2c); + +/* First byte sent by is the register address */ +if (s->len ==0 ) { + s->pointer = data; + s->len++; + }else { +pca9552_write (s, s->pointer &0xf , data); + +pca9552_autoinc (s); + } + +return 0 ; +} + +static int pca9552_event (I2CSlave *i2c,enum i2c_event event) +{ +PCA9552State *s =PCA9552 (i2c); + + s->len =0 ; +return 0 ; +} + +static const VMStateDescription pca9552_vmstate = { + .name ="PCA9552" , + .version_id =0 , + .minimum_version_id =0 , + .fields = (VMStateField []) { +VMSTATE_UINT8 (len, PCA9552State), +VMSTATE_UINT8 (pointer, PCA9552State), +VMSTATE_UINT8_ARRAY (regs, PCA9552State,PCA9552_NR_REGS ), +VMSTATE_I2C_SLAVE (i2c, PCA9552State), +VMSTATE_END_OF_LIST () + } +}; + +static void pca9552_reset (DeviceState *dev) +{ +PCA9552State *s =PCA9552 (dev); + + s->regs[PCA9552_PSC0 ] =0xFF ; + s->regs[PCA9552_PWM0 ] =0x80 ; + s->regs[PCA9552_PSC1 ] =0xFF ; + s->regs[PCA9552_PWM1 ] =0x80 ; + s->regs[PCA9552_LS0 ] =0x55 ;/* all OFF */ + s->regs[PCA9552_LS1 ] =0x55 ; + s->regs[PCA9552_LS2 ] =0x55 ; + s->regs[PCA9552_LS3 ] =0x55 ; + +pca9552_update_pin_input (s); + + s->pointer =0xFF ; + s->len =0 ; +}
hw/misc/pca9552.c line 230
Quoted 10 lines
hw/misc/pca9552.c new file +10
-0+ +static void pca9552_initfn (Object *obj) +{ +PCA9552State *s =PCA9552 (obj); + +/* If support for the other PCA955X devices are implemented, these +* constant values might be part of class structure describing the +* PCA955X device +*/ + s->max_reg =PCA9552_LS3 ;
Looks a bit weird, why not use PCA9552_NR_REGS here so pca9552_autoinc() becomes simply:
reg += 1;
reg %= s->max_reg;?
Quoted 43 lines
hw/misc/pca9552.c new file +29
-0+ s->nr_leds =16 ; +} + +static void pca9552_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); +I2CSlaveClass *k =I2C_SLAVE_CLASS (klass); + + k->event = pca9552_event; + k->recv = pca9552_recv; + k->send = pca9552_send; + dc->reset = pca9552_reset; + dc->vmsd = &pca9552_vmstate; +} + +static const TypeInfo pca9552_info = { + .name =TYPE_PCA9552 , + .parent =TYPE_I2C_SLAVE , + .instance_init = pca9552_initfn, + .instance_size =sizeof (PCA9552State), + .class_init = pca9552_class_init, +}; + +static void pca9552_register_types (void ) +{ +type_register_static (&pca9552_info); +} + +type_init (pca9552_register_types)include/hw/misc/pca9552.h new file +8
-0@@ -0,0 +1,33 @@ +/* +* PCA9552 I2C LED blinker +* +* Copyright (c) 2017, IBM Corporation. +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/
include/hw/misc/pca9552.h line 18
Quoted 10 lines
include/hw/misc/pca9552.h new file +10
-0+#ifndef PCA9552_H +#define PCA9552_H + +#include "hw/i2c/i2c.h" + +#define TYPE_PCA9552 "pca9552" +#define PCA9552 (obj) OBJECT_CHECK(PCA9552State, (obj), TYPE_PCA9552) + + +#define PCA9552_NR_REGS 10
This could be an enum (or not, matter of taste).
Quoted 42 lines
include/hw/misc/pca9552.h new file +15
-0+ +typedef struct PCA9552State { +/*< private >*/ +I2CSlave i2c; +/*< public >*/ + +uint8_t len; +uint8_t pointer; + +uint8_t regs[PCA9552_NR_REGS ]; +uint8_t max_reg; +uint8_t nr_leds; +}PCA9552State ; + +#endif tests/Makefile.include +2
-0@@ -347,6 +347,7 @@ check-qtest-sparc64-y = tests/endianness-test$(EXESUF) check-qtest-sparc64-y += tests/prom-env-test$(EXESUF) check-qtest-arm-y = tests/tmp105-test$(EXESUF) +check-qtest-arm-y += tests/pca9552-test$(EXESUF) check-qtest-arm-y += tests/ds1338-test$(EXESUF) check-qtest-arm-y += tests/m25p80-test$(EXESUF) gcov-files-arm-y += hw/misc/tmp105.c@@ -739,6 +740,7 @@ tests/bios-tables-test$(EXESUF): tests/bios-tables-test.o \ tests/boot-sector.o tests/acpi-utils.o $(libqos-obj-y) tests/pxe-test$(EXESUF): tests/pxe-test.o tests/boot-sector.o $(libqos-obj-y) tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y) +tests/pca9552-test$(EXESUF): tests/pca9552-test.o $(libqos-omap-obj-y) tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y) tests/m25p80-test$(EXESUF): tests/m25p80-test.o tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y)tests/pca9552-test.c new file +1
-0@@ -0,0 +1,131 @@ +/*
tests/pca9552-test.c line 11
Quoted 10 lines
tests/pca9552-test.c new file +10
-0+* QTest testcase for the PCA9552 LED blinker +* +* Copyright (c) 2017, IBM Corporation. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +
#include "hw/misc/pca9552.h"
Quoted 8 lines
tests/pca9552-test.c new file +8
-0+#include "libqtest.h" +#include "libqos/i2c.h" + +#define PCA9552_INPUT0 0/* read only input register 0 */ +#define PCA9552_INPUT1 1/* read only input register 1 */ +#define PCA9552_PSC0 2/* read/write frequency prescaler 0 */ +#define PCA9552_PWM0 3/* read/write PWM register 0 */ +#define PCA9552_PSC1 4/* read/write frequency prescaler 1 */
tests/pca9552-test.c line 29
Quoted 10 lines
tests/pca9552-test.c new file +10
-0+#define PCA9552_PWM1 5/* read/write PWM register 1 */ +#define PCA9552_LS0 6/* read/write LED0 to LED3 selector */ +#define PCA9552_LS1 7/* read/write LED4 to LED7 selector */ +#define PCA9552_LS2 8/* read/write LED8 to LED11 selector */ +#define PCA9552_LS3 9/* read/write LED12 to LED15 selector */ + +#define PCA9552_AUTOINC (1 << 4) + + +#define OMAP2_I2C_1_BASE 0x48070000
Can you define that in "hw/arm/omap.h" ?
Quoted 93 lines
tests/pca9552-test.c new file +92
-0+ +#define PCA9552_TEST_ID "pca9552-test" +#define PCA9552_TEST_ADDR 0x60 + +static I2CAdapter *i2c; + +static uint8_t pca9552_get8 (I2CAdapter *i2c,uint8_t addr,uint8_t reg) +{ +uint8_t resp[1 ]; +i2c_send (i2c, addr, ®,1 ); +i2c_recv (i2c, addr, resp,1 ); +return resp[0 ]; +} + +static void pca9552_set8 (I2CAdapter *i2c,uint8_t addr,uint8_t reg, +uint8_t value) +{ +uint8_t cmd[2 ]; +uint8_t resp[1 ]; + + cmd[0 ] = reg; + cmd[1 ] = value; +i2c_send (i2c, addr, cmd,2 ); +i2c_recv (i2c, addr, resp,1 ); + g_assert_cmphex(resp[0], ==, cmd[1]); +} + +static void receive_autoinc (void ) +{ +uint8_t resp; +uint8_t reg =PCA9552_LS0 |PCA9552_AUTOINC ; + +i2c_send (i2c,PCA9552_TEST_ADDR , ®,1 ); + +/* PCA9552_LS0 */ +i2c_recv (i2c,PCA9552_TEST_ADDR , &resp,1 ); + g_assert_cmphex(resp, ==, 0x54); + +/* PCA9552_LS1 */ +i2c_recv (i2c,PCA9552_TEST_ADDR , &resp,1 ); + g_assert_cmphex(resp, ==, 0x55); + +/* PCA9552_LS2 */ +i2c_recv (i2c,PCA9552_TEST_ADDR , &resp,1 ); + g_assert_cmphex(resp, ==, 0x55); + +/* PCA9552_LS3 */ +i2c_recv (i2c,PCA9552_TEST_ADDR , &resp,1 ); + g_assert_cmphex(resp, ==, 0x54); +} + +static void send_and_receive (void ) +{ +uint8_t value; + + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_LS0 ); + g_assert_cmphex(value, ==, 0x55); + + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_INPUT0 ); + g_assert_cmphex(value, ==, 0x0); + +/* Switch on LED 0 */ +pca9552_set8 (i2c,PCA9552_TEST_ADDR ,PCA9552_LS0 ,0x54 ); + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_LS0 ); + g_assert_cmphex(value, ==, 0x54); + + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_INPUT0 ); + g_assert_cmphex(value, ==, 0x01); + +/* Switch on LED 12 */ +pca9552_set8 (i2c,PCA9552_TEST_ADDR ,PCA9552_LS3 ,0x54 ); + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_LS3 ); + g_assert_cmphex(value, ==, 0x54); + + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_INPUT1 ); + g_assert_cmphex(value, ==, 0x10); +} + +int main (int argc,char **argv) +{ +QTestState *s =NULL ; +int ret; + +g_test_init (&argc, &argv,NULL ); + + s =qtest_start ("-machine n800 " +"-device pca9552,bus=i2c-bus.0,id=" PCA9552_TEST_ID +",address=0x60" ); + i2c =omap_i2c_create (OMAP2_I2C_1_BASE ); + +qtest_add_func ("/pca9552/tx-rx" , send_and_receive); +qtest_add_func ("/pca9552/rx-autoinc" , receive_autoinc);
tests/pca9552-test.c line 131
Quoted 10 lines
tests/pca9552-test.c new file +10
-0+ + ret =g_test_run (); + +if (s) { +qtest_quit (s); + } +g_free (i2c); + +return ret; +}
If Peter accept to queue 1-6, I'd appreciate if you can address my few suggestions then resend 7-8; else if you don't have time:
Regards,
Phil.
Re: [Qemu-arm] [PATCH v6 8/8] aspeed: add the pc9552 chips to the witherspoon machine
In reply to Cédric Le GoaterQuoted 14 lines · Cédric Le Goater
The pca9552 LED blinkers on the Witherspoon machine are used for leds but also as GPIOs to control fans and GPUs.
Signed-off-by Cédric Le Goater <[email protected]>Reviewed-by Andrew Jeffery <[email protected]>--- hw/arm/aspeed.c | 4 ++++ 1 file changed, 4 insertions(+)hw/arm/aspeed.c +0
-0
hw/arm/aspeed.c static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc) · line 400
Quoted 4 lines
hw/arm/aspeed.c +1
-0@@ -397,6 +397,8 @@ static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc)AspeedSoCState *soc = &bmc->soc;uint8_t *eeprom_buf =g_malloc0 (8 *1024 ); +i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),3 ),"pca9552" ,0x60 );
TYPE_PCA9552 ?
hw/arm/aspeed.c static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc) · line 414
Quoted 9 lines
hw/arm/aspeed.c +2
-0+i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),4 ),"tmp423" ,0x4c );i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),5 ),"tmp423" ,0x4c );@@ -409,6 +411,8 @@ static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc)smbus_eeprom_init_one (aspeed_i2c_get_bus (DEVICE (&soc->i2c),11 ),0x51 , eeprom_buf); + i2c_create_slave(aspeed_i2c_get_bus(DEVICE(&soc->i2c), 11), "pca9552",
ditto
hw/arm/aspeed.c static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc) · line 418
Quoted 5 lines
hw/arm/aspeed.c +1
-0+ 0x60); }static void witherspoon_bmc_init (MachineState *machine)
Re: [Qemu-arm] [PATCH v6 7/8] misc: add pca9552 LED blinker model
In reply to Philippe Mathieu-DaudéQuoted 28 lines · Philippe Mathieu-Daudé
Hi Cédric,
Quoted 25 lines · Cédric Le Goater
Specs are available here :
https://www.nxp.com/docs/en/application-note/AN264.pdfThis is a simple model supporting the basic registers for led and GPIO mode. The device also supports two blinking rates but not the model yet.
Signed-off-by Cédric Le Goater <[email protected]>Reviewed-by Peter Maydell <[email protected]>---
Changes since v3:
- introduced auto-increment support
- removed the buffer collecting bytes on the bus
- improved reset
- used extract32
- added a unit testChanges since v2:
- removed comments on the I2C buffer size, but kept the array. I did not want to rewrite the buffer handling
Quoted 6 lines
Quoted 4 lines
default-configs/arm-softmmu.mak | 1 + hw/misc/Makefile.objs | 1 + hw/misc/pca9552.c | 259 ++++++++++++++++++++++++++++++++++++++++ include/hw/misc/pca9552.h | 33 +++++If you mind using
scripts/git.orderfilethe review'd get easier :)
I didn't know about it. I will take a look.
Quoted 60 lines
Quoted 60 lines
tests/Makefile.include | 2 + tests/pca9552-test.c | 131 ++++++++++++++++++++ 6 files changed, 427 insertions(+) create mode 100644 hw/misc/pca9552.c create mode 100644 include/hw/misc/pca9552.h create mode 100644 tests/pca9552-test.cdefault-configs/arm-softmmu.mak +1
-0@@ -16,6 +16,7 @@ CONFIG_TSC2005=y CONFIG_LM832X=y CONFIG_TMP105=y CONFIG_TMP421=y +CONFIG_PCA9552=y CONFIG_STELLARIS=y CONFIG_STELLARIS_INPUT=y CONFIG_STELLARIS_ENET=yhw/misc/Makefile.objs +1
-0@@ -7,6 +7,7 @@ common-obj-$(CONFIG_SGA) += sga.o common-obj-$(CONFIG_ISA_TESTDEV) += pc-testdev.o common-obj-$(CONFIG_PCI_TESTDEV) += pci-testdev.o common-obj-$(CONFIG_EDU) += edu.o +common-obj-$(CONFIG_PCA9552) += pca9552.o common-obj-y += unimp.ohw/misc/pca9552.c new file +23
-0@@ -0,0 +1,259 @@ +/* +* PCA9552 I2C LED blinker +* +* https://www.nxp.com/docs/en/application-note/AN264.pdf +* +* Copyright (c) 2017, IBM Corporation. +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "hw/hw.h" +#include "hw/misc/pca9552.h" + +/* +* Bits [0:3] are used to address a specific register. +*/ +#define PCA9552_INPUT0 0/* read only input register 0 */ +#define PCA9552_INPUT1 1/* read only input register 1 */ +#define PCA9552_PSC0 2/* read/write frequency prescaler 0 */ +#define PCA9552_PWM0 3/* read/write PWM register 0 */
Quoted 8 lines
hw/misc/pca9552.c line 29
Quoted 6 lines
hw/misc/pca9552.c new file +6
-0+#define PCA9552_PSC1 4/* read/write frequency prescaler 1 */ +#define PCA9552_PWM1 5/* read/write PWM register 1 */ +#define PCA9552_LS0 6/* read/write LED0 to LED3 selector */ +#define PCA9552_LS1 7/* read/write LED4 to LED7 selector */ +#define PCA9552_LS2 8/* read/write LED8 to LED11 selector */ +#define PCA9552_LS3 9/* read/write LED12 to LED15 selector */ Since you use those in your test, can you move them to "
hw/misc/pca9552.h"?
yes I could stuff them under "hw/misc/pca9552_reg.h" probably.
Quoted 209 lines
hw/misc/pca9552.c line 35
Quoted 7 lines
hw/misc/pca9552.c new file +6
-0+ +/* +* Bit [4] is used to activate the Auto-Increment option of the +* register address +*/ +#define PCA9552_AUTOINC (1 << 4)ditto
Quoted 185 lines
hw/misc/pca9552.c new file +185
-0+ +#define PCA9552_LED_ON 0x0 +#define PCA9552_LED_OFF 0x1 +#define PCA9552_LED_PWM0 0x2 +#define PCA9552_LED_PWM1 0x3 + +static uint8_t pca9552_pin_get_config (PCA9552State *s,int pin) +{ +uint8_t reg =PCA9552_LS0 + (pin /4 ); +uint8_t shift = (pin %4 ) <<1 ; + +return extract32 (s->regs[reg], shift,2 ); +} + +static void pca9552_update_pin_input (PCA9552State *s) +{ +int i; + +for (i =0 ; i < s->nr_leds; i++) { +uint8_t input_reg =PCA9552_INPUT0 + (i /8 ); +uint8_t input_shift = (i %8 ); +uint8_t config =pca9552_pin_get_config (s, i); + +switch (config) { +case PCA9552_LED_ON : + s->regs[input_reg] |=1 << input_shift; +break ; +case PCA9552_LED_OFF : + s->regs[input_reg] &= ~(1 << input_shift); +break ; +case PCA9552_LED_PWM0 : +case PCA9552_LED_PWM1 : +/* TODO */ +default : +break ; + } + } +} + +static uint8_t pca9552_read (PCA9552State *s,uint8_t reg) +{ +switch (reg) { +case PCA9552_INPUT0 : +case PCA9552_INPUT1 : +case PCA9552_PSC0 : +case PCA9552_PWM0 : +case PCA9552_PSC1 : +case PCA9552_PWM1 : +case PCA9552_LS0 : +case PCA9552_LS1 : +case PCA9552_LS2 : +case PCA9552_LS3 : +return s->regs[reg]; +default : +qemu_log_mask (LOG_GUEST_ERROR ,"%s: unexpected read to register %d\n" , + __func__, reg); +return 0xFF ; + } +} + +static void pca9552_write (PCA9552State *s,uint8_t reg,uint8_t data) +{ +switch (reg) { +case PCA9552_PSC0 : +case PCA9552_PWM0 : +case PCA9552_PSC1 : +case PCA9552_PWM1 : + s->regs[reg] = data; +break ; + +case PCA9552_LS0 : +case PCA9552_LS1 : +case PCA9552_LS2 : +case PCA9552_LS3 : + s->regs[reg] = data; +pca9552_update_pin_input (s); +break ; + +case PCA9552_INPUT0 : +case PCA9552_INPUT1 : +default : +qemu_log_mask (LOG_GUEST_ERROR ,"%s: unexpected write to register %d\n" , + __func__, reg); + } +} + +/* +* When Auto-Increment is on, the register address is incremented +* after each byte is sent to or received by the device. The index +* rollovers to 0 when the maximum register address is reached. +*/ +static void pca9552_autoinc (PCA9552State *s) +{ +if (s->pointer !=0xFF && s->pointer &PCA9552_AUTOINC ) { +uint8_t reg = s->pointer &0xf ; + + reg = (reg +1 ) % (s->max_reg +1 ); + s->pointer = reg |PCA9552_AUTOINC ; + } +} + +static int pca9552_recv (I2CSlave *i2c) +{ +PCA9552State *s =PCA9552 (i2c); +uint8_t ret; + + ret =pca9552_read (s, s->pointer &0xf ); + +/* +* From the Specs: +* +* Important Note: When a Read sequence is initiated and the +* AI bit is set to Logic Level 1, the Read Sequence MUST +* start by a register different from 0. +* +* I don't know what should be done in this case, so throw an +* error. +*/ +if (s->pointer ==PCA9552_AUTOINC ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Autoincrement read starting with register 0\n" , + __func__); + } + +pca9552_autoinc (s); + +return ret; +} + +static int pca9552_send (I2CSlave *i2c,uint8_t data) +{ +PCA9552State *s =PCA9552 (i2c); + +/* First byte sent by is the register address */ +if (s->len ==0 ) { + s->pointer = data; + s->len++; + }else { +pca9552_write (s, s->pointer &0xf , data); + +pca9552_autoinc (s); + } + +return 0 ; +} + +static int pca9552_event (I2CSlave *i2c,enum i2c_event event) +{ +PCA9552State *s =PCA9552 (i2c); + + s->len =0 ; +return 0 ; +} + +static const VMStateDescription pca9552_vmstate = { + .name ="PCA9552" , + .version_id =0 , + .minimum_version_id =0 , + .fields = (VMStateField []) { +VMSTATE_UINT8 (len, PCA9552State), +VMSTATE_UINT8 (pointer, PCA9552State), +VMSTATE_UINT8_ARRAY (regs, PCA9552State,PCA9552_NR_REGS ), +VMSTATE_I2C_SLAVE (i2c, PCA9552State), +VMSTATE_END_OF_LIST () + } +}; + +static void pca9552_reset (DeviceState *dev) +{ +PCA9552State *s =PCA9552 (dev); + + s->regs[PCA9552_PSC0 ] =0xFF ; + s->regs[PCA9552_PWM0 ] =0x80 ; + s->regs[PCA9552_PSC1 ] =0xFF ; + s->regs[PCA9552_PWM1 ] =0x80 ; + s->regs[PCA9552_LS0 ] =0x55 ;/* all OFF */ + s->regs[PCA9552_LS1 ] =0x55 ; + s->regs[PCA9552_LS2 ] =0x55 ; + s->regs[PCA9552_LS3 ] =0x55 ; + +pca9552_update_pin_input (s); + + s->pointer =0xFF ; + s->len =0 ; +}hw/misc/pca9552.c line 230
Quoted 10 lines
hw/misc/pca9552.c new file +10
-0+ +static void pca9552_initfn (Object *obj) +{ +PCA9552State *s =PCA9552 (obj); + +/* If support for the other PCA955X devices are implemented, these +* constant values might be part of class structure describing the +* PCA955X device +*/ + s->max_reg =PCA9552_LS3 ;Looks a bit weird, why not use
PCA9552_NR_REGShere sopca9552_autoinc()becomes simply:
Quoted 4 lines
reg += 1; reg %= s->max_reg;?
because I expect someone (me, maybe) to add support for the PCA9550,1,3 devices and, in that case, regs[] could be shared and PCA9552_NR_REGS would represent the maximum of the maximum number of registers for all devices.
Quoted 1 line
hw/misc/pca9552.c line 231
Quoted 1 line
hw/misc/pca9552.c new file +1
-0+ s->nr_leds =16 ;
this value would change also for each device.
Quoted 46 lines
Quoted 46 lines
hw/misc/pca9552.c new file +28
-0+} + +static void pca9552_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); +I2CSlaveClass *k =I2C_SLAVE_CLASS (klass); + + k->event = pca9552_event; + k->recv = pca9552_recv; + k->send = pca9552_send; + dc->reset = pca9552_reset; + dc->vmsd = &pca9552_vmstate; +} + +static const TypeInfo pca9552_info = { + .name =TYPE_PCA9552 , + .parent =TYPE_I2C_SLAVE , + .instance_init = pca9552_initfn, + .instance_size =sizeof (PCA9552State), + .class_init = pca9552_class_init, +}; + +static void pca9552_register_types (void ) +{ +type_register_static (&pca9552_info); +} + +type_init (pca9552_register_types)include/hw/misc/pca9552.h new file +12
-0@@ -0,0 +1,33 @@ +/* +* PCA9552 I2C LED blinker +* +* Copyright (c) 2017, IBM Corporation. +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ +#ifndef PCA9552_H +#define PCA9552_H + +#include "hw/i2c/i2c.h"
Quoted 8 lines
include/hw/misc/pca9552.h line 18
Quoted 6 lines
include/hw/misc/pca9552.h new file +6
-0+ +#define TYPE_PCA9552 "pca9552" +#define PCA9552 (obj) OBJECT_CHECK(PCA9552State, (obj), TYPE_PCA9552) + + +#define PCA9552_NR_REGS 10This could be an enum (or not, matter of taste).
a define is fine IMO.
Quoted 67 lines
Quoted 42 lines
include/hw/misc/pca9552.h new file +15
-0+ +typedef struct PCA9552State { +/*< private >*/ +I2CSlave i2c; +/*< public >*/ + +uint8_t len; +uint8_t pointer; + +uint8_t regs[PCA9552_NR_REGS ]; +uint8_t max_reg; +uint8_t nr_leds; +}PCA9552State ; + +#endif tests/Makefile.include +2
-0@@ -347,6 +347,7 @@ check-qtest-sparc64-y = tests/endianness-test$(EXESUF) check-qtest-sparc64-y += tests/prom-env-test$(EXESUF) check-qtest-arm-y = tests/tmp105-test$(EXESUF) +check-qtest-arm-y += tests/pca9552-test$(EXESUF) check-qtest-arm-y += tests/ds1338-test$(EXESUF) check-qtest-arm-y += tests/m25p80-test$(EXESUF) gcov-files-arm-y += hw/misc/tmp105.c@@ -739,6 +740,7 @@ tests/bios-tables-test$(EXESUF): tests/bios-tables-test.o \ tests/boot-sector.o tests/acpi-utils.o $(libqos-obj-y) tests/pxe-test$(EXESUF): tests/pxe-test.o tests/boot-sector.o $(libqos-obj-y) tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y) +tests/pca9552-test$(EXESUF): tests/pca9552-test.o $(libqos-omap-obj-y) tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y) tests/m25p80-test$(EXESUF): tests/m25p80-test.o tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y)tests/pca9552-test.c new file +1
-0@@ -0,0 +1,131 @@ +/* tests/pca9552-test.c line 11
Quoted 10 lines
tests/pca9552-test.c new file +10
-0+* QTest testcase for the PCA9552 LED blinker +* +* Copyright (c) 2017, IBM Corporation. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "
hw/misc/pca9552.h"Quoted 12 lines
tests/pca9552-test.c new file +12
-0+#include "libqtest.h" +#include "libqos/i2c.h" + +#define PCA9552_INPUT0 0/* read only input register 0 */ +#define PCA9552_INPUT1 1/* read only input register 1 */ +#define PCA9552_PSC0 2/* read/write frequency prescaler 0 */ +#define PCA9552_PWM0 3/* read/write PWM register 0 */ +#define PCA9552_PSC1 4/* read/write frequency prescaler 1 */ +#define PCA9552_PWM1 5/* read/write PWM register 1 */ +#define PCA9552_LS0 6/* read/write LED0 to LED3 selector */ +#define PCA9552_LS1 7/* read/write LED4 to LED7 selector */ +#define PCA9552_LS2 8/* read/write LED8 to LED11 selector */
Quoted 8 lines
tests/pca9552-test.c line 29
Quoted 6 lines
tests/pca9552-test.c new file +6
-0+#define PCA9552_LS3 9/* read/write LED12 to LED15 selector */ + +#define PCA9552_AUTOINC (1 << 4) + + +#define OMAP2_I2C_1_BASE 0x48070000Can you define that in "
hw/arm/omap.h" ?
I suppose I can to share it with tmp105-test.c
Quoted 106 lines
Quoted 93 lines
tests/pca9552-test.c new file +92
-0+ +#define PCA9552_TEST_ID "pca9552-test" +#define PCA9552_TEST_ADDR 0x60 + +static I2CAdapter *i2c; + +static uint8_t pca9552_get8 (I2CAdapter *i2c,uint8_t addr,uint8_t reg) +{ +uint8_t resp[1 ]; +i2c_send (i2c, addr, ®,1 ); +i2c_recv (i2c, addr, resp,1 ); +return resp[0 ]; +} + +static void pca9552_set8 (I2CAdapter *i2c,uint8_t addr,uint8_t reg, +uint8_t value) +{ +uint8_t cmd[2 ]; +uint8_t resp[1 ]; + + cmd[0 ] = reg; + cmd[1 ] = value; +i2c_send (i2c, addr, cmd,2 ); +i2c_recv (i2c, addr, resp,1 ); + g_assert_cmphex(resp[0], ==, cmd[1]); +} + +static void receive_autoinc (void ) +{ +uint8_t resp; +uint8_t reg =PCA9552_LS0 |PCA9552_AUTOINC ; + +i2c_send (i2c,PCA9552_TEST_ADDR , ®,1 ); + +/* PCA9552_LS0 */ +i2c_recv (i2c,PCA9552_TEST_ADDR , &resp,1 ); + g_assert_cmphex(resp, ==, 0x54); + +/* PCA9552_LS1 */ +i2c_recv (i2c,PCA9552_TEST_ADDR , &resp,1 ); + g_assert_cmphex(resp, ==, 0x55); + +/* PCA9552_LS2 */ +i2c_recv (i2c,PCA9552_TEST_ADDR , &resp,1 ); + g_assert_cmphex(resp, ==, 0x55); + +/* PCA9552_LS3 */ +i2c_recv (i2c,PCA9552_TEST_ADDR , &resp,1 ); + g_assert_cmphex(resp, ==, 0x54); +} + +static void send_and_receive (void ) +{ +uint8_t value; + + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_LS0 ); + g_assert_cmphex(value, ==, 0x55); + + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_INPUT0 ); + g_assert_cmphex(value, ==, 0x0); + +/* Switch on LED 0 */ +pca9552_set8 (i2c,PCA9552_TEST_ADDR ,PCA9552_LS0 ,0x54 ); + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_LS0 ); + g_assert_cmphex(value, ==, 0x54); + + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_INPUT0 ); + g_assert_cmphex(value, ==, 0x01); + +/* Switch on LED 12 */ +pca9552_set8 (i2c,PCA9552_TEST_ADDR ,PCA9552_LS3 ,0x54 ); + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_LS3 ); + g_assert_cmphex(value, ==, 0x54); + + value =pca9552_get8 (i2c,PCA9552_TEST_ADDR ,PCA9552_INPUT1 ); + g_assert_cmphex(value, ==, 0x10); +} + +int main (int argc,char **argv) +{ +QTestState *s =NULL ; +int ret; + +g_test_init (&argc, &argv,NULL ); + + s =qtest_start ("-machine n800 " +"-device pca9552,bus=i2c-bus.0,id=" PCA9552_TEST_ID +",address=0x60" ); + i2c =omap_i2c_create (OMAP2_I2C_1_BASE ); + +qtest_add_func ("/pca9552/tx-rx" , send_and_receive); +qtest_add_func ("/pca9552/rx-autoinc" , receive_autoinc);tests/pca9552-test.c line 131
Quoted 10 lines
tests/pca9552-test.c new file +10
-0+ + ret =g_test_run (); + +if (s) { +qtest_quit (s); + } +g_free (i2c); + +return ret; +}If Peter accept to queue 1-6, I'd appreciate if you can address my few suggestions then resend 7-8; else if you don't have time:
Quoted 1 line
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
I rather address your suggestions in follow ups if you don't mind.
Thanks for the review,
C.
Quoted 4 lines
Regards,
Phil.
Re: [Qemu-arm] [PATCH v6 8/8] aspeed: add the pc9552 chips to the witherspoon machine
In reply to Philippe Mathieu-DaudéQuoted 18 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Cédric Le Goater
The pca9552 LED blinkers on the Witherspoon machine are used for leds but also as GPIOs to control fans and GPUs.
Signed-off-by Cédric Le Goater <[email protected]>Reviewed-by Andrew Jeffery <[email protected]>--- hw/arm/aspeed.c | 4 ++++ 1 file changed, 4 insertions(+)hw/arm/aspeed.c +0
-0@@ -397,6 +397,8 @@ static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc)AspeedSoCState *soc = &bmc->soc;uint8_t *eeprom_buf =g_malloc0 (8 *1024 );
Quoted 3 lines
hw/arm/aspeed.c static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc) · line 400
Quoted 1 line
hw/arm/aspeed.c +1
-0+i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),3 ),"pca9552" ,0x60 );
TYPE_PCA9552?
I expect one day that the full "pca9550,1,2,3" device family will exist and I am not sure there is much point in adding a define for each. I should probably remove the TYPE_PCA9552.
Thanks,
C.
Quoted 20 lines
hw/arm/aspeed.c static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc) · line 414
Quoted 9 lines
hw/arm/aspeed.c +2
-0+i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),4 ),"tmp423" ,0x4c );i2c_create_slave (aspeed_i2c_get_bus (DEVICE (&soc->i2c),5 ),"tmp423" ,0x4c );@@ -409,6 +411,8 @@ static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc)smbus_eeprom_init_one (aspeed_i2c_get_bus (DEVICE (&soc->i2c),11 ),0x51 , eeprom_buf); + i2c_create_slave(aspeed_i2c_get_bus(DEVICE(&soc->i2c), 11), "pca9552",ditto
hw/arm/aspeed.c static void witherspoon_bmc_i2c_init(AspeedBoardState *bmc) · line 418
Quoted 5 lines
hw/arm/aspeed.c +1
-0+ 0x60); }static void witherspoon_bmc_init (MachineState *machine)Reviewed-by Philippe Mathieu-Daudé <[email protected]>