summaryrefslogtreecommitdiffstats
path: root/sys/dev/extres/hwreset/hwreset.c
blob: 40ee915e3aacf1a47debddbe44f1b79da7613828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*-
 * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */
#include "opt_platform.h"
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/kobj.h>
#include <sys/malloc.h>
#include <sys/systm.h>

#ifdef FDT
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif

#include <dev/extres/hwreset/hwreset.h>

#include "hwreset_if.h"

struct hwreset {
	device_t consumer_dev;		/* consumer device*/
	device_t provider_dev;		/* provider device*/
	int 	rst_id;			/* reset id */
};

MALLOC_DEFINE(M_HWRESET, "hwreset", "Reset framework");

int
hwreset_assert(hwreset_t rst)
{

	return (HWRESET_ASSERT(rst->provider_dev, rst->rst_id, true));
}

int
hwreset_deassert(hwreset_t rst)
{

	return (HWRESET_ASSERT(rst->provider_dev, rst->rst_id, false));
}

int
hwreset_is_asserted(hwreset_t rst, bool *value)
{

	return (HWRESET_IS_ASSERTED(rst->provider_dev, rst->rst_id, value));
}

void
hwreset_release(hwreset_t rst)
{
	free(rst, M_HWRESET);
}

int
hwreset_get_by_id(device_t consumer_dev, device_t provider_dev, intptr_t id,
    hwreset_t *rst_out)
{
	hwreset_t rst;

	/* Create handle */
	rst = malloc(sizeof(struct hwreset), M_HWRESET,
	    M_WAITOK | M_ZERO);
	rst->consumer_dev = consumer_dev;
	rst->provider_dev = provider_dev;
	rst->rst_id = id;
	*rst_out = rst;
	return (0);
}

#ifdef FDT
int
hwreset_default_ofw_map(device_t provider_dev, phandle_t xref, int ncells,
    pcell_t *cells, intptr_t *id)
{
	if (ncells == 0)
		*id = 1;
	else if (ncells == 1)
		*id = cells[0];
	else
		return  (ERANGE);

	return (0);
}

int
hwreset_get_by_ofw_idx(device_t consumer_dev, phandle_t cnode, int idx,
    hwreset_t *rst)
{
	phandle_t xnode;
	pcell_t *cells;
	device_t rstdev;
	int ncells, rv;
	intptr_t id;

	if (cnode <= 0)
		cnode = ofw_bus_get_node(consumer_dev);
	if (cnode <= 0) {
		device_printf(consumer_dev,
		    "%s called on not ofw based device\n", __func__);
		return (ENXIO);
	}

	rv = ofw_bus_parse_xref_list_alloc(cnode, "resets", "#reset-cells",
	    idx, &xnode, &ncells, &cells);
	if (rv != 0)
		return (rv);

	/* Tranlate provider to device */
	rstdev = OF_device_from_xref(xnode);
	if (rstdev == NULL) {
		OF_prop_free(cells);
		return (ENODEV);
	}
	/* Map reset to number */
	rv = HWRESET_MAP(rstdev, xnode, ncells, cells, &id);
	OF_prop_free(cells);
	if (rv != 0)
		return (rv);

	return (hwreset_get_by_id(consumer_dev, rstdev, id, rst));
}

int
hwreset_get_by_ofw_name(device_t consumer_dev, phandle_t cnode, char *name,
    hwreset_t *rst)
{
	int rv, idx;

	if (cnode <= 0)
		cnode = ofw_bus_get_node(consumer_dev);
	if (cnode <= 0) {
		device_printf(consumer_dev,
		    "%s called on not ofw based device\n",  __func__);
		return (ENXIO);
	}
	rv = ofw_bus_find_string_index(cnode, "reset-names", name, &idx);
	if (rv != 0)
		return (rv);
	return (hwreset_get_by_ofw_idx(consumer_dev, cnode, idx, rst));
}

void
hwreset_register_ofw_provider(device_t provider_dev)
{
	phandle_t xref, node;

	node = ofw_bus_get_node(provider_dev);
	if (node <= 0)
		panic("%s called on not ofw based device.\n", __func__);

	xref = OF_xref_from_node(node);
	OF_device_register_xref(xref, provider_dev);
}

void
hwreset_unregister_ofw_provider(device_t provider_dev)
{
	phandle_t xref;

	xref = OF_xref_from_device(provider_dev);
	OF_device_register_xref(xref, NULL);
}
#endif
OpenPOWER on IntegriCloud