envctrl.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/envctrl.c 129704 2004-05-25 07:42:45Z joerg $");
32
33/*
34 * Device specific driver for the SUNW,envctrl device found on some
35 * UltraSPARC Sun systems.  This device is a Philips PCF8584 sitting
36 * on the Ebus2.
37 */
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/bus.h>
42#include <sys/conf.h>
43#include <sys/kernel.h>
44#include <sys/malloc.h>
45#include <sys/resource.h>
46#include <sys/uio.h>
47
48#include <machine/bus.h>
49#include <machine/resource.h>
50
51#include <sys/rman.h>
52
53#include <dev/ofw/openfirm.h>
54
55#include <sparc64/ebus/ebusvar.h>
56
57#include <dev/iicbus/iiconf.h>
58#include <dev/pcf/pcfvar.h>
59#include "iicbus_if.h"
60
61#undef PCF_DEFAULT_ADDR
62#define PCF_DEFAULT_ADDR	0x55 /* SUNW,pcf default */
63
64static int envctrl_probe(device_t);
65static int envctrl_attach(device_t);
66static int envctrl_detach(device_t);
67
68static device_method_t envctrl_methods[] = {
69	/* device interface */
70	DEVMETHOD(device_probe,		envctrl_probe),
71	DEVMETHOD(device_attach,	envctrl_attach),
72	DEVMETHOD(device_detach,	envctrl_detach),
73
74	/* iicbus interface */
75	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
76	DEVMETHOD(iicbus_repeated_start, pcf_repeated_start),
77	DEVMETHOD(iicbus_start,		pcf_start),
78	DEVMETHOD(iicbus_stop,		pcf_stop),
79	DEVMETHOD(iicbus_write,		pcf_write),
80	DEVMETHOD(iicbus_read,		pcf_read),
81	DEVMETHOD(iicbus_reset,		pcf_rst_card),
82	{ 0, 0 }
83};
84
85static devclass_t envctrl_devclass;
86
87static driver_t envctrl_driver = {
88	"envctrl",
89	envctrl_methods,
90	sizeof(struct pcf_softc),
91};
92
93static int
94envctrl_probe(device_t dev)
95{
96
97	if (strcmp("SUNW,envctrl", ebus_get_name(dev)) == 0) {
98		device_set_desc(dev, "EBus SUNW,envctrl");
99		return (0);
100	}
101	return (ENXIO);
102}
103
104static int
105envctrl_attach(device_t dev)
106{
107	struct pcf_softc *sc;
108	int rv = ENXIO;
109
110	sc = DEVTOSOFTC(dev);
111	bzero(sc, sizeof(struct pcf_softc));
112
113	/* IO port is mandatory */
114	sc->res_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
115						&sc->rid_ioport, RF_ACTIVE);
116	if (sc->res_ioport == 0) {
117		device_printf(dev, "cannot reserve I/O port range\n");
118		goto error;
119	}
120
121	sc->pcf_flags = device_get_flags(dev);
122
123	if (!(sc->pcf_flags & IIC_POLLED)) {
124		sc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->rid_irq,
125						     RF_ACTIVE);
126		if (sc->res_irq == 0) {
127			device_printf(dev, "can't reserve irq, polled mode.\n");
128			sc->pcf_flags |= IIC_POLLED;
129		}
130	}
131
132	/* reset the chip */
133	pcf_rst_card(dev, IIC_FASTEST, PCF_DEFAULT_ADDR, NULL);
134
135	rv = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->res_irq,
136			    INTR_TYPE_NET /* | INTR_ENTROPY */,
137			    pcf_intr, sc, &sc->intr_cookie);
138	if (rv) {
139		device_printf(dev, "could not setup IRQ\n");
140		goto error;
141	}
142
143	if ((sc->iicbus = device_add_child(dev, "iicbus", -1)) == NULL)
144		device_printf(dev, "could not allocate iicbus instance\n");
145
146	/* probe and attach the iicbus */
147	bus_generic_attach(dev);
148
149	return (0);
150
151error:
152	if (sc->res_irq != 0) {
153		bus_deactivate_resource(dev, SYS_RES_IRQ, sc->rid_irq,
154					sc->res_irq);
155		bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq,
156				     sc->res_irq);
157	}
158	if (sc->res_ioport != 0) {
159		bus_deactivate_resource(dev, SYS_RES_IOPORT, sc->rid_ioport,
160					sc->res_ioport);
161		bus_release_resource(dev, SYS_RES_IOPORT, sc->rid_ioport,
162				     sc->res_ioport);
163	}
164	return (rv);
165}
166
167static int
168envctrl_detach(device_t dev)
169{
170	struct pcf_softc *sc;
171	int rv;
172
173	sc = DEVTOSOFTC(dev);
174
175	if ((rv = bus_generic_detach(dev)) != 0)
176		return (rv);
177
178	if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
179		return (rv);
180
181	if (sc->res_irq != 0) {
182		BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->res_irq,
183				  sc->intr_cookie);
184		bus_deactivate_resource(dev, SYS_RES_IRQ, sc->rid_irq, sc->res_irq);
185		bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq, sc->res_irq);
186	}
187
188	bus_deactivate_resource(dev, SYS_RES_IOPORT, sc->rid_ioport, sc->res_ioport);
189	bus_release_resource(dev, SYS_RES_IOPORT, sc->rid_ioport, sc->res_ioport);
190
191	return (0);
192}
193
194DRIVER_MODULE(pcf, ebus, envctrl_driver, envctrl_devclass, 0, 0);
195MODULE_DEPEND(pcf, iicbus, PCF_MINVER, PCF_PREFVER, PCF_MAXVER);
196MODULE_VERSION(pcf, PCF_MODVER);
197