xref: /openbmc/linux/drivers/soc/ti/pm33xx.c (revision afe761f8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * AM33XX Power Management Routines
4  *
5  * Copyright (C) 2012-2018 Texas Instruments Incorporated - http://www.ti.com/
6  *	Vaibhav Bedia, Dave Gerlach
7  */
8 
9 #include <linux/cpu.h>
10 #include <linux/err.h>
11 #include <linux/genalloc.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/platform_data/pm33xx.h>
18 #include <linux/platform_device.h>
19 #include <linux/sizes.h>
20 #include <linux/sram.h>
21 #include <linux/suspend.h>
22 #include <linux/ti-emif-sram.h>
23 #include <linux/wkup_m3_ipc.h>
24 
25 #include <asm/proc-fns.h>
26 #include <asm/suspend.h>
27 #include <asm/system_misc.h>
28 
29 #define AMX3_PM_SRAM_SYMBOL_OFFSET(sym) ((unsigned long)(sym) - \
30 					 (unsigned long)pm_sram->do_wfi)
31 
32 static int (*am33xx_do_wfi_sram)(unsigned long unused);
33 static phys_addr_t am33xx_do_wfi_sram_phys;
34 
35 static struct gen_pool *sram_pool, *sram_pool_data;
36 static unsigned long ocmcram_location, ocmcram_location_data;
37 
38 static struct am33xx_pm_platform_data *pm_ops;
39 static struct am33xx_pm_sram_addr *pm_sram;
40 
41 static struct device *pm33xx_dev;
42 static struct wkup_m3_ipc *m3_ipc;
43 
44 static u32 sram_suspend_address(unsigned long addr)
45 {
46 	return ((unsigned long)am33xx_do_wfi_sram +
47 		AMX3_PM_SRAM_SYMBOL_OFFSET(addr));
48 }
49 
50 #ifdef CONFIG_SUSPEND
51 static int am33xx_pm_suspend(suspend_state_t suspend_state)
52 {
53 	int i, ret = 0;
54 
55 	ret = pm_ops->soc_suspend((unsigned long)suspend_state,
56 				  am33xx_do_wfi_sram);
57 
58 	if (ret) {
59 		dev_err(pm33xx_dev, "PM: Kernel suspend failure\n");
60 	} else {
61 		i = m3_ipc->ops->request_pm_status(m3_ipc);
62 
63 		switch (i) {
64 		case 0:
65 			dev_info(pm33xx_dev,
66 				 "PM: Successfully put all powerdomains to target state\n");
67 			break;
68 		case 1:
69 			dev_err(pm33xx_dev,
70 				"PM: Could not transition all powerdomains to target state\n");
71 			ret = -1;
72 			break;
73 		default:
74 			dev_err(pm33xx_dev,
75 				"PM: CM3 returned unknown result = %d\n", i);
76 			ret = -1;
77 		}
78 	}
79 
80 	return ret;
81 }
82 
83 static int am33xx_pm_enter(suspend_state_t suspend_state)
84 {
85 	int ret = 0;
86 
87 	switch (suspend_state) {
88 	case PM_SUSPEND_MEM:
89 	case PM_SUSPEND_STANDBY:
90 		ret = am33xx_pm_suspend(suspend_state);
91 		break;
92 	default:
93 		ret = -EINVAL;
94 	}
95 
96 	return ret;
97 }
98 
99 static int am33xx_pm_begin(suspend_state_t state)
100 {
101 	int ret = -EINVAL;
102 
103 	switch (state) {
104 	case PM_SUSPEND_MEM:
105 		ret = m3_ipc->ops->prepare_low_power(m3_ipc, WKUP_M3_DEEPSLEEP);
106 		break;
107 	case PM_SUSPEND_STANDBY:
108 		ret = m3_ipc->ops->prepare_low_power(m3_ipc, WKUP_M3_STANDBY);
109 		break;
110 	}
111 
112 	return ret;
113 }
114 
115 static void am33xx_pm_end(void)
116 {
117 	m3_ipc->ops->finish_low_power(m3_ipc);
118 }
119 
120 static int am33xx_pm_valid(suspend_state_t state)
121 {
122 	switch (state) {
123 	case PM_SUSPEND_STANDBY:
124 	case PM_SUSPEND_MEM:
125 		return 1;
126 	default:
127 		return 0;
128 	}
129 }
130 
131 static const struct platform_suspend_ops am33xx_pm_ops = {
132 	.begin		= am33xx_pm_begin,
133 	.end		= am33xx_pm_end,
134 	.enter		= am33xx_pm_enter,
135 	.valid		= am33xx_pm_valid,
136 };
137 #endif /* CONFIG_SUSPEND */
138 
139 static void am33xx_pm_set_ipc_ops(void)
140 {
141 	u32 resume_address;
142 	int temp;
143 
144 	temp = ti_emif_get_mem_type();
145 	if (temp < 0) {
146 		dev_err(pm33xx_dev, "PM: Cannot determine memory type, no PM available\n");
147 		return;
148 	}
149 	m3_ipc->ops->set_mem_type(m3_ipc, temp);
150 
151 	/* Physical resume address to be used by ROM code */
152 	resume_address = am33xx_do_wfi_sram_phys +
153 			 *pm_sram->resume_offset + 0x4;
154 
155 	m3_ipc->ops->set_resume_address(m3_ipc, (void *)resume_address);
156 }
157 
158 static void am33xx_pm_free_sram(void)
159 {
160 	gen_pool_free(sram_pool, ocmcram_location, *pm_sram->do_wfi_sz);
161 	gen_pool_free(sram_pool_data, ocmcram_location_data,
162 		      sizeof(struct am33xx_pm_ro_sram_data));
163 }
164 
165 /*
166  * Push the minimal suspend-resume code to SRAM
167  */
168 static int am33xx_pm_alloc_sram(void)
169 {
170 	struct device_node *np;
171 	int ret = 0;
172 
173 	np = of_find_compatible_node(NULL, NULL, "ti,omap3-mpu");
174 	if (!np) {
175 		np = of_find_compatible_node(NULL, NULL, "ti,omap4-mpu");
176 		if (!np) {
177 			dev_err(pm33xx_dev, "PM: %s: Unable to find device node for mpu\n",
178 				__func__);
179 			return -ENODEV;
180 		}
181 	}
182 
183 	sram_pool = of_gen_pool_get(np, "pm-sram", 0);
184 	if (!sram_pool) {
185 		dev_err(pm33xx_dev, "PM: %s: Unable to get sram pool for ocmcram\n",
186 			__func__);
187 		ret = -ENODEV;
188 		goto mpu_put_node;
189 	}
190 
191 	sram_pool_data = of_gen_pool_get(np, "pm-sram", 1);
192 	if (!sram_pool_data) {
193 		dev_err(pm33xx_dev, "PM: %s: Unable to get sram data pool for ocmcram\n",
194 			__func__);
195 		ret = -ENODEV;
196 		goto mpu_put_node;
197 	}
198 
199 	ocmcram_location = gen_pool_alloc(sram_pool, *pm_sram->do_wfi_sz);
200 	if (!ocmcram_location) {
201 		dev_err(pm33xx_dev, "PM: %s: Unable to allocate memory from ocmcram\n",
202 			__func__);
203 		ret = -ENOMEM;
204 		goto mpu_put_node;
205 	}
206 
207 	ocmcram_location_data = gen_pool_alloc(sram_pool_data,
208 					       sizeof(struct emif_regs_amx3));
209 	if (!ocmcram_location_data) {
210 		dev_err(pm33xx_dev, "PM: Unable to allocate memory from ocmcram\n");
211 		gen_pool_free(sram_pool, ocmcram_location, *pm_sram->do_wfi_sz);
212 		ret = -ENOMEM;
213 	}
214 
215 mpu_put_node:
216 	of_node_put(np);
217 	return ret;
218 }
219 
220 static int am33xx_push_sram_idle(void)
221 {
222 	struct am33xx_pm_ro_sram_data ro_sram_data;
223 	int ret;
224 	u32 table_addr, ro_data_addr;
225 	void *copy_addr;
226 
227 	ro_sram_data.amx3_pm_sram_data_virt = ocmcram_location_data;
228 	ro_sram_data.amx3_pm_sram_data_phys =
229 		gen_pool_virt_to_phys(sram_pool_data, ocmcram_location_data);
230 
231 	/* Save physical address to calculate resume offset during pm init */
232 	am33xx_do_wfi_sram_phys = gen_pool_virt_to_phys(sram_pool,
233 							ocmcram_location);
234 
235 	am33xx_do_wfi_sram = sram_exec_copy(sram_pool, (void *)ocmcram_location,
236 					    pm_sram->do_wfi,
237 					    *pm_sram->do_wfi_sz);
238 	if (!am33xx_do_wfi_sram) {
239 		dev_err(pm33xx_dev,
240 			"PM: %s: am33xx_do_wfi copy to sram failed\n",
241 			__func__);
242 		return -ENODEV;
243 	}
244 
245 	table_addr =
246 		sram_suspend_address((unsigned long)pm_sram->emif_sram_table);
247 	ret = ti_emif_copy_pm_function_table(sram_pool, (void *)table_addr);
248 	if (ret) {
249 		dev_dbg(pm33xx_dev,
250 			"PM: %s: EMIF function copy failed\n", __func__);
251 		return -EPROBE_DEFER;
252 	}
253 
254 	ro_data_addr =
255 		sram_suspend_address((unsigned long)pm_sram->ro_sram_data);
256 	copy_addr = sram_exec_copy(sram_pool, (void *)ro_data_addr,
257 				   &ro_sram_data,
258 				   sizeof(ro_sram_data));
259 	if (!copy_addr) {
260 		dev_err(pm33xx_dev,
261 			"PM: %s: ro_sram_data copy to sram failed\n",
262 			__func__);
263 		return -ENODEV;
264 	}
265 
266 	return 0;
267 }
268 
269 static int am33xx_pm_probe(struct platform_device *pdev)
270 {
271 	struct device *dev = &pdev->dev;
272 	int ret;
273 
274 	if (!of_machine_is_compatible("ti,am33xx") &&
275 	    !of_machine_is_compatible("ti,am43"))
276 		return -ENODEV;
277 
278 	pm_ops = dev->platform_data;
279 	if (!pm_ops) {
280 		dev_err(dev, "PM: Cannot get core PM ops!\n");
281 		return -ENODEV;
282 	}
283 
284 	pm_sram = pm_ops->get_sram_addrs();
285 	if (!pm_sram) {
286 		dev_err(dev, "PM: Cannot get PM asm function addresses!!\n");
287 		return -ENODEV;
288 	}
289 
290 	pm33xx_dev = dev;
291 
292 	ret = am33xx_pm_alloc_sram();
293 	if (ret)
294 		return ret;
295 
296 	ret = am33xx_push_sram_idle();
297 	if (ret)
298 		goto err_free_sram;
299 
300 	m3_ipc = wkup_m3_ipc_get();
301 	if (!m3_ipc) {
302 		dev_dbg(dev, "PM: Cannot get wkup_m3_ipc handle\n");
303 		ret = -EPROBE_DEFER;
304 		goto err_free_sram;
305 	}
306 
307 	am33xx_pm_set_ipc_ops();
308 
309 #ifdef CONFIG_SUSPEND
310 	suspend_set_ops(&am33xx_pm_ops);
311 #endif /* CONFIG_SUSPEND */
312 
313 	ret = pm_ops->init();
314 	if (ret) {
315 		dev_err(dev, "Unable to call core pm init!\n");
316 		ret = -ENODEV;
317 		goto err_put_wkup_m3_ipc;
318 	}
319 
320 	return 0;
321 
322 err_put_wkup_m3_ipc:
323 	wkup_m3_ipc_put(m3_ipc);
324 err_free_sram:
325 	am33xx_pm_free_sram();
326 	pm33xx_dev = NULL;
327 	return ret;
328 }
329 
330 static int am33xx_pm_remove(struct platform_device *pdev)
331 {
332 	suspend_set_ops(NULL);
333 	wkup_m3_ipc_put(m3_ipc);
334 	am33xx_pm_free_sram();
335 	return 0;
336 }
337 
338 static struct platform_driver am33xx_pm_driver = {
339 	.driver = {
340 		.name   = "pm33xx",
341 	},
342 	.probe = am33xx_pm_probe,
343 	.remove = am33xx_pm_remove,
344 };
345 module_platform_driver(am33xx_pm_driver);
346 
347 MODULE_ALIAS("platform:pm33xx");
348 MODULE_LICENSE("GPL v2");
349 MODULE_DESCRIPTION("am33xx power management driver");
350