Added facility to program entire device from SD-Card
[openwrt.git] / target / linux / at91-2.6 / image / romboot / patches / 003-SD-reflash.patch
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);\r
6  \r
7         /* Enable PIO to access the LEDs */\r
8 -       AT91C_BASE_PIOB->PIO_PER = AT91C_PIO_PB2;\r
9 -       AT91C_BASE_PIOB->PIO_OER = AT91C_PIO_PB2;\r
10 -       AT91C_BASE_PIOB->PIO_CODR = AT91C_PIO_PB2;\r
11 +       AT91C_BASE_PIOC->PIO_PER = AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15;\r
12 +       AT91C_BASE_PIOC->PIO_OER = AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15;\r
13 +       AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15;\r
14 +       AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC8 | AT91C_PIO_PC14;\r
15    \r
16         // AT91F_DBGU_Printk("\n\rAT91F_LowLevelInit(): Debug channel initialized\n\r");\r
17  }\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
21 @@ -13,6 +13,7 @@
22  //*----------------------------------------------------------------------------\r
23  #include <AT91RM9200.h>\r
24  #include <lib_AT91RM9200.h>\r
25 +#include <AT91C_MCI_Device.h>\r
26  \r
27  #include "com.h"\r
28  #include "main.h"\r
29 @@ -39,16 +40,31 @@
30  extern void AT91F_DBGU_Printk(char *);\r
31  extern "C" void AT91F_ST_ASM_Handler(void);\r
32  extern "C" void Jump(unsigned int addr);\r
33 -extern int mci_main(void);\r
34 +extern int AT91F_MCI_Init(void);\r
35 +#define TRUE 1\r
36 +#define FALSE 0\r
37 +\r
38 +/* from trxhdr.h */\r
39 +\r
40 +#define TRX_MAGIC 0x30524448   /* "HDR0" */\r
41 +#define TRX_VERSION 1\r
42 +\r
43 +struct trx_header {\r
44 +       unsigned int magic;\r
45 +       unsigned int len;\r
46 +       unsigned int crc32;\r
47 +       unsigned int flag_version;\r
48 +       unsigned int offsets[3];\r
49 +};\r
50  \r
51  //const char *menu_separ = "*----------------------------------------*\n\r";\r
52  \r
53  const char *menu_dataflash = {\r
54 -  "1: DL DF [ad]\n\r"\r
55 -  "2: RD DF [ad]\n\r"\r
56 -       "3: CP SD\n\r"\r
57 -  "4: U-BOOT\n\r"\r
58 -  "5: RM BL in DF\n\r"\r
59 +  "1: Download DF [addr]\n\r"\r
60 +  "2: Read DF [addr]\n\r"\r
61 +       "3: Copy SD-Card\n\r"\r
62 +  "4: Start U-BOOT\n\r"\r
63 +  "5: Clear bootloder\n\r"\r
64  };\r
65  \r
66  //* Globales variables \r
67 @@ -155,14 +171,15 @@
68  //*-----------------------------------------------------------------------------\r
69  void AT91F_DisplayMenu(void)\r
70  {\r
71 -  printf("\n\rFDL SD-Card LOADER %s %s %s\n\r", AT91C_VERSION, __DATE__, __TIME__);\r
72 -//  printf(menu_separ);        \r
73 -  AT91F_DataflashPrintInfo();\r
74 -//  printf(menu_separ);        \r
75    printf(menu_dataflash);                      \r
76 -//  printf(menu_separ);        \r
77  }      \r
78  \r
79 +void AT91F_DisplayIntro(void)\r
80 +{\r
81 +  printf("\n\rFDL LOADER %s %s %s\n\r", AT91C_VERSION, __DATE__, __TIME__);\r
82 +  AT91F_DataflashPrintInfo();\r
83 +}\r
84 +\r
85  //*-----------------------------------------------------------------------------\r
86  //* Function Name       : AsciiToHex()\r
87  //* Object              : ascii to hexa conversion\r
88 @@ -311,23 +328,24 @@
89    AT91F_SetPLL();\r
90  }\r
91  \r
92 -/*void LedCode(void)\r
93 +/*\r
94 +void LedCode(void)\r
95  {\r
96         int *pRegister;\r
97         pRegister = (int *)0xFFFFF800; // Enable port C peripheral reg\r
98 -        *pRegister = 0x3c00;\r
99 +        *pRegister = (AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15);\r
100          pRegister = (int *)0xFFFFF810; // Output Enable reg\r
101 -        *pRegister = 0x3c00;\r
102 +        *pRegister = (AT91C_PIO_PC7 | AT91C_PIO_PC8 | AT91C_PIO_PC14 | AT91C_PIO_PC15);\r
103          pRegister = (int *)0xFFFFF830; // Set data\r
104 -        *pRegister = 0x1400;\r
105 +        *pRegister = AT91C_PIO_PC7 | AT91C_PIO_PC15;\r
106          pRegister = (int *)0xFFFFF834; // Clear bits\r
107 -        *pRegister = 0x2800;\r
108 +        *pRegister = AT91C_PIO_PC8 | AT91C_PIO_PC14;\r
109  }\r
110  */\r
111  \r
112 +\r
113  void AT91F_StartUboot(unsigned int dummy, void *pvoid)\r
114  {\r
115 -  //printf("Load U-BOOT from dataflash[%x] to SDRAM[%x]\n\r", AT91C_UBOOT_DATAFLASH_ADDR, AT91C_UBOOT_ADDR);\r
116    read_dataflash(AT91C_UBOOT_DATAFLASH_ADDR, AT91C_UBOOT_SIZE, (char *)(AT91C_UBOOT_ADDR));\r
117    //printf("Set PLLA to 180Mhz and Master clock to 60Mhz and start U-BOOT\n\r");\r
118    //* Reset registers\r
119 @@ -337,6 +355,67 @@
120    while(1);\r
121  }\r
122  \r
123 +#define AT91C_MCI_TIMEOUT 1000000\r
124 +\r
125 +extern AT91S_MciDevice MCI_Device;\r
126 +\r
127 +extern void AT91F_MCIDeviceWaitReady(unsigned int);\r
128 +extern int AT91F_MCI_ReadBlockSwab(AT91PS_MciDevice, int, unsigned int *, int);\r
129 +\r
130 +\r
131 +int Program_From_MCI(void)\r
132 +{\r
133 +       int i;\r
134 +       unsigned int Max_Read_DataBlock_Length;\r
135 +       int block = 0;\r
136 +       int buffer = AT91C_DOWNLOAD_BASE_ADDRESS;\r
137 +       int bufpos = AT91C_DOWNLOAD_BASE_ADDRESS;\r
138 +       int NbPage = 0;\r
139 +       struct trx_header *p;\r
140 +       unsigned int data;\r
141 +\r
142 +       p = (struct trx_header *)bufpos;\r
143 +\r
144 +       Max_Read_DataBlock_Length = MCI_Device.pMCI_DeviceFeatures->Max_Read_DataBlock_Length;\r
145 +\r
146 +       AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);\r
147 +\r
148 +       AT91F_MCI_ReadBlockSwab(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);\r
149 +\r
150 +       if (p->magic != TRX_MAGIC)\r
151 +               return FALSE;\r
152 +\r
153 +       printf("Read SD-Card\n\r");\r
154 +       AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15 | AT91C_PIO_PC8 | AT91C_PIO_PC14;\r
155 +       for (i=0; i<(p->len/512); i++) {\r
156 +               AT91F_MCI_ReadBlockSwab(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);\r
157 +               block++;\r
158 +               bufpos += Max_Read_DataBlock_Length;\r
159 +               }\r
160 +\r
161 +       NbPage = 0;\r
162 +       i = dataflash_info[0].Device.pages_number;\r
163 +       while(i >>= 1)\r
164 +               NbPage++;\r
165 +       i = ((p->offsets[1] - p->offsets[0])/ 512) + 1 + (NbPage << 13) + (dataflash_info[0].Device.pages_size << 17);\r
166 +       *(int *)(buffer + p->offsets[0] + AT91C_OFFSET_VECT6) = i;\r
167 +\r
168 +       printf("Write romboot\n\r");\r
169 +       AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15 | AT91C_PIO_PC14;\r
170 +       AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC8;\r
171 +       write_dataflash(0xc0000000, buffer + p->offsets[0], p->offsets[1] - p->offsets[0]);\r
172 +       printf("Write u-boot\n\r");\r
173 +       AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC7 | AT91C_PIO_PC15;\r
174 +       AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC8 | AT91C_PIO_PC14;\r
175 +       write_dataflash(0xc0008000, buffer + p->offsets[1], p->offsets[2] - p->offsets[1]);\r
176 +       printf("Write knl/root\n\r");\r
177 +       AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC8 | AT91C_PIO_PC15;\r
178 +       AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC7 | AT91C_PIO_PC14;\r
179 +       write_dataflash(0xc0042000, buffer + p->offsets[2], p->len - p->offsets[2]);\r
180 +       AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC8 | AT91C_PIO_PC14;\r
181 +       AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC7 | AT91C_PIO_PC15;\r
182 +       return TRUE;\r
183 +       }\r
184  \r
185  //*----------------------------------------------------------------------------\r
186  //* Function Name       : main\r
187 @@ -357,6 +436,7 @@
188    unsigned int crc1 = 0, crc2 = 0;\r
189    volatile int device;\r
190    int NbPage;\r
191 +       int mci_present;\r
192  \r
193    stdin = fopen(0, at91_dbgu_getc);\r
194    stdout = fopen(at91_dbgu_putc, 0);\r
195 @@ -387,6 +467,15 @@
196    \r
197    //   DataFlash on SPI Configuration\r
198    AT91F_DataflashInit ();\r
199 +       AT91F_DisplayIntro();\r
200 +       mci_present = AT91F_MCI_Init();\r
201 +\r
202 +#ifdef PRODTEST\r
203 +       if (mci_present) {\r
204 +               Program_From_MCI();\r
205 +               AT91F_StartUboot(0, (void *)0);\r
206 +               }\r
207 +#endif\r
208  \r
209    // start tempo to start Uboot in a delay of 1 sec if no key pressed\r
210    svcUbootTempo.Start(&svcUbootTempo, 1000, 0, AT91F_StartUboot, (void *)0);\r
211 @@ -396,7 +485,7 @@
212  \r
213    // stop tempo\r
214    svcUbootTempo.Stop(&svcUbootTempo);\r
215 -  \r
216 +\r
217         while(1) {\r
218                 while(command == 0) {\r
219                         AddressToDownload = AT91C_DOWNLOAD_BASE_ADDRESS;\r
220 @@ -444,7 +533,8 @@
221  #endif\r
222         \r
223                                 case '3':\r
224 -                                       mci_main();\r
225 +                                       if (mci_present)\r
226 +                                               Program_From_MCI();\r
227                                         command=0;\r
228                                         break;\r
229  \r
230 @@ -461,7 +551,6 @@
231                                         *i = 0;\r
232                 }\r
233                 write_dataflash(0xc0000000, 0x20000000, 0x4000);\r
234 -               printf("BL CLR\r\n");\r
235                 command = 0;\r
236                 break;\r
237             \r
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
241 @@ -27,7 +27,7 @@
242  \r
243  #define AT91C_OFFSET_VECT6              0x14        //* Offset for ARM vector 6\r
244  \r
245 -#define AT91C_VERSION   "VER 1.02"\r
246 +#define AT91C_VERSION   "VER 1.03"\r
247  // Global variables and functions definition\r
248  extern unsigned int GetTickCount(void);\r
249  #endif\r
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
253 @@ -1,12 +1,17 @@
254  LINKFLAGS= -T elf32-littlearm.lds -Ttext 0
255  COMPILEFLAGS= -Os
256  TARGET=romboot
257 +TARGET2=rbptest
258 +
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
264 +
265  LIBRARIES=
266  INCLUDES= -Iinclude
267  
268 -all:$(TARGET)
269 +all:$(TARGET) $(TARGET2)
270  
271  $(TARGET): $(OBJFILES)
272         $(LD) $(OBJFILES) -o $(TARGET).out $(LINKFLAGS) -n
273 @@ -14,6 +19,12 @@
274         $(OBJDUMP) -h -s $(TARGET).out > $(TARGET).lss
275         $(NM) -n $(TARGET).out | grep -v '\( [aUw] \)\|\(__crc_\)\|\( \$[adt]\)' > $(TARGET).map
276  
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
282 +
283  asm_isr.o: asm_isr.S
284         $(CC) -c -Iinclude -o $@ $<
285  
286 @@ -32,6 +43,12 @@
287  _umodsi3.o: _umodsi3.S
288         $(CC) -c $<
289  
290 +main.o: main.cpp
291 +       $(CC) -c $(COMPILEFLAGS) $(INCLUDES) -o main.o $<
292 +
293 +ptmain.o: main.cpp
294 +       $(CC) -c $(COMPILEFLAGS) $(INCLUDES) -D PRODTEST -o ptmain.o $<
295 +
296  #%.o: %.S
297  #      $(CC) -c $(INCLUDES) -o $@ $<
298  
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
302 @@ -16,14 +16,28 @@
303  #include <AT91C_MCI_Device.h>
304  #include "com.h"
305  
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
310 +#define FALSE                                          0
311 +#define TRUE                                           1
312 +
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;
320 +
321 +#undef ENABLE_WRITE
322  #undef MMC
323  
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,
331         unsigned int Cmd,
332         unsigned int Arg)
333 @@ -63,7 +77,7 @@
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,
341         unsigned int Arg        )
342 @@ -98,7 +112,7 @@
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)
348  {
349         if (AT91F_MCI_SendCommand(pMCI_Device,
350                                                                 AT91C_SEND_STATUS_CMD,
351 @@ -139,10 +153,25 @@
352  }
353  
354  //*----------------------------------------------------------------------------
355 +//* \fn    AT91F_MCI_Handler
356 +//* \brief MCI Handler
357 +//*----------------------------------------------------------------------------
358 +extern "C" void AT91F_MCI_Handler(void);
359 +
360 +void AT91F_MCI_Handler(void)
361 +{
362 +       int status;
363 +
364 +       status = ( AT91C_BASE_MCI->MCI_SR & AT91C_BASE_MCI->MCI_IMR );
365 +
366 +       AT91F_MCI_Device_Handler(&MCI_Device,status);
367 +}
368 +
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,
376         int src,
377         unsigned int *dataBuffer,
378 @@ -205,7 +234,7 @@
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,
385         int dest,
386         unsigned int *dataBuffer,
387 @@ -268,7 +297,7 @@
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)
393  {
394      int status;
395         
396 @@ -302,7 +331,7 @@
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)
402  {
403         
404         if(AT91F_MCI_SendCommand(pMCI_Device,
405 @@ -322,7 +351,7 @@
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)
411  {
412      return( AT91F_MCI_SendCommand(pMCI_Device, AT91C_SET_BLOCKLEN_CMD, length) );
413  }
414 @@ -332,7 +361,7 @@
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)
420  {
421         unsigned int    response =0x0;
422         
423 @@ -357,7 +386,7 @@
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)
429  {
430         int Nb_Cards_Found=-1;
431    
432 @@ -391,7 +420,7 @@
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)
438  {
439      unsigned int       tab_response[4];
440         unsigned int    mult,blocknr;
441 @@ -454,7 +483,7 @@
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)
447  {
448         unsigned int    response =0x0;
449  
450 @@ -479,7 +508,7 @@
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)
456  {
457         if(AT91F_MCI_SendCommand(pMCI_Device,
458                                                         AT91C_ALL_SEND_CID_CMD,
459 @@ -498,7 +527,7 @@
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)
465  {
466         volatile int    ret_value;
467         char                    bus_width;
468 @@ -529,7 +558,7 @@
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)
474  {
475      unsigned int       tab_response[4];
476         unsigned int    mult,blocknr;
477 @@ -567,7 +596,7 @@
478  
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);
483  
484                                 if( AT91F_MCI_SDCard_SetBusWidth(pMCI_Device) == AT91C_CMD_SEND_OK )
485                                 {       
486 @@ -579,3 +608,141 @@
487         }
488      return AT91C_INIT_ERROR;
489  }
490 +
491 +//*----------------------------------------------------------------------------
492 +//* \fn    AT91F_CfgDevice
493 +//* \brief This function is used to initialise MMC or SDCard Features
494 +//*----------------------------------------------------------------------------
495 +void AT91F_CfgDevice(void)
496 +{
497 +       // Init Device Structure
498 +
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;
508 +       
509 +       MCI_Device_Desc.state                                                   = AT91C_MCI_IDLE;
510 +       MCI_Device_Desc.SDCard_bus_width                                = AT91C_MCI_SCDBUS;
511 +       
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;
515 +
516 +}
517 +
518 +//*----------------------------------------------------------------------------
519 +//* \fn    AT91F_MCI_Init
520 +//* \brief Initialsise Card
521 +//*----------------------------------------------------------------------------
522 +int AT91F_MCI_Init(void)
523 +{
524 +
525 +///////////////////////////////////////////////////////////////////////////////////////////
526 +//  MCI Init : common to MMC and SDCard
527 +///////////////////////////////////////////////////////////////////////////////////////////
528 +
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);
532 +       
533 +       // Init MCI for MMC and SDCard interface
534 +       AT91F_MCI_CfgPIO();     
535 +       AT91F_MCI_CfgPMC();
536 +       AT91F_PDC_Open(AT91C_BASE_PDC_MCI);
537 +
538 +    // Disable all the interrupts
539 +    AT91C_BASE_MCI->MCI_IDR = 0xFFFFFFFF;
540 +
541 +       // Init MCI Device Structures
542 +       AT91F_CfgDevice();
543 +
544 +       // Configure MCI interrupt 
545 +       AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
546 +                                                AT91C_ID_MCI,
547 +                                                AT91C_AIC_PRIOR_HIGHEST,
548 +                                                AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,
549 +                                                AT91F_ASM_MCI_Handler);
550 +
551 +       // Enable MCI interrupt
552 +       AT91F_AIC_EnableIt(AT91C_BASE_AIC,AT91C_ID_MCI);
553 +
554 +       // Enable Receiver
555 +       AT91F_US_EnableRx((AT91PS_USART) AT91C_BASE_DBGU);
556 +
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);
561 +       
562 +       if(AT91F_MCI_SDCard_Init(&MCI_Device) != AT91C_INIT_OK)
563 +               return FALSE;
564 +       else
565 +               return TRUE;
566 +
567 +}
568 +
569 +//*----------------------------------------------------------------------------
570 +//* \fn    AT91F_MCIDeviceWaitReady
571 +//* \brief Wait for MCI Device ready
572 +//*----------------------------------------------------------------------------
573 +void AT91F_MCIDeviceWaitReady(unsigned int timeout)
574 +{
575 +       volatile int status;
576 +       
577 +       do
578 +       {
579 +               status = AT91C_BASE_MCI->MCI_SR;
580 +               timeout--;
581 +       }
582 +       while( !(status & AT91C_MCI_NOTBUSY)  && (timeout>0) ); 
583 +}
584 +
585 +unsigned int swab32(unsigned int data)
586 +{
587 +       unsigned int res = 0;
588 +
589 +       res = (data & 0x000000ff) << 24 |
590 +                               (data & 0x0000ff00) << 8  |
591 +                               (data & 0x00ff0000) >> 8  |
592 +                               (data & 0xff000000) >> 24;
593 +
594 +       return res;
595 +}
596 +
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,
603 +       int src,
604 +       unsigned int *databuffer,
605 +       int sizeToRead)
606 +{
607 +       int i;
608 +       unsigned char *buf = (unsigned char *)databuffer;
609 +
610 +       //* Read Block 1
611 +       for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++)
612 +               *buf++ = 0x00;  
613 +       AT91F_MCI_ReadBlock(&MCI_Device,src,databuffer,sizeToRead);
614 +
615 +       //* Wait end of Read
616 +       AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
617 +
618 +       {
619 +               int index;
620 +               unsigned int *uiBuffer = databuffer;
621 +
622 +               for(index = 0; index < 512/4; index++)
623 +                       uiBuffer[index] = swab32(uiBuffer[index]);
624 +       }
625 +       return(1);
626 +}
627 +
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
631 @@ -1,317 +0,0 @@
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
644 -//*
645 -//*----------------------------------------------------------------------------
646 -#include "com.h"
647 -#include "dataflash.h"
648 -#include <AT91C_MCI_Device.h>
649 -
650 -#define AT91C_MCI_TIMEOUT                      1000000   /* For AT91F_MCIDeviceWaitReady */
651 -#define BUFFER_SIZE_MCI_DEVICE         512
652 -#define MASTER_CLOCK                           60000000
653 -#define FALSE                                          -1
654 -#define TRUE                                           1
655 -
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;
667 -
668 -unsigned int dlBuffer = 0x20000000;
669 -#undef MCI_TEST
670 -#ifdef MCI_TEST
671 -char TestString[] = "\r\nHello Hamish\r\n";
672 -#endif
673 -
674 -//*----------------------------------------------------------------------------
675 -//* \fn    AT91F_MCIDeviceWaitReady
676 -//* \brief Wait for MCI Device ready
677 -//*----------------------------------------------------------------------------
678 -void AT91F_MCIDeviceWaitReady(unsigned int timeout)
679 -{
680 -       volatile int status;
681 -       
682 -       do
683 -       {
684 -               status = AT91C_BASE_MCI->MCI_SR;
685 -               timeout--;
686 -       }
687 -       while( !(status & AT91C_MCI_NOTBUSY)  && (timeout>0) ); 
688 -}
689 -
690 -unsigned int swab32(unsigned int data)
691 -{
692 -       unsigned int res = 0;
693 -
694 -       res = (data & 0x000000ff) << 24 |
695 -                               (data & 0x0000ff00) << 8  |
696 -                               (data & 0x00ff0000) >> 8  |
697 -                               (data & 0xff000000) >> 24;
698 -
699 -       return res;
700 -}
701 -
702 -AT91S_MCIDeviceStatus readblock(
703 -       AT91PS_MciDevice pMCI_Device,
704 -       int src,
705 -       unsigned int *databuffer,
706 -       int sizeToRead)
707 -{
708 -       int i;
709 -       unsigned char *buf = (unsigned char *)databuffer;
710 -
711 -       //* Read Block 1
712 -       for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++)
713 -               *buf++ = 0x00;  
714 -       AT91F_MCI_ReadBlock(&MCI_Device,src,databuffer,sizeToRead);
715 -
716 -       //* Wait end of Read
717 -       AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
718 -
719 -       {
720 -               int index;
721 -               unsigned int *uiBuffer = databuffer;
722 -
723 -               for(index = 0; index < 512/4; index++)
724 -                       uiBuffer[index] = swab32(uiBuffer[index]);
725 -       }
726 -       return(1);
727 -}
728 -
729 -#if 0
730 -void printdata(unsigned int bufpos)
731 -       {
732 -               unsigned int *uip;
733 -               int linebytes = 16;
734 -               int nbytes = 64;
735 -               int size = 4;
736 -               int i;
737 -
738 -               uip = (unsigned int *)bufpos;
739 -
740 -               do {
741 -
742 -                       for(i=0; i<linebytes; i+=size) {
743 -                               printf(" %08x", *uip++);
744 -                               }
745 -                       
746 -                       printf("\n\r");
747 -                       nbytes -= linebytes;
748 -               } while (nbytes > 0);
749 -       }
750 -#endif
751 -//extern char message[40];
752 -
753 -int notnull(int bufpos, unsigned int len)
754 -{
755 -       int i;
756 -       unsigned char * bp = (unsigned char *)bufpos;
757 -
758 -       for (i=0; i<len; i++)
759 -               if (bp[i] != '\0')
760 -                       return(1);
761 -
762 -       return(0);
763 -}
764 -//*----------------------------------------------------------------------------
765 -//* \fn    AT91F_Test
766 -//* \brief Test Functions
767 -//*----------------------------------------------------------------------------
768 -int AT91F_Test(void)
769 -{
770 -       int i;
771 -       unsigned int Max_Read_DataBlock_Length;
772 -       int block = 0;
773 -       int bufpos = dlBuffer;
774 -       int lastvalid = 0;
775 -       int NbPage = 0;
776 -
777 -               
778 -       Max_Read_DataBlock_Length = MCI_Device.pMCI_DeviceFeatures->Max_Read_DataBlock_Length;
779 -       
780 -       //* ReadBlock & WriteBlock Test -> Entire Block
781 -
782 -       //* Wait MCI Device Ready
783 -       AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
784 -
785 -#ifdef MCI_TEST
786 -       //* Read Block 1
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);
789 -
790 -       //* Wait end of Read
791 -       AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
792 -
793 -       // Write Page 1
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);
798 -
799 -       //* Wait end of Write
800 -       AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
801 -#endif
802 -
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))
806 -                       lastvalid++;
807 -               block++;
808 -               bufpos += 512;
809 -               }
810 -
811 -       i = dataflash_info[0].Device.pages_number;
812 -       while(i>>=1)
813 -               NbPage++;
814 -       i = lastvalid + (NbPage << 13) + (dataflash_info[0].Device.pages_size << 17);
815 -       *(int *)(dlBuffer + 0x14) = i;
816 -
817 -       for(i=0; i<4688; i++) {
818 -               readblock(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
819 -               block++;
820 -               bufpos += 512;
821 -               }
822 -       write_dataflash(0xc0000000, dlBuffer, 512 * block);
823 -       //* End Of Test
824 -       printf("DONE %d\n\r", lastvalid);
825 -                       
826 -//     printf(Buffer);
827 -
828 -       return TRUE;
829 -}
830 -
831 -//*----------------------------------------------------------------------------
832 -//* \fn    AT91F_CfgDevice
833 -//* \brief This function is used to initialise MMC or SDCard Features
834 -//*----------------------------------------------------------------------------
835 -void AT91F_CfgDevice(void)
836 -{
837 -       // Init Device Structure
838 -
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;
848 -       
849 -       MCI_Device_Desc.state                                                   = AT91C_MCI_IDLE;
850 -       MCI_Device_Desc.SDCard_bus_width                                = AT91C_MCI_SCDBUS;
851 -       
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;
855 -
856 -}
857 -
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)
863 -{
864 -       //////////////////////////////////////////////////////////
865 -       //* For SDCard Init
866 -       //////////////////////////////////////////////////////////
867 -
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);
872 -       
873 -       if(AT91F_MCI_SDCard_Init(&MCI_Device) != AT91C_INIT_OK)
874 -               return FALSE;
875 -
876 -       printf("\n\rINI OK: TST\n\r");
877 -
878 -       // Enter Main Tests     
879 -       return(AT91F_Test());
880 -}
881 -
882 -//*----------------------------------------------------------------------------
883 -//* \fn    AT91F_MCI_Handler
884 -//* \brief MCI Handler
885 -//*----------------------------------------------------------------------------
886 -extern "C" void AT91F_MCI_Handler(void);
887 -
888 -void AT91F_MCI_Handler(void)
889 -{
890 -       int status;
891 -
892 -       status = ( AT91C_BASE_MCI->MCI_SR & AT91C_BASE_MCI->MCI_IMR );
893 -
894 -       AT91F_MCI_Device_Handler(&MCI_Device,status);
895 -}
896 -
897 -//*----------------------------------------------------------------------------
898 -//* \fn    main
899 -//* \brief main function
900 -//*----------------------------------------------------------------------------
901 -int mci_main(void)
902 -{
903 -//     printf("MCI Test\n\r");
904 -
905 -///////////////////////////////////////////////////////////////////////////////////////////
906 -//  MCI Init : common to MMC and SDCard
907 -///////////////////////////////////////////////////////////////////////////////////////////
908 -
909 -//     printf("\n\rInit MCI Interface\n\r");
910 -
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);
914 -       
915 -       // Init MCI for MMC and SDCard interface
916 -       AT91F_MCI_CfgPIO();     
917 -       AT91F_MCI_CfgPMC();
918 -       AT91F_PDC_Open(AT91C_BASE_PDC_MCI);
919 -
920 -    // Disable all the interrupts
921 -    AT91C_BASE_MCI->MCI_IDR = 0xFFFFFFFF;
922 -
923 -       // Init MCI Device Structures
924 -       AT91F_CfgDevice();
925 -
926 -       // Configure MCI interrupt 
927 -       AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
928 -                                                AT91C_ID_MCI,
929 -                                                AT91C_AIC_PRIOR_HIGHEST,
930 -                                                AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,
931 -                                                AT91F_ASM_MCI_Handler);
932 -
933 -       // Enable MCI interrupt
934 -       AT91F_AIC_EnableIt(AT91C_BASE_AIC,AT91C_ID_MCI);
935 -
936 -///////////////////////////////////////////////////////////////////////////////////////////
937 -//  Enter Test Menu
938 -///////////////////////////////////////////////////////////////////////////////////////////
939 -
940 -       // Enable Receiver
941 -       AT91F_US_EnableRx((AT91PS_USART) AT91C_BASE_DBGU);
942 -
943 -       if(AT91F_Test_SDCard() == TRUE)
944 -               printf("\n\rTST OK\n\r");
945 -       else
946 -               printf("\n\rTST Fail\n\r");
947 -       return(1);              
948 -}