ichsmb.c revision 1.37.4.7 1 1.37.4.7 skrll /* $NetBSD: ichsmb.c,v 1.37.4.7 2017/08/28 17:52:05 skrll Exp $ */
2 1.1 kiyohara /* $OpenBSD: ichiic.c,v 1.18 2007/05/03 09:36:26 dlg Exp $ */
3 1.1 kiyohara
4 1.1 kiyohara /*
5 1.1 kiyohara * Copyright (c) 2005, 2006 Alexander Yurchenko <grange (at) openbsd.org>
6 1.1 kiyohara *
7 1.1 kiyohara * Permission to use, copy, modify, and distribute this software for any
8 1.1 kiyohara * purpose with or without fee is hereby granted, provided that the above
9 1.1 kiyohara * copyright notice and this permission notice appear in all copies.
10 1.1 kiyohara *
11 1.1 kiyohara * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 kiyohara * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 kiyohara * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 kiyohara * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 kiyohara * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 kiyohara * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 kiyohara * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 kiyohara */
19 1.1 kiyohara
20 1.1 kiyohara /*
21 1.1 kiyohara * Intel ICH SMBus controller driver.
22 1.1 kiyohara */
23 1.1 kiyohara
24 1.6 xtraeme #include <sys/cdefs.h>
25 1.37.4.7 skrll __KERNEL_RCSID(0, "$NetBSD: ichsmb.c,v 1.37.4.7 2017/08/28 17:52:05 skrll Exp $");
26 1.6 xtraeme
27 1.1 kiyohara #include <sys/param.h>
28 1.1 kiyohara #include <sys/device.h>
29 1.1 kiyohara #include <sys/errno.h>
30 1.1 kiyohara #include <sys/kernel.h>
31 1.27 pgoyette #include <sys/mutex.h>
32 1.1 kiyohara #include <sys/proc.h>
33 1.1 kiyohara
34 1.10 ad #include <sys/bus.h>
35 1.1 kiyohara
36 1.1 kiyohara #include <dev/pci/pcidevs.h>
37 1.1 kiyohara #include <dev/pci/pcireg.h>
38 1.1 kiyohara #include <dev/pci/pcivar.h>
39 1.1 kiyohara
40 1.5 xtraeme #include <dev/ic/i82801lpcreg.h>
41 1.1 kiyohara
42 1.1 kiyohara #include <dev/i2c/i2cvar.h>
43 1.1 kiyohara
44 1.1 kiyohara #ifdef ICHIIC_DEBUG
45 1.1 kiyohara #define DPRINTF(x) printf x
46 1.1 kiyohara #else
47 1.1 kiyohara #define DPRINTF(x)
48 1.1 kiyohara #endif
49 1.1 kiyohara
50 1.1 kiyohara #define ICHIIC_DELAY 100
51 1.1 kiyohara #define ICHIIC_TIMEOUT 1
52 1.1 kiyohara
53 1.1 kiyohara struct ichsmb_softc {
54 1.12 kiyohara device_t sc_dev;
55 1.1 kiyohara
56 1.1 kiyohara bus_space_tag_t sc_iot;
57 1.1 kiyohara bus_space_handle_t sc_ioh;
58 1.1 kiyohara void * sc_ih;
59 1.1 kiyohara int sc_poll;
60 1.1 kiyohara
61 1.1 kiyohara struct i2c_controller sc_i2c_tag;
62 1.27 pgoyette kmutex_t sc_i2c_mutex;
63 1.1 kiyohara struct {
64 1.1 kiyohara i2c_op_t op;
65 1.1 kiyohara void * buf;
66 1.1 kiyohara size_t len;
67 1.1 kiyohara int flags;
68 1.1 kiyohara volatile int error;
69 1.1 kiyohara } sc_i2c_xfer;
70 1.37.4.2 skrll device_t sc_i2c_device;
71 1.1 kiyohara };
72 1.1 kiyohara
73 1.21 cegger static int ichsmb_match(device_t, cfdata_t, void *);
74 1.12 kiyohara static void ichsmb_attach(device_t, device_t, void *);
75 1.37.4.2 skrll static int ichsmb_rescan(device_t, const char *, const int *);
76 1.37.4.2 skrll static void ichsmb_chdet(device_t, device_t);
77 1.1 kiyohara
78 1.1 kiyohara static int ichsmb_i2c_acquire_bus(void *, int);
79 1.1 kiyohara static void ichsmb_i2c_release_bus(void *, int);
80 1.1 kiyohara static int ichsmb_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
81 1.1 kiyohara size_t, void *, size_t, int);
82 1.1 kiyohara
83 1.1 kiyohara static int ichsmb_intr(void *);
84 1.1 kiyohara
85 1.1 kiyohara
86 1.37.4.2 skrll CFATTACH_DECL3_NEW(ichsmb, sizeof(struct ichsmb_softc),
87 1.37.4.2 skrll ichsmb_match, ichsmb_attach, NULL, NULL, ichsmb_rescan, ichsmb_chdet, 0);
88 1.1 kiyohara
89 1.1 kiyohara
90 1.1 kiyohara static int
91 1.21 cegger ichsmb_match(device_t parent, cfdata_t match, void *aux)
92 1.1 kiyohara {
93 1.1 kiyohara struct pci_attach_args *pa = aux;
94 1.1 kiyohara
95 1.1 kiyohara if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
96 1.1 kiyohara switch (PCI_PRODUCT(pa->pa_id)) {
97 1.1 kiyohara case PCI_PRODUCT_INTEL_6300ESB_SMB:
98 1.1 kiyohara case PCI_PRODUCT_INTEL_63XXESB_SMB:
99 1.1 kiyohara case PCI_PRODUCT_INTEL_82801AA_SMB:
100 1.1 kiyohara case PCI_PRODUCT_INTEL_82801AB_SMB:
101 1.1 kiyohara case PCI_PRODUCT_INTEL_82801BA_SMB:
102 1.1 kiyohara case PCI_PRODUCT_INTEL_82801CA_SMB:
103 1.1 kiyohara case PCI_PRODUCT_INTEL_82801DB_SMB:
104 1.1 kiyohara case PCI_PRODUCT_INTEL_82801E_SMB:
105 1.1 kiyohara case PCI_PRODUCT_INTEL_82801EB_SMB:
106 1.1 kiyohara case PCI_PRODUCT_INTEL_82801FB_SMB:
107 1.1 kiyohara case PCI_PRODUCT_INTEL_82801G_SMB:
108 1.1 kiyohara case PCI_PRODUCT_INTEL_82801H_SMB:
109 1.7 xtraeme case PCI_PRODUCT_INTEL_82801I_SMB:
110 1.23 njoly case PCI_PRODUCT_INTEL_82801JD_SMB:
111 1.23 njoly case PCI_PRODUCT_INTEL_82801JI_SMB:
112 1.22 tnn case PCI_PRODUCT_INTEL_3400_SMB:
113 1.25 msaitoh case PCI_PRODUCT_INTEL_6SERIES_SMB:
114 1.28 riastrad case PCI_PRODUCT_INTEL_7SERIES_SMB:
115 1.31 msaitoh case PCI_PRODUCT_INTEL_8SERIES_SMB:
116 1.37.4.1 skrll case PCI_PRODUCT_INTEL_9SERIES_SMB:
117 1.37.4.3 skrll case PCI_PRODUCT_INTEL_100SERIES_SMB:
118 1.37.4.7 skrll case PCI_PRODUCT_INTEL_100SERIES_LP_SMB:
119 1.37.4.6 skrll case PCI_PRODUCT_INTEL_2HS_SMB:
120 1.33 msaitoh case PCI_PRODUCT_INTEL_CORE4G_M_SMB:
121 1.37.4.1 skrll case PCI_PRODUCT_INTEL_CORE5G_M_SMB:
122 1.36 msaitoh case PCI_PRODUCT_INTEL_BAYTRAIL_PCU_SMB:
123 1.37.4.3 skrll case PCI_PRODUCT_INTEL_BSW_PCU_SMB:
124 1.30 riastrad case PCI_PRODUCT_INTEL_C600_SMBUS:
125 1.29 msaitoh case PCI_PRODUCT_INTEL_C600_SMB_0:
126 1.29 msaitoh case PCI_PRODUCT_INTEL_C600_SMB_1:
127 1.29 msaitoh case PCI_PRODUCT_INTEL_C600_SMB_2:
128 1.37.4.1 skrll case PCI_PRODUCT_INTEL_C610_SMB:
129 1.36 msaitoh case PCI_PRODUCT_INTEL_EP80579_SMB:
130 1.37.4.1 skrll case PCI_PRODUCT_INTEL_DH89XXCC_SMB:
131 1.37.4.1 skrll case PCI_PRODUCT_INTEL_DH89XXCL_SMB:
132 1.34 msaitoh case PCI_PRODUCT_INTEL_C2000_PCU_SMBUS:
133 1.37.4.7 skrll case PCI_PRODUCT_INTEL_C3K_SMBUS_LEGACY:
134 1.1 kiyohara return 1;
135 1.1 kiyohara }
136 1.1 kiyohara }
137 1.1 kiyohara return 0;
138 1.1 kiyohara }
139 1.1 kiyohara
140 1.1 kiyohara static void
141 1.12 kiyohara ichsmb_attach(device_t parent, device_t self, void *aux)
142 1.1 kiyohara {
143 1.12 kiyohara struct ichsmb_softc *sc = device_private(self);
144 1.1 kiyohara struct pci_attach_args *pa = aux;
145 1.1 kiyohara pcireg_t conf;
146 1.1 kiyohara bus_size_t iosize;
147 1.1 kiyohara pci_intr_handle_t ih;
148 1.1 kiyohara const char *intrstr = NULL;
149 1.35 christos char intrbuf[PCI_INTRSTR_LEN];
150 1.37.4.2 skrll int flags;
151 1.1 kiyohara
152 1.12 kiyohara sc->sc_dev = self;
153 1.12 kiyohara
154 1.26 drochner pci_aprint_devinfo(pa, NULL);
155 1.1 kiyohara
156 1.1 kiyohara /* Read configuration */
157 1.5 xtraeme conf = pci_conf_read(pa->pa_pc, pa->pa_tag, LPCIB_SMB_HOSTC);
158 1.16 njoly DPRINTF(("%s: conf 0x%08x\n", device_xname(sc->sc_dev), conf));
159 1.1 kiyohara
160 1.5 xtraeme if ((conf & LPCIB_SMB_HOSTC_HSTEN) == 0) {
161 1.12 kiyohara aprint_error_dev(self, "SMBus disabled\n");
162 1.37 riastrad goto out;
163 1.1 kiyohara }
164 1.1 kiyohara
165 1.1 kiyohara /* Map I/O space */
166 1.5 xtraeme if (pci_mapreg_map(pa, LPCIB_SMB_BASE, PCI_MAPREG_TYPE_IO, 0,
167 1.1 kiyohara &sc->sc_iot, &sc->sc_ioh, NULL, &iosize)) {
168 1.12 kiyohara aprint_error_dev(self, "can't map I/O space\n");
169 1.37 riastrad goto out;
170 1.1 kiyohara }
171 1.1 kiyohara
172 1.1 kiyohara sc->sc_poll = 1;
173 1.5 xtraeme if (conf & LPCIB_SMB_HOSTC_SMIEN) {
174 1.1 kiyohara /* No PCI IRQ */
175 1.15 njoly aprint_normal_dev(self, "interrupting at SMI\n");
176 1.1 kiyohara } else {
177 1.1 kiyohara /* Install interrupt handler */
178 1.1 kiyohara if (pci_intr_map(pa, &ih) == 0) {
179 1.37.4.4 skrll intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf,
180 1.37.4.4 skrll sizeof(intrbuf));
181 1.37.4.5 skrll sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih,
182 1.37.4.5 skrll IPL_BIO, ichsmb_intr, sc, device_xname(sc->sc_dev));
183 1.1 kiyohara if (sc->sc_ih != NULL) {
184 1.12 kiyohara aprint_normal_dev(self, "interrupting at %s\n",
185 1.12 kiyohara intrstr);
186 1.1 kiyohara sc->sc_poll = 0;
187 1.1 kiyohara }
188 1.1 kiyohara }
189 1.1 kiyohara if (sc->sc_poll)
190 1.12 kiyohara aprint_normal_dev(self, "polling\n");
191 1.1 kiyohara }
192 1.1 kiyohara
193 1.37.4.2 skrll sc->sc_i2c_device = NULL;
194 1.37.4.2 skrll flags = 0;
195 1.37.4.3 skrll mutex_init(&sc->sc_i2c_mutex, MUTEX_DEFAULT, IPL_NONE);
196 1.37.4.2 skrll ichsmb_rescan(self, "i2cbus", &flags);
197 1.37.4.2 skrll
198 1.37.4.2 skrll out: if (!pmf_device_register(self, NULL, NULL))
199 1.37.4.2 skrll aprint_error_dev(self, "couldn't establish power handler\n");
200 1.37.4.2 skrll }
201 1.37.4.2 skrll
202 1.37.4.2 skrll static int
203 1.37.4.2 skrll ichsmb_rescan(device_t self, const char *ifattr, const int *flags)
204 1.37.4.2 skrll {
205 1.37.4.2 skrll struct ichsmb_softc *sc = device_private(self);
206 1.37.4.2 skrll struct i2cbus_attach_args iba;
207 1.37.4.2 skrll
208 1.37.4.2 skrll if (!ifattr_match(ifattr, "i2cbus"))
209 1.37.4.2 skrll return 0;
210 1.37.4.2 skrll
211 1.37.4.2 skrll if (sc->sc_i2c_device)
212 1.37.4.2 skrll return 0;
213 1.37.4.2 skrll
214 1.1 kiyohara /* Attach I2C bus */
215 1.1 kiyohara sc->sc_i2c_tag.ic_cookie = sc;
216 1.1 kiyohara sc->sc_i2c_tag.ic_acquire_bus = ichsmb_i2c_acquire_bus;
217 1.1 kiyohara sc->sc_i2c_tag.ic_release_bus = ichsmb_i2c_release_bus;
218 1.1 kiyohara sc->sc_i2c_tag.ic_exec = ichsmb_i2c_exec;
219 1.1 kiyohara
220 1.20 cegger memset(&iba, 0, sizeof(iba));
221 1.9 riz iba.iba_type = I2C_TYPE_SMBUS;
222 1.1 kiyohara iba.iba_tag = &sc->sc_i2c_tag;
223 1.37.4.2 skrll sc->sc_i2c_device = config_found_ia(self, ifattr, &iba, iicbus_print);
224 1.37.4.2 skrll
225 1.37.4.2 skrll return 0;
226 1.37.4.2 skrll }
227 1.37.4.2 skrll
228 1.37.4.2 skrll static void
229 1.37.4.2 skrll ichsmb_chdet(device_t self, device_t child)
230 1.37.4.2 skrll {
231 1.37.4.2 skrll struct ichsmb_softc *sc = device_private(self);
232 1.37.4.2 skrll
233 1.37.4.2 skrll if (sc->sc_i2c_device == child)
234 1.37.4.2 skrll sc->sc_i2c_device = NULL;
235 1.1 kiyohara
236 1.1 kiyohara }
237 1.1 kiyohara
238 1.1 kiyohara static int
239 1.1 kiyohara ichsmb_i2c_acquire_bus(void *cookie, int flags)
240 1.1 kiyohara {
241 1.1 kiyohara struct ichsmb_softc *sc = cookie;
242 1.1 kiyohara
243 1.27 pgoyette if (cold)
244 1.8 xtraeme return 0;
245 1.1 kiyohara
246 1.27 pgoyette mutex_enter(&sc->sc_i2c_mutex);
247 1.8 xtraeme return 0;
248 1.1 kiyohara }
249 1.1 kiyohara
250 1.1 kiyohara static void
251 1.1 kiyohara ichsmb_i2c_release_bus(void *cookie, int flags)
252 1.1 kiyohara {
253 1.1 kiyohara struct ichsmb_softc *sc = cookie;
254 1.1 kiyohara
255 1.27 pgoyette if (cold)
256 1.1 kiyohara return;
257 1.1 kiyohara
258 1.27 pgoyette mutex_exit(&sc->sc_i2c_mutex);
259 1.1 kiyohara }
260 1.1 kiyohara
261 1.1 kiyohara static int
262 1.1 kiyohara ichsmb_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
263 1.1 kiyohara const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
264 1.1 kiyohara {
265 1.1 kiyohara struct ichsmb_softc *sc = cookie;
266 1.1 kiyohara const uint8_t *b;
267 1.1 kiyohara uint8_t ctl = 0, st;
268 1.1 kiyohara int retries;
269 1.4 kiyohara char fbuf[64];
270 1.1 kiyohara
271 1.14 njoly DPRINTF(("%s: exec: op %d, addr 0x%02x, cmdlen %zu, len %zu, "
272 1.14 njoly "flags 0x%02x\n", device_xname(sc->sc_dev), op, addr, cmdlen,
273 1.1 kiyohara len, flags));
274 1.1 kiyohara
275 1.32 soren /* Clear status bits */
276 1.32 soren bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS,
277 1.32 soren LPCIB_SMB_HS_INTR | LPCIB_SMB_HS_DEVERR |
278 1.32 soren LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED);
279 1.32 soren bus_space_barrier(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, 1,
280 1.37.4.2 skrll BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
281 1.32 soren
282 1.1 kiyohara /* Wait for bus to be idle */
283 1.1 kiyohara for (retries = 100; retries > 0; retries--) {
284 1.5 xtraeme st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
285 1.5 xtraeme if (!(st & LPCIB_SMB_HS_BUSY))
286 1.1 kiyohara break;
287 1.1 kiyohara DELAY(ICHIIC_DELAY);
288 1.1 kiyohara }
289 1.4 kiyohara #ifdef ICHIIC_DEBUG
290 1.18 christos snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
291 1.37.4.7 skrll printf("%s: exec: st %s\n", device_xname(sc->sc_dev), fbuf);
292 1.4 kiyohara #endif
293 1.5 xtraeme if (st & LPCIB_SMB_HS_BUSY)
294 1.1 kiyohara return (1);
295 1.1 kiyohara
296 1.1 kiyohara if (cold || sc->sc_poll)
297 1.1 kiyohara flags |= I2C_F_POLL;
298 1.1 kiyohara
299 1.24 hannken if (!I2C_OP_STOP_P(op) || cmdlen > 1 || len > 2 ||
300 1.24 hannken (cmdlen == 0 && len > 1))
301 1.1 kiyohara return (1);
302 1.1 kiyohara
303 1.1 kiyohara /* Setup transfer */
304 1.1 kiyohara sc->sc_i2c_xfer.op = op;
305 1.1 kiyohara sc->sc_i2c_xfer.buf = buf;
306 1.1 kiyohara sc->sc_i2c_xfer.len = len;
307 1.1 kiyohara sc->sc_i2c_xfer.flags = flags;
308 1.1 kiyohara sc->sc_i2c_xfer.error = 0;
309 1.1 kiyohara
310 1.1 kiyohara /* Set slave address and transfer direction */
311 1.5 xtraeme bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_TXSLVA,
312 1.5 xtraeme LPCIB_SMB_TXSLVA_ADDR(addr) |
313 1.5 xtraeme (I2C_OP_READ_P(op) ? LPCIB_SMB_TXSLVA_READ : 0));
314 1.1 kiyohara
315 1.1 kiyohara b = (const uint8_t *)cmdbuf;
316 1.1 kiyohara if (cmdlen > 0)
317 1.1 kiyohara /* Set command byte */
318 1.5 xtraeme bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HCMD, b[0]);
319 1.1 kiyohara
320 1.1 kiyohara if (I2C_OP_WRITE_P(op)) {
321 1.1 kiyohara /* Write data */
322 1.1 kiyohara b = buf;
323 1.24 hannken if (cmdlen == 0 && len == 1)
324 1.24 hannken bus_space_write_1(sc->sc_iot, sc->sc_ioh,
325 1.24 hannken LPCIB_SMB_HCMD, b[0]);
326 1.24 hannken else if (len > 0)
327 1.1 kiyohara bus_space_write_1(sc->sc_iot, sc->sc_ioh,
328 1.5 xtraeme LPCIB_SMB_HD0, b[0]);
329 1.1 kiyohara if (len > 1)
330 1.1 kiyohara bus_space_write_1(sc->sc_iot, sc->sc_ioh,
331 1.5 xtraeme LPCIB_SMB_HD1, b[1]);
332 1.1 kiyohara }
333 1.1 kiyohara
334 1.1 kiyohara /* Set SMBus command */
335 1.24 hannken if (cmdlen == 0) {
336 1.24 hannken if (len == 0)
337 1.19 pgoyette ctl = LPCIB_SMB_HC_CMD_QUICK;
338 1.19 pgoyette else
339 1.19 pgoyette ctl = LPCIB_SMB_HC_CMD_BYTE;
340 1.19 pgoyette } else if (len == 1)
341 1.5 xtraeme ctl = LPCIB_SMB_HC_CMD_BDATA;
342 1.1 kiyohara else if (len == 2)
343 1.5 xtraeme ctl = LPCIB_SMB_HC_CMD_WDATA;
344 1.1 kiyohara
345 1.1 kiyohara if ((flags & I2C_F_POLL) == 0)
346 1.5 xtraeme ctl |= LPCIB_SMB_HC_INTREN;
347 1.1 kiyohara
348 1.1 kiyohara /* Start transaction */
349 1.5 xtraeme ctl |= LPCIB_SMB_HC_START;
350 1.5 xtraeme bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HC, ctl);
351 1.1 kiyohara
352 1.1 kiyohara if (flags & I2C_F_POLL) {
353 1.1 kiyohara /* Poll for completion */
354 1.1 kiyohara DELAY(ICHIIC_DELAY);
355 1.1 kiyohara for (retries = 1000; retries > 0; retries--) {
356 1.1 kiyohara st = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
357 1.5 xtraeme LPCIB_SMB_HS);
358 1.5 xtraeme if ((st & LPCIB_SMB_HS_BUSY) == 0)
359 1.1 kiyohara break;
360 1.1 kiyohara DELAY(ICHIIC_DELAY);
361 1.1 kiyohara }
362 1.5 xtraeme if (st & LPCIB_SMB_HS_BUSY)
363 1.1 kiyohara goto timeout;
364 1.1 kiyohara ichsmb_intr(sc);
365 1.1 kiyohara } else {
366 1.1 kiyohara /* Wait for interrupt */
367 1.1 kiyohara if (tsleep(sc, PRIBIO, "iicexec", ICHIIC_TIMEOUT * hz))
368 1.1 kiyohara goto timeout;
369 1.1 kiyohara }
370 1.1 kiyohara
371 1.1 kiyohara if (sc->sc_i2c_xfer.error)
372 1.1 kiyohara return (1);
373 1.1 kiyohara
374 1.1 kiyohara return (0);
375 1.1 kiyohara
376 1.1 kiyohara timeout:
377 1.1 kiyohara /*
378 1.1 kiyohara * Transfer timeout. Kill the transaction and clear status bits.
379 1.1 kiyohara */
380 1.18 christos snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
381 1.12 kiyohara aprint_error_dev(sc->sc_dev,
382 1.12 kiyohara "exec: op %d, addr 0x%02x, cmdlen %zd, len %zd, "
383 1.37.4.7 skrll "flags 0x%02x: timeout, status %s\n",
384 1.12 kiyohara op, addr, cmdlen, len, flags, fbuf);
385 1.5 xtraeme bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HC,
386 1.5 xtraeme LPCIB_SMB_HC_KILL);
387 1.1 kiyohara DELAY(ICHIIC_DELAY);
388 1.5 xtraeme st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
389 1.5 xtraeme if ((st & LPCIB_SMB_HS_FAILED) == 0) {
390 1.18 christos snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
391 1.37.4.7 skrll aprint_error_dev(sc->sc_dev, "abort failed, status %s\n",
392 1.12 kiyohara fbuf);
393 1.4 kiyohara }
394 1.5 xtraeme bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, st);
395 1.1 kiyohara return (1);
396 1.1 kiyohara }
397 1.1 kiyohara
398 1.1 kiyohara static int
399 1.1 kiyohara ichsmb_intr(void *arg)
400 1.1 kiyohara {
401 1.1 kiyohara struct ichsmb_softc *sc = arg;
402 1.1 kiyohara uint8_t st;
403 1.1 kiyohara uint8_t *b;
404 1.1 kiyohara size_t len;
405 1.4 kiyohara #ifdef ICHIIC_DEBUG
406 1.4 kiyohara char fbuf[64];
407 1.4 kiyohara #endif
408 1.1 kiyohara
409 1.1 kiyohara /* Read status */
410 1.5 xtraeme st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
411 1.5 xtraeme if ((st & LPCIB_SMB_HS_BUSY) != 0 || (st & (LPCIB_SMB_HS_INTR |
412 1.5 xtraeme LPCIB_SMB_HS_DEVERR | LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED |
413 1.5 xtraeme LPCIB_SMB_HS_SMBAL | LPCIB_SMB_HS_BDONE)) == 0)
414 1.1 kiyohara /* Interrupt was not for us */
415 1.1 kiyohara return (0);
416 1.1 kiyohara
417 1.4 kiyohara #ifdef ICHIIC_DEBUG
418 1.18 christos snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
419 1.37.4.7 skrll printf("%s: intr st %s\n", device_xname(sc->sc_dev), fbuf);
420 1.4 kiyohara #endif
421 1.1 kiyohara
422 1.1 kiyohara /* Clear status bits */
423 1.5 xtraeme bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, st);
424 1.1 kiyohara
425 1.1 kiyohara /* Check for errors */
426 1.5 xtraeme if (st & (LPCIB_SMB_HS_DEVERR | LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED)) {
427 1.1 kiyohara sc->sc_i2c_xfer.error = 1;
428 1.1 kiyohara goto done;
429 1.1 kiyohara }
430 1.1 kiyohara
431 1.5 xtraeme if (st & LPCIB_SMB_HS_INTR) {
432 1.1 kiyohara if (I2C_OP_WRITE_P(sc->sc_i2c_xfer.op))
433 1.1 kiyohara goto done;
434 1.1 kiyohara
435 1.1 kiyohara /* Read data */
436 1.1 kiyohara b = sc->sc_i2c_xfer.buf;
437 1.1 kiyohara len = sc->sc_i2c_xfer.len;
438 1.1 kiyohara if (len > 0)
439 1.1 kiyohara b[0] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
440 1.5 xtraeme LPCIB_SMB_HD0);
441 1.1 kiyohara if (len > 1)
442 1.1 kiyohara b[1] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
443 1.5 xtraeme LPCIB_SMB_HD1);
444 1.1 kiyohara }
445 1.1 kiyohara
446 1.1 kiyohara done:
447 1.1 kiyohara if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0)
448 1.1 kiyohara wakeup(sc);
449 1.1 kiyohara return (1);
450 1.1 kiyohara }
451