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: releng/11.1/sys/dev/spibus/spibus.c 315329 2017-03-15 21:01:03Z mizhka $");
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 += printf(" mode %d", devi->mode);
109 retval += bus_print_child_footer(dev, child);
110
111 return (retval);
112 }
113
114 static void
115 spibus_probe_nomatch(device_t bus, device_t child)
116 {
117 struct spibus_ivar *devi = SPIBUS_IVAR(child);
118
119 device_printf(bus, "<unknown card> at cs %d mode %d\n", devi->cs,
120 devi->mode);
121 return;
122 }
123
124 static int
125 spibus_child_location_str(device_t bus, device_t child, char *buf,
126 size_t buflen)
127 {
128 struct spibus_ivar *devi = SPIBUS_IVAR(child);
129
130 snprintf(buf, buflen, "cs=%d", devi->cs);
131 return (0);
132 }
133
134 static int
135 spibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
136 size_t buflen)
137 {
138 *buf = '\0';
139 return (0);
140 }
141
142 static int
143 spibus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
144 {
145 struct spibus_ivar *devi = SPIBUS_IVAR(child);
146
147 switch (which) {
148 default:
149 return (EINVAL);
150 case SPIBUS_IVAR_CS:
151 *(uint32_t *)result = devi->cs;
152 break;
153 case SPIBUS_IVAR_MODE:
154 *(uint32_t *)result = devi->mode;
155 break;
156 case SPIBUS_IVAR_CLOCK:
157 *(uint32_t *)result = devi->clock;
158 break;
159 }
160 return (0);
161 }
162
163 static device_t
164 spibus_add_child(device_t dev, u_int order, const char *name, int unit)
165 {
166 device_t child;
167 struct spibus_ivar *devi;
168
169 child = device_add_child_ordered(dev, order, name, unit);
170 if (child == NULL)
171 return (child);
172 devi = malloc(sizeof(struct spibus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
173 if (devi == NULL) {
174 device_delete_child(dev, child);
175 return (0);
176 }
177 device_set_ivars(child, devi);
178 return (child);
179 }
180
181 static void
182 spibus_hinted_child(device_t bus, const char *dname, int dunit)
183 {
184 device_t child;
185 struct spibus_ivar *devi;
186
187 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
188 devi = SPIBUS_IVAR(child);
189 devi->mode = SPIBUS_MODE_NONE;
190 resource_int_value(dname, dunit, "cs", &devi->cs);
191 resource_int_value(dname, dunit, "mode", &devi->mode);
192 }
193
194 static int
195 spibus_transfer_impl(device_t dev, device_t child, struct spi_command *cmd)
196 {
197 return (SPIBUS_TRANSFER(device_get_parent(dev), child, cmd));
198 }
199
200 static device_method_t spibus_methods[] = {
201 /* Device interface */
202 DEVMETHOD(device_probe, spibus_probe),
203 DEVMETHOD(device_attach, spibus_attach),
204 DEVMETHOD(device_detach, spibus_detach),
205 DEVMETHOD(device_shutdown, bus_generic_shutdown),
206 DEVMETHOD(device_suspend, spibus_suspend),
207 DEVMETHOD(device_resume, spibus_resume),
208
209 /* Bus interface */
210 DEVMETHOD(bus_add_child, spibus_add_child),
211 DEVMETHOD(bus_print_child, spibus_print_child),
212 DEVMETHOD(bus_probe_nomatch, spibus_probe_nomatch),
213 DEVMETHOD(bus_read_ivar, spibus_read_ivar),
214 DEVMETHOD(bus_child_pnpinfo_str, spibus_child_pnpinfo_str),
215 DEVMETHOD(bus_child_location_str, spibus_child_location_str),
216 DEVMETHOD(bus_hinted_child, spibus_hinted_child),
217
218 /* spibus interface */
219 DEVMETHOD(spibus_transfer, spibus_transfer_impl),
220
221 DEVMETHOD_END
222 };
223
224 driver_t spibus_driver = {
225 "spibus",
226 spibus_methods,
227 sizeof(struct spibus_softc)
228 };
229
230 devclass_t spibus_devclass;
231
232 DRIVER_MODULE(spibus, spi, spibus_driver, spibus_devclass, 0, 0);
233 MODULE_VERSION(spibus, 1);
Cache object: 1d19aea16815fcb424e0f04ae17fc2f9
|