1
0

mmu.c 8.6 KB

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