2 * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include "opt_platform.h"
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/systm.h>
37 #include <dev/ofw/ofw_bus.h>
38 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/extres/hwreset/hwreset.h>
43 #include "hwreset_if.h"
46 device_t consumer_dev; /* consumer device*/
47 device_t provider_dev; /* provider device*/
48 int rst_id; /* reset id */
51 MALLOC_DEFINE(M_HWRESET, "hwreset", "Reset framework");
54 hwreset_assert(hwreset_t rst)
57 return (HWRESET_ASSERT(rst->provider_dev, rst->rst_id, true));
61 hwreset_deassert(hwreset_t rst)
64 return (HWRESET_ASSERT(rst->provider_dev, rst->rst_id, false));
68 hwreset_is_asserted(hwreset_t rst, bool *value)
71 return (HWRESET_IS_ASSERTED(rst->provider_dev, rst->rst_id, value));
75 hwreset_release(hwreset_t rst)
81 hwreset_get_by_id(device_t consumer_dev, device_t provider_dev, intptr_t id,
87 rst = malloc(sizeof(struct hwreset), M_HWRESET,
89 rst->consumer_dev = consumer_dev;
90 rst->provider_dev = provider_dev;
98 hwreset_default_ofw_map(device_t provider_dev, phandle_t xref, int ncells,
99 pcell_t *cells, intptr_t *id)
103 else if (ncells == 1)
112 hwreset_get_by_ofw_idx(device_t consumer_dev, phandle_t cnode, int idx,
122 cnode = ofw_bus_get_node(consumer_dev);
124 device_printf(consumer_dev,
125 "%s called on not ofw based device\n", __func__);
129 rv = ofw_bus_parse_xref_list_alloc(cnode, "resets", "#reset-cells",
130 idx, &xnode, &ncells, &cells);
134 /* Tranlate provider to device */
135 rstdev = OF_device_from_xref(xnode);
136 if (rstdev == NULL) {
140 /* Map reset to number */
141 rv = HWRESET_MAP(rstdev, xnode, ncells, cells, &id);
146 return (hwreset_get_by_id(consumer_dev, rstdev, id, rst));
150 hwreset_get_by_ofw_name(device_t consumer_dev, phandle_t cnode, char *name,
156 cnode = ofw_bus_get_node(consumer_dev);
158 device_printf(consumer_dev,
159 "%s called on not ofw based device\n", __func__);
162 rv = ofw_bus_find_string_index(cnode, "reset-names", name, &idx);
165 return (hwreset_get_by_ofw_idx(consumer_dev, cnode, idx, rst));
169 hwreset_register_ofw_provider(device_t provider_dev)
171 phandle_t xref, node;
173 node = ofw_bus_get_node(provider_dev);
175 panic("%s called on not ofw based device.\n", __func__);
177 xref = OF_xref_from_node(node);
178 OF_device_register_xref(xref, provider_dev);
182 hwreset_unregister_ofw_provider(device_t provider_dev)
186 xref = OF_xref_from_device(provider_dev);
187 OF_device_register_xref(xref, NULL);