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