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
28 * Power Management support for the Acer M15x3 chipsets
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/systm.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
49 #include <dev/smbus/smbconf.h>
52 #define ALPM_DEBUG(x) if (alpm_debug) (x)
55 static int alpm_debug = 1;
57 static int alpm_debug = 0;
60 #define ACER_M1543_PMU_ID 0x710110b9
63 * I/O registers offsets - the base address is programmed via the
64 * SMBBA PCI configuration register
66 #define SMBSTS 0x0 /* SMBus host/slave status register */
67 #define SMBCMD 0x1 /* SMBus host/slave command register */
68 #define SMBSTART 0x2 /* start to generate programmed cycle */
69 #define SMBHADDR 0x3 /* host address register */
70 #define SMBHDATA 0x4 /* data A register for host controller */
71 #define SMBHDATB 0x5 /* data B register for host controller */
72 #define SMBHBLOCK 0x6 /* block register for host controller */
73 #define SMBHCMD 0x7 /* command register for host controller */
76 #define LSB 0x1 /* XXX: Better name: Read/Write? */
79 #define TERMINATE 0x80
80 #define BUS_COLLI 0x40
81 #define DEVICE_ERR 0x20
82 #define SMI_I_STS 0x10
85 #define HSTSLV_STS 0x02
86 #define HSTSLV_BSY 0x01
89 #define SMB_BLK_CLR 0x80
90 #define T_OUT_CMD 0x08
91 #define ABORT_HOST 0x04
95 #define SMBSRBYTE 0x10 /* send/receive byte */
96 #define SMBWRBYTE 0x20 /* write/read byte */
97 #define SMBWRWORD 0x30 /* write/read word */
98 #define SMBWRBLOCK 0x40 /* write/read block */
100 /* PCI configuration registers and masks
103 #define COM_ENABLE_IO 0x1
105 #define SMBBA PCIR_BAR(1)
108 #define ATPC_SMBCTRL 0x04 /* XX linux has this as 0x6 */
111 #define SMBHSI_SLAVE 0x2
112 #define SMBHSI_HOST 0x1
115 #define SMBHCBC_CLOCK 0x70
117 #define SMBCLOCK_149K 0x0
118 #define SMBCLOCK_74K 0x20
119 #define SMBCLOCK_37K 0x40
120 #define SMBCLOCK_223K 0x80
121 #define SMBCLOCK_111K 0xa0
122 #define SMBCLOCK_55K 0xc0
126 struct resource *res;
127 bus_space_tag_t smbst;
128 bus_space_handle_t smbsh;
133 #define ALPM_LOCK(alpm) mtx_lock(&(alpm)->lock)
134 #define ALPM_UNLOCK(alpm) mtx_unlock(&(alpm)->lock)
135 #define ALPM_LOCK_ASSERT(alpm) mtx_assert(&(alpm)->lock, MA_OWNED)
137 #define ALPM_SMBINB(alpm,register) \
138 (bus_space_read_1(alpm->smbst, alpm->smbsh, register))
139 #define ALPM_SMBOUTB(alpm,register,value) \
140 (bus_space_write_1(alpm->smbst, alpm->smbsh, register, value))
142 static int alpm_detach(device_t dev);
145 alpm_probe(device_t dev)
148 if (pci_get_devid(dev) == ACER_M1543_PMU_ID) {
149 device_set_desc(dev, "AcerLabs M15x3 Power Management Unit");
151 return (BUS_PROBE_DEFAULT);
158 alpm_attach(device_t dev)
162 struct alpm_softc *alpm;
164 alpm = device_get_softc(dev);
166 /* Unlock SMBIO base register access */
167 l = pci_read_config(dev, ATPC, 1);
168 pci_write_config(dev, ATPC, l & ~ATPC_SMBCTRL, 1);
171 * XX linux sets clock to 74k, should we?
172 l = pci_read_config(dev, SMBHCBC, 1);
175 pci_write_config(dev, SMBHCBC, l, 1);
178 if (bootverbose || alpm_debug) {
179 l = pci_read_config(dev, SMBHSI, 1);
180 device_printf(dev, "%s/%s",
181 (l & SMBHSI_HOST) ? "host":"nohost",
182 (l & SMBHSI_SLAVE) ? "slave":"noslave");
184 l = pci_read_config(dev, SMBHCBC, 1);
185 switch (l & SMBHCBC_CLOCK) {
212 alpm->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
215 if (alpm->res == NULL) {
216 device_printf(dev,"Could not allocate Bus space\n");
219 alpm->smbst = rman_get_bustag(alpm->res);
220 alpm->smbsh = rman_get_bushandle(alpm->res);
221 mtx_init(&alpm->lock, device_get_nameunit(dev), "alpm", MTX_DEF);
223 /* attach the smbus */
224 alpm->smbus = device_add_child(dev, "smbus", -1);
225 if (alpm->smbus == NULL) {
229 bus_generic_attach(dev);
235 alpm_detach(device_t dev)
237 struct alpm_softc *alpm = device_get_softc(dev);
240 device_delete_child(dev, alpm->smbus);
243 mtx_destroy(&alpm->lock);
246 bus_release_resource(dev, SYS_RES_IOPORT, SMBBA, alpm->res);
252 alpm_callback(device_t dev, int index, void *data)
257 case SMB_REQUEST_BUS:
258 case SMB_RELEASE_BUS:
259 /* ok, bus allocation accepted */
269 alpm_clear(struct alpm_softc *sc)
271 ALPM_SMBOUTB(sc, SMBSTS, 0xff);
279 alpm_abort(struct alpm_softc *sc)
281 ALPM_SMBOUTB(sc, SMBCMD, T_OUT_CMD | ABORT_HOST);
288 alpm_idle(struct alpm_softc *sc)
292 sts = ALPM_SMBINB(sc, SMBSTS);
294 ALPM_DEBUG(printf("alpm: idle? STS=0x%x\n", sts));
296 return (sts & IDL_STS);
300 * Poll the SMBus controller
303 alpm_wait(struct alpm_softc *sc)
309 /* wait for command to complete and SMBus controller is idle */
312 sts = ALPM_SMBINB(sc, SMBSTS);
317 ALPM_DEBUG(printf("alpm: STS=0x%x\n", sts));
322 error |= SMB_ETIMEOUT;
330 if (sts & DEVICE_ERR)
331 error |= SMB_EBUSERR;
333 if (error != SMB_ENOERR)
340 alpm_quick(device_t dev, u_char slave, int how)
342 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
347 if (!alpm_idle(sc)) {
354 ALPM_DEBUG(printf("alpm: QWRITE to 0x%x", slave));
355 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
358 ALPM_DEBUG(printf("alpm: QREAD to 0x%x", slave));
359 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
362 panic("%s: unknown QUICK command (%x)!", __func__,
365 ALPM_SMBOUTB(sc, SMBCMD, SMBQUICK);
366 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
368 error = alpm_wait(sc);
370 ALPM_DEBUG(printf(", error=0x%x\n", error));
377 alpm_sendb(device_t dev, u_char slave, char byte)
379 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
384 if (!alpm_idle(sc)) {
389 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
390 ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
391 ALPM_SMBOUTB(sc, SMBHDATA, byte);
392 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
394 error = alpm_wait(sc);
396 ALPM_DEBUG(printf("alpm: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
403 alpm_recvb(device_t dev, u_char slave, char *byte)
405 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
410 if (!alpm_idle(sc)) {
415 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
416 ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
417 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
419 if ((error = alpm_wait(sc)) == SMB_ENOERR)
420 *byte = ALPM_SMBINB(sc, SMBHDATA);
422 ALPM_DEBUG(printf("alpm: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
429 alpm_writeb(device_t dev, u_char slave, char cmd, char byte)
431 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
436 if (!alpm_idle(sc)) {
441 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
442 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
443 ALPM_SMBOUTB(sc, SMBHDATA, byte);
444 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
445 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
447 error = alpm_wait(sc);
449 ALPM_DEBUG(printf("alpm: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
456 alpm_readb(device_t dev, u_char slave, char cmd, char *byte)
458 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
463 if (!alpm_idle(sc)) {
468 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
469 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
470 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
471 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
473 if ((error = alpm_wait(sc)) == SMB_ENOERR)
474 *byte = ALPM_SMBINB(sc, SMBHDATA);
476 ALPM_DEBUG(printf("alpm: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, *byte, error));
483 alpm_writew(device_t dev, u_char slave, char cmd, short word)
485 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
490 if (!alpm_idle(sc)) {
495 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
496 ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
497 ALPM_SMBOUTB(sc, SMBHDATA, word & 0x00ff);
498 ALPM_SMBOUTB(sc, SMBHDATB, (word & 0xff00) >> 8);
499 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
500 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
502 error = alpm_wait(sc);
504 ALPM_DEBUG(printf("alpm: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
511 alpm_readw(device_t dev, u_char slave, char cmd, short *word)
513 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
519 if (!alpm_idle(sc)) {
524 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
525 ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
526 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
527 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
529 if ((error = alpm_wait(sc)) == SMB_ENOERR) {
530 low = ALPM_SMBINB(sc, SMBHDATA);
531 high = ALPM_SMBINB(sc, SMBHDATB);
533 *word = ((high & 0xff) << 8) | (low & 0xff);
536 ALPM_DEBUG(printf("alpm: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, *word, error));
543 alpm_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
545 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
549 if (count < 1 || count > 32)
559 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
561 /* set the cmd and reset the
562 * 32-byte long internal buffer */
563 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
565 ALPM_SMBOUTB(sc, SMBHDATA, count);
567 /* fill the 32-byte internal buffer */
568 for (i = 0; i < count; i++) {
569 ALPM_SMBOUTB(sc, SMBHBLOCK, buf[i]);
572 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
573 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
575 error = alpm_wait(sc);
577 ALPM_DEBUG(printf("alpm: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
584 alpm_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
586 struct alpm_softc *sc = (struct alpm_softc *)device_get_softc(dev);
590 if (*count < 1 || *count > 32)
595 if (!alpm_idle(sc)) {
600 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
602 /* set the cmd and reset the
603 * 32-byte long internal buffer */
604 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
606 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
607 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
609 if ((error = alpm_wait(sc)) != SMB_ENOERR)
612 len = ALPM_SMBINB(sc, SMBHDATA);
614 /* read the 32-byte internal buffer */
615 for (i = 0; i < len; i++) {
616 data = ALPM_SMBINB(sc, SMBHBLOCK);
624 ALPM_DEBUG(printf("alpm: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, *count, cmd, error));
630 static devclass_t alpm_devclass;
632 static device_method_t alpm_methods[] = {
633 /* device interface */
634 DEVMETHOD(device_probe, alpm_probe),
635 DEVMETHOD(device_attach, alpm_attach),
636 DEVMETHOD(device_detach, alpm_detach),
638 /* smbus interface */
639 DEVMETHOD(smbus_callback, alpm_callback),
640 DEVMETHOD(smbus_quick, alpm_quick),
641 DEVMETHOD(smbus_sendb, alpm_sendb),
642 DEVMETHOD(smbus_recvb, alpm_recvb),
643 DEVMETHOD(smbus_writeb, alpm_writeb),
644 DEVMETHOD(smbus_readb, alpm_readb),
645 DEVMETHOD(smbus_writew, alpm_writew),
646 DEVMETHOD(smbus_readw, alpm_readw),
647 DEVMETHOD(smbus_bwrite, alpm_bwrite),
648 DEVMETHOD(smbus_bread, alpm_bread),
653 static driver_t alpm_driver = {
656 sizeof(struct alpm_softc)
659 DRIVER_MODULE(alpm, pci, alpm_driver, alpm_devclass, 0, 0);
660 DRIVER_MODULE(smbus, alpm, smbus_driver, smbus_devclass, 0, 0);
661 MODULE_DEPEND(alpm, pci, 1, 1, 1);
662 MODULE_DEPEND(alpm, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
663 MODULE_VERSION(alpm, 1);