blob: 23513b2f541b4b0cbd30198f47a2b949efc24705 [file] [log] [blame]
Sagar Shrikant Kadamd04a4642020-07-29 02:36:13 -07001// 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>
Simon Glass0fd3d912020-12-22 19:30:28 -070012#include <dm/device-internal.h>
Sagar Shrikant Kadamd04a4642020-07-29 02:36:13 -070013#include <dm/lists.h>
14#include <linux/bitops.h>
15
16#define PRCI_RESETREG_OFFSET 0x28
17
18struct sifive_reset_priv {
19 void *base;
20 /* number of reset signals */
21 int nr_reset;
22};
23
24static 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
43static int sifive_reset_assert(struct reset_ctl *rst)
44{
45 return sifive_rst_trigger(rst, false);
46}
47
48static int sifive_reset_deassert(struct reset_ctl *rst)
49{
50 return sifive_rst_trigger(rst, true);
51}
52
53static 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
Sagar Shrikant Kadamd04a4642020-07-29 02:36:13 -070066static int sifive_reset_probe(struct udevice *dev)
67{
68 struct sifive_reset_priv *priv = dev_get_priv(dev);
69
70 priv->base = dev_remap_addr(dev);
71 if (!priv->base)
72 return -ENOMEM;
73
74 return 0;
75}
76
77int sifive_reset_bind(struct udevice *dev, ulong count)
78{
79 struct udevice *rst_dev;
80 struct sifive_reset_priv *priv;
81 int ret;
82
83 ret = device_bind_driver_to_node(dev, "sifive-reset", "reset",
84 dev_ofnode(dev), &rst_dev);
85 if (ret) {
86 dev_err(dev, "failed to bind sifive_reset driver (ret=%d)\n", ret);
87 return ret;
88 }
89 priv = malloc(sizeof(struct sifive_reset_priv));
90 priv->nr_reset = count;
Simon Glass0fd3d912020-12-22 19:30:28 -070091 dev_set_priv(rst_dev, priv);
Sagar Shrikant Kadamd04a4642020-07-29 02:36:13 -070092
93 return 0;
94}
95
96const struct reset_ops sifive_reset_ops = {
97 .request = sifive_reset_request,
Sagar Shrikant Kadamd04a4642020-07-29 02:36:13 -070098 .rst_assert = sifive_reset_assert,
99 .rst_deassert = sifive_reset_deassert,
100};
101
102U_BOOT_DRIVER(sifive_reset) = {
103 .name = "sifive-reset",
104 .id = UCLASS_RESET,
105 .ops = &sifive_reset_ops,
106 .probe = sifive_reset_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700107 .priv_auto = sizeof(struct sifive_reset_priv),
Sagar Shrikant Kadamd04a4642020-07-29 02:36:13 -0700108};