Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2016 Socionext Inc.
  4 *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  5 */
  6
  7#include <linux/mfd/syscon.h>
  8#include <linux/module.h>
  9#include <linux/of.h>
 
 10#include <linux/platform_device.h>
 11#include <linux/regmap.h>
 12#include <linux/reset-controller.h>
 13
 14struct uniphier_reset_data {
 15	unsigned int id;
 16	unsigned int reg;
 17	unsigned int bit;
 18	unsigned int flags;
 19#define UNIPHIER_RESET_ACTIVE_LOW		BIT(0)
 20};
 21
 22#define UNIPHIER_RESET_ID_END		((unsigned int)(-1))
 23
 24#define UNIPHIER_RESET_END				\
 25	{ .id = UNIPHIER_RESET_ID_END }
 26
 27#define UNIPHIER_RESET(_id, _reg, _bit)			\
 28	{						\
 29		.id = (_id),				\
 30		.reg = (_reg),				\
 31		.bit = (_bit),				\
 32	}
 33
 34#define UNIPHIER_RESETX(_id, _reg, _bit)		\
 35	{						\
 36		.id = (_id),				\
 37		.reg = (_reg),				\
 38		.bit = (_bit),				\
 39		.flags = UNIPHIER_RESET_ACTIVE_LOW,	\
 40	}
 41
 42/* System reset data */
 43static const struct uniphier_reset_data uniphier_ld4_sys_reset_data[] = {
 44	UNIPHIER_RESETX(2, 0x2000, 2),		/* NAND */
 45	UNIPHIER_RESETX(8, 0x2000, 10),		/* STDMAC (Ether, HSC, MIO) */
 46	UNIPHIER_RESET_END,
 47};
 48
 49static const struct uniphier_reset_data uniphier_pro4_sys_reset_data[] = {
 50	UNIPHIER_RESETX(2, 0x2000, 2),		/* NAND */
 51	UNIPHIER_RESETX(6, 0x2000, 12),		/* Ether */
 52	UNIPHIER_RESETX(8, 0x2000, 10),		/* STDMAC (HSC, MIO, RLE) */
 53	UNIPHIER_RESETX(12, 0x2000, 6),		/* GIO (Ether, SATA, USB3) */
 54	UNIPHIER_RESETX(14, 0x2000, 17),	/* USB30 */
 55	UNIPHIER_RESETX(15, 0x2004, 17),	/* USB31 */
 56	UNIPHIER_RESETX(28, 0x2000, 18),	/* SATA0 */
 57	UNIPHIER_RESETX(29, 0x2004, 18),	/* SATA1 */
 58	UNIPHIER_RESETX(30, 0x2000, 19),	/* SATA-PHY */
 59	UNIPHIER_RESETX(40, 0x2000, 13),	/* AIO */
 60	UNIPHIER_RESET_END,
 61};
 62
 63static const struct uniphier_reset_data uniphier_pro5_sys_reset_data[] = {
 64	UNIPHIER_RESETX(2, 0x2000, 2),		/* NAND */
 65	UNIPHIER_RESETX(8, 0x2000, 10),		/* STDMAC (HSC) */
 66	UNIPHIER_RESETX(12, 0x2000, 6),		/* GIO (PCIe, USB3) */
 67	UNIPHIER_RESETX(14, 0x2000, 17),	/* USB30 */
 68	UNIPHIER_RESETX(15, 0x2004, 17),	/* USB31 */
 69	UNIPHIER_RESETX(24, 0x2008, 2),		/* PCIe */
 70	UNIPHIER_RESETX(40, 0x2000, 13),	/* AIO */
 71	UNIPHIER_RESET_END,
 72};
 73
 74static const struct uniphier_reset_data uniphier_pxs2_sys_reset_data[] = {
 75	UNIPHIER_RESETX(2, 0x2000, 2),		/* NAND */
 76	UNIPHIER_RESETX(6, 0x2000, 12),		/* Ether */
 77	UNIPHIER_RESETX(8, 0x2000, 10),		/* STDMAC (HSC, RLE) */
 78	UNIPHIER_RESETX(14, 0x2000, 17),	/* USB30 */
 79	UNIPHIER_RESETX(15, 0x2004, 17),	/* USB31 */
 80	UNIPHIER_RESETX(16, 0x2014, 4),		/* USB30-PHY0 */
 81	UNIPHIER_RESETX(17, 0x2014, 0),		/* USB30-PHY1 */
 82	UNIPHIER_RESETX(18, 0x2014, 2),		/* USB30-PHY2 */
 83	UNIPHIER_RESETX(20, 0x2014, 5),		/* USB31-PHY0 */
 84	UNIPHIER_RESETX(21, 0x2014, 1),		/* USB31-PHY1 */
 85	UNIPHIER_RESETX(28, 0x2014, 12),	/* SATA */
 86	UNIPHIER_RESET(30, 0x2014, 8),		/* SATA-PHY (active high) */
 87	UNIPHIER_RESETX(40, 0x2000, 13),	/* AIO */
 88	UNIPHIER_RESET_END,
 89};
 90
 91static const struct uniphier_reset_data uniphier_ld11_sys_reset_data[] = {
 92	UNIPHIER_RESETX(2, 0x200c, 0),		/* NAND */
 93	UNIPHIER_RESETX(4, 0x200c, 2),		/* eMMC */
 94	UNIPHIER_RESETX(6, 0x200c, 6),		/* Ether */
 95	UNIPHIER_RESETX(8, 0x200c, 8),		/* STDMAC (HSC, MIO) */
 96	UNIPHIER_RESETX(9, 0x200c, 9),		/* HSC */
 97	UNIPHIER_RESETX(40, 0x2008, 0),		/* AIO */
 98	UNIPHIER_RESETX(41, 0x2008, 1),		/* EVEA */
 99	UNIPHIER_RESETX(42, 0x2010, 2),		/* EXIV */
100	UNIPHIER_RESET_END,
101};
102
103static const struct uniphier_reset_data uniphier_ld20_sys_reset_data[] = {
104	UNIPHIER_RESETX(2, 0x200c, 0),		/* NAND */
105	UNIPHIER_RESETX(4, 0x200c, 2),		/* eMMC */
106	UNIPHIER_RESETX(6, 0x200c, 6),		/* Ether */
107	UNIPHIER_RESETX(8, 0x200c, 8),		/* STDMAC (HSC) */
108	UNIPHIER_RESETX(9, 0x200c, 9),		/* HSC */
109	UNIPHIER_RESETX(14, 0x200c, 5),		/* USB30 */
110	UNIPHIER_RESETX(16, 0x200c, 12),	/* USB30-PHY0 */
111	UNIPHIER_RESETX(17, 0x200c, 13),	/* USB30-PHY1 */
112	UNIPHIER_RESETX(18, 0x200c, 14),	/* USB30-PHY2 */
113	UNIPHIER_RESETX(19, 0x200c, 15),	/* USB30-PHY3 */
114	UNIPHIER_RESETX(24, 0x200c, 4),		/* PCIe */
115	UNIPHIER_RESETX(40, 0x2008, 0),		/* AIO */
116	UNIPHIER_RESETX(41, 0x2008, 1),		/* EVEA */
117	UNIPHIER_RESETX(42, 0x2010, 2),		/* EXIV */
118	UNIPHIER_RESET_END,
119};
120
121static const struct uniphier_reset_data uniphier_pxs3_sys_reset_data[] = {
122	UNIPHIER_RESETX(2, 0x200c, 0),		/* NAND */
123	UNIPHIER_RESETX(4, 0x200c, 2),		/* eMMC */
124	UNIPHIER_RESETX(6, 0x200c, 9),		/* Ether0 */
125	UNIPHIER_RESETX(7, 0x200c, 10),		/* Ether1 */
126	UNIPHIER_RESETX(8, 0x200c, 12),		/* STDMAC */
127	UNIPHIER_RESETX(12, 0x200c, 4),		/* USB30 link */
128	UNIPHIER_RESETX(13, 0x200c, 5),		/* USB31 link */
129	UNIPHIER_RESETX(16, 0x200c, 16),	/* USB30-PHY0 */
130	UNIPHIER_RESETX(17, 0x200c, 18),	/* USB30-PHY1 */
131	UNIPHIER_RESETX(18, 0x200c, 20),	/* USB30-PHY2 */
132	UNIPHIER_RESETX(20, 0x200c, 17),	/* USB31-PHY0 */
133	UNIPHIER_RESETX(21, 0x200c, 19),	/* USB31-PHY1 */
134	UNIPHIER_RESETX(24, 0x200c, 3),		/* PCIe */
135	UNIPHIER_RESETX(28, 0x200c, 7),		/* SATA0 */
136	UNIPHIER_RESETX(29, 0x200c, 8),		/* SATA1 */
137	UNIPHIER_RESETX(30, 0x200c, 21),	/* SATA-PHY */
138	UNIPHIER_RESETX(40, 0x2008, 0),		/* AIO */
139	UNIPHIER_RESETX(42, 0x2010, 2),		/* EXIV */
140	UNIPHIER_RESET_END,
141};
142
143static const struct uniphier_reset_data uniphier_nx1_sys_reset_data[] = {
144	UNIPHIER_RESETX(4, 0x2008, 8),		/* eMMC */
145	UNIPHIER_RESETX(6, 0x200c, 0),		/* Ether */
146	UNIPHIER_RESETX(12, 0x200c, 16),        /* USB30 link */
147	UNIPHIER_RESETX(16, 0x200c, 24),        /* USB30-PHY0 */
148	UNIPHIER_RESETX(17, 0x200c, 25),        /* USB30-PHY1 */
149	UNIPHIER_RESETX(18, 0x200c, 26),        /* USB30-PHY2 */
150	UNIPHIER_RESETX(24, 0x200c, 8),         /* PCIe */
151	UNIPHIER_RESETX(52, 0x2010, 0),         /* VOC */
152	UNIPHIER_RESETX(58, 0x2010, 8),         /* HDMI-Tx */
153	UNIPHIER_RESET_END,
154};
155
156/* Media I/O reset data */
157#define UNIPHIER_MIO_RESET_SD(id, ch)			\
158	UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 0)
159
160#define UNIPHIER_MIO_RESET_SD_BRIDGE(id, ch)		\
161	UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 26)
162
163#define UNIPHIER_MIO_RESET_EMMC_HW_RESET(id, ch)	\
164	UNIPHIER_RESETX((id), 0x80 + 0x200 * (ch), 0)
165
166#define UNIPHIER_MIO_RESET_USB2(id, ch)			\
167	UNIPHIER_RESETX((id), 0x114 + 0x200 * (ch), 0)
168
169#define UNIPHIER_MIO_RESET_USB2_BRIDGE(id, ch)		\
170	UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 24)
171
172#define UNIPHIER_MIO_RESET_DMAC(id)			\
173	UNIPHIER_RESETX((id), 0x110, 17)
174
175static const struct uniphier_reset_data uniphier_ld4_mio_reset_data[] = {
176	UNIPHIER_MIO_RESET_SD(0, 0),
177	UNIPHIER_MIO_RESET_SD(1, 1),
178	UNIPHIER_MIO_RESET_SD(2, 2),
179	UNIPHIER_MIO_RESET_SD_BRIDGE(3, 0),
180	UNIPHIER_MIO_RESET_SD_BRIDGE(4, 1),
181	UNIPHIER_MIO_RESET_SD_BRIDGE(5, 2),
182	UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
183	UNIPHIER_MIO_RESET_DMAC(7),
184	UNIPHIER_MIO_RESET_USB2(8, 0),
185	UNIPHIER_MIO_RESET_USB2(9, 1),
186	UNIPHIER_MIO_RESET_USB2(10, 2),
187	UNIPHIER_MIO_RESET_USB2_BRIDGE(12, 0),
188	UNIPHIER_MIO_RESET_USB2_BRIDGE(13, 1),
189	UNIPHIER_MIO_RESET_USB2_BRIDGE(14, 2),
190	UNIPHIER_RESET_END,
191};
192
193static const struct uniphier_reset_data uniphier_pro5_sd_reset_data[] = {
194	UNIPHIER_MIO_RESET_SD(0, 0),
195	UNIPHIER_MIO_RESET_SD(1, 1),
196	UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
197	UNIPHIER_RESET_END,
198};
199
200/* Peripheral reset data */
201#define UNIPHIER_PERI_RESET_UART(id, ch)		\
202	UNIPHIER_RESETX((id), 0x114, 19 + (ch))
203
204#define UNIPHIER_PERI_RESET_I2C(id, ch)			\
205	UNIPHIER_RESETX((id), 0x114, 5 + (ch))
206
207#define UNIPHIER_PERI_RESET_FI2C(id, ch)		\
208	UNIPHIER_RESETX((id), 0x114, 24 + (ch))
209
210#define UNIPHIER_PERI_RESET_SCSSI(id, ch)		\
211	UNIPHIER_RESETX((id), 0x110, 17 + (ch))
212
213#define UNIPHIER_PERI_RESET_MCSSI(id)			\
214	UNIPHIER_RESETX((id), 0x114, 14)
215
216static const struct uniphier_reset_data uniphier_ld4_peri_reset_data[] = {
217	UNIPHIER_PERI_RESET_UART(0, 0),
218	UNIPHIER_PERI_RESET_UART(1, 1),
219	UNIPHIER_PERI_RESET_UART(2, 2),
220	UNIPHIER_PERI_RESET_UART(3, 3),
221	UNIPHIER_PERI_RESET_I2C(4, 0),
222	UNIPHIER_PERI_RESET_I2C(5, 1),
223	UNIPHIER_PERI_RESET_I2C(6, 2),
224	UNIPHIER_PERI_RESET_I2C(7, 3),
225	UNIPHIER_PERI_RESET_I2C(8, 4),
226	UNIPHIER_PERI_RESET_SCSSI(11, 0),
227	UNIPHIER_RESET_END,
228};
229
230static const struct uniphier_reset_data uniphier_pro4_peri_reset_data[] = {
231	UNIPHIER_PERI_RESET_UART(0, 0),
232	UNIPHIER_PERI_RESET_UART(1, 1),
233	UNIPHIER_PERI_RESET_UART(2, 2),
234	UNIPHIER_PERI_RESET_UART(3, 3),
235	UNIPHIER_PERI_RESET_FI2C(4, 0),
236	UNIPHIER_PERI_RESET_FI2C(5, 1),
237	UNIPHIER_PERI_RESET_FI2C(6, 2),
238	UNIPHIER_PERI_RESET_FI2C(7, 3),
239	UNIPHIER_PERI_RESET_FI2C(8, 4),
240	UNIPHIER_PERI_RESET_FI2C(9, 5),
241	UNIPHIER_PERI_RESET_FI2C(10, 6),
242	UNIPHIER_PERI_RESET_SCSSI(11, 0),
243	UNIPHIER_PERI_RESET_SCSSI(12, 1),
244	UNIPHIER_PERI_RESET_SCSSI(13, 2),
245	UNIPHIER_PERI_RESET_SCSSI(14, 3),
246	UNIPHIER_PERI_RESET_MCSSI(15),
247	UNIPHIER_RESET_END,
248};
249
250/* Analog signal amplifiers reset data */
251static const struct uniphier_reset_data uniphier_ld11_adamv_reset_data[] = {
252	UNIPHIER_RESETX(0, 0x10, 6), /* EVEA */
253	UNIPHIER_RESET_END,
254};
255
256/* core implementaton */
257struct uniphier_reset_priv {
258	struct reset_controller_dev rcdev;
259	struct device *dev;
260	struct regmap *regmap;
261	const struct uniphier_reset_data *data;
262};
263
264#define to_uniphier_reset_priv(_rcdev) \
265			container_of(_rcdev, struct uniphier_reset_priv, rcdev)
266
267static int uniphier_reset_update(struct reset_controller_dev *rcdev,
268				 unsigned long id, int assert)
269{
270	struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
271	const struct uniphier_reset_data *p;
272
273	for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
274		unsigned int mask, val;
275
276		if (p->id != id)
277			continue;
278
279		mask = BIT(p->bit);
280
281		if (assert)
282			val = mask;
283		else
284			val = ~mask;
285
286		if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
287			val = ~val;
288
289		return regmap_write_bits(priv->regmap, p->reg, mask, val);
290	}
291
292	dev_err(priv->dev, "reset_id=%lu was not handled\n", id);
293	return -EINVAL;
294}
295
296static int uniphier_reset_assert(struct reset_controller_dev *rcdev,
297				 unsigned long id)
298{
299	return uniphier_reset_update(rcdev, id, 1);
300}
301
302static int uniphier_reset_deassert(struct reset_controller_dev *rcdev,
303				   unsigned long id)
304{
305	return uniphier_reset_update(rcdev, id, 0);
306}
307
308static int uniphier_reset_status(struct reset_controller_dev *rcdev,
309				 unsigned long id)
310{
311	struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
312	const struct uniphier_reset_data *p;
313
314	for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
315		unsigned int val;
316		int ret, asserted;
317
318		if (p->id != id)
319			continue;
320
321		ret = regmap_read(priv->regmap, p->reg, &val);
322		if (ret)
323			return ret;
324
325		asserted = !!(val & BIT(p->bit));
326
327		if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
328			asserted = !asserted;
329
330		return asserted;
331	}
332
333	dev_err(priv->dev, "reset_id=%lu was not found\n", id);
334	return -EINVAL;
335}
336
337static const struct reset_control_ops uniphier_reset_ops = {
338	.assert = uniphier_reset_assert,
339	.deassert = uniphier_reset_deassert,
340	.status = uniphier_reset_status,
341};
342
343static int uniphier_reset_probe(struct platform_device *pdev)
344{
345	struct device *dev = &pdev->dev;
346	struct uniphier_reset_priv *priv;
347	const struct uniphier_reset_data *p, *data;
348	struct regmap *regmap;
349	struct device_node *parent;
350	unsigned int nr_resets = 0;
351
352	data = of_device_get_match_data(dev);
353	if (WARN_ON(!data))
354		return -EINVAL;
355
356	parent = of_get_parent(dev->of_node); /* parent should be syscon node */
357	regmap = syscon_node_to_regmap(parent);
358	of_node_put(parent);
359	if (IS_ERR(regmap)) {
360		dev_err(dev, "failed to get regmap (error %ld)\n",
361			PTR_ERR(regmap));
362		return PTR_ERR(regmap);
363	}
364
365	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
366	if (!priv)
367		return -ENOMEM;
368
369	for (p = data; p->id != UNIPHIER_RESET_ID_END; p++)
370		nr_resets = max(nr_resets, p->id + 1);
371
372	priv->rcdev.ops = &uniphier_reset_ops;
373	priv->rcdev.owner = dev->driver->owner;
374	priv->rcdev.of_node = dev->of_node;
375	priv->rcdev.nr_resets = nr_resets;
376	priv->dev = dev;
377	priv->regmap = regmap;
378	priv->data = data;
379
380	return devm_reset_controller_register(&pdev->dev, &priv->rcdev);
381}
382
383static const struct of_device_id uniphier_reset_match[] = {
384	/* System reset */
385	{
386		.compatible = "socionext,uniphier-ld4-reset",
387		.data = uniphier_ld4_sys_reset_data,
388	},
389	{
390		.compatible = "socionext,uniphier-pro4-reset",
391		.data = uniphier_pro4_sys_reset_data,
392	},
393	{
394		.compatible = "socionext,uniphier-sld8-reset",
395		.data = uniphier_ld4_sys_reset_data,
396	},
397	{
398		.compatible = "socionext,uniphier-pro5-reset",
399		.data = uniphier_pro5_sys_reset_data,
400	},
401	{
402		.compatible = "socionext,uniphier-pxs2-reset",
403		.data = uniphier_pxs2_sys_reset_data,
404	},
405	{
406		.compatible = "socionext,uniphier-ld11-reset",
407		.data = uniphier_ld11_sys_reset_data,
408	},
409	{
410		.compatible = "socionext,uniphier-ld20-reset",
411		.data = uniphier_ld20_sys_reset_data,
412	},
413	{
414		.compatible = "socionext,uniphier-pxs3-reset",
415		.data = uniphier_pxs3_sys_reset_data,
416	},
417	{
418		.compatible = "socionext,uniphier-nx1-reset",
419		.data = uniphier_nx1_sys_reset_data,
420	},
421	/* Media I/O reset, SD reset */
422	{
423		.compatible = "socionext,uniphier-ld4-mio-reset",
424		.data = uniphier_ld4_mio_reset_data,
425	},
426	{
427		.compatible = "socionext,uniphier-pro4-mio-reset",
428		.data = uniphier_ld4_mio_reset_data,
429	},
430	{
431		.compatible = "socionext,uniphier-sld8-mio-reset",
432		.data = uniphier_ld4_mio_reset_data,
433	},
434	{
435		.compatible = "socionext,uniphier-pro5-sd-reset",
436		.data = uniphier_pro5_sd_reset_data,
437	},
438	{
439		.compatible = "socionext,uniphier-pxs2-sd-reset",
440		.data = uniphier_pro5_sd_reset_data,
441	},
442	{
443		.compatible = "socionext,uniphier-ld11-mio-reset",
444		.data = uniphier_ld4_mio_reset_data,
445	},
446	{
447		.compatible = "socionext,uniphier-ld11-sd-reset",
448		.data = uniphier_pro5_sd_reset_data,
449	},
450	{
451		.compatible = "socionext,uniphier-ld20-sd-reset",
452		.data = uniphier_pro5_sd_reset_data,
453	},
454	{
455		.compatible = "socionext,uniphier-pxs3-sd-reset",
456		.data = uniphier_pro5_sd_reset_data,
457	},
458	{
459		.compatible = "socionext,uniphier-nx1-sd-reset",
460		.data = uniphier_pro5_sd_reset_data,
461	},
462	/* Peripheral reset */
463	{
464		.compatible = "socionext,uniphier-ld4-peri-reset",
465		.data = uniphier_ld4_peri_reset_data,
466	},
467	{
468		.compatible = "socionext,uniphier-pro4-peri-reset",
469		.data = uniphier_pro4_peri_reset_data,
470	},
471	{
472		.compatible = "socionext,uniphier-sld8-peri-reset",
473		.data = uniphier_ld4_peri_reset_data,
474	},
475	{
476		.compatible = "socionext,uniphier-pro5-peri-reset",
477		.data = uniphier_pro4_peri_reset_data,
478	},
479	{
480		.compatible = "socionext,uniphier-pxs2-peri-reset",
481		.data = uniphier_pro4_peri_reset_data,
482	},
483	{
484		.compatible = "socionext,uniphier-ld11-peri-reset",
485		.data = uniphier_pro4_peri_reset_data,
486	},
487	{
488		.compatible = "socionext,uniphier-ld20-peri-reset",
489		.data = uniphier_pro4_peri_reset_data,
490	},
491	{
492		.compatible = "socionext,uniphier-pxs3-peri-reset",
493		.data = uniphier_pro4_peri_reset_data,
494	},
495	{
496		.compatible = "socionext,uniphier-nx1-peri-reset",
497		.data = uniphier_pro4_peri_reset_data,
498	},
499	/* Analog signal amplifiers reset */
500	{
501		.compatible = "socionext,uniphier-ld11-adamv-reset",
502		.data = uniphier_ld11_adamv_reset_data,
503	},
504	{
505		.compatible = "socionext,uniphier-ld20-adamv-reset",
506		.data = uniphier_ld11_adamv_reset_data,
507	},
508	{ /* sentinel */ }
509};
510MODULE_DEVICE_TABLE(of, uniphier_reset_match);
511
512static struct platform_driver uniphier_reset_driver = {
513	.probe = uniphier_reset_probe,
514	.driver = {
515		.name = "uniphier-reset",
516		.of_match_table = uniphier_reset_match,
517	},
518};
519module_platform_driver(uniphier_reset_driver);
520
521MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
522MODULE_DESCRIPTION("UniPhier Reset Controller Driver");
523MODULE_LICENSE("GPL");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2016 Socionext Inc.
  4 *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  5 */
  6
  7#include <linux/mfd/syscon.h>
  8#include <linux/module.h>
  9#include <linux/of.h>
 10#include <linux/of_device.h>
 11#include <linux/platform_device.h>
 12#include <linux/regmap.h>
 13#include <linux/reset-controller.h>
 14
 15struct uniphier_reset_data {
 16	unsigned int id;
 17	unsigned int reg;
 18	unsigned int bit;
 19	unsigned int flags;
 20#define UNIPHIER_RESET_ACTIVE_LOW		BIT(0)
 21};
 22
 23#define UNIPHIER_RESET_ID_END		(unsigned int)(-1)
 24
 25#define UNIPHIER_RESET_END				\
 26	{ .id = UNIPHIER_RESET_ID_END }
 27
 28#define UNIPHIER_RESET(_id, _reg, _bit)			\
 29	{						\
 30		.id = (_id),				\
 31		.reg = (_reg),				\
 32		.bit = (_bit),				\
 33	}
 34
 35#define UNIPHIER_RESETX(_id, _reg, _bit)		\
 36	{						\
 37		.id = (_id),				\
 38		.reg = (_reg),				\
 39		.bit = (_bit),				\
 40		.flags = UNIPHIER_RESET_ACTIVE_LOW,	\
 41	}
 42
 43/* System reset data */
 44static const struct uniphier_reset_data uniphier_ld4_sys_reset_data[] = {
 45	UNIPHIER_RESETX(2, 0x2000, 2),		/* NAND */
 46	UNIPHIER_RESETX(8, 0x2000, 10),		/* STDMAC (Ether, HSC, MIO) */
 47	UNIPHIER_RESET_END,
 48};
 49
 50static const struct uniphier_reset_data uniphier_pro4_sys_reset_data[] = {
 51	UNIPHIER_RESETX(2, 0x2000, 2),		/* NAND */
 52	UNIPHIER_RESETX(6, 0x2000, 12),		/* Ether */
 53	UNIPHIER_RESETX(8, 0x2000, 10),		/* STDMAC (HSC, MIO, RLE) */
 54	UNIPHIER_RESETX(12, 0x2000, 6),		/* GIO (Ether, SATA, USB3) */
 55	UNIPHIER_RESETX(14, 0x2000, 17),	/* USB30 */
 56	UNIPHIER_RESETX(15, 0x2004, 17),	/* USB31 */
 57	UNIPHIER_RESETX(28, 0x2000, 18),	/* SATA0 */
 58	UNIPHIER_RESETX(29, 0x2004, 18),	/* SATA1 */
 59	UNIPHIER_RESETX(30, 0x2000, 19),	/* SATA-PHY */
 60	UNIPHIER_RESETX(40, 0x2000, 13),	/* AIO */
 61	UNIPHIER_RESET_END,
 62};
 63
 64static const struct uniphier_reset_data uniphier_pro5_sys_reset_data[] = {
 65	UNIPHIER_RESETX(2, 0x2000, 2),		/* NAND */
 66	UNIPHIER_RESETX(8, 0x2000, 10),		/* STDMAC (HSC) */
 67	UNIPHIER_RESETX(12, 0x2000, 6),		/* GIO (PCIe, USB3) */
 68	UNIPHIER_RESETX(14, 0x2000, 17),	/* USB30 */
 69	UNIPHIER_RESETX(15, 0x2004, 17),	/* USB31 */
 70	UNIPHIER_RESETX(24, 0x2008, 2),		/* PCIe */
 71	UNIPHIER_RESETX(40, 0x2000, 13),	/* AIO */
 72	UNIPHIER_RESET_END,
 73};
 74
 75static const struct uniphier_reset_data uniphier_pxs2_sys_reset_data[] = {
 76	UNIPHIER_RESETX(2, 0x2000, 2),		/* NAND */
 77	UNIPHIER_RESETX(6, 0x2000, 12),		/* Ether */
 78	UNIPHIER_RESETX(8, 0x2000, 10),		/* STDMAC (HSC, RLE) */
 79	UNIPHIER_RESETX(14, 0x2000, 17),	/* USB30 */
 80	UNIPHIER_RESETX(15, 0x2004, 17),	/* USB31 */
 81	UNIPHIER_RESETX(16, 0x2014, 4),		/* USB30-PHY0 */
 82	UNIPHIER_RESETX(17, 0x2014, 0),		/* USB30-PHY1 */
 83	UNIPHIER_RESETX(18, 0x2014, 2),		/* USB30-PHY2 */
 84	UNIPHIER_RESETX(20, 0x2014, 5),		/* USB31-PHY0 */
 85	UNIPHIER_RESETX(21, 0x2014, 1),		/* USB31-PHY1 */
 86	UNIPHIER_RESETX(28, 0x2014, 12),	/* SATA */
 87	UNIPHIER_RESET(30, 0x2014, 8),		/* SATA-PHY (active high) */
 88	UNIPHIER_RESETX(40, 0x2000, 13),	/* AIO */
 89	UNIPHIER_RESET_END,
 90};
 91
 92static const struct uniphier_reset_data uniphier_ld11_sys_reset_data[] = {
 93	UNIPHIER_RESETX(2, 0x200c, 0),		/* NAND */
 94	UNIPHIER_RESETX(4, 0x200c, 2),		/* eMMC */
 95	UNIPHIER_RESETX(6, 0x200c, 6),		/* Ether */
 96	UNIPHIER_RESETX(8, 0x200c, 8),		/* STDMAC (HSC, MIO) */
 97	UNIPHIER_RESETX(9, 0x200c, 9),		/* HSC */
 98	UNIPHIER_RESETX(40, 0x2008, 0),		/* AIO */
 99	UNIPHIER_RESETX(41, 0x2008, 1),		/* EVEA */
100	UNIPHIER_RESETX(42, 0x2010, 2),		/* EXIV */
101	UNIPHIER_RESET_END,
102};
103
104static const struct uniphier_reset_data uniphier_ld20_sys_reset_data[] = {
105	UNIPHIER_RESETX(2, 0x200c, 0),		/* NAND */
106	UNIPHIER_RESETX(4, 0x200c, 2),		/* eMMC */
107	UNIPHIER_RESETX(6, 0x200c, 6),		/* Ether */
108	UNIPHIER_RESETX(8, 0x200c, 8),		/* STDMAC (HSC) */
109	UNIPHIER_RESETX(9, 0x200c, 9),		/* HSC */
110	UNIPHIER_RESETX(14, 0x200c, 5),		/* USB30 */
111	UNIPHIER_RESETX(16, 0x200c, 12),	/* USB30-PHY0 */
112	UNIPHIER_RESETX(17, 0x200c, 13),	/* USB30-PHY1 */
113	UNIPHIER_RESETX(18, 0x200c, 14),	/* USB30-PHY2 */
114	UNIPHIER_RESETX(19, 0x200c, 15),	/* USB30-PHY3 */
115	UNIPHIER_RESETX(24, 0x200c, 4),		/* PCIe */
116	UNIPHIER_RESETX(40, 0x2008, 0),		/* AIO */
117	UNIPHIER_RESETX(41, 0x2008, 1),		/* EVEA */
118	UNIPHIER_RESETX(42, 0x2010, 2),		/* EXIV */
119	UNIPHIER_RESET_END,
120};
121
122static const struct uniphier_reset_data uniphier_pxs3_sys_reset_data[] = {
123	UNIPHIER_RESETX(2, 0x200c, 0),		/* NAND */
124	UNIPHIER_RESETX(4, 0x200c, 2),		/* eMMC */
125	UNIPHIER_RESETX(6, 0x200c, 9),		/* Ether0 */
126	UNIPHIER_RESETX(7, 0x200c, 10),		/* Ether1 */
127	UNIPHIER_RESETX(8, 0x200c, 12),		/* STDMAC */
128	UNIPHIER_RESETX(12, 0x200c, 4),		/* USB30 link */
129	UNIPHIER_RESETX(13, 0x200c, 5),		/* USB31 link */
130	UNIPHIER_RESETX(16, 0x200c, 16),	/* USB30-PHY0 */
131	UNIPHIER_RESETX(17, 0x200c, 18),	/* USB30-PHY1 */
132	UNIPHIER_RESETX(18, 0x200c, 20),	/* USB30-PHY2 */
133	UNIPHIER_RESETX(20, 0x200c, 17),	/* USB31-PHY0 */
134	UNIPHIER_RESETX(21, 0x200c, 19),	/* USB31-PHY1 */
135	UNIPHIER_RESETX(24, 0x200c, 3),		/* PCIe */
136	UNIPHIER_RESETX(28, 0x200c, 7),		/* SATA0 */
137	UNIPHIER_RESETX(29, 0x200c, 8),		/* SATA1 */
138	UNIPHIER_RESETX(30, 0x200c, 21),	/* SATA-PHY */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139	UNIPHIER_RESET_END,
140};
141
142/* Media I/O reset data */
143#define UNIPHIER_MIO_RESET_SD(id, ch)			\
144	UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 0)
145
146#define UNIPHIER_MIO_RESET_SD_BRIDGE(id, ch)		\
147	UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 26)
148
149#define UNIPHIER_MIO_RESET_EMMC_HW_RESET(id, ch)	\
150	UNIPHIER_RESETX((id), 0x80 + 0x200 * (ch), 0)
151
152#define UNIPHIER_MIO_RESET_USB2(id, ch)			\
153	UNIPHIER_RESETX((id), 0x114 + 0x200 * (ch), 0)
154
155#define UNIPHIER_MIO_RESET_USB2_BRIDGE(id, ch)		\
156	UNIPHIER_RESETX((id), 0x110 + 0x200 * (ch), 24)
157
158#define UNIPHIER_MIO_RESET_DMAC(id)			\
159	UNIPHIER_RESETX((id), 0x110, 17)
160
161static const struct uniphier_reset_data uniphier_ld4_mio_reset_data[] = {
162	UNIPHIER_MIO_RESET_SD(0, 0),
163	UNIPHIER_MIO_RESET_SD(1, 1),
164	UNIPHIER_MIO_RESET_SD(2, 2),
165	UNIPHIER_MIO_RESET_SD_BRIDGE(3, 0),
166	UNIPHIER_MIO_RESET_SD_BRIDGE(4, 1),
167	UNIPHIER_MIO_RESET_SD_BRIDGE(5, 2),
168	UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
169	UNIPHIER_MIO_RESET_DMAC(7),
170	UNIPHIER_MIO_RESET_USB2(8, 0),
171	UNIPHIER_MIO_RESET_USB2(9, 1),
172	UNIPHIER_MIO_RESET_USB2(10, 2),
173	UNIPHIER_MIO_RESET_USB2_BRIDGE(12, 0),
174	UNIPHIER_MIO_RESET_USB2_BRIDGE(13, 1),
175	UNIPHIER_MIO_RESET_USB2_BRIDGE(14, 2),
176	UNIPHIER_RESET_END,
177};
178
179static const struct uniphier_reset_data uniphier_pro5_sd_reset_data[] = {
180	UNIPHIER_MIO_RESET_SD(0, 0),
181	UNIPHIER_MIO_RESET_SD(1, 1),
182	UNIPHIER_MIO_RESET_EMMC_HW_RESET(6, 1),
183	UNIPHIER_RESET_END,
184};
185
186/* Peripheral reset data */
187#define UNIPHIER_PERI_RESET_UART(id, ch)		\
188	UNIPHIER_RESETX((id), 0x114, 19 + (ch))
189
190#define UNIPHIER_PERI_RESET_I2C(id, ch)			\
191	UNIPHIER_RESETX((id), 0x114, 5 + (ch))
192
193#define UNIPHIER_PERI_RESET_FI2C(id, ch)		\
194	UNIPHIER_RESETX((id), 0x114, 24 + (ch))
195
196#define UNIPHIER_PERI_RESET_SCSSI(id)			\
197	UNIPHIER_RESETX((id), 0x110, 17)
198
199#define UNIPHIER_PERI_RESET_MCSSI(id)			\
200	UNIPHIER_RESETX((id), 0x114, 14)
201
202static const struct uniphier_reset_data uniphier_ld4_peri_reset_data[] = {
203	UNIPHIER_PERI_RESET_UART(0, 0),
204	UNIPHIER_PERI_RESET_UART(1, 1),
205	UNIPHIER_PERI_RESET_UART(2, 2),
206	UNIPHIER_PERI_RESET_UART(3, 3),
207	UNIPHIER_PERI_RESET_I2C(4, 0),
208	UNIPHIER_PERI_RESET_I2C(5, 1),
209	UNIPHIER_PERI_RESET_I2C(6, 2),
210	UNIPHIER_PERI_RESET_I2C(7, 3),
211	UNIPHIER_PERI_RESET_I2C(8, 4),
212	UNIPHIER_PERI_RESET_SCSSI(11),
213	UNIPHIER_RESET_END,
214};
215
216static const struct uniphier_reset_data uniphier_pro4_peri_reset_data[] = {
217	UNIPHIER_PERI_RESET_UART(0, 0),
218	UNIPHIER_PERI_RESET_UART(1, 1),
219	UNIPHIER_PERI_RESET_UART(2, 2),
220	UNIPHIER_PERI_RESET_UART(3, 3),
221	UNIPHIER_PERI_RESET_FI2C(4, 0),
222	UNIPHIER_PERI_RESET_FI2C(5, 1),
223	UNIPHIER_PERI_RESET_FI2C(6, 2),
224	UNIPHIER_PERI_RESET_FI2C(7, 3),
225	UNIPHIER_PERI_RESET_FI2C(8, 4),
226	UNIPHIER_PERI_RESET_FI2C(9, 5),
227	UNIPHIER_PERI_RESET_FI2C(10, 6),
228	UNIPHIER_PERI_RESET_SCSSI(11),
229	UNIPHIER_PERI_RESET_MCSSI(12),
 
 
 
230	UNIPHIER_RESET_END,
231};
232
233/* Analog signal amplifiers reset data */
234static const struct uniphier_reset_data uniphier_ld11_adamv_reset_data[] = {
235	UNIPHIER_RESETX(0, 0x10, 6), /* EVEA */
236	UNIPHIER_RESET_END,
237};
238
239/* core implementaton */
240struct uniphier_reset_priv {
241	struct reset_controller_dev rcdev;
242	struct device *dev;
243	struct regmap *regmap;
244	const struct uniphier_reset_data *data;
245};
246
247#define to_uniphier_reset_priv(_rcdev) \
248			container_of(_rcdev, struct uniphier_reset_priv, rcdev)
249
250static int uniphier_reset_update(struct reset_controller_dev *rcdev,
251				 unsigned long id, int assert)
252{
253	struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
254	const struct uniphier_reset_data *p;
255
256	for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
257		unsigned int mask, val;
258
259		if (p->id != id)
260			continue;
261
262		mask = BIT(p->bit);
263
264		if (assert)
265			val = mask;
266		else
267			val = ~mask;
268
269		if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
270			val = ~val;
271
272		return regmap_write_bits(priv->regmap, p->reg, mask, val);
273	}
274
275	dev_err(priv->dev, "reset_id=%lu was not handled\n", id);
276	return -EINVAL;
277}
278
279static int uniphier_reset_assert(struct reset_controller_dev *rcdev,
280				 unsigned long id)
281{
282	return uniphier_reset_update(rcdev, id, 1);
283}
284
285static int uniphier_reset_deassert(struct reset_controller_dev *rcdev,
286				   unsigned long id)
287{
288	return uniphier_reset_update(rcdev, id, 0);
289}
290
291static int uniphier_reset_status(struct reset_controller_dev *rcdev,
292				 unsigned long id)
293{
294	struct uniphier_reset_priv *priv = to_uniphier_reset_priv(rcdev);
295	const struct uniphier_reset_data *p;
296
297	for (p = priv->data; p->id != UNIPHIER_RESET_ID_END; p++) {
298		unsigned int val;
299		int ret, asserted;
300
301		if (p->id != id)
302			continue;
303
304		ret = regmap_read(priv->regmap, p->reg, &val);
305		if (ret)
306			return ret;
307
308		asserted = !!(val & BIT(p->bit));
309
310		if (p->flags & UNIPHIER_RESET_ACTIVE_LOW)
311			asserted = !asserted;
312
313		return asserted;
314	}
315
316	dev_err(priv->dev, "reset_id=%lu was not found\n", id);
317	return -EINVAL;
318}
319
320static const struct reset_control_ops uniphier_reset_ops = {
321	.assert = uniphier_reset_assert,
322	.deassert = uniphier_reset_deassert,
323	.status = uniphier_reset_status,
324};
325
326static int uniphier_reset_probe(struct platform_device *pdev)
327{
328	struct device *dev = &pdev->dev;
329	struct uniphier_reset_priv *priv;
330	const struct uniphier_reset_data *p, *data;
331	struct regmap *regmap;
332	struct device_node *parent;
333	unsigned int nr_resets = 0;
334
335	data = of_device_get_match_data(dev);
336	if (WARN_ON(!data))
337		return -EINVAL;
338
339	parent = of_get_parent(dev->of_node); /* parent should be syscon node */
340	regmap = syscon_node_to_regmap(parent);
341	of_node_put(parent);
342	if (IS_ERR(regmap)) {
343		dev_err(dev, "failed to get regmap (error %ld)\n",
344			PTR_ERR(regmap));
345		return PTR_ERR(regmap);
346	}
347
348	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
349	if (!priv)
350		return -ENOMEM;
351
352	for (p = data; p->id != UNIPHIER_RESET_ID_END; p++)
353		nr_resets = max(nr_resets, p->id + 1);
354
355	priv->rcdev.ops = &uniphier_reset_ops;
356	priv->rcdev.owner = dev->driver->owner;
357	priv->rcdev.of_node = dev->of_node;
358	priv->rcdev.nr_resets = nr_resets;
359	priv->dev = dev;
360	priv->regmap = regmap;
361	priv->data = data;
362
363	return devm_reset_controller_register(&pdev->dev, &priv->rcdev);
364}
365
366static const struct of_device_id uniphier_reset_match[] = {
367	/* System reset */
368	{
369		.compatible = "socionext,uniphier-ld4-reset",
370		.data = uniphier_ld4_sys_reset_data,
371	},
372	{
373		.compatible = "socionext,uniphier-pro4-reset",
374		.data = uniphier_pro4_sys_reset_data,
375	},
376	{
377		.compatible = "socionext,uniphier-sld8-reset",
378		.data = uniphier_ld4_sys_reset_data,
379	},
380	{
381		.compatible = "socionext,uniphier-pro5-reset",
382		.data = uniphier_pro5_sys_reset_data,
383	},
384	{
385		.compatible = "socionext,uniphier-pxs2-reset",
386		.data = uniphier_pxs2_sys_reset_data,
387	},
388	{
389		.compatible = "socionext,uniphier-ld11-reset",
390		.data = uniphier_ld11_sys_reset_data,
391	},
392	{
393		.compatible = "socionext,uniphier-ld20-reset",
394		.data = uniphier_ld20_sys_reset_data,
395	},
396	{
397		.compatible = "socionext,uniphier-pxs3-reset",
398		.data = uniphier_pxs3_sys_reset_data,
399	},
 
 
 
 
400	/* Media I/O reset, SD reset */
401	{
402		.compatible = "socionext,uniphier-ld4-mio-reset",
403		.data = uniphier_ld4_mio_reset_data,
404	},
405	{
406		.compatible = "socionext,uniphier-pro4-mio-reset",
407		.data = uniphier_ld4_mio_reset_data,
408	},
409	{
410		.compatible = "socionext,uniphier-sld8-mio-reset",
411		.data = uniphier_ld4_mio_reset_data,
412	},
413	{
414		.compatible = "socionext,uniphier-pro5-sd-reset",
415		.data = uniphier_pro5_sd_reset_data,
416	},
417	{
418		.compatible = "socionext,uniphier-pxs2-sd-reset",
419		.data = uniphier_pro5_sd_reset_data,
420	},
421	{
422		.compatible = "socionext,uniphier-ld11-mio-reset",
423		.data = uniphier_ld4_mio_reset_data,
424	},
425	{
426		.compatible = "socionext,uniphier-ld11-sd-reset",
427		.data = uniphier_pro5_sd_reset_data,
428	},
429	{
430		.compatible = "socionext,uniphier-ld20-sd-reset",
431		.data = uniphier_pro5_sd_reset_data,
432	},
433	{
434		.compatible = "socionext,uniphier-pxs3-sd-reset",
435		.data = uniphier_pro5_sd_reset_data,
436	},
 
 
 
 
437	/* Peripheral reset */
438	{
439		.compatible = "socionext,uniphier-ld4-peri-reset",
440		.data = uniphier_ld4_peri_reset_data,
441	},
442	{
443		.compatible = "socionext,uniphier-pro4-peri-reset",
444		.data = uniphier_pro4_peri_reset_data,
445	},
446	{
447		.compatible = "socionext,uniphier-sld8-peri-reset",
448		.data = uniphier_ld4_peri_reset_data,
449	},
450	{
451		.compatible = "socionext,uniphier-pro5-peri-reset",
452		.data = uniphier_pro4_peri_reset_data,
453	},
454	{
455		.compatible = "socionext,uniphier-pxs2-peri-reset",
456		.data = uniphier_pro4_peri_reset_data,
457	},
458	{
459		.compatible = "socionext,uniphier-ld11-peri-reset",
460		.data = uniphier_pro4_peri_reset_data,
461	},
462	{
463		.compatible = "socionext,uniphier-ld20-peri-reset",
464		.data = uniphier_pro4_peri_reset_data,
465	},
466	{
467		.compatible = "socionext,uniphier-pxs3-peri-reset",
 
 
 
 
468		.data = uniphier_pro4_peri_reset_data,
469	},
470	/* Analog signal amplifiers reset */
471	{
472		.compatible = "socionext,uniphier-ld11-adamv-reset",
473		.data = uniphier_ld11_adamv_reset_data,
474	},
475	{
476		.compatible = "socionext,uniphier-ld20-adamv-reset",
477		.data = uniphier_ld11_adamv_reset_data,
478	},
479	{ /* sentinel */ }
480};
481MODULE_DEVICE_TABLE(of, uniphier_reset_match);
482
483static struct platform_driver uniphier_reset_driver = {
484	.probe = uniphier_reset_probe,
485	.driver = {
486		.name = "uniphier-reset",
487		.of_match_table = uniphier_reset_match,
488	},
489};
490module_platform_driver(uniphier_reset_driver);
491
492MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
493MODULE_DESCRIPTION("UniPhier Reset Controller Driver");
494MODULE_LICENSE("GPL");