Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hongbo Zhang | 539e4f1 | 2016-08-19 17:20:33 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2016 Freescale Semiconductor, Inc. |
| 4 | * Author: Hongbo Zhang <hongbo.zhang@nxp.com> |
Hongbo Zhang | 539e4f1 | 2016-08-19 17:20:33 +0800 | [diff] [blame] | 5 | * This file implements LS102X platform PSCI SYSTEM-SUSPEND function |
| 6 | */ |
| 7 | |
| 8 | #include <config.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <asm/psci.h> |
| 11 | #include <asm/arch/immap_ls102xa.h> |
| 12 | #include <fsl_immap.h> |
| 13 | #include "fsl_epu.h" |
| 14 | |
| 15 | #define __secure __attribute__((section("._secure.text"))) |
| 16 | |
| 17 | #define CCSR_GICD_CTLR 0x1000 |
| 18 | #define CCSR_GICC_CTLR 0x2000 |
| 19 | #define DCSR_RCPM_CG1CR0 0x31c |
| 20 | #define DCSR_RCPM_CSTTACR0 0xb00 |
| 21 | #define DCFG_CRSTSR_WDRFR 0x8 |
| 22 | #define DDR_RESV_LEN 128 |
| 23 | |
| 24 | #ifdef CONFIG_LS1_DEEP_SLEEP |
| 25 | /* |
| 26 | * DDR controller initialization training breaks the first 128 bytes of DDR, |
| 27 | * save them so that the bootloader can restore them while resuming. |
| 28 | */ |
| 29 | static void __secure ls1_save_ddr_head(void) |
| 30 | { |
| 31 | const char *src = (const char *)CONFIG_SYS_SDRAM_BASE; |
| 32 | char *dest = (char *)(OCRAM_BASE_S_ADDR + OCRAM_S_SIZE - DDR_RESV_LEN); |
| 33 | struct ccsr_scfg __iomem *scfg = (void *)CONFIG_SYS_FSL_SCFG_ADDR; |
| 34 | int i; |
| 35 | |
| 36 | out_le32(&scfg->sparecr[2], dest); |
| 37 | |
| 38 | for (i = 0; i < DDR_RESV_LEN; i++) |
| 39 | *dest++ = *src++; |
| 40 | } |
| 41 | |
| 42 | static void __secure ls1_fsm_setup(void) |
| 43 | { |
| 44 | void *dcsr_epu_base = (void *)(CONFIG_SYS_DCSRBAR + EPU_BLOCK_OFFSET); |
York Sun | 48d55ac | 2016-09-26 08:09:30 -0700 | [diff] [blame] | 45 | void *dcsr_rcpm_base = (void *)SYS_FSL_DCSR_RCPM_ADDR; |
Hongbo Zhang | 539e4f1 | 2016-08-19 17:20:33 +0800 | [diff] [blame] | 46 | |
| 47 | out_be32(dcsr_rcpm_base + DCSR_RCPM_CSTTACR0, 0x00001001); |
| 48 | out_be32(dcsr_rcpm_base + DCSR_RCPM_CG1CR0, 0x00000001); |
| 49 | |
| 50 | fsl_epu_setup((void *)dcsr_epu_base); |
| 51 | |
| 52 | /* Pull MCKE signal low before enabling deep sleep signal in FPGA */ |
| 53 | out_be32(dcsr_epu_base + EPECR0, 0x5); |
| 54 | out_be32(dcsr_epu_base + EPSMCR15, 0x76300000); |
| 55 | } |
| 56 | |
| 57 | static void __secure ls1_deepsleep_irq_cfg(void) |
| 58 | { |
| 59 | struct ccsr_scfg __iomem *scfg = (void *)CONFIG_SYS_FSL_SCFG_ADDR; |
| 60 | struct ccsr_rcpm __iomem *rcpm = (void *)CONFIG_SYS_FSL_RCPM_ADDR; |
| 61 | u32 ippdexpcr0, ippdexpcr1, pmcintecr = 0; |
| 62 | |
| 63 | /* Mask interrupts from GIC */ |
| 64 | out_be32(&rcpm->nfiqoutr, 0x0ffffffff); |
| 65 | out_be32(&rcpm->nirqoutr, 0x0ffffffff); |
| 66 | /* Mask deep sleep wake-up interrupts while entering deep sleep */ |
| 67 | out_be32(&rcpm->dsimskr, 0x0ffffffff); |
| 68 | |
| 69 | ippdexpcr0 = in_be32(&rcpm->ippdexpcr0); |
| 70 | /* |
Biwen Li | 721d50e | 2019-09-25 18:40:42 +0800 | [diff] [blame^] | 71 | * Workaround of errata A-008646 |
| 72 | * Errata states that read to register ippdexpcr1 always returns |
| 73 | * zero irrespective of what value is written into it. So its value |
| 74 | * is first saved to a spare register and then read from it |
Hongbo Zhang | 539e4f1 | 2016-08-19 17:20:33 +0800 | [diff] [blame] | 75 | */ |
Biwen Li | e5c6c8e | 2019-09-25 17:48:11 +0800 | [diff] [blame] | 76 | ippdexpcr1 = in_be32(&scfg->sparecr[7]); |
Ran Wang | aed0458 | 2018-09-26 13:46:30 +0800 | [diff] [blame] | 77 | out_be32(&rcpm->ippdexpcr1, ippdexpcr1); |
Hongbo Zhang | 539e4f1 | 2016-08-19 17:20:33 +0800 | [diff] [blame] | 78 | |
| 79 | if (ippdexpcr0 & RCPM_IPPDEXPCR0_ETSEC) |
| 80 | pmcintecr |= SCFG_PMCINTECR_ETSECRXG0 | |
| 81 | SCFG_PMCINTECR_ETSECRXG1 | |
| 82 | SCFG_PMCINTECR_ETSECERRG0 | |
| 83 | SCFG_PMCINTECR_ETSECERRG1; |
| 84 | |
| 85 | if (ippdexpcr0 & RCPM_IPPDEXPCR0_GPIO) |
| 86 | pmcintecr |= SCFG_PMCINTECR_GPIO; |
| 87 | |
| 88 | if (ippdexpcr1 & RCPM_IPPDEXPCR1_LPUART) |
| 89 | pmcintecr |= SCFG_PMCINTECR_LPUART; |
| 90 | |
| 91 | if (ippdexpcr1 & RCPM_IPPDEXPCR1_FLEXTIMER) |
| 92 | pmcintecr |= SCFG_PMCINTECR_FTM; |
| 93 | |
| 94 | /* Always set external IRQ pins as wakeup source */ |
| 95 | pmcintecr |= SCFG_PMCINTECR_IRQ0 | SCFG_PMCINTECR_IRQ1; |
| 96 | |
| 97 | out_be32(&scfg->pmcintlecr, 0); |
| 98 | /* Clear PMC interrupt status */ |
| 99 | out_be32(&scfg->pmcintsr, 0xffffffff); |
| 100 | /* Enable wakeup interrupt during deep sleep */ |
| 101 | out_be32(&scfg->pmcintecr, pmcintecr); |
| 102 | } |
| 103 | |
| 104 | static void __secure ls1_delay(unsigned int loop) |
| 105 | { |
| 106 | while (loop--) { |
| 107 | int i = 1000; |
| 108 | while (i--) |
| 109 | ; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static void __secure ls1_start_fsm(void) |
| 114 | { |
| 115 | void *dcsr_epu_base = (void *)(CONFIG_SYS_DCSRBAR + EPU_BLOCK_OFFSET); |
York Sun | 48d55ac | 2016-09-26 08:09:30 -0700 | [diff] [blame] | 116 | void *ccsr_gic_base = (void *)SYS_FSL_GIC_ADDR; |
Hongbo Zhang | 539e4f1 | 2016-08-19 17:20:33 +0800 | [diff] [blame] | 117 | struct ccsr_scfg __iomem *scfg = (void *)CONFIG_SYS_FSL_SCFG_ADDR; |
| 118 | struct ccsr_ddr __iomem *ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR; |
| 119 | |
| 120 | /* Set HRSTCR */ |
| 121 | setbits_be32(&scfg->hrstcr, 0x80000000); |
| 122 | |
| 123 | /* Place DDR controller in self refresh mode */ |
| 124 | setbits_be32(&ddr->sdram_cfg_2, 0x80000000); |
| 125 | |
| 126 | ls1_delay(2000); |
| 127 | |
| 128 | /* Set EVT4_B to lock the signal MCKE down */ |
| 129 | out_be32(dcsr_epu_base + EPECR0, 0x0); |
| 130 | |
| 131 | ls1_delay(2000); |
| 132 | |
| 133 | out_be32(ccsr_gic_base + CCSR_GICD_CTLR, 0x0); |
| 134 | out_be32(ccsr_gic_base + CCSR_GICC_CTLR, 0x0); |
| 135 | |
| 136 | /* Enable all EPU Counters */ |
| 137 | setbits_be32(dcsr_epu_base + EPGCR, 0x80000000); |
| 138 | |
| 139 | /* Enable SCU15 */ |
| 140 | setbits_be32(dcsr_epu_base + EPECR15, 0x90000004); |
| 141 | |
| 142 | /* Enter WFI mode, and EPU FSM will start */ |
| 143 | __asm__ __volatile__ ("wfi" : : : "memory"); |
| 144 | |
| 145 | /* NEVER ENTER HERE */ |
| 146 | while (1) |
| 147 | ; |
| 148 | } |
| 149 | |
| 150 | static void __secure ls1_deep_sleep(u32 entry_point) |
| 151 | { |
| 152 | struct ccsr_scfg __iomem *scfg = (void *)CONFIG_SYS_FSL_SCFG_ADDR; |
| 153 | struct ccsr_gur __iomem *gur = (void *)CONFIG_SYS_FSL_GUTS_ADDR; |
| 154 | struct ccsr_rcpm __iomem *rcpm = (void *)CONFIG_SYS_FSL_RCPM_ADDR; |
| 155 | #ifdef QIXIS_BASE |
| 156 | u32 tmp; |
| 157 | void *qixis_base = (void *)QIXIS_BASE; |
| 158 | #endif |
| 159 | |
| 160 | /* Enable cluster to enter the PCL10 state */ |
| 161 | out_be32(&scfg->clusterpmcr, SCFG_CLUSTERPMCR_WFIL2EN); |
| 162 | |
| 163 | /* Save the first 128 bytes of DDR data */ |
| 164 | ls1_save_ddr_head(); |
| 165 | |
| 166 | /* Save the kernel resume entry */ |
| 167 | out_le32(&scfg->sparecr[3], entry_point); |
| 168 | |
| 169 | /* Request to put cluster 0 in PCL10 state */ |
| 170 | setbits_be32(&rcpm->clpcl10setr, RCPM_CLPCL10SETR_C0); |
| 171 | |
| 172 | /* Setup the registers of the EPU FSM for deep sleep */ |
| 173 | ls1_fsm_setup(); |
| 174 | |
| 175 | #ifdef QIXIS_BASE |
| 176 | /* Connect the EVENT button to IRQ in FPGA */ |
| 177 | tmp = in_8(qixis_base + QIXIS_CTL_SYS); |
| 178 | tmp &= ~QIXIS_CTL_SYS_EVTSW_MASK; |
| 179 | tmp |= QIXIS_CTL_SYS_EVTSW_IRQ; |
| 180 | out_8(qixis_base + QIXIS_CTL_SYS, tmp); |
| 181 | |
| 182 | /* Enable deep sleep signals in FPGA */ |
| 183 | tmp = in_8(qixis_base + QIXIS_PWR_CTL2); |
| 184 | tmp |= QIXIS_PWR_CTL2_PCTL; |
| 185 | out_8(qixis_base + QIXIS_PWR_CTL2, tmp); |
| 186 | |
| 187 | /* Pull down PCIe RST# */ |
| 188 | tmp = in_8(qixis_base + QIXIS_RST_FORCE_3); |
| 189 | tmp |= QIXIS_RST_FORCE_3_PCIESLOT1; |
| 190 | out_8(qixis_base + QIXIS_RST_FORCE_3, tmp); |
| 191 | #endif |
| 192 | |
| 193 | /* Enable Warm Device Reset */ |
| 194 | setbits_be32(&scfg->dpslpcr, SCFG_DPSLPCR_WDRR_EN); |
| 195 | setbits_be32(&gur->crstsr, DCFG_CRSTSR_WDRFR); |
| 196 | |
Ran Wang | aed0458 | 2018-09-26 13:46:30 +0800 | [diff] [blame] | 197 | /* Disable QE */ |
| 198 | setbits_be32(&gur->devdisr, CCSR_DEVDISR1_QE); |
| 199 | |
Hongbo Zhang | 539e4f1 | 2016-08-19 17:20:33 +0800 | [diff] [blame] | 200 | ls1_deepsleep_irq_cfg(); |
| 201 | |
| 202 | psci_v7_flush_dcache_all(); |
| 203 | |
| 204 | ls1_start_fsm(); |
| 205 | } |
| 206 | |
| 207 | #else |
| 208 | static void __secure ls1_sleep(void) |
| 209 | { |
| 210 | struct ccsr_scfg __iomem *scfg = (void *)CONFIG_SYS_FSL_SCFG_ADDR; |
| 211 | struct ccsr_rcpm __iomem *rcpm = (void *)CONFIG_SYS_FSL_RCPM_ADDR; |
| 212 | |
| 213 | #ifdef QIXIS_BASE |
| 214 | u32 tmp; |
| 215 | void *qixis_base = (void *)QIXIS_BASE; |
| 216 | |
| 217 | /* Connect the EVENT button to IRQ in FPGA */ |
| 218 | tmp = in_8(qixis_base + QIXIS_CTL_SYS); |
| 219 | tmp &= ~QIXIS_CTL_SYS_EVTSW_MASK; |
| 220 | tmp |= QIXIS_CTL_SYS_EVTSW_IRQ; |
| 221 | out_8(qixis_base + QIXIS_CTL_SYS, tmp); |
| 222 | #endif |
| 223 | |
| 224 | /* Enable cluster to enter the PCL10 state */ |
| 225 | out_be32(&scfg->clusterpmcr, SCFG_CLUSTERPMCR_WFIL2EN); |
| 226 | |
| 227 | setbits_be32(&rcpm->powmgtcsr, RCPM_POWMGTCSR_LPM20_REQ); |
| 228 | |
| 229 | __asm__ __volatile__ ("wfi" : : : "memory"); |
| 230 | } |
| 231 | #endif |
| 232 | |
| 233 | void __secure ls1_system_suspend(u32 fn, u32 entry_point, u32 context_id) |
| 234 | { |
| 235 | #ifdef CONFIG_LS1_DEEP_SLEEP |
| 236 | ls1_deep_sleep(entry_point); |
| 237 | #else |
| 238 | ls1_sleep(); |
| 239 | #endif |
| 240 | } |