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