xref: /linux/drivers/reset/reset-k210.c (revision 1e0731c05c985deb68a97fa44c1adcd3305dda90)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 Western Digital Corporation or its affiliates.
4  */
5 #include <linux/of.h>
6 #include <linux/platform_device.h>
7 #include <linux/reset-controller.h>
8 #include <linux/delay.h>
9 #include <linux/mfd/syscon.h>
10 #include <linux/regmap.h>
11 #include <soc/canaan/k210-sysctl.h>
12 
13 #include <dt-bindings/reset/k210-rst.h>
14 
15 #define K210_RST_MASK	0x27FFFFFF
16 
17 struct k210_rst {
18 	struct regmap *map;
19 	struct reset_controller_dev rcdev;
20 };
21 
22 static inline struct k210_rst *
23 to_k210_rst(struct reset_controller_dev *rcdev)
24 {
25 	return container_of(rcdev, struct k210_rst, rcdev);
26 }
27 
28 static inline int k210_rst_assert(struct reset_controller_dev *rcdev,
29 				  unsigned long id)
30 {
31 	struct k210_rst *ksr = to_k210_rst(rcdev);
32 
33 	return regmap_update_bits(ksr->map, K210_SYSCTL_PERI_RESET, BIT(id), 1);
34 }
35 
36 static inline int k210_rst_deassert(struct reset_controller_dev *rcdev,
37 				    unsigned long id)
38 {
39 	struct k210_rst *ksr = to_k210_rst(rcdev);
40 
41 	return regmap_update_bits(ksr->map, K210_SYSCTL_PERI_RESET, BIT(id), 0);
42 }
43 
44 static int k210_rst_reset(struct reset_controller_dev *rcdev,
45 			  unsigned long id)
46 {
47 	int ret;
48 
49 	ret = k210_rst_assert(rcdev, id);
50 	if (ret == 0) {
51 		udelay(10);
52 		ret = k210_rst_deassert(rcdev, id);
53 	}
54 
55 	return ret;
56 }
57 
58 static int k210_rst_status(struct reset_controller_dev *rcdev,
59 			   unsigned long id)
60 {
61 	struct k210_rst *ksr = to_k210_rst(rcdev);
62 	u32 reg, bit = BIT(id);
63 	int ret;
64 
65 	ret = regmap_read(ksr->map, K210_SYSCTL_PERI_RESET, &reg);
66 	if (ret)
67 		return ret;
68 
69 	return reg & bit;
70 }
71 
72 static int k210_rst_xlate(struct reset_controller_dev *rcdev,
73 			  const struct of_phandle_args *reset_spec)
74 {
75 	unsigned long id = reset_spec->args[0];
76 
77 	if (!(BIT(id) & K210_RST_MASK))
78 		return -EINVAL;
79 
80 	return id;
81 }
82 
83 static const struct reset_control_ops k210_rst_ops = {
84 	.assert		= k210_rst_assert,
85 	.deassert	= k210_rst_deassert,
86 	.reset		= k210_rst_reset,
87 	.status		= k210_rst_status,
88 };
89 
90 static int k210_rst_probe(struct platform_device *pdev)
91 {
92 	struct device *dev = &pdev->dev;
93 	struct device_node *parent_np = of_get_parent(dev->of_node);
94 	struct k210_rst *ksr;
95 
96 	dev_info(dev, "K210 reset controller\n");
97 
98 	ksr = devm_kzalloc(dev, sizeof(*ksr), GFP_KERNEL);
99 	if (!ksr)
100 		return -ENOMEM;
101 
102 	ksr->map = syscon_node_to_regmap(parent_np);
103 	of_node_put(parent_np);
104 	if (IS_ERR(ksr->map))
105 		return PTR_ERR(ksr->map);
106 
107 	ksr->rcdev.owner = THIS_MODULE;
108 	ksr->rcdev.dev = dev;
109 	ksr->rcdev.of_node = dev->of_node;
110 	ksr->rcdev.ops = &k210_rst_ops;
111 	ksr->rcdev.nr_resets = fls(K210_RST_MASK);
112 	ksr->rcdev.of_reset_n_cells = 1;
113 	ksr->rcdev.of_xlate = k210_rst_xlate;
114 
115 	return devm_reset_controller_register(dev, &ksr->rcdev);
116 }
117 
118 static const struct of_device_id k210_rst_dt_ids[] = {
119 	{ .compatible = "canaan,k210-rst" },
120 	{ /* sentinel */ },
121 };
122 
123 static struct platform_driver k210_rst_driver = {
124 	.probe	= k210_rst_probe,
125 	.driver = {
126 		.name		= "k210-rst",
127 		.of_match_table	= k210_rst_dt_ids,
128 	},
129 };
130 builtin_platform_driver(k210_rst_driver);
131