ichlpcib.c revision 1.3 1 1.3 ober /* $NetBSD: ichlpcib.c,v 1.3 2007/09/01 21:52:06 ober Exp $ */
2 1.1 xtraeme
3 1.1 xtraeme /*-
4 1.1 xtraeme * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1 xtraeme * All rights reserved.
6 1.1 xtraeme *
7 1.1 xtraeme * This code is derived from software contributed to The NetBSD Foundation
8 1.1 xtraeme * by Minoura Makoto and Matthew R. Green.
9 1.1 xtraeme *
10 1.1 xtraeme * Redistribution and use in source and binary forms, with or without
11 1.1 xtraeme * modification, are permitted provided that the following conditions
12 1.1 xtraeme * are met:
13 1.1 xtraeme * 1. Redistributions of source code must retain the above copyright
14 1.1 xtraeme * notice, this list of conditions and the following disclaimer.
15 1.1 xtraeme * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 xtraeme * notice, this list of conditions and the following disclaimer in the
17 1.1 xtraeme * documentation and/or other materials provided with the distribution.
18 1.1 xtraeme * 3. All advertising materials mentioning features or use of this software
19 1.1 xtraeme * must display the following acknowledgement:
20 1.1 xtraeme * This product includes software developed by the NetBSD
21 1.1 xtraeme * Foundation, Inc. and its contributors.
22 1.1 xtraeme * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 xtraeme * contributors may be used to endorse or promote products derived
24 1.1 xtraeme * from this software without specific prior written permission.
25 1.1 xtraeme *
26 1.1 xtraeme * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 xtraeme * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 xtraeme * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 xtraeme * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 xtraeme * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 xtraeme * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 xtraeme * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 xtraeme * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 xtraeme * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 xtraeme * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 xtraeme * POSSIBILITY OF SUCH DAMAGE.
37 1.1 xtraeme */
38 1.1 xtraeme
39 1.1 xtraeme /*
40 1.1 xtraeme * Intel I/O Controller Hub (ICHn) LPC Interface Bridge driver
41 1.1 xtraeme *
42 1.1 xtraeme * LPC Interface Bridge is basically a pcib (PCI-ISA Bridge), but has
43 1.1 xtraeme * some power management and monitoring functions.
44 1.1 xtraeme * Currently we support the watchdog timer, SpeedStep (on some systems)
45 1.1 xtraeme * and the power management timer.
46 1.1 xtraeme */
47 1.1 xtraeme
48 1.1 xtraeme #include <sys/cdefs.h>
49 1.3 ober __KERNEL_RCSID(0, "$NetBSD: ichlpcib.c,v 1.3 2007/09/01 21:52:06 ober Exp $");
50 1.1 xtraeme
51 1.1 xtraeme #include <sys/types.h>
52 1.1 xtraeme #include <sys/param.h>
53 1.1 xtraeme #include <sys/systm.h>
54 1.1 xtraeme #include <sys/device.h>
55 1.1 xtraeme #include <sys/sysctl.h>
56 1.1 xtraeme #include <machine/bus.h>
57 1.1 xtraeme
58 1.1 xtraeme #include <dev/pci/pcivar.h>
59 1.1 xtraeme #include <dev/pci/pcireg.h>
60 1.1 xtraeme #include <dev/pci/pcidevs.h>
61 1.1 xtraeme
62 1.1 xtraeme #include <dev/sysmon/sysmonvar.h>
63 1.1 xtraeme
64 1.1 xtraeme #include <dev/ic/i82801lpcreg.h>
65 1.1 xtraeme #include <dev/ic/acpipmtimer.h>
66 1.1 xtraeme
67 1.1 xtraeme struct lpcib_softc {
68 1.1 xtraeme /* Device object. */
69 1.1 xtraeme struct device sc_dev;
70 1.1 xtraeme
71 1.1 xtraeme pci_chipset_tag_t sc_pc;
72 1.1 xtraeme pcitag_t sc_pcitag;
73 1.1 xtraeme
74 1.1 xtraeme /* Watchdog variables. */
75 1.1 xtraeme struct sysmon_wdog sc_smw;
76 1.1 xtraeme bus_space_tag_t sc_iot;
77 1.1 xtraeme bus_space_handle_t sc_ioh;
78 1.1 xtraeme
79 1.1 xtraeme /* Power management */
80 1.1 xtraeme void *sc_powerhook;
81 1.1 xtraeme struct pci_conf_state sc_pciconf;
82 1.1 xtraeme pcireg_t sc_pirq[8];
83 1.1 xtraeme };
84 1.1 xtraeme
85 1.1 xtraeme static int lpcibmatch(struct device *, struct cfdata *, void *);
86 1.1 xtraeme static void lpcibattach(struct device *, struct device *, void *);
87 1.1 xtraeme static void lpcib_powerhook(int, void *);
88 1.1 xtraeme
89 1.1 xtraeme static void pmtimer_configure(struct lpcib_softc *, struct pci_attach_args *);
90 1.1 xtraeme
91 1.1 xtraeme static void tcotimer_configure(struct lpcib_softc *, struct pci_attach_args *);
92 1.1 xtraeme static int tcotimer_setmode(struct sysmon_wdog *);
93 1.1 xtraeme static int tcotimer_tickle(struct sysmon_wdog *);
94 1.1 xtraeme static void tcotimer_stop(struct lpcib_softc *);
95 1.1 xtraeme static void tcotimer_start(struct lpcib_softc *);
96 1.1 xtraeme static void tcotimer_status_reset(struct lpcib_softc *);
97 1.1 xtraeme static int tcotimer_disable_noreboot(struct lpcib_softc *, bus_space_tag_t,
98 1.1 xtraeme bus_space_handle_t);
99 1.1 xtraeme
100 1.1 xtraeme static void speedstep_configure(struct lpcib_softc *, struct pci_attach_args *);
101 1.1 xtraeme static int speedstep_sysctl_helper(SYSCTLFN_ARGS);
102 1.1 xtraeme
103 1.1 xtraeme struct lpcib_softc *speedstep_cookie; /* XXX */
104 1.1 xtraeme static int lpcib_ich6 = 0;
105 1.1 xtraeme
106 1.1 xtraeme /* Defined in arch/.../pci/pcib.c. */
107 1.1 xtraeme extern void pcibattach(struct device *, struct device *, void *);
108 1.1 xtraeme
109 1.1 xtraeme CFATTACH_DECL(ichlpcib, sizeof(struct lpcib_softc),
110 1.1 xtraeme lpcibmatch, lpcibattach, NULL, NULL);
111 1.1 xtraeme
112 1.1 xtraeme /*
113 1.1 xtraeme * Autoconf callbacks.
114 1.1 xtraeme */
115 1.1 xtraeme static int
116 1.1 xtraeme lpcibmatch(struct device *parent, struct cfdata *match, void *aux)
117 1.1 xtraeme {
118 1.1 xtraeme struct pci_attach_args *pa = aux;
119 1.1 xtraeme
120 1.1 xtraeme /* We are ISA bridge, of course */
121 1.1 xtraeme if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
122 1.1 xtraeme PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
123 1.1 xtraeme return 0;
124 1.1 xtraeme
125 1.1 xtraeme /* Matches only Intel ICH */
126 1.1 xtraeme if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
127 1.1 xtraeme switch (PCI_PRODUCT(pa->pa_id)) {
128 1.1 xtraeme case PCI_PRODUCT_INTEL_82801AA_LPC: /* ICH */
129 1.1 xtraeme case PCI_PRODUCT_INTEL_82801AB_LPC: /* ICH0 */
130 1.1 xtraeme case PCI_PRODUCT_INTEL_82801BA_LPC: /* ICH2 */
131 1.1 xtraeme case PCI_PRODUCT_INTEL_82801BAM_LPC: /* ICH2-M */
132 1.1 xtraeme case PCI_PRODUCT_INTEL_82801CA_LPC: /* ICH3-S */
133 1.1 xtraeme case PCI_PRODUCT_INTEL_82801CAM_LPC: /* ICH3-M */
134 1.1 xtraeme case PCI_PRODUCT_INTEL_82801DB_LPC: /* ICH4 */
135 1.1 xtraeme case PCI_PRODUCT_INTEL_82801DB_ISA: /* ICH4-M */
136 1.1 xtraeme case PCI_PRODUCT_INTEL_82801EB_LPC: /* ICH5 */
137 1.1 xtraeme return 10;
138 1.1 xtraeme case PCI_PRODUCT_INTEL_82801FB_LPC: /* ICH6 */
139 1.1 xtraeme case PCI_PRODUCT_INTEL_82801FBM_LPC: /* ICH6-M */
140 1.1 xtraeme case PCI_PRODUCT_INTEL_82801G_LPC: /* ICH7 */
141 1.1 xtraeme case PCI_PRODUCT_INTEL_82801GBM_LPC: /* ICH7-M */
142 1.1 xtraeme case PCI_PRODUCT_INTEL_82801GHM_LPC: /* ICH7-M DH */
143 1.1 xtraeme case PCI_PRODUCT_INTEL_82801H_LPC: /* ICH8 */
144 1.1 xtraeme case PCI_PRODUCT_INTEL_82801HH_LPC: /* ICH8 DH */
145 1.1 xtraeme case PCI_PRODUCT_INTEL_82801HO_LPC: /* ICH8 DO */
146 1.3 ober case PCI_PRODUCT_INTEL_82801HBM_LPC: /* iCH8-M */
147 1.2 xtraeme case PCI_PRODUCT_INTEL_82801IH_LPC: /* ICH9 */
148 1.2 xtraeme case PCI_PRODUCT_INTEL_82801IR_LPC: /* ICH9-R */
149 1.2 xtraeme case PCI_PRODUCT_INTEL_82801IB_LPC: /* ICH9 ? */
150 1.1 xtraeme lpcib_ich6 = 1;
151 1.1 xtraeme return 10; /* prior to pcib */
152 1.1 xtraeme }
153 1.1 xtraeme }
154 1.1 xtraeme
155 1.1 xtraeme return 0;
156 1.1 xtraeme }
157 1.1 xtraeme
158 1.1 xtraeme static void
159 1.1 xtraeme lpcibattach(struct device *parent, struct device *self, void *aux)
160 1.1 xtraeme {
161 1.1 xtraeme struct pci_attach_args *pa = aux;
162 1.1 xtraeme struct lpcib_softc *sc = (void*) self;
163 1.1 xtraeme
164 1.1 xtraeme sc->sc_pc = pa->pa_pc;
165 1.1 xtraeme sc->sc_pcitag = pa->pa_tag;
166 1.1 xtraeme
167 1.1 xtraeme pcibattach(parent, self, aux);
168 1.1 xtraeme
169 1.1 xtraeme /*
170 1.1 xtraeme * Part of our I/O registers are used as ACPI PM regs.
171 1.1 xtraeme * Since our ACPI subsystem accesses the I/O space directly so far,
172 1.1 xtraeme * we do not have to bother bus_space I/O map confliction.
173 1.1 xtraeme */
174 1.1 xtraeme if (pci_mapreg_map(pa, LPCIB_PCI_PMBASE, PCI_MAPREG_TYPE_IO, 0,
175 1.1 xtraeme &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
176 1.1 xtraeme aprint_error("%s: can't map power management i/o space",
177 1.1 xtraeme sc->sc_dev.dv_xname);
178 1.1 xtraeme return;
179 1.1 xtraeme }
180 1.1 xtraeme
181 1.1 xtraeme /* Set up the power management timer. */
182 1.1 xtraeme pmtimer_configure(sc, pa);
183 1.1 xtraeme
184 1.1 xtraeme /* Set up the TCO (watchdog). */
185 1.1 xtraeme tcotimer_configure(sc, pa);
186 1.1 xtraeme
187 1.1 xtraeme /* Set up SpeedStep. */
188 1.1 xtraeme speedstep_configure(sc, pa);
189 1.1 xtraeme
190 1.1 xtraeme /* Install powerhook */
191 1.1 xtraeme sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
192 1.1 xtraeme lpcib_powerhook, sc);
193 1.1 xtraeme if (sc->sc_powerhook == NULL)
194 1.1 xtraeme aprint_error("%s: can't establish powerhook\n",
195 1.1 xtraeme sc->sc_dev.dv_xname);
196 1.1 xtraeme }
197 1.1 xtraeme
198 1.1 xtraeme static void
199 1.1 xtraeme lpcib_powerhook(int why, void *opaque)
200 1.1 xtraeme {
201 1.1 xtraeme struct lpcib_softc *sc;
202 1.1 xtraeme pci_chipset_tag_t pc;
203 1.1 xtraeme pcitag_t tag;
204 1.1 xtraeme
205 1.1 xtraeme sc = (struct lpcib_softc *)opaque;
206 1.1 xtraeme pc = sc->sc_pc;
207 1.1 xtraeme tag = sc->sc_pcitag;
208 1.1 xtraeme
209 1.1 xtraeme switch (why) {
210 1.1 xtraeme case PWR_SUSPEND:
211 1.1 xtraeme pci_conf_capture(pc, tag, &sc->sc_pciconf);
212 1.1 xtraeme
213 1.1 xtraeme /* capture PIRQ routing control registers */
214 1.1 xtraeme sc->sc_pirq[0] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQA_ROUT);
215 1.1 xtraeme sc->sc_pirq[1] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQB_ROUT);
216 1.1 xtraeme sc->sc_pirq[2] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQC_ROUT);
217 1.1 xtraeme sc->sc_pirq[3] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQD_ROUT);
218 1.1 xtraeme sc->sc_pirq[4] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQE_ROUT);
219 1.1 xtraeme sc->sc_pirq[5] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQF_ROUT);
220 1.1 xtraeme sc->sc_pirq[6] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQG_ROUT);
221 1.1 xtraeme sc->sc_pirq[7] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQH_ROUT);
222 1.1 xtraeme
223 1.1 xtraeme break;
224 1.1 xtraeme
225 1.1 xtraeme case PWR_RESUME:
226 1.1 xtraeme pci_conf_restore(pc, tag, &sc->sc_pciconf);
227 1.1 xtraeme
228 1.1 xtraeme /* restore PIRQ routing control registers */
229 1.1 xtraeme pci_conf_write(pc, tag, LPCIB_PCI_PIRQA_ROUT, sc->sc_pirq[0]);
230 1.1 xtraeme pci_conf_write(pc, tag, LPCIB_PCI_PIRQB_ROUT, sc->sc_pirq[1]);
231 1.1 xtraeme pci_conf_write(pc, tag, LPCIB_PCI_PIRQC_ROUT, sc->sc_pirq[2]);
232 1.1 xtraeme pci_conf_write(pc, tag, LPCIB_PCI_PIRQD_ROUT, sc->sc_pirq[3]);
233 1.1 xtraeme pci_conf_write(pc, tag, LPCIB_PCI_PIRQE_ROUT, sc->sc_pirq[4]);
234 1.1 xtraeme pci_conf_write(pc, tag, LPCIB_PCI_PIRQF_ROUT, sc->sc_pirq[5]);
235 1.1 xtraeme pci_conf_write(pc, tag, LPCIB_PCI_PIRQG_ROUT, sc->sc_pirq[6]);
236 1.1 xtraeme pci_conf_write(pc, tag, LPCIB_PCI_PIRQH_ROUT, sc->sc_pirq[7]);
237 1.1 xtraeme
238 1.1 xtraeme break;
239 1.1 xtraeme }
240 1.1 xtraeme }
241 1.1 xtraeme
242 1.1 xtraeme /*
243 1.1 xtraeme * Initialize the power management timer.
244 1.1 xtraeme */
245 1.1 xtraeme static void
246 1.1 xtraeme pmtimer_configure(struct lpcib_softc *sc, struct pci_attach_args *pa)
247 1.1 xtraeme {
248 1.1 xtraeme pcireg_t control;
249 1.1 xtraeme
250 1.1 xtraeme /*
251 1.1 xtraeme * Check if power management I/O space is enabled and enable the ACPI_EN
252 1.1 xtraeme * bit if it's disabled.
253 1.1 xtraeme */
254 1.1 xtraeme control = pci_conf_read(pa->pa_pc, pa->pa_tag, LPCIB_PCI_ACPI_CNTL);
255 1.1 xtraeme if ((control & LPCIB_PCI_ACPI_CNTL_EN) == 0) {
256 1.1 xtraeme control |= LPCIB_PCI_ACPI_CNTL_EN;
257 1.1 xtraeme pci_conf_write(pa->pa_pc, pa->pa_tag, LPCIB_PCI_ACPI_CNTL,
258 1.1 xtraeme control);
259 1.1 xtraeme }
260 1.1 xtraeme
261 1.1 xtraeme /* Attach our PM timer with the generic acpipmtimer function */
262 1.1 xtraeme acpipmtimer_attach(&sc->sc_dev, sc->sc_iot, sc->sc_ioh,
263 1.1 xtraeme LPCIB_PM1_TMR, 0);
264 1.1 xtraeme }
265 1.1 xtraeme
266 1.1 xtraeme /*
267 1.1 xtraeme * Initialize the watchdog timer.
268 1.1 xtraeme */
269 1.1 xtraeme static void
270 1.1 xtraeme tcotimer_configure(struct lpcib_softc *sc, struct pci_attach_args *pa)
271 1.1 xtraeme {
272 1.1 xtraeme bus_space_handle_t gcs_memh;
273 1.1 xtraeme pcireg_t pcireg;
274 1.1 xtraeme uint32_t ioreg;
275 1.1 xtraeme unsigned int period;
276 1.1 xtraeme
277 1.1 xtraeme /*
278 1.1 xtraeme * Map the memory space necessary for the GCS register.
279 1.1 xtraeme * This is only used for ICH6 or newer, to clear the NO_REBOOT
280 1.1 xtraeme * bit.
281 1.1 xtraeme */
282 1.1 xtraeme if (lpcib_ich6) {
283 1.1 xtraeme pcireg = pci_conf_read(pa->pa_pc, pa->pa_tag, LPCIB_RCBA);
284 1.1 xtraeme pcireg &= 0xffffc000;
285 1.1 xtraeme if (bus_space_map(pa->pa_memt, pcireg + LPCIB_GCS_OFFSET,
286 1.1 xtraeme LPCIB_GCS_SIZE, 0, &gcs_memh)) {
287 1.1 xtraeme aprint_error("%s: can't map GCS memory space; "
288 1.1 xtraeme "TCO timer disabled\n", sc->sc_dev.dv_xname);
289 1.1 xtraeme return;
290 1.1 xtraeme }
291 1.1 xtraeme }
292 1.1 xtraeme
293 1.1 xtraeme /*
294 1.1 xtraeme * Clear the NO_REBOOT bit. If this fails, enabling the TCO_EN bit
295 1.1 xtraeme * in the SMI_EN register is the last chance.
296 1.1 xtraeme */
297 1.1 xtraeme if (tcotimer_disable_noreboot(sc, pa->pa_memt, gcs_memh)) {
298 1.1 xtraeme ioreg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN);
299 1.1 xtraeme ioreg |= LPCIB_SMI_EN_TCO_EN;
300 1.1 xtraeme bus_space_write_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN, ioreg);
301 1.1 xtraeme }
302 1.1 xtraeme
303 1.1 xtraeme /* Reset the watchdog status registers. */
304 1.1 xtraeme tcotimer_status_reset(sc);
305 1.1 xtraeme
306 1.1 xtraeme /* Explicitly stop the TCO timer. */
307 1.1 xtraeme tcotimer_stop(sc);
308 1.1 xtraeme
309 1.1 xtraeme /*
310 1.1 xtraeme * Register the driver with the sysmon watchdog framework.
311 1.1 xtraeme */
312 1.1 xtraeme sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
313 1.1 xtraeme sc->sc_smw.smw_cookie = sc;
314 1.1 xtraeme sc->sc_smw.smw_setmode = tcotimer_setmode;
315 1.1 xtraeme sc->sc_smw.smw_tickle = tcotimer_tickle;
316 1.1 xtraeme if (lpcib_ich6)
317 1.1 xtraeme period = LPCIB_TCOTIMER2_MAX_TICK;
318 1.1 xtraeme else
319 1.1 xtraeme period = LPCIB_TCOTIMER_MAX_TICK;
320 1.1 xtraeme sc->sc_smw.smw_period = lpcib_tcotimer_tick_to_second(period);
321 1.1 xtraeme
322 1.1 xtraeme if (sysmon_wdog_register(&sc->sc_smw)) {
323 1.1 xtraeme aprint_error("%s: unable to register TCO timer"
324 1.1 xtraeme "as a sysmon watchdog device.\n",
325 1.1 xtraeme sc->sc_dev.dv_xname);
326 1.1 xtraeme return;
327 1.1 xtraeme }
328 1.1 xtraeme
329 1.1 xtraeme aprint_verbose("%s: TCO (watchdog) timer configured.\n",
330 1.1 xtraeme sc->sc_dev.dv_xname);
331 1.1 xtraeme }
332 1.1 xtraeme
333 1.1 xtraeme /*
334 1.1 xtraeme * Sysmon watchdog callbacks.
335 1.1 xtraeme */
336 1.1 xtraeme static int
337 1.1 xtraeme tcotimer_setmode(struct sysmon_wdog *smw)
338 1.1 xtraeme {
339 1.1 xtraeme struct lpcib_softc *sc = smw->smw_cookie;
340 1.1 xtraeme unsigned int period;
341 1.1 xtraeme uint16_t ich6period = 0;
342 1.1 xtraeme
343 1.1 xtraeme if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
344 1.1 xtraeme /* Stop the TCO timer. */
345 1.1 xtraeme tcotimer_stop(sc);
346 1.1 xtraeme } else {
347 1.1 xtraeme period = lpcib_tcotimer_second_to_tick(smw->smw_period);
348 1.1 xtraeme /*
349 1.1 xtraeme * ICH5 or older are limited to 4s min and 39s max.
350 1.1 xtraeme * ICH6 or newer are limited to 2s min and 613s max.
351 1.1 xtraeme */
352 1.1 xtraeme if (!lpcib_ich6) {
353 1.1 xtraeme if (period < LPCIB_TCOTIMER_MIN_TICK ||
354 1.1 xtraeme period > LPCIB_TCOTIMER_MAX_TICK)
355 1.1 xtraeme return EINVAL;
356 1.1 xtraeme } else {
357 1.1 xtraeme if (period < LPCIB_TCOTIMER2_MIN_TICK ||
358 1.1 xtraeme period > LPCIB_TCOTIMER2_MAX_TICK)
359 1.1 xtraeme return EINVAL;
360 1.1 xtraeme }
361 1.1 xtraeme
362 1.1 xtraeme /* Stop the TCO timer, */
363 1.1 xtraeme tcotimer_stop(sc);
364 1.1 xtraeme
365 1.1 xtraeme /* set the timeout, */
366 1.1 xtraeme if (lpcib_ich6) {
367 1.1 xtraeme /* ICH6 or newer */
368 1.1 xtraeme ich6period = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
369 1.1 xtraeme LPCIB_TCO_TMR2);
370 1.1 xtraeme ich6period &= 0xfc00;
371 1.1 xtraeme bus_space_write_2(sc->sc_iot, sc->sc_ioh,
372 1.1 xtraeme LPCIB_TCO_TMR2, ich6period | period);
373 1.1 xtraeme } else {
374 1.1 xtraeme /* ICH5 or older */
375 1.1 xtraeme period |= bus_space_read_1(sc->sc_iot, sc->sc_ioh,
376 1.1 xtraeme LPCIB_TCO_TMR);
377 1.1 xtraeme period &= 0xc0;
378 1.1 xtraeme bus_space_write_1(sc->sc_iot, sc->sc_ioh,
379 1.1 xtraeme LPCIB_TCO_TMR, period);
380 1.1 xtraeme }
381 1.1 xtraeme
382 1.1 xtraeme /* and start/reload the timer. */
383 1.1 xtraeme tcotimer_start(sc);
384 1.1 xtraeme tcotimer_tickle(smw);
385 1.1 xtraeme }
386 1.1 xtraeme
387 1.1 xtraeme return 0;
388 1.1 xtraeme }
389 1.1 xtraeme
390 1.1 xtraeme static int
391 1.1 xtraeme tcotimer_tickle(struct sysmon_wdog *smw)
392 1.1 xtraeme {
393 1.1 xtraeme struct lpcib_softc *sc = smw->smw_cookie;
394 1.1 xtraeme
395 1.1 xtraeme /* any value is allowed */
396 1.1 xtraeme if (!lpcib_ich6)
397 1.1 xtraeme bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
398 1.1 xtraeme else
399 1.1 xtraeme bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
400 1.1 xtraeme
401 1.1 xtraeme return 0;
402 1.1 xtraeme }
403 1.1 xtraeme
404 1.1 xtraeme static void
405 1.1 xtraeme tcotimer_stop(struct lpcib_softc *sc)
406 1.1 xtraeme {
407 1.1 xtraeme uint16_t ioreg;
408 1.1 xtraeme
409 1.1 xtraeme ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
410 1.1 xtraeme ioreg |= LPCIB_TCO1_CNT_TCO_TMR_HLT;
411 1.1 xtraeme bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
412 1.1 xtraeme }
413 1.1 xtraeme
414 1.1 xtraeme static void
415 1.1 xtraeme tcotimer_start(struct lpcib_softc *sc)
416 1.1 xtraeme {
417 1.1 xtraeme uint16_t ioreg;
418 1.1 xtraeme
419 1.1 xtraeme ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
420 1.1 xtraeme ioreg &= ~LPCIB_TCO1_CNT_TCO_TMR_HLT;
421 1.1 xtraeme bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
422 1.1 xtraeme }
423 1.1 xtraeme
424 1.1 xtraeme static void
425 1.1 xtraeme tcotimer_status_reset(struct lpcib_softc *sc)
426 1.1 xtraeme {
427 1.1 xtraeme bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_STS,
428 1.1 xtraeme LPCIB_TCO1_STS_TIMEOUT);
429 1.1 xtraeme bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
430 1.1 xtraeme LPCIB_TCO2_STS_BOOT_STS);
431 1.1 xtraeme bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
432 1.1 xtraeme LPCIB_TCO2_STS_SECONDS_TO_STS);
433 1.1 xtraeme }
434 1.1 xtraeme
435 1.1 xtraeme /*
436 1.1 xtraeme * Clear the NO_REBOOT bit, this enables reboots.
437 1.1 xtraeme */
438 1.1 xtraeme static int
439 1.1 xtraeme tcotimer_disable_noreboot(struct lpcib_softc *sc, bus_space_tag_t gcs_memt,
440 1.1 xtraeme bus_space_handle_t gcs_memh)
441 1.1 xtraeme {
442 1.1 xtraeme pcireg_t pcireg;
443 1.1 xtraeme uint16_t status = 0;
444 1.1 xtraeme
445 1.1 xtraeme if (!lpcib_ich6) {
446 1.1 xtraeme pcireg = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
447 1.1 xtraeme LPCIB_PCI_GEN_STA);
448 1.1 xtraeme if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT) {
449 1.1 xtraeme /* TCO timeout reset is disabled; try to enable it */
450 1.1 xtraeme pcireg &= ~LPCIB_PCI_GEN_STA_NO_REBOOT;
451 1.1 xtraeme pci_conf_write(sc->sc_pc, sc->sc_pcitag,
452 1.1 xtraeme LPCIB_PCI_GEN_STA, pcireg);
453 1.1 xtraeme if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT)
454 1.1 xtraeme goto error;
455 1.1 xtraeme }
456 1.1 xtraeme } else {
457 1.1 xtraeme status = bus_space_read_4(gcs_memt, gcs_memh, 0);
458 1.1 xtraeme status &= ~LPCIB_GCS_NO_REBOOT;
459 1.1 xtraeme bus_space_write_4(gcs_memt, gcs_memh, 0, status);
460 1.1 xtraeme status = bus_space_read_4(gcs_memt, gcs_memh, 0);
461 1.1 xtraeme bus_space_unmap(gcs_memt, gcs_memh, LPCIB_GCS_SIZE);
462 1.1 xtraeme if (status & LPCIB_GCS_NO_REBOOT)
463 1.1 xtraeme goto error;
464 1.1 xtraeme }
465 1.1 xtraeme
466 1.1 xtraeme return 0;
467 1.1 xtraeme error:
468 1.1 xtraeme aprint_error("%s: TCO timer reboot disabled by hardware; "
469 1.1 xtraeme "hope SMBIOS properly handles it.\n", sc->sc_dev.dv_xname);
470 1.1 xtraeme return EINVAL;
471 1.1 xtraeme }
472 1.1 xtraeme
473 1.1 xtraeme
474 1.1 xtraeme /*
475 1.1 xtraeme * Intel ICH SpeedStep support.
476 1.1 xtraeme */
477 1.1 xtraeme #define SS_READ(sc, reg) \
478 1.1 xtraeme bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, (reg))
479 1.1 xtraeme #define SS_WRITE(sc, reg, val) \
480 1.1 xtraeme bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
481 1.1 xtraeme
482 1.1 xtraeme /*
483 1.1 xtraeme * Linux driver says that SpeedStep on older chipsets cause
484 1.1 xtraeme * lockups on Dell Inspiron 8000 and 8100.
485 1.1 xtraeme */
486 1.1 xtraeme static int
487 1.1 xtraeme speedstep_bad_hb_check(struct pci_attach_args *pa)
488 1.1 xtraeme {
489 1.1 xtraeme
490 1.1 xtraeme if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82815_FULL_HUB &&
491 1.1 xtraeme PCI_REVISION(pa->pa_class) < 5)
492 1.1 xtraeme return 1;
493 1.1 xtraeme
494 1.1 xtraeme return 0;
495 1.1 xtraeme }
496 1.1 xtraeme
497 1.1 xtraeme static void
498 1.1 xtraeme speedstep_configure(struct lpcib_softc *sc, struct pci_attach_args *pa)
499 1.1 xtraeme {
500 1.1 xtraeme const struct sysctlnode *node, *ssnode;
501 1.1 xtraeme int rv;
502 1.1 xtraeme
503 1.1 xtraeme /* Supported on ICH2-M, ICH3-M and ICH4-M. */
504 1.1 xtraeme if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82801DB_ISA ||
505 1.1 xtraeme PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82801CAM_LPC ||
506 1.1 xtraeme (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82801BAM_LPC &&
507 1.1 xtraeme pci_find_device(pa, speedstep_bad_hb_check) == 0)) {
508 1.1 xtraeme uint8_t pmcon;
509 1.1 xtraeme
510 1.1 xtraeme /* Enable SpeedStep if it isn't already enabled. */
511 1.1 xtraeme pmcon = pci_conf_read(pa->pa_pc, pa->pa_tag,
512 1.1 xtraeme LPCIB_PCI_GEN_PMCON_1);
513 1.1 xtraeme if ((pmcon & LPCIB_PCI_GEN_PMCON_1_SS_EN) == 0)
514 1.1 xtraeme pci_conf_write(pa->pa_pc, pa->pa_tag,
515 1.1 xtraeme LPCIB_PCI_GEN_PMCON_1,
516 1.1 xtraeme pmcon | LPCIB_PCI_GEN_PMCON_1_SS_EN);
517 1.1 xtraeme
518 1.1 xtraeme /* Put in machdep.speedstep_state (0 for low, 1 for high). */
519 1.1 xtraeme if ((rv = sysctl_createv(NULL, 0, NULL, &node,
520 1.1 xtraeme CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
521 1.1 xtraeme NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL)) != 0)
522 1.1 xtraeme goto err;
523 1.1 xtraeme
524 1.1 xtraeme /* CTLFLAG_ANYWRITE? kernel option like EST? */
525 1.1 xtraeme if ((rv = sysctl_createv(NULL, 0, &node, &ssnode,
526 1.1 xtraeme CTLFLAG_READWRITE, CTLTYPE_INT, "speedstep_state", NULL,
527 1.1 xtraeme speedstep_sysctl_helper, 0, NULL, 0, CTL_CREATE,
528 1.1 xtraeme CTL_EOL)) != 0)
529 1.1 xtraeme goto err;
530 1.1 xtraeme
531 1.1 xtraeme /* XXX save the sc for IO tag/handle */
532 1.1 xtraeme speedstep_cookie = sc;
533 1.1 xtraeme aprint_verbose("%s: SpeedStep enabled\n", sc->sc_dev.dv_xname);
534 1.1 xtraeme }
535 1.1 xtraeme
536 1.1 xtraeme return;
537 1.1 xtraeme
538 1.1 xtraeme err:
539 1.1 xtraeme aprint_normal("%s: sysctl_createv failed (rv = %d)\n", __func__, rv);
540 1.1 xtraeme }
541 1.1 xtraeme
542 1.1 xtraeme /*
543 1.1 xtraeme * get/set the SpeedStep state: 0 == low power, 1 == high power.
544 1.1 xtraeme */
545 1.1 xtraeme static int
546 1.1 xtraeme speedstep_sysctl_helper(SYSCTLFN_ARGS)
547 1.1 xtraeme {
548 1.1 xtraeme struct sysctlnode node;
549 1.1 xtraeme struct lpcib_softc *sc = speedstep_cookie;
550 1.1 xtraeme uint8_t state, state2;
551 1.1 xtraeme int ostate, nstate, s, error = 0;
552 1.1 xtraeme
553 1.1 xtraeme /*
554 1.1 xtraeme * We do the dance with spl's to avoid being at high ipl during
555 1.1 xtraeme * sysctl_lookup() which can both copyin and copyout.
556 1.1 xtraeme */
557 1.1 xtraeme s = splserial();
558 1.1 xtraeme state = SS_READ(sc, LPCIB_PM_SS_CNTL);
559 1.1 xtraeme splx(s);
560 1.1 xtraeme if ((state & LPCIB_PM_SS_STATE_LOW) == 0)
561 1.1 xtraeme ostate = 1;
562 1.1 xtraeme else
563 1.1 xtraeme ostate = 0;
564 1.1 xtraeme nstate = ostate;
565 1.1 xtraeme
566 1.1 xtraeme node = *rnode;
567 1.1 xtraeme node.sysctl_data = &nstate;
568 1.1 xtraeme
569 1.1 xtraeme error = sysctl_lookup(SYSCTLFN_CALL(&node));
570 1.1 xtraeme if (error || newp == NULL)
571 1.1 xtraeme goto out;
572 1.1 xtraeme
573 1.1 xtraeme /* Only two states are available */
574 1.1 xtraeme if (nstate != 0 && nstate != 1) {
575 1.1 xtraeme error = EINVAL;
576 1.1 xtraeme goto out;
577 1.1 xtraeme }
578 1.1 xtraeme
579 1.1 xtraeme s = splserial();
580 1.1 xtraeme state2 = SS_READ(sc, LPCIB_PM_SS_CNTL);
581 1.1 xtraeme if ((state2 & LPCIB_PM_SS_STATE_LOW) == 0)
582 1.1 xtraeme ostate = 1;
583 1.1 xtraeme else
584 1.1 xtraeme ostate = 0;
585 1.1 xtraeme
586 1.1 xtraeme if (ostate != nstate) {
587 1.1 xtraeme uint8_t cntl;
588 1.1 xtraeme
589 1.1 xtraeme if (nstate == 0)
590 1.1 xtraeme state2 |= LPCIB_PM_SS_STATE_LOW;
591 1.1 xtraeme else
592 1.1 xtraeme state2 &= ~LPCIB_PM_SS_STATE_LOW;
593 1.1 xtraeme
594 1.1 xtraeme /*
595 1.1 xtraeme * Must disable bus master arbitration during the change.
596 1.1 xtraeme */
597 1.1 xtraeme cntl = SS_READ(sc, LPCIB_PM_CTRL);
598 1.1 xtraeme SS_WRITE(sc, LPCIB_PM_CTRL, cntl | LPCIB_PM_SS_CNTL_ARB_DIS);
599 1.1 xtraeme SS_WRITE(sc, LPCIB_PM_SS_CNTL, state2);
600 1.1 xtraeme SS_WRITE(sc, LPCIB_PM_CTRL, cntl);
601 1.1 xtraeme }
602 1.1 xtraeme splx(s);
603 1.1 xtraeme out:
604 1.1 xtraeme return error;
605 1.1 xtraeme }
606