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