18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Hisilicon Reset Controller Driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2015-2016 HiSilicon Technologies Co., Ltd. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/io.h> 98c2ecf20Sopenharmony_ci#include <linux/of_address.h> 108c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 118c2ecf20Sopenharmony_ci#include <linux/reset-controller.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 148c2ecf20Sopenharmony_ci#include "reset.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#define HISI_RESET_BIT_MASK 0x1f 178c2ecf20Sopenharmony_ci#define HISI_RESET_OFFSET_SHIFT 8 188c2ecf20Sopenharmony_ci#define HISI_RESET_OFFSET_MASK 0xffff00 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistruct hisi_reset_controller { 218c2ecf20Sopenharmony_ci spinlock_t lock; 228c2ecf20Sopenharmony_ci void __iomem *membase; 238c2ecf20Sopenharmony_ci struct reset_controller_dev rcdev; 248c2ecf20Sopenharmony_ci}; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define to_hisi_reset_controller(rcdev) \ 288c2ecf20Sopenharmony_ci container_of(rcdev, struct hisi_reset_controller, rcdev) 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic int hisi_reset_of_xlate(struct reset_controller_dev *rcdev, 318c2ecf20Sopenharmony_ci const struct of_phandle_args *reset_spec) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci u32 offset; 348c2ecf20Sopenharmony_ci u8 bit; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci offset = (reset_spec->args[0] << HISI_RESET_OFFSET_SHIFT) 378c2ecf20Sopenharmony_ci & HISI_RESET_OFFSET_MASK; 388c2ecf20Sopenharmony_ci bit = reset_spec->args[1] & HISI_RESET_BIT_MASK; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci return (offset | bit); 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic int hisi_reset_assert(struct reset_controller_dev *rcdev, 448c2ecf20Sopenharmony_ci unsigned long id) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci struct hisi_reset_controller *rstc = to_hisi_reset_controller(rcdev); 478c2ecf20Sopenharmony_ci unsigned long flags; 488c2ecf20Sopenharmony_ci u32 offset, reg; 498c2ecf20Sopenharmony_ci u8 bit; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci offset = (id & HISI_RESET_OFFSET_MASK) >> HISI_RESET_OFFSET_SHIFT; 528c2ecf20Sopenharmony_ci bit = id & HISI_RESET_BIT_MASK; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci spin_lock_irqsave(&rstc->lock, flags); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci reg = readl(rstc->membase + offset); 578c2ecf20Sopenharmony_ci writel(reg | BIT(bit), rstc->membase + offset); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rstc->lock, flags); 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci return 0; 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic int hisi_reset_deassert(struct reset_controller_dev *rcdev, 658c2ecf20Sopenharmony_ci unsigned long id) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci struct hisi_reset_controller *rstc = to_hisi_reset_controller(rcdev); 688c2ecf20Sopenharmony_ci unsigned long flags; 698c2ecf20Sopenharmony_ci u32 offset, reg; 708c2ecf20Sopenharmony_ci u8 bit; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci offset = (id & HISI_RESET_OFFSET_MASK) >> HISI_RESET_OFFSET_SHIFT; 738c2ecf20Sopenharmony_ci bit = id & HISI_RESET_BIT_MASK; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci spin_lock_irqsave(&rstc->lock, flags); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci reg = readl(rstc->membase + offset); 788c2ecf20Sopenharmony_ci writel(reg & ~BIT(bit), rstc->membase + offset); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rstc->lock, flags); 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci return 0; 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistatic const struct reset_control_ops hisi_reset_ops = { 868c2ecf20Sopenharmony_ci .assert = hisi_reset_assert, 878c2ecf20Sopenharmony_ci .deassert = hisi_reset_deassert, 888c2ecf20Sopenharmony_ci}; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistruct hisi_reset_controller *hisi_reset_init(struct platform_device *pdev) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci struct hisi_reset_controller *rstc; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci rstc = devm_kmalloc(&pdev->dev, sizeof(*rstc), GFP_KERNEL); 958c2ecf20Sopenharmony_ci if (!rstc) 968c2ecf20Sopenharmony_ci return NULL; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci rstc->membase = devm_platform_ioremap_resource(pdev, 0); 998c2ecf20Sopenharmony_ci if (IS_ERR(rstc->membase)) 1008c2ecf20Sopenharmony_ci return NULL; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci spin_lock_init(&rstc->lock); 1038c2ecf20Sopenharmony_ci rstc->rcdev.owner = THIS_MODULE; 1048c2ecf20Sopenharmony_ci rstc->rcdev.ops = &hisi_reset_ops; 1058c2ecf20Sopenharmony_ci rstc->rcdev.of_node = pdev->dev.of_node; 1068c2ecf20Sopenharmony_ci rstc->rcdev.of_reset_n_cells = 2; 1078c2ecf20Sopenharmony_ci rstc->rcdev.of_xlate = hisi_reset_of_xlate; 1088c2ecf20Sopenharmony_ci reset_controller_register(&rstc->rcdev); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci return rstc; 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hisi_reset_init); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_civoid hisi_reset_exit(struct hisi_reset_controller *rstc) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci reset_controller_unregister(&rstc->rcdev); 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hisi_reset_exit); 119