1 /*-
2 * Copyright (c) 2006 M. Warner Losh <imp@FreeBSD.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 */
26
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/malloc.h>
30 #include <sys/module.h>
31 #include <sys/kernel.h>
32 #include <sys/queue.h>
33 #include <sys/sbuf.h>
34 #include <sys/sysctl.h>
35
36 #include <sys/bus.h>
37 #include <machine/bus.h>
38 #include <sys/rman.h>
39 #include <machine/resource.h>
40
41 #include <dev/spibus/spibusvar.h>
42 #include <dev/spibus/spi.h>
43 #include "spibus_if.h"
44
45 static int
spibus_probe(device_t dev)46 spibus_probe(device_t dev)
47 {
48
49 device_set_desc(dev, "SPI bus");
50 return (BUS_PROBE_DEFAULT);
51 }
52
53 int
spibus_attach(device_t dev)54 spibus_attach(device_t dev)
55 {
56 struct spibus_softc *sc = SPIBUS_SOFTC(dev);
57
58 sc->dev = dev;
59 bus_enumerate_hinted_children(dev);
60 bus_attach_children(dev);
61 return (0);
62 }
63
64 /*
65 * Since this is not a self-enumerating bus, and since we always add
66 * children in attach, we have to always delete children here.
67 */
68 int
spibus_detach(device_t dev)69 spibus_detach(device_t dev)
70 {
71 return (device_delete_children(dev));
72 }
73
74 static int
spibus_suspend(device_t dev)75 spibus_suspend(device_t dev)
76 {
77 return (bus_generic_suspend(dev));
78 }
79
80 static
81 int
spibus_resume(device_t dev)82 spibus_resume(device_t dev)
83 {
84 return (bus_generic_resume(dev));
85 }
86
87 static int
spibus_print_child(device_t dev,device_t child)88 spibus_print_child(device_t dev, device_t child)
89 {
90 struct spibus_ivar *devi = SPIBUS_IVAR(child);
91 int retval = 0;
92
93 retval += bus_print_child_header(dev, child);
94 retval += printf(" at cs %d", devi->cs);
95 retval += printf(" mode %d", devi->mode);
96 retval += resource_list_print_type(&devi->rl, "irq",
97 SYS_RES_IRQ, "%jd");
98 retval += bus_print_child_footer(dev, child);
99
100 return (retval);
101 }
102
103 void
spibus_probe_nomatch(device_t bus,device_t child)104 spibus_probe_nomatch(device_t bus, device_t child)
105 {
106 struct spibus_ivar *devi = SPIBUS_IVAR(child);
107
108 device_printf(bus, "<unknown card> at cs %d mode %d\n", devi->cs,
109 devi->mode);
110 return;
111 }
112
113 int
spibus_child_location(device_t bus,device_t child,struct sbuf * sb)114 spibus_child_location(device_t bus, device_t child, struct sbuf *sb)
115 {
116 struct spibus_ivar *devi = SPIBUS_IVAR(child);
117 int cs;
118
119 cs = devi->cs & ~SPIBUS_CS_HIGH; /* trim 'cs high' bit */
120 sbuf_printf(sb, "bus=%d cs=%d", device_get_unit(bus), cs);
121 return (0);
122 }
123
124 int
spibus_read_ivar(device_t bus,device_t child,int which,uintptr_t * result)125 spibus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
126 {
127 struct spibus_ivar *devi = SPIBUS_IVAR(child);
128
129 switch (which) {
130 default:
131 return (EINVAL);
132 case SPIBUS_IVAR_CS:
133 *(uint32_t *)result = devi->cs;
134 break;
135 case SPIBUS_IVAR_MODE:
136 *(uint32_t *)result = devi->mode;
137 break;
138 case SPIBUS_IVAR_CLOCK:
139 *(uint32_t *)result = devi->clock;
140 break;
141 case SPIBUS_IVAR_CS_DELAY:
142 *(uint32_t *)result = devi->cs_delay;
143 break;
144 }
145 return (0);
146 }
147
148 int
spibus_write_ivar(device_t bus,device_t child,int which,uintptr_t value)149 spibus_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
150 {
151 struct spibus_ivar *devi = SPIBUS_IVAR(child);
152
153 if (devi == NULL || device_get_parent(child) != bus)
154 return (EDOOFUS);
155
156 switch (which) {
157 case SPIBUS_IVAR_CLOCK:
158 /* Any non-zero value is allowed for max clock frequency. */
159 if (value == 0)
160 return (EINVAL);
161 devi->clock = (uint32_t)value;
162 break;
163 case SPIBUS_IVAR_CS:
164 /* Chip select cannot be changed. */
165 return (EINVAL);
166 case SPIBUS_IVAR_MODE:
167 /* Valid SPI modes are 0-3. */
168 if (value > 3)
169 return (EINVAL);
170 devi->mode = (uint32_t)value;
171 break;
172 case SPIBUS_IVAR_CS_DELAY:
173 devi->cs_delay = (uint32_t)value;
174 break;
175 default:
176 return (EINVAL);
177 }
178
179 return (0);
180 }
181
182 device_t
spibus_add_child_common(device_t dev,u_int order,const char * name,int unit,size_t ivars_size)183 spibus_add_child_common(device_t dev, u_int order, const char *name, int unit,
184 size_t ivars_size)
185 {
186 device_t child;
187 struct spibus_ivar *devi;
188
189 child = device_add_child_ordered(dev, order, name, unit);
190 if (child == NULL)
191 return (child);
192 devi = malloc(ivars_size, M_DEVBUF, M_NOWAIT | M_ZERO);
193 if (devi == NULL) {
194 device_delete_child(dev, child);
195 return (0);
196 }
197 resource_list_init(&devi->rl);
198 device_set_ivars(child, devi);
199 return (child);
200 }
201
202 void
spibus_child_deleted(device_t dev,device_t child)203 spibus_child_deleted(device_t dev, device_t child)
204 {
205 struct spibus_ivar *devi;
206
207 devi = device_get_ivars(child);
208 if (devi == NULL)
209 return;
210 resource_list_free(&devi->rl);
211 free(devi, M_DEVBUF);
212 }
213
214 static device_t
spibus_add_child(device_t dev,u_int order,const char * name,int unit)215 spibus_add_child(device_t dev, u_int order, const char *name, int unit)
216 {
217 return (spibus_add_child_common(
218 dev, order, name, unit, sizeof(struct spibus_ivar)));
219 }
220
221 static void
spibus_hinted_child(device_t bus,const char * dname,int dunit)222 spibus_hinted_child(device_t bus, const char *dname, int dunit)
223 {
224 device_t child;
225 int irq;
226 struct spibus_ivar *devi;
227
228 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
229 devi = SPIBUS_IVAR(child);
230 devi->mode = SPIBUS_MODE_NONE;
231 resource_int_value(dname, dunit, "clock", &devi->clock);
232 resource_int_value(dname, dunit, "cs", &devi->cs);
233 resource_int_value(dname, dunit, "mode", &devi->mode);
234 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
235 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0)
236 device_printf(bus,
237 "Warning: bus_set_resource() failed\n");
238 }
239 }
240
241 static struct resource_list *
spibus_get_resource_list(device_t bus __unused,device_t child)242 spibus_get_resource_list(device_t bus __unused, device_t child)
243 {
244 struct spibus_ivar *devi;
245
246 devi = SPIBUS_IVAR(child);
247 return (&devi->rl);
248 }
249
250 static int
spibus_transfer_impl(device_t dev,device_t child,struct spi_command * cmd)251 spibus_transfer_impl(device_t dev, device_t child, struct spi_command *cmd)
252 {
253 return (SPIBUS_TRANSFER(device_get_parent(dev), child, cmd));
254 }
255
256 static device_method_t spibus_methods[] = {
257 /* Device interface */
258 DEVMETHOD(device_probe, spibus_probe),
259 DEVMETHOD(device_attach, spibus_attach),
260 DEVMETHOD(device_detach, spibus_detach),
261 DEVMETHOD(device_shutdown, bus_generic_shutdown),
262 DEVMETHOD(device_suspend, spibus_suspend),
263 DEVMETHOD(device_resume, spibus_resume),
264
265 /* Bus interface */
266 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
267 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
268 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
269 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
270 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
271 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
272 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
273 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
274 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
275 DEVMETHOD(bus_get_resource_list, spibus_get_resource_list),
276
277 DEVMETHOD(bus_add_child, spibus_add_child),
278 DEVMETHOD(bus_child_deleted, spibus_child_deleted),
279 DEVMETHOD(bus_print_child, spibus_print_child),
280 DEVMETHOD(bus_probe_nomatch, spibus_probe_nomatch),
281 DEVMETHOD(bus_read_ivar, spibus_read_ivar),
282 DEVMETHOD(bus_write_ivar, spibus_write_ivar),
283 DEVMETHOD(bus_child_location, spibus_child_location),
284 DEVMETHOD(bus_hinted_child, spibus_hinted_child),
285
286 /* spibus interface */
287 DEVMETHOD(spibus_transfer, spibus_transfer_impl),
288
289 DEVMETHOD_END
290 };
291
292 driver_t spibus_driver = {
293 "spibus",
294 spibus_methods,
295 sizeof(struct spibus_softc)
296 };
297
298 DRIVER_MODULE(spibus, spi, spibus_driver, 0, 0);
299 MODULE_VERSION(spibus, 1);
300