blob: 7427013ab60613dc3bcf7943a7a3395242142f24 [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 Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <malloc.h>
Weijie Gao3e066bc2018-12-20 16:12:51 +080013#include <dm/lists.h>
14#include <regmap.h>
15#include <reset-uclass.h>
16#include <syscon.h>
Simon Glass0fd3d912020-12-22 19:30:28 -070017#include <dm/device-internal.h>
Simon Glasscd93d622020-05-10 11:40:13 -060018#include <linux/bitops.h>
Simon Glass61b29b82020-02-03 07:36:15 -070019#include <linux/err.h>
Weijie Gao3e066bc2018-12-20 16:12:51 +080020
21struct mediatek_reset_priv {
22 struct regmap *regmap;
23 u32 regofs;
24 u32 nr_resets;
25};
26
27static int mediatek_reset_request(struct reset_ctl *reset_ctl)
28{
29 return 0;
30}
31
32static int mediatek_reset_free(struct reset_ctl *reset_ctl)
33{
34 return 0;
35}
36
37static int mediatek_reset_assert(struct reset_ctl *reset_ctl)
38{
39 struct mediatek_reset_priv *priv = dev_get_priv(reset_ctl->dev);
40 int id = reset_ctl->id;
41
42 if (id >= priv->nr_resets)
43 return -EINVAL;
44
45 return regmap_update_bits(priv->regmap,
46 priv->regofs + ((id / 32) << 2), BIT(id % 32), BIT(id % 32));
47}
48
49static int mediatek_reset_deassert(struct reset_ctl *reset_ctl)
50{
51 struct mediatek_reset_priv *priv = dev_get_priv(reset_ctl->dev);
52 int id = reset_ctl->id;
53
54 if (id >= priv->nr_resets)
55 return -EINVAL;
56
57 return regmap_update_bits(priv->regmap,
58 priv->regofs + ((id / 32) << 2), BIT(id % 32), 0);
59}
60
61struct reset_ops mediatek_reset_ops = {
62 .request = mediatek_reset_request,
Simon Glass94474b22020-02-03 07:35:52 -070063 .rfree = mediatek_reset_free,
Weijie Gao3e066bc2018-12-20 16:12:51 +080064 .rst_assert = mediatek_reset_assert,
65 .rst_deassert = mediatek_reset_deassert,
66};
67
68static int mediatek_reset_probe(struct udevice *dev)
69{
70 struct mediatek_reset_priv *priv = dev_get_priv(dev);
71
72 if (!priv->regofs && !priv->nr_resets)
73 return -EINVAL;
74
75 priv->regmap = syscon_node_to_regmap(dev_ofnode(dev));
76 if (IS_ERR(priv->regmap))
77 return PTR_ERR(priv->regmap);
78
79 return 0;
80}
81
82int mediatek_reset_bind(struct udevice *pdev, u32 regofs, u32 num_regs)
83{
84 struct udevice *rst_dev;
85 struct mediatek_reset_priv *priv;
86 int ret;
87
88 ret = device_bind_driver_to_node(pdev, "mediatek_reset", "reset",
89 dev_ofnode(pdev), &rst_dev);
90 if (ret)
91 return ret;
92
93 priv = malloc(sizeof(struct mediatek_reset_priv));
94 priv->regofs = regofs;
95 priv->nr_resets = num_regs * 32;
Simon Glass0fd3d912020-12-22 19:30:28 -070096 dev_set_priv(rst_dev, priv);
Weijie Gao3e066bc2018-12-20 16:12:51 +080097
98 return 0;
99}
100
101U_BOOT_DRIVER(mediatek_reset) = {
102 .name = "mediatek_reset",
103 .id = UCLASS_RESET,
104 .probe = mediatek_reset_probe,
105 .ops = &mediatek_reset_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700106 .priv_auto = sizeof(struct mediatek_reset_priv),
Weijie Gao3e066bc2018-12-20 16:12:51 +0800107};