xref: /netbsd-src/sys/dev/pci/ichsmb.c (revision 4b71a66d0f279143147d63ebfcfd8a59499a3684)
1 /*	$NetBSD: ichsmb.c,v 1.13 2008/04/10 19:13:36 cegger Exp $	*/
2 /*	$OpenBSD: ichiic.c,v 1.18 2007/05/03 09:36:26 dlg Exp $	*/
3 
4 /*
5  * Copyright (c) 2005, 2006 Alexander Yurchenko <grange@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.13 2008/04/10 19:13:36 cegger 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/rwlock.h>
32 #include <sys/proc.h>
33 
34 #include <sys/bus.h>
35 
36 #include <dev/pci/pcidevs.h>
37 #include <dev/pci/pcireg.h>
38 #include <dev/pci/pcivar.h>
39 
40 #include <dev/ic/i82801lpcreg.h>
41 
42 #include <dev/i2c/i2cvar.h>
43 
44 #ifdef ICHIIC_DEBUG
45 #define DPRINTF(x) printf x
46 #else
47 #define DPRINTF(x)
48 #endif
49 
50 #define ICHIIC_DELAY	100
51 #define ICHIIC_TIMEOUT	1
52 
53 struct ichsmb_softc {
54 	device_t		sc_dev;
55 
56 	bus_space_tag_t		sc_iot;
57 	bus_space_handle_t	sc_ioh;
58 	void *			sc_ih;
59 	int			sc_poll;
60 
61 	struct i2c_controller	sc_i2c_tag;
62 	krwlock_t 		sc_i2c_rwlock;
63 	struct {
64 		i2c_op_t     op;
65 		void *       buf;
66 		size_t       len;
67 		int          flags;
68 		volatile int error;
69 	}			sc_i2c_xfer;
70 };
71 
72 static int	ichsmb_match(device_t, struct cfdata *, void *);
73 static void	ichsmb_attach(device_t, device_t, void *);
74 
75 static int	ichsmb_i2c_acquire_bus(void *, int);
76 static void	ichsmb_i2c_release_bus(void *, int);
77 static int	ichsmb_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
78 		    size_t, void *, size_t, int);
79 
80 static int	ichsmb_intr(void *);
81 
82 
83 CFATTACH_DECL_NEW(ichsmb, sizeof(struct ichsmb_softc),
84     ichsmb_match, ichsmb_attach, NULL, NULL);
85 
86 
87 static int
88 ichsmb_match(device_t parent, struct cfdata *match, void *aux)
89 {
90 	struct pci_attach_args *pa = aux;
91 
92 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
93 		switch (PCI_PRODUCT(pa->pa_id)) {
94 		case PCI_PRODUCT_INTEL_6300ESB_SMB:
95 		case PCI_PRODUCT_INTEL_63XXESB_SMB:
96 		case PCI_PRODUCT_INTEL_82801AA_SMB:
97 		case PCI_PRODUCT_INTEL_82801AB_SMB:
98 		case PCI_PRODUCT_INTEL_82801BA_SMB:
99 		case PCI_PRODUCT_INTEL_82801CA_SMB:
100 		case PCI_PRODUCT_INTEL_82801DB_SMB:
101 		case PCI_PRODUCT_INTEL_82801E_SMB:
102 		case PCI_PRODUCT_INTEL_82801EB_SMB:
103 		case PCI_PRODUCT_INTEL_82801FB_SMB:
104 		case PCI_PRODUCT_INTEL_82801G_SMB:
105 		case PCI_PRODUCT_INTEL_82801H_SMB:
106 		case PCI_PRODUCT_INTEL_82801I_SMB:
107 			return 1;
108 		}
109 	}
110 	return 0;
111 }
112 
113 static void
114 ichsmb_attach(device_t parent, device_t self, void *aux)
115 {
116 	struct ichsmb_softc *sc = device_private(self);
117 	struct pci_attach_args *pa = aux;
118 	struct i2cbus_attach_args iba;
119 	pcireg_t conf;
120 	bus_size_t iosize;
121 	pci_intr_handle_t ih;
122 	const char *intrstr = NULL;
123 	char devinfo[256];
124 
125 	sc->sc_dev = self;
126 
127 	aprint_naive("\n");
128 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
129 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
130 	    PCI_REVISION(pa->pa_class));
131 
132 	/* Read configuration */
133 	conf = pci_conf_read(pa->pa_pc, pa->pa_tag, LPCIB_SMB_HOSTC);
134 	DPRINTF(("%s: conf 0x%08x", device_xname(&sc->sc_dev), conf));
135 
136 	if ((conf & LPCIB_SMB_HOSTC_HSTEN) == 0) {
137 		aprint_error_dev(self, "SMBus disabled\n");
138 		return;
139 	}
140 
141 	/* Map I/O space */
142 	if (pci_mapreg_map(pa, LPCIB_SMB_BASE, PCI_MAPREG_TYPE_IO, 0,
143 	    &sc->sc_iot, &sc->sc_ioh, NULL, &iosize)) {
144 		aprint_error_dev(self, "can't map I/O space\n");
145 		return;
146 	}
147 
148 	sc->sc_poll = 1;
149 	if (conf & LPCIB_SMB_HOSTC_SMIEN) {
150 		/* No PCI IRQ */
151 		aprint_normal_dev(self, "SMI\n");
152 	} else {
153 		/* Install interrupt handler */
154 		if (pci_intr_map(pa, &ih) == 0) {
155 			intrstr = pci_intr_string(pa->pa_pc, ih);
156 			sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
157 			    ichsmb_intr, sc);
158 			if (sc->sc_ih != NULL) {
159 				aprint_normal_dev(self, "interrupting at %s\n",
160 				    intrstr);
161 				sc->sc_poll = 0;
162 			}
163 		}
164 		if (sc->sc_poll)
165 			aprint_normal_dev(self, "polling\n");
166 	}
167 
168 	/* Attach I2C bus */
169 	rw_init(&sc->sc_i2c_rwlock);
170 	sc->sc_i2c_tag.ic_cookie = sc;
171 	sc->sc_i2c_tag.ic_acquire_bus = ichsmb_i2c_acquire_bus;
172 	sc->sc_i2c_tag.ic_release_bus = ichsmb_i2c_release_bus;
173 	sc->sc_i2c_tag.ic_exec = ichsmb_i2c_exec;
174 
175 	bzero(&iba, sizeof(iba));
176 	iba.iba_type = I2C_TYPE_SMBUS;
177 	iba.iba_tag = &sc->sc_i2c_tag;
178 	config_found(self, &iba, iicbus_print);
179 
180 	if (!pmf_device_register(self, NULL, NULL))
181 		aprint_error_dev(self, "couldn't establish power handler\n");
182 }
183 
184 static int
185 ichsmb_i2c_acquire_bus(void *cookie, int flags)
186 {
187 	struct ichsmb_softc *sc = cookie;
188 
189 	if (cold || sc->sc_poll || (flags & I2C_F_POLL))
190 		return 0;
191 
192 	rw_enter(&sc->sc_i2c_rwlock, RW_WRITER);
193 	return 0;
194 }
195 
196 static void
197 ichsmb_i2c_release_bus(void *cookie, int flags)
198 {
199 	struct ichsmb_softc *sc = cookie;
200 
201 	if (cold || sc->sc_poll || (flags & I2C_F_POLL))
202 		return;
203 
204 	rw_exit(&sc->sc_i2c_rwlock);
205 }
206 
207 static int
208 ichsmb_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
209     const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
210 {
211 	struct ichsmb_softc *sc = cookie;
212 	const uint8_t *b;
213 	uint8_t ctl = 0, st;
214 	int retries;
215 	char fbuf[64];
216 
217 	DPRINTF(("%s: exec: op %d, addr 0x%02x, cmdlen %zu, len %d, "
218 	    "flags 0x%02x\n", device_xname(&sc->sc_dev), op, addr, cmdlen,
219 	    len, flags));
220 
221 	/* Wait for bus to be idle */
222 	for (retries = 100; retries > 0; retries--) {
223 		st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
224 		if (!(st & LPCIB_SMB_HS_BUSY))
225 			break;
226 		DELAY(ICHIIC_DELAY);
227 	}
228 #ifdef ICHIIC_DEBUG
229 	bitmask_snprintf(st, LPCIB_SMB_HS_BITS, fbuf, sizeof(fbuf));
230 	printf("%s: exec: st 0x%s\n", device_private(sc->sc_dev), fbuf);
231 #endif
232 	if (st & LPCIB_SMB_HS_BUSY)
233 		return (1);
234 
235 	if (cold || sc->sc_poll)
236 		flags |= I2C_F_POLL;
237 
238 	if (!I2C_OP_STOP_P(op) || cmdlen > 1 || len > 2)
239 		return (1);
240 
241 	/* Setup transfer */
242 	sc->sc_i2c_xfer.op = op;
243 	sc->sc_i2c_xfer.buf = buf;
244 	sc->sc_i2c_xfer.len = len;
245 	sc->sc_i2c_xfer.flags = flags;
246 	sc->sc_i2c_xfer.error = 0;
247 
248 	/* Set slave address and transfer direction */
249 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_TXSLVA,
250 	    LPCIB_SMB_TXSLVA_ADDR(addr) |
251 	    (I2C_OP_READ_P(op) ? LPCIB_SMB_TXSLVA_READ : 0));
252 
253 	b = (const uint8_t *)cmdbuf;
254 	if (cmdlen > 0)
255 		/* Set command byte */
256 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HCMD, b[0]);
257 
258 	if (I2C_OP_WRITE_P(op)) {
259 		/* Write data */
260 		b = buf;
261 		if (len > 0)
262 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
263 			    LPCIB_SMB_HD0, b[0]);
264 		if (len > 1)
265 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
266 			    LPCIB_SMB_HD1, b[1]);
267 	}
268 
269 	/* Set SMBus command */
270 	if (len == 0)
271 		ctl = LPCIB_SMB_HC_CMD_BYTE;
272 	else if (len == 1)
273 		ctl = LPCIB_SMB_HC_CMD_BDATA;
274 	else if (len == 2)
275 		ctl = LPCIB_SMB_HC_CMD_WDATA;
276 
277 	if ((flags & I2C_F_POLL) == 0)
278 		ctl |= LPCIB_SMB_HC_INTREN;
279 
280 	/* Start transaction */
281 	ctl |= LPCIB_SMB_HC_START;
282 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HC, ctl);
283 
284 	if (flags & I2C_F_POLL) {
285 		/* Poll for completion */
286 		DELAY(ICHIIC_DELAY);
287 		for (retries = 1000; retries > 0; retries--) {
288 			st = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
289 			    LPCIB_SMB_HS);
290 			if ((st & LPCIB_SMB_HS_BUSY) == 0)
291 				break;
292 			DELAY(ICHIIC_DELAY);
293 		}
294 		if (st & LPCIB_SMB_HS_BUSY)
295 			goto timeout;
296 		ichsmb_intr(sc);
297 	} else {
298 		/* Wait for interrupt */
299 		if (tsleep(sc, PRIBIO, "iicexec", ICHIIC_TIMEOUT * hz))
300 			goto timeout;
301 	}
302 
303 	if (sc->sc_i2c_xfer.error)
304 		return (1);
305 
306 	return (0);
307 
308 timeout:
309 	/*
310 	 * Transfer timeout. Kill the transaction and clear status bits.
311 	 */
312 	bitmask_snprintf(st, LPCIB_SMB_HS_BITS, fbuf, sizeof(fbuf));
313 	aprint_error_dev(sc->sc_dev,
314 	    "exec: op %d, addr 0x%02x, cmdlen %zd, len %zd, "
315 	    "flags 0x%02x: timeout, status 0x%s\n",
316 	    op, addr, cmdlen, len, flags, fbuf);
317 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HC,
318 	    LPCIB_SMB_HC_KILL);
319 	DELAY(ICHIIC_DELAY);
320 	st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
321 	if ((st & LPCIB_SMB_HS_FAILED) == 0) {
322 		bitmask_snprintf(st, LPCIB_SMB_HS_BITS, fbuf, sizeof(fbuf));
323 		aprint_error_dev(sc->sc_dev, "abort failed, status 0x%s\n",
324 		    fbuf);
325 	}
326 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, st);
327 	return (1);
328 }
329 
330 static int
331 ichsmb_intr(void *arg)
332 {
333 	struct ichsmb_softc *sc = arg;
334 	uint8_t st;
335 	uint8_t *b;
336 	size_t len;
337 #ifdef ICHIIC_DEBUG
338 	char fbuf[64];
339 #endif
340 
341 	/* Read status */
342 	st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
343 	if ((st & LPCIB_SMB_HS_BUSY) != 0 || (st & (LPCIB_SMB_HS_INTR |
344 	    LPCIB_SMB_HS_DEVERR | LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED |
345 	    LPCIB_SMB_HS_SMBAL | LPCIB_SMB_HS_BDONE)) == 0)
346 		/* Interrupt was not for us */
347 		return (0);
348 
349 #ifdef ICHIIC_DEBUG
350 	bitmask_snprintf(st, LPCIB_SMB_HS_BITS, fbuf, sizeof(fbuf));
351 	printf("%s: intr st 0x%s\n", device_xname(sc->sc_dev), fbuf);
352 #endif
353 
354 	/* Clear status bits */
355 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, st);
356 
357 	/* Check for errors */
358 	if (st & (LPCIB_SMB_HS_DEVERR | LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED)) {
359 		sc->sc_i2c_xfer.error = 1;
360 		goto done;
361 	}
362 
363 	if (st & LPCIB_SMB_HS_INTR) {
364 		if (I2C_OP_WRITE_P(sc->sc_i2c_xfer.op))
365 			goto done;
366 
367 		/* Read data */
368 		b = sc->sc_i2c_xfer.buf;
369 		len = sc->sc_i2c_xfer.len;
370 		if (len > 0)
371 			b[0] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
372 			    LPCIB_SMB_HD0);
373 		if (len > 1)
374 			b[1] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
375 			    LPCIB_SMB_HD1);
376 	}
377 
378 done:
379 	if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0)
380 		wakeup(sc);
381 	return (1);
382 }
383