]> git.sur5r.net Git - u-boot/blob - cpu/mpc85xx/tlb.c
03c2449b5c3c4c999074684db5b2bee43c0a179f
[u-boot] / cpu / mpc85xx / tlb.c
1 /*
2  * Copyright 2008 Freescale Semiconductor, Inc.
3  *
4  * (C) Copyright 2000
5  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25
26 #include <common.h>
27 #include <asm/processor.h>
28 #include <asm/mmu.h>
29 #ifdef CONFIG_ADDR_MAP
30 #include <addr_map.h>
31 #endif
32
33 DECLARE_GLOBAL_DATA_PTR;
34
35 void set_tlb(u8 tlb, u32 epn, u64 rpn,
36              u8 perms, u8 wimge,
37              u8 ts, u8 esel, u8 tsize, u8 iprot)
38 {
39         u32 _mas0, _mas1, _mas2, _mas3, _mas7;
40
41         _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0);
42         _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize);
43         _mas2 = FSL_BOOKE_MAS2(epn, wimge);
44         _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms);
45         _mas7 = rpn >> 32;
46
47         mtspr(MAS0, _mas0);
48         mtspr(MAS1, _mas1);
49         mtspr(MAS2, _mas2);
50         mtspr(MAS3, _mas3);
51 #ifdef CONFIG_ENABLE_36BIT_PHYS
52         mtspr(MAS7, _mas7);
53 #endif
54 #ifdef CONFIG_SYS_BOOK3E_HV
55         mtspr(MAS8, 0);
56 #endif
57         asm volatile("isync;msync;tlbwe;isync");
58
59 #ifdef CONFIG_ADDR_MAP
60         if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
61                 addrmap_set_entry(epn, rpn, (1UL << ((tsize * 2) + 10)), esel);
62 #endif
63 }
64
65 void disable_tlb(u8 esel)
66 {
67         u32 _mas0, _mas1, _mas2, _mas3, _mas7;
68
69         _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
70         _mas1 = 0;
71         _mas2 = 0;
72         _mas3 = 0;
73         _mas7 = 0;
74
75         mtspr(MAS0, _mas0);
76         mtspr(MAS1, _mas1);
77         mtspr(MAS2, _mas2);
78         mtspr(MAS3, _mas3);
79 #ifdef CONFIG_ENABLE_36BIT_PHYS
80         mtspr(MAS7, _mas7);
81 #endif
82         asm volatile("isync;msync;tlbwe;isync");
83
84 #ifdef CONFIG_ADDR_MAP
85         if (gd->flags & GD_FLG_RELOC)
86                 addrmap_set_entry(0, 0, 0, esel);
87 #endif
88 }
89
90 void invalidate_tlb(u8 tlb)
91 {
92         if (tlb == 0)
93                 mtspr(MMUCSR0, 0x4);
94         if (tlb == 1)
95                 mtspr(MMUCSR0, 0x2);
96 }
97
98 void init_tlbs(void)
99 {
100         int i;
101
102         for (i = 0; i < num_tlb_entries; i++) {
103                 set_tlb(tlb_table[i].tlb, tlb_table[i].epn, tlb_table[i].rpn,
104                         tlb_table[i].perms, tlb_table[i].wimge,
105                         tlb_table[i].ts, tlb_table[i].esel, tlb_table[i].tsize,
106                         tlb_table[i].iprot);
107         }
108
109         return ;
110 }
111
112 static void tlbsx (const volatile unsigned *addr)
113 {
114         __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
115 }
116
117 /* return -1 if we didn't find anything */
118 int find_tlb_idx(void *addr, u8 tlbsel)
119 {
120         u32 _mas0, _mas1;
121
122         /* zero out Search PID, AS */
123         mtspr(MAS6, 0);
124
125         tlbsx(addr);
126
127         _mas0 = mfspr(MAS0);
128         _mas1 = mfspr(MAS1);
129
130         /* we found something, and its in the TLB we expect */
131         if ((MAS1_VALID & _mas1) &&
132                 (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) {
133                 return ((_mas0 & MAS0_ESEL_MSK) >> 16);
134         }
135
136         return -1;
137 }
138
139 #ifdef CONFIG_ADDR_MAP
140 void init_addr_map(void)
141 {
142         int i;
143         unsigned int max_cam = (mfspr(SPRN_TLB1CFG) >> 16) & 0xff;
144
145         /* walk all the entries */
146         for (i = 0; i < max_cam; i++) {
147                 unsigned long epn;
148                 u32 tsize, _mas1;
149                 phys_addr_t rpn;
150
151                 mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
152
153                 asm volatile("tlbre;isync");
154                 _mas1 = mfspr(MAS1);
155
156                 /* if the entry isn't valid skip it */
157                 if (!(_mas1 & MAS1_VALID))
158                         continue;
159
160                 tsize = (_mas1 >> 8) & 0xf;
161                 epn = mfspr(MAS2) & MAS2_EPN;
162                 rpn = mfspr(MAS3) & MAS3_RPN;
163 #ifdef CONFIG_ENABLE_36BIT_PHYS
164                 rpn |= ((phys_addr_t)mfspr(MAS7)) << 32;
165 #endif
166
167                 addrmap_set_entry(epn, rpn, (1UL << ((tsize * 2) + 10)), i);
168         }
169
170         return ;
171 }
172 #endif
173
174 #ifndef CONFIG_SYS_DDR_TLB_START
175 #define CONFIG_SYS_DDR_TLB_START 8
176 #endif
177
178 unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
179 {
180         unsigned int tlb_size;
181         unsigned int ram_tlb_index = CONFIG_SYS_DDR_TLB_START;
182         unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
183         unsigned int max_cam = (mfspr(SPRN_TLB1CFG) >> 16) & 0xf;
184         u64 size, memsize = (u64)memsize_in_meg << 20;
185
186         size = min(memsize, CONFIG_MAX_MEM_MAPPED);
187
188         /* Convert (4^max) kB to (2^max) bytes */
189         max_cam = max_cam * 2 + 10;
190
191         for (; size && ram_tlb_index < 16; ram_tlb_index++) {
192                 u32 camsize = __ilog2_u64(size) & ~1U;
193                 u32 align = __ilog2(ram_tlb_address) & ~1U;
194
195                 if (align == -2) align = max_cam;
196                 if (camsize > align)
197                         camsize = align;
198
199                 if (camsize > max_cam)
200                         camsize = max_cam;
201
202                 tlb_size = (camsize - 10) / 2;
203
204                 set_tlb(1, ram_tlb_address, ram_tlb_address,
205                         MAS3_SX|MAS3_SW|MAS3_SR, 0,
206                         0, ram_tlb_index, tlb_size, 1);
207
208                 size -= 1ULL << camsize;
209                 memsize -= 1ULL << camsize;
210                 ram_tlb_address += 1UL << camsize;
211         }
212
213         if (memsize)
214                 print_size(memsize, " left unmapped\n");
215
216         /*
217          * Confirm that the requested amount of memory was mapped.
218          */
219         return memsize_in_meg;
220 }