1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/bus.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/systm.h>
39
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42
43 #include <dev/bhnd/bhnd.h>
44
45 #include "bhnd_pmureg.h"
46 #include "bhnd_pmuvar.h"
47
48 /*
49 * PMU core driver.
50 */
51
52 /* Supported device identifiers */
53 static const struct bhnd_device bhnd_pmucore_devices[] = {
54 BHND_DEVICE(BCM, PMU, NULL, NULL),
55
56 BHND_DEVICE_END
57 };
58
59 static int
bhnd_pmu_core_probe(device_t dev)60 bhnd_pmu_core_probe(device_t dev)
61 {
62 const struct bhnd_device *id;
63 int error;
64
65 id = bhnd_device_lookup(dev, bhnd_pmucore_devices,
66 sizeof(bhnd_pmucore_devices[0]));
67 if (id == NULL)
68 return (ENXIO);
69
70 /* Delegate to common driver implementation */
71 if ((error = bhnd_pmu_probe(dev)) > 0)
72 return (error);
73
74 bhnd_set_default_core_desc(dev);
75 return (BUS_PROBE_DEFAULT);
76 }
77
78 static int
bhnd_pmu_core_attach(device_t dev)79 bhnd_pmu_core_attach(device_t dev)
80 {
81 struct bhnd_pmu_softc *sc;
82 struct bhnd_resource *res;
83 int error;
84 int rid;
85
86 sc = device_get_softc(dev);
87
88 /* Allocate register block */
89 rid = 0;
90 res = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
91 if (res == NULL) {
92 device_printf(dev, "failed to allocate resources\n");
93 return (ENXIO);
94 }
95
96 /* Allocate our per-core PMU state */
97 if ((error = bhnd_alloc_pmu(dev))) {
98 device_printf(sc->dev, "failed to allocate PMU state: %d\n",
99 error);
100
101 return (error);
102 }
103
104 /* Delegate to common driver implementation */
105 if ((error = bhnd_pmu_attach(dev, res))) {
106 bhnd_release_resource(dev, SYS_RES_MEMORY, rid, res);
107 return (error);
108 }
109
110 sc->rid = rid;
111 return (0);
112 }
113
114 static int
bhnd_pmu_core_detach(device_t dev)115 bhnd_pmu_core_detach(device_t dev)
116 {
117 struct bhnd_pmu_softc *sc;
118 int error;
119
120 sc = device_get_softc(dev);
121
122 /* Delegate to common driver implementation */
123 if ((error = bhnd_pmu_detach(dev)))
124 return (error);
125
126 bhnd_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
127 return (0);
128 }
129
130 static device_method_t bhnd_pmucore_methods[] = {
131 /* Device interface */
132 DEVMETHOD(device_probe, bhnd_pmu_core_probe),
133 DEVMETHOD(device_attach, bhnd_pmu_core_attach),
134 DEVMETHOD(device_detach, bhnd_pmu_core_detach),
135
136 DEVMETHOD_END
137 };
138
139 DEFINE_CLASS_1(bhnd_pmu, bhnd_pmucore_driver, bhnd_pmucore_methods,
140 sizeof(struct bhnd_pmu_softc), bhnd_pmu_driver);
141 EARLY_DRIVER_MODULE(bhnd_pmu, bhnd, bhnd_pmucore_driver, NULL, NULL,
142 BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
143
144 MODULE_DEPEND(bhnd_pmu_core, bhnd_pmu, 1, 1, 1);
145 MODULE_VERSION(bhnd_pmu_core, 1);
146