1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Copyright (C) 2018 Amarula Solutions.
4 * Author: Jagan Teki <jagan@amarulasolutions.com>
10 #include <reset-uclass.h>
13 #include <linux/log2.h>
14 #include <asm/arch/ccu.h>
16 struct sunxi_reset_priv {
19 const struct ccu_desc *desc;
22 static const struct ccu_reset *priv_to_reset(struct sunxi_reset_priv *priv,
25 return &priv->desc->resets[id];
28 static int sunxi_reset_request(struct reset_ctl *reset_ctl)
30 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
32 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
34 if (reset_ctl->id >= priv->count)
40 static int sunxi_reset_free(struct reset_ctl *reset_ctl)
42 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
47 static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
49 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
50 const struct ccu_reset *reset = priv_to_reset(priv, reset_ctl->id);
53 if (!(reset->flags & CCU_RST_F_IS_VALID)) {
54 printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id);
58 debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__,
59 reset_ctl->id, reset->off, ilog2(reset->bit));
61 reg = readl(priv->base + reset->off);
67 writel(reg, priv->base + reset->off);
72 static int sunxi_reset_assert(struct reset_ctl *reset_ctl)
74 return sunxi_set_reset(reset_ctl, false);
77 static int sunxi_reset_deassert(struct reset_ctl *reset_ctl)
79 return sunxi_set_reset(reset_ctl, true);
82 struct reset_ops sunxi_reset_ops = {
83 .request = sunxi_reset_request,
84 .free = sunxi_reset_free,
85 .rst_assert = sunxi_reset_assert,
86 .rst_deassert = sunxi_reset_deassert,
89 static int sunxi_reset_probe(struct udevice *dev)
91 struct sunxi_reset_priv *priv = dev_get_priv(dev);
93 priv->base = dev_read_addr_ptr(dev);
98 int sunxi_reset_bind(struct udevice *dev, ulong count)
100 struct udevice *rst_dev;
101 struct sunxi_reset_priv *priv;
104 ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset",
105 dev_ofnode(dev), &rst_dev);
107 debug("failed to bind sunxi_reset driver (ret=%d)\n", ret);
110 priv = malloc(sizeof(struct sunxi_reset_priv));
112 priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev);
113 rst_dev->priv = priv;
118 U_BOOT_DRIVER(sunxi_reset) = {
119 .name = "sunxi_reset",
121 .ops = &sunxi_reset_ops,
122 .probe = sunxi_reset_probe,
123 .priv_auto_alloc_size = sizeof(struct sunxi_reset_priv),