kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / powermng / ichsmb / ichsmb_pci.c
1
2 /*
3  * ichsmb_pci.c
4  *
5  * Copyright (c) 2000 Whistle Communications, Inc.
6  * All rights reserved.
7  * 
8  * Subject to the following obligations and disclaimer of warranty, use and
9  * redistribution of this software, in source or object code forms, with or
10  * without modifications are expressly permitted by Whistle Communications;
11  * provided, however, that:
12  * 1. Any and all reproductions of the source or object code must include the
13  *    copyright notice above and the following disclaimer of warranties; and
14  * 2. No rights are granted, in any manner or form, to use Whistle
15  *    Communications, Inc. trademarks, including the mark "WHISTLE
16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17  *    such appears in the above copyright notice or in the software.
18  * 
19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * Author: Archie Cobbs <archie@freebsd.org>
38  *
39  * $FreeBSD: src/sys/dev/ichsmb/ichsmb_pci.c,v 1.1.2.3 2002/10/20 14:57:19 nyan Exp $
40  * $DragonFly: src/sys/dev/powermng/ichsmb/ichsmb_pci.c,v 1.3 2003/08/07 21:17:07 dillon Exp $
41  */
42
43 /*
44  * Support for the SMBus controller logical device which is part of the
45  * Intel 81801AA (ICH) and 81801AB (ICH0) I/O controller hub chips.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
52 #include <sys/syslog.h>
53 #include <sys/bus.h>
54
55 #include <machine/bus.h>
56 #include <sys/rman.h>
57 #include <machine/resource.h>
58
59 #include <bus/pci/pcivar.h>
60 #include <bus/pci/pcireg.h>
61
62 #include <bus/smbus/smbconf.h>
63
64 #include "ichsmb_var.h"
65 #include "ichsmb_reg.h"
66
67 /* PCI unique identifiers */
68 #define ID_82801AA                      0x24138086
69 #define ID_82801AB                      0x24238086
70 #define ID_82801BA                      0x24438086
71 #define ID_82801CA                      0x24838086
72
73 #define PCIS_SERIALBUS_SMBUS_PROGIF     0x00
74
75 /* Internal functions */
76 static int      ichsmb_pci_probe(device_t dev);
77 static int      ichsmb_pci_attach(device_t dev);
78
79 /* Device methods */
80 static device_method_t ichsmb_pci_methods[] = {
81         /* Device interface */
82         DEVMETHOD(device_probe, ichsmb_pci_probe),
83         DEVMETHOD(device_attach, ichsmb_pci_attach),
84
85         /* Bus methods */
86         DEVMETHOD(bus_print_child, bus_generic_print_child),
87
88         /* SMBus methods */
89         DEVMETHOD(smbus_callback, ichsmb_callback),
90         DEVMETHOD(smbus_quick, ichsmb_quick),
91         DEVMETHOD(smbus_sendb, ichsmb_sendb),
92         DEVMETHOD(smbus_recvb, ichsmb_recvb),
93         DEVMETHOD(smbus_writeb, ichsmb_writeb),
94         DEVMETHOD(smbus_writew, ichsmb_writew),
95         DEVMETHOD(smbus_readb, ichsmb_readb),
96         DEVMETHOD(smbus_readw, ichsmb_readw),
97         DEVMETHOD(smbus_pcall, ichsmb_pcall),
98         DEVMETHOD(smbus_bwrite, ichsmb_bwrite),
99         DEVMETHOD(smbus_bread, ichsmb_bread),
100         { 0, 0 }
101 };
102
103 static driver_t ichsmb_pci_driver = {
104         "ichsmb",
105         ichsmb_pci_methods,
106         sizeof(struct ichsmb_softc)
107 };
108
109 static devclass_t ichsmb_pci_devclass;
110
111 DRIVER_MODULE(ichsmb, pci, ichsmb_pci_driver, ichsmb_pci_devclass, 0, 0);
112
113 static int
114 ichsmb_pci_probe(device_t dev)
115 {
116         /* Check PCI identifier */
117         switch (pci_get_devid(dev)) {
118         case ID_82801AA:
119                 device_set_desc(dev, "Intel 82801AA (ICH) SMBus controller");
120                 break;
121         case ID_82801AB:
122                 device_set_desc(dev, "Intel 82801AB (ICH0) SMBus controller");
123                 break;
124         case ID_82801BA:
125                 device_set_desc(dev, "Intel 82801BA (ICH2) SMBus controller");
126                 break;
127         case ID_82801CA:
128                 device_set_desc(dev, "Intel 82801CA (ICH3) SMBus controller");
129                 break;
130         default:
131                 if (pci_get_class(dev) == PCIC_SERIALBUS
132                     && pci_get_subclass(dev) == PCIS_SERIALBUS_SMBUS
133                     && pci_get_progif(dev) == PCIS_SERIALBUS_SMBUS_PROGIF) {
134                         device_set_desc(dev, "SMBus controller");
135                         return (-2);            /* XXX */
136                 }
137                 return (ENXIO);
138         }
139
140         /* Done */
141         return (ichsmb_probe(dev));
142 }
143
144 static int
145 ichsmb_pci_attach(device_t dev)
146 {
147         const sc_p sc = device_get_softc(dev);
148         u_int32_t cmd;
149         int error;
150
151         /* Initialize private state */
152         bzero(sc, sizeof(*sc));
153         sc->ich_cmd = -1;
154         sc->dev = dev;
155
156         /* Allocate an I/O range */
157         sc->io_rid = ICH_SMB_BASE;
158         sc->io_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
159             &sc->io_rid, 0, ~0, 16, RF_ACTIVE);
160         if (sc->io_res == NULL) {
161                 log(LOG_ERR, "%s: can't map I/O\n", device_get_nameunit(dev));
162                 error = ENXIO;
163                 goto fail;
164         }
165         sc->io_bst = rman_get_bustag(sc->io_res);
166         sc->io_bsh = rman_get_bushandle(sc->io_res);
167
168         /* Allocate interrupt */
169         sc->irq_rid = 0;
170         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ,
171             &sc->irq_rid, 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
172         if (sc->irq_res == NULL) {
173                 log(LOG_ERR, "%s: can't get IRQ\n", device_get_nameunit(dev));
174                 error = ENXIO;
175                 goto fail;
176         }
177
178         /* Set up interrupt handler */
179         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC,
180             ichsmb_device_intr, sc, &sc->irq_handle);
181         if (error != 0) {
182                 log(LOG_ERR, "%s: can't setup irq\n", device_get_nameunit(dev));
183                 goto fail;
184         }
185
186         /* Enable I/O mapping */
187         cmd = pci_read_config(dev, PCIR_COMMAND, 4);
188         cmd |= PCIM_CMD_PORTEN;
189         pci_write_config(dev, PCIR_COMMAND, cmd, 4);
190         cmd = pci_read_config(dev, PCIR_COMMAND, 4);
191         if ((cmd & PCIM_CMD_PORTEN) == 0) {
192                 log(LOG_ERR, "%s: can't enable memory map\n",
193                     device_get_nameunit(dev));
194                 error = ENXIO;
195                 goto fail;
196         }
197
198         /* Enable device */
199         pci_write_config(dev, ICH_HOSTC, ICH_HOSTC_HST_EN, 1);
200
201         /* Done */
202         return (ichsmb_attach(dev));
203
204 fail:
205         /* Attach failed, release resources */
206         ichsmb_release_resources(sc);
207         return (error);
208 }
209