at91twi.c revision 1.7 1 1.7 chs /* $Id: at91twi.c,v 1.7 2016/02/14 19:54:20 chs Exp $ */
2 1.7 chs /* $NetBSD: at91twi.c,v 1.7 2016/02/14 19:54:20 chs Exp $ */
3 1.2 matt
4 1.2 matt /*-
5 1.2 matt * Copyright (c) 2007 Embedtronics Oy. All rights reserved.
6 1.2 matt *
7 1.2 matt * Based on arch/macppc/dev/ki2c.c,
8 1.2 matt * Copyright (c) 2001 Tsubai Masanari. All rights reserved.
9 1.2 matt *
10 1.2 matt * Redistribution and use in source and binary forms, with or without
11 1.2 matt * modification, are permitted provided that the following conditions
12 1.2 matt * are met:
13 1.2 matt * 1. Redistributions of source code must retain the above copyright
14 1.2 matt * notice, this list of conditions and the following disclaimer.
15 1.2 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 matt * notice, this list of conditions and the following disclaimer in the
17 1.2 matt * documentation and/or other materials provided with the distribution.
18 1.2 matt * 3. The name of the author may not be used to endorse or promote products
19 1.2 matt * derived from this software without specific prior written permission.
20 1.2 matt *
21 1.2 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.2 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.2 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.2 matt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.2 matt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.2 matt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.2 matt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.2 matt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.2 matt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 1.2 matt * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.2 matt */
32 1.2 matt
33 1.2 matt #include <sys/cdefs.h>
34 1.7 chs __KERNEL_RCSID(0, "$NetBSD: at91twi.c,v 1.7 2016/02/14 19:54:20 chs Exp $");
35 1.2 matt
36 1.2 matt #include <sys/param.h>
37 1.2 matt #include <sys/device.h>
38 1.2 matt #include <sys/systm.h>
39 1.2 matt
40 1.5 dyoung #include <sys/bus.h>
41 1.2 matt #include <sys/lock.h>
42 1.2 matt
43 1.2 matt #include <arm/at91/at91var.h>
44 1.2 matt #include <arm/at91/at91reg.h>
45 1.2 matt
46 1.2 matt #include <dev/i2c/i2cvar.h>
47 1.2 matt #include <arm/at91/at91twivar.h>
48 1.2 matt #include <arm/at91/at91twireg.h>
49 1.2 matt
50 1.2 matt int at91twi_match(device_t, cfdata_t, void *);
51 1.2 matt void at91twi_attach(device_t, device_t, void *);
52 1.2 matt inline u_int at91twi_readreg(struct at91twi_softc *, int);
53 1.2 matt inline void at91twi_writereg(struct at91twi_softc *, int, u_int);
54 1.2 matt int at91twi_intr(void *);
55 1.2 matt int at91twi_poll(struct at91twi_softc *, int, int);
56 1.2 matt int at91twi_start(struct at91twi_softc *, int, void *, int, int);
57 1.2 matt int at91twi_read(struct at91twi_softc *, int, void *, int, int);
58 1.2 matt int at91twi_write(struct at91twi_softc *, int, void *, int, int);
59 1.2 matt
60 1.2 matt /* I2C glue */
61 1.2 matt static int at91twi_i2c_acquire_bus(void *, int);
62 1.2 matt static void at91twi_i2c_release_bus(void *, int);
63 1.2 matt static int at91twi_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
64 1.2 matt void *, size_t, int);
65 1.2 matt
66 1.2 matt
67 1.2 matt CFATTACH_DECL_NEW(at91twi, sizeof(struct at91twi_softc),
68 1.2 matt at91twi_match, at91twi_attach, NULL, NULL);
69 1.2 matt
70 1.2 matt int
71 1.2 matt at91twi_match(device_t parent, cfdata_t match, void *aux)
72 1.2 matt {
73 1.2 matt if (strcmp(match->cf_name, "at91twi") == 0)
74 1.2 matt return 2;
75 1.2 matt return 0;
76 1.2 matt }
77 1.2 matt
78 1.2 matt void
79 1.2 matt at91twi_attach(device_t parent, device_t self, void *aux)
80 1.2 matt {
81 1.2 matt struct at91twi_softc *sc = device_private(self);
82 1.2 matt struct at91bus_attach_args *sa = aux;
83 1.2 matt struct i2cbus_attach_args iba;
84 1.2 matt unsigned ckdiv, cxdiv;
85 1.2 matt
86 1.2 matt // gather attach data:
87 1.2 matt sc->sc_dev = self;
88 1.2 matt sc->sc_iot = sa->sa_iot;
89 1.2 matt sc->sc_pid = sa->sa_pid;
90 1.2 matt
91 1.2 matt if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh))
92 1.6 chs panic("%s: Cannot map registers", device_xname(self));
93 1.2 matt
94 1.2 matt printf(": I2C controller\n");
95 1.2 matt
96 1.2 matt /* initialize I2C controller */
97 1.2 matt at91_peripheral_clock(sc->sc_pid, 1);
98 1.2 matt
99 1.2 matt at91twi_writereg(sc, TWI_CR, TWI_CR_SWRST);
100 1.2 matt delay(1000);
101 1.2 matt #if 1
102 1.2 matt // target to 100 kHz
103 1.2 matt for (ckdiv = 0; ckdiv < 8; ckdiv++) {
104 1.2 matt if ((cxdiv = (AT91_MSTCLK / (1U << ckdiv)) / (2 * 50000U)) < 256) {
105 1.2 matt goto found_ckdiv;
106 1.2 matt }
107 1.2 matt }
108 1.2 matt panic("%s: Cannot calculate clock divider!", __FUNCTION__);
109 1.2 matt
110 1.2 matt found_ckdiv:
111 1.2 matt #else
112 1.2 matt ckdiv = 5; cxdiv = 0xFF;
113 1.2 matt #endif
114 1.2 matt at91twi_writereg(sc, TWI_CWGR, (ckdiv << 16) | (cxdiv << 8) | cxdiv);
115 1.2 matt at91twi_writereg(sc, TWI_CR, TWI_CR_MSEN);
116 1.2 matt
117 1.2 matt //#ifdef AT91TWI_DEBUG
118 1.6 chs printf("%s: ckdiv=%d cxdiv=%d CWGR=0x%08X SR=0x%08X\n", device_xname(self), ckdiv, cxdiv, at91twi_readreg(sc, TWI_CWGR), at91twi_readreg(sc, TWI_SR));
119 1.2 matt //#endif
120 1.2 matt
121 1.2 matt /* initialize rest */
122 1.2 matt mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
123 1.2 matt sc->sc_ih = at91_intr_establish(sc->sc_pid, IPL_SERIAL, INTR_HIGH_LEVEL,
124 1.2 matt at91twi_intr, sc);
125 1.2 matt
126 1.2 matt /* fill in the i2c tag */
127 1.2 matt sc->sc_i2c.ic_cookie = sc;
128 1.2 matt sc->sc_i2c.ic_acquire_bus = at91twi_i2c_acquire_bus;
129 1.2 matt sc->sc_i2c.ic_release_bus = at91twi_i2c_release_bus;
130 1.2 matt sc->sc_i2c.ic_send_start = NULL;
131 1.2 matt sc->sc_i2c.ic_send_stop = NULL;
132 1.2 matt sc->sc_i2c.ic_initiate_xfer = NULL;
133 1.2 matt sc->sc_i2c.ic_read_byte = NULL;
134 1.2 matt sc->sc_i2c.ic_write_byte = NULL;
135 1.2 matt sc->sc_i2c.ic_exec = at91twi_i2c_exec;
136 1.2 matt
137 1.7 chs memset(&iba, 0, sizeof(iba));
138 1.2 matt iba.iba_tag = &sc->sc_i2c;
139 1.2 matt (void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
140 1.2 matt }
141 1.2 matt
142 1.2 matt u_int
143 1.3 dsl at91twi_readreg(struct at91twi_softc *sc, int reg)
144 1.2 matt {
145 1.2 matt return bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg);
146 1.2 matt }
147 1.2 matt
148 1.2 matt void
149 1.3 dsl at91twi_writereg(struct at91twi_softc *sc, int reg, u_int val)
150 1.2 matt {
151 1.2 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, val);
152 1.2 matt }
153 1.2 matt
154 1.2 matt int
155 1.2 matt at91twi_intr(void *arg)
156 1.2 matt {
157 1.2 matt struct at91twi_softc *sc = arg;
158 1.2 matt u_int sr, isr, imr;
159 1.2 matt
160 1.2 matt sr = at91twi_readreg(sc, TWI_SR);
161 1.2 matt imr = at91twi_readreg(sc, TWI_IMR);
162 1.2 matt isr = sr & imr;
163 1.2 matt
164 1.2 matt if (!isr) {
165 1.2 matt #ifdef AT91TWI_DEBUG
166 1.2 matt // printf("%s(%s): interrupts are disabled (sr=%08X imr=%08X)\n", __FUNCTION__, device_xname(sc->sc_dev), sr, imr);
167 1.2 matt #endif
168 1.2 matt return 0;
169 1.2 matt }
170 1.2 matt
171 1.2 matt if (isr & TWI_SR_TXCOMP) {
172 1.2 matt // transmission has completed!
173 1.2 matt if (sr & (TWI_SR_NACK | TWI_SR_UNRE | TWI_SR_OVRE)) {
174 1.2 matt // failed!
175 1.2 matt #ifdef AT91TWI_DEBUG
176 1.2 matt printf("%s(%s): FAILED (sr=%08X)\n", __FUNCTION__,
177 1.2 matt device_xname(sc->sc_dev), sr);
178 1.2 matt #endif
179 1.2 matt sc->sc_flags |= I2C_ERROR;
180 1.2 matt } else {
181 1.2 matt #ifdef AT91TWI_DEBUG
182 1.2 matt printf("%s(%s): SUCCESS (sr=%08X)\n", __FUNCTION__,
183 1.2 matt device_xname(sc->sc_dev), sr);
184 1.2 matt #endif
185 1.2 matt }
186 1.2 matt if (sc->sc_flags & I2C_READING && sr & TWI_SR_RXRDY) {
187 1.2 matt *sc->sc_data++ = at91twi_readreg(sc, TWI_RHR);
188 1.2 matt sc->sc_resid--;
189 1.2 matt }
190 1.2 matt sc->sc_flags &= ~I2C_BUSY;
191 1.2 matt at91twi_writereg(sc, TWI_IDR, -1);
192 1.2 matt goto out;
193 1.2 matt }
194 1.2 matt
195 1.2 matt if (isr & TWI_SR_TXRDY) {
196 1.2 matt if (--sc->sc_resid > 0)
197 1.2 matt at91twi_writereg(sc, TWI_THR, *sc->sc_data++);
198 1.2 matt }
199 1.2 matt
200 1.2 matt if (isr & TWI_SR_RXRDY) {
201 1.2 matt // data has been received
202 1.2 matt *sc->sc_data++ = at91twi_readreg(sc, TWI_RHR);
203 1.2 matt sc->sc_resid--;
204 1.2 matt }
205 1.2 matt
206 1.2 matt if (isr & (TWI_SR_TXRDY | TWI_SR_RXRDY) && sc->sc_resid <= 0) {
207 1.2 matt // all bytes have been transmitted, send stop condition
208 1.2 matt at91twi_writereg(sc, TWI_IDR, TWI_SR_RXRDY | TWI_SR_TXRDY);
209 1.2 matt at91twi_writereg(sc, TWI_CR, TWI_CR_STOP);
210 1.2 matt }
211 1.2 matt out:
212 1.2 matt return 1;
213 1.2 matt }
214 1.2 matt
215 1.2 matt int
216 1.4 dsl at91twi_poll(struct at91twi_softc *sc, int timo, int flags)
217 1.2 matt {
218 1.2 matt
219 1.2 matt timo = 1000000U;
220 1.2 matt
221 1.2 matt while (sc->sc_flags & I2C_BUSY) {
222 1.2 matt if (timo < 0) {
223 1.2 matt printf("i2c_poll: timeout\n");
224 1.2 matt return -1;
225 1.2 matt }
226 1.2 matt if (flags & I2C_F_POLL) {
227 1.2 matt at91_intr_poll(sc->sc_ih, 1);
228 1.2 matt delay(1);
229 1.2 matt timo--;
230 1.2 matt } else {
231 1.2 matt delay(100); // @@@ sleep!?
232 1.2 matt timo -= 100;
233 1.2 matt }
234 1.2 matt }
235 1.2 matt return 0;
236 1.2 matt }
237 1.2 matt
238 1.2 matt int
239 1.2 matt at91twi_start(struct at91twi_softc *sc, int addr, void *data, int len,
240 1.2 matt int flags)
241 1.2 matt {
242 1.2 matt int rd = (sc->sc_flags & I2C_READING);
243 1.2 matt int timo, s;
244 1.2 matt
245 1.2 matt KASSERT((addr & 1) == 0);
246 1.2 matt
247 1.2 matt sc->sc_data = data;
248 1.2 matt sc->sc_resid = len;
249 1.2 matt sc->sc_flags |= I2C_BUSY;
250 1.2 matt
251 1.2 matt timo = 1000 + len * 200;
252 1.2 matt
253 1.2 matt s = splserial();
254 1.2 matt // if writing, queue first byte immediately
255 1.2 matt if (!rd)
256 1.2 matt at91twi_writereg(sc, TWI_THR, *sc->sc_data++);
257 1.2 matt // if there's just one byte to transmit, we must set STOP-bit too
258 1.2 matt if (sc->sc_resid == 1) {
259 1.2 matt at91twi_writereg(sc, TWI_IER, TWI_SR_TXCOMP);
260 1.2 matt at91twi_writereg(sc, TWI_CR, TWI_CR_START | TWI_CR_STOP);
261 1.2 matt } else {
262 1.2 matt at91twi_writereg(sc, TWI_IER, TWI_SR_TXCOMP
263 1.2 matt | (rd ? TWI_SR_RXRDY : TWI_SR_TXRDY));
264 1.2 matt at91twi_writereg(sc, TWI_CR, TWI_CR_START);
265 1.2 matt }
266 1.2 matt splx(s);
267 1.2 matt
268 1.2 matt if (at91twi_poll(sc, timo, flags))
269 1.2 matt return -1;
270 1.2 matt if (sc->sc_flags & I2C_ERROR) {
271 1.2 matt printf("I2C_ERROR\n");
272 1.2 matt return -1;
273 1.2 matt }
274 1.2 matt return 0;
275 1.2 matt }
276 1.2 matt
277 1.2 matt int
278 1.4 dsl at91twi_read(struct at91twi_softc *sc, int addr, void *data, int len, int flags)
279 1.2 matt {
280 1.2 matt sc->sc_flags = I2C_READING;
281 1.2 matt #ifdef AT91TWI_DEBUG
282 1.2 matt printf("at91twi_read: %02x %d\n", addr, len);
283 1.2 matt #endif
284 1.2 matt return at91twi_start(sc, addr, data, len, flags);
285 1.2 matt }
286 1.2 matt
287 1.2 matt int
288 1.4 dsl at91twi_write(struct at91twi_softc *sc, int addr, void *data, int len, int flags)
289 1.2 matt {
290 1.2 matt sc->sc_flags = 0;
291 1.2 matt #ifdef AT91TWI_DEBUG
292 1.2 matt printf("at91twi_write: %02x %d\n", addr, len);
293 1.2 matt #endif
294 1.2 matt return at91twi_start(sc, addr, data, len, flags);
295 1.2 matt }
296 1.2 matt
297 1.2 matt static int
298 1.2 matt at91twi_i2c_acquire_bus(void *cookie, int flags)
299 1.2 matt {
300 1.2 matt struct at91twi_softc *sc = cookie;
301 1.2 matt
302 1.2 matt if (flags & I2C_F_POLL)
303 1.2 matt return 0;
304 1.2 matt
305 1.2 matt mutex_enter(&sc->sc_buslock);
306 1.2 matt return 0;
307 1.2 matt }
308 1.2 matt
309 1.2 matt static void
310 1.2 matt at91twi_i2c_release_bus(void *cookie, int flags)
311 1.2 matt {
312 1.2 matt struct at91twi_softc *sc = cookie;
313 1.2 matt
314 1.2 matt if (flags & I2C_F_POLL)
315 1.2 matt return;
316 1.2 matt
317 1.2 matt mutex_exit(&sc->sc_buslock);
318 1.2 matt }
319 1.2 matt
320 1.2 matt int
321 1.2 matt at91twi_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
322 1.2 matt size_t cmdlen, void *vbuf, size_t buflen, int flags)
323 1.2 matt {
324 1.2 matt struct at91twi_softc *sc = cookie;
325 1.2 matt
326 1.2 matt if (I2C_OP_READ_P(op))
327 1.2 matt {
328 1.2 matt u_int iadr = 0;
329 1.2 matt if (vcmd) {
330 1.2 matt const uint8_t *cmd = (const uint8_t *)vcmd;
331 1.2 matt if (cmdlen > 3) {
332 1.2 matt // we're in trouble..
333 1.2 matt return -1;
334 1.2 matt }
335 1.2 matt iadr = cmd[0];
336 1.2 matt if (cmdlen > 1) {
337 1.2 matt iadr <<= 8;
338 1.2 matt iadr |= cmd[1];
339 1.2 matt }
340 1.2 matt if (cmdlen > 2) {
341 1.2 matt iadr <<= 8;
342 1.2 matt iadr |= cmd[2];
343 1.2 matt }
344 1.2 matt }
345 1.2 matt at91twi_writereg(sc, TWI_MMR, (addr << 16) | TWI_MMR_MREAD | (cmdlen << 8));
346 1.2 matt if (cmdlen > 0) {
347 1.2 matt #ifdef AT91TWI_DEBUG
348 1.2 matt printf("at91twi_read: %02x iadr=%08X mmr=%08X\n",
349 1.2 matt addr, iadr, at91twi_readreg(sc, TWI_MMR));
350 1.2 matt #endif
351 1.2 matt at91twi_writereg(sc, TWI_IADR, iadr);
352 1.2 matt }
353 1.2 matt if (at91twi_read(sc, addr, vbuf, buflen, flags) != 0)
354 1.2 matt return -1;
355 1.2 matt } else if (vcmd) {
356 1.2 matt at91twi_writereg(sc, TWI_MMR, addr << 16);
357 1.2 matt if (at91twi_write(sc, addr, __UNCONST(vcmd), cmdlen, flags) !=0)
358 1.2 matt return -1;
359 1.2 matt }
360 1.2 matt return 0;
361 1.2 matt }
362