smbus_acpi.c revision 1.11 1 /* $NetBSD: smbus_acpi.c,v 1.11 2010/07/28 16:26:56 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.11 2010/07/28 16:26:56 jruoho Exp $");
40
41 #include <sys/param.h>
42 #include <sys/device.h>
43 #include <sys/callout.h>
44 #include <sys/kernel.h>
45 #include <sys/mutex.h>
46 #include <sys/systm.h>
47
48 #include <dev/acpi/acpireg.h>
49 #include <dev/acpi/acpivar.h>
50
51 #include <dev/i2c/i2cvar.h>
52
53 #define _COMPONENT ACPI_BUS_COMPONENT
54 ACPI_MODULE_NAME ("smbus_acpi")
55
56 /*
57 * ACPI SMBus CMI protocol codes
58 */
59 #define ACPI_SMBUS_RD_QUICK 0x03
60 #define ACPI_SMBUS_RCV_BYTE 0x05
61 #define ACPI_SMBUS_RD_BYTE 0x07
62 #define ACPI_SMBUS_RD_WORD 0x09
63 #define ACPI_SMBUS_RD_BLOCK 0x0B
64 #define ACPI_SMBUS_WR_QUICK 0x02
65 #define ACPI_SMBUS_SND_BYTE 0x04
66 #define ACPI_SMBUS_WR_BYTE 0x06
67 #define ACPI_SMBUS_WR_WORD 0x08
68 #define ACPI_SMBUS_WR_BLOCK 0x0A
69 #define ACPI_SMBUS_PROCESS_CALL 0x0C
70
71 struct acpi_smbus_softc {
72 struct acpi_devnode *sc_devnode;
73 struct callout sc_callout;
74 struct i2c_controller sc_i2c_tag;
75 device_t sc_dv;
76 kmutex_t sc_i2c_mutex;
77 int sc_poll_alert;
78 };
79
80 static int acpi_smbus_match(device_t, cfdata_t, void *);
81 static void acpi_smbus_attach(device_t, device_t, void *);
82 static int acpi_smbus_detach(device_t, int);
83 static int acpi_smbus_acquire_bus(void *, int);
84 static void acpi_smbus_release_bus(void *, int);
85 static int acpi_smbus_exec(void *, i2c_op_t, i2c_addr_t, const void *,
86 size_t, void *, size_t, int);
87 static void acpi_smbus_alerts(struct acpi_smbus_softc *);
88 static void acpi_smbus_tick(void *);
89 static void acpi_smbus_notify_handler(ACPI_HANDLE, uint32_t, void *);
90
91 struct SMB_UDID {
92 uint8_t dev_cap;
93 uint8_t vers_rev;
94 uint16_t vendor;
95 uint16_t device;
96 uint16_t interface;
97 uint16_t subsys_vendor;
98 uint16_t subsys_device;
99 uint8_t reserved[4];
100 };
101
102 struct SMB_DEVICE {
103 uint8_t slave_addr;
104 uint8_t reserved;
105 struct SMB_UDID dev_id;
106 };
107
108 struct SMB_INFO {
109 uint8_t struct_ver;
110 uint8_t spec_ver;
111 uint8_t hw_cap;
112 uint8_t poll_int;
113 uint8_t dev_count;
114 struct SMB_DEVICE device[1];
115 };
116
117 static const char * const smbus_acpi_ids[] = {
118 "SMBUS01", /* SMBus CMI v1.0 */
119 NULL
120 };
121
122 static const char * const pcibus_acpi_ids[] = {
123 "PNP0A03",
124 "PNP0A08",
125 NULL
126 };
127
128 CFATTACH_DECL_NEW(acpismbus, sizeof(struct acpi_smbus_softc),
129 acpi_smbus_match, acpi_smbus_attach, acpi_smbus_detach, NULL);
130
131 /*
132 * acpi_smbus_match: autoconf(9) match routine
133 */
134 static int
135 acpi_smbus_match(device_t parent, cfdata_t match, void *aux)
136 {
137 struct acpi_attach_args *aa = aux;
138 int r = 0;
139 ACPI_STATUS rv;
140 ACPI_BUFFER smi_buf;
141 ACPI_OBJECT *e, *p;
142
143 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
144 return 0;
145
146 if (acpi_match_hid(aa->aa_node->ad_devinfo, smbus_acpi_ids) == 0)
147 return 0;
148
149 /* Ensure that device's CMI version is supported */
150 rv = acpi_eval_struct(aa->aa_node->ad_handle, "_SBI", &smi_buf);
151 if (ACPI_FAILURE(rv))
152 goto done;
153
154 p = smi_buf.Pointer;
155 if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
156 p->Package.Count >= 1) {
157 e = p->Package.Elements;
158 if (e[0].Type == ACPI_TYPE_INTEGER &&
159 e[0].Integer.Value == 0x10)
160 r = 1;
161 }
162 done:
163 if (smi_buf.Pointer != NULL)
164 ACPI_FREE(smi_buf.Pointer);
165
166 return r;
167 }
168
169 /*
170 * acpitz_attach: autoconf(9) attach routine
171 */
172 static void
173 acpi_smbus_attach(device_t parent, device_t self, void *aux)
174 {
175 struct acpi_smbus_softc *sc = device_private(self);
176 struct acpi_attach_args *aa = aux;
177 struct i2cbus_attach_args iba;
178 ACPI_STATUS rv;
179 ACPI_BUFFER smi_buf;
180 ACPI_OBJECT *e, *p;
181 struct SMB_INFO *info;
182
183 aprint_naive("\n");
184
185 sc->sc_devnode = aa->aa_node;
186 sc->sc_dv = self;
187 sc->sc_poll_alert = 2;
188
189 /* Attach I2C bus */
190 mutex_init(&sc->sc_i2c_mutex, MUTEX_DEFAULT, IPL_NONE);
191 sc->sc_i2c_tag.ic_cookie = sc;
192 sc->sc_i2c_tag.ic_acquire_bus = acpi_smbus_acquire_bus;
193 sc->sc_i2c_tag.ic_release_bus = acpi_smbus_release_bus;
194 sc->sc_i2c_tag.ic_exec = acpi_smbus_exec;
195
196 /* Retrieve polling interval for SMBus Alerts */
197 rv = acpi_eval_struct(aa->aa_node->ad_handle, "_SBI", &smi_buf);
198 if (ACPI_SUCCESS(rv)) {
199 p = smi_buf.Pointer;
200 if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
201 p->Package.Count >= 2) {
202 e = p->Package.Elements;
203 if (e[1].Type == ACPI_TYPE_BUFFER) {
204 info = (struct SMB_INFO *)(e[1].Buffer.Pointer);
205 sc->sc_poll_alert = info->poll_int;
206 }
207 }
208 }
209 if (smi_buf.Pointer != NULL)
210 ACPI_FREE(smi_buf.Pointer);
211
212 /* If failed, fall-back to polling. */
213 if (acpi_register_notify(sc->sc_devnode,
214 acpi_smbus_notify_handler) != true)
215 sc->sc_poll_alert = 2;
216
217 callout_init(&sc->sc_callout, 0);
218 callout_setfunc(&sc->sc_callout, acpi_smbus_tick, self);
219
220 if (!pmf_device_register(self, NULL, NULL))
221 aprint_error(": couldn't establish power handler\n");
222
223 if (sc->sc_poll_alert != 0) {
224 aprint_debug(" alert_poll %d sec", sc->sc_poll_alert);
225 callout_schedule(&sc->sc_callout, sc->sc_poll_alert * hz);
226 }
227 aprint_normal("\n");
228
229 memset(&iba, 0, sizeof(iba));
230 iba.iba_tag = &sc->sc_i2c_tag;
231 config_found_ia(self, "i2cbus", &iba, iicbus_print);
232 }
233
234 static int
235 acpi_smbus_detach(device_t self, int flags)
236 {
237 struct acpi_smbus_softc *sc = device_private(self);
238
239 pmf_device_deregister(self);
240 acpi_deregister_notify(sc->sc_devnode);
241
242 callout_halt(&sc->sc_callout, NULL);
243 callout_destroy(&sc->sc_callout);
244
245 mutex_destroy(&sc->sc_i2c_mutex);
246
247 return 0;
248 }
249
250 static int
251 acpi_smbus_acquire_bus(void *cookie, int flags)
252 {
253 struct acpi_smbus_softc *sc = cookie;
254
255 mutex_enter(&sc->sc_i2c_mutex);
256 return 0;
257 }
258
259 static void
260 acpi_smbus_release_bus(void *cookie, int flags)
261 {
262 struct acpi_smbus_softc *sc = cookie;
263
264 mutex_exit(&sc->sc_i2c_mutex);
265 }
266 static int
267 acpi_smbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
268 const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
269 {
270 struct acpi_smbus_softc *sc = cookie;
271 const uint8_t *c = cmdbuf;
272 uint8_t *b = buf, *xb;
273 int xlen;
274 int r = 0;
275 ACPI_BUFFER smbuf;
276 ACPI_STATUS rv;
277 ACPI_OBJECT_LIST args;
278 ACPI_OBJECT arg[5];
279 ACPI_OBJECT *p, *e;
280
281 smbuf.Pointer = NULL;
282 smbuf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
283 args.Pointer = arg;
284 arg[0].Type = ACPI_TYPE_INTEGER; /* Protocol */
285 arg[1].Type = ACPI_TYPE_INTEGER; /* Slave Addr */
286 arg[1].Integer.Value = addr;
287 arg[2].Type = ACPI_TYPE_INTEGER; /* Command */
288 if (I2C_OP_READ_P(op)) {
289 args.Count = 3;
290 if (len == 0) {
291 arg[2].Integer.Value = 0;
292 if (cmdlen == 0)
293 arg[0].Integer.Value = ACPI_SMBUS_RD_QUICK;
294 else
295 arg[0].Integer.Value = ACPI_SMBUS_RCV_BYTE;
296 } else
297 arg[2].Integer.Value = *c;
298 if (len == 1)
299 arg[0].Integer.Value = ACPI_SMBUS_RD_BYTE;
300 else if (len == 2)
301 arg[0].Integer.Value = ACPI_SMBUS_RD_WORD;
302 else if (len > 2)
303 arg[0].Integer.Value = ACPI_SMBUS_RD_BLOCK;
304 rv = AcpiEvaluateObject(sc->sc_devnode->ad_handle, "_SBR",
305 &args, &smbuf);
306 } else {
307 args.Count = 5;
308 arg[3].Type = ACPI_TYPE_INTEGER; /* Data Len */
309 arg[3].Integer.Value = len;
310 arg[4].Type = ACPI_TYPE_INTEGER; /* Data */
311 if (len == 0) {
312 arg[4].Integer.Value = 0;
313 if (cmdlen == 0) {
314 arg[2].Integer.Value = 0;
315 arg[0].Integer.Value = ACPI_SMBUS_WR_QUICK;
316 } else {
317 arg[2].Integer.Value = *c;
318 arg[0].Integer.Value = ACPI_SMBUS_SND_BYTE;
319 }
320 } else
321 arg[2].Integer.Value = *c;
322 if (len == 1) {
323 arg[0].Integer.Value = ACPI_SMBUS_WR_BYTE;
324 arg[4].Integer.Value = *b;
325 } else if (len == 2) {
326 arg[0].Integer.Value = ACPI_SMBUS_WR_WORD;
327 arg[4].Integer.Value = *b++;
328 arg[4].Integer.Value += (*b--) << 8;
329 } else if (len > 2) {
330 arg[0].Integer.Value = ACPI_SMBUS_WR_BLOCK;
331 arg[4].Type = ACPI_TYPE_BUFFER;
332 arg[4].Buffer.Pointer = buf;
333 arg[4].Buffer.Length = (len < 32?len:32);
334 }
335 rv = AcpiEvaluateObject(sc->sc_devnode->ad_handle, "_SBW",
336 &args, &smbuf);
337 }
338 if (ACPI_FAILURE(rv))
339 r = 1;
340 else {
341 p = smbuf.Pointer;
342 if (p == NULL || p->Type != ACPI_TYPE_PACKAGE ||
343 p->Package.Count < 1)
344 r = 1;
345 else {
346 e = p->Package.Elements;
347 if (e->Type == ACPI_TYPE_INTEGER)
348 r = e[0].Integer.Value;
349 else
350 r = 1;
351 }
352 if (r != 0)
353 r = 1;
354
355 /* For read operations, copy data to user buffer */
356 if (r == 0 && I2C_OP_READ_P(op)) {
357 if (p->Package.Count >= 3 &&
358 e[1].Type == ACPI_TYPE_INTEGER) {
359 xlen = e[1].Integer.Value;
360 if (xlen > len)
361 xlen = len;
362 if (xlen != 0 &&
363 e[2].Type == ACPI_TYPE_BUFFER) {
364 xb = e[2].Buffer.Pointer;
365 if (xb != NULL)
366 memcpy(b, xb, xlen);
367 else
368 r = 1;
369 } else if (e[2].Type == ACPI_TYPE_INTEGER) {
370 if (xlen > 0)
371 *b++ = e[2].Integer.Value &
372 0xff;
373 if (xlen > 1)
374 *b = (e[2].Integer.Value >> 8);
375 } else
376 r = 1;
377 } else
378 r = 1;
379 }
380 }
381 if (smbuf.Pointer)
382 ACPI_FREE(smbuf.Pointer);
383
384 return r;
385 }
386
387 /*
388 * acpi_smbus_alerts
389 *
390 * Whether triggered by periodic polling or an AcpiNotify, retrieve
391 * all pending SMBus device alerts
392 */
393 static void
394 acpi_smbus_alerts(struct acpi_smbus_softc *sc)
395 {
396 int status = 0;
397 uint8_t slave_addr;
398 ACPI_STATUS rv;
399 ACPI_BUFFER alert;
400 ACPI_OBJECT *e, *p;
401
402 do {
403 rv = acpi_eval_struct(sc->sc_devnode->ad_handle, "_SBA",
404 &alert);
405 if (ACPI_FAILURE(rv)) {
406 status = 1;
407 goto done;
408 }
409
410 p = alert.Pointer;
411 if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
412 p->Package.Count >= 2) {
413 e = p->Package.Elements;
414 if (e[0].Type == ACPI_TYPE_INTEGER)
415 status = e[0].Integer.Value;
416 else
417 status = 1;
418 if (status == 0x0 && e[1].Type == ACPI_TYPE_INTEGER) {
419 slave_addr = e[1].Integer.Value;
420 aprint_debug_dev(sc->sc_dv, "Alert for 0x%x\n",
421 slave_addr);
422 (void)iic_smbus_intr(&sc->sc_i2c_tag);
423 }
424 }
425 done:
426 if (alert.Pointer != NULL)
427 ACPI_FREE(alert.Pointer);
428 } while (status == 0);
429 }
430
431 static void
432 acpi_smbus_tick(void *opaque)
433 {
434 device_t dv = opaque;
435 struct acpi_smbus_softc *sc = device_private(dv);
436
437 acpi_smbus_alerts(sc);
438
439 callout_schedule(&sc->sc_callout, sc->sc_poll_alert * hz);
440 }
441
442 static void
443 acpi_smbus_notify_handler(ACPI_HANDLE hdl, uint32_t notify, void *opaque)
444 {
445 device_t dv = opaque;
446 struct acpi_smbus_softc *sc = device_private(dv);
447
448 aprint_debug_dev(dv, "received notify message 0x%x\n", notify);
449 acpi_smbus_alerts(sc);
450 }
451