1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Nuvoton Technology corporation.
4 #include <linux/auxiliary_bus.h>
5 #include <linux/delay.h>
8 #include <linux/init.h>
10 #include <linux/platform_device.h>
11 #include <linux/property.h>
12 #include <linux/reboot.h>
13 #include <linux/reset-controller.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/regmap.h>
18 #include <linux/of_address.h>
20 #include <soc/nuvoton/clock-npcm8xx.h>
22 /* NPCM7xx GCR registers */
23 #define NPCM_MDLR_OFFSET 0x7C
24 #define NPCM7XX_MDLR_USBD0 BIT(9)
25 #define NPCM7XX_MDLR_USBD1 BIT(8)
26 #define NPCM7XX_MDLR_USBD2_4 BIT(21)
27 #define NPCM7XX_MDLR_USBD5_9 BIT(22)
29 /* NPCM8xx MDLR bits */
30 #define NPCM8XX_MDLR_USBD0_3 BIT(9)
31 #define NPCM8XX_MDLR_USBD4_7 BIT(22)
32 #define NPCM8XX_MDLR_USBD8 BIT(24)
33 #define NPCM8XX_MDLR_USBD9 BIT(21)
35 #define NPCM_USB1PHYCTL_OFFSET 0x140
36 #define NPCM_USB2PHYCTL_OFFSET 0x144
37 #define NPCM_USB3PHYCTL_OFFSET 0x148
38 #define NPCM_USBXPHYCTL_RS BIT(28)
40 /* NPCM7xx Reset registers */
41 #define NPCM_SWRSTR 0x14
42 #define NPCM_SWRST BIT(2)
44 #define NPCM_IPSRST1 0x20
45 #define NPCM_IPSRST1_USBD1 BIT(5)
46 #define NPCM_IPSRST1_USBD2 BIT(8)
47 #define NPCM_IPSRST1_USBD3 BIT(25)
48 #define NPCM_IPSRST1_USBD4 BIT(22)
49 #define NPCM_IPSRST1_USBD5 BIT(23)
50 #define NPCM_IPSRST1_USBD6 BIT(24)
52 #define NPCM_IPSRST2 0x24
53 #define NPCM_IPSRST2_USB_HOST BIT(26)
55 #define NPCM_IPSRST3 0x34
56 #define NPCM_IPSRST3_USBD0 BIT(4)
57 #define NPCM_IPSRST3_USBD7 BIT(5)
58 #define NPCM_IPSRST3_USBD8 BIT(6)
59 #define NPCM_IPSRST3_USBD9 BIT(7)
60 #define NPCM_IPSRST3_USBPHY1 BIT(24)
61 #define NPCM_IPSRST3_USBPHY2 BIT(25)
63 #define NPCM_IPSRST4 0x74
64 #define NPCM_IPSRST4_USBPHY3 BIT(25)
65 #define NPCM_IPSRST4_USB_HOST2 BIT(31)
67 #define NPCM_RC_RESETS_PER_REG 32
68 #define NPCM_MASK_RESETS GENMASK(4, 0)
75 static const u32 npxm7xx_ipsrst[] = {NPCM_IPSRST1, NPCM_IPSRST2, NPCM_IPSRST3};
76 static const u32 npxm8xx_ipsrst[] = {NPCM_IPSRST1, NPCM_IPSRST2, NPCM_IPSRST3,
79 struct npcm_reset_info {
85 static const struct npcm_reset_info npxm7xx_reset_info[] = {
86 {.bmc_id = BMC_NPCM7XX, .num_ipsrst = 3, .ipsrst = npxm7xx_ipsrst}};
87 static const struct npcm_reset_info npxm8xx_reset_info[] = {
88 {.bmc_id = BMC_NPCM8XX, .num_ipsrst = 4, .ipsrst = npxm8xx_ipsrst}};
91 struct reset_controller_dev rcdev;
92 struct notifier_block restart_nb;
93 const struct npcm_reset_info *info;
94 struct regmap *gcr_regmap;
101 #define to_rc_data(p) container_of(p, struct npcm_rc_data, rcdev)
103 static int npcm_rc_restart(struct notifier_block *nb, unsigned long mode,
106 struct npcm_rc_data *rc = container_of(nb, struct npcm_rc_data,
109 writel(NPCM_SWRST << rc->sw_reset_number, rc->base + NPCM_SWRSTR);
112 pr_emerg("%s: unable to restart system\n", __func__);
117 static int npcm_rc_setclear_reset(struct reset_controller_dev *rcdev,
118 unsigned long id, bool set)
120 struct npcm_rc_data *rc = to_rc_data(rcdev);
121 unsigned int rst_bit = BIT(id & NPCM_MASK_RESETS);
122 unsigned int ctrl_offset = id >> 8;
126 spin_lock_irqsave(&rc->lock, flags);
127 stat = readl(rc->base + ctrl_offset);
129 writel(stat | rst_bit, rc->base + ctrl_offset);
131 writel(stat & ~rst_bit, rc->base + ctrl_offset);
132 spin_unlock_irqrestore(&rc->lock, flags);
137 static int npcm_rc_assert(struct reset_controller_dev *rcdev, unsigned long id)
139 return npcm_rc_setclear_reset(rcdev, id, true);
142 static int npcm_rc_deassert(struct reset_controller_dev *rcdev,
145 return npcm_rc_setclear_reset(rcdev, id, false);
148 static int npcm_rc_status(struct reset_controller_dev *rcdev,
151 struct npcm_rc_data *rc = to_rc_data(rcdev);
152 unsigned int rst_bit = BIT(id & NPCM_MASK_RESETS);
153 unsigned int ctrl_offset = id >> 8;
155 return (readl(rc->base + ctrl_offset) & rst_bit);
158 static int npcm_reset_xlate(struct reset_controller_dev *rcdev,
159 const struct of_phandle_args *reset_spec)
161 struct npcm_rc_data *rc = to_rc_data(rcdev);
162 unsigned int offset, bit;
163 bool offset_found = false;
166 offset = reset_spec->args[0];
167 for (off_num = 0 ; off_num < rc->info->num_ipsrst ; off_num++) {
168 if (offset == rc->info->ipsrst[off_num]) {
175 dev_err(rcdev->dev, "Error reset register (0x%x)\n", offset);
179 bit = reset_spec->args[1];
180 if (bit >= NPCM_RC_RESETS_PER_REG) {
181 dev_err(rcdev->dev, "Error reset number (%d)\n", bit);
185 return (offset << 8) | bit;
188 static const struct of_device_id npcm_rc_match[] = {
189 { .compatible = "nuvoton,npcm750-reset", .data = &npxm7xx_reset_info},
190 { .compatible = "nuvoton,npcm845-reset", .data = &npxm8xx_reset_info},
194 static void npcm_usb_reset_npcm7xx(struct npcm_rc_data *rc)
196 u32 mdlr, iprst1, iprst2, iprst3;
197 u32 ipsrst1_bits = 0;
198 u32 ipsrst2_bits = NPCM_IPSRST2_USB_HOST;
199 u32 ipsrst3_bits = 0;
201 /* checking which USB device is enabled */
202 regmap_read(rc->gcr_regmap, NPCM_MDLR_OFFSET, &mdlr);
203 if (!(mdlr & NPCM7XX_MDLR_USBD0))
204 ipsrst3_bits |= NPCM_IPSRST3_USBD0;
205 if (!(mdlr & NPCM7XX_MDLR_USBD1))
206 ipsrst1_bits |= NPCM_IPSRST1_USBD1;
207 if (!(mdlr & NPCM7XX_MDLR_USBD2_4))
208 ipsrst1_bits |= (NPCM_IPSRST1_USBD2 |
211 if (!(mdlr & NPCM7XX_MDLR_USBD0)) {
212 ipsrst1_bits |= (NPCM_IPSRST1_USBD5 |
214 ipsrst3_bits |= (NPCM_IPSRST3_USBD7 |
219 /* assert reset USB PHY and USB devices */
220 iprst1 = readl(rc->base + NPCM_IPSRST1);
221 iprst2 = readl(rc->base + NPCM_IPSRST2);
222 iprst3 = readl(rc->base + NPCM_IPSRST3);
224 iprst1 |= ipsrst1_bits;
225 iprst2 |= ipsrst2_bits;
226 iprst3 |= (ipsrst3_bits | NPCM_IPSRST3_USBPHY1 |
227 NPCM_IPSRST3_USBPHY2);
229 writel(iprst1, rc->base + NPCM_IPSRST1);
230 writel(iprst2, rc->base + NPCM_IPSRST2);
231 writel(iprst3, rc->base + NPCM_IPSRST3);
233 /* clear USB PHY RS bit */
234 regmap_update_bits(rc->gcr_regmap, NPCM_USB1PHYCTL_OFFSET,
235 NPCM_USBXPHYCTL_RS, 0);
236 regmap_update_bits(rc->gcr_regmap, NPCM_USB2PHYCTL_OFFSET,
237 NPCM_USBXPHYCTL_RS, 0);
239 /* deassert reset USB PHY */
240 iprst3 &= ~(NPCM_IPSRST3_USBPHY1 | NPCM_IPSRST3_USBPHY2);
241 writel(iprst3, rc->base + NPCM_IPSRST3);
245 /* set USB PHY RS bit */
246 regmap_update_bits(rc->gcr_regmap, NPCM_USB1PHYCTL_OFFSET,
247 NPCM_USBXPHYCTL_RS, NPCM_USBXPHYCTL_RS);
248 regmap_update_bits(rc->gcr_regmap, NPCM_USB2PHYCTL_OFFSET,
249 NPCM_USBXPHYCTL_RS, NPCM_USBXPHYCTL_RS);
251 /* deassert reset USB devices*/
252 iprst1 &= ~ipsrst1_bits;
253 iprst2 &= ~ipsrst2_bits;
254 iprst3 &= ~ipsrst3_bits;
256 writel(iprst1, rc->base + NPCM_IPSRST1);
257 writel(iprst2, rc->base + NPCM_IPSRST2);
258 writel(iprst3, rc->base + NPCM_IPSRST3);
261 static void npcm_usb_reset_npcm8xx(struct npcm_rc_data *rc)
263 u32 mdlr, iprst1, iprst2, iprst3, iprst4;
264 u32 ipsrst1_bits = 0;
265 u32 ipsrst2_bits = NPCM_IPSRST2_USB_HOST;
266 u32 ipsrst3_bits = 0;
267 u32 ipsrst4_bits = NPCM_IPSRST4_USB_HOST2 | NPCM_IPSRST4_USBPHY3;
269 /* checking which USB device is enabled */
270 regmap_read(rc->gcr_regmap, NPCM_MDLR_OFFSET, &mdlr);
271 if (!(mdlr & NPCM8XX_MDLR_USBD0_3)) {
272 ipsrst3_bits |= NPCM_IPSRST3_USBD0;
273 ipsrst1_bits |= (NPCM_IPSRST1_USBD1 |
277 if (!(mdlr & NPCM8XX_MDLR_USBD4_7)) {
278 ipsrst1_bits |= (NPCM_IPSRST1_USBD4 |
281 ipsrst3_bits |= NPCM_IPSRST3_USBD7;
284 if (!(mdlr & NPCM8XX_MDLR_USBD8))
285 ipsrst3_bits |= NPCM_IPSRST3_USBD8;
286 if (!(mdlr & NPCM8XX_MDLR_USBD9))
287 ipsrst3_bits |= NPCM_IPSRST3_USBD9;
289 /* assert reset USB PHY and USB devices */
290 iprst1 = readl(rc->base + NPCM_IPSRST1);
291 iprst2 = readl(rc->base + NPCM_IPSRST2);
292 iprst3 = readl(rc->base + NPCM_IPSRST3);
293 iprst4 = readl(rc->base + NPCM_IPSRST4);
295 iprst1 |= ipsrst1_bits;
296 iprst2 |= ipsrst2_bits;
297 iprst3 |= (ipsrst3_bits | NPCM_IPSRST3_USBPHY1 |
298 NPCM_IPSRST3_USBPHY2);
299 iprst4 |= ipsrst4_bits;
301 writel(iprst1, rc->base + NPCM_IPSRST1);
302 writel(iprst2, rc->base + NPCM_IPSRST2);
303 writel(iprst3, rc->base + NPCM_IPSRST3);
304 writel(iprst4, rc->base + NPCM_IPSRST4);
306 /* clear USB PHY RS bit */
307 regmap_update_bits(rc->gcr_regmap, NPCM_USB1PHYCTL_OFFSET,
308 NPCM_USBXPHYCTL_RS, 0);
309 regmap_update_bits(rc->gcr_regmap, NPCM_USB2PHYCTL_OFFSET,
310 NPCM_USBXPHYCTL_RS, 0);
311 regmap_update_bits(rc->gcr_regmap, NPCM_USB3PHYCTL_OFFSET,
312 NPCM_USBXPHYCTL_RS, 0);
314 /* deassert reset USB PHY */
315 iprst3 &= ~(NPCM_IPSRST3_USBPHY1 | NPCM_IPSRST3_USBPHY2);
316 writel(iprst3, rc->base + NPCM_IPSRST3);
317 iprst4 &= ~NPCM_IPSRST4_USBPHY3;
318 writel(iprst4, rc->base + NPCM_IPSRST4);
320 /* set USB PHY RS bit */
321 regmap_update_bits(rc->gcr_regmap, NPCM_USB1PHYCTL_OFFSET,
322 NPCM_USBXPHYCTL_RS, NPCM_USBXPHYCTL_RS);
323 regmap_update_bits(rc->gcr_regmap, NPCM_USB2PHYCTL_OFFSET,
324 NPCM_USBXPHYCTL_RS, NPCM_USBXPHYCTL_RS);
325 regmap_update_bits(rc->gcr_regmap, NPCM_USB3PHYCTL_OFFSET,
326 NPCM_USBXPHYCTL_RS, NPCM_USBXPHYCTL_RS);
328 /* deassert reset USB devices*/
329 iprst1 &= ~ipsrst1_bits;
330 iprst2 &= ~ipsrst2_bits;
331 iprst3 &= ~ipsrst3_bits;
332 iprst4 &= ~ipsrst4_bits;
334 writel(iprst1, rc->base + NPCM_IPSRST1);
335 writel(iprst2, rc->base + NPCM_IPSRST2);
336 writel(iprst3, rc->base + NPCM_IPSRST3);
337 writel(iprst4, rc->base + NPCM_IPSRST4);
341 * The following procedure should be observed in USB PHY, USB device and
342 * USB host initialization at BMC boot
344 static int npcm_usb_reset(struct platform_device *pdev, struct npcm_rc_data *rc)
346 struct device *dev = &pdev->dev;
348 rc->gcr_regmap = syscon_regmap_lookup_by_phandle(dev->of_node, "nuvoton,sysgcr");
349 if (IS_ERR(rc->gcr_regmap)) {
350 dev_warn(&pdev->dev, "Failed to find nuvoton,sysgcr property, please update the device tree\n");
351 dev_info(&pdev->dev, "Using nuvoton,npcm750-gcr for Poleg backward compatibility\n");
352 rc->gcr_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr");
353 if (IS_ERR(rc->gcr_regmap)) {
354 dev_err(&pdev->dev, "Failed to find nuvoton,npcm750-gcr");
355 return PTR_ERR(rc->gcr_regmap);
359 rc->info = device_get_match_data(dev);
360 switch (rc->info->bmc_id) {
362 npcm_usb_reset_npcm7xx(rc);
365 npcm_usb_reset_npcm8xx(rc);
374 static const struct reset_control_ops npcm_rc_ops = {
375 .assert = npcm_rc_assert,
376 .deassert = npcm_rc_deassert,
377 .status = npcm_rc_status,
380 static void npcm_clock_unregister_adev(void *_adev)
382 struct auxiliary_device *adev = _adev;
384 auxiliary_device_delete(adev);
385 auxiliary_device_uninit(adev);
388 static void npcm_clock_adev_release(struct device *dev)
390 struct auxiliary_device *adev = to_auxiliary_dev(dev);
391 struct npcm_clock_adev *rdev = to_npcm_clock_adev(adev);
396 static struct auxiliary_device *npcm_clock_adev_alloc(struct npcm_rc_data *rst_data, char *clk_name)
398 struct npcm_clock_adev *rdev;
399 struct auxiliary_device *adev;
402 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
404 return ERR_PTR(-ENOMEM);
406 rdev->base = rst_data->base;
409 adev->name = clk_name;
410 adev->dev.parent = rst_data->dev;
411 adev->dev.release = npcm_clock_adev_release;
414 ret = auxiliary_device_init(adev);
423 static int npcm8xx_clock_controller_register(struct npcm_rc_data *rst_data, char *clk_name)
425 struct auxiliary_device *adev;
428 adev = npcm_clock_adev_alloc(rst_data, clk_name);
430 return PTR_ERR(adev);
432 ret = auxiliary_device_add(adev);
434 auxiliary_device_uninit(adev);
438 return devm_add_action_or_reset(rst_data->dev, npcm_clock_unregister_adev, adev);
441 static int npcm_rc_probe(struct platform_device *pdev)
443 struct npcm_rc_data *rc;
446 rc = devm_kzalloc(&pdev->dev, sizeof(*rc), GFP_KERNEL);
450 rc->base = devm_platform_ioremap_resource(pdev, 0);
451 if (IS_ERR(rc->base))
452 return PTR_ERR(rc->base);
454 spin_lock_init(&rc->lock);
456 rc->rcdev.owner = THIS_MODULE;
457 rc->rcdev.ops = &npcm_rc_ops;
458 rc->rcdev.of_node = pdev->dev.of_node;
459 rc->rcdev.of_reset_n_cells = 2;
460 rc->rcdev.of_xlate = npcm_reset_xlate;
461 rc->dev = &pdev->dev;
463 ret = devm_reset_controller_register(&pdev->dev, &rc->rcdev);
465 dev_err(&pdev->dev, "unable to register device\n");
469 if (npcm_usb_reset(pdev, rc))
470 dev_warn(&pdev->dev, "NPCM USB reset failed, can cause issues with UDC and USB host\n");
472 if (!of_property_read_u32(pdev->dev.of_node, "nuvoton,sw-reset-number",
473 &rc->sw_reset_number)) {
474 if (rc->sw_reset_number && rc->sw_reset_number < 5) {
475 rc->restart_nb.priority = 192;
476 rc->restart_nb.notifier_call = npcm_rc_restart;
477 ret = register_restart_handler(&rc->restart_nb);
479 dev_warn(&pdev->dev, "failed to register restart handler\n");
485 switch (rc->info->bmc_id) {
487 return npcm8xx_clock_controller_register(rc, "clk-npcm8xx");
493 static struct platform_driver npcm_rc_driver = {
494 .probe = npcm_rc_probe,
496 .name = "npcm-reset",
497 .of_match_table = npcm_rc_match,
498 .suppress_bind_attrs = true,
501 builtin_platform_driver(npcm_rc_driver);