#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/interrupt.h>
#include <sys/malloc.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/rman.h>
#include <sys/gpio.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_extern.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <dev/fdt/fdt_common.h>
#include <dev/fdt/fdt_pinctrl.h>
#include <dev/gpio/gpiobusvar.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/clk/clk.h>
#include <dev/hwreset/hwreset.h>
#include <dev/spibus/spi.h>
#include <dev/spibus/spibusvar.h>
#include "spibus_if.h"
#include <dev/qcom_qup/qcom_spi_var.h>
#include <dev/qcom_qup/qcom_qup_reg.h>
#include <dev/qcom_qup/qcom_spi_reg.h>
#include <dev/qcom_qup/qcom_spi_debug.h>
static struct ofw_compat_data compat_data[] = {
{ "qcom,spi-qup-v1.1.1", QCOM_SPI_HW_QPI_V1_1 },
{ "qcom,spi-qup-v2.1.1", QCOM_SPI_HW_QPI_V2_1 },
{ "qcom,spi-qup-v2.2.1", QCOM_SPI_HW_QPI_V2_2 },
{ NULL, 0 }
};
static void
qcom_spi_set_chipsel(struct qcom_spi_softc *sc, int cs, bool active)
{
bool pinactive;
bool invert = !! (cs & SPIBUS_CS_HIGH);
cs = cs & ~SPIBUS_CS_HIGH;
if (sc->cs_pins[cs] == NULL) {
device_printf(sc->sc_dev,
"%s: cs=%u, active=%u, invert=%u, no gpio?\n",
__func__, cs, active, invert);
return;
}
QCOM_SPI_DPRINTF(sc, QCOM_SPI_DEBUG_CHIPSELECT,
"%s: cs=%u active=%u\n", __func__, cs, active);
if (active)
pinactive = false;
else
pinactive = true;
if (invert)
pinactive = !! pinactive;
gpio_pin_set_active(sc->cs_pins[cs], pinactive);
gpio_pin_is_active(sc->cs_pins[cs], &pinactive);
}
static void
qcom_spi_intr(void *arg)
{
struct qcom_spi_softc *sc = arg;
int ret;
QCOM_SPI_DPRINTF(sc, QCOM_SPI_DEBUG_INTR, "%s: called\n", __func__);
QCOM_SPI_LOCK(sc);
ret = qcom_spi_hw_interrupt_handle(sc);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: failed to read intr status\n");
goto done;
}
if (sc->transfer.active == false) {
device_printf(sc->sc_dev,
"ERROR: spurious interrupt\n");
qcom_spi_hw_ack_opmode(sc);
goto done;
}
if (sc->intr.error) {
sc->intr.error = false;
device_printf(sc->sc_dev, "ERROR: intr\n");
}
if (sc->intr.do_rx) {
sc->intr.do_rx = false;
QCOM_SPI_DPRINTF(sc, QCOM_SPI_DEBUG_INTR,
"%s: PIO_READ\n", __func__);
if (sc->state.transfer_mode == QUP_IO_M_MODE_FIFO)
ret = qcom_spi_hw_read_pio_fifo(sc);
else
ret = qcom_spi_hw_read_pio_block(sc);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: qcom_spi_hw_read failed (%u)\n", ret);
goto done;
}
}
if (sc->intr.do_tx) {
sc->intr.do_tx = false;
QCOM_SPI_DPRINTF(sc, QCOM_SPI_DEBUG_INTR,
"%s: PIO_WRITE\n", __func__);
if (sc->state.transfer_mode == QUP_IO_M_MODE_FIFO)
ret = qcom_spi_hw_ack_write_pio_fifo(sc);
else
ret = qcom_spi_hw_write_pio_block(sc);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: qcom_spi_hw_write failed (%u)\n", ret);
goto done;
}
}
if (sc->intr.done) {
sc->intr.done = false;
sc->transfer.done = true;
QCOM_SPI_DPRINTF(sc, QCOM_SPI_DEBUG_INTR,
"%s: transfer done\n", __func__);
wakeup(sc);
}
done:
QCOM_SPI_DPRINTF(sc, QCOM_SPI_DEBUG_INTR,
"%s: done\n", __func__);
QCOM_SPI_UNLOCK(sc);
}
static int
qcom_spi_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
return (ENXIO);
device_set_desc(dev, "Qualcomm SPI Interface");
return (BUS_PROBE_DEFAULT);
}
static void
qcom_spi_attach_gpios(struct qcom_spi_softc *sc)
{
phandle_t node;
int idx, err;
node = ofw_bus_get_node(sc->sc_dev);
for (idx = 0; idx < nitems(sc->cs_pins); idx++) {
err = gpio_pin_get_by_ofw_propidx(sc->sc_dev, node,
"cs-gpios", idx, &sc->cs_pins[idx]);
if (err == 0) {
err = gpio_pin_setflags(sc->cs_pins[idx],
GPIO_PIN_OUTPUT);
if (err != 0) {
device_printf(sc->sc_dev,
"error configuring gpio for"
" cs %u (%d)\n", idx, err);
}
#if 0
gpio_pin_set_active(sc->cs_pins[idx], 1);
gpio_pin_is_active(sc->cs_pins[idx], &tmp);
#endif
} else {
device_printf(sc->sc_dev,
"cannot configure gpio for chip select %u\n", idx);
sc->cs_pins[idx] = NULL;
}
}
}
static void
qcom_spi_sysctl_attach(struct qcom_spi_softc *sc)
{
struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
"debug", CTLFLAG_RW, &sc->sc_debug, 0,
"control debugging printfs");
}
static int
qcom_spi_attach(device_t dev)
{
struct qcom_spi_softc *sc = device_get_softc(dev);
int rid, ret, i, val;
sc->sc_dev = dev;
sc->hw_version =
ofw_bus_search_compatible(dev, compat_data)->ocd_data;
mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
rid = 0;
sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
RF_ACTIVE);
if (!sc->sc_mem_res) {
device_printf(dev, "ERROR: Could not map memory\n");
ret = ENXIO;
goto error;
}
rid = 0;
sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
RF_ACTIVE | RF_SHAREABLE);
if (!sc->sc_irq_res) {
device_printf(dev, "ERROR: Could not map interrupt\n");
ret = ENXIO;
goto error;
}
ret = bus_setup_intr(dev, sc->sc_irq_res,
INTR_TYPE_MISC | INTR_MPSAFE,
NULL, qcom_spi_intr, sc, &sc->sc_irq_h);
if (ret != 0) {
device_printf(dev, "ERROR: could not configure interrupt "
"(%d)\n",
ret);
goto error;
}
qcom_spi_attach_gpios(sc);
ret = clk_get_by_ofw_name(dev, 0, "core", &sc->clk_core);
if (ret != 0) {
device_printf(dev, "ERROR: could not get %s clock (%d)\n",
"core", ret);
goto error;
}
ret = clk_get_by_ofw_name(dev, 0, "iface", &sc->clk_iface);
if (ret != 0) {
device_printf(dev, "ERROR: could not get %s clock (%d)\n",
"iface", ret);
goto error;
}
ret = clk_enable(sc->clk_core);
if (ret != 0) {
device_printf(dev, "ERROR: couldn't enable core clock (%u)\n",
ret);
goto error;
}
ret = clk_enable(sc->clk_iface);
if (ret != 0) {
device_printf(dev, "ERROR: couldn't enable iface clock (%u)\n",
ret);
goto error;
}
if (OF_getencprop(ofw_bus_get_node(dev), "spi-max-frequency",
&val, sizeof(val)) > 0)
sc->config.max_frequency = val;
else
sc->config.max_frequency = SPI_MAX_RATE;
if (OF_getencprop(ofw_bus_get_node(dev), "cs-select",
&val, sizeof(val)) > 0)
sc->config.cs_select = val;
else
sc->config.cs_select = 0;
if (OF_getencprop(ofw_bus_get_node(dev), "num-cs",
&val, sizeof(val)) > 0)
sc->config.num_cs = val;
else
sc->config.num_cs = SPI_NUM_CHIPSELECTS;
ret = fdt_pinctrl_configure_by_name(dev, "default");
if (ret != 0) {
device_printf(dev,
"ERROR: could not configure default pinmux\n");
goto error;
}
ret = qcom_spi_hw_read_controller_transfer_sizes(sc);
if (ret != 0) {
device_printf(dev, "ERROR: Could not read transfer config\n");
goto error;
}
device_printf(dev, "BLOCK: input=%u bytes, output=%u bytes\n",
sc->config.input_block_size,
sc->config.output_block_size);
device_printf(dev, "FIFO: input=%u bytes, output=%u bytes\n",
sc->config.input_fifo_size,
sc->config.output_fifo_size);
QCOM_SPI_LOCK(sc);
ret = qcom_spi_hw_qup_init_locked(sc);
if (ret != 0) {
device_printf(dev, "ERROR: QUP init failed (%d)\n", ret);
QCOM_SPI_UNLOCK(sc);
goto error;
}
ret = qcom_spi_hw_spi_init_locked(sc);
if (ret != 0) {
device_printf(dev, "ERROR: SPI init failed (%d)\n", ret);
QCOM_SPI_UNLOCK(sc);
goto error;
}
QCOM_SPI_UNLOCK(sc);
sc->spibus = device_add_child(dev, "spibus", DEVICE_UNIT_ANY);
device_printf(dev, "DONE: shutting down interface clock for now\n");
clk_disable(sc->clk_iface);
qcom_spi_sysctl_attach(sc);
bus_attach_children(dev);
return (0);
error:
if (sc->sc_irq_h)
bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_h);
if (sc->sc_mem_res)
bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
if (sc->sc_irq_res)
bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
if (sc->clk_core) {
clk_disable(sc->clk_core);
clk_release(sc->clk_core);
}
if (sc->clk_iface) {
clk_disable(sc->clk_iface);
clk_release(sc->clk_iface);
}
for (i = 0; i < CS_MAX; i++) {
if (sc->cs_pins[i] != NULL)
gpio_pin_release(sc->cs_pins[i]);
}
mtx_destroy(&sc->sc_mtx);
return (ret);
}
static int
qcom_spi_transfer_pio_block(struct qcom_spi_softc *sc, int mode,
char *tx_buf, int tx_len, char *rx_buf, int rx_len)
{
int ret = 0;
QCOM_SPI_DPRINTF(sc, QCOM_SPI_DEBUG_TRANSFER, "%s: start\n",
__func__);
if (rx_len != tx_len) {
device_printf(sc->sc_dev,
"ERROR: tx/rx len doesn't match (%d/%d)\n",
tx_len, rx_len);
return (ENXIO);
}
QCOM_SPI_ASSERT_LOCKED(sc);
ret = qcom_spi_hw_setup_transfer_selection(sc, tx_len);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: failed to setup transfer selection (%d)\n",
ret);
return (ret);
}
sc->transfer.tx_buf = tx_buf;
sc->transfer.tx_len = tx_len;
sc->transfer.rx_buf = rx_buf;
sc->transfer.rx_len = rx_len;
sc->transfer.done = false;
sc->transfer.active = false;
while (sc->transfer.tx_offset < sc->transfer.tx_len) {
sc->transfer.active = false;
QCOM_SPI_DPRINTF(sc, QCOM_SPI_DEBUG_TRANSFER,
"%s: tx=%d of %d bytes, rx=%d of %d bytes\n",
__func__,
sc->transfer.tx_offset, sc->transfer.tx_len,
sc->transfer.rx_offset, sc->transfer.rx_len);
ret = qcom_spi_hw_qup_set_state_locked(sc, QUP_STATE_RESET);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: can't transition to RESET (%u)\n", ret);
goto done;
}
bzero(&sc->intr, sizeof(sc->intr));
sc->transfer.done = false;
ret = qcom_spi_hw_setup_current_transfer(sc);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: failed to setup sub transfer (%d)\n",
ret);
goto done;
}
ret = qcom_spi_hw_setup_pio_transfer_cnt(sc);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: qcom_spi_hw_setup_pio_transfer_cnt failed"
" (%u)\n", ret);
goto done;
}
#if 0
ret = qcom_spi_hw_setup_block_transfer_cnt(sc);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: qcom_spi_hw_setup_block_transfer_cnt failed"
" (%u)\n", ret);
goto done;
}
#endif
ret = qcom_spi_hw_setup_io_modes(sc);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: qcom_spi_hw_setup_io_modes failed"
" (%u)\n", ret);
goto done;
}
ret = qcom_spi_hw_setup_spi_io_clock_polarity(sc,
!! (mode & SPIBUS_MODE_CPOL));
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: qcom_spi_hw_setup_spi_io_clock_polarity"
" failed (%u)\n", ret);
goto done;
}
ret = qcom_spi_hw_setup_spi_config(sc, sc->state.frequency,
!! (mode & SPIBUS_MODE_CPHA));
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: qcom_spi_hw_setup_spi_config failed"
" (%u)\n", ret);
goto done;
}
ret = qcom_spi_hw_setup_qup_config(sc, !! (tx_len > 0),
!! (rx_len > 0));
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: qcom_spi_hw_setup_qup_config failed"
" (%u)\n", ret);
goto done;
}
ret = qcom_spi_hw_setup_operational_mask(sc);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: qcom_spi_hw_setup_operational_mask failed"
" (%u)\n", ret);
goto done;
}
sc->transfer.active = true;
ret = qcom_spi_hw_qup_set_state_locked(sc, QUP_STATE_RUN);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: can't transition to RUN (%u)\n", ret);
goto done;
}
ret = qcom_spi_hw_qup_set_state_locked(sc, QUP_STATE_PAUSE);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: can't transition to PAUSE (%u)\n", ret);
goto done;
}
if (sc->state.transfer_mode == QUP_IO_M_MODE_FIFO)
ret = qcom_spi_hw_write_pio_fifo(sc);
else
ret = qcom_spi_hw_write_pio_block(sc);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: qcom_spi_hw_write failed (%u)\n", ret);
goto done;
}
ret = qcom_spi_hw_qup_set_state_locked(sc, QUP_STATE_RUN);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: can't transition to RUN (%u)\n", ret);
goto done;
}
ret = 0;
while (ret == 0 && sc->transfer.done == false) {
QCOM_SPI_DPRINTF(sc, QCOM_SPI_DEBUG_TRANSFER,
"%s: waiting\n", __func__);
ret = msleep(sc, &sc->sc_mtx, 0, "qcom_spi", 0);
}
}
done:
(void) qcom_spi_hw_qup_set_state_locked(sc, QUP_STATE_RESET);
QCOM_SPI_DPRINTF(sc, QCOM_SPI_DEBUG_TRANSFER,
"%s: completed\n", __func__);
(void) qcom_spi_hw_complete_transfer(sc);
sc->transfer.active = false;
return (ret);
}
static int
qcom_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
{
struct qcom_spi_softc *sc = device_get_softc(dev);
uint32_t cs_val, mode_val, clock_val;
uint32_t ret = 0;
spibus_get_cs(child, &cs_val);
spibus_get_clock(child, &clock_val);
spibus_get_mode(child, &mode_val);
QCOM_SPI_DPRINTF(sc, QCOM_SPI_DEBUG_TRANSFER,
"%s: called; child cs=0x%08x, clock=%u, mode=0x%08x, "
"cmd=%u/%u bytes; data=%u/%u bytes\n",
__func__,
cs_val,
clock_val,
mode_val,
cmd->tx_cmd_sz, cmd->rx_cmd_sz,
cmd->tx_data_sz, cmd->rx_data_sz);
QCOM_SPI_LOCK(sc);
while (sc->sc_busy == true)
mtx_sleep(sc, &sc->sc_mtx, 0, "qcom_spi_wait", 0);
sc->sc_busy = true;
sc->state.cs_high = !! (cs_val & SPIBUS_CS_HIGH);
sc->state.frequency = clock_val;
QCOM_SPI_UNLOCK(sc);
ret = clk_set_freq(sc->clk_iface, sc->state.frequency, 0);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: failed to set frequency to %u\n",
sc->state.frequency);
goto done2;
}
clk_enable(sc->clk_iface);
QCOM_SPI_LOCK(sc);
ret = qcom_spi_hw_qup_set_state_locked(sc, QUP_STATE_RESET);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: can't transition to RESET (%u)\n", ret);
goto done;
}
if (sc->cs_pins[cs_val & ~SPIBUS_CS_HIGH] == NULL)
qcom_spi_hw_spi_cs_force(sc, cs_val & SPIBUS_CS_HIGH, true);
else
qcom_spi_set_chipsel(sc, cs_val & ~SPIBUS_CS_HIGH, true);
ret = qcom_spi_transfer_pio_block(sc, mode_val, cmd->tx_cmd,
cmd->tx_cmd_sz, cmd->rx_cmd, cmd->rx_cmd_sz);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: failed to transfer cmd payload (%u)\n", ret);
goto done;
}
if (cmd->tx_data_sz > 0) {
ret = qcom_spi_transfer_pio_block(sc, mode_val, cmd->tx_data,
cmd->tx_data_sz, cmd->rx_data, cmd->rx_data_sz);
if (ret != 0) {
device_printf(sc->sc_dev,
"ERROR: failed to transfer data payload (%u)\n",
ret);
goto done;
}
}
done:
if (sc->cs_pins[cs_val & ~SPIBUS_CS_HIGH] == NULL)
qcom_spi_hw_spi_cs_force(sc, cs_val & ~SPIBUS_CS_HIGH, false);
else
qcom_spi_set_chipsel(sc, cs_val & ~SPIBUS_CS_HIGH, false);
QCOM_SPI_UNLOCK(sc);
clk_disable(sc->clk_iface);
QCOM_SPI_LOCK(sc);
done2:
sc->sc_busy = false;
wakeup_one(sc);
QCOM_SPI_UNLOCK(sc);
return (ret);
}
static int
qcom_spi_detach(device_t dev)
{
struct qcom_spi_softc *sc = device_get_softc(dev);
int i;
bus_generic_detach(sc->sc_dev);
if (sc->sc_irq_h)
bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_h);
if (sc->clk_iface) {
clk_disable(sc->clk_iface);
clk_release(sc->clk_iface);
}
if (sc->clk_core) {
clk_disable(sc->clk_core);
clk_release(sc->clk_core);
}
for (i = 0; i < CS_MAX; i++) {
if (sc->cs_pins[i] != NULL)
gpio_pin_release(sc->cs_pins[i]);
}
if (sc->sc_mem_res)
bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
if (sc->sc_irq_res)
bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
mtx_destroy(&sc->sc_mtx);
return (0);
}
static phandle_t
qcom_spi_get_node(device_t bus, device_t dev)
{
return ofw_bus_get_node(bus);
}
static device_method_t qcom_spi_methods[] = {
DEVMETHOD(device_probe, qcom_spi_probe),
DEVMETHOD(device_attach, qcom_spi_attach),
DEVMETHOD(device_detach, qcom_spi_detach),
DEVMETHOD(spibus_transfer, qcom_spi_transfer),
DEVMETHOD(ofw_bus_get_node, qcom_spi_get_node),
DEVMETHOD_END
};
static driver_t qcom_spi_driver = {
"qcom_spi",
qcom_spi_methods,
sizeof(struct qcom_spi_softc),
};
DRIVER_MODULE(qcom_spi, simplebus, qcom_spi_driver, 0, 0);
DRIVER_MODULE(ofw_spibus, qcom_spi, ofw_spibus_driver, 0, 0);
MODULE_DEPEND(qcom_spi, ofw_spibus, 1, 1, 1);
SIMPLEBUS_PNP_INFO(compat_data);