ichsmb.c revision 1.73 1 /* $NetBSD: ichsmb.c,v 1.73 2021/10/11 15:47:10 msaitoh Exp $ */
2 /* $OpenBSD: ichiic.c,v 1.44 2020/10/07 11:23:05 jsg Exp $ */
3
4 /*
5 * Copyright (c) 2005, 2006 Alexander Yurchenko <grange (at) openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*
21 * Intel ICH SMBus controller driver.
22 */
23
24 #include <sys/cdefs.h>
25 __KERNEL_RCSID(0, "$NetBSD: ichsmb.c,v 1.73 2021/10/11 15:47:10 msaitoh Exp $");
26
27 #include <sys/param.h>
28 #include <sys/device.h>
29 #include <sys/errno.h>
30 #include <sys/kernel.h>
31 #include <sys/mutex.h>
32 #include <sys/condvar.h>
33 #include <sys/module.h>
34
35 #include <sys/bus.h>
36
37 #include <dev/pci/pcidevs.h>
38 #include <dev/pci/pcireg.h>
39 #include <dev/pci/pcivar.h>
40
41 #include <dev/ic/i82801lpcreg.h>
42
43 #include <dev/i2c/i2cvar.h>
44
45 #ifdef ICHIIC_DEBUG
46 #define DPRINTF(x) printf x
47 #else
48 #define DPRINTF(x)
49 #endif
50
51 #define ICHIIC_DELAY 100
52 #define ICHIIC_TIMEOUT 1
53
54 struct ichsmb_softc {
55 device_t sc_dev;
56
57 bus_space_tag_t sc_iot;
58 bus_space_handle_t sc_ioh;
59 bus_size_t sc_size;
60 pci_chipset_tag_t sc_pc;
61 void * sc_ih;
62 int sc_poll;
63 pci_intr_handle_t *sc_pihp;
64
65 kmutex_t sc_exec_lock;
66 kcondvar_t sc_exec_wait;
67
68 struct i2c_controller sc_i2c_tag;
69 struct {
70 i2c_op_t op;
71 void * buf;
72 size_t len;
73 int flags;
74 int error;
75 bool done;
76 } sc_i2c_xfer;
77 device_t sc_i2c_device;
78 };
79
80 static int ichsmb_match(device_t, cfdata_t, void *);
81 static void ichsmb_attach(device_t, device_t, void *);
82 static int ichsmb_detach(device_t, int);
83 static int ichsmb_rescan(device_t, const char *, const int *);
84 static void ichsmb_chdet(device_t, device_t);
85
86 static int ichsmb_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
87 size_t, void *, size_t, int);
88
89 static int ichsmb_intr(void *);
90
91 #include "ioconf.h"
92
93 CFATTACH_DECL3_NEW(ichsmb, sizeof(struct ichsmb_softc),
94 ichsmb_match, ichsmb_attach, ichsmb_detach, NULL, ichsmb_rescan,
95 ichsmb_chdet, DVF_DETACH_SHUTDOWN);
96
97
98 static int
99 ichsmb_match(device_t parent, cfdata_t match, void *aux)
100 {
101 struct pci_attach_args *pa = aux;
102
103 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
104 switch (PCI_PRODUCT(pa->pa_id)) {
105 case PCI_PRODUCT_INTEL_6300ESB_SMB:
106 case PCI_PRODUCT_INTEL_63XXESB_SMB:
107 case PCI_PRODUCT_INTEL_82801AA_SMB:
108 case PCI_PRODUCT_INTEL_82801AB_SMB:
109 case PCI_PRODUCT_INTEL_82801BA_SMB:
110 case PCI_PRODUCT_INTEL_82801CA_SMB:
111 case PCI_PRODUCT_INTEL_82801DB_SMB:
112 case PCI_PRODUCT_INTEL_82801E_SMB:
113 case PCI_PRODUCT_INTEL_82801EB_SMB:
114 case PCI_PRODUCT_INTEL_82801FB_SMB:
115 case PCI_PRODUCT_INTEL_82801G_SMB:
116 case PCI_PRODUCT_INTEL_82801H_SMB:
117 case PCI_PRODUCT_INTEL_82801I_SMB:
118 case PCI_PRODUCT_INTEL_82801JD_SMB:
119 case PCI_PRODUCT_INTEL_82801JI_SMB:
120 case PCI_PRODUCT_INTEL_3400_SMB:
121 case PCI_PRODUCT_INTEL_6SERIES_SMB:
122 case PCI_PRODUCT_INTEL_7SERIES_SMB:
123 case PCI_PRODUCT_INTEL_8SERIES_SMB:
124 case PCI_PRODUCT_INTEL_9SERIES_SMB:
125 case PCI_PRODUCT_INTEL_100SERIES_SMB:
126 case PCI_PRODUCT_INTEL_100SERIES_LP_SMB:
127 case PCI_PRODUCT_INTEL_2HS_SMB:
128 case PCI_PRODUCT_INTEL_3HS_SMB:
129 case PCI_PRODUCT_INTEL_3HS_U_SMB:
130 case PCI_PRODUCT_INTEL_4HS_H_SMB:
131 case PCI_PRODUCT_INTEL_CORE4G_M_SMB:
132 case PCI_PRODUCT_INTEL_CORE5G_M_SMB:
133 case PCI_PRODUCT_INTEL_CMTLK_SMB:
134 case PCI_PRODUCT_INTEL_BAYTRAIL_PCU_SMB:
135 case PCI_PRODUCT_INTEL_BSW_PCU_SMB:
136 case PCI_PRODUCT_INTEL_APL_SMB:
137 case PCI_PRODUCT_INTEL_GLK_SMB:
138 case PCI_PRODUCT_INTEL_JSL_SMB:
139 case PCI_PRODUCT_INTEL_C600_SMBUS:
140 case PCI_PRODUCT_INTEL_C600_SMB_0:
141 case PCI_PRODUCT_INTEL_C600_SMB_1:
142 case PCI_PRODUCT_INTEL_C600_SMB_2:
143 case PCI_PRODUCT_INTEL_C610_SMB:
144 case PCI_PRODUCT_INTEL_C620_SMB:
145 case PCI_PRODUCT_INTEL_C620_SMB_S:
146 case PCI_PRODUCT_INTEL_EP80579_SMB:
147 case PCI_PRODUCT_INTEL_DH89XXCC_SMB:
148 case PCI_PRODUCT_INTEL_DH89XXCL_SMB:
149 case PCI_PRODUCT_INTEL_C2000_PCU_SMBUS:
150 case PCI_PRODUCT_INTEL_C3K_SMBUS_LEGACY:
151 case PCI_PRODUCT_INTEL_495_YU_SMB:
152 case PCI_PRODUCT_INTEL_5HS_H_SMB:
153 case PCI_PRODUCT_INTEL_5HS_LP_SMB:
154 return 1;
155 }
156 }
157 return 0;
158 }
159
160 static void
161 ichsmb_attach(device_t parent, device_t self, void *aux)
162 {
163 struct ichsmb_softc *sc = device_private(self);
164 struct pci_attach_args *pa = aux;
165 pcireg_t conf;
166 const char *intrstr = NULL;
167 char intrbuf[PCI_INTRSTR_LEN];
168
169 sc->sc_dev = self;
170 sc->sc_pc = pa->pa_pc;
171
172 pci_aprint_devinfo(pa, NULL);
173
174 mutex_init(&sc->sc_exec_lock, MUTEX_DEFAULT, IPL_BIO);
175 cv_init(&sc->sc_exec_wait, device_xname(self));
176
177 /* Read configuration */
178 conf = pci_conf_read(pa->pa_pc, pa->pa_tag, LPCIB_SMB_HOSTC);
179 DPRINTF(("%s: conf 0x%08x\n", device_xname(sc->sc_dev), conf));
180
181 if ((conf & LPCIB_SMB_HOSTC_HSTEN) == 0) {
182 aprint_error_dev(self, "SMBus disabled\n");
183 goto out;
184 }
185
186 /* Map I/O space */
187 if (pci_mapreg_map(pa, LPCIB_SMB_BASE, PCI_MAPREG_TYPE_IO, 0,
188 &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_size)) {
189 aprint_error_dev(self, "can't map I/O space\n");
190 goto out;
191 }
192
193 sc->sc_poll = 1;
194 sc->sc_ih = NULL;
195 if (conf & LPCIB_SMB_HOSTC_SMIEN) {
196 /* No PCI IRQ */
197 aprint_normal_dev(self, "interrupting at SMI\n");
198 } else {
199 /* Install interrupt handler */
200 if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0) == 0) {
201 intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0],
202 intrbuf, sizeof(intrbuf));
203 pci_intr_setattr(pa->pa_pc, &sc->sc_pihp[0],
204 PCI_INTR_MPSAFE, true);
205 sc->sc_ih = pci_intr_establish_xname(pa->pa_pc,
206 sc->sc_pihp[0], IPL_BIO, ichsmb_intr, sc,
207 device_xname(sc->sc_dev));
208 if (sc->sc_ih != NULL) {
209 aprint_normal_dev(self, "interrupting at %s\n",
210 intrstr);
211 sc->sc_poll = 0;
212 } else {
213 pci_intr_release(pa->pa_pc, sc->sc_pihp, 1);
214 sc->sc_pihp = NULL;
215 }
216 }
217 if (sc->sc_poll)
218 aprint_normal_dev(self, "polling\n");
219 }
220
221 sc->sc_i2c_device = NULL;
222 ichsmb_rescan(self, NULL, NULL);
223
224 out: if (!pmf_device_register(self, NULL, NULL))
225 aprint_error_dev(self, "couldn't establish power handler\n");
226 }
227
228 static int
229 ichsmb_rescan(device_t self, const char *ifattr, const int *locators)
230 {
231 struct ichsmb_softc *sc = device_private(self);
232 struct i2cbus_attach_args iba;
233
234 if (sc->sc_i2c_device != NULL)
235 return 0;
236
237 /* Attach I2C bus */
238 iic_tag_init(&sc->sc_i2c_tag);
239 sc->sc_i2c_tag.ic_cookie = sc;
240 sc->sc_i2c_tag.ic_exec = ichsmb_i2c_exec;
241
242 memset(&iba, 0, sizeof(iba));
243 iba.iba_tag = &sc->sc_i2c_tag;
244 sc->sc_i2c_device = config_found(self, &iba, iicbus_print, CFARGS_NONE);
245
246 return 0;
247 }
248
249 static int
250 ichsmb_detach(device_t self, int flags)
251 {
252 struct ichsmb_softc *sc = device_private(self);
253 int error;
254
255 if (sc->sc_i2c_device) {
256 error = config_detach(sc->sc_i2c_device, flags);
257 if (error)
258 return error;
259 }
260
261 iic_tag_fini(&sc->sc_i2c_tag);
262
263 if (sc->sc_ih) {
264 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
265 sc->sc_ih = NULL;
266 }
267
268 if (sc->sc_pihp) {
269 pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
270 sc->sc_pihp = NULL;
271 }
272
273 if (sc->sc_size != 0)
274 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
275
276 mutex_destroy(&sc->sc_exec_lock);
277 cv_destroy(&sc->sc_exec_wait);
278
279 return 0;
280 }
281
282 static void
283 ichsmb_chdet(device_t self, device_t child)
284 {
285 struct ichsmb_softc *sc = device_private(self);
286
287 if (sc->sc_i2c_device == child)
288 sc->sc_i2c_device = NULL;
289 }
290
291 static int
292 ichsmb_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
293 const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
294 {
295 struct ichsmb_softc *sc = cookie;
296 const uint8_t *b;
297 uint8_t ctl = 0, st;
298 int retries;
299 char fbuf[64];
300
301 DPRINTF(("%s: exec: op %d, addr 0x%02x, cmdlen %zu, len %zu, "
302 "flags 0x%02x\n", device_xname(sc->sc_dev), op, addr, cmdlen,
303 len, flags));
304
305 mutex_enter(&sc->sc_exec_lock);
306
307 /* Clear status bits */
308 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS,
309 LPCIB_SMB_HS_INTR | LPCIB_SMB_HS_DEVERR |
310 LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED);
311 bus_space_barrier(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, 1,
312 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
313
314 /* Wait for bus to be idle */
315 for (retries = 100; retries > 0; retries--) {
316 st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
317 if (!(st & LPCIB_SMB_HS_BUSY))
318 break;
319 DELAY(ICHIIC_DELAY);
320 }
321 #ifdef ICHIIC_DEBUG
322 snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
323 printf("%s: exec: st %s\n", device_xname(sc->sc_dev), fbuf);
324 #endif
325 if (st & LPCIB_SMB_HS_BUSY) {
326 mutex_exit(&sc->sc_exec_lock);
327 return (EBUSY);
328 }
329
330 if (sc->sc_poll)
331 flags |= I2C_F_POLL;
332
333 if (!I2C_OP_STOP_P(op) || cmdlen > 1 || len > 2 ||
334 (cmdlen == 0 && len > 1)) {
335 mutex_exit(&sc->sc_exec_lock);
336 return (EINVAL);
337 }
338
339 /* Setup transfer */
340 sc->sc_i2c_xfer.op = op;
341 sc->sc_i2c_xfer.buf = buf;
342 sc->sc_i2c_xfer.len = len;
343 sc->sc_i2c_xfer.flags = flags;
344 sc->sc_i2c_xfer.error = 0;
345 sc->sc_i2c_xfer.done = false;
346
347 /* Set slave address and transfer direction */
348 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_TXSLVA,
349 LPCIB_SMB_TXSLVA_ADDR(addr) |
350 (I2C_OP_READ_P(op) ? LPCIB_SMB_TXSLVA_READ : 0));
351
352 b = (const uint8_t *)cmdbuf;
353 if (cmdlen > 0)
354 /* Set command byte */
355 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HCMD, b[0]);
356
357 if (I2C_OP_WRITE_P(op)) {
358 /* Write data */
359 b = buf;
360 if (cmdlen == 0 && len == 1)
361 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
362 LPCIB_SMB_HCMD, b[0]);
363 else if (len > 0)
364 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
365 LPCIB_SMB_HD0, b[0]);
366 if (len > 1)
367 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
368 LPCIB_SMB_HD1, b[1]);
369 }
370
371 /* Set SMBus command */
372 if (cmdlen == 0) {
373 if (len == 0)
374 ctl = LPCIB_SMB_HC_CMD_QUICK;
375 else
376 ctl = LPCIB_SMB_HC_CMD_BYTE;
377 } else if (len == 1)
378 ctl = LPCIB_SMB_HC_CMD_BDATA;
379 else if (len == 2)
380 ctl = LPCIB_SMB_HC_CMD_WDATA;
381
382 if ((flags & I2C_F_POLL) == 0)
383 ctl |= LPCIB_SMB_HC_INTREN;
384
385 /* Start transaction */
386 ctl |= LPCIB_SMB_HC_START;
387 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HC, ctl);
388
389 if (flags & I2C_F_POLL) {
390 /* Poll for completion */
391 DELAY(ICHIIC_DELAY);
392 for (retries = 1000; retries > 0; retries--) {
393 st = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
394 LPCIB_SMB_HS);
395 if ((st & LPCIB_SMB_HS_BUSY) == 0)
396 break;
397 DELAY(ICHIIC_DELAY);
398 }
399 if (st & LPCIB_SMB_HS_BUSY)
400 goto timeout;
401 ichsmb_intr(sc);
402 } else {
403 /* Wait for interrupt */
404 while (! sc->sc_i2c_xfer.done) {
405 if (cv_timedwait(&sc->sc_exec_wait, &sc->sc_exec_lock,
406 ICHIIC_TIMEOUT * hz))
407 goto timeout;
408 }
409 }
410
411 int error = sc->sc_i2c_xfer.error;
412 mutex_exit(&sc->sc_exec_lock);
413
414 return (error);
415
416 timeout:
417 /*
418 * Transfer timeout. Kill the transaction and clear status bits.
419 */
420 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HC,
421 LPCIB_SMB_HC_KILL);
422 DELAY(ICHIIC_DELAY);
423 st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
424 if ((st & LPCIB_SMB_HS_FAILED) == 0) {
425 snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
426 aprint_error_dev(sc->sc_dev, "abort failed, status %s\n",
427 fbuf);
428 }
429 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, st);
430 mutex_exit(&sc->sc_exec_lock);
431 return (ETIMEDOUT);
432 }
433
434 static int
435 ichsmb_intr(void *arg)
436 {
437 struct ichsmb_softc *sc = arg;
438 uint8_t st;
439 uint8_t *b;
440 size_t len;
441 #ifdef ICHIIC_DEBUG
442 char fbuf[64];
443 #endif
444
445 /* Read status */
446 st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
447
448 /* Clear status bits */
449 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, st);
450
451 /* XXX Ignore SMBALERT# for now */
452 if ((st & LPCIB_SMB_HS_BUSY) != 0 || (st & (LPCIB_SMB_HS_INTR |
453 LPCIB_SMB_HS_DEVERR | LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED |
454 LPCIB_SMB_HS_BDONE)) == 0)
455 /* Interrupt was not for us */
456 return (0);
457
458 #ifdef ICHIIC_DEBUG
459 snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
460 printf("%s: intr st %s\n", device_xname(sc->sc_dev), fbuf);
461 #endif
462
463 if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0)
464 mutex_enter(&sc->sc_exec_lock);
465
466 /* Check for errors */
467 if (st & (LPCIB_SMB_HS_DEVERR | LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED)) {
468 sc->sc_i2c_xfer.error = EIO;
469 goto done;
470 }
471
472 if (st & LPCIB_SMB_HS_INTR) {
473 if (I2C_OP_WRITE_P(sc->sc_i2c_xfer.op))
474 goto done;
475
476 /* Read data */
477 b = sc->sc_i2c_xfer.buf;
478 len = sc->sc_i2c_xfer.len;
479 if (len > 0)
480 b[0] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
481 LPCIB_SMB_HD0);
482 if (len > 1)
483 b[1] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
484 LPCIB_SMB_HD1);
485 }
486
487 done:
488 sc->sc_i2c_xfer.done = true;
489 if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0) {
490 cv_signal(&sc->sc_exec_wait);
491 mutex_exit(&sc->sc_exec_lock);
492 }
493 return (1);
494 }
495
496 MODULE(MODULE_CLASS_DRIVER, ichsmb, "pci,iic");
497
498 #ifdef _MODULE
499 #include "ioconf.c"
500 #endif
501
502 static int
503 ichsmb_modcmd(modcmd_t cmd, void *opaque)
504 {
505 int error = 0;
506
507 switch (cmd) {
508 case MODULE_CMD_INIT:
509 #ifdef _MODULE
510 error = config_init_component(cfdriver_ioconf_ichsmb,
511 cfattach_ioconf_ichsmb, cfdata_ioconf_ichsmb);
512 #endif
513 break;
514 case MODULE_CMD_FINI:
515 #ifdef _MODULE
516 error = config_fini_component(cfdriver_ioconf_ichsmb,
517 cfattach_ioconf_ichsmb, cfdata_ioconf_ichsmb);
518 #endif
519 break;
520 default:
521 #ifdef _MODULE
522 error = ENOTTY;
523 #endif
524 break;
525 }
526
527 return error;
528 }
529