2007-02-20 09:43:34 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2007
|
|
|
|
* Stefan Roese, DENX Software Engineering, sr@denx.de.
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_440)
|
|
|
|
|
2010-09-09 17:18:00 +00:00
|
|
|
#include <asm/ppc440.h>
|
2007-10-31 16:59:22 +00:00
|
|
|
#include <asm/cache.h>
|
2007-02-20 09:43:34 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/mmu.h>
|
|
|
|
|
|
|
|
typedef struct region {
|
2008-02-19 21:01:57 +00:00
|
|
|
u64 base;
|
|
|
|
u32 size;
|
|
|
|
u32 tlb_word2_i_value;
|
2007-02-20 09:43:34 +00:00
|
|
|
} region_t;
|
|
|
|
|
2007-07-16 06:53:51 +00:00
|
|
|
void remove_tlb(u32 vaddr, u32 size)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u32 tlb_word0_value;
|
|
|
|
u32 tlb_vaddr;
|
|
|
|
u32 tlb_size = 0;
|
|
|
|
|
|
|
|
for (i=0; i<PPC4XX_TLB_SIZE; i++) {
|
|
|
|
tlb_word0_value = mftlb1(i);
|
|
|
|
tlb_vaddr = TLB_WORD0_EPN_DECODE(tlb_word0_value);
|
|
|
|
if (((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_ENABLE) &&
|
|
|
|
(tlb_vaddr >= vaddr)) {
|
|
|
|
/*
|
|
|
|
* TLB is enabled and start address is lower or equal
|
|
|
|
* than the area we are looking for. Now we only have
|
|
|
|
* to check the size/end address for a match.
|
|
|
|
*/
|
|
|
|
switch (tlb_word0_value & TLB_WORD0_SIZE_MASK) {
|
|
|
|
case TLB_WORD0_SIZE_1KB:
|
|
|
|
tlb_size = 1 << 10;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_4KB:
|
|
|
|
tlb_size = 4 << 10;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_16KB:
|
|
|
|
tlb_size = 16 << 10;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_64KB:
|
|
|
|
tlb_size = 64 << 10;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_256KB:
|
|
|
|
tlb_size = 256 << 10;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_1MB:
|
|
|
|
tlb_size = 1 << 20;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_16MB:
|
|
|
|
tlb_size = 16 << 20;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_256MB:
|
|
|
|
tlb_size = 256 << 20;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now check the end-address if it's in the range
|
|
|
|
*/
|
|
|
|
if ((tlb_vaddr + tlb_size - 1) <= (vaddr + size - 1))
|
|
|
|
/*
|
|
|
|
* Found a TLB in the range.
|
|
|
|
* Disable it by writing 0 to tlb0 word.
|
|
|
|
*/
|
|
|
|
mttlb1(i, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute an ISYNC instruction so that the new TLB entry takes effect */
|
|
|
|
asm("isync");
|
|
|
|
}
|
|
|
|
|
2007-10-31 16:59:22 +00:00
|
|
|
/*
|
|
|
|
* Change the I attribute (cache inhibited) of a TLB or multiple TLB's.
|
|
|
|
* This function is used to either turn cache on or off in a specific
|
|
|
|
* memory area.
|
|
|
|
*/
|
|
|
|
void change_tlb(u32 vaddr, u32 size, u32 tlb_word2_i_value)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u32 tlb_word0_value;
|
|
|
|
u32 tlb_word2_value;
|
|
|
|
u32 tlb_vaddr;
|
|
|
|
u32 tlb_size = 0;
|
|
|
|
|
|
|
|
for (i=0; i<PPC4XX_TLB_SIZE; i++) {
|
|
|
|
tlb_word0_value = mftlb1(i);
|
|
|
|
tlb_vaddr = TLB_WORD0_EPN_DECODE(tlb_word0_value);
|
|
|
|
if (((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_ENABLE) &&
|
|
|
|
(tlb_vaddr >= vaddr)) {
|
|
|
|
/*
|
|
|
|
* TLB is enabled and start address is lower or equal
|
|
|
|
* than the area we are looking for. Now we only have
|
|
|
|
* to check the size/end address for a match.
|
|
|
|
*/
|
|
|
|
switch (tlb_word0_value & TLB_WORD0_SIZE_MASK) {
|
|
|
|
case TLB_WORD0_SIZE_1KB:
|
|
|
|
tlb_size = 1 << 10;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_4KB:
|
|
|
|
tlb_size = 4 << 10;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_16KB:
|
|
|
|
tlb_size = 16 << 10;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_64KB:
|
|
|
|
tlb_size = 64 << 10;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_256KB:
|
|
|
|
tlb_size = 256 << 10;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_1MB:
|
|
|
|
tlb_size = 1 << 20;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_16MB:
|
|
|
|
tlb_size = 16 << 20;
|
|
|
|
break;
|
|
|
|
case TLB_WORD0_SIZE_256MB:
|
|
|
|
tlb_size = 256 << 20;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now check the end-address if it's in the range
|
|
|
|
*/
|
2008-04-17 16:15:27 +00:00
|
|
|
if (((tlb_vaddr + tlb_size - 1) <= (vaddr + size - 1)) ||
|
|
|
|
((tlb_vaddr < (vaddr + size - 1)) &&
|
|
|
|
((tlb_vaddr + tlb_size - 1) > (vaddr + size - 1)))) {
|
2007-10-31 16:59:22 +00:00
|
|
|
/*
|
|
|
|
* Found a TLB in the range.
|
|
|
|
* Change cache attribute in tlb2 word.
|
|
|
|
*/
|
|
|
|
tlb_word2_value =
|
|
|
|
TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
|
|
|
|
TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
|
|
|
|
TLB_WORD2_W_DISABLE | tlb_word2_i_value |
|
|
|
|
TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
|
|
|
|
TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
|
|
|
|
TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
|
|
|
|
TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
|
|
|
|
TLB_WORD2_SR_ENABLE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now either flush or invalidate the dcache
|
|
|
|
*/
|
|
|
|
if (tlb_word2_i_value)
|
|
|
|
flush_dcache();
|
|
|
|
else
|
|
|
|
invalidate_dcache();
|
|
|
|
|
|
|
|
mttlb3(i, tlb_word2_value);
|
|
|
|
asm("iccci 0,0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute an ISYNC instruction so that the new TLB entry takes effect */
|
|
|
|
asm("isync");
|
|
|
|
}
|
|
|
|
|
2008-02-19 21:01:57 +00:00
|
|
|
static int add_tlb_entry(u64 phys_addr,
|
|
|
|
u32 virt_addr,
|
|
|
|
u32 tlb_word0_size_value,
|
|
|
|
u32 tlb_word2_i_value)
|
2007-02-20 09:43:34 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned long tlb_word0_value;
|
|
|
|
unsigned long tlb_word1_value;
|
|
|
|
unsigned long tlb_word2_value;
|
|
|
|
|
|
|
|
/* First, find the index of a TLB entry not being used */
|
|
|
|
for (i=0; i<PPC4XX_TLB_SIZE; i++) {
|
|
|
|
tlb_word0_value = mftlb1(i);
|
|
|
|
if ((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_DISABLE)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= PPC4XX_TLB_SIZE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Second, create the TLB entry */
|
2007-06-14 09:14:32 +00:00
|
|
|
tlb_word0_value = TLB_WORD0_EPN_ENCODE(virt_addr) | TLB_WORD0_V_ENABLE |
|
2007-02-20 09:43:34 +00:00
|
|
|
TLB_WORD0_TS_0 | tlb_word0_size_value;
|
2008-02-19 21:01:57 +00:00
|
|
|
tlb_word1_value = TLB_WORD1_RPN_ENCODE((u32)phys_addr) |
|
|
|
|
TLB_WORD1_ERPN_ENCODE(phys_addr >> 32);
|
2007-02-20 09:43:34 +00:00
|
|
|
tlb_word2_value = TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
|
|
|
|
TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
|
|
|
|
TLB_WORD2_W_DISABLE | tlb_word2_i_value |
|
|
|
|
TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
|
|
|
|
TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
|
|
|
|
TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
|
|
|
|
TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
|
|
|
|
TLB_WORD2_SR_ENABLE;
|
|
|
|
|
|
|
|
/* Wait for all memory accesses to complete */
|
|
|
|
sync();
|
|
|
|
|
|
|
|
/* Third, add the TLB entries */
|
|
|
|
mttlb1(i, tlb_word0_value);
|
|
|
|
mttlb2(i, tlb_word1_value);
|
|
|
|
mttlb3(i, tlb_word2_value);
|
|
|
|
|
|
|
|
/* Execute an ISYNC instruction so that the new TLB entry takes effect */
|
|
|
|
asm("isync");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-19 21:01:57 +00:00
|
|
|
static void program_tlb_addr(u64 phys_addr,
|
|
|
|
u32 virt_addr,
|
|
|
|
u32 mem_size,
|
|
|
|
u32 tlb_word2_i_value)
|
2007-02-20 09:43:34 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int tlb_i;
|
|
|
|
|
|
|
|
tlb_i = tlb_word2_i_value;
|
|
|
|
while (mem_size != 0) {
|
|
|
|
rc = 0;
|
|
|
|
/* Add the TLB entries in to map the region. */
|
2007-06-14 09:14:32 +00:00
|
|
|
if (((phys_addr & TLB_256MB_ALIGN_MASK) == phys_addr) &&
|
2007-02-20 09:43:34 +00:00
|
|
|
(mem_size >= TLB_256MB_SIZE)) {
|
|
|
|
/* Add a 256MB TLB entry */
|
2007-06-14 09:14:32 +00:00
|
|
|
if ((rc = add_tlb_entry(phys_addr, virt_addr,
|
|
|
|
TLB_WORD0_SIZE_256MB, tlb_i)) == 0) {
|
2007-02-20 09:43:34 +00:00
|
|
|
mem_size -= TLB_256MB_SIZE;
|
2007-06-14 09:14:32 +00:00
|
|
|
phys_addr += TLB_256MB_SIZE;
|
2007-06-22 14:58:40 +00:00
|
|
|
virt_addr += TLB_256MB_SIZE;
|
2007-02-20 09:43:34 +00:00
|
|
|
}
|
2007-06-14 09:14:32 +00:00
|
|
|
} else if (((phys_addr & TLB_16MB_ALIGN_MASK) == phys_addr) &&
|
2007-02-20 09:43:34 +00:00
|
|
|
(mem_size >= TLB_16MB_SIZE)) {
|
|
|
|
/* Add a 16MB TLB entry */
|
2007-06-14 09:14:32 +00:00
|
|
|
if ((rc = add_tlb_entry(phys_addr, virt_addr,
|
|
|
|
TLB_WORD0_SIZE_16MB, tlb_i)) == 0) {
|
2007-02-20 09:43:34 +00:00
|
|
|
mem_size -= TLB_16MB_SIZE;
|
2007-06-14 09:14:32 +00:00
|
|
|
phys_addr += TLB_16MB_SIZE;
|
2007-06-22 14:58:40 +00:00
|
|
|
virt_addr += TLB_16MB_SIZE;
|
2007-02-20 09:43:34 +00:00
|
|
|
}
|
2007-06-14 09:14:32 +00:00
|
|
|
} else if (((phys_addr & TLB_1MB_ALIGN_MASK) == phys_addr) &&
|
2007-02-20 09:43:34 +00:00
|
|
|
(mem_size >= TLB_1MB_SIZE)) {
|
|
|
|
/* Add a 1MB TLB entry */
|
2007-06-14 09:14:32 +00:00
|
|
|
if ((rc = add_tlb_entry(phys_addr, virt_addr,
|
|
|
|
TLB_WORD0_SIZE_1MB, tlb_i)) == 0) {
|
2007-02-20 09:43:34 +00:00
|
|
|
mem_size -= TLB_1MB_SIZE;
|
2007-06-14 09:14:32 +00:00
|
|
|
phys_addr += TLB_1MB_SIZE;
|
2007-06-22 14:58:40 +00:00
|
|
|
virt_addr += TLB_1MB_SIZE;
|
2007-02-20 09:43:34 +00:00
|
|
|
}
|
2007-06-14 09:14:32 +00:00
|
|
|
} else if (((phys_addr & TLB_256KB_ALIGN_MASK) == phys_addr) &&
|
2007-02-20 09:43:34 +00:00
|
|
|
(mem_size >= TLB_256KB_SIZE)) {
|
|
|
|
/* Add a 256KB TLB entry */
|
2007-06-14 09:14:32 +00:00
|
|
|
if ((rc = add_tlb_entry(phys_addr, virt_addr,
|
|
|
|
TLB_WORD0_SIZE_256KB, tlb_i)) == 0) {
|
2007-02-20 09:43:34 +00:00
|
|
|
mem_size -= TLB_256KB_SIZE;
|
2007-06-14 09:14:32 +00:00
|
|
|
phys_addr += TLB_256KB_SIZE;
|
2007-06-22 14:58:40 +00:00
|
|
|
virt_addr += TLB_256KB_SIZE;
|
2007-02-20 09:43:34 +00:00
|
|
|
}
|
2007-06-14 09:14:32 +00:00
|
|
|
} else if (((phys_addr & TLB_64KB_ALIGN_MASK) == phys_addr) &&
|
2007-02-20 09:43:34 +00:00
|
|
|
(mem_size >= TLB_64KB_SIZE)) {
|
|
|
|
/* Add a 64KB TLB entry */
|
2007-06-14 09:14:32 +00:00
|
|
|
if ((rc = add_tlb_entry(phys_addr, virt_addr,
|
|
|
|
TLB_WORD0_SIZE_64KB, tlb_i)) == 0) {
|
2007-02-20 09:43:34 +00:00
|
|
|
mem_size -= TLB_64KB_SIZE;
|
2007-06-14 09:14:32 +00:00
|
|
|
phys_addr += TLB_64KB_SIZE;
|
2007-06-22 14:58:40 +00:00
|
|
|
virt_addr += TLB_64KB_SIZE;
|
2007-02-20 09:43:34 +00:00
|
|
|
}
|
2007-06-14 09:14:32 +00:00
|
|
|
} else if (((phys_addr & TLB_16KB_ALIGN_MASK) == phys_addr) &&
|
2007-02-20 09:43:34 +00:00
|
|
|
(mem_size >= TLB_16KB_SIZE)) {
|
|
|
|
/* Add a 16KB TLB entry */
|
2007-06-14 09:14:32 +00:00
|
|
|
if ((rc = add_tlb_entry(phys_addr, virt_addr,
|
|
|
|
TLB_WORD0_SIZE_16KB, tlb_i)) == 0) {
|
2007-02-20 09:43:34 +00:00
|
|
|
mem_size -= TLB_16KB_SIZE;
|
2007-06-14 09:14:32 +00:00
|
|
|
phys_addr += TLB_16KB_SIZE;
|
2007-06-22 14:58:40 +00:00
|
|
|
virt_addr += TLB_16KB_SIZE;
|
2007-02-20 09:43:34 +00:00
|
|
|
}
|
2007-06-14 09:14:32 +00:00
|
|
|
} else if (((phys_addr & TLB_4KB_ALIGN_MASK) == phys_addr) &&
|
2007-02-20 09:43:34 +00:00
|
|
|
(mem_size >= TLB_4KB_SIZE)) {
|
|
|
|
/* Add a 4KB TLB entry */
|
2007-06-14 09:14:32 +00:00
|
|
|
if ((rc = add_tlb_entry(phys_addr, virt_addr,
|
|
|
|
TLB_WORD0_SIZE_4KB, tlb_i)) == 0) {
|
2007-02-20 09:43:34 +00:00
|
|
|
mem_size -= TLB_4KB_SIZE;
|
2007-06-14 09:14:32 +00:00
|
|
|
phys_addr += TLB_4KB_SIZE;
|
2007-06-22 14:58:40 +00:00
|
|
|
virt_addr += TLB_4KB_SIZE;
|
2007-02-20 09:43:34 +00:00
|
|
|
}
|
2007-06-14 09:14:32 +00:00
|
|
|
} else if (((phys_addr & TLB_1KB_ALIGN_MASK) == phys_addr) &&
|
2007-02-20 09:43:34 +00:00
|
|
|
(mem_size >= TLB_1KB_SIZE)) {
|
|
|
|
/* Add a 1KB TLB entry */
|
2007-06-14 09:14:32 +00:00
|
|
|
if ((rc = add_tlb_entry(phys_addr, virt_addr,
|
|
|
|
TLB_WORD0_SIZE_1KB, tlb_i)) == 0) {
|
2007-02-20 09:43:34 +00:00
|
|
|
mem_size -= TLB_1KB_SIZE;
|
2007-06-14 09:14:32 +00:00
|
|
|
phys_addr += TLB_1KB_SIZE;
|
2007-06-22 14:58:40 +00:00
|
|
|
virt_addr += TLB_1KB_SIZE;
|
2007-02-20 09:43:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-07-10 07:58:06 +00:00
|
|
|
printf("ERROR: no TLB size exists for the base address 0x%llx.\n",
|
2007-06-14 09:14:32 +00:00
|
|
|
phys_addr);
|
2007-02-20 09:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rc != 0)
|
2008-07-10 07:58:06 +00:00
|
|
|
printf("ERROR: no TLB entries available for the base addr 0x%llx.\n",
|
2007-06-14 09:14:32 +00:00
|
|
|
phys_addr);
|
2007-02-20 09:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Program one (or multiple) TLB entries for one memory region
|
|
|
|
*
|
|
|
|
* Common usage for boards with SDRAM DIMM modules to dynamically
|
|
|
|
* configure the TLB's for the SDRAM
|
|
|
|
*/
|
2008-02-19 21:01:57 +00:00
|
|
|
void program_tlb(u64 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value)
|
2007-02-20 09:43:34 +00:00
|
|
|
{
|
|
|
|
region_t region_array;
|
|
|
|
|
2007-06-14 09:14:32 +00:00
|
|
|
region_array.base = phys_addr;
|
2007-02-20 09:43:34 +00:00
|
|
|
region_array.size = size;
|
2007-03-01 20:11:36 +00:00
|
|
|
region_array.tlb_word2_i_value = tlb_word2_i_value; /* en-/disable cache */
|
2007-02-20 09:43:34 +00:00
|
|
|
|
|
|
|
/* Call the routine to add in the tlb entries for the memory regions */
|
2007-06-14 09:14:32 +00:00
|
|
|
program_tlb_addr(region_array.base, virt_addr, region_array.size,
|
2007-02-20 09:43:34 +00:00
|
|
|
region_array.tlb_word2_i_value);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_440 */
|