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
5 +#include <AT91RM9200_inc.h>
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
20 +/* -----------------------------------------------------------------------------
21 + AT91F_ASM_MCI_Handler
22 + ---------------------
23 + Handler called by the AIC
28 + ----------------------------------------------------------------------------- */
30 +.global AT91F_ASM_MCI_Handler
32 +AT91F_ASM_MCI_Handler:
33 +/* Adjust and save LR_irq in IRQ stack */
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]
43 +/* Save SPSR and r0 in IRQ stack */
45 + stmfd sp!, {r0, r14}
47 +/* Enable Interrupt and Switch in SYS Mode */
50 + orr r0, r0, #ARM_MODE_SYS
53 +/* Save scratch/used registers and LR in User Stack */
54 + stmfd sp!, { r1-r3, r12, r14}
56 + ldr r1, =AT91F_MCI_Handler
60 +/* Restore scratch/used registers and LR from User Stack */
61 + ldmia sp!, { r1-r3, r12, r14}
63 +/* Disable Interrupt and switch back in IRQ mode */
65 + bic r0, r0, #ARM_MODE_SYS
66 + orr r0, r0, #I_BIT | ARM_MODE_IRQ
69 +/* Mark the End of Interrupt on the AIC */
70 + ldr r0, =AT91C_BASE_AIC
71 + str r0, [r0, #AIC_EOICR]
73 +/* Restore SPSR_irq and r0 from IRQ stack */
74 + ldmia sp!, {r0, r14}
77 +/* Restore adjusted LR_irq from IRQ stack directly in the PC */
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
88 -CROSS=/space/arm/buildroot/build_arm_nofpu/staging_dir/bin/arm-linux-
89 -#CROSS=/opt/cross/bin/arm-linux-
91 -GCC="$CROSS"gcc-msoft-float
93 -OBJCOPY="$CROSS"objcopy
95 -OBJDUMP="$CROSS"objdump
97 -LDFLAGS="-T elf32-littlearm.lds -Ttext 0"
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
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
114 -$OBJCOPY $OUTPUT.out -O binary $OUTPUT.bin
116 -$OBJDUMP -h -s $OUTPUT.out > $OUTPUT.lss
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
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
136 +//* 1.0 26/11/02 FB : Creation
137 +//*---------------------------------------------------------------------------
139 +#ifndef AT91C_MCI_Device_h
140 +#define AT91C_MCI_Device_h
142 +#include "AT91RM9200.h"
143 +#include "lib_AT91RM9200.h"
145 +typedef unsigned int AT91S_MCIDeviceStatus;
147 +/////////////////////////////////////////////////////////////////////////////////////////////////////
149 +#define AT91C_CARD_REMOVED 0
150 +#define AT91C_MMC_CARD_INSERTED 1
151 +#define AT91C_SD_CARD_INSERTED 2
153 +#define AT91C_NO_ARGUMENT 0x0
155 +#define AT91C_FIRST_RCA 0xCAFE
156 +#define AT91C_MAX_MCI_CARDS 10
158 +#define AT91C_BUS_WIDTH_1BIT 0x00
159 +#define AT91C_BUS_WIDTH_4BITS 0x02
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
172 +#define AT91C_TIMEOUT_CMDRDY 30
174 +/////////////////////////////////////////////////////////////////////////////////////////////////////
175 +// MMC & SDCard Structures
176 +/////////////////////////////////////////////////////////////////////////////////////////////////////
178 +/*-----------------------------------------------*/
179 +/* SDCard Device Descriptor Structure Definition */
180 +/*-----------------------------------------------*/
181 +typedef struct _AT91S_MciDeviceDesc
183 + volatile unsigned char state;
184 + unsigned char SDCard_bus_width;
186 +} AT91S_MciDeviceDesc, *AT91PS_MciDeviceDesc;
188 +/*---------------------------------------------*/
189 +/* MMC & SDCard Structure Device Features */
190 +/*---------------------------------------------*/
191 +typedef struct _AT91S_MciDeviceFeatures
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
205 +} AT91S_MciDeviceFeatures, *AT91PS_MciDeviceFeatures ;
207 +/*---------------------------------------------*/
208 +/* MCI Device Structure Definition */
209 +/*---------------------------------------------*/
210 +typedef struct _AT91S_MciDevice
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;
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)
221 +/////////////////////////////////////////////////////////////////
222 +// Class 0 & 1 commands: Basic commands and Read Stream commands
223 +/////////////////////////////////////////////////////////////////
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)
233 +#define AT91C_SET_DSR_CMD (4 | AT91C_MCI_TRCMD_NO | AT91C_MCI_SPCMD_NONE | AT91C_MCI_RSPTYP_NO | AT91C_MCI_MAXLAT ) // no tested
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 )
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 )
245 +//*------------------------------------------------
246 +//* Class 2 commands: Block oriented Read commands
247 +//*------------------------------------------------
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)
253 +//*--------------------------------------------
254 +//* Class 3 commands: Sequential write commands
255 +//*--------------------------------------------
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
259 +//*------------------------------------------------
260 +//* Class 4 commands: Block oriented write commands
261 +//*------------------------------------------------
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 )
268 +//*----------------------------------------
269 +//* Class 6 commands: Group Write protect
270 +//*----------------------------------------
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 )
277 +//*----------------------------------------
278 +//* Class 5 commands: Erase commands
279 +//*----------------------------------------
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 )
289 +//*----------------------------------------
290 +//* Class 7 commands: Lock commands
291 +//*----------------------------------------
293 +#define AT91C_LOCK_UNLOCK (42 | AT91C_MCI_SPCMD_NONE | AT91C_MCI_RSPTYP_48 | AT91C_MCI_TRCMD_NO | AT91C_MCI_MAXLAT) // no tested
295 +//*-----------------------------------------------
296 +// Class 8 commands: Application specific commands
297 +//*-----------------------------------------------
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
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)
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)
318 +//*----------------------------------------
319 +//* Class 9 commands: IO Mode commands
320 +//*----------------------------------------
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)
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
341 +/////////////////////////////////////////////////////////////////////////////////////////////////////
343 +/////////////////////////////////////////////////////////////////////////////////////////////////////
344 +#define AT91C_MCI_SR_ERROR (AT91C_MCI_UNRE |\
354 +////////////////////////////////////////////////////////////////////////////////////////////////////
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)
379 +#define AT91C_MMC_HOST_VOLTAGE_RANGE (AT91C_VDD_27_28 +\
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)
400 +#define AT91C_SR_CARD_SELECTED (AT91C_SR_READY_FOR_DATA + AT91C_SR_TRAN)
402 +/////////////////////////////////////////////////////////////////////////////////////////////////////
403 +// MMC CSD register header File
404 +// AT91C_CSD_xxx_S for shift value
405 +// AT91C_CSD_xxx_M for mask value
406 +/////////////////////////////////////////////////////////////////////////////////////////////////////
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]
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
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
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
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
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]
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
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]
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
498 +/////////////////////////////////////////////////////////////////////////////////////////////////////
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
506 //*----------------------------------------------------------------------------
507 void AT91F_SpuriousHandler()
509 - AT91F_DBGU_Printk("-F- Spurious Interrupt detected\n\r");
510 + AT91F_DBGU_Printk("ISI");
515 //*----------------------------------------------------------------------------
516 void AT91F_DataAbort()
518 - AT91F_DBGU_Printk("-F- Data Abort detected\n\r");
519 + AT91F_DBGU_Printk("IDA");
524 //*----------------------------------------------------------------------------
525 void AT91F_FetchAbort()
527 - AT91F_DBGU_Printk("-F- Prefetch Abort detected\n\r");
528 + AT91F_DBGU_Printk("IPA");
533 //*----------------------------------------------------------------------------
536 - AT91F_DBGU_Printk("-F- Undef detected\n\r");
537 + AT91F_DBGU_Printk("IUD");
542 //*----------------------------------------------------------------------------
543 void AT91F_UndefHandler()
545 - AT91F_DBGU_Printk("-F- Undef detected\n\r");
546 + AT91F_DBGU_Printk("IUD");
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
554 #define DELAY_MAIN_FREQ 1000
555 #define DISP_LINE_LEN 16
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);
565 -const char *menu_separ = "*----------------------------------------*\n\r";
566 +//const char *menu_separ = "*----------------------------------------*\n\r";
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"
577 + "5: RM BL in DF\n\r"
580 //* Globales variables
581 @@ -151,12 +155,12 @@
582 //*-----------------------------------------------------------------------------
583 void AT91F_DisplayMenu(void)
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);
597 //*-----------------------------------------------------------------------------
603 //*-----------------------------------------------------------------------------
604 //* Function Name : AT91F_MemoryDisplay()
605 //* Object : Display the content of the dataflash
607 } while (nbytes > 0);
613 //*--------------------------------------------------------------------------------------
614 //* Function Name : AT91F_SetPLL
620 +/*void LedCode(void)
623 pRegister = (int *)0xFFFFF800; // Enable port C peripheral reg
624 @@ -318,15 +323,16 @@
625 pRegister = (int *)0xFFFFF834; // Clear bits
630 void AT91F_StartUboot(unsigned int dummy, void *pvoid)
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");
638 AT91F_ResetRegisters();
641 Jump(AT91C_UBOOT_ADDR);
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);
648 - printf("press any key to enter bootloader\n\r");
649 + printf("press key\n\r");
653 svcUbootTempo.Stop(&svcUbootTempo);
657 - while(command == 0)
659 - AddressToDownload = AT91C_DOWNLOAD_BASE_ADDRESS;
660 - SizeToDownload = AT91C_DOWNLOAD_MAX_SIZE;
663 + while(command == 0) {
664 + AddressToDownload = AT91C_DOWNLOAD_BASE_ADDRESS;
665 + SizeToDownload = AT91C_DOWNLOAD_MAX_SIZE;
668 - AT91F_DisplayMenu();
671 - AT91F_ReadLine("Enter: ", message);
672 + AT91F_DisplayMenu();
675 + AT91F_ReadLine("Enter: ", message);
677 - command = message[0];
678 - if(command == '1' || command == '2')
679 - if(AsciiToHex(&message[2], &DeviceAddress) == 0)
685 - printf("Download Dataflash [0x%x]\n\r", DeviceAddress);
687 - switch(DeviceAddress & 0xFF000000)
689 - case CFG_DATAFLASH_LOGIC_ADDR_CS0:
692 + command = message[0];
693 + if(command == '1' || command == '2')
694 + if(AsciiToHex(&message[2], &DeviceAddress) == 0)
699 + printf("DL DF [0x%x]\n\r", DeviceAddress);
701 + switch(DeviceAddress & 0xFF000000) {
702 + case CFG_DATAFLASH_LOGIC_ADDR_CS0:
706 - case CFG_DATAFLASH_LOGIC_ADDR_CS3:
709 + case CFG_DATAFLASH_LOGIC_ADDR_CS3:
722 - AT91F_MemoryDisplay(DeviceAddress, 4, 64);
723 - AT91F_ReadLine ((char *)0, message);
724 - DeviceAddress += 0x100;
734 + AT91F_MemoryDisplay(DeviceAddress, 4, 64);
735 + AT91F_ReadLine ((char *)0, message);
736 + DeviceAddress += 0x100;
737 + } while(message[0] == '\0');
748 + AT91F_StartUboot(0, (void *)0);
756 + for(i = (int *)0x20000000; i < (int *)0x20004000; i++)
759 + write_dataflash(0xc0000000, 0x20000000, 0x4000);
760 + printf("BL CLR\r\n");
767 + } // switch(command)
768 + } // while(command == 0)
770 + xmodemPipe.Read(&xmodemPipe, (char *)AddressToDownload, SizeToDownload, XmodemProtocol, 0);
771 + while(XmodemComplete !=1);
772 + SizeToDownload = (unsigned int)(svcXmodem.pData) - (unsigned int)AddressToDownload;
774 + // Modification of vector 6
776 + i = dataflash_info[device].Device.pages_number;
779 + i = (SizeToDownload / 512) + 1 + (NbPage << 13) + (dataflash_info[device].Device.pages_size << 17);
780 + *(int *)(AddressToDownload + AT91C_OFFSET_VECT6) = i;
782 +// printf("\n\rModification of Arm Vector 6 :%x\n\r", i);
784 + printf("\n\rWR %d in DF [0x%x]\n\r",SizeToDownload, DeviceAddress);
786 + pAT91->CRC32((const unsigned char *)AddressToDownload, SizeToDownload , &crc1);
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;
794 + //* Read dataflash page in TestBuffer
795 + read_dataflash (DeviceAddress, SizeToDownload, (char *)(AddressToDownload));
797 + printf("Vfy DF: ");
800 + pAT91->CRC32((const unsigned char *)AddressToDownload, SizeToDownload , &crc2);
802 + printf("Fail\r\n");
807 + XmodemComplete = 0;
808 + AT91F_WaitKeyPressed();
810 - while(message[0] == '\0');
815 - AT91F_StartUboot(0, (void *)0);
821 - for(i = (int *)0x20000000; i < (int *)0x20004000; i++)
824 - write_dataflash(0xc0000000, 0x20000000, 0x4000);
825 - printf("Bootsection cleared\r\n");
834 - xmodemPipe.Read(&xmodemPipe, (char *)AddressToDownload, SizeToDownload, XmodemProtocol, 0);
835 - while(XmodemComplete !=1);
836 - SizeToDownload = (unsigned int)(svcXmodem.pData) - (unsigned int)AddressToDownload;
838 - // Modification of vector 6
840 - i = dataflash_info[device].Device.pages_number;
843 - i = (SizeToDownload / 512) + 1 + (NbPage << 13) + (dataflash_info[device].Device.pages_size << 17);
844 - *(int *)(AddressToDownload + AT91C_OFFSET_VECT6) = i;
846 - printf("\n\rModification of Arm Vector 6 :%x\n\r", i);
848 - printf("\n\rWrite %d bytes in DataFlash [0x%x]\n\r",SizeToDownload, DeviceAddress);
850 - pAT91->CRC32((const unsigned char *)AddressToDownload, SizeToDownload , &crc1);
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;
858 - //* Read dataflash page in TestBuffer
859 - read_dataflash (DeviceAddress, SizeToDownload, (char *)(AddressToDownload));
861 - printf("Verify Dataflash: ");
864 - pAT91->CRC32((const unsigned char *)AddressToDownload, SizeToDownload , &crc2);
866 - printf("Failed\r\n");
871 - XmodemComplete = 0;
872 - AT91F_WaitKeyPressed();
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
880 #define AT91C_OFFSET_VECT6 0x14 //* Offset for ARM vector 6
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);
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
891 LINKFLAGS= -T elf32-littlearm.lds -Ttext 0
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
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
909 $(CC) -c -Iinclude -o $@ $<
911 +asm_mci_isr.o: asm_mci_isr.S
912 + $(CC) -c -Iinclude -o $@ $<
914 cstartup_ram.o: cstartup_ram.S
915 $(CC) -c -Iinclude -o $@ $<
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
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
934 +//*----------------------------------------------------------------------------
936 +#include <AT91C_MCI_Device.h>
939 +#define ENABLE_WRITE 1
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,
951 + unsigned int error,status;
952 + //unsigned int tick=0;
954 + // Send the command
955 + AT91C_BASE_MCI->MCI_ARGR = Arg;
956 + AT91C_BASE_MCI->MCI_CMDR = Cmd;
958 + // wait for CMDRDY Status flag to read the response
961 + status = AT91C_BASE_MCI->MCI_SR;
964 + while( !(status & AT91C_MCI_CMDRDY) );//&& (tick<100) );
966 + // Test error ==> if crc error and response R3 ==> don't check error
967 + error = (AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR;
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);
975 + if (error != AT91C_MCI_RCRCE)
976 + return ((AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR);
979 + return AT91C_CMD_SEND_OK;
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,
991 + unsigned int status;
992 + //unsigned int tick=0;
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;
998 + // wait for CMDRDY Status flag to read the response
1001 + status = AT91C_BASE_MCI->MCI_SR;
1004 + while( !(status & AT91C_MCI_CMDRDY) );//&& (tick<100) );
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);
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;
1014 + return( AT91F_MCI_SendCommand(pMCI_Device,Cmd_App,Arg) );
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)
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]);
1028 + return AT91C_CMD_SEND_ERROR;
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);
1037 +void AT91F_MCI_Device_Handler(
1038 + AT91PS_MciDevice pMCI_Device,
1039 + unsigned int status)
1041 + // If End of Tx Buffer Empty interrupt occurred
1042 + if ( status & AT91C_MCI_TXBUFE )
1044 + AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_TXBUFE;
1045 + AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_TXTDIS;
1047 + pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_IDLE;
1048 + } // End of if AT91C_MCI_TXBUFF
1050 + // If End of Rx Buffer Full interrupt occurred
1051 + if ( status & AT91C_MCI_RXBUFF )
1053 + AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_RXBUFF;
1054 + AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_RXTDIS;
1056 + pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_IDLE;
1057 + } // End of if AT91C_MCI_RXBUFF
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,
1068 + unsigned int *dataBuffer,
1071 + ////////////////////////////////////////////////////////////////////////////////////////////
1072 + if(pMCI_Device->pMCI_DeviceDesc->state != AT91C_MCI_IDLE)
1073 + return AT91C_READ_ERROR;
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;
1078 + if ( (src + sizeToRead) > pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity )
1079 + return AT91C_READ_ERROR;
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;
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;
1091 + if( sizeToRead > pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length)
1092 + return AT91C_READ_ERROR;
1093 + ////////////////////////////////////////////////////////////////////////////////////////////
1095 + // Init Mode Register
1096 + AT91C_BASE_MCI->MCI_MR |= ((pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length << 16) | AT91C_MCI_PDCMODE);
1098 + if (sizeToRead %4)
1099 + sizeToRead = (sizeToRead /4)+1;
1101 + sizeToRead = sizeToRead/4;
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;
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;
1111 + pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_RX_SINGLE_BLOCK;
1113 + // Enable AT91C_MCI_RXBUFF Interrupt
1114 + AT91C_BASE_MCI->MCI_IER = AT91C_MCI_RXBUFF;
1116 + // (PDC) Receiver Transfer Enable
1117 + AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_RXTEN;
1119 + return AT91C_READ_OK;
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,
1131 + unsigned int *dataBuffer,
1134 + ////////////////////////////////////////////////////////////////////////////////////////////
1135 + if( pMCI_Device->pMCI_DeviceDesc->state != AT91C_MCI_IDLE)
1136 + return AT91C_WRITE_ERROR;
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;
1141 + if ( (dest + sizeToWrite) > pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity )
1142 + return AT91C_WRITE_ERROR;
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;
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;
1153 + if( sizeToWrite > pMCI_Device->pMCI_DeviceFeatures->Max_Write_DataBlock_Length )
1154 + return AT91C_WRITE_ERROR;
1155 + ////////////////////////////////////////////////////////////////////////////////////////////
1157 + // Init Mode Register
1158 + AT91C_BASE_MCI->MCI_MR |= ((pMCI_Device->pMCI_DeviceFeatures->Max_Write_DataBlock_Length << 16) | AT91C_MCI_PDCMODE);
1160 + if (sizeToWrite %4)
1161 + sizeToWrite = (sizeToWrite /4)+1;
1163 + sizeToWrite = sizeToWrite/4;
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;
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;
1174 + pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_TX_SINGLE_BLOCK;
1176 + // Enable AT91C_MCI_TXBUFE Interrupt
1177 + AT91C_BASE_MCI->MCI_IER = AT91C_MCI_TXBUFE;
1179 + // Enables TX for PDC transfert requests
1180 + AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_TXTEN;
1182 + return AT91C_WRITE_OK;
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)
1195 + //* Check if the MMC card chosen is already the selected one
1196 + status = AT91F_MCI_GetStatus(pMCI_Device,relative_card_address);
1199 + return AT91C_CARD_SELECTED_ERROR;
1201 + if ((status & AT91C_SR_CARD_SELECTED) == AT91C_SR_CARD_SELECTED)
1202 + return AT91C_CARD_SELECTED_OK;
1204 + //* Search for the MMC Card to be selected, status = the Corresponding Device Number
1206 + while( (pMCI_Device->pMCI_DeviceFeatures[status].Relative_Card_Address != relative_card_address)
1207 + && (status < AT91C_MAX_MCI_CARDS) )
1210 + if (status > AT91C_MAX_MCI_CARDS)
1211 + return AT91C_CARD_SELECTED_ERROR;
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;
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)
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;
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];
1238 + return AT91C_CMD_SEND_OK;
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)
1247 + return( AT91F_MCI_SendCommand(pMCI_Device, AT91C_SET_BLOCKLEN_CMD, length) );
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)
1257 + unsigned int response =0x0;
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;
1267 + response = AT91C_BASE_MCI->MCI_RSPR[0];
1269 + if ( (response & AT91C_CARD_POWER_UP_BUSY) == AT91C_CARD_POWER_UP_BUSY)
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)
1282 + int Nb_Cards_Found=-1;
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;
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;
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;
1303 + //* If no error during assignation address ==> Increment Nb_cards_Found
1304 + Nb_Cards_Found++ ;
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)
1316 + unsigned int tab_response[4];
1317 + unsigned int mult,blocknr;
1318 + unsigned int i,Nb_Cards_Found=0;
1320 + //* Resets all MMC Cards in Idle state
1321 + AT91F_MCI_SendCommand(pMCI_Device, AT91C_MMC_GO_IDLE_STATE_CMD, AT91C_NO_ARGUMENT);
1323 + if(AT91F_MCI_MMC_GetAllOCR(pMCI_Device) == AT91C_INIT_ERROR)
1324 + return AT91C_INIT_ERROR;
1326 + Nb_Cards_Found = AT91F_MCI_MMC_GetAllCID(pMCI_Device,tab_response);
1327 + if (Nb_Cards_Found != AT91C_CMD_SEND_ERROR)
1329 + //* Set the Mode Register
1330 + AT91C_BASE_MCI->MCI_MR = AT91C_MCI_MR_PDCMODE;
1332 + for(i = 0; i < Nb_Cards_Found; i++)
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;
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;
1346 + // None in MMC specification version 2.2
1347 + pMCI_Device->pMCI_DeviceFeatures[i].Erase_Block_Enable = 0;
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;
1352 + //// Compute Memory Capacity
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 );
1360 + pMCI_Device->pMCI_DeviceFeatures[i].Memory_Capacity = pMCI_Device->pMCI_DeviceFeatures[i].Max_Read_DataBlock_Length * blocknr;
1361 + //// End of Compute Memory Capacity
1366 + return AT91C_INIT_OK;
1369 + return AT91C_INIT_ERROR;
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)
1379 + unsigned int response =0x0;
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;
1384 + while( (response & AT91C_CARD_POWER_UP_BUSY) != AT91C_CARD_POWER_UP_BUSY )
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;
1392 + response = AT91C_BASE_MCI->MCI_RSPR[0];
1395 + return(AT91C_BASE_MCI->MCI_RSPR[0]);
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)
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;
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];
1414 + return AT91C_CMD_SEND_OK;
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)
1423 + volatile int ret_value;
1428 + ret_value =AT91F_MCI_GetStatus(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address);
1430 + while((ret_value > 0) && ((ret_value & AT91C_SR_READY_FOR_DATA) == 0));
1433 + AT91F_MCI_SendCommand(pMCI_Device,
1434 + AT91C_SEL_DESEL_CARD_CMD,
1435 + (pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address)<<16);
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;
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;
1445 + return AT91C_CMD_SEND_OK;
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)
1454 + unsigned int tab_response[4];
1455 + unsigned int mult,blocknr;
1457 + AT91F_MCI_SendCommand(pMCI_Device, AT91C_GO_IDLE_STATE_CMD, AT91C_NO_ARGUMENT);
1459 + if(AT91F_MCI_SDCard_GetOCR(pMCI_Device) == AT91C_INIT_ERROR)
1460 + return AT91C_INIT_ERROR;
1462 + if (AT91F_MCI_SDCard_GetCID(pMCI_Device,tab_response) == AT91C_CMD_SEND_OK)
1464 + pMCI_Device->pMCI_DeviceFeatures->Card_Inserted = AT91C_SD_CARD_INSERTED;
1466 + if (AT91F_MCI_SendCommand(pMCI_Device, AT91C_SET_RELATIVE_ADDR_CMD, 0) == AT91C_CMD_SEND_OK)
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)
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;
1480 + //// Compute Memory Capacity
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 );
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);
1492 + if( AT91F_MCI_SDCard_SetBusWidth(pMCI_Device) == AT91C_CMD_SEND_OK )
1494 + if (AT91F_MCI_SetBlocklength(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length) == AT91C_CMD_SEND_OK)
1495 + return AT91C_INIT_OK;
1500 + return AT91C_INIT_ERROR;
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
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
1519 +//*----------------------------------------------------------------------------
1521 +#include "dataflash.h"
1522 +#include <AT91C_MCI_Device.h>
1524 +#define AT91C_MCI_TIMEOUT 1000000 /* For AT91F_MCIDeviceWaitReady */
1525 +#define BUFFER_SIZE_MCI_DEVICE 512
1526 +#define MASTER_CLOCK 60000000
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;
1542 +unsigned int dlBuffer = 0x20000000;
1545 +char TestString[] = "\r\nHello Hamish\r\n";
1548 +//*----------------------------------------------------------------------------
1549 +//* \fn AT91F_MCIDeviceWaitReady
1550 +//* \brief Wait for MCI Device ready
1551 +//*----------------------------------------------------------------------------
1552 +void AT91F_MCIDeviceWaitReady(unsigned int timeout)
1554 + volatile int status;
1558 + status = AT91C_BASE_MCI->MCI_SR;
1561 + while( !(status & AT91C_MCI_NOTBUSY) && (timeout>0) );
1564 +unsigned int swab32(unsigned int data)
1566 + unsigned int res = 0;
1568 + res = (data & 0x000000ff) << 24 |
1569 + (data & 0x0000ff00) << 8 |
1570 + (data & 0x00ff0000) >> 8 |
1571 + (data & 0xff000000) >> 24;
1576 +AT91S_MCIDeviceStatus readblock(
1577 + AT91PS_MciDevice pMCI_Device,
1579 + unsigned int *databuffer,
1583 + unsigned char *buf = (unsigned char *)databuffer;
1586 + for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++)
1588 + AT91F_MCI_ReadBlock(&MCI_Device,src,databuffer,sizeToRead);
1590 + //* Wait end of Read
1591 + AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
1595 + unsigned int *uiBuffer = databuffer;
1597 + for(index = 0; index < 512/4; index++)
1598 + uiBuffer[index] = swab32(uiBuffer[index]);
1604 +void printdata(unsigned int bufpos)
1606 + unsigned int *uip;
1607 + int linebytes = 16;
1612 + uip = (unsigned int *)bufpos;
1616 + for(i=0; i<linebytes; i+=size) {
1617 + printf(" %08x", *uip++);
1621 + nbytes -= linebytes;
1622 + } while (nbytes > 0);
1625 +//extern char message[40];
1627 +int notnull(int bufpos, unsigned int len)
1630 + unsigned char * bp = (unsigned char *)bufpos;
1632 + for (i=0; i<len; i++)
1633 + if (bp[i] != '\0')
1638 +//*----------------------------------------------------------------------------
1640 +//* \brief Test Functions
1641 +//*----------------------------------------------------------------------------
1642 +int AT91F_Test(void)
1645 + unsigned int Max_Read_DataBlock_Length;
1647 + int bufpos = dlBuffer;
1648 + int lastvalid = 0;
1652 + Max_Read_DataBlock_Length = MCI_Device.pMCI_DeviceFeatures->Max_Read_DataBlock_Length;
1654 + //* ReadBlock & WriteBlock Test -> Entire Block
1656 + //* Wait MCI Device Ready
1657 + AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
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);
1664 + //* Wait end of Read
1665 + AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
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);
1673 + //* Wait end of Write
1674 + AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
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))
1685 + i = dataflash_info[0].Device.pages_number;
1688 + i = lastvalid + (NbPage << 13) + (dataflash_info[0].Device.pages_size << 17);
1689 + *(int *)(dlBuffer + 0x14) = i;
1691 + for(i=0; i<4688; i++) {
1692 + readblock(&MCI_Device, block*Max_Read_DataBlock_Length, (unsigned int *)bufpos, Max_Read_DataBlock_Length);
1696 + write_dataflash(0xc0000000, dlBuffer, 512 * block);
1698 + printf("DONE %d\n\r", lastvalid);
1705 +//*----------------------------------------------------------------------------
1706 +//* \fn AT91F_CfgDevice
1707 +//* \brief This function is used to initialise MMC or SDCard Features
1708 +//*----------------------------------------------------------------------------
1709 +void AT91F_CfgDevice(void)
1711 + // Init Device Structure
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;
1723 + MCI_Device_Desc.state = AT91C_MCI_IDLE;
1724 + MCI_Device_Desc.SDCard_bus_width = AT91C_MCI_SCDBUS;
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;
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)
1738 + //////////////////////////////////////////////////////////
1739 + //* For SDCard Init
1740 + //////////////////////////////////////////////////////////
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);
1747 + if(AT91F_MCI_SDCard_Init(&MCI_Device) != AT91C_INIT_OK)
1750 + printf("\n\rINI OK: TST\n\r");
1752 + // Enter Main Tests
1753 + return(AT91F_Test());
1756 +//*----------------------------------------------------------------------------
1757 +//* \fn AT91F_MCI_Handler
1758 +//* \brief MCI Handler
1759 +//*----------------------------------------------------------------------------
1760 +extern "C" void AT91F_MCI_Handler(void);
1762 +void AT91F_MCI_Handler(void)
1766 + status = ( AT91C_BASE_MCI->MCI_SR & AT91C_BASE_MCI->MCI_IMR );
1768 + AT91F_MCI_Device_Handler(&MCI_Device,status);
1771 +//*----------------------------------------------------------------------------
1773 +//* \brief main function
1774 +//*----------------------------------------------------------------------------
1777 +// printf("MCI Test\n\r");
1779 +///////////////////////////////////////////////////////////////////////////////////////////
1780 +// MCI Init : common to MMC and SDCard
1781 +///////////////////////////////////////////////////////////////////////////////////////////
1783 +// printf("\n\rInit MCI Interface\n\r");
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);
1789 + // Init MCI for MMC and SDCard interface
1790 + AT91F_MCI_CfgPIO();
1791 + AT91F_MCI_CfgPMC();
1792 + AT91F_PDC_Open(AT91C_BASE_PDC_MCI);
1794 + // Disable all the interrupts
1795 + AT91C_BASE_MCI->MCI_IDR = 0xFFFFFFFF;
1797 + // Init MCI Device Structures
1798 + AT91F_CfgDevice();
1800 + // Configure MCI interrupt
1801 + AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
1803 + AT91C_AIC_PRIOR_HIGHEST,
1804 + AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,
1805 + AT91F_ASM_MCI_Handler);
1807 + // Enable MCI interrupt
1808 + AT91F_AIC_EnableIt(AT91C_BASE_AIC,AT91C_ID_MCI);
1810 +///////////////////////////////////////////////////////////////////////////////////////////
1812 +///////////////////////////////////////////////////////////////////////////////////////////
1814 + // Enable Receiver
1815 + AT91F_US_EnableRx((AT91PS_USART) AT91C_BASE_DBGU);
1817 + if(AT91F_Test_SDCard() == TRUE)
1818 + printf("\n\rTST OK\n\r");
1820 + printf("\n\rTST Fail\n\r");