blob: 8b95938dfe35358cee3c0ba7ee5a5538909265ba [file] [log] [blame]
Jagan Teki99ba4302019-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 Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070011#include <malloc.h>
Jagan Teki99ba4302019-01-18 22:18:13 +053012#include <reset-uclass.h>
13#include <asm/io.h>
Samuel Holland21d314a2021-09-12 11:48:43 -050014#include <clk/sunxi.h>
Simon Glass0fd3d912020-12-22 19:30:28 -070015#include <dm/device-internal.h>
Jagan Teki99ba4302019-01-18 22:18:13 +053016#include <dm/lists.h>
Simon Glasscd93d622020-05-10 11:40:13 -060017#include <linux/bitops.h>
Jagan Teki99ba4302019-01-18 22:18:13 +053018#include <linux/log2.h>
Jagan Teki99ba4302019-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
44static int sunxi_reset_free(struct reset_ctl *reset_ctl)
45{
46 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
47
48 return 0;
49}
50
51static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
52{
53 struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
54 const struct ccu_reset *reset = priv_to_reset(priv, reset_ctl->id);
55 u32 reg;
56
57 if (!(reset->flags & CCU_RST_F_IS_VALID)) {
58 printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id);
59 return 0;
60 }
61
62 debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__,
63 reset_ctl->id, reset->off, ilog2(reset->bit));
64
65 reg = readl(priv->base + reset->off);
66 if (on)
67 reg |= reset->bit;
68 else
69 reg &= ~reset->bit;
70
71 writel(reg, priv->base + reset->off);
72
73 return 0;
74}
75
76static int sunxi_reset_assert(struct reset_ctl *reset_ctl)
77{
78 return sunxi_set_reset(reset_ctl, false);
79}
80
81static int sunxi_reset_deassert(struct reset_ctl *reset_ctl)
82{
83 return sunxi_set_reset(reset_ctl, true);
84}
85
86struct reset_ops sunxi_reset_ops = {
87 .request = sunxi_reset_request,
Simon Glass94474b22020-02-03 07:35:52 -070088 .rfree = sunxi_reset_free,
Jagan Teki99ba4302019-01-18 22:18:13 +053089 .rst_assert = sunxi_reset_assert,
90 .rst_deassert = sunxi_reset_deassert,
91};
92
93static int sunxi_reset_probe(struct udevice *dev)
94{
95 struct sunxi_reset_priv *priv = dev_get_priv(dev);
96
97 priv->base = dev_read_addr_ptr(dev);
98
99 return 0;
100}
101
102int sunxi_reset_bind(struct udevice *dev, ulong count)
103{
104 struct udevice *rst_dev;
105 struct sunxi_reset_priv *priv;
106 int ret;
107
108 ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset",
109 dev_ofnode(dev), &rst_dev);
110 if (ret) {
111 debug("failed to bind sunxi_reset driver (ret=%d)\n", ret);
112 return ret;
113 }
114 priv = malloc(sizeof(struct sunxi_reset_priv));
115 priv->count = count;
116 priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev);
Simon Glass0fd3d912020-12-22 19:30:28 -0700117 dev_set_priv(rst_dev, priv);
Jagan Teki99ba4302019-01-18 22:18:13 +0530118
119 return 0;
120}
121
122U_BOOT_DRIVER(sunxi_reset) = {
123 .name = "sunxi_reset",
124 .id = UCLASS_RESET,
125 .ops = &sunxi_reset_ops,
126 .probe = sunxi_reset_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700127 .priv_auto = sizeof(struct sunxi_reset_priv),
Jagan Teki99ba4302019-01-18 22:18:13 +0530128};