Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017 Rockchip Electronics Co., Ltd |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 10 | #include <reset-uclass.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 12 | #include <linux/io.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 13 | #include <asm/arch-rockchip/hardware.h> |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 15 | #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 | |
| 22 | struct rockchip_reset_priv { |
| 23 | void __iomem *base; |
Eugen Hristev | bb52f1c | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 24 | const int *lut; |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 25 | /* Rockchip reset reg locate at cru controller */ |
| 26 | u32 reset_reg_offset; |
| 27 | /* Rockchip reset reg number */ |
| 28 | u32 reset_reg_num; |
| 29 | }; |
| 30 | |
| 31 | static int rockchip_reset_request(struct reset_ctl *reset_ctl) |
| 32 | { |
| 33 | struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
Eugen Hristev | bb52f1c | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 34 | unsigned long id = reset_ctl->id; |
| 35 | |
| 36 | if (priv->lut) |
| 37 | id = priv->lut[id]; |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 38 | |
| 39 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_num=%d)\n", __func__, |
Eugen Hristev | bb52f1c | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 40 | reset_ctl, reset_ctl->dev, id, priv->reset_reg_num); |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 41 | |
Eugen Hristev | bb52f1c | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 42 | if (id / ROCKCHIP_RESET_NUM_IN_REG >= priv->reset_reg_num) |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 43 | return -EINVAL; |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 48 | static int rockchip_reset_assert(struct reset_ctl *reset_ctl) |
| 49 | { |
| 50 | struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
Eugen Hristev | bb52f1c | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 51 | 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 Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 59 | |
| 60 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_addr=%p)\n", __func__, |
Eugen Hristev | bb52f1c | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 61 | reset_ctl, reset_ctl->dev, id, priv->base + (bank * 4)); |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 62 | |
| 63 | rk_setreg(priv->base + (bank * 4), BIT(offset)); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int rockchip_reset_deassert(struct reset_ctl *reset_ctl) |
| 69 | { |
| 70 | struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
Eugen Hristev | bb52f1c | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 71 | 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 Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 79 | |
| 80 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_addr=%p)\n", __func__, |
Eugen Hristev | bb52f1c | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 81 | reset_ctl, reset_ctl->dev, id, priv->base + (bank * 4)); |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 82 | |
| 83 | rk_clrreg(priv->base + (bank * 4), BIT(offset)); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | struct reset_ops rockchip_reset_ops = { |
| 89 | .request = rockchip_reset_request, |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 90 | .rst_assert = rockchip_reset_assert, |
| 91 | .rst_deassert = rockchip_reset_deassert, |
| 92 | }; |
| 93 | |
| 94 | static 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 Hristev | bb52f1c | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 116 | int rockchip_reset_bind_lut(struct udevice *pdev, |
| 117 | const int *lookup_table, |
| 118 | u32 reg_offset, |
| 119 | u32 reg_number) |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 120 | { |
| 121 | struct udevice *rst_dev; |
| 122 | struct rockchip_reset_priv *priv; |
| 123 | int ret; |
| 124 | |
Eugen Hristev | 4a08ca6 | 2023-04-11 10:20:40 +0300 | [diff] [blame] | 125 | ret = device_bind_driver_to_node(pdev, "rockchip_reset", "reset", |
| 126 | dev_ofnode(pdev), &rst_dev); |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 127 | 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 Hristev | bb52f1c | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 134 | priv->lut = lookup_table; |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 135 | dev_set_priv(rst_dev, priv); |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Eugen Hristev | bb52f1c | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 140 | int 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 Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 145 | U_BOOT_DRIVER(rockchip_reset) = { |
| 146 | .name = "rockchip_reset", |
| 147 | .id = UCLASS_RESET, |
| 148 | .probe = rockchip_reset_probe, |
| 149 | .ops = &rockchip_reset_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 150 | .priv_auto = sizeof(struct rockchip_reset_priv), |
Elaine Zhang | 760188c | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 151 | }; |