Jagan Teki | 99ba430 | 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 | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
Jagan Teki | 99ba430 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 12 | #include <reset-uclass.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <dm/lists.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Jagan Teki | 99ba430 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 16 | #include <linux/log2.h> |
| 17 | #include <asm/arch/ccu.h> |
| 18 | |
| 19 | struct sunxi_reset_priv { |
| 20 | void *base; |
| 21 | ulong count; |
| 22 | const struct ccu_desc *desc; |
| 23 | }; |
| 24 | |
| 25 | static const struct ccu_reset *priv_to_reset(struct sunxi_reset_priv *priv, |
| 26 | unsigned long id) |
| 27 | { |
| 28 | return &priv->desc->resets[id]; |
| 29 | } |
| 30 | |
| 31 | static int sunxi_reset_request(struct reset_ctl *reset_ctl) |
| 32 | { |
| 33 | struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
| 34 | |
| 35 | debug("%s: (RST#%ld)\n", __func__, reset_ctl->id); |
| 36 | |
| 37 | if (reset_ctl->id >= priv->count) |
| 38 | return -EINVAL; |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int sunxi_reset_free(struct reset_ctl *reset_ctl) |
| 44 | { |
| 45 | debug("%s: (RST#%ld)\n", __func__, reset_ctl->id); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on) |
| 51 | { |
| 52 | struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
| 53 | const struct ccu_reset *reset = priv_to_reset(priv, reset_ctl->id); |
| 54 | u32 reg; |
| 55 | |
| 56 | if (!(reset->flags & CCU_RST_F_IS_VALID)) { |
| 57 | printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__, |
| 62 | reset_ctl->id, reset->off, ilog2(reset->bit)); |
| 63 | |
| 64 | reg = readl(priv->base + reset->off); |
| 65 | if (on) |
| 66 | reg |= reset->bit; |
| 67 | else |
| 68 | reg &= ~reset->bit; |
| 69 | |
| 70 | writel(reg, priv->base + reset->off); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int sunxi_reset_assert(struct reset_ctl *reset_ctl) |
| 76 | { |
| 77 | return sunxi_set_reset(reset_ctl, false); |
| 78 | } |
| 79 | |
| 80 | static int sunxi_reset_deassert(struct reset_ctl *reset_ctl) |
| 81 | { |
| 82 | return sunxi_set_reset(reset_ctl, true); |
| 83 | } |
| 84 | |
| 85 | struct reset_ops sunxi_reset_ops = { |
| 86 | .request = sunxi_reset_request, |
Simon Glass | 94474b2 | 2020-02-03 07:35:52 -0700 | [diff] [blame] | 87 | .rfree = sunxi_reset_free, |
Jagan Teki | 99ba430 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 88 | .rst_assert = sunxi_reset_assert, |
| 89 | .rst_deassert = sunxi_reset_deassert, |
| 90 | }; |
| 91 | |
| 92 | static int sunxi_reset_probe(struct udevice *dev) |
| 93 | { |
| 94 | struct sunxi_reset_priv *priv = dev_get_priv(dev); |
| 95 | |
| 96 | priv->base = dev_read_addr_ptr(dev); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | int sunxi_reset_bind(struct udevice *dev, ulong count) |
| 102 | { |
| 103 | struct udevice *rst_dev; |
| 104 | struct sunxi_reset_priv *priv; |
| 105 | int ret; |
| 106 | |
| 107 | ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset", |
| 108 | dev_ofnode(dev), &rst_dev); |
| 109 | if (ret) { |
| 110 | debug("failed to bind sunxi_reset driver (ret=%d)\n", ret); |
| 111 | return ret; |
| 112 | } |
| 113 | priv = malloc(sizeof(struct sunxi_reset_priv)); |
| 114 | priv->count = count; |
| 115 | priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev); |
| 116 | rst_dev->priv = priv; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | U_BOOT_DRIVER(sunxi_reset) = { |
| 122 | .name = "sunxi_reset", |
| 123 | .id = UCLASS_RESET, |
| 124 | .ops = &sunxi_reset_ops, |
| 125 | .probe = sunxi_reset_probe, |
| 126 | .priv_auto_alloc_size = sizeof(struct sunxi_reset_priv), |
| 127 | }; |