2 * Copyright (c) 1998, 1999, 2001 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/pci/alpm.c,v 1.26 2007/01/11 19:56:24 jhb Exp $
31 * Power Management support for the Acer M15x3 chipsets
33 #include <sys/param.h>
35 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/systm.h>
41 #include <bus/pci/pcivar.h>
42 #include <bus/pci/pcireg.h>
44 #include <bus/smbus/smbconf.h>
47 #define ALPM_DEBUG(x) if (alpm_debug) (x)
50 static int alpm_debug = 1;
52 static int alpm_debug = 0;
55 #define ACER_M1543_PMU_ID 0x710110b9
58 * I/O registers offsets - the base address is programmed via the
59 * SMBBA PCI configuration register
61 #define SMBSTS 0x0 /* SMBus host/slave status register */
62 #define SMBCMD 0x1 /* SMBus host/slave command register */
63 #define SMBSTART 0x2 /* start to generate programmed cycle */
64 #define SMBHADDR 0x3 /* host address register */
65 #define SMBHDATA 0x4 /* data A register for host controller */
66 #define SMBHDATB 0x5 /* data B register for host controller */
67 #define SMBHBLOCK 0x6 /* block register for host controller */
68 #define SMBHCMD 0x7 /* command register for host controller */
71 #define LSB 0x1 /* XXX: Better name: Read/Write? */
74 #define TERMINATE 0x80
75 #define BUS_COLLI 0x40
76 #define DEVICE_ERR 0x20
77 #define SMI_I_STS 0x10
80 #define HSTSLV_STS 0x02
81 #define HSTSLV_BSY 0x01
84 #define SMB_BLK_CLR 0x80
85 #define T_OUT_CMD 0x08
86 #define ABORT_HOST 0x04
90 #define SMBSRBYTE 0x10 /* send/receive byte */
91 #define SMBWRBYTE 0x20 /* write/read byte */
92 #define SMBWRWORD 0x30 /* write/read word */
93 #define SMBWRBLOCK 0x40 /* write/read block */
95 /* PCI configuration registers and masks
98 #define COM_ENABLE_IO 0x1
100 #define SMBBA PCIR_BAR(1)
103 #define ATPC_SMBCTRL 0x04 /* XX linux has this as 0x6 */
106 #define SMBHSI_SLAVE 0x2
107 #define SMBHSI_HOST 0x1
110 #define SMBHCBC_CLOCK 0x70
112 #define SMBCLOCK_149K 0x0
113 #define SMBCLOCK_74K 0x20
114 #define SMBCLOCK_37K 0x40
115 #define SMBCLOCK_223K 0x80
116 #define SMBCLOCK_111K 0xa0
117 #define SMBCLOCK_55K 0xc0
121 struct resource *res;
122 bus_space_tag_t smbst;
123 bus_space_handle_t smbsh;
128 #define ALPM_LOCK(alpm) lockmgr(&(alpm)->lock, LK_EXCLUSIVE)
129 #define ALPM_UNLOCK(alpm) lockmgr(&(alpm)->lock, LK_RELEASE)
130 #define ALPM_LOCK_ASSERT(alpm) KKASSERT(lockstatus(&(alpm)->lock, curthread) != 0)
132 #define ALPM_SMBINB(alpm,register) \
133 (bus_space_read_1(alpm->smbst, alpm->smbsh, register))
134 #define ALPM_SMBOUTB(alpm,register,value) \
135 (bus_space_write_1(alpm->smbst, alpm->smbsh, register, value))
137 static int alpm_detach(device_t dev);
140 alpm_probe(device_t dev)
143 if (pci_get_devid(dev) == ACER_M1543_PMU_ID) {
144 device_set_desc(dev, "AcerLabs M15x3 Power Management Unit");
146 return (BUS_PROBE_DEFAULT);
153 alpm_attach(device_t dev)
157 struct alpm_softc *alpm;
159 alpm = device_get_softc(dev);
161 /* Unlock SMBIO base register access */
162 l = pci_read_config(dev, ATPC, 1);
163 pci_write_config(dev, ATPC, l & ~ATPC_SMBCTRL, 1);
166 * XX linux sets clock to 74k, should we?
167 l = pci_read_config(dev, SMBHCBC, 1);
170 pci_write_config(dev, SMBHCBC, l, 1);
173 if (bootverbose || alpm_debug) {
174 l = pci_read_config(dev, SMBHSI, 1);
175 device_printf(dev, "%s/%s",
176 (l & SMBHSI_HOST) ? "host":"nohost",
177 (l & SMBHSI_SLAVE) ? "slave":"noslave");
179 l = pci_read_config(dev, SMBHCBC, 1);
180 switch (l & SMBHCBC_CLOCK) {
207 alpm->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
210 if (alpm->res == NULL) {
211 device_printf(dev, "Could not allocate Bus space\n");
214 alpm->smbst = rman_get_bustag(alpm->res);
215 alpm->smbsh = rman_get_bushandle(alpm->res);
216 lockinit(&alpm->lock, "alpm", 0, LK_CANRECURSE);
218 /* attach the smbus */
219 alpm->smbus = device_add_child(dev, "smbus", -1);
220 if (alpm->smbus == NULL) {
224 bus_generic_attach(dev);
230 alpm_detach(device_t dev)
232 struct alpm_softc *alpm = device_get_softc(dev);
235 device_delete_child(dev, alpm->smbus);
238 lockuninit(&alpm->lock);
241 bus_release_resource(dev, SYS_RES_IOPORT, SMBBA, alpm->res);
247 alpm_callback(device_t dev, int index, void *data)
252 case SMB_REQUEST_BUS:
253 case SMB_RELEASE_BUS:
254 /* ok, bus allocation accepted */
264 alpm_clear(struct alpm_softc *sc)
266 ALPM_SMBOUTB(sc, SMBSTS, 0xff);
274 alpm_abort(struct alpm_softc *sc)
276 ALPM_SMBOUTB(sc, SMBCMD, T_OUT_CMD | ABORT_HOST);
283 alpm_idle(struct alpm_softc *sc)
287 sts = ALPM_SMBINB(sc, SMBSTS);
289 ALPM_DEBUG(kprintf("alpm: idle? STS=0x%x\n", sts));
291 return (sts & IDL_STS);
295 * Poll the SMBus controller
298 alpm_wait(struct alpm_softc *sc)
304 /* wait for command to complete and SMBus controller is idle */
307 sts = ALPM_SMBINB(sc, SMBSTS);
312 ALPM_DEBUG(kprintf("alpm: STS=0x%x\n", sts));
317 error |= SMB_ETIMEOUT;
325 if (sts & DEVICE_ERR)
326 error |= SMB_EBUSERR;
328 if (error != SMB_ENOERR)
335 alpm_quick(device_t dev, u_char slave, int how)
337 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
342 if (!alpm_idle(sc)) {
349 ALPM_DEBUG(kprintf("alpm: QWRITE to 0x%x", slave));
350 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
353 ALPM_DEBUG(kprintf("alpm: QREAD to 0x%x", slave));
354 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
357 panic("%s: unknown QUICK command (%x)!", __func__, how);
359 ALPM_SMBOUTB(sc, SMBCMD, SMBQUICK);
360 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
362 error = alpm_wait(sc);
364 ALPM_DEBUG(kprintf(", error=0x%x\n", error));
371 alpm_sendb(device_t dev, u_char slave, char byte)
373 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
378 if (!alpm_idle(sc)) {
383 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
384 ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
385 ALPM_SMBOUTB(sc, SMBHDATA, byte);
386 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
388 error = alpm_wait(sc);
390 ALPM_DEBUG(kprintf("alpm: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
397 alpm_recvb(device_t dev, u_char slave, char *byte)
399 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
404 if (!alpm_idle(sc)) {
409 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
410 ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
411 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
413 if ((error = alpm_wait(sc)) == SMB_ENOERR)
414 *byte = ALPM_SMBINB(sc, SMBHDATA);
416 ALPM_DEBUG(kprintf("alpm: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
423 alpm_writeb(device_t dev, u_char slave, char cmd, char byte)
425 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
430 if (!alpm_idle(sc)) {
435 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
436 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
437 ALPM_SMBOUTB(sc, SMBHDATA, byte);
438 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
439 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
441 error = alpm_wait(sc);
443 ALPM_DEBUG(kprintf("alpm: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
450 alpm_readb(device_t dev, u_char slave, char cmd, char *byte)
452 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
457 if (!alpm_idle(sc)) {
462 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
463 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
464 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
465 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
467 if ((error = alpm_wait(sc)) == SMB_ENOERR)
468 *byte = ALPM_SMBINB(sc, SMBHDATA);
470 ALPM_DEBUG(kprintf("alpm: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, *byte, error));
477 alpm_writew(device_t dev, u_char slave, char cmd, short word)
479 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
484 if (!alpm_idle(sc)) {
489 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
490 ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
491 ALPM_SMBOUTB(sc, SMBHDATA, word & 0x00ff);
492 ALPM_SMBOUTB(sc, SMBHDATB, (word & 0xff00) >> 8);
493 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
494 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
496 error = alpm_wait(sc);
498 ALPM_DEBUG(kprintf("alpm: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
505 alpm_readw(device_t dev, u_char slave, char cmd, short *word)
507 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
513 if (!alpm_idle(sc)) {
518 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
519 ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
520 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
521 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
523 if ((error = alpm_wait(sc)) == SMB_ENOERR) {
524 low = ALPM_SMBINB(sc, SMBHDATA);
525 high = ALPM_SMBINB(sc, SMBHDATB);
527 *word = ((high & 0xff) << 8) | (low & 0xff);
530 ALPM_DEBUG(kprintf("alpm: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, *word, error));
537 alpm_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
539 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
543 if (count < 1 || count > 32)
553 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
555 /* set the cmd and reset the
556 * 32-byte long internal buffer */
557 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
559 ALPM_SMBOUTB(sc, SMBHDATA, count);
561 /* fill the 32-byte internal buffer */
562 for (i = 0; i < count; i++) {
563 ALPM_SMBOUTB(sc, SMBHBLOCK, buf[i]);
566 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
567 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
569 error = alpm_wait(sc);
571 ALPM_DEBUG(kprintf("alpm: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
578 alpm_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
580 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
584 if (*count < 1 || *count > 32)
589 if (!alpm_idle(sc)) {
594 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
596 /* set the cmd and reset the
597 * 32-byte long internal buffer */
598 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
600 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
601 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
603 if ((error = alpm_wait(sc)) != SMB_ENOERR)
606 len = ALPM_SMBINB(sc, SMBHDATA);
608 /* read the 32-byte internal buffer */
609 for (i = 0; i < len; i++) {
610 data = ALPM_SMBINB(sc, SMBHBLOCK);
618 ALPM_DEBUG(kprintf("alpm: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, *count, cmd, error));
624 static devclass_t alpm_devclass;
626 static device_method_t alpm_methods[] = {
627 /* device interface */
628 DEVMETHOD(device_probe, alpm_probe),
629 DEVMETHOD(device_attach, alpm_attach),
630 DEVMETHOD(device_detach, alpm_detach),
632 /* smbus interface */
633 DEVMETHOD(smbus_callback, alpm_callback),
634 DEVMETHOD(smbus_quick, alpm_quick),
635 DEVMETHOD(smbus_sendb, alpm_sendb),
636 DEVMETHOD(smbus_recvb, alpm_recvb),
637 DEVMETHOD(smbus_writeb, alpm_writeb),
638 DEVMETHOD(smbus_readb, alpm_readb),
639 DEVMETHOD(smbus_writew, alpm_writew),
640 DEVMETHOD(smbus_readw, alpm_readw),
641 DEVMETHOD(smbus_bwrite, alpm_bwrite),
642 DEVMETHOD(smbus_bread, alpm_bread),
647 static driver_t alpm_driver = {
650 sizeof(struct alpm_softc)
653 DRIVER_MODULE(alpm, pci, alpm_driver, alpm_devclass, NULL, NULL);
654 DRIVER_MODULE(smbus, alpm, smbus_driver, smbus_devclass, NULL, NULL);
655 MODULE_DEPEND(alpm, pci, 1, 1, 1);
656 MODULE_DEPEND(alpm, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
657 MODULE_VERSION(alpm, 1);