disk_access_spinand.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/spinand.h>
  31. #include <partition/partition.h>
  32. #define CONFIG_SPINAND_DEV_NAME "spinand"
  33. const struct device *spinand_disk;
  34. //static u32_t spinand_disk_sector_cnt;
  35. int spinand_disk_status(struct disk_info *disk)
  36. {
  37. if (!spinand_disk) {
  38. return DISK_STATUS_NOMEDIA;
  39. }
  40. return DISK_STATUS_OK;
  41. }
  42. int spinand_disk_initialize(struct disk_info *disk)
  43. {
  44. uint32_t sector_cnt;
  45. int ret;
  46. if (spinand_disk) {
  47. return 0;
  48. }
  49. spinand_disk = device_get_binding(CONFIG_SPINAND_DEV_NAME);
  50. if (!spinand_disk) {
  51. return -ENODEV;
  52. }
  53. ret = spinand_storage_ioctl((struct device *)spinand_disk, DISK_IOCTL_GET_SECTOR_COUNT, (void *)&sector_cnt);
  54. if (!ret) {
  55. //sd_disk_sector_cnt = sector_cnt;
  56. if(disk->sector_cnt){
  57. if(disk->sector_offset + disk->sector_cnt > sector_cnt)
  58. printk("error:nand disk part over max capcity = %d\n", sector_cnt);
  59. }else{
  60. disk->sector_cnt = sector_cnt;
  61. }
  62. //spinand_disk_sector_cnt = sector_cnt;
  63. } else {
  64. printk("failed to get sector count error=%d\n", ret);
  65. return -EFAULT;
  66. }
  67. return 0;
  68. }
  69. int spinand_disk_read(struct disk_info *disk, uint8_t *buff, uint32_t start_sector,
  70. uint32_t sector_count)
  71. {
  72. //boot_sector is reserved for mbrec and param.
  73. //int boot_sector = (2048 * 64 * 4) >> 9;
  74. if (!spinand_disk) {
  75. return -EIO;
  76. }
  77. //if (spinand_disk_sector_cnt == 0) {
  78. //goto read;
  79. //}
  80. if (start_sector > disk->sector_cnt - 1) {
  81. return -EIO;
  82. }
  83. //if (start_sector + sector_count > spinand_disk_sector_cnt) {
  84. //sector_count = spinand_disk_sector_cnt - start_sector;
  85. //}
  86. if (start_sector + sector_count > disk->sector_cnt) {
  87. sector_count = disk->sector_cnt - start_sector;
  88. }
  89. start_sector += disk->sector_offset;
  90. //read:
  91. //start_sector += boot_sector;
  92. if (flash_read(spinand_disk, start_sector<<9, buff, sector_count<<9) != 0) {
  93. return -EIO;
  94. }
  95. return 0;
  96. }
  97. int spinand_disk_write(struct disk_info *disk, const uint8_t *buff, uint32_t start_sector,
  98. uint32_t sector_count)
  99. {
  100. //boot_sector is reserved for mbrec and param.
  101. //int boot_sector = (2048 * 64 * 4) >> 9;
  102. if (!spinand_disk) {
  103. return -EIO;
  104. }
  105. //if (spinand_disk_sector_cnt == 0) {
  106. //goto write;
  107. //}
  108. if (start_sector > disk->sector_cnt - 1) {
  109. return -EIO;
  110. }
  111. //if (start_sector + sector_count > spinand_disk_sector_cnt) {
  112. //sector_count = spinand_disk_sector_cnt - start_sector;
  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. //write:
  119. // start_sector += boot_sector;
  120. if (flash_write(spinand_disk, start_sector<<9, buff, sector_count<<9) != 0) {
  121. return -EIO;
  122. }
  123. return 0;
  124. }
  125. int spinand_disk_ioctl(struct disk_info *disk, uint8_t cmd, void *buff)
  126. {
  127. int ret = 0;
  128. if (!spinand_disk) {
  129. return -ENODEV;
  130. }
  131. switch (cmd) {
  132. case DISK_IOCTL_CTRL_SYNC:
  133. //todo
  134. break;
  135. case DISK_IOCTL_GET_SECTOR_COUNT:
  136. if (disk->sector_cnt > 0) {
  137. *(uint32_t *)buff = disk->sector_cnt;
  138. } else {
  139. ret = spinand_storage_ioctl((struct device *)spinand_disk, cmd, buff);
  140. }
  141. break;
  142. case DISK_IOCTL_GET_SECTOR_SIZE:
  143. case DISK_IOCTL_GET_ERASE_BLOCK_SZ:
  144. case DISK_IOCTL_HW_DETECT:
  145. ret = spinand_storage_ioctl((struct device *)spinand_disk, cmd, buff);
  146. break;
  147. default:
  148. return -EINVAL;
  149. }
  150. return ret;
  151. }
  152. struct disk_operations disk_spinand_operation = {
  153. .init = spinand_disk_initialize,
  154. .status = spinand_disk_status,
  155. .read = spinand_disk_read,
  156. .write = spinand_disk_write,
  157. .ioctl = spinand_disk_ioctl,
  158. };
  159. struct disk_info disk_spinand_mass = {
  160. .name = "NAND",
  161. .ops = &disk_spinand_operation,
  162. .sector_size = 512,
  163. .sector_offset = 0,
  164. .sector_cnt = 0,
  165. };
  166. static int spinand_disk_init(const struct device *dev)
  167. {
  168. const struct partition_entry *parti;
  169. ARG_UNUSED(dev);
  170. parti = partition_get_stf_part(STORAGE_ID_NAND, PARTITION_FILE_ID_UDISK);
  171. if(parti != NULL){
  172. disk_spinand_mass.sector_offset = parti->offset >> 9;
  173. disk_spinand_mass.sector_cnt = parti->size >> 9;
  174. }
  175. printk("spinand_disk_init,size=%d, off=%d secotr\n", disk_spinand_mass.sector_cnt, disk_spinand_mass.sector_offset);
  176. disk_access_register(&disk_spinand_mass);
  177. return 0;
  178. }
  179. SYS_INIT(spinand_disk_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);