qcomiic.c revision 1.1 1 1.1 jmcneill /* $NetBSD: qcomiic.c,v 1.1 2024/12/08 20:49:14 jmcneill 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 void qciic_acpi_attach_late(device_t);
68 1.1 jmcneill static int qciic_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
69 1.1 jmcneill void *, size_t, int);
70 1.1 jmcneill
71 1.1 jmcneill CFATTACH_DECL_NEW(qcomiic, sizeof(struct qciic_softc),
72 1.1 jmcneill qciic_acpi_match, qciic_acpi_attach, NULL, NULL);
73 1.1 jmcneill
74 1.1 jmcneill static const struct device_compatible_entry compat_data[] = {
75 1.1 jmcneill { .compat = "QCOM0610" },
76 1.1 jmcneill { .compat = "QCOM0811" },
77 1.1 jmcneill { .compat = "QCOM0C10" },
78 1.1 jmcneill DEVICE_COMPAT_EOL
79 1.1 jmcneill };
80 1.1 jmcneill
81 1.1 jmcneill static int
82 1.1 jmcneill qciic_acpi_match(device_t parent, cfdata_t cf, void *aux)
83 1.1 jmcneill {
84 1.1 jmcneill struct acpi_attach_args *aa = aux;
85 1.1 jmcneill
86 1.1 jmcneill return acpi_compatible_match(aa, compat_data);
87 1.1 jmcneill }
88 1.1 jmcneill
89 1.1 jmcneill static void
90 1.1 jmcneill qciic_acpi_attach(device_t parent, device_t self, void *aux)
91 1.1 jmcneill {
92 1.1 jmcneill struct qciic_softc * const sc = device_private(self);
93 1.1 jmcneill struct acpi_attach_args *aa = aux;
94 1.1 jmcneill struct acpi_resources res;
95 1.1 jmcneill struct acpi_mem *mem;
96 1.1 jmcneill struct acpi_irq *irq;
97 1.1 jmcneill ACPI_STATUS rv;
98 1.1 jmcneill int error;
99 1.1 jmcneill
100 1.1 jmcneill sc->sc_dev = self;
101 1.1 jmcneill sc->sc_acpi = aa->aa_node;
102 1.1 jmcneill sc->sc_iot = aa->aa_memt;
103 1.1 jmcneill
104 1.1 jmcneill rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
105 1.1 jmcneill &res, &acpi_resource_parse_ops_default);
106 1.1 jmcneill if (ACPI_FAILURE(rv)) {
107 1.1 jmcneill return;
108 1.1 jmcneill }
109 1.1 jmcneill
110 1.1 jmcneill mem = acpi_res_mem(&res, 0);
111 1.1 jmcneill if (mem == NULL) {
112 1.1 jmcneill aprint_error_dev(self, "couldn't find mem resource\n");
113 1.1 jmcneill goto done;
114 1.1 jmcneill }
115 1.1 jmcneill
116 1.1 jmcneill irq = acpi_res_irq(&res, 0);
117 1.1 jmcneill if (irq == NULL) {
118 1.1 jmcneill aprint_error_dev(self, "couldn't find irq resource\n");
119 1.1 jmcneill goto done;
120 1.1 jmcneill }
121 1.1 jmcneill
122 1.1 jmcneill error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0,
123 1.1 jmcneill &sc->sc_ioh);
124 1.1 jmcneill if (error != 0) {
125 1.1 jmcneill aprint_error_dev(self, "couldn't map registers\n");
126 1.1 jmcneill return;
127 1.1 jmcneill }
128 1.1 jmcneill
129 1.1 jmcneill iic_tag_init(&sc->sc_ic);
130 1.1 jmcneill sc->sc_ic.ic_cookie = sc;
131 1.1 jmcneill sc->sc_ic.ic_exec = qciic_exec;
132 1.1 jmcneill
133 1.1 jmcneill /*
134 1.1 jmcneill * Defer the attachment of I2C bus until all ACPI devices have been
135 1.1 jmcneill * enumerated, as other devices may provide resources for devices
136 1.1 jmcneill * attached to the I2C bus.
137 1.1 jmcneill */
138 1.1 jmcneill config_defer(self, qciic_acpi_attach_late);
139 1.1 jmcneill
140 1.1 jmcneill done:
141 1.1 jmcneill acpi_resource_cleanup(&res);
142 1.1 jmcneill }
143 1.1 jmcneill
144 1.1 jmcneill static void
145 1.1 jmcneill qciic_acpi_attach_late(device_t self)
146 1.1 jmcneill {
147 1.1 jmcneill struct i2cbus_attach_args iba;
148 1.1 jmcneill struct qciic_softc * const sc = device_private(self);
149 1.1 jmcneill
150 1.1 jmcneill memset(&iba, 0, sizeof(iba));
151 1.1 jmcneill iba.iba_tag = &sc->sc_ic;
152 1.1 jmcneill iba.iba_child_devices = acpi_enter_i2c_devs(self, sc->sc_acpi);
153 1.1 jmcneill
154 1.1 jmcneill config_found(self, &iba, iicbus_print, CFARGS_NONE);
155 1.1 jmcneill }
156 1.1 jmcneill
157 1.1 jmcneill static int
158 1.1 jmcneill qciic_wait(struct qciic_softc *sc, uint32_t bits)
159 1.1 jmcneill {
160 1.1 jmcneill uint32_t stat;
161 1.1 jmcneill int timo;
162 1.1 jmcneill
163 1.1 jmcneill for (timo = 50000; timo > 0; timo--) {
164 1.1 jmcneill stat = HREAD4(sc, GENI_M_IRQ_STATUS);
165 1.1 jmcneill if (stat & bits)
166 1.1 jmcneill break;
167 1.1 jmcneill delay(10);
168 1.1 jmcneill }
169 1.1 jmcneill if (timo == 0)
170 1.1 jmcneill return ETIMEDOUT;
171 1.1 jmcneill
172 1.1 jmcneill return 0;
173 1.1 jmcneill }
174 1.1 jmcneill
175 1.1 jmcneill static int
176 1.1 jmcneill qciic_read(struct qciic_softc *sc, uint8_t *buf, size_t len)
177 1.1 jmcneill {
178 1.1 jmcneill uint32_t stat, word;
179 1.1 jmcneill int timo, i;
180 1.1 jmcneill
181 1.1 jmcneill word = 0;
182 1.1 jmcneill for (i = 0; i < len; i++) {
183 1.1 jmcneill if ((i % 4) == 0) {
184 1.1 jmcneill for (timo = 50000; timo > 0; timo--) {
185 1.1 jmcneill stat = HREAD4(sc, GENI_RX_FIFO_STATUS);
186 1.1 jmcneill if (GENI_RX_FIFO_STATUS_WC(stat) > 0)
187 1.1 jmcneill break;
188 1.1 jmcneill delay(10);
189 1.1 jmcneill }
190 1.1 jmcneill if (timo == 0)
191 1.1 jmcneill return ETIMEDOUT;
192 1.1 jmcneill word = HREAD4(sc, GENI_RX_FIFO);
193 1.1 jmcneill }
194 1.1 jmcneill buf[i] = word >> ((i % 4) * 8);
195 1.1 jmcneill }
196 1.1 jmcneill
197 1.1 jmcneill return 0;
198 1.1 jmcneill }
199 1.1 jmcneill
200 1.1 jmcneill static int
201 1.1 jmcneill qciic_write(struct qciic_softc *sc, const uint8_t *buf, size_t len)
202 1.1 jmcneill {
203 1.1 jmcneill uint32_t stat, word;
204 1.1 jmcneill int timo, i;
205 1.1 jmcneill
206 1.1 jmcneill word = 0;
207 1.1 jmcneill for (i = 0; i < len; i++) {
208 1.1 jmcneill word |= buf[i] << ((i % 4) * 8);
209 1.1 jmcneill if ((i % 4) == 3 || i == (len - 1)) {
210 1.1 jmcneill for (timo = 50000; timo > 0; timo--) {
211 1.1 jmcneill stat = HREAD4(sc, GENI_TX_FIFO_STATUS);
212 1.1 jmcneill if (stat < 16)
213 1.1 jmcneill break;
214 1.1 jmcneill delay(10);
215 1.1 jmcneill }
216 1.1 jmcneill if (timo == 0)
217 1.1 jmcneill return ETIMEDOUT;
218 1.1 jmcneill HWRITE4(sc, GENI_TX_FIFO, word);
219 1.1 jmcneill word = 0;
220 1.1 jmcneill }
221 1.1 jmcneill }
222 1.1 jmcneill
223 1.1 jmcneill return 0;
224 1.1 jmcneill }
225 1.1 jmcneill
226 1.1 jmcneill static int
227 1.1 jmcneill qciic_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmd,
228 1.1 jmcneill size_t cmdlen, void *buf, size_t buflen, int flags)
229 1.1 jmcneill {
230 1.1 jmcneill struct qciic_softc *sc = cookie;
231 1.1 jmcneill uint32_t m_cmd, m_param, stat;
232 1.1 jmcneill int error;
233 1.1 jmcneill
234 1.1 jmcneill m_param = addr << GENI_M_CMD0_SLV_ADDR_SHIFT;
235 1.1 jmcneill m_param |= GENI_M_CMD0_STOP_STRETCH;
236 1.1 jmcneill
237 1.1 jmcneill if (buflen == 0 && I2C_OP_STOP_P(op))
238 1.1 jmcneill m_param &= ~GENI_M_CMD0_STOP_STRETCH;
239 1.1 jmcneill
240 1.1 jmcneill if (cmdlen > 0) {
241 1.1 jmcneill stat = HREAD4(sc, GENI_M_IRQ_STATUS);
242 1.1 jmcneill HWRITE4(sc, GENI_M_IRQ_CLEAR, stat);
243 1.1 jmcneill HWRITE4(sc, GENI_I2C_TX_TRANS_LEN, cmdlen);
244 1.1 jmcneill m_cmd = GENI_M_CMD0_OPCODE_I2C_WRITE | m_param;
245 1.1 jmcneill HWRITE4(sc, GENI_M_CMD0, m_cmd);
246 1.1 jmcneill
247 1.1 jmcneill error = qciic_write(sc, cmd, cmdlen);
248 1.1 jmcneill if (error)
249 1.1 jmcneill return error;
250 1.1 jmcneill
251 1.1 jmcneill error = qciic_wait(sc, GENI_M_IRQ_CMD_DONE);
252 1.1 jmcneill if (error)
253 1.1 jmcneill return error;
254 1.1 jmcneill }
255 1.1 jmcneill
256 1.1 jmcneill if (buflen == 0)
257 1.1 jmcneill return 0;
258 1.1 jmcneill
259 1.1 jmcneill if (I2C_OP_STOP_P(op))
260 1.1 jmcneill m_param &= ~GENI_M_CMD0_STOP_STRETCH;
261 1.1 jmcneill
262 1.1 jmcneill if (I2C_OP_READ_P(op)) {
263 1.1 jmcneill stat = HREAD4(sc, GENI_M_IRQ_STATUS);
264 1.1 jmcneill HWRITE4(sc, GENI_M_IRQ_CLEAR, stat);
265 1.1 jmcneill HWRITE4(sc, GENI_I2C_RX_TRANS_LEN, buflen);
266 1.1 jmcneill m_cmd = GENI_M_CMD0_OPCODE_I2C_READ | m_param;
267 1.1 jmcneill HWRITE4(sc, GENI_M_CMD0, m_cmd);
268 1.1 jmcneill
269 1.1 jmcneill error = qciic_read(sc, buf, buflen);
270 1.1 jmcneill if (error)
271 1.1 jmcneill return error;
272 1.1 jmcneill
273 1.1 jmcneill error = qciic_wait(sc, GENI_M_IRQ_CMD_DONE);
274 1.1 jmcneill if (error)
275 1.1 jmcneill return error;
276 1.1 jmcneill } else {
277 1.1 jmcneill stat = HREAD4(sc, GENI_M_IRQ_STATUS);
278 1.1 jmcneill HWRITE4(sc, GENI_M_IRQ_CLEAR, stat);
279 1.1 jmcneill HWRITE4(sc, GENI_I2C_TX_TRANS_LEN, buflen);
280 1.1 jmcneill m_cmd = GENI_M_CMD0_OPCODE_I2C_WRITE | m_param;
281 1.1 jmcneill HWRITE4(sc, GENI_M_CMD0, m_cmd);
282 1.1 jmcneill
283 1.1 jmcneill error = qciic_write(sc, buf, buflen);
284 1.1 jmcneill if (error)
285 1.1 jmcneill return error;
286 1.1 jmcneill
287 1.1 jmcneill error = qciic_wait(sc, GENI_M_IRQ_CMD_DONE);
288 1.1 jmcneill if (error)
289 1.1 jmcneill return error;
290 1.1 jmcneill }
291 1.1 jmcneill
292 1.1 jmcneill return 0;
293 1.1 jmcneill }
294