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