xref: /freebsd/sys/dev/spibus/spibus.c (revision f73628e7b16847a685df3bfca4145d1a9f047e1d)
146b39c90SWarner Losh /*-
246b39c90SWarner Losh  * Copyright (c) 2006 M. Warner Losh
346b39c90SWarner Losh  * All rights reserved.
446b39c90SWarner Losh  *
546b39c90SWarner Losh  * Redistribution and use in source and binary forms, with or without
646b39c90SWarner Losh  * modification, are permitted provided that the following conditions
746b39c90SWarner Losh  * are met:
846b39c90SWarner Losh  * 1. Redistributions of source code must retain the above copyright
946b39c90SWarner Losh  *    notice, this list of conditions and the following disclaimer.
1046b39c90SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
1146b39c90SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
1246b39c90SWarner Losh  *    documentation and/or other materials provided with the distribution.
1346b39c90SWarner Losh  *
1446b39c90SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1546b39c90SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1646b39c90SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1746b39c90SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1846b39c90SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1946b39c90SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2046b39c90SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2146b39c90SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2246b39c90SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2346b39c90SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2446b39c90SWarner Losh  * SUCH DAMAGE.
2546b39c90SWarner Losh  *
2646b39c90SWarner Losh  */
2746b39c90SWarner 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);
108f3e8760fSAdrian Chadd 	retval += printf(" mode %d", devi->mode);
1099a795dfaSWarner Losh 	retval += bus_print_child_footer(dev, child);
1109a795dfaSWarner Losh 
1119a795dfaSWarner Losh 	return (retval);
1129a795dfaSWarner Losh }
1139a795dfaSWarner Losh 
1149a795dfaSWarner Losh static void
1159a795dfaSWarner Losh spibus_probe_nomatch(device_t bus, device_t child)
1169a795dfaSWarner Losh {
1179a795dfaSWarner Losh 	struct spibus_ivar *devi = SPIBUS_IVAR(child);
1189a795dfaSWarner Losh 
119*f73628e7SMichael Zhilin 	device_printf(bus, "<unknown card> at cs %d mode %d\n", devi->cs,
120*f73628e7SMichael Zhilin 	    devi->mode);
1219a795dfaSWarner Losh 	return;
1229a795dfaSWarner Losh }
1239a795dfaSWarner Losh 
1249a795dfaSWarner Losh static int
1259a795dfaSWarner Losh spibus_child_location_str(device_t bus, device_t child, char *buf,
1269a795dfaSWarner Losh     size_t buflen)
1279a795dfaSWarner Losh {
1289a795dfaSWarner Losh 	struct spibus_ivar *devi = SPIBUS_IVAR(child);
1299a795dfaSWarner Losh 
1309a795dfaSWarner Losh 	snprintf(buf, buflen, "cs=%d", devi->cs);
1319a795dfaSWarner Losh 	return (0);
1329a795dfaSWarner Losh }
1339a795dfaSWarner Losh 
1349a795dfaSWarner Losh static int
1359a795dfaSWarner Losh spibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
1369a795dfaSWarner Losh     size_t buflen)
1379a795dfaSWarner Losh {
1389a795dfaSWarner Losh 	*buf = '\0';
1399a795dfaSWarner Losh 	return (0);
1409a795dfaSWarner Losh }
1419a795dfaSWarner Losh 
1429a795dfaSWarner Losh static int
1439ef5c83cSRuslan Bukin spibus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1449a795dfaSWarner Losh {
1459a795dfaSWarner Losh 	struct spibus_ivar *devi = SPIBUS_IVAR(child);
1469a795dfaSWarner Losh 
1479a795dfaSWarner Losh 	switch (which) {
1489a795dfaSWarner Losh 	default:
1499a795dfaSWarner Losh 		return (EINVAL);
1509a795dfaSWarner Losh 	case SPIBUS_IVAR_CS:
1519a795dfaSWarner Losh 		*(uint32_t *)result = devi->cs;
1529a795dfaSWarner Losh 		break;
153f3e8760fSAdrian Chadd 	case SPIBUS_IVAR_MODE:
154f3e8760fSAdrian Chadd 		*(uint32_t *)result = devi->mode;
155f3e8760fSAdrian Chadd 		break;
156f3e8760fSAdrian Chadd 	case SPIBUS_IVAR_CLOCK:
157f3e8760fSAdrian Chadd 		*(uint32_t *)result = devi->clock;
15863b92359SAdrian Chadd 		break;
1599a795dfaSWarner Losh 	}
1609a795dfaSWarner Losh 	return (0);
1619a795dfaSWarner Losh }
1629a795dfaSWarner Losh 
1639a795dfaSWarner Losh static device_t
1643d844eddSAndriy Gapon spibus_add_child(device_t dev, u_int order, const char *name, int unit)
1659a795dfaSWarner Losh {
1669a795dfaSWarner Losh 	device_t child;
1679a795dfaSWarner Losh 	struct spibus_ivar *devi;
1689a795dfaSWarner Losh 
1699a795dfaSWarner Losh 	child = device_add_child_ordered(dev, order, name, unit);
1709a795dfaSWarner Losh 	if (child == NULL)
1719a795dfaSWarner Losh 		return (child);
1729a795dfaSWarner Losh 	devi = malloc(sizeof(struct spibus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
17386663fabSWarner Losh 	if (devi == NULL) {
17486663fabSWarner Losh 		device_delete_child(dev, child);
1759a795dfaSWarner Losh 		return (0);
17686663fabSWarner Losh 	}
1779a795dfaSWarner Losh 	device_set_ivars(child, devi);
1789a795dfaSWarner Losh 	return (child);
1799a795dfaSWarner Losh }
1809a795dfaSWarner Losh 
1819a795dfaSWarner Losh static void
1829a795dfaSWarner Losh spibus_hinted_child(device_t bus, const char *dname, int dunit)
1839a795dfaSWarner Losh {
1849a795dfaSWarner Losh 	device_t child;
1859a795dfaSWarner Losh 	struct spibus_ivar *devi;
1869a795dfaSWarner Losh 
1879a795dfaSWarner Losh 	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
1889a795dfaSWarner Losh 	devi = SPIBUS_IVAR(child);
189f3e8760fSAdrian Chadd 	devi->mode = SPIBUS_MODE_NONE;
1909a795dfaSWarner Losh 	resource_int_value(dname, dunit, "cs", &devi->cs);
191f3e8760fSAdrian Chadd 	resource_int_value(dname, dunit, "mode", &devi->mode);
1929a795dfaSWarner Losh }
1939a795dfaSWarner Losh 
1949a795dfaSWarner Losh static int
1959a795dfaSWarner Losh spibus_transfer_impl(device_t dev, device_t child, struct spi_command *cmd)
1969a795dfaSWarner Losh {
19786663fabSWarner Losh 	return (SPIBUS_TRANSFER(device_get_parent(dev), child, cmd));
1989a795dfaSWarner Losh }
1999a795dfaSWarner Losh 
2009a795dfaSWarner Losh static device_method_t spibus_methods[] = {
2019a795dfaSWarner Losh 	/* Device interface */
2029a795dfaSWarner Losh 	DEVMETHOD(device_probe,		spibus_probe),
2039a795dfaSWarner Losh 	DEVMETHOD(device_attach,	spibus_attach),
2049a795dfaSWarner Losh 	DEVMETHOD(device_detach,	spibus_detach),
2059a795dfaSWarner Losh 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
2069a795dfaSWarner Losh 	DEVMETHOD(device_suspend,	spibus_suspend),
2079a795dfaSWarner Losh 	DEVMETHOD(device_resume,	spibus_resume),
2089a795dfaSWarner Losh 
2099a795dfaSWarner Losh 	/* Bus interface */
2109a795dfaSWarner Losh 	DEVMETHOD(bus_add_child,	spibus_add_child),
2119a795dfaSWarner Losh 	DEVMETHOD(bus_print_child,	spibus_print_child),
2129a795dfaSWarner Losh 	DEVMETHOD(bus_probe_nomatch,	spibus_probe_nomatch),
2139a795dfaSWarner Losh 	DEVMETHOD(bus_read_ivar,	spibus_read_ivar),
2149a795dfaSWarner Losh 	DEVMETHOD(bus_child_pnpinfo_str, spibus_child_pnpinfo_str),
2159a795dfaSWarner Losh 	DEVMETHOD(bus_child_location_str, spibus_child_location_str),
2169a795dfaSWarner Losh 	DEVMETHOD(bus_hinted_child,	spibus_hinted_child),
2179a795dfaSWarner Losh 
2189a795dfaSWarner Losh 	/* spibus interface */
2199a795dfaSWarner Losh 	DEVMETHOD(spibus_transfer,	spibus_transfer_impl),
2209a795dfaSWarner Losh 
2214b7ec270SMarius Strobl 	DEVMETHOD_END
2229a795dfaSWarner Losh };
2239a795dfaSWarner Losh 
224585300e9SLuiz Otavio O Souza driver_t spibus_driver = {
2259a795dfaSWarner Losh 	"spibus",
2269a795dfaSWarner Losh 	spibus_methods,
2279a795dfaSWarner Losh 	sizeof(struct spibus_softc)
2289a795dfaSWarner Losh };
2299a795dfaSWarner Losh 
2309a795dfaSWarner Losh devclass_t	spibus_devclass;
2319a795dfaSWarner Losh 
232ecd61633SOleksandr Tymoshenko DRIVER_MODULE(spibus, spi, spibus_driver, spibus_devclass, 0, 0);
2339a795dfaSWarner Losh MODULE_VERSION(spibus, 1);
234