1 // 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>
11 #include <log.h>
12 #include <malloc.h>
13 #include <dm/lists.h>
14 #include <regmap.h>
15 #include <reset-uclass.h>
16 #include <syscon.h>
17 #include <dm/device-internal.h>
18 #include <linux/bitops.h>
19 #include <linux/err.h>
20 
21 struct mediatek_reset_priv {
22 	struct regmap *regmap;
23 	u32 regofs;
24 	u32 nr_resets;
25 };
26 
mediatek_reset_request(struct reset_ctl * reset_ctl)27 static int mediatek_reset_request(struct reset_ctl *reset_ctl)
28 {
29 	return 0;
30 }
31 
mediatek_reset_free(struct reset_ctl * reset_ctl)32 static int mediatek_reset_free(struct reset_ctl *reset_ctl)
33 {
34 	return 0;
35 }
36 
mediatek_reset_assert(struct reset_ctl * reset_ctl)37 static 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 
mediatek_reset_deassert(struct reset_ctl * reset_ctl)49 static 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 
61 struct reset_ops mediatek_reset_ops = {
62 	.request = mediatek_reset_request,
63 	.rfree = mediatek_reset_free,
64 	.rst_assert = mediatek_reset_assert,
65 	.rst_deassert = mediatek_reset_deassert,
66 };
67 
mediatek_reset_probe(struct udevice * dev)68 static 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 
mediatek_reset_bind(struct udevice * pdev,u32 regofs,u32 num_regs)82 int 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;
96 	dev_set_priv(rst_dev, priv);
97 
98 	return 0;
99 }
100 
101 U_BOOT_DRIVER(mediatek_reset) = {
102 	.name = "mediatek_reset",
103 	.id = UCLASS_RESET,
104 	.probe = mediatek_reset_probe,
105 	.ops = &mediatek_reset_ops,
106 	.priv_auto	= sizeof(struct mediatek_reset_priv),
107 };
108