xref: /freebsd/sys/dev/p2sb/p2sb.c (revision ba58fd668a0e544b4af225f6dfee402e3213a8bf)
13706af42SArnaud Ysmal /*-
23706af42SArnaud Ysmal  * Copyright (c) 2018 Stormshield
33706af42SArnaud Ysmal  * All rights reserved.
43706af42SArnaud Ysmal  *
53706af42SArnaud Ysmal  * Redistribution and use in source and binary forms, with or without
63706af42SArnaud Ysmal  * modification, are permitted provided that the following conditions
73706af42SArnaud Ysmal  * are met:
83706af42SArnaud Ysmal  * 1. Redistributions of source code must retain the above copyright
93706af42SArnaud Ysmal  *    notice, this list of conditions and the following disclaimer.
103706af42SArnaud Ysmal  * 2. Redistributions in binary form must reproduce the above copyright
113706af42SArnaud Ysmal  *    notice, this list of conditions and the following disclaimer in the
123706af42SArnaud Ysmal  *    documentation and/or other materials provided with the distribution.
133706af42SArnaud Ysmal  *
143706af42SArnaud Ysmal  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
153706af42SArnaud Ysmal  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
163706af42SArnaud Ysmal  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
173706af42SArnaud Ysmal  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
183706af42SArnaud Ysmal  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
193706af42SArnaud Ysmal  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
203706af42SArnaud Ysmal  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
213706af42SArnaud Ysmal  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
223706af42SArnaud Ysmal  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
233706af42SArnaud Ysmal  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
243706af42SArnaud Ysmal  * SUCH DAMAGE.
253706af42SArnaud Ysmal  */
263706af42SArnaud Ysmal 
273706af42SArnaud Ysmal /*
283706af42SArnaud Ysmal  * Implementation of Primary to Sideband bridge (P2SB), the documentation is available here :
293706af42SArnaud Ysmal  * https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/c620-series-chipset-datasheet.pdf
303706af42SArnaud Ysmal  * section 36.9 P2SB Bridge.
313706af42SArnaud Ysmal  * This device exposes a 16MB memory block, this block is composed of 256 64KB blocks called ports.
323706af42SArnaud Ysmal  * The indexes of this array (target port ID) can be found on the Table 36-10 of the documentation.
333706af42SArnaud Ysmal  */
343706af42SArnaud Ysmal 
353706af42SArnaud Ysmal #include <sys/param.h>
363706af42SArnaud Ysmal #include <sys/module.h>
373706af42SArnaud Ysmal #include <sys/systm.h>
383706af42SArnaud Ysmal #include <sys/errno.h>
393706af42SArnaud Ysmal #include <sys/kernel.h>
403706af42SArnaud Ysmal #include <sys/lock.h>
413706af42SArnaud Ysmal #include <sys/malloc.h>
423706af42SArnaud Ysmal #include <sys/mutex.h>
433706af42SArnaud Ysmal #include <sys/bus.h>
443706af42SArnaud Ysmal 
453706af42SArnaud Ysmal #include <machine/bus.h>
463706af42SArnaud Ysmal #include <sys/rman.h>
473706af42SArnaud Ysmal #include <machine/resource.h>
483706af42SArnaud Ysmal 
493706af42SArnaud Ysmal #include <dev/pci/pcivar.h>
503706af42SArnaud Ysmal #include <dev/pci/pcireg.h>
513706af42SArnaud Ysmal 
523706af42SArnaud Ysmal #include "p2sb.h"
533706af42SArnaud Ysmal 
543706af42SArnaud Ysmal #define PCI_PRODUCT_LEWISBURG_P2SB 0xa1a08086
553706af42SArnaud Ysmal 
563706af42SArnaud Ysmal #define P2SB_PORT2ADDRESS_SHIFT 16
573706af42SArnaud Ysmal #define P2SB_PORT_ADDRESS(port) ((uint32_t)port << P2SB_PORT2ADDRESS_SHIFT)
583706af42SArnaud Ysmal 
593706af42SArnaud Ysmal static const uint8_t lbg_communities[] = {
603706af42SArnaud Ysmal 	0xAF, 0xAE, 0xAD, 0xAC, 0xAB, 0x11
613706af42SArnaud Ysmal };
623706af42SArnaud Ysmal 
633706af42SArnaud Ysmal /* The softc holds our per-instance data. */
643706af42SArnaud Ysmal struct p2sb_softc {
653706af42SArnaud Ysmal 	device_t	dev;
663706af42SArnaud Ysmal 	int rid;
673706af42SArnaud Ysmal 	struct resource *res;
683706af42SArnaud Ysmal 	struct intel_community *communities;
693706af42SArnaud Ysmal 	int ncommunities;
703706af42SArnaud Ysmal 	struct mtx mutex;
713706af42SArnaud Ysmal };
723706af42SArnaud Ysmal 
733706af42SArnaud Ysmal int
p2sb_get_port(device_t dev,int unit)743706af42SArnaud Ysmal p2sb_get_port(device_t dev, int unit)
753706af42SArnaud Ysmal {
763706af42SArnaud Ysmal 
773706af42SArnaud Ysmal 	if (unit >= nitems(lbg_communities))
783706af42SArnaud Ysmal 		return (EINVAL);
793706af42SArnaud Ysmal 	return (lbg_communities[unit]);
803706af42SArnaud Ysmal }
813706af42SArnaud Ysmal 
823706af42SArnaud Ysmal uint32_t
p2sb_port_read_4(device_t dev,uint8_t port,uint32_t reg)833706af42SArnaud Ysmal p2sb_port_read_4(device_t dev, uint8_t port, uint32_t reg)
843706af42SArnaud Ysmal {
853706af42SArnaud Ysmal 	struct p2sb_softc *sc;
863706af42SArnaud Ysmal 
873706af42SArnaud Ysmal 	KASSERT(reg < (1<<P2SB_PORT2ADDRESS_SHIFT), ("register out of port"));
883706af42SArnaud Ysmal 	sc = device_get_softc(dev);
893706af42SArnaud Ysmal 	return (bus_read_4(sc->res, P2SB_PORT_ADDRESS(port) + reg));
903706af42SArnaud Ysmal }
913706af42SArnaud Ysmal 
923706af42SArnaud Ysmal void
p2sb_port_write_4(device_t dev,uint8_t port,uint32_t reg,uint32_t val)933706af42SArnaud Ysmal p2sb_port_write_4(device_t dev, uint8_t port, uint32_t reg, uint32_t val)
943706af42SArnaud Ysmal {
953706af42SArnaud Ysmal 	struct p2sb_softc *sc;
963706af42SArnaud Ysmal 
973706af42SArnaud Ysmal 	KASSERT(reg < (1<<P2SB_PORT2ADDRESS_SHIFT), ("register out of port"));
983706af42SArnaud Ysmal 	sc = device_get_softc(dev);
993706af42SArnaud Ysmal 	bus_write_4(sc->res, P2SB_PORT_ADDRESS(port) + reg, val);
1003706af42SArnaud Ysmal }
1013706af42SArnaud Ysmal 
1023706af42SArnaud Ysmal void
p2sb_lock(device_t dev)1033706af42SArnaud Ysmal p2sb_lock(device_t dev)
1043706af42SArnaud Ysmal {
1053706af42SArnaud Ysmal 	struct p2sb_softc *sc;
1063706af42SArnaud Ysmal 
1073706af42SArnaud Ysmal 	sc = device_get_softc(dev);
1083706af42SArnaud Ysmal 	mtx_lock_spin(&sc->mutex);
1093706af42SArnaud Ysmal }
1103706af42SArnaud Ysmal 
1113706af42SArnaud Ysmal void
p2sb_unlock(device_t dev)1123706af42SArnaud Ysmal p2sb_unlock(device_t dev)
1133706af42SArnaud Ysmal {
1143706af42SArnaud Ysmal 	struct p2sb_softc *sc;
1153706af42SArnaud Ysmal 
1163706af42SArnaud Ysmal 	sc = device_get_softc(dev);
1173706af42SArnaud Ysmal 	mtx_unlock_spin(&sc->mutex);
1183706af42SArnaud Ysmal }
1193706af42SArnaud Ysmal 
1203706af42SArnaud Ysmal 
1213706af42SArnaud Ysmal static int
p2sb_probe(device_t dev)1223706af42SArnaud Ysmal p2sb_probe(device_t dev)
1233706af42SArnaud Ysmal {
1243706af42SArnaud Ysmal 
1253706af42SArnaud Ysmal 	if (pci_get_devid(dev) == PCI_PRODUCT_LEWISBURG_P2SB) {
1263706af42SArnaud Ysmal 		device_set_desc(dev, "Lewisburg P2SB");
1273706af42SArnaud Ysmal 		return (BUS_PROBE_DEFAULT);
1283706af42SArnaud Ysmal 	}
1293706af42SArnaud Ysmal 	return (ENXIO);
1303706af42SArnaud Ysmal }
1313706af42SArnaud Ysmal 
1323706af42SArnaud Ysmal /* Attach function is only called if the probe is successful. */
1333706af42SArnaud Ysmal 
1343706af42SArnaud Ysmal static int
p2sb_attach(device_t dev)1353706af42SArnaud Ysmal p2sb_attach(device_t dev)
1363706af42SArnaud Ysmal {
1373706af42SArnaud Ysmal 	struct p2sb_softc *sc;
1383706af42SArnaud Ysmal 	int i;
1393706af42SArnaud Ysmal 
1403706af42SArnaud Ysmal 	sc = device_get_softc(dev);
1413706af42SArnaud Ysmal 	sc->dev = dev;
1423706af42SArnaud Ysmal 	sc->rid = PCIR_BAR(0);
1433706af42SArnaud Ysmal 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid, RF_ACTIVE);
1443706af42SArnaud Ysmal 	if (sc->res == NULL) {
1453706af42SArnaud Ysmal 		device_printf(dev, "Could not allocate memory.\n");
1463706af42SArnaud Ysmal 		return (ENXIO);
1473706af42SArnaud Ysmal 	}
1483706af42SArnaud Ysmal 	mtx_init(&sc->mutex, device_get_nameunit(dev), NULL, MTX_SPIN);
1493706af42SArnaud Ysmal 	for (i = 0; i < nitems(lbg_communities); ++i)
1503706af42SArnaud Ysmal 		device_add_child(dev, "lbggpiocm", i);
1513706af42SArnaud Ysmal 
1523706af42SArnaud Ysmal 	return (bus_generic_attach(dev));
1533706af42SArnaud Ysmal }
1543706af42SArnaud Ysmal 
1553706af42SArnaud Ysmal /* Detach device. */
1563706af42SArnaud Ysmal 
1573706af42SArnaud Ysmal static int
p2sb_detach(device_t dev)1583706af42SArnaud Ysmal p2sb_detach(device_t dev)
1593706af42SArnaud Ysmal {
1603706af42SArnaud Ysmal 	struct p2sb_softc *sc;
1613706af42SArnaud Ysmal 
1623706af42SArnaud Ysmal 	/* Teardown the state in our softc created in our attach routine. */
1633706af42SArnaud Ysmal 	device_delete_children(dev);
1643706af42SArnaud Ysmal 	sc = device_get_softc(dev);
1653706af42SArnaud Ysmal 	mtx_destroy(&sc->mutex);
1663706af42SArnaud Ysmal 	if (sc->res != NULL)
1673706af42SArnaud Ysmal 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
1683706af42SArnaud Ysmal 	return (0);
1693706af42SArnaud Ysmal }
1703706af42SArnaud Ysmal 
1713706af42SArnaud Ysmal /* Called during system shutdown after sync. */
1723706af42SArnaud Ysmal 
1733706af42SArnaud Ysmal static int
p2sb_shutdown(device_t dev)1743706af42SArnaud Ysmal p2sb_shutdown(device_t dev)
1753706af42SArnaud Ysmal {
1763706af42SArnaud Ysmal 
1773706af42SArnaud Ysmal 	return (0);
1783706af42SArnaud Ysmal }
1793706af42SArnaud Ysmal 
1803706af42SArnaud Ysmal /*
1813706af42SArnaud Ysmal  * Device suspend routine.
1823706af42SArnaud Ysmal  */
1833706af42SArnaud Ysmal static int
p2sb_suspend(device_t dev)1843706af42SArnaud Ysmal p2sb_suspend(device_t dev)
1853706af42SArnaud Ysmal {
1863706af42SArnaud Ysmal 
1873706af42SArnaud Ysmal 	return (0);
1883706af42SArnaud Ysmal }
1893706af42SArnaud Ysmal 
1903706af42SArnaud Ysmal /*
1913706af42SArnaud Ysmal  * Device resume routine.
1923706af42SArnaud Ysmal  */
1933706af42SArnaud Ysmal static int
p2sb_resume(device_t dev)1943706af42SArnaud Ysmal p2sb_resume(device_t dev)
1953706af42SArnaud Ysmal {
1963706af42SArnaud Ysmal 
1973706af42SArnaud Ysmal 	return (0);
1983706af42SArnaud Ysmal }
1993706af42SArnaud Ysmal 
2003706af42SArnaud Ysmal static device_method_t p2sb_methods[] = {
2013706af42SArnaud Ysmal 	/* Device interface */
2023706af42SArnaud Ysmal 	DEVMETHOD(device_probe,		p2sb_probe),
2033706af42SArnaud Ysmal 	DEVMETHOD(device_attach,	p2sb_attach),
2043706af42SArnaud Ysmal 	DEVMETHOD(device_detach,	p2sb_detach),
2053706af42SArnaud Ysmal 	DEVMETHOD(device_shutdown,	p2sb_shutdown),
2063706af42SArnaud Ysmal 	DEVMETHOD(device_suspend,	p2sb_suspend),
2073706af42SArnaud Ysmal 	DEVMETHOD(device_resume,	p2sb_resume),
2083706af42SArnaud Ysmal 
2093706af42SArnaud Ysmal 	DEVMETHOD_END
2103706af42SArnaud Ysmal };
2113706af42SArnaud Ysmal 
2123706af42SArnaud Ysmal DEFINE_CLASS_0(p2sb, p2sb_driver, p2sb_methods, sizeof(struct p2sb_softc));
213*ba58fd66SJohn Baldwin DRIVER_MODULE(p2sb, pci, p2sb_driver, 0, 0);
214