xref: /freebsd/sys/arm/mv/armada38x/pmsu.c (revision eccfee6e)
1 /*-
2  * Copyright (c) 2015 Semihalf.
3  * Copyright (c) 2015 Stormshield.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/rman.h>
35 #include <sys/types.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/resource.h>
39 
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 
43 #include <machine/fdt.h>
44 #include <machine/smp.h>
45 
46 #include <dev/ofw/ofw_bus_subr.h>
47 
48 #include <arm/mv/mvreg.h>
49 
50 #include "pmsu.h"
51 
52 static struct resource_spec pmsu_spec[] = {
53 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
54 	{ -1, 0 }
55 };
56 
57 struct pmsu_softc {
58 	device_t	dev;
59 	struct resource	*res;
60 };
61 
62 static int pmsu_probe(device_t dev);
63 static int pmsu_attach(device_t dev);
64 static int pmsu_detach(device_t dev);
65 
66 static device_method_t pmsu_methods[] = {
67 	DEVMETHOD(device_probe,		pmsu_probe),
68 	DEVMETHOD(device_attach,	pmsu_attach),
69 	DEVMETHOD(device_detach,	pmsu_detach),
70 
71 	{ 0, 0 }
72 };
73 
74 static driver_t pmsu_driver = {
75 	"pmsu",
76 	pmsu_methods,
77 	sizeof(struct pmsu_softc)
78 };
79 
80 static devclass_t pmsu_devclass;
81 
82 DRIVER_MODULE(pmsu, simplebus, pmsu_driver, pmsu_devclass, 0, 0);
83 DRIVER_MODULE(pmsu, ofwbus, pmsu_driver, pmsu_devclass, 0, 0);
84 
85 static int
86 pmsu_probe(device_t dev)
87 {
88 
89 	if (!ofw_bus_status_okay(dev))
90 		return (ENXIO);
91 
92 	if (!ofw_bus_is_compatible(dev, "marvell,armada-380-pmsu"))
93 		return (ENXIO);
94 
95 	device_set_desc(dev, "Power Management Service Unit");
96 
97 	return (BUS_PROBE_DEFAULT);
98 }
99 
100 static int
101 pmsu_attach(device_t dev)
102 {
103 	struct pmsu_softc *sc;
104 	int err;
105 
106 	sc = device_get_softc(dev);
107 	sc->dev = dev;
108 
109 	err = bus_alloc_resources(dev, pmsu_spec, &sc->res);
110 	if (err != 0) {
111 		device_printf(dev, "could not allocate resources\n");
112 		return (ENXIO);
113 	}
114 
115 	return (0);
116 }
117 
118 static int
119 pmsu_detach(device_t dev)
120 {
121 	struct pmsu_softc *sc;
122 
123 	sc = device_get_softc(dev);
124 
125 	bus_release_resources(dev, pmsu_spec, &sc->res);
126 
127 	return (0);
128 }
129 
130 #ifdef SMP
131 int
132 pmsu_boot_secondary_cpu(void)
133 {
134 	bus_space_handle_t vaddr;
135 	int rv;
136 
137 	rv = bus_space_map(fdtbus_bs_tag, (bus_addr_t)MV_PMSU_BASE, MV_PMSU_REGS_LEN,
138 	    0, &vaddr);
139 	if (rv != 0)
140 		return (rv);
141 
142 	/* Boot cpu1 */
143 	bus_space_write_4(fdtbus_bs_tag, vaddr, PMSU_BOOT_ADDR_REDIRECT_OFFSET(1),
144 	    pmap_kextract((vm_offset_t)mpentry));
145 
146 	cpu_idcache_wbinv_all();
147 	cpu_l2cache_wbinv_all();
148 	armv7_sev();
149 
150 	bus_space_unmap(fdtbus_bs_tag, vaddr, MV_PMSU_REGS_LEN);
151 
152 	return (0);
153 }
154 #endif
155