blob: 0497a29ba83f78e91385d03f8f3650a35bbf7764 [file] [log] [blame]
Kumar Gala44a23cf2008-01-16 22:33:22 -06001/*
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>
Kumar Galaecf5b982008-12-16 14:59:20 -060029#ifdef CONFIG_ADDR_MAP
30#include <addr_map.h>
31#endif
32
33DECLARE_GLOBAL_DATA_PTR;
Kumar Gala44a23cf2008-01-16 22:33:22 -060034
35void 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 asm volatile("isync;msync;tlbwe;isync");
Kumar Galaecf5b982008-12-16 14:59:20 -060055
56#ifdef CONFIG_ADDR_MAP
57 if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
58 addrmap_set_entry(epn, rpn, (1UL << ((tsize * 2) + 10)), esel);
59#endif
Kumar Gala44a23cf2008-01-16 22:33:22 -060060}
61
62void disable_tlb(u8 esel)
63{
64 u32 _mas0, _mas1, _mas2, _mas3, _mas7;
65
66 _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
67 _mas1 = 0;
68 _mas2 = 0;
69 _mas3 = 0;
70 _mas7 = 0;
71
72 mtspr(MAS0, _mas0);
73 mtspr(MAS1, _mas1);
74 mtspr(MAS2, _mas2);
75 mtspr(MAS3, _mas3);
76#ifdef CONFIG_ENABLE_36BIT_PHYS
77 mtspr(MAS7, _mas7);
78#endif
79 asm volatile("isync;msync;tlbwe;isync");
Kumar Galaecf5b982008-12-16 14:59:20 -060080
81#ifdef CONFIG_ADDR_MAP
82 if (gd->flags & GD_FLG_RELOC)
83 addrmap_set_entry(0, 0, 0, esel);
84#endif
Kumar Gala44a23cf2008-01-16 22:33:22 -060085}
86
87void invalidate_tlb(u8 tlb)
88{
89 if (tlb == 0)
90 mtspr(MMUCSR0, 0x4);
91 if (tlb == 1)
92 mtspr(MMUCSR0, 0x2);
93}
94
95void init_tlbs(void)
96{
Kumar Gala44a23cf2008-01-16 22:33:22 -060097 int i;
98
99 for (i = 0; i < num_tlb_entries; i++) {
100 set_tlb(tlb_table[i].tlb, tlb_table[i].epn, tlb_table[i].rpn,
101 tlb_table[i].perms, tlb_table[i].wimge,
102 tlb_table[i].ts, tlb_table[i].esel, tlb_table[i].tsize,
103 tlb_table[i].iprot);
104 }
Kumar Gala44a23cf2008-01-16 22:33:22 -0600105
106 return ;
107}
Kumar Gala6fb1b732008-06-09 11:07:46 -0500108
Kumar Gala176c84e2009-09-03 08:20:24 -0500109static void tlbsx (const volatile unsigned *addr)
110{
111 __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
112}
113
114/* return -1 if we didn't find anything */
115int find_tlb_idx(void *addr, u8 tlbsel)
116{
117 u32 _mas0, _mas1;
118
119 /* zero out Search PID, AS */
120 mtspr(MAS6, 0);
121
122 tlbsx(addr);
123
124 _mas0 = mfspr(MAS0);
125 _mas1 = mfspr(MAS1);
126
127 /* we found something, and its in the TLB we expect */
128 if ((MAS1_VALID & _mas1) &&
129 (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) {
130 return ((_mas0 & MAS0_ESEL_MSK) >> 16);
131 }
132
133 return -1;
134}
135
Kumar Galaecf5b982008-12-16 14:59:20 -0600136#ifdef CONFIG_ADDR_MAP
137void init_addr_map(void)
138{
139 int i;
Kumar Galae393e2e2009-08-14 16:43:22 -0500140 unsigned int max_cam = (mfspr(SPRN_TLB1CFG) >> 16) & 0xff;
Kumar Galaecf5b982008-12-16 14:59:20 -0600141
Kumar Galae393e2e2009-08-14 16:43:22 -0500142 /* walk all the entries */
143 for (i = 0; i < max_cam; i++) {
144 unsigned long epn;
Wolfgang Denk963f2f62009-08-22 23:27:26 +0200145 u32 tsize, _mas1;
Kumar Galae393e2e2009-08-14 16:43:22 -0500146 phys_addr_t rpn;
147
148 mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
149
150 asm volatile("tlbre;isync");
151 _mas1 = mfspr(MAS1);
152
153 /* if the entry isn't valid skip it */
154 if (!(_mas1 & MAS1_VALID))
Kumar Galaecf5b982008-12-16 14:59:20 -0600155 continue;
156
Kumar Galae393e2e2009-08-14 16:43:22 -0500157 tsize = (_mas1 >> 8) & 0xf;
158 epn = mfspr(MAS2) & MAS2_EPN;
159 rpn = mfspr(MAS3) & MAS3_RPN;
160#ifdef CONFIG_ENABLE_36BIT_PHYS
161 rpn |= ((phys_addr_t)mfspr(MAS7)) << 32;
162#endif
163
164 addrmap_set_entry(epn, rpn, (1UL << ((tsize * 2) + 10)), i);
Kumar Galaecf5b982008-12-16 14:59:20 -0600165 }
166
167 return ;
168}
169#endif
170
Haiying Wang95026432009-01-13 16:29:22 -0500171#ifndef CONFIG_SYS_DDR_TLB_START
172#define CONFIG_SYS_DDR_TLB_START 8
173#endif
174
Kumar Gala6fb1b732008-06-09 11:07:46 -0500175unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
176{
177 unsigned int tlb_size;
Kumar Galaf8523cb2009-02-06 09:56:35 -0600178 unsigned int ram_tlb_index = CONFIG_SYS_DDR_TLB_START;
179 unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
Fredrik Arnerup90d13b82009-06-02 16:27:10 -0500180 unsigned int max_cam = (mfspr(SPRN_TLB1CFG) >> 16) & 0xf;
Kumar Galaf8523cb2009-02-06 09:56:35 -0600181 u64 size, memsize = (u64)memsize_in_meg << 20;
Kumar Gala6fb1b732008-06-09 11:07:46 -0500182
Kumar Galaf8523cb2009-02-06 09:56:35 -0600183 size = min(memsize, CONFIG_MAX_MEM_MAPPED);
Kumar Gala6fb1b732008-06-09 11:07:46 -0500184
Kumar Galaf8523cb2009-02-06 09:56:35 -0600185 /* Convert (4^max) kB to (2^max) bytes */
186 max_cam = max_cam * 2 + 10;
Kumar Gala6fb1b732008-06-09 11:07:46 -0500187
Kumar Galaf8523cb2009-02-06 09:56:35 -0600188 for (; size && ram_tlb_index < 16; ram_tlb_index++) {
189 u32 camsize = __ilog2_u64(size) & ~1U;
190 u32 align = __ilog2(ram_tlb_address) & ~1U;
191
192 if (align == -2) align = max_cam;
193 if (camsize > align)
194 camsize = align;
195
196 if (camsize > max_cam)
197 camsize = max_cam;
198
199 tlb_size = (camsize - 10) / 2;
200
Kumar Gala6fb1b732008-06-09 11:07:46 -0500201 set_tlb(1, ram_tlb_address, ram_tlb_address,
202 MAS3_SX|MAS3_SW|MAS3_SR, 0,
203 0, ram_tlb_index, tlb_size, 1);
204
Kumar Galaf8523cb2009-02-06 09:56:35 -0600205 size -= 1ULL << camsize;
206 memsize -= 1ULL << camsize;
207 ram_tlb_address += 1UL << camsize;
Kumar Gala6fb1b732008-06-09 11:07:46 -0500208 }
209
Kumar Galaf8523cb2009-02-06 09:56:35 -0600210 if (memsize)
Kumar Galad4b130d2009-06-11 23:40:34 -0500211 print_size(memsize, " left unmapped\n");
Kumar Galaf8523cb2009-02-06 09:56:35 -0600212
Kumar Gala6fb1b732008-06-09 11:07:46 -0500213 /*
214 * Confirm that the requested amount of memory was mapped.
215 */
216 return memsize_in_meg;
217}