1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2020 Sifive, Inc.
4 * Author: Sagar Kadam <sagar.kadam@sifive.com>
5 */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <reset-uclass.h>
10 #include <asm/io.h>
11 #include <dm/device_compat.h>
12 #include <dm/device-internal.h>
13 #include <dm/lists.h>
14 #include <linux/bitops.h>
15
16 #define PRCI_RESETREG_OFFSET 0x28
17
18 struct sifive_reset_priv {
19 void *base;
20 /* number of reset signals */
21 int nr_reset;
22 };
23
sifive_rst_trigger(struct reset_ctl * rst,bool level)24 static int sifive_rst_trigger(struct reset_ctl *rst, bool level)
25 {
26 struct sifive_reset_priv *priv = dev_get_priv(rst->dev);
27 int id = rst->id;
28 int regval = readl(priv->base + PRCI_RESETREG_OFFSET);
29
30 /* Derive bitposition from rst id */
31 if (level)
32 /* Reset deassert */
33 regval |= BIT(id);
34 else
35 /* Reset assert */
36 regval &= ~BIT(id);
37
38 writel(regval, priv->base + PRCI_RESETREG_OFFSET);
39
40 return 0;
41 }
42
sifive_reset_assert(struct reset_ctl * rst)43 static int sifive_reset_assert(struct reset_ctl *rst)
44 {
45 return sifive_rst_trigger(rst, false);
46 }
47
sifive_reset_deassert(struct reset_ctl * rst)48 static int sifive_reset_deassert(struct reset_ctl *rst)
49 {
50 return sifive_rst_trigger(rst, true);
51 }
52
sifive_reset_request(struct reset_ctl * rst)53 static int sifive_reset_request(struct reset_ctl *rst)
54 {
55 struct sifive_reset_priv *priv = dev_get_priv(rst->dev);
56
57 debug("%s(rst=%p) (dev=%p, id=%lu) (nr_reset=%d)\n", __func__,
58 rst, rst->dev, rst->id, priv->nr_reset);
59
60 if (rst->id > priv->nr_reset)
61 return -EINVAL;
62
63 return 0;
64 }
65
sifive_reset_free(struct reset_ctl * rst)66 static int sifive_reset_free(struct reset_ctl *rst)
67 {
68 struct sifive_reset_priv *priv = dev_get_priv(rst->dev);
69
70 debug("%s(rst=%p) (dev=%p, id=%lu) (nr_reset=%d)\n", __func__,
71 rst, rst->dev, rst->id, priv->nr_reset);
72
73 return 0;
74 }
75
sifive_reset_probe(struct udevice * dev)76 static int sifive_reset_probe(struct udevice *dev)
77 {
78 struct sifive_reset_priv *priv = dev_get_priv(dev);
79
80 priv->base = dev_remap_addr(dev);
81 if (!priv->base)
82 return -ENOMEM;
83
84 return 0;
85 }
86
sifive_reset_bind(struct udevice * dev,ulong count)87 int sifive_reset_bind(struct udevice *dev, ulong count)
88 {
89 struct udevice *rst_dev;
90 struct sifive_reset_priv *priv;
91 int ret;
92
93 ret = device_bind_driver_to_node(dev, "sifive-reset", "reset",
94 dev_ofnode(dev), &rst_dev);
95 if (ret) {
96 dev_err(dev, "failed to bind sifive_reset driver (ret=%d)\n", ret);
97 return ret;
98 }
99 priv = malloc(sizeof(struct sifive_reset_priv));
100 priv->nr_reset = count;
101 dev_set_priv(rst_dev, priv);
102
103 return 0;
104 }
105
106 const struct reset_ops sifive_reset_ops = {
107 .request = sifive_reset_request,
108 .rfree = sifive_reset_free,
109 .rst_assert = sifive_reset_assert,
110 .rst_deassert = sifive_reset_deassert,
111 };
112
113 U_BOOT_DRIVER(sifive_reset) = {
114 .name = "sifive-reset",
115 .id = UCLASS_RESET,
116 .ops = &sifive_reset_ops,
117 .probe = sifive_reset_probe,
118 .priv_auto = sizeof(struct sifive_reset_priv),
119 };
120