mmu.c 8.5 KB

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