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