xref: /linux/drivers/reset/reset-uniphier-glue.c (revision fcc79e1714e8c2b8e216dc3149812edd37884eef)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // reset-uniphier-glue.c - Glue layer reset driver for UniPhier
4 // Copyright 2018 Socionext Inc.
5 // Author: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
6 
7 #include <linux/clk.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/platform_device.h>
11 #include <linux/reset.h>
12 #include <linux/reset/reset-simple.h>
13 
14 #define MAX_CLKS	2
15 #define MAX_RSTS	2
16 
17 struct uniphier_glue_reset_soc_data {
18 	int nclks;
19 	const char * const *clock_names;
20 	int nrsts;
21 	const char * const *reset_names;
22 };
23 
24 struct uniphier_glue_reset_priv {
25 	struct clk_bulk_data clk[MAX_CLKS];
26 	struct reset_control_bulk_data rst[MAX_RSTS];
27 	struct reset_simple_data rdata;
28 	const struct uniphier_glue_reset_soc_data *data;
29 };
30 
31 static void uniphier_clk_disable(void *_priv)
32 {
33 	struct uniphier_glue_reset_priv *priv = _priv;
34 
35 	clk_bulk_disable_unprepare(priv->data->nclks, priv->clk);
36 }
37 
38 static int uniphier_glue_reset_probe(struct platform_device *pdev)
39 {
40 	struct device *dev = &pdev->dev;
41 	struct uniphier_glue_reset_priv *priv;
42 	struct resource *res;
43 	int i, ret;
44 
45 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
46 	if (!priv)
47 		return -ENOMEM;
48 
49 	priv->data = of_device_get_match_data(dev);
50 	if (WARN_ON(!priv->data || priv->data->nclks > MAX_CLKS ||
51 		    priv->data->nrsts > MAX_RSTS))
52 		return -EINVAL;
53 
54 	priv->rdata.membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
55 	if (IS_ERR(priv->rdata.membase))
56 		return PTR_ERR(priv->rdata.membase);
57 
58 	for (i = 0; i < priv->data->nclks; i++)
59 		priv->clk[i].id = priv->data->clock_names[i];
60 	ret = devm_clk_bulk_get(dev, priv->data->nclks, priv->clk);
61 	if (ret)
62 		return ret;
63 
64 	ret = clk_bulk_prepare_enable(priv->data->nclks, priv->clk);
65 	if (ret)
66 		return ret;
67 
68 	ret = devm_add_action_or_reset(dev, uniphier_clk_disable, priv);
69 	if (ret)
70 		return ret;
71 
72 	for (i = 0; i < priv->data->nrsts; i++)
73 		priv->rst[i].id = priv->data->reset_names[i];
74 	ret = devm_reset_control_bulk_get_shared_deasserted(dev,
75 							    priv->data->nrsts,
76 							    priv->rst);
77 	if (ret)
78 		return ret;
79 
80 	spin_lock_init(&priv->rdata.lock);
81 	priv->rdata.rcdev.owner = THIS_MODULE;
82 	priv->rdata.rcdev.nr_resets = resource_size(res) * BITS_PER_BYTE;
83 	priv->rdata.rcdev.ops = &reset_simple_ops;
84 	priv->rdata.rcdev.of_node = dev->of_node;
85 	priv->rdata.active_low = true;
86 
87 	return devm_reset_controller_register(dev, &priv->rdata.rcdev);
88 }
89 
90 static const char * const uniphier_pro4_clock_reset_names[] = {
91 	"gio", "link",
92 };
93 
94 static const struct uniphier_glue_reset_soc_data uniphier_pro4_data = {
95 	.nclks = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
96 	.clock_names = uniphier_pro4_clock_reset_names,
97 	.nrsts = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
98 	.reset_names = uniphier_pro4_clock_reset_names,
99 };
100 
101 static const char * const uniphier_pxs2_clock_reset_names[] = {
102 	"link",
103 };
104 
105 static const struct uniphier_glue_reset_soc_data uniphier_pxs2_data = {
106 	.nclks = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
107 	.clock_names = uniphier_pxs2_clock_reset_names,
108 	.nrsts = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
109 	.reset_names = uniphier_pxs2_clock_reset_names,
110 };
111 
112 static const struct of_device_id uniphier_glue_reset_match[] = {
113 	{
114 		.compatible = "socionext,uniphier-pro4-usb3-reset",
115 		.data = &uniphier_pro4_data,
116 	},
117 	{
118 		.compatible = "socionext,uniphier-pro5-usb3-reset",
119 		.data = &uniphier_pro4_data,
120 	},
121 	{
122 		.compatible = "socionext,uniphier-pxs2-usb3-reset",
123 		.data = &uniphier_pxs2_data,
124 	},
125 	{
126 		.compatible = "socionext,uniphier-ld20-usb3-reset",
127 		.data = &uniphier_pxs2_data,
128 	},
129 	{
130 		.compatible = "socionext,uniphier-pxs3-usb3-reset",
131 		.data = &uniphier_pxs2_data,
132 	},
133 	{
134 		.compatible = "socionext,uniphier-nx1-usb3-reset",
135 		.data = &uniphier_pxs2_data,
136 	},
137 	{
138 		.compatible = "socionext,uniphier-pro4-ahci-reset",
139 		.data = &uniphier_pro4_data,
140 	},
141 	{
142 		.compatible = "socionext,uniphier-pxs2-ahci-reset",
143 		.data = &uniphier_pxs2_data,
144 	},
145 	{
146 		.compatible = "socionext,uniphier-pxs3-ahci-reset",
147 		.data = &uniphier_pxs2_data,
148 	},
149 	{ /* Sentinel */ }
150 };
151 MODULE_DEVICE_TABLE(of, uniphier_glue_reset_match);
152 
153 static struct platform_driver uniphier_glue_reset_driver = {
154 	.probe = uniphier_glue_reset_probe,
155 	.driver = {
156 		.name = "uniphier-glue-reset",
157 		.of_match_table = uniphier_glue_reset_match,
158 	},
159 };
160 module_platform_driver(uniphier_glue_reset_driver);
161 
162 MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
163 MODULE_DESCRIPTION("UniPhier Glue layer reset driver");
164 MODULE_LICENSE("GPL");
165