xref: /freebsd/sys/arm/mv/armada38x/pmsu.c (revision 0957b409a90fd597c1e9124cbaf3edd2b488f4ac)
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 #include <sys/systm.h>
40 
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 
44 #include <machine/cpu.h>
45 #include <machine/fdt.h>
46 #include <machine/smp.h>
47 
48 #include <dev/ofw/ofw_bus_subr.h>
49 
50 #include <arm/mv/mvreg.h>
51 
52 #include "pmsu.h"
53 
54 static struct resource_spec pmsu_spec[] = {
55 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
56 	{ -1, 0 }
57 };
58 
59 struct pmsu_softc {
60 	device_t	dev;
61 	struct resource	*res;
62 };
63 
64 static int pmsu_probe(device_t dev);
65 static int pmsu_attach(device_t dev);
66 static int pmsu_detach(device_t dev);
67 
68 static device_method_t pmsu_methods[] = {
69 	DEVMETHOD(device_probe,		pmsu_probe),
70 	DEVMETHOD(device_attach,	pmsu_attach),
71 	DEVMETHOD(device_detach,	pmsu_detach),
72 
73 	{ 0, 0 }
74 };
75 
76 static driver_t pmsu_driver = {
77 	"pmsu",
78 	pmsu_methods,
79 	sizeof(struct pmsu_softc)
80 };
81 
82 static devclass_t pmsu_devclass;
83 
84 DRIVER_MODULE(pmsu, simplebus, pmsu_driver, pmsu_devclass, 0, 0);
85 DRIVER_MODULE(pmsu, ofwbus, pmsu_driver, pmsu_devclass, 0, 0);
86 
87 static int
88 pmsu_probe(device_t dev)
89 {
90 
91 	if (!ofw_bus_status_okay(dev))
92 		return (ENXIO);
93 
94 	if (!ofw_bus_is_compatible(dev, "marvell,armada-380-pmsu"))
95 		return (ENXIO);
96 
97 	device_set_desc(dev, "Power Management Service Unit");
98 
99 	return (BUS_PROBE_DEFAULT);
100 }
101 
102 static int
103 pmsu_attach(device_t dev)
104 {
105 	struct pmsu_softc *sc;
106 	int err;
107 
108 	sc = device_get_softc(dev);
109 	sc->dev = dev;
110 
111 	err = bus_alloc_resources(dev, pmsu_spec, &sc->res);
112 	if (err != 0) {
113 		device_printf(dev, "could not allocate resources\n");
114 		return (ENXIO);
115 	}
116 
117 	return (0);
118 }
119 
120 static int
121 pmsu_detach(device_t dev)
122 {
123 	struct pmsu_softc *sc;
124 
125 	sc = device_get_softc(dev);
126 
127 	bus_release_resources(dev, pmsu_spec, &sc->res);
128 
129 	return (0);
130 }
131 
132 #ifdef SMP
133 int
134 pmsu_boot_secondary_cpu(void)
135 {
136 	bus_space_handle_t vaddr;
137 	int rv;
138 
139 	rv = bus_space_map(fdtbus_bs_tag, (bus_addr_t)MV_PMSU_BASE, MV_PMSU_REGS_LEN,
140 	    0, &vaddr);
141 	if (rv != 0)
142 		return (rv);
143 
144 	/* Boot cpu1 */
145 	bus_space_write_4(fdtbus_bs_tag, vaddr, PMSU_BOOT_ADDR_REDIRECT_OFFSET(1),
146 	    pmap_kextract((vm_offset_t)mpentry));
147 
148 	dcache_wbinv_poc_all();
149 	dsb();
150 	sev();
151 
152 	bus_space_unmap(fdtbus_bs_tag, vaddr, MV_PMSU_REGS_LEN);
153 
154 	return (0);
155 }
156 #endif
157