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