2013-09-20 21:20:51 +08:00
|
|
|
/*
|
2021-03-27 17:51:56 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2013-09-20 21:20:51 +08:00
|
|
|
*
|
2018-10-15 01:35:07 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-09-20 21:20:51 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-01-10 bernard porting to AM1808
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include "am33xx.h"
|
2018-09-15 11:37:14 +08:00
|
|
|
#include <mmu.h>
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2018-09-15 11:37:14 +08:00
|
|
|
extern void rt_cpu_dcache_disable(void);
|
|
|
|
extern void rt_hw_cpu_dcache_enable(void);
|
|
|
|
extern void rt_cpu_icache_disable(void);
|
|
|
|
extern void rt_hw_cpu_icache_enable(void);
|
|
|
|
extern void rt_cpu_mmu_disable(void);
|
|
|
|
extern void rt_cpu_mmu_enable(void);
|
|
|
|
extern void rt_cpu_tlb_set(register rt_uint32_t i);
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2018-09-15 11:37:14 +08:00
|
|
|
void mmu_disable_dcache()
|
2013-09-20 21:20:51 +08:00
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
rt_cpu_dcache_disable();
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_enable_dcache()
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
rt_hw_cpu_dcache_enable();
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_disable_icache()
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
rt_cpu_icache_disable();
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
2018-09-15 11:37:14 +08:00
|
|
|
void mmu_enable_icache()
|
2013-09-20 21:20:51 +08:00
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
rt_hw_cpu_icache_enable();
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
2018-09-15 11:37:14 +08:00
|
|
|
void mmu_disable()
|
2013-09-20 21:20:51 +08:00
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
rt_cpu_mmu_disable();
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
2018-09-15 11:37:14 +08:00
|
|
|
void mmu_enable()
|
2013-09-20 21:20:51 +08:00
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
rt_cpu_mmu_enable();
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_setttbase(register rt_uint32_t i)
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
register rt_uint32_t value;
|
2013-09-20 21:20:51 +08:00
|
|
|
|
|
|
|
/* Invalidates all TLBs.Domain access is selected as
|
|
|
|
* client by configuring domain access register,
|
|
|
|
* in that case access controlled by permission value
|
|
|
|
* set by page table entry
|
|
|
|
*/
|
2021-03-27 17:51:56 +08:00
|
|
|
value = 0;
|
|
|
|
asm volatile ("mcr p15, 0, %0, c8, c7, 0"::"r"(value));
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
value = 0x55555555;
|
|
|
|
asm volatile ("mcr p15, 0, %0, c3, c0, 0"::"r"(value));
|
|
|
|
|
|
|
|
rt_cpu_tlb_set(i);
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_set_domain(register rt_uint32_t i)
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
asm volatile ("mcr p15,0, %0, c3, c0, 0": :"r" (i));
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_enable_alignfault()
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
register rt_uint32_t i;
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
/* read control register */
|
|
|
|
asm volatile ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
i |= (1 << 1);
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
/* write back to control register */
|
|
|
|
asm volatile ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_disable_alignfault()
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
register rt_uint32_t i;
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
/* read control register */
|
|
|
|
asm volatile ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
i &= ~(1 << 1);
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
/* write back to control register */
|
|
|
|
asm volatile ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_clean_invalidated_cache_index(int index)
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
asm volatile ("mcr p15, 0, %0, c7, c14, 2": :"r" (index));
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_clean_dcache(rt_uint32_t buffer, rt_uint32_t size)
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
unsigned int ptr;
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
ptr = buffer & ~0x1f;
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
while (ptr < buffer + size)
|
|
|
|
{
|
|
|
|
asm volatile ("mcr p15, 0, %0, c7, c10, 1": :"r" (ptr));
|
|
|
|
ptr += 32;
|
|
|
|
}
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_invalidate_dcache(rt_uint32_t buffer, rt_uint32_t size)
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
unsigned int ptr;
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
ptr = buffer & ~0x1f;
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
while (ptr < buffer + size)
|
|
|
|
{
|
|
|
|
asm volatile ("mcr p15, 0, %0, c7, c6, 1": :"r" (ptr));
|
|
|
|
ptr += 32;
|
|
|
|
}
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_invalidate_tlb()
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
asm volatile ("mcr p15, 0, %0, c8, c7, 0": :"r" (0));
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_invalidate_icache()
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
asm volatile ("mcr p15, 0, %0, c7, c5, 0": :"r" (0));
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* level1 page table */
|
|
|
|
static volatile unsigned int _page_table[4*1024] __attribute__((aligned(16*1024)));
|
|
|
|
void mmu_setmtt(rt_uint32_t vaddrStart, rt_uint32_t vaddrEnd, rt_uint32_t paddrStart, rt_uint32_t attr)
|
|
|
|
{
|
|
|
|
volatile rt_uint32_t *pTT;
|
2022-09-22 14:13:34 +08:00
|
|
|
int i,nSec;
|
2013-09-20 21:20:51 +08:00
|
|
|
pTT=(rt_uint32_t *)_page_table+(vaddrStart>>20);
|
|
|
|
nSec=(vaddrEnd>>20)-(vaddrStart>>20);
|
|
|
|
for(i=0;i<=nSec;i++)
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
*pTT = attr |(((paddrStart>>20)+i)<<20);
|
|
|
|
pTT++;
|
2013-09-20 21:20:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 11:37:14 +08:00
|
|
|
/* set page table */
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_weak void mmu_setmtts(void)
|
2018-09-15 11:37:14 +08:00
|
|
|
{
|
|
|
|
mmu_setmtt(0x00000000, 0xFFFFFFFF, 0x00000000, RW_NCNB); /* None cached for 4G memory */
|
|
|
|
mmu_setmtt(0x80200000, 0x80800000 - 1, 0x80200000, RW_CB); /* 126M cached DDR memory */
|
|
|
|
mmu_setmtt(0x80000000, 0x80200000 - 1, 0x80000000, RW_NCNB); /* 2M none-cached DDR memory */
|
2021-03-27 17:51:56 +08:00
|
|
|
mmu_setmtt(0x402F0000, 0x40300000 - 1, 0x402F0000, RW_CB); /* 63K OnChip memory */
|
2018-09-15 11:37:14 +08:00
|
|
|
}
|
|
|
|
|
2013-09-20 21:20:51 +08:00
|
|
|
void rt_hw_mmu_init(void)
|
|
|
|
{
|
2021-03-27 17:51:56 +08:00
|
|
|
/* disable I/D cache */
|
|
|
|
mmu_disable_dcache();
|
|
|
|
mmu_disable_icache();
|
|
|
|
mmu_disable();
|
|
|
|
mmu_invalidate_tlb();
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2018-09-15 11:37:14 +08:00
|
|
|
mmu_setmtts();
|
2013-09-20 21:20:51 +08:00
|
|
|
|
2021-03-27 17:51:56 +08:00
|
|
|
/* set MMU table address */
|
|
|
|
mmu_setttbase((rt_uint32_t)_page_table);
|
2013-09-20 21:20:51 +08:00
|
|
|
|
|
|
|
/* enables MMU */
|
|
|
|
mmu_enable();
|
|
|
|
|
|
|
|
/* enable Instruction Cache */
|
|
|
|
mmu_enable_icache();
|
|
|
|
|
|
|
|
/* enable Data Cache */
|
|
|
|
mmu_enable_dcache();
|
|
|
|
}
|
|
|
|
|