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