Added rule for objdump Added support for AT91 SD-Card in primary bootloader (romboot)
[oweals/openwrt.git] / target / linux / at91-2.6 / image / romboot / patches / 002-Add-SD-Card.patch
1 diff -urN romboot.old/asm_mci_isr.S romboot/asm_mci_isr.S
2 --- romboot.old/asm_mci_isr.S   1970-01-01 01:00:00.000000000 +0100
3 +++ romboot/asm_mci_isr.S       2007-03-22 18:52:05.000000000 +0100
4 @@ -0,0 +1,75 @@
5 +#include <AT91RM9200_inc.h>
6 +
7 +#define ARM_MODE_USER           0x10
8 +#define ARM_MODE_FIQ            0x11
9 +#define ARM_MODE_IRQ            0x12
10 +#define ARM_MODE_SVC            0x13
11 +#define ARM_MODE_ABORT          0x17
12 +#define ARM_MODE_UNDEF          0x1B
13 +#define ARM_MODE_SYS            0x1F
14 +
15 +#define I_BIT                   0x80
16 +#define F_BIT                   0x40
17 +#define T_BIT                   0x20
18 +
19 +
20 +/* -----------------------------------------------------------------------------
21 +   AT91F_ASM_MCI_Handler
22 +   ---------------------
23 +      Handler called by the AIC
24 +        
25 +      Save context
26 +        Call C handler
27 +    Restore context
28 +   ----------------------------------------------------------------------------- */
29 +               
30 +.global AT91F_ASM_MCI_Handler  
31 +
32 +AT91F_ASM_MCI_Handler:
33 +/*  Adjust and save LR_irq in IRQ stack */
34 +       sub         r14, r14, #4
35 +       stmfd       sp!, {r14}
36 +
37 +/*  Write in the IVR to support Protect Mode
38 +  No effect in Normal Mode
39 +  De-assert the NIRQ and clear the source in Protect Mode */
40 +       ldr         r14, =AT91C_BASE_AIC
41 +       str         r14, [r14, #AIC_IVR]
42 +
43 +/*  Save SPSR and r0 in IRQ stack */
44 +       mrs         r14, SPSR
45 +       stmfd       sp!, {r0, r14}
46 +
47 +/*  Enable Interrupt and Switch in SYS Mode */
48 +       mrs         r0, CPSR
49 +       bic         r0, r0, #I_BIT
50 +       orr         r0, r0, #ARM_MODE_SYS
51 +       msr         CPSR_c, r0
52
53 +/* Save scratch/used registers and LR in User Stack */
54 +       stmfd       sp!, { r1-r3, r12, r14}
55 +
56 +       ldr     r1, =AT91F_MCI_Handler
57 +       mov     r14, pc
58 +       bx      r1
59 +
60 +/*  Restore scratch/used registers and LR from User Stack */
61 +       ldmia       sp!, { r1-r3, r12, r14}
62 +
63 +/*  Disable Interrupt and switch back in IRQ mode */
64 +       mrs         r0, CPSR
65 +       bic         r0, r0, #ARM_MODE_SYS
66 +       orr         r0, r0, #I_BIT | ARM_MODE_IRQ
67 +       msr         CPSR_c, r0
68 +
69 +/*  Mark the End of Interrupt on the AIC */
70 +       ldr         r0, =AT91C_BASE_AIC
71 +       str         r0, [r0, #AIC_EOICR]
72 +
73 +/*  Restore SPSR_irq and r0 from IRQ stack */
74 +       ldmia       sp!, {r0, r14}
75 +       msr         SPSR_cxsf, r14
76 +
77 +/*  Restore adjusted  LR_irq from IRQ stack directly in the PC */
78 +       ldmia       sp!, {pc}^
79 +
80 diff -urN romboot.old/compile romboot/compile
81 --- romboot.old/compile 2004-08-04 18:24:24.000000000 +0200
82 +++ romboot/compile     1970-01-01 01:00:00.000000000 +0100
83 @@ -1,35 +0,0 @@
84 -#!/bin/sh
85 -
86 -OUTPUT=romboot
87 -
88 -CROSS=/space/arm/buildroot/build_arm_nofpu/staging_dir/bin/arm-linux-
89 -#CROSS=/opt/cross/bin/arm-linux-
90 -#GCC="$CROSS"gcc
91 -GCC="$CROSS"gcc-msoft-float
92 -LD="$CROSS"ld
93 -OBJCOPY="$CROSS"objcopy
94 -SIZE="$CROSS"size
95 -OBJDUMP="$CROSS"objdump
96 -
97 -LDFLAGS="-T elf32-littlearm.lds -Ttext 0"
98 -
99 -$GCC asm_isr.S -c -Iinclude
100 -$GCC cstartup_ram.S -c -Iinclude
101 -$GCC jump.S -c -Iinclude
102 -$GCC at45.cpp -c -Iinclude -Os
103 -$GCC com.cpp -c -Iinclude -Os
104 -$GCC dataflash.cpp -c -Iinclude -Os
105 -$GCC init.cpp -c -Iinclude -Os
106 -$GCC main.cpp -c -Iinclude -Os
107 -$GCC -c stdio.cpp -Os
108 -$GCC -c _udivsi3.S
109 -$GCC -c _umodsi3.S
110 -$GCC -c div0.c -Os
111 -
112 -$LD cstartup_ram.o asm_isr.o jump.o at45.o com.o dataflash.o init.o main.o stdio.o _udivsi3.o _umodsi3.o div0.o -o $OUTPUT.out $LDFLAGS -n
113 -
114 -$OBJCOPY $OUTPUT.out -O binary $OUTPUT.bin
115 -
116 -$OBJDUMP -h -s $OUTPUT.out > $OUTPUT.lss
117 -
118 -$SIZE $OUTPUT.out
119 diff -urN romboot.old/include/AT91C_MCI_Device.h romboot/include/AT91C_MCI_Device.h
120 --- romboot.old/include/AT91C_MCI_Device.h      1970-01-01 01:00:00.000000000 +0100
121 +++ romboot/include/AT91C_MCI_Device.h  2007-03-22 18:53:51.000000000 +0100
122 @@ -0,0 +1,379 @@
123 +//*---------------------------------------------------------------------------
124 +//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
125 +//*---------------------------------------------------------------------------
126 +//* The software is delivered "AS IS" without warranty or condition of any
127 +//* kind, either express, implied or statutory. This includes without
128 +//* limitation any warranty or condition with respect to merchantability or
129 +//* fitness for any particular purpose, or against the infringements of
130 +//* intellectual property rights of others.
131 +//*---------------------------------------------------------------------------
132 +//* File Name           : AT91C_MCI_Device.h
133 +//* Object              : Data Flash Atmel Description File
134 +//* Translator          :
135 +//*
136 +//* 1.0 26/11/02 FB            : Creation
137 +//*---------------------------------------------------------------------------
138 +
139 +#ifndef AT91C_MCI_Device_h
140 +#define AT91C_MCI_Device_h
141 +
142 +#include "AT91RM9200.h"
143 +#include "lib_AT91RM9200.h"
144 +
145 +typedef unsigned int AT91S_MCIDeviceStatus;
146 +
147 +/////////////////////////////////////////////////////////////////////////////////////////////////////
148 +
149 +#define AT91C_CARD_REMOVED                     0
150 +#define AT91C_MMC_CARD_INSERTED                1
151 +#define AT91C_SD_CARD_INSERTED         2
152 +
153 +#define AT91C_NO_ARGUMENT                      0x0
154 +
155 +#define AT91C_FIRST_RCA                                0xCAFE
156 +#define AT91C_MAX_MCI_CARDS                    10
157 +
158 +#define AT91C_BUS_WIDTH_1BIT           0x00
159 +#define AT91C_BUS_WIDTH_4BITS          0x02
160 +
161 +/* Driver State */
162 +#define AT91C_MCI_IDLE                 0x0
163 +#define AT91C_MCI_TIMEOUT_ERROR                0x1
164 +#define AT91C_MCI_RX_SINGLE_BLOCK      0x2
165 +#define AT91C_MCI_RX_MULTIPLE_BLOCK    0x3
166 +#define AT91C_MCI_RX_STREAM                    0x4
167 +#define AT91C_MCI_TX_SINGLE_BLOCK      0x5
168 +#define AT91C_MCI_TX_MULTIPLE_BLOCK    0x6
169 +#define AT91C_MCI_TX_STREAM            0x7
170 +
171 +/* TimeOut */
172 +#define AT91C_TIMEOUT_CMDRDY           30
173 +
174 +/////////////////////////////////////////////////////////////////////////////////////////////////////
175 +// MMC & SDCard Structures 
176 +/////////////////////////////////////////////////////////////////////////////////////////////////////
177 +
178 +/*-----------------------------------------------*/
179 +/* SDCard Device Descriptor Structure Definition */
180 +/*-----------------------------------------------*/
181 +typedef struct _AT91S_MciDeviceDesc
182 +{
183 +    volatile unsigned char     state;
184 +       unsigned char                   SDCard_bus_width;
185 +
186 +} AT91S_MciDeviceDesc, *AT91PS_MciDeviceDesc;
187 +
188 +/*---------------------------------------------*/
189 +/* MMC & SDCard Structure Device Features         */
190 +/*---------------------------------------------*/
191 +typedef struct _AT91S_MciDeviceFeatures
192 +{
193 +    unsigned char      Card_Inserted;                          // (0=AT91C_CARD_REMOVED) (1=AT91C_MMC_CARD_INSERTED) (2=AT91C_SD_CARD_INSERTED)
194 +    unsigned int       Relative_Card_Address;          // RCA
195 +       unsigned int    Max_Read_DataBlock_Length;      // 2^(READ_BL_LEN) in CSD 
196 +       unsigned int    Max_Write_DataBlock_Length;     // 2^(WRITE_BL_LEN) in CSD
197 +       unsigned char   Read_Partial;                           // READ_BL_PARTIAL
198 +       unsigned char   Write_Partial;                          // WRITE_BL_PARTIAL
199 +       unsigned char   Erase_Block_Enable;                     // ERASE_BLK_EN
200 +       unsigned char   Read_Block_Misalignment;        // READ_BLK_MISALIGN
201 +       unsigned char   Write_Block_Misalignment;       // WRITE_BLK_MISALIGN
202 +       unsigned char   Sector_Size;                            // SECTOR_SIZE
203 +       unsigned int    Memory_Capacity;                        // Size in bits of the device
204 +       
205 +}      AT91S_MciDeviceFeatures, *AT91PS_MciDeviceFeatures ;
206 +
207 +/*---------------------------------------------*/
208 +/* MCI Device Structure Definition                        */
209 +/*---------------------------------------------*/
210 +typedef struct _AT91S_MciDevice
211 +{
212 +       AT91PS_MciDeviceDesc                    pMCI_DeviceDesc;        // MCI device descriptor
213 +       AT91PS_MciDeviceFeatures                pMCI_DeviceFeatures;// Pointer on a MCI device features array  
214 +}AT91S_MciDevice, *AT91PS_MciDevice;
215 +
216 +/////////////////////////////////////////////////////////////////////////////////////////////////////
217 +// MCI_CMD Register Value 
218 +/////////////////////////////////////////////////////////////////////////////////////////////////////
219 +#define AT91C_POWER_ON_INIT                                            (0      | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_INIT | AT91C_MCI_OPDCMD)
220 +
221 +/////////////////////////////////////////////////////////////////      
222 +// Class 0 & 1 commands: Basic commands and Read Stream commands
223 +/////////////////////////////////////////////////////////////////
224 +
225 +#define AT91C_GO_IDLE_STATE_CMD                                        (0      | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE )
226 +#define AT91C_MMC_GO_IDLE_STATE_CMD                            (0      | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_OPDCMD)
227 +#define AT91C_MMC_SEND_OP_COND_CMD                             (1      | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_48 | AT91C_MCI_OPDCMD)
228 +#define AT91C_ALL_SEND_CID_CMD                                 (2      | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_136 )
229 +#define AT91C_MMC_ALL_SEND_CID_CMD                             (2      | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_136 | AT91C_MCI_OPDCMD)
230 +#define AT91C_SET_RELATIVE_ADDR_CMD                            (3      | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_48           | AT91C_MCI_MAXLAT )
231 +#define AT91C_MMC_SET_RELATIVE_ADDR_CMD                        (3      | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_48           | AT91C_MCI_MAXLAT | AT91C_MCI_OPDCMD)
232 +
233 +#define AT91C_SET_DSR_CMD                                              (4      | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_NO           | AT91C_MCI_MAXLAT )    // no tested
234 +
235 +#define AT91C_SEL_DESEL_CARD_CMD                               (7      | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_48           | AT91C_MCI_MAXLAT )
236 +#define AT91C_SEND_CSD_CMD                                             (9      | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_136          | AT91C_MCI_MAXLAT )
237 +#define AT91C_SEND_CID_CMD                                             (10     | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_136          | AT91C_MCI_MAXLAT )
238 +#define AT91C_MMC_READ_DAT_UNTIL_STOP_CMD              (11     | AT91C_MCI_TRTYP_STREAM| AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRDIR       | AT91C_MCI_TRCMD_START | AT91C_MCI_MAXLAT )
239 +
240 +#define AT91C_STOP_TRANSMISSION_CMD                            (12     | AT91C_MCI_TRCMD_STOP  | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_48           | AT91C_MCI_MAXLAT )
241 +#define AT91C_STOP_TRANSMISSION_SYNC_CMD               (12     | AT91C_MCI_TRCMD_STOP  | AT91C_MCI_SPCMD_SYNC  | AT91C_MCI_RSPTYP_48           | AT91C_MCI_MAXLAT )
242 +#define AT91C_SEND_STATUS_CMD                                  (13     | AT91C_MCI_TRCMD_NO    | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_48           | AT91C_MCI_MAXLAT )
243 +#define AT91C_GO_INACTIVE_STATE_CMD                            (15     | AT91C_MCI_RSPTYP_NO )
244 +
245 +//*------------------------------------------------
246 +//* Class 2 commands: Block oriented Read commands
247 +//*------------------------------------------------
248 +
249 +#define AT91C_SET_BLOCKLEN_CMD                                 (16 | AT91C_MCI_TRCMD_NO        | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_48           | AT91C_MCI_MAXLAT )
250 +#define AT91C_READ_SINGLE_BLOCK_CMD                            (17 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_START | AT91C_MCI_TRTYP_BLOCK | AT91C_MCI_TRDIR       | AT91C_MCI_MAXLAT)
251 +#define AT91C_READ_MULTIPLE_BLOCK_CMD                  (18 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_START | AT91C_MCI_TRTYP_MULTIPLE      | AT91C_MCI_TRDIR       | AT91C_MCI_MAXLAT)
252 +
253 +//*--------------------------------------------
254 +//* Class 3 commands: Sequential write commands
255 +//*--------------------------------------------
256 +
257 +#define AT91C_MMC_WRITE_DAT_UNTIL_STOP_CMD             (20 | AT91C_MCI_TRTYP_STREAM| AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48 & ~(AT91C_MCI_TRDIR) | AT91C_MCI_TRCMD_START | AT91C_MCI_MAXLAT ) // MMC
258 +
259 +//*------------------------------------------------
260 +//* Class 4 commands: Block oriented write commands
261 +//*------------------------------------------------
262 +       
263 +#define AT91C_WRITE_BLOCK_CMD                                  (24 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_START | (AT91C_MCI_TRTYP_BLOCK        &  ~(AT91C_MCI_TRDIR))  | AT91C_MCI_MAXLAT)
264 +#define AT91C_WRITE_MULTIPLE_BLOCK_CMD                 (25 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_START | (AT91C_MCI_TRTYP_MULTIPLE     &  ~(AT91C_MCI_TRDIR))  | AT91C_MCI_MAXLAT)
265 +#define AT91C_PROGRAM_CSD_CMD                                  (27 | AT91C_MCI_RSPTYP_48 )
266 +
267 +
268 +//*----------------------------------------
269 +//* Class 6 commands: Group Write protect
270 +//*----------------------------------------
271 +
272 +#define AT91C_SET_WRITE_PROT_CMD                               (28     | AT91C_MCI_RSPTYP_48 )
273 +#define AT91C_CLR_WRITE_PROT_CMD                               (29     | AT91C_MCI_RSPTYP_48 )
274 +#define AT91C_SEND_WRITE_PROT_CMD                              (30     | AT91C_MCI_RSPTYP_48 )
275 +
276 +
277 +//*----------------------------------------
278 +//* Class 5 commands: Erase commands
279 +//*----------------------------------------
280 +
281 +#define AT91C_TAG_SECTOR_START_CMD                             (32 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO    | AT91C_MCI_MAXLAT)
282 +#define AT91C_TAG_SECTOR_END_CMD                               (33 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO    | AT91C_MCI_MAXLAT)
283 +#define AT91C_MMC_UNTAG_SECTOR_CMD                             (34 | AT91C_MCI_RSPTYP_48 )
284 +#define AT91C_MMC_TAG_ERASE_GROUP_START_CMD            (35 | AT91C_MCI_RSPTYP_48 )
285 +#define AT91C_MMC_TAG_ERASE_GROUP_END_CMD              (36 | AT91C_MCI_RSPTYP_48 )
286 +#define AT91C_MMC_UNTAG_ERASE_GROUP_CMD                        (37 | AT91C_MCI_RSPTYP_48 )
287 +#define AT91C_ERASE_CMD                                                        (38 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO    | AT91C_MCI_MAXLAT )
288 +
289 +//*----------------------------------------
290 +//* Class 7 commands: Lock commands
291 +//*----------------------------------------
292 +
293 +#define AT91C_LOCK_UNLOCK                                              (42 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO    | AT91C_MCI_MAXLAT)     // no tested
294 +
295 +//*-----------------------------------------------
296 +// Class 8 commands: Application specific commands
297 +//*-----------------------------------------------
298 +
299 +#define AT91C_APP_CMD                                                  (55 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO | AT91C_MCI_MAXLAT)
300 +#define AT91C_GEN_CMD                                                  (56 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO | AT91C_MCI_MAXLAT)        // no tested
301 +
302 +#define AT91C_SDCARD_SET_BUS_WIDTH_CMD                 (6      | AT91C_MCI_SPCMD_NONE  | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO    | AT91C_MCI_MAXLAT)
303 +#define AT91C_SDCARD_STATUS_CMD                                        (13 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO    | AT91C_MCI_MAXLAT)
304 +#define AT91C_SDCARD_SEND_NUM_WR_BLOCKS_CMD            (22 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO    | AT91C_MCI_MAXLAT)
305 +#define AT91C_SDCARD_SET_WR_BLK_ERASE_COUNT_CMD        (23 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO    | AT91C_MCI_MAXLAT)
306 +#define AT91C_SDCARD_APP_OP_COND_CMD                   (41 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO )
307 +#define AT91C_SDCARD_SET_CLR_CARD_DETECT_CMD   (42 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO    | AT91C_MCI_MAXLAT)
308 +#define AT91C_SDCARD_SEND_SCR_CMD                              (51 | AT91C_MCI_SPCMD_NONE      | AT91C_MCI_RSPTYP_48   | AT91C_MCI_TRCMD_NO    | AT91C_MCI_MAXLAT)
309 +
310 +#define AT91C_SDCARD_APP_ALL_CMD                               (AT91C_SDCARD_SET_BUS_WIDTH_CMD +\
311 +                                                                                               AT91C_SDCARD_STATUS_CMD +\
312 +                                                                                               AT91C_SDCARD_SEND_NUM_WR_BLOCKS_CMD +\
313 +                                                                                               AT91C_SDCARD_SET_WR_BLK_ERASE_COUNT_CMD +\
314 +                                                                                               AT91C_SDCARD_APP_OP_COND_CMD +\
315 +                                                                                               AT91C_SDCARD_SET_CLR_CARD_DETECT_CMD +\
316 +                                                                                               AT91C_SDCARD_SEND_SCR_CMD)
317 +
318 +//*----------------------------------------
319 +//* Class 9 commands: IO Mode commands
320 +//*----------------------------------------
321 +
322 +#define AT91C_MMC_FAST_IO_CMD                                  (39 | AT91C_MCI_SPCMD_NONE | AT91C_MCI_RSPTYP_48 | AT91C_MCI_MAXLAT)
323 +#define AT91C_MMC_GO_IRQ_STATE_CMD                             (40 | AT91C_MCI_SPCMD_NONE | AT91C_MCI_RSPTYP_48 | AT91C_MCI_TRCMD_NO   | AT91C_MCI_MAXLAT)
324 +
325 +/////////////////////////////////////////////////////////////////////////////////////////////////////
326 +// Functions returnals
327 +/////////////////////////////////////////////////////////////////////////////////////////////////////
328 +#define AT91C_CMD_SEND_OK                                      0               // Command ok
329 +#define AT91C_CMD_SEND_ERROR                           -1              // Command failed
330 +#define AT91C_INIT_OK                                          2               // Init Successfull
331 +#define AT91C_INIT_ERROR                                       3               // Init Failed
332 +#define AT91C_READ_OK                                          4               // Read Successfull
333 +#define AT91C_READ_ERROR                                       5               // Read Failed
334 +#define AT91C_WRITE_OK                                         6               // Write Successfull
335 +#define AT91C_WRITE_ERROR                                      7               // Write Failed
336 +#define AT91C_ERASE_OK                                         8               // Erase Successfull
337 +#define AT91C_ERASE_ERROR                                      9               // Erase Failed
338 +#define AT91C_CARD_SELECTED_OK                         10              // Card Selection Successfull
339 +#define AT91C_CARD_SELECTED_ERROR                      11              // Card Selection Failed
340 +
341 +/////////////////////////////////////////////////////////////////////////////////////////////////////
342 +// MCI_SR Errors
343 +/////////////////////////////////////////////////////////////////////////////////////////////////////
344 +#define        AT91C_MCI_SR_ERROR              (AT91C_MCI_UNRE |\
345 +                                                                        AT91C_MCI_OVRE |\
346 +                                                                        AT91C_MCI_DTOE |\
347 +                                                                        AT91C_MCI_DCRCE |\
348 +                                                                        AT91C_MCI_RTOE |\
349 +                                                                        AT91C_MCI_RENDE |\
350 +                                                                        AT91C_MCI_RCRCE |\
351 +                                                                        AT91C_MCI_RDIRE |\
352 +                                                                        AT91C_MCI_RINDE)
353 +
354 +////////////////////////////////////////////////////////////////////////////////////////////////////
355 +// OCR Register
356 +////////////////////////////////////////////////////////////////////////////////////////////////////
357 +#define AT91C_VDD_16_17                                        (1 << 4)
358 +#define AT91C_VDD_17_18                                        (1 << 5)
359 +#define AT91C_VDD_18_19                                        (1 << 6)
360 +#define AT91C_VDD_19_20                                        (1 << 7)
361 +#define AT91C_VDD_20_21                                        (1 << 8)
362 +#define AT91C_VDD_21_22                                        (1 << 9)
363 +#define AT91C_VDD_22_23                                        (1 << 10)
364 +#define AT91C_VDD_23_24                                        (1 << 11)
365 +#define AT91C_VDD_24_25                                        (1 << 12)
366 +#define AT91C_VDD_25_26                                        (1 << 13)
367 +#define AT91C_VDD_26_27                                        (1 << 14)
368 +#define AT91C_VDD_27_28                                        (1 << 15)
369 +#define AT91C_VDD_28_29                                        (1 << 16)
370 +#define AT91C_VDD_29_30                                        (1 << 17)
371 +#define AT91C_VDD_30_31                                        (1 << 18)
372 +#define AT91C_VDD_31_32                                        (1 << 19)
373 +#define AT91C_VDD_32_33                                        (1 << 20)
374 +#define AT91C_VDD_33_34                                        (1 << 21)
375 +#define AT91C_VDD_34_35                                        (1 << 22)
376 +#define AT91C_VDD_35_36                                        (1 << 23)
377 +#define AT91C_CARD_POWER_UP_BUSY               (1 << 31)
378 +
379 +#define AT91C_MMC_HOST_VOLTAGE_RANGE   (AT91C_VDD_27_28 +\
380 +                                                                               AT91C_VDD_28_29 +\
381 +                                                                               AT91C_VDD_29_30 +\
382 +                                                                               AT91C_VDD_30_31 +\
383 +                                                                               AT91C_VDD_31_32 +\
384 +                                                                               AT91C_VDD_32_33)
385 +
386 +////////////////////////////////////////////////////////////////////////////////////////////////////
387 +// CURRENT_STATE & READY_FOR_DATA in SDCard Status Register definition (response type R1)
388 +////////////////////////////////////////////////////////////////////////////////////////////////////
389 +#define AT91C_SR_READY_FOR_DATA                                (1 << 8)        // corresponds to buffer empty signalling on the bus
390 +#define AT91C_SR_IDLE                                          (0 << 9)
391 +#define AT91C_SR_READY                                         (1 << 9)
392 +#define AT91C_SR_IDENT                                         (2 << 9)
393 +#define AT91C_SR_STBY                                          (3 << 9)
394 +#define AT91C_SR_TRAN                                          (4 << 9)
395 +#define AT91C_SR_DATA                                          (5 << 9)
396 +#define AT91C_SR_RCV                                           (6 << 9)
397 +#define AT91C_SR_PRG                                           (7 << 9)
398 +#define AT91C_SR_DIS                                           (8 << 9)
399 +
400 +#define AT91C_SR_CARD_SELECTED                         (AT91C_SR_READY_FOR_DATA + AT91C_SR_TRAN)
401 +
402 +/////////////////////////////////////////////////////////////////////////////////////////////////////
403 +// MMC CSD register header File                                        
404 +// AT91C_CSD_xxx_S     for shift value
405 +// AT91C_CSD_xxx_M     for mask  value
406 +/////////////////////////////////////////////////////////////////////////////////////////////////////
407 +
408 +// First Response INT <=> CSD[3] : bits 0 to 31
409 +#define        AT91C_CSD_BIT0_S                        0               // [0:0]                        
410 +#define        AT91C_CSD_BIT0_M                        0x01                            
411 +#define        AT91C_CSD_CRC_S                         1               // [7:1]
412 +#define        AT91C_CSD_CRC_M                         0x7F
413 +#define        AT91C_CSD_MMC_ECC_S                     8               // [9:8]                reserved for MMC compatibility
414 +#define        AT91C_CSD_MMC_ECC_M                     0x03
415 +#define        AT91C_CSD_FILE_FMT_S            10              // [11:10]
416 +#define        AT91C_CSD_FILE_FMT_M            0x03
417 +#define        AT91C_CSD_TMP_WP_S                      12              // [12:12]
418 +#define        AT91C_CSD_TMP_WP_M                      0x01
419 +#define        AT91C_CSD_PERM_WP_S             13              // [13:13]
420 +#define        AT91C_CSD_PERM_WP_M             0x01
421 +#define        AT91C_CSD_COPY_S                        14              // [14:14]
422 +#define        AT91C_CSD_COPY_M                        0x01
423 +#define        AT91C_CSD_FILE_FMT_GRP_S        15              // [15:15]
424 +#define        AT91C_CSD_FILE_FMT_GRP_M        0x01
425 +//     reserved                                                16              // [20:16]
426 +//     reserved                                                0x1F
427 +#define        AT91C_CSD_WBLOCK_P_S            21              // [21:21]
428 +#define        AT91C_CSD_WBLOCK_P_M            0x01
429 +#define        AT91C_CSD_WBLEN_S                       22              // [25:22]
430 +#define        AT91C_CSD_WBLEN_M                       0x0F
431 +#define        AT91C_CSD_R2W_F_S                       26              // [28:26]
432 +#define        AT91C_CSD_R2W_F_M                       0x07
433 +#define        AT91C_CSD_MMC_DEF_ECC_S         29              // [30:29]              reserved for MMC compatibility
434 +#define        AT91C_CSD_MMC_DEF_ECC_M         0x03
435 +#define        AT91C_CSD_WP_GRP_EN_S           31              // [31:31]
436 +#define        AT91C_CSD_WP_GRP_EN_M           0x01
437 +
438 +// Seconde Response INT <=> CSD[2] : bits 32 to 63
439 +#define        AT91C_CSD_v21_WP_GRP_SIZE_S     0               // [38:32]                              
440 +#define        AT91C_CSD_v21_WP_GRP_SIZE_M     0x7F                            
441 +#define        AT91C_CSD_v21_SECT_SIZE_S       7               // [45:39]
442 +#define        AT91C_CSD_v21_SECT_SIZE_M       0x7F
443 +#define        AT91C_CSD_v21_ER_BLEN_EN_S      14              // [46:46]
444 +#define        AT91C_CSD_v21_ER_BLEN_EN_M      0x01
445 +
446 +#define        AT91C_CSD_v22_WP_GRP_SIZE_S     0               // [36:32]                              
447 +#define        AT91C_CSD_v22_WP_GRP_SIZE_M     0x1F                            
448 +#define        AT91C_CSD_v22_ER_GRP_SIZE_S     5               // [41:37]
449 +#define        AT91C_CSD_v22_ER_GRP_SIZE_M     0x1F
450 +#define        AT91C_CSD_v22_SECT_SIZE_S       10              // [46:42]
451 +#define        AT91C_CSD_v22_SECT_SIZE_M       0x1F
452 +
453 +#define        AT91C_CSD_C_SIZE_M_S            15              // [49:47]
454 +#define        AT91C_CSD_C_SIZE_M_M            0x07
455 +#define        AT91C_CSD_VDD_WMAX_S            18              // [52:50]
456 +#define        AT91C_CSD_VDD_WMAX_M            0x07
457 +#define        AT91C_CSD_VDD_WMIN_S            21              // [55:53]
458 +#define        AT91C_CSD_VDD_WMIN_M            0x07
459 +#define        AT91C_CSD_RCUR_MAX_S            24              // [58:56]
460 +#define        AT91C_CSD_RCUR_MAX_M            0x07
461 +#define        AT91C_CSD_RCUR_MIN_S            27              // [61:59]
462 +#define        AT91C_CSD_RCUR_MIN_M            0x07
463 +#define        AT91C_CSD_CSIZE_L_S             30              // [63:62] <=> 2 LSB of CSIZE
464 +#define        AT91C_CSD_CSIZE_L_M             0x03
465 +
466 +// Third Response INT <=> CSD[1] : bits 64 to 95
467 +#define        AT91C_CSD_CSIZE_H_S             0               // [73:64]      <=> 10 MSB of CSIZE
468 +#define        AT91C_CSD_CSIZE_H_M             0x03FF
469 +// reserved                                                    10              // [75:74]
470 +// reserved                                                    0x03            
471 +#define        AT91C_CSD_DSR_I_S                       12              // [76:76]
472 +#define        AT91C_CSD_DSR_I_M                       0x01
473 +#define        AT91C_CSD_RD_B_MIS_S            13              // [77:77]
474 +#define        AT91C_CSD_RD_B_MIS_M            0x01
475 +#define        AT91C_CSD_WR_B_MIS_S            14              // [78:78]
476 +#define        AT91C_CSD_WR_B_MIS_M            0x01
477 +#define        AT91C_CSD_RD_B_PAR_S            15              // [79:79]
478 +#define        AT91C_CSD_RD_B_PAR_M            0x01
479 +#define        AT91C_CSD_RD_B_LEN_S            16              // [83:80]
480 +#define        AT91C_CSD_RD_B_LEN_M            0x0F
481 +#define        AT91C_CSD_CCC_S                         20              // [95:84]
482 +#define        AT91C_CSD_CCC_M                         0x0FFF
483 +
484 +// Fourth Response INT <=> CSD[0] : bits 96 to 127
485 +#define        AT91C_CSD_TRANS_SPEED_S         0               // [103:96]
486 +#define        AT91C_CSD_TRANS_SPEED_M         0xFF
487 +#define        AT91C_CSD_NSAC_S                        8               // [111:104]
488 +#define        AT91C_CSD_NSAC_M                        0xFF
489 +#define        AT91C_CSD_TAAC_S                        16              // [119:112]
490 +#define        AT91C_CSD_TAAC_M                        0xFF
491 +//     reserved                                                24              // [121:120]
492 +//     reserved                                                0x03
493 +#define        AT91C_CSD_MMC_SPEC_VERS_S       26              // [125:122]    reserved for MMC compatibility
494 +#define        AT91C_CSD_MMC_SPEC_VERS_M       0x0F
495 +#define        AT91C_CSD_STRUCT_S                      30              // [127:126]
496 +#define        AT91C_CSD_STRUCT_M                      0x03
497 +
498 +/////////////////////////////////////////////////////////////////////////////////////////////////////
499 +
500 +#endif
501 +
502 diff -urN romboot.old/init.cpp romboot/init.cpp
503 --- romboot.old/init.cpp        2004-07-06 13:01:55.000000000 +0200
504 +++ romboot/init.cpp    2007-03-21 12:43:39.000000000 +0100
505 @@ -35,7 +35,7 @@
506  //*----------------------------------------------------------------------------
507  void AT91F_SpuriousHandler() 
508  {
509 -       AT91F_DBGU_Printk("-F- Spurious Interrupt detected\n\r");
510 +       AT91F_DBGU_Printk("ISI");
511         while (1);
512  }
513  
514 @@ -46,7 +46,7 @@
515  //*----------------------------------------------------------------------------
516  void AT91F_DataAbort() 
517  {
518 -       AT91F_DBGU_Printk("-F- Data Abort detected\n\r");
519 +       AT91F_DBGU_Printk("IDA");
520         while (1);
521  }
522  
523 @@ -56,7 +56,7 @@
524  //*----------------------------------------------------------------------------
525  void AT91F_FetchAbort()
526  {
527 -       AT91F_DBGU_Printk("-F- Prefetch Abort detected\n\r");
528 +       AT91F_DBGU_Printk("IPA");
529         while (1);
530  }
531  
532 @@ -66,7 +66,7 @@
533  //*----------------------------------------------------------------------------
534  void AT91F_Undef() 
535  {
536 -       AT91F_DBGU_Printk("-F- Undef detected\n\r");
537 +       AT91F_DBGU_Printk("IUD");
538         while (1);
539  }
540  
541 @@ -76,7 +76,7 @@
542  //*----------------------------------------------------------------------------
543  void AT91F_UndefHandler() 
544  {
545 -       AT91F_DBGU_Printk("-F- Undef detected\n\r");
546 +       AT91F_DBGU_Printk("IUD");
547         while (1);
548  }
549  
550 diff -urN romboot.old/main.cpp romboot/main.cpp
551 --- romboot.old/main.cpp        2007-03-19 12:44:03.000000000 +0100
552 +++ romboot/main.cpp    2007-03-21 19:23:41.000000000 +0100
553 @@ -33,18 +33,22 @@
554  #define DELAY_MAIN_FREQ        1000
555  #define DISP_LINE_LEN 16
556  
557 +#define COMPACT 1
558 +
559  //* prototypes
560  extern void AT91F_DBGU_Printk(char *);
561  extern "C" void AT91F_ST_ASM_Handler(void);
562  extern "C" void Jump(unsigned int addr);
563 +extern int mci_main(void);
564  
565 -const char *menu_separ = "*----------------------------------------*\n\r";
566 +//const char *menu_separ = "*----------------------------------------*\n\r";
567  
568  const char *menu_dataflash = {
569 -  "1: Download Dataflash [addr]\n\r"
570 -  "2: Read Dataflash [addr]\n\r"
571 -  "3: Start U-BOOT\n\r"
572 -  "4: Clear bootloader section in Dataflash\n\r"
573 +  "1: DL DF [ad]\n\r"
574 +  "2: RD DF [ad]\n\r"
575 +       "3: CP SD\n\r"
576 +  "4: U-BOOT\n\r"
577 +  "5: RM BL in DF\n\r"
578  };
579  
580  //* Globales variables 
581 @@ -151,12 +155,12 @@
582  //*-----------------------------------------------------------------------------
583  void AT91F_DisplayMenu(void)
584  {
585 -  printf("\n\rFDL LOADER %s %s %s\n\r", AT91C_VERSION, __DATE__, __TIME__);
586 -  printf(menu_separ);  
587 +  printf("\n\rFDL SD-Card LOADER %s %s %s\n\r", AT91C_VERSION, __DATE__, __TIME__);
588 +//  printf(menu_separ);        
589    AT91F_DataflashPrintInfo();
590 -  printf(menu_separ);  
591 +//  printf(menu_separ);        
592    printf(menu_dataflash);                      
593 -  printf(menu_separ);  
594 +//  printf(menu_separ);        
595  }      
596  
597  //*-----------------------------------------------------------------------------
598 @@ -194,6 +198,7 @@
599  }
600  
601  
602 +#ifndef COMPACT
603  //*-----------------------------------------------------------------------------
604  //* Function Name       : AT91F_MemoryDisplay()
605  //* Object              : Display the content of the dataflash
606 @@ -244,7 +249,7 @@
607      } while (nbytes > 0);
608    return 0;
609  }
610 -
611 +#endif
612  
613  //*--------------------------------------------------------------------------------------
614  //* Function Name       : AT91F_SetPLL
615 @@ -306,7 +311,7 @@
616    AT91F_SetPLL();
617  }
618  
619 -void LedCode(void)
620 +/*void LedCode(void)
621  {
622         int *pRegister;
623         pRegister = (int *)0xFFFFF800; // Enable port C peripheral reg
624 @@ -318,15 +323,16 @@
625          pRegister = (int *)0xFFFFF834; // Clear bits
626          *pRegister = 0x2800;
627  }
628 +*/
629  
630  void AT91F_StartUboot(unsigned int dummy, void *pvoid)
631  {
632 -  printf("Load U-BOOT from dataflash[%x] to SDRAM[%x]\n\r", AT91C_UBOOT_DATAFLASH_ADDR, AT91C_UBOOT_ADDR);
633 +  //printf("Load U-BOOT from dataflash[%x] to SDRAM[%x]\n\r", AT91C_UBOOT_DATAFLASH_ADDR, AT91C_UBOOT_ADDR);
634    read_dataflash(AT91C_UBOOT_DATAFLASH_ADDR, AT91C_UBOOT_SIZE, (char *)(AT91C_UBOOT_ADDR));
635 -  printf("Set PLLA to 180Mhz and Master clock to 60Mhz and start U-BOOT\n\r");
636 +  //printf("Set PLLA to 180Mhz and Master clock to 60Mhz and start U-BOOT\n\r");
637    //* Reset registers
638    AT91F_ResetRegisters();
639 -  LedCode();
640 +//  LedCode();
641    Jump(AT91C_UBOOT_ADDR);
642    while(1);
643  }
644 @@ -385,120 +391,124 @@
645    // start tempo to start Uboot in a delay of 1 sec if no key pressed
646    svcUbootTempo.Start(&svcUbootTempo, 1000, 0, AT91F_StartUboot, (void *)0);
647  
648 -  printf("press any key to enter bootloader\n\r");
649 +  printf("press key\n\r");
650    getc();
651  
652    // stop tempo
653    svcUbootTempo.Stop(&svcUbootTempo);
654    
655 -  while(1)
656 -    {
657 -      while(command == 0)
658 -       {
659 -         AddressToDownload = AT91C_DOWNLOAD_BASE_ADDRESS;
660 -         SizeToDownload = AT91C_DOWNLOAD_MAX_SIZE;
661 -         DeviceAddress = 0;
662 +       while(1) {
663 +               while(command == 0) {
664 +                       AddressToDownload = AT91C_DOWNLOAD_BASE_ADDRESS;
665 +                       SizeToDownload = AT91C_DOWNLOAD_MAX_SIZE;
666 +                       DeviceAddress = 0;
667           
668 -         AT91F_DisplayMenu();
669 -         message[0] = 0;
670 -         message[2] = 0;
671 -         AT91F_ReadLine("Enter: ", message);
672 +                       AT91F_DisplayMenu();
673 +                       message[0] = 0;
674 +                       message[2] = 0;
675 +                       AT91F_ReadLine("Enter: ", message);
676           
677 -         command = message[0];
678 -         if(command == '1' || command == '2')
679 -           if(AsciiToHex(&message[2], &DeviceAddress) == 0)
680 -             command = 0;
681 -
682 -         switch(command)
683 -           {
684 -           case '1':                                   
685 -             printf("Download Dataflash [0x%x]\n\r", DeviceAddress);
686 -             
687 -             switch(DeviceAddress & 0xFF000000)
688 -               {
689 -               case CFG_DATAFLASH_LOGIC_ADDR_CS0:
690 -                 device = 0;
691 -                 break;
692 +                       command = message[0];
693 +                       if(command == '1' || command == '2')
694 +                       if(AsciiToHex(&message[2], &DeviceAddress) == 0)
695 +                       command = 0;
696 +
697 +                       switch(command) {
698 +                       case '1':                                       
699 +                       printf("DL DF [0x%x]\n\r", DeviceAddress);
700 +
701 +               switch(DeviceAddress & 0xFF000000) {
702 +                                               case CFG_DATAFLASH_LOGIC_ADDR_CS0:
703 +                                               device = 0;
704 +                                               break;
705                   
706 -               case CFG_DATAFLASH_LOGIC_ADDR_CS3:
707 -                 device = 1;
708 -                 break;
709 +                                               case CFG_DATAFLASH_LOGIC_ADDR_CS3:
710 +                                               device = 1;
711 +                                               break;
712                   
713 -               default:
714 -                 command = 0;
715 -                 break;
716 -               }
717 -             break;
718 -             
719 -           case '2':
720 -             do 
721 -               {
722 -                 AT91F_MemoryDisplay(DeviceAddress, 4, 64);
723 -                 AT91F_ReadLine ((char *)0, message);
724 -                 DeviceAddress += 0x100;
725 +                                               default:
726 +                                               command = 0;
727 +                                               break;
728 +                                               }
729 +               break;
730 +
731 +#ifndef COMPACT
732 +               case '2':
733 +               do {
734 +                                       AT91F_MemoryDisplay(DeviceAddress, 4, 64);
735 +                                       AT91F_ReadLine ((char *)0, message);
736 +                                       DeviceAddress += 0x100;
737 +                                       } while(message[0] == '\0');
738 +               command = 0;
739 +               break;
740 +#endif
741 +       
742 +                               case '3':
743 +                                       mci_main();
744 +                                       command=0;
745 +                                       break;
746 +
747 +               case '4':
748 +               AT91F_StartUboot(0, (void *)0);
749 +               command = 0;
750 +               break;
751 +
752 +               case '5':
753 +               {
754 +                                       int *i;
755 +       
756 +                                       for(i = (int *)0x20000000; i < (int *)0x20004000; i++)
757 +                                       *i = 0;
758 +               }
759 +               write_dataflash(0xc0000000, 0x20000000, 0x4000);
760 +               printf("BL CLR\r\n");
761 +               command = 0;
762 +               break;
763 +           
764 +                               default:
765 +               command = 0;
766 +               break;
767 +               } // switch(command)
768 +                       } // while(command == 0)
769 +      
770 +               xmodemPipe.Read(&xmodemPipe, (char *)AddressToDownload, SizeToDownload, XmodemProtocol, 0);     
771 +               while(XmodemComplete !=1);
772 +               SizeToDownload = (unsigned int)(svcXmodem.pData) - (unsigned int)AddressToDownload;     
773 +      
774 +               // Modification of vector 6
775 +               NbPage = 0;
776 +               i = dataflash_info[device].Device.pages_number;
777 +               while(i >>= 1)
778 +                       NbPage++;
779 +               i = (SizeToDownload / 512) + 1 + (NbPage << 13) + (dataflash_info[device].Device.pages_size << 17);
780 +               *(int *)(AddressToDownload + AT91C_OFFSET_VECT6) = i;
781 +      
782 +//             printf("\n\rModification of Arm Vector 6 :%x\n\r", i);
783 +
784 +               printf("\n\rWR %d in DF [0x%x]\n\r",SizeToDownload, DeviceAddress);
785 +               crc1 = 0;
786 +               pAT91->CRC32((const unsigned char *)AddressToDownload, SizeToDownload , &crc1);
787 +
788 +               // write the dataflash
789 +               write_dataflash (DeviceAddress, AddressToDownload, SizeToDownload);
790 +               // clear the buffer before read
791 +               for(i=0; i < SizeToDownload; i++)
792 +                       *(unsigned char *)(AddressToDownload + i) = 0;
793 +      
794 +               //* Read dataflash page in TestBuffer
795 +               read_dataflash (DeviceAddress, SizeToDownload, (char *)(AddressToDownload));
796 +
797 +               printf("Vfy DF: ");     
798 +               crc2 = 0;
799 +
800 +               pAT91->CRC32((const unsigned char *)AddressToDownload, SizeToDownload , &crc2);
801 +               if (crc1 != crc2)
802 +                       printf("Fail\r\n");     
803 +               else
804 +                       printf("OK\r\n");       
805 +      
806 +               command = 0;
807 +               XmodemComplete = 0;
808 +               AT91F_WaitKeyPressed();
809                 }
810 -             while(message[0] == '\0');
811 -             command = 0;
812 -             break;
813 -             
814 -           case '3':
815 -             AT91F_StartUboot(0, (void *)0);
816 -             command = 0;
817 -             break;
818 -           case '4':
819 -             {
820 -               int *i;
821 -               for(i = (int *)0x20000000; i < (int *)0x20004000; i++)
822 -                 *i = 0;
823 -             }
824 -             write_dataflash(0xc0000000, 0x20000000, 0x4000);
825 -             printf("Bootsection cleared\r\n");
826 -             command = 0;
827 -             break;
828 -           default:
829 -             command = 0;
830 -             break;
831 -           }
832         }
833 -      
834 -      xmodemPipe.Read(&xmodemPipe, (char *)AddressToDownload, SizeToDownload, XmodemProtocol, 0);      
835 -      while(XmodemComplete !=1);
836 -      SizeToDownload = (unsigned int)(svcXmodem.pData) - (unsigned int)AddressToDownload;      
837 -      
838 -      // Modification of vector 6
839 -      NbPage = 0;
840 -      i = dataflash_info[device].Device.pages_number;
841 -      while(i >>= 1)
842 -       NbPage++;
843 -      i = (SizeToDownload / 512) + 1 + (NbPage << 13) + (dataflash_info[device].Device.pages_size << 17);
844 -      *(int *)(AddressToDownload + AT91C_OFFSET_VECT6) = i;
845 -      
846 -      printf("\n\rModification of Arm Vector 6 :%x\n\r", i);
847 -      
848 -      printf("\n\rWrite %d bytes in DataFlash [0x%x]\n\r",SizeToDownload, DeviceAddress);
849 -      crc1 = 0;
850 -      pAT91->CRC32((const unsigned char *)AddressToDownload, SizeToDownload , &crc1);
851 -      
852 -      // write the dataflash
853 -      write_dataflash (DeviceAddress, AddressToDownload, SizeToDownload);
854 -      // clear the buffer before read
855 -      for(i=0; i < SizeToDownload; i++)
856 -       *(unsigned char *)(AddressToDownload + i) = 0;
857 -      
858 -      //* Read dataflash page in TestBuffer
859 -      read_dataflash (DeviceAddress, SizeToDownload, (char *)(AddressToDownload));
860 -      
861 -      printf("Verify Dataflash: ");    
862 -      crc2 = 0;
863 -      
864 -      pAT91->CRC32((const unsigned char *)AddressToDownload, SizeToDownload , &crc2);
865 -      if (crc1 != crc2)
866 -       printf("Failed\r\n");   
867 -      else
868 -       printf("OK\r\n");       
869 -      
870 -      command = 0;
871 -      XmodemComplete = 0;
872 -      AT91F_WaitKeyPressed();
873 -    }
874 -}
875 diff -urN romboot.old/main.h romboot/main.h
876 --- romboot.old/main.h  2004-07-03 17:41:14.000000000 +0200
877 +++ romboot/main.h      2007-03-21 21:48:52.000000000 +0100
878 @@ -27,7 +27,7 @@
879  
880  #define AT91C_OFFSET_VECT6              0x14        //* Offset for ARM vector 6
881  
882 -#define AT91C_VERSION   "VER 1.01"
883 +#define AT91C_VERSION   "VER 1.02"
884  // Global variables and functions definition
885  extern unsigned int GetTickCount(void);
886  #endif
887 diff -urN romboot.old/Makefile romboot/Makefile
888 --- romboot.old/Makefile        2007-03-19 12:44:03.000000000 +0100
889 +++ romboot/Makefile    2007-03-21 12:29:11.000000000 +0100
890 @@ -1,8 +1,8 @@
891  LINKFLAGS= -T elf32-littlearm.lds -Ttext 0
892  COMPILEFLAGS= -Os
893  TARGET=romboot
894 -OBJFILES=cstartup_ram.o asm_isr.o jump.o at45.o com.o dataflash.o \
895 -       init.o main.o stdio.o _udivsi3.o _umodsi3.o div0.o
896 +OBJFILES=cstartup_ram.o asm_isr.o asm_mci_isr.o jump.o at45.o com.o dataflash.o \
897 +       mci_device.o mci_main.o init.o main.o stdio.o _udivsi3.o _umodsi3.o div0.o
898  LIBRARIES=
899  INCLUDES= -Iinclude
900  
901 @@ -11,10 +11,15 @@
902  $(TARGET): $(OBJFILES)
903         $(LD) $(OBJFILES) -o $(TARGET).out $(LINKFLAGS) -n
904         $(OBJCOPY) $(TARGET).out -O binary $(TARGET).bin
905 +       $(OBJDUMP) -h -s $(TARGET).out > $(TARGET).lss
906 +       $(NM) -n $(TARGET).out | grep -v '\( [aUw] \)\|\(__crc_\)\|\( \$[adt]\)' > $(TARGET).map
907  
908  asm_isr.o: asm_isr.S
909         $(CC) -c -Iinclude -o $@ $<
910  
911 +asm_mci_isr.o: asm_mci_isr.S
912 +       $(CC) -c -Iinclude -o $@ $<
913 +
914  cstartup_ram.o: cstartup_ram.S
915         $(CC) -c -Iinclude -o $@ $<
916  
917 diff -urN romboot.old/mci_device.cpp romboot/mci_device.cpp
918 --- romboot.old/mci_device.cpp  1970-01-01 01:00:00.000000000 +0100
919 +++ romboot/mci_device.cpp      2007-03-22 18:52:48.000000000 +0100
920 @@ -0,0 +1,581 @@
921 +//*----------------------------------------------------------------------------
922 +//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
923 +//*----------------------------------------------------------------------------
924 +//* The software is delivered "AS IS" without warranty or condition of any
925 +//* kind, either express, implied or statutory. This includes without
926 +//* limitation any warranty or condition with respect to merchantability or
927 +//* fitness for any particular purpose, or against the infringements of
928 +//* intellectual property rights of others.
929 +//*----------------------------------------------------------------------------
930 +//* File Name           : mci_device.c
931 +//* Object              : TEST DataFlash Functions
932 +//* Creation            : FB   26/11/2002
933 +//*
934 +//*----------------------------------------------------------------------------
935 +
936 +#include <AT91C_MCI_Device.h>
937 +#include "com.h"
938 +
939 +#define ENABLE_WRITE 1
940 +#undef MMC
941 +
942 +//*----------------------------------------------------------------------------
943 +//* \fn    AT91F_MCI_SendCommand
944 +//* \brief Generic function to send a command to the MMC or SDCard
945 +//*----------------------------------------------------------------------------
946 +AT91S_MCIDeviceStatus AT91F_MCI_SendCommand (
947 +       AT91PS_MciDevice pMCI_Device,
948 +       unsigned int Cmd,
949 +       unsigned int Arg)
950 +{
951 +       unsigned int    error,status;
952 +       //unsigned int  tick=0;
953 +
954 +    // Send the command
955 +    AT91C_BASE_MCI->MCI_ARGR = Arg;
956 +    AT91C_BASE_MCI->MCI_CMDR = Cmd;
957 +
958 +       // wait for CMDRDY Status flag to read the response
959 +       do
960 +       {
961 +               status = AT91C_BASE_MCI->MCI_SR;
962 +               //tick++;
963 +       }
964 +       while( !(status & AT91C_MCI_CMDRDY) );//&& (tick<100) );
965 +
966 +    // Test error  ==> if crc error and response R3 ==> don't check error
967 +    error = (AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR;
968 +       if(error != 0 )
969 +       {
970 +               // if the command is SEND_OP_COND the CRC error flag is always present (cf : R3 response)
971 +               if ( (Cmd != AT91C_SDCARD_APP_OP_COND_CMD) && (Cmd != AT91C_MMC_SEND_OP_COND_CMD) )
972 +                       return ((AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR);
973 +               else
974 +               {
975 +                       if (error != AT91C_MCI_RCRCE)
976 +                               return ((AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR);
977 +               }
978 +       }
979 +    return AT91C_CMD_SEND_OK;
980 +}
981 +
982 +//*----------------------------------------------------------------------------
983 +//* \fn    AT91F_MCI_SDCard_SendAppCommand
984 +//* \brief Specific function to send a specific command to the SDCard
985 +//*----------------------------------------------------------------------------
986 +AT91S_MCIDeviceStatus AT91F_MCI_SDCard_SendAppCommand (
987 +       AT91PS_MciDevice pMCI_Device,
988 +       unsigned int Cmd_App,
989 +       unsigned int Arg        )
990 +{
991 +       unsigned int status;
992 +       //unsigned int  tick=0;
993 +
994 +       // Send the CMD55 for application specific command
995 +    AT91C_BASE_MCI->MCI_ARGR = (pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address << 16 );
996 +    AT91C_BASE_MCI->MCI_CMDR = AT91C_APP_CMD;
997 +
998 +       // wait for CMDRDY Status flag to read the response
999 +       do
1000 +       {
1001 +               status = AT91C_BASE_MCI->MCI_SR;
1002 +               //tick++;
1003 +       }
1004 +       while( !(status & AT91C_MCI_CMDRDY) );//&& (tick<100) );        
1005 +
1006 +    // if an error occurs
1007 +    if (((AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR) != 0 )
1008 +               return ((AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR);
1009 +
1010 +    // check if it is a specific command and then send the command
1011 +       if ( (Cmd_App && AT91C_SDCARD_APP_ALL_CMD) == 0)
1012 +               return AT91C_CMD_SEND_ERROR;
1013 +
1014 +   return( AT91F_MCI_SendCommand(pMCI_Device,Cmd_App,Arg) );
1015 +}
1016 +
1017 +//*----------------------------------------------------------------------------
1018 +//* \fn    AT91F_MCI_GetStatus
1019 +//* \brief Addressed card sends its status register
1020 +//*----------------------------------------------------------------------------
1021 +AT91S_MCIDeviceStatus AT91F_MCI_GetStatus(AT91PS_MciDevice pMCI_Device,unsigned int relative_card_address)
1022 +{
1023 +       if (AT91F_MCI_SendCommand(pMCI_Device,
1024 +                                                               AT91C_SEND_STATUS_CMD,
1025 +                                                               relative_card_address <<16) == AT91C_CMD_SEND_OK)
1026 +       return (AT91C_BASE_MCI->MCI_RSPR[0]);
1027 +
1028 +    return AT91C_CMD_SEND_ERROR;
1029 +}
1030 +
1031 +//*----------------------------------------------------------------------------
1032 +//* \fn    AT91F_MCI_Device_Handler
1033 +//* \brief MCI C interrupt handler
1034 +//*----------------------------------------------------------------------------
1035 +extern "C" void AT91F_MCI_Device_Handler(AT91PS_MciDevice, unsigned int);
1036 +
1037 +void AT91F_MCI_Device_Handler(
1038 +       AT91PS_MciDevice pMCI_Device,
1039 +       unsigned int status)
1040 +{
1041 +       // If End of Tx Buffer Empty interrupt occurred
1042 +       if ( status & AT91C_MCI_TXBUFE )
1043 +    {
1044 +               AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_TXBUFE;
1045 +               AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_TXTDIS;
1046 +               
1047 +               pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_IDLE;
1048 +       }       // End of if AT91C_MCI_TXBUFF           
1049 +       
1050 +    // If End of Rx Buffer Full interrupt occurred
1051 +    if ( status & AT91C_MCI_RXBUFF )
1052 +    {        
1053 +               AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_RXBUFF;
1054 +               AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_RXTDIS;
1055 +       
1056 +               pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_IDLE;
1057 +       }       // End of if AT91C_MCI_RXBUFF
1058 +
1059 +}
1060 +
1061 +//*----------------------------------------------------------------------------
1062 +//* \fn    AT91F_MCI_ReadBlock
1063 +//* \brief Read an ENTIRE block or PARTIAL block
1064 +//*----------------------------------------------------------------------------
1065 +AT91S_MCIDeviceStatus AT91F_MCI_ReadBlock(
1066 +       AT91PS_MciDevice pMCI_Device,
1067 +       int src,
1068 +       unsigned int *dataBuffer,
1069 +       int sizeToRead )
1070 +{
1071 +    ////////////////////////////////////////////////////////////////////////////////////////////
1072 +    if(pMCI_Device->pMCI_DeviceDesc->state != AT91C_MCI_IDLE)
1073 +       return AT91C_READ_ERROR;
1074 +    
1075 +    if( (AT91F_MCI_GetStatus(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address) & AT91C_SR_READY_FOR_DATA) != AT91C_SR_READY_FOR_DATA)
1076 +       return AT91C_READ_ERROR;
1077 +       
1078 +    if ( (src + sizeToRead) > pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity )
1079 +               return AT91C_READ_ERROR;
1080 +
1081 +    // If source does not fit a begin of a block
1082 +       if ( (src % pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length) != 0 )
1083 +               return AT91C_READ_ERROR;
1084 +   
1085 +     // Test if the MMC supports Partial Read Block
1086 +     // ALWAYS SUPPORTED IN SD Memory Card
1087 +     if( (sizeToRead < pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length) 
1088 +       && (pMCI_Device->pMCI_DeviceFeatures->Read_Partial == 0x00) )
1089 +               return AT91C_READ_ERROR;
1090 +               
1091 +    if( sizeToRead > pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length)
1092 +               return AT91C_READ_ERROR;
1093 +    ////////////////////////////////////////////////////////////////////////////////////////////
1094 +      
1095 +    // Init Mode Register
1096 +       AT91C_BASE_MCI->MCI_MR |= ((pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length << 16) | AT91C_MCI_PDCMODE);
1097 +        
1098 +    if (sizeToRead %4)
1099 +               sizeToRead = (sizeToRead /4)+1;
1100 +       else
1101 +               sizeToRead = sizeToRead/4;
1102 +
1103 +       AT91C_BASE_PDC_MCI->PDC_PTCR = (AT91C_PDC_TXTDIS | AT91C_PDC_RXTDIS);
1104 +    AT91C_BASE_PDC_MCI->PDC_RPR  = (unsigned int)dataBuffer;
1105 +    AT91C_BASE_PDC_MCI->PDC_RCR  = sizeToRead;
1106 +
1107 +       // Send the Read single block command
1108 +    if ( AT91F_MCI_SendCommand(pMCI_Device, AT91C_READ_SINGLE_BLOCK_CMD, src) != AT91C_CMD_SEND_OK )
1109 +       return AT91C_READ_ERROR;
1110 +
1111 +       pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_RX_SINGLE_BLOCK;
1112 +
1113 +       // Enable AT91C_MCI_RXBUFF Interrupt
1114 +    AT91C_BASE_MCI->MCI_IER = AT91C_MCI_RXBUFF;
1115 +
1116 +       // (PDC) Receiver Transfer Enable
1117 +       AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_RXTEN;
1118 +       
1119 +       return AT91C_READ_OK;
1120 +}
1121 +
1122 +
1123 +#ifdef ENABLE_WRITE
1124 +//*----------------------------------------------------------------------------
1125 +//* \fn    AT91F_MCI_WriteBlock
1126 +//* \brief  Write an ENTIRE block but not always PARTIAL block !!!
1127 +//*----------------------------------------------------------------------------
1128 +AT91S_MCIDeviceStatus AT91F_MCI_WriteBlock(
1129 +       AT91PS_MciDevice pMCI_Device,
1130 +       int dest,
1131 +       unsigned int *dataBuffer,
1132 +       int sizeToWrite )
1133 +{
1134 +    ////////////////////////////////////////////////////////////////////////////////////////////
1135 +       if( pMCI_Device->pMCI_DeviceDesc->state != AT91C_MCI_IDLE)
1136 +       return AT91C_WRITE_ERROR;
1137 +    
1138 +    if( (AT91F_MCI_GetStatus(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address) & AT91C_SR_READY_FOR_DATA) != AT91C_SR_READY_FOR_DATA)
1139 +       return AT91C_WRITE_ERROR;
1140 +       
1141 +    if ( (dest + sizeToWrite) > pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity )
1142 +               return AT91C_WRITE_ERROR;
1143 +
1144 +    // If source does not fit a begin of a block
1145 +       if ( (dest % pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length) != 0 )
1146 +               return AT91C_WRITE_ERROR;
1147 +   
1148 +    // Test if the MMC supports Partial Write Block 
1149 +    if( (sizeToWrite < pMCI_Device->pMCI_DeviceFeatures->Max_Write_DataBlock_Length) 
1150 +       && (pMCI_Device->pMCI_DeviceFeatures->Write_Partial == 0x00) )
1151 +               return AT91C_WRITE_ERROR;
1152 +               
1153 +       if( sizeToWrite > pMCI_Device->pMCI_DeviceFeatures->Max_Write_DataBlock_Length )
1154 +               return AT91C_WRITE_ERROR;
1155 +    ////////////////////////////////////////////////////////////////////////////////////////////
1156 +  
1157 +    // Init Mode Register
1158 +       AT91C_BASE_MCI->MCI_MR |= ((pMCI_Device->pMCI_DeviceFeatures->Max_Write_DataBlock_Length << 16) | AT91C_MCI_PDCMODE);
1159 +       
1160 +       if (sizeToWrite %4)
1161 +               sizeToWrite = (sizeToWrite /4)+1;
1162 +       else
1163 +               sizeToWrite = sizeToWrite/4;
1164 +
1165 +       // Init PDC for write sequence
1166 +    AT91C_BASE_PDC_MCI->PDC_PTCR = (AT91C_PDC_TXTDIS | AT91C_PDC_RXTDIS);
1167 +    AT91C_BASE_PDC_MCI->PDC_TPR = (unsigned int) dataBuffer;
1168 +    AT91C_BASE_PDC_MCI->PDC_TCR = sizeToWrite;
1169 +
1170 +       // Send the write single block command
1171 +    if ( AT91F_MCI_SendCommand(pMCI_Device, AT91C_WRITE_BLOCK_CMD, dest) != AT91C_CMD_SEND_OK)
1172 +       return AT91C_WRITE_ERROR;
1173 +
1174 +       pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_TX_SINGLE_BLOCK;
1175 +
1176 +       // Enable AT91C_MCI_TXBUFE Interrupt
1177 +    AT91C_BASE_MCI->MCI_IER = AT91C_MCI_TXBUFE;
1178 +  
1179 +       // Enables TX for PDC transfert requests
1180 +    AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_TXTEN;
1181 +  
1182 +       return AT91C_WRITE_OK;
1183 +}
1184 +#endif
1185 +
1186 +#ifdef MMC
1187 +//*------------------------------------------------------------------------------------------------------------
1188 +//* \fn    AT91F_MCI_MMC_SelectCard
1189 +//* \brief Toggles a card between the Stand_by and Transfer states or between Programming and Disconnect states
1190 +//*------------------------------------------------------------------------------------------------------------
1191 +AT91S_MCIDeviceStatus AT91F_MCI_MMC_SelectCard(AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address)
1192 +{
1193 +    int status;
1194 +       
1195 +       //* Check if the MMC card chosen is already the selected one
1196 +       status = AT91F_MCI_GetStatus(pMCI_Device,relative_card_address);
1197 +
1198 +       if (status < 0)
1199 +               return AT91C_CARD_SELECTED_ERROR;
1200 +
1201 +       if ((status & AT91C_SR_CARD_SELECTED) == AT91C_SR_CARD_SELECTED)
1202 +               return AT91C_CARD_SELECTED_OK;
1203 +
1204 +       //* Search for the MMC Card to be selected, status = the Corresponding Device Number
1205 +       status = 0;
1206 +       while( (pMCI_Device->pMCI_DeviceFeatures[status].Relative_Card_Address != relative_card_address)
1207 +                  && (status < AT91C_MAX_MCI_CARDS) )
1208 +               status++;
1209 +
1210 +       if (status > AT91C_MAX_MCI_CARDS)
1211 +       return AT91C_CARD_SELECTED_ERROR;
1212 +
1213 +    if (AT91F_MCI_SendCommand( pMCI_Device,
1214 +                                                                  AT91C_SEL_DESEL_CARD_CMD,
1215 +                                                                  pMCI_Device->pMCI_DeviceFeatures[status].Relative_Card_Address << 16) == AT91C_CMD_SEND_OK)
1216 +       return AT91C_CARD_SELECTED_OK;
1217 +    return AT91C_CARD_SELECTED_ERROR;
1218 +}
1219 +#endif
1220 +
1221 +//*----------------------------------------------------------------------------
1222 +//* \fn    AT91F_MCI_GetCSD
1223 +//* \brief Asks to the specified card to send its CSD
1224 +//*----------------------------------------------------------------------------
1225 +AT91S_MCIDeviceStatus AT91F_MCI_GetCSD (AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address , unsigned int * response)
1226 +{
1227 +       
1228 +       if(AT91F_MCI_SendCommand(pMCI_Device,
1229 +                                                                 AT91C_SEND_CSD_CMD,
1230 +                                                                 (relative_card_address << 16)) != AT91C_CMD_SEND_OK)
1231 +               return AT91C_CMD_SEND_ERROR;
1232 +       
1233 +    response[0] = AT91C_BASE_MCI->MCI_RSPR[0];
1234 +       response[1] = AT91C_BASE_MCI->MCI_RSPR[1];
1235 +    response[2] = AT91C_BASE_MCI->MCI_RSPR[2];
1236 +    response[3] = AT91C_BASE_MCI->MCI_RSPR[3];
1237 +    
1238 +    return AT91C_CMD_SEND_OK;
1239 +}
1240 +
1241 +//*----------------------------------------------------------------------------
1242 +//* \fn    AT91F_MCI_SetBlocklength
1243 +//* \brief Select a block length for all following block commands (R/W)
1244 +//*----------------------------------------------------------------------------
1245 +AT91S_MCIDeviceStatus AT91F_MCI_SetBlocklength(AT91PS_MciDevice pMCI_Device,unsigned int length)
1246 +{
1247 +    return( AT91F_MCI_SendCommand(pMCI_Device, AT91C_SET_BLOCKLEN_CMD, length) );
1248 +}
1249 +
1250 +#ifdef MMC
1251 +//*----------------------------------------------------------------------------
1252 +//* \fn    AT91F_MCI_MMC_GetAllOCR
1253 +//* \brief Asks to all cards to send their operations conditions
1254 +//*----------------------------------------------------------------------------
1255 +AT91S_MCIDeviceStatus AT91F_MCI_MMC_GetAllOCR (AT91PS_MciDevice pMCI_Device)
1256 +{
1257 +       unsigned int    response =0x0;
1258 +       
1259 +       while(1)
1260 +    {
1261 +       response = AT91F_MCI_SendCommand(pMCI_Device,
1262 +                                                                               AT91C_MMC_SEND_OP_COND_CMD,
1263 +                                                                               AT91C_MMC_HOST_VOLTAGE_RANGE);
1264 +               if (response != AT91C_CMD_SEND_OK)
1265 +                       return AT91C_INIT_ERROR;
1266 +               
1267 +               response = AT91C_BASE_MCI->MCI_RSPR[0];
1268 +               
1269 +               if ( (response & AT91C_CARD_POWER_UP_BUSY) == AT91C_CARD_POWER_UP_BUSY)
1270 +                       return(response);       
1271 +       }
1272 +}
1273 +#endif
1274 +
1275 +#ifdef MMC
1276 +//*----------------------------------------------------------------------------
1277 +//* \fn    AT91F_MCI_MMC_GetAllCID
1278 +//* \brief Asks to the MMC on the chosen slot to send its CID
1279 +//*----------------------------------------------------------------------------
1280 +AT91S_MCIDeviceStatus AT91F_MCI_MMC_GetAllCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
1281 +{
1282 +       int Nb_Cards_Found=-1;
1283 +  
1284 +       while(1)
1285 +       {
1286 +               if(AT91F_MCI_SendCommand(pMCI_Device,
1287 +                                                               AT91C_MMC_ALL_SEND_CID_CMD,
1288 +                                                               AT91C_NO_ARGUMENT) != AT91C_CMD_SEND_OK)
1289 +                       return Nb_Cards_Found;
1290 +               else
1291 +               {               
1292 +                       Nb_Cards_Found = 0;
1293 +                       //* Assignation of the relative address to the MMC CARD
1294 +                       pMCI_Device->pMCI_DeviceFeatures[Nb_Cards_Found].Relative_Card_Address = Nb_Cards_Found + AT91C_FIRST_RCA;
1295 +                       //* Set the insert flag
1296 +                       pMCI_Device->pMCI_DeviceFeatures[Nb_Cards_Found].Card_Inserted = AT91C_MMC_CARD_INSERTED;
1297 +       
1298 +                       if (AT91F_MCI_SendCommand(pMCI_Device,
1299 +                                                                        AT91C_MMC_SET_RELATIVE_ADDR_CMD,
1300 +                                                                        (Nb_Cards_Found + AT91C_FIRST_RCA) << 16) != AT91C_CMD_SEND_OK)
1301 +                               return AT91C_CMD_SEND_ERROR;
1302 +                                
1303 +                       //* If no error during assignation address ==> Increment Nb_cards_Found
1304 +                       Nb_Cards_Found++ ;
1305 +               }
1306 +       }
1307 +}
1308 +#endif
1309 +#ifdef MMC
1310 +//*----------------------------------------------------------------------------
1311 +//* \fn    AT91F_MCI_MMC_Init
1312 +//* \brief Return the MMC initialisation status
1313 +//*----------------------------------------------------------------------------
1314 +AT91S_MCIDeviceStatus AT91F_MCI_MMC_Init (AT91PS_MciDevice pMCI_Device)
1315 +{
1316 +    unsigned int       tab_response[4];
1317 +       unsigned int    mult,blocknr;
1318 +       unsigned int    i,Nb_Cards_Found=0;
1319 +
1320 +       //* Resets all MMC Cards in Idle state
1321 +       AT91F_MCI_SendCommand(pMCI_Device, AT91C_MMC_GO_IDLE_STATE_CMD, AT91C_NO_ARGUMENT);
1322 +
1323 +    if(AT91F_MCI_MMC_GetAllOCR(pMCI_Device) == AT91C_INIT_ERROR)
1324 +       return AT91C_INIT_ERROR;
1325 +
1326 +       Nb_Cards_Found = AT91F_MCI_MMC_GetAllCID(pMCI_Device,tab_response);
1327 +       if (Nb_Cards_Found != AT91C_CMD_SEND_ERROR)
1328 +       {
1329 +           //* Set the Mode Register
1330 +       AT91C_BASE_MCI->MCI_MR = AT91C_MCI_MR_PDCMODE;
1331 +
1332 +               for(i = 0; i < Nb_Cards_Found; i++)
1333 +               {
1334 +                       if (AT91F_MCI_GetCSD(pMCI_Device,
1335 +                                                                         pMCI_Device->pMCI_DeviceFeatures[i].Relative_Card_Address,
1336 +                                                                         tab_response) != AT91C_CMD_SEND_OK)
1337 +                               pMCI_Device->pMCI_DeviceFeatures[i].Relative_Card_Address = 0;                                    
1338 +                       else
1339 +                       {
1340 +                               pMCI_Device->pMCI_DeviceFeatures[i].Max_Read_DataBlock_Length = 1 << ((tab_response[1] >> AT91C_CSD_RD_B_LEN_S) & AT91C_CSD_RD_B_LEN_M );
1341 +                               pMCI_Device->pMCI_DeviceFeatures[i].Max_Write_DataBlock_Length =        1 << ((tab_response[3] >> AT91C_CSD_WBLEN_S) & AT91C_CSD_WBLEN_M );
1342 +                               pMCI_Device->pMCI_DeviceFeatures[i].Sector_Size = 1 + ((tab_response[2] >> AT91C_CSD_v22_SECT_SIZE_S) & AT91C_CSD_v22_SECT_SIZE_M );
1343 +                               pMCI_Device->pMCI_DeviceFeatures[i].Read_Partial = (tab_response[1] >> AT91C_CSD_RD_B_PAR_S) & AT91C_CSD_RD_B_PAR_M;
1344 +                               pMCI_Device->pMCI_DeviceFeatures[i].Write_Partial = (tab_response[3] >> AT91C_CSD_WBLOCK_P_S) & AT91C_CSD_WBLOCK_P_M;
1345 +                               
1346 +                               // None in MMC specification version 2.2
1347 +                               pMCI_Device->pMCI_DeviceFeatures[i].Erase_Block_Enable = 0;
1348 +                               
1349 +                               pMCI_Device->pMCI_DeviceFeatures[i].Read_Block_Misalignment = (tab_response[1] >> AT91C_CSD_RD_B_MIS_S) & AT91C_CSD_RD_B_MIS_M;
1350 +                               pMCI_Device->pMCI_DeviceFeatures[i].Write_Block_Misalignment = (tab_response[1] >> AT91C_CSD_WR_B_MIS_S) & AT91C_CSD_WR_B_MIS_M;
1351 +
1352 +                               //// Compute Memory Capacity
1353 +                               // compute MULT
1354 +                               mult = 1 << ( ((tab_response[2] >> AT91C_CSD_C_SIZE_M_S) & AT91C_CSD_C_SIZE_M_M) + 2 );
1355 +                               // compute MSB of C_SIZE
1356 +                               blocknr = ((tab_response[1] >> AT91C_CSD_CSIZE_H_S) & AT91C_CSD_CSIZE_H_M) << 2;
1357 +                               // compute MULT * (LSB of C-SIZE + MSB already computed + 1) = BLOCKNR
1358 +                               blocknr = mult * ( ( blocknr + ( (tab_response[2] >> AT91C_CSD_CSIZE_L_S) & AT91C_CSD_CSIZE_L_M) ) + 1 );
1359 +
1360 +                               pMCI_Device->pMCI_DeviceFeatures[i].Memory_Capacity =  pMCI_Device->pMCI_DeviceFeatures[i].Max_Read_DataBlock_Length * blocknr;
1361 +                               //// End of Compute Memory Capacity
1362 +                               
1363 +                       }       // end of else                    
1364 +               }       // end of for
1365 +               
1366 +               return AT91C_INIT_OK;
1367 +       }       // end of if
1368 +
1369 +    return AT91C_INIT_ERROR;
1370 +}
1371 +#endif
1372 +
1373 +//*----------------------------------------------------------------------------
1374 +//* \fn    AT91F_MCI_SDCard_GetOCR
1375 +//* \brief Asks to all cards to send their operations conditions
1376 +//*----------------------------------------------------------------------------
1377 +AT91S_MCIDeviceStatus AT91F_MCI_SDCard_GetOCR (AT91PS_MciDevice pMCI_Device)
1378 +{
1379 +       unsigned int    response =0x0;
1380 +
1381 +       // The RCA to be used for CMD55 in Idle state shall be the card's default RCA=0x0000.
1382 +       pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address = 0x0;
1383 +       
1384 +       while( (response & AT91C_CARD_POWER_UP_BUSY) != AT91C_CARD_POWER_UP_BUSY )
1385 +    {
1386 +       response = AT91F_MCI_SDCard_SendAppCommand(pMCI_Device,
1387 +                                                                               AT91C_SDCARD_APP_OP_COND_CMD,
1388 +                                                                               AT91C_MMC_HOST_VOLTAGE_RANGE);
1389 +               if (response != AT91C_CMD_SEND_OK)
1390 +                       return AT91C_INIT_ERROR;
1391 +               
1392 +               response = AT91C_BASE_MCI->MCI_RSPR[0];
1393 +       }
1394 +       
1395 +       return(AT91C_BASE_MCI->MCI_RSPR[0]);
1396 +}
1397 +
1398 +//*----------------------------------------------------------------------------
1399 +//* \fn    AT91F_MCI_SDCard_GetCID
1400 +//* \brief Asks to the SDCard on the chosen slot to send its CID
1401 +//*----------------------------------------------------------------------------
1402 +AT91S_MCIDeviceStatus AT91F_MCI_SDCard_GetCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
1403 +{
1404 +       if(AT91F_MCI_SendCommand(pMCI_Device,
1405 +                                                       AT91C_ALL_SEND_CID_CMD,
1406 +                                                       AT91C_NO_ARGUMENT) != AT91C_CMD_SEND_OK)
1407 +               return AT91C_CMD_SEND_ERROR;
1408 +       
1409 +    response[0] = AT91C_BASE_MCI->MCI_RSPR[0];
1410 +       response[1] = AT91C_BASE_MCI->MCI_RSPR[1];
1411 +    response[2] = AT91C_BASE_MCI->MCI_RSPR[2];
1412 +    response[3] = AT91C_BASE_MCI->MCI_RSPR[3];
1413 +    
1414 +    return AT91C_CMD_SEND_OK;
1415 +}
1416 +
1417 +//*----------------------------------------------------------------------------
1418 +//* \fn    AT91F_MCI_SDCard_SetBusWidth
1419 +//* \brief  Set bus width for SDCard
1420 +//*----------------------------------------------------------------------------
1421 +AT91S_MCIDeviceStatus AT91F_MCI_SDCard_SetBusWidth(AT91PS_MciDevice pMCI_Device)
1422 +{
1423 +       volatile int    ret_value;
1424 +       char                    bus_width;
1425 +
1426 +       do
1427 +       {
1428 +               ret_value =AT91F_MCI_GetStatus(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address);
1429 +       }
1430 +       while((ret_value > 0) && ((ret_value & AT91C_SR_READY_FOR_DATA) == 0));
1431 +
1432 +       // Select Card
1433 +    AT91F_MCI_SendCommand(pMCI_Device,
1434 +                                               AT91C_SEL_DESEL_CARD_CMD,
1435 +                                               (pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address)<<16);
1436 +
1437 +       // Set bus width for Sdcard
1438 +       if(pMCI_Device->pMCI_DeviceDesc->SDCard_bus_width == AT91C_MCI_SCDBUS)
1439 +                       bus_width = AT91C_BUS_WIDTH_4BITS;
1440 +       else    bus_width = AT91C_BUS_WIDTH_1BIT;
1441 +
1442 +       if (AT91F_MCI_SDCard_SendAppCommand(pMCI_Device,AT91C_SDCARD_SET_BUS_WIDTH_CMD,bus_width) != AT91C_CMD_SEND_OK)
1443 +               return AT91C_CMD_SEND_ERROR;
1444 +
1445 +       return AT91C_CMD_SEND_OK;
1446 +}
1447 +
1448 +//*----------------------------------------------------------------------------
1449 +//* \fn    AT91F_MCI_SDCard_Init
1450 +//* \brief Return the SDCard initialisation status
1451 +//*----------------------------------------------------------------------------
1452 +AT91S_MCIDeviceStatus AT91F_MCI_SDCard_Init (AT91PS_MciDevice pMCI_Device)
1453 +{
1454 +    unsigned int       tab_response[4];
1455 +       unsigned int    mult,blocknr;
1456 +
1457 +       AT91F_MCI_SendCommand(pMCI_Device, AT91C_GO_IDLE_STATE_CMD, AT91C_NO_ARGUMENT);
1458 +
1459 +    if(AT91F_MCI_SDCard_GetOCR(pMCI_Device) == AT91C_INIT_ERROR)
1460 +       return AT91C_INIT_ERROR;
1461 +
1462 +       if (AT91F_MCI_SDCard_GetCID(pMCI_Device,tab_response) == AT91C_CMD_SEND_OK)
1463 +       {
1464 +           pMCI_Device->pMCI_DeviceFeatures->Card_Inserted = AT91C_SD_CARD_INSERTED;
1465 +
1466 +           if (AT91F_MCI_SendCommand(pMCI_Device, AT91C_SET_RELATIVE_ADDR_CMD, 0) == AT91C_CMD_SEND_OK)
1467 +               {
1468 +                       pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address = (AT91C_BASE_MCI->MCI_RSPR[0] >> 16);
1469 +                       if (AT91F_MCI_GetCSD(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address,tab_response) == AT91C_CMD_SEND_OK)
1470 +                       {
1471 +                               pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length = 1 << ((tab_response[1] >> AT91C_CSD_RD_B_LEN_S) & AT91C_CSD_RD_B_LEN_M );
1472 +                               pMCI_Device->pMCI_DeviceFeatures->Max_Write_DataBlock_Length =  1 << ((tab_response[3] >> AT91C_CSD_WBLEN_S) & AT91C_CSD_WBLEN_M );
1473 +                               pMCI_Device->pMCI_DeviceFeatures->Sector_Size = 1 + ((tab_response[2] >> AT91C_CSD_v21_SECT_SIZE_S) & AT91C_CSD_v21_SECT_SIZE_M );
1474 +                               pMCI_Device->pMCI_DeviceFeatures->Read_Partial = (tab_response[1] >> AT91C_CSD_RD_B_PAR_S) & AT91C_CSD_RD_B_PAR_M;
1475 +                               pMCI_Device->pMCI_DeviceFeatures->Write_Partial = (tab_response[3] >> AT91C_CSD_WBLOCK_P_S) & AT91C_CSD_WBLOCK_P_M;
1476 +                               pMCI_Device->pMCI_DeviceFeatures->Erase_Block_Enable = (tab_response[3] >> AT91C_CSD_v21_ER_BLEN_EN_S) & AT91C_CSD_v21_ER_BLEN_EN_M;
1477 +                               pMCI_Device->pMCI_DeviceFeatures->Read_Block_Misalignment = (tab_response[1] >> AT91C_CSD_RD_B_MIS_S) & AT91C_CSD_RD_B_MIS_M;
1478 +                               pMCI_Device->pMCI_DeviceFeatures->Write_Block_Misalignment = (tab_response[1] >> AT91C_CSD_WR_B_MIS_S) & AT91C_CSD_WR_B_MIS_M;
1479 +
1480 +                               //// Compute Memory Capacity
1481 +                                       // compute MULT
1482 +                                       mult = 1 << ( ((tab_response[2] >> AT91C_CSD_C_SIZE_M_S) & AT91C_CSD_C_SIZE_M_M) + 2 );
1483 +                                       // compute MSB of C_SIZE
1484 +                                       blocknr = ((tab_response[1] >> AT91C_CSD_CSIZE_H_S) & AT91C_CSD_CSIZE_H_M) << 2;
1485 +                                       // compute MULT * (LSB of C-SIZE + MSB already computed + 1) = BLOCKNR
1486 +                                       blocknr = mult * ( ( blocknr + ( (tab_response[2] >> AT91C_CSD_CSIZE_L_S) & AT91C_CSD_CSIZE_L_M) ) + 1 );
1487 +
1488 +                                       pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity =  pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length * blocknr;
1489 +                               //// End of Compute Memory Capacity
1490 +                                       printf("BLK 0x%x", pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length);
1491 +
1492 +                               if( AT91F_MCI_SDCard_SetBusWidth(pMCI_Device) == AT91C_CMD_SEND_OK )
1493 +                               {       
1494 +                                        if (AT91F_MCI_SetBlocklength(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length) == AT91C_CMD_SEND_OK)
1495 +                                       return AT91C_INIT_OK;
1496 +                               }
1497 +                       }
1498 +               }
1499 +       }
1500 +    return AT91C_INIT_ERROR;
1501 +}
1502 diff -urN romboot.old/mci_main.cpp romboot/mci_main.cpp
1503 --- romboot.old/mci_main.cpp    1970-01-01 01:00:00.000000000 +0100
1504 +++ romboot/mci_main.cpp        2007-03-22 18:52:58.000000000 +0100
1505 @@ -0,0 +1,317 @@
1506 +//*----------------------------------------------------------------------------
1507 +//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
1508 +//*----------------------------------------------------------------------------
1509 +//* The software is delivered "AS IS" without warranty or condition of any
1510 +//* kind, either express, implied or statutory. This includes without
1511 +//* limitation any warranty or condition with respect to merchantability or
1512 +//* fitness for any particular purpose, or against the infringements of
1513 +//* intellectual property rights of others.
1514 +//*----------------------------------------------------------------------------
1515 +//* File Name           : main.c
1516 +//* Object              : main application written in C
1517 +//* Creation            : FB   21/11/2002
1518 +//*
1519 +//*----------------------------------------------------------------------------
1520 +#include "com.h"
1521 +#include "dataflash.h"
1522 +#include <AT91C_MCI_Device.h>
1523 +
1524 +#define AT91C_MCI_TIMEOUT                      1000000   /* For AT91F_MCIDeviceWaitReady */
1525 +#define BUFFER_SIZE_MCI_DEVICE         512
1526 +#define MASTER_CLOCK                           60000000
1527 +#define FALSE                                          -1
1528 +#define TRUE                                           1
1529 +
1530 +//* External Functions
1531 +extern "C" void AT91F_ASM_MCI_Handler(void);
1532 +extern "C" void AT91F_MCI_Device_Handler(AT91PS_MciDevice,unsigned int);
1533 +extern AT91S_MCIDeviceStatus   AT91F_MCI_SDCard_Init (AT91PS_MciDevice);
1534 +extern AT91S_MCIDeviceStatus   AT91F_MCI_SetBlocklength(AT91PS_MciDevice,unsigned int);
1535 +extern AT91S_MCIDeviceStatus   AT91F_MCI_ReadBlock(AT91PS_MciDevice,int,unsigned int *,int);
1536 +extern AT91S_MCIDeviceStatus   AT91F_MCI_WriteBlock(AT91PS_MciDevice,int,unsigned int *,int);
1537 +//* Global Variables
1538 +AT91S_MciDeviceFeatures                        MCI_Device_Features;
1539 +AT91S_MciDeviceDesc                            MCI_Device_Desc;
1540 +AT91S_MciDevice                                        MCI_Device;
1541 +
1542 +unsigned int dlBuffer = 0x20000000;
1543 +#undef MCI_TEST
1544 +#ifdef MCI_TEST
1545 +char TestString[] = "\r\nHello Hamish\r\n";
1546 +#endif
1547 +
1548 +//*----------------------------------------------------------------------------
1549 +//* \fn    AT91F_MCIDeviceWaitReady
1550 +//* \brief Wait for MCI Device ready
1551 +//*----------------------------------------------------------------------------
1552 +void AT91F_MCIDeviceWaitReady(unsigned int timeout)
1553 +{
1554 +       volatile int status;
1555 +       
1556 +       do
1557 +       {
1558 +               status = AT91C_BASE_MCI->MCI_SR;
1559 +               timeout--;
1560 +       }
1561 +       while( !(status & AT91C_MCI_NOTBUSY)  && (timeout>0) ); 
1562 +}
1563 +
1564 +unsigned int swab32(unsigned int data)
1565 +{
1566 +       unsigned int res = 0;
1567 +
1568 +       res = (data & 0x000000ff) << 24 |
1569 +                               (data & 0x0000ff00) << 8  |
1570 +                               (data & 0x00ff0000) >> 8  |
1571 +                               (data & 0xff000000) >> 24;
1572 +
1573 +       return res;
1574 +}
1575 +
1576 +AT91S_MCIDeviceStatus readblock(
1577 +       AT91PS_MciDevice pMCI_Device,
1578 +       int src,
1579 +       unsigned int *databuffer,
1580 +       int sizeToRead)
1581 +{
1582 +       int i;
1583 +       unsigned char *buf = (unsigned char *)databuffer;
1584 +
1585 +       //* Read Block 1
1586 +       for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++)
1587 +               *buf++ = 0x00;  
1588 +       AT91F_MCI_ReadBlock(&MCI_Device,src,databuffer,sizeToRead);
1589 +
1590 +       //* Wait end of Read
1591 +       AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
1592 +
1593 +       {
1594 +               int index;
1595 +               unsigned int *uiBuffer = databuffer;
1596 +
1597 +               for(index = 0; index < 512/4; index++)
1598 +                       uiBuffer[index] = swab32(uiBuffer[index]);
1599 +       }
1600 +       return(1);
1601 +}
1602 +
1603 +#if 0
1604 +void printdata(unsigned int bufpos)
1605 +       {
1606 +               unsigned int *uip;
1607 +               int linebytes = 16;
1608 +               int nbytes = 64;
1609 +               int size = 4;
1610 +               int i;
1611 +
1612 +               uip = (unsigned int *)bufpos;
1613 +
1614 +               do {
1615 +
1616 +                       for(i=0; i<linebytes; i+=size) {
1617 +                               printf(" %08x", *uip++);
1618 +                               }
1619 +                       
1620 +                       printf("\n\r");
1621 +                       nbytes -= linebytes;
1622 +               } while (nbytes > 0);
1623 +       }
1624 +#endif
1625 +//extern char message[40];
1626 +
1627 +int notnull(int bufpos, unsigned int len)
1628 +{
1629 +       int i;
1630 +       unsigned char * bp = (unsigned char *)bufpos;
1631 +
1632 +       for (i=0; i<len; i++)
1633 +               if (bp[i] != '\0')
1634 +                       return(1);
1635 +
1636 +       return(0);
1637 +}
1638 +//*----------------------------------------------------------------------------
1639 +//* \fn    AT91F_Test
1640 +//* \brief Test Functions
1641 +//*----------------------------------------------------------------------------
1642 +int AT91F_Test(void)
1643 +{
1644 +       int i;
1645 +       unsigned int Max_Read_DataBlock_Length;
1646 +       int block = 0;
1647 +       int bufpos = dlBuffer;
1648 +       int lastvalid = 0;
1649 +       int NbPage = 0;
1650 +
1651 +               
1652 +       Max_Read_DataBlock_Length = MCI_Device.pMCI_DeviceFeatures->Max_Read_DataBlock_Length;
1653 +       
1654 +       //* ReadBlock & WriteBlock Test -> Entire Block
1655 +
1656 +       //* Wait MCI Device Ready
1657 +       AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
1658 +
1659 +#ifdef MCI_TEST
1660 +       //* Read Block 1
1661 +       for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++)   Buffer[i] = 0x00;       
1662 +       AT91F_MCI_ReadBlock(&MCI_Device,(1*Max_Read_DataBlock_Length),(unsigned int*) Buffer,Max_Read_DataBlock_Length);
1663 +
1664 +       //* Wait end of Read
1665 +       AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
1666 +
1667 +       // Write Page 1
1668 +//    sprintf(Buffer,"\n\rThis sentence is written in your device... Congratulations\n\r");
1669 +       for(i=0; i<16; i++)
1670 +               Buffer[i] = TestString[i];
1671 +       AT91F_MCI_WriteBlock(&MCI_Device,(1*Max_Read_DataBlock_Length),(unsigned int*) Buffer,Max_Read_DataBlock_Length);
1672 +
1673 +       //* Wait end of Write
1674 +       AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
1675 +#endif
1676 +
1677 +       for(i=0; i<64; i++) {
1678 +               readblock(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
1679 +               if (notnull(bufpos, Max_Read_DataBlock_Length))
1680 +                       lastvalid++;
1681 +               block++;
1682 +               bufpos += 512;
1683 +               }
1684 +
1685 +       i = dataflash_info[0].Device.pages_number;
1686 +       while(i>>=1)
1687 +               NbPage++;
1688 +       i = lastvalid + (NbPage << 13) + (dataflash_info[0].Device.pages_size << 17);
1689 +       *(int *)(dlBuffer + 0x14) = i;
1690 +
1691 +       for(i=0; i<4688; i++) {
1692 +               readblock(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
1693 +               block++;
1694 +               bufpos += 512;
1695 +               }
1696 +       write_dataflash(0xc0000000, dlBuffer, 512 * block);
1697 +       //* End Of Test
1698 +       printf("DONE %d\n\r", lastvalid);
1699 +                       
1700 +//     printf(Buffer);
1701 +
1702 +       return TRUE;
1703 +}
1704 +
1705 +//*----------------------------------------------------------------------------
1706 +//* \fn    AT91F_CfgDevice
1707 +//* \brief This function is used to initialise MMC or SDCard Features
1708 +//*----------------------------------------------------------------------------
1709 +void AT91F_CfgDevice(void)
1710 +{
1711 +       // Init Device Structure
1712 +
1713 +       MCI_Device_Features.Relative_Card_Address               = 0;
1714 +       MCI_Device_Features.Card_Inserted                               = AT91C_CARD_REMOVED;
1715 +       MCI_Device_Features.Max_Read_DataBlock_Length   = 0;
1716 +       MCI_Device_Features.Max_Write_DataBlock_Length  = 0;
1717 +       MCI_Device_Features.Read_Partial                                = 0;
1718 +       MCI_Device_Features.Write_Partial                               = 0;
1719 +       MCI_Device_Features.Erase_Block_Enable                  = 0;
1720 +       MCI_Device_Features.Sector_Size                                 = 0;
1721 +       MCI_Device_Features.Memory_Capacity                     = 0;
1722 +       
1723 +       MCI_Device_Desc.state                                                   = AT91C_MCI_IDLE;
1724 +       MCI_Device_Desc.SDCard_bus_width                                = AT91C_MCI_SCDBUS;
1725 +       
1726 +       // Init AT91S_DataFlash Global Structure, by default AT45DB choosen !!!
1727 +       MCI_Device.pMCI_DeviceDesc              = &MCI_Device_Desc;
1728 +       MCI_Device.pMCI_DeviceFeatures  = &MCI_Device_Features;
1729 +
1730 +}
1731 +
1732 +//*----------------------------------------------------------------------------
1733 +//* \fn    AT91F_Test_SDCard
1734 +//* \brief Configure MCI for SDCard and complete SDCard init, then jump to Test Functions
1735 +//*----------------------------------------------------------------------------
1736 +int AT91F_Test_SDCard(void)
1737 +{
1738 +       //////////////////////////////////////////////////////////
1739 +       //* For SDCard Init
1740 +       //////////////////////////////////////////////////////////
1741 +
1742 +       AT91F_MCI_Configure(AT91C_BASE_MCI,
1743 +                                               AT91C_MCI_DTOR_1MEGA_CYCLES,
1744 +                                               AT91C_MCI_MR_PDCMODE,                   // 15MHz for MCK = 60MHz (CLKDIV = 1)
1745 +                                               AT91C_MCI_SDCARD_4BITS_SLOTA);
1746 +       
1747 +       if(AT91F_MCI_SDCard_Init(&MCI_Device) != AT91C_INIT_OK)
1748 +               return FALSE;
1749 +
1750 +       printf("\n\rINI OK: TST\n\r");
1751 +
1752 +       // Enter Main Tests     
1753 +       return(AT91F_Test());
1754 +}
1755 +
1756 +//*----------------------------------------------------------------------------
1757 +//* \fn    AT91F_MCI_Handler
1758 +//* \brief MCI Handler
1759 +//*----------------------------------------------------------------------------
1760 +extern "C" void AT91F_MCI_Handler(void);
1761 +
1762 +void AT91F_MCI_Handler(void)
1763 +{
1764 +       int status;
1765 +
1766 +       status = ( AT91C_BASE_MCI->MCI_SR & AT91C_BASE_MCI->MCI_IMR );
1767 +
1768 +       AT91F_MCI_Device_Handler(&MCI_Device,status);
1769 +}
1770 +
1771 +//*----------------------------------------------------------------------------
1772 +//* \fn    main
1773 +//* \brief main function
1774 +//*----------------------------------------------------------------------------
1775 +int mci_main(void)
1776 +{
1777 +//     printf("MCI Test\n\r");
1778 +
1779 +///////////////////////////////////////////////////////////////////////////////////////////
1780 +//  MCI Init : common to MMC and SDCard
1781 +///////////////////////////////////////////////////////////////////////////////////////////
1782 +
1783 +//     printf("\n\rInit MCI Interface\n\r");
1784 +
1785 +    // Set up PIO SDC_TYPE to switch on MMC/SDCard and not DataFlash Card
1786 +       AT91F_PIO_CfgOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
1787 +       AT91F_PIO_SetOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
1788 +       
1789 +       // Init MCI for MMC and SDCard interface
1790 +       AT91F_MCI_CfgPIO();     
1791 +       AT91F_MCI_CfgPMC();
1792 +       AT91F_PDC_Open(AT91C_BASE_PDC_MCI);
1793 +
1794 +    // Disable all the interrupts
1795 +    AT91C_BASE_MCI->MCI_IDR = 0xFFFFFFFF;
1796 +
1797 +       // Init MCI Device Structures
1798 +       AT91F_CfgDevice();
1799 +
1800 +       // Configure MCI interrupt 
1801 +       AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
1802 +                                                AT91C_ID_MCI,
1803 +                                                AT91C_AIC_PRIOR_HIGHEST,
1804 +                                                AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,
1805 +                                                AT91F_ASM_MCI_Handler);
1806 +
1807 +       // Enable MCI interrupt
1808 +       AT91F_AIC_EnableIt(AT91C_BASE_AIC,AT91C_ID_MCI);
1809 +
1810 +///////////////////////////////////////////////////////////////////////////////////////////
1811 +//  Enter Test Menu
1812 +///////////////////////////////////////////////////////////////////////////////////////////
1813 +
1814 +       // Enable Receiver
1815 +       AT91F_US_EnableRx((AT91PS_USART) AT91C_BASE_DBGU);
1816 +
1817 +       if(AT91F_Test_SDCard() == TRUE)
1818 +               printf("\n\rTST OK\n\r");
1819 +       else
1820 +               printf("\n\rTST Fail\n\r");
1821 +       return(1);              
1822 +}