summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSolomon Peachy <pizza@shaftnet.org>2024-07-05 16:00:30 -0400
committerSolomon Peachy <pizza@shaftnet.org>2024-08-12 14:23:44 -0400
commit15e52374698ba7395ff0ece0d3d70435a66406c4 (patch)
tree778f81817b2381decc920b49242e91f994f2d8ee
parent9ff308a589f3655453fe047d9b08ca5e2a01f06d (diff)
downloadrockbox-15e52374698ba7395ff0ece0d3d70435a66406c4.tar.gz
rockbox-15e52374698ba7395ff0ece0d3d70435a66406c4.zip
storage: 64-bit sector offsets
* Create new 'sector_t' type alias: * uint64_t for all targets with HAVE_LBA48 or HAVE_SDUC * unsigned long for the everything else * Alter all storage APIs to use sector_t instead of 'unsigned long' * Alter Volume/Partition/storage info structures to use sector_t * Disk cache converted to sector_t * ATA Core: * convert to using sector_t for sector addresses and drive sizes * Always fill out upper 16 bits of LBA48 addresses * IDENTIFY INFO is fixed at 512 bytes, not SECTOR_SIZE * USB mass storage: * convert to using sector_t for sector addesses and drive sizes * Implement READ_16/WRITE_16 for LBA48 addresses * Convert FAT code to use sector_t for all sector references * output_dyn_value() now accepts int64_t instead of 'int' * Corrected "rockbox info" to work for (MULTIVOLUME & !MULTIDRIVE) * Better reporting of disk and (logical+physical) sector sizes in debug info * Detect SDUC cards and report on storage debug_info screen To-do: SDUC * Refactor SD core to remove duplicate code in every driver * Card probe and init state machine * Implement core SDUC support * SD2.0 needs to be 2.0+ (fixed for jz47xx and x1000) * Host and Card ID (ACMD41) * 32-bit addressing for all read/write/erase operations (CMD22) * ADD SDUC to target device drivers, defining HAVE_SDUC as appropriate Change-Id: Ib0138781a0081664d11511037685503df1b93608
-rw-r--r--apps/debug_menu.c78
-rw-r--r--apps/menus/main_menu.c20
-rw-r--r--apps/misc.c13
-rw-r--r--apps/misc.h21
-rw-r--r--apps/plugin.h2
-rw-r--r--firmware/common/disk.c20
-rw-r--r--firmware/common/disk_cache.c8
-rw-r--r--firmware/drivers/ata.c36
-rw-r--r--firmware/drivers/fat.c44
-rw-r--r--firmware/drivers/ramdisk.c6
-rw-r--r--firmware/drivers/sd.c12
-rw-r--r--firmware/export/ata.h6
-rw-r--r--firmware/export/disk.h4
-rw-r--r--firmware/export/fat.h4
-rw-r--r--firmware/export/mmc.h4
-rw-r--r--firmware/export/mv.h16
-rw-r--r--firmware/export/nand.h4
-rw-r--r--firmware/export/ramdisk.h4
-rw-r--r--firmware/export/sd.h5
-rw-r--r--firmware/export/sdmmc.h8
-rw-r--r--firmware/export/storage.h6
-rw-r--r--firmware/include/disk_cache.h4
-rw-r--r--firmware/storage.c4
-rw-r--r--firmware/target/arm/as3525/sd-as3525.c15
-rw-r--r--firmware/target/arm/as3525/sd-as3525v2.c8
-rw-r--r--firmware/target/arm/ata-nand-telechips.c92
-rw-r--r--firmware/target/arm/imx233/nand-imx233.c4
-rw-r--r--firmware/target/arm/imx233/partitions-imx233.c2
-rw-r--r--firmware/target/arm/imx233/partitions-imx233.h6
-rw-r--r--firmware/target/arm/imx233/sdmmc-imx233.c16
-rw-r--r--firmware/target/arm/pp/ata-sd-pp.c40
-rw-r--r--firmware/target/arm/rk27xx/ata-nand-rk27xx.c8
-rw-r--r--firmware/target/arm/rk27xx/sd-rk27xx.c7
-rw-r--r--firmware/target/arm/s3c2440/sd-s3c2440.c171
-rw-r--r--firmware/target/arm/s5l8700/ata-nand-s5l8700.c8
-rw-r--r--firmware/target/arm/s5l8702/ipod6g/storage_ata-6g.c11
-rw-r--r--firmware/target/arm/tcc780x/sd-tcc780x.c94
-rw-r--r--firmware/target/arm/tms320dm320/creative-zvm/ata-creativezvm.c22
-rw-r--r--firmware/target/arm/tms320dm320/creative-zvm/ata-target.h4
-rw-r--r--firmware/target/arm/tms320dm320/sdmmc-dm320.c23
-rw-r--r--firmware/target/hosted/filesystem-unix.c4
-rw-r--r--firmware/target/hosted/filesystem-win32.c2
-rw-r--r--firmware/target/mips/ingenic_jz47xx/ata-nand-jz4740.c16
-rw-r--r--firmware/target/mips/ingenic_jz47xx/ata-nand-jz4760.c18
-rw-r--r--firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c22
-rw-r--r--firmware/target/mips/ingenic_jz47xx/ata-sd-jz4760.c17
-rw-r--r--firmware/target/mips/ingenic_x1000/msc-x1000.c2
-rw-r--r--firmware/target/mips/ingenic_x1000/sd-x1000.c7
-rw-r--r--firmware/usbstack/usb_storage.c116
49 files changed, 629 insertions, 435 deletions
diff --git a/apps/debug_menu.c b/apps/debug_menu.c
index 4a863e7484..2328784c96 100644
--- a/apps/debug_menu.c
+++ b/apps/debug_menu.c
@@ -557,14 +557,16 @@ static const char* dbg_partitions_getname(int selected_item, void *data,
557 if (!disk_partinfo(partition, &p)) 557 if (!disk_partinfo(partition, &p))
558 return buffer; 558 return buffer;
559 559
560 // XXX fix this up to use logical sector size
561 // XXX and if mounted, show free info...
560 if (selected_item%2) 562 if (selected_item%2)
561 { 563 {
562 snprintf(buffer, buffer_len, " T:%x %ld MB", p.type, 564 snprintf(buffer, buffer_len, " T:%x %llu MB", p.type,
563 p.size / ( 2048 / ( SECTOR_SIZE / 512 ))); 565 (uint64_t)(p.size / ( 2048 / ( SECTOR_SIZE / 512 ))));
564 } 566 }
565 else 567 else
566 { 568 {
567 snprintf(buffer, buffer_len, "P%d: S:%lx", partition, p.start); 569 snprintf(buffer, buffer_len, "P%d: S:%llx", partition, (uint64_t)p.start);
568 } 570 }
569 return buffer; 571 return buffer;
570} 572}
@@ -572,7 +574,7 @@ static const char* dbg_partitions_getname(int selected_item, void *data,
572static bool dbg_partitions(void) 574static bool dbg_partitions(void)
573{ 575{
574 struct simplelist_info info; 576 struct simplelist_info info;
575 simplelist_info_init(&info, "Partition Info", NUM_DRIVES * 4, NULL); 577 simplelist_info_init(&info, "Partition Info", NUM_DRIVES * MAX_PARTITIONS_PER_DRIVE, NULL);
576 info.selection_size = 2; 578 info.selection_size = 2;
577 info.scroll_all = true; 579 info.scroll_all = true;
578 info.get_name = dbg_partitions_getname; 580 info.get_name = dbg_partitions_getname;
@@ -1343,6 +1345,22 @@ static int disk_callback(int btn, struct gui_synclist *lists)
1343 "R2W: *%d", card->r2w_factor); 1345 "R2W: *%d", card->r2w_factor);
1344#if (CONFIG_STORAGE & STORAGE_SD) 1346#if (CONFIG_STORAGE & STORAGE_SD)
1345 int csd_structure = card_extract_bits(card->csd, 127, 2); 1347 int csd_structure = card_extract_bits(card->csd, 127, 2);
1348 const char *ver;
1349 switch(csd_structure) {
1350 case 0:
1351 ver = "1 (SD)";
1352 break;
1353 case 1:
1354 ver = "2 (SDHC/SDXC)";
1355 break;
1356 case 2:
1357 ver = "3 (SDUC)";
1358 break;
1359 default:
1360 ver = "Unknown";
1361 break;
1362 }
1363 simplelist_addline("SDVer: %s\n", ver);
1346 if (csd_structure == 0) /* CSD version 1.0 */ 1364 if (csd_structure == 0) /* CSD version 1.0 */
1347#endif 1365#endif
1348 { 1366 {
@@ -1407,15 +1425,41 @@ static int disk_callback(int btn, struct gui_synclist *lists)
1407 buf[8]=0; 1425 buf[8]=0;
1408 simplelist_addline( 1426 simplelist_addline(
1409 "Firmware: %s", buf); 1427 "Firmware: %s", buf);
1410 snprintf(buf, sizeof buf, "%ld MB", 1428
1411 ((unsigned long)identify_info[61] << 16 | 1429 uint64_t total_sectors = identify_info[60] | (identify_info[61] << 16);
1412 (unsigned long)identify_info[60]) / 2048 ); 1430#ifdef HAVE_LBA48
1431 if (identify_info[83] & 0x0400
1432 && total_sectors == 0x0FFFFFFF)
1433 total_sectors = identify_info[100] | (identify_info[101] << 16) | ((uint64_t)identify_info[102] << 32) | ((uint64_t)identify_info[103] << 48);
1434#endif
1435
1436 uint32_t sector_size;
1437
1438 /* Logical sector size > 512B ? */
1439 if ((identify_info[106] & 0xd000) == 0x5000)
1440 sector_size = identify_info[117] | (identify_info[118] << 16);
1441 else
1442 sector_size = SECTOR_SIZE;
1443
1444 total_sectors *= sector_size; /* Convert to bytes */
1445 total_sectors /= (1024 * 1024); /* Convert to MB */
1446
1447 simplelist_addline("Size: %llu MB", total_sectors);
1448 simplelist_addline("Logical sector size: %u B", sector_size);
1449
1450 if((identify_info[106] & 0xe000) == 0x6000)
1451 sector_size *= BIT_N(identify_info[106] & 0x000f);
1413 simplelist_addline( 1452 simplelist_addline(
1414 "Size: %s", buf); 1453 "Physical sector size: %d B", sector_size);
1415 unsigned long free; 1454
1455#ifndef HAVE_MULTIVOLUME
1456 // XXX this needs to be fixed for multi-volume setups
1457 sector_t free;
1416 volume_size( IF_MV(0,) NULL, &free ); 1458 volume_size( IF_MV(0,) NULL, &free );
1417 simplelist_addline( 1459 simplelist_addline(
1418 "Free: %ld MB", free / 1024); 1460 "Free: %llu MB", free / 1024);
1461#endif
1462
1419 simplelist_addline("SSD detected: %s", ata_disk_isssd() ? "yes" : "no"); 1463 simplelist_addline("SSD detected: %s", ata_disk_isssd() ? "yes" : "no");
1420 simplelist_addline( 1464 simplelist_addline(
1421 "Spinup time: %d ms", storage_spinup_time() * (1000/HZ)); 1465 "Spinup time: %d ms", storage_spinup_time() * (1000/HZ));
@@ -1452,11 +1496,7 @@ static int disk_callback(int btn, struct gui_synclist *lists)
1452 simplelist_addline( 1496 simplelist_addline(
1453 "No timing info"); 1497 "No timing info");
1454 } 1498 }
1455 int sector_size = 512; 1499
1456 if((identify_info[106] & 0xe000) == 0x6000)
1457 sector_size *= BIT_N(identify_info[106] & 0x000f);
1458 simplelist_addline(
1459 "Physical sector size: %d", sector_size);
1460#ifdef HAVE_ATA_DMA 1500#ifdef HAVE_ATA_DMA
1461 if (identify_info[63] & (1<<0)) { 1501 if (identify_info[63] & (1<<0)) {
1462 simplelist_addline( 1502 simplelist_addline(
@@ -1751,8 +1791,8 @@ static int disk_callback(int btn, struct gui_synclist *lists)
1751 simplelist_addline("Model: %s", info.product); 1791 simplelist_addline("Model: %s", info.product);
1752 simplelist_addline("Firmware: %s", info.revision); 1792 simplelist_addline("Firmware: %s", info.revision);
1753 simplelist_addline( 1793 simplelist_addline(
1754 "Size: %ld MB", info.num_sectors*(info.sector_size/512)/2024); 1794 "Size: %lld MB", (uint64_t)(info.num_sectors*(info.sector_size/512)/2048));
1755 unsigned long free; 1795 storage_t free;
1756 volume_size( IF_MV(0,) NULL, &free ); 1796 volume_size( IF_MV(0,) NULL, &free );
1757 simplelist_addline( 1797 simplelist_addline(
1758 "Free: %ld MB", free / 1024); 1798 "Free: %ld MB", free / 1024);
@@ -1771,13 +1811,13 @@ static bool dbg_identify_info(void)
1771 const unsigned short *identify_info = ata_get_identify(); 1811 const unsigned short *identify_info = ata_get_identify();
1772#ifdef ROCKBOX_LITTLE_ENDIAN 1812#ifdef ROCKBOX_LITTLE_ENDIAN
1773 /* this is a pointer to a driver buffer so we can't modify it */ 1813 /* this is a pointer to a driver buffer so we can't modify it */
1774 for (int i = 0; i < SECTOR_SIZE/2; ++i) 1814 for (int i = 0; i < ATA_IDENTIFY_WORDS; ++i)
1775 { 1815 {
1776 unsigned short word = swap16(identify_info[i]); 1816 unsigned short word = swap16(identify_info[i]);
1777 write(fd, &word, 2); 1817 write(fd, &word, 2);
1778 } 1818 }
1779#else 1819#else
1780 write(fd, identify_info, SECTOR_SIZE); 1820 write(fd, identify_info, ATA_IDENTIFY_WORDS*2);
1781#endif 1821#endif
1782 close(fd); 1822 close(fd);
1783 } 1823 }
diff --git a/apps/menus/main_menu.c b/apps/menus/main_menu.c
index a483a72eb0..bcaeee9089 100644
--- a/apps/menus/main_menu.c
+++ b/apps/menus/main_menu.c
@@ -134,8 +134,8 @@ static int show_legal(void)
134struct info_data 134struct info_data
135 135
136{ 136{
137 unsigned long size[NUM_VOLUMES]; 137 sector_t size[NUM_VOLUMES];
138 unsigned long free[NUM_VOLUMES]; 138 sector_t free[NUM_VOLUMES];
139 unsigned long name[NUM_VOLUMES]; 139 unsigned long name[NUM_VOLUMES];
140 bool new_data; 140 bool new_data;
141}; 141};
@@ -162,16 +162,19 @@ enum infoscreenorder
162*/ 162*/
163static int refresh_data(struct info_data *info) 163static int refresh_data(struct info_data *info)
164{ 164{
165 int i = 0; 165#ifdef HAVE_MULTIVOLUME
166#ifdef HAVE_MULTIDRIVE 166#ifdef HAVE_MULTIDRIVE
167 int drive;
168 int max = -1; 167 int max = -1;
169 168#endif
169 int drive = 0;
170 int i = 0;
170 for (i = 0 ; CHECK_VOL(i) ; i++) { 171 for (i = 0 ; CHECK_VOL(i) ; i++) {
171#endif 172#endif
172 volume_size(IF_MV(i,) &info->size[i], &info->free[i]); 173 volume_size(IF_MV(i,) &info->size[i], &info->free[i]);
174#ifdef HAVE_MULTIVOLUME
173#ifdef HAVE_MULTIDRIVE 175#ifdef HAVE_MULTIDRIVE
174 drive = volume_drive(i); 176 drive = volume_drive(i);
177#endif
175 if (drive > 0 || info->size[i] == 0) 178 if (drive > 0 || info->size[i] == 0)
176 info->name[i] = LANG_DISK_NAME_MMC; 179 info->name[i] = LANG_DISK_NAME_MMC;
177 else 180 else
@@ -182,9 +185,12 @@ static int refresh_data(struct info_data *info)
182 max = drive; 185 max = drive;
183 else if (drive < max) 186 else if (drive < max)
184 break; 187 break;
188#elif defined(HAVE_MULTIVOLUME)
189 if (volume_partition(i) == -1)
190 break;
191#endif
192#ifdef HAVE_MULTIVOLUME
185 } 193 }
186#else
187 i++;
188#endif 194#endif
189 195
190 info->new_data = false; 196 info->new_data = false;
diff --git a/apps/misc.c b/apps/misc.c
index bd7aa9485b..0bbaba965b 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -139,7 +139,7 @@ const unsigned char * const unit_strings_core[] =
139 * voiced.*/ 139 * voiced.*/
140char *output_dyn_value(char *buf, 140char *output_dyn_value(char *buf,
141 int buf_size, 141 int buf_size,
142 int value, 142 int64_t value,
143 const unsigned char * const *units, 143 const unsigned char * const *units,
144 unsigned int unit_count, 144 unsigned int unit_count,
145 bool binary_scale) 145 bool binary_scale)
@@ -147,8 +147,9 @@ char *output_dyn_value(char *buf,
147 unsigned int scale = binary_scale ? 1024 : 1000; 147 unsigned int scale = binary_scale ? 1024 : 1000;
148 unsigned int fraction = 0; 148 unsigned int fraction = 0;
149 unsigned int unit_no = 0; 149 unsigned int unit_no = 0;
150 unsigned int value_abs = (value < 0) ? -value : value; 150 uint64_t value_abs = (value < 0) ? -value : value;
151 char tbuf[5]; 151 char tbuf[5];
152 int value2;
152 153
153 while (value_abs >= scale && unit_no < (unit_count - 1)) 154 while (value_abs >= scale && unit_no < (unit_count - 1))
154 { 155 {
@@ -157,7 +158,7 @@ char *output_dyn_value(char *buf,
157 unit_no++; 158 unit_no++;
158 } 159 }
159 160
160 value = (value < 0) ? -value_abs : value_abs; /* preserve sign */ 161 value2 = (value < 0) ? -value_abs : value_abs; /* preserve sign */
161 fraction = (fraction * 1000 / scale) / 10; 162 fraction = (fraction * 1000 / scale) / 10;
162 163
163 if (value_abs >= 100 || fraction >= 100 || !unit_no) 164 if (value_abs >= 100 || fraction >= 100 || !unit_no)
@@ -170,10 +171,10 @@ char *output_dyn_value(char *buf,
170 if (buf) 171 if (buf)
171 { 172 {
172 if (*tbuf) 173 if (*tbuf)
173 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT), 174 snprintf(buf, buf_size, "%d%s%s%s", value2, str(LANG_POINT),
174 tbuf, P2STR(units[unit_no])); 175 tbuf, P2STR(units[unit_no]));
175 else 176 else
176 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no])); 177 snprintf(buf, buf_size, "%d%s", value2, P2STR(units[unit_no]));
177 } 178 }
178 else 179 else
179 { 180 {
@@ -1851,7 +1852,7 @@ enum current_activity get_current_activity(void)
1851* ** Extended error info truth table ** 1852* ** Extended error info truth table **
1852* [ Handle ][buf_reqd] 1853* [ Handle ][buf_reqd]
1853* [ > 0 ][ > 0 ] buf_reqd indicates how many bytes were used 1854* [ > 0 ][ > 0 ] buf_reqd indicates how many bytes were used
1854* [ALOC_ERR][ > 0 ] buf_reqd indicates how many bytes are needed 1855* [ALOC_ERR][ > 0 ] buf_reqd indicates how many bytes are needed
1855* [ALOC_ERR][READ_ERR] there was an error reading the file or it is empty 1856* [ALOC_ERR][READ_ERR] there was an error reading the file or it is empty
1856*/ 1857*/
1857int core_load_bmp(const char * filename, struct bitmap *bm, const int bmformat, 1858int core_load_bmp(const char * filename, struct bitmap *bm, const int bmformat,
diff --git a/apps/misc.h b/apps/misc.h
index 3b7ce89d22..87765b28b1 100644
--- a/apps/misc.h
+++ b/apps/misc.h
@@ -1,10 +1,10 @@
1/*************************************************************************** 1/***************************************************************************
2 * __________ __ ___. 2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2002 by Daniel Stenberg 10 * Copyright (C) 2002 by Daniel Stenberg
@@ -22,6 +22,7 @@
22#define MISC_H 22#define MISC_H
23 23
24#include <stdbool.h> 24#include <stdbool.h>
25#include <stdint.h>
25#include <inttypes.h> 26#include <inttypes.h>
26#include "config.h" 27#include "config.h"
27#include "screen_access.h" 28#include "screen_access.h"
@@ -36,7 +37,7 @@ extern const unsigned char * const unit_strings_core[];
36 * voiced.*/ 37 * voiced.*/
37char *output_dyn_value(char *buf, 38char *output_dyn_value(char *buf,
38 int buf_size, 39 int buf_size,
39 int value, 40 int64_t value,
40 const unsigned char * const *units, 41 const unsigned char * const *units,
41 unsigned int unit_count, 42 unsigned int unit_count,
42 bool binary_scale); 43 bool binary_scale);
@@ -113,9 +114,9 @@ bool warn_on_pl_erase(void);
113bool show_search_progress(bool init, int count, int current, int total); 114bool show_search_progress(bool init, int count, int current, int total);
114 115
115/* Read (up to) a line of text from fd into buffer and return number of bytes 116/* Read (up to) a line of text from fd into buffer and return number of bytes
116 * read (which may be larger than the number of bytes stored in buffer). If 117 * read (which may be larger than the number of bytes stored in buffer). If
117 * an error occurs, -1 is returned (and buffer contains whatever could be 118 * an error occurs, -1 is returned (and buffer contains whatever could be
118 * read). A line is terminated by a LF char. Neither LF nor CR chars are 119 * read). A line is terminated by a LF char. Neither LF nor CR chars are
119 * stored in buffer. 120 * stored in buffer.
120 */ 121 */
121int read_line(int fd, char* buffer, int buffer_size); 122int read_line(int fd, char* buffer, int buffer_size);
diff --git a/apps/plugin.h b/apps/plugin.h
index af8a65c5de..31e1c77cb7 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -649,7 +649,7 @@ struct plugin_api {
649 int (*memcmp)(const void *s1, const void *s2, size_t n); 649 int (*memcmp)(const void *s1, const void *s2, size_t n);
650 char *(*strcasestr) (const char* phaystack, const char* pneedle); 650 char *(*strcasestr) (const char* phaystack, const char* pneedle);
651 char* (*strtok_r)(char *ptr, const char *sep, char **end); 651 char* (*strtok_r)(char *ptr, const char *sep, char **end);
652 char* (*output_dyn_value)(char *buf, int buf_size, int value, 652 char* (*output_dyn_value)(char *buf, int buf_size, int64_t value,
653 const unsigned char * const *units, 653 const unsigned char * const *units,
654 unsigned int unit_count, bool binary_scale); 654 unsigned int unit_count, bool binary_scale);
655 /* unicode stuff */ 655 /* unicode stuff */
diff --git a/firmware/common/disk.c b/firmware/common/disk.c
index 9b6454c9a2..576eede143 100644
--- a/firmware/common/disk.c
+++ b/firmware/common/disk.c
@@ -172,6 +172,8 @@ bool disk_init(IF_MD_NONVOID(int drive))
172 } 172 }
173 } 173 }
174 174
175 // XXX backup GPT header at final LBA of drive...
176
175 while (is_gpt) { 177 while (is_gpt) {
176 /* Re-start partition parsing using GPT */ 178 /* Re-start partition parsing using GPT */
177 uint64_t part_lba; 179 uint64_t part_lba;
@@ -243,20 +245,24 @@ reload:
243 goto skip; /* Any flag makes us ignore this */ 245 goto skip; /* Any flag makes us ignore this */
244 } 246 }
245 tmp = BYTES2INT64(pptr, 32); /* FIRST LBA */ 247 tmp = BYTES2INT64(pptr, 32); /* FIRST LBA */
246 if (tmp > UINT32_MAX) { // XXX revisit when we resize struct partinfo! 248#ifndef STORAGE_64BIT_SECTOR
247 DEBUGF("GPT: partition starts after 2GiB mark\n"); 249 if (tmp > UINT32_MAX) {
250 DEBUGF("GPT: partition starts after 2TiB mark\n");
248 goto skip; 251 goto skip;
249 } 252 }
253#endif
250 if (tmp < 34) { 254 if (tmp < 34) {
251 DEBUGF("GPT: Invalid start LBA\n"); 255 DEBUGF("GPT: Invalid start LBA\n");
252 goto skip; 256 goto skip;
253 } 257 }
254 pinfo[part].start = tmp; 258 pinfo[part].start = tmp;
255 tmp = BYTES2INT64(pptr, 40); /* LAST LBA */ 259 tmp = BYTES2INT64(pptr, 40); /* LAST LBA */
256 if (tmp > UINT32_MAX) { // XXX revisit when we resize struct partinfo! 260#ifndef STORAGE_64BIT_SECTOR
257 DEBUGF("GPT: partition ends after 2GiB mark\n"); 261 if (tmp > UINT32_MAX) {
262 DEBUGF("GPT: partition ends after 2TiB mark\n");
258 goto skip; 263 goto skip;
259 } 264 }
265#endif
260 if (tmp <= pinfo[part].start) { 266 if (tmp <= pinfo[part].start) {
261 DEBUGF("GPT: Invalid end LBA\n"); 267 DEBUGF("GPT: Invalid end LBA\n");
262 goto skip; 268 goto skip;
@@ -264,7 +270,7 @@ reload:
264 pinfo[part].size = tmp - pinfo[part].start + 1; 270 pinfo[part].size = tmp - pinfo[part].start + 1;
265 pinfo[part].type = PARTITION_TYPE_FAT32_LBA; 271 pinfo[part].type = PARTITION_TYPE_FAT32_LBA;
266 272
267 DEBUGF("GPart%d: start: %08lx size: %08lx\n", 273 DEBUGF("GPart%d: start: %016lx size: %016lx\n",
268 part,pinfo[part].start,pinfo[part].size); 274 part,pinfo[part].start,pinfo[part].size);
269 part++; 275 part++;
270 276
@@ -499,13 +505,13 @@ unsigned int volume_get_cluster_size(IF_MV_NONVOID(int volume))
499 return clustersize; 505 return clustersize;
500} 506}
501 507
502void volume_size(IF_MV(int volume,) unsigned long *sizep, unsigned long *freep) 508void volume_size(IF_MV(int volume,) sector_t *sizep, sector_t *freep)
503{ 509{
504 disk_reader_lock(); 510 disk_reader_lock();
505 511
506 if (!CHECK_VOL(volume) || !fat_size(IF_MV(volume,) sizep, freep)) 512 if (!CHECK_VOL(volume) || !fat_size(IF_MV(volume,) sizep, freep))
507 { 513 {
508 if (freep) *sizep = 0; 514 if (sizep) *sizep = 0;
509 if (freep) *freep = 0; 515 if (freep) *freep = 0;
510 } 516 }
511 517
diff --git a/firmware/common/disk_cache.c b/firmware/common/disk_cache.c
index 9e4dee6a91..74c4d5f35b 100644
--- a/firmware/common/disk_cache.c
+++ b/firmware/common/disk_cache.c
@@ -71,12 +71,12 @@ struct disk_cache_entry
71#ifdef HAVE_MULTIVOLUME 71#ifdef HAVE_MULTIVOLUME
72 unsigned char volume; /* volume of sector */ 72 unsigned char volume; /* volume of sector */
73#endif 73#endif
74 unsigned long sector; /* cached disk sector number */ 74 sector_t sector; /* cached disk sector number */
75}; 75};
76 76
77BITARRAY_TYPE_DECLARE(cache_map_entry_t, cache_map, DC_NUM_ENTRIES) 77BITARRAY_TYPE_DECLARE(cache_map_entry_t, cache_map, DC_NUM_ENTRIES)
78 78
79static inline unsigned int map_sector(unsigned long sector) 79static inline unsigned int map_sector(sector_t sector)
80{ 80{
81 /* keep sector hash simple for now */ 81 /* keep sector hash simple for now */
82 return sector % DC_MAP_NUM_ENTRIES; 82 return sector % DC_MAP_NUM_ENTRIES;
@@ -172,7 +172,7 @@ static inline void cache_discard_entry(struct disk_cache_entry *dce,
172/* search the cache for the specified sector, returning a buffer, either 172/* search the cache for the specified sector, returning a buffer, either
173 to the specified sector, if it exists, or a new/evicted entry that must 173 to the specified sector, if it exists, or a new/evicted entry that must
174 be filled */ 174 be filled */
175void * dc_cache_probe(IF_MV(int volume,) unsigned long sector, 175void * dc_cache_probe(IF_MV(int volume,) sector_t sector,
176 unsigned int *flagsp) 176 unsigned int *flagsp)
177{ 177{
178 unsigned int mapnum = map_sector(sector); 178 unsigned int mapnum = map_sector(sector);
@@ -200,7 +200,7 @@ void * dc_cache_probe(IF_MV(int volume,) unsigned long sector,
200 if (old_flags) 200 if (old_flags)
201 { 201 {
202 int old_volume = IF_MV_VOL(dce->volume); 202 int old_volume = IF_MV_VOL(dce->volume);
203 unsigned long sector = dce->sector; 203 sector_t sector = dce->sector;
204 unsigned int old_mapnum = map_sector(sector); 204 unsigned int old_mapnum = map_sector(sector);
205 205
206 if (old_flags & DCE_DIRTY) 206 if (old_flags & DCE_DIRTY)
diff --git a/firmware/drivers/ata.c b/firmware/drivers/ata.c
index d82fb173cc..7b9c4910cf 100644
--- a/firmware/drivers/ata.c
+++ b/firmware/drivers/ata.c
@@ -109,15 +109,15 @@ static long last_disk_activity = -1;
109static long power_off_tick = 0; 109static long power_off_tick = 0;
110#endif 110#endif
111 111
112static unsigned long total_sectors; 112static sector_t total_sectors;
113static int multisectors; /* number of supported multisectors */ 113static int multisectors; /* number of supported multisectors */
114static unsigned short identify_info[SECTOR_SIZE/2]; 114static unsigned short identify_info[ATA_IDENTIFY_WORDS];
115 115
116#ifdef MAX_PHYS_SECTOR_SIZE 116#ifdef MAX_PHYS_SECTOR_SIZE
117 117
118struct sector_cache_entry { 118struct sector_cache_entry {
119 bool inuse; 119 bool inuse;
120 unsigned long sectornum; /* logical sector */ 120 sector_t sectornum; /* logical sector */
121 unsigned char data[MAX_PHYS_SECTOR_SIZE]; 121 unsigned char data[MAX_PHYS_SECTOR_SIZE];
122}; 122};
123/* buffer for reading and writing large physical sectors */ 123/* buffer for reading and writing large physical sectors */
@@ -381,7 +381,7 @@ static ICODE_ATTR void copy_write_sectors(const unsigned char* buf,
381} 381}
382#endif /* !ATA_OPTIMIZED_WRITING */ 382#endif /* !ATA_OPTIMIZED_WRITING */
383 383
384static int ata_transfer_sectors(unsigned long start, 384static int ata_transfer_sectors(uint64_t start,
385 int incount, 385 int incount,
386 void* inbuf, 386 void* inbuf,
387 int write) 387 int write)
@@ -443,9 +443,9 @@ static int ata_transfer_sectors(unsigned long start,
443 ATA_OUT8(ATA_NSECTOR, count & 0xff); 443 ATA_OUT8(ATA_NSECTOR, count & 0xff);
444 ATA_OUT8(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */ 444 ATA_OUT8(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */
445 ATA_OUT8(ATA_SECTOR, start & 0xff); /* 7:0 */ 445 ATA_OUT8(ATA_SECTOR, start & 0xff); /* 7:0 */
446 ATA_OUT8(ATA_LCYL, 0); /* 39:32 */ 446 ATA_OUT8(ATA_LCYL, (start >> 32) & 0xff); /* 39:32 */
447 ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */ 447 ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */
448 ATA_OUT8(ATA_HCYL, 0); /* 47:40 */ 448 ATA_OUT8(ATA_HCYL, (start >> 40) & 0xff); /* 47:40 */
449 ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */ 449 ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */
450 ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device); 450 ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device);
451#ifdef HAVE_ATA_DMA 451#ifdef HAVE_ATA_DMA
@@ -592,7 +592,7 @@ static int ata_transfer_sectors(unsigned long start,
592 592
593#ifndef MAX_PHYS_SECTOR_SIZE 593#ifndef MAX_PHYS_SECTOR_SIZE
594int ata_read_sectors(IF_MD(int drive,) 594int ata_read_sectors(IF_MD(int drive,)
595 unsigned long start, 595 sector_t start,
596 int incount, 596 int incount,
597 void* inbuf) 597 void* inbuf)
598{ 598{
@@ -607,7 +607,7 @@ int ata_read_sectors(IF_MD(int drive,)
607} 607}
608 608
609int ata_write_sectors(IF_MD(int drive,) 609int ata_write_sectors(IF_MD(int drive,)
610 unsigned long start, 610 sector_t start,
611 int count, 611 int count,
612 const void* buf) 612 const void* buf)
613{ 613{
@@ -623,7 +623,7 @@ int ata_write_sectors(IF_MD(int drive,)
623#endif /* ndef MAX_PHYS_SECTOR_SIZE */ 623#endif /* ndef MAX_PHYS_SECTOR_SIZE */
624 624
625#ifdef MAX_PHYS_SECTOR_SIZE 625#ifdef MAX_PHYS_SECTOR_SIZE
626static int cache_sector(unsigned long sector) 626static int cache_sector(sector_t sector)
627{ 627{
628 int rc; 628 int rc;
629 629
@@ -652,7 +652,7 @@ static inline int flush_current_sector(void)
652} 652}
653 653
654int ata_read_sectors(IF_MD(int drive,) 654int ata_read_sectors(IF_MD(int drive,)
655 unsigned long start, 655 sector_t start,
656 int incount, 656 int incount,
657 void* inbuf) 657 void* inbuf)
658{ 658{
@@ -718,7 +718,7 @@ int ata_read_sectors(IF_MD(int drive,)
718} 718}
719 719
720int ata_write_sectors(IF_MD(int drive,) 720int ata_write_sectors(IF_MD(int drive,)
721 unsigned long start, 721 sector_t start,
722 int count, 722 int count,
723 const void* buf) 723 const void* buf)
724{ 724{
@@ -916,7 +916,7 @@ static int identify(void)
916 return -2; 916 return -2;
917 } 917 }
918 918
919 for (i=0; i<SECTOR_SIZE/2; i++) { 919 for (i=0; i<ATA_IDENTIFY_WORDS; i++) {
920 /* the IDENTIFY words are already swapped, so we need to treat 920 /* the IDENTIFY words are already swapped, so we need to treat
921 this info differently that normal sector data */ 921 this info differently that normal sector data */
922 identify_info[i] = ATA_SWAP_IDENTIFY(ATA_IN16(ATA_DATA)); 922 identify_info[i] = ATA_SWAP_IDENTIFY(ATA_IN16(ATA_DATA));
@@ -1269,10 +1269,8 @@ int STORAGE_INIT_ATTR ata_init(void)
1269 if (identify_info[83] & 0x0400 /* 48 bit address support */ 1269 if (identify_info[83] & 0x0400 /* 48 bit address support */
1270 && total_sectors == 0x0FFFFFFF) /* and disk size >= 128 GiB */ 1270 && total_sectors == 0x0FFFFFFF) /* and disk size >= 128 GiB */
1271 { /* (needs BigLBA addressing) */ 1271 { /* (needs BigLBA addressing) */
1272 if (identify_info[102] || identify_info[103]) 1272 total_sectors = identify_info[100] | (identify_info[101] << 16) | ((uint64_t)identify_info[102] << 32) | ((uint64_t)identify_info[103] << 48);
1273 panicf("Unsupported disk size: >= 2^32 sectors");
1274 1273
1275 total_sectors = identify_info[100] | (identify_info[101] << 16);
1276 lba48 = true; /* use BigLBA */ 1274 lba48 = true; /* use BigLBA */
1277 } 1275 }
1278#endif /* HAVE_LBA48 */ 1276#endif /* HAVE_LBA48 */
@@ -1360,7 +1358,13 @@ void ata_get_info(IF_MD(int drive,)struct storage_info *info)
1360 (void)drive; /* unused for now */ 1358 (void)drive; /* unused for now */
1361#endif 1359#endif
1362 int i; 1360 int i;
1363 info->sector_size = SECTOR_SIZE; 1361
1362 /* Logical sector size */
1363 if ((identify_info[106] & 0xd000) == 0x5000)
1364 info->sector_size = identify_info[117] | (identify_info[118] << 16);
1365 else
1366 info->sector_size = SECTOR_SIZE;
1367
1364 info->num_sectors = total_sectors; 1368 info->num_sectors = total_sectors;
1365 1369
1366 src = (unsigned short*)&identify_info[27]; 1370 src = (unsigned short*)&identify_info[27];
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c
index ebf0f92798..f3c273cc05 100644
--- a/firmware/drivers/fat.c
+++ b/firmware/drivers/fat.c
@@ -173,9 +173,9 @@ union raw_dirent
173 173
174struct fsinfo 174struct fsinfo
175{ 175{
176 unsigned long freecount; /* last known free cluster count */ 176 sector_t freecount; /* last known free cluster count */
177 unsigned long nextfree; /* first cluster to start looking for free 177 sector_t nextfree; /* first cluster to start looking for free
178 clusters, or 0xffffffff for no hint */ 178 clusters, or 0xffffffff for no hint */
179}; 179};
180/* fsinfo offsets */ 180/* fsinfo offsets */
181#define FSINFO_SIGNATURE 0 181#define FSINFO_SIGNATURE 0
@@ -233,7 +233,7 @@ static struct bpb
233 unsigned long totalsectors; 233 unsigned long totalsectors;
234 unsigned long rootdirsector; 234 unsigned long rootdirsector;
235 unsigned long firstdatasector; 235 unsigned long firstdatasector;
236 unsigned long startsector; 236 sector_t startsector;
237 unsigned long dataclusters; 237 unsigned long dataclusters;
238 unsigned long fatrgnstart; 238 unsigned long fatrgnstart;
239 unsigned long fatrgnend; 239 unsigned long fatrgnend;
@@ -241,8 +241,8 @@ static struct bpb
241#ifdef HAVE_FAT16SUPPORT 241#ifdef HAVE_FAT16SUPPORT
242 unsigned int bpb_rootentcnt; /* Number of dir entries in the root */ 242 unsigned int bpb_rootentcnt; /* Number of dir entries in the root */
243 /* internals for FAT16 support */ 243 /* internals for FAT16 support */
244 unsigned long rootdirsectornum; /* sector offset of root dir relative to start 244 sector_t rootdirsectornum; /* sector offset of root dir relative to start
245 * of first pseudo cluster */ 245 * of first pseudo cluster */
246#endif /* HAVE_FAT16SUPPORT */ 246#endif /* HAVE_FAT16SUPPORT */
247 247
248 /** Additional information kept for each volume **/ 248 /** Additional information kept for each volume **/
@@ -329,7 +329,7 @@ static void cache_discard(IF_MV_NONVOID(struct bpb *fat_bpb))
329} 329}
330 330
331/* caches a FAT or data area sector */ 331/* caches a FAT or data area sector */
332static void * cache_sector(struct bpb *fat_bpb, unsigned long secnum) 332static void * cache_sector(struct bpb *fat_bpb, sector_t secnum)
333{ 333{
334 unsigned int flags; 334 unsigned int flags;
335 void *buf = dc_cache_probe(IF_MV(fat_bpb->volume,) secnum, &flags); 335 void *buf = dc_cache_probe(IF_MV(fat_bpb->volume,) secnum, &flags);
@@ -340,8 +340,8 @@ static void * cache_sector(struct bpb *fat_bpb, unsigned long secnum)
340 secnum + fat_bpb->startsector, 1, buf); 340 secnum + fat_bpb->startsector, 1, buf);
341 if (UNLIKELY(rc < 0)) 341 if (UNLIKELY(rc < 0))
342 { 342 {
343 DEBUGF("%s() - Could not read sector %ld" 343 DEBUGF("%s() - Could not read sector %llu"
344 " (error %d)\n", __func__, secnum, rc); 344 " (error %d)\n", __func__, (uint64_t)secnum, rc);
345 dc_discard_buf(buf); 345 dc_discard_buf(buf);
346 return NULL; 346 return NULL;
347 } 347 }
@@ -354,14 +354,14 @@ static void * cache_sector(struct bpb *fat_bpb, unsigned long secnum)
354 * contents are NOT loaded before returning - use when completely overwriting 354 * contents are NOT loaded before returning - use when completely overwriting
355 * a sector's contents in order to avoid a fill */ 355 * a sector's contents in order to avoid a fill */
356static void * cache_sector_buffer(IF_MV(struct bpb *fat_bpb,) 356static void * cache_sector_buffer(IF_MV(struct bpb *fat_bpb,)
357 unsigned long secnum) 357 sector_t secnum)
358{ 358{
359 unsigned int flags; 359 unsigned int flags;
360 return dc_cache_probe(IF_MV(fat_bpb->volume,) secnum, &flags); 360 return dc_cache_probe(IF_MV(fat_bpb->volume,) secnum, &flags);
361} 361}
362 362
363/* flush a cache buffer to storage */ 363/* flush a cache buffer to storage */
364void dc_writeback_callback(IF_MV(int volume,) unsigned long sector, void *buf) 364void dc_writeback_callback(IF_MV(int volume,) sector_t sector, void *buf)
365{ 365{
366 struct bpb * const fat_bpb = &fat_bpbs[IF_MV_VOL(volume)]; 366 struct bpb * const fat_bpb = &fat_bpbs[IF_MV_VOL(volume)];
367 unsigned int copies = !IS_FAT_SECTOR(fat_bpb, sector) ? 367 unsigned int copies = !IS_FAT_SECTOR(fat_bpb, sector) ?
@@ -374,8 +374,8 @@ void dc_writeback_callback(IF_MV(int volume,) unsigned long sector, void *buf)
374 int rc = storage_write_sectors(IF_MD(fat_bpb->drive,) sector, 1, buf); 374 int rc = storage_write_sectors(IF_MD(fat_bpb->drive,) sector, 1, buf);
375 if (rc < 0) 375 if (rc < 0)
376 { 376 {
377 panicf("%s() - Could not write sector %ld" 377 panicf("%s() - Could not write sector %llu"
378 " (error %d)\n", __func__, sector, rc); 378 " (error %d)\n", __func__, (uint64_t)sector, rc);
379 } 379 }
380 380
381 if (--copies == 0) 381 if (--copies == 0)
@@ -2397,12 +2397,12 @@ unsigned long fat_query_sectornum(const struct fat_filestr *filestr)
2397} 2397}
2398 2398
2399/* helper for fat_readwrite */ 2399/* helper for fat_readwrite */
2400static long transfer(struct bpb *fat_bpb, unsigned long start, long count, 2400static long transfer(struct bpb *fat_bpb, sector_t start, long count,
2401 char *buf, bool write) 2401 char *buf, bool write)
2402{ 2402{
2403 long rc = 0; 2403 long rc = 0;
2404 2404
2405 DEBUGF("%s(s=%lx, c=%lx, wr=%u)\n", __func__, 2405 DEBUGF("%s(s=%llx, c=%lx, wr=%u)\n", __func__,
2406 start + fat_bpb->startsector, count, write ? 1 : 0); 2406 start + fat_bpb->startsector, count, write ? 1 : 0);
2407 2407
2408 if (write) 2408 if (write)
@@ -2416,12 +2416,12 @@ static long transfer(struct bpb *fat_bpb, unsigned long start, long count,
2416 firstallowed = fat_bpb->firstdatasector; 2416 firstallowed = fat_bpb->firstdatasector;
2417 2417
2418 if (start < firstallowed) 2418 if (start < firstallowed)
2419 panicf("Write %ld before data\n", firstallowed - start); 2419 panicf("Write %llu before data\n", (uint64_t)(firstallowed - start));
2420 2420
2421 if (start + count > fat_bpb->totalsectors) 2421 if (start + count > fat_bpb->totalsectors)
2422 { 2422 {
2423 panicf("Write %ld after data\n", 2423 panicf("Write %llu after data\n",
2424 start + count - fat_bpb->totalsectors); 2424 (uint64_t)(start + count - fat_bpb->totalsectors));
2425 } 2425 }
2426 } 2426 }
2427 2427
@@ -2487,14 +2487,14 @@ long fat_readwrite(struct fat_filestr *filestr, unsigned long sectorcount,
2487 long rc; 2487 long rc;
2488 2488
2489 long cluster = filestr->lastcluster; 2489 long cluster = filestr->lastcluster;
2490 unsigned long sector = filestr->lastsector; 2490 sector_t sector = filestr->lastsector;
2491 long clusternum = filestr->clusternum; 2491 long clusternum = filestr->clusternum;
2492 unsigned long sectornum = filestr->sectornum; 2492 unsigned long sectornum = filestr->sectornum;
2493 2493
2494 DEBUGF("%s(file:%lx,count:0x%lx,buf:%lx,%s)\n", __func__, 2494 DEBUGF("%s(file:%lx,count:0x%lx,buf:%lx,%s)\n", __func__,
2495 file->firstcluster, sectorcount, (long)buf, 2495 file->firstcluster, sectorcount, (long)buf,
2496 write ? "write":"read"); 2496 write ? "write":"read");
2497 DEBUGF("%s: sec:%lx numsec:%ld eof:%d\n", __func__, 2497 DEBUGF("%s: sec:%llx numsec:%ld eof:%d\n", __func__,
2498 sector, (long)sectornum, eof ? 1 : 0); 2498 sector, (long)sectornum, eof ? 1 : 0);
2499 2499
2500 eof = false; 2500 eof = false;
@@ -2534,7 +2534,7 @@ long fat_readwrite(struct fat_filestr *filestr, unsigned long sectorcount,
2534 2534
2535 unsigned long transferred = 0; 2535 unsigned long transferred = 0;
2536 unsigned long count = 0; 2536 unsigned long count = 0;
2537 unsigned long last = sector; 2537 sector_t last = sector;
2538 2538
2539 while (transferred + count < sectorcount) 2539 while (transferred + count < sectorcount)
2540 { 2540 {
@@ -2961,7 +2961,7 @@ void fat_recalc_free(IF_MV_NONVOID(int volume))
2961 dc_unlock_cache(); 2961 dc_unlock_cache();
2962} 2962}
2963 2963
2964bool fat_size(IF_MV(int volume,) unsigned long *size, unsigned long *free) 2964bool fat_size(IF_MV(int volume,) sector_t *size, sector_t *free)
2965{ 2965{
2966 struct bpb * const fat_bpb = FAT_BPB(volume); 2966 struct bpb * const fat_bpb = FAT_BPB(volume);
2967 if (!fat_bpb) 2967 if (!fat_bpb)
diff --git a/firmware/drivers/ramdisk.c b/firmware/drivers/ramdisk.c
index 9f73b6b5c3..cecc943352 100644
--- a/firmware/drivers/ramdisk.c
+++ b/firmware/drivers/ramdisk.c
@@ -32,7 +32,7 @@ static unsigned char ramdisk[SECTOR_SIZE * NUM_SECTORS];
32static long last_disk_activity = -1; 32static long last_disk_activity = -1;
33 33
34int ramdisk_read_sectors(IF_MD(int drive,) 34int ramdisk_read_sectors(IF_MD(int drive,)
35 unsigned long start, 35 sector_t start,
36 int count, 36 int count,
37 void* buf) 37 void* buf)
38{ 38{
@@ -48,7 +48,7 @@ int ramdisk_read_sectors(IF_MD(int drive,)
48} 48}
49 49
50int ramdisk_write_sectors(IF_MD(int drive,) 50int ramdisk_write_sectors(IF_MD(int drive,)
51 unsigned long start, 51 sector_t start,
52 int count, 52 int count,
53 const void* buf) 53 const void* buf)
54{ 54{
@@ -134,7 +134,7 @@ int ramdisk_num_drives(int first_drive)
134{ 134{
135 /* We don't care which logical drive number(s) we have been assigned */ 135 /* We don't care which logical drive number(s) we have been assigned */
136 (void)first_drive; 136 (void)first_drive;
137 137
138 return 1; 138 return 1;
139} 139}
140#endif 140#endif
diff --git a/firmware/drivers/sd.c b/firmware/drivers/sd.c
index 6185d5382d..ca83498087 100644
--- a/firmware/drivers/sd.c
+++ b/firmware/drivers/sd.c
@@ -49,6 +49,13 @@ void sd_parse_csd(tCardInfo *card)
49 c_size = card_extract_bits(card->csd, 69, 22) + 1; 49 c_size = card_extract_bits(card->csd, 69, 22) + 1;
50 card->numblocks = c_size << 10; 50 card->numblocks = c_size << 10;
51 } 51 }
52 else if(csd_version == 2)
53 {
54 /* CSD version 3.0 */
55 c_size = card_extract_bits(card->csd, 75, 28) + 1;
56 card->numblocks = c_size << 10;
57 }
58 card->sd2plus = csd_version >= 1;
52 59
53 card->blocksize = 512; /* Always use 512 byte blocks */ 60 card->blocksize = 512; /* Always use 512 byte blocks */
54 61
@@ -62,7 +69,9 @@ void sd_parse_csd(tCardInfo *card)
62 69
63 card->r2w_factor = card_extract_bits(card->csd, 28, 3); 70 card->r2w_factor = card_extract_bits(card->csd, 28, 3);
64 71
65 logf("CSD%d.0 numblocks:%ld speed:%ld", csd_version+1, card->numblocks, card->speed); 72
73
74 logf("CSD%d.0 numblocks:%lld speed:%ld", csd_version+1, card->numblocks, card->speed);
66 logf("nsac: %d taac: %ld r2w: %d", card->nsac, card->taac, card->r2w_factor); 75 logf("nsac: %d taac: %ld r2w: %d", card->nsac, card->taac, card->r2w_factor);
67} 76}
68 77
@@ -99,4 +108,3 @@ void sd_get_info(IF_MD(int drive,) struct storage_info *info)
99 info->revision="0.00"; 108 info->revision="0.00";
100} 109}
101#endif 110#endif
102
diff --git a/firmware/export/ata.h b/firmware/export/ata.h
index e46b0fee71..6165eaf633 100644
--- a/firmware/export/ata.h
+++ b/firmware/export/ata.h
@@ -140,8 +140,8 @@ bool ata_disk_is_active(void);
140int ata_soft_reset(void); 140int ata_soft_reset(void);
141int ata_init(void) STORAGE_INIT_ATTR; 141int ata_init(void) STORAGE_INIT_ATTR;
142void ata_close(void); 142void ata_close(void);
143int ata_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf); 143int ata_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf);
144int ata_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf); 144int ata_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf);
145void ata_spin(void); 145void ata_spin(void);
146#if (CONFIG_LED == LED_REAL) 146#if (CONFIG_LED == LED_REAL)
147void ata_set_led_enabled(bool enabled); 147void ata_set_led_enabled(bool enabled);
@@ -233,4 +233,6 @@ int ata_read_smart(struct ata_smart_values*);
233#define STORAGE_CLOSE 233#define STORAGE_CLOSE
234#endif 234#endif
235 235
236#define ATA_IDENTIFY_WORDS 256
237
236#endif /* __ATA_H__ */ 238#endif /* __ATA_H__ */
diff --git a/firmware/export/disk.h b/firmware/export/disk.h
index e465552fdc..a19e011170 100644
--- a/firmware/export/disk.h
+++ b/firmware/export/disk.h
@@ -26,8 +26,8 @@
26 26
27struct partinfo 27struct partinfo
28{ 28{
29 unsigned long start; /* first sector (LBA) */ 29 sector_t start; /* first sector (LBA) */
30 unsigned long size; /* number of sectors */ 30 sector_t size; /* number of sectors */
31 unsigned char type; 31 unsigned char type;
32}; 32};
33 33
diff --git a/firmware/export/fat.h b/firmware/export/fat.h
index 70152985b5..7382d85ce4 100644
--- a/firmware/export/fat.h
+++ b/firmware/export/fat.h
@@ -115,7 +115,7 @@ struct fat_filestr
115{ 115{
116 struct fat_file *fatfilep; /* common file information */ 116 struct fat_file *fatfilep; /* common file information */
117 long lastcluster; /* cluster of last access */ 117 long lastcluster; /* cluster of last access */
118 unsigned long lastsector; /* sector of last access */ 118 sector_t lastsector; /* sector of last access */
119 long clusternum; /* cluster number of last access */ 119 long clusternum; /* cluster number of last access */
120 unsigned long sectornum; /* sector number within current cluster */ 120 unsigned long sectornum; /* sector number within current cluster */
121 bool eof; /* end-of-file reached */ 121 bool eof; /* end-of-file reached */
@@ -173,7 +173,7 @@ int fat_get_bytes_per_sector(IF_MV_NONVOID(int volume));
173#endif /* MAX_LOG_SECTOR_SIZE */ 173#endif /* MAX_LOG_SECTOR_SIZE */
174unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume)); 174unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume));
175void fat_recalc_free(IF_MV_NONVOID(int volume)); 175void fat_recalc_free(IF_MV_NONVOID(int volume));
176bool fat_size(IF_MV(int volume,) unsigned long *size, unsigned long *free); 176bool fat_size(IF_MV(int volume,) sector_t *size, sector_t *free);
177 177
178/** Misc. **/ 178/** Misc. **/
179void fat_empty_fat_direntry(struct fat_direntry *entry); 179void fat_empty_fat_direntry(struct fat_direntry *entry);
diff --git a/firmware/export/mmc.h b/firmware/export/mmc.h
index 0d27e20a64..06df979b81 100644
--- a/firmware/export/mmc.h
+++ b/firmware/export/mmc.h
@@ -36,8 +36,8 @@ bool mmc_disk_is_active(void);
36int mmc_soft_reset(void); 36int mmc_soft_reset(void);
37int mmc_init(void) STORAGE_INIT_ATTR; 37int mmc_init(void) STORAGE_INIT_ATTR;
38void mmc_close(void); 38void mmc_close(void);
39int mmc_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf); 39int mmc_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf);
40int mmc_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf); 40int mmc_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf);
41void mmc_spin(void); 41void mmc_spin(void);
42int mmc_spinup_time(void); 42int mmc_spinup_time(void);
43 43
diff --git a/firmware/export/mv.h b/firmware/export/mv.h
index 1122c7b85f..9d202a8087 100644
--- a/firmware/export/mv.h
+++ b/firmware/export/mv.h
@@ -23,6 +23,7 @@
23#define __MV_H__ 23#define __MV_H__
24 24
25#include <stdbool.h> 25#include <stdbool.h>
26#include <stdint.h>
26#include "config.h" 27#include "config.h"
27 28
28/* FixMe: These macros are a bit nasty and perhaps misplaced here. 29/* FixMe: These macros are a bit nasty and perhaps misplaced here.
@@ -40,6 +41,19 @@
40#define IF_MD_DRV(d) 0 41#define IF_MD_DRV(d) 0
41#endif /* HAVE_MULTIDRIVE */ 42#endif /* HAVE_MULTIDRIVE */
42 43
44/* Storage size */
45#if (CONFIG_STORAGE & STORAGE_ATA) && defined(HAVE_LBA48)
46typedef uint64_t sector_t;
47#define STORAGE_64BIT_SECTOR
48#elif (CONFIG_STORAGE & STORAGE_SD) && defined(HAVE_SDUC)
49typedef uint64_t sector_t;
50#define STORAGE_64BIT_SECTOR
51#else
52typedef unsigned long sector_t;
53#undef STORAGE_64BIT_SECTOR
54#endif
55
56
43/* Volumes mean things that have filesystems on them, like partitions */ 57/* Volumes mean things that have filesystems on them, like partitions */
44#ifdef HAVE_MULTIVOLUME 58#ifdef HAVE_MULTIVOLUME
45#define IF_MV(x...) x /* valist contents or empty */ 59#define IF_MV(x...) x /* valist contents or empty */
@@ -113,7 +127,7 @@ struct volumeinfo
113/* Volume-centric functions (in disk.c) */ 127/* Volume-centric functions (in disk.c) */
114void volume_recalc_free(IF_MV_NONVOID(int volume)); 128void volume_recalc_free(IF_MV_NONVOID(int volume));
115unsigned int volume_get_cluster_size(IF_MV_NONVOID(int volume)); 129unsigned int volume_get_cluster_size(IF_MV_NONVOID(int volume));
116void volume_size(IF_MV(int volume,) unsigned long *size, unsigned long *free); 130void volume_size(IF_MV(int volume,) sector_t *size, sector_t *free);
117#ifdef HAVE_DIRCACHE 131#ifdef HAVE_DIRCACHE
118bool volume_ismounted(IF_MV_NONVOID(int volume)); 132bool volume_ismounted(IF_MV_NONVOID(int volume));
119#endif 133#endif
diff --git a/firmware/export/nand.h b/firmware/export/nand.h
index fe25c9b407..d4fd6028ed 100644
--- a/firmware/export/nand.h
+++ b/firmware/export/nand.h
@@ -36,8 +36,8 @@ bool nand_disk_is_active(void);
36int nand_soft_reset(void); 36int nand_soft_reset(void);
37int nand_init(void) STORAGE_INIT_ATTR; 37int nand_init(void) STORAGE_INIT_ATTR;
38void nand_close(void); 38void nand_close(void);
39int nand_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf); 39int nand_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf);
40int nand_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf); 40int nand_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf);
41#ifdef HAVE_STORAGE_FLUSH 41#ifdef HAVE_STORAGE_FLUSH
42int nand_flush(void); 42int nand_flush(void);
43#endif 43#endif
diff --git a/firmware/export/ramdisk.h b/firmware/export/ramdisk.h
index d79ac50836..eb06ea9650 100644
--- a/firmware/export/ramdisk.h
+++ b/firmware/export/ramdisk.h
@@ -35,8 +35,8 @@ bool ramdisk_disk_is_active(void);
35int ramdisk_soft_reset(void); 35int ramdisk_soft_reset(void);
36int ramdisk_init(void) STORAGE_INIT_ATTR; 36int ramdisk_init(void) STORAGE_INIT_ATTR;
37void ramdisk_close(void); 37void ramdisk_close(void);
38int ramdisk_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf); 38int ramdisk_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf);
39int ramdisk_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf); 39int ramdisk_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf);
40void ramdisk_spin(void); 40void ramdisk_spin(void);
41void ramdisk_sleepnow(void); 41void ramdisk_sleepnow(void);
42int ramdisk_spinup_time(void); 42int ramdisk_spinup_time(void);
diff --git a/firmware/export/sd.h b/firmware/export/sd.h
index c657f8a545..d66e6f899c 100644
--- a/firmware/export/sd.h
+++ b/firmware/export/sd.h
@@ -42,8 +42,8 @@ bool sd_disk_is_active(void);
42int sd_soft_reset(void); 42int sd_soft_reset(void);
43int sd_init(void) STORAGE_INIT_ATTR; 43int sd_init(void) STORAGE_INIT_ATTR;
44void sd_close(void); 44void sd_close(void);
45int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf); 45int sd_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf);
46int sd_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf); 46int sd_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf);
47void sd_spin(void); 47void sd_spin(void);
48int sd_spinup_time(void); /* ticks */ 48int sd_spinup_time(void); /* ticks */
49 49
@@ -93,6 +93,7 @@ int sd_num_drives(int first_drive);
93#define SD_READ_SINGLE_BLOCK 17 93#define SD_READ_SINGLE_BLOCK 17
94#define SD_READ_MULTIPLE_BLOCK 18 94#define SD_READ_MULTIPLE_BLOCK 18
95#define SD_SEND_NUM_WR_BLOCKS 22 /* acmd22 */ 95#define SD_SEND_NUM_WR_BLOCKS 22 /* acmd22 */
96#define SD_UC_ADDRESS_EXTENSION 22
96#define SD_SET_WR_BLK_ERASE_COUNT 23 /* acmd23 */ 97#define SD_SET_WR_BLK_ERASE_COUNT 23 /* acmd23 */
97#define SD_WRITE_BLOCK 24 98#define SD_WRITE_BLOCK 24
98#define SD_WRITE_MULTIPLE_BLOCK 25 99#define SD_WRITE_MULTIPLE_BLOCK 25
diff --git a/firmware/export/sdmmc.h b/firmware/export/sdmmc.h
index 35539169d9..f84a7b96fa 100644
--- a/firmware/export/sdmmc.h
+++ b/firmware/export/sdmmc.h
@@ -22,9 +22,11 @@
22#define __SDMMC_H__ 22#define __SDMMC_H__
23 23
24#include <stdbool.h> 24#include <stdbool.h>
25#include <stdint.h>
26#include <mv.h> /* for sector_t */
25 27
26typedef struct 28typedef struct
27{ 29{
28 bool initialized; 30 bool initialized;
29 31
30 unsigned long read_timeout; /* n * 8 clock cycles */ 32 unsigned long read_timeout; /* n * 8 clock cycles */
@@ -37,7 +39,7 @@ typedef struct
37 unsigned int nsac; /* clock cycles */ 39 unsigned int nsac; /* clock cycles */
38 unsigned long taac; /* n * 0.1 ns */ 40 unsigned long taac; /* n * 0.1 ns */
39 unsigned int r2w_factor; 41 unsigned int r2w_factor;
40 unsigned long numblocks; /* size in flash blocks */ 42 sector_t numblocks; /* size in flash blocks */
41 unsigned int blocksize; /* block size in bytes */ 43 unsigned int blocksize; /* block size in bytes */
42 unsigned long rca; /* RCA register */ 44 unsigned long rca; /* RCA register */
43 45
@@ -48,6 +50,8 @@ typedef struct
48#if (CONFIG_STORAGE & STORAGE_SD) 50#if (CONFIG_STORAGE & STORAGE_SD)
49 unsigned int current_bank; 51 unsigned int current_bank;
50#endif 52#endif
53
54 unsigned int sd2plus; /* SD 2.0 or better */
51} tCardInfo; 55} tCardInfo;
52 56
53#if (CONFIG_STORAGE & STORAGE_SD) 57#if (CONFIG_STORAGE & STORAGE_SD)
diff --git a/firmware/export/storage.h b/firmware/export/storage.h
index b72cccc257..e2ae4056be 100644
--- a/firmware/export/storage.h
+++ b/firmware/export/storage.h
@@ -107,7 +107,7 @@ int ramdisk_event(long id, intptr_t data);
107struct storage_info 107struct storage_info
108{ 108{
109 unsigned int sector_size; 109 unsigned int sector_size;
110 unsigned int num_sectors; 110 sector_t num_sectors;
111 char *vendor; 111 char *vendor;
112 char *product; 112 char *product;
113 char *revision; 113 char *revision;
@@ -318,6 +318,6 @@ int storage_driver_type(int drive);
318 318
319#endif /* NOT CONFIG_STORAGE_MULTI and NOT SIMULATOR*/ 319#endif /* NOT CONFIG_STORAGE_MULTI and NOT SIMULATOR*/
320 320
321int storage_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf); 321int storage_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf);
322int storage_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf); 322int storage_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf);
323#endif 323#endif
diff --git a/firmware/include/disk_cache.h b/firmware/include/disk_cache.h
index 79b2ccf2c6..9787c64aec 100644
--- a/firmware/include/disk_cache.h
+++ b/firmware/include/disk_cache.h
@@ -36,7 +36,7 @@ static inline void dc_unlock_cache(void)
36 mutex_unlock(&disk_cache_mutex); 36 mutex_unlock(&disk_cache_mutex);
37} 37}
38 38
39void * dc_cache_probe(IF_MV(int volume,) unsigned long secnum, 39void * dc_cache_probe(IF_MV(int volume,) sector_t secnum,
40 unsigned int *flags); 40 unsigned int *flags);
41void dc_dirty_buf(void *buf); 41void dc_dirty_buf(void *buf);
42void dc_discard_buf(void *buf); 42void dc_discard_buf(void *buf);
@@ -46,7 +46,7 @@ void dc_discard_all(IF_MV_NONVOID(int volume));
46void dc_init(void) INIT_ATTR; 46void dc_init(void) INIT_ATTR;
47 47
48/* in addition to filling, writeback is implemented by the client */ 48/* in addition to filling, writeback is implemented by the client */
49extern void dc_writeback_callback(IF_MV(int volume, ) unsigned long sector, 49extern void dc_writeback_callback(IF_MV(int volume, ) sector_t sector,
50 void *buf); 50 void *buf);
51 51
52 52
diff --git a/firmware/storage.c b/firmware/storage.c
index ac4e35b66b..da3e06146d 100644
--- a/firmware/storage.c
+++ b/firmware/storage.c
@@ -343,7 +343,7 @@ int storage_init(void)
343 return rc; 343 return rc;
344} 344}
345 345
346int storage_read_sectors(IF_MD(int drive,) unsigned long start, int count, 346int storage_read_sectors(IF_MD(int drive,) sector_t start, int count,
347 void* buf) 347 void* buf)
348{ 348{
349#ifdef CONFIG_STORAGE_MULTI 349#ifdef CONFIG_STORAGE_MULTI
@@ -385,7 +385,7 @@ int storage_read_sectors(IF_MD(int drive,) unsigned long start, int count,
385 385
386} 386}
387 387
388int storage_write_sectors(IF_MD(int drive,) unsigned long start, int count, 388int storage_write_sectors(IF_MD(int drive,) sector_t start, int count,
389 const void* buf) 389 const void* buf)
390{ 390{
391#ifdef CONFIG_STORAGE_MULTI 391#ifdef CONFIG_STORAGE_MULTI
diff --git a/firmware/target/arm/as3525/sd-as3525.c b/firmware/target/arm/as3525/sd-as3525.c
index 70e0778d2b..df39cba495 100644
--- a/firmware/target/arm/as3525/sd-as3525.c
+++ b/firmware/target/arm/as3525/sd-as3525.c
@@ -446,7 +446,7 @@ static int sd_init_card(const int drive)
446 sd_parse_csd(&card_info[drive]); 446 sd_parse_csd(&card_info[drive]);
447 447
448#if defined(HAVE_MULTIDRIVE) 448#if defined(HAVE_MULTIDRIVE)
449 hs_card = (card_info[drive].speed == 50000000); 449 hs_card = (card_info[drive].speed >= 50000000);
450#endif 450#endif
451 451
452 /* Boost MCICLK to operating speed */ 452 /* Boost MCICLK to operating speed */
@@ -455,7 +455,7 @@ static int sd_init_card(const int drive)
455#if defined(HAVE_MULTIDRIVE) 455#if defined(HAVE_MULTIDRIVE)
456 else 456 else
457 /* MCICLK = PCLK/2 = 31MHz(HS) or PCLK/4 = 15.5 Mhz (STD)*/ 457 /* MCICLK = PCLK/2 = 31MHz(HS) or PCLK/4 = 15.5 Mhz (STD)*/
458 MCI_CLOCK(drive) = (hs_card ? MCI_HALFSPEED : MCI_QUARTERSPEED) | 458 MCI_CLOCK(drive) = (hs_card ? MCI_HALFSPEED : MCI_QUARTERSPEED) |
459 MCI_CLOCK_POWERSAVE; /* SD supports powersave */ 459 MCI_CLOCK_POWERSAVE; /* SD supports powersave */
460#endif 460#endif
461 461
@@ -680,7 +680,7 @@ static int sd_select_bank(signed char bank)
680 return 0; 680 return 0;
681} 681}
682 682
683static int sd_transfer_sectors(IF_MD(int drive,) unsigned long start, 683static int sd_transfer_sectors(IF_MD(int drive,) sector_t start,
684 int count, void* buf, const bool write) 684 int count, void* buf, const bool write)
685{ 685{
686#ifndef HAVE_MULTIDRIVE 686#ifndef HAVE_MULTIDRIVE
@@ -735,7 +735,8 @@ static int sd_transfer_sectors(IF_MD(int drive,) unsigned long start,
735 unsigned int transfer = (count >= 128) ? 127 : count; /* sectors */ 735 unsigned int transfer = (count >= 128) ? 127 : count; /* sectors */
736 void *dma_buf; 736 void *dma_buf;
737 737
738 unsigned long bank_start = start; 738 sector_t bank_start = start;
739 // XXX 64-bit sectors?
739 740
740 /* Only switch banks for internal storage */ 741 /* Only switch banks for internal storage */
741 if(drive == INTERNAL_AS3525) 742 if(drive == INTERNAL_AS3525)
@@ -869,7 +870,7 @@ sd_transfer_error_nodma:
869 return ret; 870 return ret;
870} 871}
871 872
872int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count, 873int sd_read_sectors(IF_MD(int drive,) sector_t start, int count,
873 void* buf) 874 void* buf)
874{ 875{
875 int ret; 876 int ret;
@@ -881,11 +882,11 @@ int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count,
881 return ret; 882 return ret;
882} 883}
883 884
884int sd_write_sectors(IF_MD(int drive,) unsigned long start, int count, 885int sd_write_sectors(IF_MD(int drive,) sector_t start, int count,
885 const void* buf) 886 const void* buf)
886{ 887{
887#ifdef VERIFY_WRITE 888#ifdef VERIFY_WRITE
888 unsigned long saved_start = start; 889 sector_t saved_start = start;
889 int saved_count = count; 890 int saved_count = count;
890 void *saved_buf = (void*)buf; 891 void *saved_buf = (void*)buf;
891#endif 892#endif
diff --git a/firmware/target/arm/as3525/sd-as3525v2.c b/firmware/target/arm/as3525/sd-as3525v2.c
index b512cc2ea4..f9ef7d9c97 100644
--- a/firmware/target/arm/as3525/sd-as3525v2.c
+++ b/firmware/target/arm/as3525/sd-as3525v2.c
@@ -677,7 +677,7 @@ int sd_init(void)
677 return 0; 677 return 0;
678} 678}
679 679
680static int sd_transfer_sectors(IF_MD(int drive,) unsigned long start, 680static int sd_transfer_sectors(IF_MD(int drive,) sector_t start,
681 int count, void* buf, bool write) 681 int count, void* buf, bool write)
682{ 682{
683 unsigned long response; 683 unsigned long response;
@@ -776,7 +776,7 @@ retry_with_reinit:
776 776
777 MCI_BYTCNT = transfer * SD_BLOCK_SIZE; 777 MCI_BYTCNT = transfer * SD_BLOCK_SIZE;
778 778
779 int arg = start; 779 sector_t arg = start; // XXX 64-bit
780 if(!(card_info[drive].ocr & (1<<30))) /* not SDHC */ 780 if(!(card_info[drive].ocr & (1<<30))) /* not SDHC */
781 arg *= SD_BLOCK_SIZE; 781 arg *= SD_BLOCK_SIZE;
782 782
@@ -858,13 +858,13 @@ exit:
858 return ret; 858 return ret;
859} 859}
860 860
861int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count, 861int sd_read_sectors(IF_MD(int drive,) sector_t start, int count,
862 void* buf) 862 void* buf)
863{ 863{
864 return sd_transfer_sectors(IF_MD(drive,) start, count, buf, false); 864 return sd_transfer_sectors(IF_MD(drive,) start, count, buf, false);
865} 865}
866 866
867int sd_write_sectors(IF_MD(int drive,) unsigned long start, int count, 867int sd_write_sectors(IF_MD(int drive,) sector_t start, int count,
868 const void* buf) 868 const void* buf)
869{ 869{
870 return sd_transfer_sectors(IF_MD(drive,) start, count, (void*)buf, true); 870 return sd_transfer_sectors(IF_MD(drive,) start, count, (void*)buf, true);
diff --git a/firmware/target/arm/ata-nand-telechips.c b/firmware/target/arm/ata-nand-telechips.c
index d61c223219..453a51ed1b 100644
--- a/firmware/target/arm/ata-nand-telechips.c
+++ b/firmware/target/arm/ata-nand-telechips.c
@@ -179,7 +179,7 @@ static int phys_segment_to_page_addr(int phys_segment, int page_in_seg)
179 break; 179 break;
180 } 180 }
181 } 181 }
182 182
183 page_addr += (page_in_seg / nand_data->planes); 183 page_addr += (page_in_seg / nand_data->planes);
184 184
185 return page_addr; 185 return page_addr;
@@ -222,7 +222,7 @@ static void nand_chip_select(int bank)
222static void nand_read_id(int bank, unsigned char* id_buf) 222static void nand_read_id(int bank, unsigned char* id_buf)
223{ 223{
224 int i; 224 int i;
225 225
226 /* Enable NFC bus clock */ 226 /* Enable NFC bus clock */
227 BCLKCTR |= DEV_NAND; 227 BCLKCTR |= DEV_NAND;
228 228
@@ -358,7 +358,7 @@ static void nand_setup_read(int bank, int row, int column)
358static void nand_end_read(void) 358static void nand_end_read(void)
359{ 359{
360 nand_chip_select(-1); 360 nand_chip_select(-1);
361 361
362 /* Disable NFC bus clock */ 362 /* Disable NFC bus clock */
363 BCLKCTR &= ~DEV_NAND; 363 BCLKCTR &= ~DEV_NAND;
364} 364}
@@ -367,7 +367,7 @@ static void nand_end_read(void)
367static void nand_read_raw(int bank, int row, int column, int size, void* buf) 367static void nand_read_raw(int bank, int row, int column, int size, void* buf)
368{ 368{
369 int i; 369 int i;
370 370
371 nand_setup_read(bank, row, column); 371 nand_setup_read(bank, row, column);
372 372
373 /* Read data into page buffer */ 373 /* Read data into page buffer */
@@ -388,7 +388,7 @@ static void nand_read_raw(int bank, int row, int column, int size, void* buf)
388 ((unsigned int*)buf)[i] = NFC_WDATA; 388 ((unsigned int*)buf)[i] = NFC_WDATA;
389 } 389 }
390 } 390 }
391 391
392 nand_end_read(); 392 nand_end_read();
393} 393}
394 394
@@ -422,7 +422,7 @@ static void nand_get_chip_info(void)
422 sectors_per_page = nand_data->page_size / SECTOR_SIZE; 422 sectors_per_page = nand_data->page_size / SECTOR_SIZE;
423 423
424 sectors_per_segment = bytes_per_segment / SECTOR_SIZE; 424 sectors_per_segment = bytes_per_segment / SECTOR_SIZE;
425 425
426 pages_per_segment = sectors_per_segment / sectors_per_page; 426 pages_per_segment = sectors_per_segment / sectors_per_page;
427 427
428 /* Establish how many banks are present */ 428 /* Establish how many banks are present */
@@ -494,7 +494,7 @@ static bool nand_read_sector_of_phys_page(int bank, int page,
494 494
495#ifdef USE_ECC_CORRECTION 495#ifdef USE_ECC_CORRECTION
496 unsigned long spare_buf[4]; 496 unsigned long spare_buf[4];
497 497
498 /* Set up the ECC controller to monitor reads from NFC_WDATA */ 498 /* Set up the ECC controller to monitor reads from NFC_WDATA */
499 BCLKCTR |= DEV_ECC; 499 BCLKCTR |= DEV_ECC;
500 ECC_BASE = (unsigned long)&NFC_WDATA; 500 ECC_BASE = (unsigned long)&NFC_WDATA;
@@ -514,27 +514,27 @@ static bool nand_read_sector_of_phys_page(int bank, int page,
514 This way, reads are always done through NFC_WDATA - otherwise they 514 This way, reads are always done through NFC_WDATA - otherwise they
515 would not be 'seen' by the ECC controller. */ 515 would not be 'seen' by the ECC controller. */
516 static char temp_buf[SECTOR_SIZE]; 516 static char temp_buf[SECTOR_SIZE];
517 517
518 unsigned int* ptr = (unsigned int*) temp_buf; 518 unsigned int* ptr = (unsigned int*) temp_buf;
519 519
520 for (i = 0; i < (SECTOR_SIZE/4); i++) 520 for (i = 0; i < (SECTOR_SIZE/4); i++)
521 { 521 {
522 *ptr++ = NFC_WDATA; 522 *ptr++ = NFC_WDATA;
523 } 523 }
524 524
525 memcpy(buf, temp_buf, SECTOR_SIZE); 525 memcpy(buf, temp_buf, SECTOR_SIZE);
526 } 526 }
527 else 527 else
528 { 528 {
529 /* Use straight word copy as buffer and size are both word-aligned */ 529 /* Use straight word copy as buffer and size are both word-aligned */
530 unsigned int* ptr = (unsigned int*) buf; 530 unsigned int* ptr = (unsigned int*) buf;
531 531
532 for (i = 0; i < (SECTOR_SIZE/4); i++) 532 for (i = 0; i < (SECTOR_SIZE/4); i++)
533 { 533 {
534 *ptr++ = NFC_WDATA; 534 *ptr++ = NFC_WDATA;
535 } 535 }
536 } 536 }
537 537
538#ifdef USE_ECC_CORRECTION 538#ifdef USE_ECC_CORRECTION
539 /* Stop monitoring before we read the OOB data */ 539 /* Stop monitoring before we read the OOB data */
540 ECC_CTRL &= ~ECC_M4EN; 540 ECC_CTRL &= ~ECC_M4EN;
@@ -549,29 +549,29 @@ static bool nand_read_sector_of_phys_page(int bank, int page,
549 /* Calculate MLC4 ECC using bytes 0,1,8-15 */ 549 /* Calculate MLC4 ECC using bytes 0,1,8-15 */
550 BCLKCTR |= DEV_ECC; 550 BCLKCTR |= DEV_ECC;
551 ECC_CTRL |= ECC_M4EN; 551 ECC_CTRL |= ECC_M4EN;
552 552
553 MLC_ECC0W = *(unsigned short*)spare_buf; 553 MLC_ECC0W = *(unsigned short*)spare_buf;
554 MLC_ECC1W = spare_buf[2]; 554 MLC_ECC1W = spare_buf[2];
555 MLC_ECC2W = spare_buf[3]; 555 MLC_ECC2W = spare_buf[3];
556 556
557 while (!(ECC_CTRL & ECC_READY)) {}; 557 while (!(ECC_CTRL & ECC_READY)) {};
558 558
559 int errors = ECC_ERR_NUM & 7; 559 int errors = ECC_ERR_NUM & 7;
560 560
561 switch (errors) 561 switch (errors)
562 { 562 {
563 case 4: /* nothing to correct */ 563 case 4: /* nothing to correct */
564 break; 564 break;
565 565
566 case 7: /* fail, can't correct */ 566 case 7: /* fail, can't correct */
567 ret = false; 567 ret = false;
568 break; 568 break;
569 569
570 default: /* between 1 and 4 errors */ 570 default: /* between 1 and 4 errors */
571 { 571 {
572 int i; 572 int i;
573 unsigned char* char_buf = (unsigned char*)buf; 573 unsigned char* char_buf = (unsigned char*)buf;
574 574
575 for (i = 0; i < errors + 1; i++) 575 for (i = 0; i < errors + 1; i++)
576 { 576 {
577 int offset = 0x207 - ECC_ERRADDR(i); 577 int offset = 0x207 - ECC_ERRADDR(i);
@@ -584,7 +584,7 @@ static bool nand_read_sector_of_phys_page(int bank, int page,
584 ECC_CTRL &= ~ECC_M4EN; 584 ECC_CTRL &= ~ECC_M4EN;
585 BCLKCTR &= ~DEV_ECC; 585 BCLKCTR &= ~DEV_ECC;
586#endif 586#endif
587 587
588 nand_end_read(); 588 nand_end_read();
589 589
590 return ret; 590 return ret;
@@ -619,7 +619,7 @@ static bool nand_read_sector_of_logical_segment(int log_segment, int sector,
619 619
620 int cache_num = 0; 620 int cache_num = 0;
621 bool found = false; 621 bool found = false;
622 622
623 while (!found && cache_num < write_caches_in_use) 623 while (!found && cache_num < write_caches_in_use)
624 { 624 {
625 if (write_caches[cache_num].log_segment == log_segment) 625 if (write_caches[cache_num].log_segment == log_segment)
@@ -628,10 +628,10 @@ static bool nand_read_sector_of_logical_segment(int log_segment, int sector,
628 { 628 {
629 /* data is located in random pages cache */ 629 /* data is located in random pages cache */
630 found = true; 630 found = true;
631 631
632 bank = write_caches[cache_num].random_bank; 632 bank = write_caches[cache_num].random_bank;
633 phys_segment = write_caches[cache_num].random_phys_segment; 633 phys_segment = write_caches[cache_num].random_phys_segment;
634 634
635 page_in_segment = 635 page_in_segment =
636 write_caches[cache_num].page_map[page_in_segment]; 636 write_caches[cache_num].page_map[page_in_segment];
637 } 637 }
@@ -640,7 +640,7 @@ static bool nand_read_sector_of_logical_segment(int log_segment, int sector,
640 { 640 {
641 /* data is located in in-place pages cache */ 641 /* data is located in in-place pages cache */
642 found = true; 642 found = true;
643 643
644 bank = write_caches[cache_num].inplace_bank; 644 bank = write_caches[cache_num].inplace_bank;
645 phys_segment = write_caches[cache_num].inplace_phys_segment; 645 phys_segment = write_caches[cache_num].inplace_phys_segment;
646 } 646 }
@@ -664,7 +664,7 @@ static inline unsigned char get_sector_type(char* spare_buf)
664static inline unsigned short get_log_segment_id(int phys_seg, char* spare_buf) 664static inline unsigned short get_log_segment_id(int phys_seg, char* spare_buf)
665{ 665{
666 (void)phys_seg; 666 (void)phys_seg;
667 667
668 return ((spare_buf[OFF_LOG_SEG_HIBYTE] << 8) | 668 return ((spare_buf[OFF_LOG_SEG_HIBYTE] << 8) |
669 spare_buf[OFF_LOG_SEG_LOBYTE]) 669 spare_buf[OFF_LOG_SEG_LOBYTE])
670#if defined(FTL_V1) 670#if defined(FTL_V1)
@@ -702,7 +702,7 @@ static void read_random_writes_cache(int bank, int phys_segment)
702 16, spare_buf); 702 16, spare_buf);
703 703
704 log_segment = get_log_segment_id(phys_segment, spare_buf); 704 log_segment = get_log_segment_id(phys_segment, spare_buf);
705 705
706 if (log_segment == -1) 706 if (log_segment == -1)
707 return; 707 return;
708 708
@@ -734,13 +734,13 @@ static void read_random_writes_cache(int bank, int phys_segment)
734 page++) 734 page++)
735 { 735 {
736 unsigned short cached_page; 736 unsigned short cached_page;
737 737
738 nand_read_raw(bank, phys_segment_to_page_addr(phys_segment, page), 738 nand_read_raw(bank, phys_segment_to_page_addr(phys_segment, page),
739 SECTOR_SIZE, /* offset to first sector's spare */ 739 SECTOR_SIZE, /* offset to first sector's spare */
740 16, spare_buf); 740 16, spare_buf);
741 741
742 cached_page = get_cached_page_id(spare_buf); 742 cached_page = get_cached_page_id(spare_buf);
743 743
744 if (cached_page != 0xFFFF) 744 if (cached_page != 0xFFFF)
745 write_caches[cache_no].page_map[cached_page] = page; 745 write_caches[cache_no].page_map[cached_page] = page;
746 } 746 }
@@ -759,10 +759,10 @@ static void read_inplace_writes_cache(int bank, int phys_segment)
759 16, spare_buf); 759 16, spare_buf);
760 760
761 log_segment = get_log_segment_id(phys_segment, spare_buf); 761 log_segment = get_log_segment_id(phys_segment, spare_buf);
762 762
763 if (log_segment == -1) 763 if (log_segment == -1)
764 return; 764 return;
765 765
766 /* Find which cache this is related to */ 766 /* Find which cache this is related to */
767 int cache_no = find_write_cache(log_segment); 767 int cache_no = find_write_cache(log_segment);
768 768
@@ -780,7 +780,7 @@ static void read_inplace_writes_cache(int bank, int phys_segment)
780 } 780 }
781 781
782 write_caches[cache_no].log_segment = log_segment; 782 write_caches[cache_no].log_segment = log_segment;
783 783
784 /* Find how many pages have been written to the new segment */ 784 /* Find how many pages have been written to the new segment */
785 while (log_segment != -1 && 785 while (log_segment != -1 &&
786 page < (nand_data->pages_per_block * nand_data->planes) - 1) 786 page < (nand_data->pages_per_block * nand_data->planes) - 1)
@@ -791,7 +791,7 @@ static void read_inplace_writes_cache(int bank, int phys_segment)
791 791
792 log_segment = get_log_segment_id(phys_segment, spare_buf); 792 log_segment = get_log_segment_id(phys_segment, spare_buf);
793 } 793 }
794 794
795 if (page != 0) 795 if (page != 0)
796 { 796 {
797 write_caches[cache_no].inplace_bank = bank; 797 write_caches[cache_no].inplace_bank = bank;
@@ -801,7 +801,7 @@ static void read_inplace_writes_cache(int bank, int phys_segment)
801} 801}
802 802
803 803
804int nand_read_sectors(IF_MD(int drive,) unsigned long start, int incount, 804int nand_read_sectors(IF_MD(int drive,) sector_t start, int incount,
805 void* inbuf) 805 void* inbuf)
806{ 806{
807#ifdef HAVE_MULTIDRIVE 807#ifdef HAVE_MULTIDRIVE
@@ -809,15 +809,15 @@ int nand_read_sectors(IF_MD(int drive,) unsigned long start, int incount,
809#endif 809#endif
810 810
811 int ret = 0; 811 int ret = 0;
812 812
813 mutex_lock(&ata_mtx); 813 mutex_lock(&ata_mtx);
814 814
815 led(true); 815 led(true);
816 816
817 while (incount > 0) 817 while (incount > 0)
818 { 818 {
819 int done = 0; 819 int done = 0;
820 int segment = start / sectors_per_segment; 820 sector_t segment = start / sectors_per_segment;
821 int secmod = start % sectors_per_segment; 821 int secmod = start % sectors_per_segment;
822 822
823 while (incount > 0 && secmod < sectors_per_segment) 823 while (incount > 0 && secmod < sectors_per_segment)
@@ -839,7 +839,7 @@ int nand_read_sectors(IF_MD(int drive,) unsigned long start, int incount,
839 secmod++; 839 secmod++;
840 done++; 840 done++;
841 } 841 }
842 842
843 if (done < 0) 843 if (done < 0)
844 { 844 {
845 ret = -1; 845 ret = -1;
@@ -852,11 +852,11 @@ nand_read_error:
852 852
853 mutex_unlock(&ata_mtx); 853 mutex_unlock(&ata_mtx);
854 led(false); 854 led(false);
855 855
856 return ret; 856 return ret;
857} 857}
858 858
859int nand_write_sectors(IF_MD(int drive,) unsigned long start, int count, 859int nand_write_sectors(IF_MD(int drive,) sector_t start, int count,
860 const void* outbuf) 860 const void* outbuf)
861{ 861{
862#ifdef HAVE_MULTIDRIVE 862#ifdef HAVE_MULTIDRIVE
@@ -903,7 +903,7 @@ int nand_init(void)
903 unsigned char spare_buf[16]; 903 unsigned char spare_buf[16];
904 904
905 if (initialized) return 0; 905 if (initialized) return 0;
906 906
907 mutex_init(&ata_mtx); 907 mutex_init(&ata_mtx);
908 908
909 /* Set GPIO direction for chip select & write protect */ 909 /* Set GPIO direction for chip select & write protect */
@@ -924,7 +924,7 @@ int nand_init(void)
924 924
925 memset(lpt_lookup, 0xff, lptbuf_size); 925 memset(lpt_lookup, 0xff, lptbuf_size);
926 memset(write_caches, 0xff, sizeof(write_caches)); 926 memset(write_caches, 0xff, sizeof(write_caches));
927 927
928 write_caches_in_use = 0; 928 write_caches_in_use = 0;
929 929
930 /* Scan banks to build up block translation table */ 930 /* Scan banks to build up block translation table */
@@ -936,7 +936,7 @@ int nand_init(void)
936 nand_read_raw(bank, phys_segment_to_page_addr(phys_segment, 0), 936 nand_read_raw(bank, phys_segment_to_page_addr(phys_segment, 0),
937 SECTOR_SIZE, /* offset */ 937 SECTOR_SIZE, /* offset */
938 16, spare_buf); 938 16, spare_buf);
939 939
940 int type = get_sector_type(spare_buf); 940 int type = get_sector_type(spare_buf);
941 941
942#ifdef FTL_V2 942#ifdef FTL_V2
@@ -948,7 +948,7 @@ int nand_init(void)
948 nand_read_raw(bank, phys_segment_to_page_addr 948 nand_read_raw(bank, phys_segment_to_page_addr
949 (phys_segment, pages_per_segment - 1), 949 (phys_segment, pages_per_segment - 1),
950 SECTOR_SIZE, 16, spare_buf); 950 SECTOR_SIZE, 16, spare_buf);
951 951
952 if (get_sector_type(spare_buf) != 0xff) 952 if (get_sector_type(spare_buf) != 0xff)
953 { 953 {
954 type = SECTYPE_MAIN_DATA; 954 type = SECTYPE_MAIN_DATA;
@@ -982,14 +982,14 @@ int nand_init(void)
982 } 982 }
983 break; 983 break;
984 } 984 }
985 985
986 case SECTYPE_MAIN_RANDOM_CACHE: 986 case SECTYPE_MAIN_RANDOM_CACHE:
987 { 987 {
988 /* Newly-written random page data (Main data area) */ 988 /* Newly-written random page data (Main data area) */
989 read_random_writes_cache(bank, phys_segment); 989 read_random_writes_cache(bank, phys_segment);
990 break; 990 break;
991 } 991 }
992 992
993 case SECTYPE_MAIN_INPLACE_CACHE: 993 case SECTYPE_MAIN_INPLACE_CACHE:
994 { 994 {
995 /* Newly-written sequential page data (Main data area) */ 995 /* Newly-written sequential page data (Main data area) */
@@ -999,7 +999,7 @@ int nand_init(void)
999 } 999 }
1000 } 1000 }
1001 } 1001 }
1002 1002
1003 initialized = true; 1003 initialized = true;
1004 1004
1005 return 0; 1005 return 0;
@@ -1029,7 +1029,7 @@ int nand_num_drives(int first_drive)
1029{ 1029{
1030 /* We don't care which logical drive number we have been assigned */ 1030 /* We don't care which logical drive number we have been assigned */
1031 (void)first_drive; 1031 (void)first_drive;
1032 1032
1033 return 1; 1033 return 1;
1034} 1034}
1035 1035
diff --git a/firmware/target/arm/imx233/nand-imx233.c b/firmware/target/arm/imx233/nand-imx233.c
index a7afba7d43..22da408e05 100644
--- a/firmware/target/arm/imx233/nand-imx233.c
+++ b/firmware/target/arm/imx233/nand-imx233.c
@@ -36,13 +36,13 @@ int nand_init(void)
36{ 36{
37 return -1; 37 return -1;
38} 38}
39int nand_read_sectors(IF_MD(int drive,) unsigned long start, int count, 39int nand_read_sectors(IF_MD(int drive,) sector_t start, int count,
40 void* buf) 40 void* buf)
41{ 41{
42 return -1; 42 return -1;
43} 43}
44 44
45int nand_write_sectors(IF_MD(int drive,) unsigned long start, int count, 45int nand_write_sectors(IF_MD(int drive,) sector_t start, int count,
46 const void* buf) 46 const void* buf)
47{ 47{
48 return -1; 48 return -1;
diff --git a/firmware/target/arm/imx233/partitions-imx233.c b/firmware/target/arm/imx233/partitions-imx233.c
index 83a0bf8b42..965f98dca5 100644
--- a/firmware/target/arm/imx233/partitions-imx233.c
+++ b/firmware/target/arm/imx233/partitions-imx233.c
@@ -71,7 +71,7 @@ static const char *creative_part_name(enum imx233_part_t part)
71} 71}
72 72
73static int compute_window_creative(intptr_t user, part_read_fn_t read_fn, 73static int compute_window_creative(intptr_t user, part_read_fn_t read_fn,
74 enum imx233_part_t part, unsigned *start, unsigned *end) 74 enum imx233_part_t part, sector_t *start, unsigned *end)
75{ 75{
76 uint8_t mblk[512]; 76 uint8_t mblk[512];
77 int ret = read_fn(user, MBLK_ADDR / 512, 1, mblk); 77 int ret = read_fn(user, MBLK_ADDR / 512, 1, mblk);
diff --git a/firmware/target/arm/imx233/partitions-imx233.h b/firmware/target/arm/imx233/partitions-imx233.h
index e5378dadbb..4c8f703c2d 100644
--- a/firmware/target/arm/imx233/partitions-imx233.h
+++ b/firmware/target/arm/imx233/partitions-imx233.h
@@ -45,7 +45,7 @@ enum imx233_part_t
45/** The computation function can be called very early in the boot, at which point 45/** The computation function can be called very early in the boot, at which point
46 * usual storage read/write function may not be available. To workaround this 46 * usual storage read/write function may not be available. To workaround this
47 * issue, one must provide a read function. */ 47 * issue, one must provide a read function. */
48typedef int (*part_read_fn_t)(intptr_t user, unsigned long start, int count, void* buf); 48typedef int (*part_read_fn_t)(intptr_t user, sector_t start, int count, void* buf);
49/* Enable/Disable window computations for internal storage following the 49/* Enable/Disable window computations for internal storage following the
50 * Freescale/Creative convention */ 50 * Freescale/Creative convention */
51void imx233_partitions_enable_window(bool enable); 51void imx233_partitions_enable_window(bool enable);
@@ -55,6 +55,6 @@ bool imx233_partitions_is_window_enabled(void);
55 * for a whole disk, *end should be the size of the disk when the function is 55 * for a whole disk, *end should be the size of the disk when the function is
56 * called */ 56 * called */
57int imx233_partitions_compute_window(intptr_t user, part_read_fn_t read_fn, 57int imx233_partitions_compute_window(intptr_t user, part_read_fn_t read_fn,
58 enum imx233_part_t part, unsigned *start, unsigned *end); 58 enum imx233_part_t part, sector_t *start, unsigned *end);
59 59
60#endif /* __PARTITIONS_IMX233__ */ \ No newline at end of file 60#endif /* __PARTITIONS_IMX233__ */
diff --git a/firmware/target/arm/imx233/sdmmc-imx233.c b/firmware/target/arm/imx233/sdmmc-imx233.c
index af090e8a07..5577c6002d 100644
--- a/firmware/target/arm/imx233/sdmmc-imx233.c
+++ b/firmware/target/arm/imx233/sdmmc-imx233.c
@@ -648,7 +648,7 @@ int mmc_event(long id, intptr_t data)
648#endif /* CONFIG_STORAGE & STORAGE_MMC */ 648#endif /* CONFIG_STORAGE & STORAGE_MMC */
649 649
650/* low-level function, don't call directly! */ 650/* low-level function, don't call directly! */
651static int __xfer_sectors(int drive, unsigned long start, int count, void *buf, bool read) 651static int __xfer_sectors(int drive, sector_t start, int count, void *buf, bool read)
652{ 652{
653 uint32_t resp; 653 uint32_t resp;
654 int ret = 0; 654 int ret = 0;
@@ -660,7 +660,7 @@ static int __xfer_sectors(int drive, unsigned long start, int count, void *buf,
660 need_stop = false; 660 need_stop = false;
661 /* Set bank_start to the correct unit (blocks or bytes). 661 /* Set bank_start to the correct unit (blocks or bytes).
662 * MMC drives use block addressing, SD cards bytes or blocks */ 662 * MMC drives use block addressing, SD cards bytes or blocks */
663 int bank_start = start; 663 sector_t bank_start = start;
664 if(SDMMC_MODE(drive) == SD_MODE && !(SDMMC_INFO(drive).ocr & (1<<30))) /* not SDHC */ 664 if(SDMMC_MODE(drive) == SD_MODE && !(SDMMC_INFO(drive).ocr & (1<<30))) /* not SDHC */
665 bank_start *= SD_BLOCK_SIZE; 665 bank_start *= SD_BLOCK_SIZE;
666 /* issue read/write 666 /* issue read/write
@@ -686,7 +686,7 @@ static int __xfer_sectors(int drive, unsigned long start, int count, void *buf,
686 return ret; 686 return ret;
687} 687}
688 688
689static int transfer_sectors(int drive, unsigned long start, int count, void *buf, bool read) 689static int transfer_sectors(int drive, sector_t start, int count, void *buf, bool read)
690{ 690{
691 int ret = 0; 691 int ret = 0;
692 /* the function doesn't work when count is 0 */ 692 /* the function doesn't work when count is 0 */
@@ -806,7 +806,7 @@ static int transfer_sectors(int drive, unsigned long start, int count, void *buf
806} 806}
807 807
808/* user specifies the sdmmc drive */ 808/* user specifies the sdmmc drive */
809static int part_read_fn(intptr_t user, unsigned long start, int count, void* buf) 809static int part_read_fn(intptr_t user, sector_t start, int count, void* buf)
810{ 810{
811 return transfer_sectors(user, start, count, buf, true); 811 return transfer_sectors(user, start, count, buf, true);
812} 812}
@@ -917,7 +917,7 @@ void sd_enable(bool on)
917 (void) on; 917 (void) on;
918} 918}
919 919
920int sd_read_sectors(IF_MD(int sd_drive,) unsigned long start, int count, void *buf) 920int sd_read_sectors(IF_MD(int sd_drive,) sector_t start, int count, void *buf)
921{ 921{
922#ifndef HAVE_MULTIDRIVE 922#ifndef HAVE_MULTIDRIVE
923 int sd_drive = 0; 923 int sd_drive = 0;
@@ -925,7 +925,7 @@ int sd_read_sectors(IF_MD(int sd_drive,) unsigned long start, int count, void *b
925 return transfer_sectors(sd_map[sd_drive], start, count, buf, true); 925 return transfer_sectors(sd_map[sd_drive], start, count, buf, true);
926} 926}
927 927
928int sd_write_sectors(IF_MD(int sd_drive,) unsigned long start, int count, const void* buf) 928int sd_write_sectors(IF_MD(int sd_drive,) sector_t start, int count, const void* buf)
929{ 929{
930#ifndef HAVE_MULTIDRIVE 930#ifndef HAVE_MULTIDRIVE
931 int sd_drive = 0; 931 int sd_drive = 0;
@@ -1039,7 +1039,7 @@ int mmc_spinup_time(void)
1039 return 0; 1039 return 0;
1040} 1040}
1041 1041
1042int mmc_read_sectors(IF_MD(int mmc_drive,) unsigned long start, int count, void *buf) 1042int mmc_read_sectors(IF_MD(int mmc_drive,) sector_t start, int count, void *buf)
1043{ 1043{
1044#ifndef HAVE_MULTIDRIVE 1044#ifndef HAVE_MULTIDRIVE
1045 int mmc_drive = 0; 1045 int mmc_drive = 0;
@@ -1047,7 +1047,7 @@ int mmc_read_sectors(IF_MD(int mmc_drive,) unsigned long start, int count, void
1047 return transfer_sectors(mmc_map[mmc_drive], start, count, buf, true); 1047 return transfer_sectors(mmc_map[mmc_drive], start, count, buf, true);
1048} 1048}
1049 1049
1050int mmc_write_sectors(IF_MD(int mmc_drive,) unsigned long start, int count, const void* buf) 1050int mmc_write_sectors(IF_MD(int mmc_drive,) sector_t start, int count, const void* buf)
1051{ 1051{
1052#ifndef HAVE_MULTIDRIVE 1052#ifndef HAVE_MULTIDRIVE
1053 int mmc_drive = 0; 1053 int mmc_drive = 0;
diff --git a/firmware/target/arm/pp/ata-sd-pp.c b/firmware/target/arm/pp/ata-sd-pp.c
index fb0a9e150e..b998afd21e 100644
--- a/firmware/target/arm/pp/ata-sd-pp.c
+++ b/firmware/target/arm/pp/ata-sd-pp.c
@@ -82,7 +82,7 @@
82#define STAT_TIME_OUT_RES (1 << 1) 82#define STAT_TIME_OUT_RES (1 << 1)
83#define STAT_TIME_OUT_READ (1) 83#define STAT_TIME_OUT_READ (1)
84#define STAT_ERROR_BITS (0x3f) 84#define STAT_ERROR_BITS (0x3f)
85 85
86/* MMC_CMDAT bits */ 86/* MMC_CMDAT bits */
87/* Some of the bits used by the OF don't make much sense with these */ 87/* Some of the bits used by the OF don't make much sense with these */
88/* definitions. So they're probably different between PXA and PP502x */ 88/* definitions. So they're probably different between PXA and PP502x */
@@ -101,7 +101,7 @@
101#define CMDAT_RES_TYPE3 (3) 101#define CMDAT_RES_TYPE3 (3)
102#define CMDAT_RES_TYPE2 (2) 102#define CMDAT_RES_TYPE2 (2)
103#define CMDAT_RES_TYPE1 (1) 103#define CMDAT_RES_TYPE1 (1)
104 104
105/* MMC_I_MASK bits */ 105/* MMC_I_MASK bits */
106/* PP502x apparently only has bits 0-3 */ 106/* PP502x apparently only has bits 0-3 */
107#define I_MASK_SDIO_SUSPEND_ACK (1 << 12) 107#define I_MASK_SDIO_SUSPEND_ACK (1 << 12)
@@ -499,18 +499,18 @@ static inline void copy_write_sectors(const unsigned char** buf)
499 { 499 {
500 asm volatile ( 500 asm volatile (
501 "ldmia %[buf]!, { r3, r5, r7, r9 } \r\n" 501 "ldmia %[buf]!, { r3, r5, r7, r9 } \r\n"
502 "mov r4, r3, lsr #16 \r\n" 502 "mov r4, r3, lsr #16 \r\n"
503 "mov r6, r5, lsr #16 \r\n" 503 "mov r6, r5, lsr #16 \r\n"
504 "mov r8, r7, lsr #16 \r\n" 504 "mov r8, r7, lsr #16 \r\n"
505 "mov r10, r9, lsr #16 \r\n" 505 "mov r10, r9, lsr #16 \r\n"
506 "stmia %[data], { r3-r10 } \r\n" 506 "stmia %[data], { r3-r10 } \r\n"
507 "ldmia %[buf]!, { r3, r5, r7, r9 } \r\n" 507 "ldmia %[buf]!, { r3, r5, r7, r9 } \r\n"
508 "mov r4, r3, lsr #16 \r\n" 508 "mov r4, r3, lsr #16 \r\n"
509 "mov r6, r5, lsr #16 \r\n" 509 "mov r6, r5, lsr #16 \r\n"
510 "mov r8, r7, lsr #16 \r\n" 510 "mov r8, r7, lsr #16 \r\n"
511 "mov %[t], r9, lsr #16 \r\n" 511 "mov %[t], r9, lsr #16 \r\n"
512 "stmia %[data], { r3-r9 } \r\n" 512 "stmia %[data], { r3-r9 } \r\n"
513 : [buf]"+&r"(*buf), [t]"=&r"(t) 513 : [buf]"+&r"(*buf), [t]"=&r"(t)
514 : [data]"r"(&MMC_DATA_FIFO) 514 : [data]"r"(&MMC_DATA_FIFO)
515 : "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10" 515 : "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
516 ); 516 );
@@ -760,7 +760,7 @@ static void sd_init_device(int card_no)
760 currcard->csd[i] = temp_reg[3-i]; 760 currcard->csd[i] = temp_reg[3-i];
761 761
762 sd_parse_csd(currcard); 762 sd_parse_csd(currcard);
763 763
764 MMC_CLKRT = 0; /* switch to highest clock rate */ 764 MMC_CLKRT = 0; /* switch to highest clock rate */
765 765
766 ret = sd_command(SD_SELECT_CARD, currcard->rca, NULL, 766 ret = sd_command(SD_SELECT_CARD, currcard->rca, NULL,
@@ -849,7 +849,7 @@ static void sd_select_device(int card_no)
849 849
850/* API Functions */ 850/* API Functions */
851 851
852int sd_read_sectors(IF_MD(int drive,) unsigned long start, int incount, 852int sd_read_sectors(IF_MD(int drive,) sector_t start, int incount,
853 void* inbuf) 853 void* inbuf)
854{ 854{
855#ifndef HAVE_MULTIDRIVE 855#ifndef HAVE_MULTIDRIVE
@@ -857,8 +857,8 @@ int sd_read_sectors(IF_MD(int drive,) unsigned long start, int incount,
857#endif 857#endif
858 int ret; 858 int ret;
859 unsigned char *buf, *buf_end; 859 unsigned char *buf, *buf_end;
860 unsigned int bank; 860 sector_t bank;
861 861
862 /* TODO: Add DMA support. */ 862 /* TODO: Add DMA support. */
863 863
864 mutex_lock(&sd_mtx); 864 mutex_lock(&sd_mtx);
@@ -894,7 +894,7 @@ sd_read_retry:
894 if (ret < 0) 894 if (ret < 0)
895 goto sd_read_error; 895 goto sd_read_error;
896 } 896 }
897 897
898 start -= bank * BLOCKS_PER_BANK; 898 start -= bank * BLOCKS_PER_BANK;
899 } 899 }
900 900
@@ -904,6 +904,8 @@ sd_read_retry:
904 904
905 MMC_NUMBLK = incount; 905 MMC_NUMBLK = incount;
906 906
907 // XXX 64-bit addresses..
908
907#ifdef HAVE_HOTSWAP 909#ifdef HAVE_HOTSWAP
908 if(currcard->ocr & (1<<30) ) 910 if(currcard->ocr & (1<<30) )
909 { 911 {
@@ -966,7 +968,7 @@ sd_read_error:
966 } 968 }
967} 969}
968 970
969int sd_write_sectors(IF_MD(int drive,) unsigned long start, int count, 971int sd_write_sectors(IF_MD(int drive,) sector_t start, int count,
970 const void* outbuf) 972 const void* outbuf)
971{ 973{
972/* Write support is not finished yet */ 974/* Write support is not finished yet */
@@ -1010,7 +1012,7 @@ sd_write_retry:
1010 if (ret < 0) 1012 if (ret < 0)
1011 goto sd_write_error; 1013 goto sd_write_error;
1012 } 1014 }
1013 1015
1014 start -= bank * BLOCKS_PER_BANK; 1016 start -= bank * BLOCKS_PER_BANK;
1015 } 1017 }
1016 1018
@@ -1250,7 +1252,7 @@ int sd_num_drives(int first_drive)
1250#else 1252#else
1251 (void)first_drive; 1253 (void)first_drive;
1252#endif 1254#endif
1253 1255
1254#ifdef HAVE_MULTIDRIVE 1256#ifdef HAVE_MULTIDRIVE
1255 return 2; 1257 return 2;
1256#else 1258#else
diff --git a/firmware/target/arm/rk27xx/ata-nand-rk27xx.c b/firmware/target/arm/rk27xx/ata-nand-rk27xx.c
index e257416cd0..54a1223cfc 100644
--- a/firmware/target/arm/rk27xx/ata-nand-rk27xx.c
+++ b/firmware/target/arm/rk27xx/ata-nand-rk27xx.c
@@ -31,18 +31,18 @@
31 31
32/* This file provides only STUBS for now */ 32/* This file provides only STUBS for now */
33 33
34/** static, private data **/ 34/** static, private data **/
35static bool initialized = false; 35static bool initialized = false;
36 36
37/* API Functions */ 37/* API Functions */
38int nand_read_sectors(IF_MD(int drive,) unsigned long start, int incount, 38int nand_read_sectors(IF_MD(int drive,) sector_t start, int incount,
39 void* inbuf) 39 void* inbuf)
40{ 40{
41 (void)drive; 41 (void)drive;
42 return ftl_read(start, incount, inbuf); 42 return ftl_read(start, incount, inbuf);
43} 43}
44 44
45int nand_write_sectors(IF_MD(int drive,) unsigned long start, int count, 45int nand_write_sectors(IF_MD(int drive,) sector_t start, int count,
46 const void* outbuf) 46 const void* outbuf)
47{ 47{
48 (void)drive; 48 (void)drive;
@@ -112,7 +112,7 @@ int nand_num_drives(int first_drive)
112{ 112{
113 /* We don't care which logical drive number(s) we have been assigned */ 113 /* We don't care which logical drive number(s) we have been assigned */
114 (void)first_drive; 114 (void)first_drive;
115 115
116 return 1; 116 return 1;
117} 117}
118#endif 118#endif
diff --git a/firmware/target/arm/rk27xx/sd-rk27xx.c b/firmware/target/arm/rk27xx/sd-rk27xx.c
index 68ecd444c6..e082a8bf2e 100644
--- a/firmware/target/arm/rk27xx/sd-rk27xx.c
+++ b/firmware/target/arm/rk27xx/sd-rk27xx.c
@@ -453,7 +453,7 @@ static inline void write_sd_data(unsigned char **src)
453 *src += 512; 453 *src += 512;
454} 454}
455 455
456int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count, 456int sd_read_sectors(IF_MD(int drive,) sector_t start, int count,
457 void* buf) 457 void* buf)
458{ 458{
459#ifdef HAVE_MULTIDRIVE 459#ifdef HAVE_MULTIDRIVE
@@ -498,6 +498,8 @@ int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count,
498 DATA_XFER_MULTI; 498 DATA_XFER_MULTI;
499 } 499 }
500 500
501 // XXX 64-bit
502
501 /* issue read command to the card */ 503 /* issue read command to the card */
502 if (!send_cmd(SD_READ_MULTIPLE_BLOCK, start, RES_R1, &response)) 504 if (!send_cmd(SD_READ_MULTIPLE_BLOCK, start, RES_R1, &response))
503 { 505 {
@@ -576,7 +578,7 @@ int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count,
576} 578}
577 579
578/* Not tested */ 580/* Not tested */
579int sd_write_sectors(IF_MD(int drive,) unsigned long start, int count, 581int sd_write_sectors(IF_MD(int drive,) sector_t start, int count,
580 const void* buf) 582 const void* buf)
581{ 583{
582#ifdef HAVE_MULTIDRIVE 584#ifdef HAVE_MULTIDRIVE
@@ -620,6 +622,7 @@ int sd_write_sectors(IF_MD(int drive,) unsigned long start, int count,
620 622
621 write_sd_data(&src); /* put data into transfer buffer */ 623 write_sd_data(&src); /* put data into transfer buffer */
622 624
625 // XXX 64-bit
623 if (!send_cmd(SD_WRITE_MULTIPLE_BLOCK, start, RES_R1, &response)) 626 if (!send_cmd(SD_WRITE_MULTIPLE_BLOCK, start, RES_R1, &response))
624 { 627 {
625 ret = -3; 628 ret = -3;
diff --git a/firmware/target/arm/s3c2440/sd-s3c2440.c b/firmware/target/arm/s3c2440/sd-s3c2440.c
index 2ff68aa4ee..82fd60c711 100644
--- a/firmware/target/arm/s3c2440/sd-s3c2440.c
+++ b/firmware/target/arm/s3c2440/sd-s3c2440.c
@@ -18,7 +18,7 @@
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22//#define SD_DEBUG 22//#define SD_DEBUG
23 23
24#include "system.h" 24#include "system.h"
@@ -33,7 +33,7 @@
33#include "sdmmc.h" 33#include "sdmmc.h"
34#endif 34#endif
35#include "storage.h" 35#include "storage.h"
36#include "dma-target.h" 36#include "dma-target.h"
37#include "system-target.h" 37#include "system-target.h"
38#include "led-mini2440.h" 38#include "led-mini2440.h"
39 39
@@ -83,7 +83,7 @@ struct sd_card_status
83 int retry_max; 83 int retry_max;
84}; 84};
85 85
86/** static, private data **/ 86/** static, private data **/
87 87
88/* for compatibility */ 88/* for compatibility */
89static long last_disk_activity = -1; 89static long last_disk_activity = -1;
@@ -117,13 +117,13 @@ static struct mutex sd_mtx SHAREDBSS_ATTR;
117static struct semaphore transfer_completion_signal; 117static struct semaphore transfer_completion_signal;
118static volatile unsigned int transfer_error[NUM_DRIVES]; 118static volatile unsigned int transfer_error[NUM_DRIVES];
119/* align on cache line size */ 119/* align on cache line size */
120static unsigned char aligned_buffer[UNALIGNED_NUM_SECTORS * SD_BLOCK_SIZE] 120static unsigned char aligned_buffer[UNALIGNED_NUM_SECTORS * SD_BLOCK_SIZE]
121 __attribute__((aligned(32))); 121 __attribute__((aligned(32)));
122static unsigned char * uncached_buffer; 122static unsigned char * uncached_buffer;
123 123
124static inline void mci_delay(void) 124static inline void mci_delay(void)
125{ 125{
126 int i = 0xffff; 126 int i = 0xffff;
127 while (i--) 127 while (i--)
128 asm volatile ("nop\n"); 128 asm volatile ("nop\n");
129} 129}
@@ -146,7 +146,7 @@ static void get_regs (unsigned *regs)
146{ 146{
147 unsigned j; 147 unsigned j;
148 volatile unsigned long *sdi_reg = &SDICON; 148 volatile unsigned long *sdi_reg = &SDICON;
149 149
150 for (j=0; j < 16;j++) 150 for (j=0; j < 16;j++)
151 { 151 {
152 *regs++ = *sdi_reg++; 152 *regs++ = *sdi_reg++;
@@ -158,7 +158,7 @@ static void dump_regs (unsigned *regs1, unsigned *regs2)
158 unsigned j; 158 unsigned j;
159 volatile unsigned long*sdi_reg = &SDICON; 159 volatile unsigned long*sdi_reg = &SDICON;
160 unsigned long diff; 160 unsigned long diff;
161 161
162 for (j=0; j < 16;j++) 162 for (j=0; j < 16;j++)
163 { 163 {
164 diff = *regs1 ^ *regs2; 164 diff = *regs1 ^ *regs2;
@@ -174,23 +174,23 @@ static void dump_regs (unsigned *regs1, unsigned *regs2)
174static void debug_r1(int cmd) 174static void debug_r1(int cmd)
175{ 175{
176#if defined(SD_DEBUG) 176#if defined(SD_DEBUG)
177 dbgprintf("CMD%2.2d:SDICSTA=%04x [%c%c%c%c%c-%c%c%c%c%c%c%c] SDIRSP0=%08x [%d %s] \n", 177 dbgprintf("CMD%2.2d:SDICSTA=%04x [%c%c%c%c%c-%c%c%c%c%c%c%c] SDIRSP0=%08x [%d %s] \n",
178 cmd, 178 cmd,
179 SDICSTA, 179 SDICSTA,
180 (SDICSTA & S3C2410_SDICMDSTAT_CRCFAIL) ? 'C' : ' ', 180 (SDICSTA & S3C2410_SDICMDSTAT_CRCFAIL) ? 'C' : ' ',
181 (SDICSTA & S3C2410_SDICMDSTAT_CMDSENT) ? 'S' : ' ', 181 (SDICSTA & S3C2410_SDICMDSTAT_CMDSENT) ? 'S' : ' ',
182 (SDICSTA & S3C2410_SDICMDSTAT_CMDTIMEOUT) ? 'T' : ' ', 182 (SDICSTA & S3C2410_SDICMDSTAT_CMDTIMEOUT) ? 'T' : ' ',
183 (SDICSTA & S3C2410_SDICMDSTAT_RSPFIN) ? 'R' : ' ', 183 (SDICSTA & S3C2410_SDICMDSTAT_RSPFIN) ? 'R' : ' ',
184 (SDICSTA & S3C2410_SDICMDSTAT_XFERING) ? 'X' : ' ', 184 (SDICSTA & S3C2410_SDICMDSTAT_XFERING) ? 'X' : ' ',
185 185
186 (SDICSTA & 0x40) ? 'P' : ' ', 186 (SDICSTA & 0x40) ? 'P' : ' ',
187 (SDICSTA & 0x20) ? 'A' : ' ', 187 (SDICSTA & 0x20) ? 'A' : ' ',
188 (SDICSTA & 0x10) ? 'E' : ' ', 188 (SDICSTA & 0x10) ? 'E' : ' ',
189 (SDICSTA & 0x08) ? 'C' : ' ', 189 (SDICSTA & 0x08) ? 'C' : ' ',
190 (SDICSTA & 0x04) ? 'I' : ' ', 190 (SDICSTA & 0x04) ? 'I' : ' ',
191 (SDICSTA & 0x02) ? 'R' : ' ', 191 (SDICSTA & 0x02) ? 'R' : ' ',
192 (SDICSTA & 0x01) ? 'Z' : ' ', 192 (SDICSTA & 0x01) ? 'Z' : ' ',
193 193
194 SDIRSP0, 194 SDIRSP0,
195 SD_R1_CURRENT_STATE(SDIRSP0), 195 SD_R1_CURRENT_STATE(SDIRSP0),
196 (SDIRSP0 & SD_R1_READY_FOR_DATA) ? "RDY " : " " 196 (SDIRSP0 & SD_R1_READY_FOR_DATA) ? "RDY " : " "
@@ -205,8 +205,8 @@ void SDI (void)
205 int status = SDIDSTA; 205 int status = SDIDSTA;
206#ifndef HAVE_MULTIDRIVE 206#ifndef HAVE_MULTIDRIVE
207 const int curr_card = 0; 207 const int curr_card = 0;
208#endif 208#endif
209 209
210 transfer_error[curr_card] = status 210 transfer_error[curr_card] = status
211#if 0 211#if 0
212 & ( S3C2410_SDIDSTA_CRCFAIL | S3C2410_SDIDSTA_RXCRCFAIL | 212 & ( S3C2410_SDIDSTA_CRCFAIL | S3C2410_SDIDSTA_RXCRCFAIL |
@@ -217,7 +217,7 @@ void SDI (void)
217 SDIDSTA |= S3C2410_SDIDSTA_CLEAR_BITS; /* needed to clear int */ 217 SDIDSTA |= S3C2410_SDIDSTA_CLEAR_BITS; /* needed to clear int */
218 218
219 dbgprintf ("SDI %x\n", transfer_error[curr_card]); 219 dbgprintf ("SDI %x\n", transfer_error[curr_card]);
220 220
221 semaphore_release(&transfer_completion_signal); 221 semaphore_release(&transfer_completion_signal);
222 222
223 /* Ack the interrupt */ 223 /* Ack the interrupt */
@@ -229,13 +229,13 @@ void SDI (void)
229void dma_callback (void) 229void dma_callback (void)
230{ 230{
231 const int status = SDIDSTA; 231 const int status = SDIDSTA;
232 232
233 transfer_error[0] = status & (S3C2410_SDIDSTA_CRCFAIL | 233 transfer_error[0] = status & (S3C2410_SDIDSTA_CRCFAIL |
234 S3C2410_SDIDSTA_RXCRCFAIL | 234 S3C2410_SDIDSTA_RXCRCFAIL |
235 S3C2410_SDIDSTA_DATATIMEOUT ); 235 S3C2410_SDIDSTA_DATATIMEOUT );
236 236
237 SDIDSTA |= S3C2410_SDIDSTA_CLEAR_BITS; /* needed to clear int */ 237 SDIDSTA |= S3C2410_SDIDSTA_CLEAR_BITS; /* needed to clear int */
238 238
239 dbgprintf ("dma_cb\n"); 239 dbgprintf ("dma_cb\n");
240 semaphore_release(&transfer_completion_signal); 240 semaphore_release(&transfer_completion_signal);
241} 241}
@@ -248,14 +248,14 @@ static void init_sdi_controller(const int card_no)
248/*****************************************************************************/ 248/*****************************************************************************/
249#ifdef MINI2440 249#ifdef MINI2440
250 /* Specific to Mini2440 */ 250 /* Specific to Mini2440 */
251 251
252 /* Enable pullups on SDCMD and SDDAT pins */ 252 /* Enable pullups on SDCMD and SDDAT pins */
253 S3C2440_GPIO_PULLUP (GPEUP, 6, GPIO_PULLUP_ENABLE); 253 S3C2440_GPIO_PULLUP (GPEUP, 6, GPIO_PULLUP_ENABLE);
254 S3C2440_GPIO_PULLUP (GPEUP, 7, GPIO_PULLUP_ENABLE); 254 S3C2440_GPIO_PULLUP (GPEUP, 7, GPIO_PULLUP_ENABLE);
255 S3C2440_GPIO_PULLUP (GPEUP, 8, GPIO_PULLUP_ENABLE); 255 S3C2440_GPIO_PULLUP (GPEUP, 8, GPIO_PULLUP_ENABLE);
256 S3C2440_GPIO_PULLUP (GPEUP, 9, GPIO_PULLUP_ENABLE); 256 S3C2440_GPIO_PULLUP (GPEUP, 9, GPIO_PULLUP_ENABLE);
257 S3C2440_GPIO_PULLUP (GPEUP, 10, GPIO_PULLUP_ENABLE); 257 S3C2440_GPIO_PULLUP (GPEUP, 10, GPIO_PULLUP_ENABLE);
258 258
259 /* Enable special function for SDCMD, SDCLK and SDDAT pins */ 259 /* Enable special function for SDCMD, SDCLK and SDDAT pins */
260 S3C2440_GPIO_CONFIG (GPECON, 5, GPIO_FUNCTION); 260 S3C2440_GPIO_CONFIG (GPECON, 5, GPIO_FUNCTION);
261 S3C2440_GPIO_CONFIG (GPECON, 6, GPIO_FUNCTION); 261 S3C2440_GPIO_CONFIG (GPECON, 6, GPIO_FUNCTION);
@@ -263,15 +263,15 @@ static void init_sdi_controller(const int card_no)
263 S3C2440_GPIO_CONFIG (GPECON, 8, GPIO_FUNCTION); 263 S3C2440_GPIO_CONFIG (GPECON, 8, GPIO_FUNCTION);
264 S3C2440_GPIO_CONFIG (GPECON, 9, GPIO_FUNCTION); 264 S3C2440_GPIO_CONFIG (GPECON, 9, GPIO_FUNCTION);
265 S3C2440_GPIO_CONFIG (GPECON, 10, GPIO_FUNCTION); 265 S3C2440_GPIO_CONFIG (GPECON, 10, GPIO_FUNCTION);
266 266
267 /* Card Detect input */ 267 /* Card Detect input */
268 S3C2440_GPIO_CONFIG (GPGCON, 8, GPIO_INPUT); 268 S3C2440_GPIO_CONFIG (GPGCON, 8, GPIO_INPUT);
269 /* enable external irq 8-23 on the internal interrupt controller */ 269 /* enable external irq 8-23 on the internal interrupt controller */
270 INTMSK &= ~1<<5; 270 INTMSK &= ~1<<5;
271 /* enable GPG8 IRQ on the external interrupt controller */ 271 /* enable GPG8 IRQ on the external interrupt controller */
272 EINTMASK &= ~(1<<16); 272 EINTMASK &= ~(1<<16);
273 273
274 274
275 /* Write Protect input */ 275 /* Write Protect input */
276 S3C2440_GPIO_CONFIG (GPHCON, 8, GPIO_INPUT); 276 S3C2440_GPIO_CONFIG (GPHCON, 8, GPIO_INPUT);
277/*****************************************************************************/ 277/*****************************************************************************/
@@ -279,11 +279,11 @@ static void init_sdi_controller(const int card_no)
279#error Unsupported target 279#error Unsupported target
280#endif 280#endif
281/*****************************************************************************/ 281/*****************************************************************************/
282 282
283 /* About 400KHz for initial comms with card */ 283 /* About 400KHz for initial comms with card */
284 SDIPRE = PCLK / INITIAL_CLK - 1; 284 SDIPRE = PCLK / INITIAL_CLK - 1;
285 /* Byte order=Type A (Little Endian), clock enable */ 285 /* Byte order=Type A (Little Endian), clock enable */
286 SDICON = S3C2410_SDICON_CLOCKTYPE; 286 SDICON = S3C2410_SDICON_CLOCKTYPE;
287 SDIFSTA |= S3C2440_SDIFSTA_FIFORESET; 287 SDIFSTA |= S3C2440_SDIFSTA_FIFORESET;
288 SDIBSIZE = SD_BLOCK_SIZE; 288 SDIBSIZE = SD_BLOCK_SIZE;
289 SDIDTIMER= 0x7fffff; /* Set timeout count - max value */ 289 SDIDTIMER= 0x7fffff; /* Set timeout count - max value */
@@ -297,11 +297,11 @@ static void init_sdi_controller(const int card_no)
297 /* Enable interrupt in controller */ 297 /* Enable interrupt in controller */
298 bitclr32(&INTMOD, SDI_MASK); 298 bitclr32(&INTMOD, SDI_MASK);
299 bitclr32(&INTMSK, SDI_MASK); 299 bitclr32(&INTMSK, SDI_MASK);
300 300
301 SDIIMSK |= S3C2410_SDIIMSK_DATAFINISH 301 SDIIMSK |= S3C2410_SDIIMSK_DATAFINISH
302 | S3C2410_SDIIMSK_DATATIMEOUT 302 | S3C2410_SDIIMSK_DATATIMEOUT
303 | S3C2410_SDIIMSK_DATACRC 303 | S3C2410_SDIIMSK_DATACRC
304 | S3C2410_SDIIMSK_CRCSTATUS 304 | S3C2410_SDIIMSK_CRCSTATUS
305 | S3C2410_SDIIMSK_FIFOFAIL 305 | S3C2410_SDIIMSK_FIFOFAIL
306 ; 306 ;
307#endif 307#endif
@@ -325,18 +325,18 @@ static bool send_cmd(const int card_no, const int cmd, const int arg,
325 get_regs (reg_copy2); 325 get_regs (reg_copy2);
326 dump_regs (reg_copy, reg_copy2); 326 dump_regs (reg_copy, reg_copy2);
327#endif 327#endif
328 328
329#if 0 329#if 0
330 while (SDICSTA & S3C2410_SDICMDSTAT_XFERING) 330 while (SDICSTA & S3C2410_SDICMDSTAT_XFERING)
331 ; /* wait ?? */ 331 ; /* wait ?? */
332#endif 332#endif
333 /* set up new command */ 333 /* set up new command */
334 334
335 if (flags & MCI_ARG) 335 if (flags & MCI_ARG)
336 SDICARG = arg; 336 SDICARG = arg;
337 else 337 else
338 SDICARG = 0; 338 SDICARG = 0;
339 339
340 val = cmd | S3C2410_SDICMDCON_CMDSTART | S3C2410_SDICMDCON_SENDERHOST; 340 val = cmd | S3C2410_SDICMDCON_CMDSTART | S3C2410_SDICMDCON_SENDERHOST;
341 if(flags & MCI_RESP) 341 if(flags & MCI_RESP)
342 { 342 {
@@ -344,27 +344,27 @@ static bool send_cmd(const int card_no, const int cmd, const int arg,
344 if(flags & MCI_LONG_RESP) 344 if(flags & MCI_LONG_RESP)
345 val |= S3C2410_SDICMDCON_LONGRSP; 345 val |= S3C2410_SDICMDCON_LONGRSP;
346 } 346 }
347 347
348 /* Clear command/data status flags */ 348 /* Clear command/data status flags */
349 SDICSTA |= 0x0f << 9; 349 SDICSTA |= 0x0f << 9;
350 SDIDSTA |= S3C2410_SDIDSTA_CLEAR_BITS; 350 SDIDSTA |= S3C2410_SDIDSTA_CLEAR_BITS;
351 351
352 /* Initiate the command */ 352 /* Initiate the command */
353 SDICCON = val; 353 SDICCON = val;
354 354
355 if (flags & MCI_RESP) 355 if (flags & MCI_RESP)
356 { 356 {
357 /* wait for response or timeout */ 357 /* wait for response or timeout */
358 do 358 do
359 { 359 {
360 status = SDICSTA; 360 status = SDICSTA;
361 } while ( (status & (S3C2410_SDICMDSTAT_RSPFIN | 361 } while ( (status & (S3C2410_SDICMDSTAT_RSPFIN |
362 S3C2410_SDICMDSTAT_CMDTIMEOUT) ) == 0); 362 S3C2410_SDICMDSTAT_CMDTIMEOUT) ) == 0);
363 debug_r1(cmd); 363 debug_r1(cmd);
364 if (status & S3C2410_SDICMDSTAT_CMDTIMEOUT) 364 if (status & S3C2410_SDICMDSTAT_CMDTIMEOUT)
365 ret = false; 365 ret = false;
366 else if (status & (S3C2410_SDICMDSTAT_RSPFIN)) 366 else if (status & (S3C2410_SDICMDSTAT_RSPFIN))
367 { 367 {
368 /* resp received */ 368 /* resp received */
369 if(flags & MCI_LONG_RESP) 369 if(flags & MCI_LONG_RESP)
370 { 370 {
@@ -381,10 +381,10 @@ static bool send_cmd(const int card_no, const int cmd, const int arg,
381 else 381 else
382 ret = true; 382 ret = true;
383 } 383 }
384 else 384 else
385 { 385 {
386 /* wait for command completion or timeout */ 386 /* wait for command completion or timeout */
387 do 387 do
388 { 388 {
389 status = SDICSTA; 389 status = SDICSTA;
390 } while ( (status & (S3C2410_SDICMDSTAT_CMDSENT | 390 } while ( (status & (S3C2410_SDICMDSTAT_CMDSENT |
@@ -395,12 +395,12 @@ static bool send_cmd(const int card_no, const int cmd, const int arg,
395 else 395 else
396 ret = true; 396 ret = true;
397 } 397 }
398 398
399 /* Clear Command status flags */ 399 /* Clear Command status flags */
400 SDICSTA |= 0x0f << 9; 400 SDICSTA |= 0x0f << 9;
401 401
402 mci_delay(); 402 mci_delay();
403 403
404 return ret; 404 return ret;
405} 405}
406 406
@@ -558,7 +558,7 @@ bool sd_removable(IF_MD_NONVOID(int card_no))
558 const int card_no = 0; 558 const int card_no = 0;
559#endif 559#endif
560 (void)card_no; 560 (void)card_no;
561 561
562 /* not applicable */ 562 /* not applicable */
563 dbgprintf ("sd_remov"); 563 dbgprintf ("sd_remov");
564 return false; 564 return false;
@@ -596,7 +596,7 @@ static int sd_wait_for_state(const int card_no, unsigned int state)
596 } 596 }
597} 597}
598 598
599static int sd_transfer_sectors(int card_no, unsigned long start, 599static int sd_transfer_sectors(int card_no, sector_t start,
600 int count, void* buf, const bool write) 600 int count, void* buf, const bool write)
601{ 601{
602 int ret = EC_OK; 602 int ret = EC_OK;
@@ -636,7 +636,7 @@ static int sd_transfer_sectors(int card_no, unsigned long start,
636 void *dma_buf; 636 void *dma_buf;
637 const int cmd = 637 const int cmd =
638 write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK; 638 write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK;
639 unsigned long start_addr = start; 639 sector_t start_addr = start;
640 640
641 dma_buf = aligned_buffer; 641 dma_buf = aligned_buffer;
642 if(transfer > UNALIGNED_NUM_SECTORS) 642 if(transfer > UNALIGNED_NUM_SECTORS)
@@ -650,10 +650,10 @@ static int sd_transfer_sectors(int card_no, unsigned long start,
650 650
651 /* TODO? */ 651 /* TODO? */
652 SDIFSTA = SDIFSTA | S3C2440_SDIFSTA_FIFORESET; 652 SDIFSTA = SDIFSTA | S3C2440_SDIFSTA_FIFORESET;
653 SDIDCON = S3C2440_SDIDCON_DS_WORD | 653 SDIDCON = S3C2440_SDIDCON_DS_WORD |
654 S3C2410_SDIDCON_BLOCKMODE | S3C2410_SDIDCON_WIDEBUS | 654 S3C2410_SDIDCON_BLOCKMODE | S3C2410_SDIDCON_WIDEBUS |
655 S3C2410_SDIDCON_DMAEN | 655 S3C2410_SDIDCON_DMAEN |
656 S3C2440_SDIDCON_DATSTART | 656 S3C2440_SDIDCON_DATSTART |
657 ( transfer << 0); 657 ( transfer << 0);
658 if (write) 658 if (write)
659 SDIDCON |= S3C2410_SDIDCON_TXAFTERRESP | S3C2410_SDIDCON_XFER_TXSTART; 659 SDIDCON |= S3C2410_SDIDCON_TXAFTERRESP | S3C2410_SDIDCON_XFER_TXSTART;
@@ -665,6 +665,7 @@ static int sd_transfer_sectors(int card_no, unsigned long start,
665 INTPND = SDI_MASK; 665 INTPND = SDI_MASK;
666 666
667 /* Initiate read/write command */ 667 /* Initiate read/write command */
668 // XXX 64-bit
668 if(!send_cmd(card_no, cmd, start_addr, MCI_ARG | MCI_RESP, NULL)) 669 if(!send_cmd(card_no, cmd, start_addr, MCI_ARG | MCI_RESP, NULL))
669 { 670 {
670 ret -= 3*20; 671 ret -= 3*20;
@@ -674,32 +675,32 @@ static int sd_transfer_sectors(int card_no, unsigned long start,
674 if(write) 675 if(write)
675 { 676 {
676 request.source_addr = dma_buf; 677 request.source_addr = dma_buf;
677 request.source_control = DISRCC_LOC_AHB | DISRCC_INC_AUTO; 678 request.source_control = DISRCC_LOC_AHB | DISRCC_INC_AUTO;
678 request.dest_addr = &SDIDAT_LLE; 679 request.dest_addr = &SDIDAT_LLE;
679 request.dest_control = DISRCC_LOC_APB | DISRCC_INC_FIXED; 680 request.dest_control = DISRCC_LOC_APB | DISRCC_INC_FIXED;
680 request.count = transfer * SD_BLOCK_SIZE / sizeof(long); 681 request.count = transfer * SD_BLOCK_SIZE / sizeof(long);
681 request.source_map = DMA_SRC_MAP_SDI; 682 request.source_map = DMA_SRC_MAP_SDI;
682 request.control = DCON_DMD_HS | DCON_SYNC_APB | 683 request.control = DCON_DMD_HS | DCON_SYNC_APB |
683 DCON_HW_SEL | 684 DCON_HW_SEL |
684 DCON_NO_RELOAD | DCON_DSZ_WORD; 685 DCON_NO_RELOAD | DCON_DSZ_WORD;
685 request.callback = NULL; 686 request.callback = NULL;
686 687
687 dma_enable_channel(0, &request); 688 dma_enable_channel(0, &request);
688 } 689 }
689 else 690 else
690 { 691 {
691 request.source_addr = &SDIDAT_LLE; 692 request.source_addr = &SDIDAT_LLE;
692 request.source_control = DISRCC_LOC_APB | DISRCC_INC_FIXED; 693 request.source_control = DISRCC_LOC_APB | DISRCC_INC_FIXED;
693 request.dest_addr = dma_buf; 694 request.dest_addr = dma_buf;
694 request.dest_control = DISRCC_LOC_AHB | DISRCC_INC_AUTO; 695 request.dest_control = DISRCC_LOC_AHB | DISRCC_INC_AUTO;
695 request.count = transfer * SD_BLOCK_SIZE / sizeof(long); 696 request.count = transfer * SD_BLOCK_SIZE / sizeof(long);
696 request.source_map = DMA_SRC_MAP_SDI; 697 request.source_map = DMA_SRC_MAP_SDI;
697 request.control = DCON_DMD_HS | DCON_SYNC_APB | 698 request.control = DCON_DMD_HS | DCON_SYNC_APB |
698 DCON_HW_SEL | 699 DCON_HW_SEL |
699 DCON_NO_RELOAD | DCON_DSZ_WORD; 700 DCON_NO_RELOAD | DCON_DSZ_WORD;
700 request.callback = NULL; 701 request.callback = NULL;
701 702
702 dma_enable_channel(0, &request); 703 dma_enable_channel(0, &request);
703 } 704 }
704 705
705#if 0 706#if 0
@@ -716,12 +717,12 @@ static int sd_transfer_sectors(int card_no, unsigned long start,
716#endif 717#endif
717 718
718 semaphore_wait(&transfer_completion_signal, 100 /*TIMEOUT_BLOCK*/); 719 semaphore_wait(&transfer_completion_signal, 100 /*TIMEOUT_BLOCK*/);
719 720
720 /* wait for DMA to finish */ 721 /* wait for DMA to finish */
721 while (DSTAT0 & DSTAT_STAT_BUSY) 722 while (DSTAT0 & DSTAT_STAT_BUSY)
722 ; 723 ;
723 724
724#if 0 725#if 0
725 status = SDIDSTA; 726 status = SDIDSTA;
726 while ((status & (S3C2410_SDIDSTA_DATATIMEOUT|S3C2410_SDIDSTA_XFERFINISH)) == 0) 727 while ((status & (S3C2410_SDIDSTA_DATATIMEOUT|S3C2410_SDIDSTA_XFERFINISH)) == 0)
727 { 728 {
@@ -738,10 +739,10 @@ static int sd_transfer_sectors(int card_no, unsigned long start,
738 count -= transfer; 739 count -= transfer;
739 loops = 0; /* reset errors counter */ 740 loops = 0; /* reset errors counter */
740 } 741 }
741 else 742 else
742 { 743 {
743 dbgprintf ("SD transfer error : 0x%x\n", transfer_error[card_no]); 744 dbgprintf ("SD transfer error : 0x%x\n", transfer_error[card_no]);
744 745
745 if(loops++ > MAX_TRANSFER_ERRORS) 746 if(loops++ > MAX_TRANSFER_ERRORS)
746 { 747 {
747 led_flash(LED1|LED2, LED3|LED4); 748 led_flash(LED1|LED2, LED3|LED4);
@@ -783,11 +784,11 @@ sd_transfer_error:
783 return ret; 784 return ret;
784} 785}
785 786
786int sd_read_sectors(IF_MD(int card_no,) unsigned long start, int incount, 787int sd_read_sectors(IF_MD(int card_no,) sector_t start, int incount,
787 void* inbuf) 788 void* inbuf)
788{ 789{
789 int ret; 790 int ret;
790 791
791#ifdef HAVE_MULTIDRIVE 792#ifdef HAVE_MULTIDRIVE
792 dbgprintf ("sd_read %d %x %d\n", card_no, start, incount); 793 dbgprintf ("sd_read %d %x %d\n", card_no, start, incount);
793#else 794#else
@@ -804,7 +805,7 @@ int sd_read_sectors(IF_MD(int card_no,) unsigned long start, int incount,
804} 805}
805 806
806/*****************************************************************************/ 807/*****************************************************************************/
807int sd_write_sectors(IF_MD(int drive,) unsigned long start, int count, 808int sd_write_sectors(IF_MD(int drive,) sector_t start, int count,
808 const void* outbuf) 809 const void* outbuf)
809{ 810{
810#ifdef BOOTLOADER /* we don't need write support in bootloader */ 811#ifdef BOOTLOADER /* we don't need write support in bootloader */
@@ -835,7 +836,7 @@ void sd_enable(bool on)
835{ 836{
836 dbgprintf ("sd_enable %d\n", on); 837 dbgprintf ("sd_enable %d\n", on);
837 /* TODO: enable/disable SDI clock */ 838 /* TODO: enable/disable SDI clock */
838 839
839 if (sd_enabled == on) 840 if (sd_enabled == on)
840 return; /* nothing to do */ 841 return; /* nothing to do */
841 if (on) 842 if (on)
@@ -847,14 +848,14 @@ void sd_enable(bool on)
847 sd_enabled = false; 848 sd_enabled = false;
848 } 849 }
849} 850}
850 851
851int sd_init(void) 852int sd_init(void)
852{ 853{
853 int ret = EC_OK; 854 int ret = EC_OK;
854 dbgprintf ("\n==============================\n"); 855 dbgprintf ("\n==============================\n");
855 dbgprintf (" sd_init\n"); 856 dbgprintf (" sd_init\n");
856 dbgprintf ("==============================\n"); 857 dbgprintf ("==============================\n");
857 858
858 init_sdi_controller (0); 859 init_sdi_controller (0);
859#ifndef BOOTLOADER 860#ifndef BOOTLOADER
860 sd_enabled = true; 861 sd_enabled = true;
@@ -893,7 +894,7 @@ long sd_last_disk_activity(void)
893} 894}
894 895
895tCardInfo *card_get_info_target(int card_no) 896tCardInfo *card_get_info_target(int card_no)
896{ 897{
897 return &card_info[card_no]; 898 return &card_info[card_no];
898} 899}
899 900
diff --git a/firmware/target/arm/s5l8700/ata-nand-s5l8700.c b/firmware/target/arm/s5l8700/ata-nand-s5l8700.c
index 7f68b82a0d..34a1c46043 100644
--- a/firmware/target/arm/s5l8700/ata-nand-s5l8700.c
+++ b/firmware/target/arm/s5l8700/ata-nand-s5l8700.c
@@ -29,17 +29,17 @@
29#include "ftl-target.h" 29#include "ftl-target.h"
30#include "nand-target.h" 30#include "nand-target.h"
31 31
32/** static, private data **/ 32/** static, private data **/
33static bool initialized = false; 33static bool initialized = false;
34 34
35/* API Functions */ 35/* API Functions */
36int nand_read_sectors(IF_MD(int drive,) unsigned long start, int incount, 36int nand_read_sectors(IF_MD(int drive,) sector_t start, int incount,
37 void* inbuf) 37 void* inbuf)
38{ 38{
39 return ftl_read(start, incount, inbuf); 39 return ftl_read(start, incount, inbuf);
40} 40}
41 41
42int nand_write_sectors(IF_MD(int drive,) unsigned long start, int count, 42int nand_write_sectors(IF_MD(int drive,) sector_t start, int count,
43 const void* outbuf) 43 const void* outbuf)
44{ 44{
45 return ftl_write(start, count, outbuf); 45 return ftl_write(start, count, outbuf);
@@ -106,7 +106,7 @@ int nand_num_drives(int first_drive)
106{ 106{
107 /* We don't care which logical drive number(s) we have been assigned */ 107 /* We don't care which logical drive number(s) we have been assigned */
108 (void)first_drive; 108 (void)first_drive;
109 109
110 return 1; 110 return 1;
111} 111}
112#endif 112#endif
diff --git a/firmware/target/arm/s5l8702/ipod6g/storage_ata-6g.c b/firmware/target/arm/s5l8702/ipod6g/storage_ata-6g.c
index 7a3be20577..9629b3e30f 100644
--- a/firmware/target/arm/s5l8702/ipod6g/storage_ata-6g.c
+++ b/firmware/target/arm/s5l8702/ipod6g/storage_ata-6g.c
@@ -58,10 +58,9 @@
58#define CEATA_DAT_NONBUSY_TIMEOUT 5000000 58#define CEATA_DAT_NONBUSY_TIMEOUT 5000000
59#define CEATA_MMC_RCA 1 59#define CEATA_MMC_RCA 1
60 60
61
62/** static, private data **/ 61/** static, private data **/
63static uint8_t ceata_taskfile[16] STORAGE_ALIGN_ATTR; 62static uint8_t ceata_taskfile[16] STORAGE_ALIGN_ATTR;
64static uint16_t ata_identify_data[0x100] STORAGE_ALIGN_ATTR; 63static uint16_t ata_identify_data[ATA_IDENTIFY_WORDS] STORAGE_ALIGN_ATTR;
65static bool ceata; 64static bool ceata;
66static bool ata_lba48; 65static bool ata_lba48;
67static bool ata_dma; 66static bool ata_dma;
@@ -510,7 +509,7 @@ static int ata_identify(uint16_t* buf)
510 ata_write_cbr(&ATA_PIO_DVR, 0); 509 ata_write_cbr(&ATA_PIO_DVR, 0);
511 ata_write_cbr(&ATA_PIO_CSD, CMD_IDENTIFY); 510 ata_write_cbr(&ATA_PIO_CSD, CMD_IDENTIFY);
512 PASS_RC(ata_wait_for_start_of_transfer(10000000), 1, 1); 511 PASS_RC(ata_wait_for_start_of_transfer(10000000), 1, 1);
513 for (i = 0; i < 0x100; i++) buf[i] = ata_read_cbr(&ATA_PIO_DTR); 512 for (i = 0; i < ATA_IDENTIFY_WORDS; i++) buf[i] = ata_read_cbr(&ATA_PIO_DTR);
514 } 513 }
515 return 0; 514 return 0;
516} 515}
@@ -701,7 +700,7 @@ static int ata_power_up(void)
701 | (((uint64_t)ata_identify_data[103]) << 48); 700 | (((uint64_t)ata_identify_data[103]) << 48);
702 else 701 else
703 ata_total_sectors = ata_identify_data[60] | (((uint32_t)ata_identify_data[61]) << 16); 702 ata_total_sectors = ata_identify_data[60] | (((uint32_t)ata_identify_data[61]) << 16);
704 ata_total_sectors >>= 3; 703 ata_total_sectors >>= 3; /* ie SECTOR_SIZE/512. */
705 ata_powered = true; 704 ata_powered = true;
706 ata_set_active(); 705 ata_set_active();
707 return 0; 706 return 0;
@@ -966,7 +965,7 @@ static int ata_reset(void)
966 return rc; 965 return rc;
967} 966}
968 967
969int ata_read_sectors(IF_MD(int drive,) unsigned long start, int incount, 968int ata_read_sectors(IF_MD(int drive,) sector_t start, int incount,
970 void* inbuf) 969 void* inbuf)
971{ 970{
972 mutex_lock(&ata_mutex); 971 mutex_lock(&ata_mutex);
@@ -975,7 +974,7 @@ int ata_read_sectors(IF_MD(int drive,) unsigned long start, int incount,
975 return rc; 974 return rc;
976} 975}
977 976
978int ata_write_sectors(IF_MD(int drive,) unsigned long start, int count, 977int ata_write_sectors(IF_MD(int drive,) sector_t start, int count,
979 const void* outbuf) 978 const void* outbuf)
980{ 979{
981 mutex_lock(&ata_mutex); 980 mutex_lock(&ata_mutex);
diff --git a/firmware/target/arm/tcc780x/sd-tcc780x.c b/firmware/target/arm/tcc780x/sd-tcc780x.c
index c80c3b746f..3ef8972ee9 100644
--- a/firmware/target/arm/tcc780x/sd-tcc780x.c
+++ b/firmware/target/arm/tcc780x/sd-tcc780x.c
@@ -104,19 +104,19 @@ static bool sd_poll_status(unsigned int trigger, long timeout)
104 return true; 104 return true;
105} 105}
106 106
107static int sd_command(unsigned int cmd, unsigned int arg, 107static int sd_command(unsigned int cmd, unsigned int arg,
108 unsigned long* response, unsigned int resp_type) 108 unsigned long* response, unsigned int resp_type)
109{ 109{
110 int sdi_cmd = cmd; 110 int sdi_cmd = cmd;
111 111
112 sdi_cmd |= (127<<12) | (1<<11); /* max wait time | enable */ 112 sdi_cmd |= (127<<12) | (1<<11); /* max wait time | enable */
113 113
114 if (resp_type) 114 if (resp_type)
115 { 115 {
116 /* response type & response required flag */ 116 /* response type & response required flag */
117 sdi_cmd |= (resp_type<<7) | (1<<6); 117 sdi_cmd |= (resp_type<<7) | (1<<6);
118 } 118 }
119 119
120 if (cmd == SD_READ_SINGLE_BLOCK || 120 if (cmd == SD_READ_SINGLE_BLOCK ||
121 cmd == SD_READ_MULTIPLE_BLOCK || 121 cmd == SD_READ_MULTIPLE_BLOCK ||
122 cmd == SD_WRITE_BLOCK || 122 cmd == SD_WRITE_BLOCK ||
@@ -124,18 +124,18 @@ static int sd_command(unsigned int cmd, unsigned int arg,
124 { 124 {
125 sdi_cmd |= (1<<10); /* request data transfer */ 125 sdi_cmd |= (1<<10); /* request data transfer */
126 } 126 }
127 127
128 if (!sd_poll_status(SDISTATUS_CMD_PATH_RDY, 100000)) 128 if (!sd_poll_status(SDISTATUS_CMD_PATH_RDY, 100000))
129 return -EC_COMMAND; 129 return -EC_COMMAND;
130 130
131 SDIARGU = arg; 131 SDIARGU = arg;
132 SDICMD = sdi_cmd; 132 SDICMD = sdi_cmd;
133 133
134 udelay(10); 134 udelay(10);
135 135
136 if (response == NULL) 136 if (response == NULL)
137 return 0; 137 return 0;
138 138
139 if (!sd_poll_status(SDISTATUS_RESP_RCVD, 100000)) 139 if (!sd_poll_status(SDISTATUS_RESP_RCVD, 100000))
140 return -EC_COMMAND; 140 return -EC_COMMAND;
141 141
@@ -150,7 +150,7 @@ static int sd_command(unsigned int cmd, unsigned int arg,
150 { 150 {
151 response[0] = SDIRSPARGU0; 151 response[0] = SDIRSPARGU0;
152 } 152 }
153 153
154 return 0; 154 return 0;
155} 155}
156 156
@@ -220,7 +220,7 @@ static int sd1_oneshot_callback(struct timeout *tmo)
220void EXT0(void) 220void EXT0(void)
221{ 221{
222 static struct timeout sd1_oneshot; 222 static struct timeout sd1_oneshot;
223 223
224 timeout_register(&sd1_oneshot, sd1_oneshot_callback, (3*HZ/10), 0); 224 timeout_register(&sd1_oneshot, sd1_oneshot_callback, (3*HZ/10), 0);
225} 225}
226 226
@@ -248,7 +248,7 @@ bool sd_removable(IF_MD_NONVOID(int card_no))
248 const int card_no = 0; 248 const int card_no = 0;
249#endif 249#endif
250 (void)card_no; 250 (void)card_no;
251 251
252 return false; 252 return false;
253} 253}
254 254
@@ -259,7 +259,7 @@ static void sd_init_device(int card_no)
259{ 259{
260 int ret; 260 int ret;
261 unsigned long response; 261 unsigned long response;
262 262
263 /* Initialise card data as blank */ 263 /* Initialise card data as blank */
264 memset(currcard, 0, sizeof(*currcard)); 264 memset(currcard, 0, sizeof(*currcard));
265 265
@@ -282,7 +282,7 @@ static void sd_init_device(int card_no)
282#endif 282#endif
283 283
284 ret = sd_command(SD_GO_IDLE_STATE, 0, NULL, SDICMD_RES_TYPE1); 284 ret = sd_command(SD_GO_IDLE_STATE, 0, NULL, SDICMD_RES_TYPE1);
285 285
286 if (ret < 0) 286 if (ret < 0)
287 goto card_init_error; 287 goto card_init_error;
288 288
@@ -290,30 +290,30 @@ static void sd_init_device(int card_no)
290 SDICLK = (1<<12) | 59; 290 SDICLK = (1<<12) | 59;
291 291
292 sd_command(SD_SEND_IF_COND, 0x1aa, &response, SDICMD_RES_TYPE3); 292 sd_command(SD_SEND_IF_COND, 0x1aa, &response, SDICMD_RES_TYPE3);
293 293
294 if (!sd_poll_status(SDISTATUS_CMD_PATH_RDY, 100000)) 294 if (!sd_poll_status(SDISTATUS_CMD_PATH_RDY, 100000))
295 goto card_init_error; 295 goto card_init_error;
296 296
297 currcard->ocr = 0; 297 currcard->ocr = 0;
298 298
299 long start_tick = current_tick; 299 long start_tick = current_tick;
300 300
301 while ((currcard->ocr & (1<<31)) == 0 301 while ((currcard->ocr & (1<<31)) == 0
302 && TIME_BEFORE(current_tick, start_tick + HZ)) 302 && TIME_BEFORE(current_tick, start_tick + HZ))
303 { 303 {
304 udelay(100); 304 udelay(100);
305 sd_command(SD_APP_CMD, 0, NULL, SDICMD_RES_TYPE1); 305 sd_command(SD_APP_CMD, 0, NULL, SDICMD_RES_TYPE1);
306 306
307 int arg = 0x100000 | ((response == 0x1aa) ? (1<<30):0); 307 int arg = 0x100000 | ((response == 0x1aa) ? (1<<30):0);
308 sd_command(SD_APP_OP_COND, arg, &currcard->ocr, SDICMD_RES_TYPE3); 308 sd_command(SD_APP_OP_COND, arg, &currcard->ocr, SDICMD_RES_TYPE3);
309 } 309 }
310 310
311 if ((currcard->ocr & (1<<31)) == 0) 311 if ((currcard->ocr & (1<<31)) == 0)
312 { 312 {
313 ret = -EC_POWER_UP; 313 ret = -EC_POWER_UP;
314 goto card_init_error; 314 goto card_init_error;
315 } 315 }
316 316
317 ret = sd_command 317 ret = sd_command
318 (SD_ALL_SEND_CID, 0, currcard->cid, SDICMD_RES_TYPE2); 318 (SD_ALL_SEND_CID, 0, currcard->cid, SDICMD_RES_TYPE2);
319 319
@@ -322,39 +322,39 @@ static void sd_init_device(int card_no)
322 322
323 ret = sd_command 323 ret = sd_command
324 (SD_SEND_RELATIVE_ADDR, 0, &currcard->rca, SDICMD_RES_TYPE1); 324 (SD_SEND_RELATIVE_ADDR, 0, &currcard->rca, SDICMD_RES_TYPE1);
325 325
326 if (ret < 0) 326 if (ret < 0)
327 goto card_init_error; 327 goto card_init_error;
328 328
329 ret = sd_command 329 ret = sd_command
330 (SD_SEND_CSD, currcard->rca, currcard->csd, SDICMD_RES_TYPE2); 330 (SD_SEND_CSD, currcard->rca, currcard->csd, SDICMD_RES_TYPE2);
331 331
332 if (ret < 0) 332 if (ret < 0)
333 goto card_init_error; 333 goto card_init_error;
334 334
335 sd_parse_csd(currcard); 335 sd_parse_csd(currcard);
336 336
337 ret = sd_command 337 ret = sd_command
338 (SD_SELECT_CARD, currcard->rca, NULL, SDICMD_RES_TYPE1); 338 (SD_SELECT_CARD, currcard->rca, NULL, SDICMD_RES_TYPE1);
339 339
340 if (ret < 0) 340 if (ret < 0)
341 goto card_init_error; 341 goto card_init_error;
342 342
343 ret = sd_command 343 ret = sd_command
344 (SD_APP_CMD, currcard->rca, NULL, SDICMD_RES_TYPE1); 344 (SD_APP_CMD, currcard->rca, NULL, SDICMD_RES_TYPE1);
345 345
346 if (ret < 0) 346 if (ret < 0)
347 goto card_init_error; 347 goto card_init_error;
348 348
349 ret = sd_command /* 4 bit */ 349 ret = sd_command /* 4 bit */
350 (SD_SET_BUS_WIDTH, currcard->rca | 2, NULL, SDICMD_RES_TYPE1); 350 (SD_SET_BUS_WIDTH, currcard->rca | 2, NULL, SDICMD_RES_TYPE1);
351 351
352 if (ret < 0) 352 if (ret < 0)
353 goto card_init_error; 353 goto card_init_error;
354 354
355 ret = sd_command 355 ret = sd_command
356 (SD_SET_BLOCKLEN, currcard->blocksize, NULL, SDICMD_RES_TYPE1); 356 (SD_SET_BLOCKLEN, currcard->blocksize, NULL, SDICMD_RES_TYPE1);
357 357
358 if (ret < 0) 358 if (ret < 0)
359 goto card_init_error; 359 goto card_init_error;
360 360
@@ -386,7 +386,7 @@ static void sd_select_device(int card_no)
386 } 386 }
387} 387}
388 388
389int sd_read_sectors(IF_MD(int card_no,) unsigned long start, int incount, 389int sd_read_sectors(IF_MD(int card_no,) sector_t start, int incount,
390 void* inbuf) 390 void* inbuf)
391{ 391{
392#ifndef HAVE_MULTIDRIVE 392#ifndef HAVE_MULTIDRIVE
@@ -416,23 +416,24 @@ sd_read_retry:
416 ret = currcard->initialized; 416 ret = currcard->initialized;
417 goto sd_read_error; 417 goto sd_read_error;
418 } 418 }
419 419
420 last_disk_activity = current_tick; 420 last_disk_activity = current_tick;
421 421
422 ret = sd_wait_for_state(SD_TRAN, EC_TRAN_READ_ENTRY); 422 ret = sd_wait_for_state(SD_TRAN, EC_TRAN_READ_ENTRY);
423 423
424 if (ret < 0) 424 if (ret < 0)
425 goto sd_read_error; 425 goto sd_read_error;
426 426
427 /* Use full SD clock for data transfer (PCK_SDMMC) */ 427 /* Use full SD clock for data transfer (PCK_SDMMC) */
428 SDICLK = (1<<13) | (1<<12); /* bypass divider | enable */ 428 SDICLK = (1<<13) | (1<<12); /* bypass divider | enable */
429 429
430 /* Block count | FIFO count | Block size (2^9) | 4-bit bus */ 430 /* Block count | FIFO count | Block size (2^9) | 4-bit bus */
431 SDIDCTRL = (incount << 13) | (4<<8) | (9<<4) | (1<<2); 431 SDIDCTRL = (incount << 13) | (4<<8) | (9<<4) | (1<<2);
432 SDIDCTRL |= (1<<12); /* nReset */ 432 SDIDCTRL |= (1<<12); /* nReset */
433 433
434 SDIDCTRL2 = (1<<2); /* multi block, read */ 434 SDIDCTRL2 = (1<<2); /* multi block, read */
435 435
436 // XXX 64-bit
436 if (currcard->ocr & (1<<30)) 437 if (currcard->ocr & (1<<30))
437 ret = sd_command(SD_READ_MULTIPLE_BLOCK, start, NULL, SDICMD_RES_TYPE1); 438 ret = sd_command(SD_READ_MULTIPLE_BLOCK, start, NULL, SDICMD_RES_TYPE1);
438 else 439 else
@@ -500,7 +501,7 @@ sd_read_error:
500 } 501 }
501} 502}
502 503
503int sd_write_sectors(IF_MD(int card_no,) unsigned long start, int count, 504int sd_write_sectors(IF_MD(int card_no,) sector_t start, int count,
504 const void* outbuf) 505 const void* outbuf)
505{ 506{
506/* Write support is not finished yet */ 507/* Write support is not finished yet */
@@ -538,21 +539,22 @@ sd_write_retry:
538 ret = currcard->initialized; 539 ret = currcard->initialized;
539 goto sd_write_error; 540 goto sd_write_error;
540 } 541 }
541 542
542 ret = sd_wait_for_state(SD_TRAN, EC_TRAN_WRITE_ENTRY); 543 ret = sd_wait_for_state(SD_TRAN, EC_TRAN_WRITE_ENTRY);
543 544
544 if (ret < 0) 545 if (ret < 0)
545 goto sd_write_error; 546 goto sd_write_error;
546 547
547 /* Use full SD clock for data transfer (PCK_SDMMC) */ 548 /* Use full SD clock for data transfer (PCK_SDMMC) */
548 SDICLK = (1<<13) | (1<<12); /* bypass divider | enable */ 549 SDICLK = (1<<13) | (1<<12); /* bypass divider | enable */
549 550
550 /* Block count | FIFO count | Block size (2^9) | 4-bit bus */ 551 /* Block count | FIFO count | Block size (2^9) | 4-bit bus */
551 SDIDCTRL = (count<<13) | (4<<8) | (9<<4) | (1<<2); 552 SDIDCTRL = (count<<13) | (4<<8) | (9<<4) | (1<<2);
552 SDIDCTRL |= (1<<12); /* nReset */ 553 SDIDCTRL |= (1<<12); /* nReset */
553 554
554 SDIDCTRL2 = (1<<2) | (1<<1); /* multi block, write */ 555 SDIDCTRL2 = (1<<2) | (1<<1); /* multi block, write */
555 556
557 // XXX 64-bit
556 if (currcard->ocr & (1<<30)) 558 if (currcard->ocr & (1<<30))
557 ret = sd_command(SD_WRITE_MULTIPLE_BLOCK, start, NULL, SDICMD_RES_TYPE1); 559 ret = sd_command(SD_WRITE_MULTIPLE_BLOCK, start, NULL, SDICMD_RES_TYPE1);
558 else 560 else
@@ -578,7 +580,7 @@ sd_write_retry:
578 else 580 else
579 { 581 {
580 int tmp_buf[4]; 582 int tmp_buf[4];
581 583
582 memcpy(tmp_buf, outbuf, 16); 584 memcpy(tmp_buf, outbuf, 16);
583 585
584 SDIWDATA = tmp_buf[0]; 586 SDIWDATA = tmp_buf[0];
@@ -646,12 +648,12 @@ void sd_enable(bool on)
646 PCLK_SDMMC &= ~PCK_EN; 648 PCLK_SDMMC &= ~PCK_EN;
647 } 649 }
648} 650}
649 651
650int sd_init(void) 652int sd_init(void)
651{ 653{
652 static bool initialized = false; 654 static bool initialized = false;
653 int ret = 0; 655 int ret = 0;
654 656
655 if (!initialized) 657 if (!initialized)
656 mutex_init(&sd_mtx); 658 mutex_init(&sd_mtx);
657 659
@@ -678,7 +680,7 @@ int sd_init(void)
678 GPIOC_DIR |= (1<<24); 680 GPIOC_DIR |= (1<<24);
679 681
680 sleep(HZ/10); 682 sleep(HZ/10);
681 683
682#ifdef HAVE_HOTSWAP 684#ifdef HAVE_HOTSWAP
683 /* Configure interrupts for the card slot */ 685 /* Configure interrupts for the card slot */
684 TMODE &= ~EXT0_IRQ_MASK; /* edge-triggered */ 686 TMODE &= ~EXT0_IRQ_MASK; /* edge-triggered */
@@ -696,7 +698,7 @@ long sd_last_disk_activity(void)
696} 698}
697 699
698tCardInfo *card_get_info_target(int card_no) 700tCardInfo *card_get_info_target(int card_no)
699{ 701{
700 return &card_info[card_no]; 702 return &card_info[card_no];
701} 703}
702 704
@@ -706,7 +708,7 @@ int sd_num_drives(int first_drive)
706{ 708{
707 /* Store which logical drive number(s) we have been assigned */ 709 /* Store which logical drive number(s) we have been assigned */
708 sd_first_drive = first_drive; 710 sd_first_drive = first_drive;
709 711
710#if defined(HAVE_INTERNAL_SD) && defined(HAVE_HOTSWAP) 712#if defined(HAVE_INTERNAL_SD) && defined(HAVE_HOTSWAP)
711 return 2; 713 return 2;
712#else 714#else
diff --git a/firmware/target/arm/tms320dm320/creative-zvm/ata-creativezvm.c b/firmware/target/arm/tms320dm320/creative-zvm/ata-creativezvm.c
index a1985472a0..76929e603e 100644
--- a/firmware/target/arm/tms320dm320/creative-zvm/ata-creativezvm.c
+++ b/firmware/target/arm/tms320dm320/creative-zvm/ata-creativezvm.c
@@ -126,8 +126,10 @@ void GIO2(void)
126 126
127#define VFAT_SECTOR_SIZE(x) ( (x)/0x8000 ) /* 1GB array requires 80kB of RAM */ 127#define VFAT_SECTOR_SIZE(x) ( (x)/0x8000 ) /* 1GB array requires 80kB of RAM */
128 128
129extern int ata_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf); 129extern int ata_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf);
130extern int ata_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf); 130extern int ata_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf);
131
132// XXX 64-bit: Due to this it's not likely that this target will ever handle 64-bit storage.
131 133
132struct main_header 134struct main_header
133{ 135{
@@ -253,9 +255,9 @@ static void cfs_init(void)
253 /* Read root inode */ 255 /* Read root inode */
254 _ata_read_sectors(CFS_CLUSTER2CLUSTER(cfs->first_inode), 64, &sector2); 256 _ata_read_sectors(CFS_CLUSTER2CLUSTER(cfs->first_inode), 64, &sector2);
255 root_inode = (struct cfs_inode*)&sector2; 257 root_inode = (struct cfs_inode*)&sector2;
256 258
257 logf("Root inode = 0x%x", root_inode); 259 logf("Root inode = 0x%x", root_inode);
258 260
259 logf("0x%x 0x%x", CFS_CLUSTER2CLUSTER(root_inode->first_class_chain[0]), root_inode->first_class_chain[0]); 261 logf("0x%x 0x%x", CFS_CLUSTER2CLUSTER(root_inode->first_class_chain[0]), root_inode->first_class_chain[0]);
260 262
261 /* Read root inode's first sector */ 263 /* Read root inode's first sector */
@@ -277,9 +279,9 @@ static void cfs_init(void)
277 vfat_inode_nr = root_direntry_items[i].inode_number; 279 vfat_inode_nr = root_direntry_items[i].inode_number;
278 } 280 }
279 } 281 }
280 282
281 logf("VFAT inode = 0x%x", vfat_inode_nr); 283 logf("VFAT inode = 0x%x", vfat_inode_nr);
282 284
283 if(vfat_inode_nr != 0) 285 if(vfat_inode_nr != 0)
284 { 286 {
285 /* Read VFAT inode */ 287 /* Read VFAT inode */
@@ -384,19 +386,19 @@ static void cfs_init(void)
384 cfs_inited = true; 386 cfs_inited = true;
385} 387}
386 388
387static inline unsigned long map_sector(unsigned long sector) 389static inline sector_t map_sector(sector_t sector)
388{ 390{
389 /* 391 /*
390 * Sector mapping: start of CFS + FAT_SECTOR2CFS_SECTOR(sector) + missing part 392 * Sector mapping: start of CFS + FAT_SECTOR2CFS_SECTOR(sector) + missing part
391 * FAT works with sectors of 0x200 bytes, CFS with sectors of 0x8000 bytes. 393 * FAT works with sectors of 0x200 bytes, CFS with sectors of 0x8000 bytes.
392 */ 394 */
393#ifndef BOOTLOADER 395#ifndef BOOTLOADER
394 unsigned long *sectors = core_get_data(sectors_handle); 396 sector_t *sectors = core_get_data(sectors_handle);
395#endif 397#endif
396 return cfs_start+sectors[sector/64]*64+sector%64; 398 return cfs_start+sectors[sector/64]*64+sector%64;
397} 399}
398 400
399int ata_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf) 401int ata_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf)
400{ 402{
401 if(!cfs_inited) 403 if(!cfs_inited)
402 cfs_init(); 404 cfs_init();
@@ -423,7 +425,7 @@ int ata_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf
423 } 425 }
424} 426}
425 427
426int ata_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf) 428int ata_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf)
427{ 429{
428 if(!cfs_inited) 430 if(!cfs_inited)
429 cfs_init(); 431 cfs_init();
diff --git a/firmware/target/arm/tms320dm320/creative-zvm/ata-target.h b/firmware/target/arm/tms320dm320/creative-zvm/ata-target.h
index d0aa12e040..41b8e73ad4 100644
--- a/firmware/target/arm/tms320dm320/creative-zvm/ata-target.h
+++ b/firmware/target/arm/tms320dm320/creative-zvm/ata-target.h
@@ -36,8 +36,8 @@
36/* Nasty hack, but Creative is nasty... */ 36/* Nasty hack, but Creative is nasty... */
37#define ata_read_sectors _ata_read_sectors 37#define ata_read_sectors _ata_read_sectors
38#define ata_write_sectors _ata_write_sectors 38#define ata_write_sectors _ata_write_sectors
39extern int _ata_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf); 39extern int _ata_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf);
40extern int _ata_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf); 40extern int _ata_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf);
41 41
42/* General purpose memory region #1 */ 42/* General purpose memory region #1 */
43#define ATA_IOBASE 0x50FEE000 43#define ATA_IOBASE 0x50FEE000
diff --git a/firmware/target/arm/tms320dm320/sdmmc-dm320.c b/firmware/target/arm/tms320dm320/sdmmc-dm320.c
index e66a4cb3c7..17cb239374 100644
--- a/firmware/target/arm/tms320dm320/sdmmc-dm320.c
+++ b/firmware/target/arm/tms320dm320/sdmmc-dm320.c
@@ -18,7 +18,7 @@
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#include "system.h" 22#include "system.h"
23#include <string.h> 23#include <string.h>
24#include "gcc_extensions.h" 24#include "gcc_extensions.h"
@@ -97,7 +97,7 @@ struct sd_card_status
97 int retry_max; 97 int retry_max;
98}; 98};
99 99
100/** static, private data **/ 100/** static, private data **/
101 101
102/* for compatibility */ 102/* for compatibility */
103static long last_disk_activity = -1; 103static long last_disk_activity = -1;
@@ -123,7 +123,7 @@ static struct mutex sd_mtx SHAREDBSS_ATTR;
123static struct semaphore data_done SHAREDBSS_ATTR; 123static struct semaphore data_done SHAREDBSS_ATTR;
124static volatile unsigned int transfer_error[NUM_DRIVES]; 124static volatile unsigned int transfer_error[NUM_DRIVES];
125/* align on cache line size */ 125/* align on cache line size */
126static unsigned char aligned_buffer[UNALIGNED_NUM_SECTORS * SD_BLOCK_SIZE] 126static unsigned char aligned_buffer[UNALIGNED_NUM_SECTORS * SD_BLOCK_SIZE]
127 __attribute__((aligned(32))); 127 __attribute__((aligned(32)));
128 128
129static void sd_card_mux(int card_no) 129static void sd_card_mux(int card_no)
@@ -397,7 +397,7 @@ static int sd_init_card(const int card_no)
397 SDHC_RESP_FMT_1, &currcard->rca); 397 SDHC_RESP_FMT_1, &currcard->rca);
398 if (ret < 0) 398 if (ret < 0)
399 { 399 {
400 dbgprintf("SD_SEND_RELATIVE_ADDR failed"); 400 dbgprintf("SD_SEND_RELATIVE_ADDR failed");
401 return -1; 401 return -1;
402 } 402 }
403 403
@@ -559,7 +559,7 @@ bool sd_removable(IF_MD_NONVOID(int card_no))
559#ifdef HAVE_MULTIDRIVE 559#ifdef HAVE_MULTIDRIVE
560 (void)card_no; 560 (void)card_no;
561#endif 561#endif
562 562
563 /* not applicable */ 563 /* not applicable */
564 return false; 564 return false;
565} 565}
@@ -597,17 +597,17 @@ static int sd_wait_for_state(unsigned int state)
597 } 597 }
598} 598}
599 599
600static int sd_transfer_sectors(int card_no, unsigned long start, 600static int sd_transfer_sectors(int card_no, sector_t start,
601 int count, void *buffer, bool write) 601 int count, void *buffer, bool write)
602{ 602{
603 int ret; 603 int ret;
604 unsigned long start_addr; 604 sector_t start_addr;
605 int dma_channel = -1; 605 int dma_channel = -1;
606 bool use_direct_dma; 606 bool use_direct_dma;
607 int count_per_dma; 607 int count_per_dma;
608 unsigned long rel_addr; 608 unsigned long rel_addr;
609 609
610 dbgprintf("transfer %d %d %d", card_no, start, count); 610 dbgprintf("transfer %d %lu %d", card_no, start, count);
611 mutex_lock(&sd_mtx); 611 mutex_lock(&sd_mtx);
612 enable_controller(true); 612 enable_controller(true);
613 613
@@ -673,6 +673,7 @@ sd_transfer_retry:
673 if (!(card_info[card_no].ocr & SD_OCR_CARD_CAPACITY_STATUS)) 673 if (!(card_info[card_no].ocr & SD_OCR_CARD_CAPACITY_STATUS))
674 start_addr *= SD_BLOCK_SIZE; /* not SDHC */ 674 start_addr *= SD_BLOCK_SIZE; /* not SDHC */
675 675
676 // XXX 64-bit
676 ret = sd_command(write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK, 677 ret = sd_command(write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK,
677 start_addr, MMC_CMD_DCLR | MMC_CMD_DATA | 678 start_addr, MMC_CMD_DCLR | MMC_CMD_DATA |
678 SDHC_RESP_FMT_1 | (write ? MMC_CMD_WRITE : 0), 679 SDHC_RESP_FMT_1 | (write ? MMC_CMD_WRITE : 0),
@@ -765,7 +766,7 @@ sd_transfer_error:
765 } 766 }
766} 767}
767 768
768int sd_read_sectors(IF_MD(int card_no,) unsigned long start, int incount, 769int sd_read_sectors(IF_MD(int card_no,) sector_t start, int incount,
769 void* inbuf) 770 void* inbuf)
770{ 771{
771#ifndef HAVE_MULTIDRIVE 772#ifndef HAVE_MULTIDRIVE
@@ -774,7 +775,7 @@ int sd_read_sectors(IF_MD(int card_no,) unsigned long start, int incount,
774 return sd_transfer_sectors(card_no, start, incount, inbuf, false); 775 return sd_transfer_sectors(card_no, start, incount, inbuf, false);
775} 776}
776 777
777int sd_write_sectors(IF_MD(int card_no,) unsigned long start, int count, 778int sd_write_sectors(IF_MD(int card_no,) sector_t start, int count,
778 const void* outbuf) 779 const void* outbuf)
779{ 780{
780#ifndef HAVE_MULTIDRIVE 781#ifndef HAVE_MULTIDRIVE
@@ -862,7 +863,7 @@ long sd_last_disk_activity(void)
862} 863}
863 864
864tCardInfo *card_get_info_target(int card_no) 865tCardInfo *card_get_info_target(int card_no)
865{ 866{
866 return &card_info[card_no]; 867 return &card_info[card_no];
867} 868}
868 869
diff --git a/firmware/target/hosted/filesystem-unix.c b/firmware/target/hosted/filesystem-unix.c
index 3a14480e11..4869d07263 100644
--- a/firmware/target/hosted/filesystem-unix.c
+++ b/firmware/target/hosted/filesystem-unix.c
@@ -209,9 +209,9 @@ int os_opendir_and_fd(const char *osdirname, DIR **osdirpp, int *osfdp)
209} 209}
210 210
211/* do we really need this in the app? (in the sim, yes) */ 211/* do we really need this in the app? (in the sim, yes) */
212void volume_size(IF_MV(int volume,) unsigned long *sizep, unsigned long *freep) 212void volume_size(IF_MV(int volume,) sector_t *sizep, sector_t *freep)
213{ 213{
214 unsigned long size = 0, free = 0; 214 sector_t size = 0, free = 0;
215 char volpath[MAX_PATH]; 215 char volpath[MAX_PATH];
216 struct statfs fs; 216 struct statfs fs;
217 217
diff --git a/firmware/target/hosted/filesystem-win32.c b/firmware/target/hosted/filesystem-win32.c
index ded73b619f..ebb7f283ac 100644
--- a/firmware/target/hosted/filesystem-win32.c
+++ b/firmware/target/hosted/filesystem-win32.c
@@ -472,7 +472,7 @@ int os_modtime(const char *path, time_t modtime)
472 472
473int os_volume_path(IF_MV(int volume, ) char *buffer, size_t bufsize); 473int os_volume_path(IF_MV(int volume, ) char *buffer, size_t bufsize);
474 474
475void volume_size(IF_MV(int volume,) unsigned long *sizep, unsigned long *freep) 475void volume_size(IF_MV(int volume,) sector_t *sizep, sector_t *freep)
476{ 476{
477 ULARGE_INTEGER free = { .QuadPart = 0 }, 477 ULARGE_INTEGER free = { .QuadPart = 0 },
478 size = { .QuadPart = 0 }; 478 size = { .QuadPart = 0 };
diff --git a/firmware/target/mips/ingenic_jz47xx/ata-nand-jz4740.c b/firmware/target/mips/ingenic_jz47xx/ata-nand-jz4740.c
index 0c9ee98eb9..77fd5c013b 100644
--- a/firmware/target/mips/ingenic_jz47xx/ata-nand-jz4740.c
+++ b/firmware/target/mips/ingenic_jz47xx/ata-nand-jz4740.c
@@ -84,7 +84,7 @@ struct nand_param
84 * 84 *
85 */ 85 */
86 86
87static volatile unsigned long nand_address; 87static volatile sector_t nand_address;
88#define NAND_DATAPORT (nand_address) 88#define NAND_DATAPORT (nand_address)
89#define NAND_ADDRPORT (nand_address+0x10000) 89#define NAND_ADDRPORT (nand_address+0x10000)
90#define NAND_COMMPORT (nand_address+0x08000) 90#define NAND_COMMPORT (nand_address+0x08000)
@@ -111,7 +111,7 @@ static volatile unsigned long nand_address;
111static struct nand_info* chip_info = NULL; 111static struct nand_info* chip_info = NULL;
112static struct nand_info* banks[4]; 112static struct nand_info* banks[4];
113static unsigned int nr_banks = 1; 113static unsigned int nr_banks = 1;
114static unsigned long bank_size; 114static sector_t bank_size;
115static struct nand_param internal_param; 115static struct nand_param internal_param;
116static struct mutex nand_mtx; 116static struct mutex nand_mtx;
117#ifdef USE_DMA 117#ifdef USE_DMA
@@ -282,7 +282,7 @@ static void jz_rs_correct(unsigned char *dat, int idx, int mask)
282/* 282/*
283 * Read oob 283 * Read oob
284 */ 284 */
285static int jz_nand_read_oob(unsigned long page_addr, unsigned char *buf, int size) 285static int jz_nand_read_oob(sector_t page_addr, unsigned char *buf, int size)
286{ 286{
287 struct nand_param *nandp = &internal_param; 287 struct nand_param *nandp = &internal_param;
288 int page_size, row_cycle, bus_width; 288 int page_size, row_cycle, bus_width;
@@ -338,7 +338,7 @@ static int jz_nand_read_oob(unsigned long page_addr, unsigned char *buf, int siz
338 * page - page number within a block: 0, 1, 2, ... 338 * page - page number within a block: 0, 1, 2, ...
339 * dst - pointer to target buffer 339 * dst - pointer to target buffer
340 */ 340 */
341static int jz_nand_read_page(unsigned long page_addr, unsigned char *dst) 341static int jz_nand_read_page(sector_t page_addr, unsigned char *dst)
342{ 342{
343 struct nand_param *nandp = &internal_param; 343 struct nand_param *nandp = &internal_param;
344 int page_size, oob_size; 344 int page_size, oob_size;
@@ -611,7 +611,7 @@ int nand_init(void)
611 return res; 611 return res;
612} 612}
613 613
614static inline int read_sector(unsigned long start, unsigned int count, 614static inline int read_sector(sector_t start, unsigned int count,
615 void* buf, unsigned int chip_size) 615 void* buf, unsigned int chip_size)
616{ 616{
617 register int ret; 617 register int ret;
@@ -627,14 +627,14 @@ static inline int read_sector(unsigned long start, unsigned int count,
627 return ret; 627 return ret;
628} 628}
629 629
630int nand_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf) 630int nand_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf)
631{ 631{
632#ifdef HAVE_MULTIDRIVE 632#ifdef HAVE_MULTIDRIVE
633 (void)drive; 633 (void)drive;
634#endif 634#endif
635 int ret = 0; 635 int ret = 0;
636 unsigned int i, _count, chip_size = chip_info->page_size; 636 unsigned int i, _count, chip_size = chip_info->page_size;
637 unsigned long _start; 637 sector_t _start;
638 638
639 logf("start"); 639 logf("start");
640 mutex_lock(&nand_mtx); 640 mutex_lock(&nand_mtx);
@@ -670,7 +670,7 @@ int nand_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* bu
670} 670}
671 671
672/* TODO */ 672/* TODO */
673int nand_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf) 673int nand_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf)
674{ 674{
675 (void)start; 675 (void)start;
676 (void)count; 676 (void)count;
diff --git a/firmware/target/mips/ingenic_jz47xx/ata-nand-jz4760.c b/firmware/target/mips/ingenic_jz47xx/ata-nand-jz4760.c
index 914cd6d9d3..ff9b7e419e 100644
--- a/firmware/target/mips/ingenic_jz47xx/ata-nand-jz4760.c
+++ b/firmware/target/mips/ingenic_jz47xx/ata-nand-jz4760.c
@@ -110,7 +110,7 @@ struct nand_param {
110 110
111static struct nand_info* chip_info = NULL; 111static struct nand_info* chip_info = NULL;
112static struct nand_info* bank; 112static struct nand_info* bank;
113static unsigned long nand_size; 113static sector_t nand_size;
114static struct nand_param internal_param; 114static struct nand_param internal_param;
115static struct mutex nand_mtx; 115static struct mutex nand_mtx;
116#ifdef USE_DMA 116#ifdef USE_DMA
@@ -281,7 +281,7 @@ static void jz_rs_correct(unsigned char *dat, int idx, int mask)
281/* 281/*
282 * Read oob 282 * Read oob
283 */ 283 */
284static int jz_nand_read_oob(unsigned long page_addr, unsigned char *buf, int size) 284static int jz_nand_read_oob(sector_t page_addr, unsigned char *buf, int size)
285{ 285{
286 struct nand_param *nandp = &internal_param; 286 struct nand_param *nandp = &internal_param;
287 int page_size, row_cycle, bus_width; 287 int page_size, row_cycle, bus_width;
@@ -337,7 +337,7 @@ static int jz_nand_read_oob(unsigned long page_addr, unsigned char *buf, int siz
337 * page - page number within a block: 0, 1, 2, ... 337 * page - page number within a block: 0, 1, 2, ...
338 * dst - pointer to target buffer 338 * dst - pointer to target buffer
339 */ 339 */
340static int jz_nand_read_page(unsigned long page_addr, unsigned char *dst) 340static int jz_nand_read_page(sector_t page_addr, unsigned char *dst)
341{ 341{
342 struct nand_param *nandp = &internal_param; 342 struct nand_param *nandp = &internal_param;
343 int page_size, oob_size; 343 int page_size, oob_size;
@@ -532,7 +532,7 @@ int nand_init(void)
532 return res; 532 return res;
533} 533}
534 534
535static inline int read_sector(unsigned long start, unsigned int count, 535static inline int read_sector(sector_t start, unsigned int count,
536 void* buf, unsigned int chip_size) 536 void* buf, unsigned int chip_size)
537{ 537{
538 register int ret; 538 register int ret;
@@ -548,7 +548,7 @@ static inline int read_sector(unsigned long start, unsigned int count,
548 return ret; 548 return ret;
549} 549}
550 550
551static inline int write_sector(unsigned long start, unsigned int count, 551static inline int write_sector(sector_t start, unsigned int count,
552 const void* buf, unsigned int chip_size) 552 const void* buf, unsigned int chip_size)
553{ 553{
554 int ret = 0; 554 int ret = 0;
@@ -563,14 +563,14 @@ static inline int write_sector(unsigned long start, unsigned int count,
563 return ret; 563 return ret;
564} 564}
565 565
566int nand_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf) 566int nand_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf)
567{ 567{
568#ifdef HAVE_MULTIDRIVE 568#ifdef HAVE_MULTIDRIVE
569 (void)drive; 569 (void)drive;
570#endif 570#endif
571 int ret = 0; 571 int ret = 0;
572 unsigned int _count, chip_size = chip_info->page_size; 572 unsigned int _count, chip_size = chip_info->page_size;
573 unsigned long _start; 573 sector_t _start;
574 574
575 logf("start"); 575 logf("start");
576 mutex_lock(&nand_mtx); 576 mutex_lock(&nand_mtx);
@@ -590,14 +590,14 @@ int nand_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* bu
590 return ret; 590 return ret;
591} 591}
592 592
593int nand_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf) 593int nand_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf)
594{ 594{
595#ifdef HAVE_MULTIDRIVE 595#ifdef HAVE_MULTIDRIVE
596 (void)drive; 596 (void)drive;
597#endif 597#endif
598 int ret = 0; 598 int ret = 0;
599 unsigned int _count, chip_size = chip_info->page_size; 599 unsigned int _count, chip_size = chip_info->page_size;
600 unsigned long _start; 600 sector_t _start;
601 601
602 logf("start"); 602 logf("start");
603 mutex_lock(&nand_mtx); 603 mutex_lock(&nand_mtx);
diff --git a/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c b/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c
index ce8a3d7479..066be4e987 100644
--- a/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c
+++ b/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c
@@ -42,7 +42,6 @@ static struct mutex sd_mtx;
42 42
43static int use_4bit; 43static int use_4bit;
44static int num_6; 44static int num_6;
45static int sd2_0;
46 45
47//#define SD_DMA_ENABLE 46//#define SD_DMA_ENABLE
48#define SD_DMA_INTERRUPT 0 47#define SD_DMA_INTERRUPT 0
@@ -598,7 +597,7 @@ static int jz_sd_transmit_data(struct sd_request *req)
598static inline unsigned int jz_sd_calc_clkrt(unsigned int rate) 597static inline unsigned int jz_sd_calc_clkrt(unsigned int rate)
599{ 598{
600 unsigned int clkrt; 599 unsigned int clkrt;
601 unsigned int clk_src = sd2_0 ? SD_CLOCK_HIGH : SD_CLOCK_FAST; 600 unsigned int clk_src = card.sd2plus ? SD_CLOCK_HIGH : SD_CLOCK_FAST;
602 601
603 clkrt = 0; 602 clkrt = 0;
604 while (rate < clk_src) 603 while (rate < clk_src)
@@ -716,7 +715,7 @@ static int jz_sd_exec_cmd(struct sd_request *request)
716 events = SD_EVENT_RX_DATA_DONE; 715 events = SD_EVENT_RX_DATA_DONE;
717 break; 716 break;
718 717
719 case 6: 718 case SD_SWITCH_FUNC:
720 if (num_6 < 2) 719 if (num_6 < 2)
721 { 720 {
722#if defined(SD_DMA_ENABLE) 721#if defined(SD_DMA_ENABLE)
@@ -1086,7 +1085,6 @@ static int sd_init_card_state(struct sd_request *request)
1086 (request->response[3+i*4]<< 8) | request->response[4+i*4]); 1085 (request->response[3+i*4]<< 8) | request->response[4+i*4]);
1087 1086
1088 sd_parse_csd(&card); 1087 sd_parse_csd(&card);
1089 sd2_0 = (card_extract_bits(card.csd, 127, 2) == 1);
1090 1088
1091 logf("CSD: %08lx%08lx%08lx%08lx", card.csd[0], card.csd[1], card.csd[2], card.csd[3]); 1089 logf("CSD: %08lx%08lx%08lx%08lx", card.csd[0], card.csd[1], card.csd[2], card.csd[3]);
1092 DEBUG("SD card is ready"); 1090 DEBUG("SD card is ready");
@@ -1155,7 +1153,7 @@ static int sd_select_card(void)
1155 if (retval) 1153 if (retval)
1156 return retval; 1154 return retval;
1157 1155
1158 if (sd2_0) 1156 if (card.sd2plus)
1159 { 1157 {
1160 retval = sd_read_switch(&request); 1158 retval = sd_read_switch(&request);
1161 if (!retval) 1159 if (!retval)
@@ -1188,7 +1186,6 @@ static int __sd_init_device(void)
1188 /* Initialise card data as blank */ 1186 /* Initialise card data as blank */
1189 memset(&card, 0, sizeof(tCardInfo)); 1187 memset(&card, 0, sizeof(tCardInfo));
1190 1188
1191 sd2_0 = 0;
1192 num_6 = 0; 1189 num_6 = 0;
1193 use_4bit = 0; 1190 use_4bit = 0;
1194 1191
@@ -1250,7 +1247,7 @@ static inline void sd_stop_transfer(void)
1250 mutex_unlock(&sd_mtx); 1247 mutex_unlock(&sd_mtx);
1251} 1248}
1252 1249
1253int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf) 1250int sd_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf)
1254{ 1251{
1255#ifdef HAVE_MULTIDRIVE 1252#ifdef HAVE_MULTIDRIVE
1256 (void)drive; 1253 (void)drive;
@@ -1276,7 +1273,8 @@ int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf)
1276 if ((retval = sd_unpack_r1(&request, &r1))) 1273 if ((retval = sd_unpack_r1(&request, &r1)))
1277 goto err; 1274 goto err;
1278 1275
1279 if (sd2_0) 1276 // XXX 64-bit
1277 if (card.sd2plus)
1280 { 1278 {
1281 sd_send_cmd(&request, SD_READ_MULTIPLE_BLOCK, start, 1279 sd_send_cmd(&request, SD_READ_MULTIPLE_BLOCK, start,
1282 count, SD_BLOCK_SIZE, RESPONSE_R1, buf); 1280 count, SD_BLOCK_SIZE, RESPONSE_R1, buf);
@@ -1292,19 +1290,18 @@ int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf)
1292 goto err; 1290 goto err;
1293 } 1291 }
1294 1292
1295 last_disk_activity = current_tick;
1296
1297 sd_simple_cmd(&request, SD_STOP_TRANSMISSION, 0, RESPONSE_R1B); 1293 sd_simple_cmd(&request, SD_STOP_TRANSMISSION, 0, RESPONSE_R1B);
1298 if ((retval = sd_unpack_r1(&request, &r1))) 1294 if ((retval = sd_unpack_r1(&request, &r1)))
1299 goto err; 1295 goto err;
1300 1296
1301err: 1297err:
1298 last_disk_activity = current_tick;
1302 sd_stop_transfer(); 1299 sd_stop_transfer();
1303 1300
1304 return retval; 1301 return retval;
1305} 1302}
1306 1303
1307int sd_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf) 1304int sd_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf)
1308{ 1305{
1309#ifdef HAVE_MULTIDRIVE 1306#ifdef HAVE_MULTIDRIVE
1310 (void)drive; 1307 (void)drive;
@@ -1330,7 +1327,8 @@ int sd_write_sectors(IF_MD(int drive,) unsigned long start, int count, const voi
1330 if ((retval = sd_unpack_r1(&request, &r1))) 1327 if ((retval = sd_unpack_r1(&request, &r1)))
1331 goto err; 1328 goto err;
1332 1329
1333 if (sd2_0) 1330 // XXX 64-bit
1331 if (card.sd2plus)
1334 { 1332 {
1335 sd_send_cmd(&request, SD_WRITE_MULTIPLE_BLOCK, start, 1333 sd_send_cmd(&request, SD_WRITE_MULTIPLE_BLOCK, start,
1336 count, SD_BLOCK_SIZE, RESPONSE_R1, 1334 count, SD_BLOCK_SIZE, RESPONSE_R1,
diff --git a/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4760.c b/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4760.c
index 1960fcbd35..3810852686 100644
--- a/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4760.c
+++ b/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4760.c
@@ -57,8 +57,6 @@ static struct semaphore sd_wakeup[NUM_DRIVES];
57 57
58static int use_4bit[NUM_DRIVES]; 58static int use_4bit[NUM_DRIVES];
59static int num_6[NUM_DRIVES]; 59static int num_6[NUM_DRIVES];
60static int sd2_0[NUM_DRIVES];
61
62 60
63//#define DEBUG(x...) logf(x) 61//#define DEBUG(x...) logf(x)
64#define DEBUG(x, ...) 62#define DEBUG(x, ...)
@@ -698,7 +696,7 @@ static inline unsigned int jz_sd_calc_clkrt(const int drive, unsigned int rate)
698 unsigned int clkrt = 0; 696 unsigned int clkrt = 0;
699 unsigned int clk_src = cpu_frequency / __cpm_get_mscdiv(); /* MSC_CLK */ 697 unsigned int clk_src = cpu_frequency / __cpm_get_mscdiv(); /* MSC_CLK */
700 698
701 if (!sd2_0[drive] && rate > SD_CLOCK_FAST) 699 if (!card[drive].sd2plus && rate > SD_CLOCK_FAST)
702 rate = SD_CLOCK_FAST; 700 rate = SD_CLOCK_FAST;
703 701
704 while (rate < clk_src) 702 while (rate < clk_src)
@@ -1192,7 +1190,6 @@ static int sd_init_card_state(const int drive, struct sd_request *request)
1192 (request->response[3+i*4]<< 8) | request->response[4+i*4]); 1190 (request->response[3+i*4]<< 8) | request->response[4+i*4]);
1193 1191
1194 sd_parse_csd(&card[drive]); 1192 sd_parse_csd(&card[drive]);
1195 sd2_0[drive] = (card_extract_bits(card[drive].csd, 127, 2) == 1);
1196 1193
1197 logf("CSD: %08lx%08lx%08lx%08lx", card[drive].csd[0], card[drive].csd[1], card[drive].csd[2], card[drive].csd[3]); 1194 logf("CSD: %08lx%08lx%08lx%08lx", card[drive].csd[0], card[drive].csd[1], card[drive].csd[2], card[drive].csd[3]);
1198 DEBUG("SD card is ready"); 1195 DEBUG("SD card is ready");
@@ -1261,7 +1258,7 @@ static int sd_select_card(const int drive)
1261 if (retval) 1258 if (retval)
1262 return retval; 1259 return retval;
1263 1260
1264 if (sd2_0[drive]) 1261 if (card[drive].sd2plus)
1265 { 1262 {
1266 retval = sd_read_switch(drive, &request); 1263 retval = sd_read_switch(drive, &request);
1267 if (!retval) 1264 if (!retval)
@@ -1292,7 +1289,6 @@ static int __sd_init_device(const int drive)
1292 /* Initialise card data as blank */ 1289 /* Initialise card data as blank */
1293 memset(&card[drive], 0, sizeof(tCardInfo)); 1290 memset(&card[drive], 0, sizeof(tCardInfo));
1294 1291
1295 sd2_0[drive] = 0;
1296 num_6[drive] = 0; 1292 num_6[drive] = 0;
1297 use_4bit[drive] = 0; 1293 use_4bit[drive] = 0;
1298 active[drive] = 0; 1294 active[drive] = 0;
@@ -1402,7 +1398,7 @@ static inline void sd_stop_transfer(const int drive)
1402 mutex_unlock(&sd_mtx[drive]); 1398 mutex_unlock(&sd_mtx[drive]);
1403} 1399}
1404 1400
1405int sd_transfer_sectors(IF_MD(const int drive,) unsigned long start, int count, void* buf, bool write) 1401int sd_transfer_sectors(IF_MD(const int drive,) sector_t start, int count, void* buf, bool write)
1406{ 1402{
1407 struct sd_request request; 1403 struct sd_request request;
1408 struct sd_response_r1 r1; 1404 struct sd_response_r1 r1;
@@ -1427,11 +1423,12 @@ int sd_transfer_sectors(IF_MD(const int drive,) unsigned long start, int count,
1427 if ((retval = sd_unpack_r1(&request, &r1))) 1423 if ((retval = sd_unpack_r1(&request, &r1)))
1428 goto err; 1424 goto err;
1429 1425
1426 // XXX 64-bit
1430 sd_send_cmd(drive, &request, 1427 sd_send_cmd(drive, &request,
1431 (count > 1) ? 1428 (count > 1) ?
1432 (write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK) : 1429 (write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK) :
1433 (write ? SD_WRITE_BLOCK : SD_READ_SINGLE_BLOCK), 1430 (write ? SD_WRITE_BLOCK : SD_READ_SINGLE_BLOCK),
1434 sd2_0[drive] ? start : (start * SD_BLOCK_SIZE), 1431 card[drive].sd2plus ? start : (start * SD_BLOCK_SIZE),
1435 count, SD_BLOCK_SIZE, RESPONSE_R1, buf); 1432 count, SD_BLOCK_SIZE, RESPONSE_R1, buf);
1436 if ((retval = sd_unpack_r1(&request, &r1))) 1433 if ((retval = sd_unpack_r1(&request, &r1)))
1437 goto err; 1434 goto err;
@@ -1451,12 +1448,12 @@ err:
1451 return retval; 1448 return retval;
1452} 1449}
1453 1450
1454int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf) 1451int sd_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf)
1455{ 1452{
1456 return sd_transfer_sectors(IF_MD(drive,) start, count, buf, false); 1453 return sd_transfer_sectors(IF_MD(drive,) start, count, buf, false);
1457} 1454}
1458 1455
1459int sd_write_sectors(IF_MD(int drive,) unsigned long start, int count, const void* buf) 1456int sd_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf)
1460{ 1457{
1461 return sd_transfer_sectors(IF_MD(drive,) start, count, (void*)buf, true); 1458 return sd_transfer_sectors(IF_MD(drive,) start, count, (void*)buf, true);
1462} 1459}
diff --git a/firmware/target/mips/ingenic_x1000/msc-x1000.c b/firmware/target/mips/ingenic_x1000/msc-x1000.c
index 62172fa213..f0d417e4f7 100644
--- a/firmware/target/mips/ingenic_x1000/msc-x1000.c
+++ b/firmware/target/mips/ingenic_x1000/msc-x1000.c
@@ -844,7 +844,7 @@ int msc_cmd_send_csd(msc_drv* d)
844 d->cardinfo.csd[i] = req.response[i]; 844 d->cardinfo.csd[i] = req.response[i];
845 sd_parse_csd(&d->cardinfo); 845 sd_parse_csd(&d->cardinfo);
846 846
847 if((req.response[0] >> 30) == 1) 847 if(d->cardinfo.sd2plus)
848 d->driver_flags |= MSC_DF_V2_CARD; 848 d->driver_flags |= MSC_DF_V2_CARD;
849 849
850 return 0; 850 return 0;
diff --git a/firmware/target/mips/ingenic_x1000/sd-x1000.c b/firmware/target/mips/ingenic_x1000/sd-x1000.c
index 4312937ee8..c1aec59aa3 100644
--- a/firmware/target/mips/ingenic_x1000/sd-x1000.c
+++ b/firmware/target/mips/ingenic_x1000/sd-x1000.c
@@ -51,7 +51,7 @@ static int sd_init_card(msc_drv* d)
51} 51}
52 52
53static int sd_transfer(msc_drv* d, bool write, 53static int sd_transfer(msc_drv* d, bool write,
54 unsigned long start, int count, void* buf) 54 sector_t start, int count, void* buf)
55{ 55{
56 int status = -1; 56 int status = -1;
57 57
@@ -114,6 +114,7 @@ static int sd_transfer(msc_drv* d, bool write,
114 : SD_READ_MULTIPLE_BLOCK; 114 : SD_READ_MULTIPLE_BLOCK;
115 } 115 }
116 116
117 // XXX 64-bit
117 if(d->driver_flags & MSC_DF_V2_CARD) 118 if(d->driver_flags & MSC_DF_V2_CARD)
118 req.argument = start; 119 req.argument = start;
119 else 120 else
@@ -142,14 +143,14 @@ static int sd_transfer(msc_drv* d, bool write,
142 return status; 143 return status;
143} 144}
144 145
145int sd_read_sectors(IF_MD(int drive,) unsigned long start, 146int sd_read_sectors(IF_MD(int drive,) sector_t start,
146 int count, void* buf) 147 int count, void* buf)
147{ 148{
148 return sd_transfer(sd_to_msc[IF_MD_DRV(drive)], false, 149 return sd_transfer(sd_to_msc[IF_MD_DRV(drive)], false,
149 start, count, buf); 150 start, count, buf);
150} 151}
151 152
152int sd_write_sectors(IF_MD(int drive,) unsigned long start, 153int sd_write_sectors(IF_MD(int drive,) sector_t start,
153 int count, const void* buf) 154 int count, const void* buf)
154{ 155{
155 return sd_transfer(sd_to_msc[IF_MD_DRV(drive)], true, 156 return sd_transfer(sd_to_msc[IF_MD_DRV(drive)], true,
diff --git a/firmware/usbstack/usb_storage.c b/firmware/usbstack/usb_storage.c
index 81d90d01db..e226c15f99 100644
--- a/firmware/usbstack/usb_storage.c
+++ b/firmware/usbstack/usb_storage.c
@@ -114,6 +114,9 @@
114#define SCSI_REPORT_LUNS 0xa0 114#define SCSI_REPORT_LUNS 0xa0
115#define SCSI_WRITE_BUFFER 0x3b 115#define SCSI_WRITE_BUFFER 0x3b
116 116
117#define SCSI_READ_16 0x88
118#define SCSI_WRITE_16 0x8a
119
117#define UMS_STATUS_GOOD 0x00 120#define UMS_STATUS_GOOD 0x00
118#define UMS_STATUS_FAIL 0x01 121#define UMS_STATUS_FAIL 0x01
119 122
@@ -273,7 +276,7 @@ static union {
273static char *cbw_buffer; 276static char *cbw_buffer;
274 277
275static struct { 278static struct {
276 unsigned int sector; 279 sector_t sector;
277 unsigned int count; 280 unsigned int count;
278 unsigned int orig_count; 281 unsigned int orig_count;
279 unsigned int cur_cmd; 282 unsigned int cur_cmd;
@@ -503,7 +506,7 @@ void usb_storage_transfer_complete(int ep,int dir,int status,int length)
503 if(dir==USB_DIR_IN) { 506 if(dir==USB_DIR_IN) {
504 logf("IN received in RECEIVING"); 507 logf("IN received in RECEIVING");
505 } 508 }
506 logf("scsi write %d %d", cur_cmd.sector, cur_cmd.count); 509 logf("scsi write %llu %d", cur_cmd.sector, cur_cmd.count);
507 if(status==0) { 510 if(status==0) {
508 if((unsigned int)length!=(SECTOR_SIZE* cur_cmd.count) 511 if((unsigned int)length!=(SECTOR_SIZE* cur_cmd.count)
509 && (unsigned int)length!=WRITE_BUFFER_SIZE) { 512 && (unsigned int)length!=WRITE_BUFFER_SIZE) {
@@ -511,7 +514,7 @@ void usb_storage_transfer_complete(int ep,int dir,int status,int length)
511 break; 514 break;
512 } 515 }
513 516
514 unsigned int next_sector = cur_cmd.sector + 517 sector_t next_sector = cur_cmd.sector +
515 (WRITE_BUFFER_SIZE/SECTOR_SIZE); 518 (WRITE_BUFFER_SIZE/SECTOR_SIZE);
516 unsigned int next_count = cur_cmd.count - 519 unsigned int next_count = cur_cmd.count -
517 MIN(cur_cmd.count,WRITE_BUFFER_SIZE/SECTOR_SIZE); 520 MIN(cur_cmd.count,WRITE_BUFFER_SIZE/SECTOR_SIZE);
@@ -739,12 +742,10 @@ static void send_and_read_next(void)
739 742
740static void handle_scsi(struct command_block_wrapper* cbw) 743static void handle_scsi(struct command_block_wrapper* cbw)
741{ 744{
742 /* USB Mass Storage assumes LBA capability.
743 TODO: support 48-bit LBA */
744
745 struct storage_info info; 745 struct storage_info info;
746 unsigned int length = cbw->data_transfer_length; 746 unsigned int length = cbw->data_transfer_length;
747 unsigned int block_size, block_count; 747 sector_t block_count;
748 unsigned int block_size;
748 bool lun_present=true; 749 bool lun_present=true;
749 unsigned char lun = cbw->lun; 750 unsigned char lun = cbw->lun;
750 unsigned int block_size_mult = 1; 751 unsigned int block_size_mult = 1;
@@ -888,6 +889,12 @@ static void handle_scsi(struct command_block_wrapper* cbw)
888 memset(tb.ms_data_10->block_descriptor.reserved,0,4); 889 memset(tb.ms_data_10->block_descriptor.reserved,0,4);
889 memset(tb.ms_data_10->block_descriptor.num_blocks,0,8); 890 memset(tb.ms_data_10->block_descriptor.num_blocks,0,8);
890 891
892#ifdef STORAGE_64BIT_SECTOR
893 tb.ms_data_10->block_descriptor.num_blocks[2] =
894 ((block_count/block_size_mult) & 0xff00000000ULL)>>40;
895 tb.ms_data_10->block_descriptor.num_blocks[3] =
896 ((block_count/block_size_mult) & 0x00ff000000ULL)>>32;
897#endif
891 tb.ms_data_10->block_descriptor.num_blocks[4] = 898 tb.ms_data_10->block_descriptor.num_blocks[4] =
892 ((block_count/block_size_mult) & 0xff000000)>>24; 899 ((block_count/block_size_mult) & 0xff000000)>>24;
893 tb.ms_data_10->block_descriptor.num_blocks[5] = 900 tb.ms_data_10->block_descriptor.num_blocks[5] =
@@ -1070,7 +1077,7 @@ static void handle_scsi(struct command_block_wrapper* cbw)
1070 cbw->command_block[8]); 1077 cbw->command_block[8]);
1071 cur_cmd.orig_count = cur_cmd.count; 1078 cur_cmd.orig_count = cur_cmd.count;
1072 1079
1073 //logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count); 1080 logf("scsi read %llu %d", cur_cmd.sector, cur_cmd.count);
1074 1081
1075 if((cur_cmd.sector + cur_cmd.count) > block_count) { 1082 if((cur_cmd.sector + cur_cmd.count) > block_count) {
1076 send_csw(UMS_STATUS_FAIL); 1083 send_csw(UMS_STATUS_FAIL);
@@ -1092,7 +1099,58 @@ static void handle_scsi(struct command_block_wrapper* cbw)
1092 send_and_read_next(); 1099 send_and_read_next();
1093 } 1100 }
1094 break; 1101 break;
1102#ifdef STORAGE_64BIT_SECTOR
1103 case SCSI_READ_16:
1104 logf("scsi read16 %d",lun);
1105 if(!lun_present) {
1106 send_command_failed_result();
1107 cur_sense_data.sense_key=SENSE_NOT_READY;
1108 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
1109 cur_sense_data.ascq=0;
1110 break;
1111 }
1112 cur_cmd.data[0] = tb.transfer_buffer;
1113 cur_cmd.data[1] = &tb.transfer_buffer[READ_BUFFER_SIZE];
1114 cur_cmd.data_select=0;
1115 cur_cmd.sector = block_size_mult *
1116 ((uint64_t)cbw->command_block[2] << 56 |
1117 (uint64_t)cbw->command_block[3] << 48 |
1118 (uint64_t)cbw->command_block[4] << 40 |
1119 (uint64_t)cbw->command_block[5] << 32 |
1120 cbw->command_block[6] << 24 |
1121 cbw->command_block[7] << 16 |
1122 cbw->command_block[8] << 8 |
1123 cbw->command_block[9]);
1124 cur_cmd.count = block_size_mult *
1125 (cbw->command_block[10] << 24 |
1126 cbw->command_block[11] << 16 |
1127 cbw->command_block[12] << 8 |
1128 cbw->command_block[13]);
1129 cur_cmd.orig_count = cur_cmd.count;
1095 1130
1131 logf("scsi read %llu %d", cur_cmd.sector, cur_cmd.count);
1132
1133 if((cur_cmd.sector + cur_cmd.count) > block_count) {
1134 send_csw(UMS_STATUS_FAIL);
1135 cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
1136 cur_sense_data.asc=ASC_LBA_OUT_OF_RANGE;
1137 cur_sense_data.ascq=0;
1138 }
1139 else {
1140#ifdef USB_USE_RAMDISK
1141 memcpy(cur_cmd.data[cur_cmd.data_select],
1142 ramdisk_buffer + cur_cmd.sector*SECTOR_SIZE,
1143 MIN(READ_BUFFER_SIZE/SECTOR_SIZE,cur_cmd.count)*SECTOR_SIZE);
1144#else
1145 cur_cmd.last_result = storage_read_sectors(IF_MD(cur_cmd.lun,)
1146 cur_cmd.sector,
1147 MIN(READ_BUFFER_SIZE/SECTOR_SIZE, cur_cmd.count),
1148 cur_cmd.data[cur_cmd.data_select]);
1149#endif
1150 send_and_read_next();
1151 }
1152 break;
1153#endif
1096 case SCSI_WRITE_10: 1154 case SCSI_WRITE_10:
1097 logf("scsi write10 %d",lun); 1155 logf("scsi write10 %d",lun);
1098 if(!lun_present) { 1156 if(!lun_present) {
@@ -1127,6 +1185,48 @@ static void handle_scsi(struct command_block_wrapper* cbw)
1127 MIN(WRITE_BUFFER_SIZE, cur_cmd.count*SECTOR_SIZE)); 1185 MIN(WRITE_BUFFER_SIZE, cur_cmd.count*SECTOR_SIZE));
1128 } 1186 }
1129 break; 1187 break;
1188#ifdef STORAGE_64BIT_SECTOR
1189 case SCSI_WRITE_16:
1190 logf("scsi write16 %d",lun);
1191 if(!lun_present) {
1192 send_csw(UMS_STATUS_FAIL);
1193 cur_sense_data.sense_key=SENSE_NOT_READY;
1194 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
1195 cur_sense_data.ascq=0;
1196 break;
1197 }
1198 cur_cmd.data[0] = tb.transfer_buffer;
1199 cur_cmd.data[1] = &tb.transfer_buffer[WRITE_BUFFER_SIZE];
1200 cur_cmd.data_select=0;
1201 cur_cmd.sector = block_size_mult *
1202 ((uint64_t)cbw->command_block[2] << 56 |
1203 (uint64_t)cbw->command_block[3] << 48 |
1204 (uint64_t)cbw->command_block[4] << 40 |
1205 (uint64_t)cbw->command_block[5] << 32 |
1206 cbw->command_block[6] << 24 |
1207 cbw->command_block[7] << 16 |
1208 cbw->command_block[8] << 8 |
1209 cbw->command_block[9]);
1210 cur_cmd.count = block_size_mult *
1211 (cbw->command_block[10] << 24 |
1212 cbw->command_block[11] << 16 |
1213 cbw->command_block[12] << 8 |
1214 cbw->command_block[13]);
1215 cur_cmd.orig_count = cur_cmd.count;
1216
1217 /* expect data */
1218 if((cur_cmd.sector + cur_cmd.count) > block_count) {
1219 send_csw(UMS_STATUS_FAIL);
1220 cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
1221 cur_sense_data.asc=ASC_LBA_OUT_OF_RANGE;
1222 cur_sense_data.ascq=0;
1223 }
1224 else {
1225 receive_block_data(cur_cmd.data[0],
1226 MIN(WRITE_BUFFER_SIZE, cur_cmd.count*SECTOR_SIZE));
1227 }
1228 break;
1229#endif
1130 1230
1131#if CONFIG_RTC 1231#if CONFIG_RTC
1132 case SCSI_WRITE_BUFFER: 1232 case SCSI_WRITE_BUFFER: