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: releng/10.3/sys/dev/pcf/pcf_ebus.c 233018 2012-03-15 22:53:39Z nwhitehorn $");
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/bus.h>
68#include <sys/lock.h>
69#include <sys/kernel.h>
70#include <sys/module.h>
71#include <sys/mutex.h>
72#include <sys/resource.h>
73#include <sys/systm.h>
74
75#include <dev/ofw/ofw_bus.h>
76#include <dev/ofw/openfirm.h>
77
78#include <machine/bus.h>
79#include <machine/resource.h>
80
81#include <sys/rman.h>
82
83#include <dev/iicbus/iiconf.h>
84#include <dev/pcf/pcfvar.h>
85#include "iicbus_if.h"
86
87#define	PCF_NAME	"pcf"
88
89static int pcf_ebus_probe(device_t);
90static int pcf_ebus_attach(device_t);
91static int pcf_ebus_detach(device_t);
92
93static device_method_t pcf_ebus_methods[] = {
94	/* device interface */
95	DEVMETHOD(device_probe,		pcf_ebus_probe),
96	DEVMETHOD(device_attach,	pcf_ebus_attach),
97	DEVMETHOD(device_detach,	pcf_ebus_detach),
98
99	/* iicbus interface */
100	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
101	DEVMETHOD(iicbus_repeated_start, pcf_repeated_start),
102	DEVMETHOD(iicbus_start,		pcf_start),
103	DEVMETHOD(iicbus_stop,		pcf_stop),
104	DEVMETHOD(iicbus_write,		pcf_write),
105	DEVMETHOD(iicbus_read,		pcf_read),
106	DEVMETHOD(iicbus_reset,		pcf_rst_card),
107	{ 0, 0 }
108};
109
110static devclass_t pcf_ebus_devclass;
111
112static driver_t pcf_ebus_driver = {
113	PCF_NAME,
114	pcf_ebus_methods,
115	sizeof(struct pcf_softc),
116};
117
118static int
119pcf_ebus_probe(device_t dev)
120{
121	const char *compat;
122
123	/*
124	 * We must not attach to this i2c device if this is a system with
125	 * a boot-bus controller. Additionally testing the compatibility
126	 * property will hopefully take care of this.
127	 */
128	if (strcmp("i2c", ofw_bus_get_name(dev)) == 0) {
129		compat = ofw_bus_get_compat(dev);
130		if (compat != NULL && strcmp("i2cpcf,8584", compat) == 0) {
131			device_set_desc(dev, "PCF8584 I2C bus controller");
132			return (0);
133		}
134	}
135	return (ENXIO);
136}
137
138static int
139pcf_ebus_attach(device_t dev)
140{
141	struct pcf_softc *sc;
142	int rv = ENXIO;
143	phandle_t node;
144	uint64_t own_addr;
145
146	sc = DEVTOSOFTC(dev);
147	mtx_init(&sc->pcf_lock, device_get_nameunit(dev), "pcf", MTX_DEF);
148
149	/* get OFW node of the pcf */
150	if ((node = ofw_bus_get_node(dev)) == -1) {
151		device_printf(dev, "cannot get OFW node\n");
152		goto error;
153	}
154
155	/* IO port is mandatory */
156	sc->res_ioport = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
157	    &sc->rid_ioport, RF_ACTIVE);
158	if (sc->res_ioport == 0) {
159		device_printf(dev, "cannot reserve I/O port range\n");
160		goto error;
161	}
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
213error:
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	mtx_destroy(&sc->pcf_lock);
223	return (rv);
224}
225
226static int
227pcf_ebus_detach(device_t dev)
228{
229	struct pcf_softc *sc;
230	int rv;
231
232	sc = DEVTOSOFTC(dev);
233
234	if ((rv = bus_generic_detach(dev)) != 0)
235		return (rv);
236
237	if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
238		return (rv);
239
240	if (sc->res_irq != 0) {
241		bus_teardown_intr(dev, sc->res_irq,
242		    sc->intr_cookie);
243		bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq,
244		    sc->res_irq);
245	}
246
247	bus_release_resource(dev, SYS_RES_MEMORY, sc->rid_ioport,
248	    sc->res_ioport);
249	mtx_destroy(&sc->pcf_lock);
250
251	return (0);
252}
253
254DRIVER_MODULE(pcf_ebus, ebus, pcf_ebus_driver, pcf_ebus_devclass, 0, 0);
255