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