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>
12 static inline const struct hwspinlock_ops *
13 hwspinlock_dev_ops(struct udevice *dev)
15 return (const struct hwspinlock_ops *)dev->driver->ops;
18 static int hwspinlock_of_xlate_default(struct hwspinlock *hws,
19 struct ofnode_phandle_args *args)
21 if (args->args_count > 1) {
22 debug("Invaild args_count: %d\n", args->args_count);
27 hws->id = args->args[0];
34 int hwspinlock_get_by_index(struct udevice *dev, int index,
35 struct hwspinlock *hws)
38 struct ofnode_phandle_args args;
39 struct udevice *dev_hws;
40 const struct hwspinlock_ops *ops;
45 ret = dev_read_phandle_with_args(dev, "hwlocks", "#hwlock-cells", 1,
48 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
53 ret = uclass_get_device_by_ofnode(UCLASS_HWSPINLOCK,
57 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
64 ops = hwspinlock_dev_ops(dev_hws);
67 ret = ops->of_xlate(hws, &args);
69 ret = hwspinlock_of_xlate_default(hws, &args);
71 dev_dbg(dev, "of_xlate() failed: %d\n", ret);
76 int hwspinlock_lock_timeout(struct hwspinlock *hws, unsigned int timeout)
78 const struct hwspinlock_ops *ops;
87 ops = hwspinlock_dev_ops(hws->dev);
93 ret = ops->lock(hws->dev, hws->id);
99 } while (get_timer(start) < timeout);
104 int hwspinlock_unlock(struct hwspinlock *hws)
106 const struct hwspinlock_ops *ops;
113 ops = hwspinlock_dev_ops(hws->dev);
117 return ops->unlock(hws->dev, hws->id);
120 static int hwspinlock_post_bind(struct udevice *dev)
122 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
123 struct hwspinlock_ops *ops = device_get_ops(dev);
124 static int reloc_done;
128 ops->lock += gd->reloc_off;
130 ops->unlock += gd->reloc_off;
132 ops->relax += gd->reloc_off;
140 UCLASS_DRIVER(hwspinlock) = {
141 .id = UCLASS_HWSPINLOCK,
142 .name = "hwspinlock",
143 .post_bind = hwspinlock_post_bind,