Home | History | Annotate | Line # | Download | only in acpi
qcomiic.c revision 1.4
      1  1.4   thorpej /* $NetBSD: qcomiic.c,v 1.4 2025/09/15 15:18:42 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*	$OpenBSD: qciic.c,v 1.7 2024/10/02 21:21:32 kettenis Exp $	*/
      4  1.1  jmcneill /*
      5  1.1  jmcneill  * Copyright (c) 2022 Mark Kettenis <kettenis (at) openbsd.org>
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Permission to use, copy, modify, and distribute this software for any
      8  1.1  jmcneill  * purpose with or without fee is hereby granted, provided that the above
      9  1.1  jmcneill  * copyright notice and this permission notice appear in all copies.
     10  1.1  jmcneill  *
     11  1.1  jmcneill  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  1.1  jmcneill  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1  jmcneill  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  1.1  jmcneill  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1  jmcneill  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.1  jmcneill  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  1.1  jmcneill  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1  jmcneill  */
     19  1.1  jmcneill 
     20  1.1  jmcneill #include <sys/param.h>
     21  1.1  jmcneill #include <sys/bus.h>
     22  1.1  jmcneill #include <sys/cpu.h>
     23  1.1  jmcneill #include <sys/device.h>
     24  1.1  jmcneill 
     25  1.1  jmcneill #include <dev/acpi/acpireg.h>
     26  1.1  jmcneill #include <dev/acpi/acpivar.h>
     27  1.1  jmcneill #include <dev/acpi/acpi_intr.h>
     28  1.1  jmcneill #include <dev/acpi/acpi_i2c.h>
     29  1.1  jmcneill 
     30  1.1  jmcneill #include <dev/i2c/i2cvar.h>
     31  1.1  jmcneill 
     32  1.1  jmcneill /* Registers */
     33  1.1  jmcneill #define GENI_I2C_TX_TRANS_LEN		0x26c
     34  1.1  jmcneill #define GENI_I2C_RX_TRANS_LEN		0x270
     35  1.1  jmcneill #define GENI_M_CMD0			0x600
     36  1.1  jmcneill #define  GENI_M_CMD0_OPCODE_I2C_WRITE	(0x1 << 27)
     37  1.1  jmcneill #define  GENI_M_CMD0_OPCODE_I2C_READ	(0x2 << 27)
     38  1.1  jmcneill #define  GENI_M_CMD0_SLV_ADDR_SHIFT	9
     39  1.1  jmcneill #define  GENI_M_CMD0_STOP_STRETCH	(1 << 2)
     40  1.1  jmcneill #define GENI_M_IRQ_STATUS		0x610
     41  1.1  jmcneill #define GENI_M_IRQ_CLEAR		0x618
     42  1.1  jmcneill #define  GENI_M_IRQ_CMD_DONE		(1 << 0)
     43  1.1  jmcneill #define GENI_TX_FIFO			0x700
     44  1.1  jmcneill #define GENI_RX_FIFO			0x780
     45  1.1  jmcneill #define GENI_TX_FIFO_STATUS		0x800
     46  1.1  jmcneill #define GENI_RX_FIFO_STATUS		0x804
     47  1.1  jmcneill #define  GENI_RX_FIFO_STATUS_WC(val)	((val) & 0xffffff)
     48  1.1  jmcneill 
     49  1.1  jmcneill #define HREAD4(sc, reg)							\
     50  1.1  jmcneill 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
     51  1.1  jmcneill #define HWRITE4(sc, reg, val)						\
     52  1.1  jmcneill 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
     53  1.1  jmcneill 
     54  1.1  jmcneill struct qciic_softc {
     55  1.1  jmcneill 	device_t		sc_dev;
     56  1.1  jmcneill 	struct acpi_devnode	*sc_acpi;
     57  1.1  jmcneill 	bus_space_tag_t		sc_iot;
     58  1.1  jmcneill 	bus_space_handle_t	sc_ioh;
     59  1.1  jmcneill 
     60  1.1  jmcneill 	device_t		sc_iic;
     61  1.1  jmcneill 
     62  1.1  jmcneill 	struct i2c_controller	sc_ic;
     63  1.1  jmcneill };
     64  1.1  jmcneill 
     65  1.1  jmcneill static int	qciic_acpi_match(device_t, cfdata_t, void *);
     66  1.1  jmcneill static void	qciic_acpi_attach(device_t, device_t, void *);
     67  1.1  jmcneill static int	qciic_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
     68  1.1  jmcneill 		    void *, size_t, int);
     69  1.1  jmcneill 
     70  1.1  jmcneill CFATTACH_DECL_NEW(qcomiic, sizeof(struct qciic_softc),
     71  1.1  jmcneill     qciic_acpi_match, qciic_acpi_attach, NULL, NULL);
     72  1.1  jmcneill 
     73  1.1  jmcneill static const struct device_compatible_entry compat_data[] = {
     74  1.1  jmcneill 	{ .compat = "QCOM0610" },
     75  1.1  jmcneill 	{ .compat = "QCOM0811" },
     76  1.1  jmcneill 	{ .compat = "QCOM0C10" },
     77  1.1  jmcneill 	DEVICE_COMPAT_EOL
     78  1.1  jmcneill };
     79  1.1  jmcneill 
     80  1.1  jmcneill static int
     81  1.1  jmcneill qciic_acpi_match(device_t parent, cfdata_t cf, void *aux)
     82  1.1  jmcneill {
     83  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
     84  1.1  jmcneill 
     85  1.1  jmcneill 	return acpi_compatible_match(aa, compat_data);
     86  1.1  jmcneill }
     87  1.1  jmcneill 
     88  1.1  jmcneill static void
     89  1.1  jmcneill qciic_acpi_attach(device_t parent, device_t self, void *aux)
     90  1.1  jmcneill {
     91  1.1  jmcneill 	struct qciic_softc * const sc = device_private(self);
     92  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
     93  1.1  jmcneill 	struct acpi_resources res;
     94  1.1  jmcneill 	struct acpi_mem *mem;
     95  1.1  jmcneill 	struct acpi_irq *irq;
     96  1.1  jmcneill 	ACPI_STATUS rv;
     97  1.1  jmcneill 	int error;
     98  1.1  jmcneill 
     99  1.1  jmcneill 	sc->sc_dev = self;
    100  1.1  jmcneill 	sc->sc_acpi = aa->aa_node;
    101  1.1  jmcneill 	sc->sc_iot = aa->aa_memt;
    102  1.1  jmcneill 
    103  1.1  jmcneill 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
    104  1.1  jmcneill 	    &res, &acpi_resource_parse_ops_default);
    105  1.1  jmcneill 	if (ACPI_FAILURE(rv)) {
    106  1.1  jmcneill 		return;
    107  1.1  jmcneill 	}
    108  1.1  jmcneill 
    109  1.1  jmcneill 	mem = acpi_res_mem(&res, 0);
    110  1.1  jmcneill 	if (mem == NULL) {
    111  1.1  jmcneill 		aprint_error_dev(self, "couldn't find mem resource\n");
    112  1.1  jmcneill 		goto done;
    113  1.1  jmcneill 	}
    114  1.1  jmcneill 
    115  1.1  jmcneill 	irq = acpi_res_irq(&res, 0);
    116  1.1  jmcneill 	if (irq == NULL) {
    117  1.1  jmcneill 		aprint_error_dev(self, "couldn't find irq resource\n");
    118  1.1  jmcneill 		goto done;
    119  1.1  jmcneill 	}
    120  1.1  jmcneill 
    121  1.1  jmcneill 	error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0,
    122  1.1  jmcneill 	    &sc->sc_ioh);
    123  1.1  jmcneill 	if (error != 0) {
    124  1.1  jmcneill 		aprint_error_dev(self, "couldn't map registers\n");
    125  1.1  jmcneill 		return;
    126  1.1  jmcneill 	}
    127  1.1  jmcneill 
    128  1.1  jmcneill 	iic_tag_init(&sc->sc_ic);
    129  1.1  jmcneill 	sc->sc_ic.ic_cookie = sc;
    130  1.1  jmcneill 	sc->sc_ic.ic_exec = qciic_exec;
    131  1.1  jmcneill 
    132  1.2  jmcneill 	acpi_i2c_register(aa->aa_node, self, &sc->sc_ic);
    133  1.2  jmcneill 
    134  1.4   thorpej 	iicbus_attach(self, &sc->sc_ic);
    135  1.3  jmcneill 
    136  1.3  jmcneill done:
    137  1.3  jmcneill 	acpi_resource_cleanup(&res);
    138  1.1  jmcneill }
    139  1.1  jmcneill 
    140  1.1  jmcneill static int
    141  1.1  jmcneill qciic_wait(struct qciic_softc *sc, uint32_t bits)
    142  1.1  jmcneill {
    143  1.1  jmcneill 	uint32_t stat;
    144  1.1  jmcneill 	int timo;
    145  1.1  jmcneill 
    146  1.1  jmcneill 	for (timo = 50000; timo > 0; timo--) {
    147  1.1  jmcneill 		stat = HREAD4(sc, GENI_M_IRQ_STATUS);
    148  1.1  jmcneill 		if (stat & bits)
    149  1.1  jmcneill 			break;
    150  1.1  jmcneill 		delay(10);
    151  1.1  jmcneill 	}
    152  1.1  jmcneill 	if (timo == 0)
    153  1.1  jmcneill 		return ETIMEDOUT;
    154  1.1  jmcneill 
    155  1.1  jmcneill 	return 0;
    156  1.1  jmcneill }
    157  1.1  jmcneill 
    158  1.1  jmcneill static int
    159  1.1  jmcneill qciic_read(struct qciic_softc *sc, uint8_t *buf, size_t len)
    160  1.1  jmcneill {
    161  1.1  jmcneill 	uint32_t stat, word;
    162  1.1  jmcneill 	int timo, i;
    163  1.1  jmcneill 
    164  1.1  jmcneill 	word = 0;
    165  1.1  jmcneill 	for (i = 0; i < len; i++) {
    166  1.1  jmcneill 		if ((i % 4) == 0) {
    167  1.1  jmcneill 			for (timo = 50000; timo > 0; timo--) {
    168  1.1  jmcneill 				stat = HREAD4(sc, GENI_RX_FIFO_STATUS);
    169  1.1  jmcneill 				if (GENI_RX_FIFO_STATUS_WC(stat) > 0)
    170  1.1  jmcneill 					break;
    171  1.1  jmcneill 				delay(10);
    172  1.1  jmcneill 			}
    173  1.1  jmcneill 			if (timo == 0)
    174  1.1  jmcneill 				return ETIMEDOUT;
    175  1.1  jmcneill 			word = HREAD4(sc, GENI_RX_FIFO);
    176  1.1  jmcneill 		}
    177  1.1  jmcneill 		buf[i] = word >> ((i % 4) * 8);
    178  1.1  jmcneill 	}
    179  1.1  jmcneill 
    180  1.1  jmcneill 	return 0;
    181  1.1  jmcneill }
    182  1.1  jmcneill 
    183  1.1  jmcneill static int
    184  1.1  jmcneill qciic_write(struct qciic_softc *sc, const uint8_t *buf, size_t len)
    185  1.1  jmcneill {
    186  1.1  jmcneill 	uint32_t stat, word;
    187  1.1  jmcneill 	int timo, i;
    188  1.1  jmcneill 
    189  1.1  jmcneill 	word = 0;
    190  1.1  jmcneill 	for (i = 0; i < len; i++) {
    191  1.1  jmcneill 		word |= buf[i] << ((i % 4) * 8);
    192  1.1  jmcneill 		if ((i % 4) == 3 || i == (len - 1)) {
    193  1.1  jmcneill 			for (timo = 50000; timo > 0; timo--) {
    194  1.1  jmcneill 				stat = HREAD4(sc, GENI_TX_FIFO_STATUS);
    195  1.1  jmcneill 				if (stat < 16)
    196  1.1  jmcneill 					break;
    197  1.1  jmcneill 				delay(10);
    198  1.1  jmcneill 			}
    199  1.1  jmcneill 			if (timo == 0)
    200  1.1  jmcneill 				return ETIMEDOUT;
    201  1.1  jmcneill 			HWRITE4(sc, GENI_TX_FIFO, word);
    202  1.1  jmcneill 			word = 0;
    203  1.1  jmcneill 		}
    204  1.1  jmcneill 	}
    205  1.1  jmcneill 
    206  1.1  jmcneill 	return 0;
    207  1.1  jmcneill }
    208  1.1  jmcneill 
    209  1.1  jmcneill static int
    210  1.1  jmcneill qciic_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmd,
    211  1.1  jmcneill     size_t cmdlen, void *buf, size_t buflen, int flags)
    212  1.1  jmcneill {
    213  1.1  jmcneill 	struct qciic_softc *sc = cookie;
    214  1.1  jmcneill 	uint32_t m_cmd, m_param, stat;
    215  1.1  jmcneill 	int error;
    216  1.1  jmcneill 
    217  1.1  jmcneill 	m_param = addr << GENI_M_CMD0_SLV_ADDR_SHIFT;
    218  1.1  jmcneill 	m_param |= GENI_M_CMD0_STOP_STRETCH;
    219  1.1  jmcneill 
    220  1.1  jmcneill 	if (buflen == 0 && I2C_OP_STOP_P(op))
    221  1.1  jmcneill 		m_param &= ~GENI_M_CMD0_STOP_STRETCH;
    222  1.1  jmcneill 
    223  1.1  jmcneill 	if (cmdlen > 0) {
    224  1.1  jmcneill 		stat = HREAD4(sc, GENI_M_IRQ_STATUS);
    225  1.1  jmcneill 		HWRITE4(sc, GENI_M_IRQ_CLEAR, stat);
    226  1.1  jmcneill 		HWRITE4(sc, GENI_I2C_TX_TRANS_LEN, cmdlen);
    227  1.1  jmcneill 		m_cmd = GENI_M_CMD0_OPCODE_I2C_WRITE | m_param;
    228  1.1  jmcneill 		HWRITE4(sc, GENI_M_CMD0, m_cmd);
    229  1.1  jmcneill 
    230  1.1  jmcneill 		error = qciic_write(sc, cmd, cmdlen);
    231  1.1  jmcneill 		if (error)
    232  1.1  jmcneill 			return error;
    233  1.1  jmcneill 
    234  1.1  jmcneill 		error = qciic_wait(sc, GENI_M_IRQ_CMD_DONE);
    235  1.1  jmcneill 		if (error)
    236  1.1  jmcneill 			return error;
    237  1.1  jmcneill 	}
    238  1.1  jmcneill 
    239  1.1  jmcneill 	if (buflen == 0)
    240  1.1  jmcneill 		return 0;
    241  1.1  jmcneill 
    242  1.1  jmcneill 	if (I2C_OP_STOP_P(op))
    243  1.1  jmcneill 		m_param &= ~GENI_M_CMD0_STOP_STRETCH;
    244  1.1  jmcneill 
    245  1.1  jmcneill 	if (I2C_OP_READ_P(op)) {
    246  1.1  jmcneill 		stat = HREAD4(sc, GENI_M_IRQ_STATUS);
    247  1.1  jmcneill 		HWRITE4(sc, GENI_M_IRQ_CLEAR, stat);
    248  1.1  jmcneill 		HWRITE4(sc, GENI_I2C_RX_TRANS_LEN, buflen);
    249  1.1  jmcneill 		m_cmd = GENI_M_CMD0_OPCODE_I2C_READ | m_param;
    250  1.1  jmcneill 		HWRITE4(sc, GENI_M_CMD0, m_cmd);
    251  1.1  jmcneill 
    252  1.1  jmcneill 		error = qciic_read(sc, buf, buflen);
    253  1.1  jmcneill 		if (error)
    254  1.1  jmcneill 			return error;
    255  1.1  jmcneill 
    256  1.1  jmcneill 		error = qciic_wait(sc, GENI_M_IRQ_CMD_DONE);
    257  1.1  jmcneill 		if (error)
    258  1.1  jmcneill 			return error;
    259  1.1  jmcneill 	} else {
    260  1.1  jmcneill 		stat = HREAD4(sc, GENI_M_IRQ_STATUS);
    261  1.1  jmcneill 		HWRITE4(sc, GENI_M_IRQ_CLEAR, stat);
    262  1.1  jmcneill 		HWRITE4(sc, GENI_I2C_TX_TRANS_LEN, buflen);
    263  1.1  jmcneill 		m_cmd = GENI_M_CMD0_OPCODE_I2C_WRITE | m_param;
    264  1.1  jmcneill 		HWRITE4(sc, GENI_M_CMD0, m_cmd);
    265  1.1  jmcneill 
    266  1.1  jmcneill 		error = qciic_write(sc, buf, buflen);
    267  1.1  jmcneill 		if (error)
    268  1.1  jmcneill 			return error;
    269  1.1  jmcneill 
    270  1.1  jmcneill 		error = qciic_wait(sc, GENI_M_IRQ_CMD_DONE);
    271  1.1  jmcneill 		if (error)
    272  1.1  jmcneill 			return error;
    273  1.1  jmcneill 	}
    274  1.1  jmcneill 
    275  1.1  jmcneill 	return 0;
    276  1.1  jmcneill }
    277