pm_slowclock.S 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * arch/arm/mach-at91/pm_slow_clock.S
 *
 *  Copyright (C) 2006 Savin Zlobec
 *
 * AT91SAM9 support:
 *  Copyright (C) 2007 Anti Sullin <anti.sullin@artecdesign.ee
 *
 * 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/linkage.h>
#include <mach/hardware.h>
#include <mach/at91_pmc.h>

19
#if defined(CONFIG_ARCH_AT91RM9200)
20
#include <mach/at91rm9200_mc.h>
21
#elif defined(CONFIG_ARCH_AT91SAM9G45)
22
#include <mach/at91sam9_ddrsdr.h>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#else
#include <mach/at91sam9_sdramc.h>
#endif


#ifdef CONFIG_ARCH_AT91SAM9263
/*
 * FIXME either or both the SDRAM controllers (EB0, EB1) might be in use;
 * handle those cases both here and in the Suspend-To-RAM support.
 */
#warning Assuming EB1 SDRAM controller is *NOT* used
#endif

/*
 * When SLOWDOWN_MASTER_CLOCK is defined we will also slow down the Master
 * clock during suspend by adjusting its prescalar and divisor.
 * NOTE: This hasn't been shown to be stable on SAM9s; and on the RM9200 there
 *       are errata regarding adjusting the prescalar and divisor.
 */
#undef SLOWDOWN_MASTER_CLOCK

#define MCKRDY_TIMEOUT		1000
#define MOSCRDY_TIMEOUT 	1000
#define PLLALOCK_TIMEOUT	1000
#define PLLBLOCK_TIMEOUT	1000

49 50 51
pmc	.req	r0
sdramc	.req	r1
ramc1	.req	r2
52 53
tmp1	.req	r3
tmp2	.req	r4
54 55 56 57 58

/*
 * Wait until master clock is ready (after switching master clock source)
 */
	.macro wait_mckrdy
59 60 61
	mov	tmp2, #MCKRDY_TIMEOUT
1:	sub	tmp2, tmp2, #1
	cmp	tmp2, #0
62
	beq	2f
63 64
	ldr	tmp1, [pmc, #(AT91_PMC_SR - AT91_PMC)]
	tst	tmp1, #AT91_PMC_MCKRDY
65 66 67 68 69 70 71 72
	beq	1b
2:
	.endm

/*
 * Wait until master oscillator has stabilized.
 */
	.macro wait_moscrdy
73 74 75
	mov	tmp2, #MOSCRDY_TIMEOUT
1:	sub	tmp2, tmp2, #1
	cmp	tmp2, #0
76
	beq	2f
77 78
	ldr	tmp1, [pmc, #(AT91_PMC_SR - AT91_PMC)]
	tst	tmp1, #AT91_PMC_MOSCS
79 80 81 82 83 84 85 86
	beq	1b
2:
	.endm

/*
 * Wait until PLLA has locked.
 */
	.macro wait_pllalock
87 88 89
	mov	tmp2, #PLLALOCK_TIMEOUT
1:	sub	tmp2, tmp2, #1
	cmp	tmp2, #0
90
	beq	2f
91 92
	ldr	tmp1, [pmc, #(AT91_PMC_SR - AT91_PMC)]
	tst	tmp1, #AT91_PMC_LOCKA
93 94 95 96 97 98 99 100
	beq	1b
2:
	.endm

/*
 * Wait until PLLB has locked.
 */
	.macro wait_pllblock
101 102 103
	mov	tmp2, #PLLBLOCK_TIMEOUT
1:	sub	tmp2, tmp2, #1
	cmp	tmp2, #0
104
	beq	2f
105 106
	ldr	tmp1, [pmc, #(AT91_PMC_SR - AT91_PMC)]
	tst	tmp1, #AT91_PMC_LOCKB
107 108 109 110 111 112
	beq	1b
2:
	.endm

	.text

113
/* void at91_slow_clock(void __iomem *pmc, void __iomem *sdramc, void __iomem *ramc1) */
114 115
ENTRY(at91_slow_clock)
	/* Save registers on stack */
116
	stmfd	sp!, {r3 - r12, lr}
117 118 119

	/*
	 * Register usage:
120 121 122
	 *  R0 = Base address of AT91_PMC
	 *  R1 = Base address of RAM Controller (SDRAM, DDRSDR, or AT91_SYS)
	 *  R2 = Base address of second RAM Controller or 0 if not present
123 124 125 126 127
	 *  R3 = temporary register
	 *  R4 = temporary register
	 */

	/* Drain write buffer */
128 129
	mov	tmp1, #0
	mcr	p15, 0, tmp1, c7, c10, 4
130 131 132

#ifdef CONFIG_ARCH_AT91RM9200
	/* Put SDRAM in self-refresh mode */
133 134
	mov	tmp1, #1
	str	tmp1, [sdramc, #AT91_SDRAMC_SRR]
135
#elif defined(CONFIG_ARCH_AT91SAM9G45)
136

137
	/* prepare for DDRAM self-refresh mode */
138 139 140 141
	ldr	tmp1, [sdramc, #AT91_DDRSDRC_LPR]
	str	tmp1, .saved_sam9_lpr
	bic	tmp1, #AT91_DDRSDRC_LPCB
	orr	tmp1, #AT91_DDRSDRC_LPCB_SELF_REFRESH
142 143

	/* figure out if we use the second ram controller */
144 145 146 147 148
	cmp	ramc1, #0
	ldrne	tmp2, [ramc1, #AT91_DDRSDRC_LPR]
	strne	tmp2, .saved_sam9_lpr1
	bicne	tmp2, #AT91_DDRSDRC_LPCB
	orrne	tmp2, #AT91_DDRSDRC_LPCB_SELF_REFRESH
149 150

	/* Enable DDRAM self-refresh mode */
151 152
	str	tmp1, [sdramc, #AT91_DDRSDRC_LPR]
	strne	tmp2, [ramc1, #AT91_DDRSDRC_LPR]
153 154
#else
	/* Enable SDRAM self-refresh mode */
155 156
	ldr	tmp1, [sdramc, #AT91_SDRAMC_LPR]
	str	tmp1, .saved_sam9_lpr
157

158 159 160
	bic	tmp1, #AT91_SDRAMC_LPCB
	orr	tmp1, #AT91_SDRAMC_LPCB_SELF_REFRESH
	str	tmp1, [sdramc, #AT91_SDRAMC_LPR]
161 162 163
#endif

	/* Save Master clock setting */
164 165
	ldr	tmp1, [pmc, #(AT91_PMC_MCKR - AT91_PMC)]
	str	tmp1, .saved_mckr
166 167 168 169

	/*
	 * Set the Master clock source to slow clock
	 */
170 171
	bic	tmp1, tmp1, #AT91_PMC_CSS
	str	tmp1, [pmc, #(AT91_PMC_MCKR - AT91_PMC)]
172 173 174 175 176 177 178 179 180

	wait_mckrdy

#ifdef SLOWDOWN_MASTER_CLOCK
	/*
	 * Set the Master Clock PRES and MDIV fields.
	 *
	 * See AT91RM9200 errata #27 and #28 for details.
	 */
181 182
	mov	tmp1, #0
	str	tmp1, [pmc, #(AT91_PMC_MCKR - AT91_PMC)]
183 184 185 186 187

	wait_mckrdy
#endif

	/* Save PLLA setting and disable it */
188 189
	ldr	tmp1, [pmc, #(AT91_CKGR_PLLAR - AT91_PMC)]
	str	tmp1, .saved_pllar
190

191 192 193
	mov	tmp1, #AT91_PMC_PLLCOUNT
	orr	tmp1, tmp1, #(1 << 29)		/* bit 29 always set */
	str	tmp1, [pmc, #(AT91_CKGR_PLLAR - AT91_PMC)]
194 195

	/* Save PLLB setting and disable it */
196 197
	ldr	tmp1, [pmc, #(AT91_CKGR_PLLBR - AT91_PMC)]
	str	tmp1, .saved_pllbr
198

199 200
	mov	tmp1, #AT91_PMC_PLLCOUNT
	str	tmp1, [pmc, #(AT91_CKGR_PLLBR - AT91_PMC)]
201 202

	/* Turn off the main oscillator */
203 204 205
	ldr	tmp1, [pmc, #(AT91_CKGR_MOR - AT91_PMC)]
	bic	tmp1, tmp1, #AT91_PMC_MOSCEN
	str	tmp1, [pmc, #(AT91_CKGR_MOR - AT91_PMC)]
206 207

	/* Wait for interrupt */
208
	mcr	p15, 0, tmp1, c7, c0, 4
209 210

	/* Turn on the main oscillator */
211 212 213
	ldr	tmp1, [pmc, #(AT91_CKGR_MOR - AT91_PMC)]
	orr	tmp1, tmp1, #AT91_PMC_MOSCEN
	str	tmp1, [pmc, #(AT91_CKGR_MOR - AT91_PMC)]
214 215 216 217

	wait_moscrdy

	/* Restore PLLB setting */
218 219
	ldr	tmp1, .saved_pllbr
	str	tmp1, [pmc, #(AT91_CKGR_PLLBR - AT91_PMC)]
220

221
	tst	tmp1, #(AT91_PMC_MUL &  0xff0000)
222
	bne	1f
223
	tst	tmp1, #(AT91_PMC_MUL & ~0xff0000)
224 225
	beq	2f
1:
226
	wait_pllblock
227
2:
228 229

	/* Restore PLLA setting */
230 231
	ldr	tmp1, .saved_pllar
	str	tmp1, [pmc, #(AT91_CKGR_PLLAR - AT91_PMC)]
232

233
	tst	tmp1, #(AT91_PMC_MUL &  0xff0000)
234
	bne	3f
235
	tst	tmp1, #(AT91_PMC_MUL & ~0xff0000)
236 237
	beq	4f
3:
238
	wait_pllalock
239
4:
240 241 242 243 244 245 246 247

#ifdef SLOWDOWN_MASTER_CLOCK
	/*
	 * First set PRES if it was not 0,
	 * than set CSS and MDIV fields.
	 *
	 * See AT91RM9200 errata #27 and #28 for details.
	 */
248 249
	ldr	tmp1, .saved_mckr
	tst	tmp1, #AT91_PMC_PRES
250
	beq	2f
251 252
	and	tmp1, tmp1, #AT91_PMC_PRES
	str	tmp1, [pmc, #(AT91_PMC_MCKR - AT91_PMC)]
253 254 255 256 257 258 259

	wait_mckrdy
#endif

	/*
	 * Restore master clock setting
	 */
260 261
2:	ldr	tmp1, .saved_mckr
	str	tmp1, [pmc, #(AT91_PMC_MCKR - AT91_PMC)]
262 263 264 265 266

	wait_mckrdy

#ifdef CONFIG_ARCH_AT91RM9200
	/* Do nothing - self-refresh is automatically disabled. */
267
#elif defined(CONFIG_ARCH_AT91SAM9G45)
268
	/* Restore LPR on AT91 with DDRAM */
269 270
	ldr	tmp1, .saved_sam9_lpr
	str	tmp1, [sdramc, #AT91_DDRSDRC_LPR]
271 272

	/* if we use the second ram controller */
273 274 275
	cmp	ramc1, #0
	ldrne	tmp2, .saved_sam9_lpr1
	strne	tmp2, [ramc1, #AT91_DDRSDRC_LPR]
276

277
#else
278
	/* Restore LPR on AT91 with SDRAM */
279 280
	ldr	tmp1, .saved_sam9_lpr
	str	tmp1, [sdramc, #AT91_SDRAMC_LPR]
281 282 283
#endif

	/* Restore registers, and return */
284
	ldmfd	sp!, {r3 - r12, pc}
285 286 287 288 289 290 291 292 293 294 295 296 297 298


.saved_mckr:
	.word 0

.saved_pllar:
	.word 0

.saved_pllbr:
	.word 0

.saved_sam9_lpr:
	.word 0

299 300 301
.saved_sam9_lpr1:
	.word 0

302 303
ENTRY(at91_slow_clock_sz)
	.word .-at91_slow_clock