1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer, 12 * without modification. 13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 15 * redistribution must be conditioned upon including a substantially 16 * similar Disclaimer requirement for further binary redistribution. 17 * 18 * NO WARRANTY 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 29 * THE POSSIBILITY OF SUCH DAMAGES. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/lock.h> 38 #include <sys/bus.h> 39 #include <sys/module.h> 40 #include <sys/mutex.h> 41 #include <sys/systm.h> 42 43 #include <machine/bus.h> 44 #include <machine/resource.h> 45 46 #include <dev/bhnd/bhnd.h> 47 48 #include "bhnd_pmureg.h" 49 #include "bhnd_pmuvar.h" 50 51 /* 52 * PMU core driver. 53 */ 54 55 /* Supported device identifiers */ 56 static const struct bhnd_device bhnd_pmucore_devices[] = { 57 BHND_DEVICE(BCM, PMU, NULL, NULL), 58 59 BHND_DEVICE_END 60 }; 61 62 static int 63 bhnd_pmu_core_probe(device_t dev) 64 { 65 const struct bhnd_device *id; 66 int error; 67 68 id = bhnd_device_lookup(dev, bhnd_pmucore_devices, 69 sizeof(bhnd_pmucore_devices[0])); 70 if (id == NULL) 71 return (ENXIO); 72 73 /* Delegate to common driver implementation */ 74 if ((error = bhnd_pmu_probe(dev)) > 0) 75 return (error); 76 77 bhnd_set_default_core_desc(dev); 78 return (BUS_PROBE_DEFAULT); 79 } 80 81 static int 82 bhnd_pmu_core_attach(device_t dev) 83 { 84 struct bhnd_pmu_softc *sc; 85 struct bhnd_resource *res; 86 int error; 87 int rid; 88 89 sc = device_get_softc(dev); 90 91 /* Allocate register block */ 92 rid = 0; 93 res = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 94 if (res == NULL) { 95 device_printf(dev, "failed to allocate resources\n"); 96 return (ENXIO); 97 } 98 99 /* Allocate our per-core PMU state */ 100 if ((error = bhnd_alloc_pmu(dev))) { 101 device_printf(sc->dev, "failed to allocate PMU state: %d\n", 102 error); 103 104 return (error); 105 } 106 107 /* Delegate to common driver implementation */ 108 if ((error = bhnd_pmu_attach(dev, res))) { 109 bhnd_release_resource(dev, SYS_RES_MEMORY, rid, res); 110 return (error); 111 } 112 113 sc->rid = rid; 114 return (0); 115 } 116 117 static int 118 bhnd_pmu_core_detach(device_t dev) 119 { 120 struct bhnd_pmu_softc *sc; 121 int error; 122 123 sc = device_get_softc(dev); 124 125 /* Delegate to common driver implementation */ 126 if ((error = bhnd_pmu_detach(dev))) 127 return (error); 128 129 bhnd_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res); 130 return (0); 131 } 132 133 static device_method_t bhnd_pmucore_methods[] = { 134 /* Device interface */ 135 DEVMETHOD(device_probe, bhnd_pmu_core_probe), 136 DEVMETHOD(device_attach, bhnd_pmu_core_attach), 137 DEVMETHOD(device_detach, bhnd_pmu_core_detach), 138 139 DEVMETHOD_END 140 }; 141 142 DEFINE_CLASS_1(bhnd_pmu, bhnd_pmucore_driver, bhnd_pmucore_methods, 143 sizeof(struct bhnd_pmu_softc), bhnd_pmu_driver); 144 EARLY_DRIVER_MODULE(bhnd_pmu, bhnd, bhnd_pmucore_driver, NULL, NULL, 145 BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE); 146 147 MODULE_DEPEND(bhnd_pmu_core, bhnd_pmu, 1, 1, 1); 148 MODULE_VERSION(bhnd_pmu_core, 1); 149