3131#include <string.h>
3232#include <stdbool.h>
3333
34- #include < target.h>
34+ #include " target.h"
3535
3636#include "mpfs250.h"
3737#include "image.h"
4242#include "printf.h"
4343#include "loader.h"
4444#include "disk.h"
45+ #include "gpt.h"
4546
4647#define DEBUG_MMC
4748
@@ -200,6 +201,7 @@ int mmc_set_timeout(uint32_t timeout_us)
200201 return 0 ;
201202}
202203
204+ /* TODO: Fix with real timer */
203205void mmc_delay (uint32_t delay )
204206{
205207 while (delay -- ) {
@@ -298,7 +300,7 @@ uint32_t mmc_set_clock(uint32_t clock_khz)
298300 last_clock_khz = clock_khz ;
299301
300302#ifdef DEBUG_MMC
301- wolfBoot_printf ("mmc_set_clock: clock_khz : %d, freq_khz : %d\n" ,
303+ wolfBoot_printf ("mmc_set_clock: requested khz : %d, actual khz : %d\n" ,
302304 clock_khz , freq_khz );
303305#endif
304306
@@ -453,8 +455,8 @@ int mmc_card_init(uint32_t acmd41_arg, uint32_t *ocr_reg)
453455 return status ;
454456}
455457
456- /* MMC_CMD17_READ_SINGLE */
457- int mmc_block_read (uint32_t cmd_index , uint32_t block_addr , uint32_t * dst ,
458+ /* MMC_CMD17_READ_SINGLE, MMC_CMD18_READ_MULTIPLE */
459+ int mmc_read (uint32_t cmd_index , uint32_t block_addr , uint32_t * dst ,
458460 uint32_t sz )
459461{
460462 int status ;
@@ -483,7 +485,7 @@ int mmc_block_read(uint32_t cmd_index, uint32_t block_addr, uint32_t* dst,
483485 }
484486
485487#ifdef DEBUG_MMC
486- wolfBoot_printf ("mmc_block_read : cmd_index: %d, block_addr: %08X, dst %p, sz: %d\n" ,
488+ wolfBoot_printf ("mmc_read : cmd_index: %d, block_addr: %08X, dst %p, sz: %d\n" ,
487489 cmd_index , block_addr , dst , sz );
488490#endif
489491
@@ -513,12 +515,13 @@ int mmc_block_read(uint32_t cmd_index, uint32_t block_addr, uint32_t* dst,
513515 status = mmc_wait_busy (0 );
514516 }
515517 else {
516- #ifdef DEBUG_MMC
517- wolfBoot_printf ("mmc_block_read: error: 0x%08X\n" , reg );
518- #endif
519- status = -1 ;
518+ status = -1 ; /* error */
520519 }
521520
521+ #ifdef DEBUG_MMC
522+ wolfBoot_printf ("mmc_read: status: %d\n" , status );
523+ #endif
524+
522525 return status ;
523526}
524527
@@ -567,10 +570,13 @@ static uint32_t get_srs_bits(int from, int count)
567570 return ret & mask ;
568571}
569572
570- int mmc_send_switch_function (uint32_t mode , uint32_t function_number , uint32_t group_number )
573+ /* check or set switch function/group:
574+ * returns 0 if supported */
575+ int mmc_send_switch_function (uint32_t mode , uint32_t function_number ,
576+ uint32_t group_number )
571577{
572578 int status ;
573- uint32_t timeout = 4 ; /* up to 5 tries */
579+ uint32_t timeout = 4 ;
574580 uint32_t cmd_arg ;
575581 uint32_t func_status [64 /sizeof (uint32_t )]; /* fixed 512 bits */
576582 uint8_t * p_func_status = (uint8_t * )func_status ;
@@ -582,7 +588,7 @@ int mmc_send_switch_function(uint32_t mode, uint32_t function_number, uint32_t g
582588 cmd_arg = (function_number << ((group_number - 1 ) * 4 ));
583589 do {
584590 /* first run check to see if function is supported */
585- status = mmc_block_read (SD_CMD_6_SWITCH_FUNC ,
591+ status = mmc_read (SD_CMD_6_SWITCH_FUNC ,
586592 (mode | cmd_arg ),
587593 func_status , sizeof (func_status ));
588594 if (status == 0 ) {
@@ -600,11 +606,12 @@ int mmc_send_switch_function(uint32_t mode, uint32_t function_number, uint32_t g
600606 /* supported: group 1 415:400 */
601607 if ((p_func_status [13 -
602608 ((group_number - 1 )* 2 )] & (1 << function_number ))) {
603- break ; /* supported */
609+ status = 0 ; /* supported */
604610 }
605611 else {
606- return -1 ; /* not supported */
612+ status = -1 ; /* not supported */
607613 }
614+ break ;
608615 }
609616 } while (status == 0 && -- timeout > 0 ); /* retry until function not busy */
610617 return status ;
@@ -848,7 +855,7 @@ int mmc_init(void)
848855 if (status == 0 ) {
849856 /* Get SCR registers - 8 bytes */
850857 uint32_t scr_reg [SCR_REG_DATA_SIZE /sizeof (uint32_t )];
851- status = mmc_block_read (SD_ACMD51_SEND_SCR , 0 , scr_reg ,
858+ status = mmc_read (SD_ACMD51_SEND_SCR , 0 , scr_reg ,
852859 sizeof (scr_reg ));
853860 }
854861 if (status == 0 ) {
@@ -882,37 +889,58 @@ int mmc_init(void)
882889 return status ;
883890}
884891
885- /* TODO: Add support for reading uSD card with GPT (Global Partition Table) */
886- /* The partition ID's are determined using BOOT_PART_A and BOOT_PART_B. */
887- int disk_open (int drv )
892+ /* returns number of bytes read on success or negative on error */
893+ int disk_read (int drv , uint64_t start , uint32_t count , uint32_t * buf )
888894{
889- wolfBoot_printf ("disk_open: drv = %d\r\n" , drv );
890- (void )drv ;
891- return mmc_init ();
895+ int status = 0 ;
896+ uint32_t read_sz ;
897+ uint8_t * p_buf = (uint8_t * )buf ;
898+ (void )drv ; /* only one drive supported */
899+
900+ #ifdef DEBUG_MMC
901+ wolfBoot_printf ("disk_read: drv:%d, start:%llu, count:%d, dst:%p\n" ,
902+ drv , start , count , buf );
903+ #endif
904+
905+ while (count > 0 ) {
906+ read_sz = count ;
907+ if (read_sz > EMMC_SD_BLOCK_SIZE ) {
908+ read_sz = EMMC_SD_BLOCK_SIZE ;
909+ }
910+ status = mmc_read (MMC_CMD17_READ_SINGLE , start , (uint32_t * )p_buf , read_sz );
911+ if (status != 0 ) {
912+ return -1 ;
913+ }
914+ start += read_sz ;
915+ p_buf += read_sz ;
916+ count -= read_sz ;
917+ }
918+ return status ;
892919}
893- int disk_read (int drv , int part , uint64_t off , uint64_t sz , uint8_t * buf )
920+
921+ int disk_write (int drv , uint64_t start , uint32_t count , const uint32_t * buf )
894922{
923+ /* not supported */
895924 (void )drv ;
896- (void )part ;
897- (void )off ;
898- (void )sz ;
925+ (void )start ;
926+ (void )count ;
899927 (void )buf ;
900928 return 0 ;
901929}
902- int disk_write (int drv , int part , uint64_t off , uint64_t sz , const uint8_t * buf )
930+
931+ int disk_init (int drv )
903932{
933+ int r = mmc_init ();
934+ if (r != 0 ) {
935+ wolfBoot_printf ("Failed to initialize MMC\n" );
936+ }
904937 (void )drv ;
905- (void )part ;
906- (void )off ;
907- (void )sz ;
908- (void )buf ;
909- return 0 ;
938+ return r ;
910939}
911- int disk_find_partition_by_label (int drv , const char * label )
940+
941+ void disk_close (int drv )
912942{
913943 (void )drv ;
914- (void )label ;
915- return 0 ;
916944}
917945
918946#ifdef DEBUG_UART
0 commit comments