xref: /openbmc/linux/drivers/reset/reset-ath79.c (revision 4a075bd4)
1 /*
2  * AR71xx Reset Controller Driver
3  * Author: Alban Bedel
4  *
5  * Copyright (C) 2015 Alban Bedel <albeu@free.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 
18 #include <linux/io.h>
19 #include <linux/init.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/platform_device.h>
22 #include <linux/reset-controller.h>
23 #include <linux/reboot.h>
24 
25 struct ath79_reset {
26 	struct reset_controller_dev rcdev;
27 	struct notifier_block restart_nb;
28 	void __iomem *base;
29 	spinlock_t lock;
30 };
31 
32 #define FULL_CHIP_RESET 24
33 
34 static int ath79_reset_update(struct reset_controller_dev *rcdev,
35 			unsigned long id, bool assert)
36 {
37 	struct ath79_reset *ath79_reset =
38 		container_of(rcdev, struct ath79_reset, rcdev);
39 	unsigned long flags;
40 	u32 val;
41 
42 	spin_lock_irqsave(&ath79_reset->lock, flags);
43 	val = readl(ath79_reset->base);
44 	if (assert)
45 		val |= BIT(id);
46 	else
47 		val &= ~BIT(id);
48 	writel(val, ath79_reset->base);
49 	spin_unlock_irqrestore(&ath79_reset->lock, flags);
50 
51 	return 0;
52 }
53 
54 static int ath79_reset_assert(struct reset_controller_dev *rcdev,
55 			unsigned long id)
56 {
57 	return ath79_reset_update(rcdev, id, true);
58 }
59 
60 static int ath79_reset_deassert(struct reset_controller_dev *rcdev,
61 				unsigned long id)
62 {
63 	return ath79_reset_update(rcdev, id, false);
64 }
65 
66 static int ath79_reset_status(struct reset_controller_dev *rcdev,
67 			unsigned long id)
68 {
69 	struct ath79_reset *ath79_reset =
70 		container_of(rcdev, struct ath79_reset, rcdev);
71 	u32 val;
72 
73 	val = readl(ath79_reset->base);
74 
75 	return !!(val & BIT(id));
76 }
77 
78 static const struct reset_control_ops ath79_reset_ops = {
79 	.assert = ath79_reset_assert,
80 	.deassert = ath79_reset_deassert,
81 	.status = ath79_reset_status,
82 };
83 
84 static int ath79_reset_restart_handler(struct notifier_block *nb,
85 				unsigned long action, void *data)
86 {
87 	struct ath79_reset *ath79_reset =
88 		container_of(nb, struct ath79_reset, restart_nb);
89 
90 	ath79_reset_assert(&ath79_reset->rcdev, FULL_CHIP_RESET);
91 
92 	return NOTIFY_DONE;
93 }
94 
95 static int ath79_reset_probe(struct platform_device *pdev)
96 {
97 	struct ath79_reset *ath79_reset;
98 	struct resource *res;
99 	int err;
100 
101 	ath79_reset = devm_kzalloc(&pdev->dev,
102 				sizeof(*ath79_reset), GFP_KERNEL);
103 	if (!ath79_reset)
104 		return -ENOMEM;
105 
106 	platform_set_drvdata(pdev, ath79_reset);
107 
108 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
109 	ath79_reset->base = devm_ioremap_resource(&pdev->dev, res);
110 	if (IS_ERR(ath79_reset->base))
111 		return PTR_ERR(ath79_reset->base);
112 
113 	spin_lock_init(&ath79_reset->lock);
114 	ath79_reset->rcdev.ops = &ath79_reset_ops;
115 	ath79_reset->rcdev.owner = THIS_MODULE;
116 	ath79_reset->rcdev.of_node = pdev->dev.of_node;
117 	ath79_reset->rcdev.of_reset_n_cells = 1;
118 	ath79_reset->rcdev.nr_resets = 32;
119 
120 	err = devm_reset_controller_register(&pdev->dev, &ath79_reset->rcdev);
121 	if (err)
122 		return err;
123 
124 	ath79_reset->restart_nb.notifier_call = ath79_reset_restart_handler;
125 	ath79_reset->restart_nb.priority = 128;
126 
127 	err = register_restart_handler(&ath79_reset->restart_nb);
128 	if (err)
129 		dev_warn(&pdev->dev, "Failed to register restart handler\n");
130 
131 	return 0;
132 }
133 
134 static const struct of_device_id ath79_reset_dt_ids[] = {
135 	{ .compatible = "qca,ar7100-reset", },
136 	{ },
137 };
138 
139 static struct platform_driver ath79_reset_driver = {
140 	.probe	= ath79_reset_probe,
141 	.driver = {
142 		.name			= "ath79-reset",
143 		.of_match_table		= ath79_reset_dt_ids,
144 		.suppress_bind_attrs	= true,
145 	},
146 };
147 builtin_platform_driver(ath79_reset_driver);
148