Index: head/sys/arm/samsung/exynos/exynos5_combiner.c =================================================================== --- head/sys/arm/samsung/exynos/exynos5_combiner.c (revision 291404) +++ head/sys/arm/samsung/exynos/exynos5_combiner.c (revision 291405) @@ -1,412 +1,415 @@ /*- * Copyright (c) 2014 Ruslan Bukin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Samsung Exynos 5 Interrupt Combiner * Chapter 7, Exynos 5 Dual User's Manual Public Rev 1.00 */ - +#ifdef USB_GLOBAL_INCLUDE_FILE +#include USB_GLOBAL_INCLUDE_FILE +#else #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#endif #include #include #define NGRP 32 #define IESR(n) (0x10 * n + 0x0) /* Interrupt enable set */ #define IECR(n) (0x10 * n + 0x4) /* Interrupt enable clear */ #define ISTR(n) (0x10 * n + 0x8) /* Interrupt status */ #define IMSR(n) (0x10 * n + 0xC) /* Interrupt masked status */ #define CIPSR 0x100 /* Combined interrupt pending */ struct combiner_softc { struct resource *res[1 + NGRP]; bus_space_tag_t bst; bus_space_handle_t bsh; void *ih[NGRP]; device_t dev; }; struct combiner_softc *combiner_sc; static struct resource_spec combiner_spec[] = { { SYS_RES_MEMORY, 0, RF_ACTIVE }, { SYS_RES_IRQ, 0, RF_ACTIVE }, { SYS_RES_IRQ, 1, RF_ACTIVE }, { SYS_RES_IRQ, 2, RF_ACTIVE }, { SYS_RES_IRQ, 3, RF_ACTIVE }, { SYS_RES_IRQ, 4, RF_ACTIVE }, { SYS_RES_IRQ, 5, RF_ACTIVE }, { SYS_RES_IRQ, 6, RF_ACTIVE }, { SYS_RES_IRQ, 7, RF_ACTIVE }, { SYS_RES_IRQ, 8, RF_ACTIVE }, { SYS_RES_IRQ, 9, RF_ACTIVE }, { SYS_RES_IRQ, 10, RF_ACTIVE }, { SYS_RES_IRQ, 11, RF_ACTIVE }, { SYS_RES_IRQ, 12, RF_ACTIVE }, { SYS_RES_IRQ, 13, RF_ACTIVE }, { SYS_RES_IRQ, 14, RF_ACTIVE }, { SYS_RES_IRQ, 15, RF_ACTIVE }, { SYS_RES_IRQ, 16, RF_ACTIVE }, { SYS_RES_IRQ, 17, RF_ACTIVE }, { SYS_RES_IRQ, 18, RF_ACTIVE }, { SYS_RES_IRQ, 19, RF_ACTIVE }, { SYS_RES_IRQ, 20, RF_ACTIVE }, { SYS_RES_IRQ, 21, RF_ACTIVE }, { SYS_RES_IRQ, 22, RF_ACTIVE }, { SYS_RES_IRQ, 23, RF_ACTIVE }, { SYS_RES_IRQ, 24, RF_ACTIVE }, { SYS_RES_IRQ, 25, RF_ACTIVE }, { SYS_RES_IRQ, 26, RF_ACTIVE }, { SYS_RES_IRQ, 27, RF_ACTIVE }, { SYS_RES_IRQ, 28, RF_ACTIVE }, { SYS_RES_IRQ, 29, RF_ACTIVE }, { SYS_RES_IRQ, 30, RF_ACTIVE }, { SYS_RES_IRQ, 31, RF_ACTIVE }, { -1, 0 } }; struct combiner_entry { int combiner_id; int bit; char *source_name; }; static struct combiner_entry interrupt_table[] = { { 63, 1, "EINT[15]" }, { 63, 0, "EINT[14]" }, { 62, 1, "EINT[13]" }, { 62, 0, "EINT[12]" }, { 61, 1, "EINT[11]" }, { 61, 0, "EINT[10]" }, { 60, 1, "EINT[9]" }, { 60, 0, "EINT[8]" }, { 59, 1, "EINT[7]" }, { 59, 0, "EINT[6]" }, { 58, 1, "EINT[5]" }, { 58, 0, "EINT[4]" }, { 57, 3, "MCT_G3" }, { 57, 2, "MCT_G2" }, { 57, 1, "EINT[3]" }, { 57, 0, "EINT[2]" }, { 56, 6, "SYSMMU_G2D[1]" }, { 56, 5, "SYSMMU_G2D[0]" }, { 56, 2, "SYSMMU_FIMC_LITE1[1]" }, { 56, 1, "SYSMMU_FIMC_LITE1[0]" }, { 56, 0, "EINT[1]" }, { 55, 4, "MCT_G1" }, { 55, 3, "MCT_G0" }, { 55, 0, "EINT[0]" }, { 54, 7, "CPU_nCNTVIRQ[1]" }, { 54, 6, "CPU_nCTIIRQ[1]" }, { 54, 5, "CPU_nCNTPSIRQ[1]" }, { 54, 4, "CPU_nPMUIRQ[1]" }, { 54, 3, "CPU_nCNTPNSIRQ[1]" }, { 54, 2, "CPU_PARITYFAILSCU[1]" }, { 54, 1, "CPU_nCNTHPIRQ[1]" }, { 54, 0, "PARITYFAIL[1]" }, { 53, 1, "CPU_nIRQ[1]" }, { 52, 0, "CPU_nIRQ[0]" }, { 51, 7, "CPU_nRAMERRIRQ" }, { 51, 6, "CPU_nAXIERRIRQ" }, { 51, 4, "INT_COMB_ISP_GIC" }, { 51, 3, "INT_COMB_IOP_GIC" }, { 51, 2, "CCI_nERRORIRQ" }, { 51, 1, "INT_COMB_ARMISP_GIC" }, { 51, 0, "INT_COMB_ARMIOP_GIC" }, { 50, 7, "DISP1[3]" }, { 50, 6, "DISP1[2]" }, { 50, 5, "DISP1[1]" }, { 50, 4, "DISP1[0]" }, { 49, 3, "SSCM_PULSE_IRQ_C2CIF[1]" }, { 49, 2, "SSCM_PULSE_IRQ_C2CIF[0]" }, { 49, 1, "SSCM_IRQ_C2CIF[1]" }, { 49, 0, "SSCM_IRQ_C2CIF[0]" }, { 48, 3, "PEREV_M1_CDREX" }, { 48, 2, "PEREV_M0_CDREX" }, { 48, 1, "PEREV_A1_CDREX" }, { 48, 0, "PEREV_A0_CDREX" }, { 47, 3, "MDMA0_ABORT" }, /* 46 is fully reserved */ { 45, 1, "MDMA1_ABORT" }, /* 44 is fully reserved */ { 43, 7, "SYSMMU_DRCISP[1]" }, { 43, 6, "SYSMMU_DRCISP[0]" }, { 43, 1, "SYSMMU_ODC[1]" }, { 43, 0, "SYSMMU_ODC[0]" }, { 42, 7, "SYSMMU_ISP[1]" }, { 42, 6, "SYSMMU_ISP[0]" }, { 42, 5, "SYSMMU_DIS0[1]" }, { 42, 4, "SYSMMU_DIS0[0]" }, { 42, 3, "DP1" }, { 41, 5, "SYSMMU_DIS1[1]" }, { 41, 4, "SYSMMU_DIS1[0]" }, { 40, 6, "SYSMMU_MFCL[1]" }, { 40, 5, "SYSMMU_MFCL[0]" }, { 39, 5, "SYSMMU_TV_M0[1]" }, { 39, 4, "SYSMMU_TV_M0[0]" }, { 39, 3, "SYSMMU_MDMA1[1]" }, { 39, 2, "SYSMMU_MDMA1[0]" }, { 39, 1, "SYSMMU_MDMA0[1]" }, { 39, 0, "SYSMMU_MDMA0[0]" }, { 38, 7, "SYSMMU_SSS[1]" }, { 38, 6, "SYSMMU_SSS[0]" }, { 38, 5, "SYSMMU_RTIC[1]" }, { 38, 4, "SYSMMU_RTIC[0]" }, { 38, 3, "SYSMMU_MFCR[1]" }, { 38, 2, "SYSMMU_MFCR[0]" }, { 38, 1, "SYSMMU_ARM[1]" }, { 38, 0, "SYSMMU_ARM[0]" }, { 37, 7, "SYSMMU_3DNR[1]" }, { 37, 6, "SYSMMU_3DNR[0]" }, { 37, 5, "SYSMMU_MCUISP[1]" }, { 37, 4, "SYSMMU_MCUISP[0]" }, { 37, 3, "SYSMMU_SCALERCISP[1]" }, { 37, 2, "SYSMMU_SCALERCISP[0]" }, { 37, 1, "SYSMMU_FDISP[1]" }, { 37, 0, "SYSMMU_FDISP[0]" }, { 36, 7, "MCUIOP_CTIIRQ" }, { 36, 6, "MCUIOP_PMUIRQ" }, { 36, 5, "MCUISP_CTIIRQ" }, { 36, 4, "MCUISP_PMUIRQ" }, { 36, 3, "SYSMMU_JPEGX[1]" }, { 36, 2, "SYSMMU_JPEGX[0]" }, { 36, 1, "SYSMMU_ROTATOR[1]" }, { 36, 0, "SYSMMU_ROTATOR[0]" }, { 35, 7, "SYSMMU_SCALERPISP[1]" }, { 35, 6, "SYSMMU_SCALERPISP[0]" }, { 35, 5, "SYSMMU_FIMC_LITE0[1]" }, { 35, 4, "SYSMMU_FIMC_LITE0[0]" }, { 35, 3, "SYSMMU_DISP1_M0[1]" }, { 35, 2, "SYSMMU_DISP1_M0[0]" }, { 35, 1, "SYSMMU_FIMC_LITE2[1]" }, { 35, 0, "SYSMMU_FIMC_LITE2[0]" }, { 34, 7, "SYSMMU_GSCL3[1]" }, { 34, 6, "SYSMMU_GSCL3[0]" }, { 34, 5, "SYSMMU_GSCL2[1]" }, { 34, 4, "SYSMMU_GSCL2[0]" }, { 34, 3, "SYSMMU_GSCL1[1]" }, { 34, 2, "SYSMMU_GSCL1[0]" }, { 34, 1, "SYSMMU_GSCL0[1]" }, { 34, 0, "SYSMMU_GSCL0[0]" }, { 33, 7, "CPU_nCNTVIRQ[0]" }, { 33, 6, "CPU_nCNTPSIRQ[0]" }, { 33, 5, "CPU_nCNTPSNIRQ[0]" }, { 33, 4, "CPU_nCNTHPIRQ[0]" }, { 33, 3, "CPU_nCTIIRQ[0]" }, { 33, 2, "CPU_nPMUIRQ[0]" }, { 33, 1, "CPU_PARITYFAILSCU[0]" }, { 33, 0, "CPU_PARITYFAIL0" }, { 32, 7, "TZASC_XR1BXW" }, { 32, 6, "TZASC_XR1BXR" }, { 32, 5, "TZASC_XLBXW" }, { 32, 4, "TZASC_XLBXR" }, { 32, 3, "TZASC_DRBXW" }, { 32, 2, "TZASC_DRBXR" }, { 32, 1, "TZASC_CBXW" }, { 32, 0, "TZASC_CBXR" }, { -1, -1, NULL }, }; struct combined_intr { uint32_t enabled; void (*ih) (void *); void *ih_user; }; static struct combined_intr intr_map[32][8]; static void combiner_intr(void *arg) { struct combiner_softc *sc; void (*ih) (void *); void *ih_user; int enabled; int intrs; int shift; int cirq; int grp; int i,n; sc = arg; intrs = READ4(sc, CIPSR); for (grp = 0; grp < 32; grp++) { if (intrs & (1 << grp)) { n = (grp / 4); shift = (grp % 4) * 8; cirq = READ4(sc, ISTR(n)); for (i = 0; i < 8; i++) { if (cirq & (1 << (i + shift))) { ih = intr_map[grp][i].ih; ih_user = intr_map[grp][i].ih_user; enabled = intr_map[grp][i].enabled; if (enabled && (ih != NULL)) { ih(ih_user); } } } } } } void combiner_setup_intr(char *source_name, void (*ih)(void *), void *ih_user) { struct combiner_entry *entry; struct combined_intr *cirq; struct combiner_softc *sc; int shift; int reg; int grp; int n; int i; sc = combiner_sc; if (sc == NULL) { device_printf(sc->dev, "Error: combiner is not attached\n"); return; } entry = NULL; for (i = 0; i < NGRP && interrupt_table[i].bit != -1; i++) { if (strcmp(interrupt_table[i].source_name, source_name) == 0) { entry = &interrupt_table[i]; } } if (entry == NULL) { device_printf(sc->dev, "Can't find interrupt name %s\n", source_name); return; } #if 0 device_printf(sc->dev, "Setting up interrupt %s\n", source_name); #endif grp = entry->combiner_id - 32; cirq = &intr_map[grp][entry->bit]; cirq->enabled = 1; cirq->ih = ih; cirq->ih_user = ih_user; n = grp / 4; shift = (grp % 4) * 8 + entry->bit; reg = (1 << shift); WRITE4(sc, IESR(n), reg); } static int combiner_probe(device_t dev) { if (!ofw_bus_status_okay(dev)) return (ENXIO); if (!ofw_bus_is_compatible(dev, "exynos,combiner")) return (ENXIO); device_set_desc(dev, "Samsung Exynos 5 Interrupt Combiner"); return (BUS_PROBE_DEFAULT); } static int combiner_attach(device_t dev) { struct combiner_softc *sc; int err; int i; sc = device_get_softc(dev); sc->dev = dev; if (bus_alloc_resources(dev, combiner_spec, sc->res)) { device_printf(dev, "could not allocate resources\n"); return (ENXIO); } /* Memory interface */ sc->bst = rman_get_bustag(sc->res[0]); sc->bsh = rman_get_bushandle(sc->res[0]); combiner_sc = sc; /* Setup interrupt handler */ for (i = 0; i < NGRP; i++) { err = bus_setup_intr(dev, sc->res[1+i], INTR_TYPE_BIO | \ INTR_MPSAFE, NULL, combiner_intr, sc, &sc->ih[i]); if (err) { device_printf(dev, "Unable to alloc int resource.\n"); return (ENXIO); } } return (0); } static device_method_t combiner_methods[] = { DEVMETHOD(device_probe, combiner_probe), DEVMETHOD(device_attach, combiner_attach), { 0, 0 } }; static driver_t combiner_driver = { "combiner", combiner_methods, sizeof(struct combiner_softc), }; static devclass_t combiner_devclass; DRIVER_MODULE(combiner, simplebus, combiner_driver, combiner_devclass, 0, 0); Index: head/sys/arm/samsung/exynos/exynos5_ehci.c =================================================================== --- head/sys/arm/samsung/exynos/exynos5_ehci.c (revision 291404) +++ head/sys/arm/samsung/exynos/exynos5_ehci.c (revision 291405) @@ -1,394 +1,398 @@ /*- * Copyright (c) 2013-2014 Ruslan Bukin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#ifdef USB_GLOBAL_INCLUDE_FILE +#include USB_GLOBAL_INCLUDE_FILE +#else #include __FBSDID("$FreeBSD$"); #include "opt_bus.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "gpio_if.h" #include "opt_platform.h" +#endif /* GPIO control */ #define GPIO_OUTPUT 1 #define GPIO_INPUT 0 #define PIN_USB 161 /* SYSREG */ #define EXYNOS5_SYSREG_USB2_PHY 0x0 #define USB2_MODE_HOST 0x1 /* USB HOST */ #define HOST_CTRL_CLK_24MHZ (5 << 16) #define HOST_CTRL_CLK_MASK (7 << 16) #define HOST_CTRL_SIDDQ (1 << 6) #define HOST_CTRL_SLEEP (1 << 5) #define HOST_CTRL_SUSPEND (1 << 4) #define HOST_CTRL_RESET_LINK (1 << 1) #define HOST_CTRL_RESET_PHY (1 << 0) #define HOST_CTRL_RESET_PHY_ALL (1U << 31) /* Forward declarations */ static int exynos_ehci_attach(device_t dev); static int exynos_ehci_detach(device_t dev); static int exynos_ehci_probe(device_t dev); struct exynos_ehci_softc { device_t dev; ehci_softc_t base; struct resource *res[4]; bus_space_tag_t host_bst; bus_space_tag_t sysreg_bst; bus_space_handle_t host_bsh; bus_space_handle_t sysreg_bsh; }; static struct resource_spec exynos_ehci_spec[] = { { SYS_RES_MEMORY, 0, RF_ACTIVE }, { SYS_RES_MEMORY, 1, RF_ACTIVE }, { SYS_RES_MEMORY, 2, RF_ACTIVE }, { SYS_RES_IRQ, 0, RF_ACTIVE }, { -1, 0 } }; static device_method_t ehci_methods[] = { /* Device interface */ DEVMETHOD(device_probe, exynos_ehci_probe), DEVMETHOD(device_attach, exynos_ehci_attach), DEVMETHOD(device_detach, exynos_ehci_detach), DEVMETHOD(device_suspend, bus_generic_suspend), DEVMETHOD(device_resume, bus_generic_resume), DEVMETHOD(device_shutdown, bus_generic_shutdown), /* Bus interface */ DEVMETHOD(bus_print_child, bus_generic_print_child), { 0, 0 } }; /* kobj_class definition */ static driver_t ehci_driver = { "ehci", ehci_methods, sizeof(struct exynos_ehci_softc) }; static devclass_t ehci_devclass; DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0); MODULE_DEPEND(ehci, usb, 1, 1, 1); /* * Public methods */ static int exynos_ehci_probe(device_t dev) { if (!ofw_bus_status_okay(dev)) return (ENXIO); if (ofw_bus_is_compatible(dev, "exynos,usb-ehci") == 0) return (ENXIO); device_set_desc(dev, "Exynos integrated USB controller"); return (BUS_PROBE_DEFAULT); } static int gpio_ctrl(struct exynos_ehci_softc *esc, int dir, int power) { device_t gpio_dev; /* Get the GPIO device, we need this to give power to USB */ gpio_dev = devclass_get_device(devclass_find("gpio"), 0); if (gpio_dev == NULL) { device_printf(esc->dev, "cant find gpio_dev\n"); return (1); } if (power) GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_HIGH); else GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_LOW); if (dir) GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_OUTPUT); else GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_INPUT); return (0); } static int reset_hsic_hub(struct exynos_ehci_softc *esc, phandle_t hub) { device_t gpio_dev; pcell_t pin; /* TODO: check that hub is compatible with "smsc,usb3503" */ if (!OF_hasprop(hub, "freebsd,reset-gpio")) { return (1); } if (OF_getencprop(hub, "freebsd,reset-gpio", &pin, sizeof(pin)) < 0) { device_printf(esc->dev, "failed to decode reset GPIO pin number for HSIC hub\n"); return (1); } /* Get the GPIO device, we need this to give power to USB */ gpio_dev = devclass_get_device(devclass_find("gpio"), 0); if (gpio_dev == NULL) { device_printf(esc->dev, "Cant find gpio device\n"); return (1); } GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_LOW); DELAY(100); GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_HIGH); return (0); } static int phy_init(struct exynos_ehci_softc *esc) { int reg; phandle_t hub; gpio_ctrl(esc, GPIO_INPUT, 1); /* set USB HOST mode */ bus_space_write_4(esc->sysreg_bst, esc->sysreg_bsh, EXYNOS5_SYSREG_USB2_PHY, USB2_MODE_HOST); /* Power ON phy */ usb2_phy_power_on(); reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0); reg &= ~(HOST_CTRL_CLK_MASK | HOST_CTRL_RESET_PHY | HOST_CTRL_RESET_PHY_ALL | HOST_CTRL_SIDDQ | HOST_CTRL_SUSPEND | HOST_CTRL_SLEEP); reg |= (HOST_CTRL_CLK_24MHZ | HOST_CTRL_RESET_LINK); bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg); DELAY(10); reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0); reg &= ~(HOST_CTRL_RESET_LINK); bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg); if ((hub = OF_finddevice("/hsichub")) != 0) { reset_hsic_hub(esc, hub); } gpio_ctrl(esc, GPIO_OUTPUT, 1); return (0); } static int exynos_ehci_attach(device_t dev) { struct exynos_ehci_softc *esc; ehci_softc_t *sc; bus_space_handle_t bsh; int err; esc = device_get_softc(dev); esc->dev = dev; sc = &esc->base; sc->sc_bus.parent = dev; sc->sc_bus.devices = sc->sc_devices; sc->sc_bus.devices_max = EHCI_MAX_DEVICES; sc->sc_bus.dma_bits = 32; if (bus_alloc_resources(dev, exynos_ehci_spec, esc->res)) { device_printf(dev, "could not allocate resources\n"); return (ENXIO); } /* EHCI registers */ sc->sc_io_tag = rman_get_bustag(esc->res[0]); bsh = rman_get_bushandle(esc->res[0]); sc->sc_io_size = rman_get_size(esc->res[0]); /* EHCI HOST ctrl registers */ esc->host_bst = rman_get_bustag(esc->res[1]); esc->host_bsh = rman_get_bushandle(esc->res[1]); /* SYSREG */ esc->sysreg_bst = rman_get_bustag(esc->res[2]); esc->sysreg_bsh = rman_get_bushandle(esc->res[2]); /* get all DMA memory */ if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev), &ehci_iterate_hw_softc)) return (ENXIO); /* * Set handle to USB related registers subregion used by * generic EHCI driver. */ err = bus_space_subregion(sc->sc_io_tag, bsh, 0x0, sc->sc_io_size, &sc->sc_io_hdl); if (err != 0) return (ENXIO); phy_init(esc); /* Setup interrupt handler */ err = bus_setup_intr(dev, esc->res[3], INTR_TYPE_BIO | INTR_MPSAFE, NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); if (err) { device_printf(dev, "Could not setup irq, " "%d\n", err); return (1); } /* Add USB device */ sc->sc_bus.bdev = device_add_child(dev, "usbus", -1); if (!sc->sc_bus.bdev) { device_printf(dev, "Could not add USB device\n"); err = bus_teardown_intr(dev, esc->res[3], sc->sc_intr_hdl); if (err) device_printf(dev, "Could not tear down irq," " %d\n", err); return (1); } device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); strlcpy(sc->sc_vendor, "Samsung", sizeof(sc->sc_vendor)); err = ehci_init(sc); if (!err) { sc->sc_flags |= EHCI_SCFLG_DONEINIT; err = device_probe_and_attach(sc->sc_bus.bdev); } else { device_printf(dev, "USB init failed err=%d\n", err); device_delete_child(dev, sc->sc_bus.bdev); sc->sc_bus.bdev = NULL; err = bus_teardown_intr(dev, esc->res[3], sc->sc_intr_hdl); if (err) device_printf(dev, "Could not tear down irq," " %d\n", err); return (1); } return (0); } static int exynos_ehci_detach(device_t dev) { struct exynos_ehci_softc *esc; ehci_softc_t *sc; int err; esc = device_get_softc(dev); sc = &esc->base; if (sc->sc_flags & EHCI_SCFLG_DONEINIT) return (0); /* * only call ehci_detach() after ehci_init() */ if (sc->sc_flags & EHCI_SCFLG_DONEINIT) { ehci_detach(sc); sc->sc_flags &= ~EHCI_SCFLG_DONEINIT; } /* * Disable interrupts that might have been switched on in * ehci_init. */ if (sc->sc_io_tag && sc->sc_io_hdl) bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, EHCI_USBINTR, 0); if (esc->res[3] && sc->sc_intr_hdl) { err = bus_teardown_intr(dev, esc->res[3], sc->sc_intr_hdl); if (err) { device_printf(dev, "Could not tear down irq," " %d\n", err); return (err); } sc->sc_intr_hdl = NULL; } if (sc->sc_bus.bdev) { device_delete_child(dev, sc->sc_bus.bdev); sc->sc_bus.bdev = NULL; } /* During module unload there are lots of children leftover */ device_delete_children(dev); bus_release_resources(dev, exynos_ehci_spec, esc->res); return (0); } Index: head/sys/arm/samsung/exynos/exynos5_pad.c =================================================================== --- head/sys/arm/samsung/exynos/exynos5_pad.c (revision 291404) +++ head/sys/arm/samsung/exynos/exynos5_pad.c (revision 291405) @@ -1,853 +1,856 @@ /*- * Copyright (c) 2014 Ruslan Bukin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Samsung Exynos 5 Pad Control * Chapter 4, Exynos 5 Dual User's Manual Public Rev 1.00 */ - +#ifdef USB_GLOBAL_INCLUDE_FILE +#include USB_GLOBAL_INCLUDE_FILE +#else #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "gpio_if.h" +#endif #include #include #define GPIO_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) #define GPIO_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT) #define MAX_PORTS 5 #define MAX_NGPIO 253 #define N_EXT_INTS 16 #define EXYNOS5250 1 #define EXYNOS5420 2 #define PIN_IN 0 #define PIN_OUT 1 #define READ4(_sc, _port, _reg) \ bus_space_read_4(_sc->bst[_port], _sc->bsh[_port], _reg) #define WRITE4(_sc, _port, _reg, _val) \ bus_space_write_4(_sc->bst[_port], _sc->bsh[_port], _reg, _val) /* * GPIO interface */ static device_t pad_get_bus(device_t); static int pad_pin_max(device_t, int *); static int pad_pin_getcaps(device_t, uint32_t, uint32_t *); static int pad_pin_getname(device_t, uint32_t, char *); static int pad_pin_getflags(device_t, uint32_t, uint32_t *); static int pad_pin_setflags(device_t, uint32_t, uint32_t); static int pad_pin_set(device_t, uint32_t, unsigned int); static int pad_pin_get(device_t, uint32_t, unsigned int *); static int pad_pin_toggle(device_t, uint32_t pin); struct gpio_bank { char *name; uint32_t port; uint32_t con; uint32_t ngpio; uint32_t ext_con; uint32_t ext_flt_con; uint32_t mask; uint32_t pend; }; struct pad_softc { struct resource *res[MAX_PORTS * 2]; bus_space_tag_t bst[MAX_PORTS]; bus_space_handle_t bsh[MAX_PORTS]; struct mtx sc_mtx; int gpio_npins; struct gpio_pin gpio_pins[MAX_NGPIO]; void *gpio_ih[MAX_PORTS]; device_t dev; device_t busdev; int model; struct resource_spec *pad_spec; struct gpio_bank *gpio_map; struct interrupt_entry *interrupt_table; int nports; }; struct pad_softc *gpio_sc; static struct resource_spec pad_spec_5250[] = { { SYS_RES_MEMORY, 0, RF_ACTIVE }, { SYS_RES_MEMORY, 1, RF_ACTIVE }, { SYS_RES_MEMORY, 2, RF_ACTIVE }, { SYS_RES_MEMORY, 3, RF_ACTIVE }, { SYS_RES_IRQ, 0, RF_ACTIVE }, { SYS_RES_IRQ, 1, RF_ACTIVE }, { SYS_RES_IRQ, 2, RF_ACTIVE }, { SYS_RES_IRQ, 3, RF_ACTIVE }, { -1, 0 } }; static struct resource_spec pad_spec_5420[] = { { SYS_RES_MEMORY, 0, RF_ACTIVE }, { SYS_RES_MEMORY, 1, RF_ACTIVE }, { SYS_RES_MEMORY, 2, RF_ACTIVE }, { SYS_RES_MEMORY, 3, RF_ACTIVE }, { SYS_RES_MEMORY, 4, RF_ACTIVE }, { SYS_RES_IRQ, 0, RF_ACTIVE }, { SYS_RES_IRQ, 1, RF_ACTIVE }, { SYS_RES_IRQ, 2, RF_ACTIVE }, { SYS_RES_IRQ, 3, RF_ACTIVE }, { SYS_RES_IRQ, 4, RF_ACTIVE }, { -1, 0 } }; static struct ofw_compat_data compat_data[] = { {"samsung,exynos5420-padctrl", EXYNOS5420}, {"samsung,exynos5250-padctrl", EXYNOS5250}, {NULL, 0} }; struct pad_intr { uint32_t enabled; void (*ih) (void *); void *ih_user; }; static struct pad_intr intr_map[MAX_NGPIO]; struct interrupt_entry { int gpio_number; char *combiner_source_name; }; struct interrupt_entry interrupt_table_5250[N_EXT_INTS] = { { 147, "EINT[15]" }, { 146, "EINT[14]" }, { 145, "EINT[13]" }, { 144, "EINT[12]" }, { 143, "EINT[11]" }, { 142, "EINT[10]" }, { 141, "EINT[9]" }, { 140, "EINT[8]" }, { 139, "EINT[7]" }, { 138, "EINT[6]" }, { 137, "EINT[5]" }, { 136, "EINT[4]" }, { 135, "EINT[3]" }, { 134, "EINT[2]" }, { 133, "EINT[1]" }, { 132, "EINT[0]" }, }; struct interrupt_entry interrupt_table_5420[N_EXT_INTS] = { { 23, "EINT[15]" }, { 22, "EINT[14]" }, { 21, "EINT[13]" }, { 20, "EINT[12]" }, { 19, "EINT[11]" }, { 18, "EINT[10]" }, { 17, "EINT[9]" }, { 16, "EINT[8]" }, { 15, "EINT[7]" }, { 14, "EINT[6]" }, { 13, "EINT[5]" }, { 12, "EINT[4]" }, { 11, "EINT[3]" }, { 10, "EINT[2]" }, { 9, "EINT[1]" }, { 8, "EINT[0]" }, }; /* * 253 multi-functional input/output ports */ static struct gpio_bank gpio_map_5250[] = { /* first 132 gpio */ { "gpa0", 0, 0x000, 8, 0x700, 0x800, 0x900, 0xA00 }, { "gpa1", 0, 0x020, 6, 0x704, 0x808, 0x904, 0xA04 }, { "gpa2", 0, 0x040, 8, 0x708, 0x810, 0x908, 0xA08 }, { "gpb0", 0, 0x060, 5, 0x70C, 0x818, 0x90C, 0xA0C }, { "gpb1", 0, 0x080, 5, 0x710, 0x820, 0x910, 0xA10 }, { "gpb2", 0, 0x0A0, 4, 0x714, 0x828, 0x914, 0xA14 }, { "gpb3", 0, 0x0C0, 4, 0x718, 0x830, 0x918, 0xA18 }, { "gpc0", 0, 0x0E0, 7, 0x71C, 0x838, 0x91C, 0xA1C }, { "gpc1", 0, 0x100, 4, 0x720, 0x840, 0x920, 0xA20 }, { "gpc2", 0, 0x120, 7, 0x724, 0x848, 0x924, 0xA24 }, { "gpc3", 0, 0x140, 7, 0x728, 0x850, 0x928, 0xA28 }, { "gpd0", 0, 0x160, 4, 0x72C, 0x858, 0x92C, 0xA2C }, { "gpd1", 0, 0x180, 8, 0x730, 0x860, 0x930, 0xA30 }, { "gpy0", 0, 0x1A0, 6, 0, 0, 0, 0 }, { "gpy1", 0, 0x1C0, 4, 0, 0, 0, 0 }, { "gpy2", 0, 0x1E0, 6, 0, 0, 0, 0 }, { "gpy3", 0, 0x200, 8, 0, 0, 0, 0 }, { "gpy4", 0, 0x220, 8, 0, 0, 0, 0 }, { "gpy5", 0, 0x240, 8, 0, 0, 0, 0 }, { "gpy6", 0, 0x260, 8, 0, 0, 0, 0 }, { "gpc4", 0, 0x2E0, 7, 0x734, 0x868, 0x934, 0xA34 }, /* next 32 */ { "gpx0", 0, 0xC00, 8, 0xE00, 0xE80, 0xF00, 0xF40 }, { "gpx1", 0, 0xC20, 8, 0xE04, 0xE88, 0xF04, 0xF44 }, { "gpx2", 0, 0xC40, 8, 0xE08, 0xE90, 0xF08, 0xF48 }, { "gpx3", 0, 0xC60, 8, 0xE0C, 0xE98, 0xF0C, 0xF4C }, { "gpe0", 1, 0x000, 8, 0x700, 0x800, 0x900, 0xA00 }, { "gpe1", 1, 0x020, 2, 0x704, 0x808, 0x904, 0xA04 }, { "gpf0", 1, 0x040, 4, 0x708, 0x810, 0x908, 0xA08 }, { "gpf1", 1, 0x060, 4, 0x70C, 0x818, 0x90C, 0xA0C }, { "gpg0", 1, 0x080, 8, 0x710, 0x820, 0x910, 0xA10 }, { "gpg1", 1, 0x0A0, 8, 0x714, 0x828, 0x914, 0xA14 }, { "gpg2", 1, 0x0C0, 2, 0x718, 0x830, 0x918, 0xA18 }, { "gph0", 1, 0x0E0, 4, 0x71C, 0x838, 0x91C, 0xA1C }, { "gph1", 1, 0x100, 8, 0x720, 0x840, 0x920, 0xA20 }, { "gpv0", 2, 0x000, 8, 0x700, 0x800, 0x900, 0xA00 }, { "gpv1", 2, 0x020, 8, 0x704, 0x808, 0x904, 0xA04 }, { "gpv2", 2, 0x060, 8, 0x708, 0x810, 0x908, 0xA08 }, { "gpv3", 2, 0x080, 8, 0x70C, 0x818, 0x90C, 0xA0C }, { "gpv4", 2, 0x0C0, 2, 0x710, 0x820, 0x910, 0xA10 }, { "gpz", 3, 0x000, 7, 0x700, 0x800, 0x900, 0xA00 }, { NULL, -1, -1, -1, -1, -1, -1, -1 }, }; static struct gpio_bank gpio_map_5420[] = { /* First 40 */ { "gpy7", 0, 0x000, 8, 0x700, 0x800, 0x900, 0xA00 }, { "gpx0", 0, 0xC00, 8, 0x704, 0xE80, 0xF00, 0xF40 }, { "gpx1", 0, 0xC20, 8, 0x708, 0xE88, 0xF04, 0xF44 }, { "gpx2", 0, 0xC40, 8, 0x70C, 0xE90, 0xF08, 0xF48 }, { "gpx3", 0, 0xC60, 8, 0x710, 0xE98, 0xF0C, 0xF4C }, /* Next 85 */ { "gpc0", 1, 0x000, 8, 0x700, 0x800, 0x900, 0xA00 }, { "gpc1", 1, 0x020, 8, 0x704, 0x808, 0x904, 0xA04 }, { "gpc2", 1, 0x040, 7, 0x708, 0x810, 0x908, 0xA08 }, { "gpc3", 1, 0x060, 4, 0x70C, 0x818, 0x90C, 0xA0C }, { "gpc4", 1, 0x080, 2, 0x710, 0x820, 0x910, 0xA10 }, { "gpd1", 1, 0x0A0, 8, 0x714, 0x828, 0x914, 0xA14 }, { "gpy0", 1, 0x0C0, 6, 0x718, 0x830, 0x918, 0xA18 }, { "gpy1", 1, 0x0E0, 4, 0x71C, 0x838, 0x91C, 0xA1C }, { "gpy2", 1, 0x100, 6, 0x720, 0x840, 0x920, 0xA20 }, { "gpy3", 1, 0x120, 8, 0x724, 0x848, 0x924, 0xA24 }, { "gpy4", 1, 0x140, 8, 0x728, 0x850, 0x928, 0xA28 }, { "gpy5", 1, 0x160, 8, 0x72C, 0x858, 0x92C, 0xA2C }, { "gpy6", 1, 0x180, 8, 0x730, 0x860, 0x930, 0xA30 }, /* Next 46 */ { "gpe0", 2, 0x000, 8, 0x700, 0x800, 0x900, 0xA00 }, { "gpe1", 2, 0x020, 2, 0x704, 0x808, 0x904, 0xA04 }, { "gpf0", 2, 0x040, 6, 0x708, 0x810, 0x908, 0xA08 }, { "gpf1", 2, 0x060, 8, 0x70C, 0x818, 0x90C, 0xA0C }, { "gpg0", 2, 0x080, 8, 0x710, 0x820, 0x910, 0xA10 }, { "gpg1", 2, 0x0A0, 8, 0x714, 0x828, 0x914, 0xA14 }, { "gpg2", 2, 0x0C0, 2, 0x718, 0x830, 0x918, 0xA18 }, { "gpj4", 2, 0x0E0, 4, 0x71C, 0x838, 0x91C, 0xA1C }, /* Next 54 */ { "gpa0", 3, 0x000, 8, 0x700, 0x800, 0x900, 0xA00 }, { "gpa1", 3, 0x020, 6, 0x704, 0x808, 0x904, 0xA04 }, { "gpa2", 3, 0x040, 8, 0x708, 0x810, 0x908, 0xA08 }, { "gpb0", 3, 0x060, 5, 0x70C, 0x818, 0x90C, 0xA0C }, { "gpb1", 3, 0x080, 5, 0x710, 0x820, 0x910, 0xA10 }, { "gpb2", 3, 0x0A0, 4, 0x714, 0x828, 0x914, 0xA14 }, { "gpb3", 3, 0x0C0, 8, 0x718, 0x830, 0x918, 0xA18 }, { "gpb4", 3, 0x0E0, 2, 0x71C, 0x838, 0x91C, 0xA1C }, { "gph0", 3, 0x100, 8, 0x720, 0x840, 0x920, 0xA20 }, /* Last 7 */ { "gpz", 4, 0x000, 7, 0x700, 0x800, 0x900, 0xA00 }, { NULL, -1, -1, -1, -1, -1, -1, -1 }, }; static int get_bank(struct pad_softc *sc, int gpio_number, struct gpio_bank *bank, int *pin_shift) { int ngpio; int i; int n; n = 0; for (i = 0; sc->gpio_map[i].ngpio != -1; i++) { ngpio = sc->gpio_map[i].ngpio; if ((n + ngpio) > gpio_number) { *bank = sc->gpio_map[i]; *pin_shift = (gpio_number - n); return (0); }; n += ngpio; }; return (-1); } static int port_intr(void *arg) { struct port_softc *sc; sc = arg; return (FILTER_HANDLED); } static void ext_intr(void *arg) { struct pad_softc *sc; void (*ih) (void *); void *ih_user; int ngpio; int found; int reg; int i,j; int n,k; sc = arg; n = 0; for (i = 0; sc->gpio_map[i].ngpio != -1; i++) { found = 0; ngpio = sc->gpio_map[i].ngpio; if (sc->gpio_map[i].pend == 0) { n += ngpio; continue; } reg = READ4(sc, sc->gpio_map[i].port, sc->gpio_map[i].pend); for (j = 0; j < ngpio; j++) { if (reg & (1 << j)) { found = 1; k = (n + j); if (intr_map[k].enabled == 1) { ih = intr_map[k].ih; ih_user = intr_map[k].ih_user; ih(ih_user); } } } if (found) { /* ACK */ WRITE4(sc, sc->gpio_map[i].port, sc->gpio_map[i].pend, reg); } n += ngpio; } } int pad_setup_intr(int gpio_number, void (*ih)(void *), void *ih_user) { struct interrupt_entry *entry; struct pad_intr *pad_irq; struct gpio_bank bank; struct pad_softc *sc; int pin_shift; int reg; int i; sc = gpio_sc; if (sc == NULL) { device_printf(sc->dev, "Error: pad is not attached\n"); return (-1); } if (get_bank(sc, gpio_number, &bank, &pin_shift) != 0) return (-1); entry = NULL; for (i = 0; i < N_EXT_INTS; i++) if (sc->interrupt_table[i].gpio_number == gpio_number) entry = &(sc->interrupt_table[i]); if (entry == NULL) { device_printf(sc->dev, "Cant find interrupt source for %d\n", gpio_number); return (-1); } #if 0 printf("Request interrupt name %s\n", entry->combiner_source_name); #endif pad_irq = &intr_map[gpio_number]; pad_irq->enabled = 1; pad_irq->ih = ih; pad_irq->ih_user = ih_user; /* Setup port as external interrupt source */ reg = READ4(sc, bank.port, bank.con); reg |= (0xf << (pin_shift * 4)); #if 0 printf("writing 0x%08x to 0x%08x\n", reg, bank.con); #endif WRITE4(sc, bank.port, bank.con, reg); /* * Configure interrupt pin * * 0x0 = Sets Low level * 0x1 = Sets High level * 0x2 = Triggers Falling edge * 0x3 = Triggers Rising edge * 0x4 = Triggers Both edge * * TODO: add parameter. For now configure as 0x0 */ reg = READ4(sc, bank.port, bank.ext_con); reg &= ~(0x7 << (pin_shift * 4)); WRITE4(sc, bank.port, bank.ext_con, reg); /* Unmask */ reg = READ4(sc, bank.port, bank.mask); reg &= ~(1 << pin_shift); WRITE4(sc, bank.port, bank.mask, reg); combiner_setup_intr(entry->combiner_source_name, ext_intr, sc); return (0); } static int pad_probe(device_t dev) { if (!ofw_bus_status_okay(dev)) return (ENXIO); if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) { device_set_desc(dev, "Exynos Pad Control"); return (BUS_PROBE_DEFAULT); } return (ENXIO); } static int pad_attach(device_t dev) { struct gpio_bank bank; struct pad_softc *sc; int pin_shift; int reg; int i; sc = device_get_softc(dev); mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); sc->model = ofw_bus_search_compatible(dev, compat_data)->ocd_data; switch (sc->model) { case EXYNOS5250: sc->pad_spec = pad_spec_5250; sc->gpio_map = gpio_map_5250; sc->interrupt_table = interrupt_table_5250; sc->gpio_npins = 253; sc->nports = 4; break; case EXYNOS5420: sc->pad_spec = pad_spec_5420; sc->gpio_map = gpio_map_5420; sc->interrupt_table = interrupt_table_5420; sc->gpio_npins = 232; sc->nports = 5; break; default: goto fail; }; if (bus_alloc_resources(dev, sc->pad_spec, sc->res)) { device_printf(dev, "could not allocate resources\n"); goto fail; } /* Memory interface */ for (i = 0; i < sc->nports; i++) { sc->bst[i] = rman_get_bustag(sc->res[i]); sc->bsh[i] = rman_get_bushandle(sc->res[i]); }; sc->dev = dev; gpio_sc = sc; for (i = 0; i < sc->nports; i++) { if ((bus_setup_intr(dev, sc->res[sc->nports + i], INTR_TYPE_BIO | INTR_MPSAFE, port_intr, NULL, sc, &sc->gpio_ih[i]))) { device_printf(dev, "ERROR: Unable to register interrupt handler\n"); goto fail; } } for (i = 0; i < sc->gpio_npins; i++) { sc->gpio_pins[i].gp_pin = i; sc->gpio_pins[i].gp_caps = DEFAULT_CAPS; if (get_bank(sc, i, &bank, &pin_shift) != 0) continue; pin_shift *= 4; reg = READ4(sc, bank.port, bank.con); if (reg & (PIN_OUT << pin_shift)) sc->gpio_pins[i].gp_flags = GPIO_PIN_OUTPUT; else sc->gpio_pins[i].gp_flags = GPIO_PIN_INPUT; /* TODO: add other pin statuses */ snprintf(sc->gpio_pins[i].gp_name, GPIOMAXNAME, "pad%d.%d", device_get_unit(dev), i); } sc->busdev = gpiobus_attach_bus(dev); if (sc->busdev == NULL) goto fail; return (0); fail: for (i = 0; i < sc->nports; i++) { if (sc->gpio_ih[i]) bus_teardown_intr(dev, sc->res[sc->nports + i], sc->gpio_ih[i]); } bus_release_resources(dev, sc->pad_spec, sc->res); mtx_destroy(&sc->sc_mtx); return (ENXIO); } static device_t pad_get_bus(device_t dev) { struct pad_softc *sc; sc = device_get_softc(dev); return (sc->busdev); } static int pad_pin_max(device_t dev, int *maxpin) { struct pad_softc *sc; sc = device_get_softc(dev); *maxpin = sc->gpio_npins - 1; return (0); } static int pad_pin_getname(device_t dev, uint32_t pin, char *name) { struct pad_softc *sc; int i; sc = device_get_softc(dev); for (i = 0; i < sc->gpio_npins; i++) { if (sc->gpio_pins[i].gp_pin == pin) break; } if (i >= sc->gpio_npins) return (EINVAL); GPIO_LOCK(sc); memcpy(name, sc->gpio_pins[i].gp_name, GPIOMAXNAME); GPIO_UNLOCK(sc); return (0); } static int pad_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps) { struct pad_softc *sc; int i; sc = device_get_softc(dev); for (i = 0; i < sc->gpio_npins; i++) { if (sc->gpio_pins[i].gp_pin == pin) break; } if (i >= sc->gpio_npins) return (EINVAL); GPIO_LOCK(sc); *caps = sc->gpio_pins[i].gp_caps; GPIO_UNLOCK(sc); return (0); } static int pad_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags) { struct pad_softc *sc; int i; sc = device_get_softc(dev); for (i = 0; i < sc->gpio_npins; i++) { if (sc->gpio_pins[i].gp_pin == pin) break; } if (i >= sc->gpio_npins) return (EINVAL); GPIO_LOCK(sc); *flags = sc->gpio_pins[i].gp_flags; GPIO_UNLOCK(sc); return (0); } static int pad_pin_get(device_t dev, uint32_t pin, unsigned int *val) { struct gpio_bank bank; struct pad_softc *sc; int pin_shift; int i; sc = device_get_softc(dev); for (i = 0; i < sc->gpio_npins; i++) { if (sc->gpio_pins[i].gp_pin == pin) break; } if (i >= sc->gpio_npins) return (EINVAL); if (get_bank(sc, pin, &bank, &pin_shift) != 0) return (EINVAL); GPIO_LOCK(sc); if (READ4(sc, bank.port, bank.con + 0x4) & (1 << pin_shift)) *val = 1; else *val = 0; GPIO_UNLOCK(sc); return (0); } static int pad_pin_toggle(device_t dev, uint32_t pin) { struct gpio_bank bank; struct pad_softc *sc; int pin_shift; int reg; int i; sc = device_get_softc(dev); for (i = 0; i < sc->gpio_npins; i++) { if (sc->gpio_pins[i].gp_pin == pin) break; } if (i >= sc->gpio_npins) return (EINVAL); if (get_bank(sc, pin, &bank, &pin_shift) != 0) return (EINVAL); GPIO_LOCK(sc); reg = READ4(sc, bank.port, bank.con + 0x4); if (reg & (1 << pin_shift)) reg &= ~(1 << pin_shift); else reg |= (1 << pin_shift); WRITE4(sc, bank.port, bank.con + 0x4, reg); GPIO_UNLOCK(sc); return (0); } static void pad_pin_configure(struct pad_softc *sc, struct gpio_pin *pin, unsigned int flags) { struct gpio_bank bank; int pin_shift; int reg; GPIO_LOCK(sc); /* * Manage input/output */ if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) { pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT); if (get_bank(sc, pin->gp_pin, &bank, &pin_shift) != 0) return; pin_shift *= 4; #if 0 printf("bank is 0x%08x pin_shift %d\n", bank.con, pin_shift); #endif if (flags & GPIO_PIN_OUTPUT) { pin->gp_flags |= GPIO_PIN_OUTPUT; reg = READ4(sc, bank.port, bank.con); reg &= ~(0xf << pin_shift); reg |= (PIN_OUT << pin_shift); WRITE4(sc, bank.port, bank.con, reg); } else { pin->gp_flags |= GPIO_PIN_INPUT; reg = READ4(sc, bank.port, bank.con); reg &= ~(0xf << pin_shift); WRITE4(sc, bank.port, bank.con, reg); } } GPIO_UNLOCK(sc); } static int pad_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) { struct pad_softc *sc; int i; sc = device_get_softc(dev); for (i = 0; i < sc->gpio_npins; i++) { if (sc->gpio_pins[i].gp_pin == pin) break; } if (i >= sc->gpio_npins) return (EINVAL); pad_pin_configure(sc, &sc->gpio_pins[i], flags); return (0); } static int pad_pin_set(device_t dev, uint32_t pin, unsigned int value) { struct pad_softc *sc; struct gpio_bank bank; int pin_shift; int reg; int i; sc = device_get_softc(dev); for (i = 0; i < sc->gpio_npins; i++) { if (sc->gpio_pins[i].gp_pin == pin) break; } if (i >= sc->gpio_npins) return (EINVAL); if (get_bank(sc, pin, &bank, &pin_shift) != 0) return (EINVAL); GPIO_LOCK(sc); reg = READ4(sc, bank.port, bank.con + 0x4); reg &= ~(PIN_OUT << pin_shift); if (value) reg |= (PIN_OUT << pin_shift); WRITE4(sc, bank.port, bank.con + 0x4, reg); GPIO_UNLOCK(sc); return (0); } static device_method_t pad_methods[] = { DEVMETHOD(device_probe, pad_probe), DEVMETHOD(device_attach, pad_attach), /* GPIO protocol */ DEVMETHOD(gpio_get_bus, pad_get_bus), DEVMETHOD(gpio_pin_max, pad_pin_max), DEVMETHOD(gpio_pin_getname, pad_pin_getname), DEVMETHOD(gpio_pin_getcaps, pad_pin_getcaps), DEVMETHOD(gpio_pin_getflags, pad_pin_getflags), DEVMETHOD(gpio_pin_get, pad_pin_get), DEVMETHOD(gpio_pin_toggle, pad_pin_toggle), DEVMETHOD(gpio_pin_setflags, pad_pin_setflags), DEVMETHOD(gpio_pin_set, pad_pin_set), { 0, 0 } }; static driver_t pad_driver = { "gpio", pad_methods, sizeof(struct pad_softc), }; static devclass_t pad_devclass; DRIVER_MODULE(pad, simplebus, pad_driver, pad_devclass, 0, 0); Index: head/sys/boot/kshim/bsd_global.h =================================================================== --- head/sys/boot/kshim/bsd_global.h (revision 291404) +++ head/sys/boot/kshim/bsd_global.h (revision 291405) @@ -1,65 +1,69 @@ /* $FreeBSD$ */ /*- * Copyright (c) 2013 Hans Petter Selasky. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef _BSD_GLOBAL_H_ #define _BSD_GLOBAL_H_ #include +#include + #define USB_DEBUG_VAR usb_debug #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include +#include extern struct usb_process usb_process[USB_PROC_MAX]; #endif /* _BSD_GLOBAL_H_ */ Index: head/sys/boot/kshim/bsd_kernel.c =================================================================== --- head/sys/boot/kshim/bsd_kernel.c (revision 291404) +++ head/sys/boot/kshim/bsd_kernel.c (revision 291405) @@ -1,1248 +1,1392 @@ /* $FreeBSD$ */ /*- * Copyright (c) 2013 Hans Petter Selasky. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include struct usb_process usb_process[USB_PROC_MAX]; static device_t usb_pci_root; /*------------------------------------------------------------------------* * Implementation of mutex API *------------------------------------------------------------------------*/ struct mtx Giant; +int (*bus_alloc_resource_any_cb)(struct resource *res, device_t dev, + int type, int *rid, unsigned int flags); +int (*ofw_bus_status_ok_cb)(device_t dev); +int (*ofw_bus_is_compatible_cb)(device_t dev, char *name); static void mtx_system_init(void *arg) { mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); } SYSINIT(mtx_system_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, mtx_system_init, NULL); +struct resource * +bus_alloc_resource_any(device_t dev, int type, int *rid, unsigned int flags) +{ + struct resource *res; + int ret = EINVAL; + + res = malloc(sizeof(*res), XXX, XXX); + if (res == NULL) + return (NULL); + + res->__r_i = malloc(sizeof(struct resource_i), XXX, XXX); + if (res->__r_i == NULL) { + free(res, XXX); + return (NULL); + } + + if (bus_alloc_resource_any_cb != NULL) + ret = (*bus_alloc_resource_any_cb)(res, dev, type, rid, flags); + if (ret == 0) + return (res); + + free(res->__r_i, XXX); + free(res, XXX); + return (NULL); +} + +int +bus_alloc_resources(device_t dev, struct resource_spec *rs, + struct resource **res) +{ + int i; + + for (i = 0; rs[i].type != -1; i++) + res[i] = NULL; + for (i = 0; rs[i].type != -1; i++) { + res[i] = bus_alloc_resource_any(dev, + rs[i].type, &rs[i].rid, rs[i].flags); + if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) { + bus_release_resources(dev, rs, res); + return (ENXIO); + } + } + return (0); +} + void +bus_release_resources(device_t dev, const struct resource_spec *rs, + struct resource **res) +{ + int i; + + for (i = 0; rs[i].type != -1; i++) + if (res[i] != NULL) { + bus_release_resource( + dev, rs[i].type, rs[i].rid, res[i]); + res[i] = NULL; + } +} + +int +bus_setup_intr(device_t dev, struct resource *r, int flags, + driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep) +{ + + dev->dev_irq_filter = filter; + dev->dev_irq_fn = handler; + dev->dev_irq_arg = arg; + + return (0); +} + +int +bus_teardown_intr(device_t dev, struct resource *r, void *cookie) +{ + + dev->dev_irq_filter = NULL; + dev->dev_irq_fn = NULL; + dev->dev_irq_arg = NULL; + + return (0); +} + +int +bus_release_resource(device_t dev, int type, int rid, struct resource *r) +{ + /* Resource releasing is not supported */ + return (EINVAL); +} + +int +bus_generic_attach(device_t dev) +{ + device_t child; + + TAILQ_FOREACH(child, &dev->dev_children, dev_link) { + device_probe_and_attach(child); + } + + return (0); +} + +bus_space_tag_t +rman_get_bustag(struct resource *r) +{ + + return (r->r_bustag); +} + +bus_space_handle_t +rman_get_bushandle(struct resource *r) +{ + + return (r->r_bushandle); +} + +u_long +rman_get_size(struct resource *r) +{ + + return (r->__r_i->r_end - r->__r_i->r_start + 1); +} + +int +ofw_bus_status_okay(device_t dev) +{ + if (ofw_bus_status_ok_cb == NULL) + return (0); + + return ((*ofw_bus_status_ok_cb)(dev)); +} + +int +ofw_bus_is_compatible(device_t dev, char *name) +{ + if (ofw_bus_is_compatible_cb == NULL) + return (0); + + return ((*ofw_bus_is_compatible_cb)(dev, name)); +} + +void mtx_init(struct mtx *mtx, const char *name, const char *type, int opt) { mtx->owned = 0; mtx->parent = mtx; } void mtx_lock(struct mtx *mtx) { mtx = mtx->parent; mtx->owned++; } void mtx_unlock(struct mtx *mtx) { mtx = mtx->parent; mtx->owned--; } int mtx_owned(struct mtx *mtx) { mtx = mtx->parent; return (mtx->owned != 0); } void mtx_destroy(struct mtx *mtx) { /* NOP */ } /*------------------------------------------------------------------------* * Implementation of shared/exclusive mutex API *------------------------------------------------------------------------*/ void sx_init_flags(struct sx *sx, const char *name, int flags) { sx->owned = 0; } void sx_destroy(struct sx *sx) { /* NOP */ } void sx_xlock(struct sx *sx) { sx->owned++; } void sx_xunlock(struct sx *sx) { sx->owned--; } int sx_xlocked(struct sx *sx) { return (sx->owned != 0); } /*------------------------------------------------------------------------* * Implementaiton of condition variable API *------------------------------------------------------------------------*/ void cv_init(struct cv *cv, const char *desc) { cv->sleeping = 0; } void cv_destroy(struct cv *cv) { /* NOP */ } void cv_wait(struct cv *cv, struct mtx *mtx) { cv_timedwait(cv, mtx, -1); } int cv_timedwait(struct cv *cv, struct mtx *mtx, int timo) { int start = ticks; int delta; int time = 0; if (cv->sleeping) return (EWOULDBLOCK); /* not allowed */ cv->sleeping = 1; while (cv->sleeping) { if (timo >= 0) { delta = ticks - start; if (delta >= timo || delta < 0) break; } mtx_unlock(mtx); usb_idle(); if (++time >= (1000000 / hz)) { time = 0; callout_process(1); } /* Sleep for 1 us */ delay(1); mtx_lock(mtx); } if (cv->sleeping) { cv->sleeping = 0; return (EWOULDBLOCK); /* not allowed */ } return (0); } void cv_signal(struct cv *cv) { cv->sleeping = 0; } void cv_broadcast(struct cv *cv) { cv->sleeping = 0; } /*------------------------------------------------------------------------* * Implementation of callout API *------------------------------------------------------------------------*/ static void callout_proc_msg(struct usb_proc_msg *); volatile int ticks = 0; static LIST_HEAD(, callout) head_callout = LIST_HEAD_INITIALIZER(&head_callout); static struct mtx mtx_callout; static struct usb_proc_msg callout_msg[2]; static void callout_system_init(void *arg) { mtx_init(&mtx_callout, "callout-mtx", NULL, MTX_DEF | MTX_RECURSE); callout_msg[0].pm_callback = &callout_proc_msg; callout_msg[1].pm_callback = &callout_proc_msg; } SYSINIT(callout_system_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, callout_system_init, NULL); static void callout_callback(struct callout *c) { mtx_lock(c->mtx); mtx_lock(&mtx_callout); if (c->entry.le_prev != NULL) { LIST_REMOVE(c, entry); c->entry.le_prev = NULL; } mtx_unlock(&mtx_callout); if (c->func) (c->func) (c->arg); if (!(c->flags & CALLOUT_RETURNUNLOCKED)) mtx_unlock(c->mtx); } void callout_process(int timeout) { ticks += timeout; usb_proc_msignal(usb_process + 2, &callout_msg[0], &callout_msg[1]); } static void callout_proc_msg(struct usb_proc_msg *pmsg) { struct callout *c; int delta; repeat: mtx_lock(&mtx_callout); LIST_FOREACH(c, &head_callout, entry) { delta = c->timeout - ticks; if (delta < 0) { mtx_unlock(&mtx_callout); callout_callback(c); goto repeat; } } mtx_unlock(&mtx_callout); } void callout_init_mtx(struct callout *c, struct mtx *mtx, int flags) { memset(c, 0, sizeof(*c)); if (mtx == NULL) mtx = &Giant; c->mtx = mtx; c->flags = (flags & CALLOUT_RETURNUNLOCKED); } void callout_reset(struct callout *c, int to_ticks, void (*func) (void *), void *arg) { callout_stop(c); c->func = func; c->arg = arg; c->timeout = ticks + to_ticks; mtx_lock(&mtx_callout); LIST_INSERT_HEAD(&head_callout, c, entry); mtx_unlock(&mtx_callout); } void callout_stop(struct callout *c) { mtx_lock(&mtx_callout); if (c->entry.le_prev != NULL) { LIST_REMOVE(c, entry); c->entry.le_prev = NULL; } mtx_unlock(&mtx_callout); c->func = NULL; c->arg = NULL; } void callout_drain(struct callout *c) { if (c->mtx == NULL) return; /* not initialised */ mtx_lock(c->mtx); callout_stop(c); mtx_unlock(c->mtx); } int callout_pending(struct callout *c) { int retval; mtx_lock(&mtx_callout); retval = (c->entry.le_prev != NULL); mtx_unlock(&mtx_callout); return (retval); } /*------------------------------------------------------------------------* * Implementation of device API *------------------------------------------------------------------------*/ static const char unknown_string[] = { "unknown" }; static TAILQ_HEAD(, module_data) module_head = TAILQ_HEAD_INITIALIZER(module_head); static uint8_t devclass_equal(const char *a, const char *b) { char ta, tb; if (a == b) return (1); while (1) { ta = *a; tb = *b; if (ta != tb) return (0); if (ta == 0) break; a++; b++; } return (1); } int bus_generic_resume(device_t dev) { return (0); } int bus_generic_shutdown(device_t dev) { return (0); } int bus_generic_suspend(device_t dev) { return (0); } int bus_generic_print_child(device_t dev, device_t child) { return (0); } void bus_generic_driver_added(device_t dev, driver_t *driver) { return; } device_t device_get_parent(device_t dev) { return (dev ? dev->dev_parent : NULL); } void device_set_interrupt(device_t dev, driver_filter_t *filter, driver_intr_t *fn, void *arg) { dev->dev_irq_filter = filter; dev->dev_irq_fn = fn; dev->dev_irq_arg = arg; } void device_run_interrupts(device_t parent) { device_t child; if (parent == NULL) return; TAILQ_FOREACH(child, &parent->dev_children, dev_link) { int status; if (child->dev_irq_filter != NULL) status = child->dev_irq_filter(child->dev_irq_arg); else status = FILTER_SCHEDULE_THREAD; if (status == FILTER_SCHEDULE_THREAD) { if (child->dev_irq_fn != NULL) (child->dev_irq_fn) (child->dev_irq_arg); } } } void device_set_ivars(device_t dev, void *ivars) { dev->dev_aux = ivars; } void * device_get_ivars(device_t dev) { return (dev ? dev->dev_aux : NULL); } int device_get_unit(device_t dev) { return (dev ? dev->dev_unit : 0); } int bus_generic_detach(device_t dev) { device_t child; int error; if (!dev->dev_attached) return (EBUSY); TAILQ_FOREACH(child, &dev->dev_children, dev_link) { if ((error = device_detach(child)) != 0) return (error); } return (0); } const char * device_get_nameunit(device_t dev) { if (dev && dev->dev_nameunit[0]) return (dev->dev_nameunit); return (unknown_string); } static uint8_t devclass_create(devclass_t *dc_pp) { if (dc_pp == NULL) { return (1); } if (dc_pp[0] == NULL) { dc_pp[0] = malloc(sizeof(**(dc_pp)), M_DEVBUF, M_WAITOK | M_ZERO); if (dc_pp[0] == NULL) { return (1); } } return (0); } static const struct module_data * devclass_find_create(const char *classname) { const struct module_data *mod; TAILQ_FOREACH(mod, &module_head, entry) { if (devclass_equal(mod->mod_name, classname)) { if (devclass_create(mod->devclass_pp)) { continue; } return (mod); } } return (NULL); } static uint8_t devclass_add_device(const struct module_data *mod, device_t dev) { device_t *pp_dev; device_t *end; uint8_t unit; pp_dev = mod->devclass_pp[0]->dev_list; end = pp_dev + DEVCLASS_MAXUNIT; unit = 0; while (pp_dev != end) { if (*pp_dev == NULL) { *pp_dev = dev; dev->dev_unit = unit; dev->dev_module = mod; snprintf(dev->dev_nameunit, sizeof(dev->dev_nameunit), "%s%d", device_get_name(dev), unit); return (0); } pp_dev++; unit++; } DPRINTF("Could not add device to devclass.\n"); return (1); } static void devclass_delete_device(const struct module_data *mod, device_t dev) { if (mod == NULL) { return; } mod->devclass_pp[0]->dev_list[dev->dev_unit] = NULL; dev->dev_module = NULL; } static device_t make_device(device_t parent, const char *name) { device_t dev = NULL; const struct module_data *mod = NULL; if (name) { mod = devclass_find_create(name); if (!mod) { DPRINTF("%s:%d:%s: can't find device " "class %s\n", __FILE__, __LINE__, __FUNCTION__, name); goto done; } } dev = malloc(sizeof(*dev), M_DEVBUF, M_WAITOK | M_ZERO); if (dev == NULL) goto done; dev->dev_parent = parent; TAILQ_INIT(&dev->dev_children); if (name) { dev->dev_fixed_class = 1; if (devclass_add_device(mod, dev)) { goto error; } } done: return (dev); error: if (dev) { free(dev, M_DEVBUF); } return (NULL); } device_t device_add_child(device_t dev, const char *name, int unit) { device_t child; if (unit != -1) { device_printf(dev, "Unit is not -1\n"); } child = make_device(dev, name); if (child == NULL) { device_printf(dev, "Could not add child '%s'\n", name); goto done; } if (dev == NULL) { /* no parent */ goto done; } TAILQ_INSERT_TAIL(&dev->dev_children, child, dev_link); done: return (child); } int device_delete_child(device_t dev, device_t child) { int error = 0; device_t grandchild; /* remove children first */ while ((grandchild = TAILQ_FIRST(&child->dev_children))) { error = device_delete_child(child, grandchild); if (error) { device_printf(dev, "Error deleting child!\n"); goto done; } } error = device_detach(child); if (error) goto done; devclass_delete_device(child->dev_module, child); if (dev != NULL) { /* remove child from parent */ TAILQ_REMOVE(&dev->dev_children, child, dev_link); } free(child, M_DEVBUF); done: return (error); } int device_delete_children(device_t dev) { device_t child; int error = 0; while ((child = TAILQ_FIRST(&dev->dev_children))) { error = device_delete_child(dev, child); if (error) { device_printf(dev, "Error deleting child!\n"); break; } } return (error); } void device_quiet(device_t dev) { dev->dev_quiet = 1; } const char * device_get_desc(device_t dev) { if (dev) return &(dev->dev_desc[0]); return (unknown_string); } static int default_method(void) { /* do nothing */ DPRINTF("Default method called\n"); return (0); } void * device_get_method(device_t dev, const char *what) { const struct device_method *mtod; mtod = dev->dev_module->driver->methods; while (mtod->func != NULL) { if (devclass_equal(mtod->desc, what)) { return (mtod->func); } mtod++; } return ((void *)&default_method); } const char * device_get_name(device_t dev) { if (dev == NULL) return (unknown_string); return (dev->dev_module->driver->name); } static int device_allocate_softc(device_t dev) { const struct module_data *mod; mod = dev->dev_module; if ((dev->dev_softc_alloc == 0) && (mod->driver->size != 0)) { dev->dev_sc = malloc(mod->driver->size, M_DEVBUF, M_WAITOK | M_ZERO); if (dev->dev_sc == NULL) return (ENOMEM); dev->dev_softc_alloc = 1; } return (0); } int device_probe_and_attach(device_t dev) { const struct module_data *mod; const char *bus_name_parent; bus_name_parent = device_get_name(device_get_parent(dev)); if (dev->dev_attached) return (0); /* fail-safe */ if (dev->dev_fixed_class) { mod = dev->dev_module; if (DEVICE_PROBE(dev) <= 0) { if (device_allocate_softc(dev) == 0) { if (DEVICE_ATTACH(dev) == 0) { /* success */ dev->dev_attached = 1; return (0); } } } device_detach(dev); goto error; } /* * Else find a module for our device, if any */ TAILQ_FOREACH(mod, &module_head, entry) { if (devclass_equal(mod->bus_name, bus_name_parent)) { if (devclass_create(mod->devclass_pp)) { continue; } if (devclass_add_device(mod, dev)) { continue; } if (DEVICE_PROBE(dev) <= 0) { if (device_allocate_softc(dev) == 0) { if (DEVICE_ATTACH(dev) == 0) { /* success */ dev->dev_attached = 1; return (0); } } } /* else try next driver */ device_detach(dev); } } error: return (ENODEV); } int device_detach(device_t dev) { const struct module_data *mod = dev->dev_module; int error; if (dev->dev_attached) { error = DEVICE_DETACH(dev); if (error) { return error; } dev->dev_attached = 0; } device_set_softc(dev, NULL); if (dev->dev_fixed_class == 0) devclass_delete_device(mod, dev); return (0); } void device_set_softc(device_t dev, void *softc) { if (dev->dev_softc_alloc) { free(dev->dev_sc, M_DEVBUF); dev->dev_sc = NULL; } dev->dev_sc = softc; dev->dev_softc_alloc = 0; } void * device_get_softc(device_t dev) { if (dev == NULL) return (NULL); return (dev->dev_sc); } int device_is_attached(device_t dev) { return (dev->dev_attached); } void device_set_desc(device_t dev, const char *desc) { snprintf(dev->dev_desc, sizeof(dev->dev_desc), "%s", desc); } void device_set_desc_copy(device_t dev, const char *desc) { device_set_desc(dev, desc); } void * devclass_get_softc(devclass_t dc, int unit) { return (device_get_softc(devclass_get_device(dc, unit))); } int devclass_get_maxunit(devclass_t dc) { int max_unit = 0; if (dc) { max_unit = DEVCLASS_MAXUNIT; while (max_unit--) { if (dc->dev_list[max_unit]) { break; } } max_unit++; } return (max_unit); } device_t devclass_get_device(devclass_t dc, int unit) { return (((unit < 0) || (unit >= DEVCLASS_MAXUNIT) || (dc == NULL)) ? NULL : dc->dev_list[unit]); } devclass_t devclass_find(const char *classname) { const struct module_data *mod; TAILQ_FOREACH(mod, &module_head, entry) { - if (devclass_equal(mod->mod_name, classname)) + if (devclass_equal(mod->driver->name, classname)) return (mod->devclass_pp[0]); } return (NULL); } void module_register(void *data) { struct module_data *mdata = data; TAILQ_INSERT_TAIL(&module_head, mdata, entry); } /*------------------------------------------------------------------------* * System startup *------------------------------------------------------------------------*/ static void sysinit_run(const void **ppdata) { const struct sysinit *psys; while ((psys = *ppdata) != NULL) { (psys->func) (psys->data); ppdata++; } } /*------------------------------------------------------------------------* * USB process API *------------------------------------------------------------------------*/ static int usb_do_process(struct usb_process *); static int usb_proc_level = -1; static struct mtx usb_proc_mtx; void usb_idle(void) { int old_level = usb_proc_level; int old_giant = Giant.owned; int worked; device_run_interrupts(usb_pci_root); do { worked = 0; Giant.owned = 0; while (++usb_proc_level < USB_PROC_MAX) worked |= usb_do_process(usb_process + usb_proc_level); usb_proc_level = old_level; Giant.owned = old_giant; } while (worked); } void usb_init(void) { sysinit_run(sysinit_data); } void usb_uninit(void) { sysinit_run(sysuninit_data); } static void usb_process_init_sub(struct usb_process *up) { TAILQ_INIT(&up->up_qhead); cv_init(&up->up_cv, "-"); cv_init(&up->up_drain, "usbdrain"); up->up_mtx = &usb_proc_mtx; } static void usb_process_init(void *arg) { uint8_t x; mtx_init(&usb_proc_mtx, "usb-proc-mtx", NULL, MTX_DEF | MTX_RECURSE); for (x = 0; x != USB_PROC_MAX; x++) usb_process_init_sub(&usb_process[x]); } SYSINIT(usb_process_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, usb_process_init, NULL); static int usb_do_process(struct usb_process *up) { struct usb_proc_msg *pm; int worked = 0; mtx_lock(&usb_proc_mtx); repeat: pm = TAILQ_FIRST(&up->up_qhead); if (pm != NULL) { worked = 1; (pm->pm_callback) (pm); if (pm == TAILQ_FIRST(&up->up_qhead)) { /* nothing changed */ TAILQ_REMOVE(&up->up_qhead, pm, pm_qentry); pm->pm_qentry.tqe_prev = NULL; } goto repeat; } mtx_unlock(&usb_proc_mtx); return (worked); } void * usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1) { struct usb_proc_msg *pm0 = _pm0; struct usb_proc_msg *pm1 = _pm1; struct usb_proc_msg *pm2; usb_size_t d; uint8_t t; t = 0; if (pm0->pm_qentry.tqe_prev) { t |= 1; } if (pm1->pm_qentry.tqe_prev) { t |= 2; } if (t == 0) { /* * No entries are queued. Queue "pm0" and use the existing * message number. */ pm2 = pm0; } else if (t == 1) { /* Check if we need to increment the message number. */ if (pm0->pm_num == up->up_msg_num) { up->up_msg_num++; } pm2 = pm1; } else if (t == 2) { /* Check if we need to increment the message number. */ if (pm1->pm_num == up->up_msg_num) { up->up_msg_num++; } pm2 = pm0; } else if (t == 3) { /* * Both entries are queued. Re-queue the entry closest to * the end. */ d = (pm1->pm_num - pm0->pm_num); /* Check sign after subtraction */ if (d & 0x80000000) { pm2 = pm0; } else { pm2 = pm1; } TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry); } else { pm2 = NULL; /* panic - should not happen */ } /* Put message last on queue */ pm2->pm_num = up->up_msg_num; TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry); return (pm2); } /*------------------------------------------------------------------------* * usb_proc_is_gone * * Return values: * 0: USB process is running * Else: USB process is tearing down *------------------------------------------------------------------------*/ uint8_t usb_proc_is_gone(struct usb_process *up) { return (0); } /*------------------------------------------------------------------------* * usb_proc_mwait * * This function will return when the USB process message pointed to * by "pm" is no longer on a queue. This function must be called * having "usb_proc_mtx" locked. *------------------------------------------------------------------------*/ void usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1) { struct usb_proc_msg *pm0 = _pm0; struct usb_proc_msg *pm1 = _pm1; /* Just remove the messages from the queue. */ if (pm0->pm_qentry.tqe_prev) { TAILQ_REMOVE(&up->up_qhead, pm0, pm_qentry); pm0->pm_qentry.tqe_prev = NULL; } if (pm1->pm_qentry.tqe_prev) { TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry); pm1->pm_qentry.tqe_prev = NULL; } } /*------------------------------------------------------------------------* * SYSTEM attach *------------------------------------------------------------------------*/ #ifdef USB_PCI_PROBE_LIST static device_method_t pci_methods[] = { DEVMETHOD_END }; static driver_t pci_driver = { .name = "pci", .methods = pci_methods, }; static devclass_t pci_devclass; DRIVER_MODULE(pci, pci, pci_driver, pci_devclass, 0, 0); static const char *usb_pci_devices[] = { USB_PCI_PROBE_LIST }; #define USB_PCI_USB_MAX (sizeof(usb_pci_devices) / sizeof(void *)) static device_t usb_pci_dev[USB_PCI_USB_MAX]; static void usb_pci_mod_load(void *arg) { uint32_t x; usb_pci_root = device_add_child(NULL, "pci", -1); if (usb_pci_root == NULL) return; for (x = 0; x != USB_PCI_USB_MAX; x++) { usb_pci_dev[x] = device_add_child(usb_pci_root, usb_pci_devices[x], -1); if (usb_pci_dev[x] == NULL) continue; if (device_probe_and_attach(usb_pci_dev[x])) { device_printf(usb_pci_dev[x], "WARNING: Probe and attach failed!\n"); } } } SYSINIT(usb_pci_mod_load, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_load, 0); static void usb_pci_mod_unload(void *arg) { uint32_t x; for (x = 0; x != USB_PCI_USB_MAX; x++) { if (usb_pci_dev[x]) { device_detach(usb_pci_dev[x]); device_delete_child(usb_pci_root, usb_pci_dev[x]); } } if (usb_pci_root) device_delete_child(NULL, usb_pci_root); } SYSUNINIT(usb_pci_mod_unload, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_unload, 0); #endif /*------------------------------------------------------------------------* * MALLOC API *------------------------------------------------------------------------*/ #ifndef HAVE_MALLOC #define USB_POOL_ALIGN 8 static uint8_t usb_pool[USB_POOL_SIZE] __aligned(USB_POOL_ALIGN); static uint32_t usb_pool_rem = USB_POOL_SIZE; static uint32_t usb_pool_entries; struct malloc_hdr { TAILQ_ENTRY(malloc_hdr) entry; uint32_t size; } __aligned(USB_POOL_ALIGN); static TAILQ_HEAD(, malloc_hdr) malloc_head = TAILQ_HEAD_INITIALIZER(malloc_head); void * usb_malloc(unsigned long size) { struct malloc_hdr *hdr; size = (size + USB_POOL_ALIGN - 1) & ~(USB_POOL_ALIGN - 1); size += sizeof(struct malloc_hdr); TAILQ_FOREACH(hdr, &malloc_head, entry) { if (hdr->size == size) break; } if (hdr) { DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n", (int)usb_pool_entries, (int)usb_pool_rem, (int)size); TAILQ_REMOVE(&malloc_head, hdr, entry); memset(hdr + 1, 0, hdr->size - sizeof(*hdr)); return (hdr + 1); } if (usb_pool_rem >= size) { hdr = (void *)(usb_pool + USB_POOL_SIZE - usb_pool_rem); hdr->size = size; usb_pool_rem -= size; usb_pool_entries++; DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n", (int)usb_pool_entries, (int)usb_pool_rem, (int)size); memset(hdr + 1, 0, hdr->size - sizeof(*hdr)); return (hdr + 1); } return (NULL); } void usb_free(void *arg) { struct malloc_hdr *hdr; if (arg == NULL) return; hdr = arg; hdr--; TAILQ_INSERT_TAIL(&malloc_head, hdr, entry); } #endif char * usb_strdup(const char *str) { char *tmp; int len; len = 1 + strlen(str); tmp = malloc(len,XXX,XXX); if (tmp == NULL) return (NULL); memcpy(tmp, str, len); return (tmp); } Index: head/sys/boot/kshim/bsd_kernel.h =================================================================== --- head/sys/boot/kshim/bsd_kernel.h (revision 291404) +++ head/sys/boot/kshim/bsd_kernel.h (revision 291405) @@ -1,501 +1,582 @@ /* $FreeBSD$ */ /*- * Copyright (c) 2011 Hans Petter Selasky. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef _BSD_KERNEL_H_ #define _BSD_KERNEL_H_ #define _KERNEL #undef __FreeBSD_version #define __FreeBSD_version 1100000 #include #include #include #define isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) #define isdigit(x) ((x) >= '0' && (x) <= '9') #define panic(...) do { printf("USB PANIC: " __VA_ARGS__); while (1) ; } while (0) #define rebooting 0 #define M_USB 0 #define M_USBDEV 0 #define USB_PROC_MAX 3 #define USB_BUS_GIANT_PROC(bus) (usb_process + 2) #define USB_BUS_NON_GIANT_BULK_PROC(bus) (usb_process + 2) #define USB_BUS_NON_GIANT_ISOC_PROC(bus) (usb_process + 2) #define USB_BUS_EXPLORE_PROC(bus) (usb_process + 0) #define USB_BUS_CONTROL_XFER_PROC(bus) (usb_process + 1) #define SYSCTL_DECL(...) #define SYSCTL_NODE(name,...) struct { } name __used #define SYSCTL_INT(...) #define TUNABLE_INT(...) #define MALLOC_DECLARE(...) #define MALLOC_DEFINE(...) #define EVENTHANDLER_DECLARE(...) #define EVENTHANDLER_INVOKE(...) #define KASSERT(...) #define SCHEDULER_STOPPED(x) (0) #define PI_SWI(...) (0) #define UNIQ_NAME(x) x #define UNIQ_NAME_STR(x) #x #define DEVCLASS_MAXUNIT 32 #define MOD_LOAD 1 #define MOD_UNLOAD 2 #define DEVMETHOD(what,func) { #what, (void *)&func } #define DEVMETHOD_END {0,0} #define DRIVER_MODULE(name, busname, driver, devclass, evh, arg) \ static struct module_data bsd_##name##_##busname##_driver_mod = { \ evh, arg, #busname, #name, #busname "/" #name, \ &driver, &devclass, { 0, 0 } }; \ SYSINIT(bsd_##name##_##busname##_driver_mod, SI_SUB_DRIVERS, \ SI_ORDER_MIDDLE, module_register, \ &bsd_##name##_##busname##_driver_mod) #define SYSINIT(uniq, subs, order, _func, _data) \ const struct sysinit UNIQ_NAME(sysinit_##uniq) = { \ .func = (_func), \ .data = __DECONST(void *, _data) \ }; \ SYSINIT_ENTRY(uniq##_entry, "sysinit", (subs), \ (order), "const struct sysinit", \ UNIQ_NAME_STR(sysinit_##uniq), "SYSINIT") #define SYSUNINIT(uniq, subs, order, _func, _data) \ const struct sysinit UNIQ_NAME(sysuninit_##uniq) = { \ .func = (_func), \ .data = __DECONST(void *, _data) \ }; \ SYSINIT_ENTRY(uniq##_entry, "sysuninit", (subs), \ (order), "const struct sysuninit", \ UNIQ_NAME_STR(sysuninit_##uniq), "SYSUNINIT") #define MODULE_DEPEND(...) #define MODULE_VERSION(...) #define NULL ((void *)0) #define BUS_SPACE_BARRIER_READ 0x01 #define BUS_SPACE_BARRIER_WRITE 0x02 #define hz 1000 #undef PAGE_SIZE #define PAGE_SIZE 4096 #undef MIN #define MIN(a,b) (((a) < (b)) ? (a) : (b)) #undef MAX #define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define MTX_DEF 0 #define MTX_SPIN 0 #define MTX_RECURSE 0 #define SX_DUPOK 0 #define SX_NOWITNESS 0 #define WITNESS_WARN(...) #define cold 0 #define BUS_PROBE_GENERIC 0 +#define BUS_PROBE_DEFAULT (-20) #define CALLOUT_RETURNUNLOCKED 0x1 #undef ffs #define ffs(x) __builtin_ffs(x) #undef va_list #define va_list __builtin_va_list #undef va_size #define va_size(type) __builtin_va_size(type) #undef va_start #define va_start(ap, last) __builtin_va_start(ap, last) #undef va_end #define va_end(ap) __builtin_va_end(ap) #undef va_arg #define va_arg(ap, type) __builtin_va_arg((ap), type) #define DEVICE_ATTACH(dev, ...) \ (((device_attach_t *)(device_get_method(dev, "device_attach")))(dev,## __VA_ARGS__)) #define DEVICE_DETACH(dev, ...) \ (((device_detach_t *)(device_get_method(dev, "device_detach")))(dev,## __VA_ARGS__)) #define DEVICE_PROBE(dev, ...) \ (((device_probe_t *)(device_get_method(dev, "device_probe")))(dev,## __VA_ARGS__)) #define DEVICE_RESUME(dev, ...) \ (((device_resume_t *)(device_get_method(dev, "device_resume")))(dev,## __VA_ARGS__)) #define DEVICE_SHUTDOWN(dev, ...) \ (((device_shutdown_t *)(device_get_method(dev, "device_shutdown")))(dev,## __VA_ARGS__)) #define DEVICE_SUSPEND(dev, ...) \ (((device_suspend_t *)(device_get_method(dev, "device_suspend")))(dev,## __VA_ARGS__)) #define USB_HANDLE_REQUEST(dev, ...) \ (((usb_handle_request_t *)(device_get_method(dev, "usb_handle_request")))(dev,## __VA_ARGS__)) #define USB_TAKE_CONTROLLER(dev, ...) \ (((usb_take_controller_t *)(device_get_method(dev, "usb_take_controller")))(dev,## __VA_ARGS__)) +#define GPIO_PIN_SET(dev, ...) \ + (((gpio_pin_set_t *)(device_get_method(dev, "gpio_pin_set")))(dev,## __VA_ARGS__)) +#define GPIO_PIN_SETFLAGS(dev, ...) \ + (((gpio_pin_setflags_t *)(device_get_method(dev, "gpio_pin_setflags")))(dev,## __VA_ARGS__)) enum { SI_SUB_DUMMY = 0x0000000, SI_SUB_LOCK = 0x1B00000, SI_SUB_KLD = 0x2000000, SI_SUB_DRIVERS = 0x3100000, SI_SUB_PSEUDO = 0x7000000, SI_SUB_KICK_SCHEDULER = 0xa000000, SI_SUB_RUN_SCHEDULER = 0xfffffff }; enum { SI_ORDER_FIRST = 0x0000000, SI_ORDER_SECOND = 0x0000001, SI_ORDER_THIRD = 0x0000002, SI_ORDER_FOURTH = 0x0000003, SI_ORDER_MIDDLE = 0x1000000, SI_ORDER_ANY = 0xfffffff /* last */ }; struct uio; struct thread; struct malloc_type; struct usb_process; #ifndef HAVE_STANDARD_DEFS #define _UINT8_T_DECLARED typedef unsigned char uint8_t; #define _INT8_T_DECLARED typedef signed char int8_t; #define _UINT16_T_DECLARED typedef unsigned short uint16_t; #define _INT16_T_DECLARED typedef signed short int16_t; #define _UINT32_T_DECLARED typedef unsigned int uint32_t; #define _INT32_T_DECLARED typedef signed int int32_t; #define _UINT64_T_DECLARED typedef unsigned long long uint64_t; #define _INT16_T_DECLARED typedef signed long long int64_t; typedef uint16_t uid_t; typedef uint16_t gid_t; typedef uint16_t mode_t; typedef uint8_t *caddr_t; #define _UINTPTR_T_DECLARED typedef unsigned long uintptr_t; #define _SIZE_T_DECLARED typedef unsigned long size_t; typedef unsigned long u_long; #endif typedef unsigned long bus_addr_t; typedef unsigned long bus_size_t; typedef void *bus_dmamap_t; typedef void *bus_dma_tag_t; typedef void *bus_space_tag_t; typedef uint8_t *bus_space_handle_t; /* SYSINIT API */ #include struct sysinit { void (*func) (void *arg); void *data; }; /* MUTEX API */ struct mtx { int owned; struct mtx *parent; }; #define mtx_assert(...) do { } while (0) void mtx_init(struct mtx *, const char *, const char *, int); void mtx_lock(struct mtx *); void mtx_unlock(struct mtx *); #define mtx_lock_spin(x) mtx_lock(x) #define mtx_unlock_spin(x) mtx_unlock(x) int mtx_owned(struct mtx *); void mtx_destroy(struct mtx *); extern struct mtx Giant; /* SX API */ struct sx { int owned; }; #define sx_assert(...) do { } while (0) #define sx_init(...) sx_init_flags(__VA_ARGS__, 0) void sx_init_flags(struct sx *, const char *, int); void sx_destroy(struct sx *); void sx_xlock(struct sx *); void sx_xunlock(struct sx *); int sx_xlocked(struct sx *); /* CONDVAR API */ struct cv { int sleeping; }; void cv_init(struct cv *, const char *desc); void cv_destroy(struct cv *); void cv_wait(struct cv *, struct mtx *); int cv_timedwait(struct cv *, struct mtx *, int); void cv_signal(struct cv *); void cv_broadcast(struct cv *); /* CALLOUT API */ typedef void callout_fn_t (void *); extern volatile int ticks; struct callout { LIST_ENTRY(callout) entry; callout_fn_t *func; void *arg; struct mtx *mtx; int flags; int timeout; }; void callout_init_mtx(struct callout *, struct mtx *, int); void callout_reset(struct callout *, int, callout_fn_t *, void *); void callout_stop(struct callout *); void callout_drain(struct callout *); int callout_pending(struct callout *); void callout_process(int timeout); /* DEVICE API */ struct driver; struct devclass; struct device; struct module; struct module_data; typedef struct driver driver_t; typedef struct devclass *devclass_t; typedef struct device *device_t; typedef void (driver_intr_t)(void *arg); typedef int (driver_filter_t)(void *arg); #define FILTER_STRAY 0x01 #define FILTER_HANDLED 0x02 #define FILTER_SCHEDULE_THREAD 0x04 typedef int device_attach_t (device_t dev); typedef int device_detach_t (device_t dev); typedef int device_resume_t (device_t dev); typedef int device_shutdown_t (device_t dev); typedef int device_probe_t (device_t dev); typedef int device_suspend_t (device_t dev); +typedef int gpio_pin_set_t (device_t dev, uint32_t, unsigned int); +typedef int gpio_pin_setflags_t (device_t dev, uint32_t, uint32_t); typedef int bus_child_location_str_t (device_t parent, device_t child, char *buf, size_t buflen); typedef int bus_child_pnpinfo_str_t (device_t parent, device_t child, char *buf, size_t buflen); typedef void bus_driver_added_t (device_t dev, driver_t *driver); struct device_method { const char *desc; void *const func; }; typedef struct device_method device_method_t; struct device { TAILQ_HEAD(device_list, device) dev_children; TAILQ_ENTRY(device) dev_link; struct device *dev_parent; const struct module_data *dev_module; void *dev_sc; void *dev_aux; driver_filter_t *dev_irq_filter; driver_intr_t *dev_irq_fn; void *dev_irq_arg; uint16_t dev_unit; char dev_nameunit[64]; char dev_desc[64]; uint8_t dev_res_alloc:1; uint8_t dev_quiet:1; uint8_t dev_softc_set:1; uint8_t dev_softc_alloc:1; uint8_t dev_attached:1; uint8_t dev_fixed_class:1; uint8_t dev_unit_manual:1; }; struct devclass { device_t dev_list[DEVCLASS_MAXUNIT]; }; struct driver { const char *name; const struct device_method *methods; uint32_t size; }; struct module_data { int (*callback) (struct module *, int, void *arg); void *arg; const char *bus_name; const char *mod_name; const char *long_name; const struct driver *driver; struct devclass **devclass_pp; TAILQ_ENTRY(module_data) entry; }; device_t device_get_parent(device_t dev); void *device_get_method(device_t dev, const char *what); const char *device_get_name(device_t dev); const char *device_get_nameunit(device_t dev); #define device_printf(dev, fmt,...) \ printf("%s: " fmt, device_get_nameunit(dev),## __VA_ARGS__) device_t device_add_child(device_t dev, const char *name, int unit); void device_quiet(device_t dev); void device_set_interrupt(device_t dev, driver_filter_t *, driver_intr_t *, void *); void device_run_interrupts(device_t parent); void device_set_ivars(device_t dev, void *ivars); void *device_get_ivars(device_t dev); const char *device_get_desc(device_t dev); int device_probe_and_attach(device_t dev); int device_detach(device_t dev); void *device_get_softc(device_t dev); void device_set_softc(device_t dev, void *softc); int device_delete_child(device_t dev, device_t child); int device_delete_children(device_t dev); int device_is_attached(device_t dev); void device_set_desc(device_t dev, const char *desc); void device_set_desc_copy(device_t dev, const char *desc); int device_get_unit(device_t dev); void *devclass_get_softc(devclass_t dc, int unit); int devclass_get_maxunit(devclass_t dc); device_t devclass_get_device(devclass_t dc, int unit); devclass_t devclass_find(const char *classname); #define bus_get_dma_tag(...) (NULL) int bus_generic_detach(device_t dev); int bus_generic_resume(device_t dev); int bus_generic_shutdown(device_t dev); int bus_generic_suspend(device_t dev); int bus_generic_print_child(device_t dev, device_t child); void bus_generic_driver_added(device_t dev, driver_t *driver); int bus_space_subregion(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp); /* BUS SPACE API */ void bus_space_write_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t data); void bus_space_write_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t data); void bus_space_write_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t data); uint8_t bus_space_read_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset); uint16_t bus_space_read_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset); uint32_t bus_space_read_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset); void bus_space_read_multi_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t *datap, bus_size_t count); void bus_space_read_multi_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t *datap, bus_size_t count); void bus_space_read_multi_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t *datap, bus_size_t count); void bus_space_write_multi_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t *datap, bus_size_t count); void bus_space_write_multi_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t *datap, bus_size_t count); void bus_space_write_multi_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t *datap, bus_size_t count); void bus_space_read_region_1(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint8_t *datap, bus_size_t count); void bus_space_write_region_1(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint8_t *datap, bus_size_t count); void bus_space_read_region_4(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint32_t *datap, bus_size_t count); void bus_space_write_region_4(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint32_t *datap, bus_size_t count); void bus_space_barrier(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, bus_size_t length, int flags); void module_register(void *); /* LIB-C */ void *memset(void *, int, size_t len); void *memcpy(void *, const void *, size_t len); int printf(const char *,...) __printflike(1, 2); int snprintf(char *restrict str, size_t size, const char *restrict format,...) __printflike(3, 4); size_t strlen(const char *s); /* MALLOC API */ #undef malloc #define malloc(s,x,f) usb_malloc(s) void *usb_malloc(size_t); #undef free #define free(p,x) usb_free(p) void usb_free(void *); #define strdup(p,x) usb_strdup(p) char *usb_strdup(const char *str); /* ENDIANNESS */ #ifndef HAVE_ENDIAN_DEFS /* Assume little endian */ #define htole64(x) ((uint64_t)(x)) #define le64toh(x) ((uint64_t)(x)) #define htole32(x) ((uint32_t)(x)) #define le32toh(x) ((uint32_t)(x)) #define htole16(x) ((uint16_t)(x)) #define le16toh(x) ((uint16_t)(x)) #define be32toh(x) ((uint32_t)(x)) #define htobe32(x) ((uint32_t)(x)) #else #include #endif /* USB */ typedef int usb_handle_request_t (device_t dev, const void *req, void **pptr, uint16_t *plen, uint16_t offset, uint8_t *pstate); typedef int usb_take_controller_t (device_t dev); void usb_idle(void); void usb_init(void); void usb_uninit(void); /* set some defaults */ #ifndef USB_POOL_SIZE #define USB_POOL_SIZE (1024*1024) /* 1 MByte */ #endif int pause(const char *, int); void DELAY(unsigned int); /* OTHER */ struct selinfo { }; /* SYSTEM STARTUP API */ extern const void *sysinit_data[]; extern const void *sysuninit_data[]; + +/* Resources */ + +enum intr_type { + INTR_TYPE_TTY = 1, + INTR_TYPE_BIO = 2, + INTR_TYPE_NET = 4, + INTR_TYPE_CAM = 8, + INTR_TYPE_MISC = 16, + INTR_TYPE_CLK = 32, + INTR_TYPE_AV = 64, + INTR_EXCL = 256, /* exclusive interrupt */ + INTR_MPSAFE = 512, /* this interrupt is SMP safe */ + INTR_ENTROPY = 1024, /* this interrupt provides entropy */ + INTR_MD1 = 4096, /* flag reserved for MD use */ + INTR_MD2 = 8192, /* flag reserved for MD use */ + INTR_MD3 = 16384, /* flag reserved for MD use */ + INTR_MD4 = 32768 /* flag reserved for MD use */ +}; + +struct resource_i { + u_long r_start; /* index of the first entry in this resource */ + u_long r_end; /* index of the last entry (inclusive) */ +}; + +struct resource { + struct resource_i *__r_i; + bus_space_tag_t r_bustag; /* bus_space tag */ + bus_space_handle_t r_bushandle; /* bus_space handle */ +}; + +struct resource_spec { + int type; + int rid; + int flags; +}; + +#define SYS_RES_IRQ 1 /* interrupt lines */ +#define SYS_RES_DRQ 2 /* isa dma lines */ +#define SYS_RES_MEMORY 3 /* i/o memory */ +#define SYS_RES_IOPORT 4 /* i/o ports */ + +#define RF_ALLOCATED 0x0001 /* resource has been reserved */ +#define RF_ACTIVE 0x0002 /* resource allocation has been activated */ +#define RF_SHAREABLE 0x0004 /* resource permits contemporaneous sharing */ +#define RF_SPARE1 0x0008 +#define RF_SPARE2 0x0010 +#define RF_FIRSTSHARE 0x0020 /* first in sharing list */ +#define RF_PREFETCHABLE 0x0040 /* resource is prefetchable */ +#define RF_OPTIONAL 0x0080 /* for bus_alloc_resources() */ + +int bus_alloc_resources(device_t, struct resource_spec *, struct resource **); +int bus_release_resource(device_t, int, int, struct resource *); +void bus_release_resources(device_t, const struct resource_spec *, + struct resource **); +struct resource *bus_alloc_resource_any(device_t, int, int *, unsigned int); +int bus_generic_attach(device_t); +bus_space_tag_t rman_get_bustag(struct resource *); +bus_space_handle_t rman_get_bushandle(struct resource *); +u_long rman_get_size(struct resource *); +int bus_setup_intr(device_t, struct resource *, int, driver_filter_t, + driver_intr_t, void *, void **); +int bus_teardown_intr(device_t, struct resource *, void *); +int ofw_bus_status_okay(device_t); +int ofw_bus_is_compatible(device_t, char *); + +extern int (*bus_alloc_resource_any_cb)(struct resource *res, device_t dev, + int type, int *rid, unsigned int flags); +extern int (*ofw_bus_status_ok_cb)(device_t dev); +extern int (*ofw_bus_is_compatible_cb)(device_t dev, char *name); + +#ifndef strlcpy +#define strlcpy(d,s,n) snprintf((d),(n),"%s",(s)) +#endif #endif /* _BSD_KERNEL_H_ */ Index: head/sys/boot/usb/usbcore.mk =================================================================== --- head/sys/boot/usb/usbcore.mk (revision 291404) +++ head/sys/boot/usb/usbcore.mk (revision 291405) @@ -1,160 +1,175 @@ # # $FreeBSD$ # # Copyright (c) 2013 Hans Petter Selasky. # Copyright (c) 2014 SRI International # All rights reserved. # # This software was developed by SRI International and the University of # Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237 # ("CTSRD"), as part of the DARPA CRASH research programme. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # USBCOREDIR:= ${.PARSEDIR} S=${USBCOREDIR}/../.. +MACHDEP_DIRS= + +.if defined(HAVE_EXYNOS_EHCI) +MACHDEP_DIRS+= ${S}/arm/samsung/exynos +.endif + .PATH: \ ${USBCOREDIR} \ ${USBCOREDIR}/storage \ ${S}/dev/usb \ ${S}/dev/usb/controller \ ${S}/dev/usb/serial \ ${S}/dev/usb/storage \ - ${S}/dev/usb/template + ${S}/dev/usb/template \ + ${MACHDEP_DIRS} .undef S USB_POOL_SIZE?= 131072 CFLAGS+= -DUSB_MSCTEST_BULK_SIZE=65536 CFLAGS+= -DUSB_POOL_SIZE=${USB_POOL_SIZE} # # BUSDMA implementation # SRCS+= usb_busdma_loader.c # # USB controller drivers # KSRCS+= usb_controller.c .if defined(HAVE_AT91DCI) CFLAGS += -DUSB_PCI_PROBE_LIST="\"at91dci\"" KSRCS+= at91dci.c .endif .if defined(HAVE_ATMEGADCI) CFLAGS += -DUSB_PCI_PROBE_LIST="\"atmegadci\"" KSRCS+= atmegadci.c .endif .if defined(HAVE_AVR32DCI) CFLAGS += -DUSB_PCI_PROBE_LIST="\"avr32dci\"" KSRCS+= avr32dci.c .endif .if defined(HAVE_DWCOTG) CFLAGS += -DUSB_PCI_PROBE_LIST="\"dwcotg\"" KSRCS+= dwcotg.c .endif .if defined(HAVE_MUSBOTG) CFLAGS += -DUSB_PCI_PROBE_LIST="\"musbotg\"" KSRCS+= musbotg.c .endif .if defined(HAVE_EHCI) CFLAGS += -DUSB_PCI_PROBE_LIST="\"ehci\"" KSRCS+= ehci.c +.endif + +.if defined(HAVE_EXYNOS_EHCI) +CFLAGS += -DUSB_PCI_PROBE_LIST="\"combiner\", \"pad\", \"ehci\"" +KSRCS+= ehci.c +KSRCS+= exynos5_combiner.c +KSRCS+= exynos5_pad.c +KSRCS+= exynos5_ehci.c .endif .if defined(HAVE_OHCI) CFLAGS += -DUSB_PCI_PROBE_LIST="\"ohci\"" KSRCS+= ohci.c .endif .if defined(HAVE_UHCI) CFLAGS += -DUSB_PCI_PROBE_LIST="\"uhci\"" KSRCS+= uhci.c .endif .if defined(HAVE_XHCI) CFLAGS += -DUSB_PCI_PROBE_LIST="\"xhci\"" KSRCS+= xhci.c .endif .if defined(HAVE_USS820DCI) CFLAGS += -DUSB_PCI_PROBE_LIST="\"uss820dci\"" KSRCS+= uss820dci.c .endif .if defined(HAVE_SAF1761OTG) CFLAGS += -DUSB_PCI_PROBE_LIST="\"saf1761otg\"" CFLAGS += -DUSB_PCI_MEMORY_ADDRESS=0x900000007f100000ULL CFLAGS += -DUSB_PCI_MEMORY_SIZE=0x40000U KSRCS+= saf1761_otg.c KSRCS+= saf1761_otg_boot.c .endif # # USB core and templates # KSRCS+= usb_core.c KSRCS+= usb_debug.c KSRCS+= usb_device.c KSRCS+= usb_dynamic.c KSRCS+= usb_error.c KSRCS+= usb_handle_request.c KSRCS+= usb_hid.c KSRCS+= usb_hub.c KSRCS+= usb_lookup.c KSRCS+= usb_msctest.c KSRCS+= usb_parse.c KSRCS+= usb_request.c KSRCS+= usb_transfer.c KSRCS+= usb_util.c KSRCS+= usb_template.c KSRCS+= usb_template_cdce.c KSRCS+= usb_template_msc.c KSRCS+= usb_template_mtp.c KSRCS+= usb_template_modem.c KSRCS+= usb_template_mouse.c KSRCS+= usb_template_kbd.c KSRCS+= usb_template_audio.c KSRCS+= usb_template_phone.c KSRCS+= usb_template_serialnet.c KSRCS+= usb_template_midi.c # # USB mass storage support # SRCS+= umass_common.c .if defined(HAVE_UMASS_LOADER) CFLAGS+= -I${.CURDIR}/../common SRCS+= umass_loader.c .endif