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