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