xref: /freebsd/sys/dev/hwreset/hwreset_array.c (revision 1f469a9fc498c3d406ef7c4e347232678f49da0a)
1*1f469a9fSEmmanuel Vadot /*-
2*1f469a9fSEmmanuel Vadot  * Copyright (c) 2022 The FreeBSD Foundation
3*1f469a9fSEmmanuel Vadot  *
4*1f469a9fSEmmanuel Vadot  * This software was developed by Andrew Turner under sponsorship from
5*1f469a9fSEmmanuel Vadot  * the FreeBSD Foundation.
6*1f469a9fSEmmanuel Vadot  *
7*1f469a9fSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
8*1f469a9fSEmmanuel Vadot  * modification, are permitted provided that the following conditions
9*1f469a9fSEmmanuel Vadot  * are met:
10*1f469a9fSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
11*1f469a9fSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
12*1f469a9fSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
13*1f469a9fSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
14*1f469a9fSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
15*1f469a9fSEmmanuel Vadot  *
16*1f469a9fSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*1f469a9fSEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*1f469a9fSEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*1f469a9fSEmmanuel Vadot  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*1f469a9fSEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*1f469a9fSEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*1f469a9fSEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*1f469a9fSEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*1f469a9fSEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*1f469a9fSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*1f469a9fSEmmanuel Vadot  * SUCH DAMAGE.
27*1f469a9fSEmmanuel Vadot  */
28*1f469a9fSEmmanuel Vadot 
29*1f469a9fSEmmanuel Vadot /*
30*1f469a9fSEmmanuel Vadot  * This manages all hwresets for a device and asserts/deasserts them in
31*1f469a9fSEmmanuel Vadot  * an undefined order.
32*1f469a9fSEmmanuel Vadot  */
33*1f469a9fSEmmanuel Vadot 
34*1f469a9fSEmmanuel Vadot #include "opt_platform.h"
35*1f469a9fSEmmanuel Vadot 
36*1f469a9fSEmmanuel Vadot #include <sys/param.h>
37*1f469a9fSEmmanuel Vadot #include <sys/malloc.h>
38*1f469a9fSEmmanuel Vadot 
39*1f469a9fSEmmanuel Vadot #ifdef FDT
40*1f469a9fSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
41*1f469a9fSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
42*1f469a9fSEmmanuel Vadot #endif
43*1f469a9fSEmmanuel Vadot 
44*1f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
45*1f469a9fSEmmanuel Vadot 
46*1f469a9fSEmmanuel Vadot MALLOC_DECLARE(M_HWRESET);
47*1f469a9fSEmmanuel Vadot 
48*1f469a9fSEmmanuel Vadot struct hwreset_array {
49*1f469a9fSEmmanuel Vadot 	hwreset_t *rst_array;
50*1f469a9fSEmmanuel Vadot 	int	count;
51*1f469a9fSEmmanuel Vadot };
52*1f469a9fSEmmanuel Vadot 
53*1f469a9fSEmmanuel Vadot int
hwreset_array_assert(hwreset_array_t rsts)54*1f469a9fSEmmanuel Vadot hwreset_array_assert(hwreset_array_t rsts)
55*1f469a9fSEmmanuel Vadot {
56*1f469a9fSEmmanuel Vadot 	int i, rv;
57*1f469a9fSEmmanuel Vadot 
58*1f469a9fSEmmanuel Vadot 	for (i = 0; i < rsts->count; i++) {
59*1f469a9fSEmmanuel Vadot 		rv = hwreset_assert(rsts->rst_array[i]);
60*1f469a9fSEmmanuel Vadot 		if (rv != 0)
61*1f469a9fSEmmanuel Vadot 			return (rv);
62*1f469a9fSEmmanuel Vadot 	}
63*1f469a9fSEmmanuel Vadot 
64*1f469a9fSEmmanuel Vadot 	return (0);
65*1f469a9fSEmmanuel Vadot }
66*1f469a9fSEmmanuel Vadot 
67*1f469a9fSEmmanuel Vadot int
hwreset_array_deassert(hwreset_array_t rsts)68*1f469a9fSEmmanuel Vadot hwreset_array_deassert(hwreset_array_t rsts)
69*1f469a9fSEmmanuel Vadot {
70*1f469a9fSEmmanuel Vadot 	int i, rv;
71*1f469a9fSEmmanuel Vadot 
72*1f469a9fSEmmanuel Vadot 	for (i = 0; i < rsts->count; i++) {
73*1f469a9fSEmmanuel Vadot 		rv = hwreset_deassert(rsts->rst_array[i]);
74*1f469a9fSEmmanuel Vadot 		if (rv != 0)
75*1f469a9fSEmmanuel Vadot 			return (rv);
76*1f469a9fSEmmanuel Vadot 	}
77*1f469a9fSEmmanuel Vadot 
78*1f469a9fSEmmanuel Vadot 	return (0);
79*1f469a9fSEmmanuel Vadot }
80*1f469a9fSEmmanuel Vadot 
81*1f469a9fSEmmanuel Vadot void
hwreset_array_release(hwreset_array_t rsts)82*1f469a9fSEmmanuel Vadot hwreset_array_release(hwreset_array_t rsts)
83*1f469a9fSEmmanuel Vadot {
84*1f469a9fSEmmanuel Vadot 	int i;
85*1f469a9fSEmmanuel Vadot 
86*1f469a9fSEmmanuel Vadot 	for (i = 0; i < rsts->count; i++) {
87*1f469a9fSEmmanuel Vadot 		hwreset_release(rsts->rst_array[i]);
88*1f469a9fSEmmanuel Vadot 	}
89*1f469a9fSEmmanuel Vadot 	free(rsts->rst_array, M_HWRESET);
90*1f469a9fSEmmanuel Vadot 	free(rsts, M_HWRESET);
91*1f469a9fSEmmanuel Vadot }
92*1f469a9fSEmmanuel Vadot 
93*1f469a9fSEmmanuel Vadot #ifdef FDT
94*1f469a9fSEmmanuel Vadot int
hwreset_array_get_ofw(device_t consumer_dev,phandle_t cnode,hwreset_array_t * rsts)95*1f469a9fSEmmanuel Vadot hwreset_array_get_ofw(device_t consumer_dev, phandle_t cnode,
96*1f469a9fSEmmanuel Vadot     hwreset_array_t *rsts)
97*1f469a9fSEmmanuel Vadot {
98*1f469a9fSEmmanuel Vadot 	hwreset_array_t resets;
99*1f469a9fSEmmanuel Vadot 	int count, i, rv;
100*1f469a9fSEmmanuel Vadot 
101*1f469a9fSEmmanuel Vadot 	if (cnode <= 0)
102*1f469a9fSEmmanuel Vadot 		cnode = ofw_bus_get_node(consumer_dev);
103*1f469a9fSEmmanuel Vadot 	if (cnode <= 0) {
104*1f469a9fSEmmanuel Vadot 		device_printf(consumer_dev,
105*1f469a9fSEmmanuel Vadot 		    "%s called on not ofw based device\n", __func__);
106*1f469a9fSEmmanuel Vadot 		return (ENXIO);
107*1f469a9fSEmmanuel Vadot 	}
108*1f469a9fSEmmanuel Vadot 
109*1f469a9fSEmmanuel Vadot 	rv = ofw_bus_parse_xref_list_get_length(cnode, "resets", "#reset-cells",
110*1f469a9fSEmmanuel Vadot 	    &count);
111*1f469a9fSEmmanuel Vadot 	if (rv != 0)
112*1f469a9fSEmmanuel Vadot 		return (rv);
113*1f469a9fSEmmanuel Vadot 
114*1f469a9fSEmmanuel Vadot 	resets = malloc(sizeof(struct hwreset_array), M_HWRESET,
115*1f469a9fSEmmanuel Vadot 	    M_WAITOK | M_ZERO);
116*1f469a9fSEmmanuel Vadot 	resets->rst_array = mallocarray(count, sizeof(hwreset_t), M_HWRESET,
117*1f469a9fSEmmanuel Vadot 	    M_WAITOK | M_ZERO);
118*1f469a9fSEmmanuel Vadot 
119*1f469a9fSEmmanuel Vadot 	for (i = 0; i < count; i++) {
120*1f469a9fSEmmanuel Vadot 		rv = hwreset_get_by_ofw_idx(consumer_dev, cnode, i,
121*1f469a9fSEmmanuel Vadot 		    &resets->rst_array[i]);
122*1f469a9fSEmmanuel Vadot 		if (rv != 0)
123*1f469a9fSEmmanuel Vadot 			break;
124*1f469a9fSEmmanuel Vadot 	}
125*1f469a9fSEmmanuel Vadot 
126*1f469a9fSEmmanuel Vadot 	if (rv != 0) {
127*1f469a9fSEmmanuel Vadot 		count = i;
128*1f469a9fSEmmanuel Vadot 		for (i = 0; i < count; i++) {
129*1f469a9fSEmmanuel Vadot 			hwreset_release(resets->rst_array[i]);
130*1f469a9fSEmmanuel Vadot 		}
131*1f469a9fSEmmanuel Vadot 		free(resets->rst_array, M_HWRESET);
132*1f469a9fSEmmanuel Vadot 		free(resets, M_HWRESET);
133*1f469a9fSEmmanuel Vadot 	} else {
134*1f469a9fSEmmanuel Vadot 		resets->count = count;
135*1f469a9fSEmmanuel Vadot 		*rsts = resets;
136*1f469a9fSEmmanuel Vadot 	}
137*1f469a9fSEmmanuel Vadot 	return (rv);
138*1f469a9fSEmmanuel Vadot }
139*1f469a9fSEmmanuel Vadot #endif
140