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.15 2001/01/17 00:38:06 peter Exp $
27 * $DragonFly: src/sys/dev/powermng/i386/alpm/alpm.c,v 1.7 2006/10/25 20:56:00 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 <bus/pci/pcivar.h>
43 #include <bus/pci/pcireg.h>
45 #include <bus/iicbus/iiconf.h>
46 #include <bus/smbus/smbconf.h>
49 #define ALPM_DEBUG(x) if (alpm_debug) (x)
52 static int alpm_debug = 1;
54 static int alpm_debug = 0;
57 #define ACER_M1543_PMU_ID 0x710110b9
59 /* Uncomment this line to force another I/O base address for SMB */
60 /* #define ALPM_SMBIO_BASE_ADDR 0x3a80 */
62 /* I/O registers offsets - the base address is programmed via the
63 * SMBBA PCI configuration register
65 #define SMBSTS 0x0 /* SMBus host/slave status register */
66 #define SMBCMD 0x1 /* SMBus host/slave command register */
67 #define SMBSTART 0x2 /* start to generate programmed cycle */
68 #define SMBHADDR 0x3 /* host address register */
69 #define SMBHDATA 0x4 /* data A register for host controller */
70 #define SMBHDATB 0x5 /* data B register for host controller */
71 #define SMBHBLOCK 0x6 /* block register for host controller */
72 #define SMBHCMD 0x7 /* command register for host controller */
75 #define TERMINATE 0x80
76 #define BUS_COLLI 0x40
77 #define DEVICE_ERR 0x20
78 #define SMI_I_STS 0x10
81 #define HSTSLV_STS 0x02
82 #define HSTSLV_BSY 0x01
85 #define SMB_BLK_CLR 0x80
86 #define T_OUT_CMD 0x08
87 #define ABORT_HOST 0x04
91 #define SMBSRBYTE 0x10 /* send/receive byte */
92 #define SMBWRBYTE 0x20 /* write/read byte */
93 #define SMBWRWORD 0x30 /* write/read word */
94 #define SMBWRBLOCK 0x40 /* write/read block */
96 /* PCI configuration registers and masks
99 #define COM_ENABLE_IO 0x1
104 #define ATPC_SMBCTRL 0x04 /* XX linux has this as 0x6 */
107 #define SMBHSI_SLAVE 0x2
108 #define SMBHSI_HOST 0x1
111 #define SMBHCBC_CLOCK 0x70
113 #define SMBCLOCK_149K 0x0
114 #define SMBCLOCK_74K 0x20
115 #define SMBCLOCK_37K 0x40
116 #define SMBCLOCK_223K 0x80
117 #define SMBCLOCK_111K 0xa0
118 #define SMBCLOCK_55K 0xc0
122 bus_space_tag_t smbst;
123 bus_space_handle_t smbsh;
129 struct alpm_data *alpm;
132 #define ALPM_SMBINB(alsmb,register) \
133 (bus_space_read_1(alsmb->alpm->smbst, alsmb->alpm->smbsh, register))
134 #define ALPM_SMBOUTB(alsmb,register,value) \
135 (bus_space_write_1(alsmb->alpm->smbst, alsmb->alpm->smbsh, register, value))
137 static int alsmb_probe(device_t);
138 static int alsmb_attach(device_t);
139 static int alsmb_smb_callback(device_t, int, caddr_t *);
140 static int alsmb_smb_quick(device_t dev, u_char slave, int how);
141 static int alsmb_smb_sendb(device_t dev, u_char slave, char byte);
142 static int alsmb_smb_recvb(device_t dev, u_char slave, char *byte);
143 static int alsmb_smb_writeb(device_t dev, u_char slave, char cmd, char byte);
144 static int alsmb_smb_readb(device_t dev, u_char slave, char cmd, char *byte);
145 static int alsmb_smb_writew(device_t dev, u_char slave, char cmd, short word);
146 static int alsmb_smb_readw(device_t dev, u_char slave, char cmd, short *word);
147 static int alsmb_smb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf);
148 static int alsmb_smb_bread(device_t dev, u_char slave, char cmd, u_char count, char *byte);
150 static devclass_t alsmb_devclass;
152 static device_method_t alsmb_methods[] = {
153 /* device interface */
154 DEVMETHOD(device_probe, alsmb_probe),
155 DEVMETHOD(device_attach, alsmb_attach),
158 DEVMETHOD(bus_print_child, bus_generic_print_child),
160 /* smbus interface */
161 DEVMETHOD(smbus_callback, alsmb_smb_callback),
162 DEVMETHOD(smbus_quick, alsmb_smb_quick),
163 DEVMETHOD(smbus_sendb, alsmb_smb_sendb),
164 DEVMETHOD(smbus_recvb, alsmb_smb_recvb),
165 DEVMETHOD(smbus_writeb, alsmb_smb_writeb),
166 DEVMETHOD(smbus_readb, alsmb_smb_readb),
167 DEVMETHOD(smbus_writew, alsmb_smb_writew),
168 DEVMETHOD(smbus_readw, alsmb_smb_readw),
169 DEVMETHOD(smbus_bwrite, alsmb_smb_bwrite),
170 DEVMETHOD(smbus_bread, alsmb_smb_bread),
175 static driver_t alsmb_driver = {
178 sizeof(struct alsmb_softc),
181 static int alpm_pci_probe(device_t dev);
182 static int alpm_pci_attach(device_t dev);
184 static devclass_t alpm_devclass;
186 static device_method_t alpm_pci_methods[] = {
187 /* device interface */
188 DEVMETHOD(device_probe, alpm_pci_probe),
189 DEVMETHOD(device_attach, alpm_pci_attach),
194 static driver_t alpm_pci_driver = {
197 sizeof(struct alpm_data)
201 alpm_pci_probe(device_t dev)
203 if(pci_get_devid(dev) == ACER_M1543_PMU_ID) {
204 device_set_desc(dev, "AcerLabs M15x3 Power Management Unit");
212 alpm_pci_attach(device_t dev)
216 struct alpm_data *alpm;
217 struct resource *res;
218 device_t smbinterface;
220 alpm = device_get_softc(dev);
221 unit = device_get_unit(dev);
223 /* Unlock SMBIO base register access */
224 l = pci_read_config(dev, ATPC, 1);
225 pci_write_config(dev, ATPC, l & ~ATPC_SMBCTRL, 1);
228 * XX linux sets clock to 74k, should we?
229 l = pci_read_config(dev, SMBHCBC, 1);
232 pci_write_config(dev, SMBHCBC, l, 1)
236 l = pci_read_config(dev, SMBHSI, 1);
237 printf("alsmb%d: %s/%s", unit,
238 (l & SMBHSI_HOST) ? "host":"nohost",
239 (l & SMBHSI_SLAVE) ? "slave":"noslave");
241 l = pci_read_config(dev, SMBHCBC, 1);
242 switch (l & SMBHCBC_CLOCK) {
264 #ifdef ALPM_SMBIO_BASE_ADDR
265 /* XX will this even work anymore? */
267 l = pci_read_config(dev, COM, 2);
268 pci_write_config(dev, COM, l & ~COM_ENABLE_IO, 2);
270 /* set the I/O base address */
271 pci_write_config(dev, SMBBA, ALPM_SMBIO_BASE_ADDR | 0x1, 4);
274 pci_write_config(dev, COM, l | COM_ENABLE_IO, 2);
278 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
279 0, ~0, 1, RF_ACTIVE);
281 device_printf(dev, "Could not allocate Bus space\n");
284 alpm->smbst = rman_get_bustag(res);
285 alpm->smbsh = rman_get_bushandle(res);
288 printf(" at 0x%x\n", alpm->smbsh);
290 smbinterface = device_add_child(dev, "alsmb", unit);
292 device_printf(dev, "could not add SMBus device\n");
294 device_probe_and_attach(smbinterface);
299 * Not a real probe, we know the device exists since the device has
300 * been added after the successfull pci probe.
303 alsmb_probe(device_t dev)
305 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
307 /* allocate a new smbus device */
308 sc->smbus = smbus_alloc_bus(dev);
311 device_set_desc(dev, "Aladdin IV/V/Pro2 SMBus controller");
317 alsmb_attach(device_t dev)
319 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
321 sc->alpm = device_get_softc(device_get_parent(dev));
323 /* probe and attach the smbus */
324 device_probe_and_attach(sc->smbus);
330 alsmb_smb_callback(device_t dev, int index, caddr_t *data)
335 case SMB_REQUEST_BUS:
336 case SMB_RELEASE_BUS:
337 /* ok, bus allocation accepted */
347 alsmb_clear(struct alsmb_softc *sc)
349 ALPM_SMBOUTB(sc, SMBSTS, 0xff);
357 alsmb_abort(struct alsmb_softc *sc)
359 ALPM_SMBOUTB(sc, SMBCMD, T_OUT_CMD | ABORT_HOST);
366 alsmb_idle(struct alsmb_softc *sc)
370 sts = ALPM_SMBINB(sc, SMBSTS);
372 ALPM_DEBUG(printf("alpm: idle? STS=0x%x\n", sts));
374 return (sts & IDL_STS);
378 * Poll the SMBus controller
381 alsmb_wait(struct alsmb_softc *sc)
387 /* wait for command to complete and SMBus controller is idle */
390 sts = ALPM_SMBINB(sc, SMBSTS);
395 ALPM_DEBUG(printf("alpm: STS=0x%x\n", sts));
400 error |= SMB_ETIMEOUT;
408 if (sts & DEVICE_ERR)
409 error |= SMB_EBUSERR;
411 if (error != SMB_ENOERR)
418 alsmb_smb_quick(device_t dev, u_char slave, int how)
420 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
429 ALPM_DEBUG(printf("alpm: QWRITE to 0x%x", slave));
430 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
433 ALPM_DEBUG(printf("alpm: QREAD to 0x%x", slave));
434 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
437 panic("%s: unknown QUICK command (%x)!", __func__, how);
439 ALPM_SMBOUTB(sc, SMBCMD, SMBQUICK);
440 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
442 error = alsmb_wait(sc);
444 ALPM_DEBUG(printf(", error=0x%x\n", error));
450 alsmb_smb_sendb(device_t dev, u_char slave, char byte)
452 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
459 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
460 ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
461 ALPM_SMBOUTB(sc, SMBHDATA, byte);
462 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
464 error = alsmb_wait(sc);
466 ALPM_DEBUG(printf("alpm: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
472 alsmb_smb_recvb(device_t dev, u_char slave, char *byte)
474 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
481 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
482 ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
483 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
485 if ((error = alsmb_wait(sc)) == SMB_ENOERR)
486 *byte = ALPM_SMBINB(sc, SMBHDATA);
488 ALPM_DEBUG(printf("alpm: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
494 alsmb_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
496 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
503 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
504 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
505 ALPM_SMBOUTB(sc, SMBHDATA, byte);
506 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
507 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
509 error = alsmb_wait(sc);
511 ALPM_DEBUG(printf("alpm: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
517 alsmb_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
519 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
526 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
527 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
528 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
529 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
531 if ((error = alsmb_wait(sc)) == SMB_ENOERR)
532 *byte = ALPM_SMBINB(sc, SMBHDATA);
534 ALPM_DEBUG(printf("alpm: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, *byte, error));
540 alsmb_smb_writew(device_t dev, u_char slave, char cmd, short word)
542 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
549 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
550 ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
551 ALPM_SMBOUTB(sc, SMBHDATA, word & 0x00ff);
552 ALPM_SMBOUTB(sc, SMBHDATB, (word & 0xff00) >> 8);
553 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
554 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
556 error = alsmb_wait(sc);
558 ALPM_DEBUG(printf("alpm: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
564 alsmb_smb_readw(device_t dev, u_char slave, char cmd, short *word)
566 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
574 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
575 ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
576 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
577 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
579 if ((error = alsmb_wait(sc)) == SMB_ENOERR) {
580 low = ALPM_SMBINB(sc, SMBHDATA);
581 high = ALPM_SMBINB(sc, SMBHDATB);
583 *word = ((high & 0xff) << 8) | (low & 0xff);
586 ALPM_DEBUG(printf("alpm: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, *word, error));
592 alsmb_smb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
594 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
595 u_char remain, len, i;
596 int error = SMB_ENOERR;
604 len = min(remain, 32);
606 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
608 /* set the cmd and reset the
609 * 32-byte long internal buffer */
610 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
612 ALPM_SMBOUTB(sc, SMBHDATA, len);
614 /* fill the 32-byte internal buffer */
615 for (i=0; i<len; i++) {
616 ALPM_SMBOUTB(sc, SMBHBLOCK, buf[count-remain+i]);
619 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
620 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
622 if ((error = alsmb_wait(sc)) != SMB_ENOERR)
629 ALPM_DEBUG(printf("alpm: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
635 alsmb_smb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf)
637 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
638 u_char remain, len, i;
639 int error = SMB_ENOERR;
647 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
649 /* set the cmd and reset the
650 * 32-byte long internal buffer */
651 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
653 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
654 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
656 if ((error = alsmb_wait(sc)) != SMB_ENOERR)
659 len = ALPM_SMBINB(sc, SMBHDATA);
661 /* read the 32-byte internal buffer */
662 for (i=0; i<len; i++) {
663 buf[count-remain+i] = ALPM_SMBINB(sc, SMBHBLOCK);
670 ALPM_DEBUG(printf("alpm: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
675 DRIVER_MODULE(alpm, pci, alpm_pci_driver, alpm_devclass, 0, 0);
676 DRIVER_MODULE(alsmb, alpm, alsmb_driver, alsmb_devclass, 0, 0);