blob: 3947fc2ef52cfd47ceb9d32584d33f3aecfdff4f [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 Yamada23ade392017-03-28 17:40:08 +090053#define UNIPHIER_LD11_SYS_RESET_NAND(id) \
54 UNIPHIER_RESETX((id), 0x200c, 0)
55
56#define UNIPHIER_LD11_SYS_RESET_EMMC(id) \
57 UNIPHIER_RESETX((id), 0x200c, 2)
58
Masahiro Yamada54e991b2016-08-02 13:18:29 +090059#define UNIPHIER_LD11_SYS_RESET_STDMAC(id) \
60 UNIPHIER_RESETX((id), 0x200c, 8)
61
62#define UNIPHIER_PRO4_SYS_RESET_GIO(id) \
63 UNIPHIER_RESETX((id), 0x2000, 6)
64
65#define UNIPHIER_LD20_SYS_RESET_GIO(id) \
66 UNIPHIER_RESETX((id), 0x200c, 5)
67
68#define UNIPHIER_PRO4_SYS_RESET_USB3(id, ch) \
69 UNIPHIER_RESETX((id), 0x2000 + 0x4 * (ch), 17)
70
Masahiro Yamada52810362017-08-06 11:44:01 +090071static const struct uniphier_reset_data uniphier_ld4_sys_reset_data[] = {
72 UNIPHIER_RESETX(2, 0x2000, 2), /* NAND */
73 UNIPHIER_RESETX(8, 0x2000, 10), /* STDMAC (Ether, HSC, MIO) */
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_pro4_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, MIO, RLE) */
Masahiro Yamada54e991b2016-08-02 13:18:29 +090080 UNIPHIER_PRO4_SYS_RESET_GIO(12), /* Ether, SATA, USB3 */
81 UNIPHIER_PRO4_SYS_RESET_USB3(14, 0),
82 UNIPHIER_PRO4_SYS_RESET_USB3(15, 1),
83 UNIPHIER_RESET_END,
84};
85
Wei Yongjun716adfe2017-02-08 15:56:20 +000086static const struct uniphier_reset_data uniphier_pro5_sys_reset_data[] = {
Masahiro Yamada52810362017-08-06 11:44:01 +090087 UNIPHIER_RESETX(2, 0x2000, 2), /* NAND */
88 UNIPHIER_RESETX(8, 0x2000, 10), /* STDMAC (HSC) */
Masahiro Yamada54e991b2016-08-02 13:18:29 +090089 UNIPHIER_PRO4_SYS_RESET_GIO(12), /* PCIe, USB3 */
90 UNIPHIER_PRO4_SYS_RESET_USB3(14, 0),
91 UNIPHIER_PRO4_SYS_RESET_USB3(15, 1),
92 UNIPHIER_RESET_END,
93};
94
Wei Yongjun716adfe2017-02-08 15:56:20 +000095static const struct uniphier_reset_data uniphier_pxs2_sys_reset_data[] = {
Masahiro Yamada52810362017-08-06 11:44:01 +090096 UNIPHIER_RESETX(2, 0x2000, 2), /* NAND */
97 UNIPHIER_RESETX(8, 0x2000, 10), /* STDMAC (HSC, RLE) */
Masahiro Yamada54e991b2016-08-02 13:18:29 +090098 UNIPHIER_PRO4_SYS_RESET_USB3(14, 0),
99 UNIPHIER_PRO4_SYS_RESET_USB3(15, 1),
100 UNIPHIER_RESETX(16, 0x2014, 4), /* USB30-PHY0 */
101 UNIPHIER_RESETX(17, 0x2014, 0), /* USB30-PHY1 */
102 UNIPHIER_RESETX(18, 0x2014, 2), /* USB30-PHY2 */
103 UNIPHIER_RESETX(20, 0x2014, 5), /* USB31-PHY0 */
104 UNIPHIER_RESETX(21, 0x2014, 1), /* USB31-PHY1 */
105 UNIPHIER_RESETX(28, 0x2014, 12), /* SATA */
106 UNIPHIER_RESET(29, 0x2014, 8), /* SATA-PHY (active high) */
107 UNIPHIER_RESET_END,
108};
109
Wei Yongjun716adfe2017-02-08 15:56:20 +0000110static const struct uniphier_reset_data uniphier_ld11_sys_reset_data[] = {
Masahiro Yamada23ade392017-03-28 17:40:08 +0900111 UNIPHIER_LD11_SYS_RESET_NAND(2),
112 UNIPHIER_LD11_SYS_RESET_EMMC(4),
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900113 UNIPHIER_LD11_SYS_RESET_STDMAC(8), /* HSC, MIO */
114 UNIPHIER_RESET_END,
115};
116
Wei Yongjun716adfe2017-02-08 15:56:20 +0000117static const struct uniphier_reset_data uniphier_ld20_sys_reset_data[] = {
Masahiro Yamada23ade392017-03-28 17:40:08 +0900118 UNIPHIER_LD11_SYS_RESET_NAND(2),
119 UNIPHIER_LD11_SYS_RESET_EMMC(4),
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900120 UNIPHIER_LD11_SYS_RESET_STDMAC(8), /* HSC */
121 UNIPHIER_LD20_SYS_RESET_GIO(12), /* PCIe, USB3 */
122 UNIPHIER_RESETX(16, 0x200c, 12), /* USB30-PHY0 */
123 UNIPHIER_RESETX(17, 0x200c, 13), /* USB30-PHY1 */
124 UNIPHIER_RESETX(18, 0x200c, 14), /* USB30-PHY2 */
125 UNIPHIER_RESETX(19, 0x200c, 15), /* USB30-PHY3 */
126 UNIPHIER_RESET_END,
127};
128
129/* Media I/O reset data */
130#define UNIPHIER_MIO_RESET_SD(id, ch) \
131 UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 0)
132
133#define UNIPHIER_MIO_RESET_SD_BRIDGE(id, ch) \
134 UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 26)
135
136#define UNIPHIER_MIO_RESET_EMMC_HW_RESET(id, ch) \
137 UNIPHIER_RESETX((id), 0x80 + 0x200 * (ch), 0)
138
139#define UNIPHIER_MIO_RESET_USB2(id, ch) \
140 UNIPHIER_RESETX((id), 0x114 + 0x200 * (ch), 0)
141
142#define UNIPHIER_MIO_RESET_USB2_BRIDGE(id, ch) \
143 UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 24)
144
145#define UNIPHIER_MIO_RESET_DMAC(id) \
146 UNIPHIER_RESETX((id), 0x110, 17)
147
Masahiro Yamada52810362017-08-06 11:44:01 +0900148static const struct uniphier_reset_data uniphier_ld4_mio_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900149 UNIPHIER_MIO_RESET_SD(0, 0),
150 UNIPHIER_MIO_RESET_SD(1, 1),
151 UNIPHIER_MIO_RESET_SD(2, 2),
152 UNIPHIER_MIO_RESET_SD_BRIDGE(3, 0),
153 UNIPHIER_MIO_RESET_SD_BRIDGE(4, 1),
154 UNIPHIER_MIO_RESET_SD_BRIDGE(5, 2),
155 UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
156 UNIPHIER_MIO_RESET_DMAC(7),
157 UNIPHIER_MIO_RESET_USB2(8, 0),
158 UNIPHIER_MIO_RESET_USB2(9, 1),
159 UNIPHIER_MIO_RESET_USB2(10, 2),
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900160 UNIPHIER_MIO_RESET_USB2_BRIDGE(12, 0),
161 UNIPHIER_MIO_RESET_USB2_BRIDGE(13, 1),
162 UNIPHIER_MIO_RESET_USB2_BRIDGE(14, 2),
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900163 UNIPHIER_RESET_END,
164};
165
Wei Yongjun716adfe2017-02-08 15:56:20 +0000166static const struct uniphier_reset_data uniphier_pro5_sd_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900167 UNIPHIER_MIO_RESET_SD(0, 0),
168 UNIPHIER_MIO_RESET_SD(1, 1),
169 UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
170 UNIPHIER_RESET_END,
171};
172
173/* Peripheral reset data */
174#define UNIPHIER_PERI_RESET_UART(id, ch) \
175 UNIPHIER_RESETX((id), 0x114, 19 + (ch))
176
177#define UNIPHIER_PERI_RESET_I2C(id, ch) \
178 UNIPHIER_RESETX((id), 0x114, 5 + (ch))
179
180#define UNIPHIER_PERI_RESET_FI2C(id, ch) \
181 UNIPHIER_RESETX((id), 0x114, 24 + (ch))
182
Wei Yongjun716adfe2017-02-08 15:56:20 +0000183static const struct uniphier_reset_data uniphier_ld4_peri_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900184 UNIPHIER_PERI_RESET_UART(0, 0),
185 UNIPHIER_PERI_RESET_UART(1, 1),
186 UNIPHIER_PERI_RESET_UART(2, 2),
187 UNIPHIER_PERI_RESET_UART(3, 3),
188 UNIPHIER_PERI_RESET_I2C(4, 0),
189 UNIPHIER_PERI_RESET_I2C(5, 1),
190 UNIPHIER_PERI_RESET_I2C(6, 2),
191 UNIPHIER_PERI_RESET_I2C(7, 3),
192 UNIPHIER_PERI_RESET_I2C(8, 4),
193 UNIPHIER_RESET_END,
194};
195
Wei Yongjun716adfe2017-02-08 15:56:20 +0000196static const struct uniphier_reset_data uniphier_pro4_peri_reset_data[] = {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900197 UNIPHIER_PERI_RESET_UART(0, 0),
198 UNIPHIER_PERI_RESET_UART(1, 1),
199 UNIPHIER_PERI_RESET_UART(2, 2),
200 UNIPHIER_PERI_RESET_UART(3, 3),
201 UNIPHIER_PERI_RESET_FI2C(4, 0),
202 UNIPHIER_PERI_RESET_FI2C(5, 1),
203 UNIPHIER_PERI_RESET_FI2C(6, 2),
204 UNIPHIER_PERI_RESET_FI2C(7, 3),
205 UNIPHIER_PERI_RESET_FI2C(8, 4),
206 UNIPHIER_PERI_RESET_FI2C(9, 5),
207 UNIPHIER_PERI_RESET_FI2C(10, 6),
208 UNIPHIER_RESET_END,
209};
210
211/* core implementaton */
212struct uniphier_reset_priv {
213 struct reset_controller_dev rcdev;
214 struct device *dev;
215 struct regmap *regmap;
216 const struct uniphier_reset_data *data;
217};
218
219#define to_uniphier_reset_priv(_rcdev) \
220 container_of(_rcdev, struct uniphier_reset_priv, rcdev)
221
222static int uniphier_reset_update(struct reset_controller_dev *rcdev,
223 unsigned long id, int assert)
224{
225 struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
226 const struct uniphier_reset_data *p;
227
228 for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
229 unsigned int mask, val;
230
231 if (p->id != id)
232 continue;
233
234 mask = BIT(p->bit);
235
236 if (assert)
237 val = mask;
238 else
239 val = ~mask;
240
241 if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
242 val = ~val;
243
244 return regmap_write_bits(priv->regmap, p->reg, mask, val);
245 }
246
247 dev_err(priv->dev, "reset_id=%lu was not handled\n", id);
248 return -EINVAL;
249}
250
251static int uniphier_reset_assert(struct reset_controller_dev *rcdev,
252 unsigned long id)
253{
254 return uniphier_reset_update(rcdev, id, 1);
255}
256
257static int uniphier_reset_deassert(struct reset_controller_dev *rcdev,
258 unsigned long id)
259{
260 return uniphier_reset_update(rcdev, id, 0);
261}
262
263static int uniphier_reset_status(struct reset_controller_dev *rcdev,
264 unsigned long id)
265{
266 struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
267 const struct uniphier_reset_data *p;
268
269 for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
270 unsigned int val;
271 int ret, asserted;
272
273 if (p->id != id)
274 continue;
275
276 ret = regmap_read(priv->regmap, p->reg, &val);
277 if (ret)
278 return ret;
279
280 asserted = !!(val & BIT(p->bit));
281
282 if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
283 asserted = !asserted;
284
285 return asserted;
286 }
287
288 dev_err(priv->dev, "reset_id=%lu was not found\n", id);
289 return -EINVAL;
290}
291
292static const struct reset_control_ops uniphier_reset_ops = {
293 .assert = uniphier_reset_assert,
294 .deassert = uniphier_reset_deassert,
295 .status = uniphier_reset_status,
296};
297
298static int uniphier_reset_probe(struct platform_device *pdev)
299{
300 struct device *dev = &pdev->dev;
301 struct uniphier_reset_priv *priv;
302 const struct uniphier_reset_data *p, *data;
303 struct regmap *regmap;
304 struct device_node *parent;
305 unsigned int nr_resets = 0;
306
307 data = of_device_get_match_data(dev);
308 if (WARN_ON(!data))
309 return -EINVAL;
310
311 parent = of_get_parent(dev->of_node); /* parent should be syscon node */
312 regmap = syscon_node_to_regmap(parent);
313 of_node_put(parent);
314 if (IS_ERR(regmap)) {
315 dev_err(dev, "failed to get regmap (error %ld)\n",
316 PTR_ERR(regmap));
317 return PTR_ERR(regmap);
318 }
319
320 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
321 if (!priv)
322 return -ENOMEM;
323
324 for (p = data; p->id != UNIPHIER_RESET_ID_END; p++)
325 nr_resets = max(nr_resets, p->id + 1);
326
327 priv->rcdev.ops = &uniphier_reset_ops;
328 priv->rcdev.owner = dev->driver->owner;
329 priv->rcdev.of_node = dev->of_node;
330 priv->rcdev.nr_resets = nr_resets;
331 priv->dev = dev;
332 priv->regmap = regmap;
333 priv->data = data;
334
335 return devm_reset_controller_register(&pdev->dev, &priv->rcdev);
336}
337
338static const struct of_device_id uniphier_reset_match[] = {
339 /* System reset */
340 {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900341 .compatible = "socionext,uniphier-ld4-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900342 .data = uniphier_ld4_sys_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900343 },
344 {
345 .compatible = "socionext,uniphier-pro4-reset",
346 .data = uniphier_pro4_sys_reset_data,
347 },
348 {
349 .compatible = "socionext,uniphier-sld8-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900350 .data = uniphier_ld4_sys_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900351 },
352 {
353 .compatible = "socionext,uniphier-pro5-reset",
354 .data = uniphier_pro5_sys_reset_data,
355 },
356 {
357 .compatible = "socionext,uniphier-pxs2-reset",
358 .data = uniphier_pxs2_sys_reset_data,
359 },
360 {
361 .compatible = "socionext,uniphier-ld11-reset",
362 .data = uniphier_ld11_sys_reset_data,
363 },
364 {
365 .compatible = "socionext,uniphier-ld20-reset",
366 .data = uniphier_ld20_sys_reset_data,
367 },
Masahiro Yamada19eb4a42016-10-19 17:23:49 +0900368 /* Media I/O reset, SD reset */
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900369 {
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900370 .compatible = "socionext,uniphier-ld4-mio-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900371 .data = uniphier_ld4_mio_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900372 },
373 {
374 .compatible = "socionext,uniphier-pro4-mio-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900375 .data = uniphier_ld4_mio_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900376 },
377 {
378 .compatible = "socionext,uniphier-sld8-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 Yamada19eb4a42016-10-19 17:23:49 +0900382 .compatible = "socionext,uniphier-pro5-sd-reset",
383 .data = uniphier_pro5_sd_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900384 },
385 {
Masahiro Yamada19eb4a42016-10-19 17:23:49 +0900386 .compatible = "socionext,uniphier-pxs2-sd-reset",
387 .data = uniphier_pro5_sd_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900388 },
389 {
390 .compatible = "socionext,uniphier-ld11-mio-reset",
Masahiro Yamada52810362017-08-06 11:44:01 +0900391 .data = uniphier_ld4_mio_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900392 },
393 {
Masahiro Yamada88a7f522017-01-15 04:04:46 +0900394 .compatible = "socionext,uniphier-ld11-sd-reset",
395 .data = uniphier_pro5_sd_reset_data,
396 },
397 {
Masahiro Yamada19eb4a42016-10-19 17:23:49 +0900398 .compatible = "socionext,uniphier-ld20-sd-reset",
399 .data = uniphier_pro5_sd_reset_data,
Masahiro Yamada54e991b2016-08-02 13:18:29 +0900400 },
401 /* Peripheral reset */
402 {
403 .compatible = "socionext,uniphier-ld4-peri-reset",
404 .data = uniphier_ld4_peri_reset_data,
405 },
406 {
407 .compatible = "socionext,uniphier-pro4-peri-reset",
408 .data = uniphier_pro4_peri_reset_data,
409 },
410 {
411 .compatible = "socionext,uniphier-sld8-peri-reset",
412 .data = uniphier_ld4_peri_reset_data,
413 },
414 {
415 .compatible = "socionext,uniphier-pro5-peri-reset",
416 .data = uniphier_pro4_peri_reset_data,
417 },
418 {
419 .compatible = "socionext,uniphier-pxs2-peri-reset",
420 .data = uniphier_pro4_peri_reset_data,
421 },
422 {
423 .compatible = "socionext,uniphier-ld11-peri-reset",
424 .data = uniphier_pro4_peri_reset_data,
425 },
426 {
427 .compatible = "socionext,uniphier-ld20-peri-reset",
428 .data = uniphier_pro4_peri_reset_data,
429 },
430 { /* sentinel */ }
431};
432MODULE_DEVICE_TABLE(of, uniphier_reset_match);
433
434static struct platform_driver uniphier_reset_driver = {
435 .probe = uniphier_reset_probe,
436 .driver = {
437 .name = "uniphier-reset",
438 .of_match_table = uniphier_reset_match,
439 },
440};
441module_platform_driver(uniphier_reset_driver);
442
443MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
444MODULE_DESCRIPTION("UniPhier Reset Controller Driver");
445MODULE_LICENSE("GPL");