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