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