Home | History | Annotate | Line # | Download | only in acpi
smbus_acpi.c revision 1.3
      1 /* $NetBSD: smbus_acpi.c,v 1.3 2010/03/01 13:16:21 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Goyette
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * ACPI SMBus Controller driver
     34  *
     35  * See http://smbus.org/specs/smbus_cmi10.pdf for specifications
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: smbus_acpi.c,v 1.3 2010/03/01 13:16:21 jruoho Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/errno.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/syslog.h>
     47 #include <sys/device.h>
     48 #include <sys/callout.h>
     49 #include <sys/proc.h>
     50 #include <sys/mutex.h>
     51 
     52 #include <dev/acpi/acpica.h>
     53 #include <dev/acpi/acpireg.h>
     54 #include <dev/acpi/acpivar.h>
     55 
     56 #include <dev/i2c/i2cvar.h>
     57 
     58 #define _COMPONENT          ACPI_SMBUS_COMPONENT
     59 ACPI_MODULE_NAME            ("acpi_smbus")
     60 
     61 /*
     62  * ACPI SMBus CMI protocol codes
     63  */
     64 #define	ACPI_SMBUS_RD_QUICK	0x03
     65 #define	ACPI_SMBUS_RCV_BYTE	0x05
     66 #define	ACPI_SMBUS_RD_BYTE	0x07
     67 #define	ACPI_SMBUS_RD_WORD	0x09
     68 #define	ACPI_SMBUS_RD_BLOCK	0x0B
     69 #define	ACPI_SMBUS_WR_QUICK	0x02
     70 #define	ACPI_SMBUS_SND_BYTE	0x04
     71 #define	ACPI_SMBUS_WR_BYTE	0x06
     72 #define	ACPI_SMBUS_WR_WORD	0x08
     73 #define	ACPI_SMBUS_WR_BLOCK	0x0A
     74 #define	ACPI_SMBUS_PROCESS_CALL	0x0C
     75 
     76 struct acpi_smbus_softc {
     77 	struct acpi_devnode 	*sc_devnode;
     78 	struct callout		sc_callout;
     79 	struct i2c_controller	sc_i2c_tag;
     80 	device_t		sc_dv;
     81 	kmutex_t		sc_i2c_mutex;
     82 	int			sc_poll_alert;
     83 };
     84 
     85 static int	acpi_smbus_match(device_t, cfdata_t, void *);
     86 static void	acpi_smbus_attach(device_t, device_t, void *);
     87 static int	acpi_smbus_detach(device_t, int);
     88 static int	acpi_smbus_acquire_bus(void *, int);
     89 static void	acpi_smbus_release_bus(void *, int);
     90 static int	acpi_smbus_exec(void *, i2c_op_t, i2c_addr_t, const void *,
     91 				size_t, void *, size_t, int);
     92 static void	acpi_smbus_alerts(struct acpi_smbus_softc *);
     93 static void	acpi_smbus_tick(void *);
     94 static void	acpi_smbus_notify_handler(ACPI_HANDLE, uint32_t, void *);
     95 
     96 struct SMB_UDID {
     97 	uint8_t		dev_cap;
     98 	uint8_t		vers_rev;
     99 	uint16_t	vendor;
    100 	uint16_t	device;
    101 	uint16_t	interface;
    102 	uint16_t	subsys_vendor;
    103 	uint16_t	subsys_device;
    104 	uint8_t		reserved[4];
    105 };
    106 
    107 struct SMB_DEVICE {
    108 	uint8_t		slave_addr;
    109 	uint8_t		reserved;
    110 	struct SMB_UDID	dev_id;
    111 };
    112 
    113 struct SMB_INFO {
    114 	uint8_t		struct_ver;
    115 	uint8_t		spec_ver;
    116 	uint8_t		hw_cap;
    117 	uint8_t		poll_int;
    118 	uint8_t		dev_count;
    119 	struct SMB_DEVICE device[1];
    120 };
    121 
    122 static const char * const smbus_acpi_ids[] = {
    123 	"SMBUS01",	/* SMBus CMI v1.0 */
    124 	NULL
    125 };
    126 
    127 static const char * const pcibus_acpi_ids[] = {
    128 	"PNP0A03",
    129 	"PNP0A08",
    130 	NULL
    131 };
    132 
    133 CFATTACH_DECL_NEW(acpismbus, sizeof(struct acpi_smbus_softc),
    134     acpi_smbus_match, acpi_smbus_attach, acpi_smbus_detach, NULL);
    135 
    136 /*
    137  * acpi_smbus_match: autoconf(9) match routine
    138  */
    139 static int
    140 acpi_smbus_match(device_t parent, cfdata_t match, void *aux)
    141 {
    142 	struct acpi_attach_args *aa = aux;
    143 	int r = 0;
    144 	ACPI_STATUS rv;
    145 	ACPI_BUFFER smi_buf;
    146 	ACPI_OBJECT *e, *p;
    147 
    148 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    149 		return 0;
    150 
    151 	if (acpi_match_hid(aa->aa_node->ad_devinfo, smbus_acpi_ids) == 0)
    152 		return 0;
    153 
    154 	/* Ensure that device's CMI version is supported */
    155 	rv = acpi_eval_struct(aa->aa_node->ad_handle, "_SBI", &smi_buf);
    156 	if (ACPI_FAILURE(rv))
    157 		goto done;
    158 
    159 	p = smi_buf.Pointer;
    160 	if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
    161 	    p->Package.Count >= 1) {
    162 		e = p->Package.Elements;
    163 		if (e[0].Type == ACPI_TYPE_INTEGER &&
    164 		    e[0].Integer.Value == 0x10)
    165 			r = 1;
    166 	}
    167 done:
    168 	if (smi_buf.Pointer != NULL)
    169 		ACPI_FREE(smi_buf.Pointer);
    170 
    171 	return r;
    172 }
    173 
    174 /*
    175  * acpitz_attach: autoconf(9) attach routine
    176  */
    177 static void
    178 acpi_smbus_attach(device_t parent, device_t self, void *aux)
    179 {
    180 	struct acpi_smbus_softc *sc = device_private(self);
    181 	struct acpi_attach_args *aa = aux;
    182 	struct i2cbus_attach_args iba;
    183 	ACPI_STATUS rv;
    184 	ACPI_HANDLE native_dev, native_bus;
    185 	ACPI_DEVICE_INFO *native_dev_info, *native_bus_info;
    186 	ACPI_BUFFER smi_buf;
    187 	ACPI_OBJECT *e, *p;
    188 	struct SMB_INFO *info;
    189 	int pci_bus, pci_dev, pci_func;
    190 
    191 	aprint_naive("\n");
    192 
    193 	sc->sc_devnode = aa->aa_node;
    194 	sc->sc_dv = self;
    195 	sc->sc_poll_alert = 2;
    196 
    197 	/* Attach I2C bus */
    198 	mutex_init(&sc->sc_i2c_mutex, MUTEX_DEFAULT, IPL_NONE);
    199 	sc->sc_i2c_tag.ic_cookie = sc;
    200 	sc->sc_i2c_tag.ic_acquire_bus = acpi_smbus_acquire_bus;
    201 	sc->sc_i2c_tag.ic_release_bus = acpi_smbus_release_bus;
    202 	sc->sc_i2c_tag.ic_exec = acpi_smbus_exec;
    203 
    204 	/* Retrieve polling interval for SMBus Alerts */
    205 	rv = acpi_eval_struct(aa->aa_node->ad_handle, "_SBI", &smi_buf);
    206 	if (ACPI_SUCCESS(rv)) {
    207 		p = smi_buf.Pointer;
    208 		if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
    209 		    p->Package.Count >= 2) {
    210 			e = p->Package.Elements;
    211 			if (e[1].Type == ACPI_TYPE_BUFFER) {
    212 				info = (struct SMB_INFO *)(e[1].Buffer.Pointer);
    213 				sc->sc_poll_alert = info->poll_int;
    214 			}
    215 		}
    216 	}
    217 	if (smi_buf.Pointer != NULL)
    218 		ACPI_FREE(smi_buf.Pointer);
    219 
    220 	/* Install notify handler if possible */
    221 	rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
    222 		ACPI_DEVICE_NOTIFY, acpi_smbus_notify_handler, self);
    223 	if (ACPI_FAILURE(rv)) {
    224 		aprint_error(": unable to install DEVICE NOTIFY handler: %s\n",
    225 		    AcpiFormatException(rv));
    226 		sc->sc_poll_alert = 2;	/* If failed, fall-back to polling */
    227 	}
    228 
    229 	callout_init(&sc->sc_callout, 0);
    230 	callout_setfunc(&sc->sc_callout, acpi_smbus_tick, self);
    231 
    232 	if (!pmf_device_register(self, NULL, NULL))
    233 		aprint_error(": couldn't establish power handler\n");
    234 
    235 	if (sc->sc_poll_alert != 0) {
    236 		aprint_debug(" alert_poll %d sec", sc->sc_poll_alert);
    237 		callout_schedule(&sc->sc_callout, sc->sc_poll_alert * hz);
    238 	}
    239 	aprint_normal("\n");
    240 
    241 	/*
    242 	 * Retrieve and display native controller info
    243 	 */
    244 	rv = AcpiGetParent(sc->sc_devnode->ad_handle, &native_dev);
    245 	if (ACPI_SUCCESS(rv))
    246 		rv = AcpiGetParent(native_dev, &native_bus);
    247 	if (ACPI_SUCCESS(rv))
    248 		rv = AcpiGetObjectInfo(native_bus, &native_bus_info);
    249 	if (ACPI_SUCCESS(rv) &&
    250 	    acpi_match_hid(native_bus_info, pcibus_acpi_ids) != 0) {
    251 		rv = AcpiGetObjectInfo(native_dev, &native_dev_info);
    252 		if (ACPI_SUCCESS(rv)) {
    253 			pci_bus = native_bus_info->Address;
    254 			pci_dev = ACPI_ADR_PCI_DEV(native_dev_info->Address);
    255 			pci_func = ACPI_ADR_PCI_FUNC(native_dev_info->Address);
    256 			aprint_debug_dev(self, "Native i2c host controller"
    257 			    " is on PCI bus %d dev %d func %d\n",
    258 			    pci_bus, pci_dev, pci_func);
    259 			/*
    260 			 * XXX We really need a mechanism to prevent the
    261 			 * XXX native controller from attaching
    262 			 */
    263 		}
    264 	}
    265 
    266 	memset(&iba, 0, sizeof(iba));
    267 	iba.iba_tag = &sc->sc_i2c_tag;
    268 	config_found_ia(self, "i2cbus", &iba, iicbus_print);
    269 }
    270 
    271 static int
    272 acpi_smbus_detach(device_t self, int flags)
    273 {
    274 	struct acpi_smbus_softc *sc = device_private(self);
    275 	ACPI_STATUS rv;
    276 
    277 	rv = AcpiRemoveNotifyHandler(sc->sc_devnode->ad_handle,
    278 	    ACPI_DEVICE_NOTIFY, acpi_smbus_notify_handler);
    279 
    280 	if (ACPI_FAILURE(rv))
    281 		return EBUSY;
    282 
    283 	pmf_device_deregister(self);
    284 
    285 	callout_halt(&sc->sc_callout, NULL);
    286 	callout_destroy(&sc->sc_callout);
    287 
    288 	mutex_destroy(&sc->sc_i2c_mutex);
    289 
    290 	return 0;
    291 }
    292 
    293 static int
    294 acpi_smbus_acquire_bus(void *cookie, int flags)
    295 {
    296         struct acpi_smbus_softc *sc = cookie;
    297 
    298         mutex_enter(&sc->sc_i2c_mutex);
    299         return 0;
    300 }
    301 
    302 static void
    303 acpi_smbus_release_bus(void *cookie, int flags)
    304 {
    305         struct acpi_smbus_softc *sc = cookie;
    306 
    307         mutex_exit(&sc->sc_i2c_mutex);
    308 }
    309 static int
    310 acpi_smbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
    311 	const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
    312 {
    313         struct acpi_smbus_softc *sc = cookie;
    314 	const uint8_t *c = cmdbuf;
    315 	uint8_t *b = buf, *xb;
    316 	int xlen;
    317 	int r = 0;
    318 	ACPI_BUFFER smbuf;
    319 	ACPI_STATUS rv;
    320 	ACPI_OBJECT_LIST args;
    321 	ACPI_OBJECT arg[5];
    322 	ACPI_OBJECT *p, *e;
    323 
    324 	smbuf.Pointer = NULL;
    325 	smbuf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    326 	args.Pointer = arg;
    327 	arg[0].Type = ACPI_TYPE_INTEGER;	/* Protocol */
    328 	arg[1].Type = ACPI_TYPE_INTEGER;	/* Slave Addr */
    329 	arg[1].Integer.Value = addr;
    330 	arg[2].Type = ACPI_TYPE_INTEGER;	/* Command */
    331 	if (I2C_OP_READ_P(op)) {
    332 		args.Count = 3;
    333 		if (len == 0) {
    334 			arg[2].Integer.Value = 0;
    335 			if (cmdlen == 0)
    336 				arg[0].Integer.Value = ACPI_SMBUS_RD_QUICK;
    337 			else
    338 				arg[0].Integer.Value = ACPI_SMBUS_RCV_BYTE;
    339 		} else
    340 			arg[2].Integer.Value = *c;
    341 		if (len == 1)
    342 			arg[0].Integer.Value = ACPI_SMBUS_RD_BYTE;
    343 		else if (len == 2)
    344 			arg[0].Integer.Value = ACPI_SMBUS_RD_WORD;
    345 		else if (len > 2)
    346 			arg[0].Integer.Value = ACPI_SMBUS_RD_BLOCK;
    347 		rv = AcpiEvaluateObject(sc->sc_devnode->ad_handle, "_SBR",
    348 				    &args, &smbuf);
    349 	} else {
    350 		args.Count = 5;
    351 		arg[3].Type = ACPI_TYPE_INTEGER;	/* Data Len */
    352 		arg[3].Integer.Value = len;
    353 		arg[4].Type = ACPI_TYPE_INTEGER;	/* Data */
    354 		if (len == 0) {
    355 			arg[4].Integer.Value = 0;
    356 			if (cmdlen == 0) {
    357 				arg[2].Integer.Value = 0;
    358 				arg[0].Integer.Value = ACPI_SMBUS_WR_QUICK;
    359 			} else {
    360 				arg[2].Integer.Value = *c;
    361 				arg[0].Integer.Value = ACPI_SMBUS_SND_BYTE;
    362 			}
    363 		} else
    364 			arg[2].Integer.Value = *c;
    365 		if (len == 1) {
    366 			arg[0].Integer.Value = ACPI_SMBUS_WR_BYTE;
    367 			arg[4].Integer.Value = *b;
    368 		} else if (len == 2) {
    369 			arg[0].Integer.Value = ACPI_SMBUS_WR_WORD;
    370 			arg[4].Integer.Value = *b++;
    371 			arg[4].Integer.Value += (*b--) << 8;
    372 		} else if (len > 2) {
    373 			arg[0].Integer.Value = ACPI_SMBUS_WR_BLOCK;
    374 			arg[4].Type = ACPI_TYPE_BUFFER;
    375 			arg[4].Buffer.Pointer = buf;
    376 			arg[4].Buffer.Length = (len < 32?len:32);
    377 		}
    378 		rv = AcpiEvaluateObject(sc->sc_devnode->ad_handle, "_SBW",
    379 				    &args, &smbuf);
    380 	}
    381 	if (ACPI_FAILURE(rv))
    382 		r = 1;
    383 	else {
    384 		p = smbuf.Pointer;
    385 		if (p == NULL || p->Type != ACPI_TYPE_PACKAGE ||
    386 		    p->Package.Count < 1)
    387 			r = 1;
    388 		else {
    389 			e = p->Package.Elements;
    390 			if (e->Type == ACPI_TYPE_INTEGER)
    391 				r = e[0].Integer.Value;
    392 			else
    393 				r = 1;
    394 		}
    395 		if (r != 0)
    396 			r = 1;
    397 
    398 		/* For read operations, copy data to user buffer */
    399 		if (r == 0 && I2C_OP_READ_P(op)) {
    400 			if (p->Package.Count >= 3 &&
    401 			    e[1].Type == ACPI_TYPE_INTEGER) {
    402 				xlen = e[1].Integer.Value;
    403 				if (xlen > len)
    404 					xlen = len;
    405 				if (xlen != 0 &&
    406 				    e[2].Type == ACPI_TYPE_BUFFER) {
    407 					xb = e[2].Buffer.Pointer;
    408 					if (xb != NULL)
    409 						memcpy(b, xb, xlen);
    410 					else
    411 						r = 1;
    412 				} else if (e[2].Type == ACPI_TYPE_INTEGER) {
    413 					if (xlen > 0)
    414 						*b++ = e[2].Integer.Value &
    415 							0xff;
    416 					if (xlen > 1)
    417 						*b = (e[2].Integer.Value >> 8);
    418 				} else
    419 					r = 1;
    420 			} else
    421 				r = 1;
    422 		}
    423 	}
    424 	if (smbuf.Pointer)
    425 		ACPI_FREE(smbuf.Pointer);
    426 
    427 	return r;
    428 }
    429 
    430 /*
    431  * acpi_smbus_alerts
    432  *
    433  * Whether triggered by periodic polling or an AcpiNotify, retrieve
    434  * all pending SMBus device alerts
    435  */
    436 static void
    437 acpi_smbus_alerts(struct acpi_smbus_softc *sc)
    438 {
    439 	int status = 0;
    440 	uint8_t slave_addr;
    441 	ACPI_STATUS rv;
    442 	ACPI_BUFFER alert;
    443 	ACPI_OBJECT *e, *p;
    444 
    445 	do {
    446 		rv = acpi_eval_struct(sc->sc_devnode->ad_handle, "_SBA",
    447 				      &alert);
    448 		if (ACPI_FAILURE(rv)) {
    449 			status = 1;
    450 			goto done;
    451 		}
    452 
    453 		p = alert.Pointer;
    454 		if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
    455 		    p->Package.Count >= 2) {
    456 			e = p->Package.Elements;
    457 			if (e[0].Type == ACPI_TYPE_INTEGER)
    458 				status = e[0].Integer.Value;
    459 			else
    460 				status = 1;
    461 			if (status == 0x0 && e[1].Type == ACPI_TYPE_INTEGER) {
    462 				slave_addr = e[1].Integer.Value;
    463 				aprint_debug_dev(sc->sc_dv, "Alert for 0x%x\n",
    464 						 slave_addr);
    465 				(void)iic_smbus_intr(&sc->sc_i2c_tag);
    466 			}
    467 		}
    468 done:
    469 		if (alert.Pointer != NULL)
    470 			ACPI_FREE(alert.Pointer);
    471 	} while (status == 0);
    472 }
    473 
    474 static void
    475 acpi_smbus_tick(void *opaque)
    476 {
    477 	device_t dv = opaque;
    478 	struct acpi_smbus_softc *sc = device_private(dv);
    479 
    480 	acpi_smbus_alerts(sc);
    481 
    482 	callout_schedule(&sc->sc_callout, sc->sc_poll_alert * hz);
    483 }
    484 
    485 static void
    486 acpi_smbus_notify_handler(ACPI_HANDLE hdl, uint32_t notify, void *opaque)
    487 {
    488 	device_t dv = opaque;
    489 	struct acpi_smbus_softc *sc = device_private(dv);
    490 
    491 	aprint_debug_dev(dv, "received notify message 0x%x\n", notify);
    492 	acpi_smbus_alerts(sc);
    493 }
    494