11195Srgrimes/*-
250472Speter * ichsmb.c
337Srgrimes *
473251Sgshapiro * Author: Archie Cobbs <archie@freebsd.org>
538103Speter * Copyright (c) 2000 Whistle Communications, Inc.
673251Sgshapiro * All rights reserved.
738103Speter *
864618Sgshapiro * Subject to the following obligations and disclaimer of warranty, use and
955230Speter * redistribution of this software, in source or object code forms, with or
1055230Speter * without modifications are expressly permitted by Whistle Communications;
1165532Snectar * provided, however, that:
1255230Speter * 1. Any and all reproductions of the source or object code must include the
1374462Salfred *    copyright notice above and the following disclaimer of warranties; and
1459257Siwasaki * 2. No rights are granted, in any manner or form, to use Whistle
1557954Sshin *    Communications, Inc. trademarks, including the mark "WHISTLE
1667929Sume *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1767929Sume *    such appears in the above copyright notice or in the software.
1870856Sjhb *
1970856Sjhb * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
2055230Speter * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
2155230Speter * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
2255230Speter * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
231734Sjkh * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2417639Swosch * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2517639Swosch * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2637Srgrimes * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2757479Speter * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2857488Speter * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
2957459Smarkm * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
3057459Smarkm * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
3157459Smarkm * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
3260677Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3360677Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3460677Skris * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3560677Skris * OF SUCH DAMAGE.
361773Sjkh */
3748734Siwasaki
38147Srgrimes#include <sys/cdefs.h>
3927487Sasami__FBSDID("$FreeBSD$");
4065168Sasami
4135832Sache/*
4243901Sbrian * Support for the SMBus controller logical device which is part of the
4349110Sbrian * Intel 81801AA (ICH) and 81801AB (ICH0) I/O controller hub chips.
4464598Sgshapiro *
4564598Sgshapiro * This driver assumes that the generic SMBus code will ensure that
4637Srgrimes * at most one process at a time calls into the SMBus methods below.
4717639Swosch */
48263Srgrimes
492779Srgrimes#include <sys/param.h>
508857Srgrimes#include <sys/systm.h>
51993Srgrimes#include <sys/kernel.h>
52263Srgrimes#include <sys/errno.h>
5338103Speter#include <sys/lock.h>
5437Srgrimes#include <sys/module.h>
554487Sphk#include <sys/mutex.h>
566717Sphk#include <sys/syslog.h>
5739590Sjkh#include <sys/bus.h>
5839636Sdima
5939590Sjkh#include <machine/bus.h>
6039590Sjkh#include <sys/rman.h>
615948Sjkh#include <machine/resource.h>
624487Sphk
631759Sjkh#include <dev/smbus/smbconf.h>
649970Sbde
6536902Sguido#include <dev/ichsmb/ichsmb_var.h>
6643832Sjkh#include <dev/ichsmb/ichsmb_reg.h>
6752609Sdillon
6858979Siwasaki/*
6961981Sbrian * Enable debugging by defining ICHSMB_DEBUG to a non-zero value.
709970Sbde */
7151033Sn_hibma#define ICHSMB_DEBUG	0
729970Sbde#if ICHSMB_DEBUG != 0 && defined(__CC_SUPPORTS___FUNC__)
731759Sjkh#define DBG(fmt, args...)	\
7457488Speter	do { printf("%s: " fmt, __func__ , ## args); } while (0)
7557488Speter#else
7617639Swosch#define DBG(fmt, args...)	do { } while (0)
7717645Swosch#endif
7857488Speter
7974194Sdes/*
8073251Sgshapiro * Our child device driver name
8173251Sgshapiro */
8273251Sgshapiro#define DRIVER_SMBUS	"smbus"
8367849Sdougb
8467849Sdougb/*
8567849Sdougb * Internal functions
8657488Speter */
8757488Speterstatic int ichsmb_wait(sc_p sc);
8857488Speter
8957488Speter/********************************************************************
9060677Skris		BUS-INDEPENDENT BUS METHODS
9160677Skris********************************************************************/
9260677Skris
9360677Skris/*
9457071Srwatson * Handle probe-time duties that are independent of the bus
9574194Sdes * our device lives on.
9674194Sdes */
9757488Speterint
9857071Srwatsonichsmb_probe(device_t dev)
991731Sjkh{
1009970Sbde	return (BUS_PROBE_DEFAULT);
10137Srgrimes}
1029970Sbde
10337Srgrimes/*
1049970Sbde * Handle attach-time duties that are independent of the bus
10537Srgrimes * our device lives on.
1069970Sbde */
10737Srgrimesint
10837Srgrimesichsmb_attach(device_t dev)
10937Srgrimes{
11037Srgrimes	const sc_p sc = device_get_softc(dev);
1119970Sbde	int error;
1121731Sjkh
1139970Sbde	/* Create mutex */
1141731Sjkh	mtx_init(&sc->mutex, device_get_nameunit(dev), "ichsmb", MTX_DEF);
1159970Sbde
1166177Samurai	/* Add child: an instance of the "smbus" device */
11749110Sbrian	if ((sc->smb = device_add_child(dev, DRIVER_SMBUS, -1)) == NULL) {
11849110Sbrian		device_printf(dev, "no \"%s\" child found\n", DRIVER_SMBUS);
11930589Sjmb		error = ENXIO;
12064598Sgshapiro		goto fail;
12164629Sgshapiro	}
12264629Sgshapiro
12364629Sgshapiro	/* Clear interrupt conditions */
12464629Sgshapiro	bus_write_1(sc->io_res, ICH_HST_STA, 0xff);
12564629Sgshapiro
1269970Sbde	/* Set up interrupt handler */
12737Srgrimes	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
12863097Speter	    NULL, ichsmb_device_intr, sc, &sc->irq_handle);
129147Srgrimes	if (error != 0) {
13063097Speter		device_printf(dev, "can't setup irq\n");
131147Srgrimes		goto fail;
13263097Speter	}
13337Srgrimes
13463097Speter	/* Attach "smbus" child */
135288Srgrimes	if ((error = bus_generic_attach(dev)) != 0) {
13663097Speter		device_printf(dev, "failed to attach child: %d\n", error);
137147Srgrimes		goto fail;
13813378Sache	}
13950126Sgreen
14050126Sgreen	return (0);
14113378Sache
14217104Spstfail:
14317104Spst	mtx_destroy(&sc->mutex);
14463097Speter	return (error);
145147Srgrimes}
14663097Speter
14737Srgrimes/********************************************************************
14864665Skris			SMBUS METHODS
1491759Sjkh********************************************************************/
1501759Sjkh
1519970Sbdeint
1529970Sbdeichsmb_callback(device_t dev, int index, void *data)
15361888Sasmodai{
1541759Sjkh	int smb_error = 0;
15561888Sasmodai
15637Srgrimes	DBG("index=%d how=%d\n", index, data ? *(int *)data : -1);
157147Srgrimes	switch (index) {
15865884Sache	case SMB_REQUEST_BUS:
15965884Sache		break;
16065884Sache	case SMB_RELEASE_BUS:
16165884Sache		break;
1627129Srgrimes	default:
16365884Sache		smb_error = SMB_EABORT;	/* XXX */
16462416Smarkm		break;
165410Srgrimes	}
16672692Sru	DBG("smb_error=%d\n", smb_error);
1677129Srgrimes	return (smb_error);
16811635Sache}
16911635Sache
17011635Sacheint
17111635Sacheichsmb_quick(device_t dev, u_char slave, int how)
17211635Sache{
17311635Sache	const sc_p sc = device_get_softc(dev);
1747129Srgrimes	int smb_error;
17511635Sache
17611635Sache	DBG("slave=0x%02x how=%d\n", slave, how);
17711635Sache	KASSERT(sc->ich_cmd == -1,
17811635Sache	    ("%s: ich_cmd=%d\n", __func__ , sc->ich_cmd));
17911635Sache	switch (how) {
18011635Sache	case SMB_QREAD:
18111635Sache	case SMB_QWRITE:
18211635Sache		mtx_lock(&sc->mutex);
18311635Sache		sc->ich_cmd = ICH_HST_CNT_SMB_CMD_QUICK;
18411635Sache		bus_write_1(sc->io_res, ICH_XMIT_SLVA,
185147Srgrimes		    slave | (how == SMB_QREAD ?
18648185Ssheldonh	    		ICH_XMIT_SLVA_READ : ICH_XMIT_SLVA_WRITE));
18748185Ssheldonh		bus_write_1(sc->io_res, ICH_HST_CNT,
18848185Ssheldonh		    ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
18948185Ssheldonh		smb_error = ichsmb_wait(sc);
19048185Ssheldonh		mtx_unlock(&sc->mutex);
19152609Sdillon		break;
19252609Sdillon	default:
19358979Siwasaki		smb_error = SMB_ENOTSUPP;
19458979Siwasaki	}
19562006Snbm	DBG("smb_error=%d\n", smb_error);
19661981Sbrian	return (smb_error);
19748185Ssheldonh}
19848185Ssheldonh
19937Srgrimesint
200ichsmb_sendb(device_t dev, u_char slave, char byte)
201{
202	const sc_p sc = device_get_softc(dev);
203	int smb_error;
204
205	DBG("slave=0x%02x byte=0x%02x\n", slave, (u_char)byte);
206	KASSERT(sc->ich_cmd == -1,
207	    ("%s: ich_cmd=%d\n", __func__ , sc->ich_cmd));
208	mtx_lock(&sc->mutex);
209	sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BYTE;
210	bus_write_1(sc->io_res, ICH_XMIT_SLVA,
211	    slave | ICH_XMIT_SLVA_WRITE);
212	bus_write_1(sc->io_res, ICH_HST_CMD, byte);
213	bus_write_1(sc->io_res, ICH_HST_CNT,
214	    ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
215	smb_error = ichsmb_wait(sc);
216	mtx_unlock(&sc->mutex);
217	DBG("smb_error=%d\n", smb_error);
218	return (smb_error);
219}
220
221int
222ichsmb_recvb(device_t dev, u_char slave, char *byte)
223{
224	const sc_p sc = device_get_softc(dev);
225	int smb_error;
226
227	DBG("slave=0x%02x\n", slave);
228	KASSERT(sc->ich_cmd == -1,
229	    ("%s: ich_cmd=%d\n", __func__ , sc->ich_cmd));
230	mtx_lock(&sc->mutex);
231	sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BYTE;
232	bus_write_1(sc->io_res, ICH_XMIT_SLVA,
233	    slave | ICH_XMIT_SLVA_READ);
234	bus_write_1(sc->io_res, ICH_HST_CNT,
235	    ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
236	if ((smb_error = ichsmb_wait(sc)) == SMB_ENOERR)
237		*byte = bus_read_1(sc->io_res, ICH_D0);
238	mtx_unlock(&sc->mutex);
239	DBG("smb_error=%d byte=0x%02x\n", smb_error, (u_char)*byte);
240	return (smb_error);
241}
242
243int
244ichsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
245{
246	const sc_p sc = device_get_softc(dev);
247	int smb_error;
248
249	DBG("slave=0x%02x cmd=0x%02x byte=0x%02x\n",
250	    slave, (u_char)cmd, (u_char)byte);
251	KASSERT(sc->ich_cmd == -1,
252	    ("%s: ich_cmd=%d\n", __func__ , sc->ich_cmd));
253	mtx_lock(&sc->mutex);
254	sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BYTE_DATA;
255	bus_write_1(sc->io_res, ICH_XMIT_SLVA,
256	    slave | ICH_XMIT_SLVA_WRITE);
257	bus_write_1(sc->io_res, ICH_HST_CMD, cmd);
258	bus_write_1(sc->io_res, ICH_D0, byte);
259	bus_write_1(sc->io_res, ICH_HST_CNT,
260	    ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
261	smb_error = ichsmb_wait(sc);
262	mtx_unlock(&sc->mutex);
263	DBG("smb_error=%d\n", smb_error);
264	return (smb_error);
265}
266
267int
268ichsmb_writew(device_t dev, u_char slave, char cmd, short word)
269{
270	const sc_p sc = device_get_softc(dev);
271	int smb_error;
272
273	DBG("slave=0x%02x cmd=0x%02x word=0x%04x\n",
274	    slave, (u_char)cmd, (u_int16_t)word);
275	KASSERT(sc->ich_cmd == -1,
276	    ("%s: ich_cmd=%d\n", __func__ , sc->ich_cmd));
277	mtx_lock(&sc->mutex);
278	sc->ich_cmd = ICH_HST_CNT_SMB_CMD_WORD_DATA;
279	bus_write_1(sc->io_res, ICH_XMIT_SLVA,
280	    slave | ICH_XMIT_SLVA_WRITE);
281	bus_write_1(sc->io_res, ICH_HST_CMD, cmd);
282	bus_write_1(sc->io_res, ICH_D0, word & 0xff);
283	bus_write_1(sc->io_res, ICH_D1, word >> 8);
284	bus_write_1(sc->io_res, ICH_HST_CNT,
285	    ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
286	smb_error = ichsmb_wait(sc);
287	mtx_unlock(&sc->mutex);
288	DBG("smb_error=%d\n", smb_error);
289	return (smb_error);
290}
291
292int
293ichsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
294{
295	const sc_p sc = device_get_softc(dev);
296	int smb_error;
297
298	DBG("slave=0x%02x cmd=0x%02x\n", slave, (u_char)cmd);
299	KASSERT(sc->ich_cmd == -1,
300	    ("%s: ich_cmd=%d\n", __func__ , sc->ich_cmd));
301	mtx_lock(&sc->mutex);
302	sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BYTE_DATA;
303	bus_write_1(sc->io_res, ICH_XMIT_SLVA,
304	    slave | ICH_XMIT_SLVA_READ);
305	bus_write_1(sc->io_res, ICH_HST_CMD, cmd);
306	bus_write_1(sc->io_res, ICH_HST_CNT,
307	    ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
308	if ((smb_error = ichsmb_wait(sc)) == SMB_ENOERR)
309		*byte = bus_read_1(sc->io_res, ICH_D0);
310	mtx_unlock(&sc->mutex);
311	DBG("smb_error=%d byte=0x%02x\n", smb_error, (u_char)*byte);
312	return (smb_error);
313}
314
315int
316ichsmb_readw(device_t dev, u_char slave, char cmd, short *word)
317{
318	const sc_p sc = device_get_softc(dev);
319	int smb_error;
320
321	DBG("slave=0x%02x cmd=0x%02x\n", slave, (u_char)cmd);
322	KASSERT(sc->ich_cmd == -1,
323	    ("%s: ich_cmd=%d\n", __func__ , sc->ich_cmd));
324	mtx_lock(&sc->mutex);
325	sc->ich_cmd = ICH_HST_CNT_SMB_CMD_WORD_DATA;
326	bus_write_1(sc->io_res, ICH_XMIT_SLVA,
327	    slave | ICH_XMIT_SLVA_READ);
328	bus_write_1(sc->io_res, ICH_HST_CMD, cmd);
329	bus_write_1(sc->io_res, ICH_HST_CNT,
330	    ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
331	if ((smb_error = ichsmb_wait(sc)) == SMB_ENOERR) {
332		*word = (bus_read_1(sc->io_res,
333			ICH_D0) & 0xff)
334		  | (bus_read_1(sc->io_res,
335			ICH_D1) << 8);
336	}
337	mtx_unlock(&sc->mutex);
338	DBG("smb_error=%d word=0x%04x\n", smb_error, (u_int16_t)*word);
339	return (smb_error);
340}
341
342int
343ichsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata)
344{
345	const sc_p sc = device_get_softc(dev);
346	int smb_error;
347
348	DBG("slave=0x%02x cmd=0x%02x sdata=0x%04x\n",
349	    slave, (u_char)cmd, (u_int16_t)sdata);
350	KASSERT(sc->ich_cmd == -1,
351	    ("%s: ich_cmd=%d\n", __func__ , sc->ich_cmd));
352	mtx_lock(&sc->mutex);
353	sc->ich_cmd = ICH_HST_CNT_SMB_CMD_PROC_CALL;
354	bus_write_1(sc->io_res, ICH_XMIT_SLVA,
355	    slave | ICH_XMIT_SLVA_WRITE);
356	bus_write_1(sc->io_res, ICH_HST_CMD, cmd);
357	bus_write_1(sc->io_res, ICH_D0, sdata & 0xff);
358	bus_write_1(sc->io_res, ICH_D1, sdata >> 8);
359	bus_write_1(sc->io_res, ICH_HST_CNT,
360	    ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
361	if ((smb_error = ichsmb_wait(sc)) == SMB_ENOERR) {
362		*rdata = (bus_read_1(sc->io_res,
363			ICH_D0) & 0xff)
364		  | (bus_read_1(sc->io_res,
365			ICH_D1) << 8);
366	}
367	mtx_unlock(&sc->mutex);
368	DBG("smb_error=%d rdata=0x%04x\n", smb_error, (u_int16_t)*rdata);
369	return (smb_error);
370}
371
372int
373ichsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
374{
375	const sc_p sc = device_get_softc(dev);
376	int smb_error;
377
378	DBG("slave=0x%02x cmd=0x%02x count=%d\n", slave, (u_char)cmd, count);
379#if ICHSMB_DEBUG
380#define DISP(ch)	(((ch) < 0x20 || (ch) >= 0x7e) ? '.' : (ch))
381	{
382	    u_char *p;
383
384	    for (p = (u_char *)buf; p - (u_char *)buf < 32; p += 8) {
385		DBG("%02x: %02x %02x %02x %02x %02x %02x %02x %02x"
386		    "  %c%c%c%c%c%c%c%c", (p - (u_char *)buf),
387		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
388		    DISP(p[0]), DISP(p[1]), DISP(p[2]), DISP(p[3]),
389		    DISP(p[4]), DISP(p[5]), DISP(p[6]), DISP(p[7]));
390	    }
391	}
392#undef DISP
393#endif
394	KASSERT(sc->ich_cmd == -1,
395	    ("%s: ich_cmd=%d\n", __func__ , sc->ich_cmd));
396	if (count < 1 || count > 32)
397		return (SMB_EINVAL);
398	bcopy(buf, sc->block_data, count);
399	sc->block_count = count;
400	sc->block_index = 1;
401	sc->block_write = 1;
402
403	mtx_lock(&sc->mutex);
404	sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BLOCK;
405	bus_write_1(sc->io_res, ICH_XMIT_SLVA,
406	    slave | ICH_XMIT_SLVA_WRITE);
407	bus_write_1(sc->io_res, ICH_HST_CMD, cmd);
408	bus_write_1(sc->io_res, ICH_D0, count);
409	bus_write_1(sc->io_res, ICH_BLOCK_DB, buf[0]);
410	bus_write_1(sc->io_res, ICH_HST_CNT,
411	    ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
412	smb_error = ichsmb_wait(sc);
413	mtx_unlock(&sc->mutex);
414	DBG("smb_error=%d\n", smb_error);
415	return (smb_error);
416}
417
418int
419ichsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
420{
421	const sc_p sc = device_get_softc(dev);
422	int smb_error;
423
424	DBG("slave=0x%02x cmd=0x%02x count=%d\n", slave, (u_char)cmd, count);
425	KASSERT(sc->ich_cmd == -1,
426	    ("%s: ich_cmd=%d\n", __func__ , sc->ich_cmd));
427	if (*count < 1 || *count > 32)
428		return (SMB_EINVAL);
429	bzero(sc->block_data, sizeof(sc->block_data));
430	sc->block_count = 0;
431	sc->block_index = 0;
432	sc->block_write = 0;
433
434	mtx_lock(&sc->mutex);
435	sc->ich_cmd = ICH_HST_CNT_SMB_CMD_BLOCK;
436	bus_write_1(sc->io_res, ICH_XMIT_SLVA,
437	    slave | ICH_XMIT_SLVA_READ);
438	bus_write_1(sc->io_res, ICH_HST_CMD, cmd);
439	bus_write_1(sc->io_res, ICH_D0, *count); /* XXX? */
440	bus_write_1(sc->io_res, ICH_HST_CNT,
441	    ICH_HST_CNT_START | ICH_HST_CNT_INTREN | sc->ich_cmd);
442	if ((smb_error = ichsmb_wait(sc)) == SMB_ENOERR) {
443		bcopy(sc->block_data, buf, min(sc->block_count, *count));
444		*count = sc->block_count;
445	}
446	mtx_unlock(&sc->mutex);
447	DBG("smb_error=%d\n", smb_error);
448#if ICHSMB_DEBUG
449#define DISP(ch)	(((ch) < 0x20 || (ch) >= 0x7e) ? '.' : (ch))
450	{
451	    u_char *p;
452
453	    for (p = (u_char *)buf; p - (u_char *)buf < 32; p += 8) {
454		DBG("%02x: %02x %02x %02x %02x %02x %02x %02x %02x"
455		    "  %c%c%c%c%c%c%c%c", (p - (u_char *)buf),
456		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
457		    DISP(p[0]), DISP(p[1]), DISP(p[2]), DISP(p[3]),
458		    DISP(p[4]), DISP(p[5]), DISP(p[6]), DISP(p[7]));
459	    }
460	}
461#undef DISP
462#endif
463	return (smb_error);
464}
465
466/********************************************************************
467			OTHER FUNCTIONS
468********************************************************************/
469
470/*
471 * This table describes what interrupts we should ever expect to
472 * see after each ICH command, not including the SMBALERT interrupt.
473 */
474static const u_int8_t ichsmb_state_irqs[] = {
475	/* quick */
476	(ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR),
477	/* byte */
478	(ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR),
479	/* byte data */
480	(ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR),
481	/* word data */
482	(ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR),
483	/* process call */
484	(ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR),
485	/* block */
486	(ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR
487	    | ICH_HST_STA_BYTE_DONE_STS),
488	/* i2c read (not used) */
489	(ICH_HST_STA_BUS_ERR | ICH_HST_STA_DEV_ERR | ICH_HST_STA_INTR
490	    | ICH_HST_STA_BYTE_DONE_STS)
491};
492
493/*
494 * Interrupt handler. This handler is bus-independent. Note that our
495 * interrupt may be shared, so we must handle "false" interrupts.
496 */
497void
498ichsmb_device_intr(void *cookie)
499{
500	const sc_p sc = cookie;
501	const device_t dev = sc->dev;
502	const int maxloops = 16;
503	u_int8_t status;
504	u_int8_t ok_bits;
505	int cmd_index;
506        int count;
507
508	mtx_lock(&sc->mutex);
509	for (count = 0; count < maxloops; count++) {
510
511		/* Get and reset status bits */
512		status = bus_read_1(sc->io_res, ICH_HST_STA);
513#if ICHSMB_DEBUG
514		if ((status & ~(ICH_HST_STA_INUSE_STS | ICH_HST_STA_HOST_BUSY))
515		    || count > 0) {
516			DBG("%d stat=0x%02x\n", count, status);
517		}
518#endif
519		status &= ~(ICH_HST_STA_INUSE_STS | ICH_HST_STA_HOST_BUSY);
520		if (status == 0)
521			break;
522
523		/* Check for unexpected interrupt */
524		ok_bits = ICH_HST_STA_SMBALERT_STS;
525		cmd_index = sc->ich_cmd >> 2;
526		if (sc->ich_cmd != -1) {
527			KASSERT(cmd_index < sizeof(ichsmb_state_irqs),
528			    ("%s: ich_cmd=%d", device_get_nameunit(dev),
529			    sc->ich_cmd));
530			ok_bits |= ichsmb_state_irqs[cmd_index];
531		}
532		if ((status & ~ok_bits) != 0) {
533			device_printf(dev, "irq 0x%02x during %d\n", status,
534			    cmd_index);
535			bus_write_1(sc->io_res,
536			    ICH_HST_STA, (status & ~ok_bits));
537			continue;
538		}
539
540		/* Handle SMBALERT interrupt */
541		if (status & ICH_HST_STA_SMBALERT_STS) {
542			static int smbalert_count = 16;
543			if (smbalert_count > 0) {
544				device_printf(dev, "SMBALERT# rec'd\n");
545				if (--smbalert_count == 0) {
546					device_printf(dev,
547					    "not logging anymore\n");
548				}
549			}
550		}
551
552		/* Check for bus error */
553		if (status & ICH_HST_STA_BUS_ERR) {
554			sc->smb_error = SMB_ECOLLI;	/* XXX SMB_EBUSERR? */
555			goto finished;
556		}
557
558		/* Check for device error */
559		if (status & ICH_HST_STA_DEV_ERR) {
560			sc->smb_error = SMB_ENOACK;	/* or SMB_ETIMEOUT? */
561			goto finished;
562		}
563
564		/* Check for byte completion in block transfer */
565		if (status & ICH_HST_STA_BYTE_DONE_STS) {
566			if (sc->block_write) {
567				if (sc->block_index < sc->block_count) {
568
569					/* Write next byte */
570					bus_write_1(sc->io_res,
571					    ICH_BLOCK_DB,
572					    sc->block_data[sc->block_index++]);
573				}
574			} else {
575
576				/* First interrupt, get the count also */
577				if (sc->block_index == 0) {
578					sc->block_count = bus_read_1(
579					    sc->io_res, ICH_D0);
580				}
581
582				/* Get next byte, if any */
583				if (sc->block_index < sc->block_count) {
584
585					/* Read next byte */
586					sc->block_data[sc->block_index++] =
587					    bus_read_1(sc->io_res,
588					      ICH_BLOCK_DB);
589
590					/* Set "LAST_BYTE" bit before reading
591					   the last byte of block data */
592					if (sc->block_index
593					    >= sc->block_count - 1) {
594						bus_write_1(sc->io_res,
595						    ICH_HST_CNT,
596						    ICH_HST_CNT_LAST_BYTE
597							| ICH_HST_CNT_INTREN
598							| sc->ich_cmd);
599					}
600				}
601			}
602		}
603
604		/* Check command completion */
605		if (status & ICH_HST_STA_INTR) {
606			sc->smb_error = SMB_ENOERR;
607finished:
608			sc->ich_cmd = -1;
609			bus_write_1(sc->io_res,
610			    ICH_HST_STA, status);
611			wakeup(sc);
612			break;
613		}
614
615		/* Clear status bits and try again */
616		bus_write_1(sc->io_res, ICH_HST_STA, status);
617	}
618	mtx_unlock(&sc->mutex);
619
620	/* Too many loops? */
621	if (count == maxloops) {
622		device_printf(dev, "interrupt loop, status=0x%02x\n",
623		    bus_read_1(sc->io_res, ICH_HST_STA));
624	}
625}
626
627/*
628 * Wait for command completion. Assumes mutex is held.
629 * Returns an SMB_* error code.
630 */
631static int
632ichsmb_wait(sc_p sc)
633{
634	const device_t dev = sc->dev;
635	int error, smb_error;
636
637	KASSERT(sc->ich_cmd != -1,
638	    ("%s: ich_cmd=%d\n", __func__ , sc->ich_cmd));
639	mtx_assert(&sc->mutex, MA_OWNED);
640	error = msleep(sc, &sc->mutex, PZERO, "ichsmb", hz / 4);
641	DBG("msleep -> %d\n", error);
642	switch (error) {
643	case 0:
644		smb_error = sc->smb_error;
645		break;
646	case EWOULDBLOCK:
647		device_printf(dev, "device timeout, status=0x%02x\n",
648		    bus_read_1(sc->io_res, ICH_HST_STA));
649		sc->ich_cmd = -1;
650		smb_error = SMB_ETIMEOUT;
651		break;
652	default:
653		smb_error = SMB_EABORT;
654		break;
655	}
656	return (smb_error);
657}
658
659/*
660 * Release resources associated with device.
661 */
662void
663ichsmb_release_resources(sc_p sc)
664{
665	const device_t dev = sc->dev;
666
667	if (sc->irq_handle != NULL) {
668		bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
669		sc->irq_handle = NULL;
670	}
671	if (sc->irq_res != NULL) {
672		bus_release_resource(dev,
673		    SYS_RES_IRQ, sc->irq_rid, sc->irq_res);
674		sc->irq_res = NULL;
675	}
676	if (sc->io_res != NULL) {
677		bus_release_resource(dev,
678		    SYS_RES_IOPORT, sc->io_rid, sc->io_res);
679		sc->io_res = NULL;
680	}
681}
682
683int
684ichsmb_detach(device_t dev)
685{
686	const sc_p sc = device_get_softc(dev);
687	int error;
688
689	error = bus_generic_detach(dev);
690	if (error)
691		return (error);
692	device_delete_child(dev, sc->smb);
693	ichsmb_release_resources(sc);
694	mtx_destroy(&sc->mutex);
695
696	return 0;
697}
698
699DRIVER_MODULE(smbus, ichsmb, smbus_driver, smbus_devclass, 0, 0);
700