xref: /linux/drivers/reset/reset-meson.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
17c61ccf6SNeil Armstrong // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2c7224dc3SNeil Armstrong /*
38290924eSPaul Gortmaker  * Amlogic Meson Reset Controller driver
48290924eSPaul Gortmaker  *
5c7224dc3SNeil Armstrong  * Copyright (c) 2016 BayLibre, SAS.
6c7224dc3SNeil Armstrong  * Author: Neil Armstrong <narmstrong@baylibre.com>
7c7224dc3SNeil Armstrong  */
8c7224dc3SNeil Armstrong #include <linux/err.h>
98290924eSPaul Gortmaker #include <linux/init.h>
10c7224dc3SNeil Armstrong #include <linux/io.h>
11c7224dc3SNeil Armstrong #include <linux/of.h>
123bfe8933SNeil Armstrong #include <linux/module.h>
13c7224dc3SNeil Armstrong #include <linux/platform_device.h>
14c7224dc3SNeil Armstrong #include <linux/reset-controller.h>
15c7224dc3SNeil Armstrong #include <linux/slab.h>
16c7224dc3SNeil Armstrong #include <linux/types.h>
17c7224dc3SNeil Armstrong 
18c7224dc3SNeil Armstrong #define BITS_PER_REG	32
19bdb369e1SXingyu Chen 
20bdb369e1SXingyu Chen struct meson_reset_param {
21bdb369e1SXingyu Chen 	int reg_count;
22bdb369e1SXingyu Chen 	int level_offset;
23bdb369e1SXingyu Chen };
24c7224dc3SNeil Armstrong 
25c7224dc3SNeil Armstrong struct meson_reset {
26c7224dc3SNeil Armstrong 	void __iomem *reg_base;
27bdb369e1SXingyu Chen 	const struct meson_reset_param *param;
28c7224dc3SNeil Armstrong 	struct reset_controller_dev rcdev;
29a5a10afeSNeil Armstrong 	spinlock_t lock;
30c7224dc3SNeil Armstrong };
31c7224dc3SNeil Armstrong 
meson_reset_reset(struct reset_controller_dev * rcdev,unsigned long id)32c7224dc3SNeil Armstrong static int meson_reset_reset(struct reset_controller_dev *rcdev,
33c7224dc3SNeil Armstrong 			      unsigned long id)
34c7224dc3SNeil Armstrong {
35c7224dc3SNeil Armstrong 	struct meson_reset *data =
36c7224dc3SNeil Armstrong 		container_of(rcdev, struct meson_reset, rcdev);
37c7224dc3SNeil Armstrong 	unsigned int bank = id / BITS_PER_REG;
38c7224dc3SNeil Armstrong 	unsigned int offset = id % BITS_PER_REG;
39c7224dc3SNeil Armstrong 	void __iomem *reg_addr = data->reg_base + (bank << 2);
40c7224dc3SNeil Armstrong 
41c7224dc3SNeil Armstrong 	writel(BIT(offset), reg_addr);
42c7224dc3SNeil Armstrong 
43c7224dc3SNeil Armstrong 	return 0;
44c7224dc3SNeil Armstrong }
45c7224dc3SNeil Armstrong 
meson_reset_level(struct reset_controller_dev * rcdev,unsigned long id,bool assert)46a5a10afeSNeil Armstrong static int meson_reset_level(struct reset_controller_dev *rcdev,
47a5a10afeSNeil Armstrong 			    unsigned long id, bool assert)
48a5a10afeSNeil Armstrong {
49a5a10afeSNeil Armstrong 	struct meson_reset *data =
50a5a10afeSNeil Armstrong 		container_of(rcdev, struct meson_reset, rcdev);
51a5a10afeSNeil Armstrong 	unsigned int bank = id / BITS_PER_REG;
52a5a10afeSNeil Armstrong 	unsigned int offset = id % BITS_PER_REG;
53bdb369e1SXingyu Chen 	void __iomem *reg_addr;
54a5a10afeSNeil Armstrong 	unsigned long flags;
55a5a10afeSNeil Armstrong 	u32 reg;
56a5a10afeSNeil Armstrong 
57bdb369e1SXingyu Chen 	reg_addr = data->reg_base + data->param->level_offset + (bank << 2);
58bdb369e1SXingyu Chen 
59a5a10afeSNeil Armstrong 	spin_lock_irqsave(&data->lock, flags);
60a5a10afeSNeil Armstrong 
61a5a10afeSNeil Armstrong 	reg = readl(reg_addr);
62a5a10afeSNeil Armstrong 	if (assert)
63a5a10afeSNeil Armstrong 		writel(reg & ~BIT(offset), reg_addr);
64a5a10afeSNeil Armstrong 	else
65a5a10afeSNeil Armstrong 		writel(reg | BIT(offset), reg_addr);
66a5a10afeSNeil Armstrong 
67a5a10afeSNeil Armstrong 	spin_unlock_irqrestore(&data->lock, flags);
68a5a10afeSNeil Armstrong 
69a5a10afeSNeil Armstrong 	return 0;
70a5a10afeSNeil Armstrong }
71a5a10afeSNeil Armstrong 
meson_reset_assert(struct reset_controller_dev * rcdev,unsigned long id)72a5a10afeSNeil Armstrong static int meson_reset_assert(struct reset_controller_dev *rcdev,
73a5a10afeSNeil Armstrong 			      unsigned long id)
74a5a10afeSNeil Armstrong {
75a5a10afeSNeil Armstrong 	return meson_reset_level(rcdev, id, true);
76a5a10afeSNeil Armstrong }
77a5a10afeSNeil Armstrong 
meson_reset_deassert(struct reset_controller_dev * rcdev,unsigned long id)78a5a10afeSNeil Armstrong static int meson_reset_deassert(struct reset_controller_dev *rcdev,
79a5a10afeSNeil Armstrong 				unsigned long id)
80a5a10afeSNeil Armstrong {
81a5a10afeSNeil Armstrong 	return meson_reset_level(rcdev, id, false);
82a5a10afeSNeil Armstrong }
83a5a10afeSNeil Armstrong 
84320da785SMartin Blumenstingl static const struct reset_control_ops meson_reset_ops = {
85a5a10afeSNeil Armstrong 	.reset		= meson_reset_reset,
86a5a10afeSNeil Armstrong 	.assert		= meson_reset_assert,
87a5a10afeSNeil Armstrong 	.deassert	= meson_reset_deassert,
88a5a10afeSNeil Armstrong };
89a5a10afeSNeil Armstrong 
90bdb369e1SXingyu Chen static const struct meson_reset_param meson8b_param = {
91bdb369e1SXingyu Chen 	.reg_count	= 8,
92bdb369e1SXingyu Chen 	.level_offset	= 0x7c,
93bdb369e1SXingyu Chen };
94bdb369e1SXingyu Chen 
95bdb369e1SXingyu Chen static const struct meson_reset_param meson_a1_param = {
96bdb369e1SXingyu Chen 	.reg_count	= 3,
97bdb369e1SXingyu Chen 	.level_offset	= 0x40,
98bdb369e1SXingyu Chen };
99bdb369e1SXingyu Chen 
100636728d0SZelong Dong static const struct meson_reset_param meson_s4_param = {
101636728d0SZelong Dong 	.reg_count	= 6,
102636728d0SZelong Dong 	.level_offset	= 0x40,
103636728d0SZelong Dong };
104636728d0SZelong Dong 
105*b062ef29SZelong Dong static const struct meson_reset_param t7_param = {
106*b062ef29SZelong Dong 	.reg_count      = 7,
107*b062ef29SZelong Dong 	.level_offset   = 0x40,
108*b062ef29SZelong Dong };
109*b062ef29SZelong Dong 
110c7224dc3SNeil Armstrong static const struct of_device_id meson_reset_dt_ids[] = {
111bdb369e1SXingyu Chen 	 { .compatible = "amlogic,meson8b-reset",    .data = &meson8b_param},
112bdb369e1SXingyu Chen 	 { .compatible = "amlogic,meson-gxbb-reset", .data = &meson8b_param},
113bdb369e1SXingyu Chen 	 { .compatible = "amlogic,meson-axg-reset",  .data = &meson8b_param},
114bdb369e1SXingyu Chen 	 { .compatible = "amlogic,meson-a1-reset",   .data = &meson_a1_param},
115636728d0SZelong Dong 	 { .compatible = "amlogic,meson-s4-reset",   .data = &meson_s4_param},
11641df5d7dSZelong Dong 	 { .compatible = "amlogic,c3-reset",   .data = &meson_s4_param},
117*b062ef29SZelong Dong 	 { .compatible = "amlogic,t7-reset",   .data = &t7_param},
118c7224dc3SNeil Armstrong 	 { /* sentinel */ },
119c7224dc3SNeil Armstrong };
1203bfe8933SNeil Armstrong MODULE_DEVICE_TABLE(of, meson_reset_dt_ids);
121c7224dc3SNeil Armstrong 
meson_reset_probe(struct platform_device * pdev)122c7224dc3SNeil Armstrong static int meson_reset_probe(struct platform_device *pdev)
123c7224dc3SNeil Armstrong {
124c7224dc3SNeil Armstrong 	struct meson_reset *data;
125c7224dc3SNeil Armstrong 
126c7224dc3SNeil Armstrong 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
127c7224dc3SNeil Armstrong 	if (!data)
128c7224dc3SNeil Armstrong 		return -ENOMEM;
129c7224dc3SNeil Armstrong 
130cabf1ce3SYe Xingchen 	data->reg_base = devm_platform_ioremap_resource(pdev, 0);
131c7224dc3SNeil Armstrong 	if (IS_ERR(data->reg_base))
132c7224dc3SNeil Armstrong 		return PTR_ERR(data->reg_base);
133c7224dc3SNeil Armstrong 
134bdb369e1SXingyu Chen 	data->param = of_device_get_match_data(&pdev->dev);
135bdb369e1SXingyu Chen 	if (!data->param)
136bdb369e1SXingyu Chen 		return -ENODEV;
137bdb369e1SXingyu Chen 
138a5a10afeSNeil Armstrong 	spin_lock_init(&data->lock);
139a5a10afeSNeil Armstrong 
140c7224dc3SNeil Armstrong 	data->rcdev.owner = THIS_MODULE;
141bdb369e1SXingyu Chen 	data->rcdev.nr_resets = data->param->reg_count * BITS_PER_REG;
142320da785SMartin Blumenstingl 	data->rcdev.ops = &meson_reset_ops;
143c7224dc3SNeil Armstrong 	data->rcdev.of_node = pdev->dev.of_node;
144c7224dc3SNeil Armstrong 
145c7224dc3SNeil Armstrong 	return devm_reset_controller_register(&pdev->dev, &data->rcdev);
146c7224dc3SNeil Armstrong }
147c7224dc3SNeil Armstrong 
148c7224dc3SNeil Armstrong static struct platform_driver meson_reset_driver = {
149c7224dc3SNeil Armstrong 	.probe	= meson_reset_probe,
150c7224dc3SNeil Armstrong 	.driver = {
151c7224dc3SNeil Armstrong 		.name		= "meson_reset",
152c7224dc3SNeil Armstrong 		.of_match_table	= meson_reset_dt_ids,
153c7224dc3SNeil Armstrong 	},
154c7224dc3SNeil Armstrong };
1553bfe8933SNeil Armstrong module_platform_driver(meson_reset_driver);
1563bfe8933SNeil Armstrong 
1573bfe8933SNeil Armstrong MODULE_DESCRIPTION("Amlogic Meson Reset Controller driver");
1583bfe8933SNeil Armstrong MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
1593bfe8933SNeil Armstrong MODULE_LICENSE("Dual BSD/GPL");
160