]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/spibus/spibus.c
MFV r362254: file 5.39.
[FreeBSD/FreeBSD.git] / sys / dev / spibus / spibus.c
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/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/kernel.h>
35 #include <sys/queue.h>
36 #include <sys/sysctl.h>
37 #include <sys/types.h>
38
39 #include <sys/bus.h>
40 #include <machine/bus.h>
41 #include <sys/rman.h>
42 #include <machine/resource.h>
43
44 #include <dev/spibus/spibusvar.h>
45 #include <dev/spibus/spi.h>
46 #include "spibus_if.h"
47
48 static int
49 spibus_probe(device_t dev)
50 {
51
52         device_set_desc(dev, "SPI bus");
53         return (BUS_PROBE_DEFAULT);
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;
74
75         if ((err = bus_generic_detach(dev)) != 0)
76                 return (err);
77         device_delete_children(dev);
78
79         return (0);
80 }
81
82 static int
83 spibus_suspend(device_t dev)
84 {
85         return (bus_generic_suspend(dev));
86 }
87
88 static
89 int
90 spibus_resume(device_t dev)
91 {
92         return (bus_generic_resume(dev));
93 }
94
95 static int
96 spibus_print_child(device_t dev, device_t child)
97 {
98         struct spibus_ivar *devi = SPIBUS_IVAR(child);
99         int retval = 0;
100
101         retval += bus_print_child_header(dev, child);
102         retval += printf(" at cs %d", devi->cs);
103         retval += printf(" mode %d", devi->mode);
104         retval += bus_print_child_footer(dev, child);
105
106         return (retval);
107 }
108
109 static void
110 spibus_probe_nomatch(device_t bus, device_t child)
111 {
112         struct spibus_ivar *devi = SPIBUS_IVAR(child);
113
114         device_printf(bus, "<unknown card> at cs %d mode %d\n", devi->cs,
115             devi->mode);
116         return;
117 }
118
119 static int
120 spibus_child_location_str(device_t bus, device_t child, char *buf,
121     size_t buflen)
122 {
123         struct spibus_ivar *devi = SPIBUS_IVAR(child);
124         int cs;
125
126         cs = devi->cs & ~SPIBUS_CS_HIGH; /* trim 'cs high' bit */
127         snprintf(buf, buflen, "bus=%d cs=%d", device_get_unit(bus), cs);
128         return (0);
129 }
130
131 static int
132 spibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
133     size_t buflen)
134 {
135         *buf = '\0';
136         return (0);
137 }
138
139 static int
140 spibus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
141 {
142         struct spibus_ivar *devi = SPIBUS_IVAR(child);
143
144         switch (which) {
145         default:
146                 return (EINVAL);
147         case SPIBUS_IVAR_CS:
148                 *(uint32_t *)result = devi->cs;
149                 break;
150         case SPIBUS_IVAR_MODE:
151                 *(uint32_t *)result = devi->mode;
152                 break;
153         case SPIBUS_IVAR_CLOCK:
154                 *(uint32_t *)result = devi->clock;
155                 break;
156         }
157         return (0);
158 }
159
160 static int
161 spibus_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
162 {
163         struct spibus_ivar *devi = SPIBUS_IVAR(child);
164
165         if (devi == NULL || device_get_parent(child) != bus)
166                 return (EDOOFUS);
167
168         switch (which) {
169         case SPIBUS_IVAR_CLOCK:
170                 /* Any non-zero value is allowed for max clock frequency. */
171                 if (value == 0)
172                         return (EINVAL);
173                 devi->clock = (uint32_t)value;
174                 break;
175         case SPIBUS_IVAR_CS:
176                  /* Chip select cannot be changed. */
177                 return (EINVAL);
178         case SPIBUS_IVAR_MODE:
179                 /* Valid SPI modes are 0-3. */
180                 if (value > 3)
181                         return (EINVAL);
182                 devi->mode = (uint32_t)value;
183                 break;
184         default:
185                 return (EINVAL);
186         }
187
188         return (0);
189 }
190
191 static device_t
192 spibus_add_child(device_t dev, u_int order, const char *name, int unit)
193 {
194         device_t child;
195         struct spibus_ivar *devi;
196
197         child = device_add_child_ordered(dev, order, name, unit);
198         if (child == NULL) 
199                 return (child);
200         devi = malloc(sizeof(struct spibus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
201         if (devi == NULL) {
202                 device_delete_child(dev, child);
203                 return (0);
204         }
205         device_set_ivars(child, devi);
206         return (child);
207 }
208
209 static void
210 spibus_hinted_child(device_t bus, const char *dname, int dunit)
211 {
212         device_t child;
213         struct spibus_ivar *devi;
214
215         child = BUS_ADD_CHILD(bus, 0, dname, dunit);
216         devi = SPIBUS_IVAR(child);
217         devi->mode = SPIBUS_MODE_NONE;
218         resource_int_value(dname, dunit, "clock", &devi->clock);
219         resource_int_value(dname, dunit, "cs", &devi->cs);
220         resource_int_value(dname, dunit, "mode", &devi->mode);
221 }
222
223 static int
224 spibus_transfer_impl(device_t dev, device_t child, struct spi_command *cmd)
225 {
226         return (SPIBUS_TRANSFER(device_get_parent(dev), child, cmd));
227 }
228
229 static device_method_t spibus_methods[] = {
230         /* Device interface */
231         DEVMETHOD(device_probe,         spibus_probe),
232         DEVMETHOD(device_attach,        spibus_attach),
233         DEVMETHOD(device_detach,        spibus_detach),
234         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
235         DEVMETHOD(device_suspend,       spibus_suspend),
236         DEVMETHOD(device_resume,        spibus_resume),
237
238         /* Bus interface */
239         DEVMETHOD(bus_add_child,        spibus_add_child),
240         DEVMETHOD(bus_print_child,      spibus_print_child),
241         DEVMETHOD(bus_probe_nomatch,    spibus_probe_nomatch),
242         DEVMETHOD(bus_read_ivar,        spibus_read_ivar),
243         DEVMETHOD(bus_write_ivar,       spibus_write_ivar),
244         DEVMETHOD(bus_child_pnpinfo_str, spibus_child_pnpinfo_str),
245         DEVMETHOD(bus_child_location_str, spibus_child_location_str),
246         DEVMETHOD(bus_hinted_child,     spibus_hinted_child),
247
248         /* spibus interface */
249         DEVMETHOD(spibus_transfer,      spibus_transfer_impl),
250
251         DEVMETHOD_END
252 };
253
254 driver_t spibus_driver = {
255         "spibus",
256         spibus_methods,
257         sizeof(struct spibus_softc)
258 };
259
260 devclass_t      spibus_devclass;
261
262 DRIVER_MODULE(spibus, spi, spibus_driver, spibus_devclass, 0, 0);
263 MODULE_VERSION(spibus, 1);