xref: /freebsd/sys/dev/spibus/spibus.c (revision 18849b5da0c5eaa88500b457be05b038813b51b1)
1 /*-
2  * Copyright (c) 2006 M. Warner Losh
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 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/kernel.h>
36 #include <sys/queue.h>
37 #include <sys/sysctl.h>
38 #include <sys/types.h>
39 
40 #include <sys/bus.h>
41 #include <machine/bus.h>
42 #include <sys/rman.h>
43 #include <machine/resource.h>
44 
45 #include <dev/spibus/spibusvar.h>
46 #include <dev/spibus/spi.h>
47 #include "spibus_if.h"
48 
49 static int
50 spibus_probe(device_t dev)
51 {
52 	device_set_desc(dev, "spibus bus");
53 	return (BUS_PROBE_GENERIC);
54 }
55 
56 static int
57 spibus_attach(device_t dev)
58 {
59 	struct spibus_softc *sc = SPIBUS_SOFTC(dev);
60 
61 	sc->dev = dev;
62 	bus_enumerate_hinted_children(dev);
63 	return (bus_generic_attach(dev));
64 }
65 
66 /*
67  * Since this is not a self-enumerating bus, and since we always add
68  * children in attach, we have to always delete children here.
69  */
70 static int
71 spibus_detach(device_t dev)
72 {
73 	int err, ndevs, i;
74 	device_t *devlist;
75 
76 	if ((err = bus_generic_detach(dev)) != 0)
77 		return (err);
78 	if ((err = device_get_children(dev, &devlist, &ndevs)) != 0)
79 		return (err);
80 	for (i = 0; i < ndevs; i++)
81 		device_delete_child(dev, devlist[i]);
82 	free(devlist, M_TEMP);
83 
84 	return (0);
85 }
86 
87 static int
88 spibus_suspend(device_t dev)
89 {
90 	return (bus_generic_suspend(dev));
91 }
92 
93 static
94 int
95 spibus_resume(device_t dev)
96 {
97 	return (bus_generic_resume(dev));
98 }
99 
100 static int
101 spibus_print_child(device_t dev, device_t child)
102 {
103 	struct spibus_ivar *devi = SPIBUS_IVAR(child);
104 	int retval = 0;
105 
106 	retval += bus_print_child_header(dev, child);
107 	retval += printf(" at cs %d", devi->cs);
108 	retval += bus_print_child_footer(dev, child);
109 
110 	return (retval);
111 }
112 
113 static void
114 spibus_probe_nomatch(device_t bus, device_t child)
115 {
116 	struct spibus_ivar *devi = SPIBUS_IVAR(child);
117 
118 	device_printf(bus, "<unknown card>");
119 	printf(" at cs %d\n", devi->cs);
120 	return;
121 }
122 
123 static int
124 spibus_child_location_str(device_t bus, device_t child, char *buf,
125     size_t buflen)
126 {
127 	struct spibus_ivar *devi = SPIBUS_IVAR(child);
128 
129 	snprintf(buf, buflen, "cs=%d", devi->cs);
130 	return (0);
131 }
132 
133 static int
134 spibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
135     size_t buflen)
136 {
137 	*buf = '\0';
138 	return (0);
139 }
140 
141 static int
142 spibus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
143 {
144 	struct spibus_ivar *devi = SPIBUS_IVAR(child);
145 
146 	switch (which) {
147 	default:
148 		return (EINVAL);
149 	case SPIBUS_IVAR_CS:
150 		*(uint32_t *)result = devi->cs;
151 		break;
152 	}
153 	return (0);
154 }
155 
156 static device_t
157 spibus_add_child(device_t dev, u_int order, const char *name, int unit)
158 {
159 	device_t child;
160 	struct spibus_ivar *devi;
161 
162 	child = device_add_child_ordered(dev, order, name, unit);
163 	if (child == NULL)
164 		return (child);
165 	devi = malloc(sizeof(struct spibus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
166 	if (devi == NULL) {
167 		device_delete_child(dev, child);
168 		return (0);
169 	}
170 	device_set_ivars(child, devi);
171 	return (child);
172 }
173 
174 static void
175 spibus_hinted_child(device_t bus, const char *dname, int dunit)
176 {
177 	device_t child;
178 	struct spibus_ivar *devi;
179 
180 	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
181 	devi = SPIBUS_IVAR(child);
182 	resource_int_value(dname, dunit, "cs", &devi->cs);
183 }
184 
185 static int
186 spibus_transfer_impl(device_t dev, device_t child, struct spi_command *cmd)
187 {
188 	return (SPIBUS_TRANSFER(device_get_parent(dev), child, cmd));
189 }
190 
191 static device_method_t spibus_methods[] = {
192 	/* Device interface */
193 	DEVMETHOD(device_probe,		spibus_probe),
194 	DEVMETHOD(device_attach,	spibus_attach),
195 	DEVMETHOD(device_detach,	spibus_detach),
196 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
197 	DEVMETHOD(device_suspend,	spibus_suspend),
198 	DEVMETHOD(device_resume,	spibus_resume),
199 
200 	/* Bus interface */
201 	DEVMETHOD(bus_add_child,	spibus_add_child),
202 	DEVMETHOD(bus_print_child,	spibus_print_child),
203 	DEVMETHOD(bus_probe_nomatch,	spibus_probe_nomatch),
204 	DEVMETHOD(bus_read_ivar,	spibus_read_ivar),
205 	DEVMETHOD(bus_child_pnpinfo_str, spibus_child_pnpinfo_str),
206 	DEVMETHOD(bus_child_location_str, spibus_child_location_str),
207 	DEVMETHOD(bus_hinted_child,	spibus_hinted_child),
208 
209 	/* spibus interface */
210 	DEVMETHOD(spibus_transfer,	spibus_transfer_impl),
211 
212 	DEVMETHOD_END
213 };
214 
215 driver_t spibus_driver = {
216 	"spibus",
217 	spibus_methods,
218 	sizeof(struct spibus_softc)
219 };
220 
221 devclass_t	spibus_devclass;
222 
223 DRIVER_MODULE(spibus, spi, spibus_driver, spibus_devclass, 0, 0);
224 MODULE_VERSION(spibus, 1);
225