blob: 90c52a96f7751758c1d7586ba2b493e1b4985b4d [file] [log] [blame]
Masahiro Yamada54e991b2016-08-02 13:18:29 +09001/*
2 * Copyright (C) 2016 Socionext Inc.
3 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/mfd/syscon.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/of_device.h>
20#include <linux/platform_device.h>
21#include <linux/regmap.h>
22#include <linux/reset-controller.h>
23
24struct uniphier_reset_data {
25 unsigned int id;
26 unsigned int reg;
27 unsigned int bit;
28 unsigned int flags;
29#define UNIPHIER_RESET_ACTIVE_LOW BIT(0)
30};
31
32#define UNIPHIER_RESET_ID_END (unsigned int)(-1)
33
34#define UNIPHIER_RESET_END \
35 { .id = UNIPHIER_RESET_ID_END }
36
37#define UNIPHIER_RESET(_id, _reg, _bit) \
38 { \
39 .id = (_id), \
40 .reg = (_reg), \
41 .bit = (_bit), \
42 }
43
44#define UNIPHIER_RESETX(_id, _reg, _bit) \
45 { \
46 .id = (_id), \
47 .reg = (_reg), \
48 .bit = (_bit), \
49 .flags = UNIPHIER_RESET_ACTIVE_LOW, \
50 }
51
52/* System reset data */
Masahiro Yamada52810362017-08-06 11:44:01 +090053static const struct uniphier_reset_data uniphier_ld4_sys_reset_data[] = {
54 UNIPHIER_RESETX(2, 0x2000, 2), /* NAND */
55 UNIPHIER_RESETX(8, 0x2000, 10), /* STDMAC (Ether, HSC, MIO) */
Masahiro Yamada54e991b2016-08-02 13:18:29 +090056 UNIPHIER_RESET_END,
57};
58
Wei Yongjun716adfe2017-02-08 15:56:20 +000059static const struct uniphier_reset_data uniphier_pro4_sys_reset_data[] = {
Masahiro Yamada52810362017-08-06 11:44:01 +090060 UNIPHIER_RESETX(2, 0x2000, 2), /* NAND */
61 UNIPHIER_RESETX(8, 0x2000, 10), /* STDMAC (HSC, MIO, RLE) */
Masahiro Yamadadec173c2017-08-06 11:44:02 +090062 UNIPHIER_RESETX(12, 0x2000, 6), /* GIO (Ether, SATA, USB3) */
63 UNIPHIER_RESETX(14, 0x2000, 17), /* USB30 */
64 UNIPHIER_RESETX(15, 0x2004, 17), /* USB31 */
Masahiro Yamada54e991b2016-08-02 13:18:29 +090065 UNIPHIER_RESET_END,
66};
67
Wei Yongjun716adfe2017-02-08 15:56:20 +000068static const struct uniphier_reset_data uniphier_pro5_sys_reset_data[] = {
Masahiro Yamada52810362017-08-06 11:44:01 +090069 UNIPHIER_RESETX(2, 0x2000, 2), /* NAND */
70 UNIPHIER_RESETX(8, 0x2000, 10), /* STDMAC (HSC) */
Masahiro Yamadadec173c2017-08-06 11:44:02 +090071 UNIPHIER_RESETX(12, 0x2000, 6), /* GIO (PCIe, USB3) */
72 UNIPHIER_RESETX(14, 0x2000, 17), /* USB30 */
73 UNIPHIER_RESETX(15, 0x2004, 17), /* USB31 */
Masahiro Yamada54e991b2016-08-02 13:18:29 +090074 UNIPHIER_RESET_END,
75};
76
Wei Yongjun716adfe2017-02-08 15:56:20 +000077static const struct uniphier_reset_data uniphier_pxs2_sys_reset_data[] = {
Masahiro Yamada52810362017-08-06 11:44:01 +090078 UNIPHIER_RESETX(2, 0x2000, 2), /* NAND */
79 UNIPHIER_RESETX(8, 0x2000, 10), /* STDMAC (HSC, RLE) */
Masahiro Yamadadec173c2017-08-06 11:44:02 +090080 UNIPHIER_RESETX(14, 0x2000, 17), /* USB30 */
81 UNIPHIER_RESETX(15, 0x2004, 17), /* USB31 */
Masahiro Yamada54e991b2016-08-02 13:18:29 +090082 UNIPHIER_RESETX(16, 0x2014, 4), /* USB30-PHY0 */
83 UNIPHIER_RESETX(17, 0x2014, 0), /* USB30-PHY1 */
84 UNIPHIER_RESETX(18, 0x2014, 2), /* USB30-PHY2 */
85 UNIPHIER_RESETX(20, 0x2014, 5), /* USB31-PHY0 */
86 UNIPHIER_RESETX(21, 0x2014, 1), /* USB31-PHY1 */
87 UNIPHIER_RESETX(28, 0x2014, 12), /* SATA */
88 UNIPHIER_RESET(29, 0x2014, 8), /* SATA-PHY (active high) */
89 UNIPHIER_RESET_END,
90};
91
Wei Yongjun716adfe2017-02-08 15:56:20 +000092static const struct uniphier_reset_data uniphier_ld11_sys_reset_data[] = {
Masahiro Yamadadec173c2017-08-06 11:44:02 +090093 UNIPHIER_RESETX(2, 0x200c, 0), /* NAND */
94 UNIPHIER_RESETX(4, 0x200c, 2), /* eMMC */
95 UNIPHIER_RESETX(8, 0x200c, 8), /* STDMAC (HSC, MIO) */
Katsuhiro Suzuki94e10c22017-08-13 18:00:41 +090096 UNIPHIER_RESETX(40, 0x2008, 0), /* AIO */
97 UNIPHIER_RESETX(41, 0x2008, 1), /* EVEA */
Masahiro Yamada54e991b2016-08-02 13:18:29 +090098 UNIPHIER_RESET_END,
99};
100
Wei Yongjun716adfe2017-02-08 15:56:20 +0000101static const struct uniphier_reset_data uniphier_ld20_sys_reset_data[] = {
Masahiro Yamadadec173c2017-08-06 11:44:02 +0900102 UNIPHIER_RESETX(2, 0x200c, 0), /* NAND */
103 UNIPHIER_RESETX(4, 0x200c, 2), /* eMMC */
104 UNIPHIER_RESETX(8, 0x200c, 8), /* STDMAC (HSC) */
105 UNIPHIER_RESETX(12, 0x200c, 5), /* GIO (PCIe, USB3) */
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900106 UNIPHIER_RESETX(16, 0x200c, 12), /* USB30-PHY0 */
107 UNIPHIER_RESETX(17, 0x200c, 13), /* USB30-PHY1 */
108 UNIPHIER_RESETX(18, 0x200c, 14), /* USB30-PHY2 */
109 UNIPHIER_RESETX(19, 0x200c, 15), /* USB30-PHY3 */
Katsuhiro Suzuki94e10c22017-08-13 18:00:41 +0900110 UNIPHIER_RESETX(40, 0x2008, 0), /* AIO */
111 UNIPHIER_RESETX(41, 0x2008, 1), /* EVEA */
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900112 UNIPHIER_RESET_END,
113};
114
115/* Media I/O reset data */
116#define UNIPHIER_MIO_RESET_SD(id, ch) \
117 UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 0)
118
119#define UNIPHIER_MIO_RESET_SD_BRIDGE(id, ch) \
120 UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 26)
121
122#define UNIPHIER_MIO_RESET_EMMC_HW_RESET(id, ch) \
123 UNIPHIER_RESETX((id), 0x80 + 0x200 * (ch), 0)
124
125#define UNIPHIER_MIO_RESET_USB2(id, ch) \
126 UNIPHIER_RESETX((id), 0x114 + 0x200 * (ch), 0)
127
128#define UNIPHIER_MIO_RESET_USB2_BRIDGE(id, ch) \
129 UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 24)
130
131#define UNIPHIER_MIO_RESET_DMAC(id) \
132 UNIPHIER_RESETX((id), 0x110, 17)
133
Masahiro Yamada52810362017-08-06 11:44:01 +0900134static const struct uniphier_reset_data uniphier_ld4_mio_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900135 UNIPHIER_MIO_RESET_SD(0, 0),
136 UNIPHIER_MIO_RESET_SD(1, 1),
137 UNIPHIER_MIO_RESET_SD(2, 2),
138 UNIPHIER_MIO_RESET_SD_BRIDGE(3, 0),
139 UNIPHIER_MIO_RESET_SD_BRIDGE(4, 1),
140 UNIPHIER_MIO_RESET_SD_BRIDGE(5, 2),
141 UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
142 UNIPHIER_MIO_RESET_DMAC(7),
143 UNIPHIER_MIO_RESET_USB2(8, 0),
144 UNIPHIER_MIO_RESET_USB2(9, 1),
145 UNIPHIER_MIO_RESET_USB2(10, 2),
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900146 UNIPHIER_MIO_RESET_USB2_BRIDGE(12, 0),
147 UNIPHIER_MIO_RESET_USB2_BRIDGE(13, 1),
148 UNIPHIER_MIO_RESET_USB2_BRIDGE(14, 2),
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900149 UNIPHIER_RESET_END,
150};
151
Wei Yongjun716adfe2017-02-08 15:56:20 +0000152static const struct uniphier_reset_data uniphier_pro5_sd_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900153 UNIPHIER_MIO_RESET_SD(0, 0),
154 UNIPHIER_MIO_RESET_SD(1, 1),
155 UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
156 UNIPHIER_RESET_END,
157};
158
159/* Peripheral reset data */
160#define UNIPHIER_PERI_RESET_UART(id, ch) \
161 UNIPHIER_RESETX((id), 0x114, 19 + (ch))
162
163#define UNIPHIER_PERI_RESET_I2C(id, ch) \
164 UNIPHIER_RESETX((id), 0x114, 5 + (ch))
165
166#define UNIPHIER_PERI_RESET_FI2C(id, ch) \
167 UNIPHIER_RESETX((id), 0x114, 24 + (ch))
168
Wei Yongjun716adfe2017-02-08 15:56:20 +0000169static const struct uniphier_reset_data uniphier_ld4_peri_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900170 UNIPHIER_PERI_RESET_UART(0, 0),
171 UNIPHIER_PERI_RESET_UART(1, 1),
172 UNIPHIER_PERI_RESET_UART(2, 2),
173 UNIPHIER_PERI_RESET_UART(3, 3),
174 UNIPHIER_PERI_RESET_I2C(4, 0),
175 UNIPHIER_PERI_RESET_I2C(5, 1),
176 UNIPHIER_PERI_RESET_I2C(6, 2),
177 UNIPHIER_PERI_RESET_I2C(7, 3),
178 UNIPHIER_PERI_RESET_I2C(8, 4),
179 UNIPHIER_RESET_END,
180};
181
Wei Yongjun716adfe2017-02-08 15:56:20 +0000182static const struct uniphier_reset_data uniphier_pro4_peri_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900183 UNIPHIER_PERI_RESET_UART(0, 0),
184 UNIPHIER_PERI_RESET_UART(1, 1),
185 UNIPHIER_PERI_RESET_UART(2, 2),
186 UNIPHIER_PERI_RESET_UART(3, 3),
187 UNIPHIER_PERI_RESET_FI2C(4, 0),
188 UNIPHIER_PERI_RESET_FI2C(5, 1),
189 UNIPHIER_PERI_RESET_FI2C(6, 2),
190 UNIPHIER_PERI_RESET_FI2C(7, 3),
191 UNIPHIER_PERI_RESET_FI2C(8, 4),
192 UNIPHIER_PERI_RESET_FI2C(9, 5),
193 UNIPHIER_PERI_RESET_FI2C(10, 6),
194 UNIPHIER_RESET_END,
195};
196
197/* core implementaton */
198struct uniphier_reset_priv {
199 struct reset_controller_dev rcdev;
200 struct device *dev;
201 struct regmap *regmap;
202 const struct uniphier_reset_data *data;
203};
204
205#define to_uniphier_reset_priv(_rcdev) \
206 container_of(_rcdev, struct uniphier_reset_priv, rcdev)
207
208static int uniphier_reset_update(struct reset_controller_dev *rcdev,
209 unsigned long id, int assert)
210{
211 struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
212 const struct uniphier_reset_data *p;
213
214 for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
215 unsigned int mask, val;
216
217 if (p->id != id)
218 continue;
219
220 mask = BIT(p->bit);
221
222 if (assert)
223 val = mask;
224 else
225 val = ~mask;
226
227 if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
228 val = ~val;
229
230 return regmap_write_bits(priv->regmap, p->reg, mask, val);
231 }
232
233 dev_err(priv->dev, "reset_id=%lu was not handled\n", id);
234 return -EINVAL;
235}
236
237static int uniphier_reset_assert(struct reset_controller_dev *rcdev,
238 unsigned long id)
239{
240 return uniphier_reset_update(rcdev, id, 1);
241}
242
243static int uniphier_reset_deassert(struct reset_controller_dev *rcdev,
244 unsigned long id)
245{
246 return uniphier_reset_update(rcdev, id, 0);
247}
248
249static int uniphier_reset_status(struct reset_controller_dev *rcdev,
250 unsigned long id)
251{
252 struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
253 const struct uniphier_reset_data *p;
254
255 for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
256 unsigned int val;
257 int ret, asserted;
258
259 if (p->id != id)
260 continue;
261
262 ret = regmap_read(priv->regmap, p->reg, &val);
263 if (ret)
264 return ret;
265
266 asserted = !!(val & BIT(p->bit));
267
268 if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
269 asserted = !asserted;
270
271 return asserted;
272 }
273
274 dev_err(priv->dev, "reset_id=%lu was not found\n", id);
275 return -EINVAL;
276}
277
278static const struct reset_control_ops uniphier_reset_ops = {
279 .assert = uniphier_reset_assert,
280 .deassert = uniphier_reset_deassert,
281 .status = uniphier_reset_status,
282};
283
284static int uniphier_reset_probe(struct platform_device *pdev)
285{
286 struct device *dev = &pdev->dev;
287 struct uniphier_reset_priv *priv;
288 const struct uniphier_reset_data *p, *data;
289 struct regmap *regmap;
290 struct device_node *parent;
291 unsigned int nr_resets = 0;
292
293 data = of_device_get_match_data(dev);
294 if (WARN_ON(!data))
295 return -EINVAL;
296
297 parent = of_get_parent(dev->of_node); /* parent should be syscon node */
298 regmap = syscon_node_to_regmap(parent);
299 of_node_put(parent);
300 if (IS_ERR(regmap)) {
301 dev_err(dev, "failed to get regmap (error %ld)\n",
302 PTR_ERR(regmap));
303 return PTR_ERR(regmap);
304 }
305
306 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
307 if (!priv)
308 return -ENOMEM;
309
310 for (p = data; p->id != UNIPHIER_RESET_ID_END; p++)
311 nr_resets = max(nr_resets, p->id + 1);
312
313 priv->rcdev.ops = &uniphier_reset_ops;
314 priv->rcdev.owner = dev->driver->owner;
315 priv->rcdev.of_node = dev->of_node;
316 priv->rcdev.nr_resets = nr_resets;
317 priv->dev = dev;
318 priv->regmap = regmap;
319 priv->data = data;
320
321 return devm_reset_controller_register(&pdev->dev, &priv->rcdev);
322}
323
324static const struct of_device_id uniphier_reset_match[] = {
325 /* System reset */
326 {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900327 .compatible = "socionext,uniphier-ld4-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900328 .data = uniphier_ld4_sys_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900329 },
330 {
331 .compatible = "socionext,uniphier-pro4-reset",
332 .data = uniphier_pro4_sys_reset_data,
333 },
334 {
335 .compatible = "socionext,uniphier-sld8-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900336 .data = uniphier_ld4_sys_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900337 },
338 {
339 .compatible = "socionext,uniphier-pro5-reset",
340 .data = uniphier_pro5_sys_reset_data,
341 },
342 {
343 .compatible = "socionext,uniphier-pxs2-reset",
344 .data = uniphier_pxs2_sys_reset_data,
345 },
346 {
347 .compatible = "socionext,uniphier-ld11-reset",
348 .data = uniphier_ld11_sys_reset_data,
349 },
350 {
351 .compatible = "socionext,uniphier-ld20-reset",
352 .data = uniphier_ld20_sys_reset_data,
353 },
Masahiro Yamada19eb4a42016-10-19 17:23:49 +0900354 /* Media I/O reset, SD reset */
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900355 {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900356 .compatible = "socionext,uniphier-ld4-mio-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900357 .data = uniphier_ld4_mio_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900358 },
359 {
360 .compatible = "socionext,uniphier-pro4-mio-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900361 .data = uniphier_ld4_mio_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900362 },
363 {
364 .compatible = "socionext,uniphier-sld8-mio-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900365 .data = uniphier_ld4_mio_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900366 },
367 {
Masahiro Yamada19eb4a42016-10-19 17:23:49 +0900368 .compatible = "socionext,uniphier-pro5-sd-reset",
369 .data = uniphier_pro5_sd_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900370 },
371 {
Masahiro Yamada19eb4a42016-10-19 17:23:49 +0900372 .compatible = "socionext,uniphier-pxs2-sd-reset",
373 .data = uniphier_pro5_sd_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900374 },
375 {
376 .compatible = "socionext,uniphier-ld11-mio-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900377 .data = uniphier_ld4_mio_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900378 },
379 {
Masahiro Yamada88a7f522017-01-15 04:04:46 +0900380 .compatible = "socionext,uniphier-ld11-sd-reset",
381 .data = uniphier_pro5_sd_reset_data,
382 },
383 {
Masahiro Yamada19eb4a42016-10-19 17:23:49 +0900384 .compatible = "socionext,uniphier-ld20-sd-reset",
385 .data = uniphier_pro5_sd_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900386 },
387 /* Peripheral reset */
388 {
389 .compatible = "socionext,uniphier-ld4-peri-reset",
390 .data = uniphier_ld4_peri_reset_data,
391 },
392 {
393 .compatible = "socionext,uniphier-pro4-peri-reset",
394 .data = uniphier_pro4_peri_reset_data,
395 },
396 {
397 .compatible = "socionext,uniphier-sld8-peri-reset",
398 .data = uniphier_ld4_peri_reset_data,
399 },
400 {
401 .compatible = "socionext,uniphier-pro5-peri-reset",
402 .data = uniphier_pro4_peri_reset_data,
403 },
404 {
405 .compatible = "socionext,uniphier-pxs2-peri-reset",
406 .data = uniphier_pro4_peri_reset_data,
407 },
408 {
409 .compatible = "socionext,uniphier-ld11-peri-reset",
410 .data = uniphier_pro4_peri_reset_data,
411 },
412 {
413 .compatible = "socionext,uniphier-ld20-peri-reset",
414 .data = uniphier_pro4_peri_reset_data,
415 },
416 { /* sentinel */ }
417};
418MODULE_DEVICE_TABLE(of, uniphier_reset_match);
419
420static struct platform_driver uniphier_reset_driver = {
421 .probe = uniphier_reset_probe,
422 .driver = {
423 .name = "uniphier-reset",
424 .of_match_table = uniphier_reset_match,
425 },
426};
427module_platform_driver(uniphier_reset_driver);
428
429MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
430MODULE_DESCRIPTION("UniPhier Reset Controller Driver");
431MODULE_LICENSE("GPL");