1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Nuvoton Technology corporation.
4 #include <linux/delay.h>
7 #include <linux/init.h>
9 #include <linux/of_device.h>
10 #include <linux/platform_device.h>
11 #include <linux/reboot.h>
12 #include <linux/reset-controller.h>
13 #include <linux/spinlock.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/regmap.h>
16 #include <linux/of_address.h>
18 /* NPCM7xx GCR registers */
19 #define NPCM_MDLR_OFFSET 0x7C
20 #define NPCM_MDLR_USBD0 BIT(9)
21 #define NPCM_MDLR_USBD1 BIT(8)
22 #define NPCM_MDLR_USBD2_4 BIT(21)
23 #define NPCM_MDLR_USBD5_9 BIT(22)
25 #define NPCM_USB1PHYCTL_OFFSET 0x140
26 #define NPCM_USB2PHYCTL_OFFSET 0x144
27 #define NPCM_USBXPHYCTL_RS BIT(28)
29 /* NPCM7xx Reset registers */
30 #define NPCM_SWRSTR 0x14
31 #define NPCM_SWRST BIT(2)
33 #define NPCM_IPSRST1 0x20
34 #define NPCM_IPSRST1_USBD1 BIT(5)
35 #define NPCM_IPSRST1_USBD2 BIT(8)
36 #define NPCM_IPSRST1_USBD3 BIT(25)
37 #define NPCM_IPSRST1_USBD4 BIT(22)
38 #define NPCM_IPSRST1_USBD5 BIT(23)
39 #define NPCM_IPSRST1_USBD6 BIT(24)
41 #define NPCM_IPSRST2 0x24
42 #define NPCM_IPSRST2_USB_HOST BIT(26)
44 #define NPCM_IPSRST3 0x34
45 #define NPCM_IPSRST3_USBD0 BIT(4)
46 #define NPCM_IPSRST3_USBD7 BIT(5)
47 #define NPCM_IPSRST3_USBD8 BIT(6)
48 #define NPCM_IPSRST3_USBD9 BIT(7)
49 #define NPCM_IPSRST3_USBPHY1 BIT(24)
50 #define NPCM_IPSRST3_USBPHY2 BIT(25)
52 #define NPCM_RC_RESETS_PER_REG 32
53 #define NPCM_MASK_RESETS GENMASK(4, 0)
56 struct reset_controller_dev rcdev
;
57 struct notifier_block restart_nb
;
63 #define to_rc_data(p) container_of(p, struct npcm_rc_data, rcdev)
65 static int npcm_rc_restart(struct notifier_block
*nb
, unsigned long mode
,
68 struct npcm_rc_data
*rc
= container_of(nb
, struct npcm_rc_data
,
71 writel(NPCM_SWRST
<< rc
->sw_reset_number
, rc
->base
+ NPCM_SWRSTR
);
74 pr_emerg("%s: unable to restart system\n", __func__
);
79 static int npcm_rc_setclear_reset(struct reset_controller_dev
*rcdev
,
80 unsigned long id
, bool set
)
82 struct npcm_rc_data
*rc
= to_rc_data(rcdev
);
83 unsigned int rst_bit
= BIT(id
& NPCM_MASK_RESETS
);
84 unsigned int ctrl_offset
= id
>> 8;
88 spin_lock_irqsave(&rc
->lock
, flags
);
89 stat
= readl(rc
->base
+ ctrl_offset
);
91 writel(stat
| rst_bit
, rc
->base
+ ctrl_offset
);
93 writel(stat
& ~rst_bit
, rc
->base
+ ctrl_offset
);
94 spin_unlock_irqrestore(&rc
->lock
, flags
);
99 static int npcm_rc_assert(struct reset_controller_dev
*rcdev
, unsigned long id
)
101 return npcm_rc_setclear_reset(rcdev
, id
, true);
104 static int npcm_rc_deassert(struct reset_controller_dev
*rcdev
,
107 return npcm_rc_setclear_reset(rcdev
, id
, false);
110 static int npcm_rc_status(struct reset_controller_dev
*rcdev
,
113 struct npcm_rc_data
*rc
= to_rc_data(rcdev
);
114 unsigned int rst_bit
= BIT(id
& NPCM_MASK_RESETS
);
115 unsigned int ctrl_offset
= id
>> 8;
117 return (readl(rc
->base
+ ctrl_offset
) & rst_bit
);
120 static int npcm_reset_xlate(struct reset_controller_dev
*rcdev
,
121 const struct of_phandle_args
*reset_spec
)
123 unsigned int offset
, bit
;
125 offset
= reset_spec
->args
[0];
126 if (offset
!= NPCM_IPSRST1
&& offset
!= NPCM_IPSRST2
&&
127 offset
!= NPCM_IPSRST3
) {
128 dev_err(rcdev
->dev
, "Error reset register (0x%x)\n", offset
);
131 bit
= reset_spec
->args
[1];
132 if (bit
>= NPCM_RC_RESETS_PER_REG
) {
133 dev_err(rcdev
->dev
, "Error reset number (%d)\n", bit
);
137 return (offset
<< 8) | bit
;
140 static const struct of_device_id npcm_rc_match
[] = {
141 { .compatible
= "nuvoton,npcm750-reset",
142 .data
= (void *)"nuvoton,npcm750-gcr" },
147 * The following procedure should be observed in USB PHY, USB device and
148 * USB host initialization at BMC boot
150 static int npcm_usb_reset(struct platform_device
*pdev
, struct npcm_rc_data
*rc
)
152 u32 mdlr
, iprst1
, iprst2
, iprst3
;
153 struct device
*dev
= &pdev
->dev
;
154 struct regmap
*gcr_regmap
;
155 u32 ipsrst1_bits
= 0;
156 u32 ipsrst2_bits
= NPCM_IPSRST2_USB_HOST
;
157 u32 ipsrst3_bits
= 0;
160 gcr_dt
= (const char *)
161 of_match_device(dev
->driver
->of_match_table
, dev
)->data
;
163 gcr_regmap
= syscon_regmap_lookup_by_compatible(gcr_dt
);
164 if (IS_ERR(gcr_regmap
)) {
165 dev_err(&pdev
->dev
, "Failed to find %s\n", gcr_dt
);
166 return PTR_ERR(gcr_regmap
);
169 /* checking which USB device is enabled */
170 regmap_read(gcr_regmap
, NPCM_MDLR_OFFSET
, &mdlr
);
171 if (!(mdlr
& NPCM_MDLR_USBD0
))
172 ipsrst3_bits
|= NPCM_IPSRST3_USBD0
;
173 if (!(mdlr
& NPCM_MDLR_USBD1
))
174 ipsrst1_bits
|= NPCM_IPSRST1_USBD1
;
175 if (!(mdlr
& NPCM_MDLR_USBD2_4
))
176 ipsrst1_bits
|= (NPCM_IPSRST1_USBD2
|
179 if (!(mdlr
& NPCM_MDLR_USBD0
)) {
180 ipsrst1_bits
|= (NPCM_IPSRST1_USBD5
|
182 ipsrst3_bits
|= (NPCM_IPSRST3_USBD7
|
187 /* assert reset USB PHY and USB devices */
188 iprst1
= readl(rc
->base
+ NPCM_IPSRST1
);
189 iprst2
= readl(rc
->base
+ NPCM_IPSRST2
);
190 iprst3
= readl(rc
->base
+ NPCM_IPSRST3
);
192 iprst1
|= ipsrst1_bits
;
193 iprst2
|= ipsrst2_bits
;
194 iprst3
|= (ipsrst3_bits
| NPCM_IPSRST3_USBPHY1
|
195 NPCM_IPSRST3_USBPHY2
);
197 writel(iprst1
, rc
->base
+ NPCM_IPSRST1
);
198 writel(iprst2
, rc
->base
+ NPCM_IPSRST2
);
199 writel(iprst3
, rc
->base
+ NPCM_IPSRST3
);
201 /* clear USB PHY RS bit */
202 regmap_update_bits(gcr_regmap
, NPCM_USB1PHYCTL_OFFSET
,
203 NPCM_USBXPHYCTL_RS
, 0);
204 regmap_update_bits(gcr_regmap
, NPCM_USB2PHYCTL_OFFSET
,
205 NPCM_USBXPHYCTL_RS
, 0);
207 /* deassert reset USB PHY */
208 iprst3
&= ~(NPCM_IPSRST3_USBPHY1
| NPCM_IPSRST3_USBPHY2
);
209 writel(iprst3
, rc
->base
+ NPCM_IPSRST3
);
213 /* set USB PHY RS bit */
214 regmap_update_bits(gcr_regmap
, NPCM_USB1PHYCTL_OFFSET
,
215 NPCM_USBXPHYCTL_RS
, NPCM_USBXPHYCTL_RS
);
216 regmap_update_bits(gcr_regmap
, NPCM_USB2PHYCTL_OFFSET
,
217 NPCM_USBXPHYCTL_RS
, NPCM_USBXPHYCTL_RS
);
219 /* deassert reset USB devices*/
220 iprst1
&= ~ipsrst1_bits
;
221 iprst2
&= ~ipsrst2_bits
;
222 iprst3
&= ~ipsrst3_bits
;
224 writel(iprst1
, rc
->base
+ NPCM_IPSRST1
);
225 writel(iprst2
, rc
->base
+ NPCM_IPSRST2
);
226 writel(iprst3
, rc
->base
+ NPCM_IPSRST3
);
231 static const struct reset_control_ops npcm_rc_ops
= {
232 .assert = npcm_rc_assert
,
233 .deassert
= npcm_rc_deassert
,
234 .status
= npcm_rc_status
,
237 static int npcm_rc_probe(struct platform_device
*pdev
)
239 struct npcm_rc_data
*rc
;
242 rc
= devm_kzalloc(&pdev
->dev
, sizeof(*rc
), GFP_KERNEL
);
246 rc
->base
= devm_platform_ioremap_resource(pdev
, 0);
247 if (IS_ERR(rc
->base
))
248 return PTR_ERR(rc
->base
);
250 spin_lock_init(&rc
->lock
);
252 rc
->rcdev
.owner
= THIS_MODULE
;
253 rc
->rcdev
.ops
= &npcm_rc_ops
;
254 rc
->rcdev
.of_node
= pdev
->dev
.of_node
;
255 rc
->rcdev
.of_reset_n_cells
= 2;
256 rc
->rcdev
.of_xlate
= npcm_reset_xlate
;
258 platform_set_drvdata(pdev
, rc
);
260 ret
= devm_reset_controller_register(&pdev
->dev
, &rc
->rcdev
);
262 dev_err(&pdev
->dev
, "unable to register device\n");
266 if (npcm_usb_reset(pdev
, rc
))
267 dev_warn(&pdev
->dev
, "NPCM USB reset failed, can cause issues with UDC and USB host\n");
269 if (!of_property_read_u32(pdev
->dev
.of_node
, "nuvoton,sw-reset-number",
270 &rc
->sw_reset_number
)) {
271 if (rc
->sw_reset_number
&& rc
->sw_reset_number
< 5) {
272 rc
->restart_nb
.priority
= 192,
273 rc
->restart_nb
.notifier_call
= npcm_rc_restart
,
274 ret
= register_restart_handler(&rc
->restart_nb
);
276 dev_warn(&pdev
->dev
, "failed to register restart handler\n");
283 static struct platform_driver npcm_rc_driver
= {
284 .probe
= npcm_rc_probe
,
286 .name
= "npcm-reset",
287 .of_match_table
= npcm_rc_match
,
288 .suppress_bind_attrs
= true,
291 builtin_platform_driver(npcm_rc_driver
);