2 * Copyright (c) 1998, 1999 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.8 1999/12/03 08:41:21 mdodd Exp $
27 * $DragonFly: src/sys/dev/powermng/i386/alpm/alpm.c,v 1.2 2003/06/17 04:28:56 dillon Exp $
32 * Power Management support for the Acer M15x3 chipsets
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/module.h>
42 #include <sys/malloc.h>
44 #include <machine/clock.h>
46 #include <machine/bus_pio.h>
47 #include <machine/bus_memio.h>
48 #include <machine/bus.h>
50 #include <pci/pcivar.h>
51 #include <pci/pcireg.h>
53 #include <dev/iicbus/iiconf.h>
54 #include <dev/smbus/smbconf.h>
59 #define ALPM_DEBUG(x) if (alpm_debug) (x)
62 static int alpm_debug = 1;
64 static int alpm_debug = 0;
67 #define ACER_M1543_PMU_ID 0x710110b9
69 /* Uncomment this line to force another I/O base address for SMB */
70 /* #define ALPM_SMBIO_BASE_ADDR 0x3a80 */
72 /* I/O registers offsets - the base address is programmed via the
73 * SMBBA PCI configuration register
75 #define SMBSTS 0x0 /* SMBus host/slave status register */
76 #define SMBCMD 0x1 /* SMBus host/slave command register */
77 #define SMBSTART 0x2 /* start to generate programmed cycle */
78 #define SMBHADDR 0x3 /* host address register */
79 #define SMBHDATA 0x4 /* data A register for host controller */
80 #define SMBHDATB 0x5 /* data B register for host controller */
81 #define SMBHBLOCK 0x6 /* block register for host controller */
82 #define SMBHCMD 0x7 /* command register for host controller */
85 #define TERMINATE 0x80
86 #define BUS_COLLI 0x40
87 #define DEVICE_ERR 0x20
88 #define SMI_I_STS 0x10
91 #define HSTSLV_STS 0x02
92 #define HSTSLV_BSY 0x01
95 #define SMB_BLK_CLR 0x80
96 #define T_OUT_CMD 0x08
97 #define ABORT_HOST 0x04
100 #define SMBQUICK 0x00
101 #define SMBSRBYTE 0x10 /* send/receive byte */
102 #define SMBWRBYTE 0x20 /* write/read byte */
103 #define SMBWRWORD 0x30 /* write/read word */
104 #define SMBWRBLOCK 0x40 /* write/read block */
106 /* PCI configuration registers and masks
109 #define COM_ENABLE_IO 0x1
114 #define ATPC_SMBCTRL 0x04
117 #define SMBHSI_SLAVE 0x2
118 #define SMBHSI_HOST 0x1
121 #define SMBHCBC_CLOCK 0x70
123 #define SMBCLOCK_149K 0x0
124 #define SMBCLOCK_74K 0x20
125 #define SMBCLOCK_37K 0x40
126 #define SMBCLOCK_223K 0x80
127 #define SMBCLOCK_111K 0xa0
128 #define SMBCLOCK_55K 0xc0
132 bus_space_tag_t smbst;
133 bus_space_handle_t smbsh;
136 struct alpm_data alpmdata[NALPM];
141 struct alpm_data *alpm;
144 #define ALPM_SMBINB(alsmb,register) \
145 (bus_space_read_1(alsmb->alpm->smbst, alsmb->alpm->smbsh, register))
146 #define ALPM_SMBOUTB(alsmb,register,value) \
147 (bus_space_write_1(alsmb->alpm->smbst, alsmb->alpm->smbsh, register, value))
149 static int alsmb_probe(device_t);
150 static int alsmb_attach(device_t);
151 static int alsmb_smb_callback(device_t, int, caddr_t *);
152 static int alsmb_smb_quick(device_t dev, u_char slave, int how);
153 static int alsmb_smb_sendb(device_t dev, u_char slave, char byte);
154 static int alsmb_smb_recvb(device_t dev, u_char slave, char *byte);
155 static int alsmb_smb_writeb(device_t dev, u_char slave, char cmd, char byte);
156 static int alsmb_smb_readb(device_t dev, u_char slave, char cmd, char *byte);
157 static int alsmb_smb_writew(device_t dev, u_char slave, char cmd, short word);
158 static int alsmb_smb_readw(device_t dev, u_char slave, char cmd, short *word);
159 static int alsmb_smb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf);
160 static int alsmb_smb_bread(device_t dev, u_char slave, char cmd, u_char count, char *byte);
162 static devclass_t alsmb_devclass;
164 static device_method_t alsmb_methods[] = {
165 /* device interface */
166 DEVMETHOD(device_probe, alsmb_probe),
167 DEVMETHOD(device_attach, alsmb_attach),
170 DEVMETHOD(bus_print_child, bus_generic_print_child),
172 /* smbus interface */
173 DEVMETHOD(smbus_callback, alsmb_smb_callback),
174 DEVMETHOD(smbus_quick, alsmb_smb_quick),
175 DEVMETHOD(smbus_sendb, alsmb_smb_sendb),
176 DEVMETHOD(smbus_recvb, alsmb_smb_recvb),
177 DEVMETHOD(smbus_writeb, alsmb_smb_writeb),
178 DEVMETHOD(smbus_readb, alsmb_smb_readb),
179 DEVMETHOD(smbus_writew, alsmb_smb_writew),
180 DEVMETHOD(smbus_readw, alsmb_smb_readw),
181 DEVMETHOD(smbus_bwrite, alsmb_smb_bwrite),
182 DEVMETHOD(smbus_bread, alsmb_smb_bread),
187 static driver_t alsmb_driver = {
190 sizeof(struct alsmb_softc),
193 static const char* alpm_pci_probe(pcici_t tag, pcidi_t type);
194 static void alpm_pci_attach(pcici_t tag, int unit);
196 static u_long alpm_count;
198 static struct pci_device alpm_device = {
205 COMPAT_PCI_DRIVER (alpm, alpm_device);
208 alpm_pci_probe(pcici_t tag, pcidi_t type)
210 if (type == ACER_M1543_PMU_ID)
211 return ("AcerLabs M15x3 Power Management Unit");
217 alpm_pci_attach(pcici_t tag, int unit)
219 struct alpm_data *alpm;
223 printf("alpm%d: attach: only %d units configured.\n",
227 alpm = &alpmdata[unit];
231 /* Unlock SMBIO base register access */
232 l = pci_cfgread(tag, ATPC, 1);
233 pci_cfgwrite(tag, ATPC, l & ~ATPC_SMBCTRL, 1);
236 l = pci_cfgread(tag, SMBHSI, 1);
237 printf("alsmb%d: %s/%s", unit,
238 (l & SMBHSI_HOST) ? "host":"nohost",
239 (l & SMBHSI_SLAVE) ? "slave":"noslave");
241 l = pci_cfgread(tag, SMBHCBC, 1);
242 switch (l & SMBHCBC_CLOCK) {
264 alpm->smbst = I386_BUS_SPACE_IO;
266 #ifdef ALPM_SMBIO_BASE_ADDR
268 l = pci_cfgread(tag, COM, 2);
269 pci_cfgwrite(tag, COM, l & ~COM_ENABLE_IO, 2);
271 /* set the I/O base address */
272 pci_cfgwrite(tag, SMBBA, ALPM_SMBIO_BASE_ADDR | 0x1, 4);
275 pci_cfgwrite(tag, COM, l | COM_ENABLE_IO, 2);
277 alpm->smbsh = ALPM_SMBIO_BASE_ADDR;
279 alpm->smbsh = pci_cfgread(tag, SMBBA, 4) & ~0x1;
282 printf(" at 0x%x\n", alpm->smbsh);
284 /* XXX add the I2C interface to the root_bus until pcibus is ready */
285 device_add_child(root_bus, "alsmb", unit);
291 * Not a real probe, we know the device exists since the device has
292 * been added after the successfull pci probe.
295 alsmb_probe(device_t dev)
297 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
299 sc->alpm = &alpmdata[device_get_unit(dev)];
301 device_set_desc(dev, "Aladdin IV/V/Pro2 SMBus controller");
307 alsmb_attach(device_t dev)
309 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
311 /* allocate a new smbus device */
312 sc->smbus = smbus_alloc_bus(dev);
314 /* probe and attach the smbus */
315 device_probe_and_attach(sc->smbus);
321 alsmb_smb_callback(device_t dev, int index, caddr_t *data)
326 case SMB_REQUEST_BUS:
327 case SMB_RELEASE_BUS:
328 /* ok, bus allocation accepted */
338 alsmb_clear(struct alsmb_softc *sc)
340 ALPM_SMBOUTB(sc, SMBSTS, 0xff);
348 alsmb_abort(struct alsmb_softc *sc)
350 ALPM_SMBOUTB(sc, SMBCMD, T_OUT_CMD | ABORT_HOST);
357 alsmb_idle(struct alsmb_softc *sc)
361 sts = ALPM_SMBINB(sc, SMBSTS);
363 ALPM_DEBUG(printf("alpm: idle? STS=0x%x\n", sts));
365 return (sts & IDL_STS);
369 * Poll the SMBus controller
372 alsmb_wait(struct alsmb_softc *sc)
378 /* wait for command to complete and SMBus controller is idle */
381 sts = ALPM_SMBINB(sc, SMBSTS);
386 ALPM_DEBUG(printf("alpm: STS=0x%x\n", sts));
391 error |= SMB_ETIMEOUT;
399 if (sts & DEVICE_ERR)
400 error |= SMB_EBUSERR;
402 if (error != SMB_ENOERR)
409 alsmb_smb_quick(device_t dev, u_char slave, int how)
411 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
420 ALPM_DEBUG(printf("alpm: QWRITE to 0x%x", slave));
421 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
424 ALPM_DEBUG(printf("alpm: QREAD to 0x%x", slave));
425 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
428 panic("%s: unknown QUICK command (%x)!", __FUNCTION__,
431 ALPM_SMBOUTB(sc, SMBCMD, SMBQUICK);
432 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
434 error = alsmb_wait(sc);
436 ALPM_DEBUG(printf(", error=0x%x\n", error));
442 alsmb_smb_sendb(device_t dev, u_char slave, char byte)
444 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
451 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
452 ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
453 ALPM_SMBOUTB(sc, SMBHDATA, byte);
454 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
456 error = alsmb_wait(sc);
458 ALPM_DEBUG(printf("alpm: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
464 alsmb_smb_recvb(device_t dev, u_char slave, char *byte)
466 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
473 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
474 ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
475 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
477 if ((error = alsmb_wait(sc)) == SMB_ENOERR)
478 *byte = ALPM_SMBINB(sc, SMBHDATA);
480 ALPM_DEBUG(printf("alpm: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
486 alsmb_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
488 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
495 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
496 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
497 ALPM_SMBOUTB(sc, SMBHDATA, byte);
498 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
499 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
501 error = alsmb_wait(sc);
503 ALPM_DEBUG(printf("alpm: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
509 alsmb_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
511 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
518 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
519 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
520 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
521 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
523 if ((error = alsmb_wait(sc)) == SMB_ENOERR)
524 *byte = ALPM_SMBINB(sc, SMBHDATA);
526 ALPM_DEBUG(printf("alpm: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, *byte, error));
532 alsmb_smb_writew(device_t dev, u_char slave, char cmd, short word)
534 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
541 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
542 ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
543 ALPM_SMBOUTB(sc, SMBHDATA, word & 0x00ff);
544 ALPM_SMBOUTB(sc, SMBHDATB, (word & 0xff00) >> 8);
545 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
546 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
548 error = alsmb_wait(sc);
550 ALPM_DEBUG(printf("alpm: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
556 alsmb_smb_readw(device_t dev, u_char slave, char cmd, short *word)
558 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
566 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
567 ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
568 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
569 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
571 if ((error = alsmb_wait(sc)) == SMB_ENOERR) {
572 low = ALPM_SMBINB(sc, SMBHDATA);
573 high = ALPM_SMBINB(sc, SMBHDATB);
575 *word = ((high & 0xff) << 8) | (low & 0xff);
578 ALPM_DEBUG(printf("alpm: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, *word, error));
584 alsmb_smb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
586 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
587 u_char remain, len, i;
588 int error = SMB_ENOERR;
596 len = min(remain, 32);
598 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
600 /* set the cmd and reset the
601 * 32-byte long internal buffer */
602 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
604 ALPM_SMBOUTB(sc, SMBHDATA, len);
606 /* fill the 32-byte internal buffer */
607 for (i=0; i<len; i++) {
608 ALPM_SMBOUTB(sc, SMBHBLOCK, buf[count-remain+i]);
611 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
612 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
614 if ((error = alsmb_wait(sc)) != SMB_ENOERR)
621 ALPM_DEBUG(printf("alpm: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
627 alsmb_smb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf)
629 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
630 u_char remain, len, i;
631 int error = SMB_ENOERR;
639 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
641 /* set the cmd and reset the
642 * 32-byte long internal buffer */
643 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
645 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
646 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
648 if ((error = alsmb_wait(sc)) != SMB_ENOERR)
651 len = ALPM_SMBINB(sc, SMBHDATA);
653 /* read the 32-byte internal buffer */
654 for (i=0; i<len; i++) {
655 buf[count-remain+i] = ALPM_SMBINB(sc, SMBHBLOCK);
662 ALPM_DEBUG(printf("alpm: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
667 DRIVER_MODULE(alsmb, root, alsmb_driver, alsmb_devclass, 0, 0);