blob: 6d17f52ac78b6de7ee249de58923dae757d56a1e [file] [log] [blame]
Weijie Gao3e066bc2018-12-20 16:12:51 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2018 MediaTek Inc.
4 *
5 * Author: Ryder Lee <ryder.lee@mediatek.com>
6 * Weijie Gao <weijie.gao@mediatek.com>
7 */
8
9#include <common.h>
10#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -070011#include <malloc.h>
Weijie Gao3e066bc2018-12-20 16:12:51 +080012#include <dm/lists.h>
13#include <regmap.h>
14#include <reset-uclass.h>
15#include <syscon.h>
Simon Glass61b29b82020-02-03 07:36:15 -070016#include <linux/err.h>
Weijie Gao3e066bc2018-12-20 16:12:51 +080017
18struct mediatek_reset_priv {
19 struct regmap *regmap;
20 u32 regofs;
21 u32 nr_resets;
22};
23
24static int mediatek_reset_request(struct reset_ctl *reset_ctl)
25{
26 return 0;
27}
28
29static int mediatek_reset_free(struct reset_ctl *reset_ctl)
30{
31 return 0;
32}
33
34static int mediatek_reset_assert(struct reset_ctl *reset_ctl)
35{
36 struct mediatek_reset_priv *priv = dev_get_priv(reset_ctl->dev);
37 int id = reset_ctl->id;
38
39 if (id >= priv->nr_resets)
40 return -EINVAL;
41
42 return regmap_update_bits(priv->regmap,
43 priv->regofs + ((id / 32) << 2), BIT(id % 32), BIT(id % 32));
44}
45
46static int mediatek_reset_deassert(struct reset_ctl *reset_ctl)
47{
48 struct mediatek_reset_priv *priv = dev_get_priv(reset_ctl->dev);
49 int id = reset_ctl->id;
50
51 if (id >= priv->nr_resets)
52 return -EINVAL;
53
54 return regmap_update_bits(priv->regmap,
55 priv->regofs + ((id / 32) << 2), BIT(id % 32), 0);
56}
57
58struct reset_ops mediatek_reset_ops = {
59 .request = mediatek_reset_request,
Simon Glass94474b22020-02-03 07:35:52 -070060 .rfree = mediatek_reset_free,
Weijie Gao3e066bc2018-12-20 16:12:51 +080061 .rst_assert = mediatek_reset_assert,
62 .rst_deassert = mediatek_reset_deassert,
63};
64
65static int mediatek_reset_probe(struct udevice *dev)
66{
67 struct mediatek_reset_priv *priv = dev_get_priv(dev);
68
69 if (!priv->regofs && !priv->nr_resets)
70 return -EINVAL;
71
72 priv->regmap = syscon_node_to_regmap(dev_ofnode(dev));
73 if (IS_ERR(priv->regmap))
74 return PTR_ERR(priv->regmap);
75
76 return 0;
77}
78
79int mediatek_reset_bind(struct udevice *pdev, u32 regofs, u32 num_regs)
80{
81 struct udevice *rst_dev;
82 struct mediatek_reset_priv *priv;
83 int ret;
84
85 ret = device_bind_driver_to_node(pdev, "mediatek_reset", "reset",
86 dev_ofnode(pdev), &rst_dev);
87 if (ret)
88 return ret;
89
90 priv = malloc(sizeof(struct mediatek_reset_priv));
91 priv->regofs = regofs;
92 priv->nr_resets = num_regs * 32;
93 rst_dev->priv = priv;
94
95 return 0;
96}
97
98U_BOOT_DRIVER(mediatek_reset) = {
99 .name = "mediatek_reset",
100 .id = UCLASS_RESET,
101 .probe = mediatek_reset_probe,
102 .ops = &mediatek_reset_ops,
103 .priv_auto_alloc_size = sizeof(struct mediatek_reset_priv),
104};