rk_i2c.c revision 1.1.2.2 1 1.1.2.2 pgoyette /* $NetBSD: rk_i2c.c,v 1.1.2.2 2018/07/28 04:37:29 pgoyette Exp $ */
2 1.1.2.2 pgoyette
3 1.1.2.2 pgoyette /*-
4 1.1.2.2 pgoyette * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1.2.2 pgoyette * All rights reserved.
6 1.1.2.2 pgoyette *
7 1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
9 1.1.2.2 pgoyette * are met:
10 1.1.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1.2.2 pgoyette *
16 1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.1.2.2 pgoyette */
28 1.1.2.2 pgoyette
29 1.1.2.2 pgoyette #include <sys/cdefs.h>
30 1.1.2.2 pgoyette
31 1.1.2.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: rk_i2c.c,v 1.1.2.2 2018/07/28 04:37:29 pgoyette Exp $");
32 1.1.2.2 pgoyette
33 1.1.2.2 pgoyette #include <sys/param.h>
34 1.1.2.2 pgoyette #include <sys/bus.h>
35 1.1.2.2 pgoyette #include <sys/device.h>
36 1.1.2.2 pgoyette #include <sys/intr.h>
37 1.1.2.2 pgoyette #include <sys/systm.h>
38 1.1.2.2 pgoyette #include <sys/time.h>
39 1.1.2.2 pgoyette #include <sys/kmem.h>
40 1.1.2.2 pgoyette
41 1.1.2.2 pgoyette #include <dev/i2c/i2cvar.h>
42 1.1.2.2 pgoyette
43 1.1.2.2 pgoyette #include <dev/fdt/fdtvar.h>
44 1.1.2.2 pgoyette
45 1.1.2.2 pgoyette #define RKI2C_CON 0x000
46 1.1.2.2 pgoyette #define RKI2C_CON_ACT2NAK __BIT(6)
47 1.1.2.2 pgoyette #define RKI2C_CON_ACK __BIT(5)
48 1.1.2.2 pgoyette #define RKI2C_CON_STOP __BIT(4)
49 1.1.2.2 pgoyette #define RKI2C_CON_START __BIT(3)
50 1.1.2.2 pgoyette #define RKI2C_CON_I2C_MODE __BITS(2,1)
51 1.1.2.2 pgoyette #define RKI2C_CON_I2C_MODE_TX 0
52 1.1.2.2 pgoyette #define RKI2C_CON_I2C_MODE_RTX 1
53 1.1.2.2 pgoyette #define RKI2C_CON_I2C_MODE_RX 2
54 1.1.2.2 pgoyette #define RKI2C_CON_I2C_MODE_RRX 3
55 1.1.2.2 pgoyette #define RKI2C_CON_I2C_EN __BIT(0)
56 1.1.2.2 pgoyette
57 1.1.2.2 pgoyette #define RKI2C_CLKDIV 0x004
58 1.1.2.2 pgoyette #define RKI2C_CLKDIV_CLKDIVH __BITS(31,16)
59 1.1.2.2 pgoyette #define RKI2C_CLKDIV_CLKDIVL __BITS(15,0)
60 1.1.2.2 pgoyette
61 1.1.2.2 pgoyette #define RKI2C_MRXADDR 0x008
62 1.1.2.2 pgoyette #define RKI2C_MRXADDR_ADDHVLD __BIT(26)
63 1.1.2.2 pgoyette #define RKI2C_MRXADDR_ADDMVLD __BIT(25)
64 1.1.2.2 pgoyette #define RKI2C_MRXADDR_ADDLVLD __BIT(24)
65 1.1.2.2 pgoyette #define RKI2C_MRXADDR_SADDR __BITS(23,0)
66 1.1.2.2 pgoyette
67 1.1.2.2 pgoyette #define RKI2C_MRXRADDR 0x00c
68 1.1.2.2 pgoyette #define RKI2C_MRXRADDR_ADDHVLD __BIT(26)
69 1.1.2.2 pgoyette #define RKI2C_MRXRADDR_ADDMVLD __BIT(25)
70 1.1.2.2 pgoyette #define RKI2C_MRXRADDR_ADDLVLD __BIT(24)
71 1.1.2.2 pgoyette #define RKI2C_MRXRADDR_SADDR __BITS(23,0)
72 1.1.2.2 pgoyette
73 1.1.2.2 pgoyette #define RKI2C_MTXCNT 0x010
74 1.1.2.2 pgoyette #define RKI2C_MTXCNT_MTXCNT __BITS(5,0)
75 1.1.2.2 pgoyette
76 1.1.2.2 pgoyette #define RKI2C_MRXCNT 0x014
77 1.1.2.2 pgoyette #define RKI2C_MRXCNT_MRXCNT __BITS(5,0)
78 1.1.2.2 pgoyette
79 1.1.2.2 pgoyette #define RKI2C_IEN 0x018
80 1.1.2.2 pgoyette #define RKI2C_IEN_NAKRCVIEN __BIT(6)
81 1.1.2.2 pgoyette #define RKI2C_IEN_STOPIEN __BIT(5)
82 1.1.2.2 pgoyette #define RKI2C_IEN_STARTIEN __BIT(4)
83 1.1.2.2 pgoyette #define RKI2C_IEN_MBRFIEN __BIT(3)
84 1.1.2.2 pgoyette #define RKI2C_IEN_MBTFIEN __BIT(2)
85 1.1.2.2 pgoyette #define RKI2C_IEN_BRFIEN __BIT(1)
86 1.1.2.2 pgoyette #define RKI2C_IEN_BTFIEN __BIT(0)
87 1.1.2.2 pgoyette
88 1.1.2.2 pgoyette #define RKI2C_IPD 0x01c
89 1.1.2.2 pgoyette #define RKI2C_IPD_NAKRCVIPD __BIT(6)
90 1.1.2.2 pgoyette #define RKI2C_IPD_STOPIPD __BIT(5)
91 1.1.2.2 pgoyette #define RKI2C_IPD_STARTIPD __BIT(4)
92 1.1.2.2 pgoyette #define RKI2C_IPD_MBRFIPD __BIT(3)
93 1.1.2.2 pgoyette #define RKI2C_IPD_MBTFIPD __BIT(2)
94 1.1.2.2 pgoyette #define RKI2C_IPD_BRFIPD __BIT(1)
95 1.1.2.2 pgoyette #define RKI2C_IPD_BTFIPD __BIT(0)
96 1.1.2.2 pgoyette
97 1.1.2.2 pgoyette #define RKI2C_FCNT 0x020
98 1.1.2.2 pgoyette #define RKI2C_FCNT_FCNT __BITS(5,0)
99 1.1.2.2 pgoyette
100 1.1.2.2 pgoyette #define RKI2C_TXDATA(n) (0x100 + (n) * 4)
101 1.1.2.2 pgoyette #define RKI2C_RXDATA(n) (0x200 + (n) * 4)
102 1.1.2.2 pgoyette
103 1.1.2.2 pgoyette static const char * const compatible[] = {
104 1.1.2.2 pgoyette "rockchip,rk3399-i2c",
105 1.1.2.2 pgoyette NULL
106 1.1.2.2 pgoyette };
107 1.1.2.2 pgoyette
108 1.1.2.2 pgoyette struct rk_i2c_softc {
109 1.1.2.2 pgoyette device_t sc_dev;
110 1.1.2.2 pgoyette bus_space_tag_t sc_bst;
111 1.1.2.2 pgoyette bus_space_handle_t sc_bsh;
112 1.1.2.2 pgoyette struct clk *sc_sclk;
113 1.1.2.2 pgoyette struct clk *sc_pclk;
114 1.1.2.2 pgoyette
115 1.1.2.2 pgoyette u_int sc_clkfreq;
116 1.1.2.2 pgoyette
117 1.1.2.2 pgoyette struct i2c_controller sc_ic;
118 1.1.2.2 pgoyette kmutex_t sc_lock;
119 1.1.2.2 pgoyette kcondvar_t sc_cv;
120 1.1.2.2 pgoyette };
121 1.1.2.2 pgoyette
122 1.1.2.2 pgoyette #define RD4(sc, reg) \
123 1.1.2.2 pgoyette bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
124 1.1.2.2 pgoyette #define WR4(sc, reg, val) \
125 1.1.2.2 pgoyette bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
126 1.1.2.2 pgoyette
127 1.1.2.2 pgoyette static void
128 1.1.2.2 pgoyette rk_i2c_init(struct rk_i2c_softc *sc)
129 1.1.2.2 pgoyette {
130 1.1.2.2 pgoyette int div, divl, divh;
131 1.1.2.2 pgoyette u_int rate;
132 1.1.2.2 pgoyette
133 1.1.2.2 pgoyette /*
134 1.1.2.2 pgoyette * SCL frequency is calculated by the following formula:
135 1.1.2.2 pgoyette *
136 1.1.2.2 pgoyette * SCL Divisor = 8 * (CLKDIVL + 1 + CLKDIVH + 1)
137 1.1.2.2 pgoyette * SCL = PCLK / SCLK Divisor
138 1.1.2.2 pgoyette */
139 1.1.2.2 pgoyette
140 1.1.2.2 pgoyette rate = clk_get_rate(sc->sc_pclk);
141 1.1.2.2 pgoyette div = howmany(rate, sc->sc_clkfreq * 8) - 2;
142 1.1.2.2 pgoyette if (div >= 0) {
143 1.1.2.2 pgoyette divl = div / 2;
144 1.1.2.2 pgoyette if (div % 2 == 0)
145 1.1.2.2 pgoyette divh = divl;
146 1.1.2.2 pgoyette else
147 1.1.2.2 pgoyette divh = howmany(div, 2);
148 1.1.2.2 pgoyette } else {
149 1.1.2.2 pgoyette divl = divh = 0;
150 1.1.2.2 pgoyette }
151 1.1.2.2 pgoyette
152 1.1.2.2 pgoyette WR4(sc, RKI2C_CLKDIV,
153 1.1.2.2 pgoyette __SHIFTIN(divh, RKI2C_CLKDIV_CLKDIVH) |
154 1.1.2.2 pgoyette __SHIFTIN(divl, RKI2C_CLKDIV_CLKDIVL));
155 1.1.2.2 pgoyette
156 1.1.2.2 pgoyette /*
157 1.1.2.2 pgoyette * Disable the module until we are ready to use it.
158 1.1.2.2 pgoyette */
159 1.1.2.2 pgoyette WR4(sc, RKI2C_CON, 0);
160 1.1.2.2 pgoyette WR4(sc, RKI2C_IEN, 0);
161 1.1.2.2 pgoyette WR4(sc, RKI2C_IPD, RD4(sc, RKI2C_IPD));
162 1.1.2.2 pgoyette }
163 1.1.2.2 pgoyette
164 1.1.2.2 pgoyette static int
165 1.1.2.2 pgoyette rk_i2c_acquire_bus(void *priv, int flags)
166 1.1.2.2 pgoyette {
167 1.1.2.2 pgoyette struct rk_i2c_softc * const sc = priv;
168 1.1.2.2 pgoyette
169 1.1.2.2 pgoyette mutex_enter(&sc->sc_lock);
170 1.1.2.2 pgoyette
171 1.1.2.2 pgoyette return 0;
172 1.1.2.2 pgoyette }
173 1.1.2.2 pgoyette
174 1.1.2.2 pgoyette static void
175 1.1.2.2 pgoyette rk_i2c_release_bus(void *priv, int flags)
176 1.1.2.2 pgoyette {
177 1.1.2.2 pgoyette struct rk_i2c_softc * const sc = priv;
178 1.1.2.2 pgoyette
179 1.1.2.2 pgoyette mutex_exit(&sc->sc_lock);
180 1.1.2.2 pgoyette }
181 1.1.2.2 pgoyette
182 1.1.2.2 pgoyette static int
183 1.1.2.2 pgoyette rk_i2c_wait(struct rk_i2c_softc *sc, uint32_t mask)
184 1.1.2.2 pgoyette {
185 1.1.2.2 pgoyette u_int timeo = 100000;
186 1.1.2.2 pgoyette uint32_t val;
187 1.1.2.2 pgoyette
188 1.1.2.2 pgoyette const uint32_t ipdmask = mask | RKI2C_IPD_NAKRCVIPD;
189 1.1.2.2 pgoyette do {
190 1.1.2.2 pgoyette val = RD4(sc, RKI2C_IPD);
191 1.1.2.2 pgoyette if (val & ipdmask)
192 1.1.2.2 pgoyette break;
193 1.1.2.2 pgoyette delay(1);
194 1.1.2.2 pgoyette } while (--timeo > 0);
195 1.1.2.2 pgoyette
196 1.1.2.2 pgoyette WR4(sc, RKI2C_IPD, val & ipdmask);
197 1.1.2.2 pgoyette
198 1.1.2.2 pgoyette if ((val & RKI2C_IPD_NAKRCVIPD) != 0)
199 1.1.2.2 pgoyette return EIO;
200 1.1.2.2 pgoyette if ((val & mask) != 0)
201 1.1.2.2 pgoyette return 0;
202 1.1.2.2 pgoyette
203 1.1.2.2 pgoyette return ETIMEDOUT;
204 1.1.2.2 pgoyette }
205 1.1.2.2 pgoyette
206 1.1.2.2 pgoyette static int
207 1.1.2.2 pgoyette rk_i2c_start(struct rk_i2c_softc *sc)
208 1.1.2.2 pgoyette {
209 1.1.2.2 pgoyette uint32_t con;
210 1.1.2.2 pgoyette int error;
211 1.1.2.2 pgoyette
212 1.1.2.2 pgoyette /* Send start */
213 1.1.2.2 pgoyette con = RD4(sc, RKI2C_CON);
214 1.1.2.2 pgoyette con |= RKI2C_CON_START;
215 1.1.2.2 pgoyette WR4(sc, RKI2C_CON, con);
216 1.1.2.2 pgoyette
217 1.1.2.2 pgoyette if ((error = rk_i2c_wait(sc, RKI2C_IPD_STARTIPD)) != 0)
218 1.1.2.2 pgoyette return error;
219 1.1.2.2 pgoyette
220 1.1.2.2 pgoyette con &= ~RKI2C_CON_START;
221 1.1.2.2 pgoyette WR4(sc, RKI2C_CON, con);
222 1.1.2.2 pgoyette
223 1.1.2.2 pgoyette return 0;
224 1.1.2.2 pgoyette }
225 1.1.2.2 pgoyette
226 1.1.2.2 pgoyette static int
227 1.1.2.2 pgoyette rk_i2c_stop(struct rk_i2c_softc *sc)
228 1.1.2.2 pgoyette {
229 1.1.2.2 pgoyette uint32_t con;
230 1.1.2.2 pgoyette int error;
231 1.1.2.2 pgoyette
232 1.1.2.2 pgoyette /* Send start */
233 1.1.2.2 pgoyette con = RD4(sc, RKI2C_CON);
234 1.1.2.2 pgoyette con |= RKI2C_CON_STOP;
235 1.1.2.2 pgoyette WR4(sc, RKI2C_CON, con);
236 1.1.2.2 pgoyette
237 1.1.2.2 pgoyette if ((error = rk_i2c_wait(sc, RKI2C_IPD_STOPIPD)) != 0)
238 1.1.2.2 pgoyette return error;
239 1.1.2.2 pgoyette
240 1.1.2.2 pgoyette con &= ~RKI2C_CON_STOP;
241 1.1.2.2 pgoyette WR4(sc, RKI2C_CON, con);
242 1.1.2.2 pgoyette
243 1.1.2.2 pgoyette return 0;
244 1.1.2.2 pgoyette }
245 1.1.2.2 pgoyette
246 1.1.2.2 pgoyette static int
247 1.1.2.2 pgoyette rk_i2c_write(struct rk_i2c_softc *sc, i2c_addr_t addr, const uint8_t *buf,
248 1.1.2.2 pgoyette size_t buflen, int flags, bool send_start)
249 1.1.2.2 pgoyette {
250 1.1.2.2 pgoyette union {
251 1.1.2.2 pgoyette uint8_t data8[32];
252 1.1.2.2 pgoyette uint32_t data32[8];
253 1.1.2.2 pgoyette } txdata;
254 1.1.2.2 pgoyette uint32_t con;
255 1.1.2.2 pgoyette u_int mode;
256 1.1.2.2 pgoyette int error;
257 1.1.2.2 pgoyette
258 1.1.2.2 pgoyette if (buflen > 31)
259 1.1.2.2 pgoyette return EINVAL;
260 1.1.2.2 pgoyette
261 1.1.2.2 pgoyette mode = RKI2C_CON_I2C_MODE_TX;
262 1.1.2.2 pgoyette con = RKI2C_CON_I2C_EN | __SHIFTIN(mode, RKI2C_CON_I2C_MODE);
263 1.1.2.2 pgoyette WR4(sc, RKI2C_CON, con);
264 1.1.2.2 pgoyette
265 1.1.2.2 pgoyette if (send_start && (error = rk_i2c_start(sc)) != 0)
266 1.1.2.2 pgoyette return error;
267 1.1.2.2 pgoyette
268 1.1.2.2 pgoyette /* Transmit data. Slave address goes in the lower 8 bits of TXDATA0 */
269 1.1.2.2 pgoyette txdata.data8[0] = addr << 1;
270 1.1.2.2 pgoyette memcpy(&txdata.data8[1], buf, buflen);
271 1.1.2.2 pgoyette bus_space_write_region_4(sc->sc_bst, sc->sc_bsh, RKI2C_TXDATA(0),
272 1.1.2.2 pgoyette txdata.data32, howmany(buflen + 1, 4));
273 1.1.2.2 pgoyette WR4(sc, RKI2C_MTXCNT, __SHIFTIN(buflen + 1, RKI2C_MTXCNT_MTXCNT));
274 1.1.2.2 pgoyette
275 1.1.2.2 pgoyette if ((error = rk_i2c_wait(sc, RKI2C_IPD_MBTFIPD)) != 0)
276 1.1.2.2 pgoyette return error;
277 1.1.2.2 pgoyette
278 1.1.2.2 pgoyette return 0;
279 1.1.2.2 pgoyette }
280 1.1.2.2 pgoyette
281 1.1.2.2 pgoyette static int
282 1.1.2.2 pgoyette rk_i2c_read(struct rk_i2c_softc *sc, i2c_addr_t addr,
283 1.1.2.2 pgoyette const uint8_t *cmd, size_t cmdlen, uint8_t *buf,
284 1.1.2.2 pgoyette size_t buflen, int flags, bool send_start)
285 1.1.2.2 pgoyette {
286 1.1.2.2 pgoyette uint32_t rxdata[8];
287 1.1.2.2 pgoyette uint32_t con, mrxaddr, mrxraddr;
288 1.1.2.2 pgoyette u_int mode;
289 1.1.2.2 pgoyette int error, n;
290 1.1.2.2 pgoyette
291 1.1.2.2 pgoyette if (buflen > 32)
292 1.1.2.2 pgoyette return EINVAL;
293 1.1.2.2 pgoyette if (cmdlen > 3)
294 1.1.2.2 pgoyette return EINVAL;
295 1.1.2.2 pgoyette
296 1.1.2.2 pgoyette mode = RKI2C_CON_I2C_MODE_RTX;
297 1.1.2.2 pgoyette con = RKI2C_CON_I2C_EN | __SHIFTIN(mode, RKI2C_CON_I2C_MODE);
298 1.1.2.2 pgoyette WR4(sc, RKI2C_CON, con);
299 1.1.2.2 pgoyette
300 1.1.2.2 pgoyette if (send_start && (error = rk_i2c_start(sc)) != 0)
301 1.1.2.2 pgoyette return error;
302 1.1.2.2 pgoyette
303 1.1.2.2 pgoyette mrxaddr = __SHIFTIN((addr << 1) | 1, RKI2C_MRXADDR_SADDR) |
304 1.1.2.2 pgoyette RKI2C_MRXADDR_ADDLVLD;
305 1.1.2.2 pgoyette WR4(sc, RKI2C_MRXADDR, mrxaddr);
306 1.1.2.2 pgoyette for (n = 0, mrxraddr = 0; n < cmdlen; n++) {
307 1.1.2.2 pgoyette mrxraddr |= cmd[n] << (n * 8);
308 1.1.2.2 pgoyette mrxraddr |= (RKI2C_MRXRADDR_ADDLVLD << n);
309 1.1.2.2 pgoyette }
310 1.1.2.2 pgoyette WR4(sc, RKI2C_MRXRADDR, mrxraddr);
311 1.1.2.2 pgoyette
312 1.1.2.2 pgoyette /* Acknowledge last byte read */
313 1.1.2.2 pgoyette con |= RKI2C_CON_ACK;
314 1.1.2.2 pgoyette WR4(sc, RKI2C_CON, con);
315 1.1.2.2 pgoyette
316 1.1.2.2 pgoyette /* Receive data. Slave address goes in the lower 8 bits of MRXADDR */
317 1.1.2.2 pgoyette WR4(sc, RKI2C_MRXCNT, __SHIFTIN(buflen, RKI2C_MRXCNT_MRXCNT));
318 1.1.2.2 pgoyette if ((error = rk_i2c_wait(sc, RKI2C_IPD_MBRFIPD)) != 0)
319 1.1.2.2 pgoyette return error;
320 1.1.2.2 pgoyette
321 1.1.2.2 pgoyette bus_space_read_region_4(sc->sc_bst, sc->sc_bsh, RKI2C_RXDATA(0),
322 1.1.2.2 pgoyette rxdata, howmany(buflen, 4));
323 1.1.2.2 pgoyette memcpy(buf, rxdata, buflen);
324 1.1.2.2 pgoyette
325 1.1.2.2 pgoyette return 0;
326 1.1.2.2 pgoyette }
327 1.1.2.2 pgoyette
328 1.1.2.2 pgoyette static int
329 1.1.2.2 pgoyette rk_i2c_exec(void *priv, i2c_op_t op, i2c_addr_t addr,
330 1.1.2.2 pgoyette const void *cmdbuf, size_t cmdlen, void *buf, size_t buflen, int flags)
331 1.1.2.2 pgoyette {
332 1.1.2.2 pgoyette struct rk_i2c_softc * const sc = priv;
333 1.1.2.2 pgoyette bool send_start = true;
334 1.1.2.2 pgoyette int error;
335 1.1.2.2 pgoyette
336 1.1.2.2 pgoyette KASSERT(mutex_owned(&sc->sc_lock));
337 1.1.2.2 pgoyette
338 1.1.2.2 pgoyette if (I2C_OP_READ_P(op)) {
339 1.1.2.2 pgoyette error = rk_i2c_read(sc, addr, cmdbuf, cmdlen, buf, buflen, flags, send_start);
340 1.1.2.2 pgoyette } else {
341 1.1.2.2 pgoyette if (cmdlen > 0) {
342 1.1.2.2 pgoyette error = rk_i2c_write(sc, addr, cmdbuf, cmdlen, flags, send_start);
343 1.1.2.2 pgoyette if (error != 0)
344 1.1.2.2 pgoyette goto done;
345 1.1.2.2 pgoyette send_start = false;
346 1.1.2.2 pgoyette }
347 1.1.2.2 pgoyette error = rk_i2c_write(sc, addr, buf, buflen, flags, send_start);
348 1.1.2.2 pgoyette }
349 1.1.2.2 pgoyette
350 1.1.2.2 pgoyette done:
351 1.1.2.2 pgoyette if (error != 0 || I2C_OP_STOP_P(op))
352 1.1.2.2 pgoyette rk_i2c_stop(sc);
353 1.1.2.2 pgoyette
354 1.1.2.2 pgoyette WR4(sc, RKI2C_CON, 0);
355 1.1.2.2 pgoyette WR4(sc, RKI2C_IPD, RD4(sc, RKI2C_IPD));
356 1.1.2.2 pgoyette
357 1.1.2.2 pgoyette return error;
358 1.1.2.2 pgoyette }
359 1.1.2.2 pgoyette
360 1.1.2.2 pgoyette static i2c_tag_t
361 1.1.2.2 pgoyette rk_i2c_get_tag(device_t dev)
362 1.1.2.2 pgoyette {
363 1.1.2.2 pgoyette struct rk_i2c_softc * const sc = device_private(dev);
364 1.1.2.2 pgoyette
365 1.1.2.2 pgoyette return &sc->sc_ic;
366 1.1.2.2 pgoyette }
367 1.1.2.2 pgoyette
368 1.1.2.2 pgoyette static const struct fdtbus_i2c_controller_func rk_i2c_funcs = {
369 1.1.2.2 pgoyette .get_tag = rk_i2c_get_tag,
370 1.1.2.2 pgoyette };
371 1.1.2.2 pgoyette
372 1.1.2.2 pgoyette static int
373 1.1.2.2 pgoyette rk_i2c_match(device_t parent, cfdata_t cf, void *aux)
374 1.1.2.2 pgoyette {
375 1.1.2.2 pgoyette struct fdt_attach_args * const faa = aux;
376 1.1.2.2 pgoyette
377 1.1.2.2 pgoyette return of_match_compatible(faa->faa_phandle, compatible);
378 1.1.2.2 pgoyette }
379 1.1.2.2 pgoyette
380 1.1.2.2 pgoyette static void
381 1.1.2.2 pgoyette rk_i2c_attach(device_t parent, device_t self, void *aux)
382 1.1.2.2 pgoyette {
383 1.1.2.2 pgoyette struct rk_i2c_softc * const sc = device_private(self);
384 1.1.2.2 pgoyette struct fdt_attach_args * const faa = aux;
385 1.1.2.2 pgoyette const int phandle = faa->faa_phandle;
386 1.1.2.2 pgoyette bus_addr_t addr;
387 1.1.2.2 pgoyette bus_size_t size;
388 1.1.2.2 pgoyette
389 1.1.2.2 pgoyette if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
390 1.1.2.2 pgoyette aprint_error(": couldn't get registers\n");
391 1.1.2.2 pgoyette return;
392 1.1.2.2 pgoyette }
393 1.1.2.2 pgoyette
394 1.1.2.2 pgoyette sc->sc_sclk = fdtbus_clock_get(phandle, "i2c");
395 1.1.2.2 pgoyette if (sc->sc_sclk == NULL || clk_enable(sc->sc_sclk) != 0) {
396 1.1.2.2 pgoyette aprint_error(": couldn't enable sclk\n");
397 1.1.2.2 pgoyette return;
398 1.1.2.2 pgoyette }
399 1.1.2.2 pgoyette
400 1.1.2.2 pgoyette sc->sc_pclk = fdtbus_clock_get(phandle, "pclk");
401 1.1.2.2 pgoyette if (sc->sc_pclk == NULL || clk_enable(sc->sc_pclk) != 0) {
402 1.1.2.2 pgoyette aprint_error(": couldn't enable pclk\n");
403 1.1.2.2 pgoyette return;
404 1.1.2.2 pgoyette }
405 1.1.2.2 pgoyette
406 1.1.2.2 pgoyette if (of_getprop_uint32(phandle, "clock-frequency", &sc->sc_clkfreq))
407 1.1.2.2 pgoyette sc->sc_clkfreq = 100000;
408 1.1.2.2 pgoyette
409 1.1.2.2 pgoyette sc->sc_dev = self;
410 1.1.2.2 pgoyette sc->sc_bst = faa->faa_bst;
411 1.1.2.2 pgoyette if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
412 1.1.2.2 pgoyette aprint_error(": couldn't map registers\n");
413 1.1.2.2 pgoyette return;
414 1.1.2.2 pgoyette }
415 1.1.2.2 pgoyette
416 1.1.2.2 pgoyette mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
417 1.1.2.2 pgoyette cv_init(&sc->sc_cv, "rkiic");
418 1.1.2.2 pgoyette
419 1.1.2.2 pgoyette aprint_naive("\n");
420 1.1.2.2 pgoyette aprint_normal(": Rockchip I2C (%u Hz)\n", sc->sc_clkfreq);
421 1.1.2.2 pgoyette
422 1.1.2.2 pgoyette rk_i2c_init(sc);
423 1.1.2.2 pgoyette
424 1.1.2.2 pgoyette sc->sc_ic.ic_cookie = sc;
425 1.1.2.2 pgoyette sc->sc_ic.ic_acquire_bus = rk_i2c_acquire_bus;
426 1.1.2.2 pgoyette sc->sc_ic.ic_release_bus = rk_i2c_release_bus;
427 1.1.2.2 pgoyette sc->sc_ic.ic_exec = rk_i2c_exec;
428 1.1.2.2 pgoyette
429 1.1.2.2 pgoyette fdtbus_register_i2c_controller(self, phandle, &rk_i2c_funcs);
430 1.1.2.2 pgoyette
431 1.1.2.2 pgoyette fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print);
432 1.1.2.2 pgoyette }
433 1.1.2.2 pgoyette
434 1.1.2.2 pgoyette CFATTACH_DECL_NEW(rk_i2c, sizeof(struct rk_i2c_softc),
435 1.1.2.2 pgoyette rk_i2c_match, rk_i2c_attach, NULL, NULL);
436