1 /*- 2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * BHNDB PCI SPROM driver. 35 * 36 * Provides support for early PCI bridge cores that vend SPROM CSRs 37 * via PCI configuration space. 38 */ 39 40 #include <sys/param.h> 41 #include <sys/kernel.h> 42 #include <sys/bus.h> 43 #include <sys/limits.h> 44 #include <sys/malloc.h> 45 #include <sys/module.h> 46 #include <sys/systm.h> 47 48 #include <dev/pci/pcireg.h> 49 #include <dev/pci/pcivar.h> 50 51 #include <dev/bhnd/bhnd.h> 52 #include <dev/bhnd/cores/pci/bhnd_pci_hostbvar.h> 53 #include <dev/bhnd/nvram/bhnd_spromvar.h> 54 55 #include "bhnd_nvram_if.h" 56 #include "bhndb_pcireg.h" 57 #include "bhndb_pcivar.h" 58 59 struct bhndb_pci_sprom_softc { 60 device_t dev; 61 struct bhnd_resource *sprom_res; /**< SPROM resource */ 62 int sprom_rid; /**< SPROM RID */ 63 struct bhnd_sprom shadow; /**< SPROM shadow */ 64 struct mtx mtx; /**< SPROM shadow mutex */ 65 }; 66 67 #define SPROM_LOCK_INIT(sc) \ 68 mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev), \ 69 "BHND PCI SPROM lock", MTX_DEF) 70 #define SPROM_LOCK(sc) mtx_lock(&(sc)->mtx) 71 #define SPROM_UNLOCK(sc) mtx_unlock(&(sc)->mtx) 72 #define SPROM_LOCK_ASSERT(sc, what) mtx_assert(&(sc)->mtx, what) 73 #define SPROM_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx) 74 75 static int 76 bhndb_pci_sprom_probe(device_t dev) 77 { 78 device_t bridge, bus; 79 80 /* Our parent must be a PCI-BHND bridge with an attached bhnd bus */ 81 bridge = device_get_parent(dev); 82 if (device_get_driver(bridge) != &bhndb_pci_driver) 83 return (ENXIO); 84 85 bus = device_find_child(bridge, devclass_get_name(bhnd_devclass), 0); 86 if (bus == NULL) 87 return (ENXIO); 88 89 /* Found */ 90 device_set_desc(dev, "PCI-BHNDB SPROM/OTP"); 91 if (!bootverbose) 92 device_quiet(dev); 93 94 /* Refuse wildcard attachments */ 95 return (BUS_PROBE_NOWILDCARD); 96 } 97 98 static int 99 bhndb_pci_sprom_attach(device_t dev) 100 { 101 struct bhndb_pci_sprom_softc *sc; 102 int error; 103 104 sc = device_get_softc(dev); 105 sc->dev = dev; 106 107 /* Allocate SPROM resource */ 108 sc->sprom_rid = 0; 109 sc->sprom_res = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, 110 &sc->sprom_rid, RF_ACTIVE); 111 if (sc->sprom_res == NULL) { 112 device_printf(dev, "failed to allocate resources\n"); 113 return (ENXIO); 114 } 115 116 /* Initialize SPROM shadow */ 117 if ((error = bhnd_sprom_init(&sc->shadow, sc->sprom_res, 0))) { 118 device_printf(dev, "unrecognized SPROM format\n"); 119 goto failed; 120 } 121 122 /* Initialize mutex */ 123 SPROM_LOCK_INIT(sc); 124 125 return (0); 126 127 failed: 128 bhnd_release_resource(dev, SYS_RES_MEMORY, sc->sprom_rid, 129 sc->sprom_res); 130 return (error); 131 } 132 133 static int 134 bhndb_pci_sprom_resume(device_t dev) 135 { 136 return (0); 137 } 138 139 static int 140 bhndb_pci_sprom_suspend(device_t dev) 141 { 142 return (0); 143 } 144 145 static int 146 bhndb_pci_sprom_detach(device_t dev) 147 { 148 struct bhndb_pci_sprom_softc *sc; 149 150 sc = device_get_softc(dev); 151 152 bhnd_release_resource(dev, SYS_RES_MEMORY, sc->sprom_rid, 153 sc->sprom_res); 154 bhnd_sprom_fini(&sc->shadow); 155 SPROM_LOCK_DESTROY(sc); 156 157 return (0); 158 } 159 160 static int 161 bhndb_pci_sprom_getvar(device_t dev, const char *name, void *buf, size_t *len) 162 { 163 struct bhndb_pci_sprom_softc *sc; 164 int error; 165 166 sc = device_get_softc(dev); 167 168 SPROM_LOCK(sc); 169 error = bhnd_sprom_getvar(&sc->shadow, name, buf, len); 170 SPROM_UNLOCK(sc); 171 172 return (error); 173 } 174 175 static int 176 bhndb_pci_sprom_setvar(device_t dev, const char *name, const void *buf, 177 size_t len) 178 { 179 struct bhndb_pci_sprom_softc *sc; 180 int error; 181 182 sc = device_get_softc(dev); 183 184 SPROM_LOCK(sc); 185 error = bhnd_sprom_setvar(&sc->shadow, name, buf, len); 186 SPROM_UNLOCK(sc); 187 188 return (error); 189 } 190 191 static device_method_t bhndb_pci_sprom_methods[] = { 192 /* Device interface */ 193 DEVMETHOD(device_probe, bhndb_pci_sprom_probe), 194 DEVMETHOD(device_attach, bhndb_pci_sprom_attach), 195 DEVMETHOD(device_resume, bhndb_pci_sprom_resume), 196 DEVMETHOD(device_suspend, bhndb_pci_sprom_suspend), 197 DEVMETHOD(device_detach, bhndb_pci_sprom_detach), 198 199 /* NVRAM interface */ 200 DEVMETHOD(bhnd_nvram_getvar, bhndb_pci_sprom_getvar), 201 DEVMETHOD(bhnd_nvram_setvar, bhndb_pci_sprom_setvar), 202 203 DEVMETHOD_END 204 }; 205 206 DEFINE_CLASS_0(bhnd_nvram, bhndb_pci_sprom_driver, bhndb_pci_sprom_methods, sizeof(struct bhndb_pci_sprom_softc)); 207 208 DRIVER_MODULE(bhndb_pci_sprom, bhndb, bhndb_pci_sprom_driver, bhnd_nvram_devclass, NULL, NULL); 209 MODULE_DEPEND(bhndb_pci_sprom, bhnd, 1, 1, 1); 210 MODULE_VERSION(bhndb_pci_sprom, 1); 211