1 // SPDX-License-Identifier: GPL-2.0+
2 /* Microchip Sparx5 Switch Reset driver
4 * Copyright (c) 2020 Microchip Technology Inc. and its subsidiaries.
6 * The Sparx5 Chip Register Model can be browsed at this location:
7 * https://github.com/microchip-ung/sparx-5_reginfo
9 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/property.h>
14 #include <linux/regmap.h>
15 #include <linux/reset-controller.h>
24 struct mchp_reset_context {
25 struct regmap *cpu_ctrl;
26 struct regmap *gcb_ctrl;
27 struct reset_controller_dev rcdev;
28 const struct reset_props *props;
31 static struct regmap_config sparx5_reset_regmap_config = {
37 static int sparx5_switch_reset(struct mchp_reset_context *ctx)
41 /* Make sure the core is PROTECTED from reset */
42 regmap_update_bits(ctx->cpu_ctrl, ctx->props->protect_reg,
43 ctx->props->protect_bit, ctx->props->protect_bit);
45 /* Start soft reset */
46 regmap_write(ctx->gcb_ctrl, ctx->props->reset_reg,
47 ctx->props->reset_bit);
49 /* Wait for soft reset done */
50 return regmap_read_poll_timeout(ctx->gcb_ctrl, ctx->props->reset_reg, val,
51 (val & ctx->props->reset_bit) == 0,
55 static int sparx5_reset_noop(struct reset_controller_dev *rcdev,
61 static const struct reset_control_ops sparx5_reset_ops = {
62 .reset = sparx5_reset_noop,
65 static int mchp_sparx5_map_syscon(struct platform_device *pdev, char *name,
66 struct regmap **target)
68 struct device_node *syscon_np;
69 struct regmap *regmap;
72 syscon_np = of_parse_phandle(pdev->dev.of_node, name, 0);
75 regmap = syscon_node_to_regmap(syscon_np);
76 of_node_put(syscon_np);
78 err = PTR_ERR(regmap);
79 dev_err(&pdev->dev, "No '%s' map: %d\n", name, err);
86 static int mchp_sparx5_map_io(struct platform_device *pdev, int index,
87 struct regmap **target)
93 mem = devm_platform_get_and_ioremap_resource(pdev, index, &res);
95 dev_err(&pdev->dev, "Could not map resource %d\n", index);
98 sparx5_reset_regmap_config.name = res->name;
99 map = devm_regmap_init_mmio(&pdev->dev, mem, &sparx5_reset_regmap_config);
106 static int mchp_sparx5_reset_probe(struct platform_device *pdev)
108 struct device_node *dn = pdev->dev.of_node;
109 struct mchp_reset_context *ctx;
112 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
116 err = mchp_sparx5_map_syscon(pdev, "cpu-syscon", &ctx->cpu_ctrl);
119 err = mchp_sparx5_map_io(pdev, 0, &ctx->gcb_ctrl);
123 ctx->rcdev.owner = THIS_MODULE;
124 ctx->rcdev.nr_resets = 1;
125 ctx->rcdev.ops = &sparx5_reset_ops;
126 ctx->rcdev.of_node = dn;
127 ctx->props = device_get_match_data(&pdev->dev);
129 /* Issue the reset very early, our actual reset callback is a noop. */
130 err = sparx5_switch_reset(ctx);
134 return devm_reset_controller_register(&pdev->dev, &ctx->rcdev);
137 static const struct reset_props reset_props_sparx5 = {
139 .protect_bit = BIT(10),
144 static const struct reset_props reset_props_lan966x = {
146 .protect_bit = BIT(5),
151 static const struct of_device_id mchp_sparx5_reset_of_match[] = {
153 .compatible = "microchip,sparx5-switch-reset",
154 .data = &reset_props_sparx5,
156 .compatible = "microchip,lan966x-switch-reset",
157 .data = &reset_props_lan966x,
162 static struct platform_driver mchp_sparx5_reset_driver = {
163 .probe = mchp_sparx5_reset_probe,
165 .name = "sparx5-switch-reset",
166 .of_match_table = mchp_sparx5_reset_of_match,
170 static int __init mchp_sparx5_reset_init(void)
172 return platform_driver_register(&mchp_sparx5_reset_driver);
176 * Because this is a global reset, keep this postcore_initcall() to issue the
177 * reset as early as possible during the kernel startup.
179 postcore_initcall(mchp_sparx5_reset_init);
181 MODULE_DESCRIPTION("Microchip Sparx5 switch reset driver");
182 MODULE_AUTHOR("Steen Hegelund <steen.hegelund@microchip.com>");