1f0745f36SLee Jones /*
2f0745f36SLee Jones  * Copyright (C) 2014 STMicroelectronics
3f0745f36SLee Jones  *
4f0745f36SLee Jones  * Power off Restart driver, used in STMicroelectronics devices.
5f0745f36SLee Jones  *
6f0745f36SLee Jones  * Author: Christophe Kerello <christophe.kerello@st.com>
7f0745f36SLee Jones  *
8f0745f36SLee Jones  * This program is free software; you can redistribute it and/or modify
9f0745f36SLee Jones  * it under the terms of the GNU General Public License version 2, as
10f0745f36SLee Jones  * published by the Free Software Foundation.
11f0745f36SLee Jones  */
12f0745f36SLee Jones 
13f0745f36SLee Jones #include <linux/module.h>
14f0745f36SLee Jones #include <linux/of.h>
15f0745f36SLee Jones #include <linux/of_platform.h>
16f0745f36SLee Jones #include <linux/platform_device.h>
17f0745f36SLee Jones #include <linux/mfd/syscon.h>
18453fe4f1SGuenter Roeck #include <linux/reboot.h>
19f0745f36SLee Jones #include <linux/regmap.h>
20f0745f36SLee Jones 
21f0745f36SLee Jones struct reset_syscfg {
22f0745f36SLee Jones 	struct regmap *regmap;
23f0745f36SLee Jones 	/* syscfg used for reset */
24f0745f36SLee Jones 	unsigned int offset_rst;
25f0745f36SLee Jones 	unsigned int mask_rst;
26f0745f36SLee Jones 	/* syscfg used for unmask the reset */
27f0745f36SLee Jones 	unsigned int offset_rst_msk;
28f0745f36SLee Jones 	unsigned int mask_rst_msk;
29f0745f36SLee Jones };
30f0745f36SLee Jones 
31f0745f36SLee Jones /* STiH415 */
32f0745f36SLee Jones #define STIH415_SYSCFG_11	0x2c
33f0745f36SLee Jones #define STIH415_SYSCFG_15	0x3c
34f0745f36SLee Jones 
35f0745f36SLee Jones static struct reset_syscfg stih415_reset = {
36f0745f36SLee Jones 	.offset_rst = STIH415_SYSCFG_11,
37f0745f36SLee Jones 	.mask_rst = BIT(0),
38f0745f36SLee Jones 	.offset_rst_msk = STIH415_SYSCFG_15,
39f0745f36SLee Jones 	.mask_rst_msk = BIT(0)
40f0745f36SLee Jones };
41f0745f36SLee Jones 
42f0745f36SLee Jones /* STiH416 */
43f0745f36SLee Jones #define STIH416_SYSCFG_500	0x7d0
44f0745f36SLee Jones #define STIH416_SYSCFG_504	0x7e0
45f0745f36SLee Jones 
46f0745f36SLee Jones static struct reset_syscfg stih416_reset = {
47f0745f36SLee Jones 	.offset_rst = STIH416_SYSCFG_500,
48f0745f36SLee Jones 	.mask_rst = BIT(0),
49f0745f36SLee Jones 	.offset_rst_msk = STIH416_SYSCFG_504,
50f0745f36SLee Jones 	.mask_rst_msk = BIT(0)
51f0745f36SLee Jones };
52f0745f36SLee Jones 
53f0745f36SLee Jones /* STiH407 */
54f0745f36SLee Jones #define STIH407_SYSCFG_4000	0x0
55f0745f36SLee Jones #define STIH407_SYSCFG_4008	0x20
56f0745f36SLee Jones 
57f0745f36SLee Jones static struct reset_syscfg stih407_reset = {
58f0745f36SLee Jones 	.offset_rst = STIH407_SYSCFG_4000,
59f0745f36SLee Jones 	.mask_rst = BIT(0),
60f0745f36SLee Jones 	.offset_rst_msk = STIH407_SYSCFG_4008,
61f0745f36SLee Jones 	.mask_rst_msk = BIT(0)
62f0745f36SLee Jones };
63f0745f36SLee Jones 
64f0745f36SLee Jones /* STiD127 */
65f0745f36SLee Jones #define STID127_SYSCFG_700	0x0
66f0745f36SLee Jones #define STID127_SYSCFG_773	0x124
67f0745f36SLee Jones 
68f0745f36SLee Jones static struct reset_syscfg stid127_reset = {
69f0745f36SLee Jones 	.offset_rst = STID127_SYSCFG_773,
70f0745f36SLee Jones 	.mask_rst = BIT(0),
71f0745f36SLee Jones 	.offset_rst_msk = STID127_SYSCFG_700,
72f0745f36SLee Jones 	.mask_rst_msk = BIT(8)
73f0745f36SLee Jones };
74f0745f36SLee Jones 
75f0745f36SLee Jones static struct reset_syscfg *st_restart_syscfg;
76f0745f36SLee Jones 
77453fe4f1SGuenter Roeck static int st_restart(struct notifier_block *this, unsigned long mode,
78453fe4f1SGuenter Roeck 		      void *cmd)
79f0745f36SLee Jones {
80f0745f36SLee Jones 	/* reset syscfg updated */
81f0745f36SLee Jones 	regmap_update_bits(st_restart_syscfg->regmap,
82f0745f36SLee Jones 			   st_restart_syscfg->offset_rst,
83f0745f36SLee Jones 			   st_restart_syscfg->mask_rst,
84f0745f36SLee Jones 			   0);
85f0745f36SLee Jones 
86f0745f36SLee Jones 	/* unmask the reset */
87f0745f36SLee Jones 	regmap_update_bits(st_restart_syscfg->regmap,
88f0745f36SLee Jones 			   st_restart_syscfg->offset_rst_msk,
89f0745f36SLee Jones 			   st_restart_syscfg->mask_rst_msk,
90f0745f36SLee Jones 			   0);
91453fe4f1SGuenter Roeck 
92453fe4f1SGuenter Roeck 	return NOTIFY_DONE;
93f0745f36SLee Jones }
94f0745f36SLee Jones 
95453fe4f1SGuenter Roeck static struct notifier_block st_restart_nb = {
96453fe4f1SGuenter Roeck 	.notifier_call = st_restart,
97453fe4f1SGuenter Roeck 	.priority = 192,
98453fe4f1SGuenter Roeck };
99453fe4f1SGuenter Roeck 
1008fb08855SFabian Frederick static const struct of_device_id st_reset_of_match[] = {
101f0745f36SLee Jones 	{
102f0745f36SLee Jones 		.compatible = "st,stih415-restart",
103f0745f36SLee Jones 		.data = (void *)&stih415_reset,
104f0745f36SLee Jones 	}, {
105f0745f36SLee Jones 		.compatible = "st,stih416-restart",
106f0745f36SLee Jones 		.data = (void *)&stih416_reset,
107f0745f36SLee Jones 	}, {
108f0745f36SLee Jones 		.compatible = "st,stih407-restart",
109f0745f36SLee Jones 		.data = (void *)&stih407_reset,
110f0745f36SLee Jones 	}, {
111f0745f36SLee Jones 		.compatible = "st,stid127-restart",
112f0745f36SLee Jones 		.data = (void *)&stid127_reset,
113f0745f36SLee Jones 	},
114f0745f36SLee Jones 	{}
115f0745f36SLee Jones };
116f0745f36SLee Jones 
117f0745f36SLee Jones static int st_reset_probe(struct platform_device *pdev)
118f0745f36SLee Jones {
119f0745f36SLee Jones 	struct device_node *np = pdev->dev.of_node;
120f0745f36SLee Jones 	const struct of_device_id *match;
121f0745f36SLee Jones 	struct device *dev = &pdev->dev;
122f0745f36SLee Jones 
123f0745f36SLee Jones 	match = of_match_device(st_reset_of_match, dev);
124f0745f36SLee Jones 	if (!match)
125f0745f36SLee Jones 		return -ENODEV;
126f0745f36SLee Jones 
127f0745f36SLee Jones 	st_restart_syscfg = (struct reset_syscfg *)match->data;
128f0745f36SLee Jones 
129f0745f36SLee Jones 	st_restart_syscfg->regmap =
130f0745f36SLee Jones 		syscon_regmap_lookup_by_phandle(np, "st,syscfg");
131f0745f36SLee Jones 	if (IS_ERR(st_restart_syscfg->regmap)) {
132f0745f36SLee Jones 		dev_err(dev, "No syscfg phandle specified\n");
133f0745f36SLee Jones 		return PTR_ERR(st_restart_syscfg->regmap);
134f0745f36SLee Jones 	}
135f0745f36SLee Jones 
136453fe4f1SGuenter Roeck 	return register_restart_handler(&st_restart_nb);
137f0745f36SLee Jones }
138f0745f36SLee Jones 
139f0745f36SLee Jones static struct platform_driver st_reset_driver = {
140f0745f36SLee Jones 	.probe = st_reset_probe,
141f0745f36SLee Jones 	.driver = {
142f0745f36SLee Jones 		.name = "st_reset",
143f0745f36SLee Jones 		.of_match_table = st_reset_of_match,
144f0745f36SLee Jones 	},
145f0745f36SLee Jones };
146f0745f36SLee Jones 
147f0745f36SLee Jones static int __init st_reset_init(void)
148f0745f36SLee Jones {
149f0745f36SLee Jones 	return platform_driver_register(&st_reset_driver);
150f0745f36SLee Jones }
151f0745f36SLee Jones 
152f0745f36SLee Jones device_initcall(st_reset_init);
153f0745f36SLee Jones 
154f0745f36SLee Jones MODULE_AUTHOR("Christophe Kerello <christophe.kerello@st.com>");
155f0745f36SLee Jones MODULE_DESCRIPTION("STMicroelectronics Power off Restart driver");
156f0745f36SLee Jones MODULE_LICENSE("GPL v2");
157