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 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 10 | #include <reset-uclass.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 12 | #include <linux/io.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 13 | #include <asm/arch-rockchip/hardware.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame^] | 14 | #include <dm/device-internal.h> |
Elaine Zhang | 6e9a3a7 | 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; |
| 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); |
| 33 | |
| 34 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_num=%d)\n", __func__, |
| 35 | reset_ctl, reset_ctl->dev, reset_ctl->id, priv->reset_reg_num); |
| 36 | |
| 37 | if (reset_ctl->id / ROCKCHIP_RESET_NUM_IN_REG >= priv->reset_reg_num) |
| 38 | return -EINVAL; |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int rockchip_reset_free(struct reset_ctl *reset_ctl) |
| 44 | { |
| 45 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu)\n", __func__, reset_ctl, |
| 46 | reset_ctl->dev, reset_ctl->id); |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int rockchip_reset_assert(struct reset_ctl *reset_ctl) |
| 52 | { |
| 53 | struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
| 54 | int bank = reset_ctl->id / ROCKCHIP_RESET_NUM_IN_REG; |
| 55 | int offset = reset_ctl->id % ROCKCHIP_RESET_NUM_IN_REG; |
| 56 | |
| 57 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_addr=%p)\n", __func__, |
| 58 | reset_ctl, reset_ctl->dev, reset_ctl->id, |
| 59 | priv->base + (bank * 4)); |
| 60 | |
| 61 | rk_setreg(priv->base + (bank * 4), BIT(offset)); |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int rockchip_reset_deassert(struct reset_ctl *reset_ctl) |
| 67 | { |
| 68 | struct rockchip_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
| 69 | int bank = reset_ctl->id / ROCKCHIP_RESET_NUM_IN_REG; |
| 70 | int offset = reset_ctl->id % ROCKCHIP_RESET_NUM_IN_REG; |
| 71 | |
| 72 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu) (reg_addr=%p)\n", __func__, |
| 73 | reset_ctl, reset_ctl->dev, reset_ctl->id, |
| 74 | priv->base + (bank * 4)); |
| 75 | |
| 76 | rk_clrreg(priv->base + (bank * 4), BIT(offset)); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | struct reset_ops rockchip_reset_ops = { |
| 82 | .request = rockchip_reset_request, |
Simon Glass | 1928cd4 | 2020-02-03 07:35:52 -0700 | [diff] [blame] | 83 | .rfree = rockchip_reset_free, |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 84 | .rst_assert = rockchip_reset_assert, |
| 85 | .rst_deassert = rockchip_reset_deassert, |
| 86 | }; |
| 87 | |
| 88 | static int rockchip_reset_probe(struct udevice *dev) |
| 89 | { |
| 90 | struct rockchip_reset_priv *priv = dev_get_priv(dev); |
| 91 | fdt_addr_t addr; |
| 92 | fdt_size_t size; |
| 93 | |
| 94 | addr = dev_read_addr_size(dev, "reg", &size); |
| 95 | if (addr == FDT_ADDR_T_NONE) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | if ((priv->reset_reg_offset == 0) && (priv->reset_reg_num == 0)) |
| 99 | return -EINVAL; |
| 100 | |
| 101 | addr += priv->reset_reg_offset; |
| 102 | priv->base = ioremap(addr, size); |
| 103 | |
| 104 | debug("%s(base=%p) (reg_offset=%x, reg_num=%d)\n", __func__, |
| 105 | priv->base, priv->reset_reg_offset, priv->reset_reg_num); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int rockchip_reset_bind(struct udevice *pdev, u32 reg_offset, u32 reg_number) |
| 111 | { |
| 112 | struct udevice *rst_dev; |
| 113 | struct rockchip_reset_priv *priv; |
| 114 | int ret; |
| 115 | |
| 116 | ret = device_bind_driver_to_node(pdev, "rockchip_reset", "reset", |
| 117 | dev_ofnode(pdev), &rst_dev); |
| 118 | if (ret) { |
| 119 | debug("Warning: No rockchip reset driver: ret=%d\n", ret); |
| 120 | return ret; |
| 121 | } |
| 122 | priv = malloc(sizeof(struct rockchip_reset_priv)); |
| 123 | priv->reset_reg_offset = reg_offset; |
| 124 | priv->reset_reg_num = reg_number; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame^] | 125 | dev_set_priv(rst_dev, priv); |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | U_BOOT_DRIVER(rockchip_reset) = { |
| 131 | .name = "rockchip_reset", |
| 132 | .id = UCLASS_RESET, |
| 133 | .probe = rockchip_reset_probe, |
| 134 | .ops = &rockchip_reset_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 135 | .priv_auto = sizeof(struct rockchip_reset_priv), |
Elaine Zhang | 6e9a3a7 | 2017-12-19 18:22:37 +0800 | [diff] [blame] | 136 | }; |