blob: eeb3d2eea7748fb6456a7f88f58f26d0e3080416 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Elaine Zhang760188c2017-12-19 18:22:37 +08002/*
3 * (C) Copyright 2017 Rockchip Electronics Co., Ltd
Elaine Zhang760188c2017-12-19 18:22:37 +08004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -07009#include <malloc.h>
Elaine Zhang760188c2017-12-19 18:22:37 +080010#include <reset-uclass.h>
Simon Glasscd93d622020-05-10 11:40:13 -060011#include <linux/bitops.h>
Elaine Zhang760188c2017-12-19 18:22:37 +080012#include <linux/io.h>
Kever Yang15f09a12019-03-28 11:01:23 +080013#include <asm/arch-rockchip/hardware.h>
Simon Glass0fd3d912020-12-22 19:30:28 -070014#include <dm/device-internal.h>
Elaine Zhang760188c2017-12-19 18:22:37 +080015#include <dm/lists.h>
16/*
17 * Each reg has 16 bits reset signal for devices
18 * Note: Not including rk2818 and older SoCs
19 */
20#define ROCKCHIP_RESET_NUM_IN_REG 16
21
22struct rockchip_reset_priv {
23 void __iomem *base;
24 /* Rockchip reset reg locate at cru controller */
25 u32 reset_reg_offset;
26 /* Rockchip reset reg number */
27 u32 reset_reg_num;
28};
29
30static int rockchip_reset_request(struct reset_ctl *reset_ctl)
31{
32 struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev);
33
34 debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_num=%d)\n", __func__,
35 reset_ctl, reset_ctl->dev, reset_ctl->id, priv->reset_reg_num);
36
37 if (reset_ctl->id / ROCKCHIP_RESET_NUM_IN_REG >= priv->reset_reg_num)
38 return -EINVAL;
39
40 return 0;
41}
42
43static int rockchip_reset_free(struct reset_ctl *reset_ctl)
44{
45 debug("%s(reset_ctl=%p) (dev=%p, id=%lu)\n", __func__, reset_ctl,
46 reset_ctl->dev, reset_ctl->id);
47
48 return 0;
49}
50
51static int rockchip_reset_assert(struct reset_ctl *reset_ctl)
52{
53 struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev);
54 int bank = reset_ctl->id / ROCKCHIP_RESET_NUM_IN_REG;
55 int offset = reset_ctl->id % ROCKCHIP_RESET_NUM_IN_REG;
56
57 debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_addr=%p)\n", __func__,
58 reset_ctl, reset_ctl->dev, reset_ctl->id,
59 priv->base + (bank * 4));
60
61 rk_setreg(priv->base + (bank * 4), BIT(offset));
62
63 return 0;
64}
65
66static int rockchip_reset_deassert(struct reset_ctl *reset_ctl)
67{
68 struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev);
69 int bank = reset_ctl->id / ROCKCHIP_RESET_NUM_IN_REG;
70 int offset = reset_ctl->id % ROCKCHIP_RESET_NUM_IN_REG;
71
72 debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_addr=%p)\n", __func__,
73 reset_ctl, reset_ctl->dev, reset_ctl->id,
74 priv->base + (bank * 4));
75
76 rk_clrreg(priv->base + (bank * 4), BIT(offset));
77
78 return 0;
79}
80
81struct reset_ops rockchip_reset_ops = {
82 .request = rockchip_reset_request,
Simon Glass94474b22020-02-03 07:35:52 -070083 .rfree = rockchip_reset_free,
Elaine Zhang760188c2017-12-19 18:22:37 +080084 .rst_assert = rockchip_reset_assert,
85 .rst_deassert = rockchip_reset_deassert,
86};
87
88static int rockchip_reset_probe(struct udevice *dev)
89{
90 struct rockchip_reset_priv *priv = dev_get_priv(dev);
91 fdt_addr_t addr;
92 fdt_size_t size;
93
94 addr = dev_read_addr_size(dev, "reg", &size);
95 if (addr == FDT_ADDR_T_NONE)
96 return -EINVAL;
97
98 if ((priv->reset_reg_offset == 0) && (priv->reset_reg_num == 0))
99 return -EINVAL;
100
101 addr += priv->reset_reg_offset;
102 priv->base = ioremap(addr, size);
103
104 debug("%s(base=%p) (reg_offset=%x, reg_num=%d)\n", __func__,
105 priv->base, priv->reset_reg_offset, priv->reset_reg_num);
106
107 return 0;
108}
109
110int rockchip_reset_bind(struct udevice *pdev, u32 reg_offset, u32 reg_number)
111{
112 struct udevice *rst_dev;
113 struct rockchip_reset_priv *priv;
114 int ret;
115
116 ret = device_bind_driver_to_node(pdev, "rockchip_reset", "reset",
117 dev_ofnode(pdev), &rst_dev);
118 if (ret) {
119 debug("Warning: No rockchip reset driver: ret=%d\n", ret);
120 return ret;
121 }
122 priv = malloc(sizeof(struct rockchip_reset_priv));
123 priv->reset_reg_offset = reg_offset;
124 priv->reset_reg_num = reg_number;
Simon Glass0fd3d912020-12-22 19:30:28 -0700125 dev_set_priv(rst_dev, priv);
Elaine Zhang760188c2017-12-19 18:22:37 +0800126
127 return 0;
128}
129
130U_BOOT_DRIVER(rockchip_reset) = {
131 .name = "rockchip_reset",
132 .id = UCLASS_RESET,
133 .probe = rockchip_reset_probe,
134 .ops = &rockchip_reset_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700135 .priv_auto = sizeof(struct rockchip_reset_priv),
Elaine Zhang760188c2017-12-19 18:22:37 +0800136};