pcf_isa.c revision 129704
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: head/sys/dev/pcf/pcf_isa.c 129704 2004-05-25 07:42:45Z joerg $");
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/resource.h>
43
44#include <machine/bus.h>
45#include <machine/resource.h>
46
47#include <sys/rman.h>
48
49#include <isa/isareg.h>
50#include <isa/isavar.h>
51
52#include <dev/iicbus/iiconf.h>
53#include <dev/pcf/pcfvar.h>
54#include "iicbus_if.h"
55
56static int pcf_probe(device_t);
57static int pcf_attach(device_t);
58static int pcf_detach(device_t);
59
60static device_method_t pcf_methods[] = {
61	/* device interface */
62	DEVMETHOD(device_probe,		pcf_probe),
63	DEVMETHOD(device_attach,	pcf_attach),
64	DEVMETHOD(device_detach,	pcf_detach),
65
66	/* iicbus interface */
67	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
68	DEVMETHOD(iicbus_repeated_start, pcf_repeated_start),
69	DEVMETHOD(iicbus_start,		pcf_start),
70	DEVMETHOD(iicbus_stop,		pcf_stop),
71	DEVMETHOD(iicbus_write,		pcf_write),
72	DEVMETHOD(iicbus_read,		pcf_read),
73	DEVMETHOD(iicbus_reset,		pcf_rst_card),
74	{ 0, 0 }
75};
76
77static devclass_t pcf_devclass;
78
79static driver_t pcf_driver = {
80	"pcf",
81	pcf_methods,
82	sizeof(struct pcf_softc),
83};
84
85static int
86pcf_probe(device_t dev)
87{
88
89	device_set_desc(dev, "PCF8584 I2C bus controller");
90	return (0);
91}
92
93static int
94pcf_attach(device_t dev)
95{
96	struct pcf_softc *sc;
97	int rv = ENXIO;
98
99	sc = DEVTOSOFTC(dev);
100	bzero(sc, sizeof(struct pcf_softc));
101
102	/* IO port is mandatory */
103	sc->res_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
104						&sc->rid_ioport, RF_ACTIVE);
105	if (sc->res_ioport == 0) {
106		device_printf(dev, "cannot reserve I/O port range\n");
107		goto error;
108	}
109
110	sc->pcf_flags = device_get_flags(dev);
111
112	if (!(sc->pcf_flags & IIC_POLLED)) {
113		sc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->rid_irq,
114						     RF_ACTIVE);
115		if (sc->res_irq == 0) {
116			device_printf(dev, "can't reserve irq, polled mode.\n");
117			sc->pcf_flags |= IIC_POLLED;
118		}
119	}
120
121	/* reset the chip */
122	pcf_rst_card(dev, IIC_FASTEST, PCF_DEFAULT_ADDR, NULL);
123
124	rv = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->res_irq,
125			    INTR_TYPE_NET /* | INTR_ENTROPY */,
126			    pcf_intr, sc, &sc->intr_cookie);
127	if (rv) {
128		device_printf(dev, "could not setup IRQ\n");
129		goto error;
130	}
131
132	if ((sc->iicbus = device_add_child(dev, "iicbus", -1)) == NULL)
133		device_printf(dev, "could not allocate iicbus instance\n");
134
135	/* probe and attach the iicbus */
136	bus_generic_attach(dev);
137
138	return (0);
139
140error:
141	if (sc->res_irq != 0) {
142		bus_deactivate_resource(dev, SYS_RES_IRQ, sc->rid_irq,
143					sc->res_irq);
144		bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq,
145				     sc->res_irq);
146	}
147	if (sc->res_ioport != 0) {
148		bus_deactivate_resource(dev, SYS_RES_IOPORT, sc->rid_ioport,
149					sc->res_ioport);
150		bus_release_resource(dev, SYS_RES_IOPORT, sc->rid_ioport,
151				     sc->res_ioport);
152	}
153	return (rv);
154}
155
156static int
157pcf_detach(device_t dev)
158{
159	struct pcf_softc *sc;
160	int rv;
161
162	sc = DEVTOSOFTC(dev);
163
164	if ((rv = bus_generic_detach(dev)) != 0)
165		return (rv);
166
167	if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
168		return (rv);
169
170	if (sc->res_irq != 0) {
171		BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->res_irq,
172				  sc->intr_cookie);
173		bus_deactivate_resource(dev, SYS_RES_IRQ, sc->rid_irq, sc->res_irq);
174		bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq, sc->res_irq);
175	}
176
177	bus_deactivate_resource(dev, SYS_RES_IOPORT, sc->rid_ioport, sc->res_ioport);
178	bus_release_resource(dev, SYS_RES_IOPORT, sc->rid_ioport, sc->res_ioport);
179
180	return (0);
181}
182
183DRIVER_MODULE(pcf, ebus, pcf_driver, pcf_devclass, 0, 0);
184MODULE_DEPEND(pcf, iicbus, PCF_MINVER, PCF_PREFVER, PCF_MAXVER);
185MODULE_VERSION(pcf, PCF_MODVER);
186