xref: /freebsd/sys/dev/pcf/pcf_isa.c (revision c98323078dede7579020518ec84cdcb478e5c142)
1 /*-
2  * Copyright (c) 2004 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  * Hardware driver for a Philips PCF8584 I2C bus controller sitting
35  * on a generic ISA bus.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/resource.h>
44 
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 
48 #include <sys/rman.h>
49 
50 #include <isa/isareg.h>
51 #include <isa/isavar.h>
52 
53 #include <dev/iicbus/iiconf.h>
54 #include <dev/pcf/pcfvar.h>
55 #include "iicbus_if.h"
56 
57 #define	PCF_NAME	"pcf"
58 
59 static void pcf_isa_identify(driver_t *, device_t);
60 static int pcf_isa_probe(device_t);
61 static int pcf_isa_attach(device_t);
62 static int pcf_isa_detach(device_t);
63 
64 static device_method_t pcf_isa_methods[] = {
65 	/* device interface */
66 	DEVMETHOD(device_identify,	pcf_isa_identify),
67 	DEVMETHOD(device_probe,		pcf_isa_probe),
68 	DEVMETHOD(device_attach,	pcf_isa_attach),
69 	DEVMETHOD(device_detach,	pcf_isa_detach),
70 
71 	/* iicbus interface */
72 	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
73 	DEVMETHOD(iicbus_repeated_start, pcf_repeated_start),
74 	DEVMETHOD(iicbus_start,		pcf_start),
75 	DEVMETHOD(iicbus_stop,		pcf_stop),
76 	DEVMETHOD(iicbus_write,		pcf_write),
77 	DEVMETHOD(iicbus_read,		pcf_read),
78 	DEVMETHOD(iicbus_reset,		pcf_rst_card),
79 	{ 0, 0 }
80 };
81 
82 static devclass_t pcf_isa_devclass;
83 
84 static driver_t pcf_isa_driver = {
85 	PCF_NAME,
86 	pcf_isa_methods,
87 	sizeof(struct pcf_softc),
88 };
89 
90 static void
91 pcf_isa_identify(driver_t *driver, device_t parent)
92 {
93 	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, PCF_NAME, 0);
94 
95 	return;
96 }
97 
98 static int
99 pcf_isa_probe(device_t dev)
100 {
101 	u_long		start, count;
102 	u_int		rid = 0, port, error;
103 
104 	bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count);
105 
106 	/* The port address must be explicitly specified */
107 	if ((error = resource_int_value(PCF_NAME, 0, "port", &port) != 0))
108 		return (error);
109 
110 	/* Probe is only successfull for the specified base io */
111 	if (port != (u_int)start)
112 		return (ENXIO);
113 
114 	device_set_desc(dev, "PCF8584 I2C bus controller");
115 
116 	return (0);
117 }
118 
119 static int
120 pcf_isa_attach(device_t dev)
121 {
122 	struct pcf_softc *sc;
123 	int rv = ENXIO;
124 
125 	sc = DEVTOSOFTC(dev);
126 	bzero(sc, sizeof(struct pcf_softc));
127 
128 	/* IO port is mandatory */
129 	sc->res_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
130 						&sc->rid_ioport, RF_ACTIVE);
131 	if (sc->res_ioport == 0) {
132 		device_printf(dev, "cannot reserve I/O port range\n");
133 		goto error;
134 	}
135 
136 	sc->pcf_flags = device_get_flags(dev);
137 
138 	if (!(sc->pcf_flags & IIC_POLLED)) {
139 		sc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->rid_irq,
140 						     RF_ACTIVE);
141 		if (sc->res_irq == 0) {
142 			device_printf(dev, "can't reserve irq, polled mode.\n");
143 			sc->pcf_flags |= IIC_POLLED;
144 		}
145 	}
146 
147 	/* reset the chip */
148 	pcf_rst_card(dev, IIC_FASTEST, PCF_DEFAULT_ADDR, NULL);
149 
150 	if (sc->res_irq) {
151 		rv = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->res_irq,
152 				    INTR_TYPE_NET /* | INTR_ENTROPY */,
153 				    pcf_intr, sc, &sc->intr_cookie);
154 		if (rv) {
155 			device_printf(dev, "could not setup IRQ\n");
156 			goto error;
157 		}
158 	}
159 
160 	if ((sc->iicbus = device_add_child(dev, "iicbus", -1)) == NULL)
161 		device_printf(dev, "could not allocate iicbus instance\n");
162 
163 	/* probe and attach the iicbus */
164 	bus_generic_attach(dev);
165 
166 	return (0);
167 
168 error:
169 	if (sc->res_irq != 0) {
170 		bus_deactivate_resource(dev, SYS_RES_IRQ, sc->rid_irq,
171 					sc->res_irq);
172 		bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq,
173 				     sc->res_irq);
174 	}
175 	if (sc->res_ioport != 0) {
176 		bus_deactivate_resource(dev, SYS_RES_IOPORT, sc->rid_ioport,
177 					sc->res_ioport);
178 		bus_release_resource(dev, SYS_RES_IOPORT, sc->rid_ioport,
179 				     sc->res_ioport);
180 	}
181 	return (rv);
182 }
183 
184 static int
185 pcf_isa_detach(device_t dev)
186 {
187 	struct pcf_softc *sc;
188 	int rv;
189 
190 	sc = DEVTOSOFTC(dev);
191 
192 	if ((rv = bus_generic_detach(dev)) != 0)
193 		return (rv);
194 
195 	if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
196 		return (rv);
197 
198 	if (sc->res_irq != 0) {
199 		BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->res_irq,
200 				  sc->intr_cookie);
201 		bus_deactivate_resource(dev, SYS_RES_IRQ, sc->rid_irq, sc->res_irq);
202 		bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq, sc->res_irq);
203 	}
204 
205 	bus_deactivate_resource(dev, SYS_RES_IOPORT, sc->rid_ioport, sc->res_ioport);
206 	bus_release_resource(dev, SYS_RES_IOPORT, sc->rid_ioport, sc->res_ioport);
207 
208 	return (0);
209 }
210 
211 DRIVER_MODULE(pcf_isa, isa, pcf_isa_driver, pcf_isa_devclass, 0, 0);
212 MODULE_DEPEND(pcf_isa, iicbus, PCF_MINVER, PCF_PREFVER, PCF_MAXVER);
213 MODULE_VERSION(pcf_isa, PCF_MODVER);
214