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