1 diff -urN romboot.old/init.cpp romboot/init.cpp
2 --- romboot.old/init.cpp 2007-03-24 13:34:19.000000000 +0100
3 +++ romboot/init.cpp 2007-03-24 12:23:19.000000000 +0100
5 AT91F_US_EnableRx((AT91PS_USART)AT91C_BASE_DBGU);
7 /* Enable PIO to access the LEDs */
8 - AT91C_BASE_PIOB->PIO_PER = AT91C_PIO_PB2;
9 - AT91C_BASE_PIOB->PIO_OER = AT91C_PIO_PB2;
10 - AT91C_BASE_PIOB->PIO_CODR = AT91C_PIO_PB2;
11 + AT91C_BASE_PIOC->PIO_PER = AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15;
12 + AT91C_BASE_PIOC->PIO_OER = AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15;
13 + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15;
14 + AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC8 | AT91C_PIO_PC14;
16 // AT91F_DBGU_Printk("\n\rAT91F_LowLevelInit(): Debug channel initialized\n\r");
18 diff -urN romboot.old/main.cpp romboot/main.cpp
19 --- romboot.old/main.cpp 2007-03-24 13:34:19.000000000 +0100
20 +++ romboot/main.cpp 2007-03-24 12:28:55.000000000 +0100
22 //*----------------------------------------------------------------------------
23 #include <AT91RM9200.h>
24 #include <lib_AT91RM9200.h>
25 +#include <AT91C_MCI_Device.h>
30 extern void AT91F_DBGU_Printk(char *);
31 extern "C" void AT91F_ST_ASM_Handler(void);
32 extern "C" void Jump(unsigned int addr);
33 -extern int mci_main(void);
34 +extern int AT91F_MCI_Init(void);
40 +#define TRX_MAGIC 0x30524448 /* "HDR0" */
41 +#define TRX_VERSION 1
47 + unsigned int flag_version;
48 + unsigned int offsets[3];
51 //const char *menu_separ = "*----------------------------------------*\n\r";
53 const char *menu_dataflash = {
58 - "5: RM BL in DF\n\r"
59 + "1: Download DF [addr]\n\r"
60 + "2: Read DF [addr]\n\r"
61 + "3: Copy SD-Card\n\r"
62 + "4: Start U-BOOT\n\r"
63 + "5: Clear bootloder\n\r"
66 //* Globales variables
68 //*-----------------------------------------------------------------------------
69 void AT91F_DisplayMenu(void)
71 - printf("\n\rFDL SD-Card LOADER %s %s %s\n\r", AT91C_VERSION, __DATE__, __TIME__);
72 -// printf(menu_separ);
73 - AT91F_DataflashPrintInfo();
74 -// printf(menu_separ);
75 printf(menu_dataflash);
76 -// printf(menu_separ);
79 +void AT91F_DisplayIntro(void)
81 + printf("\n\rFDL LOADER %s %s %s\n\r", AT91C_VERSION, __DATE__, __TIME__);
82 + AT91F_DataflashPrintInfo();
85 //*-----------------------------------------------------------------------------
86 //* Function Name : AsciiToHex()
87 //* Object : ascii to hexa conversion
97 pRegister = (int *)0xFFFFF800; // Enable port C peripheral reg
98 - *pRegister = 0x3c00;
99 + *pRegister = (AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15);
100 pRegister = (int *)0xFFFFF810; // Output Enable reg
101 - *pRegister = 0x3c00;
102 + *pRegister = (AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15);
103 pRegister = (int *)0xFFFFF830; // Set data
104 - *pRegister = 0x1400;
105 + *pRegister = AT91C_PIO_PC7 | AT91C_PIO_PC15;
106 pRegister = (int *)0xFFFFF834; // Clear bits
107 - *pRegister = 0x2800;
108 + *pRegister = AT91C_PIO_PC8 | AT91C_PIO_PC14;
113 void AT91F_StartUboot(unsigned int dummy, void *pvoid)
115 - //printf("Load U-BOOT from dataflash[%x] to SDRAM[%x]\n\r", AT91C_UBOOT_DATAFLASH_ADDR, AT91C_UBOOT_ADDR);
116 read_dataflash(AT91C_UBOOT_DATAFLASH_ADDR, AT91C_UBOOT_SIZE, (char *)(AT91C_UBOOT_ADDR));
117 //printf("Set PLLA to 180Mhz and Master clock to 60Mhz and start U-BOOT\n\r");
123 +#define AT91C_MCI_TIMEOUT 1000000
125 +extern AT91S_MciDevice MCI_Device;
127 +extern void AT91F_MCIDeviceWaitReady(unsigned int);
128 +extern int AT91F_MCI_ReadBlockSwab(AT91PS_MciDevice, int, unsigned int *, int);
131 +int Program_From_MCI(void)
134 + unsigned int Max_Read_DataBlock_Length;
136 + int buffer = AT91C_DOWNLOAD_BASE_ADDRESS;
137 + int bufpos = AT91C_DOWNLOAD_BASE_ADDRESS;
139 + struct trx_header *p;
142 + p = (struct trx_header *)bufpos;
144 + Max_Read_DataBlock_Length = MCI_Device.pMCI_DeviceFeatures->Max_Read_DataBlock_Length;
146 + AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
148 + AT91F_MCI_ReadBlockSwab(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
150 + if (p->magic != TRX_MAGIC)
153 + printf("Read SD-Card\n\r");
154 + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15 | AT91C_PIO_PC8 | AT91C_PIO_PC14;
155 + for (i=0; i<(p->len/512); i++) {
156 + AT91F_MCI_ReadBlockSwab(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
158 + bufpos += Max_Read_DataBlock_Length;
162 + i = dataflash_info[0].Device.pages_number;
165 + i = ((p->offsets[1] - p->offsets[0])/ 512) + 1 + (NbPage << 13) + (dataflash_info[0].Device.pages_size << 17);
166 + *(int *)(buffer + p->offsets[0] + AT91C_OFFSET_VECT6) = i;
168 + printf("Write romboot\n\r");
169 + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15 | AT91C_PIO_PC14;
170 + AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC8;
171 + write_dataflash(0xc0000000, buffer + p->offsets[0], p->offsets[1] - p->offsets[0]);
172 + printf("Write u-boot\n\r");
173 + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15;
174 + AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC8 | AT91C_PIO_PC14;
175 + write_dataflash(0xc0008000, buffer + p->offsets[1], p->offsets[2] - p->offsets[1]);
176 + printf("Write knl/root\n\r");
177 + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC8 | AT91C_PIO_PC15;
178 + AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC7 | AT91C_PIO_PC14;
179 + write_dataflash(0xc0042000, buffer + p->offsets[2], p->len - p->offsets[2]);
180 + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC8 | AT91C_PIO_PC14;
181 + AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC7 | AT91C_PIO_PC15;
185 //*----------------------------------------------------------------------------
186 //* Function Name : main
188 unsigned int crc1 = 0, crc2 = 0;
193 stdin = fopen(0, at91_dbgu_getc);
194 stdout = fopen(at91_dbgu_putc, 0);
197 // DataFlash on SPI Configuration
198 AT91F_DataflashInit ();
199 + AT91F_DisplayIntro();
200 + mci_present = AT91F_MCI_Init();
204 + Program_From_MCI();
205 + AT91F_StartUboot(0, (void *)0);
209 // start tempo to start Uboot in a delay of 1 sec if no key pressed
210 svcUbootTempo.Start(&svcUbootTempo, 1000, 0, AT91F_StartUboot, (void *)0);
214 svcUbootTempo.Stop(&svcUbootTempo);
218 while(command == 0) {
219 AddressToDownload = AT91C_DOWNLOAD_BASE_ADDRESS;
226 + Program_From_MCI();
233 write_dataflash(0xc0000000, 0x20000000, 0x4000);
234 - printf("BL CLR\r\n");
238 diff -urN romboot.old/main.h romboot/main.h
239 --- romboot.old/main.h 2007-03-24 13:34:19.000000000 +0100
240 +++ romboot/main.h 2007-03-23 19:06:52.000000000 +0100
243 #define AT91C_OFFSET_VECT6 0x14 //* Offset for ARM vector 6
245 -#define AT91C_VERSION "VER 1.02"
246 +#define AT91C_VERSION "VER 1.03"
247 // Global variables and functions definition
248 extern unsigned int GetTickCount(void);
250 diff -urN romboot.old/Makefile romboot/Makefile
251 --- romboot.old/Makefile 2007-03-24 13:34:19.000000000 +0100
252 +++ romboot/Makefile 2007-03-24 10:45:38.000000000 +0100
254 LINKFLAGS= -T elf32-littlearm.lds -Ttext 0
259 OBJFILES=cstartup_ram.o asm_isr.o asm_mci_isr.o jump.o at45.o com.o dataflash.o \
260 - mci_device.o mci_main.o init.o main.o stdio.o _udivsi3.o _umodsi3.o div0.o
261 + mci_device.o init.o main.o stdio.o _udivsi3.o _umodsi3.o div0.o
262 +OBJFILES2=cstartup_ram.o asm_isr.o asm_mci_isr.o jump.o at45.o com.o dataflash.o \
263 + mci_device.o init.o ptmain.o stdio.o _udivsi3.o _umodsi3.o div0.o
269 +all:$(TARGET) $(TARGET2)
271 $(TARGET): $(OBJFILES)
272 $(LD) $(OBJFILES) -o $(TARGET).out $(LINKFLAGS) -n
274 $(OBJDUMP) -h -s $(TARGET).out > $(TARGET).lss
275 $(NM) -n $(TARGET).out | grep -v '\( [aUw] \)\|\(__crc_\)\|\( \$[adt]\)' > $(TARGET).map
277 +$(TARGET2): $(OBJFILES2)
278 + $(LD) $(OBJFILES2) -o $(TARGET2).out $(LINKFLAGS) -n
279 + $(OBJCOPY) $(TARGET2).out -O binary $(TARGET2).bin
280 + $(OBJDUMP) -h -s $(TARGET2).out > $(TARGET2).lss
281 + $(NM) -n $(TARGET2).out | grep -v '\( [aUw] \)\|\(__crc_\)\|\( \$[adt]\)' > $(TARGET2).map
284 $(CC) -c -Iinclude -o $@ $<
287 _umodsi3.o: _umodsi3.S
291 + $(CC) -c $(COMPILEFLAGS) $(INCLUDES) -o main.o $<
294 + $(CC) -c $(COMPILEFLAGS) $(INCLUDES) -D PRODTEST -o ptmain.o $<
297 # $(CC) -c $(INCLUDES) -o $@ $<
299 diff -urN romboot.old/mci_device.cpp romboot/mci_device.cpp
300 --- romboot.old/mci_device.cpp 2007-03-24 13:34:19.000000000 +0100
301 +++ romboot/mci_device.cpp 2007-03-24 11:23:38.000000000 +0100
303 #include <AT91C_MCI_Device.h>
306 -#define ENABLE_WRITE 1
307 +#define AT91C_MCI_TIMEOUT 1000000 /* For AT91F_MCIDeviceWaitReady */
308 +#define BUFFER_SIZE_MCI_DEVICE 512
309 +#define MASTER_CLOCK 60000000
313 +//* External Functions
314 +extern "C" void AT91F_ASM_MCI_Handler(void);
315 +extern "C" void AT91F_MCI_Device_Handler(AT91PS_MciDevice,unsigned int);
316 +//* Global Variables
317 +AT91S_MciDeviceFeatures MCI_Device_Features;
318 +AT91S_MciDeviceDesc MCI_Device_Desc;
319 +AT91S_MciDevice MCI_Device;
324 //*----------------------------------------------------------------------------
325 //* \fn AT91F_MCI_SendCommand
326 //* \brief Generic function to send a command to the MMC or SDCard
327 //*----------------------------------------------------------------------------
328 -AT91S_MCIDeviceStatus AT91F_MCI_SendCommand (
329 +int AT91F_MCI_SendCommand (
330 AT91PS_MciDevice pMCI_Device,
334 //* \fn AT91F_MCI_SDCard_SendAppCommand
335 //* \brief Specific function to send a specific command to the SDCard
336 //*----------------------------------------------------------------------------
337 -AT91S_MCIDeviceStatus AT91F_MCI_SDCard_SendAppCommand (
338 +int AT91F_MCI_SDCard_SendAppCommand (
339 AT91PS_MciDevice pMCI_Device,
340 unsigned int Cmd_App,
343 //* \fn AT91F_MCI_GetStatus
344 //* \brief Addressed card sends its status register
345 //*----------------------------------------------------------------------------
346 -AT91S_MCIDeviceStatus AT91F_MCI_GetStatus(AT91PS_MciDevice pMCI_Device,unsigned int relative_card_address)
347 +int AT91F_MCI_GetStatus(AT91PS_MciDevice pMCI_Device,unsigned int relative_card_address)
349 if (AT91F_MCI_SendCommand(pMCI_Device,
350 AT91C_SEND_STATUS_CMD,
351 @@ -139,10 +153,25 @@
354 //*----------------------------------------------------------------------------
355 +//* \fn AT91F_MCI_Handler
356 +//* \brief MCI Handler
357 +//*----------------------------------------------------------------------------
358 +extern "C" void AT91F_MCI_Handler(void);
360 +void AT91F_MCI_Handler(void)
364 + status = ( AT91C_BASE_MCI->MCI_SR & AT91C_BASE_MCI->MCI_IMR );
366 + AT91F_MCI_Device_Handler(&MCI_Device,status);
369 +//*----------------------------------------------------------------------------
370 //* \fn AT91F_MCI_ReadBlock
371 //* \brief Read an ENTIRE block or PARTIAL block
372 //*----------------------------------------------------------------------------
373 -AT91S_MCIDeviceStatus AT91F_MCI_ReadBlock(
374 +int AT91F_MCI_ReadBlock(
375 AT91PS_MciDevice pMCI_Device,
377 unsigned int *dataBuffer,
379 //* \fn AT91F_MCI_WriteBlock
380 //* \brief Write an ENTIRE block but not always PARTIAL block !!!
381 //*----------------------------------------------------------------------------
382 -AT91S_MCIDeviceStatus AT91F_MCI_WriteBlock(
383 +int AT91F_MCI_WriteBlock(
384 AT91PS_MciDevice pMCI_Device,
386 unsigned int *dataBuffer,
388 //* \fn AT91F_MCI_MMC_SelectCard
389 //* \brief Toggles a card between the Stand_by and Transfer states or between Programming and Disconnect states
390 //*------------------------------------------------------------------------------------------------------------
391 -AT91S_MCIDeviceStatus AT91F_MCI_MMC_SelectCard(AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address)
392 +int AT91F_MCI_MMC_SelectCard(AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address)
397 //* \fn AT91F_MCI_GetCSD
398 //* \brief Asks to the specified card to send its CSD
399 //*----------------------------------------------------------------------------
400 -AT91S_MCIDeviceStatus AT91F_MCI_GetCSD (AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address , unsigned int * response)
401 +int AT91F_MCI_GetCSD (AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address , unsigned int * response)
404 if(AT91F_MCI_SendCommand(pMCI_Device,
406 //* \fn AT91F_MCI_SetBlocklength
407 //* \brief Select a block length for all following block commands (R/W)
408 //*----------------------------------------------------------------------------
409 -AT91S_MCIDeviceStatus AT91F_MCI_SetBlocklength(AT91PS_MciDevice pMCI_Device,unsigned int length)
410 +int AT91F_MCI_SetBlocklength(AT91PS_MciDevice pMCI_Device,unsigned int length)
412 return( AT91F_MCI_SendCommand(pMCI_Device, AT91C_SET_BLOCKLEN_CMD, length) );
415 //* \fn AT91F_MCI_MMC_GetAllOCR
416 //* \brief Asks to all cards to send their operations conditions
417 //*----------------------------------------------------------------------------
418 -AT91S_MCIDeviceStatus AT91F_MCI_MMC_GetAllOCR (AT91PS_MciDevice pMCI_Device)
419 +int AT91F_MCI_MMC_GetAllOCR (AT91PS_MciDevice pMCI_Device)
421 unsigned int response =0x0;
424 //* \fn AT91F_MCI_MMC_GetAllCID
425 //* \brief Asks to the MMC on the chosen slot to send its CID
426 //*----------------------------------------------------------------------------
427 -AT91S_MCIDeviceStatus AT91F_MCI_MMC_GetAllCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
428 +int AT91F_MCI_MMC_GetAllCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
430 int Nb_Cards_Found=-1;
433 //* \fn AT91F_MCI_MMC_Init
434 //* \brief Return the MMC initialisation status
435 //*----------------------------------------------------------------------------
436 -AT91S_MCIDeviceStatus AT91F_MCI_MMC_Init (AT91PS_MciDevice pMCI_Device)
437 +int AT91F_MCI_MMC_Init (AT91PS_MciDevice pMCI_Device)
439 unsigned int tab_response[4];
440 unsigned int mult,blocknr;
442 //* \fn AT91F_MCI_SDCard_GetOCR
443 //* \brief Asks to all cards to send their operations conditions
444 //*----------------------------------------------------------------------------
445 -AT91S_MCIDeviceStatus AT91F_MCI_SDCard_GetOCR (AT91PS_MciDevice pMCI_Device)
446 +int AT91F_MCI_SDCard_GetOCR (AT91PS_MciDevice pMCI_Device)
448 unsigned int response =0x0;
451 //* \fn AT91F_MCI_SDCard_GetCID
452 //* \brief Asks to the SDCard on the chosen slot to send its CID
453 //*----------------------------------------------------------------------------
454 -AT91S_MCIDeviceStatus AT91F_MCI_SDCard_GetCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
455 +int AT91F_MCI_SDCard_GetCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
457 if(AT91F_MCI_SendCommand(pMCI_Device,
458 AT91C_ALL_SEND_CID_CMD,
460 //* \fn AT91F_MCI_SDCard_SetBusWidth
461 //* \brief Set bus width for SDCard
462 //*----------------------------------------------------------------------------
463 -AT91S_MCIDeviceStatus AT91F_MCI_SDCard_SetBusWidth(AT91PS_MciDevice pMCI_Device)
464 +int AT91F_MCI_SDCard_SetBusWidth(AT91PS_MciDevice pMCI_Device)
466 volatile int ret_value;
469 //* \fn AT91F_MCI_SDCard_Init
470 //* \brief Return the SDCard initialisation status
471 //*----------------------------------------------------------------------------
472 -AT91S_MCIDeviceStatus AT91F_MCI_SDCard_Init (AT91PS_MciDevice pMCI_Device)
473 +int AT91F_MCI_SDCard_Init (AT91PS_MciDevice pMCI_Device)
475 unsigned int tab_response[4];
476 unsigned int mult,blocknr;
479 pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity = pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length * blocknr;
480 //// End of Compute Memory Capacity
481 - printf("BLK 0x%x", pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length);
482 + printf("SD-Card: %d Bytes\n\r", pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity);
484 if( AT91F_MCI_SDCard_SetBusWidth(pMCI_Device) == AT91C_CMD_SEND_OK )
486 @@ -579,3 +608,141 @@
488 return AT91C_INIT_ERROR;
491 +//*----------------------------------------------------------------------------
492 +//* \fn AT91F_CfgDevice
493 +//* \brief This function is used to initialise MMC or SDCard Features
494 +//*----------------------------------------------------------------------------
495 +void AT91F_CfgDevice(void)
497 + // Init Device Structure
499 + MCI_Device_Features.Relative_Card_Address = 0;
500 + MCI_Device_Features.Card_Inserted = AT91C_CARD_REMOVED;
501 + MCI_Device_Features.Max_Read_DataBlock_Length = 0;
502 + MCI_Device_Features.Max_Write_DataBlock_Length = 0;
503 + MCI_Device_Features.Read_Partial = 0;
504 + MCI_Device_Features.Write_Partial = 0;
505 + MCI_Device_Features.Erase_Block_Enable = 0;
506 + MCI_Device_Features.Sector_Size = 0;
507 + MCI_Device_Features.Memory_Capacity = 0;
509 + MCI_Device_Desc.state = AT91C_MCI_IDLE;
510 + MCI_Device_Desc.SDCard_bus_width = AT91C_MCI_SCDBUS;
512 + // Init AT91S_DataFlash Global Structure, by default AT45DB choosen !!!
513 + MCI_Device.pMCI_DeviceDesc = &MCI_Device_Desc;
514 + MCI_Device.pMCI_DeviceFeatures = &MCI_Device_Features;
518 +//*----------------------------------------------------------------------------
519 +//* \fn AT91F_MCI_Init
520 +//* \brief Initialsise Card
521 +//*----------------------------------------------------------------------------
522 +int AT91F_MCI_Init(void)
525 +///////////////////////////////////////////////////////////////////////////////////////////
526 +// MCI Init : common to MMC and SDCard
527 +///////////////////////////////////////////////////////////////////////////////////////////
529 + // Set up PIO SDC_TYPE to switch on MMC/SDCard and not DataFlash Card
530 + AT91F_PIO_CfgOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
531 + AT91F_PIO_SetOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
533 + // Init MCI for MMC and SDCard interface
534 + AT91F_MCI_CfgPIO();
535 + AT91F_MCI_CfgPMC();
536 + AT91F_PDC_Open(AT91C_BASE_PDC_MCI);
538 + // Disable all the interrupts
539 + AT91C_BASE_MCI->MCI_IDR = 0xFFFFFFFF;
541 + // Init MCI Device Structures
544 + // Configure MCI interrupt
545 + AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
547 + AT91C_AIC_PRIOR_HIGHEST,
548 + AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,
549 + AT91F_ASM_MCI_Handler);
551 + // Enable MCI interrupt
552 + AT91F_AIC_EnableIt(AT91C_BASE_AIC,AT91C_ID_MCI);
555 + AT91F_US_EnableRx((AT91PS_USART) AT91C_BASE_DBGU);
557 + AT91F_MCI_Configure(AT91C_BASE_MCI,
558 + AT91C_MCI_DTOR_1MEGA_CYCLES,
559 + AT91C_MCI_MR_PDCMODE, // 15MHz for MCK = 60MHz (CLKDIV = 1)
560 + AT91C_MCI_SDCARD_4BITS_SLOTA);
562 + if(AT91F_MCI_SDCard_Init(&MCI_Device) != AT91C_INIT_OK)
569 +//*----------------------------------------------------------------------------
570 +//* \fn AT91F_MCIDeviceWaitReady
571 +//* \brief Wait for MCI Device ready
572 +//*----------------------------------------------------------------------------
573 +void AT91F_MCIDeviceWaitReady(unsigned int timeout)
575 + volatile int status;
579 + status = AT91C_BASE_MCI->MCI_SR;
582 + while( !(status & AT91C_MCI_NOTBUSY) && (timeout>0) );
585 +unsigned int swab32(unsigned int data)
587 + unsigned int res = 0;
589 + res = (data & 0x000000ff) << 24 |
590 + (data & 0x0000ff00) << 8 |
591 + (data & 0x00ff0000) >> 8 |
592 + (data & 0xff000000) >> 24;
597 +//*--------------------------------------------------------------------
598 +//* \fn AT91F_MCI_ReadBlockSwab
599 +//* \brief Read Block and swap byte order
600 +//*--------------------------------------------------------------------
601 +int AT91F_MCI_ReadBlockSwab(
602 + AT91PS_MciDevice pMCI_Device,
604 + unsigned int *databuffer,
608 + unsigned char *buf = (unsigned char *)databuffer;
611 + for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++)
613 + AT91F_MCI_ReadBlock(&MCI_Device,src,databuffer,sizeToRead);
615 + //* Wait end of Read
616 + AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
620 + unsigned int *uiBuffer = databuffer;
622 + for(index = 0; index < 512/4; index++)
623 + uiBuffer[index] = swab32(uiBuffer[index]);
628 diff -urN romboot.old/mci_main.cpp romboot/mci_main.cpp
629 --- romboot.old/mci_main.cpp 2007-03-24 13:34:19.000000000 +0100
630 +++ romboot/mci_main.cpp 1970-01-01 01:00:00.000000000 +0100
632 -//*----------------------------------------------------------------------------
633 -//* ATMEL Microcontroller Software Support - ROUSSET -
634 -//*----------------------------------------------------------------------------
635 -//* The software is delivered "AS IS" without warranty or condition of any
636 -//* kind, either express, implied or statutory. This includes without
637 -//* limitation any warranty or condition with respect to merchantability or
638 -//* fitness for any particular purpose, or against the infringements of
639 -//* intellectual property rights of others.
640 -//*----------------------------------------------------------------------------
641 -//* File Name : main.c
642 -//* Object : main application written in C
643 -//* Creation : FB 21/11/2002
645 -//*----------------------------------------------------------------------------
647 -#include "dataflash.h"
648 -#include <AT91C_MCI_Device.h>
650 -#define AT91C_MCI_TIMEOUT 1000000 /* For AT91F_MCIDeviceWaitReady */
651 -#define BUFFER_SIZE_MCI_DEVICE 512
652 -#define MASTER_CLOCK 60000000
656 -//* External Functions
657 -extern "C" void AT91F_ASM_MCI_Handler(void);
658 -extern "C" void AT91F_MCI_Device_Handler(AT91PS_MciDevice,unsigned int);
659 -extern AT91S_MCIDeviceStatus AT91F_MCI_SDCard_Init (AT91PS_MciDevice);
660 -extern AT91S_MCIDeviceStatus AT91F_MCI_SetBlocklength(AT91PS_MciDevice,unsigned int);
661 -extern AT91S_MCIDeviceStatus AT91F_MCI_ReadBlock(AT91PS_MciDevice,int,unsigned int *,int);
662 -extern AT91S_MCIDeviceStatus AT91F_MCI_WriteBlock(AT91PS_MciDevice,int,unsigned int *,int);
663 -//* Global Variables
664 -AT91S_MciDeviceFeatures MCI_Device_Features;
665 -AT91S_MciDeviceDesc MCI_Device_Desc;
666 -AT91S_MciDevice MCI_Device;
668 -unsigned int dlBuffer = 0x20000000;
671 -char TestString[] = "\r\nHello Hamish\r\n";
674 -//*----------------------------------------------------------------------------
675 -//* \fn AT91F_MCIDeviceWaitReady
676 -//* \brief Wait for MCI Device ready
677 -//*----------------------------------------------------------------------------
678 -void AT91F_MCIDeviceWaitReady(unsigned int timeout)
680 - volatile int status;
684 - status = AT91C_BASE_MCI->MCI_SR;
687 - while( !(status & AT91C_MCI_NOTBUSY) && (timeout>0) );
690 -unsigned int swab32(unsigned int data)
692 - unsigned int res = 0;
694 - res = (data & 0x000000ff) << 24 |
695 - (data & 0x0000ff00) << 8 |
696 - (data & 0x00ff0000) >> 8 |
697 - (data & 0xff000000) >> 24;
702 -AT91S_MCIDeviceStatus readblock(
703 - AT91PS_MciDevice pMCI_Device,
705 - unsigned int *databuffer,
709 - unsigned char *buf = (unsigned char *)databuffer;
712 - for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++)
714 - AT91F_MCI_ReadBlock(&MCI_Device,src,databuffer,sizeToRead);
716 - //* Wait end of Read
717 - AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
721 - unsigned int *uiBuffer = databuffer;
723 - for(index = 0; index < 512/4; index++)
724 - uiBuffer[index] = swab32(uiBuffer[index]);
730 -void printdata(unsigned int bufpos)
733 - int linebytes = 16;
738 - uip = (unsigned int *)bufpos;
742 - for(i=0; i<linebytes; i+=size) {
743 - printf(" %08x", *uip++);
747 - nbytes -= linebytes;
748 - } while (nbytes > 0);
751 -//extern char message[40];
753 -int notnull(int bufpos, unsigned int len)
756 - unsigned char * bp = (unsigned char *)bufpos;
758 - for (i=0; i<len; i++)
764 -//*----------------------------------------------------------------------------
766 -//* \brief Test Functions
767 -//*----------------------------------------------------------------------------
768 -int AT91F_Test(void)
771 - unsigned int Max_Read_DataBlock_Length;
773 - int bufpos = dlBuffer;
778 - Max_Read_DataBlock_Length = MCI_Device.pMCI_DeviceFeatures->Max_Read_DataBlock_Length;
780 - //* ReadBlock & WriteBlock Test -> Entire Block
782 - //* Wait MCI Device Ready
783 - AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
787 - for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++) Buffer[i] = 0x00;
788 - AT91F_MCI_ReadBlock(&MCI_Device,(1*Max_Read_DataBlock_Length),(unsigned int*) Buffer,Max_Read_DataBlock_Length);
790 - //* Wait end of Read
791 - AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
794 -// sprintf(Buffer,"\n\rThis sentence is written in your device... Congratulations\n\r");
795 - for(i=0; i<16; i++)
796 - Buffer[i] = TestString[i];
797 - AT91F_MCI_WriteBlock(&MCI_Device,(1*Max_Read_DataBlock_Length),(unsigned int*) Buffer,Max_Read_DataBlock_Length);
799 - //* Wait end of Write
800 - AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
803 - for(i=0; i<64; i++) {
804 - readblock(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
805 - if (notnull(bufpos, Max_Read_DataBlock_Length))
811 - i = dataflash_info[0].Device.pages_number;
814 - i = lastvalid + (NbPage << 13) + (dataflash_info[0].Device.pages_size << 17);
815 - *(int *)(dlBuffer + 0x14) = i;
817 - for(i=0; i<4688; i++) {
818 - readblock(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
822 - write_dataflash(0xc0000000, dlBuffer, 512 * block);
824 - printf("DONE %d\n\r", lastvalid);
831 -//*----------------------------------------------------------------------------
832 -//* \fn AT91F_CfgDevice
833 -//* \brief This function is used to initialise MMC or SDCard Features
834 -//*----------------------------------------------------------------------------
835 -void AT91F_CfgDevice(void)
837 - // Init Device Structure
839 - MCI_Device_Features.Relative_Card_Address = 0;
840 - MCI_Device_Features.Card_Inserted = AT91C_CARD_REMOVED;
841 - MCI_Device_Features.Max_Read_DataBlock_Length = 0;
842 - MCI_Device_Features.Max_Write_DataBlock_Length = 0;
843 - MCI_Device_Features.Read_Partial = 0;
844 - MCI_Device_Features.Write_Partial = 0;
845 - MCI_Device_Features.Erase_Block_Enable = 0;
846 - MCI_Device_Features.Sector_Size = 0;
847 - MCI_Device_Features.Memory_Capacity = 0;
849 - MCI_Device_Desc.state = AT91C_MCI_IDLE;
850 - MCI_Device_Desc.SDCard_bus_width = AT91C_MCI_SCDBUS;
852 - // Init AT91S_DataFlash Global Structure, by default AT45DB choosen !!!
853 - MCI_Device.pMCI_DeviceDesc = &MCI_Device_Desc;
854 - MCI_Device.pMCI_DeviceFeatures = &MCI_Device_Features;
858 -//*----------------------------------------------------------------------------
859 -//* \fn AT91F_Test_SDCard
860 -//* \brief Configure MCI for SDCard and complete SDCard init, then jump to Test Functions
861 -//*----------------------------------------------------------------------------
862 -int AT91F_Test_SDCard(void)
864 - //////////////////////////////////////////////////////////
865 - //* For SDCard Init
866 - //////////////////////////////////////////////////////////
868 - AT91F_MCI_Configure(AT91C_BASE_MCI,
869 - AT91C_MCI_DTOR_1MEGA_CYCLES,
870 - AT91C_MCI_MR_PDCMODE, // 15MHz for MCK = 60MHz (CLKDIV = 1)
871 - AT91C_MCI_SDCARD_4BITS_SLOTA);
873 - if(AT91F_MCI_SDCard_Init(&MCI_Device) != AT91C_INIT_OK)
876 - printf("\n\rINI OK: TST\n\r");
878 - // Enter Main Tests
879 - return(AT91F_Test());
882 -//*----------------------------------------------------------------------------
883 -//* \fn AT91F_MCI_Handler
884 -//* \brief MCI Handler
885 -//*----------------------------------------------------------------------------
886 -extern "C" void AT91F_MCI_Handler(void);
888 -void AT91F_MCI_Handler(void)
892 - status = ( AT91C_BASE_MCI->MCI_SR & AT91C_BASE_MCI->MCI_IMR );
894 - AT91F_MCI_Device_Handler(&MCI_Device,status);
897 -//*----------------------------------------------------------------------------
899 -//* \brief main function
900 -//*----------------------------------------------------------------------------
903 -// printf("MCI Test\n\r");
905 -///////////////////////////////////////////////////////////////////////////////////////////
906 -// MCI Init : common to MMC and SDCard
907 -///////////////////////////////////////////////////////////////////////////////////////////
909 -// printf("\n\rInit MCI Interface\n\r");
911 - // Set up PIO SDC_TYPE to switch on MMC/SDCard and not DataFlash Card
912 - AT91F_PIO_CfgOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
913 - AT91F_PIO_SetOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
915 - // Init MCI for MMC and SDCard interface
916 - AT91F_MCI_CfgPIO();
917 - AT91F_MCI_CfgPMC();
918 - AT91F_PDC_Open(AT91C_BASE_PDC_MCI);
920 - // Disable all the interrupts
921 - AT91C_BASE_MCI->MCI_IDR = 0xFFFFFFFF;
923 - // Init MCI Device Structures
926 - // Configure MCI interrupt
927 - AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
929 - AT91C_AIC_PRIOR_HIGHEST,
930 - AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,
931 - AT91F_ASM_MCI_Handler);
933 - // Enable MCI interrupt
934 - AT91F_AIC_EnableIt(AT91C_BASE_AIC,AT91C_ID_MCI);
936 -///////////////////////////////////////////////////////////////////////////////////////////
938 -///////////////////////////////////////////////////////////////////////////////////////////
941 - AT91F_US_EnableRx((AT91PS_USART) AT91C_BASE_DBGU);
943 - if(AT91F_Test_SDCard() == TRUE)
944 - printf("\n\rTST OK\n\r");
946 - printf("\n\rTST Fail\n\r");