1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Copyright (C) 2018 Amarula Solutions.
12 #include <reset-uclass.h>
14 #include <clk/sunxi.h>
15 #include <dm/device-internal.h>
17 #include <linux/bitops.h>
18 #include <linux/log2.h>
20 struct sunxi_reset_priv {
23 const struct ccu_desc *desc;
26 static const struct ccu_reset *priv_to_reset(struct sunxi_reset_priv *priv,
29 return &priv->desc->resets[id];
32 static int sunxi_reset_request(struct reset_ctl *reset_ctl)
34 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
36 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
38 if (reset_ctl->id >= priv->count)
44 static int sunxi_reset_free(struct reset_ctl *reset_ctl)
46 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
51 static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
53 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
54 const struct ccu_reset *reset = priv_to_reset(priv, reset_ctl->id);
57 if (!(reset->flags & CCU_RST_F_IS_VALID)) {
58 printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id);
62 debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__,
63 reset_ctl->id, reset->off, ilog2(reset->bit));
65 reg = readl(priv->base + reset->off);
71 writel(reg, priv->base + reset->off);
76 static int sunxi_reset_assert(struct reset_ctl *reset_ctl)
78 return sunxi_set_reset(reset_ctl, false);
81 static int sunxi_reset_deassert(struct reset_ctl *reset_ctl)
83 return sunxi_set_reset(reset_ctl, true);
86 struct reset_ops sunxi_reset_ops = {
87 .request = sunxi_reset_request,
88 .rfree = sunxi_reset_free,
89 .rst_assert = sunxi_reset_assert,
90 .rst_deassert = sunxi_reset_deassert,
93 static int sunxi_reset_probe(struct udevice *dev)
95 struct sunxi_reset_priv *priv = dev_get_priv(dev);
97 priv->base = dev_read_addr_ptr(dev);
102 int sunxi_reset_bind(struct udevice *dev, ulong count)
104 struct udevice *rst_dev;
105 struct sunxi_reset_priv *priv;
108 ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset",
109 dev_ofnode(dev), &rst_dev);
111 debug("failed to bind sunxi_reset driver (ret=%d)\n", ret);
114 priv = malloc(sizeof(struct sunxi_reset_priv));
116 priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev);
117 dev_set_priv(rst_dev, priv);
122 U_BOOT_DRIVER(sunxi_reset) = {
123 .name = "sunxi_reset",
125 .ops = &sunxi_reset_ops,
126 .probe = sunxi_reset_probe,
127 .priv_auto = sizeof(struct sunxi_reset_priv),