blob: ca657748ed9a0865dbf32ccf7b89be98ff25e57b [file] [log] [blame]
Peng Fanfe1bf872021-08-07 16:00:56 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
Peng Fan9c87e462021-08-07 16:00:59 +08003 * Copyright 2021 NXP
Peng Fanfe1bf872021-08-07 16:00:56 +08004 */
5
Tom Rinidec7ea02024-05-20 13:35:03 -06006#include <config.h>
7#include <linux/errno.h>
Peng Fanfe1bf872021-08-07 16:00:56 +08008#include <asm/io.h>
Peng Fan9c87e462021-08-07 16:00:59 +08009#include <asm/types.h>
10#include <asm/arch/imx-regs.h>
Peng Fanfe1bf872021-08-07 16:00:56 +080011#include <asm/arch/sys_proto.h>
Ye Lic408ed32022-07-26 16:40:49 +080012#include <asm/mach-imx/mu_hal.h>
Peng Fand5c31832023-06-15 18:09:05 +080013#include <asm/mach-imx/ele_api.h>
Peng Fan9c87e462021-08-07 16:00:59 +080014#include <asm/arch/rdc.h>
15#include <div64.h>
Peng Fanfe1bf872021-08-07 16:00:56 +080016
17#define XRDC_ADDR 0x292f0000
18#define MRC_OFFSET 0x2000
19#define MRC_STEP 0x200
20
21#define SP(X) ((X) << 9)
22#define SU(X) ((X) << 6)
23#define NP(X) ((X) << 3)
24#define NU(X) ((X) << 0)
25
26#define RWX 7
27#define RW 6
28#define R 4
29#define X 1
30
31#define D7SEL_CODE (SP(RW) | SU(RW) | NP(RWX) | NU(RWX))
32#define D6SEL_CODE (SP(RW) | SU(RW) | NP(RWX))
33#define D5SEL_CODE (SP(RW) | SU(RWX))
34#define D4SEL_CODE SP(RWX)
35#define D3SEL_CODE (SP(X) | SU(X) | NP(X) | NU(X))
36#define D0SEL_CODE 0
37
38#define D7SEL_DAT (SP(RW) | SU(RW) | NP(RW) | NU(RW))
39#define D6SEL_DAT (SP(RW) | SU(RW) | NP(RW))
40#define D5SEL_DAT (SP(RW) | SU(RW) | NP(R) | NU(R))
41#define D4SEL_DAT (SP(RW) | SU(RW))
42#define D3SEL_DAT SP(RW)
43
Peng Fan9c87e462021-08-07 16:00:59 +080044struct mbc_mem_dom {
45 u32 mem_glbcfg[4];
46 u32 nse_blk_index;
47 u32 nse_blk_set;
48 u32 nse_blk_clr;
49 u32 nsr_blk_clr_all;
50 u32 memn_glbac[8];
51 /* The upper only existed in the beginning of each MBC */
52 u32 mem0_blk_cfg_w[64];
53 u32 mem0_blk_nse_w[16];
54 u32 mem1_blk_cfg_w[8];
55 u32 mem1_blk_nse_w[2];
56 u32 mem2_blk_cfg_w[8];
57 u32 mem2_blk_nse_w[2];
58 u32 mem3_blk_cfg_w[8];
59 u32 mem3_blk_nse_w[2];/*0x1F0, 0x1F4 */
60 u32 reserved[2];
61};
62
63struct mrc_rgn_dom {
64 u32 mrc_glbcfg[4];
65 u32 nse_rgn_indirect;
66 u32 nse_rgn_set;
67 u32 nse_rgn_clr;
68 u32 nse_rgn_clr_all;
69 u32 memn_glbac[8];
70 /* The upper only existed in the beginning of each MRC */
71 u32 rgn_desc_words[8][2]; /* 8 regions, 2 words per region */
72 u32 reserved[16];
73 u32 rgn_nse;
74 u32 reserved2[15];
75};
76
77struct trdc {
78 u8 res0[0x1000];
79 struct mbc_mem_dom mem_dom[4][8];
80 struct mrc_rgn_dom mrc_dom[2][8];
81};
82
Peng Fanfe1bf872021-08-07 16:00:56 +080083union dxsel_perm {
84 struct {
85 u8 dx;
86 u8 perm;
87 };
88
89 u32 dom_perm;
90};
91
92int xrdc_config_mrc_dx_perm(u32 mrc_con, u32 region, u32 dom, u32 dxsel)
93{
94 ulong w2_addr;
95 u32 val = 0;
96
97 w2_addr = XRDC_ADDR + MRC_OFFSET + mrc_con * 0x200 + region * 0x20 + 0x8;
98
99 val = (readl(w2_addr) & (~(7 << (3 * dom)))) | (dxsel << (3 * dom));
100 writel(val, w2_addr);
101
102 return 0;
103}
104
105int xrdc_config_mrc_w0_w1(u32 mrc_con, u32 region, u32 w0, u32 size)
106{
107 ulong w0_addr, w1_addr;
108
109 w0_addr = XRDC_ADDR + MRC_OFFSET + mrc_con * 0x200 + region * 0x20;
110 w1_addr = w0_addr + 4;
111
112 if ((size % 32) != 0)
113 return -EINVAL;
114
115 writel(w0 & ~0x1f, w0_addr);
116 writel(w0 + size - 1, w1_addr);
117
118 return 0;
119}
120
121int xrdc_config_mrc_w3_w4(u32 mrc_con, u32 region, u32 w3, u32 w4)
122{
123 ulong w3_addr = XRDC_ADDR + MRC_OFFSET + mrc_con * 0x200 + region * 0x20 + 0xC;
124 ulong w4_addr = w3_addr + 4;
125
126 writel(w3, w3_addr);
127 writel(w4, w4_addr);
128
129 return 0;
130}
131
132int xrdc_config_pdac_openacc(u32 bridge, u32 index)
133{
134 ulong w0_addr;
135 u32 val;
136
137 switch (bridge) {
138 case 3:
139 w0_addr = XRDC_ADDR + 0x1000 + 0x8 * index;
140 break;
141 case 4:
142 w0_addr = XRDC_ADDR + 0x1400 + 0x8 * index;
143 break;
144 case 5:
145 w0_addr = XRDC_ADDR + 0x1800 + 0x8 * index;
146 break;
147 default:
148 return -EINVAL;
149 }
150 writel(0xffffff, w0_addr);
151
152 val = readl(w0_addr + 4);
153 writel(val | BIT(31), w0_addr + 4);
154
155 return 0;
156}
157
158int xrdc_config_pdac(u32 bridge, u32 index, u32 dom, u32 perm)
159{
160 ulong w0_addr;
161 u32 val;
162
163 switch (bridge) {
164 case 3:
165 w0_addr = XRDC_ADDR + 0x1000 + 0x8 * index;
166 break;
167 case 4:
168 w0_addr = XRDC_ADDR + 0x1400 + 0x8 * index;
169 break;
170 case 5:
171 w0_addr = XRDC_ADDR + 0x1800 + 0x8 * index;
172 break;
173 default:
174 return -EINVAL;
175 }
176 val = readl(w0_addr);
177 writel((val & ~(0x7 << (dom * 3))) | (perm << (dom * 3)), w0_addr);
178
179 val = readl(w0_addr + 4);
180 writel(val | BIT(31), w0_addr + 4);
181
182 return 0;
183}
Peng Fan9c87e462021-08-07 16:00:59 +0800184
Ye Li7edb3622023-01-31 16:42:24 +0800185int xrdc_config_msc(u32 msc, u32 index, u32 dom, u32 perm)
186{
187 ulong w0_addr;
188 u32 val;
189
190 if (msc > 2)
191 return -EINVAL;
192
193 w0_addr = XRDC_ADDR + 0x4000 + 0x400 * msc + 0x8 * index;
194
195 val = readl(w0_addr);
196 writel((val & ~(0x7 << (dom * 3))) | (perm << (dom * 3)), w0_addr);
197
198 val = readl(w0_addr + 4);
199 writel(val | BIT(31), w0_addr + 4);
200
201 return 0;
202}
203
Peng Fan9c87e462021-08-07 16:00:59 +0800204int release_rdc(enum rdc_type type)
205{
206 ulong s_mu_base = 0x27020000UL;
Peng Fand5c31832023-06-15 18:09:05 +0800207 struct ele_msg msg;
Peng Fan9c87e462021-08-07 16:00:59 +0800208 int ret;
209 u32 rdc_id = (type == RDC_XRDC) ? 0x78 : 0x74;
210
Peng Fand5c31832023-06-15 18:09:05 +0800211 msg.version = ELE_VERSION;
212 msg.tag = ELE_CMD_TAG;
Peng Fan9c87e462021-08-07 16:00:59 +0800213 msg.size = 2;
Ye Liebb2be52023-01-30 18:39:53 +0800214 msg.command = ELE_RELEASE_RDC_REQ;
Peng Fan9c87e462021-08-07 16:00:59 +0800215 msg.data[0] = (rdc_id << 8) | 0x2; /* A35 XRDC */
216
217 mu_hal_init(s_mu_base);
218 mu_hal_sendmsg(s_mu_base, 0, *((u32 *)&msg));
219 mu_hal_sendmsg(s_mu_base, 1, msg.data[0]);
220
221 ret = mu_hal_receivemsg(s_mu_base, 0, (u32 *)&msg);
222 if (!ret) {
223 ret = mu_hal_receivemsg(s_mu_base, 1, &msg.data[0]);
224 if (!ret) {
225 if ((msg.data[0] & 0xff) == 0xd6)
226 return 0;
227 }
228
229 return -EIO;
230 }
231
232 return ret;
233}
234
235void xrdc_mrc_region_set_access(int mrc_index, u32 addr, u32 access)
236{
237 ulong xrdc_base = 0x292f0000, off;
238 u32 mrgd[5];
239 u8 mrcfg, j, region_num;
240 u8 dsel;
241
242 mrcfg = readb(xrdc_base + 0x140 + mrc_index);
243 region_num = mrcfg & 0x1f;
244
245 for (j = 0; j < region_num; j++) {
246 off = 0x2000 + mrc_index * 0x200 + j * 0x20;
247
248 mrgd[0] = readl(xrdc_base + off);
249 mrgd[1] = readl(xrdc_base + off + 4);
250 mrgd[2] = readl(xrdc_base + off + 8);
251 mrgd[3] = readl(xrdc_base + off + 0xc);
252 mrgd[4] = readl(xrdc_base + off + 0x10);
253
254 debug("MRC [%u][%u]\n", mrc_index, j);
255 debug("0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
256 mrgd[0], mrgd[1], mrgd[2], mrgd[3], mrgd[4]);
257
258 /* hit */
259 if (addr >= mrgd[0] && addr <= mrgd[1]) {
260 /* find domain 7 DSEL */
261 dsel = (mrgd[2] >> 21) & 0x7;
262 if (dsel == 1) {
263 mrgd[4] &= ~0xFFF;
264 mrgd[4] |= (access & 0xFFF);
265 } else if (dsel == 2) {
266 mrgd[4] &= ~0xFFF0000;
267 mrgd[4] |= ((access & 0xFFF) << 16);
268 }
269
Peng Fand5c31832023-06-15 18:09:05 +0800270 /* not handle other cases, since ELE only set ACCESS1 and 2 */
Peng Fan9c87e462021-08-07 16:00:59 +0800271 writel(mrgd[4], xrdc_base + off + 0x10);
272 return;
273 }
274 }
275}
276
277void xrdc_init_mda(void)
278{
279 ulong xrdc_base = XRDC_ADDR, off;
280 u32 i = 0;
281
282 /* Set MDA3-5 for PXP, ENET, CAAM to DID 1*/
283 for (i = 3; i <= 5; i++) {
284 off = 0x800 + i * 0x20;
285 writel(0x200000A1, xrdc_base + off);
286 writel(0xA00000A1, xrdc_base + off);
287 }
288
289 /* Set MDA10 -15 to DID 3 for video */
290 for (i = 10; i <= 15; i++) {
291 off = 0x800 + i * 0x20;
292 writel(0x200000A3, xrdc_base + off);
293 writel(0xA00000A3, xrdc_base + off);
294 }
295}
296
297void xrdc_init_mrc(void)
298{
Peng Fand5c31832023-06-15 18:09:05 +0800299 /* Re-config MRC3 for SRAM0 in case protected by ELE */
Ye Liec7a3852023-01-31 16:42:20 +0800300 xrdc_config_mrc_w0_w1(3, 0, 0x22010000, 0x10000);
301 xrdc_config_mrc_dx_perm(3, 0, 0, 1);
302 xrdc_config_mrc_dx_perm(3, 0, 1, 1);
303 xrdc_config_mrc_dx_perm(3, 0, 4, 1);
304 xrdc_config_mrc_dx_perm(3, 0, 5, 1);
305 xrdc_config_mrc_dx_perm(3, 0, 6, 1);
306 xrdc_config_mrc_dx_perm(3, 0, 7, 1);
307 xrdc_config_mrc_w3_w4(3, 0, 0x0, 0x80000FFF);
308
309 /* Clear other 3 regions of MRC3 to invalid */
310 xrdc_config_mrc_w3_w4(3, 1, 0x0, 0x0);
311 xrdc_config_mrc_w3_w4(3, 2, 0x0, 0x0);
312 xrdc_config_mrc_w3_w4(3, 3, 0x0, 0x0);
313
Ye Li43e9b7b2023-01-31 16:42:15 +0800314 /* Set MRC4 and MRC5 for DDR access from A35 and AP NIC PER masters */
315 xrdc_config_mrc_w0_w1(4, 0, CFG_SYS_SDRAM_BASE, PHYS_SDRAM_SIZE);
316 xrdc_config_mrc_dx_perm(4, 0, 1, 1);
317 xrdc_config_mrc_dx_perm(4, 0, 7, 1);
318 xrdc_config_mrc_w3_w4(4, 0, 0x0, 0x80000FFF);
319
320 xrdc_config_mrc_w0_w1(5, 0, CFG_SYS_SDRAM_BASE, PHYS_SDRAM_SIZE);
321 xrdc_config_mrc_dx_perm(5, 0, 1, 1);
322 xrdc_config_mrc_w3_w4(5, 0, 0x0, 0x80000FFF);
323
Peng Fand5c31832023-06-15 18:09:05 +0800324 /* Set MRC6 for DDR access from ELE */
Ye Li5e35bdc2023-01-31 16:42:18 +0800325 xrdc_config_mrc_w0_w1(6, 0, CFG_SYS_SDRAM_BASE, PHYS_SDRAM_SIZE);
326 xrdc_config_mrc_dx_perm(6, 0, 4, 1);
327 xrdc_config_mrc_w3_w4(6, 0, 0x0, 0x80000FFF);
328
Peng Fan9c87e462021-08-07 16:00:59 +0800329 /* The MRC8 is for SRAM1 */
330 xrdc_config_mrc_w0_w1(8, 0, 0x21000000, 0x10000);
331 /* Allow for all domains: So domain 2/3 (HIFI DSP/LPAV) is ok to access */
332 xrdc_config_mrc_dx_perm(8, 0, 0, 1);
333 xrdc_config_mrc_dx_perm(8, 0, 1, 1);
334 xrdc_config_mrc_dx_perm(8, 0, 2, 1);
335 xrdc_config_mrc_dx_perm(8, 0, 3, 1);
336 xrdc_config_mrc_dx_perm(8, 0, 4, 1);
337 xrdc_config_mrc_dx_perm(8, 0, 5, 1);
338 xrdc_config_mrc_dx_perm(8, 0, 6, 1);
339 xrdc_config_mrc_dx_perm(8, 0, 7, 1);
340 xrdc_config_mrc_w3_w4(8, 0, 0x0, 0x80000FFF);
341
342 /* The MRC6 is for video modules to ddr */
343 xrdc_config_mrc_w0_w1(6, 0, 0x80000000, 0x80000000);
344 xrdc_config_mrc_dx_perm(6, 0, 3, 1); /* allow for domain 3 video */
345 xrdc_config_mrc_w3_w4(6, 0, 0x0, 0x80000FFF);
346}
347
Ye Li7edb3622023-01-31 16:42:24 +0800348void xrdc_init_pdac_msc(void)
349{
350 /* Init LPAV PDAC and MSC for DDR init */
351 xrdc_config_pdac(5, 36, 6, 0x7); /* CMC2*/
352 xrdc_config_pdac(5, 36, 7, 0x7);
353 xrdc_config_pdac(5, 37, 6, 0x7); /* SIM2 */
354 xrdc_config_pdac(5, 37, 7, 0x7);
355 xrdc_config_pdac(5, 38, 6, 0x7); /* CGC2 */
356 xrdc_config_pdac(5, 38, 7, 0x7);
357 xrdc_config_pdac(5, 39, 6, 0x7); /* PCC5 */
358 xrdc_config_pdac(5, 39, 7, 0x7);
359
360 xrdc_config_msc(0, 0, 6, 0x7); /* GPIOE */
361 xrdc_config_msc(0, 0, 7, 0x7);
362 xrdc_config_msc(0, 1, 6, 0x7); /* GPIOF */
363 xrdc_config_msc(0, 1, 7, 0x7);
364 xrdc_config_msc(1, 0, 6, 0x7); /* GPIOD */
365 xrdc_config_msc(1, 0, 7, 0x7);
366 xrdc_config_msc(2, 6, 6, 0x7); /* DDR controller */
367 xrdc_config_msc(2, 6, 7, 0x7);
368}
369
Peng Fan9c87e462021-08-07 16:00:59 +0800370int trdc_mbc_set_access(u32 mbc_x, u32 dom_x, u32 mem_x, u32 blk_x, bool sec_access)
371{
372 struct trdc *trdc_base = (struct trdc *)0x28031000U;
373 struct mbc_mem_dom *mbc_dom;
374 u32 *cfg_w, *nse_w;
375 u32 index, offset, val;
376
377 mbc_dom = &trdc_base->mem_dom[mbc_x][dom_x];
378
379 switch (mem_x) {
380 case 0:
381 cfg_w = &mbc_dom->mem0_blk_cfg_w[blk_x / 8];
382 nse_w = &mbc_dom->mem0_blk_nse_w[blk_x / 32];
383 break;
384 case 1:
385 cfg_w = &mbc_dom->mem1_blk_cfg_w[blk_x / 8];
386 nse_w = &mbc_dom->mem1_blk_nse_w[blk_x / 32];
387 break;
388 case 2:
389 cfg_w = &mbc_dom->mem2_blk_cfg_w[blk_x / 8];
390 nse_w = &mbc_dom->mem2_blk_nse_w[blk_x / 32];
391 break;
392 case 3:
393 cfg_w = &mbc_dom->mem3_blk_cfg_w[blk_x / 8];
394 nse_w = &mbc_dom->mem3_blk_nse_w[blk_x / 32];
395 break;
396 default:
397 return -EINVAL;
398 };
399
400 index = blk_x % 8;
401 offset = index * 4;
402
403 val = readl((void __iomem *)cfg_w);
404
405 val &= ~(0xFU << offset);
406
407 /* MBC0-3
Peng Fand5c31832023-06-15 18:09:05 +0800408 * Global 0, 0x7777 secure pri/user read/write/execute, ELE has already set it.
Peng Fan9c87e462021-08-07 16:00:59 +0800409 * So select MBC0_MEMN_GLBAC0
410 */
411 if (sec_access) {
412 val |= (0x0 << offset);
413 writel(val, (void __iomem *)cfg_w);
414 } else {
415 val |= (0x8 << offset); /* nse bit set */
416 writel(val, (void __iomem *)cfg_w);
417 }
418
419 return 0;
420}
421
422int trdc_mrc_region_set_access(u32 mrc_x, u32 dom_x, u32 addr_start, u32 addr_end, bool sec_access)
423{
424 struct trdc *trdc_base = (struct trdc *)0x28031000U;
425 struct mrc_rgn_dom *mrc_dom;
426 u32 *desc_w;
427 u32 start, end;
428 u32 i, free = 8;
429 bool vld, hit = false;
430
431 mrc_dom = &trdc_base->mrc_dom[mrc_x][dom_x];
432
433 for (i = 0; i < 8; i++) {
434 desc_w = &mrc_dom->rgn_desc_words[i][0];
435
436 start = readl((void __iomem *)desc_w) & 0xfff;
437 end = readl((void __iomem *)(desc_w + 1));
438 vld = end & 0x1;
439 end = end & 0xfff;
440
441 if (start == 0 && end == 0 && !vld && free >= 8)
442 free = i;
443
444 /* Check all the region descriptors, even overlap */
445 if (addr_start >= end || addr_end <= start || !vld)
446 continue;
447
448 /* MRC0,1
Peng Fand5c31832023-06-15 18:09:05 +0800449 * Global 0, 0x7777 secure pri/user read/write/execute, ELE has already set it.
Peng Fan9c87e462021-08-07 16:00:59 +0800450 * So select MRCx_MEMN_GLBAC0
451 */
452 if (sec_access) {
453 writel(start, (void __iomem *)desc_w);
454 writel(end | 0x1, (void __iomem *)(desc_w + 1));
455 } else {
456 writel(start, (void __iomem *)desc_w);
457 writel((end | 0x1 | 0x10), (void __iomem *)(desc_w + 1));
458 }
459
460 if (addr_start >= start && addr_end <= end)
461 hit = true;
462 }
463
464 if (!hit) {
465 if (free >= 8)
466 return -EFAULT;
467
468 desc_w = &mrc_dom->rgn_desc_words[free][0];
469
470 addr_start &= ~0xfff;
471 addr_end &= ~0xfff;
472
473 if (sec_access) {
474 writel(addr_start, (void __iomem *)desc_w);
475 writel(addr_end | 0x1, (void __iomem *)(desc_w + 1));
476 } else {
477 writel(addr_start, (void __iomem *)desc_w);
478 writel((addr_end | 0x1 | 0x10), (void __iomem *)(desc_w + 1));
479 }
480 }
481
482 return 0;
483}