4a1ebfc7caec85986f0ddee17b7a4204a528b7bc
[dragonfly.git] / sys / dev / powermng / ichsmb / ichsmb.c
1
2 /*
3  * ichsmb.c
4  *
5  * Copyright (c) 2000 Whistle Communications, Inc.
6  * All rights reserved.
7  * 
8  * Subject to the following obligations and disclaimer of warranty, use and
9  * redistribution of this software, in source or object code forms, with or
10  * without modifications are expressly permitted by Whistle Communications;
11  * provided, however, that:
12  * 1. Any and all reproductions of the source or object code must include the
13  *    copyright notice above and the following disclaimer of warranties; and
14  * 2. No rights are granted, in any manner or form, to use Whistle
15  *    Communications, Inc. trademarks, including the mark "WHISTLE
16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17  *    such appears in the above copyright notice or in the software.
18  * 
19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * Author: Archie Cobbs <archie@freebsd.org>
38  *
39  * $FreeBSD: src/sys/dev/ichsmb/ichsmb.c,v 1.1.2.1 2000/10/09 00:52:43 archie Exp $
40  * $DragonFly: src/sys/dev/powermng/ichsmb/ichsmb.c,v 1.4 2003/08/07 21:17:07 dillon Exp $
41  */
42
43 /*
44  * Support for the SMBus controller logical device which is part of the
45  * Intel 81801AA (ICH) and 81801AB (ICH0) I/O controller hub chips.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
52 #include <sys/syslog.h>
53 #include <sys/bus.h>
54
55 #include <machine/bus.h>
56 #include <sys/rman.h>
57 #include <machine/resource.h>
58
59 #include <bus/smbus/smbconf.h>
60
61 #include "ichsmb_var.h"
62 #include "ichsmb_reg.h"
63
64 /*
65  * Enable debugging by defining ICHSMB_DEBUG to a non-zero value.
66  */
67 #define ICHSMB_DEBUG    0
68 #if ICHSMB_DEBUG != 0 && defined(__GNUC__)
69 #define DBG(fmt, args...)       \
70         do { log(LOG_DEBUG, "%s: " fmt, __FUNCTION__ , ## args); } while (0)
71 #else
72 #define DBG(fmt, args...)       do { } while (0)
73 #endif
74
75 /*
76  * Our child device driver name
77  */
78 #define DRIVER_SMBUS    "smbus"
79
80 /*
81  * Internal functions
82  */
83 static int ichsmb_wait(sc_p sc);
84
85 /********************************************************************
86                 BUS-INDEPENDENT BUS METHODS
87 ********************************************************************/
88
89 /*
90  * Handle probe-time duties that are independent of the bus
91  * our device lives on.
92  */
93 int
94 ichsmb_probe(device_t dev)
95 {
96         device_t smb;
97
98         /* Add child: an instance of the "smbus" device */
99         if ((smb = device_add_child(dev, DRIVER_SMBUS, -1)) == NULL) {
100                 log(LOG_ERR, "%s: no \"%s\" child found\n",
101                     device_get_nameunit(dev), DRIVER_SMBUS);
102                 return (ENXIO);
103         }
104         return (0);
105 }
106
107 /*
108  * Handle attach-time duties that are independent of the bus
109  * our device lives on.
110  */
111 int
112 ichsmb_attach(device_t dev)
113 {
114         const sc_p sc = device_get_softc(dev);
115         int error;
116
117         /* Clear interrupt conditions */
118         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_STA, 0xff);
119
120         /* Add "smbus" child */
121         if ((error = bus_generic_attach(dev)) != 0) {
122                 log(LOG_ERR, "%s: failed to attach child: %d\n",
123                     device_get_nameunit(dev), error);
124                 error = ENXIO;
125         }
126
127         /* Done */
128         return (error);
129 }
130
131 /********************************************************************
132                         SMBUS METHODS
133 ********************************************************************/
134
135 int 
136 ichsmb_callback(device_t dev, int index, caddr_t data)
137 {
138         int smb_error = 0;
139
140         DBG("index=%d how=%d\n", index, data ? *(int *)data : -1);
141         switch (index) {
142         case SMB_REQUEST_BUS:
143                 break;
144         case SMB_RELEASE_BUS:
145                 break;
146         default:
147                 smb_error = SMB_EABORT; /* XXX */
148                 break;
149         }
150         DBG("smb_error=%d\n", smb_error);
151         return (smb_error);
152 }
153
154 int
155 ichsmb_quick(device_t dev, u_char slave, int how)
156 {
157         const sc_p sc = device_get_softc(dev);
158         int smb_error;
159         int s;
160
161         DBG("slave=0x%02x how=%d\n", slave, how);
162         KASSERT(sc->ich_cmd == -1,
163             ("%s: ich_cmd=%d\n", __FUNCTION__ , sc->ich_cmd));
164         switch (how) {
165         case SMB_QREAD:
166         case SMB_QWRITE:
167                 s = splhigh();
168                 sc->ich_cmd = ICH_HST_CNT_SMB_CMD_QUICK;
169                 bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_XMIT_SLVA,
170                     (slave << 1) | (how == SMB_QREAD ?
171                         ICH_XMIT_SLVA_READ : ICH_XMIT_SLVA_WRITE));
172                 bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CNT,
173                     ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
174                 smb_error = ichsmb_wait(sc);
175                 splx(s);
176                 break;
177         default:
178                 smb_error = SMB_ENOTSUPP;
179         }
180         DBG("smb_error=%d\n", smb_error);
181         return (smb_error);
182 }
183
184 int
185 ichsmb_sendb(device_t dev, u_char slave, char byte)
186 {
187         const sc_p sc = device_get_softc(dev);
188         int smb_error;
189         int s;
190
191         DBG("slave=0x%02x byte=0x%02x\n", slave, (u_char)byte);
192         KASSERT(sc->ich_cmd == -1,
193             ("%s: ich_cmd=%d\n", __FUNCTION__ , sc->ich_cmd));
194         s = splhigh();
195         sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BYTE;
196         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_XMIT_SLVA,
197             (slave << 1) | ICH_XMIT_SLVA_WRITE);
198         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CMD, byte);
199         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CNT,
200             ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
201         smb_error = ichsmb_wait(sc);
202         splx(s);
203         DBG("smb_error=%d\n", smb_error);
204         return (smb_error);
205 }
206
207 int
208 ichsmb_recvb(device_t dev, u_char slave, char *byte)
209 {
210         const sc_p sc = device_get_softc(dev);
211         int smb_error;
212         int s;
213
214         DBG("slave=0x%02x\n", slave);
215         KASSERT(sc->ich_cmd == -1,
216             ("%s: ich_cmd=%d\n", __FUNCTION__ , sc->ich_cmd));
217         s = splhigh();
218         sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BYTE;
219         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_XMIT_SLVA,
220             (slave << 1) | ICH_XMIT_SLVA_READ);
221         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CNT,
222             ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
223         if ((smb_error = ichsmb_wait(sc)) == SMB_ENOERR)
224                 *byte = bus_space_read_1(sc->io_bst, sc->io_bsh, ICH_D0);
225         splx(s);
226         DBG("smb_error=%d byte=0x%02x\n", smb_error, (u_char)*byte);
227         return (smb_error);
228 }
229
230 int
231 ichsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
232 {
233         const sc_p sc = device_get_softc(dev);
234         int smb_error;
235         int s;
236
237         DBG("slave=0x%02x cmd=0x%02x byte=0x%02x\n",
238             slave, (u_char)cmd, (u_char)byte);
239         KASSERT(sc->ich_cmd == -1,
240             ("%s: ich_cmd=%d\n", __FUNCTION__ , sc->ich_cmd));
241         s = splhigh();
242         sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BYTE_DATA;
243         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_XMIT_SLVA,
244             (slave << 1) | ICH_XMIT_SLVA_WRITE);
245         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CMD, cmd);
246         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_D0, byte);
247         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CNT,
248             ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
249         smb_error = ichsmb_wait(sc);
250         splx(s);
251         DBG("smb_error=%d\n", smb_error);
252         return (smb_error);
253 }
254
255 int
256 ichsmb_writew(device_t dev, u_char slave, char cmd, short word)
257 {
258         const sc_p sc = device_get_softc(dev);
259         int smb_error;
260         int s;
261
262         DBG("slave=0x%02x cmd=0x%02x word=0x%04x\n",
263             slave, (u_char)cmd, (u_int16_t)word);
264         KASSERT(sc->ich_cmd == -1,
265             ("%s: ich_cmd=%d\n", __FUNCTION__ , sc->ich_cmd));
266         s = splhigh();
267         sc->ich_cmd = ICH_HST_CNT_SMB_CMD_WORD_DATA;
268         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_XMIT_SLVA,
269             (slave << 1) | ICH_XMIT_SLVA_WRITE);
270         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CMD, cmd);
271         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_D0, word & 0xff);
272         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_D1, word >> 8);
273         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CNT,
274             ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
275         smb_error = ichsmb_wait(sc);
276         splx(s);
277         DBG("smb_error=%d\n", smb_error);
278         return (smb_error);
279 }
280
281 int
282 ichsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
283 {
284         const sc_p sc = device_get_softc(dev);
285         int smb_error;
286         int s;
287
288         DBG("slave=0x%02x cmd=0x%02x\n", slave, (u_char)cmd);
289         KASSERT(sc->ich_cmd == -1,
290             ("%s: ich_cmd=%d\n", __FUNCTION__ , sc->ich_cmd));
291         s = splhigh();
292         sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BYTE_DATA;
293         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_XMIT_SLVA,
294             (slave << 1) | ICH_XMIT_SLVA_READ);
295         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CMD, cmd);
296         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CNT,
297             ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
298         if ((smb_error = ichsmb_wait(sc)) == SMB_ENOERR)
299                 *byte = bus_space_read_1(sc->io_bst, sc->io_bsh, ICH_D0);
300         splx(s);
301         DBG("smb_error=%d byte=0x%02x\n", smb_error, (u_char)*byte);
302         return (smb_error);
303 }
304
305 int
306 ichsmb_readw(device_t dev, u_char slave, char cmd, short *word)
307 {
308         const sc_p sc = device_get_softc(dev);
309         int smb_error;
310         int s;
311
312         DBG("slave=0x%02x cmd=0x%02x\n", slave, (u_char)cmd);
313         KASSERT(sc->ich_cmd == -1,
314             ("%s: ich_cmd=%d\n", __FUNCTION__ , sc->ich_cmd));
315         s = splhigh();
316         sc->ich_cmd = ICH_HST_CNT_SMB_CMD_WORD_DATA;
317         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_XMIT_SLVA,
318             (slave << 1) | ICH_XMIT_SLVA_READ);
319         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CMD, cmd);
320         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CNT,
321             ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
322         if ((smb_error = ichsmb_wait(sc)) == SMB_ENOERR) {
323                 *word = (bus_space_read_1(sc->io_bst,
324                         sc->io_bsh, ICH_D0) & 0xff)
325                   | (bus_space_read_1(sc->io_bst,
326                         sc->io_bsh, ICH_D1) << 8);
327         }
328         splx(s);
329         DBG("smb_error=%d word=0x%04x\n", smb_error, (u_int16_t)*word);
330         return (smb_error);
331 }
332
333 int
334 ichsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata)
335 {
336         const sc_p sc = device_get_softc(dev);
337         int smb_error;
338         int s;
339
340         DBG("slave=0x%02x cmd=0x%02x sdata=0x%04x\n",
341             slave, (u_char)cmd, (u_int16_t)sdata);
342         KASSERT(sc->ich_cmd == -1,
343             ("%s: ich_cmd=%d\n", __FUNCTION__ , sc->ich_cmd));
344         s = splhigh();
345         sc->ich_cmd = ICH_HST_CNT_SMB_CMD_PROC_CALL;
346         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_XMIT_SLVA,
347             (slave << 1) | ICH_XMIT_SLVA_WRITE);
348         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CMD, cmd);
349         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_D0, sdata & 0xff);
350         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_D1, sdata >> 8);
351         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CNT,
352             ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
353         if ((smb_error = ichsmb_wait(sc)) == SMB_ENOERR) {
354                 *rdata = (bus_space_read_1(sc->io_bst,
355                         sc->io_bsh, ICH_D0) & 0xff)
356                   | (bus_space_read_1(sc->io_bst,
357                         sc->io_bsh, ICH_D1) << 8);
358         }
359         splx(s);
360         DBG("smb_error=%d rdata=0x%04x\n", smb_error, (u_int16_t)*rdata);
361         return (smb_error);
362 }
363
364 int
365 ichsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
366 {
367         const sc_p sc = device_get_softc(dev);
368         int smb_error;
369         int s;
370
371         DBG("slave=0x%02x cmd=0x%02x count=%d\n", slave, (u_char)cmd, count);
372 #if ICHSMB_DEBUG
373 #define DISP(ch)        (((ch) < 0x20 || (ch) >= 0x7e) ? '.' : (ch))
374         {
375             u_char *p;
376
377             for (p = (u_char *)buf; p - (u_char *)buf < 32; p += 8) {
378                 DBG("%02x: %02x %02x %02x %02x %02x %02x %02x %02x"
379                     "  %c%c%c%c%c%c%c%c", (p - (u_char *)buf),
380                     p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
381                     DISP(p[0]), DISP(p[1]), DISP(p[2]), DISP(p[3]), 
382                     DISP(p[4]), DISP(p[5]), DISP(p[6]), DISP(p[7]));
383             }
384         }
385 #undef DISP
386 #endif
387         KASSERT(sc->ich_cmd == -1,
388             ("%s: ich_cmd=%d\n", __FUNCTION__ , sc->ich_cmd));
389         if (count < 1 || count > 32)
390                 return (EINVAL);
391         bcopy(buf, sc->block_data, count);
392         sc->block_count = count;
393         sc->block_index = 1;
394         sc->block_write = 1;
395
396         s = splhigh();
397         sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BLOCK;
398         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_XMIT_SLVA,
399             (slave << 1) | ICH_XMIT_SLVA_WRITE);
400         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CMD, cmd);
401         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_D0, count);
402         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_BLOCK_DB, buf[0]);
403         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CNT,
404             ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
405         smb_error = ichsmb_wait(sc);
406         splx(s);
407         DBG("smb_error=%d\n", smb_error);
408         return (smb_error);
409 }
410
411 int
412 ichsmb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf)
413 {
414         const sc_p sc = device_get_softc(dev);
415         int smb_error;
416         int s;
417
418         DBG("slave=0x%02x cmd=0x%02x count=%d\n", slave, (u_char)cmd, count);
419         KASSERT(sc->ich_cmd == -1,
420             ("%s: ich_cmd=%d\n", __FUNCTION__ , sc->ich_cmd));
421         if (count < 1 || count > 32)
422                 return (EINVAL);
423         bzero(sc->block_data, sizeof(sc->block_data));
424         sc->block_count = count;
425         sc->block_index = 0;
426         sc->block_write = 0;
427
428         s = splhigh();
429         sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BLOCK;
430         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_XMIT_SLVA,
431             (slave << 1) | ICH_XMIT_SLVA_READ);
432         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CMD, cmd);
433         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_D0, count); /* XXX? */
434         bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_CNT,
435             ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
436         if ((smb_error = ichsmb_wait(sc)) == SMB_ENOERR)
437                 bcopy(sc->block_data, buf, sc->block_count);
438         splx(s);
439         DBG("smb_error=%d\n", smb_error);
440 #if ICHSMB_DEBUG
441 #define DISP(ch)        (((ch) < 0x20 || (ch) >= 0x7e) ? '.' : (ch))
442         {
443             u_char *p;
444
445             for (p = (u_char *)buf; p - (u_char *)buf < 32; p += 8) {
446                 DBG("%02x: %02x %02x %02x %02x %02x %02x %02x %02x"
447                     "  %c%c%c%c%c%c%c%c", (p - (u_char *)buf),
448                     p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
449                     DISP(p[0]), DISP(p[1]), DISP(p[2]), DISP(p[3]), 
450                     DISP(p[4]), DISP(p[5]), DISP(p[6]), DISP(p[7]));
451             }
452         }
453 #undef DISP
454 #endif
455         return (smb_error);
456 }
457
458 /********************************************************************
459                         OTHER FUNCTIONS
460 ********************************************************************/
461
462 /*
463  * This table describes what interrupts we should ever expect to
464  * see after each ICH command, not including the SMBALERT interrupt.
465  */
466 static const u_int8_t ichsmb_state_irqs[] = {
467         /* quick */
468         (ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR),
469         /* byte */
470         (ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR),
471         /* byte data */
472         (ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR),
473         /* word data */
474         (ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR),
475         /* process call */
476         (ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR),
477         /* block */
478         (ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR
479             | ICH_HST_STA_BYTE_DONE_STS),
480         /* i2c read (not used) */
481         (ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR
482             | ICH_HST_STA_BYTE_DONE_STS)
483 };
484
485 /*
486  * Interrupt handler. This handler is bus-independent. Note that our
487  * interrupt may be shared, so we must handle "false" interrupts.
488  */
489 void
490 ichsmb_device_intr(void *cookie)
491 {
492         const sc_p sc = cookie;
493         const device_t dev = sc->dev;
494         const int maxloops = 16;
495         u_int8_t status;
496         u_int8_t ok_bits;
497         int cmd_index;
498         int count;
499         int s;
500
501         s = splhigh();
502         for (count = 0; count < maxloops; count++) {
503
504                 /* Get and reset status bits */
505                 status = bus_space_read_1(sc->io_bst, sc->io_bsh, ICH_HST_STA);
506 #if ICHSMB_DEBUG
507                 if ((status & ~(ICH_HST_STA_INUSE_STS | ICH_HST_STA_HOST_BUSY))
508                     || count > 0) {
509                         DBG("%d stat=0x%02x\n", count, status);
510                 }
511 #endif
512                 status &= ~(ICH_HST_STA_INUSE_STS | ICH_HST_STA_HOST_BUSY);
513                 if (status == 0)
514                         break;
515
516                 /* Check for unexpected interrupt */
517                 ok_bits = ICH_HST_STA_SMBALERT_STS;
518                 cmd_index = sc->ich_cmd >> 2;
519                 if (sc->ich_cmd != -1) {
520                         KASSERT(cmd_index < sizeof(ichsmb_state_irqs),
521                             ("%s: ich_cmd=%d", device_get_nameunit(dev),
522                             sc->ich_cmd));
523                         ok_bits |= ichsmb_state_irqs[cmd_index];
524                 }
525                 if ((status & ~ok_bits) != 0) {
526                         log(LOG_ERR, "%s: irq 0x%02x during %d\n",
527                             device_get_nameunit(dev), status, cmd_index);
528                         bus_space_write_1(sc->io_bst, sc->io_bsh,
529                             ICH_HST_STA, (status & ~ok_bits));
530                         continue;
531                 }
532
533                 /* Handle SMBALERT interrupt */
534                 if (status & ICH_HST_STA_SMBALERT_STS) {
535                         static int smbalert_count = 16;
536                         if (smbalert_count > 0) {
537                                 log(LOG_WARNING, "%s: SMBALERT# rec'd\n",
538                                     device_get_nameunit(dev));
539                                 if (--smbalert_count == 0) {
540                                         log(LOG_WARNING,
541                                             "%s: not logging anymore\n",
542                                             device_get_nameunit(dev));
543                                 }
544                         }
545                 }
546
547                 /* Check for bus error */
548                 if (status & ICH_HST_STA_BUS_ERR) {
549                         sc->smb_error = SMB_ECOLLI;     /* XXX SMB_EBUSERR? */
550                         goto finished;
551                 }
552
553                 /* Check for device error */
554                 if (status & ICH_HST_STA_DEV_ERR) {
555                         sc->smb_error = SMB_ENOACK;     /* or SMB_ETIMEOUT? */
556                         goto finished;
557                 }
558
559                 /* Check for byte completion in block transfer */
560                 if (status & ICH_HST_STA_BYTE_DONE_STS) {
561                         if (sc->block_write) {
562                                 if (sc->block_index < sc->block_count) {
563
564                                         /* Write next byte */
565                                         bus_space_write_1(sc->io_bst,
566                                             sc->io_bsh, ICH_BLOCK_DB,
567                                             sc->block_data[sc->block_index++]);
568                                 }
569                         } else {
570
571                                 /* First interrupt, get the count also */
572                                 if (sc->block_index == 0) {
573                                         sc->block_count = bus_space_read_1(
574                                             sc->io_bst, sc->io_bsh, ICH_D0);
575                                 }
576
577                                 /* Get next byte, if any */
578                                 if (sc->block_index < sc->block_count) {
579
580                                         /* Read next byte */
581                                         sc->block_data[sc->block_index++] =
582                                             bus_space_read_1(sc->io_bst,
583                                               sc->io_bsh, ICH_BLOCK_DB);
584
585                                         /* Set "LAST_BYTE" bit before reading
586                                            the last byte of block data */
587                                         if (sc->block_index
588                                             >= sc->block_count - 1) {
589                                                 bus_space_write_1(sc->io_bst,
590                                                     sc->io_bsh, ICH_HST_CNT,
591                                                     ICH_HST_CNT_LAST_BYTE
592                                                         | ICH_HST_CNT_INTREN
593                                                         | sc->ich_cmd);
594                                         }
595                                 }
596                         }
597                 }
598
599                 /* Check command completion */
600                 if (status & ICH_HST_STA_INTR) {
601                         sc->smb_error = SMB_ENOERR;
602 finished:
603                         sc->ich_cmd = -1;
604                         bus_space_write_1(sc->io_bst, sc->io_bsh,
605                             ICH_HST_STA, status);
606                         wakeup(sc);
607                         break;
608                 }
609
610                 /* Clear status bits and try again */
611                 bus_space_write_1(sc->io_bst, sc->io_bsh, ICH_HST_STA, status);
612         }
613         splx(s);
614
615         /* Too many loops? */
616         if (count == maxloops) {
617                 log(LOG_ERR, "%s: interrupt loop, status=0x%02x\n",
618                     device_get_nameunit(dev),
619                     bus_space_read_1(sc->io_bst, sc->io_bsh, ICH_HST_STA));
620         }
621 }
622
623 /*
624  * Wait for command completion. Assumes splhigh().
625  * Returns an SMB_* error code.
626  */
627 static int
628 ichsmb_wait(sc_p sc)
629 {
630         const device_t dev = sc->dev;
631         int error, smb_error;
632
633         KASSERT(sc->ich_cmd != -1,
634             ("%s: ich_cmd=%d\n", __FUNCTION__ , sc->ich_cmd));
635 sleep:
636         error = tsleep(sc, PCATCH, "ichsmb", hz / 4);
637         DBG("tsleep -> %d\n", error);
638         switch (error) {
639         case ERESTART:
640                 if (sc->ich_cmd != -1)
641                         goto sleep;
642                 /* FALLTHROUGH */
643         case 0:
644                 smb_error = sc->smb_error;
645                 break;
646         case EWOULDBLOCK:
647                 log(LOG_ERR, "%s: device timeout, status=0x%02x\n",
648                     device_get_nameunit(dev),
649                     bus_space_read_1(sc->io_bst, sc->io_bsh, ICH_HST_STA));
650                 sc->ich_cmd = -1;
651                 smb_error = SMB_ETIMEOUT;
652                 break;
653         default:
654                 smb_error = SMB_EABORT;
655                 break;
656         }
657         return (smb_error);
658 }
659
660 /*
661  * Release resources associated with device.
662  */
663 void
664 ichsmb_release_resources(sc_p sc)
665 {
666         const device_t dev = sc->dev;
667
668         if (sc->irq_handle != NULL) {
669                 bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
670                 sc->irq_handle = NULL;
671         }
672         if (sc->irq_res != NULL) {
673                 bus_release_resource(dev,
674                     SYS_RES_IRQ, sc->irq_rid, sc->irq_res);
675                 sc->irq_res = NULL;
676         }
677         if (sc->io_res != NULL) {
678                 bus_release_resource(dev,
679                     SYS_RES_IOPORT, sc->io_rid, sc->io_res);
680                 sc->io_res = NULL;
681         }
682 }
683