mmu.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * File : mmu.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2008-04-25 Yi.qiu first version
  13. * 2009-12-18 Bernard port to armcc
  14. */
  15. #include <rtthread.h>
  16. #include "s3c24x0.h"
  17. // #define _MMUTT_STARTADDRESS 0x30080000
  18. #define _MMUTT_STARTADDRESS 0x30200000
  19. #define DESC_SEC (0x2|(1<<4))
  20. #define CB (3<<2) //cache_on, write_back
  21. #define CNB (2<<2) //cache_on, write_through
  22. #define NCB (1<<2) //cache_off,WR_BUF on
  23. #define NCNB (0<<2) //cache_off,WR_BUF off
  24. #define AP_RW (3<<10) //supervisor=RW, user=RW
  25. #define AP_RO (2<<10) //supervisor=RW, user=RO
  26. #define DOMAIN_FAULT (0x0)
  27. #define DOMAIN_CHK (0x1)
  28. #define DOMAIN_NOTCHK (0x3)
  29. #define DOMAIN0 (0x0<<5)
  30. #define DOMAIN1 (0x1<<5)
  31. #define DOMAIN0_ATTR (DOMAIN_CHK<<0)
  32. #define DOMAIN1_ATTR (DOMAIN_FAULT<<2)
  33. #define RW_CB (AP_RW|DOMAIN0|CB|DESC_SEC)
  34. #define RW_CNB (AP_RW|DOMAIN0|CNB|DESC_SEC)
  35. #define RW_NCNB (AP_RW|DOMAIN0|NCNB|DESC_SEC)
  36. #define RW_FAULT (AP_RW|DOMAIN1|NCNB|DESC_SEC)
  37. #ifdef __GNUC__
  38. void mmu_setttbase(register rt_uint32_t i)
  39. {
  40. asm ("mcr p15, 0, %0, c2, c2, 0": :"r" (i));
  41. }
  42. void mmu_set_domain(register rt_uint32_t i)
  43. {
  44. asm ("mcr p15,0, %0, c3, c0, 0": :"r" (i));
  45. }
  46. void mmu_enable()
  47. {
  48. register rt_uint32_t i;
  49. /* read control register */
  50. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  51. i |= 0x1;
  52. /* write back to control register */
  53. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  54. }
  55. void mmu_disable()
  56. {
  57. register rt_uint32_t i;
  58. /* read control register */
  59. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  60. i &= ~0x1;
  61. /* write back to control register */
  62. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  63. }
  64. void mmu_enable_icache()
  65. {
  66. register rt_uint32_t i;
  67. /* read control register */
  68. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  69. i |= (1 << 12);
  70. /* write back to control register */
  71. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  72. }
  73. void mmu_enable_dcache()
  74. {
  75. register rt_uint32_t i;
  76. /* read control register */
  77. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  78. i |= (1 << 2);
  79. /* write back to control register */
  80. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  81. }
  82. void mmu_disable_icache()
  83. {
  84. register rt_uint32_t i;
  85. /* read control register */
  86. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  87. i &= ~(1 << 12);
  88. /* write back to control register */
  89. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  90. }
  91. void mmu_disable_dcache()
  92. {
  93. register rt_uint32_t i;
  94. /* read control register */
  95. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  96. i &= ~(1 << 2);
  97. /* write back to control register */
  98. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  99. }
  100. void mmu_enable_alignfault()
  101. {
  102. register rt_uint32_t i;
  103. /* read control register */
  104. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  105. i |= (1 << 1);
  106. /* write back to control register */
  107. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  108. }
  109. void mmu_disable_alignfault()
  110. {
  111. register rt_uint32_t i;
  112. /* read control register */
  113. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  114. i &= ~(1 << 1);
  115. /* write back to control register */
  116. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  117. }
  118. void mmu_clean_invalidated_cache_index(int index)
  119. {
  120. asm ("mcr p15, 0, %0, c7, c14, 2": :"r" (index));
  121. }
  122. void mmu_invalidate_tlb()
  123. {
  124. asm ("mcr p15, 0, %0, c8, c7, 0": :"r" (0));
  125. }
  126. void mmu_invalidate_icache()
  127. {
  128. asm ("mcr p15, 0, %0, c7, c5, 0": :"r" (0));
  129. }
  130. #endif
  131. #ifdef __CC_ARM
  132. void mmu_setttbase(rt_uint32_t i)
  133. {
  134. __asm
  135. {
  136. mcr p15, 0, i, c2, c2, 0
  137. }
  138. }
  139. void mmu_set_domain(rt_uint32_t i)
  140. {
  141. __asm
  142. {
  143. mcr p15,0, i, c3, c0, 0
  144. }
  145. }
  146. void mmu_enable()
  147. {
  148. register rt_uint32_t value;
  149. __asm
  150. {
  151. mrc p15, 0, value, c1, c0, 0
  152. orr value, value, #0x01
  153. mcr p15, 0, value, c1, c0, 0
  154. }
  155. }
  156. void mmu_disable()
  157. {
  158. register rt_uint32_t value;
  159. __asm
  160. {
  161. mrc p15, 0, value, c1, c0, 0
  162. bic value, value, #0x01
  163. mcr p15, 0, value, c1, c0, 0
  164. }
  165. }
  166. void mmu_enable_icache()
  167. {
  168. register rt_uint32_t value;
  169. __asm
  170. {
  171. mrc p15, 0, value, c1, c0, 0
  172. orr value, value, #0x1000
  173. mcr p15, 0, value, c1, c0, 0
  174. }
  175. }
  176. void mmu_enable_dcache()
  177. {
  178. register rt_uint32_t value;
  179. __asm
  180. {
  181. mrc p15, 0, value, c1, c0, 0
  182. orr value, value, #0x04
  183. mcr p15, 0, value, c1, c0, 0
  184. }
  185. }
  186. void mmu_disable_icache()
  187. {
  188. register rt_uint32_t value;
  189. __asm
  190. {
  191. mrc p15, 0, value, c1, c0, 0
  192. bic value, value, #0x1000
  193. mcr p15, 0, value, c1, c0, 0
  194. }
  195. }
  196. void mmu_disable_dcache()
  197. {
  198. register rt_uint32_t value;
  199. __asm
  200. {
  201. mrc p15, 0, value, c1, c0, 0
  202. bic value, value, #0x04
  203. mcr p15, 0, value, c1, c0, 0
  204. }
  205. }
  206. void mmu_enable_alignfault()
  207. {
  208. register rt_uint32_t value;
  209. __asm
  210. {
  211. mrc p15, 0, value, c1, c0, 0
  212. orr value, value, #0x02
  213. mcr p15, 0, value, c1, c0, 0
  214. }
  215. }
  216. void mmu_disable_alignfault()
  217. {
  218. register rt_uint32_t value;
  219. __asm
  220. {
  221. mrc p15, 0, value, c1, c0, 0
  222. bic value, value, #0x02
  223. mcr p15, 0, value, c1, c0, 0
  224. }
  225. }
  226. void mmu_clean_invalidated_cache_index(int index)
  227. {
  228. __asm
  229. {
  230. mcr p15, 0, index, c7, c14, 2
  231. }
  232. }
  233. void mmu_invalidate_tlb()
  234. {
  235. register rt_uint32_t value;
  236. value = 0;
  237. __asm
  238. {
  239. mcr p15, 0, value, c8, c7, 0
  240. }
  241. }
  242. void mmu_invalidate_icache()
  243. {
  244. register rt_uint32_t value;
  245. value = 0;
  246. __asm
  247. {
  248. mcr p15, 0, value, c7, c5, 0
  249. }
  250. }
  251. #endif
  252. void mmu_setmtt(int vaddrStart,int vaddrEnd,int paddrStart,int attr)
  253. {
  254. volatile rt_uint32_t *pTT;
  255. volatile int i,nSec;
  256. pTT=(rt_uint32_t *)_MMUTT_STARTADDRESS+(vaddrStart>>20);
  257. nSec=(vaddrEnd>>20)-(vaddrStart>>20);
  258. for(i=0;i<=nSec;i++)
  259. {
  260. *pTT = attr |(((paddrStart>>20)+i)<<20);
  261. pTT++;
  262. }
  263. }
  264. void rt_hw_mmu_init(void)
  265. {
  266. int i,j;
  267. //========================== IMPORTANT NOTE =========================
  268. //The current stack and code area can't be re-mapped in this routine.
  269. //If you want memory map mapped freely, your own sophiscated mmu
  270. //initialization code is needed.
  271. //===================================================================
  272. mmu_disable_dcache();
  273. mmu_disable_icache();
  274. //If write-back is used,the DCache should be cleared.
  275. for(i=0;i<64;i++)
  276. for(j=0;j<8;j++)
  277. mmu_clean_invalidated_cache_index((i<<26)|(j<<5));
  278. mmu_invalidate_icache();
  279. //To complete mmu_Init() fast, Icache may be turned on here.
  280. mmu_enable_icache();
  281. mmu_disable();
  282. mmu_invalidate_tlb();
  283. //mmu_setmtt(int vaddrStart,int vaddrEnd,int paddrStart,int attr);
  284. mmu_setmtt(0x00000000,0x07f00000,0x00000000,RW_CNB); //bank0
  285. mmu_setmtt(0x00000000,0x03f00000,(int)0x30000000,RW_CB); //bank0
  286. mmu_setmtt(0x04000000,0x07f00000,0,RW_NCNB); //bank0
  287. mmu_setmtt(0x08000000,0x0ff00000,0x08000000,RW_CNB); //bank1
  288. mmu_setmtt(0x10000000,0x17f00000,0x10000000,RW_NCNB); //bank2
  289. mmu_setmtt(0x18000000,0x1ff00000,0x18000000,RW_NCNB); //bank3
  290. //mmu_setmtt(0x20000000,0x27f00000,0x20000000,RW_CB); //bank4
  291. mmu_setmtt(0x20000000,0x27f00000,0x20000000,RW_NCNB); //bank4 for DM9000
  292. mmu_setmtt(0x28000000,0x2ff00000,0x28000000,RW_NCNB); //bank5
  293. //30f00000->30100000, 31000000->30200000
  294. mmu_setmtt(0x30000000,0x30100000,0x30000000,RW_CB); //bank6-1
  295. mmu_setmtt(0x30200000,0x33e00000,0x30200000,RW_NCNB); //bank6-2
  296. mmu_setmtt(0x33f00000,0x33f00000,0x33f00000,RW_CB); //bank6-3
  297. mmu_setmtt(0x38000000,0x3ff00000,0x38000000,RW_NCNB); //bank7
  298. mmu_setmtt(0x40000000,0x47f00000,0x40000000,RW_NCNB); //SFR
  299. mmu_setmtt(0x48000000,0x5af00000,0x48000000,RW_NCNB); //SFR
  300. mmu_setmtt(0x5b000000,0x5b000000,0x5b000000,RW_NCNB); //SFR
  301. mmu_setmtt(0x5b100000,0xfff00000,0x5b100000,RW_FAULT);//not used
  302. mmu_setmtt(0x60000000,0x67f00000,0x60000000,RW_NCNB); //SFR
  303. mmu_setttbase(_MMUTT_STARTADDRESS);
  304. /* DOMAIN1: no_access, DOMAIN0,2~15=client(AP is checked) */
  305. mmu_set_domain(0x55555550|DOMAIN1_ATTR|DOMAIN0_ATTR);
  306. mmu_enable_alignfault();
  307. mmu_enable();
  308. /* ICache enable */
  309. mmu_enable_icache();
  310. /* DCache should be turned on after mmu is turned on. */
  311. mmu_enable_dcache();
  312. }