Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017 Rockchip Electronics Co., Ltd |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 6 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 8 | #include <malloc.h> |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 9 | #include <reset-uclass.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 11 | #include <linux/io.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 12 | #include <asm/arch-rockchip/hardware.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 14 | #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 | |
| 21 | struct rockchip_reset_priv { |
| 22 | void __iomem *base; |
Eugen Hristev | 2f55082 | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 23 | const int *lut; |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 24 | /* Rockchip reset reg locate at cru controller */ |
| 25 | u32 reset_reg_offset; |
| 26 | /* Rockchip reset reg number */ |
| 27 | u32 reset_reg_num; |
| 28 | }; |
| 29 | |
| 30 | static int rockchip_reset_request(struct reset_ctl *reset_ctl) |
| 31 | { |
| 32 | struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
Eugen Hristev | 2f55082 | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 33 | unsigned long id = reset_ctl->id; |
| 34 | |
| 35 | if (priv->lut) |
| 36 | id = priv->lut[id]; |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 37 | |
| 38 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_num=%d)\n", __func__, |
Eugen Hristev | 2f55082 | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 39 | reset_ctl, reset_ctl->dev, id, priv->reset_reg_num); |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 40 | |
Eugen Hristev | 2f55082 | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 41 | if (id / ROCKCHIP_RESET_NUM_IN_REG >= priv->reset_reg_num) |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 42 | return -EINVAL; |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 47 | static int rockchip_reset_assert(struct reset_ctl *reset_ctl) |
| 48 | { |
| 49 | struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
Eugen Hristev | 2f55082 | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 50 | unsigned long id = reset_ctl->id; |
| 51 | int bank, offset; |
| 52 | |
| 53 | if (priv->lut) |
| 54 | id = priv->lut[id]; |
| 55 | |
| 56 | bank = id / ROCKCHIP_RESET_NUM_IN_REG; |
| 57 | offset = id % ROCKCHIP_RESET_NUM_IN_REG; |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 58 | |
| 59 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_addr=%p)\n", __func__, |
Eugen Hristev | 2f55082 | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 60 | reset_ctl, reset_ctl->dev, id, priv->base + (bank * 4)); |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 61 | |
| 62 | rk_setreg(priv->base + (bank * 4), BIT(offset)); |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int rockchip_reset_deassert(struct reset_ctl *reset_ctl) |
| 68 | { |
| 69 | struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
Eugen Hristev | 2f55082 | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 70 | unsigned long id = reset_ctl->id; |
| 71 | int bank, offset; |
| 72 | |
| 73 | if (priv->lut) |
| 74 | id = priv->lut[id]; |
| 75 | |
| 76 | bank = id / ROCKCHIP_RESET_NUM_IN_REG; |
| 77 | offset = id % ROCKCHIP_RESET_NUM_IN_REG; |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 78 | |
| 79 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_addr=%p)\n", __func__, |
Eugen Hristev | 2f55082 | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 80 | reset_ctl, reset_ctl->dev, id, priv->base + (bank * 4)); |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 81 | |
| 82 | rk_clrreg(priv->base + (bank * 4), BIT(offset)); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | struct reset_ops rockchip_reset_ops = { |
| 88 | .request = rockchip_reset_request, |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 89 | .rst_assert = rockchip_reset_assert, |
| 90 | .rst_deassert = rockchip_reset_deassert, |
| 91 | }; |
| 92 | |
| 93 | static int rockchip_reset_probe(struct udevice *dev) |
| 94 | { |
| 95 | struct rockchip_reset_priv *priv = dev_get_priv(dev); |
| 96 | fdt_addr_t addr; |
| 97 | fdt_size_t size; |
| 98 | |
John Keeping | ab62d0b | 2023-06-01 15:11:19 +0100 | [diff] [blame] | 99 | addr = dev_read_addr_size(dev, &size); |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 100 | if (addr == FDT_ADDR_T_NONE) |
| 101 | return -EINVAL; |
| 102 | |
| 103 | if ((priv->reset_reg_offset == 0) && (priv->reset_reg_num == 0)) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | addr += priv->reset_reg_offset; |
| 107 | priv->base = ioremap(addr, size); |
| 108 | |
| 109 | debug("%s(base=%p) (reg_offset=%x, reg_num=%d)\n", __func__, |
| 110 | priv->base, priv->reset_reg_offset, priv->reset_reg_num); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Eugen Hristev | 2f55082 | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 115 | int rockchip_reset_bind_lut(struct udevice *pdev, |
| 116 | const int *lookup_table, |
| 117 | u32 reg_offset, |
| 118 | u32 reg_number) |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 119 | { |
| 120 | struct udevice *rst_dev; |
| 121 | struct rockchip_reset_priv *priv; |
| 122 | int ret; |
| 123 | |
Eugen Hristev | ca4134a | 2023-04-11 10:20:40 +0300 | [diff] [blame] | 124 | ret = device_bind_driver_to_node(pdev, "rockchip_reset", "reset", |
| 125 | dev_ofnode(pdev), &rst_dev); |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 126 | if (ret) { |
| 127 | debug("Warning: No rockchip reset driver: ret=%d\n", ret); |
| 128 | return ret; |
| 129 | } |
| 130 | priv = malloc(sizeof(struct rockchip_reset_priv)); |
| 131 | priv->reset_reg_offset = reg_offset; |
| 132 | priv->reset_reg_num = reg_number; |
Eugen Hristev | 2f55082 | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 133 | priv->lut = lookup_table; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 134 | dev_set_priv(rst_dev, priv); |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Eugen Hristev | 2f55082 | 2023-05-15 13:55:04 +0300 | [diff] [blame] | 139 | int rockchip_reset_bind(struct udevice *pdev, u32 reg_offset, u32 reg_number) |
| 140 | { |
| 141 | return rockchip_reset_bind_lut(pdev, NULL, reg_offset, reg_number); |
| 142 | } |
| 143 | |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 144 | U_BOOT_DRIVER(rockchip_reset) = { |
| 145 | .name = "rockchip_reset", |
| 146 | .id = UCLASS_RESET, |
| 147 | .probe = rockchip_reset_probe, |
| 148 | .ops = &rockchip_reset_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 149 | .priv_auto = sizeof(struct rockchip_reset_priv), |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 150 | }; |