blob: f60c137c17cb991321386982de5657969920f478 [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 */
Katsuhiro Suzuki0f195432017-08-13 18:00:42 +090098 UNIPHIER_RESETX(42, 0x2010, 2), /* EXIV */
Masahiro Yamada54e991b2016-08-02 13:18:29 +090099 UNIPHIER_RESET_END,
100};
101
Wei Yongjun716adfe2017-02-08 15:56:20 +0000102static const struct uniphier_reset_data uniphier_ld20_sys_reset_data[] = {
Masahiro Yamadadec173c2017-08-06 11:44:02 +0900103 UNIPHIER_RESETX(2, 0x200c, 0), /* NAND */
104 UNIPHIER_RESETX(4, 0x200c, 2), /* eMMC */
105 UNIPHIER_RESETX(8, 0x200c, 8), /* STDMAC (HSC) */
106 UNIPHIER_RESETX(12, 0x200c, 5), /* GIO (PCIe, USB3) */
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900107 UNIPHIER_RESETX(16, 0x200c, 12), /* USB30-PHY0 */
108 UNIPHIER_RESETX(17, 0x200c, 13), /* USB30-PHY1 */
109 UNIPHIER_RESETX(18, 0x200c, 14), /* USB30-PHY2 */
110 UNIPHIER_RESETX(19, 0x200c, 15), /* USB30-PHY3 */
Katsuhiro Suzuki94e10c22017-08-13 18:00:41 +0900111 UNIPHIER_RESETX(40, 0x2008, 0), /* AIO */
112 UNIPHIER_RESETX(41, 0x2008, 1), /* EVEA */
Katsuhiro Suzuki0f195432017-08-13 18:00:42 +0900113 UNIPHIER_RESETX(42, 0x2010, 2), /* EXIV */
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900114 UNIPHIER_RESET_END,
115};
116
117/* Media I/O reset data */
118#define UNIPHIER_MIO_RESET_SD(id, ch) \
119 UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 0)
120
121#define UNIPHIER_MIO_RESET_SD_BRIDGE(id, ch) \
122 UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 26)
123
124#define UNIPHIER_MIO_RESET_EMMC_HW_RESET(id, ch) \
125 UNIPHIER_RESETX((id), 0x80 + 0x200 * (ch), 0)
126
127#define UNIPHIER_MIO_RESET_USB2(id, ch) \
128 UNIPHIER_RESETX((id), 0x114 + 0x200 * (ch), 0)
129
130#define UNIPHIER_MIO_RESET_USB2_BRIDGE(id, ch) \
131 UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 24)
132
133#define UNIPHIER_MIO_RESET_DMAC(id) \
134 UNIPHIER_RESETX((id), 0x110, 17)
135
Masahiro Yamada52810362017-08-06 11:44:01 +0900136static const struct uniphier_reset_data uniphier_ld4_mio_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900137 UNIPHIER_MIO_RESET_SD(0, 0),
138 UNIPHIER_MIO_RESET_SD(1, 1),
139 UNIPHIER_MIO_RESET_SD(2, 2),
140 UNIPHIER_MIO_RESET_SD_BRIDGE(3, 0),
141 UNIPHIER_MIO_RESET_SD_BRIDGE(4, 1),
142 UNIPHIER_MIO_RESET_SD_BRIDGE(5, 2),
143 UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
144 UNIPHIER_MIO_RESET_DMAC(7),
145 UNIPHIER_MIO_RESET_USB2(8, 0),
146 UNIPHIER_MIO_RESET_USB2(9, 1),
147 UNIPHIER_MIO_RESET_USB2(10, 2),
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900148 UNIPHIER_MIO_RESET_USB2_BRIDGE(12, 0),
149 UNIPHIER_MIO_RESET_USB2_BRIDGE(13, 1),
150 UNIPHIER_MIO_RESET_USB2_BRIDGE(14, 2),
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900151 UNIPHIER_RESET_END,
152};
153
Wei Yongjun716adfe2017-02-08 15:56:20 +0000154static const struct uniphier_reset_data uniphier_pro5_sd_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900155 UNIPHIER_MIO_RESET_SD(0, 0),
156 UNIPHIER_MIO_RESET_SD(1, 1),
157 UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
158 UNIPHIER_RESET_END,
159};
160
161/* Peripheral reset data */
162#define UNIPHIER_PERI_RESET_UART(id, ch) \
163 UNIPHIER_RESETX((id), 0x114, 19 + (ch))
164
165#define UNIPHIER_PERI_RESET_I2C(id, ch) \
166 UNIPHIER_RESETX((id), 0x114, 5 + (ch))
167
168#define UNIPHIER_PERI_RESET_FI2C(id, ch) \
169 UNIPHIER_RESETX((id), 0x114, 24 + (ch))
170
Wei Yongjun716adfe2017-02-08 15:56:20 +0000171static const struct uniphier_reset_data uniphier_ld4_peri_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900172 UNIPHIER_PERI_RESET_UART(0, 0),
173 UNIPHIER_PERI_RESET_UART(1, 1),
174 UNIPHIER_PERI_RESET_UART(2, 2),
175 UNIPHIER_PERI_RESET_UART(3, 3),
176 UNIPHIER_PERI_RESET_I2C(4, 0),
177 UNIPHIER_PERI_RESET_I2C(5, 1),
178 UNIPHIER_PERI_RESET_I2C(6, 2),
179 UNIPHIER_PERI_RESET_I2C(7, 3),
180 UNIPHIER_PERI_RESET_I2C(8, 4),
181 UNIPHIER_RESET_END,
182};
183
Wei Yongjun716adfe2017-02-08 15:56:20 +0000184static const struct uniphier_reset_data uniphier_pro4_peri_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900185 UNIPHIER_PERI_RESET_UART(0, 0),
186 UNIPHIER_PERI_RESET_UART(1, 1),
187 UNIPHIER_PERI_RESET_UART(2, 2),
188 UNIPHIER_PERI_RESET_UART(3, 3),
189 UNIPHIER_PERI_RESET_FI2C(4, 0),
190 UNIPHIER_PERI_RESET_FI2C(5, 1),
191 UNIPHIER_PERI_RESET_FI2C(6, 2),
192 UNIPHIER_PERI_RESET_FI2C(7, 3),
193 UNIPHIER_PERI_RESET_FI2C(8, 4),
194 UNIPHIER_PERI_RESET_FI2C(9, 5),
195 UNIPHIER_PERI_RESET_FI2C(10, 6),
196 UNIPHIER_RESET_END,
197};
198
199/* core implementaton */
200struct uniphier_reset_priv {
201 struct reset_controller_dev rcdev;
202 struct device *dev;
203 struct regmap *regmap;
204 const struct uniphier_reset_data *data;
205};
206
207#define to_uniphier_reset_priv(_rcdev) \
208 container_of(_rcdev, struct uniphier_reset_priv, rcdev)
209
210static int uniphier_reset_update(struct reset_controller_dev *rcdev,
211 unsigned long id, int assert)
212{
213 struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
214 const struct uniphier_reset_data *p;
215
216 for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
217 unsigned int mask, val;
218
219 if (p->id != id)
220 continue;
221
222 mask = BIT(p->bit);
223
224 if (assert)
225 val = mask;
226 else
227 val = ~mask;
228
229 if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
230 val = ~val;
231
232 return regmap_write_bits(priv->regmap, p->reg, mask, val);
233 }
234
235 dev_err(priv->dev, "reset_id=%lu was not handled\n", id);
236 return -EINVAL;
237}
238
239static int uniphier_reset_assert(struct reset_controller_dev *rcdev,
240 unsigned long id)
241{
242 return uniphier_reset_update(rcdev, id, 1);
243}
244
245static int uniphier_reset_deassert(struct reset_controller_dev *rcdev,
246 unsigned long id)
247{
248 return uniphier_reset_update(rcdev, id, 0);
249}
250
251static int uniphier_reset_status(struct reset_controller_dev *rcdev,
252 unsigned long id)
253{
254 struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
255 const struct uniphier_reset_data *p;
256
257 for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
258 unsigned int val;
259 int ret, asserted;
260
261 if (p->id != id)
262 continue;
263
264 ret = regmap_read(priv->regmap, p->reg, &val);
265 if (ret)
266 return ret;
267
268 asserted = !!(val & BIT(p->bit));
269
270 if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
271 asserted = !asserted;
272
273 return asserted;
274 }
275
276 dev_err(priv->dev, "reset_id=%lu was not found\n", id);
277 return -EINVAL;
278}
279
280static const struct reset_control_ops uniphier_reset_ops = {
281 .assert = uniphier_reset_assert,
282 .deassert = uniphier_reset_deassert,
283 .status = uniphier_reset_status,
284};
285
286static int uniphier_reset_probe(struct platform_device *pdev)
287{
288 struct device *dev = &pdev->dev;
289 struct uniphier_reset_priv *priv;
290 const struct uniphier_reset_data *p, *data;
291 struct regmap *regmap;
292 struct device_node *parent;
293 unsigned int nr_resets = 0;
294
295 data = of_device_get_match_data(dev);
296 if (WARN_ON(!data))
297 return -EINVAL;
298
299 parent = of_get_parent(dev->of_node); /* parent should be syscon node */
300 regmap = syscon_node_to_regmap(parent);
301 of_node_put(parent);
302 if (IS_ERR(regmap)) {
303 dev_err(dev, "failed to get regmap (error %ld)\n",
304 PTR_ERR(regmap));
305 return PTR_ERR(regmap);
306 }
307
308 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
309 if (!priv)
310 return -ENOMEM;
311
312 for (p = data; p->id != UNIPHIER_RESET_ID_END; p++)
313 nr_resets = max(nr_resets, p->id + 1);
314
315 priv->rcdev.ops = &uniphier_reset_ops;
316 priv->rcdev.owner = dev->driver->owner;
317 priv->rcdev.of_node = dev->of_node;
318 priv->rcdev.nr_resets = nr_resets;
319 priv->dev = dev;
320 priv->regmap = regmap;
321 priv->data = data;
322
323 return devm_reset_controller_register(&pdev->dev, &priv->rcdev);
324}
325
326static const struct of_device_id uniphier_reset_match[] = {
327 /* System reset */
328 {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900329 .compatible = "socionext,uniphier-ld4-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900330 .data = uniphier_ld4_sys_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900331 },
332 {
333 .compatible = "socionext,uniphier-pro4-reset",
334 .data = uniphier_pro4_sys_reset_data,
335 },
336 {
337 .compatible = "socionext,uniphier-sld8-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900338 .data = uniphier_ld4_sys_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900339 },
340 {
341 .compatible = "socionext,uniphier-pro5-reset",
342 .data = uniphier_pro5_sys_reset_data,
343 },
344 {
345 .compatible = "socionext,uniphier-pxs2-reset",
346 .data = uniphier_pxs2_sys_reset_data,
347 },
348 {
349 .compatible = "socionext,uniphier-ld11-reset",
350 .data = uniphier_ld11_sys_reset_data,
351 },
352 {
353 .compatible = "socionext,uniphier-ld20-reset",
354 .data = uniphier_ld20_sys_reset_data,
355 },
Masahiro Yamada19eb4a42016-10-19 17:23:49 +0900356 /* Media I/O reset, SD reset */
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900357 {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900358 .compatible = "socionext,uniphier-ld4-mio-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900359 .data = uniphier_ld4_mio_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900360 },
361 {
362 .compatible = "socionext,uniphier-pro4-mio-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900363 .data = uniphier_ld4_mio_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900364 },
365 {
366 .compatible = "socionext,uniphier-sld8-mio-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900367 .data = uniphier_ld4_mio_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900368 },
369 {
Masahiro Yamada19eb4a42016-10-19 17:23:49 +0900370 .compatible = "socionext,uniphier-pro5-sd-reset",
371 .data = uniphier_pro5_sd_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900372 },
373 {
Masahiro Yamada19eb4a42016-10-19 17:23:49 +0900374 .compatible = "socionext,uniphier-pxs2-sd-reset",
375 .data = uniphier_pro5_sd_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900376 },
377 {
378 .compatible = "socionext,uniphier-ld11-mio-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900379 .data = uniphier_ld4_mio_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900380 },
381 {
Masahiro Yamada88a7f522017-01-15 04:04:46 +0900382 .compatible = "socionext,uniphier-ld11-sd-reset",
383 .data = uniphier_pro5_sd_reset_data,
384 },
385 {
Masahiro Yamada19eb4a42016-10-19 17:23:49 +0900386 .compatible = "socionext,uniphier-ld20-sd-reset",
387 .data = uniphier_pro5_sd_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900388 },
389 /* Peripheral reset */
390 {
391 .compatible = "socionext,uniphier-ld4-peri-reset",
392 .data = uniphier_ld4_peri_reset_data,
393 },
394 {
395 .compatible = "socionext,uniphier-pro4-peri-reset",
396 .data = uniphier_pro4_peri_reset_data,
397 },
398 {
399 .compatible = "socionext,uniphier-sld8-peri-reset",
400 .data = uniphier_ld4_peri_reset_data,
401 },
402 {
403 .compatible = "socionext,uniphier-pro5-peri-reset",
404 .data = uniphier_pro4_peri_reset_data,
405 },
406 {
407 .compatible = "socionext,uniphier-pxs2-peri-reset",
408 .data = uniphier_pro4_peri_reset_data,
409 },
410 {
411 .compatible = "socionext,uniphier-ld11-peri-reset",
412 .data = uniphier_pro4_peri_reset_data,
413 },
414 {
415 .compatible = "socionext,uniphier-ld20-peri-reset",
416 .data = uniphier_pro4_peri_reset_data,
417 },
418 { /* sentinel */ }
419};
420MODULE_DEVICE_TABLE(of, uniphier_reset_match);
421
422static struct platform_driver uniphier_reset_driver = {
423 .probe = uniphier_reset_probe,
424 .driver = {
425 .name = "uniphier-reset",
426 .of_match_table = uniphier_reset_match,
427 },
428};
429module_platform_driver(uniphier_reset_driver);
430
431MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
432MODULE_DESCRIPTION("UniPhier Reset Controller Driver");
433MODULE_LICENSE("GPL");