xref: /kernel/linux/linux-5.10/drivers/clk/mmp/reset.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/slab.h>
38c2ecf20Sopenharmony_ci#include <linux/io.h>
48c2ecf20Sopenharmony_ci#include <linux/of.h>
58c2ecf20Sopenharmony_ci#include <linux/of_address.h>
68c2ecf20Sopenharmony_ci#include <linux/reset-controller.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "reset.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define rcdev_to_unit(rcdev) container_of(rcdev, struct mmp_clk_reset_unit, rcdev)
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic int mmp_of_reset_xlate(struct reset_controller_dev *rcdev,
138c2ecf20Sopenharmony_ci			  const struct of_phandle_args *reset_spec)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	struct mmp_clk_reset_unit *unit = rcdev_to_unit(rcdev);
168c2ecf20Sopenharmony_ci	struct mmp_clk_reset_cell *cell;
178c2ecf20Sopenharmony_ci	int i;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
208c2ecf20Sopenharmony_ci		return -EINVAL;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	for (i = 0; i < rcdev->nr_resets; i++) {
238c2ecf20Sopenharmony_ci		cell = &unit->cells[i];
248c2ecf20Sopenharmony_ci		if (cell->clk_id == reset_spec->args[0])
258c2ecf20Sopenharmony_ci			break;
268c2ecf20Sopenharmony_ci	}
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	if (i == rcdev->nr_resets)
298c2ecf20Sopenharmony_ci		return -EINVAL;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	return i;
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic int mmp_clk_reset_assert(struct reset_controller_dev *rcdev,
358c2ecf20Sopenharmony_ci				unsigned long id)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	struct mmp_clk_reset_unit *unit = rcdev_to_unit(rcdev);
388c2ecf20Sopenharmony_ci	struct mmp_clk_reset_cell *cell;
398c2ecf20Sopenharmony_ci	unsigned long flags = 0;
408c2ecf20Sopenharmony_ci	u32 val;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	cell = &unit->cells[id];
438c2ecf20Sopenharmony_ci	if (cell->lock)
448c2ecf20Sopenharmony_ci		spin_lock_irqsave(cell->lock, flags);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	val = readl(cell->reg);
478c2ecf20Sopenharmony_ci	val |= cell->bits;
488c2ecf20Sopenharmony_ci	writel(val, cell->reg);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if (cell->lock)
518c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(cell->lock, flags);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return 0;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int mmp_clk_reset_deassert(struct reset_controller_dev *rcdev,
578c2ecf20Sopenharmony_ci				unsigned long id)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct mmp_clk_reset_unit *unit = rcdev_to_unit(rcdev);
608c2ecf20Sopenharmony_ci	struct mmp_clk_reset_cell *cell;
618c2ecf20Sopenharmony_ci	unsigned long flags = 0;
628c2ecf20Sopenharmony_ci	u32 val;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	cell = &unit->cells[id];
658c2ecf20Sopenharmony_ci	if (cell->lock)
668c2ecf20Sopenharmony_ci		spin_lock_irqsave(cell->lock, flags);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	val = readl(cell->reg);
698c2ecf20Sopenharmony_ci	val &= ~cell->bits;
708c2ecf20Sopenharmony_ci	writel(val, cell->reg);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (cell->lock)
738c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(cell->lock, flags);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	return 0;
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic const struct reset_control_ops mmp_clk_reset_ops = {
798c2ecf20Sopenharmony_ci	.assert		= mmp_clk_reset_assert,
808c2ecf20Sopenharmony_ci	.deassert	= mmp_clk_reset_deassert,
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_civoid mmp_clk_reset_register(struct device_node *np,
848c2ecf20Sopenharmony_ci			struct mmp_clk_reset_cell *cells, int nr_resets)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	struct mmp_clk_reset_unit *unit;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	unit = kzalloc(sizeof(*unit), GFP_KERNEL);
898c2ecf20Sopenharmony_ci	if (!unit)
908c2ecf20Sopenharmony_ci		return;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	unit->cells = cells;
938c2ecf20Sopenharmony_ci	unit->rcdev.of_reset_n_cells = 1;
948c2ecf20Sopenharmony_ci	unit->rcdev.nr_resets = nr_resets;
958c2ecf20Sopenharmony_ci	unit->rcdev.ops = &mmp_clk_reset_ops;
968c2ecf20Sopenharmony_ci	unit->rcdev.of_node = np;
978c2ecf20Sopenharmony_ci	unit->rcdev.of_xlate = mmp_of_reset_xlate;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	reset_controller_register(&unit->rcdev);
1008c2ecf20Sopenharmony_ci}
101