blob: e2a9c2a142b1ea7f912b8400660dd9f2bb2e6851 [file] [log] [blame]
Jagan Teki7f6c2a82019-01-18 22:18:13 +05301// 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 Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Jagan Teki7f6c2a82019-01-18 22:18:13 +053012#include <reset-uclass.h>
13#include <asm/io.h>
Samuel Holland12e3faa2021-09-12 11:48:43 -050014#include <clk/sunxi.h>
Simon Glass95588622020-12-22 19:30:28 -070015#include <dm/device-internal.h>
Jagan Teki7f6c2a82019-01-18 22:18:13 +053016#include <dm/lists.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Jagan Teki7f6c2a82019-01-18 22:18:13 +053018#include <linux/log2.h>
Jagan Teki7f6c2a82019-01-18 22:18:13 +053019
20struct sunxi_reset_priv {
21 void *base;
22 ulong count;
23 const struct ccu_desc *desc;
24};
25
26static const struct ccu_reset *priv_to_reset(struct sunxi_reset_priv *priv,
27 unsigned long id)
28{
29 return &priv->desc->resets[id];
30}
31
32static int sunxi_reset_request(struct reset_ctl *reset_ctl)
33{
34 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
35
36 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
37
38 if (reset_ctl->id >= priv->count)
39 return -EINVAL;
40
41 return 0;
42}
43
Jagan Teki7f6c2a82019-01-18 22:18:13 +053044static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
45{
46 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
47 const struct ccu_reset *reset = priv_to_reset(priv, reset_ctl->id);
48 u32 reg;
49
50 if (!(reset->flags & CCU_RST_F_IS_VALID)) {
51 printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id);
52 return 0;
53 }
54
55 debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__,
56 reset_ctl->id, reset->off, ilog2(reset->bit));
57
58 reg = readl(priv->base + reset->off);
59 if (on)
60 reg |= reset->bit;
61 else
62 reg &= ~reset->bit;
63
64 writel(reg, priv->base + reset->off);
65
66 return 0;
67}
68
69static int sunxi_reset_assert(struct reset_ctl *reset_ctl)
70{
71 return sunxi_set_reset(reset_ctl, false);
72}
73
74static int sunxi_reset_deassert(struct reset_ctl *reset_ctl)
75{
76 return sunxi_set_reset(reset_ctl, true);
77}
78
79struct reset_ops sunxi_reset_ops = {
80 .request = sunxi_reset_request,
Jagan Teki7f6c2a82019-01-18 22:18:13 +053081 .rst_assert = sunxi_reset_assert,
82 .rst_deassert = sunxi_reset_deassert,
83};
84
85static int sunxi_reset_probe(struct udevice *dev)
86{
87 struct sunxi_reset_priv *priv = dev_get_priv(dev);
88
89 priv->base = dev_read_addr_ptr(dev);
90
91 return 0;
92}
93
94int sunxi_reset_bind(struct udevice *dev, ulong count)
95{
96 struct udevice *rst_dev;
97 struct sunxi_reset_priv *priv;
98 int ret;
99
100 ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset",
101 dev_ofnode(dev), &rst_dev);
102 if (ret) {
103 debug("failed to bind sunxi_reset driver (ret=%d)\n", ret);
104 return ret;
105 }
106 priv = malloc(sizeof(struct sunxi_reset_priv));
107 priv->count = count;
108 priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev);
Simon Glass95588622020-12-22 19:30:28 -0700109 dev_set_priv(rst_dev, priv);
Jagan Teki7f6c2a82019-01-18 22:18:13 +0530110
111 return 0;
112}
113
114U_BOOT_DRIVER(sunxi_reset) = {
115 .name = "sunxi_reset",
116 .id = UCLASS_RESET,
117 .ops = &sunxi_reset_ops,
118 .probe = sunxi_reset_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700119 .priv_auto = sizeof(struct sunxi_reset_priv),
Jagan Teki7f6c2a82019-01-18 22:18:13 +0530120};