1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Copyright (C) 2018 Amarula Solutions.
4 * Author: Jagan Teki <jagan@amarulasolutions.com>
11 #include <reset-uclass.h>
14 #include <linux/log2.h>
15 #include <asm/arch/ccu.h>
17 struct sunxi_reset_priv {
20 const struct ccu_desc *desc;
23 static const struct ccu_reset *priv_to_reset(struct sunxi_reset_priv *priv,
26 return &priv->desc->resets[id];
29 static int sunxi_reset_request(struct reset_ctl *reset_ctl)
31 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
33 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
35 if (reset_ctl->id >= priv->count)
41 static int sunxi_reset_free(struct reset_ctl *reset_ctl)
43 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
48 static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
50 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
51 const struct ccu_reset *reset = priv_to_reset(priv, reset_ctl->id);
54 if (!(reset->flags & CCU_RST_F_IS_VALID)) {
55 printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id);
59 debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__,
60 reset_ctl->id, reset->off, ilog2(reset->bit));
62 reg = readl(priv->base + reset->off);
68 writel(reg, priv->base + reset->off);
73 static int sunxi_reset_assert(struct reset_ctl *reset_ctl)
75 return sunxi_set_reset(reset_ctl, false);
78 static int sunxi_reset_deassert(struct reset_ctl *reset_ctl)
80 return sunxi_set_reset(reset_ctl, true);
83 struct reset_ops sunxi_reset_ops = {
84 .request = sunxi_reset_request,
85 .rfree = sunxi_reset_free,
86 .rst_assert = sunxi_reset_assert,
87 .rst_deassert = sunxi_reset_deassert,
90 static int sunxi_reset_probe(struct udevice *dev)
92 struct sunxi_reset_priv *priv = dev_get_priv(dev);
94 priv->base = dev_read_addr_ptr(dev);
99 int sunxi_reset_bind(struct udevice *dev, ulong count)
101 struct udevice *rst_dev;
102 struct sunxi_reset_priv *priv;
105 ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset",
106 dev_ofnode(dev), &rst_dev);
108 debug("failed to bind sunxi_reset driver (ret=%d)\n", ret);
111 priv = malloc(sizeof(struct sunxi_reset_priv));
113 priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev);
114 rst_dev->priv = priv;
119 U_BOOT_DRIVER(sunxi_reset) = {
120 .name = "sunxi_reset",
122 .ops = &sunxi_reset_ops,
123 .probe = sunxi_reset_probe,
124 .priv_auto_alloc_size = sizeof(struct sunxi_reset_priv),