ichsmb.c revision 1.83 1 1.83 msaitoh /* $NetBSD: ichsmb.c,v 1.83 2023/05/30 04:14:30 msaitoh Exp $ */
2 1.69 thorpej /* $OpenBSD: ichiic.c,v 1.44 2020/10/07 11:23:05 jsg 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.83 msaitoh __KERNEL_RCSID(0, "$NetBSD: ichsmb.c,v 1.83 2023/05/30 04:14:30 msaitoh 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.65 thorpej #include <sys/condvar.h>
33 1.53 pgoyette #include <sys/module.h>
34 1.1 kiyohara
35 1.10 ad #include <sys/bus.h>
36 1.1 kiyohara
37 1.1 kiyohara #include <dev/pci/pcidevs.h>
38 1.1 kiyohara #include <dev/pci/pcireg.h>
39 1.1 kiyohara #include <dev/pci/pcivar.h>
40 1.1 kiyohara
41 1.5 xtraeme #include <dev/ic/i82801lpcreg.h>
42 1.1 kiyohara
43 1.1 kiyohara #include <dev/i2c/i2cvar.h>
44 1.1 kiyohara
45 1.82 riastrad #include <x86/pci/tco.h>
46 1.82 riastrad
47 1.1 kiyohara #ifdef ICHIIC_DEBUG
48 1.1 kiyohara #define DPRINTF(x) printf x
49 1.1 kiyohara #else
50 1.1 kiyohara #define DPRINTF(x)
51 1.1 kiyohara #endif
52 1.1 kiyohara
53 1.1 kiyohara #define ICHIIC_DELAY 100
54 1.1 kiyohara #define ICHIIC_TIMEOUT 1
55 1.1 kiyohara
56 1.1 kiyohara struct ichsmb_softc {
57 1.12 kiyohara device_t sc_dev;
58 1.1 kiyohara
59 1.1 kiyohara bus_space_tag_t sc_iot;
60 1.1 kiyohara bus_space_handle_t sc_ioh;
61 1.55 pgoyette bus_size_t sc_size;
62 1.55 pgoyette pci_chipset_tag_t sc_pc;
63 1.1 kiyohara void * sc_ih;
64 1.1 kiyohara int sc_poll;
65 1.58 jdolecek pci_intr_handle_t *sc_pihp;
66 1.1 kiyohara
67 1.65 thorpej kmutex_t sc_exec_lock;
68 1.65 thorpej kcondvar_t sc_exec_wait;
69 1.65 thorpej
70 1.1 kiyohara struct i2c_controller sc_i2c_tag;
71 1.1 kiyohara struct {
72 1.1 kiyohara i2c_op_t op;
73 1.1 kiyohara void * buf;
74 1.1 kiyohara size_t len;
75 1.1 kiyohara int flags;
76 1.65 thorpej int error;
77 1.65 thorpej bool done;
78 1.1 kiyohara } sc_i2c_xfer;
79 1.42 pgoyette device_t sc_i2c_device;
80 1.82 riastrad
81 1.82 riastrad bus_space_tag_t sc_tcot;
82 1.82 riastrad bus_space_handle_t sc_tcoh;
83 1.82 riastrad bus_size_t sc_tcosz;
84 1.82 riastrad bus_space_tag_t sc_sbregt;
85 1.82 riastrad bus_space_handle_t sc_sbregh;
86 1.82 riastrad bus_size_t sc_sbregsz;
87 1.82 riastrad bus_space_tag_t sc_pmt;
88 1.82 riastrad bus_space_handle_t sc_pmh;
89 1.82 riastrad bus_size_t sc_pmsz;
90 1.82 riastrad bool sc_tco_probed;
91 1.82 riastrad device_t sc_tco_device;
92 1.1 kiyohara };
93 1.1 kiyohara
94 1.21 cegger static int ichsmb_match(device_t, cfdata_t, void *);
95 1.12 kiyohara static void ichsmb_attach(device_t, device_t, void *);
96 1.55 pgoyette static int ichsmb_detach(device_t, int);
97 1.42 pgoyette static int ichsmb_rescan(device_t, const char *, const int *);
98 1.42 pgoyette static void ichsmb_chdet(device_t, device_t);
99 1.1 kiyohara
100 1.82 riastrad static void ichsmb_probe_tco(struct ichsmb_softc *,
101 1.82 riastrad const struct pci_attach_args *);
102 1.82 riastrad
103 1.1 kiyohara static int ichsmb_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
104 1.1 kiyohara size_t, void *, size_t, int);
105 1.1 kiyohara
106 1.1 kiyohara static int ichsmb_intr(void *);
107 1.1 kiyohara
108 1.53 pgoyette #include "ioconf.h"
109 1.1 kiyohara
110 1.42 pgoyette CFATTACH_DECL3_NEW(ichsmb, sizeof(struct ichsmb_softc),
111 1.55 pgoyette ichsmb_match, ichsmb_attach, ichsmb_detach, NULL, ichsmb_rescan,
112 1.59 jdolecek ichsmb_chdet, DVF_DETACH_SHUTDOWN);
113 1.1 kiyohara
114 1.1 kiyohara
115 1.1 kiyohara static int
116 1.21 cegger ichsmb_match(device_t parent, cfdata_t match, void *aux)
117 1.1 kiyohara {
118 1.1 kiyohara struct pci_attach_args *pa = aux;
119 1.1 kiyohara
120 1.1 kiyohara if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
121 1.1 kiyohara switch (PCI_PRODUCT(pa->pa_id)) {
122 1.1 kiyohara case PCI_PRODUCT_INTEL_6300ESB_SMB:
123 1.1 kiyohara case PCI_PRODUCT_INTEL_63XXESB_SMB:
124 1.1 kiyohara case PCI_PRODUCT_INTEL_82801AA_SMB:
125 1.1 kiyohara case PCI_PRODUCT_INTEL_82801AB_SMB:
126 1.1 kiyohara case PCI_PRODUCT_INTEL_82801BA_SMB:
127 1.1 kiyohara case PCI_PRODUCT_INTEL_82801CA_SMB:
128 1.1 kiyohara case PCI_PRODUCT_INTEL_82801DB_SMB:
129 1.1 kiyohara case PCI_PRODUCT_INTEL_82801E_SMB:
130 1.1 kiyohara case PCI_PRODUCT_INTEL_82801EB_SMB:
131 1.1 kiyohara case PCI_PRODUCT_INTEL_82801FB_SMB:
132 1.1 kiyohara case PCI_PRODUCT_INTEL_82801G_SMB:
133 1.1 kiyohara case PCI_PRODUCT_INTEL_82801H_SMB:
134 1.7 xtraeme case PCI_PRODUCT_INTEL_82801I_SMB:
135 1.23 njoly case PCI_PRODUCT_INTEL_82801JD_SMB:
136 1.23 njoly case PCI_PRODUCT_INTEL_82801JI_SMB:
137 1.22 tnn case PCI_PRODUCT_INTEL_3400_SMB:
138 1.25 msaitoh case PCI_PRODUCT_INTEL_6SERIES_SMB:
139 1.28 riastrad case PCI_PRODUCT_INTEL_7SERIES_SMB:
140 1.31 msaitoh case PCI_PRODUCT_INTEL_8SERIES_SMB:
141 1.39 msaitoh case PCI_PRODUCT_INTEL_9SERIES_SMB:
142 1.44 msaitoh case PCI_PRODUCT_INTEL_100SERIES_SMB:
143 1.49 msaitoh case PCI_PRODUCT_INTEL_100SERIES_LP_SMB:
144 1.48 msaitoh case PCI_PRODUCT_INTEL_2HS_SMB:
145 1.57 msaitoh case PCI_PRODUCT_INTEL_3HS_SMB:
146 1.68 msaitoh case PCI_PRODUCT_INTEL_3HS_U_SMB:
147 1.69 thorpej case PCI_PRODUCT_INTEL_4HS_H_SMB:
148 1.74 msaitoh case PCI_PRODUCT_INTEL_4HS_V_SMB:
149 1.33 msaitoh case PCI_PRODUCT_INTEL_CORE4G_M_SMB:
150 1.41 tnn case PCI_PRODUCT_INTEL_CORE5G_M_SMB:
151 1.67 msaitoh case PCI_PRODUCT_INTEL_CMTLK_SMB:
152 1.36 msaitoh case PCI_PRODUCT_INTEL_BAYTRAIL_PCU_SMB:
153 1.43 msaitoh case PCI_PRODUCT_INTEL_BSW_PCU_SMB:
154 1.56 msaitoh case PCI_PRODUCT_INTEL_APL_SMB:
155 1.56 msaitoh case PCI_PRODUCT_INTEL_GLK_SMB:
156 1.75 msaitoh case PCI_PRODUCT_INTEL_EHL_SMB:
157 1.71 msaitoh case PCI_PRODUCT_INTEL_JSL_SMB:
158 1.30 riastrad case PCI_PRODUCT_INTEL_C600_SMBUS:
159 1.29 msaitoh case PCI_PRODUCT_INTEL_C600_SMB_0:
160 1.29 msaitoh case PCI_PRODUCT_INTEL_C600_SMB_1:
161 1.29 msaitoh case PCI_PRODUCT_INTEL_C600_SMB_2:
162 1.40 msaitoh case PCI_PRODUCT_INTEL_C610_SMB:
163 1.52 msaitoh case PCI_PRODUCT_INTEL_C620_SMB:
164 1.52 msaitoh case PCI_PRODUCT_INTEL_C620_SMB_S:
165 1.36 msaitoh case PCI_PRODUCT_INTEL_EP80579_SMB:
166 1.38 msaitoh case PCI_PRODUCT_INTEL_DH89XXCC_SMB:
167 1.38 msaitoh case PCI_PRODUCT_INTEL_DH89XXCL_SMB:
168 1.34 msaitoh case PCI_PRODUCT_INTEL_C2000_PCU_SMBUS:
169 1.51 msaitoh case PCI_PRODUCT_INTEL_C3K_SMBUS_LEGACY:
170 1.69 thorpej case PCI_PRODUCT_INTEL_495_YU_SMB:
171 1.73 msaitoh case PCI_PRODUCT_INTEL_5HS_H_SMB:
172 1.69 thorpej case PCI_PRODUCT_INTEL_5HS_LP_SMB:
173 1.76 msaitoh case PCI_PRODUCT_INTEL_6HS_H_SMB:
174 1.77 msaitoh case PCI_PRODUCT_INTEL_6HS_LP_SMB:
175 1.1 kiyohara return 1;
176 1.1 kiyohara }
177 1.1 kiyohara }
178 1.1 kiyohara return 0;
179 1.1 kiyohara }
180 1.1 kiyohara
181 1.1 kiyohara static void
182 1.12 kiyohara ichsmb_attach(device_t parent, device_t self, void *aux)
183 1.1 kiyohara {
184 1.12 kiyohara struct ichsmb_softc *sc = device_private(self);
185 1.1 kiyohara struct pci_attach_args *pa = aux;
186 1.1 kiyohara pcireg_t conf;
187 1.1 kiyohara const char *intrstr = NULL;
188 1.35 christos char intrbuf[PCI_INTRSTR_LEN];
189 1.1 kiyohara
190 1.12 kiyohara sc->sc_dev = self;
191 1.55 pgoyette sc->sc_pc = pa->pa_pc;
192 1.12 kiyohara
193 1.26 drochner pci_aprint_devinfo(pa, NULL);
194 1.1 kiyohara
195 1.65 thorpej mutex_init(&sc->sc_exec_lock, MUTEX_DEFAULT, IPL_BIO);
196 1.65 thorpej cv_init(&sc->sc_exec_wait, device_xname(self));
197 1.65 thorpej
198 1.1 kiyohara /* Read configuration */
199 1.81 riastrad conf = pci_conf_read(pa->pa_pc, pa->pa_tag, SMB_HOSTC);
200 1.16 njoly DPRINTF(("%s: conf 0x%08x\n", device_xname(sc->sc_dev), conf));
201 1.1 kiyohara
202 1.81 riastrad if ((conf & SMB_HOSTC_HSTEN) == 0) {
203 1.12 kiyohara aprint_error_dev(self, "SMBus disabled\n");
204 1.37 riastrad goto out;
205 1.1 kiyohara }
206 1.1 kiyohara
207 1.1 kiyohara /* Map I/O space */
208 1.81 riastrad if (pci_mapreg_map(pa, SMB_BASE, PCI_MAPREG_TYPE_IO, 0,
209 1.55 pgoyette &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_size)) {
210 1.12 kiyohara aprint_error_dev(self, "can't map I/O space\n");
211 1.37 riastrad goto out;
212 1.1 kiyohara }
213 1.1 kiyohara
214 1.1 kiyohara sc->sc_poll = 1;
215 1.55 pgoyette sc->sc_ih = NULL;
216 1.81 riastrad if (conf & SMB_HOSTC_SMIEN) {
217 1.1 kiyohara /* No PCI IRQ */
218 1.15 njoly aprint_normal_dev(self, "interrupting at SMI\n");
219 1.1 kiyohara } else {
220 1.1 kiyohara /* Install interrupt handler */
221 1.58 jdolecek if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0) == 0) {
222 1.58 jdolecek intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0],
223 1.58 jdolecek intrbuf, sizeof(intrbuf));
224 1.65 thorpej pci_intr_setattr(pa->pa_pc, &sc->sc_pihp[0],
225 1.65 thorpej PCI_INTR_MPSAFE, true);
226 1.58 jdolecek sc->sc_ih = pci_intr_establish_xname(pa->pa_pc,
227 1.58 jdolecek sc->sc_pihp[0], IPL_BIO, ichsmb_intr, sc,
228 1.58 jdolecek device_xname(sc->sc_dev));
229 1.1 kiyohara if (sc->sc_ih != NULL) {
230 1.12 kiyohara aprint_normal_dev(self, "interrupting at %s\n",
231 1.12 kiyohara intrstr);
232 1.1 kiyohara sc->sc_poll = 0;
233 1.60 jdolecek } else {
234 1.60 jdolecek pci_intr_release(pa->pa_pc, sc->sc_pihp, 1);
235 1.60 jdolecek sc->sc_pihp = NULL;
236 1.1 kiyohara }
237 1.1 kiyohara }
238 1.1 kiyohara if (sc->sc_poll)
239 1.12 kiyohara aprint_normal_dev(self, "polling\n");
240 1.1 kiyohara }
241 1.1 kiyohara
242 1.78 riastrad /* Attach I2C bus */
243 1.78 riastrad iic_tag_init(&sc->sc_i2c_tag);
244 1.78 riastrad sc->sc_i2c_tag.ic_cookie = sc;
245 1.78 riastrad sc->sc_i2c_tag.ic_exec = ichsmb_i2c_exec;
246 1.78 riastrad
247 1.82 riastrad /*
248 1.82 riastrad * Probe to see if there's a TCO hanging here instead of the
249 1.82 riastrad * LPCIB and map it if we can.
250 1.82 riastrad */
251 1.82 riastrad ichsmb_probe_tco(sc, pa);
252 1.82 riastrad
253 1.42 pgoyette sc->sc_i2c_device = NULL;
254 1.70 thorpej ichsmb_rescan(self, NULL, NULL);
255 1.42 pgoyette
256 1.42 pgoyette out: if (!pmf_device_register(self, NULL, NULL))
257 1.42 pgoyette aprint_error_dev(self, "couldn't establish power handler\n");
258 1.42 pgoyette }
259 1.42 pgoyette
260 1.82 riastrad static void
261 1.82 riastrad ichsmb_probe_tco(struct ichsmb_softc *sc, const struct pci_attach_args *pa)
262 1.82 riastrad {
263 1.82 riastrad const device_t self = sc->sc_dev;
264 1.82 riastrad const pci_chipset_tag_t pc = sc->sc_pc;
265 1.82 riastrad const pcitag_t p2sb_tag = pci_make_tag(pc,
266 1.82 riastrad /*bus*/0, /*dev*/0x1f, /*fn*/1);
267 1.82 riastrad const pcitag_t pmc_tag = pci_make_tag(pc,
268 1.82 riastrad /*bus*/0, /*dev*/0x1f, /*fn*/2);
269 1.82 riastrad pcireg_t tcoctl, tcobase, p2sbc, sbreglo, sbreghi;
270 1.82 riastrad bus_addr_t sbreg, pmbase;
271 1.82 riastrad int error = EIO;
272 1.82 riastrad
273 1.82 riastrad /*
274 1.82 riastrad * Only attempt this on devices where we expect to find a TCO.
275 1.82 riastrad */
276 1.82 riastrad switch (PCI_PRODUCT(pa->pa_id)) {
277 1.82 riastrad case PCI_PRODUCT_INTEL_100SERIES_LP_SMB:
278 1.82 riastrad case PCI_PRODUCT_INTEL_100SERIES_SMB:
279 1.82 riastrad break;
280 1.82 riastrad default:
281 1.82 riastrad goto fail;
282 1.82 riastrad }
283 1.82 riastrad
284 1.82 riastrad /*
285 1.82 riastrad * Verify the TCO base address register is enabled.
286 1.82 riastrad */
287 1.82 riastrad tcoctl = pci_conf_read(pa->pa_pc, pa->pa_tag, SMB_TCOCTL);
288 1.82 riastrad aprint_debug_dev(self, "TCOCTL=0x%"PRIx32"\n", tcoctl);
289 1.82 riastrad if ((tcoctl & SMB_TCOCTL_TCO_BASE_EN) == 0) {
290 1.82 riastrad aprint_debug_dev(self, "TCO disabled\n");
291 1.82 riastrad goto fail;
292 1.82 riastrad }
293 1.82 riastrad
294 1.82 riastrad /*
295 1.82 riastrad * Verify the TCO base address register has the I/O space bit
296 1.82 riastrad * set -- otherwise we don't know how to interpret the
297 1.82 riastrad * register.
298 1.82 riastrad */
299 1.82 riastrad tcobase = pci_conf_read(pa->pa_pc, pa->pa_tag, SMB_TCOBASE);
300 1.82 riastrad aprint_debug_dev(self, "TCOBASE=0x%"PRIx32"\n", tcobase);
301 1.82 riastrad if ((tcobase & SMB_TCOBASE_IOS) == 0) {
302 1.82 riastrad aprint_error_dev(self, "unrecognized TCO space\n");
303 1.82 riastrad goto fail;
304 1.82 riastrad }
305 1.82 riastrad
306 1.82 riastrad /*
307 1.82 riastrad * Map the TCO I/O space.
308 1.82 riastrad */
309 1.82 riastrad sc->sc_tcot = sc->sc_iot;
310 1.82 riastrad error = bus_space_map(sc->sc_tcot, tcobase & SMB_TCOBASE_TCOBA,
311 1.82 riastrad TCO_REGSIZE, 0, &sc->sc_tcoh);
312 1.82 riastrad if (error) {
313 1.82 riastrad aprint_error_dev(self, "failed to map TCO: %d\n", error);
314 1.82 riastrad goto fail;
315 1.82 riastrad }
316 1.82 riastrad sc->sc_tcosz = TCO_REGSIZE;
317 1.82 riastrad
318 1.82 riastrad /*
319 1.82 riastrad * Clear the Hide Device bit so we can map the SBREG_BAR from
320 1.82 riastrad * the P2SB registers; then restore the Hide Device bit so
321 1.82 riastrad * nobody else gets confused.
322 1.82 riastrad *
323 1.82 riastrad * XXX Hope nobody else is trying to touch the P2SB!
324 1.82 riastrad *
325 1.82 riastrad * XXX Should we have a way to lock PCI bus enumeration,
326 1.82 riastrad * e.g. from concurrent drvctl rescan?
327 1.82 riastrad *
328 1.82 riastrad * XXX pci_mapreg_info doesn't work to get the size, somehow
329 1.82 riastrad * comes out as 4. Datasheet for 100-series chipset says the
330 1.82 riastrad * size is 16 MB, unconditionally, and the type is memory.
331 1.82 riastrad *
332 1.82 riastrad * XXX The above XXX comment was probably a result of PEBCAK
333 1.82 riastrad * when I tried to use 0xe4 instead of 0xe0 for P2SBC -- should
334 1.82 riastrad * try again with pci_mapreg_info or pci_mapreg_map.
335 1.82 riastrad */
336 1.82 riastrad p2sbc = pci_conf_read(pc, p2sb_tag, P2SB_P2SBC);
337 1.82 riastrad aprint_debug_dev(self, "P2SBC=0x%x\n", p2sbc);
338 1.82 riastrad pci_conf_write(pc, p2sb_tag, P2SB_P2SBC, p2sbc & ~P2SB_P2SBC_HIDE);
339 1.82 riastrad aprint_debug_dev(self, "P2SBC=0x%x -> 0x%x\n", p2sbc,
340 1.82 riastrad pci_conf_read(pc, p2sb_tag, P2SB_P2SBC));
341 1.82 riastrad sbreglo = pci_conf_read(pc, p2sb_tag, P2SB_SBREG_BAR);
342 1.82 riastrad sbreghi = pci_conf_read(pc, p2sb_tag, P2SB_SBREG_BARH);
343 1.82 riastrad aprint_debug_dev(self, "SBREG_BAR=0x%08x 0x%08x\n", sbreglo, sbreghi);
344 1.82 riastrad pci_conf_write(sc->sc_pc, p2sb_tag, P2SB_P2SBC, p2sbc);
345 1.82 riastrad
346 1.82 riastrad /*
347 1.82 riastrad * Map the sideband registers so we can touch the NO_REBOOT
348 1.82 riastrad * bit.
349 1.82 riastrad */
350 1.82 riastrad sbreg = ((uint64_t)sbreghi << 32) | (sbreglo & ~__BITS(0,3));
351 1.82 riastrad if (((uint64_t)sbreg >> 32) != sbreghi) {
352 1.82 riastrad /* paranoia for 32-bit non-PAE */
353 1.82 riastrad aprint_error_dev(self, "can't map 64-bit SBREG\n");
354 1.82 riastrad goto fail;
355 1.82 riastrad }
356 1.82 riastrad sc->sc_sbregt = pa->pa_memt;
357 1.82 riastrad error = bus_space_map(sc->sc_sbregt, sbreg + SB_SMBUS_BASE,
358 1.82 riastrad SB_SMBUS_SIZE, 0, &sc->sc_sbregh);
359 1.82 riastrad if (error) {
360 1.82 riastrad aprint_error_dev(self, "failed to map SMBUS sideband: %d\n",
361 1.82 riastrad error);
362 1.82 riastrad goto fail;
363 1.82 riastrad }
364 1.82 riastrad sc->sc_sbregsz = SB_SMBUS_SIZE;
365 1.82 riastrad
366 1.82 riastrad /*
367 1.82 riastrad * Map the power management configuration controller's I/O
368 1.82 riastrad * space. Older manual call this PMBASE for power management;
369 1.82 riastrad * newer manuals call it ABASE for ACPI. The chapters
370 1.82 riastrad * describing the registers say `power management' and I can't
371 1.82 riastrad * find any connection to ACPI (I suppose ACPI firmware logic
372 1.82 riastrad * probably peeks and pokes registers here?) so we say PMBASE
373 1.82 riastrad * here.
374 1.82 riastrad *
375 1.82 riastrad * XXX Hope nobody else is trying to touch it!
376 1.82 riastrad */
377 1.82 riastrad pmbase = pci_conf_read(pc, pmc_tag, LPCIB_PCI_PMBASE);
378 1.82 riastrad aprint_debug_dev(self, "PMBASE=0x%"PRIxBUSADDR"\n", pmbase);
379 1.82 riastrad if ((pmbase & 1) != 1) { /* I/O space bit? */
380 1.82 riastrad aprint_error_dev(self, "unrecognized PMC space\n");
381 1.82 riastrad goto fail;
382 1.82 riastrad }
383 1.82 riastrad sc->sc_pmt = sc->sc_iot;
384 1.82 riastrad error = bus_space_map(sc->sc_pmt, PCI_MAPREG_IO_ADDR(pmbase),
385 1.82 riastrad LPCIB_PCI_PM_SIZE, 0, &sc->sc_pmh);
386 1.82 riastrad if (error) {
387 1.82 riastrad aprint_error_dev(self, "failed to map PMC space: %d\n", error);
388 1.82 riastrad goto fail;
389 1.82 riastrad }
390 1.82 riastrad sc->sc_pmsz = LPCIB_PCI_PM_SIZE;
391 1.82 riastrad
392 1.82 riastrad /* Success! */
393 1.82 riastrad sc->sc_tco_probed = true;
394 1.82 riastrad return;
395 1.82 riastrad
396 1.82 riastrad fail: if (sc->sc_pmsz) {
397 1.82 riastrad bus_space_unmap(sc->sc_pmt, sc->sc_pmh, sc->sc_pmsz);
398 1.82 riastrad sc->sc_pmsz = 0;
399 1.82 riastrad }
400 1.82 riastrad if (sc->sc_sbregsz) {
401 1.82 riastrad bus_space_unmap(sc->sc_sbregt, sc->sc_sbregh, sc->sc_sbregsz);
402 1.82 riastrad sc->sc_sbregsz = 0;
403 1.82 riastrad }
404 1.82 riastrad if (sc->sc_tcosz) {
405 1.82 riastrad bus_space_unmap(sc->sc_tcot, sc->sc_tcoh, sc->sc_tcosz);
406 1.82 riastrad sc->sc_tcosz = 0;
407 1.82 riastrad }
408 1.82 riastrad }
409 1.82 riastrad
410 1.82 riastrad static int
411 1.82 riastrad ichsmb_tco_set_noreboot(device_t tco, bool noreboot)
412 1.82 riastrad {
413 1.82 riastrad device_t self = device_parent(tco);
414 1.82 riastrad struct ichsmb_softc *sc = device_private(self);
415 1.82 riastrad uint32_t gc, gc1;
416 1.82 riastrad
417 1.82 riastrad KASSERTMSG(tco == sc->sc_tco_device || sc->sc_tco_device == NULL,
418 1.82 riastrad "tco=%p child=%p", tco, sc->sc_tco_device);
419 1.82 riastrad KASSERTMSG(device_is_a(self, "ichsmb"), "%s@%s",
420 1.82 riastrad device_xname(tco), device_xname(self));
421 1.82 riastrad
422 1.82 riastrad /*
423 1.82 riastrad * Try to clear the No Reboot bit.
424 1.82 riastrad */
425 1.82 riastrad gc = bus_space_read_4(sc->sc_sbregt, sc->sc_sbregh, SB_SMBUS_GC);
426 1.82 riastrad if (noreboot)
427 1.82 riastrad gc |= SB_SMBUS_GC_NR;
428 1.82 riastrad else
429 1.82 riastrad gc &= ~SB_SMBUS_GC_NR;
430 1.82 riastrad bus_space_write_4(sc->sc_sbregt, sc->sc_sbregh, SB_SMBUS_GC, gc);
431 1.82 riastrad
432 1.82 riastrad /*
433 1.82 riastrad * Check whether we could make it what we want.
434 1.82 riastrad */
435 1.82 riastrad gc1 = bus_space_read_4(sc->sc_sbregt, sc->sc_sbregh, SB_SMBUS_GC);
436 1.82 riastrad aprint_debug_dev(self, "gc=0x%x -> 0x%x\n", gc, gc1);
437 1.82 riastrad if ((gc1 & SB_SMBUS_GC_NR) != (gc & SB_SMBUS_GC_NR))
438 1.82 riastrad return ENODEV;
439 1.82 riastrad return 0;
440 1.82 riastrad }
441 1.82 riastrad
442 1.42 pgoyette static int
443 1.70 thorpej ichsmb_rescan(device_t self, const char *ifattr, const int *locators)
444 1.42 pgoyette {
445 1.42 pgoyette struct ichsmb_softc *sc = device_private(self);
446 1.42 pgoyette
447 1.80 riastrad if (ifattr_match(ifattr, "i2cbus") && sc->sc_i2c_device == NULL) {
448 1.80 riastrad struct i2cbus_attach_args iba;
449 1.42 pgoyette
450 1.80 riastrad memset(&iba, 0, sizeof(iba));
451 1.80 riastrad iba.iba_tag = &sc->sc_i2c_tag;
452 1.80 riastrad sc->sc_i2c_device = config_found(self, &iba, iicbus_print,
453 1.82 riastrad CFARGS(.iattr = "i2cbus"));
454 1.82 riastrad }
455 1.82 riastrad if (sc->sc_tco_probed &&
456 1.82 riastrad ifattr_match(ifattr, "tcoichbus") &&
457 1.82 riastrad sc->sc_tco_device == NULL) {
458 1.82 riastrad struct tco_attach_args ta;
459 1.82 riastrad
460 1.82 riastrad memset(&ta, 0, sizeof(ta));
461 1.82 riastrad ta.ta_version = TCO_VERSION_SMBUS;
462 1.82 riastrad ta.ta_pmt = sc->sc_pmt;
463 1.82 riastrad ta.ta_pmh = sc->sc_pmh;
464 1.82 riastrad ta.ta_tcot = sc->sc_tcot;
465 1.82 riastrad ta.ta_tcoh = sc->sc_tcoh;
466 1.82 riastrad ta.ta_set_noreboot = &ichsmb_tco_set_noreboot;
467 1.82 riastrad sc->sc_tco_device = config_found(self, &ta, NULL,
468 1.82 riastrad CFARGS(.iattr = "tcoichbus"));
469 1.80 riastrad }
470 1.42 pgoyette
471 1.42 pgoyette return 0;
472 1.42 pgoyette }
473 1.42 pgoyette
474 1.55 pgoyette static int
475 1.55 pgoyette ichsmb_detach(device_t self, int flags)
476 1.55 pgoyette {
477 1.55 pgoyette struct ichsmb_softc *sc = device_private(self);
478 1.55 pgoyette int error;
479 1.55 pgoyette
480 1.79 riastrad error = config_detach_children(self, flags);
481 1.79 riastrad if (error)
482 1.79 riastrad return error;
483 1.55 pgoyette
484 1.63 thorpej iic_tag_fini(&sc->sc_i2c_tag);
485 1.55 pgoyette
486 1.58 jdolecek if (sc->sc_ih) {
487 1.55 pgoyette pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
488 1.58 jdolecek sc->sc_ih = NULL;
489 1.58 jdolecek }
490 1.58 jdolecek
491 1.58 jdolecek if (sc->sc_pihp) {
492 1.58 jdolecek pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
493 1.58 jdolecek sc->sc_pihp = NULL;
494 1.58 jdolecek }
495 1.55 pgoyette
496 1.82 riastrad if (sc->sc_pmsz != 0)
497 1.82 riastrad bus_space_unmap(sc->sc_pmt, sc->sc_pmh, sc->sc_pmsz);
498 1.82 riastrad if (sc->sc_sbregsz != 0)
499 1.82 riastrad bus_space_unmap(sc->sc_sbregt, sc->sc_sbregh, sc->sc_sbregsz);
500 1.82 riastrad if (sc->sc_tcosz != 0)
501 1.82 riastrad bus_space_unmap(sc->sc_tcot, sc->sc_tcoh, sc->sc_tcosz);
502 1.61 ad if (sc->sc_size != 0)
503 1.61 ad bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
504 1.55 pgoyette
505 1.65 thorpej mutex_destroy(&sc->sc_exec_lock);
506 1.65 thorpej cv_destroy(&sc->sc_exec_wait);
507 1.65 thorpej
508 1.55 pgoyette return 0;
509 1.55 pgoyette }
510 1.55 pgoyette
511 1.42 pgoyette static void
512 1.42 pgoyette ichsmb_chdet(device_t self, device_t child)
513 1.42 pgoyette {
514 1.42 pgoyette struct ichsmb_softc *sc = device_private(self);
515 1.42 pgoyette
516 1.42 pgoyette if (sc->sc_i2c_device == child)
517 1.42 pgoyette sc->sc_i2c_device = NULL;
518 1.82 riastrad if (sc->sc_tco_device == child)
519 1.82 riastrad sc->sc_tco_device = NULL;
520 1.1 kiyohara }
521 1.1 kiyohara
522 1.1 kiyohara static int
523 1.1 kiyohara ichsmb_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
524 1.1 kiyohara const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
525 1.1 kiyohara {
526 1.1 kiyohara struct ichsmb_softc *sc = cookie;
527 1.1 kiyohara const uint8_t *b;
528 1.1 kiyohara uint8_t ctl = 0, st;
529 1.1 kiyohara int retries;
530 1.4 kiyohara char fbuf[64];
531 1.1 kiyohara
532 1.14 njoly DPRINTF(("%s: exec: op %d, addr 0x%02x, cmdlen %zu, len %zu, "
533 1.14 njoly "flags 0x%02x\n", device_xname(sc->sc_dev), op, addr, cmdlen,
534 1.1 kiyohara len, flags));
535 1.1 kiyohara
536 1.65 thorpej mutex_enter(&sc->sc_exec_lock);
537 1.65 thorpej
538 1.32 soren /* Clear status bits */
539 1.81 riastrad bus_space_write_1(sc->sc_iot, sc->sc_ioh, SMB_HS,
540 1.81 riastrad SMB_HS_INTR | SMB_HS_DEVERR | SMB_HS_BUSERR | SMB_HS_FAILED);
541 1.81 riastrad bus_space_barrier(sc->sc_iot, sc->sc_ioh, SMB_HS, 1,
542 1.66 msaitoh BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
543 1.32 soren
544 1.1 kiyohara /* Wait for bus to be idle */
545 1.1 kiyohara for (retries = 100; retries > 0; retries--) {
546 1.81 riastrad st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SMB_HS);
547 1.81 riastrad if (!(st & SMB_HS_BUSY))
548 1.1 kiyohara break;
549 1.1 kiyohara DELAY(ICHIIC_DELAY);
550 1.1 kiyohara }
551 1.4 kiyohara #ifdef ICHIIC_DEBUG
552 1.81 riastrad snprintb(fbuf, sizeof(fbuf), SMB_HS_BITS, st);
553 1.50 msaitoh printf("%s: exec: st %s\n", device_xname(sc->sc_dev), fbuf);
554 1.4 kiyohara #endif
555 1.81 riastrad if (st & SMB_HS_BUSY) {
556 1.65 thorpej mutex_exit(&sc->sc_exec_lock);
557 1.65 thorpej return (EBUSY);
558 1.65 thorpej }
559 1.1 kiyohara
560 1.64 thorpej if (sc->sc_poll)
561 1.1 kiyohara flags |= I2C_F_POLL;
562 1.1 kiyohara
563 1.24 hannken if (!I2C_OP_STOP_P(op) || cmdlen > 1 || len > 2 ||
564 1.65 thorpej (cmdlen == 0 && len > 1)) {
565 1.65 thorpej mutex_exit(&sc->sc_exec_lock);
566 1.65 thorpej return (EINVAL);
567 1.65 thorpej }
568 1.1 kiyohara
569 1.1 kiyohara /* Setup transfer */
570 1.1 kiyohara sc->sc_i2c_xfer.op = op;
571 1.1 kiyohara sc->sc_i2c_xfer.buf = buf;
572 1.1 kiyohara sc->sc_i2c_xfer.len = len;
573 1.1 kiyohara sc->sc_i2c_xfer.flags = flags;
574 1.1 kiyohara sc->sc_i2c_xfer.error = 0;
575 1.65 thorpej sc->sc_i2c_xfer.done = false;
576 1.1 kiyohara
577 1.1 kiyohara /* Set slave address and transfer direction */
578 1.81 riastrad bus_space_write_1(sc->sc_iot, sc->sc_ioh, SMB_TXSLVA,
579 1.81 riastrad SMB_TXSLVA_ADDR(addr) |
580 1.81 riastrad (I2C_OP_READ_P(op) ? SMB_TXSLVA_READ : 0));
581 1.1 kiyohara
582 1.1 kiyohara b = (const uint8_t *)cmdbuf;
583 1.1 kiyohara if (cmdlen > 0)
584 1.1 kiyohara /* Set command byte */
585 1.81 riastrad bus_space_write_1(sc->sc_iot, sc->sc_ioh, SMB_HCMD, b[0]);
586 1.1 kiyohara
587 1.1 kiyohara if (I2C_OP_WRITE_P(op)) {
588 1.1 kiyohara /* Write data */
589 1.1 kiyohara b = buf;
590 1.24 hannken if (cmdlen == 0 && len == 1)
591 1.24 hannken bus_space_write_1(sc->sc_iot, sc->sc_ioh,
592 1.81 riastrad SMB_HCMD, b[0]);
593 1.24 hannken else if (len > 0)
594 1.1 kiyohara bus_space_write_1(sc->sc_iot, sc->sc_ioh,
595 1.81 riastrad SMB_HD0, b[0]);
596 1.1 kiyohara if (len > 1)
597 1.1 kiyohara bus_space_write_1(sc->sc_iot, sc->sc_ioh,
598 1.81 riastrad SMB_HD1, b[1]);
599 1.1 kiyohara }
600 1.1 kiyohara
601 1.1 kiyohara /* Set SMBus command */
602 1.24 hannken if (cmdlen == 0) {
603 1.24 hannken if (len == 0)
604 1.81 riastrad ctl = SMB_HC_CMD_QUICK;
605 1.19 pgoyette else
606 1.81 riastrad ctl = SMB_HC_CMD_BYTE;
607 1.19 pgoyette } else if (len == 1)
608 1.81 riastrad ctl = SMB_HC_CMD_BDATA;
609 1.1 kiyohara else if (len == 2)
610 1.81 riastrad ctl = SMB_HC_CMD_WDATA;
611 1.1 kiyohara
612 1.1 kiyohara if ((flags & I2C_F_POLL) == 0)
613 1.81 riastrad ctl |= SMB_HC_INTREN;
614 1.1 kiyohara
615 1.1 kiyohara /* Start transaction */
616 1.81 riastrad ctl |= SMB_HC_START;
617 1.81 riastrad bus_space_write_1(sc->sc_iot, sc->sc_ioh, SMB_HC, ctl);
618 1.1 kiyohara
619 1.1 kiyohara if (flags & I2C_F_POLL) {
620 1.1 kiyohara /* Poll for completion */
621 1.1 kiyohara DELAY(ICHIIC_DELAY);
622 1.1 kiyohara for (retries = 1000; retries > 0; retries--) {
623 1.81 riastrad st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SMB_HS);
624 1.81 riastrad if ((st & SMB_HS_BUSY) == 0)
625 1.1 kiyohara break;
626 1.1 kiyohara DELAY(ICHIIC_DELAY);
627 1.1 kiyohara }
628 1.81 riastrad if (st & SMB_HS_BUSY)
629 1.1 kiyohara goto timeout;
630 1.1 kiyohara ichsmb_intr(sc);
631 1.1 kiyohara } else {
632 1.1 kiyohara /* Wait for interrupt */
633 1.65 thorpej while (! sc->sc_i2c_xfer.done) {
634 1.65 thorpej if (cv_timedwait(&sc->sc_exec_wait, &sc->sc_exec_lock,
635 1.65 thorpej ICHIIC_TIMEOUT * hz))
636 1.65 thorpej goto timeout;
637 1.65 thorpej }
638 1.1 kiyohara }
639 1.1 kiyohara
640 1.65 thorpej int error = sc->sc_i2c_xfer.error;
641 1.65 thorpej mutex_exit(&sc->sc_exec_lock);
642 1.1 kiyohara
643 1.65 thorpej return (error);
644 1.1 kiyohara
645 1.1 kiyohara timeout:
646 1.1 kiyohara /*
647 1.1 kiyohara * Transfer timeout. Kill the transaction and clear status bits.
648 1.1 kiyohara */
649 1.81 riastrad bus_space_write_1(sc->sc_iot, sc->sc_ioh, SMB_HC, SMB_HC_KILL);
650 1.1 kiyohara DELAY(ICHIIC_DELAY);
651 1.81 riastrad st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SMB_HS);
652 1.81 riastrad if ((st & SMB_HS_FAILED) == 0) {
653 1.81 riastrad snprintb(fbuf, sizeof(fbuf), SMB_HS_BITS, st);
654 1.83 msaitoh device_printf(sc->sc_dev, "abort failed, status %s\n",
655 1.12 kiyohara fbuf);
656 1.4 kiyohara }
657 1.81 riastrad bus_space_write_1(sc->sc_iot, sc->sc_ioh, SMB_HS, st);
658 1.65 thorpej mutex_exit(&sc->sc_exec_lock);
659 1.65 thorpej return (ETIMEDOUT);
660 1.1 kiyohara }
661 1.1 kiyohara
662 1.1 kiyohara static int
663 1.1 kiyohara ichsmb_intr(void *arg)
664 1.1 kiyohara {
665 1.1 kiyohara struct ichsmb_softc *sc = arg;
666 1.1 kiyohara uint8_t st;
667 1.1 kiyohara uint8_t *b;
668 1.1 kiyohara size_t len;
669 1.4 kiyohara #ifdef ICHIIC_DEBUG
670 1.4 kiyohara char fbuf[64];
671 1.4 kiyohara #endif
672 1.1 kiyohara
673 1.1 kiyohara /* Read status */
674 1.81 riastrad st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SMB_HS);
675 1.69 thorpej
676 1.69 thorpej /* Clear status bits */
677 1.81 riastrad bus_space_write_1(sc->sc_iot, sc->sc_ioh, SMB_HS, st);
678 1.69 thorpej
679 1.69 thorpej /* XXX Ignore SMBALERT# for now */
680 1.81 riastrad if ((st & SMB_HS_BUSY) != 0 ||
681 1.81 riastrad (st & (SMB_HS_INTR | SMB_HS_DEVERR | SMB_HS_BUSERR |
682 1.81 riastrad SMB_HS_FAILED | SMB_HS_BDONE)) == 0)
683 1.1 kiyohara /* Interrupt was not for us */
684 1.1 kiyohara return (0);
685 1.1 kiyohara
686 1.4 kiyohara #ifdef ICHIIC_DEBUG
687 1.81 riastrad snprintb(fbuf, sizeof(fbuf), SMB_HS_BITS, st);
688 1.50 msaitoh printf("%s: intr st %s\n", device_xname(sc->sc_dev), fbuf);
689 1.4 kiyohara #endif
690 1.1 kiyohara
691 1.65 thorpej if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0)
692 1.65 thorpej mutex_enter(&sc->sc_exec_lock);
693 1.65 thorpej
694 1.1 kiyohara /* Check for errors */
695 1.81 riastrad if (st & (SMB_HS_DEVERR | SMB_HS_BUSERR | SMB_HS_FAILED)) {
696 1.65 thorpej sc->sc_i2c_xfer.error = EIO;
697 1.1 kiyohara goto done;
698 1.1 kiyohara }
699 1.1 kiyohara
700 1.81 riastrad if (st & SMB_HS_INTR) {
701 1.1 kiyohara if (I2C_OP_WRITE_P(sc->sc_i2c_xfer.op))
702 1.1 kiyohara goto done;
703 1.1 kiyohara
704 1.1 kiyohara /* Read data */
705 1.1 kiyohara b = sc->sc_i2c_xfer.buf;
706 1.1 kiyohara len = sc->sc_i2c_xfer.len;
707 1.1 kiyohara if (len > 0)
708 1.1 kiyohara b[0] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
709 1.81 riastrad SMB_HD0);
710 1.1 kiyohara if (len > 1)
711 1.1 kiyohara b[1] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
712 1.81 riastrad SMB_HD1);
713 1.1 kiyohara }
714 1.1 kiyohara
715 1.1 kiyohara done:
716 1.65 thorpej sc->sc_i2c_xfer.done = true;
717 1.65 thorpej if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0) {
718 1.65 thorpej cv_signal(&sc->sc_exec_wait);
719 1.65 thorpej mutex_exit(&sc->sc_exec_lock);
720 1.65 thorpej }
721 1.1 kiyohara return (1);
722 1.1 kiyohara }
723 1.53 pgoyette
724 1.54 pgoyette MODULE(MODULE_CLASS_DRIVER, ichsmb, "pci,iic");
725 1.53 pgoyette
726 1.53 pgoyette #ifdef _MODULE
727 1.53 pgoyette #include "ioconf.c"
728 1.53 pgoyette #endif
729 1.53 pgoyette
730 1.53 pgoyette static int
731 1.53 pgoyette ichsmb_modcmd(modcmd_t cmd, void *opaque)
732 1.53 pgoyette {
733 1.53 pgoyette int error = 0;
734 1.53 pgoyette
735 1.53 pgoyette switch (cmd) {
736 1.53 pgoyette case MODULE_CMD_INIT:
737 1.53 pgoyette #ifdef _MODULE
738 1.53 pgoyette error = config_init_component(cfdriver_ioconf_ichsmb,
739 1.53 pgoyette cfattach_ioconf_ichsmb, cfdata_ioconf_ichsmb);
740 1.53 pgoyette #endif
741 1.53 pgoyette break;
742 1.53 pgoyette case MODULE_CMD_FINI:
743 1.53 pgoyette #ifdef _MODULE
744 1.53 pgoyette error = config_fini_component(cfdriver_ioconf_ichsmb,
745 1.53 pgoyette cfattach_ioconf_ichsmb, cfdata_ioconf_ichsmb);
746 1.53 pgoyette #endif
747 1.53 pgoyette break;
748 1.53 pgoyette default:
749 1.53 pgoyette #ifdef _MODULE
750 1.53 pgoyette error = ENOTTY;
751 1.53 pgoyette #endif
752 1.53 pgoyette break;
753 1.53 pgoyette }
754 1.53 pgoyette
755 1.53 pgoyette return error;
756 1.53 pgoyette }
757