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.6 2005/02/17 13:59:36 joerg 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>
41 #include <machine/bus_pio.h>
42 #include <machine/bus_memio.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
47 #include <bus/pci/pcivar.h>
48 #include <bus/pci/pcireg.h>
50 #include <bus/iicbus/iiconf.h>
51 #include <bus/smbus/smbconf.h>
54 #define ALPM_DEBUG(x) if (alpm_debug) (x)
57 static int alpm_debug = 1;
59 static int alpm_debug = 0;
62 #define ACER_M1543_PMU_ID 0x710110b9
64 /* Uncomment this line to force another I/O base address for SMB */
65 /* #define ALPM_SMBIO_BASE_ADDR 0x3a80 */
67 /* I/O registers offsets - the base address is programmed via the
68 * SMBBA PCI configuration register
70 #define SMBSTS 0x0 /* SMBus host/slave status register */
71 #define SMBCMD 0x1 /* SMBus host/slave command register */
72 #define SMBSTART 0x2 /* start to generate programmed cycle */
73 #define SMBHADDR 0x3 /* host address register */
74 #define SMBHDATA 0x4 /* data A register for host controller */
75 #define SMBHDATB 0x5 /* data B register for host controller */
76 #define SMBHBLOCK 0x6 /* block register for host controller */
77 #define SMBHCMD 0x7 /* command register for host controller */
80 #define TERMINATE 0x80
81 #define BUS_COLLI 0x40
82 #define DEVICE_ERR 0x20
83 #define SMI_I_STS 0x10
86 #define HSTSLV_STS 0x02
87 #define HSTSLV_BSY 0x01
90 #define SMB_BLK_CLR 0x80
91 #define T_OUT_CMD 0x08
92 #define ABORT_HOST 0x04
96 #define SMBSRBYTE 0x10 /* send/receive byte */
97 #define SMBWRBYTE 0x20 /* write/read byte */
98 #define SMBWRWORD 0x30 /* write/read word */
99 #define SMBWRBLOCK 0x40 /* write/read block */
101 /* PCI configuration registers and masks
104 #define COM_ENABLE_IO 0x1
109 #define ATPC_SMBCTRL 0x04 /* XX linux has this as 0x6 */
112 #define SMBHSI_SLAVE 0x2
113 #define SMBHSI_HOST 0x1
116 #define SMBHCBC_CLOCK 0x70
118 #define SMBCLOCK_149K 0x0
119 #define SMBCLOCK_74K 0x20
120 #define SMBCLOCK_37K 0x40
121 #define SMBCLOCK_223K 0x80
122 #define SMBCLOCK_111K 0xa0
123 #define SMBCLOCK_55K 0xc0
127 bus_space_tag_t smbst;
128 bus_space_handle_t smbsh;
134 struct alpm_data *alpm;
137 #define ALPM_SMBINB(alsmb,register) \
138 (bus_space_read_1(alsmb->alpm->smbst, alsmb->alpm->smbsh, register))
139 #define ALPM_SMBOUTB(alsmb,register,value) \
140 (bus_space_write_1(alsmb->alpm->smbst, alsmb->alpm->smbsh, register, value))
142 static int alsmb_probe(device_t);
143 static int alsmb_attach(device_t);
144 static int alsmb_smb_callback(device_t, int, caddr_t *);
145 static int alsmb_smb_quick(device_t dev, u_char slave, int how);
146 static int alsmb_smb_sendb(device_t dev, u_char slave, char byte);
147 static int alsmb_smb_recvb(device_t dev, u_char slave, char *byte);
148 static int alsmb_smb_writeb(device_t dev, u_char slave, char cmd, char byte);
149 static int alsmb_smb_readb(device_t dev, u_char slave, char cmd, char *byte);
150 static int alsmb_smb_writew(device_t dev, u_char slave, char cmd, short word);
151 static int alsmb_smb_readw(device_t dev, u_char slave, char cmd, short *word);
152 static int alsmb_smb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf);
153 static int alsmb_smb_bread(device_t dev, u_char slave, char cmd, u_char count, char *byte);
155 static devclass_t alsmb_devclass;
157 static device_method_t alsmb_methods[] = {
158 /* device interface */
159 DEVMETHOD(device_probe, alsmb_probe),
160 DEVMETHOD(device_attach, alsmb_attach),
163 DEVMETHOD(bus_print_child, bus_generic_print_child),
165 /* smbus interface */
166 DEVMETHOD(smbus_callback, alsmb_smb_callback),
167 DEVMETHOD(smbus_quick, alsmb_smb_quick),
168 DEVMETHOD(smbus_sendb, alsmb_smb_sendb),
169 DEVMETHOD(smbus_recvb, alsmb_smb_recvb),
170 DEVMETHOD(smbus_writeb, alsmb_smb_writeb),
171 DEVMETHOD(smbus_readb, alsmb_smb_readb),
172 DEVMETHOD(smbus_writew, alsmb_smb_writew),
173 DEVMETHOD(smbus_readw, alsmb_smb_readw),
174 DEVMETHOD(smbus_bwrite, alsmb_smb_bwrite),
175 DEVMETHOD(smbus_bread, alsmb_smb_bread),
180 static driver_t alsmb_driver = {
183 sizeof(struct alsmb_softc),
186 static int alpm_pci_probe(device_t dev);
187 static int alpm_pci_attach(device_t dev);
189 static devclass_t alpm_devclass;
191 static device_method_t alpm_pci_methods[] = {
192 /* device interface */
193 DEVMETHOD(device_probe, alpm_pci_probe),
194 DEVMETHOD(device_attach, alpm_pci_attach),
199 static driver_t alpm_pci_driver = {
202 sizeof(struct alpm_data)
206 alpm_pci_probe(device_t dev)
208 if(pci_get_devid(dev) == ACER_M1543_PMU_ID) {
209 device_set_desc(dev, "AcerLabs M15x3 Power Management Unit");
217 alpm_pci_attach(device_t dev)
221 struct alpm_data *alpm;
222 struct resource *res;
223 device_t smbinterface;
225 alpm = device_get_softc(dev);
226 unit = device_get_unit(dev);
228 /* Unlock SMBIO base register access */
229 l = pci_read_config(dev, ATPC, 1);
230 pci_write_config(dev, ATPC, l & ~ATPC_SMBCTRL, 1);
233 * XX linux sets clock to 74k, should we?
234 l = pci_read_config(dev, SMBHCBC, 1);
237 pci_write_config(dev, SMBHCBC, l, 1)
241 l = pci_read_config(dev, SMBHSI, 1);
242 printf("alsmb%d: %s/%s", unit,
243 (l & SMBHSI_HOST) ? "host":"nohost",
244 (l & SMBHSI_SLAVE) ? "slave":"noslave");
246 l = pci_read_config(dev, SMBHCBC, 1);
247 switch (l & SMBHCBC_CLOCK) {
269 #ifdef ALPM_SMBIO_BASE_ADDR
270 /* XX will this even work anymore? */
272 l = pci_read_config(dev, COM, 2);
273 pci_write_config(dev, COM, l & ~COM_ENABLE_IO, 2);
275 /* set the I/O base address */
276 pci_write_config(dev, SMBBA, ALPM_SMBIO_BASE_ADDR | 0x1, 4);
279 pci_write_config(dev, COM, l | COM_ENABLE_IO, 2);
283 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
284 0, ~0, 1, RF_ACTIVE);
286 device_printf(dev, "Could not allocate Bus space\n");
289 alpm->smbst = rman_get_bustag(res);
290 alpm->smbsh = rman_get_bushandle(res);
293 printf(" at 0x%x\n", alpm->smbsh);
295 smbinterface = device_add_child(dev, "alsmb", unit);
297 device_printf(dev, "could not add SMBus device\n");
299 device_probe_and_attach(smbinterface);
304 * Not a real probe, we know the device exists since the device has
305 * been added after the successfull pci probe.
308 alsmb_probe(device_t dev)
310 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
312 /* allocate a new smbus device */
313 sc->smbus = smbus_alloc_bus(dev);
316 device_set_desc(dev, "Aladdin IV/V/Pro2 SMBus controller");
322 alsmb_attach(device_t dev)
324 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
326 sc->alpm = device_get_softc(device_get_parent(dev));
328 /* probe and attach the smbus */
329 device_probe_and_attach(sc->smbus);
335 alsmb_smb_callback(device_t dev, int index, caddr_t *data)
340 case SMB_REQUEST_BUS:
341 case SMB_RELEASE_BUS:
342 /* ok, bus allocation accepted */
352 alsmb_clear(struct alsmb_softc *sc)
354 ALPM_SMBOUTB(sc, SMBSTS, 0xff);
362 alsmb_abort(struct alsmb_softc *sc)
364 ALPM_SMBOUTB(sc, SMBCMD, T_OUT_CMD | ABORT_HOST);
371 alsmb_idle(struct alsmb_softc *sc)
375 sts = ALPM_SMBINB(sc, SMBSTS);
377 ALPM_DEBUG(printf("alpm: idle? STS=0x%x\n", sts));
379 return (sts & IDL_STS);
383 * Poll the SMBus controller
386 alsmb_wait(struct alsmb_softc *sc)
392 /* wait for command to complete and SMBus controller is idle */
395 sts = ALPM_SMBINB(sc, SMBSTS);
400 ALPM_DEBUG(printf("alpm: STS=0x%x\n", sts));
405 error |= SMB_ETIMEOUT;
413 if (sts & DEVICE_ERR)
414 error |= SMB_EBUSERR;
416 if (error != SMB_ENOERR)
423 alsmb_smb_quick(device_t dev, u_char slave, int how)
425 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
434 ALPM_DEBUG(printf("alpm: QWRITE to 0x%x", slave));
435 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
438 ALPM_DEBUG(printf("alpm: QREAD to 0x%x", slave));
439 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
442 panic("%s: unknown QUICK command (%x)!", __func__, how);
444 ALPM_SMBOUTB(sc, SMBCMD, SMBQUICK);
445 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
447 error = alsmb_wait(sc);
449 ALPM_DEBUG(printf(", error=0x%x\n", error));
455 alsmb_smb_sendb(device_t dev, u_char slave, char byte)
457 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
464 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
465 ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
466 ALPM_SMBOUTB(sc, SMBHDATA, byte);
467 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
469 error = alsmb_wait(sc);
471 ALPM_DEBUG(printf("alpm: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
477 alsmb_smb_recvb(device_t dev, u_char slave, char *byte)
479 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
486 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
487 ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
488 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
490 if ((error = alsmb_wait(sc)) == SMB_ENOERR)
491 *byte = ALPM_SMBINB(sc, SMBHDATA);
493 ALPM_DEBUG(printf("alpm: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
499 alsmb_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
501 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
508 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
509 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
510 ALPM_SMBOUTB(sc, SMBHDATA, byte);
511 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
512 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
514 error = alsmb_wait(sc);
516 ALPM_DEBUG(printf("alpm: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
522 alsmb_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
524 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
531 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
532 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
533 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
534 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
536 if ((error = alsmb_wait(sc)) == SMB_ENOERR)
537 *byte = ALPM_SMBINB(sc, SMBHDATA);
539 ALPM_DEBUG(printf("alpm: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, *byte, error));
545 alsmb_smb_writew(device_t dev, u_char slave, char cmd, short word)
547 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
554 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
555 ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
556 ALPM_SMBOUTB(sc, SMBHDATA, word & 0x00ff);
557 ALPM_SMBOUTB(sc, SMBHDATB, (word & 0xff00) >> 8);
558 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
559 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
561 error = alsmb_wait(sc);
563 ALPM_DEBUG(printf("alpm: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
569 alsmb_smb_readw(device_t dev, u_char slave, char cmd, short *word)
571 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
579 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
580 ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
581 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
582 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
584 if ((error = alsmb_wait(sc)) == SMB_ENOERR) {
585 low = ALPM_SMBINB(sc, SMBHDATA);
586 high = ALPM_SMBINB(sc, SMBHDATB);
588 *word = ((high & 0xff) << 8) | (low & 0xff);
591 ALPM_DEBUG(printf("alpm: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, *word, error));
597 alsmb_smb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
599 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
600 u_char remain, len, i;
601 int error = SMB_ENOERR;
609 len = min(remain, 32);
611 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
613 /* set the cmd and reset the
614 * 32-byte long internal buffer */
615 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
617 ALPM_SMBOUTB(sc, SMBHDATA, len);
619 /* fill the 32-byte internal buffer */
620 for (i=0; i<len; i++) {
621 ALPM_SMBOUTB(sc, SMBHBLOCK, buf[count-remain+i]);
624 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
625 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
627 if ((error = alsmb_wait(sc)) != SMB_ENOERR)
634 ALPM_DEBUG(printf("alpm: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
640 alsmb_smb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf)
642 struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
643 u_char remain, len, i;
644 int error = SMB_ENOERR;
652 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
654 /* set the cmd and reset the
655 * 32-byte long internal buffer */
656 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
658 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
659 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
661 if ((error = alsmb_wait(sc)) != SMB_ENOERR)
664 len = ALPM_SMBINB(sc, SMBHDATA);
666 /* read the 32-byte internal buffer */
667 for (i=0; i<len; i++) {
668 buf[count-remain+i] = ALPM_SMBINB(sc, SMBHBLOCK);
675 ALPM_DEBUG(printf("alpm: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
680 DRIVER_MODULE(alpm, pci, alpm_pci_driver, alpm_devclass, 0, 0);
681 DRIVER_MODULE(alsmb, alpm, alsmb_driver, alsmb_devclass, 0, 0);