Nico Huber has uploaded this change for review.
[RFC] tests: Abstract port I/O and factor mec1308 specifics out
Change-Id: Ia3a424478456a7c968b3c736ba371d755c494f30
Signed-off-by: Nico Huber <nico.huber@secunet.com>
---
M tests/init_shutdown.c
A tests/io_mock.h
M tests/tests.c
3 files changed, 62 insertions(+), 13 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/04/52604/1
diff --git a/tests/init_shutdown.c b/tests/init_shutdown.c
index bfb979c..737066a 100644
--- a/tests/init_shutdown.c
+++ b/tests/init_shutdown.c
@@ -18,6 +18,8 @@
#include "programmer.h"
+#include "io_mock.h"
+
static void run_lifecycle(void **state, enum programmer prog, const char *param)
{
(void) state; /* unused */
@@ -36,10 +38,38 @@
run_lifecycle(state, PROGRAMMER_DUMMY, "bus=parallel+lpc+fwh+spi");
}
+struct mec1308_priv {
+ uint8_t outb_val;
+};
+static struct mec1308_priv mec1308_priv = { 0 };
+
+void mec1308_outb(unsigned char value, unsigned short port) {
+ struct mec1308_priv *priv = current_io->priv;
+ priv->outb_val = value;
+}
+
+unsigned char mec1308_inb(unsigned short port) {
+ struct mec1308_priv *priv = current_io->priv;
+ return ((port == 0x2e /* MEC1308_SIO_PORT1 */
+ || port == 0x4e /* MEC1308_SIO_PORT2 */)
+ ? 0x20 /* MEC1308_DEVICE_ID_REG */
+ : ((priv->outb_val == 0x84 /* MEC1308_MBX_DATA_START */)
+ ? 0xaa /* MEC1308_CMD_PASSTHRU_SUCCESS */
+ : 0));
+}
+
+static const struct io_mock mec1308_io = {
+ .priv = &mec1308_priv,
+ .outb = mec1308_outb,
+ .inb = mec1308_inb,
+};
+
void mec1308_init_and_shutdown_test_success(void **state)
{
will_return_always(__wrap_sio_read, 0x4d); /* MEC1308_DEVICE_ID_VAL */
+ current_io = &mec1308_io;
run_lifecycle(state, PROGRAMMER_MEC1308, "");
+ current_io = NULL;
}
void linux_spi_init_and_shutdown_test_success(void **state)
diff --git a/tests/io_mock.h b/tests/io_mock.h
new file mode 100644
index 0000000..ef2c0c3
--- /dev/null
+++ b/tests/io_mock.h
@@ -0,0 +1,16 @@
+#ifndef _IO_MOCK_H_
+#define _IO_MOCK_H_
+
+struct io_mock {
+ void *priv;
+ void (*outb)(unsigned char value, unsigned short port);
+ unsigned char (*inb)(unsigned short port);
+ void (*outw)(unsigned short value, unsigned short port);
+ unsigned short (*inw)(unsigned short port);
+ void (*outl)(unsigned int value, unsigned short port);
+ unsigned int (*inl)(unsigned short port);
+};
+
+extern const struct io_mock *current_io;
+
+#endif
diff --git a/tests/tests.c b/tests/tests.c
index 2b98d5e..b61c17f 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -14,18 +14,17 @@
*/
#include <include/test.h>
+#include "io_mock.h"
#include "tests.h"
#include <stdio.h>
#include <stdint.h>
+const struct io_mock *current_io = NULL;
+
/* redefinitions/wrapping */
#define LOG_ME printf("%s is called\n", __func__)
-uint8_t outb_val = 0;
-uint16_t outw_val = 0;
-uint32_t outl_val = 0;
-
void __wrap_physunmap(void *virt_addr, size_t len)
{
LOG_ME;
@@ -90,7 +89,8 @@
* It is commented by default because of too many calls.
*/
/* LOG_ME; */
- outb_val = value;
+ if (current_io && current_io->outb)
+ current_io->outb(value, port);
}
uint8_t __wrap_test_inb(uint16_t port) {
@@ -99,12 +99,9 @@
* It is commented by default because of too many calls.
*/
/* LOG_ME; */
- return ((port == 0x2e /* MEC1308_SIO_PORT1 */
- || port == 0x4e /* MEC1308_SIO_PORT2 */)
- ? 0x20 /* MEC1308_DEVICE_ID_REG */
- : ((outb_val == 0x84 /* MEC1308_MBX_DATA_START */)
- ? 0xaa /* MEC1308_CMD_PASSTHRU_SUCCESS */
- : 0));
+ if (current_io && current_io->inb)
+ return current_io->inb(port);
+ return 0;
}
void __wrap_test_outw(uint16_t value, uint16_t port) {
@@ -113,7 +110,8 @@
* It is commented by default because of too many calls.
*/
/* LOG_ME; */
- outw_val = value;
+ if (current_io && current_io->outw)
+ current_io->outw(value, port);
}
uint16_t __wrap_test_inw(uint16_t port) {
@@ -122,6 +120,8 @@
* It is commented by default because of too many calls.
*/
/* LOG_ME; */
+ if (current_io && current_io->inw)
+ return current_io->inw(port);
return 0;
}
@@ -131,7 +131,8 @@
* It is commented by default because of too many calls.
*/
/* LOG_ME; */
- outl_val = value;
+ if (current_io && current_io->outl)
+ current_io->outl(value, port);
}
uint32_t __wrap_test_inl(uint16_t port) {
@@ -140,6 +141,8 @@
* It is commented by default because of too many calls.
*/
/* LOG_ME; */
+ if (current_io && current_io->inl)
+ return current_io->inl(port);
return 0;
}
To view, visit change 52604. To unsubscribe, or for help writing mail filters, visit settings.