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