1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Copyright (C) 2018 Amarula Solutions.
4 * Author: Jagan Teki <jagan@amarulasolutions.com>
12 #include <reset-uclass.h>
15 #include <linux/log2.h>
16 #include <asm/arch/ccu.h>
18 struct sunxi_reset_priv {
21 const struct ccu_desc *desc;
24 static const struct ccu_reset *priv_to_reset(struct sunxi_reset_priv *priv,
27 return &priv->desc->resets[id];
30 static int sunxi_reset_request(struct reset_ctl *reset_ctl)
32 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
34 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
36 if (reset_ctl->id >= priv->count)
42 static int sunxi_reset_free(struct reset_ctl *reset_ctl)
44 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
49 static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
51 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
52 const struct ccu_reset *reset = priv_to_reset(priv, reset_ctl->id);
55 if (!(reset->flags & CCU_RST_F_IS_VALID)) {
56 printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id);
60 debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__,
61 reset_ctl->id, reset->off, ilog2(reset->bit));
63 reg = readl(priv->base + reset->off);
69 writel(reg, priv->base + reset->off);
74 static int sunxi_reset_assert(struct reset_ctl *reset_ctl)
76 return sunxi_set_reset(reset_ctl, false);
79 static int sunxi_reset_deassert(struct reset_ctl *reset_ctl)
81 return sunxi_set_reset(reset_ctl, true);
84 struct reset_ops sunxi_reset_ops = {
85 .request = sunxi_reset_request,
86 .rfree = sunxi_reset_free,
87 .rst_assert = sunxi_reset_assert,
88 .rst_deassert = sunxi_reset_deassert,
91 static int sunxi_reset_probe(struct udevice *dev)
93 struct sunxi_reset_priv *priv = dev_get_priv(dev);
95 priv->base = dev_read_addr_ptr(dev);
100 int sunxi_reset_bind(struct udevice *dev, ulong count)
102 struct udevice *rst_dev;
103 struct sunxi_reset_priv *priv;
106 ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset",
107 dev_ofnode(dev), &rst_dev);
109 debug("failed to bind sunxi_reset driver (ret=%d)\n", ret);
112 priv = malloc(sizeof(struct sunxi_reset_priv));
114 priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev);
115 rst_dev->priv = priv;
120 U_BOOT_DRIVER(sunxi_reset) = {
121 .name = "sunxi_reset",
123 .ops = &sunxi_reset_ops,
124 .probe = sunxi_reset_probe,
125 .priv_auto_alloc_size = sizeof(struct sunxi_reset_priv),