xref: /freebsd/sys/dev/bhnd/cores/pmu/bhnd_pmu_core.c (revision ca942ab4b2b665e5375cc72afc4930d3b33b14f9)
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 
85 	sc = device_get_softc(dev);
86 
87 	/* Allocate register block */
88 	res = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, 0, RF_ACTIVE);
89 	if (res == NULL) {
90 		device_printf(dev, "failed to allocate resources\n");
91 		return (ENXIO);
92 	}
93 
94 	/* Allocate our per-core PMU state */
95 	if ((error = bhnd_alloc_pmu(dev))) {
96 		device_printf(sc->dev, "failed to allocate PMU state: %d\n",
97 		    error);
98 
99 		return (error);
100 	}
101 
102 	/* Delegate to common driver implementation */
103 	if ((error = bhnd_pmu_attach(dev, res))) {
104 		bhnd_release_resource(dev, res);
105 		return (error);
106 	}
107 
108 	return (0);
109 }
110 
111 static int
bhnd_pmu_core_detach(device_t dev)112 bhnd_pmu_core_detach(device_t dev)
113 {
114 	struct bhnd_pmu_softc	*sc;
115 	int			 error;
116 
117 	sc = device_get_softc(dev);
118 
119 	/* Delegate to common driver implementation */
120 	if ((error = bhnd_pmu_detach(dev)))
121 		return (error);
122 
123 	bhnd_release_resource(dev, sc->res);
124 	return (0);
125 }
126 
127 static device_method_t bhnd_pmucore_methods[] = {
128 	/* Device interface */
129 	DEVMETHOD(device_probe,		bhnd_pmu_core_probe),
130 	DEVMETHOD(device_attach,	bhnd_pmu_core_attach),
131 	DEVMETHOD(device_detach,	bhnd_pmu_core_detach),
132 
133 	DEVMETHOD_END
134 };
135 
136 DEFINE_CLASS_1(bhnd_pmu, bhnd_pmucore_driver, bhnd_pmucore_methods,
137     sizeof(struct bhnd_pmu_softc), bhnd_pmu_driver);
138 EARLY_DRIVER_MODULE(bhnd_pmu, bhnd, bhnd_pmucore_driver, NULL, NULL,
139     BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
140 
141 MODULE_DEPEND(bhnd_pmu_core, bhnd_pmu, 1, 1, 1);
142 MODULE_VERSION(bhnd_pmu_core, 1);
143