aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/hwreset
diff options
context:
space:
mode:
authorEmmanuel Vadot <manu@FreeBSD.org>2023-12-26 17:49:19 +0000
committerEmmanuel Vadot <manu@FreeBSD.org>2024-01-10 18:20:28 +0000
commit1f469a9fc498c3d406ef7c4e347232678f49da0a (patch)
treebc241c5031f1ae1793e4c6f2dfd84eb31b9e0ae8 /sys/dev/hwreset
parentbe82b3a0bf72ed3b5f01ac9fcd8dcd3802e3c742 (diff)
Diffstat (limited to 'sys/dev/hwreset')
-rw-r--r--sys/dev/hwreset/hwreset.c187
-rw-r--r--sys/dev/hwreset/hwreset.h73
-rw-r--r--sys/dev/hwreset/hwreset_array.c139
-rw-r--r--sys/dev/hwreset/hwreset_if.m73
4 files changed, 472 insertions, 0 deletions
diff --git a/sys/dev/hwreset/hwreset.c b/sys/dev/hwreset/hwreset.c
new file mode 100644
index 000000000000..82ffa3491a11
--- /dev/null
+++ b/sys/dev/hwreset/hwreset.c
@@ -0,0 +1,187 @@
+/*-
+ * 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.
+ */
+#include "opt_platform.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/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
diff --git a/sys/dev/hwreset/hwreset.h b/sys/dev/hwreset/hwreset.h
new file mode 100644
index 000000000000..e5cf51ce3ace
--- /dev/null
+++ b/sys/dev/hwreset/hwreset.h
@@ -0,0 +1,73 @@
+/*-
+ * 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 ``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 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.
+ */
+
+#ifndef _DEV_HWRESET_HWRESET_H_
+#define _DEV_HWRESET_HWRESET_H_
+
+#include "opt_platform.h"
+#include <sys/types.h>
+#ifdef FDT
+#include <dev/ofw/ofw_bus.h>
+#endif
+
+typedef struct hwreset *hwreset_t;
+typedef struct hwreset_array *hwreset_array_t;
+
+/*
+ * Provider interface
+ */
+#ifdef FDT
+void hwreset_register_ofw_provider(device_t provider_dev);
+void hwreset_unregister_ofw_provider(device_t provider_dev);
+#endif
+
+/*
+ * Consumer interface
+ */
+int hwreset_get_by_id(device_t consumer_dev, device_t provider_dev, intptr_t id,
+ hwreset_t *rst);
+void hwreset_release(hwreset_t rst);
+
+int hwreset_assert(hwreset_t rst);
+int hwreset_deassert(hwreset_t rst);
+int hwreset_is_asserted(hwreset_t rst, bool *value);
+
+#ifdef FDT
+int hwreset_get_by_ofw_name(device_t consumer_dev, phandle_t node, char *name,
+ hwreset_t *rst);
+int hwreset_get_by_ofw_idx(device_t consumer_dev, phandle_t node, int idx,
+ hwreset_t *rst);
+#endif
+
+void hwreset_array_release(hwreset_array_t rsts);
+int hwreset_array_assert(hwreset_array_t rsts);
+int hwreset_array_deassert(hwreset_array_t rsts);
+#ifdef FDT
+int hwreset_array_get_ofw(device_t consumer_dev, phandle_t cnode,
+ hwreset_array_t *rsts);
+#endif
+
+
+#endif /* _DEV_HWRESET_HWRESET_H_ */
diff --git a/sys/dev/hwreset/hwreset_array.c b/sys/dev/hwreset/hwreset_array.c
new file mode 100644
index 000000000000..25b73ad4d74a
--- /dev/null
+++ b/sys/dev/hwreset/hwreset_array.c
@@ -0,0 +1,139 @@
+/*-
+ * Copyright (c) 2022 The FreeBSD Foundation
+ *
+ * This software was developed by Andrew Turner under sponsorship from
+ * the FreeBSD Foundation.
+ *
+ * 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.
+ */
+
+/*
+ * This manages all hwresets for a device and asserts/deasserts them in
+ * an undefined order.
+ */
+
+#include "opt_platform.h"
+
+#include <sys/param.h>
+#include <sys/malloc.h>
+
+#ifdef FDT
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+#endif
+
+#include <dev/hwreset/hwreset.h>
+
+MALLOC_DECLARE(M_HWRESET);
+
+struct hwreset_array {
+ hwreset_t *rst_array;
+ int count;
+};
+
+int
+hwreset_array_assert(hwreset_array_t rsts)
+{
+ int i, rv;
+
+ for (i = 0; i < rsts->count; i++) {
+ rv = hwreset_assert(rsts->rst_array[i]);
+ if (rv != 0)
+ return (rv);
+ }
+
+ return (0);
+}
+
+int
+hwreset_array_deassert(hwreset_array_t rsts)
+{
+ int i, rv;
+
+ for (i = 0; i < rsts->count; i++) {
+ rv = hwreset_deassert(rsts->rst_array[i]);
+ if (rv != 0)
+ return (rv);
+ }
+
+ return (0);
+}
+
+void
+hwreset_array_release(hwreset_array_t rsts)
+{
+ int i;
+
+ for (i = 0; i < rsts->count; i++) {
+ hwreset_release(rsts->rst_array[i]);
+ }
+ free(rsts->rst_array, M_HWRESET);
+ free(rsts, M_HWRESET);
+}
+
+#ifdef FDT
+int
+hwreset_array_get_ofw(device_t consumer_dev, phandle_t cnode,
+ hwreset_array_t *rsts)
+{
+ hwreset_array_t resets;
+ int count, i, rv;
+
+ 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_get_length(cnode, "resets", "#reset-cells",
+ &count);
+ if (rv != 0)
+ return (rv);
+
+ resets = malloc(sizeof(struct hwreset_array), M_HWRESET,
+ M_WAITOK | M_ZERO);
+ resets->rst_array = mallocarray(count, sizeof(hwreset_t), M_HWRESET,
+ M_WAITOK | M_ZERO);
+
+ for (i = 0; i < count; i++) {
+ rv = hwreset_get_by_ofw_idx(consumer_dev, cnode, i,
+ &resets->rst_array[i]);
+ if (rv != 0)
+ break;
+ }
+
+ if (rv != 0) {
+ count = i;
+ for (i = 0; i < count; i++) {
+ hwreset_release(resets->rst_array[i]);
+ }
+ free(resets->rst_array, M_HWRESET);
+ free(resets, M_HWRESET);
+ } else {
+ resets->count = count;
+ *rsts = resets;
+ }
+ return (rv);
+}
+#endif
diff --git a/sys/dev/hwreset/hwreset_if.m b/sys/dev/hwreset/hwreset_if.m
new file mode 100644
index 000000000000..53b75c246d64
--- /dev/null
+++ b/sys/dev/hwreset/hwreset_if.m
@@ -0,0 +1,73 @@
+#-
+# 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.
+#
+#
+
+#include "opt_platform.h"
+
+#ifdef FDT
+#include <sys/types.h>
+#include <dev/ofw/ofw_bus.h>
+#endif
+
+INTERFACE hwreset;
+
+#ifdef FDT
+HEADER {
+int hwreset_default_ofw_map(device_t , phandle_t, int, pcell_t *, intptr_t *);
+}
+
+#
+# map fdt property cells to reset id
+# Returns 0 on success or a standard errno value.
+#
+METHOD int map {
+ device_t provider_dev;
+ phandle_t xref;
+ int ncells;
+ pcell_t *cells;
+ intptr_t *id;
+} DEFAULT hwreset_default_ofw_map;
+#endif
+
+#
+# Assert/deassert given reset.
+# Returns 0 on success or a standard errno value.
+#
+METHOD int assert {
+ device_t provider_dev;
+ intptr_t id;
+ bool value;
+};
+
+#
+# Get actual status of given reset.
+# Returns 0 on success or a standard errno value.
+#
+METHOD int is_asserted {
+ device_t provider_dev;
+ intptr_t id;
+ bool *value;
+};