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