ARM: dts: npcm8xx: add npcm845 function node
[platform/kernel/u-boot.git] / drivers / reset / reset-sunxi.c
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>
10 #include <log.h>
11 #include <malloc.h>
12 #include <reset-uclass.h>
13 #include <asm/io.h>
14 #include <clk/sunxi.h>
15 #include <linux/bitops.h>
16 #include <linux/log2.h>
17
18 static const struct ccu_reset *plat_to_reset(struct ccu_plat *plat,
19                                              unsigned long id)
20 {
21         return  &plat->desc->resets[id];
22 }
23
24 static int sunxi_reset_request(struct reset_ctl *reset_ctl)
25 {
26         struct ccu_plat *plat = dev_get_plat(reset_ctl->dev);
27
28         debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
29
30         if (reset_ctl->id >= plat->desc->num_resets)
31                 return -EINVAL;
32
33         return 0;
34 }
35
36 static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
37 {
38         struct ccu_plat *plat = dev_get_plat(reset_ctl->dev);
39         const struct ccu_reset *reset = plat_to_reset(plat, reset_ctl->id);
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
50         reg = readl(plat->base + reset->off);
51         if (on)
52                 reg |= reset->bit;
53         else
54                 reg &= ~reset->bit;
55
56         writel(reg, plat->base + reset->off);
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,
73         .rst_assert = sunxi_reset_assert,
74         .rst_deassert = sunxi_reset_deassert,
75 };
76
77 U_BOOT_DRIVER(sunxi_reset) = {
78         .name           = "sunxi_reset",
79         .id             = UCLASS_RESET,
80         .ops            = &sunxi_reset_ops,
81 };