1
2
3
4
5
6
7
8#include <galileo/core.h>
9#include <galileo/memory.h>
10
11
12
13
14
15
16
17
18
19
20static unsigned long memoryGetBankRegOffset(MEMORY_BANK bank)
21{
22 switch (bank)
23 {
24 case BANK0:
25 return SCS_0_LOW_DECODE_ADDRESS;
26 case BANK1:
27 return SCS_1_LOW_DECODE_ADDRESS;
28 case BANK2:
29 return SCS_2_LOW_DECODE_ADDRESS;
30 case BANK3:
31 return SCS_3_LOW_DECODE_ADDRESS;
32 }
33 return SCS_0_LOW_DECODE_ADDRESS;
34}
35
36unsigned int memoryGetBankBaseAddress(MEMORY_BANK bank)
37{
38 unsigned int base;
39 unsigned int regOffset=memoryGetBankRegOffset(bank);
40
41 GT_REG_READ(regOffset,&base);
42 base = base << 20;
43 return base;
44}
45
46
47
48
49
50
51
52
53
54
55static unsigned int memoryGetDeviceRegOffset(DEVICE device)
56{
57 switch (device)
58 {
59 case DEVICE0:
60 return CS_0_LOW_DECODE_ADDRESS;
61 case DEVICE1:
62 return CS_1_LOW_DECODE_ADDRESS;
63 case DEVICE2:
64 return CS_2_LOW_DECODE_ADDRESS;
65 case DEVICE3:
66 return CS_3_LOW_DECODE_ADDRESS;
67 case BOOT_DEVICE:
68 return BOOTCS_LOW_DECODE_ADDRESS;
69 }
70 return CS_0_LOW_DECODE_ADDRESS;
71}
72
73unsigned int memoryGetDeviceBaseAddress(DEVICE device)
74{
75 unsigned int regBase;
76 unsigned int regEnd;
77 unsigned int regOffset=memoryGetDeviceRegOffset(device);
78
79 GT_REG_READ(regOffset, ®Base);
80 GT_REG_READ(regOffset+8, ®End);
81
82 if(regEnd<=regBase) return 0xffffffff;
83
84 regBase = regBase << 20;
85 return regBase;
86}
87
88
89
90
91
92
93
94
95
96unsigned int memoryGetBankSize(MEMORY_BANK bank)
97{
98 unsigned int size,base;
99 unsigned int highValue;
100 unsigned int highAddress=memoryGetBankRegOffset(bank)+8;
101
102 base = memoryGetBankBaseAddress(bank);
103 GT_REG_READ(highAddress,&highValue);
104 highValue = (highValue + 1) << 20;
105 if(base > highValue)
106 size=0;
107 else
108 size = highValue - base;
109 return size;
110}
111
112
113
114
115
116
117
118
119
120unsigned int memoryGetDeviceSize(DEVICE device)
121{
122 unsigned int size,base;
123 unsigned int highValue;
124 unsigned int highAddress=memoryGetDeviceRegOffset(device)+8;
125
126 base = memoryGetDeviceBaseAddress(device);
127 GT_REG_READ(highAddress,&highValue);
128 if (highValue == 0xfff)
129 {
130 size = (~base) + 1;
131 return size;
132 }
133 else
134 highValue = (highValue + 1) << 20;
135
136 if(base > highValue)
137 size=0;
138 else
139 size = highValue - base;
140 return size;
141}
142
143
144
145
146
147
148
149
150
151
152
153unsigned int memoryGetDeviceWidth(DEVICE device)
154{
155 unsigned int width;
156 unsigned int regValue;
157
158 GT_REG_READ(DEVICE_BANK0PARAMETERS + device*4,®Value);
159 width = (regValue & 0x00300000) >> 20;
160 switch (width)
161 {
162 case 0:
163 return 1;
164 case 1:
165 return 2;
166 case 2:
167 return 4;
168 case 3:
169 return 8;
170 default:
171 return 0;
172 }
173}
174
175bool memoryMapBank(MEMORY_BANK bank, unsigned int bankBase,unsigned int bankLength)
176{
177 unsigned int low=0xfff;
178 unsigned int high=0x0;
179 unsigned int regOffset=memoryGetBankRegOffset(bank);
180
181 if(bankLength!=0) {
182 low = (bankBase >> 20) & 0xffff;
183 high=((bankBase+bankLength)>>20)-1;
184 }
185
186#ifdef DEBUG
187 {
188 unsigned int oldLow, oldHigh;
189 GT_REG_READ(regOffset,&oldLow);
190 GT_REG_READ(regOffset+8,&oldHigh);
191
192 printf("b%d %x-%x->%x-%x\n", bank, oldLow, oldHigh, low, high);
193 }
194#endif
195
196 GT_REG_WRITE(regOffset,low);
197 GT_REG_WRITE(regOffset+8,high);
198
199 return true;
200}
201bool memoryMapDeviceSpace(DEVICE device, unsigned int deviceBase,unsigned int deviceLength)
202{
203
204 unsigned int low=0xfff;
205 unsigned int high=0x0;
206 unsigned int regOffset=memoryGetDeviceRegOffset(device);
207
208 if(deviceLength != 0) {
209 low=deviceBase>>20;
210 high=((deviceBase+deviceLength)>>20)-1;
211 } else {
212
213
214 }
215
216 GT_REG_WRITE(regOffset,low);
217 GT_REG_WRITE(regOffset+8,high);
218
219 return true;
220}
221
222
223
224
225
226
227
228
229
230bool memoryMapInternalRegistersSpace(unsigned int internalRegBase)
231{
232 unsigned int currentValue;
233 unsigned int internalValue = internalRegBase;
234
235 internalRegBase = (internalRegBase >> 20);
236 GT_REG_READ(INTERNAL_SPACE_DECODE,¤tValue);
237 internalRegBase = (currentValue & 0xffff0000) | internalRegBase;
238 GT_REG_WRITE(INTERNAL_SPACE_DECODE,internalRegBase);
239 INTERNAL_REG_BASE_ADDR = internalValue;
240 return true;
241}
242
243
244
245
246
247
248
249unsigned int memoryGetInternalRegistersSpace(void)
250{
251 return INTERNAL_REG_BASE_ADDR;
252}
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267bool memorySetProtectRegion(MEMORY_PROTECT_REGION region,
268 MEMORY_ACCESS memAccess,
269 MEMORY_ACCESS_WRITE memWrite,
270 MEMORY_CACHE_PROTECT cacheProtection,
271 unsigned int baseAddress,
272 unsigned int regionLength)
273{
274 unsigned int protectHigh = baseAddress + regionLength;
275
276 if(regionLength == 0)
277 {
278 GT_REG_WRITE(CPU_LOW_PROTECT_ADDRESS_0 + 0x10*region,0x0000ffff);
279 GT_REG_WRITE(CPU_HIGH_PROTECT_ADDRESS_0 + 0x10*region,0);
280 return true;
281 }
282 baseAddress = (baseAddress & 0xfff00000) >> 20;
283 baseAddress = baseAddress | memAccess << 16 | memWrite << 17
284 | cacheProtection << 18;
285 GT_REG_WRITE(CPU_LOW_PROTECT_ADDRESS_0 + 0x10*region,baseAddress);
286 protectHigh = (protectHigh & 0xfff00000) >> 20;
287 GT_REG_WRITE(CPU_HIGH_PROTECT_ADDRESS_0 + 0x10*region,protectHigh - 1);
288 return true;
289}
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306bool memorySetRegionSnoopMode(MEMORY_SNOOP_REGION region,
307 MEMORY_SNOOP_TYPE snoopType,
308 unsigned int baseAddress,
309 unsigned int regionLength)
310{
311 unsigned int snoopXbaseAddress;
312 unsigned int snoopXtopAddress;
313 unsigned int data;
314 unsigned int snoopHigh = baseAddress + regionLength;
315
316 if( (region > MEM_SNOOP_REGION3) || (snoopType > MEM_SNOOP_WB) )
317 return false;
318 snoopXbaseAddress = SNOOP_BASE_ADDRESS_0 + 0x10 * region;
319 snoopXtopAddress = SNOOP_TOP_ADDRESS_0 + 0x10 * region;
320 if(regionLength == 0)
321 {
322 GT_REG_WRITE(snoopXbaseAddress,0x0000ffff);
323 GT_REG_WRITE(snoopXtopAddress,0);
324 return true;
325 }
326 baseAddress = baseAddress & 0xffff0000;
327 data = (baseAddress >> 16) | snoopType << 16;
328 GT_REG_WRITE(snoopXbaseAddress,data);
329 snoopHigh = (snoopHigh & 0xfff00000) >> 20;
330 GT_REG_WRITE(snoopXtopAddress,snoopHigh - 1);
331 return true;
332}
333
334
335
336
337
338
339
340
341
342bool memoryRemapAddress(unsigned int remapReg, unsigned int remapValue)
343{
344 unsigned int valueForReg;
345 valueForReg = (remapValue & 0xfff00000) >> 20;
346 GT_REG_WRITE(remapReg, valueForReg);
347 return true;
348}
349
350
351
352
353
354
355
356
357
358
359
360bool memoryGetDeviceParam(DEVICE_PARAM *deviceParam, DEVICE deviceNum)
361{
362 unsigned int valueOfReg;
363 unsigned int calcData;
364
365 GT_REG_READ(DEVICE_BANK0PARAMETERS + 4 * deviceNum, &valueOfReg);
366 calcData = (0x7 & valueOfReg) + ((0x400000 & valueOfReg) >> 19);
367 deviceParam -> turnOff = calcData;
368
369 calcData = ((0x78 & valueOfReg) >> 3) + ((0x800000 & valueOfReg) >> 19);
370 deviceParam -> acc2First = calcData;
371
372 calcData = ((0x780 & valueOfReg) >> 7) + ((0x1000000 & valueOfReg) >> 20);
373 deviceParam -> acc2Next = calcData;
374
375 calcData = ((0x3800 & valueOfReg) >> 11) + ((0x2000000 & valueOfReg) >> 22);
376 deviceParam -> ale2Wr = calcData;
377
378 calcData = ((0x1c000 & valueOfReg) >> 14) + ((0x4000000 & valueOfReg) >> 23);
379 deviceParam -> wrLow = calcData;
380
381 calcData = ((0xe0000 & valueOfReg) >> 17) + ((0x8000000 & valueOfReg) >> 24);
382 deviceParam -> wrHigh = calcData;
383
384 calcData = ((0x300000 & valueOfReg) >> 20);
385 switch (calcData)
386 {
387 case 0:
388 deviceParam -> deviceWidth = 1;
389 break;
390 case 1:
391 deviceParam -> deviceWidth = 2;
392 break;
393 case 2:
394 deviceParam -> deviceWidth = 4;
395 break;
396 case 3:
397 deviceParam -> deviceWidth = 8;
398 break;
399 default:
400 deviceParam -> deviceWidth = 1;
401 break;
402 }
403 return true;
404}
405
406
407
408
409
410
411
412
413
414
415
416bool memorySetDeviceParam(DEVICE_PARAM *deviceParam, DEVICE deviceNum)
417{
418 unsigned int valueForReg;
419
420 if((deviceParam -> turnOff >= 0xf) || (deviceParam -> acc2First >= 0x1f) ||
421 (deviceParam -> acc2Next >= 0x1f) || (deviceParam -> ale2Wr >= 0xf) ||
422 (deviceParam -> wrLow >= 0xf) || (deviceParam -> wrHigh >= 0xf))
423 return false;
424 valueForReg = (((deviceParam -> turnOff) & 0x7) |
425 (((deviceParam -> turnOff) & 0x8) << 19) |
426 (((deviceParam -> acc2First) & 0xf) << 3) |
427 (((deviceParam -> acc2First) & 0x10) << 19) |
428 (((deviceParam -> acc2Next) & 0xf) << 7) |
429 (((deviceParam -> acc2Next) & 0x10) << 20) |
430 (((deviceParam -> ale2Wr) & 0x7) << 11) |
431 (((deviceParam -> ale2Wr) & 0xf) << 22) |
432 (((deviceParam -> wrLow) & 0x7) << 14) |
433 (((deviceParam -> wrLow) & 0xf) << 23) |
434 (((deviceParam -> wrHigh) & 0x7) << 17) |
435 (((deviceParam -> wrHigh) & 0xf) << 24));
436
437 switch(deviceParam->deviceWidth)
438 {
439 case 1:
440 valueForReg = valueForReg | _8BIT;
441 break;
442 case 2:
443 valueForReg = valueForReg | _16BIT;
444 break;
445 case 4:
446 valueForReg = valueForReg | _32BIT;
447 break;
448 case 8:
449 valueForReg = valueForReg | _64BIT;
450 break;
451 default:
452 valueForReg = valueForReg | _8BIT;
453 break;
454 }
455 GT_REG_WRITE(DEVICE_BANK0PARAMETERS + 4 * deviceNum, valueForReg);
456 return true;
457}
458