blob: 2ebe3382f70af0c4943b5a615c0eeff2997bab4b [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;
Eugen Hristevbb52f1c2023-05-15 13:55:04 +030024 const int *lut;
Elaine Zhang760188c2017-12-19 18:22:37 +080025 /* Rockchip reset reg locate at cru controller */
26 u32 reset_reg_offset;
27 /* Rockchip reset reg number */
28 u32 reset_reg_num;
29};
30
31static int rockchip_reset_request(struct reset_ctl *reset_ctl)
32{
33 struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev);
Eugen Hristevbb52f1c2023-05-15 13:55:04 +030034 unsigned long id = reset_ctl->id;
35
36 if (priv->lut)
37 id = priv->lut[id];
Elaine Zhang760188c2017-12-19 18:22:37 +080038
39 debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_num=%d)\n", __func__,
Eugen Hristevbb52f1c2023-05-15 13:55:04 +030040 reset_ctl, reset_ctl->dev, id, priv->reset_reg_num);
Elaine Zhang760188c2017-12-19 18:22:37 +080041
Eugen Hristevbb52f1c2023-05-15 13:55:04 +030042 if (id / ROCKCHIP_RESET_NUM_IN_REG >= priv->reset_reg_num)
Elaine Zhang760188c2017-12-19 18:22:37 +080043 return -EINVAL;
44
45 return 0;
46}
47
Elaine Zhang760188c2017-12-19 18:22:37 +080048static int rockchip_reset_assert(struct reset_ctl *reset_ctl)
49{
50 struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev);
Eugen Hristevbb52f1c2023-05-15 13:55:04 +030051 unsigned long id = reset_ctl->id;
52 int bank, offset;
53
54 if (priv->lut)
55 id = priv->lut[id];
56
57 bank = id / ROCKCHIP_RESET_NUM_IN_REG;
58 offset = id % ROCKCHIP_RESET_NUM_IN_REG;
Elaine Zhang760188c2017-12-19 18:22:37 +080059
60 debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_addr=%p)\n", __func__,
Eugen Hristevbb52f1c2023-05-15 13:55:04 +030061 reset_ctl, reset_ctl->dev, id, priv->base + (bank * 4));
Elaine Zhang760188c2017-12-19 18:22:37 +080062
63 rk_setreg(priv->base + (bank * 4), BIT(offset));
64
65 return 0;
66}
67
68static int rockchip_reset_deassert(struct reset_ctl *reset_ctl)
69{
70 struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev);
Eugen Hristevbb52f1c2023-05-15 13:55:04 +030071 unsigned long id = reset_ctl->id;
72 int bank, offset;
73
74 if (priv->lut)
75 id = priv->lut[id];
76
77 bank = id / ROCKCHIP_RESET_NUM_IN_REG;
78 offset = id % ROCKCHIP_RESET_NUM_IN_REG;
Elaine Zhang760188c2017-12-19 18:22:37 +080079
80 debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_addr=%p)\n", __func__,
Eugen Hristevbb52f1c2023-05-15 13:55:04 +030081 reset_ctl, reset_ctl->dev, id, priv->base + (bank * 4));
Elaine Zhang760188c2017-12-19 18:22:37 +080082
83 rk_clrreg(priv->base + (bank * 4), BIT(offset));
84
85 return 0;
86}
87
88struct reset_ops rockchip_reset_ops = {
89 .request = rockchip_reset_request,
Elaine Zhang760188c2017-12-19 18:22:37 +080090 .rst_assert = rockchip_reset_assert,
91 .rst_deassert = rockchip_reset_deassert,
92};
93
94static int rockchip_reset_probe(struct udevice *dev)
95{
96 struct rockchip_reset_priv *priv = dev_get_priv(dev);
97 fdt_addr_t addr;
98 fdt_size_t size;
99
100 addr = dev_read_addr_size(dev, "reg", &size);
101 if (addr == FDT_ADDR_T_NONE)
102 return -EINVAL;
103
104 if ((priv->reset_reg_offset == 0) && (priv->reset_reg_num == 0))
105 return -EINVAL;
106
107 addr += priv->reset_reg_offset;
108 priv->base = ioremap(addr, size);
109
110 debug("%s(base=%p) (reg_offset=%x, reg_num=%d)\n", __func__,
111 priv->base, priv->reset_reg_offset, priv->reset_reg_num);
112
113 return 0;
114}
115
Eugen Hristevbb52f1c2023-05-15 13:55:04 +0300116int rockchip_reset_bind_lut(struct udevice *pdev,
117 const int *lookup_table,
118 u32 reg_offset,
119 u32 reg_number)
Elaine Zhang760188c2017-12-19 18:22:37 +0800120{
121 struct udevice *rst_dev;
122 struct rockchip_reset_priv *priv;
123 int ret;
124
Eugen Hristev4a08ca62023-04-11 10:20:40 +0300125 ret = device_bind_driver_to_node(pdev, "rockchip_reset", "reset",
126 dev_ofnode(pdev), &rst_dev);
Elaine Zhang760188c2017-12-19 18:22:37 +0800127 if (ret) {
128 debug("Warning: No rockchip reset driver: ret=%d\n", ret);
129 return ret;
130 }
131 priv = malloc(sizeof(struct rockchip_reset_priv));
132 priv->reset_reg_offset = reg_offset;
133 priv->reset_reg_num = reg_number;
Eugen Hristevbb52f1c2023-05-15 13:55:04 +0300134 priv->lut = lookup_table;
Simon Glass0fd3d912020-12-22 19:30:28 -0700135 dev_set_priv(rst_dev, priv);
Elaine Zhang760188c2017-12-19 18:22:37 +0800136
137 return 0;
138}
139
Eugen Hristevbb52f1c2023-05-15 13:55:04 +0300140int rockchip_reset_bind(struct udevice *pdev, u32 reg_offset, u32 reg_number)
141{
142 return rockchip_reset_bind_lut(pdev, NULL, reg_offset, reg_number);
143}
144
Elaine Zhang760188c2017-12-19 18:22:37 +0800145U_BOOT_DRIVER(rockchip_reset) = {
146 .name = "rockchip_reset",
147 .id = UCLASS_RESET,
148 .probe = rockchip_reset_probe,
149 .ops = &rockchip_reset_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700150 .priv_auto = sizeof(struct rockchip_reset_priv),
Elaine Zhang760188c2017-12-19 18:22:37 +0800151};