xref: /openbsd-src/sys/dev/fdt/rkiic.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /*	$OpenBSD: rkiic.c,v 1.4 2018/12/31 21:59:44 kettenis Exp $	*/
2 /*
3  * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 
22 #include <machine/intr.h>
23 #include <machine/bus.h>
24 #include <machine/fdt.h>
25 
26 #define _I2C_PRIVATE
27 #include <dev/i2c/i2cvar.h>
28 
29 #include <dev/ofw/openfirm.h>
30 #include <dev/ofw/ofw_clock.h>
31 #include <dev/ofw/ofw_pinctrl.h>
32 #include <dev/ofw/fdt.h>
33 
34 /* Registers. */
35 #define RKI2C_CON		0x0000
36 #define  RKI2C_CON_ACT2NAK		(1 << 6)
37 #define  RKI2C_CON_ACK			(0 << 5)
38 #define  RKI2C_CON_NAK			(1 << 5)
39 #define  RKI2C_CON_STOP			(1 << 4)
40 #define  RKI2C_CON_START		(1 << 3)
41 #define  RKI2C_CON_I2C_MODE_MASK	(3 << 1)
42 #define  RKI2C_CON_I2C_MODE_TX		(0 << 1)
43 #define  RKI2C_CON_I2C_MODE_RRX		(1 << 1)
44 #define  RKI2C_CON_I2C_MODE_RX		(2 << 1)
45 #define  RKI2C_CON_I2C_MODE_BROKEN	(3 << 1)
46 #define  RKI2C_CON_I2C_EN		(1 << 0)
47 #define RKI2C_CLKDIV		0x0004
48 #define RKI2C_MRXADDR		0x0008
49 #define  RKI2C_MRXADDR_ADDLVLD		(1 << 24)
50 #define RKI2C_MRXRADDR		0x000c
51 #define  RKI2C_MRXRADDR_SRADDLVLD	(1 << 24)
52 #define RKI2C_MTXCNT		0x0010
53 #define RKI2C_MRXCNT		0x0014
54 #define RKI2C_IEN		0x0018
55 #define  RKI2C_IEN_START		(1 << 4)
56 #define RKI2C_IPD		0x001c
57 #define  RKI2C_IPD_STOP			(1 << 5)
58 #define  RKI2C_IPD_START		(1 << 4)
59 #define  RKI2C_IPD_MBRF			(1 << 3)
60 #define  RKI2C_IPD_MBTF			(1 << 2)
61 #define  RKI2C_IPD_ALL			0xff
62 #define RKI2C_FCNT		0x0020
63 #define RKI2C_SCL_OE_DB		0x0024
64 #define RKI2C_TXDATA0		0x0100
65 #define RKI2C_RXDATA0		0x0200
66 #define RKI2C_ST		0x0220
67 
68 #define HREAD4(sc, reg)							\
69 	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
70 #define HWRITE4(sc, reg, val)						\
71 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
72 #define HSET4(sc, reg, bits)						\
73 	HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
74 #define HCLR4(sc, reg, bits)						\
75 	HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))
76 
77 struct rkiic_softc {
78 	struct device		sc_dev;
79 	bus_space_tag_t		sc_iot;
80 	bus_space_handle_t	sc_ioh;
81 
82 	int			sc_node;
83 	struct i2c_controller	sc_ic;
84 };
85 
86 int rkiic_match(struct device *, void *, void *);
87 void rkiic_attach(struct device *, struct device *, void *);
88 
89 struct cfattach	rkiic_ca = {
90 	sizeof (struct rkiic_softc), rkiic_match, rkiic_attach
91 };
92 
93 struct cfdriver rkiic_cd = {
94 	NULL, "rkiic", DV_DULL
95 };
96 
97 int	rkiic_acquire_bus(void *, int);
98 void	rkiic_release_bus(void *, int);
99 int	rkiic_send_start(void *, int);
100 int	rkiic_send_stop(void *, int);
101 int	rkiic_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
102 	    void *, size_t, int);
103 
104 void	rkiic_bus_scan(struct device *, struct i2cbus_attach_args *, void *);
105 
106 int
107 rkiic_match(struct device *parent, void *match, void *aux)
108 {
109 	struct fdt_attach_args *faa = aux;
110 
111 	return (OF_is_compatible(faa->fa_node, "rockchip,rk3288-i2c") ||
112 	    OF_is_compatible(faa->fa_node, "rockchip,rk3328-i2c") ||
113 	    OF_is_compatible(faa->fa_node, "rockchip,rk3399-i2c"));
114 }
115 
116 void
117 rkiic_attach(struct device *parent, struct device *self, void *aux)
118 {
119 	struct rkiic_softc *sc = (struct rkiic_softc *)self;
120 	struct fdt_attach_args *faa = aux;
121 	struct i2cbus_attach_args iba;
122 	uint32_t clock_speed, bus_speed;
123 	uint32_t div, divl, divh;
124 	uint32_t clkdivl, clkdivh;
125 
126 	if (faa->fa_nreg < 1) {
127 		printf(": no registers\n");
128 		return;
129 	}
130 
131 	sc->sc_iot = faa->fa_iot;
132 	sc->sc_node = faa->fa_node;
133 
134 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
135 	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
136 		printf(": can't map registers\n");
137 		return;
138 	}
139 
140 	printf("\n");
141 
142 	pinctrl_byname(sc->sc_node, "default");
143 
144 	clock_set_assigned(sc->sc_node);
145 	clock_enable(sc->sc_node, "i2c");
146 	clock_enable(sc->sc_node, "pclk");
147 
148 	clock_speed = clock_get_frequency(sc->sc_node, "i2c");
149 	bus_speed = OF_getpropint(sc->sc_node, "clock-frequency", 100000);
150 
151 	div = 2;
152 	while (clock_speed > div * bus_speed * 8)
153 		div++;
154 	divl = div / 2;
155 	divh = div - divl;
156 	clkdivl = (divl - 1) & 0xffff;
157 	clkdivh = (divh - 1) & 0xffff;
158 	HWRITE4(sc, RKI2C_CLKDIV, clkdivh << 16 | clkdivl);
159 
160 	sc->sc_ic.ic_cookie = sc;
161 	sc->sc_ic.ic_acquire_bus = rkiic_acquire_bus;
162 	sc->sc_ic.ic_release_bus = rkiic_release_bus;
163 	sc->sc_ic.ic_exec = rkiic_exec;
164 
165 	/* Configure its children */
166 	memset(&iba, 0, sizeof(iba));
167 	iba.iba_name = "iic";
168 	iba.iba_tag = &sc->sc_ic;
169 	iba.iba_bus_scan = rkiic_bus_scan;
170 	iba.iba_bus_scan_arg = &sc->sc_node;
171 
172 	config_found(&sc->sc_dev, &iba, iicbus_print);
173 }
174 
175 int
176 rkiic_acquire_bus(void *cookie, int flags)
177 {
178 	struct rkiic_softc *sc = cookie;
179 
180 	HSET4(sc, RKI2C_CON, RKI2C_CON_I2C_EN);
181 	return 0;
182 }
183 
184 void
185 rkiic_release_bus(void *cookie, int flags)
186 {
187 	struct rkiic_softc *sc = cookie;
188 
189 	HCLR4(sc, RKI2C_CON, RKI2C_CON_I2C_EN);
190 }
191 
192 int
193 rkiic_send_start(void *cookie, int flags)
194 {
195 	struct rkiic_softc *sc = cookie;
196 	int timo;
197 
198 	HSET4(sc, RKI2C_IPD, RKI2C_IPD_START);
199 	HSET4(sc, RKI2C_CON, RKI2C_CON_START);
200 	for (timo = 1000; timo > 0; timo--) {
201 		if (HREAD4(sc, RKI2C_IPD) & RKI2C_IPD_START)
202 			break;
203 		delay(10);
204 	}
205 	HCLR4(sc, RKI2C_CON, RKI2C_CON_START);
206 	if (timo == 0)
207 		return ETIMEDOUT;
208 	return 0;
209 }
210 
211 int
212 rkiic_send_stop(void *cookie, int flags)
213 {
214 	struct rkiic_softc *sc = cookie;
215 	int timo;
216 
217 	HSET4(sc, RKI2C_IPD, RKI2C_IPD_STOP);
218 	HSET4(sc, RKI2C_CON, RKI2C_CON_STOP);
219 	for (timo = 1000; timo > 0; timo--) {
220 		if (HREAD4(sc, RKI2C_IPD) & RKI2C_IPD_STOP)
221 			break;
222 		delay(10);
223 	}
224 	HCLR4(sc, RKI2C_CON, RKI2C_CON_STOP);
225 	if (timo == 0)
226 		return ETIMEDOUT;
227 	return 0;
228 }
229 
230 int
231 rkiic_write(struct rkiic_softc *sc, i2c_addr_t addr, const void *cmd,
232     size_t cmdlen, void *buf, size_t buflen)
233 {
234 	uint8_t txbuf[32];
235 	int len = 0;
236 	int timo, i;
237 
238 	/*
239 	 * Lump slave address, command and data into one single buffer
240 	 * and transfer it in a single operation.
241 	 */
242 	txbuf[len++] = addr << 1;
243 	for (i = 0; i < cmdlen; i++)
244 		txbuf[len++] = ((uint8_t *)cmd)[i];
245 	for (i = 0; i < buflen; i++)
246 		txbuf[len++] = ((uint8_t *)buf)[i];
247 
248 	for (i = 0; i < len; i += 4) {
249 		HWRITE4(sc, RKI2C_TXDATA0 + i,
250 			*((uint32_t *)&txbuf[i]));
251 	}
252 
253 	/* Start operation. */
254 	HWRITE4(sc, RKI2C_MTXCNT, len);
255 
256 	/* Wait for completion. */
257 	for (timo = 1000; timo > 0; timo--) {
258 		if (HREAD4(sc, RKI2C_IPD) & RKI2C_IPD_MBTF)
259 			break;
260 		delay(10);
261 	}
262 	if (timo == 0)
263 		return ETIMEDOUT;
264 
265 	return 0;
266 }
267 
268 int
269 rkiic_read(struct rkiic_softc *sc, i2c_addr_t addr, const void *cmd,
270     size_t cmdlen, void *buf, size_t buflen)
271 {
272 	uint32_t mrxraddr, rxdata;
273 	int timo, i;
274 
275 	HWRITE4(sc, RKI2C_MRXADDR, (addr << 1) | RKI2C_MRXADDR_ADDLVLD);
276 
277 	/* Send the command as "register address". */
278 	mrxraddr = 0;
279 	for (i = 0; i < cmdlen; i++) {
280 		mrxraddr |= ((uint8_t *)cmd)[i] << (i * 8);
281 		mrxraddr |= RKI2C_MRXRADDR_SRADDLVLD << i;
282 	}
283 	HWRITE4(sc, RKI2C_MRXRADDR, mrxraddr);
284 
285 	/* Indicate that we're done after this operation. */
286 	HSET4(sc, RKI2C_CON, RKI2C_CON_NAK);
287 
288 	/* Start operation. */
289 	HWRITE4(sc, RKI2C_MRXCNT, buflen);
290 
291 	/* Wait for completion. */
292 	for (timo = 1000; timo > 0; timo--) {
293 		if (HREAD4(sc, RKI2C_IPD) & RKI2C_IPD_MBRF)
294 			break;
295 		delay(10);
296 	}
297 	if (timo == 0)
298 		return ETIMEDOUT;
299 
300 	for (i = 0; i < buflen; i++) {
301 		if (i % 4 == 0)
302 			rxdata = HREAD4(sc, RKI2C_RXDATA0 + i);
303 		((uint8_t *)buf)[i] = rxdata;
304 		rxdata >>= 8;
305 	}
306 
307 	return 0;
308 }
309 
310 int
311 rkiic_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmd,
312     size_t cmdlen, void *buf, size_t buflen, int flags)
313 {
314 	struct rkiic_softc *sc = cookie;
315 	uint32_t con;
316 	int error;
317 
318 	if (cmdlen > 3 || buflen > 28)
319 		return EINVAL;
320 
321 	/* Clear interrupts.  */
322 	HWRITE4(sc, RKI2C_IPD, RKI2C_IPD_ALL);
323 
324 	/* Configure transfer more. */
325 	con = HREAD4(sc, RKI2C_CON);
326 	con &= ~RKI2C_CON_I2C_MODE_MASK;
327 	if (I2C_OP_WRITE_P(op))
328 		con |= RKI2C_CON_I2C_MODE_TX;
329 	else
330 		con |= RKI2C_CON_I2C_MODE_RRX;
331 	con &= ~RKI2C_CON_NAK;
332 	con |= RKI2C_CON_ACT2NAK;
333 	HWRITE4(sc, RKI2C_CON, con);
334 
335 	error = rkiic_send_start(sc, flags);
336 	if (error)
337 		return error;
338 
339 	if (I2C_OP_WRITE_P(op))
340 		error = rkiic_write(sc, addr, cmd, cmdlen, buf, buflen);
341 	else
342 		error = rkiic_read(sc, addr, cmd, cmdlen, buf, buflen);
343 
344 	if (I2C_OP_STOP_P(op))
345 		rkiic_send_stop(sc, flags);
346 
347 	return error;
348 }
349 
350 void
351 rkiic_bus_scan(struct device *self, struct i2cbus_attach_args *iba, void *arg)
352 {
353 	int iba_node = *(int *)arg;
354 	struct i2c_attach_args ia;
355 	char name[32];
356 	uint32_t reg[1];
357 	int node;
358 
359 	for (node = OF_child(iba_node); node; node = OF_peer(node)) {
360 		memset(name, 0, sizeof(name));
361 		memset(reg, 0, sizeof(reg));
362 
363 		if (OF_getprop(node, "compatible", name, sizeof(name)) == -1)
364 			continue;
365 		if (name[0] == '\0')
366 			continue;
367 
368 		if (OF_getprop(node, "reg", &reg, sizeof(reg)) != sizeof(reg))
369 			continue;
370 
371 		memset(&ia, 0, sizeof(ia));
372 		ia.ia_tag = iba->iba_tag;
373 		ia.ia_addr = bemtoh32(&reg[0]);
374 		ia.ia_name = name;
375 		ia.ia_cookie = &node;
376 		config_found(self, &ia, iic_print);
377 	}
378 }
379