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