xref: /openbmc/linux/drivers/reset/reset-npcm.c (revision cd3c50d842d3104f9d78af16aaec11fa6c87237e)
19c81b2ccSTomer Maimon // SPDX-License-Identifier: GPL-2.0
29c81b2ccSTomer Maimon // Copyright (c) 2019 Nuvoton Technology corporation.
39c81b2ccSTomer Maimon 
4*cd3c50d8STomer Maimon #include <linux/auxiliary_bus.h>
59c81b2ccSTomer Maimon #include <linux/delay.h>
69c81b2ccSTomer Maimon #include <linux/err.h>
79c81b2ccSTomer Maimon #include <linux/io.h>
89c81b2ccSTomer Maimon #include <linux/init.h>
99c81b2ccSTomer Maimon #include <linux/of.h>
109c81b2ccSTomer Maimon #include <linux/of_device.h>
119c81b2ccSTomer Maimon #include <linux/platform_device.h>
129c81b2ccSTomer Maimon #include <linux/reboot.h>
139c81b2ccSTomer Maimon #include <linux/reset-controller.h>
14*cd3c50d8STomer Maimon #include <linux/slab.h>
159c81b2ccSTomer Maimon #include <linux/spinlock.h>
169c81b2ccSTomer Maimon #include <linux/mfd/syscon.h>
179c81b2ccSTomer Maimon #include <linux/regmap.h>
189c81b2ccSTomer Maimon #include <linux/of_address.h>
199c81b2ccSTomer Maimon 
20*cd3c50d8STomer Maimon #include <soc/nuvoton/clock-npcm8xx.h>
21*cd3c50d8STomer Maimon 
229c81b2ccSTomer Maimon /* NPCM7xx GCR registers */
239c81b2ccSTomer Maimon #define NPCM_MDLR_OFFSET	0x7C
24fc5d2a2fSTomer Maimon #define NPCM7XX_MDLR_USBD0	BIT(9)
25fc5d2a2fSTomer Maimon #define NPCM7XX_MDLR_USBD1	BIT(8)
26fc5d2a2fSTomer Maimon #define NPCM7XX_MDLR_USBD2_4	BIT(21)
27fc5d2a2fSTomer Maimon #define NPCM7XX_MDLR_USBD5_9	BIT(22)
28fc5d2a2fSTomer Maimon 
29fc5d2a2fSTomer Maimon /* NPCM8xx MDLR bits */
30fc5d2a2fSTomer Maimon #define NPCM8XX_MDLR_USBD0_3	BIT(9)
31fc5d2a2fSTomer Maimon #define NPCM8XX_MDLR_USBD4_7	BIT(22)
32fc5d2a2fSTomer Maimon #define NPCM8XX_MDLR_USBD8	BIT(24)
33fc5d2a2fSTomer Maimon #define NPCM8XX_MDLR_USBD9	BIT(21)
349c81b2ccSTomer Maimon 
359c81b2ccSTomer Maimon #define NPCM_USB1PHYCTL_OFFSET	0x140
369c81b2ccSTomer Maimon #define NPCM_USB2PHYCTL_OFFSET	0x144
37fc5d2a2fSTomer Maimon #define NPCM_USB3PHYCTL_OFFSET	0x148
389c81b2ccSTomer Maimon #define NPCM_USBXPHYCTL_RS	BIT(28)
399c81b2ccSTomer Maimon 
409c81b2ccSTomer Maimon /* NPCM7xx Reset registers */
419c81b2ccSTomer Maimon #define NPCM_SWRSTR		0x14
429c81b2ccSTomer Maimon #define NPCM_SWRST		BIT(2)
439c81b2ccSTomer Maimon 
449c81b2ccSTomer Maimon #define NPCM_IPSRST1		0x20
459c81b2ccSTomer Maimon #define NPCM_IPSRST1_USBD1	BIT(5)
469c81b2ccSTomer Maimon #define NPCM_IPSRST1_USBD2	BIT(8)
479c81b2ccSTomer Maimon #define NPCM_IPSRST1_USBD3	BIT(25)
489c81b2ccSTomer Maimon #define NPCM_IPSRST1_USBD4	BIT(22)
499c81b2ccSTomer Maimon #define NPCM_IPSRST1_USBD5	BIT(23)
509c81b2ccSTomer Maimon #define NPCM_IPSRST1_USBD6	BIT(24)
519c81b2ccSTomer Maimon 
529c81b2ccSTomer Maimon #define NPCM_IPSRST2		0x24
539c81b2ccSTomer Maimon #define NPCM_IPSRST2_USB_HOST	BIT(26)
549c81b2ccSTomer Maimon 
559c81b2ccSTomer Maimon #define NPCM_IPSRST3		0x34
569c81b2ccSTomer Maimon #define NPCM_IPSRST3_USBD0	BIT(4)
579c81b2ccSTomer Maimon #define NPCM_IPSRST3_USBD7	BIT(5)
589c81b2ccSTomer Maimon #define NPCM_IPSRST3_USBD8	BIT(6)
599c81b2ccSTomer Maimon #define NPCM_IPSRST3_USBD9	BIT(7)
609c81b2ccSTomer Maimon #define NPCM_IPSRST3_USBPHY1	BIT(24)
619c81b2ccSTomer Maimon #define NPCM_IPSRST3_USBPHY2	BIT(25)
629c81b2ccSTomer Maimon 
63fc5d2a2fSTomer Maimon #define NPCM_IPSRST4		0x74
64fc5d2a2fSTomer Maimon #define NPCM_IPSRST4_USBPHY3	BIT(25)
65fc5d2a2fSTomer Maimon #define NPCM_IPSRST4_USB_HOST2	BIT(31)
66fc5d2a2fSTomer Maimon 
679c81b2ccSTomer Maimon #define NPCM_RC_RESETS_PER_REG	32
689c81b2ccSTomer Maimon #define NPCM_MASK_RESETS	GENMASK(4, 0)
699c81b2ccSTomer Maimon 
70fc5d2a2fSTomer Maimon enum {
71fc5d2a2fSTomer Maimon 	BMC_NPCM7XX = 0,
72fc5d2a2fSTomer Maimon 	BMC_NPCM8XX,
73fc5d2a2fSTomer Maimon };
74fc5d2a2fSTomer Maimon 
75fc5d2a2fSTomer Maimon static const u32 npxm7xx_ipsrst[] = {NPCM_IPSRST1, NPCM_IPSRST2, NPCM_IPSRST3};
76fc5d2a2fSTomer Maimon static const u32 npxm8xx_ipsrst[] = {NPCM_IPSRST1, NPCM_IPSRST2, NPCM_IPSRST3,
77fc5d2a2fSTomer Maimon 	NPCM_IPSRST4};
78fc5d2a2fSTomer Maimon 
79fc5d2a2fSTomer Maimon struct npcm_reset_info {
80fc5d2a2fSTomer Maimon 	u32 bmc_id;
81fc5d2a2fSTomer Maimon 	u32 num_ipsrst;
82fc5d2a2fSTomer Maimon 	const u32 *ipsrst;
83fc5d2a2fSTomer Maimon };
84fc5d2a2fSTomer Maimon 
85fc5d2a2fSTomer Maimon static const struct npcm_reset_info npxm7xx_reset_info[] = {
86fc5d2a2fSTomer Maimon 	{.bmc_id = BMC_NPCM7XX, .num_ipsrst = 3, .ipsrst = npxm7xx_ipsrst}};
87fc5d2a2fSTomer Maimon static const struct npcm_reset_info npxm8xx_reset_info[] = {
88fc5d2a2fSTomer Maimon 	{.bmc_id = BMC_NPCM8XX, .num_ipsrst = 4, .ipsrst = npxm8xx_ipsrst}};
89fc5d2a2fSTomer Maimon 
909c81b2ccSTomer Maimon struct npcm_rc_data {
919c81b2ccSTomer Maimon 	struct reset_controller_dev rcdev;
929c81b2ccSTomer Maimon 	struct notifier_block restart_nb;
93fc5d2a2fSTomer Maimon 	const struct npcm_reset_info *info;
94fc5d2a2fSTomer Maimon 	struct regmap *gcr_regmap;
959c81b2ccSTomer Maimon 	u32 sw_reset_number;
96*cd3c50d8STomer Maimon 	struct device *dev;
979c81b2ccSTomer Maimon 	void __iomem *base;
989c81b2ccSTomer Maimon 	spinlock_t lock;
999c81b2ccSTomer Maimon };
1009c81b2ccSTomer Maimon 
1019c81b2ccSTomer Maimon #define to_rc_data(p) container_of(p, struct npcm_rc_data, rcdev)
1029c81b2ccSTomer Maimon 
npcm_rc_restart(struct notifier_block * nb,unsigned long mode,void * cmd)1039c81b2ccSTomer Maimon static int npcm_rc_restart(struct notifier_block *nb, unsigned long mode,
1049c81b2ccSTomer Maimon 			   void *cmd)
1059c81b2ccSTomer Maimon {
1069c81b2ccSTomer Maimon 	struct npcm_rc_data *rc = container_of(nb, struct npcm_rc_data,
1079c81b2ccSTomer Maimon 					       restart_nb);
1089c81b2ccSTomer Maimon 
1099c81b2ccSTomer Maimon 	writel(NPCM_SWRST << rc->sw_reset_number, rc->base + NPCM_SWRSTR);
1109c81b2ccSTomer Maimon 	mdelay(1000);
1119c81b2ccSTomer Maimon 
1129c81b2ccSTomer Maimon 	pr_emerg("%s: unable to restart system\n", __func__);
1139c81b2ccSTomer Maimon 
1149c81b2ccSTomer Maimon 	return NOTIFY_DONE;
1159c81b2ccSTomer Maimon }
1169c81b2ccSTomer Maimon 
npcm_rc_setclear_reset(struct reset_controller_dev * rcdev,unsigned long id,bool set)1179c81b2ccSTomer Maimon static int npcm_rc_setclear_reset(struct reset_controller_dev *rcdev,
1189c81b2ccSTomer Maimon 				  unsigned long id, bool set)
1199c81b2ccSTomer Maimon {
1209c81b2ccSTomer Maimon 	struct npcm_rc_data *rc = to_rc_data(rcdev);
1219c81b2ccSTomer Maimon 	unsigned int rst_bit = BIT(id & NPCM_MASK_RESETS);
1229c81b2ccSTomer Maimon 	unsigned int ctrl_offset = id >> 8;
1239c81b2ccSTomer Maimon 	unsigned long flags;
1249c81b2ccSTomer Maimon 	u32 stat;
1259c81b2ccSTomer Maimon 
1269c81b2ccSTomer Maimon 	spin_lock_irqsave(&rc->lock, flags);
1279c81b2ccSTomer Maimon 	stat = readl(rc->base + ctrl_offset);
1289c81b2ccSTomer Maimon 	if (set)
1299c81b2ccSTomer Maimon 		writel(stat | rst_bit, rc->base + ctrl_offset);
1309c81b2ccSTomer Maimon 	else
1319c81b2ccSTomer Maimon 		writel(stat & ~rst_bit, rc->base + ctrl_offset);
1329c81b2ccSTomer Maimon 	spin_unlock_irqrestore(&rc->lock, flags);
1339c81b2ccSTomer Maimon 
1349c81b2ccSTomer Maimon 	return 0;
1359c81b2ccSTomer Maimon }
1369c81b2ccSTomer Maimon 
npcm_rc_assert(struct reset_controller_dev * rcdev,unsigned long id)1379c81b2ccSTomer Maimon static int npcm_rc_assert(struct reset_controller_dev *rcdev, unsigned long id)
1389c81b2ccSTomer Maimon {
1399c81b2ccSTomer Maimon 	return npcm_rc_setclear_reset(rcdev, id, true);
1409c81b2ccSTomer Maimon }
1419c81b2ccSTomer Maimon 
npcm_rc_deassert(struct reset_controller_dev * rcdev,unsigned long id)1429c81b2ccSTomer Maimon static int npcm_rc_deassert(struct reset_controller_dev *rcdev,
1439c81b2ccSTomer Maimon 			    unsigned long id)
1449c81b2ccSTomer Maimon {
1459c81b2ccSTomer Maimon 	return npcm_rc_setclear_reset(rcdev, id, false);
1469c81b2ccSTomer Maimon }
1479c81b2ccSTomer Maimon 
npcm_rc_status(struct reset_controller_dev * rcdev,unsigned long id)1489c81b2ccSTomer Maimon static int npcm_rc_status(struct reset_controller_dev *rcdev,
1499c81b2ccSTomer Maimon 			  unsigned long id)
1509c81b2ccSTomer Maimon {
1519c81b2ccSTomer Maimon 	struct npcm_rc_data *rc = to_rc_data(rcdev);
1529c81b2ccSTomer Maimon 	unsigned int rst_bit = BIT(id & NPCM_MASK_RESETS);
1539c81b2ccSTomer Maimon 	unsigned int ctrl_offset = id >> 8;
1549c81b2ccSTomer Maimon 
1559c81b2ccSTomer Maimon 	return (readl(rc->base + ctrl_offset) & rst_bit);
1569c81b2ccSTomer Maimon }
1579c81b2ccSTomer Maimon 
npcm_reset_xlate(struct reset_controller_dev * rcdev,const struct of_phandle_args * reset_spec)1589c81b2ccSTomer Maimon static int npcm_reset_xlate(struct reset_controller_dev *rcdev,
1599c81b2ccSTomer Maimon 			    const struct of_phandle_args *reset_spec)
1609c81b2ccSTomer Maimon {
161fc5d2a2fSTomer Maimon 	struct npcm_rc_data *rc = to_rc_data(rcdev);
1629c81b2ccSTomer Maimon 	unsigned int offset, bit;
163fc5d2a2fSTomer Maimon 	bool offset_found = false;
164fc5d2a2fSTomer Maimon 	int off_num;
1659c81b2ccSTomer Maimon 
1669c81b2ccSTomer Maimon 	offset = reset_spec->args[0];
167fc5d2a2fSTomer Maimon 	for (off_num = 0 ; off_num < rc->info->num_ipsrst ; off_num++) {
168fc5d2a2fSTomer Maimon 		if (offset == rc->info->ipsrst[off_num]) {
169fc5d2a2fSTomer Maimon 			offset_found = true;
170fc5d2a2fSTomer Maimon 			break;
171fc5d2a2fSTomer Maimon 		}
172fc5d2a2fSTomer Maimon 	}
173fc5d2a2fSTomer Maimon 
174fc5d2a2fSTomer Maimon 	if (!offset_found) {
1759c81b2ccSTomer Maimon 		dev_err(rcdev->dev, "Error reset register (0x%x)\n", offset);
1769c81b2ccSTomer Maimon 		return -EINVAL;
1779c81b2ccSTomer Maimon 	}
178fc5d2a2fSTomer Maimon 
1799c81b2ccSTomer Maimon 	bit = reset_spec->args[1];
1809c81b2ccSTomer Maimon 	if (bit >= NPCM_RC_RESETS_PER_REG) {
1819c81b2ccSTomer Maimon 		dev_err(rcdev->dev, "Error reset number (%d)\n", bit);
1829c81b2ccSTomer Maimon 		return -EINVAL;
1839c81b2ccSTomer Maimon 	}
1849c81b2ccSTomer Maimon 
1859c81b2ccSTomer Maimon 	return (offset << 8) | bit;
1869c81b2ccSTomer Maimon }
1879c81b2ccSTomer Maimon 
1889c81b2ccSTomer Maimon static const struct of_device_id npcm_rc_match[] = {
189fc5d2a2fSTomer Maimon 	{ .compatible = "nuvoton,npcm750-reset", .data = &npxm7xx_reset_info},
190fc5d2a2fSTomer Maimon 	{ .compatible = "nuvoton,npcm845-reset", .data = &npxm8xx_reset_info},
1919c81b2ccSTomer Maimon 	{ }
1929c81b2ccSTomer Maimon };
1939c81b2ccSTomer Maimon 
npcm_usb_reset_npcm7xx(struct npcm_rc_data * rc)194fc5d2a2fSTomer Maimon static void npcm_usb_reset_npcm7xx(struct npcm_rc_data *rc)
1959c81b2ccSTomer Maimon {
1969c81b2ccSTomer Maimon 	u32 mdlr, iprst1, iprst2, iprst3;
1979c81b2ccSTomer Maimon 	u32 ipsrst1_bits = 0;
1989c81b2ccSTomer Maimon 	u32 ipsrst2_bits = NPCM_IPSRST2_USB_HOST;
1999c81b2ccSTomer Maimon 	u32 ipsrst3_bits = 0;
2009c81b2ccSTomer Maimon 
2019c81b2ccSTomer Maimon 	/* checking which USB device is enabled */
202fc5d2a2fSTomer Maimon 	regmap_read(rc->gcr_regmap, NPCM_MDLR_OFFSET, &mdlr);
203fc5d2a2fSTomer Maimon 	if (!(mdlr & NPCM7XX_MDLR_USBD0))
2049c81b2ccSTomer Maimon 		ipsrst3_bits |= NPCM_IPSRST3_USBD0;
205fc5d2a2fSTomer Maimon 	if (!(mdlr & NPCM7XX_MDLR_USBD1))
2069c81b2ccSTomer Maimon 		ipsrst1_bits |= NPCM_IPSRST1_USBD1;
207fc5d2a2fSTomer Maimon 	if (!(mdlr & NPCM7XX_MDLR_USBD2_4))
2089c81b2ccSTomer Maimon 		ipsrst1_bits |= (NPCM_IPSRST1_USBD2 |
2099c81b2ccSTomer Maimon 				 NPCM_IPSRST1_USBD3 |
2109c81b2ccSTomer Maimon 				 NPCM_IPSRST1_USBD4);
211fc5d2a2fSTomer Maimon 	if (!(mdlr & NPCM7XX_MDLR_USBD0)) {
2129c81b2ccSTomer Maimon 		ipsrst1_bits |= (NPCM_IPSRST1_USBD5 |
2139c81b2ccSTomer Maimon 				 NPCM_IPSRST1_USBD6);
2149c81b2ccSTomer Maimon 		ipsrst3_bits |= (NPCM_IPSRST3_USBD7 |
2159c81b2ccSTomer Maimon 				 NPCM_IPSRST3_USBD8 |
2169c81b2ccSTomer Maimon 				 NPCM_IPSRST3_USBD9);
2179c81b2ccSTomer Maimon 	}
2189c81b2ccSTomer Maimon 
2199c81b2ccSTomer Maimon 	/* assert reset USB PHY and USB devices */
2209c81b2ccSTomer Maimon 	iprst1 = readl(rc->base + NPCM_IPSRST1);
2219c81b2ccSTomer Maimon 	iprst2 = readl(rc->base + NPCM_IPSRST2);
2229c81b2ccSTomer Maimon 	iprst3 = readl(rc->base + NPCM_IPSRST3);
2239c81b2ccSTomer Maimon 
2249c81b2ccSTomer Maimon 	iprst1 |= ipsrst1_bits;
2259c81b2ccSTomer Maimon 	iprst2 |= ipsrst2_bits;
2269c81b2ccSTomer Maimon 	iprst3 |= (ipsrst3_bits | NPCM_IPSRST3_USBPHY1 |
2279c81b2ccSTomer Maimon 		   NPCM_IPSRST3_USBPHY2);
2289c81b2ccSTomer Maimon 
2299c81b2ccSTomer Maimon 	writel(iprst1, rc->base + NPCM_IPSRST1);
2309c81b2ccSTomer Maimon 	writel(iprst2, rc->base + NPCM_IPSRST2);
2319c81b2ccSTomer Maimon 	writel(iprst3, rc->base + NPCM_IPSRST3);
2329c81b2ccSTomer Maimon 
2339c81b2ccSTomer Maimon 	/* clear USB PHY RS bit */
234fc5d2a2fSTomer Maimon 	regmap_update_bits(rc->gcr_regmap, NPCM_USB1PHYCTL_OFFSET,
2359c81b2ccSTomer Maimon 			   NPCM_USBXPHYCTL_RS, 0);
236fc5d2a2fSTomer Maimon 	regmap_update_bits(rc->gcr_regmap, NPCM_USB2PHYCTL_OFFSET,
2379c81b2ccSTomer Maimon 			   NPCM_USBXPHYCTL_RS, 0);
2389c81b2ccSTomer Maimon 
2399c81b2ccSTomer Maimon 	/* deassert reset USB PHY */
2409c81b2ccSTomer Maimon 	iprst3 &= ~(NPCM_IPSRST3_USBPHY1 | NPCM_IPSRST3_USBPHY2);
2419c81b2ccSTomer Maimon 	writel(iprst3, rc->base + NPCM_IPSRST3);
2429c81b2ccSTomer Maimon 
2439c81b2ccSTomer Maimon 	udelay(50);
2449c81b2ccSTomer Maimon 
2459c81b2ccSTomer Maimon 	/* set USB PHY RS bit */
246fc5d2a2fSTomer Maimon 	regmap_update_bits(rc->gcr_regmap, NPCM_USB1PHYCTL_OFFSET,
2479c81b2ccSTomer Maimon 			   NPCM_USBXPHYCTL_RS, NPCM_USBXPHYCTL_RS);
248fc5d2a2fSTomer Maimon 	regmap_update_bits(rc->gcr_regmap, NPCM_USB2PHYCTL_OFFSET,
2499c81b2ccSTomer Maimon 			   NPCM_USBXPHYCTL_RS, NPCM_USBXPHYCTL_RS);
2509c81b2ccSTomer Maimon 
2519c81b2ccSTomer Maimon 	/* deassert reset USB devices*/
2529c81b2ccSTomer Maimon 	iprst1 &= ~ipsrst1_bits;
2539c81b2ccSTomer Maimon 	iprst2 &= ~ipsrst2_bits;
2549c81b2ccSTomer Maimon 	iprst3 &= ~ipsrst3_bits;
2559c81b2ccSTomer Maimon 
2569c81b2ccSTomer Maimon 	writel(iprst1, rc->base + NPCM_IPSRST1);
2579c81b2ccSTomer Maimon 	writel(iprst2, rc->base + NPCM_IPSRST2);
2589c81b2ccSTomer Maimon 	writel(iprst3, rc->base + NPCM_IPSRST3);
259fc5d2a2fSTomer Maimon }
260fc5d2a2fSTomer Maimon 
npcm_usb_reset_npcm8xx(struct npcm_rc_data * rc)261fc5d2a2fSTomer Maimon static void npcm_usb_reset_npcm8xx(struct npcm_rc_data *rc)
262fc5d2a2fSTomer Maimon {
263fc5d2a2fSTomer Maimon 	u32 mdlr, iprst1, iprst2, iprst3, iprst4;
264fc5d2a2fSTomer Maimon 	u32 ipsrst1_bits = 0;
265fc5d2a2fSTomer Maimon 	u32 ipsrst2_bits = NPCM_IPSRST2_USB_HOST;
266fc5d2a2fSTomer Maimon 	u32 ipsrst3_bits = 0;
267fc5d2a2fSTomer Maimon 	u32 ipsrst4_bits = NPCM_IPSRST4_USB_HOST2 | NPCM_IPSRST4_USBPHY3;
268fc5d2a2fSTomer Maimon 
269fc5d2a2fSTomer Maimon 	/* checking which USB device is enabled */
270fc5d2a2fSTomer Maimon 	regmap_read(rc->gcr_regmap, NPCM_MDLR_OFFSET, &mdlr);
271fc5d2a2fSTomer Maimon 	if (!(mdlr & NPCM8XX_MDLR_USBD0_3)) {
272fc5d2a2fSTomer Maimon 		ipsrst3_bits |= NPCM_IPSRST3_USBD0;
273fc5d2a2fSTomer Maimon 		ipsrst1_bits |= (NPCM_IPSRST1_USBD1 |
274fc5d2a2fSTomer Maimon 				 NPCM_IPSRST1_USBD2 |
275fc5d2a2fSTomer Maimon 				 NPCM_IPSRST1_USBD3);
276fc5d2a2fSTomer Maimon 	}
277fc5d2a2fSTomer Maimon 	if (!(mdlr & NPCM8XX_MDLR_USBD4_7)) {
278fc5d2a2fSTomer Maimon 		ipsrst1_bits |= (NPCM_IPSRST1_USBD4 |
279fc5d2a2fSTomer Maimon 				 NPCM_IPSRST1_USBD5 |
280fc5d2a2fSTomer Maimon 				 NPCM_IPSRST1_USBD6);
281fc5d2a2fSTomer Maimon 		ipsrst3_bits |= NPCM_IPSRST3_USBD7;
282fc5d2a2fSTomer Maimon 	}
283fc5d2a2fSTomer Maimon 
284fc5d2a2fSTomer Maimon 	if (!(mdlr & NPCM8XX_MDLR_USBD8))
285fc5d2a2fSTomer Maimon 		ipsrst3_bits |= NPCM_IPSRST3_USBD8;
286fc5d2a2fSTomer Maimon 	if (!(mdlr & NPCM8XX_MDLR_USBD9))
287fc5d2a2fSTomer Maimon 		ipsrst3_bits |= NPCM_IPSRST3_USBD9;
288fc5d2a2fSTomer Maimon 
289fc5d2a2fSTomer Maimon 	/* assert reset USB PHY and USB devices */
290fc5d2a2fSTomer Maimon 	iprst1 = readl(rc->base + NPCM_IPSRST1);
291fc5d2a2fSTomer Maimon 	iprst2 = readl(rc->base + NPCM_IPSRST2);
292fc5d2a2fSTomer Maimon 	iprst3 = readl(rc->base + NPCM_IPSRST3);
293fc5d2a2fSTomer Maimon 	iprst4 = readl(rc->base + NPCM_IPSRST4);
294fc5d2a2fSTomer Maimon 
295fc5d2a2fSTomer Maimon 	iprst1 |= ipsrst1_bits;
296fc5d2a2fSTomer Maimon 	iprst2 |= ipsrst2_bits;
297fc5d2a2fSTomer Maimon 	iprst3 |= (ipsrst3_bits | NPCM_IPSRST3_USBPHY1 |
298fc5d2a2fSTomer Maimon 		   NPCM_IPSRST3_USBPHY2);
299ae358d71STomer Maimon 	iprst4 |= ipsrst4_bits;
300fc5d2a2fSTomer Maimon 
301fc5d2a2fSTomer Maimon 	writel(iprst1, rc->base + NPCM_IPSRST1);
302fc5d2a2fSTomer Maimon 	writel(iprst2, rc->base + NPCM_IPSRST2);
303fc5d2a2fSTomer Maimon 	writel(iprst3, rc->base + NPCM_IPSRST3);
304fc5d2a2fSTomer Maimon 	writel(iprst4, rc->base + NPCM_IPSRST4);
305fc5d2a2fSTomer Maimon 
306fc5d2a2fSTomer Maimon 	/* clear USB PHY RS bit */
307fc5d2a2fSTomer Maimon 	regmap_update_bits(rc->gcr_regmap, NPCM_USB1PHYCTL_OFFSET,
308fc5d2a2fSTomer Maimon 			   NPCM_USBXPHYCTL_RS, 0);
309fc5d2a2fSTomer Maimon 	regmap_update_bits(rc->gcr_regmap, NPCM_USB2PHYCTL_OFFSET,
310fc5d2a2fSTomer Maimon 			   NPCM_USBXPHYCTL_RS, 0);
311fc5d2a2fSTomer Maimon 	regmap_update_bits(rc->gcr_regmap, NPCM_USB3PHYCTL_OFFSET,
312fc5d2a2fSTomer Maimon 			   NPCM_USBXPHYCTL_RS, 0);
313fc5d2a2fSTomer Maimon 
314fc5d2a2fSTomer Maimon 	/* deassert reset USB PHY */
315fc5d2a2fSTomer Maimon 	iprst3 &= ~(NPCM_IPSRST3_USBPHY1 | NPCM_IPSRST3_USBPHY2);
316fc5d2a2fSTomer Maimon 	writel(iprst3, rc->base + NPCM_IPSRST3);
317fc5d2a2fSTomer Maimon 	iprst4 &= ~NPCM_IPSRST4_USBPHY3;
318fc5d2a2fSTomer Maimon 	writel(iprst4, rc->base + NPCM_IPSRST4);
319fc5d2a2fSTomer Maimon 
320fc5d2a2fSTomer Maimon 	/* set USB PHY RS bit */
321fc5d2a2fSTomer Maimon 	regmap_update_bits(rc->gcr_regmap, NPCM_USB1PHYCTL_OFFSET,
322fc5d2a2fSTomer Maimon 			   NPCM_USBXPHYCTL_RS, NPCM_USBXPHYCTL_RS);
323fc5d2a2fSTomer Maimon 	regmap_update_bits(rc->gcr_regmap, NPCM_USB2PHYCTL_OFFSET,
324fc5d2a2fSTomer Maimon 			   NPCM_USBXPHYCTL_RS, NPCM_USBXPHYCTL_RS);
325fc5d2a2fSTomer Maimon 	regmap_update_bits(rc->gcr_regmap, NPCM_USB3PHYCTL_OFFSET,
326fc5d2a2fSTomer Maimon 			   NPCM_USBXPHYCTL_RS, NPCM_USBXPHYCTL_RS);
327fc5d2a2fSTomer Maimon 
328fc5d2a2fSTomer Maimon 	/* deassert reset USB devices*/
329fc5d2a2fSTomer Maimon 	iprst1 &= ~ipsrst1_bits;
330fc5d2a2fSTomer Maimon 	iprst2 &= ~ipsrst2_bits;
331fc5d2a2fSTomer Maimon 	iprst3 &= ~ipsrst3_bits;
332fc5d2a2fSTomer Maimon 	iprst4 &= ~ipsrst4_bits;
333fc5d2a2fSTomer Maimon 
334fc5d2a2fSTomer Maimon 	writel(iprst1, rc->base + NPCM_IPSRST1);
335fc5d2a2fSTomer Maimon 	writel(iprst2, rc->base + NPCM_IPSRST2);
336fc5d2a2fSTomer Maimon 	writel(iprst3, rc->base + NPCM_IPSRST3);
337fc5d2a2fSTomer Maimon 	writel(iprst4, rc->base + NPCM_IPSRST4);
338fc5d2a2fSTomer Maimon }
339fc5d2a2fSTomer Maimon 
340fc5d2a2fSTomer Maimon /*
341fc5d2a2fSTomer Maimon  *  The following procedure should be observed in USB PHY, USB device and
342fc5d2a2fSTomer Maimon  *  USB host initialization at BMC boot
343fc5d2a2fSTomer Maimon  */
npcm_usb_reset(struct platform_device * pdev,struct npcm_rc_data * rc)344fc5d2a2fSTomer Maimon static int npcm_usb_reset(struct platform_device *pdev, struct npcm_rc_data *rc)
345fc5d2a2fSTomer Maimon {
346fc5d2a2fSTomer Maimon 	struct device *dev = &pdev->dev;
347fc5d2a2fSTomer Maimon 
348fc5d2a2fSTomer Maimon 	rc->gcr_regmap = syscon_regmap_lookup_by_phandle(dev->of_node, "nuvoton,sysgcr");
349fc5d2a2fSTomer Maimon 	if (IS_ERR(rc->gcr_regmap)) {
350fc5d2a2fSTomer Maimon 		dev_warn(&pdev->dev, "Failed to find nuvoton,sysgcr property, please update the device tree\n");
351fc5d2a2fSTomer Maimon 		dev_info(&pdev->dev, "Using nuvoton,npcm750-gcr for Poleg backward compatibility\n");
352fc5d2a2fSTomer Maimon 		rc->gcr_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr");
353fc5d2a2fSTomer Maimon 		if (IS_ERR(rc->gcr_regmap)) {
354fc5d2a2fSTomer Maimon 			dev_err(&pdev->dev, "Failed to find nuvoton,npcm750-gcr");
355fc5d2a2fSTomer Maimon 			return PTR_ERR(rc->gcr_regmap);
356fc5d2a2fSTomer Maimon 		}
357fc5d2a2fSTomer Maimon 	}
358fc5d2a2fSTomer Maimon 
359fc5d2a2fSTomer Maimon 	rc->info = (const struct npcm_reset_info *)
360fc5d2a2fSTomer Maimon 			of_match_device(dev->driver->of_match_table, dev)->data;
361fc5d2a2fSTomer Maimon 	switch (rc->info->bmc_id) {
362fc5d2a2fSTomer Maimon 	case BMC_NPCM7XX:
363fc5d2a2fSTomer Maimon 		npcm_usb_reset_npcm7xx(rc);
364fc5d2a2fSTomer Maimon 		break;
365fc5d2a2fSTomer Maimon 	case BMC_NPCM8XX:
366fc5d2a2fSTomer Maimon 		npcm_usb_reset_npcm8xx(rc);
367fc5d2a2fSTomer Maimon 		break;
368fc5d2a2fSTomer Maimon 	default:
369fc5d2a2fSTomer Maimon 		return -ENODEV;
370fc5d2a2fSTomer Maimon 	}
3719c81b2ccSTomer Maimon 
3729c81b2ccSTomer Maimon 	return 0;
3739c81b2ccSTomer Maimon }
3749c81b2ccSTomer Maimon 
3759c81b2ccSTomer Maimon static const struct reset_control_ops npcm_rc_ops = {
3769c81b2ccSTomer Maimon 	.assert		= npcm_rc_assert,
3779c81b2ccSTomer Maimon 	.deassert	= npcm_rc_deassert,
3789c81b2ccSTomer Maimon 	.status		= npcm_rc_status,
3799c81b2ccSTomer Maimon };
3809c81b2ccSTomer Maimon 
npcm_clock_unregister_adev(void * _adev)381*cd3c50d8STomer Maimon static void npcm_clock_unregister_adev(void *_adev)
382*cd3c50d8STomer Maimon {
383*cd3c50d8STomer Maimon 	struct auxiliary_device *adev = _adev;
384*cd3c50d8STomer Maimon 
385*cd3c50d8STomer Maimon 	auxiliary_device_delete(adev);
386*cd3c50d8STomer Maimon 	auxiliary_device_uninit(adev);
387*cd3c50d8STomer Maimon }
388*cd3c50d8STomer Maimon 
npcm_clock_adev_release(struct device * dev)389*cd3c50d8STomer Maimon static void npcm_clock_adev_release(struct device *dev)
390*cd3c50d8STomer Maimon {
391*cd3c50d8STomer Maimon 	struct auxiliary_device *adev = to_auxiliary_dev(dev);
392*cd3c50d8STomer Maimon 	struct npcm_clock_adev *rdev = to_npcm_clock_adev(adev);
393*cd3c50d8STomer Maimon 
394*cd3c50d8STomer Maimon 	kfree(rdev);
395*cd3c50d8STomer Maimon }
396*cd3c50d8STomer Maimon 
npcm_clock_adev_alloc(struct npcm_rc_data * rst_data,char * clk_name)397*cd3c50d8STomer Maimon static struct auxiliary_device *npcm_clock_adev_alloc(struct npcm_rc_data *rst_data, char *clk_name)
398*cd3c50d8STomer Maimon {
399*cd3c50d8STomer Maimon 	struct npcm_clock_adev *rdev;
400*cd3c50d8STomer Maimon 	struct auxiliary_device *adev;
401*cd3c50d8STomer Maimon 	int ret;
402*cd3c50d8STomer Maimon 
403*cd3c50d8STomer Maimon 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
404*cd3c50d8STomer Maimon 	if (!rdev)
405*cd3c50d8STomer Maimon 		return ERR_PTR(-ENOMEM);
406*cd3c50d8STomer Maimon 
407*cd3c50d8STomer Maimon 	rdev->base = rst_data->base;
408*cd3c50d8STomer Maimon 
409*cd3c50d8STomer Maimon 	adev = &rdev->adev;
410*cd3c50d8STomer Maimon 	adev->name = clk_name;
411*cd3c50d8STomer Maimon 	adev->dev.parent = rst_data->dev;
412*cd3c50d8STomer Maimon 	adev->dev.release = npcm_clock_adev_release;
413*cd3c50d8STomer Maimon 	adev->id = 555u;
414*cd3c50d8STomer Maimon 
415*cd3c50d8STomer Maimon 	ret = auxiliary_device_init(adev);
416*cd3c50d8STomer Maimon 	if (ret) {
417*cd3c50d8STomer Maimon 		kfree(rdev);
418*cd3c50d8STomer Maimon 		return ERR_PTR(ret);
419*cd3c50d8STomer Maimon 	}
420*cd3c50d8STomer Maimon 
421*cd3c50d8STomer Maimon 	return adev;
422*cd3c50d8STomer Maimon }
423*cd3c50d8STomer Maimon 
npcm8xx_clock_controller_register(struct npcm_rc_data * rst_data,char * clk_name)424*cd3c50d8STomer Maimon static int npcm8xx_clock_controller_register(struct npcm_rc_data *rst_data, char *clk_name)
425*cd3c50d8STomer Maimon {
426*cd3c50d8STomer Maimon 	struct auxiliary_device *adev;
427*cd3c50d8STomer Maimon 	int ret;
428*cd3c50d8STomer Maimon 
429*cd3c50d8STomer Maimon 	adev = npcm_clock_adev_alloc(rst_data, clk_name);
430*cd3c50d8STomer Maimon 	if (IS_ERR(adev))
431*cd3c50d8STomer Maimon 		return PTR_ERR(adev);
432*cd3c50d8STomer Maimon 
433*cd3c50d8STomer Maimon 	ret = auxiliary_device_add(adev);
434*cd3c50d8STomer Maimon 	if (ret) {
435*cd3c50d8STomer Maimon 		auxiliary_device_uninit(adev);
436*cd3c50d8STomer Maimon 		return ret;
437*cd3c50d8STomer Maimon 	}
438*cd3c50d8STomer Maimon 
439*cd3c50d8STomer Maimon 	return devm_add_action_or_reset(rst_data->dev, npcm_clock_unregister_adev, adev);
440*cd3c50d8STomer Maimon }
441*cd3c50d8STomer Maimon 
npcm_rc_probe(struct platform_device * pdev)4429c81b2ccSTomer Maimon static int npcm_rc_probe(struct platform_device *pdev)
4439c81b2ccSTomer Maimon {
4449c81b2ccSTomer Maimon 	struct npcm_rc_data *rc;
4459c81b2ccSTomer Maimon 	int ret;
4469c81b2ccSTomer Maimon 
4479c81b2ccSTomer Maimon 	rc = devm_kzalloc(&pdev->dev, sizeof(*rc), GFP_KERNEL);
4489c81b2ccSTomer Maimon 	if (!rc)
4499c81b2ccSTomer Maimon 		return -ENOMEM;
4509c81b2ccSTomer Maimon 
4519c81b2ccSTomer Maimon 	rc->base = devm_platform_ioremap_resource(pdev, 0);
4529c81b2ccSTomer Maimon 	if (IS_ERR(rc->base))
4539c81b2ccSTomer Maimon 		return PTR_ERR(rc->base);
4549c81b2ccSTomer Maimon 
4559c81b2ccSTomer Maimon 	spin_lock_init(&rc->lock);
4569c81b2ccSTomer Maimon 
4579c81b2ccSTomer Maimon 	rc->rcdev.owner = THIS_MODULE;
4589c81b2ccSTomer Maimon 	rc->rcdev.ops = &npcm_rc_ops;
4599c81b2ccSTomer Maimon 	rc->rcdev.of_node = pdev->dev.of_node;
4609c81b2ccSTomer Maimon 	rc->rcdev.of_reset_n_cells = 2;
4619c81b2ccSTomer Maimon 	rc->rcdev.of_xlate = npcm_reset_xlate;
462*cd3c50d8STomer Maimon 	rc->dev = &pdev->dev;
4639c81b2ccSTomer Maimon 
4649c81b2ccSTomer Maimon 	ret = devm_reset_controller_register(&pdev->dev, &rc->rcdev);
4659c81b2ccSTomer Maimon 	if (ret) {
4669c81b2ccSTomer Maimon 		dev_err(&pdev->dev, "unable to register device\n");
4679c81b2ccSTomer Maimon 		return ret;
4689c81b2ccSTomer Maimon 	}
4699c81b2ccSTomer Maimon 
4709c81b2ccSTomer Maimon 	if (npcm_usb_reset(pdev, rc))
4719c81b2ccSTomer Maimon 		dev_warn(&pdev->dev, "NPCM USB reset failed, can cause issues with UDC and USB host\n");
4729c81b2ccSTomer Maimon 
4739c81b2ccSTomer Maimon 	if (!of_property_read_u32(pdev->dev.of_node, "nuvoton,sw-reset-number",
4749c81b2ccSTomer Maimon 				  &rc->sw_reset_number)) {
4759c81b2ccSTomer Maimon 		if (rc->sw_reset_number && rc->sw_reset_number < 5) {
4769c81b2ccSTomer Maimon 			rc->restart_nb.priority = 192,
4779c81b2ccSTomer Maimon 			rc->restart_nb.notifier_call = npcm_rc_restart,
4789c81b2ccSTomer Maimon 			ret = register_restart_handler(&rc->restart_nb);
4799c81b2ccSTomer Maimon 			if (ret)
4809c81b2ccSTomer Maimon 				dev_warn(&pdev->dev, "failed to register restart handler\n");
4819c81b2ccSTomer Maimon 		}
4829c81b2ccSTomer Maimon 	}
4839c81b2ccSTomer Maimon 
484*cd3c50d8STomer Maimon 	switch (rc->info->bmc_id) {
485*cd3c50d8STomer Maimon 	case BMC_NPCM8XX:
486*cd3c50d8STomer Maimon 		return npcm8xx_clock_controller_register(rc, "clk-npcm8xx");
487*cd3c50d8STomer Maimon 	default:
4889c81b2ccSTomer Maimon 		return ret;
4899c81b2ccSTomer Maimon 	}
490*cd3c50d8STomer Maimon }
4919c81b2ccSTomer Maimon 
4929c81b2ccSTomer Maimon static struct platform_driver npcm_rc_driver = {
4939c81b2ccSTomer Maimon 	.probe	= npcm_rc_probe,
4949c81b2ccSTomer Maimon 	.driver	= {
4959c81b2ccSTomer Maimon 		.name			= "npcm-reset",
4969c81b2ccSTomer Maimon 		.of_match_table		= npcm_rc_match,
4979c81b2ccSTomer Maimon 		.suppress_bind_attrs	= true,
4989c81b2ccSTomer Maimon 	},
4999c81b2ccSTomer Maimon };
5009c81b2ccSTomer Maimon builtin_platform_driver(npcm_rc_driver);
501