]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/pcf/pcf_ebus.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / pcf / pcf_ebus.c
1 /*-
2  * Copyright (c) 2004 Marius Strobl, Joerg Wunsch
3  *
4  * derived from sys/i386/isa/pcf.c which is:
5  *
6  * Copyright (c) 1998 Nicolas Souchu, Marc Bouget
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 /*
34  * Device specific driver for the EBus i2c devices found on some sun4u
35  * systems. On systems not having a boot-bus controller the i2c devices
36  * are PCF8584.
37  *
38  * Known onboard slave devices on the primary bus are:
39  *
40  * AXe:
41  *      0x40    PCF8574 I/O     fan status (CPU fans 1+2)
42  *      0x9e    PCF8591 A/D     temperature (CPU + hotspot)
43  *
44  * AXmp:
45  *      0x70    PCF8574 I/O     fan status (fans 1-4)
46  *      0x78    PCF8574 I/O     fan fail interrupt
47  *      0x9a    PCF8591 A/D     voltage (CPU core)
48  *      0x9c    PCF8591 A/D     temperature (hotspots 1+2, aux. analog 1+2)
49  *      0x9e    PCF8591 A/D     temperature (CPUs 1-4)
50  *
51  * CP1400:
52  *      0x70    PCF8574 I/O     reserved for factory use
53  *      0x9e    PCF8591 A/D     temperature (CPU)
54  *
55  * CP1500:
56  *      0x70    PCF8574 I/O     reserved for factory use
57  *      0x72    PCF8574 I/O     geographic address + power supply status lines
58  *      0x9e    PCF8591 A/D     temperature (CPU)
59  *      0xa0    AT24C01A        hostid
60  *
61  * For AXmp, CP1400 and CP1500 these are described in more detail in:
62  * http://www.sun.com/oem/products/manuals/805-7581-04.pdf
63  *
64  */
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/bus.h>
69 #include <sys/kernel.h>
70 #include <sys/module.h>
71 #include <sys/resource.h>
72
73 #include <dev/ofw/ofw_bus.h>
74 #include <dev/ofw/openfirm.h>
75
76 #include <machine/bus.h>
77 #include <machine/resource.h>
78
79 #include <sys/rman.h>
80
81 #include <dev/iicbus/iiconf.h>
82 #include <dev/pcf/pcfvar.h>
83 #include "iicbus_if.h"
84
85 #define PCF_NAME        "pcf"
86
87 static int pcf_ebus_probe(device_t);
88 static int pcf_ebus_attach(device_t);
89 static int pcf_ebus_detach(device_t);
90
91 static device_method_t pcf_ebus_methods[] = {
92         /* device interface */
93         DEVMETHOD(device_probe,         pcf_ebus_probe),
94         DEVMETHOD(device_attach,        pcf_ebus_attach),
95         DEVMETHOD(device_detach,        pcf_ebus_detach),
96
97         /* iicbus interface */
98         DEVMETHOD(iicbus_callback,      iicbus_null_callback),
99         DEVMETHOD(iicbus_repeated_start, pcf_repeated_start),
100         DEVMETHOD(iicbus_start,         pcf_start),
101         DEVMETHOD(iicbus_stop,          pcf_stop),
102         DEVMETHOD(iicbus_write,         pcf_write),
103         DEVMETHOD(iicbus_read,          pcf_read),
104         DEVMETHOD(iicbus_reset,         pcf_rst_card),
105         { 0, 0 }
106 };
107
108 static devclass_t pcf_ebus_devclass;
109
110 static driver_t pcf_ebus_driver = {
111         PCF_NAME,
112         pcf_ebus_methods,
113         sizeof(struct pcf_softc),
114 };
115
116 static int
117 pcf_ebus_probe(device_t dev)
118 {
119         const char *compat;
120
121         /*
122          * We must not attach to this i2c device if this is a system with
123          * a boot-bus controller. Additionally testing the compatibility
124          * property will hopefully take care of this.
125          */
126         if (strcmp("i2c", ofw_bus_get_name(dev)) == 0) {
127                 compat = ofw_bus_get_compat(dev);
128                 if (compat != NULL && strcmp("i2cpcf,8584", compat) == 0) {
129                         device_set_desc(dev, "PCF8584 I2C bus controller");
130                         return (0);
131                 }
132         }
133         return (ENXIO);
134 }
135
136 static int
137 pcf_ebus_attach(device_t dev)
138 {
139         struct pcf_softc *sc;
140         int rv = ENXIO;
141         phandle_t node;
142         uint64_t own_addr;
143
144         sc = DEVTOSOFTC(dev);
145         bzero(sc, sizeof(struct pcf_softc));
146
147         /* get OFW node of the pcf */
148         if ((node = ofw_bus_get_node(dev)) <= 0) {
149                 device_printf(dev, "cannot get OFW node\n");
150                 goto error;
151         }
152
153         /* IO port is mandatory */
154         sc->res_ioport = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
155             &sc->rid_ioport, RF_ACTIVE);
156         if (sc->res_ioport == 0) {
157                 device_printf(dev, "cannot reserve I/O port range\n");
158                 goto error;
159         }
160         sc->bt_ioport = rman_get_bustag(sc->res_ioport);
161         sc->bh_ioport = rman_get_bushandle(sc->res_ioport);
162
163         sc->pcf_flags = device_get_flags(dev);
164
165         /*
166          * XXX use poll-mode property?
167          */
168         if (!(sc->pcf_flags & IIC_POLLED)) {
169                 sc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
170                     &sc->rid_irq, RF_ACTIVE);
171                 if (sc->res_irq == 0) {
172                         device_printf(dev, "can't reserve irq, polled mode.\n");
173                         sc->pcf_flags |= IIC_POLLED;
174                 }
175         }
176
177         /*
178          * XXX on AXmp there's probably a second IRQ which is the fan fail
179          *     interrupt genererated by the PCF8574 at 0x78.
180          */
181
182         /* get address of the pcf */
183         if (OF_getprop(node, "own-address", &own_addr, sizeof(own_addr)) ==
184             -1) {
185                 device_printf(dev, "cannot get own address\n");
186                 goto error;
187         }
188         if (bootverbose)
189                 device_printf(dev, "PCF8584 address: 0x%08llx\n", (unsigned
190                     long long)own_addr);
191
192         /* reset the chip */
193         pcf_rst_card(dev, IIC_FASTEST, own_addr, NULL);
194
195         if (sc->res_irq) {
196                 rv = bus_setup_intr(dev, sc->res_irq,
197                     INTR_TYPE_NET /* | INTR_ENTROPY */, NULL, pcf_intr, sc,
198                     &sc->intr_cookie);
199                 if (rv) {
200                         device_printf(dev, "could not setup IRQ\n");
201                         goto error;
202                 }
203         }
204
205         if ((sc->iicbus = device_add_child(dev, "iicbus", -1)) == NULL)
206                 device_printf(dev, "could not allocate iicbus instance\n");
207
208         /* probe and attach the iicbus */
209         bus_generic_attach(dev);
210
211         return (0);
212
213 error:
214         if (sc->res_irq != 0) {
215                 bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq,
216                     sc->res_irq);
217         }
218         if (sc->res_ioport != 0) {
219                 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid_ioport,
220                     sc->res_ioport);
221         }
222         return (rv);
223 }
224
225 static int
226 pcf_ebus_detach(device_t dev)
227 {
228         struct pcf_softc *sc;
229         int rv;
230
231         sc = DEVTOSOFTC(dev);
232
233         if ((rv = bus_generic_detach(dev)) != 0)
234                 return (rv);
235
236         if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
237                 return (rv);
238
239         if (sc->res_irq != 0) {
240                 bus_teardown_intr(dev, sc->res_irq,
241                     sc->intr_cookie);
242                 bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq,
243                     sc->res_irq);
244         }
245
246         bus_release_resource(dev, SYS_RES_MEMORY, sc->rid_ioport,
247             sc->res_ioport);
248
249         return (0);
250 }
251
252 DRIVER_MODULE(pcf_ebus, ebus, pcf_ebus_driver, pcf_ebus_devclass, 0, 0);
253 MODULE_DEPEND(pcf_ebus, iicbus, PCF_MINVER, PCF_PREFVER, PCF_MAXVER);
254 MODULE_VERSION(pcf_ebus, PCF_MODVER);