1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * arch/arm/mach-at91/pm_slow_clock.S
5 * Copyright (C) 2006 Savin Zlobec
8 * Copyright (C) 2007 Anti Sullin <anti.sullin@artecdesign.ee>
10 #include <linux/linkage.h>
11 #include <linux/clk/at91_pmc.h>
13 #include "pm_data-offsets.h"
15 #define SRAMC_SELF_FRESH_ACTIVE 0x01
16 #define SRAMC_SELF_FRESH_EXIT 0x00
24 * Wait until master clock is ready (after switching master clock source)
27 1: ldr tmp1, [pmc, #AT91_PMC_SR]
28 tst tmp1, #AT91_PMC_MCKRDY
33 * Wait until master oscillator has stabilized.
36 1: ldr tmp1, [pmc, #AT91_PMC_SR]
37 tst tmp1, #AT91_PMC_MOSCS
42 * Wait for main oscillator selection is done
45 1: ldr tmp1, [pmc, #AT91_PMC_SR]
46 tst tmp1, #AT91_PMC_MOSCSELS
51 * Put the processor to enter the idle state
55 #if defined(CONFIG_CPU_V7)
56 mov tmp1, #AT91_PMC_PCK
57 str tmp1, [pmc, #AT91_PMC_SCDR]
61 wfi @ Wait For Interrupt
63 mcr p15, 0, tmp1, c7, c0, 4
73 * void at91_suspend_sram_fn(struct at91_pm_data*)
75 * @r0: base address of struct at91_pm_data
77 /* at91_pm_suspend_in_sram must be 8-byte aligned per the requirements of fncpy() */
79 ENTRY(at91_pm_suspend_in_sram)
80 /* Save registers on stack */
81 stmfd sp!, {r4 - r12, lr}
83 /* Drain write buffer */
85 mcr p15, 0, tmp1, c7, c10, 4
87 ldr tmp1, [r0, #PM_DATA_PMC]
89 ldr tmp1, [r0, #PM_DATA_RAMC0]
91 ldr tmp1, [r0, #PM_DATA_RAMC1]
92 str tmp1, .sramc1_base
93 ldr tmp1, [r0, #PM_DATA_MEMCTRL]
95 ldr tmp1, [r0, #PM_DATA_MODE]
97 ldr tmp1, [r0, #PM_DATA_PMC_MCKR_OFFSET]
98 str tmp1, .mckr_offset
99 ldr tmp1, [r0, #PM_DATA_PMC_VERSION]
100 str tmp1, .pmc_version
101 /* Both ldrne below are here to preload their address in the TLB */
102 ldr tmp1, [r0, #PM_DATA_SHDWC]
105 ldrne tmp2, [tmp1, #0]
106 ldr tmp1, [r0, #PM_DATA_SFRBU]
109 ldrne tmp2, [tmp1, #0x10]
111 /* Active the self-refresh mode */
112 mov r0, #SRAMC_SELF_FRESH_ACTIVE
113 bl at91_sramc_self_refresh
116 cmp r0, #AT91_PM_STANDBY
118 cmp r0, #AT91_PM_BACKUP
125 /* Wait for interrupt */
135 /* Exit the self-refresh mode */
136 mov r0, #SRAMC_SELF_FRESH_EXIT
137 bl at91_sramc_self_refresh
139 /* Restore registers, and return */
140 ldmfd sp!, {r4 - r12, pc}
141 ENDPROC(at91_pm_suspend_in_sram)
143 ENTRY(at91_backup_mode)
144 /* Switch the master clock source to slow clock. */
146 ldr tmp2, .mckr_offset
147 ldr tmp1, [pmc, tmp2]
148 bic tmp1, tmp1, #AT91_PMC_CSS
149 str tmp1, [pmc, tmp2]
156 str tmp1, [r0, #0x10]
160 mov tmp1, #0xA5000000
163 ENDPROC(at91_backup_mode)
165 .macro at91_pm_ulp0_mode
168 ldr tmp3, .mckr_offset
170 /* Check if ULP0 fast variant has been requested. */
171 cmp tmp2, #AT91_PM_ULP0_FAST
174 /* Set highest prescaler for power saving */
175 ldr tmp1, [pmc, tmp3]
176 bic tmp1, tmp1, #AT91_PMC_PRES
177 orr tmp1, tmp1, #AT91_PMC_PRES_64
178 str tmp1, [pmc, tmp3]
183 /* Turn off the crystal oscillator */
184 ldr tmp1, [pmc, #AT91_CKGR_MOR]
185 bic tmp1, tmp1, #AT91_PMC_MOSCEN
186 orr tmp1, tmp1, #AT91_PMC_KEY
187 str tmp1, [pmc, #AT91_CKGR_MOR]
189 /* Save RC oscillator state */
190 ldr tmp1, [pmc, #AT91_PMC_SR]
191 str tmp1, .saved_osc_status
192 tst tmp1, #AT91_PMC_MOSCRCS
195 /* Turn off RC oscillator */
196 ldr tmp1, [pmc, #AT91_CKGR_MOR]
197 bic tmp1, tmp1, #AT91_PMC_MOSCRCEN
198 bic tmp1, tmp1, #AT91_PMC_KEY_MASK
199 orr tmp1, tmp1, #AT91_PMC_KEY
200 str tmp1, [pmc, #AT91_CKGR_MOR]
202 /* Wait main RC disabled done */
203 2: ldr tmp1, [pmc, #AT91_PMC_SR]
204 tst tmp1, #AT91_PMC_MOSCRCS
207 /* Wait for interrupt */
210 /* Check if ULP0 fast variant has been requested. */
211 cmp tmp2, #AT91_PM_ULP0_FAST
214 /* Set lowest prescaler for fast resume. */
215 ldr tmp1, [pmc, tmp3]
216 bic tmp1, tmp1, #AT91_PMC_PRES
217 str tmp1, [pmc, tmp3]
221 5: /* Restore RC oscillator state */
222 ldr tmp1, .saved_osc_status
223 tst tmp1, #AT91_PMC_MOSCRCS
226 /* Turn on RC oscillator */
227 ldr tmp1, [pmc, #AT91_CKGR_MOR]
228 orr tmp1, tmp1, #AT91_PMC_MOSCRCEN
229 bic tmp1, tmp1, #AT91_PMC_KEY_MASK
230 orr tmp1, tmp1, #AT91_PMC_KEY
231 str tmp1, [pmc, #AT91_CKGR_MOR]
233 /* Wait main RC stabilization */
234 3: ldr tmp1, [pmc, #AT91_PMC_SR]
235 tst tmp1, #AT91_PMC_MOSCRCS
238 /* Turn on the crystal oscillator */
239 4: ldr tmp1, [pmc, #AT91_CKGR_MOR]
240 orr tmp1, tmp1, #AT91_PMC_MOSCEN
241 orr tmp1, tmp1, #AT91_PMC_KEY
242 str tmp1, [pmc, #AT91_CKGR_MOR]
249 * Note: This procedure only applies on the platform which uses
250 * the external crystal oscillator as a main clock source.
252 .macro at91_pm_ulp1_mode
254 ldr tmp2, .mckr_offset
256 /* Save RC oscillator state and check if it is enabled. */
257 ldr tmp1, [pmc, #AT91_PMC_SR]
258 str tmp1, .saved_osc_status
259 tst tmp1, #AT91_PMC_MOSCRCS
262 /* Enable RC oscillator */
263 ldr tmp1, [pmc, #AT91_CKGR_MOR]
264 orr tmp1, tmp1, #AT91_PMC_MOSCRCEN
265 bic tmp1, tmp1, #AT91_PMC_KEY_MASK
266 orr tmp1, tmp1, #AT91_PMC_KEY
267 str tmp1, [pmc, #AT91_CKGR_MOR]
269 /* Wait main RC stabilization */
270 1: ldr tmp1, [pmc, #AT91_PMC_SR]
271 tst tmp1, #AT91_PMC_MOSCRCS
274 /* Switch the main clock source to 12-MHz RC oscillator */
275 2: ldr tmp1, [pmc, #AT91_CKGR_MOR]
276 bic tmp1, tmp1, #AT91_PMC_MOSCSEL
277 bic tmp1, tmp1, #AT91_PMC_KEY_MASK
278 orr tmp1, tmp1, #AT91_PMC_KEY
279 str tmp1, [pmc, #AT91_CKGR_MOR]
283 /* Disable the crystal oscillator */
284 ldr tmp1, [pmc, #AT91_CKGR_MOR]
285 bic tmp1, tmp1, #AT91_PMC_MOSCEN
286 bic tmp1, tmp1, #AT91_PMC_KEY_MASK
287 orr tmp1, tmp1, #AT91_PMC_KEY
288 str tmp1, [pmc, #AT91_CKGR_MOR]
290 /* Switch the master clock source to main clock */
291 ldr tmp1, [pmc, tmp2]
292 bic tmp1, tmp1, #AT91_PMC_CSS
293 orr tmp1, tmp1, #AT91_PMC_CSS_MAIN
294 str tmp1, [pmc, tmp2]
298 /* Enter the ULP1 mode by set WAITMODE bit in CKGR_MOR */
299 ldr tmp1, [pmc, #AT91_CKGR_MOR]
300 orr tmp1, tmp1, #AT91_PMC_WAITMODE
301 bic tmp1, tmp1, #AT91_PMC_KEY_MASK
302 orr tmp1, tmp1, #AT91_PMC_KEY
303 str tmp1, [pmc, #AT91_CKGR_MOR]
305 /* Quirk for SAM9X60's PMC */
311 /* Enable the crystal oscillator */
312 ldr tmp1, [pmc, #AT91_CKGR_MOR]
313 orr tmp1, tmp1, #AT91_PMC_MOSCEN
314 bic tmp1, tmp1, #AT91_PMC_KEY_MASK
315 orr tmp1, tmp1, #AT91_PMC_KEY
316 str tmp1, [pmc, #AT91_CKGR_MOR]
320 /* Switch the master clock source to slow clock */
321 ldr tmp1, [pmc, tmp2]
322 bic tmp1, tmp1, #AT91_PMC_CSS
323 str tmp1, [pmc, tmp2]
327 /* Switch main clock source to crystal oscillator */
328 ldr tmp1, [pmc, #AT91_CKGR_MOR]
329 orr tmp1, tmp1, #AT91_PMC_MOSCSEL
330 bic tmp1, tmp1, #AT91_PMC_KEY_MASK
331 orr tmp1, tmp1, #AT91_PMC_KEY
332 str tmp1, [pmc, #AT91_CKGR_MOR]
336 /* Switch the master clock source to main clock */
337 ldr tmp1, [pmc, tmp2]
338 bic tmp1, tmp1, #AT91_PMC_CSS
339 orr tmp1, tmp1, #AT91_PMC_CSS_MAIN
340 str tmp1, [pmc, tmp2]
344 /* Restore RC oscillator state */
345 ldr tmp1, .saved_osc_status
346 tst tmp1, #AT91_PMC_MOSCRCS
349 /* Disable RC oscillator */
350 ldr tmp1, [pmc, #AT91_CKGR_MOR]
351 bic tmp1, tmp1, #AT91_PMC_MOSCRCEN
352 bic tmp1, tmp1, #AT91_PMC_KEY_MASK
353 orr tmp1, tmp1, #AT91_PMC_KEY
354 str tmp1, [pmc, #AT91_CKGR_MOR]
356 /* Wait RC oscillator disable done */
357 4: ldr tmp1, [pmc, #AT91_PMC_SR]
358 tst tmp1, #AT91_PMC_MOSCRCS
364 .macro at91_plla_disable
365 /* Save PLLA setting and disable it */
366 ldr tmp1, .pmc_version
367 cmp tmp1, #AT91_PMC_V1
370 #ifdef CONFIG_SOC_SAM9X60
371 /* Save PLLA settings. */
372 ldr tmp2, [pmc, #AT91_PMC_PLL_UPDT]
373 bic tmp2, tmp2, #AT91_PMC_PLL_UPDT_ID
374 str tmp2, [pmc, #AT91_PMC_PLL_UPDT]
378 ldr tmp2, [pmc, #AT91_PMC_PLL_CTRL0]
379 bic tmp2, tmp2, #0xffffff00
383 ldr tmp2, [pmc, #AT91_PMC_PLL_CTRL1]
384 bic tmp2, tmp2, #0xffffff
386 str tmp1, .saved_pllar
389 ldr tmp1, [pmc, #AT91_PMC_PLL_UPDT]
390 bic tmp1, tmp1, #AT91_PMC_PLL_UPDT_UPDATE
391 bic tmp1, tmp1, #AT91_PMC_PLL_UPDT_ID
392 str tmp1, [pmc, #AT91_PMC_PLL_UPDT]
395 ldr tmp1, [pmc, #AT91_PMC_PLL_CTRL0]
396 bic tmp1, tmp1, #AT91_PMC_PLL_CTRL0_ENPLLCK
397 orr tmp1, tmp1, #AT91_PMC_PLL_CTRL0_ENPLL
398 str tmp1, [pmc, #AT91_PMC_PLL_CTRL0]
401 ldr tmp1, [pmc, #AT91_PMC_PLL_UPDT]
402 orr tmp1, tmp1, #AT91_PMC_PLL_UPDT_UPDATE
403 bic tmp1, tmp1, #AT91_PMC_PLL_UPDT_ID
404 str tmp1, [pmc, #AT91_PMC_PLL_UPDT]
407 ldr tmp1, [pmc, #AT91_PMC_PLL_CTRL0]
408 bic tmp1, tmp1, #AT91_PMC_PLL_CTRL0_ENPLL
409 str tmp1, [pmc, #AT91_PMC_PLL_CTRL0]
412 ldr tmp1, [pmc, #AT91_PMC_PLL_UPDT]
413 orr tmp1, tmp1, #AT91_PMC_PLL_UPDT_UPDATE
414 bic tmp1, tmp1, #AT91_PMC_PLL_UPDT_ID
415 str tmp1, [pmc, #AT91_PMC_PLL_UPDT]
420 1: /* Save PLLA setting and disable it */
421 ldr tmp1, [pmc, #AT91_CKGR_PLLAR]
422 str tmp1, .saved_pllar
425 mov tmp1, #AT91_PMC_PLLCOUNT
426 orr tmp1, tmp1, #(1 << 29) /* bit 29 always set */
427 str tmp1, [pmc, #AT91_CKGR_PLLAR]
431 .macro at91_plla_enable
432 ldr tmp2, .saved_pllar
433 ldr tmp3, .pmc_version
434 cmp tmp3, #AT91_PMC_V1
437 #ifdef CONFIG_SOC_SAM9X60
439 ldr tmp1, [pmc, #AT91_PMC_PLL_UPDT]
440 bic tmp1, tmp1, #AT91_PMC_PLL_UPDT_ID
441 bic tmp1, tmp1, #AT91_PMC_PLL_UPDT_UPDATE
442 str tmp1, [pmc, #AT91_PMC_PLL_UPDT]
445 ldr tmp1, =#AT91_PMC_PLL_ACR_DEFAULT_PLLA
446 str tmp1, [pmc, #AT91_PMC_PLL_ACR]
449 ldr tmp1, [pmc, #AT91_PMC_PLL_CTRL1]
451 bic tmp3, tmp3, #0xffffff
453 str tmp1, [pmc, #AT91_PMC_PLL_CTRL1]
456 ldr tmp1, [pmc, #AT91_PMC_PLL_UPDT]
457 bic tmp1, tmp1, #AT91_PMC_PLL_UPDT_ID
458 orr tmp1, tmp1, #AT91_PMC_PLL_UPDT_UPDATE
459 str tmp1, [pmc, #AT91_PMC_PLL_UPDT]
462 ldr tmp1, [pmc, #AT91_PMC_PLL_CTRL0]
463 orr tmp1, tmp1, #AT91_PMC_PLL_CTRL0_ENLOCK
464 orr tmp1, tmp1, #AT91_PMC_PLL_CTRL0_ENPLL
465 orr tmp1, tmp1, #AT91_PMC_PLL_CTRL0_ENPLLCK
466 bic tmp1, tmp1, #0xff
468 bic tmp3, tmp3, #0xffffff00
470 str tmp1, [pmc, #AT91_PMC_PLL_CTRL0]
473 ldr tmp1, [pmc, #AT91_PMC_PLL_UPDT]
474 orr tmp1, tmp1, #AT91_PMC_PLL_UPDT_UPDATE
475 bic tmp1, tmp1, #AT91_PMC_PLL_UPDT_ID
476 str tmp1, [pmc, #AT91_PMC_PLL_UPDT]
479 3: ldr tmp1, [pmc, #AT91_PMC_PLL_ISR0]
485 /* Restore PLLA setting */
486 4: str tmp2, [pmc, #AT91_CKGR_PLLAR]
489 tst tmp2, #(AT91_PMC_MUL & 0xff0000)
491 tst tmp2, #(AT91_PMC_MUL & ~0xff0000)
494 1: ldr tmp1, [pmc, #AT91_PMC_SR]
495 tst tmp1, #AT91_PMC_LOCKA
502 ldr tmp2, .mckr_offset
505 /* Save Master clock setting */
506 ldr tmp1, [pmc, tmp2]
507 str tmp1, .saved_mckr
510 * Set master clock source to:
511 * - MAINCK if using ULP0 fast variant
512 * - slow clock, otherwise
514 bic tmp1, tmp1, #AT91_PMC_CSS
515 cmp tmp3, #AT91_PM_ULP0_FAST
517 orr tmp1, tmp1, #AT91_PMC_CSS_MAIN
519 str tmp1, [pmc, tmp2]
525 cmp tmp3, #AT91_PM_ULP1
541 * Restore master clock setting
543 ldr tmp1, .mckr_offset
544 ldr tmp2, .saved_mckr
545 str tmp2, [pmc, tmp1]
550 ENDPROC(at91_ulp_mode)
553 * void at91_sramc_self_refresh(unsigned int is_active)
556 * @r0: 1 - active self-refresh mode
557 * 0 - exit self-refresh mode
560 * @r2: base address of the sram controller
563 ENTRY(at91_sramc_self_refresh)
567 cmp r1, #AT91_MEMCTRL_MC
571 * at91rm9200 Memory controller
575 * For exiting the self-refresh mode, do nothing,
576 * automatically exit the self-refresh mode.
578 tst r0, #SRAMC_SELF_FRESH_ACTIVE
581 /* Active SDRAM self-refresh mode */
583 str r3, [r2, #AT91_MC_SDRAMC_SRR]
587 cmp r1, #AT91_MEMCTRL_DDRSDR
591 * DDR Memory controller
593 tst r0, #SRAMC_SELF_FRESH_ACTIVE
596 /* LPDDR1 --> force DDR2 mode during self-refresh */
597 ldr r3, [r2, #AT91_DDRSDRC_MDR]
598 str r3, .saved_sam9_mdr
599 bic r3, r3, #~AT91_DDRSDRC_MD
600 cmp r3, #AT91_DDRSDRC_MD_LOW_POWER_DDR
601 ldreq r3, [r2, #AT91_DDRSDRC_MDR]
602 biceq r3, r3, #AT91_DDRSDRC_MD
603 orreq r3, r3, #AT91_DDRSDRC_MD_DDR2
604 streq r3, [r2, #AT91_DDRSDRC_MDR]
606 /* Active DDRC self-refresh mode */
607 ldr r3, [r2, #AT91_DDRSDRC_LPR]
608 str r3, .saved_sam9_lpr
609 bic r3, r3, #AT91_DDRSDRC_LPCB
610 orr r3, r3, #AT91_DDRSDRC_LPCB_SELF_REFRESH
611 str r3, [r2, #AT91_DDRSDRC_LPR]
613 /* If using the 2nd ddr controller */
618 ldr r3, [r2, #AT91_DDRSDRC_MDR]
619 str r3, .saved_sam9_mdr1
620 bic r3, r3, #~AT91_DDRSDRC_MD
621 cmp r3, #AT91_DDRSDRC_MD_LOW_POWER_DDR
622 ldreq r3, [r2, #AT91_DDRSDRC_MDR]
623 biceq r3, r3, #AT91_DDRSDRC_MD
624 orreq r3, r3, #AT91_DDRSDRC_MD_DDR2
625 streq r3, [r2, #AT91_DDRSDRC_MDR]
627 /* Active DDRC self-refresh mode */
628 ldr r3, [r2, #AT91_DDRSDRC_LPR]
629 str r3, .saved_sam9_lpr1
630 bic r3, r3, #AT91_DDRSDRC_LPCB
631 orr r3, r3, #AT91_DDRSDRC_LPCB_SELF_REFRESH
632 str r3, [r2, #AT91_DDRSDRC_LPR]
638 /* Restore MDR in case of LPDDR1 */
639 ldr r3, .saved_sam9_mdr
640 str r3, [r2, #AT91_DDRSDRC_MDR]
641 /* Restore LPR on AT91 with DDRAM */
642 ldr r3, .saved_sam9_lpr
643 str r3, [r2, #AT91_DDRSDRC_LPR]
645 /* If using the 2nd ddr controller */
648 ldrne r3, .saved_sam9_mdr1
649 strne r3, [r2, #AT91_DDRSDRC_MDR]
650 ldrne r3, .saved_sam9_lpr1
651 strne r3, [r2, #AT91_DDRSDRC_LPR]
656 * SDRAMC Memory controller
659 tst r0, #SRAMC_SELF_FRESH_ACTIVE
662 /* Active SDRAMC self-refresh mode */
663 ldr r3, [r2, #AT91_SDRAMC_LPR]
664 str r3, .saved_sam9_lpr
665 bic r3, r3, #AT91_SDRAMC_LPCB
666 orr r3, r3, #AT91_SDRAMC_LPCB_SELF_REFRESH
667 str r3, [r2, #AT91_SDRAMC_LPR]
670 ldr r3, .saved_sam9_lpr
671 str r3, [r2, #AT91_SDRAMC_LPR]
675 ENDPROC(at91_sramc_self_refresh)
710 ENTRY(at91_pm_suspend_in_sram_sz)
711 .word .-at91_pm_suspend_in_sram