]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iicbus/twsi/twsi.c
MFV r346563:
[FreeBSD/FreeBSD.git] / sys / dev / iicbus / twsi / twsi.c
1 /*-
2  * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
3  * All rights reserved.
4  *
5  * Developed by Semihalf.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of MARVELL nor the names of contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 /*
33  * Driver for the TWSI (aka I2C, aka IIC) bus controller found on Marvell
34  * and Allwinner SoCs. Supports master operation only.
35  *
36  * Calls to DELAY() are needed per Application Note AN-179 "TWSI Software
37  * Guidelines for Discovery(TM), Horizon (TM) and Feroceon(TM) Devices".
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/bus.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/resource.h>
49
50 #include <machine/_inttypes.h>
51 #include <machine/bus.h>
52 #include <machine/resource.h>
53
54 #include <sys/rman.h>
55
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58
59 #include <dev/iicbus/iiconf.h>
60 #include <dev/iicbus/iicbus.h>
61 #include <dev/ofw/ofw_bus.h>
62 #include <dev/ofw/ofw_bus_subr.h>
63
64 #include <dev/iicbus/twsi/twsi.h>
65
66 #include "iicbus_if.h"
67
68 #define TWSI_CONTROL_ACK        (1 << 2)
69 #define TWSI_CONTROL_IFLG       (1 << 3)
70 #define TWSI_CONTROL_STOP       (1 << 4)
71 #define TWSI_CONTROL_START      (1 << 5)
72 #define TWSI_CONTROL_TWSIEN     (1 << 6)
73 #define TWSI_CONTROL_INTEN      (1 << 7)
74
75 #define TWSI_STATUS_START               0x08
76 #define TWSI_STATUS_RPTD_START          0x10
77 #define TWSI_STATUS_ADDR_W_ACK          0x18
78 #define TWSI_STATUS_ADDR_W_NACK         0x20
79 #define TWSI_STATUS_DATA_WR_ACK         0x28
80 #define TWSI_STATUS_DATA_WR_NACK        0x30
81 #define TWSI_STATUS_ADDR_R_ACK          0x40
82 #define TWSI_STATUS_ADDR_R_NACK         0x48
83 #define TWSI_STATUS_DATA_RD_ACK         0x50
84 #define TWSI_STATUS_DATA_RD_NOACK       0x58
85
86 #define TWSI_DEBUG
87 #undef TWSI_DEBUG
88
89 #ifdef TWSI_DEBUG
90 #define debugf(dev, fmt, args...) device_printf(dev, "%s: " fmt, __func__, ##args)
91 #else
92 #define debugf(dev, fmt, args...)
93 #endif
94
95 static struct resource_spec res_spec[] = {
96         { SYS_RES_MEMORY, 0, RF_ACTIVE },
97         { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE},
98         { -1, 0 }
99 };
100
101 static __inline uint32_t
102 TWSI_READ(struct twsi_softc *sc, bus_size_t off)
103 {
104         uint32_t val;
105
106         val = bus_read_4(sc->res[0], off);
107         debugf(sc->dev, "read %x from %lx\n", val, off);
108         return (val);
109 }
110
111 static __inline void
112 TWSI_WRITE(struct twsi_softc *sc, bus_size_t off, uint32_t val)
113 {
114
115         debugf(sc->dev, "Writing %x to %lx\n", val, off);
116         bus_write_4(sc->res[0], off, val);
117 }
118
119 static __inline void
120 twsi_control_clear(struct twsi_softc *sc, uint32_t mask)
121 {
122         uint32_t val;
123
124         val = TWSI_READ(sc, sc->reg_control);
125         debugf(sc->dev, "read val=%x\n", val);
126         val &= ~(TWSI_CONTROL_STOP | TWSI_CONTROL_START);
127         val &= ~mask;
128         debugf(sc->dev, "write val=%x\n", val);
129         TWSI_WRITE(sc, sc->reg_control, val);
130 }
131
132 static __inline void
133 twsi_control_set(struct twsi_softc *sc, uint32_t mask)
134 {
135         uint32_t val;
136
137         val = TWSI_READ(sc, sc->reg_control);
138         debugf(sc->dev, "read val=%x\n", val);
139         val &= ~(TWSI_CONTROL_STOP | TWSI_CONTROL_START);
140         val |= mask;
141         debugf(sc->dev, "write val=%x\n", val);
142         TWSI_WRITE(sc, sc->reg_control, val);
143 }
144
145 static __inline void
146 twsi_clear_iflg(struct twsi_softc *sc)
147 {
148
149         DELAY(1000);
150         twsi_control_clear(sc, TWSI_CONTROL_IFLG);
151         DELAY(1000);
152 }
153
154
155 /*
156  * timeout given in us
157  * returns
158  *   0 on successful mask change
159  *   non-zero on timeout
160  */
161 static int
162 twsi_poll_ctrl(struct twsi_softc *sc, int timeout, uint32_t mask)
163 {
164
165         timeout /= 10;
166         debugf(sc->dev, "Waiting for ctrl reg to match mask %x\n", mask);
167         while (!(TWSI_READ(sc, sc->reg_control) & mask)) {
168                 DELAY(10);
169                 if (--timeout < 0)
170                         return (timeout);
171         }
172         debugf(sc->dev, "done\n");
173         return (0);
174 }
175
176
177 /*
178  * 'timeout' is given in us. Note also that timeout handling is not exact --
179  * twsi_locked_start() total wait can be more than 2 x timeout
180  * (twsi_poll_ctrl() is called twice). 'mask' can be either TWSI_STATUS_START
181  * or TWSI_STATUS_RPTD_START
182  */
183 static int
184 twsi_locked_start(device_t dev, struct twsi_softc *sc, int32_t mask,
185     u_char slave, int timeout)
186 {
187         int read_access, iflg_set = 0;
188         uint32_t status;
189
190         mtx_assert(&sc->mutex, MA_OWNED);
191
192         if (mask == TWSI_STATUS_RPTD_START)
193                 /* read IFLG to know if it should be cleared later; from NBSD */
194                 iflg_set = TWSI_READ(sc, sc->reg_control) & TWSI_CONTROL_IFLG;
195
196         debugf(dev, "send start\n");
197         twsi_control_set(sc, TWSI_CONTROL_START);
198
199         if (mask == TWSI_STATUS_RPTD_START && iflg_set) {
200                 debugf(dev, "IFLG set, clearing (mask=%x)\n", mask);
201                 twsi_clear_iflg(sc);
202         }
203
204         /*
205          * Without this delay we timeout checking IFLG if the timeout is 0.
206          * NBSD driver always waits here too.
207          */
208         DELAY(1000);
209
210         if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
211                 debugf(dev, "timeout sending %sSTART condition\n",
212                     mask == TWSI_STATUS_START ? "" : "repeated ");
213                 return (IIC_ETIMEOUT);
214         }
215
216         status = TWSI_READ(sc, sc->reg_status);
217         debugf(dev, "status=%x\n", status);
218
219         if (status != mask) {
220                 debugf(dev, "wrong status (%02x) after sending %sSTART condition\n",
221                     status, mask == TWSI_STATUS_START ? "" : "repeated ");
222                 return (IIC_ESTATUS);
223         }
224
225         TWSI_WRITE(sc, sc->reg_data, slave);
226         twsi_clear_iflg(sc);
227         DELAY(1000);
228
229         if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
230                 debugf(dev, "timeout sending slave address (timeout=%d)\n", timeout);
231                 return (IIC_ETIMEOUT);
232         }
233
234         read_access = (slave & 0x1) ? 1 : 0;
235         status = TWSI_READ(sc, sc->reg_status);
236         if (status != (read_access ?
237             TWSI_STATUS_ADDR_R_ACK : TWSI_STATUS_ADDR_W_ACK)) {
238                 debugf(dev, "no ACK (status: %02x) after sending slave address\n",
239                     status);
240                 return (IIC_ENOACK);
241         }
242
243         return (IIC_NOERR);
244 }
245
246 #ifdef EXT_RESOURCES
247 #define TWSI_BAUD_RATE_RAW(C,M,N)       ((C)/((10*(M+1))<<(N)))
248 #define ABSSUB(a,b)     (((a) > (b)) ? (a) - (b) : (b) - (a))
249
250 static int
251 twsi_calc_baud_rate(struct twsi_softc *sc, const u_int target,
252   int *param)
253 {
254         uint64_t clk;
255         uint32_t cur, diff, diff0;
256         int m, n, m0, n0;
257
258         /* Calculate baud rate. */
259         diff0 = 0xffffffff;
260
261         if (clk_get_freq(sc->clk_core, &clk) < 0)
262                 return (-1);
263
264         debugf(sc->dev, "Bus clock is at %ju\n", clk);
265
266         for (n = 0; n < 8; n++) {
267                 for (m = 0; m < 16; m++) {
268                         cur = TWSI_BAUD_RATE_RAW(clk,m,n);
269                         diff = ABSSUB(target, cur);
270                         if (diff < diff0) {
271                                 m0 = m;
272                                 n0 = n;
273                                 diff0 = diff;
274                         }
275                 }
276         }
277         *param = TWSI_BAUD_RATE_PARAM(m0, n0);
278
279         return (0);
280 }
281 #endif /* EXT_RESOURCES */
282
283 /*
284  * Only slave mode supported, disregard [old]addr
285  */
286 static int
287 twsi_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
288 {
289         struct twsi_softc *sc;
290         uint32_t param;
291 #ifdef EXT_RESOURCES
292         u_int busfreq;
293 #endif
294
295         sc = device_get_softc(dev);
296
297 #ifdef EXT_RESOURCES
298         busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
299
300         if (twsi_calc_baud_rate(sc, busfreq, &param) == -1) {
301 #endif
302                 switch (speed) {
303                 case IIC_SLOW:
304                 case IIC_FAST:
305                         param = sc->baud_rate[speed].param;
306                         debugf(dev, "Using IIC_FAST mode with speed param=%x\n", param);
307                         break;
308                 case IIC_FASTEST:
309                 case IIC_UNKNOWN:
310                 default:
311                         param = sc->baud_rate[IIC_FAST].param;
312                         debugf(dev, "Using IIC_FASTEST/UNKNOWN mode with speed param=%x\n", param);
313                         break;
314                 }
315 #ifdef EXT_RESOURCES
316         }
317 #endif
318
319         debugf(dev, "Using clock param=%x\n", param);
320
321         mtx_lock(&sc->mutex);
322         TWSI_WRITE(sc, sc->reg_soft_reset, 0x0);
323         TWSI_WRITE(sc, sc->reg_baud_rate, param);
324         TWSI_WRITE(sc, sc->reg_control, TWSI_CONTROL_TWSIEN);
325         DELAY(1000);
326         mtx_unlock(&sc->mutex);
327
328         return (0);
329 }
330
331 static int
332 twsi_stop(device_t dev)
333 {
334         struct twsi_softc *sc;
335
336         sc = device_get_softc(dev);
337
338         debugf(dev, "%s\n", __func__);
339         mtx_lock(&sc->mutex);
340         twsi_control_clear(sc, TWSI_CONTROL_ACK);
341         twsi_control_set(sc, TWSI_CONTROL_STOP);
342         twsi_clear_iflg(sc);
343         DELAY(1000);
344         mtx_unlock(&sc->mutex);
345
346         return (IIC_NOERR);
347 }
348
349 /*
350  * timeout is given in us
351  */
352 static int
353 twsi_repeated_start(device_t dev, u_char slave, int timeout)
354 {
355         struct twsi_softc *sc;
356         int rv;
357
358         sc = device_get_softc(dev);
359
360         debugf(dev, "%s: slave=%x\n", __func__, slave);
361         mtx_lock(&sc->mutex);
362         rv = twsi_locked_start(dev, sc, TWSI_STATUS_RPTD_START, slave,
363             timeout);
364         mtx_unlock(&sc->mutex);
365
366         if (rv) {
367                 twsi_stop(dev);
368                 return (rv);
369         } else
370                 return (IIC_NOERR);
371 }
372
373 /*
374  * timeout is given in us
375  */
376 static int
377 twsi_start(device_t dev, u_char slave, int timeout)
378 {
379         struct twsi_softc *sc;
380         int rv;
381
382         sc = device_get_softc(dev);
383
384         debugf(dev, "%s: slave=%x\n", __func__, slave);
385         mtx_lock(&sc->mutex);
386         rv = twsi_locked_start(dev, sc, TWSI_STATUS_START, slave, timeout);
387         mtx_unlock(&sc->mutex);
388
389         if (rv) {
390                 twsi_stop(dev);
391                 return (rv);
392         } else
393                 return (IIC_NOERR);
394 }
395
396 static int
397 twsi_read(device_t dev, char *buf, int len, int *read, int last, int delay)
398 {
399         struct twsi_softc *sc;
400         uint32_t status;
401         int last_byte, rv;
402
403         sc = device_get_softc(dev);
404
405         mtx_lock(&sc->mutex);
406         *read = 0;
407         while (*read < len) {
408                 /*
409                  * Check if we are reading last byte of the last buffer,
410                  * do not send ACK then, per I2C specs
411                  */
412                 last_byte = ((*read == len - 1) && last) ? 1 : 0;
413                 if (last_byte)
414                         twsi_control_clear(sc, TWSI_CONTROL_ACK);
415                 else
416                         twsi_control_set(sc, TWSI_CONTROL_ACK);
417
418                 twsi_clear_iflg(sc);
419                 DELAY(1000);
420
421                 if (twsi_poll_ctrl(sc, delay, TWSI_CONTROL_IFLG)) {
422                         debugf(dev, "timeout reading data (delay=%d)\n", delay);
423                         rv = IIC_ETIMEOUT;
424                         goto out;
425                 }
426
427                 status = TWSI_READ(sc, sc->reg_status);
428                 if (status != (last_byte ?
429                     TWSI_STATUS_DATA_RD_NOACK : TWSI_STATUS_DATA_RD_ACK)) {
430                         debugf(dev, "wrong status (%02x) while reading\n", status);
431                         rv = IIC_ESTATUS;
432                         goto out;
433                 }
434
435                 *buf++ = TWSI_READ(sc, sc->reg_data);
436                 (*read)++;
437         }
438         rv = IIC_NOERR;
439 out:
440         mtx_unlock(&sc->mutex);
441         return (rv);
442 }
443
444 static int
445 twsi_write(device_t dev, const char *buf, int len, int *sent, int timeout)
446 {
447         struct twsi_softc *sc;
448         uint32_t status;
449         int rv;
450
451         sc = device_get_softc(dev);
452
453         mtx_lock(&sc->mutex);
454         *sent = 0;
455         while (*sent < len) {
456                 TWSI_WRITE(sc, sc->reg_data, *buf++);
457
458                 twsi_clear_iflg(sc);
459                 DELAY(1000);
460                 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
461                         debugf(dev, "timeout writing data (timeout=%d)\n", timeout);
462                         rv = IIC_ETIMEOUT;
463                         goto out;
464                 }
465
466                 status = TWSI_READ(sc, sc->reg_status);
467                 if (status != TWSI_STATUS_DATA_WR_ACK) {
468                         debugf(dev, "wrong status (%02x) while writing\n", status);
469                         rv = IIC_ESTATUS;
470                         goto out;
471                 }
472                 (*sent)++;
473         }
474         rv = IIC_NOERR;
475 out:
476         mtx_unlock(&sc->mutex);
477         return (rv);
478 }
479
480 static int
481 twsi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
482 {
483         struct twsi_softc *sc;
484         int i;
485
486         sc = device_get_softc(dev);
487
488         if (sc->have_intr == false)
489                 return (iicbus_transfer_gen(dev, msgs, nmsgs));
490
491         sc->error = 0;
492
493         sc->control_val = TWSI_CONTROL_TWSIEN |
494                 TWSI_CONTROL_INTEN | TWSI_CONTROL_ACK;
495         TWSI_WRITE(sc, sc->reg_control, sc->control_val);
496         debugf(dev, "transmitting %d messages\n", nmsgs);
497         debugf(sc->dev, "status=%x\n", TWSI_READ(sc, sc->reg_status));
498         for (i = 0; i < nmsgs && sc->error == 0; i++) {
499                 sc->transfer = 1;
500                 sc->msg = &msgs[i];
501                 debugf(dev, "msg[%d] flags: %x\n", i, msgs[i].flags);
502                 debugf(dev, "msg[%d] len: %d\n", i, msgs[i].len);
503
504                 /* Send start and re-enable interrupts */
505                 sc->control_val = TWSI_CONTROL_TWSIEN |
506                         TWSI_CONTROL_INTEN | TWSI_CONTROL_ACK;
507                 if (sc->msg->len == 1)
508                         sc->control_val &= ~TWSI_CONTROL_ACK;
509                 TWSI_WRITE(sc, sc->reg_control, sc->control_val | TWSI_CONTROL_START);
510                 while (sc->error == 0 && sc->transfer != 0) {
511                         pause_sbt("twsi", SBT_1MS * 30, SBT_1MS, 0);
512                 }
513
514                 debugf(dev, "Done with msg[%d]\n", i);
515                 if (sc->error) {
516                         debugf(sc->dev, "Error, aborting (%d)\n", sc->error);
517                         TWSI_WRITE(sc, sc->reg_control, 0);
518                         goto out;
519                 }
520         }
521
522         /* Disable module and interrupts */
523         debugf(sc->dev, "status=%x\n", TWSI_READ(sc, sc->reg_status));
524         TWSI_WRITE(sc, sc->reg_control, 0);
525         debugf(sc->dev, "status=%x\n", TWSI_READ(sc, sc->reg_status));
526
527 out:
528         return (sc->error);
529 }
530
531 static void
532 twsi_intr(void *arg)
533 {
534         struct twsi_softc *sc;
535         uint32_t status;
536         int transfer_done = 0;
537
538         sc = arg;
539
540         debugf(sc->dev, "Got interrupt\n");
541
542         while (TWSI_READ(sc, sc->reg_control) & TWSI_CONTROL_IFLG) {
543                 status = TWSI_READ(sc, sc->reg_status);
544                 debugf(sc->dev, "status=%x\n", status);
545
546                 switch (status) {
547                 case TWSI_STATUS_START:
548                 case TWSI_STATUS_RPTD_START:
549                         /* Transmit the address */
550                         debugf(sc->dev, "Send the address\n");
551
552                         if (sc->msg->flags & IIC_M_RD)
553                                 TWSI_WRITE(sc, sc->reg_data,
554                                     sc->msg->slave | LSB);
555                         else
556                                 TWSI_WRITE(sc, sc->reg_data,
557                                     sc->msg->slave & ~LSB);
558
559                         TWSI_WRITE(sc, sc->reg_control, sc->control_val);
560                         break;
561
562                 case TWSI_STATUS_ADDR_W_ACK:
563                         debugf(sc->dev, "Ack received after transmitting the address\n");
564                         /* Directly send the first byte */
565                         sc->sent_bytes = 0;
566                         debugf(sc->dev, "Sending byte 0 = %x\n", sc->msg->buf[0]);
567                         TWSI_WRITE(sc, sc->reg_data, sc->msg->buf[0]);
568
569                         TWSI_WRITE(sc, sc->reg_control, sc->control_val);
570                         break;
571
572                 case TWSI_STATUS_ADDR_R_ACK:
573                         debugf(sc->dev, "Ack received after transmitting the address\n");
574                         sc->recv_bytes = 0;
575
576                         TWSI_WRITE(sc, sc->reg_control, sc->control_val);
577                         break;
578
579                 case TWSI_STATUS_ADDR_W_NACK:
580                 case TWSI_STATUS_ADDR_R_NACK:
581                         debugf(sc->dev, "No ack received after transmitting the address\n");
582                         sc->transfer = 0;
583                         sc->error = ETIMEDOUT;
584                         sc->control_val = 0;
585                         wakeup(sc);
586                         break;
587
588                 case TWSI_STATUS_DATA_WR_ACK:
589                         debugf(sc->dev, "Ack received after transmitting data\n");
590                         if (sc->sent_bytes++ == (sc->msg->len - 1)) {
591                                 debugf(sc->dev, "Done sending all the bytes\n");
592                                 /* Send stop, no interrupts on stop */
593                                 if (!(sc->msg->flags & IIC_M_NOSTOP)) {
594                                         debugf(sc->dev, "Done TX data, send stop\n");
595                                         TWSI_WRITE(sc, sc->reg_control,
596                                           sc->control_val | TWSI_CONTROL_STOP);
597                                 } else {
598                                         sc->control_val &= ~TWSI_CONTROL_INTEN;
599                                         TWSI_WRITE(sc, sc->reg_control,
600                                             sc->control_val);
601                                 }
602                                 transfer_done = 1;
603                         } else {
604                                 debugf(sc->dev, "Sending byte %d = %x\n",
605                                     sc->sent_bytes,
606                                     sc->msg->buf[sc->sent_bytes]);
607                                 TWSI_WRITE(sc, sc->reg_data,
608                                     sc->msg->buf[sc->sent_bytes]);
609                                 TWSI_WRITE(sc, sc->reg_control,
610                                     sc->control_val);
611                         }
612
613                         break;
614                 case TWSI_STATUS_DATA_RD_ACK:
615                         debugf(sc->dev, "Ack received after receiving data\n");
616                         debugf(sc->dev, "msg_len=%d recv_bytes=%d\n", sc->msg->len, sc->recv_bytes);
617                         sc->msg->buf[sc->recv_bytes++] = TWSI_READ(sc, sc->reg_data);
618
619                         /* If we only have one byte left, disable ACK */
620                         if (sc->msg->len - sc->recv_bytes == 1)
621                                 sc->control_val &= ~TWSI_CONTROL_ACK;
622                         TWSI_WRITE(sc, sc->reg_control, sc->control_val);
623                         break;
624
625                 case TWSI_STATUS_DATA_RD_NOACK:
626                         if (sc->msg->len - sc->recv_bytes == 1) {
627                                 sc->msg->buf[sc->recv_bytes++] = TWSI_READ(sc, sc->reg_data);
628                                 debugf(sc->dev, "Done RX data, send stop (2)\n");
629                                 if (!(sc->msg->flags & IIC_M_NOSTOP))
630                                         TWSI_WRITE(sc, sc->reg_control,
631                                           sc->control_val | TWSI_CONTROL_STOP);
632                         } else {
633                                 debugf(sc->dev, "No ack when receiving data\n");
634                                 sc->error = ENXIO;
635                                 sc->control_val = 0;
636                         }
637                         sc->transfer = 0;
638                         transfer_done = 1;
639                         break;
640
641                 default:
642                         debugf(sc->dev, "status=%x hot handled\n", status);
643                         sc->transfer = 0;
644                         sc->error = ENXIO;
645                         sc->control_val = 0;
646                         wakeup(sc);
647                         break;
648                 }
649
650                 if (sc->need_ack)
651                         TWSI_WRITE(sc, sc->reg_control,
652                             sc->control_val | TWSI_CONTROL_IFLG);
653         }
654
655         debugf(sc->dev, "Done with interrupts\n");
656         if (transfer_done == 1) {
657                 sc->transfer = 0;
658                 wakeup(sc);
659         }
660 }
661
662 static void
663 twsi_intr_start(void *pdev)
664 {
665         struct twsi_softc *sc;
666
667         sc = device_get_softc(pdev);
668
669         if ((bus_setup_intr(pdev, sc->res[1], INTR_TYPE_MISC | INTR_MPSAFE,
670               NULL, twsi_intr, sc, &sc->intrhand)))
671                 device_printf(pdev, "unable to register interrupt handler\n");
672
673         sc->have_intr = true;
674 }
675
676 int
677 twsi_attach(device_t dev)
678 {
679         struct twsi_softc *sc;
680
681         sc = device_get_softc(dev);
682         sc->dev = dev;
683
684         mtx_init(&sc->mutex, device_get_nameunit(dev), "twsi", MTX_DEF);
685
686         if (bus_alloc_resources(dev, res_spec, sc->res)) {
687                 device_printf(dev, "could not allocate resources\n");
688                 twsi_detach(dev);
689                 return (ENXIO);
690         }
691
692         /* Attach the iicbus. */
693         if ((sc->iicbus = device_add_child(dev, "iicbus", -1)) == NULL) {
694                 device_printf(dev, "could not allocate iicbus instance\n");
695                 twsi_detach(dev);
696                 return (ENXIO);
697         }
698         bus_generic_attach(dev);
699
700         config_intrhook_oneshot(twsi_intr_start, dev);
701
702         return (0);
703 }
704
705 int
706 twsi_detach(device_t dev)
707 {
708         struct twsi_softc *sc;
709         int rv;
710
711         sc = device_get_softc(dev);
712
713         if ((rv = bus_generic_detach(dev)) != 0)
714                 return (rv);
715
716         if (sc->iicbus != NULL)
717                 if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
718                         return (rv);
719
720         if (sc->intrhand != NULL)
721                 bus_teardown_intr(sc->dev, sc->res[1], sc->intrhand);
722
723         bus_release_resources(dev, res_spec, sc->res);
724
725         mtx_destroy(&sc->mutex);
726         return (0);
727 }
728
729 static device_method_t twsi_methods[] = {
730         /* device interface */
731         DEVMETHOD(device_detach,        twsi_detach),
732
733         /* Bus interface */
734         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
735         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
736         DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
737         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
738         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
739         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
740         DEVMETHOD(bus_adjust_resource,  bus_generic_adjust_resource),
741         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
742         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
743
744         /* iicbus interface */
745         DEVMETHOD(iicbus_callback, iicbus_null_callback),
746         DEVMETHOD(iicbus_repeated_start, twsi_repeated_start),
747         DEVMETHOD(iicbus_start,         twsi_start),
748         DEVMETHOD(iicbus_stop,          twsi_stop),
749         DEVMETHOD(iicbus_write,         twsi_write),
750         DEVMETHOD(iicbus_read,          twsi_read),
751         DEVMETHOD(iicbus_reset,         twsi_reset),
752         DEVMETHOD(iicbus_transfer,      twsi_transfer),
753         { 0, 0 }
754 };
755
756 DEFINE_CLASS_0(twsi, twsi_driver, twsi_methods,
757     sizeof(struct twsi_softc));