1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Support for power management features of the OLPC XO-1 laptop
4  *
5  * Copyright (C) 2010 Andres Salomon <dilinger@queued.net>
6  * Copyright (C) 2010 One Laptop per Child
7  * Copyright (C) 2006 Red Hat, Inc.
8  * Copyright (C) 2006 Advanced Micro Devices, Inc.
9  */
10 
11 #include <linux/cs5535.h>
12 #include <linux/platform_device.h>
13 #include <linux/export.h>
14 #include <linux/pm.h>
15 #include <linux/suspend.h>
16 #include <linux/olpc-ec.h>
17 
18 #include <asm/io.h>
19 #include <asm/olpc.h>
20 
21 #define DRV_NAME "olpc-xo1-pm"
22 
23 static unsigned long acpi_base;
24 static unsigned long pms_base;
25 
26 static u16 wakeup_mask = CS5536_PM_PWRBTN;
27 
28 static struct {
29 	unsigned long address;
30 	unsigned short segment;
31 } ofw_bios_entry = { 0xF0000 + PAGE_OFFSET, __KERNEL_CS };
32 
33 /* Set bits in the wakeup mask */
olpc_xo1_pm_wakeup_set(u16 value)34 void olpc_xo1_pm_wakeup_set(u16 value)
35 {
36 	wakeup_mask |= value;
37 }
38 EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_set);
39 
40 /* Clear bits in the wakeup mask */
olpc_xo1_pm_wakeup_clear(u16 value)41 void olpc_xo1_pm_wakeup_clear(u16 value)
42 {
43 	wakeup_mask &= ~value;
44 }
45 EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_clear);
46 
xo1_power_state_enter(suspend_state_t pm_state)47 static int xo1_power_state_enter(suspend_state_t pm_state)
48 {
49 	unsigned long saved_sci_mask;
50 
51 	/* Only STR is supported */
52 	if (pm_state != PM_SUSPEND_MEM)
53 		return -EINVAL;
54 
55 	/*
56 	 * Save SCI mask (this gets lost since PM1_EN is used as a mask for
57 	 * wakeup events, which is not necessarily the same event set)
58 	 */
59 	saved_sci_mask = inl(acpi_base + CS5536_PM1_STS);
60 	saved_sci_mask &= 0xffff0000;
61 
62 	/* Save CPU state */
63 	do_olpc_suspend_lowlevel();
64 
65 	/* Resume path starts here */
66 
67 	/* Restore SCI mask (using dword access to CS5536_PM1_EN) */
68 	outl(saved_sci_mask, acpi_base + CS5536_PM1_STS);
69 
70 	return 0;
71 }
72 
xo1_do_sleep(u8 sleep_state)73 asmlinkage __visible int xo1_do_sleep(u8 sleep_state)
74 {
75 	void *pgd_addr = __va(read_cr3_pa());
76 
77 	/* Program wakeup mask (using dword access to CS5536_PM1_EN) */
78 	outl(wakeup_mask << 16, acpi_base + CS5536_PM1_STS);
79 
80 	__asm__("movl %0,%%eax" : : "r" (pgd_addr));
81 	__asm__("call *(%%edi); cld"
82 		: : "D" (&ofw_bios_entry));
83 	__asm__("movb $0x34, %al\n\t"
84 		"outb %al, $0x70\n\t"
85 		"movb $0x30, %al\n\t"
86 		"outb %al, $0x71\n\t");
87 	return 0;
88 }
89 
xo1_power_off(void)90 static void xo1_power_off(void)
91 {
92 	printk(KERN_INFO "OLPC XO-1 power off sequence...\n");
93 
94 	/* Enable all of these controls with 0 delay */
95 	outl(0x40000000, pms_base + CS5536_PM_SCLK);
96 	outl(0x40000000, pms_base + CS5536_PM_IN_SLPCTL);
97 	outl(0x40000000, pms_base + CS5536_PM_WKXD);
98 	outl(0x40000000, pms_base + CS5536_PM_WKD);
99 
100 	/* Clear status bits (possibly unnecessary) */
101 	outl(0x0002ffff, pms_base  + CS5536_PM_SSC);
102 	outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
103 
104 	/* Write SLP_EN bit to start the machinery */
105 	outl(0x00002000, acpi_base + CS5536_PM1_CNT);
106 }
107 
xo1_power_state_valid(suspend_state_t pm_state)108 static int xo1_power_state_valid(suspend_state_t pm_state)
109 {
110 	/* suspend-to-RAM only */
111 	return pm_state == PM_SUSPEND_MEM;
112 }
113 
114 static const struct platform_suspend_ops xo1_suspend_ops = {
115 	.valid = xo1_power_state_valid,
116 	.enter = xo1_power_state_enter,
117 };
118 
xo1_pm_probe(struct platform_device * pdev)119 static int xo1_pm_probe(struct platform_device *pdev)
120 {
121 	struct resource *res;
122 
123 	/* don't run on non-XOs */
124 	if (!machine_is_olpc())
125 		return -ENODEV;
126 
127 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
128 	if (!res) {
129 		dev_err(&pdev->dev, "can't fetch device resource info\n");
130 		return -EIO;
131 	}
132 	if (strcmp(pdev->name, "cs5535-pms") == 0)
133 		pms_base = res->start;
134 	else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0)
135 		acpi_base = res->start;
136 
137 	/* If we have both addresses, we can override the poweroff hook */
138 	if (pms_base && acpi_base) {
139 		suspend_set_ops(&xo1_suspend_ops);
140 		pm_power_off = xo1_power_off;
141 		printk(KERN_INFO "OLPC XO-1 support registered\n");
142 	}
143 
144 	return 0;
145 }
146 
xo1_pm_remove(struct platform_device * pdev)147 static int xo1_pm_remove(struct platform_device *pdev)
148 {
149 	if (strcmp(pdev->name, "cs5535-pms") == 0)
150 		pms_base = 0;
151 	else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0)
152 		acpi_base = 0;
153 
154 	pm_power_off = NULL;
155 	return 0;
156 }
157 
158 static struct platform_driver cs5535_pms_driver = {
159 	.driver = {
160 		.name = "cs5535-pms",
161 	},
162 	.probe = xo1_pm_probe,
163 	.remove = xo1_pm_remove,
164 };
165 
166 static struct platform_driver cs5535_acpi_driver = {
167 	.driver = {
168 		.name = "olpc-xo1-pm-acpi",
169 	},
170 	.probe = xo1_pm_probe,
171 	.remove = xo1_pm_remove,
172 };
173 
xo1_pm_init(void)174 static int __init xo1_pm_init(void)
175 {
176 	int r;
177 
178 	r = platform_driver_register(&cs5535_pms_driver);
179 	if (r)
180 		return r;
181 
182 	r = platform_driver_register(&cs5535_acpi_driver);
183 	if (r)
184 		platform_driver_unregister(&cs5535_pms_driver);
185 
186 	return r;
187 }
188 arch_initcall(xo1_pm_init);
189