xref: /freebsd/sys/dev/spibus/spibus.c (revision 46b39c90f44e166cdad703e5aceab54047cf54f2)
1*46b39c90SWarner Losh /*-
2*46b39c90SWarner Losh  * Copyright (c) 2006 M. Warner Losh
3*46b39c90SWarner Losh  * All rights reserved.
4*46b39c90SWarner Losh  *
5*46b39c90SWarner Losh  * Redistribution and use in source and binary forms, with or without
6*46b39c90SWarner Losh  * modification, are permitted provided that the following conditions
7*46b39c90SWarner Losh  * are met:
8*46b39c90SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9*46b39c90SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10*46b39c90SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11*46b39c90SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12*46b39c90SWarner Losh  *    documentation and/or other materials provided with the distribution.
13*46b39c90SWarner Losh  *
14*46b39c90SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*46b39c90SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*46b39c90SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*46b39c90SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*46b39c90SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*46b39c90SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*46b39c90SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*46b39c90SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*46b39c90SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*46b39c90SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*46b39c90SWarner Losh  * SUCH DAMAGE.
25*46b39c90SWarner Losh  *
26*46b39c90SWarner Losh  */
27*46b39c90SWarner Losh 
289a795dfaSWarner Losh #include <sys/cdefs.h>
299a795dfaSWarner Losh __FBSDID("$FreeBSD$");
309a795dfaSWarner Losh 
319a795dfaSWarner Losh #include <sys/param.h>
329a795dfaSWarner Losh #include <sys/systm.h>
339a795dfaSWarner Losh #include <sys/malloc.h>
349a795dfaSWarner Losh #include <sys/module.h>
359a795dfaSWarner Losh #include <sys/kernel.h>
369a795dfaSWarner Losh #include <sys/queue.h>
379a795dfaSWarner Losh #include <sys/sysctl.h>
389a795dfaSWarner Losh #include <sys/types.h>
399a795dfaSWarner Losh 
409a795dfaSWarner Losh #include <sys/bus.h>
419a795dfaSWarner Losh #include <machine/bus.h>
429a795dfaSWarner Losh #include <sys/rman.h>
439a795dfaSWarner Losh #include <machine/resource.h>
449a795dfaSWarner Losh 
459a795dfaSWarner Losh #include <dev/spibus/spibusvar.h>
469a795dfaSWarner Losh #include <dev/spibus/spi.h>
479a795dfaSWarner Losh #include "spibus_if.h"
489a795dfaSWarner Losh 
499a795dfaSWarner Losh static int
509a795dfaSWarner Losh spibus_probe(device_t dev)
519a795dfaSWarner Losh {
529a795dfaSWarner Losh 	device_set_desc(dev, "spibus bus");
53585300e9SLuiz Otavio O Souza 	return (BUS_PROBE_GENERIC);
549a795dfaSWarner Losh }
559a795dfaSWarner Losh 
569a795dfaSWarner Losh static int
579a795dfaSWarner Losh spibus_attach(device_t dev)
589a795dfaSWarner Losh {
599a795dfaSWarner Losh 	struct spibus_softc *sc = SPIBUS_SOFTC(dev);
609a795dfaSWarner Losh 
619a795dfaSWarner Losh 	sc->dev = dev;
629a795dfaSWarner Losh 	bus_enumerate_hinted_children(dev);
639a795dfaSWarner Losh 	return (bus_generic_attach(dev));
649a795dfaSWarner Losh }
659a795dfaSWarner Losh 
669a795dfaSWarner Losh /*
679a795dfaSWarner Losh  * Since this is not a self-enumerating bus, and since we always add
689a795dfaSWarner Losh  * children in attach, we have to always delete children here.
699a795dfaSWarner Losh  */
709a795dfaSWarner Losh static int
719a795dfaSWarner Losh spibus_detach(device_t dev)
729a795dfaSWarner Losh {
739a795dfaSWarner Losh 	int err, ndevs, i;
749a795dfaSWarner Losh 	device_t *devlist;
759a795dfaSWarner Losh 
769a795dfaSWarner Losh 	if ((err = bus_generic_detach(dev)) != 0)
779a795dfaSWarner Losh 		return (err);
789a795dfaSWarner Losh 	if ((err = device_get_children(dev, &devlist, &ndevs)) != 0)
799a795dfaSWarner Losh 		return (err);
809a795dfaSWarner Losh 	for (i = 0; i < ndevs; i++)
819a795dfaSWarner Losh 		device_delete_child(dev, devlist[i]);
829a795dfaSWarner Losh 	free(devlist, M_TEMP);
839a795dfaSWarner Losh 
849a795dfaSWarner Losh 	return (0);
859a795dfaSWarner Losh }
869a795dfaSWarner Losh 
879a795dfaSWarner Losh static int
889a795dfaSWarner Losh spibus_suspend(device_t dev)
899a795dfaSWarner Losh {
909a795dfaSWarner Losh 	return (bus_generic_suspend(dev));
919a795dfaSWarner Losh }
929a795dfaSWarner Losh 
939a795dfaSWarner Losh static
949a795dfaSWarner Losh int
959a795dfaSWarner Losh spibus_resume(device_t dev)
969a795dfaSWarner Losh {
979a795dfaSWarner Losh 	return (bus_generic_resume(dev));
989a795dfaSWarner Losh }
999a795dfaSWarner Losh 
1009a795dfaSWarner Losh static int
1019a795dfaSWarner Losh spibus_print_child(device_t dev, device_t child)
1029a795dfaSWarner Losh {
1039a795dfaSWarner Losh 	struct spibus_ivar *devi = SPIBUS_IVAR(child);
1049a795dfaSWarner Losh 	int retval = 0;
1059a795dfaSWarner Losh 
1069a795dfaSWarner Losh 	retval += bus_print_child_header(dev, child);
1079a795dfaSWarner Losh 	retval += printf(" at cs %d", devi->cs);
1089a795dfaSWarner Losh 	retval += bus_print_child_footer(dev, child);
1099a795dfaSWarner Losh 
1109a795dfaSWarner Losh 	return (retval);
1119a795dfaSWarner Losh }
1129a795dfaSWarner Losh 
1139a795dfaSWarner Losh static void
1149a795dfaSWarner Losh spibus_probe_nomatch(device_t bus, device_t child)
1159a795dfaSWarner Losh {
1169a795dfaSWarner Losh 	struct spibus_ivar *devi = SPIBUS_IVAR(child);
1179a795dfaSWarner Losh 
1189a795dfaSWarner Losh 	device_printf(bus, "<unknown card>");
1199a795dfaSWarner Losh 	printf(" at cs %d\n", devi->cs);
1209a795dfaSWarner Losh 	return;
1219a795dfaSWarner Losh }
1229a795dfaSWarner Losh 
1239a795dfaSWarner Losh static int
1249a795dfaSWarner Losh spibus_child_location_str(device_t bus, device_t child, char *buf,
1259a795dfaSWarner Losh     size_t buflen)
1269a795dfaSWarner Losh {
1279a795dfaSWarner Losh 	struct spibus_ivar *devi = SPIBUS_IVAR(child);
1289a795dfaSWarner Losh 
1299a795dfaSWarner Losh 	snprintf(buf, buflen, "cs=%d", devi->cs);
1309a795dfaSWarner Losh 	return (0);
1319a795dfaSWarner Losh }
1329a795dfaSWarner Losh 
1339a795dfaSWarner Losh static int
1349a795dfaSWarner Losh spibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
1359a795dfaSWarner Losh     size_t buflen)
1369a795dfaSWarner Losh {
1379a795dfaSWarner Losh 	*buf = '\0';
1389a795dfaSWarner Losh 	return (0);
1399a795dfaSWarner Losh }
1409a795dfaSWarner Losh 
1419a795dfaSWarner Losh static int
1427c6ee94fSAndriy Gapon spibus_read_ivar(device_t bus, device_t child, int which, u_int *result)
1439a795dfaSWarner Losh {
1449a795dfaSWarner Losh 	struct spibus_ivar *devi = SPIBUS_IVAR(child);
1459a795dfaSWarner Losh 
1469a795dfaSWarner Losh 	switch (which) {
1479a795dfaSWarner Losh 	default:
1489a795dfaSWarner Losh 		return (EINVAL);
1499a795dfaSWarner Losh 	case SPIBUS_IVAR_CS:
1509a795dfaSWarner Losh 		*(uint32_t *)result = devi->cs;
1519a795dfaSWarner Losh 		break;
1529a795dfaSWarner Losh 	}
1539a795dfaSWarner Losh 	return (0);
1549a795dfaSWarner Losh }
1559a795dfaSWarner Losh 
1569a795dfaSWarner Losh static device_t
1573d844eddSAndriy Gapon spibus_add_child(device_t dev, u_int order, const char *name, int unit)
1589a795dfaSWarner Losh {
1599a795dfaSWarner Losh 	device_t child;
1609a795dfaSWarner Losh 	struct spibus_ivar *devi;
1619a795dfaSWarner Losh 
1629a795dfaSWarner Losh 	child = device_add_child_ordered(dev, order, name, unit);
1639a795dfaSWarner Losh 	if (child == NULL)
1649a795dfaSWarner Losh 		return (child);
1659a795dfaSWarner Losh 	devi = malloc(sizeof(struct spibus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
16686663fabSWarner Losh 	if (devi == NULL) {
16786663fabSWarner Losh 		device_delete_child(dev, child);
1689a795dfaSWarner Losh 		return (0);
16986663fabSWarner Losh 	}
1709a795dfaSWarner Losh 	device_set_ivars(child, devi);
1719a795dfaSWarner Losh 	return (child);
1729a795dfaSWarner Losh }
1739a795dfaSWarner Losh 
1749a795dfaSWarner Losh static void
1759a795dfaSWarner Losh spibus_hinted_child(device_t bus, const char *dname, int dunit)
1769a795dfaSWarner Losh {
1779a795dfaSWarner Losh 	device_t child;
1789a795dfaSWarner Losh 	struct spibus_ivar *devi;
1799a795dfaSWarner Losh 
1809a795dfaSWarner Losh 	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
1819a795dfaSWarner Losh 	devi = SPIBUS_IVAR(child);
1829a795dfaSWarner Losh 	resource_int_value(dname, dunit, "cs", &devi->cs);
1839a795dfaSWarner Losh }
1849a795dfaSWarner Losh 
1859a795dfaSWarner Losh static int
1869a795dfaSWarner Losh spibus_transfer_impl(device_t dev, device_t child, struct spi_command *cmd)
1879a795dfaSWarner Losh {
18886663fabSWarner Losh 	return (SPIBUS_TRANSFER(device_get_parent(dev), child, cmd));
1899a795dfaSWarner Losh }
1909a795dfaSWarner Losh 
1919a795dfaSWarner Losh static device_method_t spibus_methods[] = {
1929a795dfaSWarner Losh 	/* Device interface */
1939a795dfaSWarner Losh 	DEVMETHOD(device_probe,		spibus_probe),
1949a795dfaSWarner Losh 	DEVMETHOD(device_attach,	spibus_attach),
1959a795dfaSWarner Losh 	DEVMETHOD(device_detach,	spibus_detach),
1969a795dfaSWarner Losh 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1979a795dfaSWarner Losh 	DEVMETHOD(device_suspend,	spibus_suspend),
1989a795dfaSWarner Losh 	DEVMETHOD(device_resume,	spibus_resume),
1999a795dfaSWarner Losh 
2009a795dfaSWarner Losh 	/* Bus interface */
2019a795dfaSWarner Losh 	DEVMETHOD(bus_add_child,	spibus_add_child),
2029a795dfaSWarner Losh 	DEVMETHOD(bus_print_child,	spibus_print_child),
2039a795dfaSWarner Losh 	DEVMETHOD(bus_probe_nomatch,	spibus_probe_nomatch),
2049a795dfaSWarner Losh 	DEVMETHOD(bus_read_ivar,	spibus_read_ivar),
2059a795dfaSWarner Losh 	DEVMETHOD(bus_child_pnpinfo_str, spibus_child_pnpinfo_str),
2069a795dfaSWarner Losh 	DEVMETHOD(bus_child_location_str, spibus_child_location_str),
2079a795dfaSWarner Losh 	DEVMETHOD(bus_hinted_child,	spibus_hinted_child),
2089a795dfaSWarner Losh 
2099a795dfaSWarner Losh 	/* spibus interface */
2109a795dfaSWarner Losh 	DEVMETHOD(spibus_transfer,	spibus_transfer_impl),
2119a795dfaSWarner Losh 
2124b7ec270SMarius Strobl 	DEVMETHOD_END
2139a795dfaSWarner Losh };
2149a795dfaSWarner Losh 
215585300e9SLuiz Otavio O Souza driver_t spibus_driver = {
2169a795dfaSWarner Losh 	"spibus",
2179a795dfaSWarner Losh 	spibus_methods,
2189a795dfaSWarner Losh 	sizeof(struct spibus_softc)
2199a795dfaSWarner Losh };
2209a795dfaSWarner Losh 
2219a795dfaSWarner Losh devclass_t	spibus_devclass;
2229a795dfaSWarner Losh 
223ecd61633SOleksandr Tymoshenko DRIVER_MODULE(spibus, spi, spibus_driver, spibus_devclass, 0, 0);
2249a795dfaSWarner Losh MODULE_VERSION(spibus, 1);
225