disk_access_sd.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2016 Actions Corporation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * Copyright (c) 2016 Intel Corporation.
  18. *
  19. * SPDX-License-Identifier: Apache-2.0
  20. */
  21. #include <string.h>
  22. #include <zephyr/types.h>
  23. #include <sys/__assert.h>
  24. #include <sys/util.h>
  25. #include <disk/disk_access.h>
  26. #include <errno.h>
  27. #include <init.h>
  28. #include <device.h>
  29. #include <drivers/flash.h>
  30. #include <drivers/mmc/sd.h>
  31. #include <partition/partition.h>
  32. #define CONFIG_MMC_SDCARD_DEV_NAME "sd"
  33. const struct device *sd_disk;
  34. //static u32_t sd_disk_sector_cnt;
  35. static int sd_disk_status(struct disk_info *disk)
  36. {
  37. if (!sd_disk) {
  38. return DISK_STATUS_NOMEDIA;
  39. }
  40. return DISK_STATUS_OK;
  41. }
  42. static int sd_disk_initialize(struct disk_info *disk)
  43. {
  44. u32_t sector_cnt;
  45. int ret;
  46. if (sd_disk) {
  47. return 0;
  48. }
  49. sd_disk = device_get_binding(CONFIG_MMC_SDCARD_DEV_NAME);
  50. if (!sd_disk) {
  51. return -ENODEV;
  52. }
  53. if (0 != sd_card_storage_init(sd_disk)) {
  54. sd_disk = NULL;
  55. return -ENODEV;
  56. }
  57. ret = sd_card_storage_ioctl(sd_disk, DISK_IOCTL_GET_SECTOR_COUNT, (void *)&sector_cnt);
  58. if (!ret) {
  59. //sd_disk_sector_cnt = sector_cnt;
  60. if(disk->sector_cnt){
  61. if(disk->sector_offset + disk->sector_cnt > sector_cnt)
  62. printk("error:sd disk part over sd max capcity = %d\n", sector_cnt);
  63. }else{
  64. disk->sector_cnt = sector_cnt;
  65. }
  66. } else {
  67. printk("failed to get sector count error=%d\n", ret);
  68. sd_disk = NULL;
  69. return -EFAULT;
  70. }
  71. return 0;
  72. }
  73. static int sd_disk_read(struct disk_info *disk, uint8_t *buff, uint32_t start_sector,
  74. uint32_t sector_count)
  75. {
  76. uint32_t start_addr;
  77. uint32_t len;
  78. if (!sd_disk) {
  79. return -ENODEV;
  80. }
  81. //if (sd_disk_sector_cnt == 0) {
  82. //goto read;
  83. //}
  84. if ((start_sector >= disk->sector_cnt) || (sector_count == 0)){
  85. return -EIO;
  86. }
  87. if (start_sector + sector_count > disk->sector_cnt) {
  88. sector_count = disk->sector_cnt - start_sector;
  89. }
  90. start_sector += disk->sector_offset;
  91. start_addr = start_sector << 9;
  92. len = sector_count << 9;
  93. //read:
  94. /* is it OK? */
  95. if (flash_read(sd_disk, start_addr, buff, len) != 0) {
  96. return -EIO;
  97. }
  98. return 0;
  99. }
  100. static int sd_disk_write(struct disk_info *disk, const uint8_t *buff, uint32_t start_sector,
  101. uint32_t sector_count)
  102. {
  103. uint32_t start_addr;
  104. uint32_t len;
  105. if (!sd_disk) {
  106. return -ENODEV;
  107. }
  108. //if (sd_disk_sector_cnt == 0) {
  109. //goto write;
  110. //}
  111. if ((start_sector >= disk->sector_cnt) || (sector_count == 0)){
  112. return -EIO;
  113. }
  114. if (start_sector + sector_count > disk->sector_cnt) {
  115. sector_count = disk->sector_cnt - start_sector;
  116. }
  117. start_sector += disk->sector_offset;
  118. start_addr = start_sector << 9;
  119. len = sector_count << 9;
  120. //write:
  121. if (flash_write(sd_disk, start_addr, buff, len) != 0) {
  122. return -EIO;
  123. }
  124. return 0;
  125. }
  126. static int sd_disk_ioctl(struct disk_info *disk, uint8_t cmd, void *buff)
  127. {
  128. int ret = 0;
  129. const struct device *tmp_disk;
  130. if (!sd_disk && (cmd != DISK_IOCTL_HW_DETECT)) {
  131. return -ENODEV;
  132. }
  133. switch (cmd) {
  134. case DISK_IOCTL_CTRL_SYNC:
  135. break;
  136. case DISK_IOCTL_GET_SECTOR_COUNT:
  137. if (disk->sector_cnt > 0) {
  138. *(uint32_t *)buff = disk->sector_cnt;
  139. } else {
  140. ret = sd_card_storage_ioctl(sd_disk, cmd, buff);
  141. }
  142. break;
  143. case DISK_IOCTL_GET_SECTOR_SIZE:
  144. case DISK_IOCTL_GET_ERASE_BLOCK_SZ:
  145. //case DISK_IOCTL_GET_DISK_SIZE:
  146. ret = sd_card_storage_ioctl(sd_disk, cmd, buff);
  147. break;
  148. case DISK_IOCTL_HW_DETECT:
  149. if (sd_disk) {
  150. ret = sd_card_storage_ioctl(sd_disk, cmd, buff);
  151. } else {
  152. tmp_disk = device_get_binding(CONFIG_MMC_SDCARD_DEV_NAME);
  153. if (tmp_disk) {
  154. ret = sd_card_storage_ioctl(tmp_disk, cmd, buff);
  155. } else {
  156. return -ENODEV;
  157. }
  158. }
  159. if (ret == 0) {
  160. if (*(uint8_t *)buff != STA_DISK_OK) {
  161. sd_disk = NULL;
  162. }
  163. }
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. return ret;
  169. }
  170. struct disk_operations disk_sd_operation = {
  171. .init = sd_disk_initialize,
  172. .status = sd_disk_status,
  173. .read = sd_disk_read,
  174. .write = sd_disk_write,
  175. .ioctl = sd_disk_ioctl,
  176. };
  177. struct disk_info disk_sd_mass = {
  178. .name = "SD",
  179. .sector_size = 512,
  180. .sector_offset = 0,
  181. .sector_cnt = 0,
  182. .ops = &disk_sd_operation,
  183. };
  184. static int sd_disk_init(const struct device *dev)
  185. {
  186. const struct partition_entry *parti;
  187. ARG_UNUSED(dev);
  188. parti = partition_get_stf_part(STORAGE_ID_SD, PARTITION_FILE_ID_UDISK);
  189. if(parti != NULL){
  190. disk_sd_mass.sector_offset = parti->offset >> 9;
  191. disk_sd_mass.sector_cnt = parti->size >> 9;
  192. }
  193. printk("sd_disk_init,size=%d, off=%d secotr\n", disk_sd_mass.sector_cnt, disk_sd_mass.sector_offset);
  194. disk_access_register(&disk_sd_mass);
  195. return 0;
  196. }
  197. SYS_INIT(sd_disk_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);