spibus.c revision 298268
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: head/sys/dev/spibus/spibus.c 298268 2016-04-19 14:18:12Z br $");
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
49static int
50spibus_probe(device_t dev)
51{
52	device_set_desc(dev, "spibus bus");
53	return (BUS_PROBE_GENERIC);
54}
55
56static int
57spibus_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 */
70static int
71spibus_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
87static int
88spibus_suspend(device_t dev)
89{
90	return (bus_generic_suspend(dev));
91}
92
93static
94int
95spibus_resume(device_t dev)
96{
97	return (bus_generic_resume(dev));
98}
99
100static int
101spibus_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
113static void
114spibus_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
123static int
124spibus_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
133static int
134spibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
135    size_t buflen)
136{
137	*buf = '\0';
138	return (0);
139}
140
141static int
142spibus_read_ivar(device_t bus, device_t child, int which, u_int *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
156static device_t
157spibus_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
174static void
175spibus_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
185static int
186spibus_transfer_impl(device_t dev, device_t child, struct spi_command *cmd)
187{
188
189	return (SPIBUS_TRANSFER(device_get_parent(dev), child, cmd));
190}
191
192static int
193spibus_chip_select_impl(device_t dev, device_t child)
194{
195
196	return (SPIBUS_CHIP_SELECT(device_get_parent(dev), child));
197}
198
199static int
200spibus_chip_deselect_impl(device_t dev, device_t child)
201{
202
203	return (SPIBUS_CHIP_DESELECT(device_get_parent(dev), child));
204}
205
206static device_method_t spibus_methods[] = {
207	/* Device interface */
208	DEVMETHOD(device_probe,		spibus_probe),
209	DEVMETHOD(device_attach,	spibus_attach),
210	DEVMETHOD(device_detach,	spibus_detach),
211	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
212	DEVMETHOD(device_suspend,	spibus_suspend),
213	DEVMETHOD(device_resume,	spibus_resume),
214
215	/* Bus interface */
216	DEVMETHOD(bus_add_child,	spibus_add_child),
217	DEVMETHOD(bus_print_child,	spibus_print_child),
218	DEVMETHOD(bus_probe_nomatch,	spibus_probe_nomatch),
219	DEVMETHOD(bus_read_ivar,	spibus_read_ivar),
220	DEVMETHOD(bus_child_pnpinfo_str, spibus_child_pnpinfo_str),
221	DEVMETHOD(bus_child_location_str, spibus_child_location_str),
222	DEVMETHOD(bus_hinted_child,	spibus_hinted_child),
223
224	/* spibus interface */
225	DEVMETHOD(spibus_transfer,	spibus_transfer_impl),
226	DEVMETHOD(spibus_chip_select,	spibus_chip_select_impl),
227	DEVMETHOD(spibus_chip_deselect,	spibus_chip_deselect_impl),
228
229	DEVMETHOD_END
230};
231
232driver_t spibus_driver = {
233	"spibus",
234	spibus_methods,
235	sizeof(struct spibus_softc)
236};
237
238devclass_t	spibus_devclass;
239
240DRIVER_MODULE(spibus, spi, spibus_driver, spibus_devclass, 0, 0);
241MODULE_VERSION(spibus, 1);
242