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 | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 16 | #include <linux/log2.h> |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 17 | |
Samuel Holland | 86b561c | 2022-05-09 00:29:37 -0500 | [diff] [blame] | 18 | static const struct ccu_reset *plat_to_reset(struct ccu_plat *plat, |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 19 | unsigned long id) |
| 20 | { |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame] | 21 | return &plat->desc->resets[id]; |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | static int sunxi_reset_request(struct reset_ctl *reset_ctl) |
| 25 | { |
Samuel Holland | 86b561c | 2022-05-09 00:29:37 -0500 | [diff] [blame] | 26 | struct ccu_plat *plat = dev_get_plat(reset_ctl->dev); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 27 | |
| 28 | debug("%s: (RST#%ld)\n", __func__, reset_ctl->id); |
| 29 | |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame] | 30 | if (reset_ctl->id >= plat->desc->num_resets) |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 31 | return -EINVAL; |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 36 | static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on) |
| 37 | { |
Samuel Holland | 86b561c | 2022-05-09 00:29:37 -0500 | [diff] [blame] | 38 | struct ccu_plat *plat = dev_get_plat(reset_ctl->dev); |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame] | 39 | const struct ccu_reset *reset = plat_to_reset(plat, reset_ctl->id); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 40 | u32 reg; |
| 41 | |
| 42 | if (!(reset->flags & CCU_RST_F_IS_VALID)) { |
| 43 | printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id); |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__, |
| 48 | reset_ctl->id, reset->off, ilog2(reset->bit)); |
| 49 | |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame] | 50 | reg = readl(plat->base + reset->off); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 51 | if (on) |
| 52 | reg |= reset->bit; |
| 53 | else |
| 54 | reg &= ~reset->bit; |
| 55 | |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame] | 56 | writel(reg, plat->base + reset->off); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int sunxi_reset_assert(struct reset_ctl *reset_ctl) |
| 62 | { |
| 63 | return sunxi_set_reset(reset_ctl, false); |
| 64 | } |
| 65 | |
| 66 | static int sunxi_reset_deassert(struct reset_ctl *reset_ctl) |
| 67 | { |
| 68 | return sunxi_set_reset(reset_ctl, true); |
| 69 | } |
| 70 | |
| 71 | struct reset_ops sunxi_reset_ops = { |
| 72 | .request = sunxi_reset_request, |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 73 | .rst_assert = sunxi_reset_assert, |
| 74 | .rst_deassert = sunxi_reset_deassert, |
| 75 | }; |
| 76 | |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 77 | U_BOOT_DRIVER(sunxi_reset) = { |
| 78 | .name = "sunxi_reset", |
| 79 | .id = UCLASS_RESET, |
| 80 | .ops = &sunxi_reset_ops, |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 81 | }; |