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/bitops.h>
16 #include <linux/log2.h>
17 #include <asm/arch/ccu.h>
19 struct sunxi_reset_priv {
22 const struct ccu_desc *desc;
25 static const struct ccu_reset *priv_to_reset(struct sunxi_reset_priv *priv,
28 return &priv->desc->resets[id];
31 static int sunxi_reset_request(struct reset_ctl *reset_ctl)
33 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
35 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
37 if (reset_ctl->id >= priv->count)
43 static int sunxi_reset_free(struct reset_ctl *reset_ctl)
45 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
50 static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
52 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
53 const struct ccu_reset *reset = priv_to_reset(priv, reset_ctl->id);
56 if (!(reset->flags & CCU_RST_F_IS_VALID)) {
57 printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id);
61 debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__,
62 reset_ctl->id, reset->off, ilog2(reset->bit));
64 reg = readl(priv->base + reset->off);
70 writel(reg, priv->base + reset->off);
75 static int sunxi_reset_assert(struct reset_ctl *reset_ctl)
77 return sunxi_set_reset(reset_ctl, false);
80 static int sunxi_reset_deassert(struct reset_ctl *reset_ctl)
82 return sunxi_set_reset(reset_ctl, true);
85 struct reset_ops sunxi_reset_ops = {
86 .request = sunxi_reset_request,
87 .rfree = sunxi_reset_free,
88 .rst_assert = sunxi_reset_assert,
89 .rst_deassert = sunxi_reset_deassert,
92 static int sunxi_reset_probe(struct udevice *dev)
94 struct sunxi_reset_priv *priv = dev_get_priv(dev);
96 priv->base = dev_read_addr_ptr(dev);
101 int sunxi_reset_bind(struct udevice *dev, ulong count)
103 struct udevice *rst_dev;
104 struct sunxi_reset_priv *priv;
107 ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset",
108 dev_ofnode(dev), &rst_dev);
110 debug("failed to bind sunxi_reset driver (ret=%d)\n", ret);
113 priv = malloc(sizeof(struct sunxi_reset_priv));
115 priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev);
116 rst_dev->priv = priv;
121 U_BOOT_DRIVER(sunxi_reset) = {
122 .name = "sunxi_reset",
124 .ops = &sunxi_reset_ops,
125 .probe = sunxi_reset_probe,
126 .priv_auto = sizeof(struct sunxi_reset_priv),