summaryrefslogtreecommitdiff
path: root/drivers/hwspinlock/hwspinlock-uclass.c
blob: 61d226bcbb5eb79e6cdedea5253b7dc81317726c (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
// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
/*
 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
 */

#include <common.h>
#include <dm.h>
#include <errno.h>
#include <hwspinlock.h>
#include <dm/device-internal.h>
#include <dm/device_compat.h>
#include <linux/compat.h>

static inline const struct hwspinlock_ops *
hwspinlock_dev_ops(struct udevice *dev)
{
	return (const struct hwspinlock_ops *)dev->driver->ops;
}

static int hwspinlock_of_xlate_default(struct hwspinlock *hws,
				       struct ofnode_phandle_args *args)
{
	if (args->args_count > 1) {
		debug("Invaild args_count: %d\n", args->args_count);
		return -EINVAL;
	}

	if (args->args_count)
		hws->id = args->args[0];
	else
		hws->id = 0;

	return 0;
}

int hwspinlock_get_by_index(struct udevice *dev, int index,
			    struct hwspinlock *hws)
{
	int ret;
	struct ofnode_phandle_args args;
	struct udevice *dev_hws;
	const struct hwspinlock_ops *ops;

	assert(hws);
	hws->dev = NULL;

	ret = dev_read_phandle_with_args(dev, "hwlocks", "#hwlock-cells", 1,
					 index, &args);
	if (ret) {
		dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
			__func__, ret);
		return ret;
	}

	ret = uclass_get_device_by_ofnode(UCLASS_HWSPINLOCK,
					  args.node, &dev_hws);
	if (ret) {
		dev_dbg(dev,
			"%s: uclass_get_device_by_of_offset failed: err=%d\n",
			__func__, ret);
		return ret;
	}

	hws->dev = dev_hws;

	ops = hwspinlock_dev_ops(dev_hws);

	if (ops->of_xlate)
		ret = ops->of_xlate(hws, &args);
	else
		ret = hwspinlock_of_xlate_default(hws, &args);
	if (ret)
		dev_dbg(dev, "of_xlate() failed: %d\n", ret);

	return ret;
}

int hwspinlock_lock_timeout(struct hwspinlock *hws, unsigned int timeout)
{
	const struct hwspinlock_ops *ops;
	ulong start;
	int ret;

	assert(hws);

	if (!hws->dev)
		return -EINVAL;

	ops = hwspinlock_dev_ops(hws->dev);
	if (!ops->lock)
		return -ENOSYS;

	start = get_timer(0);
	do {
		ret = ops->lock(hws->dev, hws->id);
		if (!ret)
			return ret;

		if (ops->relax)
			ops->relax(hws->dev);
	} while (get_timer(start) < timeout);

	return -ETIMEDOUT;
}

int hwspinlock_unlock(struct hwspinlock *hws)
{
	const struct hwspinlock_ops *ops;

	assert(hws);

	if (!hws->dev)
		return -EINVAL;

	ops = hwspinlock_dev_ops(hws->dev);
	if (!ops->unlock)
		return -ENOSYS;

	return ops->unlock(hws->dev, hws->id);
}

static int hwspinlock_post_bind(struct udevice *dev)
{
#if defined(CONFIG_NEEDS_MANUAL_RELOC)
	struct hwspinlock_ops *ops = device_get_ops(dev);
	static int reloc_done;

	if (!reloc_done) {
		if (ops->lock)
			ops->lock += gd->reloc_off;
		if (ops->unlock)
			ops->unlock += gd->reloc_off;
		if (ops->relax)
			ops->relax += gd->reloc_off;

		reloc_done++;
	}
#endif
	return 0;
}

UCLASS_DRIVER(hwspinlock) = {
	.id		= UCLASS_HWSPINLOCK,
	.name		= "hwspinlock",
	.post_bind	= hwspinlock_post_bind,
};