1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
4  *
5  * Derived from linux/arch/mips/bcm63xx/reset.c:
6  *	Copyright (C) 2012 Jonas Gorski <jonas.gorski@gmail.com>
7  */
8 
9 #include <common.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <reset-uclass.h>
13 #include <asm/io.h>
14 
15 #define MAX_RESETS	32
16 
17 struct bcm6345_reset_priv {
18 	void __iomem *regs;
19 };
20 
21 static int bcm6345_reset_assert(struct reset_ctl *rst)
22 {
23 	struct bcm6345_reset_priv *priv = dev_get_priv(rst->dev);
24 
25 	clrbits_be32(priv->regs, BIT(rst->id));
26 	mdelay(20);
27 
28 	return 0;
29 }
30 
31 static int bcm6345_reset_deassert(struct reset_ctl *rst)
32 {
33 	struct bcm6345_reset_priv *priv = dev_get_priv(rst->dev);
34 
35 	setbits_be32(priv->regs, BIT(rst->id));
36 	mdelay(20);
37 
38 	return 0;
39 }
40 
41 static int bcm6345_reset_free(struct reset_ctl *rst)
42 {
43 	return 0;
44 }
45 
46 static int bcm6345_reset_request(struct reset_ctl *rst)
47 {
48 	if (rst->id >= MAX_RESETS)
49 		return -EINVAL;
50 
51 	return bcm6345_reset_assert(rst);
52 }
53 
54 struct reset_ops bcm6345_reset_reset_ops = {
55 	.free = bcm6345_reset_free,
56 	.request = bcm6345_reset_request,
57 	.rst_assert = bcm6345_reset_assert,
58 	.rst_deassert = bcm6345_reset_deassert,
59 };
60 
61 static const struct udevice_id bcm6345_reset_ids[] = {
62 	{ .compatible = "brcm,bcm6345-reset" },
63 	{ /* sentinel */ }
64 };
65 
66 static int bcm6345_reset_probe(struct udevice *dev)
67 {
68 	struct bcm6345_reset_priv *priv = dev_get_priv(dev);
69 	fdt_addr_t addr;
70 	fdt_size_t size;
71 
72 	addr = devfdt_get_addr_size_index(dev, 0, &size);
73 	if (addr == FDT_ADDR_T_NONE)
74 		return -EINVAL;
75 
76 	priv->regs = ioremap(addr, size);
77 
78 	return 0;
79 }
80 
81 U_BOOT_DRIVER(bcm6345_reset) = {
82 	.name = "bcm6345-reset",
83 	.id = UCLASS_RESET,
84 	.of_match = bcm6345_reset_ids,
85 	.ops = &bcm6345_reset_reset_ops,
86 	.probe = bcm6345_reset_probe,
87 	.priv_auto_alloc_size = sizeof(struct bcm6345_reset_priv),
88 };
89