xref: /freebsd/sys/dev/smbus/smbus.c (revision d01498defbe804f66435b44f22da9278acddf082)
1 /*-
2  * Copyright (c) 1998, 2001 Nicolas Souchu
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/bus.h>
38 
39 #include <dev/smbus/smbconf.h>
40 #include <dev/smbus/smbus.h>
41 
42 #include "smbus_if.h"
43 #include "bus_if.h"
44 
45 struct smbus_ivar
46 {
47 	uint8_t	addr;
48 };
49 
50 /*
51  * Autoconfiguration and support routines for System Management bus
52  */
53 static void smbus_probe_device(device_t dev, u_char addr);
54 
55 static int
56 smbus_probe(device_t dev)
57 {
58 
59 	device_set_desc(dev, "System Management Bus");
60 
61 	return (0);
62 }
63 
64 static int
65 smbus_attach(device_t dev)
66 {
67 	struct smbus_softc *sc = device_get_softc(dev);
68 	unsigned char addr;
69 
70 	mtx_init(&sc->lock, device_get_nameunit(dev), "smbus", MTX_DEF);
71 	bus_generic_probe(dev);
72 	for (addr = SMBUS_ADDR_MIN; addr < SMBUS_ADDR_MAX; ++addr) {
73 		smbus_probe_device(dev, addr);
74 	}
75 	bus_enumerate_hinted_children(dev);
76 	bus_generic_attach(dev);
77 
78 	return (0);
79 }
80 
81 static int
82 smbus_detach(device_t dev)
83 {
84 	struct smbus_softc *sc = device_get_softc(dev);
85 	int error;
86 
87 	error = bus_generic_detach(dev);
88 	if (error)
89 		return (error);
90 	device_delete_children(dev);
91 	mtx_destroy(&sc->lock);
92 
93 	return (0);
94 }
95 
96 void
97 smbus_generic_intr(device_t dev, u_char devaddr, char low, char high, int err)
98 {
99 }
100 
101 static void
102 smbus_probe_device(device_t dev, u_char addr)
103 {
104 	device_t child;
105 	int error;
106 	u_char cmd;
107 	u_char buf[2];
108 	struct smbus_ivar *devi;
109 
110 	cmd = 0x01;
111 	error = smbus_trans(dev, addr, cmd,
112 			    SMB_TRANS_NOCNT | SMB_TRANS_NOREPORT,
113 			    NULL, 0, buf, 1, NULL);
114 	if (error == 0) {
115 		if (bootverbose)
116 			device_printf(dev, "Probed address 0x%02x\n", addr);
117 		child = BUS_ADD_CHILD(dev, SMBUS_ORDER_PNP, NULL, -1);
118 		if (child == NULL)
119 			return;
120 		devi = device_get_ivars(child);
121 		devi->addr = addr;
122 	}
123 }
124 
125 static device_t
126 smbus_add_child(device_t dev, u_int order, const char *name, int unit)
127 {
128 	struct smbus_ivar *devi;
129 	device_t child;
130 
131 	child = device_add_child_ordered(dev, order, name, unit);
132 	if (child == NULL)
133 		return (child);
134 	devi = malloc(sizeof(struct smbus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
135 	if (devi == NULL) {
136 		device_delete_child(dev, child);
137 		return (NULL);
138 	}
139 	device_set_ivars(child, devi);
140 	return (child);
141 }
142 
143 static void
144 smbus_hinted_child(device_t bus, const char *dname, int dunit)
145 {
146 	struct smbus_ivar *devi;
147 	device_t child;
148 	int addr;
149 
150 	addr = 0;
151 	resource_int_value(dname, dunit, "addr", &addr);
152 	if (addr > UINT8_MAX) {
153 		device_printf(bus, "ignored incorrect slave address hint 0x%x"
154 		    " for %s%d\n", addr, dname, dunit);
155 		return;
156 	}
157 	child = BUS_ADD_CHILD(bus, SMBUS_ORDER_HINTED, dname, dunit);
158 	if (child == NULL)
159 		return;
160 	devi = device_get_ivars(child);
161 	devi->addr = addr;
162 }
163 
164 
165 static int
166 smbus_child_location_str(device_t parent, device_t child, char *buf,
167     size_t buflen)
168 {
169 	struct smbus_ivar *devi;
170 
171 	devi = device_get_ivars(child);
172 	if (devi->addr != 0)
173 		snprintf(buf, buflen, "addr=0x%x", devi->addr);
174 	else if (buflen)
175 		buf[0] = 0;
176 	return (0);
177 }
178 
179 static int
180 smbus_print_child(device_t parent, device_t child)
181 {
182 	struct smbus_ivar *devi;
183 	int retval;
184 
185 	devi = device_get_ivars(child);
186 	retval = bus_print_child_header(parent, child);
187 	if (devi->addr != 0)
188 		retval += printf(" at addr 0x%x", devi->addr);
189 	retval += bus_print_child_footer(parent, child);
190 
191 	return (retval);
192 }
193 
194 static int
195 smbus_read_ivar(device_t parent, device_t child, int which, uintptr_t *result)
196 {
197 	struct smbus_ivar *devi;
198 
199 	devi = device_get_ivars(child);
200 	switch (which) {
201 	case SMBUS_IVAR_ADDR:
202 		if (devi->addr != 0)
203 			*result = devi->addr;
204 		else
205 			*result = -1;
206 		break;
207 	default:
208 		return (ENOENT);
209 	}
210 	return (0);
211 }
212 
213 static int
214 smbus_write_ivar(device_t parent, device_t child, int which, uintptr_t value)
215 {
216 	struct smbus_ivar *devi;
217 
218 	devi = device_get_ivars(child);
219 	switch (which) {
220 	case SMBUS_IVAR_ADDR:
221 		/* Allow to set but no change the slave address. */
222 		if (devi->addr != 0)
223 			return (EINVAL);
224 		devi->addr = value;
225 		break;
226 	default:
227 		return (ENOENT);
228 	}
229 	return (0);
230 }
231 
232 static void
233 smbus_probe_nomatch(device_t bus, device_t child)
234 {
235 	struct smbus_ivar *devi = device_get_ivars(child);
236 
237 	/*
238 	 * Ignore (self-identified) devices without a slave address set.
239 	 * For example, smb(4).
240 	 */
241 	if (devi->addr != 0)
242 		device_printf(bus, "<unknown device> at addr %#x\n",
243 		    devi->addr);
244 }
245 
246 /*
247  * Device methods
248  */
249 static device_method_t smbus_methods[] = {
250         /* device interface */
251         DEVMETHOD(device_probe,         smbus_probe),
252         DEVMETHOD(device_attach,        smbus_attach),
253         DEVMETHOD(device_detach,        smbus_detach),
254 
255 	/* bus interface */
256 	DEVMETHOD(bus_add_child,	smbus_add_child),
257 	DEVMETHOD(bus_hinted_child,	smbus_hinted_child),
258 	DEVMETHOD(bus_probe_nomatch,	smbus_probe_nomatch),
259 	DEVMETHOD(bus_child_location_str, smbus_child_location_str),
260 	DEVMETHOD(bus_print_child,	smbus_print_child),
261 	DEVMETHOD(bus_read_ivar,	smbus_read_ivar),
262 	DEVMETHOD(bus_write_ivar,	smbus_write_ivar),
263 
264 	DEVMETHOD_END
265 };
266 
267 driver_t smbus_driver = {
268         "smbus",
269         smbus_methods,
270         sizeof(struct smbus_softc),
271 };
272 
273 devclass_t smbus_devclass;
274 
275 MODULE_VERSION(smbus, SMBUS_MODVER);
276