]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/arm/xscale/triton270/v2_0/src/sdcard.c
Initial revision
[karo-tx-redboot.git] / packages / hal / arm / xscale / triton270 / v2_0 / src / sdcard.c
1 //==========================================================================
2 //#####DESCRIPTIONBEGIN####
3 //
4 // Author(s):    usteinkohl
5 // Contributors: usteinkohl
6 //               Uwe Steinkohl <us@karo-electronics.de>
7 // Date:         2005-05-24
8 // Purpose:
9 // Description:
10 //
11 // This code is to handle the SD-Card
12 //
13 //####DESCRIPTIONEND####
14 //
15 //==========================================================================
16
17 #include <redboot.h>
18
19 #include <cyg/hal/hal_triton270.h>
20 #include <cyg/hal/hal_intr.h>
21 #include <cyg/hal/hal_cache.h>
22
23 #include <cyg/hal/hal_io.h>             // IO macros
24
25 #define DMA_ACCESS                      1
26
27 #define SDCARD_DEBUG                    0
28 #define FAT16_ONLY                      0
29
30 #define MMCCLKRT_19M5                   0
31 #define MMCCLKRT_9M75                   1
32 #define MMCCLKRT_4M88                   2
33 #define MMCCLKRT_2M44                   3
34 #define MMCCLKRT_1M22                   4
35 #define MMCCLKRT_0M609                  5
36 #define MMCCLKRT_0M304                  6
37
38 #define MMCSTAT_TIME_OUT_READ           0x00000001
39 #define MMCSTAT_TIME_OUT_RES            0x00000002
40 #define MMCSTAT_CRC_WR_ERR              0x00000004
41 #define MMCSTAT_CRC_RD_ERR              0x00000008
42 #define MMCSTAT_DAT_ERR_TOKEN           0x00000010
43 #define MMCSTAT_RES_CRC_ERR             0x00000020
44
45 #define MMCSTAT_CLK_EN                  0x00000100
46 #define MMCSTAT_FLASH_ERR               0x00000200
47 #define MMCSTAT_SPI_WR_ERR              0x00000400
48 #define MMCSTAT_DATA_TRAN_DONE          0x00000800
49
50 #define MMCSTAT_PRG_DONE                0x00001000
51 #define MMCSTAT_END_CMD_RES             0x00002000
52 #define MMCSTAT_RD_STALLED              0x00004000
53 #define MMCSTAT_SDIO_INT                0x00008000
54
55 #define MMCSTAT_SDIO_SUSPEND_ACK        0x00010000
56
57 #define CMD_RES_TYPE_NORESP             0
58 #define CMD_RES_TYPE_R1                 1
59 #define CMD_RES_TYPE_R6                 1
60 #define CMD_RES_TYPE_R2                 2
61 #define CMD_RES_TYPE_R3                 3
62
63 #define SD_CMD0                         0
64 #define SD_CMD2                         2
65 #define SD_CMD3                         3
66 #define SD_CMD4                         4
67 #define SD_CMD7                         7
68 #define SD_CMD9                         9
69 #define SD_CMD10                        10
70 #define SD_CMD12                        12
71 #define SD_CMD13                        13
72 #define SD_CMD15                        15
73 #define SD_CMD16                        16
74 #define SD_CMD17                        17
75 #define SD_CMD18                        18
76 #define SD_CMD55                        55
77
78 #define SD_ACMD6                        (6 + 128)
79 #define SD_ACMD41                       (41 + 128)
80
81 typedef unsigned char   u8;
82 typedef unsigned short  u16;
83 typedef unsigned long   u32;
84
85 typedef struct command_parameter_struct {
86         unsigned char           command_number;
87         unsigned short          rca;
88         unsigned short          command_parameter_low;
89         unsigned short          command_parameter_high;
90         unsigned char           res_type;
91         unsigned char           data_en;
92         unsigned char           dma_en;
93         unsigned char           data_write;
94         unsigned char           stream_blk;
95         unsigned char           busy;
96         unsigned char           init;
97         unsigned char           sd_4dat;
98         unsigned char           *rbuffer;                       // pointer to response buffer
99         unsigned char           *dbuffer;                       // pointer to data buffer
100         int                     isWinceImage;
101 } COMPAR;
102
103 typedef struct drive_spec_t {
104         u32                     partition_size;
105         u32                     partition_offset;
106         u8                      partition_type;
107         u16                     number_of_cylinders;
108         u16                     number_of_heads;
109         u16                     number_of_sectors_per_track;
110         u16                     bytes_per_sector;
111         u8                      sectors_per_cluster;
112         u16                     reserved_sectors;
113         u8                      number_of_copies_of_fat;
114         u16                     maximum_root_entries;
115         u16                     sectors_per_fat;
116         u32                     fat_start_block;
117         u32                     root_dir_start_block;
118         u32                     data_area_start_block;
119         u16                     number_of_files;
120         int                     fat16;
121 } drive_spec;
122
123 typedef struct dir_data_t {
124         char            dir_fname[13];
125         u16                     dir_start_cluster;
126         u32                     dir_fsize;
127         u16                     dir_year;
128         u8                      dir_month;
129         u8                      dir_day;
130         u8                      dir_second;
131         u8                      dir_minute;
132         u8                      dir_hour;
133 } dir_data;
134
135 int     init_sdcard(COMPAR *pp);
136 void    enable_sdcard_power(void);
137 int     chk_sd_status(COMPAR *pp);
138 int     send_command(COMPAR *pp);
139 int             wait4cmd_end(void);
140 int     read_single_block(COMPAR *pp, unsigned long address);
141 void    dump_buf(unsigned char *address, unsigned short length);
142 int     get_dir_data(drive_spec *ds, COMPAR *pp);
143 void    dump_dir(dir_data *dird, drive_spec *ds);
144 int     get_dir_entry(dir_data  *dentry, drive_spec *ds, COMPAR *pp, unsigned char *filename);
145 void    kill_space(char *string);
146 int     read_multiple_block(COMPAR *pp, unsigned long address, unsigned long blocks);
147
148 int     read_file(dir_data *dird, drive_spec *ds, unsigned long *address, COMPAR *pp);
149 int     get_next_cluster(dir_data *dird, drive_spec *ds,  COMPAR *pp, unsigned short last_cluster);
150
151 int     isprint(int character);
152
153 RedBoot_cmd("sdcard",
154             "read from SD-Card",
155             "[-d] [-f <filename>] [-b <base_address]",
156             do_sdcard
157             );
158
159 static inline unsigned long cmd_value(COMPAR *pp)
160 {
161         unsigned long cvalue;
162
163         cvalue = (((pp->res_type & 0x3) << 0) |
164                   ((pp->data_en & 0x1) << 2) |
165                   ((pp->data_write & 0x1) << 3) |
166                   ((pp->stream_blk & 0x1) << 4) |
167                   ((pp->busy & 0x1) << 5) |
168                   ((pp->init & 0x1) << 6) |
169                   ((pp->dma_en & 0x1) << 7) |
170                   ((pp->sd_4dat & 0x1) << 8));
171         return cvalue;
172 }
173
174 void do_sdcard(int argc, char *argv[])
175 {
176         struct option_info opts[3];
177         bool dflag_set, base_address_set, filename_set;
178         unsigned char *fn_buffer;
179         unsigned long base_address;
180         int dflag;
181         static COMPAR   cpar;
182         static unsigned char    rbuffer[32];
183         static unsigned char    dbuffer[512];
184         drive_spec      my_drive;
185         drive_spec      *ds = &my_drive;
186         u16             *p16 = (u16 *)dbuffer;
187         int             valid_partition;
188         dir_data        dentry;
189         int i;
190
191         init_opts(&opts[0], 'd', false, OPTION_ARG_TYPE_FLG, (void *)&dflag,
192                   &dflag_set, "directory");
193         init_opts(&opts[1], 'b', true, OPTION_ARG_TYPE_NUM, (void *)&base_address,
194                   &base_address_set, "base address to load file contents");
195         init_opts(&opts[2], 'f', true, OPTION_ARG_TYPE_STR, (void *)&fn_buffer,
196                   &filename_set, "filename");
197
198         if (!scan_opts(argc, argv, 1, opts, 3, 0, 0, "")) {
199                 return;
200         }
201         if (dflag & base_address_set) {
202                 diag_printf("warning, -b parameter is ignored\n");
203         }
204
205         if (dflag & filename_set) {
206                 diag_printf("warning, -f parameter is ignored\n");
207         }
208
209 #ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
210         // Do not check for the base address now since
211         // possibly we are about to load a wince .BIN image.
212         // We will validate it latter instead.
213
214         if (!dflag && !filename_set) {
215                 diag_printf("error, filename must be specified with -f parameter\n");
216                 return;
217         }
218
219         if (!base_address_set) {
220                 base_address = 0;
221         }
222 #else
223         if (base_address_set & !filename_set) {
224                 diag_printf("error, filename must be specified with -f parameter\n");
225                 return;
226         }
227
228         if (!base_address_set & filename_set) {
229                 diag_printf("error, base address must be specified with -b parameter\n");
230                 return;
231         }
232 #endif
233         cpar.rbuffer = rbuffer;         // specify buffer for command response
234         cpar.dbuffer = dbuffer;         // specify buffer for data transfer
235
236         if (dflag) {
237                 if (chk_sd_status(&cpar) < 0) {
238                         return;
239                 }
240 #if SDCARD_DEBUG
241                 diag_printf("\n\nreading master boot record now\n");
242 #endif
243                 if (read_single_block(&cpar, 0) < 0) {
244                         diag_printf("read single block command failed!\n");
245                         return;
246                 }
247 #if SDCARD_DEBUG
248                 diag_printf("\n\n");
249                 dump_buf(dbuffer, 512);
250 #endif
251                 if (*(p16 + 255) != 0xaa55) {
252                         diag_printf("warning: no patition table found on drive\n");
253                         diag_printf("exiting command\n");
254                         return;
255                 }
256
257                 //search for first FAT16 partition
258                 valid_partition = 0;
259                 for (i = 0; i < 4; i++) {
260                         ds->partition_size = dbuffer[446 + i * 16 + 12] +
261                                 (dbuffer[446 + i * 16 + 13] << 8) +
262                                 (dbuffer[446 + i * 16 + 14] << 16) +
263                                 (dbuffer[446 + i * 16 + 15] << 24);
264
265                         ds->partition_offset = dbuffer[446 + i * 16 + 8] +
266                                 (dbuffer[446 + i * 16 + 9] << 8) +
267                                 (dbuffer[446 + i * 16 + 10] << 16) +
268                                 (dbuffer[446 + i * 16 + 11] << 24);
269
270                         ds->partition_type = dbuffer[446 + i * 16 + 4];
271
272                         if ((ds->partition_type != 0x01 &&
273                              ds->partition_type != 0x04 &&
274                              ds->partition_type != 0x05 &&
275                              ds->partition_type != 0x06) ||
276                             ds->partition_size == 0 || ds->partition_offset == 0) {
277                                 continue;
278                         } else {
279                                 valid_partition = i + 1;
280                                 break;
281                         }
282                 }
283
284                 if (!valid_partition) {
285                         diag_printf("warning, no valid partiton table found\n");
286                         ds->partition_offset = 0;
287                 } else {
288 #if SDCARD_DEBUG
289                         diag_printf("Partition Size: 0x%08X\n", ds->partition_size);
290                         diag_printf("Partition Offset: 0x%08X\n", ds->partition_offset);
291                         diag_printf("Partition Type: 0x%08X\n", ds->partition_type);
292 #endif
293                 }
294
295                 if (read_single_block(&cpar, ds->partition_offset * 512) < 0) {
296                         diag_printf("read single block command failed!\n");
297                         return;
298                 }
299 #if SDCARD_DEBUG
300                 diag_printf("\n\nreading FAT boot record now:\n");
301                 dump_buf(dbuffer, 512);
302 #endif
303 #if !FAT16_ONLY
304                 if (dbuffer[0x36] =='F' &&
305                     dbuffer[0x37] == 'A' &&
306                     dbuffer[0x38] == 'T' &&
307                     dbuffer[0x39] == '1' &&
308                     (dbuffer[0x3A] == '2' || dbuffer[0x3A] == '6')) {
309                         if (dbuffer[0x3A] == '6') {
310 #if SDCARD_DEBUG
311                                 diag_printf("FAT16 found\n");
312 #endif
313                                 ds->fat16 = 1;
314                         } else {
315                                 diag_printf("FAT12 found\n");
316                                 ds->fat16 = 0;
317                         }
318                 } else {
319                         diag_printf("no valid FAT entry found\n");
320                         while (1);
321                 }
322 #else
323                 ds->fat16 = 1;
324 #endif
325                 ds->bytes_per_sector = dbuffer[0x0b] + (dbuffer[0x0c] << 8);
326                 ds->sectors_per_cluster = dbuffer[0x0d];
327                 ds->reserved_sectors = dbuffer[0x0e] + (dbuffer[0x0f] << 8);
328                 ds->number_of_copies_of_fat = dbuffer[0x10];
329                 ds->maximum_root_entries = dbuffer[0x11] + (dbuffer[0x12] << 8);
330                 ds->sectors_per_fat = dbuffer[0x16] + (dbuffer[0x17] << 8);
331
332                 ds->fat_start_block = ds->partition_offset + ds->reserved_sectors;
333                 ds->root_dir_start_block = ds->fat_start_block + ds->sectors_per_fat * ds->number_of_copies_of_fat;
334                 ds->data_area_start_block = ds->root_dir_start_block + ds->maximum_root_entries * 32 / ds->bytes_per_sector;
335                 ds->number_of_files = 0;
336 #if SDCARD_DEBUG
337                 diag_printf("partition size is: .................... %lu blocks\n",
338                             ds->partition_size);
339                 diag_printf("partition offset is: .................. %lu blocks\n",
340                             ds->partition_offset);
341
342                 diag_printf("bytes per sector ...................... %d\n",
343                             ds->bytes_per_sector);
344                 diag_printf("sectors per cluster ................... %d\n",
345                             ds->sectors_per_cluster);
346                 diag_printf("reserved sectors found ................ %d\n",
347                             ds->reserved_sectors);
348                 diag_printf("number of FAT copies .................. %d\n",
349                             ds->number_of_copies_of_fat);
350                 diag_printf("maximum entries in root file system ... %d\n",
351                             ds->maximum_root_entries);
352                 diag_printf("sectors per fat ....................... %d\n",
353                             ds->sectors_per_fat);
354
355                 diag_printf("FAT starts at block ................... %lu\n",
356                             ds->fat_start_block);
357                 diag_printf("Root DIR starts at block .............. %lu\n",
358                             ds->root_dir_start_block);
359                 diag_printf("Data (Cluster 2) starts at block ...... %lu\n",
360                             ds->data_area_start_block);
361 #endif
362                 if (get_dir_data(ds, &cpar) < 0) {
363                         diag_printf("failed to read directory entries !\n");
364                         return;
365                 }
366                 diag_printf("\n\n%d files found in directory\n", ds->number_of_files);
367         }
368
369         if (filename_set) {
370                 if (chk_sd_status(&cpar) < 0) {
371                         return;
372                 }
373 #if SDCARD_DEBUG
374                 diag_printf("\n\nreading master boot record now\n");
375 #endif
376                 if (read_single_block(&cpar, 0) < 0) {
377                         diag_printf("read single block command failed!\n");
378                         return;
379                 }
380 #if SDCARD_DEBUG
381                 diag_printf("\n\n");
382                 dump_buf(dbuffer, 512);
383 #endif
384                 if (*(p16 + 255) != 0xaa55) {
385                         diag_printf("warning: no patition table found on drive\n");
386                         return;
387                 }
388
389                 //search for first FAT16 partition
390                 valid_partition = 0;
391                 for (i = 0; i < 4; i++) {
392                         ds->partition_size = dbuffer[446 + i * 16 + 12] + (dbuffer[446 + i * 16 + 13] << 8)
393                                 + (dbuffer[446 + i * 16 + 14] << 16) + (dbuffer[446 + i * 16 + 15] << 24);
394
395                         ds->partition_offset = dbuffer[446 + i * 16 + 8] + (dbuffer[446 + i * 16 + 9] << 8)
396                                 + (dbuffer[446 + i * 16 + 10] << 16) + (dbuffer[446 + i * 16 + 11] << 24);
397
398                         ds->partition_type = dbuffer[446 + i * 16 + 4];
399
400                         if ((ds->partition_type != 0x01 &&
401                              ds->partition_type != 0x04 &&
402                              ds->partition_type != 0x05 &&
403                              ds->partition_type != 0x06) ||
404                             ds->partition_size == 0 || ds->partition_offset == 0) {
405                                 continue;
406                         } else {
407                                 valid_partition = i + 1;
408                                 break;
409                         }
410                 }
411                 if (!valid_partition) {
412                         diag_printf("warning, no valid partiton table found\n");
413                         ds->partition_offset = 0;
414                 }
415                 if (read_single_block(&cpar, ds->partition_offset * 512) < 0) {
416                         diag_printf("read single block command failed!\n");
417                         return;
418                 }
419 #if SDCARD_DEBUG
420                 diag_printf("\n\nreading FAT boot record now:\n");
421                 dump_buf(dbuffer, 512);
422 #endif
423 #if !FAT16_ONLY
424                 if (dbuffer[0x36] == 'F' &&
425                     dbuffer[0x37] == 'A' &&
426                     dbuffer[0x38] == 'T' &&
427                     dbuffer[0x39] == '1' &&
428                     (dbuffer[0x3A] == '2' || dbuffer[0x3A] == '6')) {
429                         if (dbuffer[0x3A] == '6') {
430 #if SDCARD_DEBUG
431                                 diag_printf("FAT16 found\n");
432 #endif
433                                 ds->fat16 = 1;
434                         } else {
435                                 diag_printf("FAT12 found\n");
436                                 ds->fat16 = 0;
437                         }
438                 } else {
439                         diag_printf("no valid FAT entry found\n");
440                         while (1);
441                 }
442 #else
443                 ds->fat16 = 1;
444 #endif
445                 ds->bytes_per_sector = dbuffer[0x0b] + (dbuffer[0x0c] << 8);
446                 ds->sectors_per_cluster = dbuffer[0x0d];
447                 ds->reserved_sectors = dbuffer[0x0e] + (dbuffer[0x0f] << 8);
448                 ds->number_of_copies_of_fat = dbuffer[0x10];
449                 ds->maximum_root_entries = dbuffer[0x11] + (dbuffer[0x12] << 8);
450                 ds->sectors_per_fat = dbuffer[0x16] + (dbuffer[0x17] << 8);
451
452                 ds->fat_start_block = ds->partition_offset + ds->reserved_sectors;
453                 ds->root_dir_start_block = ds->fat_start_block + ds->sectors_per_fat * ds->number_of_copies_of_fat;
454                 ds->data_area_start_block = ds->root_dir_start_block + ds->maximum_root_entries * 32 / ds->bytes_per_sector;
455                 ds->number_of_files = 0;
456 #if SDCARD_DEBUG
457                 diag_printf("partition size is: .................... %lu blocks\n",
458                             ds->partition_size);
459                 diag_printf("partition offset is: .................. %lu blocks\n",
460                             ds->partition_offset);
461
462                 diag_printf("bytes per sector ...................... %d\n",
463                             ds->bytes_per_sector);
464                 diag_printf("sectors per cluster ................... %d\n",
465                             ds->sectors_per_cluster);
466                 diag_printf("reserved sectors found ................ %d\n",
467                             ds->reserved_sectors);
468                 diag_printf("number of FAT copies .................. %d\n",
469                             ds->number_of_copies_of_fat);
470                 diag_printf("maximum entries in root file system ... %d\n",
471                             ds->maximum_root_entries);
472                 diag_printf("sectors per fat ....................... %d\n",
473                             ds->sectors_per_fat);
474
475                 diag_printf("FAT starts at block ................... %lu\n",
476                             ds->fat_start_block);
477                 diag_printf("Root DIR starts at block .............. %lu\n",
478                             ds->root_dir_start_block);
479                 diag_printf("Data (Cluster 2) starts at block ...... %lu\n",
480                             ds->data_area_start_block);
481 #endif
482                 if (get_dir_entry(&dentry, ds, &cpar, fn_buffer) < 0) {
483                         diag_printf("file %s not found !\n", dentry.dir_fname);
484                         return;
485                 }
486
487                 if (read_file(&dentry, ds, (unsigned long *)base_address, &cpar) < 0) {
488                         diag_printf("failed to read file %s\n", dentry.dir_fname);
489                         return;
490                 }
491 #ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
492                 if (!cpar.isWinceImage) {
493                         diag_printf("\n%s loaded       address: 0x%08X      size: %10lu bytes\n\n",
494                                     dentry.dir_fname, base_address, dentry.dir_fsize);
495                 }
496 #else
497                 diag_printf("\n%s loaded       address: 0x%08X      size: %10lu bytes\n\n",
498                             dentry.dir_fname, base_address, dentry.dir_fsize);
499 #endif
500                 return;
501         }
502 }
503
504 int chk_sd_status(COMPAR *pp)
505 {
506         unsigned char   rbuffer[32];
507 #if SDCARD_DEBUG
508         int i;
509 #endif
510
511         pp->command_number = SD_CMD13;
512         if (send_command(pp) < 0) {
513                 diag_printf("initialising SD-Card interface\n");
514                 if (init_sdcard(pp) < 0) {
515                         diag_printf("card initialisation failed !\n");
516                         return -1;
517                 } else {
518                         return 0;
519                 }
520         }
521 #if SDCARD_DEBUG
522         diag_printf("Response got from CMD13: ");
523         for (i = 0; i < 6; i++) {
524                 diag_printf("%02X ", rbuffer[i]);
525         }
526         diag_printf("\n\n\n");
527 #endif
528         if (rbuffer[3] != 0x09 && (rbuffer[2] & 0x01) != 0x00) {
529                 diag_printf("warning, Card is not in the Transfer state as expected !\n");
530                 diag_printf("initialising SD-Card interface\n");
531                 if (init_sdcard(pp) < 0) {
532                         diag_printf("card initialisation failed !\n");
533                         return -1;
534                 } else {
535                         return 0;
536                 }
537         }
538         return 0;
539
540 }
541
542 int init_sdcard(COMPAR *pp)
543 {
544         unsigned long   value;
545         int             timeout;
546         unsigned char   tbuffer[6];
547         unsigned short  newrca;
548         unsigned long   trans_speed;
549         unsigned long   read_bl_len;
550         unsigned long   c_size;
551         unsigned long   c_size_mult;
552         unsigned long   sector_size;
553         unsigned long   conbuf[3];
554         unsigned long   mult;
555         unsigned long   blen;
556         unsigned long   card_size;
557         unsigned long   trans_rate;
558         int             i;
559         unsigned long   trans_rate_exp[8] = {100, 1000, 10000, 100000, 0, 0, 0, 0};
560         unsigned short  time_mantissa[16] = {0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80};
561
562         enable_sdcard_power();
563
564         pp->rca = 0;                    // relative card address
565
566         HAL_READ_UINT32(ICMR, value);
567         value &= (~0x00800000);
568         HAL_WRITE_UINT32(ICMR, value);  // mask interrupt source SD-card controller
569
570         HAL_WRITE_UINT32(MMCCLKRT, MMCCLKRT_0M304);     // set clock rate to lowest rate first
571         HAL_WRITE_UINT32(MMCSPI, 0);    // do not use SPI mode
572         HAL_WRITE_UINT32(MMCRESTO, 0x7f); // set to maximum timeout
573         HAL_WRITE_UINT32(MMCBLKLEN, 512);
574         HAL_WRITE_UINT32(MMCNOB, 1);
575
576         // start the clock now
577         HAL_WRITE_UINT32(MMCSTRPCL, 2); // start MMCLK
578         HAL_READ_UINT32(MMCSTRPCL, value);
579         while (value & 2) {             // wait until clock is started
580                 HAL_DELAY_US(1000);
581                 HAL_READ_UINT32(MMCSTRPCL, value);
582         }
583         HAL_DELAY_US(100 * 1000);
584
585         pp->command_number = SD_CMD0;
586         if (send_command(pp) < 0) {
587                 diag_printf("CMD0 failed, repeating command \n");
588                 return -1;
589         }
590
591         timeout = 100;
592         pp->command_number = SD_ACMD41;
593         pp->command_parameter_high = 0x001f;    // 2,8V - 3,3V
594
595         while (timeout > 0) {
596                 if (send_command(pp) < 0) {
597                         HAL_DELAY_US(1000);
598                         timeout--;
599                         continue;
600                 }
601                 if (!(pp->rbuffer[1] & 0x80)) {  // card not powered up
602                         HAL_DELAY_US(1000);
603                         timeout--;
604                         continue;
605                 } else {
606                         break;
607                 }
608         }
609         if (timeout == 0) {
610                 diag_printf("failed to power up card, ACMD41 failed\n");
611                 return -1;
612         }
613 #if SDCARD_DEBUG
614         diag_printf("Response got from ACMD41: ");
615         for (i = 0; i < 6; i++) {
616                 diag_printf("%02X ", pp->rbuffer[i]);
617         }
618         diag_printf("\n\n\n");
619 #endif
620         pp->command_number = SD_CMD2;
621         if (send_command(pp) < 0) {
622                 diag_printf("CMD2 failed !!!\n");
623                 return -1;
624         }
625 #if SDCARD_DEBUG
626         diag_printf("Response got from CMD2: ");
627         for (i = 0; i < 17; i++) {
628                 diag_printf("%02X ", pp->rbuffer[i]);
629         }
630         diag_printf("\n\n\n");
631 #endif
632         for (i = 0; i < 5; i++) {
633                 tbuffer[i] = pp->rbuffer[i + 4];
634         }
635         tbuffer[5] = '\0';
636
637         diag_printf("found SD-Card: %s\n", tbuffer);
638
639         pp->command_number = SD_CMD3;
640         if (send_command(pp) < 0) {
641                 diag_printf("CMD3 failed !!!\n");
642                 return -1;
643         }
644 #if SDCARD_DEBUG
645         diag_printf("Response got from CMD3: ");
646         for (i = 0; i < 6; i++) {
647                 diag_printf("%02X ", pp->rbuffer[i]);
648         }
649         diag_printf("\n\n\n");
650 #endif
651         newrca = 0;
652         newrca = (pp->rbuffer[1] << 8) | pp->rbuffer[2];
653 #if 1
654         diag_printf("new RCA is: %04X\n", newrca);
655 #endif
656         pp->rca = newrca;                       // relative card address
657
658 //////////////////////////////////////////////////////////////////////////////////
659 // SEND_CSD Command
660 //
661 // get clock information of CSD register
662 //
663 //////////////////////////////////////////////////////////////////////////////////
664
665         pp->command_number = SD_CMD9;
666         if (send_command(pp) < 0) {
667                 diag_printf("CMD9 (SEND_CSD) failed !!!\n");
668                 return -1;
669         }
670 #if SDCARD_DEBUG
671         diag_printf("Response got from CMD9 (SEND_CSD): ");
672         for (i = 0; i < 17; i++) {
673                 diag_printf("%02X ", pp->rbuffer[i]);
674         }
675         diag_printf("\n\n\n");
676 #endif
677         trans_speed = pp->rbuffer[4];
678         read_bl_len = pp->rbuffer[6] & 0x0f;
679         conbuf[0] = (unsigned long)(pp->rbuffer[9] & 0xc0);
680         conbuf[1] = (unsigned long)(pp->rbuffer[8] & 0xff);
681         conbuf[2] = (unsigned long)(pp->rbuffer[7] & 0x03);
682         c_size = (conbuf[0] >> 6) | (conbuf[1] << 2)  | (conbuf[2] << 10);
683         conbuf[0] = (pp->rbuffer[11] & 0x80);
684         conbuf[1] = (pp->rbuffer[10] & 0x03);
685         c_size_mult = conbuf[0] >> 7 | conbuf[1] << 1;
686         conbuf[0] = (pp->rbuffer[12] & 0x80);
687         conbuf[1] = (pp->rbuffer[11] & 0x3f);
688         sector_size = conbuf[0] >> 7 | conbuf[1] << 1;
689
690         for (i = 0, mult = 4; i < c_size_mult; i++)
691                 mult *= 2;
692
693         for (i = 0, blen = 1; i < read_bl_len; i++)
694                 blen *= 2;
695
696         card_size = (c_size + 1) * mult * blen;
697         diag_printf("total card capacity is %lu.%03lu MBytes\n",
698                     card_size / 1024 / 1024, card_size / 1024 % 1024);
699
700         conbuf[0] = (unsigned long)trans_speed & 0x07;
701         conbuf[1] = ((unsigned long)(trans_speed & 0x78)) >> 3;
702
703         trans_rate = time_mantissa[conbuf[1]] * trans_rate_exp[conbuf[0]] / 10; // in kbit/sec
704
705         diag_printf("transfer rate of card is %lu kbit/sec\n", trans_rate);
706
707         if (trans_rate >= 19500) {
708                 diag_printf("set interface clock rate to 19,5 MHz\n\n");
709                 HAL_WRITE_UINT32(MMCCLKRT, MMCCLKRT_19M5);
710         } else if (trans_rate >= 9750) {
711                 diag_printf("set interface clock rate to 9,75 MHz\n\n");
712                 HAL_WRITE_UINT32(MMCCLKRT, MMCCLKRT_9M75);
713         } else if (trans_rate >= 4880) {
714                 diag_printf("set interface clock rate to 4,88 MHz\n\n");
715                 HAL_WRITE_UINT32(MMCCLKRT, MMCCLKRT_4M88);
716         } else if (trans_rate >= 2440) {
717                 diag_printf("set interface clock rate to 2,44 MHz\n\n");
718                 HAL_WRITE_UINT32(MMCCLKRT, MMCCLKRT_2M44);
719         } else if (trans_rate >= 1220) {
720                 diag_printf("set interface clock rate to 1,22 MHz\n\n");
721                 HAL_WRITE_UINT32(MMCCLKRT, MMCCLKRT_1M22);
722         } else if (trans_rate >= 609) {
723                 diag_printf("set interface clock rate to 609 kHz\n\n");
724                 HAL_WRITE_UINT32(MMCCLKRT, MMCCLKRT_0M609);
725         }
726 /////////////////////////////////////////////////////////////////////////////////
727         pp->command_number = SD_CMD7;
728         if (send_command(pp) < 0) {
729                 diag_printf("CMD7 failed !!!\n");
730                 return -1;
731         }
732 #if SDCARD_DEBUG
733         diag_printf("Response got from CMD7: ");
734         for (i = 0; i < 6; i++) {
735                 diag_printf("%02X ", pp->rbuffer[i]);
736         }
737         diag_printf("\n\n\n");
738 #endif
739         pp->command_number = SD_CMD13;
740         if (send_command(pp) < 0) {
741                 diag_printf("CMD13 failed !!!\n");
742                 return -1;
743         }
744 #if SDCARD_DEBUG
745         diag_printf("Response got from CMD13: ");
746         for (i = 0; i < 6; i++) {
747                 diag_printf("%02X ", pp->rbuffer[i]);
748         }
749         diag_printf("\n\n\n");
750 #endif
751         if (pp->rbuffer[3] != 0x09 && (pp->rbuffer[2] & 0x01) != 0x00) {
752                 diag_printf("error, Card is not in the Transfer state as expected !\n");
753                 return -1;
754         }
755
756         pp->command_number = SD_ACMD6;
757         if (send_command(pp) < 0) {
758                 diag_printf("ACMD6 failed !!!\n");
759                 return -1;
760         }
761
762         return 1;
763
764 }
765
766 int send_command(COMPAR *pp)
767 {
768         unsigned long cvalue;
769         int i;
770         unsigned short val;
771         unsigned char   *cp;
772         unsigned char   cnumber;
773
774         //diag_printf("processing CMD: %d\n", pp->command_number);
775         cnumber = pp->command_number;
776         switch (pp->command_number) {
777                 case SD_CMD0:
778                         pp->command_parameter_low = 0;  // stuff bits
779                         pp->command_parameter_high = 0; // RCA Relative Card Address;
780                         pp->res_type = CMD_RES_TYPE_NORESP;
781                         pp->data_en = 0;
782                         pp->dma_en = 0;
783                         pp->data_write = 0;
784                         pp->stream_blk = 0;
785                         pp->busy = 0;
786                         pp->init = 0;
787                         pp->sd_4dat = 1;
788                         break;
789                 case SD_CMD2:
790                         pp->command_parameter_low = 0;
791                         pp->command_parameter_high = 0;
792                         pp->res_type = CMD_RES_TYPE_R2;
793                         pp->data_en = 0;
794                         pp->dma_en = 0;
795                         pp->data_write = 0;
796                         pp->stream_blk = 0;
797                         pp->busy = 0;
798                         pp->init = 0;
799                         pp->sd_4dat = 1;
800                         break;
801                 case SD_CMD3:
802                         pp->command_parameter_low = 0;  // stuff bits
803                         pp->command_parameter_high = 0; // stuff bits
804                         pp->res_type = CMD_RES_TYPE_R6;
805                         pp->data_en = 0;
806                         pp->dma_en = 0;
807                         pp->data_write = 0;
808                         pp->stream_blk = 0;
809                         pp->busy = 0;
810                         pp->init = 0;
811                         pp->sd_4dat = 1;
812                         break;
813                 case SD_CMD7:
814                         pp->command_parameter_low = 0;
815                         pp->command_parameter_high = pp->rca;
816                         pp->res_type = CMD_RES_TYPE_R1;
817                         pp->dma_en = 0;
818                         pp->data_en = 0;
819                         pp->data_write = 0;
820                         pp->stream_blk = 0;
821                         pp->busy = 0;
822                         pp->init = 0;
823                         pp->sd_4dat = 1;
824                         break;
825                 case SD_CMD9:           // SEND_CSD
826                         pp->command_parameter_low = 0;
827                         pp->command_parameter_high = pp->rca;
828                         pp->res_type = CMD_RES_TYPE_R2;
829                         pp->dma_en = 0;
830                         pp->data_en = 0;
831                         pp->data_write = 0;
832                         pp->stream_blk = 0;
833                         pp->busy = 0;
834                         pp->init = 0;
835                         pp->sd_4dat = 1;
836                         break;
837                 case SD_CMD12:          // Stop Transmission
838                         pp->command_parameter_low = 0;
839                         pp->command_parameter_high = 0;
840                         pp->res_type = CMD_RES_TYPE_R1;
841                         pp->dma_en = 0;
842                         pp->data_en = 0;
843                         pp->data_write = 0;
844                         pp->stream_blk = 0;
845                         pp->busy = 0;
846                         pp->init = 0;
847                         pp->sd_4dat = 1;
848                         break;
849                 case SD_CMD13:
850                         pp->command_parameter_low = 0;
851                         pp->command_parameter_high = pp->rca;
852                         pp->res_type = CMD_RES_TYPE_R1;
853                         pp->dma_en = 0;
854                         pp->data_en = 0;
855                         pp->data_write = 0;
856                         pp->stream_blk = 0;
857                         pp->busy = 0;
858                         pp->init = 0;
859                         pp->sd_4dat = 1;
860                         break;
861                 case SD_CMD17:          // READ_SINGLE_BLOCK
862                         pp->res_type = CMD_RES_TYPE_R1;
863                         pp->dma_en = 0;
864                         pp->data_en = 1;
865                         pp->data_write = 0;
866                         pp->stream_blk = 0;
867                         pp->busy = 0;
868                         pp->init = 0;
869                         pp->sd_4dat = 1;
870                         break;
871
872                 case SD_ACMD6:
873                         // we issue a CMD 55 first.
874                         pp->command_parameter_low = 0;  // stuff bits
875                         pp->command_parameter_high = 0; // RCA Relative Card Address;
876                         pp->res_type = CMD_RES_TYPE_R1;
877                         pp->dma_en = 0;
878                         pp->data_en = 0;
879                         pp->data_write = 0;
880                         pp->stream_blk = 0;
881                         pp->busy = 0;
882                         pp->init = 0;
883                         pp->sd_4dat = 1;
884                         HAL_WRITE_UINT32(MMCCMD, 55);   // command number
885                         HAL_WRITE_UINT32(MMCARGH, pp->rca);
886                         HAL_WRITE_UINT32(MMCARGL, pp->command_parameter_low);
887
888                         cvalue = cmd_value(pp);
889                         HAL_WRITE_UINT32(MMCCMDAT, cvalue);
890                         if (wait4cmd_end() < 0) {
891                                 diag_printf("CMD55 failed !!!\n");
892                                 return -1;
893                         }
894
895                         // issue CMD 6 now
896                         pp->command_parameter_low = 2;  // 4 Bit Bus width
897                         pp->command_parameter_high = 0;
898                         pp->res_type = CMD_RES_TYPE_R1;
899                         pp->dma_en = 0;
900                         pp->data_en = 0;
901                         pp->data_write = 0;
902                         pp->stream_blk = 0;
903                         pp->busy = 1;
904                         pp->init = 0;
905                         pp->sd_4dat = 1;
906                         cnumber = 6;
907                         break;
908
909                 case SD_ACMD41:
910                         // we issue a CMD 55 first.
911                         pp->command_parameter_low = 0;  // stuff bits
912                         pp->command_parameter_high = 0; // RCA Relative Card Address;
913                         pp->res_type = CMD_RES_TYPE_R1;
914                         pp->dma_en = 0;
915                         pp->data_en = 0;
916                         pp->data_write = 0;
917                         pp->stream_blk = 0;
918                         pp->busy = 0;
919                         pp->init = 0;
920                         pp->sd_4dat = 1;
921                         HAL_WRITE_UINT32(MMCCMD, 55);   // command number
922                         HAL_WRITE_UINT32(MMCARGH, pp->rca);
923                         HAL_WRITE_UINT32(MMCARGL, pp->command_parameter_low);
924
925                         cvalue = cmd_value(pp);
926                         HAL_WRITE_UINT32(MMCCMDAT, cvalue);
927                         if (wait4cmd_end() < 0) {
928                                 diag_printf("CMD55 failed !!!\n");
929                                 return -1;
930                         }
931
932                         // issue CMD 41 now
933                         pp->command_parameter_low = 0;
934                         pp->command_parameter_high = 0x001f;    // 2,8V - 3,3V
935                         pp->res_type = CMD_RES_TYPE_R3;
936                         pp->dma_en = 0;
937                         pp->data_en = 0;
938                         pp->data_write = 0;
939                         pp->stream_blk = 0;
940                         pp->busy = 1;
941                         pp->init = 0;
942                         pp->sd_4dat = 1;
943                         cnumber = 41;
944                         break;
945                 default:
946 #if SDCARD_DEBUG
947                         diag_printf("found unsupported command: %d\n", pp->command_number);
948 #else
949                         diag_printf("found unsupported command\n\n");
950 #endif
951                         return -1;
952         }
953
954         HAL_WRITE_UINT32(MMCCMD, pp->command_number);   // command number
955         HAL_WRITE_UINT32(MMCARGH, pp->command_parameter_high);  // command parameter upper 16 bits
956         HAL_WRITE_UINT32(MMCARGL, pp->command_parameter_low);   // command parameter lower 16 bits
957
958         cvalue = cmd_value(pp);
959         //diag_printf("writing into command register: 0x%08X\n", cvalue);
960         HAL_WRITE_UINT32(MMCCMDAT, cvalue);
961
962         if (wait4cmd_end() < 0) {
963                 return -1;
964         }
965
966         if (pp->res_type == CMD_RES_TYPE_NORESP) {
967                 return 0;
968         }
969
970         cp = pp->rbuffer;
971         if (pp->res_type == CMD_RES_TYPE_R2) {
972                 for (i = 0; i < 17 / 2; i++) {
973                         HAL_READ_UINT16(MMCRES, val);
974                         *(cp++) = (unsigned char)((val >> 8) & 0xff);
975                         *(cp++) = (unsigned char)(val & 0xff);
976                 }
977         } else {
978                 for (i = 0; i < 6 / 2; i++) {
979                         HAL_READ_UINT16(MMCRES, val);
980                         *(cp++) = (unsigned char)((val >> 8) & 0xff);
981                         *(cp++) = (unsigned char)(val & 0xff);
982                 }
983         }
984         return 0;
985 }
986
987 int wait4cmd_end(void)
988 {
989         int timeout;
990         unsigned long cvalue;
991         unsigned long val;
992
993         timeout = 1000;
994         HAL_READ_UINT32(MMCSTAT, val);
995         while (!(val & MMCSTAT_END_CMD_RES)) {
996                 HAL_DELAY_US(1000);
997                 timeout--;
998                 if (timeout <= 0){
999                         break;
1000                 }
1001                 HAL_READ_UINT32(MMCSTAT, val);
1002         }
1003         if (timeout <= 0) {
1004                 diag_printf("Command timed out, no response from card controller\n");
1005                 //HAL_READ_UINT32(MMCSTAT, val);
1006                 //diag_printf("Status register: 0x%08X\n", val);
1007                 return -1;
1008         }
1009
1010         HAL_READ_UINT32(MMCSTAT, cvalue);
1011
1012         if (cvalue & MMCSTAT_TIME_OUT_RES) {
1013                 diag_printf("Command Response Time Out Error\n");
1014                 return -1;
1015         }
1016
1017         if (cvalue & MMCSTAT_RES_CRC_ERR) {
1018                 diag_printf("Response CRC Error occured\n");
1019                 return -1;
1020         }
1021         return 0;
1022 }
1023
1024 #if DMA_ACCESS                  // DMA Version
1025 int read_single_block(COMPAR *pp, unsigned long address)
1026 {
1027         unsigned long val, cvalue;
1028
1029         HAL_DCACHE_SYNC();
1030         HAL_WRITE_UINT32(MMCNOB, 1);
1031
1032         HAL_WRITE_UINT32(DCSR8, 0x40000000);    // set DMA channel to no descriptor fetch mode
1033
1034         HAL_WRITE_UINT32(DSADR8, 0x41100040);   // SDCARD FIFO
1035         HAL_WRITE_UINT32(DTADR8, (unsigned long)pp->dbuffer); // target address
1036         HAL_WRITE_UINT32(DCMD8, 0x60034200);    // command
1037         HAL_WRITE_UINT32(DCSR8, 0xc0000000);    // run channel
1038
1039         pp->command_parameter_low = address & 0xffff;
1040         pp->command_parameter_high = (address & 0xffff0000) >> 16;
1041
1042         pp->command_number = SD_CMD17;
1043         pp->res_type = CMD_RES_TYPE_R1;
1044         pp->data_en = 1;
1045         pp->dma_en = 1;
1046         pp->data_write = 0;
1047         pp->stream_blk = 0;
1048         pp->busy = 0;
1049         pp->init = 0;
1050         pp->sd_4dat = 1;
1051
1052         HAL_WRITE_UINT32(MMCCMD, pp->command_number);           // command number
1053         HAL_WRITE_UINT32(MMCARGH, pp->command_parameter_high);  // command parameter upper 16 bits
1054         HAL_WRITE_UINT32(MMCARGL, pp->command_parameter_low);   // command parameter lower 16 bits
1055
1056         cvalue = cmd_value(pp);
1057         HAL_WRITE_UINT32(MMCCMDAT, cvalue);
1058
1059         HAL_READ_UINT32(DCMD8, val);            // command
1060         while (val & 0x1fff) {
1061                 HAL_READ_UINT32(DCMD8, val);
1062         }
1063
1064         HAL_WRITE_UINT32(DCSR8, 0x40000000);    // set DMA channel to no descriptor fetch mode
1065 #if 0
1066         HAL_READ_UINT32(MMCIREG, val);
1067         while (!(val & 0x1)) {  // wait for DATA_TRANS_DONE
1068
1069                 HAL_READ_UINT32(MMCIREG, val);
1070         }
1071 #endif
1072         //dump_buf((unsigned char *)pp->dbuffer, 512);
1073
1074         if (wait4cmd_end() < 0) {
1075                 diag_printf("read single block command timed out !!!!");
1076                 return -1;
1077         }
1078 #if 0
1079         HAL_READ_UINT32(DSADR8, val);
1080         diag_printf("DMA source address: 0x%08X\n", val);
1081         HAL_READ_UINT32(DTADR8, val);   // target address
1082         diag_printf("DMA target address: 0x%08X\n", val);
1083
1084         HAL_READ_UINT32(DCMD8, val);    // command
1085         diag_printf("DMA command register: 0x%08X\n", val);
1086
1087         HAL_READ_UINT32(DCSR8, val);
1088         diag_printf("DMA status register: 0x%08X\n", val);
1089 #endif
1090         return 0;
1091
1092 }
1093 #else           // no DMA
1094 int read_single_block(COMPAR *pp, unsigned long address)
1095 {
1096         int length;
1097         int i;
1098         int bp;
1099         unsigned long val, cvalue;
1100         unsigned char val8;
1101         unsigned long   *bap;
1102
1103         HAL_WRITE_UINT32(MMCNOB, 1);
1104
1105         bap = (unsigned long *)pp->dbuffer;
1106
1107         pp->command_parameter_low = address & 0xffff;
1108         pp->command_parameter_high = (address & 0xffff0000) >> 16;
1109
1110         pp->command_number = SD_CMD17;
1111         pp->res_type = CMD_RES_TYPE_R1;
1112         pp->data_en = 1;
1113         pp->dma_en = 0;
1114         pp->data_write = 0;
1115         pp->stream_blk = 0;
1116         pp->busy = 0;
1117         pp->init = 0;
1118         pp->sd_4dat = 1;
1119
1120         HAL_WRITE_UINT32(MMCCMD, pp->command_number);   // command number
1121         HAL_WRITE_UINT32(MMCARGH, pp->command_parameter_high);  // command parameter upper 16 bits
1122         HAL_WRITE_UINT32(MMCARGL, pp->command_parameter_low);   // command parameter lower 16 bits
1123
1124         cvalue = cmd_value(pp);
1125         HAL_WRITE_UINT32(MMCCMDAT, cvalue);
1126
1127         length = 512;
1128         bp = 0;
1129
1130         while (length > 0) {
1131                 HAL_READ_UINT32(MMCIREG, val);
1132                 while (!(val & 0x20)) { // wait for RXFIFO_RD_REQ
1133                         //HAL_DELAY_US(100);
1134
1135                         HAL_READ_UINT32(MMCIREG, val);
1136                 }
1137 #if 0
1138                 for (i = 0; i < 32; i++) {
1139                         HAL_READ_UINT8(MMCRXFIFO, val8);
1140                         pp->dbuffer[bp++] = val8;
1141                 }
1142 #else
1143                 *(bap++) = *(unsigned long *)MMCRXFIFO;
1144                 *(bap++) = *(unsigned long *)MMCRXFIFO;
1145                 *(bap++) = *(unsigned long *)MMCRXFIFO;
1146                 *(bap++) = *(unsigned long *)MMCRXFIFO;
1147                 *(bap++) = *(unsigned long *)MMCRXFIFO;
1148                 *(bap++) = *(unsigned long *)MMCRXFIFO;
1149                 *(bap++) = *(unsigned long *)MMCRXFIFO;
1150                 *(bap++) = *(unsigned long *)MMCRXFIFO;
1151 #endif
1152                 length -=32;
1153         }
1154         if (wait4cmd_end() < 0) {
1155                 diag_printf("read single block command timed out !!!!");
1156                 return -1;
1157         }
1158
1159         return 0;
1160
1161 }
1162 #endif
1163
1164 #if 1
1165 void dump_buf(unsigned char *address, u16 length)
1166 {
1167         u16 lines = length / 16;
1168         u16 rest = length % 16;
1169         int i, j;
1170
1171         for (i = 0; i < lines; i++) {
1172                 for (j = 0; j < 16; j++)
1173                         diag_printf("%02X ", address[i * 16 + j]);
1174                 diag_printf("     ");
1175                 for (j = 0; j < 16; j++) {
1176                         if (isprint(address[i * 16 + j])) {
1177                                 diag_printf("%c", address[i * 16 + j]);
1178                         }
1179                         else {
1180                                 diag_printf(".");
1181                         }
1182                 }
1183                 diag_printf("\n");
1184         }
1185
1186         // behandle Restzeile
1187         for (j = 0; j < rest; j++)
1188                 diag_printf("%02X ", address[i * 16 + j]);
1189         diag_printf("     ");
1190         for (j = 0; j < 16 - rest; j++)
1191                 diag_printf("   ");
1192         for (j = 0; j < rest; j++) {
1193                 if (isprint(address[i * 16 + j])) {
1194                         diag_printf("%c", address[i * 16 + j]);
1195                 }
1196                 else {
1197                         diag_printf(".");
1198                 }
1199         }
1200
1201         diag_printf("\n");
1202
1203 }
1204 #endif
1205
1206 int get_dir_data(drive_spec *ds, COMPAR *pp)
1207 {
1208         u32             actual_block;
1209         u32             block_needed;
1210         u32             entry_offset;
1211         int             i, j, k;
1212         unsigned char   *buffer;
1213         dir_data        dentry;
1214
1215         k = 0;
1216         actual_block = 0;
1217         ds->number_of_files = 0;
1218         buffer = pp->dbuffer;
1219
1220         for (i = 0; i < ds->maximum_root_entries; i++) {
1221                 block_needed = ds->root_dir_start_block + i * 32 / ds->bytes_per_sector;
1222                 if (actual_block != block_needed) {
1223                         if (read_single_block(pp, block_needed * 512) < 0) {
1224                                 diag_printf("read single block command failed!\n");
1225                                 return -1;
1226                         }
1227
1228                         actual_block = block_needed;
1229                 }
1230                 entry_offset = (i * 32) % ds->bytes_per_sector;
1231                 if (               buffer[entry_offset] == 0x00 || buffer[entry_offset] == 0x05
1232                                 || buffer[entry_offset] == 0x2e || buffer[entry_offset] == 0xe5)
1233                                 continue;
1234                 if (buffer[entry_offset + 0x0b] & 0x1e)
1235                         continue;               // hidden system or label
1236
1237                 // copy filename
1238                 for (j = 0;j < 8;j++)
1239                         if (buffer[entry_offset + j] > 0x40 && buffer[entry_offset + j] < 0x5b)
1240                                 (dentry.dir_fname)[j] = buffer[entry_offset + j] + 0x20;
1241                         else
1242                                 (dentry.dir_fname)[j] = buffer[entry_offset + j];
1243
1244                 for (j = 0;j < 3;j++)
1245                         if (buffer[entry_offset + j + 8] > 0x40 && buffer[entry_offset + j + 8] < 0x5b)
1246                                 (dentry.dir_fname)[j + 9] = buffer[entry_offset + j + 8] + 0x20;
1247                         else
1248                                 (dentry.dir_fname)[j + 9] = buffer[entry_offset + j + 8];
1249
1250                 dentry.dir_fname[8] = '.';
1251                 dentry.dir_fname[12] = '\0';
1252                 kill_space(dentry.dir_fname);
1253
1254                 dentry.dir_year = ((buffer[entry_offset + 0x19] & 0xfe) >> 1) + 1980;
1255                 dentry.dir_month = ((buffer[entry_offset + 0x18] & 0xe0) >> 5) + (buffer[entry_offset + 0x19] & 0x01) * 8;
1256                 dentry.dir_day = (buffer[entry_offset + 0x18] & 0x1f);
1257
1258                 dentry.dir_second = (buffer[entry_offset + 0x16] & 0x1f);
1259                 dentry.dir_minute = ((buffer[entry_offset + 0x16] & 0xe0) >> 5) + (buffer[entry_offset + 0x17] & 0x07) * 8;
1260                 dentry.dir_hour = (buffer[entry_offset + 0x17] & 0xf1) >> 3;
1261
1262                 dentry.dir_start_cluster =buffer[entry_offset + 0x1a] + ((buffer[entry_offset + 0x1b]) << 8);
1263                 dentry.dir_fsize = ((buffer[entry_offset + 0x1c]) << 0)
1264                                                                 + ((buffer[entry_offset + 0x1d]) << 8)
1265                                                                 + ((buffer[entry_offset + 0x1e]) << 16)
1266                                                                 + ((buffer[entry_offset + 0x1f]) << 24);
1267
1268                 ds->number_of_files++;
1269                 dump_dir(&dentry, ds);
1270         }
1271         return 0;
1272 }
1273
1274 void dump_dir(dir_data *dird, drive_spec *ds)
1275 {
1276         int j;
1277
1278         diag_printf("%2d: %s", ds->number_of_files, (dird)->dir_fname);
1279         for (j = 0; j < 15 - strlen((dird)->dir_fname); j++) {
1280                 diag_printf(" ");
1281         }
1282         diag_printf("%02d.%02d.%04d    %02d:%02d:%02d    size: %10lu bytes   start cluster: %6u\n",
1283                 (dird)->dir_day,
1284                 (dird)->dir_month,
1285                 (dird)->dir_year,
1286                 (dird)->dir_hour,
1287                 (dird)->dir_minute,
1288                 (dird)->dir_second,
1289                 (dird)->dir_fsize,
1290                 (dird)->dir_start_cluster);
1291 }
1292
1293 int get_next_cluster(dir_data *dird, drive_spec *ds,  COMPAR *pp, unsigned short last_cluster)
1294 {
1295         u16             cluster;
1296         u32             block;
1297         static u16      fat[256];
1298         static u32      actual_fat_block = 0;
1299         u32             fat_block_needed;
1300         u16             fat_entry;
1301         u16             fvalue;
1302
1303         // find next block in fat
1304         fat_block_needed = ds->fat_start_block + last_cluster * 2 / ds->bytes_per_sector;
1305         fat_entry = (last_cluster * 2) % ds->bytes_per_sector;   // this is the offset to the sector start
1306         if (actual_fat_block != fat_block_needed) {
1307 #if SDCARD_DEBUG
1308                 diag_printf("loading fat block %lu\n", fat_block_needed);
1309 #endif
1310                 pp->dbuffer = (unsigned char *)fat;
1311                 if (read_single_block(pp, fat_block_needed * 512) < 0) {
1312                         diag_printf("read single block command failed!\n");
1313                         return -1;
1314                 }
1315
1316                 actual_fat_block = fat_block_needed;
1317 #if SDCARD_DEBUG
1318                 dump_buf((u8 *)fat, 512);
1319 #endif
1320         }
1321
1322         fvalue = fat[fat_entry / 2];
1323 #if SDCARD_DEBUG
1324         diag_printf("cluster is %d\n", cluster);
1325
1326         diag_printf("fat contents of entry %d is: %d\n", fat_entry, fvalue);
1327         diag_printf("length = %lu \n", length);
1328 #endif
1329         if (!fvalue) {
1330                 diag_printf("unexpected NULL pointer in FAT\n");
1331                 return -1;
1332         }
1333         if ((fvalue & 0xfff0) == 0xfff0) {
1334                 //diag_printf("end of file found in FAT\n");
1335                 return 0;
1336         }
1337         cluster = fvalue;
1338         return (int)cluster;
1339
1340         block = (cluster - 2) * ds->sectors_per_cluster + ds->data_area_start_block;
1341
1342 }
1343
1344 int read_file(dir_data *dird, drive_spec *ds, unsigned long *address, COMPAR *pp)
1345 {
1346         u32     length;
1347         u32     block;
1348         u32     *daddr;
1349         u16     fat[256];
1350         u32     actual_fat_block;
1351         u8      *bp;
1352         int     new_cluster;
1353         u32     clusters;
1354         u32     max_clusters;
1355         u16     start_cluster;                  // first cluster of multiple block region
1356         int     got_eof;
1357 #ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
1358         u8      blockBuffer[512];
1359
1360         // Read the first block to determine if we are about
1361         // to load Wince .BIN image
1362
1363         pp->isWinceImage = 0;
1364         pp->dbuffer = blockBuffer;
1365         block = (dird->dir_start_cluster - 2) * ds->sectors_per_cluster + ds->data_area_start_block;
1366
1367         if (read_single_block(pp, block * 512) < 0) {
1368                 diag_printf("failed to read the first data block!\n");
1369                 return -1;
1370         }
1371
1372         // Check signature
1373
1374         if (ce_is_bin_image((void*)blockBuffer, 512)) {
1375                 // Wince image, setup the .BIN parser.
1376                 pp->isWinceImage = 1;
1377                 ce_bin_init_parser();
1378         } else {
1379                 pp->isWinceImage = 0;
1380         }
1381 #endif
1382         max_clusters = 1020 / ds->sectors_per_cluster;
1383         got_eof = 0;
1384
1385         bp = (u8 *)fat;
1386
1387         actual_fat_block = 0;   //no fat block is loaded already
1388         daddr = address;
1389         start_cluster = dird->dir_start_cluster;
1390         length = dird->dir_fsize;
1391         block = (start_cluster - 2) * ds->sectors_per_cluster + ds->data_area_start_block;
1392         //diag_printf("Sektors per Cluster: %d\n", ds->sectors_per_cluster);
1393
1394         while (length > 0) {
1395                 u16     last_cluster = start_cluster;   // last cluster of multiple block region
1396
1397                 clusters = 1;
1398
1399                 if (length > (ds->bytes_per_sector * ds->sectors_per_cluster)) {
1400                         length -= (ds->bytes_per_sector * ds->sectors_per_cluster);
1401                 } else {
1402                         length = 0;
1403                 }
1404
1405                 while (clusters < max_clusters) {
1406                         new_cluster = get_next_cluster(dird, ds,  pp, last_cluster);
1407                         if (new_cluster < 0) {
1408                                 diag_printf("error !!!!\n");
1409                                 return -1;
1410                         }
1411                         if (new_cluster == 0) {
1412                                 //diag_printf("EOF number of clusters is %d !!!!\n", clusters);
1413                                 got_eof = 1;
1414                                 break;
1415                         }
1416                         if ((new_cluster - last_cluster) == 1) {
1417                                 last_cluster = (u16)new_cluster;
1418                                 //diag_printf("%d: cluster number %d found\n", clusters, last_cluster);
1419                                 clusters++;
1420                                 if (length > (ds->bytes_per_sector * ds->sectors_per_cluster)) {
1421                                         length -= (ds->bytes_per_sector * ds->sectors_per_cluster);
1422                                 }
1423                                 else {
1424                                         length = 0;
1425                                 }
1426                         } else {
1427                                 break;
1428                         }
1429                 }
1430 #if SDCARD_DEBUG
1431                 diag_printf("start cluster is: %d \n", start_cluster);
1432                 diag_printf("last cluster is:  %d \n", last_cluster);
1433                 diag_printf("number of clusters are: %d\n", last_cluster - start_cluster + 1);
1434                 diag_printf("length is: %d\n", length);
1435 #endif
1436                 if (got_eof && (length != 0)) {
1437                         diag_printf("found unexpected EOF!\n");
1438                         return -1;
1439                 }
1440
1441                 block = (start_cluster - 2) * ds->sectors_per_cluster + ds->data_area_start_block;
1442                 pp->dbuffer = (unsigned char *)daddr;
1443
1444                 if (read_multiple_block(pp, block * 512, ds->sectors_per_cluster * 
1445                                         (last_cluster - start_cluster + 1)) < 0) {
1446                         diag_printf("read multiple block command failed!\n");
1447                         return -1;
1448                 }
1449                 daddr += (ds->bytes_per_sector / 4 * ds->sectors_per_cluster *
1450                           (last_cluster - start_cluster + 1));
1451
1452                 start_cluster = get_next_cluster(dird, ds,  pp, last_cluster);
1453         }
1454
1455         return 0;
1456
1457 }
1458
1459 int read_file_old(dir_data *dird, drive_spec *ds, unsigned long *address, COMPAR *pp)
1460 {
1461         u16     cluster;
1462         u32     length;
1463         u32     block;
1464         u32     *daddr;
1465         u16     fat[256];
1466         u32     actual_fat_block;
1467         u32     fat_block_needed;
1468         u16     fat_entry;
1469         u16     fvalue;
1470
1471         actual_fat_block = 0;   //no fat block is loaded already
1472         daddr = address;
1473         cluster = dird->dir_start_cluster;
1474         length = dird->dir_fsize;
1475         block = (cluster - 2) * ds->sectors_per_cluster + ds->data_area_start_block;
1476
1477         diag_printf("Sektors per Cluster: %d\n", ds->sectors_per_cluster);
1478
1479         while (length > 0) {
1480 #if 0
1481                 for (s = 0; s < ds->sectors_per_cluster; s++) {
1482                         if (length == 0)
1483                                 break;
1484
1485                         pp->dbuffer = (unsigned char *)daddr;
1486                         if (read_single_block(pp, block * 512) < 0) {
1487                                 diag_printf("read single block command failed!\n");
1488                                 return -1;
1489                         }
1490 #if SDCARD_DEBUG
1491                         diag_printf("loading block: %d\n", block);
1492 #endif
1493                         block++;
1494                         daddr += ds->bytes_per_sector / 4;
1495                         if (length > ds->bytes_per_sector) {
1496                                 length -= ds->bytes_per_sector;
1497                         } else {
1498                                 length = 0;
1499                         }
1500 #if SDCARD_DEBUG
1501                         diag_printf("new length is: %lu    \n", length);
1502 #endif
1503                 }
1504 #else
1505                 pp->dbuffer = (unsigned char *)daddr;
1506                 if (read_multiple_block(pp, block * 512, ds->sectors_per_cluster) < 0) {
1507                         diag_printf("read multiple block command failed!\n");
1508                         return -1;
1509                 }
1510
1511                 block += ds->sectors_per_cluster;
1512                 daddr += (ds->bytes_per_sector / 4 * ds->sectors_per_cluster);
1513                 if (length > (ds->bytes_per_sector * ds->sectors_per_cluster)) {
1514                         length -= (ds->bytes_per_sector * ds->sectors_per_cluster);
1515                 } else {
1516                         length = 0;
1517                 }
1518 #if SDCARD_DEBUG
1519                 diag_printf("new length is: %lu    \n", length);
1520 #endif
1521 #endif
1522                 if (!length) {                  // all data loaded
1523                         return 0;
1524                 }
1525
1526                 // find next block in fat
1527                 fat_block_needed = ds->fat_start_block + cluster * 2 / ds->bytes_per_sector;
1528                 fat_entry = (cluster * 2) % ds->bytes_per_sector;   // this is the offset to the sector start
1529                 if (actual_fat_block != fat_block_needed) {
1530 #if SDCARD_DEBUG
1531                         diag_printf("loading fat block %lu\n", fat_block_needed);
1532 #endif
1533                         pp->dbuffer = (unsigned char *)fat;
1534                         if (read_single_block(pp, fat_block_needed * 512) < 0) {
1535                                 diag_printf("read single block command failed!\n");
1536                                 return -1;
1537                         }
1538
1539                         actual_fat_block = fat_block_needed;
1540 #if SDCARD_DEBUG
1541                         dump_buf((u8 *)fat, 512);
1542 #endif
1543                 }
1544
1545                 fvalue = fat[fat_entry / 2];
1546 #if SDCARD_DEBUG
1547                 diag_printf("cluster is %d\n", cluster);
1548
1549                 diag_printf("fat contents of entry %d is: %d\n", fat_entry, fvalue);
1550                 diag_printf("length = %lu \n", length);
1551 #endif
1552                 if (!fvalue) {
1553                         diag_printf("unexpected NULL pointer in FAT\n");
1554                         return -1;
1555                 }
1556                 if ((fvalue & 0xfff0) == 0xfff0) {
1557                         diag_printf("unexpected end of file or reserved cluster found in FAT\n");
1558                         return -1;
1559                 }
1560                 cluster = fvalue;
1561                 block = (cluster - 2) * ds->sectors_per_cluster + ds->data_area_start_block;
1562         }
1563
1564         return 0;
1565 }
1566
1567 int get_dir_entry(dir_data *dentry, drive_spec *ds, COMPAR *pp, unsigned char *filename)
1568 {
1569         u32             actual_block;
1570         u32             block_needed;
1571         u32             entry_offset;
1572         int             i, j, k;
1573         unsigned char   *buffer;
1574         unsigned char   *cp;
1575         int             fnlength;
1576
1577         cp = filename;
1578         fnlength = 0;
1579
1580         while (*(cp++) != '\0') {
1581                 fnlength++;
1582         }
1583         //diag_printf("filename length is %d\n", fnlength);
1584
1585         k = 0;
1586         actual_block = 0;
1587         ds->number_of_files = 0;
1588         buffer = pp->dbuffer;
1589
1590         for (i = 0; i < ds->maximum_root_entries; i++) {
1591                 block_needed = ds->root_dir_start_block + i * 32 / ds->bytes_per_sector;
1592                 if (actual_block != block_needed) {
1593                         if (read_single_block(pp, block_needed * 512) < 0) {
1594                                 diag_printf("read single block command failed!\n");
1595                                 return -1;
1596                         }
1597
1598                         actual_block = block_needed;
1599                 }
1600                 entry_offset = (i * 32) % ds->bytes_per_sector;
1601                 if (buffer[entry_offset] == 0x00 ||
1602                     buffer[entry_offset] == 0x05 ||
1603                     buffer[entry_offset] == 0x2e ||
1604                     buffer[entry_offset] == 0xe5) {
1605                         continue;
1606                 }
1607                 if (buffer[entry_offset + 0x0b] & 0x1e) {
1608                         continue;               // hidden system or label
1609                 }
1610                 // copy filename
1611                 for (j = 0;j < 8;j++)
1612                         if (buffer[entry_offset + j] > 0x40 && buffer[entry_offset + j] < 0x5b)
1613                                 (dentry->dir_fname)[j] = buffer[entry_offset + j] + 0x20;
1614                         else
1615                                 (dentry->dir_fname)[j] = buffer[entry_offset + j];
1616
1617                 for (j = 0;j < 3;j++)
1618                         if (buffer[entry_offset + j + 8] > 0x40 && buffer[entry_offset + j + 8] < 0x5b)
1619                                 (dentry->dir_fname)[j + 9] = buffer[entry_offset + j + 8] + 0x20;
1620                         else
1621                                 (dentry->dir_fname)[j + 9] = buffer[entry_offset + j + 8];
1622
1623                 dentry->dir_fname[8] = '.';
1624                 dentry->dir_fname[12] = '\0';
1625                 kill_space(dentry->dir_fname);
1626
1627                 dentry->dir_year = ((buffer[entry_offset + 0x19] & 0xfe) >> 1) + 1980;
1628                 dentry->dir_month = ((buffer[entry_offset + 0x18] & 0xe0) >> 5) + (buffer[entry_offset + 0x19] & 0x01) * 8;
1629                 dentry->dir_day = (buffer[entry_offset + 0x18] & 0x1f);
1630
1631                 dentry->dir_second = (buffer[entry_offset + 0x16] & 0x1f);
1632                 dentry->dir_minute = ((buffer[entry_offset + 0x16] & 0xe0) >> 5) + (buffer[entry_offset + 0x17] & 0x07) * 8;
1633                 dentry->dir_hour = (buffer[entry_offset + 0x17] & 0xf1) >> 3;
1634
1635                 dentry->dir_start_cluster = buffer[entry_offset + 0x1a] + ((buffer[entry_offset + 0x1b]) << 8);
1636                 dentry->dir_fsize = ((buffer[entry_offset + 0x1c]) << 0)
1637                         + ((buffer[entry_offset + 0x1d]) << 8)
1638                         + ((buffer[entry_offset + 0x1e]) << 16)
1639                         + ((buffer[entry_offset + 0x1f]) << 24);
1640
1641                 // compare filename
1642                 cp = filename;
1643                 for (k = 0; k < fnlength; k++) {
1644                         if (*(cp++) != dentry->dir_fname[k]) {
1645                                 break;
1646                         }
1647                 }
1648                 //diag_printf("k= %d \n", k);
1649                 if (k == fnlength) {
1650 #if SDCARD_DEBUG
1651                         diag_printf("file found !!!\n");
1652                         dump_dir(&dentry, ds);
1653 #endif
1654                         return 0;
1655                 }
1656                 ds->number_of_files++;
1657         }
1658         return -1;
1659 }
1660
1661 int isprint(int character)
1662 {
1663         unsigned char a;
1664
1665         a = (unsigned char)(character & 0xff);
1666
1667         if (a > 0x1f && a < 0x7f)
1668                 return 1;
1669         else
1670                 return 0;
1671 }
1672
1673 void kill_space(char *string)
1674 {
1675         int i = 0;
1676         int j;
1677
1678         while (string[i] != '\0') {
1679                 if (string[i] == ' ') {
1680                         j = i;
1681                         while (string[j] != '\0') {
1682                                 string[j] = string[j + 1];
1683                                 j++;
1684                         }
1685                 } else {
1686                         i++;
1687                 }
1688         }
1689 }
1690
1691 int read_multiple_block(COMPAR *pp, unsigned long address, unsigned long blocks)
1692 {
1693         static unsigned long __attribute__ ((aligned (32))) dma_desc[64][4];
1694         unsigned long val, cvalue, length, last_size, dma_buffer;
1695         int i;
1696         unsigned long   cebuffer[1024];
1697         int             bi;
1698         unsigned long   *bap;
1699
1700         //HAL_WRITE_UINT32(GPSRa, 0x800);
1701         //HAL_WRITE_UINT32(GPCRa, 0x800);
1702
1703         if (!pp->isWinceImage) {        // use chained descriptor list dma
1704                 dma_buffer = (unsigned long)pp->dbuffer;
1705
1706                 length = blocks * 512;
1707
1708                 i = 0;
1709                 while (length > 0) {
1710                         if (i > 63) {
1711                                 diag_printf("descriptor overrun!!!\n");
1712                                 return -1;
1713                         }
1714
1715                         dma_desc[i][0] = (unsigned long)&dma_desc[i + 1][0];
1716                         dma_desc[i][1] = 0x41100040;    // source address is SDCARD FIFO
1717                         dma_desc[i][2] = dma_buffer;    // target address
1718                         val = 0x60034000;
1719                         if (length >= 8160) {
1720                                 last_size = 8160;
1721                                 dma_desc[i][3] = val | 8160;    // command
1722                                 //diag_printf("descriptor %d: DMA length set to %d bytes\n", i, last_size);
1723                         } else {
1724                                 last_size = length;
1725                                 dma_desc[i][3] = val | length;  // command
1726                                 //diag_printf("descriptor %d: DMA length set to %d bytes\n", i, last_size);
1727                         }
1728                         dma_buffer += last_size;
1729                         length -= last_size;
1730                         if (length == 0) {
1731                                 dma_desc[i][0] |= 1;            // set stop bit
1732                         }
1733                         i++;
1734                 }
1735
1736                 HAL_DCACHE_SYNC();
1737
1738                 //diag_printf("descriptor start is 0x%08X\n", (unsigned long)&dma_desc[0][0]);
1739
1740                 HAL_READ_UINT32(DCSR8, val);
1741                 val &= 0x3fffffff;                      // clear RUN and NODESCRIPTOR FETCH
1742                 HAL_WRITE_UINT32(DCSR8, val);
1743
1744                 HAL_WRITE_UINT32(DDADR8, (unsigned long)&dma_desc[0][0]);
1745                 val |= 0x80000000;                      // run channel
1746                 HAL_WRITE_UINT32(DCSR8, val);
1747
1748                 HAL_WRITE_UINT32(MMCNOB, blocks);
1749
1750                 pp->command_parameter_low = address & 0xffff;
1751                 pp->command_parameter_high = (address & 0xffff0000) >> 16;
1752
1753                 pp->command_number = SD_CMD18;
1754                 pp->res_type = CMD_RES_TYPE_R1;
1755                 pp->data_en = 1;
1756                 pp->dma_en = 1;
1757                 pp->data_write = 0;
1758                 pp->stream_blk = 0;
1759                 pp->busy = 0;
1760                 pp->init = 0;
1761                 pp->sd_4dat = 1;
1762
1763                 HAL_WRITE_UINT32(MMCCMD, pp->command_number);           // command number
1764                 HAL_WRITE_UINT32(MMCARGH, pp->command_parameter_high);  // command parameter upper 16 bits
1765                 HAL_WRITE_UINT32(MMCARGL, pp->command_parameter_low);   // command parameter lower 16 bits
1766
1767                 cvalue = cmd_value(pp);
1768                 HAL_WRITE_UINT32(MMCCMDAT, cvalue);
1769
1770                 // wait for dma end
1771                 HAL_READ_UINT32(DCSR8, val);            // wait for dma end
1772                 while (val & 0x80000000) {
1773                         HAL_READ_UINT32(DCSR8, val);
1774                 }
1775 #if SDCARD_DEBUG
1776                 diag_printf("DMA finished \n");
1777                 diag_printf("DMA Control Status is: 0x%08X\n", val);
1778
1779                 HAL_READ_UINT32(DDADR8, val);
1780                 diag_printf("Descriptor Address Register: 0x%08X\n", val);
1781
1782                 HAL_READ_UINT32(DSADR8, val);
1783                 diag_printf("Source Address Register: 0x%08X\n", val);
1784
1785                 HAL_READ_UINT32(DTADR8, val);
1786                 diag_printf("Target Address Register: 0x%08X\n", val);
1787
1788                 HAL_READ_UINT32(DCMD8, val);
1789                 diag_printf("Command Register: 0x%08X\n", val);
1790 #endif
1791                 HAL_WRITE_UINT32(MMCNOB, 1);
1792
1793                 pp->command_number = SD_CMD12;          //Stop Transmission
1794                 if (send_command(pp) < 0) {
1795                         diag_printf("CMD12 failed !!!\n");
1796                         return -1;
1797                 }
1798
1799                 return 0;
1800         } else {                // read wince image
1801 #ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
1802                 HAL_WRITE_UINT32(MMCNOB, blocks);
1803                 //HAL_WRITE_UINT32(MMCNOB, blocks);
1804
1805                 pp->command_parameter_low = address & 0xffff;
1806                 pp->command_parameter_high = (address & 0xffff0000) >> 16;
1807
1808                 pp->command_number = SD_CMD18;
1809                 pp->res_type = CMD_RES_TYPE_R1;
1810                 pp->data_en = 1;
1811                 pp->dma_en = 0;
1812                 pp->data_write = 0;
1813                 pp->stream_blk = 0;
1814                 pp->busy = 0;
1815                 pp->init = 0;
1816                 pp->sd_4dat = 1;
1817
1818                 HAL_WRITE_UINT32(MMCCMD, pp->command_number);
1819                 HAL_WRITE_UINT32(MMCARGH, pp->command_parameter_high);
1820                 HAL_WRITE_UINT32(MMCARGL, pp->command_parameter_low);
1821
1822                 cvalue = cmd_value(pp);
1823                 HAL_WRITE_UINT32(MMCCMDAT, cvalue);
1824
1825                 length = 512 * blocks;
1826
1827                 bap = cebuffer;
1828
1829                 bi = 0;
1830
1831                 while (length > 0) {
1832                         HAL_READ_UINT32(MMCIREG, val);
1833                         while (!(val & 0x20)) { // wait for RXFIFO_RD_REQ
1834                                 HAL_READ_UINT32(MMCIREG, val);
1835                         }
1836
1837                         *(bap++) = *(unsigned long *)MMCRXFIFO;
1838                         *(bap++) = *(unsigned long *)MMCRXFIFO;
1839                         *(bap++) = *(unsigned long *)MMCRXFIFO;
1840                         *(bap++) = *(unsigned long *)MMCRXFIFO;
1841                         *(bap++) = *(unsigned long *)MMCRXFIFO;
1842                         *(bap++) = *(unsigned long *)MMCRXFIFO;
1843                         *(bap++) = *(unsigned long *)MMCRXFIFO;
1844                         *(bap++) = *(unsigned long *)MMCRXFIFO;
1845
1846                         length -= 32;
1847                         bi += 8;
1848
1849                         if (bi == 1024) {
1850                                 switch (ce_bin_parse_next(cebuffer, 4096)) {
1851                                 case CE_PR_EOF:   // End of file
1852                                         // The image was successfully loaded, just go out
1853                                         break;
1854
1855                                 case CE_PR_ERROR:
1856                                         // Ops! Corrupted or invalid image!
1857                                         return -1;
1858
1859                                 case CE_PR_MORE:
1860                                         // Need more data. Just process to the next block
1861                                         break;
1862                                 }
1863                                 bi = 0;
1864                                 bap = cebuffer;
1865                         }
1866
1867                         if (!length) {
1868                                 switch (ce_bin_parse_next(cebuffer, bi * 4)) {
1869                                 case CE_PR_EOF:   // End of file
1870                                         // The image was successfully loaded, just go out
1871                                         break;
1872                                         //return 0;
1873                                 case CE_PR_ERROR:
1874                                         // Ops! Corrupted or invalid image!
1875                                         return -1;
1876                                 case CE_PR_MORE:
1877                                         // Need more data. Just process to the next block
1878                                         break;
1879                                 }
1880                         }
1881
1882                 }
1883
1884                 if (wait4cmd_end() < 0) {
1885                         diag_printf("read multiple block command timed out !!!!");
1886                         return -1;
1887                 }
1888
1889                 HAL_WRITE_UINT32(MMCNOB, 1);
1890
1891                 pp->command_number = SD_CMD12;  // Stop Transmission
1892                 if (send_command(pp) < 0) {
1893                         diag_printf("CMD12 failed !!!\n");
1894                         return -1;
1895                 }
1896 #endif
1897                 return 0;
1898         }
1899 }