test: stabilize test_efi_secboot
[platform/kernel/u-boot.git] / drivers / reset / reset-sunxi.c
1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3  * Copyright (C) 2018 Amarula Solutions.
4  * Author: Jagan Teki <jagan@amarulasolutions.com>
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <malloc.h>
11 #include <reset-uclass.h>
12 #include <asm/io.h>
13 #include <dm/lists.h>
14 #include <linux/log2.h>
15 #include <asm/arch/ccu.h>
16
17 struct sunxi_reset_priv {
18         void *base;
19         ulong count;
20         const struct ccu_desc *desc;
21 };
22
23 static const struct ccu_reset *priv_to_reset(struct sunxi_reset_priv *priv,
24                                              unsigned long id)
25 {
26         return  &priv->desc->resets[id];
27 }
28
29 static int sunxi_reset_request(struct reset_ctl *reset_ctl)
30 {
31         struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
32
33         debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
34
35         if (reset_ctl->id >= priv->count)
36                 return -EINVAL;
37
38         return 0;
39 }
40
41 static int sunxi_reset_free(struct reset_ctl *reset_ctl)
42 {
43         debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
44
45         return 0;
46 }
47
48 static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
49 {
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);
52         u32 reg;
53
54         if (!(reset->flags & CCU_RST_F_IS_VALID)) {
55                 printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id);
56                 return 0;
57         }
58
59         debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__,
60               reset_ctl->id, reset->off, ilog2(reset->bit));
61
62         reg = readl(priv->base + reset->off);
63         if (on)
64                 reg |= reset->bit;
65         else
66                 reg &= ~reset->bit;
67
68         writel(reg, priv->base + reset->off);
69
70         return 0;
71 }
72
73 static int sunxi_reset_assert(struct reset_ctl *reset_ctl)
74 {
75         return sunxi_set_reset(reset_ctl, false);
76 }
77
78 static int sunxi_reset_deassert(struct reset_ctl *reset_ctl)
79 {
80         return sunxi_set_reset(reset_ctl, true);
81 }
82
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,
88 };
89
90 static int sunxi_reset_probe(struct udevice *dev)
91 {
92         struct sunxi_reset_priv *priv = dev_get_priv(dev);
93
94         priv->base = dev_read_addr_ptr(dev);
95
96         return 0;
97 }
98
99 int sunxi_reset_bind(struct udevice *dev, ulong count)
100 {
101         struct udevice *rst_dev;
102         struct sunxi_reset_priv *priv;
103         int ret;
104
105         ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset",
106                                          dev_ofnode(dev), &rst_dev);
107         if (ret) {
108                 debug("failed to bind sunxi_reset driver (ret=%d)\n", ret);
109                 return ret;
110         }
111         priv = malloc(sizeof(struct sunxi_reset_priv));
112         priv->count = count;
113         priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev);
114         rst_dev->priv = priv;
115
116         return 0;
117 }
118
119 U_BOOT_DRIVER(sunxi_reset) = {
120         .name           = "sunxi_reset",
121         .id             = UCLASS_RESET,
122         .ops            = &sunxi_reset_ops,
123         .probe          = sunxi_reset_probe,
124         .priv_auto_alloc_size = sizeof(struct sunxi_reset_priv),
125 };