piixpm.c revision 1.14 1 1.14 martin /* $NetBSD: piixpm.c,v 1.14 2007/08/06 22:41:22 martin Exp $ */
2 1.1 jmcneill /* $OpenBSD: piixpm.c,v 1.20 2006/02/27 08:25:02 grange Exp $ */
3 1.1 jmcneill
4 1.1 jmcneill /*
5 1.1 jmcneill * Copyright (c) 2005, 2006 Alexander Yurchenko <grange (at) openbsd.org>
6 1.1 jmcneill *
7 1.1 jmcneill * Permission to use, copy, modify, and distribute this software for any
8 1.1 jmcneill * purpose with or without fee is hereby granted, provided that the above
9 1.1 jmcneill * copyright notice and this permission notice appear in all copies.
10 1.1 jmcneill *
11 1.1 jmcneill * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 jmcneill * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 jmcneill * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 jmcneill * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 jmcneill * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 jmcneill * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 jmcneill * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 jmcneill */
19 1.1 jmcneill
20 1.1 jmcneill /*
21 1.1 jmcneill * Intel PIIX and compatible Power Management controller driver.
22 1.1 jmcneill */
23 1.1 jmcneill
24 1.1 jmcneill #include <sys/param.h>
25 1.1 jmcneill #include <sys/systm.h>
26 1.1 jmcneill #include <sys/device.h>
27 1.1 jmcneill #include <sys/kernel.h>
28 1.1 jmcneill #include <sys/lock.h>
29 1.1 jmcneill #include <sys/proc.h>
30 1.1 jmcneill
31 1.1 jmcneill #include <machine/bus.h>
32 1.1 jmcneill
33 1.1 jmcneill #include <dev/pci/pcidevs.h>
34 1.1 jmcneill #include <dev/pci/pcireg.h>
35 1.1 jmcneill #include <dev/pci/pcivar.h>
36 1.1 jmcneill
37 1.1 jmcneill #include <dev/pci/piixpmreg.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <dev/i2c/i2cvar.h>
40 1.1 jmcneill
41 1.4 jmcneill #ifdef __HAVE_TIMECOUNTER
42 1.5 drochner #include <dev/ic/acpipmtimer.h>
43 1.4 jmcneill #endif
44 1.4 jmcneill
45 1.1 jmcneill #ifdef PIIXPM_DEBUG
46 1.1 jmcneill #define DPRINTF(x) printf x
47 1.1 jmcneill #else
48 1.1 jmcneill #define DPRINTF(x)
49 1.1 jmcneill #endif
50 1.1 jmcneill
51 1.1 jmcneill #define PIIXPM_DELAY 200
52 1.1 jmcneill #define PIIXPM_TIMEOUT 1
53 1.1 jmcneill
54 1.1 jmcneill struct piixpm_softc {
55 1.1 jmcneill struct device sc_dev;
56 1.1 jmcneill
57 1.4 jmcneill bus_space_tag_t sc_smb_iot;
58 1.4 jmcneill bus_space_handle_t sc_smb_ioh;
59 1.4 jmcneill void * sc_smb_ih;
60 1.1 jmcneill int sc_poll;
61 1.1 jmcneill
62 1.4 jmcneill bus_space_tag_t sc_pm_iot;
63 1.4 jmcneill bus_space_handle_t sc_pm_ioh;
64 1.4 jmcneill
65 1.3 jmcneill pci_chipset_tag_t sc_pc;
66 1.3 jmcneill pcitag_t sc_pcitag;
67 1.3 jmcneill
68 1.1 jmcneill struct i2c_controller sc_i2c_tag;
69 1.1 jmcneill struct lock sc_i2c_lock;
70 1.1 jmcneill struct {
71 1.1 jmcneill i2c_op_t op;
72 1.13 christos void * buf;
73 1.1 jmcneill size_t len;
74 1.1 jmcneill int flags;
75 1.1 jmcneill volatile int error;
76 1.1 jmcneill } sc_i2c_xfer;
77 1.3 jmcneill
78 1.3 jmcneill void * sc_powerhook;
79 1.3 jmcneill struct pci_conf_state sc_pciconf;
80 1.3 jmcneill pcireg_t sc_devact[2];
81 1.1 jmcneill };
82 1.1 jmcneill
83 1.1 jmcneill int piixpm_match(struct device *, struct cfdata *, void *);
84 1.1 jmcneill void piixpm_attach(struct device *, struct device *, void *);
85 1.1 jmcneill
86 1.3 jmcneill void piixpm_powerhook(int, void *);
87 1.3 jmcneill
88 1.1 jmcneill int piixpm_i2c_acquire_bus(void *, int);
89 1.1 jmcneill void piixpm_i2c_release_bus(void *, int);
90 1.1 jmcneill int piixpm_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
91 1.1 jmcneill void *, size_t, int);
92 1.1 jmcneill
93 1.1 jmcneill int piixpm_intr(void *);
94 1.1 jmcneill
95 1.1 jmcneill CFATTACH_DECL(piixpm, sizeof(struct piixpm_softc),
96 1.1 jmcneill piixpm_match, piixpm_attach, NULL, NULL);
97 1.1 jmcneill
98 1.1 jmcneill int
99 1.11 christos piixpm_match(struct device *parent, struct cfdata *match,
100 1.9 christos void *aux)
101 1.1 jmcneill {
102 1.1 jmcneill struct pci_attach_args *pa;
103 1.1 jmcneill
104 1.1 jmcneill pa = (struct pci_attach_args *)aux;
105 1.1 jmcneill switch (PCI_VENDOR(pa->pa_id)) {
106 1.1 jmcneill case PCI_VENDOR_INTEL:
107 1.1 jmcneill switch (PCI_PRODUCT(pa->pa_id)) {
108 1.1 jmcneill case PCI_PRODUCT_INTEL_82371AB_PMC:
109 1.1 jmcneill case PCI_PRODUCT_INTEL_82440MX_PMC:
110 1.1 jmcneill return 1;
111 1.1 jmcneill }
112 1.1 jmcneill break;
113 1.1 jmcneill case PCI_VENDOR_ATI:
114 1.1 jmcneill switch (PCI_PRODUCT(pa->pa_id)) {
115 1.1 jmcneill case PCI_PRODUCT_ATI_SB200_SMB:
116 1.10 toshii case PCI_PRODUCT_ATI_SB300_SMB:
117 1.10 toshii case PCI_PRODUCT_ATI_SB400_SMB:
118 1.1 jmcneill return 1;
119 1.1 jmcneill }
120 1.1 jmcneill break;
121 1.14 martin case PCI_VENDOR_SERVERWORKS:
122 1.14 martin switch (PCI_PRODUCT(pa->pa_id)) {
123 1.14 martin case PCI_PRODUCT_SERVERWORKS_OSB4:
124 1.14 martin case PCI_PRODUCT_SERVERWORKS_CSB5:
125 1.14 martin case PCI_PRODUCT_SERVERWORKS_CSB6:
126 1.14 martin case PCI_PRODUCT_SERVERWORKS_HT1000SB:
127 1.14 martin return 1;
128 1.14 martin }
129 1.1 jmcneill }
130 1.1 jmcneill
131 1.1 jmcneill return 0;
132 1.1 jmcneill }
133 1.1 jmcneill
134 1.1 jmcneill void
135 1.11 christos piixpm_attach(struct device *parent, struct device *self, void *aux)
136 1.1 jmcneill {
137 1.1 jmcneill struct piixpm_softc *sc = (struct piixpm_softc *)self;
138 1.1 jmcneill struct pci_attach_args *pa = aux;
139 1.1 jmcneill struct i2cbus_attach_args iba;
140 1.1 jmcneill pcireg_t base, conf;
141 1.5 drochner #ifdef __HAVE_TIMECOUNTER
142 1.5 drochner pcireg_t pmmisc;
143 1.5 drochner #endif
144 1.1 jmcneill pci_intr_handle_t ih;
145 1.12 uwe char devinfo[256];
146 1.1 jmcneill const char *intrstr = NULL;
147 1.1 jmcneill
148 1.3 jmcneill sc->sc_pc = pa->pa_pc;
149 1.3 jmcneill sc->sc_pcitag = pa->pa_tag;
150 1.3 jmcneill
151 1.3 jmcneill aprint_naive("\n");
152 1.12 uwe
153 1.12 uwe pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
154 1.12 uwe aprint_normal("\n%s: %s (rev. 0x%02x)\n",
155 1.12 uwe device_xname(self), devinfo, PCI_REVISION(pa->pa_class));
156 1.3 jmcneill
157 1.8 jmcneill sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
158 1.8 jmcneill piixpm_powerhook, sc);
159 1.3 jmcneill if (sc->sc_powerhook == NULL)
160 1.3 jmcneill aprint_error("%s: can't establish powerhook\n",
161 1.3 jmcneill sc->sc_dev.dv_xname);
162 1.3 jmcneill
163 1.1 jmcneill /* Read configuration */
164 1.1 jmcneill conf = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_SMB_HOSTC);
165 1.1 jmcneill DPRINTF((": conf 0x%x", conf));
166 1.1 jmcneill
167 1.4 jmcneill #ifdef __HAVE_TIMECOUNTER
168 1.5 drochner if ((PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL) ||
169 1.5 drochner (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_INTEL_82371AB_PMC))
170 1.5 drochner goto nopowermanagement;
171 1.5 drochner
172 1.5 drochner /* check whether I/O access to PM regs is enabled */
173 1.5 drochner pmmisc = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_PMREGMISC);
174 1.5 drochner if (!(pmmisc & 1))
175 1.5 drochner goto nopowermanagement;
176 1.5 drochner
177 1.4 jmcneill sc->sc_pm_iot = pa->pa_iot;
178 1.4 jmcneill /* Map I/O space */
179 1.5 drochner base = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_PM_BASE);
180 1.4 jmcneill if (bus_space_map(sc->sc_pm_iot, PCI_MAPREG_IO_ADDR(base),
181 1.4 jmcneill PIIX_PM_SIZE, 0, &sc->sc_pm_ioh)) {
182 1.4 jmcneill aprint_error("%s: can't map power management I/O space\n",
183 1.4 jmcneill sc->sc_dev.dv_xname);
184 1.4 jmcneill goto nopowermanagement;
185 1.4 jmcneill }
186 1.4 jmcneill
187 1.5 drochner /*
188 1.5 drochner * Revision 0 and 1 are PIIX4, 2 is PIIX4E, 3 is PIIX4M.
189 1.5 drochner * PIIX4 and PIIX4E have a bug in the timer latch, see Errata #20
190 1.5 drochner * in the "Specification update" (document #297738).
191 1.5 drochner */
192 1.5 drochner acpipmtimer_attach(&sc->sc_dev, sc->sc_pm_iot, sc->sc_pm_ioh,
193 1.5 drochner PIIX_PM_PMTMR,
194 1.5 drochner (PCI_REVISION(pa->pa_class) < 3) ? ACPIPMT_BADLATCH : 0 );
195 1.4 jmcneill
196 1.5 drochner nopowermanagement:
197 1.4 jmcneill #endif
198 1.4 jmcneill
199 1.1 jmcneill if ((conf & PIIX_SMB_HOSTC_HSTEN) == 0) {
200 1.3 jmcneill aprint_normal("%s: SMBus disabled\n", sc->sc_dev.dv_xname);
201 1.1 jmcneill return;
202 1.1 jmcneill }
203 1.1 jmcneill
204 1.1 jmcneill /* Map I/O space */
205 1.4 jmcneill sc->sc_smb_iot = pa->pa_iot;
206 1.1 jmcneill base = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_SMB_BASE) & 0xffff;
207 1.4 jmcneill if (bus_space_map(sc->sc_smb_iot, PCI_MAPREG_IO_ADDR(base),
208 1.4 jmcneill PIIX_SMB_SIZE, 0, &sc->sc_smb_ioh)) {
209 1.4 jmcneill aprint_error("%s: can't map smbus I/O space\n",
210 1.3 jmcneill sc->sc_dev.dv_xname);
211 1.1 jmcneill return;
212 1.1 jmcneill }
213 1.1 jmcneill
214 1.1 jmcneill sc->sc_poll = 1;
215 1.1 jmcneill if ((conf & PIIX_SMB_HOSTC_INTMASK) == PIIX_SMB_HOSTC_SMI) {
216 1.1 jmcneill /* No PCI IRQ */
217 1.3 jmcneill aprint_normal("%s: interrupting at SMI", sc->sc_dev.dv_xname);
218 1.1 jmcneill } else if ((conf & PIIX_SMB_HOSTC_INTMASK) == PIIX_SMB_HOSTC_IRQ) {
219 1.1 jmcneill /* Install interrupt handler */
220 1.1 jmcneill if (pci_intr_map(pa, &ih) == 0) {
221 1.1 jmcneill intrstr = pci_intr_string(pa->pa_pc, ih);
222 1.4 jmcneill sc->sc_smb_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
223 1.1 jmcneill piixpm_intr, sc);
224 1.4 jmcneill if (sc->sc_smb_ih != NULL) {
225 1.3 jmcneill aprint_normal("%s: interrupting at %s",
226 1.3 jmcneill sc->sc_dev.dv_xname, intrstr);
227 1.1 jmcneill sc->sc_poll = 0;
228 1.1 jmcneill }
229 1.1 jmcneill }
230 1.1 jmcneill if (sc->sc_poll)
231 1.3 jmcneill aprint_normal("%s: polling", sc->sc_dev.dv_xname);
232 1.1 jmcneill }
233 1.1 jmcneill
234 1.3 jmcneill aprint_normal("\n");
235 1.1 jmcneill
236 1.1 jmcneill /* Attach I2C bus */
237 1.1 jmcneill lockinit(&sc->sc_i2c_lock, PRIBIO | PCATCH, "iiclk", 0, 0);
238 1.1 jmcneill sc->sc_i2c_tag.ic_cookie = sc;
239 1.1 jmcneill sc->sc_i2c_tag.ic_acquire_bus = piixpm_i2c_acquire_bus;
240 1.1 jmcneill sc->sc_i2c_tag.ic_release_bus = piixpm_i2c_release_bus;
241 1.1 jmcneill sc->sc_i2c_tag.ic_exec = piixpm_i2c_exec;
242 1.1 jmcneill
243 1.1 jmcneill bzero(&iba, sizeof(iba));
244 1.1 jmcneill iba.iba_tag = &sc->sc_i2c_tag;
245 1.6 drochner config_found_ia(self, "i2cbus", &iba, iicbus_print);
246 1.1 jmcneill
247 1.1 jmcneill return;
248 1.1 jmcneill }
249 1.1 jmcneill
250 1.3 jmcneill void
251 1.3 jmcneill piixpm_powerhook(int why, void *cookie)
252 1.3 jmcneill {
253 1.3 jmcneill struct piixpm_softc *sc = cookie;
254 1.3 jmcneill pci_chipset_tag_t pc = sc->sc_pc;
255 1.3 jmcneill pcitag_t tag = sc->sc_pcitag;
256 1.3 jmcneill
257 1.3 jmcneill switch (why) {
258 1.3 jmcneill case PWR_SUSPEND:
259 1.3 jmcneill pci_conf_capture(pc, tag, &sc->sc_pciconf);
260 1.3 jmcneill sc->sc_devact[0] = pci_conf_read(pc, tag, PIIX_DEVACTA);
261 1.3 jmcneill sc->sc_devact[1] = pci_conf_read(pc, tag, PIIX_DEVACTB);
262 1.3 jmcneill break;
263 1.3 jmcneill case PWR_RESUME:
264 1.3 jmcneill pci_conf_restore(pc, tag, &sc->sc_pciconf);
265 1.3 jmcneill pci_conf_write(pc, tag, PIIX_DEVACTA, sc->sc_devact[0]);
266 1.3 jmcneill pci_conf_write(pc, tag, PIIX_DEVACTB, sc->sc_devact[1]);
267 1.3 jmcneill break;
268 1.3 jmcneill }
269 1.3 jmcneill
270 1.3 jmcneill return;
271 1.3 jmcneill }
272 1.3 jmcneill
273 1.1 jmcneill int
274 1.1 jmcneill piixpm_i2c_acquire_bus(void *cookie, int flags)
275 1.1 jmcneill {
276 1.1 jmcneill struct piixpm_softc *sc = cookie;
277 1.1 jmcneill
278 1.1 jmcneill if (cold || sc->sc_poll || (flags & I2C_F_POLL))
279 1.1 jmcneill return (0);
280 1.1 jmcneill
281 1.1 jmcneill return (lockmgr(&sc->sc_i2c_lock, LK_EXCLUSIVE, NULL));
282 1.1 jmcneill }
283 1.1 jmcneill
284 1.1 jmcneill void
285 1.1 jmcneill piixpm_i2c_release_bus(void *cookie, int flags)
286 1.1 jmcneill {
287 1.1 jmcneill struct piixpm_softc *sc = cookie;
288 1.1 jmcneill
289 1.1 jmcneill if (cold || sc->sc_poll || (flags & I2C_F_POLL))
290 1.1 jmcneill return;
291 1.1 jmcneill
292 1.1 jmcneill lockmgr(&sc->sc_i2c_lock, LK_RELEASE, NULL);
293 1.1 jmcneill }
294 1.1 jmcneill
295 1.1 jmcneill int
296 1.1 jmcneill piixpm_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
297 1.1 jmcneill const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
298 1.1 jmcneill {
299 1.1 jmcneill struct piixpm_softc *sc = cookie;
300 1.1 jmcneill const u_int8_t *b;
301 1.1 jmcneill u_int8_t ctl = 0, st;
302 1.1 jmcneill int retries;
303 1.1 jmcneill
304 1.1 jmcneill DPRINTF(("%s: exec: op %d, addr 0x%x, cmdlen %d, len %d, flags 0x%x\n",
305 1.1 jmcneill sc->sc_dev.dv_xname, op, addr, cmdlen, len, flags));
306 1.1 jmcneill
307 1.1 jmcneill /* Wait for bus to be idle */
308 1.1 jmcneill for (retries = 100; retries > 0; retries--) {
309 1.4 jmcneill st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
310 1.4 jmcneill PIIX_SMB_HS);
311 1.1 jmcneill if (!(st & PIIX_SMB_HS_BUSY))
312 1.1 jmcneill break;
313 1.1 jmcneill DELAY(PIIXPM_DELAY);
314 1.1 jmcneill }
315 1.7 christos DPRINTF(("%s: exec: st 0x%d\n", sc->sc_dev.dv_xname, st & 0xff));
316 1.1 jmcneill if (st & PIIX_SMB_HS_BUSY)
317 1.1 jmcneill return (1);
318 1.1 jmcneill
319 1.1 jmcneill if (cold || sc->sc_poll)
320 1.1 jmcneill flags |= I2C_F_POLL;
321 1.1 jmcneill
322 1.1 jmcneill if (!I2C_OP_STOP_P(op) || cmdlen > 1 || len > 2)
323 1.1 jmcneill return (1);
324 1.1 jmcneill
325 1.1 jmcneill /* Setup transfer */
326 1.1 jmcneill sc->sc_i2c_xfer.op = op;
327 1.1 jmcneill sc->sc_i2c_xfer.buf = buf;
328 1.1 jmcneill sc->sc_i2c_xfer.len = len;
329 1.1 jmcneill sc->sc_i2c_xfer.flags = flags;
330 1.1 jmcneill sc->sc_i2c_xfer.error = 0;
331 1.1 jmcneill
332 1.1 jmcneill /* Set slave address and transfer direction */
333 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_TXSLVA,
334 1.1 jmcneill PIIX_SMB_TXSLVA_ADDR(addr) |
335 1.1 jmcneill (I2C_OP_READ_P(op) ? PIIX_SMB_TXSLVA_READ : 0));
336 1.1 jmcneill
337 1.1 jmcneill b = cmdbuf;
338 1.1 jmcneill if (cmdlen > 0)
339 1.1 jmcneill /* Set command byte */
340 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh,
341 1.4 jmcneill PIIX_SMB_HCMD, b[0]);
342 1.1 jmcneill
343 1.1 jmcneill if (I2C_OP_WRITE_P(op)) {
344 1.1 jmcneill /* Write data */
345 1.1 jmcneill b = buf;
346 1.1 jmcneill if (len > 0)
347 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh,
348 1.1 jmcneill PIIX_SMB_HD0, b[0]);
349 1.1 jmcneill if (len > 1)
350 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh,
351 1.1 jmcneill PIIX_SMB_HD1, b[1]);
352 1.1 jmcneill }
353 1.1 jmcneill
354 1.1 jmcneill /* Set SMBus command */
355 1.1 jmcneill if (len == 0)
356 1.1 jmcneill ctl = PIIX_SMB_HC_CMD_BYTE;
357 1.1 jmcneill else if (len == 1)
358 1.1 jmcneill ctl = PIIX_SMB_HC_CMD_BDATA;
359 1.1 jmcneill else if (len == 2)
360 1.1 jmcneill ctl = PIIX_SMB_HC_CMD_WDATA;
361 1.1 jmcneill
362 1.1 jmcneill if ((flags & I2C_F_POLL) == 0)
363 1.1 jmcneill ctl |= PIIX_SMB_HC_INTREN;
364 1.1 jmcneill
365 1.1 jmcneill /* Start transaction */
366 1.1 jmcneill ctl |= PIIX_SMB_HC_START;
367 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HC, ctl);
368 1.1 jmcneill
369 1.1 jmcneill if (flags & I2C_F_POLL) {
370 1.1 jmcneill /* Poll for completion */
371 1.1 jmcneill DELAY(PIIXPM_DELAY);
372 1.1 jmcneill for (retries = 1000; retries > 0; retries--) {
373 1.4 jmcneill st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
374 1.1 jmcneill PIIX_SMB_HS);
375 1.1 jmcneill if ((st & PIIX_SMB_HS_BUSY) == 0)
376 1.1 jmcneill break;
377 1.1 jmcneill DELAY(PIIXPM_DELAY);
378 1.1 jmcneill }
379 1.1 jmcneill if (st & PIIX_SMB_HS_BUSY)
380 1.1 jmcneill goto timeout;
381 1.1 jmcneill piixpm_intr(sc);
382 1.1 jmcneill } else {
383 1.1 jmcneill /* Wait for interrupt */
384 1.1 jmcneill if (tsleep(sc, PRIBIO, "iicexec", PIIXPM_TIMEOUT * hz))
385 1.1 jmcneill goto timeout;
386 1.1 jmcneill }
387 1.1 jmcneill
388 1.1 jmcneill if (sc->sc_i2c_xfer.error)
389 1.1 jmcneill return (1);
390 1.1 jmcneill
391 1.1 jmcneill return (0);
392 1.1 jmcneill
393 1.1 jmcneill timeout:
394 1.1 jmcneill /*
395 1.1 jmcneill * Transfer timeout. Kill the transaction and clear status bits.
396 1.1 jmcneill */
397 1.3 jmcneill aprint_error("%s: timeout, status 0x%x\n", sc->sc_dev.dv_xname, st);
398 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HC,
399 1.1 jmcneill PIIX_SMB_HC_KILL);
400 1.1 jmcneill DELAY(PIIXPM_DELAY);
401 1.4 jmcneill st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS);
402 1.1 jmcneill if ((st & PIIX_SMB_HS_FAILED) == 0)
403 1.3 jmcneill aprint_error("%s: transaction abort failed, status 0x%x\n",
404 1.1 jmcneill sc->sc_dev.dv_xname, st);
405 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, st);
406 1.1 jmcneill return (1);
407 1.1 jmcneill }
408 1.1 jmcneill
409 1.1 jmcneill int
410 1.1 jmcneill piixpm_intr(void *arg)
411 1.1 jmcneill {
412 1.1 jmcneill struct piixpm_softc *sc = arg;
413 1.1 jmcneill u_int8_t st;
414 1.1 jmcneill u_int8_t *b;
415 1.1 jmcneill size_t len;
416 1.1 jmcneill
417 1.1 jmcneill /* Read status */
418 1.4 jmcneill st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS);
419 1.1 jmcneill if ((st & PIIX_SMB_HS_BUSY) != 0 || (st & (PIIX_SMB_HS_INTR |
420 1.1 jmcneill PIIX_SMB_HS_DEVERR | PIIX_SMB_HS_BUSERR |
421 1.1 jmcneill PIIX_SMB_HS_FAILED)) == 0)
422 1.1 jmcneill /* Interrupt was not for us */
423 1.1 jmcneill return (0);
424 1.1 jmcneill
425 1.7 christos DPRINTF(("%s: intr st 0x%d\n", sc->sc_dev.dv_xname, st & 0xff));
426 1.1 jmcneill
427 1.1 jmcneill /* Clear status bits */
428 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, st);
429 1.1 jmcneill
430 1.1 jmcneill /* Check for errors */
431 1.1 jmcneill if (st & (PIIX_SMB_HS_DEVERR | PIIX_SMB_HS_BUSERR |
432 1.1 jmcneill PIIX_SMB_HS_FAILED)) {
433 1.1 jmcneill sc->sc_i2c_xfer.error = 1;
434 1.1 jmcneill goto done;
435 1.1 jmcneill }
436 1.1 jmcneill
437 1.1 jmcneill if (st & PIIX_SMB_HS_INTR) {
438 1.1 jmcneill if (I2C_OP_WRITE_P(sc->sc_i2c_xfer.op))
439 1.1 jmcneill goto done;
440 1.1 jmcneill
441 1.1 jmcneill /* Read data */
442 1.1 jmcneill b = sc->sc_i2c_xfer.buf;
443 1.1 jmcneill len = sc->sc_i2c_xfer.len;
444 1.1 jmcneill if (len > 0)
445 1.4 jmcneill b[0] = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
446 1.1 jmcneill PIIX_SMB_HD0);
447 1.1 jmcneill if (len > 1)
448 1.4 jmcneill b[1] = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
449 1.1 jmcneill PIIX_SMB_HD1);
450 1.1 jmcneill }
451 1.1 jmcneill
452 1.1 jmcneill done:
453 1.1 jmcneill if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0)
454 1.1 jmcneill wakeup(sc);
455 1.1 jmcneill return (1);
456 1.1 jmcneill }
457