mmu.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. */
  13. #include <rtthread.h>
  14. #define CACHE_LINE_SIZE 32
  15. #define DESC_SEC (0x2|(1<<4))
  16. #define CB (3<<2) //cache_on, write_back
  17. #define CNB (2<<2) //cache_on, write_through
  18. #define NCB (1<<2) //cache_off,WR_BUF on
  19. #define NCNB (0<<2) //cache_off,WR_BUF off
  20. #define AP_RW (3<<10) //supervisor=RW, user=RW
  21. #define AP_RO (2<<10) //supervisor=RW, user=RO
  22. #define DOMAIN_FAULT (0x0)
  23. #define DOMAIN_CHK (0x1)
  24. #define DOMAIN_NOTCHK (0x3)
  25. #define DOMAIN0 (0x0<<5)
  26. #define DOMAIN1 (0x1<<5)
  27. #define DOMAIN0_ATTR (DOMAIN_CHK<<0)
  28. #define DOMAIN1_ATTR (DOMAIN_FAULT<<2)
  29. #define RW_CB (AP_RW|DOMAIN0|CB|DESC_SEC) /* Read/Write, cache, write back */
  30. #define RW_CNB (AP_RW|DOMAIN0|CNB|DESC_SEC) /* Read/Write, cache, write through */
  31. #define RW_NCNB (AP_RW|DOMAIN0|NCNB|DESC_SEC) /* Read/Write without cache and write buffer */
  32. #define RW_FAULT (AP_RW|DOMAIN1|NCNB|DESC_SEC) /* Read/Write without cache and write buffer */
  33. #ifdef __CC_ARM
  34. void mmu_setttbase(rt_uint32_t i)
  35. {
  36. register rt_uint32_t value;
  37. /* Invalidates all TLBs.Domain access is selected as
  38. * client by configuring domain access register,
  39. * in that case access controlled by permission value
  40. * set by page table entry
  41. */
  42. value = 0;
  43. __asm
  44. {
  45. mcr p15, 0, value, c8, c7, 0
  46. }
  47. value = 0x55555555;
  48. __asm
  49. {
  50. mcr p15, 0, value, c3, c0, 0
  51. mcr p15, 0, i, c2, c0, 0
  52. }
  53. }
  54. void mmu_set_domain(rt_uint32_t i)
  55. {
  56. __asm
  57. {
  58. mcr p15,0, i, c3, c0, 0
  59. }
  60. }
  61. void mmu_enable()
  62. {
  63. register rt_uint32_t value;
  64. __asm
  65. {
  66. mrc p15, 0, value, c1, c0, 0
  67. orr value, value, #0x01
  68. mcr p15, 0, value, c1, c0, 0
  69. }
  70. }
  71. void mmu_disable()
  72. {
  73. register rt_uint32_t value;
  74. __asm
  75. {
  76. mrc p15, 0, value, c1, c0, 0
  77. bic value, value, #0x01
  78. mcr p15, 0, value, c1, c0, 0
  79. }
  80. }
  81. void mmu_enable_icache()
  82. {
  83. register rt_uint32_t value;
  84. __asm
  85. {
  86. mrc p15, 0, value, c1, c0, 0
  87. orr value, value, #0x1000
  88. mcr p15, 0, value, c1, c0, 0
  89. }
  90. }
  91. void mmu_enable_dcache()
  92. {
  93. register rt_uint32_t value;
  94. __asm
  95. {
  96. mrc p15, 0, value, c1, c0, 0
  97. orr value, value, #0x04
  98. mcr p15, 0, value, c1, c0, 0
  99. }
  100. }
  101. void mmu_disable_icache()
  102. {
  103. register rt_uint32_t value;
  104. __asm
  105. {
  106. mrc p15, 0, value, c1, c0, 0
  107. bic value, value, #0x1000
  108. mcr p15, 0, value, c1, c0, 0
  109. }
  110. }
  111. void mmu_disable_dcache()
  112. {
  113. register rt_uint32_t value;
  114. __asm
  115. {
  116. mrc p15, 0, value, c1, c0, 0
  117. bic value, value, #0x04
  118. mcr p15, 0, value, c1, c0, 0
  119. }
  120. }
  121. void mmu_enable_alignfault()
  122. {
  123. register rt_uint32_t value;
  124. __asm
  125. {
  126. mrc p15, 0, value, c1, c0, 0
  127. orr value, value, #0x02
  128. mcr p15, 0, value, c1, c0, 0
  129. }
  130. }
  131. void mmu_disable_alignfault()
  132. {
  133. register rt_uint32_t value;
  134. __asm
  135. {
  136. mrc p15, 0, value, c1, c0, 0
  137. bic value, value, #0x02
  138. mcr p15, 0, value, c1, c0, 0
  139. }
  140. }
  141. void mmu_clean_invalidated_cache_index(int index)
  142. {
  143. __asm
  144. {
  145. mcr p15, 0, index, c7, c14, 2
  146. }
  147. }
  148. void mmu_clean_invalidated_dcache(rt_uint32_t buffer, rt_uint32_t size)
  149. {
  150. unsigned int ptr;
  151. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  152. while(ptr < buffer + size)
  153. {
  154. __asm
  155. {
  156. MCR p15, 0, ptr, c7, c14, 1
  157. }
  158. ptr += CACHE_LINE_SIZE;
  159. }
  160. }
  161. void mmu_clean_dcache(rt_uint32_t buffer, rt_uint32_t size)
  162. {
  163. unsigned int ptr;
  164. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  165. while (ptr < buffer + size)
  166. {
  167. __asm
  168. {
  169. MCR p15, 0, ptr, c7, c10, 1
  170. }
  171. ptr += CACHE_LINE_SIZE;
  172. }
  173. }
  174. void mmu_invalidate_dcache(rt_uint32_t buffer, rt_uint32_t size)
  175. {
  176. unsigned int ptr;
  177. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  178. while (ptr < buffer + size)
  179. {
  180. __asm
  181. {
  182. MCR p15, 0, ptr, c7, c6, 1
  183. }
  184. ptr += CACHE_LINE_SIZE;
  185. }
  186. }
  187. void mmu_invalidate_tlb()
  188. {
  189. register rt_uint32_t value;
  190. value = 0;
  191. __asm
  192. {
  193. mcr p15, 0, value, c8, c7, 0
  194. }
  195. }
  196. void mmu_invalidate_icache()
  197. {
  198. register rt_uint32_t value;
  199. value = 0;
  200. __asm
  201. {
  202. mcr p15, 0, value, c7, c5, 0
  203. }
  204. }
  205. void mmu_invalidate_dcache_all()
  206. {
  207. register rt_uint32_t value;
  208. value = 0;
  209. __asm
  210. {
  211. mcr p15, 0, value, c7, c6, 0
  212. }
  213. }
  214. #elif defined(__GNUC__)
  215. void mmu_setttbase(register rt_uint32_t i)
  216. {
  217. register rt_uint32_t value;
  218. /* Invalidates all TLBs.Domain access is selected as
  219. * client by configuring domain access register,
  220. * in that case access controlled by permission value
  221. * set by page table entry
  222. */
  223. value = 0;
  224. asm ("mcr p15, 0, %0, c8, c7, 0"::"r"(value));
  225. value = 0x55555555;
  226. asm ("mcr p15, 0, %0, c3, c0, 0"::"r"(value));
  227. asm ("mcr p15, 0, %0, c2, c0, 0"::"r"(i));
  228. }
  229. void mmu_set_domain(register rt_uint32_t i)
  230. {
  231. asm ("mcr p15,0, %0, c3, c0, 0": :"r" (i));
  232. }
  233. void mmu_enable()
  234. {
  235. register rt_uint32_t i;
  236. /* read control register */
  237. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  238. i |= 0x1;
  239. /* write back to control register */
  240. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  241. }
  242. void mmu_disable()
  243. {
  244. register rt_uint32_t i;
  245. /* read control register */
  246. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  247. i &= ~0x1;
  248. /* write back to control register */
  249. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  250. }
  251. void mmu_enable_icache()
  252. {
  253. register rt_uint32_t i;
  254. /* read control register */
  255. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  256. i |= (1 << 12);
  257. /* write back to control register */
  258. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  259. }
  260. void mmu_enable_dcache()
  261. {
  262. register rt_uint32_t i;
  263. /* read control register */
  264. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  265. i |= (1 << 2);
  266. /* write back to control register */
  267. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  268. }
  269. void mmu_disable_icache()
  270. {
  271. register rt_uint32_t i;
  272. /* read control register */
  273. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  274. i &= ~(1 << 12);
  275. /* write back to control register */
  276. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  277. }
  278. void mmu_disable_dcache()
  279. {
  280. register rt_uint32_t i;
  281. /* read control register */
  282. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  283. i &= ~(1 << 2);
  284. /* write back to control register */
  285. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  286. }
  287. void mmu_enable_alignfault()
  288. {
  289. register rt_uint32_t i;
  290. /* read control register */
  291. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  292. i |= (1 << 1);
  293. /* write back to control register */
  294. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  295. }
  296. void mmu_disable_alignfault()
  297. {
  298. register rt_uint32_t i;
  299. /* read control register */
  300. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  301. i &= ~(1 << 1);
  302. /* write back to control register */
  303. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  304. }
  305. void mmu_clean_invalidated_cache_index(int index)
  306. {
  307. asm ("mcr p15, 0, %0, c7, c14, 2": :"r" (index));
  308. }
  309. void mmu_clean_invalidated_dcache(rt_uint32_t buffer, rt_uint32_t size)
  310. {
  311. unsigned int ptr;
  312. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  313. while(ptr < buffer + size)
  314. {
  315. asm ("mcr p15, 0, %0, c7, c14, 1": :"r" (ptr));
  316. ptr += CACHE_LINE_SIZE;
  317. }
  318. }
  319. void mmu_clean_dcache(rt_uint32_t buffer, rt_uint32_t size)
  320. {
  321. unsigned int ptr;
  322. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  323. while (ptr < buffer + size)
  324. {
  325. asm ("mcr p15, 0, %0, c7, c10, 1": :"r" (ptr));
  326. ptr += CACHE_LINE_SIZE;
  327. }
  328. }
  329. void mmu_invalidate_dcache(rt_uint32_t buffer, rt_uint32_t size)
  330. {
  331. unsigned int ptr;
  332. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  333. while (ptr < buffer + size)
  334. {
  335. asm ("mcr p15, 0, %0, c7, c6, 1": :"r" (ptr));
  336. ptr += CACHE_LINE_SIZE;
  337. }
  338. }
  339. void mmu_invalidate_tlb()
  340. {
  341. asm ("mcr p15, 0, %0, c8, c7, 0": :"r" (0));
  342. }
  343. void mmu_invalidate_icache()
  344. {
  345. asm ("mcr p15, 0, %0, c7, c5, 0": :"r" (0));
  346. }
  347. void mmu_invalidate_dcache_all()
  348. {
  349. asm ("mcr p15, 0, %0, c7, c6, 0": :"r" (0));
  350. }
  351. #endif
  352. /* level1 page table */
  353. static volatile unsigned int _page_table[4*1024] __attribute__((aligned(16*1024)));
  354. void mmu_setmtt(rt_uint32_t vaddrStart, rt_uint32_t vaddrEnd, rt_uint32_t paddrStart, rt_uint32_t attr)
  355. {
  356. volatile rt_uint32_t *pTT;
  357. volatile int i,nSec;
  358. pTT=(rt_uint32_t *)_page_table+(vaddrStart>>20);
  359. nSec=(vaddrEnd>>20)-(vaddrStart>>20);
  360. for(i=0;i<=nSec;i++)
  361. {
  362. *pTT = attr |(((paddrStart>>20)+i)<<20);
  363. pTT++;
  364. }
  365. }
  366. void rt_hw_mmu_init(void)
  367. {
  368. /* disable I/D cache */
  369. mmu_disable_dcache();
  370. mmu_disable_icache();
  371. mmu_disable();
  372. mmu_invalidate_tlb();
  373. /* set page table */
  374. mmu_setmtt(0x00000000, 0xFFFFFFFF, 0x00000000, RW_NCNB); /* None cached for 4G memory */
  375. mmu_setmtt(0x20000000, 0x24000000-1, 0x20000000, RW_CB); /* 64M cached SDRAM memory */
  376. mmu_setmtt(0x00000000, 0x100000, 0x20000000, RW_CB); /* isr vector table */
  377. mmu_setmtt(0x90000000, 0x90400000 - 1, 0x00200000, RW_NCNB); /* 4K SRAM0 + 4k SRAM1 */
  378. mmu_setmtt(0xA0000000, 0xA4000000-1, 0x20000000, RW_NCNB); /* 64M none-cached SDRAM memory */
  379. /* set MMU table address */
  380. mmu_setttbase((rt_uint32_t)_page_table);
  381. /* enables MMU */
  382. mmu_enable();
  383. /* enable Instruction Cache */
  384. mmu_enable_icache();
  385. /* enable Data Cache */
  386. mmu_enable_dcache();
  387. mmu_invalidate_icache();
  388. mmu_invalidate_dcache_all();
  389. }