1 // SPDX-License-Identifier: GPL-2.0+
9 #include <asm/arch/imx-regs.h>
10 #include <asm/arch/sys_proto.h>
11 #include <asm/mach-imx/mu_hal.h>
12 #include <asm/mach-imx/s400_api.h>
13 #include <asm/arch/rdc.h>
16 #define XRDC_ADDR 0x292f0000
17 #define MRC_OFFSET 0x2000
18 #define MRC_STEP 0x200
20 #define SP(X) ((X) << 9)
21 #define SU(X) ((X) << 6)
22 #define NP(X) ((X) << 3)
23 #define NU(X) ((X) << 0)
30 #define D7SEL_CODE (SP(RW) | SU(RW) | NP(RWX) | NU(RWX))
31 #define D6SEL_CODE (SP(RW) | SU(RW) | NP(RWX))
32 #define D5SEL_CODE (SP(RW) | SU(RWX))
33 #define D4SEL_CODE SP(RWX)
34 #define D3SEL_CODE (SP(X) | SU(X) | NP(X) | NU(X))
37 #define D7SEL_DAT (SP(RW) | SU(RW) | NP(RW) | NU(RW))
38 #define D6SEL_DAT (SP(RW) | SU(RW) | NP(RW))
39 #define D5SEL_DAT (SP(RW) | SU(RW) | NP(R) | NU(R))
40 #define D4SEL_DAT (SP(RW) | SU(RW))
41 #define D3SEL_DAT SP(RW)
50 /* The upper only existed in the beginning of each MBC */
51 u32 mem0_blk_cfg_w[64];
52 u32 mem0_blk_nse_w[16];
53 u32 mem1_blk_cfg_w[8];
54 u32 mem1_blk_nse_w[2];
55 u32 mem2_blk_cfg_w[8];
56 u32 mem2_blk_nse_w[2];
57 u32 mem3_blk_cfg_w[8];
58 u32 mem3_blk_nse_w[2];/*0x1F0, 0x1F4 */
69 /* The upper only existed in the beginning of each MRC */
70 u32 rgn_desc_words[8][2]; /* 8 regions, 2 words per region */
78 struct mbc_mem_dom mem_dom[4][8];
79 struct mrc_rgn_dom mrc_dom[2][8];
91 int xrdc_config_mrc_dx_perm(u32 mrc_con, u32 region, u32 dom, u32 dxsel)
96 w2_addr = XRDC_ADDR + MRC_OFFSET + mrc_con * 0x200 + region * 0x20 + 0x8;
98 val = (readl(w2_addr) & (~(7 << (3 * dom)))) | (dxsel << (3 * dom));
104 int xrdc_config_mrc_w0_w1(u32 mrc_con, u32 region, u32 w0, u32 size)
106 ulong w0_addr, w1_addr;
108 w0_addr = XRDC_ADDR + MRC_OFFSET + mrc_con * 0x200 + region * 0x20;
109 w1_addr = w0_addr + 4;
111 if ((size % 32) != 0)
114 writel(w0 & ~0x1f, w0_addr);
115 writel(w0 + size - 1, w1_addr);
120 int xrdc_config_mrc_w3_w4(u32 mrc_con, u32 region, u32 w3, u32 w4)
122 ulong w3_addr = XRDC_ADDR + MRC_OFFSET + mrc_con * 0x200 + region * 0x20 + 0xC;
123 ulong w4_addr = w3_addr + 4;
131 int xrdc_config_pdac_openacc(u32 bridge, u32 index)
138 w0_addr = XRDC_ADDR + 0x1000 + 0x8 * index;
141 w0_addr = XRDC_ADDR + 0x1400 + 0x8 * index;
144 w0_addr = XRDC_ADDR + 0x1800 + 0x8 * index;
149 writel(0xffffff, w0_addr);
151 val = readl(w0_addr + 4);
152 writel(val | BIT(31), w0_addr + 4);
157 int xrdc_config_pdac(u32 bridge, u32 index, u32 dom, u32 perm)
164 w0_addr = XRDC_ADDR + 0x1000 + 0x8 * index;
167 w0_addr = XRDC_ADDR + 0x1400 + 0x8 * index;
170 w0_addr = XRDC_ADDR + 0x1800 + 0x8 * index;
175 val = readl(w0_addr);
176 writel((val & ~(0x7 << (dom * 3))) | (perm << (dom * 3)), w0_addr);
178 val = readl(w0_addr + 4);
179 writel(val | BIT(31), w0_addr + 4);
184 int release_rdc(enum rdc_type type)
186 ulong s_mu_base = 0x27020000UL;
187 struct sentinel_msg msg;
189 u32 rdc_id = (type == RDC_XRDC) ? 0x78 : 0x74;
191 msg.version = AHAB_VERSION;
192 msg.tag = AHAB_CMD_TAG;
194 msg.command = AHAB_RELEASE_RDC_REQ_CID;
195 msg.data[0] = (rdc_id << 8) | 0x2; /* A35 XRDC */
197 mu_hal_init(s_mu_base);
198 mu_hal_sendmsg(s_mu_base, 0, *((u32 *)&msg));
199 mu_hal_sendmsg(s_mu_base, 1, msg.data[0]);
201 ret = mu_hal_receivemsg(s_mu_base, 0, (u32 *)&msg);
203 ret = mu_hal_receivemsg(s_mu_base, 1, &msg.data[0]);
205 if ((msg.data[0] & 0xff) == 0xd6)
215 void xrdc_mrc_region_set_access(int mrc_index, u32 addr, u32 access)
217 ulong xrdc_base = 0x292f0000, off;
219 u8 mrcfg, j, region_num;
222 mrcfg = readb(xrdc_base + 0x140 + mrc_index);
223 region_num = mrcfg & 0x1f;
225 for (j = 0; j < region_num; j++) {
226 off = 0x2000 + mrc_index * 0x200 + j * 0x20;
228 mrgd[0] = readl(xrdc_base + off);
229 mrgd[1] = readl(xrdc_base + off + 4);
230 mrgd[2] = readl(xrdc_base + off + 8);
231 mrgd[3] = readl(xrdc_base + off + 0xc);
232 mrgd[4] = readl(xrdc_base + off + 0x10);
234 debug("MRC [%u][%u]\n", mrc_index, j);
235 debug("0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
236 mrgd[0], mrgd[1], mrgd[2], mrgd[3], mrgd[4]);
239 if (addr >= mrgd[0] && addr <= mrgd[1]) {
240 /* find domain 7 DSEL */
241 dsel = (mrgd[2] >> 21) & 0x7;
244 mrgd[4] |= (access & 0xFFF);
245 } else if (dsel == 2) {
246 mrgd[4] &= ~0xFFF0000;
247 mrgd[4] |= ((access & 0xFFF) << 16);
250 /* not handle other cases, since S400 only set ACCESS1 and 2 */
251 writel(mrgd[4], xrdc_base + off + 0x10);
257 void xrdc_init_mda(void)
259 ulong xrdc_base = XRDC_ADDR, off;
262 /* Set MDA3-5 for PXP, ENET, CAAM to DID 1*/
263 for (i = 3; i <= 5; i++) {
264 off = 0x800 + i * 0x20;
265 writel(0x200000A1, xrdc_base + off);
266 writel(0xA00000A1, xrdc_base + off);
269 /* Set MDA10 -15 to DID 3 for video */
270 for (i = 10; i <= 15; i++) {
271 off = 0x800 + i * 0x20;
272 writel(0x200000A3, xrdc_base + off);
273 writel(0xA00000A3, xrdc_base + off);
277 void xrdc_init_mrc(void)
279 /* Set MRC4 and MRC5 for DDR access from A35 and AP NIC PER masters */
280 xrdc_config_mrc_w0_w1(4, 0, CFG_SYS_SDRAM_BASE, PHYS_SDRAM_SIZE);
281 xrdc_config_mrc_dx_perm(4, 0, 1, 1);
282 xrdc_config_mrc_dx_perm(4, 0, 7, 1);
283 xrdc_config_mrc_w3_w4(4, 0, 0x0, 0x80000FFF);
285 xrdc_config_mrc_w0_w1(5, 0, CFG_SYS_SDRAM_BASE, PHYS_SDRAM_SIZE);
286 xrdc_config_mrc_dx_perm(5, 0, 1, 1);
287 xrdc_config_mrc_w3_w4(5, 0, 0x0, 0x80000FFF);
289 /* The MRC8 is for SRAM1 */
290 xrdc_config_mrc_w0_w1(8, 0, 0x21000000, 0x10000);
291 /* Allow for all domains: So domain 2/3 (HIFI DSP/LPAV) is ok to access */
292 xrdc_config_mrc_dx_perm(8, 0, 0, 1);
293 xrdc_config_mrc_dx_perm(8, 0, 1, 1);
294 xrdc_config_mrc_dx_perm(8, 0, 2, 1);
295 xrdc_config_mrc_dx_perm(8, 0, 3, 1);
296 xrdc_config_mrc_dx_perm(8, 0, 4, 1);
297 xrdc_config_mrc_dx_perm(8, 0, 5, 1);
298 xrdc_config_mrc_dx_perm(8, 0, 6, 1);
299 xrdc_config_mrc_dx_perm(8, 0, 7, 1);
300 xrdc_config_mrc_w3_w4(8, 0, 0x0, 0x80000FFF);
302 /* The MRC6 is for video modules to ddr */
303 xrdc_config_mrc_w0_w1(6, 0, 0x80000000, 0x80000000);
304 xrdc_config_mrc_dx_perm(6, 0, 3, 1); /* allow for domain 3 video */
305 xrdc_config_mrc_w3_w4(6, 0, 0x0, 0x80000FFF);
308 int trdc_mbc_set_access(u32 mbc_x, u32 dom_x, u32 mem_x, u32 blk_x, bool sec_access)
310 struct trdc *trdc_base = (struct trdc *)0x28031000U;
311 struct mbc_mem_dom *mbc_dom;
313 u32 index, offset, val;
315 mbc_dom = &trdc_base->mem_dom[mbc_x][dom_x];
319 cfg_w = &mbc_dom->mem0_blk_cfg_w[blk_x / 8];
320 nse_w = &mbc_dom->mem0_blk_nse_w[blk_x / 32];
323 cfg_w = &mbc_dom->mem1_blk_cfg_w[blk_x / 8];
324 nse_w = &mbc_dom->mem1_blk_nse_w[blk_x / 32];
327 cfg_w = &mbc_dom->mem2_blk_cfg_w[blk_x / 8];
328 nse_w = &mbc_dom->mem2_blk_nse_w[blk_x / 32];
331 cfg_w = &mbc_dom->mem3_blk_cfg_w[blk_x / 8];
332 nse_w = &mbc_dom->mem3_blk_nse_w[blk_x / 32];
341 val = readl((void __iomem *)cfg_w);
343 val &= ~(0xFU << offset);
346 * Global 0, 0x7777 secure pri/user read/write/execute, S400 has already set it.
347 * So select MBC0_MEMN_GLBAC0
350 val |= (0x0 << offset);
351 writel(val, (void __iomem *)cfg_w);
353 val |= (0x8 << offset); /* nse bit set */
354 writel(val, (void __iomem *)cfg_w);
360 int trdc_mrc_region_set_access(u32 mrc_x, u32 dom_x, u32 addr_start, u32 addr_end, bool sec_access)
362 struct trdc *trdc_base = (struct trdc *)0x28031000U;
363 struct mrc_rgn_dom *mrc_dom;
367 bool vld, hit = false;
369 mrc_dom = &trdc_base->mrc_dom[mrc_x][dom_x];
371 for (i = 0; i < 8; i++) {
372 desc_w = &mrc_dom->rgn_desc_words[i][0];
374 start = readl((void __iomem *)desc_w) & 0xfff;
375 end = readl((void __iomem *)(desc_w + 1));
379 if (start == 0 && end == 0 && !vld && free >= 8)
382 /* Check all the region descriptors, even overlap */
383 if (addr_start >= end || addr_end <= start || !vld)
387 * Global 0, 0x7777 secure pri/user read/write/execute, S400 has already set it.
388 * So select MRCx_MEMN_GLBAC0
391 writel(start, (void __iomem *)desc_w);
392 writel(end | 0x1, (void __iomem *)(desc_w + 1));
394 writel(start, (void __iomem *)desc_w);
395 writel((end | 0x1 | 0x10), (void __iomem *)(desc_w + 1));
398 if (addr_start >= start && addr_end <= end)
406 desc_w = &mrc_dom->rgn_desc_words[free][0];
408 addr_start &= ~0xfff;
412 writel(addr_start, (void __iomem *)desc_w);
413 writel(addr_end | 0x1, (void __iomem *)(desc_w + 1));
415 writel(addr_start, (void __iomem *)desc_w);
416 writel((addr_end | 0x1 | 0x10), (void __iomem *)(desc_w + 1));