pxa2x0_i2c.c revision 1.8 1 1.8 kiyohara /* $NetBSD: pxa2x0_i2c.c,v 1.8 2011/08/06 03:42:11 kiyohara Exp $ */
2 1.1 peter /* $OpenBSD: pxa2x0_i2c.c,v 1.2 2005/05/26 03:52:07 pascoe Exp $ */
3 1.1 peter
4 1.1 peter /*
5 1.1 peter * Copyright (c) 2005 Christopher Pascoe <pascoe (at) openbsd.org>
6 1.1 peter *
7 1.1 peter * Permission to use, copy, modify, and distribute this software for any
8 1.1 peter * purpose with or without fee is hereby granted, provided that the above
9 1.1 peter * copyright notice and this permission notice appear in all copies.
10 1.1 peter *
11 1.1 peter * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 peter * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 peter * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 peter * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 peter * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 peter * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 peter * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 peter */
19 1.1 peter
20 1.1 peter #include <sys/cdefs.h>
21 1.8 kiyohara __KERNEL_RCSID(0, "$NetBSD: pxa2x0_i2c.c,v 1.8 2011/08/06 03:42:11 kiyohara Exp $");
22 1.1 peter
23 1.1 peter #include <sys/param.h>
24 1.8 kiyohara #include <sys/bus.h>
25 1.8 kiyohara #include <sys/device.h>
26 1.8 kiyohara #include <sys/errno.h>
27 1.1 peter #include <sys/systm.h>
28 1.1 peter
29 1.5 nonaka #include <dev/i2c/i2cvar.h>
30 1.1 peter
31 1.1 peter #include <arm/xscale/pxa2x0reg.h>
32 1.1 peter #include <arm/xscale/pxa2x0var.h>
33 1.1 peter #include <arm/xscale/pxa2x0_i2c.h>
34 1.5 nonaka
35 1.5 nonaka #ifdef PXAIIC_DEBUG
36 1.5 nonaka #define DPRINTF(s) printf s
37 1.5 nonaka #else
38 1.5 nonaka #define DPRINTF(s) do { } while (/*CONSTCOND*/0)
39 1.5 nonaka #endif
40 1.1 peter
41 1.1 peter #define I2C_RETRY_COUNT 10
42 1.1 peter
43 1.1 peter int
44 1.1 peter pxa2x0_i2c_attach_sub(struct pxa2x0_i2c_softc *sc)
45 1.1 peter {
46 1.5 nonaka int error;
47 1.1 peter
48 1.6 kiyohara error = bus_space_map(sc->sc_iot, sc->sc_addr, sc->sc_size, 0,
49 1.5 nonaka &sc->sc_ioh);
50 1.5 nonaka if (error) {
51 1.5 nonaka aprint_error_dev(sc->sc_dev, "unable to map register\n");
52 1.1 peter sc->sc_size = 0;
53 1.5 nonaka return error;
54 1.1 peter }
55 1.5 nonaka
56 1.1 peter bus_space_barrier(sc->sc_iot, sc->sc_ioh, 0, sc->sc_size,
57 1.1 peter BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
58 1.1 peter
59 1.5 nonaka sc->sc_icr = ICR_GCD | ICR_SCLE | ICR_IUE;
60 1.5 nonaka #if 0
61 1.5 nonaka if (ISSET(sc->sc_flags, PI2CF_ENABLE_INTR))
62 1.5 nonaka sc->sc_icr |= ICR_BEIE | ICR_DRFIE | ICR_ITEIE;
63 1.5 nonaka #endif
64 1.5 nonaka if (ISSET(sc->sc_flags, PI2CF_FAST_MODE))
65 1.5 nonaka sc->sc_icr |= ICR_FM;
66 1.5 nonaka
67 1.1 peter pxa2x0_i2c_init(sc);
68 1.1 peter
69 1.1 peter return 0;
70 1.1 peter }
71 1.1 peter
72 1.1 peter int
73 1.1 peter pxa2x0_i2c_detach_sub(struct pxa2x0_i2c_softc *sc)
74 1.1 peter {
75 1.1 peter
76 1.5 nonaka if (sc->sc_size != 0) {
77 1.1 peter bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
78 1.1 peter sc->sc_size = 0;
79 1.1 peter }
80 1.1 peter pxa2x0_clkman_config(CKEN_I2C, 0);
81 1.1 peter return 0;
82 1.1 peter }
83 1.1 peter
84 1.1 peter void
85 1.1 peter pxa2x0_i2c_init(struct pxa2x0_i2c_softc *sc)
86 1.1 peter {
87 1.1 peter
88 1.1 peter pxa2x0_i2c_open(sc);
89 1.1 peter pxa2x0_i2c_close(sc);
90 1.1 peter }
91 1.1 peter
92 1.1 peter void
93 1.1 peter pxa2x0_i2c_open(struct pxa2x0_i2c_softc *sc)
94 1.1 peter {
95 1.1 peter
96 1.1 peter /* Enable the clock to the standard I2C unit. */
97 1.1 peter pxa2x0_clkman_config(CKEN_I2C, 1);
98 1.1 peter }
99 1.1 peter
100 1.1 peter void
101 1.1 peter pxa2x0_i2c_close(struct pxa2x0_i2c_softc *sc)
102 1.1 peter {
103 1.1 peter
104 1.1 peter /* Reset and disable the standard I2C unit. */
105 1.1 peter bus_space_write_4(sc->sc_iot, sc->sc_ioh, I2C_ICR, ICR_UR);
106 1.1 peter bus_space_write_4(sc->sc_iot, sc->sc_ioh, I2C_ISAR, 0);
107 1.1 peter delay(1);
108 1.1 peter pxa2x0_clkman_config(CKEN_I2C, 0);
109 1.1 peter }
110 1.1 peter
111 1.1 peter int
112 1.1 peter pxa2x0_i2c_read(struct pxa2x0_i2c_softc *sc, u_char slave, u_char *valuep)
113 1.1 peter {
114 1.1 peter bus_space_tag_t iot = sc->sc_iot;
115 1.1 peter bus_space_handle_t ioh = sc->sc_ioh;
116 1.1 peter int timeout;
117 1.1 peter int tries = I2C_RETRY_COUNT;
118 1.1 peter uint32_t rv;
119 1.1 peter
120 1.1 peter retry:
121 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, ICR_UR);
122 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISAR, sc->sc_isar);
123 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE | ISR_IRF);
124 1.1 peter delay(1);
125 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_SCLE);
126 1.1 peter
127 1.1 peter /* Write slave device address. */
128 1.1 peter bus_space_write_4(iot, ioh, I2C_IDBR, (slave<<1) | 0x1);
129 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
130 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_START);
131 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
132 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv & ~ICR_STOP);
133 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
134 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_TB);
135 1.1 peter
136 1.1 peter timeout = 10000;
137 1.1 peter while ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ITE) == 0) {
138 1.1 peter if (timeout-- == 0)
139 1.1 peter goto err;
140 1.1 peter delay(1);
141 1.1 peter }
142 1.1 peter
143 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
144 1.1 peter
145 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
146 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv & ~ICR_START);
147 1.1 peter
148 1.1 peter /* Read data value. */
149 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
150 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv |
151 1.1 peter (ICR_STOP | ICR_ACKNAK));
152 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
153 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_TB);
154 1.1 peter
155 1.1 peter timeout = 10000;
156 1.1 peter while ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_IRF) == 0) {
157 1.1 peter if (timeout-- == 0)
158 1.1 peter goto err;
159 1.1 peter delay(1);
160 1.1 peter }
161 1.1 peter
162 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_IRF);
163 1.1 peter
164 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_IDBR);
165 1.1 peter *valuep = (u_char)rv;
166 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_BEIE | ICR_SADIE);
167 1.1 peter
168 1.1 peter return 0;
169 1.1 peter
170 1.1 peter err:
171 1.1 peter if (tries-- >= 0)
172 1.1 peter goto retry;
173 1.1 peter
174 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, ICR_UR);
175 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISAR, sc->sc_isar);
176 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE | ISR_IRF);
177 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_BEIE | ICR_SADIE);
178 1.1 peter
179 1.1 peter return EIO;
180 1.1 peter }
181 1.1 peter
182 1.1 peter int
183 1.1 peter pxa2x0_i2c_write(struct pxa2x0_i2c_softc *sc, u_char slave, u_char value)
184 1.1 peter {
185 1.1 peter bus_space_tag_t iot = sc->sc_iot;
186 1.1 peter bus_space_handle_t ioh = sc->sc_ioh;
187 1.1 peter int timeout;
188 1.1 peter int tries = I2C_RETRY_COUNT;
189 1.1 peter uint32_t rv;
190 1.1 peter
191 1.1 peter retry:
192 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, ICR_UR);
193 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISAR, sc->sc_isar);
194 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
195 1.1 peter delay(1);
196 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_SCLE);
197 1.1 peter
198 1.1 peter /* Write slave device address. */
199 1.1 peter bus_space_write_4(iot, ioh, I2C_IDBR, (slave<<1));
200 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
201 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_START);
202 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
203 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv & ~ICR_STOP);
204 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
205 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_TB);
206 1.1 peter
207 1.1 peter timeout = 10000;
208 1.1 peter while ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ITE) == 0) {
209 1.1 peter if (timeout-- == 0)
210 1.1 peter goto err;
211 1.1 peter delay(1);
212 1.1 peter }
213 1.1 peter if ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ACKNAK) != 0)
214 1.1 peter goto err;
215 1.1 peter
216 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
217 1.1 peter
218 1.1 peter /* Write data. */
219 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
220 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv & ~ICR_START);
221 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
222 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_STOP);
223 1.1 peter bus_space_write_4(iot, ioh, I2C_IDBR, value);
224 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
225 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_TB);
226 1.1 peter
227 1.1 peter timeout = 10000;
228 1.1 peter while ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ITE) == 0) {
229 1.1 peter if (timeout-- == 0)
230 1.1 peter goto err;
231 1.1 peter delay(1);
232 1.1 peter }
233 1.1 peter if ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ACKNAK) != 0)
234 1.1 peter goto err;
235 1.1 peter
236 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
237 1.1 peter
238 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_BEIE | ICR_SADIE);
239 1.1 peter
240 1.1 peter return 0;
241 1.1 peter
242 1.1 peter err:
243 1.1 peter if (tries-- >= 0)
244 1.1 peter goto retry;
245 1.1 peter
246 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, ICR_UR);
247 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISAR, sc->sc_isar);
248 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
249 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_BEIE | ICR_SADIE);
250 1.1 peter
251 1.1 peter return EIO;
252 1.1 peter }
253 1.1 peter
254 1.4 pgoyette /*
255 1.4 pgoyette * XXX The quick_{read,write} opertions are untested!
256 1.4 pgoyette */
257 1.4 pgoyette int
258 1.4 pgoyette pxa2x0_i2c_quick(struct pxa2x0_i2c_softc *sc, u_char slave, u_char rw)
259 1.4 pgoyette {
260 1.4 pgoyette bus_space_tag_t iot = sc->sc_iot;
261 1.4 pgoyette bus_space_handle_t ioh = sc->sc_ioh;
262 1.4 pgoyette int timeout;
263 1.4 pgoyette int tries = I2C_RETRY_COUNT;
264 1.4 pgoyette uint32_t rv;
265 1.4 pgoyette
266 1.4 pgoyette retry:
267 1.4 pgoyette bus_space_write_4(iot, ioh, I2C_ICR, ICR_UR);
268 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISAR, sc->sc_isar);
269 1.4 pgoyette bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
270 1.4 pgoyette delay(1);
271 1.4 pgoyette bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_SCLE);
272 1.4 pgoyette
273 1.4 pgoyette /* Write slave device address. */
274 1.4 pgoyette bus_space_write_4(iot, ioh, I2C_IDBR, (slave<<1) | (rw & 1));
275 1.4 pgoyette rv = bus_space_read_4(iot, ioh, I2C_ICR);
276 1.4 pgoyette bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_START);
277 1.4 pgoyette rv = bus_space_read_4(iot, ioh, I2C_ICR);
278 1.4 pgoyette bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_STOP);
279 1.4 pgoyette rv = bus_space_read_4(iot, ioh, I2C_ICR);
280 1.4 pgoyette
281 1.4 pgoyette timeout = 10000;
282 1.4 pgoyette while ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ITE) == 0) {
283 1.4 pgoyette if (timeout-- == 0)
284 1.4 pgoyette goto err;
285 1.4 pgoyette delay(1);
286 1.4 pgoyette }
287 1.4 pgoyette if ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ACKNAK) != 0)
288 1.4 pgoyette goto err;
289 1.4 pgoyette
290 1.4 pgoyette bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
291 1.4 pgoyette
292 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_BEIE | ICR_SADIE);
293 1.4 pgoyette
294 1.4 pgoyette return 0;
295 1.4 pgoyette
296 1.4 pgoyette err:
297 1.4 pgoyette if (tries-- >= 0)
298 1.4 pgoyette goto retry;
299 1.4 pgoyette
300 1.4 pgoyette bus_space_write_4(iot, ioh, I2C_ICR, ICR_UR);
301 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISAR, sc->sc_isar);
302 1.4 pgoyette bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
303 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_BEIE | ICR_SADIE);
304 1.4 pgoyette
305 1.4 pgoyette return EIO;
306 1.4 pgoyette }
307 1.4 pgoyette
308 1.1 peter int
309 1.1 peter pxa2x0_i2c_write_2(struct pxa2x0_i2c_softc *sc, u_char slave, u_short value)
310 1.1 peter {
311 1.1 peter bus_space_tag_t iot = sc->sc_iot;
312 1.1 peter bus_space_handle_t ioh = sc->sc_ioh;
313 1.1 peter int timeout;
314 1.1 peter int tries = I2C_RETRY_COUNT;
315 1.1 peter uint32_t rv;
316 1.1 peter
317 1.1 peter retry:
318 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, ICR_UR);
319 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISAR, sc->sc_isar);
320 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
321 1.1 peter delay(1);
322 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_SCLE);
323 1.1 peter
324 1.1 peter /* Write slave device address. */
325 1.1 peter bus_space_write_4(iot, ioh, I2C_IDBR, (slave<<1));
326 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
327 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_START);
328 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
329 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv & ~ICR_STOP);
330 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
331 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_TB);
332 1.1 peter
333 1.1 peter timeout = 10000;
334 1.1 peter while ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ITE) == 0) {
335 1.1 peter if (timeout-- == 0)
336 1.1 peter goto err;
337 1.1 peter delay(1);
338 1.1 peter }
339 1.1 peter if ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ACKNAK) != 0)
340 1.1 peter goto err;
341 1.1 peter
342 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
343 1.1 peter
344 1.1 peter /* Write upper 8 bits of data. */
345 1.1 peter bus_space_write_4(iot, ioh, I2C_IDBR, (value >> 8) & 0xff);
346 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
347 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv & ~ICR_START);
348 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
349 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv & ~ICR_STOP);
350 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
351 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_TB);
352 1.1 peter
353 1.1 peter timeout = 10000;
354 1.1 peter while ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ITE) == 0) {
355 1.1 peter if (timeout-- == 0)
356 1.1 peter goto err;
357 1.1 peter delay(1);
358 1.1 peter }
359 1.1 peter if ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ACKNAK) != 0)
360 1.1 peter goto err;
361 1.1 peter
362 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
363 1.1 peter
364 1.1 peter /* Write lower 8 bits of data. */
365 1.1 peter bus_space_write_4(iot, ioh, I2C_IDBR, value & 0xff);
366 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
367 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv & ~ICR_START);
368 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
369 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_STOP);
370 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
371 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_TB);
372 1.1 peter
373 1.1 peter timeout = 10000;
374 1.1 peter while ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ITE) == 0) {
375 1.1 peter if (timeout-- == 0)
376 1.1 peter goto err;
377 1.1 peter delay(1);
378 1.1 peter }
379 1.1 peter if ((bus_space_read_4(iot, ioh, I2C_ISR) & ISR_ACKNAK) != 0)
380 1.1 peter goto err;
381 1.1 peter
382 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
383 1.1 peter
384 1.1 peter rv = bus_space_read_4(iot, ioh, I2C_ICR);
385 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, rv & ~ICR_STOP);
386 1.1 peter
387 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_BEIE | ICR_SADIE);
388 1.8 kiyohara
389 1.1 peter return 0;
390 1.1 peter
391 1.1 peter err:
392 1.1 peter if (tries-- >= 0)
393 1.1 peter goto retry;
394 1.1 peter
395 1.1 peter bus_space_write_4(iot, ioh, I2C_ICR, ICR_UR);
396 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISAR, sc->sc_isar);
397 1.1 peter bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
398 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_BEIE | ICR_SADIE);
399 1.1 peter
400 1.1 peter return EIO;
401 1.1 peter }
402 1.5 nonaka
403 1.5 nonaka /* ----------------------------------------------------------------------------
404 1.5 nonaka * for i2c_controller
405 1.5 nonaka */
406 1.5 nonaka
407 1.5 nonaka #define CSR_READ_4(sc,r) bus_space_read_4(sc->sc_iot, sc->sc_ioh, r)
408 1.5 nonaka #define CSR_WRITE_4(sc,r,v) bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v)
409 1.5 nonaka
410 1.7 kiyohara #define ISR_ALL (ISR_RWM | ISR_ACKNAK | ISR_UB | ISR_IBB \
411 1.5 nonaka | ISR_SSD | ISR_ALD | ISR_ITE | ISR_IRF \
412 1.5 nonaka | ISR_GCAD | ISR_SAD | ISR_BED)
413 1.5 nonaka
414 1.5 nonaka #define I2C_TIMEOUT 100 /* protocol timeout, in uSecs */
415 1.5 nonaka
416 1.5 nonaka void
417 1.5 nonaka pxa2x0_i2c_reset(struct pxa2x0_i2c_softc *sc)
418 1.5 nonaka {
419 1.5 nonaka
420 1.5 nonaka CSR_WRITE_4(sc, I2C_ICR, ICR_UR);
421 1.5 nonaka CSR_WRITE_4(sc, I2C_ISAR, 0);
422 1.5 nonaka CSR_WRITE_4(sc, I2C_ISR, ISR_ALL);
423 1.5 nonaka while (CSR_READ_4(sc, I2C_ICR) & ~ICR_UR)
424 1.5 nonaka continue;
425 1.5 nonaka CSR_WRITE_4(sc, I2C_ICR, sc->sc_icr);
426 1.5 nonaka }
427 1.5 nonaka
428 1.5 nonaka int
429 1.5 nonaka pxa2x0_i2c_wait(struct pxa2x0_i2c_softc *sc, int bit, int flags)
430 1.5 nonaka {
431 1.5 nonaka uint32_t isr;
432 1.5 nonaka int error;
433 1.5 nonaka int i;
434 1.5 nonaka
435 1.5 nonaka for (i = I2C_TIMEOUT; i >= 0; --i) {
436 1.5 nonaka isr = CSR_READ_4(sc, I2C_ISR);
437 1.5 nonaka if (isr & (bit | ISR_BED))
438 1.5 nonaka break;
439 1.5 nonaka delay(1);
440 1.5 nonaka }
441 1.5 nonaka
442 1.5 nonaka if (isr & (ISR_BED | (bit & ISR_ALD)))
443 1.5 nonaka error = EIO;
444 1.5 nonaka else if (isr & (bit & ~ISR_ALD))
445 1.5 nonaka error = 0;
446 1.5 nonaka else
447 1.5 nonaka error = ETIMEDOUT;
448 1.5 nonaka
449 1.5 nonaka CSR_WRITE_4(sc, I2C_ISR, isr);
450 1.5 nonaka
451 1.5 nonaka return error;
452 1.5 nonaka }
453 1.5 nonaka
454 1.5 nonaka int
455 1.5 nonaka pxa2x0_i2c_send_start(struct pxa2x0_i2c_softc *sc, int flags)
456 1.5 nonaka {
457 1.5 nonaka
458 1.5 nonaka CSR_WRITE_4(sc, I2C_ICR, sc->sc_icr | ICR_START);
459 1.5 nonaka delay(I2C_TIMEOUT);
460 1.5 nonaka return 0;
461 1.5 nonaka }
462 1.5 nonaka
463 1.5 nonaka int
464 1.5 nonaka pxa2x0_i2c_send_stop(struct pxa2x0_i2c_softc *sc, int flags)
465 1.5 nonaka {
466 1.5 nonaka
467 1.5 nonaka CSR_WRITE_4(sc, I2C_ICR, sc->sc_icr | ICR_STOP);
468 1.5 nonaka delay(I2C_TIMEOUT);
469 1.5 nonaka return 0;
470 1.5 nonaka }
471 1.5 nonaka
472 1.5 nonaka int
473 1.5 nonaka pxa2x0_i2c_initiate_xfer(struct pxa2x0_i2c_softc *sc, uint16_t addr, int flags)
474 1.5 nonaka {
475 1.5 nonaka int rd_req = (flags & I2C_F_READ) ? 1 : 0;
476 1.5 nonaka int error;
477 1.5 nonaka
478 1.5 nonaka if ((addr & ~0x7f) != 0) {
479 1.5 nonaka error = EINVAL;
480 1.5 nonaka goto error;
481 1.5 nonaka }
482 1.5 nonaka
483 1.5 nonaka CSR_WRITE_4(sc, I2C_IDBR, (addr << 1) | rd_req);
484 1.5 nonaka CSR_WRITE_4(sc, I2C_ICR, sc->sc_icr | ICR_START | ICR_TB);
485 1.5 nonaka
486 1.5 nonaka error = pxa2x0_i2c_wait(sc, ISR_ITE, flags);
487 1.5 nonaka error:
488 1.5 nonaka if (error) {
489 1.5 nonaka DPRINTF(("%s: failed to initiate %s xfer (error=%d)\n",
490 1.5 nonaka device_xname(sc->sc_dev),
491 1.5 nonaka rd_req ? "read" : "write", error));
492 1.5 nonaka return error;
493 1.5 nonaka }
494 1.5 nonaka return 0;
495 1.5 nonaka }
496 1.5 nonaka
497 1.5 nonaka int
498 1.5 nonaka pxa2x0_i2c_read_byte(struct pxa2x0_i2c_softc *sc, uint8_t *bytep, int flags)
499 1.5 nonaka {
500 1.5 nonaka int last_byte = flags & I2C_F_LAST;
501 1.5 nonaka int send_stop = flags & I2C_F_STOP;
502 1.5 nonaka int error;
503 1.5 nonaka
504 1.5 nonaka CSR_WRITE_4(sc, I2C_ICR, sc->sc_icr | ICR_TB
505 1.5 nonaka | (last_byte ? ICR_ACKNAK : 0) | (send_stop ? ICR_STOP : 0));
506 1.5 nonaka error = pxa2x0_i2c_wait(sc, ISR_IRF | ISR_ALD, flags);
507 1.5 nonaka if (error) {
508 1.5 nonaka DPRINTF(("%s: read byte failed\n", device_xname(sc->sc_dev)));
509 1.5 nonaka return error;
510 1.5 nonaka }
511 1.5 nonaka
512 1.5 nonaka *bytep = CSR_READ_4(sc, I2C_IDBR);
513 1.5 nonaka return 0;
514 1.5 nonaka }
515 1.5 nonaka
516 1.5 nonaka int
517 1.5 nonaka pxa2x0_i2c_write_byte(struct pxa2x0_i2c_softc *sc, uint8_t byte, int flags)
518 1.5 nonaka {
519 1.5 nonaka int send_stop = flags & I2C_F_STOP;
520 1.5 nonaka int error;
521 1.5 nonaka
522 1.5 nonaka CSR_WRITE_4(sc, I2C_IDBR, byte);
523 1.5 nonaka CSR_WRITE_4(sc, I2C_ICR, sc->sc_icr | ICR_TB
524 1.5 nonaka | (send_stop ? ICR_STOP : 0));
525 1.5 nonaka error = pxa2x0_i2c_wait(sc, ISR_ITE | ISR_ALD, flags);
526 1.5 nonaka if (error) {
527 1.5 nonaka DPRINTF(("%s: write byte failed\n", device_xname(sc->sc_dev)));
528 1.5 nonaka return error;
529 1.5 nonaka }
530 1.5 nonaka return 0;
531 1.5 nonaka }
532 1.8 kiyohara
533 1.8 kiyohara
534 1.8 kiyohara int
535 1.8 kiyohara pxa2x0_i2c_poll(struct pxa2x0_i2c_softc *sc, int len, char *data, int op)
536 1.8 kiyohara {
537 1.8 kiyohara bus_space_tag_t iot = sc->sc_iot;
538 1.8 kiyohara bus_space_handle_t ioh = sc->sc_ioh;
539 1.8 kiyohara uint32_t rv;
540 1.8 kiyohara int timeout, tries, n = 0;
541 1.8 kiyohara
542 1.8 kiyohara KASSERT(len > 0);
543 1.8 kiyohara
544 1.8 kiyohara if (sc->sc_stat == PI2C_STAT_SEND) {
545 1.8 kiyohara if (op != I2C_F_WRITE)
546 1.8 kiyohara return 0;
547 1.8 kiyohara goto send;
548 1.8 kiyohara } else if (sc->sc_stat == PI2C_STAT_RECEIVE) {
549 1.8 kiyohara if (op != I2C_F_READ)
550 1.8 kiyohara return 0;
551 1.8 kiyohara goto receive;
552 1.8 kiyohara }
553 1.8 kiyohara
554 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISAR, sc->sc_isar);
555 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_SADIE);
556 1.8 kiyohara
557 1.8 kiyohara /* Poll Slave Address Detected */
558 1.8 kiyohara tries = I2C_RETRY_COUNT;
559 1.8 kiyohara while (1 /*CONSTCOND*/) {
560 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ISR);
561 1.8 kiyohara if ((rv & (ISR_SAD | ISR_UB)) == (ISR_SAD | ISR_UB))
562 1.8 kiyohara break;
563 1.8 kiyohara if (--tries <= 0)
564 1.8 kiyohara return 0;
565 1.8 kiyohara delay(1000); /* XXXX */
566 1.8 kiyohara }
567 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISR, ISR_SAD); /* Clear SAD */
568 1.8 kiyohara
569 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ISR);
570 1.8 kiyohara if (rv & ISR_RWM) {
571 1.8 kiyohara if (op != I2C_F_WRITE)
572 1.8 kiyohara return 0;
573 1.8 kiyohara if (rv & ISR_ACKNAK)
574 1.8 kiyohara return 0;
575 1.8 kiyohara
576 1.8 kiyohara send:
577 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_IDBR, data[n]);
578 1.8 kiyohara
579 1.8 kiyohara /* Initiate the transfer */
580 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ICR);
581 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_TB | ICR_ITEIE);
582 1.8 kiyohara
583 1.8 kiyohara while (n < len - 1) {
584 1.8 kiyohara timeout = 10000;
585 1.8 kiyohara while (--timeout > 0) {
586 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ISR);
587 1.8 kiyohara rv &= (ISR_ITE | ISR_ACKNAK | ISR_RWM);
588 1.8 kiyohara if (rv == ISR_ITE)
589 1.8 kiyohara break;
590 1.8 kiyohara delay(1);
591 1.8 kiyohara }
592 1.8 kiyohara if (timeout == 0)
593 1.8 kiyohara goto err;
594 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
595 1.8 kiyohara
596 1.8 kiyohara n++;
597 1.8 kiyohara if (n < len)
598 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_IDBR, data[n]);
599 1.8 kiyohara
600 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ICR);
601 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR,
602 1.8 kiyohara rv | ICR_TB | ICR_ITEIE);
603 1.8 kiyohara }
604 1.8 kiyohara
605 1.8 kiyohara timeout = 10000;
606 1.8 kiyohara while (--timeout > 0) {
607 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ISR);
608 1.8 kiyohara rv &= (ISR_ITE | ISR_ACKNAK | ISR_RWM);
609 1.8 kiyohara if (rv == (ISR_ITE | ISR_ACKNAK))
610 1.8 kiyohara break;
611 1.8 kiyohara delay(1);
612 1.8 kiyohara }
613 1.8 kiyohara if (timeout == 0)
614 1.8 kiyohara goto err;
615 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISR, ISR_ITE);
616 1.8 kiyohara
617 1.8 kiyohara n++;
618 1.8 kiyohara } else {
619 1.8 kiyohara if (op != I2C_F_READ)
620 1.8 kiyohara return 0;
621 1.8 kiyohara
622 1.8 kiyohara while (n < len) {
623 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ICR);
624 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR,
625 1.8 kiyohara rv | ICR_TB | ICR_IRFIE);
626 1.8 kiyohara
627 1.8 kiyohara receive:
628 1.8 kiyohara timeout = 10000;
629 1.8 kiyohara while (--timeout > 0) {
630 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ISR);
631 1.8 kiyohara rv &= (ISR_IRF | ISR_ACKNAK | ISR_RWM);
632 1.8 kiyohara if (rv == ISR_IRF)
633 1.8 kiyohara break;
634 1.8 kiyohara delay(1);
635 1.8 kiyohara }
636 1.8 kiyohara if (timeout == 0)
637 1.8 kiyohara goto err;
638 1.8 kiyohara
639 1.8 kiyohara data[n++] = bus_space_read_4(iot, ioh, I2C_IDBR);
640 1.8 kiyohara
641 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISR, ISR_IRF);
642 1.8 kiyohara }
643 1.8 kiyohara
644 1.8 kiyohara /* Release I2C bus and allow next transfer. */
645 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ICR);
646 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, rv | ICR_TB);
647 1.8 kiyohara }
648 1.8 kiyohara
649 1.8 kiyohara timeout = 10000;
650 1.8 kiyohara while (--timeout > 0) {
651 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ISR);
652 1.8 kiyohara rv &= (ISR_UB | ISR_SSD);
653 1.8 kiyohara if (rv == ISR_SSD)
654 1.8 kiyohara break;
655 1.8 kiyohara delay(1);
656 1.8 kiyohara }
657 1.8 kiyohara if (timeout == 0)
658 1.8 kiyohara goto err;
659 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISR, ISR_SSD);
660 1.8 kiyohara
661 1.8 kiyohara err:
662 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR, ICR_IUE | ICR_BEIE | ICR_SADIE);
663 1.8 kiyohara
664 1.8 kiyohara sc->sc_stat = PI2C_STAT_STOP;
665 1.8 kiyohara return n;
666 1.8 kiyohara }
667 1.8 kiyohara
668 1.8 kiyohara int
669 1.8 kiyohara pxa2x0_i2c_intr_sub(struct pxa2x0_i2c_softc *sc, int *len, uint8_t *buf)
670 1.8 kiyohara {
671 1.8 kiyohara bus_space_tag_t iot = sc->sc_iot;
672 1.8 kiyohara bus_space_handle_t ioh = sc->sc_ioh;
673 1.8 kiyohara int rv;
674 1.8 kiyohara uint16_t isr;
675 1.8 kiyohara
676 1.8 kiyohara isr = bus_space_read_4(iot, ioh, I2C_ISR);
677 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ISR,
678 1.8 kiyohara isr & (ISR_BED|ISR_SAD|ISR_IRF|ISR_ITE|ISR_ALD|ISR_SSD));
679 1.8 kiyohara
680 1.8 kiyohara DPRINTF(("%s: Interrupt Status 0x%x\n", __func__, isr));
681 1.8 kiyohara
682 1.8 kiyohara *len = 0;
683 1.8 kiyohara if (isr & ISR_SAD) { /* Slave Address Detected */
684 1.8 kiyohara if (isr & ISR_RWM)
685 1.8 kiyohara sc->sc_stat = PI2C_STAT_SEND;
686 1.8 kiyohara else {
687 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ICR);
688 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR,
689 1.8 kiyohara rv | ICR_TB | ICR_IRFIE);
690 1.8 kiyohara sc->sc_stat = PI2C_STAT_RECEIVE;
691 1.8 kiyohara }
692 1.8 kiyohara return 1; /* handled */
693 1.8 kiyohara } else if (isr & ISR_IRF) { /* IDBR Receive Full */
694 1.8 kiyohara *buf = bus_space_read_4(iot, ioh, I2C_IDBR);
695 1.8 kiyohara *len = 1;
696 1.8 kiyohara
697 1.8 kiyohara /* Next transfer start */
698 1.8 kiyohara rv = bus_space_read_4(iot, ioh, I2C_ICR);
699 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR,
700 1.8 kiyohara rv | ICR_TB | ICR_IRFIE | ICR_SSDIE);
701 1.8 kiyohara return 1; /* handled */
702 1.8 kiyohara } else if (isr & ISR_SSD) { /* Slave STOP Detected */
703 1.8 kiyohara sc->sc_stat = PI2C_STAT_STOP;
704 1.8 kiyohara
705 1.8 kiyohara bus_space_write_4(iot, ioh, I2C_ICR,
706 1.8 kiyohara ICR_IUE | ICR_BEIE | ICR_SADIE);
707 1.8 kiyohara return 1; /* handled */
708 1.8 kiyohara } else if (isr & ISR_BED) { /* Bus Error Detected */
709 1.8 kiyohara aprint_error_dev(sc->sc_dev,
710 1.8 kiyohara "%s: Bus Error Detected\n", __func__);
711 1.8 kiyohara sc->sc_stat = PI2C_STAT_ERROR;
712 1.8 kiyohara return 0; /* not handled */
713 1.8 kiyohara }
714 1.8 kiyohara
715 1.8 kiyohara sc->sc_stat = PI2C_STAT_UNKNOWN;
716 1.8 kiyohara aprint_error_dev(sc->sc_dev, "Interrupt not handled 0x%x\n", isr);
717 1.8 kiyohara return 0; /* not handled */
718 1.8 kiyohara }
719