xref: /freebsd-12.1/sys/dev/pcf/envctrl.c (revision 718cf2cc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004 Joerg Wunsch
5  *
6  * derived from sys/i386/isa/pcf.c which is:
7  *
8  * Copyright (c) 1998 Nicolas Souchu, Marc Bouget
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 /*
36  * Device specific driver for the SUNW,envctrl device found on some
37  * UltraSPARC Sun systems.  This device is a Philips PCF8584 sitting
38  * on the Ebus2.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/bus.h>
43 #include <sys/conf.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/resource.h>
50 #include <sys/systm.h>
51 #include <sys/uio.h>
52 
53 #include <dev/ofw/ofw_bus.h>
54 
55 #include <machine/bus.h>
56 #include <machine/resource.h>
57 
58 #include <sys/rman.h>
59 
60 #include <dev/iicbus/iicbus.h>
61 #include <dev/iicbus/iiconf.h>
62 #include <dev/pcf/pcfvar.h>
63 #include "iicbus_if.h"
64 
65 #undef PCF_DEFAULT_ADDR
66 #define PCF_DEFAULT_ADDR	0x55 /* SUNW,pcf default */
67 
68 static int envctrl_probe(device_t);
69 static int envctrl_attach(device_t);
70 static int envctrl_detach(device_t);
71 
72 static device_method_t envctrl_methods[] = {
73 	/* device interface */
74 	DEVMETHOD(device_probe,		envctrl_probe),
75 	DEVMETHOD(device_attach,	envctrl_attach),
76 	DEVMETHOD(device_detach,	envctrl_detach),
77 
78 	/* iicbus interface */
79 	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
80 	DEVMETHOD(iicbus_repeated_start, pcf_repeated_start),
81 	DEVMETHOD(iicbus_start,		pcf_start),
82 	DEVMETHOD(iicbus_stop,		pcf_stop),
83 	DEVMETHOD(iicbus_write,		pcf_write),
84 	DEVMETHOD(iicbus_read,		pcf_read),
85 	DEVMETHOD(iicbus_reset,		pcf_rst_card),
86 	{ 0, 0 }
87 };
88 
89 static devclass_t envctrl_devclass;
90 
91 static driver_t envctrl_driver = {
92 	"envctrl",
93 	envctrl_methods,
94 	sizeof(struct pcf_softc),
95 };
96 
97 static int
envctrl_probe(device_t dev)98 envctrl_probe(device_t dev)
99 {
100 
101 	if (strcmp("SUNW,envctrl", ofw_bus_get_name(dev)) == 0) {
102 		device_set_desc(dev, "EBus SUNW,envctrl");
103 		return (0);
104 	}
105 	return (ENXIO);
106 }
107 
108 static int
envctrl_attach(device_t dev)109 envctrl_attach(device_t dev)
110 {
111 	struct pcf_softc *sc;
112 	int rv = ENXIO;
113 
114 	sc = DEVTOSOFTC(dev);
115 	mtx_init(&sc->pcf_lock, device_get_nameunit(dev), "pcf", MTX_DEF);
116 
117 	/* IO port is mandatory */
118 	sc->res_ioport = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
119 						&sc->rid_ioport, RF_ACTIVE);
120 	if (sc->res_ioport == 0) {
121 		device_printf(dev, "cannot reserve I/O port range\n");
122 		goto error;
123 	}
124 
125 	sc->pcf_flags = device_get_flags(dev);
126 
127 	if (!(sc->pcf_flags & IIC_POLLED)) {
128 		sc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->rid_irq,
129 						     RF_ACTIVE);
130 		if (sc->res_irq == 0) {
131 			device_printf(dev, "can't reserve irq, polled mode.\n");
132 			sc->pcf_flags |= IIC_POLLED;
133 		}
134 	}
135 
136 	/* reset the chip */
137 	pcf_rst_card(dev, IIC_FASTEST, PCF_DEFAULT_ADDR, NULL);
138 
139 	rv = bus_setup_intr(dev, sc->res_irq,
140 			    INTR_TYPE_NET | INTR_MPSAFE /* | INTR_ENTROPY */,
141 			    NULL, pcf_intr, sc, &sc->intr_cookie);
142 	if (rv) {
143 		device_printf(dev, "could not setup IRQ\n");
144 		goto error;
145 	}
146 
147 	if ((sc->iicbus = device_add_child(dev, "iicbus", -1)) == NULL)
148 		device_printf(dev, "could not allocate iicbus instance\n");
149 
150 	/* probe and attach the iicbus */
151 	bus_generic_attach(dev);
152 
153 	return (0);
154 
155 error:
156 	if (sc->res_irq != 0) {
157 		bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq,
158 				     sc->res_irq);
159 	}
160 	if (sc->res_ioport != 0) {
161 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid_ioport,
162 				     sc->res_ioport);
163 	}
164 	mtx_destroy(&sc->pcf_lock);
165 	return (rv);
166 }
167 
168 static int
envctrl_detach(device_t dev)169 envctrl_detach(device_t dev)
170 {
171 	struct pcf_softc *sc;
172 	int rv;
173 
174 	sc = DEVTOSOFTC(dev);
175 
176 	if ((rv = bus_generic_detach(dev)) != 0)
177 		return (rv);
178 
179 	if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
180 		return (rv);
181 
182 	if (sc->res_irq != 0) {
183 		bus_teardown_intr(dev, sc->res_irq, sc->intr_cookie);
184 		bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq, sc->res_irq);
185 	}
186 
187 	bus_release_resource(dev, SYS_RES_MEMORY, sc->rid_ioport, sc->res_ioport);
188 	mtx_destroy(&sc->pcf_lock);
189 
190 	return (0);
191 }
192 
193 DRIVER_MODULE(envctrl, ebus, envctrl_driver, envctrl_devclass, 0, 0);
194 DRIVER_MODULE(iicbus, envctrl, iicbus_driver, iicbus_devclass, 0, 0);
195