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