Alex Badea has uploaded this change for review.

View Change

asm106x: add programmer for ASM106x SATA controllers

The ASMedia ASM106x series is a PCIe-SATA controller chip. It supports
an attached SPI flash chip that can contain configuration and PCI option
ROM. The interface is a simple shifter accessed via PCI config space, up
to 4 bytes at a time. Add a programmer driver for it.

Change-Id: I591b117be911bdb8249247c20530c1cf70f6e70d
Signed-off-by: Alex Badea <vamposdecampos@gmail.com>
---
M Makefile
A asm106x.c
M flashrom.8.tmpl
M include/programmer.h
M meson.build
M programmer_table.c
M test_build.sh
7 files changed, 206 insertions(+), 3 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/37/73037/1
diff --git a/Makefile b/Makefile
index b37e807..75ffeee 100644
--- a/Makefile
+++ b/Makefile
@@ -137,6 +137,7 @@
CONFIG_SATAMV \

DEPENDS_ON_LIBPCI := \
+ CONFIG_ASM106X \
CONFIG_ATAHPT \
CONFIG_ATAPROMISE \
CONFIG_ATAVIA \
@@ -437,6 +438,9 @@
# Always enable SiI SATA controllers for now.
CONFIG_SATASII ?= yes

+# Asmedia ASM106x
+CONFIG_ASM106X ?= yes
+
# Highpoint (HPT) ATA/RAID controller support.
# IMPORTANT: This code is not yet working!
CONFIG_ATAHPT ?= no
@@ -642,6 +646,12 @@
ACTIVE_PROGRAMMERS += satasii
endif

+ifeq ($(CONFIG_ASM106X), yes)
+FEATURE_FLAGS += -D'CONFIG_ASM106X=1'
+PROGRAMMER_OBJS += asm106x.o
+ACTIVE_PROGRAMMERS += asm106x
+endif
+
ifeq ($(CONFIG_ATAHPT), yes)
FEATURE_FLAGS += -D'CONFIG_ATAHPT=1'
PROGRAMMER_OBJS += atahpt.o
diff --git a/asm106x.c b/asm106x.c
new file mode 100644
index 0000000..7b54391
--- /dev/null
+++ b/asm106x.c
@@ -0,0 +1,162 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2023 Alex Badea <vamposdecampos@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <stdlib.h>
+#include "programmer.h"
+#include "platform/pci.h"
+
+#define PCI_VENDOR_ID_ASMEDIA 0x1b21
+
+#define ASM106X_REG_DATA 0xf0
+#define ASM106X_REG_CTRL 0xf4
+#define ASM106X_CTRL_RUN 0x20 /* SPI master is running */
+#define ASM106X_CTRL_CSN 0x10 /* CS_n pin output */
+#define ASM106X_CTRL_WRITE 0x08 /* 0=read, 1=write */
+#define ASM106X_CTRL_MASK 0xc0 /* unknown, untouched */
+
+struct asm106x_data {
+ struct pci_dev *pci;
+};
+
+static const struct dev_entry asm106x_devs[] = {
+ {PCI_VENDOR_ID_ASMEDIA, 0x0612, OK, "ASMedia", "ASM106x"},
+
+ {0},
+};
+
+static int asm106x_wait_ready(const struct flashctx *flash, uint8_t *pval)
+{
+ struct asm106x_data *data = flash->mst->spi.data;
+ unsigned int timeout = 100;
+ uint8_t val;
+
+ while (timeout) {
+ val = pci_read_byte(data->pci, ASM106X_REG_CTRL);
+ msg_pdbg2("asm106x status 0x%02x tries %d\n", val, timeout);
+ if (!(val & ASM106X_CTRL_RUN))
+ break;
+ programmer_delay(flash, 10);
+ timeout--;
+ }
+
+ if (!timeout) {
+ msg_pdbg("asm106x timed out, ctrl 0x%02x\n", val);
+ return 1;
+ }
+ if (pval)
+ *pval = val;
+ return 0;
+}
+
+
+static int asm106x_command(const struct flashctx *flash,
+ unsigned int writecnt, unsigned int readcnt,
+ const unsigned char *writearr, unsigned char *readarr)
+{
+ struct asm106x_data *data = flash->mst->spi.data;
+ uint8_t ctrl;
+ int rv = 1;
+
+ msg_pdbg2("asm106x command: wr %d rd %d\n",
+ writecnt, readcnt);
+ if (asm106x_wait_ready(flash, &ctrl))
+ return 1;
+ ctrl &= ASM106X_CTRL_MASK;
+
+ while (writecnt) {
+ unsigned int chunk = min(writecnt, 4);
+ uint32_t val = 0;
+
+ for (int k = chunk-1; k >= 0; k--)
+ val = (val << 8) | writearr[k];
+ msg_pdbg2("asm106x write 0x%08x chunk %u\n", val, chunk);
+ pci_write_long(data->pci, ASM106X_REG_DATA, val);
+ pci_write_byte(data->pci, ASM106X_REG_CTRL,
+ ctrl | ASM106X_CTRL_RUN | ASM106X_CTRL_WRITE | chunk);
+ if (asm106x_wait_ready(flash, NULL))
+ goto out;
+ writecnt -= chunk;
+ writearr += chunk;
+ }
+ while (readcnt) {
+ unsigned int chunk = min(readcnt, 4);
+ uint32_t val;
+
+ pci_write_byte(data->pci, ASM106X_REG_CTRL,
+ ctrl | ASM106X_CTRL_RUN | chunk);
+ if (asm106x_wait_ready(flash, NULL))
+ goto out;
+ val = pci_read_long(data->pci, ASM106X_REG_DATA);
+ msg_pdbg2("asm106x read 0x%08x chunk %u\n", val, chunk);
+ for (unsigned k = 0; k < chunk; k++) {
+ readarr[k] = val & 0xff;
+ val >>= 8;
+ }
+ readcnt -= chunk;
+ readarr += chunk;
+ }
+
+ rv = 0;
+out:
+ pci_write_byte(data->pci, ASM106X_REG_CTRL,
+ ctrl | ASM106X_CTRL_CSN);
+ return rv;
+}
+
+static int asm106x_shutdown(void *data)
+{
+ free(data);
+ return 0;
+}
+
+static const struct spi_master asm106x_spi_master = {
+ .features = SPI_MASTER_4BA,
+ .max_data_read = MAX_DATA_READ_UNLIMITED,
+ .max_data_write = MAX_DATA_WRITE_UNLIMITED,
+ .command = asm106x_command,
+ .shutdown = asm106x_shutdown,
+ .multicommand = default_spi_send_multicommand,
+ .read = default_spi_read,
+ .write_256 = default_spi_write_256,
+ .write_aai = default_spi_write_aai,
+ .probe_opcode = default_spi_probe_opcode,
+};
+
+static int asm106x_init(const struct programmer_cfg *cfg)
+{
+ struct pci_dev *pci;
+ struct asm106x_data *data;
+
+ /* TODO: no BAR required (just config space) */
+ pci = pcidev_init(cfg, asm106x_devs, PCI_ROM_ADDRESS);
+ if (!pci)
+ return 1;
+
+ data = calloc(1, sizeof(*data));
+ if (!data) {
+ msg_perr("cannot allocate memory for asm106x_data\n");
+ return 1;
+ }
+ data->pci = pci;
+ return register_spi_master(&asm106x_spi_master, (void *) data);
+}
+
+const struct programmer_entry programmer_asm106x = {
+ .name = "asm106x",
+ .type = PCI,
+ .devs.dev = asm106x_devs,
+ .init = asm106x_init,
+};
diff --git a/flashrom.8.tmpl b/flashrom.8.tmpl
index 0eabee5..dd4864d 100644
--- a/flashrom.8.tmpl
+++ b/flashrom.8.tmpl
@@ -433,6 +433,8 @@
.sp
.BR "* dirtyjtag_spi" " (for SPI flash ROMs attached to DirtyJTAG-compatible devices)"
.sp
+.BR "* asm106x" " (for SPI flash ROMs attached to ASM106x PCIe SATA controllers)"
+.sp
Some programmers have optional or mandatory parameters which are described
in detail in the
.B PROGRAMMER-SPECIFIC INFORMATION
@@ -1685,7 +1687,8 @@
needs no access permissions at all.
.sp
.BR internal ", " nic3com ", " nicrealtek ", " nicnatsemi ", "
-.BR gfxnvidia ", " drkaiser ", " satasii ", " satamv ", " atahpt ", " atavia " and " atapromise
+.BR gfxnvidia ", " drkaiser ", " satasii ", " satamv ", " asm106x ", "
+.BR atahpt ", " atavia " and " atapromise
have to be run as superuser/root, and need additional raw access permission.
.sp
.BR serprog ", " buspirate_spi ", " dediprog ", " usbblaster_spi ", " ft2232_spi ", " pickit2_spi ", " \
diff --git a/include/programmer.h b/include/programmer.h
index db32b2c..fe56e82 100644
--- a/include/programmer.h
+++ b/include/programmer.h
@@ -55,6 +55,7 @@
extern const size_t programmer_table_size;

/* programmer drivers */
+extern const struct programmer_entry programmer_asm106x;
extern const struct programmer_entry programmer_atahpt;
extern const struct programmer_entry programmer_atapromise;
extern const struct programmer_entry programmer_atavia;
diff --git a/meson.build b/meson.build
index 2575fe1..ab2cb82 100644
--- a/meson.build
+++ b/meson.build
@@ -147,6 +147,14 @@
# 'active' : boolean, # added on runtime
# }
programmer = {
+ 'asm106x' : {
+ 'systems' : systems_hwaccess,
+ 'cpu_families' : cpus_port_io,
+ 'deps' : [ libpci ],
+ 'groups' : [ group_pci, group_internal ],
+ 'srcs' : files('asm106x.c', 'pcidev.c'),
+ 'flags' : [ '-DCONFIG_ASM106X=1' ],
+ },
'atahpt' : {
'systems' : systems_hwaccess,
'cpu_families' : cpus_port_io,
diff --git a/programmer_table.c b/programmer_table.c
index d58a155..0935167 100644
--- a/programmer_table.c
+++ b/programmer_table.c
@@ -52,6 +52,10 @@
&programmer_satasii,
#endif

+#if CONFIG_ASM106X == 1
+ &programmer_asm106x,
+#endif
+
#if CONFIG_ATAHPT == 1
&programmer_atahpt,
#endif
diff --git a/test_build.sh b/test_build.sh
index c2f74d2..0e9d704 100755
--- a/test_build.sh
+++ b/test_build.sh
@@ -10,7 +10,7 @@
PICKIT2_SPI STLINKV3_SPI PARADE_LSPCON MEDIATEK_I2C_SPI REALTEK_MST_I2C_SPI DUMMY \
DRKAISER NICREALTEK NICNATSEMI NICINTEL NICINTEL_SPI NICINTEL_EEPROM OGP_SPI \
BUSPIRATE_SPI DEDIPROG DEVELOPERBOX_SPI SATAMV LINUX_MTD LINUX_SPI IT8212 \
- CH341A_SPI DIGILENT_SPI DIRTYJTAG_SPI JLINK_SPI"
+ CH341A_SPI DIGILENT_SPI DIRTYJTAG_SPI JLINK_SPI ASM106X"

meson_programmer_opts="all auto group_ftdi group_i2c group_jlink group_pci group_serial group_usb \
atahpt atapromise atavia buspirate_spi ch341a_spi dediprog developerbox_spi \
@@ -18,7 +18,7 @@
jlink_spi linux_mtd linux_spi parade_lspcon mediatek_i2c_spi mstarddc_spi \
nic3com nicintel nicintel_eeprom nicintel_spi nicnatsemi nicrealtek \
ogp_spi pickit2_spi pony_spi raiden_debug_spi rayer_spi realtek_mst_i2c_spi \
- satamv satasii serprog stlinkv3_spi usbblaster_spi"
+ satamv satasii serprog stlinkv3_spi usbblaster_spi asm106x"


if [ "$(basename "${CC}")" = "ccc-analyzer" ] || [ -n "${COVERITY_OUTPUT}" ]; then

To view, visit change 73037. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I591b117be911bdb8249247c20530c1cf70f6e70d
Gerrit-Change-Number: 73037
Gerrit-PatchSet: 1
Gerrit-Owner: Alex Badea <vamposdecampos@gmail.com>
Gerrit-MessageType: newchange