xref: /openbmc/linux/drivers/power/reset/at91-poweroff.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1ae499f0fSMaxime Ripard /*
2ae499f0fSMaxime Ripard  * Atmel AT91 SAM9 SoCs reset code
3ae499f0fSMaxime Ripard  *
4ae499f0fSMaxime Ripard  * Copyright (C) 2007 Atmel Corporation.
5ae499f0fSMaxime Ripard  * Copyright (C) 2011 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
6ae499f0fSMaxime Ripard  * Copyright (C) 2014 Free Electrons
7ae499f0fSMaxime Ripard  *
8ae499f0fSMaxime Ripard  * This file is licensed under the terms of the GNU General Public
9ae499f0fSMaxime Ripard  * License version 2.  This program is licensed "as is" without any
10ae499f0fSMaxime Ripard  * warranty of any kind, whether express or implied.
11ae499f0fSMaxime Ripard  */
12ae499f0fSMaxime Ripard 
13064380a1SAlexandre Belloni #include <linux/clk.h>
14ae499f0fSMaxime Ripard #include <linux/io.h>
15ae499f0fSMaxime Ripard #include <linux/module.h>
16ae499f0fSMaxime Ripard #include <linux/of.h>
170b040874SAlexandre Belloni #include <linux/of_address.h>
18ae499f0fSMaxime Ripard #include <linux/platform_device.h>
19ae499f0fSMaxime Ripard #include <linux/printk.h>
20ae499f0fSMaxime Ripard 
210b040874SAlexandre Belloni #include <soc/at91/at91sam9_ddrsdr.h>
220b040874SAlexandre Belloni 
23ae499f0fSMaxime Ripard #define AT91_SHDW_CR	0x00		/* Shut Down Control Register */
24ae499f0fSMaxime Ripard #define AT91_SHDW_SHDW		BIT(0)			/* Shut Down command */
25ae499f0fSMaxime Ripard #define AT91_SHDW_KEY		(0xa5 << 24)		/* KEY Password */
26ae499f0fSMaxime Ripard 
27ae499f0fSMaxime Ripard #define AT91_SHDW_MR	0x04		/* Shut Down Mode Register */
28ae499f0fSMaxime Ripard #define AT91_SHDW_WKMODE0	GENMASK(2, 0)		/* Wake-up 0 Mode Selection */
29ae499f0fSMaxime Ripard #define AT91_SHDW_CPTWK0_MAX	0xf			/* Maximum Counter On Wake Up 0 */
30ae499f0fSMaxime Ripard #define AT91_SHDW_CPTWK0	(AT91_SHDW_CPTWK0_MAX << 4) /* Counter On Wake Up 0 */
31ae499f0fSMaxime Ripard #define AT91_SHDW_CPTWK0_(x)	((x) << 4)
32ae499f0fSMaxime Ripard #define AT91_SHDW_RTTWKEN	BIT(16)			/* Real Time Timer Wake-up Enable */
33ae499f0fSMaxime Ripard #define AT91_SHDW_RTCWKEN	BIT(17)			/* Real Time Clock Wake-up Enable */
34ae499f0fSMaxime Ripard 
35ae499f0fSMaxime Ripard #define AT91_SHDW_SR	0x08		/* Shut Down Status Register */
36ae499f0fSMaxime Ripard #define AT91_SHDW_WAKEUP0	BIT(0)			/* Wake-up 0 Status */
37ae499f0fSMaxime Ripard #define AT91_SHDW_RTTWK		BIT(16)			/* Real-time Timer Wake-up */
38ae499f0fSMaxime Ripard #define AT91_SHDW_RTCWK		BIT(17)			/* Real-time Clock Wake-up [SAM9RL] */
39ae499f0fSMaxime Ripard 
40ae499f0fSMaxime Ripard enum wakeup_type {
41ae499f0fSMaxime Ripard 	AT91_SHDW_WKMODE0_NONE		= 0,
42ae499f0fSMaxime Ripard 	AT91_SHDW_WKMODE0_HIGH		= 1,
43ae499f0fSMaxime Ripard 	AT91_SHDW_WKMODE0_LOW		= 2,
44ae499f0fSMaxime Ripard 	AT91_SHDW_WKMODE0_ANYLEVEL	= 3,
45ae499f0fSMaxime Ripard };
46ae499f0fSMaxime Ripard 
47ae499f0fSMaxime Ripard static const char *shdwc_wakeup_modes[] = {
48ae499f0fSMaxime Ripard 	[AT91_SHDW_WKMODE0_NONE]	= "none",
49ae499f0fSMaxime Ripard 	[AT91_SHDW_WKMODE0_HIGH]	= "high",
50ae499f0fSMaxime Ripard 	[AT91_SHDW_WKMODE0_LOW]		= "low",
51ae499f0fSMaxime Ripard 	[AT91_SHDW_WKMODE0_ANYLEVEL]	= "any",
52ae499f0fSMaxime Ripard };
53ae499f0fSMaxime Ripard 
54caa6e150SClaudiu Beznea static struct shdwc {
55caa6e150SClaudiu Beznea 	struct clk *sclk;
56caa6e150SClaudiu Beznea 	void __iomem *shdwc_base;
57caa6e150SClaudiu Beznea 	void __iomem *mpddrc_base;
58caa6e150SClaudiu Beznea } at91_shdwc;
59ae499f0fSMaxime Ripard 
at91_wakeup_status(struct platform_device * pdev)60062836dbSLadislav Michl static void __init at91_wakeup_status(struct platform_device *pdev)
61ae499f0fSMaxime Ripard {
62062836dbSLadislav Michl 	const char *reason;
63caa6e150SClaudiu Beznea 	u32 reg = readl(at91_shdwc.shdwc_base + AT91_SHDW_SR);
64ae499f0fSMaxime Ripard 
65ae499f0fSMaxime Ripard 	/* Simple power-on, just bail out */
66ae499f0fSMaxime Ripard 	if (!reg)
67ae499f0fSMaxime Ripard 		return;
68ae499f0fSMaxime Ripard 
69ae499f0fSMaxime Ripard 	if (reg & AT91_SHDW_RTTWK)
70ae499f0fSMaxime Ripard 		reason = "RTT";
71ae499f0fSMaxime Ripard 	else if (reg & AT91_SHDW_RTCWK)
72ae499f0fSMaxime Ripard 		reason = "RTC";
73062836dbSLadislav Michl 	else
74062836dbSLadislav Michl 		reason = "unknown";
75ae499f0fSMaxime Ripard 
76062836dbSLadislav Michl 	dev_info(&pdev->dev, "Wake-Up source: %s\n", reason);
77ae499f0fSMaxime Ripard }
78ae499f0fSMaxime Ripard 
at91_poweroff(void)79ae499f0fSMaxime Ripard static void at91_poweroff(void)
80ae499f0fSMaxime Ripard {
810b040874SAlexandre Belloni 	asm volatile(
820b040874SAlexandre Belloni 		/* Align to cache lines */
830b040874SAlexandre Belloni 		".balign 32\n\t"
840b040874SAlexandre Belloni 
850b040874SAlexandre Belloni 		/* Ensure AT91_SHDW_CR is in the TLB by reading it */
860b040874SAlexandre Belloni 		"	ldr	r6, [%2, #" __stringify(AT91_SHDW_CR) "]\n\t"
870b040874SAlexandre Belloni 
880b040874SAlexandre Belloni 		/* Power down SDRAM0 */
89996463fdSClaudiu.Beznea@microchip.com 		"	tst	%0, #0\n\t"
90996463fdSClaudiu.Beznea@microchip.com 		"	beq	1f\n\t"
910b040874SAlexandre Belloni 		"	str	%1, [%0, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
920b040874SAlexandre Belloni 		/* Shutdown CPU */
93996463fdSClaudiu.Beznea@microchip.com 		"1:	str	%3, [%2, #" __stringify(AT91_SHDW_CR) "]\n\t"
940b040874SAlexandre Belloni 
950b040874SAlexandre Belloni 		"	b	.\n\t"
960b040874SAlexandre Belloni 		:
97caa6e150SClaudiu Beznea 		: "r" (at91_shdwc.mpddrc_base),
980b040874SAlexandre Belloni 		  "r" cpu_to_le32(AT91_DDRSDRC_LPDDR2_PWOFF),
99caa6e150SClaudiu Beznea 		  "r" (at91_shdwc.shdwc_base),
1000b040874SAlexandre Belloni 		  "r" cpu_to_le32(AT91_SHDW_KEY | AT91_SHDW_SHDW)
1012e9bbbf6SAlexandre Belloni 		: "r6");
1020b040874SAlexandre Belloni }
1030b040874SAlexandre Belloni 
at91_poweroff_get_wakeup_mode(struct device_node * np)104a538cf04SGuenter Roeck static int at91_poweroff_get_wakeup_mode(struct device_node *np)
105ae499f0fSMaxime Ripard {
106ae499f0fSMaxime Ripard 	const char *pm;
107a538cf04SGuenter Roeck 	unsigned int i;
108a538cf04SGuenter Roeck 	int err;
109ae499f0fSMaxime Ripard 
110ae499f0fSMaxime Ripard 	err = of_property_read_string(np, "atmel,wakeup-mode", &pm);
111ae499f0fSMaxime Ripard 	if (err < 0)
112ae499f0fSMaxime Ripard 		return AT91_SHDW_WKMODE0_ANYLEVEL;
113ae499f0fSMaxime Ripard 
114ae499f0fSMaxime Ripard 	for (i = 0; i < ARRAY_SIZE(shdwc_wakeup_modes); i++)
115ae499f0fSMaxime Ripard 		if (!strcasecmp(pm, shdwc_wakeup_modes[i]))
116ae499f0fSMaxime Ripard 			return i;
117ae499f0fSMaxime Ripard 
118ae499f0fSMaxime Ripard 	return -ENODEV;
119ae499f0fSMaxime Ripard }
120ae499f0fSMaxime Ripard 
at91_poweroff_dt_set_wakeup_mode(struct platform_device * pdev)121ae499f0fSMaxime Ripard static void at91_poweroff_dt_set_wakeup_mode(struct platform_device *pdev)
122ae499f0fSMaxime Ripard {
123ae499f0fSMaxime Ripard 	struct device_node *np = pdev->dev.of_node;
124a538cf04SGuenter Roeck 	int wakeup_mode;
125ae499f0fSMaxime Ripard 	u32 mode = 0, tmp;
126ae499f0fSMaxime Ripard 
127ae499f0fSMaxime Ripard 	wakeup_mode = at91_poweroff_get_wakeup_mode(np);
128ae499f0fSMaxime Ripard 	if (wakeup_mode < 0) {
129ae499f0fSMaxime Ripard 		dev_warn(&pdev->dev, "shdwc unknown wakeup mode\n");
130ae499f0fSMaxime Ripard 		return;
131ae499f0fSMaxime Ripard 	}
132ae499f0fSMaxime Ripard 
133ae499f0fSMaxime Ripard 	if (!of_property_read_u32(np, "atmel,wakeup-counter", &tmp)) {
134ae499f0fSMaxime Ripard 		if (tmp > AT91_SHDW_CPTWK0_MAX) {
135ae499f0fSMaxime Ripard 			dev_warn(&pdev->dev,
136ae499f0fSMaxime Ripard 				 "shdwc wakeup counter 0x%x > 0x%x reduce it to 0x%x\n",
137ae499f0fSMaxime Ripard 				 tmp, AT91_SHDW_CPTWK0_MAX, AT91_SHDW_CPTWK0_MAX);
138ae499f0fSMaxime Ripard 			tmp = AT91_SHDW_CPTWK0_MAX;
139ae499f0fSMaxime Ripard 		}
140ae499f0fSMaxime Ripard 		mode |= AT91_SHDW_CPTWK0_(tmp);
141ae499f0fSMaxime Ripard 	}
142ae499f0fSMaxime Ripard 
143ae499f0fSMaxime Ripard 	if (of_property_read_bool(np, "atmel,wakeup-rtc-timer"))
144ae499f0fSMaxime Ripard 			mode |= AT91_SHDW_RTCWKEN;
145ae499f0fSMaxime Ripard 
146ae499f0fSMaxime Ripard 	if (of_property_read_bool(np, "atmel,wakeup-rtt-timer"))
147ae499f0fSMaxime Ripard 			mode |= AT91_SHDW_RTTWKEN;
148ae499f0fSMaxime Ripard 
149caa6e150SClaudiu Beznea 	writel(wakeup_mode | mode, at91_shdwc.shdwc_base + AT91_SHDW_MR);
150ae499f0fSMaxime Ripard }
151ae499f0fSMaxime Ripard 
at91_poweroff_probe(struct platform_device * pdev)1526dd1ad1fSAlexandre Belloni static int __init at91_poweroff_probe(struct platform_device *pdev)
153ae499f0fSMaxime Ripard {
1540b040874SAlexandre Belloni 	struct device_node *np;
1550b040874SAlexandre Belloni 	u32 ddr_type;
156064380a1SAlexandre Belloni 	int ret;
157ae499f0fSMaxime Ripard 
158*0b64a0e5SYangtao Li 	at91_shdwc.shdwc_base = devm_platform_ioremap_resource(pdev, 0);
159caa6e150SClaudiu Beznea 	if (IS_ERR(at91_shdwc.shdwc_base))
160caa6e150SClaudiu Beznea 		return PTR_ERR(at91_shdwc.shdwc_base);
161ae499f0fSMaxime Ripard 
162caa6e150SClaudiu Beznea 	at91_shdwc.sclk = devm_clk_get(&pdev->dev, NULL);
163caa6e150SClaudiu Beznea 	if (IS_ERR(at91_shdwc.sclk))
164caa6e150SClaudiu Beznea 		return PTR_ERR(at91_shdwc.sclk);
165064380a1SAlexandre Belloni 
166caa6e150SClaudiu Beznea 	ret = clk_prepare_enable(at91_shdwc.sclk);
167064380a1SAlexandre Belloni 	if (ret) {
168064380a1SAlexandre Belloni 		dev_err(&pdev->dev, "Could not enable slow clock\n");
169064380a1SAlexandre Belloni 		return ret;
170064380a1SAlexandre Belloni 	}
171064380a1SAlexandre Belloni 
172062836dbSLadislav Michl 	at91_wakeup_status(pdev);
173ae499f0fSMaxime Ripard 
174ae499f0fSMaxime Ripard 	if (pdev->dev.of_node)
175ae499f0fSMaxime Ripard 		at91_poweroff_dt_set_wakeup_mode(pdev);
176ae499f0fSMaxime Ripard 
1770b040874SAlexandre Belloni 	np = of_find_compatible_node(NULL, NULL, "atmel,sama5d3-ddramc");
178996463fdSClaudiu.Beznea@microchip.com 	if (np) {
179caa6e150SClaudiu Beznea 		at91_shdwc.mpddrc_base = of_iomap(np, 0);
1800b040874SAlexandre Belloni 		of_node_put(np);
1810b040874SAlexandre Belloni 
182caa6e150SClaudiu Beznea 		if (!at91_shdwc.mpddrc_base) {
183996463fdSClaudiu.Beznea@microchip.com 			ret = -ENOMEM;
184996463fdSClaudiu.Beznea@microchip.com 			goto clk_disable;
185996463fdSClaudiu.Beznea@microchip.com 		}
1860b040874SAlexandre Belloni 
187caa6e150SClaudiu Beznea 		ddr_type = readl(at91_shdwc.mpddrc_base + AT91_DDRSDRC_MDR) &
188996463fdSClaudiu.Beznea@microchip.com 				 AT91_DDRSDRC_MD;
189996463fdSClaudiu.Beznea@microchip.com 		if (ddr_type != AT91_DDRSDRC_MD_LPDDR2 &&
190996463fdSClaudiu.Beznea@microchip.com 		    ddr_type != AT91_DDRSDRC_MD_LPDDR3) {
191caa6e150SClaudiu Beznea 			iounmap(at91_shdwc.mpddrc_base);
192caa6e150SClaudiu Beznea 			at91_shdwc.mpddrc_base = NULL;
193996463fdSClaudiu.Beznea@microchip.com 		}
194996463fdSClaudiu.Beznea@microchip.com 	}
195996463fdSClaudiu.Beznea@microchip.com 
196996463fdSClaudiu.Beznea@microchip.com 	pm_power_off = at91_poweroff;
1970b040874SAlexandre Belloni 
198ae499f0fSMaxime Ripard 	return 0;
199996463fdSClaudiu.Beznea@microchip.com 
200996463fdSClaudiu.Beznea@microchip.com clk_disable:
201caa6e150SClaudiu Beznea 	clk_disable_unprepare(at91_shdwc.sclk);
202996463fdSClaudiu.Beznea@microchip.com 	return ret;
203ae499f0fSMaxime Ripard }
204ae499f0fSMaxime Ripard 
at91_poweroff_remove(struct platform_device * pdev)2056dd1ad1fSAlexandre Belloni static int __exit at91_poweroff_remove(struct platform_device *pdev)
2066dd1ad1fSAlexandre Belloni {
207996463fdSClaudiu.Beznea@microchip.com 	if (pm_power_off == at91_poweroff)
2086dd1ad1fSAlexandre Belloni 		pm_power_off = NULL;
2096dd1ad1fSAlexandre Belloni 
210caa6e150SClaudiu Beznea 	if (at91_shdwc.mpddrc_base)
211caa6e150SClaudiu Beznea 		iounmap(at91_shdwc.mpddrc_base);
212996463fdSClaudiu.Beznea@microchip.com 
213caa6e150SClaudiu Beznea 	clk_disable_unprepare(at91_shdwc.sclk);
214064380a1SAlexandre Belloni 
2156dd1ad1fSAlexandre Belloni 	return 0;
2166dd1ad1fSAlexandre Belloni }
2176dd1ad1fSAlexandre Belloni 
2188fb08855SFabian Frederick static const struct of_device_id at91_poweroff_of_match[] = {
219ae499f0fSMaxime Ripard 	{ .compatible = "atmel,at91sam9260-shdwc", },
220ae499f0fSMaxime Ripard 	{ .compatible = "atmel,at91sam9rl-shdwc", },
221ae499f0fSMaxime Ripard 	{ .compatible = "atmel,at91sam9x5-shdwc", },
222ae499f0fSMaxime Ripard 	{ /*sentinel*/ }
223ae499f0fSMaxime Ripard };
224c9ba9b77SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, at91_poweroff_of_match);
225ae499f0fSMaxime Ripard 
226ae499f0fSMaxime Ripard static struct platform_driver at91_poweroff_driver = {
2276dd1ad1fSAlexandre Belloni 	.remove = __exit_p(at91_poweroff_remove),
228ae499f0fSMaxime Ripard 	.driver = {
229ae499f0fSMaxime Ripard 		.name = "at91-poweroff",
230ae499f0fSMaxime Ripard 		.of_match_table = at91_poweroff_of_match,
231ae499f0fSMaxime Ripard 	},
232ae499f0fSMaxime Ripard };
2336dd1ad1fSAlexandre Belloni module_platform_driver_probe(at91_poweroff_driver, at91_poweroff_probe);
2346dd1ad1fSAlexandre Belloni 
2356dd1ad1fSAlexandre Belloni MODULE_AUTHOR("Atmel Corporation");
2366dd1ad1fSAlexandre Belloni MODULE_DESCRIPTION("Shutdown driver for Atmel SoCs");
2376dd1ad1fSAlexandre Belloni MODULE_LICENSE("GPL v2");
238