pm34xx.c 14.8 KB
Newer Older
1 2 3 4 5 6 7
/*
 * OMAP3 Power Management Routines
 *
 * Copyright (C) 2006-2008 Nokia Corporation
 * Tony Lindgren <tony@atomide.com>
 * Jouni Hogander
 *
8 9 10
 * Copyright (C) 2007 Texas Instruments, Inc.
 * Rajendra Nayak <rnayak@ti.com>
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * Copyright (C) 2005 Texas Instruments, Inc.
 * Richard Woodruff <r-woodruff2@ti.com>
 *
 * Based on pm.c for omap1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/pm.h>
#include <linux/suspend.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/err.h>
#include <linux/gpio.h>
28
#include <linux/clk.h>
29
#include <linux/delay.h>
30
#include <linux/slab.h>
31
#include <linux/omap-dma.h>
32
#include <linux/omap-gpmc.h>
33 34
#include <linux/platform_data/gpio-omap.h>

35
#include <trace/events/power.h>
36

37
#include <asm/fncpy.h>
38
#include <asm/suspend.h>
39
#include <asm/system_misc.h>
40

41
#include "clockdomain.h"
42
#include "powerdomain.h"
43
#include "soc.h"
44
#include "common.h"
45
#include "cm3xxx.h"
46 47
#include "cm-regbits-34xx.h"
#include "prm-regbits-34xx.h"
48
#include "prm3xxx.h"
49
#include "pm.h"
50
#include "sdrc.h"
51
#include "sram.h"
52
#include "control.h"
53
#include "vc.h"
54

55 56 57
/* pm34xx errata defined in pm.h */
u16 pm34xx_errata;

58 59 60
struct power_state {
	struct powerdomain *pwrdm;
	u32 next_state;
61
#ifdef CONFIG_SUSPEND
62
	u32 saved_state;
63
#endif
64 65 66 67 68
	struct list_head node;
};

static LIST_HEAD(pwrst_list);

69
static int (*_omap_save_secure_sram)(u32 *addr);
70
void (*omap3_do_wfi_sram)(void);
71

72 73
static struct powerdomain *mpu_pwrdm, *neon_pwrdm;
static struct powerdomain *core_pwrdm, *per_pwrdm;
74

75 76
static void omap3_core_save_context(void)
{
77
	omap3_ctrl_save_padconf();
78 79 80

	/*
	 * Force write last pad into memory, as this can fail in some
81
	 * cases according to errata 1.157, 1.185
82 83 84 85
	 */
	omap_ctrl_writel(omap_ctrl_readl(OMAP343X_PADCONF_ETK_D14),
		OMAP343X_CONTROL_MEM_WKUP + 0x2a0);

86 87 88 89 90 91
	/* Save the Interrupt controller context */
	omap_intc_save_context();
	/* Save the GPMC context */
	omap3_gpmc_save_context();
	/* Save the system control module context, padconf already save above*/
	omap3_control_save_context();
92
	omap_dma_global_context_save();
93 94 95 96 97 98 99 100 101 102
}

static void omap3_core_restore_context(void)
{
	/* Restore the control module context, padconf restored by h/w */
	omap3_control_restore_context();
	/* Restore the GPMC context */
	omap3_gpmc_restore_context();
	/* Restore the interrupt controller context */
	omap_intc_restore_context();
103
	omap_dma_global_context_restore();
104 105
}

106 107 108 109 110 111
/*
 * FIXME: This function should be called before entering off-mode after
 * OMAP3 secure services have been accessed. Currently it is only called
 * once during boot sequence, but this works as we are not using secure
 * services.
 */
112
static void omap3_save_secure_ram_context(void)
113 114
{
	u32 ret;
115
	int mpu_next_state = pwrdm_read_next_pwrst(mpu_pwrdm);
116 117 118 119 120 121 122 123

	if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
		/*
		 * MPU next state must be set to POWER_ON temporarily,
		 * otherwise the WFI executed inside the ROM code
		 * will hang the system.
		 */
		pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_ON);
124
		ret = _omap_save_secure_sram((u32 *)(unsigned long)
125
				__pa(omap3_secure_ram_storage));
126
		pwrdm_set_next_pwrst(mpu_pwrdm, mpu_next_state);
127 128
		/* Following is for error tracking, it should not happen */
		if (ret) {
129
			pr_err("save_secure_sram() returns %08x\n", ret);
130 131 132 133 134 135
			while (1)
				;
		}
	}
}

136
static irqreturn_t _prcm_int_handle_io(int irq, void *unused)
137 138 139
{
	int c;

140 141
	c = omap3xxx_prm_clear_mod_irqs(WKUP_MOD, 1, OMAP3430_ST_IO_MASK |
					OMAP3430_ST_IO_CHAIN_MASK);
142

143
	return c ? IRQ_HANDLED : IRQ_NONE;
144
}
145

146
static irqreturn_t _prcm_int_handle_wakeup(int irq, void *unused)
147
{
148
	int c;
149

150 151 152 153 154
	/*
	 * Clear all except ST_IO and ST_IO_CHAIN for wkup module,
	 * these are handled in a separate handler to avoid acking
	 * IO events before parsing in mux code
	 */
155
	c = omap3xxx_prm_clear_mod_irqs(WKUP_MOD, 1,
156 157 158 159
					~(OMAP3430_ST_IO_MASK |
					  OMAP3430_ST_IO_CHAIN_MASK));
	c += omap3xxx_prm_clear_mod_irqs(CORE_MOD, 1, ~0);
	c += omap3xxx_prm_clear_mod_irqs(OMAP3430_PER_MOD, 1, ~0);
160
	if (omap_rev() > OMAP3430_REV_ES1_0) {
161 162 163
		c += omap3xxx_prm_clear_mod_irqs(CORE_MOD, 3, ~0);
		c += omap3xxx_prm_clear_mod_irqs(OMAP3430ES2_USBHOST_MOD, 1,
						 ~0);
164
	}
165

166
	return c ? IRQ_HANDLED : IRQ_NONE;
167 168
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
static void omap34xx_save_context(u32 *save)
{
	u32 val;

	/* Read Auxiliary Control Register */
	asm("mrc p15, 0, %0, c1, c0, 1" : "=r" (val));
	*save++ = 1;
	*save++ = val;

	/* Read L2 AUX ctrl register */
	asm("mrc p15, 1, %0, c9, c0, 2" : "=r" (val));
	*save++ = 1;
	*save++ = val;
}

184
static int omap34xx_do_sram_idle(unsigned long save_state)
185
{
186
	omap34xx_cpu_suspend(save_state);
187
	return 0;
188 189
}

190
void omap_sram_idle(void)
191 192 193 194 195 196 197
{
	/* Variable to tell what needs to be saved and restored
	 * in omap_sram_idle*/
	/* save_state = 0 => Nothing to save and restored */
	/* save_state = 1 => Only L1 and logic lost */
	/* save_state = 2 => Only L2 lost */
	/* save_state = 3 => L1, L2 and logic lost */
198 199 200 201
	int save_state = 0;
	int mpu_next_state = PWRDM_POWER_ON;
	int per_next_state = PWRDM_POWER_ON;
	int core_next_state = PWRDM_POWER_ON;
202
	int per_going_off;
203
	int core_prev_state;
204
	u32 sdrc_pwr = 0;
205 206 207

	mpu_next_state = pwrdm_read_next_pwrst(mpu_pwrdm);
	switch (mpu_next_state) {
208
	case PWRDM_POWER_ON:
209 210 211 212
	case PWRDM_POWER_RET:
		/* No need to save context */
		save_state = 0;
		break;
R
Rajendra Nayak 已提交
213 214 215
	case PWRDM_POWER_OFF:
		save_state = 3;
		break;
216 217
	default:
		/* Invalid state */
218
		pr_err("Invalid mpu state in sram_idle\n");
219 220
		return;
	}
221

222 223
	/* NEON control */
	if (pwrdm_read_pwrst(neon_pwrdm) == PWRDM_POWER_ON)
224
		pwrdm_set_next_pwrst(neon_pwrdm, mpu_next_state);
225

226
	/* Enable IO-PAD and IO-CHAIN wakeups */
227
	per_next_state = pwrdm_read_next_pwrst(per_pwrdm);
228
	core_next_state = pwrdm_read_next_pwrst(core_pwrdm);
229

230
	pwrdm_pre_transition(NULL);
231

232
	/* PER */
233
	if (per_next_state < PWRDM_POWER_ON) {
234 235
		per_going_off = (per_next_state == PWRDM_POWER_OFF) ? 1 : 0;
		omap2_gpio_prepare_for_idle(per_going_off);
236 237 238
	}

	/* CORE */
239
	if (core_next_state < PWRDM_POWER_ON) {
240 241
		if (core_next_state == PWRDM_POWER_OFF) {
			omap3_core_save_context();
242
			omap3_cm_save_context();
243
		}
244
	}
245

246 247 248
	/* Configure PMIC signaling for I2C4 or sys_off_mode */
	omap3_vc_set_pmic_signaling(core_next_state);

249
	omap3_intc_prepare_idle();
250

251
	/*
252 253 254 255 256 257 258 259
	 * On EMU/HS devices ROM code restores a SRDC value
	 * from scratchpad which has automatic self refresh on timeout
	 * of AUTO_CNT = 1 enabled. This takes care of erratum ID i443.
	 * Hence store/restore the SDRC_POWER register here.
	 */
	if (cpu_is_omap3430() && omap_rev() >= OMAP3430_REV_ES3_0 &&
	    (omap_type() == OMAP2_DEVICE_TYPE_EMU ||
	     omap_type() == OMAP2_DEVICE_TYPE_SEC) &&
260
	    core_next_state == PWRDM_POWER_OFF)
261 262
		sdrc_pwr = sdrc_read_reg(SDRC_POWER);

R
Rajendra Nayak 已提交
263
	/*
264 265 266
	 * omap3_arm_context is the location where some ARM context
	 * get saved. The rest is placed on the stack, and restored
	 * from there before resuming.
R
Rajendra Nayak 已提交
267
	 */
268 269
	if (save_state)
		omap34xx_save_context(omap3_arm_context);
270
	if (save_state == 1 || save_state == 3)
271
		cpu_suspend(save_state, omap34xx_do_sram_idle);
272 273
	else
		omap34xx_do_sram_idle(save_state);
274

275
	/* Restore normal SDRC POWER settings */
276 277 278
	if (cpu_is_omap3430() && omap_rev() >= OMAP3430_REV_ES3_0 &&
	    (omap_type() == OMAP2_DEVICE_TYPE_EMU ||
	     omap_type() == OMAP2_DEVICE_TYPE_SEC) &&
279 280 281
	    core_next_state == PWRDM_POWER_OFF)
		sdrc_write_reg(sdrc_pwr, SDRC_POWER);

282
	/* CORE */
283
	if (core_next_state < PWRDM_POWER_ON) {
284 285 286
		core_prev_state = pwrdm_read_prev_pwrst(core_pwrdm);
		if (core_prev_state == PWRDM_POWER_OFF) {
			omap3_core_restore_context();
287
			omap3_cm_restore_context();
288
			omap3_sram_restore_context();
289
			omap2_sms_restore_context();
290
		}
291
	}
292
	omap3_intc_resume_idle();
293

294 295
	pwrdm_post_transition(NULL);

296
	/* PER */
297
	if (per_next_state < PWRDM_POWER_ON)
298
		omap2_gpio_resume_after_idle();
299 300 301 302
}

static void omap3_pm_idle(void)
{
303
	if (omap_irq_pending())
304
		return;
305

306 307
	trace_cpu_idle(1, smp_processor_id());

308 309
	omap_sram_idle();

310
	trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
311 312
}

313
#ifdef CONFIG_SUSPEND
314 315 316 317 318 319 320 321 322 323
static int omap3_pm_suspend(void)
{
	struct power_state *pwrst;
	int state, ret = 0;

	/* Read current next_pwrsts */
	list_for_each_entry(pwrst, &pwrst_list, node)
		pwrst->saved_state = pwrdm_read_next_pwrst(pwrst->pwrdm);
	/* Set ones wanted by suspend */
	list_for_each_entry(pwrst, &pwrst_list, node) {
324
		if (omap_set_pwrdm_state(pwrst->pwrdm, pwrst->next_state))
325 326 327 328 329
			goto restore;
		if (pwrdm_clear_all_prev_pwrst(pwrst->pwrdm))
			goto restore;
	}

330 331
	omap3_intc_suspend();

332 333 334 335 336 337 338
	omap_sram_idle();

restore:
	/* Restore next_pwrsts */
	list_for_each_entry(pwrst, &pwrst_list, node) {
		state = pwrdm_read_prev_pwrst(pwrst->pwrdm);
		if (state > pwrst->next_state) {
P
Paul Walmsley 已提交
339 340
			pr_info("Powerdomain (%s) didn't enter target state %d\n",
				pwrst->pwrdm->name, pwrst->next_state);
341 342
			ret = -1;
		}
343
		omap_set_pwrdm_state(pwrst->pwrdm, pwrst->saved_state);
344 345
	}
	if (ret)
346
		pr_err("Could not enter target state in pm_suspend\n");
347
	else
348
		pr_info("Successfully put all powerdomains to target state\n");
349 350 351

	return ret;
}
352 353
#else
#define omap3_pm_suspend NULL
354
#endif /* CONFIG_SUSPEND */
355

356 357
static void __init prcm_setup_regs(void)
{
358
	omap3_ctrl_init();
359

360
	omap3_prm_init_pm(cpu_is_omap3630(), omap3_has_iva());
361 362
}

363 364 365 366 367 368 369 370 371 372 373
void omap3_pm_off_mode_enable(int enable)
{
	struct power_state *pwrst;
	u32 state;

	if (enable)
		state = PWRDM_POWER_OFF;
	else
		state = PWRDM_POWER_RET;

	list_for_each_entry(pwrst, &pwrst_list, node) {
374 375 376 377
		if (IS_PM34XX_ERRATUM(PM_SDRC_WAKEUP_ERRATUM_i583) &&
				pwrst->pwrdm == core_pwrdm &&
				state == PWRDM_POWER_OFF) {
			pwrst->next_state = PWRDM_POWER_RET;
378
			pr_warn("%s: Core OFF disabled due to errata i583\n",
379 380 381 382 383
				__func__);
		} else {
			pwrst->next_state = state;
		}
		omap_set_pwrdm_state(pwrst->pwrdm, pwrst->next_state);
384 385 386
	}
}

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
int omap3_pm_get_suspend_state(struct powerdomain *pwrdm)
{
	struct power_state *pwrst;

	list_for_each_entry(pwrst, &pwrst_list, node) {
		if (pwrst->pwrdm == pwrdm)
			return pwrst->next_state;
	}
	return -EINVAL;
}

int omap3_pm_set_suspend_state(struct powerdomain *pwrdm, int state)
{
	struct power_state *pwrst;

	list_for_each_entry(pwrst, &pwrst_list, node) {
		if (pwrst->pwrdm == pwrdm) {
			pwrst->next_state = state;
			return 0;
		}
	}
	return -EINVAL;
}

411
static int __init pwrdms_setup(struct powerdomain *pwrdm, void *unused)
412 413 414 415 416 417
{
	struct power_state *pwrst;

	if (!pwrdm->pwrsts)
		return 0;

418
	pwrst = kmalloc(sizeof(struct power_state), GFP_ATOMIC);
419 420 421 422 423 424 425 426 427
	if (!pwrst)
		return -ENOMEM;
	pwrst->pwrdm = pwrdm;
	pwrst->next_state = PWRDM_POWER_RET;
	list_add(&pwrst->node, &pwrst_list);

	if (pwrdm_has_hdwr_sar(pwrdm))
		pwrdm_enable_hdwr_sar(pwrdm);

428
	return omap_set_pwrdm_state(pwrst->pwrdm, pwrst->next_state);
429 430
}

431 432 433 434 435 436 437
/*
 * Push functions to SRAM
 *
 * The minimum set of functions is pushed to SRAM for execution:
 * - omap3_do_wfi for erratum i581 WA,
 * - save_secure_ram_context for security extensions.
 */
438 439
void omap_push_sram_idle(void)
{
440 441
	omap3_do_wfi_sram = omap_sram_push(omap3_do_wfi, omap3_do_wfi_sz);

442 443 444
	if (omap_type() != OMAP2_DEVICE_TYPE_GP)
		_omap_save_secure_sram = omap_sram_push(save_secure_ram_context,
				save_secure_ram_context_sz);
445 446
}

447 448
static void __init pm_errata_configure(void)
{
449
	if (cpu_is_omap3630()) {
450
		pm34xx_errata |= PM_RTA_ERRATUM_i608;
451 452
		/* Enable the l2 cache toggling in sleep logic */
		enable_omap3630_toggle_l2_on_restore();
453
		if (omap_rev() < OMAP3630_REV_ES1_2)
454 455 456 457
			pm34xx_errata |= (PM_SDRC_WAKEUP_ERRATUM_i583 |
					  PM_PER_MEMORIES_ERRATUM_i582);
	} else if (cpu_is_omap34xx()) {
		pm34xx_errata |= PM_PER_MEMORIES_ERRATUM_i582;
458
	}
459 460
}

461
int __init omap3_pm_init(void)
462 463
{
	struct power_state *pwrst, *tmp;
464
	struct clockdomain *neon_clkdm, *mpu_clkdm, *per_clkdm, *wkup_clkdm;
465 466
	int ret;

467
	if (!omap3_has_io_chain_ctrl())
468
		pr_warn("PM: no software I/O chain control; some wakeups may be lost\n");
469

470 471
	pm_errata_configure();

472 473 474 475
	/* XXX prcm_setup_regs needs to be before enabling hw
	 * supervised mode for powerdomains */
	prcm_setup_regs();

476 477 478 479 480 481 482 483 484 485 486 487
	ret = request_irq(omap_prcm_event_to_irq("wkup"),
		_prcm_int_handle_wakeup, IRQF_NO_SUSPEND, "pm_wkup", NULL);

	if (ret) {
		pr_err("pm: Failed to request pm_wkup irq\n");
		goto err1;
	}

	/* IO interrupt is shared with mux code */
	ret = request_irq(omap_prcm_event_to_irq("io"),
		_prcm_int_handle_io, IRQF_SHARED | IRQF_NO_SUSPEND, "pm_io",
		omap3_pm_init);
488
	enable_irq(omap_prcm_event_to_irq("io"));
489

490
	if (ret) {
491
		pr_err("pm: Failed to request pm_io irq\n");
492
		goto err2;
493 494
	}

495
	ret = pwrdm_for_each(pwrdms_setup, NULL);
496
	if (ret) {
497
		pr_err("Failed to setup powerdomains\n");
498
		goto err3;
499 500
	}

501
	(void) clkdm_for_each(omap_pm_clkdms_setup, NULL);
502 503 504

	mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
	if (mpu_pwrdm == NULL) {
505
		pr_err("Failed to get mpu_pwrdm\n");
506 507
		ret = -EINVAL;
		goto err3;
508 509
	}

510 511 512 513
	neon_pwrdm = pwrdm_lookup("neon_pwrdm");
	per_pwrdm = pwrdm_lookup("per_pwrdm");
	core_pwrdm = pwrdm_lookup("core_pwrdm");

514 515
	neon_clkdm = clkdm_lookup("neon_clkdm");
	mpu_clkdm = clkdm_lookup("mpu_clkdm");
516 517
	per_clkdm = clkdm_lookup("per_clkdm");
	wkup_clkdm = clkdm_lookup("wkup_clkdm");
518

519
	omap_common_suspend_init(omap3_pm_suspend);
520

521
	arm_pm_idle = omap3_pm_idle;
522
	omap3_idle_init();
523

524 525 526 527 528 529 530 531
	/*
	 * RTA is disabled during initialization as per erratum i608
	 * it is safer to disable RTA by the bootloader, but we would like
	 * to be doubly sure here and prevent any mishaps.
	 */
	if (IS_PM34XX_ERRATUM(PM_RTA_ERRATUM_i608))
		omap3630_ctrl_disable_rta();

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	/*
	 * The UART3/4 FIFO and the sidetone memory in McBSP2/3 are
	 * not correctly reset when the PER powerdomain comes back
	 * from OFF or OSWR when the CORE powerdomain is kept active.
	 * See OMAP36xx Erratum i582 "PER Domain reset issue after
	 * Domain-OFF/OSWR Wakeup".  This wakeup dependency is not a
	 * complete workaround.  The kernel must also prevent the PER
	 * powerdomain from going to OSWR/OFF while the CORE
	 * powerdomain is not going to OSWR/OFF.  And if PER last
	 * power state was off while CORE last power state was ON, the
	 * UART3/4 and McBSP2/3 SIDETONE devices need to run a
	 * self-test using their loopback tests; if that fails, those
	 * devices are unusable until the PER/CORE can complete a transition
	 * from ON to OSWR/OFF and then back to ON.
	 *
	 * XXX Technically this workaround is only needed if off-mode
	 * or OSWR is enabled.
	 */
	if (IS_PM34XX_ERRATUM(PM_PER_MEMORIES_ERRATUM_i582))
		clkdm_add_wkdep(per_clkdm, wkup_clkdm);

553
	clkdm_add_wkdep(neon_clkdm, mpu_clkdm);
554 555 556 557
	if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
		omap3_secure_ram_storage =
			kmalloc(0x803F, GFP_KERNEL);
		if (!omap3_secure_ram_storage)
P
Paul Walmsley 已提交
558
			pr_err("Memory allocation failed when allocating for secure sram context\n");
559 560 561 562

		local_irq_disable();

		omap_dma_global_context_save();
563
		omap3_save_secure_ram_context();
564 565 566
		omap_dma_global_context_restore();

		local_irq_enable();
567 568
	}

569
	omap3_save_scratchpad_contents();
570
	return ret;
571 572

err3:
573 574 575 576
	list_for_each_entry_safe(pwrst, tmp, &pwrst_list, node) {
		list_del(&pwrst->node);
		kfree(pwrst);
	}
577 578 579 580
	free_irq(omap_prcm_event_to_irq("io"), omap3_pm_init);
err2:
	free_irq(omap_prcm_event_to_irq("wkup"), NULL);
err1:
581 582
	return ret;
}