003-SD-reflash.patch 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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
  4. @@ -207,9 +207,10 @@
  5. AT91F_US_EnableRx((AT91PS_USART)AT91C_BASE_DBGU);
  6. /* Enable PIO to access the LEDs */
  7. - AT91C_BASE_PIOB->PIO_PER = AT91C_PIO_PB2;
  8. - AT91C_BASE_PIOB->PIO_OER = AT91C_PIO_PB2;
  9. - AT91C_BASE_PIOB->PIO_CODR = AT91C_PIO_PB2;
  10. + AT91C_BASE_PIOC->PIO_PER = AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15;
  11. + AT91C_BASE_PIOC->PIO_OER = AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15;
  12. + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15;
  13. + AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC8 | AT91C_PIO_PC14;
  14. // AT91F_DBGU_Printk("\n\rAT91F_LowLevelInit(): Debug channel initialized\n\r");
  15. }
  16. diff -urN romboot.old/main.cpp romboot/main.cpp
  17. --- romboot.old/main.cpp 2007-03-24 13:34:19.000000000 +0100
  18. +++ romboot/main.cpp 2007-03-24 12:28:55.000000000 +0100
  19. @@ -13,6 +13,7 @@
  20. //*----------------------------------------------------------------------------
  21. #include <AT91RM9200.h>
  22. #include <lib_AT91RM9200.h>
  23. +#include <AT91C_MCI_Device.h>
  24. #include "com.h"
  25. #include "main.h"
  26. @@ -39,16 +40,31 @@
  27. extern void AT91F_DBGU_Printk(char *);
  28. extern "C" void AT91F_ST_ASM_Handler(void);
  29. extern "C" void Jump(unsigned int addr);
  30. -extern int mci_main(void);
  31. +extern int AT91F_MCI_Init(void);
  32. +#define TRUE 1
  33. +#define FALSE 0
  34. +
  35. +/* from trxhdr.h */
  36. +
  37. +#define TRX_MAGIC 0x30524448 /* "HDR0" */
  38. +#define TRX_VERSION 1
  39. +
  40. +struct trx_header {
  41. + unsigned int magic;
  42. + unsigned int len;
  43. + unsigned int crc32;
  44. + unsigned int flag_version;
  45. + unsigned int offsets[3];
  46. +};
  47. //const char *menu_separ = "*----------------------------------------*\n\r";
  48. const char *menu_dataflash = {
  49. - "1: DL DF [ad]\n\r"
  50. - "2: RD DF [ad]\n\r"
  51. - "3: CP SD\n\r"
  52. - "4: U-BOOT\n\r"
  53. - "5: RM BL in DF\n\r"
  54. + "1: Download DF [addr]\n\r"
  55. + "2: Read DF [addr]\n\r"
  56. + "3: Copy SD-Card\n\r"
  57. + "4: Start U-BOOT\n\r"
  58. + "5: Clear bootloder\n\r"
  59. };
  60. //* Globales variables
  61. @@ -155,14 +171,15 @@
  62. //*-----------------------------------------------------------------------------
  63. void AT91F_DisplayMenu(void)
  64. {
  65. - printf("\n\rFDL SD-Card LOADER %s %s %s\n\r", AT91C_VERSION, __DATE__, __TIME__);
  66. -// printf(menu_separ);
  67. - AT91F_DataflashPrintInfo();
  68. -// printf(menu_separ);
  69. printf(menu_dataflash);
  70. -// printf(menu_separ);
  71. }
  72. +void AT91F_DisplayIntro(void)
  73. +{
  74. + printf("\n\rFDL LOADER %s %s %s\n\r", AT91C_VERSION, __DATE__, __TIME__);
  75. + AT91F_DataflashPrintInfo();
  76. +}
  77. +
  78. //*-----------------------------------------------------------------------------
  79. //* Function Name : AsciiToHex()
  80. //* Object : ascii to hexa conversion
  81. @@ -311,23 +328,24 @@
  82. AT91F_SetPLL();
  83. }
  84. -/*void LedCode(void)
  85. +/*
  86. +void LedCode(void)
  87. {
  88. int *pRegister;
  89. pRegister = (int *)0xFFFFF800; // Enable port C peripheral reg
  90. - *pRegister = 0x3c00;
  91. + *pRegister = (AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15);
  92. pRegister = (int *)0xFFFFF810; // Output Enable reg
  93. - *pRegister = 0x3c00;
  94. + *pRegister = (AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15);
  95. pRegister = (int *)0xFFFFF830; // Set data
  96. - *pRegister = 0x1400;
  97. + *pRegister = AT91C_PIO_PC7 | AT91C_PIO_PC15;
  98. pRegister = (int *)0xFFFFF834; // Clear bits
  99. - *pRegister = 0x2800;
  100. + *pRegister = AT91C_PIO_PC8 | AT91C_PIO_PC14;
  101. }
  102. */
  103. +
  104. void AT91F_StartUboot(unsigned int dummy, void *pvoid)
  105. {
  106. - //printf("Load U-BOOT from dataflash[%x] to SDRAM[%x]\n\r", AT91C_UBOOT_DATAFLASH_ADDR, AT91C_UBOOT_ADDR);
  107. read_dataflash(AT91C_UBOOT_DATAFLASH_ADDR, AT91C_UBOOT_SIZE, (char *)(AT91C_UBOOT_ADDR));
  108. //printf("Set PLLA to 180Mhz and Master clock to 60Mhz and start U-BOOT\n\r");
  109. //* Reset registers
  110. @@ -337,6 +355,67 @@
  111. while(1);
  112. }
  113. +#define AT91C_MCI_TIMEOUT 1000000
  114. +
  115. +extern AT91S_MciDevice MCI_Device;
  116. +
  117. +extern void AT91F_MCIDeviceWaitReady(unsigned int);
  118. +extern int AT91F_MCI_ReadBlockSwab(AT91PS_MciDevice, int, unsigned int *, int);
  119. +
  120. +
  121. +int Program_From_MCI(void)
  122. +{
  123. + int i;
  124. + unsigned int Max_Read_DataBlock_Length;
  125. + int block = 0;
  126. + int buffer = AT91C_DOWNLOAD_BASE_ADDRESS;
  127. + int bufpos = AT91C_DOWNLOAD_BASE_ADDRESS;
  128. + int NbPage = 0;
  129. + struct trx_header *p;
  130. + unsigned int data;
  131. +
  132. + p = (struct trx_header *)bufpos;
  133. +
  134. + Max_Read_DataBlock_Length = MCI_Device.pMCI_DeviceFeatures->Max_Read_DataBlock_Length;
  135. +
  136. + AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
  137. +
  138. + AT91F_MCI_ReadBlockSwab(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
  139. +
  140. + if (p->magic != TRX_MAGIC)
  141. + return FALSE;
  142. +
  143. + printf("Read SD-Card\n\r");
  144. + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15 | AT91C_PIO_PC8 | AT91C_PIO_PC14;
  145. + for (i=0; i<(p->len/512); i++) {
  146. + AT91F_MCI_ReadBlockSwab(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
  147. + block++;
  148. + bufpos += Max_Read_DataBlock_Length;
  149. + }
  150. +
  151. + NbPage = 0;
  152. + i = dataflash_info[0].Device.pages_number;
  153. + while(i >>= 1)
  154. + NbPage++;
  155. + i = ((p->offsets[1] - p->offsets[0])/ 512) + 1 + (NbPage << 13) + (dataflash_info[0].Device.pages_size << 17);
  156. + *(int *)(buffer + p->offsets[0] + AT91C_OFFSET_VECT6) = i;
  157. +
  158. + printf("Write romboot\n\r");
  159. + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15 | AT91C_PIO_PC14;
  160. + AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC8;
  161. + write_dataflash(0xc0000000, buffer + p->offsets[0], p->offsets[1] - p->offsets[0]);
  162. + printf("Write u-boot\n\r");
  163. + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15;
  164. + AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC8 | AT91C_PIO_PC14;
  165. + write_dataflash(0xc0008000, buffer + p->offsets[1], p->offsets[2] - p->offsets[1]);
  166. + printf("Write knl/root\n\r");
  167. + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC8 | AT91C_PIO_PC15;
  168. + AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC7 | AT91C_PIO_PC14;
  169. + write_dataflash(0xc0042000, buffer + p->offsets[2], p->len - p->offsets[2]);
  170. + AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC8 | AT91C_PIO_PC14;
  171. + AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC7 | AT91C_PIO_PC15;
  172. + return TRUE;
  173. + }
  174. //*----------------------------------------------------------------------------
  175. //* Function Name : main
  176. @@ -357,6 +436,7 @@
  177. unsigned int crc1 = 0, crc2 = 0;
  178. volatile int device;
  179. int NbPage;
  180. + int mci_present;
  181. stdin = fopen(0, at91_dbgu_getc);
  182. stdout = fopen(at91_dbgu_putc, 0);
  183. @@ -387,6 +467,15 @@
  184. // DataFlash on SPI Configuration
  185. AT91F_DataflashInit ();
  186. + AT91F_DisplayIntro();
  187. + mci_present = AT91F_MCI_Init();
  188. +
  189. +#ifdef PRODTEST
  190. + if (mci_present) {
  191. + Program_From_MCI();
  192. + AT91F_StartUboot(0, (void *)0);
  193. + }
  194. +#endif
  195. // start tempo to start Uboot in a delay of 1 sec if no key pressed
  196. svcUbootTempo.Start(&svcUbootTempo, 1000, 0, AT91F_StartUboot, (void *)0);
  197. @@ -396,7 +485,7 @@
  198. // stop tempo
  199. svcUbootTempo.Stop(&svcUbootTempo);
  200. -
  201. +
  202. while(1) {
  203. while(command == 0) {
  204. AddressToDownload = AT91C_DOWNLOAD_BASE_ADDRESS;
  205. @@ -444,7 +533,8 @@
  206. #endif
  207. case '3':
  208. - mci_main();
  209. + if (mci_present)
  210. + Program_From_MCI();
  211. command=0;
  212. break;
  213. @@ -461,7 +551,6 @@
  214. *i = 0;
  215. }
  216. write_dataflash(0xc0000000, 0x20000000, 0x4000);
  217. - printf("BL CLR\r\n");
  218. command = 0;
  219. break;
  220. diff -urN romboot.old/main.h romboot/main.h
  221. --- romboot.old/main.h 2007-03-24 13:34:19.000000000 +0100
  222. +++ romboot/main.h 2007-03-23 19:06:52.000000000 +0100
  223. @@ -27,7 +27,7 @@
  224. #define AT91C_OFFSET_VECT6 0x14 //* Offset for ARM vector 6
  225. -#define AT91C_VERSION "VER 1.02"
  226. +#define AT91C_VERSION "VER 1.03"
  227. // Global variables and functions definition
  228. extern unsigned int GetTickCount(void);
  229. #endif
  230. diff -urN romboot.old/Makefile romboot/Makefile
  231. --- romboot.old/Makefile 2007-03-24 13:34:19.000000000 +0100
  232. +++ romboot/Makefile 2007-03-24 10:45:38.000000000 +0100
  233. @@ -1,12 +1,17 @@
  234. LINKFLAGS= -T elf32-littlearm.lds -Ttext 0
  235. COMPILEFLAGS= -Os
  236. TARGET=romboot
  237. +TARGET2=rbptest
  238. +
  239. OBJFILES=cstartup_ram.o asm_isr.o asm_mci_isr.o jump.o at45.o com.o dataflash.o \
  240. - mci_device.o mci_main.o init.o main.o stdio.o _udivsi3.o _umodsi3.o div0.o
  241. + mci_device.o init.o main.o stdio.o _udivsi3.o _umodsi3.o div0.o
  242. +OBJFILES2=cstartup_ram.o asm_isr.o asm_mci_isr.o jump.o at45.o com.o dataflash.o \
  243. + mci_device.o init.o ptmain.o stdio.o _udivsi3.o _umodsi3.o div0.o
  244. +
  245. LIBRARIES=
  246. INCLUDES= -Iinclude
  247. -all:$(TARGET)
  248. +all:$(TARGET) $(TARGET2)
  249. $(TARGET): $(OBJFILES)
  250. $(LD) $(OBJFILES) -o $(TARGET).out $(LINKFLAGS) -n
  251. @@ -14,6 +19,12 @@
  252. $(OBJDUMP) -h -s $(TARGET).out > $(TARGET).lss
  253. $(NM) -n $(TARGET).out | grep -v '\( [aUw] \)\|\(__crc_\)\|\( \$[adt]\)' > $(TARGET).map
  254. +$(TARGET2): $(OBJFILES2)
  255. + $(LD) $(OBJFILES2) -o $(TARGET2).out $(LINKFLAGS) -n
  256. + $(OBJCOPY) $(TARGET2).out -O binary $(TARGET2).bin
  257. + $(OBJDUMP) -h -s $(TARGET2).out > $(TARGET2).lss
  258. + $(NM) -n $(TARGET2).out | grep -v '\( [aUw] \)\|\(__crc_\)\|\( \$[adt]\)' > $(TARGET2).map
  259. +
  260. asm_isr.o: asm_isr.S
  261. $(CC) -c -Iinclude -o $@ $<
  262. @@ -32,6 +43,12 @@
  263. _umodsi3.o: _umodsi3.S
  264. $(CC) -c $<
  265. +main.o: main.cpp
  266. + $(CC) -c $(COMPILEFLAGS) $(INCLUDES) -o main.o $<
  267. +
  268. +ptmain.o: main.cpp
  269. + $(CC) -c $(COMPILEFLAGS) $(INCLUDES) -D PRODTEST -o ptmain.o $<
  270. +
  271. #%.o: %.S
  272. # $(CC) -c $(INCLUDES) -o $@ $<
  273. diff -urN romboot.old/mci_device.cpp romboot/mci_device.cpp
  274. --- romboot.old/mci_device.cpp 2007-03-24 13:34:19.000000000 +0100
  275. +++ romboot/mci_device.cpp 2007-03-24 11:23:38.000000000 +0100
  276. @@ -16,14 +16,28 @@
  277. #include <AT91C_MCI_Device.h>
  278. #include "com.h"
  279. -#define ENABLE_WRITE 1
  280. +#define AT91C_MCI_TIMEOUT 1000000 /* For AT91F_MCIDeviceWaitReady */
  281. +#define BUFFER_SIZE_MCI_DEVICE 512
  282. +#define MASTER_CLOCK 60000000
  283. +#define FALSE 0
  284. +#define TRUE 1
  285. +
  286. +//* External Functions
  287. +extern "C" void AT91F_ASM_MCI_Handler(void);
  288. +extern "C" void AT91F_MCI_Device_Handler(AT91PS_MciDevice,unsigned int);
  289. +//* Global Variables
  290. +AT91S_MciDeviceFeatures MCI_Device_Features;
  291. +AT91S_MciDeviceDesc MCI_Device_Desc;
  292. +AT91S_MciDevice MCI_Device;
  293. +
  294. +#undef ENABLE_WRITE
  295. #undef MMC
  296. //*----------------------------------------------------------------------------
  297. //* \fn AT91F_MCI_SendCommand
  298. //* \brief Generic function to send a command to the MMC or SDCard
  299. //*----------------------------------------------------------------------------
  300. -AT91S_MCIDeviceStatus AT91F_MCI_SendCommand (
  301. +int AT91F_MCI_SendCommand (
  302. AT91PS_MciDevice pMCI_Device,
  303. unsigned int Cmd,
  304. unsigned int Arg)
  305. @@ -63,7 +77,7 @@
  306. //* \fn AT91F_MCI_SDCard_SendAppCommand
  307. //* \brief Specific function to send a specific command to the SDCard
  308. //*----------------------------------------------------------------------------
  309. -AT91S_MCIDeviceStatus AT91F_MCI_SDCard_SendAppCommand (
  310. +int AT91F_MCI_SDCard_SendAppCommand (
  311. AT91PS_MciDevice pMCI_Device,
  312. unsigned int Cmd_App,
  313. unsigned int Arg )
  314. @@ -98,7 +112,7 @@
  315. //* \fn AT91F_MCI_GetStatus
  316. //* \brief Addressed card sends its status register
  317. //*----------------------------------------------------------------------------
  318. -AT91S_MCIDeviceStatus AT91F_MCI_GetStatus(AT91PS_MciDevice pMCI_Device,unsigned int relative_card_address)
  319. +int AT91F_MCI_GetStatus(AT91PS_MciDevice pMCI_Device,unsigned int relative_card_address)
  320. {
  321. if (AT91F_MCI_SendCommand(pMCI_Device,
  322. AT91C_SEND_STATUS_CMD,
  323. @@ -139,10 +153,25 @@
  324. }
  325. //*----------------------------------------------------------------------------
  326. +//* \fn AT91F_MCI_Handler
  327. +//* \brief MCI Handler
  328. +//*----------------------------------------------------------------------------
  329. +extern "C" void AT91F_MCI_Handler(void);
  330. +
  331. +void AT91F_MCI_Handler(void)
  332. +{
  333. + int status;
  334. +
  335. + status = ( AT91C_BASE_MCI->MCI_SR & AT91C_BASE_MCI->MCI_IMR );
  336. +
  337. + AT91F_MCI_Device_Handler(&MCI_Device,status);
  338. +}
  339. +
  340. +//*----------------------------------------------------------------------------
  341. //* \fn AT91F_MCI_ReadBlock
  342. //* \brief Read an ENTIRE block or PARTIAL block
  343. //*----------------------------------------------------------------------------
  344. -AT91S_MCIDeviceStatus AT91F_MCI_ReadBlock(
  345. +int AT91F_MCI_ReadBlock(
  346. AT91PS_MciDevice pMCI_Device,
  347. int src,
  348. unsigned int *dataBuffer,
  349. @@ -205,7 +234,7 @@
  350. //* \fn AT91F_MCI_WriteBlock
  351. //* \brief Write an ENTIRE block but not always PARTIAL block !!!
  352. //*----------------------------------------------------------------------------
  353. -AT91S_MCIDeviceStatus AT91F_MCI_WriteBlock(
  354. +int AT91F_MCI_WriteBlock(
  355. AT91PS_MciDevice pMCI_Device,
  356. int dest,
  357. unsigned int *dataBuffer,
  358. @@ -268,7 +297,7 @@
  359. //* \fn AT91F_MCI_MMC_SelectCard
  360. //* \brief Toggles a card between the Stand_by and Transfer states or between Programming and Disconnect states
  361. //*------------------------------------------------------------------------------------------------------------
  362. -AT91S_MCIDeviceStatus AT91F_MCI_MMC_SelectCard(AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address)
  363. +int AT91F_MCI_MMC_SelectCard(AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address)
  364. {
  365. int status;
  366. @@ -302,7 +331,7 @@
  367. //* \fn AT91F_MCI_GetCSD
  368. //* \brief Asks to the specified card to send its CSD
  369. //*----------------------------------------------------------------------------
  370. -AT91S_MCIDeviceStatus AT91F_MCI_GetCSD (AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address , unsigned int * response)
  371. +int AT91F_MCI_GetCSD (AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address , unsigned int * response)
  372. {
  373. if(AT91F_MCI_SendCommand(pMCI_Device,
  374. @@ -322,7 +351,7 @@
  375. //* \fn AT91F_MCI_SetBlocklength
  376. //* \brief Select a block length for all following block commands (R/W)
  377. //*----------------------------------------------------------------------------
  378. -AT91S_MCIDeviceStatus AT91F_MCI_SetBlocklength(AT91PS_MciDevice pMCI_Device,unsigned int length)
  379. +int AT91F_MCI_SetBlocklength(AT91PS_MciDevice pMCI_Device,unsigned int length)
  380. {
  381. return( AT91F_MCI_SendCommand(pMCI_Device, AT91C_SET_BLOCKLEN_CMD, length) );
  382. }
  383. @@ -332,7 +361,7 @@
  384. //* \fn AT91F_MCI_MMC_GetAllOCR
  385. //* \brief Asks to all cards to send their operations conditions
  386. //*----------------------------------------------------------------------------
  387. -AT91S_MCIDeviceStatus AT91F_MCI_MMC_GetAllOCR (AT91PS_MciDevice pMCI_Device)
  388. +int AT91F_MCI_MMC_GetAllOCR (AT91PS_MciDevice pMCI_Device)
  389. {
  390. unsigned int response =0x0;
  391. @@ -357,7 +386,7 @@
  392. //* \fn AT91F_MCI_MMC_GetAllCID
  393. //* \brief Asks to the MMC on the chosen slot to send its CID
  394. //*----------------------------------------------------------------------------
  395. -AT91S_MCIDeviceStatus AT91F_MCI_MMC_GetAllCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
  396. +int AT91F_MCI_MMC_GetAllCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
  397. {
  398. int Nb_Cards_Found=-1;
  399. @@ -391,7 +420,7 @@
  400. //* \fn AT91F_MCI_MMC_Init
  401. //* \brief Return the MMC initialisation status
  402. //*----------------------------------------------------------------------------
  403. -AT91S_MCIDeviceStatus AT91F_MCI_MMC_Init (AT91PS_MciDevice pMCI_Device)
  404. +int AT91F_MCI_MMC_Init (AT91PS_MciDevice pMCI_Device)
  405. {
  406. unsigned int tab_response[4];
  407. unsigned int mult,blocknr;
  408. @@ -454,7 +483,7 @@
  409. //* \fn AT91F_MCI_SDCard_GetOCR
  410. //* \brief Asks to all cards to send their operations conditions
  411. //*----------------------------------------------------------------------------
  412. -AT91S_MCIDeviceStatus AT91F_MCI_SDCard_GetOCR (AT91PS_MciDevice pMCI_Device)
  413. +int AT91F_MCI_SDCard_GetOCR (AT91PS_MciDevice pMCI_Device)
  414. {
  415. unsigned int response =0x0;
  416. @@ -479,7 +508,7 @@
  417. //* \fn AT91F_MCI_SDCard_GetCID
  418. //* \brief Asks to the SDCard on the chosen slot to send its CID
  419. //*----------------------------------------------------------------------------
  420. -AT91S_MCIDeviceStatus AT91F_MCI_SDCard_GetCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
  421. +int AT91F_MCI_SDCard_GetCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
  422. {
  423. if(AT91F_MCI_SendCommand(pMCI_Device,
  424. AT91C_ALL_SEND_CID_CMD,
  425. @@ -498,7 +527,7 @@
  426. //* \fn AT91F_MCI_SDCard_SetBusWidth
  427. //* \brief Set bus width for SDCard
  428. //*----------------------------------------------------------------------------
  429. -AT91S_MCIDeviceStatus AT91F_MCI_SDCard_SetBusWidth(AT91PS_MciDevice pMCI_Device)
  430. +int AT91F_MCI_SDCard_SetBusWidth(AT91PS_MciDevice pMCI_Device)
  431. {
  432. volatile int ret_value;
  433. char bus_width;
  434. @@ -529,7 +558,7 @@
  435. //* \fn AT91F_MCI_SDCard_Init
  436. //* \brief Return the SDCard initialisation status
  437. //*----------------------------------------------------------------------------
  438. -AT91S_MCIDeviceStatus AT91F_MCI_SDCard_Init (AT91PS_MciDevice pMCI_Device)
  439. +int AT91F_MCI_SDCard_Init (AT91PS_MciDevice pMCI_Device)
  440. {
  441. unsigned int tab_response[4];
  442. unsigned int mult,blocknr;
  443. @@ -567,7 +596,7 @@
  444. pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity = pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length * blocknr;
  445. //// End of Compute Memory Capacity
  446. - printf("BLK 0x%x", pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length);
  447. + printf("SD-Card: %d Bytes\n\r", pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity);
  448. if( AT91F_MCI_SDCard_SetBusWidth(pMCI_Device) == AT91C_CMD_SEND_OK )
  449. {
  450. @@ -579,3 +608,141 @@
  451. }
  452. return AT91C_INIT_ERROR;
  453. }
  454. +
  455. +//*----------------------------------------------------------------------------
  456. +//* \fn AT91F_CfgDevice
  457. +//* \brief This function is used to initialise MMC or SDCard Features
  458. +//*----------------------------------------------------------------------------
  459. +void AT91F_CfgDevice(void)
  460. +{
  461. + // Init Device Structure
  462. +
  463. + MCI_Device_Features.Relative_Card_Address = 0;
  464. + MCI_Device_Features.Card_Inserted = AT91C_CARD_REMOVED;
  465. + MCI_Device_Features.Max_Read_DataBlock_Length = 0;
  466. + MCI_Device_Features.Max_Write_DataBlock_Length = 0;
  467. + MCI_Device_Features.Read_Partial = 0;
  468. + MCI_Device_Features.Write_Partial = 0;
  469. + MCI_Device_Features.Erase_Block_Enable = 0;
  470. + MCI_Device_Features.Sector_Size = 0;
  471. + MCI_Device_Features.Memory_Capacity = 0;
  472. +
  473. + MCI_Device_Desc.state = AT91C_MCI_IDLE;
  474. + MCI_Device_Desc.SDCard_bus_width = AT91C_MCI_SCDBUS;
  475. +
  476. + // Init AT91S_DataFlash Global Structure, by default AT45DB choosen !!!
  477. + MCI_Device.pMCI_DeviceDesc = &MCI_Device_Desc;
  478. + MCI_Device.pMCI_DeviceFeatures = &MCI_Device_Features;
  479. +
  480. +}
  481. +
  482. +//*----------------------------------------------------------------------------
  483. +//* \fn AT91F_MCI_Init
  484. +//* \brief Initialsise Card
  485. +//*----------------------------------------------------------------------------
  486. +int AT91F_MCI_Init(void)
  487. +{
  488. +
  489. +///////////////////////////////////////////////////////////////////////////////////////////
  490. +// MCI Init : common to MMC and SDCard
  491. +///////////////////////////////////////////////////////////////////////////////////////////
  492. +
  493. + // Set up PIO SDC_TYPE to switch on MMC/SDCard and not DataFlash Card
  494. + AT91F_PIO_CfgOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
  495. + AT91F_PIO_SetOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
  496. +
  497. + // Init MCI for MMC and SDCard interface
  498. + AT91F_MCI_CfgPIO();
  499. + AT91F_MCI_CfgPMC();
  500. + AT91F_PDC_Open(AT91C_BASE_PDC_MCI);
  501. +
  502. + // Disable all the interrupts
  503. + AT91C_BASE_MCI->MCI_IDR = 0xFFFFFFFF;
  504. +
  505. + // Init MCI Device Structures
  506. + AT91F_CfgDevice();
  507. +
  508. + // Configure MCI interrupt
  509. + AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
  510. + AT91C_ID_MCI,
  511. + AT91C_AIC_PRIOR_HIGHEST,
  512. + AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,
  513. + AT91F_ASM_MCI_Handler);
  514. +
  515. + // Enable MCI interrupt
  516. + AT91F_AIC_EnableIt(AT91C_BASE_AIC,AT91C_ID_MCI);
  517. +
  518. + // Enable Receiver
  519. + AT91F_US_EnableRx((AT91PS_USART) AT91C_BASE_DBGU);
  520. +
  521. + AT91F_MCI_Configure(AT91C_BASE_MCI,
  522. + AT91C_MCI_DTOR_1MEGA_CYCLES,
  523. + AT91C_MCI_MR_PDCMODE, // 15MHz for MCK = 60MHz (CLKDIV = 1)
  524. + AT91C_MCI_SDCARD_4BITS_SLOTA);
  525. +
  526. + if(AT91F_MCI_SDCard_Init(&MCI_Device) != AT91C_INIT_OK)
  527. + return FALSE;
  528. + else
  529. + return TRUE;
  530. +
  531. +}
  532. +
  533. +//*----------------------------------------------------------------------------
  534. +//* \fn AT91F_MCIDeviceWaitReady
  535. +//* \brief Wait for MCI Device ready
  536. +//*----------------------------------------------------------------------------
  537. +void AT91F_MCIDeviceWaitReady(unsigned int timeout)
  538. +{
  539. + volatile int status;
  540. +
  541. + do
  542. + {
  543. + status = AT91C_BASE_MCI->MCI_SR;
  544. + timeout--;
  545. + }
  546. + while( !(status & AT91C_MCI_NOTBUSY) && (timeout>0) );
  547. +}
  548. +
  549. +unsigned int swab32(unsigned int data)
  550. +{
  551. + unsigned int res = 0;
  552. +
  553. + res = (data & 0x000000ff) << 24 |
  554. + (data & 0x0000ff00) << 8 |
  555. + (data & 0x00ff0000) >> 8 |
  556. + (data & 0xff000000) >> 24;
  557. +
  558. + return res;
  559. +}
  560. +
  561. +//*--------------------------------------------------------------------
  562. +//* \fn AT91F_MCI_ReadBlockSwab
  563. +//* \brief Read Block and swap byte order
  564. +//*--------------------------------------------------------------------
  565. +int AT91F_MCI_ReadBlockSwab(
  566. + AT91PS_MciDevice pMCI_Device,
  567. + int src,
  568. + unsigned int *databuffer,
  569. + int sizeToRead)
  570. +{
  571. + int i;
  572. + unsigned char *buf = (unsigned char *)databuffer;
  573. +
  574. + //* Read Block 1
  575. + for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++)
  576. + *buf++ = 0x00;
  577. + AT91F_MCI_ReadBlock(&MCI_Device,src,databuffer,sizeToRead);
  578. +
  579. + //* Wait end of Read
  580. + AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
  581. +
  582. + {
  583. + int index;
  584. + unsigned int *uiBuffer = databuffer;
  585. +
  586. + for(index = 0; index < 512/4; index++)
  587. + uiBuffer[index] = swab32(uiBuffer[index]);
  588. + }
  589. + return(1);
  590. +}
  591. +
  592. diff -urN romboot.old/mci_main.cpp romboot/mci_main.cpp
  593. --- romboot.old/mci_main.cpp 2007-03-24 13:34:19.000000000 +0100
  594. +++ romboot/mci_main.cpp 1970-01-01 01:00:00.000000000 +0100
  595. @@ -1,317 +0,0 @@
  596. -//*----------------------------------------------------------------------------
  597. -//* ATMEL Microcontroller Software Support - ROUSSET -
  598. -//*----------------------------------------------------------------------------
  599. -//* The software is delivered "AS IS" without warranty or condition of any
  600. -//* kind, either express, implied or statutory. This includes without
  601. -//* limitation any warranty or condition with respect to merchantability or
  602. -//* fitness for any particular purpose, or against the infringements of
  603. -//* intellectual property rights of others.
  604. -//*----------------------------------------------------------------------------
  605. -//* File Name : main.c
  606. -//* Object : main application written in C
  607. -//* Creation : FB 21/11/2002
  608. -//*
  609. -//*----------------------------------------------------------------------------
  610. -#include "com.h"
  611. -#include "dataflash.h"
  612. -#include <AT91C_MCI_Device.h>
  613. -
  614. -#define AT91C_MCI_TIMEOUT 1000000 /* For AT91F_MCIDeviceWaitReady */
  615. -#define BUFFER_SIZE_MCI_DEVICE 512
  616. -#define MASTER_CLOCK 60000000
  617. -#define FALSE -1
  618. -#define TRUE 1
  619. -
  620. -//* External Functions
  621. -extern "C" void AT91F_ASM_MCI_Handler(void);
  622. -extern "C" void AT91F_MCI_Device_Handler(AT91PS_MciDevice,unsigned int);
  623. -extern AT91S_MCIDeviceStatus AT91F_MCI_SDCard_Init (AT91PS_MciDevice);
  624. -extern AT91S_MCIDeviceStatus AT91F_MCI_SetBlocklength(AT91PS_MciDevice,unsigned int);
  625. -extern AT91S_MCIDeviceStatus AT91F_MCI_ReadBlock(AT91PS_MciDevice,int,unsigned int *,int);
  626. -extern AT91S_MCIDeviceStatus AT91F_MCI_WriteBlock(AT91PS_MciDevice,int,unsigned int *,int);
  627. -//* Global Variables
  628. -AT91S_MciDeviceFeatures MCI_Device_Features;
  629. -AT91S_MciDeviceDesc MCI_Device_Desc;
  630. -AT91S_MciDevice MCI_Device;
  631. -
  632. -unsigned int dlBuffer = 0x20000000;
  633. -#undef MCI_TEST
  634. -#ifdef MCI_TEST
  635. -char TestString[] = "\r\nHello Hamish\r\n";
  636. -#endif
  637. -
  638. -//*----------------------------------------------------------------------------
  639. -//* \fn AT91F_MCIDeviceWaitReady
  640. -//* \brief Wait for MCI Device ready
  641. -//*----------------------------------------------------------------------------
  642. -void AT91F_MCIDeviceWaitReady(unsigned int timeout)
  643. -{
  644. - volatile int status;
  645. -
  646. - do
  647. - {
  648. - status = AT91C_BASE_MCI->MCI_SR;
  649. - timeout--;
  650. - }
  651. - while( !(status & AT91C_MCI_NOTBUSY) && (timeout>0) );
  652. -}
  653. -
  654. -unsigned int swab32(unsigned int data)
  655. -{
  656. - unsigned int res = 0;
  657. -
  658. - res = (data & 0x000000ff) << 24 |
  659. - (data & 0x0000ff00) << 8 |
  660. - (data & 0x00ff0000) >> 8 |
  661. - (data & 0xff000000) >> 24;
  662. -
  663. - return res;
  664. -}
  665. -
  666. -AT91S_MCIDeviceStatus readblock(
  667. - AT91PS_MciDevice pMCI_Device,
  668. - int src,
  669. - unsigned int *databuffer,
  670. - int sizeToRead)
  671. -{
  672. - int i;
  673. - unsigned char *buf = (unsigned char *)databuffer;
  674. -
  675. - //* Read Block 1
  676. - for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++)
  677. - *buf++ = 0x00;
  678. - AT91F_MCI_ReadBlock(&MCI_Device,src,databuffer,sizeToRead);
  679. -
  680. - //* Wait end of Read
  681. - AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
  682. -
  683. - {
  684. - int index;
  685. - unsigned int *uiBuffer = databuffer;
  686. -
  687. - for(index = 0; index < 512/4; index++)
  688. - uiBuffer[index] = swab32(uiBuffer[index]);
  689. - }
  690. - return(1);
  691. -}
  692. -
  693. -#if 0
  694. -void printdata(unsigned int bufpos)
  695. - {
  696. - unsigned int *uip;
  697. - int linebytes = 16;
  698. - int nbytes = 64;
  699. - int size = 4;
  700. - int i;
  701. -
  702. - uip = (unsigned int *)bufpos;
  703. -
  704. - do {
  705. -
  706. - for(i=0; i<linebytes; i+=size) {
  707. - printf(" %08x", *uip++);
  708. - }
  709. -
  710. - printf("\n\r");
  711. - nbytes -= linebytes;
  712. - } while (nbytes > 0);
  713. - }
  714. -#endif
  715. -//extern char message[40];
  716. -
  717. -int notnull(int bufpos, unsigned int len)
  718. -{
  719. - int i;
  720. - unsigned char * bp = (unsigned char *)bufpos;
  721. -
  722. - for (i=0; i<len; i++)
  723. - if (bp[i] != '\0')
  724. - return(1);
  725. -
  726. - return(0);
  727. -}
  728. -//*----------------------------------------------------------------------------
  729. -//* \fn AT91F_Test
  730. -//* \brief Test Functions
  731. -//*----------------------------------------------------------------------------
  732. -int AT91F_Test(void)
  733. -{
  734. - int i;
  735. - unsigned int Max_Read_DataBlock_Length;
  736. - int block = 0;
  737. - int bufpos = dlBuffer;
  738. - int lastvalid = 0;
  739. - int NbPage = 0;
  740. -
  741. -
  742. - Max_Read_DataBlock_Length = MCI_Device.pMCI_DeviceFeatures->Max_Read_DataBlock_Length;
  743. -
  744. - //* ReadBlock & WriteBlock Test -> Entire Block
  745. -
  746. - //* Wait MCI Device Ready
  747. - AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
  748. -
  749. -#ifdef MCI_TEST
  750. - //* Read Block 1
  751. - for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++) Buffer[i] = 0x00;
  752. - AT91F_MCI_ReadBlock(&MCI_Device,(1*Max_Read_DataBlock_Length),(unsigned int*) Buffer,Max_Read_DataBlock_Length);
  753. -
  754. - //* Wait end of Read
  755. - AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
  756. -
  757. - // Write Page 1
  758. -// sprintf(Buffer,"\n\rThis sentence is written in your device... Congratulations\n\r");
  759. - for(i=0; i<16; i++)
  760. - Buffer[i] = TestString[i];
  761. - AT91F_MCI_WriteBlock(&MCI_Device,(1*Max_Read_DataBlock_Length),(unsigned int*) Buffer,Max_Read_DataBlock_Length);
  762. -
  763. - //* Wait end of Write
  764. - AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
  765. -#endif
  766. -
  767. - for(i=0; i<64; i++) {
  768. - readblock(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
  769. - if (notnull(bufpos, Max_Read_DataBlock_Length))
  770. - lastvalid++;
  771. - block++;
  772. - bufpos += 512;
  773. - }
  774. -
  775. - i = dataflash_info[0].Device.pages_number;
  776. - while(i>>=1)
  777. - NbPage++;
  778. - i = lastvalid + (NbPage << 13) + (dataflash_info[0].Device.pages_size << 17);
  779. - *(int *)(dlBuffer + 0x14) = i;
  780. -
  781. - for(i=0; i<4688; i++) {
  782. - readblock(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
  783. - block++;
  784. - bufpos += 512;
  785. - }
  786. - write_dataflash(0xc0000000, dlBuffer, 512 * block);
  787. - //* End Of Test
  788. - printf("DONE %d\n\r", lastvalid);
  789. -
  790. -// printf(Buffer);
  791. -
  792. - return TRUE;
  793. -}
  794. -
  795. -//*----------------------------------------------------------------------------
  796. -//* \fn AT91F_CfgDevice
  797. -//* \brief This function is used to initialise MMC or SDCard Features
  798. -//*----------------------------------------------------------------------------
  799. -void AT91F_CfgDevice(void)
  800. -{
  801. - // Init Device Structure
  802. -
  803. - MCI_Device_Features.Relative_Card_Address = 0;
  804. - MCI_Device_Features.Card_Inserted = AT91C_CARD_REMOVED;
  805. - MCI_Device_Features.Max_Read_DataBlock_Length = 0;
  806. - MCI_Device_Features.Max_Write_DataBlock_Length = 0;
  807. - MCI_Device_Features.Read_Partial = 0;
  808. - MCI_Device_Features.Write_Partial = 0;
  809. - MCI_Device_Features.Erase_Block_Enable = 0;
  810. - MCI_Device_Features.Sector_Size = 0;
  811. - MCI_Device_Features.Memory_Capacity = 0;
  812. -
  813. - MCI_Device_Desc.state = AT91C_MCI_IDLE;
  814. - MCI_Device_Desc.SDCard_bus_width = AT91C_MCI_SCDBUS;
  815. -
  816. - // Init AT91S_DataFlash Global Structure, by default AT45DB choosen !!!
  817. - MCI_Device.pMCI_DeviceDesc = &MCI_Device_Desc;
  818. - MCI_Device.pMCI_DeviceFeatures = &MCI_Device_Features;
  819. -
  820. -}
  821. -
  822. -//*----------------------------------------------------------------------------
  823. -//* \fn AT91F_Test_SDCard
  824. -//* \brief Configure MCI for SDCard and complete SDCard init, then jump to Test Functions
  825. -//*----------------------------------------------------------------------------
  826. -int AT91F_Test_SDCard(void)
  827. -{
  828. - //////////////////////////////////////////////////////////
  829. - //* For SDCard Init
  830. - //////////////////////////////////////////////////////////
  831. -
  832. - AT91F_MCI_Configure(AT91C_BASE_MCI,
  833. - AT91C_MCI_DTOR_1MEGA_CYCLES,
  834. - AT91C_MCI_MR_PDCMODE, // 15MHz for MCK = 60MHz (CLKDIV = 1)
  835. - AT91C_MCI_SDCARD_4BITS_SLOTA);
  836. -
  837. - if(AT91F_MCI_SDCard_Init(&MCI_Device) != AT91C_INIT_OK)
  838. - return FALSE;
  839. -
  840. - printf("\n\rINI OK: TST\n\r");
  841. -
  842. - // Enter Main Tests
  843. - return(AT91F_Test());
  844. -}
  845. -
  846. -//*----------------------------------------------------------------------------
  847. -//* \fn AT91F_MCI_Handler
  848. -//* \brief MCI Handler
  849. -//*----------------------------------------------------------------------------
  850. -extern "C" void AT91F_MCI_Handler(void);
  851. -
  852. -void AT91F_MCI_Handler(void)
  853. -{
  854. - int status;
  855. -
  856. - status = ( AT91C_BASE_MCI->MCI_SR & AT91C_BASE_MCI->MCI_IMR );
  857. -
  858. - AT91F_MCI_Device_Handler(&MCI_Device,status);
  859. -}
  860. -
  861. -//*----------------------------------------------------------------------------
  862. -//* \fn main
  863. -//* \brief main function
  864. -//*----------------------------------------------------------------------------
  865. -int mci_main(void)
  866. -{
  867. -// printf("MCI Test\n\r");
  868. -
  869. -///////////////////////////////////////////////////////////////////////////////////////////
  870. -// MCI Init : common to MMC and SDCard
  871. -///////////////////////////////////////////////////////////////////////////////////////////
  872. -
  873. -// printf("\n\rInit MCI Interface\n\r");
  874. -
  875. - // Set up PIO SDC_TYPE to switch on MMC/SDCard and not DataFlash Card
  876. - AT91F_PIO_CfgOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
  877. - AT91F_PIO_SetOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
  878. -
  879. - // Init MCI for MMC and SDCard interface
  880. - AT91F_MCI_CfgPIO();
  881. - AT91F_MCI_CfgPMC();
  882. - AT91F_PDC_Open(AT91C_BASE_PDC_MCI);
  883. -
  884. - // Disable all the interrupts
  885. - AT91C_BASE_MCI->MCI_IDR = 0xFFFFFFFF;
  886. -
  887. - // Init MCI Device Structures
  888. - AT91F_CfgDevice();
  889. -
  890. - // Configure MCI interrupt
  891. - AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
  892. - AT91C_ID_MCI,
  893. - AT91C_AIC_PRIOR_HIGHEST,
  894. - AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,
  895. - AT91F_ASM_MCI_Handler);
  896. -
  897. - // Enable MCI interrupt
  898. - AT91F_AIC_EnableIt(AT91C_BASE_AIC,AT91C_ID_MCI);
  899. -
  900. -///////////////////////////////////////////////////////////////////////////////////////////
  901. -// Enter Test Menu
  902. -///////////////////////////////////////////////////////////////////////////////////////////
  903. -
  904. - // Enable Receiver
  905. - AT91F_US_EnableRx((AT91PS_USART) AT91C_BASE_DBGU);
  906. -
  907. - if(AT91F_Test_SDCard() == TRUE)
  908. - printf("\n\rTST OK\n\r");
  909. - else
  910. - printf("\n\rTST Fail\n\r");
  911. - return(1);
  912. -}