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 | |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 20 | struct sunxi_reset_plat { |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 21 | void *base; |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 22 | const struct ccu_desc *desc; |
| 23 | }; |
| 24 | |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 25 | static const struct ccu_reset *plat_to_reset(struct sunxi_reset_plat *plat, |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 26 | unsigned long id) |
| 27 | { |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 28 | return &plat->desc->resets[id]; |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static int sunxi_reset_request(struct reset_ctl *reset_ctl) |
| 32 | { |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 33 | struct sunxi_reset_plat *plat = dev_get_plat(reset_ctl->dev); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 34 | |
| 35 | debug("%s: (RST#%ld)\n", __func__, reset_ctl->id); |
| 36 | |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 37 | if (reset_ctl->id >= plat->desc->num_resets) |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 38 | return -EINVAL; |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 43 | static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on) |
| 44 | { |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 45 | struct sunxi_reset_plat *plat = dev_get_plat(reset_ctl->dev); |
| 46 | const struct ccu_reset *reset = plat_to_reset(plat, reset_ctl->id); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 47 | u32 reg; |
| 48 | |
| 49 | if (!(reset->flags & CCU_RST_F_IS_VALID)) { |
| 50 | printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__, |
| 55 | reset_ctl->id, reset->off, ilog2(reset->bit)); |
| 56 | |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 57 | reg = readl(plat->base + reset->off); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 58 | if (on) |
| 59 | reg |= reset->bit; |
| 60 | else |
| 61 | reg &= ~reset->bit; |
| 62 | |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 63 | writel(reg, plat->base + reset->off); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int sunxi_reset_assert(struct reset_ctl *reset_ctl) |
| 69 | { |
| 70 | return sunxi_set_reset(reset_ctl, false); |
| 71 | } |
| 72 | |
| 73 | static int sunxi_reset_deassert(struct reset_ctl *reset_ctl) |
| 74 | { |
| 75 | return sunxi_set_reset(reset_ctl, true); |
| 76 | } |
| 77 | |
| 78 | struct reset_ops sunxi_reset_ops = { |
| 79 | .request = sunxi_reset_request, |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 80 | .rst_assert = sunxi_reset_assert, |
| 81 | .rst_deassert = sunxi_reset_deassert, |
| 82 | }; |
| 83 | |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 84 | static int sunxi_reset_of_to_plat(struct udevice *dev) |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 85 | { |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 86 | struct sunxi_reset_plat *plat = dev_get_plat(dev); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 87 | |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 88 | plat->base = dev_read_addr_ptr(dev); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Samuel Holland | 1567fdf | 2022-05-09 00:29:33 -0500 | [diff] [blame] | 93 | int sunxi_reset_bind(struct udevice *dev) |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 94 | { |
| 95 | struct udevice *rst_dev; |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 96 | struct sunxi_reset_plat *plat; |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 97 | int ret; |
| 98 | |
| 99 | ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset", |
| 100 | dev_ofnode(dev), &rst_dev); |
| 101 | if (ret) { |
| 102 | debug("failed to bind sunxi_reset driver (ret=%d)\n", ret); |
| 103 | return ret; |
| 104 | } |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 105 | plat = malloc(sizeof(struct sunxi_reset_plat)); |
| 106 | plat->desc = (const struct ccu_desc *)dev_get_driver_data(dev); |
| 107 | dev_set_plat(rst_dev, plat); |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | U_BOOT_DRIVER(sunxi_reset) = { |
| 113 | .name = "sunxi_reset", |
| 114 | .id = UCLASS_RESET, |
| 115 | .ops = &sunxi_reset_ops, |
Samuel Holland | 24a870c | 2022-05-09 00:29:36 -0500 | [diff] [blame^] | 116 | .of_to_plat = sunxi_reset_of_to_plat, |
| 117 | .plat_auto = sizeof(struct sunxi_reset_plat), |
Jagan Teki | 7f6c2a8 | 2019-01-18 22:18:13 +0530 | [diff] [blame] | 118 | }; |