osbdev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
  2. Copyright (c) 2014-2017 Datalight, Inc.
  3. All Rights Reserved Worldwide.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; use version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. */
  15. /* Businesses and individuals that for commercial or other reasons cannot
  16. comply with the terms of the GPLv2 license may obtain a commercial license
  17. before incorporating Reliance Edge into proprietary software for
  18. distribution in any form. Visit http://www.datalight.com/reliance-edge for
  19. more information.
  20. */
  21. /** @file
  22. @brief Implements block device I/O.
  23. */
  24. #include "hal.h"
  25. #if (HAL_USE_SDMMC == TRUE)
  26. #include "sama_sdmmc_lld.h"
  27. #if SDMMC_USE_RELEDGE_LIB == 1
  28. #include "sama_sdmmc_lld.h"
  29. #include "ch_sdmmc_device.h"
  30. #include "ch_sdmmc_cmds.h"
  31. #include "ch_sdmmc_sdio.h"
  32. #include "ch_sdmmc_sd.h"
  33. #include "ch_sdmmc_mmc.h"
  34. #include "ch_sdmmc_reledge.h"
  35. #include <redfs.h>
  36. #include <redvolume.h>
  37. #include <redosdeviations.h>
  38. #if REDCONF_API_POSIX == 0
  39. #error "REDCONF_API_POSIX should be 1"
  40. #endif
  41. #if REDCONF_API_FSE == 1
  42. #error "REDCONF_API_FSE not supported, should be 0"
  43. #endif
  44. /* sd_mmc_mem_2_ram_multi() and sd_mmc_ram_2_mem_multi() use an unsigned
  45. 16-bit value to specify the sector count, so no transfer can be larger
  46. than UINT16_MAX sectors.
  47. */
  48. #define MAX_SECTOR_TRANSFER UINT16_MAX
  49. /** @brief Initialize a disk.
  50. @param bVolNum The volume number of the volume whose block device is being
  51. initialized.
  52. @param mode The open mode, indicating the type of access required.
  53. @return A negated ::REDSTATUS code indicating the operation result.
  54. @retval 0 Operation was successful.
  55. @retval -RED_EIO A disk I/O error occurred.
  56. @retval -RED_EROFS The device is read-only media and write access was
  57. requested.
  58. */
  59. static REDSTATUS DiskOpen(
  60. uint8_t bVolNum,
  61. BDEVOPENMODE mode)
  62. {
  63. REDSTATUS ret = 0;
  64. uint32_t ulTries;
  65. eSDMMC_RC cs;
  66. SdmmcDriver *sdmmcp = NULL;
  67. if (!sdmmcGetInstance(bVolNum, &sdmmcp))
  68. return RED_EINVAL;
  69. /* Note: Assuming the volume number is the same as the SD card slot. The
  70. ASF SD/MMC driver supports two SD slots. This implementation will need
  71. to be modified if multiple volumes share a single SD card.
  72. */
  73. /* The first time the disk is opened, the SD card can take a while to get
  74. ready, in which time sd_mmc_test_unit_ready() returns either CTRL_BUSY
  75. or CTRL_NO_PRESENT. Try numerous times, waiting half a second after
  76. each failure. Empirically, this has been observed to succeed on the
  77. second try, so trying 10x more than that provides a margin of error.
  78. */
  79. for(ulTries = 0U; ulTries < 20U; ulTries++)
  80. {
  81. cs = sd_mmc_test_unit_ready(sdmmcp);
  82. if((cs != SDMMC_OK) && (cs != SDMMC_BUSY))
  83. {
  84. break;
  85. }
  86. // t_msleep(sdmmcp,500);
  87. }
  88. if(cs == SDMMC_OK)
  89. {
  90. #if REDCONF_READ_ONLY == 0
  91. if(mode != BDEV_O_RDONLY)
  92. {
  93. if(sd_mmc_is_write_protected(sdmmcp))
  94. {
  95. ret = -RED_EROFS;
  96. }
  97. }
  98. if(ret == 0)
  99. #endif
  100. {
  101. uint32_t ulSectorLast;
  102. IGNORE_ERRORS(sd_mmc_read_capacity(sdmmcp, &ulSectorLast));
  103. /* The ASF SD/MMC driver only supports 512-byte sectors.
  104. */
  105. if( (gaRedVolConf[bVolNum].ulSectorSize != 512U)
  106. || (((uint64_t)ulSectorLast + 1U) < gaRedVolConf[bVolNum].ullSectorCount))
  107. {
  108. ret = -RED_EINVAL;
  109. }
  110. }
  111. }
  112. else
  113. {
  114. ret = -RED_EIO;
  115. }
  116. return ret;
  117. }
  118. /** @brief Uninitialize a disk.
  119. @param bVolNum The volume number of the volume whose block device is being
  120. uninitialized.
  121. @return A negated ::REDSTATUS code indicating the operation result.
  122. @retval 0 Operation was successful.
  123. */
  124. static REDSTATUS DiskClose(
  125. uint8_t bVolNum)
  126. {
  127. (void)bVolNum;
  128. return 0;
  129. }
  130. /** @brief Read sectors from a disk.
  131. @param bVolNum The volume number of the volume whose block device
  132. is being read from.
  133. @param ullSectorStart The starting sector number.
  134. @param ulSectorCount The number of sectors to read.
  135. @param pBuffer The buffer into which to read the sector data.
  136. @return A negated ::REDSTATUS code indicating the operation result.
  137. @retval 0 Operation was successful.
  138. */
  139. static REDSTATUS DiskRead(
  140. uint8_t bVolNum,
  141. uint64_t ullSectorStart,
  142. uint32_t ulSectorCount,
  143. void *pBuffer)
  144. {
  145. REDSTATUS ret = 0;
  146. uint8_t *pbBuffer = CAST_VOID_PTR_TO_UINT8_PTR(pBuffer);
  147. SdmmcDriver *sdmmcp = NULL;
  148. eSDMMC_RC cs;
  149. if (!sdmmcGetInstance(bVolNum, &sdmmcp))
  150. return RED_EINVAL;
  151. cs = SD_ReadBlocks(sdmmcp, ullSectorStart, pbBuffer,ulSectorCount);
  152. if(cs != SDMMC_OK)
  153. {
  154. ret = -RED_EIO;
  155. }
  156. return ret;
  157. }
  158. #if REDCONF_READ_ONLY == 0
  159. /** @brief Write sectors to a disk.
  160. @param bVolNum The volume number of the volume whose block device
  161. is being written to.
  162. @param ullSectorStart The starting sector number.
  163. @param ulSectorCount The number of sectors to write.
  164. @param pBuffer The buffer from which to write the sector data.
  165. @return A negated ::REDSTATUS code indicating the operation result.
  166. @retval 0 Operation was successful.
  167. */
  168. static REDSTATUS DiskWrite(
  169. uint8_t bVolNum,
  170. uint64_t ullSectorStart,
  171. uint32_t ulSectorCount,
  172. const void *pBuffer)
  173. {
  174. REDSTATUS ret = 0;
  175. const uint8_t *pbBuffer = CAST_VOID_PTR_TO_CONST_UINT8_PTR(pBuffer);
  176. SdmmcDriver *sdmmcp = NULL;
  177. eSDMMC_RC cs;
  178. if (!sdmmcGetInstance(bVolNum, &sdmmcp))
  179. return RED_EINVAL;
  180. cs = SD_WriteBlocks(sdmmcp, ullSectorStart, pbBuffer, ulSectorCount);
  181. if(cs != SDMMC_OK)
  182. {
  183. ret = -RED_EIO;
  184. }
  185. return ret;
  186. }
  187. /** @brief Flush any caches beneath the file system.
  188. @param bVolNum The volume number of the volume whose block device is being
  189. flushed.
  190. @return A negated ::REDSTATUS code indicating the operation result.
  191. @retval 0 Operation was successful.
  192. */
  193. static REDSTATUS DiskFlush(
  194. uint8_t bVolNum)
  195. {
  196. REDSTATUS ret;
  197. eSDMMC_RC cs;
  198. SdmmcDriver *sdmmcp = NULL;
  199. if (!sdmmcGetInstance(bVolNum, &sdmmcp))
  200. return RED_EINVAL;
  201. /* The ASF SD/MMC driver appears to write sectors synchronously, so it
  202. should be fine to do nothing and return success. However, Atmel's
  203. implementation of the FatFs diskio.c file does the equivalent of the
  204. below when the disk is flushed. Just in case this is important for some
  205. non-obvious reason, do the same.
  206. */
  207. cs = sd_mmc_test_unit_ready(sdmmcp);
  208. if(cs == SDMMC_OK)
  209. {
  210. ret = 0;
  211. }
  212. else
  213. {
  214. ret = -RED_EIO;
  215. }
  216. return ret;
  217. }
  218. #if REDCONF_DISCARDS == 1
  219. /** @brief Discard sectors on a disk.
  220. @param bVolNum The volume number of the volume whose block device
  221. is being accessed.
  222. @param ullSectorStart The starting sector number.
  223. @param ullSectorCount The number of sectors to discard.
  224. */
  225. static void DiskDiscard(
  226. uint8_t bVolNum,
  227. uint64_t ullSectorStart,
  228. uint64_t ullSectorCount)
  229. {
  230. #error "this SD/MMC driver does not support discards."
  231. }
  232. #endif /* REDCONF_DISCARDS == 1 */
  233. #endif /* REDCONF_READ_ONLY == 0 */
  234. /** @brief Initialize a block device.
  235. This function is called when the file system needs access to a block
  236. device.
  237. Upon successful return, the block device should be fully initialized and
  238. ready to service read/write/flush/close requests.
  239. The behavior of calling this function on a block device which is already
  240. open is undefined.
  241. @param bVolNum The volume number of the volume whose block device is being
  242. initialized.
  243. @param mode The open mode, indicating the type of access required.
  244. @return A negated ::REDSTATUS code indicating the operation result.
  245. @retval 0 Operation was successful.
  246. @retval -RED_EINVAL @p bVolNum is an invalid volume number.
  247. @retval -RED_EIO A disk I/O error occurred.
  248. */
  249. REDSTATUS RedOsBDevOpen(
  250. uint8_t bVolNum,
  251. BDEVOPENMODE mode)
  252. {
  253. REDSTATUS ret;
  254. if(bVolNum >= REDCONF_VOLUME_COUNT)
  255. {
  256. ret = -RED_EINVAL;
  257. }
  258. else
  259. {
  260. ret = DiskOpen(bVolNum, mode);
  261. }
  262. return ret;
  263. }
  264. /** @brief Uninitialize a block device.
  265. This function is called when the file system no longer needs access to a
  266. block device. If any resource were allocated by RedOsBDevOpen() to service
  267. block device requests, they should be freed at this time.
  268. Upon successful return, the block device must be in such a state that it
  269. can be opened again.
  270. The behavior of calling this function on a block device which is already
  271. closed is undefined.
  272. @param bVolNum The volume number of the volume whose block device is being
  273. uninitialized.
  274. @return A negated ::REDSTATUS code indicating the operation result.
  275. @retval 0 Operation was successful.
  276. @retval -RED_EINVAL @p bVolNum is an invalid volume number.
  277. */
  278. REDSTATUS RedOsBDevClose(
  279. uint8_t bVolNum)
  280. {
  281. REDSTATUS ret;
  282. if(bVolNum >= REDCONF_VOLUME_COUNT)
  283. {
  284. ret = -RED_EINVAL;
  285. }
  286. else
  287. {
  288. ret = DiskClose(bVolNum);
  289. }
  290. return ret;
  291. }
  292. /** @brief Read sectors from a physical block device.
  293. The behavior of calling this function is undefined if the block device is
  294. closed or if it was opened with ::BDEV_O_WRONLY.
  295. @param bVolNum The volume number of the volume whose block device
  296. is being read from.
  297. @param ullSectorStart The starting sector number.
  298. @param ulSectorCount The number of sectors to read.
  299. @param pBuffer The buffer into which to read the sector data.
  300. @return A negated ::REDSTATUS code indicating the operation result.
  301. @retval 0 Operation was successful.
  302. @retval -RED_EINVAL @p bVolNum is an invalid volume number, @p pBuffer is
  303. `NULL`, or @p ullStartSector and/or @p ulSectorCount
  304. refer to an invalid range of sectors.
  305. @retval -RED_EIO A disk I/O error occurred.
  306. */
  307. REDSTATUS RedOsBDevRead(
  308. uint8_t bVolNum,
  309. uint64_t ullSectorStart,
  310. uint32_t ulSectorCount,
  311. void *pBuffer)
  312. {
  313. REDSTATUS ret = 0;
  314. if( (bVolNum >= REDCONF_VOLUME_COUNT)
  315. || (ullSectorStart >= gaRedVolConf[bVolNum].ullSectorCount)
  316. || ((gaRedVolConf[bVolNum].ullSectorCount - ullSectorStart) < ulSectorCount)
  317. || (pBuffer == NULL))
  318. {
  319. ret = -RED_EINVAL;
  320. }
  321. else
  322. {
  323. ret = DiskRead(bVolNum, ullSectorStart, ulSectorCount, pBuffer);
  324. }
  325. return ret;
  326. }
  327. #if REDCONF_READ_ONLY == 0
  328. /** @brief Write sectors to a physical block device.
  329. The behavior of calling this function is undefined if the block device is
  330. closed or if it was opened with ::BDEV_O_RDONLY.
  331. @param bVolNum The volume number of the volume whose block device
  332. is being written to.
  333. @param ullSectorStart The starting sector number.
  334. @param ulSectorCount The number of sectors to write.
  335. @param pBuffer The buffer from which to write the sector data.
  336. @return A negated ::REDSTATUS code indicating the operation result.
  337. @retval 0 Operation was successful.
  338. @retval -RED_EINVAL @p bVolNum is an invalid volume number, @p pBuffer is
  339. `NULL`, or @p ullStartSector and/or @p ulSectorCount
  340. refer to an invalid range of sectors.
  341. @retval -RED_EIO A disk I/O error occurred.
  342. */
  343. REDSTATUS RedOsBDevWrite(
  344. uint8_t bVolNum,
  345. uint64_t ullSectorStart,
  346. uint32_t ulSectorCount,
  347. const void *pBuffer)
  348. {
  349. REDSTATUS ret = 0;
  350. if( (bVolNum >= REDCONF_VOLUME_COUNT)
  351. || (ullSectorStart >= gaRedVolConf[bVolNum].ullSectorCount)
  352. || ((gaRedVolConf[bVolNum].ullSectorCount - ullSectorStart) < ulSectorCount)
  353. || (pBuffer == NULL))
  354. {
  355. ret = -RED_EINVAL;
  356. }
  357. else
  358. {
  359. ret = DiskWrite(bVolNum, ullSectorStart, ulSectorCount, pBuffer);
  360. }
  361. return ret;
  362. }
  363. /** @brief Flush any caches beneath the file system.
  364. This function must synchronously flush all software and hardware caches
  365. beneath the file system, ensuring that all sectors written previously are
  366. committed to permanent storage.
  367. If the environment has no caching beneath the file system, the
  368. implementation of this function can do nothing and return success.
  369. The behavior of calling this function is undefined if the block device is
  370. closed or if it was opened with ::BDEV_O_RDONLY.
  371. @param bVolNum The volume number of the volume whose block device is being
  372. flushed.
  373. @return A negated ::REDSTATUS code indicating the operation result.
  374. @retval 0 Operation was successful.
  375. @retval -RED_EINVAL @p bVolNum is an invalid volume number.
  376. @retval -RED_EIO A disk I/O error occurred.
  377. */
  378. REDSTATUS RedOsBDevFlush(
  379. uint8_t bVolNum)
  380. {
  381. REDSTATUS ret;
  382. if(bVolNum >= REDCONF_VOLUME_COUNT)
  383. {
  384. ret = -RED_EINVAL;
  385. }
  386. else
  387. {
  388. ret = DiskFlush(bVolNum);
  389. }
  390. return ret;
  391. }
  392. #endif /* REDCONF_READ_ONLY == 0 */
  393. #endif
  394. #endif