xref: /freebsd/sys/dev/bhnd/cores/pmu/bhnd_pmu_core.c (revision d6b92ffa)
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 #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 	/* Delegate to common driver implementation */
98 	if ((error = bhnd_pmu_attach(dev, res))) {
99 		bhnd_release_resource(dev, SYS_RES_MEMORY, rid, res);
100 		return (error);
101 	}
102 
103 	sc->rid = rid;
104 	return (0);
105 }
106 
107 static int
108 bhnd_pmu_core_detach(device_t dev)
109 {
110 	struct bhnd_pmu_softc	*sc;
111 	int			 error;
112 
113 	sc = device_get_softc(dev);
114 
115 	/* Delegate to common driver implementation */
116 	if ((error = bhnd_pmu_detach(dev)))
117 		return (error);
118 
119 	bhnd_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
120 	return (0);
121 }
122 
123 static device_method_t bhnd_pmucore_methods[] = {
124 	/* Device interface */
125 	DEVMETHOD(device_probe,		bhnd_pmu_core_probe),
126 	DEVMETHOD(device_attach,	bhnd_pmu_core_attach),
127 	DEVMETHOD(device_detach,	bhnd_pmu_core_detach),
128 
129 	DEVMETHOD_END
130 };
131 
132 DEFINE_CLASS_1(bhnd_pmu, bhnd_pmucore_driver, bhnd_pmucore_methods,
133     sizeof(struct bhnd_pmu_softc), bhnd_pmu_driver);
134 EARLY_DRIVER_MODULE(bhnd_pmu, bhnd, bhnd_pmucore_driver, bhnd_pmu_devclass,
135     NULL, NULL, BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
136 
137 MODULE_DEPEND(bhnd_pmu_core, bhnd_pmu, 1, 1, 1);
138 MODULE_VERSION(bhnd_pmu_core, 1);
139