xref: /linux/drivers/power/reset/ocelot-reset.c (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
16ab739bcSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
26ab739bcSAlexandre Belloni /*
36ab739bcSAlexandre Belloni  * Microsemi MIPS SoC reset driver
46ab739bcSAlexandre Belloni  *
56ab739bcSAlexandre Belloni  * License: Dual MIT/GPL
66ab739bcSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
76ab739bcSAlexandre Belloni  */
86ab739bcSAlexandre Belloni #include <linux/delay.h>
96ab739bcSAlexandre Belloni #include <linux/io.h>
106ab739bcSAlexandre Belloni #include <linux/notifier.h>
112ce8284cSRob Herring #include <linux/mod_devicetable.h>
126ab739bcSAlexandre Belloni #include <linux/mfd/syscon.h>
136ab739bcSAlexandre Belloni #include <linux/platform_device.h>
142ce8284cSRob Herring #include <linux/property.h>
156ab739bcSAlexandre Belloni #include <linux/reboot.h>
166ab739bcSAlexandre Belloni #include <linux/regmap.h>
176ab739bcSAlexandre Belloni 
18ec871696SLars Povlsen struct reset_props {
19ec871696SLars Povlsen 	const char *syscon;
20ec871696SLars Povlsen 	u32 protect_reg;
21ec871696SLars Povlsen 	u32 vcore_protect;
22ec871696SLars Povlsen 	u32 if_si_owner_bit;
23ec871696SLars Povlsen };
24ec871696SLars Povlsen 
256ab739bcSAlexandre Belloni struct ocelot_reset_context {
266ab739bcSAlexandre Belloni 	void __iomem *base;
276ab739bcSAlexandre Belloni 	struct regmap *cpu_ctrl;
28ec871696SLars Povlsen 	const struct reset_props *props;
296ab739bcSAlexandre Belloni 	struct notifier_block restart_handler;
306ab739bcSAlexandre Belloni };
316ab739bcSAlexandre Belloni 
32aa4302c4SGregory CLEMENT #define BIT_OFF_INVALID				32
33aa4302c4SGregory CLEMENT 
346ab739bcSAlexandre Belloni #define SOFT_CHIP_RST BIT(0)
356ab739bcSAlexandre Belloni 
369afe6250SAlexandre Belloni #define ICPU_CFG_CPU_SYSTEM_CTRL_GENERAL_CTRL	0x24
379afe6250SAlexandre Belloni #define IF_SI_OWNER_MASK			GENMASK(1, 0)
389afe6250SAlexandre Belloni #define IF_SI_OWNER_SISL			0
399afe6250SAlexandre Belloni #define IF_SI_OWNER_SIBM			1
409afe6250SAlexandre Belloni #define IF_SI_OWNER_SIMC			2
419afe6250SAlexandre Belloni 
ocelot_restart_handle(struct notifier_block * this,unsigned long mode,void * cmd)426ab739bcSAlexandre Belloni static int ocelot_restart_handle(struct notifier_block *this,
436ab739bcSAlexandre Belloni 				 unsigned long mode, void *cmd)
446ab739bcSAlexandre Belloni {
456ab739bcSAlexandre Belloni 	struct ocelot_reset_context *ctx = container_of(this, struct
466ab739bcSAlexandre Belloni 							ocelot_reset_context,
476ab739bcSAlexandre Belloni 							restart_handler);
48ec871696SLars Povlsen 	u32 if_si_owner_bit = ctx->props->if_si_owner_bit;
496ab739bcSAlexandre Belloni 
506ab739bcSAlexandre Belloni 	/* Make sure the core is not protected from reset */
51ec871696SLars Povlsen 	regmap_update_bits(ctx->cpu_ctrl, ctx->props->protect_reg,
52ec871696SLars Povlsen 			   ctx->props->vcore_protect, 0);
536ab739bcSAlexandre Belloni 
549afe6250SAlexandre Belloni 	/* Make the SI back to boot mode */
55aa4302c4SGregory CLEMENT 	if (if_si_owner_bit != BIT_OFF_INVALID)
56aa4302c4SGregory CLEMENT 		regmap_update_bits(ctx->cpu_ctrl,
57aa4302c4SGregory CLEMENT 				   ICPU_CFG_CPU_SYSTEM_CTRL_GENERAL_CTRL,
58ec871696SLars Povlsen 				   IF_SI_OWNER_MASK << if_si_owner_bit,
59ec871696SLars Povlsen 				   IF_SI_OWNER_SIBM << if_si_owner_bit);
60ec871696SLars Povlsen 
61ec871696SLars Povlsen 	pr_emerg("Resetting SoC\n");
629afe6250SAlexandre Belloni 
636ab739bcSAlexandre Belloni 	writel(SOFT_CHIP_RST, ctx->base);
646ab739bcSAlexandre Belloni 
656ab739bcSAlexandre Belloni 	pr_emerg("Unable to restart system\n");
666ab739bcSAlexandre Belloni 	return NOTIFY_DONE;
676ab739bcSAlexandre Belloni }
686ab739bcSAlexandre Belloni 
ocelot_reset_probe(struct platform_device * pdev)696ab739bcSAlexandre Belloni static int ocelot_reset_probe(struct platform_device *pdev)
706ab739bcSAlexandre Belloni {
716ab739bcSAlexandre Belloni 	struct ocelot_reset_context *ctx;
726ab739bcSAlexandre Belloni 	struct device *dev = &pdev->dev;
736ab739bcSAlexandre Belloni 	int err;
746ab739bcSAlexandre Belloni 
756ab739bcSAlexandre Belloni 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
766ab739bcSAlexandre Belloni 	if (!ctx)
776ab739bcSAlexandre Belloni 		return -ENOMEM;
786ab739bcSAlexandre Belloni 
79*1a805883SYangtao Li 	ctx->base = devm_platform_ioremap_resource(pdev, 0);
806ab739bcSAlexandre Belloni 	if (IS_ERR(ctx->base))
816ab739bcSAlexandre Belloni 		return PTR_ERR(ctx->base);
826ab739bcSAlexandre Belloni 
83ec871696SLars Povlsen 	ctx->props = device_get_match_data(dev);
84ec871696SLars Povlsen 
85ec871696SLars Povlsen 	ctx->cpu_ctrl = syscon_regmap_lookup_by_compatible(ctx->props->syscon);
86ec871696SLars Povlsen 	if (IS_ERR(ctx->cpu_ctrl)) {
87ec871696SLars Povlsen 		dev_err(dev, "No syscon map: %s\n", ctx->props->syscon);
886ab739bcSAlexandre Belloni 		return PTR_ERR(ctx->cpu_ctrl);
89ec871696SLars Povlsen 	}
906ab739bcSAlexandre Belloni 
916ab739bcSAlexandre Belloni 	ctx->restart_handler.notifier_call = ocelot_restart_handle;
926ab739bcSAlexandre Belloni 	ctx->restart_handler.priority = 192;
936ab739bcSAlexandre Belloni 	err = register_restart_handler(&ctx->restart_handler);
946ab739bcSAlexandre Belloni 	if (err)
956ab739bcSAlexandre Belloni 		dev_err(dev, "can't register restart notifier (err=%d)\n", err);
966ab739bcSAlexandre Belloni 
976ab739bcSAlexandre Belloni 	return err;
986ab739bcSAlexandre Belloni }
996ab739bcSAlexandre Belloni 
100aa4302c4SGregory CLEMENT static const struct reset_props reset_props_jaguar2 = {
101aa4302c4SGregory CLEMENT 	.syscon		 = "mscc,ocelot-cpu-syscon",
102aa4302c4SGregory CLEMENT 	.protect_reg     = 0x20,
103aa4302c4SGregory CLEMENT 	.vcore_protect   = BIT(2),
104aa4302c4SGregory CLEMENT 	.if_si_owner_bit = 6,
105aa4302c4SGregory CLEMENT };
106aa4302c4SGregory CLEMENT 
107aa4302c4SGregory CLEMENT static const struct reset_props reset_props_luton = {
108aa4302c4SGregory CLEMENT 	.syscon		 = "mscc,ocelot-cpu-syscon",
109aa4302c4SGregory CLEMENT 	.protect_reg     = 0x20,
110aa4302c4SGregory CLEMENT 	.vcore_protect   = BIT(2),
111aa4302c4SGregory CLEMENT 	.if_si_owner_bit = BIT_OFF_INVALID, /* n/a */
112aa4302c4SGregory CLEMENT };
113aa4302c4SGregory CLEMENT 
114ec871696SLars Povlsen static const struct reset_props reset_props_ocelot = {
115ec871696SLars Povlsen 	.syscon		 = "mscc,ocelot-cpu-syscon",
116ec871696SLars Povlsen 	.protect_reg     = 0x20,
117ec871696SLars Povlsen 	.vcore_protect   = BIT(2),
118ec871696SLars Povlsen 	.if_si_owner_bit = 4,
119ec871696SLars Povlsen };
120ec871696SLars Povlsen 
121ec871696SLars Povlsen static const struct reset_props reset_props_sparx5 = {
122ec871696SLars Povlsen 	.syscon		 = "microchip,sparx5-cpu-syscon",
123ec871696SLars Povlsen 	.protect_reg     = 0x84,
124ec871696SLars Povlsen 	.vcore_protect   = BIT(10),
125ec871696SLars Povlsen 	.if_si_owner_bit = 6,
126ec871696SLars Povlsen };
127ec871696SLars Povlsen 
1286ab739bcSAlexandre Belloni static const struct of_device_id ocelot_reset_of_match[] = {
129ec871696SLars Povlsen 	{
130aa4302c4SGregory CLEMENT 		.compatible = "mscc,jaguar2-chip-reset",
131aa4302c4SGregory CLEMENT 		.data = &reset_props_jaguar2
132aa4302c4SGregory CLEMENT 	}, {
133aa4302c4SGregory CLEMENT 		.compatible = "mscc,luton-chip-reset",
134aa4302c4SGregory CLEMENT 		.data = &reset_props_luton
135aa4302c4SGregory CLEMENT 	}, {
136ec871696SLars Povlsen 		.compatible = "mscc,ocelot-chip-reset",
137ec871696SLars Povlsen 		.data = &reset_props_ocelot
138ec871696SLars Povlsen 	}, {
139ec871696SLars Povlsen 		.compatible = "microchip,sparx5-chip-reset",
140ec871696SLars Povlsen 		.data = &reset_props_sparx5
141ec871696SLars Povlsen 	},
142ec871696SLars Povlsen 	{ /*sentinel*/ }
1436ab739bcSAlexandre Belloni };
1446ab739bcSAlexandre Belloni 
1456ab739bcSAlexandre Belloni static struct platform_driver ocelot_reset_driver = {
1466ab739bcSAlexandre Belloni 	.probe = ocelot_reset_probe,
1476ab739bcSAlexandre Belloni 	.driver = {
1486ab739bcSAlexandre Belloni 		.name = "ocelot-chip-reset",
1496ab739bcSAlexandre Belloni 		.of_match_table = ocelot_reset_of_match,
1506ab739bcSAlexandre Belloni 	},
1516ab739bcSAlexandre Belloni };
1526ab739bcSAlexandre Belloni builtin_platform_driver(ocelot_reset_driver);
153