sdio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "comdef.h"
  2. #include "sys.h"
  3. #include "ezGPIO_fullMux_ctrl_macro.h"
  4. #include "dwmmc.h"
  5. #include "gpt.h"
  6. struct dwmci_host local_host0;
  7. struct mmc local_mmc0;
  8. #define DWMMC_BUS_FREQ 100000000
  9. #define DWMMC_MAX_FREQ 10000000
  10. #define DWMMC_MIN_FREQ 400000
  11. #define GPT_BLOCK_SIZE 512
  12. static void sdio0_gpio_mux_select()
  13. {
  14. #if 0//for fpga
  15. SET_GPIO_18_dout_sdio0_pad_cclk_out;
  16. //SET_GPIO_18_doen_sdio0_pad_cclk_out;
  17. SET_GPIO_18_doen_LOW;
  18. //_SET_SYSCON_REG_register9_SCFG_gpio_pad_ctrl_9(5);
  19. SET_GPIO_17_doen_reverse_(1);
  20. SET_GPIO_17_doen_sdio0_pad_ccmd_oe;
  21. SET_GPIO_17_dout_sdio0_pad_ccmd_out;
  22. SET_GPIO_sdio0_pad_ccmd_in(17);
  23. SET_GPIO_15_doen_reverse_(1);
  24. SET_GPIO_16_doen_reverse_(1);
  25. SET_GPIO_19_doen_reverse_(1);
  26. SET_GPIO_20_doen_reverse_(1);
  27. SET_GPIO_19_doen_sdio0_pad_cdata_oe_bit0;
  28. SET_GPIO_19_dout_sdio0_pad_cdata_out_bit0;
  29. SET_GPIO_sdio0_pad_cdata_in_bit0(19);
  30. // _SET_SYSCON_REG_register9_SCFG_gpio_pad_ctrl_9(5);
  31. SET_GPIO_20_doen_sdio0_pad_cdata_oe_bit1;
  32. SET_GPIO_20_dout_sdio0_pad_cdata_out_bit1;
  33. SET_GPIO_sdio0_pad_cdata_in_bit1(20);
  34. //_SET_SYSCON_REG_register10_SCFG_gpio_pad_ctrl_10(5);
  35. SET_GPIO_15_doen_sdio0_pad_cdata_oe_bit2;
  36. SET_GPIO_15_dout_sdio0_pad_cdata_out_bit2;
  37. SET_GPIO_sdio0_pad_cdata_in_bit2(15);
  38. SET_GPIO_16_doen_sdio0_pad_cdata_oe_bit3;
  39. SET_GPIO_16_dout_sdio0_pad_cdata_out_bit3;
  40. SET_GPIO_sdio0_pad_cdata_in_bit3(16);
  41. #endif
  42. SET_GPIO_sdio0_pad_card_detect_n(26);
  43. SET_GPIO_26_doen_HIGH;
  44. SET_GPIO_33_dout_sdio0_pad_cclk_out;
  45. SET_GPIO_33_doen_LOW;
  46. SET_GPIO_34_doen_reverse_(1);
  47. SET_GPIO_34_doen_sdio0_pad_ccmd_oe;
  48. SET_GPIO_34_dout_sdio0_pad_ccmd_out;
  49. SET_GPIO_sdio0_pad_ccmd_in(34);
  50. SET_GPIO_32_doen_reverse_(1);
  51. SET_GPIO_31_doen_reverse_(1);
  52. SET_GPIO_30_doen_reverse_(1);
  53. SET_GPIO_36_doen_reverse_(1);
  54. SET_GPIO_32_doen_sdio0_pad_cdata_oe_bit0;
  55. SET_GPIO_32_dout_sdio0_pad_cdata_out_bit0;
  56. SET_GPIO_sdio0_pad_cdata_in_bit0(32);
  57. SET_GPIO_31_doen_sdio0_pad_cdata_oe_bit1;
  58. SET_GPIO_31_dout_sdio0_pad_cdata_out_bit1;
  59. SET_GPIO_sdio0_pad_cdata_in_bit1(31);
  60. SET_GPIO_30_doen_sdio0_pad_cdata_oe_bit2;
  61. SET_GPIO_30_dout_sdio0_pad_cdata_out_bit2;
  62. SET_GPIO_sdio0_pad_cdata_in_bit2(30);
  63. SET_GPIO_36_doen_sdio0_pad_cdata_oe_bit3;
  64. SET_GPIO_36_dout_sdio0_pad_cdata_out_bit3;
  65. SET_GPIO_sdio0_pad_cdata_in_bit3(36);
  66. }
  67. static struct mmc *init_mmc_device(int dev, unsigned int force_init)
  68. {
  69. struct mmc *mmc;
  70. if(dev != 0)
  71. return NULL;
  72. mmc = &local_mmc0;
  73. if (force_init)
  74. mmc->has_init = 0;
  75. if (mmc_init(mmc))
  76. return NULL;
  77. return mmc;
  78. }
  79. int boot_sdio_init(void)
  80. {
  81. struct dwmci_host *host;
  82. u32 fifo_depth = 32;
  83. /*HOST 0*/
  84. host = &local_host0;
  85. host->ioaddr = (void *)SDIO0_BASE_ADDR;
  86. host->name = "VIC DWMMC0";
  87. host->dev_index = 0;
  88. sdio0_gpio_mux_select();
  89. host->fifoth_val = MSIZE(0x2) |
  90. RX_WMARK(fifo_depth / 2 - 1) | TX_WMARK(fifo_depth / 2);
  91. host->buswidth = 4;
  92. host->bus_hz = DWMMC_BUS_FREQ;
  93. /* Add the mmc channel to be registered with mmc core */
  94. if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ, 0)) {
  95. printk("DWMMC0 registration failed\n");
  96. return -1;
  97. }
  98. if(init_mmc_device(0, 0) == NULL)
  99. {
  100. printk("init dev fail.");
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. static int sd_read_data(int dev_num,uint32_t start_lba, size_t blkcnt,void* dst)
  106. {
  107. struct mmc *mmc=&local_mmc0;
  108. int size;
  109. size = mmc->block_dev.block_read(dev_num,start_lba,blkcnt,dst);
  110. if(size != blkcnt)
  111. {
  112. printk("sd_read_data size:%d blkcnt:%d\n",size,blkcnt);
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. static gpt_partition_range find_sd_gpt_partition(
  118. uint64_t partition_entries_lba,
  119. uint32_t num_partition_entries,
  120. uint32_t partition_entry_size,
  121. const gpt_guid* partition_type_guid,
  122. void* block_buf // Used to temporarily load blocks of SD card
  123. )
  124. {
  125. // Exclusive end
  126. uint16_t partition_entries_lba_end = (
  127. partition_entries_lba +
  128. (num_partition_entries * partition_entry_size + GPT_BLOCK_SIZE - 1) / GPT_BLOCK_SIZE
  129. );
  130. for (uint16_t i = partition_entries_lba; i < partition_entries_lba_end; i++)
  131. {
  132. sd_read_data(0, i, 1,block_buf);
  133. gpt_partition_range range = gpt_find_partition_by_guid(block_buf, partition_type_guid, GPT_BLOCK_SIZE / partition_entry_size);
  134. if (gpt_is_valid_partition_range(range))
  135. return range;
  136. }
  137. return gpt_invalid_partition_range();
  138. }
  139. /*read data from sd to dst*/
  140. int boot_load_gpt_partition(void* dst, const gpt_guid* partition_type_guid)
  141. {
  142. uint8_t gpt_buf[GPT_BLOCK_SIZE];
  143. gpt_partition_range part_range;
  144. /*READ LBA1*/
  145. sd_read_data(0,1,1,gpt_buf);
  146. {
  147. // header will be overwritten by find_sd_gpt_partition(), so locally
  148. // scope it.
  149. gpt_header* header = (gpt_header*) gpt_buf;
  150. part_range = find_sd_gpt_partition(
  151. header->partition_entries_lba,
  152. header->num_partition_entries,
  153. header->partition_entry_size,
  154. partition_type_guid,
  155. gpt_buf
  156. );
  157. }
  158. printk("gpt_partition %d:%d\n",part_range.first_lba,part_range.last_lba);
  159. sd_read_data(0,part_range.first_lba,part_range.last_lba + 1 - part_range.first_lba,dst);
  160. }