mmu.c 7.6 KB
Newer Older
1 2 3 4 5 6 7
/*
 * File      : mmu.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
8
 * http://www.rt-thread.org/license/LICENSE
9 10 11 12
 *
 * Change Logs:
 * Date           Author       Notes
 * 2008-04-25     Yi.qiu       first version
13
 * 2009-12-18     Bernard      port to armcc
14 15 16 17 18 19 20 21
 */

#include <rtthread.h>
#include "s3c24x0.h"

// #define _MMUTT_STARTADDRESS	 0x30080000
#define _MMUTT_STARTADDRESS	 0x30200000

22 23 24 25 26 27 28
#define DESC_SEC		(0x2|(1<<4))
#define CB				(3<<2)  //cache_on, write_back
#define CNB				(2<<2)  //cache_on, write_through
#define NCB				(1<<2)  //cache_off,WR_BUF on
#define NCNB			(0<<2)  //cache_off,WR_BUF off
#define AP_RW			(3<<10) //supervisor=RW, user=RW
#define AP_RO			(2<<10) //supervisor=RW, user=RO
29 30

#define DOMAIN_FAULT	(0x0)
31
#define DOMAIN_CHK		(0x1)
32
#define DOMAIN_NOTCHK	(0x3)
33 34
#define DOMAIN0			(0x0<<5)
#define DOMAIN1			(0x1<<5)
35 36 37 38 39 40 41 42 43

#define DOMAIN0_ATTR	(DOMAIN_CHK<<0)
#define DOMAIN1_ATTR	(DOMAIN_FAULT<<2)

#define RW_CB		(AP_RW|DOMAIN0|CB|DESC_SEC)
#define RW_CNB		(AP_RW|DOMAIN0|CNB|DESC_SEC)
#define RW_NCNB		(AP_RW|DOMAIN0|NCNB|DESC_SEC)
#define RW_FAULT	(AP_RW|DOMAIN1|NCNB|DESC_SEC)

B
bernard.xiong 已提交
44
#ifdef __GNUC__
45 46 47 48 49
void mmu_setttbase(register rt_uint32_t i)
{
	asm ("mcr p15, 0, %0, c2, c2, 0": :"r" (i));
}

50
void mmu_set_domain(register rt_uint32_t i)
51 52 53 54
{
	asm ("mcr p15,0, %0, c3, c0,  0": :"r" (i));
}

55
void mmu_enable()
56 57 58 59 60 61 62 63 64 65 66 67
{
	register rt_uint32_t i;

	/* read control register */
	asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));

	i |= 0x1;

	/* write back to control register */
	asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
}

68
void mmu_disable()
69 70 71 72 73 74 75 76 77 78 79 80
{
	register rt_uint32_t i;

	/* read control register */
	asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));

	i &= ~0x1;

	/* write back to control register */
	asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
}

81
void mmu_enable_icache()
82 83 84 85 86 87 88 89 90 91 92 93
{
	register rt_uint32_t i;

	/* read control register */
	asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));

	i |= (1 << 12);

	/* write back to control register */
	asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
}

94
void mmu_enable_dcache()
95 96 97 98 99 100 101 102 103 104 105 106
{
	register rt_uint32_t i;

	/* read control register */
	asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));

	i |= (1 << 2);

	/* write back to control register */
	asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
}

107
void mmu_disable_icache()
108 109 110 111 112 113 114 115 116 117 118 119
{
	register rt_uint32_t i;

	/* read control register */
	asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));

	i &= ~(1 << 12);

	/* write back to control register */
	asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
}

120
void mmu_disable_dcache()
121 122 123 124 125 126 127 128 129 130 131 132
{
	register rt_uint32_t i;

	/* read control register */
	asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));

	i &= ~(1 << 2);

	/* write back to control register */
	asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
}

133
void mmu_enable_alignfault()
134 135 136 137 138 139 140 141 142 143 144 145
{
	register rt_uint32_t i;

	/* read control register */
	asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));

	i |= (1 << 1);

	/* write back to control register */
	asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
}

146
void mmu_disable_alignfault()
147 148 149 150 151 152 153 154 155 156 157 158
{
	register rt_uint32_t i;

	/* read control register */
	asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));

	i &= ~(1 << 1);

	/* write back to control register */
	asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
}

159
void mmu_clean_invalidated_cache_index(int index)
160 161 162 163
{
	asm ("mcr p15, 0, %0, c7, c14, 2": :"r" (index));
}

164
void mmu_invalidate_tlb()
165 166 167 168
{
	asm ("mcr p15, 0, %0, c8, c7, 0": :"r" (0));
}

169
void mmu_invalidate_icache()
170 171 172
{
	asm ("mcr p15, 0, %0, c7, c5, 0": :"r" (0));
}
B
bernard.xiong 已提交
173 174 175 176 177 178 179 180
#endif

#ifdef __CC_ARM
__asm void mmu_setttbase(rt_uint32_t i)
{
	mcr p15, 0, r0, c2, c2, 0
}

181
__asm void mmu_set_domain(rt_uint32_t i)
B
bernard.xiong 已提交
182 183 184 185
{
	mcr p15,0, r0, c3, c0,  0
}

186
__asm void mmu_enable()
B
bernard.xiong 已提交
187 188 189 190 191 192
{
	mrc p15, 0, r0, c1, c0, 0
	orr r0, r0, #0x01
	mcr p15, 0, r0, c1, c0, 0
}

193
__asm void mmu_disable()
B
bernard.xiong 已提交
194 195 196 197 198 199
{
	mrc p15, 0, r0, c1, c0, 0
	bic r0, r0, #0x01
	mcr p15, 0, r0, c1, c0, 0
}

200
__asm void mmu_enable_icache()
B
bernard.xiong 已提交
201 202
{
	mrc p15, 0, r0, c1, c0, 0
B
bernard.xiong 已提交
203
	orr r0, r0, #0x1000
B
bernard.xiong 已提交
204 205 206
	mcr p15, 0, r0, c1, c0, 0
}

207
__asm void mmu_enable_dcache()
B
bernard.xiong 已提交
208 209
{
	mrc p15, 0, r0, c1, c0, 0
B
bernard.xiong 已提交
210
	orr r0, r0, #0x04
B
bernard.xiong 已提交
211 212 213
	mcr p15, 0, r0, c1, c0, 0
}

214
__asm void mmu_disable_icache()
B
bernard.xiong 已提交
215 216
{
	mrc p15, 0, r0, c1, c0, 0
B
bernard.xiong 已提交
217
	bic r0, r0, #0x1000
B
bernard.xiong 已提交
218 219 220
	mcr p15, 0, r0, c1, c0, 0
}

221
__asm void mmu_disable_dcache()
B
bernard.xiong 已提交
222 223
{
	mrc p15, 0, r0, c1, c0, 0
B
bernard.xiong 已提交
224
	bic r0, r0, #0x04
B
bernard.xiong 已提交
225 226 227
	mcr p15, 0, r0, c1, c0, 0
}

228
__asm void mmu_enable_alignfault()
B
bernard.xiong 已提交
229 230
{
	mrc p15, 0, r0, c1, c0, 0
B
bernard.xiong 已提交
231
	orr r0, r0, #0x02
B
bernard.xiong 已提交
232 233 234
	mcr p15, 0, r0, c1, c0, 0
}

235
__asm void mmu_disable_alignfault()
B
bernard.xiong 已提交
236 237 238 239 240 241
{
	mrc p15, 0, r0, c1, c0, 0
	bic r0, r0, #0x02
	mcr p15, 0, r0, c1, c0, 0
}

242
__asm void mmu_clean_invalidated_cache_index(int index)
B
bernard.xiong 已提交
243 244 245 246
{
	mcr p15, 0, r0, c7, c14, 2
}

247
__asm void mmu_invalidate_tlb()
B
bernard.xiong 已提交
248 249 250 251 252
{
	mov r0, #0x0
	mcr p15, 0, r0, c8, c7, 0
}

253
__asm void mmu_invalidate_icache()
B
bernard.xiong 已提交
254 255 256 257 258
{
	mov r0, #0
	mcr p15, 0, r0, c7, c5, 0
}
#endif
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

void mmu_setmtt(int vaddrStart,int vaddrEnd,int paddrStart,int attr)
{
    volatile rt_uint32_t *pTT;
    volatile int i,nSec;
    pTT=(rt_uint32_t *)_MMUTT_STARTADDRESS+(vaddrStart>>20);
    nSec=(vaddrEnd>>20)-(vaddrStart>>20);
    for(i=0;i<=nSec;i++)
    {
		*pTT = attr |(((paddrStart>>20)+i)<<20);
		pTT++;
    }
}

void rt_hw_mmu_init(void)
{
	int i,j;
	//========================== IMPORTANT NOTE =========================
	//The current stack and code area can't be re-mapped in this routine.
	//If you want memory map mapped freely, your own sophiscated mmu
	//initialization code is needed.
	//===================================================================

282 283
	mmu_disable_dcache();
	mmu_disable_icache();
284 285 286 287

	//If write-back is used,the DCache should be cleared.
	for(i=0;i<64;i++)
		for(j=0;j<8;j++)
288
			mmu_clean_invalidated_cache_index((i<<26)|(j<<5));
289

290
	mmu_invalidate_icache();
291 292

	//To complete mmu_Init() fast, Icache may be turned on here.
293
	mmu_enable_icache();
294

295 296
	mmu_disable();
	mmu_invalidate_tlb();
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

	//mmu_setmtt(int vaddrStart,int vaddrEnd,int paddrStart,int attr);
	mmu_setmtt(0x00000000,0x07f00000,0x00000000,RW_CNB);  //bank0
	mmu_setmtt(0x00000000,0x03f00000,(int)0x30000000,RW_CB);  //bank0
	mmu_setmtt(0x04000000,0x07f00000,0,RW_NCNB);  			//bank0
	mmu_setmtt(0x08000000,0x0ff00000,0x08000000,RW_CNB);  //bank1
	mmu_setmtt(0x10000000,0x17f00000,0x10000000,RW_NCNB); //bank2
	mmu_setmtt(0x18000000,0x1ff00000,0x18000000,RW_NCNB); //bank3
	//mmu_setmtt(0x20000000,0x27f00000,0x20000000,RW_CB); //bank4
	mmu_setmtt(0x20000000,0x27f00000,0x20000000,RW_CNB); //bank4 for STRATA Flash
	mmu_setmtt(0x28000000,0x2ff00000,0x28000000,RW_NCNB); //bank5
	//30f00000->30100000, 31000000->30200000
	mmu_setmtt(0x30000000,0x30100000,0x30000000,RW_CB);	  //bank6-1
	mmu_setmtt(0x30200000,0x33e00000,0x30200000,RW_NCNB); //bank6-2

	mmu_setmtt(0x33f00000,0x33f00000,0x33f00000,RW_CB);   //bank6-3
	mmu_setmtt(0x38000000,0x3ff00000,0x38000000,RW_NCNB); //bank7

	mmu_setmtt(0x40000000,0x47f00000,0x40000000,RW_NCNB); //SFR
	mmu_setmtt(0x48000000,0x5af00000,0x48000000,RW_NCNB); //SFR
	mmu_setmtt(0x5b000000,0x5b000000,0x5b000000,RW_NCNB); //SFR
	mmu_setmtt(0x5b100000,0xfff00000,0x5b100000,RW_FAULT);//not used
	mmu_setmtt(0x60000000,0x67f00000,0x60000000,RW_NCNB); //SFR

	mmu_setttbase(_MMUTT_STARTADDRESS);

	/* DOMAIN1: no_access, DOMAIN0,2~15=client(AP is checked) */
324
	mmu_set_domain(0x55555550|DOMAIN1_ATTR|DOMAIN0_ATTR);
325 326

	//mmu_SetProcessId(0x0);
327
	mmu_enable_alignfault();
328

329 330
	mmu_enable();
	mmu_enable_icache();
331 332

	/* DCache should be turned on after mmu is turned on. */
333
	mmu_enable_dcache();
334
}