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