1 // SPDX-License-Identifier: GPL-2.0-only
3 * PolarFire SoC (MPFS) Peripheral Clock Reset Controller
6 * Copyright (c) 2022 Microchip Technology Inc. and its subsidiaries.
9 #include <linux/auxiliary_bus.h>
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/reset-controller.h>
14 #include <dt-bindings/clock/microchip,mpfs-clock.h>
15 #include <soc/microchip/mpfs.h>
18 * The ENVM reset is the lowest bit in the register & I am using the CLK_FOO
19 * defines in the dt to make things easier to configure - so this is accounting
20 * for the offset of 3 there.
22 #define MPFS_PERIPH_OFFSET CLK_ENVM
23 #define MPFS_NUM_RESETS 30u
24 #define MPFS_SLEEP_MIN_US 100
25 #define MPFS_SLEEP_MAX_US 200
27 /* block concurrent access to the soft reset register */
28 static DEFINE_SPINLOCK(mpfs_reset_lock);
31 * Peripheral clock resets
34 static int mpfs_assert(struct reset_controller_dev *rcdev, unsigned long id)
39 spin_lock_irqsave(&mpfs_reset_lock, flags);
41 reg = mpfs_reset_read(rcdev->dev);
43 mpfs_reset_write(rcdev->dev, reg);
45 spin_unlock_irqrestore(&mpfs_reset_lock, flags);
50 static int mpfs_deassert(struct reset_controller_dev *rcdev, unsigned long id)
55 spin_lock_irqsave(&mpfs_reset_lock, flags);
57 reg = mpfs_reset_read(rcdev->dev);
59 mpfs_reset_write(rcdev->dev, reg);
61 spin_unlock_irqrestore(&mpfs_reset_lock, flags);
66 static int mpfs_status(struct reset_controller_dev *rcdev, unsigned long id)
68 u32 reg = mpfs_reset_read(rcdev->dev);
71 * It is safe to return here as MPFS_NUM_RESETS makes sure the sign bit
74 return (reg & BIT(id));
77 static int mpfs_reset(struct reset_controller_dev *rcdev, unsigned long id)
79 mpfs_assert(rcdev, id);
81 usleep_range(MPFS_SLEEP_MIN_US, MPFS_SLEEP_MAX_US);
83 mpfs_deassert(rcdev, id);
88 static const struct reset_control_ops mpfs_reset_ops = {
90 .assert = mpfs_assert,
91 .deassert = mpfs_deassert,
92 .status = mpfs_status,
95 static int mpfs_reset_xlate(struct reset_controller_dev *rcdev,
96 const struct of_phandle_args *reset_spec)
98 unsigned int index = reset_spec->args[0];
101 * CLK_RESERVED does not map to a clock, but it does map to a reset,
102 * so it has to be accounted for here. It is the reset for the fabric,
103 * so if this reset gets called - do not reset it.
105 if (index == CLK_RESERVED) {
106 dev_err(rcdev->dev, "Resetting the fabric is not supported\n");
110 if (index < MPFS_PERIPH_OFFSET || index >= (MPFS_PERIPH_OFFSET + rcdev->nr_resets)) {
111 dev_err(rcdev->dev, "Invalid reset index %u\n", index);
115 return index - MPFS_PERIPH_OFFSET;
118 static int mpfs_reset_probe(struct auxiliary_device *adev,
119 const struct auxiliary_device_id *id)
121 struct device *dev = &adev->dev;
122 struct reset_controller_dev *rcdev;
124 rcdev = devm_kzalloc(dev, sizeof(*rcdev), GFP_KERNEL);
129 rcdev->dev->parent = dev->parent;
130 rcdev->ops = &mpfs_reset_ops;
131 rcdev->of_node = dev->parent->of_node;
132 rcdev->of_reset_n_cells = 1;
133 rcdev->of_xlate = mpfs_reset_xlate;
134 rcdev->nr_resets = MPFS_NUM_RESETS;
136 return devm_reset_controller_register(dev, rcdev);
139 static const struct auxiliary_device_id mpfs_reset_ids[] = {
141 .name = "clk_mpfs.reset-mpfs",
145 MODULE_DEVICE_TABLE(auxiliary, mpfs_reset_ids);
147 static struct auxiliary_driver mpfs_reset_driver = {
148 .probe = mpfs_reset_probe,
149 .id_table = mpfs_reset_ids,
152 module_auxiliary_driver(mpfs_reset_driver);
154 MODULE_DESCRIPTION("Microchip PolarFire SoC Reset Driver");
156 MODULE_IMPORT_NS(MCHP_CLK_MPFS);