Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/acpi Import my experimental ACPI SMBus Control Metho...



details:   https://anonhg.NetBSD.org/src/rev/966984b6c5d9
branches:  trunk
changeset: 751518:966984b6c5d9
user:      pgoyette <pgoyette%NetBSD.org@localhost>
date:      Sat Feb 06 20:10:18 2010 +0000

description:
Import my experimental ACPI SMBus Control Method Interface driver

XXX Should not be enabled in any configuration file until you have
XXX disabled corresponding "native" i2c driver!  Read the acpismbus(4)
XXX man page!

diffstat:

 sys/dev/acpi/files.acpi   |    7 +-
 sys/dev/acpi/smbus_acpi.c |  495 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 501 insertions(+), 1 deletions(-)

diffs (truncated from 517 to 300 lines):

diff -r 8450b2897468 -r 966984b6c5d9 sys/dev/acpi/files.acpi
--- a/sys/dev/acpi/files.acpi   Sat Feb 06 17:48:54 2010 +0000
+++ b/sys/dev/acpi/files.acpi   Sat Feb 06 20:10:18 2010 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.acpi,v 1.65 2010/01/31 11:26:20 jruoho Exp $
+#      $NetBSD: files.acpi,v 1.66 2010/02/06 20:10:18 pgoyette Exp $
 
 include "dev/acpi/acpica/files.acpica"
 
@@ -155,3 +155,8 @@
 device acpiwmi: acpiwmibus
 attach acpiwmi at acpinodebus
 file   dev/acpi/wmi_acpi.c             acpiwmi
+
+# ACPI SMBus controller
+device acpismbus: i2cbus
+attach acpismbus at acpinodebus
+file   dev/acpi/smbus_acpi.c           acpismbus
diff -r 8450b2897468 -r 966984b6c5d9 sys/dev/acpi/smbus_acpi.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/acpi/smbus_acpi.c Sat Feb 06 20:10:18 2010 +0000
@@ -0,0 +1,495 @@
+/* $NetBSD: smbus_acpi.c,v 1.1 2010/02/06 20:10:18 pgoyette Exp $ */
+
+/*-
+ * Copyright (c) 2009 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Paul Goyette
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * ACPI SMBus Controller driver
+ *
+ * See http://smbus.org/specs/smbus_cmi10.pdf for specifications
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: smbus_acpi.c,v 1.1 2010/02/06 20:10:18 pgoyette Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/errno.h>
+#include <sys/ioctl.h>
+#include <sys/syslog.h>
+#include <sys/device.h>
+#include <sys/callout.h>
+#include <sys/proc.h>
+#include <sys/mutex.h>
+
+#include <dev/acpi/acpica.h>
+#include <dev/acpi/acpireg.h>
+#include <dev/acpi/acpivar.h>
+
+#include <dev/i2c/i2cvar.h>
+
+#define _COMPONENT          ACPI_SMBUS_COMPONENT
+ACPI_MODULE_NAME            ("acpi_smbus")
+
+/*
+ * ACPI SMBus CMI protocol codes
+ */
+#define        ACPI_SMBUS_RD_QUICK     0x03
+#define        ACPI_SMBUS_RCV_BYTE     0x05
+#define        ACPI_SMBUS_RD_BYTE      0x07
+#define        ACPI_SMBUS_RD_WORD      0x09
+#define        ACPI_SMBUS_RD_BLOCK     0x0B
+#define        ACPI_SMBUS_WR_QUICK     0x02
+#define        ACPI_SMBUS_SND_BYTE     0x04
+#define        ACPI_SMBUS_WR_BYTE      0x06
+#define        ACPI_SMBUS_WR_WORD      0x08
+#define        ACPI_SMBUS_WR_BLOCK     0x0A
+#define        ACPI_SMBUS_PROCESS_CALL 0x0C
+
+struct acpi_smbus_softc {
+       struct acpi_devnode     *sc_devnode;
+       struct callout          sc_callout;
+       struct i2c_controller   sc_i2c_tag;
+       device_t                sc_dv;
+       kmutex_t                sc_i2c_mutex;
+       int                     sc_poll_alert;
+};
+
+static int     acpi_smbus_match(device_t, cfdata_t, void *);
+static void    acpi_smbus_attach(device_t, device_t, void *);
+static int     acpi_smbus_detach(device_t, int); 
+static int     acpi_smbus_acquire_bus(void *, int);
+static void    acpi_smbus_release_bus(void *, int);
+static int     acpi_smbus_exec(void *, i2c_op_t, i2c_addr_t, const void *,
+                               size_t, void *, size_t, int);
+static void    acpi_smbus_alerts(struct acpi_smbus_softc *);
+static void    acpi_smbus_tick(void *);
+static void    acpi_smbus_notify_handler(ACPI_HANDLE, UINT32, void *);
+
+struct SMB_UDID {
+       uint8_t         dev_cap;
+       uint8_t         vers_rev;
+       uint16_t        vendor;
+       uint16_t        device;
+       uint16_t        interface;
+       uint16_t        subsys_vendor;
+       uint16_t        subsys_device;
+       uint8_t         reserved[4];
+};
+
+struct SMB_DEVICE {
+       uint8_t         slave_addr;
+       uint8_t         reserved;
+       struct SMB_UDID dev_id;
+};
+
+struct SMB_INFO {
+       uint8_t         struct_ver;
+       uint8_t         spec_ver;
+       uint8_t         hw_cap;
+       uint8_t         poll_int;
+       uint8_t         dev_count;
+       struct SMB_DEVICE device[1];
+};
+
+static const char * const smbus_acpi_ids[] = {
+       "SMBUS01",      /* SMBus CMI v1.0 */
+       NULL
+};
+
+static const char * const pcibus_acpi_ids[] = {
+       "PNP0A03",
+       "PNP0A08",
+       NULL
+};
+
+CFATTACH_DECL_NEW(acpismbus, sizeof(struct acpi_smbus_softc),
+    acpi_smbus_match, acpi_smbus_attach, acpi_smbus_detach, NULL);
+
+/*
+ * acpi_smbus_match: autoconf(9) match routine
+ */
+static int
+acpi_smbus_match(device_t parent, cfdata_t match, void *aux)
+{
+       struct acpi_attach_args *aa = aux;
+       int r = 0;
+       ACPI_STATUS rv;
+       ACPI_BUFFER smi_buf;
+       ACPI_OBJECT *e, *p;
+
+       if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
+               return 0;
+
+       if (acpi_match_hid(aa->aa_node->ad_devinfo, smbus_acpi_ids) == 0)
+               return 0;
+
+       /* Ensure that device's CMI version is supported */
+       rv = acpi_eval_struct(aa->aa_node->ad_handle, "_SBI", &smi_buf);
+       if (ACPI_FAILURE(rv))
+               goto done;
+
+       p = smi_buf.Pointer;
+       if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
+           p->Package.Count >= 1) {
+               e = p->Package.Elements;
+               if (e[0].Type == ACPI_TYPE_INTEGER &&
+                   e[0].Integer.Value == 0x10)
+                       r = 1;
+       }
+done:
+       if (smi_buf.Pointer != NULL)
+               ACPI_FREE(smi_buf.Pointer);
+
+       return r;
+}
+
+/*
+ * acpitz_attach: autoconf(9) attach routine
+ */
+static void
+acpi_smbus_attach(device_t parent, device_t self, void *aux)
+{
+       struct acpi_smbus_softc *sc = device_private(self);
+       struct acpi_attach_args *aa = aux;
+       struct i2cbus_attach_args iba;
+       ACPI_STATUS rv;
+       ACPI_HANDLE native_dev, native_bus;
+       ACPI_DEVICE_INFO *native_dev_info, *native_bus_info;
+       ACPI_BUFFER smi_buf;
+       ACPI_OBJECT *e, *p;
+       struct SMB_INFO *info;
+       int pci_bus, pci_dev, pci_func;
+
+       aprint_naive("\n");
+
+       sc->sc_devnode = aa->aa_node;
+       sc->sc_dv = self;
+       sc->sc_poll_alert = 2;
+
+       /* Attach I2C bus */
+       mutex_init(&sc->sc_i2c_mutex, MUTEX_DEFAULT, IPL_NONE);
+       sc->sc_i2c_tag.ic_cookie = sc;
+       sc->sc_i2c_tag.ic_acquire_bus = acpi_smbus_acquire_bus;
+       sc->sc_i2c_tag.ic_release_bus = acpi_smbus_release_bus;
+       sc->sc_i2c_tag.ic_exec = acpi_smbus_exec;
+
+       /* Retrieve polling interval for SMBus Alerts */
+       rv = acpi_eval_struct(aa->aa_node->ad_handle, "_SBI", &smi_buf);
+       if (!ACPI_FAILURE(rv)) {
+               p = smi_buf.Pointer;
+               if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
+                   p->Package.Count >= 2) {
+                       e = p->Package.Elements;
+                       if (e[1].Type == ACPI_TYPE_BUFFER) {
+                               info = (struct SMB_INFO *)(e[1].Buffer.Pointer);
+                               sc->sc_poll_alert = info->poll_int;
+                       }
+               }
+       }
+       if (smi_buf.Pointer != NULL)
+               ACPI_FREE(smi_buf.Pointer);
+
+       /* Install notify handler if possible */
+       rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
+               ACPI_DEVICE_NOTIFY, acpi_smbus_notify_handler, self);
+       if (ACPI_FAILURE(rv)) {
+               aprint_error(": unable to install DEVICE NOTIFY handler: %s\n",
+                   AcpiFormatException(rv));
+               sc->sc_poll_alert = 2;  /* If failed, fall-back to polling */
+       }
+
+       callout_init(&sc->sc_callout, 0);
+       callout_setfunc(&sc->sc_callout, acpi_smbus_tick, self);
+
+       if (!pmf_device_register(self, NULL, NULL))
+               aprint_error(": couldn't establish power handler\n");
+
+       if (sc->sc_poll_alert != 0) {
+               aprint_debug(" alert_poll %d sec", sc->sc_poll_alert);
+               callout_schedule(&sc->sc_callout, sc->sc_poll_alert * hz);
+       }
+       aprint_normal("\n");
+
+       /*
+        * Retrieve and display native controller info
+        */
+       rv = AcpiGetParent(sc->sc_devnode->ad_handle, &native_dev);
+       if (!ACPI_FAILURE(rv))
+               rv = AcpiGetParent(native_dev, &native_bus);
+       if (!ACPI_FAILURE(rv))
+               rv = AcpiGetObjectInfo(native_bus, &native_bus_info);
+       if (!ACPI_FAILURE(rv) &&
+           acpi_match_hid(native_bus_info, pcibus_acpi_ids) != 0) {
+               rv = AcpiGetObjectInfo(native_dev, &native_dev_info);
+               if (!ACPI_FAILURE(rv)) {
+                       pci_bus = native_bus_info->Address;
+                       pci_dev = (native_dev_info->Address >> 16) & 0xffff;
+                       pci_func = native_dev_info->Address & 0xffff;
+                       aprint_debug_dev(self, "Native i2c host controller"
+                           " is on PCI bus %d dev %d func %d\n",
+                           pci_bus, pci_dev, pci_func);
+                       /*
+                        * XXX We really need a mechanism to prevent the
+                        * XXX native controller from attaching
+                        */
+               }
+       }
+
+       memset(&iba, 0, sizeof(iba));
+       iba.iba_tag = &sc->sc_i2c_tag;
+       config_found_ia(self, "i2cbus", &iba, iicbus_print);
+}
+
+static int
+acpi_smbus_detach(device_t self, int flags) 
+{
+       struct acpi_smbus_softc *sc = device_private(self);
+       ACPI_STATUS rv;
+ 
+       pmf_device_deregister(self);
+



Home | Main Index | Thread Index | Old Index