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