1/*-
2 * Copyright (c) 2016 Stanislav Galabov
3 * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 */
29
30#include <sys/cdefs.h>
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/lock.h>
34#include <sys/mutex.h>
35#include <sys/queue.h>
36#include <sys/systm.h>
37
38#include <dev/ofw/ofw_bus.h>
39#include <dev/ofw/ofw_bus_subr.h>
40
41#include "fdt_reset_if.h"
42#include <mips/mediatek/fdt_reset.h>
43
44/*
45 * Loop through all the tuples in the resets= property for a device, asserting
46 * or deasserting each reset.
47 *
48 * Be liberal about errors for now: warn about a failure to (de)assert but keep
49 * trying with any other resets in the list.  Return ENXIO if any errors were
50 * found, and let the caller decide whether the problem is fatal.
51 */
52static int
53assert_deassert_all(device_t consumer, boolean_t assert)
54{
55	phandle_t rnode;
56	device_t resetdev;
57	int resetnum, err, i, ncells;
58	uint32_t *resets;
59	boolean_t anyerrors;
60
61	rnode = ofw_bus_get_node(consumer);
62	ncells = OF_getencprop_alloc(rnode, "resets", sizeof(*resets),
63	    (void **)&resets);
64	if (!assert && ncells < 2) {
65		device_printf(consumer, "Warning: No resets specified in fdt "
66		    "data; device may not function.");
67		return (ENXIO);
68	}
69	anyerrors = false;
70	for (i = 0; i < ncells; i += 2) {
71		resetdev = OF_device_from_xref(resets[i]);
72		resetnum = resets[i + 1];
73		if (resetdev == NULL) {
74			if (!assert)
75				device_printf(consumer, "Warning: can not find "
76				    "driver for reset number %u; device may "
77				    "not function\n", resetnum);
78			anyerrors = true;
79			continue;
80		}
81		if (assert)
82			err = FDT_RESET_ASSERT(resetdev, resetnum);
83		else
84			err = FDT_RESET_DEASSERT(resetdev, resetnum);
85		if (err != 0) {
86			if (!assert)
87				device_printf(consumer, "Warning: failed to "
88				    "deassert reset number %u; device may not "
89				    "function\n", resetnum);
90			anyerrors = true;
91		}
92	}
93	OF_prop_free(resets);
94	return (anyerrors ? ENXIO : 0);
95}
96
97int
98fdt_reset_assert_all(device_t consumer)
99{
100
101	return (assert_deassert_all(consumer, true));
102}
103
104int
105fdt_reset_deassert_all(device_t consumer)
106{
107
108	return (assert_deassert_all(consumer, false));
109}
110
111void
112fdt_reset_register_provider(device_t provider)
113{
114
115	OF_device_register_xref(
116	    OF_xref_from_node(ofw_bus_get_node(provider)), provider);
117}
118
119void
120fdt_reset_unregister_provider(device_t provider)
121{
122
123	OF_device_register_xref(OF_xref_from_device(provider), NULL);
124}
125
126