Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0+ OR MIT) |
| 2 | /* |
| 3 | * Copyright (C) 2018 Amarula Solutions. |
| 4 | * Author: Jagan Teki <jagan@amarulasolutions.com> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 12 | #include <reset-uclass.h> |
| 13 | #include <asm/io.h> |
Samuel Holland | 12e3faa | 2021-09-12 11:48:43 -0500 | [diff] [blame] | 14 | #include <clk/sunxi.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 16 | #include <dm/lists.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 18 | #include <linux/log2.h> |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 19 | |
| 20 | struct sunxi_reset_priv { |
| 21 | void *base; |
| 22 | ulong count; |
| 23 | const struct ccu_desc *desc; |
| 24 | }; |
| 25 | |
| 26 | static const struct ccu_reset *priv_to_reset(struct sunxi_reset_priv *priv, |
| 27 | unsigned long id) |
| 28 | { |
| 29 | return &priv->desc->resets[id]; |
| 30 | } |
| 31 | |
| 32 | static int sunxi_reset_request(struct reset_ctl *reset_ctl) |
| 33 | { |
| 34 | struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
| 35 | |
| 36 | debug("%s: (RST#%ld)\n", __func__, reset_ctl->id); |
| 37 | |
| 38 | if (reset_ctl->id >= priv->count) |
| 39 | return -EINVAL; |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 44 | static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on) |
| 45 | { |
| 46 | struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
| 47 | const struct ccu_reset *reset = priv_to_reset(priv, reset_ctl->id); |
| 48 | u32 reg; |
| 49 | |
| 50 | if (!(reset->flags & CCU_RST_F_IS_VALID)) { |
| 51 | printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__, |
| 56 | reset_ctl->id, reset->off, ilog2(reset->bit)); |
| 57 | |
| 58 | reg = readl(priv->base + reset->off); |
| 59 | if (on) |
| 60 | reg |= reset->bit; |
| 61 | else |
| 62 | reg &= ~reset->bit; |
| 63 | |
| 64 | writel(reg, priv->base + reset->off); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int sunxi_reset_assert(struct reset_ctl *reset_ctl) |
| 70 | { |
| 71 | return sunxi_set_reset(reset_ctl, false); |
| 72 | } |
| 73 | |
| 74 | static int sunxi_reset_deassert(struct reset_ctl *reset_ctl) |
| 75 | { |
| 76 | return sunxi_set_reset(reset_ctl, true); |
| 77 | } |
| 78 | |
| 79 | struct reset_ops sunxi_reset_ops = { |
| 80 | .request = sunxi_reset_request, |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 81 | .rst_assert = sunxi_reset_assert, |
| 82 | .rst_deassert = sunxi_reset_deassert, |
| 83 | }; |
| 84 | |
| 85 | static int sunxi_reset_probe(struct udevice *dev) |
| 86 | { |
| 87 | struct sunxi_reset_priv *priv = dev_get_priv(dev); |
| 88 | |
| 89 | priv->base = dev_read_addr_ptr(dev); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int sunxi_reset_bind(struct udevice *dev, ulong count) |
| 95 | { |
| 96 | struct udevice *rst_dev; |
| 97 | struct sunxi_reset_priv *priv; |
| 98 | int ret; |
| 99 | |
| 100 | ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset", |
| 101 | dev_ofnode(dev), &rst_dev); |
| 102 | if (ret) { |
| 103 | debug("failed to bind sunxi_reset driver (ret=%d)\n", ret); |
| 104 | return ret; |
| 105 | } |
| 106 | priv = malloc(sizeof(struct sunxi_reset_priv)); |
| 107 | priv->count = count; |
| 108 | priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 109 | dev_set_priv(rst_dev, priv); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | U_BOOT_DRIVER(sunxi_reset) = { |
| 115 | .name = "sunxi_reset", |
| 116 | .id = UCLASS_RESET, |
| 117 | .ops = &sunxi_reset_ops, |
| 118 | .probe = sunxi_reset_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 119 | .priv_auto = sizeof(struct sunxi_reset_priv), |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 120 | }; |