piixpm.c revision 1.59 1 1.59 msaitoh /* $NetBSD: piixpm.c,v 1.59 2019/12/24 03:43:34 msaitoh Exp $ */
2 1.54 msaitoh /* $OpenBSD: piixpm.c,v 1.39 2013/10/01 20:06:02 sf 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.19 lukem #include <sys/cdefs.h>
25 1.59 msaitoh __KERNEL_RCSID(0, "$NetBSD: piixpm.c,v 1.59 2019/12/24 03:43:34 msaitoh Exp $");
26 1.19 lukem
27 1.1 jmcneill #include <sys/param.h>
28 1.1 jmcneill #include <sys/systm.h>
29 1.1 jmcneill #include <sys/device.h>
30 1.1 jmcneill #include <sys/kernel.h>
31 1.40 pgoyette #include <sys/mutex.h>
32 1.1 jmcneill #include <sys/proc.h>
33 1.1 jmcneill
34 1.17 ad #include <sys/bus.h>
35 1.1 jmcneill
36 1.1 jmcneill #include <dev/pci/pcidevs.h>
37 1.1 jmcneill #include <dev/pci/pcireg.h>
38 1.1 jmcneill #include <dev/pci/pcivar.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/pci/piixpmreg.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/i2c/i2cvar.h>
43 1.1 jmcneill
44 1.5 drochner #include <dev/ic/acpipmtimer.h>
45 1.4 jmcneill
46 1.1 jmcneill #ifdef PIIXPM_DEBUG
47 1.1 jmcneill #define DPRINTF(x) printf x
48 1.1 jmcneill #else
49 1.1 jmcneill #define DPRINTF(x)
50 1.1 jmcneill #endif
51 1.1 jmcneill
52 1.54 msaitoh #define PIIXPM_IS_CSB5(sc) \
53 1.54 msaitoh (PCI_VENDOR((sc)->sc_id) == PCI_VENDOR_SERVERWORKS && \
54 1.54 msaitoh PCI_PRODUCT((sc)->sc_id) == PCI_PRODUCT_SERVERWORKS_CSB5)
55 1.1 jmcneill #define PIIXPM_DELAY 200
56 1.1 jmcneill #define PIIXPM_TIMEOUT 1
57 1.1 jmcneill
58 1.54 msaitoh #define PIIXPM_IS_SB800GRP(sc) \
59 1.54 msaitoh ((PCI_VENDOR((sc)->sc_id) == PCI_VENDOR_ATI) && \
60 1.54 msaitoh ((PCI_PRODUCT((sc)->sc_id) == PCI_PRODUCT_ATI_SB600_SMB) && \
61 1.54 msaitoh ((sc)->sc_rev >= 0x40)))
62 1.54 msaitoh
63 1.54 msaitoh #define PIIXPM_IS_HUDSON(sc) \
64 1.54 msaitoh ((PCI_VENDOR((sc)->sc_id) == PCI_VENDOR_AMD) && \
65 1.54 msaitoh (PCI_PRODUCT((sc)->sc_id) == PCI_PRODUCT_AMD_HUDSON_SMB))
66 1.54 msaitoh
67 1.54 msaitoh #define PIIXPM_IS_KERNCZ(sc) \
68 1.54 msaitoh ((PCI_VENDOR((sc)->sc_id) == PCI_VENDOR_AMD) && \
69 1.54 msaitoh (PCI_PRODUCT((sc)->sc_id) == PCI_PRODUCT_AMD_KERNCZ_SMB))
70 1.54 msaitoh
71 1.54 msaitoh #define PIIXPM_IS_FCHGRP(sc) (PIIXPM_IS_HUDSON(sc) || PIIXPM_IS_KERNCZ(sc))
72 1.54 msaitoh
73 1.42 soren struct piixpm_smbus {
74 1.42 soren int sda;
75 1.42 soren struct piixpm_softc *softc;
76 1.42 soren };
77 1.36 jmcneill
78 1.1 jmcneill struct piixpm_softc {
79 1.25 joerg device_t sc_dev;
80 1.1 jmcneill
81 1.42 soren bus_space_tag_t sc_iot;
82 1.42 soren #define sc_pm_iot sc_iot
83 1.42 soren #define sc_smb_iot sc_iot
84 1.42 soren bus_space_handle_t sc_pm_ioh;
85 1.42 soren bus_space_handle_t sc_sb800_ioh;
86 1.4 jmcneill bus_space_handle_t sc_smb_ioh;
87 1.4 jmcneill void * sc_smb_ih;
88 1.1 jmcneill int sc_poll;
89 1.59 msaitoh bool sc_sb800_selen; /* Use SMBUS0SEL */
90 1.1 jmcneill
91 1.3 jmcneill pci_chipset_tag_t sc_pc;
92 1.3 jmcneill pcitag_t sc_pcitag;
93 1.35 hannken pcireg_t sc_id;
94 1.54 msaitoh pcireg_t sc_rev;
95 1.3 jmcneill
96 1.46 pgoyette int sc_numbusses;
97 1.46 pgoyette device_t sc_i2c_device[4];
98 1.42 soren struct piixpm_smbus sc_busses[4];
99 1.42 soren struct i2c_controller sc_i2c_tags[4];
100 1.42 soren
101 1.1 jmcneill struct {
102 1.42 soren i2c_op_t op;
103 1.42 soren void * buf;
104 1.42 soren size_t len;
105 1.42 soren int flags;
106 1.42 soren volatile int error;
107 1.1 jmcneill } sc_i2c_xfer;
108 1.3 jmcneill
109 1.3 jmcneill pcireg_t sc_devact[2];
110 1.1 jmcneill };
111 1.1 jmcneill
112 1.25 joerg static int piixpm_match(device_t, cfdata_t, void *);
113 1.25 joerg static void piixpm_attach(device_t, device_t, void *);
114 1.46 pgoyette static int piixpm_rescan(device_t, const char *, const int *);
115 1.46 pgoyette static void piixpm_chdet(device_t, device_t);
116 1.1 jmcneill
117 1.32 dyoung static bool piixpm_suspend(device_t, const pmf_qual_t *);
118 1.32 dyoung static bool piixpm_resume(device_t, const pmf_qual_t *);
119 1.3 jmcneill
120 1.42 soren static int piixpm_sb800_init(struct piixpm_softc *);
121 1.35 hannken static void piixpm_csb5_reset(void *);
122 1.25 joerg static int piixpm_i2c_acquire_bus(void *, int);
123 1.25 joerg static void piixpm_i2c_release_bus(void *, int);
124 1.25 joerg static int piixpm_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
125 1.25 joerg size_t, void *, size_t, int);
126 1.1 jmcneill
127 1.25 joerg static int piixpm_intr(void *);
128 1.1 jmcneill
129 1.46 pgoyette CFATTACH_DECL3_NEW(piixpm, sizeof(struct piixpm_softc),
130 1.46 pgoyette piixpm_match, piixpm_attach, NULL, NULL, piixpm_rescan, piixpm_chdet, 0);
131 1.1 jmcneill
132 1.25 joerg static int
133 1.25 joerg piixpm_match(device_t parent, cfdata_t match, void *aux)
134 1.1 jmcneill {
135 1.1 jmcneill struct pci_attach_args *pa;
136 1.1 jmcneill
137 1.1 jmcneill pa = (struct pci_attach_args *)aux;
138 1.1 jmcneill switch (PCI_VENDOR(pa->pa_id)) {
139 1.1 jmcneill case PCI_VENDOR_INTEL:
140 1.1 jmcneill switch (PCI_PRODUCT(pa->pa_id)) {
141 1.1 jmcneill case PCI_PRODUCT_INTEL_82371AB_PMC:
142 1.1 jmcneill case PCI_PRODUCT_INTEL_82440MX_PMC:
143 1.1 jmcneill return 1;
144 1.1 jmcneill }
145 1.1 jmcneill break;
146 1.1 jmcneill case PCI_VENDOR_ATI:
147 1.1 jmcneill switch (PCI_PRODUCT(pa->pa_id)) {
148 1.1 jmcneill case PCI_PRODUCT_ATI_SB200_SMB:
149 1.10 toshii case PCI_PRODUCT_ATI_SB300_SMB:
150 1.10 toshii case PCI_PRODUCT_ATI_SB400_SMB:
151 1.23 jmcneill case PCI_PRODUCT_ATI_SB600_SMB: /* matches SB600/SB700/SB800 */
152 1.1 jmcneill return 1;
153 1.1 jmcneill }
154 1.1 jmcneill break;
155 1.14 martin case PCI_VENDOR_SERVERWORKS:
156 1.14 martin switch (PCI_PRODUCT(pa->pa_id)) {
157 1.14 martin case PCI_PRODUCT_SERVERWORKS_OSB4:
158 1.14 martin case PCI_PRODUCT_SERVERWORKS_CSB5:
159 1.14 martin case PCI_PRODUCT_SERVERWORKS_CSB6:
160 1.14 martin case PCI_PRODUCT_SERVERWORKS_HT1000SB:
161 1.53 msaitoh case PCI_PRODUCT_SERVERWORKS_HT1100SB:
162 1.14 martin return 1;
163 1.14 martin }
164 1.50 pgoyette break;
165 1.48 pgoyette case PCI_VENDOR_AMD:
166 1.48 pgoyette switch (PCI_PRODUCT(pa->pa_id)) {
167 1.48 pgoyette case PCI_PRODUCT_AMD_HUDSON_SMB:
168 1.54 msaitoh case PCI_PRODUCT_AMD_KERNCZ_SMB:
169 1.48 pgoyette return 1;
170 1.48 pgoyette }
171 1.50 pgoyette break;
172 1.1 jmcneill }
173 1.1 jmcneill
174 1.1 jmcneill return 0;
175 1.1 jmcneill }
176 1.1 jmcneill
177 1.25 joerg static void
178 1.25 joerg piixpm_attach(device_t parent, device_t self, void *aux)
179 1.1 jmcneill {
180 1.25 joerg struct piixpm_softc *sc = device_private(self);
181 1.1 jmcneill struct pci_attach_args *pa = aux;
182 1.1 jmcneill pcireg_t base, conf;
183 1.5 drochner pcireg_t pmmisc;
184 1.1 jmcneill pci_intr_handle_t ih;
185 1.54 msaitoh bool usesmi = false;
186 1.1 jmcneill const char *intrstr = NULL;
187 1.46 pgoyette int i, flags;
188 1.44 christos char intrbuf[PCI_INTRSTR_LEN];
189 1.1 jmcneill
190 1.25 joerg sc->sc_dev = self;
191 1.42 soren sc->sc_iot = pa->pa_iot;
192 1.35 hannken sc->sc_id = pa->pa_id;
193 1.54 msaitoh sc->sc_rev = PCI_REVISION(pa->pa_class);
194 1.3 jmcneill sc->sc_pc = pa->pa_pc;
195 1.3 jmcneill sc->sc_pcitag = pa->pa_tag;
196 1.46 pgoyette sc->sc_numbusses = 1;
197 1.3 jmcneill
198 1.39 drochner pci_aprint_devinfo(pa, NULL);
199 1.3 jmcneill
200 1.18 jmcneill if (!pmf_device_register(self, piixpm_suspend, piixpm_resume))
201 1.18 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
202 1.3 jmcneill
203 1.5 drochner if ((PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL) ||
204 1.5 drochner (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_INTEL_82371AB_PMC))
205 1.5 drochner goto nopowermanagement;
206 1.5 drochner
207 1.5 drochner /* check whether I/O access to PM regs is enabled */
208 1.5 drochner pmmisc = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_PMREGMISC);
209 1.5 drochner if (!(pmmisc & 1))
210 1.5 drochner goto nopowermanagement;
211 1.5 drochner
212 1.4 jmcneill /* Map I/O space */
213 1.5 drochner base = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_PM_BASE);
214 1.53 msaitoh if (base == 0 || bus_space_map(sc->sc_pm_iot, PCI_MAPREG_IO_ADDR(base),
215 1.4 jmcneill PIIX_PM_SIZE, 0, &sc->sc_pm_ioh)) {
216 1.49 msaitoh aprint_error_dev(self,
217 1.49 msaitoh "can't map power management I/O space\n");
218 1.4 jmcneill goto nopowermanagement;
219 1.4 jmcneill }
220 1.4 jmcneill
221 1.5 drochner /*
222 1.5 drochner * Revision 0 and 1 are PIIX4, 2 is PIIX4E, 3 is PIIX4M.
223 1.5 drochner * PIIX4 and PIIX4E have a bug in the timer latch, see Errata #20
224 1.5 drochner * in the "Specification update" (document #297738).
225 1.5 drochner */
226 1.54 msaitoh acpipmtimer_attach(self, sc->sc_pm_iot, sc->sc_pm_ioh, PIIX_PM_PMTMR,
227 1.54 msaitoh (PCI_REVISION(pa->pa_class) < 3) ? ACPIPMT_BADLATCH : 0);
228 1.4 jmcneill
229 1.5 drochner nopowermanagement:
230 1.36 jmcneill
231 1.54 msaitoh /* SB800 rev 0x40+, AMD HUDSON and newer need special initialization */
232 1.54 msaitoh if (PIIXPM_IS_FCHGRP(sc) || PIIXPM_IS_SB800GRP(sc)) {
233 1.42 soren if (piixpm_sb800_init(sc) == 0) {
234 1.54 msaitoh /* Read configuration */
235 1.58 msaitoh conf = bus_space_read_1(sc->sc_iot,
236 1.58 msaitoh sc->sc_smb_ioh, SB800_SMB_HOSTC);
237 1.58 msaitoh usesmi = ((conf & SB800_SMB_HOSTC_IRQ) == 0);
238 1.54 msaitoh goto setintr;
239 1.42 soren }
240 1.48 pgoyette aprint_normal_dev(self, "SMBus initialization failed\n");
241 1.36 jmcneill return;
242 1.36 jmcneill }
243 1.36 jmcneill
244 1.54 msaitoh /* Read configuration */
245 1.54 msaitoh conf = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_SMB_HOSTC);
246 1.54 msaitoh DPRINTF(("%s: conf 0x%08x\n", device_xname(self), conf));
247 1.54 msaitoh
248 1.1 jmcneill if ((conf & PIIX_SMB_HOSTC_HSTEN) == 0) {
249 1.25 joerg aprint_normal_dev(self, "SMBus disabled\n");
250 1.1 jmcneill return;
251 1.1 jmcneill }
252 1.54 msaitoh usesmi = (conf & PIIX_SMB_HOSTC_INTMASK) == PIIX_SMB_HOSTC_SMI;
253 1.1 jmcneill
254 1.1 jmcneill /* Map I/O space */
255 1.1 jmcneill base = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_SMB_BASE) & 0xffff;
256 1.54 msaitoh if (base == 0 ||
257 1.54 msaitoh bus_space_map(sc->sc_smb_iot, PCI_MAPREG_IO_ADDR(base),
258 1.4 jmcneill PIIX_SMB_SIZE, 0, &sc->sc_smb_ioh)) {
259 1.25 joerg aprint_error_dev(self, "can't map smbus I/O space\n");
260 1.1 jmcneill return;
261 1.1 jmcneill }
262 1.1 jmcneill
263 1.54 msaitoh setintr:
264 1.1 jmcneill sc->sc_poll = 1;
265 1.28 pgoyette aprint_normal_dev(self, "");
266 1.54 msaitoh if (usesmi) {
267 1.1 jmcneill /* No PCI IRQ */
268 1.28 pgoyette aprint_normal("interrupting at SMI, ");
269 1.53 msaitoh } else {
270 1.53 msaitoh if ((conf & PIIX_SMB_HOSTC_INTMASK) == PIIX_SMB_HOSTC_IRQ) {
271 1.53 msaitoh /* Install interrupt handler */
272 1.53 msaitoh if (pci_intr_map(pa, &ih) == 0) {
273 1.53 msaitoh intrstr = pci_intr_string(pa->pa_pc, ih,
274 1.53 msaitoh intrbuf, sizeof(intrbuf));
275 1.53 msaitoh sc->sc_smb_ih = pci_intr_establish_xname(
276 1.53 msaitoh pa->pa_pc, ih, IPL_BIO, piixpm_intr,
277 1.53 msaitoh sc, device_xname(sc->sc_dev));
278 1.53 msaitoh if (sc->sc_smb_ih != NULL) {
279 1.53 msaitoh aprint_normal("interrupting at %s",
280 1.53 msaitoh intrstr);
281 1.53 msaitoh sc->sc_poll = 0;
282 1.53 msaitoh }
283 1.1 jmcneill }
284 1.1 jmcneill }
285 1.53 msaitoh if (sc->sc_poll)
286 1.53 msaitoh aprint_normal("polling");
287 1.1 jmcneill }
288 1.1 jmcneill
289 1.3 jmcneill aprint_normal("\n");
290 1.1 jmcneill
291 1.46 pgoyette for (i = 0; i < sc->sc_numbusses; i++)
292 1.46 pgoyette sc->sc_i2c_device[i] = NULL;
293 1.46 pgoyette
294 1.46 pgoyette flags = 0;
295 1.46 pgoyette piixpm_rescan(self, "i2cbus", &flags);
296 1.46 pgoyette }
297 1.46 pgoyette
298 1.46 pgoyette static int
299 1.54 msaitoh piixpm_iicbus_print(void *aux, const char *pnp)
300 1.54 msaitoh {
301 1.54 msaitoh struct i2cbus_attach_args *iba = aux;
302 1.54 msaitoh struct i2c_controller *tag = iba->iba_tag;
303 1.54 msaitoh struct piixpm_smbus *bus = tag->ic_cookie;
304 1.54 msaitoh struct piixpm_softc *sc = bus->softc;
305 1.54 msaitoh
306 1.54 msaitoh iicbus_print(aux, pnp);
307 1.54 msaitoh if (sc->sc_numbusses != 0)
308 1.54 msaitoh aprint_normal(" port %d", bus->sda);
309 1.54 msaitoh
310 1.54 msaitoh return UNCONF;
311 1.54 msaitoh }
312 1.54 msaitoh static int
313 1.46 pgoyette piixpm_rescan(device_t self, const char *ifattr, const int *flags)
314 1.46 pgoyette {
315 1.46 pgoyette struct piixpm_softc *sc = device_private(self);
316 1.46 pgoyette struct i2cbus_attach_args iba;
317 1.46 pgoyette int i;
318 1.46 pgoyette
319 1.46 pgoyette if (!ifattr_match(ifattr, "i2cbus"))
320 1.46 pgoyette return 0;
321 1.46 pgoyette
322 1.1 jmcneill /* Attach I2C bus */
323 1.1 jmcneill
324 1.46 pgoyette for (i = 0; i < sc->sc_numbusses; i++) {
325 1.46 pgoyette if (sc->sc_i2c_device[i])
326 1.46 pgoyette continue;
327 1.42 soren sc->sc_busses[i].sda = i;
328 1.42 soren sc->sc_busses[i].softc = sc;
329 1.55 thorpej iic_tag_init(&sc->sc_i2c_tags[i]);
330 1.42 soren sc->sc_i2c_tags[i].ic_cookie = &sc->sc_busses[i];
331 1.42 soren sc->sc_i2c_tags[i].ic_acquire_bus = piixpm_i2c_acquire_bus;
332 1.42 soren sc->sc_i2c_tags[i].ic_release_bus = piixpm_i2c_release_bus;
333 1.42 soren sc->sc_i2c_tags[i].ic_exec = piixpm_i2c_exec;
334 1.42 soren memset(&iba, 0, sizeof(iba));
335 1.42 soren iba.iba_tag = &sc->sc_i2c_tags[i];
336 1.46 pgoyette sc->sc_i2c_device[i] = config_found_ia(self, ifattr, &iba,
337 1.54 msaitoh piixpm_iicbus_print);
338 1.42 soren }
339 1.46 pgoyette
340 1.46 pgoyette return 0;
341 1.1 jmcneill }
342 1.1 jmcneill
343 1.46 pgoyette static void
344 1.46 pgoyette piixpm_chdet(device_t self, device_t child)
345 1.46 pgoyette {
346 1.46 pgoyette struct piixpm_softc *sc = device_private(self);
347 1.46 pgoyette int i;
348 1.46 pgoyette
349 1.46 pgoyette for (i = 0; i < sc->sc_numbusses; i++) {
350 1.46 pgoyette if (sc->sc_i2c_device[i] == child) {
351 1.46 pgoyette sc->sc_i2c_device[i] = NULL;
352 1.46 pgoyette break;
353 1.46 pgoyette }
354 1.46 pgoyette }
355 1.46 pgoyette }
356 1.46 pgoyette
357 1.46 pgoyette
358 1.18 jmcneill static bool
359 1.32 dyoung piixpm_suspend(device_t dv, const pmf_qual_t *qual)
360 1.18 jmcneill {
361 1.18 jmcneill struct piixpm_softc *sc = device_private(dv);
362 1.18 jmcneill
363 1.18 jmcneill sc->sc_devact[0] = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
364 1.18 jmcneill PIIX_DEVACTA);
365 1.18 jmcneill sc->sc_devact[1] = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
366 1.18 jmcneill PIIX_DEVACTB);
367 1.18 jmcneill
368 1.18 jmcneill return true;
369 1.18 jmcneill }
370 1.18 jmcneill
371 1.18 jmcneill static bool
372 1.32 dyoung piixpm_resume(device_t dv, const pmf_qual_t *qual)
373 1.3 jmcneill {
374 1.18 jmcneill struct piixpm_softc *sc = device_private(dv);
375 1.3 jmcneill
376 1.18 jmcneill pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_DEVACTA,
377 1.18 jmcneill sc->sc_devact[0]);
378 1.18 jmcneill pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_DEVACTB,
379 1.18 jmcneill sc->sc_devact[1]);
380 1.3 jmcneill
381 1.18 jmcneill return true;
382 1.3 jmcneill }
383 1.3 jmcneill
384 1.36 jmcneill /*
385 1.36 jmcneill * Extract SMBus base address from SB800 Power Management (PM) registers.
386 1.36 jmcneill * The PM registers can be accessed either through indirect I/O (CD6/CD7) or
387 1.36 jmcneill * direct mapping if AcpiMMioDecodeEn is enabled. Since this function is only
388 1.36 jmcneill * called once it uses indirect I/O for simplicity.
389 1.36 jmcneill */
390 1.36 jmcneill static int
391 1.42 soren piixpm_sb800_init(struct piixpm_softc *sc)
392 1.36 jmcneill {
393 1.42 soren bus_space_tag_t iot = sc->sc_iot;
394 1.36 jmcneill bus_space_handle_t ioh; /* indirect I/O handle */
395 1.36 jmcneill uint16_t val, base_addr;
396 1.54 msaitoh bool enabled;
397 1.36 jmcneill
398 1.57 msaitoh if (PIIXPM_IS_KERNCZ(sc) ||
399 1.57 msaitoh (PIIXPM_IS_HUDSON(sc) && (sc->sc_rev >= 0x1f)))
400 1.57 msaitoh sc->sc_numbusses = 2;
401 1.57 msaitoh else
402 1.57 msaitoh sc->sc_numbusses = 4;
403 1.57 msaitoh
404 1.36 jmcneill /* Fetch SMB base address */
405 1.36 jmcneill if (bus_space_map(iot,
406 1.54 msaitoh SB800_INDIRECTIO_BASE, SB800_INDIRECTIO_SIZE, 0, &ioh)) {
407 1.36 jmcneill device_printf(sc->sc_dev, "couldn't map indirect I/O space\n");
408 1.36 jmcneill return EBUSY;
409 1.36 jmcneill }
410 1.54 msaitoh if (PIIXPM_IS_FCHGRP(sc)) {
411 1.54 msaitoh bus_space_write_1(iot, ioh, SB800_INDIRECTIO_INDEX,
412 1.54 msaitoh AMDFCH41_PM_DECODE_EN0);
413 1.54 msaitoh val = bus_space_read_1(iot, ioh, SB800_INDIRECTIO_DATA);
414 1.54 msaitoh enabled = val & AMDFCH41_SMBUS_EN;
415 1.54 msaitoh if (!enabled)
416 1.54 msaitoh return ENOENT;
417 1.54 msaitoh
418 1.54 msaitoh bus_space_write_1(iot, ioh, SB800_INDIRECTIO_INDEX,
419 1.54 msaitoh AMDFCH41_PM_DECODE_EN1);
420 1.54 msaitoh val = bus_space_read_1(iot, ioh, SB800_INDIRECTIO_DATA) << 8;
421 1.54 msaitoh base_addr = val;
422 1.54 msaitoh } else {
423 1.59 msaitoh uint8_t data;
424 1.59 msaitoh
425 1.54 msaitoh bus_space_write_1(iot, ioh, SB800_INDIRECTIO_INDEX,
426 1.54 msaitoh SB800_PM_SMBUS0EN_LO);
427 1.54 msaitoh val = bus_space_read_1(iot, ioh, SB800_INDIRECTIO_DATA);
428 1.54 msaitoh enabled = val & SB800_PM_SMBUS0EN_ENABLE;
429 1.54 msaitoh if (!enabled)
430 1.54 msaitoh return ENOENT;
431 1.54 msaitoh
432 1.54 msaitoh bus_space_write_1(iot, ioh, SB800_INDIRECTIO_INDEX,
433 1.54 msaitoh SB800_PM_SMBUS0EN_HI);
434 1.54 msaitoh val |= bus_space_read_1(iot, ioh, SB800_INDIRECTIO_DATA) << 8;
435 1.54 msaitoh base_addr = val & SB800_PM_SMBUS0EN_BADDR;
436 1.54 msaitoh
437 1.54 msaitoh bus_space_write_1(iot, ioh, SB800_INDIRECTIO_INDEX,
438 1.54 msaitoh SB800_PM_SMBUS0SELEN);
439 1.59 msaitoh data = bus_space_read_1(iot, ioh, SB800_INDIRECTIO_DATA);
440 1.59 msaitoh if ((data & SB800_PM_USE_SMBUS0SEL) != 0)
441 1.59 msaitoh sc->sc_sb800_selen = true;
442 1.54 msaitoh }
443 1.54 msaitoh
444 1.42 soren sc->sc_sb800_ioh = ioh;
445 1.36 jmcneill aprint_debug_dev(sc->sc_dev, "SMBus @ 0x%04x\n", base_addr);
446 1.36 jmcneill
447 1.42 soren if (bus_space_map(iot, PCI_MAPREG_IO_ADDR(base_addr),
448 1.58 msaitoh SB800_SMB_SIZE, 0, &sc->sc_smb_ioh)) {
449 1.36 jmcneill aprint_error_dev(sc->sc_dev, "can't map smbus I/O space\n");
450 1.36 jmcneill return EBUSY;
451 1.36 jmcneill }
452 1.36 jmcneill
453 1.36 jmcneill return 0;
454 1.36 jmcneill }
455 1.36 jmcneill
456 1.35 hannken static void
457 1.35 hannken piixpm_csb5_reset(void *arg)
458 1.35 hannken {
459 1.35 hannken struct piixpm_softc *sc = arg;
460 1.35 hannken pcireg_t base, hostc, pmbase;
461 1.35 hannken
462 1.35 hannken base = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PIIX_SMB_BASE);
463 1.35 hannken hostc = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PIIX_SMB_HOSTC);
464 1.35 hannken
465 1.35 hannken pmbase = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PIIX_PM_BASE);
466 1.35 hannken pmbase |= PIIX_PM_BASE_CSB5_RESET;
467 1.35 hannken pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_PM_BASE, pmbase);
468 1.35 hannken pmbase &= ~PIIX_PM_BASE_CSB5_RESET;
469 1.35 hannken pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_PM_BASE, pmbase);
470 1.35 hannken
471 1.35 hannken pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_SMB_BASE, base);
472 1.35 hannken pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_SMB_HOSTC, hostc);
473 1.35 hannken
474 1.35 hannken (void) tsleep(&sc, PRIBIO, "csb5reset", hz/2);
475 1.35 hannken }
476 1.35 hannken
477 1.25 joerg static int
478 1.1 jmcneill piixpm_i2c_acquire_bus(void *cookie, int flags)
479 1.1 jmcneill {
480 1.42 soren struct piixpm_smbus *smbus = cookie;
481 1.42 soren struct piixpm_softc *sc = smbus->softc;
482 1.1 jmcneill
483 1.54 msaitoh if (PIIXPM_IS_KERNCZ(sc)) {
484 1.54 msaitoh bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
485 1.54 msaitoh SB800_INDIRECTIO_INDEX, AMDFCH41_PM_PORT_INDEX);
486 1.42 soren bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
487 1.54 msaitoh SB800_INDIRECTIO_DATA, smbus->sda << 3);
488 1.54 msaitoh } else if (PIIXPM_IS_SB800GRP(sc) || PIIXPM_IS_HUDSON(sc)) {
489 1.59 msaitoh if (sc->sc_sb800_selen) {
490 1.59 msaitoh bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
491 1.59 msaitoh SB800_INDIRECTIO_INDEX, SB800_PM_SMBUS0SEL);
492 1.59 msaitoh bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
493 1.59 msaitoh SB800_INDIRECTIO_DATA,
494 1.59 msaitoh __SHIFTIN(smbus->sda, SB800_PM_SMBUS0_MASK_E));
495 1.59 msaitoh } else {
496 1.59 msaitoh uint8_t data;
497 1.59 msaitoh
498 1.59 msaitoh bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
499 1.59 msaitoh SB800_INDIRECTIO_INDEX, SB800_PM_SMBUS0EN_LO);
500 1.59 msaitoh data = bus_space_read_1(sc->sc_iot, sc->sc_sb800_ioh,
501 1.59 msaitoh SB800_INDIRECTIO_DATA) & ~SB800_PM_SMBUS0_MASK_C;
502 1.59 msaitoh data |= __SHIFTIN(smbus->sda, SB800_PM_SMBUS0_MASK_C);
503 1.59 msaitoh bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
504 1.59 msaitoh SB800_INDIRECTIO_DATA, data);
505 1.59 msaitoh }
506 1.42 soren }
507 1.42 soren
508 1.16 xtraeme return 0;
509 1.1 jmcneill }
510 1.1 jmcneill
511 1.25 joerg static void
512 1.1 jmcneill piixpm_i2c_release_bus(void *cookie, int flags)
513 1.1 jmcneill {
514 1.42 soren struct piixpm_smbus *smbus = cookie;
515 1.42 soren struct piixpm_softc *sc = smbus->softc;
516 1.42 soren
517 1.54 msaitoh if (PIIXPM_IS_KERNCZ(sc)) {
518 1.54 msaitoh bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
519 1.54 msaitoh SB800_INDIRECTIO_INDEX, AMDFCH41_PM_PORT_INDEX);
520 1.59 msaitoh /* Set to port 0 */
521 1.54 msaitoh bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
522 1.54 msaitoh SB800_INDIRECTIO_DATA, 0);
523 1.54 msaitoh } else if (PIIXPM_IS_SB800GRP(sc) || PIIXPM_IS_HUDSON(sc)) {
524 1.59 msaitoh if (sc->sc_sb800_selen) {
525 1.59 msaitoh bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
526 1.59 msaitoh SB800_INDIRECTIO_INDEX, SB800_PM_SMBUS0SEL);
527 1.59 msaitoh
528 1.59 msaitoh /* Set to port 0 */
529 1.59 msaitoh bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
530 1.59 msaitoh SB800_INDIRECTIO_DATA, 0);
531 1.59 msaitoh } else {
532 1.59 msaitoh uint8_t data;
533 1.59 msaitoh
534 1.59 msaitoh bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
535 1.59 msaitoh SB800_INDIRECTIO_INDEX, SB800_PM_SMBUS0EN_LO);
536 1.59 msaitoh
537 1.59 msaitoh /* Set to port 0 */
538 1.59 msaitoh data = bus_space_read_1(sc->sc_iot, sc->sc_sb800_ioh,
539 1.59 msaitoh SB800_INDIRECTIO_DATA) & ~SB800_PM_SMBUS0_MASK_C;
540 1.59 msaitoh bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
541 1.59 msaitoh SB800_INDIRECTIO_DATA, data);
542 1.59 msaitoh }
543 1.42 soren }
544 1.1 jmcneill }
545 1.1 jmcneill
546 1.25 joerg static int
547 1.1 jmcneill piixpm_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
548 1.1 jmcneill const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
549 1.1 jmcneill {
550 1.42 soren struct piixpm_smbus *smbus = cookie;
551 1.42 soren struct piixpm_softc *sc = smbus->softc;
552 1.54 msaitoh const uint8_t *b;
553 1.54 msaitoh uint8_t ctl = 0, st;
554 1.1 jmcneill int retries;
555 1.1 jmcneill
556 1.53 msaitoh DPRINTF(("%s: exec: op %d, addr 0x%02x, cmdlen %zu, len %zu, "
557 1.53 msaitoh "flags 0x%x\n",
558 1.53 msaitoh device_xname(sc->sc_dev), op, addr, cmdlen, len, flags));
559 1.1 jmcneill
560 1.41 soren /* Clear status bits */
561 1.49 msaitoh bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS,
562 1.49 msaitoh PIIX_SMB_HS_INTR | PIIX_SMB_HS_DEVERR |
563 1.41 soren PIIX_SMB_HS_BUSERR | PIIX_SMB_HS_FAILED);
564 1.42 soren bus_space_barrier(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, 1,
565 1.41 soren BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
566 1.41 soren
567 1.1 jmcneill /* Wait for bus to be idle */
568 1.1 jmcneill for (retries = 100; retries > 0; retries--) {
569 1.4 jmcneill st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
570 1.4 jmcneill PIIX_SMB_HS);
571 1.1 jmcneill if (!(st & PIIX_SMB_HS_BUSY))
572 1.1 jmcneill break;
573 1.1 jmcneill DELAY(PIIXPM_DELAY);
574 1.1 jmcneill }
575 1.52 msaitoh DPRINTF(("%s: exec: st %#x\n", device_xname(sc->sc_dev), st & 0xff));
576 1.1 jmcneill if (st & PIIX_SMB_HS_BUSY)
577 1.1 jmcneill return (1);
578 1.1 jmcneill
579 1.56 thorpej if (sc->sc_poll)
580 1.1 jmcneill flags |= I2C_F_POLL;
581 1.1 jmcneill
582 1.34 hannken if (!I2C_OP_STOP_P(op) || cmdlen > 1 || len > 2 ||
583 1.34 hannken (cmdlen == 0 && len > 1))
584 1.1 jmcneill return (1);
585 1.1 jmcneill
586 1.1 jmcneill /* Setup transfer */
587 1.1 jmcneill sc->sc_i2c_xfer.op = op;
588 1.1 jmcneill sc->sc_i2c_xfer.buf = buf;
589 1.1 jmcneill sc->sc_i2c_xfer.len = len;
590 1.1 jmcneill sc->sc_i2c_xfer.flags = flags;
591 1.1 jmcneill sc->sc_i2c_xfer.error = 0;
592 1.1 jmcneill
593 1.1 jmcneill /* Set slave address and transfer direction */
594 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_TXSLVA,
595 1.1 jmcneill PIIX_SMB_TXSLVA_ADDR(addr) |
596 1.1 jmcneill (I2C_OP_READ_P(op) ? PIIX_SMB_TXSLVA_READ : 0));
597 1.1 jmcneill
598 1.1 jmcneill b = cmdbuf;
599 1.1 jmcneill if (cmdlen > 0)
600 1.1 jmcneill /* Set command byte */
601 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh,
602 1.4 jmcneill PIIX_SMB_HCMD, b[0]);
603 1.1 jmcneill
604 1.1 jmcneill if (I2C_OP_WRITE_P(op)) {
605 1.1 jmcneill /* Write data */
606 1.1 jmcneill b = buf;
607 1.34 hannken if (cmdlen == 0 && len == 1)
608 1.34 hannken bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh,
609 1.34 hannken PIIX_SMB_HCMD, b[0]);
610 1.34 hannken else if (len > 0)
611 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh,
612 1.1 jmcneill PIIX_SMB_HD0, b[0]);
613 1.1 jmcneill if (len > 1)
614 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh,
615 1.1 jmcneill PIIX_SMB_HD1, b[1]);
616 1.1 jmcneill }
617 1.1 jmcneill
618 1.1 jmcneill /* Set SMBus command */
619 1.34 hannken if (cmdlen == 0) {
620 1.34 hannken if (len == 0)
621 1.27 pgoyette ctl = PIIX_SMB_HC_CMD_QUICK;
622 1.27 pgoyette else
623 1.27 pgoyette ctl = PIIX_SMB_HC_CMD_BYTE;
624 1.27 pgoyette } else if (len == 1)
625 1.1 jmcneill ctl = PIIX_SMB_HC_CMD_BDATA;
626 1.1 jmcneill else if (len == 2)
627 1.1 jmcneill ctl = PIIX_SMB_HC_CMD_WDATA;
628 1.54 msaitoh else
629 1.54 msaitoh panic("%s: unexpected len %zu", __func__, len);
630 1.1 jmcneill
631 1.1 jmcneill if ((flags & I2C_F_POLL) == 0)
632 1.1 jmcneill ctl |= PIIX_SMB_HC_INTREN;
633 1.1 jmcneill
634 1.1 jmcneill /* Start transaction */
635 1.1 jmcneill ctl |= PIIX_SMB_HC_START;
636 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HC, ctl);
637 1.1 jmcneill
638 1.1 jmcneill if (flags & I2C_F_POLL) {
639 1.1 jmcneill /* Poll for completion */
640 1.54 msaitoh if (PIIXPM_IS_CSB5(sc))
641 1.35 hannken DELAY(2*PIIXPM_DELAY);
642 1.35 hannken else
643 1.35 hannken DELAY(PIIXPM_DELAY);
644 1.1 jmcneill for (retries = 1000; retries > 0; retries--) {
645 1.4 jmcneill st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
646 1.1 jmcneill PIIX_SMB_HS);
647 1.1 jmcneill if ((st & PIIX_SMB_HS_BUSY) == 0)
648 1.1 jmcneill break;
649 1.1 jmcneill DELAY(PIIXPM_DELAY);
650 1.1 jmcneill }
651 1.1 jmcneill if (st & PIIX_SMB_HS_BUSY)
652 1.1 jmcneill goto timeout;
653 1.45 hannken piixpm_intr(sc);
654 1.1 jmcneill } else {
655 1.1 jmcneill /* Wait for interrupt */
656 1.53 msaitoh if (tsleep(sc, PRIBIO, "piixpm", PIIXPM_TIMEOUT * hz))
657 1.1 jmcneill goto timeout;
658 1.1 jmcneill }
659 1.1 jmcneill
660 1.1 jmcneill if (sc->sc_i2c_xfer.error)
661 1.1 jmcneill return (1);
662 1.1 jmcneill
663 1.1 jmcneill return (0);
664 1.1 jmcneill
665 1.1 jmcneill timeout:
666 1.1 jmcneill /*
667 1.1 jmcneill * Transfer timeout. Kill the transaction and clear status bits.
668 1.1 jmcneill */
669 1.25 joerg aprint_error_dev(sc->sc_dev, "timeout, status 0x%x\n", st);
670 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HC,
671 1.1 jmcneill PIIX_SMB_HC_KILL);
672 1.1 jmcneill DELAY(PIIXPM_DELAY);
673 1.4 jmcneill st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS);
674 1.1 jmcneill if ((st & PIIX_SMB_HS_FAILED) == 0)
675 1.49 msaitoh aprint_error_dev(sc->sc_dev,
676 1.49 msaitoh "transaction abort failed, status 0x%x\n", st);
677 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, st);
678 1.35 hannken /*
679 1.35 hannken * CSB5 needs hard reset to unlock the smbus after timeout.
680 1.35 hannken */
681 1.54 msaitoh if (PIIXPM_IS_CSB5(sc))
682 1.35 hannken piixpm_csb5_reset(sc);
683 1.1 jmcneill return (1);
684 1.1 jmcneill }
685 1.1 jmcneill
686 1.25 joerg static int
687 1.1 jmcneill piixpm_intr(void *arg)
688 1.1 jmcneill {
689 1.45 hannken struct piixpm_softc *sc = arg;
690 1.54 msaitoh uint8_t st;
691 1.54 msaitoh uint8_t *b;
692 1.1 jmcneill size_t len;
693 1.1 jmcneill
694 1.1 jmcneill /* Read status */
695 1.4 jmcneill st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS);
696 1.1 jmcneill if ((st & PIIX_SMB_HS_BUSY) != 0 || (st & (PIIX_SMB_HS_INTR |
697 1.1 jmcneill PIIX_SMB_HS_DEVERR | PIIX_SMB_HS_BUSERR |
698 1.1 jmcneill PIIX_SMB_HS_FAILED)) == 0)
699 1.1 jmcneill /* Interrupt was not for us */
700 1.1 jmcneill return (0);
701 1.1 jmcneill
702 1.52 msaitoh DPRINTF(("%s: intr st %#x\n", device_xname(sc->sc_dev), st & 0xff));
703 1.1 jmcneill
704 1.1 jmcneill /* Clear status bits */
705 1.4 jmcneill bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, st);
706 1.1 jmcneill
707 1.1 jmcneill /* Check for errors */
708 1.1 jmcneill if (st & (PIIX_SMB_HS_DEVERR | PIIX_SMB_HS_BUSERR |
709 1.1 jmcneill PIIX_SMB_HS_FAILED)) {
710 1.1 jmcneill sc->sc_i2c_xfer.error = 1;
711 1.1 jmcneill goto done;
712 1.1 jmcneill }
713 1.1 jmcneill
714 1.1 jmcneill if (st & PIIX_SMB_HS_INTR) {
715 1.1 jmcneill if (I2C_OP_WRITE_P(sc->sc_i2c_xfer.op))
716 1.1 jmcneill goto done;
717 1.1 jmcneill
718 1.1 jmcneill /* Read data */
719 1.1 jmcneill b = sc->sc_i2c_xfer.buf;
720 1.1 jmcneill len = sc->sc_i2c_xfer.len;
721 1.1 jmcneill if (len > 0)
722 1.4 jmcneill b[0] = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
723 1.1 jmcneill PIIX_SMB_HD0);
724 1.1 jmcneill if (len > 1)
725 1.4 jmcneill b[1] = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
726 1.1 jmcneill PIIX_SMB_HD1);
727 1.1 jmcneill }
728 1.1 jmcneill
729 1.1 jmcneill done:
730 1.1 jmcneill if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0)
731 1.1 jmcneill wakeup(sc);
732 1.1 jmcneill return (1);
733 1.1 jmcneill }
734