1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2017 Google, Inc
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <misc.h>
9 #include <reset.h>
10 #include <reset-uclass.h>
11 #include <wdt.h>
12 #include <asm/io.h>
13 #include <asm/arch/wdt.h>
14 #include <asm/arch/scu_ast2500.h>
15 
16 struct ast2500_reset_priv {
17 	/* WDT used to perform resets. */
18 	struct udevice *wdt;
19 	struct ast2500_scu *scu;
20 };
21 
22 static int ast2500_reset_assert(struct reset_ctl *reset_ctl)
23 {
24 	struct ast2500_reset_priv *priv = dev_get_priv(reset_ctl->dev);
25 	struct ast2500_scu *scu = priv->scu;
26 //	u32 reset_mode, reset_mask;
27 	bool reset_sdram;
28 	int ret = 0;
29 
30 	printf("ast2500_reset_assert reset_ctl->id %d \n", reset_ctl->id);
31 	/*
32 	 * To reset SDRAM, a specifal flag in SYSRESET register
33 	 * needs to be enabled first
34 	 */
35 #if 0
36 	reset_mode = ast_reset_mode_from_flags(reset_ctl->id);
37 	reset_mask = ast_reset_mask_from_flags(reset_ctl->id);
38 	reset_sdram = reset_mode == WDT_CTRL_RESET_SOC &&
39 		(reset_mask & WDT_RESET_SDRAM);
40 
41 	if (reset_sdram) {
42 		ast_scu_unlock(priv->scu);
43 		setbits_le32(&priv->scu->sysreset_ctrl1,
44 			     SCU_SYSRESET_SDRAM_WDT);
45 		ret = wdt_expire_now(priv->wdt, reset_ctl->id);
46 		clrbits_le32(&priv->scu->sysreset_ctrl1,
47 			     SCU_SYSRESET_SDRAM_WDT);
48 		ast_scu_lock(priv->scu);
49 	} else {
50 		ret = wdt_expire_now(priv->wdt, reset_ctl->id);
51 	}
52 #endif
53 	if(reset_ctl->id >= 32)
54 		setbits_le32(&scu->sysreset_ctrl2 , BIT(reset_ctl->id - 32));
55 	else
56 		setbits_le32(&scu->sysreset_ctrl1 , BIT(reset_ctl->id));
57 
58 	printf("end \n");
59 	return ret;
60 }
61 
62 static int ast2500_reset_deassert(struct reset_ctl *reset_ctl)
63 {
64 	struct ast2500_reset_priv *priv = dev_get_priv(reset_ctl->dev);
65 	struct ast2500_scu *scu = priv->scu;
66 	u32 reset_mode, reset_mask;
67 	bool reset_sdram;
68 	int ret = 0;
69 
70 	printf("ast2500_reset_deassert reset_ctl->id %d \n", reset_ctl->id);
71 
72 	if(reset_ctl->id >= 32)
73 		clrbits_le32(&scu->sysreset_ctrl2 , BIT(reset_ctl->id - 32));
74 	else
75 		clrbits_le32(&scu->sysreset_ctrl1 , BIT(reset_ctl->id));
76 
77 	return ret;
78 }
79 
80 static int ast2500_reset_request(struct reset_ctl *reset_ctl)
81 {
82 	debug("%s(reset_ctl=%p) (dev=%p, id=%lu)\n", __func__, reset_ctl,
83 	      reset_ctl->dev, reset_ctl->id);
84 
85 	return 0;
86 }
87 
88 static int ast2500_reset_probe(struct udevice *dev)
89 {
90 	struct ast2500_reset_priv *priv = dev_get_priv(dev);
91 	struct udevice *clk_dev;
92 	int ret = 0;
93 
94 	/* find SCU base address from clock device */
95 	ret = uclass_get_device_by_driver(UCLASS_CLK, DM_GET_DRIVER(aspeed_scu),
96                                           &clk_dev);
97     if (ret) {
98             debug("clock device not found\n");
99             return ret;
100     }
101 
102 	priv->scu = devfdt_get_addr_ptr(clk_dev);
103 	if (IS_ERR(priv->scu)) {
104 	        debug("%s(): can't get SCU\n", __func__);
105 	        return PTR_ERR(priv->scu);
106 	}
107 
108 	return 0;
109 }
110 
111 static int ast2500_ofdata_to_platdata(struct udevice *dev)
112 {
113 	struct ast2500_reset_priv *priv = dev_get_priv(dev);
114 	int ret;
115 
116 	ret = uclass_get_device_by_phandle(UCLASS_WDT, dev, "aspeed,wdt",
117 					   &priv->wdt);
118 	if (ret) {
119 		debug("%s: can't find WDT for reset controller", __func__);
120 		return ret;
121 	}
122 
123 	return 0;
124 }
125 
126 static const struct udevice_id aspeed_reset_ids[] = {
127 	{ .compatible = "aspeed,ast2500-reset" },
128 	{ }
129 };
130 
131 struct reset_ops aspeed_reset_ops = {
132 	.rst_assert = ast2500_reset_assert,
133 	.rst_deassert = ast2500_reset_deassert,
134 	.request = ast2500_reset_request,
135 };
136 
137 U_BOOT_DRIVER(aspeed_reset) = {
138 	.name		= "aspeed_reset",
139 	.id		= UCLASS_RESET,
140 	.of_match = aspeed_reset_ids,
141 	.probe = ast2500_reset_probe,
142 	.ops = &aspeed_reset_ops,
143 	.ofdata_to_platdata = ast2500_ofdata_to_platdata,
144 	.priv_auto_alloc_size = sizeof(struct ast2500_reset_priv),
145 };
146