1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
9 #include <hwspinlock.h>
10 #include <dm/device-internal.h>
11 #include <dm/device_compat.h>
12 #include <linux/compat.h>
14 static inline const struct hwspinlock_ops *
15 hwspinlock_dev_ops(struct udevice *dev)
17 return (const struct hwspinlock_ops *)dev->driver->ops;
20 static int hwspinlock_of_xlate_default(struct hwspinlock *hws,
21 struct ofnode_phandle_args *args)
23 if (args->args_count > 1) {
24 debug("Invaild args_count: %d\n", args->args_count);
29 hws->id = args->args[0];
36 int hwspinlock_get_by_index(struct udevice *dev, int index,
37 struct hwspinlock *hws)
40 struct ofnode_phandle_args args;
41 struct udevice *dev_hws;
42 const struct hwspinlock_ops *ops;
47 ret = dev_read_phandle_with_args(dev, "hwlocks", "#hwlock-cells", 1,
50 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
55 ret = uclass_get_device_by_ofnode(UCLASS_HWSPINLOCK,
59 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
66 ops = hwspinlock_dev_ops(dev_hws);
69 ret = ops->of_xlate(hws, &args);
71 ret = hwspinlock_of_xlate_default(hws, &args);
73 dev_dbg(dev, "of_xlate() failed: %d\n", ret);
78 int hwspinlock_lock_timeout(struct hwspinlock *hws, unsigned int timeout)
80 const struct hwspinlock_ops *ops;
89 ops = hwspinlock_dev_ops(hws->dev);
95 ret = ops->lock(hws->dev, hws->id);
100 ops->relax(hws->dev);
101 } while (get_timer(start) < timeout);
106 int hwspinlock_unlock(struct hwspinlock *hws)
108 const struct hwspinlock_ops *ops;
115 ops = hwspinlock_dev_ops(hws->dev);
119 return ops->unlock(hws->dev, hws->id);
122 static int hwspinlock_post_bind(struct udevice *dev)
124 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
125 struct hwspinlock_ops *ops = device_get_ops(dev);
126 static int reloc_done;
130 ops->lock += gd->reloc_off;
132 ops->unlock += gd->reloc_off;
134 ops->relax += gd->reloc_off;
142 UCLASS_DRIVER(hwspinlock) = {
143 .id = UCLASS_HWSPINLOCK,
144 .name = "hwspinlock",
145 .post_bind = hwspinlock_post_bind,